1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 8 -*- */
9 compare_pixbufs (GdkPixbuf *pixbuf, GdkPixbuf *compare, const gchar *file_type)
11 if ((gdk_pixbuf_get_width (pixbuf) !=
12 gdk_pixbuf_get_width (compare)) ||
13 (gdk_pixbuf_get_height (pixbuf) !=
14 gdk_pixbuf_get_height (compare)) ||
15 (gdk_pixbuf_get_n_channels (pixbuf) !=
16 gdk_pixbuf_get_n_channels (compare)) ||
17 (gdk_pixbuf_get_has_alpha (pixbuf) !=
18 gdk_pixbuf_get_has_alpha (compare)) ||
19 (gdk_pixbuf_get_bits_per_sample (pixbuf) !=
20 gdk_pixbuf_get_bits_per_sample (compare))) {
22 "saved %s file differs from copy in memory\n",
26 guchar *compare_pixels;
28 gint compare_rowstride;
36 orig_pixels = gdk_pixbuf_get_pixels (pixbuf);
37 compare_pixels = gdk_pixbuf_get_pixels (compare);
39 orig_rowstride = gdk_pixbuf_get_rowstride (pixbuf);
40 compare_rowstride = gdk_pixbuf_get_rowstride (compare);
42 width = gdk_pixbuf_get_width (pixbuf);
43 height = gdk_pixbuf_get_height (pixbuf);
46 bytes_per_pixel = gdk_pixbuf_get_n_channels (pixbuf);
51 for (y = 0; y < height; y++) {
52 for (x = 0; x < width * bytes_per_pixel; x++)
53 count += (*p1++ != *p2++);
55 orig_pixels += orig_rowstride;
56 compare_pixels += compare_rowstride;
64 "saved %s file differs from copy in memory\n",
71 keypress_check (GtkWidget *widget, GdkEventKey *evt, gpointer data)
74 GtkDrawingArea *da = (GtkDrawingArea*)data;
77 pixbuf = (GdkPixbuf *) g_object_get_data (G_OBJECT (da), "pixbuf");
79 if (evt->keyval == 'q')
81 if (evt->keyval == 's') {
83 fprintf (stderr, "PIXBUF NULL\n");
87 if (!gdk_pixbuf_save (pixbuf, "foo.jpg", "jpeg",
91 fprintf (stderr, "%s", err->message);
96 compare = gdk_pixbuf_new_from_file ("foo.jpg", &err);
99 fprintf (stderr, "%s", err->message);
102 compare_pixbufs (pixbuf, compare, "jpeg");
103 g_object_unref (compare);
107 } else if (evt->keyval == 'p') {
108 if (pixbuf == NULL) {
109 fprintf (stderr, "PIXBUF NULL\n");
113 if (!gdk_pixbuf_save (pixbuf, "foo.png", "png",
115 "tEXt::Software", "testpixbuf-save",
117 fprintf (stderr, "%s", err->message);
122 compare = gdk_pixbuf_new_from_file ("foo.png", &err);
125 fprintf (stderr, "%s", err->message);
128 compare_pixbufs (pixbuf, compare, "png");
129 g_object_unref (compare);
133 } else if (evt->keyval == 'a') {
134 if (pixbuf == NULL) {
135 fprintf (stderr, "PIXBUF NULL\n");
138 GdkPixbuf *alpha_buf;
140 alpha_buf = gdk_pixbuf_add_alpha (pixbuf,
143 g_object_set_data_full (G_OBJECT (da),
145 (GDestroyNotify) g_object_unref);
152 close_app (GtkWidget *widget, gpointer data)
159 expose_cb (GtkWidget *drawing_area, GdkEventExpose *evt, gpointer data)
163 pixbuf = (GdkPixbuf *) g_object_get_data (G_OBJECT (drawing_area),
165 if (gdk_pixbuf_get_has_alpha (pixbuf)) {
166 gdk_draw_rgb_32_image (drawing_area->window,
167 drawing_area->style->black_gc,
168 evt->area.x, evt->area.y,
172 gdk_pixbuf_get_pixels (pixbuf) +
173 (evt->area.y * gdk_pixbuf_get_rowstride (pixbuf)) +
174 (evt->area.x * gdk_pixbuf_get_n_channels (pixbuf)),
175 gdk_pixbuf_get_rowstride (pixbuf));
177 gdk_draw_rgb_image (drawing_area->window,
178 drawing_area->style->black_gc,
179 evt->area.x, evt->area.y,
182 GDK_RGB_DITHER_NORMAL,
183 gdk_pixbuf_get_pixels (pixbuf) +
184 (evt->area.y * gdk_pixbuf_get_rowstride (pixbuf)) +
185 (evt->area.x * gdk_pixbuf_get_n_channels (pixbuf)),
186 gdk_pixbuf_get_rowstride (pixbuf));
192 configure_cb (GtkWidget *drawing_area, GdkEventConfigure *evt, gpointer data)
196 pixbuf = (GdkPixbuf *) g_object_get_data (G_OBJECT (drawing_area),
199 g_print ("X:%d Y:%d\n", evt->width, evt->height);
200 if (evt->width != gdk_pixbuf_get_width (pixbuf) || evt->height != gdk_pixbuf_get_height (pixbuf)) {
202 GdkPixbuf *new_pixbuf;
204 root = gdk_get_default_root_window ();
205 new_pixbuf = gdk_pixbuf_get_from_drawable (NULL, root, NULL,
206 0, 0, 0, 0, evt->width, evt->height);
207 g_object_set_data_full (G_OBJECT (drawing_area), "pixbuf", new_pixbuf,
208 (GDestroyNotify) g_object_unref);
215 main (int argc, char **argv)
220 GtkWidget *drawing_area;
223 gtk_init (&argc, &argv);
225 gtk_widget_set_default_colormap (gdk_rgb_get_colormap ());
227 root = gdk_get_default_root_window ();
228 pixbuf = gdk_pixbuf_get_from_drawable (NULL, root, NULL,
229 0, 0, 0, 0, 150, 160);
231 window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
232 g_signal_connect (window, "delete_event",
233 G_CALLBACK (close_app), NULL);
234 g_signal_connect (window, "destroy",
235 G_CALLBACK (close_app), NULL);
237 vbox = gtk_vbox_new (FALSE, 0);
238 gtk_container_add (GTK_CONTAINER (window), vbox);
240 drawing_area = gtk_drawing_area_new ();
241 gtk_widget_set_size_request (GTK_WIDGET (drawing_area),
242 gdk_pixbuf_get_width (pixbuf),
243 gdk_pixbuf_get_height (pixbuf));
244 g_signal_connect (drawing_area, "expose_event",
245 G_CALLBACK (expose_cb), NULL);
247 g_signal_connect (drawing_area, "configure_event",
248 G_CALLBACK (configure_cb), NULL);
249 g_signal_connect (window, "key_press_event",
250 G_CALLBACK (keypress_check), drawing_area);
251 g_object_set_data_full (G_OBJECT (drawing_area), "pixbuf", pixbuf,
252 (GDestroyNotify) g_object_unref);
253 gtk_box_pack_start (GTK_BOX (vbox), drawing_area, TRUE, TRUE, 0);
255 gtk_widget_show_all (window);