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 % https://www.imagemagick.org/script/license.php %
29 % Unless required by applicable law or agreed to in writing, software %
30 % distributed under the License is distributed on an "AS IS" BASIS, %
31 % WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. %
32 % See the License for the specific language governing permissions and %
33 % limitations under the License. %
35 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
43 #include "MagickCore/studio.h"
44 #include "MagickCore/blob.h"
45 #include "MagickCore/blob-private.h"
46 #include "MagickCore/cache.h"
47 #include "MagickCore/colormap-private.h"
48 #include "MagickCore/color-private.h"
49 #include "MagickCore/colormap.h"
50 #include "MagickCore/colorspace.h"
51 #include "MagickCore/colorspace-private.h"
52 #include "MagickCore/exception.h"
53 #include "MagickCore/exception-private.h"
54 #include "MagickCore/image.h"
55 #include "MagickCore/image-private.h"
56 #include "MagickCore/list.h"
57 #include "MagickCore/log.h"
58 #include "MagickCore/magick.h"
59 #include "MagickCore/memory_.h"
60 #include "MagickCore/monitor.h"
61 #include "MagickCore/monitor-private.h"
62 #include "MagickCore/option.h"
63 #include "MagickCore/pixel-accessor.h"
64 #include "MagickCore/profile.h"
65 #include "MagickCore/quantum-private.h"
66 #include "MagickCore/static.h"
67 #include "MagickCore/string_.h"
68 #include "MagickCore/module.h"
69 #include "MagickCore/transform.h"
72 Macro definitions (from Windows wingdi.h).
78 #if !defined(MAGICKCORE_WINDOWS_SUPPORT) || defined(__MINGW32__)
86 #define BI_BITFIELDS 3
88 #undef LCS_CALIBRATED_RBG
89 #define LCS_CALIBRATED_RBG 0
92 #undef LCS_WINDOWS_COLOR_SPACE
93 #define LCS_WINDOWS_COLOR_SPACE 2
95 #define PROFILE_LINKED 3
96 #undef PROFILE_EMBEDDED
97 #define PROFILE_EMBEDDED 4
99 #undef LCS_GM_BUSINESS
100 #define LCS_GM_BUSINESS 1 /* Saturation */
101 #undef LCS_GM_GRAPHICS
102 #define LCS_GM_GRAPHICS 2 /* Relative */
104 #define LCS_GM_IMAGES 4 /* Perceptual */
105 #undef LCS_GM_ABS_COLORIMETRIC
106 #define LCS_GM_ABS_COLORIMETRIC 8 /* Absolute */
110 Enumerated declaractions.
122 Typedef declarations.
124 typedef struct _BMPInfo
163 Forward declarations.
165 static MagickBooleanType
166 WriteBMPImage(const ImageInfo *,Image *,ExceptionInfo *);
169 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
173 % D e c o d e I m a g e %
177 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
179 % DecodeImage unpacks the packed image pixels into runlength-encoded
182 % The format of the DecodeImage method is:
184 % MagickBooleanType DecodeImage(Image *image,
185 % const size_t compression,unsigned char *pixels)
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);
905 bmp_colormap=(unsigned char *) RelinquishMagickMemory(bmp_colormap);
906 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
908 count=ReadBlob(image,packet_size*image->colors,bmp_colormap);
909 if (count != (ssize_t) (packet_size*image->colors))
911 bmp_colormap=(unsigned char *) RelinquishMagickMemory(bmp_colormap);
912 ThrowReaderException(CorruptImageError,
913 "InsufficientImageDataInFile");
916 for (i=0; i < (ssize_t) image->colors; i++)
918 image->colormap[i].blue=(MagickRealType) ScaleCharToQuantum(*p++);
919 image->colormap[i].green=(MagickRealType) ScaleCharToQuantum(*p++);
920 image->colormap[i].red=(MagickRealType) ScaleCharToQuantum(*p++);
921 if (packet_size == 4)
924 bmp_colormap=(unsigned char *) RelinquishMagickMemory(bmp_colormap);
926 image->resolution.x=(double) bmp_info.x_pixels/100.0;
927 image->resolution.y=(double) bmp_info.y_pixels/100.0;
928 image->units=PixelsPerCentimeterResolution;
929 if ((image_info->ping != MagickFalse) && (image_info->number_scenes != 0))
930 if (image->scene >= (image_info->scene+image_info->number_scenes-1))
932 status=SetImageExtent(image,image->columns,image->rows,exception);
933 if (status == MagickFalse)
934 return(DestroyImageList(image));
938 offset=SeekBlob(image,start_position+bmp_info.offset_bits,SEEK_SET);
940 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
941 if (bmp_info.compression == BI_RLE4)
942 bmp_info.bits_per_pixel<<=1;
943 bytes_per_line=4*((image->columns*bmp_info.bits_per_pixel+31)/32);
944 length=(size_t) bytes_per_line*image->rows;
945 if ((bmp_info.compression == BI_RGB) ||
946 (bmp_info.compression == BI_BITFIELDS))
948 if (length > GetBlobSize(image))
949 ThrowReaderException(CorruptImageError,"InsufficientImageDataInFile");
950 pixel_info=AcquireVirtualMemory((size_t) image->rows,
951 MagickMax(bytes_per_line,image->columns+256UL)*sizeof(*pixels));
952 if (pixel_info == (MemoryInfo *) NULL)
953 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
954 pixels=(unsigned char *) GetVirtualMemoryBlob(pixel_info);
955 if (image->debug != MagickFalse)
956 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
957 " Reading pixels (%.20g bytes)",(double) length);
958 count=ReadBlob(image,length,pixels);
959 if (count != (ssize_t) length)
961 pixel_info=RelinquishVirtualMemory(pixel_info);
962 ThrowReaderException(CorruptImageError,
963 "InsufficientImageDataInFile");
969 Convert run-length encoded raster pixels.
971 pixel_info=AcquireVirtualMemory((size_t) image->rows,
972 MagickMax(bytes_per_line,image->columns+256UL)*sizeof(*pixels));
973 if (pixel_info == (MemoryInfo *) NULL)
974 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
975 pixels=(unsigned char *) GetVirtualMemoryBlob(pixel_info);
976 status=DecodeImage(image,bmp_info.compression,pixels);
977 if (status == MagickFalse)
979 pixel_info=RelinquishVirtualMemory(pixel_info);
980 ThrowReaderException(CorruptImageError,
981 "UnableToRunlengthDecodeImage");
985 Convert BMP raster image to pixel packets.
987 if (bmp_info.compression == BI_RGB)
990 We should ignore the alpha value in BMP3 files but there have been
991 reports about 32 bit files with alpha. We do a quick check to see if
992 the alpha channel contains a value that is not zero (default value).
993 If we find a non zero value we asume the program that wrote the file
994 wants to use the alpha channel.
996 if ((image->alpha_trait == UndefinedPixelTrait) && (bmp_info.size == 40) &&
997 (bmp_info.bits_per_pixel == 32))
999 bytes_per_line=4*(image->columns);
1000 for (y=(ssize_t) image->rows-1; y >= 0; y--)
1002 p=pixels+(image->rows-y-1)*bytes_per_line;
1003 for (x=0; x < (ssize_t) image->columns; x++)
1007 image->alpha_trait=BlendPixelTrait;
1015 bmp_info.alpha_mask=image->alpha_trait != UndefinedPixelTrait ?
1017 bmp_info.red_mask=0x00ff0000U;
1018 bmp_info.green_mask=0x0000ff00U;
1019 bmp_info.blue_mask=0x000000ffU;
1020 if (bmp_info.bits_per_pixel == 16)
1025 bmp_info.red_mask=0x00007c00U;
1026 bmp_info.green_mask=0x000003e0U;
1027 bmp_info.blue_mask=0x0000001fU;
1030 (void) ResetMagickMemory(&shift,0,sizeof(shift));
1031 (void) ResetMagickMemory(&quantum_bits,0,sizeof(quantum_bits));
1032 if ((bmp_info.bits_per_pixel == 16) || (bmp_info.bits_per_pixel == 32))
1038 Get shift and quantum bits info from bitfield masks.
1040 if (bmp_info.red_mask != 0)
1041 while (((bmp_info.red_mask << shift.red) & 0x80000000UL) == 0)
1043 if (bmp_info.green_mask != 0)
1044 while (((bmp_info.green_mask << shift.green) & 0x80000000UL) == 0)
1046 if (bmp_info.blue_mask != 0)
1047 while (((bmp_info.blue_mask << shift.blue) & 0x80000000UL) == 0)
1049 if (bmp_info.alpha_mask != 0)
1050 while (((bmp_info.alpha_mask << shift.alpha) & 0x80000000UL) == 0)
1053 while (((bmp_info.red_mask << sample) & 0x80000000UL) != 0)
1055 quantum_bits.red=(MagickRealType) (sample-shift.red);
1057 while (((bmp_info.green_mask << sample) & 0x80000000UL) != 0)
1059 quantum_bits.green=(MagickRealType) (sample-shift.green);
1061 while (((bmp_info.blue_mask << sample) & 0x80000000UL) != 0)
1063 quantum_bits.blue=(MagickRealType) (sample-shift.blue);
1065 while (((bmp_info.alpha_mask << sample) & 0x80000000UL) != 0)
1067 quantum_bits.alpha=(MagickRealType) (sample-shift.alpha);
1069 switch (bmp_info.bits_per_pixel)
1074 Convert bitmap scanline.
1076 for (y=(ssize_t) image->rows-1; y >= 0; y--)
1078 p=pixels+(image->rows-y-1)*bytes_per_line;
1079 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
1080 if (q == (Quantum *) NULL)
1082 for (x=0; x < ((ssize_t) image->columns-7); x+=8)
1084 for (bit=0; bit < 8; bit++)
1086 index=(Quantum) (((*p) & (0x80 >> bit)) != 0 ? 0x01 : 0x00);
1087 SetPixelIndex(image,index,q);
1088 q+=GetPixelChannels(image);
1092 if ((image->columns % 8) != 0)
1094 for (bit=0; bit < (image->columns % 8); bit++)
1096 index=(Quantum) (((*p) & (0x80 >> bit)) != 0 ? 0x01 : 0x00);
1097 SetPixelIndex(image,index,q);
1098 q+=GetPixelChannels(image);
1102 if (SyncAuthenticPixels(image,exception) == MagickFalse)
1104 if (image->previous == (Image *) NULL)
1106 status=SetImageProgress(image,LoadImageTag,(MagickOffsetType)
1107 (image->rows-y),image->rows);
1108 if (status == MagickFalse)
1112 (void) SyncImage(image,exception);
1118 Convert PseudoColor scanline.
1120 for (y=(ssize_t) image->rows-1; y >= 0; y--)
1122 p=pixels+(image->rows-y-1)*bytes_per_line;
1123 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
1124 if (q == (Quantum *) NULL)
1126 for (x=0; x < ((ssize_t) image->columns-1); x+=2)
1128 ValidateColormapValue(image,(*p >> 4) & 0x0f,&index,exception);
1129 SetPixelIndex(image,index,q);
1130 q+=GetPixelChannels(image);
1131 ValidateColormapValue(image,*p & 0x0f,&index,exception);
1132 SetPixelIndex(image,index,q);
1133 q+=GetPixelChannels(image);
1136 if ((image->columns % 2) != 0)
1138 ValidateColormapValue(image,(*p >> 4) & 0xf,&index,exception);
1139 SetPixelIndex(image,index,q);
1140 q+=GetPixelChannels(image);
1144 if (x < (ssize_t) image->columns)
1146 if (SyncAuthenticPixels(image,exception) == MagickFalse)
1148 if (image->previous == (Image *) NULL)
1150 status=SetImageProgress(image,LoadImageTag,(MagickOffsetType)
1151 (image->rows-y),image->rows);
1152 if (status == MagickFalse)
1156 (void) SyncImage(image,exception);
1162 Convert PseudoColor scanline.
1164 if ((bmp_info.compression == BI_RLE8) ||
1165 (bmp_info.compression == BI_RLE4))
1166 bytes_per_line=image->columns;
1167 for (y=(ssize_t) image->rows-1; y >= 0; y--)
1169 p=pixels+(image->rows-y-1)*bytes_per_line;
1170 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
1171 if (q == (Quantum *) NULL)
1173 for (x=(ssize_t) image->columns; x != 0; --x)
1175 ValidateColormapValue(image,*p++,&index,exception);
1176 SetPixelIndex(image,index,q);
1177 q+=GetPixelChannels(image);
1179 if (SyncAuthenticPixels(image,exception) == MagickFalse)
1181 offset=(MagickOffsetType) (image->rows-y-1);
1182 if (image->previous == (Image *) NULL)
1184 status=SetImageProgress(image,LoadImageTag,(MagickOffsetType)
1185 (image->rows-y),image->rows);
1186 if (status == MagickFalse)
1190 (void) SyncImage(image,exception);
1200 Convert bitfield encoded 16-bit PseudoColor scanline.
1202 if (bmp_info.compression != BI_RGB &&
1203 bmp_info.compression != BI_BITFIELDS)
1205 pixel_info=RelinquishVirtualMemory(pixel_info);
1206 ThrowReaderException(CorruptImageError,
1207 "UnrecognizedImageCompression");
1209 bytes_per_line=2*(image->columns+image->columns % 2);
1210 image->storage_class=DirectClass;
1211 for (y=(ssize_t) image->rows-1; y >= 0; y--)
1213 p=pixels+(image->rows-y-1)*bytes_per_line;
1214 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
1215 if (q == (Quantum *) NULL)
1217 for (x=0; x < (ssize_t) image->columns; x++)
1219 pixel=(size_t) (*p++);
1221 red=((pixel & bmp_info.red_mask) << shift.red) >> 16;
1222 if (quantum_bits.red == 5)
1223 red|=((red & 0xe000) >> 5);
1224 if (quantum_bits.red <= 8)
1225 red|=((red & 0xff00) >> 8);
1226 green=((pixel & bmp_info.green_mask) << shift.green) >> 16;
1227 if (quantum_bits.green == 5)
1228 green|=((green & 0xe000) >> 5);
1229 if (quantum_bits.green == 6)
1230 green|=((green & 0xc000) >> 6);
1231 if (quantum_bits.green <= 8)
1232 green|=((green & 0xff00) >> 8);
1233 blue=((pixel & bmp_info.blue_mask) << shift.blue) >> 16;
1234 if (quantum_bits.blue == 5)
1235 blue|=((blue & 0xe000) >> 5);
1236 if (quantum_bits.blue <= 8)
1237 blue|=((blue & 0xff00) >> 8);
1238 SetPixelRed(image,ScaleShortToQuantum((unsigned short) red),q);
1239 SetPixelGreen(image,ScaleShortToQuantum((unsigned short) green),q);
1240 SetPixelBlue(image,ScaleShortToQuantum((unsigned short) blue),q);
1241 SetPixelAlpha(image,OpaqueAlpha,q);
1242 if (image->alpha_trait != UndefinedPixelTrait)
1244 alpha=((pixel & bmp_info.alpha_mask) << shift.alpha) >> 16;
1245 if (quantum_bits.alpha <= 8)
1246 alpha|=((alpha & 0xff00) >> 8);
1247 SetPixelAlpha(image,ScaleShortToQuantum(
1248 (unsigned short) alpha),q);
1250 q+=GetPixelChannels(image);
1252 if (SyncAuthenticPixels(image,exception) == MagickFalse)
1254 offset=(MagickOffsetType) (image->rows-y-1);
1255 if (image->previous == (Image *) NULL)
1257 status=SetImageProgress(image,LoadImageTag,(MagickOffsetType)
1258 (image->rows-y),image->rows);
1259 if (status == MagickFalse)
1268 Convert DirectColor scanline.
1270 bytes_per_line=4*((image->columns*24+31)/32);
1271 for (y=(ssize_t) image->rows-1; y >= 0; y--)
1273 p=pixels+(image->rows-y-1)*bytes_per_line;
1274 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
1275 if (q == (Quantum *) NULL)
1277 for (x=0; x < (ssize_t) image->columns; x++)
1279 SetPixelBlue(image,ScaleCharToQuantum(*p++),q);
1280 SetPixelGreen(image,ScaleCharToQuantum(*p++),q);
1281 SetPixelRed(image,ScaleCharToQuantum(*p++),q);
1282 SetPixelAlpha(image,OpaqueAlpha,q);
1283 q+=GetPixelChannels(image);
1285 if (SyncAuthenticPixels(image,exception) == MagickFalse)
1287 offset=(MagickOffsetType) (image->rows-y-1);
1288 if (image->previous == (Image *) NULL)
1290 status=SetImageProgress(image,LoadImageTag,(MagickOffsetType)
1291 (image->rows-y),image->rows);
1292 if (status == MagickFalse)
1301 Convert bitfield encoded DirectColor scanline.
1303 if ((bmp_info.compression != BI_RGB) &&
1304 (bmp_info.compression != BI_BITFIELDS))
1306 pixel_info=RelinquishVirtualMemory(pixel_info);
1307 ThrowReaderException(CorruptImageError,
1308 "UnrecognizedImageCompression");
1310 bytes_per_line=4*(image->columns);
1311 for (y=(ssize_t) image->rows-1; y >= 0; y--)
1317 p=pixels+(image->rows-y-1)*bytes_per_line;
1318 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
1319 if (q == (Quantum *) NULL)
1321 for (x=0; x < (ssize_t) image->columns; x++)
1323 pixel=(size_t) (*p++);
1324 pixel|=((size_t) *p++ << 8);
1325 pixel|=((size_t) *p++ << 16);
1326 pixel|=((size_t) *p++ << 24);
1327 red=((pixel & bmp_info.red_mask) << shift.red) >> 16;
1328 if (quantum_bits.red == 8)
1330 green=((pixel & bmp_info.green_mask) << shift.green) >> 16;
1331 if (quantum_bits.green == 8)
1332 green|=(green >> 8);
1333 blue=((pixel & bmp_info.blue_mask) << shift.blue) >> 16;
1334 if (quantum_bits.blue == 8)
1336 SetPixelRed(image,ScaleShortToQuantum((unsigned short) red),q);
1337 SetPixelGreen(image,ScaleShortToQuantum((unsigned short) green),q);
1338 SetPixelBlue(image,ScaleShortToQuantum((unsigned short) blue),q);
1339 SetPixelAlpha(image,OpaqueAlpha,q);
1340 if (image->alpha_trait != UndefinedPixelTrait)
1342 alpha=((pixel & bmp_info.alpha_mask) << shift.alpha) >> 16;
1343 if (quantum_bits.alpha == 8)
1344 alpha|=(alpha >> 8);
1345 SetPixelAlpha(image,ScaleShortToQuantum(
1346 (unsigned short) alpha),q);
1348 q+=GetPixelChannels(image);
1350 if (SyncAuthenticPixels(image,exception) == MagickFalse)
1352 offset=(MagickOffsetType) (image->rows-y-1);
1353 if (image->previous == (Image *) NULL)
1355 status=SetImageProgress(image,LoadImageTag,(MagickOffsetType)
1356 (image->rows-y),image->rows);
1357 if (status == MagickFalse)
1365 pixel_info=RelinquishVirtualMemory(pixel_info);
1366 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
1369 pixel_info=RelinquishVirtualMemory(pixel_info);
1372 if (EOFBlob(image) != MagickFalse)
1374 ThrowFileException(exception,CorruptImageError,"UnexpectedEndOfFile",
1378 if (bmp_info.height < 0)
1384 Correct image orientation.
1386 flipped_image=FlipImage(image,exception);
1387 if (flipped_image != (Image *) NULL)
1389 DuplicateBlob(flipped_image,image);
1390 ReplaceImageInList(&image, flipped_image);
1391 image=flipped_image;
1395 Proceed to next image.
1397 if (image_info->number_scenes != 0)
1398 if (image->scene >= (image_info->scene+image_info->number_scenes-1))
1401 if (bmp_info.ba_offset != 0)
1403 offset=SeekBlob(image,(MagickOffsetType) bmp_info.ba_offset,SEEK_SET);
1405 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
1407 count=ReadBlob(image,2,magick);
1408 if ((count == 2) && (IsBMP(magick,2) != MagickFalse))
1411 Acquire next image structure.
1413 AcquireNextImage(image_info,image,exception);
1414 if (GetNextImageInList(image) == (Image *) NULL)
1416 image=DestroyImageList(image);
1417 return((Image *) NULL);
1419 image=SyncNextImageInList(image);
1420 status=SetImageProgress(image,LoadImagesTag,TellBlob(image),
1421 GetBlobSize(image));
1422 if (status == MagickFalse)
1425 } while (IsBMP(magick,2) != MagickFalse);
1426 (void) CloseBlob(image);
1427 return(GetFirstImageInList(image));
1431 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1435 % R e g i s t e r B M P I m a g e %
1439 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1441 % RegisterBMPImage() adds attributes for the BMP image format to
1442 % the list of supported formats. The attributes include the image format
1443 % tag, a method to read and/or write the format, whether the format
1444 % supports the saving of more than one frame to the same file or blob,
1445 % whether the format supports native in-memory I/O, and a brief
1446 % description of the format.
1448 % The format of the RegisterBMPImage method is:
1450 % size_t RegisterBMPImage(void)
1453 ModuleExport size_t RegisterBMPImage(void)
1458 entry=AcquireMagickInfo("BMP","BMP","Microsoft Windows bitmap image");
1459 entry->decoder=(DecodeImageHandler *) ReadBMPImage;
1460 entry->encoder=(EncodeImageHandler *) WriteBMPImage;
1461 entry->magick=(IsImageFormatHandler *) IsBMP;
1462 entry->flags^=CoderAdjoinFlag;
1463 entry->flags|=CoderDecoderSeekableStreamFlag;
1464 (void) RegisterMagickInfo(entry);
1465 entry=AcquireMagickInfo("BMP","BMP2","Microsoft Windows bitmap image (V2)");
1466 entry->encoder=(EncodeImageHandler *) WriteBMPImage;
1467 entry->magick=(IsImageFormatHandler *) IsBMP;
1468 entry->flags^=CoderAdjoinFlag;
1469 entry->flags|=CoderDecoderSeekableStreamFlag;
1470 (void) RegisterMagickInfo(entry);
1471 entry=AcquireMagickInfo("BMP","BMP3","Microsoft Windows bitmap image (V3)");
1472 entry->encoder=(EncodeImageHandler *) WriteBMPImage;
1473 entry->magick=(IsImageFormatHandler *) IsBMP;
1474 entry->flags^=CoderAdjoinFlag;
1475 entry->flags|=CoderDecoderSeekableStreamFlag;
1476 (void) RegisterMagickInfo(entry);
1477 return(MagickImageCoderSignature);
1481 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1485 % U n r e g i s t e r B M P I m a g e %
1489 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1491 % UnregisterBMPImage() removes format registrations made by the
1492 % BMP module from the list of supported formats.
1494 % The format of the UnregisterBMPImage method is:
1496 % UnregisterBMPImage(void)
1499 ModuleExport void UnregisterBMPImage(void)
1501 (void) UnregisterMagickInfo("BMP");
1502 (void) UnregisterMagickInfo("BMP2");
1503 (void) UnregisterMagickInfo("BMP3");
1507 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1511 % W r i t e B M P I m a g e %
1515 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1517 % WriteBMPImage() writes an image in Microsoft Windows bitmap encoded
1518 % image format, version 3 for Windows or (if the image has a matte channel)
1521 % The format of the WriteBMPImage method is:
1523 % MagickBooleanType WriteBMPImage(const ImageInfo *image_info,
1524 % Image *image,ExceptionInfo *exception)
1526 % A description of each parameter follows.
1528 % o image_info: the image info.
1530 % o image: The image.
1532 % o exception: return any errors or warnings in this structure.
1535 static MagickBooleanType WriteBMPImage(const ImageInfo *image_info,Image *image,
1536 ExceptionInfo *exception)
1560 register const Quantum
1567 register unsigned char
1582 Open output image file.
1584 assert(image_info != (const ImageInfo *) NULL);
1585 assert(image_info->signature == MagickCoreSignature);
1586 assert(image != (Image *) NULL);
1587 assert(image->signature == MagickCoreSignature);
1588 if (image->debug != MagickFalse)
1589 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1590 assert(exception != (ExceptionInfo *) NULL);
1591 assert(exception->signature == MagickCoreSignature);
1592 status=OpenBlob(image_info,image,WriteBinaryBlobMode,exception);
1593 if (status == MagickFalse)
1596 if (LocaleCompare(image_info->magick,"BMP2") == 0)
1599 if (LocaleCompare(image_info->magick,"BMP3") == 0)
1602 option=GetImageOption(image_info,"bmp:format");
1603 if (option != (char *) NULL)
1605 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1606 " Format=%s",option);
1608 if (LocaleCompare(option,"bmp2") == 0)
1610 if (LocaleCompare(option,"bmp3") == 0)
1612 if (LocaleCompare(option,"bmp4") == 0)
1620 Initialize BMP raster file header.
1622 (void) TransformImageColorspace(image,sRGBColorspace,exception);
1623 (void) ResetMagickMemory(&bmp_info,0,sizeof(bmp_info));
1624 bmp_info.file_size=14+12;
1626 bmp_info.file_size+=28;
1627 bmp_info.offset_bits=bmp_info.file_size;
1628 bmp_info.compression=BI_RGB;
1629 bmp_info.red_mask=0x00ff0000U;
1630 bmp_info.green_mask=0x0000ff00U;
1631 bmp_info.blue_mask=0x000000ffU;
1632 bmp_info.alpha_mask=0xff000000U;
1633 bmp_subtype=UndefinedSubtype;
1634 if ((image->storage_class == PseudoClass) && (image->colors > 256))
1635 (void) SetImageStorageClass(image,DirectClass,exception);
1636 if (image->storage_class != DirectClass)
1639 Colormapped BMP raster.
1641 bmp_info.bits_per_pixel=8;
1642 if (image->colors <= 2)
1643 bmp_info.bits_per_pixel=1;
1645 if (image->colors <= 16)
1646 bmp_info.bits_per_pixel=4;
1648 if (image->colors <= 256)
1649 bmp_info.bits_per_pixel=8;
1650 if (image_info->compression == RLECompression)
1651 bmp_info.bits_per_pixel=8;
1652 bmp_info.number_colors=1U << bmp_info.bits_per_pixel;
1653 if (image->alpha_trait != UndefinedPixelTrait)
1654 (void) SetImageStorageClass(image,DirectClass,exception);
1656 if ((size_t) bmp_info.number_colors < image->colors)
1657 (void) SetImageStorageClass(image,DirectClass,exception);
1660 bmp_info.file_size+=3*(1UL << bmp_info.bits_per_pixel);
1661 bmp_info.offset_bits+=3*(1UL << bmp_info.bits_per_pixel);
1664 bmp_info.file_size+=(1UL << bmp_info.bits_per_pixel);
1665 bmp_info.offset_bits+=(1UL << bmp_info.bits_per_pixel);
1669 if (image->storage_class == DirectClass)
1672 Full color BMP raster.
1674 bmp_info.number_colors=0;
1675 option=GetImageOption(image_info,"bmp:subtype");
1676 if (option != (const char *) NULL)
1678 if (image->alpha_trait != UndefinedPixelTrait)
1680 if (LocaleNCompare(option,"ARGB4444",8) == 0)
1682 bmp_subtype=ARGB4444;
1683 bmp_info.red_mask=0x00000f00U;
1684 bmp_info.green_mask=0x000000f0U;
1685 bmp_info.blue_mask=0x0000000fU;
1686 bmp_info.alpha_mask=0x0000f000U;
1688 else if (LocaleNCompare(option,"ARGB1555",8) == 0)
1690 bmp_subtype=ARGB1555;
1691 bmp_info.red_mask=0x00007c00U;
1692 bmp_info.green_mask=0x000003e0U;
1693 bmp_info.blue_mask=0x0000001fU;
1694 bmp_info.alpha_mask=0x00008000U;
1699 if (LocaleNCompare(option,"RGB555",6) == 0)
1702 bmp_info.red_mask=0x00007c00U;
1703 bmp_info.green_mask=0x000003e0U;
1704 bmp_info.blue_mask=0x0000001fU;
1705 bmp_info.alpha_mask=0U;
1707 else if (LocaleNCompare(option,"RGB565",6) == 0)
1710 bmp_info.red_mask=0x0000f800U;
1711 bmp_info.green_mask=0x000007e0U;
1712 bmp_info.blue_mask=0x0000001fU;
1713 bmp_info.alpha_mask=0U;
1717 if (bmp_subtype != UndefinedSubtype)
1719 bmp_info.bits_per_pixel=16;
1720 bmp_info.compression=BI_BITFIELDS;
1724 bmp_info.bits_per_pixel=(unsigned short) ((type > 3) &&
1725 (image->alpha_trait != UndefinedPixelTrait) ? 32 : 24);
1726 bmp_info.compression=(unsigned int) ((type > 3) &&
1727 (image->alpha_trait != UndefinedPixelTrait) ? BI_BITFIELDS : BI_RGB);
1728 if ((type == 3) && (image->alpha_trait != UndefinedPixelTrait))
1730 option=GetImageOption(image_info,"bmp3:alpha");
1731 if (IsStringTrue(option))
1732 bmp_info.bits_per_pixel=32;
1736 bytes_per_line=4*((image->columns*bmp_info.bits_per_pixel+31)/32);
1737 bmp_info.ba_offset=0;
1738 profile=GetImageProfile(image,"icc");
1739 have_color_info=(image->rendering_intent != UndefinedIntent) ||
1740 (profile != (StringInfo *) NULL) || (image->gamma != 0.0) ? MagickTrue :
1745 if ((type == 3) || ((image->alpha_trait == UndefinedPixelTrait) &&
1746 (have_color_info == MagickFalse)))
1758 if ((image->rendering_intent != UndefinedIntent) ||
1759 (profile != (StringInfo *) NULL))
1764 bmp_info.file_size+=extra_size;
1765 bmp_info.offset_bits+=extra_size;
1767 if (((ssize_t) image->columns != (signed int) image->columns) ||
1768 ((ssize_t) image->rows != (signed int) image->rows))
1769 ThrowWriterException(ImageError,"WidthOrHeightExceedsLimit");
1770 bmp_info.width=(ssize_t) image->columns;
1771 bmp_info.height=(ssize_t) image->rows;
1773 bmp_info.image_size=(unsigned long) (bytes_per_line*image->rows);
1774 bmp_info.file_size+=bmp_info.image_size;
1775 bmp_info.x_pixels=75*39;
1776 bmp_info.y_pixels=75*39;
1777 switch (image->units)
1779 case UndefinedResolution:
1780 case PixelsPerInchResolution:
1782 bmp_info.x_pixels=(unsigned int) (100.0*image->resolution.x/2.54);
1783 bmp_info.y_pixels=(unsigned int) (100.0*image->resolution.y/2.54);
1786 case PixelsPerCentimeterResolution:
1788 bmp_info.x_pixels=(unsigned int) (100.0*image->resolution.x);
1789 bmp_info.y_pixels=(unsigned int) (100.0*image->resolution.y);
1793 bmp_info.colors_important=bmp_info.number_colors;
1795 Convert MIFF to BMP raster pixels.
1797 pixel_info=AcquireVirtualMemory((size_t) bmp_info.image_size,
1799 if (pixel_info == (MemoryInfo *) NULL)
1800 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
1801 pixels=(unsigned char *) GetVirtualMemoryBlob(pixel_info);
1802 (void) ResetMagickMemory(pixels,0,(size_t) bmp_info.image_size);
1803 switch (bmp_info.bits_per_pixel)
1812 Convert PseudoClass image to a BMP monochrome image.
1814 for (y=0; y < (ssize_t) image->rows; y++)
1819 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
1820 if (p == (const Quantum *) NULL)
1822 q=pixels+(image->rows-y-1)*bytes_per_line;
1825 for (x=0; x < (ssize_t) image->columns; x++)
1828 byte|=GetPixelIndex(image,p) != 0 ? 0x01 : 0x00;
1832 *q++=(unsigned char) byte;
1836 p+=GetPixelChannels(image);
1840 *q++=(unsigned char) (byte << (8-bit));
1843 offset=(ssize_t) (image->columns+7)/8;
1844 for (x=offset; x < (ssize_t) bytes_per_line; x++)
1846 if (image->previous == (Image *) NULL)
1848 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
1850 if (status == MagickFalse)
1866 Convert PseudoClass image to a BMP monochrome image.
1868 for (y=0; y < (ssize_t) image->rows; y++)
1870 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
1871 if (p == (const Quantum *) NULL)
1873 q=pixels+(image->rows-y-1)*bytes_per_line;
1876 for (x=0; x < (ssize_t) image->columns; x++)
1879 byte|=((size_t) GetPixelIndex(image,p) & 0x0f);
1883 *q++=(unsigned char) byte;
1887 p+=GetPixelChannels(image);
1891 *q++=(unsigned char) (byte << 4);
1894 offset=(ssize_t) (image->columns+1)/2;
1895 for (x=offset; x < (ssize_t) bytes_per_line; x++)
1897 if (image->previous == (Image *) NULL)
1899 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
1901 if (status == MagickFalse)
1910 Convert PseudoClass packet to BMP pixel.
1912 for (y=0; y < (ssize_t) image->rows; y++)
1914 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
1915 if (p == (const Quantum *) NULL)
1917 q=pixels+(image->rows-y-1)*bytes_per_line;
1918 for (x=0; x < (ssize_t) image->columns; x++)
1920 *q++=(unsigned char) GetPixelIndex(image,p);
1921 p+=GetPixelChannels(image);
1923 for ( ; x < (ssize_t) bytes_per_line; x++)
1925 if (image->previous == (Image *) NULL)
1927 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
1929 if (status == MagickFalse)
1938 Convert DirectClass packet to BMP BGR888.
1940 for (y=0; y < (ssize_t) image->rows; y++)
1942 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
1943 if (p == (const Quantum *) NULL)
1945 q=pixels+(image->rows-y-1)*bytes_per_line;
1946 for (x=0; x < (ssize_t) image->columns; x++)
1952 if (bmp_subtype == ARGB4444)
1954 pixel=(unsigned short) ScaleQuantumToAny(
1955 GetPixelAlpha(image,p),15) << 12;
1956 pixel|=(unsigned short) ScaleQuantumToAny(
1957 GetPixelRed(image,p),15) << 8;
1958 pixel|=(unsigned short) ScaleQuantumToAny(
1959 GetPixelGreen(image,p),15) << 4;
1960 pixel|=(unsigned short) ScaleQuantumToAny(
1961 GetPixelBlue(image,p),15);
1963 else if (bmp_subtype == RGB565)
1965 pixel=(unsigned short) ScaleQuantumToAny(
1966 GetPixelRed(image,p),31) << 11;
1967 pixel|=(unsigned short) ScaleQuantumToAny(
1968 GetPixelGreen(image,p),63) << 5;
1969 pixel|=(unsigned short) ScaleQuantumToAny(
1970 GetPixelBlue(image,p),31);
1974 if (bmp_subtype == ARGB1555)
1975 pixel=(unsigned short) ScaleQuantumToAny(
1976 GetPixelAlpha(image,p),1) << 15;
1977 pixel|=(unsigned short) ScaleQuantumToAny(
1978 GetPixelRed(image,p),31) << 10;
1979 pixel|=(unsigned short) ScaleQuantumToAny(
1980 GetPixelGreen(image,p),31) << 5;
1981 pixel|=(unsigned short) ScaleQuantumToAny(
1982 GetPixelBlue(image,p),31);
1984 *((unsigned short *) q)=pixel;
1986 p+=GetPixelChannels(image);
1988 for (x=2L*(ssize_t) image->columns; x < (ssize_t) bytes_per_line; x++)
1990 if (image->previous == (Image *) NULL)
1992 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
1994 if (status == MagickFalse)
2003 Convert DirectClass packet to BMP BGR888.
2005 for (y=0; y < (ssize_t) image->rows; y++)
2007 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
2008 if (p == (const Quantum *) NULL)
2010 q=pixels+(image->rows-y-1)*bytes_per_line;
2011 for (x=0; x < (ssize_t) image->columns; x++)
2013 *q++=ScaleQuantumToChar(GetPixelBlue(image,p));
2014 *q++=ScaleQuantumToChar(GetPixelGreen(image,p));
2015 *q++=ScaleQuantumToChar(GetPixelRed(image,p));
2016 p+=GetPixelChannels(image);
2018 for (x=3L*(ssize_t) image->columns; x < (ssize_t) bytes_per_line; x++)
2020 if (image->previous == (Image *) NULL)
2022 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
2024 if (status == MagickFalse)
2033 Convert DirectClass packet to ARGB8888 pixel.
2035 for (y=0; y < (ssize_t) image->rows; y++)
2037 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
2038 if (p == (const Quantum *) NULL)
2040 q=pixels+(image->rows-y-1)*bytes_per_line;
2041 for (x=0; x < (ssize_t) image->columns; x++)
2043 *q++=ScaleQuantumToChar(GetPixelBlue(image,p));
2044 *q++=ScaleQuantumToChar(GetPixelGreen(image,p));
2045 *q++=ScaleQuantumToChar(GetPixelRed(image,p));
2046 *q++=ScaleQuantumToChar(GetPixelAlpha(image,p));
2047 p+=GetPixelChannels(image);
2049 if (image->previous == (Image *) NULL)
2051 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
2053 if (status == MagickFalse)
2060 if ((type > 2) && (bmp_info.bits_per_pixel == 8))
2061 if (image_info->compression != NoCompression)
2067 Convert run-length encoded raster pixels.
2069 rle_info=AcquireVirtualMemory((size_t) (2*(bytes_per_line+2)+2),
2070 (image->rows+2)*sizeof(*pixels));
2071 if (rle_info == (MemoryInfo *) NULL)
2073 pixel_info=RelinquishVirtualMemory(pixel_info);
2074 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
2076 bmp_data=(unsigned char *) GetVirtualMemoryBlob(rle_info);
2077 bmp_info.file_size-=bmp_info.image_size;
2078 bmp_info.image_size=(unsigned int) EncodeImage(image,bytes_per_line,
2080 bmp_info.file_size+=bmp_info.image_size;
2081 pixel_info=RelinquishVirtualMemory(pixel_info);
2082 pixel_info=rle_info;
2084 bmp_info.compression=BI_RLE8;
2087 Write BMP for Windows, all versions, 14-byte header.
2089 if (image->debug != MagickFalse)
2091 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
2092 " Writing BMP version %.20g datastream",(double) type);
2093 if (image->storage_class == DirectClass)
2094 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
2095 " Storage class=DirectClass");
2097 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
2098 " Storage class=PseudoClass");
2099 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
2100 " Image depth=%.20g",(double) image->depth);
2101 if (image->alpha_trait != UndefinedPixelTrait)
2102 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
2105 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
2106 " Matte=MagickFalse");
2107 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
2108 " BMP bits_per_pixel=%.20g",(double) bmp_info.bits_per_pixel);
2109 switch ((int) bmp_info.compression)
2113 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
2114 " Compression=BI_RGB");
2119 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
2120 " Compression=BI_RLE8");
2125 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
2126 " Compression=BI_BITFIELDS");
2131 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
2132 " Compression=UNKNOWN (%lu)",bmp_info.compression);
2136 if (bmp_info.number_colors == 0)
2137 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
2138 " Number_colors=unspecified");
2140 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
2141 " Number_colors=%lu",bmp_info.number_colors);
2143 (void) WriteBlob(image,2,(unsigned char *) "BM");
2144 (void) WriteBlobLSBLong(image,bmp_info.file_size);
2145 (void) WriteBlobLSBLong(image,bmp_info.ba_offset); /* always 0 */
2146 (void) WriteBlobLSBLong(image,bmp_info.offset_bits);
2150 Write 12-byte version 2 bitmap header.
2152 (void) WriteBlobLSBLong(image,bmp_info.size);
2153 (void) WriteBlobLSBSignedShort(image,(signed short) bmp_info.width);
2154 (void) WriteBlobLSBSignedShort(image,(signed short) bmp_info.height);
2155 (void) WriteBlobLSBShort(image,bmp_info.planes);
2156 (void) WriteBlobLSBShort(image,bmp_info.bits_per_pixel);
2161 Write 40-byte version 3+ bitmap header.
2163 (void) WriteBlobLSBLong(image,bmp_info.size);
2164 (void) WriteBlobLSBSignedLong(image,(signed int) bmp_info.width);
2165 (void) WriteBlobLSBSignedLong(image,(signed int) bmp_info.height);
2166 (void) WriteBlobLSBShort(image,bmp_info.planes);
2167 (void) WriteBlobLSBShort(image,bmp_info.bits_per_pixel);
2168 (void) WriteBlobLSBLong(image,bmp_info.compression);
2169 (void) WriteBlobLSBLong(image,bmp_info.image_size);
2170 (void) WriteBlobLSBLong(image,bmp_info.x_pixels);
2171 (void) WriteBlobLSBLong(image,bmp_info.y_pixels);
2172 (void) WriteBlobLSBLong(image,bmp_info.number_colors);
2173 (void) WriteBlobLSBLong(image,bmp_info.colors_important);
2175 if ((type > 3) && ((image->alpha_trait != UndefinedPixelTrait) ||
2176 (have_color_info != MagickFalse)))
2179 Write the rest of the 108-byte BMP Version 4 header.
2181 (void) WriteBlobLSBLong(image,bmp_info.red_mask);
2182 (void) WriteBlobLSBLong(image,bmp_info.green_mask);
2183 (void) WriteBlobLSBLong(image,bmp_info.blue_mask);
2184 (void) WriteBlobLSBLong(image,bmp_info.alpha_mask);
2185 (void) WriteBlobLSBLong(image,0x73524742U); /* sRGB */
2186 (void) WriteBlobLSBLong(image,(unsigned int)
2187 (image->chromaticity.red_primary.x*0x40000000));
2188 (void) WriteBlobLSBLong(image,(unsigned int)
2189 (image->chromaticity.red_primary.y*0x40000000));
2190 (void) WriteBlobLSBLong(image,(unsigned int)
2191 ((1.000f-(image->chromaticity.red_primary.x+
2192 image->chromaticity.red_primary.y))*0x40000000));
2193 (void) WriteBlobLSBLong(image,(unsigned int)
2194 (image->chromaticity.green_primary.x*0x40000000));
2195 (void) WriteBlobLSBLong(image,(unsigned int)
2196 (image->chromaticity.green_primary.y*0x40000000));
2197 (void) WriteBlobLSBLong(image,(unsigned int)
2198 ((1.000f-(image->chromaticity.green_primary.x+
2199 image->chromaticity.green_primary.y))*0x40000000));
2200 (void) WriteBlobLSBLong(image,(unsigned int)
2201 (image->chromaticity.blue_primary.x*0x40000000));
2202 (void) WriteBlobLSBLong(image,(unsigned int)
2203 (image->chromaticity.blue_primary.y*0x40000000));
2204 (void) WriteBlobLSBLong(image,(unsigned int)
2205 ((1.000f-(image->chromaticity.blue_primary.x+
2206 image->chromaticity.blue_primary.y))*0x40000000));
2207 (void) WriteBlobLSBLong(image,(unsigned int)
2208 (bmp_info.gamma_scale.x*0x10000));
2209 (void) WriteBlobLSBLong(image,(unsigned int)
2210 (bmp_info.gamma_scale.y*0x10000));
2211 (void) WriteBlobLSBLong(image,(unsigned int)
2212 (bmp_info.gamma_scale.z*0x10000));
2213 if ((image->rendering_intent != UndefinedIntent) ||
2214 (profile != (StringInfo *) NULL))
2219 switch ((int) image->rendering_intent)
2221 case SaturationIntent:
2223 intent=LCS_GM_BUSINESS;
2226 case RelativeIntent:
2228 intent=LCS_GM_GRAPHICS;
2231 case PerceptualIntent:
2233 intent=LCS_GM_IMAGES;
2236 case AbsoluteIntent:
2238 intent=LCS_GM_ABS_COLORIMETRIC;
2247 (void) WriteBlobLSBLong(image,(unsigned int) intent);
2248 (void) WriteBlobLSBLong(image,0x00); /* dummy profile data */
2249 (void) WriteBlobLSBLong(image,0x00); /* dummy profile length */
2250 (void) WriteBlobLSBLong(image,0x00); /* reserved */
2253 if (image->storage_class == PseudoClass)
2259 Dump colormap to file.
2261 if (image->debug != MagickFalse)
2262 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
2263 " Colormap: %.20g entries",(double) image->colors);
2264 bmp_colormap=(unsigned char *) AcquireQuantumMemory((size_t) (1UL <<
2265 bmp_info.bits_per_pixel),4*sizeof(*bmp_colormap));
2266 if (bmp_colormap == (unsigned char *) NULL)
2267 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
2269 for (i=0; i < (ssize_t) MagickMin((ssize_t) image->colors,(ssize_t) bmp_info.number_colors); i++)
2271 *q++=ScaleQuantumToChar(ClampToQuantum(image->colormap[i].blue));
2272 *q++=ScaleQuantumToChar(ClampToQuantum(image->colormap[i].green));
2273 *q++=ScaleQuantumToChar(ClampToQuantum(image->colormap[i].red));
2275 *q++=(unsigned char) 0x0;
2277 for ( ; i < (ssize_t) (1UL << bmp_info.bits_per_pixel); i++)
2279 *q++=(unsigned char) 0x00;
2280 *q++=(unsigned char) 0x00;
2281 *q++=(unsigned char) 0x00;
2283 *q++=(unsigned char) 0x00;
2286 (void) WriteBlob(image,(size_t) (3*(1L << bmp_info.bits_per_pixel)),
2289 (void) WriteBlob(image,(size_t) (4*(1L << bmp_info.bits_per_pixel)),
2291 bmp_colormap=(unsigned char *) RelinquishMagickMemory(bmp_colormap);
2293 if (image->debug != MagickFalse)
2294 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
2295 " Pixels: %lu bytes",bmp_info.image_size);
2296 (void) WriteBlob(image,(size_t) bmp_info.image_size,pixels);
2297 pixel_info=RelinquishVirtualMemory(pixel_info);
2298 if (GetNextImageInList(image) == (Image *) NULL)
2300 image=SyncNextImageInList(image);
2301 status=SetImageProgress(image,SaveImagesTag,scene++,
2302 GetImageListLength(image));
2303 if (status == MagickFalse)
2305 } while (image_info->adjoin != MagickFalse);
2306 (void) CloseBlob(image);