]> granicus.if.org Git - imagemagick/blobdiff - coders/dib.c
(no commit message)
[imagemagick] / coders / dib.c
index 9ae1748ee99dc81a0d35eb7b8dd708afa20b8e50..66bef8b2ef71d160ae82696155db67b39349050a 100644 (file)
@@ -17,7 +17,7 @@
 %                                 July 1992                                   %
 %                                                                             %
 %                                                                             %
-%  Copyright 1999-2010 ImageMagick Studio LLC, a non-profit organization      %
+%  Copyright 1999-2013 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/colormap.h"
-#include "magick/colormap-private.h"
-#include "magick/colorspace.h"
-#include "magick/draw.h"
-#include "magick/exception.h"
-#include "magick/exception-private.h"
-#include "magick/geometry.h"
-#include "magick/image.h"
-#include "magick/image-private.h"
-#include "magick/list.h"
-#include "magick/log.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 "magick/transform.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/colormap-private.h"
+#include "MagickCore/colorspace.h"
+#include "MagickCore/colorspace-private.h"
+#include "MagickCore/draw.h"
+#include "MagickCore/exception.h"
+#include "MagickCore/exception-private.h"
+#include "MagickCore/geometry.h"
+#include "MagickCore/image.h"
+#include "MagickCore/image-private.h"
+#include "MagickCore/list.h"
+#include "MagickCore/log.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"
+#include "MagickCore/transform.h"
 \f
 /*
   Typedef declarations.
@@ -108,7 +111,7 @@ typedef struct _DIBInfo
   Forward declarations.
 */
 static MagickBooleanType
-  WriteDIBImage(const ImageInfo *,Image *);
+  WriteDIBImage(const ImageInfo *,Image *,ExceptionInfo *);
 \f
 /*
 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
@@ -457,23 +460,16 @@ static Image *ReadDIBImage(const ImageInfo *image_info,ExceptionInfo *exception)
   Image
     *image;
 
-  IndexPacket
-    index;
-
-  ssize_t
-    bit,
-    y;
-
   MagickBooleanType
     status;
 
-  register IndexPacket
-    *indexes;
+  Quantum
+    index;
 
   register ssize_t
     x;
 
-  register PixelPacket
+  register Quantum
     *q;
 
   register ssize_t
@@ -483,17 +479,18 @@ static Image *ReadDIBImage(const ImageInfo *image_info,ExceptionInfo *exception)
     *p;
 
   size_t
+    bytes_per_line,
     length;
 
   ssize_t
-    count;
+    bit,
+    count,
+    y;
+
 
   unsigned char
     *pixels;
 
-  size_t
-    bytes_per_line;
-
   /*
     Open image file.
   */
@@ -504,7 +501,7 @@ static Image *ReadDIBImage(const ImageInfo *image_info,ExceptionInfo *exception)
       image_info->filename);
   assert(exception != (ExceptionInfo *) NULL);
   assert(exception->signature == MagickSignature);
-  image=AcquireImage(image_info);
+  image=AcquireImage(image_info,exception);
   status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception);
   if (status == MagickFalse)
     {
@@ -538,16 +535,21 @@ static Image *ReadDIBImage(const ImageInfo *image_info,ExceptionInfo *exception)
       dib_info.green_mask=ReadBlobLSBLong(image);
       dib_info.blue_mask=ReadBlobLSBLong(image);
     }
-  image->matte=dib_info.bits_per_pixel == 32 ? MagickTrue : MagickFalse;
+  image->alpha_trait=dib_info.bits_per_pixel == 32 ? BlendPixelTrait :
+    UndefinedPixelTrait;
   image->columns=(size_t) MagickAbsoluteValue(dib_info.width);
   image->rows=(size_t) MagickAbsoluteValue(dib_info.height);
   image->depth=8;
   if ((dib_info.number_colors != 0) || (dib_info.bits_per_pixel < 16))
     {
+      size_t
+        one;
+
       image->storage_class=PseudoClass;
       image->colors=dib_info.number_colors;
+      one=1;
       if (image->colors == 0)
-        image->colors=1L << dib_info.bits_per_pixel;
+        image->colors=one << dib_info.bits_per_pixel;
     }
   if (image_info->size)
     {
@@ -577,7 +579,7 @@ static Image *ReadDIBImage(const ImageInfo *image_info,ExceptionInfo *exception)
       /*
         Read DIB raster colormap.
       */
-      if (AcquireImageColormap(image,image->colors) == MagickFalse)
+      if (AcquireImageColormap(image,image->colors,exception) == MagickFalse)
         ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
       length=(size_t) image->colors;
       dib_colormap=(unsigned char *) AcquireQuantumMemory(length,
@@ -631,8 +633,8 @@ static Image *ReadDIBImage(const ImageInfo *image_info,ExceptionInfo *exception)
     Initialize image structure.
   */
   image->units=PixelsPerCentimeterResolution;
-  image->x_resolution=(double) dib_info.x_pixels/100.0;
-  image->y_resolution=(double) dib_info.y_pixels/100.0;
+  image->resolution.x=(double) dib_info.x_pixels/100.0;
+  image->resolution.y=(double) dib_info.y_pixels/100.0;
   /*
     Convert DIB raster image to pixel packets.
   */
@@ -647,16 +649,15 @@ static Image *ReadDIBImage(const ImageInfo *image_info,ExceptionInfo *exception)
       {
         p=pixels+(image->rows-y-1)*bytes_per_line;
         q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
-        if (q == (PixelPacket *) NULL)
+        if (q == (Quantum *) NULL)
           break;
-        indexes=GetAuthenticIndexQueue(image);
         for (x=0; x < ((ssize_t) image->columns-7); x+=8)
         {
           for (bit=0; bit < 8; bit++)
           {
-            index=(IndexPacket) ((*p) & (0x80 >> bit) ? 0x01 : 0x00);
-            indexes[x+bit]=index;
-            *q++=image->colormap[(ssize_t) index];
+            index=(Quantum) ((*p) & (0x80 >> bit) ? 0x01 : 0x00);
+            SetPixelIndex(image,index,q);
+            q+=GetPixelChannels(image);
           }
           p++;
         }
@@ -664,9 +665,9 @@ static Image *ReadDIBImage(const ImageInfo *image_info,ExceptionInfo *exception)
           {
             for (bit=0; bit < (ssize_t) (image->columns % 8); bit++)
             {
-              index=(IndexPacket) ((*p) & (0x80 >> bit) ? 0x01 : 0x00);
-              indexes[x+bit]=index;
-              *q++=image->colormap[(ssize_t) index];
+              index=(Quantum) ((*p) & (0x80 >> bit) ? 0x01 : 0x00);
+              SetPixelIndex(image,index,q);
+              q+=GetPixelChannels(image);
             }
             p++;
           }
@@ -680,6 +681,7 @@ static Image *ReadDIBImage(const ImageInfo *image_info,ExceptionInfo *exception)
               break;
           }
       }
+      (void) SyncImage(image,exception);
       break;
     }
     case 4:
@@ -691,24 +693,23 @@ static Image *ReadDIBImage(const ImageInfo *image_info,ExceptionInfo *exception)
       {
         p=pixels+(image->rows-y-1)*bytes_per_line;
         q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
-        if (q == (PixelPacket *) NULL)
+        if (q == (Quantum *) NULL)
           break;
-        indexes=GetAuthenticIndexQueue(image);
         for (x=0; x < ((ssize_t) image->columns-1); x+=2)
         {
-          index=ConstrainColormapIndex(image,(*p >> 4) & 0xf);
-          indexes[x]=index;
-          *q++=image->colormap[(ssize_t) index];
-          index=ConstrainColormapIndex(image,*p & 0xf);
-          indexes[x+1]=index;
-          *q++=image->colormap[(ssize_t) index];
+          index=ConstrainColormapIndex(image,(*p >> 4) & 0xf,exception);
+          SetPixelIndex(image,index,q);
+          q+=GetPixelChannels(image);
+          index=ConstrainColormapIndex(image,*p & 0xf,exception);
+          SetPixelIndex(image,index,q);
           p++;
+          q+=GetPixelChannels(image);
         }
         if ((image->columns % 2) != 0)
           {
-            index=ConstrainColormapIndex(image,(*p >> 4) & 0xf);
-            indexes[x]=index;
-            *q++=image->colormap[(ssize_t) index];
+            index=ConstrainColormapIndex(image,(*p >> 4) & 0xf,exception);
+            SetPixelIndex(image,index,q);
+            q+=GetPixelChannels(image);
             p++;
           }
         if (SyncAuthenticPixels(image,exception) == MagickFalse)
@@ -721,6 +722,7 @@ static Image *ReadDIBImage(const ImageInfo *image_info,ExceptionInfo *exception)
               break;
           }
       }
+      (void) SyncImage(image,exception);
       break;
     }
     case 8:
@@ -735,16 +737,14 @@ static Image *ReadDIBImage(const ImageInfo *image_info,ExceptionInfo *exception)
       {
         p=pixels+(image->rows-y-1)*bytes_per_line;
         q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
-        if (q == (PixelPacket *) NULL)
+        if (q == (Quantum *) NULL)
           break;
-        indexes=GetAuthenticIndexQueue(image);
         for (x=0; x < (ssize_t) image->columns; x++)
         {
-          index=ConstrainColormapIndex(image,*p);
-          indexes[x]=index;
-          *q=image->colormap[(ssize_t) index];
+          index=ConstrainColormapIndex(image,*p,exception);
+          SetPixelIndex(image,index,q);
           p++;
-          q++;
+          q+=GetPixelChannels(image);
         }
         if (SyncAuthenticPixels(image,exception) == MagickFalse)
           break;
@@ -756,6 +756,7 @@ static Image *ReadDIBImage(const ImageInfo *image_info,ExceptionInfo *exception)
               break;
           }
       }
+      (void) SyncImage(image,exception);
       break;
     }
     case 16:
@@ -773,7 +774,7 @@ static Image *ReadDIBImage(const ImageInfo *image_info,ExceptionInfo *exception)
       {
         p=pixels+(image->rows-y-1)*bytes_per_line;
         q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
-        if (q == (PixelPacket *) NULL)
+        if (q == (Quantum *) NULL)
           break;
         for (x=0; x < (ssize_t) image->columns; x++)
         {
@@ -781,23 +782,23 @@ static Image *ReadDIBImage(const ImageInfo *image_info,ExceptionInfo *exception)
           word|=(*p++ << 8);
           if (dib_info.red_mask == 0)
             {
-              q->red=ScaleCharToQuantum(ScaleColor5to8((unsigned char)
-                ((word >> 10) & 0x1f)));
-              q->green=ScaleCharToQuantum(ScaleColor5to8((unsigned char)
-                ((word >> 5) & 0x1f)));
-              q->blue=ScaleCharToQuantum(ScaleColor5to8((unsigned char)
-                (word & 0x1f)));
+              SetPixelRed(image,ScaleCharToQuantum(ScaleColor5to8(
+                (unsigned char) ((word >> 10) & 0x1f))),q);
+              SetPixelGreen(image,ScaleCharToQuantum(ScaleColor5to8(
+                (unsigned char) ((word >> 5) & 0x1f))),q);
+              SetPixelBlue(image,ScaleCharToQuantum(ScaleColor5to8(
+                (unsigned char) (word & 0x1f))),q);
             }
           else
             {
-              q->red=ScaleCharToQuantum(ScaleColor5to8((unsigned char)
-                ((word >> 11) & 0x1f)));
-              q->green=ScaleCharToQuantum(ScaleColor6to8((unsigned char)
-                ((word >> 5) & 0x3f)));
-              q->blue=ScaleCharToQuantum(ScaleColor5to8((unsigned char)
-                (word & 0x1f)));
+              SetPixelRed(image,ScaleCharToQuantum(ScaleColor5to8(
+                (unsigned char) ((word >> 11) & 0x1f))),q);
+              SetPixelGreen(image,ScaleCharToQuantum(ScaleColor6to8(
+                (unsigned char) ((word >> 5) & 0x3f))),q);
+              SetPixelBlue(image,ScaleCharToQuantum(ScaleColor5to8(
+                (unsigned char) (word & 0x1f))),q);
             }
-          q++;
+          q+=GetPixelChannels(image);
         }
         if (SyncAuthenticPixels(image,exception) == MagickFalse)
           break;
@@ -821,16 +822,16 @@ static Image *ReadDIBImage(const ImageInfo *image_info,ExceptionInfo *exception)
       {
         p=pixels+(image->rows-y-1)*bytes_per_line;
         q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
-        if (q == (PixelPacket *) NULL)
+        if (q == (Quantum *) NULL)
           break;
         for (x=0; x < (ssize_t) image->columns; x++)
         {
-          q->blue=ScaleCharToQuantum(*p++);
-          q->green=ScaleCharToQuantum(*p++);
-          q->red=ScaleCharToQuantum(*p++);
-          if (image->matte != MagickFalse)
-            q->opacity=ScaleCharToQuantum(*p++);
-          q++;
+          SetPixelBlue(image,ScaleCharToQuantum(*p++),q);
+          SetPixelGreen(image,ScaleCharToQuantum(*p++),q);
+          SetPixelRed(image,ScaleCharToQuantum(*p++),q);
+          if (image->alpha_trait == BlendPixelTrait)
+            SetPixelAlpha(image,ScaleCharToQuantum(*p++),q);
+          q+=GetPixelChannels(image);
         }
         if (SyncAuthenticPixels(image,exception) == MagickFalse)
           break;
@@ -952,7 +953,8 @@ ModuleExport void UnregisterDIBImage(void)
 %
 %  The format of the WriteDIBImage method is:
 %
-%      MagickBooleanType WriteDIBImage(const ImageInfo *image_info,Image *image)
+%      MagickBooleanType WriteDIBImage(const ImageInfo *image_info,
+%        Image *image,ExceptionInfo *exception)
 %
 %  A description of each parameter follows.
 %
@@ -960,22 +962,19 @@ ModuleExport void UnregisterDIBImage(void)
 %
 %    o image:  The image.
 %
+%    o exception: return any errors or warnings in this structure.
+%
 */
-static MagickBooleanType WriteDIBImage(const ImageInfo *image_info,Image *image)
+static MagickBooleanType WriteDIBImage(const ImageInfo *image_info,Image *image,
+  ExceptionInfo *exception)
 {
   DIBInfo
     dib_info;
 
-  ssize_t
-    y;
-
   MagickBooleanType
     status;
 
-  register const IndexPacket
-    *indexes;
-
-  register const PixelPacket
+  register const Quantum
     *p;
 
   register ssize_t
@@ -985,13 +984,16 @@ static MagickBooleanType WriteDIBImage(const ImageInfo *image_info,Image *image)
   register unsigned char
     *q;
 
+  size_t
+    bytes_per_line;
+
+  ssize_t
+    y;
+
   unsigned char
     *dib_data,
     *pixels;
 
-  size_t
-    bytes_per_line;
-
   /*
     Open output image file.
   */
@@ -1001,21 +1003,23 @@ static MagickBooleanType WriteDIBImage(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);
+  assert(exception != (ExceptionInfo *) NULL);
+  assert(exception->signature == MagickSignature);
+  status=OpenBlob(image_info,image,WriteBinaryBlobMode,exception);
   if (status == MagickFalse)
     return(status);
   /*
     Initialize DIB raster file header.
   */
-  if (image->colorspace != RGBColorspace)
-    (void) TransformImageColorspace(image,RGBColorspace);
+  if (IssRGBCompatibleColorspace(image->colorspace) == MagickFalse)
+    (void) TransformImageColorspace(image,sRGBColorspace,exception);
   if (image->storage_class == DirectClass)
     {
       /*
         Full color DIB raster.
       */
       dib_info.number_colors=0;
-      dib_info.bits_per_pixel=(unsigned short) (image->matte ? 32 : 24);
+      dib_info.bits_per_pixel=(unsigned short) (image->alpha_trait ? 32 : 24);
     }
   else
     {
@@ -1025,7 +1029,7 @@ static MagickBooleanType WriteDIBImage(const ImageInfo *image_info,Image *image)
       dib_info.bits_per_pixel=8;
       if (image_info->depth > 8)
         dib_info.bits_per_pixel=16;
-      if (IsMonochromeImage(image,&image->exception) != MagickFalse)
+      if (IsImageMonochrome(image,exception) != MagickFalse)
         dib_info.bits_per_pixel=1;
       dib_info.number_colors=(dib_info.bits_per_pixel == 16) ? 0 :
         (1UL << dib_info.bits_per_pixel);
@@ -1045,14 +1049,14 @@ static MagickBooleanType WriteDIBImage(const ImageInfo *image_info,Image *image)
     case UndefinedResolution:
     case PixelsPerInchResolution:
     {
-      dib_info.x_pixels=(size_t) (100.0*image->x_resolution/2.54);
-      dib_info.y_pixels=(size_t) (100.0*image->y_resolution/2.54);
+      dib_info.x_pixels=(size_t) (100.0*image->resolution.x/2.54);
+      dib_info.y_pixels=(size_t) (100.0*image->resolution.y/2.54);
       break;
     }
     case PixelsPerCentimeterResolution:
     {
-      dib_info.x_pixels=(size_t) (100.0*image->x_resolution);
-      dib_info.y_pixels=(size_t) (100.0*image->y_resolution);
+      dib_info.x_pixels=(size_t) (100.0*image->resolution.x);
+      dib_info.y_pixels=(size_t) (100.0*image->resolution.y);
       break;
     }
   }
@@ -1078,17 +1082,16 @@ static MagickBooleanType WriteDIBImage(const ImageInfo *image_info,Image *image)
       */
       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=pixels+(image->rows-y-1)*bytes_per_line;
         bit=0;
         byte=0;
         for (x=0; x < (ssize_t) image->columns; x++)
         {
           byte<<=1;
-          byte|=indexes[x] != 0 ? 0x01 : 0x00;
+          byte|=GetPixelIndex(image,p) != 0 ? 0x01 : 0x00;
           bit++;
           if (bit == 8)
             {
@@ -1096,7 +1099,7 @@ static MagickBooleanType WriteDIBImage(const ImageInfo *image_info,Image *image)
               bit=0;
               byte=0;
             }
-           p++;
+           p+=GetPixelChannels(image);
          }
          if (bit != 0)
            {
@@ -1106,7 +1109,7 @@ static MagickBooleanType WriteDIBImage(const ImageInfo *image_info,Image *image)
         for (x=(ssize_t) (image->columns+7)/8; x < (ssize_t) bytes_per_line; x++)
           *q++=0x00;
         status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
-                image->rows);
+          image->rows);
         if (status == MagickFalse)
           break;
       }
@@ -1119,17 +1122,19 @@ static MagickBooleanType WriteDIBImage(const ImageInfo *image_info,Image *image)
       */
       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=pixels+(image->rows-y-1)*bytes_per_line;
         for (x=0; x < (ssize_t) image->columns; x++)
-          *q++=(unsigned char) indexes[x];
+        {
+          *q++=(unsigned char) GetPixelIndex(image,p);
+          p+=GetPixelChannels(image);
+        }
         for ( ; x < (ssize_t) bytes_per_line; x++)
           *q++=0x00;
         status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
-                image->rows);
+          image->rows);
         if (status == MagickFalse)
           break;
       }
@@ -1144,24 +1149,25 @@ static MagickBooleanType WriteDIBImage(const ImageInfo *image_info,Image *image)
       */
       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;
         q=pixels+(image->rows-y-1)*bytes_per_line;
         for (x=0; x < (ssize_t) image->columns; x++)
         {
           word=(unsigned short) ((ScaleColor8to5((unsigned char)
-            ScaleQuantumToChar(GetRedPixelComponent(p))) << 11) | (ScaleColor8to6((unsigned char)
-            ScaleQuantumToChar(GetGreenPixelComponent(p))) << 5) | (ScaleColor8to5(
-            (unsigned char) ScaleQuantumToChar((unsigned char) GetBluePixelComponent(p)) << 0)));
+            ScaleQuantumToChar(GetPixelRed(image,p))) << 11) | (ScaleColor8to6(
+            (unsigned char) ScaleQuantumToChar(GetPixelGreen(image,p))) << 5) |
+            (ScaleColor8to5((unsigned char) ScaleQuantumToChar((unsigned char)
+            GetPixelBlue(image,p)) << 0)));
           *q++=(unsigned char)(word & 0xff);
           *q++=(unsigned char)(word >> 8);
-          p++;
+          p+=GetPixelChannels(image);
         }
-        for (x=2L*image->columns; x < (ssize_t) bytes_per_line; x++)
+        for (x=(ssize_t) (2*image->columns); x < (ssize_t) bytes_per_line; x++)
           *q++=0x00;
         status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
-                image->rows);
+          image->rows);
         if (status == MagickFalse)
           break;
       }
@@ -1175,24 +1181,24 @@ static MagickBooleanType WriteDIBImage(const ImageInfo *image_info,Image *image)
       */
       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;
         q=pixels+(image->rows-y-1)*bytes_per_line;
         for (x=0; x < (ssize_t) image->columns; x++)
         {
-          *q++=ScaleQuantumToChar(GetBluePixelComponent(p));
-          *q++=ScaleQuantumToChar(GetGreenPixelComponent(p));
-          *q++=ScaleQuantumToChar(GetRedPixelComponent(p));
-          if (image->matte != MagickFalse)
-            *q++=ScaleQuantumToChar(GetOpacityPixelComponent(p));
-          p++;
+          *q++=ScaleQuantumToChar(GetPixelBlue(image,p));
+          *q++=ScaleQuantumToChar(GetPixelGreen(image,p));
+          *q++=ScaleQuantumToChar(GetPixelRed(image,p));
+          if (image->alpha_trait == BlendPixelTrait)
+            *q++=ScaleQuantumToChar(GetPixelAlpha(image,p));
+          p+=GetPixelChannels(image);
         }
         if (dib_info.bits_per_pixel == 24)
-          for (x=3L*image->columns; x < (ssize_t) bytes_per_line; x++)
+          for (x=(ssize_t) (3*image->columns); x < (ssize_t) bytes_per_line; x++)
             *q++=0x00;
         status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
-                image->rows);
+          image->rows);
         if (status == MagickFalse)
           break;
       }
@@ -1225,17 +1231,17 @@ static MagickBooleanType WriteDIBImage(const ImageInfo *image_info,Image *image)
   /*
     Write DIB header.
   */
-  (void) WriteBlobLSBLong(image,dib_info.size);
-  (void) WriteBlobLSBLong(image,(size_t) dib_info.width);
+  (void) WriteBlobLSBLong(image,(unsigned int) dib_info.size);
+  (void) WriteBlobLSBLong(image,dib_info.width);
   (void) WriteBlobLSBLong(image,(unsigned short) dib_info.height);
   (void) WriteBlobLSBShort(image,(unsigned short) dib_info.planes);
   (void) WriteBlobLSBShort(image,dib_info.bits_per_pixel);
-  (void) WriteBlobLSBLong(image,dib_info.compression);
-  (void) WriteBlobLSBLong(image,dib_info.image_size);
-  (void) WriteBlobLSBLong(image,dib_info.x_pixels);
-  (void) WriteBlobLSBLong(image,dib_info.y_pixels);
-  (void) WriteBlobLSBLong(image,dib_info.number_colors);
-  (void) WriteBlobLSBLong(image,dib_info.colors_important);
+  (void) WriteBlobLSBLong(image,(unsigned int) dib_info.compression);
+  (void) WriteBlobLSBLong(image,(unsigned int) dib_info.image_size);
+  (void) WriteBlobLSBLong(image,(unsigned int) dib_info.x_pixels);
+  (void) WriteBlobLSBLong(image,(unsigned int) dib_info.y_pixels);
+  (void) WriteBlobLSBLong(image,(unsigned int) dib_info.number_colors);
+  (void) WriteBlobLSBLong(image,(unsigned int) dib_info.colors_important);
   if (image->storage_class == PseudoClass)
     {
       if (dib_info.bits_per_pixel <= 8)