]> granicus.if.org Git - imagemagick/blobdiff - coders/pcx.c
(no commit message)
[imagemagick] / coders / pcx.c
index 932ae067e82b951c01541152d8374ef4c4b869f5..3ad7e1df5a7e67a435c67bf85c57edbd0ba6f225 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  %
 /*
   Include declarations.
 */
-#include "magick/studio.h"
-#include "magick/blob.h"
-#include "magick/blob-private.h"
-#include "magick/cache.h"
-#include "magick/color.h"
-#include "magick/color-private.h"
-#include "magick/colorspace.h"
-#include "magick/exception.h"
-#include "magick/exception-private.h"
-#include "magick/image.h"
-#include "magick/image-private.h"
-#include "magick/list.h"
-#include "magick/magick.h"
-#include "magick/memory_.h"
-#include "magick/monitor.h"
-#include "magick/monitor-private.h"
-#include "magick/quantum-private.h"
-#include "magick/static.h"
-#include "magick/string_.h"
-#include "magick/module.h"
+#include "MagickCore/studio.h"
+#include "MagickCore/attribute.h"
+#include "MagickCore/blob.h"
+#include "MagickCore/blob-private.h"
+#include "MagickCore/cache.h"
+#include "MagickCore/color.h"
+#include "MagickCore/color-private.h"
+#include "MagickCore/colormap.h"
+#include "MagickCore/colorspace.h"
+#include "MagickCore/colorspace-private.h"
+#include "MagickCore/exception.h"
+#include "MagickCore/exception-private.h"
+#include "MagickCore/image.h"
+#include "MagickCore/image-private.h"
+#include "MagickCore/list.h"
+#include "MagickCore/magick.h"
+#include "MagickCore/memory_.h"
+#include "MagickCore/monitor.h"
+#include "MagickCore/monitor-private.h"
+#include "MagickCore/pixel-accessor.h"
+#include "MagickCore/quantum-private.h"
+#include "MagickCore/static.h"
+#include "MagickCore/string_.h"
+#include "MagickCore/module.h"
 \f
 /*
   Typedef declarations.
@@ -95,7 +99,7 @@ typedef struct _PCXInfo
   Forward declarations.
 */
 static MagickBooleanType
-  WritePCXImage(const ImageInfo *,Image *);
+  WritePCXImage(const ImageInfo *,Image *,ExceptionInfo *);
 \f
 /*
 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
@@ -196,7 +200,7 @@ static MagickBooleanType IsPCX(const unsigned char *magick,const size_t length)
 %
 */
 
-static inline long MagickAbsoluteValue(const long x)
+static inline ssize_t MagickAbsoluteValue(const ssize_t x)
 {
   if (x < 0)
     return(-x);
@@ -227,9 +231,6 @@ static Image *ReadPCXImage(const ImageInfo *image_info,ExceptionInfo *exception)
     id,
     mask;
 
-  long
-    y;
-
   MagickBooleanType
     status;
 
@@ -240,24 +241,26 @@ static Image *ReadPCXImage(const ImageInfo *image_info,ExceptionInfo *exception)
   PCXInfo
     pcx_info;
 
-  register IndexPacket
-    *indexes;
-
-  register long
+  register ssize_t
     x;
 
-  register PixelPacket
+  register Quantum
     *q;
 
-  register long
+  register ssize_t
     i;
 
   register unsigned char
     *p,
     *r;
 
+  size_t
+    one,
+    pcx_packets;
+
   ssize_t
-    count;
+    count,
+    y;
 
   unsigned char
     packet,
@@ -265,9 +268,6 @@ static Image *ReadPCXImage(const ImageInfo *image_info,ExceptionInfo *exception)
     *pcx_pixels,
     *scanline;
 
-  unsigned long
-    pcx_packets;
-
   /*
     Open image file.
   */
@@ -291,7 +291,7 @@ static Image *ReadPCXImage(const ImageInfo *image_info,ExceptionInfo *exception)
   page_table=(MagickOffsetType *) NULL;
   if (LocaleCompare(image_info->magick,"DCX") == 0)
     {
-      unsigned long
+      size_t
         magic;
 
       /*
@@ -338,9 +338,9 @@ static Image *ReadPCXImage(const ImageInfo *image_info,ExceptionInfo *exception)
     /*
       Read PCX raster colormap.
     */
-    image->columns=(unsigned long) MagickAbsoluteValue((long) pcx_info.right-
+    image->columns=(size_t) MagickAbsoluteValue((ssize_t) pcx_info.right-
       pcx_info.left)+1UL;
-    image->rows=(unsigned long) MagickAbsoluteValue((long) pcx_info.bottom-
+    image->rows=(size_t) MagickAbsoluteValue((ssize_t) pcx_info.bottom-
       pcx_info.top)+1UL;
     if ((image->columns == 0) || (image->rows == 0) ||
         (pcx_info.bits_per_pixel == 0))
@@ -357,17 +357,18 @@ static Image *ReadPCXImage(const ImageInfo *image_info,ExceptionInfo *exception)
     count=ReadBlob(image,3*image->colors,pcx_colormap);
     pcx_info.reserved=(unsigned char) ReadBlobByte(image);
     pcx_info.planes=(unsigned char) ReadBlobByte(image);
+    one=1;
     if ((pcx_info.bits_per_pixel != 8) || (pcx_info.planes == 1))
       if ((pcx_info.version == 3) || (pcx_info.version == 5) ||
           ((pcx_info.bits_per_pixel*pcx_info.planes) == 1))
-        image->colors=(unsigned long) MagickMin(1UL << (1UL*
+        image->colors=(size_t) MagickMin(one << (1UL*
           (pcx_info.bits_per_pixel*pcx_info.planes)),256UL);
     if (AcquireImageColormap(image,image->colors) == MagickFalse)
       ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
     if ((pcx_info.bits_per_pixel >= 8) && (pcx_info.planes != 1))
       image->storage_class=DirectClass;
     p=pcx_colormap;
-    for (i=0; i < (long) image->colors; i++)
+    for (i=0; i < (ssize_t) image->colors; i++)
     {
       image->colormap[i].red=ScaleCharToQuantum(*p++);
       image->colormap[i].green=ScaleCharToQuantum(*p++);
@@ -383,7 +384,7 @@ static Image *ReadPCXImage(const ImageInfo *image_info,ExceptionInfo *exception)
     /*
       Read image data.
     */
-    pcx_packets=(unsigned long) image->rows*pcx_info.bytes_per_line*
+    pcx_packets=(size_t) image->rows*pcx_info.bytes_per_line*
       pcx_info.planes;
     pcx_pixels=(unsigned char *) AcquireQuantumMemory(pcx_packets,
       sizeof(*pcx_pixels));
@@ -461,7 +462,7 @@ static Image *ReadPCXImage(const ImageInfo *image_info,ExceptionInfo *exception)
                 pcx_info.colormap_signature=(unsigned char) ReadBlobByte(image);
                 count=ReadBlob(image,3*image->colors,pcx_colormap);
                 p=pcx_colormap;
-                for (i=0; i < (long) image->colors; i++)
+                for (i=0; i < (ssize_t) image->colors; i++)
                 {
                   image->colormap[i].red=ScaleCharToQuantum(*p++);
                   image->colormap[i].green=ScaleCharToQuantum(*p++);
@@ -473,19 +474,18 @@ static Image *ReadPCXImage(const ImageInfo *image_info,ExceptionInfo *exception)
     /*
       Convert PCX raster image to pixel packets.
     */
-    for (y=0; y < (long) image->rows; y++)
+    for (y=0; y < (ssize_t) image->rows; y++)
     {
       p=pcx_pixels+(y*pcx_info.bytes_per_line*pcx_info.planes);
       q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
-      if (q == (PixelPacket *) NULL)
+      if (q == (const Quantum *) NULL)
         break;
-      indexes=GetAuthenticIndexQueue(image);
       r=scanline;
       if (image->storage_class == DirectClass)
         for (i=0; i < pcx_info.planes; i++)
         {
           r=scanline+i;
-          for (x=0; x < (long) pcx_info.bytes_per_line; x++)
+          for (x=0; x < (ssize_t) pcx_info.bytes_per_line; x++)
           {
             switch (i)
             {
@@ -517,12 +517,12 @@ static Image *ReadPCXImage(const ImageInfo *image_info,ExceptionInfo *exception)
       else
         if (pcx_info.planes > 1)
           {
-            for (x=0; x < (long) image->columns; x++)
+            for (x=0; x < (ssize_t) image->columns; x++)
               *r++=0;
             for (i=0; i < pcx_info.planes; i++)
             {
               r=scanline;
-              for (x=0; x < (long) pcx_info.bytes_per_line; x++)
+              for (x=0; x < (ssize_t) pcx_info.bytes_per_line; x++)
               {
                  bits=(*p++);
                  for (mask=0x80; mask != 0; mask>>=1)
@@ -539,10 +539,10 @@ static Image *ReadPCXImage(const ImageInfo *image_info,ExceptionInfo *exception)
           {
             case 1:
             {
-              register long
+              register ssize_t
                 bit;
 
-              for (x=0; x < ((long) image->columns-7); x+=8)
+              for (x=0; x < ((ssize_t) image->columns-7); x+=8)
               {
                 for (bit=7; bit >= 0; bit--)
                   *r++=(unsigned char) ((*p) & (0x01 << bit) ? 0x01 : 0x00);
@@ -550,7 +550,7 @@ static Image *ReadPCXImage(const ImageInfo *image_info,ExceptionInfo *exception)
               }
               if ((image->columns % 8) != 0)
                 {
-                  for (bit=7; bit >= (long) (8-(image->columns % 8)); bit--)
+                  for (bit=7; bit >= (ssize_t) (8-(image->columns % 8)); bit--)
                     *r++=(unsigned char) ((*p) & (0x01 << bit) ? 0x01 : 0x00);
                   p++;
                 }
@@ -558,7 +558,7 @@ static Image *ReadPCXImage(const ImageInfo *image_info,ExceptionInfo *exception)
             }
             case 2:
             {
-              for (x=0; x < ((long) image->columns-3); x+=4)
+              for (x=0; x < ((ssize_t) image->columns-3); x+=4)
               {
                 *r++=(*p >> 6) & 0x3;
                 *r++=(*p >> 4) & 0x3;
@@ -568,7 +568,7 @@ static Image *ReadPCXImage(const ImageInfo *image_info,ExceptionInfo *exception)
               }
               if ((image->columns % 4) != 0)
                 {
-                  for (i=3; i >= (long) (4-(image->columns % 4)); i--)
+                  for (i=3; i >= (ssize_t) (4-(image->columns % 4)); i--)
                     *r++=(unsigned char) ((*p >> (i*2)) & 0x03);
                   p++;
                 }
@@ -576,7 +576,7 @@ static Image *ReadPCXImage(const ImageInfo *image_info,ExceptionInfo *exception)
             }
             case 4:
             {
-              for (x=0; x < ((long) image->columns-1); x+=2)
+              for (x=0; x < ((ssize_t) image->columns-1); x+=2)
               {
                 *r++=(*p >> 4) & 0xf;
                 *r++=(*p) & 0xf;
@@ -598,25 +598,26 @@ static Image *ReadPCXImage(const ImageInfo *image_info,ExceptionInfo *exception)
         Transfer image scanline.
       */
       r=scanline;
-      for (x=0; x < (long) image->columns; x++)
+      for (x=0; x < (ssize_t) image->columns; x++)
       {
         if (image->storage_class == PseudoClass)
-          indexes[x]=(IndexPacket) (*r++);
+          SetPixelIndex(image,*r++,q);
         else
           {
-            q->red=ScaleCharToQuantum(*r++);
-            q->green=ScaleCharToQuantum(*r++);
-            q->blue=ScaleCharToQuantum(*r++);
+            SetPixelRed(image,ScaleCharToQuantum(*r++),q);
+            SetPixelGreen(image,ScaleCharToQuantum(*r++),q);
+            SetPixelBlue(image,ScaleCharToQuantum(*r++),q);
             if (image->matte != MagickFalse)
-              q->opacity=(Quantum) (QuantumRange-ScaleCharToQuantum(*r++));
+              SetPixelAlpha(image,ScaleCharToQuantum(*r++),q);
           }
-        q++;
+        q+=GetPixelChannels(image);
       }
       if (SyncAuthenticPixels(image,exception) == MagickFalse)
         break;
       if (image->previous == (Image *) NULL)
         {
-          status=SetImageProgress(image,LoadImageTag,y,image->rows);
+          status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y,
+            image->rows);
           if (status == MagickFalse)
             break;
         }
@@ -691,10 +692,10 @@ static Image *ReadPCXImage(const ImageInfo *image_info,ExceptionInfo *exception)
 %
 %  The format of the RegisterPCXImage method is:
 %
-%      unsigned long RegisterPCXImage(void)
+%      size_t RegisterPCXImage(void)
 %
 */
-ModuleExport unsigned long RegisterPCXImage(void)
+ModuleExport size_t RegisterPCXImage(void)
 {
   MagickInfo
     *entry;
@@ -760,7 +761,8 @@ ModuleExport void UnregisterPCXImage(void)
 %
 %  The format of the WritePCXImage method is:
 %
-%      MagickBooleanType WritePCXImage(const ImageInfo *image_info,Image *image)
+%      MagickBooleanType WritePCXImage(const ImageInfo *image_info,
+%        Image *image,ExceptionInfo *exception)
 %
 %  A description of each parameter follows.
 %
@@ -768,15 +770,17 @@ ModuleExport void UnregisterPCXImage(void)
 %
 %    o image:  The image.
 %
+%    o exception: return any errors or warnings in this structure.
 %
 */
+
 static MagickBooleanType PCXWritePixels(PCXInfo *pcx_info,
   const unsigned char *pixels,Image *image)
 {
   register const unsigned char
     *q;
 
-  register long
+  register ssize_t
     i,
     x;
 
@@ -788,42 +792,48 @@ static MagickBooleanType PCXWritePixels(PCXInfo *pcx_info,
     previous;
 
   q=pixels;
-  for (i=0; i < (long) pcx_info->planes; i++)
+  for (i=0; i < (ssize_t) pcx_info->planes; i++)
   {
-    previous=(*q++);
-    count=1;
-    for (x=0; x < (long) (pcx_info->bytes_per_line-1); x++)
-    {
-      packet=(*q++);
-      if ((packet == previous) && (count < 63))
-        {
-          count++;
-          continue;
-        }
-      if ((count > 1) || ((previous & 0xc0) == 0xc0))
+    if (pcx_info->encoding == 0)
+      {
+        for (x=0; x < (ssize_t) pcx_info->bytes_per_line; x++)
+          (void) WriteBlobByte(image,(unsigned char) (*q++));
+      }
+    else
+      {
+        previous=(*q++);
+        count=1;
+        for (x=0; x < (ssize_t) (pcx_info->bytes_per_line-1); x++)
         {
-          count|=0xc0;
-          (void) WriteBlobByte(image,(unsigned char) count);
+          packet=(*q++);
+          if ((packet == previous) && (count < 63))
+            {
+              count++;
+              continue;
+            }
+          if ((count > 1) || ((previous & 0xc0) == 0xc0))
+            {
+              count|=0xc0;
+              (void) WriteBlobByte(image,(unsigned char) count);
+            }
+          (void) WriteBlobByte(image,previous);
+          previous=packet;
+          count=1;
         }
-      (void) WriteBlobByte(image,previous);
-      previous=packet;
-      count=1;
-    }
-    if ((count > 1) || ((previous & 0xc0) == 0xc0))
-      {
-        count|=0xc0;
-        (void) WriteBlobByte(image,(unsigned char) count);
+        if ((count > 1) || ((previous & 0xc0) == 0xc0))
+          {
+            count|=0xc0;
+            (void) WriteBlobByte(image,(unsigned char) count);
+          }
+        (void) WriteBlobByte(image,previous);
       }
-    (void) WriteBlobByte(image,previous);
   }
   return (MagickTrue);
 }
 
-static MagickBooleanType WritePCXImage(const ImageInfo *image_info,Image *image)
+static MagickBooleanType WritePCXImage(const ImageInfo *image_info,Image *image,
+  ExceptionInfo *exception)
 {
-  long
-    y;
-
   MagickBooleanType
     status;
 
@@ -835,13 +845,10 @@ static MagickBooleanType WritePCXImage(const ImageInfo *image_info,Image *image)
   PCXInfo
     pcx_info;
 
-  register const IndexPacket
-    *indexes;
-
-  register const PixelPacket
+  register const Quantum
     *p;
 
-  register long
+  register ssize_t
     i,
     x;
 
@@ -851,6 +858,9 @@ static MagickBooleanType WritePCXImage(const ImageInfo *image_info,Image *image)
   size_t
     length;
 
+  ssize_t
+    y;
+
   unsigned char
     *pcx_colormap,
     *pcx_pixels;
@@ -864,10 +874,10 @@ static MagickBooleanType WritePCXImage(const ImageInfo *image_info,Image *image)
   assert(image->signature == MagickSignature);
   if (image->debug != MagickFalse)
     (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
-  status=OpenBlob(image_info,image,WriteBinaryBlobMode,&image->exception);
+  status=OpenBlob(image_info,image,WriteBinaryBlobMode,exception);
   if (status == MagickFalse)
     return(status);
-  if (image->colorspace != RGBColorspace)
+  if (IsRGBColorspace(image->colorspace) == MagickFalse)
     (void) TransformImageColorspace(image,RGBColorspace);
   page_table=(MagickOffsetType *) NULL;
   if ((LocaleCompare(image_info->magick,"DCX") == 0) ||
@@ -895,10 +905,10 @@ static MagickBooleanType WritePCXImage(const ImageInfo *image_info,Image *image)
     */
     pcx_info.identifier=0x0a;
     pcx_info.version=5;
-    pcx_info.encoding=1;
+    pcx_info.encoding=image_info->compression == NoCompression ? 0 : 1;
     pcx_info.bits_per_pixel=8;
     if ((image->storage_class == PseudoClass) &&
-        (IsMonochromeImage(image,&image->exception) != MagickFalse))
+        (IsImageMonochrome(image,exception) != MagickFalse))
       pcx_info.bits_per_pixel=1;
     pcx_info.left=0;
     pcx_info.top=0;
@@ -931,7 +941,7 @@ static MagickBooleanType WritePCXImage(const ImageInfo *image_info,Image *image)
         if (image->matte != MagickFalse)
           pcx_info.planes++;
       }
-    pcx_info.bytes_per_line=(unsigned short) (((unsigned long) image->columns*
+    pcx_info.bytes_per_line=(unsigned short) (((size_t) image->columns*
       pcx_info.bits_per_pixel+7)/8);
     pcx_info.palette_info=1;
     pcx_info.colormap_signature=0x0c;
@@ -958,7 +968,7 @@ static MagickBooleanType WritePCXImage(const ImageInfo *image_info,Image *image)
     (void) memset(pcx_colormap,0,3*256*sizeof(*pcx_colormap));
     q=pcx_colormap;
     if ((image->storage_class == PseudoClass) && (image->colors <= 256))
-      for (i=0; i < (long) image->colors; i++)
+      for (i=0; i < (ssize_t) image->colors; i++)
       {
         *q++=ScaleQuantumToChar(image->colormap[i].red);
         *q++=ScaleQuantumToChar(image->colormap[i].green);
@@ -972,24 +982,23 @@ static MagickBooleanType WritePCXImage(const ImageInfo *image_info,Image *image)
     for (i=0; i < 58; i++)
       (void) WriteBlobByte(image,'\0');
     length=(size_t) pcx_info.bytes_per_line;
-    pcx_pixels=(unsigned char *) AcquireQuantumMemory(length,
-      pcx_info.planes*sizeof(*pcx_pixels));
+    pcx_pixels=(unsigned char *) AcquireQuantumMemory(length,pcx_info.planes*
+      sizeof(*pcx_pixels));
     if (pcx_pixels == (unsigned char *) NULL)
       ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
     q=pcx_pixels;
     if ((image->storage_class == DirectClass) || (image->colors > 256))
       {
-        const PixelPacket
+        const Quantum
           *pixels;
 
         /*
           Convert DirectClass image to PCX raster pixels.
         */
-        for (y=0; y < (long) image->rows; y++)
+        for (y=0; y < (ssize_t) image->rows; y++)
         {
-          pixels=GetVirtualPixels(image,0,y,image->columns,1,
-            &image->exception);
-          if (pixels == (const PixelPacket *) NULL)
+          pixels=GetVirtualPixels(image,0,y,image->columns,1,exception);
+          if (pixels == (const Quantum *) NULL)
             break;
           q=pcx_pixels;
           for (i=0; i < pcx_info.planes; i++)
@@ -999,38 +1008,38 @@ static MagickBooleanType WritePCXImage(const ImageInfo *image_info,Image *image)
             {
               case 0:
               {
-                for (x=0; x < (long) pcx_info.bytes_per_line; x++)
+                for (x=0; x < (ssize_t) pcx_info.bytes_per_line; x++)
                 {
-                  *q++=ScaleQuantumToChar(GetRedSample(p));
-                  p++;
+                  *q++=ScaleQuantumToChar(GetPixelRed(image,p));
+                  p+=GetPixelChannels(image);
                 }
                 break;
               }
               case 1:
               {
-                for (x=0; x < (long) pcx_info.bytes_per_line; x++)
+                for (x=0; x < (ssize_t) pcx_info.bytes_per_line; x++)
                 {
-                  *q++=ScaleQuantumToChar(GetGreenSample(p));
-                  p++;
+                  *q++=ScaleQuantumToChar(GetPixelGreen(image,p));
+                  p+=GetPixelChannels(image);
                 }
                 break;
               }
               case 2:
               {
-                for (x=0; x < (long) pcx_info.bytes_per_line; x++)
+                for (x=0; x < (ssize_t) pcx_info.bytes_per_line; x++)
                 {
-                  *q++=ScaleQuantumToChar(GetBlueSample(p));
-                  p++;
+                  *q++=ScaleQuantumToChar(GetPixelBlue(image,p));
+                  p+=GetPixelChannels(image);
                 }
                 break;
               }
               case 3:
               default:
               {
-                for (x=(long) pcx_info.bytes_per_line; x != 0; x--)
+                for (x=(ssize_t) pcx_info.bytes_per_line; x != 0; x--)
                 {
-                  *q++=ScaleQuantumToChar((Quantum) (QuantumRange-GetOpacitySample(p)));
-                  p++;
+                  *q++=ScaleQuantumToChar((Quantum) (GetPixelAlpha(image,p)));
+                  p+=GetPixelChannels(image);
                 }
                 break;
               }
@@ -1040,7 +1049,8 @@ static MagickBooleanType WritePCXImage(const ImageInfo *image_info,Image *image)
             break;
           if (image->previous == (Image *) NULL)
             {
-              status=SetImageProgress(image,SaveImageTag,y,image->rows);
+              status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
+                image->rows);
               if (status == MagickFalse)
                 break;
             }
@@ -1049,27 +1059,30 @@ static MagickBooleanType WritePCXImage(const ImageInfo *image_info,Image *image)
     else
       {
         if (pcx_info.bits_per_pixel > 1)
-          for (y=0; y < (long) image->rows; y++)
+          for (y=0; y < (ssize_t) image->rows; y++)
           {
-            p=GetVirtualPixels(image,0,y,image->columns,1,&image->exception);
-            if (p == (const PixelPacket *) NULL)
+            p=GetVirtualPixels(image,0,y,image->columns,1,exception);
+            if (p == (const Quantum *) NULL)
               break;
-            indexes=GetVirtualIndexQueue(image);
             q=pcx_pixels;
-            for (x=0; x < (long) image->columns; x++)
-              *q++=(unsigned char) indexes[x];
+            for (x=0; x < (ssize_t) image->columns; x++)
+            {
+              *q++=(unsigned char) GetPixelIndex(image,p);
+              p+=GetPixelChannels(image);
+            }
             if (PCXWritePixels(&pcx_info,pcx_pixels,image) == MagickFalse)
               break;
             if (image->previous == (Image *) NULL)
               {
-                status=SetImageProgress(image,SaveImageTag,y,image->rows);
+                status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
+                image->rows);
                 if (status == MagickFalse)
                   break;
               }
           }
         else
           {
-            IndexPacket
+            Quantum
               polarity;
 
             register unsigned char
@@ -1079,26 +1092,23 @@ static MagickBooleanType WritePCXImage(const ImageInfo *image_info,Image *image)
             /*
               Convert PseudoClass image to a PCX monochrome image.
             */
-            polarity=(IndexPacket) (PixelIntensityToQuantum(
+            polarity=(Quantum) (GetPixelPacketIntensity(
               &image->colormap[0]) < ((Quantum) QuantumRange/2) ? 1 : 0);
             if (image->colors == 2)
-              polarity=(IndexPacket) (
-                PixelIntensityToQuantum(&image->colormap[0]) <
-                PixelIntensityToQuantum(&image->colormap[1]) ? 1 : 0);
-            for (y=0; y < (long) image->rows; y++)
+              polarity=(Quantum) (GetPixelPacketIntensity(&image->colormap[0]) <
+                GetPixelPacketIntensity(&image->colormap[1]) ? 1 : 0);
+            for (y=0; y < (ssize_t) image->rows; y++)
             {
-              p=GetVirtualPixels(image,0,y,image->columns,1,
-                &image->exception);
-              if (p == (const PixelPacket *) NULL)
+              p=GetVirtualPixels(image,0,y,image->columns,1,exception);
+              if (p == (const Quantum *) NULL)
                 break;
-              indexes=GetVirtualIndexQueue(image);
               bit=0;
               byte=0;
               q=pcx_pixels;
-              for (x=0; x < (long) image->columns; x++)
+              for (x=0; x < (ssize_t) image->columns; x++)
               {
                 byte<<=1;
-                if (indexes[x] == polarity)
+                if (GetPixelIndex(image,p) == polarity)
                   byte|=0x01;
                 bit++;
                 if (bit == 8)
@@ -1107,7 +1117,7 @@ static MagickBooleanType WritePCXImage(const ImageInfo *image_info,Image *image)
                     bit=0;
                     byte=0;
                   }
-                p++;
+                p+=GetPixelChannels(image);
               }
               if (bit != 0)
                 *q++=byte << (8-bit);
@@ -1115,7 +1125,8 @@ static MagickBooleanType WritePCXImage(const ImageInfo *image_info,Image *image)
                 break;
               if (image->previous == (Image *) NULL)
                 {
-                  status=SetImageProgress(image,SaveImageTag,y,image->rows);
+                  status=SetImageProgress(image,SaveImageTag,(MagickOffsetType)
+                    y,image->rows);
                   if (status == MagickFalse)
                     break;
                 }
@@ -1126,6 +1137,10 @@ static MagickBooleanType WritePCXImage(const ImageInfo *image_info,Image *image)
       }
     pcx_pixels=(unsigned char *) RelinquishMagickMemory(pcx_pixels);
     pcx_colormap=(unsigned char *) RelinquishMagickMemory(pcx_colormap);
+    if (page_table == (MagickOffsetType *) NULL)
+      break;
+    if (scene >= 1023)
+      break;
     if (GetNextImageInList(image) == (Image *) NULL)
       break;
     image=SyncNextImageInList(image);
@@ -1133,8 +1148,6 @@ static MagickBooleanType WritePCXImage(const ImageInfo *image_info,Image *image)
       GetImageListLength(image));
     if (status == MagickFalse)
       break;
-    if (scene >= 1023)
-      break;
   } while (image_info->adjoin != MagickFalse);
   if (page_table != (MagickOffsetType *) NULL)
     {
@@ -1146,8 +1159,8 @@ static MagickBooleanType WritePCXImage(const ImageInfo *image_info,Image *image)
       if (offset < 0)
         ThrowWriterException(CorruptImageError,"ImproperImageHeader");
       (void) WriteBlobLSBLong(image,0x3ADE68B1L);
-      for (i=0; i <= (long) scene; i++)
-        (void) WriteBlobLSBLong(image,(unsigned long) page_table[i]);
+      for (i=0; i <= (ssize_t) scene; i++)
+        (void) WriteBlobLSBLong(image,(unsigned int) page_table[i]);
       page_table=(MagickOffsetType *) RelinquishMagickMemory(page_table);
     }
   if (status == MagickFalse)
@@ -1156,8 +1169,8 @@ static MagickBooleanType WritePCXImage(const ImageInfo *image_info,Image *image)
         *message;
 
       message=GetExceptionMessage(errno);
-      (void) ThrowMagickException(&image->exception,GetMagickModule(),
-        FileOpenError,"UnableToWriteFile","`%s': %s",image->filename,message);
+      (void) ThrowMagickException(exception,GetMagickModule(),FileOpenError,
+        "UnableToWriteFile","`%s': %s",image->filename,message);
       message=DestroyString(message);
     }
   (void) CloseBlob(image);