2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
13 % Read/Write Microsoft Windows Bitmap Image Format %
17 % Glenn Randers-Pehrson %
21 % Copyright 1999-2011 ImageMagick Studio LLC, a non-profit organization %
22 % dedicated to making software imaging solutions freely available. %
24 % You may not use this file except in compliance with the License. You may %
25 % obtain a copy of the License at %
27 % http://www.imagemagick.org/script/license.php %
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. %
35 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
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"
71 Macro definitions (from Windows wingdi.h).
77 #if !defined(MAGICKCORE_WINDOWS_SUPPORT) || defined(__MINGW32__)
81 #define BI_BITFIELDS 3
83 #define LCS_CALIBRATED_RBG 0
85 #define LCS_WINDOWS_COLOR_SPACE 2
86 #define PROFILE_LINKED 3
87 #define PROFILE_EMBEDDED 4
89 #define LCS_GM_BUSINESS 1 /* Saturation */
90 #define LCS_GM_GRAPHICS 2 /* Relative */
91 #define LCS_GM_IMAGES 4 /* Perceptual */
92 #define LCS_GM_ABS_COLORIMETRIC 8 /* Absolute */
98 typedef struct _BMPInfo
137 Forward declarations.
139 static MagickBooleanType
140 WriteBMPImage(const ImageInfo *,Image *,ExceptionInfo *);
143 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
147 % D e c o d e I m a g e %
151 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
153 % DecodeImage unpacks the packed image pixels into runlength-encoded
156 % The format of the DecodeImage method is:
158 % MagickBooleanType DecodeImage(Image *image,
159 % const size_t compression,unsigned char *pixels)
161 % A description of each parameter follows:
163 % o image: the address of a structure of type Image.
165 % o compression: Zero means uncompressed. A value of 1 means the
166 % compressed pixels are runlength encoded for a 256-color bitmap.
167 % A value of 2 means a 16-color bitmap. A value of 3 means bitfields
170 % o pixels: The address of a byte (8 bits) array of pixel data created by
171 % the decoding process.
175 static inline ssize_t MagickAbsoluteValue(const ssize_t x)
182 static inline size_t MagickMax(const size_t x,const size_t y)
189 static inline ssize_t MagickMin(const ssize_t x,const ssize_t y)
196 static MagickBooleanType DecodeImage(Image *image,const size_t compression,
197 unsigned char *pixels)
206 register unsigned char
216 assert(image != (Image *) NULL);
217 assert(image->signature == MagickSignature);
218 if (image->debug != MagickFalse)
219 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
220 assert(pixels != (unsigned char *) NULL);
221 (void) ResetMagickMemory(pixels,0,(size_t) image->columns*image->rows*
226 q=pixels+(size_t) image->columns*image->rows;
227 for (y=0; y < (ssize_t) image->rows; )
229 if ((p < pixels) || (p >= q))
231 count=ReadBlobByte(image);
239 count=MagickMin(count,(int) (q-p));
240 byte=(unsigned char) ReadBlobByte(image);
241 if (compression == BI_RLE8)
243 for (i=0; i < count; i++)
244 *p++=(unsigned char) byte;
248 for (i=0; i < count; i++)
250 ((i & 0x01) != 0 ? (byte & 0x0f) : ((byte >> 4) & 0x0f));
259 count=ReadBlobByte(image);
271 p=pixels+y*image->columns;
279 x+=ReadBlobByte(image);
280 y+=ReadBlobByte(image);
281 p=pixels+y*image->columns+x;
289 count=MagickMin(count,(int) (q-p));
290 if (compression == BI_RLE8)
291 for (i=0; i < count; i++)
292 *p++=(unsigned char) ReadBlobByte(image);
294 for (i=0; i < count; i++)
297 byte=(unsigned char) ReadBlobByte(image);
299 ((i & 0x01) != 0 ? (byte & 0x0f) : ((byte >> 4) & 0x0f));
305 if (compression == BI_RLE8)
307 if ((count & 0x01) != 0)
308 (void) ReadBlobByte(image);
311 if (((count & 0x03) == 1) || ((count & 0x03) == 2))
312 (void) ReadBlobByte(image);
317 if (SetImageProgress(image,LoadImageTag,y,image->rows) == MagickFalse)
320 (void) ReadBlobByte(image); /* end of line */
321 (void) ReadBlobByte(image);
326 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
330 % E n c o d e I m a g e %
334 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
336 % EncodeImage compresses pixels using a runlength encoded format.
338 % The format of the EncodeImage method is:
340 % static MagickBooleanType EncodeImage(Image *image,
341 % const size_t bytes_per_line,const unsigned char *pixels,
342 % unsigned char *compressed_pixels)
344 % A description of each parameter follows:
346 % o image: The image.
348 % o bytes_per_line: the number of bytes in a scanline of compressed pixels
350 % o pixels: The address of a byte (8 bits) array of pixel data created by
351 % the compression process.
353 % o compressed_pixels: The address of a byte (8 bits) array of compressed
357 static size_t EncodeImage(Image *image,const size_t bytes_per_line,
358 const unsigned char *pixels,unsigned char *compressed_pixels)
363 register const unsigned char
370 register unsigned char
377 Runlength encode pixels.
379 assert(image != (Image *) NULL);
380 assert(image->signature == MagickSignature);
381 if (image->debug != MagickFalse)
382 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
383 assert(pixels != (const unsigned char *) NULL);
384 assert(compressed_pixels != (unsigned char *) NULL);
388 for (y=0; y < (ssize_t) image->rows; y++)
390 for (x=0; x < (ssize_t) bytes_per_line; x+=i)
395 for (i=1; ((x+i) < (ssize_t) bytes_per_line); i++)
396 if ((i == 255) || (*(p+i) != *p))
398 *q++=(unsigned char) i;
405 *q++=(unsigned char) 0x00;
406 *q++=(unsigned char) 0x00;
407 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
409 if (status == MagickFalse)
415 *q++=(unsigned char) 0x00;
416 *q++=(unsigned char) 0x01;
417 return((size_t) (q-compressed_pixels));
421 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
429 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
431 % IsBMP() returns MagickTrue if the image format type, identified by the
432 % magick string, is BMP.
434 % The format of the IsBMP method is:
436 % MagickBooleanType IsBMP(const unsigned char *magick,const size_t length)
438 % A description of each parameter follows:
440 % o magick: compare image format pattern against these bytes.
442 % o length: Specifies the length of the magick string.
445 static MagickBooleanType IsBMP(const unsigned char *magick,const size_t length)
449 if ((LocaleNCompare((char *) magick,"BA",2) == 0) ||
450 (LocaleNCompare((char *) magick,"BM",2) == 0) ||
451 (LocaleNCompare((char *) magick,"IC",2) == 0) ||
452 (LocaleNCompare((char *) magick,"PI",2) == 0) ||
453 (LocaleNCompare((char *) magick,"CI",2) == 0) ||
454 (LocaleNCompare((char *) magick,"CP",2) == 0))
460 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
464 % R e a d B M P I m a g e %
468 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
470 % ReadBMPImage() reads a Microsoft Windows bitmap image file, Version
471 % 2, 3 (for Windows or NT), or 4, and returns it. It allocates the memory
472 % necessary for the new Image structure and returns a pointer to the new
475 % The format of the ReadBMPImage method is:
477 % image=ReadBMPImage(image_info)
479 % A description of each parameter follows:
481 % o image_info: the image info.
483 % o exception: return any errors or warnings in this structure.
487 static Image *ReadBMPImage(const ImageInfo *image_info,ExceptionInfo *exception)
512 register unsigned char
535 assert(image_info != (const ImageInfo *) NULL);
536 assert(image_info->signature == MagickSignature);
537 if (image_info->debug != MagickFalse)
538 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
539 image_info->filename);
540 assert(exception != (ExceptionInfo *) NULL);
541 assert(exception->signature == MagickSignature);
542 image=AcquireImage(image_info,exception);
543 status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception);
544 if (status == MagickFalse)
546 image=DestroyImageList(image);
547 return((Image *) NULL);
550 Determine if this a BMP file.
552 (void) ResetMagickMemory(&bmp_info,0,sizeof(bmp_info));
553 bmp_info.ba_offset=0;
555 count=ReadBlob(image,2,magick);
569 Verify BMP identifier.
571 if (bmp_info.ba_offset == 0)
572 start_position=TellBlob(image)-2;
573 bmp_info.ba_offset=0;
574 while (LocaleNCompare((char *) magick,"BA",2) == 0)
576 bmp_info.file_size=ReadBlobLSBLong(image);
577 bmp_info.ba_offset=ReadBlobLSBLong(image);
578 bmp_info.offset_bits=ReadBlobLSBLong(image);
579 count=ReadBlob(image,2,magick);
583 if (image->debug != MagickFalse)
584 (void) LogMagickEvent(CoderEvent,GetMagickModule()," Magick: %c%c",
585 magick[0],magick[1]);
586 if ((count == 0) || ((LocaleNCompare((char *) magick,"BM",2) != 0) &&
587 (LocaleNCompare((char *) magick,"CI",2) != 0)))
588 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
589 bmp_info.file_size=ReadBlobLSBLong(image);
590 (void) ReadBlobLSBLong(image);
591 bmp_info.offset_bits=ReadBlobLSBLong(image);
592 bmp_info.size=ReadBlobLSBLong(image);
593 if (image->debug != MagickFalse)
594 (void) LogMagickEvent(CoderEvent,GetMagickModule()," BMP size: %u",
596 if (bmp_info.size == 12)
601 bmp_info.width=(short) ReadBlobLSBShort(image);
602 bmp_info.height=(short) ReadBlobLSBShort(image);
603 bmp_info.planes=ReadBlobLSBShort(image);
604 bmp_info.bits_per_pixel=ReadBlobLSBShort(image);
607 bmp_info.number_colors=0;
608 bmp_info.compression=BI_RGB;
609 bmp_info.image_size=0;
610 bmp_info.alpha_mask=0;
611 if (image->debug != MagickFalse)
613 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
614 " Format: OS/2 Bitmap");
615 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
616 " Geometry: %.20gx%.20g",(double) bmp_info.width,(double)
623 Microsoft Windows BMP image file.
625 if (bmp_info.size < 40)
626 ThrowReaderException(CorruptImageError,"NonOS2HeaderSizeError");
627 bmp_info.width=(int) ReadBlobLSBLong(image);
628 bmp_info.height=(int) ReadBlobLSBLong(image);
629 bmp_info.planes=ReadBlobLSBShort(image);
630 bmp_info.bits_per_pixel=ReadBlobLSBShort(image);
631 bmp_info.compression=ReadBlobLSBLong(image);
632 bmp_info.image_size=ReadBlobLSBLong(image);
633 bmp_info.x_pixels=ReadBlobLSBLong(image);
634 bmp_info.y_pixels=ReadBlobLSBLong(image);
635 bmp_info.number_colors=ReadBlobLSBLong(image);
636 bmp_info.colors_important=ReadBlobLSBLong(image);
639 if (image->debug != MagickFalse)
641 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
642 " Format: MS Windows bitmap");
643 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
644 " Geometry: %.20gx%.20g",(double) bmp_info.width,(double)
646 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
647 " Bits per pixel: %.20g",(double) bmp_info.bits_per_pixel);
648 switch ((int) bmp_info.compression)
652 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
653 " Compression: BI_RGB");
658 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
659 " Compression: BI_RLE4");
664 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
665 " Compression: BI_RLE8");
670 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
671 " Compression: BI_BITFIELDS");
676 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
677 " Compression: BI_PNG");
682 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
683 " Compression: BI_JPEG");
688 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
689 " Compression: UNKNOWN (%u)",bmp_info.compression);
692 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
693 " Number of colors: %u",bmp_info.number_colors);
695 bmp_info.red_mask=ReadBlobLSBLong(image);
696 bmp_info.green_mask=ReadBlobLSBLong(image);
697 bmp_info.blue_mask=ReadBlobLSBLong(image);
698 if (bmp_info.size > 40)
704 Read color management information.
706 bmp_info.alpha_mask=ReadBlobLSBLong(image);
707 bmp_info.colorspace=(int) ReadBlobLSBLong(image);
709 Decode 2^30 fixed point formatted CIE primaries.
711 bmp_info.red_primary.x=(double) ReadBlobLSBLong(image)/0x3ffffff;
712 bmp_info.red_primary.y=(double) ReadBlobLSBLong(image)/0x3ffffff;
713 bmp_info.red_primary.z=(double) ReadBlobLSBLong(image)/0x3ffffff;
714 bmp_info.green_primary.x=(double) ReadBlobLSBLong(image)/0x3ffffff;
715 bmp_info.green_primary.y=(double) ReadBlobLSBLong(image)/0x3ffffff;
716 bmp_info.green_primary.z=(double) ReadBlobLSBLong(image)/0x3ffffff;
717 bmp_info.blue_primary.x=(double) ReadBlobLSBLong(image)/0x3ffffff;
718 bmp_info.blue_primary.y=(double) ReadBlobLSBLong(image)/0x3ffffff;
719 bmp_info.blue_primary.z=(double) ReadBlobLSBLong(image)/0x3ffffff;
720 sum=bmp_info.red_primary.x+bmp_info.red_primary.y+
721 bmp_info.red_primary.z;
722 image->chromaticity.red_primary.x/=sum;
723 image->chromaticity.red_primary.y/=sum;
724 sum=bmp_info.green_primary.x+bmp_info.green_primary.y+
725 bmp_info.green_primary.z;
726 image->chromaticity.green_primary.x/=sum;
727 image->chromaticity.green_primary.y/=sum;
728 sum=bmp_info.blue_primary.x+bmp_info.blue_primary.y+
729 bmp_info.blue_primary.z;
730 image->chromaticity.blue_primary.x/=sum;
731 image->chromaticity.blue_primary.y/=sum;
733 Decode 16^16 fixed point formatted gamma_scales.
735 bmp_info.gamma_scale.x=(double) ReadBlobLSBLong(image)/0xffff;
736 bmp_info.gamma_scale.y=(double) ReadBlobLSBLong(image)/0xffff;
737 bmp_info.gamma_scale.z=(double) ReadBlobLSBLong(image)/0xffff;
739 Compute a single gamma from the BMP 3-channel gamma.
741 image->gamma=(bmp_info.gamma_scale.x+bmp_info.gamma_scale.y+
742 bmp_info.gamma_scale.z)/3.0;
744 if (bmp_info.size > 108)
750 Read BMP Version 5 color management information.
752 intent=ReadBlobLSBLong(image);
753 switch ((int) intent)
755 case LCS_GM_BUSINESS:
757 image->rendering_intent=SaturationIntent;
760 case LCS_GM_GRAPHICS:
762 image->rendering_intent=RelativeIntent;
767 image->rendering_intent=PerceptualIntent;
770 case LCS_GM_ABS_COLORIMETRIC:
772 image->rendering_intent=AbsoluteIntent;
776 profile_data=ReadBlobLSBLong(image);
777 profile_size=ReadBlobLSBLong(image);
780 (void) ReadBlobLSBLong(image); /* Reserved byte */
783 if ((MagickSizeType) bmp_info.file_size > GetBlobSize(image))
784 (void) ThrowMagickException(exception,GetMagickModule(),CorruptImageError,
785 "LengthAndFilesizeDoNotMatch","`%s'",image->filename);
787 if ((MagickSizeType) bmp_info.file_size < GetBlobSize(image))
788 (void) ThrowMagickException(exception,GetMagickModule(),
789 CorruptImageWarning,"LengthAndFilesizeDoNotMatch","`%s'",
791 if (bmp_info.width <= 0)
792 ThrowReaderException(CorruptImageError,"NegativeOrZeroImageSize");
793 if (bmp_info.height == 0)
794 ThrowReaderException(CorruptImageError,"NegativeOrZeroImageSize");
795 if (bmp_info.planes != 1)
796 ThrowReaderException(CorruptImageError,"StaticPlanesValueNotEqualToOne");
797 if ((bmp_info.bits_per_pixel != 1) && (bmp_info.bits_per_pixel != 4) &&
798 (bmp_info.bits_per_pixel != 8) && (bmp_info.bits_per_pixel != 16) &&
799 (bmp_info.bits_per_pixel != 24) && (bmp_info.bits_per_pixel != 32))
800 ThrowReaderException(CorruptImageError,"UnrecognizedBitsPerPixel");
801 if (bmp_info.number_colors > (1U << bmp_info.bits_per_pixel))
803 if (bmp_info.bits_per_pixel < 24)
804 ThrowReaderException(CorruptImageError,"UnrecognizedNumberOfColors");
805 bmp_info.number_colors=0;
807 if ((bmp_info.compression == 1) && (bmp_info.bits_per_pixel != 8))
808 ThrowReaderException(CorruptImageError,"UnrecognizedBitsPerPixel");
809 if ((bmp_info.compression == 2) && (bmp_info.bits_per_pixel != 4))
810 ThrowReaderException(CorruptImageError,"UnrecognizedBitsPerPixel");
811 if ((bmp_info.compression == 3) && (bmp_info.bits_per_pixel < 16))
812 ThrowReaderException(CorruptImageError,"UnrecognizedBitsPerPixel");
813 switch (bmp_info.compression)
821 ThrowReaderException(CoderError,"JPEGCompressNotSupported");
823 ThrowReaderException(CoderError,"PNGCompressNotSupported");
825 ThrowReaderException(CorruptImageError,"UnrecognizedImageCompression");
827 image->columns=(size_t) MagickAbsoluteValue(bmp_info.width);
828 image->rows=(size_t) MagickAbsoluteValue(bmp_info.height);
829 image->depth=bmp_info.bits_per_pixel <= 8 ? bmp_info.bits_per_pixel : 8;
830 if ((bmp_info.bits_per_pixel == 16) ||
831 (bmp_info.bits_per_pixel == 32))
832 image->matte=bmp_info.alpha_mask != 0 ? MagickTrue : MagickFalse;
833 if ((bmp_info.number_colors != 0) || (bmp_info.bits_per_pixel < 16))
838 image->storage_class=PseudoClass;
839 image->colors=bmp_info.number_colors;
841 if (image->colors == 0)
842 image->colors=one << bmp_info.bits_per_pixel;
844 if (image->storage_class == PseudoClass)
853 Read BMP raster colormap.
855 if (image->debug != MagickFalse)
856 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
857 " Reading colormap of %.20g colors",(double) image->colors);
858 if (AcquireImageColormap(image,image->colors,exception) == MagickFalse)
859 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
860 bmp_colormap=(unsigned char *) AcquireQuantumMemory((size_t)
861 image->colors,4*sizeof(*bmp_colormap));
862 if (bmp_colormap == (unsigned char *) NULL)
863 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
864 if ((bmp_info.size == 12) || (bmp_info.size == 64))
868 offset=SeekBlob(image,start_position+14+bmp_info.size,SEEK_SET);
870 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
871 count=ReadBlob(image,packet_size*image->colors,bmp_colormap);
872 if (count != (ssize_t) (packet_size*image->colors))
873 ThrowReaderException(CorruptImageError,"InsufficientImageDataInFile");
875 for (i=0; i < (ssize_t) image->colors; i++)
877 image->colormap[i].blue=ScaleCharToQuantum(*p++);
878 image->colormap[i].green=ScaleCharToQuantum(*p++);
879 image->colormap[i].red=ScaleCharToQuantum(*p++);
880 if (packet_size == 4)
883 bmp_colormap=(unsigned char *) RelinquishMagickMemory(bmp_colormap);
885 if ((image_info->ping != MagickFalse) && (image_info->number_scenes != 0))
886 if (image->scene >= (image_info->scene+image_info->number_scenes-1))
891 offset=SeekBlob(image,start_position+bmp_info.offset_bits,SEEK_SET);
893 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
894 if (bmp_info.compression == BI_RLE4)
895 bmp_info.bits_per_pixel<<=1;
896 bytes_per_line=4*((image->columns*bmp_info.bits_per_pixel+31)/32);
897 length=(size_t) bytes_per_line*image->rows;
898 pixels=(unsigned char *) AcquireQuantumMemory((size_t) image->rows,
899 MagickMax(bytes_per_line,image->columns+256UL)*sizeof(*pixels));
900 if (pixels == (unsigned char *) NULL)
901 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
902 if ((bmp_info.compression == BI_RGB) ||
903 (bmp_info.compression == BI_BITFIELDS))
905 if (image->debug != MagickFalse)
906 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
907 " Reading pixels (%.20g bytes)",(double) length);
908 count=ReadBlob(image,length,pixels);
909 if (count != (ssize_t) length)
911 pixels=(unsigned char *) RelinquishMagickMemory(pixels);
912 ThrowReaderException(CorruptImageError,
913 "InsufficientImageDataInFile");
919 Convert run-length encoded raster pixels.
921 status=DecodeImage(image,bmp_info.compression,pixels);
922 if (status == MagickFalse)
924 pixels=(unsigned char *) RelinquishMagickMemory(pixels);
925 ThrowReaderException(CorruptImageError,
926 "UnableToRunlengthDecodeImage");
930 Initialize image structure.
932 image->resolution.x=(double) bmp_info.x_pixels/100.0;
933 image->resolution.y=(double) bmp_info.y_pixels/100.0;
934 image->units=PixelsPerCentimeterResolution;
936 Convert BMP raster image to pixel packets.
938 if (bmp_info.compression == BI_RGB)
940 bmp_info.alpha_mask=0;
941 bmp_info.red_mask=0x00ff0000U;
942 bmp_info.green_mask=0x0000ff00U;
943 bmp_info.blue_mask=0x000000ffU;
944 if (bmp_info.bits_per_pixel == 16)
949 bmp_info.red_mask=0x00007c00U;
950 bmp_info.green_mask=0x000003e0U;
951 bmp_info.blue_mask=0x0000001fU;
954 if ((bmp_info.bits_per_pixel == 16) || (bmp_info.bits_per_pixel == 32))
960 Get shift and quantum bits info from bitfield masks.
962 (void) ResetMagickMemory(&shift,0,sizeof(shift));
963 (void) ResetMagickMemory(&quantum_bits,0,sizeof(quantum_bits));
964 if (bmp_info.red_mask != 0)
965 while (((bmp_info.red_mask << shift.red) & 0x80000000UL) == 0)
967 if (bmp_info.green_mask != 0)
968 while (((bmp_info.green_mask << shift.green) & 0x80000000UL) == 0)
970 if (bmp_info.blue_mask != 0)
971 while (((bmp_info.blue_mask << shift.blue) & 0x80000000UL) == 0)
973 if (bmp_info.alpha_mask != 0)
974 while (((bmp_info.alpha_mask << shift.alpha) & 0x80000000UL) == 0)
977 while (((bmp_info.red_mask << sample) & 0x80000000UL) != 0)
979 quantum_bits.red=(Quantum) (sample-shift.red);
981 while (((bmp_info.green_mask << sample) & 0x80000000UL) != 0)
983 quantum_bits.green=(Quantum) (sample-shift.green);
985 while (((bmp_info.blue_mask << sample) & 0x80000000UL) != 0)
987 quantum_bits.blue=(Quantum) (sample-shift.blue);
989 while (((bmp_info.alpha_mask << sample) & 0x80000000UL) != 0)
991 quantum_bits.alpha=(Quantum) (sample-shift.alpha);
993 switch (bmp_info.bits_per_pixel)
998 Convert bitmap scanline.
1000 for (y=(ssize_t) image->rows-1; y >= 0; y--)
1002 p=pixels+(image->rows-y-1)*bytes_per_line;
1003 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
1004 if (q == (Quantum *) NULL)
1006 for (x=0; x < ((ssize_t) image->columns-7); x+=8)
1008 for (bit=0; bit < 8; bit++)
1010 index=(Quantum) (((*p) & (0x80 >> bit)) != 0 ? 0x01 : 0x00);
1011 SetPixelIndex(image,index,q);
1012 q+=GetPixelChannels(image);
1016 if ((image->columns % 8) != 0)
1018 for (bit=0; bit < (image->columns % 8); bit++)
1020 index=(Quantum) (((*p) & (0x80 >> bit)) != 0 ? 0x01 : 0x00);
1021 SetPixelIndex(image,index,q);
1022 q+=GetPixelChannels(image);
1026 if (SyncAuthenticPixels(image,exception) == MagickFalse)
1028 if (image->previous == (Image *) NULL)
1030 status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y,
1032 if (status == MagickFalse)
1036 (void) SyncImage(image,exception);
1042 Convert PseudoColor scanline.
1044 for (y=(ssize_t) image->rows-1; y >= 0; y--)
1046 p=pixels+(image->rows-y-1)*bytes_per_line;
1047 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
1048 if (q == (Quantum *) NULL)
1050 for (x=0; x < ((ssize_t) image->columns-1); x+=2)
1052 index=ConstrainColormapIndex(image,(*p >> 4) & 0x0f,exception);
1053 SetPixelIndex(image,index,q);
1054 q+=GetPixelChannels(image);
1055 index=ConstrainColormapIndex(image,*p & 0x0f,exception);
1056 SetPixelIndex(image,index,q);
1057 q+=GetPixelChannels(image);
1060 if ((image->columns % 2) != 0)
1062 index=ConstrainColormapIndex(image,(*p >> 4) & 0xf,exception);
1063 SetPixelIndex(image,index,q);
1064 q+=GetPixelChannels(image);
1067 if (SyncAuthenticPixels(image,exception) == MagickFalse)
1069 if (image->previous == (Image *) NULL)
1071 status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y,
1073 if (status == MagickFalse)
1077 (void) SyncImage(image,exception);
1083 Convert PseudoColor scanline.
1085 if ((bmp_info.compression == BI_RLE8) ||
1086 (bmp_info.compression == BI_RLE4))
1087 bytes_per_line=image->columns;
1088 for (y=(ssize_t) image->rows-1; y >= 0; y--)
1090 p=pixels+(image->rows-y-1)*bytes_per_line;
1091 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
1092 if (q == (Quantum *) NULL)
1094 for (x = (ssize_t)image->columns; x != 0; --x)
1096 index=ConstrainColormapIndex(image,*p++,exception);
1097 SetPixelIndex(image,index,q);
1098 q+=GetPixelChannels(image);
1100 if (SyncAuthenticPixels(image,exception) == MagickFalse)
1102 offset=(MagickOffsetType) (image->rows-y-1);
1103 if (image->previous == (Image *) NULL)
1105 status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y,
1107 if (status == MagickFalse)
1111 (void) SyncImage(image,exception);
1120 Convert bitfield encoded 16-bit PseudoColor scanline.
1122 if (bmp_info.compression != BI_RGB &&
1123 bmp_info.compression != BI_BITFIELDS)
1125 pixels=(unsigned char *) RelinquishMagickMemory(pixels);
1126 ThrowReaderException(CorruptImageError,
1127 "UnrecognizedImageCompression");
1129 bytes_per_line=2*(image->columns+image->columns % 2);
1130 image->storage_class=DirectClass;
1131 for (y=(ssize_t) image->rows-1; y >= 0; y--)
1133 p=pixels+(image->rows-y-1)*bytes_per_line;
1134 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
1135 if (q == (Quantum *) NULL)
1137 for (x=0; x < (ssize_t) image->columns; x++)
1139 pixel=(size_t) (*p++);
1141 red=((pixel & bmp_info.red_mask) << shift.red) >> 16;
1142 if (quantum_bits.red == 5)
1143 red|=((red & 0xe000) >> 5);
1144 if (quantum_bits.red <= 8)
1145 red|=((red & 0xff00) >> 8);
1146 green=((pixel & bmp_info.green_mask) << shift.green) >> 16;
1147 if (quantum_bits.green == 5)
1148 green|=((green & 0xe000) >> 5);
1149 if (quantum_bits.green == 6)
1150 green|=((green & 0xc000) >> 6);
1151 if (quantum_bits.green <= 8)
1152 green|=((green & 0xff00) >> 8);
1153 blue=((pixel & bmp_info.blue_mask) << shift.blue) >> 16;
1154 if (quantum_bits.blue == 5)
1155 blue|=((blue & 0xe000) >> 5);
1156 if (quantum_bits.blue <= 8)
1157 blue|=((blue & 0xff00) >> 8);
1158 opacity=((pixel & bmp_info.alpha_mask) << shift.alpha) >> 16;
1159 if (quantum_bits.alpha <= 8)
1160 opacity|=((opacity & 0xff00) >> 8);
1161 SetPixelRed(image,ScaleShortToQuantum((unsigned short) red),q);
1162 SetPixelGreen(image,ScaleShortToQuantum((unsigned short) green),q);
1163 SetPixelBlue(image,ScaleShortToQuantum((unsigned short) blue),q);
1164 SetPixelAlpha(image,OpaqueAlpha,q);
1165 if (image->matte != MagickFalse)
1166 SetPixelAlpha(image,
1167 ScaleShortToQuantum((unsigned short) opacity),q);
1168 q+=GetPixelChannels(image);
1170 if (SyncAuthenticPixels(image,exception) == MagickFalse)
1172 offset=(MagickOffsetType) (image->rows-y-1);
1173 if (image->previous == (Image *) NULL)
1175 status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y,
1177 if (status == MagickFalse)
1186 Convert DirectColor scanline.
1188 bytes_per_line=4*((image->columns*24+31)/32);
1189 for (y=(ssize_t) image->rows-1; y >= 0; y--)
1191 p=pixels+(image->rows-y-1)*bytes_per_line;
1192 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
1193 if (q == (Quantum *) NULL)
1195 for (x=0; x < (ssize_t) image->columns; x++)
1197 SetPixelBlue(image,ScaleCharToQuantum(*p++),q);
1198 SetPixelGreen(image,ScaleCharToQuantum(*p++),q);
1199 SetPixelRed(image,ScaleCharToQuantum(*p++),q);
1200 SetPixelAlpha(image,OpaqueAlpha,q);
1201 q+=GetPixelChannels(image);
1203 if (SyncAuthenticPixels(image,exception) == MagickFalse)
1205 offset=(MagickOffsetType) (image->rows-y-1);
1206 if (image->previous == (Image *) NULL)
1208 status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y,
1210 if (status == MagickFalse)
1219 Convert bitfield encoded DirectColor scanline.
1221 if ((bmp_info.compression != BI_RGB) &&
1222 (bmp_info.compression != BI_BITFIELDS))
1224 pixels=(unsigned char *) RelinquishMagickMemory(pixels);
1225 ThrowReaderException(CorruptImageError,
1226 "UnrecognizedImageCompression");
1228 bytes_per_line=4*(image->columns);
1229 for (y=(ssize_t) image->rows-1; y >= 0; y--)
1234 p=pixels+(image->rows-y-1)*bytes_per_line;
1235 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
1236 if (q == (Quantum *) NULL)
1238 for (x=0; x < (ssize_t) image->columns; x++)
1240 pixel=(size_t) (*p++);
1242 pixel|=(*p++ << 16);
1243 pixel|=(*p++ << 24);
1244 red=((pixel & bmp_info.red_mask) << shift.red) >> 16;
1245 if (quantum_bits.red == 8)
1247 green=((pixel & bmp_info.green_mask) << shift.green) >> 16;
1248 if (quantum_bits.green == 8)
1249 green|=(green >> 8);
1250 blue=((pixel & bmp_info.blue_mask) << shift.blue) >> 16;
1251 if (quantum_bits.blue == 8)
1253 opacity=((pixel & bmp_info.alpha_mask) << shift.alpha) >> 16;
1254 if (quantum_bits.alpha == 8)
1255 opacity|=(opacity >> 8);
1256 SetPixelRed(image,ScaleShortToQuantum((unsigned short) red),q);
1257 SetPixelGreen(image,ScaleShortToQuantum((unsigned short) green),q);
1258 SetPixelBlue(image,ScaleShortToQuantum((unsigned short) blue),q);
1259 SetPixelAlpha(image,OpaqueAlpha,q);
1260 if (image->matte != MagickFalse)
1261 SetPixelAlpha(image,
1262 ScaleShortToQuantum((unsigned short) opacity),q);
1263 q+=GetPixelChannels(image);
1265 if (SyncAuthenticPixels(image,exception) == MagickFalse)
1267 offset=(MagickOffsetType) (image->rows-y-1);
1268 if (image->previous == (Image *) NULL)
1270 status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y,
1272 if (status == MagickFalse)
1280 pixels=(unsigned char *) RelinquishMagickMemory(pixels);
1281 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
1284 pixels=(unsigned char *) RelinquishMagickMemory(pixels);
1285 if (EOFBlob(image) != MagickFalse)
1287 ThrowFileException(exception,CorruptImageError,"UnexpectedEndOfFile",
1291 if (bmp_info.height < 0)
1297 Correct image orientation.
1299 flipped_image=FlipImage(image,exception);
1300 if (flipped_image != (Image *) NULL)
1302 DuplicateBlob(flipped_image,image);
1303 image=DestroyImage(image);
1304 image=flipped_image;
1308 Proceed to next image.
1310 if (image_info->number_scenes != 0)
1311 if (image->scene >= (image_info->scene+image_info->number_scenes-1))
1314 if (bmp_info.ba_offset != 0)
1316 offset=SeekBlob(image,(MagickOffsetType) bmp_info.ba_offset,SEEK_SET);
1318 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
1320 count=ReadBlob(image,2,magick);
1321 if ((count == 2) && (IsBMP(magick,2) != MagickFalse))
1324 Acquire next image structure.
1326 AcquireNextImage(image_info,image,exception);
1327 if (GetNextImageInList(image) == (Image *) NULL)
1329 image=DestroyImageList(image);
1330 return((Image *) NULL);
1332 image=SyncNextImageInList(image);
1333 status=SetImageProgress(image,LoadImagesTag,TellBlob(image),
1334 GetBlobSize(image));
1335 if (status == MagickFalse)
1338 } while (IsBMP(magick,2) != MagickFalse);
1339 (void) CloseBlob(image);
1340 return(GetFirstImageInList(image));
1344 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1348 % R e g i s t e r B M P I m a g e %
1352 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1354 % RegisterBMPImage() adds attributes for the BMP image format to
1355 % the list of supported formats. The attributes include the image format
1356 % tag, a method to read and/or write the format, whether the format
1357 % supports the saving of more than one frame to the same file or blob,
1358 % whether the format supports native in-memory I/O, and a brief
1359 % description of the format.
1361 % The format of the RegisterBMPImage method is:
1363 % size_t RegisterBMPImage(void)
1366 ModuleExport size_t RegisterBMPImage(void)
1371 entry=SetMagickInfo("BMP");
1372 entry->decoder=(DecodeImageHandler *) ReadBMPImage;
1373 entry->encoder=(EncodeImageHandler *) WriteBMPImage;
1374 entry->magick=(IsImageFormatHandler *) IsBMP;
1375 entry->description=ConstantString("Microsoft Windows bitmap image");
1376 entry->module=ConstantString("BMP");
1377 entry->adjoin=MagickFalse;
1378 entry->seekable_stream=MagickTrue;
1379 (void) RegisterMagickInfo(entry);
1380 entry=SetMagickInfo("BMP2");
1381 entry->encoder=(EncodeImageHandler *) WriteBMPImage;
1382 entry->magick=(IsImageFormatHandler *) IsBMP;
1383 entry->description=ConstantString("Microsoft Windows bitmap image (V2)");
1384 entry->module=ConstantString("BMP");
1385 entry->adjoin=MagickFalse;
1386 entry->seekable_stream=MagickTrue;
1387 (void) RegisterMagickInfo(entry);
1388 entry=SetMagickInfo("BMP3");
1389 entry->encoder=(EncodeImageHandler *) WriteBMPImage;
1390 entry->magick=(IsImageFormatHandler *) IsBMP;
1391 entry->description=ConstantString("Microsoft Windows bitmap image (V3)");
1392 entry->module=ConstantString("BMP");
1393 entry->adjoin=MagickFalse;
1394 entry->seekable_stream=MagickTrue;
1395 (void) RegisterMagickInfo(entry);
1396 return(MagickImageCoderSignature);
1400 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1404 % U n r e g i s t e r B M P I m a g e %
1408 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1410 % UnregisterBMPImage() removes format registrations made by the
1411 % BMP module from the list of supported formats.
1413 % The format of the UnregisterBMPImage method is:
1415 % UnregisterBMPImage(void)
1418 ModuleExport void UnregisterBMPImage(void)
1420 (void) UnregisterMagickInfo("BMP");
1421 (void) UnregisterMagickInfo("BMP2");
1422 (void) UnregisterMagickInfo("BMP3");
1426 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1430 % W r i t e B M P I m a g e %
1434 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1436 % WriteBMPImage() writes an image in Microsoft Windows bitmap encoded
1437 % image format, version 3 for Windows or (if the image has a matte channel)
1440 % The format of the WriteBMPImage method is:
1442 % MagickBooleanType WriteBMPImage(const ImageInfo *image_info,
1443 % Image *image,ExceptionInfo *exception)
1445 % A description of each parameter follows.
1447 % o image_info: the image info.
1449 % o image: The image.
1451 % o exception: return any errors or warnings in this structure.
1454 static MagickBooleanType WriteBMPImage(const ImageInfo *image_info,Image *image,
1455 ExceptionInfo *exception)
1470 register const Quantum
1477 register unsigned char
1492 Open output image file.
1494 assert(image_info != (const ImageInfo *) NULL);
1495 assert(image_info->signature == MagickSignature);
1496 assert(image != (Image *) NULL);
1497 assert(image->signature == MagickSignature);
1498 if (image->debug != MagickFalse)
1499 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1500 assert(exception != (ExceptionInfo *) NULL);
1501 assert(exception->signature == MagickSignature);
1502 status=OpenBlob(image_info,image,WriteBinaryBlobMode,exception);
1503 if (status == MagickFalse)
1506 if (LocaleCompare(image_info->magick,"BMP2") == 0)
1509 if (LocaleCompare(image_info->magick,"BMP3") == 0)
1515 Initialize BMP raster file header.
1517 if (IsRGBColorspace(image->colorspace) == MagickFalse)
1518 (void) TransformImageColorspace(image,RGBColorspace,exception);
1519 (void) ResetMagickMemory(&bmp_info,0,sizeof(bmp_info));
1520 bmp_info.file_size=14+12;
1522 bmp_info.file_size+=28;
1523 bmp_info.offset_bits=bmp_info.file_size;
1524 bmp_info.compression=BI_RGB;
1525 if ((image->storage_class == PseudoClass) && (image->colors > 256))
1526 (void) SetImageStorageClass(image,DirectClass,exception);
1527 if (image->storage_class != DirectClass)
1530 Colormapped BMP raster.
1532 bmp_info.bits_per_pixel=8;
1533 if (image->colors <= 2)
1534 bmp_info.bits_per_pixel=1;
1536 if (image->colors <= 16)
1537 bmp_info.bits_per_pixel=4;
1539 if (image->colors <= 256)
1540 bmp_info.bits_per_pixel=8;
1541 if (image_info->compression == RLECompression)
1542 bmp_info.bits_per_pixel=8;
1543 bmp_info.number_colors=1U << bmp_info.bits_per_pixel;
1544 if (image->matte != MagickFalse)
1545 (void) SetImageStorageClass(image,DirectClass,exception);
1547 if ((size_t) bmp_info.number_colors < image->colors)
1548 (void) SetImageStorageClass(image,DirectClass,exception);
1551 bmp_info.file_size+=3*(1UL << bmp_info.bits_per_pixel);
1552 bmp_info.offset_bits+=3*(1UL << bmp_info.bits_per_pixel);
1555 bmp_info.file_size+=(1UL << bmp_info.bits_per_pixel);
1556 bmp_info.offset_bits+=(1UL << bmp_info.bits_per_pixel);
1560 if (image->storage_class == DirectClass)
1563 Full color BMP raster.
1565 bmp_info.number_colors=0;
1566 bmp_info.bits_per_pixel=(unsigned short)
1567 ((type > 3) && (image->matte != MagickFalse) ? 32 : 24);
1568 bmp_info.compression=(unsigned int) ((type > 3) &&
1569 (image->matte != MagickFalse) ? BI_BITFIELDS : BI_RGB);
1571 bytes_per_line=4*((image->columns*bmp_info.bits_per_pixel+31)/32);
1572 bmp_info.ba_offset=0;
1573 profile=GetImageProfile(image,"icc");
1574 have_color_info=(image->rendering_intent != UndefinedIntent) ||
1575 (profile != (StringInfo *) NULL) || (image->gamma != 0.0) ? MagickTrue :
1580 if ((type == 3) || ((image->matte == MagickFalse) &&
1581 (have_color_info == MagickFalse)))
1593 if ((image->rendering_intent != UndefinedIntent) ||
1594 (profile != (StringInfo *) NULL))
1599 bmp_info.file_size+=extra_size;
1600 bmp_info.offset_bits+=extra_size;
1602 bmp_info.width=(ssize_t) image->columns;
1603 bmp_info.height=(ssize_t) image->rows;
1605 bmp_info.image_size=(unsigned int) (bytes_per_line*image->rows);
1606 bmp_info.file_size+=bmp_info.image_size;
1607 bmp_info.x_pixels=75*39;
1608 bmp_info.y_pixels=75*39;
1609 switch (image->units)
1611 case UndefinedResolution:
1612 case PixelsPerInchResolution:
1614 bmp_info.x_pixels=(unsigned int) (100.0*image->resolution.x/2.54);
1615 bmp_info.y_pixels=(unsigned int) (100.0*image->resolution.y/2.54);
1618 case PixelsPerCentimeterResolution:
1620 bmp_info.x_pixels=(unsigned int) (100.0*image->resolution.x);
1621 bmp_info.y_pixels=(unsigned int) (100.0*image->resolution.y);
1625 bmp_info.colors_important=bmp_info.number_colors;
1627 Convert MIFF to BMP raster pixels.
1629 pixels=(unsigned char *) AcquireQuantumMemory((size_t) bmp_info.image_size,
1631 if (pixels == (unsigned char *) NULL)
1632 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
1633 (void) ResetMagickMemory(pixels,0,(size_t) bmp_info.image_size);
1634 switch (bmp_info.bits_per_pixel)
1643 Convert PseudoClass image to a BMP monochrome image.
1645 for (y=0; y < (ssize_t) image->rows; y++)
1650 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
1651 if (p == (const Quantum *) NULL)
1653 q=pixels+(image->rows-y-1)*bytes_per_line;
1656 for (x=0; x < (ssize_t) image->columns; x++)
1659 byte|=GetPixelIndex(image,p) != 0 ? 0x01 : 0x00;
1663 *q++=(unsigned char) byte;
1667 p+=GetPixelChannels(image);
1671 *q++=(unsigned char) (byte << (8-bit));
1674 offset=(ssize_t) (image->columns+7)/8;
1675 for (x=offset; x < (ssize_t) bytes_per_line; x++)
1677 if (image->previous == (Image *) NULL)
1679 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
1681 if (status == MagickFalse)
1695 Convert PseudoClass image to a BMP monochrome image.
1697 for (y=0; y < (ssize_t) image->rows; y++)
1699 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
1700 if (p == (const Quantum *) NULL)
1702 q=pixels+(image->rows-y-1)*bytes_per_line;
1705 for (x=0; x < (ssize_t) image->columns; x++)
1708 byte|=((size_t) GetPixelIndex(image,p) & 0x0f);
1712 *q++=(unsigned char) byte;
1716 p+=GetPixelChannels(image);
1720 *q++=(unsigned char) (byte << 4);
1723 offset=(ssize_t) (image->columns+1)/2;
1724 for (x=offset; x < (ssize_t) bytes_per_line; x++)
1726 if (image->previous == (Image *) NULL)
1728 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
1730 if (status == MagickFalse)
1739 Convert PseudoClass packet to BMP pixel.
1741 for (y=0; y < (ssize_t) image->rows; y++)
1743 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
1744 if (p == (const Quantum *) NULL)
1746 q=pixels+(image->rows-y-1)*bytes_per_line;
1747 for (x=0; x < (ssize_t) image->columns; x++)
1749 *q++=(unsigned char) GetPixelIndex(image,p);
1750 p+=GetPixelChannels(image);
1752 for ( ; x < (ssize_t) bytes_per_line; x++)
1754 if (image->previous == (Image *) NULL)
1756 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
1758 if (status == MagickFalse)
1767 Convert DirectClass packet to BMP BGR888.
1769 for (y=0; y < (ssize_t) image->rows; y++)
1771 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
1772 if (p == (const Quantum *) NULL)
1774 q=pixels+(image->rows-y-1)*bytes_per_line;
1775 for (x=0; x < (ssize_t) image->columns; x++)
1777 *q++=ScaleQuantumToChar(GetPixelBlue(image,p));
1778 *q++=ScaleQuantumToChar(GetPixelGreen(image,p));
1779 *q++=ScaleQuantumToChar(GetPixelRed(image,p));
1780 p+=GetPixelChannels(image);
1782 for (x=3L*(ssize_t) image->columns; x < (ssize_t) bytes_per_line; x++)
1784 if (image->previous == (Image *) NULL)
1786 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
1788 if (status == MagickFalse)
1797 Convert DirectClass packet to ARGB8888 pixel.
1799 for (y=0; y < (ssize_t) image->rows; y++)
1801 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
1802 if (p == (const Quantum *) NULL)
1804 q=pixels+(image->rows-y-1)*bytes_per_line;
1805 for (x=0; x < (ssize_t) image->columns; x++)
1807 *q++=ScaleQuantumToChar(GetPixelBlue(image,p));
1808 *q++=ScaleQuantumToChar(GetPixelGreen(image,p));
1809 *q++=ScaleQuantumToChar(GetPixelRed(image,p));
1810 *q++=ScaleQuantumToChar(GetPixelAlpha(image,p));
1811 p+=GetPixelChannels(image);
1813 if (image->previous == (Image *) NULL)
1815 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
1817 if (status == MagickFalse)
1824 if ((type > 2) && (bmp_info.bits_per_pixel == 8))
1825 if (image_info->compression != NoCompression)
1831 Convert run-length encoded raster pixels.
1833 length=(size_t) (2*(bytes_per_line+2)*(image->rows+2)+2);
1834 bmp_data=(unsigned char *) NULL;
1835 if (~length >= bytes_per_line)
1836 bmp_data=(unsigned char *) AcquireQuantumMemory(length+
1837 bytes_per_line,sizeof(*bmp_data));
1838 if (bmp_data == (unsigned char *) NULL)
1840 pixels=(unsigned char *) RelinquishMagickMemory(pixels);
1841 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
1843 bmp_info.file_size-=bmp_info.image_size;
1844 bmp_info.image_size=(unsigned int) EncodeImage(image,bytes_per_line,
1846 bmp_info.file_size+=bmp_info.image_size;
1847 pixels=(unsigned char *) RelinquishMagickMemory(pixels);
1849 bmp_info.compression=BI_RLE8;
1852 Write BMP for Windows, all versions, 14-byte header.
1854 if (image->debug != MagickFalse)
1856 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1857 " Writing BMP version %.20g datastream",(double) type);
1858 if (image->storage_class == DirectClass)
1859 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1860 " Storage class=DirectClass");
1862 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1863 " Storage class=PseudoClass");
1864 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1865 " Image depth=%.20g",(double) image->depth);
1866 if (image->matte != MagickFalse)
1867 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1870 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1871 " Matte=MagickFalse");
1872 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1873 " BMP bits_per_pixel=%.20g",(double) bmp_info.bits_per_pixel);
1874 switch ((int) bmp_info.compression)
1878 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1879 " Compression=BI_RGB");
1884 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1885 " Compression=BI_RLE8");
1890 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1891 " Compression=BI_BITFIELDS");
1896 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1897 " Compression=UNKNOWN (%u)",bmp_info.compression);
1901 if (bmp_info.number_colors == 0)
1902 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1903 " Number_colors=unspecified");
1905 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1906 " Number_colors=%u",bmp_info.number_colors);
1908 (void) WriteBlob(image,2,(unsigned char *) "BM");
1909 (void) WriteBlobLSBLong(image,bmp_info.file_size);
1910 (void) WriteBlobLSBLong(image,bmp_info.ba_offset); /* always 0 */
1911 (void) WriteBlobLSBLong(image,bmp_info.offset_bits);
1915 Write 12-byte version 2 bitmap header.
1917 (void) WriteBlobLSBLong(image,bmp_info.size);
1918 (void) WriteBlobLSBShort(image,(unsigned short) bmp_info.width);
1919 (void) WriteBlobLSBShort(image,(unsigned short) bmp_info.height);
1920 (void) WriteBlobLSBShort(image,bmp_info.planes);
1921 (void) WriteBlobLSBShort(image,bmp_info.bits_per_pixel);
1926 Write 40-byte version 3+ bitmap header.
1928 (void) WriteBlobLSBLong(image,bmp_info.size);
1929 (void) WriteBlobLSBLong(image,(unsigned int) bmp_info.width);
1930 (void) WriteBlobLSBLong(image,(unsigned int) bmp_info.height);
1931 (void) WriteBlobLSBShort(image,bmp_info.planes);
1932 (void) WriteBlobLSBShort(image,bmp_info.bits_per_pixel);
1933 (void) WriteBlobLSBLong(image,bmp_info.compression);
1934 (void) WriteBlobLSBLong(image,bmp_info.image_size);
1935 (void) WriteBlobLSBLong(image,bmp_info.x_pixels);
1936 (void) WriteBlobLSBLong(image,bmp_info.y_pixels);
1937 (void) WriteBlobLSBLong(image,bmp_info.number_colors);
1938 (void) WriteBlobLSBLong(image,bmp_info.colors_important);
1940 if ((type > 3) && ((image->matte != MagickFalse) ||
1941 (have_color_info != MagickFalse)))
1944 Write the rest of the 108-byte BMP Version 4 header.
1946 (void) WriteBlobLSBLong(image,0x00ff0000U); /* Red mask */
1947 (void) WriteBlobLSBLong(image,0x0000ff00U); /* Green mask */
1948 (void) WriteBlobLSBLong(image,0x000000ffU); /* Blue mask */
1949 (void) WriteBlobLSBLong(image,0xff000000U); /* Alpha mask */
1950 (void) WriteBlobLSBLong(image,0x00000001U); /* CSType==Calib. RGB */
1951 (void) WriteBlobLSBLong(image,(unsigned int)
1952 image->chromaticity.red_primary.x*0x3ffffff);
1953 (void) WriteBlobLSBLong(image,(unsigned int)
1954 image->chromaticity.red_primary.y*0x3ffffff);
1955 (void) WriteBlobLSBLong(image,(unsigned int)
1956 (1.000f-(image->chromaticity.red_primary.x+
1957 image->chromaticity.red_primary.y)*0x3ffffff));
1958 (void) WriteBlobLSBLong(image,(unsigned int)
1959 image->chromaticity.green_primary.x*0x3ffffff);
1960 (void) WriteBlobLSBLong(image,(unsigned int)
1961 image->chromaticity.green_primary.y*0x3ffffff);
1962 (void) WriteBlobLSBLong(image,(unsigned int)
1963 (1.000f-(image->chromaticity.green_primary.x+
1964 image->chromaticity.green_primary.y)*0x3ffffff));
1965 (void) WriteBlobLSBLong(image,(unsigned int)
1966 image->chromaticity.blue_primary.x*0x3ffffff);
1967 (void) WriteBlobLSBLong(image,(unsigned int)
1968 image->chromaticity.blue_primary.y*0x3ffffff);
1969 (void) WriteBlobLSBLong(image,(unsigned int)
1970 (1.000f-(image->chromaticity.blue_primary.x+
1971 image->chromaticity.blue_primary.y)*0x3ffffff));
1972 (void) WriteBlobLSBLong(image,(unsigned int)
1973 bmp_info.gamma_scale.x*0xffff);
1974 (void) WriteBlobLSBLong(image,(unsigned int)
1975 bmp_info.gamma_scale.y*0xffff);
1976 (void) WriteBlobLSBLong(image,(unsigned int)
1977 bmp_info.gamma_scale.z*0xffff);
1978 if ((image->rendering_intent != UndefinedIntent) ||
1979 (profile != (StringInfo *) NULL))
1984 switch ((int) image->rendering_intent)
1986 case SaturationIntent:
1988 intent=LCS_GM_BUSINESS;
1991 case RelativeIntent:
1993 intent=LCS_GM_GRAPHICS;
1996 case PerceptualIntent:
1998 intent=LCS_GM_IMAGES;
2001 case AbsoluteIntent:
2003 intent=LCS_GM_ABS_COLORIMETRIC;
2012 (void) WriteBlobLSBLong(image,(unsigned int) intent);
2013 (void) WriteBlobLSBLong(image,0x00); /* dummy profile data */
2014 (void) WriteBlobLSBLong(image,0x00); /* dummy profile length */
2015 (void) WriteBlobLSBLong(image,0x00); /* reserved */
2018 if (image->storage_class == PseudoClass)
2024 Dump colormap to file.
2026 if (image->debug != MagickFalse)
2027 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
2028 " Colormap: %.20g entries",(double) image->colors);
2029 bmp_colormap=(unsigned char *) AcquireQuantumMemory((size_t) (1UL <<
2030 bmp_info.bits_per_pixel),4*sizeof(*bmp_colormap));
2031 if (bmp_colormap == (unsigned char *) NULL)
2032 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
2034 for (i=0; i < (ssize_t) MagickMin((ssize_t) image->colors,(ssize_t) bmp_info.number_colors); i++)
2036 *q++=ScaleQuantumToChar(image->colormap[i].blue);
2037 *q++=ScaleQuantumToChar(image->colormap[i].green);
2038 *q++=ScaleQuantumToChar(image->colormap[i].red);
2040 *q++=(unsigned char) 0x0;
2042 for ( ; i < (ssize_t) (1UL << bmp_info.bits_per_pixel); i++)
2044 *q++=(unsigned char) 0x00;
2045 *q++=(unsigned char) 0x00;
2046 *q++=(unsigned char) 0x00;
2048 *q++=(unsigned char) 0x00;
2051 (void) WriteBlob(image,(size_t) (3*(1L << bmp_info.bits_per_pixel)),
2054 (void) WriteBlob(image,(size_t) (4*(1L << bmp_info.bits_per_pixel)),
2056 bmp_colormap=(unsigned char *) RelinquishMagickMemory(bmp_colormap);
2058 if (image->debug != MagickFalse)
2059 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
2060 " Pixels: %u bytes",bmp_info.image_size);
2061 (void) WriteBlob(image,(size_t) bmp_info.image_size,pixels);
2062 pixels=(unsigned char *) RelinquishMagickMemory(pixels);
2063 if (GetNextImageInList(image) == (Image *) NULL)
2065 image=SyncNextImageInList(image);
2066 status=SetImageProgress(image,SaveImagesTag,scene++,
2067 GetImageListLength(image));
2068 if (status == MagickFalse)
2070 } while (image_info->adjoin != MagickFalse);
2071 (void) CloseBlob(image);