2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
13 % Read/Write Microsoft Windows Bitmap Image Format %
17 % Glenn Randers-Pehrson %
21 % Copyright 1999-2019 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://imagemagick.org/script/license.php %
29 % Unless required by applicable law or agreed to in writing, software %
30 % distributed under the License is distributed on an "AS IS" BASIS, %
31 % WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. %
32 % See the License for the specific language governing permissions and %
33 % limitations under the License. %
35 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
43 #include "MagickCore/studio.h"
44 #include "MagickCore/blob.h"
45 #include "MagickCore/blob-private.h"
46 #include "MagickCore/cache.h"
47 #include "MagickCore/colormap-private.h"
48 #include "MagickCore/color-private.h"
49 #include "MagickCore/colormap.h"
50 #include "MagickCore/colorspace.h"
51 #include "MagickCore/colorspace-private.h"
52 #include "MagickCore/exception.h"
53 #include "MagickCore/exception-private.h"
54 #include "MagickCore/image.h"
55 #include "MagickCore/image-private.h"
56 #include "MagickCore/list.h"
57 #include "MagickCore/log.h"
58 #include "MagickCore/magick.h"
59 #include "MagickCore/memory_.h"
60 #include "MagickCore/monitor.h"
61 #include "MagickCore/monitor-private.h"
62 #include "MagickCore/option.h"
63 #include "MagickCore/pixel-accessor.h"
64 #include "MagickCore/profile.h"
65 #include "MagickCore/quantum-private.h"
66 #include "MagickCore/static.h"
67 #include "MagickCore/string_.h"
68 #include "MagickCore/module.h"
69 #include "MagickCore/transform.h"
72 Macro definitions (from Windows wingdi.h).
78 #if !defined(MAGICKCORE_WINDOWS_SUPPORT) || defined(__MINGW32__)
86 #define BI_BITFIELDS 3
88 #undef LCS_CALIBRATED_RBG
89 #define LCS_CALIBRATED_RBG 0
92 #undef LCS_WINDOWS_COLOR_SPACE
93 #define LCS_WINDOWS_COLOR_SPACE 2
95 #define PROFILE_LINKED 3
96 #undef PROFILE_EMBEDDED
97 #define PROFILE_EMBEDDED 4
99 #undef LCS_GM_BUSINESS
100 #define LCS_GM_BUSINESS 1 /* Saturation */
101 #undef LCS_GM_GRAPHICS
102 #define LCS_GM_GRAPHICS 2 /* Relative */
104 #define LCS_GM_IMAGES 4 /* Perceptual */
105 #undef LCS_GM_ABS_COLORIMETRIC
106 #define LCS_GM_ABS_COLORIMETRIC 8 /* Absolute */
110 Enumerated declaractions.
122 Typedef declarations.
124 typedef struct _BMPInfo
163 Forward declarations.
165 static MagickBooleanType
166 WriteBMPImage(const ImageInfo *,Image *,ExceptionInfo *);
169 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
173 % D e c o d e I m a g e %
177 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
179 % DecodeImage unpacks the packed image pixels into runlength-encoded
182 % The format of the DecodeImage method is:
184 % MagickBooleanType DecodeImage(Image *image,const size_t compression,
185 % unsigned char *pixels,const size_t number_pixels)
187 % A description of each parameter follows:
189 % o image: the address of a structure of type Image.
191 % o compression: Zero means uncompressed. A value of 1 means the
192 % compressed pixels are runlength encoded for a 256-color bitmap.
193 % A value of 2 means a 16-color bitmap. A value of 3 means bitfields
196 % o pixels: The address of a byte (8 bits) array of pixel data created by
197 % the decoding process.
199 % o number_pixels: The number of pixels.
202 static MagickBooleanType DecodeImage(Image *image,const size_t compression,
203 unsigned char *pixels,const size_t number_pixels)
213 register unsigned char
220 assert(image != (Image *) NULL);
221 assert(image->signature == MagickCoreSignature);
222 if (image->debug != MagickFalse)
223 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
224 assert(pixels != (unsigned char *) NULL);
225 (void) memset(pixels,0,number_pixels*sizeof(*pixels));
229 q=pixels+number_pixels;
230 for (y=0; y < (ssize_t) image->rows; )
235 if ((p < pixels) || (p > q))
237 count=ReadBlobByte(image);
245 count=(int) MagickMin((ssize_t) count,(ssize_t) (q-p));
246 byte=ReadBlobByte(image);
249 if (compression == BI_RLE8)
251 for (i=0; i < (ssize_t) count; i++)
252 *p++=(unsigned char) byte;
256 for (i=0; i < (ssize_t) count; i++)
258 ((i & 0x01) != 0 ? (byte & 0x0f) : ((byte >> 4) & 0x0f));
267 count=ReadBlobByte(image);
281 p=pixels+y*image->columns;
289 x+=ReadBlobByte(image);
290 y+=ReadBlobByte(image);
291 p=pixels+y*image->columns+x;
299 count=(int) MagickMin((ssize_t) count,(ssize_t) (q-p));
300 if (compression == BI_RLE8)
301 for (i=0; i < (ssize_t) count; i++)
303 byte=ReadBlobByte(image);
306 *p++=(unsigned char) byte;
309 for (i=0; i < (ssize_t) count; i++)
313 byte=ReadBlobByte(image);
318 ((i & 0x01) != 0 ? (byte & 0x0f) : ((byte >> 4) & 0x0f));
324 if (compression == BI_RLE8)
326 if ((count & 0x01) != 0)
327 if (ReadBlobByte(image) == EOF)
331 if (((count & 0x03) == 1) || ((count & 0x03) == 2))
332 if (ReadBlobByte(image) == EOF)
338 status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y,
340 if (status == MagickFalse)
343 (void) ReadBlobByte(image); /* end of line */
344 (void) ReadBlobByte(image);
345 return(y < (ssize_t) image->rows ? MagickFalse : MagickTrue);
349 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
353 % E n c o d e I m a g e %
357 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
359 % EncodeImage compresses pixels using a runlength encoded format.
361 % The format of the EncodeImage method is:
363 % static MagickBooleanType EncodeImage(Image *image,
364 % const size_t bytes_per_line,const unsigned char *pixels,
365 % unsigned char *compressed_pixels)
367 % A description of each parameter follows:
369 % o image: The image.
371 % o bytes_per_line: the number of bytes in a scanline of compressed pixels
373 % o pixels: The address of a byte (8 bits) array of pixel data created by
374 % the compression process.
376 % o compressed_pixels: The address of a byte (8 bits) array of compressed
380 static size_t EncodeImage(Image *image,const size_t bytes_per_line,
381 const unsigned char *pixels,unsigned char *compressed_pixels)
386 register const unsigned char
393 register unsigned char
400 Runlength encode pixels.
402 assert(image != (Image *) NULL);
403 assert(image->signature == MagickCoreSignature);
404 if (image->debug != MagickFalse)
405 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
406 assert(pixels != (const unsigned char *) NULL);
407 assert(compressed_pixels != (unsigned char *) NULL);
411 for (y=0; y < (ssize_t) image->rows; y++)
413 for (x=0; x < (ssize_t) bytes_per_line; x+=i)
418 for (i=1; ((x+i) < (ssize_t) bytes_per_line); i++)
419 if ((i == 255) || (*(p+i) != *p))
421 *q++=(unsigned char) i;
428 *q++=(unsigned char) 0x00;
429 *q++=(unsigned char) 0x00;
430 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
432 if (status == MagickFalse)
438 *q++=(unsigned char) 0x00;
439 *q++=(unsigned char) 0x01;
440 return((size_t) (q-compressed_pixels));
444 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
452 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
454 % IsBMP() returns MagickTrue if the image format type, identified by the
455 % magick string, is BMP.
457 % The format of the IsBMP method is:
459 % MagickBooleanType IsBMP(const unsigned char *magick,const size_t length)
461 % A description of each parameter follows:
463 % o magick: compare image format pattern against these bytes.
465 % o length: Specifies the length of the magick string.
468 static MagickBooleanType IsBMP(const unsigned char *magick,const size_t length)
472 if ((LocaleNCompare((char *) magick,"BA",2) == 0) ||
473 (LocaleNCompare((char *) magick,"BM",2) == 0) ||
474 (LocaleNCompare((char *) magick,"IC",2) == 0) ||
475 (LocaleNCompare((char *) magick,"PI",2) == 0) ||
476 (LocaleNCompare((char *) magick,"CI",2) == 0) ||
477 (LocaleNCompare((char *) magick,"CP",2) == 0))
483 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
487 % R e a d B M P I m a g e %
491 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
493 % ReadBMPImage() reads a Microsoft Windows bitmap image file, Version
494 % 2, 3 (for Windows or NT), or 4, and returns it. It allocates the memory
495 % necessary for the new Image structure and returns a pointer to the new
498 % The format of the ReadBMPImage method is:
500 % image=ReadBMPImage(image_info)
502 % A description of each parameter follows:
504 % o image_info: the image info.
506 % o exception: return any errors or warnings in this structure.
510 static Image *ReadBMPImage(const ImageInfo *image_info,ExceptionInfo *exception)
543 register unsigned char
568 assert(image_info != (const ImageInfo *) NULL);
569 assert(image_info->signature == MagickCoreSignature);
570 if (image_info->debug != MagickFalse)
571 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
572 image_info->filename);
573 assert(exception != (ExceptionInfo *) NULL);
574 assert(exception->signature == MagickCoreSignature);
575 image=AcquireImage(image_info,exception);
576 status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception);
577 if (status == MagickFalse)
579 image=DestroyImageList(image);
580 return((Image *) NULL);
583 Determine if this a BMP file.
585 (void) memset(&bmp_info,0,sizeof(bmp_info));
586 bmp_info.ba_offset=0;
589 count=ReadBlob(image,2,magick);
591 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
592 blob_size=GetBlobSize(image);
602 Verify BMP identifier.
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: %u",
630 if (bmp_info.size == 12)
635 (void) CopyMagickString(image->magick,"BMP2",MagickPathExtent);
636 bmp_info.width=(ssize_t) ((short) ReadBlobLSBShort(image));
637 bmp_info.height=(ssize_t) ((short) ReadBlobLSBShort(image));
638 bmp_info.planes=ReadBlobLSBShort(image);
639 bmp_info.bits_per_pixel=ReadBlobLSBShort(image);
642 bmp_info.number_colors=0;
643 bmp_info.compression=BI_RGB;
644 bmp_info.image_size=0;
645 bmp_info.alpha_mask=0;
646 if (image->debug != MagickFalse)
648 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
649 " Format: OS/2 Bitmap");
650 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
651 " Geometry: %.20gx%.20g",(double) bmp_info.width,(double)
658 Microsoft Windows BMP image file.
660 if (bmp_info.size < 40)
661 ThrowReaderException(CorruptImageError,"NonOS2HeaderSizeError");
662 bmp_info.width=(ssize_t) ReadBlobLSBSignedLong(image);
663 bmp_info.height=(ssize_t) ReadBlobLSBSignedLong(image);
664 bmp_info.planes=ReadBlobLSBShort(image);
665 bmp_info.bits_per_pixel=ReadBlobLSBShort(image);
666 bmp_info.compression=ReadBlobLSBLong(image);
667 bmp_info.image_size=ReadBlobLSBLong(image);
668 bmp_info.x_pixels=ReadBlobLSBLong(image);
669 bmp_info.y_pixels=ReadBlobLSBLong(image);
670 bmp_info.number_colors=ReadBlobLSBLong(image);
671 if ((MagickSizeType) bmp_info.number_colors > blob_size)
672 ThrowReaderException(CorruptImageError,"InsufficientImageDataInFile");
673 bmp_info.colors_important=ReadBlobLSBLong(image);
674 if (image->debug != MagickFalse)
676 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
677 " Format: MS Windows bitmap");
678 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
679 " Geometry: %.20gx%.20g",(double) bmp_info.width,(double)
681 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
682 " Bits per pixel: %.20g",(double) bmp_info.bits_per_pixel);
683 switch (bmp_info.compression)
687 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
688 " Compression: BI_RGB");
693 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
694 " Compression: BI_RLE4");
699 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
700 " Compression: BI_RLE8");
705 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
706 " Compression: BI_BITFIELDS");
711 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
712 " Compression: BI_PNG");
717 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
718 " Compression: BI_JPEG");
723 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
724 " Compression: UNKNOWN (%u)",bmp_info.compression);
727 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
728 " Number of colors: %u",bmp_info.number_colors);
730 if ((bmp_info.size > 40) || (bmp_info.compression == BI_BITFIELDS))
732 bmp_info.red_mask=ReadBlobLSBLong(image);
733 bmp_info.green_mask=ReadBlobLSBLong(image);
734 bmp_info.blue_mask=ReadBlobLSBLong(image);
736 if (bmp_info.size > 40)
742 Read color management information.
744 bmp_info.alpha_mask=ReadBlobLSBLong(image);
745 bmp_info.colorspace=ReadBlobLSBSignedLong(image);
747 Decode 2^30 fixed point formatted CIE primaries.
749 # define BMP_DENOM ((double) 0x40000000)
750 bmp_info.red_primary.x=(double) ReadBlobLSBLong(image)/BMP_DENOM;
751 bmp_info.red_primary.y=(double) ReadBlobLSBLong(image)/BMP_DENOM;
752 bmp_info.red_primary.z=(double) ReadBlobLSBLong(image)/BMP_DENOM;
753 bmp_info.green_primary.x=(double) ReadBlobLSBLong(image)/BMP_DENOM;
754 bmp_info.green_primary.y=(double) ReadBlobLSBLong(image)/BMP_DENOM;
755 bmp_info.green_primary.z=(double) ReadBlobLSBLong(image)/BMP_DENOM;
756 bmp_info.blue_primary.x=(double) ReadBlobLSBLong(image)/BMP_DENOM;
757 bmp_info.blue_primary.y=(double) ReadBlobLSBLong(image)/BMP_DENOM;
758 bmp_info.blue_primary.z=(double) ReadBlobLSBLong(image)/BMP_DENOM;
760 gamma=bmp_info.red_primary.x+bmp_info.red_primary.y+
761 bmp_info.red_primary.z;
762 gamma=PerceptibleReciprocal(gamma);
763 bmp_info.red_primary.x*=gamma;
764 bmp_info.red_primary.y*=gamma;
765 image->chromaticity.red_primary.x=bmp_info.red_primary.x;
766 image->chromaticity.red_primary.y=bmp_info.red_primary.y;
768 gamma=bmp_info.green_primary.x+bmp_info.green_primary.y+
769 bmp_info.green_primary.z;
770 gamma=PerceptibleReciprocal(gamma);
771 bmp_info.green_primary.x*=gamma;
772 bmp_info.green_primary.y*=gamma;
773 image->chromaticity.green_primary.x=bmp_info.green_primary.x;
774 image->chromaticity.green_primary.y=bmp_info.green_primary.y;
776 gamma=bmp_info.blue_primary.x+bmp_info.blue_primary.y+
777 bmp_info.blue_primary.z;
778 gamma=PerceptibleReciprocal(gamma);
779 bmp_info.blue_primary.x*=gamma;
780 bmp_info.blue_primary.y*=gamma;
781 image->chromaticity.blue_primary.x=bmp_info.blue_primary.x;
782 image->chromaticity.blue_primary.y=bmp_info.blue_primary.y;
785 Decode 16^16 fixed point formatted gamma_scales.
787 bmp_info.gamma_scale.x=(double) ReadBlobLSBLong(image)/0x10000;
788 bmp_info.gamma_scale.y=(double) ReadBlobLSBLong(image)/0x10000;
789 bmp_info.gamma_scale.z=(double) ReadBlobLSBLong(image)/0x10000;
791 Compute a single gamma from the BMP 3-channel gamma.
793 image->gamma=(bmp_info.gamma_scale.x+bmp_info.gamma_scale.y+
794 bmp_info.gamma_scale.z)/3.0;
797 (void) CopyMagickString(image->magick,"BMP3",MagickPathExtent);
799 if (bmp_info.size > 108)
805 Read BMP Version 5 color management information.
807 intent=ReadBlobLSBLong(image);
808 switch ((int) intent)
810 case LCS_GM_BUSINESS:
812 image->rendering_intent=SaturationIntent;
815 case LCS_GM_GRAPHICS:
817 image->rendering_intent=RelativeIntent;
822 image->rendering_intent=PerceptualIntent;
825 case LCS_GM_ABS_COLORIMETRIC:
827 image->rendering_intent=AbsoluteIntent;
831 profile_data=(MagickOffsetType)ReadBlobLSBLong(image);
832 profile_size=(MagickOffsetType)ReadBlobLSBLong(image);
833 (void) ReadBlobLSBLong(image); /* Reserved byte */
836 if ((MagickSizeType) bmp_info.file_size != blob_size)
841 severity=CorruptImageWarning;
842 if ((MagickSizeType) bmp_info.file_size > blob_size + 4)
843 severity=CorruptImageError;
844 (void) ThrowMagickException(exception,GetMagickModule(),severity,
845 "LengthAndFilesizeDoNotMatch","`%s'",image->filename);
847 if (bmp_info.width <= 0)
848 ThrowReaderException(CorruptImageError,"NegativeOrZeroImageSize");
849 if (bmp_info.height == 0)
850 ThrowReaderException(CorruptImageError,"NegativeOrZeroImageSize");
851 if (bmp_info.planes != 1)
852 ThrowReaderException(CorruptImageError,"StaticPlanesValueNotEqualToOne");
853 if ((bmp_info.bits_per_pixel != 1) && (bmp_info.bits_per_pixel != 4) &&
854 (bmp_info.bits_per_pixel != 8) && (bmp_info.bits_per_pixel != 16) &&
855 (bmp_info.bits_per_pixel != 24) && (bmp_info.bits_per_pixel != 32))
856 ThrowReaderException(CorruptImageError,"UnsupportedBitsPerPixel");
857 if (bmp_info.bits_per_pixel < 16 &&
858 bmp_info.number_colors > (1U << bmp_info.bits_per_pixel))
859 ThrowReaderException(CorruptImageError,"UnrecognizedNumberOfColors");
860 if ((bmp_info.compression == BI_RLE8) && (bmp_info.bits_per_pixel != 8))
861 ThrowReaderException(CorruptImageError,"UnsupportedBitsPerPixel");
862 if ((bmp_info.compression == BI_RLE4) && (bmp_info.bits_per_pixel != 4))
863 ThrowReaderException(CorruptImageError,"UnsupportedBitsPerPixel");
864 if ((bmp_info.compression == BI_BITFIELDS) && (bmp_info.bits_per_pixel < 16))
865 ThrowReaderException(CorruptImageError,"UnsupportedBitsPerPixel");
866 switch (bmp_info.compression)
869 image->compression=NoCompression;
873 image->compression=RLECompression;
878 ThrowReaderException(CoderError,"JPEGCompressNotSupported");
880 ThrowReaderException(CoderError,"PNGCompressNotSupported");
882 ThrowReaderException(CorruptImageError,"UnrecognizedImageCompression");
884 image->columns=(size_t) MagickAbsoluteValue(bmp_info.width);
885 image->rows=(size_t) MagickAbsoluteValue(bmp_info.height);
886 image->depth=bmp_info.bits_per_pixel <= 8 ? bmp_info.bits_per_pixel : 8;
887 image->alpha_trait=((bmp_info.alpha_mask != 0) &&
888 (bmp_info.compression == BI_BITFIELDS)) ? BlendPixelTrait :
890 if (bmp_info.bits_per_pixel < 16)
895 image->storage_class=PseudoClass;
896 image->colors=bmp_info.number_colors;
898 if (image->colors == 0)
899 image->colors=one << bmp_info.bits_per_pixel;
901 image->resolution.x=(double) bmp_info.x_pixels/100.0;
902 image->resolution.y=(double) bmp_info.y_pixels/100.0;
903 image->units=PixelsPerCentimeterResolution;
904 if ((image_info->ping != MagickFalse) && (image_info->number_scenes != 0))
905 if (image->scene >= (image_info->scene+image_info->number_scenes-1))
907 status=SetImageExtent(image,image->columns,image->rows,exception);
908 if (status == MagickFalse)
909 return(DestroyImageList(image));
910 if (image->storage_class == PseudoClass)
919 Read BMP raster colormap.
921 if (image->debug != MagickFalse)
922 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
923 " Reading colormap of %.20g colors",(double) image->colors);
924 if (AcquireImageColormap(image,image->colors,exception) == MagickFalse)
925 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
926 bmp_colormap=(unsigned char *) AcquireQuantumMemory((size_t)
927 image->colors,4*sizeof(*bmp_colormap));
928 if (bmp_colormap == (unsigned char *) NULL)
929 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
930 if ((bmp_info.size == 12) || (bmp_info.size == 64))
934 offset=SeekBlob(image,start_position+14+bmp_info.size,SEEK_SET);
937 bmp_colormap=(unsigned char *) RelinquishMagickMemory(bmp_colormap);
938 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
940 count=ReadBlob(image,packet_size*image->colors,bmp_colormap);
941 if (count != (ssize_t) (packet_size*image->colors))
943 bmp_colormap=(unsigned char *) RelinquishMagickMemory(bmp_colormap);
944 ThrowReaderException(CorruptImageError,
945 "InsufficientImageDataInFile");
948 for (i=0; i < (ssize_t) image->colors; i++)
950 image->colormap[i].blue=(MagickRealType) ScaleCharToQuantum(*p++);
951 image->colormap[i].green=(MagickRealType) ScaleCharToQuantum(*p++);
952 image->colormap[i].red=(MagickRealType) ScaleCharToQuantum(*p++);
953 if (packet_size == 4)
956 bmp_colormap=(unsigned char *) RelinquishMagickMemory(bmp_colormap);
961 if (bmp_info.offset_bits == offset_bits)
962 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
963 offset_bits=bmp_info.offset_bits;
964 offset=SeekBlob(image,start_position+bmp_info.offset_bits,SEEK_SET);
966 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
967 if (bmp_info.compression == BI_RLE4)
968 bmp_info.bits_per_pixel<<=1;
969 bytes_per_line=4*((image->columns*bmp_info.bits_per_pixel+31)/32);
970 length=(size_t) bytes_per_line*image->rows;
971 if ((MagickSizeType) (length/256) > blob_size)
972 ThrowReaderException(CorruptImageError,"InsufficientImageDataInFile");
973 if ((bmp_info.compression == BI_RGB) ||
974 (bmp_info.compression == BI_BITFIELDS))
976 pixel_info=AcquireVirtualMemory(image->rows,
977 MagickMax(bytes_per_line,image->columns+256UL)*sizeof(*pixels));
978 if (pixel_info == (MemoryInfo *) NULL)
979 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
980 pixels=(unsigned char *) GetVirtualMemoryBlob(pixel_info);
981 if (image->debug != MagickFalse)
982 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
983 " Reading pixels (%.20g bytes)",(double) length);
984 count=ReadBlob(image,length,pixels);
985 if (count != (ssize_t) length)
987 pixel_info=RelinquishVirtualMemory(pixel_info);
988 ThrowReaderException(CorruptImageError,
989 "InsufficientImageDataInFile");
995 Convert run-length encoded raster pixels.
997 pixel_info=AcquireVirtualMemory(image->rows,
998 MagickMax(bytes_per_line,image->columns+256UL)*sizeof(*pixels));
999 if (pixel_info == (MemoryInfo *) NULL)
1000 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
1001 pixels=(unsigned char *) GetVirtualMemoryBlob(pixel_info);
1002 status=DecodeImage(image,bmp_info.compression,pixels,
1003 image->columns*image->rows);
1004 if (status == MagickFalse)
1006 pixel_info=RelinquishVirtualMemory(pixel_info);
1007 ThrowReaderException(CorruptImageError,
1008 "UnableToRunlengthDecodeImage");
1012 Convert BMP raster image to pixel packets.
1014 if (bmp_info.compression == BI_RGB)
1017 We should ignore the alpha value in BMP3 files but there have been
1018 reports about 32 bit files with alpha. We do a quick check to see if
1019 the alpha channel contains a value that is not zero (default value).
1020 If we find a non zero value we asume the program that wrote the file
1021 wants to use the alpha channel.
1023 if ((image->alpha_trait == UndefinedPixelTrait) &&
1024 (bmp_info.size == 40) && (bmp_info.bits_per_pixel == 32))
1026 bytes_per_line=4*(image->columns);
1027 for (y=(ssize_t) image->rows-1; y >= 0; y--)
1029 p=pixels+(image->rows-y-1)*bytes_per_line;
1030 for (x=0; x < (ssize_t) image->columns; x++)
1034 image->alpha_trait=BlendPixelTrait;
1042 bmp_info.alpha_mask=image->alpha_trait != UndefinedPixelTrait ?
1044 bmp_info.red_mask=0x00ff0000U;
1045 bmp_info.green_mask=0x0000ff00U;
1046 bmp_info.blue_mask=0x000000ffU;
1047 if (bmp_info.bits_per_pixel == 16)
1052 bmp_info.red_mask=0x00007c00U;
1053 bmp_info.green_mask=0x000003e0U;
1054 bmp_info.blue_mask=0x0000001fU;
1057 (void) memset(&shift,0,sizeof(shift));
1058 (void) memset(&quantum_bits,0,sizeof(quantum_bits));
1059 if ((bmp_info.bits_per_pixel == 16) || (bmp_info.bits_per_pixel == 32))
1061 register unsigned int
1065 Get shift and quantum bits info from bitfield masks.
1067 if (bmp_info.red_mask != 0)
1068 while (((bmp_info.red_mask << shift.red) & 0x80000000UL) == 0)
1071 if (shift.red >= 32U)
1074 if (bmp_info.green_mask != 0)
1075 while (((bmp_info.green_mask << shift.green) & 0x80000000UL) == 0)
1078 if (shift.green >= 32U)
1081 if (bmp_info.blue_mask != 0)
1082 while (((bmp_info.blue_mask << shift.blue) & 0x80000000UL) == 0)
1085 if (shift.blue >= 32U)
1088 if (bmp_info.alpha_mask != 0)
1089 while (((bmp_info.alpha_mask << shift.alpha) & 0x80000000UL) == 0)
1092 if (shift.alpha >= 32U)
1096 while (((bmp_info.red_mask << sample) & 0x80000000UL) != 0)
1102 quantum_bits.red=(MagickRealType) (sample-shift.red);
1104 while (((bmp_info.green_mask << sample) & 0x80000000UL) != 0)
1110 quantum_bits.green=(MagickRealType) (sample-shift.green);
1112 while (((bmp_info.blue_mask << sample) & 0x80000000UL) != 0)
1118 quantum_bits.blue=(MagickRealType) (sample-shift.blue);
1120 while (((bmp_info.alpha_mask << sample) & 0x80000000UL) != 0)
1126 quantum_bits.alpha=(MagickRealType) (sample-shift.alpha);
1128 switch (bmp_info.bits_per_pixel)
1133 Convert bitmap scanline.
1135 for (y=(ssize_t) image->rows-1; y >= 0; y--)
1137 p=pixels+(image->rows-y-1)*bytes_per_line;
1138 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
1139 if (q == (Quantum *) NULL)
1141 for (x=0; x < ((ssize_t) image->columns-7); x+=8)
1143 for (bit=0; bit < 8; bit++)
1145 index=(Quantum) (((*p) & (0x80 >> bit)) != 0 ? 0x01 : 0x00);
1146 SetPixelIndex(image,index,q);
1147 q+=GetPixelChannels(image);
1151 if ((image->columns % 8) != 0)
1153 for (bit=0; bit < (image->columns % 8); bit++)
1155 index=(Quantum) (((*p) & (0x80 >> bit)) != 0 ? 0x01 : 0x00);
1156 SetPixelIndex(image,index,q);
1157 q+=GetPixelChannels(image);
1161 if (SyncAuthenticPixels(image,exception) == MagickFalse)
1163 if (image->previous == (Image *) NULL)
1165 status=SetImageProgress(image,LoadImageTag,(MagickOffsetType)
1166 (image->rows-y),image->rows);
1167 if (status == MagickFalse)
1171 (void) SyncImage(image,exception);
1177 Convert PseudoColor scanline.
1179 for (y=(ssize_t) image->rows-1; y >= 0; y--)
1181 p=pixels+(image->rows-y-1)*bytes_per_line;
1182 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
1183 if (q == (Quantum *) NULL)
1185 for (x=0; x < ((ssize_t) image->columns-1); x+=2)
1187 ValidateColormapValue(image,(ssize_t) ((*p >> 4) & 0x0f),&index,
1189 SetPixelIndex(image,index,q);
1190 q+=GetPixelChannels(image);
1191 ValidateColormapValue(image,(ssize_t) (*p & 0x0f),&index,exception);
1192 SetPixelIndex(image,index,q);
1193 q+=GetPixelChannels(image);
1196 if ((image->columns % 2) != 0)
1198 ValidateColormapValue(image,(ssize_t) ((*p >> 4) & 0xf),&index,
1200 SetPixelIndex(image,index,q);
1201 q+=GetPixelChannels(image);
1205 if (x < (ssize_t) image->columns)
1207 if (SyncAuthenticPixels(image,exception) == MagickFalse)
1209 if (image->previous == (Image *) NULL)
1211 status=SetImageProgress(image,LoadImageTag,(MagickOffsetType)
1212 (image->rows-y),image->rows);
1213 if (status == MagickFalse)
1217 (void) SyncImage(image,exception);
1223 Convert PseudoColor scanline.
1225 if ((bmp_info.compression == BI_RLE8) ||
1226 (bmp_info.compression == BI_RLE4))
1227 bytes_per_line=image->columns;
1228 for (y=(ssize_t) image->rows-1; y >= 0; y--)
1230 p=pixels+(image->rows-y-1)*bytes_per_line;
1231 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
1232 if (q == (Quantum *) NULL)
1234 for (x=(ssize_t) image->columns; x != 0; --x)
1236 ValidateColormapValue(image,(ssize_t) *p++,&index,exception);
1237 SetPixelIndex(image,index,q);
1238 q+=GetPixelChannels(image);
1240 if (SyncAuthenticPixels(image,exception) == MagickFalse)
1242 offset=(MagickOffsetType) (image->rows-y-1);
1243 if (image->previous == (Image *) NULL)
1245 status=SetImageProgress(image,LoadImageTag,(MagickOffsetType)
1246 (image->rows-y),image->rows);
1247 if (status == MagickFalse)
1251 (void) SyncImage(image,exception);
1261 Convert bitfield encoded 16-bit PseudoColor scanline.
1263 if ((bmp_info.compression != BI_RGB) &&
1264 (bmp_info.compression != BI_BITFIELDS))
1266 pixel_info=RelinquishVirtualMemory(pixel_info);
1267 ThrowReaderException(CorruptImageError,
1268 "UnrecognizedImageCompression");
1270 bytes_per_line=2*(image->columns+image->columns % 2);
1271 image->storage_class=DirectClass;
1272 for (y=(ssize_t) image->rows-1; y >= 0; y--)
1274 p=pixels+(image->rows-y-1)*bytes_per_line;
1275 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
1276 if (q == (Quantum *) NULL)
1278 for (x=0; x < (ssize_t) image->columns; x++)
1280 pixel=(unsigned int) (*p++);
1282 red=((pixel & bmp_info.red_mask) << shift.red) >> 16;
1283 if (quantum_bits.red == 5)
1284 red|=((red & 0xe000) >> 5);
1285 if (quantum_bits.red <= 8)
1286 red|=((red & 0xff00) >> 8);
1287 green=((pixel & bmp_info.green_mask) << shift.green) >> 16;
1288 if (quantum_bits.green == 5)
1289 green|=((green & 0xe000) >> 5);
1290 if (quantum_bits.green == 6)
1291 green|=((green & 0xc000) >> 6);
1292 if (quantum_bits.green <= 8)
1293 green|=((green & 0xff00) >> 8);
1294 blue=((pixel & bmp_info.blue_mask) << shift.blue) >> 16;
1295 if (quantum_bits.blue == 5)
1296 blue|=((blue & 0xe000) >> 5);
1297 if (quantum_bits.blue <= 8)
1298 blue|=((blue & 0xff00) >> 8);
1299 SetPixelRed(image,ScaleShortToQuantum((unsigned short) red),q);
1300 SetPixelGreen(image,ScaleShortToQuantum((unsigned short) green),q);
1301 SetPixelBlue(image,ScaleShortToQuantum((unsigned short) blue),q);
1302 SetPixelAlpha(image,OpaqueAlpha,q);
1303 if (image->alpha_trait != UndefinedPixelTrait)
1305 alpha=((pixel & bmp_info.alpha_mask) << shift.alpha) >> 16;
1306 if (quantum_bits.alpha <= 8)
1307 alpha|=((alpha & 0xff00) >> 8);
1308 SetPixelAlpha(image,ScaleShortToQuantum(
1309 (unsigned short) alpha),q);
1311 q+=GetPixelChannels(image);
1313 if (SyncAuthenticPixels(image,exception) == MagickFalse)
1315 offset=(MagickOffsetType) (image->rows-y-1);
1316 if (image->previous == (Image *) NULL)
1318 status=SetImageProgress(image,LoadImageTag,(MagickOffsetType)
1319 (image->rows-y),image->rows);
1320 if (status == MagickFalse)
1329 Convert DirectColor scanline.
1331 bytes_per_line=4*((image->columns*24+31)/32);
1332 for (y=(ssize_t) image->rows-1; y >= 0; y--)
1334 p=pixels+(image->rows-y-1)*bytes_per_line;
1335 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
1336 if (q == (Quantum *) NULL)
1338 for (x=0; x < (ssize_t) image->columns; x++)
1340 SetPixelBlue(image,ScaleCharToQuantum(*p++),q);
1341 SetPixelGreen(image,ScaleCharToQuantum(*p++),q);
1342 SetPixelRed(image,ScaleCharToQuantum(*p++),q);
1343 SetPixelAlpha(image,OpaqueAlpha,q);
1344 q+=GetPixelChannels(image);
1346 if (SyncAuthenticPixels(image,exception) == MagickFalse)
1348 offset=(MagickOffsetType) (image->rows-y-1);
1349 if (image->previous == (Image *) NULL)
1351 status=SetImageProgress(image,LoadImageTag,(MagickOffsetType)
1352 (image->rows-y),image->rows);
1353 if (status == MagickFalse)
1362 Convert bitfield encoded DirectColor scanline.
1364 if ((bmp_info.compression != BI_RGB) &&
1365 (bmp_info.compression != BI_BITFIELDS))
1367 pixel_info=RelinquishVirtualMemory(pixel_info);
1368 ThrowReaderException(CorruptImageError,
1369 "UnrecognizedImageCompression");
1371 bytes_per_line=4*(image->columns);
1372 for (y=(ssize_t) image->rows-1; y >= 0; y--)
1378 p=pixels+(image->rows-y-1)*bytes_per_line;
1379 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
1380 if (q == (Quantum *) NULL)
1382 for (x=0; x < (ssize_t) image->columns; x++)
1384 pixel=(unsigned int) (*p++);
1385 pixel|=((unsigned int) *p++ << 8);
1386 pixel|=((unsigned int) *p++ << 16);
1387 pixel|=((unsigned int) *p++ << 24);
1388 red=((pixel & bmp_info.red_mask) << shift.red) >> 16;
1389 if (quantum_bits.red == 8)
1391 green=((pixel & bmp_info.green_mask) << shift.green) >> 16;
1392 if (quantum_bits.green == 8)
1393 green|=(green >> 8);
1394 blue=((pixel & bmp_info.blue_mask) << shift.blue) >> 16;
1395 if (quantum_bits.blue == 8)
1397 SetPixelRed(image,ScaleShortToQuantum((unsigned short) red),q);
1398 SetPixelGreen(image,ScaleShortToQuantum((unsigned short) green),q);
1399 SetPixelBlue(image,ScaleShortToQuantum((unsigned short) blue),q);
1400 SetPixelAlpha(image,OpaqueAlpha,q);
1401 if (image->alpha_trait != UndefinedPixelTrait)
1403 alpha=((pixel & bmp_info.alpha_mask) << shift.alpha) >> 16;
1404 if (quantum_bits.alpha == 8)
1405 alpha|=(alpha >> 8);
1406 SetPixelAlpha(image,ScaleShortToQuantum(
1407 (unsigned short) alpha),q);
1409 q+=GetPixelChannels(image);
1411 if (SyncAuthenticPixels(image,exception) == MagickFalse)
1413 offset=(MagickOffsetType) (image->rows-y-1);
1414 if (image->previous == (Image *) NULL)
1416 status=SetImageProgress(image,LoadImageTag,(MagickOffsetType)
1417 (image->rows-y),image->rows);
1418 if (status == MagickFalse)
1426 pixel_info=RelinquishVirtualMemory(pixel_info);
1427 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
1430 pixel_info=RelinquishVirtualMemory(pixel_info);
1433 if (EOFBlob(image) != MagickFalse)
1435 ThrowFileException(exception,CorruptImageError,"UnexpectedEndOfFile",
1439 if (bmp_info.height < 0)
1445 Correct image orientation.
1447 flipped_image=FlipImage(image,exception);
1448 if (flipped_image != (Image *) NULL)
1450 DuplicateBlob(flipped_image,image);
1451 ReplaceImageInList(&image, flipped_image);
1452 image=flipped_image;
1456 Read embeded ICC profile
1458 if ((bmp_info.colorspace == 0x4D424544L) && (profile_data > 0) &&
1467 offset=start_position+14+profile_data;
1468 if ((offset < TellBlob(image)) ||
1469 (SeekBlob(image,offset,SEEK_SET) != offset) ||
1470 (blob_size < (MagickSizeType) (offset+profile_size)))
1471 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
1472 profile=AcquireStringInfo((size_t) profile_size);
1473 if (profile == (StringInfo *) NULL)
1474 ThrowReaderException(CorruptImageError,"MemoryAllocationFailed");
1475 datum=GetStringInfoDatum(profile);
1476 if (ReadBlob(image,(size_t) profile_size,datum) == (ssize_t) profile_size)
1482 Trimming padded bytes.
1484 profile_size_orig=(MagickOffsetType) datum[0] << 24;
1485 profile_size_orig|=(MagickOffsetType) datum[1] << 16;
1486 profile_size_orig|=(MagickOffsetType) datum[2] << 8;
1487 profile_size_orig|=(MagickOffsetType) datum[3];
1488 if (profile_size_orig < profile_size)
1489 SetStringInfoLength(profile,(size_t) profile_size_orig);
1490 if (image->debug != MagickFalse)
1491 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1492 "Profile: ICC, %u bytes",(unsigned int) profile_size_orig);
1493 (void) SetImageProfile(image,"icc",profile,exception);
1495 profile=DestroyStringInfo(profile);
1498 Proceed to next image.
1500 if (image_info->number_scenes != 0)
1501 if (image->scene >= (image_info->scene+image_info->number_scenes-1))
1503 offset=(MagickOffsetType) bmp_info.ba_offset;
1505 if ((offset < TellBlob(image)) ||
1506 (SeekBlob(image,offset,SEEK_SET) != offset))
1507 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
1509 count=ReadBlob(image,2,magick);
1510 if ((count == 2) && (IsBMP(magick,2) != MagickFalse))
1513 Acquire next image structure.
1515 AcquireNextImage(image_info,image,exception);
1516 if (GetNextImageInList(image) == (Image *) NULL)
1521 image=SyncNextImageInList(image);
1522 status=SetImageProgress(image,LoadImagesTag,TellBlob(image),blob_size);
1523 if (status == MagickFalse)
1526 } while (IsBMP(magick,2) != MagickFalse);
1527 (void) CloseBlob(image);
1528 if (status == MagickFalse)
1529 return(DestroyImageList(image));
1530 return(GetFirstImageInList(image));
1534 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1538 % R e g i s t e r B M P I m a g e %
1542 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1544 % RegisterBMPImage() adds attributes for the BMP image format to
1545 % the list of supported formats. The attributes include the image format
1546 % tag, a method to read and/or write the format, whether the format
1547 % supports the saving of more than one frame to the same file or blob,
1548 % whether the format supports native in-memory I/O, and a brief
1549 % description of the format.
1551 % The format of the RegisterBMPImage method is:
1553 % size_t RegisterBMPImage(void)
1556 ModuleExport size_t RegisterBMPImage(void)
1561 entry=AcquireMagickInfo("BMP","BMP","Microsoft Windows bitmap image");
1562 entry->decoder=(DecodeImageHandler *) ReadBMPImage;
1563 entry->encoder=(EncodeImageHandler *) WriteBMPImage;
1564 entry->magick=(IsImageFormatHandler *) IsBMP;
1565 entry->flags^=CoderAdjoinFlag;
1566 entry->flags|=CoderDecoderSeekableStreamFlag;
1567 (void) RegisterMagickInfo(entry);
1568 entry=AcquireMagickInfo("BMP","BMP2","Microsoft Windows bitmap image (V2)");
1569 entry->decoder=(DecodeImageHandler *) ReadBMPImage;
1570 entry->encoder=(EncodeImageHandler *) WriteBMPImage;
1571 entry->magick=(IsImageFormatHandler *) IsBMP;
1572 entry->flags^=CoderAdjoinFlag;
1573 entry->flags|=CoderDecoderSeekableStreamFlag;
1574 (void) RegisterMagickInfo(entry);
1575 entry=AcquireMagickInfo("BMP","BMP3","Microsoft Windows bitmap image (V3)");
1576 entry->decoder=(DecodeImageHandler *) ReadBMPImage;
1577 entry->encoder=(EncodeImageHandler *) WriteBMPImage;
1578 entry->magick=(IsImageFormatHandler *) IsBMP;
1579 entry->flags^=CoderAdjoinFlag;
1580 entry->flags|=CoderDecoderSeekableStreamFlag;
1581 (void) RegisterMagickInfo(entry);
1582 return(MagickImageCoderSignature);
1586 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1590 % U n r e g i s t e r B M P I m a g e %
1594 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1596 % UnregisterBMPImage() removes format registrations made by the
1597 % BMP module from the list of supported formats.
1599 % The format of the UnregisterBMPImage method is:
1601 % UnregisterBMPImage(void)
1604 ModuleExport void UnregisterBMPImage(void)
1606 (void) UnregisterMagickInfo("BMP");
1607 (void) UnregisterMagickInfo("BMP2");
1608 (void) UnregisterMagickInfo("BMP3");
1612 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1616 % W r i t e B M P I m a g e %
1620 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1622 % WriteBMPImage() writes an image in Microsoft Windows bitmap encoded
1623 % image format, version 3 for Windows or (if the image has a matte channel)
1626 % The format of the WriteBMPImage method is:
1628 % MagickBooleanType WriteBMPImage(const ImageInfo *image_info,
1629 % Image *image,ExceptionInfo *exception)
1631 % A description of each parameter follows.
1633 % o image_info: the image info.
1635 % o image: The image.
1637 % o exception: return any errors or warnings in this structure.
1640 static MagickBooleanType WriteBMPImage(const ImageInfo *image_info,Image *image,
1641 ExceptionInfo *exception)
1665 register const Quantum
1672 register unsigned char
1693 Open output image file.
1695 assert(image_info != (const ImageInfo *) NULL);
1696 assert(image_info->signature == MagickCoreSignature);
1697 assert(image != (Image *) NULL);
1698 assert(image->signature == MagickCoreSignature);
1699 if (image->debug != MagickFalse)
1700 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1701 assert(exception != (ExceptionInfo *) NULL);
1702 assert(exception->signature == MagickCoreSignature);
1703 status=OpenBlob(image_info,image,WriteBinaryBlobMode,exception);
1704 if (status == MagickFalse)
1706 if (((image->columns << 3) != (int) (image->columns << 3)) ||
1707 ((image->rows << 3) != (int) (image->rows << 3)))
1708 ThrowWriterException(ImageError,"WidthOrHeightExceedsLimit");
1710 if (LocaleCompare(image_info->magick,"BMP2") == 0)
1713 if (LocaleCompare(image_info->magick,"BMP3") == 0)
1715 option=GetImageOption(image_info,"bmp:format");
1716 if (option != (char *) NULL)
1718 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1719 " Format=%s",option);
1720 if (LocaleCompare(option,"bmp2") == 0)
1722 if (LocaleCompare(option,"bmp3") == 0)
1724 if (LocaleCompare(option,"bmp4") == 0)
1728 imageListLength=GetImageListLength(image);
1732 Initialize BMP raster file header.
1734 if (IssRGBCompatibleColorspace(image->colorspace) == MagickFalse)
1735 (void) TransformImageColorspace(image,sRGBColorspace,exception);
1736 (void) memset(&bmp_info,0,sizeof(bmp_info));
1737 bmp_info.file_size=14+12;
1739 bmp_info.file_size+=28;
1740 bmp_info.offset_bits=bmp_info.file_size;
1741 bmp_info.compression=BI_RGB;
1742 bmp_info.red_mask=0x00ff0000U;
1743 bmp_info.green_mask=0x0000ff00U;
1744 bmp_info.blue_mask=0x000000ffU;
1745 bmp_info.alpha_mask=0xff000000U;
1746 bmp_subtype=UndefinedSubtype;
1747 if ((image->storage_class == PseudoClass) && (image->colors > 256))
1748 (void) SetImageStorageClass(image,DirectClass,exception);
1749 if (image->storage_class != DirectClass)
1752 Colormapped BMP raster.
1754 bmp_info.bits_per_pixel=8;
1755 if (image->colors <= 2)
1756 bmp_info.bits_per_pixel=1;
1758 if (image->colors <= 16)
1759 bmp_info.bits_per_pixel=4;
1761 if (image->colors <= 256)
1762 bmp_info.bits_per_pixel=8;
1763 if (image_info->compression == RLECompression)
1764 bmp_info.bits_per_pixel=8;
1765 bmp_info.number_colors=1U << bmp_info.bits_per_pixel;
1766 if (image->alpha_trait != UndefinedPixelTrait)
1767 (void) SetImageStorageClass(image,DirectClass,exception);
1769 if ((size_t) bmp_info.number_colors < image->colors)
1770 (void) SetImageStorageClass(image,DirectClass,exception);
1773 bmp_info.file_size+=3*(1UL << bmp_info.bits_per_pixel);
1774 bmp_info.offset_bits+=3*(1UL << bmp_info.bits_per_pixel);
1777 bmp_info.file_size+=(1UL << bmp_info.bits_per_pixel);
1778 bmp_info.offset_bits+=(1UL << bmp_info.bits_per_pixel);
1782 if (image->storage_class == DirectClass)
1785 Full color BMP raster.
1787 bmp_info.number_colors=0;
1788 option=GetImageOption(image_info,"bmp:subtype");
1789 if (option != (const char *) NULL)
1791 if (image->alpha_trait != UndefinedPixelTrait)
1793 if (LocaleNCompare(option,"ARGB4444",8) == 0)
1795 bmp_subtype=ARGB4444;
1796 bmp_info.red_mask=0x00000f00U;
1797 bmp_info.green_mask=0x000000f0U;
1798 bmp_info.blue_mask=0x0000000fU;
1799 bmp_info.alpha_mask=0x0000f000U;
1801 else if (LocaleNCompare(option,"ARGB1555",8) == 0)
1803 bmp_subtype=ARGB1555;
1804 bmp_info.red_mask=0x00007c00U;
1805 bmp_info.green_mask=0x000003e0U;
1806 bmp_info.blue_mask=0x0000001fU;
1807 bmp_info.alpha_mask=0x00008000U;
1812 if (LocaleNCompare(option,"RGB555",6) == 0)
1815 bmp_info.red_mask=0x00007c00U;
1816 bmp_info.green_mask=0x000003e0U;
1817 bmp_info.blue_mask=0x0000001fU;
1818 bmp_info.alpha_mask=0U;
1820 else if (LocaleNCompare(option,"RGB565",6) == 0)
1823 bmp_info.red_mask=0x0000f800U;
1824 bmp_info.green_mask=0x000007e0U;
1825 bmp_info.blue_mask=0x0000001fU;
1826 bmp_info.alpha_mask=0U;
1830 if (bmp_subtype != UndefinedSubtype)
1832 bmp_info.bits_per_pixel=16;
1833 bmp_info.compression=BI_BITFIELDS;
1837 bmp_info.bits_per_pixel=(unsigned short) ((type > 3) &&
1838 (image->alpha_trait != UndefinedPixelTrait) ? 32 : 24);
1839 bmp_info.compression=(unsigned int) ((type > 3) &&
1840 (image->alpha_trait != UndefinedPixelTrait) ? BI_BITFIELDS : BI_RGB);
1841 if ((type == 3) && (image->alpha_trait != UndefinedPixelTrait))
1843 option=GetImageOption(image_info,"bmp3:alpha");
1844 if (IsStringTrue(option))
1845 bmp_info.bits_per_pixel=32;
1849 bytes_per_line=4*((image->columns*bmp_info.bits_per_pixel+31)/32);
1850 bmp_info.ba_offset=0;
1851 profile=GetImageProfile(image,"icc");
1852 have_color_info=(image->rendering_intent != UndefinedIntent) ||
1853 (profile != (StringInfo *) NULL) || (image->gamma != 0.0) ? MagickTrue :
1858 if ((type == 3) || ((image->alpha_trait == UndefinedPixelTrait) &&
1859 (have_color_info == MagickFalse)))
1871 if ((image->rendering_intent != UndefinedIntent) ||
1872 (profile != (StringInfo *) NULL))
1877 bmp_info.file_size+=extra_size;
1878 bmp_info.offset_bits+=extra_size;
1880 if (((ssize_t) image->columns != (ssize_t) ((signed int) image->columns)) ||
1881 ((ssize_t) image->rows != (ssize_t) ((signed int) image->rows)))
1882 ThrowWriterException(ImageError,"WidthOrHeightExceedsLimit");
1883 bmp_info.width=(ssize_t) image->columns;
1884 bmp_info.height=(ssize_t) image->rows;
1886 bmp_info.image_size=(unsigned int) (bytes_per_line*image->rows);
1887 bmp_info.file_size+=bmp_info.image_size;
1888 bmp_info.x_pixels=75*39;
1889 bmp_info.y_pixels=75*39;
1890 switch (image->units)
1892 case UndefinedResolution:
1893 case PixelsPerInchResolution:
1895 bmp_info.x_pixels=(unsigned int) (100.0*image->resolution.x/2.54);
1896 bmp_info.y_pixels=(unsigned int) (100.0*image->resolution.y/2.54);
1899 case PixelsPerCentimeterResolution:
1901 bmp_info.x_pixels=(unsigned int) (100.0*image->resolution.x);
1902 bmp_info.y_pixels=(unsigned int) (100.0*image->resolution.y);
1906 bmp_info.colors_important=bmp_info.number_colors;
1908 Convert MIFF to BMP raster pixels.
1910 pixel_info=AcquireVirtualMemory(image->rows,MagickMax(bytes_per_line,
1911 image->columns+256UL)*sizeof(*pixels));
1912 if (pixel_info == (MemoryInfo *) NULL)
1913 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
1914 pixels=(unsigned char *) GetVirtualMemoryBlob(pixel_info);
1915 (void) memset(pixels,0,(size_t) bmp_info.image_size);
1916 switch (bmp_info.bits_per_pixel)
1925 Convert PseudoClass image to a BMP monochrome image.
1927 for (y=0; y < (ssize_t) image->rows; y++)
1932 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
1933 if (p == (const Quantum *) NULL)
1935 q=pixels+(image->rows-y-1)*bytes_per_line;
1938 for (x=0; x < (ssize_t) image->columns; x++)
1941 byte|=GetPixelIndex(image,p) != 0 ? 0x01 : 0x00;
1945 *q++=(unsigned char) byte;
1949 p+=GetPixelChannels(image);
1953 *q++=(unsigned char) (byte << (8-bit));
1956 offset=(ssize_t) (image->columns+7)/8;
1957 for (x=offset; x < (ssize_t) bytes_per_line; x++)
1959 if (image->previous == (Image *) NULL)
1961 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
1963 if (status == MagickFalse)
1979 Convert PseudoClass image to a BMP monochrome image.
1981 for (y=0; y < (ssize_t) image->rows; y++)
1983 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
1984 if (p == (const Quantum *) NULL)
1986 q=pixels+(image->rows-y-1)*bytes_per_line;
1989 for (x=0; x < (ssize_t) image->columns; x++)
1992 byte|=((unsigned int) GetPixelIndex(image,p) & 0x0f);
1996 *q++=(unsigned char) byte;
2000 p+=GetPixelChannels(image);
2004 *q++=(unsigned char) (byte << 4);
2007 offset=(ssize_t) (image->columns+1)/2;
2008 for (x=offset; x < (ssize_t) bytes_per_line; x++)
2010 if (image->previous == (Image *) NULL)
2012 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
2014 if (status == MagickFalse)
2023 Convert PseudoClass packet to BMP pixel.
2025 for (y=0; y < (ssize_t) image->rows; y++)
2027 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
2028 if (p == (const Quantum *) NULL)
2030 q=pixels+(image->rows-y-1)*bytes_per_line;
2031 for (x=0; x < (ssize_t) image->columns; x++)
2033 *q++=(unsigned char) GetPixelIndex(image,p);
2034 p+=GetPixelChannels(image);
2036 for ( ; x < (ssize_t) bytes_per_line; x++)
2038 if (image->previous == (Image *) NULL)
2040 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
2042 if (status == MagickFalse)
2051 Convert DirectClass packet to BMP BGR888.
2053 for (y=0; y < (ssize_t) image->rows; y++)
2055 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
2056 if (p == (const Quantum *) NULL)
2058 q=pixels+(image->rows-y-1)*bytes_per_line;
2059 for (x=0; x < (ssize_t) image->columns; x++)
2065 if (bmp_subtype == ARGB4444)
2067 pixel=(unsigned short) (ScaleQuantumToAny(
2068 GetPixelAlpha(image,p),15) << 12);
2069 pixel|=(unsigned short) (ScaleQuantumToAny(
2070 GetPixelRed(image,p),15) << 8);
2071 pixel|=(unsigned short) (ScaleQuantumToAny(
2072 GetPixelGreen(image,p),15) << 4);
2073 pixel|=(unsigned short) (ScaleQuantumToAny(
2074 GetPixelBlue(image,p),15));
2076 else if (bmp_subtype == RGB565)
2078 pixel=(unsigned short) (ScaleQuantumToAny(
2079 GetPixelRed(image,p),31) << 11);
2080 pixel|=(unsigned short) (ScaleQuantumToAny(
2081 GetPixelGreen(image,p),63) << 5);
2082 pixel|=(unsigned short) (ScaleQuantumToAny(
2083 GetPixelBlue(image,p),31));
2087 if (bmp_subtype == ARGB1555)
2088 pixel=(unsigned short) (ScaleQuantumToAny(
2089 GetPixelAlpha(image,p),1) << 15);
2090 pixel|=(unsigned short) (ScaleQuantumToAny(
2091 GetPixelRed(image,p),31) << 10);
2092 pixel|=(unsigned short) (ScaleQuantumToAny(
2093 GetPixelGreen(image,p),31) << 5);
2094 pixel|=(unsigned short) (ScaleQuantumToAny(
2095 GetPixelBlue(image,p),31));
2097 *((unsigned short *) q)=pixel;
2099 p+=GetPixelChannels(image);
2101 for (x=2L*(ssize_t) image->columns; x < (ssize_t) bytes_per_line; x++)
2103 if (image->previous == (Image *) NULL)
2105 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
2107 if (status == MagickFalse)
2116 Convert DirectClass packet to BMP BGR888.
2118 for (y=0; y < (ssize_t) image->rows; y++)
2120 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
2121 if (p == (const Quantum *) NULL)
2123 q=pixels+(image->rows-y-1)*bytes_per_line;
2124 for (x=0; x < (ssize_t) image->columns; x++)
2126 *q++=ScaleQuantumToChar(GetPixelBlue(image,p));
2127 *q++=ScaleQuantumToChar(GetPixelGreen(image,p));
2128 *q++=ScaleQuantumToChar(GetPixelRed(image,p));
2129 p+=GetPixelChannels(image);
2131 for (x=3L*(ssize_t) image->columns; x < (ssize_t) bytes_per_line; x++)
2133 if (image->previous == (Image *) NULL)
2135 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
2137 if (status == MagickFalse)
2146 Convert DirectClass packet to ARGB8888 pixel.
2148 for (y=0; y < (ssize_t) image->rows; y++)
2150 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
2151 if (p == (const Quantum *) NULL)
2153 q=pixels+(image->rows-y-1)*bytes_per_line;
2154 for (x=0; x < (ssize_t) image->columns; x++)
2156 *q++=ScaleQuantumToChar(GetPixelBlue(image,p));
2157 *q++=ScaleQuantumToChar(GetPixelGreen(image,p));
2158 *q++=ScaleQuantumToChar(GetPixelRed(image,p));
2159 *q++=ScaleQuantumToChar(GetPixelAlpha(image,p));
2160 p+=GetPixelChannels(image);
2162 if (image->previous == (Image *) NULL)
2164 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
2166 if (status == MagickFalse)
2173 if ((type > 2) && (bmp_info.bits_per_pixel == 8))
2174 if (image_info->compression != NoCompression)
2180 Convert run-length encoded raster pixels.
2182 rle_info=AcquireVirtualMemory((size_t) (2*(bytes_per_line+2)+2),
2183 (image->rows+2)*sizeof(*pixels));
2184 if (rle_info == (MemoryInfo *) NULL)
2186 pixel_info=RelinquishVirtualMemory(pixel_info);
2187 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
2189 bmp_data=(unsigned char *) GetVirtualMemoryBlob(rle_info);
2190 bmp_info.file_size-=bmp_info.image_size;
2191 bmp_info.image_size=(unsigned int) EncodeImage(image,bytes_per_line,
2193 bmp_info.file_size+=bmp_info.image_size;
2194 pixel_info=RelinquishVirtualMemory(pixel_info);
2195 pixel_info=rle_info;
2197 bmp_info.compression=BI_RLE8;
2200 Write BMP for Windows, all versions, 14-byte header.
2202 if (image->debug != MagickFalse)
2204 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
2205 " Writing BMP version %.20g datastream",(double) type);
2206 if (image->storage_class == DirectClass)
2207 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
2208 " Storage class=DirectClass");
2210 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
2211 " Storage class=PseudoClass");
2212 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
2213 " Image depth=%.20g",(double) image->depth);
2214 if (image->alpha_trait != UndefinedPixelTrait)
2215 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
2218 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
2219 " Matte=MagickFalse");
2220 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
2221 " BMP bits_per_pixel=%.20g",(double) bmp_info.bits_per_pixel);
2222 switch ((int) bmp_info.compression)
2226 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
2227 " Compression=BI_RGB");
2232 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
2233 " Compression=BI_RLE8");
2238 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
2239 " Compression=BI_BITFIELDS");
2244 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
2245 " Compression=UNKNOWN (%u)",bmp_info.compression);
2249 if (bmp_info.number_colors == 0)
2250 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
2251 " Number_colors=unspecified");
2253 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
2254 " Number_colors=%u",bmp_info.number_colors);
2259 if (profile != (StringInfo *) NULL)
2261 profile_data=(MagickOffsetType) bmp_info.file_size-14; /* from head of BMP info header */
2262 profile_size=(MagickOffsetType) GetStringInfoLength(profile);
2263 if ((profile_size % 4) > 0)
2264 profile_size_pad=4-(profile_size%4);
2265 bmp_info.file_size+=profile_size+profile_size_pad;
2267 (void) WriteBlob(image,2,(unsigned char *) "BM");
2268 (void) WriteBlobLSBLong(image,bmp_info.file_size);
2269 (void) WriteBlobLSBLong(image,bmp_info.ba_offset); /* always 0 */
2270 (void) WriteBlobLSBLong(image,bmp_info.offset_bits);
2274 Write 12-byte version 2 bitmap header.
2276 (void) WriteBlobLSBLong(image,bmp_info.size);
2277 (void) WriteBlobLSBSignedShort(image,(signed short) bmp_info.width);
2278 (void) WriteBlobLSBSignedShort(image,(signed short) bmp_info.height);
2279 (void) WriteBlobLSBShort(image,bmp_info.planes);
2280 (void) WriteBlobLSBShort(image,bmp_info.bits_per_pixel);
2285 Write 40-byte version 3+ bitmap header.
2287 (void) WriteBlobLSBLong(image,bmp_info.size);
2288 (void) WriteBlobLSBSignedLong(image,(signed int) bmp_info.width);
2289 (void) WriteBlobLSBSignedLong(image,(signed int) bmp_info.height);
2290 (void) WriteBlobLSBShort(image,bmp_info.planes);
2291 (void) WriteBlobLSBShort(image,bmp_info.bits_per_pixel);
2292 (void) WriteBlobLSBLong(image,bmp_info.compression);
2293 (void) WriteBlobLSBLong(image,bmp_info.image_size);
2294 (void) WriteBlobLSBLong(image,bmp_info.x_pixels);
2295 (void) WriteBlobLSBLong(image,bmp_info.y_pixels);
2296 (void) WriteBlobLSBLong(image,bmp_info.number_colors);
2297 (void) WriteBlobLSBLong(image,bmp_info.colors_important);
2299 if ((type > 3) && ((image->alpha_trait != UndefinedPixelTrait) ||
2300 (have_color_info != MagickFalse)))
2303 Write the rest of the 108-byte BMP Version 4 header.
2305 (void) WriteBlobLSBLong(image,bmp_info.red_mask);
2306 (void) WriteBlobLSBLong(image,bmp_info.green_mask);
2307 (void) WriteBlobLSBLong(image,bmp_info.blue_mask);
2308 (void) WriteBlobLSBLong(image,bmp_info.alpha_mask);
2309 if (profile != (StringInfo *) NULL)
2310 (void) WriteBlobLSBLong(image,0x4D424544U); /* PROFILE_EMBEDDED */
2312 (void) WriteBlobLSBLong(image,0x73524742U); /* sRGB */
2313 (void) WriteBlobLSBLong(image,(unsigned int)
2314 ((ssize_t) image->chromaticity.red_primary.x*0x40000000));
2315 (void) WriteBlobLSBLong(image,(unsigned int)
2316 ((ssize_t) image->chromaticity.red_primary.y*0x40000000));
2317 (void) WriteBlobLSBLong(image,(unsigned int)
2318 ((ssize_t) (1.000f-(image->chromaticity.red_primary.x+
2319 image->chromaticity.red_primary.y))*0x40000000));
2320 (void) WriteBlobLSBLong(image,(unsigned int)
2321 ((ssize_t) image->chromaticity.green_primary.x*0x40000000));
2322 (void) WriteBlobLSBLong(image,(unsigned int)
2323 ((ssize_t) image->chromaticity.green_primary.y*0x40000000));
2324 (void) WriteBlobLSBLong(image,(unsigned int)
2325 ((ssize_t) (1.000f-(image->chromaticity.green_primary.x+
2326 image->chromaticity.green_primary.y))*0x40000000));
2327 (void) WriteBlobLSBLong(image,(unsigned int)
2328 ((ssize_t) image->chromaticity.blue_primary.x*0x40000000));
2329 (void) WriteBlobLSBLong(image,(unsigned int)
2330 ((ssize_t) image->chromaticity.blue_primary.y*0x40000000));
2331 (void) WriteBlobLSBLong(image,(unsigned int)
2332 ((ssize_t) (1.000f-(image->chromaticity.blue_primary.x+
2333 image->chromaticity.blue_primary.y))*0x40000000));
2334 (void) WriteBlobLSBLong(image,(unsigned int)
2335 ((ssize_t) bmp_info.gamma_scale.x*0x10000));
2336 (void) WriteBlobLSBLong(image,(unsigned int)
2337 ((ssize_t) bmp_info.gamma_scale.y*0x10000));
2338 (void) WriteBlobLSBLong(image,(unsigned int)
2339 ((ssize_t) bmp_info.gamma_scale.z*0x10000));
2340 if ((image->rendering_intent != UndefinedIntent) ||
2341 (profile != (StringInfo *) NULL))
2346 switch ((int) image->rendering_intent)
2348 case SaturationIntent:
2350 intent=LCS_GM_BUSINESS;
2353 case RelativeIntent:
2355 intent=LCS_GM_GRAPHICS;
2358 case PerceptualIntent:
2360 intent=LCS_GM_IMAGES;
2363 case AbsoluteIntent:
2365 intent=LCS_GM_ABS_COLORIMETRIC;
2374 (void) WriteBlobLSBLong(image,(unsigned int) intent);
2375 (void) WriteBlobLSBLong(image,(unsigned int) profile_data);
2376 (void) WriteBlobLSBLong(image,(unsigned int)
2377 (profile_size+profile_size_pad));
2378 (void) WriteBlobLSBLong(image,0x00); /* reserved */
2381 if (image->storage_class == PseudoClass)
2387 Dump colormap to file.
2389 if (image->debug != MagickFalse)
2390 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
2391 " Colormap: %.20g entries",(double) image->colors);
2392 bmp_colormap=(unsigned char *) AcquireQuantumMemory((size_t) (1UL <<
2393 bmp_info.bits_per_pixel),4*sizeof(*bmp_colormap));
2394 if (bmp_colormap == (unsigned char *) NULL)
2396 pixel_info=RelinquishVirtualMemory(pixel_info);
2397 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
2400 for (i=0; i < (ssize_t) MagickMin((ssize_t) image->colors,(ssize_t) bmp_info.number_colors); i++)
2402 *q++=ScaleQuantumToChar(ClampToQuantum(image->colormap[i].blue));
2403 *q++=ScaleQuantumToChar(ClampToQuantum(image->colormap[i].green));
2404 *q++=ScaleQuantumToChar(ClampToQuantum(image->colormap[i].red));
2406 *q++=(unsigned char) 0x0;
2408 for ( ; i < (ssize_t) (1UL << bmp_info.bits_per_pixel); i++)
2410 *q++=(unsigned char) 0x00;
2411 *q++=(unsigned char) 0x00;
2412 *q++=(unsigned char) 0x00;
2414 *q++=(unsigned char) 0x00;
2417 (void) WriteBlob(image,(size_t) (3*(1L << bmp_info.bits_per_pixel)),
2420 (void) WriteBlob(image,(size_t) (4*(1L << bmp_info.bits_per_pixel)),
2422 bmp_colormap=(unsigned char *) RelinquishMagickMemory(bmp_colormap);
2424 if (image->debug != MagickFalse)
2425 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
2426 " Pixels: %u bytes",bmp_info.image_size);
2427 (void) WriteBlob(image,(size_t) bmp_info.image_size,pixels);
2428 if (profile != (StringInfo *) NULL)
2430 if (image->debug != MagickFalse)
2431 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
2432 " Profile: %g bytes",(double) profile_size+profile_size_pad);
2433 (void) WriteBlob(image,(size_t) profile_size,GetStringInfoDatum(profile));
2434 if (profile_size_pad > 0) /* padding for 4 bytes multiple */
2435 (void) WriteBlob(image,(size_t) profile_size_pad,"\0\0\0");
2437 pixel_info=RelinquishVirtualMemory(pixel_info);
2438 if (GetNextImageInList(image) == (Image *) NULL)
2440 image=SyncNextImageInList(image);
2441 status=SetImageProgress(image,SaveImagesTag,scene++,imageListLength);
2442 if (status == MagickFalse)
2444 } while (image_info->adjoin != MagickFalse);
2445 (void) CloseBlob(image);