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) ResetMagickMemory(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) ResetMagickMemory(&bmp_info,0,sizeof(bmp_info));
581 bmp_info.ba_offset=0;
584 count=ReadBlob(image,2,magick);
586 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
600 Verify BMP identifier.
602 if (bmp_info.ba_offset == 0)
603 start_position=TellBlob(image)-2;
604 bmp_info.ba_offset=0;
605 while (LocaleNCompare((char *) magick,"BA",2) == 0)
607 bmp_info.file_size=ReadBlobLSBLong(image);
608 bmp_info.ba_offset=ReadBlobLSBLong(image);
609 bmp_info.offset_bits=ReadBlobLSBLong(image);
610 count=ReadBlob(image,2,magick);
614 if (image->debug != MagickFalse)
615 (void) LogMagickEvent(CoderEvent,GetMagickModule()," Magick: %c%c",
616 magick[0],magick[1]);
617 if ((count != 2) || ((LocaleNCompare((char *) magick,"BM",2) != 0) &&
618 (LocaleNCompare((char *) magick,"CI",2) != 0)))
619 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
620 bmp_info.file_size=ReadBlobLSBLong(image);
621 (void) ReadBlobLSBLong(image);
622 bmp_info.offset_bits=ReadBlobLSBLong(image);
623 bmp_info.size=ReadBlobLSBLong(image);
624 if (image->debug != MagickFalse)
625 (void) LogMagickEvent(CoderEvent,GetMagickModule()," BMP size: %u",
627 if (bmp_info.size == 12)
632 (void) CopyMagickString(image->magick,"BMP2",MagickPathExtent);
633 bmp_info.width=(ssize_t) ((short) ReadBlobLSBShort(image));
634 bmp_info.height=(ssize_t) ((short) ReadBlobLSBShort(image));
635 bmp_info.planes=ReadBlobLSBShort(image);
636 bmp_info.bits_per_pixel=ReadBlobLSBShort(image);
639 bmp_info.number_colors=0;
640 bmp_info.compression=BI_RGB;
641 bmp_info.image_size=0;
642 bmp_info.alpha_mask=0;
643 if (image->debug != MagickFalse)
645 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
646 " Format: OS/2 Bitmap");
647 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
648 " Geometry: %.20gx%.20g",(double) bmp_info.width,(double)
655 Microsoft Windows BMP image file.
657 if (bmp_info.size < 40)
658 ThrowReaderException(CorruptImageError,"NonOS2HeaderSizeError");
659 bmp_info.width=(ssize_t) ReadBlobLSBSignedLong(image);
660 bmp_info.height=(ssize_t) ReadBlobLSBSignedLong(image);
661 bmp_info.planes=ReadBlobLSBShort(image);
662 bmp_info.bits_per_pixel=ReadBlobLSBShort(image);
663 bmp_info.compression=ReadBlobLSBLong(image);
664 bmp_info.image_size=ReadBlobLSBLong(image);
665 bmp_info.x_pixels=ReadBlobLSBLong(image);
666 bmp_info.y_pixels=ReadBlobLSBLong(image);
667 bmp_info.number_colors=ReadBlobLSBLong(image);
668 bmp_info.colors_important=ReadBlobLSBLong(image);
671 if (image->debug != MagickFalse)
673 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
674 " Format: MS Windows bitmap");
675 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
676 " Geometry: %.20gx%.20g",(double) bmp_info.width,(double)
678 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
679 " Bits per pixel: %.20g",(double) bmp_info.bits_per_pixel);
680 switch ((int) bmp_info.compression)
684 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
685 " Compression: BI_RGB");
690 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
691 " Compression: BI_RLE4");
696 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
697 " Compression: BI_RLE8");
702 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
703 " Compression: BI_BITFIELDS");
708 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
709 " Compression: BI_PNG");
714 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
715 " Compression: BI_JPEG");
720 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
721 " Compression: UNKNOWN (%u)",bmp_info.compression);
724 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
725 " Number of colors: %u",bmp_info.number_colors);
727 bmp_info.red_mask=ReadBlobLSBLong(image);
728 bmp_info.green_mask=ReadBlobLSBLong(image);
729 bmp_info.blue_mask=ReadBlobLSBLong(image);
730 if (bmp_info.size > 40)
736 Read color management information.
738 bmp_info.alpha_mask=ReadBlobLSBLong(image);
739 bmp_info.colorspace=ReadBlobLSBSignedLong(image);
741 Decode 2^30 fixed point formatted CIE primaries.
743 # define BMP_DENOM ((double) 0x40000000)
744 bmp_info.red_primary.x=(double) ReadBlobLSBLong(image)/BMP_DENOM;
745 bmp_info.red_primary.y=(double) ReadBlobLSBLong(image)/BMP_DENOM;
746 bmp_info.red_primary.z=(double) ReadBlobLSBLong(image)/BMP_DENOM;
747 bmp_info.green_primary.x=(double) ReadBlobLSBLong(image)/BMP_DENOM;
748 bmp_info.green_primary.y=(double) ReadBlobLSBLong(image)/BMP_DENOM;
749 bmp_info.green_primary.z=(double) ReadBlobLSBLong(image)/BMP_DENOM;
750 bmp_info.blue_primary.x=(double) ReadBlobLSBLong(image)/BMP_DENOM;
751 bmp_info.blue_primary.y=(double) ReadBlobLSBLong(image)/BMP_DENOM;
752 bmp_info.blue_primary.z=(double) ReadBlobLSBLong(image)/BMP_DENOM;
754 gamma=bmp_info.red_primary.x+bmp_info.red_primary.y+
755 bmp_info.red_primary.z;
756 gamma=PerceptibleReciprocal(gamma);
757 bmp_info.red_primary.x*=gamma;
758 bmp_info.red_primary.y*=gamma;
759 image->chromaticity.red_primary.x=bmp_info.red_primary.x;
760 image->chromaticity.red_primary.y=bmp_info.red_primary.y;
762 gamma=bmp_info.green_primary.x+bmp_info.green_primary.y+
763 bmp_info.green_primary.z;
764 gamma=PerceptibleReciprocal(gamma);
765 bmp_info.green_primary.x*=gamma;
766 bmp_info.green_primary.y*=gamma;
767 image->chromaticity.green_primary.x=bmp_info.green_primary.x;
768 image->chromaticity.green_primary.y=bmp_info.green_primary.y;
770 gamma=bmp_info.blue_primary.x+bmp_info.blue_primary.y+
771 bmp_info.blue_primary.z;
772 gamma=PerceptibleReciprocal(gamma);
773 bmp_info.blue_primary.x*=gamma;
774 bmp_info.blue_primary.y*=gamma;
775 image->chromaticity.blue_primary.x=bmp_info.blue_primary.x;
776 image->chromaticity.blue_primary.y=bmp_info.blue_primary.y;
779 Decode 16^16 fixed point formatted gamma_scales.
781 bmp_info.gamma_scale.x=(double) ReadBlobLSBLong(image)/0x10000;
782 bmp_info.gamma_scale.y=(double) ReadBlobLSBLong(image)/0x10000;
783 bmp_info.gamma_scale.z=(double) ReadBlobLSBLong(image)/0x10000;
785 Compute a single gamma from the BMP 3-channel gamma.
787 image->gamma=(bmp_info.gamma_scale.x+bmp_info.gamma_scale.y+
788 bmp_info.gamma_scale.z)/3.0;
791 (void) CopyMagickString(image->magick,"BMP3",MagickPathExtent);
793 if (bmp_info.size > 108)
799 Read BMP Version 5 color management information.
801 intent=ReadBlobLSBLong(image);
802 switch ((int) intent)
804 case LCS_GM_BUSINESS:
806 image->rendering_intent=SaturationIntent;
809 case LCS_GM_GRAPHICS:
811 image->rendering_intent=RelativeIntent;
816 image->rendering_intent=PerceptualIntent;
819 case LCS_GM_ABS_COLORIMETRIC:
821 image->rendering_intent=AbsoluteIntent;
825 profile_data=ReadBlobLSBLong(image);
826 profile_size=ReadBlobLSBLong(image);
829 (void) ReadBlobLSBLong(image); /* Reserved byte */
832 if ((MagickSizeType) bmp_info.file_size > GetBlobSize(image))
833 (void) ThrowMagickException(exception,GetMagickModule(),CorruptImageError,
834 "LengthAndFilesizeDoNotMatch","`%s'",image->filename);
836 if ((MagickSizeType) bmp_info.file_size < GetBlobSize(image))
837 (void) ThrowMagickException(exception,GetMagickModule(),
838 CorruptImageWarning,"LengthAndFilesizeDoNotMatch","`%s'",
840 if (bmp_info.width <= 0)
841 ThrowReaderException(CorruptImageError,"NegativeOrZeroImageSize");
842 if (bmp_info.height == 0)
843 ThrowReaderException(CorruptImageError,"NegativeOrZeroImageSize");
844 if (bmp_info.planes != 1)
845 ThrowReaderException(CorruptImageError,"StaticPlanesValueNotEqualToOne");
846 if ((bmp_info.bits_per_pixel != 1) && (bmp_info.bits_per_pixel != 4) &&
847 (bmp_info.bits_per_pixel != 8) && (bmp_info.bits_per_pixel != 16) &&
848 (bmp_info.bits_per_pixel != 24) && (bmp_info.bits_per_pixel != 32))
849 ThrowReaderException(CorruptImageError,"UnrecognizedBitsPerPixel");
850 if (bmp_info.bits_per_pixel < 16 &&
851 bmp_info.number_colors > (1U << bmp_info.bits_per_pixel))
852 ThrowReaderException(CorruptImageError,"UnrecognizedNumberOfColors");
853 if ((bmp_info.compression == 1) && (bmp_info.bits_per_pixel != 8))
854 ThrowReaderException(CorruptImageError,"UnrecognizedBitsPerPixel");
855 if ((bmp_info.compression == 2) && (bmp_info.bits_per_pixel != 4))
856 ThrowReaderException(CorruptImageError,"UnrecognizedBitsPerPixel");
857 if ((bmp_info.compression == 3) && (bmp_info.bits_per_pixel < 16))
858 ThrowReaderException(CorruptImageError,"UnrecognizedBitsPerPixel");
859 switch (bmp_info.compression)
862 image->compression=NoCompression;
866 image->compression=RLECompression;
871 ThrowReaderException(CoderError,"JPEGCompressNotSupported");
873 ThrowReaderException(CoderError,"PNGCompressNotSupported");
875 ThrowReaderException(CorruptImageError,"UnrecognizedImageCompression");
877 image->columns=(size_t) MagickAbsoluteValue(bmp_info.width);
878 image->rows=(size_t) MagickAbsoluteValue(bmp_info.height);
879 image->depth=bmp_info.bits_per_pixel <= 8 ? bmp_info.bits_per_pixel : 8;
880 image->alpha_trait=((bmp_info.alpha_mask != 0) &&
881 (bmp_info.compression == BI_BITFIELDS)) ? BlendPixelTrait :
883 if (bmp_info.bits_per_pixel < 16)
888 image->storage_class=PseudoClass;
889 image->colors=bmp_info.number_colors;
891 if (image->colors == 0)
892 image->colors=one << bmp_info.bits_per_pixel;
894 image->resolution.x=(double) bmp_info.x_pixels/100.0;
895 image->resolution.y=(double) bmp_info.y_pixels/100.0;
896 image->units=PixelsPerCentimeterResolution;
897 if ((image_info->ping != MagickFalse) && (image_info->number_scenes != 0))
898 if (image->scene >= (image_info->scene+image_info->number_scenes-1))
900 status=SetImageExtent(image,image->columns,image->rows,exception);
901 if (status == MagickFalse)
902 return(DestroyImageList(image));
903 if (image->storage_class == PseudoClass)
912 Read BMP raster colormap.
914 if (image->debug != MagickFalse)
915 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
916 " Reading colormap of %.20g colors",(double) image->colors);
917 if (AcquireImageColormap(image,image->colors,exception) == MagickFalse)
918 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
919 bmp_colormap=(unsigned char *) AcquireQuantumMemory((size_t)
920 image->colors,4*sizeof(*bmp_colormap));
921 if (bmp_colormap == (unsigned char *) NULL)
922 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
923 if ((bmp_info.size == 12) || (bmp_info.size == 64))
927 offset=SeekBlob(image,start_position+14+bmp_info.size,SEEK_SET);
930 bmp_colormap=(unsigned char *) RelinquishMagickMemory(bmp_colormap);
931 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
933 count=ReadBlob(image,packet_size*image->colors,bmp_colormap);
934 if (count != (ssize_t) (packet_size*image->colors))
936 bmp_colormap=(unsigned char *) RelinquishMagickMemory(bmp_colormap);
937 ThrowReaderException(CorruptImageError,
938 "InsufficientImageDataInFile");
941 for (i=0; i < (ssize_t) image->colors; i++)
943 image->colormap[i].blue=(MagickRealType) ScaleCharToQuantum(*p++);
944 image->colormap[i].green=(MagickRealType) ScaleCharToQuantum(*p++);
945 image->colormap[i].red=(MagickRealType) ScaleCharToQuantum(*p++);
946 if (packet_size == 4)
949 bmp_colormap=(unsigned char *) RelinquishMagickMemory(bmp_colormap);
954 if (bmp_info.offset_bits == offset_bits)
955 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
956 offset_bits=bmp_info.offset_bits;
957 offset=SeekBlob(image,start_position+bmp_info.offset_bits,SEEK_SET);
959 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
960 if (bmp_info.compression == BI_RLE4)
961 bmp_info.bits_per_pixel<<=1;
962 bytes_per_line=4*((image->columns*bmp_info.bits_per_pixel+31)/32);
963 length=(size_t) bytes_per_line*image->rows;
964 if ((MagickSizeType) length > GetBlobSize(image))
965 ThrowReaderException(CorruptImageError,"InsufficientImageDataInFile");
966 if ((bmp_info.compression == BI_RGB) ||
967 (bmp_info.compression == BI_BITFIELDS))
969 pixel_info=AcquireVirtualMemory((size_t) image->rows,
970 MagickMax(bytes_per_line,image->columns+256UL)*sizeof(*pixels));
971 if (pixel_info == (MemoryInfo *) NULL)
972 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
973 pixels=(unsigned char *) GetVirtualMemoryBlob(pixel_info);
974 if (image->debug != MagickFalse)
975 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
976 " Reading pixels (%.20g bytes)",(double) length);
977 count=ReadBlob(image,length,pixels);
978 if (count != (ssize_t) length)
980 pixel_info=RelinquishVirtualMemory(pixel_info);
981 ThrowReaderException(CorruptImageError,
982 "InsufficientImageDataInFile");
988 Convert run-length encoded raster pixels.
990 pixel_info=AcquireVirtualMemory((size_t) image->rows,
991 MagickMax(bytes_per_line,image->columns+256UL)*sizeof(*pixels));
992 if (pixel_info == (MemoryInfo *) NULL)
993 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
994 pixels=(unsigned char *) GetVirtualMemoryBlob(pixel_info);
995 status=DecodeImage(image,bmp_info.compression,pixels,
996 image->columns*image->rows);
997 if (status == MagickFalse)
999 pixel_info=RelinquishVirtualMemory(pixel_info);
1000 ThrowReaderException(CorruptImageError,
1001 "UnableToRunlengthDecodeImage");
1005 Convert BMP raster image to pixel packets.
1007 if (bmp_info.compression == BI_RGB)
1010 We should ignore the alpha value in BMP3 files but there have been
1011 reports about 32 bit files with alpha. We do a quick check to see if
1012 the alpha channel contains a value that is not zero (default value).
1013 If we find a non zero value we asume the program that wrote the file
1014 wants to use the alpha channel.
1016 if ((image->alpha_trait == UndefinedPixelTrait) && (bmp_info.size == 40) &&
1017 (bmp_info.bits_per_pixel == 32))
1019 bytes_per_line=4*(image->columns);
1020 for (y=(ssize_t) image->rows-1; y >= 0; y--)
1022 p=pixels+(image->rows-y-1)*bytes_per_line;
1023 for (x=0; x < (ssize_t) image->columns; x++)
1027 image->alpha_trait=BlendPixelTrait;
1035 bmp_info.alpha_mask=image->alpha_trait != UndefinedPixelTrait ?
1037 bmp_info.red_mask=0x00ff0000U;
1038 bmp_info.green_mask=0x0000ff00U;
1039 bmp_info.blue_mask=0x000000ffU;
1040 if (bmp_info.bits_per_pixel == 16)
1045 bmp_info.red_mask=0x00007c00U;
1046 bmp_info.green_mask=0x000003e0U;
1047 bmp_info.blue_mask=0x0000001fU;
1050 (void) ResetMagickMemory(&shift,0,sizeof(shift));
1051 (void) ResetMagickMemory(&quantum_bits,0,sizeof(quantum_bits));
1052 if ((bmp_info.bits_per_pixel == 16) || (bmp_info.bits_per_pixel == 32))
1054 register unsigned int
1058 Get shift and quantum bits info from bitfield masks.
1060 if (bmp_info.red_mask != 0)
1061 while (((bmp_info.red_mask << shift.red) & 0x80000000UL) == 0)
1064 if (shift.red >= 32U)
1067 if (bmp_info.green_mask != 0)
1068 while (((bmp_info.green_mask << shift.green) & 0x80000000UL) == 0)
1071 if (shift.green >= 32U)
1074 if (bmp_info.blue_mask != 0)
1075 while (((bmp_info.blue_mask << shift.blue) & 0x80000000UL) == 0)
1078 if (shift.blue >= 32U)
1081 if (bmp_info.alpha_mask != 0)
1082 while (((bmp_info.alpha_mask << shift.alpha) & 0x80000000UL) == 0)
1085 if (shift.alpha >= 32U)
1089 while (((bmp_info.red_mask << sample) & 0x80000000UL) != 0)
1095 quantum_bits.red=(MagickRealType) (sample-shift.red);
1097 while (((bmp_info.green_mask << sample) & 0x80000000UL) != 0)
1103 quantum_bits.green=(MagickRealType) (sample-shift.green);
1105 while (((bmp_info.blue_mask << sample) & 0x80000000UL) != 0)
1111 quantum_bits.blue=(MagickRealType) (sample-shift.blue);
1113 while (((bmp_info.alpha_mask << sample) & 0x80000000UL) != 0)
1119 quantum_bits.alpha=(MagickRealType) (sample-shift.alpha);
1121 switch (bmp_info.bits_per_pixel)
1126 Convert bitmap scanline.
1128 for (y=(ssize_t) image->rows-1; y >= 0; y--)
1130 p=pixels+(image->rows-y-1)*bytes_per_line;
1131 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
1132 if (q == (Quantum *) NULL)
1134 for (x=0; x < ((ssize_t) image->columns-7); x+=8)
1136 for (bit=0; bit < 8; bit++)
1138 index=(Quantum) (((*p) & (0x80 >> bit)) != 0 ? 0x01 : 0x00);
1139 SetPixelIndex(image,index,q);
1140 q+=GetPixelChannels(image);
1144 if ((image->columns % 8) != 0)
1146 for (bit=0; bit < (image->columns % 8); bit++)
1148 index=(Quantum) (((*p) & (0x80 >> bit)) != 0 ? 0x01 : 0x00);
1149 SetPixelIndex(image,index,q);
1150 q+=GetPixelChannels(image);
1154 if (SyncAuthenticPixels(image,exception) == MagickFalse)
1156 if (image->previous == (Image *) NULL)
1158 status=SetImageProgress(image,LoadImageTag,(MagickOffsetType)
1159 (image->rows-y),image->rows);
1160 if (status == MagickFalse)
1164 (void) SyncImage(image,exception);
1170 Convert PseudoColor scanline.
1172 for (y=(ssize_t) image->rows-1; y >= 0; y--)
1174 p=pixels+(image->rows-y-1)*bytes_per_line;
1175 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
1176 if (q == (Quantum *) NULL)
1178 for (x=0; x < ((ssize_t) image->columns-1); x+=2)
1180 ValidateColormapValue(image,(ssize_t) ((*p >> 4) & 0x0f),&index,
1182 SetPixelIndex(image,index,q);
1183 q+=GetPixelChannels(image);
1184 ValidateColormapValue(image,(ssize_t) (*p & 0x0f),&index,exception);
1185 SetPixelIndex(image,index,q);
1186 q+=GetPixelChannels(image);
1189 if ((image->columns % 2) != 0)
1191 ValidateColormapValue(image,(ssize_t) ((*p >> 4) & 0xf),&index,
1193 SetPixelIndex(image,index,q);
1194 q+=GetPixelChannels(image);
1198 if (x < (ssize_t) image->columns)
1200 if (SyncAuthenticPixels(image,exception) == MagickFalse)
1202 if (image->previous == (Image *) NULL)
1204 status=SetImageProgress(image,LoadImageTag,(MagickOffsetType)
1205 (image->rows-y),image->rows);
1206 if (status == MagickFalse)
1210 (void) SyncImage(image,exception);
1216 Convert PseudoColor scanline.
1218 if ((bmp_info.compression == BI_RLE8) ||
1219 (bmp_info.compression == BI_RLE4))
1220 bytes_per_line=image->columns;
1221 for (y=(ssize_t) image->rows-1; y >= 0; y--)
1223 p=pixels+(image->rows-y-1)*bytes_per_line;
1224 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
1225 if (q == (Quantum *) NULL)
1227 for (x=(ssize_t) image->columns; x != 0; --x)
1229 ValidateColormapValue(image,(ssize_t) *p++,&index,exception);
1230 SetPixelIndex(image,index,q);
1231 q+=GetPixelChannels(image);
1233 if (SyncAuthenticPixels(image,exception) == MagickFalse)
1235 offset=(MagickOffsetType) (image->rows-y-1);
1236 if (image->previous == (Image *) NULL)
1238 status=SetImageProgress(image,LoadImageTag,(MagickOffsetType)
1239 (image->rows-y),image->rows);
1240 if (status == MagickFalse)
1244 (void) SyncImage(image,exception);
1254 Convert bitfield encoded 16-bit PseudoColor scanline.
1256 if ((bmp_info.compression != BI_RGB) &&
1257 (bmp_info.compression != BI_BITFIELDS))
1259 pixel_info=RelinquishVirtualMemory(pixel_info);
1260 ThrowReaderException(CorruptImageError,
1261 "UnrecognizedImageCompression");
1263 bytes_per_line=2*(image->columns+image->columns % 2);
1264 image->storage_class=DirectClass;
1265 for (y=(ssize_t) image->rows-1; y >= 0; y--)
1267 p=pixels+(image->rows-y-1)*bytes_per_line;
1268 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
1269 if (q == (Quantum *) NULL)
1271 for (x=0; x < (ssize_t) image->columns; x++)
1273 pixel=(unsigned int) (*p++);
1275 red=((pixel & bmp_info.red_mask) << shift.red) >> 16;
1276 if (quantum_bits.red == 5)
1277 red|=((red & 0xe000) >> 5);
1278 if (quantum_bits.red <= 8)
1279 red|=((red & 0xff00) >> 8);
1280 green=((pixel & bmp_info.green_mask) << shift.green) >> 16;
1281 if (quantum_bits.green == 5)
1282 green|=((green & 0xe000) >> 5);
1283 if (quantum_bits.green == 6)
1284 green|=((green & 0xc000) >> 6);
1285 if (quantum_bits.green <= 8)
1286 green|=((green & 0xff00) >> 8);
1287 blue=((pixel & bmp_info.blue_mask) << shift.blue) >> 16;
1288 if (quantum_bits.blue == 5)
1289 blue|=((blue & 0xe000) >> 5);
1290 if (quantum_bits.blue <= 8)
1291 blue|=((blue & 0xff00) >> 8);
1292 SetPixelRed(image,ScaleShortToQuantum((unsigned short) red),q);
1293 SetPixelGreen(image,ScaleShortToQuantum((unsigned short) green),q);
1294 SetPixelBlue(image,ScaleShortToQuantum((unsigned short) blue),q);
1295 SetPixelAlpha(image,OpaqueAlpha,q);
1296 if (image->alpha_trait != UndefinedPixelTrait)
1298 alpha=((pixel & bmp_info.alpha_mask) << shift.alpha) >> 16;
1299 if (quantum_bits.alpha <= 8)
1300 alpha|=((alpha & 0xff00) >> 8);
1301 SetPixelAlpha(image,ScaleShortToQuantum(
1302 (unsigned short) alpha),q);
1304 q+=GetPixelChannels(image);
1306 if (SyncAuthenticPixels(image,exception) == MagickFalse)
1308 offset=(MagickOffsetType) (image->rows-y-1);
1309 if (image->previous == (Image *) NULL)
1311 status=SetImageProgress(image,LoadImageTag,(MagickOffsetType)
1312 (image->rows-y),image->rows);
1313 if (status == MagickFalse)
1322 Convert DirectColor scanline.
1324 bytes_per_line=4*((image->columns*24+31)/32);
1325 for (y=(ssize_t) image->rows-1; y >= 0; y--)
1327 p=pixels+(image->rows-y-1)*bytes_per_line;
1328 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
1329 if (q == (Quantum *) NULL)
1331 for (x=0; x < (ssize_t) image->columns; x++)
1333 SetPixelBlue(image,ScaleCharToQuantum(*p++),q);
1334 SetPixelGreen(image,ScaleCharToQuantum(*p++),q);
1335 SetPixelRed(image,ScaleCharToQuantum(*p++),q);
1336 SetPixelAlpha(image,OpaqueAlpha,q);
1337 q+=GetPixelChannels(image);
1339 if (SyncAuthenticPixels(image,exception) == MagickFalse)
1341 offset=(MagickOffsetType) (image->rows-y-1);
1342 if (image->previous == (Image *) NULL)
1344 status=SetImageProgress(image,LoadImageTag,(MagickOffsetType)
1345 (image->rows-y),image->rows);
1346 if (status == MagickFalse)
1355 Convert bitfield encoded DirectColor scanline.
1357 if ((bmp_info.compression != BI_RGB) &&
1358 (bmp_info.compression != BI_BITFIELDS))
1360 pixel_info=RelinquishVirtualMemory(pixel_info);
1361 ThrowReaderException(CorruptImageError,
1362 "UnrecognizedImageCompression");
1364 bytes_per_line=4*(image->columns);
1365 for (y=(ssize_t) image->rows-1; y >= 0; y--)
1371 p=pixels+(image->rows-y-1)*bytes_per_line;
1372 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
1373 if (q == (Quantum *) NULL)
1375 for (x=0; x < (ssize_t) image->columns; x++)
1377 pixel=(unsigned int) (*p++);
1378 pixel|=((unsigned int) *p++ << 8);
1379 pixel|=((unsigned int) *p++ << 16);
1380 pixel|=((unsigned int) *p++ << 24);
1381 red=((pixel & bmp_info.red_mask) << shift.red) >> 16;
1382 if (quantum_bits.red == 8)
1384 green=((pixel & bmp_info.green_mask) << shift.green) >> 16;
1385 if (quantum_bits.green == 8)
1386 green|=(green >> 8);
1387 blue=((pixel & bmp_info.blue_mask) << shift.blue) >> 16;
1388 if (quantum_bits.blue == 8)
1390 SetPixelRed(image,ScaleShortToQuantum((unsigned short) red),q);
1391 SetPixelGreen(image,ScaleShortToQuantum((unsigned short) green),q);
1392 SetPixelBlue(image,ScaleShortToQuantum((unsigned short) blue),q);
1393 SetPixelAlpha(image,OpaqueAlpha,q);
1394 if (image->alpha_trait != UndefinedPixelTrait)
1396 alpha=((pixel & bmp_info.alpha_mask) << shift.alpha) >> 16;
1397 if (quantum_bits.alpha == 8)
1398 alpha|=(alpha >> 8);
1399 SetPixelAlpha(image,ScaleShortToQuantum(
1400 (unsigned short) alpha),q);
1402 q+=GetPixelChannels(image);
1404 if (SyncAuthenticPixels(image,exception) == MagickFalse)
1406 offset=(MagickOffsetType) (image->rows-y-1);
1407 if (image->previous == (Image *) NULL)
1409 status=SetImageProgress(image,LoadImageTag,(MagickOffsetType)
1410 (image->rows-y),image->rows);
1411 if (status == MagickFalse)
1419 pixel_info=RelinquishVirtualMemory(pixel_info);
1420 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
1423 pixel_info=RelinquishVirtualMemory(pixel_info);
1426 if (EOFBlob(image) != MagickFalse)
1428 ThrowFileException(exception,CorruptImageError,"UnexpectedEndOfFile",
1432 if (bmp_info.height < 0)
1438 Correct image orientation.
1440 flipped_image=FlipImage(image,exception);
1441 if (flipped_image != (Image *) NULL)
1443 DuplicateBlob(flipped_image,image);
1444 ReplaceImageInList(&image, flipped_image);
1445 image=flipped_image;
1449 Proceed to next image.
1451 if (image_info->number_scenes != 0)
1452 if (image->scene >= (image_info->scene+image_info->number_scenes-1))
1455 if (bmp_info.ba_offset != 0)
1457 offset=SeekBlob(image,(MagickOffsetType) bmp_info.ba_offset,SEEK_SET);
1459 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
1461 count=ReadBlob(image,2,magick);
1462 if ((count == 2) && (IsBMP(magick,2) != MagickFalse))
1465 Acquire next image structure.
1467 AcquireNextImage(image_info,image,exception);
1468 if (GetNextImageInList(image) == (Image *) NULL)
1470 image=DestroyImageList(image);
1471 return((Image *) NULL);
1473 image=SyncNextImageInList(image);
1474 status=SetImageProgress(image,LoadImagesTag,TellBlob(image),
1475 GetBlobSize(image));
1476 if (status == MagickFalse)
1479 } while (IsBMP(magick,2) != MagickFalse);
1480 (void) CloseBlob(image);
1481 return(GetFirstImageInList(image));
1485 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1489 % R e g i s t e r B M P I m a g e %
1493 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1495 % RegisterBMPImage() adds attributes for the BMP image format to
1496 % the list of supported formats. The attributes include the image format
1497 % tag, a method to read and/or write the format, whether the format
1498 % supports the saving of more than one frame to the same file or blob,
1499 % whether the format supports native in-memory I/O, and a brief
1500 % description of the format.
1502 % The format of the RegisterBMPImage method is:
1504 % size_t RegisterBMPImage(void)
1507 ModuleExport size_t RegisterBMPImage(void)
1512 entry=AcquireMagickInfo("BMP","BMP","Microsoft Windows bitmap image");
1513 entry->decoder=(DecodeImageHandler *) ReadBMPImage;
1514 entry->encoder=(EncodeImageHandler *) WriteBMPImage;
1515 entry->magick=(IsImageFormatHandler *) IsBMP;
1516 entry->flags^=CoderAdjoinFlag;
1517 entry->flags|=CoderDecoderSeekableStreamFlag;
1518 (void) RegisterMagickInfo(entry);
1519 entry=AcquireMagickInfo("BMP","BMP2","Microsoft Windows bitmap image (V2)");
1520 entry->encoder=(EncodeImageHandler *) WriteBMPImage;
1521 entry->magick=(IsImageFormatHandler *) IsBMP;
1522 entry->flags^=CoderAdjoinFlag;
1523 entry->flags|=CoderDecoderSeekableStreamFlag;
1524 (void) RegisterMagickInfo(entry);
1525 entry=AcquireMagickInfo("BMP","BMP3","Microsoft Windows bitmap image (V3)");
1526 entry->encoder=(EncodeImageHandler *) WriteBMPImage;
1527 entry->magick=(IsImageFormatHandler *) IsBMP;
1528 entry->flags^=CoderAdjoinFlag;
1529 entry->flags|=CoderDecoderSeekableStreamFlag;
1530 (void) RegisterMagickInfo(entry);
1531 return(MagickImageCoderSignature);
1535 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1539 % U n r e g i s t e r B M P I m a g e %
1543 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1545 % UnregisterBMPImage() removes format registrations made by the
1546 % BMP module from the list of supported formats.
1548 % The format of the UnregisterBMPImage method is:
1550 % UnregisterBMPImage(void)
1553 ModuleExport void UnregisterBMPImage(void)
1555 (void) UnregisterMagickInfo("BMP");
1556 (void) UnregisterMagickInfo("BMP2");
1557 (void) UnregisterMagickInfo("BMP3");
1561 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1565 % W r i t e B M P I m a g e %
1569 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1571 % WriteBMPImage() writes an image in Microsoft Windows bitmap encoded
1572 % image format, version 3 for Windows or (if the image has a matte channel)
1575 % The format of the WriteBMPImage method is:
1577 % MagickBooleanType WriteBMPImage(const ImageInfo *image_info,
1578 % Image *image,ExceptionInfo *exception)
1580 % A description of each parameter follows.
1582 % o image_info: the image info.
1584 % o image: The image.
1586 % o exception: return any errors or warnings in this structure.
1589 static MagickBooleanType WriteBMPImage(const ImageInfo *image_info,Image *image,
1590 ExceptionInfo *exception)
1614 register const Quantum
1621 register unsigned char
1636 Open output image file.
1638 assert(image_info != (const ImageInfo *) NULL);
1639 assert(image_info->signature == MagickCoreSignature);
1640 assert(image != (Image *) NULL);
1641 assert(image->signature == MagickCoreSignature);
1642 if (image->debug != MagickFalse)
1643 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1644 assert(exception != (ExceptionInfo *) NULL);
1645 assert(exception->signature == MagickCoreSignature);
1646 status=OpenBlob(image_info,image,WriteBinaryBlobMode,exception);
1647 if (status == MagickFalse)
1650 if (LocaleCompare(image_info->magick,"BMP2") == 0)
1653 if (LocaleCompare(image_info->magick,"BMP3") == 0)
1656 option=GetImageOption(image_info,"bmp:format");
1657 if (option != (char *) NULL)
1659 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1660 " Format=%s",option);
1662 if (LocaleCompare(option,"bmp2") == 0)
1664 if (LocaleCompare(option,"bmp3") == 0)
1666 if (LocaleCompare(option,"bmp4") == 0)
1674 Initialize BMP raster file header.
1676 if (IssRGBCompatibleColorspace(image->colorspace) == MagickFalse)
1677 (void) TransformImageColorspace(image,sRGBColorspace,exception);
1678 (void) ResetMagickMemory(&bmp_info,0,sizeof(bmp_info));
1679 bmp_info.file_size=14+12;
1681 bmp_info.file_size+=28;
1682 bmp_info.offset_bits=bmp_info.file_size;
1683 bmp_info.compression=BI_RGB;
1684 bmp_info.red_mask=0x00ff0000U;
1685 bmp_info.green_mask=0x0000ff00U;
1686 bmp_info.blue_mask=0x000000ffU;
1687 bmp_info.alpha_mask=0xff000000U;
1688 bmp_subtype=UndefinedSubtype;
1689 if ((image->storage_class == PseudoClass) && (image->colors > 256))
1690 (void) SetImageStorageClass(image,DirectClass,exception);
1691 if (image->storage_class != DirectClass)
1694 Colormapped BMP raster.
1696 bmp_info.bits_per_pixel=8;
1697 if (image->colors <= 2)
1698 bmp_info.bits_per_pixel=1;
1700 if (image->colors <= 16)
1701 bmp_info.bits_per_pixel=4;
1703 if (image->colors <= 256)
1704 bmp_info.bits_per_pixel=8;
1705 if (image_info->compression == RLECompression)
1706 bmp_info.bits_per_pixel=8;
1707 bmp_info.number_colors=1U << bmp_info.bits_per_pixel;
1708 if (image->alpha_trait != UndefinedPixelTrait)
1709 (void) SetImageStorageClass(image,DirectClass,exception);
1711 if ((size_t) bmp_info.number_colors < image->colors)
1712 (void) SetImageStorageClass(image,DirectClass,exception);
1715 bmp_info.file_size+=3*(1UL << bmp_info.bits_per_pixel);
1716 bmp_info.offset_bits+=3*(1UL << bmp_info.bits_per_pixel);
1719 bmp_info.file_size+=(1UL << bmp_info.bits_per_pixel);
1720 bmp_info.offset_bits+=(1UL << bmp_info.bits_per_pixel);
1724 if (image->storage_class == DirectClass)
1727 Full color BMP raster.
1729 bmp_info.number_colors=0;
1730 option=GetImageOption(image_info,"bmp:subtype");
1731 if (option != (const char *) NULL)
1733 if (image->alpha_trait != UndefinedPixelTrait)
1735 if (LocaleNCompare(option,"ARGB4444",8) == 0)
1737 bmp_subtype=ARGB4444;
1738 bmp_info.red_mask=0x00000f00U;
1739 bmp_info.green_mask=0x000000f0U;
1740 bmp_info.blue_mask=0x0000000fU;
1741 bmp_info.alpha_mask=0x0000f000U;
1743 else if (LocaleNCompare(option,"ARGB1555",8) == 0)
1745 bmp_subtype=ARGB1555;
1746 bmp_info.red_mask=0x00007c00U;
1747 bmp_info.green_mask=0x000003e0U;
1748 bmp_info.blue_mask=0x0000001fU;
1749 bmp_info.alpha_mask=0x00008000U;
1754 if (LocaleNCompare(option,"RGB555",6) == 0)
1757 bmp_info.red_mask=0x00007c00U;
1758 bmp_info.green_mask=0x000003e0U;
1759 bmp_info.blue_mask=0x0000001fU;
1760 bmp_info.alpha_mask=0U;
1762 else if (LocaleNCompare(option,"RGB565",6) == 0)
1765 bmp_info.red_mask=0x0000f800U;
1766 bmp_info.green_mask=0x000007e0U;
1767 bmp_info.blue_mask=0x0000001fU;
1768 bmp_info.alpha_mask=0U;
1772 if (bmp_subtype != UndefinedSubtype)
1774 bmp_info.bits_per_pixel=16;
1775 bmp_info.compression=BI_BITFIELDS;
1779 bmp_info.bits_per_pixel=(unsigned short) ((type > 3) &&
1780 (image->alpha_trait != UndefinedPixelTrait) ? 32 : 24);
1781 bmp_info.compression=(unsigned int) ((type > 3) &&
1782 (image->alpha_trait != UndefinedPixelTrait) ? BI_BITFIELDS : BI_RGB);
1783 if ((type == 3) && (image->alpha_trait != UndefinedPixelTrait))
1785 option=GetImageOption(image_info,"bmp3:alpha");
1786 if (IsStringTrue(option))
1787 bmp_info.bits_per_pixel=32;
1791 bytes_per_line=4*((image->columns*bmp_info.bits_per_pixel+31)/32);
1792 bmp_info.ba_offset=0;
1793 profile=GetImageProfile(image,"icc");
1794 have_color_info=(image->rendering_intent != UndefinedIntent) ||
1795 (profile != (StringInfo *) NULL) || (image->gamma != 0.0) ? MagickTrue :
1800 if ((type == 3) || ((image->alpha_trait == UndefinedPixelTrait) &&
1801 (have_color_info == MagickFalse)))
1813 if ((image->rendering_intent != UndefinedIntent) ||
1814 (profile != (StringInfo *) NULL))
1819 bmp_info.file_size+=extra_size;
1820 bmp_info.offset_bits+=extra_size;
1822 if (((ssize_t) image->columns != (ssize_t) ((signed int) image->columns)) ||
1823 ((ssize_t) image->rows != (ssize_t) ((signed int) image->rows)))
1824 ThrowWriterException(ImageError,"WidthOrHeightExceedsLimit");
1825 bmp_info.width=(ssize_t) image->columns;
1826 bmp_info.height=(ssize_t) image->rows;
1828 bmp_info.image_size=(unsigned int) (bytes_per_line*image->rows);
1829 bmp_info.file_size+=bmp_info.image_size;
1830 bmp_info.x_pixels=75*39;
1831 bmp_info.y_pixels=75*39;
1832 switch (image->units)
1834 case UndefinedResolution:
1835 case PixelsPerInchResolution:
1837 bmp_info.x_pixels=(unsigned int) (100.0*image->resolution.x/2.54);
1838 bmp_info.y_pixels=(unsigned int) (100.0*image->resolution.y/2.54);
1841 case PixelsPerCentimeterResolution:
1843 bmp_info.x_pixels=(unsigned int) (100.0*image->resolution.x);
1844 bmp_info.y_pixels=(unsigned int) (100.0*image->resolution.y);
1848 bmp_info.colors_important=bmp_info.number_colors;
1850 Convert MIFF to BMP raster pixels.
1852 pixel_info=AcquireVirtualMemory((size_t) bmp_info.image_size,
1854 if (pixel_info == (MemoryInfo *) NULL)
1855 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
1856 pixels=(unsigned char *) GetVirtualMemoryBlob(pixel_info);
1857 (void) ResetMagickMemory(pixels,0,(size_t) bmp_info.image_size);
1858 switch (bmp_info.bits_per_pixel)
1867 Convert PseudoClass image to a BMP monochrome image.
1869 for (y=0; y < (ssize_t) image->rows; y++)
1874 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
1875 if (p == (const Quantum *) NULL)
1877 q=pixels+(image->rows-y-1)*bytes_per_line;
1880 for (x=0; x < (ssize_t) image->columns; x++)
1883 byte|=GetPixelIndex(image,p) != 0 ? 0x01 : 0x00;
1887 *q++=(unsigned char) byte;
1891 p+=GetPixelChannels(image);
1895 *q++=(unsigned char) (byte << (8-bit));
1898 offset=(ssize_t) (image->columns+7)/8;
1899 for (x=offset; x < (ssize_t) bytes_per_line; x++)
1901 if (image->previous == (Image *) NULL)
1903 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
1905 if (status == MagickFalse)
1921 Convert PseudoClass image to a BMP monochrome image.
1923 for (y=0; y < (ssize_t) image->rows; y++)
1925 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
1926 if (p == (const Quantum *) NULL)
1928 q=pixels+(image->rows-y-1)*bytes_per_line;
1931 for (x=0; x < (ssize_t) image->columns; x++)
1934 byte|=((unsigned int) GetPixelIndex(image,p) & 0x0f);
1938 *q++=(unsigned char) byte;
1942 p+=GetPixelChannels(image);
1946 *q++=(unsigned char) (byte << 4);
1949 offset=(ssize_t) (image->columns+1)/2;
1950 for (x=offset; x < (ssize_t) bytes_per_line; x++)
1952 if (image->previous == (Image *) NULL)
1954 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
1956 if (status == MagickFalse)
1965 Convert PseudoClass packet to BMP pixel.
1967 for (y=0; y < (ssize_t) image->rows; y++)
1969 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
1970 if (p == (const Quantum *) NULL)
1972 q=pixels+(image->rows-y-1)*bytes_per_line;
1973 for (x=0; x < (ssize_t) image->columns; x++)
1975 *q++=(unsigned char) GetPixelIndex(image,p);
1976 p+=GetPixelChannels(image);
1978 for ( ; x < (ssize_t) bytes_per_line; x++)
1980 if (image->previous == (Image *) NULL)
1982 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
1984 if (status == MagickFalse)
1993 Convert DirectClass packet to BMP BGR888.
1995 for (y=0; y < (ssize_t) image->rows; y++)
1997 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
1998 if (p == (const Quantum *) NULL)
2000 q=pixels+(image->rows-y-1)*bytes_per_line;
2001 for (x=0; x < (ssize_t) image->columns; x++)
2007 if (bmp_subtype == ARGB4444)
2009 pixel=(unsigned short) (ScaleQuantumToAny(
2010 GetPixelAlpha(image,p),15) << 12);
2011 pixel|=(unsigned short) (ScaleQuantumToAny(
2012 GetPixelRed(image,p),15) << 8);
2013 pixel|=(unsigned short) (ScaleQuantumToAny(
2014 GetPixelGreen(image,p),15) << 4);
2015 pixel|=(unsigned short) (ScaleQuantumToAny(
2016 GetPixelBlue(image,p),15));
2018 else if (bmp_subtype == RGB565)
2020 pixel=(unsigned short) (ScaleQuantumToAny(
2021 GetPixelRed(image,p),31) << 11);
2022 pixel|=(unsigned short) (ScaleQuantumToAny(
2023 GetPixelGreen(image,p),63) << 5);
2024 pixel|=(unsigned short) (ScaleQuantumToAny(
2025 GetPixelBlue(image,p),31));
2029 if (bmp_subtype == ARGB1555)
2030 pixel=(unsigned short) (ScaleQuantumToAny(
2031 GetPixelAlpha(image,p),1) << 15);
2032 pixel|=(unsigned short) (ScaleQuantumToAny(
2033 GetPixelRed(image,p),31) << 10);
2034 pixel|=(unsigned short) (ScaleQuantumToAny(
2035 GetPixelGreen(image,p),31) << 5);
2036 pixel|=(unsigned short) (ScaleQuantumToAny(
2037 GetPixelBlue(image,p),31));
2039 *((unsigned short *) q)=pixel;
2041 p+=GetPixelChannels(image);
2043 for (x=2L*(ssize_t) image->columns; x < (ssize_t) bytes_per_line; x++)
2045 if (image->previous == (Image *) NULL)
2047 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
2049 if (status == MagickFalse)
2058 Convert DirectClass packet to BMP BGR888.
2060 for (y=0; y < (ssize_t) image->rows; y++)
2062 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
2063 if (p == (const Quantum *) NULL)
2065 q=pixels+(image->rows-y-1)*bytes_per_line;
2066 for (x=0; x < (ssize_t) image->columns; x++)
2068 *q++=ScaleQuantumToChar(GetPixelBlue(image,p));
2069 *q++=ScaleQuantumToChar(GetPixelGreen(image,p));
2070 *q++=ScaleQuantumToChar(GetPixelRed(image,p));
2071 p+=GetPixelChannels(image);
2073 for (x=3L*(ssize_t) image->columns; x < (ssize_t) bytes_per_line; x++)
2075 if (image->previous == (Image *) NULL)
2077 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
2079 if (status == MagickFalse)
2088 Convert DirectClass packet to ARGB8888 pixel.
2090 for (y=0; y < (ssize_t) image->rows; y++)
2092 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
2093 if (p == (const Quantum *) NULL)
2095 q=pixels+(image->rows-y-1)*bytes_per_line;
2096 for (x=0; x < (ssize_t) image->columns; x++)
2098 *q++=ScaleQuantumToChar(GetPixelBlue(image,p));
2099 *q++=ScaleQuantumToChar(GetPixelGreen(image,p));
2100 *q++=ScaleQuantumToChar(GetPixelRed(image,p));
2101 *q++=ScaleQuantumToChar(GetPixelAlpha(image,p));
2102 p+=GetPixelChannels(image);
2104 if (image->previous == (Image *) NULL)
2106 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
2108 if (status == MagickFalse)
2115 if ((type > 2) && (bmp_info.bits_per_pixel == 8))
2116 if (image_info->compression != NoCompression)
2122 Convert run-length encoded raster pixels.
2124 rle_info=AcquireVirtualMemory((size_t) (2*(bytes_per_line+2)+2),
2125 (image->rows+2)*sizeof(*pixels));
2126 if (rle_info == (MemoryInfo *) NULL)
2128 pixel_info=RelinquishVirtualMemory(pixel_info);
2129 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
2131 bmp_data=(unsigned char *) GetVirtualMemoryBlob(rle_info);
2132 bmp_info.file_size-=bmp_info.image_size;
2133 bmp_info.image_size=(unsigned int) EncodeImage(image,bytes_per_line,
2135 bmp_info.file_size+=bmp_info.image_size;
2136 pixel_info=RelinquishVirtualMemory(pixel_info);
2137 pixel_info=rle_info;
2139 bmp_info.compression=BI_RLE8;
2142 Write BMP for Windows, all versions, 14-byte header.
2144 if (image->debug != MagickFalse)
2146 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
2147 " Writing BMP version %.20g datastream",(double) type);
2148 if (image->storage_class == DirectClass)
2149 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
2150 " Storage class=DirectClass");
2152 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
2153 " Storage class=PseudoClass");
2154 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
2155 " Image depth=%.20g",(double) image->depth);
2156 if (image->alpha_trait != UndefinedPixelTrait)
2157 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
2160 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
2161 " Matte=MagickFalse");
2162 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
2163 " BMP bits_per_pixel=%.20g",(double) bmp_info.bits_per_pixel);
2164 switch ((int) bmp_info.compression)
2168 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
2169 " Compression=BI_RGB");
2174 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
2175 " Compression=BI_RLE8");
2180 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
2181 " Compression=BI_BITFIELDS");
2186 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
2187 " Compression=UNKNOWN (%u)",bmp_info.compression);
2191 if (bmp_info.number_colors == 0)
2192 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
2193 " Number_colors=unspecified");
2195 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
2196 " Number_colors=%u",bmp_info.number_colors);
2198 (void) WriteBlob(image,2,(unsigned char *) "BM");
2199 (void) WriteBlobLSBLong(image,bmp_info.file_size);
2200 (void) WriteBlobLSBLong(image,bmp_info.ba_offset); /* always 0 */
2201 (void) WriteBlobLSBLong(image,bmp_info.offset_bits);
2205 Write 12-byte version 2 bitmap header.
2207 (void) WriteBlobLSBLong(image,bmp_info.size);
2208 (void) WriteBlobLSBSignedShort(image,(signed short) bmp_info.width);
2209 (void) WriteBlobLSBSignedShort(image,(signed short) bmp_info.height);
2210 (void) WriteBlobLSBShort(image,bmp_info.planes);
2211 (void) WriteBlobLSBShort(image,bmp_info.bits_per_pixel);
2216 Write 40-byte version 3+ bitmap header.
2218 (void) WriteBlobLSBLong(image,bmp_info.size);
2219 (void) WriteBlobLSBSignedLong(image,(signed int) bmp_info.width);
2220 (void) WriteBlobLSBSignedLong(image,(signed int) bmp_info.height);
2221 (void) WriteBlobLSBShort(image,bmp_info.planes);
2222 (void) WriteBlobLSBShort(image,bmp_info.bits_per_pixel);
2223 (void) WriteBlobLSBLong(image,bmp_info.compression);
2224 (void) WriteBlobLSBLong(image,bmp_info.image_size);
2225 (void) WriteBlobLSBLong(image,bmp_info.x_pixels);
2226 (void) WriteBlobLSBLong(image,bmp_info.y_pixels);
2227 (void) WriteBlobLSBLong(image,bmp_info.number_colors);
2228 (void) WriteBlobLSBLong(image,bmp_info.colors_important);
2230 if ((type > 3) && ((image->alpha_trait != UndefinedPixelTrait) ||
2231 (have_color_info != MagickFalse)))
2234 Write the rest of the 108-byte BMP Version 4 header.
2236 (void) WriteBlobLSBLong(image,bmp_info.red_mask);
2237 (void) WriteBlobLSBLong(image,bmp_info.green_mask);
2238 (void) WriteBlobLSBLong(image,bmp_info.blue_mask);
2239 (void) WriteBlobLSBLong(image,bmp_info.alpha_mask);
2240 (void) WriteBlobLSBLong(image,0x73524742U); /* sRGB */
2241 (void) WriteBlobLSBLong(image,(unsigned int)
2242 (image->chromaticity.red_primary.x*0x40000000));
2243 (void) WriteBlobLSBLong(image,(unsigned int)
2244 (image->chromaticity.red_primary.y*0x40000000));
2245 (void) WriteBlobLSBLong(image,(unsigned int)
2246 ((1.000f-(image->chromaticity.red_primary.x+
2247 image->chromaticity.red_primary.y))*0x40000000));
2248 (void) WriteBlobLSBLong(image,(unsigned int)
2249 (image->chromaticity.green_primary.x*0x40000000));
2250 (void) WriteBlobLSBLong(image,(unsigned int)
2251 (image->chromaticity.green_primary.y*0x40000000));
2252 (void) WriteBlobLSBLong(image,(unsigned int)
2253 ((1.000f-(image->chromaticity.green_primary.x+
2254 image->chromaticity.green_primary.y))*0x40000000));
2255 (void) WriteBlobLSBLong(image,(unsigned int)
2256 (image->chromaticity.blue_primary.x*0x40000000));
2257 (void) WriteBlobLSBLong(image,(unsigned int)
2258 (image->chromaticity.blue_primary.y*0x40000000));
2259 (void) WriteBlobLSBLong(image,(unsigned int)
2260 ((1.000f-(image->chromaticity.blue_primary.x+
2261 image->chromaticity.blue_primary.y))*0x40000000));
2262 (void) WriteBlobLSBLong(image,(unsigned int)
2263 (bmp_info.gamma_scale.x*0x10000));
2264 (void) WriteBlobLSBLong(image,(unsigned int)
2265 (bmp_info.gamma_scale.y*0x10000));
2266 (void) WriteBlobLSBLong(image,(unsigned int)
2267 (bmp_info.gamma_scale.z*0x10000));
2268 if ((image->rendering_intent != UndefinedIntent) ||
2269 (profile != (StringInfo *) NULL))
2274 switch ((int) image->rendering_intent)
2276 case SaturationIntent:
2278 intent=LCS_GM_BUSINESS;
2281 case RelativeIntent:
2283 intent=LCS_GM_GRAPHICS;
2286 case PerceptualIntent:
2288 intent=LCS_GM_IMAGES;
2291 case AbsoluteIntent:
2293 intent=LCS_GM_ABS_COLORIMETRIC;
2302 (void) WriteBlobLSBLong(image,(unsigned int) intent);
2303 (void) WriteBlobLSBLong(image,0x00); /* dummy profile data */
2304 (void) WriteBlobLSBLong(image,0x00); /* dummy profile length */
2305 (void) WriteBlobLSBLong(image,0x00); /* reserved */
2308 if (image->storage_class == PseudoClass)
2314 Dump colormap to file.
2316 if (image->debug != MagickFalse)
2317 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
2318 " Colormap: %.20g entries",(double) image->colors);
2319 bmp_colormap=(unsigned char *) AcquireQuantumMemory((size_t) (1UL <<
2320 bmp_info.bits_per_pixel),4*sizeof(*bmp_colormap));
2321 if (bmp_colormap == (unsigned char *) NULL)
2322 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
2324 for (i=0; i < (ssize_t) MagickMin((ssize_t) image->colors,(ssize_t) bmp_info.number_colors); i++)
2326 *q++=ScaleQuantumToChar(ClampToQuantum(image->colormap[i].blue));
2327 *q++=ScaleQuantumToChar(ClampToQuantum(image->colormap[i].green));
2328 *q++=ScaleQuantumToChar(ClampToQuantum(image->colormap[i].red));
2330 *q++=(unsigned char) 0x0;
2332 for ( ; i < (ssize_t) (1UL << bmp_info.bits_per_pixel); i++)
2334 *q++=(unsigned char) 0x00;
2335 *q++=(unsigned char) 0x00;
2336 *q++=(unsigned char) 0x00;
2338 *q++=(unsigned char) 0x00;
2341 (void) WriteBlob(image,(size_t) (3*(1L << bmp_info.bits_per_pixel)),
2344 (void) WriteBlob(image,(size_t) (4*(1L << bmp_info.bits_per_pixel)),
2346 bmp_colormap=(unsigned char *) RelinquishMagickMemory(bmp_colormap);
2348 if (image->debug != MagickFalse)
2349 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
2350 " Pixels: %u bytes",bmp_info.image_size);
2351 (void) WriteBlob(image,(size_t) bmp_info.image_size,pixels);
2352 pixel_info=RelinquishVirtualMemory(pixel_info);
2353 if (GetNextImageInList(image) == (Image *) NULL)
2355 image=SyncNextImageInList(image);
2356 status=SetImageProgress(image,SaveImagesTag,scene++,
2357 GetImageListLength(image));
2358 if (status == MagickFalse)
2360 } while (image_info->adjoin != MagickFalse);
2361 (void) CloseBlob(image);