pngread.c 40.7 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 [November 26, 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
#ifdef 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
#ifdef PNG_SET_USER_LIMITS_SUPPORTED
68 69
   png_ptr->user_width_max = PNG_USER_WIDTH_MAX;
   png_ptr->user_height_max = PNG_USER_HEIGHT_MAX;
70
   /* Added at libpng-1.4.0 */
71
   png_ptr->user_chunk_cache_max = PNG_USER_CHUNK_CACHE_MAX;
72 73
#endif

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
81
   if (setjmp(png_jmpbuf(png_ptr))) /* Sets longjmp to match setjmp */
82
#endif
83
      PNG_ABORT();
84 85 86
#ifdef USE_FAR_KEYWORD
   png_memcpy(png_jmpbuf(png_ptr), jmpbuf, png_sizeof(jmp_buf));
#endif
87
#endif /* PNG_SETJMP_SUPPORTED */
88

89 90
#ifdef PNG_USER_MEM_SUPPORTED
   png_set_mem_fn(png_ptr, mem_ptr, malloc_fn, free_fn);
91
#endif
92

A
Andreas Dilger 已提交
93 94
   png_set_error_fn(png_ptr, error_ptr, error_fn, warn_fn);

95
   if (user_png_ver)
G
Guy Schalnat 已提交
96
   {
97 98 99 100 101 102 103 104 105
      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;
106

107

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

139 140
         png_cleanup_needed = 1;
      }
141 142
   }

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

156 157
   if (!png_cleanup_needed)
   {
158 159 160 161 162 163 164 165 166 167 168
      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;
      }
169 170 171 172
   }

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

A
Andreas Dilger 已提交
185 186
   png_ptr->zstream.next_out = png_ptr->zbuf;
   png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size;
G
Guy Schalnat 已提交
187

188
   png_set_read_fn(png_ptr, NULL, NULL);
G
Guy Schalnat 已提交
189

190

G
Guy Schalnat 已提交
191 192 193
   return (png_ptr);
}

194

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

218 219 220
#ifdef PNG_IO_STATE_SUPPORTED
      png_ptr->io_state = PNG_IO_READING | PNG_IO_SIGNATURE;
#endif
221

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

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

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

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

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

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

449
#ifdef PNG_SEQUENTIAL_READ_SUPPORTED
450
void PNGAPI
G
Guy Schalnat 已提交
451
png_read_row(png_structp png_ptr, png_bytep row, png_bytep dsp_row)
G
Guy Schalnat 已提交
452
{
453
   PNG_IDAT;
454 455 456
   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};
G
Guy Schalnat 已提交
457
   int ret;
458
 
459 460
   if (png_ptr == NULL)
      return;
461
 
462
   png_debug2(1, "in png_read_row (row %lu, pass %d)",
463
      (unsigned long) png_ptr->row_number, png_ptr->pass);
464

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

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

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

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

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

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

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

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

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

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

642

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

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

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

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

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

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

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

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

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

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

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

778

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

A
Andreas Dilger 已提交
1132
   inflateEnd(&png_ptr->zstream);
G
Guy Schalnat 已提交
1133
#ifdef PNG_PROGRESSIVE_READ_SUPPORTED
A
Andreas Dilger 已提交
1134
   png_free(png_ptr, png_ptr->save_buffer);
G
Guy Schalnat 已提交
1135
#endif
G
Guy Schalnat 已提交
1136

1137 1138 1139 1140 1141 1142
#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 已提交
1143 1144 1145
   /* Save the important info out of the png_struct, in case it is
    * being used again.
    */
1146
#ifdef PNG_SETJMP_SUPPORTED
1147
   png_memcpy(tmp_jmp, png_ptr->jmpbuf, png_sizeof(jmp_buf));
1148
#endif
G
Guy Schalnat 已提交
1149 1150 1151 1152

   error_fn = png_ptr->error_fn;
   warning_fn = png_ptr->warning_fn;
   error_ptr = png_ptr->error_ptr;
1153 1154 1155
#ifdef PNG_USER_MEM_SUPPORTED
   free_fn = png_ptr->free_fn;
#endif
G
Guy Schalnat 已提交
1156

1157
   png_memset(png_ptr, 0, png_sizeof(png_struct));
G
Guy Schalnat 已提交
1158 1159 1160 1161

   png_ptr->error_fn = error_fn;
   png_ptr->warning_fn = warning_fn;
   png_ptr->error_ptr = error_ptr;
1162 1163 1164
#ifdef PNG_USER_MEM_SUPPORTED
   png_ptr->free_fn = free_fn;
#endif
G
Guy Schalnat 已提交
1165

1166
#ifdef PNG_SETJMP_SUPPORTED
1167
   png_memcpy(png_ptr->jmpbuf, tmp_jmp, png_sizeof(jmp_buf));
1168 1169
#endif

G
Guy Schalnat 已提交
1170
}
1171

1172
void PNGAPI
1173 1174
png_set_read_status_fn(png_structp png_ptr, png_read_status_ptr read_row_fn)
{
1175 1176
   if (png_ptr == NULL)
      return;
1177 1178
   png_ptr->read_row_fn = read_row_fn;
}
1179

1180

1181
#ifdef PNG_SEQUENTIAL_READ_SUPPORTED
1182
#ifdef PNG_INFO_IMAGE_SUPPORTED
1183 1184
void PNGAPI
png_read_png(png_structp png_ptr, png_infop info_ptr,
1185 1186 1187 1188 1189
                           int transforms,
                           voidp params)
{
   int row;

1190 1191
   if (png_ptr == NULL)
      return;
1192

1193
   /* png_read_info() gives us all of the information from the
1194 1195 1196
    * PNG file before the first IDAT (image data chunk).
    */
   png_read_info(png_ptr, info_ptr);
1197 1198
   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()");
1199 1200 1201

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

1202
#ifdef PNG_READ_16_TO_8_SUPPORTED
1203
   /* Tell libpng to strip 16 bit/color files down to 8 bits per color.
1204
    */
1205
   if (transforms & PNG_TRANSFORM_STRIP_16)
1206
      png_set_strip_16(png_ptr);
1207 1208
#endif

1209
#ifdef PNG_READ_STRIP_ALPHA_SUPPORTED
1210 1211
   /* Strip alpha bytes from the input data without combining with
    * the background (not recommended).
1212 1213
    */
   if (transforms & PNG_TRANSFORM_STRIP_ALPHA)
1214
      png_set_strip_alpha(png_ptr);
1215 1216
#endif

1217
#if defined(PNG_READ_PACK_SUPPORTED) && !defined(PNG_READ_EXPAND_SUPPORTED)
1218
   /* Extract multiple pixels with bit depths of 1, 2, or 4 from a single
1219 1220 1221
    * byte into separate bytes (useful for paletted and grayscale images).
    */
   if (transforms & PNG_TRANSFORM_PACKING)
1222
      png_set_packing(png_ptr);
1223 1224
#endif

1225
#ifdef PNG_READ_PACKSWAP_SUPPORTED
1226
   /* Change the order of packed pixels to least significant bit first
1227 1228
    * (not useful if you are using png_set_packing).
    */
1229
   if (transforms & PNG_TRANSFORM_PACKSWAP)
1230
      png_set_packswap(png_ptr);
1231 1232
#endif

1233
#ifdef PNG_READ_EXPAND_SUPPORTED
1234 1235 1236 1237 1238 1239
   /* 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)
1240 1241 1242
      if ((png_ptr->bit_depth < 8) ||
          (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE) ||
          (png_get_valid(png_ptr, info_ptr, PNG_INFO_tRNS)))
1243
         png_set_expand(png_ptr);
1244 1245
#endif

1246 1247
   /* We don't handle background color or gamma transformation or dithering.
    */
1248

1249
#ifdef PNG_READ_INVERT_SUPPORTED
1250
   /* Invert monochrome files to have 0 as white and 1 as black
1251
    */
1252
   if (transforms & PNG_TRANSFORM_INVERT_MONO)
1253
      png_set_invert_mono(png_ptr);
1254 1255
#endif

1256
#ifdef PNG_READ_SHIFT_SUPPORTED
1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270
   /* 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

1271
#ifdef PNG_READ_BGR_SUPPORTED
1272
   /* Flip the RGB pixels to BGR (or RGBA to BGRA)
1273
    */
1274
   if (transforms & PNG_TRANSFORM_BGR)
1275
      png_set_bgr(png_ptr);
1276 1277
#endif

1278
#ifdef PNG_READ_SWAP_ALPHA_SUPPORTED
1279
   /* Swap the RGBA or GA data to ARGB or AG (or BGRA to ABGR)
1280
    */
1281 1282 1283 1284
   if (transforms & PNG_TRANSFORM_SWAP_ALPHA)
       png_set_swap_alpha(png_ptr);
#endif

1285
#ifdef PNG_READ_SWAP_SUPPORTED
1286
   /* Swap bytes of 16 bit files to least significant byte first
1287
    */
1288
   if (transforms & PNG_TRANSFORM_SWAP_ENDIAN)
1289
      png_set_swap(png_ptr);
1290 1291
#endif

1292
/* Added at libpng-1.2.41 */
1293
#ifdef PNG_READ_INVERT_ALPHA_SUPPORTED
1294 1295 1296 1297 1298 1299
   /* Invert the alpha channel from opacity to transparency
    */
   if (transforms & PNG_TRANSFORM_INVERT_ALPHA)
       png_set_invert_alpha(png_ptr);
#endif

1300
/* Added at libpng-1.2.41 */
1301
#ifdef PNG_READ_GRAY_TO_RGB_SUPPORTED
1302 1303 1304
   /* Expand grayscale image to RGB
    */
   if (transforms & PNG_TRANSFORM_GRAY_TO_RGB)
1305
       png_set_gray_to_rgb(png_ptr);
1306 1307
#endif

1308 1309 1310 1311
   /* 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
1312
    * update the palette for you (i.e., you selected such a transform above).
1313 1314 1315 1316 1317
    */
   png_read_update_info(png_ptr, info_ptr);

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

1318
   png_free_data(png_ptr, info_ptr, PNG_FREE_ROWS, 0);
1319
   if (info_ptr->row_pointers == NULL)
1320
   {
1321 1322
    png_uint_32 iptr;

1323 1324
      info_ptr->row_pointers = (png_bytepp)png_malloc(png_ptr,
         info_ptr->height * png_sizeof(png_bytep));
1325 1326 1327
      for (iptr=0; iptr<info_ptr->height; iptr++)
         info_ptr->row_pointers[iptr] = NULL;

1328
      info_ptr->free_me |= PNG_FREE_ROWS;
1329

1330
      for (row = 0; row < (int)info_ptr->height; row++)
1331
         info_ptr->row_pointers[row] = (png_bytep)png_malloc(png_ptr,
1332
            png_get_rowbytes(png_ptr, info_ptr));
1333
   }
1334 1335 1336 1337

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

1338
   /* Read rest of file, and get additional chunks in info_ptr - REQUIRED */
1339
   png_read_end(png_ptr, info_ptr);
1340

1341
   transforms = transforms; /* Quiet compiler warnings */
1342
   params = params;
1343

1344
}
1345
#endif /* PNG_INFO_IMAGE_SUPPORTED */
1346
#endif /* PNG_SEQUENTIAL_READ_SUPPORTED */
1347
#endif /* PNG_READ_SUPPORTED */