pngread.c 42.0 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 [June 24, 2009]
5
 * For conditions of distribution and use, see copyright notice in png.h
6
 * Copyright (c) 1998-2009 Glenn Randers-Pehrson
7 8
 * (Version 0.96 Copyright (c) 1996, 1997 Andreas Dilger)
 * (Version 0.88 Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.)
9 10 11 12
 *
 * This file contains routines that an application calls directly to
 * read a PNG file or stream.
 */
G
Guy Schalnat 已提交
13 14

#include "png.h"
15
#if defined(PNG_READ_SUPPORTED)
16
#include "pngpriv.h"
17

18

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

#ifdef PNG_USER_MEM_SUPPORTED
   return (png_create_read_struct_2(user_png_ver, error_ptr, error_fn,
27
      warn_fn, NULL, NULL, NULL));
28 29 30
}

/* Alternate create PNG structure for reading, and allocate any memory needed. */
31
png_structp PNGAPI
32 33 34 35 36 37
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 */

38 39 40 41
#ifdef PNG_SETJMP_SUPPORTED
   volatile
#endif
   png_structp png_ptr;
42
   int png_cleanup_needed = 0;
43 44

#ifdef PNG_SETJMP_SUPPORTED
A
Andreas Dilger 已提交
45 46 47
#ifdef USE_FAR_KEYWORD
   jmp_buf jmpbuf;
#endif
48 49
#endif

50 51
   int i;

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

62
   /* Added at libpng-1.2.6 */
63 64 65 66 67
#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

68 69 70 71 72 73 74 75 76
#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
77
      PNG_ABORT();
78 79
#endif

80 81
#ifdef PNG_USER_MEM_SUPPORTED
   png_set_mem_fn(png_ptr, mem_ptr, malloc_fn, free_fn);
82
#endif
83

A
Andreas Dilger 已提交
84 85
   png_set_error_fn(png_ptr, error_ptr, error_fn, warn_fn);

86
   if (user_png_ver)
G
Guy Schalnat 已提交
87
   {
88 89 90 91 92 93 94 95 96
      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;
97

98

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

130 131
         png_cleanup_needed = 1;
      }
132 133
   }

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

147 148
   if (!png_cleanup_needed)
   {
149 150 151 152 153 154 155 156 157 158 159
      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;
      }
160 161 162 163
   }

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

A
Andreas Dilger 已提交
176 177
   png_ptr->zstream.next_out = png_ptr->zbuf;
   png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size;
G
Guy Schalnat 已提交
178

179
   png_set_read_fn(png_ptr, NULL, NULL);
G
Guy Schalnat 已提交
180

181

G
Guy Schalnat 已提交
182 183 184
   return (png_ptr);
}

185

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

207 208 209
#ifdef PNG_IO_STATE_SUPPORTED
      png_ptr->io_state = PNG_IO_READING | PNG_IO_SIGNATURE;
#endif
210

A
Andreas Dilger 已提交
211
      png_read_data(png_ptr, &(info_ptr->signature[num_checked]), num_to_check);
A
Andreas Dilger 已提交
212 213 214 215 216 217 218 219 220 221
      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");
      }
222 223
      if (num_checked < 3)
         png_ptr->mode |= PNG_HAVE_PNG_SIGNATURE;
G
Guy Schalnat 已提交
224
   }
G
Guy Schalnat 已提交
225

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

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

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

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

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

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

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

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

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

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

   } while (png_ptr->zstream.avail_out);
G
Guy Schalnat 已提交
605 606 607 608 609 610

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

614
   if (png_ptr->row_buf[0])
G
Guy Schalnat 已提交
615 616 617
   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 已提交
618

619
   png_memcpy(png_ptr->prev_row, png_ptr->row_buf, png_ptr->rowbytes + 1);
620

621
#if defined(PNG_MNG_FEATURES_SUPPORTED)
622
   if ((png_ptr->mng_features_permitted & PNG_FLAG_MNG_FILTER_64) &&
623 624 625 626 627 628
      (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 已提交
629

630

631
   if (png_ptr->transformations || (png_ptr->flags&PNG_FLAG_STRIP_ALPHA))
G
Guy Schalnat 已提交
632 633
      png_do_read_transformations(png_ptr);

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

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

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

668
#ifndef PNG_NO_SEQUENTIAL_READ_SUPPORTED
A
Andreas Dilger 已提交
669
/* Read one or more rows of image data.  If the image is interlaced,
670 671
 * and png_set_interlace_handling() has been called, the rows need to
 * contain the contents of the rows from the previous pass.  If the
672
 * image has alpha or transparency, and png_handle_alpha()[*] has been
673 674
 * called, the rows contents must be initialized to the contents of the
 * screen.
675
 *
676 677 678 679 680 681 682 683 684 685 686 687 688
 * "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.
689
 *
690
 * [*] png_handle_alpha() does not exist yet, as of this version of libpng
691
 */
G
Guy Schalnat 已提交
692

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

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

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

731
#ifndef PNG_NO_SEQUENTIAL_READ_SUPPORTED
A
Andreas Dilger 已提交
732
/* Read the entire image.  If the image has an alpha channel or a tRNS
733
 * chunk, and you have called png_handle_alpha()[*], you will need to
734 735 736 737 738 739 740
 * 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.
741
 *
742
 * [*] png_handle_alpha() does not exist yet, as of this version of libpng
743
 */
744
void PNGAPI
G
Guy Schalnat 已提交
745
png_read_image(png_structp png_ptr, png_bytepp image)
G
Guy Schalnat 已提交
746
{
747
   png_uint_32 i, image_height;
G
Guy Schalnat 已提交
748
   int pass, j;
G
Guy Schalnat 已提交
749
   png_bytepp rp;
G
Guy Schalnat 已提交
750

751
   png_debug(1, "in png_read_image");
752 753
   if (png_ptr == NULL)
      return;
754 755

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

764

765 766
   image_height=png_ptr->height;
   png_ptr->num_rows = image_height; /* Make sure this is set correctly */
G
Guy Schalnat 已提交
767

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

780
#ifndef PNG_NO_SEQUENTIAL_READ_SUPPORTED
A
Andreas Dilger 已提交
781
/* Read the end of the PNG file.  Will not read past the end of the
782 783 784
 * 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.
 */
785
void PNGAPI
A
Andreas Dilger 已提交
786
png_read_end(png_structp png_ptr, png_infop info_ptr)
G
Guy Schalnat 已提交
787
{
788
   png_debug(1, "in png_read_end");
789 790
   if (png_ptr == NULL)
      return;
A
Andreas Dilger 已提交
791
   png_crc_finish(png_ptr, 0); /* Finish off CRC from last IDAT chunk */
G
Guy Schalnat 已提交
792 793 794

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

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

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

969
   png_debug(1, "in png_destroy_read_struct");
A
Andreas Dilger 已提交
970
   if (png_ptr_ptr != NULL)
G
Guy Schalnat 已提交
971
      png_ptr = *png_ptr_ptr;
972 973 974 975 976 977 978
   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 已提交
979

A
Andreas Dilger 已提交
980
   if (info_ptr_ptr != NULL)
G
Guy Schalnat 已提交
981 982
      info_ptr = *info_ptr_ptr;

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

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

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

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

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

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

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

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

A
Andreas Dilger 已提交
1047
   if (end_info_ptr != NULL)
A
Andreas Dilger 已提交
1048
      png_info_destroy(png_ptr, end_info_ptr);
G
Guy Schalnat 已提交
1049

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

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

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

   error_fn = png_ptr->error_fn;
   warning_fn = png_ptr->warning_fn;
   error_ptr = png_ptr->error_ptr;
1156 1157 1158
#ifdef PNG_USER_MEM_SUPPORTED
   free_fn = png_ptr->free_fn;
#endif
G
Guy Schalnat 已提交
1159

1160
   png_memset(png_ptr, 0, png_sizeof(png_struct));
G
Guy Schalnat 已提交
1161 1162 1163 1164

   png_ptr->error_fn = error_fn;
   png_ptr->warning_fn = warning_fn;
   png_ptr->error_ptr = error_ptr;
1165 1166 1167
#ifdef PNG_USER_MEM_SUPPORTED
   png_ptr->free_fn = free_fn;
#endif
G
Guy Schalnat 已提交
1168

1169
#ifdef PNG_SETJMP_SUPPORTED
1170
   png_memcpy(png_ptr->jmpbuf, tmp_jmp, png_sizeof(jmp_buf));
1171 1172
#endif

G
Guy Schalnat 已提交
1173
}
1174

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

1183 1184

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

1193 1194
   if (png_ptr == NULL)
      return;
1195
#if defined(PNG_READ_INVERT_ALPHA_SUPPORTED)
1196
   /* Invert the alpha channel from opacity to transparency
1197
    */
1198 1199 1200 1201
   if (transforms & PNG_TRANSFORM_INVERT_ALPHA)
       png_set_invert_alpha(png_ptr);
#endif

1202
   /* png_read_info() gives us all of the information from the
1203 1204 1205
    * PNG file before the first IDAT (image data chunk).
    */
   png_read_info(png_ptr, info_ptr);
1206 1207
   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()");
1208 1209 1210 1211

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

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

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

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

#if defined(PNG_READ_PACKSWAP_SUPPORTED)
   /* Change the order of packed pixels to least significant bit first
1236 1237
    * (not useful if you are using png_set_packing).
    */
1238
   if (transforms & PNG_TRANSFORM_PACKSWAP)
1239
      png_set_packswap(png_ptr);
1240 1241 1242 1243 1244 1245 1246 1247 1248
#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)
1249 1250 1251
      if ((png_ptr->bit_depth < 8) ||
          (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE) ||
          (png_get_valid(png_ptr, info_ptr, PNG_INFO_tRNS)))
1252
         png_set_expand(png_ptr);
1253 1254
#endif

1255 1256
   /* We don't handle background color or gamma transformation or dithering.
    */
1257 1258

#if defined(PNG_READ_INVERT_SUPPORTED)
1259
   /* Invert monochrome files to have 0 as white and 1 as black
1260
    */
1261
   if (transforms & PNG_TRANSFORM_INVERT_MONO)
1262
      png_set_invert_mono(png_ptr);
1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280
#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)
1281
   /* Flip the RGB pixels to BGR (or RGBA to BGRA)
1282
    */
1283
   if (transforms & PNG_TRANSFORM_BGR)
1284
      png_set_bgr(png_ptr);
1285 1286 1287
#endif

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

#if defined(PNG_READ_SWAP_SUPPORTED)
1295
   /* Swap bytes of 16 bit files to least significant byte first
1296
    */
1297
   if (transforms & PNG_TRANSFORM_SWAP_ENDIAN)
1298
      png_set_swap(png_ptr);
1299 1300 1301 1302 1303 1304
#endif

   /* 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
1305
    * update the palette for you (i.e., you selected such a transform above).
1306 1307 1308 1309 1310
    */
   png_read_update_info(png_ptr, info_ptr);

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

1311
#ifdef PNG_FREE_ME_SUPPORTED
1312
   png_free_data(png_ptr, info_ptr, PNG_FREE_ROWS, 0);
1313
#endif
1314
   if (info_ptr->row_pointers == NULL)
1315
   {
1316
#ifdef PNG_CALLOC_SUPPORTED
1317
      info_ptr->row_pointers = (png_bytepp)png_calloc(png_ptr,
1318
         info_ptr->height * png_sizeof(png_bytep));
1319 1320 1321
#else
      info_ptr->row_pointers = (png_bytepp)png_malloc(png_ptr,
         info_ptr->height * png_sizeof(png_bytep));
1322 1323
      png_memset(info_ptr->row_pointers, 0, info_ptr->height
         * png_sizeof(png_bytep));
1324
#endif
1325
#ifdef PNG_FREE_ME_SUPPORTED
1326
      info_ptr->free_me |= PNG_FREE_ROWS;
1327 1328
#endif
      for (row = 0; row < (int)info_ptr->height; row++)
1329
         info_ptr->row_pointers[row] = (png_bytep)png_malloc(png_ptr,
1330
            png_get_rowbytes(png_ptr, info_ptr));
1331
   }
1332 1333 1334 1335

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

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

1339
   transforms = transforms; /* Quiet compiler warnings */
1340
   params = params;
1341

1342
}
1343
#endif /* PNG_INFO_IMAGE_SUPPORTED */
1344
#endif /* PNG_NO_SEQUENTIAL_READ_SUPPORTED */
1345
#endif /* PNG_READ_SUPPORTED */