/* image_utils.c generated by valac 0.56.19, the Vala compiler
 * generated from image_utils.vala, do not modify */

#include <pixbuf_animation_compat.h>
#include <stdlib.h>
#include <string.h>
#include <glib.h>
#include <gdk-pixbuf/gdk-pixbuf.h>
#include <float.h>
#include <math.h>
#include <gdk/gdk.h>

#if !defined(VALA_STRICT_C)
#if !defined(__clang__) && defined(__GNUC__) && (__GNUC__ >= 14)
#pragma GCC diagnostic warning "-Wincompatible-pointer-types"
#elif defined(__clang__) && (__clang_major__ >= 16)
#pragma clang diagnostic ignored "-Wincompatible-function-pointer-types"
#pragma clang diagnostic ignored "-Wincompatible-pointer-types"
#endif
#endif
#if !defined(VALA_EXTERN)
#if defined(_MSC_VER)
#define VALA_EXTERN __declspec(dllexport) extern
#elif __GNUC__ >= 4
#define VALA_EXTERN __attribute__((visibility("default"))) extern
#else
#define VALA_EXTERN extern
#endif
#endif

#define _g_object_unref0(var) ((var == NULL) ? NULL : (var = (g_object_unref (var), NULL)))
#define _g_bytes_unref0(var) ((var == NULL) ? NULL : (var = (g_bytes_unref (var), NULL)))

VALA_EXTERN GdkPixbuf* dc_scaled_frame_from_file (const gchar* path,
                                      gint max_px,
                                      GError** error);
VALA_EXTERN GdkTexture* dc_texture_from_pixbuf (GdkPixbuf* pixbuf);
static inline gpointer _vala_memdup2 (gconstpointer mem,
                        gsize byte_size);

/** Static first frame of an image file, downscaled to fit max_px in
        both dimensions (never upscaled). PixbufAnimation copes with
        animated GIF/WebP, where Pixbuf.from_file_at_scale refuses
        multi-frame files (stickers, typically). */
static gpointer
_g_object_ref0 (gpointer self)
{
	return self ? g_object_ref (self) : NULL;
}

GdkPixbuf*
dc_scaled_frame_from_file (const gchar* path,
                           gint max_px,
                           GError** error)
{
	GdkPixbufAnimation* anim = NULL;
	GdkPixbufAnimation* _tmp0_;
	GdkPixbuf* frame = NULL;
	GdkPixbuf* _tmp1_;
	GdkPixbuf* _tmp2_;
	gdouble scale = 0.0;
	GdkPixbuf* _tmp3_;
	gint _tmp4_;
	gint _tmp5_;
	GdkPixbuf* _tmp6_;
	gint _tmp7_;
	gint _tmp8_;
	GError* _inner_error0_ = NULL;
	GdkPixbuf* result;
	g_return_val_if_fail (path != NULL, NULL);
	_tmp0_ = parla_pixbuf_animation_load (path, &_inner_error0_);
	anim = _tmp0_;
	if (G_UNLIKELY (_inner_error0_ != NULL)) {
		g_propagate_error (error, _inner_error0_);
		return NULL;
	}
	_tmp1_ = parla_pixbuf_animation_get_static_image (anim);
	_tmp2_ = _g_object_ref0 (_tmp1_);
	frame = _tmp2_;
	_tmp3_ = frame;
	_tmp4_ = gdk_pixbuf_get_width (_tmp3_);
	_tmp5_ = _tmp4_;
	_tmp6_ = frame;
	_tmp7_ = gdk_pixbuf_get_height (_tmp6_);
	_tmp8_ = _tmp7_;
	scale = MIN (1.0, MIN (((gdouble) max_px) / _tmp5_, ((gdouble) max_px) / _tmp8_));
	if (scale < 1.0) {
		GdkPixbuf* _tmp9_;
		GdkPixbuf* _tmp10_;
		gint _tmp11_;
		gint _tmp12_;
		GdkPixbuf* _tmp13_;
		gint _tmp14_;
		gint _tmp15_;
		GdkPixbuf* _tmp16_;
		_tmp9_ = frame;
		_tmp10_ = frame;
		_tmp11_ = gdk_pixbuf_get_width (_tmp10_);
		_tmp12_ = _tmp11_;
		_tmp13_ = frame;
		_tmp14_ = gdk_pixbuf_get_height (_tmp13_);
		_tmp15_ = _tmp14_;
		_tmp16_ = gdk_pixbuf_scale_simple (_tmp9_, MAX (1, (gint) ((_tmp12_ * scale) + 0.5)), MAX (1, (gint) ((_tmp15_ * scale) + 0.5)), GDK_INTERP_BILINEAR);
		_g_object_unref0 (frame);
		frame = _tmp16_;
	}
	result = frame;
	_g_object_unref0 (anim);
	return result;
}

GdkTexture*
dc_texture_from_pixbuf (GdkPixbuf* pixbuf)
{
	GdkMemoryFormat _tmp0_ = 0;
	gboolean _tmp1_;
	gboolean _tmp2_;
	GdkMemoryFormat format = 0;
	gint _tmp3_;
	gint _tmp4_;
	gint _tmp5_;
	gint _tmp6_;
	GBytes* _tmp7_;
	GBytes* _tmp8_;
	gint _tmp9_;
	gint _tmp10_;
	GdkMemoryTexture* _tmp11_;
	GdkTexture* _tmp12_;
	GdkTexture* result;
	g_return_val_if_fail (pixbuf != NULL, NULL);
	_tmp1_ = gdk_pixbuf_get_has_alpha (pixbuf);
	_tmp2_ = _tmp1_;
	if (_tmp2_) {
		_tmp0_ = GDK_MEMORY_R8G8B8A8;
	} else {
		_tmp0_ = GDK_MEMORY_R8G8B8;
	}
	format = _tmp0_;
	_tmp3_ = gdk_pixbuf_get_width (pixbuf);
	_tmp4_ = _tmp3_;
	_tmp5_ = gdk_pixbuf_get_height (pixbuf);
	_tmp6_ = _tmp5_;
	_tmp7_ = gdk_pixbuf_read_pixel_bytes (pixbuf);
	_tmp8_ = _tmp7_;
	_tmp9_ = gdk_pixbuf_get_rowstride (pixbuf);
	_tmp10_ = _tmp9_;
	_tmp11_ = (GdkMemoryTexture*) gdk_memory_texture_new (_tmp4_, _tmp6_, format, _tmp8_, (gsize) _tmp10_);
	_tmp12_ = (GdkTexture*) _tmp11_;
	_g_bytes_unref0 (_tmp8_);
	result = _tmp12_;
	return result;
}

static inline gpointer
_vala_memdup2 (gconstpointer mem,
               gsize byte_size)
{
	gpointer new_mem;
	if (mem && byte_size != 0) {
		new_mem = g_malloc (byte_size);
		memcpy (new_mem, mem, byte_size);
	} else {
		new_mem = NULL;
	}
	return new_mem;
}

