dbf-halloween2015

diff libs/libpng/pngread.c @ 1:c3f5c32cb210

barfed all the libraries in the source tree to make porting easier
author John Tsiombikas <nuclear@member.fsf.org>
date Sun, 01 Nov 2015 00:36:56 +0200
parents
children
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/libs/libpng/pngread.c	Sun Nov 01 00:36:56 2015 +0200
     1.3 @@ -0,0 +1,1459 @@
     1.4 +
     1.5 +/* pngread.c - read a PNG file
     1.6 + *
     1.7 + * Last changed in libpng 1.2.30 [August 15, 2008]
     1.8 + * For conditions of distribution and use, see copyright notice in png.h
     1.9 + * Copyright (c) 1998-2008 Glenn Randers-Pehrson
    1.10 + * (Version 0.96 Copyright (c) 1996, 1997 Andreas Dilger)
    1.11 + * (Version 0.88 Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.)
    1.12 + *
    1.13 + * This file contains routines that an application calls directly to
    1.14 + * read a PNG file or stream.
    1.15 + */
    1.16 +
    1.17 +#define PNG_INTERNAL
    1.18 +#include "png.h"
    1.19 +#if defined(PNG_READ_SUPPORTED)
    1.20 +
    1.21 +/* Create a PNG structure for reading, and allocate any memory needed. */
    1.22 +png_structp PNGAPI
    1.23 +png_create_read_struct(png_const_charp user_png_ver, png_voidp error_ptr,
    1.24 +   png_error_ptr error_fn, png_error_ptr warn_fn)
    1.25 +{
    1.26 +
    1.27 +#ifdef PNG_USER_MEM_SUPPORTED
    1.28 +   return (png_create_read_struct_2(user_png_ver, error_ptr, error_fn,
    1.29 +      warn_fn, png_voidp_NULL, png_malloc_ptr_NULL, png_free_ptr_NULL));
    1.30 +}
    1.31 +
    1.32 +/* Alternate create PNG structure for reading, and allocate any memory needed. */
    1.33 +png_structp PNGAPI
    1.34 +png_create_read_struct_2(png_const_charp user_png_ver, png_voidp error_ptr,
    1.35 +   png_error_ptr error_fn, png_error_ptr warn_fn, png_voidp mem_ptr,
    1.36 +   png_malloc_ptr malloc_fn, png_free_ptr free_fn)
    1.37 +{
    1.38 +#endif /* PNG_USER_MEM_SUPPORTED */
    1.39 +
    1.40 +#ifdef PNG_SETJMP_SUPPORTED
    1.41 +   volatile
    1.42 +#endif
    1.43 +   png_structp png_ptr;
    1.44 +
    1.45 +#ifdef PNG_SETJMP_SUPPORTED
    1.46 +#ifdef USE_FAR_KEYWORD
    1.47 +   jmp_buf jmpbuf;
    1.48 +#endif
    1.49 +#endif
    1.50 +
    1.51 +   int i;
    1.52 +
    1.53 +   png_debug(1, "in png_create_read_struct\n");
    1.54 +#ifdef PNG_USER_MEM_SUPPORTED
    1.55 +   png_ptr = (png_structp)png_create_struct_2(PNG_STRUCT_PNG,
    1.56 +      (png_malloc_ptr)malloc_fn, (png_voidp)mem_ptr);
    1.57 +#else
    1.58 +   png_ptr = (png_structp)png_create_struct(PNG_STRUCT_PNG);
    1.59 +#endif
    1.60 +   if (png_ptr == NULL)
    1.61 +      return (NULL);
    1.62 +
    1.63 +   /* added at libpng-1.2.6 */
    1.64 +#ifdef PNG_SET_USER_LIMITS_SUPPORTED
    1.65 +   png_ptr->user_width_max=PNG_USER_WIDTH_MAX;
    1.66 +   png_ptr->user_height_max=PNG_USER_HEIGHT_MAX;
    1.67 +#endif
    1.68 +
    1.69 +#ifdef PNG_SETJMP_SUPPORTED
    1.70 +#ifdef USE_FAR_KEYWORD
    1.71 +   if (setjmp(jmpbuf))
    1.72 +#else
    1.73 +   if (setjmp(png_ptr->jmpbuf))
    1.74 +#endif
    1.75 +   {
    1.76 +      png_free(png_ptr, png_ptr->zbuf);
    1.77 +      png_ptr->zbuf = NULL;
    1.78 +#ifdef PNG_USER_MEM_SUPPORTED
    1.79 +      png_destroy_struct_2((png_voidp)png_ptr,
    1.80 +         (png_free_ptr)free_fn, (png_voidp)mem_ptr);
    1.81 +#else
    1.82 +      png_destroy_struct((png_voidp)png_ptr);
    1.83 +#endif
    1.84 +      return (NULL);
    1.85 +   }
    1.86 +#ifdef USE_FAR_KEYWORD
    1.87 +   png_memcpy(png_ptr->jmpbuf, jmpbuf, png_sizeof(jmp_buf));
    1.88 +#endif
    1.89 +#endif
    1.90 +
    1.91 +#ifdef PNG_USER_MEM_SUPPORTED
    1.92 +   png_set_mem_fn(png_ptr, mem_ptr, malloc_fn, free_fn);
    1.93 +#endif
    1.94 +
    1.95 +   png_set_error_fn(png_ptr, error_ptr, error_fn, warn_fn);
    1.96 +
    1.97 +   if (user_png_ver)
    1.98 +   {
    1.99 +     i = 0;
   1.100 +     do
   1.101 +     {
   1.102 +       if (user_png_ver[i] != png_libpng_ver[i])
   1.103 +          png_ptr->flags |= PNG_FLAG_LIBRARY_MISMATCH;
   1.104 +     } while (png_libpng_ver[i++]);
   1.105 +   }
   1.106 +   else
   1.107 +        png_ptr->flags |= PNG_FLAG_LIBRARY_MISMATCH;
   1.108 +   
   1.109 +
   1.110 +   if (png_ptr->flags & PNG_FLAG_LIBRARY_MISMATCH)
   1.111 +   {
   1.112 +     /* Libpng 0.90 and later are binary incompatible with libpng 0.89, so
   1.113 +      * we must recompile any applications that use any older library version.
   1.114 +      * For versions after libpng 1.0, we will be compatible, so we need
   1.115 +      * only check the first digit.
   1.116 +      */
   1.117 +     if (user_png_ver == NULL || user_png_ver[0] != png_libpng_ver[0] ||
   1.118 +         (user_png_ver[0] == '1' && user_png_ver[2] != png_libpng_ver[2]) ||
   1.119 +         (user_png_ver[0] == '0' && user_png_ver[2] < '9'))
   1.120 +     {
   1.121 +#if !defined(PNG_NO_STDIO) && !defined(_WIN32_WCE)
   1.122 +        char msg[80];
   1.123 +        if (user_png_ver)
   1.124 +        {
   1.125 +          png_snprintf(msg, 80,
   1.126 +             "Application was compiled with png.h from libpng-%.20s",
   1.127 +             user_png_ver);
   1.128 +          png_warning(png_ptr, msg);
   1.129 +        }
   1.130 +        png_snprintf(msg, 80,
   1.131 +             "Application  is  running with png.c from libpng-%.20s",
   1.132 +           png_libpng_ver);
   1.133 +        png_warning(png_ptr, msg);
   1.134 +#endif
   1.135 +#ifdef PNG_ERROR_NUMBERS_SUPPORTED
   1.136 +        png_ptr->flags = 0;
   1.137 +#endif
   1.138 +        png_error(png_ptr,
   1.139 +           "Incompatible libpng version in application and library");
   1.140 +     }
   1.141 +   }
   1.142 +
   1.143 +   /* initialize zbuf - compression buffer */
   1.144 +   png_ptr->zbuf_size = PNG_ZBUF_SIZE;
   1.145 +   png_ptr->zbuf = (png_bytep)png_malloc(png_ptr,
   1.146 +     (png_uint_32)png_ptr->zbuf_size);
   1.147 +   png_ptr->zstream.zalloc = png_zalloc;
   1.148 +   png_ptr->zstream.zfree = png_zfree;
   1.149 +   png_ptr->zstream.opaque = (voidpf)png_ptr;
   1.150 +
   1.151 +   switch (inflateInit(&png_ptr->zstream))
   1.152 +   {
   1.153 +     case Z_OK: /* Do nothing */ break;
   1.154 +     case Z_MEM_ERROR:
   1.155 +     case Z_STREAM_ERROR: png_error(png_ptr, "zlib memory error"); break;
   1.156 +     case Z_VERSION_ERROR: png_error(png_ptr, "zlib version error"); break;
   1.157 +     default: png_error(png_ptr, "Unknown zlib error");
   1.158 +   }
   1.159 +
   1.160 +   png_ptr->zstream.next_out = png_ptr->zbuf;
   1.161 +   png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size;
   1.162 +
   1.163 +   png_set_read_fn(png_ptr, png_voidp_NULL, png_rw_ptr_NULL);
   1.164 +
   1.165 +#ifdef PNG_SETJMP_SUPPORTED
   1.166 +/* Applications that neglect to set up their own setjmp() and then encounter
   1.167 +   a png_error() will longjmp here.  Since the jmpbuf is then meaningless we
   1.168 +   abort instead of returning. */
   1.169 +#ifdef USE_FAR_KEYWORD
   1.170 +   if (setjmp(jmpbuf))
   1.171 +      PNG_ABORT();
   1.172 +   png_memcpy(png_ptr->jmpbuf, jmpbuf, png_sizeof(jmp_buf));
   1.173 +#else
   1.174 +   if (setjmp(png_ptr->jmpbuf))
   1.175 +      PNG_ABORT();
   1.176 +#endif
   1.177 +#endif
   1.178 +   return (png_ptr);
   1.179 +}
   1.180 +
   1.181 +#if defined(PNG_1_0_X) || defined(PNG_1_2_X)
   1.182 +/* Initialize PNG structure for reading, and allocate any memory needed.
   1.183 +   This interface is deprecated in favour of the png_create_read_struct(),
   1.184 +   and it will disappear as of libpng-1.3.0. */
   1.185 +#undef png_read_init
   1.186 +void PNGAPI
   1.187 +png_read_init(png_structp png_ptr)
   1.188 +{
   1.189 +   /* We only come here via pre-1.0.7-compiled applications */
   1.190 +   png_read_init_2(png_ptr, "1.0.6 or earlier", 0, 0);
   1.191 +}
   1.192 +
   1.193 +void PNGAPI
   1.194 +png_read_init_2(png_structp png_ptr, png_const_charp user_png_ver,
   1.195 +   png_size_t png_struct_size, png_size_t png_info_size)
   1.196 +{
   1.197 +   /* We only come here via pre-1.0.12-compiled applications */
   1.198 +   if (png_ptr == NULL) return;
   1.199 +#if !defined(PNG_NO_STDIO) && !defined(_WIN32_WCE)
   1.200 +   if (png_sizeof(png_struct) > png_struct_size ||
   1.201 +      png_sizeof(png_info) > png_info_size)
   1.202 +   {
   1.203 +      char msg[80];
   1.204 +      png_ptr->warning_fn = NULL;
   1.205 +      if (user_png_ver)
   1.206 +      {
   1.207 +        png_snprintf(msg, 80,
   1.208 +           "Application was compiled with png.h from libpng-%.20s",
   1.209 +           user_png_ver);
   1.210 +        png_warning(png_ptr, msg);
   1.211 +      }
   1.212 +      png_snprintf(msg, 80,
   1.213 +         "Application  is  running with png.c from libpng-%.20s",
   1.214 +         png_libpng_ver);
   1.215 +      png_warning(png_ptr, msg);
   1.216 +   }
   1.217 +#endif
   1.218 +   if (png_sizeof(png_struct) > png_struct_size)
   1.219 +     {
   1.220 +       png_ptr->error_fn = NULL;
   1.221 +#ifdef PNG_ERROR_NUMBERS_SUPPORTED
   1.222 +       png_ptr->flags = 0;
   1.223 +#endif
   1.224 +       png_error(png_ptr,
   1.225 +       "The png struct allocated by the application for reading is too small.");
   1.226 +     }
   1.227 +   if (png_sizeof(png_info) > png_info_size)
   1.228 +     {
   1.229 +       png_ptr->error_fn = NULL;
   1.230 +#ifdef PNG_ERROR_NUMBERS_SUPPORTED
   1.231 +       png_ptr->flags = 0;
   1.232 +#endif
   1.233 +       png_error(png_ptr,
   1.234 +         "The info struct allocated by application for reading is too small.");
   1.235 +     }
   1.236 +   png_read_init_3(&png_ptr, user_png_ver, png_struct_size);
   1.237 +}
   1.238 +#endif /* PNG_1_0_X || PNG_1_2_X */
   1.239 +
   1.240 +void PNGAPI
   1.241 +png_read_init_3(png_structpp ptr_ptr, png_const_charp user_png_ver,
   1.242 +   png_size_t png_struct_size)
   1.243 +{
   1.244 +#ifdef PNG_SETJMP_SUPPORTED
   1.245 +   jmp_buf tmp_jmp;  /* to save current jump buffer */
   1.246 +#endif
   1.247 +
   1.248 +   int i = 0;
   1.249 +
   1.250 +   png_structp png_ptr=*ptr_ptr;
   1.251 +
   1.252 +   if (png_ptr == NULL) return;
   1.253 +
   1.254 +   do
   1.255 +   {
   1.256 +     if (user_png_ver[i] != png_libpng_ver[i])
   1.257 +     {
   1.258 +#ifdef PNG_LEGACY_SUPPORTED
   1.259 +       png_ptr->flags |= PNG_FLAG_LIBRARY_MISMATCH;
   1.260 +#else
   1.261 +       png_ptr->warning_fn = NULL;
   1.262 +       png_warning(png_ptr,
   1.263 +        "Application uses deprecated png_read_init() and should be recompiled.");
   1.264 +       break;
   1.265 +#endif
   1.266 +     }
   1.267 +   } while (png_libpng_ver[i++]);
   1.268 +
   1.269 +   png_debug(1, "in png_read_init_3\n");
   1.270 +
   1.271 +#ifdef PNG_SETJMP_SUPPORTED
   1.272 +   /* save jump buffer and error functions */
   1.273 +   png_memcpy(tmp_jmp, png_ptr->jmpbuf, png_sizeof(jmp_buf));
   1.274 +#endif
   1.275 +
   1.276 +   if (png_sizeof(png_struct) > png_struct_size)
   1.277 +   {
   1.278 +      png_destroy_struct(png_ptr);
   1.279 +      *ptr_ptr = (png_structp)png_create_struct(PNG_STRUCT_PNG);
   1.280 +      png_ptr = *ptr_ptr;
   1.281 +   }
   1.282 +
   1.283 +   /* reset all variables to 0 */
   1.284 +   png_memset(png_ptr, 0, png_sizeof(png_struct));
   1.285 +
   1.286 +#ifdef PNG_SETJMP_SUPPORTED
   1.287 +   /* restore jump buffer */
   1.288 +   png_memcpy(png_ptr->jmpbuf, tmp_jmp, png_sizeof(jmp_buf));
   1.289 +#endif
   1.290 +
   1.291 +   /* added at libpng-1.2.6 */
   1.292 +#ifdef PNG_SET_USER_LIMITS_SUPPORTED
   1.293 +   png_ptr->user_width_max=PNG_USER_WIDTH_MAX;
   1.294 +   png_ptr->user_height_max=PNG_USER_HEIGHT_MAX;
   1.295 +#endif
   1.296 +
   1.297 +   /* initialize zbuf - compression buffer */
   1.298 +   png_ptr->zbuf_size = PNG_ZBUF_SIZE;
   1.299 +   png_ptr->zbuf = (png_bytep)png_malloc(png_ptr,
   1.300 +     (png_uint_32)png_ptr->zbuf_size);
   1.301 +   png_ptr->zstream.zalloc = png_zalloc;
   1.302 +   png_ptr->zstream.zfree = png_zfree;
   1.303 +   png_ptr->zstream.opaque = (voidpf)png_ptr;
   1.304 +
   1.305 +   switch (inflateInit(&png_ptr->zstream))
   1.306 +   {
   1.307 +     case Z_OK: /* Do nothing */ break;
   1.308 +     case Z_MEM_ERROR:
   1.309 +     case Z_STREAM_ERROR: png_error(png_ptr, "zlib memory"); break;
   1.310 +     case Z_VERSION_ERROR: png_error(png_ptr, "zlib version"); break;
   1.311 +     default: png_error(png_ptr, "Unknown zlib error");
   1.312 +   }
   1.313 +
   1.314 +   png_ptr->zstream.next_out = png_ptr->zbuf;
   1.315 +   png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size;
   1.316 +
   1.317 +   png_set_read_fn(png_ptr, png_voidp_NULL, png_rw_ptr_NULL);
   1.318 +}
   1.319 +
   1.320 +#ifndef PNG_NO_SEQUENTIAL_READ_SUPPORTED
   1.321 +/* Read the information before the actual image data.  This has been
   1.322 + * changed in v0.90 to allow reading a file that already has the magic
   1.323 + * bytes read from the stream.  You can tell libpng how many bytes have
   1.324 + * been read from the beginning of the stream (up to the maximum of 8)
   1.325 + * via png_set_sig_bytes(), and we will only check the remaining bytes
   1.326 + * here.  The application can then have access to the signature bytes we
   1.327 + * read if it is determined that this isn't a valid PNG file.
   1.328 + */
   1.329 +void PNGAPI
   1.330 +png_read_info(png_structp png_ptr, png_infop info_ptr)
   1.331 +{
   1.332 +   if (png_ptr == NULL || info_ptr == NULL) return;
   1.333 +   png_debug(1, "in png_read_info\n");
   1.334 +   /* If we haven't checked all of the PNG signature bytes, do so now. */
   1.335 +   if (png_ptr->sig_bytes < 8)
   1.336 +   {
   1.337 +      png_size_t num_checked = png_ptr->sig_bytes,
   1.338 +                 num_to_check = 8 - num_checked;
   1.339 +
   1.340 +      png_read_data(png_ptr, &(info_ptr->signature[num_checked]), num_to_check);
   1.341 +      png_ptr->sig_bytes = 8;
   1.342 +
   1.343 +      if (png_sig_cmp(info_ptr->signature, num_checked, num_to_check))
   1.344 +      {
   1.345 +         if (num_checked < 4 &&
   1.346 +             png_sig_cmp(info_ptr->signature, num_checked, num_to_check - 4))
   1.347 +            png_error(png_ptr, "Not a PNG file");
   1.348 +         else
   1.349 +            png_error(png_ptr, "PNG file corrupted by ASCII conversion");
   1.350 +      }
   1.351 +      if (num_checked < 3)
   1.352 +         png_ptr->mode |= PNG_HAVE_PNG_SIGNATURE;
   1.353 +   }
   1.354 +
   1.355 +   for (;;)
   1.356 +   {
   1.357 +#ifdef PNG_USE_LOCAL_ARRAYS
   1.358 +      PNG_CONST PNG_IHDR;
   1.359 +      PNG_CONST PNG_IDAT;
   1.360 +      PNG_CONST PNG_IEND;
   1.361 +      PNG_CONST PNG_PLTE;
   1.362 +#if defined(PNG_READ_bKGD_SUPPORTED)
   1.363 +      PNG_CONST PNG_bKGD;
   1.364 +#endif
   1.365 +#if defined(PNG_READ_cHRM_SUPPORTED)
   1.366 +      PNG_CONST PNG_cHRM;
   1.367 +#endif
   1.368 +#if defined(PNG_READ_gAMA_SUPPORTED)
   1.369 +      PNG_CONST PNG_gAMA;
   1.370 +#endif
   1.371 +#if defined(PNG_READ_hIST_SUPPORTED)
   1.372 +      PNG_CONST PNG_hIST;
   1.373 +#endif
   1.374 +#if defined(PNG_READ_iCCP_SUPPORTED)
   1.375 +      PNG_CONST PNG_iCCP;
   1.376 +#endif
   1.377 +#if defined(PNG_READ_iTXt_SUPPORTED)
   1.378 +      PNG_CONST PNG_iTXt;
   1.379 +#endif
   1.380 +#if defined(PNG_READ_oFFs_SUPPORTED)
   1.381 +      PNG_CONST PNG_oFFs;
   1.382 +#endif
   1.383 +#if defined(PNG_READ_pCAL_SUPPORTED)
   1.384 +      PNG_CONST PNG_pCAL;
   1.385 +#endif
   1.386 +#if defined(PNG_READ_pHYs_SUPPORTED)
   1.387 +      PNG_CONST PNG_pHYs;
   1.388 +#endif
   1.389 +#if defined(PNG_READ_sBIT_SUPPORTED)
   1.390 +      PNG_CONST PNG_sBIT;
   1.391 +#endif
   1.392 +#if defined(PNG_READ_sCAL_SUPPORTED)
   1.393 +      PNG_CONST PNG_sCAL;
   1.394 +#endif
   1.395 +#if defined(PNG_READ_sPLT_SUPPORTED)
   1.396 +      PNG_CONST PNG_sPLT;
   1.397 +#endif
   1.398 +#if defined(PNG_READ_sRGB_SUPPORTED)
   1.399 +      PNG_CONST PNG_sRGB;
   1.400 +#endif
   1.401 +#if defined(PNG_READ_tEXt_SUPPORTED)
   1.402 +      PNG_CONST PNG_tEXt;
   1.403 +#endif
   1.404 +#if defined(PNG_READ_tIME_SUPPORTED)
   1.405 +      PNG_CONST PNG_tIME;
   1.406 +#endif
   1.407 +#if defined(PNG_READ_tRNS_SUPPORTED)
   1.408 +      PNG_CONST PNG_tRNS;
   1.409 +#endif
   1.410 +#if defined(PNG_READ_zTXt_SUPPORTED)
   1.411 +      PNG_CONST PNG_zTXt;
   1.412 +#endif
   1.413 +#endif /* PNG_USE_LOCAL_ARRAYS */
   1.414 +      png_uint_32 length = png_read_chunk_header(png_ptr);
   1.415 +      PNG_CONST png_bytep chunk_name = png_ptr->chunk_name;
   1.416 +
   1.417 +      /* This should be a binary subdivision search or a hash for
   1.418 +       * matching the chunk name rather than a linear search.
   1.419 +       */
   1.420 +      if (!png_memcmp(chunk_name, png_IDAT, 4))
   1.421 +        if (png_ptr->mode & PNG_AFTER_IDAT)
   1.422 +          png_ptr->mode |= PNG_HAVE_CHUNK_AFTER_IDAT;
   1.423 +
   1.424 +      if (!png_memcmp(chunk_name, png_IHDR, 4))
   1.425 +         png_handle_IHDR(png_ptr, info_ptr, length);
   1.426 +      else if (!png_memcmp(chunk_name, png_IEND, 4))
   1.427 +         png_handle_IEND(png_ptr, info_ptr, length);
   1.428 +#ifdef PNG_HANDLE_AS_UNKNOWN_SUPPORTED
   1.429 +      else if (png_handle_as_unknown(png_ptr, chunk_name))
   1.430 +      {
   1.431 +         if (!png_memcmp(chunk_name, png_IDAT, 4))
   1.432 +            png_ptr->mode |= PNG_HAVE_IDAT;
   1.433 +         png_handle_unknown(png_ptr, info_ptr, length);
   1.434 +         if (!png_memcmp(chunk_name, png_PLTE, 4))
   1.435 +            png_ptr->mode |= PNG_HAVE_PLTE;
   1.436 +         else if (!png_memcmp(chunk_name, png_IDAT, 4))
   1.437 +         {
   1.438 +            if (!(png_ptr->mode & PNG_HAVE_IHDR))
   1.439 +               png_error(png_ptr, "Missing IHDR before IDAT");
   1.440 +            else if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE &&
   1.441 +                     !(png_ptr->mode & PNG_HAVE_PLTE))
   1.442 +               png_error(png_ptr, "Missing PLTE before IDAT");
   1.443 +            break;
   1.444 +         }
   1.445 +      }
   1.446 +#endif
   1.447 +      else if (!png_memcmp(chunk_name, png_PLTE, 4))
   1.448 +         png_handle_PLTE(png_ptr, info_ptr, length);
   1.449 +      else if (!png_memcmp(chunk_name, png_IDAT, 4))
   1.450 +      {
   1.451 +         if (!(png_ptr->mode & PNG_HAVE_IHDR))
   1.452 +            png_error(png_ptr, "Missing IHDR before IDAT");
   1.453 +         else if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE &&
   1.454 +                  !(png_ptr->mode & PNG_HAVE_PLTE))
   1.455 +            png_error(png_ptr, "Missing PLTE before IDAT");
   1.456 +
   1.457 +         png_ptr->idat_size = length;
   1.458 +         png_ptr->mode |= PNG_HAVE_IDAT;
   1.459 +         break;
   1.460 +      }
   1.461 +#if defined(PNG_READ_bKGD_SUPPORTED)
   1.462 +      else if (!png_memcmp(chunk_name, png_bKGD, 4))
   1.463 +         png_handle_bKGD(png_ptr, info_ptr, length);
   1.464 +#endif
   1.465 +#if defined(PNG_READ_cHRM_SUPPORTED)
   1.466 +      else if (!png_memcmp(chunk_name, png_cHRM, 4))
   1.467 +         png_handle_cHRM(png_ptr, info_ptr, length);
   1.468 +#endif
   1.469 +#if defined(PNG_READ_gAMA_SUPPORTED)
   1.470 +      else if (!png_memcmp(chunk_name, png_gAMA, 4))
   1.471 +         png_handle_gAMA(png_ptr, info_ptr, length);
   1.472 +#endif
   1.473 +#if defined(PNG_READ_hIST_SUPPORTED)
   1.474 +      else if (!png_memcmp(chunk_name, png_hIST, 4))
   1.475 +         png_handle_hIST(png_ptr, info_ptr, length);
   1.476 +#endif
   1.477 +#if defined(PNG_READ_oFFs_SUPPORTED)
   1.478 +      else if (!png_memcmp(chunk_name, png_oFFs, 4))
   1.479 +         png_handle_oFFs(png_ptr, info_ptr, length);
   1.480 +#endif
   1.481 +#if defined(PNG_READ_pCAL_SUPPORTED)
   1.482 +      else if (!png_memcmp(chunk_name, png_pCAL, 4))
   1.483 +         png_handle_pCAL(png_ptr, info_ptr, length);
   1.484 +#endif
   1.485 +#if defined(PNG_READ_sCAL_SUPPORTED)
   1.486 +      else if (!png_memcmp(chunk_name, png_sCAL, 4))
   1.487 +         png_handle_sCAL(png_ptr, info_ptr, length);
   1.488 +#endif
   1.489 +#if defined(PNG_READ_pHYs_SUPPORTED)
   1.490 +      else if (!png_memcmp(chunk_name, png_pHYs, 4))
   1.491 +         png_handle_pHYs(png_ptr, info_ptr, length);
   1.492 +#endif
   1.493 +#if defined(PNG_READ_sBIT_SUPPORTED)
   1.494 +      else if (!png_memcmp(chunk_name, png_sBIT, 4))
   1.495 +         png_handle_sBIT(png_ptr, info_ptr, length);
   1.496 +#endif
   1.497 +#if defined(PNG_READ_sRGB_SUPPORTED)
   1.498 +      else if (!png_memcmp(chunk_name, png_sRGB, 4))
   1.499 +         png_handle_sRGB(png_ptr, info_ptr, length);
   1.500 +#endif
   1.501 +#if defined(PNG_READ_iCCP_SUPPORTED)
   1.502 +      else if (!png_memcmp(chunk_name, png_iCCP, 4))
   1.503 +         png_handle_iCCP(png_ptr, info_ptr, length);
   1.504 +#endif
   1.505 +#if defined(PNG_READ_sPLT_SUPPORTED)
   1.506 +      else if (!png_memcmp(chunk_name, png_sPLT, 4))
   1.507 +         png_handle_sPLT(png_ptr, info_ptr, length);
   1.508 +#endif
   1.509 +#if defined(PNG_READ_tEXt_SUPPORTED)
   1.510 +      else if (!png_memcmp(chunk_name, png_tEXt, 4))
   1.511 +         png_handle_tEXt(png_ptr, info_ptr, length);
   1.512 +#endif
   1.513 +#if defined(PNG_READ_tIME_SUPPORTED)
   1.514 +      else if (!png_memcmp(chunk_name, png_tIME, 4))
   1.515 +         png_handle_tIME(png_ptr, info_ptr, length);
   1.516 +#endif
   1.517 +#if defined(PNG_READ_tRNS_SUPPORTED)
   1.518 +      else if (!png_memcmp(chunk_name, png_tRNS, 4))
   1.519 +         png_handle_tRNS(png_ptr, info_ptr, length);
   1.520 +#endif
   1.521 +#if defined(PNG_READ_zTXt_SUPPORTED)
   1.522 +      else if (!png_memcmp(chunk_name, png_zTXt, 4))
   1.523 +         png_handle_zTXt(png_ptr, info_ptr, length);
   1.524 +#endif
   1.525 +#if defined(PNG_READ_iTXt_SUPPORTED)
   1.526 +      else if (!png_memcmp(chunk_name, png_iTXt, 4))
   1.527 +         png_handle_iTXt(png_ptr, info_ptr, length);
   1.528 +#endif
   1.529 +      else
   1.530 +         png_handle_unknown(png_ptr, info_ptr, length);
   1.531 +   }
   1.532 +}
   1.533 +#endif /* PNG_NO_SEQUENTIAL_READ_SUPPORTED */
   1.534 +
   1.535 +/* optional call to update the users info_ptr structure */
   1.536 +void PNGAPI
   1.537 +png_read_update_info(png_structp png_ptr, png_infop info_ptr)
   1.538 +{
   1.539 +   png_debug(1, "in png_read_update_info\n");
   1.540 +   if (png_ptr == NULL) return;
   1.541 +   if (!(png_ptr->flags & PNG_FLAG_ROW_INIT))
   1.542 +      png_read_start_row(png_ptr);
   1.543 +   else
   1.544 +      png_warning(png_ptr,
   1.545 +      "Ignoring extra png_read_update_info() call; row buffer not reallocated");
   1.546 +   png_read_transform_info(png_ptr, info_ptr);
   1.547 +}
   1.548 +
   1.549 +#ifndef PNG_NO_SEQUENTIAL_READ_SUPPORTED
   1.550 +/* Initialize palette, background, etc, after transformations
   1.551 + * are set, but before any reading takes place.  This allows
   1.552 + * the user to obtain a gamma-corrected palette, for example.
   1.553 + * If the user doesn't call this, we will do it ourselves.
   1.554 + */
   1.555 +void PNGAPI
   1.556 +png_start_read_image(png_structp png_ptr)
   1.557 +{
   1.558 +   png_debug(1, "in png_start_read_image\n");
   1.559 +   if (png_ptr == NULL) return;
   1.560 +   if (!(png_ptr->flags & PNG_FLAG_ROW_INIT))
   1.561 +      png_read_start_row(png_ptr);
   1.562 +}
   1.563 +#endif /* PNG_NO_SEQUENTIAL_READ_SUPPORTED */
   1.564 +
   1.565 +#ifndef PNG_NO_SEQUENTIAL_READ_SUPPORTED
   1.566 +void PNGAPI
   1.567 +png_read_row(png_structp png_ptr, png_bytep row, png_bytep dsp_row)
   1.568 +{
   1.569 +#ifdef PNG_USE_LOCAL_ARRAYS
   1.570 +   PNG_CONST PNG_IDAT;
   1.571 +   PNG_CONST int png_pass_dsp_mask[7] = {0xff, 0x0f, 0xff, 0x33, 0xff, 0x55,
   1.572 +      0xff};
   1.573 +   PNG_CONST int png_pass_mask[7] = {0x80, 0x08, 0x88, 0x22, 0xaa, 0x55, 0xff};
   1.574 +#endif
   1.575 +   int ret;
   1.576 +   if (png_ptr == NULL) return;
   1.577 +   png_debug2(1, "in png_read_row (row %lu, pass %d)\n",
   1.578 +      png_ptr->row_number, png_ptr->pass);
   1.579 +   if (!(png_ptr->flags & PNG_FLAG_ROW_INIT))
   1.580 +      png_read_start_row(png_ptr);
   1.581 +   if (png_ptr->row_number == 0 && png_ptr->pass == 0)
   1.582 +   {
   1.583 +   /* check for transforms that have been set but were defined out */
   1.584 +#if defined(PNG_WRITE_INVERT_SUPPORTED) && !defined(PNG_READ_INVERT_SUPPORTED)
   1.585 +   if (png_ptr->transformations & PNG_INVERT_MONO)
   1.586 +      png_warning(png_ptr, "PNG_READ_INVERT_SUPPORTED is not defined.");
   1.587 +#endif
   1.588 +#if defined(PNG_WRITE_FILLER_SUPPORTED) && !defined(PNG_READ_FILLER_SUPPORTED)
   1.589 +   if (png_ptr->transformations & PNG_FILLER)
   1.590 +      png_warning(png_ptr, "PNG_READ_FILLER_SUPPORTED is not defined.");
   1.591 +#endif
   1.592 +#if defined(PNG_WRITE_PACKSWAP_SUPPORTED) && !defined(PNG_READ_PACKSWAP_SUPPORTED)
   1.593 +   if (png_ptr->transformations & PNG_PACKSWAP)
   1.594 +      png_warning(png_ptr, "PNG_READ_PACKSWAP_SUPPORTED is not defined.");
   1.595 +#endif
   1.596 +#if defined(PNG_WRITE_PACK_SUPPORTED) && !defined(PNG_READ_PACK_SUPPORTED)
   1.597 +   if (png_ptr->transformations & PNG_PACK)
   1.598 +      png_warning(png_ptr, "PNG_READ_PACK_SUPPORTED is not defined.");
   1.599 +#endif
   1.600 +#if defined(PNG_WRITE_SHIFT_SUPPORTED) && !defined(PNG_READ_SHIFT_SUPPORTED)
   1.601 +   if (png_ptr->transformations & PNG_SHIFT)
   1.602 +      png_warning(png_ptr, "PNG_READ_SHIFT_SUPPORTED is not defined.");
   1.603 +#endif
   1.604 +#if defined(PNG_WRITE_BGR_SUPPORTED) && !defined(PNG_READ_BGR_SUPPORTED)
   1.605 +   if (png_ptr->transformations & PNG_BGR)
   1.606 +      png_warning(png_ptr, "PNG_READ_BGR_SUPPORTED is not defined.");
   1.607 +#endif
   1.608 +#if defined(PNG_WRITE_SWAP_SUPPORTED) && !defined(PNG_READ_SWAP_SUPPORTED)
   1.609 +   if (png_ptr->transformations & PNG_SWAP_BYTES)
   1.610 +      png_warning(png_ptr, "PNG_READ_SWAP_SUPPORTED is not defined.");
   1.611 +#endif
   1.612 +   }
   1.613 +
   1.614 +#if defined(PNG_READ_INTERLACING_SUPPORTED)
   1.615 +   /* if interlaced and we do not need a new row, combine row and return */
   1.616 +   if (png_ptr->interlaced && (png_ptr->transformations & PNG_INTERLACE))
   1.617 +   {
   1.618 +      switch (png_ptr->pass)
   1.619 +      {
   1.620 +         case 0:
   1.621 +            if (png_ptr->row_number & 0x07)
   1.622 +            {
   1.623 +               if (dsp_row != NULL)
   1.624 +                  png_combine_row(png_ptr, dsp_row,
   1.625 +                     png_pass_dsp_mask[png_ptr->pass]);
   1.626 +               png_read_finish_row(png_ptr);
   1.627 +               return;
   1.628 +            }
   1.629 +            break;
   1.630 +         case 1:
   1.631 +            if ((png_ptr->row_number & 0x07) || png_ptr->width < 5)
   1.632 +            {
   1.633 +               if (dsp_row != NULL)
   1.634 +                  png_combine_row(png_ptr, dsp_row,
   1.635 +                     png_pass_dsp_mask[png_ptr->pass]);
   1.636 +               png_read_finish_row(png_ptr);
   1.637 +               return;
   1.638 +            }
   1.639 +            break;
   1.640 +         case 2:
   1.641 +            if ((png_ptr->row_number & 0x07) != 4)
   1.642 +            {
   1.643 +               if (dsp_row != NULL && (png_ptr->row_number & 4))
   1.644 +                  png_combine_row(png_ptr, dsp_row,
   1.645 +                     png_pass_dsp_mask[png_ptr->pass]);
   1.646 +               png_read_finish_row(png_ptr);
   1.647 +               return;
   1.648 +            }
   1.649 +            break;
   1.650 +         case 3:
   1.651 +            if ((png_ptr->row_number & 3) || png_ptr->width < 3)
   1.652 +            {
   1.653 +               if (dsp_row != NULL)
   1.654 +                  png_combine_row(png_ptr, dsp_row,
   1.655 +                     png_pass_dsp_mask[png_ptr->pass]);
   1.656 +               png_read_finish_row(png_ptr);
   1.657 +               return;
   1.658 +            }
   1.659 +            break;
   1.660 +         case 4:
   1.661 +            if ((png_ptr->row_number & 3) != 2)
   1.662 +            {
   1.663 +               if (dsp_row != NULL && (png_ptr->row_number & 2))
   1.664 +                  png_combine_row(png_ptr, dsp_row,
   1.665 +                     png_pass_dsp_mask[png_ptr->pass]);
   1.666 +               png_read_finish_row(png_ptr);
   1.667 +               return;
   1.668 +            }
   1.669 +            break;
   1.670 +         case 5:
   1.671 +            if ((png_ptr->row_number & 1) || png_ptr->width < 2)
   1.672 +            {
   1.673 +               if (dsp_row != NULL)
   1.674 +                  png_combine_row(png_ptr, dsp_row,
   1.675 +                     png_pass_dsp_mask[png_ptr->pass]);
   1.676 +               png_read_finish_row(png_ptr);
   1.677 +               return;
   1.678 +            }
   1.679 +            break;
   1.680 +         case 6:
   1.681 +            if (!(png_ptr->row_number & 1))
   1.682 +            {
   1.683 +               png_read_finish_row(png_ptr);
   1.684 +               return;
   1.685 +            }
   1.686 +            break;
   1.687 +      }
   1.688 +   }
   1.689 +#endif
   1.690 +
   1.691 +   if (!(png_ptr->mode & PNG_HAVE_IDAT))
   1.692 +      png_error(png_ptr, "Invalid attempt to read row data");
   1.693 +
   1.694 +   png_ptr->zstream.next_out = png_ptr->row_buf;
   1.695 +   png_ptr->zstream.avail_out = (uInt)png_ptr->irowbytes;
   1.696 +   do
   1.697 +   {
   1.698 +      if (!(png_ptr->zstream.avail_in))
   1.699 +      {
   1.700 +         while (!png_ptr->idat_size)
   1.701 +         {
   1.702 +            png_crc_finish(png_ptr, 0);
   1.703 +
   1.704 +            png_ptr->idat_size = png_read_chunk_header(png_ptr);
   1.705 +            if (png_memcmp(png_ptr->chunk_name, png_IDAT, 4))
   1.706 +               png_error(png_ptr, "Not enough image data");
   1.707 +         }
   1.708 +         png_ptr->zstream.avail_in = (uInt)png_ptr->zbuf_size;
   1.709 +         png_ptr->zstream.next_in = png_ptr->zbuf;
   1.710 +         if (png_ptr->zbuf_size > png_ptr->idat_size)
   1.711 +            png_ptr->zstream.avail_in = (uInt)png_ptr->idat_size;
   1.712 +         png_crc_read(png_ptr, png_ptr->zbuf,
   1.713 +            (png_size_t)png_ptr->zstream.avail_in);
   1.714 +         png_ptr->idat_size -= png_ptr->zstream.avail_in;
   1.715 +      }
   1.716 +      ret = inflate(&png_ptr->zstream, Z_PARTIAL_FLUSH);
   1.717 +      if (ret == Z_STREAM_END)
   1.718 +      {
   1.719 +         if (png_ptr->zstream.avail_out || png_ptr->zstream.avail_in ||
   1.720 +            png_ptr->idat_size)
   1.721 +            png_error(png_ptr, "Extra compressed data");
   1.722 +         png_ptr->mode |= PNG_AFTER_IDAT;
   1.723 +         png_ptr->flags |= PNG_FLAG_ZLIB_FINISHED;
   1.724 +         break;
   1.725 +      }
   1.726 +      if (ret != Z_OK)
   1.727 +         png_error(png_ptr, png_ptr->zstream.msg ? png_ptr->zstream.msg :
   1.728 +                   "Decompression error");
   1.729 +
   1.730 +   } while (png_ptr->zstream.avail_out);
   1.731 +
   1.732 +   png_ptr->row_info.color_type = png_ptr->color_type;
   1.733 +   png_ptr->row_info.width = png_ptr->iwidth;
   1.734 +   png_ptr->row_info.channels = png_ptr->channels;
   1.735 +   png_ptr->row_info.bit_depth = png_ptr->bit_depth;
   1.736 +   png_ptr->row_info.pixel_depth = png_ptr->pixel_depth;
   1.737 +   png_ptr->row_info.rowbytes = PNG_ROWBYTES(png_ptr->row_info.pixel_depth,
   1.738 +       png_ptr->row_info.width);
   1.739 +
   1.740 +   if (png_ptr->row_buf[0])
   1.741 +   png_read_filter_row(png_ptr, &(png_ptr->row_info),
   1.742 +      png_ptr->row_buf + 1, png_ptr->prev_row + 1,
   1.743 +      (int)(png_ptr->row_buf[0]));
   1.744 +
   1.745 +   png_memcpy_check(png_ptr, png_ptr->prev_row, png_ptr->row_buf,
   1.746 +      png_ptr->rowbytes + 1);
   1.747 +
   1.748 +#if defined(PNG_MNG_FEATURES_SUPPORTED)
   1.749 +   if ((png_ptr->mng_features_permitted & PNG_FLAG_MNG_FILTER_64) &&
   1.750 +      (png_ptr->filter_type == PNG_INTRAPIXEL_DIFFERENCING))
   1.751 +   {
   1.752 +      /* Intrapixel differencing */
   1.753 +      png_do_read_intrapixel(&(png_ptr->row_info), png_ptr->row_buf + 1);
   1.754 +   }
   1.755 +#endif
   1.756 +
   1.757 +
   1.758 +   if (png_ptr->transformations || (png_ptr->flags&PNG_FLAG_STRIP_ALPHA))
   1.759 +      png_do_read_transformations(png_ptr);
   1.760 +
   1.761 +#if defined(PNG_READ_INTERLACING_SUPPORTED)
   1.762 +   /* blow up interlaced rows to full size */
   1.763 +   if (png_ptr->interlaced &&
   1.764 +      (png_ptr->transformations & PNG_INTERLACE))
   1.765 +   {
   1.766 +      if (png_ptr->pass < 6)
   1.767 +/*       old interface (pre-1.0.9):
   1.768 +         png_do_read_interlace(&(png_ptr->row_info),
   1.769 +            png_ptr->row_buf + 1, png_ptr->pass, png_ptr->transformations);
   1.770 + */
   1.771 +         png_do_read_interlace(png_ptr);
   1.772 +
   1.773 +      if (dsp_row != NULL)
   1.774 +         png_combine_row(png_ptr, dsp_row,
   1.775 +            png_pass_dsp_mask[png_ptr->pass]);
   1.776 +      if (row != NULL)
   1.777 +         png_combine_row(png_ptr, row,
   1.778 +            png_pass_mask[png_ptr->pass]);
   1.779 +   }
   1.780 +   else
   1.781 +#endif
   1.782 +   {
   1.783 +      if (row != NULL)
   1.784 +         png_combine_row(png_ptr, row, 0xff);
   1.785 +      if (dsp_row != NULL)
   1.786 +         png_combine_row(png_ptr, dsp_row, 0xff);
   1.787 +   }
   1.788 +   png_read_finish_row(png_ptr);
   1.789 +
   1.790 +   if (png_ptr->read_row_fn != NULL)
   1.791 +      (*(png_ptr->read_row_fn))(png_ptr, png_ptr->row_number, png_ptr->pass);
   1.792 +}
   1.793 +#endif /* PNG_NO_SEQUENTIAL_READ_SUPPORTED */
   1.794 +
   1.795 +#ifndef PNG_NO_SEQUENTIAL_READ_SUPPORTED
   1.796 +/* Read one or more rows of image data.  If the image is interlaced,
   1.797 + * and png_set_interlace_handling() has been called, the rows need to
   1.798 + * contain the contents of the rows from the previous pass.  If the
   1.799 + * image has alpha or transparency, and png_handle_alpha()[*] has been
   1.800 + * called, the rows contents must be initialized to the contents of the
   1.801 + * screen.
   1.802 + *
   1.803 + * "row" holds the actual image, and pixels are placed in it
   1.804 + * as they arrive.  If the image is displayed after each pass, it will
   1.805 + * appear to "sparkle" in.  "display_row" can be used to display a
   1.806 + * "chunky" progressive image, with finer detail added as it becomes
   1.807 + * available.  If you do not want this "chunky" display, you may pass
   1.808 + * NULL for display_row.  If you do not want the sparkle display, and
   1.809 + * you have not called png_handle_alpha(), you may pass NULL for rows.
   1.810 + * If you have called png_handle_alpha(), and the image has either an
   1.811 + * alpha channel or a transparency chunk, you must provide a buffer for
   1.812 + * rows.  In this case, you do not have to provide a display_row buffer
   1.813 + * also, but you may.  If the image is not interlaced, or if you have
   1.814 + * not called png_set_interlace_handling(), the display_row buffer will
   1.815 + * be ignored, so pass NULL to it.
   1.816 + *
   1.817 + * [*] png_handle_alpha() does not exist yet, as of this version of libpng
   1.818 + */
   1.819 +
   1.820 +void PNGAPI
   1.821 +png_read_rows(png_structp png_ptr, png_bytepp row,
   1.822 +   png_bytepp display_row, png_uint_32 num_rows)
   1.823 +{
   1.824 +   png_uint_32 i;
   1.825 +   png_bytepp rp;
   1.826 +   png_bytepp dp;
   1.827 +
   1.828 +   png_debug(1, "in png_read_rows\n");
   1.829 +   if (png_ptr == NULL) return;
   1.830 +   rp = row;
   1.831 +   dp = display_row;
   1.832 +   if (rp != NULL && dp != NULL)
   1.833 +      for (i = 0; i < num_rows; i++)
   1.834 +      {
   1.835 +         png_bytep rptr = *rp++;
   1.836 +         png_bytep dptr = *dp++;
   1.837 +
   1.838 +         png_read_row(png_ptr, rptr, dptr);
   1.839 +      }
   1.840 +   else if (rp != NULL)
   1.841 +      for (i = 0; i < num_rows; i++)
   1.842 +      {
   1.843 +         png_bytep rptr = *rp;
   1.844 +         png_read_row(png_ptr, rptr, png_bytep_NULL);
   1.845 +         rp++;
   1.846 +      }
   1.847 +   else if (dp != NULL)
   1.848 +      for (i = 0; i < num_rows; i++)
   1.849 +      {
   1.850 +         png_bytep dptr = *dp;
   1.851 +         png_read_row(png_ptr, png_bytep_NULL, dptr);
   1.852 +         dp++;
   1.853 +      }
   1.854 +}
   1.855 +#endif /* PNG_NO_SEQUENTIAL_READ_SUPPORTED */
   1.856 +
   1.857 +#ifndef PNG_NO_SEQUENTIAL_READ_SUPPORTED
   1.858 +/* Read the entire image.  If the image has an alpha channel or a tRNS
   1.859 + * chunk, and you have called png_handle_alpha()[*], you will need to
   1.860 + * initialize the image to the current image that PNG will be overlaying.
   1.861 + * We set the num_rows again here, in case it was incorrectly set in
   1.862 + * png_read_start_row() by a call to png_read_update_info() or
   1.863 + * png_start_read_image() if png_set_interlace_handling() wasn't called
   1.864 + * prior to either of these functions like it should have been.  You can
   1.865 + * only call this function once.  If you desire to have an image for
   1.866 + * each pass of a interlaced image, use png_read_rows() instead.
   1.867 + *
   1.868 + * [*] png_handle_alpha() does not exist yet, as of this version of libpng
   1.869 + */
   1.870 +void PNGAPI
   1.871 +png_read_image(png_structp png_ptr, png_bytepp image)
   1.872 +{
   1.873 +   png_uint_32 i, image_height;
   1.874 +   int pass, j;
   1.875 +   png_bytepp rp;
   1.876 +
   1.877 +   png_debug(1, "in png_read_image\n");
   1.878 +   if (png_ptr == NULL) return;
   1.879 +
   1.880 +#ifdef PNG_READ_INTERLACING_SUPPORTED
   1.881 +   pass = png_set_interlace_handling(png_ptr);
   1.882 +#else
   1.883 +   if (png_ptr->interlaced)
   1.884 +      png_error(png_ptr,
   1.885 +        "Cannot read interlaced image -- interlace handler disabled.");
   1.886 +   pass = 1;
   1.887 +#endif
   1.888 +
   1.889 +
   1.890 +   image_height=png_ptr->height;
   1.891 +   png_ptr->num_rows = image_height; /* Make sure this is set correctly */
   1.892 +
   1.893 +   for (j = 0; j < pass; j++)
   1.894 +   {
   1.895 +      rp = image;
   1.896 +      for (i = 0; i < image_height; i++)
   1.897 +      {
   1.898 +         png_read_row(png_ptr, *rp, png_bytep_NULL);
   1.899 +         rp++;
   1.900 +      }
   1.901 +   }
   1.902 +}
   1.903 +#endif /* PNG_NO_SEQUENTIAL_READ_SUPPORTED */
   1.904 +
   1.905 +#ifndef PNG_NO_SEQUENTIAL_READ_SUPPORTED
   1.906 +/* Read the end of the PNG file.  Will not read past the end of the
   1.907 + * file, will verify the end is accurate, and will read any comments
   1.908 + * or time information at the end of the file, if info is not NULL.
   1.909 + */
   1.910 +void PNGAPI
   1.911 +png_read_end(png_structp png_ptr, png_infop info_ptr)
   1.912 +{
   1.913 +   png_debug(1, "in png_read_end\n");
   1.914 +   if (png_ptr == NULL) return;
   1.915 +   png_crc_finish(png_ptr, 0); /* Finish off CRC from last IDAT chunk */
   1.916 +
   1.917 +   do
   1.918 +   {
   1.919 +#ifdef PNG_USE_LOCAL_ARRAYS
   1.920 +      PNG_CONST PNG_IHDR;
   1.921 +      PNG_CONST PNG_IDAT;
   1.922 +      PNG_CONST PNG_IEND;
   1.923 +      PNG_CONST PNG_PLTE;
   1.924 +#if defined(PNG_READ_bKGD_SUPPORTED)
   1.925 +      PNG_CONST PNG_bKGD;
   1.926 +#endif
   1.927 +#if defined(PNG_READ_cHRM_SUPPORTED)
   1.928 +      PNG_CONST PNG_cHRM;
   1.929 +#endif
   1.930 +#if defined(PNG_READ_gAMA_SUPPORTED)
   1.931 +      PNG_CONST PNG_gAMA;
   1.932 +#endif
   1.933 +#if defined(PNG_READ_hIST_SUPPORTED)
   1.934 +      PNG_CONST PNG_hIST;
   1.935 +#endif
   1.936 +#if defined(PNG_READ_iCCP_SUPPORTED)
   1.937 +      PNG_CONST PNG_iCCP;
   1.938 +#endif
   1.939 +#if defined(PNG_READ_iTXt_SUPPORTED)
   1.940 +      PNG_CONST PNG_iTXt;
   1.941 +#endif
   1.942 +#if defined(PNG_READ_oFFs_SUPPORTED)
   1.943 +      PNG_CONST PNG_oFFs;
   1.944 +#endif
   1.945 +#if defined(PNG_READ_pCAL_SUPPORTED)
   1.946 +      PNG_CONST PNG_pCAL;
   1.947 +#endif
   1.948 +#if defined(PNG_READ_pHYs_SUPPORTED)
   1.949 +      PNG_CONST PNG_pHYs;
   1.950 +#endif
   1.951 +#if defined(PNG_READ_sBIT_SUPPORTED)
   1.952 +      PNG_CONST PNG_sBIT;
   1.953 +#endif
   1.954 +#if defined(PNG_READ_sCAL_SUPPORTED)
   1.955 +      PNG_CONST PNG_sCAL;
   1.956 +#endif
   1.957 +#if defined(PNG_READ_sPLT_SUPPORTED)
   1.958 +      PNG_CONST PNG_sPLT;
   1.959 +#endif
   1.960 +#if defined(PNG_READ_sRGB_SUPPORTED)
   1.961 +      PNG_CONST PNG_sRGB;
   1.962 +#endif
   1.963 +#if defined(PNG_READ_tEXt_SUPPORTED)
   1.964 +      PNG_CONST PNG_tEXt;
   1.965 +#endif
   1.966 +#if defined(PNG_READ_tIME_SUPPORTED)
   1.967 +      PNG_CONST PNG_tIME;
   1.968 +#endif
   1.969 +#if defined(PNG_READ_tRNS_SUPPORTED)
   1.970 +      PNG_CONST PNG_tRNS;
   1.971 +#endif
   1.972 +#if defined(PNG_READ_zTXt_SUPPORTED)
   1.973 +      PNG_CONST PNG_zTXt;
   1.974 +#endif
   1.975 +#endif /* PNG_USE_LOCAL_ARRAYS */
   1.976 +      png_uint_32 length = png_read_chunk_header(png_ptr);
   1.977 +      PNG_CONST png_bytep chunk_name = png_ptr->chunk_name;
   1.978 +
   1.979 +      if (!png_memcmp(chunk_name, png_IHDR, 4))
   1.980 +         png_handle_IHDR(png_ptr, info_ptr, length);
   1.981 +      else if (!png_memcmp(chunk_name, png_IEND, 4))
   1.982 +         png_handle_IEND(png_ptr, info_ptr, length);
   1.983 +#ifdef PNG_HANDLE_AS_UNKNOWN_SUPPORTED
   1.984 +      else if (png_handle_as_unknown(png_ptr, chunk_name))
   1.985 +      {
   1.986 +         if (!png_memcmp(chunk_name, png_IDAT, 4))
   1.987 +         {
   1.988 +            if ((length > 0) || (png_ptr->mode & PNG_HAVE_CHUNK_AFTER_IDAT))
   1.989 +               png_error(png_ptr, "Too many IDAT's found");
   1.990 +         }
   1.991 +         png_handle_unknown(png_ptr, info_ptr, length);
   1.992 +         if (!png_memcmp(chunk_name, png_PLTE, 4))
   1.993 +            png_ptr->mode |= PNG_HAVE_PLTE;
   1.994 +      }
   1.995 +#endif
   1.996 +      else if (!png_memcmp(chunk_name, png_IDAT, 4))
   1.997 +      {
   1.998 +         /* Zero length IDATs are legal after the last IDAT has been
   1.999 +          * read, but not after other chunks have been read.
  1.1000 +          */
  1.1001 +         if ((length > 0) || (png_ptr->mode & PNG_HAVE_CHUNK_AFTER_IDAT))
  1.1002 +            png_error(png_ptr, "Too many IDAT's found");
  1.1003 +         png_crc_finish(png_ptr, length);
  1.1004 +      }
  1.1005 +      else if (!png_memcmp(chunk_name, png_PLTE, 4))
  1.1006 +         png_handle_PLTE(png_ptr, info_ptr, length);
  1.1007 +#if defined(PNG_READ_bKGD_SUPPORTED)
  1.1008 +      else if (!png_memcmp(chunk_name, png_bKGD, 4))
  1.1009 +         png_handle_bKGD(png_ptr, info_ptr, length);
  1.1010 +#endif
  1.1011 +#if defined(PNG_READ_cHRM_SUPPORTED)
  1.1012 +      else if (!png_memcmp(chunk_name, png_cHRM, 4))
  1.1013 +         png_handle_cHRM(png_ptr, info_ptr, length);
  1.1014 +#endif
  1.1015 +#if defined(PNG_READ_gAMA_SUPPORTED)
  1.1016 +      else if (!png_memcmp(chunk_name, png_gAMA, 4))
  1.1017 +         png_handle_gAMA(png_ptr, info_ptr, length);
  1.1018 +#endif
  1.1019 +#if defined(PNG_READ_hIST_SUPPORTED)
  1.1020 +      else if (!png_memcmp(chunk_name, png_hIST, 4))
  1.1021 +         png_handle_hIST(png_ptr, info_ptr, length);
  1.1022 +#endif
  1.1023 +#if defined(PNG_READ_oFFs_SUPPORTED)
  1.1024 +      else if (!png_memcmp(chunk_name, png_oFFs, 4))
  1.1025 +         png_handle_oFFs(png_ptr, info_ptr, length);
  1.1026 +#endif
  1.1027 +#if defined(PNG_READ_pCAL_SUPPORTED)
  1.1028 +      else if (!png_memcmp(chunk_name, png_pCAL, 4))
  1.1029 +         png_handle_pCAL(png_ptr, info_ptr, length);
  1.1030 +#endif
  1.1031 +#if defined(PNG_READ_sCAL_SUPPORTED)
  1.1032 +      else if (!png_memcmp(chunk_name, png_sCAL, 4))
  1.1033 +         png_handle_sCAL(png_ptr, info_ptr, length);
  1.1034 +#endif
  1.1035 +#if defined(PNG_READ_pHYs_SUPPORTED)
  1.1036 +      else if (!png_memcmp(chunk_name, png_pHYs, 4))
  1.1037 +         png_handle_pHYs(png_ptr, info_ptr, length);
  1.1038 +#endif
  1.1039 +#if defined(PNG_READ_sBIT_SUPPORTED)
  1.1040 +      else if (!png_memcmp(chunk_name, png_sBIT, 4))
  1.1041 +         png_handle_sBIT(png_ptr, info_ptr, length);
  1.1042 +#endif
  1.1043 +#if defined(PNG_READ_sRGB_SUPPORTED)
  1.1044 +      else if (!png_memcmp(chunk_name, png_sRGB, 4))
  1.1045 +         png_handle_sRGB(png_ptr, info_ptr, length);
  1.1046 +#endif
  1.1047 +#if defined(PNG_READ_iCCP_SUPPORTED)
  1.1048 +      else if (!png_memcmp(chunk_name, png_iCCP, 4))
  1.1049 +         png_handle_iCCP(png_ptr, info_ptr, length);
  1.1050 +#endif
  1.1051 +#if defined(PNG_READ_sPLT_SUPPORTED)
  1.1052 +      else if (!png_memcmp(chunk_name, png_sPLT, 4))
  1.1053 +         png_handle_sPLT(png_ptr, info_ptr, length);
  1.1054 +#endif
  1.1055 +#if defined(PNG_READ_tEXt_SUPPORTED)
  1.1056 +      else if (!png_memcmp(chunk_name, png_tEXt, 4))
  1.1057 +         png_handle_tEXt(png_ptr, info_ptr, length);
  1.1058 +#endif
  1.1059 +#if defined(PNG_READ_tIME_SUPPORTED)
  1.1060 +      else if (!png_memcmp(chunk_name, png_tIME, 4))
  1.1061 +         png_handle_tIME(png_ptr, info_ptr, length);
  1.1062 +#endif
  1.1063 +#if defined(PNG_READ_tRNS_SUPPORTED)
  1.1064 +      else if (!png_memcmp(chunk_name, png_tRNS, 4))
  1.1065 +         png_handle_tRNS(png_ptr, info_ptr, length);
  1.1066 +#endif
  1.1067 +#if defined(PNG_READ_zTXt_SUPPORTED)
  1.1068 +      else if (!png_memcmp(chunk_name, png_zTXt, 4))
  1.1069 +         png_handle_zTXt(png_ptr, info_ptr, length);
  1.1070 +#endif
  1.1071 +#if defined(PNG_READ_iTXt_SUPPORTED)
  1.1072 +      else if (!png_memcmp(chunk_name, png_iTXt, 4))
  1.1073 +         png_handle_iTXt(png_ptr, info_ptr, length);
  1.1074 +#endif
  1.1075 +      else
  1.1076 +         png_handle_unknown(png_ptr, info_ptr, length);
  1.1077 +   } while (!(png_ptr->mode & PNG_HAVE_IEND));
  1.1078 +}
  1.1079 +#endif /* PNG_NO_SEQUENTIAL_READ_SUPPORTED */
  1.1080 +
  1.1081 +/* free all memory used by the read */
  1.1082 +void PNGAPI
  1.1083 +png_destroy_read_struct(png_structpp png_ptr_ptr, png_infopp info_ptr_ptr,
  1.1084 +   png_infopp end_info_ptr_ptr)
  1.1085 +{
  1.1086 +   png_structp png_ptr = NULL;
  1.1087 +   png_infop info_ptr = NULL, end_info_ptr = NULL;
  1.1088 +#ifdef PNG_USER_MEM_SUPPORTED
  1.1089 +   png_free_ptr free_fn = NULL;
  1.1090 +   png_voidp mem_ptr = NULL;
  1.1091 +#endif
  1.1092 +
  1.1093 +   png_debug(1, "in png_destroy_read_struct\n");
  1.1094 +   if (png_ptr_ptr != NULL)
  1.1095 +      png_ptr = *png_ptr_ptr;
  1.1096 +   if (png_ptr == NULL)
  1.1097 +      return;
  1.1098 +
  1.1099 +#ifdef PNG_USER_MEM_SUPPORTED
  1.1100 +   free_fn = png_ptr->free_fn;
  1.1101 +   mem_ptr = png_ptr->mem_ptr;
  1.1102 +#endif
  1.1103 +
  1.1104 +   if (info_ptr_ptr != NULL)
  1.1105 +      info_ptr = *info_ptr_ptr;
  1.1106 +
  1.1107 +   if (end_info_ptr_ptr != NULL)
  1.1108 +      end_info_ptr = *end_info_ptr_ptr;
  1.1109 +
  1.1110 +   png_read_destroy(png_ptr, info_ptr, end_info_ptr);
  1.1111 +
  1.1112 +   if (info_ptr != NULL)
  1.1113 +   {
  1.1114 +#if defined(PNG_TEXT_SUPPORTED)
  1.1115 +      png_free_data(png_ptr, info_ptr, PNG_FREE_TEXT, -1);
  1.1116 +#endif
  1.1117 +
  1.1118 +#ifdef PNG_USER_MEM_SUPPORTED
  1.1119 +      png_destroy_struct_2((png_voidp)info_ptr, (png_free_ptr)free_fn,
  1.1120 +          (png_voidp)mem_ptr);
  1.1121 +#else
  1.1122 +      png_destroy_struct((png_voidp)info_ptr);
  1.1123 +#endif
  1.1124 +      *info_ptr_ptr = NULL;
  1.1125 +   }
  1.1126 +
  1.1127 +   if (end_info_ptr != NULL)
  1.1128 +   {
  1.1129 +#if defined(PNG_READ_TEXT_SUPPORTED)
  1.1130 +      png_free_data(png_ptr, end_info_ptr, PNG_FREE_TEXT, -1);
  1.1131 +#endif
  1.1132 +#ifdef PNG_USER_MEM_SUPPORTED
  1.1133 +      png_destroy_struct_2((png_voidp)end_info_ptr, (png_free_ptr)free_fn,
  1.1134 +         (png_voidp)mem_ptr);
  1.1135 +#else
  1.1136 +      png_destroy_struct((png_voidp)end_info_ptr);
  1.1137 +#endif
  1.1138 +      *end_info_ptr_ptr = NULL;
  1.1139 +   }
  1.1140 +
  1.1141 +   if (png_ptr != NULL)
  1.1142 +   {
  1.1143 +#ifdef PNG_USER_MEM_SUPPORTED
  1.1144 +      png_destroy_struct_2((png_voidp)png_ptr, (png_free_ptr)free_fn,
  1.1145 +          (png_voidp)mem_ptr);
  1.1146 +#else
  1.1147 +      png_destroy_struct((png_voidp)png_ptr);
  1.1148 +#endif
  1.1149 +      *png_ptr_ptr = NULL;
  1.1150 +   }
  1.1151 +}
  1.1152 +
  1.1153 +/* free all memory used by the read (old method) */
  1.1154 +void /* PRIVATE */
  1.1155 +png_read_destroy(png_structp png_ptr, png_infop info_ptr, png_infop end_info_ptr)
  1.1156 +{
  1.1157 +#ifdef PNG_SETJMP_SUPPORTED
  1.1158 +   jmp_buf tmp_jmp;
  1.1159 +#endif
  1.1160 +   png_error_ptr error_fn;
  1.1161 +   png_error_ptr warning_fn;
  1.1162 +   png_voidp error_ptr;
  1.1163 +#ifdef PNG_USER_MEM_SUPPORTED
  1.1164 +   png_free_ptr free_fn;
  1.1165 +#endif
  1.1166 +
  1.1167 +   png_debug(1, "in png_read_destroy\n");
  1.1168 +   if (info_ptr != NULL)
  1.1169 +      png_info_destroy(png_ptr, info_ptr);
  1.1170 +
  1.1171 +   if (end_info_ptr != NULL)
  1.1172 +      png_info_destroy(png_ptr, end_info_ptr);
  1.1173 +
  1.1174 +   png_free(png_ptr, png_ptr->zbuf);
  1.1175 +   png_free(png_ptr, png_ptr->big_row_buf);
  1.1176 +   png_free(png_ptr, png_ptr->prev_row);
  1.1177 +   png_free(png_ptr, png_ptr->chunkdata);
  1.1178 +#if defined(PNG_READ_DITHER_SUPPORTED)
  1.1179 +   png_free(png_ptr, png_ptr->palette_lookup);
  1.1180 +   png_free(png_ptr, png_ptr->dither_index);
  1.1181 +#endif
  1.1182 +#if defined(PNG_READ_GAMMA_SUPPORTED)
  1.1183 +   png_free(png_ptr, png_ptr->gamma_table);
  1.1184 +#endif
  1.1185 +#if defined(PNG_READ_BACKGROUND_SUPPORTED)
  1.1186 +   png_free(png_ptr, png_ptr->gamma_from_1);
  1.1187 +   png_free(png_ptr, png_ptr->gamma_to_1);
  1.1188 +#endif
  1.1189 +#ifdef PNG_FREE_ME_SUPPORTED
  1.1190 +   if (png_ptr->free_me & PNG_FREE_PLTE)
  1.1191 +      png_zfree(png_ptr, png_ptr->palette);
  1.1192 +   png_ptr->free_me &= ~PNG_FREE_PLTE;
  1.1193 +#else
  1.1194 +   if (png_ptr->flags & PNG_FLAG_FREE_PLTE)
  1.1195 +      png_zfree(png_ptr, png_ptr->palette);
  1.1196 +   png_ptr->flags &= ~PNG_FLAG_FREE_PLTE;
  1.1197 +#endif
  1.1198 +#if defined(PNG_tRNS_SUPPORTED) || \
  1.1199 +    defined(PNG_READ_EXPAND_SUPPORTED) || defined(PNG_READ_BACKGROUND_SUPPORTED)
  1.1200 +#ifdef PNG_FREE_ME_SUPPORTED
  1.1201 +   if (png_ptr->free_me & PNG_FREE_TRNS)
  1.1202 +      png_free(png_ptr, png_ptr->trans);
  1.1203 +   png_ptr->free_me &= ~PNG_FREE_TRNS;
  1.1204 +#else
  1.1205 +   if (png_ptr->flags & PNG_FLAG_FREE_TRNS)
  1.1206 +      png_free(png_ptr, png_ptr->trans);
  1.1207 +   png_ptr->flags &= ~PNG_FLAG_FREE_TRNS;
  1.1208 +#endif
  1.1209 +#endif
  1.1210 +#if defined(PNG_READ_hIST_SUPPORTED)
  1.1211 +#ifdef PNG_FREE_ME_SUPPORTED
  1.1212 +   if (png_ptr->free_me & PNG_FREE_HIST)
  1.1213 +      png_free(png_ptr, png_ptr->hist);
  1.1214 +   png_ptr->free_me &= ~PNG_FREE_HIST;
  1.1215 +#else
  1.1216 +   if (png_ptr->flags & PNG_FLAG_FREE_HIST)
  1.1217 +      png_free(png_ptr, png_ptr->hist);
  1.1218 +   png_ptr->flags &= ~PNG_FLAG_FREE_HIST;
  1.1219 +#endif
  1.1220 +#endif
  1.1221 +#if defined(PNG_READ_GAMMA_SUPPORTED)
  1.1222 +   if (png_ptr->gamma_16_table != NULL)
  1.1223 +   {
  1.1224 +      int i;
  1.1225 +      int istop = (1 << (8 - png_ptr->gamma_shift));
  1.1226 +      for (i = 0; i < istop; i++)
  1.1227 +      {
  1.1228 +         png_free(png_ptr, png_ptr->gamma_16_table[i]);
  1.1229 +      }
  1.1230 +   png_free(png_ptr, png_ptr->gamma_16_table);
  1.1231 +   }
  1.1232 +#if defined(PNG_READ_BACKGROUND_SUPPORTED)
  1.1233 +   if (png_ptr->gamma_16_from_1 != NULL)
  1.1234 +   {
  1.1235 +      int i;
  1.1236 +      int istop = (1 << (8 - png_ptr->gamma_shift));
  1.1237 +      for (i = 0; i < istop; i++)
  1.1238 +      {
  1.1239 +         png_free(png_ptr, png_ptr->gamma_16_from_1[i]);
  1.1240 +      }
  1.1241 +   png_free(png_ptr, png_ptr->gamma_16_from_1);
  1.1242 +   }
  1.1243 +   if (png_ptr->gamma_16_to_1 != NULL)
  1.1244 +   {
  1.1245 +      int i;
  1.1246 +      int istop = (1 << (8 - png_ptr->gamma_shift));
  1.1247 +      for (i = 0; i < istop; i++)
  1.1248 +      {
  1.1249 +         png_free(png_ptr, png_ptr->gamma_16_to_1[i]);
  1.1250 +      }
  1.1251 +   png_free(png_ptr, png_ptr->gamma_16_to_1);
  1.1252 +   }
  1.1253 +#endif
  1.1254 +#endif
  1.1255 +#if defined(PNG_TIME_RFC1123_SUPPORTED)
  1.1256 +   png_free(png_ptr, png_ptr->time_buffer);
  1.1257 +#endif
  1.1258 +
  1.1259 +   inflateEnd(&png_ptr->zstream);
  1.1260 +#ifdef PNG_PROGRESSIVE_READ_SUPPORTED
  1.1261 +   png_free(png_ptr, png_ptr->save_buffer);
  1.1262 +#endif
  1.1263 +
  1.1264 +#ifdef PNG_PROGRESSIVE_READ_SUPPORTED
  1.1265 +#ifdef PNG_TEXT_SUPPORTED
  1.1266 +   png_free(png_ptr, png_ptr->current_text);
  1.1267 +#endif /* PNG_TEXT_SUPPORTED */
  1.1268 +#endif /* PNG_PROGRESSIVE_READ_SUPPORTED */
  1.1269 +
  1.1270 +   /* Save the important info out of the png_struct, in case it is
  1.1271 +    * being used again.
  1.1272 +    */
  1.1273 +#ifdef PNG_SETJMP_SUPPORTED
  1.1274 +   png_memcpy(tmp_jmp, png_ptr->jmpbuf, png_sizeof(jmp_buf));
  1.1275 +#endif
  1.1276 +
  1.1277 +   error_fn = png_ptr->error_fn;
  1.1278 +   warning_fn = png_ptr->warning_fn;
  1.1279 +   error_ptr = png_ptr->error_ptr;
  1.1280 +#ifdef PNG_USER_MEM_SUPPORTED
  1.1281 +   free_fn = png_ptr->free_fn;
  1.1282 +#endif
  1.1283 +
  1.1284 +   png_memset(png_ptr, 0, png_sizeof(png_struct));
  1.1285 +
  1.1286 +   png_ptr->error_fn = error_fn;
  1.1287 +   png_ptr->warning_fn = warning_fn;
  1.1288 +   png_ptr->error_ptr = error_ptr;
  1.1289 +#ifdef PNG_USER_MEM_SUPPORTED
  1.1290 +   png_ptr->free_fn = free_fn;
  1.1291 +#endif
  1.1292 +
  1.1293 +#ifdef PNG_SETJMP_SUPPORTED
  1.1294 +   png_memcpy(png_ptr->jmpbuf, tmp_jmp, png_sizeof(jmp_buf));
  1.1295 +#endif
  1.1296 +
  1.1297 +}
  1.1298 +
  1.1299 +void PNGAPI
  1.1300 +png_set_read_status_fn(png_structp png_ptr, png_read_status_ptr read_row_fn)
  1.1301 +{
  1.1302 +   if (png_ptr == NULL) return;
  1.1303 +   png_ptr->read_row_fn = read_row_fn;
  1.1304 +}
  1.1305 +
  1.1306 +
  1.1307 +#ifndef PNG_NO_SEQUENTIAL_READ_SUPPORTED
  1.1308 +#if defined(PNG_INFO_IMAGE_SUPPORTED)
  1.1309 +void PNGAPI
  1.1310 +png_read_png(png_structp png_ptr, png_infop info_ptr,
  1.1311 +                           int transforms,
  1.1312 +                           voidp params)
  1.1313 +{
  1.1314 +   int row;
  1.1315 +
  1.1316 +   if (png_ptr == NULL) return;
  1.1317 +#if defined(PNG_READ_INVERT_ALPHA_SUPPORTED)
  1.1318 +   /* invert the alpha channel from opacity to transparency
  1.1319 +    */
  1.1320 +   if (transforms & PNG_TRANSFORM_INVERT_ALPHA)
  1.1321 +       png_set_invert_alpha(png_ptr);
  1.1322 +#endif
  1.1323 +
  1.1324 +   /* png_read_info() gives us all of the information from the
  1.1325 +    * PNG file before the first IDAT (image data chunk).
  1.1326 +    */
  1.1327 +   png_read_info(png_ptr, info_ptr);
  1.1328 +   if (info_ptr->height > PNG_UINT_32_MAX/png_sizeof(png_bytep))
  1.1329 +      png_error(png_ptr, "Image is too high to process with png_read_png()");
  1.1330 +
  1.1331 +   /* -------------- image transformations start here ------------------- */
  1.1332 +
  1.1333 +#if defined(PNG_READ_16_TO_8_SUPPORTED)
  1.1334 +   /* tell libpng to strip 16 bit/color files down to 8 bits per color
  1.1335 +    */
  1.1336 +   if (transforms & PNG_TRANSFORM_STRIP_16)
  1.1337 +       png_set_strip_16(png_ptr);
  1.1338 +#endif
  1.1339 +
  1.1340 +#if defined(PNG_READ_STRIP_ALPHA_SUPPORTED)
  1.1341 +   /* Strip alpha bytes from the input data without combining with
  1.1342 +    * the background (not recommended).
  1.1343 +    */
  1.1344 +   if (transforms & PNG_TRANSFORM_STRIP_ALPHA)
  1.1345 +       png_set_strip_alpha(png_ptr);
  1.1346 +#endif
  1.1347 +
  1.1348 +#if defined(PNG_READ_PACK_SUPPORTED) && !defined(PNG_READ_EXPAND_SUPPORTED)
  1.1349 +   /* Extract multiple pixels with bit depths of 1, 2, or 4 from a single
  1.1350 +    * byte into separate bytes (useful for paletted and grayscale images).
  1.1351 +    */
  1.1352 +   if (transforms & PNG_TRANSFORM_PACKING)
  1.1353 +       png_set_packing(png_ptr);
  1.1354 +#endif
  1.1355 +
  1.1356 +#if defined(PNG_READ_PACKSWAP_SUPPORTED)
  1.1357 +   /* Change the order of packed pixels to least significant bit first
  1.1358 +    * (not useful if you are using png_set_packing).
  1.1359 +    */
  1.1360 +   if (transforms & PNG_TRANSFORM_PACKSWAP)
  1.1361 +       png_set_packswap(png_ptr);
  1.1362 +#endif
  1.1363 +
  1.1364 +#if defined(PNG_READ_EXPAND_SUPPORTED)
  1.1365 +   /* Expand paletted colors into true RGB triplets
  1.1366 +    * Expand grayscale images to full 8 bits from 1, 2, or 4 bits/pixel
  1.1367 +    * Expand paletted or RGB images with transparency to full alpha
  1.1368 +    * channels so the data will be available as RGBA quartets.
  1.1369 +    */
  1.1370 +   if (transforms & PNG_TRANSFORM_EXPAND)
  1.1371 +       if ((png_ptr->bit_depth < 8) ||
  1.1372 +           (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE) ||
  1.1373 +           (png_get_valid(png_ptr, info_ptr, PNG_INFO_tRNS)))
  1.1374 +         png_set_expand(png_ptr);
  1.1375 +#endif
  1.1376 +
  1.1377 +   /* We don't handle background color or gamma transformation or dithering.
  1.1378 +    */
  1.1379 +
  1.1380 +#if defined(PNG_READ_INVERT_SUPPORTED)
  1.1381 +   /* invert monochrome files to have 0 as white and 1 as black
  1.1382 +    */
  1.1383 +   if (transforms & PNG_TRANSFORM_INVERT_MONO)
  1.1384 +       png_set_invert_mono(png_ptr);
  1.1385 +#endif
  1.1386 +
  1.1387 +#if defined(PNG_READ_SHIFT_SUPPORTED)
  1.1388 +   /* If you want to shift the pixel values from the range [0,255] or
  1.1389 +    * [0,65535] to the original [0,7] or [0,31], or whatever range the
  1.1390 +    * colors were originally in:
  1.1391 +    */
  1.1392 +   if ((transforms & PNG_TRANSFORM_SHIFT)
  1.1393 +       && png_get_valid(png_ptr, info_ptr, PNG_INFO_sBIT))
  1.1394 +   {
  1.1395 +      png_color_8p sig_bit;
  1.1396 +
  1.1397 +      png_get_sBIT(png_ptr, info_ptr, &sig_bit);
  1.1398 +      png_set_shift(png_ptr, sig_bit);
  1.1399 +   }
  1.1400 +#endif
  1.1401 +
  1.1402 +#if defined(PNG_READ_BGR_SUPPORTED)
  1.1403 +   /* flip the RGB pixels to BGR (or RGBA to BGRA)
  1.1404 +    */
  1.1405 +   if (transforms & PNG_TRANSFORM_BGR)
  1.1406 +       png_set_bgr(png_ptr);
  1.1407 +#endif
  1.1408 +
  1.1409 +#if defined(PNG_READ_SWAP_ALPHA_SUPPORTED)
  1.1410 +   /* swap the RGBA or GA data to ARGB or AG (or BGRA to ABGR)
  1.1411 +    */
  1.1412 +   if (transforms & PNG_TRANSFORM_SWAP_ALPHA)
  1.1413 +       png_set_swap_alpha(png_ptr);
  1.1414 +#endif
  1.1415 +
  1.1416 +#if defined(PNG_READ_SWAP_SUPPORTED)
  1.1417 +   /* swap bytes of 16 bit files to least significant byte first
  1.1418 +    */
  1.1419 +   if (transforms & PNG_TRANSFORM_SWAP_ENDIAN)
  1.1420 +       png_set_swap(png_ptr);
  1.1421 +#endif
  1.1422 +
  1.1423 +   /* We don't handle adding filler bytes */
  1.1424 +
  1.1425 +   /* Optional call to gamma correct and add the background to the palette
  1.1426 +    * and update info structure.  REQUIRED if you are expecting libpng to
  1.1427 +    * update the palette for you (i.e., you selected such a transform above).
  1.1428 +    */
  1.1429 +   png_read_update_info(png_ptr, info_ptr);
  1.1430 +
  1.1431 +   /* -------------- image transformations end here ------------------- */
  1.1432 +
  1.1433 +#ifdef PNG_FREE_ME_SUPPORTED
  1.1434 +   png_free_data(png_ptr, info_ptr, PNG_FREE_ROWS, 0);
  1.1435 +#endif
  1.1436 +   if (info_ptr->row_pointers == NULL)
  1.1437 +   {
  1.1438 +      info_ptr->row_pointers = (png_bytepp)png_malloc(png_ptr,
  1.1439 +         info_ptr->height * png_sizeof(png_bytep));
  1.1440 +#ifdef PNG_FREE_ME_SUPPORTED
  1.1441 +      info_ptr->free_me |= PNG_FREE_ROWS;
  1.1442 +#endif
  1.1443 +      for (row = 0; row < (int)info_ptr->height; row++)
  1.1444 +      {
  1.1445 +         info_ptr->row_pointers[row] = (png_bytep)png_malloc(png_ptr,
  1.1446 +            png_get_rowbytes(png_ptr, info_ptr));
  1.1447 +      }
  1.1448 +   }
  1.1449 +
  1.1450 +   png_read_image(png_ptr, info_ptr->row_pointers);
  1.1451 +   info_ptr->valid |= PNG_INFO_IDAT;
  1.1452 +
  1.1453 +   /* read rest of file, and get additional chunks in info_ptr - REQUIRED */
  1.1454 +   png_read_end(png_ptr, info_ptr);
  1.1455 +
  1.1456 +   transforms = transforms; /* quiet compiler warnings */
  1.1457 +   params = params;
  1.1458 +
  1.1459 +}
  1.1460 +#endif /* PNG_INFO_IMAGE_SUPPORTED */
  1.1461 +#endif /* PNG_NO_SEQUENTIAL_READ_SUPPORTED */
  1.1462 +#endif /* PNG_READ_SUPPORTED */