pngread.c 42.3 KB
Newer Older
G
Guy Schalnat 已提交
1

A
Andreas Dilger 已提交
2
/* pngread.c - read a PNG file
3
 *
4
 * Last changed in libpng 1.4.0 [July 20, 2009]
5
 * Copyright (c) 1998-2009 Glenn Randers-Pehrson
6 7
 * (Version 0.96 Copyright (c) 1996, 1997 Andreas Dilger)
 * (Version 0.88 Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.)
8
 *
9
 * This code is released under the libpng license.
10
 * For conditions of distribution and use, see the disclaimer
11
 * and license in png.h
12
 *
13 14 15
 * This file contains routines that an application calls directly to
 * read a PNG file or stream.
 */
G
Guy Schalnat 已提交
16 17

#include "png.h"
18
#if defined(PNG_READ_SUPPORTED)
19
#include "pngpriv.h"
20

21

A
Andreas Dilger 已提交
22
/* Create a PNG structure for reading, and allocate any memory needed. */
23
png_structp PNGAPI
24
png_create_read_struct(png_const_charp user_png_ver, png_voidp error_ptr,
A
Andreas Dilger 已提交
25
   png_error_ptr error_fn, png_error_ptr warn_fn)
G
Guy Schalnat 已提交
26
{
27 28 29

#ifdef PNG_USER_MEM_SUPPORTED
   return (png_create_read_struct_2(user_png_ver, error_ptr, error_fn,
30
      warn_fn, NULL, NULL, NULL));
31 32 33
}

/* Alternate create PNG structure for reading, and allocate any memory needed. */
34
png_structp PNGAPI
35 36 37 38 39 40
png_create_read_struct_2(png_const_charp user_png_ver, png_voidp error_ptr,
   png_error_ptr error_fn, png_error_ptr warn_fn, png_voidp mem_ptr,
   png_malloc_ptr malloc_fn, png_free_ptr free_fn)
{
#endif /* PNG_USER_MEM_SUPPORTED */

41 42 43 44
#ifdef PNG_SETJMP_SUPPORTED
   volatile
#endif
   png_structp png_ptr;
45
   int png_cleanup_needed = 0;
46 47

#ifdef PNG_SETJMP_SUPPORTED
A
Andreas Dilger 已提交
48 49 50
#ifdef USE_FAR_KEYWORD
   jmp_buf jmpbuf;
#endif
51 52
#endif

53 54
   int i;

55
   png_debug(1, "in png_create_read_struct");
56
#ifdef PNG_USER_MEM_SUPPORTED
57
   png_ptr = (png_structp)png_create_struct_2(PNG_STRUCT_PNG,
58
      malloc_fn, mem_ptr);
59
#else
60
   png_ptr = (png_structp)png_create_struct(PNG_STRUCT_PNG);
61
#endif
62
   if (png_ptr == NULL)
63
      return (NULL);
64

65
   /* Added at libpng-1.2.6 */
66 67 68 69 70
#ifdef PNG_SET_USER_LIMITS_SUPPORTED
   png_ptr->user_width_max=PNG_USER_WIDTH_MAX;
   png_ptr->user_height_max=PNG_USER_HEIGHT_MAX;
#endif

71 72 73 74 75 76 77 78 79
#ifdef PNG_SETJMP_SUPPORTED
/* Applications that neglect to set up their own setjmp() and then
   encounter a png_error() will longjmp here.  Since the jmpbuf is
   then meaningless we abort instead of returning. */
#ifdef USE_FAR_KEYWORD
   if (setjmp(jmpbuf))
#else
   if (setjmp(png_ptr->jmpbuf))
#endif
80
      PNG_ABORT();
81 82
#endif

83 84
#ifdef PNG_USER_MEM_SUPPORTED
   png_set_mem_fn(png_ptr, mem_ptr, malloc_fn, free_fn);
85
#endif
86

A
Andreas Dilger 已提交
87 88
   png_set_error_fn(png_ptr, error_ptr, error_fn, warn_fn);

89
   if (user_png_ver)
G
Guy Schalnat 已提交
90
   {
91 92 93 94 95 96 97 98 99
      i = 0;
      do
      {
         if (user_png_ver[i] != png_libpng_ver[i])
            png_ptr->flags |= PNG_FLAG_LIBRARY_MISMATCH;
      } while (png_libpng_ver[i++]);
    }
    else
         png_ptr->flags |= PNG_FLAG_LIBRARY_MISMATCH;
100

101

102 103 104 105 106 107 108 109 110 111 112
    if (png_ptr->flags & PNG_FLAG_LIBRARY_MISMATCH)
    {
       /* Libpng 0.90 and later are binary incompatible with libpng 0.89, so
       * we must recompile any applications that use any older library version.
       * For versions after libpng 1.0, we will be compatible, so we need
       * only check the first digit.
       */
      if (user_png_ver == NULL || user_png_ver[0] != png_libpng_ver[0] ||
          (user_png_ver[0] == '1' && user_png_ver[2] != png_libpng_ver[2]) ||
          (user_png_ver[0] == '0' && user_png_ver[2] < '9'))
      {
113
#if !defined(PNG_NO_STDIO) && !defined(_WIN32_WCE)
114 115 116 117 118 119 120 121 122
         char msg[80];
         if (user_png_ver)
         {
           png_snprintf(msg, 80,
              "Application was compiled with png.h from libpng-%.20s",
              user_png_ver);
           png_warning(png_ptr, msg);
         }
         png_snprintf(msg, 80,
123
             "Application  is  running with png.c from libpng-%.20s",
124 125
             png_libpng_ver);
         png_warning(png_ptr, msg);
126 127
#endif
#ifdef PNG_ERROR_NUMBERS_SUPPORTED
128
         png_ptr->flags = 0;
129
#endif
130 131
         png_warning(png_ptr,
            "Incompatible libpng version in application and library");
132

133 134
         png_cleanup_needed = 1;
      }
135 136
   }

137 138
   if (!png_cleanup_needed)
   {
139
   /* Initialize zbuf - compression buffer */
G
Guy Schalnat 已提交
140
   png_ptr->zbuf_size = PNG_ZBUF_SIZE;
141
   png_ptr->zbuf = (png_bytep)png_malloc_warn(png_ptr,
142
     png_ptr->zbuf_size);
143
   if (png_ptr->zbuf == NULL)
144
        png_cleanup_needed = 1;
145
   }
A
Andreas Dilger 已提交
146 147 148
   png_ptr->zstream.zalloc = png_zalloc;
   png_ptr->zstream.zfree = png_zfree;
   png_ptr->zstream.opaque = (voidpf)png_ptr;
G
Guy Schalnat 已提交
149

150 151
   if (!png_cleanup_needed)
   {
152 153 154 155 156 157 158 159 160 161 162
      switch (inflateInit(&png_ptr->zstream))
      {
         case Z_OK: /* Do nothing */ break;
         case Z_MEM_ERROR:
         case Z_STREAM_ERROR: png_warning(png_ptr, "zlib memory error");
            png_cleanup_needed = 1; break;
         case Z_VERSION_ERROR: png_warning(png_ptr, "zlib version error");
            png_cleanup_needed = 1; break;
         default: png_warning(png_ptr, "Unknown zlib error");
            png_cleanup_needed = 1;
      }
163 164 165 166
   }

   if (png_cleanup_needed)
   {
167 168 169
      /* Clean up PNG structure and deallocate any memory. */
      png_free(png_ptr, png_ptr->zbuf);
      png_ptr->zbuf = NULL;
170
#ifdef PNG_USER_MEM_SUPPORTED
171 172
      png_destroy_struct_2((png_voidp)png_ptr,
         (png_free_ptr)free_fn, (png_voidp)mem_ptr);
173
#else
174
      png_destroy_struct((png_voidp)png_ptr);
175
#endif
176
      return (NULL);
G
Guy Schalnat 已提交
177 178
   }

A
Andreas Dilger 已提交
179 180
   png_ptr->zstream.next_out = png_ptr->zbuf;
   png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size;
G
Guy Schalnat 已提交
181

182
   png_set_read_fn(png_ptr, NULL, NULL);
G
Guy Schalnat 已提交
183

184

G
Guy Schalnat 已提交
185 186 187
   return (png_ptr);
}

188

189
#ifndef PNG_NO_SEQUENTIAL_READ_SUPPORTED
A
Andreas Dilger 已提交
190
/* Read the information before the actual image data.  This has been
191
 * changed in v0.90 to allow reading a file that already has the magic
A
Andreas Dilger 已提交
192
 * bytes read from the stream.  You can tell libpng how many bytes have
193
 * been read from the beginning of the stream (up to the maximum of 8)
A
Andreas Dilger 已提交
194 195 196 197
 * via png_set_sig_bytes(), and we will only check the remaining bytes
 * here.  The application can then have access to the signature bytes we
 * read if it is determined that this isn't a valid PNG file.
 */
198
void PNGAPI
A
Andreas Dilger 已提交
199
png_read_info(png_structp png_ptr, png_infop info_ptr)
G
Guy Schalnat 已提交
200
{
201 202
   if (png_ptr == NULL || info_ptr == NULL)
      return;
203
   png_debug(1, "in png_read_info");
A
Andreas Dilger 已提交
204 205
   /* If we haven't checked all of the PNG signature bytes, do so now. */
   if (png_ptr->sig_bytes < 8)
G
Guy Schalnat 已提交
206
   {
A
Andreas Dilger 已提交
207 208
      png_size_t num_checked = png_ptr->sig_bytes,
                 num_to_check = 8 - num_checked;
A
Andreas Dilger 已提交
209

210 211 212
#ifdef PNG_IO_STATE_SUPPORTED
      png_ptr->io_state = PNG_IO_READING | PNG_IO_SIGNATURE;
#endif
213

A
Andreas Dilger 已提交
214
      png_read_data(png_ptr, &(info_ptr->signature[num_checked]), num_to_check);
A
Andreas Dilger 已提交
215 216 217 218 219 220 221 222 223 224
      png_ptr->sig_bytes = 8;

      if (png_sig_cmp(info_ptr->signature, num_checked, num_to_check))
      {
         if (num_checked < 4 &&
             png_sig_cmp(info_ptr->signature, num_checked, num_to_check - 4))
            png_error(png_ptr, "Not a PNG file");
         else
            png_error(png_ptr, "PNG file corrupted by ASCII conversion");
      }
225 226
      if (num_checked < 3)
         png_ptr->mode |= PNG_HAVE_PNG_SIGNATURE;
G
Guy Schalnat 已提交
227
   }
G
Guy Schalnat 已提交
228

229
   for (;;)
G
Guy Schalnat 已提交
230
   {
231
#ifdef PNG_USE_LOCAL_ARRAYS
232 233 234 235
      PNG_CONST PNG_IHDR;
      PNG_CONST PNG_IDAT;
      PNG_CONST PNG_IEND;
      PNG_CONST PNG_PLTE;
236
#if defined(PNG_READ_bKGD_SUPPORTED)
237
      PNG_CONST PNG_bKGD;
238 239
#endif
#if defined(PNG_READ_cHRM_SUPPORTED)
240
      PNG_CONST PNG_cHRM;
241 242
#endif
#if defined(PNG_READ_gAMA_SUPPORTED)
243
      PNG_CONST PNG_gAMA;
244 245
#endif
#if defined(PNG_READ_hIST_SUPPORTED)
246
      PNG_CONST PNG_hIST;
247
#endif
248
#if defined(PNG_READ_iCCP_SUPPORTED)
249
      PNG_CONST PNG_iCCP;
250 251
#endif
#if defined(PNG_READ_iTXt_SUPPORTED)
252
      PNG_CONST PNG_iTXt;
253
#endif
254
#if defined(PNG_READ_oFFs_SUPPORTED)
255
      PNG_CONST PNG_oFFs;
256 257
#endif
#if defined(PNG_READ_pCAL_SUPPORTED)
258
      PNG_CONST PNG_pCAL;
259 260
#endif
#if defined(PNG_READ_pHYs_SUPPORTED)
261
      PNG_CONST PNG_pHYs;
262 263
#endif
#if defined(PNG_READ_sBIT_SUPPORTED)
264
      PNG_CONST PNG_sBIT;
265
#endif
266
#if defined(PNG_READ_sCAL_SUPPORTED)
267
      PNG_CONST PNG_sCAL;
268 269
#endif
#if defined(PNG_READ_sPLT_SUPPORTED)
270
      PNG_CONST PNG_sPLT;
271
#endif
272
#if defined(PNG_READ_sRGB_SUPPORTED)
273
      PNG_CONST PNG_sRGB;
274 275
#endif
#if defined(PNG_READ_tEXt_SUPPORTED)
276
      PNG_CONST PNG_tEXt;
277 278
#endif
#if defined(PNG_READ_tIME_SUPPORTED)
279
      PNG_CONST PNG_tIME;
280 281
#endif
#if defined(PNG_READ_tRNS_SUPPORTED)
282
      PNG_CONST PNG_tRNS;
283 284
#endif
#if defined(PNG_READ_zTXt_SUPPORTED)
285
      PNG_CONST PNG_zTXt;
286
#endif
287
#endif /* PNG_USE_LOCAL_ARRAYS */
288
      png_uint_32 length = png_read_chunk_header(png_ptr);
289
      PNG_CONST png_bytep chunk_name = png_ptr->chunk_name;
A
Andreas Dilger 已提交
290 291 292 293

      /* This should be a binary subdivision search or a hash for
       * matching the chunk name rather than a linear search.
       */
294 295
      if (!png_memcmp(chunk_name, png_IDAT, 4))
        if (png_ptr->mode & PNG_AFTER_IDAT)
296 297
          png_ptr->mode |= PNG_HAVE_CHUNK_AFTER_IDAT;

298
      if (!png_memcmp(chunk_name, png_IHDR, 4))
A
Andreas Dilger 已提交
299
         png_handle_IHDR(png_ptr, info_ptr, length);
300
      else if (!png_memcmp(chunk_name, png_IEND, 4))
A
Andreas Dilger 已提交
301
         png_handle_IEND(png_ptr, info_ptr, length);
302
#ifdef PNG_HANDLE_AS_UNKNOWN_SUPPORTED
303
      else if (png_handle_as_unknown(png_ptr, chunk_name))
304
      {
305
         if (!png_memcmp(chunk_name, png_IDAT, 4))
306 307
            png_ptr->mode |= PNG_HAVE_IDAT;
         png_handle_unknown(png_ptr, info_ptr, length);
308
         if (!png_memcmp(chunk_name, png_PLTE, 4))
309
            png_ptr->mode |= PNG_HAVE_PLTE;
310
         else if (!png_memcmp(chunk_name, png_IDAT, 4))
311 312 313 314 315 316 317 318 319 320
         {
            if (!(png_ptr->mode & PNG_HAVE_IHDR))
               png_error(png_ptr, "Missing IHDR before IDAT");
            else if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE &&
                     !(png_ptr->mode & PNG_HAVE_PLTE))
               png_error(png_ptr, "Missing PLTE before IDAT");
            break;
         }
      }
#endif
321
      else if (!png_memcmp(chunk_name, png_PLTE, 4))
322
         png_handle_PLTE(png_ptr, info_ptr, length);
323
      else if (!png_memcmp(chunk_name, png_IDAT, 4))
A
Andreas Dilger 已提交
324 325 326 327 328 329 330 331 332 333 334 335
      {
         if (!(png_ptr->mode & PNG_HAVE_IHDR))
            png_error(png_ptr, "Missing IHDR before IDAT");
         else if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE &&
                  !(png_ptr->mode & PNG_HAVE_PLTE))
            png_error(png_ptr, "Missing PLTE before IDAT");

         png_ptr->idat_size = length;
         png_ptr->mode |= PNG_HAVE_IDAT;
         break;
      }
#if defined(PNG_READ_bKGD_SUPPORTED)
336
      else if (!png_memcmp(chunk_name, png_bKGD, 4))
A
Andreas Dilger 已提交
337
         png_handle_bKGD(png_ptr, info_ptr, length);
G
Guy Schalnat 已提交
338 339
#endif
#if defined(PNG_READ_cHRM_SUPPORTED)
340
      else if (!png_memcmp(chunk_name, png_cHRM, 4))
A
Andreas Dilger 已提交
341
         png_handle_cHRM(png_ptr, info_ptr, length);
G
Guy Schalnat 已提交
342
#endif
A
Andreas Dilger 已提交
343
#if defined(PNG_READ_gAMA_SUPPORTED)
344
      else if (!png_memcmp(chunk_name, png_gAMA, 4))
A
Andreas Dilger 已提交
345
         png_handle_gAMA(png_ptr, info_ptr, length);
G
Guy Schalnat 已提交
346 347
#endif
#if defined(PNG_READ_hIST_SUPPORTED)
348
      else if (!png_memcmp(chunk_name, png_hIST, 4))
A
Andreas Dilger 已提交
349
         png_handle_hIST(png_ptr, info_ptr, length);
G
Guy Schalnat 已提交
350
#endif
A
Andreas Dilger 已提交
351
#if defined(PNG_READ_oFFs_SUPPORTED)
352
      else if (!png_memcmp(chunk_name, png_oFFs, 4))
A
Andreas Dilger 已提交
353 354 355
         png_handle_oFFs(png_ptr, info_ptr, length);
#endif
#if defined(PNG_READ_pCAL_SUPPORTED)
356
      else if (!png_memcmp(chunk_name, png_pCAL, 4))
A
Andreas Dilger 已提交
357 358
         png_handle_pCAL(png_ptr, info_ptr, length);
#endif
359
#if defined(PNG_READ_sCAL_SUPPORTED)
360
      else if (!png_memcmp(chunk_name, png_sCAL, 4))
361 362
         png_handle_sCAL(png_ptr, info_ptr, length);
#endif
G
Guy Schalnat 已提交
363
#if defined(PNG_READ_pHYs_SUPPORTED)
364
      else if (!png_memcmp(chunk_name, png_pHYs, 4))
A
Andreas Dilger 已提交
365
         png_handle_pHYs(png_ptr, info_ptr, length);
G
Guy Schalnat 已提交
366
#endif
A
Andreas Dilger 已提交
367
#if defined(PNG_READ_sBIT_SUPPORTED)
368
      else if (!png_memcmp(chunk_name, png_sBIT, 4))
A
Andreas Dilger 已提交
369 370
         png_handle_sBIT(png_ptr, info_ptr, length);
#endif
371
#if defined(PNG_READ_sRGB_SUPPORTED)
372
      else if (!png_memcmp(chunk_name, png_sRGB, 4))
373 374
         png_handle_sRGB(png_ptr, info_ptr, length);
#endif
375
#if defined(PNG_READ_iCCP_SUPPORTED)
376
      else if (!png_memcmp(chunk_name, png_iCCP, 4))
377 378 379
         png_handle_iCCP(png_ptr, info_ptr, length);
#endif
#if defined(PNG_READ_sPLT_SUPPORTED)
380
      else if (!png_memcmp(chunk_name, png_sPLT, 4))
381 382
         png_handle_sPLT(png_ptr, info_ptr, length);
#endif
A
Andreas Dilger 已提交
383
#if defined(PNG_READ_tEXt_SUPPORTED)
384
      else if (!png_memcmp(chunk_name, png_tEXt, 4))
A
Andreas Dilger 已提交
385
         png_handle_tEXt(png_ptr, info_ptr, length);
G
Guy Schalnat 已提交
386 387
#endif
#if defined(PNG_READ_tIME_SUPPORTED)
388
      else if (!png_memcmp(chunk_name, png_tIME, 4))
A
Andreas Dilger 已提交
389
         png_handle_tIME(png_ptr, info_ptr, length);
G
Guy Schalnat 已提交
390
#endif
A
Andreas Dilger 已提交
391
#if defined(PNG_READ_tRNS_SUPPORTED)
392
      else if (!png_memcmp(chunk_name, png_tRNS, 4))
A
Andreas Dilger 已提交
393
         png_handle_tRNS(png_ptr, info_ptr, length);
G
Guy Schalnat 已提交
394 395
#endif
#if defined(PNG_READ_zTXt_SUPPORTED)
396
      else if (!png_memcmp(chunk_name, png_zTXt, 4))
A
Andreas Dilger 已提交
397
         png_handle_zTXt(png_ptr, info_ptr, length);
398 399
#endif
#if defined(PNG_READ_iTXt_SUPPORTED)
400
      else if (!png_memcmp(chunk_name, png_iTXt, 4))
401
         png_handle_iTXt(png_ptr, info_ptr, length);
G
Guy Schalnat 已提交
402
#endif
A
Andreas Dilger 已提交
403 404
      else
         png_handle_unknown(png_ptr, info_ptr, length);
G
Guy Schalnat 已提交
405 406
   }
}
407
#endif /* PNG_NO_SEQUENTIAL_READ_SUPPORTED */
G
Guy Schalnat 已提交
408

409
/* Optional call to update the users info_ptr structure */
410
void PNGAPI
A
Andreas Dilger 已提交
411
png_read_update_info(png_structp png_ptr, png_infop info_ptr)
G
Guy Schalnat 已提交
412
{
413
   png_debug(1, "in png_read_update_info");
414 415
   if (png_ptr == NULL)
      return;
G
Guy Schalnat 已提交
416
   if (!(png_ptr->flags & PNG_FLAG_ROW_INIT))
G
Guy Schalnat 已提交
417
      png_read_start_row(png_ptr);
418 419 420
   else
      png_warning(png_ptr,
      "Ignoring extra png_read_update_info() call; row buffer not reallocated");
A
Andreas Dilger 已提交
421
   png_read_transform_info(png_ptr, info_ptr);
G
Guy Schalnat 已提交
422 423
}

424
#ifndef PNG_NO_SEQUENTIAL_READ_SUPPORTED
425 426
/* Initialize palette, background, etc, after transformations
 * are set, but before any reading takes place.  This allows
427
 * the user to obtain a gamma-corrected palette, for example.
428 429
 * If the user doesn't call this, we will do it ourselves.
 */
430
void PNGAPI
G
Guy Schalnat 已提交
431
png_start_read_image(png_structp png_ptr)
G
Guy Schalnat 已提交
432
{
433
   png_debug(1, "in png_start_read_image");
434 435
   if (png_ptr == NULL)
      return;
G
Guy Schalnat 已提交
436
   if (!(png_ptr->flags & PNG_FLAG_ROW_INIT))
G
Guy Schalnat 已提交
437
      png_read_start_row(png_ptr);
G
Guy Schalnat 已提交
438
}
439
#endif /* PNG_NO_SEQUENTIAL_READ_SUPPORTED */
G
Guy Schalnat 已提交
440

441
#ifndef PNG_NO_SEQUENTIAL_READ_SUPPORTED
442
void PNGAPI
G
Guy Schalnat 已提交
443
png_read_row(png_structp png_ptr, png_bytep row, png_bytep dsp_row)
G
Guy Schalnat 已提交
444
{
445
#ifdef PNG_USE_LOCAL_ARRAYS
446 447 448 449
   PNG_CONST PNG_IDAT;
   PNG_CONST int png_pass_dsp_mask[7] = {0xff, 0x0f, 0xff, 0x33, 0xff, 0x55,
      0xff};
   PNG_CONST int png_pass_mask[7] = {0x80, 0x08, 0x88, 0x22, 0xaa, 0x55, 0xff};
450
#endif
G
Guy Schalnat 已提交
451
   int ret;
452 453
   if (png_ptr == NULL)
      return;
454
   png_debug2(1, "in png_read_row (row %lu, pass %d)",
455
      (unsigned long) png_ptr->row_number, png_ptr->pass);
G
Guy Schalnat 已提交
456
   if (!(png_ptr->flags & PNG_FLAG_ROW_INIT))
G
Guy Schalnat 已提交
457
      png_read_start_row(png_ptr);
458 459
   if (png_ptr->row_number == 0 && png_ptr->pass == 0)
   {
460
   /* Check for transforms that have been set but were defined out */
461 462
#if defined(PNG_WRITE_INVERT_SUPPORTED) && !defined(PNG_READ_INVERT_SUPPORTED)
   if (png_ptr->transformations & PNG_INVERT_MONO)
463
      png_warning(png_ptr, "PNG_READ_INVERT_SUPPORTED is not defined");
464 465 466
#endif
#if defined(PNG_WRITE_FILLER_SUPPORTED) && !defined(PNG_READ_FILLER_SUPPORTED)
   if (png_ptr->transformations & PNG_FILLER)
467
      png_warning(png_ptr, "PNG_READ_FILLER_SUPPORTED is not defined");
468 469 470
#endif
#if defined(PNG_WRITE_PACKSWAP_SUPPORTED) && !defined(PNG_READ_PACKSWAP_SUPPORTED)
   if (png_ptr->transformations & PNG_PACKSWAP)
471
      png_warning(png_ptr, "PNG_READ_PACKSWAP_SUPPORTED is not defined");
472 473 474
#endif
#if defined(PNG_WRITE_PACK_SUPPORTED) && !defined(PNG_READ_PACK_SUPPORTED)
   if (png_ptr->transformations & PNG_PACK)
475
      png_warning(png_ptr, "PNG_READ_PACK_SUPPORTED is not defined");
476 477 478
#endif
#if defined(PNG_WRITE_SHIFT_SUPPORTED) && !defined(PNG_READ_SHIFT_SUPPORTED)
   if (png_ptr->transformations & PNG_SHIFT)
479
      png_warning(png_ptr, "PNG_READ_SHIFT_SUPPORTED is not defined");
480 481 482
#endif
#if defined(PNG_WRITE_BGR_SUPPORTED) && !defined(PNG_READ_BGR_SUPPORTED)
   if (png_ptr->transformations & PNG_BGR)
483
      png_warning(png_ptr, "PNG_READ_BGR_SUPPORTED is not defined");
484 485 486
#endif
#if defined(PNG_WRITE_SWAP_SUPPORTED) && !defined(PNG_READ_SWAP_SUPPORTED)
   if (png_ptr->transformations & PNG_SWAP_BYTES)
487
      png_warning(png_ptr, "PNG_READ_SWAP_SUPPORTED is not defined");
488 489
#endif
   }
G
Guy Schalnat 已提交
490

G
Guy Schalnat 已提交
491
#if defined(PNG_READ_INTERLACING_SUPPORTED)
492
   /* If interlaced and we do not need a new row, combine row and return */
G
Guy Schalnat 已提交
493 494 495 496 497
   if (png_ptr->interlaced && (png_ptr->transformations & PNG_INTERLACE))
   {
      switch (png_ptr->pass)
      {
         case 0:
498
            if (png_ptr->row_number & 0x07)
G
Guy Schalnat 已提交
499
            {
A
Andreas Dilger 已提交
500
               if (dsp_row != NULL)
G
Guy Schalnat 已提交
501 502
                  png_combine_row(png_ptr, dsp_row,
                     png_pass_dsp_mask[png_ptr->pass]);
G
Guy Schalnat 已提交
503
               png_read_finish_row(png_ptr);
G
Guy Schalnat 已提交
504 505 506 507
               return;
            }
            break;
         case 1:
508
            if ((png_ptr->row_number & 0x07) || png_ptr->width < 5)
G
Guy Schalnat 已提交
509
            {
A
Andreas Dilger 已提交
510
               if (dsp_row != NULL)
G
Guy Schalnat 已提交
511 512 513 514 515 516 517
                  png_combine_row(png_ptr, dsp_row,
                     png_pass_dsp_mask[png_ptr->pass]);
               png_read_finish_row(png_ptr);
               return;
            }
            break;
         case 2:
518
            if ((png_ptr->row_number & 0x07) != 4)
G
Guy Schalnat 已提交
519
            {
A
Andreas Dilger 已提交
520
               if (dsp_row != NULL && (png_ptr->row_number & 4))
G
Guy Schalnat 已提交
521
                  png_combine_row(png_ptr, dsp_row,
G
Guy Schalnat 已提交
522 523 524 525 526 527 528 529
                     png_pass_dsp_mask[png_ptr->pass]);
               png_read_finish_row(png_ptr);
               return;
            }
            break;
         case 3:
            if ((png_ptr->row_number & 3) || png_ptr->width < 3)
            {
A
Andreas Dilger 已提交
530
               if (dsp_row != NULL)
G
Guy Schalnat 已提交
531 532 533 534 535 536 537 538
                  png_combine_row(png_ptr, dsp_row,
                     png_pass_dsp_mask[png_ptr->pass]);
               png_read_finish_row(png_ptr);
               return;
            }
            break;
         case 4:
            if ((png_ptr->row_number & 3) != 2)
G
Guy Schalnat 已提交
539
            {
A
Andreas Dilger 已提交
540
               if (dsp_row != NULL && (png_ptr->row_number & 2))
G
Guy Schalnat 已提交
541 542 543 544 545 546 547 548 549
                  png_combine_row(png_ptr, dsp_row,
                     png_pass_dsp_mask[png_ptr->pass]);
               png_read_finish_row(png_ptr);
               return;
            }
            break;
         case 5:
            if ((png_ptr->row_number & 1) || png_ptr->width < 2)
            {
A
Andreas Dilger 已提交
550
               if (dsp_row != NULL)
G
Guy Schalnat 已提交
551 552 553 554 555 556
                  png_combine_row(png_ptr, dsp_row,
                     png_pass_dsp_mask[png_ptr->pass]);
               png_read_finish_row(png_ptr);
               return;
            }
            break;
G
Guy Schalnat 已提交
557
         case 6:
G
Guy Schalnat 已提交
558 559 560 561 562 563 564 565
            if (!(png_ptr->row_number & 1))
            {
               png_read_finish_row(png_ptr);
               return;
            }
            break;
      }
   }
G
Guy Schalnat 已提交
566
#endif
G
Guy Schalnat 已提交
567

G
Guy Schalnat 已提交
568 569
   if (!(png_ptr->mode & PNG_HAVE_IDAT))
      png_error(png_ptr, "Invalid attempt to read row data");
G
Guy Schalnat 已提交
570

A
Andreas Dilger 已提交
571 572
   png_ptr->zstream.next_out = png_ptr->row_buf;
   png_ptr->zstream.avail_out = (uInt)png_ptr->irowbytes;
G
Guy Schalnat 已提交
573 574
   do
   {
A
Andreas Dilger 已提交
575
      if (!(png_ptr->zstream.avail_in))
G
Guy Schalnat 已提交
576 577 578
      {
         while (!png_ptr->idat_size)
         {
A
Andreas Dilger 已提交
579
            png_crc_finish(png_ptr, 0);
G
Guy Schalnat 已提交
580

581
            png_ptr->idat_size = png_read_chunk_header(png_ptr);
A
Andreas Dilger 已提交
582 583
            if (png_memcmp(png_ptr->chunk_name, png_IDAT, 4))
               png_error(png_ptr, "Not enough image data");
G
Guy Schalnat 已提交
584
         }
A
Andreas Dilger 已提交
585 586
         png_ptr->zstream.avail_in = (uInt)png_ptr->zbuf_size;
         png_ptr->zstream.next_in = png_ptr->zbuf;
G
Guy Schalnat 已提交
587
         if (png_ptr->zbuf_size > png_ptr->idat_size)
A
Andreas Dilger 已提交
588
            png_ptr->zstream.avail_in = (uInt)png_ptr->idat_size;
A
Andreas Dilger 已提交
589 590
         png_crc_read(png_ptr, png_ptr->zbuf,
            (png_size_t)png_ptr->zstream.avail_in);
A
Andreas Dilger 已提交
591
         png_ptr->idat_size -= png_ptr->zstream.avail_in;
G
Guy Schalnat 已提交
592
      }
A
Andreas Dilger 已提交
593
      ret = inflate(&png_ptr->zstream, Z_PARTIAL_FLUSH);
G
Guy Schalnat 已提交
594 595
      if (ret == Z_STREAM_END)
      {
A
Andreas Dilger 已提交
596
         if (png_ptr->zstream.avail_out || png_ptr->zstream.avail_in ||
G
Guy Schalnat 已提交
597
            png_ptr->idat_size)
598
            png_benign_error(png_ptr, "Extra compressed data");
A
Andreas Dilger 已提交
599
         png_ptr->mode |= PNG_AFTER_IDAT;
G
Guy Schalnat 已提交
600
         png_ptr->flags |= PNG_FLAG_ZLIB_FINISHED;
G
Guy Schalnat 已提交
601
         break;
G
Guy Schalnat 已提交
602 603
      }
      if (ret != Z_OK)
A
Andreas Dilger 已提交
604
         png_error(png_ptr, png_ptr->zstream.msg ? png_ptr->zstream.msg :
G
Guy Schalnat 已提交
605
                   "Decompression error");
A
Andreas Dilger 已提交
606 607

   } while (png_ptr->zstream.avail_out);
G
Guy Schalnat 已提交
608 609 610 611 612 613

   png_ptr->row_info.color_type = png_ptr->color_type;
   png_ptr->row_info.width = png_ptr->iwidth;
   png_ptr->row_info.channels = png_ptr->channels;
   png_ptr->row_info.bit_depth = png_ptr->bit_depth;
   png_ptr->row_info.pixel_depth = png_ptr->pixel_depth;
614 615
   png_ptr->row_info.rowbytes = PNG_ROWBYTES(png_ptr->row_info.pixel_depth,
       png_ptr->row_info.width);
G
Guy Schalnat 已提交
616

617
   if (png_ptr->row_buf[0])
G
Guy Schalnat 已提交
618 619 620
   png_read_filter_row(png_ptr, &(png_ptr->row_info),
      png_ptr->row_buf + 1, png_ptr->prev_row + 1,
      (int)(png_ptr->row_buf[0]));
G
Guy Schalnat 已提交
621

622
   png_memcpy(png_ptr->prev_row, png_ptr->row_buf, png_ptr->rowbytes + 1);
623

624
#if defined(PNG_MNG_FEATURES_SUPPORTED)
625
   if ((png_ptr->mng_features_permitted & PNG_FLAG_MNG_FILTER_64) &&
626 627 628 629 630 631
      (png_ptr->filter_type == PNG_INTRAPIXEL_DIFFERENCING))
   {
      /* Intrapixel differencing */
      png_do_read_intrapixel(&(png_ptr->row_info), png_ptr->row_buf + 1);
   }
#endif
G
Guy Schalnat 已提交
632

633

634
   if (png_ptr->transformations || (png_ptr->flags&PNG_FLAG_STRIP_ALPHA))
G
Guy Schalnat 已提交
635 636
      png_do_read_transformations(png_ptr);

G
Guy Schalnat 已提交
637
#if defined(PNG_READ_INTERLACING_SUPPORTED)
638
   /* Blow up interlaced rows to full size */
G
Guy Schalnat 已提交
639 640 641 642
   if (png_ptr->interlaced &&
      (png_ptr->transformations & PNG_INTERLACE))
   {
      if (png_ptr->pass < 6)
643
         /* Old interface (pre-1.0.9):
644 645 646
          * png_do_read_interlace(&(png_ptr->row_info),
          *    png_ptr->row_buf + 1, png_ptr->pass, png_ptr->transformations);
          */
647
         png_do_read_interlace(png_ptr);
G
Guy Schalnat 已提交
648

A
Andreas Dilger 已提交
649
      if (dsp_row != NULL)
G
Guy Schalnat 已提交
650 651
         png_combine_row(png_ptr, dsp_row,
            png_pass_dsp_mask[png_ptr->pass]);
A
Andreas Dilger 已提交
652
      if (row != NULL)
G
Guy Schalnat 已提交
653 654 655 656
         png_combine_row(png_ptr, row,
            png_pass_mask[png_ptr->pass]);
   }
   else
G
Guy Schalnat 已提交
657
#endif
G
Guy Schalnat 已提交
658
   {
A
Andreas Dilger 已提交
659
      if (row != NULL)
G
Guy Schalnat 已提交
660
         png_combine_row(png_ptr, row, 0xff);
A
Andreas Dilger 已提交
661
      if (dsp_row != NULL)
G
Guy Schalnat 已提交
662 663 664
         png_combine_row(png_ptr, dsp_row, 0xff);
   }
   png_read_finish_row(png_ptr);
665 666 667

   if (png_ptr->read_row_fn != NULL)
      (*(png_ptr->read_row_fn))(png_ptr, png_ptr->row_number, png_ptr->pass);
G
Guy Schalnat 已提交
668
}
669
#endif /* PNG_NO_SEQUENTIAL_READ_SUPPORTED */
G
Guy Schalnat 已提交
670

671
#ifndef PNG_NO_SEQUENTIAL_READ_SUPPORTED
A
Andreas Dilger 已提交
672
/* Read one or more rows of image data.  If the image is interlaced,
673 674
 * and png_set_interlace_handling() has been called, the rows need to
 * contain the contents of the rows from the previous pass.  If the
675
 * image has alpha or transparency, and png_handle_alpha()[*] has been
676 677
 * called, the rows contents must be initialized to the contents of the
 * screen.
678
 *
679 680 681 682 683 684 685 686 687 688 689 690 691
 * "row" holds the actual image, and pixels are placed in it
 * as they arrive.  If the image is displayed after each pass, it will
 * appear to "sparkle" in.  "display_row" can be used to display a
 * "chunky" progressive image, with finer detail added as it becomes
 * available.  If you do not want this "chunky" display, you may pass
 * NULL for display_row.  If you do not want the sparkle display, and
 * you have not called png_handle_alpha(), you may pass NULL for rows.
 * If you have called png_handle_alpha(), and the image has either an
 * alpha channel or a transparency chunk, you must provide a buffer for
 * rows.  In this case, you do not have to provide a display_row buffer
 * also, but you may.  If the image is not interlaced, or if you have
 * not called png_set_interlace_handling(), the display_row buffer will
 * be ignored, so pass NULL to it.
692
 *
693
 * [*] png_handle_alpha() does not exist yet, as of this version of libpng
694
 */
G
Guy Schalnat 已提交
695

696
void PNGAPI
G
Guy Schalnat 已提交
697
png_read_rows(png_structp png_ptr, png_bytepp row,
G
Guy Schalnat 已提交
698
   png_bytepp display_row, png_uint_32 num_rows)
G
Guy Schalnat 已提交
699
{
G
Guy Schalnat 已提交
700 701 702
   png_uint_32 i;
   png_bytepp rp;
   png_bytepp dp;
G
Guy Schalnat 已提交
703

704
   png_debug(1, "in png_read_rows");
705 706
   if (png_ptr == NULL)
      return;
G
Guy Schalnat 已提交
707 708
   rp = row;
   dp = display_row;
709
   if (rp != NULL && dp != NULL)
710 711 712 713
      for (i = 0; i < num_rows; i++)
      {
         png_bytep rptr = *rp++;
         png_bytep dptr = *dp++;
714

715 716
         png_read_row(png_ptr, rptr, dptr);
      }
717
   else if (rp != NULL)
718 719
      for (i = 0; i < num_rows; i++)
      {
720
         png_bytep rptr = *rp;
721
         png_read_row(png_ptr, rptr, NULL);
722 723
         rp++;
      }
724
   else if (dp != NULL)
725 726 727
      for (i = 0; i < num_rows; i++)
      {
         png_bytep dptr = *dp;
728
         png_read_row(png_ptr, NULL, dptr);
729
         dp++;
730
      }
G
Guy Schalnat 已提交
731
}
732
#endif /* PNG_NO_SEQUENTIAL_READ_SUPPORTED */
G
Guy Schalnat 已提交
733

734
#ifndef PNG_NO_SEQUENTIAL_READ_SUPPORTED
A
Andreas Dilger 已提交
735
/* Read the entire image.  If the image has an alpha channel or a tRNS
736
 * chunk, and you have called png_handle_alpha()[*], you will need to
737 738 739 740 741 742 743
 * initialize the image to the current image that PNG will be overlaying.
 * We set the num_rows again here, in case it was incorrectly set in
 * png_read_start_row() by a call to png_read_update_info() or
 * png_start_read_image() if png_set_interlace_handling() wasn't called
 * prior to either of these functions like it should have been.  You can
 * only call this function once.  If you desire to have an image for
 * each pass of a interlaced image, use png_read_rows() instead.
744
 *
745
 * [*] png_handle_alpha() does not exist yet, as of this version of libpng
746
 */
747
void PNGAPI
G
Guy Schalnat 已提交
748
png_read_image(png_structp png_ptr, png_bytepp image)
G
Guy Schalnat 已提交
749
{
750
   png_uint_32 i, image_height;
G
Guy Schalnat 已提交
751
   int pass, j;
G
Guy Schalnat 已提交
752
   png_bytepp rp;
G
Guy Schalnat 已提交
753

754
   png_debug(1, "in png_read_image");
755 756
   if (png_ptr == NULL)
      return;
757 758

#ifdef PNG_READ_INTERLACING_SUPPORTED
G
Guy Schalnat 已提交
759
   pass = png_set_interlace_handling(png_ptr);
760 761 762
#else
   if (png_ptr->interlaced)
      png_error(png_ptr,
763
        "Cannot read interlaced image -- interlace handler disabled");
764 765 766
   pass = 1;
#endif

767

768 769
   image_height=png_ptr->height;
   png_ptr->num_rows = image_height; /* Make sure this is set correctly */
G
Guy Schalnat 已提交
770

G
Guy Schalnat 已提交
771 772 773
   for (j = 0; j < pass; j++)
   {
      rp = image;
774
      for (i = 0; i < image_height; i++)
G
Guy Schalnat 已提交
775
      {
776
         png_read_row(png_ptr, *rp, NULL);
G
Guy Schalnat 已提交
777 778 779 780
         rp++;
      }
   }
}
781
#endif /* PNG_NO_SEQUENTIAL_READ_SUPPORTED */
G
Guy Schalnat 已提交
782

783
#ifndef PNG_NO_SEQUENTIAL_READ_SUPPORTED
A
Andreas Dilger 已提交
784
/* Read the end of the PNG file.  Will not read past the end of the
785 786 787
 * file, will verify the end is accurate, and will read any comments
 * or time information at the end of the file, if info is not NULL.
 */
788
void PNGAPI
A
Andreas Dilger 已提交
789
png_read_end(png_structp png_ptr, png_infop info_ptr)
G
Guy Schalnat 已提交
790
{
791
   png_debug(1, "in png_read_end");
792 793
   if (png_ptr == NULL)
      return;
A
Andreas Dilger 已提交
794
   png_crc_finish(png_ptr, 0); /* Finish off CRC from last IDAT chunk */
G
Guy Schalnat 已提交
795 796 797

   do
   {
798
#ifdef PNG_USE_LOCAL_ARRAYS
799 800 801 802
      PNG_CONST PNG_IHDR;
      PNG_CONST PNG_IDAT;
      PNG_CONST PNG_IEND;
      PNG_CONST PNG_PLTE;
803
#if defined(PNG_READ_bKGD_SUPPORTED)
804
      PNG_CONST PNG_bKGD;
805 806
#endif
#if defined(PNG_READ_cHRM_SUPPORTED)
807
      PNG_CONST PNG_cHRM;
808 809
#endif
#if defined(PNG_READ_gAMA_SUPPORTED)
810
      PNG_CONST PNG_gAMA;
811 812
#endif
#if defined(PNG_READ_hIST_SUPPORTED)
813
      PNG_CONST PNG_hIST;
814
#endif
815
#if defined(PNG_READ_iCCP_SUPPORTED)
816
      PNG_CONST PNG_iCCP;
817 818
#endif
#if defined(PNG_READ_iTXt_SUPPORTED)
819
      PNG_CONST PNG_iTXt;
820
#endif
821
#if defined(PNG_READ_oFFs_SUPPORTED)
822
      PNG_CONST PNG_oFFs;
823 824
#endif
#if defined(PNG_READ_pCAL_SUPPORTED)
825
      PNG_CONST PNG_pCAL;
826 827
#endif
#if defined(PNG_READ_pHYs_SUPPORTED)
828
      PNG_CONST PNG_pHYs;
829 830
#endif
#if defined(PNG_READ_sBIT_SUPPORTED)
831
      PNG_CONST PNG_sBIT;
832
#endif
833
#if defined(PNG_READ_sCAL_SUPPORTED)
834
      PNG_CONST PNG_sCAL;
835 836
#endif
#if defined(PNG_READ_sPLT_SUPPORTED)
837
      PNG_CONST PNG_sPLT;
838
#endif
839
#if defined(PNG_READ_sRGB_SUPPORTED)
840
      PNG_CONST PNG_sRGB;
841 842
#endif
#if defined(PNG_READ_tEXt_SUPPORTED)
843
      PNG_CONST PNG_tEXt;
844 845
#endif
#if defined(PNG_READ_tIME_SUPPORTED)
846
      PNG_CONST PNG_tIME;
847 848
#endif
#if defined(PNG_READ_tRNS_SUPPORTED)
849
      PNG_CONST PNG_tRNS;
850 851
#endif
#if defined(PNG_READ_zTXt_SUPPORTED)
852
      PNG_CONST PNG_zTXt;
853
#endif
854
#endif /* PNG_USE_LOCAL_ARRAYS */
855 856
      png_uint_32 length = png_read_chunk_header(png_ptr);
      PNG_CONST png_bytep chunk_name = png_ptr->chunk_name;
A
Andreas Dilger 已提交
857

858
      if (!png_memcmp(chunk_name, png_IHDR, 4))
A
Andreas Dilger 已提交
859
         png_handle_IHDR(png_ptr, info_ptr, length);
860
      else if (!png_memcmp(chunk_name, png_IEND, 4))
861 862
         png_handle_IEND(png_ptr, info_ptr, length);
#ifdef PNG_HANDLE_AS_UNKNOWN_SUPPORTED
863
      else if (png_handle_as_unknown(png_ptr, chunk_name))
864
      {
865
         if (!png_memcmp(chunk_name, png_IDAT, 4))
866
         {
867
            if ((length > 0) || (png_ptr->mode & PNG_HAVE_CHUNK_AFTER_IDAT))
868
               png_benign_error(png_ptr, "Too many IDATs found");
869 870
         }
         png_handle_unknown(png_ptr, info_ptr, length);
871
         if (!png_memcmp(chunk_name, png_PLTE, 4))
872 873 874
            png_ptr->mode |= PNG_HAVE_PLTE;
      }
#endif
875
      else if (!png_memcmp(chunk_name, png_IDAT, 4))
A
Andreas Dilger 已提交
876 877 878 879
      {
         /* Zero length IDATs are legal after the last IDAT has been
          * read, but not after other chunks have been read.
          */
880
         if ((length > 0) || (png_ptr->mode & PNG_HAVE_CHUNK_AFTER_IDAT))
881
            png_benign_error(png_ptr, "Too many IDATs found");
882
         png_crc_finish(png_ptr, length);
A
Andreas Dilger 已提交
883
      }
884
      else if (!png_memcmp(chunk_name, png_PLTE, 4))
A
Andreas Dilger 已提交
885
         png_handle_PLTE(png_ptr, info_ptr, length);
A
Andreas Dilger 已提交
886
#if defined(PNG_READ_bKGD_SUPPORTED)
887
      else if (!png_memcmp(chunk_name, png_bKGD, 4))
A
Andreas Dilger 已提交
888
         png_handle_bKGD(png_ptr, info_ptr, length);
A
Andreas Dilger 已提交
889 890
#endif
#if defined(PNG_READ_cHRM_SUPPORTED)
891
      else if (!png_memcmp(chunk_name, png_cHRM, 4))
A
Andreas Dilger 已提交
892 893 894
         png_handle_cHRM(png_ptr, info_ptr, length);
#endif
#if defined(PNG_READ_gAMA_SUPPORTED)
895
      else if (!png_memcmp(chunk_name, png_gAMA, 4))
A
Andreas Dilger 已提交
896 897 898
         png_handle_gAMA(png_ptr, info_ptr, length);
#endif
#if defined(PNG_READ_hIST_SUPPORTED)
899
      else if (!png_memcmp(chunk_name, png_hIST, 4))
A
Andreas Dilger 已提交
900
         png_handle_hIST(png_ptr, info_ptr, length);
A
Andreas Dilger 已提交
901 902
#endif
#if defined(PNG_READ_oFFs_SUPPORTED)
903
      else if (!png_memcmp(chunk_name, png_oFFs, 4))
A
Andreas Dilger 已提交
904
         png_handle_oFFs(png_ptr, info_ptr, length);
A
Andreas Dilger 已提交
905 906
#endif
#if defined(PNG_READ_pCAL_SUPPORTED)
907
      else if (!png_memcmp(chunk_name, png_pCAL, 4))
A
Andreas Dilger 已提交
908 909
         png_handle_pCAL(png_ptr, info_ptr, length);
#endif
910
#if defined(PNG_READ_sCAL_SUPPORTED)
911
      else if (!png_memcmp(chunk_name, png_sCAL, 4))
912 913
         png_handle_sCAL(png_ptr, info_ptr, length);
#endif
A
Andreas Dilger 已提交
914
#if defined(PNG_READ_pHYs_SUPPORTED)
915
      else if (!png_memcmp(chunk_name, png_pHYs, 4))
A
Andreas Dilger 已提交
916 917 918
         png_handle_pHYs(png_ptr, info_ptr, length);
#endif
#if defined(PNG_READ_sBIT_SUPPORTED)
919
      else if (!png_memcmp(chunk_name, png_sBIT, 4))
A
Andreas Dilger 已提交
920
         png_handle_sBIT(png_ptr, info_ptr, length);
G
Guy Schalnat 已提交
921
#endif
922
#if defined(PNG_READ_sRGB_SUPPORTED)
923
      else if (!png_memcmp(chunk_name, png_sRGB, 4))
924 925
         png_handle_sRGB(png_ptr, info_ptr, length);
#endif
926
#if defined(PNG_READ_iCCP_SUPPORTED)
927
      else if (!png_memcmp(chunk_name, png_iCCP, 4))
928 929 930
         png_handle_iCCP(png_ptr, info_ptr, length);
#endif
#if defined(PNG_READ_sPLT_SUPPORTED)
931
      else if (!png_memcmp(chunk_name, png_sPLT, 4))
932 933
         png_handle_sPLT(png_ptr, info_ptr, length);
#endif
G
Guy Schalnat 已提交
934
#if defined(PNG_READ_tEXt_SUPPORTED)
935
      else if (!png_memcmp(chunk_name, png_tEXt, 4))
A
Andreas Dilger 已提交
936
         png_handle_tEXt(png_ptr, info_ptr, length);
G
Guy Schalnat 已提交
937
#endif
A
Andreas Dilger 已提交
938
#if defined(PNG_READ_tIME_SUPPORTED)
939
      else if (!png_memcmp(chunk_name, png_tIME, 4))
A
Andreas Dilger 已提交
940 941 942
         png_handle_tIME(png_ptr, info_ptr, length);
#endif
#if defined(PNG_READ_tRNS_SUPPORTED)
943
      else if (!png_memcmp(chunk_name, png_tRNS, 4))
A
Andreas Dilger 已提交
944 945
         png_handle_tRNS(png_ptr, info_ptr, length);
#endif
G
Guy Schalnat 已提交
946
#if defined(PNG_READ_zTXt_SUPPORTED)
947
      else if (!png_memcmp(chunk_name, png_zTXt, 4))
A
Andreas Dilger 已提交
948
         png_handle_zTXt(png_ptr, info_ptr, length);
949 950
#endif
#if defined(PNG_READ_iTXt_SUPPORTED)
951
      else if (!png_memcmp(chunk_name, png_iTXt, 4))
952
         png_handle_iTXt(png_ptr, info_ptr, length);
G
Guy Schalnat 已提交
953
#endif
G
Guy Schalnat 已提交
954
      else
A
Andreas Dilger 已提交
955 956
         png_handle_unknown(png_ptr, info_ptr, length);
   } while (!(png_ptr->mode & PNG_HAVE_IEND));
G
Guy Schalnat 已提交
957
}
958
#endif /* PNG_NO_SEQUENTIAL_READ_SUPPORTED */
G
Guy Schalnat 已提交
959

960
/* Free all memory used by the read */
961
void PNGAPI
G
Guy Schalnat 已提交
962
png_destroy_read_struct(png_structpp png_ptr_ptr, png_infopp info_ptr_ptr,
A
Andreas Dilger 已提交
963
   png_infopp end_info_ptr_ptr)
G
Guy Schalnat 已提交
964 965
{
   png_structp png_ptr = NULL;
A
Andreas Dilger 已提交
966
   png_infop info_ptr = NULL, end_info_ptr = NULL;
967
#ifdef PNG_USER_MEM_SUPPORTED
968 969
   png_free_ptr free_fn = NULL;
   png_voidp mem_ptr = NULL;
970
#endif
G
Guy Schalnat 已提交
971

972
   png_debug(1, "in png_destroy_read_struct");
A
Andreas Dilger 已提交
973
   if (png_ptr_ptr != NULL)
G
Guy Schalnat 已提交
974
      png_ptr = *png_ptr_ptr;
975 976 977 978 979 980 981
   if (png_ptr == NULL)
      return;

#ifdef PNG_USER_MEM_SUPPORTED
   free_fn = png_ptr->free_fn;
   mem_ptr = png_ptr->mem_ptr;
#endif
G
Guy Schalnat 已提交
982

A
Andreas Dilger 已提交
983
   if (info_ptr_ptr != NULL)
G
Guy Schalnat 已提交
984 985
      info_ptr = *info_ptr_ptr;

A
Andreas Dilger 已提交
986
   if (end_info_ptr_ptr != NULL)
A
Andreas Dilger 已提交
987
      end_info_ptr = *end_info_ptr_ptr;
G
Guy Schalnat 已提交
988

A
Andreas Dilger 已提交
989
   png_read_destroy(png_ptr, info_ptr, end_info_ptr);
G
Guy Schalnat 已提交
990

A
Andreas Dilger 已提交
991
   if (info_ptr != NULL)
G
Guy Schalnat 已提交
992
   {
993
#if defined(PNG_TEXT_SUPPORTED)
994
      png_free_data(png_ptr, info_ptr, PNG_FREE_TEXT, -1);
995
#endif
996 997

#ifdef PNG_USER_MEM_SUPPORTED
998 999
      png_destroy_struct_2((png_voidp)info_ptr, (png_free_ptr)free_fn,
          (png_voidp)mem_ptr);
1000
#else
A
Andreas Dilger 已提交
1001
      png_destroy_struct((png_voidp)info_ptr);
1002
#endif
1003
      *info_ptr_ptr = NULL;
G
Guy Schalnat 已提交
1004 1005
   }

A
Andreas Dilger 已提交
1006
   if (end_info_ptr != NULL)
G
Guy Schalnat 已提交
1007
   {
1008
#if defined(PNG_READ_TEXT_SUPPORTED)
1009
      png_free_data(png_ptr, end_info_ptr, PNG_FREE_TEXT, -1);
1010
#endif
1011
#ifdef PNG_USER_MEM_SUPPORTED
1012 1013
      png_destroy_struct_2((png_voidp)end_info_ptr, (png_free_ptr)free_fn,
         (png_voidp)mem_ptr);
1014
#else
A
Andreas Dilger 已提交
1015
      png_destroy_struct((png_voidp)end_info_ptr);
1016
#endif
1017
      *end_info_ptr_ptr = NULL;
G
Guy Schalnat 已提交
1018 1019
   }

A
Andreas Dilger 已提交
1020
   if (png_ptr != NULL)
G
Guy Schalnat 已提交
1021
   {
1022
#ifdef PNG_USER_MEM_SUPPORTED
1023 1024
      png_destroy_struct_2((png_voidp)png_ptr, (png_free_ptr)free_fn,
          (png_voidp)mem_ptr);
1025
#else
A
Andreas Dilger 已提交
1026
      png_destroy_struct((png_voidp)png_ptr);
1027
#endif
1028
      *png_ptr_ptr = NULL;
G
Guy Schalnat 已提交
1029 1030 1031
   }
}

1032
/* Free all memory used by the read (old method) */
1033
void /* PRIVATE */
A
Andreas Dilger 已提交
1034
png_read_destroy(png_structp png_ptr, png_infop info_ptr, png_infop end_info_ptr)
G
Guy Schalnat 已提交
1035
{
1036
#ifdef PNG_SETJMP_SUPPORTED
G
Guy Schalnat 已提交
1037
   jmp_buf tmp_jmp;
1038
#endif
G
Guy Schalnat 已提交
1039 1040 1041
   png_error_ptr error_fn;
   png_error_ptr warning_fn;
   png_voidp error_ptr;
1042 1043 1044
#ifdef PNG_USER_MEM_SUPPORTED
   png_free_ptr free_fn;
#endif
G
Guy Schalnat 已提交
1045

1046
   png_debug(1, "in png_read_destroy");
A
Andreas Dilger 已提交
1047
   if (info_ptr != NULL)
A
Andreas Dilger 已提交
1048
      png_info_destroy(png_ptr, info_ptr);
G
Guy Schalnat 已提交
1049

A
Andreas Dilger 已提交
1050
   if (end_info_ptr != NULL)
A
Andreas Dilger 已提交
1051
      png_info_destroy(png_ptr, end_info_ptr);
G
Guy Schalnat 已提交
1052

A
Andreas Dilger 已提交
1053
   png_free(png_ptr, png_ptr->zbuf);
1054
   png_free(png_ptr, png_ptr->big_row_buf);
A
Andreas Dilger 已提交
1055
   png_free(png_ptr, png_ptr->prev_row);
1056
   png_free(png_ptr, png_ptr->chunkdata);
G
Guy Schalnat 已提交
1057
#if defined(PNG_READ_DITHER_SUPPORTED)
A
Andreas Dilger 已提交
1058 1059
   png_free(png_ptr, png_ptr->palette_lookup);
   png_free(png_ptr, png_ptr->dither_index);
G
Guy Schalnat 已提交
1060 1061
#endif
#if defined(PNG_READ_GAMMA_SUPPORTED)
A
Andreas Dilger 已提交
1062
   png_free(png_ptr, png_ptr->gamma_table);
G
Guy Schalnat 已提交
1063 1064
#endif
#if defined(PNG_READ_BACKGROUND_SUPPORTED)
A
Andreas Dilger 已提交
1065 1066
   png_free(png_ptr, png_ptr->gamma_from_1);
   png_free(png_ptr, png_ptr->gamma_to_1);
G
Guy Schalnat 已提交
1067
#endif
1068
#ifdef PNG_FREE_ME_SUPPORTED
1069
   if (png_ptr->free_me & PNG_FREE_PLTE)
1070
      png_zfree(png_ptr, png_ptr->palette);
1071
   png_ptr->free_me &= ~PNG_FREE_PLTE;
1072 1073 1074 1075 1076
#else
   if (png_ptr->flags & PNG_FLAG_FREE_PLTE)
      png_zfree(png_ptr, png_ptr->palette);
   png_ptr->flags &= ~PNG_FLAG_FREE_PLTE;
#endif
1077
#if defined(PNG_tRNS_SUPPORTED) || \
1078
    defined(PNG_READ_EXPAND_SUPPORTED) || defined(PNG_READ_BACKGROUND_SUPPORTED)
1079
#ifdef PNG_FREE_ME_SUPPORTED
1080
   if (png_ptr->free_me & PNG_FREE_TRNS)
A
Andreas Dilger 已提交
1081
      png_free(png_ptr, png_ptr->trans);
1082
   png_ptr->free_me &= ~PNG_FREE_TRNS;
1083 1084 1085 1086 1087
#else
   if (png_ptr->flags & PNG_FLAG_FREE_TRNS)
      png_free(png_ptr, png_ptr->trans);
   png_ptr->flags &= ~PNG_FLAG_FREE_TRNS;
#endif
1088
#endif
G
Guy Schalnat 已提交
1089
#if defined(PNG_READ_hIST_SUPPORTED)
1090
#ifdef PNG_FREE_ME_SUPPORTED
1091
   if (png_ptr->free_me & PNG_FREE_HIST)
A
Andreas Dilger 已提交
1092
      png_free(png_ptr, png_ptr->hist);
1093
   png_ptr->free_me &= ~PNG_FREE_HIST;
1094 1095 1096 1097 1098
#else
   if (png_ptr->flags & PNG_FLAG_FREE_HIST)
      png_free(png_ptr, png_ptr->hist);
   png_ptr->flags &= ~PNG_FLAG_FREE_HIST;
#endif
G
Guy Schalnat 已提交
1099 1100
#endif
#if defined(PNG_READ_GAMMA_SUPPORTED)
A
Andreas Dilger 已提交
1101
   if (png_ptr->gamma_16_table != NULL)
G
Guy Schalnat 已提交
1102
   {
1103 1104
      int i;
      int istop = (1 << (8 - png_ptr->gamma_shift));
1105
      for (i = 0; i < istop; i++)
G
Guy Schalnat 已提交
1106
      {
A
Andreas Dilger 已提交
1107
         png_free(png_ptr, png_ptr->gamma_16_table[i]);
G
Guy Schalnat 已提交
1108
      }
1109
   png_free(png_ptr, png_ptr->gamma_16_table);
G
Guy Schalnat 已提交
1110
   }
G
Guy Schalnat 已提交
1111
#if defined(PNG_READ_BACKGROUND_SUPPORTED)
A
Andreas Dilger 已提交
1112
   if (png_ptr->gamma_16_from_1 != NULL)
G
Guy Schalnat 已提交
1113
   {
1114 1115
      int i;
      int istop = (1 << (8 - png_ptr->gamma_shift));
1116
      for (i = 0; i < istop; i++)
G
Guy Schalnat 已提交
1117
      {
A
Andreas Dilger 已提交
1118
         png_free(png_ptr, png_ptr->gamma_16_from_1[i]);
G
Guy Schalnat 已提交
1119
      }
A
Andreas Dilger 已提交
1120
   png_free(png_ptr, png_ptr->gamma_16_from_1);
1121
   }
A
Andreas Dilger 已提交
1122
   if (png_ptr->gamma_16_to_1 != NULL)
G
Guy Schalnat 已提交
1123
   {
1124 1125
      int i;
      int istop = (1 << (8 - png_ptr->gamma_shift));
1126
      for (i = 0; i < istop; i++)
G
Guy Schalnat 已提交
1127
      {
A
Andreas Dilger 已提交
1128
         png_free(png_ptr, png_ptr->gamma_16_to_1[i]);
G
Guy Schalnat 已提交
1129
      }
A
Andreas Dilger 已提交
1130
   png_free(png_ptr, png_ptr->gamma_16_to_1);
1131
   }
G
Guy Schalnat 已提交
1132
#endif
1133 1134 1135
#endif
#if defined(PNG_TIME_RFC1123_SUPPORTED)
   png_free(png_ptr, png_ptr->time_buffer);
1136
#endif
G
Guy Schalnat 已提交
1137

A
Andreas Dilger 已提交
1138
   inflateEnd(&png_ptr->zstream);
G
Guy Schalnat 已提交
1139
#ifdef PNG_PROGRESSIVE_READ_SUPPORTED
A
Andreas Dilger 已提交
1140
   png_free(png_ptr, png_ptr->save_buffer);
G
Guy Schalnat 已提交
1141
#endif
G
Guy Schalnat 已提交
1142

1143 1144 1145 1146 1147 1148
#ifdef PNG_PROGRESSIVE_READ_SUPPORTED
#ifdef PNG_TEXT_SUPPORTED
   png_free(png_ptr, png_ptr->current_text);
#endif /* PNG_TEXT_SUPPORTED */
#endif /* PNG_PROGRESSIVE_READ_SUPPORTED */

G
Guy Schalnat 已提交
1149 1150 1151
   /* Save the important info out of the png_struct, in case it is
    * being used again.
    */
1152
#ifdef PNG_SETJMP_SUPPORTED
1153
   png_memcpy(tmp_jmp, png_ptr->jmpbuf, png_sizeof(jmp_buf));
1154
#endif
G
Guy Schalnat 已提交
1155 1156 1157 1158

   error_fn = png_ptr->error_fn;
   warning_fn = png_ptr->warning_fn;
   error_ptr = png_ptr->error_ptr;
1159 1160 1161
#ifdef PNG_USER_MEM_SUPPORTED
   free_fn = png_ptr->free_fn;
#endif
G
Guy Schalnat 已提交
1162

1163
   png_memset(png_ptr, 0, png_sizeof(png_struct));
G
Guy Schalnat 已提交
1164 1165 1166 1167

   png_ptr->error_fn = error_fn;
   png_ptr->warning_fn = warning_fn;
   png_ptr->error_ptr = error_ptr;
1168 1169 1170
#ifdef PNG_USER_MEM_SUPPORTED
   png_ptr->free_fn = free_fn;
#endif
G
Guy Schalnat 已提交
1171

1172
#ifdef PNG_SETJMP_SUPPORTED
1173
   png_memcpy(png_ptr->jmpbuf, tmp_jmp, png_sizeof(jmp_buf));
1174 1175
#endif

G
Guy Schalnat 已提交
1176
}
1177

1178
void PNGAPI
1179 1180
png_set_read_status_fn(png_structp png_ptr, png_read_status_ptr read_row_fn)
{
1181 1182
   if (png_ptr == NULL)
      return;
1183 1184
   png_ptr->read_row_fn = read_row_fn;
}
1185

1186 1187

#ifndef PNG_NO_SEQUENTIAL_READ_SUPPORTED
1188
#if defined(PNG_INFO_IMAGE_SUPPORTED)
1189 1190
void PNGAPI
png_read_png(png_structp png_ptr, png_infop info_ptr,
1191 1192 1193 1194 1195
                           int transforms,
                           voidp params)
{
   int row;

1196 1197
   if (png_ptr == NULL)
      return;
1198

1199
   /* png_read_info() gives us all of the information from the
1200 1201 1202
    * PNG file before the first IDAT (image data chunk).
    */
   png_read_info(png_ptr, info_ptr);
1203 1204
   if (info_ptr->height > PNG_UINT_32_MAX/png_sizeof(png_bytep))
      png_error(png_ptr, "Image is too high to process with png_read_png()");
1205 1206 1207 1208

   /* -------------- image transformations start here ------------------- */

#if defined(PNG_READ_16_TO_8_SUPPORTED)
1209
   /* Tell libpng to strip 16 bit/color files down to 8 bits per color.
1210
    */
1211
   if (transforms & PNG_TRANSFORM_STRIP_16)
1212
      png_set_strip_16(png_ptr);
1213 1214 1215
#endif

#if defined(PNG_READ_STRIP_ALPHA_SUPPORTED)
1216 1217
   /* Strip alpha bytes from the input data without combining with
    * the background (not recommended).
1218 1219
    */
   if (transforms & PNG_TRANSFORM_STRIP_ALPHA)
1220
      png_set_strip_alpha(png_ptr);
1221 1222
#endif

1223
#if defined(PNG_READ_PACK_SUPPORTED) && !defined(PNG_READ_EXPAND_SUPPORTED)
1224
   /* Extract multiple pixels with bit depths of 1, 2, or 4 from a single
1225 1226 1227
    * byte into separate bytes (useful for paletted and grayscale images).
    */
   if (transforms & PNG_TRANSFORM_PACKING)
1228
      png_set_packing(png_ptr);
1229 1230 1231 1232
#endif

#if defined(PNG_READ_PACKSWAP_SUPPORTED)
   /* Change the order of packed pixels to least significant bit first
1233 1234
    * (not useful if you are using png_set_packing).
    */
1235
   if (transforms & PNG_TRANSFORM_PACKSWAP)
1236
      png_set_packswap(png_ptr);
1237 1238 1239 1240 1241 1242 1243 1244 1245
#endif

#if defined(PNG_READ_EXPAND_SUPPORTED)
   /* Expand paletted colors into true RGB triplets
    * Expand grayscale images to full 8 bits from 1, 2, or 4 bits/pixel
    * Expand paletted or RGB images with transparency to full alpha
    * channels so the data will be available as RGBA quartets.
    */
   if (transforms & PNG_TRANSFORM_EXPAND)
1246 1247 1248
      if ((png_ptr->bit_depth < 8) ||
          (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE) ||
          (png_get_valid(png_ptr, info_ptr, PNG_INFO_tRNS)))
1249
         png_set_expand(png_ptr);
1250 1251
#endif

1252 1253
   /* We don't handle background color or gamma transformation or dithering.
    */
1254 1255

#if defined(PNG_READ_INVERT_SUPPORTED)
1256
   /* Invert monochrome files to have 0 as white and 1 as black
1257
    */
1258
   if (transforms & PNG_TRANSFORM_INVERT_MONO)
1259
      png_set_invert_mono(png_ptr);
1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277
#endif

#if defined(PNG_READ_SHIFT_SUPPORTED)
   /* If you want to shift the pixel values from the range [0,255] or
    * [0,65535] to the original [0,7] or [0,31], or whatever range the
    * colors were originally in:
    */
   if ((transforms & PNG_TRANSFORM_SHIFT)
       && png_get_valid(png_ptr, info_ptr, PNG_INFO_sBIT))
   {
      png_color_8p sig_bit;

      png_get_sBIT(png_ptr, info_ptr, &sig_bit);
      png_set_shift(png_ptr, sig_bit);
   }
#endif

#if defined(PNG_READ_BGR_SUPPORTED)
1278
   /* Flip the RGB pixels to BGR (or RGBA to BGRA)
1279
    */
1280
   if (transforms & PNG_TRANSFORM_BGR)
1281
      png_set_bgr(png_ptr);
1282 1283 1284
#endif

#if defined(PNG_READ_SWAP_ALPHA_SUPPORTED)
1285
   /* Swap the RGBA or GA data to ARGB or AG (or BGRA to ABGR)
1286
    */
1287 1288 1289 1290 1291
   if (transforms & PNG_TRANSFORM_SWAP_ALPHA)
       png_set_swap_alpha(png_ptr);
#endif

#if defined(PNG_READ_SWAP_SUPPORTED)
1292
   /* Swap bytes of 16 bit files to least significant byte first
1293
    */
1294
   if (transforms & PNG_TRANSFORM_SWAP_ENDIAN)
1295
      png_set_swap(png_ptr);
1296 1297
#endif

1298 1299 1300 1301 1302 1303 1304
#if defined(PNG_READ_INVERT_ALPHA_SUPPORTED)
   /* Invert the alpha channel from opacity to transparency
    */
   if (transforms & PNG_TRANSFORM_INVERT_ALPHA)
       png_set_invert_alpha(png_ptr);
#endif

1305 1306 1307 1308
#if defined(PNG_READ_GRAY_TO_RGB_SUPPORTED)
   /* Expand grayscale image to RGB
    */
   if (transforms & PNG_TRANSFORM_GRAY_TO_RGB)
1309
       png_set_gray_to_rgb(png_ptr);
1310 1311
#endif

1312 1313 1314 1315
   /* We don't handle adding filler bytes */

   /* Optional call to gamma correct and add the background to the palette
    * and update info structure.  REQUIRED if you are expecting libpng to
1316
    * update the palette for you (i.e., you selected such a transform above).
1317 1318 1319 1320 1321
    */
   png_read_update_info(png_ptr, info_ptr);

   /* -------------- image transformations end here ------------------- */

1322
#ifdef PNG_FREE_ME_SUPPORTED
1323
   png_free_data(png_ptr, info_ptr, PNG_FREE_ROWS, 0);
1324
#endif
1325
   if (info_ptr->row_pointers == NULL)
1326
   {
1327
#ifdef PNG_CALLOC_SUPPORTED
1328
      info_ptr->row_pointers = (png_bytepp)png_calloc(png_ptr,
1329
         info_ptr->height * png_sizeof(png_bytep));
1330 1331 1332
#else
      info_ptr->row_pointers = (png_bytepp)png_malloc(png_ptr,
         info_ptr->height * png_sizeof(png_bytep));
1333 1334
      png_memset(info_ptr->row_pointers, 0, info_ptr->height
         * png_sizeof(png_bytep));
1335
#endif
1336
#ifdef PNG_FREE_ME_SUPPORTED
1337
      info_ptr->free_me |= PNG_FREE_ROWS;
1338 1339
#endif
      for (row = 0; row < (int)info_ptr->height; row++)
1340
         info_ptr->row_pointers[row] = (png_bytep)png_malloc(png_ptr,
1341
            png_get_rowbytes(png_ptr, info_ptr));
1342
   }
1343 1344 1345 1346

   png_read_image(png_ptr, info_ptr->row_pointers);
   info_ptr->valid |= PNG_INFO_IDAT;

1347
   /* Read rest of file, and get additional chunks in info_ptr - REQUIRED */
1348
   png_read_end(png_ptr, info_ptr);
1349

1350
   transforms = transforms; /* Quiet compiler warnings */
1351
   params = params;
1352

1353
}
1354
#endif /* PNG_INFO_IMAGE_SUPPORTED */
1355
#endif /* PNG_NO_SEQUENTIAL_READ_SUPPORTED */
1356
#endif /* PNG_READ_SUPPORTED */