DO NOT MERGE Update libpng to 1.6.20

BUG:23265085
Change-Id: I85199805636d771f3597b691b63bc0bf46084833
(cherry picked from commit bbe98b40cda082024b669fa508931042eed18f82)
diff --git a/pngread.c b/pngread.c
index 2ce83ed..7699608 100644
--- a/pngread.c
+++ b/pngread.c
@@ -1,8 +1,8 @@
 
 /* pngread.c - read a PNG file
  *
- * Last changed in libpng 1.6.10 [March 6, 2014]
- * Copyright (c) 1998-2014 Glenn Randers-Pehrson
+ * Last changed in libpng 1.6.17 [March 26, 2015]
+ * Copyright (c) 1998-2015 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.)
  *
@@ -44,7 +44,7 @@
 {
    png_structp png_ptr = png_create_png_struct(user_png_ver, error_ptr,
       error_fn, warn_fn, mem_ptr, malloc_fn, free_fn);
-#endif /* PNG_USER_MEM_SUPPORTED */
+#endif /* USER_MEM */
 
    if (png_ptr != NULL)
    {
@@ -63,7 +63,7 @@
          /* In stable builds only warn if an application error can be completely
           * handled.
           */
-#        if PNG_LIBPNG_BUILD_BASE_TYPE >= PNG_LIBPNG_BUILD_RC
+#        if PNG_RELEASE_BUILD
             png_ptr->flags |= PNG_FLAG_APP_WARNINGS_WARN;
 #        endif
 #     endif
@@ -117,20 +117,20 @@
        */
       if (chunk_name == png_IDAT)
       {
-         if (!(png_ptr->mode & PNG_HAVE_IHDR))
+         if ((png_ptr->mode & PNG_HAVE_IHDR) == 0)
             png_chunk_error(png_ptr, "Missing IHDR before IDAT");
 
          else if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE &&
-             !(png_ptr->mode & PNG_HAVE_PLTE))
+             (png_ptr->mode & PNG_HAVE_PLTE) == 0)
             png_chunk_error(png_ptr, "Missing PLTE before IDAT");
 
-         else if (png_ptr->mode & PNG_AFTER_IDAT)
+         else if ((png_ptr->mode & PNG_AFTER_IDAT) != 0)
             png_chunk_benign_error(png_ptr, "Too many IDATs found");
 
          png_ptr->mode |= PNG_HAVE_IDAT;
       }
 
-      else if (png_ptr->mode & PNG_HAVE_IDAT)
+      else if ((png_ptr->mode & PNG_HAVE_IDAT) != 0)
          png_ptr->mode |= PNG_AFTER_IDAT;
 
       /* This should be a binary subdivision search or a hash for
@@ -256,7 +256,7 @@
             PNG_HANDLE_CHUNK_AS_DEFAULT);
    }
 }
-#endif /* PNG_SEQUENTIAL_READ_SUPPORTED */
+#endif /* SEQUENTIAL_READ */
 
 /* Optional call to update the users info_ptr structure */
 void PNGAPI
@@ -312,7 +312,7 @@
             "png_start_read_image/png_read_update_info: duplicate call");
    }
 }
-#endif /* PNG_SEQUENTIAL_READ_SUPPORTED */
+#endif /* SEQUENTIAL_READ */
 
 #ifdef PNG_SEQUENTIAL_READ_SUPPORTED
 #ifdef PNG_MNG_FEATURES_SUPPORTED
@@ -325,7 +325,7 @@
    png_debug(1, "in png_do_read_intrapixel");
 
    if (
-       (row_info->color_type & PNG_COLOR_MASK_COLOR))
+       (row_info->color_type & PNG_COLOR_MASK_COLOR) != 0)
    {
       int bytes_per_pixel;
       png_uint_32 row_width = row_info->width;
@@ -379,7 +379,7 @@
       }
    }
 }
-#endif /* PNG_MNG_FEATURES_SUPPORTED */
+#endif /* MNG_FEATURES */
 
 void PNGAPI
 png_read_row(png_structrp png_ptr, png_bytep row, png_bytep dsp_row)
@@ -395,7 +395,7 @@
    /* png_read_start_row sets the information (in particular iwidth) for this
     * interlace pass.
     */
-   if (!(png_ptr->flags & PNG_FLAG_ROW_INIT))
+   if ((png_ptr->flags & PNG_FLAG_ROW_INIT) == 0)
       png_read_start_row(png_ptr);
 
    /* 1.5.6: row_info moved out of png_struct to a local here. */
@@ -406,45 +406,47 @@
    row_info.pixel_depth = png_ptr->pixel_depth;
    row_info.rowbytes = PNG_ROWBYTES(row_info.pixel_depth, row_info.width);
 
+#ifdef PNG_WARNINGS_SUPPORTED
    if (png_ptr->row_number == 0 && png_ptr->pass == 0)
    {
    /* Check for transforms that have been set but were defined out */
 #if defined(PNG_WRITE_INVERT_SUPPORTED) && !defined(PNG_READ_INVERT_SUPPORTED)
-   if (png_ptr->transformations & PNG_INVERT_MONO)
+   if ((png_ptr->transformations & PNG_INVERT_MONO) != 0)
       png_warning(png_ptr, "PNG_READ_INVERT_SUPPORTED is not defined");
 #endif
 
 #if defined(PNG_WRITE_FILLER_SUPPORTED) && !defined(PNG_READ_FILLER_SUPPORTED)
-   if (png_ptr->transformations & PNG_FILLER)
+   if ((png_ptr->transformations & PNG_FILLER) != 0)
       png_warning(png_ptr, "PNG_READ_FILLER_SUPPORTED is not defined");
 #endif
 
 #if defined(PNG_WRITE_PACKSWAP_SUPPORTED) && \
     !defined(PNG_READ_PACKSWAP_SUPPORTED)
-   if (png_ptr->transformations & PNG_PACKSWAP)
+   if ((png_ptr->transformations & PNG_PACKSWAP) != 0)
       png_warning(png_ptr, "PNG_READ_PACKSWAP_SUPPORTED is not defined");
 #endif
 
 #if defined(PNG_WRITE_PACK_SUPPORTED) && !defined(PNG_READ_PACK_SUPPORTED)
-   if (png_ptr->transformations & PNG_PACK)
+   if ((png_ptr->transformations & PNG_PACK) != 0)
       png_warning(png_ptr, "PNG_READ_PACK_SUPPORTED is not defined");
 #endif
 
 #if defined(PNG_WRITE_SHIFT_SUPPORTED) && !defined(PNG_READ_SHIFT_SUPPORTED)
-   if (png_ptr->transformations & PNG_SHIFT)
+   if ((png_ptr->transformations & PNG_SHIFT) != 0)
       png_warning(png_ptr, "PNG_READ_SHIFT_SUPPORTED is not defined");
 #endif
 
 #if defined(PNG_WRITE_BGR_SUPPORTED) && !defined(PNG_READ_BGR_SUPPORTED)
-   if (png_ptr->transformations & PNG_BGR)
+   if ((png_ptr->transformations & PNG_BGR) != 0)
       png_warning(png_ptr, "PNG_READ_BGR_SUPPORTED is not defined");
 #endif
 
 #if defined(PNG_WRITE_SWAP_SUPPORTED) && !defined(PNG_READ_SWAP_SUPPORTED)
-   if (png_ptr->transformations & PNG_SWAP_BYTES)
+   if ((png_ptr->transformations & PNG_SWAP_BYTES) != 0)
       png_warning(png_ptr, "PNG_READ_SWAP_SUPPORTED is not defined");
 #endif
    }
+#endif /* WARNINGS */
 
 #ifdef PNG_READ_INTERLACING_SUPPORTED
    /* If interlaced and we do not need a new row, combine row and return.
@@ -453,7 +455,8 @@
     * untransformed) and, because of the libpng API for interlaced images, this
     * means we must transform before de-interlacing.
     */
-   if (png_ptr->interlaced && (png_ptr->transformations & PNG_INTERLACE))
+   if (png_ptr->interlaced != 0 &&
+       (png_ptr->transformations & PNG_INTERLACE) != 0)
    {
       switch (png_ptr->pass)
       {
@@ -524,7 +527,7 @@
 
          default:
          case 6:
-            if (!(png_ptr->row_number & 1))
+            if ((png_ptr->row_number & 1) == 0)
             {
                png_read_finish_row(png_ptr);
                return;
@@ -534,7 +537,7 @@
    }
 #endif
 
-   if (!(png_ptr->mode & PNG_HAVE_IDAT))
+   if ((png_ptr->mode & PNG_HAVE_IDAT) == 0)
       png_error(png_ptr, "Invalid attempt to read row data");
 
    /* Fill the row with IDAT data: */
@@ -557,7 +560,7 @@
    memcpy(png_ptr->prev_row, png_ptr->row_buf, row_info.rowbytes + 1);
 
 #ifdef PNG_MNG_FEATURES_SUPPORTED
-   if ((png_ptr->mng_features_permitted & PNG_FLAG_MNG_FILTER_64) &&
+   if ((png_ptr->mng_features_permitted & PNG_FLAG_MNG_FILTER_64) != 0 &&
        (png_ptr->filter_type == PNG_INTRAPIXEL_DIFFERENCING))
    {
       /* Intrapixel differencing */
@@ -582,9 +585,9 @@
       png_error(png_ptr, "internal sequential row size calculation error");
 
 #ifdef PNG_READ_INTERLACING_SUPPORTED
-   /* Blow up interlaced rows to full size */
-   if (png_ptr->interlaced &&
-      (png_ptr->transformations & PNG_INTERLACE))
+   /* Expand interlaced rows to full size */
+   if (png_ptr->interlaced != 0 &&
+      (png_ptr->transformations & PNG_INTERLACE) != 0)
    {
       if (png_ptr->pass < 6)
          png_do_read_interlace(&row_info, png_ptr->row_buf + 1, png_ptr->pass,
@@ -612,7 +615,7 @@
       (*(png_ptr->read_row_fn))(png_ptr, png_ptr->row_number, png_ptr->pass);
 
 }
-#endif /* PNG_SEQUENTIAL_READ_SUPPORTED */
+#endif /* SEQUENTIAL_READ */
 
 #ifdef PNG_SEQUENTIAL_READ_SUPPORTED
 /* Read one or more rows of image data.  If the image is interlaced,
@@ -679,7 +682,7 @@
          dp++;
       }
 }
-#endif /* PNG_SEQUENTIAL_READ_SUPPORTED */
+#endif /* SEQUENTIAL_READ */
 
 #ifdef PNG_INDEX_SUPPORTED
 #define IDAT_HEADER_SIZE 8
@@ -849,7 +852,7 @@
       return;
 
 #ifdef PNG_READ_INTERLACING_SUPPORTED
-   if (!(png_ptr->flags & PNG_FLAG_ROW_INIT))
+   if ((png_ptr->flags & PNG_FLAG_ROW_INIT) == 0)
    {
       pass = png_set_interlace_handling(png_ptr);
       /* And make sure transforms are initialized. */
@@ -857,7 +860,8 @@
    }
    else
    {
-      if (png_ptr->interlaced && !(png_ptr->transformations & PNG_INTERLACE))
+      if (png_ptr->interlaced != 0 &&
+          (png_ptr->transformations & PNG_INTERLACE) == 0)
       {
          /* Caller called png_start_read_image or png_read_update_info without
           * first turning on the PNG_INTERLACE transform.  We can fix this here,
@@ -894,7 +898,7 @@
       }
    }
 }
-#endif /* PNG_SEQUENTIAL_READ_SUPPORTED */
+#endif /* SEQUENTIAL_READ */
 
 #ifdef PNG_SEQUENTIAL_READ_SUPPORTED
 /* Read the end of the PNG file.  Will not read past the end of the
@@ -917,7 +921,7 @@
     * still be pending IDAT data and an owned zstream.  Deal with this here.
     */
 #ifdef PNG_HANDLE_AS_UNKNOWN_SUPPORTED
-   if (!png_chunk_unknown_handling(png_ptr, png_IDAT))
+   if (png_chunk_unknown_handling(png_ptr, png_IDAT) == 0)
 #endif
       png_read_finish_IDAT(png_ptr);
 
@@ -947,7 +951,8 @@
       {
          if (chunk_name == png_IDAT)
          {
-            if ((length > 0) || (png_ptr->mode & PNG_HAVE_CHUNK_AFTER_IDAT))
+            if ((length > 0) ||
+                (png_ptr->mode & PNG_HAVE_CHUNK_AFTER_IDAT) != 0)
                png_benign_error(png_ptr, "Too many IDATs found");
          }
          png_handle_unknown(png_ptr, info_ptr, length, keep);
@@ -961,7 +966,7 @@
          /* Zero length IDATs are legal after the last IDAT has been
           * read, but not after other chunks have been read.
           */
-         if ((length > 0) || (png_ptr->mode & PNG_HAVE_CHUNK_AFTER_IDAT))
+         if ((length > 0) || (png_ptr->mode & PNG_HAVE_CHUNK_AFTER_IDAT) != 0)
             png_benign_error(png_ptr, "Too many IDATs found");
 
          png_crc_finish(png_ptr, length);
@@ -1057,9 +1062,9 @@
       else
          png_handle_unknown(png_ptr, info_ptr, length,
             PNG_HANDLE_CHUNK_AS_DEFAULT);
-   } while (!(png_ptr->mode & PNG_HAVE_IEND));
+   } while ((png_ptr->mode & PNG_HAVE_IEND) == 0);
 }
-#endif /* PNG_SEQUENTIAL_READ_SUPPORTED */
+#endif /* SEQUENTIAL_READ */
 
 /* Free all memory used in the read struct */
 static void
@@ -1072,22 +1077,33 @@
 #endif
 
    png_free(png_ptr, png_ptr->big_row_buf);
+   png_ptr->big_row_buf = NULL;
    png_free(png_ptr, png_ptr->big_prev_row);
+   png_ptr->big_prev_row = NULL;
    png_free(png_ptr, png_ptr->read_buffer);
+   png_ptr->read_buffer = NULL;
 
 #ifdef PNG_READ_QUANTIZE_SUPPORTED
    png_free(png_ptr, png_ptr->palette_lookup);
+   png_ptr->palette_lookup = NULL;
    png_free(png_ptr, png_ptr->quantize_index);
+   png_ptr->quantize_index = NULL;
 #endif
 
-   if (png_ptr->free_me & PNG_FREE_PLTE)
+   if ((png_ptr->free_me & PNG_FREE_PLTE) != 0)
+   {
       png_zfree(png_ptr, png_ptr->palette);
+      png_ptr->palette = NULL;
+   }
    png_ptr->free_me &= ~PNG_FREE_PLTE;
 
 #if defined(PNG_tRNS_SUPPORTED) || \
     defined(PNG_READ_EXPAND_SUPPORTED) || defined(PNG_READ_BACKGROUND_SUPPORTED)
-   if (png_ptr->free_me & PNG_FREE_TRNS)
+   if ((png_ptr->free_me & PNG_FREE_TRNS) != 0)
+   {
       png_free(png_ptr, png_ptr->trans_alpha);
+      png_ptr->trans_alpha = NULL;
+   }
    png_ptr->free_me &= ~PNG_FREE_TRNS;
 #endif
 
@@ -1095,15 +1111,18 @@
 
 #ifdef PNG_PROGRESSIVE_READ_SUPPORTED
    png_free(png_ptr, png_ptr->save_buffer);
+   png_ptr->save_buffer = NULL;
 #endif
 
-#if defined(PNG_STORE_UNKNOWN_CHUNKS_SUPPORTED) &&\
+#if defined(PNG_STORE_UNKNOWN_CHUNKS_SUPPORTED) && \
    defined(PNG_READ_UNKNOWN_CHUNKS_SUPPORTED)
    png_free(png_ptr, png_ptr->unknown_chunk.data);
+   png_ptr->unknown_chunk.data = NULL;
 #endif
 
 #ifdef PNG_SET_UNKNOWN_CHUNKS_SUPPORTED
    png_free(png_ptr, png_ptr->chunk_list);
+   png_ptr->chunk_list = NULL;
 #endif
 
 #ifdef PNG_INDEX_SUPPORTED
@@ -1194,10 +1213,10 @@
 
    /* 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.
-      */
+   if ((transforms & PNG_TRANSFORM_SCALE_16) != 0)
+      /* 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
@@ -1208,7 +1227,7 @@
     * 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)
+   if ((transforms & PNG_TRANSFORM_STRIP_16) != 0)
 #ifdef PNG_READ_STRIP_16_TO_8_SUPPORTED
       png_set_strip_16(png_ptr);
 #else
@@ -1218,7 +1237,7 @@
    /* Strip alpha bytes from the input data without combining with
     * the background (not recommended).
     */
-   if (transforms & PNG_TRANSFORM_STRIP_ALPHA)
+   if ((transforms & PNG_TRANSFORM_STRIP_ALPHA) != 0)
 #ifdef PNG_READ_STRIP_ALPHA_SUPPORTED
       png_set_strip_alpha(png_ptr);
 #else
@@ -1228,7 +1247,7 @@
    /* 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)
+   if ((transforms & PNG_TRANSFORM_PACKING) != 0)
 #ifdef PNG_READ_PACK_SUPPORTED
       png_set_packing(png_ptr);
 #else
@@ -1238,7 +1257,7 @@
    /* 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)
+   if ((transforms & PNG_TRANSFORM_PACKSWAP) != 0)
 #ifdef PNG_READ_PACKSWAP_SUPPORTED
       png_set_packswap(png_ptr);
 #else
@@ -1250,7 +1269,7 @@
     * 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 ((transforms & PNG_TRANSFORM_EXPAND) != 0)
 #ifdef PNG_READ_EXPAND_SUPPORTED
       png_set_expand(png_ptr);
 #else
@@ -1262,7 +1281,7 @@
 
    /* Invert monochrome files to have 0 as white and 1 as black
     */
-   if (transforms & PNG_TRANSFORM_INVERT_MONO)
+   if ((transforms & PNG_TRANSFORM_INVERT_MONO) != 0)
 #ifdef PNG_READ_INVERT_SUPPORTED
       png_set_invert_mono(png_ptr);
 #else
@@ -1273,16 +1292,16 @@
     * [0,65535] to the original [0,7] or [0,31], or whatever range the
     * colors were originally in:
     */
-   if (transforms & PNG_TRANSFORM_SHIFT)
+   if ((transforms & PNG_TRANSFORM_SHIFT) != 0)
 #ifdef PNG_READ_SHIFT_SUPPORTED
-      if (info_ptr->valid & PNG_INFO_sBIT)
+      if ((info_ptr->valid & PNG_INFO_sBIT) != 0)
          png_set_shift(png_ptr, &info_ptr->sig_bit);
 #else
       png_app_error(png_ptr, "PNG_TRANSFORM_SHIFT not supported");
 #endif
 
    /* Flip the RGB pixels to BGR (or RGBA to BGRA) */
-   if (transforms & PNG_TRANSFORM_BGR)
+   if ((transforms & PNG_TRANSFORM_BGR) != 0)
 #ifdef PNG_READ_BGR_SUPPORTED
       png_set_bgr(png_ptr);
 #else
@@ -1290,7 +1309,7 @@
 #endif
 
    /* Swap the RGBA or GA data to ARGB or AG (or BGRA to ABGR) */
-   if (transforms & PNG_TRANSFORM_SWAP_ALPHA)
+   if ((transforms & PNG_TRANSFORM_SWAP_ALPHA) != 0)
 #ifdef PNG_READ_SWAP_ALPHA_SUPPORTED
       png_set_swap_alpha(png_ptr);
 #else
@@ -1298,7 +1317,7 @@
 #endif
 
    /* Swap bytes of 16-bit files to least significant byte first */
-   if (transforms & PNG_TRANSFORM_SWAP_ENDIAN)
+   if ((transforms & PNG_TRANSFORM_SWAP_ENDIAN) != 0)
 #ifdef PNG_READ_SWAP_SUPPORTED
       png_set_swap(png_ptr);
 #else
@@ -1307,7 +1326,7 @@
 
 /* Added at libpng-1.2.41 */
    /* Invert the alpha channel from opacity to transparency */
-   if (transforms & PNG_TRANSFORM_INVERT_ALPHA)
+   if ((transforms & PNG_TRANSFORM_INVERT_ALPHA) != 0)
 #ifdef PNG_READ_INVERT_ALPHA_SUPPORTED
       png_set_invert_alpha(png_ptr);
 #else
@@ -1316,7 +1335,7 @@
 
 /* Added at libpng-1.2.41 */
    /* Expand grayscale image to RGB */
-   if (transforms & PNG_TRANSFORM_GRAY_TO_RGB)
+   if ((transforms & PNG_TRANSFORM_GRAY_TO_RGB) != 0)
 #ifdef PNG_READ_GRAY_TO_RGB_SUPPORTED
       png_set_gray_to_rgb(png_ptr);
 #else
@@ -1324,7 +1343,7 @@
 #endif
 
 /* Added at libpng-1.5.4 */
-   if (transforms & PNG_TRANSFORM_EXPAND_16)
+   if ((transforms & PNG_TRANSFORM_EXPAND_16) != 0)
 #ifdef PNG_READ_EXPAND_16_SUPPORTED
       png_set_expand_16(png_ptr);
 #else
@@ -1361,7 +1380,7 @@
 
       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_malloc(png_ptr, info_ptr->rowbytes));
    }
 
    png_read_image(png_ptr, info_ptr->row_pointers);
@@ -1372,8 +1391,8 @@
 
    PNG_UNUSED(params)
 }
-#endif /* PNG_INFO_IMAGE_SUPPORTED */
-#endif /* PNG_SEQUENTIAL_READ_SUPPORTED */
+#endif /* INFO_IMAGE */
+#endif /* SEQUENTIAL_READ */
 
 #ifdef PNG_SIMPLIFIED_READ_SUPPORTED
 /* SIMPLIFIED READ
@@ -1391,7 +1410,7 @@
 #  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.
+ * processing may be needed to convert the data to color-map indices.
  */
 #define PNG_CMAP_NONE      0
 #define PNG_CMAP_GA        1 /* Process GA data to a color-map with alpha */
@@ -1482,10 +1501,10 @@
 {
    png_uint_32 format = 0;
 
-   if (png_ptr->color_type & PNG_COLOR_MASK_COLOR)
+   if ((png_ptr->color_type & PNG_COLOR_MASK_COLOR) != 0)
       format |= PNG_FORMAT_FLAG_COLOR;
 
-   if (png_ptr->color_type & PNG_COLOR_MASK_ALPHA)
+   if ((png_ptr->color_type & PNG_COLOR_MASK_ALPHA) != 0)
       format |= PNG_FORMAT_FLAG_ALPHA;
 
    /* Use png_ptr here, not info_ptr, because by examination png_handle_tRNS
@@ -1499,7 +1518,7 @@
    if (png_ptr->bit_depth == 16)
       format |= PNG_FORMAT_FLAG_LINEAR;
 
-   if (png_ptr->color_type & PNG_COLOR_MASK_PALETTE)
+   if ((png_ptr->color_type & PNG_COLOR_MASK_PALETTE) != 0)
       format |= PNG_FORMAT_FLAG_COLORMAP;
 
    return format;
@@ -1599,7 +1618,7 @@
    {
       if (file != NULL)
       {
-         if (png_image_read_init(image))
+         if (png_image_read_init(image) != 0)
          {
             /* This is slightly evil, but png_init_io doesn't do anything other
              * than this and we haven't changed the standard IO functions so
@@ -1633,7 +1652,7 @@
 
          if (fp != NULL)
          {
-            if (png_image_read_init(image))
+            if (png_image_read_init(image) != 0)
             {
                image->opaque->png_ptr->io_ptr = fp;
                image->opaque->owned_file = 1;
@@ -1659,7 +1678,7 @@
 
    return 0;
 }
-#endif /* PNG_STDIO_SUPPORTED */
+#endif /* STDIO */
 
 static void PNGCBAPI
 png_image_memory_read(png_structp png_ptr, png_bytep out, png_size_t need)
@@ -1698,7 +1717,7 @@
    {
       if (memory != NULL && size > 0)
       {
-         if (png_image_read_init(image))
+         if (png_image_read_init(image) != 0)
          {
             /* Now set the IO functions to read from the memory buffer and
              * store it into io_ptr.  Again do this in-place to avoid calling a
@@ -1770,14 +1789,14 @@
 
        /* But do not ignore image data handling chunks */
        png_set_keep_unknown_chunks(png_ptr, PNG_HANDLE_CHUNK_AS_DEFAULT,
-         chunks_to_process, (sizeof chunks_to_process)/5);
+         chunks_to_process, (int)/*SAFE*/(sizeof chunks_to_process)/5);
     }
 }
 
 #  define PNG_SKIP_CHUNKS(p) png_image_skip_unused_chunks(p)
 #else
 #  define PNG_SKIP_CHUNKS(p) ((void)0)
-#endif /* PNG_HANDLE_AS_UNKNOWN_SUPPORTED */
+#endif /* HANDLE_AS_UNKNOWN */
 
 /* The following macro gives the exact rounded answer for all values in the
  * range 0..255 (it actually divides by 51.2, but the rounding still generates
@@ -1790,9 +1809,9 @@
 set_file_encoding(png_image_read_control *display)
 {
    png_fixed_point g = display->image->opaque->png_ptr->colorspace.gamma;
-   if (png_gamma_significant(g))
+   if (png_gamma_significant(g) != 0)
    {
-      if (png_gamma_not_sRGB(g))
+      if (png_gamma_not_sRGB(g) != 0)
       {
          display->file_encoding = P_FILE;
          display->gamma_to_linear = png_reciprocal(g);
@@ -1835,10 +1854,11 @@
          value *= 257;
          break;
 
+#ifdef __GNUC__
       default:
          png_error(display->image->opaque->png_ptr,
             "unexpected encoding (internal error)");
-         break;
+#endif
    }
 
    return value;
@@ -1887,7 +1907,7 @@
    png_uint_32 alpha, int encoding)
 {
    png_imagep image = display->image;
-   const int output_encoding = (image->format & PNG_FORMAT_FLAG_LINEAR) ?
+   const int output_encoding = (image->format & PNG_FORMAT_FLAG_LINEAR) != 0 ?
       P_LINEAR : P_sRGB;
    const int convert_to_Y = (image->format & PNG_FORMAT_FLAG_COLOR) == 0 &&
       (red != green || green != blue);
@@ -1917,7 +1937,7 @@
       green = png_gamma_16bit_correct(green*257, g);
       blue = png_gamma_16bit_correct(blue*257, g);
 
-      if (convert_to_Y || output_encoding == P_LINEAR)
+      if (convert_to_Y != 0 || output_encoding == P_LINEAR)
       {
          alpha *= 257;
          encoding = P_LINEAR;
@@ -1944,7 +1964,8 @@
       encoding = P_LINEAR;
    }
 
-   else if (encoding == P_sRGB && (convert_to_Y || output_encoding == P_LINEAR))
+   else if (encoding == P_sRGB &&
+       (convert_to_Y  != 0 || output_encoding == P_LINEAR))
    {
       /* The values are 8-bit sRGB values, but must be converted to 16-bit
        * linear.
@@ -1959,7 +1980,7 @@
    /* This is set if the color isn't gray but the output is. */
    if (encoding == P_LINEAR)
    {
-      if (convert_to_Y)
+      if (convert_to_Y != 0)
       {
          /* NOTE: these values are copied from png_do_rgb_to_gray */
          png_uint_32 y = (png_uint_32)6968 * red  + (png_uint_32)23434 * green +
@@ -1974,6 +1995,7 @@
             y = (y + 128) >> 8;
             y *= 255;
             y = PNG_sRGB_FROM_LINEAR((y + 64) >> 7);
+            alpha = PNG_DIV257(alpha);
             encoding = P_sRGB;
          }
 
@@ -2002,7 +2024,7 @@
 #        define afirst 0
 #     endif
 #     ifdef PNG_FORMAT_BGR_SUPPORTED
-         const int bgr = (image->format & PNG_FORMAT_FLAG_BGR) ? 2 : 0;
+         const int bgr = (image->format & PNG_FORMAT_FLAG_BGR) != 0 ? 2 : 0;
 #     else
 #        define bgr 0
 #     endif
@@ -2214,7 +2236,7 @@
 
    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) ?
+   const int output_encoding = (output_format & PNG_FORMAT_FLAG_LINEAR) != 0 ?
       P_LINEAR : P_sRGB;
 
    unsigned int cmap_entries;
@@ -2253,7 +2275,7 @@
       else
       {
          back_g = display->background->green;
-         if (output_format & PNG_FORMAT_FLAG_COLOR)
+         if ((output_format & PNG_FORMAT_FLAG_COLOR) != 0)
          {
             back_r = display->background->red;
             back_b = display->background->blue;
@@ -2358,7 +2380,7 @@
             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
+             * color-map indices; however, they may need to be expanded to 1
              * byte per pixel.  This is what png_set_packing does (i.e., it
              * unpacks the bit values into bytes.)
              */
@@ -2395,7 +2417,7 @@
             {
                unsigned int back_alpha;
 
-               if (output_format & PNG_FORMAT_FLAG_ALPHA)
+               if ((output_format & PNG_FORMAT_FLAG_ALPHA) != 0)
                   back_alpha = 0;
 
                else
@@ -2436,16 +2458,22 @@
                      output_processing = PNG_CMAP_NONE;
                      break;
                   }
-
+#ifdef __COVERITY__
+                 /* Coverity claims that output_encoding cannot be 2 (P_LINEAR)
+                  * here.
+                  */
+                  back_alpha = 255;
+#else
                   back_alpha = output_encoding == P_LINEAR ? 65535 : 255;
+#endif
                }
 
                /* output_processing means that the libpng-processed row will be
                 * 8-bit GA and it has to be processing to single byte color-map
                 * values.  Entry 254 is replaced by either a completely
                 * transparent entry or by the background color at full
-                * precision (and the background color is not a simple gray leve
-                * in this case.)
+                * precision (and the background color is not a simple gray
+                * level in this case.)
                 */
                expand_tRNS = 1;
                output_processing = PNG_CMAP_TRANS;
@@ -2476,7 +2504,7 @@
           */
          data_encoding = P_sRGB;
 
-         if (output_format & PNG_FORMAT_FLAG_ALPHA)
+         if ((output_format & PNG_FORMAT_FLAG_ALPHA) != 0)
          {
             if (PNG_GA_COLORMAP_ENTRIES > image->colormap_entries)
                png_error(png_ptr, "gray+alpha color-map: too few entries");
@@ -2562,7 +2590,14 @@
                 */
                background_index = i;
                png_create_colormap_entry(display, i++, back_r, back_g, back_b,
-                  output_encoding == P_LINEAR ? 65535U : 255U, output_encoding);
+#ifdef __COVERITY__
+                 /* Coverity claims that output_encoding cannot be 2 (P_LINEAR)
+                  * here.
+                  */ 255U,
+#else
+                  output_encoding == P_LINEAR ? 65535U : 255U,
+#endif
+                  output_encoding);
 
                /* For non-opaque input composite on the sRGB background - this
                 * requires inverting the encoding for each component.  The input
@@ -2671,7 +2706,7 @@
                 */
                if ((png_ptr->color_type == PNG_COLOR_TYPE_RGB_ALPHA ||
                   png_ptr->num_trans > 0) &&
-                  png_gamma_not_sRGB(png_ptr->colorspace.gamma))
+                  png_gamma_not_sRGB(png_ptr->colorspace.gamma) != 0)
                {
                   cmap_entries = make_gray_file_colormap(display);
                   data_encoding = P_FILE;
@@ -2759,7 +2794,7 @@
                /* Is there alpha in the output too?  If so all four channels are
                 * processed into a special RGB cube with alpha support.
                 */
-               if (output_format & PNG_FORMAT_FLAG_ALPHA)
+               if ((output_format & PNG_FORMAT_FLAG_ALPHA) != 0)
                {
                   png_uint_32 r;
 
@@ -2923,7 +2958,7 @@
                num_trans = 0;
 
             output_processing = PNG_CMAP_NONE;
-            data_encoding = P_FILE; /* Don't change from color-map indicies */
+            data_encoding = P_FILE; /* Don't change from color-map indices */
             cmap_entries = png_ptr->num_palette;
             if (cmap_entries > 256)
                cmap_entries = 256;
@@ -2933,7 +2968,7 @@
 
             for (i=0; i < cmap_entries; ++i)
             {
-               if (do_background && i < num_trans && trans[i] < 255)
+               if (do_background != 0 && i < num_trans && trans[i] < 255)
                {
                   if (trans[i] == 0)
                      png_create_colormap_entry(display, i, back_r, back_g,
@@ -2963,7 +2998,7 @@
                      i < num_trans ? trans[i] : 255U, P_FILE/*8-bit*/);
             }
 
-            /* The PNG data may have indicies packed in fewer than 8 bits, it
+            /* The PNG data may have indices packed in fewer than 8 bits, it
              * must be expanded if so.
              */
             if (png_ptr->bit_depth < 8)
@@ -2974,20 +3009,15 @@
       default:
          png_error(png_ptr, "invalid PNG color type");
          /*NOT REACHED*/
-         break;
    }
 
    /* Now deal with the output processing */
-   if (expand_tRNS && png_ptr->num_trans > 0 &&
-      (png_ptr->color_type & PNG_COLOR_MASK_ALPHA) == 0)
+   if (expand_tRNS != 0 && png_ptr->num_trans > 0 &&
+       (png_ptr->color_type & PNG_COLOR_MASK_ALPHA) == 0)
       png_set_tRNS_to_alpha(png_ptr);
 
    switch (data_encoding)
    {
-      default:
-         png_error(png_ptr, "bad data option (internal error)");
-         break;
-
       case P_sRGB:
          /* Change to 8-bit sRGB */
          png_set_alpha_mode_fixed(png_ptr, PNG_ALPHA_PNG, PNG_GAMMA_sRGB);
@@ -2997,6 +3027,11 @@
          if (png_ptr->bit_depth > 8)
             png_set_scale_16(png_ptr);
          break;
+
+#ifdef __GNUC__
+      default:
+         png_error(png_ptr, "bad data option (internal error)");
+#endif
    }
 
    if (cmap_entries > 256 || cmap_entries > image->colormap_entries)
@@ -3395,7 +3430,8 @@
       png_uint_32  height = image->height;
       png_uint_32  width = image->width;
       ptrdiff_t    step_row = display->row_bytes;
-      unsigned int channels = (image->format & PNG_FORMAT_FLAG_COLOR) ? 3 : 1;
+      unsigned int channels =
+          (image->format & PNG_FORMAT_FLAG_COLOR) != 0 ? 3 : 1;
       int pass;
 
       for (pass = 0; pass < passes; ++pass)
@@ -3488,11 +3524,11 @@
  * PNG_COMPOSITE
  * PNG_GAMMA
  *
- * This is a work-round for the fact that both the PNG_RGB_TO_GRAY and
+ * This is a work-around for the fact that both the PNG_RGB_TO_GRAY and
  * PNG_COMPOSITE code performs gamma correction, so we get double gamma
- * correction.  The fix-up is to prevent the PNG_COMPOSITE operation happening
- * inside libpng, so this routine sees an 8 or 16-bit gray+alpha row and handles
- * the removal or pre-multiplication of the alpha channel.
+ * correction.  The fix-up is to prevent the PNG_COMPOSITE operation from
+ * happening inside libpng, so this routine sees an 8 or 16-bit gray+alpha
+ * row and handles the removal or pre-multiplication of the alpha channel.
  */
 static int
 png_image_read_background(png_voidp argument)
@@ -3546,10 +3582,6 @@
     */
    switch (info_ptr->bit_depth)
    {
-      default:
-         png_error(png_ptr, "unexpected bit depth");
-         break;
-
       case 8:
          /* 8-bit sRGB gray values with an alpha channel; the alpha channel is
           * to be removed by composing on a background: either the row if
@@ -3693,7 +3725,8 @@
             int swap_alpha = 0;
 
 #           ifdef PNG_SIMPLIFIED_READ_AFIRST_SUPPORTED
-               if (preserve_alpha && (image->format & PNG_FORMAT_FLAG_AFIRST))
+               if (preserve_alpha != 0 &&
+                   (image->format & PNG_FORMAT_FLAG_AFIRST) != 0)
                   swap_alpha = 1;
 #           endif
 
@@ -3757,7 +3790,7 @@
                         component = 0;
 
                      outrow[swap_alpha] = (png_uint_16)component;
-                     if (preserve_alpha)
+                     if (preserve_alpha != 0)
                         outrow[1 ^ swap_alpha] = alpha;
 
                      inrow += 2; /* components and alpha channel */
@@ -3766,6 +3799,11 @@
             }
          }
          break;
+
+#ifdef __GNUC__
+      default:
+         png_error(png_ptr, "unexpected bit depth");
+#endif
    }
 
    return 1;
@@ -3802,10 +3840,10 @@
       int mode; /* alpha mode */
 
       /* Do this first so that we have a record if rgb to gray is happening. */
-      if (change & PNG_FORMAT_FLAG_COLOR)
+      if ((change & PNG_FORMAT_FLAG_COLOR) != 0)
       {
          /* gray<->color transformation required. */
-         if (format & PNG_FORMAT_FLAG_COLOR)
+         if ((format & PNG_FORMAT_FLAG_COLOR) != 0)
             png_set_gray_to_rgb(png_ptr);
 
          else
@@ -3823,7 +3861,7 @@
              * enormous change) 'do_local_background' is used to indicate that
              * the problem exists.
              */
-            if (base_format & PNG_FORMAT_FLAG_ALPHA)
+            if ((base_format & PNG_FORMAT_FLAG_ALPHA) != 0)
                do_local_background = 1/*maybe*/;
 
             png_set_rgb_to_gray_fixed(png_ptr, PNG_ERROR_ACTION_NONE,
@@ -3838,8 +3876,8 @@
       {
          png_fixed_point input_gamma_default;
 
-         if ((base_format & PNG_FORMAT_FLAG_LINEAR) &&
-            (image->flags & PNG_IMAGE_FLAG_16BIT_sRGB) == 0)
+         if ((base_format & PNG_FORMAT_FLAG_LINEAR) != 0 &&
+             (image->flags & PNG_IMAGE_FLAG_16BIT_sRGB) == 0)
             input_gamma_default = PNG_GAMMA_LINEAR;
          else
             input_gamma_default = PNG_DEFAULT_sRGB;
@@ -3850,12 +3888,12 @@
          png_set_alpha_mode_fixed(png_ptr, PNG_ALPHA_PNG, input_gamma_default);
       }
 
-      if (linear)
+      if (linear != 0)
       {
          /* If there *is* an alpha channel in the input it must be multiplied
           * out; use PNG_ALPHA_STANDARD, otherwise just use PNG_ALPHA_PNG.
           */
-         if (base_format & PNG_FORMAT_FLAG_ALPHA)
+         if ((base_format & PNG_FORMAT_FLAG_ALPHA) != 0)
             mode = PNG_ALPHA_STANDARD; /* associated alpha */
 
          else
@@ -3876,7 +3914,7 @@
        *
        * TODO: fix libpng and remove this.
        */
-      if (do_local_background)
+      if (do_local_background != 0)
       {
          png_fixed_point gtest;
 
@@ -3886,7 +3924,7 @@
           * final value.
           */
          if (png_muldiv(&gtest, output_gamma, png_ptr->colorspace.gamma,
-               PNG_FP_1) && !png_gamma_significant(gtest))
+               PNG_FP_1) != 0 && png_gamma_significant(gtest) == 0)
             do_local_background = 0;
 
          else if (mode == PNG_ALPHA_STANDARD)
@@ -3899,9 +3937,9 @@
       }
 
       /* If the bit-depth changes then handle that here. */
-      if (change & PNG_FORMAT_FLAG_LINEAR)
+      if ((change & PNG_FORMAT_FLAG_LINEAR) != 0)
       {
-         if (linear /*16-bit output*/)
+         if (linear != 0 /*16-bit output*/)
             png_set_expand_16(png_ptr);
 
          else /* 8-bit output */
@@ -3911,24 +3949,24 @@
       }
 
       /* Now the background/alpha channel changes. */
-      if (change & PNG_FORMAT_FLAG_ALPHA)
+      if ((change & PNG_FORMAT_FLAG_ALPHA) != 0)
       {
          /* Removing an alpha channel requires composition for the 8-bit
           * formats; for the 16-bit it is already done, above, by the
           * pre-multiplication and the channel just needs to be stripped.
           */
-         if (base_format & PNG_FORMAT_FLAG_ALPHA)
+         if ((base_format & PNG_FORMAT_FLAG_ALPHA) != 0)
          {
             /* If RGB->gray is happening the alpha channel must be left and the
              * operation completed locally.
              *
              * TODO: fix libpng and remove this.
              */
-            if (do_local_background)
+            if (do_local_background != 0)
                do_local_background = 2/*required*/;
 
             /* 16-bit output: just remove the channel */
-            else if (linear) /* compose on black (well, pre-multiply) */
+            else if (linear != 0) /* compose on black (well, pre-multiply) */
                png_set_strip_alpha(png_ptr);
 
             /* 8-bit output: do an appropriate compose */
@@ -3975,14 +4013,14 @@
             png_uint_32 filler; /* opaque filler */
             int where;
 
-            if (linear)
+            if (linear != 0)
                filler = 65535;
 
             else
                filler = 255;
 
 #           ifdef PNG_FORMAT_AFIRST_SUPPORTED
-               if (format & PNG_FORMAT_FLAG_AFIRST)
+               if ((format & PNG_FORMAT_FLAG_AFIRST) != 0)
                {
                   where = PNG_FILLER_BEFORE;
                   change &= ~PNG_FORMAT_FLAG_AFIRST;
@@ -4006,12 +4044,12 @@
       png_set_alpha_mode_fixed(png_ptr, mode, output_gamma);
 
 #     ifdef PNG_FORMAT_BGR_SUPPORTED
-         if (change & PNG_FORMAT_FLAG_BGR)
+         if ((change & PNG_FORMAT_FLAG_BGR) != 0)
          {
             /* Check only the output format; PNG is never BGR; don't do this if
              * the output is gray, but fix up the 'format' value in that case.
              */
-            if (format & PNG_FORMAT_FLAG_COLOR)
+            if ((format & PNG_FORMAT_FLAG_COLOR) != 0)
                png_set_bgr(png_ptr);
 
             else
@@ -4022,14 +4060,14 @@
 #     endif
 
 #     ifdef PNG_FORMAT_AFIRST_SUPPORTED
-         if (change & PNG_FORMAT_FLAG_AFIRST)
+         if ((change & PNG_FORMAT_FLAG_AFIRST) != 0)
          {
             /* Only relevant if there is an alpha channel - it's particularly
              * important to handle this correctly because do_local_compose may
              * be set above and then libpng will keep the alpha channel for this
              * code to remove.
              */
-            if (format & PNG_FORMAT_FLAG_ALPHA)
+            if ((format & PNG_FORMAT_FLAG_ALPHA) != 0)
             {
                /* Disable this if doing a local background,
                 * TODO: remove this when local background is no longer required.
@@ -4048,16 +4086,16 @@
       /* If the *output* is 16-bit then we need to check for a byte-swap on this
        * architecture.
        */
-      if (linear)
+      if (linear != 0)
       {
          PNG_CONST png_uint_16 le = 0x0001;
 
-         if (*(png_const_bytep)&le)
+         if ((*(png_const_bytep) & le) != 0)
             png_set_swap(png_ptr);
       }
 
       /* If change is not now 0 some transformation is missing - error out. */
-      if (change)
+      if (change != 0)
          png_error(png_ptr, "png_read_image: unsupported transformation");
    }
 
@@ -4069,7 +4107,7 @@
     *
     * TODO: remove the do_local_background fixup below.
     */
-   if (!do_local_compose && do_local_background != 2)
+   if (do_local_compose == 0 && do_local_background != 2)
       passes = png_set_interlace_handling(png_ptr);
 
    png_read_update_info(png_ptr, info_ptr);
@@ -4077,13 +4115,13 @@
    {
       png_uint_32 info_format = 0;
 
-      if (info_ptr->color_type & PNG_COLOR_MASK_COLOR)
+      if ((info_ptr->color_type & PNG_COLOR_MASK_COLOR) != 0)
          info_format |= PNG_FORMAT_FLAG_COLOR;
 
-      if (info_ptr->color_type & PNG_COLOR_MASK_ALPHA)
+      if ((info_ptr->color_type & PNG_COLOR_MASK_ALPHA) != 0)
       {
          /* do_local_compose removes this channel below. */
-         if (!do_local_compose)
+         if (do_local_compose == 0)
          {
             /* do_local_background does the same if required. */
             if (do_local_background != 2 ||
@@ -4092,21 +4130,21 @@
          }
       }
 
-      else if (do_local_compose) /* internal error */
+      else if (do_local_compose != 0) /* internal error */
          png_error(png_ptr, "png_image_read: alpha channel lost");
 
       if (info_ptr->bit_depth == 16)
          info_format |= PNG_FORMAT_FLAG_LINEAR;
 
 #     ifdef PNG_FORMAT_BGR_SUPPORTED
-         if (png_ptr->transformations & PNG_BGR)
+         if ((png_ptr->transformations & PNG_BGR) != 0)
             info_format |= PNG_FORMAT_FLAG_BGR;
 #     endif
 
 #     ifdef PNG_FORMAT_AFIRST_SUPPORTED
          if (do_local_background == 2)
          {
-            if (format & PNG_FORMAT_FLAG_AFIRST)
+            if ((format & PNG_FORMAT_FLAG_AFIRST) != 0)
                info_format |= PNG_FORMAT_FLAG_AFIRST;
          }
 
@@ -4135,7 +4173,7 @@
       png_voidp first_row = display->buffer;
       ptrdiff_t row_bytes = display->row_stride;
 
-      if (linear)
+      if (linear != 0)
          row_bytes *= 2;
 
       /* The following expression is designed to work correctly whether it gives
@@ -4152,7 +4190,7 @@
       display->row_bytes = row_bytes;
    }
 
-   if (do_local_compose)
+   if (do_local_compose != 0)
    {
       int result;
       png_voidp row = png_malloc(png_ptr, png_get_rowbytes(png_ptr, info_ptr));
@@ -4235,7 +4273,7 @@
             /* Choose the correct 'end' routine; for the color-map case all the
              * setup has already been done.
              */
-            if (image->format & PNG_FORMAT_FLAG_COLORMAP)
+            if ((image->format & PNG_FORMAT_FLAG_COLORMAP) != 0)
                result =
                   png_safe_execute(image, png_image_read_colormap, &display) &&
                   png_safe_execute(image, png_image_read_colormapped, &display);
@@ -4265,5 +4303,5 @@
    return 0;
 }
 
-#endif /* PNG_SIMPLIFIED_READ_SUPPORTED */
-#endif /* PNG_READ_SUPPORTED */
+#endif /* SIMPLIFIED_READ */
+#endif /* READ */