2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
13 % Read/Write Microsoft Windows Bitmap Image Format %
17 % Glenn Randers-Pehrson %
21 % Copyright 1999-2018 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 % https://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/option.h"
63 #include "MagickCore/pixel-accessor.h"
64 #include "MagickCore/profile.h"
65 #include "MagickCore/quantum-private.h"
66 #include "MagickCore/static.h"
67 #include "MagickCore/string_.h"
68 #include "MagickCore/module.h"
69 #include "MagickCore/transform.h"
72 Macro definitions (from Windows wingdi.h).
78 #if !defined(MAGICKCORE_WINDOWS_SUPPORT) || defined(__MINGW32__)
86 #define BI_BITFIELDS 3
88 #undef LCS_CALIBRATED_RBG
89 #define LCS_CALIBRATED_RBG 0
92 #undef LCS_WINDOWS_COLOR_SPACE
93 #define LCS_WINDOWS_COLOR_SPACE 2
95 #define PROFILE_LINKED 3
96 #undef PROFILE_EMBEDDED
97 #define PROFILE_EMBEDDED 4
99 #undef LCS_GM_BUSINESS
100 #define LCS_GM_BUSINESS 1 /* Saturation */
101 #undef LCS_GM_GRAPHICS
102 #define LCS_GM_GRAPHICS 2 /* Relative */
104 #define LCS_GM_IMAGES 4 /* Perceptual */
105 #undef LCS_GM_ABS_COLORIMETRIC
106 #define LCS_GM_ABS_COLORIMETRIC 8 /* Absolute */
110 Enumerated declaractions.
122 Typedef declarations.
124 typedef struct _BMPInfo
163 Forward declarations.
165 static MagickBooleanType
166 WriteBMPImage(const ImageInfo *,Image *,ExceptionInfo *);
169 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
173 % D e c o d e I m a g e %
177 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
179 % DecodeImage unpacks the packed image pixels into runlength-encoded
182 % The format of the DecodeImage method is:
184 % MagickBooleanType DecodeImage(Image *image,const size_t compression,
185 % unsigned char *pixels,const size_t number_pixels)
187 % A description of each parameter follows:
189 % o image: the address of a structure of type Image.
191 % o compression: Zero means uncompressed. A value of 1 means the
192 % compressed pixels are runlength encoded for a 256-color bitmap.
193 % A value of 2 means a 16-color bitmap. A value of 3 means bitfields
196 % o pixels: The address of a byte (8 bits) array of pixel data created by
197 % the decoding process.
199 % o number_pixels: The number of pixels.
202 static MagickBooleanType DecodeImage(Image *image,const size_t compression,
203 unsigned char *pixels,const size_t number_pixels)
213 register unsigned char
220 assert(image != (Image *) NULL);
221 assert(image->signature == MagickCoreSignature);
222 if (image->debug != MagickFalse)
223 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
224 assert(pixels != (unsigned char *) NULL);
225 (void) memset(pixels,0,number_pixels*sizeof(*pixels));
229 q=pixels+number_pixels;
230 for (y=0; y < (ssize_t) image->rows; )
235 if ((p < pixels) || (p > q))
237 count=ReadBlobByte(image);
245 count=(int) MagickMin((ssize_t) count,(ssize_t) (q-p));
246 byte=ReadBlobByte(image);
249 if (compression == BI_RLE8)
251 for (i=0; i < (ssize_t) count; i++)
252 *p++=(unsigned char) byte;
256 for (i=0; i < (ssize_t) count; i++)
258 ((i & 0x01) != 0 ? (byte & 0x0f) : ((byte >> 4) & 0x0f));
267 count=ReadBlobByte(image);
281 p=pixels+y*image->columns;
289 x+=ReadBlobByte(image);
290 y+=ReadBlobByte(image);
291 p=pixels+y*image->columns+x;
299 count=(int) MagickMin((ssize_t) count,(ssize_t) (q-p));
300 if (compression == BI_RLE8)
301 for (i=0; i < (ssize_t) count; i++)
303 byte=ReadBlobByte(image);
306 *p++=(unsigned char) byte;
309 for (i=0; i < (ssize_t) count; i++)
313 byte=ReadBlobByte(image);
318 ((i & 0x01) != 0 ? (byte & 0x0f) : ((byte >> 4) & 0x0f));
324 if (compression == BI_RLE8)
326 if ((count & 0x01) != 0)
327 if (ReadBlobByte(image) == EOF)
331 if (((count & 0x03) == 1) || ((count & 0x03) == 2))
332 if (ReadBlobByte(image) == EOF)
338 status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y,
340 if (status == MagickFalse)
343 (void) ReadBlobByte(image); /* end of line */
344 (void) ReadBlobByte(image);
345 return(y < (ssize_t) image->rows ? MagickFalse : MagickTrue);
349 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
353 % E n c o d e I m a g e %
357 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
359 % EncodeImage compresses pixels using a runlength encoded format.
361 % The format of the EncodeImage method is:
363 % static MagickBooleanType EncodeImage(Image *image,
364 % const size_t bytes_per_line,const unsigned char *pixels,
365 % unsigned char *compressed_pixels)
367 % A description of each parameter follows:
369 % o image: The image.
371 % o bytes_per_line: the number of bytes in a scanline of compressed pixels
373 % o pixels: The address of a byte (8 bits) array of pixel data created by
374 % the compression process.
376 % o compressed_pixels: The address of a byte (8 bits) array of compressed
380 static size_t EncodeImage(Image *image,const size_t bytes_per_line,
381 const unsigned char *pixels,unsigned char *compressed_pixels)
386 register const unsigned char
393 register unsigned char
400 Runlength encode pixels.
402 assert(image != (Image *) NULL);
403 assert(image->signature == MagickCoreSignature);
404 if (image->debug != MagickFalse)
405 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
406 assert(pixels != (const unsigned char *) NULL);
407 assert(compressed_pixels != (unsigned char *) NULL);
411 for (y=0; y < (ssize_t) image->rows; y++)
413 for (x=0; x < (ssize_t) bytes_per_line; x+=i)
418 for (i=1; ((x+i) < (ssize_t) bytes_per_line); i++)
419 if ((i == 255) || (*(p+i) != *p))
421 *q++=(unsigned char) i;
428 *q++=(unsigned char) 0x00;
429 *q++=(unsigned char) 0x00;
430 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
432 if (status == MagickFalse)
438 *q++=(unsigned char) 0x00;
439 *q++=(unsigned char) 0x01;
440 return((size_t) (q-compressed_pixels));
444 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
452 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
454 % IsBMP() returns MagickTrue if the image format type, identified by the
455 % magick string, is BMP.
457 % The format of the IsBMP method is:
459 % MagickBooleanType IsBMP(const unsigned char *magick,const size_t length)
461 % A description of each parameter follows:
463 % o magick: compare image format pattern against these bytes.
465 % o length: Specifies the length of the magick string.
468 static MagickBooleanType IsBMP(const unsigned char *magick,const size_t length)
472 if ((LocaleNCompare((char *) magick,"BA",2) == 0) ||
473 (LocaleNCompare((char *) magick,"BM",2) == 0) ||
474 (LocaleNCompare((char *) magick,"IC",2) == 0) ||
475 (LocaleNCompare((char *) magick,"PI",2) == 0) ||
476 (LocaleNCompare((char *) magick,"CI",2) == 0) ||
477 (LocaleNCompare((char *) magick,"CP",2) == 0))
483 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
487 % R e a d B M P I m a g e %
491 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
493 % ReadBMPImage() reads a Microsoft Windows bitmap image file, Version
494 % 2, 3 (for Windows or NT), or 4, and returns it. It allocates the memory
495 % necessary for the new Image structure and returns a pointer to the new
498 % The format of the ReadBMPImage method is:
500 % image=ReadBMPImage(image_info)
502 % A description of each parameter follows:
504 % o image_info: the image info.
506 % o exception: return any errors or warnings in this structure.
510 static Image *ReadBMPImage(const ImageInfo *image_info,ExceptionInfo *exception)
538 register unsigned char
563 assert(image_info != (const ImageInfo *) NULL);
564 assert(image_info->signature == MagickCoreSignature);
565 if (image_info->debug != MagickFalse)
566 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
567 image_info->filename);
568 assert(exception != (ExceptionInfo *) NULL);
569 assert(exception->signature == MagickCoreSignature);
570 image=AcquireImage(image_info,exception);
571 status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception);
572 if (status == MagickFalse)
574 image=DestroyImageList(image);
575 return((Image *) NULL);
578 Determine if this a BMP file.
580 (void) memset(&bmp_info,0,sizeof(bmp_info));
581 bmp_info.ba_offset=0;
584 count=ReadBlob(image,2,magick);
586 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
596 Verify BMP identifier.
598 if (bmp_info.ba_offset == 0)
599 start_position=TellBlob(image)-2;
600 bmp_info.ba_offset=0;
601 while (LocaleNCompare((char *) magick,"BA",2) == 0)
603 bmp_info.file_size=ReadBlobLSBLong(image);
604 bmp_info.ba_offset=ReadBlobLSBLong(image);
605 bmp_info.offset_bits=ReadBlobLSBLong(image);
606 count=ReadBlob(image,2,magick);
610 if (image->debug != MagickFalse)
611 (void) LogMagickEvent(CoderEvent,GetMagickModule()," Magick: %c%c",
612 magick[0],magick[1]);
613 if ((count != 2) || ((LocaleNCompare((char *) magick,"BM",2) != 0) &&
614 (LocaleNCompare((char *) magick,"CI",2) != 0)))
615 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
616 bmp_info.file_size=ReadBlobLSBLong(image);
617 (void) ReadBlobLSBLong(image);
618 bmp_info.offset_bits=ReadBlobLSBLong(image);
619 bmp_info.size=ReadBlobLSBLong(image);
620 if (image->debug != MagickFalse)
621 (void) LogMagickEvent(CoderEvent,GetMagickModule()," BMP size: %u",
623 if (bmp_info.size == 12)
628 (void) CopyMagickString(image->magick,"BMP2",MagickPathExtent);
629 bmp_info.width=(ssize_t) ((short) ReadBlobLSBShort(image));
630 bmp_info.height=(ssize_t) ((short) ReadBlobLSBShort(image));
631 bmp_info.planes=ReadBlobLSBShort(image);
632 bmp_info.bits_per_pixel=ReadBlobLSBShort(image);
635 bmp_info.number_colors=0;
636 bmp_info.compression=BI_RGB;
637 bmp_info.image_size=0;
638 bmp_info.alpha_mask=0;
639 if (image->debug != MagickFalse)
641 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
642 " Format: OS/2 Bitmap");
643 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
644 " Geometry: %.20gx%.20g",(double) bmp_info.width,(double)
651 Microsoft Windows BMP image file.
653 if (bmp_info.size < 40)
654 ThrowReaderException(CorruptImageError,"NonOS2HeaderSizeError");
655 bmp_info.width=(ssize_t) ReadBlobLSBSignedLong(image);
656 bmp_info.height=(ssize_t) ReadBlobLSBSignedLong(image);
657 bmp_info.planes=ReadBlobLSBShort(image);
658 bmp_info.bits_per_pixel=ReadBlobLSBShort(image);
659 bmp_info.compression=ReadBlobLSBLong(image);
660 bmp_info.image_size=ReadBlobLSBLong(image);
661 bmp_info.x_pixels=ReadBlobLSBLong(image);
662 bmp_info.y_pixels=ReadBlobLSBLong(image);
663 bmp_info.number_colors=ReadBlobLSBLong(image);
664 bmp_info.colors_important=ReadBlobLSBLong(image);
665 if (image->debug != MagickFalse)
667 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
668 " Format: MS Windows bitmap");
669 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
670 " Geometry: %.20gx%.20g",(double) bmp_info.width,(double)
672 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
673 " Bits per pixel: %.20g",(double) bmp_info.bits_per_pixel);
674 switch (bmp_info.compression)
678 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
679 " Compression: BI_RGB");
684 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
685 " Compression: BI_RLE4");
690 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
691 " Compression: BI_RLE8");
696 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
697 " Compression: BI_BITFIELDS");
702 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
703 " Compression: BI_PNG");
708 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
709 " Compression: BI_JPEG");
714 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
715 " Compression: UNKNOWN (%u)",bmp_info.compression);
718 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
719 " Number of colors: %u",bmp_info.number_colors);
721 bmp_info.red_mask=ReadBlobLSBLong(image);
722 bmp_info.green_mask=ReadBlobLSBLong(image);
723 bmp_info.blue_mask=ReadBlobLSBLong(image);
724 if (bmp_info.size > 40)
730 Read color management information.
732 bmp_info.alpha_mask=ReadBlobLSBLong(image);
733 bmp_info.colorspace=ReadBlobLSBSignedLong(image);
735 Decode 2^30 fixed point formatted CIE primaries.
737 # define BMP_DENOM ((double) 0x40000000)
738 bmp_info.red_primary.x=(double) ReadBlobLSBLong(image)/BMP_DENOM;
739 bmp_info.red_primary.y=(double) ReadBlobLSBLong(image)/BMP_DENOM;
740 bmp_info.red_primary.z=(double) ReadBlobLSBLong(image)/BMP_DENOM;
741 bmp_info.green_primary.x=(double) ReadBlobLSBLong(image)/BMP_DENOM;
742 bmp_info.green_primary.y=(double) ReadBlobLSBLong(image)/BMP_DENOM;
743 bmp_info.green_primary.z=(double) ReadBlobLSBLong(image)/BMP_DENOM;
744 bmp_info.blue_primary.x=(double) ReadBlobLSBLong(image)/BMP_DENOM;
745 bmp_info.blue_primary.y=(double) ReadBlobLSBLong(image)/BMP_DENOM;
746 bmp_info.blue_primary.z=(double) ReadBlobLSBLong(image)/BMP_DENOM;
748 gamma=bmp_info.red_primary.x+bmp_info.red_primary.y+
749 bmp_info.red_primary.z;
750 gamma=PerceptibleReciprocal(gamma);
751 bmp_info.red_primary.x*=gamma;
752 bmp_info.red_primary.y*=gamma;
753 image->chromaticity.red_primary.x=bmp_info.red_primary.x;
754 image->chromaticity.red_primary.y=bmp_info.red_primary.y;
756 gamma=bmp_info.green_primary.x+bmp_info.green_primary.y+
757 bmp_info.green_primary.z;
758 gamma=PerceptibleReciprocal(gamma);
759 bmp_info.green_primary.x*=gamma;
760 bmp_info.green_primary.y*=gamma;
761 image->chromaticity.green_primary.x=bmp_info.green_primary.x;
762 image->chromaticity.green_primary.y=bmp_info.green_primary.y;
764 gamma=bmp_info.blue_primary.x+bmp_info.blue_primary.y+
765 bmp_info.blue_primary.z;
766 gamma=PerceptibleReciprocal(gamma);
767 bmp_info.blue_primary.x*=gamma;
768 bmp_info.blue_primary.y*=gamma;
769 image->chromaticity.blue_primary.x=bmp_info.blue_primary.x;
770 image->chromaticity.blue_primary.y=bmp_info.blue_primary.y;
773 Decode 16^16 fixed point formatted gamma_scales.
775 bmp_info.gamma_scale.x=(double) ReadBlobLSBLong(image)/0x10000;
776 bmp_info.gamma_scale.y=(double) ReadBlobLSBLong(image)/0x10000;
777 bmp_info.gamma_scale.z=(double) ReadBlobLSBLong(image)/0x10000;
779 Compute a single gamma from the BMP 3-channel gamma.
781 image->gamma=(bmp_info.gamma_scale.x+bmp_info.gamma_scale.y+
782 bmp_info.gamma_scale.z)/3.0;
785 (void) CopyMagickString(image->magick,"BMP3",MagickPathExtent);
787 if (bmp_info.size > 108)
793 Read BMP Version 5 color management information.
795 intent=ReadBlobLSBLong(image);
796 switch ((int) intent)
798 case LCS_GM_BUSINESS:
800 image->rendering_intent=SaturationIntent;
803 case LCS_GM_GRAPHICS:
805 image->rendering_intent=RelativeIntent;
810 image->rendering_intent=PerceptualIntent;
813 case LCS_GM_ABS_COLORIMETRIC:
815 image->rendering_intent=AbsoluteIntent;
819 (void) ReadBlobLSBLong(image); /* Profile data */
820 (void) ReadBlobLSBLong(image); /* Profile size */
821 (void) ReadBlobLSBLong(image); /* Reserved byte */
824 if ((MagickSizeType) bmp_info.file_size > GetBlobSize(image))
825 (void) ThrowMagickException(exception,GetMagickModule(),CorruptImageError,
826 "LengthAndFilesizeDoNotMatch","`%s'",image->filename);
828 if ((MagickSizeType) bmp_info.file_size < GetBlobSize(image))
829 (void) ThrowMagickException(exception,GetMagickModule(),
830 CorruptImageWarning,"LengthAndFilesizeDoNotMatch","`%s'",
832 if (bmp_info.width <= 0)
833 ThrowReaderException(CorruptImageError,"NegativeOrZeroImageSize");
834 if (bmp_info.height == 0)
835 ThrowReaderException(CorruptImageError,"NegativeOrZeroImageSize");
836 if (bmp_info.planes != 1)
837 ThrowReaderException(CorruptImageError,"StaticPlanesValueNotEqualToOne");
838 if ((bmp_info.bits_per_pixel != 1) && (bmp_info.bits_per_pixel != 4) &&
839 (bmp_info.bits_per_pixel != 8) && (bmp_info.bits_per_pixel != 16) &&
840 (bmp_info.bits_per_pixel != 24) && (bmp_info.bits_per_pixel != 32))
841 ThrowReaderException(CorruptImageError,"UnrecognizedBitsPerPixel");
842 if (bmp_info.bits_per_pixel < 16 &&
843 bmp_info.number_colors > (1U << bmp_info.bits_per_pixel))
844 ThrowReaderException(CorruptImageError,"UnrecognizedNumberOfColors");
845 if ((bmp_info.compression == 1) && (bmp_info.bits_per_pixel != 8))
846 ThrowReaderException(CorruptImageError,"UnrecognizedBitsPerPixel");
847 if ((bmp_info.compression == 2) && (bmp_info.bits_per_pixel != 4))
848 ThrowReaderException(CorruptImageError,"UnrecognizedBitsPerPixel");
849 if ((bmp_info.compression == 3) && (bmp_info.bits_per_pixel < 16))
850 ThrowReaderException(CorruptImageError,"UnrecognizedBitsPerPixel");
851 switch (bmp_info.compression)
854 image->compression=NoCompression;
858 image->compression=RLECompression;
863 ThrowReaderException(CoderError,"JPEGCompressNotSupported");
865 ThrowReaderException(CoderError,"PNGCompressNotSupported");
867 ThrowReaderException(CorruptImageError,"UnrecognizedImageCompression");
869 image->columns=(size_t) MagickAbsoluteValue(bmp_info.width);
870 image->rows=(size_t) MagickAbsoluteValue(bmp_info.height);
871 image->depth=bmp_info.bits_per_pixel <= 8 ? bmp_info.bits_per_pixel : 8;
872 image->alpha_trait=((bmp_info.alpha_mask != 0) &&
873 (bmp_info.compression == BI_BITFIELDS)) ? BlendPixelTrait :
875 if (bmp_info.bits_per_pixel < 16)
880 image->storage_class=PseudoClass;
881 image->colors=bmp_info.number_colors;
883 if (image->colors == 0)
884 image->colors=one << bmp_info.bits_per_pixel;
886 image->resolution.x=(double) bmp_info.x_pixels/100.0;
887 image->resolution.y=(double) bmp_info.y_pixels/100.0;
888 image->units=PixelsPerCentimeterResolution;
889 if ((image_info->ping != MagickFalse) && (image_info->number_scenes != 0))
890 if (image->scene >= (image_info->scene+image_info->number_scenes-1))
892 status=SetImageExtent(image,image->columns,image->rows,exception);
893 if (status == MagickFalse)
894 return(DestroyImageList(image));
895 if (image->storage_class == PseudoClass)
904 Read BMP raster colormap.
906 if (image->debug != MagickFalse)
907 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
908 " Reading colormap of %.20g colors",(double) image->colors);
909 if (AcquireImageColormap(image,image->colors,exception) == MagickFalse)
910 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
911 bmp_colormap=(unsigned char *) AcquireQuantumMemory((size_t)
912 image->colors,4*sizeof(*bmp_colormap));
913 if (bmp_colormap == (unsigned char *) NULL)
914 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
915 if ((bmp_info.size == 12) || (bmp_info.size == 64))
919 offset=SeekBlob(image,start_position+14+bmp_info.size,SEEK_SET);
922 bmp_colormap=(unsigned char *) RelinquishMagickMemory(bmp_colormap);
923 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
925 count=ReadBlob(image,packet_size*image->colors,bmp_colormap);
926 if (count != (ssize_t) (packet_size*image->colors))
928 bmp_colormap=(unsigned char *) RelinquishMagickMemory(bmp_colormap);
929 ThrowReaderException(CorruptImageError,
930 "InsufficientImageDataInFile");
933 for (i=0; i < (ssize_t) image->colors; i++)
935 image->colormap[i].blue=(MagickRealType) ScaleCharToQuantum(*p++);
936 image->colormap[i].green=(MagickRealType) ScaleCharToQuantum(*p++);
937 image->colormap[i].red=(MagickRealType) ScaleCharToQuantum(*p++);
938 if (packet_size == 4)
941 bmp_colormap=(unsigned char *) RelinquishMagickMemory(bmp_colormap);
946 if (bmp_info.offset_bits == offset_bits)
947 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
948 offset_bits=bmp_info.offset_bits;
949 offset=SeekBlob(image,start_position+bmp_info.offset_bits,SEEK_SET);
951 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
952 if (bmp_info.compression == BI_RLE4)
953 bmp_info.bits_per_pixel<<=1;
954 bytes_per_line=4*((image->columns*bmp_info.bits_per_pixel+31)/32);
955 length=(size_t) bytes_per_line*image->rows;
956 if (((MagickSizeType) length/8) > GetBlobSize(image))
957 ThrowReaderException(CorruptImageError,"InsufficientImageDataInFile");
958 if ((bmp_info.compression == BI_RGB) ||
959 (bmp_info.compression == BI_BITFIELDS))
961 pixel_info=AcquireVirtualMemory((size_t) image->rows,
962 MagickMax(bytes_per_line,image->columns+256UL)*sizeof(*pixels));
963 if (pixel_info == (MemoryInfo *) NULL)
964 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
965 pixels=(unsigned char *) GetVirtualMemoryBlob(pixel_info);
966 if (image->debug != MagickFalse)
967 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
968 " Reading pixels (%.20g bytes)",(double) length);
969 count=ReadBlob(image,length,pixels);
970 if (count != (ssize_t) length)
972 pixel_info=RelinquishVirtualMemory(pixel_info);
973 ThrowReaderException(CorruptImageError,
974 "InsufficientImageDataInFile");
980 Convert run-length encoded raster pixels.
982 pixel_info=AcquireVirtualMemory((size_t) image->rows,
983 MagickMax(bytes_per_line,image->columns+256UL)*sizeof(*pixels));
984 if (pixel_info == (MemoryInfo *) NULL)
985 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
986 pixels=(unsigned char *) GetVirtualMemoryBlob(pixel_info);
987 status=DecodeImage(image,bmp_info.compression,pixels,
988 image->columns*image->rows);
989 if (status == MagickFalse)
991 pixel_info=RelinquishVirtualMemory(pixel_info);
992 ThrowReaderException(CorruptImageError,
993 "UnableToRunlengthDecodeImage");
997 Convert BMP raster image to pixel packets.
999 if (bmp_info.compression == BI_RGB)
1002 We should ignore the alpha value in BMP3 files but there have been
1003 reports about 32 bit files with alpha. We do a quick check to see if
1004 the alpha channel contains a value that is not zero (default value).
1005 If we find a non zero value we asume the program that wrote the file
1006 wants to use the alpha channel.
1008 if ((image->alpha_trait == UndefinedPixelTrait) && (bmp_info.size == 40) &&
1009 (bmp_info.bits_per_pixel == 32))
1011 bytes_per_line=4*(image->columns);
1012 for (y=(ssize_t) image->rows-1; y >= 0; y--)
1014 p=pixels+(image->rows-y-1)*bytes_per_line;
1015 for (x=0; x < (ssize_t) image->columns; x++)
1019 image->alpha_trait=BlendPixelTrait;
1027 bmp_info.alpha_mask=image->alpha_trait != UndefinedPixelTrait ?
1029 bmp_info.red_mask=0x00ff0000U;
1030 bmp_info.green_mask=0x0000ff00U;
1031 bmp_info.blue_mask=0x000000ffU;
1032 if (bmp_info.bits_per_pixel == 16)
1037 bmp_info.red_mask=0x00007c00U;
1038 bmp_info.green_mask=0x000003e0U;
1039 bmp_info.blue_mask=0x0000001fU;
1042 (void) memset(&shift,0,sizeof(shift));
1043 (void) memset(&quantum_bits,0,sizeof(quantum_bits));
1044 if ((bmp_info.bits_per_pixel == 16) || (bmp_info.bits_per_pixel == 32))
1046 register unsigned int
1050 Get shift and quantum bits info from bitfield masks.
1052 if (bmp_info.red_mask != 0)
1053 while (((bmp_info.red_mask << shift.red) & 0x80000000UL) == 0)
1056 if (shift.red >= 32U)
1059 if (bmp_info.green_mask != 0)
1060 while (((bmp_info.green_mask << shift.green) & 0x80000000UL) == 0)
1063 if (shift.green >= 32U)
1066 if (bmp_info.blue_mask != 0)
1067 while (((bmp_info.blue_mask << shift.blue) & 0x80000000UL) == 0)
1070 if (shift.blue >= 32U)
1073 if (bmp_info.alpha_mask != 0)
1074 while (((bmp_info.alpha_mask << shift.alpha) & 0x80000000UL) == 0)
1077 if (shift.alpha >= 32U)
1081 while (((bmp_info.red_mask << sample) & 0x80000000UL) != 0)
1087 quantum_bits.red=(MagickRealType) (sample-shift.red);
1089 while (((bmp_info.green_mask << sample) & 0x80000000UL) != 0)
1095 quantum_bits.green=(MagickRealType) (sample-shift.green);
1097 while (((bmp_info.blue_mask << sample) & 0x80000000UL) != 0)
1103 quantum_bits.blue=(MagickRealType) (sample-shift.blue);
1105 while (((bmp_info.alpha_mask << sample) & 0x80000000UL) != 0)
1111 quantum_bits.alpha=(MagickRealType) (sample-shift.alpha);
1113 switch (bmp_info.bits_per_pixel)
1118 Convert bitmap scanline.
1120 for (y=(ssize_t) image->rows-1; y >= 0; y--)
1122 p=pixels+(image->rows-y-1)*bytes_per_line;
1123 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
1124 if (q == (Quantum *) NULL)
1126 for (x=0; x < ((ssize_t) image->columns-7); x+=8)
1128 for (bit=0; bit < 8; bit++)
1130 index=(Quantum) (((*p) & (0x80 >> bit)) != 0 ? 0x01 : 0x00);
1131 SetPixelIndex(image,index,q);
1132 q+=GetPixelChannels(image);
1136 if ((image->columns % 8) != 0)
1138 for (bit=0; bit < (image->columns % 8); bit++)
1140 index=(Quantum) (((*p) & (0x80 >> bit)) != 0 ? 0x01 : 0x00);
1141 SetPixelIndex(image,index,q);
1142 q+=GetPixelChannels(image);
1146 if (SyncAuthenticPixels(image,exception) == MagickFalse)
1148 if (image->previous == (Image *) NULL)
1150 status=SetImageProgress(image,LoadImageTag,(MagickOffsetType)
1151 (image->rows-y),image->rows);
1152 if (status == MagickFalse)
1156 (void) SyncImage(image,exception);
1162 Convert PseudoColor scanline.
1164 for (y=(ssize_t) image->rows-1; y >= 0; y--)
1166 p=pixels+(image->rows-y-1)*bytes_per_line;
1167 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
1168 if (q == (Quantum *) NULL)
1170 for (x=0; x < ((ssize_t) image->columns-1); x+=2)
1172 ValidateColormapValue(image,(ssize_t) ((*p >> 4) & 0x0f),&index,
1174 SetPixelIndex(image,index,q);
1175 q+=GetPixelChannels(image);
1176 ValidateColormapValue(image,(ssize_t) (*p & 0x0f),&index,exception);
1177 SetPixelIndex(image,index,q);
1178 q+=GetPixelChannels(image);
1181 if ((image->columns % 2) != 0)
1183 ValidateColormapValue(image,(ssize_t) ((*p >> 4) & 0xf),&index,
1185 SetPixelIndex(image,index,q);
1186 q+=GetPixelChannels(image);
1190 if (x < (ssize_t) image->columns)
1192 if (SyncAuthenticPixels(image,exception) == MagickFalse)
1194 if (image->previous == (Image *) NULL)
1196 status=SetImageProgress(image,LoadImageTag,(MagickOffsetType)
1197 (image->rows-y),image->rows);
1198 if (status == MagickFalse)
1202 (void) SyncImage(image,exception);
1208 Convert PseudoColor scanline.
1210 if ((bmp_info.compression == BI_RLE8) ||
1211 (bmp_info.compression == BI_RLE4))
1212 bytes_per_line=image->columns;
1213 for (y=(ssize_t) image->rows-1; y >= 0; y--)
1215 p=pixels+(image->rows-y-1)*bytes_per_line;
1216 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
1217 if (q == (Quantum *) NULL)
1219 for (x=(ssize_t) image->columns; x != 0; --x)
1221 ValidateColormapValue(image,(ssize_t) *p++,&index,exception);
1222 SetPixelIndex(image,index,q);
1223 q+=GetPixelChannels(image);
1225 if (SyncAuthenticPixels(image,exception) == MagickFalse)
1227 offset=(MagickOffsetType) (image->rows-y-1);
1228 if (image->previous == (Image *) NULL)
1230 status=SetImageProgress(image,LoadImageTag,(MagickOffsetType)
1231 (image->rows-y),image->rows);
1232 if (status == MagickFalse)
1236 (void) SyncImage(image,exception);
1246 Convert bitfield encoded 16-bit PseudoColor scanline.
1248 if ((bmp_info.compression != BI_RGB) &&
1249 (bmp_info.compression != BI_BITFIELDS))
1251 pixel_info=RelinquishVirtualMemory(pixel_info);
1252 ThrowReaderException(CorruptImageError,
1253 "UnrecognizedImageCompression");
1255 bytes_per_line=2*(image->columns+image->columns % 2);
1256 image->storage_class=DirectClass;
1257 for (y=(ssize_t) image->rows-1; y >= 0; y--)
1259 p=pixels+(image->rows-y-1)*bytes_per_line;
1260 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
1261 if (q == (Quantum *) NULL)
1263 for (x=0; x < (ssize_t) image->columns; x++)
1265 pixel=(unsigned int) (*p++);
1267 red=((pixel & bmp_info.red_mask) << shift.red) >> 16;
1268 if (quantum_bits.red == 5)
1269 red|=((red & 0xe000) >> 5);
1270 if (quantum_bits.red <= 8)
1271 red|=((red & 0xff00) >> 8);
1272 green=((pixel & bmp_info.green_mask) << shift.green) >> 16;
1273 if (quantum_bits.green == 5)
1274 green|=((green & 0xe000) >> 5);
1275 if (quantum_bits.green == 6)
1276 green|=((green & 0xc000) >> 6);
1277 if (quantum_bits.green <= 8)
1278 green|=((green & 0xff00) >> 8);
1279 blue=((pixel & bmp_info.blue_mask) << shift.blue) >> 16;
1280 if (quantum_bits.blue == 5)
1281 blue|=((blue & 0xe000) >> 5);
1282 if (quantum_bits.blue <= 8)
1283 blue|=((blue & 0xff00) >> 8);
1284 SetPixelRed(image,ScaleShortToQuantum((unsigned short) red),q);
1285 SetPixelGreen(image,ScaleShortToQuantum((unsigned short) green),q);
1286 SetPixelBlue(image,ScaleShortToQuantum((unsigned short) blue),q);
1287 SetPixelAlpha(image,OpaqueAlpha,q);
1288 if (image->alpha_trait != UndefinedPixelTrait)
1290 alpha=((pixel & bmp_info.alpha_mask) << shift.alpha) >> 16;
1291 if (quantum_bits.alpha <= 8)
1292 alpha|=((alpha & 0xff00) >> 8);
1293 SetPixelAlpha(image,ScaleShortToQuantum(
1294 (unsigned short) alpha),q);
1296 q+=GetPixelChannels(image);
1298 if (SyncAuthenticPixels(image,exception) == MagickFalse)
1300 offset=(MagickOffsetType) (image->rows-y-1);
1301 if (image->previous == (Image *) NULL)
1303 status=SetImageProgress(image,LoadImageTag,(MagickOffsetType)
1304 (image->rows-y),image->rows);
1305 if (status == MagickFalse)
1314 Convert DirectColor scanline.
1316 bytes_per_line=4*((image->columns*24+31)/32);
1317 for (y=(ssize_t) image->rows-1; y >= 0; y--)
1319 p=pixels+(image->rows-y-1)*bytes_per_line;
1320 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
1321 if (q == (Quantum *) NULL)
1323 for (x=0; x < (ssize_t) image->columns; x++)
1325 SetPixelBlue(image,ScaleCharToQuantum(*p++),q);
1326 SetPixelGreen(image,ScaleCharToQuantum(*p++),q);
1327 SetPixelRed(image,ScaleCharToQuantum(*p++),q);
1328 SetPixelAlpha(image,OpaqueAlpha,q);
1329 q+=GetPixelChannels(image);
1331 if (SyncAuthenticPixels(image,exception) == MagickFalse)
1333 offset=(MagickOffsetType) (image->rows-y-1);
1334 if (image->previous == (Image *) NULL)
1336 status=SetImageProgress(image,LoadImageTag,(MagickOffsetType)
1337 (image->rows-y),image->rows);
1338 if (status == MagickFalse)
1347 Convert bitfield encoded DirectColor scanline.
1349 if ((bmp_info.compression != BI_RGB) &&
1350 (bmp_info.compression != BI_BITFIELDS))
1352 pixel_info=RelinquishVirtualMemory(pixel_info);
1353 ThrowReaderException(CorruptImageError,
1354 "UnrecognizedImageCompression");
1356 bytes_per_line=4*(image->columns);
1357 for (y=(ssize_t) image->rows-1; y >= 0; y--)
1363 p=pixels+(image->rows-y-1)*bytes_per_line;
1364 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
1365 if (q == (Quantum *) NULL)
1367 for (x=0; x < (ssize_t) image->columns; x++)
1369 pixel=(unsigned int) (*p++);
1370 pixel|=((unsigned int) *p++ << 8);
1371 pixel|=((unsigned int) *p++ << 16);
1372 pixel|=((unsigned int) *p++ << 24);
1373 red=((pixel & bmp_info.red_mask) << shift.red) >> 16;
1374 if (quantum_bits.red == 8)
1376 green=((pixel & bmp_info.green_mask) << shift.green) >> 16;
1377 if (quantum_bits.green == 8)
1378 green|=(green >> 8);
1379 blue=((pixel & bmp_info.blue_mask) << shift.blue) >> 16;
1380 if (quantum_bits.blue == 8)
1382 SetPixelRed(image,ScaleShortToQuantum((unsigned short) red),q);
1383 SetPixelGreen(image,ScaleShortToQuantum((unsigned short) green),q);
1384 SetPixelBlue(image,ScaleShortToQuantum((unsigned short) blue),q);
1385 SetPixelAlpha(image,OpaqueAlpha,q);
1386 if (image->alpha_trait != UndefinedPixelTrait)
1388 alpha=((pixel & bmp_info.alpha_mask) << shift.alpha) >> 16;
1389 if (quantum_bits.alpha == 8)
1390 alpha|=(alpha >> 8);
1391 SetPixelAlpha(image,ScaleShortToQuantum(
1392 (unsigned short) alpha),q);
1394 q+=GetPixelChannels(image);
1396 if (SyncAuthenticPixels(image,exception) == MagickFalse)
1398 offset=(MagickOffsetType) (image->rows-y-1);
1399 if (image->previous == (Image *) NULL)
1401 status=SetImageProgress(image,LoadImageTag,(MagickOffsetType)
1402 (image->rows-y),image->rows);
1403 if (status == MagickFalse)
1411 pixel_info=RelinquishVirtualMemory(pixel_info);
1412 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
1415 pixel_info=RelinquishVirtualMemory(pixel_info);
1418 if (EOFBlob(image) != MagickFalse)
1420 ThrowFileException(exception,CorruptImageError,"UnexpectedEndOfFile",
1424 if (bmp_info.height < 0)
1430 Correct image orientation.
1432 flipped_image=FlipImage(image,exception);
1433 if (flipped_image != (Image *) NULL)
1435 DuplicateBlob(flipped_image,image);
1436 ReplaceImageInList(&image, flipped_image);
1437 image=flipped_image;
1441 Proceed to next image.
1443 if (image_info->number_scenes != 0)
1444 if (image->scene >= (image_info->scene+image_info->number_scenes-1))
1447 if (bmp_info.ba_offset != 0)
1449 offset=SeekBlob(image,(MagickOffsetType) bmp_info.ba_offset,SEEK_SET);
1451 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
1453 count=ReadBlob(image,2,magick);
1454 if ((count == 2) && (IsBMP(magick,2) != MagickFalse))
1457 Acquire next image structure.
1459 AcquireNextImage(image_info,image,exception);
1460 if (GetNextImageInList(image) == (Image *) NULL)
1462 image=DestroyImageList(image);
1463 return((Image *) NULL);
1465 image=SyncNextImageInList(image);
1466 status=SetImageProgress(image,LoadImagesTag,TellBlob(image),
1467 GetBlobSize(image));
1468 if (status == MagickFalse)
1471 } while (IsBMP(magick,2) != MagickFalse);
1472 (void) CloseBlob(image);
1473 return(GetFirstImageInList(image));
1477 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1481 % R e g i s t e r B M P I m a g e %
1485 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1487 % RegisterBMPImage() adds attributes for the BMP image format to
1488 % the list of supported formats. The attributes include the image format
1489 % tag, a method to read and/or write the format, whether the format
1490 % supports the saving of more than one frame to the same file or blob,
1491 % whether the format supports native in-memory I/O, and a brief
1492 % description of the format.
1494 % The format of the RegisterBMPImage method is:
1496 % size_t RegisterBMPImage(void)
1499 ModuleExport size_t RegisterBMPImage(void)
1504 entry=AcquireMagickInfo("BMP","BMP","Microsoft Windows bitmap image");
1505 entry->decoder=(DecodeImageHandler *) ReadBMPImage;
1506 entry->encoder=(EncodeImageHandler *) WriteBMPImage;
1507 entry->magick=(IsImageFormatHandler *) IsBMP;
1508 entry->flags^=CoderAdjoinFlag;
1509 entry->flags|=CoderDecoderSeekableStreamFlag;
1510 (void) RegisterMagickInfo(entry);
1511 entry=AcquireMagickInfo("BMP","BMP2","Microsoft Windows bitmap image (V2)");
1512 entry->decoder=(DecodeImageHandler *) ReadBMPImage;
1513 entry->encoder=(EncodeImageHandler *) WriteBMPImage;
1514 entry->magick=(IsImageFormatHandler *) IsBMP;
1515 entry->flags^=CoderAdjoinFlag;
1516 entry->flags|=CoderDecoderSeekableStreamFlag;
1517 (void) RegisterMagickInfo(entry);
1518 entry=AcquireMagickInfo("BMP","BMP3","Microsoft Windows bitmap image (V3)");
1519 entry->decoder=(DecodeImageHandler *) ReadBMPImage;
1520 entry->encoder=(EncodeImageHandler *) WriteBMPImage;
1521 entry->magick=(IsImageFormatHandler *) IsBMP;
1522 entry->flags^=CoderAdjoinFlag;
1523 entry->flags|=CoderDecoderSeekableStreamFlag;
1524 (void) RegisterMagickInfo(entry);
1525 return(MagickImageCoderSignature);
1529 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1533 % U n r e g i s t e r B M P I m a g e %
1537 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1539 % UnregisterBMPImage() removes format registrations made by the
1540 % BMP module from the list of supported formats.
1542 % The format of the UnregisterBMPImage method is:
1544 % UnregisterBMPImage(void)
1547 ModuleExport void UnregisterBMPImage(void)
1549 (void) UnregisterMagickInfo("BMP");
1550 (void) UnregisterMagickInfo("BMP2");
1551 (void) UnregisterMagickInfo("BMP3");
1555 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1559 % W r i t e B M P I m a g e %
1563 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1565 % WriteBMPImage() writes an image in Microsoft Windows bitmap encoded
1566 % image format, version 3 for Windows or (if the image has a matte channel)
1569 % The format of the WriteBMPImage method is:
1571 % MagickBooleanType WriteBMPImage(const ImageInfo *image_info,
1572 % Image *image,ExceptionInfo *exception)
1574 % A description of each parameter follows.
1576 % o image_info: the image info.
1578 % o image: The image.
1580 % o exception: return any errors or warnings in this structure.
1583 static MagickBooleanType WriteBMPImage(const ImageInfo *image_info,Image *image,
1584 ExceptionInfo *exception)
1608 register const Quantum
1615 register unsigned char
1631 Open output image file.
1633 assert(image_info != (const ImageInfo *) NULL);
1634 assert(image_info->signature == MagickCoreSignature);
1635 assert(image != (Image *) NULL);
1636 assert(image->signature == MagickCoreSignature);
1637 if (image->debug != MagickFalse)
1638 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1639 assert(exception != (ExceptionInfo *) NULL);
1640 assert(exception->signature == MagickCoreSignature);
1641 status=OpenBlob(image_info,image,WriteBinaryBlobMode,exception);
1642 if (status == MagickFalse)
1645 if (LocaleCompare(image_info->magick,"BMP2") == 0)
1648 if (LocaleCompare(image_info->magick,"BMP3") == 0)
1650 option=GetImageOption(image_info,"bmp:format");
1651 if (option != (char *) NULL)
1653 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1654 " Format=%s",option);
1655 if (LocaleCompare(option,"bmp2") == 0)
1657 if (LocaleCompare(option,"bmp3") == 0)
1659 if (LocaleCompare(option,"bmp4") == 0)
1663 imageListLength=GetImageListLength(image);
1667 Initialize BMP raster file header.
1669 if (IssRGBCompatibleColorspace(image->colorspace) == MagickFalse)
1670 (void) TransformImageColorspace(image,sRGBColorspace,exception);
1671 (void) memset(&bmp_info,0,sizeof(bmp_info));
1672 bmp_info.file_size=14+12;
1674 bmp_info.file_size+=28;
1675 bmp_info.offset_bits=bmp_info.file_size;
1676 bmp_info.compression=BI_RGB;
1677 bmp_info.red_mask=0x00ff0000U;
1678 bmp_info.green_mask=0x0000ff00U;
1679 bmp_info.blue_mask=0x000000ffU;
1680 bmp_info.alpha_mask=0xff000000U;
1681 bmp_subtype=UndefinedSubtype;
1682 if ((image->storage_class == PseudoClass) && (image->colors > 256))
1683 (void) SetImageStorageClass(image,DirectClass,exception);
1684 if (image->storage_class != DirectClass)
1687 Colormapped BMP raster.
1689 bmp_info.bits_per_pixel=8;
1690 if (image->colors <= 2)
1691 bmp_info.bits_per_pixel=1;
1693 if (image->colors <= 16)
1694 bmp_info.bits_per_pixel=4;
1696 if (image->colors <= 256)
1697 bmp_info.bits_per_pixel=8;
1698 if (image_info->compression == RLECompression)
1699 bmp_info.bits_per_pixel=8;
1700 bmp_info.number_colors=1U << bmp_info.bits_per_pixel;
1701 if (image->alpha_trait != UndefinedPixelTrait)
1702 (void) SetImageStorageClass(image,DirectClass,exception);
1704 if ((size_t) bmp_info.number_colors < image->colors)
1705 (void) SetImageStorageClass(image,DirectClass,exception);
1708 bmp_info.file_size+=3*(1UL << bmp_info.bits_per_pixel);
1709 bmp_info.offset_bits+=3*(1UL << bmp_info.bits_per_pixel);
1712 bmp_info.file_size+=(1UL << bmp_info.bits_per_pixel);
1713 bmp_info.offset_bits+=(1UL << bmp_info.bits_per_pixel);
1717 if (image->storage_class == DirectClass)
1720 Full color BMP raster.
1722 bmp_info.number_colors=0;
1723 option=GetImageOption(image_info,"bmp:subtype");
1724 if (option != (const char *) NULL)
1726 if (image->alpha_trait != UndefinedPixelTrait)
1728 if (LocaleNCompare(option,"ARGB4444",8) == 0)
1730 bmp_subtype=ARGB4444;
1731 bmp_info.red_mask=0x00000f00U;
1732 bmp_info.green_mask=0x000000f0U;
1733 bmp_info.blue_mask=0x0000000fU;
1734 bmp_info.alpha_mask=0x0000f000U;
1736 else if (LocaleNCompare(option,"ARGB1555",8) == 0)
1738 bmp_subtype=ARGB1555;
1739 bmp_info.red_mask=0x00007c00U;
1740 bmp_info.green_mask=0x000003e0U;
1741 bmp_info.blue_mask=0x0000001fU;
1742 bmp_info.alpha_mask=0x00008000U;
1747 if (LocaleNCompare(option,"RGB555",6) == 0)
1750 bmp_info.red_mask=0x00007c00U;
1751 bmp_info.green_mask=0x000003e0U;
1752 bmp_info.blue_mask=0x0000001fU;
1753 bmp_info.alpha_mask=0U;
1755 else if (LocaleNCompare(option,"RGB565",6) == 0)
1758 bmp_info.red_mask=0x0000f800U;
1759 bmp_info.green_mask=0x000007e0U;
1760 bmp_info.blue_mask=0x0000001fU;
1761 bmp_info.alpha_mask=0U;
1765 if (bmp_subtype != UndefinedSubtype)
1767 bmp_info.bits_per_pixel=16;
1768 bmp_info.compression=BI_BITFIELDS;
1772 bmp_info.bits_per_pixel=(unsigned short) ((type > 3) &&
1773 (image->alpha_trait != UndefinedPixelTrait) ? 32 : 24);
1774 bmp_info.compression=(unsigned int) ((type > 3) &&
1775 (image->alpha_trait != UndefinedPixelTrait) ? BI_BITFIELDS : BI_RGB);
1776 if ((type == 3) && (image->alpha_trait != UndefinedPixelTrait))
1778 option=GetImageOption(image_info,"bmp3:alpha");
1779 if (IsStringTrue(option))
1780 bmp_info.bits_per_pixel=32;
1784 bytes_per_line=4*((image->columns*bmp_info.bits_per_pixel+31)/32);
1785 bmp_info.ba_offset=0;
1786 profile=GetImageProfile(image,"icc");
1787 have_color_info=(image->rendering_intent != UndefinedIntent) ||
1788 (profile != (StringInfo *) NULL) || (image->gamma != 0.0) ? MagickTrue :
1793 if ((type == 3) || ((image->alpha_trait == UndefinedPixelTrait) &&
1794 (have_color_info == MagickFalse)))
1806 if ((image->rendering_intent != UndefinedIntent) ||
1807 (profile != (StringInfo *) NULL))
1812 bmp_info.file_size+=extra_size;
1813 bmp_info.offset_bits+=extra_size;
1815 if (((ssize_t) image->columns != (ssize_t) ((signed int) image->columns)) ||
1816 ((ssize_t) image->rows != (ssize_t) ((signed int) image->rows)))
1817 ThrowWriterException(ImageError,"WidthOrHeightExceedsLimit");
1818 bmp_info.width=(ssize_t) image->columns;
1819 bmp_info.height=(ssize_t) image->rows;
1821 bmp_info.image_size=(unsigned int) (bytes_per_line*image->rows);
1822 bmp_info.file_size+=bmp_info.image_size;
1823 bmp_info.x_pixels=75*39;
1824 bmp_info.y_pixels=75*39;
1825 switch (image->units)
1827 case UndefinedResolution:
1828 case PixelsPerInchResolution:
1830 bmp_info.x_pixels=(unsigned int) (100.0*image->resolution.x/2.54);
1831 bmp_info.y_pixels=(unsigned int) (100.0*image->resolution.y/2.54);
1834 case PixelsPerCentimeterResolution:
1836 bmp_info.x_pixels=(unsigned int) (100.0*image->resolution.x);
1837 bmp_info.y_pixels=(unsigned int) (100.0*image->resolution.y);
1841 bmp_info.colors_important=bmp_info.number_colors;
1843 Convert MIFF to BMP raster pixels.
1845 pixel_info=AcquireVirtualMemory((size_t) bmp_info.image_size,
1847 if (pixel_info == (MemoryInfo *) NULL)
1848 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
1849 pixels=(unsigned char *) GetVirtualMemoryBlob(pixel_info);
1850 (void) memset(pixels,0,(size_t) bmp_info.image_size);
1851 switch (bmp_info.bits_per_pixel)
1860 Convert PseudoClass image to a BMP monochrome image.
1862 for (y=0; y < (ssize_t) image->rows; y++)
1867 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
1868 if (p == (const Quantum *) NULL)
1870 q=pixels+(image->rows-y-1)*bytes_per_line;
1873 for (x=0; x < (ssize_t) image->columns; x++)
1876 byte|=GetPixelIndex(image,p) != 0 ? 0x01 : 0x00;
1880 *q++=(unsigned char) byte;
1884 p+=GetPixelChannels(image);
1888 *q++=(unsigned char) (byte << (8-bit));
1891 offset=(ssize_t) (image->columns+7)/8;
1892 for (x=offset; x < (ssize_t) bytes_per_line; x++)
1894 if (image->previous == (Image *) NULL)
1896 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
1898 if (status == MagickFalse)
1914 Convert PseudoClass image to a BMP monochrome image.
1916 for (y=0; y < (ssize_t) image->rows; y++)
1918 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
1919 if (p == (const Quantum *) NULL)
1921 q=pixels+(image->rows-y-1)*bytes_per_line;
1924 for (x=0; x < (ssize_t) image->columns; x++)
1927 byte|=((unsigned int) GetPixelIndex(image,p) & 0x0f);
1931 *q++=(unsigned char) byte;
1935 p+=GetPixelChannels(image);
1939 *q++=(unsigned char) (byte << 4);
1942 offset=(ssize_t) (image->columns+1)/2;
1943 for (x=offset; x < (ssize_t) bytes_per_line; x++)
1945 if (image->previous == (Image *) NULL)
1947 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
1949 if (status == MagickFalse)
1958 Convert PseudoClass packet to BMP pixel.
1960 for (y=0; y < (ssize_t) image->rows; y++)
1962 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
1963 if (p == (const Quantum *) NULL)
1965 q=pixels+(image->rows-y-1)*bytes_per_line;
1966 for (x=0; x < (ssize_t) image->columns; x++)
1968 *q++=(unsigned char) GetPixelIndex(image,p);
1969 p+=GetPixelChannels(image);
1971 for ( ; x < (ssize_t) bytes_per_line; x++)
1973 if (image->previous == (Image *) NULL)
1975 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
1977 if (status == MagickFalse)
1986 Convert DirectClass packet to BMP BGR888.
1988 for (y=0; y < (ssize_t) image->rows; y++)
1990 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
1991 if (p == (const Quantum *) NULL)
1993 q=pixels+(image->rows-y-1)*bytes_per_line;
1994 for (x=0; x < (ssize_t) image->columns; x++)
2000 if (bmp_subtype == ARGB4444)
2002 pixel=(unsigned short) (ScaleQuantumToAny(
2003 GetPixelAlpha(image,p),15) << 12);
2004 pixel|=(unsigned short) (ScaleQuantumToAny(
2005 GetPixelRed(image,p),15) << 8);
2006 pixel|=(unsigned short) (ScaleQuantumToAny(
2007 GetPixelGreen(image,p),15) << 4);
2008 pixel|=(unsigned short) (ScaleQuantumToAny(
2009 GetPixelBlue(image,p),15));
2011 else if (bmp_subtype == RGB565)
2013 pixel=(unsigned short) (ScaleQuantumToAny(
2014 GetPixelRed(image,p),31) << 11);
2015 pixel|=(unsigned short) (ScaleQuantumToAny(
2016 GetPixelGreen(image,p),63) << 5);
2017 pixel|=(unsigned short) (ScaleQuantumToAny(
2018 GetPixelBlue(image,p),31));
2022 if (bmp_subtype == ARGB1555)
2023 pixel=(unsigned short) (ScaleQuantumToAny(
2024 GetPixelAlpha(image,p),1) << 15);
2025 pixel|=(unsigned short) (ScaleQuantumToAny(
2026 GetPixelRed(image,p),31) << 10);
2027 pixel|=(unsigned short) (ScaleQuantumToAny(
2028 GetPixelGreen(image,p),31) << 5);
2029 pixel|=(unsigned short) (ScaleQuantumToAny(
2030 GetPixelBlue(image,p),31));
2032 *((unsigned short *) q)=pixel;
2034 p+=GetPixelChannels(image);
2036 for (x=2L*(ssize_t) image->columns; x < (ssize_t) bytes_per_line; x++)
2038 if (image->previous == (Image *) NULL)
2040 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
2042 if (status == MagickFalse)
2051 Convert DirectClass packet to BMP BGR888.
2053 for (y=0; y < (ssize_t) image->rows; y++)
2055 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
2056 if (p == (const Quantum *) NULL)
2058 q=pixels+(image->rows-y-1)*bytes_per_line;
2059 for (x=0; x < (ssize_t) image->columns; x++)
2061 *q++=ScaleQuantumToChar(GetPixelBlue(image,p));
2062 *q++=ScaleQuantumToChar(GetPixelGreen(image,p));
2063 *q++=ScaleQuantumToChar(GetPixelRed(image,p));
2064 p+=GetPixelChannels(image);
2066 for (x=3L*(ssize_t) image->columns; x < (ssize_t) bytes_per_line; x++)
2068 if (image->previous == (Image *) NULL)
2070 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
2072 if (status == MagickFalse)
2081 Convert DirectClass packet to ARGB8888 pixel.
2083 for (y=0; y < (ssize_t) image->rows; y++)
2085 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
2086 if (p == (const Quantum *) NULL)
2088 q=pixels+(image->rows-y-1)*bytes_per_line;
2089 for (x=0; x < (ssize_t) image->columns; x++)
2091 *q++=ScaleQuantumToChar(GetPixelBlue(image,p));
2092 *q++=ScaleQuantumToChar(GetPixelGreen(image,p));
2093 *q++=ScaleQuantumToChar(GetPixelRed(image,p));
2094 *q++=ScaleQuantumToChar(GetPixelAlpha(image,p));
2095 p+=GetPixelChannels(image);
2097 if (image->previous == (Image *) NULL)
2099 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
2101 if (status == MagickFalse)
2108 if ((type > 2) && (bmp_info.bits_per_pixel == 8))
2109 if (image_info->compression != NoCompression)
2115 Convert run-length encoded raster pixels.
2117 rle_info=AcquireVirtualMemory((size_t) (2*(bytes_per_line+2)+2),
2118 (image->rows+2)*sizeof(*pixels));
2119 if (rle_info == (MemoryInfo *) NULL)
2121 pixel_info=RelinquishVirtualMemory(pixel_info);
2122 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
2124 bmp_data=(unsigned char *) GetVirtualMemoryBlob(rle_info);
2125 bmp_info.file_size-=bmp_info.image_size;
2126 bmp_info.image_size=(unsigned int) EncodeImage(image,bytes_per_line,
2128 bmp_info.file_size+=bmp_info.image_size;
2129 pixel_info=RelinquishVirtualMemory(pixel_info);
2130 pixel_info=rle_info;
2132 bmp_info.compression=BI_RLE8;
2135 Write BMP for Windows, all versions, 14-byte header.
2137 if (image->debug != MagickFalse)
2139 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
2140 " Writing BMP version %.20g datastream",(double) type);
2141 if (image->storage_class == DirectClass)
2142 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
2143 " Storage class=DirectClass");
2145 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
2146 " Storage class=PseudoClass");
2147 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
2148 " Image depth=%.20g",(double) image->depth);
2149 if (image->alpha_trait != UndefinedPixelTrait)
2150 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
2153 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
2154 " Matte=MagickFalse");
2155 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
2156 " BMP bits_per_pixel=%.20g",(double) bmp_info.bits_per_pixel);
2157 switch ((int) bmp_info.compression)
2161 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
2162 " Compression=BI_RGB");
2167 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
2168 " Compression=BI_RLE8");
2173 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
2174 " Compression=BI_BITFIELDS");
2179 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
2180 " Compression=UNKNOWN (%u)",bmp_info.compression);
2184 if (bmp_info.number_colors == 0)
2185 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
2186 " Number_colors=unspecified");
2188 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
2189 " Number_colors=%u",bmp_info.number_colors);
2191 (void) WriteBlob(image,2,(unsigned char *) "BM");
2192 (void) WriteBlobLSBLong(image,bmp_info.file_size);
2193 (void) WriteBlobLSBLong(image,bmp_info.ba_offset); /* always 0 */
2194 (void) WriteBlobLSBLong(image,bmp_info.offset_bits);
2198 Write 12-byte version 2 bitmap header.
2200 (void) WriteBlobLSBLong(image,bmp_info.size);
2201 (void) WriteBlobLSBSignedShort(image,(signed short) bmp_info.width);
2202 (void) WriteBlobLSBSignedShort(image,(signed short) bmp_info.height);
2203 (void) WriteBlobLSBShort(image,bmp_info.planes);
2204 (void) WriteBlobLSBShort(image,bmp_info.bits_per_pixel);
2209 Write 40-byte version 3+ bitmap header.
2211 (void) WriteBlobLSBLong(image,bmp_info.size);
2212 (void) WriteBlobLSBSignedLong(image,(signed int) bmp_info.width);
2213 (void) WriteBlobLSBSignedLong(image,(signed int) bmp_info.height);
2214 (void) WriteBlobLSBShort(image,bmp_info.planes);
2215 (void) WriteBlobLSBShort(image,bmp_info.bits_per_pixel);
2216 (void) WriteBlobLSBLong(image,bmp_info.compression);
2217 (void) WriteBlobLSBLong(image,bmp_info.image_size);
2218 (void) WriteBlobLSBLong(image,bmp_info.x_pixels);
2219 (void) WriteBlobLSBLong(image,bmp_info.y_pixels);
2220 (void) WriteBlobLSBLong(image,bmp_info.number_colors);
2221 (void) WriteBlobLSBLong(image,bmp_info.colors_important);
2223 if ((type > 3) && ((image->alpha_trait != UndefinedPixelTrait) ||
2224 (have_color_info != MagickFalse)))
2227 Write the rest of the 108-byte BMP Version 4 header.
2229 (void) WriteBlobLSBLong(image,bmp_info.red_mask);
2230 (void) WriteBlobLSBLong(image,bmp_info.green_mask);
2231 (void) WriteBlobLSBLong(image,bmp_info.blue_mask);
2232 (void) WriteBlobLSBLong(image,bmp_info.alpha_mask);
2233 (void) WriteBlobLSBLong(image,0x73524742U); /* sRGB */
2234 (void) WriteBlobLSBLong(image,(unsigned int)
2235 (image->chromaticity.red_primary.x*0x40000000));
2236 (void) WriteBlobLSBLong(image,(unsigned int)
2237 (image->chromaticity.red_primary.y*0x40000000));
2238 (void) WriteBlobLSBLong(image,(unsigned int)
2239 ((1.000f-(image->chromaticity.red_primary.x+
2240 image->chromaticity.red_primary.y))*0x40000000));
2241 (void) WriteBlobLSBLong(image,(unsigned int)
2242 (image->chromaticity.green_primary.x*0x40000000));
2243 (void) WriteBlobLSBLong(image,(unsigned int)
2244 (image->chromaticity.green_primary.y*0x40000000));
2245 (void) WriteBlobLSBLong(image,(unsigned int)
2246 ((1.000f-(image->chromaticity.green_primary.x+
2247 image->chromaticity.green_primary.y))*0x40000000));
2248 (void) WriteBlobLSBLong(image,(unsigned int)
2249 (image->chromaticity.blue_primary.x*0x40000000));
2250 (void) WriteBlobLSBLong(image,(unsigned int)
2251 (image->chromaticity.blue_primary.y*0x40000000));
2252 (void) WriteBlobLSBLong(image,(unsigned int)
2253 ((1.000f-(image->chromaticity.blue_primary.x+
2254 image->chromaticity.blue_primary.y))*0x40000000));
2255 (void) WriteBlobLSBLong(image,(unsigned int)
2256 (bmp_info.gamma_scale.x*0x10000));
2257 (void) WriteBlobLSBLong(image,(unsigned int)
2258 (bmp_info.gamma_scale.y*0x10000));
2259 (void) WriteBlobLSBLong(image,(unsigned int)
2260 (bmp_info.gamma_scale.z*0x10000));
2261 if ((image->rendering_intent != UndefinedIntent) ||
2262 (profile != (StringInfo *) NULL))
2267 switch ((int) image->rendering_intent)
2269 case SaturationIntent:
2271 intent=LCS_GM_BUSINESS;
2274 case RelativeIntent:
2276 intent=LCS_GM_GRAPHICS;
2279 case PerceptualIntent:
2281 intent=LCS_GM_IMAGES;
2284 case AbsoluteIntent:
2286 intent=LCS_GM_ABS_COLORIMETRIC;
2295 (void) WriteBlobLSBLong(image,(unsigned int) intent);
2296 (void) WriteBlobLSBLong(image,0x00); /* dummy profile data */
2297 (void) WriteBlobLSBLong(image,0x00); /* dummy profile length */
2298 (void) WriteBlobLSBLong(image,0x00); /* reserved */
2301 if (image->storage_class == PseudoClass)
2307 Dump colormap to file.
2309 if (image->debug != MagickFalse)
2310 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
2311 " Colormap: %.20g entries",(double) image->colors);
2312 bmp_colormap=(unsigned char *) AcquireQuantumMemory((size_t) (1UL <<
2313 bmp_info.bits_per_pixel),4*sizeof(*bmp_colormap));
2314 if (bmp_colormap == (unsigned char *) NULL)
2315 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
2317 for (i=0; i < (ssize_t) MagickMin((ssize_t) image->colors,(ssize_t) bmp_info.number_colors); i++)
2319 *q++=ScaleQuantumToChar(ClampToQuantum(image->colormap[i].blue));
2320 *q++=ScaleQuantumToChar(ClampToQuantum(image->colormap[i].green));
2321 *q++=ScaleQuantumToChar(ClampToQuantum(image->colormap[i].red));
2323 *q++=(unsigned char) 0x0;
2325 for ( ; i < (ssize_t) (1UL << bmp_info.bits_per_pixel); i++)
2327 *q++=(unsigned char) 0x00;
2328 *q++=(unsigned char) 0x00;
2329 *q++=(unsigned char) 0x00;
2331 *q++=(unsigned char) 0x00;
2334 (void) WriteBlob(image,(size_t) (3*(1L << bmp_info.bits_per_pixel)),
2337 (void) WriteBlob(image,(size_t) (4*(1L << bmp_info.bits_per_pixel)),
2339 bmp_colormap=(unsigned char *) RelinquishMagickMemory(bmp_colormap);
2341 if (image->debug != MagickFalse)
2342 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
2343 " Pixels: %u bytes",bmp_info.image_size);
2344 (void) WriteBlob(image,(size_t) bmp_info.image_size,pixels);
2345 pixel_info=RelinquishVirtualMemory(pixel_info);
2346 if (GetNextImageInList(image) == (Image *) NULL)
2348 image=SyncNextImageInList(image);
2349 status=SetImageProgress(image,SaveImagesTag,scene++,imageListLength);
2350 if (status == MagickFalse)
2352 } while (image_info->adjoin != MagickFalse);
2353 (void) CloseBlob(image);