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