You need to sign in or sign up before continuing.
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.5.3 [(PENDING RELEASE)]
5
 * Copyright (c) 1998-2011 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 "pngpriv.h"
18

19
#ifdef PNG_READ_SUPPORTED
20

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

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

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

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

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

54
   png_debug(1, "in png_create_read_struct");
55

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

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

70 71
#  ifdef PNG_USER_CHUNK_CACHE_MAX
   /* Added at libpng-1.2.43 and 1.4.0 */
72
   png_ptr->user_chunk_cache_max = PNG_USER_CHUNK_CACHE_MAX;
73
#  endif
74

75 76 77 78
#  ifdef PNG_SET_USER_CHUNK_MALLOC_MAX
   /* Added at libpng-1.2.43 and 1.4.1 */
   png_ptr->user_chunk_malloc_max = PNG_USER_CHUNK_MALLOC_MAX;
#  endif
79 80
#endif

81 82 83 84 85
#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
86
   if (setjmp(tmp_jmpbuf))
87
#else
88
   if (setjmp(png_jmpbuf(png_ptr))) /* Sets longjmp to match setjmp */
89
#endif
90
      PNG_ABORT();
91
#ifdef USE_FAR_KEYWORD
92
   png_memcpy(png_jmpbuf(png_ptr), tmp_jmpbuf, png_sizeof(jmp_buf));
93
#endif
94
#endif /* PNG_SETJMP_SUPPORTED */
95

96 97
#ifdef PNG_USER_MEM_SUPPORTED
   png_set_mem_fn(png_ptr, mem_ptr, malloc_fn, free_fn);
98
#endif
99

A
Andreas Dilger 已提交
100 101
   png_set_error_fn(png_ptr, error_ptr, error_fn, warn_fn);

102 103 104
   /* Call the general version checker (shared with read and write code): */
   if (!png_user_version_check(png_ptr, user_png_ver))
      png_cleanup_needed = 1;
105

106 107
   if (!png_cleanup_needed)
   {
108
   /* Initialize zbuf - compression buffer */
G
Guy Schalnat 已提交
109
   png_ptr->zbuf_size = PNG_ZBUF_SIZE;
110 111
   png_ptr->zbuf = (png_bytep)png_malloc_warn(png_ptr, png_ptr->zbuf_size);

112
   if (png_ptr->zbuf == NULL)
113
      png_cleanup_needed = 1;
114
   }
115

A
Andreas Dilger 已提交
116 117 118
   png_ptr->zstream.zalloc = png_zalloc;
   png_ptr->zstream.zfree = png_zfree;
   png_ptr->zstream.opaque = (voidpf)png_ptr;
G
Guy Schalnat 已提交
119

120 121
   if (!png_cleanup_needed)
   {
122 123
      switch (inflateInit(&png_ptr->zstream))
      {
124 125 126
         case Z_OK:
            break; /* Do nothing */

127
         case Z_MEM_ERROR:
128 129 130 131 132 133 134 135 136 137 138 139 140 141
            png_warning(png_ptr, "zlib memory error");
            png_cleanup_needed = 1;
            break;

         case Z_STREAM_ERROR:
            png_warning(png_ptr, "zlib stream error");
            png_cleanup_needed = 1;
            break;

         case Z_VERSION_ERROR:
            png_warning(png_ptr, "zlib version error");
            png_cleanup_needed = 1;
            break;

142 143 144
         default: png_warning(png_ptr, "Unknown zlib error");
            png_cleanup_needed = 1;
      }
145 146 147 148
   }

   if (png_cleanup_needed)
   {
149 150 151
      /* Clean up PNG structure and deallocate any memory. */
      png_free(png_ptr, png_ptr->zbuf);
      png_ptr->zbuf = NULL;
152
#ifdef PNG_USER_MEM_SUPPORTED
153
      png_destroy_struct_2((png_voidp)png_ptr,
154
          (png_free_ptr)free_fn, (png_voidp)mem_ptr);
155
#else
156
      png_destroy_struct((png_voidp)png_ptr);
157
#endif
158
      return (NULL);
G
Guy Schalnat 已提交
159 160
   }

A
Andreas Dilger 已提交
161 162
   png_ptr->zstream.next_out = png_ptr->zbuf;
   png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size;
G
Guy Schalnat 已提交
163

164
   png_set_read_fn(png_ptr, NULL, NULL);
G
Guy Schalnat 已提交
165

166

G
Guy Schalnat 已提交
167 168 169
   return (png_ptr);
}

170

171
#ifdef PNG_SEQUENTIAL_READ_SUPPORTED
A
Andreas Dilger 已提交
172
/* Read the information before the actual image data.  This has been
173
 * changed in v0.90 to allow reading a file that already has the magic
A
Andreas Dilger 已提交
174
 * bytes read from the stream.  You can tell libpng how many bytes have
175
 * been read from the beginning of the stream (up to the maximum of 8)
A
Andreas Dilger 已提交
176 177 178 179
 * 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.
 */
180
void PNGAPI
A
Andreas Dilger 已提交
181
png_read_info(png_structp png_ptr, png_infop info_ptr)
G
Guy Schalnat 已提交
182
{
183
   png_debug(1, "in png_read_info");
184

185 186
   if (png_ptr == NULL || info_ptr == NULL)
      return;
187

188 189
   /* Read and check the PNG file signature. */
   png_read_sig(png_ptr, info_ptr);
G
Guy Schalnat 已提交
190

191
   for (;;)
G
Guy Schalnat 已提交
192
   {
193 194 195 196
      PNG_IHDR;
      PNG_IDAT;
      PNG_IEND;
      PNG_PLTE;
197
#ifdef PNG_READ_bKGD_SUPPORTED
198
      PNG_bKGD;
199
#endif
200
#ifdef PNG_READ_cHRM_SUPPORTED
201
      PNG_cHRM;
202
#endif
203
#ifdef PNG_READ_gAMA_SUPPORTED
204
      PNG_gAMA;
205
#endif
206
#ifdef PNG_READ_hIST_SUPPORTED
207
      PNG_hIST;
208
#endif
209
#ifdef PNG_READ_iCCP_SUPPORTED
210
      PNG_iCCP;
211
#endif
212
#ifdef PNG_READ_iTXt_SUPPORTED
213
      PNG_iTXt;
214
#endif
215
#ifdef PNG_READ_oFFs_SUPPORTED
216
      PNG_oFFs;
217
#endif
218
#ifdef PNG_READ_pCAL_SUPPORTED
219
      PNG_pCAL;
220
#endif
221
#ifdef PNG_READ_pHYs_SUPPORTED
222
      PNG_pHYs;
223
#endif
224
#ifdef PNG_READ_sBIT_SUPPORTED
225
      PNG_sBIT;
226
#endif
227
#ifdef PNG_READ_sCAL_SUPPORTED
228
      PNG_sCAL;
229
#endif
230
#ifdef PNG_READ_sPLT_SUPPORTED
231
      PNG_sPLT;
232
#endif
233
#ifdef PNG_READ_sRGB_SUPPORTED
234
      PNG_sRGB;
235
#endif
236
#ifdef PNG_READ_tEXt_SUPPORTED
237
      PNG_tEXt;
238
#endif
239
#ifdef PNG_READ_tIME_SUPPORTED
240
      PNG_tIME;
241
#endif
242
#ifdef PNG_READ_tRNS_SUPPORTED
243
      PNG_tRNS;
244
#endif
245
#ifdef PNG_READ_zTXt_SUPPORTED
246
      PNG_zTXt;
247
#endif
248
      png_uint_32 length = png_read_chunk_header(png_ptr);
249
      PNG_CONST png_bytep chunk_name = png_ptr->chunk_name;
A
Andreas Dilger 已提交
250 251 252 253

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

258
      if (!png_memcmp(chunk_name, png_IHDR, 4))
A
Andreas Dilger 已提交
259
         png_handle_IHDR(png_ptr, info_ptr, length);
260

261
      else if (!png_memcmp(chunk_name, png_IEND, 4))
A
Andreas Dilger 已提交
262
         png_handle_IEND(png_ptr, info_ptr, length);
263

264
#ifdef PNG_HANDLE_AS_UNKNOWN_SUPPORTED
265
      else if (png_handle_as_unknown(png_ptr, chunk_name))
266
      {
267
         if (!png_memcmp(chunk_name, png_IDAT, 4))
268
            png_ptr->mode |= PNG_HAVE_IDAT;
269

270
         png_handle_unknown(png_ptr, info_ptr, length);
271

272
         if (!png_memcmp(chunk_name, png_PLTE, 4))
273
            png_ptr->mode |= PNG_HAVE_PLTE;
274

275
         else if (!png_memcmp(chunk_name, png_IDAT, 4))
276 277 278
         {
            if (!(png_ptr->mode & PNG_HAVE_IHDR))
               png_error(png_ptr, "Missing IHDR before IDAT");
279

280
            else if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE &&
281
                !(png_ptr->mode & PNG_HAVE_PLTE))
282
               png_error(png_ptr, "Missing PLTE before IDAT");
283

284 285 286 287
            break;
         }
      }
#endif
288
      else if (!png_memcmp(chunk_name, png_PLTE, 4))
289
         png_handle_PLTE(png_ptr, info_ptr, length);
290

291
      else if (!png_memcmp(chunk_name, png_IDAT, 4))
A
Andreas Dilger 已提交
292 293 294
      {
         if (!(png_ptr->mode & PNG_HAVE_IHDR))
            png_error(png_ptr, "Missing IHDR before IDAT");
295

A
Andreas Dilger 已提交
296
         else if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE &&
297
             !(png_ptr->mode & PNG_HAVE_PLTE))
A
Andreas Dilger 已提交
298 299 300 301 302 303
            png_error(png_ptr, "Missing PLTE before IDAT");

         png_ptr->idat_size = length;
         png_ptr->mode |= PNG_HAVE_IDAT;
         break;
      }
304

305
#ifdef PNG_READ_bKGD_SUPPORTED
306
      else if (!png_memcmp(chunk_name, png_bKGD, 4))
A
Andreas Dilger 已提交
307
         png_handle_bKGD(png_ptr, info_ptr, length);
G
Guy Schalnat 已提交
308
#endif
309

310
#ifdef PNG_READ_cHRM_SUPPORTED
311
      else if (!png_memcmp(chunk_name, png_cHRM, 4))
A
Andreas Dilger 已提交
312
         png_handle_cHRM(png_ptr, info_ptr, length);
G
Guy Schalnat 已提交
313
#endif
314

315
#ifdef PNG_READ_gAMA_SUPPORTED
316
      else if (!png_memcmp(chunk_name, png_gAMA, 4))
A
Andreas Dilger 已提交
317
         png_handle_gAMA(png_ptr, info_ptr, length);
G
Guy Schalnat 已提交
318
#endif
319

320
#ifdef PNG_READ_hIST_SUPPORTED
321
      else if (!png_memcmp(chunk_name, png_hIST, 4))
A
Andreas Dilger 已提交
322
         png_handle_hIST(png_ptr, info_ptr, length);
G
Guy Schalnat 已提交
323
#endif
324

325
#ifdef PNG_READ_oFFs_SUPPORTED
326
      else if (!png_memcmp(chunk_name, png_oFFs, 4))
A
Andreas Dilger 已提交
327 328
         png_handle_oFFs(png_ptr, info_ptr, length);
#endif
329

330
#ifdef PNG_READ_pCAL_SUPPORTED
331
      else if (!png_memcmp(chunk_name, png_pCAL, 4))
A
Andreas Dilger 已提交
332 333
         png_handle_pCAL(png_ptr, info_ptr, length);
#endif
334

335
#ifdef PNG_READ_sCAL_SUPPORTED
336
      else if (!png_memcmp(chunk_name, png_sCAL, 4))
337 338
         png_handle_sCAL(png_ptr, info_ptr, length);
#endif
339

340
#ifdef PNG_READ_pHYs_SUPPORTED
341
      else if (!png_memcmp(chunk_name, png_pHYs, 4))
A
Andreas Dilger 已提交
342
         png_handle_pHYs(png_ptr, info_ptr, length);
G
Guy Schalnat 已提交
343
#endif
344

345
#ifdef PNG_READ_sBIT_SUPPORTED
346
      else if (!png_memcmp(chunk_name, png_sBIT, 4))
A
Andreas Dilger 已提交
347 348
         png_handle_sBIT(png_ptr, info_ptr, length);
#endif
349

350
#ifdef PNG_READ_sRGB_SUPPORTED
351
      else if (!png_memcmp(chunk_name, png_sRGB, 4))
352 353
         png_handle_sRGB(png_ptr, info_ptr, length);
#endif
354

355
#ifdef PNG_READ_iCCP_SUPPORTED
356
      else if (!png_memcmp(chunk_name, png_iCCP, 4))
357 358
         png_handle_iCCP(png_ptr, info_ptr, length);
#endif
359

360
#ifdef PNG_READ_sPLT_SUPPORTED
361
      else if (!png_memcmp(chunk_name, png_sPLT, 4))
362 363
         png_handle_sPLT(png_ptr, info_ptr, length);
#endif
364

365
#ifdef PNG_READ_tEXt_SUPPORTED
366
      else if (!png_memcmp(chunk_name, png_tEXt, 4))
A
Andreas Dilger 已提交
367
         png_handle_tEXt(png_ptr, info_ptr, length);
G
Guy Schalnat 已提交
368
#endif
369

370
#ifdef PNG_READ_tIME_SUPPORTED
371
      else if (!png_memcmp(chunk_name, png_tIME, 4))
A
Andreas Dilger 已提交
372
         png_handle_tIME(png_ptr, info_ptr, length);
G
Guy Schalnat 已提交
373
#endif
374

375
#ifdef PNG_READ_tRNS_SUPPORTED
376
      else if (!png_memcmp(chunk_name, png_tRNS, 4))
A
Andreas Dilger 已提交
377
         png_handle_tRNS(png_ptr, info_ptr, length);
G
Guy Schalnat 已提交
378
#endif
379

380
#ifdef PNG_READ_zTXt_SUPPORTED
381
      else if (!png_memcmp(chunk_name, png_zTXt, 4))
A
Andreas Dilger 已提交
382
         png_handle_zTXt(png_ptr, info_ptr, length);
383
#endif
384

385
#ifdef PNG_READ_iTXt_SUPPORTED
386
      else if (!png_memcmp(chunk_name, png_iTXt, 4))
387
         png_handle_iTXt(png_ptr, info_ptr, length);
G
Guy Schalnat 已提交
388
#endif
389

A
Andreas Dilger 已提交
390 391
      else
         png_handle_unknown(png_ptr, info_ptr, length);
G
Guy Schalnat 已提交
392 393
   }
}
394
#endif /* PNG_SEQUENTIAL_READ_SUPPORTED */
G
Guy Schalnat 已提交
395

396
/* Optional call to update the users info_ptr structure */
397
void PNGAPI
A
Andreas Dilger 已提交
398
png_read_update_info(png_structp png_ptr, png_infop info_ptr)
G
Guy Schalnat 已提交
399
{
400
   png_debug(1, "in png_read_update_info");
401

402 403
   if (png_ptr == NULL)
      return;
404

G
Guy Schalnat 已提交
405
   if (!(png_ptr->flags & PNG_FLAG_ROW_INIT))
G
Guy Schalnat 已提交
406
      png_read_start_row(png_ptr);
407

408 409
   else
      png_warning(png_ptr,
410 411
          "Ignoring extra png_read_update_info() call;"
          " row buffer not reallocated");
412

413
#ifdef PNG_READ_TRANSFORMS_SUPPORTED
A
Andreas Dilger 已提交
414
   png_read_transform_info(png_ptr, info_ptr);
415 416 417
#else
   PNG_UNUSED(info_ptr)
#endif
G
Guy Schalnat 已提交
418 419
}

420
#ifdef PNG_SEQUENTIAL_READ_SUPPORTED
421 422
/* Initialize palette, background, etc, after transformations
 * are set, but before any reading takes place.  This allows
423
 * the user to obtain a gamma-corrected palette, for example.
424 425
 * If the user doesn't call this, we will do it ourselves.
 */
426
void PNGAPI
G
Guy Schalnat 已提交
427
png_start_read_image(png_structp png_ptr)
G
Guy Schalnat 已提交
428
{
429
   png_debug(1, "in png_start_read_image");
430

431 432
   if (png_ptr == NULL)
      return;
433

G
Guy Schalnat 已提交
434
   if (!(png_ptr->flags & PNG_FLAG_ROW_INIT))
G
Guy Schalnat 已提交
435
      png_read_start_row(png_ptr);
G
[devel]  
Glenn Randers-Pehrson 已提交
436 437 438 439
   else
      png_warning(png_ptr,
          "Ignoring extra png_start_read_image() call;"
          " row buffer not reallocated");
G
Guy Schalnat 已提交
440
}
441
#endif /* PNG_SEQUENTIAL_READ_SUPPORTED */
G
Guy Schalnat 已提交
442

443
#ifdef PNG_SEQUENTIAL_READ_SUPPORTED
444
void PNGAPI
G
Guy Schalnat 已提交
445
png_read_row(png_structp png_ptr, png_bytep row, png_bytep dsp_row)
G
Guy Schalnat 已提交
446
{
447
   PNG_IDAT;
448
#ifdef PNG_READ_INTERLACING_SUPPORTED
449
   PNG_CONST int png_pass_dsp_mask[7] = {0xff, 0x0f, 0xff, 0x33, 0xff, 0x55,
450
       0xff};
451
   PNG_CONST int png_pass_mask[7] = {0x80, 0x08, 0x88, 0x22, 0xaa, 0x55, 0xff};
452
#endif
G
Guy Schalnat 已提交
453
   int ret;
454

455 456
   if (png_ptr == NULL)
      return;
457

458
   png_debug2(1, "in png_read_row (row %lu, pass %d)",
459
       (unsigned long)png_ptr->row_number, png_ptr->pass);
460

G
Guy Schalnat 已提交
461
   if (!(png_ptr->flags & PNG_FLAG_ROW_INIT))
G
Guy Schalnat 已提交
462
      png_read_start_row(png_ptr);
463

464 465
   if (png_ptr->row_number == 0 && png_ptr->pass == 0)
   {
466
   /* Check for transforms that have been set but were defined out */
467 468
#if defined(PNG_WRITE_INVERT_SUPPORTED) && !defined(PNG_READ_INVERT_SUPPORTED)
   if (png_ptr->transformations & PNG_INVERT_MONO)
469
      png_warning(png_ptr, "PNG_READ_INVERT_SUPPORTED is not defined");
470
#endif
471

472 473
#if defined(PNG_WRITE_FILLER_SUPPORTED) && !defined(PNG_READ_FILLER_SUPPORTED)
   if (png_ptr->transformations & PNG_FILLER)
474
      png_warning(png_ptr, "PNG_READ_FILLER_SUPPORTED is not defined");
475
#endif
476

477 478
#if defined(PNG_WRITE_PACKSWAP_SUPPORTED) && \
    !defined(PNG_READ_PACKSWAP_SUPPORTED)
479
   if (png_ptr->transformations & PNG_PACKSWAP)
480
      png_warning(png_ptr, "PNG_READ_PACKSWAP_SUPPORTED is not defined");
481
#endif
482

483 484
#if defined(PNG_WRITE_PACK_SUPPORTED) && !defined(PNG_READ_PACK_SUPPORTED)
   if (png_ptr->transformations & PNG_PACK)
485
      png_warning(png_ptr, "PNG_READ_PACK_SUPPORTED is not defined");
486
#endif
487

488 489
#if defined(PNG_WRITE_SHIFT_SUPPORTED) && !defined(PNG_READ_SHIFT_SUPPORTED)
   if (png_ptr->transformations & PNG_SHIFT)
490
      png_warning(png_ptr, "PNG_READ_SHIFT_SUPPORTED is not defined");
491
#endif
492

493 494
#if defined(PNG_WRITE_BGR_SUPPORTED) && !defined(PNG_READ_BGR_SUPPORTED)
   if (png_ptr->transformations & PNG_BGR)
495
      png_warning(png_ptr, "PNG_READ_BGR_SUPPORTED is not defined");
496
#endif
497

498 499
#if defined(PNG_WRITE_SWAP_SUPPORTED) && !defined(PNG_READ_SWAP_SUPPORTED)
   if (png_ptr->transformations & PNG_SWAP_BYTES)
500
      png_warning(png_ptr, "PNG_READ_SWAP_SUPPORTED is not defined");
501 502
#endif
   }
G
Guy Schalnat 已提交
503

504
#ifdef PNG_READ_INTERLACING_SUPPORTED
505
   /* If interlaced and we do not need a new row, combine row and return */
G
Guy Schalnat 已提交
506 507 508 509 510
   if (png_ptr->interlaced && (png_ptr->transformations & PNG_INTERLACE))
   {
      switch (png_ptr->pass)
      {
         case 0:
511
            if (png_ptr->row_number & 0x07)
G
Guy Schalnat 已提交
512
            {
A
Andreas Dilger 已提交
513
               if (dsp_row != NULL)
G
Guy Schalnat 已提交
514 515
                  png_combine_row(png_ptr, dsp_row,
                     png_pass_dsp_mask[png_ptr->pass]);
G
Guy Schalnat 已提交
516
               png_read_finish_row(png_ptr);
G
Guy Schalnat 已提交
517 518 519
               return;
            }
            break;
520

G
Guy Schalnat 已提交
521
         case 1:
522
            if ((png_ptr->row_number & 0x07) || png_ptr->width < 5)
G
Guy Schalnat 已提交
523
            {
A
Andreas Dilger 已提交
524
               if (dsp_row != NULL)
G
Guy Schalnat 已提交
525
                  png_combine_row(png_ptr, dsp_row,
526 527
                      png_pass_dsp_mask[png_ptr->pass]);

G
Guy Schalnat 已提交
528 529 530 531
               png_read_finish_row(png_ptr);
               return;
            }
            break;
532

G
Guy Schalnat 已提交
533
         case 2:
534
            if ((png_ptr->row_number & 0x07) != 4)
G
Guy Schalnat 已提交
535
            {
A
Andreas Dilger 已提交
536
               if (dsp_row != NULL && (png_ptr->row_number & 4))
G
Guy Schalnat 已提交
537
                  png_combine_row(png_ptr, dsp_row,
538 539
                      png_pass_dsp_mask[png_ptr->pass]);

G
Guy Schalnat 已提交
540 541 542 543
               png_read_finish_row(png_ptr);
               return;
            }
            break;
544

G
Guy Schalnat 已提交
545 546 547
         case 3:
            if ((png_ptr->row_number & 3) || png_ptr->width < 3)
            {
A
Andreas Dilger 已提交
548
               if (dsp_row != NULL)
G
Guy Schalnat 已提交
549
                  png_combine_row(png_ptr, dsp_row,
550 551
                      png_pass_dsp_mask[png_ptr->pass]);

G
Guy Schalnat 已提交
552 553 554 555
               png_read_finish_row(png_ptr);
               return;
            }
            break;
556

G
Guy Schalnat 已提交
557 558
         case 4:
            if ((png_ptr->row_number & 3) != 2)
G
Guy Schalnat 已提交
559
            {
A
Andreas Dilger 已提交
560
               if (dsp_row != NULL && (png_ptr->row_number & 2))
G
Guy Schalnat 已提交
561
                  png_combine_row(png_ptr, dsp_row,
562 563
                      png_pass_dsp_mask[png_ptr->pass]);

G
Guy Schalnat 已提交
564 565 566 567 568 569 570
               png_read_finish_row(png_ptr);
               return;
            }
            break;
         case 5:
            if ((png_ptr->row_number & 1) || png_ptr->width < 2)
            {
A
Andreas Dilger 已提交
571
               if (dsp_row != NULL)
G
Guy Schalnat 已提交
572
                  png_combine_row(png_ptr, dsp_row,
573 574
                      png_pass_dsp_mask[png_ptr->pass]);

G
Guy Schalnat 已提交
575 576 577 578
               png_read_finish_row(png_ptr);
               return;
            }
            break;
579

580
         default:
G
Guy Schalnat 已提交
581
         case 6:
G
Guy Schalnat 已提交
582 583 584 585 586 587 588 589
            if (!(png_ptr->row_number & 1))
            {
               png_read_finish_row(png_ptr);
               return;
            }
            break;
      }
   }
G
Guy Schalnat 已提交
590
#endif
G
Guy Schalnat 已提交
591

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

A
Andreas Dilger 已提交
595
   png_ptr->zstream.next_out = png_ptr->row_buf;
596 597 598
   png_ptr->zstream.avail_out =
       (uInt)(PNG_ROWBYTES(png_ptr->pixel_depth,
       png_ptr->iwidth) + 1);
599

G
Guy Schalnat 已提交
600 601
   do
   {
A
Andreas Dilger 已提交
602
      if (!(png_ptr->zstream.avail_in))
G
Guy Schalnat 已提交
603 604 605
      {
         while (!png_ptr->idat_size)
         {
A
Andreas Dilger 已提交
606
            png_crc_finish(png_ptr, 0);
G
Guy Schalnat 已提交
607

608
            png_ptr->idat_size = png_read_chunk_header(png_ptr);
A
Andreas Dilger 已提交
609 610
            if (png_memcmp(png_ptr->chunk_name, png_IDAT, 4))
               png_error(png_ptr, "Not enough image data");
G
Guy Schalnat 已提交
611
         }
A
Andreas Dilger 已提交
612 613
         png_ptr->zstream.avail_in = (uInt)png_ptr->zbuf_size;
         png_ptr->zstream.next_in = png_ptr->zbuf;
G
Guy Schalnat 已提交
614
         if (png_ptr->zbuf_size > png_ptr->idat_size)
A
Andreas Dilger 已提交
615
            png_ptr->zstream.avail_in = (uInt)png_ptr->idat_size;
A
Andreas Dilger 已提交
616
         png_crc_read(png_ptr, png_ptr->zbuf,
617
             (png_size_t)png_ptr->zstream.avail_in);
A
Andreas Dilger 已提交
618
         png_ptr->idat_size -= png_ptr->zstream.avail_in;
G
Guy Schalnat 已提交
619
      }
620

A
Andreas Dilger 已提交
621
      ret = inflate(&png_ptr->zstream, Z_PARTIAL_FLUSH);
622

G
Guy Schalnat 已提交
623 624
      if (ret == Z_STREAM_END)
      {
A
Andreas Dilger 已提交
625
         if (png_ptr->zstream.avail_out || png_ptr->zstream.avail_in ||
G
Guy Schalnat 已提交
626
            png_ptr->idat_size)
627
            png_benign_error(png_ptr, "Extra compressed data");
A
Andreas Dilger 已提交
628
         png_ptr->mode |= PNG_AFTER_IDAT;
G
Guy Schalnat 已提交
629
         png_ptr->flags |= PNG_FLAG_ZLIB_FINISHED;
G
Guy Schalnat 已提交
630
         break;
G
Guy Schalnat 已提交
631
      }
632

G
Guy Schalnat 已提交
633
      if (ret != Z_OK)
A
Andreas Dilger 已提交
634
         png_error(png_ptr, png_ptr->zstream.msg ? png_ptr->zstream.msg :
635
             "Decompression error");
A
Andreas Dilger 已提交
636 637

   } while (png_ptr->zstream.avail_out);
G
Guy Schalnat 已提交
638 639 640 641 642 643

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

647
   if (png_ptr->row_buf[0])
G
Guy Schalnat 已提交
648
   png_read_filter_row(png_ptr, &(png_ptr->row_info),
649 650
       png_ptr->row_buf + 1, png_ptr->prev_row + 1,
       (int)(png_ptr->row_buf[0]));
G
Guy Schalnat 已提交
651

652
   png_memcpy(png_ptr->prev_row, png_ptr->row_buf, png_ptr->rowbytes + 1);
653

654
#ifdef PNG_MNG_FEATURES_SUPPORTED
655
   if ((png_ptr->mng_features_permitted & PNG_FLAG_MNG_FILTER_64) &&
656
       (png_ptr->filter_type == PNG_INTRAPIXEL_DIFFERENCING))
657 658 659 660 661
   {
      /* Intrapixel differencing */
      png_do_read_intrapixel(&(png_ptr->row_info), png_ptr->row_buf + 1);
   }
#endif
G
Guy Schalnat 已提交
662

663

664
#ifdef PNG_READ_TRANSFORMS_SUPPORTED
665
   if (png_ptr->transformations)
G
Guy Schalnat 已提交
666
      png_do_read_transformations(png_ptr);
667
#endif
G
Guy Schalnat 已提交
668

669
#ifdef PNG_READ_INTERLACING_SUPPORTED
670
   /* Blow up interlaced rows to full size */
G
Guy Schalnat 已提交
671 672 673 674
   if (png_ptr->interlaced &&
      (png_ptr->transformations & PNG_INTERLACE))
   {
      if (png_ptr->pass < 6)
675
         /* Old interface (pre-1.0.9):
676 677 678
          * png_do_read_interlace(&(png_ptr->row_info),
          *    png_ptr->row_buf + 1, png_ptr->pass, png_ptr->transformations);
          */
679
         png_do_read_interlace(png_ptr);
G
Guy Schalnat 已提交
680

A
Andreas Dilger 已提交
681
      if (dsp_row != NULL)
682
         png_combine_row(png_ptr, dsp_row, png_pass_dsp_mask[png_ptr->pass]);
683

A
Andreas Dilger 已提交
684
      if (row != NULL)
685
         png_combine_row(png_ptr, row, png_pass_mask[png_ptr->pass]);
G
Guy Schalnat 已提交
686
   }
687

G
Guy Schalnat 已提交
688
   else
G
Guy Schalnat 已提交
689
#endif
G
Guy Schalnat 已提交
690
   {
A
Andreas Dilger 已提交
691
      if (row != NULL)
G
Guy Schalnat 已提交
692
         png_combine_row(png_ptr, row, 0xff);
693

A
Andreas Dilger 已提交
694
      if (dsp_row != NULL)
G
Guy Schalnat 已提交
695 696 697
         png_combine_row(png_ptr, dsp_row, 0xff);
   }
   png_read_finish_row(png_ptr);
698 699 700

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

704
#ifdef PNG_SEQUENTIAL_READ_SUPPORTED
A
Andreas Dilger 已提交
705
/* Read one or more rows of image data.  If the image is interlaced,
706 707
 * and png_set_interlace_handling() has been called, the rows need to
 * contain the contents of the rows from the previous pass.  If the
708
 * image has alpha or transparency, and png_handle_alpha()[*] has been
709 710
 * called, the rows contents must be initialized to the contents of the
 * screen.
711
 *
712 713 714 715 716 717 718 719 720 721 722 723 724
 * "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.
725
 *
726
 * [*] png_handle_alpha() does not exist yet, as of this version of libpng
727
 */
G
Guy Schalnat 已提交
728

729
void PNGAPI
G
Guy Schalnat 已提交
730
png_read_rows(png_structp png_ptr, png_bytepp row,
731
    png_bytepp display_row, png_uint_32 num_rows)
G
Guy Schalnat 已提交
732
{
G
Guy Schalnat 已提交
733 734 735
   png_uint_32 i;
   png_bytepp rp;
   png_bytepp dp;
G
Guy Schalnat 已提交
736

737
   png_debug(1, "in png_read_rows");
738

739 740
   if (png_ptr == NULL)
      return;
741

G
Guy Schalnat 已提交
742 743
   rp = row;
   dp = display_row;
744
   if (rp != NULL && dp != NULL)
745 746 747 748
      for (i = 0; i < num_rows; i++)
      {
         png_bytep rptr = *rp++;
         png_bytep dptr = *dp++;
749

750 751
         png_read_row(png_ptr, rptr, dptr);
      }
752

753
   else if (rp != NULL)
754 755
      for (i = 0; i < num_rows; i++)
      {
756
         png_bytep rptr = *rp;
757
         png_read_row(png_ptr, rptr, NULL);
758 759
         rp++;
      }
760

761
   else if (dp != NULL)
762 763 764
      for (i = 0; i < num_rows; i++)
      {
         png_bytep dptr = *dp;
765
         png_read_row(png_ptr, NULL, dptr);
766
         dp++;
767
      }
G
Guy Schalnat 已提交
768
}
769
#endif /* PNG_SEQUENTIAL_READ_SUPPORTED */
G
Guy Schalnat 已提交
770

771
#ifdef PNG_SEQUENTIAL_READ_SUPPORTED
A
Andreas Dilger 已提交
772
/* Read the entire image.  If the image has an alpha channel or a tRNS
773
 * chunk, and you have called png_handle_alpha()[*], you will need to
774 775 776 777 778 779 780
 * 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.
781
 *
782
 * [*] png_handle_alpha() does not exist yet, as of this version of libpng
783
 */
784
void PNGAPI
G
Guy Schalnat 已提交
785
png_read_image(png_structp png_ptr, png_bytepp image)
G
Guy Schalnat 已提交
786
{
787
   png_uint_32 i, image_height;
G
Guy Schalnat 已提交
788
   int pass, j;
G
Guy Schalnat 已提交
789
   png_bytepp rp;
G
Guy Schalnat 已提交
790

791
   png_debug(1, "in png_read_image");
792

793 794
   if (png_ptr == NULL)
      return;
795 796

#ifdef PNG_READ_INTERLACING_SUPPORTED
G
[devel]  
Glenn Randers-Pehrson 已提交
797 798 799 800 801 802 803 804
   if (!(png_ptr->flags & PNG_FLAG_ROW_INIT))
   {
      pass = png_set_interlace_handling(png_ptr);
      /* And make sure transforms are initialized. */
      png_start_read_image(png_ptr);
   }
   else
   {
805
      if (png_ptr->interlaced && !(png_ptr->transformations & PNG_INTERLACE))
G
[devel]  
Glenn Randers-Pehrson 已提交
806
      {
807 808 809 810 811 812 813 814
         /* Caller called png_start_read_image or png_read_update_info without
          * first turning on the PNG_INTERLACE transform.  We can fix this here,
          * but the caller should do it!
          */
         png_warning(png_ptr, "Interlace handling should be turned on when "
            "using png_read_image");
         /* Make sure this is set correctly */
         png_ptr->num_rows = png_ptr->height;
G
[devel]  
Glenn Randers-Pehrson 已提交
815 816 817 818 819 820 821
      }

      /* Obtain the pass number, which also turns on the PNG_INTERLACE flag in
       * the above error case.
       */
      pass = png_set_interlace_handling(png_ptr);
   }
822 823 824
#else
   if (png_ptr->interlaced)
      png_error(png_ptr,
825
          "Cannot read interlaced image -- interlace handler disabled");
826

827 828 829
   pass = 1;
#endif

830
   image_height=png_ptr->height;
G
Guy Schalnat 已提交
831

G
Guy Schalnat 已提交
832 833 834
   for (j = 0; j < pass; j++)
   {
      rp = image;
835
      for (i = 0; i < image_height; i++)
G
Guy Schalnat 已提交
836
      {
837
         png_read_row(png_ptr, *rp, NULL);
G
Guy Schalnat 已提交
838 839 840 841
         rp++;
      }
   }
}
842
#endif /* PNG_SEQUENTIAL_READ_SUPPORTED */
G
Guy Schalnat 已提交
843

844
#ifdef PNG_SEQUENTIAL_READ_SUPPORTED
A
Andreas Dilger 已提交
845
/* Read the end of the PNG file.  Will not read past the end of the
846 847 848
 * 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.
 */
849
void PNGAPI
A
Andreas Dilger 已提交
850
png_read_end(png_structp png_ptr, png_infop info_ptr)
G
Guy Schalnat 已提交
851
{
852
   png_debug(1, "in png_read_end");
853

854 855
   if (png_ptr == NULL)
      return;
856

A
Andreas Dilger 已提交
857
   png_crc_finish(png_ptr, 0); /* Finish off CRC from last IDAT chunk */
G
Guy Schalnat 已提交
858 859 860

   do
   {
861 862 863 864
      PNG_IHDR;
      PNG_IDAT;
      PNG_IEND;
      PNG_PLTE;
865
#ifdef PNG_READ_bKGD_SUPPORTED
866
      PNG_bKGD;
867
#endif
868
#ifdef PNG_READ_cHRM_SUPPORTED
869
      PNG_cHRM;
870
#endif
871
#ifdef PNG_READ_gAMA_SUPPORTED
872
      PNG_gAMA;
873
#endif
874
#ifdef PNG_READ_hIST_SUPPORTED
875
      PNG_hIST;
876
#endif
877
#ifdef PNG_READ_iCCP_SUPPORTED
878
      PNG_iCCP;
879
#endif
880
#ifdef PNG_READ_iTXt_SUPPORTED
881
      PNG_iTXt;
882
#endif
883
#ifdef PNG_READ_oFFs_SUPPORTED
884
      PNG_oFFs;
885
#endif
886
#ifdef PNG_READ_pCAL_SUPPORTED
887
      PNG_pCAL;
888
#endif
889
#ifdef PNG_READ_pHYs_SUPPORTED
890
      PNG_pHYs;
891
#endif
892
#ifdef PNG_READ_sBIT_SUPPORTED
893
      PNG_sBIT;
894
#endif
895
#ifdef PNG_READ_sCAL_SUPPORTED
896
      PNG_sCAL;
897
#endif
898
#ifdef PNG_READ_sPLT_SUPPORTED
899
      PNG_sPLT;
900
#endif
901
#ifdef PNG_READ_sRGB_SUPPORTED
902
      PNG_sRGB;
903
#endif
904
#ifdef PNG_READ_tEXt_SUPPORTED
905
      PNG_tEXt;
906
#endif
907
#ifdef PNG_READ_tIME_SUPPORTED
908
      PNG_tIME;
909
#endif
910
#ifdef PNG_READ_tRNS_SUPPORTED
911
      PNG_tRNS;
912
#endif
913
#ifdef PNG_READ_zTXt_SUPPORTED
914
      PNG_zTXt;
915
#endif
916 917
      png_uint_32 length = png_read_chunk_header(png_ptr);
      PNG_CONST png_bytep chunk_name = png_ptr->chunk_name;
A
Andreas Dilger 已提交
918

919
      if (!png_memcmp(chunk_name, png_IHDR, 4))
A
Andreas Dilger 已提交
920
         png_handle_IHDR(png_ptr, info_ptr, length);
921

922
      else if (!png_memcmp(chunk_name, png_IEND, 4))
923
         png_handle_IEND(png_ptr, info_ptr, length);
924

925
#ifdef PNG_HANDLE_AS_UNKNOWN_SUPPORTED
926
      else if (png_handle_as_unknown(png_ptr, chunk_name))
927
      {
928
         if (!png_memcmp(chunk_name, png_IDAT, 4))
929
         {
930
            if ((length > 0) || (png_ptr->mode & PNG_HAVE_CHUNK_AFTER_IDAT))
931
               png_benign_error(png_ptr, "Too many IDATs found");
932 933
         }
         png_handle_unknown(png_ptr, info_ptr, length);
934
         if (!png_memcmp(chunk_name, png_PLTE, 4))
935 936 937
            png_ptr->mode |= PNG_HAVE_PLTE;
      }
#endif
938

939
      else if (!png_memcmp(chunk_name, png_IDAT, 4))
A
Andreas Dilger 已提交
940 941 942 943
      {
         /* Zero length IDATs are legal after the last IDAT has been
          * read, but not after other chunks have been read.
          */
944
         if ((length > 0) || (png_ptr->mode & PNG_HAVE_CHUNK_AFTER_IDAT))
945
            png_benign_error(png_ptr, "Too many IDATs found");
946

947
         png_crc_finish(png_ptr, length);
A
Andreas Dilger 已提交
948
      }
949
      else if (!png_memcmp(chunk_name, png_PLTE, 4))
A
Andreas Dilger 已提交
950
         png_handle_PLTE(png_ptr, info_ptr, length);
951

952
#ifdef PNG_READ_bKGD_SUPPORTED
953
      else if (!png_memcmp(chunk_name, png_bKGD, 4))
A
Andreas Dilger 已提交
954
         png_handle_bKGD(png_ptr, info_ptr, length);
A
Andreas Dilger 已提交
955
#endif
956

957
#ifdef PNG_READ_cHRM_SUPPORTED
958
      else if (!png_memcmp(chunk_name, png_cHRM, 4))
A
Andreas Dilger 已提交
959 960
         png_handle_cHRM(png_ptr, info_ptr, length);
#endif
961

962
#ifdef PNG_READ_gAMA_SUPPORTED
963
      else if (!png_memcmp(chunk_name, png_gAMA, 4))
A
Andreas Dilger 已提交
964 965
         png_handle_gAMA(png_ptr, info_ptr, length);
#endif
966

967
#ifdef PNG_READ_hIST_SUPPORTED
968
      else if (!png_memcmp(chunk_name, png_hIST, 4))
A
Andreas Dilger 已提交
969
         png_handle_hIST(png_ptr, info_ptr, length);
A
Andreas Dilger 已提交
970
#endif
971

972
#ifdef PNG_READ_oFFs_SUPPORTED
973
      else if (!png_memcmp(chunk_name, png_oFFs, 4))
A
Andreas Dilger 已提交
974
         png_handle_oFFs(png_ptr, info_ptr, length);
A
Andreas Dilger 已提交
975
#endif
976

977
#ifdef PNG_READ_pCAL_SUPPORTED
978
      else if (!png_memcmp(chunk_name, png_pCAL, 4))
A
Andreas Dilger 已提交
979 980
         png_handle_pCAL(png_ptr, info_ptr, length);
#endif
981

982
#ifdef PNG_READ_sCAL_SUPPORTED
983
      else if (!png_memcmp(chunk_name, png_sCAL, 4))
984 985
         png_handle_sCAL(png_ptr, info_ptr, length);
#endif
986

987
#ifdef PNG_READ_pHYs_SUPPORTED
988
      else if (!png_memcmp(chunk_name, png_pHYs, 4))
A
Andreas Dilger 已提交
989 990
         png_handle_pHYs(png_ptr, info_ptr, length);
#endif
991

992
#ifdef PNG_READ_sBIT_SUPPORTED
993
      else if (!png_memcmp(chunk_name, png_sBIT, 4))
A
Andreas Dilger 已提交
994
         png_handle_sBIT(png_ptr, info_ptr, length);
G
Guy Schalnat 已提交
995
#endif
996

997
#ifdef PNG_READ_sRGB_SUPPORTED
998
      else if (!png_memcmp(chunk_name, png_sRGB, 4))
999 1000
         png_handle_sRGB(png_ptr, info_ptr, length);
#endif
1001

1002
#ifdef PNG_READ_iCCP_SUPPORTED
1003
      else if (!png_memcmp(chunk_name, png_iCCP, 4))
1004 1005
         png_handle_iCCP(png_ptr, info_ptr, length);
#endif
1006

1007
#ifdef PNG_READ_sPLT_SUPPORTED
1008
      else if (!png_memcmp(chunk_name, png_sPLT, 4))
1009 1010
         png_handle_sPLT(png_ptr, info_ptr, length);
#endif
1011

1012
#ifdef PNG_READ_tEXt_SUPPORTED
1013
      else if (!png_memcmp(chunk_name, png_tEXt, 4))
A
Andreas Dilger 已提交
1014
         png_handle_tEXt(png_ptr, info_ptr, length);
G
Guy Schalnat 已提交
1015
#endif
1016

1017
#ifdef PNG_READ_tIME_SUPPORTED
1018
      else if (!png_memcmp(chunk_name, png_tIME, 4))
A
Andreas Dilger 已提交
1019 1020
         png_handle_tIME(png_ptr, info_ptr, length);
#endif
1021

1022
#ifdef PNG_READ_tRNS_SUPPORTED
1023
      else if (!png_memcmp(chunk_name, png_tRNS, 4))
A
Andreas Dilger 已提交
1024 1025
         png_handle_tRNS(png_ptr, info_ptr, length);
#endif
1026

1027
#ifdef PNG_READ_zTXt_SUPPORTED
1028
      else if (!png_memcmp(chunk_name, png_zTXt, 4))
A
Andreas Dilger 已提交
1029
         png_handle_zTXt(png_ptr, info_ptr, length);
1030
#endif
1031

1032
#ifdef PNG_READ_iTXt_SUPPORTED
1033
      else if (!png_memcmp(chunk_name, png_iTXt, 4))
1034
         png_handle_iTXt(png_ptr, info_ptr, length);
G
Guy Schalnat 已提交
1035
#endif
1036

G
Guy Schalnat 已提交
1037
      else
A
Andreas Dilger 已提交
1038 1039
         png_handle_unknown(png_ptr, info_ptr, length);
   } while (!(png_ptr->mode & PNG_HAVE_IEND));
G
Guy Schalnat 已提交
1040
}
1041
#endif /* PNG_SEQUENTIAL_READ_SUPPORTED */
G
Guy Schalnat 已提交
1042

1043
/* Free all memory used by the read */
1044
void PNGAPI
G
Guy Schalnat 已提交
1045
png_destroy_read_struct(png_structpp png_ptr_ptr, png_infopp info_ptr_ptr,
1046
    png_infopp end_info_ptr_ptr)
G
Guy Schalnat 已提交
1047 1048
{
   png_structp png_ptr = NULL;
A
Andreas Dilger 已提交
1049
   png_infop info_ptr = NULL, end_info_ptr = NULL;
1050
#ifdef PNG_USER_MEM_SUPPORTED
1051 1052
   png_free_ptr free_fn = NULL;
   png_voidp mem_ptr = NULL;
1053
#endif
G
Guy Schalnat 已提交
1054

1055
   png_debug(1, "in png_destroy_read_struct");
1056

A
Andreas Dilger 已提交
1057
   if (png_ptr_ptr != NULL)
G
Guy Schalnat 已提交
1058
      png_ptr = *png_ptr_ptr;
1059 1060 1061 1062 1063 1064 1065
   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 已提交
1066

A
Andreas Dilger 已提交
1067
   if (info_ptr_ptr != NULL)
G
Guy Schalnat 已提交
1068 1069
      info_ptr = *info_ptr_ptr;

A
Andreas Dilger 已提交
1070
   if (end_info_ptr_ptr != NULL)
A
Andreas Dilger 已提交
1071
      end_info_ptr = *end_info_ptr_ptr;
G
Guy Schalnat 已提交
1072

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

A
Andreas Dilger 已提交
1075
   if (info_ptr != NULL)
G
Guy Schalnat 已提交
1076
   {
1077
#ifdef PNG_TEXT_SUPPORTED
1078
      png_free_data(png_ptr, info_ptr, PNG_FREE_TEXT, -1);
1079
#endif
1080 1081

#ifdef PNG_USER_MEM_SUPPORTED
1082 1083
      png_destroy_struct_2((png_voidp)info_ptr, (png_free_ptr)free_fn,
          (png_voidp)mem_ptr);
1084
#else
A
Andreas Dilger 已提交
1085
      png_destroy_struct((png_voidp)info_ptr);
1086
#endif
1087
      *info_ptr_ptr = NULL;
G
Guy Schalnat 已提交
1088 1089
   }

A
Andreas Dilger 已提交
1090
   if (end_info_ptr != NULL)
G
Guy Schalnat 已提交
1091
   {
1092
#ifdef PNG_READ_TEXT_SUPPORTED
1093
      png_free_data(png_ptr, end_info_ptr, PNG_FREE_TEXT, -1);
1094
#endif
1095
#ifdef PNG_USER_MEM_SUPPORTED
1096
      png_destroy_struct_2((png_voidp)end_info_ptr, (png_free_ptr)free_fn,
1097
          (png_voidp)mem_ptr);
1098
#else
A
Andreas Dilger 已提交
1099
      png_destroy_struct((png_voidp)end_info_ptr);
1100
#endif
1101
      *end_info_ptr_ptr = NULL;
G
Guy Schalnat 已提交
1102 1103
   }

A
Andreas Dilger 已提交
1104
   if (png_ptr != NULL)
G
Guy Schalnat 已提交
1105
   {
1106
#ifdef PNG_USER_MEM_SUPPORTED
1107 1108
      png_destroy_struct_2((png_voidp)png_ptr, (png_free_ptr)free_fn,
          (png_voidp)mem_ptr);
1109
#else
A
Andreas Dilger 已提交
1110
      png_destroy_struct((png_voidp)png_ptr);
1111
#endif
1112
      *png_ptr_ptr = NULL;
G
Guy Schalnat 已提交
1113 1114 1115
   }
}

1116
/* Free all memory used by the read (old method) */
1117
void /* PRIVATE */
1118 1119
png_read_destroy(png_structp png_ptr, png_infop info_ptr,
    png_infop end_info_ptr)
G
Guy Schalnat 已提交
1120
{
1121
#ifdef PNG_SETJMP_SUPPORTED
G
Guy Schalnat 已提交
1122
   jmp_buf tmp_jmp;
1123
#endif
G
Guy Schalnat 已提交
1124
   png_error_ptr error_fn;
1125
#ifdef PNG_WARNINGS_SUPPORTED
G
Guy Schalnat 已提交
1126
   png_error_ptr warning_fn;
1127
#endif
G
Guy Schalnat 已提交
1128
   png_voidp error_ptr;
1129 1130 1131
#ifdef PNG_USER_MEM_SUPPORTED
   png_free_ptr free_fn;
#endif
G
Guy Schalnat 已提交
1132

1133
   png_debug(1, "in png_read_destroy");
1134

A
Andreas Dilger 已提交
1135
   if (info_ptr != NULL)
A
Andreas Dilger 已提交
1136
      png_info_destroy(png_ptr, info_ptr);
G
Guy Schalnat 已提交
1137

A
Andreas Dilger 已提交
1138
   if (end_info_ptr != NULL)
A
Andreas Dilger 已提交
1139
      png_info_destroy(png_ptr, end_info_ptr);
G
Guy Schalnat 已提交
1140

A
Andreas Dilger 已提交
1141
   png_free(png_ptr, png_ptr->zbuf);
1142
   png_free(png_ptr, png_ptr->big_row_buf);
A
Andreas Dilger 已提交
1143
   png_free(png_ptr, png_ptr->prev_row);
1144
   png_free(png_ptr, png_ptr->chunkdata);
1145

1146
#ifdef PNG_READ_QUANTIZE_SUPPORTED
A
Andreas Dilger 已提交
1147
   png_free(png_ptr, png_ptr->palette_lookup);
1148
   png_free(png_ptr, png_ptr->quantize_index);
G
Guy Schalnat 已提交
1149
#endif
1150

1151
#ifdef PNG_READ_GAMMA_SUPPORTED
A
Andreas Dilger 已提交
1152
   png_free(png_ptr, png_ptr->gamma_table);
G
Guy Schalnat 已提交
1153
#endif
1154

1155
#ifdef PNG_READ_BACKGROUND_SUPPORTED
A
Andreas Dilger 已提交
1156 1157
   png_free(png_ptr, png_ptr->gamma_from_1);
   png_free(png_ptr, png_ptr->gamma_to_1);
G
Guy Schalnat 已提交
1158
#endif
1159

1160
   if (png_ptr->free_me & PNG_FREE_PLTE)
1161
      png_zfree(png_ptr, png_ptr->palette);
1162
   png_ptr->free_me &= ~PNG_FREE_PLTE;
1163

1164
#if defined(PNG_tRNS_SUPPORTED) || \
1165
    defined(PNG_READ_EXPAND_SUPPORTED) || defined(PNG_READ_BACKGROUND_SUPPORTED)
1166
   if (png_ptr->free_me & PNG_FREE_TRNS)
1167
      png_free(png_ptr, png_ptr->trans_alpha);
1168
   png_ptr->free_me &= ~PNG_FREE_TRNS;
1169
#endif
1170

1171
#ifdef PNG_READ_hIST_SUPPORTED
1172
   if (png_ptr->free_me & PNG_FREE_HIST)
A
Andreas Dilger 已提交
1173
      png_free(png_ptr, png_ptr->hist);
1174
   png_ptr->free_me &= ~PNG_FREE_HIST;
G
Guy Schalnat 已提交
1175
#endif
1176

1177
#ifdef PNG_READ_GAMMA_SUPPORTED
A
Andreas Dilger 已提交
1178
   if (png_ptr->gamma_16_table != NULL)
G
Guy Schalnat 已提交
1179
   {
1180 1181
      int i;
      int istop = (1 << (8 - png_ptr->gamma_shift));
1182
      for (i = 0; i < istop; i++)
G
Guy Schalnat 已提交
1183
      {
A
Andreas Dilger 已提交
1184
         png_free(png_ptr, png_ptr->gamma_16_table[i]);
G
Guy Schalnat 已提交
1185
      }
1186
   png_free(png_ptr, png_ptr->gamma_16_table);
G
Guy Schalnat 已提交
1187
   }
1188

1189
#ifdef PNG_READ_BACKGROUND_SUPPORTED
A
Andreas Dilger 已提交
1190
   if (png_ptr->gamma_16_from_1 != NULL)
G
Guy Schalnat 已提交
1191
   {
1192 1193
      int i;
      int istop = (1 << (8 - png_ptr->gamma_shift));
1194
      for (i = 0; i < istop; i++)
G
Guy Schalnat 已提交
1195
      {
A
Andreas Dilger 已提交
1196
         png_free(png_ptr, png_ptr->gamma_16_from_1[i]);
G
Guy Schalnat 已提交
1197
      }
A
Andreas Dilger 已提交
1198
   png_free(png_ptr, png_ptr->gamma_16_from_1);
1199
   }
A
Andreas Dilger 已提交
1200
   if (png_ptr->gamma_16_to_1 != NULL)
G
Guy Schalnat 已提交
1201
   {
1202 1203
      int i;
      int istop = (1 << (8 - png_ptr->gamma_shift));
1204
      for (i = 0; i < istop; i++)
G
Guy Schalnat 已提交
1205
      {
A
Andreas Dilger 已提交
1206
         png_free(png_ptr, png_ptr->gamma_16_to_1[i]);
G
Guy Schalnat 已提交
1207
      }
A
Andreas Dilger 已提交
1208
   png_free(png_ptr, png_ptr->gamma_16_to_1);
1209
   }
G
Guy Schalnat 已提交
1210
#endif
1211
#endif
1212

A
Andreas Dilger 已提交
1213
   inflateEnd(&png_ptr->zstream);
1214

G
Guy Schalnat 已提交
1215
#ifdef PNG_PROGRESSIVE_READ_SUPPORTED
A
Andreas Dilger 已提交
1216
   png_free(png_ptr, png_ptr->save_buffer);
G
Guy Schalnat 已提交
1217
#endif
G
Guy Schalnat 已提交
1218

1219 1220 1221 1222 1223 1224
#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 已提交
1225 1226 1227
   /* Save the important info out of the png_struct, in case it is
    * being used again.
    */
1228
#ifdef PNG_SETJMP_SUPPORTED
1229
   png_memcpy(tmp_jmp, png_ptr->longjmp_buffer, png_sizeof(jmp_buf));
1230
#endif
G
Guy Schalnat 已提交
1231 1232

   error_fn = png_ptr->error_fn;
1233
#ifdef PNG_WARNINGS_SUPPORTED
G
Guy Schalnat 已提交
1234
   warning_fn = png_ptr->warning_fn;
1235
#endif
G
Guy Schalnat 已提交
1236
   error_ptr = png_ptr->error_ptr;
1237 1238 1239
#ifdef PNG_USER_MEM_SUPPORTED
   free_fn = png_ptr->free_fn;
#endif
G
Guy Schalnat 已提交
1240

1241
   png_memset(png_ptr, 0, png_sizeof(png_struct));
G
Guy Schalnat 已提交
1242 1243

   png_ptr->error_fn = error_fn;
1244
#ifdef PNG_WARNINGS_SUPPORTED
G
Guy Schalnat 已提交
1245
   png_ptr->warning_fn = warning_fn;
1246
#endif
G
Guy Schalnat 已提交
1247
   png_ptr->error_ptr = error_ptr;
1248 1249 1250
#ifdef PNG_USER_MEM_SUPPORTED
   png_ptr->free_fn = free_fn;
#endif
G
Guy Schalnat 已提交
1251

1252
#ifdef PNG_SETJMP_SUPPORTED
1253
   png_memcpy(png_ptr->longjmp_buffer, tmp_jmp, png_sizeof(jmp_buf));
1254 1255
#endif

G
Guy Schalnat 已提交
1256
}
1257

1258
void PNGAPI
1259 1260
png_set_read_status_fn(png_structp png_ptr, png_read_status_ptr read_row_fn)
{
1261 1262
   if (png_ptr == NULL)
      return;
1263

1264 1265
   png_ptr->read_row_fn = read_row_fn;
}
1266

1267

1268
#ifdef PNG_SEQUENTIAL_READ_SUPPORTED
1269
#ifdef PNG_INFO_IMAGE_SUPPORTED
1270 1271
void PNGAPI
png_read_png(png_structp png_ptr, png_infop info_ptr,
1272 1273 1274 1275 1276
                           int transforms,
                           voidp params)
{
   int row;

1277
   if (png_ptr == NULL || info_ptr == NULL)
1278
      return;
1279

1280
   /* png_read_info() gives us all of the information from the
1281 1282 1283
    * PNG file before the first IDAT (image data chunk).
    */
   png_read_info(png_ptr, info_ptr);
1284 1285
   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()");
1286 1287 1288

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

1289
#ifdef PNG_READ_16_TO_8_SUPPORTED
1290
   /* Tell libpng to strip 16 bit/color files down to 8 bits per color.
1291
    */
1292
   if (transforms & PNG_TRANSFORM_STRIP_16)
1293
      png_set_strip_16(png_ptr);
1294 1295
#endif

1296
#ifdef PNG_READ_STRIP_ALPHA_SUPPORTED
1297 1298
   /* Strip alpha bytes from the input data without combining with
    * the background (not recommended).
1299 1300
    */
   if (transforms & PNG_TRANSFORM_STRIP_ALPHA)
1301
      png_set_strip_alpha(png_ptr);
1302 1303
#endif

1304
#if defined(PNG_READ_PACK_SUPPORTED) && !defined(PNG_READ_EXPAND_SUPPORTED)
1305
   /* Extract multiple pixels with bit depths of 1, 2, or 4 from a single
1306 1307 1308
    * byte into separate bytes (useful for paletted and grayscale images).
    */
   if (transforms & PNG_TRANSFORM_PACKING)
1309
      png_set_packing(png_ptr);
1310 1311
#endif

1312
#ifdef PNG_READ_PACKSWAP_SUPPORTED
1313
   /* Change the order of packed pixels to least significant bit first
1314 1315
    * (not useful if you are using png_set_packing).
    */
1316
   if (transforms & PNG_TRANSFORM_PACKSWAP)
1317
      png_set_packswap(png_ptr);
1318 1319
#endif

1320
#ifdef PNG_READ_EXPAND_SUPPORTED
1321 1322 1323 1324 1325 1326
   /* 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)
1327 1328 1329
      if ((png_ptr->bit_depth < 8) ||
          (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE) ||
          (png_get_valid(png_ptr, info_ptr, PNG_INFO_tRNS)))
1330
         png_set_expand(png_ptr);
1331 1332
#endif

1333
   /* We don't handle background color or gamma transformation or quantizing.
1334
    */
1335

1336
#ifdef PNG_READ_INVERT_SUPPORTED
1337
   /* Invert monochrome files to have 0 as white and 1 as black
1338
    */
1339
   if (transforms & PNG_TRANSFORM_INVERT_MONO)
1340
      png_set_invert_mono(png_ptr);
1341 1342
#endif

1343
#ifdef PNG_READ_SHIFT_SUPPORTED
1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357
   /* 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

1358
#ifdef PNG_READ_BGR_SUPPORTED
1359
   /* Flip the RGB pixels to BGR (or RGBA to BGRA) */
1360
   if (transforms & PNG_TRANSFORM_BGR)
1361
      png_set_bgr(png_ptr);
1362 1363
#endif

1364
#ifdef PNG_READ_SWAP_ALPHA_SUPPORTED
1365
   /* Swap the RGBA or GA data to ARGB or AG (or BGRA to ABGR) */
1366
   if (transforms & PNG_TRANSFORM_SWAP_ALPHA)
1367
      png_set_swap_alpha(png_ptr);
1368 1369
#endif

1370
#ifdef PNG_READ_SWAP_SUPPORTED
1371
   /* Swap bytes of 16 bit files to least significant byte first */
1372
   if (transforms & PNG_TRANSFORM_SWAP_ENDIAN)
1373
      png_set_swap(png_ptr);
1374 1375
#endif

1376
/* Added at libpng-1.2.41 */
1377
#ifdef PNG_READ_INVERT_ALPHA_SUPPORTED
1378
   /* Invert the alpha channel from opacity to transparency */
1379
   if (transforms & PNG_TRANSFORM_INVERT_ALPHA)
1380
      png_set_invert_alpha(png_ptr);
1381 1382
#endif

1383
/* Added at libpng-1.2.41 */
1384
#ifdef PNG_READ_GRAY_TO_RGB_SUPPORTED
1385
   /* Expand grayscale image to RGB */
1386
   if (transforms & PNG_TRANSFORM_GRAY_TO_RGB)
1387
      png_set_gray_to_rgb(png_ptr);
1388 1389
#endif

1390 1391 1392 1393 1394 1395
/* Added at libpng-1.5.3 */
#ifdef PNG_READ_EXPAND_16_SUPPORTED
   if (transforms & PNG_TRANSFORM_EXPAND_16)
      png_set_expand_16(png_ptr);
#endif

1396 1397
   /* We don't handle adding filler bytes */

1398 1399 1400 1401 1402
   /* We use png_read_image and rely on that for interlace handling, but we also
    * call png_read_update_info therefore must turn on interlace handling now:
    */
   (void)png_set_interlace_handling(png_ptr);

1403 1404
   /* Optional call to gamma correct and add the background to the palette
    * and update info structure.  REQUIRED if you are expecting libpng to
1405
    * update the palette for you (i.e., you selected such a transform above).
1406 1407 1408 1409 1410
    */
   png_read_update_info(png_ptr, info_ptr);

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

1411
   png_free_data(png_ptr, info_ptr, PNG_FREE_ROWS, 0);
1412
   if (info_ptr->row_pointers == NULL)
1413
   {
1414
      png_uint_32 iptr;
1415

1416
      info_ptr->row_pointers = (png_bytepp)png_malloc(png_ptr,
1417
          info_ptr->height * png_sizeof(png_bytep));
1418 1419 1420
      for (iptr=0; iptr<info_ptr->height; iptr++)
         info_ptr->row_pointers[iptr] = NULL;

1421
      info_ptr->free_me |= PNG_FREE_ROWS;
1422

1423
      for (row = 0; row < (int)info_ptr->height; row++)
1424
         info_ptr->row_pointers[row] = (png_bytep)png_malloc(png_ptr,
1425
            png_get_rowbytes(png_ptr, info_ptr));
1426
   }
1427 1428 1429 1430

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

1431
   /* Read rest of file, and get additional chunks in info_ptr - REQUIRED */
1432
   png_read_end(png_ptr, info_ptr);
1433

1434 1435
   PNG_UNUSED(transforms)   /* Quiet compiler warnings */
   PNG_UNUSED(params)
1436

1437
}
1438
#endif /* PNG_INFO_IMAGE_SUPPORTED */
1439
#endif /* PNG_SEQUENTIAL_READ_SUPPORTED */
1440
#endif /* PNG_READ_SUPPORTED */