/* gitg-diff-image-difference.c generated by valac 0.34.7, the Vala compiler
 * generated from gitg-diff-image-difference.vala, do not modify */

/*
 * This file is part of gitg
 *
 * Copyright (C) 2016 - Jesse van den Kieboom
 *
 * gitg is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * gitg is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with gitg. If not, see <http://www.gnu.org/licenses/>.
 */

#include <glib.h>
#include <glib-object.h>
#include <gtk/gtk.h>
#include <cairo.h>
#include <gdk/gdk.h>
#include <gdk-pixbuf/gdk-pixbuf.h>
#include <float.h>
#include <math.h>


#define GITG_TYPE_DIFF_IMAGE_COMPOSITE (gitg_diff_image_composite_get_type ())
#define GITG_DIFF_IMAGE_COMPOSITE(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), GITG_TYPE_DIFF_IMAGE_COMPOSITE, GitgDiffImageComposite))
#define GITG_DIFF_IMAGE_COMPOSITE_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), GITG_TYPE_DIFF_IMAGE_COMPOSITE, GitgDiffImageCompositeClass))
#define GITG_IS_DIFF_IMAGE_COMPOSITE(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), GITG_TYPE_DIFF_IMAGE_COMPOSITE))
#define GITG_IS_DIFF_IMAGE_COMPOSITE_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), GITG_TYPE_DIFF_IMAGE_COMPOSITE))
#define GITG_DIFF_IMAGE_COMPOSITE_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), GITG_TYPE_DIFF_IMAGE_COMPOSITE, GitgDiffImageCompositeClass))

typedef struct _GitgDiffImageComposite GitgDiffImageComposite;
typedef struct _GitgDiffImageCompositeClass GitgDiffImageCompositeClass;
typedef struct _GitgDiffImageCompositePrivate GitgDiffImageCompositePrivate;

#define GITG_TYPE_DIFF_IMAGE_DIFFERENCE (gitg_diff_image_difference_get_type ())
#define GITG_DIFF_IMAGE_DIFFERENCE(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), GITG_TYPE_DIFF_IMAGE_DIFFERENCE, GitgDiffImageDifference))
#define GITG_DIFF_IMAGE_DIFFERENCE_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), GITG_TYPE_DIFF_IMAGE_DIFFERENCE, GitgDiffImageDifferenceClass))
#define GITG_IS_DIFF_IMAGE_DIFFERENCE(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), GITG_TYPE_DIFF_IMAGE_DIFFERENCE))
#define GITG_IS_DIFF_IMAGE_DIFFERENCE_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), GITG_TYPE_DIFF_IMAGE_DIFFERENCE))
#define GITG_DIFF_IMAGE_DIFFERENCE_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), GITG_TYPE_DIFF_IMAGE_DIFFERENCE, GitgDiffImageDifferenceClass))

typedef struct _GitgDiffImageDifference GitgDiffImageDifference;
typedef struct _GitgDiffImageDifferenceClass GitgDiffImageDifferenceClass;
typedef struct _GitgDiffImageDifferencePrivate GitgDiffImageDifferencePrivate;

#define GITG_TYPE_DIFF_IMAGE_SURFACE_CACHE (gitg_diff_image_surface_cache_get_type ())
#define GITG_DIFF_IMAGE_SURFACE_CACHE(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), GITG_TYPE_DIFF_IMAGE_SURFACE_CACHE, GitgDiffImageSurfaceCache))
#define GITG_IS_DIFF_IMAGE_SURFACE_CACHE(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), GITG_TYPE_DIFF_IMAGE_SURFACE_CACHE))
#define GITG_DIFF_IMAGE_SURFACE_CACHE_GET_INTERFACE(obj) (G_TYPE_INSTANCE_GET_INTERFACE ((obj), GITG_TYPE_DIFF_IMAGE_SURFACE_CACHE, GitgDiffImageSurfaceCacheIface))

typedef struct _GitgDiffImageSurfaceCache GitgDiffImageSurfaceCache;
typedef struct _GitgDiffImageSurfaceCacheIface GitgDiffImageSurfaceCacheIface;
#define _cairo_surface_destroy0(var) ((var == NULL) ? NULL : (var = (cairo_surface_destroy (var), NULL)))
#define _g_object_unref0(var) ((var == NULL) ? NULL : (var = (g_object_unref (var), NULL)))

struct _GitgDiffImageComposite {
	GtkDrawingArea parent_instance;
	GitgDiffImageCompositePrivate * priv;
};

struct _GitgDiffImageCompositeClass {
	GtkDrawingAreaClass parent_class;
};

struct _GitgDiffImageDifference {
	GitgDiffImageComposite parent_instance;
	GitgDiffImageDifferencePrivate * priv;
};

struct _GitgDiffImageDifferenceClass {
	GitgDiffImageCompositeClass parent_class;
};

struct _GitgDiffImageSurfaceCacheIface {
	GTypeInterface parent_iface;
	cairo_surface_t* (*get_old_surface) (GitgDiffImageSurfaceCache* self, GdkWindow* window);
	cairo_surface_t* (*get_new_surface) (GitgDiffImageSurfaceCache* self, GdkWindow* window);
	GdkPixbuf* (*get_old_pixbuf) (GitgDiffImageSurfaceCache* self);
	void (*set_old_pixbuf) (GitgDiffImageSurfaceCache* self, GdkPixbuf* value);
	GdkPixbuf* (*get_new_pixbuf) (GitgDiffImageSurfaceCache* self);
	void (*set_new_pixbuf) (GitgDiffImageSurfaceCache* self, GdkPixbuf* value);
	GdkWindow* (*get_window) (GitgDiffImageSurfaceCache* self);
	void (*set_window) (GitgDiffImageSurfaceCache* self, GdkWindow* value);
};


static gpointer gitg_diff_image_difference_parent_class = NULL;

GType gitg_diff_image_composite_get_type (void) G_GNUC_CONST;
GType gitg_diff_image_difference_get_type (void) G_GNUC_CONST;
enum  {
	GITG_DIFF_IMAGE_DIFFERENCE_DUMMY_PROPERTY
};
static gboolean gitg_diff_image_difference_real_draw (GtkWidget* base, cairo_t* cr);
void gitg_diff_image_composite_get_sizing (GitgDiffImageComposite* self, gint width, gint* image_width, gint* image_height);
GType gitg_diff_image_surface_cache_get_type (void) G_GNUC_CONST;
GitgDiffImageSurfaceCache* gitg_diff_image_composite_get_cache (GitgDiffImageComposite* self);
cairo_surface_t* gitg_diff_image_surface_cache_get_old_surface (GitgDiffImageSurfaceCache* self, GdkWindow* window);
cairo_surface_t* gitg_diff_image_surface_cache_get_new_surface (GitgDiffImageSurfaceCache* self, GdkWindow* window);
static void gitg_diff_image_difference_real_realize (GtkWidget* base);
GitgDiffImageDifference* gitg_diff_image_difference_new (void);
GitgDiffImageDifference* gitg_diff_image_difference_construct (GType object_type);
GitgDiffImageComposite* gitg_diff_image_composite_new (void);
GitgDiffImageComposite* gitg_diff_image_composite_construct (GType object_type);


static gpointer _g_object_ref0 (gpointer self) {
#line 26 "/home/jesse/dev/gitg/libgitg/gitg-diff-image-difference.vala"
	return self ? g_object_ref (self) : NULL;
#line 120 "gitg-diff-image-difference.c"
}


static gboolean gitg_diff_image_difference_real_draw (GtkWidget* base, cairo_t* cr) {
	GitgDiffImageDifference * self;
	gboolean result = FALSE;
	cairo_t* _tmp0_ = NULL;
	GdkWindow* window = NULL;
	GdkWindow* _tmp1_ = NULL;
	GdkWindow* _tmp2_ = NULL;
	GtkAllocation alloc = {0};
	GtkAllocation _tmp3_ = {0};
	gint image_width = 0;
	gint image_height = 0;
	GtkAllocation _tmp4_ = {0};
	gint _tmp5_ = 0;
	gint _tmp6_ = 0;
	gint _tmp7_ = 0;
	cairo_surface_t* old_surface = NULL;
	GitgDiffImageSurfaceCache* _tmp8_ = NULL;
	GitgDiffImageSurfaceCache* _tmp9_ = NULL;
	GdkWindow* _tmp10_ = NULL;
	cairo_surface_t* _tmp11_ = NULL;
	cairo_surface_t* new_surface = NULL;
	GitgDiffImageSurfaceCache* _tmp12_ = NULL;
	GitgDiffImageSurfaceCache* _tmp13_ = NULL;
	GdkWindow* _tmp14_ = NULL;
	cairo_surface_t* _tmp15_ = NULL;
	gint x = 0;
	GtkAllocation _tmp16_ = {0};
	gint _tmp17_ = 0;
	gint _tmp18_ = 0;
	gint y = 0;
	cairo_surface_t* _tmp19_ = NULL;
	cairo_surface_t* _tmp25_ = NULL;
#line 22 "/home/jesse/dev/gitg/libgitg/gitg-diff-image-difference.vala"
	self = (GitgDiffImageDifference*) base;
#line 22 "/home/jesse/dev/gitg/libgitg/gitg-diff-image-difference.vala"
	g_return_val_if_fail (cr != NULL, FALSE);
#line 24 "/home/jesse/dev/gitg/libgitg/gitg-diff-image-difference.vala"
	_tmp0_ = cr;
#line 24 "/home/jesse/dev/gitg/libgitg/gitg-diff-image-difference.vala"
	GTK_WIDGET_CLASS (gitg_diff_image_difference_parent_class)->draw ((GtkWidget*) G_TYPE_CHECK_INSTANCE_CAST (self, GITG_TYPE_DIFF_IMAGE_COMPOSITE, GitgDiffImageComposite), _tmp0_);
#line 26 "/home/jesse/dev/gitg/libgitg/gitg-diff-image-difference.vala"
	_tmp1_ = gtk_widget_get_window ((GtkWidget*) self);
#line 26 "/home/jesse/dev/gitg/libgitg/gitg-diff-image-difference.vala"
	_tmp2_ = _g_object_ref0 (_tmp1_);
#line 26 "/home/jesse/dev/gitg/libgitg/gitg-diff-image-difference.vala"
	window = _tmp2_;
#line 29 "/home/jesse/dev/gitg/libgitg/gitg-diff-image-difference.vala"
	gtk_widget_get_allocation ((GtkWidget*) self, &_tmp3_);
#line 29 "/home/jesse/dev/gitg/libgitg/gitg-diff-image-difference.vala"
	alloc = _tmp3_;
#line 32 "/home/jesse/dev/gitg/libgitg/gitg-diff-image-difference.vala"
	_tmp4_ = alloc;
#line 32 "/home/jesse/dev/gitg/libgitg/gitg-diff-image-difference.vala"
	_tmp5_ = _tmp4_.width;
#line 32 "/home/jesse/dev/gitg/libgitg/gitg-diff-image-difference.vala"
	gitg_diff_image_composite_get_sizing ((GitgDiffImageComposite*) self, _tmp5_, &_tmp6_, &_tmp7_);
#line 32 "/home/jesse/dev/gitg/libgitg/gitg-diff-image-difference.vala"
	image_width = _tmp6_;
#line 32 "/home/jesse/dev/gitg/libgitg/gitg-diff-image-difference.vala"
	image_height = _tmp7_;
#line 34 "/home/jesse/dev/gitg/libgitg/gitg-diff-image-difference.vala"
	_tmp8_ = gitg_diff_image_composite_get_cache ((GitgDiffImageComposite*) self);
#line 34 "/home/jesse/dev/gitg/libgitg/gitg-diff-image-difference.vala"
	_tmp9_ = _tmp8_;
#line 34 "/home/jesse/dev/gitg/libgitg/gitg-diff-image-difference.vala"
	_tmp10_ = window;
#line 34 "/home/jesse/dev/gitg/libgitg/gitg-diff-image-difference.vala"
	_tmp11_ = gitg_diff_image_surface_cache_get_old_surface (_tmp9_, _tmp10_);
#line 34 "/home/jesse/dev/gitg/libgitg/gitg-diff-image-difference.vala"
	old_surface = _tmp11_;
#line 35 "/home/jesse/dev/gitg/libgitg/gitg-diff-image-difference.vala"
	_tmp12_ = gitg_diff_image_composite_get_cache ((GitgDiffImageComposite*) self);
#line 35 "/home/jesse/dev/gitg/libgitg/gitg-diff-image-difference.vala"
	_tmp13_ = _tmp12_;
#line 35 "/home/jesse/dev/gitg/libgitg/gitg-diff-image-difference.vala"
	_tmp14_ = window;
#line 35 "/home/jesse/dev/gitg/libgitg/gitg-diff-image-difference.vala"
	_tmp15_ = gitg_diff_image_surface_cache_get_new_surface (_tmp13_, _tmp14_);
#line 35 "/home/jesse/dev/gitg/libgitg/gitg-diff-image-difference.vala"
	new_surface = _tmp15_;
#line 37 "/home/jesse/dev/gitg/libgitg/gitg-diff-image-difference.vala"
	_tmp16_ = alloc;
#line 37 "/home/jesse/dev/gitg/libgitg/gitg-diff-image-difference.vala"
	_tmp17_ = _tmp16_.width;
#line 37 "/home/jesse/dev/gitg/libgitg/gitg-diff-image-difference.vala"
	_tmp18_ = image_width;
#line 37 "/home/jesse/dev/gitg/libgitg/gitg-diff-image-difference.vala"
	x = (_tmp17_ - _tmp18_) / 2;
#line 38 "/home/jesse/dev/gitg/libgitg/gitg-diff-image-difference.vala"
	y = 0;
#line 40 "/home/jesse/dev/gitg/libgitg/gitg-diff-image-difference.vala"
	_tmp19_ = old_surface;
#line 40 "/home/jesse/dev/gitg/libgitg/gitg-diff-image-difference.vala"
	if (_tmp19_ != NULL) {
#line 218 "gitg-diff-image-difference.c"
		cairo_t* _tmp20_ = NULL;
		cairo_surface_t* _tmp21_ = NULL;
		gint _tmp22_ = 0;
		gint _tmp23_ = 0;
		cairo_t* _tmp24_ = NULL;
#line 42 "/home/jesse/dev/gitg/libgitg/gitg-diff-image-difference.vala"
		_tmp20_ = cr;
#line 42 "/home/jesse/dev/gitg/libgitg/gitg-diff-image-difference.vala"
		_tmp21_ = old_surface;
#line 42 "/home/jesse/dev/gitg/libgitg/gitg-diff-image-difference.vala"
		_tmp22_ = x;
#line 42 "/home/jesse/dev/gitg/libgitg/gitg-diff-image-difference.vala"
		_tmp23_ = y;
#line 42 "/home/jesse/dev/gitg/libgitg/gitg-diff-image-difference.vala"
		cairo_set_source_surface (_tmp20_, _tmp21_, (gdouble) _tmp22_, (gdouble) _tmp23_);
#line 43 "/home/jesse/dev/gitg/libgitg/gitg-diff-image-difference.vala"
		_tmp24_ = cr;
#line 43 "/home/jesse/dev/gitg/libgitg/gitg-diff-image-difference.vala"
		cairo_paint (_tmp24_);
#line 238 "gitg-diff-image-difference.c"
	}
#line 46 "/home/jesse/dev/gitg/libgitg/gitg-diff-image-difference.vala"
	_tmp25_ = new_surface;
#line 46 "/home/jesse/dev/gitg/libgitg/gitg-diff-image-difference.vala"
	if (_tmp25_ != NULL) {
#line 244 "gitg-diff-image-difference.c"
		cairo_t* _tmp26_ = NULL;
		cairo_t* _tmp33_ = NULL;
#line 48 "/home/jesse/dev/gitg/libgitg/gitg-diff-image-difference.vala"
		_tmp26_ = cr;
#line 48 "/home/jesse/dev/gitg/libgitg/gitg-diff-image-difference.vala"
		cairo_save (_tmp26_);
#line 251 "gitg-diff-image-difference.c"
		{
			cairo_t* _tmp27_ = NULL;
			cairo_t* _tmp28_ = NULL;
			cairo_surface_t* _tmp29_ = NULL;
			gint _tmp30_ = 0;
			gint _tmp31_ = 0;
			cairo_t* _tmp32_ = NULL;
#line 50 "/home/jesse/dev/gitg/libgitg/gitg-diff-image-difference.vala"
			_tmp27_ = cr;
#line 50 "/home/jesse/dev/gitg/libgitg/gitg-diff-image-difference.vala"
			cairo_set_operator (_tmp27_, CAIRO_OPERATOR_DIFFERENCE);
#line 51 "/home/jesse/dev/gitg/libgitg/gitg-diff-image-difference.vala"
			_tmp28_ = cr;
#line 51 "/home/jesse/dev/gitg/libgitg/gitg-diff-image-difference.vala"
			_tmp29_ = new_surface;
#line 51 "/home/jesse/dev/gitg/libgitg/gitg-diff-image-difference.vala"
			_tmp30_ = x;
#line 51 "/home/jesse/dev/gitg/libgitg/gitg-diff-image-difference.vala"
			_tmp31_ = y;
#line 51 "/home/jesse/dev/gitg/libgitg/gitg-diff-image-difference.vala"
			cairo_set_source_surface (_tmp28_, _tmp29_, (gdouble) _tmp30_, (gdouble) _tmp31_);
#line 52 "/home/jesse/dev/gitg/libgitg/gitg-diff-image-difference.vala"
			_tmp32_ = cr;
#line 52 "/home/jesse/dev/gitg/libgitg/gitg-diff-image-difference.vala"
			cairo_paint (_tmp32_);
#line 277 "gitg-diff-image-difference.c"
		}
#line 54 "/home/jesse/dev/gitg/libgitg/gitg-diff-image-difference.vala"
		_tmp33_ = cr;
#line 54 "/home/jesse/dev/gitg/libgitg/gitg-diff-image-difference.vala"
		cairo_restore (_tmp33_);
#line 283 "gitg-diff-image-difference.c"
	}
#line 57 "/home/jesse/dev/gitg/libgitg/gitg-diff-image-difference.vala"
	result = TRUE;
#line 57 "/home/jesse/dev/gitg/libgitg/gitg-diff-image-difference.vala"
	_cairo_surface_destroy0 (new_surface);
#line 57 "/home/jesse/dev/gitg/libgitg/gitg-diff-image-difference.vala"
	_cairo_surface_destroy0 (old_surface);
#line 57 "/home/jesse/dev/gitg/libgitg/gitg-diff-image-difference.vala"
	_g_object_unref0 (window);
#line 57 "/home/jesse/dev/gitg/libgitg/gitg-diff-image-difference.vala"
	return result;
#line 295 "gitg-diff-image-difference.c"
}


static void gitg_diff_image_difference_real_realize (GtkWidget* base) {
	GitgDiffImageDifference * self;
#line 60 "/home/jesse/dev/gitg/libgitg/gitg-diff-image-difference.vala"
	self = (GitgDiffImageDifference*) base;
#line 62 "/home/jesse/dev/gitg/libgitg/gitg-diff-image-difference.vala"
	GTK_WIDGET_CLASS (gitg_diff_image_difference_parent_class)->realize ((GtkWidget*) G_TYPE_CHECK_INSTANCE_CAST (self, GITG_TYPE_DIFF_IMAGE_COMPOSITE, GitgDiffImageComposite));
#line 63 "/home/jesse/dev/gitg/libgitg/gitg-diff-image-difference.vala"
	gtk_widget_queue_resize ((GtkWidget*) self);
#line 307 "gitg-diff-image-difference.c"
}


GitgDiffImageDifference* gitg_diff_image_difference_construct (GType object_type) {
	GitgDiffImageDifference * self = NULL;
#line 20 "/home/jesse/dev/gitg/libgitg/gitg-diff-image-difference.vala"
	self = (GitgDiffImageDifference*) gitg_diff_image_composite_construct (object_type);
#line 20 "/home/jesse/dev/gitg/libgitg/gitg-diff-image-difference.vala"
	return self;
#line 317 "gitg-diff-image-difference.c"
}


GitgDiffImageDifference* gitg_diff_image_difference_new (void) {
#line 20 "/home/jesse/dev/gitg/libgitg/gitg-diff-image-difference.vala"
	return gitg_diff_image_difference_construct (GITG_TYPE_DIFF_IMAGE_DIFFERENCE);
#line 324 "gitg-diff-image-difference.c"
}


static void gitg_diff_image_difference_class_init (GitgDiffImageDifferenceClass * klass) {
#line 20 "/home/jesse/dev/gitg/libgitg/gitg-diff-image-difference.vala"
	gitg_diff_image_difference_parent_class = g_type_class_peek_parent (klass);
#line 20 "/home/jesse/dev/gitg/libgitg/gitg-diff-image-difference.vala"
	((GtkWidgetClass *) klass)->draw = (gboolean (*)(GtkWidget*, cairo_t*)) gitg_diff_image_difference_real_draw;
#line 20 "/home/jesse/dev/gitg/libgitg/gitg-diff-image-difference.vala"
	((GtkWidgetClass *) klass)->realize = (void (*)(GtkWidget*)) gitg_diff_image_difference_real_realize;
#line 335 "gitg-diff-image-difference.c"
}


static void gitg_diff_image_difference_instance_init (GitgDiffImageDifference * self) {
}


GType gitg_diff_image_difference_get_type (void) {
	static volatile gsize gitg_diff_image_difference_type_id__volatile = 0;
	if (g_once_init_enter (&gitg_diff_image_difference_type_id__volatile)) {
		static const GTypeInfo g_define_type_info = { sizeof (GitgDiffImageDifferenceClass), (GBaseInitFunc) NULL, (GBaseFinalizeFunc) NULL, (GClassInitFunc) gitg_diff_image_difference_class_init, (GClassFinalizeFunc) NULL, NULL, sizeof (GitgDiffImageDifference), 0, (GInstanceInitFunc) gitg_diff_image_difference_instance_init, NULL };
		GType gitg_diff_image_difference_type_id;
		gitg_diff_image_difference_type_id = g_type_register_static (GITG_TYPE_DIFF_IMAGE_COMPOSITE, "GitgDiffImageDifference", &g_define_type_info, 0);
		g_once_init_leave (&gitg_diff_image_difference_type_id__volatile, gitg_diff_image_difference_type_id);
	}
	return gitg_diff_image_difference_type_id__volatile;
}



