1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
/* This file is part of CTK+
 *
 * AUTHORS
 *     Sven Herzberg
 *
 * Copyright (C) 2008  Sven Herzberg
 *
 * This work is provided "as is"; redistribution and modification
 * in whole or in part, in any medium, physical or electronic is
 * permitted without restriction.
 *
 * This work 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.
 *
 * In no event shall the authors or contributors be liable for any
 * direct, indirect, incidental, special, exemplary, or consequential
 * damages (including, but not limited to, procurement of substitute
 * goods or services; loss of use, data, or profits; or business
 * interruption) however caused and on any theory of liability, whether
 * in contract, strict liability, or tort (including negligence or
 * otherwise) arising in any way out of the use of this software, even
 * if advised of the possibility of such damage.
 */

#include <glib/gstdio.h>
#include <cdk/cdk.h>
#ifdef CAIRO_HAS_QUARTZ_SURFACE
#include <cairo-quartz.h>
#endif

static void
test (cairo_t* cr)
{
	cairo_save (cr);
	 cairo_set_source_rgb (cr, 1.0, 1.0, 1.0);
	 cairo_paint (cr);
	cairo_restore (cr);

	cairo_move_to (cr, 10.0, 20.0);
	cairo_line_to (cr, 10.0, 30.0);
	cairo_stroke (cr);

	cairo_set_source_rgba (cr, 0.0, 0.0, 0.0, 0.5);
	cairo_arc (cr, 0.0, 0.0, 10.0, 0.0, G_PI/2);
	cairo_stroke (cr);
}

static void
test_surface_orientation (void)
{
	cairo_surface_t *surface;<--- Unused variable: surface
	GdkPixbuf* pixbuf;
	GdkPixbuf* pbuf_platform;
	GdkPixbuf* pbuf_imagesrf;
	GError   * error = NULL;
	cairo_surface_t* surface;
	cairo_t* cr;
	guchar* data_platform;<--- Variable 'data_platform' can be declared as pointer to const
	guchar* data_imagesrf;<--- Variable 'data_imagesrf' can be declared as pointer to const
	guint i;

	/* create "platform.png" via native cairo surface */
	surface = cdk_window_create_similar_surface (cdk_get_default_root_window (),
                                                     CAIRO_CONTENT_COLOR,
                                                     100,
                                                     80);
	cr = cairo_create (surface);
	test (cr);
	cairo_destroy (cr);

	pixbuf = gdk_pixbuf_get_from_surface (NULL,
					      surface,
					      0, 0,
					      0, 0,
					      100, 80);
	if (!gdk_pixbuf_save (pixbuf, "cdksurface.png", "png", NULL, NULL)) {
		g_error ("Eeek! Couldn't save the file \"cdksurface.png\"");
	}
	g_object_unref (pixbuf);

	cairo_surface_destroy (surface);

	/* create "cairosurface.png" via pure cairo */
#ifndef CAIRO_HAS_QUARTZ_SURFACE
	surface = cairo_image_surface_create (CAIRO_FORMAT_RGB24, 100, 80);
#else
	surface = cairo_quartz_surface_create (CAIRO_FORMAT_RGB24, 100, 80);
#endif
	cr = cairo_create (surface);
	test (cr);
	cairo_destroy (cr);
	if (CAIRO_STATUS_SUCCESS != cairo_surface_write_to_png (surface, "cairosurface.png")) {
		g_error ("Eeek! Couldn't save the file \"cairosurface.png\"");
	}
	cairo_surface_destroy (surface);

	/* compare the images */
	pbuf_platform = gdk_pixbuf_new_from_file ("cdksurface.png", &error);
	if (!pbuf_platform || error) {
		g_error ("Eeek! Error loading \"cdksurface.png\"");
	}
	pbuf_imagesrf = gdk_pixbuf_new_from_file ("cairosurface.png", &error);
	if (!pbuf_imagesrf || error) {
		g_object_unref (pbuf_platform);
		g_error ("Eeek! Error loading \"cairosurface.png\"");
	}

	g_assert (gdk_pixbuf_get_width (pbuf_platform) ==
		  gdk_pixbuf_get_width (pbuf_imagesrf));
	g_assert (gdk_pixbuf_get_height (pbuf_platform) ==
		  gdk_pixbuf_get_height (pbuf_imagesrf));
	g_assert (gdk_pixbuf_get_rowstride (pbuf_platform) ==
		  gdk_pixbuf_get_rowstride (pbuf_imagesrf));
	g_assert (gdk_pixbuf_get_n_channels (pbuf_platform) ==
		  gdk_pixbuf_get_n_channels (pbuf_imagesrf));

	data_platform = gdk_pixbuf_get_pixels (pbuf_platform);
	data_imagesrf = gdk_pixbuf_get_pixels (pbuf_imagesrf);

	for (i = 0; i < gdk_pixbuf_get_height (pbuf_platform) * gdk_pixbuf_get_rowstride (pbuf_platform); i++) {
		if (data_platform[i] != data_imagesrf[i]) {
			g_warning ("Eeek! Images are differing at byte %d", i);
			g_object_unref (pbuf_platform);
			g_object_unref (pbuf_imagesrf);
			g_assert_not_reached ();
		}
	}

	g_object_unref (pbuf_platform);
	g_object_unref (pbuf_imagesrf);

	g_unlink ("cdksurface.png");
	g_unlink ("cairosurface.png");
}

int
main (int   argc,
      char**argv)
{
	g_test_init (&argc, &argv, NULL);
	cdk_init (&argc, &argv);

	g_test_add_func ("/cdk/surface/orientation",
			 test_surface_orientation);

	return g_test_run ();
}