Imported from libpng-1.0.1a.tar
diff --git a/pngrtran.c b/pngrtran.c
index 885af53..1b925a7 100644
--- a/pngrtran.c
+++ b/pngrtran.c
@@ -1,12 +1,12 @@
 
 /* pngrtran.c - transforms the data in a row for PNG readers
  *
- * libpng 1.0.1
+ * libpng 1.0.1a
  * For conditions of distribution and use, see copyright notice in png.h
  * Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.
  * Copyright (c) 1996, 1997 Andreas Dilger
  * Copyright (c) 1998, Glenn Randers-Pehrson
- * March 15, 1998
+ * April 21, 1998
  *
  * This file contains functions optionally called by an application 
  * in order to tell libpng how to handle data when reading a PNG.
@@ -670,8 +670,9 @@
            {
            /* invert the alpha channel (in tRNS) unless the pixels are 
               going to be expanded, in which case leave it for later */
-              int i;
-              for (i=0; i<(int)png_ptr->num_trans; i++)
+              int i,istop;
+              istop=(int)png_ptr->num_trans;
+              for (i=0; i<istop; i++)
                  png_ptr->trans[i] = 255 - png_ptr->trans[i];
            }
         }
@@ -872,7 +873,7 @@
    if (png_ptr->transformations & PNG_BACKGROUND &&
        color_type == PNG_COLOR_TYPE_PALETTE)
    {
-      int i;
+      int i,istop;
       png_color back;
       png_colorp palette;
 
@@ -881,7 +882,8 @@
       back.green = (png_byte)png_ptr->background.green;
       back.blue  = (png_byte)png_ptr->background.blue;
 
-      for (i = 0; i < (int)png_ptr->num_trans; i++)
+      istop = (int)png_ptr->num_trans;
+      for (i = 0; i < istop; i++)
       {
          if (png_ptr->trans[i] == 0)
          {
@@ -904,7 +906,7 @@
    if ((png_ptr->transformations & PNG_SHIFT) &&
       color_type == PNG_COLOR_TYPE_PALETTE)
    {
-      png_uint_16 i;
+      png_uint_16 i,istop;
       int sr, sg, sb;
 
       sr = 8 - png_ptr->sig_bit.red;
@@ -916,7 +918,8 @@
       sb = 8 - png_ptr->sig_bit.blue;
       if (sb < 0 || sb > 8)
          sb = 0;
-      for (i = 0; i < png_ptr->num_palette; i++)
+      istop = png_ptr->num_palette;
+      for (i = 0; i < istop; i++)
       {
          png_ptr->palette[i].red >>= sr;
          png_ptr->palette[i].green >>= sg;
@@ -1006,11 +1009,14 @@
 #endif
 
 #if defined(PNG_READ_FILLER_SUPPORTED)
-   if ((png_ptr->transformations & PNG_FILLER) &&
-       info_ptr->color_type & PNG_COLOR_TYPE_RGB &&
-       info_ptr->channels == 3)
+   if (png_ptr->transformations & PNG_FILLER)
    {
-      info_ptr->channels = 4;
+       if((info_ptr->color_type & PNG_COLOR_TYPE_RGB) &&
+          info_ptr->channels == 3)
+             info_ptr->channels = 4;
+       if((info_ptr->color_type & PNG_COLOR_TYPE_GRAY) &&
+          info_ptr->channels == 1)
+             info_ptr->channels = 2;
    }
 #endif
 
@@ -1200,16 +1206,17 @@
 #endif
    {
       png_uint_32 shift, i;
+      png_uint_32 row_width=row_info->width;
       png_bytep sp, dp;
 
       switch (row_info->bit_depth)
       {
          case 1:
          {
-            sp = row + (png_size_t)((row_info->width - 1) >> 3);
-            dp = row + (png_size_t)row_info->width - 1;
-            shift = 7 - (int)((row_info->width + 7) & 7);
-            for (i = 0; i < row_info->width; i++)
+            sp = row + (png_size_t)((row_width - 1) >> 3);
+            dp = row + (png_size_t)row_width - 1;
+            shift = 7 - (int)((row_width + 7) & 7);
+            for (i = 0; i < row_width; i++)
             {
                *dp = (png_byte)((*sp >> shift) & 0x1);
                if (shift == 7)
@@ -1227,10 +1234,10 @@
          case 2:
          {
 
-            sp = row + (png_size_t)((row_info->width - 1) >> 2);
-            dp = row + (png_size_t)row_info->width - 1;
-            shift = (int)((3 - ((row_info->width + 3) & 3)) << 1);
-            for (i = 0; i < row_info->width; i++)
+            sp = row + (png_size_t)((row_width - 1) >> 2);
+            dp = row + (png_size_t)row_width - 1;
+            shift = (int)((3 - ((row_width + 3) & 3)) << 1);
+            for (i = 0; i < row_width; i++)
             {
                *dp = (png_byte)((*sp >> shift) & 0x3);
                if (shift == 6)
@@ -1247,10 +1254,10 @@
          }
          case 4:
          {
-            sp = row + (png_size_t)((row_info->width - 1) >> 1);
-            dp = row + (png_size_t)row_info->width - 1;
-            shift = (int)((1 - ((row_info->width + 1) & 1)) << 2);
-            for (i = 0; i < row_info->width; i++)
+            sp = row + (png_size_t)((row_width - 1) >> 1);
+            dp = row + (png_size_t)row_width - 1;
+            shift = (int)((1 - ((row_width + 1) & 1)) << 2);
+            for (i = 0; i < row_width; i++)
             {
                *dp = (png_byte)((*sp >> shift) & 0xf);
                if (shift == 4)
@@ -1268,7 +1275,7 @@
       }
       row_info->bit_depth = 8;
       row_info->pixel_depth = (png_byte)(8 * row_info->channels);
-      row_info->rowbytes = row_info->width * row_info->channels;
+      row_info->rowbytes = row_width * row_info->channels;
    }
 }
 #endif
@@ -1292,6 +1299,7 @@
       int shift[4];
       int channels, c;
       png_uint_16 value;
+      png_uint_32 row_width = row_info->width;
 
       channels = 0;
       if (row_info->color_type & PNG_COLOR_MASK_COLOR)
@@ -1327,9 +1335,10 @@
          case 2:
          {
             png_bytep bp;
-            png_uint_32 i;
+            png_uint_32 i,istop;
 
-            for (bp = row, i = 0; i < row_info->rowbytes; i++, bp++)
+            istop = row_info->rowbytes;
+            for (bp = row, i = 0; i < istop; i++, bp++)
             {
                *bp >>= 1;
                *bp &= 0x55;
@@ -1340,11 +1349,12 @@
          {
             png_bytep bp;
             png_byte mask;
-            png_uint_32 i;
+            png_uint_32 i,istop;
 
             mask = (png_byte)(((int)0xf0 >> shift[0]) & (int)0xf0) |
                (png_byte)((int)0xf >> shift[0]);
-            for (bp = row, i = 0; i < row_info->rowbytes; i++, bp++)
+            istop = row_info->rowbytes;
+            for (bp = row, i = 0; i < istop; i++, bp++)
             {
                *bp >>= shift[0];
                *bp &= mask;
@@ -1355,10 +1365,12 @@
          {
             png_bytep bp;
             png_uint_32 i;
+            int cstop;
 
-            for (bp = row, i = 0; i < row_info->width; i++)
+            cstop=(int)row_info->channels;
+            for (bp = row, i = 0; i < row_width; i++)
             {
-               for (c = 0; c < (int)row_info->channels; c++, bp++)
+               for (c = 0; c < cstop; c++, bp++)
                {
                   *bp >>= shift[c];
                }
@@ -1369,10 +1381,12 @@
          {
             png_bytep bp;
             png_size_t i;
+            int cstop;
 
-            for (bp = row, i = 0; i < row_info->width; i++)
+            cstop=(int)row_info->channels;
+            for (bp = row, i = 0; i < row_width; i++)
             {
-               for (c = 0; c < (int)row_info->channels; c++, bp += 2)
+               for (c = 0; c < cstop; c++, bp += 2)
                {
                   value = (png_uint_16)((*bp << 8) + *(bp + 1));
                   value >>= shift[c];
@@ -1400,37 +1414,39 @@
 #endif
    {
       png_bytep sp, dp;
-      png_uint_32 i;
+      png_uint_32 i, istop;
 
       sp = row;
       dp = row;
-      for (i = 0; i < row_info->width * row_info->channels; i++, sp += 2, dp++)
+      istop = row_info->width * row_info->channels;
+      for (i = 0; i<istop; i++, sp += 2, dp++)
       {
 #if defined(PNG_READ_16_TO_8_ACCURATE_SCALE_SUPPORTED)
-         /* This does a more accurate scaling of the 16-bit color
-          * value, rather than a simple low-byte truncation.
-          *
-          * What the ideal calculation should be:
-         *dp = (((((png_uint_32)(*sp) << 8) |
-                   (png_uint_32)(*(sp + 1))) * 255 + 127) / (png_uint_32)65535L;
+      /* This does a more accurate scaling of the 16-bit color
+       * value, rather than a simple low-byte truncation.
+       *
+       * What the ideal calculation should be:
+       *   *dp = (((((png_uint_32)(*sp) << 8) |
+       *          (png_uint_32)(*(sp + 1))) * 255 + 127) / (png_uint_32)65535L;
+       *
+       * GRR: no, I think this is what it really should be:
+       *   *dp = (((((png_uint_32)(*sp) << 8) |
+       *           (png_uint_32)(*(sp + 1))) + 128L) / (png_uint_32)257L;
+       *
+       * GRR: here's the exact calculation with shifts:
+       *   temp = (((png_uint_32)(*sp) << 8) | (png_uint_32)(*(sp + 1))) + 128L;
+       *   *dp = (temp - (temp >> 8)) >> 8;
+       *
+       * Approximate calculation with shift/add instead of multiply/divide:
+       *   *dp = ((((png_uint_32)(*sp) << 8) |
+       *          (png_uint_32)((int)(*(sp + 1)) - *sp)) + 128) >> 8;
+       *
+       * What we actually do to avoid extra shifting and conversion:
+       */
 
-
-          * GRR: no, I think this is what it really should be:
-         *dp = (((((png_uint_32)(*sp) << 8) |
-                   (png_uint_32)(*(sp + 1))) + 128L) / (png_uint_32)257L;
-
-          * GRR: here's the exact calculation with shifts:
-         temp = (((png_uint_32)(*sp) << 8) | (png_uint_32)(*(sp + 1))) + 128L;
-         *dp = (temp - (temp >> 8)) >> 8;
-
-
-          * Approximate calculation with shift/add instead of multiply/divide:
-         *dp = ((((png_uint_32)(*sp) << 8) |
-                  (png_uint_32)((int)(*(sp + 1)) - *sp)) + 128) >> 8;
-
-          * What we actually do to avoid extra shifting and conversion: */
          *dp = *sp + ((((int)(*(sp + 1)) - *sp) > 128) ? 1 : 0);
 #else
+       /* Simply discard the low order byte */
          *dp = *sp;
 #endif
       }
@@ -1450,6 +1466,7 @@
    if (row != NULL && row_info != NULL)
 #endif
    {
+      png_uint_32 row_width = row_info->width;
       if (row_info->color_type == PNG_COLOR_TYPE_RGB_ALPHA)
       {
          /* This converts from RGBA to ARGB */
@@ -1458,9 +1475,8 @@
             png_bytep sp, dp;
             png_byte save;
             png_uint_32 i;
-
-            for (i = 0, sp = dp = row + row_info->rowbytes;
-               i < row_info->width; i++)
+            sp = dp = row + row_info->rowbytes;
+            for (i = 0; i < row_width; i++)
             {
                save = *(--sp);
                *(--dp) = *(--sp);
@@ -1475,9 +1491,8 @@
             png_bytep sp, dp;
             png_byte save[2];
             png_uint_32 i;
-
-            for (i = 0, sp = dp = row + row_info->rowbytes;
-               i < row_info->width; i++)
+            sp = dp = row + row_info->rowbytes;
+            for (i = 0; i < row_width; i++)
             {
                save[0] = *(--sp);
                save[1] = *(--sp);
@@ -1500,9 +1515,8 @@
             png_bytep sp, dp;
             png_byte save;
             png_uint_32 i;
-
-            for (i = 0, sp = dp = row + row_info->rowbytes;
-               i < row_info->width; i++)
+            sp = dp = row + row_info->rowbytes;
+            for (i = 0; i < row_width; i++)
             {
                save = *(--sp);
                *(--dp) = *(--sp);
@@ -1516,8 +1530,8 @@
             png_byte save[2];
             png_uint_32 i;
 
-            for (i = 0, sp = dp = row + row_info->rowbytes;
-               i < row_info->width; i++)
+            sp = dp = row + row_info->rowbytes;
+            for (i = 0; i < row_width; i++)
             {
                save[0] = *(--sp);
                save[1] = *(--sp);
@@ -1541,6 +1555,7 @@
    if (row != NULL && row_info != NULL)
 #endif
    {
+      png_uint_32 row_width = row_info->width;
       if (row_info->color_type == PNG_COLOR_TYPE_RGB_ALPHA)
       {
          /* This inverts the alpha channel in RGBA */
@@ -1549,8 +1564,8 @@
             png_bytep sp, dp;
             png_uint_32 i;
 
-            for (i = 0, sp = dp = row + row_info->rowbytes;
-               i < row_info->width; i++)
+            sp = dp = row + row_info->rowbytes;
+            for (i = 0; i < row_width; i++)
             {
                *(--dp) = 255 - *(--sp);
                *(--dp) = *(--sp);
@@ -1564,8 +1579,8 @@
             png_bytep sp, dp;
             png_uint_32 i;
 
-            for (i = 0, sp = dp = row + row_info->rowbytes;
-               i < row_info->width; i++)
+            sp = dp = row + row_info->rowbytes;
+            for (i = 0; i < row_width; i++)
             {
                *(--dp) = 255 - *(--sp);
                *(--dp) = 255 - *(--sp);
@@ -1586,8 +1601,8 @@
             png_bytep sp, dp;
             png_uint_32 i;
 
-            for (i = 0, sp = dp = row + row_info->rowbytes;
-               i < row_info->width; i++)
+            sp = dp = row + row_info->rowbytes;
+            for (i = 0; i < row_width; i++)
             {
                *(--dp) = 255 - *(--sp);
                *(--dp) = *(--sp);
@@ -1599,8 +1614,8 @@
             png_bytep sp, dp;
             png_uint_32 i;
 
-            for (i = 0, sp = dp = row + row_info->rowbytes;
-               i < row_info->width; i++)
+            sp = dp = row + row_info->rowbytes;
+            for (i = 0; i < row_width; i++)
             {
                *(--dp) = 255 - *(--sp);
                *(--dp) = 255 - *(--sp);
@@ -1621,50 +1636,166 @@
 {
    png_bytep sp, dp;
    png_uint_32 i;
+   png_uint_32 row_width = row_info->width;
+
+   png_byte hi_filler = (png_byte)((filler>>8) & 0xf);
+   png_byte low_filler = (png_byte)(filler & 0xf);
 
    png_debug(1, "in png_do_read_filler\n");
    if (
 #if defined(PNG_USELESS_TESTS_SUPPORTED)
        row != NULL  && row_info != NULL &&
 #endif
-       row_info->color_type == PNG_COLOR_TYPE_RGB && row_info->bit_depth == 8)
+       row_info->color_type == PNG_COLOR_TYPE_GRAY)
    {
-      /* This changes the data from RGB to RGBX */
-      if (flags & PNG_FLAG_FILLER_AFTER)
+      if(row_info->bit_depth == 8)
       {
-         for (i = 1, sp = row + (png_size_t)row_info->width * 3,
-            dp = row + (png_size_t)row_info->width * 4;
-            i < row_info->width;
-            i++)
+         /* This changes the data from G to GX */
+         if (flags & PNG_FLAG_FILLER_AFTER)
          {
-            *(--dp) = (png_byte)filler;
-            *(--dp) = *(--sp);
-            *(--dp) = *(--sp);
-            *(--dp) = *(--sp);
+            sp = row + (png_size_t)row_width;
+            dp =  sp + (png_size_t)row_width;
+            for (i = 1; i < row_width; i++)
+            {
+               *(--dp) = low_filler;
+               *(--dp) = *(--sp);
+            }
+            row_info->channels = 2;
+            row_info->pixel_depth = 16;
+            row_info->rowbytes = row_width * 2;
          }
-         *(--dp) = (png_byte)filler;
-         row_info->channels = 4;
-         row_info->pixel_depth = 32;
-         row_info->rowbytes = row_info->width * 4;
+      /* This changes the data from G to XG */
+         else
+         {
+            sp = row + (png_size_t)row_width;
+            dp = sp  + (png_size_t)row_width;
+            for (i = 0; i < row_width; i++)
+            {
+               *(--dp) = *(--sp);
+               *(--dp) = low_filler;
+            }
+            row_info->channels = 2;
+            row_info->pixel_depth = 16;
+            row_info->rowbytes = row_width * 2;
+         }
       }
+      else if(row_info->bit_depth == 16)
+      {
+         /* This changes the data from GG to GGXX */
+         if (flags & PNG_FLAG_FILLER_AFTER)
+         {
+            sp = row + (png_size_t)row_width;
+            dp = sp  + (png_size_t)row_width;
+            for (i = 1; i < row_width; i++)
+            {
+               *(--dp) = hi_filler;
+               *(--dp) = low_filler;
+               *(--dp) = *(--sp);
+               *(--dp) = *(--sp);
+            }
+            row_info->channels = 2;
+            row_info->pixel_depth = 32;
+            row_info->rowbytes = row_width * 2;
+         }
+         /* This changes the data from GG to XXGG */
+         else
+         {
+            sp = row + (png_size_t)row_width;
+            dp = sp  + (png_size_t)row_width;
+            for (i = 0; i < row_width; i++)
+            {
+               *(--dp) = *(--sp);
+               *(--dp) = *(--sp);
+               *(--dp) = hi_filler;
+               *(--dp) = low_filler;
+            }
+            row_info->channels = 2;
+            row_info->pixel_depth = 16;
+            row_info->rowbytes = row_width * 2;
+         }
+      }
+   } /* COLOR_TYPE == GRAY */
+   else if (row_info->color_type == PNG_COLOR_TYPE_RGB)
+   {
+      if(row_info->bit_depth == 8)
+      {
+         /* This changes the data from RGB to RGBX */
+         if (flags & PNG_FLAG_FILLER_AFTER)
+         {
+            sp = row + (png_size_t)row_width * 3;
+            dp = sp  + (png_size_t)row_width;
+            for (i = 1; i < row_width; i++)
+            {
+               *(--dp) = low_filler;
+               *(--dp) = *(--sp);
+               *(--dp) = *(--sp);
+               *(--dp) = *(--sp);
+            }
+            row_info->channels = 4;
+            row_info->pixel_depth = 32;
+            row_info->rowbytes = row_width * 4;
+         }
       /* This changes the data from RGB to XRGB */
-      else
-      {
-         for (i = 0, sp = row + (png_size_t)row_info->width * 3,
-            dp = row + (png_size_t)row_info->width * 4;
-            i < row_info->width;
-            i++)
+         else
          {
-            *(--dp) = *(--sp);
-            *(--dp) = *(--sp);
-            *(--dp) = *(--sp);
-            *(--dp) = (png_byte)filler;
+            sp = row + (png_size_t)row_width * 3;
+            dp = sp + (png_size_t)row_width;
+            for (i = 0; i < row_width; i++)
+            {
+               *(--dp) = *(--sp);
+               *(--dp) = *(--sp);
+               *(--dp) = *(--sp);
+               *(--dp) = low_filler;
+            }
+            row_info->channels = 4;
+            row_info->pixel_depth = 32;
+            row_info->rowbytes = row_width * 4;
          }
-         row_info->channels = 4;
-         row_info->pixel_depth = 32;
-         row_info->rowbytes = row_info->width * 4;
       }
-   }
+      else if(row_info->bit_depth == 16)
+      {
+         /* This changes the data from RRGGBB to RRGGBBXX */
+         if (flags & PNG_FLAG_FILLER_AFTER)
+         {
+            sp = row + (png_size_t)row_width * 3;
+            dp = sp  + (png_size_t)row_width;
+            for (i = 1; i < row_width; i++)
+            {
+               *(--dp) = hi_filler;
+               *(--dp) = low_filler;
+               *(--dp) = *(--sp);
+               *(--dp) = *(--sp);
+               *(--dp) = *(--sp);
+               *(--dp) = *(--sp);
+               *(--dp) = *(--sp);
+               *(--dp) = *(--sp);
+            }
+            row_info->channels = 4;
+            row_info->pixel_depth = 64;
+            row_info->rowbytes = row_width * 4;
+         }
+         /* This changes the data from RRGGBB to XXRRGGBB */
+         else
+         {
+            sp = row + (png_size_t)row_width * 3;
+            dp = sp  + (png_size_t)row_width;
+            for (i = 0; i < row_width; i++)
+            {
+               *(--dp) = *(--sp);
+               *(--dp) = *(--sp);
+               *(--dp) = *(--sp);
+               *(--dp) = *(--sp);
+               *(--dp) = *(--sp);
+               *(--dp) = *(--sp);
+               *(--dp) = hi_filler;
+               *(--dp) = low_filler;
+            }
+            row_info->channels = 4;
+            row_info->pixel_depth = 64;
+            row_info->rowbytes = row_width * 4;
+         }
+      }
+   } /* COLOR_TYPE == RGB */
 }
 #endif
 
@@ -1675,6 +1806,7 @@
 {
    png_bytep sp, dp;
    png_uint_32 i;
+   png_uint_32 row_width = row_info->width;
 
    png_debug(1, "in png_do_gray_to_rgb\n");
    if (row_info->bit_depth >= 8 &&
@@ -1687,10 +1819,9 @@
       {
          if (row_info->bit_depth == 8)
          {
-            for (i = 0, sp = row + (png_size_t)row_info->width - 1,
-               dp = row + (png_size_t)row_info->width * 3 - 1;
-               i < row_info->width;
-               i++)
+            sp = row + (png_size_t)row_width - 1;
+            dp = sp  + (png_size_t)row_width * 2;
+            for (i = 0; i < row_width; i++)
             {
                *(dp--) = *sp;
                *(dp--) = *sp;
@@ -1700,10 +1831,9 @@
          }
          else
          {
-            for (i = 0, sp = row + (png_size_t)row_info->width * 2 - 1,
-               dp = row + (png_size_t)row_info->width * 6 - 1;
-               i < row_info->width;
-               i++)
+            sp = row + (png_size_t)row_width * 2 - 1;
+            dp = sp  + (png_size_t)row_width * 4;
+            for (i = 0; i < row_width; i++)
             {
                *(dp--) = *sp;
                *(dp--) = *(sp - 1);
@@ -1720,10 +1850,9 @@
       {
          if (row_info->bit_depth == 8)
          {
-            for (i = 0, sp = row + (png_size_t)row_info->width * 2 - 1,
-               dp = row + (png_size_t)row_info->width * 4 - 1;
-               i < row_info->width;
-               i++)
+            sp = row + (png_size_t)row_width * 2 - 1;
+            dp = sp  + (png_size_t)row_width * 2;
+            for (i = 0; i < row_width; i++)
             {
                *(dp--) = *(sp--);
                *(dp--) = *sp;
@@ -1734,10 +1863,9 @@
          }
          else
          {
-            for (i = 0, sp = row + (png_size_t)row_info->width * 4 - 1,
-               dp = row + (png_size_t)row_info->width * 8 - 1;
-               i < row_info->width;
-               i++)
+            sp = row + (png_size_t)row_width * 4 - 1;
+            dp = sp  + (png_size_t)row_width * 4;
+            for (i = 0; i < row_width; i++)
             {
                *(dp--) = *(sp--);
                *(dp--) = *(sp--);
@@ -1756,7 +1884,7 @@
       row_info->color_type |= PNG_COLOR_MASK_COLOR;
       row_info->pixel_depth = (png_byte)(row_info->channels *
          row_info->bit_depth);
-      row_info->rowbytes = ((row_info->width *
+      row_info->rowbytes = ((row_width *
          row_info->pixel_depth + 7) >> 3);
    }
 }
@@ -2008,6 +2136,7 @@
 {
    png_bytep sp, dp;
    png_uint_32 i;
+   png_uint_32 row_width=row_info->width;
    int shift;
 
    png_debug(1, "in png_do_background\n");
@@ -2028,7 +2157,7 @@
                {
                   sp = row;
                   shift = 7;
-                  for (i = 0; i < row_info->width; i++)
+                  for (i = 0; i < row_width; i++)
                   {
                      if ((png_uint_16)((*sp >> shift) & 0x1)
                         == trans_values->gray)
@@ -2050,7 +2179,7 @@
                {
                   sp = row;
                   shift = 6;
-                  for (i = 0; i < row_info->width; i++)
+                  for (i = 0; i < row_width; i++)
                   {
                      if ((png_uint_16)((*sp >> shift) & 0x3)
                          == trans_values->gray)
@@ -2072,7 +2201,7 @@
                {
                   sp = row;
                   shift = 4;
-                  for (i = 0; i < row_info->width; i++)
+                  for (i = 0; i < row_width; i++)
                   {
                      if ((png_uint_16)((*sp >> shift) & 0xf)
                          == trans_values->gray)
@@ -2095,7 +2224,8 @@
 #if defined(PNG_READ_GAMMA_SUPPORTED)
                   if (gamma_table != NULL)
                   {
-                     for (i = 0, sp = row; i < row_info->width; i++, sp++)
+                     sp = row;
+                     for (i = 0; i < row_width; i++, sp++)
                      {
                         if (*sp == trans_values->gray)
                         {
@@ -2110,7 +2240,8 @@
                   else
 #endif
                   {
-                     for (i = 0, sp = row; i < row_info->width; i++, sp++)
+                     sp = row;
+                     for (i = 0; i < row_width; i++, sp++)
                      {
                         if (*sp == trans_values->gray)
                         {
@@ -2125,7 +2256,8 @@
 #if defined(PNG_READ_GAMMA_SUPPORTED)
                   if (gamma_16 != NULL)
                   {
-                     for (i = 0, sp = row; i < row_info->width; i++, sp += 2)
+                     sp = row;
+                     for (i = 0; i < row_width; i++, sp += 2)
                      {
                         png_uint_16 v;
 
@@ -2147,7 +2279,8 @@
                   else
 #endif
                   {
-                     for (i = 0, sp = row; i < row_info->width; i++, sp += 2)
+                     sp = row;
+                     for (i = 0; i < row_width; i++, sp += 2)
                      {
                         png_uint_16 v;
 
@@ -2171,7 +2304,8 @@
 #if defined(PNG_READ_GAMMA_SUPPORTED)
                if (gamma_table != NULL)
                {
-                  for (i = 0, sp = row; i < row_info->width; i++, sp += 3)
+                  sp = row;
+                  for (i = 0; i < row_width; i++, sp += 3)
                   {
                      if (*sp == trans_values->red &&
                         *(sp + 1) == trans_values->green &&
@@ -2192,7 +2326,8 @@
                else
 #endif
                {
-                  for (i = 0, sp = row; i < row_info->width; i++, sp += 3)
+                  sp = row;
+                  for (i = 0; i < row_width; i++, sp += 3)
                   {
                      if (*sp == trans_values->red &&
                         *(sp + 1) == trans_values->green &&
@@ -2210,7 +2345,8 @@
 #if defined(PNG_READ_GAMMA_SUPPORTED)
                if (gamma_16 != NULL)
                {
-                  for (i = 0, sp = row; i < row_info->width; i++, sp += 6)
+                  sp = row;
+                  for (i = 0; i < row_width; i++, sp += 6)
                   {
                      png_uint_16 r, g, b;
 
@@ -2246,7 +2382,8 @@
                else
 #endif
                {
-                  for (i = 0, sp = row; i < row_info->width; i++, sp += 6)
+                  sp = row;
+                  for (i = 0; i < row_width; i++, sp += 6)
                   {
                      png_uint_16 r, g, b;
 
@@ -2276,8 +2413,9 @@
                if (gamma_to_1 != NULL && gamma_from_1 != NULL &&
                    gamma_table != NULL)
                {
-                  for (i = 0, sp = row, dp = row;
-                     i < row_info->width; i++, sp += 2, dp++)
+                  sp = row;
+                  dp = row;
+                  for (i = 0; i < row_width; i++, sp += 2, dp++)
                   {
                      png_uint_16 a;
 
@@ -2304,8 +2442,9 @@
                else
 #endif
                {
-                  for (i = 0, sp = row, dp = row;
-                     i < row_info->width; i++, sp += 2, dp++)
+                  sp = row;
+                  dp = row;
+                  for (i = 0; i < row_width; i++, sp += 2, dp++)
                   {
                      png_byte a;
 
@@ -2331,8 +2470,9 @@
                if (gamma_16 != NULL && gamma_16_from_1 != NULL &&
                    gamma_16_to_1 != NULL)
                {
-                  for (i = 0, sp = row, dp = row;
-                     i < row_info->width; i++, sp += 4, dp += 2)
+                  sp = row;
+                  dp = row;
+                  for (i = 0; i < row_width; i++, sp += 4, dp += 2)
                   {
                      png_uint_16 a;
 
@@ -2366,8 +2506,9 @@
                else
 #endif
                {
-                  for (i = 0, sp = row, dp = row;
-                     i < row_info->width; i++, sp += 4, dp += 2)
+                  sp = row;
+                  dp = row;
+                  for (i = 0; i < row_width; i++, sp += 4, dp += 2)
                   {
                      png_uint_16 a;
 
@@ -2403,8 +2544,9 @@
                if (gamma_to_1 != NULL && gamma_from_1 != NULL &&
                    gamma_table != NULL)
                {
-                  for (i = 0, sp = row, dp = row;
-                     i < row_info->width; i++, sp += 4, dp += 3)
+                  sp = row;
+                  dp = row;
+                  for (i = 0; i < row_width; i++, sp += 4, dp += 3)
                   {
                      png_byte a;
 
@@ -2441,8 +2583,9 @@
                else
 #endif
                {
-                  for (i = 0, sp = row, dp = row;
-                     i < row_info->width; i++, sp += 4, dp += 3)
+                  sp = row;
+                  dp = row;
+                  for (i = 0; i < row_width; i++, sp += 4, dp += 3)
                   {
                      png_byte a;
 
@@ -2476,8 +2619,9 @@
                if (gamma_16 != NULL && gamma_16_from_1 != NULL &&
                    gamma_16_to_1 != NULL)
                {
-                  for (i = 0, sp = row, dp = row;
-                     i < row_info->width; i++, sp += 8, dp += 6)
+                  sp = row;
+                  dp = row;
+                  for (i = 0; i < row_width; i++, sp += 8, dp += 6)
                   {
                      png_uint_16 a;
 
@@ -2532,8 +2676,9 @@
                else
 #endif
                {
-                  for (i = 0, sp = row, dp = row;
-                     i < row_info->width; i++, sp += 8, dp += 6)
+                  sp = row;
+                  dp = row;
+                  for (i = 0; i < row_width; i++, sp += 8, dp += 6)
                   {
                      png_uint_16 a;
 
@@ -2583,7 +2728,7 @@
          row_info->channels--;
          row_info->pixel_depth = (png_byte)(row_info->channels *
             row_info->bit_depth);
-         row_info->rowbytes = ((row_info->width *
+         row_info->rowbytes = ((row_width *
             row_info->pixel_depth + 7) >> 3);
       }
    }
@@ -2604,6 +2749,7 @@
 {
    png_bytep sp;
    png_uint_32 i;
+   png_uint_32 row_width=row_info->width;
 
    png_debug(1, "in png_do_gamma\n");
    if (
@@ -2619,7 +2765,8 @@
          {
             if (row_info->bit_depth == 8)
             {
-               for (i = 0, sp = row; i < row_info->width; i++)
+               sp = row;
+               for (i = 0; i < row_width; i++)
                {
                   *sp = gamma_table[*sp];
                   sp++;
@@ -2631,7 +2778,8 @@
             }
             else /* if (row_info->bit_depth == 16) */
             {
-               for (i = 0, sp = row; i < row_info->width; i++)
+               sp = row;
+               for (i = 0; i < row_width; i++)
                {
                   png_uint_16 v;
 
@@ -2655,8 +2803,8 @@
          {
             if (row_info->bit_depth == 8)
             {
-               for (i = 0, sp = row;
-                  i < row_info->width; i++)
+               sp = row;
+               for (i = 0; i < row_width; i++)
                {
                   *sp = gamma_table[*sp];
                   sp++;
@@ -2669,8 +2817,8 @@
             }
             else /* if (row_info->bit_depth == 16) */
             {
-               for (i = 0, sp = row;
-                  i < row_info->width; i++)
+               sp = row;
+               for (i = 0; i < row_width; i++)
                {
                   png_uint_16 v;
 
@@ -2694,8 +2842,8 @@
          {
             if (row_info->bit_depth == 8)
             {
-               for (i = 0, sp = row;
-                  i < row_info->width; i++)
+               sp = row;
+               for (i = 0; i < row_width; i++)
                {
                   *sp = gamma_table[*sp];
                   sp += 2;
@@ -2703,8 +2851,8 @@
             }
             else /* if (row_info->bit_depth == 16) */
             {
-               for (i = 0, sp = row;
-                  i < row_info->width; i++)
+               sp = row;
+               for (i = 0; i < row_width; i++)
                {
                   png_uint_16 v;
 
@@ -2720,7 +2868,8 @@
          {
             if (row_info->bit_depth == 2)
             {
-               for (i = 0, sp = row; i < row_info->width; i += 4)
+               sp = row;
+               for (i = 0; i < row_width; i += 4)
                {
                   int a = *sp & 0xc0;
                   int b = *sp & 0x30;
@@ -2736,7 +2885,8 @@
             }
             if (row_info->bit_depth == 4)
             {
-               for (i = 0, sp = row; i < row_info->width; i += 2)
+               sp = row;
+               for (i = 0; i < row_width; i += 2)
                {
                   int msb = *sp & 0xf0;
                   int lsb = *sp & 0x0f;
@@ -2748,7 +2898,8 @@
             }
             else if (row_info->bit_depth == 8)
             {
-               for (i = 0, sp = row; i < row_info->width; i++)
+               sp = row;
+               for (i = 0; i < row_width; i++)
                {
                   *sp = gamma_table[*sp];
                   sp++;
@@ -2756,7 +2907,8 @@
             }
             else if (row_info->bit_depth == 16)
             {
-               for (i = 0, sp = row; i < row_info->width; i++)
+               sp = row;
+               for (i = 0; i < row_width; i++)
                {
                   png_uint_16 v;
 
@@ -2784,6 +2936,7 @@
    int shift, value;
    png_bytep sp, dp;
    png_uint_32 i;
+   png_uint_32 row_width=row_info->width;
 
    png_debug(1, "in png_do_expand_palette\n");
    if (
@@ -2798,10 +2951,10 @@
          {
             case 1:
             {
-               sp = row + (png_size_t)((row_info->width - 1) >> 3);
-               dp = row + (png_size_t)row_info->width - 1;
-               shift = 7 - (int)((row_info->width + 7) & 7);
-               for (i = 0; i < row_info->width; i++)
+               sp = row + (png_size_t)((row_width - 1) >> 3);
+               dp = row + (png_size_t)row_width - 1;
+               shift = 7 - (int)((row_width + 7) & 7);
+               for (i = 0; i < row_width; i++)
                {
                   if ((*sp >> shift) & 0x1)
                      *dp = 1;
@@ -2821,10 +2974,10 @@
             }
             case 2:
             {
-               sp = row + (png_size_t)((row_info->width - 1) >> 2);
-               dp = row + (png_size_t)row_info->width - 1;
-               shift = (int)((3 - ((row_info->width + 3) & 3)) << 1);
-               for (i = 0; i < row_info->width; i++)
+               sp = row + (png_size_t)((row_width - 1) >> 2);
+               dp = row + (png_size_t)row_width - 1;
+               shift = (int)((3 - ((row_width + 3) & 3)) << 1);
+               for (i = 0; i < row_width; i++)
                {
                   value = (*sp >> shift) & 0x3;
                   *dp = (png_byte)value;
@@ -2842,10 +2995,10 @@
             }
             case 4:
             {
-               sp = row + (png_size_t)((row_info->width - 1) >> 1);
-               dp = row + (png_size_t)row_info->width - 1;
-               shift = (int)((row_info->width & 1) << 2);
-               for (i = 0; i < row_info->width; i++)
+               sp = row + (png_size_t)((row_width - 1) >> 1);
+               dp = row + (png_size_t)row_width - 1;
+               shift = (int)((row_width & 1) << 2);
+               for (i = 0; i < row_width; i++)
                {
                   value = (*sp >> shift) & 0xf;
                   *dp = (png_byte)value;
@@ -2864,7 +3017,7 @@
          }
          row_info->bit_depth = 8;
          row_info->pixel_depth = 8;
-         row_info->rowbytes = row_info->width;
+         row_info->rowbytes = row_width;
       }
       switch (row_info->bit_depth)
       {
@@ -2872,10 +3025,10 @@
          {
             if (trans != NULL)
             {
-               sp = row + (png_size_t)row_info->width - 1;
-               dp = row + (png_size_t)(row_info->width << 2) - 1;
+               sp = row + (png_size_t)row_width - 1;
+               dp = row + (png_size_t)(row_width << 2) - 1;
 
-               for (i = 0; i < row_info->width; i++)
+               for (i = 0; i < row_width; i++)
                {
                   if ((int)(*sp) >= num_trans)
                      *dp-- = 0xff;
@@ -2888,16 +3041,16 @@
                }
                row_info->bit_depth = 8;
                row_info->pixel_depth = 32;
-               row_info->rowbytes = row_info->width * 4;
+               row_info->rowbytes = row_width * 4;
                row_info->color_type = 6;
                row_info->channels = 4;
             }
             else
             {
-               sp = row + (png_size_t)row_info->width - 1;
-               dp = row + (png_size_t)(row_info->width * 3) - 1;
+               sp = row + (png_size_t)row_width - 1;
+               dp = row + (png_size_t)(row_width * 3) - 1;
 
-               for (i = 0; i < row_info->width; i++)
+               for (i = 0; i < row_width; i++)
                {
                   *dp-- = palette[*sp].blue;
                   *dp-- = palette[*sp].green;
@@ -2906,7 +3059,7 @@
                }
                row_info->bit_depth = 8;
                row_info->pixel_depth = 24;
-               row_info->rowbytes = row_info->width * 3;
+               row_info->rowbytes = row_width * 3;
                row_info->color_type = 2;
                row_info->channels = 3;
             }
@@ -2926,6 +3079,7 @@
    int shift, value;
    png_bytep sp, dp;
    png_uint_32 i;
+   png_uint_32 row_width=row_info->width;
 
    png_debug(1, "in png_do_expand\n");
 #if defined(PNG_USELESS_TESTS_SUPPORTED)
@@ -2943,10 +3097,10 @@
                case 1:
                {
                   gray *= 0xff;
-                  sp = row + (png_size_t)((row_info->width - 1) >> 3);
-                  dp = row + (png_size_t)row_info->width - 1;
-                  shift = 7 - (int)((row_info->width + 7) & 7);
-                  for (i = 0; i < row_info->width; i++)
+                  sp = row + (png_size_t)((row_width - 1) >> 3);
+                  dp = row + (png_size_t)row_width - 1;
+                  shift = 7 - (int)((row_width + 7) & 7);
+                  for (i = 0; i < row_width; i++)
                   {
                      if ((*sp >> shift) & 0x1)
                         *dp = 0xff;
@@ -2967,10 +3121,10 @@
                case 2:
                {
                   gray *= 0x55;
-                  sp = row + (png_size_t)((row_info->width - 1) >> 2);
-                  dp = row + (png_size_t)row_info->width - 1;
-                  shift = (int)((3 - ((row_info->width + 3) & 3)) << 1);
-                  for (i = 0; i < row_info->width; i++)
+                  sp = row + (png_size_t)((row_width - 1) >> 2);
+                  dp = row + (png_size_t)row_width - 1;
+                  shift = (int)((3 - ((row_width + 3) & 3)) << 1);
+                  for (i = 0; i < row_width; i++)
                   {
                      value = (*sp >> shift) & 0x3;
                      *dp = (png_byte)(value | (value << 2) | (value << 4) |
@@ -2990,10 +3144,10 @@
                case 4:
                {
                   gray *= 0x11;
-                  sp = row + (png_size_t)((row_info->width - 1) >> 1);
-                  dp = row + (png_size_t)row_info->width - 1;
-                  shift = (int)((1 - ((row_info->width + 1) & 1)) << 2);
-                  for (i = 0; i < row_info->width; i++)
+                  sp = row + (png_size_t)((row_width - 1) >> 1);
+                  dp = row + (png_size_t)row_width - 1;
+                  shift = (int)((1 - ((row_width + 1) & 1)) << 2);
+                  for (i = 0; i < row_width; i++)
                   {
                      value = (*sp >> shift) & 0xf;
                      *dp = (png_byte)(value | (value << 4));
@@ -3012,16 +3166,16 @@
             }
             row_info->bit_depth = 8;
             row_info->pixel_depth = 8;
-            row_info->rowbytes = row_info->width;
+            row_info->rowbytes = row_width;
          }
 
          if (trans_value != NULL)
          {
             if (row_info->bit_depth == 8)
             {
-               sp = row + (png_size_t)row_info->width - 1;
-               dp = row + (png_size_t)(row_info->width << 1) - 1;
-               for (i = 0; i < row_info->width; i++)
+               sp = row + (png_size_t)row_width - 1;
+               dp = row + (png_size_t)(row_width << 1) - 1;
+               for (i = 0; i < row_width; i++)
                {
                   if (*sp == gray)
                      *dp-- = 0;
@@ -3034,7 +3188,7 @@
             {
                sp = row + row_info->rowbytes - 1;
                dp = row + (row_info->rowbytes << 1) - 1;
-               for (i = 0; i < row_info->width; i++)
+               for (i = 0; i < row_width; i++)
                {
                   if (((png_uint_16)*(sp) |
                      ((png_uint_16)*(sp - 1) << 8)) == gray)
@@ -3055,7 +3209,7 @@
             row_info->channels = 2;
             row_info->pixel_depth = (png_byte)(row_info->bit_depth << 1);
             row_info->rowbytes =
-               ((row_info->width * row_info->pixel_depth) >> 3);
+               ((row_width * row_info->pixel_depth) >> 3);
          }
       }
       else if (row_info->color_type == PNG_COLOR_TYPE_RGB && trans_value)
@@ -3063,8 +3217,8 @@
          if (row_info->bit_depth == 8)
          {
             sp = row + (png_size_t)row_info->rowbytes - 1;
-            dp = row + (png_size_t)(row_info->width << 2) - 1;
-            for (i = 0; i < row_info->width; i++)
+            dp = row + (png_size_t)(row_width << 2) - 1;
+            for (i = 0; i < row_width; i++)
             {
                if (*(sp - 2) == trans_value->red &&
                   *(sp - 1) == trans_value->green &&
@@ -3080,8 +3234,8 @@
          else if (row_info->bit_depth == 16)
          {
             sp = row + row_info->rowbytes - 1;
-            dp = row + (png_size_t)(row_info->width << 3) - 1;
-            for (i = 0; i < row_info->width; i++)
+            dp = row + (png_size_t)(row_width << 3) - 1;
+            for (i = 0; i < row_width; i++)
             {
                if ((((png_uint_16)*(sp - 4) |
                   ((png_uint_16)*(sp - 5) << 8)) == trans_value->red) &&
@@ -3110,7 +3264,7 @@
          row_info->channels = 4;
          row_info->pixel_depth = (png_byte)(row_info->bit_depth << 2);
          row_info->rowbytes =
-            ((row_info->width * row_info->pixel_depth) >> 3);
+            ((row_width * row_info->pixel_depth) >> 3);
       }
    }
 }
@@ -3123,6 +3277,7 @@
 {
    png_bytep sp, dp;
    png_uint_32 i;
+   png_uint_32 row_width=row_info->width;
 
    png_debug(1, "in png_do_dither\n");
 #if defined(PNG_USELESS_TESTS_SUPPORTED)
@@ -3135,7 +3290,7 @@
          int r, g, b, p;
          sp = row;
          dp = row;
-         for (i = 0; i < row_info->width; i++)
+         for (i = 0; i < row_width; i++)
          {
             r = *sp++;
             g = *sp++;
@@ -3163,7 +3318,7 @@
          row_info->channels = 1;
          row_info->pixel_depth = row_info->bit_depth;
          row_info->rowbytes =
-             ((row_info->width * row_info->pixel_depth + 7) >> 3);
+             ((row_width * row_info->pixel_depth + 7) >> 3);
       }
       else if (row_info->color_type == PNG_COLOR_TYPE_RGB_ALPHA &&
          palette_lookup != NULL && row_info->bit_depth == 8)
@@ -3171,7 +3326,7 @@
          int r, g, b, p;
          sp = row;
          dp = row;
-         for (i = 0; i < row_info->width; i++)
+         for (i = 0; i < row_width; i++)
          {
             r = *sp++;
             g = *sp++;
@@ -3193,13 +3348,13 @@
          row_info->channels = 1;
          row_info->pixel_depth = row_info->bit_depth;
          row_info->rowbytes =
-            ((row_info->width * row_info->pixel_depth + 7) >> 3);
+            ((row_width * row_info->pixel_depth + 7) >> 3);
       }
       else if (row_info->color_type == PNG_COLOR_TYPE_PALETTE &&
          dither_lookup && row_info->bit_depth == 8)
       {
          sp = row;
-         for (i = 0; i < row_info->width; i++, sp++)
+         for (i = 0; i < row_width; i++, sp++)
          {
             *sp = dither_lookup[*sp];
          }