2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
13 % Read/Write Microsoft Windows Bitmap Image Format %
17 % Glenn Randers-Pehrson %
21 % Copyright 1999-2012 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/pixel-accessor.h"
63 #include "MagickCore/profile.h"
64 #include "MagickCore/quantum-private.h"
65 #include "MagickCore/static.h"
66 #include "MagickCore/string_.h"
67 #include "MagickCore/module.h"
68 #include "MagickCore/transform.h"
71 Macro definitions (from Windows wingdi.h).
77 #if !defined(MAGICKCORE_WINDOWS_SUPPORT) || defined(__MINGW32__)
85 #define BI_BITFIELDS 3
87 #undef LCS_CALIBRATED_RBG
88 #define LCS_CALIBRATED_RBG 0
91 #undef LCS_WINDOWS_COLOR_SPACE
92 #define LCS_WINDOWS_COLOR_SPACE 2
94 #define PROFILE_LINKED 3
95 #undef PROFILE_EMBEDDED
96 #define PROFILE_EMBEDDED 4
98 #undef LCS_GM_BUSINESS
99 #define LCS_GM_BUSINESS 1 /* Saturation */
100 #undef LCS_GM_GRAPHICS
101 #define LCS_GM_GRAPHICS 2 /* Relative */
103 #define LCS_GM_IMAGES 4 /* Perceptual */
104 #undef LCS_GM_ABS_COLORIMETRIC
105 #define LCS_GM_ABS_COLORIMETRIC 8 /* Absolute */
109 Typedef declarations.
111 typedef struct _BMPInfo
150 Forward declarations.
152 static MagickBooleanType
153 WriteBMPImage(const ImageInfo *,Image *,ExceptionInfo *);
156 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
160 % D e c o d e I m a g e %
164 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
166 % DecodeImage unpacks the packed image pixels into runlength-encoded
169 % The format of the DecodeImage method is:
171 % MagickBooleanType DecodeImage(Image *image,
172 % const size_t compression,unsigned char *pixels)
174 % A description of each parameter follows:
176 % o image: the address of a structure of type Image.
178 % o compression: Zero means uncompressed. A value of 1 means the
179 % compressed pixels are runlength encoded for a 256-color bitmap.
180 % A value of 2 means a 16-color bitmap. A value of 3 means bitfields
183 % o pixels: The address of a byte (8 bits) array of pixel data created by
184 % the decoding process.
188 static inline ssize_t MagickAbsoluteValue(const ssize_t x)
195 static inline size_t MagickMax(const size_t x,const size_t y)
202 static inline ssize_t MagickMin(const ssize_t x,const ssize_t y)
209 static MagickBooleanType DecodeImage(Image *image,const size_t compression,
210 unsigned char *pixels)
219 register unsigned char
229 assert(image != (Image *) NULL);
230 assert(image->signature == MagickSignature);
231 if (image->debug != MagickFalse)
232 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
233 assert(pixels != (unsigned char *) NULL);
234 (void) ResetMagickMemory(pixels,0,(size_t) image->columns*image->rows*
239 q=pixels+(size_t) image->columns*image->rows;
240 for (y=0; y < (ssize_t) image->rows; )
242 if ((p < pixels) || (p >= q))
244 count=ReadBlobByte(image);
252 count=MagickMin(count,(int) (q-p));
253 byte=(unsigned char) ReadBlobByte(image);
254 if (compression == BI_RLE8)
256 for (i=0; i < count; i++)
257 *p++=(unsigned char) byte;
261 for (i=0; i < count; i++)
263 ((i & 0x01) != 0 ? (byte & 0x0f) : ((byte >> 4) & 0x0f));
272 count=ReadBlobByte(image);
284 p=pixels+y*image->columns;
292 x+=ReadBlobByte(image);
293 y+=ReadBlobByte(image);
294 p=pixels+y*image->columns+x;
302 count=MagickMin(count,(int) (q-p));
303 if (compression == BI_RLE8)
304 for (i=0; i < count; i++)
305 *p++=(unsigned char) ReadBlobByte(image);
307 for (i=0; i < count; i++)
310 byte=(unsigned char) ReadBlobByte(image);
312 ((i & 0x01) != 0 ? (byte & 0x0f) : ((byte >> 4) & 0x0f));
318 if (compression == BI_RLE8)
320 if ((count & 0x01) != 0)
321 (void) ReadBlobByte(image);
324 if (((count & 0x03) == 1) || ((count & 0x03) == 2))
325 (void) ReadBlobByte(image);
330 if (SetImageProgress(image,LoadImageTag,y,image->rows) == MagickFalse)
333 (void) ReadBlobByte(image); /* end of line */
334 (void) ReadBlobByte(image);
339 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
343 % E n c o d e I m a g e %
347 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
349 % EncodeImage compresses pixels using a runlength encoded format.
351 % The format of the EncodeImage method is:
353 % static MagickBooleanType EncodeImage(Image *image,
354 % const size_t bytes_per_line,const unsigned char *pixels,
355 % unsigned char *compressed_pixels)
357 % A description of each parameter follows:
359 % o image: The image.
361 % o bytes_per_line: the number of bytes in a scanline of compressed pixels
363 % o pixels: The address of a byte (8 bits) array of pixel data created by
364 % the compression process.
366 % o compressed_pixels: The address of a byte (8 bits) array of compressed
370 static size_t EncodeImage(Image *image,const size_t bytes_per_line,
371 const unsigned char *pixels,unsigned char *compressed_pixels)
376 register const unsigned char
383 register unsigned char
390 Runlength encode pixels.
392 assert(image != (Image *) NULL);
393 assert(image->signature == MagickSignature);
394 if (image->debug != MagickFalse)
395 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
396 assert(pixels != (const unsigned char *) NULL);
397 assert(compressed_pixels != (unsigned char *) NULL);
401 for (y=0; y < (ssize_t) image->rows; y++)
403 for (x=0; x < (ssize_t) bytes_per_line; x+=i)
408 for (i=1; ((x+i) < (ssize_t) bytes_per_line); i++)
409 if ((i == 255) || (*(p+i) != *p))
411 *q++=(unsigned char) i;
418 *q++=(unsigned char) 0x00;
419 *q++=(unsigned char) 0x00;
420 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
422 if (status == MagickFalse)
428 *q++=(unsigned char) 0x00;
429 *q++=(unsigned char) 0x01;
430 return((size_t) (q-compressed_pixels));
434 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
442 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
444 % IsBMP() returns MagickTrue if the image format type, identified by the
445 % magick string, is BMP.
447 % The format of the IsBMP method is:
449 % MagickBooleanType IsBMP(const unsigned char *magick,const size_t length)
451 % A description of each parameter follows:
453 % o magick: compare image format pattern against these bytes.
455 % o length: Specifies the length of the magick string.
458 static MagickBooleanType IsBMP(const unsigned char *magick,const size_t length)
462 if ((LocaleNCompare((char *) magick,"BA",2) == 0) ||
463 (LocaleNCompare((char *) magick,"BM",2) == 0) ||
464 (LocaleNCompare((char *) magick,"IC",2) == 0) ||
465 (LocaleNCompare((char *) magick,"PI",2) == 0) ||
466 (LocaleNCompare((char *) magick,"CI",2) == 0) ||
467 (LocaleNCompare((char *) magick,"CP",2) == 0))
473 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
477 % R e a d B M P I m a g e %
481 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
483 % ReadBMPImage() reads a Microsoft Windows bitmap image file, Version
484 % 2, 3 (for Windows or NT), or 4, and returns it. It allocates the memory
485 % necessary for the new Image structure and returns a pointer to the new
488 % The format of the ReadBMPImage method is:
490 % image=ReadBMPImage(image_info)
492 % A description of each parameter follows:
494 % o image_info: the image info.
496 % o exception: return any errors or warnings in this structure.
500 static Image *ReadBMPImage(const ImageInfo *image_info,ExceptionInfo *exception)
525 register unsigned char
548 assert(image_info != (const ImageInfo *) NULL);
549 assert(image_info->signature == MagickSignature);
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 == MagickSignature);
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);
582 Verify BMP identifier.
584 if (bmp_info.ba_offset == 0)
585 start_position=TellBlob(image)-2;
586 bmp_info.ba_offset=0;
587 while (LocaleNCompare((char *) magick,"BA",2) == 0)
589 bmp_info.file_size=ReadBlobLSBLong(image);
590 bmp_info.ba_offset=ReadBlobLSBLong(image);
591 bmp_info.offset_bits=ReadBlobLSBLong(image);
592 count=ReadBlob(image,2,magick);
596 if (image->debug != MagickFalse)
597 (void) LogMagickEvent(CoderEvent,GetMagickModule()," Magick: %c%c",
598 magick[0],magick[1]);
599 if ((count == 0) || ((LocaleNCompare((char *) magick,"BM",2) != 0) &&
600 (LocaleNCompare((char *) magick,"CI",2) != 0)))
601 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
602 bmp_info.file_size=ReadBlobLSBLong(image);
603 (void) ReadBlobLSBLong(image);
604 bmp_info.offset_bits=ReadBlobLSBLong(image);
605 bmp_info.size=ReadBlobLSBLong(image);
606 if (image->debug != MagickFalse)
607 (void) LogMagickEvent(CoderEvent,GetMagickModule()," BMP size: %u",
609 if (bmp_info.size == 12)
614 bmp_info.width=(short) ReadBlobLSBShort(image);
615 bmp_info.height=(short) ReadBlobLSBShort(image);
616 bmp_info.planes=ReadBlobLSBShort(image);
617 bmp_info.bits_per_pixel=ReadBlobLSBShort(image);
620 bmp_info.number_colors=0;
621 bmp_info.compression=BI_RGB;
622 bmp_info.image_size=0;
623 bmp_info.alpha_mask=0;
624 if (image->debug != MagickFalse)
626 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
627 " Format: OS/2 Bitmap");
628 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
629 " Geometry: %.20gx%.20g",(double) bmp_info.width,(double)
636 Microsoft Windows BMP image file.
638 if (bmp_info.size < 40)
639 ThrowReaderException(CorruptImageError,"NonOS2HeaderSizeError");
640 bmp_info.width=(int) ReadBlobLSBLong(image);
641 bmp_info.height=(int) ReadBlobLSBLong(image);
642 bmp_info.planes=ReadBlobLSBShort(image);
643 bmp_info.bits_per_pixel=ReadBlobLSBShort(image);
644 bmp_info.compression=ReadBlobLSBLong(image);
645 bmp_info.image_size=ReadBlobLSBLong(image);
646 bmp_info.x_pixels=ReadBlobLSBLong(image);
647 bmp_info.y_pixels=ReadBlobLSBLong(image);
648 bmp_info.number_colors=ReadBlobLSBLong(image);
649 bmp_info.colors_important=ReadBlobLSBLong(image);
652 if (image->debug != MagickFalse)
654 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
655 " Format: MS Windows bitmap");
656 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
657 " Geometry: %.20gx%.20g",(double) bmp_info.width,(double)
659 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
660 " Bits per pixel: %.20g",(double) bmp_info.bits_per_pixel);
661 switch ((int) bmp_info.compression)
665 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
666 " Compression: BI_RGB");
671 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
672 " Compression: BI_RLE4");
677 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
678 " Compression: BI_RLE8");
683 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
684 " Compression: BI_BITFIELDS");
689 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
690 " Compression: BI_PNG");
695 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
696 " Compression: BI_JPEG");
701 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
702 " Compression: UNKNOWN (%u)",bmp_info.compression);
705 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
706 " Number of colors: %u",bmp_info.number_colors);
708 bmp_info.red_mask=ReadBlobLSBLong(image);
709 bmp_info.green_mask=ReadBlobLSBLong(image);
710 bmp_info.blue_mask=ReadBlobLSBLong(image);
711 if (bmp_info.size > 40)
717 Read color management information.
719 bmp_info.alpha_mask=ReadBlobLSBLong(image);
720 bmp_info.colorspace=(int) ReadBlobLSBLong(image);
722 Decode 2^30 fixed point formatted CIE primaries.
724 bmp_info.red_primary.x=(double) ReadBlobLSBLong(image)/0x3ffffff;
725 bmp_info.red_primary.y=(double) ReadBlobLSBLong(image)/0x3ffffff;
726 bmp_info.red_primary.z=(double) ReadBlobLSBLong(image)/0x3ffffff;
727 bmp_info.green_primary.x=(double) ReadBlobLSBLong(image)/0x3ffffff;
728 bmp_info.green_primary.y=(double) ReadBlobLSBLong(image)/0x3ffffff;
729 bmp_info.green_primary.z=(double) ReadBlobLSBLong(image)/0x3ffffff;
730 bmp_info.blue_primary.x=(double) ReadBlobLSBLong(image)/0x3ffffff;
731 bmp_info.blue_primary.y=(double) ReadBlobLSBLong(image)/0x3ffffff;
732 bmp_info.blue_primary.z=(double) ReadBlobLSBLong(image)/0x3ffffff;
733 sum=bmp_info.red_primary.x+bmp_info.red_primary.y+
734 bmp_info.red_primary.z;
735 image->chromaticity.red_primary.x/=sum;
736 image->chromaticity.red_primary.y/=sum;
737 sum=bmp_info.green_primary.x+bmp_info.green_primary.y+
738 bmp_info.green_primary.z;
739 image->chromaticity.green_primary.x/=sum;
740 image->chromaticity.green_primary.y/=sum;
741 sum=bmp_info.blue_primary.x+bmp_info.blue_primary.y+
742 bmp_info.blue_primary.z;
743 image->chromaticity.blue_primary.x/=sum;
744 image->chromaticity.blue_primary.y/=sum;
746 Decode 16^16 fixed point formatted gamma_scales.
748 bmp_info.gamma_scale.x=(double) ReadBlobLSBLong(image)/0xffff;
749 bmp_info.gamma_scale.y=(double) ReadBlobLSBLong(image)/0xffff;
750 bmp_info.gamma_scale.z=(double) ReadBlobLSBLong(image)/0xffff;
752 Compute a single gamma from the BMP 3-channel gamma.
754 image->gamma=(bmp_info.gamma_scale.x+bmp_info.gamma_scale.y+
755 bmp_info.gamma_scale.z)/3.0;
757 if (bmp_info.size > 108)
763 Read BMP Version 5 color management information.
765 intent=ReadBlobLSBLong(image);
766 switch ((int) intent)
768 case LCS_GM_BUSINESS:
770 image->rendering_intent=SaturationIntent;
773 case LCS_GM_GRAPHICS:
775 image->rendering_intent=RelativeIntent;
780 image->rendering_intent=PerceptualIntent;
783 case LCS_GM_ABS_COLORIMETRIC:
785 image->rendering_intent=AbsoluteIntent;
789 profile_data=ReadBlobLSBLong(image);
790 profile_size=ReadBlobLSBLong(image);
793 (void) ReadBlobLSBLong(image); /* Reserved byte */
796 if ((MagickSizeType) bmp_info.file_size > GetBlobSize(image))
797 (void) ThrowMagickException(exception,GetMagickModule(),CorruptImageError,
798 "LengthAndFilesizeDoNotMatch","`%s'",image->filename);
800 if ((MagickSizeType) bmp_info.file_size < GetBlobSize(image))
801 (void) ThrowMagickException(exception,GetMagickModule(),
802 CorruptImageWarning,"LengthAndFilesizeDoNotMatch","`%s'",
804 if (bmp_info.width <= 0)
805 ThrowReaderException(CorruptImageError,"NegativeOrZeroImageSize");
806 if (bmp_info.height == 0)
807 ThrowReaderException(CorruptImageError,"NegativeOrZeroImageSize");
808 if (bmp_info.planes != 1)
809 ThrowReaderException(CorruptImageError,"StaticPlanesValueNotEqualToOne");
810 if ((bmp_info.bits_per_pixel != 1) && (bmp_info.bits_per_pixel != 4) &&
811 (bmp_info.bits_per_pixel != 8) && (bmp_info.bits_per_pixel != 16) &&
812 (bmp_info.bits_per_pixel != 24) && (bmp_info.bits_per_pixel != 32))
813 ThrowReaderException(CorruptImageError,"UnrecognizedBitsPerPixel");
814 if (bmp_info.number_colors > (1U << bmp_info.bits_per_pixel))
816 if (bmp_info.bits_per_pixel < 24)
817 ThrowReaderException(CorruptImageError,"UnrecognizedNumberOfColors");
818 bmp_info.number_colors=0;
820 if ((bmp_info.compression == 1) && (bmp_info.bits_per_pixel != 8))
821 ThrowReaderException(CorruptImageError,"UnrecognizedBitsPerPixel");
822 if ((bmp_info.compression == 2) && (bmp_info.bits_per_pixel != 4))
823 ThrowReaderException(CorruptImageError,"UnrecognizedBitsPerPixel");
824 if ((bmp_info.compression == 3) && (bmp_info.bits_per_pixel < 16))
825 ThrowReaderException(CorruptImageError,"UnrecognizedBitsPerPixel");
826 switch (bmp_info.compression)
834 ThrowReaderException(CoderError,"JPEGCompressNotSupported");
836 ThrowReaderException(CoderError,"PNGCompressNotSupported");
838 ThrowReaderException(CorruptImageError,"UnrecognizedImageCompression");
840 image->columns=(size_t) MagickAbsoluteValue(bmp_info.width);
841 image->rows=(size_t) MagickAbsoluteValue(bmp_info.height);
842 image->depth=bmp_info.bits_per_pixel <= 8 ? bmp_info.bits_per_pixel : 8;
843 if ((bmp_info.bits_per_pixel == 16) || (bmp_info.bits_per_pixel == 32))
844 image->matte=bmp_info.alpha_mask != 0 ? MagickTrue : MagickFalse;
845 if ((bmp_info.number_colors != 0) || (bmp_info.bits_per_pixel < 16))
850 image->storage_class=PseudoClass;
851 image->colors=bmp_info.number_colors;
853 if (image->colors == 0)
854 image->colors=one << bmp_info.bits_per_pixel;
856 if (image->storage_class == PseudoClass)
865 Read BMP raster colormap.
867 if (image->debug != MagickFalse)
868 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
869 " Reading colormap of %.20g colors",(double) image->colors);
870 if (AcquireImageColormap(image,image->colors,exception) == MagickFalse)
871 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
872 bmp_colormap=(unsigned char *) AcquireQuantumMemory((size_t)
873 image->colors,4*sizeof(*bmp_colormap));
874 if (bmp_colormap == (unsigned char *) NULL)
875 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
876 if ((bmp_info.size == 12) || (bmp_info.size == 64))
880 offset=SeekBlob(image,start_position+14+bmp_info.size,SEEK_SET);
882 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
883 count=ReadBlob(image,packet_size*image->colors,bmp_colormap);
884 if (count != (ssize_t) (packet_size*image->colors))
885 ThrowReaderException(CorruptImageError,"InsufficientImageDataInFile");
887 for (i=0; i < (ssize_t) image->colors; i++)
889 image->colormap[i].blue=ScaleCharToQuantum(*p++);
890 image->colormap[i].green=ScaleCharToQuantum(*p++);
891 image->colormap[i].red=ScaleCharToQuantum(*p++);
892 if (packet_size == 4)
895 bmp_colormap=(unsigned char *) RelinquishMagickMemory(bmp_colormap);
897 image->resolution.x=(double) bmp_info.x_pixels/100.0;
898 image->resolution.y=(double) bmp_info.y_pixels/100.0;
899 image->units=PixelsPerCentimeterResolution;
900 if ((image_info->ping != MagickFalse) && (image_info->number_scenes != 0))
901 if (image->scene >= (image_info->scene+image_info->number_scenes-1))
906 if ((start_position+bmp_info.offset_bits) > TellBlob(image))
908 offset=SeekBlob(image,start_position+bmp_info.offset_bits,SEEK_SET);
910 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
912 if (bmp_info.compression == BI_RLE4)
913 bmp_info.bits_per_pixel<<=1;
914 bytes_per_line=4*((image->columns*bmp_info.bits_per_pixel+31)/32);
915 length=(size_t) bytes_per_line*image->rows;
916 pixels=(unsigned char *) AcquireQuantumMemory((size_t) image->rows,
917 MagickMax(bytes_per_line,image->columns+256UL)*sizeof(*pixels));
918 if (pixels == (unsigned char *) NULL)
919 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
920 if ((bmp_info.compression == BI_RGB) ||
921 (bmp_info.compression == BI_BITFIELDS))
923 if (image->debug != MagickFalse)
924 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
925 " Reading pixels (%.20g bytes)",(double) length);
926 count=ReadBlob(image,length,pixels);
927 if (count != (ssize_t) length)
929 pixels=(unsigned char *) RelinquishMagickMemory(pixels);
930 ThrowReaderException(CorruptImageError,
931 "InsufficientImageDataInFile");
937 Convert run-length encoded raster pixels.
939 status=DecodeImage(image,bmp_info.compression,pixels);
940 if (status == MagickFalse)
942 pixels=(unsigned char *) RelinquishMagickMemory(pixels);
943 ThrowReaderException(CorruptImageError,
944 "UnableToRunlengthDecodeImage");
948 Convert BMP raster image to pixel packets.
950 if (bmp_info.compression == BI_RGB)
952 bmp_info.alpha_mask=0;
953 bmp_info.red_mask=0x00ff0000U;
954 bmp_info.green_mask=0x0000ff00U;
955 bmp_info.blue_mask=0x000000ffU;
956 if (bmp_info.bits_per_pixel == 16)
961 bmp_info.red_mask=0x00007c00U;
962 bmp_info.green_mask=0x000003e0U;
963 bmp_info.blue_mask=0x0000001fU;
966 if ((bmp_info.bits_per_pixel == 16) || (bmp_info.bits_per_pixel == 32))
972 Get shift and quantum bits info from bitfield masks.
974 (void) ResetMagickMemory(&shift,0,sizeof(shift));
975 (void) ResetMagickMemory(&quantum_bits,0,sizeof(quantum_bits));
976 if (bmp_info.red_mask != 0)
977 while (((bmp_info.red_mask << shift.red) & 0x80000000UL) == 0)
979 if (bmp_info.green_mask != 0)
980 while (((bmp_info.green_mask << shift.green) & 0x80000000UL) == 0)
982 if (bmp_info.blue_mask != 0)
983 while (((bmp_info.blue_mask << shift.blue) & 0x80000000UL) == 0)
985 if (bmp_info.alpha_mask != 0)
986 while (((bmp_info.alpha_mask << shift.alpha) & 0x80000000UL) == 0)
989 while (((bmp_info.red_mask << sample) & 0x80000000UL) != 0)
991 quantum_bits.red=(Quantum) (sample-shift.red);
993 while (((bmp_info.green_mask << sample) & 0x80000000UL) != 0)
995 quantum_bits.green=(Quantum) (sample-shift.green);
997 while (((bmp_info.blue_mask << sample) & 0x80000000UL) != 0)
999 quantum_bits.blue=(Quantum) (sample-shift.blue);
1001 while (((bmp_info.alpha_mask << sample) & 0x80000000UL) != 0)
1003 quantum_bits.alpha=(Quantum) (sample-shift.alpha);
1005 switch (bmp_info.bits_per_pixel)
1010 Convert bitmap scanline.
1012 for (y=(ssize_t) image->rows-1; y >= 0; y--)
1014 p=pixels+(image->rows-y-1)*bytes_per_line;
1015 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
1016 if (q == (Quantum *) NULL)
1018 for (x=0; x < ((ssize_t) image->columns-7); x+=8)
1020 for (bit=0; bit < 8; bit++)
1022 index=(Quantum) (((*p) & (0x80 >> bit)) != 0 ? 0x01 : 0x00);
1023 SetPixelIndex(image,index,q);
1024 q+=GetPixelChannels(image);
1028 if ((image->columns % 8) != 0)
1030 for (bit=0; bit < (image->columns % 8); bit++)
1032 index=(Quantum) (((*p) & (0x80 >> bit)) != 0 ? 0x01 : 0x00);
1033 SetPixelIndex(image,index,q);
1034 q+=GetPixelChannels(image);
1038 if (SyncAuthenticPixels(image,exception) == MagickFalse)
1040 if (image->previous == (Image *) NULL)
1042 status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y,
1044 if (status == MagickFalse)
1048 (void) SyncImage(image,exception);
1054 Convert PseudoColor scanline.
1056 for (y=(ssize_t) image->rows-1; y >= 0; y--)
1058 p=pixels+(image->rows-y-1)*bytes_per_line;
1059 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
1060 if (q == (Quantum *) NULL)
1062 for (x=0; x < ((ssize_t) image->columns-1); x+=2)
1064 index=ConstrainColormapIndex(image,(*p >> 4) & 0x0f,exception);
1065 SetPixelIndex(image,index,q);
1066 q+=GetPixelChannels(image);
1067 index=ConstrainColormapIndex(image,*p & 0x0f,exception);
1068 SetPixelIndex(image,index,q);
1069 q+=GetPixelChannels(image);
1072 if ((image->columns % 2) != 0)
1074 index=ConstrainColormapIndex(image,(*p >> 4) & 0xf,exception);
1075 SetPixelIndex(image,index,q);
1076 q+=GetPixelChannels(image);
1079 if (SyncAuthenticPixels(image,exception) == MagickFalse)
1081 if (image->previous == (Image *) NULL)
1083 status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y,
1085 if (status == MagickFalse)
1089 (void) SyncImage(image,exception);
1095 Convert PseudoColor scanline.
1097 if ((bmp_info.compression == BI_RLE8) ||
1098 (bmp_info.compression == BI_RLE4))
1099 bytes_per_line=image->columns;
1100 for (y=(ssize_t) image->rows-1; y >= 0; y--)
1102 p=pixels+(image->rows-y-1)*bytes_per_line;
1103 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
1104 if (q == (Quantum *) NULL)
1106 for (x = (ssize_t)image->columns; x != 0; --x)
1108 index=ConstrainColormapIndex(image,*p++,exception);
1109 SetPixelIndex(image,index,q);
1110 q+=GetPixelChannels(image);
1112 if (SyncAuthenticPixels(image,exception) == MagickFalse)
1114 offset=(MagickOffsetType) (image->rows-y-1);
1115 if (image->previous == (Image *) NULL)
1117 status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y,
1119 if (status == MagickFalse)
1123 (void) SyncImage(image,exception);
1132 Convert bitfield encoded 16-bit PseudoColor scanline.
1134 if (bmp_info.compression != BI_RGB &&
1135 bmp_info.compression != BI_BITFIELDS)
1137 pixels=(unsigned char *) RelinquishMagickMemory(pixels);
1138 ThrowReaderException(CorruptImageError,
1139 "UnrecognizedImageCompression");
1141 bytes_per_line=2*(image->columns+image->columns % 2);
1142 image->storage_class=DirectClass;
1143 for (y=(ssize_t) image->rows-1; y >= 0; y--)
1145 p=pixels+(image->rows-y-1)*bytes_per_line;
1146 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
1147 if (q == (Quantum *) NULL)
1149 for (x=0; x < (ssize_t) image->columns; x++)
1151 pixel=(size_t) (*p++);
1153 red=((pixel & bmp_info.red_mask) << shift.red) >> 16;
1154 if (quantum_bits.red == 5)
1155 red|=((red & 0xe000) >> 5);
1156 if (quantum_bits.red <= 8)
1157 red|=((red & 0xff00) >> 8);
1158 green=((pixel & bmp_info.green_mask) << shift.green) >> 16;
1159 if (quantum_bits.green == 5)
1160 green|=((green & 0xe000) >> 5);
1161 if (quantum_bits.green == 6)
1162 green|=((green & 0xc000) >> 6);
1163 if (quantum_bits.green <= 8)
1164 green|=((green & 0xff00) >> 8);
1165 blue=((pixel & bmp_info.blue_mask) << shift.blue) >> 16;
1166 if (quantum_bits.blue == 5)
1167 blue|=((blue & 0xe000) >> 5);
1168 if (quantum_bits.blue <= 8)
1169 blue|=((blue & 0xff00) >> 8);
1170 opacity=((pixel & bmp_info.alpha_mask) << shift.alpha) >> 16;
1171 if (quantum_bits.alpha <= 8)
1172 opacity|=((opacity & 0xff00) >> 8);
1173 SetPixelRed(image,ScaleShortToQuantum((unsigned short) red),q);
1174 SetPixelGreen(image,ScaleShortToQuantum((unsigned short) green),q);
1175 SetPixelBlue(image,ScaleShortToQuantum((unsigned short) blue),q);
1176 SetPixelAlpha(image,OpaqueAlpha,q);
1177 if (image->matte != MagickFalse)
1178 SetPixelAlpha(image,
1179 ScaleShortToQuantum((unsigned short) opacity),q);
1180 q+=GetPixelChannels(image);
1182 if (SyncAuthenticPixels(image,exception) == MagickFalse)
1184 offset=(MagickOffsetType) (image->rows-y-1);
1185 if (image->previous == (Image *) NULL)
1187 status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y,
1189 if (status == MagickFalse)
1198 Convert DirectColor scanline.
1200 bytes_per_line=4*((image->columns*24+31)/32);
1201 for (y=(ssize_t) image->rows-1; y >= 0; y--)
1203 p=pixels+(image->rows-y-1)*bytes_per_line;
1204 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
1205 if (q == (Quantum *) NULL)
1207 for (x=0; x < (ssize_t) image->columns; x++)
1209 SetPixelBlue(image,ScaleCharToQuantum(*p++),q);
1210 SetPixelGreen(image,ScaleCharToQuantum(*p++),q);
1211 SetPixelRed(image,ScaleCharToQuantum(*p++),q);
1212 SetPixelAlpha(image,OpaqueAlpha,q);
1213 q+=GetPixelChannels(image);
1215 if (SyncAuthenticPixels(image,exception) == MagickFalse)
1217 offset=(MagickOffsetType) (image->rows-y-1);
1218 if (image->previous == (Image *) NULL)
1220 status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y,
1222 if (status == MagickFalse)
1231 Convert bitfield encoded DirectColor scanline.
1233 if ((bmp_info.compression != BI_RGB) &&
1234 (bmp_info.compression != BI_BITFIELDS))
1236 pixels=(unsigned char *) RelinquishMagickMemory(pixels);
1237 ThrowReaderException(CorruptImageError,
1238 "UnrecognizedImageCompression");
1240 bytes_per_line=4*(image->columns);
1241 for (y=(ssize_t) image->rows-1; y >= 0; y--)
1246 p=pixels+(image->rows-y-1)*bytes_per_line;
1247 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
1248 if (q == (Quantum *) NULL)
1250 for (x=0; x < (ssize_t) image->columns; x++)
1252 pixel=(size_t) (*p++);
1254 pixel|=(*p++ << 16);
1255 pixel|=(*p++ << 24);
1256 red=((pixel & bmp_info.red_mask) << shift.red) >> 16;
1257 if (quantum_bits.red == 8)
1259 green=((pixel & bmp_info.green_mask) << shift.green) >> 16;
1260 if (quantum_bits.green == 8)
1261 green|=(green >> 8);
1262 blue=((pixel & bmp_info.blue_mask) << shift.blue) >> 16;
1263 if (quantum_bits.blue == 8)
1265 opacity=((pixel & bmp_info.alpha_mask) << shift.alpha) >> 16;
1266 if (quantum_bits.alpha == 8)
1267 opacity|=(opacity >> 8);
1268 SetPixelRed(image,ScaleShortToQuantum((unsigned short) red),q);
1269 SetPixelGreen(image,ScaleShortToQuantum((unsigned short) green),q);
1270 SetPixelBlue(image,ScaleShortToQuantum((unsigned short) blue),q);
1271 SetPixelAlpha(image,OpaqueAlpha,q);
1272 if (image->matte != MagickFalse)
1273 SetPixelAlpha(image,
1274 ScaleShortToQuantum((unsigned short) opacity),q);
1275 q+=GetPixelChannels(image);
1277 if (SyncAuthenticPixels(image,exception) == MagickFalse)
1279 offset=(MagickOffsetType) (image->rows-y-1);
1280 if (image->previous == (Image *) NULL)
1282 status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y,
1284 if (status == MagickFalse)
1292 pixels=(unsigned char *) RelinquishMagickMemory(pixels);
1293 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
1296 pixels=(unsigned char *) RelinquishMagickMemory(pixels);
1297 if (EOFBlob(image) != MagickFalse)
1299 ThrowFileException(exception,CorruptImageError,"UnexpectedEndOfFile",
1303 if (bmp_info.height < 0)
1309 Correct image orientation.
1311 flipped_image=FlipImage(image,exception);
1312 if (flipped_image != (Image *) NULL)
1314 DuplicateBlob(flipped_image,image);
1315 image=DestroyImage(image);
1316 image=flipped_image;
1320 Proceed to next image.
1322 if (image_info->number_scenes != 0)
1323 if (image->scene >= (image_info->scene+image_info->number_scenes-1))
1326 if (bmp_info.ba_offset != 0)
1328 offset=SeekBlob(image,(MagickOffsetType) bmp_info.ba_offset,SEEK_SET);
1330 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
1332 count=ReadBlob(image,2,magick);
1333 if ((count == 2) && (IsBMP(magick,2) != MagickFalse))
1336 Acquire next image structure.
1338 AcquireNextImage(image_info,image,exception);
1339 if (GetNextImageInList(image) == (Image *) NULL)
1341 image=DestroyImageList(image);
1342 return((Image *) NULL);
1344 image=SyncNextImageInList(image);
1345 status=SetImageProgress(image,LoadImagesTag,TellBlob(image),
1346 GetBlobSize(image));
1347 if (status == MagickFalse)
1350 } while (IsBMP(magick,2) != MagickFalse);
1351 (void) CloseBlob(image);
1352 return(GetFirstImageInList(image));
1356 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1360 % R e g i s t e r B M P I m a g e %
1364 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1366 % RegisterBMPImage() adds attributes for the BMP image format to
1367 % the list of supported formats. The attributes include the image format
1368 % tag, a method to read and/or write the format, whether the format
1369 % supports the saving of more than one frame to the same file or blob,
1370 % whether the format supports native in-memory I/O, and a brief
1371 % description of the format.
1373 % The format of the RegisterBMPImage method is:
1375 % size_t RegisterBMPImage(void)
1378 ModuleExport size_t RegisterBMPImage(void)
1383 entry=SetMagickInfo("BMP");
1384 entry->decoder=(DecodeImageHandler *) ReadBMPImage;
1385 entry->encoder=(EncodeImageHandler *) WriteBMPImage;
1386 entry->magick=(IsImageFormatHandler *) IsBMP;
1387 entry->description=ConstantString("Microsoft Windows bitmap image");
1388 entry->module=ConstantString("BMP");
1389 entry->adjoin=MagickFalse;
1390 entry->seekable_stream=MagickTrue;
1391 (void) RegisterMagickInfo(entry);
1392 entry=SetMagickInfo("BMP2");
1393 entry->encoder=(EncodeImageHandler *) WriteBMPImage;
1394 entry->magick=(IsImageFormatHandler *) IsBMP;
1395 entry->description=ConstantString("Microsoft Windows bitmap image (V2)");
1396 entry->module=ConstantString("BMP");
1397 entry->adjoin=MagickFalse;
1398 entry->seekable_stream=MagickTrue;
1399 (void) RegisterMagickInfo(entry);
1400 entry=SetMagickInfo("BMP3");
1401 entry->encoder=(EncodeImageHandler *) WriteBMPImage;
1402 entry->magick=(IsImageFormatHandler *) IsBMP;
1403 entry->description=ConstantString("Microsoft Windows bitmap image (V3)");
1404 entry->module=ConstantString("BMP");
1405 entry->adjoin=MagickFalse;
1406 entry->seekable_stream=MagickTrue;
1407 (void) RegisterMagickInfo(entry);
1408 return(MagickImageCoderSignature);
1412 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1416 % U n r e g i s t e r B M P I m a g e %
1420 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1422 % UnregisterBMPImage() removes format registrations made by the
1423 % BMP module from the list of supported formats.
1425 % The format of the UnregisterBMPImage method is:
1427 % UnregisterBMPImage(void)
1430 ModuleExport void UnregisterBMPImage(void)
1432 (void) UnregisterMagickInfo("BMP");
1433 (void) UnregisterMagickInfo("BMP2");
1434 (void) UnregisterMagickInfo("BMP3");
1438 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1442 % W r i t e B M P I m a g e %
1446 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1448 % WriteBMPImage() writes an image in Microsoft Windows bitmap encoded
1449 % image format, version 3 for Windows or (if the image has a matte channel)
1452 % The format of the WriteBMPImage method is:
1454 % MagickBooleanType WriteBMPImage(const ImageInfo *image_info,
1455 % Image *image,ExceptionInfo *exception)
1457 % A description of each parameter follows.
1459 % o image_info: the image info.
1461 % o image: The image.
1463 % o exception: return any errors or warnings in this structure.
1466 static MagickBooleanType WriteBMPImage(const ImageInfo *image_info,Image *image,
1467 ExceptionInfo *exception)
1482 register const Quantum
1489 register unsigned char
1504 Open output image file.
1506 assert(image_info != (const ImageInfo *) NULL);
1507 assert(image_info->signature == MagickSignature);
1508 assert(image != (Image *) NULL);
1509 assert(image->signature == MagickSignature);
1510 if (image->debug != MagickFalse)
1511 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1512 assert(exception != (ExceptionInfo *) NULL);
1513 assert(exception->signature == MagickSignature);
1514 status=OpenBlob(image_info,image,WriteBinaryBlobMode,exception);
1515 if (status == MagickFalse)
1518 if (LocaleCompare(image_info->magick,"BMP2") == 0)
1521 if (LocaleCompare(image_info->magick,"BMP3") == 0)
1527 Initialize BMP raster file header.
1529 if (IsRGBColorspace(image->colorspace) == MagickFalse)
1530 (void) TransformImageColorspace(image,sRGBColorspace,exception);
1531 (void) ResetMagickMemory(&bmp_info,0,sizeof(bmp_info));
1532 bmp_info.file_size=14+12;
1534 bmp_info.file_size+=28;
1535 bmp_info.offset_bits=bmp_info.file_size;
1536 bmp_info.compression=BI_RGB;
1537 if ((image->storage_class == PseudoClass) && (image->colors > 256))
1538 (void) SetImageStorageClass(image,DirectClass,exception);
1539 if (image->storage_class != DirectClass)
1542 Colormapped BMP raster.
1544 bmp_info.bits_per_pixel=8;
1545 if (image->colors <= 2)
1546 bmp_info.bits_per_pixel=1;
1548 if (image->colors <= 16)
1549 bmp_info.bits_per_pixel=4;
1551 if (image->colors <= 256)
1552 bmp_info.bits_per_pixel=8;
1553 if (image_info->compression == RLECompression)
1554 bmp_info.bits_per_pixel=8;
1555 bmp_info.number_colors=1U << bmp_info.bits_per_pixel;
1556 if (image->matte != MagickFalse)
1557 (void) SetImageStorageClass(image,DirectClass,exception);
1559 if ((size_t) bmp_info.number_colors < image->colors)
1560 (void) SetImageStorageClass(image,DirectClass,exception);
1563 bmp_info.file_size+=3*(1UL << bmp_info.bits_per_pixel);
1564 bmp_info.offset_bits+=3*(1UL << bmp_info.bits_per_pixel);
1567 bmp_info.file_size+=(1UL << bmp_info.bits_per_pixel);
1568 bmp_info.offset_bits+=(1UL << bmp_info.bits_per_pixel);
1572 if (image->storage_class == DirectClass)
1575 Full color BMP raster.
1577 bmp_info.number_colors=0;
1578 bmp_info.bits_per_pixel=(unsigned short)
1579 ((type > 3) && (image->matte != MagickFalse) ? 32 : 24);
1580 bmp_info.compression=(unsigned int) ((type > 3) &&
1581 (image->matte != MagickFalse) ? BI_BITFIELDS : BI_RGB);
1583 bytes_per_line=4*((image->columns*bmp_info.bits_per_pixel+31)/32);
1584 bmp_info.ba_offset=0;
1585 profile=GetImageProfile(image,"icc");
1586 have_color_info=(image->rendering_intent != UndefinedIntent) ||
1587 (profile != (StringInfo *) NULL) || (image->gamma != 0.0) ? MagickTrue :
1592 if ((type == 3) || ((image->matte == MagickFalse) &&
1593 (have_color_info == MagickFalse)))
1605 if ((image->rendering_intent != UndefinedIntent) ||
1606 (profile != (StringInfo *) NULL))
1611 bmp_info.file_size+=extra_size;
1612 bmp_info.offset_bits+=extra_size;
1614 bmp_info.width=(ssize_t) image->columns;
1615 bmp_info.height=(ssize_t) image->rows;
1617 bmp_info.image_size=(unsigned int) (bytes_per_line*image->rows);
1618 bmp_info.file_size+=bmp_info.image_size;
1619 bmp_info.x_pixels=75*39;
1620 bmp_info.y_pixels=75*39;
1621 switch (image->units)
1623 case UndefinedResolution:
1624 case PixelsPerInchResolution:
1626 bmp_info.x_pixels=(unsigned int) (100.0*image->resolution.x/2.54);
1627 bmp_info.y_pixels=(unsigned int) (100.0*image->resolution.y/2.54);
1630 case PixelsPerCentimeterResolution:
1632 bmp_info.x_pixels=(unsigned int) (100.0*image->resolution.x);
1633 bmp_info.y_pixels=(unsigned int) (100.0*image->resolution.y);
1637 bmp_info.colors_important=bmp_info.number_colors;
1639 Convert MIFF to BMP raster pixels.
1641 pixels=(unsigned char *) AcquireQuantumMemory((size_t) bmp_info.image_size,
1643 if (pixels == (unsigned char *) NULL)
1644 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
1645 (void) ResetMagickMemory(pixels,0,(size_t) bmp_info.image_size);
1646 switch (bmp_info.bits_per_pixel)
1655 Convert PseudoClass image to a BMP monochrome image.
1657 for (y=0; y < (ssize_t) image->rows; y++)
1662 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
1663 if (p == (const Quantum *) NULL)
1665 q=pixels+(image->rows-y-1)*bytes_per_line;
1668 for (x=0; x < (ssize_t) image->columns; x++)
1671 byte|=GetPixelIndex(image,p) != 0 ? 0x01 : 0x00;
1675 *q++=(unsigned char) byte;
1679 p+=GetPixelChannels(image);
1683 *q++=(unsigned char) (byte << (8-bit));
1686 offset=(ssize_t) (image->columns+7)/8;
1687 for (x=offset; x < (ssize_t) bytes_per_line; x++)
1689 if (image->previous == (Image *) NULL)
1691 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
1693 if (status == MagickFalse)
1707 Convert PseudoClass image to a BMP monochrome image.
1709 for (y=0; y < (ssize_t) image->rows; y++)
1711 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
1712 if (p == (const Quantum *) NULL)
1714 q=pixels+(image->rows-y-1)*bytes_per_line;
1717 for (x=0; x < (ssize_t) image->columns; x++)
1720 byte|=((size_t) GetPixelIndex(image,p) & 0x0f);
1724 *q++=(unsigned char) byte;
1728 p+=GetPixelChannels(image);
1732 *q++=(unsigned char) (byte << 4);
1735 offset=(ssize_t) (image->columns+1)/2;
1736 for (x=offset; x < (ssize_t) bytes_per_line; x++)
1738 if (image->previous == (Image *) NULL)
1740 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
1742 if (status == MagickFalse)
1751 Convert PseudoClass packet to BMP pixel.
1753 for (y=0; y < (ssize_t) image->rows; y++)
1755 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
1756 if (p == (const Quantum *) NULL)
1758 q=pixels+(image->rows-y-1)*bytes_per_line;
1759 for (x=0; x < (ssize_t) image->columns; x++)
1761 *q++=(unsigned char) GetPixelIndex(image,p);
1762 p+=GetPixelChannels(image);
1764 for ( ; x < (ssize_t) bytes_per_line; x++)
1766 if (image->previous == (Image *) NULL)
1768 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
1770 if (status == MagickFalse)
1779 Convert DirectClass packet to BMP BGR888.
1781 for (y=0; y < (ssize_t) image->rows; y++)
1783 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
1784 if (p == (const Quantum *) NULL)
1786 q=pixels+(image->rows-y-1)*bytes_per_line;
1787 for (x=0; x < (ssize_t) image->columns; x++)
1789 *q++=ScaleQuantumToChar(GetPixelBlue(image,p));
1790 *q++=ScaleQuantumToChar(GetPixelGreen(image,p));
1791 *q++=ScaleQuantumToChar(GetPixelRed(image,p));
1792 p+=GetPixelChannels(image);
1794 for (x=3L*(ssize_t) image->columns; x < (ssize_t) bytes_per_line; x++)
1796 if (image->previous == (Image *) NULL)
1798 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
1800 if (status == MagickFalse)
1809 Convert DirectClass packet to ARGB8888 pixel.
1811 for (y=0; y < (ssize_t) image->rows; y++)
1813 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
1814 if (p == (const Quantum *) NULL)
1816 q=pixels+(image->rows-y-1)*bytes_per_line;
1817 for (x=0; x < (ssize_t) image->columns; x++)
1819 *q++=ScaleQuantumToChar(GetPixelBlue(image,p));
1820 *q++=ScaleQuantumToChar(GetPixelGreen(image,p));
1821 *q++=ScaleQuantumToChar(GetPixelRed(image,p));
1822 *q++=ScaleQuantumToChar(GetPixelAlpha(image,p));
1823 p+=GetPixelChannels(image);
1825 if (image->previous == (Image *) NULL)
1827 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
1829 if (status == MagickFalse)
1836 if ((type > 2) && (bmp_info.bits_per_pixel == 8))
1837 if (image_info->compression != NoCompression)
1843 Convert run-length encoded raster pixels.
1845 length=(size_t) (2*(bytes_per_line+2)*(image->rows+2)+2);
1846 bmp_data=(unsigned char *) NULL;
1847 if (~length >= bytes_per_line)
1848 bmp_data=(unsigned char *) AcquireQuantumMemory(length+
1849 bytes_per_line,sizeof(*bmp_data));
1850 if (bmp_data == (unsigned char *) NULL)
1852 pixels=(unsigned char *) RelinquishMagickMemory(pixels);
1853 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
1855 bmp_info.file_size-=bmp_info.image_size;
1856 bmp_info.image_size=(unsigned int) EncodeImage(image,bytes_per_line,
1858 bmp_info.file_size+=bmp_info.image_size;
1859 pixels=(unsigned char *) RelinquishMagickMemory(pixels);
1861 bmp_info.compression=BI_RLE8;
1864 Write BMP for Windows, all versions, 14-byte header.
1866 if (image->debug != MagickFalse)
1868 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1869 " Writing BMP version %.20g datastream",(double) type);
1870 if (image->storage_class == DirectClass)
1871 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1872 " Storage class=DirectClass");
1874 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1875 " Storage class=PseudoClass");
1876 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1877 " Image depth=%.20g",(double) image->depth);
1878 if (image->matte != MagickFalse)
1879 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1882 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1883 " Matte=MagickFalse");
1884 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1885 " BMP bits_per_pixel=%.20g",(double) bmp_info.bits_per_pixel);
1886 switch ((int) bmp_info.compression)
1890 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1891 " Compression=BI_RGB");
1896 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1897 " Compression=BI_RLE8");
1902 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1903 " Compression=BI_BITFIELDS");
1908 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1909 " Compression=UNKNOWN (%u)",bmp_info.compression);
1913 if (bmp_info.number_colors == 0)
1914 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1915 " Number_colors=unspecified");
1917 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1918 " Number_colors=%u",bmp_info.number_colors);
1920 (void) WriteBlob(image,2,(unsigned char *) "BM");
1921 (void) WriteBlobLSBLong(image,bmp_info.file_size);
1922 (void) WriteBlobLSBLong(image,bmp_info.ba_offset); /* always 0 */
1923 (void) WriteBlobLSBLong(image,bmp_info.offset_bits);
1927 Write 12-byte version 2 bitmap header.
1929 (void) WriteBlobLSBLong(image,bmp_info.size);
1930 (void) WriteBlobLSBShort(image,(unsigned short) bmp_info.width);
1931 (void) WriteBlobLSBShort(image,(unsigned short) bmp_info.height);
1932 (void) WriteBlobLSBShort(image,bmp_info.planes);
1933 (void) WriteBlobLSBShort(image,bmp_info.bits_per_pixel);
1938 Write 40-byte version 3+ bitmap header.
1940 (void) WriteBlobLSBLong(image,bmp_info.size);
1941 (void) WriteBlobLSBLong(image,(unsigned int) bmp_info.width);
1942 (void) WriteBlobLSBLong(image,(unsigned int) bmp_info.height);
1943 (void) WriteBlobLSBShort(image,bmp_info.planes);
1944 (void) WriteBlobLSBShort(image,bmp_info.bits_per_pixel);
1945 (void) WriteBlobLSBLong(image,bmp_info.compression);
1946 (void) WriteBlobLSBLong(image,bmp_info.image_size);
1947 (void) WriteBlobLSBLong(image,bmp_info.x_pixels);
1948 (void) WriteBlobLSBLong(image,bmp_info.y_pixels);
1949 (void) WriteBlobLSBLong(image,bmp_info.number_colors);
1950 (void) WriteBlobLSBLong(image,bmp_info.colors_important);
1952 if ((type > 3) && ((image->matte != MagickFalse) ||
1953 (have_color_info != MagickFalse)))
1956 Write the rest of the 108-byte BMP Version 4 header.
1958 (void) WriteBlobLSBLong(image,0x00ff0000U); /* Red mask */
1959 (void) WriteBlobLSBLong(image,0x0000ff00U); /* Green mask */
1960 (void) WriteBlobLSBLong(image,0x000000ffU); /* Blue mask */
1961 (void) WriteBlobLSBLong(image,0xff000000U); /* Alpha mask */
1962 (void) WriteBlobLSBLong(image,0x00000001U); /* CSType==Calib. RGB */
1963 (void) WriteBlobLSBLong(image,(unsigned int)
1964 image->chromaticity.red_primary.x*0x3ffffff);
1965 (void) WriteBlobLSBLong(image,(unsigned int)
1966 image->chromaticity.red_primary.y*0x3ffffff);
1967 (void) WriteBlobLSBLong(image,(unsigned int)
1968 (1.000f-(image->chromaticity.red_primary.x+
1969 image->chromaticity.red_primary.y)*0x3ffffff));
1970 (void) WriteBlobLSBLong(image,(unsigned int)
1971 image->chromaticity.green_primary.x*0x3ffffff);
1972 (void) WriteBlobLSBLong(image,(unsigned int)
1973 image->chromaticity.green_primary.y*0x3ffffff);
1974 (void) WriteBlobLSBLong(image,(unsigned int)
1975 (1.000f-(image->chromaticity.green_primary.x+
1976 image->chromaticity.green_primary.y)*0x3ffffff));
1977 (void) WriteBlobLSBLong(image,(unsigned int)
1978 image->chromaticity.blue_primary.x*0x3ffffff);
1979 (void) WriteBlobLSBLong(image,(unsigned int)
1980 image->chromaticity.blue_primary.y*0x3ffffff);
1981 (void) WriteBlobLSBLong(image,(unsigned int)
1982 (1.000f-(image->chromaticity.blue_primary.x+
1983 image->chromaticity.blue_primary.y)*0x3ffffff));
1984 (void) WriteBlobLSBLong(image,(unsigned int)
1985 bmp_info.gamma_scale.x*0xffff);
1986 (void) WriteBlobLSBLong(image,(unsigned int)
1987 bmp_info.gamma_scale.y*0xffff);
1988 (void) WriteBlobLSBLong(image,(unsigned int)
1989 bmp_info.gamma_scale.z*0xffff);
1990 if ((image->rendering_intent != UndefinedIntent) ||
1991 (profile != (StringInfo *) NULL))
1996 switch ((int) image->rendering_intent)
1998 case SaturationIntent:
2000 intent=LCS_GM_BUSINESS;
2003 case RelativeIntent:
2005 intent=LCS_GM_GRAPHICS;
2008 case PerceptualIntent:
2010 intent=LCS_GM_IMAGES;
2013 case AbsoluteIntent:
2015 intent=LCS_GM_ABS_COLORIMETRIC;
2024 (void) WriteBlobLSBLong(image,(unsigned int) intent);
2025 (void) WriteBlobLSBLong(image,0x00); /* dummy profile data */
2026 (void) WriteBlobLSBLong(image,0x00); /* dummy profile length */
2027 (void) WriteBlobLSBLong(image,0x00); /* reserved */
2030 if (image->storage_class == PseudoClass)
2036 Dump colormap to file.
2038 if (image->debug != MagickFalse)
2039 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
2040 " Colormap: %.20g entries",(double) image->colors);
2041 bmp_colormap=(unsigned char *) AcquireQuantumMemory((size_t) (1UL <<
2042 bmp_info.bits_per_pixel),4*sizeof(*bmp_colormap));
2043 if (bmp_colormap == (unsigned char *) NULL)
2044 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
2046 for (i=0; i < (ssize_t) MagickMin((ssize_t) image->colors,(ssize_t) bmp_info.number_colors); i++)
2048 *q++=ScaleQuantumToChar(image->colormap[i].blue);
2049 *q++=ScaleQuantumToChar(image->colormap[i].green);
2050 *q++=ScaleQuantumToChar(image->colormap[i].red);
2052 *q++=(unsigned char) 0x0;
2054 for ( ; i < (ssize_t) (1UL << bmp_info.bits_per_pixel); i++)
2056 *q++=(unsigned char) 0x00;
2057 *q++=(unsigned char) 0x00;
2058 *q++=(unsigned char) 0x00;
2060 *q++=(unsigned char) 0x00;
2063 (void) WriteBlob(image,(size_t) (3*(1L << bmp_info.bits_per_pixel)),
2066 (void) WriteBlob(image,(size_t) (4*(1L << bmp_info.bits_per_pixel)),
2068 bmp_colormap=(unsigned char *) RelinquishMagickMemory(bmp_colormap);
2070 if (image->debug != MagickFalse)
2071 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
2072 " Pixels: %u bytes",bmp_info.image_size);
2073 (void) WriteBlob(image,(size_t) bmp_info.image_size,pixels);
2074 pixels=(unsigned char *) RelinquishMagickMemory(pixels);
2075 if (GetNextImageInList(image) == (Image *) NULL)
2077 image=SyncNextImageInList(image);
2078 status=SetImageProgress(image,SaveImagesTag,scene++,
2079 GetImageListLength(image));
2080 if (status == MagickFalse)
2082 } while (image_info->adjoin != MagickFalse);
2083 (void) CloseBlob(image);