Imported Upstream version 1.6.39
[platform/upstream/libpng.git] / contrib / libtests / pngimage.c
index f130c04..be176b2 100644 (file)
@@ -1,9 +1,9 @@
+
 /* pngimage.c
  *
+ * Copyright (c) 2021 Cosmin Truta
  * Copyright (c) 2015,2016 John Cunningham Bowler
  *
- * Last changed in libpng 1.6.24 [August 4, 2016]
- *
  * This code is released under the libpng license.
  * For conditions of distribution and use, see the disclaimer
  * and license in png.h
@@ -12,6 +12,7 @@
  * using png_read_png and then write with png_write_png.  Test all possible
  * transforms.
  */
+
 #include <stdarg.h>
 #include <stdlib.h>
 #include <string.h>
@@ -317,11 +318,10 @@ transform_name(int t)
 
    t &= -t; /* first set bit */
 
-   for (i=0; i<TTABLE_SIZE; ++i) if (transform_info[i].name != NULL)
-   {
-      if ((transform_info[i].transform & t) != 0)
-         return transform_info[i].name;
-   }
+   for (i=0; i<TTABLE_SIZE; ++i)
+      if (transform_info[i].name != NULL)
+         if ((transform_info[i].transform & t) != 0)
+            return transform_info[i].name;
 
    return "invalid transform";
 }
@@ -338,13 +338,16 @@ validate_T(void)
 {
    unsigned int i;
 
-   for (i=0; i<TTABLE_SIZE; ++i) if (transform_info[i].name != NULL)
+   for (i=0; i<TTABLE_SIZE; ++i)
    {
-      if (transform_info[i].when & TRANSFORM_R)
-         read_transforms |= transform_info[i].transform;
+      if (transform_info[i].name != NULL)
+      {
+         if (transform_info[i].when & TRANSFORM_R)
+            read_transforms |= transform_info[i].transform;
 
-      if (transform_info[i].when & TRANSFORM_W)
-         write_transforms |= transform_info[i].transform;
+         if (transform_info[i].when & TRANSFORM_W)
+            write_transforms |= transform_info[i].transform;
+      }
    }
 
    /* Reversible transforms are those which are supported on both read and
@@ -962,21 +965,24 @@ update_display(struct display *dp)
       int bd = dp->bit_depth;
       unsigned int i;
 
-      for (i=0; i<TTABLE_SIZE; ++i) if (transform_info[i].name != NULL)
+      for (i=0; i<TTABLE_SIZE; ++i)
       {
-         int transform = transform_info[i].transform;
-
-         if ((transform_info[i].valid_chunks == 0 ||
-               (transform_info[i].valid_chunks & chunks) != 0) &&
-            (transform_info[i].color_mask_required & ct) ==
-               transform_info[i].color_mask_required &&
-            (transform_info[i].color_mask_absent & ct) == 0 &&
-            (transform_info[i].bit_depths & bd) != 0 &&
-            (transform_info[i].when & TRANSFORM_R) != 0)
-            active |= transform;
-
-         else if ((transform_info[i].when & TRANSFORM_R) != 0)
-            inactive |= transform;
+         if (transform_info[i].name != NULL)
+         {
+            int transform = transform_info[i].transform;
+
+            if ((transform_info[i].valid_chunks == 0 ||
+                  (transform_info[i].valid_chunks & chunks) != 0) &&
+               (transform_info[i].color_mask_required & ct) ==
+                  transform_info[i].color_mask_required &&
+               (transform_info[i].color_mask_absent & ct) == 0 &&
+               (transform_info[i].bit_depths & bd) != 0 &&
+               (transform_info[i].when & TRANSFORM_R) != 0)
+               active |= transform;
+
+            else if ((transform_info[i].when & TRANSFORM_R) != 0)
+               inactive |= transform;
+         }
       }
 
       /* Some transforms appear multiple times in the table; the 'active' status
@@ -1000,8 +1006,9 @@ compare_read(struct display *dp, int applied_transforms)
    int interlace_method, compression_method, filter_method;
    const char *e = NULL;
 
-   png_get_IHDR(dp->read_pp, dp->read_ip, &width, &height, &bit_depth,
-      &color_type, &interlace_method, &compression_method, &filter_method);
+   if (!png_get_IHDR(dp->read_pp, dp->read_ip, &width, &height, &bit_depth,
+      &color_type, &interlace_method, &compression_method, &filter_method))
+      display_log(dp, LIBPNG_BUG, "png_get_IHDR failed");
 
 #  define C(item) if (item != dp->item) \
       display_log(dp, APP_WARNING, "IHDR " #item "(%lu) changed to %lu",\
@@ -1081,8 +1088,9 @@ compare_read(struct display *dp, int applied_transforms)
                size_t x;
 
                /* Find the first error */
-               for (x=0; x<rowbytes-1; ++x) if (row[x] != orig[x])
-                  break;
+               for (x=0; x<rowbytes-1; ++x)
+                  if (row[x] != orig[x])
+                     break;
 
                display_log(dp, APP_FAIL,
                   "byte(%lu,%lu) changed 0x%.2x -> 0x%.2x",
@@ -1137,6 +1145,7 @@ compare_read(struct display *dp, int applied_transforms)
                display_log(dp, LIBPNG_ERROR, "invalid colour type %d",
                   color_type);
                /*NOTREACHED*/
+               memset(sig_bits, 0, sizeof(sig_bits));
                bpp = 0;
                break;
          }
@@ -1198,7 +1207,7 @@ compare_read(struct display *dp, int applied_transforms)
                sig_bits[0] = (png_byte)b;
                break;
 
-            case 4: /* Relicate twice */
+            case 4: /* Replicate twice */
                /* Value is 1, 2, 3 or 4 */
                b = 0xf & ((0xf << 4) >> sig_bits[0]);
                b |= b << 4;
@@ -1686,8 +1695,9 @@ main(int argc, char **argv)
 
                printf("%s: pngimage ", pass ? "PASS" : "FAIL");
 
-               for (j=1; j<option_end; ++j) if (j != ilog)
-                  printf("%s ", argv[j]);
+               for (j=1; j<option_end; ++j)
+                  if (j != ilog)
+                     printf("%s ", argv[j]);
 
                printf("%s\n", d.filename);
             }