]> granicus.if.org Git - imagemagick/blobdiff - coders/jpeg.c
(no commit message)
[imagemagick] / coders / jpeg.c
index ca358cccac3e75341f2daf922627889bb8b3620f..136169f437aa7975627256d26b15472a187e8c90 100644 (file)
@@ -17,7 +17,7 @@
 %                                 July 1992                                   %
 %                                                                             %
 %                                                                             %
-%  Copyright 1999-2010 ImageMagick Studio LLC, a non-profit organization      %
+%  Copyright 1999-2011 ImageMagick Studio LLC, a non-profit organization      %
 %  dedicated to making software imaging solutions freely available.           %
 %                                                                             %
 %  You may not use this file except in compliance with the License.  You may  %
@@ -81,6 +81,7 @@
 #if defined(__MINGW32__)
 # define XMD_H 1  /* Avoid conflicting typedef for INT32 */
 typedef unsigned char boolean;
+#define HAVE_BOOLEAN
 #endif
 #undef HAVE_STDLIB_H
 #include "jpeglib.h"
@@ -242,8 +243,8 @@ static boolean FillInputBuffer(j_decompress_ptr cinfo)
     *source;
 
   source=(SourceManager *) cinfo->src;
-  source->manager.bytes_in_buffer=(size_t)
-    ReadBlob(source->image,MaxBufferExtent,source->buffer);
+  source->manager.bytes_in_buffer=(size_t) ReadBlob(source->image,
+    MaxBufferExtent,source->buffer);
   if (source->manager.bytes_in_buffer == 0)
     {
       if (source->start_of_blob != 0)
@@ -432,7 +433,7 @@ static boolean ReadICCProfile(j_decompress_ptr jpeg_info)
     }
   if (image->debug != MagickFalse)
     (void) LogMagickEvent(CoderEvent,GetMagickModule(),
-      "Profile: ICC, %lu bytes",(unsigned long) length);
+      "Profile: ICC, %.20g bytes",(double) length);
   return(MagickTrue);
 }
 
@@ -510,7 +511,7 @@ static boolean ReadIPTCProfile(j_decompress_ptr jpeg_info)
     ThrowBinaryException(ResourceLimitError,"MemoryAllocationFailed",
       image->filename);
   p=GetStringInfoDatum(profile);
-  for (i=(ssize_t) GetStringInfoLength(profile)-1; i >= 0; i--)
+  for (i=0;  i < (ssize_t) GetStringInfoLength(profile); i++)
     *p++=(unsigned char) GetCharacter(jpeg_info);
   iptc_profile=(StringInfo *) GetImageProfile(image,"8bim");
   if (iptc_profile != (StringInfo *) NULL)
@@ -528,7 +529,7 @@ static boolean ReadIPTCProfile(j_decompress_ptr jpeg_info)
     }
   if (image->debug != MagickFalse)
     (void) LogMagickEvent(CoderEvent,GetMagickModule(),
-      "Profile: iptc, %lu bytes",(unsigned long) length);
+      "Profile: iptc, %.20g bytes",(double) length);
   return(MagickTrue);
 }
 
@@ -612,7 +613,7 @@ static boolean ReadProfile(j_decompress_ptr jpeg_info)
       image->filename);
   if (image->debug != MagickFalse)
     (void) LogMagickEvent(CoderEvent,GetMagickModule(),
-      "Profile: %s, %lu bytes",name,(unsigned long) length);
+      "Profile: %s, %.20g bytes",name,(double) length);
   return(MagickTrue);
 }
 
@@ -624,13 +625,13 @@ static void SkipInputData(j_decompress_ptr cinfo,long number_bytes)
   if (number_bytes <= 0)
     return;
   source=(SourceManager *) cinfo->src;
-  while (number_bytes > (ssize_t) source->manager.bytes_in_buffer)
+  while (number_bytes > (long) source->manager.bytes_in_buffer)
   {
     number_bytes-=(long) source->manager.bytes_in_buffer;
     (void) FillInputBuffer(cinfo);
   }
-  source->manager.next_input_byte+=(size_t) number_bytes;
-  source->manager.bytes_in_buffer-=(size_t) number_bytes;
+  source->manager.next_input_byte+=number_bytes;
+  source->manager.bytes_in_buffer-=number_bytes;
 }
 
 static void TerminateSource(j_decompress_ptr cinfo)
@@ -737,7 +738,7 @@ static void JPEGSetImageQuality(struct jpeg_decompress_struct *jpeg_info,
              image->quality=(size_t) i+1;
            if (image->debug != MagickFalse)
              (void) LogMagickEvent(CoderEvent,GetMagickModule(),
-               "Quality: %ld (%s)",(long) i+1,(qvalue <= hash[i]) &&
+               "Quality: %.20g (%s)",(double) i+1,(qvalue <= hash[i]) &&
                (sum <= sums[i]) ? "exact" : "approximate");
            break;
          }
@@ -786,7 +787,7 @@ static void JPEGSetImageQuality(struct jpeg_decompress_struct *jpeg_info,
                image->quality=(size_t) i+1;
              if (image->debug != MagickFalse)
                (void) LogMagickEvent(CoderEvent,GetMagickModule(),
-                 "Quality: %ld (%s)",(long) i+1,(qvalue <= hash[i]) &&
+                 "Quality: %.20g (%s)",(double) i+1,(qvalue <= hash[i]) &&
                  (sum <= sums[i]) ? "exact" : "approximate");
              break;
            }
@@ -875,9 +876,6 @@ static Image *ReadJPEGImage(const ImageInfo *image_info,
   IndexPacket
     index;
 
-  ssize_t
-    y;
-
   JSAMPLE
     *jpeg_pixels;
 
@@ -907,6 +905,9 @@ static Image *ReadJPEGImage(const ImageInfo *image_info,
     precision,
     units;
 
+  ssize_t
+    y;
+
   /*
     Open image file.
   */
@@ -918,6 +919,7 @@ static Image *ReadJPEGImage(const ImageInfo *image_info,
   assert(exception != (ExceptionInfo *) NULL);
   assert(exception->signature == MagickSignature);
   debug=IsEventLogging();
+  (void) debug;
   image=AcquireImage(image_info);
   status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception);
   if (status == MagickFalse)
@@ -928,6 +930,7 @@ static Image *ReadJPEGImage(const ImageInfo *image_info,
   /*
     Initialize JPEG parameters.
   */
+  (void) ResetMagickMemory(&error_manager,0,sizeof(error_manager));
   (void) ResetMagickMemory(&jpeg_info,0,sizeof(jpeg_info));
   (void) ResetMagickMemory(&jpeg_error,0,sizeof(jpeg_error));
   jpeg_info.err=jpeg_std_error(&jpeg_error);
@@ -954,7 +957,7 @@ static Image *ReadJPEGImage(const ImageInfo *image_info,
   for (i=1; i < 16; i++)
     if ((i != 2) && (i != 13) && (i != 14))
       jpeg_set_marker_processor(&jpeg_info,(int) (JPEG_APP0+i),ReadProfile);
-  i=jpeg_read_header(&jpeg_info,MagickTrue);
+  i=(ssize_t) jpeg_read_header(&jpeg_info,MagickTrue);
   if ((image_info->colorspace == YCbCrColorspace) ||
       (image_info->colorspace == Rec601YCbCrColorspace) ||
       (image_info->colorspace == Rec709YCbCrColorspace))
@@ -1014,8 +1017,8 @@ static Image *ReadJPEGImage(const ImageInfo *image_info,
       jpeg_info.scale_denom=(unsigned int) scale_factor;
       jpeg_calc_output_dimensions(&jpeg_info);
       if (image->debug != MagickFalse)
-        (void) LogMagickEvent(CoderEvent,GetMagickModule(),"Scale factor: %ld",
-          (long) scale_factor);
+        (void) LogMagickEvent(CoderEvent,GetMagickModule(),
+          "Scale factor: %.20g",(double) scale_factor);
     }
   precision=(size_t) jpeg_info.data_precision;
 #if (JPEG_LIB_VERSION >= 61) && defined(D_PROGRESSIVE_SUPPORTED)
@@ -1127,7 +1130,7 @@ static Image *ReadJPEGImage(const ImageInfo *image_info,
     }
   JPEGSetImageQuality(&jpeg_info,image);
   JPEGSetImageSamplingFactor(&jpeg_info,image);
-  (void) FormatMagickString(value,MaxTextExtent,"%ld",(long)
+  (void) FormatMagickString(value,MaxTextExtent,"%.20g",(double)
     jpeg_info.out_color_space);
   (void) SetImageProperty(image,"jpeg:colorspace",value);
   if (image_info->ping != MagickFalse)
@@ -1210,35 +1213,38 @@ static Image *ReadJPEGImage(const ImageInfo *image_info,
             else
               pixel=(size_t) ((GETJSAMPLE(*p) ^ 0x80) << 4);
             index=ConstrainColormapIndex(image,pixel);
-            indexes[x]=index;
-            *q++=image->colormap[(int) index];
+            SetIndexPixelComponent(indexes+x,index);
+            SetRedPixelComponent(q,image->colormap[(int) index].red);
+            SetGreenPixelComponent(q,image->colormap[(int) index].green);
+            SetBluePixelComponent(q,image->colormap[(int) index].blue);
             p++;
+            q++;
           }
         else
           if (image->colorspace != CMYKColorspace)
             for (x=0; x < (ssize_t) image->columns; x++)
             {
-              q->red=ScaleShortToQuantum((unsigned char)
-                (GETJSAMPLE(*p++) << 4));
-              q->green=ScaleShortToQuantum((unsigned char)
-                (GETJSAMPLE(*p++) << 4));
-              q->blue=ScaleShortToQuantum((unsigned char)
-                (GETJSAMPLE(*p++) << 4));
+              SetRedPixelComponent(q,ScaleShortToQuantum((unsigned char)
+                (GETJSAMPLE(*p++) << 4)));
+              SetGreenPixelComponent(q,ScaleShortToQuantum((unsigned char)
+                (GETJSAMPLE(*p++) << 4)));
+              SetBluePixelComponent(q,ScaleShortToQuantum((unsigned char)
+                (GETJSAMPLE(*p++) << 4)));
               SetOpacityPixelComponent(q,OpaqueOpacity);
               q++;
             }
           else
             for (x=0; x < (ssize_t) image->columns; x++)
             {
-              q->red=(Quantum) QuantumRange-ScaleShortToQuantum((unsigned char)
-                (GETJSAMPLE(*p++) << 4));
-              q->green=(Quantum) QuantumRange-ScaleShortToQuantum(
-                (unsigned char) (GETJSAMPLE(*p++) << 4));
-              q->blue=(Quantum) QuantumRange-ScaleShortToQuantum((unsigned char)
-                (GETJSAMPLE(*p++) << 4));
+              SetCyanPixelComponent(q,QuantumRange-ScaleShortToQuantum(
+                (unsigned char) (GETJSAMPLE(*p++) << 4)));
+              SetMagentaPixelComponent(q,QuantumRange-ScaleShortToQuantum(
+                (unsigned char) (GETJSAMPLE(*p++) << 4)));
+              SetYellowPixelComponent(q,QuantumRange-ScaleShortToQuantum(
+                (unsigned char) (GETJSAMPLE(*p++) << 4)));
+              SetBlackPixelComponent(indexes+x,QuantumRange-ScaleShortToQuantum(
+                (unsigned char) (GETJSAMPLE(*p++) << 4)));
               SetOpacityPixelComponent(q,OpaqueOpacity);
-              indexes[x]=(IndexPacket) QuantumRange-ScaleShortToQuantum(
-                (unsigned char) (GETJSAMPLE(*p++) << 4));
               q++;
             }
       }
@@ -1247,37 +1253,45 @@ static Image *ReadJPEGImage(const ImageInfo *image_info,
         for (x=0; x < (ssize_t) image->columns; x++)
         {
           index=ConstrainColormapIndex(image,(size_t) GETJSAMPLE(*p));
-          indexes[x]=(IndexPacket) index;
-          *q++=image->colormap[(int) index];
+          SetIndexPixelComponent(indexes+x,index);
+          SetRedPixelComponent(q,image->colormap[(int) index].red);
+          SetGreenPixelComponent(q,image->colormap[(int) index].green);
+          SetBluePixelComponent(q,image->colormap[(int) index].blue);
           p++;
+          q++;
         }
       else
         if (image->colorspace != CMYKColorspace)
           for (x=0; x < (ssize_t) image->columns; x++)
           {
-            q->red=ScaleCharToQuantum((unsigned char) GETJSAMPLE(*p++));
-            q->green=ScaleCharToQuantum((unsigned char) GETJSAMPLE(*p++));
-            q->blue=ScaleCharToQuantum((unsigned char) GETJSAMPLE(*p++));
+            SetRedPixelComponent(q,ScaleCharToQuantum((unsigned char)
+              GETJSAMPLE(*p++)));
+            SetGreenPixelComponent(q,ScaleCharToQuantum((unsigned char)
+              GETJSAMPLE(*p++)));
+            SetBluePixelComponent(q,ScaleCharToQuantum((unsigned char)
+              GETJSAMPLE(*p++)));
             SetOpacityPixelComponent(q,OpaqueOpacity);
             q++;
           }
         else
           for (x=0; x < (ssize_t) image->columns; x++)
           {
-            q->red=(Quantum) QuantumRange-ScaleCharToQuantum((unsigned char)
-              GETJSAMPLE(*p++));
-            q->green=(Quantum) QuantumRange-ScaleCharToQuantum((unsigned char)
-              GETJSAMPLE(*p++));
-            q->blue=(Quantum) QuantumRange-ScaleCharToQuantum((unsigned char)
-              GETJSAMPLE(*p++));
+            SetCyanPixelComponent(q,QuantumRange-ScaleCharToQuantum(
+              (unsigned char) GETJSAMPLE(*p++)));
+            SetMagentaPixelComponent(q,QuantumRange-ScaleCharToQuantum(
+              (unsigned char) GETJSAMPLE(*p++)));
+            SetYellowPixelComponent(q,QuantumRange-ScaleCharToQuantum(
+              (unsigned char) GETJSAMPLE(*p++)));
+            SetBlackPixelComponent(indexes+x,QuantumRange-ScaleCharToQuantum(
+              (unsigned char) GETJSAMPLE(*p++)));
             SetOpacityPixelComponent(q,OpaqueOpacity);
-            indexes[x]=(IndexPacket) QuantumRange-ScaleCharToQuantum(
-              (unsigned char) GETJSAMPLE(*p++));
             q++;
           }
     if (SyncAuthenticPixels(image,exception) == MagickFalse)
       break;
-    if (SetImageProgress(image,LoadImageTag,y,image->rows) == MagickFalse)
+    status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y,
+      image->rows);
+    if (status == MagickFalse)
       break;
   }
   /*
@@ -1413,7 +1427,8 @@ ModuleExport void UnregisterJPEGImage(void)
 %
 %  The format of the WriteJPEGImage method is:
 %
-%      MagickBooleanType WriteJPEGImage(const ImageInfo *image_info,Image *image)
+%      MagickBooleanType WriteJPEGImage(const ImageInfo *image_info,
+%        Image *image)
 %
 %  A description of each parameter follows:
 %
@@ -1491,14 +1506,12 @@ static void WriteProfile(j_compress_ptr jpeg_info,Image *image)
     i;
 
   size_t
-    length;
+    length,
+    tag_length;
 
   StringInfo
     *custom_profile;
 
-  size_t
-    tag_length;
-
   /*
     Save image profile as a APP marker.
   */
@@ -1507,7 +1520,11 @@ static void WriteProfile(j_compress_ptr jpeg_info,Image *image)
   ResetImageProfileIterator(image);
   for (name=GetNextImageProfile(image); name != (const char *) NULL; )
   {
+    register unsigned char
+      *p;
+
     profile=GetImageProfile(image,name);
+    p=GetStringInfoDatum(custom_profile);
     if (LocaleCompare(name,"EXIF") == 0)
       for (i=0; i < (ssize_t) GetStringInfoLength(profile); i+=65533L)
       {
@@ -1526,7 +1543,6 @@ static void WriteProfile(j_compress_ptr jpeg_info,Image *image)
         for (i=0; i < (ssize_t) GetStringInfoLength(profile); i+=65519L)
         {
           length=MagickMin(GetStringInfoLength(profile)-i,65519L);
-          p=GetStringInfoDatum(custom_profile);
           p[12]=(unsigned char) ((i/65519L)+1);
           p[13]=(unsigned char) (GetStringInfoLength(profile)/65519L+1);
           (void) CopyMagickMemory(p+tag_length,GetStringInfoDatum(profile)+i,
@@ -1538,33 +1554,28 @@ static void WriteProfile(j_compress_ptr jpeg_info,Image *image)
     if (((LocaleCompare(name,"IPTC") == 0) ||
         (LocaleCompare(name,"8BIM") == 0)) && (iptc == MagickFalse))
       {
-        register unsigned char
-          *p;
-
         size_t
           roundup;
 
         iptc=MagickTrue;
-        p=GetStringInfoDatum(custom_profile);
-        if (LocaleNCompare((char *) GetStringInfoDatum(profile),"8BIM",4) == 0)
-          {
-            (void) CopyMagickMemory(p,"Photoshop 3.0\0",14);
-            tag_length=14;
-          }
-        else
-          {
-            (void) CopyMagickMemory(p,"Photoshop 3.0\08BIM\04\04\0\0\0\0",24);
-            p[13]=0x00;
-            p[24]=(unsigned char) (GetStringInfoLength(profile) >> 8);
-            p[25]=(unsigned char) (GetStringInfoLength(profile) & 0xff);
-            tag_length=26;
-          }
         for (i=0; i < (ssize_t) GetStringInfoLength(profile); i+=65500L)
         {
           length=MagickMin(GetStringInfoLength(profile)-i,65500L);
           roundup=(size_t) (length & 0x01);
-          (void) CopyMagickMemory(p+tag_length,GetStringInfoDatum(profile)+i,
-            length);
+          if (LocaleNCompare((char *) GetStringInfoDatum(profile),"8BIM",4) == 0)
+            {
+              (void) memcpy(p,"Photoshop 3.0 ",14);
+              tag_length=14;
+            }
+          else
+            {
+              (void) CopyMagickMemory(p,"Photoshop 3.0 8BIM\04\04\0\0\0\0",24);
+              tag_length=26;
+              p[24]=(unsigned char) (length >> 8);
+              p[25]=(unsigned char) (length & 0xff);
+            }
+          p[13]=0x00;
+          (void) memcpy(p+tag_length,GetStringInfoDatum(profile)+i,length);
           if (roundup != 0)
             p[length+tag_length]='\0';
           jpeg_write_marker(jpeg_info,IPTC_MARKER,GetStringInfoDatum(
@@ -1590,8 +1601,8 @@ static void WriteProfile(j_compress_ptr jpeg_info,Image *image)
         }
         xmp_profile=DestroyStringInfo(xmp_profile);
       }
-    (void) LogMagickEvent(CoderEvent,GetMagickModule(),"%s profile: %lu bytes",
-      name,(unsigned long) GetStringInfoLength(profile));
+    (void) LogMagickEvent(CoderEvent,GetMagickModule(),
+      "%s profile: %.20g bytes",name,(double) GetStringInfoLength(profile));
     name=GetNextImageProfile(image);
   }
   custom_profile=DestroyStringInfo(custom_profile);
@@ -1677,9 +1688,6 @@ static MagickBooleanType WriteJPEGImage(const ImageInfo *image_info,
   JSAMPROW
     scanline[1];
 
-  ssize_t
-    y;
-
   MagickBooleanType
     status;
 
@@ -1689,6 +1697,9 @@ static MagickBooleanType WriteJPEGImage(const ImageInfo *image_info,
   register ssize_t
     i;
 
+  ssize_t
+    y;
+
   struct jpeg_compress_struct
     jpeg_info;
 
@@ -1710,6 +1721,7 @@ static MagickBooleanType WriteJPEGImage(const ImageInfo *image_info,
   /*
     Initialize JPEG parameters.
   */
+  (void) ResetMagickMemory(&error_manager,0,sizeof(error_manager));
   (void) ResetMagickMemory(&jpeg_info,0,sizeof(jpeg_info));
   (void) ResetMagickMemory(&jpeg_error,0,sizeof(jpeg_error));
   jpeg_info.client_data=(void *) image;
@@ -1780,8 +1792,8 @@ static MagickBooleanType WriteJPEGImage(const ImageInfo *image_info,
   jpeg_info.density_unit=(UINT8) 1;
   if (image->debug != MagickFalse)
     (void) LogMagickEvent(CoderEvent,GetMagickModule(),
-      "Image resolution: %ld,%ld",(long) floor(image->x_resolution+0.5),
-      (long) floor(image->y_resolution+0.5));
+      "Image resolution: %.20g,%.20g",floor(image->x_resolution+0.5),
+      floor(image->y_resolution+0.5));
   if ((image->x_resolution != 0.0) && (image->y_resolution != 0.0))
     {
       /*
@@ -1919,8 +1931,8 @@ static MagickBooleanType WriteJPEGImage(const ImageInfo *image_info,
       else
         jpeg_set_quality(&jpeg_info,(int) image->quality,MagickTrue);
       if (image->debug != MagickFalse)
-        (void) LogMagickEvent(CoderEvent,GetMagickModule(),"Quality: %lu",
-          (unsigned long) image->quality);
+        (void) LogMagickEvent(CoderEvent,GetMagickModule(),"Quality: %.20g",
+          (double) image->quality);
     }
   else
     {
@@ -2025,11 +2037,11 @@ static MagickBooleanType WriteJPEGImage(const ImageInfo *image_info,
       else
         (void) LogMagickEvent(CoderEvent,GetMagickModule(),
           "Storage class: DirectClass");
-      (void) LogMagickEvent(CoderEvent,GetMagickModule(),"Depth: %lu",
-        (unsigned long) image->depth);
+      (void) LogMagickEvent(CoderEvent,GetMagickModule(),"Depth: %.20g",
+        (double) image->depth);
       if (image->colors != 0)
         (void) LogMagickEvent(CoderEvent,GetMagickModule(),
-          "Number of colors: %lu",(unsigned long) image->colors);
+          "Number of colors: %.20g",(double) image->colors);
       else
         (void) LogMagickEvent(CoderEvent,GetMagickModule(),
           "Number of colors: unspecified");
@@ -2183,7 +2195,9 @@ static MagickBooleanType WriteJPEGImage(const ImageInfo *image_info,
             p++;
           }
           (void) jpeg_write_scanlines(&jpeg_info,scanline,1);
-          if (SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,image->rows) == MagickFalse)
+          status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
+            image->rows);
+          if (status == MagickFalse)
             break;
         }
       else
@@ -2206,7 +2220,9 @@ static MagickBooleanType WriteJPEGImage(const ImageInfo *image_info,
               p++;
             }
             (void) jpeg_write_scanlines(&jpeg_info,scanline,1);
-            if (SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,image->rows) == MagickFalse)
+            status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
+              image->rows);
+            if (status == MagickFalse)
               break;
           }
         else
@@ -2238,11 +2254,13 @@ static MagickBooleanType WriteJPEGImage(const ImageInfo *image_info,
               *q++=(JSAMPLE) (ScaleQuantumToChar((Quantum) (QuantumRange-
                 GetBluePixelComponent(p))));
               *q++=(JSAMPLE) (ScaleQuantumToChar((Quantum) (QuantumRange-
-                indexes[x])));
+                GetIndexPixelComponent(indexes+x))));
               p++;
             }
             (void) jpeg_write_scanlines(&jpeg_info,scanline,1);
-            if (SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,image->rows) == MagickFalse)
+            status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
+              image->rows);
+            if (status == MagickFalse)
               break;
           }
     }
@@ -2267,7 +2285,9 @@ static MagickBooleanType WriteJPEGImage(const ImageInfo *image_info,
           p++;
         }
         (void) jpeg_write_scanlines(&jpeg_info,scanline,1);
-        if (SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,image->rows) == MagickFalse)
+        status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
+          image->rows);
+        if (status == MagickFalse)
           break;
       }
     else
@@ -2297,7 +2317,7 @@ static MagickBooleanType WriteJPEGImage(const ImageInfo *image_info,
           }
           (void) jpeg_write_scanlines(&jpeg_info,scanline,1);
           status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
-                image->rows);
+            image->rows);
           if (status == MagickFalse)
             break;
         }
@@ -2329,12 +2349,13 @@ static MagickBooleanType WriteJPEGImage(const ImageInfo *image_info,
               GetGreenPixelComponent(p)) >> 4));
             *q++=(JSAMPLE) (4095-(ScaleQuantumToShort(
               GetBluePixelComponent(p)) >> 4));
-            *q++=(JSAMPLE) (4095-(ScaleQuantumToShort(indexes[x]) >> 4));
+            *q++=(JSAMPLE) (4095-(ScaleQuantumToShort(
+              GetIndexPixelComponent(indexes+x)) >> 4));
             p++;
           }
           (void) jpeg_write_scanlines(&jpeg_info,scanline,1);
           status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
-                image->rows);
+            image->rows);
           if (status == MagickFalse)
             break;
         }