testpixbuf-save.c 15.7 KB
Newer Older
1
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 8 -*- */
2

3
#include "config.h"
4 5
#include <stdio.h>

6 7
#include <gtk/gtk.h>

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
static void
compare_pixbufs (GdkPixbuf *pixbuf, GdkPixbuf *compare, const gchar *file_type)
{
        if ((gdk_pixbuf_get_width (pixbuf) !=
             gdk_pixbuf_get_width (compare)) ||
            (gdk_pixbuf_get_height (pixbuf) !=
             gdk_pixbuf_get_height (compare)) ||
            (gdk_pixbuf_get_n_channels (pixbuf) !=
             gdk_pixbuf_get_n_channels (compare)) ||
            (gdk_pixbuf_get_has_alpha (pixbuf) !=
             gdk_pixbuf_get_has_alpha (compare)) ||
            (gdk_pixbuf_get_bits_per_sample (pixbuf) !=
             gdk_pixbuf_get_bits_per_sample (compare))) {
                fprintf (stderr,
                         "saved %s file differs from copy in memory\n",
                         file_type);
        } else {
                guchar *orig_pixels;
                guchar *compare_pixels;
                gint    orig_rowstride;
                gint    compare_rowstride;
                gint    width;
                gint    height;
                gint    bytes_per_pixel;
                gint    x, y;
                guchar *p1, *p2;
                gint    count = 0;

                orig_pixels = gdk_pixbuf_get_pixels (pixbuf);
                compare_pixels = gdk_pixbuf_get_pixels (compare);

                orig_rowstride = gdk_pixbuf_get_rowstride (pixbuf);
                compare_rowstride = gdk_pixbuf_get_rowstride (compare);

                width = gdk_pixbuf_get_width (pixbuf);
                height = gdk_pixbuf_get_height (pixbuf);

                /*  well...  */
                bytes_per_pixel = gdk_pixbuf_get_n_channels (pixbuf);

                p1 = orig_pixels;
                p2 = compare_pixels;

                for (y = 0; y < height; y++) {
                        for (x = 0; x < width * bytes_per_pixel; x++)
                                count += (*p1++ != *p2++);

                        orig_pixels += orig_rowstride;
                        compare_pixels += compare_rowstride;

                        p1 = orig_pixels;
                        p2 = compare_pixels;
                }

                if (count > 0) {
                        fprintf (stderr,
                                 "saved %s file differs from copy in memory\n",
                                 file_type);
                }
        }
}

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
static gboolean
save_to_loader (const gchar *buf, gsize count, GError **err, gpointer data)
{
        GdkPixbufLoader *loader = data;

        return gdk_pixbuf_loader_write (loader, buf, count, err);
}

static GdkPixbuf *
buffer_to_pixbuf (const gchar *buf, gsize count, GError **err)
{
        GdkPixbufLoader *loader;
        GdkPixbuf *pixbuf;

        loader = gdk_pixbuf_loader_new ();
        if (gdk_pixbuf_loader_write (loader, buf, count, err) && 
            gdk_pixbuf_loader_close (loader, err)) {
                pixbuf = g_object_ref (gdk_pixbuf_loader_get_pixbuf (loader));
                g_object_unref (G_OBJECT (loader));
                return pixbuf;
        } else {
                return NULL;
        }
}

static void
do_compare (GdkPixbuf *pixbuf, GdkPixbuf *compare, GError *err)
{
        if (compare == NULL) {
                fprintf (stderr, "%s", err->message);
                g_error_free (err);
        } else {
                compare_pixbufs (pixbuf, compare, "jpeg");
                g_object_unref (compare);
        }
}

107
static void
108 109 110 111 112
keypress_check (GtkWidget *widget, GdkEventKey *evt, gpointer data)
{
        GdkPixbuf *pixbuf;
        GtkDrawingArea *da = (GtkDrawingArea*)data;
        GError *err = NULL;
113 114 115 116
        gchar *buffer;
        gsize count;
        GdkPixbufLoader *loader;

117
        pixbuf = (GdkPixbuf *) g_object_get_data (G_OBJECT (da), "pixbuf");
118 119 120

        if (evt->keyval == 'q')
                gtk_main_quit ();
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 150 151 152 153 154 155 156 157 158 159

        if (evt->keyval == 's' && (evt->state & GDK_CONTROL_MASK)) {
                /* save to callback */
                if (pixbuf == NULL) {
                        fprintf (stderr, "PIXBUF NULL\n");
                        return;
                }	

                loader = gdk_pixbuf_loader_new ();
                if (!gdk_pixbuf_save_to_callback (pixbuf, save_to_loader, loader, "jpeg",
                                                  &err,
                                                  "quality", "100",
                                                  NULL) ||
                    !gdk_pixbuf_loader_close (loader, &err)) {
                        fprintf (stderr, "%s", err->message);
                        g_error_free (err);
                } else {
                        do_compare (pixbuf,
                                    g_object_ref (gdk_pixbuf_loader_get_pixbuf (loader)),
                                    err);
                        g_object_unref (G_OBJECT (loader));
                }
        }
        else if (evt->keyval == 'S') {
                /* save to buffer */
                if (!gdk_pixbuf_save_to_buffer (pixbuf, &buffer, &count, "jpeg",
                                                &err,
                                                "quality", "100",
                                                NULL)) {
                        fprintf (stderr, "%s", err->message);
                        g_error_free (err);
                } else {
                        do_compare (pixbuf,
                                    buffer_to_pixbuf (buffer, count, &err),
                                    err);
                }
        }
        else if (evt->keyval == 's') {
                /* save normally */
160 161 162 163 164 165 166 167 168 169 170
                if (pixbuf == NULL) {
                        fprintf (stderr, "PIXBUF NULL\n");
                        return;
                }	

                if (!gdk_pixbuf_save (pixbuf, "foo.jpg", "jpeg",
                                      &err,
                                      "quality", "100",
                                      NULL)) {
                        fprintf (stderr, "%s", err->message);
                        g_error_free (err);
171
                } else {
172 173 174 175 176
                        do_compare (pixbuf,
                                    gdk_pixbuf_new_from_file ("foo.jpg", &err),
                                    err);
                }
        }
177

178 179 180 181 182 183
        if (evt->keyval == 'p' && (evt->state & GDK_CONTROL_MASK)) {
                /* save to callback */
                if (pixbuf == NULL) {
                        fprintf (stderr, "PIXBUF NULL\n");
                        return;
                }
184

185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211
                loader = gdk_pixbuf_loader_new ();
                if (!gdk_pixbuf_save_to_callback (pixbuf, save_to_loader, loader, "png",
                                                  &err,
                                                  "tEXt::Software", "testpixbuf-save",
                                                  NULL)
                    || !gdk_pixbuf_loader_close (loader, &err)) {
                        fprintf (stderr, "%s", err->message);
                        g_error_free (err);
                } else {
                        do_compare (pixbuf,
                                    g_object_ref (gdk_pixbuf_loader_get_pixbuf (loader)),
                                    err);
                        g_object_unref (G_OBJECT (loader));
                }
        }
        else if (evt->keyval == 'P') {
                /* save to buffer */
                if (!gdk_pixbuf_save_to_buffer (pixbuf, &buffer, &count, "png",
                                                &err,
                                                "tEXt::Software", "testpixbuf-save",
                                                NULL)) {
                        fprintf (stderr, "%s", err->message);
                        g_error_free (err);
                } else {
                        do_compare (pixbuf,
                                    buffer_to_pixbuf (buffer, count, &err),
                                    err);
212
                }
213 214
        }
        else if (evt->keyval == 'p') {
215 216 217 218 219
                if (pixbuf == NULL) {
                        fprintf (stderr, "PIXBUF NULL\n");
                        return;
                }

220 221 222 223
                if (!gdk_pixbuf_save (pixbuf, "foo.png", "png", 
                                      &err,
                                      "tEXt::Software", "testpixbuf-save",
                                      NULL)) {
224 225
                        fprintf (stderr, "%s", err->message);
                        g_error_free (err);
226
                } else {
227 228 229 230 231
                        do_compare(pixbuf,
                                   gdk_pixbuf_new_from_file ("foo.png", &err),
                                   err);
                }
        }
232

233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271
        if (evt->keyval == 'i' && (evt->state & GDK_CONTROL_MASK)) {
                /* save to callback */
                if (pixbuf == NULL) {
                        fprintf (stderr, "PIXBUF NULL\n");
                        return;
                }

                loader = gdk_pixbuf_loader_new ();
                if (!gdk_pixbuf_save_to_callback (pixbuf, save_to_loader, loader, "ico",
                                                  &err,
                                                  NULL)
                    || !gdk_pixbuf_loader_close (loader, &err)) {
                        fprintf (stderr, "%s", err->message);
                        g_error_free (err);
                } else {
                        do_compare (pixbuf,
                                    g_object_ref (gdk_pixbuf_loader_get_pixbuf (loader)),
                                    err);
                        g_object_unref (G_OBJECT (loader));
                }
        }
        else if (evt->keyval == 'I') {
                /* save to buffer */
                if (!gdk_pixbuf_save_to_buffer (pixbuf, &buffer, &count, "ico",
                                                &err,
                                                NULL)) {
                        fprintf (stderr, "%s", err->message);
                        g_error_free (err);
                } else {
                        do_compare (pixbuf,
                                    buffer_to_pixbuf (buffer, count, &err),
                                    err);
                }
        }
        else if (evt->keyval == 'i') {
                if (pixbuf == NULL) {
                        fprintf (stderr, "PIXBUF NULL\n");
                        return;
                }
272

273 274 275 276 277 278 279 280 281
                if (!gdk_pixbuf_save (pixbuf, "foo.ico", "ico", 
                                      &err,
                                      NULL)) {
                        fprintf (stderr, "%s", err->message);
                        g_error_free (err);
                } else {
                        do_compare(pixbuf,
                                   gdk_pixbuf_new_from_file ("foo.ico", &err),
                                   err);
282
                }
283 284 285
        }

        if (evt->keyval == 'a') {
286 287 288 289 290 291 292 293 294 295 296 297
                if (pixbuf == NULL) {
                        fprintf (stderr, "PIXBUF NULL\n");
                        return;
                } else {
                        GdkPixbuf *alpha_buf;

                        alpha_buf = gdk_pixbuf_add_alpha (pixbuf,
                                                          FALSE, 0, 0, 0);

                        g_object_set_data_full (G_OBJECT (da),
                                                "pixbuf", alpha_buf,
                                                (GDestroyNotify) g_object_unref);
298 299 300 301 302
                }
        }
}


303
static int
304 305 306 307 308 309
close_app (GtkWidget *widget, gpointer data)
{
        gtk_main_quit ();
        return TRUE;
}

310
static int
311 312 313 314
expose_cb (GtkWidget *drawing_area, GdkEventExpose *evt, gpointer data)
{
        GdkPixbuf *pixbuf;
         
315 316
        pixbuf = (GdkPixbuf *) g_object_get_data (G_OBJECT (drawing_area),
						  "pixbuf");
317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342
        if (gdk_pixbuf_get_has_alpha (pixbuf)) {
                gdk_draw_rgb_32_image (drawing_area->window,
                                       drawing_area->style->black_gc,
                                       evt->area.x, evt->area.y,
                                       evt->area.width,
                                       evt->area.height,
                                       GDK_RGB_DITHER_MAX,
                                       gdk_pixbuf_get_pixels (pixbuf) +
                                       (evt->area.y * gdk_pixbuf_get_rowstride (pixbuf)) +
                                       (evt->area.x * gdk_pixbuf_get_n_channels (pixbuf)),
                                       gdk_pixbuf_get_rowstride (pixbuf));
        } else {
                gdk_draw_rgb_image (drawing_area->window, 
                                    drawing_area->style->black_gc, 
                                    evt->area.x, evt->area.y,
                                    evt->area.width,
                                    evt->area.height,  
                                    GDK_RGB_DITHER_NORMAL,
                                    gdk_pixbuf_get_pixels (pixbuf) +
                                    (evt->area.y * gdk_pixbuf_get_rowstride (pixbuf)) +
                                    (evt->area.x * gdk_pixbuf_get_n_channels (pixbuf)),
                                    gdk_pixbuf_get_rowstride (pixbuf));
        }
        return FALSE;
}

343
static int
344 345 346 347
configure_cb (GtkWidget *drawing_area, GdkEventConfigure *evt, gpointer data)
{
        GdkPixbuf *pixbuf;
                           
348 349
        pixbuf = (GdkPixbuf *) g_object_get_data (G_OBJECT (drawing_area),   
						  "pixbuf");
350 351 352 353 354 355
    
        g_print ("X:%d Y:%d\n", evt->width, evt->height);
        if (evt->width != gdk_pixbuf_get_width (pixbuf) || evt->height != gdk_pixbuf_get_height (pixbuf)) {
                GdkWindow *root;
                GdkPixbuf *new_pixbuf;

356
                root = gdk_get_default_root_window ();
357 358
                new_pixbuf = gdk_pixbuf_get_from_drawable (NULL, root, NULL,
                                                           0, 0, 0, 0, evt->width, evt->height);
359 360
                g_object_set_data_full (G_OBJECT (drawing_area), "pixbuf", new_pixbuf,
                                        (GDestroyNotify) g_object_unref);
361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376
        }

        return FALSE;
}

int
main (int argc, char **argv)
{   
        GdkWindow     *root;
        GtkWidget     *window;
        GtkWidget     *vbox;
        GtkWidget     *drawing_area;
        GdkPixbuf     *pixbuf;    
   
        gtk_init (&argc, &argv);   

377
        gtk_widget_set_default_colormap (gdk_rgb_get_colormap ());
378

379
        root = gdk_get_default_root_window ();
380 381 382 383
        pixbuf = gdk_pixbuf_get_from_drawable (NULL, root, NULL,
                                               0, 0, 0, 0, 150, 160);
   
        window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
384 385 386 387
        g_signal_connect (window, "delete_event",
			  G_CALLBACK (close_app), NULL);
        g_signal_connect (window, "destroy",   
			  G_CALLBACK (close_app), NULL);
388 389 390 391 392
   
        vbox = gtk_vbox_new (FALSE, 0);
        gtk_container_add (GTK_CONTAINER (window), vbox);  
   
        drawing_area = gtk_drawing_area_new ();
Havoc Pennington's avatar
Havoc Pennington committed
393 394 395
        gtk_widget_set_size_request (GTK_WIDGET (drawing_area),
                                     gdk_pixbuf_get_width (pixbuf),
                                     gdk_pixbuf_get_height (pixbuf));
396 397 398 399 400 401 402
        g_signal_connect (drawing_area, "expose_event",
			  G_CALLBACK (expose_cb), NULL);

        g_signal_connect (drawing_area, "configure_event",
			  G_CALLBACK (configure_cb), NULL);
        g_signal_connect (window, "key_press_event", 
			  G_CALLBACK (keypress_check), drawing_area);    
403 404
        g_object_set_data_full (G_OBJECT (drawing_area), "pixbuf", pixbuf,
                                (GDestroyNotify) g_object_unref);
405 406 407 408 409 410
        gtk_box_pack_start (GTK_BOX (vbox), drawing_area, TRUE, TRUE, 0);
   
        gtk_widget_show_all (window);
        gtk_main ();
        return 0;
}