io-png.c 19.9 KB
Newer Older
1
/* GdkPixbuf library - PNG image loader
2
 *
3
 * Copyright (C) 1999 Mark Crichton
4 5 6 7
 * Copyright (C) 1999 The Free Software Foundation
 *
 * Authors: Mark Crichton <crichton@gimp.org>
 *          Federico Mena-Quintero <federico@gimp.org>
8
 *
9
 * This library is free software; you can redistribute it and/or
10
 * modify it under the terms of the GNU Lesser General Public
11 12 13 14 15 16
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library 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.  See the GNU
17
 * Lesser General Public License for more details.
18
 *
19
 * You should have received a copy of the GNU Lesser General Public
20 21 22
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
23
 */
24

25 26
#include <config.h>
#include <stdio.h>
27
#include <stdlib.h>
28
#include <png.h>
29
#include "gdk-pixbuf-private.h"
30
#include "gdk-pixbuf-io.h"
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
static void
setup_png_transformations(png_structp png_read_ptr, png_infop png_info_ptr,
                          gboolean *fatal_error_occurred,
                          png_uint_32* width_p, png_uint_32* height_p,
                          int* color_type_p)
{
        png_uint_32 width, height;
        int bit_depth, color_type, interlace_type, compression_type, filter_type;
        int channels;
        
        /* Get the image info */

        png_get_IHDR (png_read_ptr, png_info_ptr,
                      &width, &height,
                      &bit_depth,
                      &color_type,
                      &interlace_type,
                      &compression_type,
                      &filter_type);

        /* set_expand() basically needs to be called unless
           we are already in RGB/RGBA mode
        */
        if (color_type == PNG_COLOR_TYPE_PALETTE &&
            bit_depth <= 8) {

                /* Convert indexed images to RGB */
                png_set_expand (png_read_ptr);

        } else if (color_type == PNG_COLOR_TYPE_GRAY &&
                   bit_depth < 8) {

                /* Convert grayscale to RGB */
                png_set_expand (png_read_ptr);

        } else if (png_get_valid (png_read_ptr, 
                                  png_info_ptr, PNG_INFO_tRNS)) {

                /* If we have transparency header, convert it to alpha
                   channel */
                png_set_expand(png_read_ptr);
                
        } else if (bit_depth < 8) {

                /* If we have < 8 scale it up to 8 */
                png_set_expand(png_read_ptr);


                /* Conceivably, png_set_packing() is a better idea;
                 * God only knows how libpng works
                 */
        }

        /* If we are 16-bit, convert to 8-bit */
        if (bit_depth == 16) {
                png_set_strip_16(png_read_ptr);
        }

        /* If gray scale, convert to RGB */
        if (color_type == PNG_COLOR_TYPE_GRAY ||
            color_type == PNG_COLOR_TYPE_GRAY_ALPHA) {
                png_set_gray_to_rgb(png_read_ptr);
        }
        
        /* If interlaced, handle that */
        if (interlace_type != PNG_INTERLACE_NONE) {
                png_set_interlace_handling(png_read_ptr);
        }
        
        /* Update the info the reflect our transformations */
        png_read_update_info(png_read_ptr, png_info_ptr);
        
        png_get_IHDR (png_read_ptr, png_info_ptr,
                      &width, &height,
                      &bit_depth,
                      &color_type,
                      &interlace_type,
                      &compression_type,
                      &filter_type);

        *width_p = width;
        *height_p = height;
        *color_type_p = color_type;
        
#ifndef G_DISABLE_CHECKS
        /* Check that the new info is what we want */
        
        if (bit_depth != 8) {
                g_warning("Bits per channel of transformed PNG is %d, not 8.", bit_depth);
                *fatal_error_occurred = TRUE;
                return;
        }

        if ( ! (color_type == PNG_COLOR_TYPE_RGB ||
                color_type == PNG_COLOR_TYPE_RGB_ALPHA) ) {
                g_warning("Transformed PNG not RGB or RGBA.");
                *fatal_error_occurred = TRUE;
                return;
        }

        channels = png_get_channels(png_read_ptr, png_info_ptr);
        if ( ! (channels == 3 || channels == 4) ) {
                g_warning("Transformed PNG has %d channels, must be 3 or 4.", channels);
                *fatal_error_occurred = TRUE;
                return;
        }
#endif
}

143
/* Destroy notification function for the pixbuf */
144
static void
145
free_buffer (guchar *pixels, gpointer data)
146
{
147
	free (pixels);
148
}
149 150

/* Shared library entry point */
151
GdkPixbuf *
Federico Mena Quintero's avatar
Federico Mena Quintero committed
152
gdk_pixbuf__png_image_load (FILE *f)
153
{
154 155
	png_structp png_ptr;
	png_infop info_ptr, end_info;
156
        gboolean failed = FALSE;
157
	gint i, ctype, bpp;
158
	png_uint_32 w, h;
159
	png_bytepp rows;
160
	guchar *pixels;
161

162
	png_ptr = png_create_read_struct (PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182
	if (!png_ptr)
		return NULL;

	info_ptr = png_create_info_struct (png_ptr);
	if (!info_ptr) {
		png_destroy_read_struct (&png_ptr, NULL, NULL);
		return NULL;
	}

	end_info = png_create_info_struct (png_ptr);
	if (!end_info) {
		png_destroy_read_struct (&png_ptr, &info_ptr, NULL);
		return NULL;
	}

	if (setjmp (png_ptr->jmpbuf)) {
		png_destroy_read_struct (&png_ptr, &info_ptr, &end_info);
		return NULL;
	}

183
	png_init_io (png_ptr, f);
184
	png_read_info (png_ptr, info_ptr);
185

186
        setup_png_transformations(png_ptr, info_ptr, &failed, &w, &h, &ctype);
187

188 189 190 191 192
        if (failed) {
                png_destroy_read_struct (&png_ptr, &info_ptr, &end_info);
                return NULL;
        }
        
193 194 195 196 197
	if (ctype & PNG_COLOR_MASK_ALPHA)
		bpp = 4;
	else
		bpp = 3;

198
	pixels = malloc (w * h * bpp);
199
	if (!pixels) {
200 201 202
		png_destroy_read_struct (&png_ptr, &info_ptr, &end_info);
		return NULL;
	}
203

204
	rows = g_new (png_bytep, h);
205

206 207
	for (i = 0; i < h; i++)
		rows[i] = pixels + i * w * bpp;
208

209 210
	png_read_image (png_ptr, rows);
	png_destroy_read_struct (&png_ptr, &info_ptr, &end_info);
211
	g_free (rows);
212

213
	if (ctype & PNG_COLOR_MASK_ALPHA)
214
		return gdk_pixbuf_new_from_data (pixels, GDK_COLORSPACE_RGB, TRUE, 8,
215 216
						 w, h, w * 4,
						 free_buffer, NULL);
217
	else
218
		return gdk_pixbuf_new_from_data (pixels, GDK_COLORSPACE_RGB, FALSE, 8,
219 220
						 w, h, w * 3,
						 free_buffer, NULL);
221
}
222

223 224
/* I wish these avoided the setjmp()/longjmp() crap in libpng instead
   just allow you to change the error reporting. */
225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251
static void png_error_callback  (png_structp png_read_ptr,
                                 png_const_charp error_msg);

static void png_warning_callback(png_structp png_read_ptr,
                                 png_const_charp warning_msg);

/* Called at the start of the progressive load */
static void png_info_callback   (png_structp png_read_ptr,
                                 png_infop   png_info_ptr);

/* Called for each row; note that you will get duplicate row numbers
   for interlaced PNGs */
static void png_row_callback   (png_structp png_read_ptr,
                                png_bytep   new_row,
                                png_uint_32 row_num,
                                int pass_num);

/* Called after reading the entire image */
static void png_end_callback   (png_structp png_read_ptr,
                                png_infop   png_info_ptr);

typedef struct _LoadContext LoadContext;

struct _LoadContext {
        png_structp png_read_ptr;
        png_infop   png_info_ptr;

252 253
        ModulePreparedNotifyFunc prepare_func;
        ModuleUpdatedNotifyFunc update_func;
254 255 256
        gpointer notify_user_data;

        GdkPixbuf* pixbuf;
257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272

        /* row number of first row seen, or -1 if none yet seen */

        gint first_row_seen_in_chunk;

        /* pass number for the first row seen */

        gint first_pass_seen_in_chunk;
        
        /* row number of last row seen */
        gint last_row_seen_in_chunk;

        gint last_pass_seen_in_chunk;

        /* highest row number seen */
        gint max_row_seen_in_chunk;
273 274 275 276 277 278
        
        guint fatal_error_occurred : 1;

};

gpointer
Federico Mena Quintero's avatar
Federico Mena Quintero committed
279 280 281 282 283
gdk_pixbuf__png_image_begin_load (ModulePreparedNotifyFunc prepare_func,
				  ModuleUpdatedNotifyFunc update_func,
				  ModuleFrameDoneNotifyFunc frame_done_func,
				  ModuleAnimationDoneNotifyFunc anim_done_func,
				  gpointer user_data)
284 285 286 287 288 289 290
{
        LoadContext* lc;
        
        lc = g_new0(LoadContext, 1);
        
        lc->fatal_error_occurred = FALSE;

291 292
        lc->prepare_func = prepare_func;
        lc->update_func = update_func;
293 294
        lc->notify_user_data = user_data;

295 296 297 298 299 300
        lc->first_row_seen_in_chunk = -1;
        lc->last_row_seen_in_chunk = -1;
        lc->first_pass_seen_in_chunk = -1;
        lc->last_pass_seen_in_chunk = -1;
        lc->max_row_seen_in_chunk = -1;
        
301 302
        /* Create the main PNG context struct */

303
		
304 305 306 307 308 309 310 311 312 313
        lc->png_read_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING,
                                                  lc, /* error/warning callback data */
                                                  png_error_callback,
                                                  png_warning_callback);

        if (lc->png_read_ptr == NULL) {
                g_free(lc);
                return NULL;
        }

314 315 316 317 318 319 320
	if (setjmp (lc->png_read_ptr->jmpbuf)) {
		if (lc->png_info_ptr)
			png_destroy_read_struct(&lc->png_read_ptr, NULL, NULL);
                g_free(lc);
                return NULL;
	}

321
        /* Create the auxiliary context struct */
322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341

        lc->png_info_ptr = png_create_info_struct(lc->png_read_ptr);

        if (lc->png_info_ptr == NULL) {
                png_destroy_read_struct(&lc->png_read_ptr, NULL, NULL);
                g_free(lc);
                return NULL;
        }

        png_set_progressive_read_fn(lc->png_read_ptr,
                                    lc, /* callback data */
                                    png_info_callback,
                                    png_row_callback,
                                    png_end_callback);
        

        return lc;
}

void
Federico Mena Quintero's avatar
Federico Mena Quintero committed
342
gdk_pixbuf__png_image_stop_load (gpointer context)
343 344 345 346 347 348 349 350 351 352 353
{
        LoadContext* lc = context;

        g_return_if_fail(lc != NULL);

        gdk_pixbuf_unref(lc->pixbuf);
        
        png_destroy_read_struct(&lc->png_read_ptr, NULL, NULL);
        g_free(lc);
}

354
gboolean
Federico Mena Quintero's avatar
Federico Mena Quintero committed
355
gdk_pixbuf__png_image_load_increment(gpointer context, guchar *buf, guint size)
356 357 358 359 360
{
        LoadContext* lc = context;

        g_return_val_if_fail(lc != NULL, FALSE);

361 362 363 364 365 366 367
        /* reset */
        lc->first_row_seen_in_chunk = -1;
        lc->last_row_seen_in_chunk = -1;
        lc->first_pass_seen_in_chunk = -1;
        lc->last_pass_seen_in_chunk = -1;
        lc->max_row_seen_in_chunk = -1;
        
368
        /* Invokes our callbacks as needed */
369 370 371 372 373
	if (setjmp (lc->png_read_ptr->jmpbuf)) {
		return FALSE;
	} else {
		png_process_data(lc->png_read_ptr, lc->png_info_ptr, buf, size);
	}
374 375 376

        if (lc->fatal_error_occurred)
                return FALSE;
377 378 379 380 381 382 383 384 385
        else {
                if (lc->first_row_seen_in_chunk >= 0) {
                        /* We saw at least one row */
                        gint pass_diff = lc->last_pass_seen_in_chunk - lc->first_pass_seen_in_chunk;
                        
                        g_assert(pass_diff >= 0);
                        
                        if (pass_diff == 0) {
                                /* start and end row were in the same pass */
386
                                (lc->update_func)(lc->pixbuf, 0,
387
                                                  lc->first_row_seen_in_chunk,
388
                                                  lc->pixbuf->width,
389
                                                  (lc->last_row_seen_in_chunk -
390 391
                                                   lc->first_row_seen_in_chunk) + 1,
						  lc->notify_user_data);
392 393 394 395 396 397
                        } else if (pass_diff == 1) {
                                /* We have from the first row seen to
                                   the end of the image (max row
                                   seen), then from the top of the
                                   image to the last row seen */
                                /* first row to end */
398
                                (lc->update_func)(lc->pixbuf, 0,
399
                                                  lc->first_row_seen_in_chunk,
400
                                                  lc->pixbuf->width,
401
                                                  (lc->max_row_seen_in_chunk -
402 403
                                                   lc->first_row_seen_in_chunk) + 1,
						  lc->notify_user_data);
404
                                /* top to last row */
405
                                (lc->update_func)(lc->pixbuf,
406
                                                  0, 0, 
407
                                                  lc->pixbuf->width,
408 409
                                                  lc->last_row_seen_in_chunk + 1,
						  lc->notify_user_data);
410 411 412
                        } else {
                                /* We made at least one entire pass, so update the
                                   whole image */
413
                                (lc->update_func)(lc->pixbuf,
414
                                                  0, 0, 
415
                                                  lc->pixbuf->width,
416 417
                                                  lc->max_row_seen_in_chunk + 1,
						  lc->notify_user_data);
418 419 420
                        }
                }
                
421
                return TRUE;
422
        }
423 424 425 426 427 428 429 430 431
}

/* Called at the start of the progressive load, once we have image info */
static void
png_info_callback   (png_structp png_read_ptr,
                     png_infop   png_info_ptr)
{
        LoadContext* lc;
        png_uint_32 width, height;
432
        int color_type;
433
        gboolean have_alpha = FALSE;
434
        gboolean failed = FALSE;
435 436 437 438 439 440 441
        
        lc = png_get_progressive_ptr(png_read_ptr);

        if (lc->fatal_error_occurred)
                return;
        

442 443 444 445
        setup_png_transformations(lc->png_read_ptr,
                                  lc->png_info_ptr,
                                  &failed,
                                  &width, &height, &color_type);
446

447 448 449
        if (failed) {
                lc->fatal_error_occurred = TRUE;
                return;
450 451 452 453
        }

        /* If we have alpha, set a flag */
        if (color_type & PNG_COLOR_MASK_ALPHA)
454
                have_alpha = TRUE;
455
        
456
        lc->pixbuf = gdk_pixbuf_new(GDK_COLORSPACE_RGB, have_alpha, 8, width, height);
457 458 459 460 461 462 463 464 465

        if (lc->pixbuf == NULL) {
                /* Failed to allocate memory */
                lc->fatal_error_occurred = TRUE;
                return;
        }
        
        /* Notify the client that we are ready to go */

466 467
        if (lc->prepare_func)
                (* lc->prepare_func) (lc->pixbuf, lc->notify_user_data);
468 469 470 471 472 473 474 475 476 477 478 479 480 481
        
        return;
}

/* Called for each row; note that you will get duplicate row numbers
   for interlaced PNGs */
static void
png_row_callback   (png_structp png_read_ptr,
                    png_bytep   new_row,
                    png_uint_32 row_num,
                    int pass_num)
{
        LoadContext* lc;
        guchar* old_row = NULL;
482

483 484 485 486
        lc = png_get_progressive_ptr(png_read_ptr);

        if (lc->fatal_error_occurred)
                return;
487 488 489 490 491 492 493 494 495 496

        if (lc->first_row_seen_in_chunk < 0) {
                lc->first_row_seen_in_chunk = row_num;
                lc->first_pass_seen_in_chunk = pass_num;
        }

        lc->max_row_seen_in_chunk = MAX(lc->max_row_seen_in_chunk, ((gint)row_num));
        lc->last_row_seen_in_chunk = row_num;
        lc->last_pass_seen_in_chunk = pass_num;
        
497
        old_row = lc->pixbuf->pixels + (row_num * lc->pixbuf->rowstride);
498

499 500 501 502 503 504 505
        png_progressive_combine_row(lc->png_read_ptr, old_row, new_row);
}

/* Called after reading the entire image */
static void
png_end_callback   (png_structp png_read_ptr,
                    png_infop   png_info_ptr)
506
{
507 508 509 510 511 512
        LoadContext* lc;

        lc = png_get_progressive_ptr(png_read_ptr);

        if (lc->fatal_error_occurred)
                return;
513
}
514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537

static void
png_error_callback(png_structp png_read_ptr,
                   png_const_charp error_msg)
{
        LoadContext* lc;
        
        lc = png_get_error_ptr(png_read_ptr);
        
        lc->fatal_error_occurred = TRUE;
        
        fprintf(stderr, "Fatal error loading PNG: %s\n", error_msg);
}

static void
png_warning_callback(png_structp png_read_ptr,
                     png_const_charp warning_msg)
{
        LoadContext* lc;
        
        lc = png_get_error_ptr(png_read_ptr);
        
        fprintf(stderr, "Warning loading PNG: %s\n", warning_msg);
}
538 539


540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646
/* Save */
gboolean
gdk_pixbuf__png_image_save (FILE          *f, 
                            GdkPixbuf     *pixbuf, 
                            gchar        **keys,
                            gchar        **values,
                            GError       **error)
{
        /* FIXME error handling is broken */
        
       png_structp png_ptr;
       png_infop info_ptr;
       guchar *ptr;
       guchar *pixels;
       int x, y, j;
       png_bytep row_ptr, data = NULL;
       png_color_8 sig_bit;
       int w, h, rowstride;
       int has_alpha;
       int bpc;

       if (keys && *keys) {
               g_warning ("Bad option name '%s' passed to PNG saver",
                          *keys);
               return FALSE;
#if 0
               gchar **kiter = keys;
               gchar **viter = values;

               
               while (*kiter) {
                       
                       ++kiter;
                       ++viter;
               }
#endif
       }
       
       bpc = gdk_pixbuf_get_bits_per_sample (pixbuf);
       w = gdk_pixbuf_get_width (pixbuf);
       h = gdk_pixbuf_get_height (pixbuf);
       rowstride = gdk_pixbuf_get_rowstride (pixbuf);
       has_alpha = gdk_pixbuf_get_has_alpha (pixbuf);
       pixels = gdk_pixbuf_get_pixels (pixbuf);

       png_ptr = png_create_write_struct (PNG_LIBPNG_VER_STRING,
                                          NULL, NULL, NULL);

       g_return_val_if_fail (png_ptr != NULL, FALSE);

       info_ptr = png_create_info_struct (png_ptr);
       if (info_ptr == NULL) {
               png_destroy_write_struct (&png_ptr, (png_infopp) NULL);
               return FALSE;
       }
       if (setjmp (png_ptr->jmpbuf)) {
               png_destroy_write_struct (&png_ptr, (png_infopp) NULL);
               return FALSE;
       }
       png_init_io (png_ptr, f);
       if (has_alpha) {
               png_set_IHDR (png_ptr, info_ptr, w, h, bpc,
                            PNG_COLOR_TYPE_RGB_ALPHA, PNG_INTERLACE_NONE,
                            PNG_COMPRESSION_TYPE_BASE, PNG_FILTER_TYPE_BASE);
#ifdef WORDS_BIGENDIAN
               png_set_swap_alpha (png_ptr);
#else
               png_set_bgr (png_ptr);
#endif
       } else {
               png_set_IHDR (png_ptr, info_ptr, w, h, bpc,
                            PNG_COLOR_TYPE_RGB, PNG_INTERLACE_NONE,
                            PNG_COMPRESSION_TYPE_BASE, PNG_FILTER_TYPE_BASE);
               data = malloc (w * 3 * sizeof (char));
       }
       sig_bit.red = bpc;
       sig_bit.green = bpc;
       sig_bit.blue = bpc;
       sig_bit.alpha = bpc;
       png_set_sBIT (png_ptr, info_ptr, &sig_bit);
       png_write_info (png_ptr, info_ptr);
       png_set_shift (png_ptr, &sig_bit);
       png_set_packing (png_ptr);

       ptr = pixels;
       for (y = 0; y < h; y++) {
               if (has_alpha)
                       row_ptr = (png_bytep)ptr;
               else {
                       for (j = 0, x = 0; x < w; x++)
                               memcpy (&(data[x*3]), &(ptr[x*3]), 3);

                       row_ptr = (png_bytep)data;
               }
               png_write_rows (png_ptr, &row_ptr, 1);
               ptr += rowstride;
       }

       if (data)
               free (data);

       png_write_end (png_ptr, info_ptr);
       png_destroy_write_struct (&png_ptr, (png_infopp) NULL);

       return TRUE;
}

647 648