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 if (bmp_info.number_colors > GetBlobSize(image))
665 ThrowReaderException(CorruptImageError,"InsufficientImageDataInFile");
666 bmp_info.colors_important=ReadBlobLSBLong(image);
667 if (image->debug != MagickFalse)
669 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
670 " Format: MS Windows bitmap");
671 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
672 " Geometry: %.20gx%.20g",(double) bmp_info.width,(double)
674 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
675 " Bits per pixel: %.20g",(double) bmp_info.bits_per_pixel);
676 switch (bmp_info.compression)
680 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
681 " Compression: BI_RGB");
686 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
687 " Compression: BI_RLE4");
692 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
693 " Compression: BI_RLE8");
698 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
699 " Compression: BI_BITFIELDS");
704 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
705 " Compression: BI_PNG");
710 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
711 " Compression: BI_JPEG");
716 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
717 " Compression: UNKNOWN (%u)",bmp_info.compression);
720 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
721 " Number of colors: %u",bmp_info.number_colors);
723 bmp_info.red_mask=ReadBlobLSBLong(image);
724 bmp_info.green_mask=ReadBlobLSBLong(image);
725 bmp_info.blue_mask=ReadBlobLSBLong(image);
726 if (bmp_info.size > 40)
732 Read color management information.
734 bmp_info.alpha_mask=ReadBlobLSBLong(image);
735 bmp_info.colorspace=ReadBlobLSBSignedLong(image);
737 Decode 2^30 fixed point formatted CIE primaries.
739 # define BMP_DENOM ((double) 0x40000000)
740 bmp_info.red_primary.x=(double) ReadBlobLSBLong(image)/BMP_DENOM;
741 bmp_info.red_primary.y=(double) ReadBlobLSBLong(image)/BMP_DENOM;
742 bmp_info.red_primary.z=(double) ReadBlobLSBLong(image)/BMP_DENOM;
743 bmp_info.green_primary.x=(double) ReadBlobLSBLong(image)/BMP_DENOM;
744 bmp_info.green_primary.y=(double) ReadBlobLSBLong(image)/BMP_DENOM;
745 bmp_info.green_primary.z=(double) ReadBlobLSBLong(image)/BMP_DENOM;
746 bmp_info.blue_primary.x=(double) ReadBlobLSBLong(image)/BMP_DENOM;
747 bmp_info.blue_primary.y=(double) ReadBlobLSBLong(image)/BMP_DENOM;
748 bmp_info.blue_primary.z=(double) ReadBlobLSBLong(image)/BMP_DENOM;
750 gamma=bmp_info.red_primary.x+bmp_info.red_primary.y+
751 bmp_info.red_primary.z;
752 gamma=PerceptibleReciprocal(gamma);
753 bmp_info.red_primary.x*=gamma;
754 bmp_info.red_primary.y*=gamma;
755 image->chromaticity.red_primary.x=bmp_info.red_primary.x;
756 image->chromaticity.red_primary.y=bmp_info.red_primary.y;
758 gamma=bmp_info.green_primary.x+bmp_info.green_primary.y+
759 bmp_info.green_primary.z;
760 gamma=PerceptibleReciprocal(gamma);
761 bmp_info.green_primary.x*=gamma;
762 bmp_info.green_primary.y*=gamma;
763 image->chromaticity.green_primary.x=bmp_info.green_primary.x;
764 image->chromaticity.green_primary.y=bmp_info.green_primary.y;
766 gamma=bmp_info.blue_primary.x+bmp_info.blue_primary.y+
767 bmp_info.blue_primary.z;
768 gamma=PerceptibleReciprocal(gamma);
769 bmp_info.blue_primary.x*=gamma;
770 bmp_info.blue_primary.y*=gamma;
771 image->chromaticity.blue_primary.x=bmp_info.blue_primary.x;
772 image->chromaticity.blue_primary.y=bmp_info.blue_primary.y;
775 Decode 16^16 fixed point formatted gamma_scales.
777 bmp_info.gamma_scale.x=(double) ReadBlobLSBLong(image)/0x10000;
778 bmp_info.gamma_scale.y=(double) ReadBlobLSBLong(image)/0x10000;
779 bmp_info.gamma_scale.z=(double) ReadBlobLSBLong(image)/0x10000;
781 Compute a single gamma from the BMP 3-channel gamma.
783 image->gamma=(bmp_info.gamma_scale.x+bmp_info.gamma_scale.y+
784 bmp_info.gamma_scale.z)/3.0;
787 (void) CopyMagickString(image->magick,"BMP3",MagickPathExtent);
789 if (bmp_info.size > 108)
795 Read BMP Version 5 color management information.
797 intent=ReadBlobLSBLong(image);
798 switch ((int) intent)
800 case LCS_GM_BUSINESS:
802 image->rendering_intent=SaturationIntent;
805 case LCS_GM_GRAPHICS:
807 image->rendering_intent=RelativeIntent;
812 image->rendering_intent=PerceptualIntent;
815 case LCS_GM_ABS_COLORIMETRIC:
817 image->rendering_intent=AbsoluteIntent;
821 (void) ReadBlobLSBLong(image); /* Profile data */
822 (void) ReadBlobLSBLong(image); /* Profile size */
823 (void) ReadBlobLSBLong(image); /* Reserved byte */
826 if ((MagickSizeType) bmp_info.file_size > GetBlobSize(image))
827 (void) ThrowMagickException(exception,GetMagickModule(),CorruptImageError,
828 "LengthAndFilesizeDoNotMatch","`%s'",image->filename);
830 if ((MagickSizeType) bmp_info.file_size < GetBlobSize(image))
831 (void) ThrowMagickException(exception,GetMagickModule(),
832 CorruptImageWarning,"LengthAndFilesizeDoNotMatch","`%s'",
834 if (bmp_info.width <= 0)
835 ThrowReaderException(CorruptImageError,"NegativeOrZeroImageSize");
836 if (bmp_info.height == 0)
837 ThrowReaderException(CorruptImageError,"NegativeOrZeroImageSize");
838 if (bmp_info.planes != 1)
839 ThrowReaderException(CorruptImageError,"StaticPlanesValueNotEqualToOne");
840 if ((bmp_info.bits_per_pixel != 1) && (bmp_info.bits_per_pixel != 4) &&
841 (bmp_info.bits_per_pixel != 8) && (bmp_info.bits_per_pixel != 16) &&
842 (bmp_info.bits_per_pixel != 24) && (bmp_info.bits_per_pixel != 32))
843 ThrowReaderException(CorruptImageError,"UnrecognizedBitsPerPixel");
844 if (bmp_info.bits_per_pixel < 16 &&
845 bmp_info.number_colors > (1U << bmp_info.bits_per_pixel))
846 ThrowReaderException(CorruptImageError,"UnrecognizedNumberOfColors");
847 if ((bmp_info.compression == 1) && (bmp_info.bits_per_pixel != 8))
848 ThrowReaderException(CorruptImageError,"UnrecognizedBitsPerPixel");
849 if ((bmp_info.compression == 2) && (bmp_info.bits_per_pixel != 4))
850 ThrowReaderException(CorruptImageError,"UnrecognizedBitsPerPixel");
851 if ((bmp_info.compression == 3) && (bmp_info.bits_per_pixel < 16))
852 ThrowReaderException(CorruptImageError,"UnrecognizedBitsPerPixel");
853 switch (bmp_info.compression)
856 image->compression=NoCompression;
860 image->compression=RLECompression;
865 ThrowReaderException(CoderError,"JPEGCompressNotSupported");
867 ThrowReaderException(CoderError,"PNGCompressNotSupported");
869 ThrowReaderException(CorruptImageError,"UnrecognizedImageCompression");
871 image->columns=(size_t) MagickAbsoluteValue(bmp_info.width);
872 image->rows=(size_t) MagickAbsoluteValue(bmp_info.height);
873 image->depth=bmp_info.bits_per_pixel <= 8 ? bmp_info.bits_per_pixel : 8;
874 image->alpha_trait=((bmp_info.alpha_mask != 0) &&
875 (bmp_info.compression == BI_BITFIELDS)) ? BlendPixelTrait :
877 if (bmp_info.bits_per_pixel < 16)
882 image->storage_class=PseudoClass;
883 image->colors=bmp_info.number_colors;
885 if (image->colors == 0)
886 image->colors=one << bmp_info.bits_per_pixel;
888 image->resolution.x=(double) bmp_info.x_pixels/100.0;
889 image->resolution.y=(double) bmp_info.y_pixels/100.0;
890 image->units=PixelsPerCentimeterResolution;
891 if ((image_info->ping != MagickFalse) && (image_info->number_scenes != 0))
892 if (image->scene >= (image_info->scene+image_info->number_scenes-1))
894 status=SetImageExtent(image,image->columns,image->rows,exception);
895 if (status == MagickFalse)
896 return(DestroyImageList(image));
897 if (image->storage_class == PseudoClass)
906 Read BMP raster colormap.
908 if (image->debug != MagickFalse)
909 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
910 " Reading colormap of %.20g colors",(double) image->colors);
911 if (AcquireImageColormap(image,image->colors,exception) == MagickFalse)
912 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
913 bmp_colormap=(unsigned char *) AcquireQuantumMemory((size_t)
914 image->colors,4*sizeof(*bmp_colormap));
915 if (bmp_colormap == (unsigned char *) NULL)
916 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
917 if ((bmp_info.size == 12) || (bmp_info.size == 64))
921 offset=SeekBlob(image,start_position+14+bmp_info.size,SEEK_SET);
924 bmp_colormap=(unsigned char *) RelinquishMagickMemory(bmp_colormap);
925 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
927 count=ReadBlob(image,packet_size*image->colors,bmp_colormap);
928 if (count != (ssize_t) (packet_size*image->colors))
930 bmp_colormap=(unsigned char *) RelinquishMagickMemory(bmp_colormap);
931 ThrowReaderException(CorruptImageError,
932 "InsufficientImageDataInFile");
935 for (i=0; i < (ssize_t) image->colors; i++)
937 image->colormap[i].blue=(MagickRealType) ScaleCharToQuantum(*p++);
938 image->colormap[i].green=(MagickRealType) ScaleCharToQuantum(*p++);
939 image->colormap[i].red=(MagickRealType) ScaleCharToQuantum(*p++);
940 if (packet_size == 4)
943 bmp_colormap=(unsigned char *) RelinquishMagickMemory(bmp_colormap);
948 if (bmp_info.offset_bits == offset_bits)
949 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
950 offset_bits=bmp_info.offset_bits;
951 offset=SeekBlob(image,start_position+bmp_info.offset_bits,SEEK_SET);
953 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
954 if (bmp_info.compression == BI_RLE4)
955 bmp_info.bits_per_pixel<<=1;
956 bytes_per_line=4*((image->columns*bmp_info.bits_per_pixel+31)/32);
957 length=(size_t) bytes_per_line*image->rows;
958 if (((MagickSizeType) length/8) > GetBlobSize(image))
959 ThrowReaderException(CorruptImageError,"InsufficientImageDataInFile");
960 if ((bmp_info.compression == BI_RGB) ||
961 (bmp_info.compression == BI_BITFIELDS))
963 pixel_info=AcquireVirtualMemory(image->rows,
964 MagickMax(bytes_per_line,image->columns+256UL)*sizeof(*pixels));
965 if (pixel_info == (MemoryInfo *) NULL)
966 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
967 pixels=(unsigned char *) GetVirtualMemoryBlob(pixel_info);
968 if (image->debug != MagickFalse)
969 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
970 " Reading pixels (%.20g bytes)",(double) length);
971 count=ReadBlob(image,length,pixels);
972 if (count != (ssize_t) length)
974 pixel_info=RelinquishVirtualMemory(pixel_info);
975 ThrowReaderException(CorruptImageError,
976 "InsufficientImageDataInFile");
982 Convert run-length encoded raster pixels.
984 pixel_info=AcquireVirtualMemory(image->rows,
985 MagickMax(bytes_per_line,image->columns+256UL)*sizeof(*pixels));
986 if (pixel_info == (MemoryInfo *) NULL)
987 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
988 pixels=(unsigned char *) GetVirtualMemoryBlob(pixel_info);
989 status=DecodeImage(image,bmp_info.compression,pixels,
990 image->columns*image->rows);
991 if (status == MagickFalse)
993 pixel_info=RelinquishVirtualMemory(pixel_info);
994 ThrowReaderException(CorruptImageError,
995 "UnableToRunlengthDecodeImage");
999 Convert BMP raster image to pixel packets.
1001 if (bmp_info.compression == BI_RGB)
1004 We should ignore the alpha value in BMP3 files but there have been
1005 reports about 32 bit files with alpha. We do a quick check to see if
1006 the alpha channel contains a value that is not zero (default value).
1007 If we find a non zero value we asume the program that wrote the file
1008 wants to use the alpha channel.
1010 if ((image->alpha_trait == UndefinedPixelTrait) && (bmp_info.size == 40) &&
1011 (bmp_info.bits_per_pixel == 32))
1013 bytes_per_line=4*(image->columns);
1014 for (y=(ssize_t) image->rows-1; y >= 0; y--)
1016 p=pixels+(image->rows-y-1)*bytes_per_line;
1017 for (x=0; x < (ssize_t) image->columns; x++)
1021 image->alpha_trait=BlendPixelTrait;
1029 bmp_info.alpha_mask=image->alpha_trait != UndefinedPixelTrait ?
1031 bmp_info.red_mask=0x00ff0000U;
1032 bmp_info.green_mask=0x0000ff00U;
1033 bmp_info.blue_mask=0x000000ffU;
1034 if (bmp_info.bits_per_pixel == 16)
1039 bmp_info.red_mask=0x00007c00U;
1040 bmp_info.green_mask=0x000003e0U;
1041 bmp_info.blue_mask=0x0000001fU;
1044 (void) memset(&shift,0,sizeof(shift));
1045 (void) memset(&quantum_bits,0,sizeof(quantum_bits));
1046 if ((bmp_info.bits_per_pixel == 16) || (bmp_info.bits_per_pixel == 32))
1048 register unsigned int
1052 Get shift and quantum bits info from bitfield masks.
1054 if (bmp_info.red_mask != 0)
1055 while (((bmp_info.red_mask << shift.red) & 0x80000000UL) == 0)
1058 if (shift.red >= 32U)
1061 if (bmp_info.green_mask != 0)
1062 while (((bmp_info.green_mask << shift.green) & 0x80000000UL) == 0)
1065 if (shift.green >= 32U)
1068 if (bmp_info.blue_mask != 0)
1069 while (((bmp_info.blue_mask << shift.blue) & 0x80000000UL) == 0)
1072 if (shift.blue >= 32U)
1075 if (bmp_info.alpha_mask != 0)
1076 while (((bmp_info.alpha_mask << shift.alpha) & 0x80000000UL) == 0)
1079 if (shift.alpha >= 32U)
1083 while (((bmp_info.red_mask << sample) & 0x80000000UL) != 0)
1089 quantum_bits.red=(MagickRealType) (sample-shift.red);
1091 while (((bmp_info.green_mask << sample) & 0x80000000UL) != 0)
1097 quantum_bits.green=(MagickRealType) (sample-shift.green);
1099 while (((bmp_info.blue_mask << sample) & 0x80000000UL) != 0)
1105 quantum_bits.blue=(MagickRealType) (sample-shift.blue);
1107 while (((bmp_info.alpha_mask << sample) & 0x80000000UL) != 0)
1113 quantum_bits.alpha=(MagickRealType) (sample-shift.alpha);
1115 switch (bmp_info.bits_per_pixel)
1120 Convert bitmap scanline.
1122 for (y=(ssize_t) image->rows-1; y >= 0; y--)
1124 p=pixels+(image->rows-y-1)*bytes_per_line;
1125 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
1126 if (q == (Quantum *) NULL)
1128 for (x=0; x < ((ssize_t) image->columns-7); x+=8)
1130 for (bit=0; bit < 8; bit++)
1132 index=(Quantum) (((*p) & (0x80 >> bit)) != 0 ? 0x01 : 0x00);
1133 SetPixelIndex(image,index,q);
1134 q+=GetPixelChannels(image);
1138 if ((image->columns % 8) != 0)
1140 for (bit=0; bit < (image->columns % 8); bit++)
1142 index=(Quantum) (((*p) & (0x80 >> bit)) != 0 ? 0x01 : 0x00);
1143 SetPixelIndex(image,index,q);
1144 q+=GetPixelChannels(image);
1148 if (SyncAuthenticPixels(image,exception) == MagickFalse)
1150 if (image->previous == (Image *) NULL)
1152 status=SetImageProgress(image,LoadImageTag,(MagickOffsetType)
1153 (image->rows-y),image->rows);
1154 if (status == MagickFalse)
1158 (void) SyncImage(image,exception);
1164 Convert PseudoColor scanline.
1166 for (y=(ssize_t) image->rows-1; y >= 0; y--)
1168 p=pixels+(image->rows-y-1)*bytes_per_line;
1169 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
1170 if (q == (Quantum *) NULL)
1172 for (x=0; x < ((ssize_t) image->columns-1); x+=2)
1174 ValidateColormapValue(image,(ssize_t) ((*p >> 4) & 0x0f),&index,
1176 SetPixelIndex(image,index,q);
1177 q+=GetPixelChannels(image);
1178 ValidateColormapValue(image,(ssize_t) (*p & 0x0f),&index,exception);
1179 SetPixelIndex(image,index,q);
1180 q+=GetPixelChannels(image);
1183 if ((image->columns % 2) != 0)
1185 ValidateColormapValue(image,(ssize_t) ((*p >> 4) & 0xf),&index,
1187 SetPixelIndex(image,index,q);
1188 q+=GetPixelChannels(image);
1192 if (x < (ssize_t) image->columns)
1194 if (SyncAuthenticPixels(image,exception) == MagickFalse)
1196 if (image->previous == (Image *) NULL)
1198 status=SetImageProgress(image,LoadImageTag,(MagickOffsetType)
1199 (image->rows-y),image->rows);
1200 if (status == MagickFalse)
1204 (void) SyncImage(image,exception);
1210 Convert PseudoColor scanline.
1212 if ((bmp_info.compression == BI_RLE8) ||
1213 (bmp_info.compression == BI_RLE4))
1214 bytes_per_line=image->columns;
1215 for (y=(ssize_t) image->rows-1; y >= 0; y--)
1217 p=pixels+(image->rows-y-1)*bytes_per_line;
1218 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
1219 if (q == (Quantum *) NULL)
1221 for (x=(ssize_t) image->columns; x != 0; --x)
1223 ValidateColormapValue(image,(ssize_t) *p++,&index,exception);
1224 SetPixelIndex(image,index,q);
1225 q+=GetPixelChannels(image);
1227 if (SyncAuthenticPixels(image,exception) == MagickFalse)
1229 offset=(MagickOffsetType) (image->rows-y-1);
1230 if (image->previous == (Image *) NULL)
1232 status=SetImageProgress(image,LoadImageTag,(MagickOffsetType)
1233 (image->rows-y),image->rows);
1234 if (status == MagickFalse)
1238 (void) SyncImage(image,exception);
1248 Convert bitfield encoded 16-bit PseudoColor scanline.
1250 if ((bmp_info.compression != BI_RGB) &&
1251 (bmp_info.compression != BI_BITFIELDS))
1253 pixel_info=RelinquishVirtualMemory(pixel_info);
1254 ThrowReaderException(CorruptImageError,
1255 "UnrecognizedImageCompression");
1257 bytes_per_line=2*(image->columns+image->columns % 2);
1258 image->storage_class=DirectClass;
1259 for (y=(ssize_t) image->rows-1; y >= 0; y--)
1261 p=pixels+(image->rows-y-1)*bytes_per_line;
1262 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
1263 if (q == (Quantum *) NULL)
1265 for (x=0; x < (ssize_t) image->columns; x++)
1267 pixel=(unsigned int) (*p++);
1269 red=((pixel & bmp_info.red_mask) << shift.red) >> 16;
1270 if (quantum_bits.red == 5)
1271 red|=((red & 0xe000) >> 5);
1272 if (quantum_bits.red <= 8)
1273 red|=((red & 0xff00) >> 8);
1274 green=((pixel & bmp_info.green_mask) << shift.green) >> 16;
1275 if (quantum_bits.green == 5)
1276 green|=((green & 0xe000) >> 5);
1277 if (quantum_bits.green == 6)
1278 green|=((green & 0xc000) >> 6);
1279 if (quantum_bits.green <= 8)
1280 green|=((green & 0xff00) >> 8);
1281 blue=((pixel & bmp_info.blue_mask) << shift.blue) >> 16;
1282 if (quantum_bits.blue == 5)
1283 blue|=((blue & 0xe000) >> 5);
1284 if (quantum_bits.blue <= 8)
1285 blue|=((blue & 0xff00) >> 8);
1286 SetPixelRed(image,ScaleShortToQuantum((unsigned short) red),q);
1287 SetPixelGreen(image,ScaleShortToQuantum((unsigned short) green),q);
1288 SetPixelBlue(image,ScaleShortToQuantum((unsigned short) blue),q);
1289 SetPixelAlpha(image,OpaqueAlpha,q);
1290 if (image->alpha_trait != UndefinedPixelTrait)
1292 alpha=((pixel & bmp_info.alpha_mask) << shift.alpha) >> 16;
1293 if (quantum_bits.alpha <= 8)
1294 alpha|=((alpha & 0xff00) >> 8);
1295 SetPixelAlpha(image,ScaleShortToQuantum(
1296 (unsigned short) alpha),q);
1298 q+=GetPixelChannels(image);
1300 if (SyncAuthenticPixels(image,exception) == MagickFalse)
1302 offset=(MagickOffsetType) (image->rows-y-1);
1303 if (image->previous == (Image *) NULL)
1305 status=SetImageProgress(image,LoadImageTag,(MagickOffsetType)
1306 (image->rows-y),image->rows);
1307 if (status == MagickFalse)
1316 Convert DirectColor scanline.
1318 bytes_per_line=4*((image->columns*24+31)/32);
1319 for (y=(ssize_t) image->rows-1; y >= 0; y--)
1321 p=pixels+(image->rows-y-1)*bytes_per_line;
1322 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
1323 if (q == (Quantum *) NULL)
1325 for (x=0; x < (ssize_t) image->columns; x++)
1327 SetPixelBlue(image,ScaleCharToQuantum(*p++),q);
1328 SetPixelGreen(image,ScaleCharToQuantum(*p++),q);
1329 SetPixelRed(image,ScaleCharToQuantum(*p++),q);
1330 SetPixelAlpha(image,OpaqueAlpha,q);
1331 q+=GetPixelChannels(image);
1333 if (SyncAuthenticPixels(image,exception) == MagickFalse)
1335 offset=(MagickOffsetType) (image->rows-y-1);
1336 if (image->previous == (Image *) NULL)
1338 status=SetImageProgress(image,LoadImageTag,(MagickOffsetType)
1339 (image->rows-y),image->rows);
1340 if (status == MagickFalse)
1349 Convert bitfield encoded DirectColor scanline.
1351 if ((bmp_info.compression != BI_RGB) &&
1352 (bmp_info.compression != BI_BITFIELDS))
1354 pixel_info=RelinquishVirtualMemory(pixel_info);
1355 ThrowReaderException(CorruptImageError,
1356 "UnrecognizedImageCompression");
1358 bytes_per_line=4*(image->columns);
1359 for (y=(ssize_t) image->rows-1; y >= 0; y--)
1365 p=pixels+(image->rows-y-1)*bytes_per_line;
1366 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
1367 if (q == (Quantum *) NULL)
1369 for (x=0; x < (ssize_t) image->columns; x++)
1371 pixel=(unsigned int) (*p++);
1372 pixel|=((unsigned int) *p++ << 8);
1373 pixel|=((unsigned int) *p++ << 16);
1374 pixel|=((unsigned int) *p++ << 24);
1375 red=((pixel & bmp_info.red_mask) << shift.red) >> 16;
1376 if (quantum_bits.red == 8)
1378 green=((pixel & bmp_info.green_mask) << shift.green) >> 16;
1379 if (quantum_bits.green == 8)
1380 green|=(green >> 8);
1381 blue=((pixel & bmp_info.blue_mask) << shift.blue) >> 16;
1382 if (quantum_bits.blue == 8)
1384 SetPixelRed(image,ScaleShortToQuantum((unsigned short) red),q);
1385 SetPixelGreen(image,ScaleShortToQuantum((unsigned short) green),q);
1386 SetPixelBlue(image,ScaleShortToQuantum((unsigned short) blue),q);
1387 SetPixelAlpha(image,OpaqueAlpha,q);
1388 if (image->alpha_trait != UndefinedPixelTrait)
1390 alpha=((pixel & bmp_info.alpha_mask) << shift.alpha) >> 16;
1391 if (quantum_bits.alpha == 8)
1392 alpha|=(alpha >> 8);
1393 SetPixelAlpha(image,ScaleShortToQuantum(
1394 (unsigned short) alpha),q);
1396 q+=GetPixelChannels(image);
1398 if (SyncAuthenticPixels(image,exception) == MagickFalse)
1400 offset=(MagickOffsetType) (image->rows-y-1);
1401 if (image->previous == (Image *) NULL)
1403 status=SetImageProgress(image,LoadImageTag,(MagickOffsetType)
1404 (image->rows-y),image->rows);
1405 if (status == MagickFalse)
1413 pixel_info=RelinquishVirtualMemory(pixel_info);
1414 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
1417 pixel_info=RelinquishVirtualMemory(pixel_info);
1420 if (EOFBlob(image) != MagickFalse)
1422 ThrowFileException(exception,CorruptImageError,"UnexpectedEndOfFile",
1426 if (bmp_info.height < 0)
1432 Correct image orientation.
1434 flipped_image=FlipImage(image,exception);
1435 if (flipped_image != (Image *) NULL)
1437 DuplicateBlob(flipped_image,image);
1438 ReplaceImageInList(&image, flipped_image);
1439 image=flipped_image;
1443 Proceed to next image.
1445 if (image_info->number_scenes != 0)
1446 if (image->scene >= (image_info->scene+image_info->number_scenes-1))
1449 if (bmp_info.ba_offset != 0)
1451 offset=SeekBlob(image,(MagickOffsetType) bmp_info.ba_offset,SEEK_SET);
1453 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
1455 count=ReadBlob(image,2,magick);
1456 if ((count == 2) && (IsBMP(magick,2) != MagickFalse))
1459 Acquire next image structure.
1461 AcquireNextImage(image_info,image,exception);
1462 if (GetNextImageInList(image) == (Image *) NULL)
1465 return((Image *) NULL);
1467 image=SyncNextImageInList(image);
1468 status=SetImageProgress(image,LoadImagesTag,TellBlob(image),
1469 GetBlobSize(image));
1470 if (status == MagickFalse)
1473 } while (IsBMP(magick,2) != MagickFalse);
1474 (void) CloseBlob(image);
1475 if (status == MagickFalse)
1476 return(DestroyImageList(image));
1477 return(GetFirstImageInList(image));
1481 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1485 % R e g i s t e r B M P I m a g e %
1489 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1491 % RegisterBMPImage() adds attributes for the BMP image format to
1492 % the list of supported formats. The attributes include the image format
1493 % tag, a method to read and/or write the format, whether the format
1494 % supports the saving of more than one frame to the same file or blob,
1495 % whether the format supports native in-memory I/O, and a brief
1496 % description of the format.
1498 % The format of the RegisterBMPImage method is:
1500 % size_t RegisterBMPImage(void)
1503 ModuleExport size_t RegisterBMPImage(void)
1508 entry=AcquireMagickInfo("BMP","BMP","Microsoft Windows bitmap image");
1509 entry->decoder=(DecodeImageHandler *) ReadBMPImage;
1510 entry->encoder=(EncodeImageHandler *) WriteBMPImage;
1511 entry->magick=(IsImageFormatHandler *) IsBMP;
1512 entry->flags^=CoderAdjoinFlag;
1513 entry->flags|=CoderDecoderSeekableStreamFlag;
1514 (void) RegisterMagickInfo(entry);
1515 entry=AcquireMagickInfo("BMP","BMP2","Microsoft Windows bitmap image (V2)");
1516 entry->decoder=(DecodeImageHandler *) ReadBMPImage;
1517 entry->encoder=(EncodeImageHandler *) WriteBMPImage;
1518 entry->magick=(IsImageFormatHandler *) IsBMP;
1519 entry->flags^=CoderAdjoinFlag;
1520 entry->flags|=CoderDecoderSeekableStreamFlag;
1521 (void) RegisterMagickInfo(entry);
1522 entry=AcquireMagickInfo("BMP","BMP3","Microsoft Windows bitmap image (V3)");
1523 entry->decoder=(DecodeImageHandler *) ReadBMPImage;
1524 entry->encoder=(EncodeImageHandler *) WriteBMPImage;
1525 entry->magick=(IsImageFormatHandler *) IsBMP;
1526 entry->flags^=CoderAdjoinFlag;
1527 entry->flags|=CoderDecoderSeekableStreamFlag;
1528 (void) RegisterMagickInfo(entry);
1529 return(MagickImageCoderSignature);
1533 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1537 % U n r e g i s t e r B M P I m a g e %
1541 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1543 % UnregisterBMPImage() removes format registrations made by the
1544 % BMP module from the list of supported formats.
1546 % The format of the UnregisterBMPImage method is:
1548 % UnregisterBMPImage(void)
1551 ModuleExport void UnregisterBMPImage(void)
1553 (void) UnregisterMagickInfo("BMP");
1554 (void) UnregisterMagickInfo("BMP2");
1555 (void) UnregisterMagickInfo("BMP3");
1559 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1563 % W r i t e B M P I m a g e %
1567 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1569 % WriteBMPImage() writes an image in Microsoft Windows bitmap encoded
1570 % image format, version 3 for Windows or (if the image has a matte channel)
1573 % The format of the WriteBMPImage method is:
1575 % MagickBooleanType WriteBMPImage(const ImageInfo *image_info,
1576 % Image *image,ExceptionInfo *exception)
1578 % A description of each parameter follows.
1580 % o image_info: the image info.
1582 % o image: The image.
1584 % o exception: return any errors or warnings in this structure.
1587 static MagickBooleanType WriteBMPImage(const ImageInfo *image_info,Image *image,
1588 ExceptionInfo *exception)
1612 register const Quantum
1619 register unsigned char
1635 Open output image file.
1637 assert(image_info != (const ImageInfo *) NULL);
1638 assert(image_info->signature == MagickCoreSignature);
1639 assert(image != (Image *) NULL);
1640 assert(image->signature == MagickCoreSignature);
1641 if (image->debug != MagickFalse)
1642 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1643 assert(exception != (ExceptionInfo *) NULL);
1644 assert(exception->signature == MagickCoreSignature);
1645 status=OpenBlob(image_info,image,WriteBinaryBlobMode,exception);
1646 if (status == MagickFalse)
1649 if (LocaleCompare(image_info->magick,"BMP2") == 0)
1652 if (LocaleCompare(image_info->magick,"BMP3") == 0)
1654 option=GetImageOption(image_info,"bmp:format");
1655 if (option != (char *) NULL)
1657 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1658 " Format=%s",option);
1659 if (LocaleCompare(option,"bmp2") == 0)
1661 if (LocaleCompare(option,"bmp3") == 0)
1663 if (LocaleCompare(option,"bmp4") == 0)
1667 imageListLength=GetImageListLength(image);
1671 Initialize BMP raster file header.
1673 if (IssRGBCompatibleColorspace(image->colorspace) == MagickFalse)
1674 (void) TransformImageColorspace(image,sRGBColorspace,exception);
1675 (void) memset(&bmp_info,0,sizeof(bmp_info));
1676 bmp_info.file_size=14+12;
1678 bmp_info.file_size+=28;
1679 bmp_info.offset_bits=bmp_info.file_size;
1680 bmp_info.compression=BI_RGB;
1681 bmp_info.red_mask=0x00ff0000U;
1682 bmp_info.green_mask=0x0000ff00U;
1683 bmp_info.blue_mask=0x000000ffU;
1684 bmp_info.alpha_mask=0xff000000U;
1685 bmp_subtype=UndefinedSubtype;
1686 if ((image->storage_class == PseudoClass) && (image->colors > 256))
1687 (void) SetImageStorageClass(image,DirectClass,exception);
1688 if (image->storage_class != DirectClass)
1691 Colormapped BMP raster.
1693 bmp_info.bits_per_pixel=8;
1694 if (image->colors <= 2)
1695 bmp_info.bits_per_pixel=1;
1697 if (image->colors <= 16)
1698 bmp_info.bits_per_pixel=4;
1700 if (image->colors <= 256)
1701 bmp_info.bits_per_pixel=8;
1702 if (image_info->compression == RLECompression)
1703 bmp_info.bits_per_pixel=8;
1704 bmp_info.number_colors=1U << bmp_info.bits_per_pixel;
1705 if (image->alpha_trait != UndefinedPixelTrait)
1706 (void) SetImageStorageClass(image,DirectClass,exception);
1708 if ((size_t) bmp_info.number_colors < image->colors)
1709 (void) SetImageStorageClass(image,DirectClass,exception);
1712 bmp_info.file_size+=3*(1UL << bmp_info.bits_per_pixel);
1713 bmp_info.offset_bits+=3*(1UL << bmp_info.bits_per_pixel);
1716 bmp_info.file_size+=(1UL << bmp_info.bits_per_pixel);
1717 bmp_info.offset_bits+=(1UL << bmp_info.bits_per_pixel);
1721 if (image->storage_class == DirectClass)
1724 Full color BMP raster.
1726 bmp_info.number_colors=0;
1727 option=GetImageOption(image_info,"bmp:subtype");
1728 if (option != (const char *) NULL)
1730 if (image->alpha_trait != UndefinedPixelTrait)
1732 if (LocaleNCompare(option,"ARGB4444",8) == 0)
1734 bmp_subtype=ARGB4444;
1735 bmp_info.red_mask=0x00000f00U;
1736 bmp_info.green_mask=0x000000f0U;
1737 bmp_info.blue_mask=0x0000000fU;
1738 bmp_info.alpha_mask=0x0000f000U;
1740 else if (LocaleNCompare(option,"ARGB1555",8) == 0)
1742 bmp_subtype=ARGB1555;
1743 bmp_info.red_mask=0x00007c00U;
1744 bmp_info.green_mask=0x000003e0U;
1745 bmp_info.blue_mask=0x0000001fU;
1746 bmp_info.alpha_mask=0x00008000U;
1751 if (LocaleNCompare(option,"RGB555",6) == 0)
1754 bmp_info.red_mask=0x00007c00U;
1755 bmp_info.green_mask=0x000003e0U;
1756 bmp_info.blue_mask=0x0000001fU;
1757 bmp_info.alpha_mask=0U;
1759 else if (LocaleNCompare(option,"RGB565",6) == 0)
1762 bmp_info.red_mask=0x0000f800U;
1763 bmp_info.green_mask=0x000007e0U;
1764 bmp_info.blue_mask=0x0000001fU;
1765 bmp_info.alpha_mask=0U;
1769 if (bmp_subtype != UndefinedSubtype)
1771 bmp_info.bits_per_pixel=16;
1772 bmp_info.compression=BI_BITFIELDS;
1776 bmp_info.bits_per_pixel=(unsigned short) ((type > 3) &&
1777 (image->alpha_trait != UndefinedPixelTrait) ? 32 : 24);
1778 bmp_info.compression=(unsigned int) ((type > 3) &&
1779 (image->alpha_trait != UndefinedPixelTrait) ? BI_BITFIELDS : BI_RGB);
1780 if ((type == 3) && (image->alpha_trait != UndefinedPixelTrait))
1782 option=GetImageOption(image_info,"bmp3:alpha");
1783 if (IsStringTrue(option))
1784 bmp_info.bits_per_pixel=32;
1788 bytes_per_line=4*((image->columns*bmp_info.bits_per_pixel+31)/32);
1789 bmp_info.ba_offset=0;
1790 profile=GetImageProfile(image,"icc");
1791 have_color_info=(image->rendering_intent != UndefinedIntent) ||
1792 (profile != (StringInfo *) NULL) || (image->gamma != 0.0) ? MagickTrue :
1797 if ((type == 3) || ((image->alpha_trait == UndefinedPixelTrait) &&
1798 (have_color_info == MagickFalse)))
1810 if ((image->rendering_intent != UndefinedIntent) ||
1811 (profile != (StringInfo *) NULL))
1816 bmp_info.file_size+=extra_size;
1817 bmp_info.offset_bits+=extra_size;
1819 if (((ssize_t) image->columns != (ssize_t) ((signed int) image->columns)) ||
1820 ((ssize_t) image->rows != (ssize_t) ((signed int) image->rows)))
1821 ThrowWriterException(ImageError,"WidthOrHeightExceedsLimit");
1822 bmp_info.width=(ssize_t) image->columns;
1823 bmp_info.height=(ssize_t) image->rows;
1825 bmp_info.image_size=(unsigned int) (bytes_per_line*image->rows);
1826 bmp_info.file_size+=bmp_info.image_size;
1827 bmp_info.x_pixels=75*39;
1828 bmp_info.y_pixels=75*39;
1829 switch (image->units)
1831 case UndefinedResolution:
1832 case PixelsPerInchResolution:
1834 bmp_info.x_pixels=(unsigned int) (100.0*image->resolution.x/2.54);
1835 bmp_info.y_pixels=(unsigned int) (100.0*image->resolution.y/2.54);
1838 case PixelsPerCentimeterResolution:
1840 bmp_info.x_pixels=(unsigned int) (100.0*image->resolution.x);
1841 bmp_info.y_pixels=(unsigned int) (100.0*image->resolution.y);
1845 bmp_info.colors_important=bmp_info.number_colors;
1847 Convert MIFF to BMP raster pixels.
1849 pixel_info=AcquireVirtualMemory(image->rows,
1850 MagickMax(bytes_per_line,image->columns+256UL)*sizeof(*pixels));
1851 if (pixel_info == (MemoryInfo *) NULL)
1852 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
1853 pixels=(unsigned char *) GetVirtualMemoryBlob(pixel_info);
1854 (void) memset(pixels,0,(size_t) bmp_info.image_size);
1855 switch (bmp_info.bits_per_pixel)
1864 Convert PseudoClass image to a BMP monochrome image.
1866 for (y=0; y < (ssize_t) image->rows; y++)
1871 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
1872 if (p == (const Quantum *) NULL)
1874 q=pixels+(image->rows-y-1)*bytes_per_line;
1877 for (x=0; x < (ssize_t) image->columns; x++)
1880 byte|=GetPixelIndex(image,p) != 0 ? 0x01 : 0x00;
1884 *q++=(unsigned char) byte;
1888 p+=GetPixelChannels(image);
1892 *q++=(unsigned char) (byte << (8-bit));
1895 offset=(ssize_t) (image->columns+7)/8;
1896 for (x=offset; x < (ssize_t) bytes_per_line; x++)
1898 if (image->previous == (Image *) NULL)
1900 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
1902 if (status == MagickFalse)
1918 Convert PseudoClass image to a BMP monochrome image.
1920 for (y=0; y < (ssize_t) image->rows; y++)
1922 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
1923 if (p == (const Quantum *) NULL)
1925 q=pixels+(image->rows-y-1)*bytes_per_line;
1928 for (x=0; x < (ssize_t) image->columns; x++)
1931 byte|=((unsigned int) GetPixelIndex(image,p) & 0x0f);
1935 *q++=(unsigned char) byte;
1939 p+=GetPixelChannels(image);
1943 *q++=(unsigned char) (byte << 4);
1946 offset=(ssize_t) (image->columns+1)/2;
1947 for (x=offset; x < (ssize_t) bytes_per_line; x++)
1949 if (image->previous == (Image *) NULL)
1951 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
1953 if (status == MagickFalse)
1962 Convert PseudoClass packet to BMP pixel.
1964 for (y=0; y < (ssize_t) image->rows; y++)
1966 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
1967 if (p == (const Quantum *) NULL)
1969 q=pixels+(image->rows-y-1)*bytes_per_line;
1970 for (x=0; x < (ssize_t) image->columns; x++)
1972 *q++=(unsigned char) GetPixelIndex(image,p);
1973 p+=GetPixelChannels(image);
1975 for ( ; x < (ssize_t) bytes_per_line; x++)
1977 if (image->previous == (Image *) NULL)
1979 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
1981 if (status == MagickFalse)
1990 Convert DirectClass packet to BMP BGR888.
1992 for (y=0; y < (ssize_t) image->rows; y++)
1994 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
1995 if (p == (const Quantum *) NULL)
1997 q=pixels+(image->rows-y-1)*bytes_per_line;
1998 for (x=0; x < (ssize_t) image->columns; x++)
2004 if (bmp_subtype == ARGB4444)
2006 pixel=(unsigned short) (ScaleQuantumToAny(
2007 GetPixelAlpha(image,p),15) << 12);
2008 pixel|=(unsigned short) (ScaleQuantumToAny(
2009 GetPixelRed(image,p),15) << 8);
2010 pixel|=(unsigned short) (ScaleQuantumToAny(
2011 GetPixelGreen(image,p),15) << 4);
2012 pixel|=(unsigned short) (ScaleQuantumToAny(
2013 GetPixelBlue(image,p),15));
2015 else if (bmp_subtype == RGB565)
2017 pixel=(unsigned short) (ScaleQuantumToAny(
2018 GetPixelRed(image,p),31) << 11);
2019 pixel|=(unsigned short) (ScaleQuantumToAny(
2020 GetPixelGreen(image,p),63) << 5);
2021 pixel|=(unsigned short) (ScaleQuantumToAny(
2022 GetPixelBlue(image,p),31));
2026 if (bmp_subtype == ARGB1555)
2027 pixel=(unsigned short) (ScaleQuantumToAny(
2028 GetPixelAlpha(image,p),1) << 15);
2029 pixel|=(unsigned short) (ScaleQuantumToAny(
2030 GetPixelRed(image,p),31) << 10);
2031 pixel|=(unsigned short) (ScaleQuantumToAny(
2032 GetPixelGreen(image,p),31) << 5);
2033 pixel|=(unsigned short) (ScaleQuantumToAny(
2034 GetPixelBlue(image,p),31));
2036 *((unsigned short *) q)=pixel;
2038 p+=GetPixelChannels(image);
2040 for (x=2L*(ssize_t) image->columns; x < (ssize_t) bytes_per_line; x++)
2042 if (image->previous == (Image *) NULL)
2044 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
2046 if (status == MagickFalse)
2055 Convert DirectClass packet to BMP BGR888.
2057 for (y=0; y < (ssize_t) image->rows; y++)
2059 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
2060 if (p == (const Quantum *) NULL)
2062 q=pixels+(image->rows-y-1)*bytes_per_line;
2063 for (x=0; x < (ssize_t) image->columns; x++)
2065 *q++=ScaleQuantumToChar(GetPixelBlue(image,p));
2066 *q++=ScaleQuantumToChar(GetPixelGreen(image,p));
2067 *q++=ScaleQuantumToChar(GetPixelRed(image,p));
2068 p+=GetPixelChannels(image);
2070 for (x=3L*(ssize_t) image->columns; x < (ssize_t) bytes_per_line; x++)
2072 if (image->previous == (Image *) NULL)
2074 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
2076 if (status == MagickFalse)
2085 Convert DirectClass packet to ARGB8888 pixel.
2087 for (y=0; y < (ssize_t) image->rows; y++)
2089 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
2090 if (p == (const Quantum *) NULL)
2092 q=pixels+(image->rows-y-1)*bytes_per_line;
2093 for (x=0; x < (ssize_t) image->columns; x++)
2095 *q++=ScaleQuantumToChar(GetPixelBlue(image,p));
2096 *q++=ScaleQuantumToChar(GetPixelGreen(image,p));
2097 *q++=ScaleQuantumToChar(GetPixelRed(image,p));
2098 *q++=ScaleQuantumToChar(GetPixelAlpha(image,p));
2099 p+=GetPixelChannels(image);
2101 if (image->previous == (Image *) NULL)
2103 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
2105 if (status == MagickFalse)
2112 if ((type > 2) && (bmp_info.bits_per_pixel == 8))
2113 if (image_info->compression != NoCompression)
2119 Convert run-length encoded raster pixels.
2121 rle_info=AcquireVirtualMemory((size_t) (2*(bytes_per_line+2)+2),
2122 (image->rows+2)*sizeof(*pixels));
2123 if (rle_info == (MemoryInfo *) NULL)
2125 pixel_info=RelinquishVirtualMemory(pixel_info);
2126 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
2128 bmp_data=(unsigned char *) GetVirtualMemoryBlob(rle_info);
2129 bmp_info.file_size-=bmp_info.image_size;
2130 bmp_info.image_size=(unsigned int) EncodeImage(image,bytes_per_line,
2132 bmp_info.file_size+=bmp_info.image_size;
2133 pixel_info=RelinquishVirtualMemory(pixel_info);
2134 pixel_info=rle_info;
2136 bmp_info.compression=BI_RLE8;
2139 Write BMP for Windows, all versions, 14-byte header.
2141 if (image->debug != MagickFalse)
2143 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
2144 " Writing BMP version %.20g datastream",(double) type);
2145 if (image->storage_class == DirectClass)
2146 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
2147 " Storage class=DirectClass");
2149 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
2150 " Storage class=PseudoClass");
2151 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
2152 " Image depth=%.20g",(double) image->depth);
2153 if (image->alpha_trait != UndefinedPixelTrait)
2154 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
2157 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
2158 " Matte=MagickFalse");
2159 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
2160 " BMP bits_per_pixel=%.20g",(double) bmp_info.bits_per_pixel);
2161 switch ((int) bmp_info.compression)
2165 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
2166 " Compression=BI_RGB");
2171 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
2172 " Compression=BI_RLE8");
2177 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
2178 " Compression=BI_BITFIELDS");
2183 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
2184 " Compression=UNKNOWN (%u)",bmp_info.compression);
2188 if (bmp_info.number_colors == 0)
2189 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
2190 " Number_colors=unspecified");
2192 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
2193 " Number_colors=%u",bmp_info.number_colors);
2195 (void) WriteBlob(image,2,(unsigned char *) "BM");
2196 (void) WriteBlobLSBLong(image,bmp_info.file_size);
2197 (void) WriteBlobLSBLong(image,bmp_info.ba_offset); /* always 0 */
2198 (void) WriteBlobLSBLong(image,bmp_info.offset_bits);
2202 Write 12-byte version 2 bitmap header.
2204 (void) WriteBlobLSBLong(image,bmp_info.size);
2205 (void) WriteBlobLSBSignedShort(image,(signed short) bmp_info.width);
2206 (void) WriteBlobLSBSignedShort(image,(signed short) bmp_info.height);
2207 (void) WriteBlobLSBShort(image,bmp_info.planes);
2208 (void) WriteBlobLSBShort(image,bmp_info.bits_per_pixel);
2213 Write 40-byte version 3+ bitmap header.
2215 (void) WriteBlobLSBLong(image,bmp_info.size);
2216 (void) WriteBlobLSBSignedLong(image,(signed int) bmp_info.width);
2217 (void) WriteBlobLSBSignedLong(image,(signed int) bmp_info.height);
2218 (void) WriteBlobLSBShort(image,bmp_info.planes);
2219 (void) WriteBlobLSBShort(image,bmp_info.bits_per_pixel);
2220 (void) WriteBlobLSBLong(image,bmp_info.compression);
2221 (void) WriteBlobLSBLong(image,bmp_info.image_size);
2222 (void) WriteBlobLSBLong(image,bmp_info.x_pixels);
2223 (void) WriteBlobLSBLong(image,bmp_info.y_pixels);
2224 (void) WriteBlobLSBLong(image,bmp_info.number_colors);
2225 (void) WriteBlobLSBLong(image,bmp_info.colors_important);
2227 if ((type > 3) && ((image->alpha_trait != UndefinedPixelTrait) ||
2228 (have_color_info != MagickFalse)))
2231 Write the rest of the 108-byte BMP Version 4 header.
2233 (void) WriteBlobLSBLong(image,bmp_info.red_mask);
2234 (void) WriteBlobLSBLong(image,bmp_info.green_mask);
2235 (void) WriteBlobLSBLong(image,bmp_info.blue_mask);
2236 (void) WriteBlobLSBLong(image,bmp_info.alpha_mask);
2237 (void) WriteBlobLSBLong(image,0x73524742U); /* sRGB */
2238 (void) WriteBlobLSBLong(image,(unsigned int)
2239 (image->chromaticity.red_primary.x*0x40000000));
2240 (void) WriteBlobLSBLong(image,(unsigned int)
2241 (image->chromaticity.red_primary.y*0x40000000));
2242 (void) WriteBlobLSBLong(image,(unsigned int)
2243 ((1.000f-(image->chromaticity.red_primary.x+
2244 image->chromaticity.red_primary.y))*0x40000000));
2245 (void) WriteBlobLSBLong(image,(unsigned int)
2246 (image->chromaticity.green_primary.x*0x40000000));
2247 (void) WriteBlobLSBLong(image,(unsigned int)
2248 (image->chromaticity.green_primary.y*0x40000000));
2249 (void) WriteBlobLSBLong(image,(unsigned int)
2250 ((1.000f-(image->chromaticity.green_primary.x+
2251 image->chromaticity.green_primary.y))*0x40000000));
2252 (void) WriteBlobLSBLong(image,(unsigned int)
2253 (image->chromaticity.blue_primary.x*0x40000000));
2254 (void) WriteBlobLSBLong(image,(unsigned int)
2255 (image->chromaticity.blue_primary.y*0x40000000));
2256 (void) WriteBlobLSBLong(image,(unsigned int)
2257 ((1.000f-(image->chromaticity.blue_primary.x+
2258 image->chromaticity.blue_primary.y))*0x40000000));
2259 (void) WriteBlobLSBLong(image,(unsigned int)
2260 (bmp_info.gamma_scale.x*0x10000));
2261 (void) WriteBlobLSBLong(image,(unsigned int)
2262 (bmp_info.gamma_scale.y*0x10000));
2263 (void) WriteBlobLSBLong(image,(unsigned int)
2264 (bmp_info.gamma_scale.z*0x10000));
2265 if ((image->rendering_intent != UndefinedIntent) ||
2266 (profile != (StringInfo *) NULL))
2271 switch ((int) image->rendering_intent)
2273 case SaturationIntent:
2275 intent=LCS_GM_BUSINESS;
2278 case RelativeIntent:
2280 intent=LCS_GM_GRAPHICS;
2283 case PerceptualIntent:
2285 intent=LCS_GM_IMAGES;
2288 case AbsoluteIntent:
2290 intent=LCS_GM_ABS_COLORIMETRIC;
2299 (void) WriteBlobLSBLong(image,(unsigned int) intent);
2300 (void) WriteBlobLSBLong(image,0x00); /* dummy profile data */
2301 (void) WriteBlobLSBLong(image,0x00); /* dummy profile length */
2302 (void) WriteBlobLSBLong(image,0x00); /* reserved */
2305 if (image->storage_class == PseudoClass)
2311 Dump colormap to file.
2313 if (image->debug != MagickFalse)
2314 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
2315 " Colormap: %.20g entries",(double) image->colors);
2316 bmp_colormap=(unsigned char *) AcquireQuantumMemory((size_t) (1UL <<
2317 bmp_info.bits_per_pixel),4*sizeof(*bmp_colormap));
2318 if (bmp_colormap == (unsigned char *) NULL)
2319 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
2321 for (i=0; i < (ssize_t) MagickMin((ssize_t) image->colors,(ssize_t) bmp_info.number_colors); i++)
2323 *q++=ScaleQuantumToChar(ClampToQuantum(image->colormap[i].blue));
2324 *q++=ScaleQuantumToChar(ClampToQuantum(image->colormap[i].green));
2325 *q++=ScaleQuantumToChar(ClampToQuantum(image->colormap[i].red));
2327 *q++=(unsigned char) 0x0;
2329 for ( ; i < (ssize_t) (1UL << bmp_info.bits_per_pixel); i++)
2331 *q++=(unsigned char) 0x00;
2332 *q++=(unsigned char) 0x00;
2333 *q++=(unsigned char) 0x00;
2335 *q++=(unsigned char) 0x00;
2338 (void) WriteBlob(image,(size_t) (3*(1L << bmp_info.bits_per_pixel)),
2341 (void) WriteBlob(image,(size_t) (4*(1L << bmp_info.bits_per_pixel)),
2343 bmp_colormap=(unsigned char *) RelinquishMagickMemory(bmp_colormap);
2345 if (image->debug != MagickFalse)
2346 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
2347 " Pixels: %u bytes",bmp_info.image_size);
2348 (void) WriteBlob(image,(size_t) bmp_info.image_size,pixels);
2349 pixel_info=RelinquishVirtualMemory(pixel_info);
2350 if (GetNextImageInList(image) == (Image *) NULL)
2352 image=SyncNextImageInList(image);
2353 status=SetImageProgress(image,SaveImagesTag,scene++,imageListLength);
2354 if (status == MagickFalse)
2356 } while (image_info->adjoin != MagickFalse);
2357 (void) CloseBlob(image);