]> granicus.if.org Git - imagemagick/blob - coders/dib.c
...
[imagemagick] / coders / dib.c
1 /*
2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3 %                                                                             %
4 %                                                                             %
5 %                                                                             %
6 %                            DDDD   IIIII  BBBB                               %
7 %                            D   D    I    B   B                              %
8 %                            D   D    I    BBBB                               %
9 %                            D   D    I    B   B                              %
10 %                            DDDD   IIIII  BBBB                               %
11 %                                                                             %
12 %                                                                             %
13 %                   Read/Write Windows DIB Image Format                       %
14 %                                                                             %
15 %                              Software Design                                %
16 %                                   Cristy                                    %
17 %                                 July 1992                                   %
18 %                                                                             %
19 %                                                                             %
20 %  Copyright 1999-2018 ImageMagick Studio LLC, a non-profit organization      %
21 %  dedicated to making software imaging solutions freely available.           %
22 %                                                                             %
23 %  You may not use this file except in compliance with the License.  You may  %
24 %  obtain a copy of the License at                                            %
25 %                                                                             %
26 %    https://www.imagemagick.org/script/license.php                           %
27 %                                                                             %
28 %  Unless required by applicable law or agreed to in writing, software        %
29 %  distributed under the License is distributed on an "AS IS" BASIS,          %
30 %  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.   %
31 %  See the License for the specific language governing permissions and        %
32 %  limitations under the License.                                             %
33 %                                                                             %
34 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
35 %
36 %
37 */
38 \f
39 /*
40   Include declarations.
41 */
42 #include "MagickCore/studio.h"
43 #include "MagickCore/attribute.h"
44 #include "MagickCore/blob.h"
45 #include "MagickCore/blob-private.h"
46 #include "MagickCore/cache.h"
47 #include "MagickCore/color.h"
48 #include "MagickCore/color-private.h"
49 #include "MagickCore/colormap.h"
50 #include "MagickCore/colormap-private.h"
51 #include "MagickCore/colorspace.h"
52 #include "MagickCore/colorspace-private.h"
53 #include "MagickCore/draw.h"
54 #include "MagickCore/exception.h"
55 #include "MagickCore/exception-private.h"
56 #include "MagickCore/geometry.h"
57 #include "MagickCore/image.h"
58 #include "MagickCore/image-private.h"
59 #include "MagickCore/list.h"
60 #include "MagickCore/log.h"
61 #include "MagickCore/magick.h"
62 #include "MagickCore/memory_.h"
63 #include "MagickCore/monitor.h"
64 #include "MagickCore/monitor-private.h"
65 #include "MagickCore/pixel-accessor.h"
66 #include "MagickCore/quantum-private.h"
67 #include "MagickCore/static.h"
68 #include "MagickCore/string_.h"
69 #include "MagickCore/module.h"
70 #include "MagickCore/transform.h"
71 \f
72 /*
73   Typedef declarations.
74 */
75 typedef struct _DIBInfo
76 {
77   size_t
78     size;
79
80   ssize_t
81     width,
82     height;
83
84   unsigned short
85     planes,
86     bits_per_pixel;
87
88   size_t
89     compression,
90     image_size,
91     x_pixels,
92     y_pixels,
93     number_colors,
94     red_mask,
95     green_mask,
96     blue_mask,
97     alpha_mask,
98     colors_important;
99
100   ssize_t
101     colorspace;
102
103   PointInfo
104     red_primary,
105     green_primary,
106     blue_primary,
107     gamma_scale;
108 } DIBInfo;
109 \f
110 /*
111   Forward declarations.
112 */
113 static MagickBooleanType
114   WriteDIBImage(const ImageInfo *,Image *,ExceptionInfo *);
115 \f
116 /*
117 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
118 %                                                                             %
119 %                                                                             %
120 %                                                                             %
121 %   D e c o d e I m a g e                                                     %
122 %                                                                             %
123 %                                                                             %
124 %                                                                             %
125 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
126 %
127 %  DecodeImage unpacks the packed image pixels into runlength-encoded
128 %  pixel packets.
129 %
130 %  The format of the DecodeImage method is:
131 %
132 %      MagickBooleanType DecodeImage(Image *image,
133 %        const MagickBooleanType compression,unsigned char *pixels,
134 %        const size_t number_pixels)
135 %
136 %  A description of each parameter follows:
137 %
138 %    o image: the address of a structure of type Image.
139 %
140 %    o compression:  A value of 1 means the compressed pixels are runlength
141 %      encoded for a 256-color bitmap.  A value of 2 means a 16-color bitmap.
142 %
143 %    o pixels:  The address of a byte (8 bits) array of pixel data created by
144 %      the decoding process.
145 %
146 %    o number_pixels:  the number of pixels.
147 %
148 */
149 static MagickBooleanType DecodeImage(Image *image,
150   const MagickBooleanType compression,unsigned char *pixels,
151   const size_t number_pixels)
152 {
153 #if !defined(MAGICKCORE_WINDOWS_SUPPORT) || defined(__MINGW32__)
154 #define BI_RGB  0
155 #define BI_RLE8  1
156 #define BI_RLE4  2
157 #define BI_BITFIELDS  3
158 #undef BI_JPEG
159 #define BI_JPEG  4
160 #undef BI_PNG
161 #define BI_PNG  5
162 #endif
163
164   int
165     byte,
166     count;
167
168   ssize_t
169     y;
170
171   register ssize_t
172     i,
173     x;
174
175   register unsigned char
176     *p,
177     *q;
178
179   assert(image != (Image *) NULL);
180   assert(image->signature == MagickCoreSignature);
181   if (image->debug != MagickFalse)
182     (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
183   assert(pixels != (unsigned char *) NULL);
184   (void) ResetMagickMemory(pixels,0,number_pixels*sizeof(*pixels));
185   byte=0;
186   x=0;
187   p=pixels;
188   q=pixels+number_pixels;
189   for (y=0; y < (ssize_t) image->rows; )
190   {
191     if ((p < pixels) || (p >= q))
192       break;
193     count=ReadBlobByte(image);
194     if (count == EOF)
195       break;
196     if (count > 0)
197       {
198         count=(int) MagickMin((size_t) count,(size_t) (q-p));
199         /*
200           Encoded mode.
201         */
202         byte=ReadBlobByte(image);
203         if (byte == EOF)
204           break;
205         if (compression == BI_RLE8)
206           {
207             for (i=0; i < count; i++)
208               *p++=(unsigned char) byte;
209           }
210         else
211           {
212             for (i=0; i < count; i++)
213               *p++=(unsigned char)
214                 ((i & 0x01) != 0 ? (byte & 0x0f) : ((byte >> 4) & 0x0f));
215           }
216         x+=count;
217       }
218     else
219       {
220         /*
221           Escape mode.
222         */
223         count=ReadBlobByte(image);
224         if (count == 0x01)
225           return(MagickTrue);
226         switch (count)
227         {
228           case 0x00:
229           {
230             /*
231               End of line.
232             */
233             x=0;
234             y++;
235             p=pixels+y*image->columns;
236             break;
237           }
238           case 0x02:
239           {
240             /*
241               Delta mode.
242             */
243             x+=ReadBlobByte(image);
244             y+=ReadBlobByte(image);
245             p=pixels+y*image->columns+x;
246             break;
247           }
248           default:
249           {
250             /*
251               Absolute mode.
252             */
253             count=(int) MagickMin((size_t) count,(size_t) (q-p));
254             if (compression == BI_RLE8)
255               for (i=0; i < count; i++)
256               {
257                 byte=ReadBlobByte(image);
258                 if (byte == EOF)
259                   break;
260                 *p++=(unsigned char) byte;
261               }
262             else
263               for (i=0; i < count; i++)
264               {
265                 if ((i & 0x01) == 0)
266                   {
267                     byte=ReadBlobByte(image);
268                     if (byte == EOF)
269                       break;
270                   }
271                 *p++=(unsigned char)
272                   ((i & 0x01) != 0 ? (byte & 0x0f) : ((byte >> 4) & 0x0f));
273               }
274             x+=count;
275             /*
276               Read pad byte.
277             */
278             if (compression == BI_RLE8)
279               {
280                 if ((count & 0x01) != 0)
281                   if (ReadBlobByte(image) == EOF)
282                     break;
283               }
284             else
285               if (((count & 0x03) == 1) || ((count & 0x03) == 2))
286                 if (ReadBlobByte(image) == EOF)
287                   break;
288             break;
289           }
290         }
291       }
292     if (SetImageProgress(image,LoadImageTag,y,image->rows) == MagickFalse)
293       break;
294   }
295   (void) ReadBlobByte(image);  /* end of line */
296   (void) ReadBlobByte(image);
297   return(MagickTrue);
298 }
299 \f
300 /*
301 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
302 %                                                                             %
303 %                                                                             %
304 %                                                                             %
305 %   E n c o d e I m a g e                                                     %
306 %                                                                             %
307 %                                                                             %
308 %                                                                             %
309 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
310 %
311 %  EncodeImage compresses pixels using a runlength encoded format.
312 %
313 %  The format of the EncodeImage method is:
314 %
315 %    static MagickBooleanType EncodeImage(Image *image,
316 %      const size_t bytes_per_line,const unsigned char *pixels,
317 %      unsigned char *compressed_pixels)
318 %
319 %  A description of each parameter follows:
320 %
321 %    o image:  The image.
322 %
323 %    o bytes_per_line: the number of bytes in a scanline of compressed pixels
324 %
325 %    o pixels:  The address of a byte (8 bits) array of pixel data created by
326 %      the compression process.
327 %
328 %    o compressed_pixels:  The address of a byte (8 bits) array of compressed
329 %      pixel data.
330 %
331 */
332 static size_t EncodeImage(Image *image,const size_t bytes_per_line,
333   const unsigned char *pixels,unsigned char *compressed_pixels)
334 {
335   ssize_t
336     y;
337
338   register const unsigned char
339     *p;
340
341   register ssize_t
342     i,
343     x;
344
345   register unsigned char
346     *q;
347
348   /*
349     Runlength encode pixels.
350   */
351   assert(image != (Image *) NULL);
352   assert(image->signature == MagickCoreSignature);
353   if (image->debug != MagickFalse)
354     (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
355   assert(pixels != (const unsigned char *) NULL);
356   assert(compressed_pixels != (unsigned char *) NULL);
357   p=pixels;
358   q=compressed_pixels;
359   i=0;
360   for (y=0; y < (ssize_t) image->rows; y++)
361   {
362     for (x=0; x < (ssize_t) bytes_per_line; x+=i)
363     {
364       /*
365         Determine runlength.
366       */
367       for (i=1; ((x+i) < (ssize_t) bytes_per_line); i++)
368         if ((*(p+i) != *p) || (i == 255))
369           break;
370       *q++=(unsigned char) i;
371       *q++=(*p);
372       p+=i;
373     }
374     /*
375       End of line.
376     */
377     *q++=0x00;
378     *q++=0x00;
379     if (SetImageProgress(image,LoadImageTag,y,image->rows) == MagickFalse)
380       break;
381   }
382   /*
383     End of bitmap.
384   */
385   *q++=0;
386   *q++=0x01;
387   return((size_t) (q-compressed_pixels));
388 }
389 \f
390 /*
391 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
392 %                                                                             %
393 %                                                                             %
394 %                                                                             %
395 %   I s D I B                                                                 %
396 %                                                                             %
397 %                                                                             %
398 %                                                                             %
399 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
400 %
401 %  IsDIB() returns MagickTrue if the image format type, identified by the
402 %  magick string, is DIB.
403 %
404 %  The format of the IsDIB method is:
405 %
406 %      MagickBooleanType IsDIB(const unsigned char *magick,const size_t length)
407 %
408 %  A description of each parameter follows:
409 %
410 %    o magick: compare image format pattern against these bytes.
411 %
412 %    o length: Specifies the length of the magick string.
413 %
414 */
415 static MagickBooleanType IsDIB(const unsigned char *magick,const size_t length)
416 {
417   if (length < 2)
418     return(MagickFalse);
419   if (memcmp(magick,"\050\000",2) == 0)
420     return(MagickTrue);
421   return(MagickFalse);
422 }
423 \f
424 /*
425 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
426 %                                                                             %
427 %                                                                             %
428 %                                                                             %
429 %   R e a d D I B I m a g e                                                   %
430 %                                                                             %
431 %                                                                             %
432 %                                                                             %
433 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
434 %
435 %  ReadDIBImage() reads a Microsoft Windows bitmap image file and
436 %  returns it.  It allocates the memory necessary for the new Image structure
437 %  and returns a pointer to the new image.
438 %
439 %  The format of the ReadDIBImage method is:
440 %
441 %      image=ReadDIBImage(image_info)
442 %
443 %  A description of each parameter follows:
444 %
445 %    o image_info: the image info.
446 %
447 %    o exception: return any errors or warnings in this structure.
448 %
449 */
450 static Image *ReadDIBImage(const ImageInfo *image_info,ExceptionInfo *exception)
451 {
452   DIBInfo
453     dib_info;
454
455   Image
456     *image;
457
458   MagickBooleanType
459     status;
460
461   MemoryInfo
462     *pixel_info;
463
464   Quantum
465     index;
466
467   register ssize_t
468     x;
469
470   register Quantum
471     *q;
472
473   register ssize_t
474     i;
475
476   register unsigned char
477     *p;
478
479   size_t
480     bytes_per_line,
481     length;
482
483   ssize_t
484     bit,
485     count,
486     y;
487
488
489   unsigned char
490     *pixels;
491
492   /*
493     Open image file.
494   */
495   assert(image_info != (const ImageInfo *) NULL);
496   assert(image_info->signature == MagickCoreSignature);
497   if (image_info->debug != MagickFalse)
498     (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
499       image_info->filename);
500   assert(exception != (ExceptionInfo *) NULL);
501   assert(exception->signature == MagickCoreSignature);
502   image=AcquireImage(image_info,exception);
503   status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception);
504   if (status == MagickFalse)
505     {
506       image=DestroyImageList(image);
507       return((Image *) NULL);
508     }
509   /*
510     Determine if this a DIB file.
511   */
512   (void) ResetMagickMemory(&dib_info,0,sizeof(dib_info));
513   dib_info.size=ReadBlobLSBLong(image);
514   if (dib_info.size != 40)
515     ThrowReaderException(CorruptImageError,"ImproperImageHeader");
516   /*
517     Microsoft Windows 3.X DIB image file.
518   */
519   dib_info.width=ReadBlobLSBSignedLong(image);
520   dib_info.height=ReadBlobLSBSignedLong(image);
521   dib_info.planes=ReadBlobLSBShort(image);
522   dib_info.bits_per_pixel=ReadBlobLSBShort(image);
523   if (dib_info.bits_per_pixel > 32)
524     ThrowReaderException(CorruptImageError,"ImproperImageHeader");
525   dib_info.compression=ReadBlobLSBLong(image);
526   dib_info.image_size=ReadBlobLSBLong(image);
527   dib_info.x_pixels=ReadBlobLSBLong(image);
528   dib_info.y_pixels=ReadBlobLSBLong(image);
529   dib_info.number_colors=ReadBlobLSBLong(image);
530   dib_info.colors_important=ReadBlobLSBLong(image);
531   if ((dib_info.bits_per_pixel != 1) && (dib_info.bits_per_pixel != 4) &&
532       (dib_info.bits_per_pixel != 8) && (dib_info.bits_per_pixel != 16) &&
533       (dib_info.bits_per_pixel != 24) && (dib_info.bits_per_pixel != 32))
534     ThrowReaderException(CorruptImageError,"ImproperImageHeader");
535   if ((dib_info.compression == BI_BITFIELDS) &&
536       ((dib_info.bits_per_pixel == 16) || (dib_info.bits_per_pixel == 32)))
537     {
538       dib_info.red_mask=ReadBlobLSBLong(image);
539       dib_info.green_mask=ReadBlobLSBLong(image);
540       dib_info.blue_mask=ReadBlobLSBLong(image);
541     }
542   if (EOFBlob(image) != MagickFalse)
543     ThrowReaderException(CorruptImageError,"UnexpectedEndOfFile");
544   if (dib_info.width <= 0)
545     ThrowReaderException(CorruptImageError,"NegativeOrZeroImageSize");
546   if (dib_info.height == 0)
547     ThrowReaderException(CorruptImageError,"NegativeOrZeroImageSize");
548   if (dib_info.planes != 1)
549     ThrowReaderException(CorruptImageError,"StaticPlanesValueNotEqualToOne");
550   if ((dib_info.bits_per_pixel != 1) && (dib_info.bits_per_pixel != 4) &&
551       (dib_info.bits_per_pixel != 8) && (dib_info.bits_per_pixel != 16) &&
552       (dib_info.bits_per_pixel != 24) && (dib_info.bits_per_pixel != 32))
553     ThrowReaderException(CorruptImageError,"UnrecognizedBitsPerPixel");
554   if (dib_info.bits_per_pixel < 16 &&
555       dib_info.number_colors > (size_t) (1UL << dib_info.bits_per_pixel))
556     ThrowReaderException(CorruptImageError,"UnrecognizedNumberOfColors");
557   if ((dib_info.compression == 1) && (dib_info.bits_per_pixel != 8))
558     ThrowReaderException(CorruptImageError,"UnrecognizedBitsPerPixel");
559   if ((dib_info.compression == 2) && (dib_info.bits_per_pixel != 4))
560     ThrowReaderException(CorruptImageError,"UnrecognizedBitsPerPixel");
561   if ((dib_info.compression == 3) && (dib_info.bits_per_pixel < 16))
562     ThrowReaderException(CorruptImageError,"UnrecognizedBitsPerPixel");
563   switch (dib_info.compression)
564   {
565     case BI_RGB:
566     case BI_RLE8:
567     case BI_RLE4:
568     case BI_BITFIELDS:
569       break;
570     case BI_JPEG:
571       ThrowReaderException(CoderError,"JPEGCompressNotSupported");
572     case BI_PNG:
573       ThrowReaderException(CoderError,"PNGCompressNotSupported");
574     default:
575       ThrowReaderException(CorruptImageError,"UnrecognizedImageCompression");
576   }
577   image->columns=(size_t) MagickAbsoluteValue(dib_info.width);
578   image->rows=(size_t) MagickAbsoluteValue(dib_info.height);
579   image->depth=8;
580   image->alpha_trait=dib_info.bits_per_pixel == 32 ? BlendPixelTrait :
581     UndefinedPixelTrait;
582   if ((dib_info.number_colors > 256) || (dib_info.colors_important > 256))
583     ThrowReaderException(CorruptImageError,"ImproperImageHeader");
584   if ((dib_info.number_colors != 0) || (dib_info.bits_per_pixel < 16))
585     {
586       size_t
587         one;
588
589       image->storage_class=PseudoClass;
590       image->colors=dib_info.number_colors;
591       one=1;
592       if (image->colors == 0)
593         image->colors=one << dib_info.bits_per_pixel;
594     }
595   if (image_info->size)
596     {
597       RectangleInfo
598         geometry;
599
600       MagickStatusType
601         flags;
602
603       flags=ParseAbsoluteGeometry(image_info->size,&geometry);
604       if (flags & WidthValue)
605         if ((geometry.width != 0) && (geometry.width < image->columns))
606           image->columns=geometry.width;
607       if (flags & HeightValue)
608         if ((geometry.height != 0) && (geometry.height < image->rows))
609           image->rows=geometry.height;
610     }
611   status=SetImageExtent(image,image->columns,image->rows,exception);
612   if (status == MagickFalse)
613     return(DestroyImageList(image));
614   if (image->storage_class == PseudoClass)
615     {
616       size_t
617         packet_size;
618
619       unsigned char
620         *dib_colormap;
621
622       /*
623         Read DIB raster colormap.
624       */
625       if (AcquireImageColormap(image,image->colors,exception) == MagickFalse)
626         ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
627       length=(size_t) image->colors;
628       dib_colormap=(unsigned char *) AcquireQuantumMemory(length,
629         4*sizeof(*dib_colormap));
630       if (dib_colormap == (unsigned char *) NULL)
631         ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
632       packet_size=4;
633       count=ReadBlob(image,packet_size*image->colors,dib_colormap);
634       if (count != (ssize_t) (packet_size*image->colors))
635         {
636           dib_colormap=(unsigned char *) RelinquishMagickMemory(dib_colormap);
637           ThrowReaderException(CorruptImageError,"InsufficientImageDataInFile");
638         }
639       p=dib_colormap;
640       for (i=0; i < (ssize_t) image->colors; i++)
641       {
642         image->colormap[i].blue=ScaleCharToQuantum(*p++);
643         image->colormap[i].green=ScaleCharToQuantum(*p++);
644         image->colormap[i].red=ScaleCharToQuantum(*p++);
645         if (packet_size == 4)
646           p++;
647       }
648       dib_colormap=(unsigned char *) RelinquishMagickMemory(dib_colormap);
649     }
650   /*
651     Read image data.
652   */
653   if (dib_info.compression == BI_RLE4)
654     dib_info.bits_per_pixel<<=1;
655   bytes_per_line=4*((image->columns*dib_info.bits_per_pixel+31)/32);
656   length=bytes_per_line*image->rows;
657   pixel_info=AcquireVirtualMemory((size_t) image->rows,MagickMax(
658     bytes_per_line,image->columns+256UL)*sizeof(*pixels));
659   if (pixel_info == (MemoryInfo *) NULL)
660     ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
661   pixels=(unsigned char *) GetVirtualMemoryBlob(pixel_info);
662   if ((dib_info.compression == BI_RGB) ||
663       (dib_info.compression == BI_BITFIELDS))
664     {
665       count=ReadBlob(image,length,pixels);
666       if (count != (ssize_t) (length))
667         {
668           pixel_info=RelinquishVirtualMemory(pixel_info);
669           ThrowReaderException(CorruptImageError,"InsufficientImageDataInFile");
670         }
671     }
672   else
673     {
674       /*
675         Convert run-length encoded raster pixels.
676       */
677       status=DecodeImage(image,dib_info.compression ? MagickTrue : MagickFalse,
678         pixels,image->columns*image->rows);
679       if (status == MagickFalse)
680         {
681           pixel_info=RelinquishVirtualMemory(pixel_info);
682           ThrowReaderException(CorruptImageError,
683             "UnableToRunlengthDecodeImage");
684         }
685     }
686   /*
687     Initialize image structure.
688   */
689   image->units=PixelsPerCentimeterResolution;
690   image->resolution.x=(double) dib_info.x_pixels/100.0;
691   image->resolution.y=(double) dib_info.y_pixels/100.0;
692   /*
693     Convert DIB raster image to pixel packets.
694   */
695   switch (dib_info.bits_per_pixel)
696   {
697     case 1:
698     {
699       /*
700         Convert bitmap scanline.
701       */
702       for (y=(ssize_t) image->rows-1; y >= 0; y--)
703       {
704         p=pixels+(image->rows-y-1)*bytes_per_line;
705         q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
706         if (q == (Quantum *) NULL)
707           break;
708         for (x=0; x < ((ssize_t) image->columns-7); x+=8)
709         {
710           for (bit=0; bit < 8; bit++)
711           {
712             index=(Quantum) ((*p) & (0x80 >> bit) ? 0x01 : 0x00);
713             SetPixelIndex(image,index,q);
714             q+=GetPixelChannels(image);
715           }
716           p++;
717         }
718         if ((image->columns % 8) != 0)
719           {
720             for (bit=0; bit < (ssize_t) (image->columns % 8); bit++)
721             {
722               index=(Quantum) ((*p) & (0x80 >> bit) ? 0x01 : 0x00);
723               SetPixelIndex(image,index,q);
724               q+=GetPixelChannels(image);
725             }
726             p++;
727           }
728         if (SyncAuthenticPixels(image,exception) == MagickFalse)
729           break;
730         if (image->previous == (Image *) NULL)
731           {
732             status=SetImageProgress(image,LoadImageTag,image->rows-y-1,
733               image->rows);
734             if (status == MagickFalse)
735               break;
736           }
737       }
738       (void) SyncImage(image,exception);
739       break;
740     }
741     case 4:
742     {
743       /*
744         Convert PseudoColor scanline.
745       */
746       for (y=(ssize_t) image->rows-1; y >= 0; y--)
747       {
748         p=pixels+(image->rows-y-1)*bytes_per_line;
749         q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
750         if (q == (Quantum *) NULL)
751           break;
752         for (x=0; x < ((ssize_t) image->columns-1); x+=2)
753         {
754           index=ConstrainColormapIndex(image,(*p >> 4) & 0xf,exception);
755           SetPixelIndex(image,index,q);
756           q+=GetPixelChannels(image);
757           index=ConstrainColormapIndex(image,*p & 0xf,exception);
758           SetPixelIndex(image,index,q);
759           p++;
760           q+=GetPixelChannels(image);
761         }
762         if ((image->columns % 2) != 0)
763           {
764             index=ConstrainColormapIndex(image,(*p >> 4) & 0xf,exception);
765             SetPixelIndex(image,index,q);
766             q+=GetPixelChannels(image);
767             p++;
768           }
769         if (SyncAuthenticPixels(image,exception) == MagickFalse)
770           break;
771         if (image->previous == (Image *) NULL)
772           {
773             status=SetImageProgress(image,LoadImageTag,image->rows-y-1,
774               image->rows);
775             if (status == MagickFalse)
776               break;
777           }
778       }
779       (void) SyncImage(image,exception);
780       break;
781     }
782     case 8:
783     {
784       /*
785         Convert PseudoColor scanline.
786       */
787       if ((dib_info.compression == BI_RLE8) ||
788           (dib_info.compression == BI_RLE4))
789         bytes_per_line=image->columns;
790       for (y=(ssize_t) image->rows-1; y >= 0; y--)
791       {
792         p=pixels+(image->rows-y-1)*bytes_per_line;
793         q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
794         if (q == (Quantum *) NULL)
795           break;
796         for (x=0; x < (ssize_t) image->columns; x++)
797         {
798           index=ConstrainColormapIndex(image,*p,exception);
799           SetPixelIndex(image,index,q);
800           p++;
801           q+=GetPixelChannels(image);
802         }
803         if (SyncAuthenticPixels(image,exception) == MagickFalse)
804           break;
805         if (image->previous == (Image *) NULL)
806           {
807             status=SetImageProgress(image,LoadImageTag,image->rows-y-1,
808               image->rows);
809             if (status == MagickFalse)
810               break;
811           }
812       }
813       (void) SyncImage(image,exception);
814       break;
815     }
816     case 16:
817     {
818       unsigned short
819         word;
820
821       /*
822         Convert PseudoColor scanline.
823       */
824       image->storage_class=DirectClass;
825       if (dib_info.compression == BI_RLE8)
826         bytes_per_line=2*image->columns;
827       for (y=(ssize_t) image->rows-1; y >= 0; y--)
828       {
829         p=pixels+(image->rows-y-1)*bytes_per_line;
830         q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
831         if (q == (Quantum *) NULL)
832           break;
833         for (x=0; x < (ssize_t) image->columns; x++)
834         {
835           word=(*p++);
836           word|=(*p++ << 8);
837           if (dib_info.red_mask == 0)
838             {
839               SetPixelRed(image,ScaleCharToQuantum(ScaleColor5to8(
840                 (unsigned char) ((word >> 10) & 0x1f))),q);
841               SetPixelGreen(image,ScaleCharToQuantum(ScaleColor5to8(
842                 (unsigned char) ((word >> 5) & 0x1f))),q);
843               SetPixelBlue(image,ScaleCharToQuantum(ScaleColor5to8(
844                 (unsigned char) (word & 0x1f))),q);
845             }
846           else
847             {
848               SetPixelRed(image,ScaleCharToQuantum(ScaleColor5to8(
849                 (unsigned char) ((word >> 11) & 0x1f))),q);
850               SetPixelGreen(image,ScaleCharToQuantum(ScaleColor6to8(
851                 (unsigned char) ((word >> 5) & 0x3f))),q);
852               SetPixelBlue(image,ScaleCharToQuantum(ScaleColor5to8(
853                 (unsigned char) (word & 0x1f))),q);
854             }
855           q+=GetPixelChannels(image);
856         }
857         if (SyncAuthenticPixels(image,exception) == MagickFalse)
858           break;
859         if (image->previous == (Image *) NULL)
860           {
861             status=SetImageProgress(image,LoadImageTag,image->rows-y-1,
862               image->rows);
863             if (status == MagickFalse)
864               break;
865           }
866       }
867       break;
868     }
869     case 24:
870     case 32:
871     {
872       /*
873         Convert DirectColor scanline.
874       */
875       for (y=(ssize_t) image->rows-1; y >= 0; y--)
876       {
877         p=pixels+(image->rows-y-1)*bytes_per_line;
878         q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
879         if (q == (Quantum *) NULL)
880           break;
881         for (x=0; x < (ssize_t) image->columns; x++)
882         {
883           SetPixelBlue(image,ScaleCharToQuantum(*p++),q);
884           SetPixelGreen(image,ScaleCharToQuantum(*p++),q);
885           SetPixelRed(image,ScaleCharToQuantum(*p++),q);
886           if (image->alpha_trait != UndefinedPixelTrait)
887             SetPixelAlpha(image,ScaleCharToQuantum(*p++),q);
888           q+=GetPixelChannels(image);
889         }
890         if (SyncAuthenticPixels(image,exception) == MagickFalse)
891           break;
892         if (image->previous == (Image *) NULL)
893           {
894             status=SetImageProgress(image,LoadImageTag,image->rows-y-1,
895               image->rows);
896             if (status == MagickFalse)
897               break;
898           }
899       }
900       break;
901     }
902     default:
903       pixel_info=RelinquishVirtualMemory(pixel_info);
904       ThrowReaderException(CorruptImageError,"ImproperImageHeader");
905   }
906   pixel_info=RelinquishVirtualMemory(pixel_info);
907   if (EOFBlob(image) != MagickFalse)
908     ThrowFileException(exception,CorruptImageError,"UnexpectedEndOfFile",
909       image->filename);
910   if (dib_info.height < 0)
911     {
912       Image
913         *flipped_image;
914
915       /*
916         Correct image orientation.
917       */
918       flipped_image=FlipImage(image,exception);
919       if (flipped_image != (Image *) NULL)
920         {
921           DuplicateBlob(flipped_image,image);
922           image=DestroyImage(image);
923           image=flipped_image;
924         }
925     }
926   (void) CloseBlob(image);
927   return(GetFirstImageInList(image));
928 }
929 \f
930 /*
931 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
932 %                                                                             %
933 %                                                                             %
934 %                                                                             %
935 %   R e g i s t e r D I B I m a g e                                           %
936 %                                                                             %
937 %                                                                             %
938 %                                                                             %
939 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
940 %
941 %  RegisterDIBImage() adds attributes for the DIB image format to
942 %  the list of supported formats.  The attributes include the image format
943 %  tag, a method to read and/or write the format, whether the format
944 %  supports the saving of more than one frame to the same file or blob,
945 %  whether the format supports native in-memory I/O, and a brief
946 %  description of the format.
947 %
948 %  The format of the RegisterDIBImage method is:
949 %
950 %      size_t RegisterDIBImage(void)
951 %
952 */
953 ModuleExport size_t RegisterDIBImage(void)
954 {
955   MagickInfo
956     *entry;
957
958   entry=AcquireMagickInfo("DIB","DIB",
959     "Microsoft Windows 3.X Packed Device-Independent Bitmap");
960   entry->decoder=(DecodeImageHandler *) ReadDIBImage;
961   entry->encoder=(EncodeImageHandler *) WriteDIBImage;
962   entry->magick=(IsImageFormatHandler *) IsDIB;
963   entry->flags^=CoderAdjoinFlag;
964   entry->flags|=CoderStealthFlag;
965   (void) RegisterMagickInfo(entry);
966   return(MagickImageCoderSignature);
967 }
968 \f
969 /*
970 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
971 %                                                                             %
972 %                                                                             %
973 %                                                                             %
974 %   U n r e g i s t e r D I B I m a g e                                       %
975 %                                                                             %
976 %                                                                             %
977 %                                                                             %
978 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
979 %
980 %  UnregisterDIBImage() removes format registrations made by the
981 %  DIB module from the list of supported formats.
982 %
983 %  The format of the UnregisterDIBImage method is:
984 %
985 %      UnregisterDIBImage(void)
986 %
987 */
988 ModuleExport void UnregisterDIBImage(void)
989 {
990   (void) UnregisterMagickInfo("DIB");
991 }
992 \f
993 /*
994 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
995 %                                                                             %
996 %                                                                             %
997 %                                                                             %
998 %   W r i t e D I B I m a g e                                                 %
999 %                                                                             %
1000 %                                                                             %
1001 %                                                                             %
1002 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1003 %
1004 %  WriteDIBImage() writes an image in Microsoft Windows bitmap encoded
1005 %  image format.
1006 %
1007 %  The format of the WriteDIBImage method is:
1008 %
1009 %      MagickBooleanType WriteDIBImage(const ImageInfo *image_info,
1010 %        Image *image,ExceptionInfo *exception)
1011 %
1012 %  A description of each parameter follows.
1013 %
1014 %    o image_info: the image info.
1015 %
1016 %    o image:  The image.
1017 %
1018 %    o exception: return any errors or warnings in this structure.
1019 %
1020 */
1021 static MagickBooleanType WriteDIBImage(const ImageInfo *image_info,Image *image,
1022   ExceptionInfo *exception)
1023 {
1024   DIBInfo
1025     dib_info;
1026
1027   MagickBooleanType
1028     status;
1029
1030   register const Quantum
1031     *p;
1032
1033   register ssize_t
1034     i,
1035     x;
1036
1037   register unsigned char
1038     *q;
1039
1040   size_t
1041     bytes_per_line;
1042
1043   ssize_t
1044     y;
1045
1046   unsigned char
1047     *dib_data,
1048     *pixels;
1049
1050   /*
1051     Open output image file.
1052   */
1053   assert(image_info != (const ImageInfo *) NULL);
1054   assert(image_info->signature == MagickCoreSignature);
1055   assert(image != (Image *) NULL);
1056   assert(image->signature == MagickCoreSignature);
1057   if (image->debug != MagickFalse)
1058     (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1059   assert(exception != (ExceptionInfo *) NULL);
1060   assert(exception->signature == MagickCoreSignature);
1061   status=OpenBlob(image_info,image,WriteBinaryBlobMode,exception);
1062   if (status == MagickFalse)
1063     return(status);
1064   /*
1065     Initialize DIB raster file header.
1066   */
1067   (void) TransformImageColorspace(image,sRGBColorspace,exception);
1068   if (image->storage_class == DirectClass)
1069     {
1070       /*
1071         Full color DIB raster.
1072       */
1073       dib_info.number_colors=0;
1074       dib_info.bits_per_pixel=(unsigned short) (image->alpha_trait ? 32 : 24);
1075     }
1076   else
1077     {
1078       /*
1079         Colormapped DIB raster.
1080       */
1081       dib_info.bits_per_pixel=8;
1082       if (image_info->depth > 8)
1083         dib_info.bits_per_pixel=16;
1084       if (SetImageMonochrome(image,exception) != MagickFalse)
1085         dib_info.bits_per_pixel=1;
1086       dib_info.number_colors=(dib_info.bits_per_pixel == 16) ? 0 :
1087         (1UL << dib_info.bits_per_pixel);
1088     }
1089   bytes_per_line=4*((image->columns*dib_info.bits_per_pixel+31)/32);
1090   dib_info.size=40;
1091   dib_info.width=(ssize_t) image->columns;
1092   dib_info.height=(ssize_t) image->rows;
1093   dib_info.planes=1;
1094   dib_info.compression=(size_t) (dib_info.bits_per_pixel == 16 ?
1095     BI_BITFIELDS : BI_RGB);
1096   dib_info.image_size=bytes_per_line*image->rows;
1097   dib_info.x_pixels=75*39;
1098   dib_info.y_pixels=75*39;
1099   switch (image->units)
1100   {
1101     case UndefinedResolution:
1102     case PixelsPerInchResolution:
1103     {
1104       dib_info.x_pixels=(size_t) (100.0*image->resolution.x/2.54);
1105       dib_info.y_pixels=(size_t) (100.0*image->resolution.y/2.54);
1106       break;
1107     }
1108     case PixelsPerCentimeterResolution:
1109     {
1110       dib_info.x_pixels=(size_t) (100.0*image->resolution.x);
1111       dib_info.y_pixels=(size_t) (100.0*image->resolution.y);
1112       break;
1113     }
1114   }
1115   dib_info.colors_important=dib_info.number_colors;
1116   /*
1117     Convert MIFF to DIB raster pixels.
1118   */
1119   pixels=(unsigned char *) AcquireQuantumMemory(dib_info.image_size,
1120     sizeof(*pixels));
1121   if (pixels == (unsigned char *) NULL)
1122     ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
1123   (void) ResetMagickMemory(pixels,0,dib_info.image_size);
1124   switch (dib_info.bits_per_pixel)
1125   {
1126     case 1:
1127     {
1128       register unsigned char
1129         bit,
1130         byte;
1131
1132       /*
1133         Convert PseudoClass image to a DIB monochrome image.
1134       */
1135       for (y=0; y < (ssize_t) image->rows; y++)
1136       {
1137         p=GetVirtualPixels(image,0,y,image->columns,1,exception);
1138         if (p == (const Quantum *) NULL)
1139           break;
1140         q=pixels+(image->rows-y-1)*bytes_per_line;
1141         bit=0;
1142         byte=0;
1143         for (x=0; x < (ssize_t) image->columns; x++)
1144         {
1145           byte<<=1;
1146           byte|=GetPixelIndex(image,p) != 0 ? 0x01 : 0x00;
1147           bit++;
1148           if (bit == 8)
1149             {
1150               *q++=byte;
1151               bit=0;
1152               byte=0;
1153             }
1154            p+=GetPixelChannels(image);
1155          }
1156          if (bit != 0)
1157            {
1158              *q++=(unsigned char) (byte << (8-bit));
1159              x++;
1160            }
1161         for (x=(ssize_t) (image->columns+7)/8; x < (ssize_t) bytes_per_line; x++)
1162           *q++=0x00;
1163         status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
1164           image->rows);
1165         if (status == MagickFalse)
1166           break;
1167       }
1168       break;
1169     }
1170     case 8:
1171     {
1172       /*
1173         Convert PseudoClass packet to DIB pixel.
1174       */
1175       for (y=0; y < (ssize_t) image->rows; y++)
1176       {
1177         p=GetVirtualPixels(image,0,y,image->columns,1,exception);
1178         if (p == (const Quantum *) NULL)
1179           break;
1180         q=pixels+(image->rows-y-1)*bytes_per_line;
1181         for (x=0; x < (ssize_t) image->columns; x++)
1182         {
1183           *q++=(unsigned char) GetPixelIndex(image,p);
1184           p+=GetPixelChannels(image);
1185         }
1186         for ( ; x < (ssize_t) bytes_per_line; x++)
1187           *q++=0x00;
1188         status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
1189           image->rows);
1190         if (status == MagickFalse)
1191           break;
1192       }
1193       break;
1194     }
1195     case 16:
1196     {
1197       unsigned short
1198         word;
1199       /*
1200         Convert PseudoClass packet to DIB pixel.
1201       */
1202       for (y=0; y < (ssize_t) image->rows; y++)
1203       {
1204         p=GetVirtualPixels(image,0,y,image->columns,1,exception);
1205         if (p == (const Quantum *) NULL)
1206           break;
1207         q=pixels+(image->rows-y-1)*bytes_per_line;
1208         for (x=0; x < (ssize_t) image->columns; x++)
1209         {
1210           word=(unsigned short) ((ScaleColor8to5((unsigned char)
1211             ScaleQuantumToChar(GetPixelRed(image,p))) << 11) | (ScaleColor8to6(
1212             (unsigned char) ScaleQuantumToChar(GetPixelGreen(image,p))) << 5) |
1213             (ScaleColor8to5((unsigned char) ScaleQuantumToChar((unsigned char)
1214             GetPixelBlue(image,p)) << 0)));
1215           *q++=(unsigned char)(word & 0xff);
1216           *q++=(unsigned char)(word >> 8);
1217           p+=GetPixelChannels(image);
1218         }
1219         for (x=(ssize_t) (2*image->columns); x < (ssize_t) bytes_per_line; x++)
1220           *q++=0x00;
1221         status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
1222           image->rows);
1223         if (status == MagickFalse)
1224           break;
1225       }
1226       break;
1227     }
1228     case 24:
1229     case 32:
1230     {
1231       /*
1232         Convert DirectClass packet to DIB RGB pixel.
1233       */
1234       for (y=0; y < (ssize_t) image->rows; y++)
1235       {
1236         p=GetVirtualPixels(image,0,y,image->columns,1,exception);
1237         if (p == (const Quantum *) NULL)
1238           break;
1239         q=pixels+(image->rows-y-1)*bytes_per_line;
1240         for (x=0; x < (ssize_t) image->columns; x++)
1241         {
1242           *q++=ScaleQuantumToChar(GetPixelBlue(image,p));
1243           *q++=ScaleQuantumToChar(GetPixelGreen(image,p));
1244           *q++=ScaleQuantumToChar(GetPixelRed(image,p));
1245           if (image->alpha_trait != UndefinedPixelTrait)
1246             *q++=ScaleQuantumToChar(GetPixelAlpha(image,p));
1247           p+=GetPixelChannels(image);
1248         }
1249         if (dib_info.bits_per_pixel == 24)
1250           for (x=(ssize_t) (3*image->columns); x < (ssize_t) bytes_per_line; x++)
1251             *q++=0x00;
1252         status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
1253           image->rows);
1254         if (status == MagickFalse)
1255           break;
1256       }
1257       break;
1258     }
1259   }
1260   if (dib_info.bits_per_pixel == 8)
1261     if (image_info->compression != NoCompression)
1262       {
1263         size_t
1264           length;
1265
1266         /*
1267           Convert run-length encoded raster pixels.
1268         */
1269         length=2UL*(bytes_per_line+2UL)+2UL;
1270         dib_data=(unsigned char *) AcquireQuantumMemory(length,
1271           (image->rows+2UL)*sizeof(*dib_data));
1272         if (dib_data == (unsigned char *) NULL)
1273           {
1274             pixels=(unsigned char *) RelinquishMagickMemory(pixels);
1275             ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
1276           }
1277         dib_info.image_size=(size_t) EncodeImage(image,bytes_per_line,
1278           pixels,dib_data);
1279         pixels=(unsigned char *) RelinquishMagickMemory(pixels);
1280         pixels=dib_data;
1281         dib_info.compression = BI_RLE8;
1282       }
1283   /*
1284     Write DIB header.
1285   */
1286   (void) WriteBlobLSBLong(image,(unsigned int) dib_info.size);
1287   (void) WriteBlobLSBLong(image,dib_info.width);
1288   (void) WriteBlobLSBLong(image,(unsigned short) dib_info.height);
1289   (void) WriteBlobLSBShort(image,(unsigned short) dib_info.planes);
1290   (void) WriteBlobLSBShort(image,dib_info.bits_per_pixel);
1291   (void) WriteBlobLSBLong(image,(unsigned int) dib_info.compression);
1292   (void) WriteBlobLSBLong(image,(unsigned int) dib_info.image_size);
1293   (void) WriteBlobLSBLong(image,(unsigned int) dib_info.x_pixels);
1294   (void) WriteBlobLSBLong(image,(unsigned int) dib_info.y_pixels);
1295   (void) WriteBlobLSBLong(image,(unsigned int) dib_info.number_colors);
1296   (void) WriteBlobLSBLong(image,(unsigned int) dib_info.colors_important);
1297   if (image->storage_class == PseudoClass)
1298     {
1299       if (dib_info.bits_per_pixel <= 8)
1300         {
1301           unsigned char
1302             *dib_colormap;
1303
1304           /*
1305             Dump colormap to file.
1306           */
1307           dib_colormap=(unsigned char *) AcquireQuantumMemory((size_t)
1308             (1UL << dib_info.bits_per_pixel),4*sizeof(*dib_colormap));
1309           if (dib_colormap == (unsigned char *) NULL)
1310             ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
1311           q=dib_colormap;
1312           for (i=0; i < (ssize_t) MagickMin(image->colors,dib_info.number_colors); i++)
1313           {
1314             *q++=ScaleQuantumToChar(image->colormap[i].blue);
1315             *q++=ScaleQuantumToChar(image->colormap[i].green);
1316             *q++=ScaleQuantumToChar(image->colormap[i].red);
1317             *q++=(Quantum) 0x0;
1318           }
1319           for ( ; i < (ssize_t) (1L << dib_info.bits_per_pixel); i++)
1320           {
1321             *q++=(Quantum) 0x0;
1322             *q++=(Quantum) 0x0;
1323             *q++=(Quantum) 0x0;
1324             *q++=(Quantum) 0x0;
1325           }
1326           (void) WriteBlob(image,(size_t) (4*(1 << dib_info.bits_per_pixel)),
1327             dib_colormap);
1328           dib_colormap=(unsigned char *) RelinquishMagickMemory(dib_colormap);
1329         }
1330       else
1331         if ((dib_info.bits_per_pixel == 16) &&
1332             (dib_info.compression == BI_BITFIELDS))
1333           {
1334             (void) WriteBlobLSBLong(image,0xf800);
1335             (void) WriteBlobLSBLong(image,0x07e0);
1336             (void) WriteBlobLSBLong(image,0x001f);
1337           }
1338     }
1339   (void) WriteBlob(image,dib_info.image_size,pixels);
1340   pixels=(unsigned char *) RelinquishMagickMemory(pixels);
1341   (void) CloseBlob(image);
1342   return(MagickTrue);
1343 }