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 [August 21, 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

57
#ifdef PNG_USER_MEM_SUPPORTED
58
   png_ptr = (png_structp)png_create_struct_2(PNG_STRUCT_PNG,
59
      malloc_fn, mem_ptr);
60
#else
61
   png_ptr = (png_structp)png_create_struct(PNG_STRUCT_PNG);
62
#endif
63
   if (png_ptr == NULL)
64
      return (NULL);
65

66
   /* Added at libpng-1.2.6 */
67 68 69 70 71
#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

72 73 74 75 76 77 78 79 80
#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
81
      PNG_ABORT();
82 83
#endif

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

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

90
   if (user_png_ver)
G
Guy Schalnat 已提交
91
   {
92 93 94 95 96 97 98 99 100
      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;
101

102

103 104 105 106 107 108 109 110 111 112 113
    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'))
      {
114
#if !defined(PNG_NO_STDIO) && !defined(_WIN32_WCE)
115 116 117 118 119 120 121 122 123
         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,
124
             "Application  is  running with png.c from libpng-%.20s",
125 126
             png_libpng_ver);
         png_warning(png_ptr, msg);
127 128
#endif
#ifdef PNG_ERROR_NUMBERS_SUPPORTED
129
         png_ptr->flags = 0;
130
#endif
131 132
         png_warning(png_ptr,
            "Incompatible libpng version in application and library");
133

134 135
         png_cleanup_needed = 1;
      }
136 137
   }

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

151 152
   if (!png_cleanup_needed)
   {
153 154 155 156 157 158 159 160 161 162 163
      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;
      }
164 165 166 167
   }

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

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

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

185

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

189

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

213 214 215
#ifdef PNG_IO_STATE_SUPPORTED
      png_ptr->io_state = PNG_IO_READING | PNG_IO_SIGNATURE;
#endif
216

A
Andreas Dilger 已提交
217
      png_read_data(png_ptr, &(info_ptr->signature[num_checked]), num_to_check);
A
Andreas Dilger 已提交
218 219 220 221 222 223 224 225 226 227
      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");
      }
228 229
      if (num_checked < 3)
         png_ptr->mode |= PNG_HAVE_PNG_SIGNATURE;
G
Guy Schalnat 已提交
230
   }
G
Guy Schalnat 已提交
231

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

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

301
      if (!png_memcmp(chunk_name, png_IHDR, 4))
A
Andreas Dilger 已提交
302
         png_handle_IHDR(png_ptr, info_ptr, length);
303
      else if (!png_memcmp(chunk_name, png_IEND, 4))
A
Andreas Dilger 已提交
304
         png_handle_IEND(png_ptr, info_ptr, length);
305
#ifdef PNG_HANDLE_AS_UNKNOWN_SUPPORTED
306
      else if (png_handle_as_unknown(png_ptr, chunk_name))
307
      {
308
         if (!png_memcmp(chunk_name, png_IDAT, 4))
309 310
            png_ptr->mode |= PNG_HAVE_IDAT;
         png_handle_unknown(png_ptr, info_ptr, length);
311
         if (!png_memcmp(chunk_name, png_PLTE, 4))
312
            png_ptr->mode |= PNG_HAVE_PLTE;
313
         else if (!png_memcmp(chunk_name, png_IDAT, 4))
314 315 316 317 318 319 320 321 322 323
         {
            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
324
      else if (!png_memcmp(chunk_name, png_PLTE, 4))
325
         png_handle_PLTE(png_ptr, info_ptr, length);
326
      else if (!png_memcmp(chunk_name, png_IDAT, 4))
A
Andreas Dilger 已提交
327 328 329 330 331 332 333 334 335 336 337 338
      {
         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)
339
      else if (!png_memcmp(chunk_name, png_bKGD, 4))
A
Andreas Dilger 已提交
340
         png_handle_bKGD(png_ptr, info_ptr, length);
G
Guy Schalnat 已提交
341 342
#endif
#if defined(PNG_READ_cHRM_SUPPORTED)
343
      else if (!png_memcmp(chunk_name, png_cHRM, 4))
A
Andreas Dilger 已提交
344
         png_handle_cHRM(png_ptr, info_ptr, length);
G
Guy Schalnat 已提交
345
#endif
A
Andreas Dilger 已提交
346
#if defined(PNG_READ_gAMA_SUPPORTED)
347
      else if (!png_memcmp(chunk_name, png_gAMA, 4))
A
Andreas Dilger 已提交
348
         png_handle_gAMA(png_ptr, info_ptr, length);
G
Guy Schalnat 已提交
349 350
#endif
#if defined(PNG_READ_hIST_SUPPORTED)
351
      else if (!png_memcmp(chunk_name, png_hIST, 4))
A
Andreas Dilger 已提交
352
         png_handle_hIST(png_ptr, info_ptr, length);
G
Guy Schalnat 已提交
353
#endif
A
Andreas Dilger 已提交
354
#if defined(PNG_READ_oFFs_SUPPORTED)
355
      else if (!png_memcmp(chunk_name, png_oFFs, 4))
A
Andreas Dilger 已提交
356 357 358
         png_handle_oFFs(png_ptr, info_ptr, length);
#endif
#if defined(PNG_READ_pCAL_SUPPORTED)
359
      else if (!png_memcmp(chunk_name, png_pCAL, 4))
A
Andreas Dilger 已提交
360 361
         png_handle_pCAL(png_ptr, info_ptr, length);
#endif
362
#if defined(PNG_READ_sCAL_SUPPORTED)
363
      else if (!png_memcmp(chunk_name, png_sCAL, 4))
364 365
         png_handle_sCAL(png_ptr, info_ptr, length);
#endif
G
Guy Schalnat 已提交
366
#if defined(PNG_READ_pHYs_SUPPORTED)
367
      else if (!png_memcmp(chunk_name, png_pHYs, 4))
A
Andreas Dilger 已提交
368
         png_handle_pHYs(png_ptr, info_ptr, length);
G
Guy Schalnat 已提交
369
#endif
A
Andreas Dilger 已提交
370
#if defined(PNG_READ_sBIT_SUPPORTED)
371
      else if (!png_memcmp(chunk_name, png_sBIT, 4))
A
Andreas Dilger 已提交
372 373
         png_handle_sBIT(png_ptr, info_ptr, length);
#endif
374
#if defined(PNG_READ_sRGB_SUPPORTED)
375
      else if (!png_memcmp(chunk_name, png_sRGB, 4))
376 377
         png_handle_sRGB(png_ptr, info_ptr, length);
#endif
378
#if defined(PNG_READ_iCCP_SUPPORTED)
379
      else if (!png_memcmp(chunk_name, png_iCCP, 4))
380 381 382
         png_handle_iCCP(png_ptr, info_ptr, length);
#endif
#if defined(PNG_READ_sPLT_SUPPORTED)
383
      else if (!png_memcmp(chunk_name, png_sPLT, 4))
384 385
         png_handle_sPLT(png_ptr, info_ptr, length);
#endif
A
Andreas Dilger 已提交
386
#if defined(PNG_READ_tEXt_SUPPORTED)
387
      else if (!png_memcmp(chunk_name, png_tEXt, 4))
A
Andreas Dilger 已提交
388
         png_handle_tEXt(png_ptr, info_ptr, length);
G
Guy Schalnat 已提交
389 390
#endif
#if defined(PNG_READ_tIME_SUPPORTED)
391
      else if (!png_memcmp(chunk_name, png_tIME, 4))
A
Andreas Dilger 已提交
392
         png_handle_tIME(png_ptr, info_ptr, length);
G
Guy Schalnat 已提交
393
#endif
A
Andreas Dilger 已提交
394
#if defined(PNG_READ_tRNS_SUPPORTED)
395
      else if (!png_memcmp(chunk_name, png_tRNS, 4))
A
Andreas Dilger 已提交
396
         png_handle_tRNS(png_ptr, info_ptr, length);
G
Guy Schalnat 已提交
397 398
#endif
#if defined(PNG_READ_zTXt_SUPPORTED)
399
      else if (!png_memcmp(chunk_name, png_zTXt, 4))
A
Andreas Dilger 已提交
400
         png_handle_zTXt(png_ptr, info_ptr, length);
401 402
#endif
#if defined(PNG_READ_iTXt_SUPPORTED)
403
      else if (!png_memcmp(chunk_name, png_iTXt, 4))
404
         png_handle_iTXt(png_ptr, info_ptr, length);
G
Guy Schalnat 已提交
405
#endif
A
Andreas Dilger 已提交
406 407
      else
         png_handle_unknown(png_ptr, info_ptr, length);
G
Guy Schalnat 已提交
408 409
   }
}
410
#endif /* PNG_NO_SEQUENTIAL_READ_SUPPORTED */
G
Guy Schalnat 已提交
411

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

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

446
#ifndef PNG_NO_SEQUENTIAL_READ_SUPPORTED
447
void PNGAPI
G
Guy Schalnat 已提交
448
png_read_row(png_structp png_ptr, png_bytep row, png_bytep dsp_row)
G
Guy Schalnat 已提交
449
{
450
#ifdef PNG_USE_LOCAL_ARRAYS
451 452 453 454
   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};
455
#endif
G
Guy Schalnat 已提交
456
   int ret;
457 458 459

   png_debug2(1, "in png_read_row (row %lu, pass %d)",
 
460 461
   if (png_ptr == NULL)
      return;
462
 
463
      (unsigned long) png_ptr->row_number, png_ptr->pass);
G
Guy Schalnat 已提交
464
   if (!(png_ptr->flags & PNG_FLAG_ROW_INIT))
G
Guy Schalnat 已提交
465
      png_read_start_row(png_ptr);
466 467
   if (png_ptr->row_number == 0 && png_ptr->pass == 0)
   {
468
   /* Check for transforms that have been set but were defined out */
469 470
#if defined(PNG_WRITE_INVERT_SUPPORTED) && !defined(PNG_READ_INVERT_SUPPORTED)
   if (png_ptr->transformations & PNG_INVERT_MONO)
471
      png_warning(png_ptr, "PNG_READ_INVERT_SUPPORTED is not defined");
472 473 474
#endif
#if defined(PNG_WRITE_FILLER_SUPPORTED) && !defined(PNG_READ_FILLER_SUPPORTED)
   if (png_ptr->transformations & PNG_FILLER)
475
      png_warning(png_ptr, "PNG_READ_FILLER_SUPPORTED is not defined");
476 477 478
#endif
#if defined(PNG_WRITE_PACKSWAP_SUPPORTED) && !defined(PNG_READ_PACKSWAP_SUPPORTED)
   if (png_ptr->transformations & PNG_PACKSWAP)
479
      png_warning(png_ptr, "PNG_READ_PACKSWAP_SUPPORTED is not defined");
480 481 482
#endif
#if defined(PNG_WRITE_PACK_SUPPORTED) && !defined(PNG_READ_PACK_SUPPORTED)
   if (png_ptr->transformations & PNG_PACK)
483
      png_warning(png_ptr, "PNG_READ_PACK_SUPPORTED is not defined");
484 485 486
#endif
#if defined(PNG_WRITE_SHIFT_SUPPORTED) && !defined(PNG_READ_SHIFT_SUPPORTED)
   if (png_ptr->transformations & PNG_SHIFT)
487
      png_warning(png_ptr, "PNG_READ_SHIFT_SUPPORTED is not defined");
488 489 490
#endif
#if defined(PNG_WRITE_BGR_SUPPORTED) && !defined(PNG_READ_BGR_SUPPORTED)
   if (png_ptr->transformations & PNG_BGR)
491
      png_warning(png_ptr, "PNG_READ_BGR_SUPPORTED is not defined");
492 493 494
#endif
#if defined(PNG_WRITE_SWAP_SUPPORTED) && !defined(PNG_READ_SWAP_SUPPORTED)
   if (png_ptr->transformations & PNG_SWAP_BYTES)
495
      png_warning(png_ptr, "PNG_READ_SWAP_SUPPORTED is not defined");
496 497
#endif
   }
G
Guy Schalnat 已提交
498

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

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

A
Andreas Dilger 已提交
579 580
   png_ptr->zstream.next_out = png_ptr->row_buf;
   png_ptr->zstream.avail_out = (uInt)png_ptr->irowbytes;
G
Guy Schalnat 已提交
581 582
   do
   {
A
Andreas Dilger 已提交
583
      if (!(png_ptr->zstream.avail_in))
G
Guy Schalnat 已提交
584 585 586
      {
         while (!png_ptr->idat_size)
         {
A
Andreas Dilger 已提交
587
            png_crc_finish(png_ptr, 0);
G
Guy Schalnat 已提交
588

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

   } while (png_ptr->zstream.avail_out);
G
Guy Schalnat 已提交
616 617 618 619 620 621

   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;
622 623
   png_ptr->row_info.rowbytes = PNG_ROWBYTES(png_ptr->row_info.pixel_depth,
       png_ptr->row_info.width);
G
Guy Schalnat 已提交
624

625
   if (png_ptr->row_buf[0])
G
Guy Schalnat 已提交
626 627 628
   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 已提交
629

630
   png_memcpy(png_ptr->prev_row, png_ptr->row_buf, png_ptr->rowbytes + 1);
631

632
#if defined(PNG_MNG_FEATURES_SUPPORTED)
633
   if ((png_ptr->mng_features_permitted & PNG_FLAG_MNG_FILTER_64) &&
634 635 636 637 638 639
      (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 已提交
640

641

642
   if (png_ptr->transformations || (png_ptr->flags&PNG_FLAG_STRIP_ALPHA))
G
Guy Schalnat 已提交
643 644
      png_do_read_transformations(png_ptr);

G
Guy Schalnat 已提交
645
#if defined(PNG_READ_INTERLACING_SUPPORTED)
646
   /* Blow up interlaced rows to full size */
G
Guy Schalnat 已提交
647 648 649 650
   if (png_ptr->interlaced &&
      (png_ptr->transformations & PNG_INTERLACE))
   {
      if (png_ptr->pass < 6)
651
         /* Old interface (pre-1.0.9):
652 653 654
          * png_do_read_interlace(&(png_ptr->row_info),
          *    png_ptr->row_buf + 1, png_ptr->pass, png_ptr->transformations);
          */
655
         png_do_read_interlace(png_ptr);
G
Guy Schalnat 已提交
656

A
Andreas Dilger 已提交
657
      if (dsp_row != NULL)
G
Guy Schalnat 已提交
658 659
         png_combine_row(png_ptr, dsp_row,
            png_pass_dsp_mask[png_ptr->pass]);
A
Andreas Dilger 已提交
660
      if (row != NULL)
G
Guy Schalnat 已提交
661 662 663 664
         png_combine_row(png_ptr, row,
            png_pass_mask[png_ptr->pass]);
   }
   else
G
Guy Schalnat 已提交
665
#endif
G
Guy Schalnat 已提交
666
   {
A
Andreas Dilger 已提交
667
      if (row != NULL)
G
Guy Schalnat 已提交
668
         png_combine_row(png_ptr, row, 0xff);
A
Andreas Dilger 已提交
669
      if (dsp_row != NULL)
G
Guy Schalnat 已提交
670 671 672
         png_combine_row(png_ptr, dsp_row, 0xff);
   }
   png_read_finish_row(png_ptr);
673 674 675

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

679
#ifndef PNG_NO_SEQUENTIAL_READ_SUPPORTED
A
Andreas Dilger 已提交
680
/* Read one or more rows of image data.  If the image is interlaced,
681 682
 * and png_set_interlace_handling() has been called, the rows need to
 * contain the contents of the rows from the previous pass.  If the
683
 * image has alpha or transparency, and png_handle_alpha()[*] has been
684 685
 * called, the rows contents must be initialized to the contents of the
 * screen.
686
 *
687 688 689 690 691 692 693 694 695 696 697 698 699
 * "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.
700
 *
701
 * [*] png_handle_alpha() does not exist yet, as of this version of libpng
702
 */
G
Guy Schalnat 已提交
703

704
void PNGAPI
G
Guy Schalnat 已提交
705
png_read_rows(png_structp png_ptr, png_bytepp row,
G
Guy Schalnat 已提交
706
   png_bytepp display_row, png_uint_32 num_rows)
G
Guy Schalnat 已提交
707
{
G
Guy Schalnat 已提交
708 709 710
   png_uint_32 i;
   png_bytepp rp;
   png_bytepp dp;
G
Guy Schalnat 已提交
711

712
   png_debug(1, "in png_read_rows");
713
 
714 715
   if (png_ptr == NULL)
      return;
G
Guy Schalnat 已提交
716 717
   rp = row;
   dp = display_row;
718
   if (rp != NULL && dp != NULL)
719 720 721 722
      for (i = 0; i < num_rows; i++)
      {
         png_bytep rptr = *rp++;
         png_bytep dptr = *dp++;
723

724 725
         png_read_row(png_ptr, rptr, dptr);
      }
726
   else if (rp != NULL)
727 728
      for (i = 0; i < num_rows; i++)
      {
729
         png_bytep rptr = *rp;
730
         png_read_row(png_ptr, rptr, NULL);
731 732
         rp++;
      }
733
   else if (dp != NULL)
734 735 736
      for (i = 0; i < num_rows; i++)
      {
         png_bytep dptr = *dp;
737
         png_read_row(png_ptr, NULL, dptr);
738
         dp++;
739
      }
G
Guy Schalnat 已提交
740
}
741
#endif /* PNG_NO_SEQUENTIAL_READ_SUPPORTED */
G
Guy Schalnat 已提交
742

743
#ifndef PNG_NO_SEQUENTIAL_READ_SUPPORTED
A
Andreas Dilger 已提交
744
/* Read the entire image.  If the image has an alpha channel or a tRNS
745
 * chunk, and you have called png_handle_alpha()[*], you will need to
746 747 748 749 750 751 752
 * 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.
753
 *
754
 * [*] png_handle_alpha() does not exist yet, as of this version of libpng
755
 */
756
void PNGAPI
G
Guy Schalnat 已提交
757
png_read_image(png_structp png_ptr, png_bytepp image)
G
Guy Schalnat 已提交
758
{
759
   png_uint_32 i, image_height;
G
Guy Schalnat 已提交
760
   int pass, j;
G
Guy Schalnat 已提交
761
   png_bytepp rp;
G
Guy Schalnat 已提交
762

763
   png_debug(1, "in png_read_image");
764
 
765 766
   if (png_ptr == NULL)
      return;
767 768

#ifdef PNG_READ_INTERLACING_SUPPORTED
G
Guy Schalnat 已提交
769
   pass = png_set_interlace_handling(png_ptr);
770 771 772
#else
   if (png_ptr->interlaced)
      png_error(png_ptr,
773
        "Cannot read interlaced image -- interlace handler disabled");
774 775 776
   pass = 1;
#endif

777

778 779
   image_height=png_ptr->height;
   png_ptr->num_rows = image_height; /* Make sure this is set correctly */
G
Guy Schalnat 已提交
780

G
Guy Schalnat 已提交
781 782 783
   for (j = 0; j < pass; j++)
   {
      rp = image;
784
      for (i = 0; i < image_height; i++)
G
Guy Schalnat 已提交
785
      {
786
         png_read_row(png_ptr, *rp, NULL);
G
Guy Schalnat 已提交
787 788 789 790
         rp++;
      }
   }
}
791
#endif /* PNG_NO_SEQUENTIAL_READ_SUPPORTED */
G
Guy Schalnat 已提交
792

793
#ifndef PNG_NO_SEQUENTIAL_READ_SUPPORTED
A
Andreas Dilger 已提交
794
/* Read the end of the PNG file.  Will not read past the end of the
795 796 797
 * 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.
 */
798
void PNGAPI
A
Andreas Dilger 已提交
799
png_read_end(png_structp png_ptr, png_infop info_ptr)
G
Guy Schalnat 已提交
800
{
801
   png_debug(1, "in png_read_end");
802
 
803 804
   if (png_ptr == NULL)
      return;
A
Andreas Dilger 已提交
805
   png_crc_finish(png_ptr, 0); /* Finish off CRC from last IDAT chunk */
G
Guy Schalnat 已提交
806 807 808

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

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

971
/* Free all memory used by the read */
972
void PNGAPI
G
Guy Schalnat 已提交
973
png_destroy_read_struct(png_structpp png_ptr_ptr, png_infopp info_ptr_ptr,
A
Andreas Dilger 已提交
974
   png_infopp end_info_ptr_ptr)
G
Guy Schalnat 已提交
975 976
{
   png_structp png_ptr = NULL;
A
Andreas Dilger 已提交
977
   png_infop info_ptr = NULL, end_info_ptr = NULL;
978
#ifdef PNG_USER_MEM_SUPPORTED
979 980
   png_free_ptr free_fn = NULL;
   png_voidp mem_ptr = NULL;
981
#endif
G
Guy Schalnat 已提交
982

983
   png_debug(1, "in png_destroy_read_struct");
984
 
A
Andreas Dilger 已提交
985
   if (png_ptr_ptr != NULL)
G
Guy Schalnat 已提交
986
      png_ptr = *png_ptr_ptr;
987 988 989 990 991 992 993
   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 已提交
994

A
Andreas Dilger 已提交
995
   if (info_ptr_ptr != NULL)
G
Guy Schalnat 已提交
996 997
      info_ptr = *info_ptr_ptr;

A
Andreas Dilger 已提交
998
   if (end_info_ptr_ptr != NULL)
A
Andreas Dilger 已提交
999
      end_info_ptr = *end_info_ptr_ptr;
G
Guy Schalnat 已提交
1000

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

A
Andreas Dilger 已提交
1003
   if (info_ptr != NULL)
G
Guy Schalnat 已提交
1004
   {
1005
#if defined(PNG_TEXT_SUPPORTED)
1006
      png_free_data(png_ptr, info_ptr, PNG_FREE_TEXT, -1);
1007
#endif
1008 1009

#ifdef PNG_USER_MEM_SUPPORTED
1010 1011
      png_destroy_struct_2((png_voidp)info_ptr, (png_free_ptr)free_fn,
          (png_voidp)mem_ptr);
1012
#else
A
Andreas Dilger 已提交
1013
      png_destroy_struct((png_voidp)info_ptr);
1014
#endif
1015
      *info_ptr_ptr = NULL;
G
Guy Schalnat 已提交
1016 1017
   }

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

A
Andreas Dilger 已提交
1032
   if (png_ptr != NULL)
G
Guy Schalnat 已提交
1033
   {
1034
#ifdef PNG_USER_MEM_SUPPORTED
1035 1036
      png_destroy_struct_2((png_voidp)png_ptr, (png_free_ptr)free_fn,
          (png_voidp)mem_ptr);
1037
#else
A
Andreas Dilger 已提交
1038
      png_destroy_struct((png_voidp)png_ptr);
1039
#endif
1040
      *png_ptr_ptr = NULL;
G
Guy Schalnat 已提交
1041 1042 1043
   }
}

1044
/* Free all memory used by the read (old method) */
1045
void /* PRIVATE */
A
Andreas Dilger 已提交
1046
png_read_destroy(png_structp png_ptr, png_infop info_ptr, png_infop end_info_ptr)
G
Guy Schalnat 已提交
1047
{
1048
#ifdef PNG_SETJMP_SUPPORTED
G
Guy Schalnat 已提交
1049
   jmp_buf tmp_jmp;
1050
#endif
G
Guy Schalnat 已提交
1051 1052 1053
   png_error_ptr error_fn;
   png_error_ptr warning_fn;
   png_voidp error_ptr;
1054 1055 1056
#ifdef PNG_USER_MEM_SUPPORTED
   png_free_ptr free_fn;
#endif
G
Guy Schalnat 已提交
1057

1058
   png_debug(1, "in png_read_destroy");
1059
 
A
Andreas Dilger 已提交
1060
   if (info_ptr != NULL)
A
Andreas Dilger 已提交
1061
      png_info_destroy(png_ptr, info_ptr);
G
Guy Schalnat 已提交
1062

A
Andreas Dilger 已提交
1063
   if (end_info_ptr != NULL)
A
Andreas Dilger 已提交
1064
      png_info_destroy(png_ptr, end_info_ptr);
G
Guy Schalnat 已提交
1065

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

A
Andreas Dilger 已提交
1151
   inflateEnd(&png_ptr->zstream);
G
Guy Schalnat 已提交
1152
#ifdef PNG_PROGRESSIVE_READ_SUPPORTED
A
Andreas Dilger 已提交
1153
   png_free(png_ptr, png_ptr->save_buffer);
G
Guy Schalnat 已提交
1154
#endif
G
Guy Schalnat 已提交
1155

1156 1157 1158 1159 1160 1161
#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 已提交
1162 1163 1164
   /* Save the important info out of the png_struct, in case it is
    * being used again.
    */
1165
#ifdef PNG_SETJMP_SUPPORTED
1166
   png_memcpy(tmp_jmp, png_ptr->jmpbuf, png_sizeof(jmp_buf));
1167
#endif
G
Guy Schalnat 已提交
1168 1169 1170 1171

   error_fn = png_ptr->error_fn;
   warning_fn = png_ptr->warning_fn;
   error_ptr = png_ptr->error_ptr;
1172 1173 1174
#ifdef PNG_USER_MEM_SUPPORTED
   free_fn = png_ptr->free_fn;
#endif
G
Guy Schalnat 已提交
1175

1176
   png_memset(png_ptr, 0, png_sizeof(png_struct));
G
Guy Schalnat 已提交
1177 1178 1179 1180

   png_ptr->error_fn = error_fn;
   png_ptr->warning_fn = warning_fn;
   png_ptr->error_ptr = error_ptr;
1181 1182 1183
#ifdef PNG_USER_MEM_SUPPORTED
   png_ptr->free_fn = free_fn;
#endif
G
Guy Schalnat 已提交
1184

1185
#ifdef PNG_SETJMP_SUPPORTED
1186
   png_memcpy(png_ptr->jmpbuf, tmp_jmp, png_sizeof(jmp_buf));
1187 1188
#endif

G
Guy Schalnat 已提交
1189
}
1190

1191
void PNGAPI
1192 1193
png_set_read_status_fn(png_structp png_ptr, png_read_status_ptr read_row_fn)
{
1194 1195
   if (png_ptr == NULL)
      return;
1196 1197
   png_ptr->read_row_fn = read_row_fn;
}
1198

1199 1200

#ifndef PNG_NO_SEQUENTIAL_READ_SUPPORTED
1201
#if defined(PNG_INFO_IMAGE_SUPPORTED)
1202 1203
void PNGAPI
png_read_png(png_structp png_ptr, png_infop info_ptr,
1204 1205 1206 1207 1208
                           int transforms,
                           voidp params)
{
   int row;

1209 1210
   if (png_ptr == NULL)
      return;
1211

1212
   /* png_read_info() gives us all of the information from the
1213 1214 1215
    * PNG file before the first IDAT (image data chunk).
    */
   png_read_info(png_ptr, info_ptr);
1216 1217
   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()");
1218 1219 1220 1221

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

#if defined(PNG_READ_16_TO_8_SUPPORTED)
1222
   /* Tell libpng to strip 16 bit/color files down to 8 bits per color.
1223
    */
1224
   if (transforms & PNG_TRANSFORM_STRIP_16)
1225
      png_set_strip_16(png_ptr);
1226 1227 1228
#endif

#if defined(PNG_READ_STRIP_ALPHA_SUPPORTED)
1229 1230
   /* Strip alpha bytes from the input data without combining with
    * the background (not recommended).
1231 1232
    */
   if (transforms & PNG_TRANSFORM_STRIP_ALPHA)
1233
      png_set_strip_alpha(png_ptr);
1234 1235
#endif

1236
#if defined(PNG_READ_PACK_SUPPORTED) && !defined(PNG_READ_EXPAND_SUPPORTED)
1237
   /* Extract multiple pixels with bit depths of 1, 2, or 4 from a single
1238 1239 1240
    * byte into separate bytes (useful for paletted and grayscale images).
    */
   if (transforms & PNG_TRANSFORM_PACKING)
1241
      png_set_packing(png_ptr);
1242 1243 1244 1245
#endif

#if defined(PNG_READ_PACKSWAP_SUPPORTED)
   /* Change the order of packed pixels to least significant bit first
1246 1247
    * (not useful if you are using png_set_packing).
    */
1248
   if (transforms & PNG_TRANSFORM_PACKSWAP)
1249
      png_set_packswap(png_ptr);
1250 1251 1252 1253 1254 1255 1256 1257 1258
#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)
1259 1260 1261
      if ((png_ptr->bit_depth < 8) ||
          (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE) ||
          (png_get_valid(png_ptr, info_ptr, PNG_INFO_tRNS)))
1262
         png_set_expand(png_ptr);
1263 1264
#endif

1265 1266
   /* We don't handle background color or gamma transformation or dithering.
    */
1267 1268

#if defined(PNG_READ_INVERT_SUPPORTED)
1269
   /* Invert monochrome files to have 0 as white and 1 as black
1270
    */
1271
   if (transforms & PNG_TRANSFORM_INVERT_MONO)
1272
      png_set_invert_mono(png_ptr);
1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290
#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)
1291
   /* Flip the RGB pixels to BGR (or RGBA to BGRA)
1292
    */
1293
   if (transforms & PNG_TRANSFORM_BGR)
1294
      png_set_bgr(png_ptr);
1295 1296 1297
#endif

#if defined(PNG_READ_SWAP_ALPHA_SUPPORTED)
1298
   /* Swap the RGBA or GA data to ARGB or AG (or BGRA to ABGR)
1299
    */
1300 1301 1302 1303 1304
   if (transforms & PNG_TRANSFORM_SWAP_ALPHA)
       png_set_swap_alpha(png_ptr);
#endif

#if defined(PNG_READ_SWAP_SUPPORTED)
1305
   /* Swap bytes of 16 bit files to least significant byte first
1306
    */
1307
   if (transforms & PNG_TRANSFORM_SWAP_ENDIAN)
1308
      png_set_swap(png_ptr);
1309 1310
#endif

1311 1312 1313 1314 1315 1316 1317
#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

1318 1319 1320 1321
#if defined(PNG_READ_GRAY_TO_RGB_SUPPORTED)
   /* Expand grayscale image to RGB
    */
   if (transforms & PNG_TRANSFORM_GRAY_TO_RGB)
1322
       png_set_gray_to_rgb(png_ptr);
1323 1324
#endif

1325 1326 1327 1328
   /* 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
1329
    * update the palette for you (i.e., you selected such a transform above).
1330 1331 1332 1333 1334
    */
   png_read_update_info(png_ptr, info_ptr);

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

1335
#ifdef PNG_FREE_ME_SUPPORTED
1336
   png_free_data(png_ptr, info_ptr, PNG_FREE_ROWS, 0);
1337
#endif
1338
   if (info_ptr->row_pointers == NULL)
1339
   {
1340
#ifdef PNG_CALLOC_SUPPORTED
1341
      info_ptr->row_pointers = (png_bytepp)png_calloc(png_ptr,
1342
         info_ptr->height * png_sizeof(png_bytep));
1343 1344 1345
#else
      info_ptr->row_pointers = (png_bytepp)png_malloc(png_ptr,
         info_ptr->height * png_sizeof(png_bytep));
1346 1347
      png_memset(info_ptr->row_pointers, 0, info_ptr->height
         * png_sizeof(png_bytep));
1348
#endif
1349
#ifdef PNG_FREE_ME_SUPPORTED
1350
      info_ptr->free_me |= PNG_FREE_ROWS;
1351 1352
#endif
      for (row = 0; row < (int)info_ptr->height; row++)
1353
         info_ptr->row_pointers[row] = (png_bytep)png_malloc(png_ptr,
1354
            png_get_rowbytes(png_ptr, info_ptr));
1355
   }
1356 1357 1358 1359

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

1360
   /* Read rest of file, and get additional chunks in info_ptr - REQUIRED */
1361
   png_read_end(png_ptr, info_ptr);
1362

1363
   transforms = transforms; /* Quiet compiler warnings */
1364
   params = params;
1365

1366
}
1367
#endif /* PNG_INFO_IMAGE_SUPPORTED */
1368
#endif /* PNG_NO_SEQUENTIAL_READ_SUPPORTED */
1369
#endif /* PNG_READ_SUPPORTED */