Update libpng from 1.6.3 to 1.6.10

Change-Id: I76e81e7fd267d15991cd342c5caeb2fe77964ebf
diff --git a/pngread.c b/pngread.c
index aabf1f5..cd9ab58 100644
--- a/pngread.c
+++ b/pngread.c
@@ -1,8 +1,8 @@
 
 /* pngread.c - read a PNG file
  *
- * Last changed in libpng 1.6.1 [March 28, 2013]
- * Copyright (c) 1998-2013 Glenn Randers-Pehrson
+ * Last changed in libpng 1.6.10 [March 6, 2014]
+ * Copyright (c) 1998-2014 Glenn Randers-Pehrson
  * (Version 0.96 Copyright (c) 1996, 1997 Andreas Dilger)
  * (Version 0.88 Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.)
  *
@@ -73,10 +73,6 @@
        * required.
        */
       png_set_read_fn(png_ptr, NULL, NULL);
-
-#ifdef PNG_INDEX_SUPPORTED
-      png_ptr->index = NULL;
-#endif
    }
 
    return png_ptr;
@@ -266,11 +262,6 @@
 
    if (png_ptr != NULL)
    {
-#ifdef PNG_INDEX_SUPPORTED
-      if (png_ptr->index) {
-         png_read_start_row(png_ptr);
-      }
-#endif
       if ((png_ptr->flags & PNG_FLAG_ROW_INIT) == 0)
       {
          png_read_start_row(png_ptr);
@@ -281,12 +272,11 @@
             PNG_UNUSED(info_ptr)
 #        endif
       }
-#ifndef PNG_INDEX_SUPPORTED
+
       /* New in 1.6.0 this avoids the bug of doing the initializations twice */
       else
          png_app_error(png_ptr,
             "png_read_update_info/png_start_read_image: duplicate call");
-#endif
    }
 }
 
@@ -315,6 +305,72 @@
 #endif /* PNG_SEQUENTIAL_READ_SUPPORTED */
 
 #ifdef PNG_SEQUENTIAL_READ_SUPPORTED
+#ifdef PNG_MNG_FEATURES_SUPPORTED
+/* Undoes intrapixel differencing,
+ * NOTE: this is apparently only supported in the 'sequential' reader.
+ */
+static void
+png_do_read_intrapixel(png_row_infop row_info, png_bytep row)
+{
+   png_debug(1, "in png_do_read_intrapixel");
+
+   if (
+       (row_info->color_type & PNG_COLOR_MASK_COLOR))
+   {
+      int bytes_per_pixel;
+      png_uint_32 row_width = row_info->width;
+
+      if (row_info->bit_depth == 8)
+      {
+         png_bytep rp;
+         png_uint_32 i;
+
+         if (row_info->color_type == PNG_COLOR_TYPE_RGB)
+            bytes_per_pixel = 3;
+
+         else if (row_info->color_type == PNG_COLOR_TYPE_RGB_ALPHA)
+            bytes_per_pixel = 4;
+
+         else
+            return;
+
+         for (i = 0, rp = row; i < row_width; i++, rp += bytes_per_pixel)
+         {
+            *(rp) = (png_byte)((256 + *rp + *(rp + 1)) & 0xff);
+            *(rp+2) = (png_byte)((256 + *(rp + 2) + *(rp + 1)) & 0xff);
+         }
+      }
+      else if (row_info->bit_depth == 16)
+      {
+         png_bytep rp;
+         png_uint_32 i;
+
+         if (row_info->color_type == PNG_COLOR_TYPE_RGB)
+            bytes_per_pixel = 6;
+
+         else if (row_info->color_type == PNG_COLOR_TYPE_RGB_ALPHA)
+            bytes_per_pixel = 8;
+
+         else
+            return;
+
+         for (i = 0, rp = row; i < row_width; i++, rp += bytes_per_pixel)
+         {
+            png_uint_32 s0   = (*(rp    ) << 8) | *(rp + 1);
+            png_uint_32 s1   = (*(rp + 2) << 8) | *(rp + 3);
+            png_uint_32 s2   = (*(rp + 4) << 8) | *(rp + 5);
+            png_uint_32 red  = (s0 + s1 + 65536) & 0xffff;
+            png_uint_32 blue = (s2 + s1 + 65536) & 0xffff;
+            *(rp    ) = (png_byte)((red >> 8) & 0xff);
+            *(rp + 1) = (png_byte)(red & 0xff);
+            *(rp + 4) = (png_byte)((blue >> 8) & 0xff);
+            *(rp + 5) = (png_byte)(blue & 0xff);
+         }
+      }
+   }
+}
+#endif /* PNG_MNG_FEATURES_SUPPORTED */
+
 void PNGAPI
 png_read_row(png_structrp png_ptr, png_bytep row, png_bytep dsp_row)
 {
@@ -499,7 +555,6 @@
    }
 #endif
 
-
 #ifdef PNG_READ_TRANSFORMS_SUPPORTED
    if (png_ptr->transformations)
       png_do_read_transformations(png_ptr, &row_info);
@@ -616,142 +671,6 @@
 }
 #endif /* PNG_SEQUENTIAL_READ_SUPPORTED */
 
-#ifdef PNG_INDEX_SUPPORTED
-#define IDAT_HEADER_SIZE 8
-
-/* Set the png read position to a new position based on idat_position and
- * offset.
- */
-void
-png_set_read_offset(png_structp png_ptr,
-      png_uint_32 idat_position, png_uint_32 bytes_left)
-{
-   png_seek_data(png_ptr, idat_position);
-   png_ptr->idat_size = png_read_chunk_header(png_ptr);
-
-   // We need to add back IDAT_HEADER_SIZE because in zlib's perspective,
-   // IDAT_HEADER in PNG is already stripped out.
-   png_seek_data(png_ptr, idat_position + IDAT_HEADER_SIZE + png_ptr->idat_size - bytes_left);
-   png_ptr->idat_size = bytes_left;
-}
-
-/* Configure png decoder to decode the pass starting from *row.
- * The requested row may be adjusted to align with an indexing row.
- * The actual row for the decoder to start its decoding will be returned in
- * *row.
- */
-void PNGAPI
-png_configure_decoder(png_structp png_ptr, int *row, int pass)
-{
-   png_indexp index = png_ptr->index;
-   int n = *row / index->step[pass];
-   png_line_indexp line_index = index->pass_line_index[pass][n];
-
-   // Adjust row to an indexing row.
-   *row = n * index->step[pass];
-   png_ptr->row_number = *row;
-
-#ifdef PNG_READ_INTERLACING_SUPPORTED
-   if (png_ptr->interlaced)
-      png_set_interlaced_pass(png_ptr, pass);
-#endif
-
-   long row_byte_length =
-      PNG_ROWBYTES(png_ptr->pixel_depth, png_ptr->iwidth) + 1;
-
-   inflateEnd(&png_ptr->zstream);
-   inflateCopy(&png_ptr->zstream, line_index->z_state);
-
-   // Set the png read position to line_index.
-   png_set_read_offset(png_ptr, line_index->stream_idat_position,
-         line_index->bytes_left_in_idat);
-   memcpy(png_ptr->prev_row, line_index->prev_row, row_byte_length);
-   png_ptr->zstream.avail_in = 0;
-}
-
-/* Build the line index and store the index in png_ptr->index.
- */
-void PNGAPI
-png_build_index(png_structp png_ptr)
-{
-   // number of rows in a 8x8 block for each interlaced pass.
-   int number_rows_in_pass[7] = {1, 1, 1, 2, 2, 4, 4};
-
-   int ret;
-   png_uint_32 i, j;
-   png_bytep rp;
-   int p, pass_number = 1;
-
-#ifdef PNG_READ_INTERLACING_SUPPORTED
-   pass_number = png_set_interlace_handling(png_ptr);
-#endif
-
-   if (png_ptr == NULL)
-      return;
-
-   png_read_start_row(png_ptr);
-
-#ifdef PNG_READ_INTERLACING_SUPPORTED
-   if (!png_ptr->interlaced)
-#endif
-   {
-      number_rows_in_pass[0] = 8;
-   }
-
-   rp = png_malloc(png_ptr, png_ptr->rowbytes);
-
-   png_indexp index = png_malloc(png_ptr, sizeof(png_index));
-   png_ptr->index = index;
-
-   index->stream_idat_position = png_ptr->total_data_read - IDAT_HEADER_SIZE;
-
-   // Set the default size of index in each pass to 0,
-   // so that we can free index correctly in png_destroy_read_struct.
-   for (p = 0; p < 7; p++)
-      index->size[p] = 0;
-
-   for (p = 0; p < pass_number; p++)
-   {
-      // We adjust the index step in each pass to make sure each pass
-      // has roughly the same size of index.
-      // This way, we won't consume to much memory in recording index.
-      index->step[p] = INDEX_SAMPLE_SIZE * (8 / number_rows_in_pass[p]);
-      index->size[p] =
-         (png_ptr->height + index->step[p] - 1) / index->step[p];
-      index->pass_line_index[p] =
-         png_malloc(png_ptr, index->size[p] * sizeof(png_line_indexp));
-
-      // Get the row_byte_length seen by the filter. This value may be
-      // different from the row_byte_length of a bitmap in the case of
-      // color palette mode.
-      int row_byte_length =
-         PNG_ROWBYTES(png_ptr->pixel_depth, png_ptr->iwidth) + 1;
-
-      // Now, we record index for each indexing row.
-      for (i = 0; i < index->size[p]; i++)
-      {
-         png_line_indexp line_index = png_malloc(png_ptr, sizeof(png_line_index));
-         index->pass_line_index[p][i] = line_index;
-
-         line_index->z_state = png_malloc(png_ptr, sizeof(z_stream));
-         inflateCopy(line_index->z_state, &png_ptr->zstream);
-         line_index->prev_row = png_malloc(png_ptr, row_byte_length);
-         memcpy(line_index->prev_row, png_ptr->prev_row, row_byte_length);
-         line_index->stream_idat_position = index->stream_idat_position;
-         line_index->bytes_left_in_idat = png_ptr->idat_size + png_ptr->zstream.avail_in;
-
-         // Skip the "step" number of rows to the next indexing row.
-         for (j = 0; j < index->step[p] &&
-               i * index->step[p] + j < png_ptr->height; j++)
-         {
-            png_read_row(png_ptr, rp, NULL);
-         }
-      }
-   }
-   png_free(png_ptr, rp);
-}
-#endif
-
 #ifdef PNG_SEQUENTIAL_READ_SUPPORTED
 /* Read the entire image.  If the image has an alpha channel or a tRNS
  * chunk, and you have called png_handle_alpha()[*], you will need to
@@ -862,11 +781,14 @@
       png_uint_32 length = png_read_chunk_header(png_ptr);
       png_uint_32 chunk_name = png_ptr->chunk_name;
 
-      if (chunk_name == png_IHDR)
+      if (chunk_name == png_IEND)
+         png_handle_IEND(png_ptr, info_ptr, length);
+
+      else if (chunk_name == png_IHDR)
          png_handle_IHDR(png_ptr, info_ptr, length);
 
-      else if (chunk_name == png_IEND)
-         png_handle_IEND(png_ptr, info_ptr, length);
+      else if (info_ptr == NULL)
+         png_crc_finish(png_ptr, length);
 
 #ifdef PNG_HANDLE_AS_UNKNOWN_SUPPORTED
       else if ((keep = png_chunk_unknown_handling(png_ptr, chunk_name)) != 0)
@@ -1032,25 +954,6 @@
    png_free(png_ptr, png_ptr->chunk_list);
 #endif
 
-#ifdef PNG_INDEX_SUPPORTED
-   if (png_ptr->index) {
-      unsigned int i, p;
-      png_indexp index = png_ptr->index;
-      for (p = 0; p < 7; p++) {
-         for (i = 0; i < index->size[p]; i++) {
-            inflateEnd(index->pass_line_index[p][i]->z_state);
-            png_free(png_ptr, index->pass_line_index[p][i]->z_state);
-            png_free(png_ptr, index->pass_line_index[p][i]->prev_row);
-            png_free(png_ptr, index->pass_line_index[p][i]);
-         }
-         if (index->size[p] != 0) {
-            png_free(png_ptr, index->pass_line_index[p]);
-         }
-      }
-      png_free(png_ptr, index);
-   }
-#endif
-
    /* NOTE: the 'setjmp' buffer may still be allocated and the memory and error
     * callbacks are still set at this point.  They are required to complete the
     * destruction of the png_struct itself.
@@ -1101,8 +1004,6 @@
                            int transforms,
                            voidp params)
 {
-   int row;
-
    if (png_ptr == NULL || info_ptr == NULL)
       return;
 
@@ -1114,126 +1015,149 @@
       png_error(png_ptr, "Image is too high to process with png_read_png()");
 
    /* -------------- image transformations start here ------------------- */
+   /* libpng 1.6.10: add code to cause a png_app_error if a selected TRANSFORM
+    * is not implemented.  This will only happen in de-configured (non-default)
+    * libpng builds.  The results can be unexpected - png_read_png may return
+    * short or mal-formed rows because the transform is skipped.
+    */
 
-#ifdef PNG_READ_SCALE_16_TO_8_SUPPORTED
    /* Tell libpng to strip 16-bit/color files down to 8 bits per color.
     */
    if (transforms & PNG_TRANSFORM_SCALE_16)
-   {
      /* Added at libpng-1.5.4. "strip_16" produces the same result that it
       * did in earlier versions, while "scale_16" is now more accurate.
       */
+#ifdef PNG_READ_SCALE_16_TO_8_SUPPORTED
       png_set_scale_16(png_ptr);
-   }
+#else
+      png_app_error(png_ptr, "PNG_TRANSFORM_SCALE_16 not supported");
 #endif
 
-#ifdef PNG_READ_STRIP_16_TO_8_SUPPORTED
    /* If both SCALE and STRIP are required pngrtran will effectively cancel the
     * latter by doing SCALE first.  This is ok and allows apps not to check for
     * which is supported to get the right answer.
     */
    if (transforms & PNG_TRANSFORM_STRIP_16)
+#ifdef PNG_READ_STRIP_16_TO_8_SUPPORTED
       png_set_strip_16(png_ptr);
+#else
+      png_app_error(png_ptr, "PNG_TRANSFORM_STRIP_16 not supported");
 #endif
 
-#ifdef PNG_READ_STRIP_ALPHA_SUPPORTED
    /* Strip alpha bytes from the input data without combining with
     * the background (not recommended).
     */
    if (transforms & PNG_TRANSFORM_STRIP_ALPHA)
+#ifdef PNG_READ_STRIP_ALPHA_SUPPORTED
       png_set_strip_alpha(png_ptr);
+#else
+      png_app_error(png_ptr, "PNG_TRANSFORM_STRIP_ALPHA not supported");
 #endif
 
-#if defined(PNG_READ_PACK_SUPPORTED) && !defined(PNG_READ_EXPAND_SUPPORTED)
    /* Extract multiple pixels with bit depths of 1, 2, or 4 from a single
     * byte into separate bytes (useful for paletted and grayscale images).
     */
    if (transforms & PNG_TRANSFORM_PACKING)
+#ifdef PNG_READ_PACK_SUPPORTED
       png_set_packing(png_ptr);
+#else
+      png_app_error(png_ptr, "PNG_TRANSFORM_PACKING not supported");
 #endif
 
-#ifdef PNG_READ_PACKSWAP_SUPPORTED
    /* Change the order of packed pixels to least significant bit first
     * (not useful if you are using png_set_packing).
     */
    if (transforms & PNG_TRANSFORM_PACKSWAP)
+#ifdef PNG_READ_PACKSWAP_SUPPORTED
       png_set_packswap(png_ptr);
+#else
+      png_app_error(png_ptr, "PNG_TRANSFORM_PACKSWAP not supported");
 #endif
 
-#ifdef 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)
-      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);
+#ifdef PNG_READ_EXPAND_SUPPORTED
+      png_set_expand(png_ptr);
+#else
+      png_app_error(png_ptr, "PNG_TRANSFORM_EXPAND not supported");
 #endif
 
    /* We don't handle background color or gamma transformation or quantizing.
     */
 
-#ifdef PNG_READ_INVERT_SUPPORTED
    /* Invert monochrome files to have 0 as white and 1 as black
     */
    if (transforms & PNG_TRANSFORM_INVERT_MONO)
+#ifdef PNG_READ_INVERT_SUPPORTED
       png_set_invert_mono(png_ptr);
+#else
+      png_app_error(png_ptr, "PNG_TRANSFORM_INVERT_MONO not supported");
 #endif
 
-#ifdef 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);
-   }
+   if (transforms & PNG_TRANSFORM_SHIFT)
+#ifdef PNG_READ_SHIFT_SUPPORTED
+      if (info_ptr->valid & PNG_INFO_sBIT)
+         png_set_shift(png_ptr, &info_ptr->sig_bit);
+#else
+      png_app_error(png_ptr, "PNG_TRANSFORM_SHIFT not supported");
 #endif
 
-#ifdef PNG_READ_BGR_SUPPORTED
    /* Flip the RGB pixels to BGR (or RGBA to BGRA) */
    if (transforms & PNG_TRANSFORM_BGR)
+#ifdef PNG_READ_BGR_SUPPORTED
       png_set_bgr(png_ptr);
+#else
+      png_app_error(png_ptr, "PNG_TRANSFORM_BGR not supported");
 #endif
 
-#ifdef PNG_READ_SWAP_ALPHA_SUPPORTED
    /* Swap the RGBA or GA data to ARGB or AG (or BGRA to ABGR) */
    if (transforms & PNG_TRANSFORM_SWAP_ALPHA)
+#ifdef PNG_READ_SWAP_ALPHA_SUPPORTED
       png_set_swap_alpha(png_ptr);
+#else
+      png_app_error(png_ptr, "PNG_TRANSFORM_SWAP_ALPHA not supported");
 #endif
 
-#ifdef PNG_READ_SWAP_SUPPORTED
    /* Swap bytes of 16-bit files to least significant byte first */
    if (transforms & PNG_TRANSFORM_SWAP_ENDIAN)
+#ifdef PNG_READ_SWAP_SUPPORTED
       png_set_swap(png_ptr);
+#else
+      png_app_error(png_ptr, "PNG_TRANSFORM_SWAP_ENDIAN not supported");
 #endif
 
 /* Added at libpng-1.2.41 */
-#ifdef PNG_READ_INVERT_ALPHA_SUPPORTED
    /* Invert the alpha channel from opacity to transparency */
    if (transforms & PNG_TRANSFORM_INVERT_ALPHA)
+#ifdef PNG_READ_INVERT_ALPHA_SUPPORTED
       png_set_invert_alpha(png_ptr);
+#else
+      png_app_error(png_ptr, "PNG_TRANSFORM_INVERT_ALPHA not supported");
 #endif
 
 /* Added at libpng-1.2.41 */
-#ifdef PNG_READ_GRAY_TO_RGB_SUPPORTED
    /* Expand grayscale image to RGB */
    if (transforms & PNG_TRANSFORM_GRAY_TO_RGB)
+#ifdef PNG_READ_GRAY_TO_RGB_SUPPORTED
       png_set_gray_to_rgb(png_ptr);
+#else
+      png_app_error(png_ptr, "PNG_TRANSFORM_GRAY_TO_RGB not supported");
 #endif
 
 /* Added at libpng-1.5.4 */
-#ifdef PNG_READ_EXPAND_16_SUPPORTED
    if (transforms & PNG_TRANSFORM_EXPAND_16)
+#ifdef PNG_READ_EXPAND_16_SUPPORTED
       png_set_expand_16(png_ptr);
+#else
+      png_app_error(png_ptr, "PNG_TRANSFORM_EXPAND_16 not supported");
 #endif
 
    /* We don't handle adding filler bytes */
@@ -1256,16 +1180,17 @@
    {
       png_uint_32 iptr;
 
-      info_ptr->row_pointers = (png_bytepp)png_malloc(png_ptr,
-          info_ptr->height * (sizeof (png_bytep)));
+      info_ptr->row_pointers = png_voidcast(png_bytepp, png_malloc(png_ptr,
+          info_ptr->height * (sizeof (png_bytep))));
+
       for (iptr=0; iptr<info_ptr->height; iptr++)
          info_ptr->row_pointers[iptr] = NULL;
 
       info_ptr->free_me |= PNG_FREE_ROWS;
 
-      for (row = 0; row < (int)info_ptr->height; row++)
-         info_ptr->row_pointers[row] = (png_bytep)png_malloc(png_ptr,
-            png_get_rowbytes(png_ptr, info_ptr));
+      for (iptr = 0; iptr < info_ptr->height; iptr++)
+         info_ptr->row_pointers[iptr] = png_voidcast(png_bytep,
+            png_malloc(png_ptr, info_ptr->rowbytes));
    }
 
    png_read_image(png_ptr, info_ptr->row_pointers);
@@ -1274,9 +1199,7 @@
    /* Read rest of file, and get additional chunks in info_ptr - REQUIRED */
    png_read_end(png_ptr, info_ptr);
 
-   PNG_UNUSED(transforms)   /* Quiet compiler warnings */
    PNG_UNUSED(params)
-
 }
 #endif /* PNG_INFO_IMAGE_SUPPORTED */
 #endif /* PNG_SEQUENTIAL_READ_SUPPORTED */
@@ -1290,12 +1213,11 @@
 /* Arguments to png_image_finish_read: */
 
 /* Encoding of PNG data (used by the color-map code) */
-/* TODO: change these, dang, ANSI-C reserves the 'E' namespace. */
-#  define E_NOTSET  0 /* File encoding not yet known */
-#  define E_sRGB    1 /* 8-bit encoded to sRGB gamma */
-#  define E_LINEAR  2 /* 16-bit linear: not encoded, NOT pre-multiplied! */
-#  define E_FILE    3 /* 8-bit encoded to file gamma, not sRGB or linear */
-#  define E_LINEAR8 4 /* 8-bit linear: only from a file value */
+#  define P_NOTSET  0 /* File encoding not yet known */
+#  define P_sRGB    1 /* 8-bit encoded to sRGB gamma */
+#  define P_LINEAR  2 /* 16-bit linear: not encoded, NOT pre-multiplied! */
+#  define P_FILE    3 /* 8-bit encoded to file gamma, not sRGB or linear */
+#  define P_LINEAR8 4 /* 8-bit linear: only from a file value */
 
 /* Color-map processing: after libpng has run on the PNG image further
  * processing may be needed to conver the data to color-map indicies.
@@ -1326,7 +1248,7 @@
    png_voidp       first_row;
    ptrdiff_t       row_bytes;           /* step between rows */
    int             file_encoding;       /* E_ values above */
-   png_fixed_point gamma_to_linear;     /* For E_FILE, reciprocal of gamma */
+   png_fixed_point gamma_to_linear;     /* For P_FILE, reciprocal of gamma */
    int             colormap_processing; /* PNG_CMAP_ values above */
 } png_image_read_control;
 
@@ -1458,7 +1380,7 @@
 #ifdef PNG_COLORSPACE_SUPPORTED
       /* Does the colorspace match sRGB?  If there is no color endpoint
        * (colorant) information assume yes, otherwise require the
-       * 'ENDPOINTS_MATCHE_sRGB' colorspace flag to have been set.  If the
+       * 'ENDPOINTS_MATCHP_sRGB' colorspace flag to have been set.  If the
        * colorspace has been determined to be invalid ignore it.
        */
       if ((format & PNG_FORMAT_FLAG_COLOR) != 0 && ((png_ptr->colorspace.flags
@@ -1647,17 +1569,24 @@
     *
     * Or image data handling:
     *
-    *    tRNS, bKGD, gAMA, cHRM, sRGB, iCCP and sBIT.
+    *    tRNS, bKGD, gAMA, cHRM, sRGB, [iCCP] and sBIT.
     *
     * This provides a small performance improvement and eliminates any
     * potential vulnerability to security problems in the unused chunks.
+    *
+    * At present the iCCP chunk data isn't used, so iCCP chunk can be ignored
+    * too.  This allows the simplified API to be compiled without iCCP support,
+    * however if the support is there the chunk is still checked to detect
+    * errors (which are unfortunately quite common.)
     */
    {
          static PNG_CONST png_byte chunks_to_process[] = {
             98,  75,  71,  68, '\0',  /* bKGD */
             99,  72,  82,  77, '\0',  /* cHRM */
            103,  65,  77,  65, '\0',  /* gAMA */
+#        ifdef PNG_READ_iCCP_SUPPORTED
            105,  67,  67,  80, '\0',  /* iCCP */
+#        endif
            115,  66,  73,  84, '\0',  /* sBIT */
            115,  82,  71,  66, '\0',  /* sRGB */
            };
@@ -1694,25 +1623,25 @@
    {
       if (png_gamma_not_sRGB(g))
       {
-         display->file_encoding = E_FILE;
+         display->file_encoding = P_FILE;
          display->gamma_to_linear = png_reciprocal(g);
       }
 
       else
-         display->file_encoding = E_sRGB;
+         display->file_encoding = P_sRGB;
    }
 
    else
-      display->file_encoding = E_LINEAR8;
+      display->file_encoding = P_LINEAR8;
 }
 
 static unsigned int
 decode_gamma(png_image_read_control *display, png_uint_32 value, int encoding)
 {
-   if (encoding == E_FILE) /* double check */
+   if (encoding == P_FILE) /* double check */
       encoding = display->file_encoding;
 
-   if (encoding == E_NOTSET) /* must be the file encoding */
+   if (encoding == P_NOTSET) /* must be the file encoding */
    {
       set_file_encoding(display);
       encoding = display->file_encoding;
@@ -1720,18 +1649,18 @@
 
    switch (encoding)
    {
-      case E_FILE:
+      case P_FILE:
          value = png_gamma_16bit_correct(value*257, display->gamma_to_linear);
          break;
 
-      case E_sRGB:
+      case P_sRGB:
          value = png_sRGB_table[value];
          break;
 
-      case E_LINEAR:
+      case P_LINEAR:
          break;
 
-      case E_LINEAR8:
+      case P_LINEAR8:
          value *= 257;
          break;
 
@@ -1750,9 +1679,9 @@
    png_uint_32 background, int encoding)
 {
    /* The file value is composed on the background, the background has the given
-    * encoding and so does the result, the file is encoded with E_FILE and the
+    * encoding and so does the result, the file is encoded with P_FILE and the
     * file and alpha are 8-bit values.  The (output) encoding will always be
-    * E_LINEAR or E_sRGB.
+    * P_LINEAR or P_sRGB.
     */
    png_uint_32 f = decode_gamma(display, foreground, foreground_encoding);
    png_uint_32 b = decode_gamma(display, background, encoding);
@@ -1762,7 +1691,7 @@
     */
    f = f * alpha + b * (255-alpha);
 
-   if (encoding == E_LINEAR)
+   if (encoding == P_LINEAR)
    {
       /* Scale to 65535; divide by 255, approximately (in fact this is extremely
        * accurate, it divides by 255.00000005937181414556, with no overflow.)
@@ -1772,13 +1701,13 @@
       f = (f+32768) >> 16;
    }
 
-   else /* E_sRGB */
+   else /* P_sRGB */
       f = PNG_sRGB_FROM_LINEAR(f);
 
    return f;
 }
 
-/* NOTE: E_LINEAR values to this routine must be 16-bit, but E_FILE values must
+/* NOTE: P_LINEAR values to this routine must be 16-bit, but P_FILE values must
  * be 8-bit.
  */
 static void
@@ -1788,7 +1717,7 @@
 {
    png_imagep image = display->image;
    const int output_encoding = (image->format & PNG_FORMAT_FLAG_LINEAR) ?
-      E_LINEAR : E_sRGB;
+      P_LINEAR : P_sRGB;
    const int convert_to_Y = (image->format & PNG_FORMAT_FLAG_COLOR) == 0 &&
       (red != green || green != blue);
 
@@ -1798,18 +1727,18 @@
    /* Update the cache with whether the file gamma is significantly different
     * from sRGB.
     */
-   if (encoding == E_FILE)
+   if (encoding == P_FILE)
    {
-      if (display->file_encoding == E_NOTSET)
+      if (display->file_encoding == P_NOTSET)
          set_file_encoding(display);
 
-      /* Note that the cached value may be E_FILE too, but if it is then the
+      /* Note that the cached value may be P_FILE too, but if it is then the
        * gamma_to_linear member has been set.
        */
       encoding = display->file_encoding;
    }
 
-   if (encoding == E_FILE)
+   if (encoding == P_FILE)
    {
       png_fixed_point g = display->gamma_to_linear;
 
@@ -1817,10 +1746,10 @@
       green = png_gamma_16bit_correct(green*257, g);
       blue = png_gamma_16bit_correct(blue*257, g);
 
-      if (convert_to_Y || output_encoding == E_LINEAR)
+      if (convert_to_Y || output_encoding == P_LINEAR)
       {
          alpha *= 257;
-         encoding = E_LINEAR;
+         encoding = P_LINEAR;
       }
 
       else
@@ -1828,11 +1757,11 @@
          red = PNG_sRGB_FROM_LINEAR(red * 255);
          green = PNG_sRGB_FROM_LINEAR(green * 255);
          blue = PNG_sRGB_FROM_LINEAR(blue * 255);
-         encoding = E_sRGB;
+         encoding = P_sRGB;
       }
    }
 
-   else if (encoding == E_LINEAR8)
+   else if (encoding == P_LINEAR8)
    {
       /* This encoding occurs quite frequently in test cases because PngSuite
        * includes a gAMA 1.0 chunk with most images.
@@ -1841,10 +1770,10 @@
       green *= 257;
       blue *= 257;
       alpha *= 257;
-      encoding = E_LINEAR;
+      encoding = P_LINEAR;
    }
 
-   else if (encoding == E_sRGB && (convert_to_Y || output_encoding == E_LINEAR))
+   else if (encoding == P_sRGB && (convert_to_Y || output_encoding == P_LINEAR))
    {
       /* The values are 8-bit sRGB values, but must be converted to 16-bit
        * linear.
@@ -1853,11 +1782,11 @@
       green = png_sRGB_table[green];
       blue = png_sRGB_table[blue];
       alpha *= 257;
-      encoding = E_LINEAR;
+      encoding = P_LINEAR;
    }
 
    /* This is set if the color isn't gray but the output is. */
-   if (encoding == E_LINEAR)
+   if (encoding == P_LINEAR)
    {
       if (convert_to_Y)
       {
@@ -1865,7 +1794,7 @@
          png_uint_32 y = (png_uint_32)6968 * red  + (png_uint_32)23434 * green +
             (png_uint_32)2366 * blue;
 
-         if (output_encoding == E_LINEAR)
+         if (output_encoding == P_LINEAR)
             y = (y + 16384) >> 15;
 
          else
@@ -1874,19 +1803,19 @@
             y = (y + 128) >> 8;
             y *= 255;
             y = PNG_sRGB_FROM_LINEAR((y + 64) >> 7);
-            encoding = E_sRGB;
+            encoding = P_sRGB;
          }
 
          blue = red = green = y;
       }
 
-      else if (output_encoding == E_sRGB)
+      else if (output_encoding == P_sRGB)
       {
          red = PNG_sRGB_FROM_LINEAR(red * 255);
          green = PNG_sRGB_FROM_LINEAR(green * 255);
          blue = PNG_sRGB_FROM_LINEAR(blue * 255);
          alpha = PNG_DIV257(alpha);
-         encoding = E_sRGB;
+         encoding = P_sRGB;
       }
    }
 
@@ -1895,7 +1824,7 @@
 
    /* Store the value. */
    {
-#     ifdef PNG_FORMAT_BGR_SUPPORTED
+#     ifdef PNG_FORMAT_AFIRST_SUPPORTED
          const int afirst = (image->format & PNG_FORMAT_FLAG_AFIRST) != 0 &&
             (image->format & PNG_FORMAT_FLAG_ALPHA) != 0;
 #     else
@@ -1907,7 +1836,7 @@
 #        define bgr 0
 #     endif
 
-      if (output_encoding == E_LINEAR)
+      if (output_encoding == P_LINEAR)
       {
          png_uint_16p entry = png_voidcast(png_uint_16p, display->colormap);
 
@@ -1962,7 +1891,7 @@
          }
       }
 
-      else /* output encoding is E_sRGB */
+      else /* output encoding is P_sRGB */
       {
          png_bytep entry = png_voidcast(png_bytep, display->colormap);
 
@@ -2004,7 +1933,7 @@
    unsigned int i;
 
    for (i=0; i<256; ++i)
-      png_create_colormap_entry(display, i, i, i, i, 255, E_FILE);
+      png_create_colormap_entry(display, i, i, i, i, 255, P_FILE);
 
    return i;
 }
@@ -2015,7 +1944,7 @@
    unsigned int i;
 
    for (i=0; i<256; ++i)
-      png_create_colormap_entry(display, i, i, i, i, 255, E_sRGB);
+      png_create_colormap_entry(display, i, i, i, i, 255, P_sRGB);
 
    return i;
 }
@@ -2054,13 +1983,13 @@
    while (i < 231)
    {
       unsigned int gray = (i * 256 + 115) / 231;
-      png_create_colormap_entry(display, i++, gray, gray, gray, 255, E_sRGB);
+      png_create_colormap_entry(display, i++, gray, gray, gray, 255, P_sRGB);
    }
 
    /* 255 is used here for the component values for consistency with the code
     * that undoes premultiplication in pngwrite.c.
     */
-   png_create_colormap_entry(display, i++, 255, 255, 255, 0, E_sRGB);
+   png_create_colormap_entry(display, i++, 255, 255, 255, 0, P_sRGB);
 
    for (a=1; a<5; ++a)
    {
@@ -2068,7 +1997,7 @@
 
       for (g=0; g<6; ++g)
          png_create_colormap_entry(display, i++, g*51, g*51, g*51, a*51,
-            E_sRGB);
+            P_sRGB);
    }
 
    return i;
@@ -2092,7 +2021,7 @@
 
          for (b=0; b<6; ++b)
             png_create_colormap_entry(display, i++, r*51, g*51, b*51, 255,
-               E_sRGB);
+               P_sRGB);
       }
    }
 
@@ -2115,11 +2044,11 @@
    const png_structrp png_ptr = image->opaque->png_ptr;
    const png_uint_32 output_format = image->format;
    const int output_encoding = (output_format & PNG_FORMAT_FLAG_LINEAR) ?
-      E_LINEAR : E_sRGB;
+      P_LINEAR : P_sRGB;
 
    unsigned int cmap_entries;
    unsigned int output_processing;        /* Output processing option */
-   unsigned int data_encoding = E_NOTSET; /* Encoding libpng must produce */
+   unsigned int data_encoding = P_NOTSET; /* Encoding libpng must produce */
 
    /* Background information; the background color and the index of this color
     * in the color-map if it exists (else 256).
@@ -2139,7 +2068,7 @@
          png_ptr->num_trans > 0) /* alpha in input */ &&
       ((output_format & PNG_FORMAT_FLAG_ALPHA) == 0) /* no alpha in output */)
    {
-      if (output_encoding == E_LINEAR) /* compose on black */
+      if (output_encoding == P_LINEAR) /* compose on black */
          back_b = back_g = back_r = 0;
 
       else if (display->background == NULL /* no way to remove it */)
@@ -2163,7 +2092,7 @@
       }
    }
 
-   else if (output_encoding == E_LINEAR)
+   else if (output_encoding == P_LINEAR)
       back_b = back_r = back_g = 65535;
 
    else
@@ -2221,7 +2150,7 @@
                trans = png_ptr->trans_color.gray;
 
                if ((output_format & PNG_FORMAT_FLAG_ALPHA) == 0)
-                  back_alpha = output_encoding == E_LINEAR ? 65535 : 255;
+                  back_alpha = output_encoding == P_LINEAR ? 65535 : 255;
             }
 
             /* png_create_colormap_entry just takes an RGBA and writes the
@@ -2239,7 +2168,7 @@
                 */
                if (i != trans)
                   png_create_colormap_entry(display, i, val, val, val, 255,
-                     E_FILE/*8-bit with file gamma*/);
+                     P_FILE/*8-bit with file gamma*/);
 
                /* Else this entry is transparent.  The colors don't matter if
                 * there is an alpha channel (back_alpha == 0), but it does no
@@ -2255,7 +2184,7 @@
             }
 
             /* We need libpng to preserve the original encoding. */
-            data_encoding = E_FILE;
+            data_encoding = P_FILE;
 
             /* The rows from libpng, while technically gray values, are now also
              * color-map indicies; however, they may need to be expanded to 1
@@ -2284,7 +2213,7 @@
              * ensuring that the corresponding gray level matches the background
              * color exactly.
              */
-            data_encoding = E_sRGB;
+            data_encoding = P_sRGB;
 
             if (PNG_GRAY_COLORMAP_ENTRIES > image->colormap_entries)
                png_error(png_ptr, "gray[16] color-map: too few entries");
@@ -2308,7 +2237,7 @@
                      png_color_16 c;
                      png_uint_32 gray = back_g;
 
-                     if (output_encoding == E_LINEAR)
+                     if (output_encoding == P_LINEAR)
                      {
                         gray = PNG_sRGB_FROM_LINEAR(gray * 255);
 
@@ -2316,7 +2245,7 @@
                          * matches.
                          */
                         png_create_colormap_entry(display, gray, back_g, back_g,
-                           back_g, 65535, E_LINEAR);
+                           back_g, 65535, P_LINEAR);
                      }
 
                      /* The background passed to libpng, however, must be the
@@ -2337,7 +2266,7 @@
                      break;
                   }
 
-                  back_alpha = output_encoding == E_LINEAR ? 65535 : 255;
+                  back_alpha = output_encoding == P_LINEAR ? 65535 : 255;
                }
 
                /* output_processing means that the libpng-processed row will be
@@ -2374,7 +2303,7 @@
           * worry about tRNS matching - tRNS is ignored if there is an alpha
           * channel.
           */
-         data_encoding = E_sRGB;
+         data_encoding = P_sRGB;
 
          if (output_format & PNG_FORMAT_FLAG_ALPHA)
          {
@@ -2417,13 +2346,13 @@
 
                cmap_entries = make_gray_colormap(display);
 
-               if (output_encoding == E_LINEAR)
+               if (output_encoding == P_LINEAR)
                {
                   gray = PNG_sRGB_FROM_LINEAR(gray * 255);
 
                   /* And make sure the corresponding palette entry matches. */
                   png_create_colormap_entry(display, gray, back_g, back_g,
-                     back_g, 65535, E_LINEAR);
+                     back_g, 65535, P_LINEAR);
                }
 
                /* The background passed to libpng, however, must be the sRGB
@@ -2454,7 +2383,7 @@
                {
                   png_uint_32 gray = (i * 256 + 115) / 231;
                   png_create_colormap_entry(display, i++, gray, gray, gray,
-                     255, E_sRGB);
+                     255, P_sRGB);
                }
 
                /* NOTE: this preserves the full precision of the application
@@ -2462,7 +2391,7 @@
                 */
                background_index = i;
                png_create_colormap_entry(display, i++, back_r, back_g, back_b,
-                  output_encoding == E_LINEAR ? 65535U : 255U, output_encoding);
+                  output_encoding == P_LINEAR ? 65535U : 255U, output_encoding);
 
                /* For non-opaque input composite on the sRGB background - this
                 * requires inverting the encoding for each component.  The input
@@ -2472,7 +2401,7 @@
                 * represents.  Consequently 'G' is always sRGB encoded, while
                 * 'A' is linear.  We need the linear background colors.
                 */
-               if (output_encoding == E_sRGB) /* else already linear */
+               if (output_encoding == P_sRGB) /* else already linear */
                {
                   /* This may produce a value not exactly matching the
                    * background, but that's ok because these numbers are only
@@ -2502,7 +2431,7 @@
                      png_create_colormap_entry(display, i++,
                         PNG_sRGB_FROM_LINEAR(gray + back_rx),
                         PNG_sRGB_FROM_LINEAR(gray + back_gx),
-                        PNG_sRGB_FROM_LINEAR(gray + back_bx), 255, E_sRGB);
+                        PNG_sRGB_FROM_LINEAR(gray + back_bx), 255, P_sRGB);
                   }
                }
 
@@ -2529,7 +2458,7 @@
              */
             png_set_rgb_to_gray_fixed(png_ptr, PNG_ERROR_ACTION_NONE, -1,
                -1);
-            data_encoding = E_sRGB;
+            data_encoding = P_sRGB;
 
             /* The output will now be one or two 8-bit gray or gray+alpha
              * channels.  The more complex case arises when the input has alpha.
@@ -2574,7 +2503,7 @@
                   png_gamma_not_sRGB(png_ptr->colorspace.gamma))
                {
                   cmap_entries = make_gray_file_colormap(display);
-                  data_encoding = E_FILE;
+                  data_encoding = P_FILE;
                }
 
                else
@@ -2593,18 +2522,18 @@
                    * it.  Achieve this simply by ensuring that the entry
                    * selected for the background really is the background color.
                    */
-                  if (data_encoding == E_FILE) /* from the fixup above */
+                  if (data_encoding == P_FILE) /* from the fixup above */
                   {
                      /* The app supplied a gray which is in output_encoding, we
-                      * need to convert it to a value of the input (E_FILE)
+                      * need to convert it to a value of the input (P_FILE)
                       * encoding then set this palette entry to the required
                       * output encoding.
                       */
-                     if (output_encoding == E_sRGB)
-                        gray = png_sRGB_table[gray]; /* now E_LINEAR */
+                     if (output_encoding == P_sRGB)
+                        gray = png_sRGB_table[gray]; /* now P_LINEAR */
 
                      gray = PNG_DIV257(png_gamma_16bit_correct(gray,
-                        png_ptr->colorspace.gamma)); /* now E_FILE */
+                        png_ptr->colorspace.gamma)); /* now P_FILE */
 
                      /* And make sure the corresponding palette entry contains
                       * exactly the required sRGB value.
@@ -2613,14 +2542,14 @@
                         back_g, 0/*unused*/, output_encoding);
                   }
 
-                  else if (output_encoding == E_LINEAR)
+                  else if (output_encoding == P_LINEAR)
                   {
                      gray = PNG_sRGB_FROM_LINEAR(gray * 255);
 
                      /* And make sure the corresponding palette entry matches.
                       */
                      png_create_colormap_entry(display, gray, back_g, back_g,
-                        back_g, 0/*unused*/, E_LINEAR);
+                        back_g, 0/*unused*/, P_LINEAR);
                   }
 
                   /* The background passed to libpng, however, must be the
@@ -2650,7 +2579,7 @@
              * to do it once and using PNG_DIV51 on the 6x6x6 reduced RGB cube.
              * Consequently we always want libpng to produce sRGB data.
              */
-            data_encoding = E_sRGB;
+            data_encoding = P_sRGB;
 
             /* Is there any transparency or alpha? */
             if (png_ptr->color_type == PNG_COLOR_TYPE_RGB_ALPHA ||
@@ -2670,7 +2599,7 @@
 
                   /* Add a transparent entry. */
                   png_create_colormap_entry(display, cmap_entries, 255, 255,
-                     255, 0, E_sRGB);
+                     255, 0, P_sRGB);
 
                   /* This is stored as the background index for the processing
                    * algorithm.
@@ -2691,7 +2620,7 @@
                          */
                         for (b=0; b<256; b = (b << 1) | 0x7f)
                            png_create_colormap_entry(display, cmap_entries++,
-                              r, g, b, 128, E_sRGB);
+                              r, g, b, 128, P_sRGB);
                      }
                   }
 
@@ -2720,7 +2649,7 @@
                   png_create_colormap_entry(display, cmap_entries, back_r,
                         back_g, back_b, 0/*unused*/, output_encoding);
 
-                  if (output_encoding == E_LINEAR)
+                  if (output_encoding == P_LINEAR)
                   {
                      r = PNG_sRGB_FROM_LINEAR(back_r * 255);
                      g = PNG_sRGB_FROM_LINEAR(back_g * 255);
@@ -2760,11 +2689,11 @@
                             */
                            for (b=0; b<256; b = (b << 1) | 0x7f)
                               png_create_colormap_entry(display, cmap_entries++,
-                                 png_colormap_compose(display, r, E_sRGB, 128,
+                                 png_colormap_compose(display, r, P_sRGB, 128,
                                     back_r, output_encoding),
-                                 png_colormap_compose(display, g, E_sRGB, 128,
+                                 png_colormap_compose(display, g, P_sRGB, 128,
                                     back_g, output_encoding),
-                                 png_colormap_compose(display, b, E_sRGB, 128,
+                                 png_colormap_compose(display, b, P_sRGB, 128,
                                     back_b, output_encoding),
                                  0/*unused*/, output_encoding);
                         }
@@ -2823,7 +2752,7 @@
                num_trans = 0;
 
             output_processing = PNG_CMAP_NONE;
-            data_encoding = E_FILE; /* Don't change from color-map indicies */
+            data_encoding = P_FILE; /* Don't change from color-map indicies */
             cmap_entries = png_ptr->num_palette;
             if (cmap_entries > 256)
                cmap_entries = 256;
@@ -2845,13 +2774,13 @@
                       * on the sRGB color in 'back'.
                       */
                      png_create_colormap_entry(display, i,
-                        png_colormap_compose(display, colormap[i].red, E_FILE,
+                        png_colormap_compose(display, colormap[i].red, P_FILE,
                            trans[i], back_r, output_encoding),
-                        png_colormap_compose(display, colormap[i].green, E_FILE,
+                        png_colormap_compose(display, colormap[i].green, P_FILE,
                            trans[i], back_g, output_encoding),
-                        png_colormap_compose(display, colormap[i].blue, E_FILE,
+                        png_colormap_compose(display, colormap[i].blue, P_FILE,
                            trans[i], back_b, output_encoding),
-                        output_encoding == E_LINEAR ? trans[i] * 257U :
+                        output_encoding == P_LINEAR ? trans[i] * 257U :
                            trans[i],
                         output_encoding);
                   }
@@ -2860,7 +2789,7 @@
                else
                   png_create_colormap_entry(display, i, colormap[i].red,
                      colormap[i].green, colormap[i].blue,
-                     i < num_trans ? trans[i] : 255U, E_FILE/*8-bit*/);
+                     i < num_trans ? trans[i] : 255U, P_FILE/*8-bit*/);
             }
 
             /* The PNG data may have indicies packed in fewer than 8 bits, it
@@ -2888,12 +2817,12 @@
          png_error(png_ptr, "bad data option (internal error)");
          break;
 
-      case E_sRGB:
+      case P_sRGB:
          /* Change to 8-bit sRGB */
          png_set_alpha_mode_fixed(png_ptr, PNG_ALPHA_PNG, PNG_GAMMA_sRGB);
          /* FALL THROUGH */
 
-      case E_FILE:
+      case P_FILE:
          if (png_ptr->bit_depth > 8)
             png_set_scale_16(png_ptr);
          break;
@@ -2970,7 +2899,6 @@
          break;
 
       default:
-         passes = 0;
          png_error(png_ptr, "unknown interlace type");
    }
 
@@ -3289,7 +3217,6 @@
          break;
 
       default:
-         passes = 0;
          png_error(png_ptr, "unknown interlace type");
    }
 
@@ -3438,11 +3365,15 @@
          break;
 
       default:
-         passes = 0;
          png_error(png_ptr, "unknown interlace type");
    }
 
-   switch (png_get_bit_depth(png_ptr, info_ptr))
+   /* Use direct access to info_ptr here because otherwise the simplified API
+    * would require PNG_EASY_ACCESS_SUPPORTED (just for this.)  Note this is
+    * checking the value after libpng expansions, not the original value in the
+    * PNG.
+    */
+   switch (info_ptr->bit_depth)
    {
       default:
          png_error(png_ptr, "unexpected bit depth");
@@ -3450,7 +3381,7 @@
 
       case 8:
          /* 8-bit sRGB gray values with an alpha channel; the alpha channel is
-          * to be removed by composing on a backgroundi: either the row if
+          * to be removed by composing on a background: either the row if
           * display->background is NULL or display->background->green if not.
           * Unlike the code above ALPHA_OPTIMIZED has *not* been done.
           */
@@ -3590,8 +3521,10 @@
             unsigned int outchannels = 1+preserve_alpha;
             int swap_alpha = 0;
 
-            if (preserve_alpha && (image->format & PNG_FORMAT_FLAG_AFIRST))
-               swap_alpha = 1;
+#           ifdef PNG_SIMPLIFIED_READ_AFIRST_SUPPORTED
+               if (preserve_alpha && (image->format & PNG_FORMAT_FLAG_AFIRST))
+                  swap_alpha = 1;
+#           endif
 
             for (pass = 0; pass < passes; ++pass)
             {