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,
185 % const size_t compression,unsigned char *pixels,
186 % const size_t number_pixels)
188 % A description of each parameter follows:
190 % o image: the address of a structure of type Image.
192 % o compression: Zero means uncompressed. A value of 1 means the
193 % compressed pixels are runlength encoded for a 256-color bitmap.
194 % A value of 2 means a 16-color bitmap. A value of 3 means bitfields
197 % o pixels: The address of a byte (8 bits) array of pixel data created by
198 % the decoding process.
200 % o number_pixels: The number of pixels.
203 static MagickBooleanType DecodeImage(Image *image,const size_t compression,
204 unsigned char *pixels,const size_t number_pixels)
214 register unsigned char
221 assert(image != (Image *) NULL);
222 assert(image->signature == MagickCoreSignature);
223 if (image->debug != MagickFalse)
224 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
225 assert(pixels != (unsigned char *) NULL);
226 (void) ResetMagickMemory(pixels,0,number_pixels*sizeof(*pixels));
230 q=pixels+number_pixels;
231 for (y=0; y < (ssize_t) image->rows; )
236 if ((p < pixels) || (p > q))
238 count=ReadBlobByte(image);
246 count=(int) MagickMin((ssize_t) count,(ssize_t) (q-p));
247 byte=ReadBlobByte(image);
250 if (compression == BI_RLE8)
252 for (i=0; i < (ssize_t) count; i++)
253 *p++=(unsigned char) byte;
257 for (i=0; i < (ssize_t) count; i++)
259 ((i & 0x01) != 0 ? (byte & 0x0f) : ((byte >> 4) & 0x0f));
268 count=ReadBlobByte(image);
282 p=pixels+y*image->columns;
290 x+=ReadBlobByte(image);
291 y+=ReadBlobByte(image);
292 p=pixels+y*image->columns+x;
300 count=(int) MagickMin((ssize_t) count,(ssize_t) (q-p));
301 if (compression == BI_RLE8)
302 for (i=0; i < (ssize_t) count; i++)
304 byte=ReadBlobByte(image);
307 *p++=(unsigned char) byte;
310 for (i=0; i < (ssize_t) count; i++)
314 byte=ReadBlobByte(image);
319 ((i & 0x01) != 0 ? (byte & 0x0f) : ((byte >> 4) & 0x0f));
325 if (compression == BI_RLE8)
327 if ((count & 0x01) != 0)
328 if (ReadBlobByte(image) == EOF)
332 if (((count & 0x03) == 1) || ((count & 0x03) == 2))
333 if (ReadBlobByte(image) == EOF)
339 status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y,
341 if (status == MagickFalse)
344 (void) ReadBlobByte(image); /* end of line */
345 (void) ReadBlobByte(image);
346 return(y < (ssize_t) image->rows ? MagickFalse : MagickTrue);
350 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
354 % E n c o d e I m a g e %
358 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
360 % EncodeImage compresses pixels using a runlength encoded format.
362 % The format of the EncodeImage method is:
364 % static MagickBooleanType EncodeImage(Image *image,
365 % const size_t bytes_per_line,const unsigned char *pixels,
366 % unsigned char *compressed_pixels)
368 % A description of each parameter follows:
370 % o image: The image.
372 % o bytes_per_line: the number of bytes in a scanline of compressed pixels
374 % o pixels: The address of a byte (8 bits) array of pixel data created by
375 % the compression process.
377 % o compressed_pixels: The address of a byte (8 bits) array of compressed
381 static size_t EncodeImage(Image *image,const size_t bytes_per_line,
382 const unsigned char *pixels,unsigned char *compressed_pixels)
387 register const unsigned char
394 register unsigned char
401 Runlength encode pixels.
403 assert(image != (Image *) NULL);
404 assert(image->signature == MagickCoreSignature);
405 if (image->debug != MagickFalse)
406 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
407 assert(pixels != (const unsigned char *) NULL);
408 assert(compressed_pixels != (unsigned char *) NULL);
412 for (y=0; y < (ssize_t) image->rows; y++)
414 for (x=0; x < (ssize_t) bytes_per_line; x+=i)
419 for (i=1; ((x+i) < (ssize_t) bytes_per_line); i++)
420 if ((i == 255) || (*(p+i) != *p))
422 *q++=(unsigned char) i;
429 *q++=(unsigned char) 0x00;
430 *q++=(unsigned char) 0x00;
431 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
433 if (status == MagickFalse)
439 *q++=(unsigned char) 0x00;
440 *q++=(unsigned char) 0x01;
441 return((size_t) (q-compressed_pixels));
445 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
453 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
455 % IsBMP() returns MagickTrue if the image format type, identified by the
456 % magick string, is BMP.
458 % The format of the IsBMP method is:
460 % MagickBooleanType IsBMP(const unsigned char *magick,const size_t length)
462 % A description of each parameter follows:
464 % o magick: compare image format pattern against these bytes.
466 % o length: Specifies the length of the magick string.
469 static MagickBooleanType IsBMP(const unsigned char *magick,const size_t length)
473 if ((LocaleNCompare((char *) magick,"BA",2) == 0) ||
474 (LocaleNCompare((char *) magick,"BM",2) == 0) ||
475 (LocaleNCompare((char *) magick,"IC",2) == 0) ||
476 (LocaleNCompare((char *) magick,"PI",2) == 0) ||
477 (LocaleNCompare((char *) magick,"CI",2) == 0) ||
478 (LocaleNCompare((char *) magick,"CP",2) == 0))
484 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
488 % R e a d B M P I m a g e %
492 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
494 % ReadBMPImage() reads a Microsoft Windows bitmap image file, Version
495 % 2, 3 (for Windows or NT), or 4, and returns it. It allocates the memory
496 % necessary for the new Image structure and returns a pointer to the new
499 % The format of the ReadBMPImage method is:
501 % image=ReadBMPImage(image_info)
503 % A description of each parameter follows:
505 % o image_info: the image info.
507 % o exception: return any errors or warnings in this structure.
511 static Image *ReadBMPImage(const ImageInfo *image_info,ExceptionInfo *exception)
539 register unsigned char
564 assert(image_info != (const ImageInfo *) NULL);
565 assert(image_info->signature == MagickCoreSignature);
566 if (image_info->debug != MagickFalse)
567 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
568 image_info->filename);
569 assert(exception != (ExceptionInfo *) NULL);
570 assert(exception->signature == MagickCoreSignature);
571 image=AcquireImage(image_info,exception);
572 status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception);
573 if (status == MagickFalse)
575 image=DestroyImageList(image);
576 return((Image *) NULL);
579 Determine if this a BMP file.
581 (void) ResetMagickMemory(&bmp_info,0,sizeof(bmp_info));
582 bmp_info.ba_offset=0;
585 count=ReadBlob(image,2,magick);
587 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
601 Verify BMP identifier.
603 if (bmp_info.ba_offset == 0)
604 start_position=TellBlob(image)-2;
605 bmp_info.ba_offset=0;
606 while (LocaleNCompare((char *) magick,"BA",2) == 0)
608 bmp_info.file_size=ReadBlobLSBLong(image);
609 bmp_info.ba_offset=ReadBlobLSBLong(image);
610 bmp_info.offset_bits=ReadBlobLSBLong(image);
611 count=ReadBlob(image,2,magick);
615 if (image->debug != MagickFalse)
616 (void) LogMagickEvent(CoderEvent,GetMagickModule()," Magick: %c%c",
617 magick[0],magick[1]);
618 if ((count != 2) || ((LocaleNCompare((char *) magick,"BM",2) != 0) &&
619 (LocaleNCompare((char *) magick,"CI",2) != 0)))
620 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
621 bmp_info.file_size=ReadBlobLSBLong(image);
622 (void) ReadBlobLSBLong(image);
623 bmp_info.offset_bits=ReadBlobLSBLong(image);
624 bmp_info.size=ReadBlobLSBLong(image);
625 if (image->debug != MagickFalse)
626 (void) LogMagickEvent(CoderEvent,GetMagickModule()," BMP size: %lu",
628 if (bmp_info.size == 12)
633 (void) CopyMagickString(image->magick,"BMP2",MagickPathExtent);
634 bmp_info.width=(ssize_t) ((short) ReadBlobLSBShort(image));
635 bmp_info.height=(ssize_t) ((short) ReadBlobLSBShort(image));
636 bmp_info.planes=ReadBlobLSBShort(image);
637 bmp_info.bits_per_pixel=ReadBlobLSBShort(image);
640 bmp_info.number_colors=0;
641 bmp_info.compression=BI_RGB;
642 bmp_info.image_size=0;
643 bmp_info.alpha_mask=0;
644 if (image->debug != MagickFalse)
646 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
647 " Format: OS/2 Bitmap");
648 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
649 " Geometry: %.20gx%.20g",(double) bmp_info.width,(double)
656 Microsoft Windows BMP image file.
658 if (bmp_info.size < 40)
659 ThrowReaderException(CorruptImageError,"NonOS2HeaderSizeError");
660 bmp_info.width=(ssize_t) ReadBlobLSBSignedLong(image);
661 bmp_info.height=(ssize_t) ReadBlobLSBSignedLong(image);
662 bmp_info.planes=ReadBlobLSBShort(image);
663 bmp_info.bits_per_pixel=ReadBlobLSBShort(image);
664 bmp_info.compression=ReadBlobLSBLong(image);
665 bmp_info.image_size=ReadBlobLSBLong(image);
666 bmp_info.x_pixels=ReadBlobLSBLong(image);
667 bmp_info.y_pixels=ReadBlobLSBLong(image);
668 bmp_info.number_colors=ReadBlobLSBLong(image);
669 bmp_info.colors_important=ReadBlobLSBLong(image);
672 if (image->debug != MagickFalse)
674 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
675 " Format: MS Windows bitmap");
676 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
677 " Geometry: %.20gx%.20g",(double) bmp_info.width,(double)
679 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
680 " Bits per pixel: %.20g",(double) bmp_info.bits_per_pixel);
681 switch ((int) bmp_info.compression)
685 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
686 " Compression: BI_RGB");
691 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
692 " Compression: BI_RLE4");
697 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
698 " Compression: BI_RLE8");
703 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
704 " Compression: BI_BITFIELDS");
709 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
710 " Compression: BI_PNG");
715 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
716 " Compression: BI_JPEG");
721 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
722 " Compression: UNKNOWN (%lu)",bmp_info.compression);
725 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
726 " Number of colors: %lu",bmp_info.number_colors);
728 bmp_info.red_mask=ReadBlobLSBLong(image);
729 bmp_info.green_mask=ReadBlobLSBLong(image);
730 bmp_info.blue_mask=ReadBlobLSBLong(image);
731 if (bmp_info.size > 40)
737 Read color management information.
739 bmp_info.alpha_mask=ReadBlobLSBLong(image);
740 bmp_info.colorspace=ReadBlobLSBSignedLong(image);
742 Decode 2^30 fixed point formatted CIE primaries.
744 # define BMP_DENOM ((double) 0x40000000)
745 bmp_info.red_primary.x=(double) ReadBlobLSBLong(image)/BMP_DENOM;
746 bmp_info.red_primary.y=(double) ReadBlobLSBLong(image)/BMP_DENOM;
747 bmp_info.red_primary.z=(double) ReadBlobLSBLong(image)/BMP_DENOM;
748 bmp_info.green_primary.x=(double) ReadBlobLSBLong(image)/BMP_DENOM;
749 bmp_info.green_primary.y=(double) ReadBlobLSBLong(image)/BMP_DENOM;
750 bmp_info.green_primary.z=(double) ReadBlobLSBLong(image)/BMP_DENOM;
751 bmp_info.blue_primary.x=(double) ReadBlobLSBLong(image)/BMP_DENOM;
752 bmp_info.blue_primary.y=(double) ReadBlobLSBLong(image)/BMP_DENOM;
753 bmp_info.blue_primary.z=(double) ReadBlobLSBLong(image)/BMP_DENOM;
755 gamma=bmp_info.red_primary.x+bmp_info.red_primary.y+
756 bmp_info.red_primary.z;
757 gamma=PerceptibleReciprocal(gamma);
758 bmp_info.red_primary.x*=gamma;
759 bmp_info.red_primary.y*=gamma;
760 image->chromaticity.red_primary.x=bmp_info.red_primary.x;
761 image->chromaticity.red_primary.y=bmp_info.red_primary.y;
763 gamma=bmp_info.green_primary.x+bmp_info.green_primary.y+
764 bmp_info.green_primary.z;
765 gamma=PerceptibleReciprocal(gamma);
766 bmp_info.green_primary.x*=gamma;
767 bmp_info.green_primary.y*=gamma;
768 image->chromaticity.green_primary.x=bmp_info.green_primary.x;
769 image->chromaticity.green_primary.y=bmp_info.green_primary.y;
771 gamma=bmp_info.blue_primary.x+bmp_info.blue_primary.y+
772 bmp_info.blue_primary.z;
773 gamma=PerceptibleReciprocal(gamma);
774 bmp_info.blue_primary.x*=gamma;
775 bmp_info.blue_primary.y*=gamma;
776 image->chromaticity.blue_primary.x=bmp_info.blue_primary.x;
777 image->chromaticity.blue_primary.y=bmp_info.blue_primary.y;
780 Decode 16^16 fixed point formatted gamma_scales.
782 bmp_info.gamma_scale.x=(double) ReadBlobLSBLong(image)/0x10000;
783 bmp_info.gamma_scale.y=(double) ReadBlobLSBLong(image)/0x10000;
784 bmp_info.gamma_scale.z=(double) ReadBlobLSBLong(image)/0x10000;
786 Compute a single gamma from the BMP 3-channel gamma.
788 image->gamma=(bmp_info.gamma_scale.x+bmp_info.gamma_scale.y+
789 bmp_info.gamma_scale.z)/3.0;
792 (void) CopyMagickString(image->magick,"BMP3",MagickPathExtent);
794 if (bmp_info.size > 108)
800 Read BMP Version 5 color management information.
802 intent=ReadBlobLSBLong(image);
803 switch ((int) intent)
805 case LCS_GM_BUSINESS:
807 image->rendering_intent=SaturationIntent;
810 case LCS_GM_GRAPHICS:
812 image->rendering_intent=RelativeIntent;
817 image->rendering_intent=PerceptualIntent;
820 case LCS_GM_ABS_COLORIMETRIC:
822 image->rendering_intent=AbsoluteIntent;
826 profile_data=ReadBlobLSBLong(image);
827 profile_size=ReadBlobLSBLong(image);
830 (void) ReadBlobLSBLong(image); /* Reserved byte */
833 if ((MagickSizeType) bmp_info.file_size > GetBlobSize(image))
834 (void) ThrowMagickException(exception,GetMagickModule(),CorruptImageError,
835 "LengthAndFilesizeDoNotMatch","`%s'",image->filename);
837 if ((MagickSizeType) bmp_info.file_size < GetBlobSize(image))
838 (void) ThrowMagickException(exception,GetMagickModule(),
839 CorruptImageWarning,"LengthAndFilesizeDoNotMatch","`%s'",
841 if (bmp_info.width <= 0)
842 ThrowReaderException(CorruptImageError,"NegativeOrZeroImageSize");
843 if (bmp_info.height == 0)
844 ThrowReaderException(CorruptImageError,"NegativeOrZeroImageSize");
845 if (bmp_info.planes != 1)
846 ThrowReaderException(CorruptImageError,"StaticPlanesValueNotEqualToOne");
847 if ((bmp_info.bits_per_pixel != 1) && (bmp_info.bits_per_pixel != 4) &&
848 (bmp_info.bits_per_pixel != 8) && (bmp_info.bits_per_pixel != 16) &&
849 (bmp_info.bits_per_pixel != 24) && (bmp_info.bits_per_pixel != 32))
850 ThrowReaderException(CorruptImageError,"UnrecognizedBitsPerPixel");
851 if (bmp_info.bits_per_pixel < 16 &&
852 bmp_info.number_colors > (1U << bmp_info.bits_per_pixel))
853 ThrowReaderException(CorruptImageError,"UnrecognizedNumberOfColors");
854 if ((bmp_info.compression == 1) && (bmp_info.bits_per_pixel != 8))
855 ThrowReaderException(CorruptImageError,"UnrecognizedBitsPerPixel");
856 if ((bmp_info.compression == 2) && (bmp_info.bits_per_pixel != 4))
857 ThrowReaderException(CorruptImageError,"UnrecognizedBitsPerPixel");
858 if ((bmp_info.compression == 3) && (bmp_info.bits_per_pixel < 16))
859 ThrowReaderException(CorruptImageError,"UnrecognizedBitsPerPixel");
860 switch (bmp_info.compression)
863 image->compression=NoCompression;
867 image->compression=RLECompression;
872 ThrowReaderException(CoderError,"JPEGCompressNotSupported");
874 ThrowReaderException(CoderError,"PNGCompressNotSupported");
876 ThrowReaderException(CorruptImageError,"UnrecognizedImageCompression");
878 image->columns=(size_t) MagickAbsoluteValue(bmp_info.width);
879 image->rows=(size_t) MagickAbsoluteValue(bmp_info.height);
880 image->depth=bmp_info.bits_per_pixel <= 8 ? bmp_info.bits_per_pixel : 8;
881 image->alpha_trait=((bmp_info.alpha_mask != 0) &&
882 (bmp_info.compression == BI_BITFIELDS)) ? BlendPixelTrait :
884 if (bmp_info.bits_per_pixel < 16)
889 image->storage_class=PseudoClass;
890 image->colors=bmp_info.number_colors;
892 if (image->colors == 0)
893 image->colors=one << bmp_info.bits_per_pixel;
895 image->resolution.x=(double) bmp_info.x_pixels/100.0;
896 image->resolution.y=(double) bmp_info.y_pixels/100.0;
897 image->units=PixelsPerCentimeterResolution;
898 if ((image_info->ping != MagickFalse) && (image_info->number_scenes != 0))
899 if (image->scene >= (image_info->scene+image_info->number_scenes-1))
901 status=SetImageExtent(image,image->columns,image->rows,exception);
902 if (status == MagickFalse)
903 return(DestroyImageList(image));
904 if (image->storage_class == PseudoClass)
913 Read BMP raster colormap.
915 if (image->debug != MagickFalse)
916 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
917 " Reading colormap of %.20g colors",(double) image->colors);
918 if (AcquireImageColormap(image,image->colors,exception) == MagickFalse)
919 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
920 bmp_colormap=(unsigned char *) AcquireQuantumMemory((size_t)
921 image->colors,4*sizeof(*bmp_colormap));
922 if (bmp_colormap == (unsigned char *) NULL)
923 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
924 if ((bmp_info.size == 12) || (bmp_info.size == 64))
928 offset=SeekBlob(image,start_position+14+bmp_info.size,SEEK_SET);
931 bmp_colormap=(unsigned char *) RelinquishMagickMemory(bmp_colormap);
932 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
934 count=ReadBlob(image,packet_size*image->colors,bmp_colormap);
935 if (count != (ssize_t) (packet_size*image->colors))
937 bmp_colormap=(unsigned char *) RelinquishMagickMemory(bmp_colormap);
938 ThrowReaderException(CorruptImageError,
939 "InsufficientImageDataInFile");
942 for (i=0; i < (ssize_t) image->colors; i++)
944 image->colormap[i].blue=(MagickRealType) ScaleCharToQuantum(*p++);
945 image->colormap[i].green=(MagickRealType) ScaleCharToQuantum(*p++);
946 image->colormap[i].red=(MagickRealType) ScaleCharToQuantum(*p++);
947 if (packet_size == 4)
950 bmp_colormap=(unsigned char *) RelinquishMagickMemory(bmp_colormap);
955 if (bmp_info.offset_bits == offset_bits)
956 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
957 offset_bits=bmp_info.offset_bits;
958 offset=SeekBlob(image,start_position+bmp_info.offset_bits,SEEK_SET);
960 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
961 if (bmp_info.compression == BI_RLE4)
962 bmp_info.bits_per_pixel<<=1;
963 bytes_per_line=4*((image->columns*bmp_info.bits_per_pixel+31)/32);
964 length=(size_t) bytes_per_line*image->rows;
965 if ((bmp_info.compression == BI_RGB) ||
966 (bmp_info.compression == BI_BITFIELDS))
968 if (length > GetBlobSize(image))
969 ThrowReaderException(CorruptImageError,"InsufficientImageDataInFile");
970 pixel_info=AcquireVirtualMemory((size_t) image->rows,
971 MagickMax(bytes_per_line,image->columns+256UL)*sizeof(*pixels));
972 if (pixel_info == (MemoryInfo *) NULL)
973 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
974 pixels=(unsigned char *) GetVirtualMemoryBlob(pixel_info);
975 if (image->debug != MagickFalse)
976 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
977 " Reading pixels (%.20g bytes)",(double) length);
978 count=ReadBlob(image,length,pixels);
979 if (count != (ssize_t) length)
981 pixel_info=RelinquishVirtualMemory(pixel_info);
982 ThrowReaderException(CorruptImageError,
983 "InsufficientImageDataInFile");
989 Convert run-length encoded raster pixels.
991 pixel_info=AcquireVirtualMemory((size_t) image->rows,
992 MagickMax(bytes_per_line,image->columns+256UL)*sizeof(*pixels));
993 if (pixel_info == (MemoryInfo *) NULL)
994 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
995 pixels=(unsigned char *) GetVirtualMemoryBlob(pixel_info);
996 status=DecodeImage(image,bmp_info.compression,pixels,
997 image->columns*image->rows);
998 if (status == MagickFalse)
1000 pixel_info=RelinquishVirtualMemory(pixel_info);
1001 ThrowReaderException(CorruptImageError,
1002 "UnableToRunlengthDecodeImage");
1006 Convert BMP raster image to pixel packets.
1008 if (bmp_info.compression == BI_RGB)
1011 We should ignore the alpha value in BMP3 files but there have been
1012 reports about 32 bit files with alpha. We do a quick check to see if
1013 the alpha channel contains a value that is not zero (default value).
1014 If we find a non zero value we asume the program that wrote the file
1015 wants to use the alpha channel.
1017 if ((image->alpha_trait == UndefinedPixelTrait) && (bmp_info.size == 40) &&
1018 (bmp_info.bits_per_pixel == 32))
1020 bytes_per_line=4*(image->columns);
1021 for (y=(ssize_t) image->rows-1; y >= 0; y--)
1023 p=pixels+(image->rows-y-1)*bytes_per_line;
1024 for (x=0; x < (ssize_t) image->columns; x++)
1028 image->alpha_trait=BlendPixelTrait;
1036 bmp_info.alpha_mask=image->alpha_trait != UndefinedPixelTrait ?
1038 bmp_info.red_mask=0x00ff0000U;
1039 bmp_info.green_mask=0x0000ff00U;
1040 bmp_info.blue_mask=0x000000ffU;
1041 if (bmp_info.bits_per_pixel == 16)
1046 bmp_info.red_mask=0x00007c00U;
1047 bmp_info.green_mask=0x000003e0U;
1048 bmp_info.blue_mask=0x0000001fU;
1051 (void) ResetMagickMemory(&shift,0,sizeof(shift));
1052 (void) ResetMagickMemory(&quantum_bits,0,sizeof(quantum_bits));
1053 if ((bmp_info.bits_per_pixel == 16) || (bmp_info.bits_per_pixel == 32))
1059 Get shift and quantum bits info from bitfield masks.
1061 if (bmp_info.red_mask != 0)
1062 while (((bmp_info.red_mask << shift.red) & 0x80000000UL) == 0)
1065 if (shift.red >= 32U)
1068 if (bmp_info.green_mask != 0)
1069 while (((bmp_info.green_mask << shift.green) & 0x80000000UL) == 0)
1072 if (shift.green >= 32U)
1075 if (bmp_info.blue_mask != 0)
1076 while (((bmp_info.blue_mask << shift.blue) & 0x80000000UL) == 0)
1079 if (shift.blue >= 32U)
1082 if (bmp_info.alpha_mask != 0)
1083 while (((bmp_info.alpha_mask << shift.alpha) & 0x80000000UL) == 0)
1086 if (shift.alpha >= 32U)
1090 while (((bmp_info.red_mask << sample) & 0x80000000UL) != 0)
1096 quantum_bits.red=(MagickRealType) (sample-shift.red);
1098 while (((bmp_info.green_mask << sample) & 0x80000000UL) != 0)
1104 quantum_bits.green=(MagickRealType) (sample-shift.green);
1106 while (((bmp_info.blue_mask << sample) & 0x80000000UL) != 0)
1112 quantum_bits.blue=(MagickRealType) (sample-shift.blue);
1114 while (((bmp_info.alpha_mask << sample) & 0x80000000UL) != 0)
1120 quantum_bits.alpha=(MagickRealType) (sample-shift.alpha);
1122 switch (bmp_info.bits_per_pixel)
1127 Convert bitmap scanline.
1129 for (y=(ssize_t) image->rows-1; y >= 0; y--)
1131 p=pixels+(image->rows-y-1)*bytes_per_line;
1132 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
1133 if (q == (Quantum *) NULL)
1135 for (x=0; x < ((ssize_t) image->columns-7); x+=8)
1137 for (bit=0; bit < 8; bit++)
1139 index=(Quantum) (((*p) & (0x80 >> bit)) != 0 ? 0x01 : 0x00);
1140 SetPixelIndex(image,index,q);
1141 q+=GetPixelChannels(image);
1145 if ((image->columns % 8) != 0)
1147 for (bit=0; bit < (image->columns % 8); bit++)
1149 index=(Quantum) (((*p) & (0x80 >> bit)) != 0 ? 0x01 : 0x00);
1150 SetPixelIndex(image,index,q);
1151 q+=GetPixelChannels(image);
1155 if (SyncAuthenticPixels(image,exception) == MagickFalse)
1157 if (image->previous == (Image *) NULL)
1159 status=SetImageProgress(image,LoadImageTag,(MagickOffsetType)
1160 (image->rows-y),image->rows);
1161 if (status == MagickFalse)
1165 (void) SyncImage(image,exception);
1171 Convert PseudoColor scanline.
1173 for (y=(ssize_t) image->rows-1; y >= 0; y--)
1175 p=pixels+(image->rows-y-1)*bytes_per_line;
1176 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
1177 if (q == (Quantum *) NULL)
1179 for (x=0; x < ((ssize_t) image->columns-1); x+=2)
1181 ValidateColormapValue(image,(*p >> 4) & 0x0f,&index,exception);
1182 SetPixelIndex(image,index,q);
1183 q+=GetPixelChannels(image);
1184 ValidateColormapValue(image,*p & 0x0f,&index,exception);
1185 SetPixelIndex(image,index,q);
1186 q+=GetPixelChannels(image);
1189 if ((image->columns % 2) != 0)
1191 ValidateColormapValue(image,(*p >> 4) & 0xf,&index,exception);
1192 SetPixelIndex(image,index,q);
1193 q+=GetPixelChannels(image);
1197 if (x < (ssize_t) image->columns)
1199 if (SyncAuthenticPixels(image,exception) == MagickFalse)
1201 if (image->previous == (Image *) NULL)
1203 status=SetImageProgress(image,LoadImageTag,(MagickOffsetType)
1204 (image->rows-y),image->rows);
1205 if (status == MagickFalse)
1209 (void) SyncImage(image,exception);
1215 Convert PseudoColor scanline.
1217 if ((bmp_info.compression == BI_RLE8) ||
1218 (bmp_info.compression == BI_RLE4))
1219 bytes_per_line=image->columns;
1220 for (y=(ssize_t) image->rows-1; y >= 0; y--)
1222 p=pixels+(image->rows-y-1)*bytes_per_line;
1223 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
1224 if (q == (Quantum *) NULL)
1226 for (x=(ssize_t) image->columns; x != 0; --x)
1228 ValidateColormapValue(image,*p++,&index,exception);
1229 SetPixelIndex(image,index,q);
1230 q+=GetPixelChannels(image);
1232 if (SyncAuthenticPixels(image,exception) == MagickFalse)
1234 offset=(MagickOffsetType) (image->rows-y-1);
1235 if (image->previous == (Image *) NULL)
1237 status=SetImageProgress(image,LoadImageTag,(MagickOffsetType)
1238 (image->rows-y),image->rows);
1239 if (status == MagickFalse)
1243 (void) SyncImage(image,exception);
1253 Convert bitfield encoded 16-bit PseudoColor scanline.
1255 if (bmp_info.compression != BI_RGB &&
1256 bmp_info.compression != BI_BITFIELDS)
1258 pixel_info=RelinquishVirtualMemory(pixel_info);
1259 ThrowReaderException(CorruptImageError,
1260 "UnrecognizedImageCompression");
1262 bytes_per_line=2*(image->columns+image->columns % 2);
1263 image->storage_class=DirectClass;
1264 for (y=(ssize_t) image->rows-1; y >= 0; y--)
1266 p=pixels+(image->rows-y-1)*bytes_per_line;
1267 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
1268 if (q == (Quantum *) NULL)
1270 for (x=0; x < (ssize_t) image->columns; x++)
1272 pixel=(size_t) (*p++);
1274 red=((pixel & bmp_info.red_mask) << shift.red) >> 16;
1275 if (quantum_bits.red == 5)
1276 red|=((red & 0xe000) >> 5);
1277 if (quantum_bits.red <= 8)
1278 red|=((red & 0xff00) >> 8);
1279 green=((pixel & bmp_info.green_mask) << shift.green) >> 16;
1280 if (quantum_bits.green == 5)
1281 green|=((green & 0xe000) >> 5);
1282 if (quantum_bits.green == 6)
1283 green|=((green & 0xc000) >> 6);
1284 if (quantum_bits.green <= 8)
1285 green|=((green & 0xff00) >> 8);
1286 blue=((pixel & bmp_info.blue_mask) << shift.blue) >> 16;
1287 if (quantum_bits.blue == 5)
1288 blue|=((blue & 0xe000) >> 5);
1289 if (quantum_bits.blue <= 8)
1290 blue|=((blue & 0xff00) >> 8);
1291 SetPixelRed(image,ScaleShortToQuantum((unsigned short) red),q);
1292 SetPixelGreen(image,ScaleShortToQuantum((unsigned short) green),q);
1293 SetPixelBlue(image,ScaleShortToQuantum((unsigned short) blue),q);
1294 SetPixelAlpha(image,OpaqueAlpha,q);
1295 if (image->alpha_trait != UndefinedPixelTrait)
1297 alpha=((pixel & bmp_info.alpha_mask) << shift.alpha) >> 16;
1298 if (quantum_bits.alpha <= 8)
1299 alpha|=((alpha & 0xff00) >> 8);
1300 SetPixelAlpha(image,ScaleShortToQuantum(
1301 (unsigned short) alpha),q);
1303 q+=GetPixelChannels(image);
1305 if (SyncAuthenticPixels(image,exception) == MagickFalse)
1307 offset=(MagickOffsetType) (image->rows-y-1);
1308 if (image->previous == (Image *) NULL)
1310 status=SetImageProgress(image,LoadImageTag,(MagickOffsetType)
1311 (image->rows-y),image->rows);
1312 if (status == MagickFalse)
1321 Convert DirectColor scanline.
1323 bytes_per_line=4*((image->columns*24+31)/32);
1324 for (y=(ssize_t) image->rows-1; y >= 0; y--)
1326 p=pixels+(image->rows-y-1)*bytes_per_line;
1327 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
1328 if (q == (Quantum *) NULL)
1330 for (x=0; x < (ssize_t) image->columns; x++)
1332 SetPixelBlue(image,ScaleCharToQuantum(*p++),q);
1333 SetPixelGreen(image,ScaleCharToQuantum(*p++),q);
1334 SetPixelRed(image,ScaleCharToQuantum(*p++),q);
1335 SetPixelAlpha(image,OpaqueAlpha,q);
1336 q+=GetPixelChannels(image);
1338 if (SyncAuthenticPixels(image,exception) == MagickFalse)
1340 offset=(MagickOffsetType) (image->rows-y-1);
1341 if (image->previous == (Image *) NULL)
1343 status=SetImageProgress(image,LoadImageTag,(MagickOffsetType)
1344 (image->rows-y),image->rows);
1345 if (status == MagickFalse)
1354 Convert bitfield encoded DirectColor scanline.
1356 if ((bmp_info.compression != BI_RGB) &&
1357 (bmp_info.compression != BI_BITFIELDS))
1359 pixel_info=RelinquishVirtualMemory(pixel_info);
1360 ThrowReaderException(CorruptImageError,
1361 "UnrecognizedImageCompression");
1363 bytes_per_line=4*(image->columns);
1364 for (y=(ssize_t) image->rows-1; y >= 0; y--)
1370 p=pixels+(image->rows-y-1)*bytes_per_line;
1371 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
1372 if (q == (Quantum *) NULL)
1374 for (x=0; x < (ssize_t) image->columns; x++)
1376 pixel=(size_t) (*p++);
1377 pixel|=((size_t) *p++ << 8);
1378 pixel|=((size_t) *p++ << 16);
1379 pixel|=((size_t) *p++ << 24);
1380 red=((pixel & bmp_info.red_mask) << shift.red) >> 16;
1381 if (quantum_bits.red == 8)
1383 green=((pixel & bmp_info.green_mask) << shift.green) >> 16;
1384 if (quantum_bits.green == 8)
1385 green|=(green >> 8);
1386 blue=((pixel & bmp_info.blue_mask) << shift.blue) >> 16;
1387 if (quantum_bits.blue == 8)
1389 SetPixelRed(image,ScaleShortToQuantum((unsigned short) red),q);
1390 SetPixelGreen(image,ScaleShortToQuantum((unsigned short) green),q);
1391 SetPixelBlue(image,ScaleShortToQuantum((unsigned short) blue),q);
1392 SetPixelAlpha(image,OpaqueAlpha,q);
1393 if (image->alpha_trait != UndefinedPixelTrait)
1395 alpha=((pixel & bmp_info.alpha_mask) << shift.alpha) >> 16;
1396 if (quantum_bits.alpha == 8)
1397 alpha|=(alpha >> 8);
1398 SetPixelAlpha(image,ScaleShortToQuantum(
1399 (unsigned short) alpha),q);
1401 q+=GetPixelChannels(image);
1403 if (SyncAuthenticPixels(image,exception) == MagickFalse)
1405 offset=(MagickOffsetType) (image->rows-y-1);
1406 if (image->previous == (Image *) NULL)
1408 status=SetImageProgress(image,LoadImageTag,(MagickOffsetType)
1409 (image->rows-y),image->rows);
1410 if (status == MagickFalse)
1418 pixel_info=RelinquishVirtualMemory(pixel_info);
1419 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
1422 pixel_info=RelinquishVirtualMemory(pixel_info);
1425 if (EOFBlob(image) != MagickFalse)
1427 ThrowFileException(exception,CorruptImageError,"UnexpectedEndOfFile",
1431 if (bmp_info.height < 0)
1437 Correct image orientation.
1439 flipped_image=FlipImage(image,exception);
1440 if (flipped_image != (Image *) NULL)
1442 DuplicateBlob(flipped_image,image);
1443 ReplaceImageInList(&image, flipped_image);
1444 image=flipped_image;
1448 Proceed to next image.
1450 if (image_info->number_scenes != 0)
1451 if (image->scene >= (image_info->scene+image_info->number_scenes-1))
1454 if (bmp_info.ba_offset != 0)
1456 offset=SeekBlob(image,(MagickOffsetType) bmp_info.ba_offset,SEEK_SET);
1458 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
1460 count=ReadBlob(image,2,magick);
1461 if ((count == 2) && (IsBMP(magick,2) != MagickFalse))
1464 Acquire next image structure.
1466 AcquireNextImage(image_info,image,exception);
1467 if (GetNextImageInList(image) == (Image *) NULL)
1469 image=DestroyImageList(image);
1470 return((Image *) NULL);
1472 image=SyncNextImageInList(image);
1473 status=SetImageProgress(image,LoadImagesTag,TellBlob(image),
1474 GetBlobSize(image));
1475 if (status == MagickFalse)
1478 } while (IsBMP(magick,2) != MagickFalse);
1479 (void) CloseBlob(image);
1480 return(GetFirstImageInList(image));
1484 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1488 % R e g i s t e r B M P I m a g e %
1492 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1494 % RegisterBMPImage() adds attributes for the BMP image format to
1495 % the list of supported formats. The attributes include the image format
1496 % tag, a method to read and/or write the format, whether the format
1497 % supports the saving of more than one frame to the same file or blob,
1498 % whether the format supports native in-memory I/O, and a brief
1499 % description of the format.
1501 % The format of the RegisterBMPImage method is:
1503 % size_t RegisterBMPImage(void)
1506 ModuleExport size_t RegisterBMPImage(void)
1511 entry=AcquireMagickInfo("BMP","BMP","Microsoft Windows bitmap image");
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","BMP2","Microsoft Windows bitmap image (V2)");
1519 entry->encoder=(EncodeImageHandler *) WriteBMPImage;
1520 entry->magick=(IsImageFormatHandler *) IsBMP;
1521 entry->flags^=CoderAdjoinFlag;
1522 entry->flags|=CoderDecoderSeekableStreamFlag;
1523 (void) RegisterMagickInfo(entry);
1524 entry=AcquireMagickInfo("BMP","BMP3","Microsoft Windows bitmap image (V3)");
1525 entry->encoder=(EncodeImageHandler *) WriteBMPImage;
1526 entry->magick=(IsImageFormatHandler *) IsBMP;
1527 entry->flags^=CoderAdjoinFlag;
1528 entry->flags|=CoderDecoderSeekableStreamFlag;
1529 (void) RegisterMagickInfo(entry);
1530 return(MagickImageCoderSignature);
1534 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1538 % U n r e g i s t e r B M P I m a g e %
1542 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1544 % UnregisterBMPImage() removes format registrations made by the
1545 % BMP module from the list of supported formats.
1547 % The format of the UnregisterBMPImage method is:
1549 % UnregisterBMPImage(void)
1552 ModuleExport void UnregisterBMPImage(void)
1554 (void) UnregisterMagickInfo("BMP");
1555 (void) UnregisterMagickInfo("BMP2");
1556 (void) UnregisterMagickInfo("BMP3");
1560 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1564 % W r i t e B M P I m a g e %
1568 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1570 % WriteBMPImage() writes an image in Microsoft Windows bitmap encoded
1571 % image format, version 3 for Windows or (if the image has a matte channel)
1574 % The format of the WriteBMPImage method is:
1576 % MagickBooleanType WriteBMPImage(const ImageInfo *image_info,
1577 % Image *image,ExceptionInfo *exception)
1579 % A description of each parameter follows.
1581 % o image_info: the image info.
1583 % o image: The image.
1585 % o exception: return any errors or warnings in this structure.
1588 static MagickBooleanType WriteBMPImage(const ImageInfo *image_info,Image *image,
1589 ExceptionInfo *exception)
1613 register const Quantum
1620 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)
1655 option=GetImageOption(image_info,"bmp:format");
1656 if (option != (char *) NULL)
1658 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1659 " Format=%s",option);
1661 if (LocaleCompare(option,"bmp2") == 0)
1663 if (LocaleCompare(option,"bmp3") == 0)
1665 if (LocaleCompare(option,"bmp4") == 0)
1673 Initialize BMP raster file header.
1675 if (IssRGBCompatibleColorspace(image->colorspace) == MagickFalse)
1676 (void) TransformImageColorspace(image,sRGBColorspace,exception);
1677 (void) ResetMagickMemory(&bmp_info,0,sizeof(bmp_info));
1678 bmp_info.file_size=14+12;
1680 bmp_info.file_size+=28;
1681 bmp_info.offset_bits=bmp_info.file_size;
1682 bmp_info.compression=BI_RGB;
1683 bmp_info.red_mask=0x00ff0000U;
1684 bmp_info.green_mask=0x0000ff00U;
1685 bmp_info.blue_mask=0x000000ffU;
1686 bmp_info.alpha_mask=0xff000000U;
1687 bmp_subtype=UndefinedSubtype;
1688 if ((image->storage_class == PseudoClass) && (image->colors > 256))
1689 (void) SetImageStorageClass(image,DirectClass,exception);
1690 if (image->storage_class != DirectClass)
1693 Colormapped BMP raster.
1695 bmp_info.bits_per_pixel=8;
1696 if (image->colors <= 2)
1697 bmp_info.bits_per_pixel=1;
1699 if (image->colors <= 16)
1700 bmp_info.bits_per_pixel=4;
1702 if (image->colors <= 256)
1703 bmp_info.bits_per_pixel=8;
1704 if (image_info->compression == RLECompression)
1705 bmp_info.bits_per_pixel=8;
1706 bmp_info.number_colors=1U << bmp_info.bits_per_pixel;
1707 if (image->alpha_trait != UndefinedPixelTrait)
1708 (void) SetImageStorageClass(image,DirectClass,exception);
1710 if ((size_t) bmp_info.number_colors < image->colors)
1711 (void) SetImageStorageClass(image,DirectClass,exception);
1714 bmp_info.file_size+=3*(1UL << bmp_info.bits_per_pixel);
1715 bmp_info.offset_bits+=3*(1UL << bmp_info.bits_per_pixel);
1718 bmp_info.file_size+=(1UL << bmp_info.bits_per_pixel);
1719 bmp_info.offset_bits+=(1UL << bmp_info.bits_per_pixel);
1723 if (image->storage_class == DirectClass)
1726 Full color BMP raster.
1728 bmp_info.number_colors=0;
1729 option=GetImageOption(image_info,"bmp:subtype");
1730 if (option != (const char *) NULL)
1732 if (image->alpha_trait != UndefinedPixelTrait)
1734 if (LocaleNCompare(option,"ARGB4444",8) == 0)
1736 bmp_subtype=ARGB4444;
1737 bmp_info.red_mask=0x00000f00U;
1738 bmp_info.green_mask=0x000000f0U;
1739 bmp_info.blue_mask=0x0000000fU;
1740 bmp_info.alpha_mask=0x0000f000U;
1742 else if (LocaleNCompare(option,"ARGB1555",8) == 0)
1744 bmp_subtype=ARGB1555;
1745 bmp_info.red_mask=0x00007c00U;
1746 bmp_info.green_mask=0x000003e0U;
1747 bmp_info.blue_mask=0x0000001fU;
1748 bmp_info.alpha_mask=0x00008000U;
1753 if (LocaleNCompare(option,"RGB555",6) == 0)
1756 bmp_info.red_mask=0x00007c00U;
1757 bmp_info.green_mask=0x000003e0U;
1758 bmp_info.blue_mask=0x0000001fU;
1759 bmp_info.alpha_mask=0U;
1761 else if (LocaleNCompare(option,"RGB565",6) == 0)
1764 bmp_info.red_mask=0x0000f800U;
1765 bmp_info.green_mask=0x000007e0U;
1766 bmp_info.blue_mask=0x0000001fU;
1767 bmp_info.alpha_mask=0U;
1771 if (bmp_subtype != UndefinedSubtype)
1773 bmp_info.bits_per_pixel=16;
1774 bmp_info.compression=BI_BITFIELDS;
1778 bmp_info.bits_per_pixel=(unsigned short) ((type > 3) &&
1779 (image->alpha_trait != UndefinedPixelTrait) ? 32 : 24);
1780 bmp_info.compression=(unsigned int) ((type > 3) &&
1781 (image->alpha_trait != UndefinedPixelTrait) ? BI_BITFIELDS : BI_RGB);
1782 if ((type == 3) && (image->alpha_trait != UndefinedPixelTrait))
1784 option=GetImageOption(image_info,"bmp3:alpha");
1785 if (IsStringTrue(option))
1786 bmp_info.bits_per_pixel=32;
1790 bytes_per_line=4*((image->columns*bmp_info.bits_per_pixel+31)/32);
1791 bmp_info.ba_offset=0;
1792 profile=GetImageProfile(image,"icc");
1793 have_color_info=(image->rendering_intent != UndefinedIntent) ||
1794 (profile != (StringInfo *) NULL) || (image->gamma != 0.0) ? MagickTrue :
1799 if ((type == 3) || ((image->alpha_trait == UndefinedPixelTrait) &&
1800 (have_color_info == MagickFalse)))
1812 if ((image->rendering_intent != UndefinedIntent) ||
1813 (profile != (StringInfo *) NULL))
1818 bmp_info.file_size+=extra_size;
1819 bmp_info.offset_bits+=extra_size;
1821 if (((ssize_t) image->columns != (signed int) image->columns) ||
1822 ((ssize_t) image->rows != (signed int) image->rows))
1823 ThrowWriterException(ImageError,"WidthOrHeightExceedsLimit");
1824 bmp_info.width=(ssize_t) image->columns;
1825 bmp_info.height=(ssize_t) image->rows;
1827 bmp_info.image_size=(unsigned long) (bytes_per_line*image->rows);
1828 bmp_info.file_size+=bmp_info.image_size;
1829 bmp_info.x_pixels=75*39;
1830 bmp_info.y_pixels=75*39;
1831 switch (image->units)
1833 case UndefinedResolution:
1834 case PixelsPerInchResolution:
1836 bmp_info.x_pixels=(unsigned int) (100.0*image->resolution.x/2.54);
1837 bmp_info.y_pixels=(unsigned int) (100.0*image->resolution.y/2.54);
1840 case PixelsPerCentimeterResolution:
1842 bmp_info.x_pixels=(unsigned int) (100.0*image->resolution.x);
1843 bmp_info.y_pixels=(unsigned int) (100.0*image->resolution.y);
1847 bmp_info.colors_important=bmp_info.number_colors;
1849 Convert MIFF to BMP raster pixels.
1851 pixel_info=AcquireVirtualMemory((size_t) bmp_info.image_size,
1853 if (pixel_info == (MemoryInfo *) NULL)
1854 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
1855 pixels=(unsigned char *) GetVirtualMemoryBlob(pixel_info);
1856 (void) ResetMagickMemory(pixels,0,(size_t) bmp_info.image_size);
1857 switch (bmp_info.bits_per_pixel)
1866 Convert PseudoClass image to a BMP monochrome image.
1868 for (y=0; y < (ssize_t) image->rows; y++)
1873 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
1874 if (p == (const Quantum *) NULL)
1876 q=pixels+(image->rows-y-1)*bytes_per_line;
1879 for (x=0; x < (ssize_t) image->columns; x++)
1882 byte|=GetPixelIndex(image,p) != 0 ? 0x01 : 0x00;
1886 *q++=(unsigned char) byte;
1890 p+=GetPixelChannels(image);
1894 *q++=(unsigned char) (byte << (8-bit));
1897 offset=(ssize_t) (image->columns+7)/8;
1898 for (x=offset; x < (ssize_t) bytes_per_line; x++)
1900 if (image->previous == (Image *) NULL)
1902 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
1904 if (status == MagickFalse)
1920 Convert PseudoClass image to a BMP monochrome image.
1922 for (y=0; y < (ssize_t) image->rows; y++)
1924 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
1925 if (p == (const Quantum *) NULL)
1927 q=pixels+(image->rows-y-1)*bytes_per_line;
1930 for (x=0; x < (ssize_t) image->columns; x++)
1933 byte|=((size_t) GetPixelIndex(image,p) & 0x0f);
1937 *q++=(unsigned char) byte;
1941 p+=GetPixelChannels(image);
1945 *q++=(unsigned char) (byte << 4);
1948 offset=(ssize_t) (image->columns+1)/2;
1949 for (x=offset; x < (ssize_t) bytes_per_line; x++)
1951 if (image->previous == (Image *) NULL)
1953 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
1955 if (status == MagickFalse)
1964 Convert PseudoClass packet to BMP pixel.
1966 for (y=0; y < (ssize_t) image->rows; y++)
1968 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
1969 if (p == (const Quantum *) NULL)
1971 q=pixels+(image->rows-y-1)*bytes_per_line;
1972 for (x=0; x < (ssize_t) image->columns; x++)
1974 *q++=(unsigned char) GetPixelIndex(image,p);
1975 p+=GetPixelChannels(image);
1977 for ( ; x < (ssize_t) bytes_per_line; x++)
1979 if (image->previous == (Image *) NULL)
1981 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
1983 if (status == MagickFalse)
1992 Convert DirectClass packet to BMP BGR888.
1994 for (y=0; y < (ssize_t) image->rows; y++)
1996 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
1997 if (p == (const Quantum *) NULL)
1999 q=pixels+(image->rows-y-1)*bytes_per_line;
2000 for (x=0; x < (ssize_t) image->columns; x++)
2006 if (bmp_subtype == ARGB4444)
2008 pixel=(unsigned short) ScaleQuantumToAny(
2009 GetPixelAlpha(image,p),15) << 12;
2010 pixel|=(unsigned short) ScaleQuantumToAny(
2011 GetPixelRed(image,p),15) << 8;
2012 pixel|=(unsigned short) ScaleQuantumToAny(
2013 GetPixelGreen(image,p),15) << 4;
2014 pixel|=(unsigned short) ScaleQuantumToAny(
2015 GetPixelBlue(image,p),15);
2017 else if (bmp_subtype == RGB565)
2019 pixel=(unsigned short) ScaleQuantumToAny(
2020 GetPixelRed(image,p),31) << 11;
2021 pixel|=(unsigned short) ScaleQuantumToAny(
2022 GetPixelGreen(image,p),63) << 5;
2023 pixel|=(unsigned short) ScaleQuantumToAny(
2024 GetPixelBlue(image,p),31);
2028 if (bmp_subtype == ARGB1555)
2029 pixel=(unsigned short) ScaleQuantumToAny(
2030 GetPixelAlpha(image,p),1) << 15;
2031 pixel|=(unsigned short) ScaleQuantumToAny(
2032 GetPixelRed(image,p),31) << 10;
2033 pixel|=(unsigned short) ScaleQuantumToAny(
2034 GetPixelGreen(image,p),31) << 5;
2035 pixel|=(unsigned short) ScaleQuantumToAny(
2036 GetPixelBlue(image,p),31);
2038 *((unsigned short *) q)=pixel;
2040 p+=GetPixelChannels(image);
2042 for (x=2L*(ssize_t) image->columns; x < (ssize_t) bytes_per_line; x++)
2044 if (image->previous == (Image *) NULL)
2046 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
2048 if (status == MagickFalse)
2057 Convert DirectClass packet to BMP BGR888.
2059 for (y=0; y < (ssize_t) image->rows; y++)
2061 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
2062 if (p == (const Quantum *) NULL)
2064 q=pixels+(image->rows-y-1)*bytes_per_line;
2065 for (x=0; x < (ssize_t) image->columns; x++)
2067 *q++=ScaleQuantumToChar(GetPixelBlue(image,p));
2068 *q++=ScaleQuantumToChar(GetPixelGreen(image,p));
2069 *q++=ScaleQuantumToChar(GetPixelRed(image,p));
2070 p+=GetPixelChannels(image);
2072 for (x=3L*(ssize_t) image->columns; x < (ssize_t) bytes_per_line; x++)
2074 if (image->previous == (Image *) NULL)
2076 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
2078 if (status == MagickFalse)
2087 Convert DirectClass packet to ARGB8888 pixel.
2089 for (y=0; y < (ssize_t) image->rows; y++)
2091 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
2092 if (p == (const Quantum *) NULL)
2094 q=pixels+(image->rows-y-1)*bytes_per_line;
2095 for (x=0; x < (ssize_t) image->columns; x++)
2097 *q++=ScaleQuantumToChar(GetPixelBlue(image,p));
2098 *q++=ScaleQuantumToChar(GetPixelGreen(image,p));
2099 *q++=ScaleQuantumToChar(GetPixelRed(image,p));
2100 *q++=ScaleQuantumToChar(GetPixelAlpha(image,p));
2101 p+=GetPixelChannels(image);
2103 if (image->previous == (Image *) NULL)
2105 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
2107 if (status == MagickFalse)
2114 if ((type > 2) && (bmp_info.bits_per_pixel == 8))
2115 if (image_info->compression != NoCompression)
2121 Convert run-length encoded raster pixels.
2123 rle_info=AcquireVirtualMemory((size_t) (2*(bytes_per_line+2)+2),
2124 (image->rows+2)*sizeof(*pixels));
2125 if (rle_info == (MemoryInfo *) NULL)
2127 pixel_info=RelinquishVirtualMemory(pixel_info);
2128 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
2130 bmp_data=(unsigned char *) GetVirtualMemoryBlob(rle_info);
2131 bmp_info.file_size-=bmp_info.image_size;
2132 bmp_info.image_size=(unsigned int) EncodeImage(image,bytes_per_line,
2134 bmp_info.file_size+=bmp_info.image_size;
2135 pixel_info=RelinquishVirtualMemory(pixel_info);
2136 pixel_info=rle_info;
2138 bmp_info.compression=BI_RLE8;
2141 Write BMP for Windows, all versions, 14-byte header.
2143 if (image->debug != MagickFalse)
2145 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
2146 " Writing BMP version %.20g datastream",(double) type);
2147 if (image->storage_class == DirectClass)
2148 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
2149 " Storage class=DirectClass");
2151 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
2152 " Storage class=PseudoClass");
2153 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
2154 " Image depth=%.20g",(double) image->depth);
2155 if (image->alpha_trait != UndefinedPixelTrait)
2156 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
2159 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
2160 " Matte=MagickFalse");
2161 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
2162 " BMP bits_per_pixel=%.20g",(double) bmp_info.bits_per_pixel);
2163 switch ((int) bmp_info.compression)
2167 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
2168 " Compression=BI_RGB");
2173 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
2174 " Compression=BI_RLE8");
2179 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
2180 " Compression=BI_BITFIELDS");
2185 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
2186 " Compression=UNKNOWN (%lu)",bmp_info.compression);
2190 if (bmp_info.number_colors == 0)
2191 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
2192 " Number_colors=unspecified");
2194 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
2195 " Number_colors=%lu",bmp_info.number_colors);
2197 (void) WriteBlob(image,2,(unsigned char *) "BM");
2198 (void) WriteBlobLSBLong(image,bmp_info.file_size);
2199 (void) WriteBlobLSBLong(image,bmp_info.ba_offset); /* always 0 */
2200 (void) WriteBlobLSBLong(image,bmp_info.offset_bits);
2204 Write 12-byte version 2 bitmap header.
2206 (void) WriteBlobLSBLong(image,bmp_info.size);
2207 (void) WriteBlobLSBSignedShort(image,(signed short) bmp_info.width);
2208 (void) WriteBlobLSBSignedShort(image,(signed short) bmp_info.height);
2209 (void) WriteBlobLSBShort(image,bmp_info.planes);
2210 (void) WriteBlobLSBShort(image,bmp_info.bits_per_pixel);
2215 Write 40-byte version 3+ bitmap header.
2217 (void) WriteBlobLSBLong(image,bmp_info.size);
2218 (void) WriteBlobLSBSignedLong(image,(signed int) bmp_info.width);
2219 (void) WriteBlobLSBSignedLong(image,(signed int) bmp_info.height);
2220 (void) WriteBlobLSBShort(image,bmp_info.planes);
2221 (void) WriteBlobLSBShort(image,bmp_info.bits_per_pixel);
2222 (void) WriteBlobLSBLong(image,bmp_info.compression);
2223 (void) WriteBlobLSBLong(image,bmp_info.image_size);
2224 (void) WriteBlobLSBLong(image,bmp_info.x_pixels);
2225 (void) WriteBlobLSBLong(image,bmp_info.y_pixels);
2226 (void) WriteBlobLSBLong(image,bmp_info.number_colors);
2227 (void) WriteBlobLSBLong(image,bmp_info.colors_important);
2229 if ((type > 3) && ((image->alpha_trait != UndefinedPixelTrait) ||
2230 (have_color_info != MagickFalse)))
2233 Write the rest of the 108-byte BMP Version 4 header.
2235 (void) WriteBlobLSBLong(image,bmp_info.red_mask);
2236 (void) WriteBlobLSBLong(image,bmp_info.green_mask);
2237 (void) WriteBlobLSBLong(image,bmp_info.blue_mask);
2238 (void) WriteBlobLSBLong(image,bmp_info.alpha_mask);
2239 (void) WriteBlobLSBLong(image,0x73524742U); /* sRGB */
2240 (void) WriteBlobLSBLong(image,(unsigned int)
2241 (image->chromaticity.red_primary.x*0x40000000));
2242 (void) WriteBlobLSBLong(image,(unsigned int)
2243 (image->chromaticity.red_primary.y*0x40000000));
2244 (void) WriteBlobLSBLong(image,(unsigned int)
2245 ((1.000f-(image->chromaticity.red_primary.x+
2246 image->chromaticity.red_primary.y))*0x40000000));
2247 (void) WriteBlobLSBLong(image,(unsigned int)
2248 (image->chromaticity.green_primary.x*0x40000000));
2249 (void) WriteBlobLSBLong(image,(unsigned int)
2250 (image->chromaticity.green_primary.y*0x40000000));
2251 (void) WriteBlobLSBLong(image,(unsigned int)
2252 ((1.000f-(image->chromaticity.green_primary.x+
2253 image->chromaticity.green_primary.y))*0x40000000));
2254 (void) WriteBlobLSBLong(image,(unsigned int)
2255 (image->chromaticity.blue_primary.x*0x40000000));
2256 (void) WriteBlobLSBLong(image,(unsigned int)
2257 (image->chromaticity.blue_primary.y*0x40000000));
2258 (void) WriteBlobLSBLong(image,(unsigned int)
2259 ((1.000f-(image->chromaticity.blue_primary.x+
2260 image->chromaticity.blue_primary.y))*0x40000000));
2261 (void) WriteBlobLSBLong(image,(unsigned int)
2262 (bmp_info.gamma_scale.x*0x10000));
2263 (void) WriteBlobLSBLong(image,(unsigned int)
2264 (bmp_info.gamma_scale.y*0x10000));
2265 (void) WriteBlobLSBLong(image,(unsigned int)
2266 (bmp_info.gamma_scale.z*0x10000));
2267 if ((image->rendering_intent != UndefinedIntent) ||
2268 (profile != (StringInfo *) NULL))
2273 switch ((int) image->rendering_intent)
2275 case SaturationIntent:
2277 intent=LCS_GM_BUSINESS;
2280 case RelativeIntent:
2282 intent=LCS_GM_GRAPHICS;
2285 case PerceptualIntent:
2287 intent=LCS_GM_IMAGES;
2290 case AbsoluteIntent:
2292 intent=LCS_GM_ABS_COLORIMETRIC;
2301 (void) WriteBlobLSBLong(image,(unsigned int) intent);
2302 (void) WriteBlobLSBLong(image,0x00); /* dummy profile data */
2303 (void) WriteBlobLSBLong(image,0x00); /* dummy profile length */
2304 (void) WriteBlobLSBLong(image,0x00); /* reserved */
2307 if (image->storage_class == PseudoClass)
2313 Dump colormap to file.
2315 if (image->debug != MagickFalse)
2316 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
2317 " Colormap: %.20g entries",(double) image->colors);
2318 bmp_colormap=(unsigned char *) AcquireQuantumMemory((size_t) (1UL <<
2319 bmp_info.bits_per_pixel),4*sizeof(*bmp_colormap));
2320 if (bmp_colormap == (unsigned char *) NULL)
2321 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
2323 for (i=0; i < (ssize_t) MagickMin((ssize_t) image->colors,(ssize_t) bmp_info.number_colors); i++)
2325 *q++=ScaleQuantumToChar(ClampToQuantum(image->colormap[i].blue));
2326 *q++=ScaleQuantumToChar(ClampToQuantum(image->colormap[i].green));
2327 *q++=ScaleQuantumToChar(ClampToQuantum(image->colormap[i].red));
2329 *q++=(unsigned char) 0x0;
2331 for ( ; i < (ssize_t) (1UL << bmp_info.bits_per_pixel); i++)
2333 *q++=(unsigned char) 0x00;
2334 *q++=(unsigned char) 0x00;
2335 *q++=(unsigned char) 0x00;
2337 *q++=(unsigned char) 0x00;
2340 (void) WriteBlob(image,(size_t) (3*(1L << bmp_info.bits_per_pixel)),
2343 (void) WriteBlob(image,(size_t) (4*(1L << bmp_info.bits_per_pixel)),
2345 bmp_colormap=(unsigned char *) RelinquishMagickMemory(bmp_colormap);
2347 if (image->debug != MagickFalse)
2348 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
2349 " Pixels: %lu bytes",bmp_info.image_size);
2350 (void) WriteBlob(image,(size_t) bmp_info.image_size,pixels);
2351 pixel_info=RelinquishVirtualMemory(pixel_info);
2352 if (GetNextImageInList(image) == (Image *) NULL)
2354 image=SyncNextImageInList(image);
2355 status=SetImageProgress(image,SaveImagesTag,scene++,
2356 GetImageListLength(image));
2357 if (status == MagickFalse)
2359 } while (image_info->adjoin != MagickFalse);
2360 (void) CloseBlob(image);