2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
13 % Read/Write Microsoft Windows Bitmap Image Format %
17 % Glenn Randers-Pehrson %
21 % Copyright 1999-2017 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 % http://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__) || defined(__MINGW64__)
86 #define BI_BITFIELDS 3
88 #undef LCS_CALIBRATED_RBG
89 #define LCS_CALIBRATED_RBG 0
92 #undef LCS_WINDOWS_COLOR_SPACE
93 #define LCS_WINDOWS_COLOR_SPACE 2
95 #define PROFILE_LINKED 3
96 #undef PROFILE_EMBEDDED
97 #define PROFILE_EMBEDDED 4
99 #undef LCS_GM_BUSINESS
100 #define LCS_GM_BUSINESS 1 /* Saturation */
101 #undef LCS_GM_GRAPHICS
102 #define LCS_GM_GRAPHICS 2 /* Relative */
104 #define LCS_GM_IMAGES 4 /* Perceptual */
105 #undef LCS_GM_ABS_COLORIMETRIC
106 #define LCS_GM_ABS_COLORIMETRIC 8 /* Absolute */
110 Enumerated declaractions.
122 Typedef declarations.
124 typedef struct _BMPInfo
163 Forward declarations.
165 static MagickBooleanType
166 WriteBMPImage(const ImageInfo *,Image *,ExceptionInfo *);
169 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
173 % D e c o d e I m a g e %
177 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
179 % DecodeImage unpacks the packed image pixels into runlength-encoded
182 % The format of the DecodeImage method is:
184 % MagickBooleanType DecodeImage(Image *image,
185 % const size_t compression,unsigned char *pixels)
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.
200 static MagickBooleanType DecodeImage(Image *image,const size_t compression,
201 unsigned char *pixels)
210 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,(size_t) image->columns*image->rows*
230 q=pixels+(size_t) image->columns*image->rows;
231 for (y=0; y < (ssize_t) image->rows; )
236 if ((p < pixels) || (p > q))
238 count=ReadBlobByte(image);
246 count=(int) MagickMin((ssize_t) count,(ssize_t) (q-p));
247 byte=(unsigned char) ReadBlobByte(image);
248 if (compression == BI_RLE8)
250 for (i=0; i < (ssize_t) count; i++)
251 *p++=(unsigned char) byte;
255 for (i=0; i < (ssize_t) count; i++)
257 ((i & 0x01) != 0 ? (byte & 0x0f) : ((byte >> 4) & 0x0f));
266 count=ReadBlobByte(image);
280 p=pixels+y*image->columns;
288 x+=ReadBlobByte(image);
289 y+=ReadBlobByte(image);
290 p=pixels+y*image->columns+x;
298 count=(int) MagickMin((ssize_t) count,(ssize_t) (q-p));
299 if (compression == BI_RLE8)
300 for (i=0; i < (ssize_t) count; i++)
301 *p++=(unsigned char) ReadBlobByte(image);
303 for (i=0; i < (ssize_t) count; i++)
306 byte=(unsigned char) ReadBlobByte(image);
308 ((i & 0x01) != 0 ? (byte & 0x0f) : ((byte >> 4) & 0x0f));
314 if (compression == BI_RLE8)
316 if ((count & 0x01) != 0)
317 (void) ReadBlobByte(image);
320 if (((count & 0x03) == 1) || ((count & 0x03) == 2))
321 (void) ReadBlobByte(image);
326 status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y,
328 if (status == MagickFalse)
331 (void) ReadBlobByte(image); /* end of line */
332 (void) ReadBlobByte(image);
333 return(y < (ssize_t) image->rows ? MagickFalse : MagickTrue);
337 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
341 % E n c o d e I m a g e %
345 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
347 % EncodeImage compresses pixels using a runlength encoded format.
349 % The format of the EncodeImage method is:
351 % static MagickBooleanType EncodeImage(Image *image,
352 % const size_t bytes_per_line,const unsigned char *pixels,
353 % unsigned char *compressed_pixels)
355 % A description of each parameter follows:
357 % o image: The image.
359 % o bytes_per_line: the number of bytes in a scanline of compressed pixels
361 % o pixels: The address of a byte (8 bits) array of pixel data created by
362 % the compression process.
364 % o compressed_pixels: The address of a byte (8 bits) array of compressed
368 static size_t EncodeImage(Image *image,const size_t bytes_per_line,
369 const unsigned char *pixels,unsigned char *compressed_pixels)
374 register const unsigned char
381 register unsigned char
388 Runlength encode pixels.
390 assert(image != (Image *) NULL);
391 assert(image->signature == MagickCoreSignature);
392 if (image->debug != MagickFalse)
393 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
394 assert(pixels != (const unsigned char *) NULL);
395 assert(compressed_pixels != (unsigned char *) NULL);
399 for (y=0; y < (ssize_t) image->rows; y++)
401 for (x=0; x < (ssize_t) bytes_per_line; x+=i)
406 for (i=1; ((x+i) < (ssize_t) bytes_per_line); i++)
407 if ((i == 255) || (*(p+i) != *p))
409 *q++=(unsigned char) i;
416 *q++=(unsigned char) 0x00;
417 *q++=(unsigned char) 0x00;
418 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
420 if (status == MagickFalse)
426 *q++=(unsigned char) 0x00;
427 *q++=(unsigned char) 0x01;
428 return((size_t) (q-compressed_pixels));
432 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
440 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
442 % IsBMP() returns MagickTrue if the image format type, identified by the
443 % magick string, is BMP.
445 % The format of the IsBMP method is:
447 % MagickBooleanType IsBMP(const unsigned char *magick,const size_t length)
449 % A description of each parameter follows:
451 % o magick: compare image format pattern against these bytes.
453 % o length: Specifies the length of the magick string.
456 static MagickBooleanType IsBMP(const unsigned char *magick,const size_t length)
460 if ((LocaleNCompare((char *) magick,"BA",2) == 0) ||
461 (LocaleNCompare((char *) magick,"BM",2) == 0) ||
462 (LocaleNCompare((char *) magick,"IC",2) == 0) ||
463 (LocaleNCompare((char *) magick,"PI",2) == 0) ||
464 (LocaleNCompare((char *) magick,"CI",2) == 0) ||
465 (LocaleNCompare((char *) magick,"CP",2) == 0))
471 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
475 % R e a d B M P I m a g e %
479 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
481 % ReadBMPImage() reads a Microsoft Windows bitmap image file, Version
482 % 2, 3 (for Windows or NT), or 4, and returns it. It allocates the memory
483 % necessary for the new Image structure and returns a pointer to the new
486 % The format of the ReadBMPImage method is:
488 % image=ReadBMPImage(image_info)
490 % A description of each parameter follows:
492 % o image_info: the image info.
494 % o exception: return any errors or warnings in this structure.
498 static Image *ReadBMPImage(const ImageInfo *image_info,ExceptionInfo *exception)
526 register unsigned char
548 assert(image_info != (const ImageInfo *) NULL);
549 assert(image_info->signature == MagickCoreSignature);
550 if (image_info->debug != MagickFalse)
551 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
552 image_info->filename);
553 assert(exception != (ExceptionInfo *) NULL);
554 assert(exception->signature == MagickCoreSignature);
555 image=AcquireImage(image_info,exception);
556 status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception);
557 if (status == MagickFalse)
559 image=DestroyImageList(image);
560 return((Image *) NULL);
563 Determine if this a BMP file.
565 (void) ResetMagickMemory(&bmp_info,0,sizeof(bmp_info));
566 bmp_info.ba_offset=0;
568 count=ReadBlob(image,2,magick);
570 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
584 Verify BMP identifier.
586 if (bmp_info.ba_offset == 0)
587 start_position=TellBlob(image)-2;
588 bmp_info.ba_offset=0;
589 while (LocaleNCompare((char *) magick,"BA",2) == 0)
591 bmp_info.file_size=ReadBlobLSBLong(image);
592 bmp_info.ba_offset=ReadBlobLSBLong(image);
593 bmp_info.offset_bits=ReadBlobLSBLong(image);
594 count=ReadBlob(image,2,magick);
598 if (image->debug != MagickFalse)
599 (void) LogMagickEvent(CoderEvent,GetMagickModule()," Magick: %c%c",
600 magick[0],magick[1]);
601 if ((count != 2) || ((LocaleNCompare((char *) magick,"BM",2) != 0) &&
602 (LocaleNCompare((char *) magick,"CI",2) != 0)))
603 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
604 bmp_info.file_size=ReadBlobLSBLong(image);
605 (void) ReadBlobLSBLong(image);
606 bmp_info.offset_bits=ReadBlobLSBLong(image);
607 bmp_info.size=ReadBlobLSBLong(image);
608 if (image->debug != MagickFalse)
609 (void) LogMagickEvent(CoderEvent,GetMagickModule()," BMP size: %lu",
611 if (bmp_info.size == 12)
616 (void) CopyMagickString(image->magick,"BMP2",MagickPathExtent);
617 bmp_info.width=(ssize_t) ((short) ReadBlobLSBShort(image));
618 bmp_info.height=(ssize_t) ((short) ReadBlobLSBShort(image));
619 bmp_info.planes=ReadBlobLSBShort(image);
620 bmp_info.bits_per_pixel=ReadBlobLSBShort(image);
623 bmp_info.number_colors=0;
624 bmp_info.compression=BI_RGB;
625 bmp_info.image_size=0;
626 bmp_info.alpha_mask=0;
627 if (image->debug != MagickFalse)
629 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
630 " Format: OS/2 Bitmap");
631 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
632 " Geometry: %.20gx%.20g",(double) bmp_info.width,(double)
639 Microsoft Windows BMP image file.
641 if (bmp_info.size < 40)
642 ThrowReaderException(CorruptImageError,"NonOS2HeaderSizeError");
643 bmp_info.width=(ssize_t) ReadBlobLSBSignedLong(image);
644 bmp_info.height=(ssize_t) ReadBlobLSBSignedLong(image);
645 bmp_info.planes=ReadBlobLSBShort(image);
646 bmp_info.bits_per_pixel=ReadBlobLSBShort(image);
647 bmp_info.compression=ReadBlobLSBLong(image);
648 bmp_info.image_size=ReadBlobLSBLong(image);
649 bmp_info.x_pixels=ReadBlobLSBLong(image);
650 bmp_info.y_pixels=ReadBlobLSBLong(image);
651 bmp_info.number_colors=ReadBlobLSBLong(image);
652 bmp_info.colors_important=ReadBlobLSBLong(image);
655 if (image->debug != MagickFalse)
657 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
658 " Format: MS Windows bitmap");
659 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
660 " Geometry: %.20gx%.20g",(double) bmp_info.width,(double)
662 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
663 " Bits per pixel: %.20g",(double) bmp_info.bits_per_pixel);
664 switch ((int) bmp_info.compression)
668 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
669 " Compression: BI_RGB");
674 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
675 " Compression: BI_RLE4");
680 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
681 " Compression: BI_RLE8");
686 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
687 " Compression: BI_BITFIELDS");
692 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
693 " Compression: BI_PNG");
698 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
699 " Compression: BI_JPEG");
704 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
705 " Compression: UNKNOWN (%lu)",bmp_info.compression);
708 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
709 " Number of colors: %lu",bmp_info.number_colors);
711 bmp_info.red_mask=ReadBlobLSBLong(image);
712 bmp_info.green_mask=ReadBlobLSBLong(image);
713 bmp_info.blue_mask=ReadBlobLSBLong(image);
714 if (bmp_info.size > 40)
720 Read color management information.
722 bmp_info.alpha_mask=ReadBlobLSBLong(image);
723 bmp_info.colorspace=ReadBlobLSBSignedLong(image);
725 Decode 2^30 fixed point formatted CIE primaries.
727 # define BMP_DENOM ((double) 0x40000000)
728 bmp_info.red_primary.x=(double) ReadBlobLSBLong(image)/BMP_DENOM;
729 bmp_info.red_primary.y=(double) ReadBlobLSBLong(image)/BMP_DENOM;
730 bmp_info.red_primary.z=(double) ReadBlobLSBLong(image)/BMP_DENOM;
731 bmp_info.green_primary.x=(double) ReadBlobLSBLong(image)/BMP_DENOM;
732 bmp_info.green_primary.y=(double) ReadBlobLSBLong(image)/BMP_DENOM;
733 bmp_info.green_primary.z=(double) ReadBlobLSBLong(image)/BMP_DENOM;
734 bmp_info.blue_primary.x=(double) ReadBlobLSBLong(image)/BMP_DENOM;
735 bmp_info.blue_primary.y=(double) ReadBlobLSBLong(image)/BMP_DENOM;
736 bmp_info.blue_primary.z=(double) ReadBlobLSBLong(image)/BMP_DENOM;
738 gamma=bmp_info.red_primary.x+bmp_info.red_primary.y+
739 bmp_info.red_primary.z;
740 gamma=PerceptibleReciprocal(gamma);
741 bmp_info.red_primary.x*=gamma;
742 bmp_info.red_primary.y*=gamma;
743 image->chromaticity.red_primary.x=bmp_info.red_primary.x;
744 image->chromaticity.red_primary.y=bmp_info.red_primary.y;
746 gamma=bmp_info.green_primary.x+bmp_info.green_primary.y+
747 bmp_info.green_primary.z;
748 gamma=PerceptibleReciprocal(gamma);
749 bmp_info.green_primary.x*=gamma;
750 bmp_info.green_primary.y*=gamma;
751 image->chromaticity.green_primary.x=bmp_info.green_primary.x;
752 image->chromaticity.green_primary.y=bmp_info.green_primary.y;
754 gamma=bmp_info.blue_primary.x+bmp_info.blue_primary.y+
755 bmp_info.blue_primary.z;
756 gamma=PerceptibleReciprocal(gamma);
757 bmp_info.blue_primary.x*=gamma;
758 bmp_info.blue_primary.y*=gamma;
759 image->chromaticity.blue_primary.x=bmp_info.blue_primary.x;
760 image->chromaticity.blue_primary.y=bmp_info.blue_primary.y;
763 Decode 16^16 fixed point formatted gamma_scales.
765 bmp_info.gamma_scale.x=(double) ReadBlobLSBLong(image)/0x10000;
766 bmp_info.gamma_scale.y=(double) ReadBlobLSBLong(image)/0x10000;
767 bmp_info.gamma_scale.z=(double) ReadBlobLSBLong(image)/0x10000;
769 Compute a single gamma from the BMP 3-channel gamma.
771 image->gamma=(bmp_info.gamma_scale.x+bmp_info.gamma_scale.y+
772 bmp_info.gamma_scale.z)/3.0;
775 (void) CopyMagickString(image->magick,"BMP3",MagickPathExtent);
777 if (bmp_info.size > 108)
783 Read BMP Version 5 color management information.
785 intent=ReadBlobLSBLong(image);
786 switch ((int) intent)
788 case LCS_GM_BUSINESS:
790 image->rendering_intent=SaturationIntent;
793 case LCS_GM_GRAPHICS:
795 image->rendering_intent=RelativeIntent;
800 image->rendering_intent=PerceptualIntent;
803 case LCS_GM_ABS_COLORIMETRIC:
805 image->rendering_intent=AbsoluteIntent;
809 profile_data=ReadBlobLSBLong(image);
810 profile_size=ReadBlobLSBLong(image);
813 (void) ReadBlobLSBLong(image); /* Reserved byte */
816 if ((MagickSizeType) bmp_info.file_size > GetBlobSize(image))
817 (void) ThrowMagickException(exception,GetMagickModule(),CorruptImageError,
818 "LengthAndFilesizeDoNotMatch","`%s'",image->filename);
820 if ((MagickSizeType) bmp_info.file_size < GetBlobSize(image))
821 (void) ThrowMagickException(exception,GetMagickModule(),
822 CorruptImageWarning,"LengthAndFilesizeDoNotMatch","`%s'",
824 if (bmp_info.width <= 0)
825 ThrowReaderException(CorruptImageError,"NegativeOrZeroImageSize");
826 if (bmp_info.height == 0)
827 ThrowReaderException(CorruptImageError,"NegativeOrZeroImageSize");
828 if (bmp_info.planes != 1)
829 ThrowReaderException(CorruptImageError,"StaticPlanesValueNotEqualToOne");
830 if ((bmp_info.bits_per_pixel != 1) && (bmp_info.bits_per_pixel != 4) &&
831 (bmp_info.bits_per_pixel != 8) && (bmp_info.bits_per_pixel != 16) &&
832 (bmp_info.bits_per_pixel != 24) && (bmp_info.bits_per_pixel != 32))
833 ThrowReaderException(CorruptImageError,"UnrecognizedBitsPerPixel");
834 if (bmp_info.bits_per_pixel < 16 &&
835 bmp_info.number_colors > (1U << bmp_info.bits_per_pixel))
836 ThrowReaderException(CorruptImageError,"UnrecognizedNumberOfColors");
837 if ((bmp_info.compression == 1) && (bmp_info.bits_per_pixel != 8))
838 ThrowReaderException(CorruptImageError,"UnrecognizedBitsPerPixel");
839 if ((bmp_info.compression == 2) && (bmp_info.bits_per_pixel != 4))
840 ThrowReaderException(CorruptImageError,"UnrecognizedBitsPerPixel");
841 if ((bmp_info.compression == 3) && (bmp_info.bits_per_pixel < 16))
842 ThrowReaderException(CorruptImageError,"UnrecognizedBitsPerPixel");
843 switch (bmp_info.compression)
846 image->compression=NoCompression;
850 image->compression=RLECompression;
855 ThrowReaderException(CoderError,"JPEGCompressNotSupported");
857 ThrowReaderException(CoderError,"PNGCompressNotSupported");
859 ThrowReaderException(CorruptImageError,"UnrecognizedImageCompression");
861 image->columns=(size_t) MagickAbsoluteValue(bmp_info.width);
862 image->rows=(size_t) MagickAbsoluteValue(bmp_info.height);
863 image->depth=bmp_info.bits_per_pixel <= 8 ? bmp_info.bits_per_pixel : 8;
864 image->alpha_trait=((bmp_info.alpha_mask != 0) &&
865 (bmp_info.compression == BI_BITFIELDS)) ? BlendPixelTrait :
867 if (bmp_info.bits_per_pixel < 16)
872 image->storage_class=PseudoClass;
873 image->colors=bmp_info.number_colors;
875 if (image->colors == 0)
876 image->colors=one << bmp_info.bits_per_pixel;
878 if (image->storage_class == PseudoClass)
887 Read BMP raster colormap.
889 if (image->debug != MagickFalse)
890 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
891 " Reading colormap of %.20g colors",(double) image->colors);
892 if (AcquireImageColormap(image,image->colors,exception) == MagickFalse)
893 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
894 bmp_colormap=(unsigned char *) AcquireQuantumMemory((size_t)
895 image->colors,4*sizeof(*bmp_colormap));
896 if (bmp_colormap == (unsigned char *) NULL)
897 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
898 if ((bmp_info.size == 12) || (bmp_info.size == 64))
902 offset=SeekBlob(image,start_position+14+bmp_info.size,SEEK_SET);
904 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
905 count=ReadBlob(image,packet_size*image->colors,bmp_colormap);
906 if (count != (ssize_t) (packet_size*image->colors))
907 ThrowReaderException(CorruptImageError,"InsufficientImageDataInFile");
909 for (i=0; i < (ssize_t) image->colors; i++)
911 image->colormap[i].blue=(MagickRealType) ScaleCharToQuantum(*p++);
912 image->colormap[i].green=(MagickRealType) ScaleCharToQuantum(*p++);
913 image->colormap[i].red=(MagickRealType) ScaleCharToQuantum(*p++);
914 if (packet_size == 4)
917 bmp_colormap=(unsigned char *) RelinquishMagickMemory(bmp_colormap);
919 image->resolution.x=(double) bmp_info.x_pixels/100.0;
920 image->resolution.y=(double) bmp_info.y_pixels/100.0;
921 image->units=PixelsPerCentimeterResolution;
922 if ((image_info->ping != MagickFalse) && (image_info->number_scenes != 0))
923 if (image->scene >= (image_info->scene+image_info->number_scenes-1))
925 status=SetImageExtent(image,image->columns,image->rows,exception);
926 if (status == MagickFalse)
927 return(DestroyImageList(image));
931 offset=SeekBlob(image,start_position+bmp_info.offset_bits,SEEK_SET);
933 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
934 if (bmp_info.compression == BI_RLE4)
935 bmp_info.bits_per_pixel<<=1;
936 bytes_per_line=4*((image->columns*bmp_info.bits_per_pixel+31)/32);
937 length=(size_t) bytes_per_line*image->rows;
938 pixel_info=AcquireVirtualMemory((size_t) image->rows,
939 MagickMax(bytes_per_line,image->columns+256UL)*sizeof(*pixels));
940 if (pixel_info == (MemoryInfo *) NULL)
941 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
942 pixels=(unsigned char *) GetVirtualMemoryBlob(pixel_info);
943 if ((bmp_info.compression == BI_RGB) ||
944 (bmp_info.compression == BI_BITFIELDS))
946 if (image->debug != MagickFalse)
947 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
948 " Reading pixels (%.20g bytes)",(double) length);
949 count=ReadBlob(image,length,pixels);
950 if (count != (ssize_t) length)
952 pixel_info=RelinquishVirtualMemory(pixel_info);
953 ThrowReaderException(CorruptImageError,
954 "InsufficientImageDataInFile");
960 Convert run-length encoded raster pixels.
962 status=DecodeImage(image,bmp_info.compression,pixels);
963 if (status == MagickFalse)
965 pixel_info=RelinquishVirtualMemory(pixel_info);
966 ThrowReaderException(CorruptImageError,
967 "UnableToRunlengthDecodeImage");
971 Convert BMP raster image to pixel packets.
973 if (bmp_info.compression == BI_RGB)
976 We should ignore the alpha value in BMP3 files but there have been
977 reports about 32 bit files with alpha. We do a quick check to see if
978 the alpha channel contains a value that is not zero (default value).
979 If we find a non zero value we asume the program that wrote the file
980 wants to use the alpha channel.
982 if ((image->alpha_trait == UndefinedPixelTrait) && (bmp_info.size == 40) &&
983 (bmp_info.bits_per_pixel == 32))
985 bytes_per_line=4*(image->columns);
986 for (y=(ssize_t) image->rows-1; y >= 0; y--)
988 p=pixels+(image->rows-y-1)*bytes_per_line;
989 for (x=0; x < (ssize_t) image->columns; x++)
993 image->alpha_trait=BlendPixelTrait;
1001 bmp_info.alpha_mask=image->alpha_trait != UndefinedPixelTrait ?
1003 bmp_info.red_mask=0x00ff0000U;
1004 bmp_info.green_mask=0x0000ff00U;
1005 bmp_info.blue_mask=0x000000ffU;
1006 if (bmp_info.bits_per_pixel == 16)
1011 bmp_info.red_mask=0x00007c00U;
1012 bmp_info.green_mask=0x000003e0U;
1013 bmp_info.blue_mask=0x0000001fU;
1016 (void) ResetMagickMemory(&shift,0,sizeof(shift));
1017 (void) ResetMagickMemory(&quantum_bits,0,sizeof(quantum_bits));
1018 if ((bmp_info.bits_per_pixel == 16) || (bmp_info.bits_per_pixel == 32))
1024 Get shift and quantum bits info from bitfield masks.
1026 if (bmp_info.red_mask != 0)
1027 while (((bmp_info.red_mask << shift.red) & 0x80000000UL) == 0)
1029 if (bmp_info.green_mask != 0)
1030 while (((bmp_info.green_mask << shift.green) & 0x80000000UL) == 0)
1032 if (bmp_info.blue_mask != 0)
1033 while (((bmp_info.blue_mask << shift.blue) & 0x80000000UL) == 0)
1035 if (bmp_info.alpha_mask != 0)
1036 while (((bmp_info.alpha_mask << shift.alpha) & 0x80000000UL) == 0)
1039 while (((bmp_info.red_mask << sample) & 0x80000000UL) != 0)
1041 quantum_bits.red=(MagickRealType) (sample-shift.red);
1043 while (((bmp_info.green_mask << sample) & 0x80000000UL) != 0)
1045 quantum_bits.green=(MagickRealType) (sample-shift.green);
1047 while (((bmp_info.blue_mask << sample) & 0x80000000UL) != 0)
1049 quantum_bits.blue=(MagickRealType) (sample-shift.blue);
1051 while (((bmp_info.alpha_mask << sample) & 0x80000000UL) != 0)
1053 quantum_bits.alpha=(MagickRealType) (sample-shift.alpha);
1055 switch (bmp_info.bits_per_pixel)
1060 Convert bitmap scanline.
1062 for (y=(ssize_t) image->rows-1; y >= 0; y--)
1064 p=pixels+(image->rows-y-1)*bytes_per_line;
1065 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
1066 if (q == (Quantum *) NULL)
1068 for (x=0; x < ((ssize_t) image->columns-7); x+=8)
1070 for (bit=0; bit < 8; bit++)
1072 index=(Quantum) (((*p) & (0x80 >> bit)) != 0 ? 0x01 : 0x00);
1073 SetPixelIndex(image,index,q);
1074 q+=GetPixelChannels(image);
1078 if ((image->columns % 8) != 0)
1080 for (bit=0; bit < (image->columns % 8); bit++)
1082 index=(Quantum) (((*p) & (0x80 >> bit)) != 0 ? 0x01 : 0x00);
1083 SetPixelIndex(image,index,q);
1084 q+=GetPixelChannels(image);
1088 if (SyncAuthenticPixels(image,exception) == MagickFalse)
1090 if (image->previous == (Image *) NULL)
1092 status=SetImageProgress(image,LoadImageTag,(MagickOffsetType)
1093 (image->rows-y),image->rows);
1094 if (status == MagickFalse)
1098 (void) SyncImage(image,exception);
1104 Convert PseudoColor scanline.
1106 for (y=(ssize_t) image->rows-1; y >= 0; y--)
1108 p=pixels+(image->rows-y-1)*bytes_per_line;
1109 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
1110 if (q == (Quantum *) NULL)
1112 for (x=0; x < ((ssize_t) image->columns-1); x+=2)
1114 ValidateColormapValue(image,(*p >> 4) & 0x0f,&index,exception);
1115 SetPixelIndex(image,index,q);
1116 q+=GetPixelChannels(image);
1117 ValidateColormapValue(image,*p & 0x0f,&index,exception);
1118 SetPixelIndex(image,index,q);
1119 q+=GetPixelChannels(image);
1122 if ((image->columns % 2) != 0)
1124 ValidateColormapValue(image,(*p >> 4) & 0xf,&index,exception);
1125 SetPixelIndex(image,index,q);
1126 q+=GetPixelChannels(image);
1130 if (x < (ssize_t) image->columns)
1132 if (SyncAuthenticPixels(image,exception) == MagickFalse)
1134 if (image->previous == (Image *) NULL)
1136 status=SetImageProgress(image,LoadImageTag,(MagickOffsetType)
1137 (image->rows-y),image->rows);
1138 if (status == MagickFalse)
1142 (void) SyncImage(image,exception);
1148 Convert PseudoColor scanline.
1150 if ((bmp_info.compression == BI_RLE8) ||
1151 (bmp_info.compression == BI_RLE4))
1152 bytes_per_line=image->columns;
1153 for (y=(ssize_t) image->rows-1; y >= 0; y--)
1155 p=pixels+(image->rows-y-1)*bytes_per_line;
1156 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
1157 if (q == (Quantum *) NULL)
1159 for (x=(ssize_t) image->columns; x != 0; --x)
1161 ValidateColormapValue(image,*p++,&index,exception);
1162 SetPixelIndex(image,index,q);
1163 q+=GetPixelChannels(image);
1165 if (SyncAuthenticPixels(image,exception) == MagickFalse)
1167 offset=(MagickOffsetType) (image->rows-y-1);
1168 if (image->previous == (Image *) NULL)
1170 status=SetImageProgress(image,LoadImageTag,(MagickOffsetType)
1171 (image->rows-y),image->rows);
1172 if (status == MagickFalse)
1176 (void) SyncImage(image,exception);
1186 Convert bitfield encoded 16-bit PseudoColor scanline.
1188 if (bmp_info.compression != BI_RGB &&
1189 bmp_info.compression != BI_BITFIELDS)
1191 pixel_info=RelinquishVirtualMemory(pixel_info);
1192 ThrowReaderException(CorruptImageError,
1193 "UnrecognizedImageCompression");
1195 bytes_per_line=2*(image->columns+image->columns % 2);
1196 image->storage_class=DirectClass;
1197 for (y=(ssize_t) image->rows-1; y >= 0; y--)
1199 p=pixels+(image->rows-y-1)*bytes_per_line;
1200 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
1201 if (q == (Quantum *) NULL)
1203 for (x=0; x < (ssize_t) image->columns; x++)
1205 pixel=(size_t) (*p++);
1207 red=((pixel & bmp_info.red_mask) << shift.red) >> 16;
1208 if (quantum_bits.red == 5)
1209 red|=((red & 0xe000) >> 5);
1210 if (quantum_bits.red <= 8)
1211 red|=((red & 0xff00) >> 8);
1212 green=((pixel & bmp_info.green_mask) << shift.green) >> 16;
1213 if (quantum_bits.green == 5)
1214 green|=((green & 0xe000) >> 5);
1215 if (quantum_bits.green == 6)
1216 green|=((green & 0xc000) >> 6);
1217 if (quantum_bits.green <= 8)
1218 green|=((green & 0xff00) >> 8);
1219 blue=((pixel & bmp_info.blue_mask) << shift.blue) >> 16;
1220 if (quantum_bits.blue == 5)
1221 blue|=((blue & 0xe000) >> 5);
1222 if (quantum_bits.blue <= 8)
1223 blue|=((blue & 0xff00) >> 8);
1224 SetPixelRed(image,ScaleShortToQuantum((unsigned short) red),q);
1225 SetPixelGreen(image,ScaleShortToQuantum((unsigned short) green),q);
1226 SetPixelBlue(image,ScaleShortToQuantum((unsigned short) blue),q);
1227 SetPixelAlpha(image,OpaqueAlpha,q);
1228 if (image->alpha_trait != UndefinedPixelTrait)
1230 alpha=((pixel & bmp_info.alpha_mask) << shift.alpha) >> 16;
1231 if (quantum_bits.alpha <= 8)
1232 alpha|=((alpha & 0xff00) >> 8);
1233 SetPixelAlpha(image,ScaleShortToQuantum(
1234 (unsigned short) alpha),q);
1236 q+=GetPixelChannels(image);
1238 if (SyncAuthenticPixels(image,exception) == MagickFalse)
1240 offset=(MagickOffsetType) (image->rows-y-1);
1241 if (image->previous == (Image *) NULL)
1243 status=SetImageProgress(image,LoadImageTag,(MagickOffsetType)
1244 (image->rows-y),image->rows);
1245 if (status == MagickFalse)
1254 Convert DirectColor scanline.
1256 bytes_per_line=4*((image->columns*24+31)/32);
1257 for (y=(ssize_t) image->rows-1; y >= 0; y--)
1259 p=pixels+(image->rows-y-1)*bytes_per_line;
1260 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
1261 if (q == (Quantum *) NULL)
1263 for (x=0; x < (ssize_t) image->columns; x++)
1265 SetPixelBlue(image,ScaleCharToQuantum(*p++),q);
1266 SetPixelGreen(image,ScaleCharToQuantum(*p++),q);
1267 SetPixelRed(image,ScaleCharToQuantum(*p++),q);
1268 SetPixelAlpha(image,OpaqueAlpha,q);
1269 q+=GetPixelChannels(image);
1271 if (SyncAuthenticPixels(image,exception) == MagickFalse)
1273 offset=(MagickOffsetType) (image->rows-y-1);
1274 if (image->previous == (Image *) NULL)
1276 status=SetImageProgress(image,LoadImageTag,(MagickOffsetType)
1277 (image->rows-y),image->rows);
1278 if (status == MagickFalse)
1287 Convert bitfield encoded DirectColor scanline.
1289 if ((bmp_info.compression != BI_RGB) &&
1290 (bmp_info.compression != BI_BITFIELDS))
1292 pixel_info=RelinquishVirtualMemory(pixel_info);
1293 ThrowReaderException(CorruptImageError,
1294 "UnrecognizedImageCompression");
1296 bytes_per_line=4*(image->columns);
1297 for (y=(ssize_t) image->rows-1; y >= 0; y--)
1303 p=pixels+(image->rows-y-1)*bytes_per_line;
1304 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
1305 if (q == (Quantum *) NULL)
1307 for (x=0; x < (ssize_t) image->columns; x++)
1309 pixel=(size_t) (*p++);
1310 pixel|=((size_t) *p++ << 8);
1311 pixel|=((size_t) *p++ << 16);
1312 pixel|=((size_t) *p++ << 24);
1313 red=((pixel & bmp_info.red_mask) << shift.red) >> 16;
1314 if (quantum_bits.red == 8)
1316 green=((pixel & bmp_info.green_mask) << shift.green) >> 16;
1317 if (quantum_bits.green == 8)
1318 green|=(green >> 8);
1319 blue=((pixel & bmp_info.blue_mask) << shift.blue) >> 16;
1320 if (quantum_bits.blue == 8)
1322 SetPixelRed(image,ScaleShortToQuantum((unsigned short) red),q);
1323 SetPixelGreen(image,ScaleShortToQuantum((unsigned short) green),q);
1324 SetPixelBlue(image,ScaleShortToQuantum((unsigned short) blue),q);
1325 SetPixelAlpha(image,OpaqueAlpha,q);
1326 if (image->alpha_trait != UndefinedPixelTrait)
1328 alpha=((pixel & bmp_info.alpha_mask) << shift.alpha) >> 16;
1329 if (quantum_bits.alpha == 8)
1330 alpha|=(alpha >> 8);
1331 SetPixelAlpha(image,ScaleShortToQuantum(
1332 (unsigned short) alpha),q);
1334 q+=GetPixelChannels(image);
1336 if (SyncAuthenticPixels(image,exception) == MagickFalse)
1338 offset=(MagickOffsetType) (image->rows-y-1);
1339 if (image->previous == (Image *) NULL)
1341 status=SetImageProgress(image,LoadImageTag,(MagickOffsetType)
1342 (image->rows-y),image->rows);
1343 if (status == MagickFalse)
1351 pixel_info=RelinquishVirtualMemory(pixel_info);
1352 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
1355 pixel_info=RelinquishVirtualMemory(pixel_info);
1358 if (EOFBlob(image) != MagickFalse)
1360 ThrowFileException(exception,CorruptImageError,"UnexpectedEndOfFile",
1364 if (bmp_info.height < 0)
1370 Correct image orientation.
1372 flipped_image=FlipImage(image,exception);
1373 if (flipped_image != (Image *) NULL)
1375 DuplicateBlob(flipped_image,image);
1376 image=DestroyImage(image);
1377 image=flipped_image;
1381 Proceed to next image.
1383 if (image_info->number_scenes != 0)
1384 if (image->scene >= (image_info->scene+image_info->number_scenes-1))
1387 if (bmp_info.ba_offset != 0)
1389 offset=SeekBlob(image,(MagickOffsetType) bmp_info.ba_offset,SEEK_SET);
1391 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
1393 count=ReadBlob(image,2,magick);
1394 if ((count == 2) && (IsBMP(magick,2) != MagickFalse))
1397 Acquire next image structure.
1399 AcquireNextImage(image_info,image,exception);
1400 if (GetNextImageInList(image) == (Image *) NULL)
1402 image=DestroyImageList(image);
1403 return((Image *) NULL);
1405 image=SyncNextImageInList(image);
1406 status=SetImageProgress(image,LoadImagesTag,TellBlob(image),
1407 GetBlobSize(image));
1408 if (status == MagickFalse)
1411 } while (IsBMP(magick,2) != MagickFalse);
1412 (void) CloseBlob(image);
1413 return(GetFirstImageInList(image));
1417 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1421 % R e g i s t e r B M P I m a g e %
1425 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1427 % RegisterBMPImage() adds attributes for the BMP image format to
1428 % the list of supported formats. The attributes include the image format
1429 % tag, a method to read and/or write the format, whether the format
1430 % supports the saving of more than one frame to the same file or blob,
1431 % whether the format supports native in-memory I/O, and a brief
1432 % description of the format.
1434 % The format of the RegisterBMPImage method is:
1436 % size_t RegisterBMPImage(void)
1439 ModuleExport size_t RegisterBMPImage(void)
1444 entry=AcquireMagickInfo("BMP","BMP","Microsoft Windows bitmap image");
1445 entry->decoder=(DecodeImageHandler *) ReadBMPImage;
1446 entry->encoder=(EncodeImageHandler *) WriteBMPImage;
1447 entry->magick=(IsImageFormatHandler *) IsBMP;
1448 entry->flags^=CoderAdjoinFlag;
1449 entry->flags|=CoderDecoderSeekableStreamFlag;
1450 (void) RegisterMagickInfo(entry);
1451 entry=AcquireMagickInfo("BMP","BMP2","Microsoft Windows bitmap image (V2)");
1452 entry->encoder=(EncodeImageHandler *) WriteBMPImage;
1453 entry->magick=(IsImageFormatHandler *) IsBMP;
1454 entry->flags^=CoderAdjoinFlag;
1455 entry->flags|=CoderDecoderSeekableStreamFlag;
1456 (void) RegisterMagickInfo(entry);
1457 entry=AcquireMagickInfo("BMP","BMP3","Microsoft Windows bitmap image (V3)");
1458 entry->encoder=(EncodeImageHandler *) WriteBMPImage;
1459 entry->magick=(IsImageFormatHandler *) IsBMP;
1460 entry->flags^=CoderAdjoinFlag;
1461 entry->flags|=CoderDecoderSeekableStreamFlag;
1462 (void) RegisterMagickInfo(entry);
1463 return(MagickImageCoderSignature);
1467 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1471 % U n r e g i s t e r B M P I m a g e %
1475 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1477 % UnregisterBMPImage() removes format registrations made by the
1478 % BMP module from the list of supported formats.
1480 % The format of the UnregisterBMPImage method is:
1482 % UnregisterBMPImage(void)
1485 ModuleExport void UnregisterBMPImage(void)
1487 (void) UnregisterMagickInfo("BMP");
1488 (void) UnregisterMagickInfo("BMP2");
1489 (void) UnregisterMagickInfo("BMP3");
1493 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1497 % W r i t e B M P I m a g e %
1501 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1503 % WriteBMPImage() writes an image in Microsoft Windows bitmap encoded
1504 % image format, version 3 for Windows or (if the image has a matte channel)
1507 % The format of the WriteBMPImage method is:
1509 % MagickBooleanType WriteBMPImage(const ImageInfo *image_info,
1510 % Image *image,ExceptionInfo *exception)
1512 % A description of each parameter follows.
1514 % o image_info: the image info.
1516 % o image: The image.
1518 % o exception: return any errors or warnings in this structure.
1521 static MagickBooleanType WriteBMPImage(const ImageInfo *image_info,Image *image,
1522 ExceptionInfo *exception)
1546 register const Quantum
1553 register unsigned char
1568 Open output image file.
1570 assert(image_info != (const ImageInfo *) NULL);
1571 assert(image_info->signature == MagickCoreSignature);
1572 assert(image != (Image *) NULL);
1573 assert(image->signature == MagickCoreSignature);
1574 if (image->debug != MagickFalse)
1575 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1576 assert(exception != (ExceptionInfo *) NULL);
1577 assert(exception->signature == MagickCoreSignature);
1578 status=OpenBlob(image_info,image,WriteBinaryBlobMode,exception);
1579 if (status == MagickFalse)
1582 if (LocaleCompare(image_info->magick,"BMP2") == 0)
1585 if (LocaleCompare(image_info->magick,"BMP3") == 0)
1588 option=GetImageOption(image_info,"bmp:format");
1589 if (option != (char *) NULL)
1591 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1592 " Format=%s",option);
1594 if (LocaleCompare(option,"bmp2") == 0)
1596 if (LocaleCompare(option,"bmp3") == 0)
1598 if (LocaleCompare(option,"bmp4") == 0)
1606 Initialize BMP raster file header.
1608 (void) TransformImageColorspace(image,sRGBColorspace,exception);
1609 (void) ResetMagickMemory(&bmp_info,0,sizeof(bmp_info));
1610 bmp_info.file_size=14+12;
1612 bmp_info.file_size+=28;
1613 bmp_info.offset_bits=bmp_info.file_size;
1614 bmp_info.compression=BI_RGB;
1615 bmp_info.red_mask=0x00ff0000U;
1616 bmp_info.green_mask=0x0000ff00U;
1617 bmp_info.blue_mask=0x000000ffU;
1618 bmp_info.alpha_mask=0xff000000U;
1619 bmp_subtype=UndefinedSubtype;
1620 if ((image->storage_class == PseudoClass) && (image->colors > 256))
1621 (void) SetImageStorageClass(image,DirectClass,exception);
1622 if (image->storage_class != DirectClass)
1625 Colormapped BMP raster.
1627 bmp_info.bits_per_pixel=8;
1628 if (image->colors <= 2)
1629 bmp_info.bits_per_pixel=1;
1631 if (image->colors <= 16)
1632 bmp_info.bits_per_pixel=4;
1634 if (image->colors <= 256)
1635 bmp_info.bits_per_pixel=8;
1636 if (image_info->compression == RLECompression)
1637 bmp_info.bits_per_pixel=8;
1638 bmp_info.number_colors=1U << bmp_info.bits_per_pixel;
1639 if (image->alpha_trait != UndefinedPixelTrait)
1640 (void) SetImageStorageClass(image,DirectClass,exception);
1642 if ((size_t) bmp_info.number_colors < image->colors)
1643 (void) SetImageStorageClass(image,DirectClass,exception);
1646 bmp_info.file_size+=3*(1UL << bmp_info.bits_per_pixel);
1647 bmp_info.offset_bits+=3*(1UL << bmp_info.bits_per_pixel);
1650 bmp_info.file_size+=(1UL << bmp_info.bits_per_pixel);
1651 bmp_info.offset_bits+=(1UL << bmp_info.bits_per_pixel);
1655 if (image->storage_class == DirectClass)
1658 Full color BMP raster.
1660 bmp_info.number_colors=0;
1661 option=GetImageOption(image_info,"bmp:subtype");
1662 if (option != (const char *) NULL)
1664 if (image->alpha_trait != UndefinedPixelTrait)
1666 if (LocaleNCompare(option,"ARGB4444",8) == 0)
1668 bmp_subtype=ARGB4444;
1669 bmp_info.red_mask=0x00000f00U;
1670 bmp_info.green_mask=0x000000f0U;
1671 bmp_info.blue_mask=0x0000000fU;
1672 bmp_info.alpha_mask=0x0000f000U;
1674 else if (LocaleNCompare(option,"ARGB1555",8) == 0)
1676 bmp_subtype=ARGB1555;
1677 bmp_info.red_mask=0x00007c00U;
1678 bmp_info.green_mask=0x000003e0U;
1679 bmp_info.blue_mask=0x0000001fU;
1680 bmp_info.alpha_mask=0x00008000U;
1685 if (LocaleNCompare(option,"RGB555",6) == 0)
1688 bmp_info.red_mask=0x00007c00U;
1689 bmp_info.green_mask=0x000003e0U;
1690 bmp_info.blue_mask=0x0000001fU;
1691 bmp_info.alpha_mask=0U;
1693 else if (LocaleNCompare(option,"RGB565",6) == 0)
1696 bmp_info.red_mask=0x0000f800U;
1697 bmp_info.green_mask=0x000007e0U;
1698 bmp_info.blue_mask=0x0000001fU;
1699 bmp_info.alpha_mask=0U;
1703 if (bmp_subtype != UndefinedSubtype)
1705 bmp_info.bits_per_pixel=16;
1706 bmp_info.compression=BI_BITFIELDS;
1710 bmp_info.bits_per_pixel=(unsigned short) ((type > 3) &&
1711 (image->alpha_trait != UndefinedPixelTrait) ? 32 : 24);
1712 bmp_info.compression=(unsigned int) ((type > 3) &&
1713 (image->alpha_trait != UndefinedPixelTrait) ? BI_BITFIELDS : BI_RGB);
1714 if ((type == 3) && (image->alpha_trait != UndefinedPixelTrait))
1716 option=GetImageOption(image_info,"bmp3:alpha");
1717 if (IsStringTrue(option))
1718 bmp_info.bits_per_pixel=32;
1722 bytes_per_line=4*((image->columns*bmp_info.bits_per_pixel+31)/32);
1723 bmp_info.ba_offset=0;
1724 profile=GetImageProfile(image,"icc");
1725 have_color_info=(image->rendering_intent != UndefinedIntent) ||
1726 (profile != (StringInfo *) NULL) || (image->gamma != 0.0) ? MagickTrue :
1731 if ((type == 3) || ((image->alpha_trait == UndefinedPixelTrait) &&
1732 (have_color_info == MagickFalse)))
1744 if ((image->rendering_intent != UndefinedIntent) ||
1745 (profile != (StringInfo *) NULL))
1750 bmp_info.file_size+=extra_size;
1751 bmp_info.offset_bits+=extra_size;
1753 if (((ssize_t) image->columns != (signed int) image->columns) ||
1754 ((ssize_t) image->rows != (signed int) image->rows))
1755 ThrowWriterException(ImageError,"WidthOrHeightExceedsLimit");
1756 bmp_info.width=(ssize_t) image->columns;
1757 bmp_info.height=(ssize_t) image->rows;
1759 bmp_info.image_size=(unsigned long) (bytes_per_line*image->rows);
1760 bmp_info.file_size+=bmp_info.image_size;
1761 bmp_info.x_pixels=75*39;
1762 bmp_info.y_pixels=75*39;
1763 switch (image->units)
1765 case UndefinedResolution:
1766 case PixelsPerInchResolution:
1768 bmp_info.x_pixels=(unsigned int) (100.0*image->resolution.x/2.54);
1769 bmp_info.y_pixels=(unsigned int) (100.0*image->resolution.y/2.54);
1772 case PixelsPerCentimeterResolution:
1774 bmp_info.x_pixels=(unsigned int) (100.0*image->resolution.x);
1775 bmp_info.y_pixels=(unsigned int) (100.0*image->resolution.y);
1779 bmp_info.colors_important=bmp_info.number_colors;
1781 Convert MIFF to BMP raster pixels.
1783 pixel_info=AcquireVirtualMemory((size_t) bmp_info.image_size,
1785 if (pixel_info == (MemoryInfo *) NULL)
1786 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
1787 pixels=(unsigned char *) GetVirtualMemoryBlob(pixel_info);
1788 (void) ResetMagickMemory(pixels,0,(size_t) bmp_info.image_size);
1789 switch (bmp_info.bits_per_pixel)
1798 Convert PseudoClass image to a BMP monochrome image.
1800 for (y=0; y < (ssize_t) image->rows; y++)
1805 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
1806 if (p == (const Quantum *) NULL)
1808 q=pixels+(image->rows-y-1)*bytes_per_line;
1811 for (x=0; x < (ssize_t) image->columns; x++)
1814 byte|=GetPixelIndex(image,p) != 0 ? 0x01 : 0x00;
1818 *q++=(unsigned char) byte;
1822 p+=GetPixelChannels(image);
1826 *q++=(unsigned char) (byte << (8-bit));
1829 offset=(ssize_t) (image->columns+7)/8;
1830 for (x=offset; x < (ssize_t) bytes_per_line; x++)
1832 if (image->previous == (Image *) NULL)
1834 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
1836 if (status == MagickFalse)
1852 Convert PseudoClass image to a BMP monochrome image.
1854 for (y=0; y < (ssize_t) image->rows; y++)
1856 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
1857 if (p == (const Quantum *) NULL)
1859 q=pixels+(image->rows-y-1)*bytes_per_line;
1862 for (x=0; x < (ssize_t) image->columns; x++)
1865 byte|=((size_t) GetPixelIndex(image,p) & 0x0f);
1869 *q++=(unsigned char) byte;
1873 p+=GetPixelChannels(image);
1877 *q++=(unsigned char) (byte << 4);
1880 offset=(ssize_t) (image->columns+1)/2;
1881 for (x=offset; x < (ssize_t) bytes_per_line; x++)
1883 if (image->previous == (Image *) NULL)
1885 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
1887 if (status == MagickFalse)
1896 Convert PseudoClass packet to BMP pixel.
1898 for (y=0; y < (ssize_t) image->rows; y++)
1900 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
1901 if (p == (const Quantum *) NULL)
1903 q=pixels+(image->rows-y-1)*bytes_per_line;
1904 for (x=0; x < (ssize_t) image->columns; x++)
1906 *q++=(unsigned char) GetPixelIndex(image,p);
1907 p+=GetPixelChannels(image);
1909 for ( ; x < (ssize_t) bytes_per_line; x++)
1911 if (image->previous == (Image *) NULL)
1913 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
1915 if (status == MagickFalse)
1924 Convert DirectClass packet to BMP BGR888.
1926 for (y=0; y < (ssize_t) image->rows; y++)
1928 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
1929 if (p == (const Quantum *) NULL)
1931 q=pixels+(image->rows-y-1)*bytes_per_line;
1932 for (x=0; x < (ssize_t) image->columns; x++)
1938 if (bmp_subtype == ARGB4444)
1940 pixel=(unsigned short) ScaleQuantumToAny(
1941 GetPixelAlpha(image,p),15) << 12;
1942 pixel|=(unsigned short) ScaleQuantumToAny(
1943 GetPixelRed(image,p),15) << 8;
1944 pixel|=(unsigned short) ScaleQuantumToAny(
1945 GetPixelGreen(image,p),15) << 4;
1946 pixel|=(unsigned short) ScaleQuantumToAny(
1947 GetPixelBlue(image,p),15);
1949 else if (bmp_subtype == RGB565)
1951 pixel=(unsigned short) ScaleQuantumToAny(
1952 GetPixelRed(image,p),31) << 11;
1953 pixel|=(unsigned short) ScaleQuantumToAny(
1954 GetPixelGreen(image,p),63) << 5;
1955 pixel|=(unsigned short) ScaleQuantumToAny(
1956 GetPixelBlue(image,p),31);
1960 if (bmp_subtype == ARGB1555)
1961 pixel=(unsigned short) ScaleQuantumToAny(
1962 GetPixelAlpha(image,p),1) << 15;
1963 pixel|=(unsigned short) ScaleQuantumToAny(
1964 GetPixelRed(image,p),31) << 10;
1965 pixel|=(unsigned short) ScaleQuantumToAny(
1966 GetPixelGreen(image,p),31) << 5;
1967 pixel|=(unsigned short) ScaleQuantumToAny(
1968 GetPixelBlue(image,p),31);
1970 *((unsigned short *) q)=pixel;
1972 p+=GetPixelChannels(image);
1974 for (x=2L*(ssize_t) image->columns; x < (ssize_t) bytes_per_line; x++)
1976 if (image->previous == (Image *) NULL)
1978 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
1980 if (status == MagickFalse)
1989 Convert DirectClass packet to BMP BGR888.
1991 for (y=0; y < (ssize_t) image->rows; y++)
1993 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
1994 if (p == (const Quantum *) NULL)
1996 q=pixels+(image->rows-y-1)*bytes_per_line;
1997 for (x=0; x < (ssize_t) image->columns; x++)
1999 *q++=ScaleQuantumToChar(GetPixelBlue(image,p));
2000 *q++=ScaleQuantumToChar(GetPixelGreen(image,p));
2001 *q++=ScaleQuantumToChar(GetPixelRed(image,p));
2002 p+=GetPixelChannels(image);
2004 for (x=3L*(ssize_t) image->columns; x < (ssize_t) bytes_per_line; x++)
2006 if (image->previous == (Image *) NULL)
2008 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
2010 if (status == MagickFalse)
2019 Convert DirectClass packet to ARGB8888 pixel.
2021 for (y=0; y < (ssize_t) image->rows; y++)
2023 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
2024 if (p == (const Quantum *) NULL)
2026 q=pixels+(image->rows-y-1)*bytes_per_line;
2027 for (x=0; x < (ssize_t) image->columns; x++)
2029 *q++=ScaleQuantumToChar(GetPixelBlue(image,p));
2030 *q++=ScaleQuantumToChar(GetPixelGreen(image,p));
2031 *q++=ScaleQuantumToChar(GetPixelRed(image,p));
2032 *q++=ScaleQuantumToChar(GetPixelAlpha(image,p));
2033 p+=GetPixelChannels(image);
2035 if (image->previous == (Image *) NULL)
2037 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
2039 if (status == MagickFalse)
2046 if ((type > 2) && (bmp_info.bits_per_pixel == 8))
2047 if (image_info->compression != NoCompression)
2053 Convert run-length encoded raster pixels.
2055 rle_info=AcquireVirtualMemory((size_t) (2*(bytes_per_line+2)+2),
2056 (image->rows+2)*sizeof(*pixels));
2057 if (rle_info == (MemoryInfo *) NULL)
2059 pixel_info=RelinquishVirtualMemory(pixel_info);
2060 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
2062 bmp_data=(unsigned char *) GetVirtualMemoryBlob(rle_info);
2063 bmp_info.file_size-=bmp_info.image_size;
2064 bmp_info.image_size=(unsigned int) EncodeImage(image,bytes_per_line,
2066 bmp_info.file_size+=bmp_info.image_size;
2067 pixel_info=RelinquishVirtualMemory(pixel_info);
2068 pixel_info=rle_info;
2070 bmp_info.compression=BI_RLE8;
2073 Write BMP for Windows, all versions, 14-byte header.
2075 if (image->debug != MagickFalse)
2077 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
2078 " Writing BMP version %.20g datastream",(double) type);
2079 if (image->storage_class == DirectClass)
2080 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
2081 " Storage class=DirectClass");
2083 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
2084 " Storage class=PseudoClass");
2085 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
2086 " Image depth=%.20g",(double) image->depth);
2087 if (image->alpha_trait != UndefinedPixelTrait)
2088 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
2091 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
2092 " Matte=MagickFalse");
2093 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
2094 " BMP bits_per_pixel=%.20g",(double) bmp_info.bits_per_pixel);
2095 switch ((int) bmp_info.compression)
2099 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
2100 " Compression=BI_RGB");
2105 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
2106 " Compression=BI_RLE8");
2111 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
2112 " Compression=BI_BITFIELDS");
2117 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
2118 " Compression=UNKNOWN (%lu)",bmp_info.compression);
2122 if (bmp_info.number_colors == 0)
2123 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
2124 " Number_colors=unspecified");
2126 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
2127 " Number_colors=%lu",bmp_info.number_colors);
2129 (void) WriteBlob(image,2,(unsigned char *) "BM");
2130 (void) WriteBlobLSBLong(image,bmp_info.file_size);
2131 (void) WriteBlobLSBLong(image,bmp_info.ba_offset); /* always 0 */
2132 (void) WriteBlobLSBLong(image,bmp_info.offset_bits);
2136 Write 12-byte version 2 bitmap header.
2138 (void) WriteBlobLSBLong(image,bmp_info.size);
2139 (void) WriteBlobLSBSignedShort(image,(signed short) bmp_info.width);
2140 (void) WriteBlobLSBSignedShort(image,(signed short) bmp_info.height);
2141 (void) WriteBlobLSBShort(image,bmp_info.planes);
2142 (void) WriteBlobLSBShort(image,bmp_info.bits_per_pixel);
2147 Write 40-byte version 3+ bitmap header.
2149 (void) WriteBlobLSBLong(image,bmp_info.size);
2150 (void) WriteBlobLSBSignedLong(image,(signed int) bmp_info.width);
2151 (void) WriteBlobLSBSignedLong(image,(signed int) bmp_info.height);
2152 (void) WriteBlobLSBShort(image,bmp_info.planes);
2153 (void) WriteBlobLSBShort(image,bmp_info.bits_per_pixel);
2154 (void) WriteBlobLSBLong(image,bmp_info.compression);
2155 (void) WriteBlobLSBLong(image,bmp_info.image_size);
2156 (void) WriteBlobLSBLong(image,bmp_info.x_pixels);
2157 (void) WriteBlobLSBLong(image,bmp_info.y_pixels);
2158 (void) WriteBlobLSBLong(image,bmp_info.number_colors);
2159 (void) WriteBlobLSBLong(image,bmp_info.colors_important);
2161 if ((type > 3) && ((image->alpha_trait != UndefinedPixelTrait) ||
2162 (have_color_info != MagickFalse)))
2165 Write the rest of the 108-byte BMP Version 4 header.
2167 (void) WriteBlobLSBLong(image,bmp_info.red_mask);
2168 (void) WriteBlobLSBLong(image,bmp_info.green_mask);
2169 (void) WriteBlobLSBLong(image,bmp_info.blue_mask);
2170 (void) WriteBlobLSBLong(image,bmp_info.alpha_mask);
2171 (void) WriteBlobLSBLong(image,0x73524742U); /* sRGB */
2172 (void) WriteBlobLSBLong(image,(unsigned int)
2173 (image->chromaticity.red_primary.x*0x40000000));
2174 (void) WriteBlobLSBLong(image,(unsigned int)
2175 (image->chromaticity.red_primary.y*0x40000000));
2176 (void) WriteBlobLSBLong(image,(unsigned int)
2177 ((1.000f-(image->chromaticity.red_primary.x+
2178 image->chromaticity.red_primary.y))*0x40000000));
2179 (void) WriteBlobLSBLong(image,(unsigned int)
2180 (image->chromaticity.green_primary.x*0x40000000));
2181 (void) WriteBlobLSBLong(image,(unsigned int)
2182 (image->chromaticity.green_primary.y*0x40000000));
2183 (void) WriteBlobLSBLong(image,(unsigned int)
2184 ((1.000f-(image->chromaticity.green_primary.x+
2185 image->chromaticity.green_primary.y))*0x40000000));
2186 (void) WriteBlobLSBLong(image,(unsigned int)
2187 (image->chromaticity.blue_primary.x*0x40000000));
2188 (void) WriteBlobLSBLong(image,(unsigned int)
2189 (image->chromaticity.blue_primary.y*0x40000000));
2190 (void) WriteBlobLSBLong(image,(unsigned int)
2191 ((1.000f-(image->chromaticity.blue_primary.x+
2192 image->chromaticity.blue_primary.y))*0x40000000));
2193 (void) WriteBlobLSBLong(image,(unsigned int)
2194 (bmp_info.gamma_scale.x*0x10000));
2195 (void) WriteBlobLSBLong(image,(unsigned int)
2196 (bmp_info.gamma_scale.y*0x10000));
2197 (void) WriteBlobLSBLong(image,(unsigned int)
2198 (bmp_info.gamma_scale.z*0x10000));
2199 if ((image->rendering_intent != UndefinedIntent) ||
2200 (profile != (StringInfo *) NULL))
2205 switch ((int) image->rendering_intent)
2207 case SaturationIntent:
2209 intent=LCS_GM_BUSINESS;
2212 case RelativeIntent:
2214 intent=LCS_GM_GRAPHICS;
2217 case PerceptualIntent:
2219 intent=LCS_GM_IMAGES;
2222 case AbsoluteIntent:
2224 intent=LCS_GM_ABS_COLORIMETRIC;
2233 (void) WriteBlobLSBLong(image,(unsigned int) intent);
2234 (void) WriteBlobLSBLong(image,0x00); /* dummy profile data */
2235 (void) WriteBlobLSBLong(image,0x00); /* dummy profile length */
2236 (void) WriteBlobLSBLong(image,0x00); /* reserved */
2239 if (image->storage_class == PseudoClass)
2245 Dump colormap to file.
2247 if (image->debug != MagickFalse)
2248 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
2249 " Colormap: %.20g entries",(double) image->colors);
2250 bmp_colormap=(unsigned char *) AcquireQuantumMemory((size_t) (1UL <<
2251 bmp_info.bits_per_pixel),4*sizeof(*bmp_colormap));
2252 if (bmp_colormap == (unsigned char *) NULL)
2253 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
2255 for (i=0; i < (ssize_t) MagickMin((ssize_t) image->colors,(ssize_t) bmp_info.number_colors); i++)
2257 *q++=ScaleQuantumToChar(ClampToQuantum(image->colormap[i].blue));
2258 *q++=ScaleQuantumToChar(ClampToQuantum(image->colormap[i].green));
2259 *q++=ScaleQuantumToChar(ClampToQuantum(image->colormap[i].red));
2261 *q++=(unsigned char) 0x0;
2263 for ( ; i < (ssize_t) (1UL << bmp_info.bits_per_pixel); i++)
2265 *q++=(unsigned char) 0x00;
2266 *q++=(unsigned char) 0x00;
2267 *q++=(unsigned char) 0x00;
2269 *q++=(unsigned char) 0x00;
2272 (void) WriteBlob(image,(size_t) (3*(1L << bmp_info.bits_per_pixel)),
2275 (void) WriteBlob(image,(size_t) (4*(1L << bmp_info.bits_per_pixel)),
2277 bmp_colormap=(unsigned char *) RelinquishMagickMemory(bmp_colormap);
2279 if (image->debug != MagickFalse)
2280 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
2281 " Pixels: %lu bytes",bmp_info.image_size);
2282 (void) WriteBlob(image,(size_t) bmp_info.image_size,pixels);
2283 pixel_info=RelinquishVirtualMemory(pixel_info);
2284 if (GetNextImageInList(image) == (Image *) NULL)
2286 image=SyncNextImageInList(image);
2287 status=SetImageProgress(image,SaveImagesTag,scene++,
2288 GetImageListLength(image));
2289 if (status == MagickFalse)
2291 } while (image_info->adjoin != MagickFalse);
2292 (void) CloseBlob(image);