| 1 |
/* This file is part of GTK+ |
| 2 |
* |
| 3 |
* AUTHORS |
| 4 |
* Sven Herzberg |
| 5 |
* |
| 6 |
* Copyright (C) 2008 Sven Herzberg |
| 7 |
* |
| 8 |
* This work is provided "as is"; redistribution and modification |
| 9 |
* in whole or in part, in any medium, physical or electronic is |
| 10 |
* permitted without restriction. |
| 11 |
* |
| 12 |
* This work is distributed in the hope that it will be useful, |
| 13 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
| 15 |
* |
| 16 |
* In no event shall the authors or contributors be liable for any |
| 17 |
* direct, indirect, incidental, special, exemplary, or consequential |
| 18 |
* damages (including, but not limited to, procurement of substitute |
| 19 |
* goods or services; loss of use, data, or profits; or business |
| 20 |
* interruption) however caused and on any theory of liability, whether |
| 21 |
* in contract, strict liability, or tort (including negligence or |
| 22 |
* otherwise) arising in any way out of the use of this software, even |
| 23 |
* if advised of the possibility of such damage. |
| 24 |
*/ |
| 25 |
|
| 26 |
#include <glib/gstdio.h> |
| 27 |
#include <gdk/gdk.h> |
| 28 |
#ifdef CAIRO_HAS_QUARTZ_SURFACE |
| 29 |
#include <cairo-quartz.h> |
| 30 |
#endif |
| 31 |
|
| 32 |
static void |
| 33 |
test (cairo_t* cr) |
| 34 |
{ |
| 35 |
cairo_save (cr); |
| 36 |
cairo_set_source_rgb (cr, 1.0, 1.0, 1.0); |
| 37 |
cairo_paint (cr); |
| 38 |
cairo_restore (cr); |
| 39 |
|
| 40 |
cairo_move_to (cr, 10.0, 20.0); |
| 41 |
cairo_line_to (cr, 10.0, 30.0); |
| 42 |
cairo_stroke (cr); |
| 43 |
|
| 44 |
cairo_set_source_rgba (cr, 0.0, 0.0, 0.0, 0.5); |
| 45 |
cairo_arc (cr, 0.0, 0.0, 10.0, 0.0, G_PI/2); |
| 46 |
cairo_stroke (cr); |
| 47 |
} |
| 48 |
|
| 49 |
static void |
| 50 |
test_surface_orientation (void) |
| 51 |
{ |
| 52 |
cairo_surface_t *surface; |
| 53 |
GdkPixbuf* pixbuf; |
| 54 |
GdkPixbuf* pbuf_platform; |
| 55 |
GdkPixbuf* pbuf_imagesrf; |
| 56 |
GError * error = NULL; |
| 57 |
cairo_surface_t* surface; |
| 58 |
cairo_t* cr; |
| 59 |
guchar* data_platform; |
| 60 |
guchar* data_imagesrf; |
| 61 |
guint i; |
| 62 |
|
| 63 |
/* create "platform.png" via native cairo surface */ |
| 64 |
surface = gdk_window_create_similar_surface (gdk_get_default_root_window (), |
| 65 |
CAIRO_CONTENT_COLOR, |
| 66 |
100, |
| 67 |
80); |
| 68 |
cr = cairo_create (surface); |
| 69 |
test (cr); |
| 70 |
cairo_destroy (cr); |
| 71 |
|
| 72 |
pixbuf = gdk_pixbuf_get_from_surface (NULL, |
| 73 |
surface, |
| 74 |
0, 0, |
| 75 |
0, 0, |
| 76 |
100, 80); |
| 77 |
if (!gdk_pixbuf_save (pixbuf, "gdksurface.png", "png", NULL, NULL)) { |
| 78 |
g_error ("Eeek! Couldn't save the file \"gdksurface.png\""); |
| 79 |
} |
| 80 |
g_object_unref (pixbuf); |
| 81 |
|
| 82 |
cairo_surface_destroy (surface); |
| 83 |
|
| 84 |
/* create "cairosurface.png" via pure cairo */ |
| 85 |
#ifndef CAIRO_HAS_QUARTZ_SURFACE |
| 86 |
surface = cairo_image_surface_create (CAIRO_FORMAT_RGB24, 100, 80); |
| 87 |
#else |
| 88 |
surface = cairo_quartz_surface_create (CAIRO_FORMAT_RGB24, 100, 80); |
| 89 |
#endif |
| 90 |
cr = cairo_create (surface); |
| 91 |
test (cr); |
| 92 |
cairo_destroy (cr); |
| 93 |
if (CAIRO_STATUS_SUCCESS != cairo_surface_write_to_png (surface, "cairosurface.png")) { |
| 94 |
g_error ("Eeek! Couldn't save the file \"cairosurface.png\""); |
| 95 |
} |
| 96 |
cairo_surface_destroy (surface); |
| 97 |
|
| 98 |
/* compare the images */ |
| 99 |
pbuf_platform = gdk_pixbuf_new_from_file ("gdksurface.png", &error); |
| 100 |
if (!pbuf_platform || error) { |
| 101 |
g_error ("Eeek! Error loading \"gdksurface.png\""); |
| 102 |
} |
| 103 |
pbuf_imagesrf = gdk_pixbuf_new_from_file ("cairosurface.png", &error); |
| 104 |
if (!pbuf_imagesrf || error) { |
| 105 |
g_object_unref (pbuf_platform); |
| 106 |
g_error ("Eeek! Error loading \"cairosurface.png\""); |
| 107 |
} |
| 108 |
|
| 109 |
g_assert (gdk_pixbuf_get_width (pbuf_platform) == |
| 110 |
gdk_pixbuf_get_width (pbuf_imagesrf)); |
| 111 |
g_assert (gdk_pixbuf_get_height (pbuf_platform) == |
| 112 |
gdk_pixbuf_get_height (pbuf_imagesrf)); |
| 113 |
g_assert (gdk_pixbuf_get_rowstride (pbuf_platform) == |
| 114 |
gdk_pixbuf_get_rowstride (pbuf_imagesrf)); |
| 115 |
g_assert (gdk_pixbuf_get_n_channels (pbuf_platform) == |
| 116 |
gdk_pixbuf_get_n_channels (pbuf_imagesrf)); |
| 117 |
|
| 118 |
data_platform = gdk_pixbuf_get_pixels (pbuf_platform); |
| 119 |
data_imagesrf = gdk_pixbuf_get_pixels (pbuf_imagesrf); |
| 120 |
|
| 121 |
for (i = 0; i < gdk_pixbuf_get_height (pbuf_platform) * gdk_pixbuf_get_rowstride (pbuf_platform); i++) { |
| 122 |
if (data_platform[i] != data_imagesrf[i]) { |
| 123 |
g_warning ("Eeek! Images are differing at byte %d", i); |
| 124 |
g_object_unref (pbuf_platform); |
| 125 |
g_object_unref (pbuf_imagesrf); |
| 126 |
g_assert_not_reached (); |
| 127 |
} |
| 128 |
} |
| 129 |
|
| 130 |
g_object_unref (pbuf_platform); |
| 131 |
g_object_unref (pbuf_imagesrf); |
| 132 |
|
| 133 |
g_unlink ("gdksurface.png"); |
| 134 |
g_unlink ("cairosurface.png"); |
| 135 |
} |
| 136 |
|
| 137 |
int |
| 138 |
main (int argc, |
| 139 |
char**argv) |
| 140 |
{ |
| 141 |
g_test_init (&argc, &argv, NULL); |
| 142 |
gdk_init (&argc, &argv); |
| 143 |
|
| 144 |
g_test_add_func ("/gdk/surface/orientation", |
| 145 |
test_surface_orientation); |
| 146 |
|
| 147 |
return g_test_run (); |
| 148 |
} |
| 149 |
|