]> granicus.if.org Git - imagemagick/commitdiff
Backed out today's updates to coders/png.c
authorglennrp <glennrp@git.imagemagick.org>
Sat, 23 Apr 2011 02:46:39 +0000 (02:46 +0000)
committerglennrp <glennrp@git.imagemagick.org>
Sat, 23 Apr 2011 02:46:39 +0000 (02:46 +0000)
ChangeLog
coders/png.c

index ad59debd0d69f2eed0b2746cd8834eb0be77886d..c84c3dd5798df50b0089da06f8856adc1bc3563c 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -14,7 +14,6 @@
     text chunks.
   * Added "-define PNG:preserve-colormap" option (reference
     http://www.imagemagick.org/discourse-server/viewtopic.php?f=3&t=18564).
-  * Simplified the LosslessReduceDepthOK() function.
 
 2011-04-18  6.6.9-6 Cristy  <quetzlzacatenango@image...>
   * Add support for writing RGB FITS images as separate planes.
index c60608fb6ecd6156fa7d295e33b7978134e2aac6..e4fcfac4b9ba1c052b5d52fa1d87e5fe5f19b3a8 100644 (file)
@@ -501,16 +501,14 @@ LosslessReduceDepthOK(Image *image)
      *
      * This is true if the high byte and the next highest byte of
      * each sample of the image, the colormap, and the background color
-     * are equal to each other.  We check this by seeing if the samples
-     * are unchanged when we scale them down to 8 and back up to Quantum.
+     * are equal to each other.
      *
      * We don't use the method GetImageDepth() because it doesn't check
-     * background and doesn't handle PseudoClass specially.
+     * background * and doesn't handle PseudoClass specially.  Also
+     * GetImageDepth() uses multiplication and division by 257 instead of
+     * shifting, so it might be slower.
      */
 
-#define ScaleQuantumToCharToQuantum(quantum) \
-        (ScaleCharToQuantum((unsigned char) ScaleQuantumToChar(quantum)))
-
     MagickBooleanType
       ok_to_reduce=MagickFalse;
 
@@ -520,13 +518,17 @@ LosslessReduceDepthOK(Image *image)
         const PixelPacket
           *p;
 
+        const int
+          pnghi= MAGICKCORE_QUANTUM_DEPTH - 8,
+          pnglo= MAGICKCORE_QUANTUM_DEPTH - 16;
+
         ok_to_reduce=
-          (image->background_color.red == 
-             ScaleQuantumToCharToQuantum(image->background_color.red) &&
-          image->background_color.green == 
-             ScaleQuantumToCharToQuantum(image->background_color.green) &&
-          image->background_color.blue == 
-             ScaleQuantumToCharToQuantum(image->background_color.blue)) ? 
+          (((((size_t) image->background_color.red >> pnghi) & 0xff)
+          == (((size_t) image->background_color.red >> pnglo) & 0xff)) &&
+           ((((size_t) image->background_color.green >> pnghi) & 0xff)
+          == (((size_t) image->background_color.green >> pnglo) & 0xff)) &&
+           ((((size_t) image->background_color.blue >> pnghi) & 0xff)
+          == (((size_t) image->background_color.blue >> pnglo) & 0xff))) ?
           MagickTrue : MagickFalse;
 
         if (ok_to_reduce != MagickFalse && image->storage_class == PseudoClass)
@@ -535,20 +537,19 @@ LosslessReduceDepthOK(Image *image)
 
             for (indx=0; indx < (ssize_t) image->colors; indx++)
               {
-                ok_to_reduce=(
-                   image->colormap[indx].red == 
-                   ScaleQuantumToCharToQuantum(image->colormap[indx].red) &&
-                   image->colormap[indx].green == 
-                   ScaleQuantumToCharToQuantum(image->colormap[indx].green) &&
-                   image->colormap[indx].blue == 
-                   ScaleQuantumToCharToQuantum(image->colormap[indx].blue) &&
-                   (image->matte == MagickFalse ||
-                   image->colormap[indx].opacity == 
-                   ScaleQuantumToCharToQuantum(image->colormap[indx].opacity)))
-                   ? MagickTrue : MagickFalse;
-
+                ok_to_reduce=
+                 (((((size_t) image->colormap[indx].red >> pnghi) & 0xff)
+                  == (((size_t) image->colormap[indx].red >> pnglo) & 0xff)) &&
+                  ((((size_t) image->colormap[indx].green >> pnghi) & 0xff)
+                  == (((size_t) image->colormap[indx].green >> pnglo) & 0xff))
+                  && ((((size_t) image->colormap[indx].blue >> pnghi) & 0xff)
+                  == (((size_t) image->colormap[indx].blue >> pnglo) & 0xff)) &&
+                  (image->matte == MagickFalse ||
+                  (((size_t) image->colormap[indx].opacity >> pnghi) & 0xff)
+                  == (((size_t) image->colormap[indx].opacity >> pnglo)
+                  & 0xff))) ?  MagickTrue : MagickFalse;
                 if (ok_to_reduce == MagickFalse)
-                   break;
+                  break;
               }
           }
 
@@ -573,17 +574,17 @@ LosslessReduceDepthOK(Image *image)
 
               for (x=(ssize_t) image->columns-1; x >= 0; x--)
               {
-                ok_to_reduce=
-                  (GetRedPixelComponent(p) ==
-                  ScaleQuantumToCharToQuantum(GetRedPixelComponent(p)) &&
-                  GetGreenPixelComponent(p) ==
-                  ScaleQuantumToCharToQuantum(GetGreenPixelComponent(p)) &&
-                  GetBluePixelComponent(p) ==
-                  ScaleQuantumToCharToQuantum(GetBluePixelComponent(p)) &&
-                  (image->matte == MagickFalse ||
-                  GetOpacityPixelComponent(p)
-                  == ScaleQuantumToCharToQuantum(GetOpacityPixelComponent(p))))
-                  MagickTrue : MagickFalse;
+                ok_to_reduce=(
+                  ((((size_t) p->red >> pnghi) & 0xff) ==
+                  (((size_t) p->red >> pnglo) & 0xff)) &&
+                  ((((size_t) p->green >> pnghi) & 0xff) ==
+                  (((size_t) p->green >> pnglo) & 0xff)) &&
+                  ((((size_t) p->blue >> pnghi) & 0xff) ==
+                  (((size_t) p->blue >> pnglo) & 0xff)) &&
+                  (((image->matte == MagickFalse ||
+                  (((size_t) p->opacity >> pnghi) & 0xff) ==
+                  (((size_t) p->opacity >> pnglo) & 0xff))))) ?
+                  MagickTrue : MagickFalse;
 
                 if (ok_to_reduce == MagickFalse)
                   break;
@@ -2450,6 +2451,10 @@ static Image *ReadOnePNGImage(MngInfo *mng_info,
           if (q == (PixelPacket *) NULL)
             break;
 
+#if  (0 && (MAGICKCORE_QUANTUM_DEPTH == 8) && !defined(MAGICKCORE_HDRI_SUPPORT))
+/* code deleted from version 6.6.6-8 */
+#else  /* (MAGICKCORE_QUANTUM_DEPTH != 8) */
+
           if ((int) ping_color_type == PNG_COLOR_TYPE_GRAY)
             (void) ImportQuantumPixels(image,(CacheView *) NULL,quantum_info,
               GrayQuantum,ping_pixels+row_offset,exception);
@@ -2469,7 +2474,7 @@ static Image *ReadOnePNGImage(MngInfo *mng_info,
           else /* ping_color_type == PNG_COLOR_TYPE_RGB */
             (void) ImportQuantumPixels(image,(CacheView *) NULL,quantum_info,
               RGBQuantum,ping_pixels+row_offset,exception);
-
+#endif
           if (found_transparent_pixel == MagickFalse)
             {
               /* Is there a transparent pixel in the row? */
@@ -2577,16 +2582,14 @@ static Image *ReadOnePNGImage(MngInfo *mng_info,
             for (x=(ssize_t) image->columns-7; x > 0; x-=8)
             {
               for (bit=7; bit >= 0; bit--)
-                *r++=ScaleCharToQuantum(
-                     (unsigned char) ((*p) & (0x01 << bit) ? 0xff : 0x00));
+                *r++=(Quantum) ((*p) & (0x01 << bit) ? 0x01 : 0x00);
               p++;
             }
 
             if ((image->columns % 8) != 0)
               {
                 for (bit=7; bit >= (ssize_t) (8-(image->columns % 8)); bit--)
-                  *r++=ScaleCharToQuantum(
-                  (unsigned char) ((*p) & (0x01 << bit) ? 0xff : 0x00));
+                  *r++=(Quantum) ((*p) & (0x01 << bit) ? 0x01 : 0x00);
               }
 
             break;
@@ -2596,17 +2599,16 @@ static Image *ReadOnePNGImage(MngInfo *mng_info,
           {
             for (x=(ssize_t) image->columns-3; x > 0; x-=4)
             {
-              *r++=ScaleCharToQuantum((unsigned char) (0x55*(*p >> 6) & 0x03));
-              *r++=ScaleCharToQuantum((unsigned char) (0x55*(*p >> 4) & 0x03));
-              *r++=ScaleCharToQuantum((unsigned char) (0x55*(*p >> 2) & 0x03));
-              *r++=ScaleCharToQuantum((unsigned char) (0x55*(*p++) & 0x03));
+              *r++=(*p >> 6) & 0x03;
+              *r++=(*p >> 4) & 0x03;
+              *r++=(*p >> 2) & 0x03;
+              *r++=(*p++) & 0x03;
             }
 
             if ((image->columns % 4) != 0)
               {
                 for (i=3; i >= (ssize_t) (4-(image->columns % 4)); i--)
-                  *r++=ScaleCharToQuantum((unsigned char)
-                       (0x55*((*p >> (i*2)) & 0x03)));
+                  *r++=(Quantum) ((*p >> (i*2)) & 0x03);
               }
 
             break;
@@ -2616,12 +2618,12 @@ static Image *ReadOnePNGImage(MngInfo *mng_info,
           {
             for (x=(ssize_t) image->columns-1; x > 0; x-=2)
             {
-              *r++=ScaleCharToQuantum((unsigned char) (0x11*(*p >> 4) & 0x0f));
-              *r++=ScaleCharToQuantum((unsigned char) (0x11*(*p++) & 0x0f));
+              *r++=(*p >> 4) & 0x0f;
+              *r++=(*p++) & 0x0f;
             }
 
             if ((image->columns % 2) != 0)
-              *r++=ScaleCharToQuantum((unsigned char) (0x11*(*p >> 4) & 0x0f));
+              *r++=(*p++ >> 4) & 0x0f;
 
             break;
           }
@@ -2631,7 +2633,7 @@ static Image *ReadOnePNGImage(MngInfo *mng_info,
             if (ping_color_type == 4)
               for (x=(ssize_t) image->columns-1; x >= 0; x--)
               {
-                *r++=ScaleCharToQuantum((unsigned char) *p++);
+                *r++=*p++;
                 /* In image.h, OpaqueOpacity is 0
                  * TransparentOpacity is QuantumRange
                  * In a PNG datastream, Opaque is QuantumRange
@@ -2645,7 +2647,7 @@ static Image *ReadOnePNGImage(MngInfo *mng_info,
 
             else
               for (x=(ssize_t) image->columns-1; x >= 0; x--)
-                *r++=ScaleCharToQuantum((unsigned char) *p++);
+                *r++=*p++;
 
             break;
           }
@@ -2654,34 +2656,70 @@ static Image *ReadOnePNGImage(MngInfo *mng_info,
           {
             for (x=(ssize_t) image->columns-1; x >= 0; x--)
             {
-#if MAGICKCORE_QUANTUM_DEPTH == 8
-              *r++=*p++;
-              p++; /* skip low byte */
+#if (MAGICKCORE_QUANTUM_DEPTH == 16)
+              size_t
+                quantum;
+
+              if (image->colors > 256)
+                *r=((*p++) << 8);
+
+              else
+                *r=0;
+
+              quantum=(*r);
+              quantum|=(*p++);
+              *r=(Quantum) quantum;
+              r++;
 
               if (ping_color_type == 4)
                 {
-                  q->opacity=(Quantum) (QuantumRange - *p++);
+                  quantum=((*p++) << 8);
+                  quantum|=(*p++);
+                  q->opacity=(Quantum) (QuantumRange-quantum);
                   if (q->opacity != OpaqueOpacity)
                     found_transparent_pixel = MagickTrue;
-                  p++;
                   q++;
                 }
+#else
+#if (MAGICKCORE_QUANTUM_DEPTH == 32)
+              size_t
+                quantum;
 
-#else /* MAGICKCORE_QUANTUM_DEPTH >= 16 */
-              *r++ = ScaleShortToQuantum(
-                   (unsigned short) (((*p) << 8) | (*(p+1))));
-              p+=2;
+              if (image->colors > 256)
+                *r=((*p++) << 8);
+
+              else
+                *r=0;
+
+              quantum=(*r);
+              quantum|=(*p++);
+              *r=quantum;
+              r++;
 
               if (ping_color_type == 4)
                 {
-                  q->opacity = QuantumRange - ScaleShortToQuantum(
-                      (unsigned short) (((*p) << 8) | (*(p+1))));
+                  q->opacity=(*p << 8) | *(p+1);
+                  q->opacity*=65537L;
+                  q->opacity=(Quantum) GetAlphaPixelComponent(q);
+                  if (q->opacity != OpaqueOpacity)
+                    found_transparent_pixel = MagickTrue;
                   p+=2;
+                  q++;
+                }
+
+#else /* MAGICKCORE_QUANTUM_DEPTH == 8 */
+              *r++=(*p++);
+              p++; /* strip low byte */
 
+              if (ping_color_type == 4)
+                {
+                  q->opacity=(Quantum) (QuantumRange-(*p++));
                   if (q->opacity != OpaqueOpacity)
                     found_transparent_pixel = MagickTrue;
+                  p++;
                   q++;
                 }
+#endif
 #endif
             }
 
@@ -2691,7 +2729,6 @@ static Image *ReadOnePNGImage(MngInfo *mng_info,
           default:
             break;
         }
-
         /*
           Transfer image scanline.
         */
@@ -3231,7 +3268,6 @@ static Image *ReadPNGImage(const ImageInfo *image_info,ExceptionInfo *exception)
       ThrowReaderException(CorruptImageError,"CorruptImage");
     }
 
-#if 0 /* This is probably redundant now */
   if (LocaleCompare(image_info->magick,"PNG8") == 0)
     {
       (void) SetImageType(image,PaletteType);
@@ -3241,7 +3277,6 @@ static Image *ReadPNGImage(const ImageInfo *image_info,ExceptionInfo *exception)
           /* To do: Reduce to binary transparency */
         }
     }
-#endif
 
   if (LocaleCompare(image_info->magick,"PNG24") == 0)
     {
@@ -5729,7 +5764,7 @@ static Image *ReadMNGImage(const ImageInfo *image_info,ExceptionInfo *exception)
                 magn_methx=mng_info->magn_methx;
                 magn_methy=mng_info->magn_methy;
 
-#if (MAGICKCORE_QUANTUM_DEPTH > 16)
+#if (MAGICKCORE_QUANTUM_DEPTH == 32)
 #define QM unsigned short
                 if (magn_methx != 1 || magn_methy != 1)
                   {
@@ -6033,7 +6068,7 @@ static Image *ReadMNGImage(const ImageInfo *image_info,ExceptionInfo *exception)
                   if (SyncAuthenticPixels(image,exception) == MagickFalse)
                     break;
                 }
-#if (MAGICKCORE_QUANTUM_DEPTH > 16)
+#if (MAGICKCORE_QUANTUM_DEPTH == 32)
               if (magn_methx != 1 || magn_methy != 1)
                 {
                 /*
@@ -6151,7 +6186,7 @@ static Image *ReadMNGImage(const ImageInfo *image_info,ExceptionInfo *exception)
          image->depth=16;
 #endif
 
-#if (MAGICKCORE_QUANTUM_DEPTH > 8)
+#if (MAGICKCORE_QUANTUM_DEPTH >= 16)
       if (LosslessReduceDepthOK(image) != MagickFalse)
          image->depth = 8;
 #endif
@@ -7098,7 +7133,7 @@ static MagickBooleanType WriteOnePNGImage(MngInfo *mng_info,
     }
 #endif
 
-#if 0 /* To do: respect the -depth option */
+#if 0 /* To do: honor -depth */
   if (image->depth < MAGICKCORE_QUANTUM_DEPTH)
     {
     }
@@ -7116,7 +7151,7 @@ static MagickBooleanType WriteOnePNGImage(MngInfo *mng_info,
       image->depth=16;
 #endif
 
-#if (MAGICKCORE_QUANTUM_DEPTH > 8)
+#if (MAGICKCORE_QUANTUM_DEPTH >= 16)
   if (image->depth == 16 && mng_info->write_png_depth != 16)
     if (mng_info->write_png8 || LosslessReduceDepthOK(image) != MagickFalse)
       image->depth = 8;
@@ -7157,6 +7192,9 @@ static MagickBooleanType WriteOnePNGImage(MngInfo *mng_info,
      * we need to check for bogus non-opaque values, at least.
      */
 
+#  define PNGK (MAGICKCORE_QUANTUM_DEPTH-8) /* Shift */
+#  define PNGM (ScaleCharToQuantum((unsigned char) 0x01)) /* Scale */
+
    ExceptionInfo
      *exception;
 
@@ -7380,8 +7418,10 @@ static MagickBooleanType WriteOnePNGImage(MngInfo *mng_info,
                   "      image has %d colors",image_colors);
        }
 
+#if 1 /* To do */
      if (ping_preserve_colormap != MagickFalse)
        break;
+#endif
 
      if (mng_info->write_png_colortype != 7) /* We won't need this info */
        {
@@ -7503,8 +7543,7 @@ static MagickBooleanType WriteOnePNGImage(MngInfo *mng_info,
 
             if (AcquireImageColormap(image,image_colors) ==
                 MagickFalse)
-               ThrowWriterException(ResourceLimitError,
-                   "MemoryAllocationFailed");
+               ThrowWriterException(ResourceLimitError, "MemoryAllocationFailed");
 
             for (i=0; i< (ssize_t) image_colors; i++)
                image->colormap[i] = colormap[i];
@@ -7681,17 +7720,20 @@ static MagickBooleanType WriteOnePNGImage(MngInfo *mng_info,
         tried_444 = MagickTrue;
 
         image->background_color.red=
-            ScaleCharToQuantum(
-            (ScaleQuantumToChar(image->background_color.red) & 0xf0) |
-            (ScaleQuantumToChar(image->background_color.red) & 0xf0) >> 4);
+            ((((((size_t)
+            image->background_color.red) >> PNGK) & 0xf0)     )  |
+            (((((size_t)
+            image->background_color.red) >> PNGK) & 0xf0) >> 4)) * PNGM;
         image->background_color.green=
-            ScaleCharToQuantum(
-            (ScaleQuantumToChar(image->background_color.green) & 0xf0) |
-            (ScaleQuantumToChar(image->background_color.green) & 0xf0) >> 4);
+            ((((((size_t)
+            image->background_color.green) >> PNGK) & 0xf0)     )  |
+            (((((size_t)
+            image->background_color.green) >> PNGK) & 0xf0) >> 4)) * PNGM;
         image->background_color.blue=
-            ScaleCharToQuantum(
-            (ScaleQuantumToChar(image->background_color.blue) & 0xf0) |
-            (ScaleQuantumToChar(image->background_color.blue) & 0xf0) >> 4);
+            ((((((size_t)
+            image->background_color.blue) >> PNGK) & 0xf0)     )  |
+            (((((size_t)
+            image->background_color.blue) >> PNGK) & 0xf0) >> 4)) * PNGM;
 
         if (logging != MagickFalse)
           (void) LogMagickEvent(CoderEvent,GetMagickModule(),
@@ -7717,15 +7759,15 @@ static MagickBooleanType WriteOnePNGImage(MngInfo *mng_info,
                 }
               else
                 {
-                  r->red=ScaleCharToQuantum(
-                       (ScaleQuantumToChar(r->red) & 0xf0) |
-                       (ScaleQuantumToChar(r->red) & 0xf0) >> 4);
-                  r->green=ScaleCharToQuantum(
-                       (ScaleQuantumToChar(r->green) & 0xf0) |
-                       (ScaleQuantumToChar(r->green) & 0xf0) >> 4);
-                  r->blue=ScaleCharToQuantum(
-                       (ScaleQuantumToChar(r->blue) & 0xf0) |
-                       (ScaleQuantumToChar(r->blue) & 0xf0) >> 4);
+                  r->red=
+                       ((((((size_t) r->red) >> PNGK) & 0xf0)    )  |
+                       (((((size_t) r->red) >> PNGK) & 0xf0) >> 4)) * PNGM;
+                  r->green=
+                       ((((((size_t) r->green) >> PNGK) & 0xf0)    )  |
+                       (((((size_t) r->green) >> PNGK) & 0xf0) >> 4)) * PNGM;
+                  r->blue=
+                       ((((((size_t) r->blue) >> PNGK) & 0xf0)    )  |
+                       (((((size_t) r->blue) >> PNGK) & 0xf0) >> 4)) * PNGM;
                 }
               r++;
             }
@@ -7743,15 +7785,21 @@ static MagickBooleanType WriteOnePNGImage(MngInfo *mng_info,
               "    Quantizing the colormap to 4-4-4");
           for (i=0; i<image_colors; i++)
           {
-            image->colormap[i].red=ScaleCharToQuantum(
-                 (ScaleQuantumToChar(image->colormap[i].red) & 0xf0) |
-                 (ScaleQuantumToChar(image->colormap[i].red) & 0xf0) >> 4);
-            image->colormap[i].green=ScaleCharToQuantum(
-                 (ScaleQuantumToChar(image->colormap[i].green) & 0xf0) |
-                 (ScaleQuantumToChar(image->colormap[i].green) & 0xf0) >> 4);
-            image->colormap[i].blue=ScaleCharToQuantum(
-                 (ScaleQuantumToChar(image->colormap[i].blue) & 0xf0) |
-                 (ScaleQuantumToChar(image->colormap[i].blue) & 0xf0 >> 4));
+              image->colormap[i].red=
+                  ((((((size_t)
+                  image->colormap[i].red) >> PNGK) & 0xf0)     )  |
+                  (((((size_t)
+                  image->colormap[i].red) >> PNGK) & 0xf0) >> 4)) * PNGM;
+              image->colormap[i].green=
+                  ((((((size_t)
+                  image->colormap[i].green) >> PNGK) & 0xf0)     )  |
+                  (((((size_t)
+                  image->colormap[i].green) >> PNGK) & 0xf0) >> 4)) * PNGM;
+              image->colormap[i].blue=
+                  ((((((size_t)
+                  image->colormap[i].blue) >> PNGK) & 0xf0)     )  |
+                  (((((size_t)
+                  image->colormap[i].blue) >> PNGK) & 0xf0) >> 4)) * PNGM;
           }
         }
         continue;
@@ -7766,20 +7814,26 @@ static MagickBooleanType WriteOnePNGImage(MngInfo *mng_info,
         tried_333 = MagickTrue;
 
         image->background_color.red=
-            ScaleCharToQuantum(
-            (ScaleQuantumToChar(image->background_color.red) & 0xe0) |
-            (ScaleQuantumToChar(image->background_color.red) & 0xe0) >> 3 |
-            (ScaleQuantumToChar(image->background_color.red) & 0xc0) >> 6);
+            ((((((size_t)
+            image->background_color.red) >> PNGK) & 0xe0)     )  |
+            (((((size_t)
+            image->background_color.red) >> PNGK) & 0xe0) >> 3)  |
+            (((((size_t)
+            image->background_color.red) >> PNGK) & 0xc0) >> 6)) * PNGM;
         image->background_color.green=
-            ScaleCharToQuantum(
-            (ScaleQuantumToChar(image->background_color.green) & 0xe0) |
-            (ScaleQuantumToChar(image->background_color.green) & 0xe0) >> 3 |
-            (ScaleQuantumToChar(image->background_color.green) & 0xc0) >> 6);
+            ((((((size_t)
+            image->background_color.green) >> PNGK) & 0xe0)     )  |
+            (((((size_t)
+            image->background_color.green) >> PNGK) & 0xe0) >> 3)  |
+            (((((size_t)
+            image->background_color.green) >> PNGK) & 0xc0) >> 6)) * PNGM;
         image->background_color.blue=
-            ScaleCharToQuantum(
-            (ScaleQuantumToChar(image->background_color.blue) & 0xe0) |
-            (ScaleQuantumToChar(image->background_color.blue) & 0xe0) >> 3 |
-            (ScaleQuantumToChar(image->background_color.blue) & 0xc0) >> 6);
+            ((((((size_t)
+            image->background_color.blue) >> PNGK) & 0xe0)     )  |
+            (((((size_t)
+            image->background_color.blue) >> PNGK) & 0xe0) >> 3)  |
+            (((((size_t)
+            image->background_color.blue) >> PNGK) & 0xc0) >> 6)) * PNGM;
 
         if (logging != MagickFalse)
           (void) LogMagickEvent(CoderEvent,GetMagickModule(),
@@ -7805,18 +7859,18 @@ static MagickBooleanType WriteOnePNGImage(MngInfo *mng_info,
                 }
               else
                 {
-                  r->red=ScaleCharToQuantum(
-                       (ScaleQuantumToChar(r->red) & 0xe0) |
-                       (ScaleQuantumToChar(r->red) & 0xe0) >> 3 |
-                       (ScaleQuantumToChar(r->red) & 0xc0) >> 6);
-                  r->green=ScaleCharToQuantum(
-                       (ScaleQuantumToChar(r->green) & 0xe0) |
-                       (ScaleQuantumToChar(r->green) & 0xe0) >> 3 |
-                       (ScaleQuantumToChar(r->green) & 0xc0) >> 6);
-                  r->blue=ScaleCharToQuantum(
-                       (ScaleQuantumToChar(r->blue) & 0xe0) |
-                       (ScaleQuantumToChar(r->blue) & 0xe0) >> 3 |
-                       (ScaleQuantumToChar(r->blue) & 0xc0) >> 6);
+                  r->red=
+                       ((((((size_t) r->red) >> PNGK) & 0xe0)    )  |
+                       (((((size_t) r->red) >> PNGK) & 0xe0) >> 3)  |
+                       (((((size_t) r->red) >> PNGK) & 0xc0) >> 6)) * PNGM;
+                  r->green=
+                       ((((((size_t) r->green) >> PNGK) & 0xe0)    )  |
+                       (((((size_t) r->green) >> PNGK) & 0xe0) >> 3)  |
+                       (((((size_t) r->green) >> PNGK) & 0xc0) >> 6)) * PNGM;
+                  r->blue=
+                       ((((((size_t) r->blue) >> PNGK) & 0xe0)    )  |
+                       (((((size_t) r->blue) >> PNGK) & 0xe0) >> 3)  |
+                       (((((size_t) r->blue) >> PNGK) & 0xc0) >> 6)) * PNGM;
                 }
               r++;
             }
@@ -7834,18 +7888,27 @@ static MagickBooleanType WriteOnePNGImage(MngInfo *mng_info,
               "    Quantizing the colormap to 3-3-3-1");
           for (i=0; i<image_colors; i++)
           {
-              image->colormap[i].red=ScaleCharToQuantum(
-                   (ScaleQuantumToChar(image->colormap[i].red) & 0xe0) |
-                   (ScaleQuantumToChar(image->colormap[i].red) & 0xe0) >> 3 |
-                   (ScaleQuantumToChar(image->colormap[i].red) & 0xc0) >> 6);
-              image->colormap[i].green=ScaleCharToQuantum(
-                   (ScaleQuantumToChar(image->colormap[i].green) & 0xe0) |
-                   (ScaleQuantumToChar(image->colormap[i].green) & 0xe0) >> 3 |
-                   (ScaleQuantumToChar(image->colormap[i].green) & 0xc0) >> 6);
-              image->colormap[i].blue=ScaleCharToQuantum(
-                   (ScaleQuantumToChar(image->colormap[i].blue) & 0xe0) |
-                   (ScaleQuantumToChar(image->colormap[i].blue) & 0xe0) >> 3 |
-                   (ScaleQuantumToChar(image->colormap[i].blue) & 0xc0) >> 6);
+              image->colormap[i].red=
+                  ((((((size_t)
+                  image->colormap[i].red) >> PNGK) & 0xe0)     )  |
+                  (((((size_t)
+                  image->colormap[i].red) >> PNGK) & 0xe0) >> 3)  |
+                  (((((size_t)
+                  image->colormap[i].red) >> PNGK) & 0xc0) >> 6)) * PNGM;
+              image->colormap[i].green=
+                  ((((((size_t)
+                  image->colormap[i].green) >> PNGK) & 0xe0)     )  |
+                  (((((size_t)
+                  image->colormap[i].green) >> PNGK) & 0xe0) >> 3)  |
+                  (((((size_t)
+                  image->colormap[i].green) >> PNGK) & 0xc0) >> 6)) * PNGM;
+              image->colormap[i].blue=
+                  ((((((size_t)
+                  image->colormap[i].blue) >> PNGK) & 0xe0)     )  |
+                  (((((size_t)
+                  image->colormap[i].blue) >> PNGK) & 0xe0) >> 3)  |
+                  (((((size_t)
+                  image->colormap[i].blue) >> PNGK) & 0xc0) >> 6)) * PNGM;
           }
         }
         continue;
@@ -7857,15 +7920,29 @@ static MagickBooleanType WriteOnePNGImage(MngInfo *mng_info,
            (void) LogMagickEvent(CoderEvent,GetMagickModule(),
                "    Quantizing the background color to 3-3-2");
 
-        /* Red and green were already done so we only quantize the blue
-         * channel
-         */
-
-        image->background_color.blue=ScaleCharToQuantum(
-            (ScaleQuantumToChar(image->background_color.blue) & 0xc0) |
-            (ScaleQuantumToChar(image->background_color.blue) & 0xc0) >> 2 |
-            (ScaleQuantumToChar(image->background_color.blue) & 0xc0) >> 4 |
-            (ScaleQuantumToChar(image->background_color.blue) & 0xc0) >> 6);
+        image->background_color.red=
+            ((((((size_t)
+            image->background_color.red) >> PNGK) & 0xe0)     )  |
+            (((((size_t)
+            image->background_color.red) >> PNGK) & 0xe0) >> 3)  |
+            (((((size_t)
+            image->background_color.red) >> PNGK) & 0xc0) >> 6)) * PNGM;
+        image->background_color.green=
+            ((((((size_t)
+            image->background_color.green) >> PNGK) & 0xe0)     )  |
+            (((((size_t)
+            image->background_color.green) >> PNGK) & 0xe0) >> 3)  |
+            (((((size_t)
+            image->background_color.green) >> PNGK) & 0xc0) >> 6)) * PNGM;
+        image->background_color.blue=
+            ((((((size_t)
+            image->background_color.blue) >> PNGK) & 0xc0)     )  |
+            (((((size_t)
+            image->background_color.blue) >> PNGK) & 0xc0) >> 2)  |
+            (((((size_t)
+            image->background_color.blue) >> PNGK) & 0xc0) >> 4)  |
+            (((((size_t)
+            image->background_color.blue) >> PNGK) & 0xc0) >> 6)) * PNGM;
 
         if (logging != MagickFalse)
           (void) LogMagickEvent(CoderEvent,GetMagickModule(),
@@ -7891,11 +7968,19 @@ static MagickBooleanType WriteOnePNGImage(MngInfo *mng_info,
                 }
               else
                 {
-                  r->blue=ScaleCharToQuantum(
-                      (ScaleQuantumToChar(r->blue) & 0xc0) |
-                      (ScaleQuantumToChar(r->blue) & 0xc0) >> 2 |
-                      (ScaleQuantumToChar(r->blue) & 0xc0) >> 4 |
-                      (ScaleQuantumToChar(r->blue) & 0xc0) >> 6);
+                  r->red=
+                       ((((((size_t) r->red) >> PNGK) & 0xe0)    )  |
+                       (((((size_t) r->red) >> PNGK) & 0xe0) >> 3)  |
+                       (((((size_t) r->red) >> PNGK) & 0xc0) >> 6)) * PNGM;
+                  r->green=
+                       ((((((size_t) r->green) >> PNGK) & 0xe0)    )  |
+                       (((((size_t) r->green) >> PNGK) & 0xe0) >> 3)  |
+                       (((((size_t) r->green) >> PNGK) & 0xc0) >> 6)) * PNGM;
+                  r->blue=
+                       ((((((size_t) r->blue) >> PNGK) & 0xc0)    )  |
+                       (((((size_t) r->blue) >> PNGK) & 0xc0) >> 2)  |
+                       (((((size_t) r->blue) >> PNGK) & 0xc0) >> 4)  |
+                       (((((size_t) r->blue) >> PNGK) & 0xc0) >> 6)) * PNGM;
                 }
               r++;
             }
@@ -7913,11 +7998,29 @@ static MagickBooleanType WriteOnePNGImage(MngInfo *mng_info,
               "    Quantizing the colormap to 3-3-2-1");
           for (i=0; i<image_colors; i++)
           {
-              image->colormap[i].blue=ScaleCharToQuantum(
-                  (ScaleQuantumToChar(image->colormap[i].blue) & 0xc0) |
-                  (ScaleQuantumToChar(image->colormap[i].blue) & 0xc0) >> 2 |
-                  (ScaleQuantumToChar(image->colormap[i].blue) & 0xc0) >> 4 |
-                  (ScaleQuantumToChar(image->colormap[i].blue) & 0xc0) >> 6);
+              image->colormap[i].red=
+                  ((((((size_t)
+                  image->colormap[i].red) >> PNGK) & 0xe0)     )  |
+                  (((((size_t)
+                  image->colormap[i].red) >> PNGK) & 0xe0) >> 3)  |
+                  (((((size_t)
+                  image->colormap[i].red) >> PNGK) & 0xc0) >> 6)) * PNGM;
+              image->colormap[i].green=
+                  ((((((size_t)
+                  image->colormap[i].green) >> PNGK) & 0xe0)     )  |
+                  (((((size_t)
+                  image->colormap[i].green) >> PNGK) & 0xe0) >> 3)  |
+                  (((((size_t)
+                  image->colormap[i].green) >> PNGK) & 0xc0) >> 6)) * PNGM;
+              image->colormap[i].blue=
+                  ((((((size_t)
+                  image->colormap[i].blue) >> PNGK) & 0xc0)     )  |
+                  (((((size_t)
+                  image->colormap[i].blue) >> PNGK) & 0xc0) >> 2)  |
+                  (((((size_t)
+                  image->colormap[i].blue) >> PNGK) & 0xc0) >> 4)  |
+                  (((((size_t)
+                  image->colormap[i].blue) >> PNGK) & 0xc0) >> 6)) * PNGM;
           }
       }
       continue;