]> Pileus Git - ~andy/gtk/blob - demos/testpixbuf-save.c
add deprecation compile flags
[~andy/gtk] / demos / testpixbuf-save.c
1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 8 -*- */
2
3 #include "config.h"
4 #include <stdio.h>
5
6 #include <gtk/gtk.h>
7
8 static void
9 compare_pixbufs (GdkPixbuf *pixbuf, GdkPixbuf *compare, const gchar *file_type)
10 {
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))) {
21                 fprintf (stderr,
22                          "saved %s file differs from copy in memory\n",
23                          file_type);
24         } else {
25                 guchar *orig_pixels;
26                 guchar *compare_pixels;
27                 gint    orig_rowstride;
28                 gint    compare_rowstride;
29                 gint    width;
30                 gint    height;
31                 gint    bytes_per_pixel;
32                 gint    x, y;
33                 guchar *p1, *p2;
34                 gint    count = 0;
35
36                 orig_pixels = gdk_pixbuf_get_pixels (pixbuf);
37                 compare_pixels = gdk_pixbuf_get_pixels (compare);
38
39                 orig_rowstride = gdk_pixbuf_get_rowstride (pixbuf);
40                 compare_rowstride = gdk_pixbuf_get_rowstride (compare);
41
42                 width = gdk_pixbuf_get_width (pixbuf);
43                 height = gdk_pixbuf_get_height (pixbuf);
44
45                 /*  well...  */
46                 bytes_per_pixel = gdk_pixbuf_get_n_channels (pixbuf);
47
48                 p1 = orig_pixels;
49                 p2 = compare_pixels;
50
51                 for (y = 0; y < height; y++) {
52                         for (x = 0; x < width * bytes_per_pixel; x++)
53                                 count += (*p1++ != *p2++);
54
55                         orig_pixels += orig_rowstride;
56                         compare_pixels += compare_rowstride;
57
58                         p1 = orig_pixels;
59                         p2 = compare_pixels;
60                 }
61
62                 if (count > 0) {
63                         fprintf (stderr,
64                                  "saved %s file differs from copy in memory\n",
65                                  file_type);
66                 }
67         }
68 }
69
70 static void
71 keypress_check (GtkWidget *widget, GdkEventKey *evt, gpointer data)
72 {
73         GdkPixbuf *pixbuf;
74         GtkDrawingArea *da = (GtkDrawingArea*)data;
75         GError *err = NULL;
76         
77         pixbuf = (GdkPixbuf *) g_object_get_data (G_OBJECT (da), "pixbuf");
78
79         if (evt->keyval == 'q')
80                 gtk_main_quit ();
81         if (evt->keyval == 's') {
82                 if (pixbuf == NULL) {
83                         fprintf (stderr, "PIXBUF NULL\n");
84                         return;
85                 }       
86
87                 if (!gdk_pixbuf_save (pixbuf, "foo.jpg", "jpeg",
88                                       &err,
89                                       "quality", "100",
90                                       NULL)) {
91                         fprintf (stderr, "%s", err->message);
92                         g_error_free (err);
93                 } else {
94                         GdkPixbuf *compare;
95
96                         compare = gdk_pixbuf_new_from_file ("foo.jpg", &err);
97
98                         if (!compare) {
99                                 fprintf (stderr, "%s", err->message);
100                                 g_error_free (err);
101                         } else {
102                                 compare_pixbufs (pixbuf, compare, "jpeg");
103                                 g_object_unref (compare);
104                         }
105                                         
106                 }
107         } else if (evt->keyval == 'p') {
108                 if (pixbuf == NULL) {
109                         fprintf (stderr, "PIXBUF NULL\n");
110                         return;
111                 }
112
113                 if (!gdk_pixbuf_save (pixbuf, "foo.png", "png", 
114                                       &err,
115                                       "tEXt::Software", "testpixbuf-save",
116                                       NULL)) {
117                         fprintf (stderr, "%s", err->message);
118                         g_error_free (err);
119                 } else {
120                         GdkPixbuf *compare;
121
122                         compare = gdk_pixbuf_new_from_file ("foo.png", &err);
123
124                         if (!compare) {
125                                 fprintf (stderr, "%s", err->message);
126                                 g_error_free (err);
127                         } else {
128                                 compare_pixbufs (pixbuf, compare, "png");
129                                 g_object_unref (compare);
130                         }
131                                         
132                 }
133         } else if (evt->keyval == 'a') {
134                 if (pixbuf == NULL) {
135                         fprintf (stderr, "PIXBUF NULL\n");
136                         return;
137                 } else {
138                         GdkPixbuf *alpha_buf;
139
140                         alpha_buf = gdk_pixbuf_add_alpha (pixbuf,
141                                                           FALSE, 0, 0, 0);
142
143                         g_object_set_data_full (G_OBJECT (da),
144                                                 "pixbuf", alpha_buf,
145                                                 (GDestroyNotify) g_object_unref);
146                 }
147         }
148 }
149
150
151 static int
152 close_app (GtkWidget *widget, gpointer data)
153 {
154         gtk_main_quit ();
155         return TRUE;
156 }
157
158 static int
159 expose_cb (GtkWidget *drawing_area, GdkEventExpose *evt, gpointer data)
160 {
161         GdkPixbuf *pixbuf;
162          
163         pixbuf = (GdkPixbuf *) g_object_get_data (G_OBJECT (drawing_area),
164                                                   "pixbuf");
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,
169                                        evt->area.width,
170                                        evt->area.height,
171                                        GDK_RGB_DITHER_MAX,
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));
176         } else {
177                 gdk_draw_rgb_image (drawing_area->window, 
178                                     drawing_area->style->black_gc, 
179                                     evt->area.x, evt->area.y,
180                                     evt->area.width,
181                                     evt->area.height,  
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));
187         }
188         return FALSE;
189 }
190
191 static int
192 configure_cb (GtkWidget *drawing_area, GdkEventConfigure *evt, gpointer data)
193 {
194         GdkPixbuf *pixbuf;
195                            
196         pixbuf = (GdkPixbuf *) g_object_get_data (G_OBJECT (drawing_area),   
197                                                   "pixbuf");
198     
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)) {
201                 GdkWindow *root;
202                 GdkPixbuf *new_pixbuf;
203
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);
209         }
210
211         return FALSE;
212 }
213
214 int
215 main (int argc, char **argv)
216 {   
217         GdkWindow     *root;
218         GtkWidget     *window;
219         GtkWidget     *vbox;
220         GtkWidget     *drawing_area;
221         GdkPixbuf     *pixbuf;    
222    
223         gtk_init (&argc, &argv);   
224
225         gtk_widget_set_default_colormap (gdk_rgb_get_colormap ());
226
227         root = gdk_get_default_root_window ();
228         pixbuf = gdk_pixbuf_get_from_drawable (NULL, root, NULL,
229                                                0, 0, 0, 0, 150, 160);
230    
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);
236    
237         vbox = gtk_vbox_new (FALSE, 0);
238         gtk_container_add (GTK_CONTAINER (window), vbox);  
239    
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);
246
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);
254    
255         gtk_widget_show_all (window);
256         gtk_main ();
257         return 0;
258 }