pngread.c 42.9 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 May 15, 2007
5
 * For conditions of distribution and use, see copyright notice in png.h
6
 * Copyright (c) 1998-2007 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
#include "pngpriv.h"
G
Guy Schalnat 已提交
16

17 18
#if defined(PNG_READ_SUPPORTED)

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 */

G
Guy Schalnat 已提交
38
   png_structp png_ptr;
39 40

#ifdef PNG_SETJMP_SUPPORTED
A
Andreas Dilger 已提交
41 42 43
#ifdef USE_FAR_KEYWORD
   jmp_buf jmpbuf;
#endif
44 45
#endif

46 47
   int i;

A
Andreas Dilger 已提交
48
   png_debug(1, "in png_create_read_struct\n");
49
#ifdef PNG_USER_MEM_SUPPORTED
50
   png_ptr = (png_structp)png_create_struct_2(PNG_STRUCT_PNG,
51
      malloc_fn, mem_ptr);
52
#else
53
   png_ptr = (png_structp)png_create_struct(PNG_STRUCT_PNG);
54
#endif
55
   if (png_ptr == NULL)
56
      return (NULL);
57

58
#ifdef PNG_MMX_CODE_SUPPORTED
59 60 61
   png_init_mmx_flags(png_ptr);   /* 1.2.0 addition */
#endif

62 63 64 65 66 67
   /* added at libpng-1.2.6 */
#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
#ifdef PNG_SETJMP_SUPPORTED
A
Andreas Dilger 已提交
69 70 71
#ifdef USE_FAR_KEYWORD
   if (setjmp(jmpbuf))
#else
G
Guy Schalnat 已提交
72
   if (setjmp(png_ptr->jmpbuf))
A
Andreas Dilger 已提交
73
#endif
G
Guy Schalnat 已提交
74
   {
A
Andreas Dilger 已提交
75
      png_free(png_ptr, png_ptr->zbuf);
76
      png_ptr->zbuf=NULL;
77
#ifdef PNG_USER_MEM_SUPPORTED
78
      png_destroy_struct_2((png_voidp)png_ptr,
79
         (png_free_ptr)free_fn, (png_voidp)mem_ptr);
80 81 82
#else
      png_destroy_struct((png_voidp)png_ptr);
#endif
83
      return (NULL);
G
Guy Schalnat 已提交
84
   }
A
Andreas Dilger 已提交
85
#ifdef USE_FAR_KEYWORD
86
   png_memcpy(png_ptr->jmpbuf, jmpbuf, sizeof(jmp_buf));
A
Andreas Dilger 已提交
87
#endif
88
#endif
89 90 91

#ifdef PNG_USER_MEM_SUPPORTED
   png_set_mem_fn(png_ptr, mem_ptr, malloc_fn, free_fn);
92
#endif
93

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

96 97
   i=0;
   do
G
Guy Schalnat 已提交
98
   {
99 100 101
     if(user_png_ver[i] != png_libpng_ver[i])
        png_ptr->flags |= PNG_FLAG_LIBRARY_MISMATCH;
   } while (png_libpng_ver[i++]);
102

103
   if (png_ptr->flags & PNG_FLAG_LIBRARY_MISMATCH)
104
   {
105 106 107 108 109 110
     /* 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] ||
111
         (user_png_ver[0] == '1' && user_png_ver[2] != png_libpng_ver[2]) ||
112 113
         (user_png_ver[0] == '0' && user_png_ver[2] < '9'))
     {
114
#ifndef PNG_NO_STDIO
115 116 117
        char msg[80];
        if (user_png_ver)
        {
118
          png_sprintf(msg, "Application was compiled with png.h from libpng-%.20s",
119 120 121
             user_png_ver);
          png_warning(png_ptr, msg);
        }
122
        png_sprintf(msg, "Application  is  running with png.c from libpng-%.20s",
123 124 125 126 127 128
           png_libpng_ver);
        png_warning(png_ptr, msg);
#endif
#ifdef PNG_ERROR_NUMBERS_SUPPORTED
        png_ptr->flags=0;
#endif
129 130 131
        png_error(png_ptr,
           "Incompatible libpng version in application and library");
     }
132 133
   }

G
Guy Schalnat 已提交
134 135
   /* initialize zbuf - compression buffer */
   png_ptr->zbuf_size = PNG_ZBUF_SIZE;
136
   png_ptr->zbuf = (png_bytep)png_malloc(png_ptr, png_ptr->zbuf_size);
A
Andreas Dilger 已提交
137 138 139
   png_ptr->zstream.zalloc = png_zalloc;
   png_ptr->zstream.zfree = png_zfree;
   png_ptr->zstream.opaque = (voidpf)png_ptr;
G
Guy Schalnat 已提交
140

A
Andreas Dilger 已提交
141
   switch (inflateInit(&png_ptr->zstream))
G
Guy Schalnat 已提交
142 143 144 145 146 147 148 149
   {
     case Z_OK: /* Do nothing */ break;
     case Z_MEM_ERROR:
     case Z_STREAM_ERROR: png_error(png_ptr, "zlib memory error"); break;
     case Z_VERSION_ERROR: png_error(png_ptr, "zlib version error"); break;
     default: png_error(png_ptr, "Unknown zlib error");
   }

A
Andreas Dilger 已提交
150 151
   png_ptr->zstream.next_out = png_ptr->zbuf;
   png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size;
G
Guy Schalnat 已提交
152

153
   png_set_read_fn(png_ptr, NULL, NULL);
G
Guy Schalnat 已提交
154

155 156 157 158 159 160 161
#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))
      PNG_ABORT();
162
   png_memcpy(png_ptr->jmpbuf, jmpbuf, sizeof(jmp_buf));
163 164 165 166 167
#else
   if (setjmp(png_ptr->jmpbuf))
      PNG_ABORT();
#endif
#endif
G
Guy Schalnat 已提交
168 169 170
   return (png_ptr);
}

171 172 173 174

void PNGAPI
png_read_init_3(png_structpp ptr_ptr, png_const_charp user_png_ver,
   png_size_t png_struct_size)
G
Guy Schalnat 已提交
175
{
176
#ifdef PNG_SETJMP_SUPPORTED
G
Guy Schalnat 已提交
177
   jmp_buf tmp_jmp;  /* to save current jump buffer */
178
#endif
G
Guy Schalnat 已提交
179

180
   int i=0;
181 182 183

   png_structp png_ptr=*ptr_ptr;

184 185
   if(png_ptr == NULL) return;

186 187
   do
   {
188
     if (user_png_ver[i] != png_libpng_ver[i])
189
     {
190 191 192
#ifdef PNG_LEGACY_SUPPORTED
       png_ptr->flags |= PNG_FLAG_LIBRARY_MISMATCH;
#else
193
       png_ptr->warning_fn=NULL;
194
       png_warning(png_ptr,
195
        "Application uses deprecated png_read_init() and should be recompiled");
196
       break;
197
#endif
198 199 200
     }
   } while (png_libpng_ver[i++]);

201
   png_debug(1, "in png_read_init_3\n");
202 203

#ifdef PNG_SETJMP_SUPPORTED
G
Guy Schalnat 已提交
204
   /* save jump buffer and error functions */
205
   png_memcpy(tmp_jmp, png_ptr->jmpbuf, sizeof(jmp_buf));
206
#endif
G
Guy Schalnat 已提交
207

208 209 210 211 212 213
   if (sizeof(png_struct) > png_struct_size)
   {
      png_destroy_struct(png_ptr);
      *ptr_ptr = (png_structp)png_create_struct(PNG_STRUCT_PNG);
      png_ptr = *ptr_ptr;
   }
214

G
Guy Schalnat 已提交
215
   /* reset all variables to 0 */
216
   png_memset(png_ptr, 0, sizeof(png_struct));
G
Guy Schalnat 已提交
217

218
#ifdef PNG_SETJMP_SUPPORTED
G
Guy Schalnat 已提交
219
   /* restore jump buffer */
220
   png_memcpy(png_ptr->jmpbuf, tmp_jmp, sizeof(jmp_buf));
221
#endif
G
Guy Schalnat 已提交
222

223 224 225 226 227 228
   /* added at libpng-1.2.6 */
#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

G
Guy Schalnat 已提交
229
   /* initialize zbuf - compression buffer */
G
Guy Schalnat 已提交
230
   png_ptr->zbuf_size = PNG_ZBUF_SIZE;
231
   png_ptr->zbuf = (png_bytep)png_malloc(png_ptr, png_ptr->zbuf_size);
A
Andreas Dilger 已提交
232 233 234
   png_ptr->zstream.zalloc = png_zalloc;
   png_ptr->zstream.zfree = png_zfree;
   png_ptr->zstream.opaque = (voidpf)png_ptr;
G
Guy Schalnat 已提交
235

A
Andreas Dilger 已提交
236
   switch (inflateInit(&png_ptr->zstream))
G
Guy Schalnat 已提交
237 238 239 240 241 242 243 244
   {
     case Z_OK: /* Do nothing */ break;
     case Z_MEM_ERROR:
     case Z_STREAM_ERROR: png_error(png_ptr, "zlib memory"); break;
     case Z_VERSION_ERROR: png_error(png_ptr, "zlib version"); break;
     default: png_error(png_ptr, "Unknown zlib error");
   }

A
Andreas Dilger 已提交
245 246
   png_ptr->zstream.next_out = png_ptr->zbuf;
   png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size;
G
Guy Schalnat 已提交
247

248
   png_set_read_fn(png_ptr, NULL, NULL);
G
Guy Schalnat 已提交
249 250
}

251
#ifndef PNG_NO_SEQUENTIAL_READ_SUPPORTED
A
Andreas Dilger 已提交
252
/* Read the information before the actual image data.  This has been
253
 * changed in v0.90 to allow reading a file that already has the magic
A
Andreas Dilger 已提交
254
 * bytes read from the stream.  You can tell libpng how many bytes have
255
 * been read from the beginning of the stream (up to the maximum of 8)
A
Andreas Dilger 已提交
256 257 258 259
 * 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.
 */
260
void PNGAPI
A
Andreas Dilger 已提交
261
png_read_info(png_structp png_ptr, png_infop info_ptr)
G
Guy Schalnat 已提交
262
{
263
   if(png_ptr == NULL) return;
A
Andreas Dilger 已提交
264
   png_debug(1, "in png_read_info\n");
A
Andreas Dilger 已提交
265 266
   /* If we haven't checked all of the PNG signature bytes, do so now. */
   if (png_ptr->sig_bytes < 8)
G
Guy Schalnat 已提交
267
   {
A
Andreas Dilger 已提交
268 269
      png_size_t num_checked = png_ptr->sig_bytes,
                 num_to_check = 8 - num_checked;
A
Andreas Dilger 已提交
270

271 272 273
#ifdef PNG_IO_STATE_SUPPORTED
      png_ptr->io_state = PNG_IO_READING | PNG_IO_SIGNATURE;
#endif
A
Andreas Dilger 已提交
274
      png_read_data(png_ptr, &(info_ptr->signature[num_checked]), num_to_check);
A
Andreas Dilger 已提交
275 276 277 278 279 280 281 282 283 284
      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");
      }
285 286
      if (num_checked < 3)
         png_ptr->mode |= PNG_HAVE_PNG_SIGNATURE;
G
Guy Schalnat 已提交
287
   }
G
Guy Schalnat 已提交
288

289
   for(;;)
G
Guy Schalnat 已提交
290
   {
291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307
#ifdef PNG_USE_LOCAL_ARRAYS
      PNG_IHDR;
      PNG_IDAT;
      PNG_IEND;
      PNG_PLTE;
#if defined(PNG_READ_bKGD_SUPPORTED)
      PNG_bKGD;
#endif
#if defined(PNG_READ_cHRM_SUPPORTED)
      PNG_cHRM;
#endif
#if defined(PNG_READ_gAMA_SUPPORTED)
      PNG_gAMA;
#endif
#if defined(PNG_READ_hIST_SUPPORTED)
      PNG_hIST;
#endif
308 309 310 311 312 313
#if defined(PNG_READ_iCCP_SUPPORTED)
      PNG_iCCP;
#endif
#if defined(PNG_READ_iTXt_SUPPORTED)
      PNG_iTXt;
#endif
314 315 316 317 318 319 320 321 322 323 324 325
#if defined(PNG_READ_oFFs_SUPPORTED)
      PNG_oFFs;
#endif
#if defined(PNG_READ_pCAL_SUPPORTED)
      PNG_pCAL;
#endif
#if defined(PNG_READ_pHYs_SUPPORTED)
      PNG_pHYs;
#endif
#if defined(PNG_READ_sBIT_SUPPORTED)
      PNG_sBIT;
#endif
326
#if defined(PNG_READ_sCAL_SUPPORTED)
327 328 329 330 331
      PNG_sCAL;
#endif
#if defined(PNG_READ_sPLT_SUPPORTED)
      PNG_sPLT;
#endif
332 333 334 335 336 337 338 339 340 341 342 343 344 345 346
#if defined(PNG_READ_sRGB_SUPPORTED)
      PNG_sRGB;
#endif
#if defined(PNG_READ_tEXt_SUPPORTED)
      PNG_tEXt;
#endif
#if defined(PNG_READ_tIME_SUPPORTED)
      PNG_tIME;
#endif
#if defined(PNG_READ_tRNS_SUPPORTED)
      PNG_tRNS;
#endif
#if defined(PNG_READ_zTXt_SUPPORTED)
      PNG_zTXt;
#endif
347
#endif /* PNG_USE_LOCAL_ARRAYS */
348 349
      png_uint_32 length = png_read_chunk_header(png_ptr);
      png_bytep chunk_name = png_ptr->chunk_name;
A
Andreas Dilger 已提交
350 351 352 353

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

358
      if (!png_memcmp(chunk_name, png_IHDR, 4))
A
Andreas Dilger 已提交
359
         png_handle_IHDR(png_ptr, info_ptr, length);
360
      else if (!png_memcmp(chunk_name, png_IEND, 4))
A
Andreas Dilger 已提交
361
         png_handle_IEND(png_ptr, info_ptr, length);
362
#ifdef PNG_HANDLE_AS_UNKNOWN_SUPPORTED
363
      else if (png_handle_as_unknown(png_ptr, chunk_name))
364
      {
365
         if (!png_memcmp(chunk_name, png_IDAT, 4))
366 367
            png_ptr->mode |= PNG_HAVE_IDAT;
         png_handle_unknown(png_ptr, info_ptr, length);
368
         if (!png_memcmp(chunk_name, png_PLTE, 4))
369
            png_ptr->mode |= PNG_HAVE_PLTE;
370
         else if (!png_memcmp(chunk_name, png_IDAT, 4))
371 372 373 374 375 376 377 378 379 380
         {
            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
381
      else if (!png_memcmp(chunk_name, png_PLTE, 4))
382
         png_handle_PLTE(png_ptr, info_ptr, length);
383
      else if (!png_memcmp(chunk_name, png_IDAT, 4))
A
Andreas Dilger 已提交
384 385 386 387 388 389 390 391 392 393 394 395
      {
         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)
396
      else if (!png_memcmp(chunk_name, png_bKGD, 4))
A
Andreas Dilger 已提交
397
         png_handle_bKGD(png_ptr, info_ptr, length);
G
Guy Schalnat 已提交
398 399
#endif
#if defined(PNG_READ_cHRM_SUPPORTED)
400
      else if (!png_memcmp(chunk_name, png_cHRM, 4))
A
Andreas Dilger 已提交
401
         png_handle_cHRM(png_ptr, info_ptr, length);
G
Guy Schalnat 已提交
402
#endif
A
Andreas Dilger 已提交
403
#if defined(PNG_READ_gAMA_SUPPORTED)
404
      else if (!png_memcmp(chunk_name, png_gAMA, 4))
A
Andreas Dilger 已提交
405
         png_handle_gAMA(png_ptr, info_ptr, length);
G
Guy Schalnat 已提交
406 407
#endif
#if defined(PNG_READ_hIST_SUPPORTED)
408
      else if (!png_memcmp(chunk_name, png_hIST, 4))
A
Andreas Dilger 已提交
409
         png_handle_hIST(png_ptr, info_ptr, length);
G
Guy Schalnat 已提交
410
#endif
A
Andreas Dilger 已提交
411
#if defined(PNG_READ_oFFs_SUPPORTED)
412
      else if (!png_memcmp(chunk_name, png_oFFs, 4))
A
Andreas Dilger 已提交
413 414 415
         png_handle_oFFs(png_ptr, info_ptr, length);
#endif
#if defined(PNG_READ_pCAL_SUPPORTED)
416
      else if (!png_memcmp(chunk_name, png_pCAL, 4))
A
Andreas Dilger 已提交
417 418
         png_handle_pCAL(png_ptr, info_ptr, length);
#endif
419
#if defined(PNG_READ_sCAL_SUPPORTED)
420
      else if (!png_memcmp(chunk_name, png_sCAL, 4))
421 422
         png_handle_sCAL(png_ptr, info_ptr, length);
#endif
G
Guy Schalnat 已提交
423
#if defined(PNG_READ_pHYs_SUPPORTED)
424
      else if (!png_memcmp(chunk_name, png_pHYs, 4))
A
Andreas Dilger 已提交
425
         png_handle_pHYs(png_ptr, info_ptr, length);
G
Guy Schalnat 已提交
426
#endif
A
Andreas Dilger 已提交
427
#if defined(PNG_READ_sBIT_SUPPORTED)
428
      else if (!png_memcmp(chunk_name, png_sBIT, 4))
A
Andreas Dilger 已提交
429 430
         png_handle_sBIT(png_ptr, info_ptr, length);
#endif
431
#if defined(PNG_READ_sRGB_SUPPORTED)
432
      else if (!png_memcmp(chunk_name, png_sRGB, 4))
433 434
         png_handle_sRGB(png_ptr, info_ptr, length);
#endif
435
#if defined(PNG_READ_iCCP_SUPPORTED)
436
      else if (!png_memcmp(chunk_name, png_iCCP, 4))
437 438 439
         png_handle_iCCP(png_ptr, info_ptr, length);
#endif
#if defined(PNG_READ_sPLT_SUPPORTED)
440
      else if (!png_memcmp(chunk_name, png_sPLT, 4))
441 442
         png_handle_sPLT(png_ptr, info_ptr, length);
#endif
A
Andreas Dilger 已提交
443
#if defined(PNG_READ_tEXt_SUPPORTED)
444
      else if (!png_memcmp(chunk_name, png_tEXt, 4))
A
Andreas Dilger 已提交
445
         png_handle_tEXt(png_ptr, info_ptr, length);
G
Guy Schalnat 已提交
446 447
#endif
#if defined(PNG_READ_tIME_SUPPORTED)
448
      else if (!png_memcmp(chunk_name, png_tIME, 4))
A
Andreas Dilger 已提交
449
         png_handle_tIME(png_ptr, info_ptr, length);
G
Guy Schalnat 已提交
450
#endif
A
Andreas Dilger 已提交
451
#if defined(PNG_READ_tRNS_SUPPORTED)
452
      else if (!png_memcmp(chunk_name, png_tRNS, 4))
A
Andreas Dilger 已提交
453
         png_handle_tRNS(png_ptr, info_ptr, length);
G
Guy Schalnat 已提交
454 455
#endif
#if defined(PNG_READ_zTXt_SUPPORTED)
456
      else if (!png_memcmp(chunk_name, png_zTXt, 4))
A
Andreas Dilger 已提交
457
         png_handle_zTXt(png_ptr, info_ptr, length);
458 459
#endif
#if defined(PNG_READ_iTXt_SUPPORTED)
460
      else if (!png_memcmp(chunk_name, png_iTXt, 4))
461
         png_handle_iTXt(png_ptr, info_ptr, length);
G
Guy Schalnat 已提交
462
#endif
A
Andreas Dilger 已提交
463 464
      else
         png_handle_unknown(png_ptr, info_ptr, length);
G
Guy Schalnat 已提交
465 466
   }
}
467
#endif /* PNG_NO_SEQUENTIAL_READ_SUPPORTED */
G
Guy Schalnat 已提交
468

A
Andreas Dilger 已提交
469
/* optional call to update the users info_ptr structure */
470
void PNGAPI
A
Andreas Dilger 已提交
471
png_read_update_info(png_structp png_ptr, png_infop info_ptr)
G
Guy Schalnat 已提交
472
{
A
Andreas Dilger 已提交
473
   png_debug(1, "in png_read_update_info\n");
474
   if(png_ptr == NULL) return;
G
Guy Schalnat 已提交
475
   if (!(png_ptr->flags & PNG_FLAG_ROW_INIT))
G
Guy Schalnat 已提交
476
      png_read_start_row(png_ptr);
477 478 479
   else
      png_warning(png_ptr,
      "Ignoring extra png_read_update_info() call; row buffer not reallocated");
A
Andreas Dilger 已提交
480
   png_read_transform_info(png_ptr, info_ptr);
G
Guy Schalnat 已提交
481 482
}

483
#ifndef PNG_NO_SEQUENTIAL_READ_SUPPORTED
484 485
/* Initialize palette, background, etc, after transformations
 * are set, but before any reading takes place.  This allows
486
 * the user to obtain a gamma-corrected palette, for example.
487 488
 * If the user doesn't call this, we will do it ourselves.
 */
489
void PNGAPI
G
Guy Schalnat 已提交
490
png_start_read_image(png_structp png_ptr)
G
Guy Schalnat 已提交
491
{
A
Andreas Dilger 已提交
492
   png_debug(1, "in png_start_read_image\n");
493
   if(png_ptr == NULL) return;
G
Guy Schalnat 已提交
494
   if (!(png_ptr->flags & PNG_FLAG_ROW_INIT))
G
Guy Schalnat 已提交
495
      png_read_start_row(png_ptr);
G
Guy Schalnat 已提交
496
}
497
#endif /* PNG_NO_SEQUENTIAL_READ_SUPPORTED */
G
Guy Schalnat 已提交
498

499
#ifndef PNG_NO_SEQUENTIAL_READ_SUPPORTED
500
void PNGAPI
G
Guy Schalnat 已提交
501
png_read_row(png_structp png_ptr, png_bytep row, png_bytep dsp_row)
G
Guy Schalnat 已提交
502
{
503 504
#ifdef PNG_USE_LOCAL_ARRAYS
   PNG_IDAT;
505 506
   const int png_pass_dsp_mask[7] = {0xff, 0x0f, 0xff, 0x33, 0xff, 0x55, 0xff};
   const int png_pass_mask[7] = {0x80, 0x08, 0x88, 0x22, 0xaa, 0x55, 0xff};
507
#endif
G
Guy Schalnat 已提交
508
   int ret;
509
   if(png_ptr == NULL) return;
510
   png_debug2(1, "in png_read_row (row %lu, pass %d)\n",
511
      (unsigned long) png_ptr->row_number, png_ptr->pass);
G
Guy Schalnat 已提交
512
   if (!(png_ptr->flags & PNG_FLAG_ROW_INIT))
G
Guy Schalnat 已提交
513
      png_read_start_row(png_ptr);
514 515 516 517 518
   if (png_ptr->row_number == 0 && png_ptr->pass == 0)
   {
   /* check for transforms that have been set but were defined out */
#if defined(PNG_WRITE_INVERT_SUPPORTED) && !defined(PNG_READ_INVERT_SUPPORTED)
   if (png_ptr->transformations & PNG_INVERT_MONO)
519
      png_warning(png_ptr, "PNG_READ_INVERT_SUPPORTED is not defined");
520 521 522
#endif
#if defined(PNG_WRITE_FILLER_SUPPORTED) && !defined(PNG_READ_FILLER_SUPPORTED)
   if (png_ptr->transformations & PNG_FILLER)
523
      png_warning(png_ptr, "PNG_READ_FILLER_SUPPORTED is not defined");
524 525 526
#endif
#if defined(PNG_WRITE_PACKSWAP_SUPPORTED) && !defined(PNG_READ_PACKSWAP_SUPPORTED)
   if (png_ptr->transformations & PNG_PACKSWAP)
527
      png_warning(png_ptr, "PNG_READ_PACKSWAP_SUPPORTED is not defined");
528 529 530
#endif
#if defined(PNG_WRITE_PACK_SUPPORTED) && !defined(PNG_READ_PACK_SUPPORTED)
   if (png_ptr->transformations & PNG_PACK)
531
      png_warning(png_ptr, "PNG_READ_PACK_SUPPORTED is not defined");
532 533 534
#endif
#if defined(PNG_WRITE_SHIFT_SUPPORTED) && !defined(PNG_READ_SHIFT_SUPPORTED)
   if (png_ptr->transformations & PNG_SHIFT)
535
      png_warning(png_ptr, "PNG_READ_SHIFT_SUPPORTED is not defined");
536 537 538
#endif
#if defined(PNG_WRITE_BGR_SUPPORTED) && !defined(PNG_READ_BGR_SUPPORTED)
   if (png_ptr->transformations & PNG_BGR)
539
      png_warning(png_ptr, "PNG_READ_BGR_SUPPORTED is not defined");
540 541 542
#endif
#if defined(PNG_WRITE_SWAP_SUPPORTED) && !defined(PNG_READ_SWAP_SUPPORTED)
   if (png_ptr->transformations & PNG_SWAP_BYTES)
543
      png_warning(png_ptr, "PNG_READ_SWAP_SUPPORTED is not defined");
544 545
#endif
   }
G
Guy Schalnat 已提交
546

G
Guy Schalnat 已提交
547
#if defined(PNG_READ_INTERLACING_SUPPORTED)
G
Guy Schalnat 已提交
548 549 550 551 552 553
   /* if interlaced and we do not need a new row, combine row and return */
   if (png_ptr->interlaced && (png_ptr->transformations & PNG_INTERLACE))
   {
      switch (png_ptr->pass)
      {
         case 0:
554
            if (png_ptr->row_number & 0x07)
G
Guy Schalnat 已提交
555
            {
A
Andreas Dilger 已提交
556
               if (dsp_row != NULL)
G
Guy Schalnat 已提交
557 558
                  png_combine_row(png_ptr, dsp_row,
                     png_pass_dsp_mask[png_ptr->pass]);
G
Guy Schalnat 已提交
559
               png_read_finish_row(png_ptr);
G
Guy Schalnat 已提交
560 561 562 563
               return;
            }
            break;
         case 1:
564
            if ((png_ptr->row_number & 0x07) || png_ptr->width < 5)
G
Guy Schalnat 已提交
565
            {
A
Andreas Dilger 已提交
566
               if (dsp_row != NULL)
G
Guy Schalnat 已提交
567 568 569 570 571 572 573
                  png_combine_row(png_ptr, dsp_row,
                     png_pass_dsp_mask[png_ptr->pass]);
               png_read_finish_row(png_ptr);
               return;
            }
            break;
         case 2:
574
            if ((png_ptr->row_number & 0x07) != 4)
G
Guy Schalnat 已提交
575
            {
A
Andreas Dilger 已提交
576
               if (dsp_row != NULL && (png_ptr->row_number & 4))
G
Guy Schalnat 已提交
577
                  png_combine_row(png_ptr, dsp_row,
G
Guy Schalnat 已提交
578 579 580 581 582 583 584 585
                     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 已提交
586
               if (dsp_row != NULL)
G
Guy Schalnat 已提交
587 588 589 590 591 592 593 594
                  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 已提交
595
            {
A
Andreas Dilger 已提交
596
               if (dsp_row != NULL && (png_ptr->row_number & 2))
G
Guy Schalnat 已提交
597 598 599 600 601 602 603 604 605
                  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 已提交
606
               if (dsp_row != NULL)
G
Guy Schalnat 已提交
607 608 609 610 611 612
                  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 已提交
613
         case 6:
G
Guy Schalnat 已提交
614 615 616 617 618 619 620 621
            if (!(png_ptr->row_number & 1))
            {
               png_read_finish_row(png_ptr);
               return;
            }
            break;
      }
   }
G
Guy Schalnat 已提交
622
#endif
G
Guy Schalnat 已提交
623

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

A
Andreas Dilger 已提交
627 628
   png_ptr->zstream.next_out = png_ptr->row_buf;
   png_ptr->zstream.avail_out = (uInt)png_ptr->irowbytes;
G
Guy Schalnat 已提交
629 630
   do
   {
A
Andreas Dilger 已提交
631
      if (!(png_ptr->zstream.avail_in))
G
Guy Schalnat 已提交
632 633 634
      {
         while (!png_ptr->idat_size)
         {
A
Andreas Dilger 已提交
635
            png_crc_finish(png_ptr, 0);
G
Guy Schalnat 已提交
636

637
            png_ptr->idat_size = png_read_chunk_header(png_ptr);
A
Andreas Dilger 已提交
638 639
            if (png_memcmp(png_ptr->chunk_name, png_IDAT, 4))
               png_error(png_ptr, "Not enough image data");
G
Guy Schalnat 已提交
640
         }
A
Andreas Dilger 已提交
641 642
         png_ptr->zstream.avail_in = (uInt)png_ptr->zbuf_size;
         png_ptr->zstream.next_in = png_ptr->zbuf;
G
Guy Schalnat 已提交
643
         if (png_ptr->zbuf_size > png_ptr->idat_size)
A
Andreas Dilger 已提交
644
            png_ptr->zstream.avail_in = (uInt)png_ptr->idat_size;
A
Andreas Dilger 已提交
645 646
         png_crc_read(png_ptr, png_ptr->zbuf,
            (png_size_t)png_ptr->zstream.avail_in);
A
Andreas Dilger 已提交
647
         png_ptr->idat_size -= png_ptr->zstream.avail_in;
G
Guy Schalnat 已提交
648
      }
A
Andreas Dilger 已提交
649
      ret = inflate(&png_ptr->zstream, Z_PARTIAL_FLUSH);
G
Guy Schalnat 已提交
650 651
      if (ret == Z_STREAM_END)
      {
A
Andreas Dilger 已提交
652
         if (png_ptr->zstream.avail_out || png_ptr->zstream.avail_in ||
G
Guy Schalnat 已提交
653
            png_ptr->idat_size)
654
            png_benign_error(png_ptr, "Extra compressed data");
A
Andreas Dilger 已提交
655
         png_ptr->mode |= PNG_AFTER_IDAT;
G
Guy Schalnat 已提交
656
         png_ptr->flags |= PNG_FLAG_ZLIB_FINISHED;
G
Guy Schalnat 已提交
657
         break;
G
Guy Schalnat 已提交
658 659
      }
      if (ret != Z_OK)
A
Andreas Dilger 已提交
660
         png_error(png_ptr, png_ptr->zstream.msg ? png_ptr->zstream.msg :
G
Guy Schalnat 已提交
661
                   "Decompression error");
A
Andreas Dilger 已提交
662 663

   } while (png_ptr->zstream.avail_out);
G
Guy Schalnat 已提交
664 665 666 667 668 669

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

673
   if(png_ptr->row_buf[0])
G
Guy Schalnat 已提交
674 675 676
   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 已提交
677

678
   png_memcpy(png_ptr->prev_row, png_ptr->row_buf, png_ptr->rowbytes + 1);
679

680 681 682 683 684 685 686 687
#if defined(PNG_MNG_FEATURES_SUPPORTED)
   if((png_ptr->mng_features_permitted & PNG_FLAG_MNG_FILTER_64) &&
      (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 已提交
688

689
   if (png_ptr->transformations || (png_ptr->flags&PNG_FLAG_STRIP_ALPHA))
G
Guy Schalnat 已提交
690 691
      png_do_read_transformations(png_ptr);

G
Guy Schalnat 已提交
692
#if defined(PNG_READ_INTERLACING_SUPPORTED)
G
Guy Schalnat 已提交
693 694 695 696 697
   /* blow up interlaced rows to full size */
   if (png_ptr->interlaced &&
      (png_ptr->transformations & PNG_INTERLACE))
   {
      if (png_ptr->pass < 6)
698
/*       old interface (pre-1.0.9):
G
Guy Schalnat 已提交
699
         png_do_read_interlace(&(png_ptr->row_info),
A
Andreas Dilger 已提交
700
            png_ptr->row_buf + 1, png_ptr->pass, png_ptr->transformations);
701 702
 */
         png_do_read_interlace(png_ptr);
G
Guy Schalnat 已提交
703

A
Andreas Dilger 已提交
704
      if (dsp_row != NULL)
G
Guy Schalnat 已提交
705 706
         png_combine_row(png_ptr, dsp_row,
            png_pass_dsp_mask[png_ptr->pass]);
A
Andreas Dilger 已提交
707
      if (row != NULL)
G
Guy Schalnat 已提交
708 709 710 711
         png_combine_row(png_ptr, row,
            png_pass_mask[png_ptr->pass]);
   }
   else
G
Guy Schalnat 已提交
712
#endif
G
Guy Schalnat 已提交
713
   {
A
Andreas Dilger 已提交
714
      if (row != NULL)
G
Guy Schalnat 已提交
715
         png_combine_row(png_ptr, row, 0xff);
A
Andreas Dilger 已提交
716
      if (dsp_row != NULL)
G
Guy Schalnat 已提交
717 718 719
         png_combine_row(png_ptr, dsp_row, 0xff);
   }
   png_read_finish_row(png_ptr);
720 721 722

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

726
#ifndef PNG_NO_SEQUENTIAL_READ_SUPPORTED
A
Andreas Dilger 已提交
727
/* Read one or more rows of image data.  If the image is interlaced,
728 729
 * and png_set_interlace_handling() has been called, the rows need to
 * contain the contents of the rows from the previous pass.  If the
730
 * image has alpha or transparency, and png_handle_alpha()[*] has been
731 732
 * called, the rows contents must be initialized to the contents of the
 * screen.
733
 *
734 735 736 737 738 739 740 741 742 743 744 745 746
 * "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.
747
 *
748
 * [*] png_handle_alpha() does not exist yet, as of this version of libpng
749
 */
G
Guy Schalnat 已提交
750

751
void PNGAPI
G
Guy Schalnat 已提交
752
png_read_rows(png_structp png_ptr, png_bytepp row,
G
Guy Schalnat 已提交
753
   png_bytepp display_row, png_uint_32 num_rows)
G
Guy Schalnat 已提交
754
{
G
Guy Schalnat 已提交
755 756 757
   png_uint_32 i;
   png_bytepp rp;
   png_bytepp dp;
G
Guy Schalnat 已提交
758

A
Andreas Dilger 已提交
759
   png_debug(1, "in png_read_rows\n");
760
   if(png_ptr == NULL) return;
G
Guy Schalnat 已提交
761 762
   rp = row;
   dp = display_row;
763
   if (rp != NULL && dp != NULL)
764 765 766 767
      for (i = 0; i < num_rows; i++)
      {
         png_bytep rptr = *rp++;
         png_bytep dptr = *dp++;
768

769 770
         png_read_row(png_ptr, rptr, dptr);
      }
771
   else if(rp != NULL)
772 773
      for (i = 0; i < num_rows; i++)
      {
774
         png_bytep rptr = *rp;
775
         png_read_row(png_ptr, rptr, NULL);
776 777 778 779 780 781
         rp++;
      }
   else if(dp != NULL)
      for (i = 0; i < num_rows; i++)
      {
         png_bytep dptr = *dp;
782
         png_read_row(png_ptr, NULL, dptr);
783
         dp++;
784
      }
G
Guy Schalnat 已提交
785
}
786
#endif /* PNG_NO_SEQUENTIAL_READ_SUPPORTED */
G
Guy Schalnat 已提交
787

788
#ifndef PNG_NO_SEQUENTIAL_READ_SUPPORTED
A
Andreas Dilger 已提交
789
/* Read the entire image.  If the image has an alpha channel or a tRNS
790
 * chunk, and you have called png_handle_alpha()[*], you will need to
791 792 793 794 795 796 797
 * 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.
798
 *
799
 * [*] png_handle_alpha() does not exist yet, as of this version of libpng
800
 */
801
void PNGAPI
G
Guy Schalnat 已提交
802
png_read_image(png_structp png_ptr, png_bytepp image)
G
Guy Schalnat 已提交
803
{
804
   png_uint_32 i,image_height;
G
Guy Schalnat 已提交
805
   int pass, j;
G
Guy Schalnat 已提交
806
   png_bytepp rp;
G
Guy Schalnat 已提交
807

A
Andreas Dilger 已提交
808
   png_debug(1, "in png_read_image\n");
809
   if(png_ptr == NULL) return;
810 811

#ifdef PNG_READ_INTERLACING_SUPPORTED
G
Guy Schalnat 已提交
812
   pass = png_set_interlace_handling(png_ptr);
813 814 815
#else
   if (png_ptr->interlaced)
      png_error(png_ptr,
816
        "Cannot read interlaced image -- interlace handler disabled");
817 818 819
   pass = 1;
#endif

820 821
   image_height=png_ptr->height;
   png_ptr->num_rows = image_height; /* Make sure this is set correctly */
G
Guy Schalnat 已提交
822

G
Guy Schalnat 已提交
823 824 825
   for (j = 0; j < pass; j++)
   {
      rp = image;
826
      for (i = 0; i < image_height; i++)
G
Guy Schalnat 已提交
827
      {
828
         png_read_row(png_ptr, *rp, NULL);
G
Guy Schalnat 已提交
829 830 831 832
         rp++;
      }
   }
}
833
#endif /* PNG_NO_SEQUENTIAL_READ_SUPPORTED */
G
Guy Schalnat 已提交
834

835
#ifndef PNG_NO_SEQUENTIAL_READ_SUPPORTED
A
Andreas Dilger 已提交
836
/* Read the end of the PNG file.  Will not read past the end of the
837 838 839
 * 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.
 */
840
void PNGAPI
A
Andreas Dilger 已提交
841
png_read_end(png_structp png_ptr, png_infop info_ptr)
G
Guy Schalnat 已提交
842
{
843
   png_bytep chunk_name;
G
Guy Schalnat 已提交
844 845
   png_uint_32 length;

A
Andreas Dilger 已提交
846
   png_debug(1, "in png_read_end\n");
847
   if(png_ptr == NULL) return;
A
Andreas Dilger 已提交
848
   png_crc_finish(png_ptr, 0); /* Finish off CRC from last IDAT chunk */
G
Guy Schalnat 已提交
849 850 851

   do
   {
852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868
#ifdef PNG_USE_LOCAL_ARRAYS
      PNG_IHDR;
      PNG_IDAT;
      PNG_IEND;
      PNG_PLTE;
#if defined(PNG_READ_bKGD_SUPPORTED)
      PNG_bKGD;
#endif
#if defined(PNG_READ_cHRM_SUPPORTED)
      PNG_cHRM;
#endif
#if defined(PNG_READ_gAMA_SUPPORTED)
      PNG_gAMA;
#endif
#if defined(PNG_READ_hIST_SUPPORTED)
      PNG_hIST;
#endif
869 870 871 872 873 874
#if defined(PNG_READ_iCCP_SUPPORTED)
      PNG_iCCP;
#endif
#if defined(PNG_READ_iTXt_SUPPORTED)
      PNG_iTXt;
#endif
875 876 877 878 879 880 881 882 883 884 885 886
#if defined(PNG_READ_oFFs_SUPPORTED)
      PNG_oFFs;
#endif
#if defined(PNG_READ_pCAL_SUPPORTED)
      PNG_pCAL;
#endif
#if defined(PNG_READ_pHYs_SUPPORTED)
      PNG_pHYs;
#endif
#if defined(PNG_READ_sBIT_SUPPORTED)
      PNG_sBIT;
#endif
887
#if defined(PNG_READ_sCAL_SUPPORTED)
888 889 890 891 892
      PNG_sCAL;
#endif
#if defined(PNG_READ_sPLT_SUPPORTED)
      PNG_sPLT;
#endif
893 894 895 896 897 898 899 900 901 902 903 904 905 906 907
#if defined(PNG_READ_sRGB_SUPPORTED)
      PNG_sRGB;
#endif
#if defined(PNG_READ_tEXt_SUPPORTED)
      PNG_tEXt;
#endif
#if defined(PNG_READ_tIME_SUPPORTED)
      PNG_tIME;
#endif
#if defined(PNG_READ_tRNS_SUPPORTED)
      PNG_tRNS;
#endif
#if defined(PNG_READ_zTXt_SUPPORTED)
      PNG_zTXt;
#endif
908
#endif /* PNG_USE_LOCAL_ARRAYS */
909

910 911
      length = png_read_chunk_header(png_ptr);
      chunk_name = png_ptr->chunk_name;
A
Andreas Dilger 已提交
912

913
      if (!png_memcmp(chunk_name, png_IHDR, 4))
A
Andreas Dilger 已提交
914
         png_handle_IHDR(png_ptr, info_ptr, length);
915
      else if (!png_memcmp(chunk_name, png_IEND, 4))
916 917
         png_handle_IEND(png_ptr, info_ptr, length);
#ifdef PNG_HANDLE_AS_UNKNOWN_SUPPORTED
918
      else if (png_handle_as_unknown(png_ptr, chunk_name))
919
      {
920
         if (!png_memcmp(chunk_name, png_IDAT, 4))
921
         {
922 923
            if ((length > 0) || (png_ptr->mode & PNG_HAVE_CHUNK_AFTER_IDAT))
               png_benign_error(png_ptr, "Too many IDAT's found");
924 925
         }
         png_handle_unknown(png_ptr, info_ptr, length);
926
         if (!png_memcmp(chunk_name, png_PLTE, 4))
927 928 929
            png_ptr->mode |= PNG_HAVE_PLTE;
      }
#endif
930
      else if (!png_memcmp(chunk_name, png_IDAT, 4))
A
Andreas Dilger 已提交
931 932 933 934
      {
         /* Zero length IDATs are legal after the last IDAT has been
          * read, but not after other chunks have been read.
          */
935 936
         if ((length > 0) || (png_ptr->mode & PNG_HAVE_CHUNK_AFTER_IDAT))
            png_benign_error(png_ptr, "Too many IDAT's found");
937
         png_crc_finish(png_ptr, length);
A
Andreas Dilger 已提交
938
      }
939
      else if (!png_memcmp(chunk_name, png_PLTE, 4))
A
Andreas Dilger 已提交
940
         png_handle_PLTE(png_ptr, info_ptr, length);
A
Andreas Dilger 已提交
941
#if defined(PNG_READ_bKGD_SUPPORTED)
942
      else if (!png_memcmp(chunk_name, png_bKGD, 4))
A
Andreas Dilger 已提交
943
         png_handle_bKGD(png_ptr, info_ptr, length);
A
Andreas Dilger 已提交
944 945
#endif
#if defined(PNG_READ_cHRM_SUPPORTED)
946
      else if (!png_memcmp(chunk_name, png_cHRM, 4))
A
Andreas Dilger 已提交
947 948 949
         png_handle_cHRM(png_ptr, info_ptr, length);
#endif
#if defined(PNG_READ_gAMA_SUPPORTED)
950
      else if (!png_memcmp(chunk_name, png_gAMA, 4))
A
Andreas Dilger 已提交
951 952 953
         png_handle_gAMA(png_ptr, info_ptr, length);
#endif
#if defined(PNG_READ_hIST_SUPPORTED)
954
      else if (!png_memcmp(chunk_name, png_hIST, 4))
A
Andreas Dilger 已提交
955
         png_handle_hIST(png_ptr, info_ptr, length);
A
Andreas Dilger 已提交
956 957
#endif
#if defined(PNG_READ_oFFs_SUPPORTED)
958
      else if (!png_memcmp(chunk_name, png_oFFs, 4))
A
Andreas Dilger 已提交
959
         png_handle_oFFs(png_ptr, info_ptr, length);
A
Andreas Dilger 已提交
960 961
#endif
#if defined(PNG_READ_pCAL_SUPPORTED)
962
      else if (!png_memcmp(chunk_name, png_pCAL, 4))
A
Andreas Dilger 已提交
963 964
         png_handle_pCAL(png_ptr, info_ptr, length);
#endif
965
#if defined(PNG_READ_sCAL_SUPPORTED)
966
      else if (!png_memcmp(chunk_name, png_sCAL, 4))
967 968
         png_handle_sCAL(png_ptr, info_ptr, length);
#endif
A
Andreas Dilger 已提交
969
#if defined(PNG_READ_pHYs_SUPPORTED)
970
      else if (!png_memcmp(chunk_name, png_pHYs, 4))
A
Andreas Dilger 已提交
971 972 973
         png_handle_pHYs(png_ptr, info_ptr, length);
#endif
#if defined(PNG_READ_sBIT_SUPPORTED)
974
      else if (!png_memcmp(chunk_name, png_sBIT, 4))
A
Andreas Dilger 已提交
975
         png_handle_sBIT(png_ptr, info_ptr, length);
G
Guy Schalnat 已提交
976
#endif
977
#if defined(PNG_READ_sRGB_SUPPORTED)
978
      else if (!png_memcmp(chunk_name, png_sRGB, 4))
979 980
         png_handle_sRGB(png_ptr, info_ptr, length);
#endif
981
#if defined(PNG_READ_iCCP_SUPPORTED)
982
      else if (!png_memcmp(chunk_name, png_iCCP, 4))
983 984 985
         png_handle_iCCP(png_ptr, info_ptr, length);
#endif
#if defined(PNG_READ_sPLT_SUPPORTED)
986
      else if (!png_memcmp(chunk_name, png_sPLT, 4))
987 988
         png_handle_sPLT(png_ptr, info_ptr, length);
#endif
G
Guy Schalnat 已提交
989
#if defined(PNG_READ_tEXt_SUPPORTED)
990
      else if (!png_memcmp(chunk_name, png_tEXt, 4))
A
Andreas Dilger 已提交
991
         png_handle_tEXt(png_ptr, info_ptr, length);
G
Guy Schalnat 已提交
992
#endif
A
Andreas Dilger 已提交
993
#if defined(PNG_READ_tIME_SUPPORTED)
994
      else if (!png_memcmp(chunk_name, png_tIME, 4))
A
Andreas Dilger 已提交
995 996 997
         png_handle_tIME(png_ptr, info_ptr, length);
#endif
#if defined(PNG_READ_tRNS_SUPPORTED)
998
      else if (!png_memcmp(chunk_name, png_tRNS, 4))
A
Andreas Dilger 已提交
999 1000
         png_handle_tRNS(png_ptr, info_ptr, length);
#endif
G
Guy Schalnat 已提交
1001
#if defined(PNG_READ_zTXt_SUPPORTED)
1002
      else if (!png_memcmp(chunk_name, png_zTXt, 4))
A
Andreas Dilger 已提交
1003
         png_handle_zTXt(png_ptr, info_ptr, length);
1004 1005
#endif
#if defined(PNG_READ_iTXt_SUPPORTED)
1006
      else if (!png_memcmp(chunk_name, png_iTXt, 4))
1007
         png_handle_iTXt(png_ptr, info_ptr, length);
G
Guy Schalnat 已提交
1008
#endif
G
Guy Schalnat 已提交
1009
      else
A
Andreas Dilger 已提交
1010 1011
         png_handle_unknown(png_ptr, info_ptr, length);
   } while (!(png_ptr->mode & PNG_HAVE_IEND));
G
Guy Schalnat 已提交
1012
}
1013
#endif /* PNG_NO_SEQUENTIAL_READ_SUPPORTED */
G
Guy Schalnat 已提交
1014 1015

/* free all memory used by the read */
1016
void PNGAPI
G
Guy Schalnat 已提交
1017
png_destroy_read_struct(png_structpp png_ptr_ptr, png_infopp info_ptr_ptr,
A
Andreas Dilger 已提交
1018
   png_infopp end_info_ptr_ptr)
G
Guy Schalnat 已提交
1019 1020
{
   png_structp png_ptr = NULL;
A
Andreas Dilger 已提交
1021
   png_infop info_ptr = NULL, end_info_ptr = NULL;
1022
#ifdef PNG_USER_MEM_SUPPORTED
1023 1024
   png_free_ptr free_fn;
   png_voidp mem_ptr;
1025
#endif
G
Guy Schalnat 已提交
1026

A
Andreas Dilger 已提交
1027 1028
   png_debug(1, "in png_destroy_read_struct\n");
   if (png_ptr_ptr != NULL)
G
Guy Schalnat 已提交
1029 1030
      png_ptr = *png_ptr_ptr;

A
Andreas Dilger 已提交
1031
   if (info_ptr_ptr != NULL)
G
Guy Schalnat 已提交
1032 1033
      info_ptr = *info_ptr_ptr;

A
Andreas Dilger 已提交
1034
   if (end_info_ptr_ptr != NULL)
A
Andreas Dilger 已提交
1035
      end_info_ptr = *end_info_ptr_ptr;
G
Guy Schalnat 已提交
1036

1037 1038
#ifdef PNG_USER_MEM_SUPPORTED
   free_fn = png_ptr->free_fn;
1039
   mem_ptr = png_ptr->mem_ptr;
1040 1041
#endif

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

A
Andreas Dilger 已提交
1044
   if (info_ptr != NULL)
G
Guy Schalnat 已提交
1045
   {
1046
#if defined(PNG_TEXT_SUPPORTED)
1047
      png_free_data(png_ptr, info_ptr, PNG_FREE_TEXT, -1);
1048
#endif
1049 1050

#ifdef PNG_USER_MEM_SUPPORTED
1051 1052
      png_destroy_struct_2((png_voidp)info_ptr, (png_free_ptr)free_fn,
          (png_voidp)mem_ptr);
1053
#else
A
Andreas Dilger 已提交
1054
      png_destroy_struct((png_voidp)info_ptr);
1055
#endif
1056
      *info_ptr_ptr = NULL;
G
Guy Schalnat 已提交
1057 1058
   }

A
Andreas Dilger 已提交
1059
   if (end_info_ptr != NULL)
G
Guy Schalnat 已提交
1060
   {
1061
#if defined(PNG_READ_TEXT_SUPPORTED)
1062
      png_free_data(png_ptr, end_info_ptr, PNG_FREE_TEXT, -1);
1063
#endif
1064
#ifdef PNG_USER_MEM_SUPPORTED
1065 1066
      png_destroy_struct_2((png_voidp)end_info_ptr, (png_free_ptr)free_fn,
         (png_voidp)mem_ptr);
1067
#else
A
Andreas Dilger 已提交
1068
      png_destroy_struct((png_voidp)end_info_ptr);
1069
#endif
1070
      *end_info_ptr_ptr = NULL;
G
Guy Schalnat 已提交
1071 1072
   }

A
Andreas Dilger 已提交
1073
   if (png_ptr != NULL)
G
Guy Schalnat 已提交
1074
   {
1075
#ifdef PNG_USER_MEM_SUPPORTED
1076 1077
      png_destroy_struct_2((png_voidp)png_ptr, (png_free_ptr)free_fn,
          (png_voidp)mem_ptr);
1078
#else
A
Andreas Dilger 已提交
1079
      png_destroy_struct((png_voidp)png_ptr);
1080
#endif
1081
      *png_ptr_ptr = NULL;
G
Guy Schalnat 已提交
1082 1083 1084
   }
}

A
Andreas Dilger 已提交
1085
/* free all memory used by the read (old method) */
1086
void /* PRIVATE */
A
Andreas Dilger 已提交
1087
png_read_destroy(png_structp png_ptr, png_infop info_ptr, png_infop end_info_ptr)
G
Guy Schalnat 已提交
1088
{
1089
#ifdef PNG_SETJMP_SUPPORTED
G
Guy Schalnat 已提交
1090
   jmp_buf tmp_jmp;
1091
#endif
G
Guy Schalnat 已提交
1092 1093 1094
   png_error_ptr error_fn;
   png_error_ptr warning_fn;
   png_voidp error_ptr;
1095 1096 1097
#ifdef PNG_USER_MEM_SUPPORTED
   png_free_ptr free_fn;
#endif
G
Guy Schalnat 已提交
1098

A
Andreas Dilger 已提交
1099 1100
   png_debug(1, "in png_read_destroy\n");
   if (info_ptr != NULL)
A
Andreas Dilger 已提交
1101
      png_info_destroy(png_ptr, info_ptr);
G
Guy Schalnat 已提交
1102

A
Andreas Dilger 已提交
1103
   if (end_info_ptr != NULL)
A
Andreas Dilger 已提交
1104
      png_info_destroy(png_ptr, end_info_ptr);
G
Guy Schalnat 已提交
1105

A
Andreas Dilger 已提交
1106
   png_free(png_ptr, png_ptr->zbuf);
1107
   png_free(png_ptr, png_ptr->big_row_buf);
A
Andreas Dilger 已提交
1108
   png_free(png_ptr, png_ptr->prev_row);
G
Guy Schalnat 已提交
1109
#if defined(PNG_READ_DITHER_SUPPORTED)
A
Andreas Dilger 已提交
1110 1111
   png_free(png_ptr, png_ptr->palette_lookup);
   png_free(png_ptr, png_ptr->dither_index);
G
Guy Schalnat 已提交
1112 1113
#endif
#if defined(PNG_READ_GAMMA_SUPPORTED)
A
Andreas Dilger 已提交
1114
   png_free(png_ptr, png_ptr->gamma_table);
G
Guy Schalnat 已提交
1115 1116
#endif
#if defined(PNG_READ_BACKGROUND_SUPPORTED)
A
Andreas Dilger 已提交
1117 1118
   png_free(png_ptr, png_ptr->gamma_from_1);
   png_free(png_ptr, png_ptr->gamma_to_1);
G
Guy Schalnat 已提交
1119
#endif
1120
#ifdef PNG_FREE_ME_SUPPORTED
1121
   if (png_ptr->free_me & PNG_FREE_PLTE)
1122
      png_zfree(png_ptr, png_ptr->palette);
1123
   png_ptr->free_me &= ~PNG_FREE_PLTE;
1124 1125 1126 1127 1128
#else
   if (png_ptr->flags & PNG_FLAG_FREE_PLTE)
      png_zfree(png_ptr, png_ptr->palette);
   png_ptr->flags &= ~PNG_FLAG_FREE_PLTE;
#endif
1129
#if defined(PNG_tRNS_SUPPORTED) || \
1130
    defined(PNG_READ_EXPAND_SUPPORTED) || defined(PNG_READ_BACKGROUND_SUPPORTED)
1131
#ifdef PNG_FREE_ME_SUPPORTED
1132
   if (png_ptr->free_me & PNG_FREE_TRNS)
A
Andreas Dilger 已提交
1133
      png_free(png_ptr, png_ptr->trans);
1134
   png_ptr->free_me &= ~PNG_FREE_TRNS;
1135 1136 1137 1138 1139
#else
   if (png_ptr->flags & PNG_FLAG_FREE_TRNS)
      png_free(png_ptr, png_ptr->trans);
   png_ptr->flags &= ~PNG_FLAG_FREE_TRNS;
#endif
1140
#endif
G
Guy Schalnat 已提交
1141
#if defined(PNG_READ_hIST_SUPPORTED)
1142
#ifdef PNG_FREE_ME_SUPPORTED
1143
   if (png_ptr->free_me & PNG_FREE_HIST)
A
Andreas Dilger 已提交
1144
      png_free(png_ptr, png_ptr->hist);
1145
   png_ptr->free_me &= ~PNG_FREE_HIST;
1146 1147 1148 1149 1150
#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 已提交
1151 1152
#endif
#if defined(PNG_READ_GAMMA_SUPPORTED)
A
Andreas Dilger 已提交
1153
   if (png_ptr->gamma_16_table != NULL)
G
Guy Schalnat 已提交
1154
   {
1155 1156
      int i;
      int istop = (1 << (8 - png_ptr->gamma_shift));
1157
      for (i = 0; i < istop; i++)
G
Guy Schalnat 已提交
1158
      {
A
Andreas Dilger 已提交
1159
         png_free(png_ptr, png_ptr->gamma_16_table[i]);
G
Guy Schalnat 已提交
1160
      }
1161
   png_free(png_ptr, png_ptr->gamma_16_table);
G
Guy Schalnat 已提交
1162
   }
G
Guy Schalnat 已提交
1163
#if defined(PNG_READ_BACKGROUND_SUPPORTED)
A
Andreas Dilger 已提交
1164
   if (png_ptr->gamma_16_from_1 != NULL)
G
Guy Schalnat 已提交
1165
   {
1166 1167
      int i;
      int istop = (1 << (8 - png_ptr->gamma_shift));
1168
      for (i = 0; i < istop; i++)
G
Guy Schalnat 已提交
1169
      {
A
Andreas Dilger 已提交
1170
         png_free(png_ptr, png_ptr->gamma_16_from_1[i]);
G
Guy Schalnat 已提交
1171
      }
A
Andreas Dilger 已提交
1172
   png_free(png_ptr, png_ptr->gamma_16_from_1);
1173
   }
A
Andreas Dilger 已提交
1174
   if (png_ptr->gamma_16_to_1 != NULL)
G
Guy Schalnat 已提交
1175
   {
1176 1177
      int i;
      int istop = (1 << (8 - png_ptr->gamma_shift));
1178
      for (i = 0; i < istop; i++)
G
Guy Schalnat 已提交
1179
      {
A
Andreas Dilger 已提交
1180
         png_free(png_ptr, png_ptr->gamma_16_to_1[i]);
G
Guy Schalnat 已提交
1181
      }
A
Andreas Dilger 已提交
1182
   png_free(png_ptr, png_ptr->gamma_16_to_1);
1183
   }
G
Guy Schalnat 已提交
1184
#endif
1185 1186 1187
#endif
#if defined(PNG_TIME_RFC1123_SUPPORTED)
   png_free(png_ptr, png_ptr->time_buffer);
1188
#endif
G
Guy Schalnat 已提交
1189

A
Andreas Dilger 已提交
1190
   inflateEnd(&png_ptr->zstream);
G
Guy Schalnat 已提交
1191
#ifdef PNG_PROGRESSIVE_READ_SUPPORTED
A
Andreas Dilger 已提交
1192
   png_free(png_ptr, png_ptr->save_buffer);
G
Guy Schalnat 已提交
1193
#endif
G
Guy Schalnat 已提交
1194

1195 1196 1197 1198 1199 1200
#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 已提交
1201 1202 1203
   /* Save the important info out of the png_struct, in case it is
    * being used again.
    */
1204
#ifdef PNG_SETJMP_SUPPORTED
1205
   png_memcpy(tmp_jmp, png_ptr->jmpbuf, sizeof(jmp_buf));
1206
#endif
G
Guy Schalnat 已提交
1207 1208 1209 1210

   error_fn = png_ptr->error_fn;
   warning_fn = png_ptr->warning_fn;
   error_ptr = png_ptr->error_ptr;
1211 1212 1213
#ifdef PNG_USER_MEM_SUPPORTED
   free_fn = png_ptr->free_fn;
#endif
G
Guy Schalnat 已提交
1214

1215
   png_memset(png_ptr, 0, sizeof(png_struct));
G
Guy Schalnat 已提交
1216 1217 1218 1219

   png_ptr->error_fn = error_fn;
   png_ptr->warning_fn = warning_fn;
   png_ptr->error_ptr = error_ptr;
1220 1221 1222
#ifdef PNG_USER_MEM_SUPPORTED
   png_ptr->free_fn = free_fn;
#endif
G
Guy Schalnat 已提交
1223

1224
#ifdef PNG_SETJMP_SUPPORTED
1225
   png_memcpy(png_ptr->jmpbuf, tmp_jmp, sizeof(jmp_buf));
1226 1227
#endif

G
Guy Schalnat 已提交
1228
}
1229

1230
void PNGAPI
1231 1232
png_set_read_status_fn(png_structp png_ptr, png_read_status_ptr read_row_fn)
{
1233
   if(png_ptr == NULL) return;
1234 1235
   png_ptr->read_row_fn = read_row_fn;
}
1236

1237 1238

#ifndef PNG_NO_SEQUENTIAL_READ_SUPPORTED
1239
#if defined(PNG_INFO_IMAGE_SUPPORTED)
1240 1241
void PNGAPI
png_read_png(png_structp png_ptr, png_infop info_ptr,
1242 1243 1244 1245 1246
                           int transforms,
                           voidp params)
{
   int row;

1247
   if(png_ptr == NULL) return;
1248
#if defined(PNG_READ_INVERT_ALPHA_SUPPORTED)
1249 1250
   /* invert the alpha channel from opacity to transparency
    */
1251 1252 1253 1254
   if (transforms & PNG_TRANSFORM_INVERT_ALPHA)
       png_set_invert_alpha(png_ptr);
#endif

1255
   /* png_read_info() gives us all of the information from the
1256 1257 1258
    * PNG file before the first IDAT (image data chunk).
    */
   png_read_info(png_ptr, info_ptr);
1259
   if (info_ptr->height > PNG_UINT_32_MAX/sizeof(png_bytep))
1260
      png_error(png_ptr,"Image is too high to process with png_read_png()");
1261 1262 1263 1264

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

#if defined(PNG_READ_16_TO_8_SUPPORTED)
1265 1266
   /* tell libpng to strip 16 bit/color files down to 8 bits per color
    */
1267 1268 1269 1270 1271
   if (transforms & PNG_TRANSFORM_STRIP_16)
       png_set_strip_16(png_ptr);
#endif

#if defined(PNG_READ_STRIP_ALPHA_SUPPORTED)
1272 1273
   /* Strip alpha bytes from the input data without combining with
    * the background (not recommended).
1274 1275 1276 1277 1278
    */
   if (transforms & PNG_TRANSFORM_STRIP_ALPHA)
       png_set_strip_alpha(png_ptr);
#endif

1279
#if defined(PNG_READ_PACK_SUPPORTED) && !defined(PNG_READ_EXPAND_SUPPORTED)
1280
   /* Extract multiple pixels with bit depths of 1, 2, or 4 from a single
1281 1282 1283 1284 1285 1286 1287 1288
    * byte into separate bytes (useful for paletted and grayscale images).
    */
   if (transforms & PNG_TRANSFORM_PACKING)
       png_set_packing(png_ptr);
#endif

#if defined(PNG_READ_PACKSWAP_SUPPORTED)
   /* Change the order of packed pixels to least significant bit first
1289 1290
    * (not useful if you are using png_set_packing).
    */
1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301
   if (transforms & PNG_TRANSFORM_PACKSWAP)
       png_set_packswap(png_ptr);
#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)
1302 1303 1304 1305
       if ((png_ptr->bit_depth < 8) ||
           (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE) ||
           (png_get_valid(png_ptr, info_ptr, PNG_INFO_tRNS)))
         png_set_expand(png_ptr);
1306 1307
#endif

1308 1309
   /* We don't handle background color or gamma transformation or dithering.
    */
1310 1311

#if defined(PNG_READ_INVERT_SUPPORTED)
1312 1313
   /* invert monochrome files to have 0 as white and 1 as black
    */
1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333
   if (transforms & PNG_TRANSFORM_INVERT_MONO)
       png_set_invert_mono(png_ptr);
#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)
1334 1335
   /* flip the RGB pixels to BGR (or RGBA to BGRA)
    */
1336 1337 1338 1339 1340
   if (transforms & PNG_TRANSFORM_BGR)
       png_set_bgr(png_ptr);
#endif

#if defined(PNG_READ_SWAP_ALPHA_SUPPORTED)
1341 1342
   /* swap the RGBA or GA data to ARGB or AG (or BGRA to ABGR)
    */
1343 1344 1345 1346 1347
   if (transforms & PNG_TRANSFORM_SWAP_ALPHA)
       png_set_swap_alpha(png_ptr);
#endif

#if defined(PNG_READ_SWAP_SUPPORTED)
1348 1349
   /* swap bytes of 16 bit files to least significant byte first
    */
1350 1351 1352 1353 1354 1355 1356 1357
   if (transforms & PNG_TRANSFORM_SWAP_ENDIAN)
       png_set_swap(png_ptr);
#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
1358
    * update the palette for you (i.e., you selected such a transform above).
1359 1360 1361 1362 1363
    */
   png_read_update_info(png_ptr, info_ptr);

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

1364
#ifdef PNG_FREE_ME_SUPPORTED
1365
   png_free_data(png_ptr, info_ptr, PNG_FREE_ROWS, 0);
1366
#endif
1367 1368 1369
   if(info_ptr->row_pointers == NULL)
   {
      info_ptr->row_pointers = (png_bytepp)png_malloc(png_ptr,
1370
         info_ptr->height * sizeof(png_bytep));
1371
#ifdef PNG_FREE_ME_SUPPORTED
1372
      info_ptr->free_me |= PNG_FREE_ROWS;
1373 1374
#endif
      for (row = 0; row < (int)info_ptr->height; row++)
1375 1376
      {
         info_ptr->row_pointers[row] = (png_bytep)png_malloc(png_ptr,
1377
            png_get_rowbytes(png_ptr, info_ptr));
1378
      }
1379
   }
1380 1381 1382 1383 1384 1385

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

   /* read rest of file, and get additional chunks in info_ptr - REQUIRED */
   png_read_end(png_ptr, info_ptr);
1386

1387
   if(transforms == 0 || params == NULL)
1388 1389
      /* quiet compiler warnings */ return;

1390
}
1391
#endif /* PNG_INFO_IMAGE_SUPPORTED */
1392
#endif /* PNG_NO_SEQUENTIAL_READ_SUPPORTED */
1393
#endif /* PNG_READ_SUPPORTED */