]> granicus.if.org Git - imagemagick/blob - coders/bmp.c
Horizon validity (anti-aliased) added to Plane2Cylinder
[imagemagick] / coders / bmp.c
1 /*
2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3 %                                                                             %
4 %                                                                             %
5 %                                                                             %
6 %                            BBBB   M   M  PPPP                               %
7 %                            B   B  MM MM  P   P                              %
8 %                            BBBB   M M M  PPPP                               %
9 %                            B   B  M   M  P                                  %
10 %                            BBBB   M   M  P                                  %
11 %                                                                             %
12 %                                                                             %
13 %             Read/Write Microsoft Windows Bitmap Image Format                %
14 %                                                                             %
15 %                              Software Design                                %
16 %                                John Cristy                                  %
17 %                            Glenn Randers-Pehrson                            %
18 %                               December 2001                                 %
19 %                                                                             %
20 %                                                                             %
21 %  Copyright 1999-2011 ImageMagick Studio LLC, a non-profit organization      %
22 %  dedicated to making software imaging solutions freely available.           %
23 %                                                                             %
24 %  You may not use this file except in compliance with the License.  You may  %
25 %  obtain a copy of the License at                                            %
26 %                                                                             %
27 %    http://www.imagemagick.org/script/license.php                            %
28 %                                                                             %
29 %  Unless required by applicable law or agreed to in writing, software        %
30 %  distributed under the License is distributed on an "AS IS" BASIS,          %
31 %  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.   %
32 %  See the License for the specific language governing permissions and        %
33 %  limitations under the License.                                             %
34 %                                                                             %
35 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
36 %
37 %
38 */
39 \f
40 /*
41   Include declarations.
42 */
43 #include "magick/studio.h"
44 #include "magick/blob.h"
45 #include "magick/blob-private.h"
46 #include "magick/cache.h"
47 #include "magick/colormap-private.h"
48 #include "magick/color-private.h"
49 #include "magick/colormap.h"
50 #include "magick/colorspace.h"
51 #include "magick/exception.h"
52 #include "magick/exception-private.h"
53 #include "magick/image.h"
54 #include "magick/image-private.h"
55 #include "magick/list.h"
56 #include "magick/log.h"
57 #include "magick/magick.h"
58 #include "magick/memory_.h"
59 #include "magick/monitor.h"
60 #include "magick/monitor-private.h"
61 #include "magick/profile.h"
62 #include "magick/quantum-private.h"
63 #include "magick/static.h"
64 #include "magick/string_.h"
65 #include "magick/module.h"
66 #include "magick/transform.h"
67 \f
68 /*
69   Macro definitions (from Windows wingdi.h).
70 */
71 #undef BI_JPEG
72 #define BI_JPEG  4
73 #undef BI_PNG
74 #define BI_PNG  5
75 #if !defined(MAGICKCORE_WINDOWS_SUPPORT) || defined(__MINGW32__)
76 #define BI_RGB  0
77 #define BI_RLE8  1
78 #define BI_RLE4  2
79 #define BI_BITFIELDS  3
80
81 #define LCS_CALIBRATED_RBG  0
82 #define LCS_sRGB  1
83 #define LCS_WINDOWS_COLOR_SPACE  2
84 #define PROFILE_LINKED  3
85 #define PROFILE_EMBEDDED  4
86
87 #define LCS_GM_BUSINESS  1  /* Saturation */
88 #define LCS_GM_GRAPHICS  2  /* Relative */
89 #define LCS_GM_IMAGES  4  /* Perceptual */
90 #define LCS_GM_ABS_COLORIMETRIC  8  /* Absolute */
91 #endif
92 \f
93 /*
94   Typedef declarations.
95 */
96 typedef struct _BMPInfo
97 {
98   unsigned int
99     file_size,
100     ba_offset,
101     offset_bits,
102     size;
103
104   ssize_t
105     width,
106     height;
107
108   unsigned short
109     planes,
110     bits_per_pixel;
111
112   unsigned int
113     compression,
114     image_size,
115     x_pixels,
116     y_pixels,
117     number_colors,
118     red_mask,
119     green_mask,
120     blue_mask,
121     alpha_mask,
122     colors_important;
123
124   int
125     colorspace;
126
127   PrimaryInfo
128     red_primary,
129     green_primary,
130     blue_primary,
131     gamma_scale;
132 } BMPInfo;
133 \f
134 /*
135   Forward declarations.
136 */
137 static MagickBooleanType
138   WriteBMPImage(const ImageInfo *,Image *);
139 \f
140 /*
141 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
142 %                                                                             %
143 %                                                                             %
144 %                                                                             %
145 %   D e c o d e I m a g e                                                     %
146 %                                                                             %
147 %                                                                             %
148 %                                                                             %
149 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
150 %
151 %  DecodeImage unpacks the packed image pixels into runlength-encoded
152 %  pixel packets.
153 %
154 %  The format of the DecodeImage method is:
155 %
156 %      MagickBooleanType DecodeImage(Image *image,
157 %        const size_t compression,unsigned char *pixels)
158 %
159 %  A description of each parameter follows:
160 %
161 %    o image: the address of a structure of type Image.
162 %
163 %    o compression:  Zero means uncompressed.  A value of 1 means the
164 %      compressed pixels are runlength encoded for a 256-color bitmap.
165 %      A value of 2 means a 16-color bitmap.  A value of 3 means bitfields
166 %      encoding.
167 %
168 %    o pixels:  The address of a byte (8 bits) array of pixel data created by
169 %      the decoding process.
170 %
171 */
172
173 static inline ssize_t MagickAbsoluteValue(const ssize_t x)
174 {
175   if (x < 0)
176     return(-x);
177   return(x);
178 }
179
180 static inline size_t MagickMax(const size_t x,const size_t y)
181 {
182   if (x > y)
183     return(x);
184   return(y);
185 }
186
187 static inline ssize_t MagickMin(const ssize_t x,const ssize_t y)
188 {
189   if (x < y)
190     return(x);
191   return(y);
192 }
193
194 static MagickBooleanType DecodeImage(Image *image,const size_t compression,
195   unsigned char *pixels)
196 {
197   int
198     count;
199
200   register ssize_t
201     i,
202     x;
203
204   register unsigned char
205     *p,
206     *q;
207
208   ssize_t
209     y;
210
211   unsigned char
212     byte;
213
214   assert(image != (Image *) NULL);
215   assert(image->signature == MagickSignature);
216   if (image->debug != MagickFalse)
217     (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
218   assert(pixels != (unsigned char *) NULL);
219   (void) ResetMagickMemory(pixels,0,(size_t) image->columns*image->rows*
220     sizeof(*pixels));
221   byte=0;
222   x=0;
223   p=pixels;
224   q=pixels+(size_t) image->columns*image->rows;
225   for (y=0; y < (ssize_t) image->rows; )
226   {
227     if ((p < pixels) || (p >= q))
228       break;
229     count=ReadBlobByte(image);
230     if (count == EOF)
231       break;
232     if (count != 0)
233       {
234         /*
235           Encoded mode.
236         */
237         count=MagickMin(count,(int) (q-p));
238         byte=(unsigned char) ReadBlobByte(image);
239         if (compression == BI_RLE8)
240           {
241             for (i=0; i < count; i++)
242               *p++=(unsigned char) byte;
243           }
244         else
245           {
246             for (i=0; i < count; i++)
247               *p++=(unsigned char)
248                 ((i & 0x01) != 0 ? (byte & 0x0f) : ((byte >> 4) & 0x0f));
249           }
250         x+=count;
251       }
252     else
253       {
254         /*
255           Escape mode.
256         */
257         count=ReadBlobByte(image);
258         if (count == 0x01)
259           return(MagickTrue);
260         switch (count)
261         {
262           case 0x00:
263           {
264             /*
265               End of line.
266             */
267             x=0;
268             y++;
269             p=pixels+y*image->columns;
270             break;
271           }
272           case 0x02:
273           {
274             /*
275               Delta mode.
276             */
277             x+=ReadBlobByte(image);
278             y+=ReadBlobByte(image);
279             p=pixels+y*image->columns+x;
280             break;
281           }
282           default:
283           {
284             /*
285               Absolute mode.
286             */
287             count=MagickMin(count,(int) (q-p));
288             if (compression == BI_RLE8)
289               for (i=0; i < count; i++)
290                 *p++=(unsigned char) ReadBlobByte(image);
291             else
292               for (i=0; i < count; i++)
293               {
294                 if ((i & 0x01) == 0)
295                   byte=(unsigned char) ReadBlobByte(image);
296                 *p++=(unsigned char)
297                   ((i & 0x01) != 0 ? (byte & 0x0f) : ((byte >> 4) & 0x0f));
298               }
299             x+=count;
300             /*
301               Read pad byte.
302             */
303             if (compression == BI_RLE8)
304               {
305                 if ((count & 0x01) != 0)
306                   (void) ReadBlobByte(image);
307               }
308             else
309               if (((count & 0x03) == 1) || ((count & 0x03) == 2))
310                 (void) ReadBlobByte(image);
311             break;
312           }
313         }
314       }
315     if (SetImageProgress(image,LoadImageTag,y,image->rows) == MagickFalse)
316       break;
317   }
318   (void) ReadBlobByte(image);  /* end of line */
319   (void) ReadBlobByte(image);
320   return(MagickTrue);
321 }
322 \f
323 /*
324 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
325 %                                                                             %
326 %                                                                             %
327 %                                                                             %
328 %   E n c o d e I m a g e                                                     %
329 %                                                                             %
330 %                                                                             %
331 %                                                                             %
332 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
333 %
334 %  EncodeImage compresses pixels using a runlength encoded format.
335 %
336 %  The format of the EncodeImage method is:
337 %
338 %    static MagickBooleanType EncodeImage(Image *image,
339 %      const size_t bytes_per_line,const unsigned char *pixels,
340 %      unsigned char *compressed_pixels)
341 %
342 %  A description of each parameter follows:
343 %
344 %    o image:  The image.
345 %
346 %    o bytes_per_line: the number of bytes in a scanline of compressed pixels
347 %
348 %    o pixels:  The address of a byte (8 bits) array of pixel data created by
349 %      the compression process.
350 %
351 %    o compressed_pixels:  The address of a byte (8 bits) array of compressed
352 %      pixel data.
353 %
354 */
355 static size_t EncodeImage(Image *image,const size_t bytes_per_line,
356   const unsigned char *pixels,unsigned char *compressed_pixels)
357 {
358   MagickBooleanType
359     status;
360
361   register const unsigned char
362     *p;
363
364   register ssize_t
365     i,
366     x;
367
368   register unsigned char
369     *q;
370
371   ssize_t
372     y;
373
374   /*
375     Runlength encode pixels.
376   */
377   assert(image != (Image *) NULL);
378   assert(image->signature == MagickSignature);
379   if (image->debug != MagickFalse)
380     (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
381   assert(pixels != (const unsigned char *) NULL);
382   assert(compressed_pixels != (unsigned char *) NULL);
383   p=pixels;
384   q=compressed_pixels;
385   i=0;
386   for (y=0; y < (ssize_t) image->rows; y++)
387   {
388     for (x=0; x < (ssize_t) bytes_per_line; x+=i)
389     {
390       /*
391         Determine runlength.
392       */
393       for (i=1; ((x+i) < (ssize_t) bytes_per_line); i++)
394         if ((i == 255) || (*(p+i) != *p))
395           break;
396       *q++=(unsigned char) i;
397       *q++=(*p);
398       p+=i;
399     }
400     /*
401       End of line.
402     */
403     *q++=(unsigned char) 0x00;
404     *q++=(unsigned char) 0x00;
405     status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
406       image->rows);
407     if (status == MagickFalse)
408       break;
409   }
410   /*
411     End of bitmap.
412   */
413   *q++=(unsigned char) 0x00;
414   *q++=(unsigned char) 0x01;
415   return((size_t) (q-compressed_pixels));
416 }
417 \f
418 /*
419 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
420 %                                                                             %
421 %                                                                             %
422 %                                                                             %
423 %   I s B M P                                                                 %
424 %                                                                             %
425 %                                                                             %
426 %                                                                             %
427 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
428 %
429 %  IsBMP() returns MagickTrue if the image format type, identified by the
430 %  magick string, is BMP.
431 %
432 %  The format of the IsBMP method is:
433 %
434 %      MagickBooleanType IsBMP(const unsigned char *magick,const size_t length)
435 %
436 %  A description of each parameter follows:
437 %
438 %    o magick: compare image format pattern against these bytes.
439 %
440 %    o length: Specifies the length of the magick string.
441 %
442 */
443 static MagickBooleanType IsBMP(const unsigned char *magick,const size_t length)
444 {
445   if (length < 2)
446     return(MagickFalse);
447   if ((LocaleNCompare((char *) magick,"BA",2) == 0) ||
448       (LocaleNCompare((char *) magick,"BM",2) == 0) ||
449       (LocaleNCompare((char *) magick,"IC",2) == 0) ||
450       (LocaleNCompare((char *) magick,"PI",2) == 0) ||
451       (LocaleNCompare((char *) magick,"CI",2) == 0) ||
452       (LocaleNCompare((char *) magick,"CP",2) == 0))
453     return(MagickTrue);
454   return(MagickFalse);
455 }
456 \f
457 /*
458 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
459 %                                                                             %
460 %                                                                             %
461 %                                                                             %
462 %   R e a d B M P I m a g e                                                   %
463 %                                                                             %
464 %                                                                             %
465 %                                                                             %
466 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
467 %
468 %  ReadBMPImage() reads a Microsoft Windows bitmap image file, Version
469 %  2, 3 (for Windows or NT), or 4, and  returns it.  It allocates the memory
470 %  necessary for the new Image structure and returns a pointer to the new
471 %  image.
472 %
473 %  The format of the ReadBMPImage method is:
474 %
475 %      image=ReadBMPImage(image_info)
476 %
477 %  A description of each parameter follows:
478 %
479 %    o image_info: the image info.
480 %
481 %    o exception: return any errors or warnings in this structure.
482 %
483 */
484
485 static Image *ReadBMPImage(const ImageInfo *image_info,ExceptionInfo *exception)
486 {
487   BMPInfo
488     bmp_info;
489
490   Image
491     *image;
492
493   IndexPacket
494     index;
495
496   MagickBooleanType
497     status;
498
499   MagickOffsetType
500     offset,
501     start_position;
502
503   register IndexPacket
504     *indexes;
505
506   register PixelPacket
507     *q;
508
509   register ssize_t
510     i,
511     x;
512
513   register unsigned char
514     *p;
515
516   size_t
517     bit,
518     blue,
519     bytes_per_line,
520     green,
521     length,
522     opacity,
523     red;
524
525   ssize_t
526     count,
527     y;
528
529   unsigned char
530     magick[12],
531     *pixels;
532
533   /*
534     Open image file.
535   */
536   assert(image_info != (const ImageInfo *) NULL);
537   assert(image_info->signature == MagickSignature);
538   if (image_info->debug != MagickFalse)
539     (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
540       image_info->filename);
541   assert(exception != (ExceptionInfo *) NULL);
542   assert(exception->signature == MagickSignature);
543   image=AcquireImage(image_info);
544   status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception);
545   if (status == MagickFalse)
546     {
547       image=DestroyImageList(image);
548       return((Image *) NULL);
549     }
550   /*
551     Determine if this a BMP file.
552   */
553   (void) ResetMagickMemory(&bmp_info,0,sizeof(bmp_info));
554   bmp_info.ba_offset=0;
555   start_position=0;
556   count=ReadBlob(image,2,magick);
557   do
558   {
559     LongPixelPacket
560       shift;
561
562     PixelPacket
563       quantum_bits;
564
565     size_t
566       profile_data,
567       profile_size;
568
569     /*
570       Verify BMP identifier.
571     */
572     if (bmp_info.ba_offset == 0)
573       start_position=TellBlob(image)-2;
574     bmp_info.ba_offset=0;
575     while (LocaleNCompare((char *) magick,"BA",2) == 0)
576     {
577       bmp_info.file_size=ReadBlobLSBLong(image);
578       bmp_info.ba_offset=ReadBlobLSBLong(image);
579       bmp_info.offset_bits=ReadBlobLSBLong(image);
580       count=ReadBlob(image,2,magick);
581       if (count != 2)
582         break;
583     }
584     if (image->debug != MagickFalse)
585       (void) LogMagickEvent(CoderEvent,GetMagickModule(),"  Magick: %c%c",
586         magick[0],magick[1]);
587     if ((count == 0) || ((LocaleNCompare((char *) magick,"BM",2) != 0) &&
588         (LocaleNCompare((char *) magick,"CI",2) != 0)))
589       ThrowReaderException(CorruptImageError,"ImproperImageHeader");
590     bmp_info.file_size=ReadBlobLSBLong(image);
591     (void) ReadBlobLSBLong(image);
592     bmp_info.offset_bits=ReadBlobLSBLong(image);
593     bmp_info.size=ReadBlobLSBLong(image);
594     if (image->debug != MagickFalse)
595       (void) LogMagickEvent(CoderEvent,GetMagickModule(),"  BMP size: %u",
596         bmp_info.size);
597     if (bmp_info.size == 12)
598       {
599         /*
600           OS/2 BMP image file.
601         */
602         bmp_info.width=(short) ReadBlobLSBShort(image);
603         bmp_info.height=(short) ReadBlobLSBShort(image);
604         bmp_info.planes=ReadBlobLSBShort(image);
605         bmp_info.bits_per_pixel=ReadBlobLSBShort(image);
606         bmp_info.x_pixels=0;
607         bmp_info.y_pixels=0;
608         bmp_info.number_colors=0;
609         bmp_info.compression=BI_RGB;
610         bmp_info.image_size=0;
611         bmp_info.alpha_mask=0;
612         if (image->debug != MagickFalse)
613           {
614             (void) LogMagickEvent(CoderEvent,GetMagickModule(),
615               "  Format: OS/2 Bitmap");
616             (void) LogMagickEvent(CoderEvent,GetMagickModule(),
617               "  Geometry: %.20gx%.20g",(double) bmp_info.width,(double)
618               bmp_info.height);
619           }
620       }
621     else
622       {
623         /*
624           Microsoft Windows BMP image file.
625         */
626         if (bmp_info.size < 40)
627           ThrowReaderException(CorruptImageError,"NonOS2HeaderSizeError");
628         bmp_info.width=(int) ReadBlobLSBLong(image);
629         bmp_info.height=(int) ReadBlobLSBLong(image);
630         bmp_info.planes=ReadBlobLSBShort(image);
631         bmp_info.bits_per_pixel=ReadBlobLSBShort(image);
632         bmp_info.compression=ReadBlobLSBLong(image);
633         bmp_info.image_size=ReadBlobLSBLong(image);
634         bmp_info.x_pixels=ReadBlobLSBLong(image);
635         bmp_info.y_pixels=ReadBlobLSBLong(image);
636         bmp_info.number_colors=ReadBlobLSBLong(image);
637         bmp_info.colors_important=ReadBlobLSBLong(image);
638         profile_data=0;
639         profile_size=0;
640         if (image->debug != MagickFalse)
641           {
642             (void) LogMagickEvent(CoderEvent,GetMagickModule(),
643               "  Format: MS Windows bitmap");
644             (void) LogMagickEvent(CoderEvent,GetMagickModule(),
645               "  Geometry: %.20gx%.20g",(double) bmp_info.width,(double)
646               bmp_info.height);
647             (void) LogMagickEvent(CoderEvent,GetMagickModule(),
648               "  Bits per pixel: %.20g",(double) bmp_info.bits_per_pixel);
649             switch ((int) bmp_info.compression)
650             {
651               case BI_RGB:
652               {
653                 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
654                   "  Compression: BI_RGB");
655                 break;
656               }
657               case BI_RLE4:
658               {
659                 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
660                   "  Compression: BI_RLE4");
661                 break;
662               }
663               case BI_RLE8:
664               {
665                 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
666                   "  Compression: BI_RLE8");
667                 break;
668               }
669               case BI_BITFIELDS:
670               {
671                 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
672                   "  Compression: BI_BITFIELDS");
673                 break;
674               }
675               case BI_PNG:
676               {
677                 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
678                   "  Compression: BI_PNG");
679                 break;
680               }
681               case BI_JPEG:
682               {
683                 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
684                   "  Compression: BI_JPEG");
685                 break;
686               }
687               default:
688               {
689                 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
690                   "  Compression: UNKNOWN (%u)",bmp_info.compression);
691               }
692             }
693             (void) LogMagickEvent(CoderEvent,GetMagickModule(),
694               "  Number of colors: %u",bmp_info.number_colors);
695           }
696         bmp_info.red_mask=ReadBlobLSBLong(image);
697         bmp_info.green_mask=ReadBlobLSBLong(image);
698         bmp_info.blue_mask=ReadBlobLSBLong(image);
699         if (bmp_info.size > 40)
700           {
701             double
702               sum;
703
704             /*
705               Read color management information.
706             */
707             bmp_info.alpha_mask=ReadBlobLSBLong(image);
708             bmp_info.colorspace=(int) ReadBlobLSBLong(image);
709             /*
710               Decode 2^30 fixed point formatted CIE primaries.
711             */
712             bmp_info.red_primary.x=(double) ReadBlobLSBLong(image)/0x3ffffff;
713             bmp_info.red_primary.y=(double) ReadBlobLSBLong(image)/0x3ffffff;
714             bmp_info.red_primary.z=(double) ReadBlobLSBLong(image)/0x3ffffff;
715             bmp_info.green_primary.x=(double) ReadBlobLSBLong(image)/0x3ffffff;
716             bmp_info.green_primary.y=(double) ReadBlobLSBLong(image)/0x3ffffff;
717             bmp_info.green_primary.z=(double) ReadBlobLSBLong(image)/0x3ffffff;
718             bmp_info.blue_primary.x=(double) ReadBlobLSBLong(image)/0x3ffffff;
719             bmp_info.blue_primary.y=(double) ReadBlobLSBLong(image)/0x3ffffff;
720             bmp_info.blue_primary.z=(double) ReadBlobLSBLong(image)/0x3ffffff;
721             sum=bmp_info.red_primary.x+bmp_info.red_primary.y+
722               bmp_info.red_primary.z;
723             image->chromaticity.red_primary.x/=sum;
724             image->chromaticity.red_primary.y/=sum;
725             sum=bmp_info.green_primary.x+bmp_info.green_primary.y+
726               bmp_info.green_primary.z;
727             image->chromaticity.green_primary.x/=sum;
728             image->chromaticity.green_primary.y/=sum;
729             sum=bmp_info.blue_primary.x+bmp_info.blue_primary.y+
730               bmp_info.blue_primary.z;
731             image->chromaticity.blue_primary.x/=sum;
732             image->chromaticity.blue_primary.y/=sum;
733             /*
734               Decode 16^16 fixed point formatted gamma_scales.
735             */
736             bmp_info.gamma_scale.x=(double) ReadBlobLSBLong(image)/0xffff;
737             bmp_info.gamma_scale.y=(double) ReadBlobLSBLong(image)/0xffff;
738             bmp_info.gamma_scale.z=(double) ReadBlobLSBLong(image)/0xffff;
739             /*
740               Compute a single gamma from the BMP 3-channel gamma.
741             */
742             image->gamma=(bmp_info.gamma_scale.x+bmp_info.gamma_scale.y+
743               bmp_info.gamma_scale.z)/3.0;
744           }
745         if (bmp_info.size > 108)
746           {
747             size_t
748               intent;
749
750             /*
751               Read BMP Version 5 color management information.
752             */
753             intent=ReadBlobLSBLong(image);
754             switch ((int) intent)
755             {
756               case LCS_GM_BUSINESS:
757               {
758                 image->rendering_intent=SaturationIntent;
759                 break;
760               }
761               case LCS_GM_GRAPHICS:
762               {
763                 image->rendering_intent=RelativeIntent;
764                 break;
765               }
766               case LCS_GM_IMAGES:
767               {
768                 image->rendering_intent=PerceptualIntent;
769                 break;
770               }
771               case LCS_GM_ABS_COLORIMETRIC:
772               {
773                 image->rendering_intent=AbsoluteIntent;
774                 break;
775               }
776             }
777             profile_data=ReadBlobLSBLong(image);
778             profile_size=ReadBlobLSBLong(image);
779             (void) profile_data;
780             (void) profile_size;
781             (void) ReadBlobLSBLong(image);  /* Reserved byte */
782           }
783       }
784     if ((MagickSizeType) bmp_info.file_size > GetBlobSize(image))
785       (void) ThrowMagickException(exception,GetMagickModule(),CorruptImageError,
786         "LengthAndFilesizeDoNotMatch","`%s'",image->filename);
787     else
788       if ((MagickSizeType) bmp_info.file_size < GetBlobSize(image))
789         (void) ThrowMagickException(exception,GetMagickModule(),
790           CorruptImageWarning,"LengthAndFilesizeDoNotMatch","`%s'",
791           image->filename);
792     if (bmp_info.width <= 0)
793       ThrowReaderException(CorruptImageError,"NegativeOrZeroImageSize");
794     if (bmp_info.height == 0)
795       ThrowReaderException(CorruptImageError,"NegativeOrZeroImageSize");
796     if (bmp_info.planes != 1)
797       ThrowReaderException(CorruptImageError,"StaticPlanesValueNotEqualToOne");
798     if ((bmp_info.bits_per_pixel != 1) && (bmp_info.bits_per_pixel != 4) &&
799         (bmp_info.bits_per_pixel != 8) && (bmp_info.bits_per_pixel != 16) &&
800         (bmp_info.bits_per_pixel != 24) && (bmp_info.bits_per_pixel != 32))
801       ThrowReaderException(CorruptImageError,"UnrecognizedBitsPerPixel");
802     if (bmp_info.number_colors > (1U << bmp_info.bits_per_pixel))
803       {
804         if (bmp_info.bits_per_pixel < 24)
805           ThrowReaderException(CorruptImageError,"UnrecognizedNumberOfColors");
806         bmp_info.number_colors=0;
807       }
808     if ((bmp_info.compression == 1) && (bmp_info.bits_per_pixel != 8))
809       ThrowReaderException(CorruptImageError,"UnrecognizedBitsPerPixel");
810     if ((bmp_info.compression == 2) && (bmp_info.bits_per_pixel != 4))
811       ThrowReaderException(CorruptImageError,"UnrecognizedBitsPerPixel");
812     if ((bmp_info.compression == 3) && (bmp_info.bits_per_pixel < 16))
813       ThrowReaderException(CorruptImageError,"UnrecognizedBitsPerPixel");
814     switch (bmp_info.compression)
815     {
816       case BI_RGB:
817       case BI_RLE8:
818       case BI_RLE4:
819       case BI_BITFIELDS:
820         break;
821       case BI_JPEG:
822         ThrowReaderException(CoderError,"JPEGCompressNotSupported");
823       case BI_PNG:
824         ThrowReaderException(CoderError,"PNGCompressNotSupported");
825       default:
826         ThrowReaderException(CorruptImageError,"UnrecognizedImageCompression");
827     }
828     image->columns=(size_t) MagickAbsoluteValue(bmp_info.width);
829     image->rows=(size_t) MagickAbsoluteValue(bmp_info.height);
830     image->depth=bmp_info.bits_per_pixel <= 8 ? bmp_info.bits_per_pixel : 8;
831     if ((bmp_info.bits_per_pixel == 16) ||
832         (bmp_info.bits_per_pixel == 32))
833       image->matte=bmp_info.alpha_mask != 0 ? MagickTrue : MagickFalse;
834     if ((bmp_info.number_colors != 0) || (bmp_info.bits_per_pixel < 16))
835       {
836         size_t
837           one;
838
839         image->storage_class=PseudoClass;
840         image->colors=bmp_info.number_colors;
841         one=1;
842         if (image->colors == 0)
843           image->colors=one << bmp_info.bits_per_pixel;
844       }
845     if (image->storage_class == PseudoClass)
846       {
847         unsigned char
848           *bmp_colormap;
849
850         size_t
851           packet_size;
852
853         /*
854           Read BMP raster colormap.
855         */
856         if (image->debug != MagickFalse)
857           (void) LogMagickEvent(CoderEvent,GetMagickModule(),
858             "  Reading colormap of %.20g colors",(double) image->colors);
859         if (AcquireImageColormap(image,image->colors) == MagickFalse)
860           ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
861         bmp_colormap=(unsigned char *) AcquireQuantumMemory((size_t)
862           image->colors,4*sizeof(*bmp_colormap));
863         if (bmp_colormap == (unsigned char *) NULL)
864           ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
865         if ((bmp_info.size == 12) || (bmp_info.size == 64))
866           packet_size=3;
867         else
868           packet_size=4;
869         offset=SeekBlob(image,start_position+14+bmp_info.size,SEEK_SET);
870         if (offset < 0)
871           ThrowReaderException(CorruptImageError,"ImproperImageHeader");
872         count=ReadBlob(image,packet_size*image->colors,bmp_colormap);
873         if (count != (ssize_t) (packet_size*image->colors))
874           ThrowReaderException(CorruptImageError,"InsufficientImageDataInFile");
875         p=bmp_colormap;
876         for (i=0; i < (ssize_t) image->colors; i++)
877         {
878           image->colormap[i].blue=ScaleCharToQuantum(*p++);
879           image->colormap[i].green=ScaleCharToQuantum(*p++);
880           image->colormap[i].red=ScaleCharToQuantum(*p++);
881           if (packet_size == 4)
882             p++;
883         }
884         bmp_colormap=(unsigned char *) RelinquishMagickMemory(bmp_colormap);
885       }
886     if ((image_info->ping != MagickFalse) && (image_info->number_scenes != 0))
887       if (image->scene >= (image_info->scene+image_info->number_scenes-1))
888         break;
889     /*
890       Read image data.
891     */
892     offset=SeekBlob(image,start_position+bmp_info.offset_bits,SEEK_SET);
893     if (offset < 0)
894       ThrowReaderException(CorruptImageError,"ImproperImageHeader");
895     if (bmp_info.compression == BI_RLE4)
896       bmp_info.bits_per_pixel<<=1;
897     bytes_per_line=4*((image->columns*bmp_info.bits_per_pixel+31)/32);
898     length=(size_t) bytes_per_line*image->rows;
899     pixels=(unsigned char *) AcquireQuantumMemory((size_t) image->rows,
900       MagickMax(bytes_per_line,image->columns+256UL)*sizeof(*pixels));
901     if (pixels == (unsigned char *) NULL)
902       ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
903     if ((bmp_info.compression == BI_RGB) ||
904         (bmp_info.compression == BI_BITFIELDS))
905       {
906         if (image->debug != MagickFalse)
907           (void) LogMagickEvent(CoderEvent,GetMagickModule(),
908             "  Reading pixels (%.20g bytes)",(double) length);
909         count=ReadBlob(image,length,pixels);
910         if (count != (ssize_t) length)
911           {
912             pixels=(unsigned char *) RelinquishMagickMemory(pixels);
913             ThrowReaderException(CorruptImageError,
914               "InsufficientImageDataInFile");
915           }
916       }
917     else
918       {
919         /*
920           Convert run-length encoded raster pixels.
921         */
922         status=DecodeImage(image,bmp_info.compression,pixels);
923         if (status == MagickFalse)
924           {
925             pixels=(unsigned char *) RelinquishMagickMemory(pixels);
926             ThrowReaderException(CorruptImageError,
927               "UnableToRunlengthDecodeImage");
928           }
929       }
930     /*
931       Initialize image structure.
932     */
933     image->x_resolution=(double) bmp_info.x_pixels/100.0;
934     image->y_resolution=(double) bmp_info.y_pixels/100.0;
935     image->units=PixelsPerCentimeterResolution;
936     /*
937       Convert BMP raster image to pixel packets.
938     */
939     if (bmp_info.compression == BI_RGB)
940       {
941         bmp_info.alpha_mask=0;
942         bmp_info.red_mask=0x00ff0000U;
943         bmp_info.green_mask=0x0000ff00U;
944         bmp_info.blue_mask=0x000000ffU;
945         if (bmp_info.bits_per_pixel == 16)
946           {
947             /*
948               RGB555.
949             */
950             bmp_info.red_mask=0x00007c00U;
951             bmp_info.green_mask=0x000003e0U;
952             bmp_info.blue_mask=0x0000001fU;
953           }
954       }
955     if ((bmp_info.bits_per_pixel == 16) || (bmp_info.bits_per_pixel == 32))
956       {
957         register size_t
958           sample;
959
960         /*
961           Get shift and quantum bits info from bitfield masks.
962         */
963         (void) ResetMagickMemory(&shift,0,sizeof(shift));
964         (void) ResetMagickMemory(&quantum_bits,0,sizeof(quantum_bits));
965         if (bmp_info.red_mask != 0)
966           while (((bmp_info.red_mask << shift.red) & 0x80000000UL) == 0)
967             shift.red++;
968         if (bmp_info.green_mask != 0)
969           while (((bmp_info.green_mask << shift.green) & 0x80000000UL) == 0)
970             shift.green++;
971         if (bmp_info.blue_mask != 0)
972           while (((bmp_info.blue_mask << shift.blue) & 0x80000000UL) == 0)
973             shift.blue++;
974         if (bmp_info.alpha_mask != 0)
975           while (((bmp_info.alpha_mask << shift.opacity) & 0x80000000UL) == 0)
976             shift.opacity++;
977         sample=shift.red;
978         while (((bmp_info.red_mask << sample) & 0x80000000UL) != 0)
979           sample++;
980         quantum_bits.red=(Quantum) (sample-shift.red);
981         sample=shift.green;
982         while (((bmp_info.green_mask << sample) & 0x80000000UL) != 0)
983           sample++;
984         quantum_bits.green=(Quantum) (sample-shift.green);
985         sample=shift.blue;
986         while (((bmp_info.blue_mask << sample) & 0x80000000UL) != 0)
987           sample++;
988         quantum_bits.blue=(Quantum) (sample-shift.blue);
989         sample=shift.opacity;
990         while (((bmp_info.alpha_mask << sample) & 0x80000000UL) != 0)
991           sample++;
992         quantum_bits.opacity=(Quantum) (sample-shift.opacity);
993       }
994     switch (bmp_info.bits_per_pixel)
995     {
996       case 1:
997       {
998         /*
999           Convert bitmap scanline.
1000         */
1001         for (y=(ssize_t) image->rows-1; y >= 0; y--)
1002         {
1003           p=pixels+(image->rows-y-1)*bytes_per_line;
1004           q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
1005           if (q == (PixelPacket *) NULL)
1006             break;
1007           indexes=GetAuthenticIndexQueue(image);
1008           for (x=0; x < ((ssize_t) image->columns-7); x+=8)
1009           {
1010             for (bit=0; bit < 8; bit++)
1011             {
1012               index=(IndexPacket) (((*p) & (0x80 >> bit)) != 0 ? 0x01 : 0x00);
1013               SetIndexPixelComponent(indexes+x+bit,index);
1014               q++;
1015             }
1016             p++;
1017           }
1018           if ((image->columns % 8) != 0)
1019             {
1020               for (bit=0; bit < (image->columns % 8); bit++)
1021               {
1022                 index=(IndexPacket) (((*p) & (0x80 >> bit)) != 0 ? 0x01 : 0x00);
1023                 SetIndexPixelComponent(indexes+x+bit,index);
1024               }
1025               p++;
1026             }
1027           if (SyncAuthenticPixels(image,exception) == MagickFalse)
1028             break;
1029           if (image->previous == (Image *) NULL)
1030             {
1031               status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y,
1032                 image->rows);
1033               if (status == MagickFalse)
1034                 break;
1035             }
1036         }
1037         (void) SyncImage(image);
1038         break;
1039       }
1040       case 4:
1041       {
1042         /*
1043           Convert PseudoColor scanline.
1044         */
1045         for (y=(ssize_t) image->rows-1; y >= 0; y--)
1046         {
1047           p=pixels+(image->rows-y-1)*bytes_per_line;
1048           q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
1049           if (q == (PixelPacket *) NULL)
1050             break;
1051           indexes=GetAuthenticIndexQueue(image);
1052           for (x=0; x < ((ssize_t) image->columns-1); x+=2)
1053           {
1054             index=ConstrainColormapIndex(image,(*p >> 4) & 0x0f);
1055             SetIndexPixelComponent(indexes+x,index);
1056             index=ConstrainColormapIndex(image,*p & 0x0f);
1057             SetIndexPixelComponent(indexes+x+1,index);
1058             p++;
1059           }
1060           if ((image->columns % 2) != 0)
1061             {
1062               index=ConstrainColormapIndex(image,(*p >> 4) & 0xf);
1063               SetIndexPixelComponent(indexes+x,index);
1064               p++;
1065             }
1066           if (SyncAuthenticPixels(image,exception) == MagickFalse)
1067             break;
1068           if (image->previous == (Image *) NULL)
1069             {
1070               status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y,
1071                 image->rows);
1072               if (status == MagickFalse)
1073                 break;
1074             }
1075         }
1076         (void) SyncImage(image);
1077         break;
1078       }
1079       case 8:
1080       {
1081         /*
1082           Convert PseudoColor scanline.
1083         */
1084         if ((bmp_info.compression == BI_RLE8) ||
1085             (bmp_info.compression == BI_RLE4))
1086           bytes_per_line=image->columns;
1087         for (y=(ssize_t) image->rows-1; y >= 0; y--)
1088         {
1089           p=pixels+(image->rows-y-1)*bytes_per_line;
1090           q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
1091           if (q == (PixelPacket *) NULL)
1092             break;
1093           indexes=GetAuthenticIndexQueue(image);
1094           for (x = (ssize_t)image->columns; x != 0; --x)
1095           {
1096             index=ConstrainColormapIndex(image,*p);
1097             SetIndexPixelComponent(indexes,index);
1098             indexes++;
1099             p++;
1100             q++;
1101           }
1102           if (SyncAuthenticPixels(image,exception) == MagickFalse)
1103             break;
1104           offset=(MagickOffsetType) (image->rows-y-1);
1105           if (image->previous == (Image *) NULL)
1106             {
1107               status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y,
1108                 image->rows);
1109               if (status == MagickFalse)
1110                 break;
1111             }
1112         }
1113         (void) SyncImage(image);
1114         break;
1115       }
1116       case 16:
1117       {
1118         size_t
1119           pixel;
1120
1121         /*
1122           Convert bitfield encoded 16-bit PseudoColor scanline.
1123         */
1124         if (bmp_info.compression != BI_RGB &&
1125             bmp_info.compression != BI_BITFIELDS)
1126           {
1127             pixels=(unsigned char *) RelinquishMagickMemory(pixels);
1128             ThrowReaderException(CorruptImageError,
1129               "UnrecognizedImageCompression");
1130           }
1131         bytes_per_line=2*(image->columns+image->columns % 2);
1132         image->storage_class=DirectClass;
1133         for (y=(ssize_t) image->rows-1; y >= 0; y--)
1134         {
1135           p=pixels+(image->rows-y-1)*bytes_per_line;
1136           q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
1137           if (q == (PixelPacket *) NULL)
1138             break;
1139           for (x=0; x < (ssize_t) image->columns; x++)
1140           {
1141             pixel=(size_t) (*p++);
1142             pixel|=(*p++) << 8;
1143             red=((pixel & bmp_info.red_mask) << shift.red) >> 16;
1144             if (quantum_bits.red == 5)
1145               red|=((red & 0xe000) >> 5);
1146             if (quantum_bits.red <= 8)
1147               red|=((red & 0xff00) >> 8);
1148             green=((pixel & bmp_info.green_mask) << shift.green) >> 16;
1149             if (quantum_bits.green == 5)
1150               green|=((green & 0xe000) >> 5);
1151             if (quantum_bits.green == 6)
1152               green|=((green & 0xc000) >> 6);
1153             if (quantum_bits.green <= 8)
1154               green|=((green & 0xff00) >> 8);
1155             blue=((pixel & bmp_info.blue_mask) << shift.blue) >> 16;
1156             if (quantum_bits.blue == 5)
1157               blue|=((blue & 0xe000) >> 5);
1158             if (quantum_bits.blue <= 8)
1159               blue|=((blue & 0xff00) >> 8);
1160             opacity=((pixel & bmp_info.alpha_mask) << shift.opacity) >> 16;
1161             if (quantum_bits.opacity <= 8)
1162               opacity|=((opacity & 0xff00) >> 8);
1163             SetRedPixelComponent(q,ScaleShortToQuantum((unsigned short) red));
1164             SetGreenPixelComponent(q,ScaleShortToQuantum((unsigned short)
1165               green));
1166             SetBluePixelComponent(q,ScaleShortToQuantum((unsigned short) blue));
1167             SetOpacityPixelComponent(q,OpaqueOpacity);
1168             if (image->matte != MagickFalse)
1169               SetAlphaPixelComponent(q,ScaleShortToQuantum((unsigned short)
1170                 opacity));
1171             q++;
1172           }
1173           if (SyncAuthenticPixels(image,exception) == MagickFalse)
1174             break;
1175           offset=(MagickOffsetType) (image->rows-y-1);
1176           if (image->previous == (Image *) NULL)
1177             {
1178               status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y,
1179                 image->rows);
1180               if (status == MagickFalse)
1181                 break;
1182             }
1183         }
1184         break;
1185       }
1186       case 24:
1187       {
1188         /*
1189           Convert DirectColor scanline.
1190         */
1191         bytes_per_line=4*((image->columns*24+31)/32);
1192         for (y=(ssize_t) image->rows-1; y >= 0; y--)
1193         {
1194           p=pixels+(image->rows-y-1)*bytes_per_line;
1195           q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
1196           if (q == (PixelPacket *) NULL)
1197             break;
1198           for (x=0; x < (ssize_t) image->columns; x++)
1199           {
1200             SetBluePixelComponent(q,ScaleCharToQuantum(*p++));
1201             SetGreenPixelComponent(q,ScaleCharToQuantum(*p++));
1202             SetRedPixelComponent(q,ScaleCharToQuantum(*p++));
1203             SetOpacityPixelComponent(q,OpaqueOpacity);
1204             q++;
1205           }
1206           if (SyncAuthenticPixels(image,exception) == MagickFalse)
1207             break;
1208           offset=(MagickOffsetType) (image->rows-y-1);
1209           if (image->previous == (Image *) NULL)
1210             {
1211               status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y,
1212                 image->rows);
1213               if (status == MagickFalse)
1214                 break;
1215             }
1216         }
1217         break;
1218       }
1219       case 32:
1220       {
1221         /*
1222           Convert bitfield encoded DirectColor scanline.
1223         */
1224         if ((bmp_info.compression != BI_RGB) &&
1225             (bmp_info.compression != BI_BITFIELDS))
1226           {
1227             pixels=(unsigned char *) RelinquishMagickMemory(pixels);
1228             ThrowReaderException(CorruptImageError,
1229               "UnrecognizedImageCompression");
1230           }
1231         bytes_per_line=4*(image->columns);
1232         for (y=(ssize_t) image->rows-1; y >= 0; y--)
1233         {
1234           size_t
1235             pixel;
1236
1237           p=pixels+(image->rows-y-1)*bytes_per_line;
1238           q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
1239           if (q == (PixelPacket *) NULL)
1240             break;
1241           for (x=0; x < (ssize_t) image->columns; x++)
1242           {
1243             pixel=(size_t) (*p++);
1244             pixel|=(*p++ << 8);
1245             pixel|=(*p++ << 16);
1246             pixel|=(*p++ << 24);
1247             red=((pixel & bmp_info.red_mask) << shift.red) >> 16;
1248             if (quantum_bits.red == 8)
1249               red|=(red >> 8);
1250             green=((pixel & bmp_info.green_mask) << shift.green) >> 16;
1251             if (quantum_bits.green == 8)
1252               green|=(green >> 8);
1253             blue=((pixel & bmp_info.blue_mask) << shift.blue) >> 16;
1254             if (quantum_bits.blue == 8)
1255               blue|=(blue >> 8);
1256             opacity=((pixel & bmp_info.alpha_mask) << shift.opacity) >> 16;
1257             if (quantum_bits.opacity == 8)
1258               opacity|=(opacity >> 8);
1259             SetRedPixelComponent(q,ScaleShortToQuantum((unsigned short) red));
1260             SetGreenPixelComponent(q,ScaleShortToQuantum((unsigned short)
1261               green));
1262             SetBluePixelComponent(q,ScaleShortToQuantum((unsigned short) blue));
1263             SetOpacityPixelComponent(q,OpaqueOpacity);
1264             if (image->matte != MagickFalse)
1265               SetAlphaPixelComponent(q,ScaleShortToQuantum((unsigned short)
1266                 opacity));
1267             q++;
1268           }
1269           if (SyncAuthenticPixels(image,exception) == MagickFalse)
1270             break;
1271           offset=(MagickOffsetType) (image->rows-y-1);
1272           if (image->previous == (Image *) NULL)
1273             {
1274               status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y,
1275                 image->rows);
1276               if (status == MagickFalse)
1277                 break;
1278             }
1279         }
1280         break;
1281       }
1282       default:
1283       {
1284         pixels=(unsigned char *) RelinquishMagickMemory(pixels);
1285         ThrowReaderException(CorruptImageError,"ImproperImageHeader");
1286       }
1287     }
1288     pixels=(unsigned char *) RelinquishMagickMemory(pixels);
1289     if (EOFBlob(image) != MagickFalse)
1290       {
1291         ThrowFileException(exception,CorruptImageError,"UnexpectedEndOfFile",
1292           image->filename);
1293         break;
1294       }
1295     if (bmp_info.height < 0)
1296       {
1297         Image
1298           *flipped_image;
1299
1300         /*
1301           Correct image orientation.
1302         */
1303         flipped_image=FlipImage(image,exception);
1304         if (flipped_image != (Image *) NULL)
1305           {
1306             DuplicateBlob(flipped_image,image);
1307             image=DestroyImage(image);
1308             image=flipped_image;
1309           }
1310       }
1311     /*
1312       Proceed to next image.
1313     */
1314     if (image_info->number_scenes != 0)
1315       if (image->scene >= (image_info->scene+image_info->number_scenes-1))
1316         break;
1317     *magick='\0';
1318     if (bmp_info.ba_offset != 0)
1319       {
1320         offset=SeekBlob(image,(MagickOffsetType) bmp_info.ba_offset,SEEK_SET);
1321         if (offset < 0)
1322           ThrowReaderException(CorruptImageError,"ImproperImageHeader");
1323       }
1324     count=ReadBlob(image,2,magick);
1325     if ((count == 2) && (IsBMP(magick,2) != MagickFalse))
1326       {
1327         /*
1328           Acquire next image structure.
1329         */
1330         AcquireNextImage(image_info,image);
1331         if (GetNextImageInList(image) == (Image *) NULL)
1332           {
1333             image=DestroyImageList(image);
1334             return((Image *) NULL);
1335           }
1336         image=SyncNextImageInList(image);
1337         status=SetImageProgress(image,LoadImagesTag,TellBlob(image),
1338           GetBlobSize(image));
1339         if (status == MagickFalse)
1340           break;
1341       }
1342   } while (IsBMP(magick,2) != MagickFalse);
1343   (void) CloseBlob(image);
1344   return(GetFirstImageInList(image));
1345 }
1346 \f
1347 /*
1348 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1349 %                                                                             %
1350 %                                                                             %
1351 %                                                                             %
1352 %   R e g i s t e r B M P I m a g e                                           %
1353 %                                                                             %
1354 %                                                                             %
1355 %                                                                             %
1356 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1357 %
1358 %  RegisterBMPImage() adds attributes for the BMP image format to
1359 %  the list of supported formats.  The attributes include the image format
1360 %  tag, a method to read and/or write the format, whether the format
1361 %  supports the saving of more than one frame to the same file or blob,
1362 %  whether the format supports native in-memory I/O, and a brief
1363 %  description of the format.
1364 %
1365 %  The format of the RegisterBMPImage method is:
1366 %
1367 %      size_t RegisterBMPImage(void)
1368 %
1369 */
1370 ModuleExport size_t RegisterBMPImage(void)
1371 {
1372   MagickInfo
1373     *entry;
1374
1375   entry=SetMagickInfo("BMP");
1376   entry->decoder=(DecodeImageHandler *) ReadBMPImage;
1377   entry->encoder=(EncodeImageHandler *) WriteBMPImage;
1378   entry->magick=(IsImageFormatHandler *) IsBMP;
1379   entry->description=ConstantString("Microsoft Windows bitmap image");
1380   entry->module=ConstantString("BMP");
1381   entry->adjoin=MagickFalse;
1382   entry->seekable_stream=MagickTrue;
1383   (void) RegisterMagickInfo(entry);
1384   entry=SetMagickInfo("BMP2");
1385   entry->encoder=(EncodeImageHandler *) WriteBMPImage;
1386   entry->magick=(IsImageFormatHandler *) IsBMP;
1387   entry->description=ConstantString("Microsoft Windows bitmap image (V2)");
1388   entry->module=ConstantString("BMP");
1389   entry->adjoin=MagickFalse;
1390   entry->seekable_stream=MagickTrue;
1391   (void) RegisterMagickInfo(entry);
1392   entry=SetMagickInfo("BMP3");
1393   entry->encoder=(EncodeImageHandler *) WriteBMPImage;
1394   entry->magick=(IsImageFormatHandler *) IsBMP;
1395   entry->description=ConstantString("Microsoft Windows bitmap image (V3)");
1396   entry->module=ConstantString("BMP");
1397   entry->adjoin=MagickFalse;
1398   entry->seekable_stream=MagickTrue;
1399   (void) RegisterMagickInfo(entry);
1400   return(MagickImageCoderSignature);
1401 }
1402 \f
1403 /*
1404 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1405 %                                                                             %
1406 %                                                                             %
1407 %                                                                             %
1408 %   U n r e g i s t e r B M P I m a g e                                       %
1409 %                                                                             %
1410 %                                                                             %
1411 %                                                                             %
1412 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1413 %
1414 %  UnregisterBMPImage() removes format registrations made by the
1415 %  BMP module from the list of supported formats.
1416 %
1417 %  The format of the UnregisterBMPImage method is:
1418 %
1419 %      UnregisterBMPImage(void)
1420 %
1421 */
1422 ModuleExport void UnregisterBMPImage(void)
1423 {
1424   (void) UnregisterMagickInfo("BMP");
1425   (void) UnregisterMagickInfo("BMP2");
1426   (void) UnregisterMagickInfo("BMP3");
1427 }
1428 \f
1429 /*
1430 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1431 %                                                                             %
1432 %                                                                             %
1433 %                                                                             %
1434 %   W r i t e B M P I m a g e                                                 %
1435 %                                                                             %
1436 %                                                                             %
1437 %                                                                             %
1438 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1439 %
1440 %  WriteBMPImage() writes an image in Microsoft Windows bitmap encoded
1441 %  image format, version 3 for Windows or (if the image has a matte channel)
1442 %  version 4.
1443 %
1444 %  The format of the WriteBMPImage method is:
1445 %
1446 %      MagickBooleanType WriteBMPImage(const ImageInfo *image_info,Image *image)
1447 %
1448 %  A description of each parameter follows.
1449 %
1450 %    o image_info: the image info.
1451 %
1452 %    o image:  The image.
1453 %
1454 */
1455 static MagickBooleanType WriteBMPImage(const ImageInfo *image_info,Image *image)
1456 {
1457   BMPInfo
1458     bmp_info;
1459
1460   const StringInfo
1461     *profile;
1462
1463   MagickBooleanType
1464     have_color_info,
1465     status;
1466
1467   MagickOffsetType
1468     scene;
1469
1470   register const IndexPacket
1471     *indexes;
1472
1473   register const PixelPacket
1474     *p;
1475
1476   register ssize_t
1477     i,
1478     x;
1479
1480   register unsigned char
1481     *q;
1482
1483   size_t
1484     bytes_per_line,
1485     type;
1486
1487   ssize_t
1488     y;
1489
1490   unsigned char
1491     *bmp_data,
1492     *pixels;
1493
1494   /*
1495     Open output image file.
1496   */
1497   assert(image_info != (const ImageInfo *) NULL);
1498   assert(image_info->signature == MagickSignature);
1499   assert(image != (Image *) NULL);
1500   assert(image->signature == MagickSignature);
1501   if (image->debug != MagickFalse)
1502     (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1503   status=OpenBlob(image_info,image,WriteBinaryBlobMode,&image->exception);
1504   if (status == MagickFalse)
1505     return(status);
1506   type=4;
1507   if (LocaleCompare(image_info->magick,"BMP2") == 0)
1508     type=2;
1509   else
1510     if (LocaleCompare(image_info->magick,"BMP3") == 0)
1511       type=3;
1512   scene=0;
1513   do
1514   {
1515     /*
1516       Initialize BMP raster file header.
1517     */
1518     if (image->colorspace != RGBColorspace)
1519       (void) TransformImageColorspace(image,RGBColorspace);
1520     (void) ResetMagickMemory(&bmp_info,0,sizeof(bmp_info));
1521     bmp_info.file_size=14+12;
1522     if (type > 2)
1523       bmp_info.file_size+=28;
1524     bmp_info.offset_bits=bmp_info.file_size;
1525     bmp_info.compression=BI_RGB;
1526     if ((image->storage_class == PseudoClass) && (image->colors > 256))
1527       (void) SetImageStorageClass(image,DirectClass);
1528     if (image->storage_class != DirectClass)
1529       {
1530         /*
1531           Colormapped BMP raster.
1532         */
1533         bmp_info.bits_per_pixel=8;
1534         if (image->colors <= 2)
1535           bmp_info.bits_per_pixel=1;
1536         else
1537           if (image->colors <= 16)
1538             bmp_info.bits_per_pixel=4;
1539           else
1540             if (image->colors <= 256)
1541               bmp_info.bits_per_pixel=8;
1542         if (image_info->compression == RLECompression)
1543           bmp_info.bits_per_pixel=8;
1544         bmp_info.number_colors=1U << bmp_info.bits_per_pixel;
1545         if (image->matte != MagickFalse)
1546           (void) SetImageStorageClass(image,DirectClass);
1547         else
1548           if ((size_t) bmp_info.number_colors < image->colors)
1549             (void) SetImageStorageClass(image,DirectClass);
1550           else
1551             {
1552               bmp_info.file_size+=3*(1UL << bmp_info.bits_per_pixel);
1553               bmp_info.offset_bits+=3*(1UL << bmp_info.bits_per_pixel);
1554               if (type > 2)
1555                 {
1556                   bmp_info.file_size+=(1UL << bmp_info.bits_per_pixel);
1557                   bmp_info.offset_bits+=(1UL << bmp_info.bits_per_pixel);
1558                 }
1559             }
1560       }
1561     if (image->storage_class == DirectClass)
1562       {
1563         /*
1564           Full color BMP raster.
1565         */
1566         bmp_info.number_colors=0;
1567         bmp_info.bits_per_pixel=(unsigned short)
1568           ((type > 3) && (image->matte != MagickFalse) ? 32 : 24);
1569         bmp_info.compression=(unsigned int) ((type > 3) &&
1570           (image->matte != MagickFalse) ?  BI_BITFIELDS : BI_RGB);
1571       }
1572     bytes_per_line=4*((image->columns*bmp_info.bits_per_pixel+31)/32);
1573     bmp_info.ba_offset=0;
1574     profile=GetImageProfile(image,"icc");
1575     have_color_info=(image->rendering_intent != UndefinedIntent) ||
1576       (profile != (StringInfo *) NULL) || (image->gamma != 0.0) ?  MagickTrue :
1577       MagickFalse;
1578     if (type == 2)
1579       bmp_info.size=12;
1580     else
1581       if ((type == 3) || ((image->matte == MagickFalse) &&
1582           (have_color_info == MagickFalse)))
1583         {
1584           type=3;
1585           bmp_info.size=40;
1586         }
1587       else
1588         {
1589           int
1590             extra_size;
1591
1592           bmp_info.size=108;
1593           extra_size=68;
1594           if ((image->rendering_intent != UndefinedIntent) ||
1595               (profile != (StringInfo *) NULL))
1596             {
1597               bmp_info.size=124;
1598               extra_size+=16;
1599             }
1600           bmp_info.file_size+=extra_size;
1601           bmp_info.offset_bits+=extra_size;
1602         }
1603     bmp_info.width=(ssize_t) image->columns;
1604     bmp_info.height=(ssize_t) image->rows;
1605     bmp_info.planes=1;
1606     bmp_info.image_size=(unsigned int) (bytes_per_line*image->rows);
1607     bmp_info.file_size+=bmp_info.image_size;
1608     bmp_info.x_pixels=75*39;
1609     bmp_info.y_pixels=75*39;
1610     switch (image->units)
1611     {
1612       case UndefinedResolution:
1613       case PixelsPerInchResolution:
1614       {
1615         bmp_info.x_pixels=(unsigned int) (100.0*image->x_resolution/2.54);
1616         bmp_info.y_pixels=(unsigned int) (100.0*image->y_resolution/2.54);
1617         break;
1618       }
1619       case PixelsPerCentimeterResolution:
1620       {
1621         bmp_info.x_pixels=(unsigned int) (100.0*image->x_resolution);
1622         bmp_info.y_pixels=(unsigned int) (100.0*image->y_resolution);
1623         break;
1624       }
1625     }
1626     bmp_info.colors_important=bmp_info.number_colors;
1627     /*
1628       Convert MIFF to BMP raster pixels.
1629     */
1630     pixels=(unsigned char *) AcquireQuantumMemory((size_t) bmp_info.image_size,
1631       sizeof(*pixels));
1632     if (pixels == (unsigned char *) NULL)
1633       ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
1634     (void) ResetMagickMemory(pixels,0,(size_t) bmp_info.image_size);
1635     switch (bmp_info.bits_per_pixel)
1636     {
1637       case 1:
1638       {
1639         size_t
1640           bit,
1641           byte;
1642
1643         /*
1644           Convert PseudoClass image to a BMP monochrome image.
1645         */
1646         for (y=0; y < (ssize_t) image->rows; y++)
1647         {
1648           p=GetVirtualPixels(image,0,y,image->columns,1,&image->exception);
1649           if (p == (const PixelPacket *) NULL)
1650             break;
1651           indexes=GetVirtualIndexQueue(image);
1652           q=pixels+(image->rows-y-1)*bytes_per_line;
1653           bit=0;
1654           byte=0;
1655           for (x=0; x < (ssize_t) image->columns; x++)
1656           {
1657             byte<<=1;
1658             byte|=GetIndexPixelComponent(indexes+x) != 0 ? 0x01 : 0x00;
1659             bit++;
1660             if (bit == 8)
1661               {
1662                 *q++=(unsigned char) byte;
1663                 bit=0;
1664                 byte=0;
1665               }
1666            }
1667            if (bit != 0)
1668              {
1669                *q++=(unsigned char) (byte << (8-bit));
1670                x++;
1671              }
1672           for (x=(ssize_t) (image->columns+7)/8; x < (ssize_t) bytes_per_line; x++)
1673             *q++=0x00;
1674           if (image->previous == (Image *) NULL)
1675             {
1676               status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
1677                 image->rows);
1678               if (status == MagickFalse)
1679                 break;
1680             }
1681         }
1682         break;
1683       }
1684       case 4:
1685       {
1686         size_t
1687           nibble,
1688           byte;
1689
1690         /*
1691           Convert PseudoClass image to a BMP monochrome image.
1692         */
1693         for (y=0; y < (ssize_t) image->rows; y++)
1694         {
1695           p=GetVirtualPixels(image,0,y,image->columns,1,&image->exception);
1696           if (p == (const PixelPacket *) NULL)
1697             break;
1698           indexes=GetVirtualIndexQueue(image);
1699           q=pixels+(image->rows-y-1)*bytes_per_line;
1700           nibble=0;
1701           byte=0;
1702           for (x=0; x < (ssize_t) image->columns; x++)
1703           {
1704             byte<<=4;
1705             byte|=((size_t) GetIndexPixelComponent(indexes+x) & 0x0f);
1706             nibble++;
1707             if (nibble == 2)
1708               {
1709                 *q++=(unsigned char) byte;
1710                 nibble=0;
1711                 byte=0;
1712               }
1713            }
1714          if (nibble != 0)
1715            {
1716              *q++=(unsigned char) (byte << 4);
1717              x++;
1718            }
1719           for (x=(ssize_t) (image->columns+1)/2; x < (ssize_t) bytes_per_line; x++)
1720             *q++=0x00;
1721           if (image->previous == (Image *) NULL)
1722             {
1723               status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
1724                 image->rows);
1725               if (status == MagickFalse)
1726                 break;
1727             }
1728         }
1729         break;
1730       }
1731       case 8:
1732       {
1733         /*
1734           Convert PseudoClass packet to BMP pixel.
1735         */
1736         for (y=0; y < (ssize_t) image->rows; y++)
1737         {
1738           p=GetVirtualPixels(image,0,y,image->columns,1,&image->exception);
1739           if (p == (const PixelPacket *) NULL)
1740             break;
1741           indexes=GetVirtualIndexQueue(image);
1742           q=pixels+(image->rows-y-1)*bytes_per_line;
1743           for (x=0; x < (ssize_t) image->columns; x++)
1744             *q++=(unsigned char) GetIndexPixelComponent(indexes+x);
1745           for ( ; x < (ssize_t) bytes_per_line; x++)
1746             *q++=0x00;
1747           if (image->previous == (Image *) NULL)
1748             {
1749               status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
1750                 image->rows);
1751               if (status == MagickFalse)
1752                 break;
1753             }
1754         }
1755         break;
1756       }
1757       case 24:
1758       {
1759         /*
1760           Convert DirectClass packet to BMP BGR888.
1761         */
1762         for (y=0; y < (ssize_t) image->rows; y++)
1763         {
1764           p=GetVirtualPixels(image,0,y,image->columns,1,&image->exception);
1765           if (p == (const PixelPacket *) NULL)
1766             break;
1767           q=pixels+(image->rows-y-1)*bytes_per_line;
1768           for (x=0; x < (ssize_t) image->columns; x++)
1769           {
1770             *q++=ScaleQuantumToChar(GetBluePixelComponent(p));
1771             *q++=ScaleQuantumToChar(GetGreenPixelComponent(p));
1772             *q++=ScaleQuantumToChar(GetRedPixelComponent(p));
1773             p++;
1774           }
1775           for (x=3L*(ssize_t) image->columns; x < (ssize_t) bytes_per_line; x++)
1776             *q++=0x00;
1777           if (image->previous == (Image *) NULL)
1778             {
1779               status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
1780                 image->rows);
1781               if (status == MagickFalse)
1782                 break;
1783             }
1784         }
1785         break;
1786       }
1787       case 32:
1788       {
1789         /*
1790           Convert DirectClass packet to ARGB8888 pixel.
1791         */
1792         for (y=0; y < (ssize_t) image->rows; y++)
1793         {
1794           p=GetVirtualPixels(image,0,y,image->columns,1,&image->exception);
1795           if (p == (const PixelPacket *) NULL)
1796             break;
1797           q=pixels+(image->rows-y-1)*bytes_per_line;
1798           for (x=0; x < (ssize_t) image->columns; x++)
1799           {
1800             *q++=ScaleQuantumToChar(GetBluePixelComponent(p));
1801             *q++=ScaleQuantumToChar(GetGreenPixelComponent(p));
1802             *q++=ScaleQuantumToChar(GetRedPixelComponent(p));
1803             *q++=ScaleQuantumToChar(QuantumRange-GetOpacityPixelComponent(p));
1804             p++;
1805           }
1806           if (image->previous == (Image *) NULL)
1807             {
1808               status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
1809                 image->rows);
1810               if (status == MagickFalse)
1811                 break;
1812             }
1813         }
1814         break;
1815       }
1816     }
1817     if ((type > 2) && (bmp_info.bits_per_pixel == 8))
1818       if (image_info->compression != NoCompression)
1819         {
1820           size_t
1821             length;
1822
1823           /*
1824             Convert run-length encoded raster pixels.
1825           */
1826           length=(size_t) (2*(bytes_per_line+2)*(image->rows+2)+2);
1827           bmp_data=(unsigned char *) NULL;
1828           if (~length >= bytes_per_line)
1829             bmp_data=(unsigned char *) AcquireQuantumMemory(length+
1830               bytes_per_line,sizeof(*bmp_data));
1831           if (bmp_data == (unsigned char *) NULL)
1832             {
1833               pixels=(unsigned char *) RelinquishMagickMemory(pixels);
1834               ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
1835             }
1836           bmp_info.file_size-=bmp_info.image_size;
1837           bmp_info.image_size=(unsigned int) EncodeImage(image,bytes_per_line,
1838             pixels,bmp_data);
1839           bmp_info.file_size+=bmp_info.image_size;
1840           pixels=(unsigned char *) RelinquishMagickMemory(pixels);
1841           pixels=bmp_data;
1842           bmp_info.compression=BI_RLE8;
1843         }
1844     /*
1845       Write BMP for Windows, all versions, 14-byte header.
1846     */
1847     if (image->debug != MagickFalse)
1848       {
1849         (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1850           "   Writing BMP version %.20g datastream",(double) type);
1851         if (image->storage_class == DirectClass)
1852           (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1853             "   Storage class=DirectClass");
1854         else
1855           (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1856             "   Storage class=PseudoClass");
1857         (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1858           "   Image depth=%.20g",(double) image->depth);
1859         if (image->matte != MagickFalse)
1860           (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1861             "   Matte=True");
1862         else
1863           (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1864             "   Matte=MagickFalse");
1865         (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1866           "   BMP bits_per_pixel=%.20g",(double) bmp_info.bits_per_pixel);
1867         switch ((int) bmp_info.compression)
1868         {
1869            case BI_RGB:
1870            {
1871              (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1872                "   Compression=BI_RGB");
1873              break;
1874            }
1875            case BI_RLE8:
1876            {
1877              (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1878                "   Compression=BI_RLE8");
1879              break;
1880            }
1881            case BI_BITFIELDS:
1882            {
1883              (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1884                "   Compression=BI_BITFIELDS");
1885              break;
1886            }
1887            default:
1888            {
1889              (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1890                "   Compression=UNKNOWN (%u)",bmp_info.compression);
1891              break;
1892            }
1893         }
1894         if (bmp_info.number_colors == 0)
1895           (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1896             "   Number_colors=unspecified");
1897         else
1898           (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1899             "   Number_colors=%u",bmp_info.number_colors);
1900       }
1901     (void) WriteBlob(image,2,(unsigned char *) "BM");
1902     (void) WriteBlobLSBLong(image,bmp_info.file_size);
1903     (void) WriteBlobLSBLong(image,bmp_info.ba_offset);  /* always 0 */
1904     (void) WriteBlobLSBLong(image,bmp_info.offset_bits);
1905     if (type == 2)
1906       {
1907         /*
1908           Write 12-byte version 2 bitmap header.
1909         */
1910         (void) WriteBlobLSBLong(image,bmp_info.size);
1911         (void) WriteBlobLSBShort(image,(unsigned short) bmp_info.width);
1912         (void) WriteBlobLSBShort(image,(unsigned short) bmp_info.height);
1913         (void) WriteBlobLSBShort(image,bmp_info.planes);
1914         (void) WriteBlobLSBShort(image,bmp_info.bits_per_pixel);
1915       }
1916     else
1917       {
1918         /*
1919           Write 40-byte version 3+ bitmap header.
1920         */
1921         (void) WriteBlobLSBLong(image,bmp_info.size);
1922         (void) WriteBlobLSBLong(image,(unsigned int) bmp_info.width);
1923         (void) WriteBlobLSBLong(image,(unsigned int) bmp_info.height);
1924         (void) WriteBlobLSBShort(image,bmp_info.planes);
1925         (void) WriteBlobLSBShort(image,bmp_info.bits_per_pixel);
1926         (void) WriteBlobLSBLong(image,bmp_info.compression);
1927         (void) WriteBlobLSBLong(image,bmp_info.image_size);
1928         (void) WriteBlobLSBLong(image,bmp_info.x_pixels);
1929         (void) WriteBlobLSBLong(image,bmp_info.y_pixels);
1930         (void) WriteBlobLSBLong(image,bmp_info.number_colors);
1931         (void) WriteBlobLSBLong(image,bmp_info.colors_important);
1932       }
1933     if ((type > 3) && ((image->matte != MagickFalse) ||
1934         (have_color_info != MagickFalse)))
1935       {
1936         /*
1937           Write the rest of the 108-byte BMP Version 4 header.
1938         */
1939         (void) WriteBlobLSBLong(image,0x00ff0000U);  /* Red mask */
1940         (void) WriteBlobLSBLong(image,0x0000ff00U);  /* Green mask */
1941         (void) WriteBlobLSBLong(image,0x000000ffU);  /* Blue mask */
1942         (void) WriteBlobLSBLong(image,0xff000000U);  /* Alpha mask */
1943         (void) WriteBlobLSBLong(image,0x00000001U);  /* CSType==Calib. RGB */
1944         (void) WriteBlobLSBLong(image,(unsigned int)
1945           image->chromaticity.red_primary.x*0x3ffffff);
1946         (void) WriteBlobLSBLong(image,(unsigned int)
1947           image->chromaticity.red_primary.y*0x3ffffff);
1948         (void) WriteBlobLSBLong(image,(unsigned int)
1949           (1.000f-(image->chromaticity.red_primary.x+
1950           image->chromaticity.red_primary.y)*0x3ffffff));
1951         (void) WriteBlobLSBLong(image,(unsigned int)
1952           image->chromaticity.green_primary.x*0x3ffffff);
1953         (void) WriteBlobLSBLong(image,(unsigned int)
1954           image->chromaticity.green_primary.y*0x3ffffff);
1955         (void) WriteBlobLSBLong(image,(unsigned int)
1956           (1.000f-(image->chromaticity.green_primary.x+
1957           image->chromaticity.green_primary.y)*0x3ffffff));
1958         (void) WriteBlobLSBLong(image,(unsigned int)
1959           image->chromaticity.blue_primary.x*0x3ffffff);
1960         (void) WriteBlobLSBLong(image,(unsigned int)
1961           image->chromaticity.blue_primary.y*0x3ffffff);
1962         (void) WriteBlobLSBLong(image,(unsigned int)
1963           (1.000f-(image->chromaticity.blue_primary.x+
1964           image->chromaticity.blue_primary.y)*0x3ffffff));
1965         (void) WriteBlobLSBLong(image,(unsigned int)
1966           bmp_info.gamma_scale.x*0xffff);
1967         (void) WriteBlobLSBLong(image,(unsigned int)
1968           bmp_info.gamma_scale.y*0xffff);
1969         (void) WriteBlobLSBLong(image,(unsigned int)
1970           bmp_info.gamma_scale.z*0xffff);
1971         if ((image->rendering_intent != UndefinedIntent) ||
1972             (profile != (StringInfo *) NULL))
1973           {
1974             ssize_t
1975               intent;
1976
1977             switch ((int) image->rendering_intent)
1978             {
1979               case SaturationIntent:
1980               {
1981                 intent=LCS_GM_BUSINESS;
1982                 break;
1983               }
1984               case RelativeIntent:
1985               {
1986                 intent=LCS_GM_GRAPHICS;
1987                 break;
1988               }
1989               case PerceptualIntent:
1990               {
1991                 intent=LCS_GM_IMAGES;
1992                 break;
1993               }
1994               case AbsoluteIntent:
1995               {
1996                 intent=LCS_GM_ABS_COLORIMETRIC;
1997                 break;
1998               }
1999               default:
2000               {
2001                 intent=0;
2002                 break;
2003               }
2004             }
2005             (void) WriteBlobLSBLong(image,(unsigned int) intent);
2006             (void) WriteBlobLSBLong(image,0x00);  /* dummy profile data */
2007             (void) WriteBlobLSBLong(image,0x00);  /* dummy profile length */
2008             (void) WriteBlobLSBLong(image,0x00);  /* reserved */
2009           }
2010       }
2011     if (image->storage_class == PseudoClass)
2012       {
2013         unsigned char
2014           *bmp_colormap;
2015
2016         /*
2017           Dump colormap to file.
2018         */
2019         if (image->debug != MagickFalse)
2020           (void) LogMagickEvent(CoderEvent,GetMagickModule(),
2021             "  Colormap: %.20g entries",(double) image->colors);
2022         bmp_colormap=(unsigned char *) AcquireQuantumMemory((size_t) (1UL <<
2023           bmp_info.bits_per_pixel),4*sizeof(*bmp_colormap));
2024         if (bmp_colormap == (unsigned char *) NULL)
2025           ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
2026         q=bmp_colormap;
2027         for (i=0; i < (ssize_t) MagickMin((ssize_t) image->colors,(ssize_t) bmp_info.number_colors); i++)
2028         {
2029           *q++=ScaleQuantumToChar(image->colormap[i].blue);
2030           *q++=ScaleQuantumToChar(image->colormap[i].green);
2031           *q++=ScaleQuantumToChar(image->colormap[i].red);
2032           if (type > 2)
2033             *q++=(unsigned char) 0x0;
2034         }
2035         for ( ; i < (ssize_t) (1UL << bmp_info.bits_per_pixel); i++)
2036         {
2037           *q++=(unsigned char) 0x00;
2038           *q++=(unsigned char) 0x00;
2039           *q++=(unsigned char) 0x00;
2040           if (type > 2)
2041             *q++=(unsigned char) 0x00;
2042         }
2043         if (type <= 2)
2044           (void) WriteBlob(image,(size_t) (3*(1L << bmp_info.bits_per_pixel)),
2045             bmp_colormap);
2046         else
2047           (void) WriteBlob(image,(size_t) (4*(1L << bmp_info.bits_per_pixel)),
2048             bmp_colormap);
2049         bmp_colormap=(unsigned char *) RelinquishMagickMemory(bmp_colormap);
2050       }
2051     if (image->debug != MagickFalse)
2052       (void) LogMagickEvent(CoderEvent,GetMagickModule(),
2053         "  Pixels:  %u bytes",bmp_info.image_size);
2054     (void) WriteBlob(image,(size_t) bmp_info.image_size,pixels);
2055     pixels=(unsigned char *) RelinquishMagickMemory(pixels);
2056     if (GetNextImageInList(image) == (Image *) NULL)
2057       break;
2058     image=SyncNextImageInList(image);
2059     status=SetImageProgress(image,SaveImagesTag,scene++,
2060       GetImageListLength(image));
2061     if (status == MagickFalse)
2062       break;
2063   } while (image_info->adjoin != MagickFalse);
2064   (void) CloseBlob(image);
2065   return(MagickTrue);
2066 }