2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
13 % Read/Write Microsoft Windows Bitmap Image Format %
17 % Glenn Randers-Pehrson %
21 % Copyright 1999-2013 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__) || defined(__MINGW64__)
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)
216 register unsigned char
227 assert(image != (Image *) NULL);
228 assert(image->signature == MagickSignature);
229 if (image->debug != MagickFalse)
230 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
231 assert(pixels != (unsigned char *) NULL);
232 (void) ResetMagickMemory(pixels,0,(size_t) image->columns*image->rows*
237 q=pixels+(size_t) image->columns*image->rows;
238 for (y=0; y < (ssize_t) image->rows; )
240 if ((p < pixels) || (p >= q))
242 count=(ssize_t) ReadBlobByte(image);
243 if ((int) count == EOF)
250 count=MagickMin(count,(ssize_t) (q-p));
251 byte=(unsigned char) ReadBlobByte(image);
252 if (compression == BI_RLE8)
254 for (i=0; i < count; i++)
255 *p++=(unsigned char) byte;
259 for (i=0; i < count; i++)
261 ((i & 0x01) != 0 ? (byte & 0x0f) : ((byte >> 4) & 0x0f));
270 count=(ssize_t) ReadBlobByte(image);
282 p=pixels+y*image->columns;
290 x+=ReadBlobByte(image);
291 y+=ReadBlobByte(image);
292 p=pixels+y*image->columns+x;
300 count=MagickMin(count,(ssize_t) (q-p));
301 if (compression == BI_RLE8)
302 for (i=0; i < count; i++)
303 *p++=(unsigned char) ReadBlobByte(image);
305 for (i=0; i < count; i++)
308 byte=(unsigned char) ReadBlobByte(image);
310 ((i & 0x01) != 0 ? (byte & 0x0f) : ((byte >> 4) & 0x0f));
316 if (compression == BI_RLE8)
318 if ((count & 0x01) != 0)
319 (void) ReadBlobByte(image);
322 if (((count & 0x03) == 1) || ((count & 0x03) == 2))
323 (void) ReadBlobByte(image);
328 if (SetImageProgress(image,LoadImageTag,(MagickOffsetType) y,image->rows) == MagickFalse)
331 (void) ReadBlobByte(image); /* end of line */
332 (void) ReadBlobByte(image);
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 == MagickSignature);
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)
524 register unsigned char
547 assert(image_info != (const ImageInfo *) NULL);
548 assert(image_info->signature == MagickSignature);
549 if (image_info->debug != MagickFalse)
550 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
551 image_info->filename);
552 assert(exception != (ExceptionInfo *) NULL);
553 assert(exception->signature == MagickSignature);
554 image=AcquireImage(image_info,exception);
555 status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception);
556 if (status == MagickFalse)
558 image=DestroyImageList(image);
559 return((Image *) NULL);
562 Determine if this a BMP file.
564 (void) ResetMagickMemory(&bmp_info,0,sizeof(bmp_info));
565 bmp_info.ba_offset=0;
567 count=ReadBlob(image,2,magick);
581 Verify BMP identifier.
583 if (bmp_info.ba_offset == 0)
584 start_position=TellBlob(image)-2;
585 bmp_info.ba_offset=0;
586 while (LocaleNCompare((char *) magick,"BA",2) == 0)
588 bmp_info.file_size=ReadBlobLSBLong(image);
589 bmp_info.ba_offset=ReadBlobLSBLong(image);
590 bmp_info.offset_bits=ReadBlobLSBLong(image);
591 count=ReadBlob(image,2,magick);
595 if (image->debug != MagickFalse)
596 (void) LogMagickEvent(CoderEvent,GetMagickModule()," Magick: %c%c",
597 magick[0],magick[1]);
598 if ((count == 0) || ((LocaleNCompare((char *) magick,"BM",2) != 0) &&
599 (LocaleNCompare((char *) magick,"CI",2) != 0)))
600 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
601 bmp_info.file_size=ReadBlobLSBLong(image);
602 (void) ReadBlobLSBLong(image);
603 bmp_info.offset_bits=ReadBlobLSBLong(image);
604 bmp_info.size=ReadBlobLSBLong(image);
605 if (image->debug != MagickFalse)
606 (void) LogMagickEvent(CoderEvent,GetMagickModule()," BMP size: %u",
608 if (bmp_info.size == 12)
613 bmp_info.width=(short) ReadBlobLSBShort(image);
614 bmp_info.height=(short) ReadBlobLSBShort(image);
615 bmp_info.planes=ReadBlobLSBShort(image);
616 bmp_info.bits_per_pixel=ReadBlobLSBShort(image);
619 bmp_info.number_colors=0;
620 bmp_info.compression=BI_RGB;
621 bmp_info.image_size=0;
622 bmp_info.alpha_mask=0;
623 if (image->debug != MagickFalse)
625 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
626 " Format: OS/2 Bitmap");
627 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
628 " Geometry: %.20gx%.20g",(double) bmp_info.width,(double)
635 Microsoft Windows BMP image file.
637 if (bmp_info.size < 40)
638 ThrowReaderException(CorruptImageError,"NonOS2HeaderSizeError");
639 bmp_info.width=(int) ReadBlobLSBLong(image);
640 bmp_info.height=(int) ReadBlobLSBLong(image);
641 bmp_info.planes=ReadBlobLSBShort(image);
642 bmp_info.bits_per_pixel=ReadBlobLSBShort(image);
643 bmp_info.compression=ReadBlobLSBLong(image);
644 bmp_info.image_size=ReadBlobLSBLong(image);
645 bmp_info.x_pixels=ReadBlobLSBLong(image);
646 bmp_info.y_pixels=ReadBlobLSBLong(image);
647 bmp_info.number_colors=ReadBlobLSBLong(image);
648 bmp_info.colors_important=ReadBlobLSBLong(image);
651 if (image->debug != MagickFalse)
653 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
654 " Format: MS Windows bitmap");
655 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
656 " Geometry: %.20gx%.20g",(double) bmp_info.width,(double)
658 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
659 " Bits per pixel: %.20g",(double) bmp_info.bits_per_pixel);
660 switch ((int) bmp_info.compression)
664 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
665 " Compression: BI_RGB");
670 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
671 " Compression: BI_RLE4");
676 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
677 " Compression: BI_RLE8");
682 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
683 " Compression: BI_BITFIELDS");
688 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
689 " Compression: BI_PNG");
694 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
695 " Compression: BI_JPEG");
700 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
701 " Compression: UNKNOWN (%u)",bmp_info.compression);
704 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
705 " Number of colors: %u",bmp_info.number_colors);
707 bmp_info.red_mask=ReadBlobLSBLong(image);
708 bmp_info.green_mask=ReadBlobLSBLong(image);
709 bmp_info.blue_mask=ReadBlobLSBLong(image);
710 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 # define BMP_DENOM ((double) 0x40000000)
725 bmp_info.red_primary.x=(double) ReadBlobLSBLong(image)/BMP_DENOM;
726 bmp_info.red_primary.y=(double) ReadBlobLSBLong(image)/BMP_DENOM;
727 bmp_info.red_primary.z=(double) ReadBlobLSBLong(image)/BMP_DENOM;
728 bmp_info.green_primary.x=(double) ReadBlobLSBLong(image)/BMP_DENOM;
729 bmp_info.green_primary.y=(double) ReadBlobLSBLong(image)/BMP_DENOM;
730 bmp_info.green_primary.z=(double) ReadBlobLSBLong(image)/BMP_DENOM;
731 bmp_info.blue_primary.x=(double) ReadBlobLSBLong(image)/BMP_DENOM;
732 bmp_info.blue_primary.y=(double) ReadBlobLSBLong(image)/BMP_DENOM;
733 bmp_info.blue_primary.z=(double) ReadBlobLSBLong(image)/BMP_DENOM;
735 sum=bmp_info.red_primary.x+bmp_info.red_primary.y+
736 bmp_info.red_primary.z;
737 bmp_info.red_primary.x/=sum;
738 bmp_info.red_primary.y/=sum;
739 image->chromaticity.red_primary.x=bmp_info.red_primary.x;
740 image->chromaticity.red_primary.y=bmp_info.red_primary.y;
742 sum=bmp_info.green_primary.x+bmp_info.green_primary.y+
743 bmp_info.green_primary.z;
744 bmp_info.green_primary.x/=sum;
745 bmp_info.green_primary.y/=sum;
746 image->chromaticity.green_primary.x=bmp_info.green_primary.x;
747 image->chromaticity.green_primary.y=bmp_info.green_primary.y;
749 sum=bmp_info.blue_primary.x+bmp_info.blue_primary.y+
750 bmp_info.blue_primary.z;
751 bmp_info.blue_primary.x/=sum;
752 bmp_info.blue_primary.y/=sum;
753 image->chromaticity.blue_primary.x=bmp_info.blue_primary.x;
754 image->chromaticity.blue_primary.y=bmp_info.blue_primary.y;
757 Decode 16^16 fixed point formatted gamma_scales.
759 bmp_info.gamma_scale.x=(double) ReadBlobLSBLong(image)/0x10000;
760 bmp_info.gamma_scale.y=(double) ReadBlobLSBLong(image)/0x10000;
761 bmp_info.gamma_scale.z=(double) ReadBlobLSBLong(image)/0x10000;
763 Compute a single gamma from the BMP 3-channel gamma.
765 image->gamma=(bmp_info.gamma_scale.x+bmp_info.gamma_scale.y+
766 bmp_info.gamma_scale.z)/3.0;
769 if (bmp_info.size > 108)
775 Read BMP Version 5 color management information.
777 intent=ReadBlobLSBLong(image);
778 switch ((int) intent)
780 case LCS_GM_BUSINESS:
782 image->rendering_intent=SaturationIntent;
785 case LCS_GM_GRAPHICS:
787 image->rendering_intent=RelativeIntent;
792 image->rendering_intent=PerceptualIntent;
795 case LCS_GM_ABS_COLORIMETRIC:
797 image->rendering_intent=AbsoluteIntent;
801 profile_data=ReadBlobLSBLong(image);
802 profile_size=ReadBlobLSBLong(image);
805 (void) ReadBlobLSBLong(image); /* Reserved byte */
808 if ((MagickSizeType) bmp_info.file_size > GetBlobSize(image))
809 (void) ThrowMagickException(exception,GetMagickModule(),CorruptImageError,
810 "LengthAndFilesizeDoNotMatch","`%s'",image->filename);
812 if ((MagickSizeType) bmp_info.file_size < GetBlobSize(image))
813 (void) ThrowMagickException(exception,GetMagickModule(),
814 CorruptImageWarning,"LengthAndFilesizeDoNotMatch","`%s'",
816 if (bmp_info.width <= 0)
817 ThrowReaderException(CorruptImageError,"NegativeOrZeroImageSize");
818 if (bmp_info.height == 0)
819 ThrowReaderException(CorruptImageError,"NegativeOrZeroImageSize");
820 if (bmp_info.planes != 1)
821 ThrowReaderException(CorruptImageError,"StaticPlanesValueNotEqualToOne");
822 if ((bmp_info.bits_per_pixel != 1) && (bmp_info.bits_per_pixel != 4) &&
823 (bmp_info.bits_per_pixel != 8) && (bmp_info.bits_per_pixel != 16) &&
824 (bmp_info.bits_per_pixel != 24) && (bmp_info.bits_per_pixel != 32))
825 ThrowReaderException(CorruptImageError,"UnrecognizedBitsPerPixel");
826 if (bmp_info.bits_per_pixel < 16 &&
827 bmp_info.number_colors > (1U << bmp_info.bits_per_pixel))
829 ThrowReaderException(CorruptImageError,
830 "UnrecognizedNumberOfColors");
832 if ((bmp_info.compression == 1) && (bmp_info.bits_per_pixel != 8))
833 ThrowReaderException(CorruptImageError,"UnrecognizedBitsPerPixel");
834 if ((bmp_info.compression == 2) && (bmp_info.bits_per_pixel != 4))
835 ThrowReaderException(CorruptImageError,"UnrecognizedBitsPerPixel");
836 if ((bmp_info.compression == 3) && (bmp_info.bits_per_pixel < 16))
837 ThrowReaderException(CorruptImageError,"UnrecognizedBitsPerPixel");
838 switch (bmp_info.compression)
846 ThrowReaderException(CoderError,"JPEGCompressNotSupported");
848 ThrowReaderException(CoderError,"PNGCompressNotSupported");
850 ThrowReaderException(CorruptImageError,"UnrecognizedImageCompression");
852 image->columns=(size_t) MagickAbsoluteValue((ssize_t) bmp_info.width);
853 image->rows=(size_t) MagickAbsoluteValue((ssize_t) bmp_info.height);
854 image->depth=bmp_info.bits_per_pixel <= 8 ? bmp_info.bits_per_pixel : 8;
855 image->alpha_trait=(bmp_info.alpha_mask != 0) &&
856 (bmp_info.compression == BI_BITFIELDS) ? BlendPixelTrait :
858 if (bmp_info.bits_per_pixel < 16)
863 image->storage_class=PseudoClass;
864 image->colors=bmp_info.number_colors;
866 if (image->colors == 0)
867 image->colors=one << bmp_info.bits_per_pixel;
869 if (image->storage_class == PseudoClass)
878 Read BMP raster colormap.
880 if (image->debug != MagickFalse)
881 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
882 " Reading colormap of %.20g colors",(double) image->colors);
883 if (AcquireImageColormap(image,image->colors,exception) == MagickFalse)
884 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
885 bmp_colormap=(unsigned char *) AcquireQuantumMemory((size_t)
886 image->colors,4*sizeof(*bmp_colormap));
887 if (bmp_colormap == (unsigned char *) NULL)
888 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
889 if ((bmp_info.size == 12) || (bmp_info.size == 64))
893 offset=SeekBlob(image,start_position+14+bmp_info.size,SEEK_SET);
895 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
896 count=ReadBlob(image,packet_size*image->colors,bmp_colormap);
897 if (count != (ssize_t) (packet_size*image->colors))
898 ThrowReaderException(CorruptImageError,"InsufficientImageDataInFile");
900 for (i=0; i < (ssize_t) image->colors; i++)
902 image->colormap[i].blue=(MagickRealType) ScaleCharToQuantum(*p++);
903 image->colormap[i].green=(MagickRealType) ScaleCharToQuantum(*p++);
904 image->colormap[i].red=(MagickRealType) ScaleCharToQuantum(*p++);
905 if (packet_size == 4)
908 bmp_colormap=(unsigned char *) RelinquishMagickMemory(bmp_colormap);
910 image->resolution.x=(double) bmp_info.x_pixels/100.0;
911 image->resolution.y=(double) bmp_info.y_pixels/100.0;
912 image->units=PixelsPerCentimeterResolution;
913 if ((image_info->ping != MagickFalse) && (image_info->number_scenes != 0))
914 if (image->scene >= (image_info->scene+image_info->number_scenes-1))
919 offset=SeekBlob(image,start_position+bmp_info.offset_bits,SEEK_SET);
921 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
922 if (bmp_info.compression == BI_RLE4)
923 bmp_info.bits_per_pixel<<=1;
924 bytes_per_line=4*((image->columns*bmp_info.bits_per_pixel+31)/32);
925 length=(size_t) bytes_per_line*image->rows;
927 if ((bmp_info.compression == BI_RGB) ||
928 (bmp_info.compression == BI_BITFIELDS))
930 if (image->debug != MagickFalse)
931 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
932 " Reading pixels (%.20g bytes)",(double) length);
933 if (GetBlobStreamData(image) != (unsigned char *) NULL)
936 pixels=GetBlobStreamData(image)+TellBlob(image);
937 if (DiscardBlobBytes(image,length) == MagickFalse)
938 ThrowReaderException(CorruptImageError,
939 "InsufficientImageDataInFile");
943 pixels=(unsigned char *) AcquireQuantumMemory((size_t) image->rows,
944 MagickMax(bytes_per_line,image->columns)*sizeof(*pixels));
945 if (pixels == (unsigned char *) NULL)
946 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
947 count=ReadBlob(image,length,pixels);
948 if (count != (ssize_t) length)
950 pixels=(unsigned char *) RelinquishMagickMemory(pixels);
951 ThrowReaderException(CorruptImageError,
952 "InsufficientImageDataInFile");
959 Convert run-length encoded raster pixels.
961 pixels=(unsigned char *) AcquireQuantumMemory((size_t) image->rows,
962 MagickMax(bytes_per_line,image->columns+256UL)*sizeof(*pixels));
963 if (pixels == (unsigned char *) NULL)
964 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
965 status=DecodeImage(image,bmp_info.compression,pixels);
966 if (status == MagickFalse)
968 pixels=(unsigned char *) RelinquishMagickMemory(pixels);
969 ThrowReaderException(CorruptImageError,
970 "UnableToRunlengthDecodeImage");
974 Convert BMP raster image to pixel packets.
976 if (bmp_info.compression == BI_RGB)
978 bmp_info.alpha_mask=image->alpha_trait == BlendPixelTrait ?
980 bmp_info.red_mask=0x00ff0000U;
981 bmp_info.green_mask=0x0000ff00U;
982 bmp_info.blue_mask=0x000000ffU;
983 if (bmp_info.bits_per_pixel == 16)
988 bmp_info.red_mask=0x00007c00U;
989 bmp_info.green_mask=0x000003e0U;
990 bmp_info.blue_mask=0x0000001fU;
993 if ((bmp_info.bits_per_pixel == 16) || (bmp_info.bits_per_pixel == 32))
999 Get shift and quantum bits info from bitfield masks.
1001 (void) ResetMagickMemory(&shift,0,sizeof(shift));
1002 (void) ResetMagickMemory(&quantum_bits,0,sizeof(quantum_bits));
1003 if (bmp_info.red_mask != 0)
1004 while (((bmp_info.red_mask << shift.red) & 0x80000000UL) == 0)
1006 if (bmp_info.green_mask != 0)
1007 while (((bmp_info.green_mask << shift.green) & 0x80000000UL) == 0)
1009 if (bmp_info.blue_mask != 0)
1010 while (((bmp_info.blue_mask << shift.blue) & 0x80000000UL) == 0)
1012 if (bmp_info.alpha_mask != 0)
1013 while (((bmp_info.alpha_mask << shift.alpha) & 0x80000000UL) == 0)
1016 while (((bmp_info.red_mask << sample) & 0x80000000UL) != 0)
1018 quantum_bits.red=(MagickRealType) (sample-shift.red);
1020 while (((bmp_info.green_mask << sample) & 0x80000000UL) != 0)
1022 quantum_bits.green=(MagickRealType) (sample-shift.green);
1024 while (((bmp_info.blue_mask << sample) & 0x80000000UL) != 0)
1026 quantum_bits.blue=(MagickRealType) (sample-shift.blue);
1028 while (((bmp_info.alpha_mask << sample) & 0x80000000UL) != 0)
1030 quantum_bits.alpha=(MagickRealType) (sample-shift.alpha);
1032 switch (bmp_info.bits_per_pixel)
1037 Convert bitmap scanline.
1039 for (y=(ssize_t) image->rows-1; y >= 0; y--)
1041 p=pixels+(image->rows-y-1)*bytes_per_line;
1042 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
1043 if (q == (Quantum *) NULL)
1045 for (x=0; x < ((ssize_t) image->columns-7); x+=8)
1047 for (bit=0; bit < 8; bit++)
1049 index=(Quantum) (((*p) & (0x80 >> bit)) != 0 ? 0x01 : 0x00);
1050 SetPixelIndex(image,index,q);
1051 q+=GetPixelChannels(image);
1055 if ((image->columns % 8) != 0)
1057 for (bit=0; bit < (image->columns % 8); bit++)
1059 index=(Quantum) (((*p) & (0x80 >> bit)) != 0 ? 0x01 : 0x00);
1060 SetPixelIndex(image,index,q);
1061 q+=GetPixelChannels(image);
1065 if (SyncAuthenticPixels(image,exception) == MagickFalse)
1067 if (image->previous == (Image *) NULL)
1069 status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y,
1071 if (status == MagickFalse)
1075 (void) SyncImage(image,exception);
1081 Convert PseudoColor scanline.
1083 for (y=(ssize_t) image->rows-1; y >= 0; y--)
1085 p=pixels+(image->rows-y-1)*bytes_per_line;
1086 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
1087 if (q == (Quantum *) NULL)
1089 for (x=0; x < ((ssize_t) image->columns-1); x+=2)
1091 index=ConstrainColormapIndex(image,(*p >> 4) & 0x0f,exception);
1092 SetPixelIndex(image,index,q);
1093 q+=GetPixelChannels(image);
1094 index=ConstrainColormapIndex(image,*p & 0x0f,exception);
1095 SetPixelIndex(image,index,q);
1096 q+=GetPixelChannels(image);
1099 if ((image->columns % 2) != 0)
1101 index=ConstrainColormapIndex(image,(*p >> 4) & 0xf,exception);
1102 SetPixelIndex(image,index,q);
1103 q+=GetPixelChannels(image);
1106 if (SyncAuthenticPixels(image,exception) == MagickFalse)
1108 if (image->previous == (Image *) NULL)
1110 status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y,
1112 if (status == MagickFalse)
1116 (void) SyncImage(image,exception);
1122 Convert PseudoColor scanline.
1124 if ((bmp_info.compression == BI_RLE8) ||
1125 (bmp_info.compression == BI_RLE4))
1126 bytes_per_line=image->columns;
1127 for (y=(ssize_t) image->rows-1; y >= 0; y--)
1129 p=pixels+(image->rows-y-1)*bytes_per_line;
1130 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
1131 if (q == (Quantum *) NULL)
1133 for (x=(ssize_t) image->columns; x != 0; --x)
1135 index=ConstrainColormapIndex(image,*p++,exception);
1136 SetPixelIndex(image,index,q);
1137 q+=GetPixelChannels(image);
1139 if (SyncAuthenticPixels(image,exception) == MagickFalse)
1141 offset=(MagickOffsetType) (image->rows-y-1);
1142 if (image->previous == (Image *) NULL)
1144 status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y,
1146 if (status == MagickFalse)
1150 (void) SyncImage(image,exception);
1159 Convert bitfield encoded 16-bit PseudoColor scanline.
1161 if (bmp_info.compression != BI_RGB &&
1162 bmp_info.compression != BI_BITFIELDS)
1164 if (mapped == MagickFalse)
1165 pixels=(unsigned char *) RelinquishMagickMemory(pixels);
1166 ThrowReaderException(CorruptImageError,
1167 "UnrecognizedImageCompression");
1169 bytes_per_line=2*(image->columns+image->columns % 2);
1170 image->storage_class=DirectClass;
1171 for (y=(ssize_t) image->rows-1; y >= 0; y--)
1173 p=pixels+(image->rows-y-1)*bytes_per_line;
1174 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
1175 if (q == (Quantum *) NULL)
1177 for (x=0; x < (ssize_t) image->columns; x++)
1179 pixel=(size_t) (*p++);
1181 red=((pixel & bmp_info.red_mask) << shift.red) >> 16;
1182 if (quantum_bits.red == 5)
1183 red|=((red & 0xe000) >> 5);
1184 if (quantum_bits.red <= 8)
1185 red|=((red & 0xff00) >> 8);
1186 green=((pixel & bmp_info.green_mask) << shift.green) >> 16;
1187 if (quantum_bits.green == 5)
1188 green|=((green & 0xe000) >> 5);
1189 if (quantum_bits.green == 6)
1190 green|=((green & 0xc000) >> 6);
1191 if (quantum_bits.green <= 8)
1192 green|=((green & 0xff00) >> 8);
1193 blue=((pixel & bmp_info.blue_mask) << shift.blue) >> 16;
1194 if (quantum_bits.blue == 5)
1195 blue|=((blue & 0xe000) >> 5);
1196 if (quantum_bits.blue <= 8)
1197 blue|=((blue & 0xff00) >> 8);
1198 opacity=((pixel & bmp_info.alpha_mask) << shift.alpha) >> 16;
1199 if (quantum_bits.alpha <= 8)
1200 opacity|=((opacity & 0xff00) >> 8);
1201 SetPixelRed(image,ScaleShortToQuantum((unsigned short) red),q);
1202 SetPixelGreen(image,ScaleShortToQuantum((unsigned short) green),q);
1203 SetPixelBlue(image,ScaleShortToQuantum((unsigned short) blue),q);
1204 SetPixelAlpha(image,OpaqueAlpha,q);
1205 if (image->alpha_trait == BlendPixelTrait)
1206 SetPixelAlpha(image,
1207 ScaleShortToQuantum((unsigned short) opacity),q);
1208 q+=GetPixelChannels(image);
1210 if (SyncAuthenticPixels(image,exception) == MagickFalse)
1212 offset=(MagickOffsetType) (image->rows-y-1);
1213 if (image->previous == (Image *) NULL)
1215 status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y,
1217 if (status == MagickFalse)
1226 Convert DirectColor scanline.
1228 bytes_per_line=4*((image->columns*24+31)/32);
1229 for (y=(ssize_t) image->rows-1; y >= 0; y--)
1231 p=pixels+(image->rows-y-1)*bytes_per_line;
1232 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
1233 if (q == (Quantum *) NULL)
1235 for (x=0; x < (ssize_t) image->columns; x++)
1237 SetPixelBlue(image,ScaleCharToQuantum(*p++),q);
1238 SetPixelGreen(image,ScaleCharToQuantum(*p++),q);
1239 SetPixelRed(image,ScaleCharToQuantum(*p++),q);
1240 SetPixelAlpha(image,OpaqueAlpha,q);
1241 q+=GetPixelChannels(image);
1243 if (SyncAuthenticPixels(image,exception) == MagickFalse)
1245 offset=(MagickOffsetType) (image->rows-y-1);
1246 if (image->previous == (Image *) NULL)
1248 status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y,
1250 if (status == MagickFalse)
1259 Convert bitfield encoded DirectColor scanline.
1261 if ((bmp_info.compression != BI_RGB) &&
1262 (bmp_info.compression != BI_BITFIELDS))
1264 if (mapped == MagickFalse)
1265 pixels=(unsigned char *) RelinquishMagickMemory(pixels);
1266 ThrowReaderException(CorruptImageError,
1267 "UnrecognizedImageCompression");
1269 bytes_per_line=4*(image->columns);
1270 for (y=(ssize_t) image->rows-1; y >= 0; y--)
1275 p=pixels+(image->rows-y-1)*bytes_per_line;
1276 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
1277 if (q == (Quantum *) NULL)
1279 for (x=0; x < (ssize_t) image->columns; x++)
1281 pixel=(size_t) (*p++);
1283 pixel|=(*p++ << 16);
1284 pixel|=(*p++ << 24);
1285 red=((pixel & bmp_info.red_mask) << shift.red) >> 16;
1286 if (quantum_bits.red == 8)
1288 green=((pixel & bmp_info.green_mask) << shift.green) >> 16;
1289 if (quantum_bits.green == 8)
1290 green|=(green >> 8);
1291 blue=((pixel & bmp_info.blue_mask) << shift.blue) >> 16;
1292 if (quantum_bits.blue == 8)
1294 opacity=((pixel & bmp_info.alpha_mask) << shift.alpha) >> 16;
1295 if (quantum_bits.alpha == 8)
1296 opacity|=(opacity >> 8);
1297 SetPixelRed(image,ScaleShortToQuantum((unsigned short) red),q);
1298 SetPixelGreen(image,ScaleShortToQuantum((unsigned short) green),q);
1299 SetPixelBlue(image,ScaleShortToQuantum((unsigned short) blue),q);
1300 SetPixelAlpha(image,OpaqueAlpha,q);
1301 if (image->alpha_trait == BlendPixelTrait)
1302 SetPixelAlpha(image,
1303 ScaleShortToQuantum((unsigned short) opacity),q);
1304 q+=GetPixelChannels(image);
1306 if (SyncAuthenticPixels(image,exception) == MagickFalse)
1308 offset=(MagickOffsetType) (image->rows-y-1);
1309 if (image->previous == (Image *) NULL)
1311 status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y,
1313 if (status == MagickFalse)
1321 if (mapped == MagickFalse)
1322 pixels=(unsigned char *) RelinquishMagickMemory(pixels);
1323 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
1326 if (mapped == MagickFalse)
1327 pixels=(unsigned char *) RelinquishMagickMemory(pixels);
1328 if (EOFBlob(image) != MagickFalse)
1330 ThrowFileException(exception,CorruptImageError,"UnexpectedEndOfFile",
1334 if (bmp_info.height < 0)
1340 Correct image orientation.
1342 flipped_image=FlipImage(image,exception);
1343 if (flipped_image != (Image *) NULL)
1345 DuplicateBlob(flipped_image,image);
1346 image=DestroyImage(image);
1347 image=flipped_image;
1351 Proceed to next image.
1353 if (image_info->number_scenes != 0)
1354 if (image->scene >= (image_info->scene+image_info->number_scenes-1))
1357 if (bmp_info.ba_offset != 0)
1359 offset=SeekBlob(image,(MagickOffsetType) bmp_info.ba_offset,SEEK_SET);
1361 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
1363 count=ReadBlob(image,2,magick);
1364 if ((count == 2) && (IsBMP(magick,2) != MagickFalse))
1367 Acquire next image structure.
1369 AcquireNextImage(image_info,image,exception);
1370 if (GetNextImageInList(image) == (Image *) NULL)
1372 image=DestroyImageList(image);
1373 return((Image *) NULL);
1375 image=SyncNextImageInList(image);
1376 status=SetImageProgress(image,LoadImagesTag,TellBlob(image),
1377 GetBlobSize(image));
1378 if (status == MagickFalse)
1381 } while (IsBMP(magick,2) != MagickFalse);
1382 (void) CloseBlob(image);
1383 return(GetFirstImageInList(image));
1387 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1391 % R e g i s t e r B M P I m a g e %
1395 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1397 % RegisterBMPImage() adds attributes for the BMP image format to
1398 % the list of supported formats. The attributes include the image format
1399 % tag, a method to read and/or write the format, whether the format
1400 % supports the saving of more than one frame to the same file or blob,
1401 % whether the format supports native in-memory I/O, and a brief
1402 % description of the format.
1404 % The format of the RegisterBMPImage method is:
1406 % size_t RegisterBMPImage(void)
1409 ModuleExport size_t RegisterBMPImage(void)
1414 entry=SetMagickInfo("BMP");
1415 entry->decoder=(DecodeImageHandler *) ReadBMPImage;
1416 entry->encoder=(EncodeImageHandler *) WriteBMPImage;
1417 entry->magick=(IsImageFormatHandler *) IsBMP;
1418 entry->description=ConstantString("Microsoft Windows bitmap image");
1419 entry->module=ConstantString("BMP");
1420 entry->adjoin=MagickFalse;
1421 entry->seekable_stream=MagickTrue;
1422 (void) RegisterMagickInfo(entry);
1423 entry=SetMagickInfo("BMP2");
1424 entry->encoder=(EncodeImageHandler *) WriteBMPImage;
1425 entry->magick=(IsImageFormatHandler *) IsBMP;
1426 entry->description=ConstantString("Microsoft Windows bitmap image (V2)");
1427 entry->module=ConstantString("BMP");
1428 entry->adjoin=MagickFalse;
1429 entry->seekable_stream=MagickTrue;
1430 (void) RegisterMagickInfo(entry);
1431 entry=SetMagickInfo("BMP3");
1432 entry->encoder=(EncodeImageHandler *) WriteBMPImage;
1433 entry->magick=(IsImageFormatHandler *) IsBMP;
1434 entry->description=ConstantString("Microsoft Windows bitmap image (V3)");
1435 entry->module=ConstantString("BMP");
1436 entry->adjoin=MagickFalse;
1437 entry->seekable_stream=MagickTrue;
1438 (void) RegisterMagickInfo(entry);
1439 return(MagickImageCoderSignature);
1443 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1447 % U n r e g i s t e r B M P I m a g e %
1451 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1453 % UnregisterBMPImage() removes format registrations made by the
1454 % BMP module from the list of supported formats.
1456 % The format of the UnregisterBMPImage method is:
1458 % UnregisterBMPImage(void)
1461 ModuleExport void UnregisterBMPImage(void)
1463 (void) UnregisterMagickInfo("BMP");
1464 (void) UnregisterMagickInfo("BMP2");
1465 (void) UnregisterMagickInfo("BMP3");
1469 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1473 % W r i t e B M P I m a g e %
1477 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1479 % WriteBMPImage() writes an image in Microsoft Windows bitmap encoded
1480 % image format, version 3 for Windows or (if the image has a matte channel)
1483 % The format of the WriteBMPImage method is:
1485 % MagickBooleanType WriteBMPImage(const ImageInfo *image_info,
1486 % Image *image,ExceptionInfo *exception)
1488 % A description of each parameter follows.
1490 % o image_info: the image info.
1492 % o image: The image.
1494 % o exception: return any errors or warnings in this structure.
1497 static MagickBooleanType WriteBMPImage(const ImageInfo *image_info,Image *image,
1498 ExceptionInfo *exception)
1513 register const Quantum
1520 register unsigned char
1535 Open output image file.
1537 assert(image_info != (const ImageInfo *) NULL);
1538 assert(image_info->signature == MagickSignature);
1539 assert(image != (Image *) NULL);
1540 assert(image->signature == MagickSignature);
1541 if (image->debug != MagickFalse)
1542 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1543 assert(exception != (ExceptionInfo *) NULL);
1544 assert(exception->signature == MagickSignature);
1545 status=OpenBlob(image_info,image,WriteBinaryBlobMode,exception);
1546 if (status == MagickFalse)
1549 if (LocaleCompare(image_info->magick,"BMP2") == 0)
1552 if (LocaleCompare(image_info->magick,"BMP3") == 0)
1558 Initialize BMP raster file header.
1560 if (IssRGBCompatibleColorspace(image->colorspace) == MagickFalse)
1561 (void) TransformImageColorspace(image,sRGBColorspace,exception);
1562 (void) ResetMagickMemory(&bmp_info,0,sizeof(bmp_info));
1563 bmp_info.file_size=14+12;
1565 bmp_info.file_size+=28;
1566 bmp_info.offset_bits=bmp_info.file_size;
1567 bmp_info.compression=BI_RGB;
1568 if ((image->storage_class == PseudoClass) && (image->colors > 256))
1569 (void) SetImageStorageClass(image,DirectClass,exception);
1570 if (image->storage_class != DirectClass)
1573 Colormapped BMP raster.
1575 bmp_info.bits_per_pixel=8;
1576 if (image->colors <= 2)
1577 bmp_info.bits_per_pixel=1;
1579 if (image->colors <= 16)
1580 bmp_info.bits_per_pixel=4;
1582 if (image->colors <= 256)
1583 bmp_info.bits_per_pixel=8;
1584 if (image_info->compression == RLECompression)
1585 bmp_info.bits_per_pixel=8;
1586 bmp_info.number_colors=1U << bmp_info.bits_per_pixel;
1587 if (image->alpha_trait == BlendPixelTrait)
1588 (void) SetImageStorageClass(image,DirectClass,exception);
1590 if ((size_t) bmp_info.number_colors < image->colors)
1591 (void) SetImageStorageClass(image,DirectClass,exception);
1594 bmp_info.file_size+=3*(1UL << bmp_info.bits_per_pixel);
1595 bmp_info.offset_bits+=3*(1UL << bmp_info.bits_per_pixel);
1598 bmp_info.file_size+=(1UL << bmp_info.bits_per_pixel);
1599 bmp_info.offset_bits+=(1UL << bmp_info.bits_per_pixel);
1603 if (image->storage_class == DirectClass)
1606 Full color BMP raster.
1608 bmp_info.number_colors=0;
1609 bmp_info.bits_per_pixel=(unsigned short)
1610 ((type > 3) && (image->alpha_trait == BlendPixelTrait) ? 32 : 24);
1611 bmp_info.compression=(unsigned int) ((type > 3) &&
1612 (image->alpha_trait == BlendPixelTrait) ? BI_BITFIELDS : BI_RGB);
1614 bytes_per_line=4*((image->columns*bmp_info.bits_per_pixel+31)/32);
1615 bmp_info.ba_offset=0;
1616 profile=GetImageProfile(image,"icc");
1617 have_color_info=(image->rendering_intent != UndefinedIntent) ||
1618 (profile != (StringInfo *) NULL) || (image->gamma != 0.0) ? MagickTrue :
1623 if ((type == 3) || ((image->alpha_trait != BlendPixelTrait) &&
1624 (have_color_info == MagickFalse)))
1636 if ((image->rendering_intent != UndefinedIntent) ||
1637 (profile != (StringInfo *) NULL))
1642 bmp_info.file_size+=extra_size;
1643 bmp_info.offset_bits+=extra_size;
1645 bmp_info.width=(int) image->columns;
1646 bmp_info.height=(int) image->rows;
1648 bmp_info.image_size=(unsigned int) (bytes_per_line*image->rows);
1649 bmp_info.file_size+=bmp_info.image_size;
1650 bmp_info.x_pixels=75*39;
1651 bmp_info.y_pixels=75*39;
1652 switch (image->units)
1654 case UndefinedResolution:
1655 case PixelsPerInchResolution:
1657 bmp_info.x_pixels=(unsigned int) (100.0*image->resolution.x/2.54);
1658 bmp_info.y_pixels=(unsigned int) (100.0*image->resolution.y/2.54);
1661 case PixelsPerCentimeterResolution:
1663 bmp_info.x_pixels=(unsigned int) (100.0*image->resolution.x);
1664 bmp_info.y_pixels=(unsigned int) (100.0*image->resolution.y);
1668 bmp_info.colors_important=bmp_info.number_colors;
1670 Convert MIFF to BMP raster pixels.
1672 pixels=(unsigned char *) AcquireQuantumMemory((size_t) bmp_info.image_size,
1674 if (pixels == (unsigned char *) NULL)
1675 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
1676 (void) ResetMagickMemory(pixels,0,(size_t) bmp_info.image_size);
1677 switch (bmp_info.bits_per_pixel)
1686 Convert PseudoClass image to a BMP monochrome image.
1688 for (y=0; y < (ssize_t) image->rows; y++)
1693 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
1694 if (p == (const Quantum *) NULL)
1696 q=pixels+(image->rows-y-1)*bytes_per_line;
1699 for (x=0; x < (ssize_t) image->columns; x++)
1702 byte|=GetPixelIndex(image,p) != 0 ? 0x01 : 0x00;
1706 *q++=(unsigned char) byte;
1710 p+=GetPixelChannels(image);
1714 *q++=(unsigned char) (byte << (8-bit));
1717 offset=(ssize_t) (image->columns+7)/8;
1718 for (x=offset; x < (ssize_t) bytes_per_line; x++)
1720 if (image->previous == (Image *) NULL)
1722 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
1724 if (status == MagickFalse)
1740 Convert PseudoClass image to a BMP monochrome image.
1742 for (y=0; y < (ssize_t) image->rows; y++)
1744 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
1745 if (p == (const Quantum *) NULL)
1747 q=pixels+(image->rows-y-1)*bytes_per_line;
1750 for (x=0; x < (ssize_t) image->columns; x++)
1753 byte|=((size_t) GetPixelIndex(image,p) & 0x0f);
1757 *q++=(unsigned char) byte;
1761 p+=GetPixelChannels(image);
1765 *q++=(unsigned char) (byte << 4);
1768 offset=(ssize_t) (image->columns+1)/2;
1769 for (x=offset; x < (ssize_t) bytes_per_line; x++)
1771 if (image->previous == (Image *) NULL)
1773 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
1775 if (status == MagickFalse)
1784 Convert PseudoClass packet to BMP pixel.
1786 for (y=0; y < (ssize_t) image->rows; y++)
1788 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
1789 if (p == (const Quantum *) NULL)
1791 q=pixels+(image->rows-y-1)*bytes_per_line;
1792 for (x=0; x < (ssize_t) image->columns; x++)
1794 *q++=(unsigned char) GetPixelIndex(image,p);
1795 p+=GetPixelChannels(image);
1797 for ( ; x < (ssize_t) bytes_per_line; x++)
1799 if (image->previous == (Image *) NULL)
1801 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
1803 if (status == MagickFalse)
1812 Convert DirectClass packet to BMP BGR888.
1814 for (y=0; y < (ssize_t) image->rows; y++)
1816 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
1817 if (p == (const Quantum *) NULL)
1819 q=pixels+(image->rows-y-1)*bytes_per_line;
1820 for (x=0; x < (ssize_t) image->columns; x++)
1822 *q++=ScaleQuantumToChar(GetPixelBlue(image,p));
1823 *q++=ScaleQuantumToChar(GetPixelGreen(image,p));
1824 *q++=ScaleQuantumToChar(GetPixelRed(image,p));
1825 p+=GetPixelChannels(image);
1827 for (x=3L*(ssize_t) image->columns; x < (ssize_t) bytes_per_line; x++)
1829 if (image->previous == (Image *) NULL)
1831 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
1833 if (status == MagickFalse)
1842 Convert DirectClass packet to ARGB8888 pixel.
1844 for (y=0; y < (ssize_t) image->rows; y++)
1846 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
1847 if (p == (const Quantum *) NULL)
1849 q=pixels+(image->rows-y-1)*bytes_per_line;
1850 for (x=0; x < (ssize_t) image->columns; x++)
1852 *q++=ScaleQuantumToChar(GetPixelBlue(image,p));
1853 *q++=ScaleQuantumToChar(GetPixelGreen(image,p));
1854 *q++=ScaleQuantumToChar(GetPixelRed(image,p));
1855 *q++=ScaleQuantumToChar(GetPixelAlpha(image,p));
1856 p+=GetPixelChannels(image);
1858 if (image->previous == (Image *) NULL)
1860 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
1862 if (status == MagickFalse)
1869 if ((type > 2) && (bmp_info.bits_per_pixel == 8))
1870 if (image_info->compression != NoCompression)
1876 Convert run-length encoded raster pixels.
1878 length=(size_t) (2*(bytes_per_line+2)*(image->rows+2)+2);
1879 bmp_data=(unsigned char *) NULL;
1880 if (~length >= bytes_per_line)
1881 bmp_data=(unsigned char *) AcquireQuantumMemory(length+
1882 bytes_per_line,sizeof(*bmp_data));
1883 if (bmp_data == (unsigned char *) NULL)
1885 pixels=(unsigned char *) RelinquishMagickMemory(pixels);
1886 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
1888 bmp_info.file_size-=bmp_info.image_size;
1889 bmp_info.image_size=(unsigned int) EncodeImage(image,bytes_per_line,
1891 bmp_info.file_size+=bmp_info.image_size;
1892 pixels=(unsigned char *) RelinquishMagickMemory(pixels);
1894 bmp_info.compression=BI_RLE8;
1897 Write BMP for Windows, all versions, 14-byte header.
1899 if (image->debug != MagickFalse)
1901 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1902 " Writing BMP version %.20g datastream",(double) type);
1903 if (image->storage_class == DirectClass)
1904 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1905 " Storage class=DirectClass");
1907 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1908 " Storage class=PseudoClass");
1909 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1910 " Image depth=%.20g",(double) image->depth);
1911 if (image->alpha_trait == BlendPixelTrait)
1912 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1915 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1916 " Matte=MagickFalse");
1917 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1918 " BMP bits_per_pixel=%.20g",(double) bmp_info.bits_per_pixel);
1919 switch ((int) bmp_info.compression)
1923 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1924 " Compression=BI_RGB");
1929 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1930 " Compression=BI_RLE8");
1935 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1936 " Compression=BI_BITFIELDS");
1941 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1942 " Compression=UNKNOWN (%u)",bmp_info.compression);
1946 if (bmp_info.number_colors == 0)
1947 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1948 " Number_colors=unspecified");
1950 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1951 " Number_colors=%u",bmp_info.number_colors);
1953 (void) WriteBlob(image,2,(unsigned char *) "BM");
1954 (void) WriteBlobLSBLong(image,bmp_info.file_size);
1955 (void) WriteBlobLSBLong(image,bmp_info.ba_offset); /* always 0 */
1956 (void) WriteBlobLSBLong(image,bmp_info.offset_bits);
1960 Write 12-byte version 2 bitmap header.
1962 (void) WriteBlobLSBLong(image,bmp_info.size);
1963 (void) WriteBlobLSBShort(image,(unsigned short) bmp_info.width);
1964 (void) WriteBlobLSBShort(image,(unsigned short) bmp_info.height);
1965 (void) WriteBlobLSBShort(image,bmp_info.planes);
1966 (void) WriteBlobLSBShort(image,bmp_info.bits_per_pixel);
1971 Write 40-byte version 3+ bitmap header.
1973 (void) WriteBlobLSBLong(image,bmp_info.size);
1974 (void) WriteBlobLSBLong(image,(unsigned int) bmp_info.width);
1975 (void) WriteBlobLSBLong(image,(unsigned int) bmp_info.height);
1976 (void) WriteBlobLSBShort(image,bmp_info.planes);
1977 (void) WriteBlobLSBShort(image,bmp_info.bits_per_pixel);
1978 (void) WriteBlobLSBLong(image,bmp_info.compression);
1979 (void) WriteBlobLSBLong(image,bmp_info.image_size);
1980 (void) WriteBlobLSBLong(image,bmp_info.x_pixels);
1981 (void) WriteBlobLSBLong(image,bmp_info.y_pixels);
1982 (void) WriteBlobLSBLong(image,bmp_info.number_colors);
1983 (void) WriteBlobLSBLong(image,bmp_info.colors_important);
1985 if ((type > 3) && ((image->alpha_trait == BlendPixelTrait) ||
1986 (have_color_info != MagickFalse)))
1989 Write the rest of the 108-byte BMP Version 4 header.
1991 (void) WriteBlobLSBLong(image,0x00ff0000U); /* Red mask */
1992 (void) WriteBlobLSBLong(image,0x0000ff00U); /* Green mask */
1993 (void) WriteBlobLSBLong(image,0x000000ffU); /* Blue mask */
1994 (void) WriteBlobLSBLong(image,0xff000000U); /* Alpha mask */
1995 (void) WriteBlobLSBLong(image,0x73524742U); /* sRGB */
1996 (void) WriteBlobLSBLong(image,(unsigned int)
1997 (image->chromaticity.red_primary.x*0x40000000));
1998 (void) WriteBlobLSBLong(image,(unsigned int)
1999 (image->chromaticity.red_primary.y*0x40000000));
2000 (void) WriteBlobLSBLong(image,(unsigned int)
2001 ((1.000f-(image->chromaticity.red_primary.x+
2002 image->chromaticity.red_primary.y))*0x40000000));
2003 (void) WriteBlobLSBLong(image,(unsigned int)
2004 (image->chromaticity.green_primary.x*0x40000000));
2005 (void) WriteBlobLSBLong(image,(unsigned int)
2006 (image->chromaticity.green_primary.y*0x40000000));
2007 (void) WriteBlobLSBLong(image,(unsigned int)
2008 ((1.000f-(image->chromaticity.green_primary.x+
2009 image->chromaticity.green_primary.y))*0x40000000));
2010 (void) WriteBlobLSBLong(image,(unsigned int)
2011 (image->chromaticity.blue_primary.x*0x40000000));
2012 (void) WriteBlobLSBLong(image,(unsigned int)
2013 (image->chromaticity.blue_primary.y*0x40000000));
2014 (void) WriteBlobLSBLong(image,(unsigned int)
2015 ((1.000f-(image->chromaticity.blue_primary.x+
2016 image->chromaticity.blue_primary.y))*0x40000000));
2017 (void) WriteBlobLSBLong(image,(unsigned int)
2018 (bmp_info.gamma_scale.x*0x10000));
2019 (void) WriteBlobLSBLong(image,(unsigned int)
2020 (bmp_info.gamma_scale.y*0x10000));
2021 (void) WriteBlobLSBLong(image,(unsigned int)
2022 (bmp_info.gamma_scale.z*0x10000));
2023 if ((image->rendering_intent != UndefinedIntent) ||
2024 (profile != (StringInfo *) NULL))
2029 switch ((int) image->rendering_intent)
2031 case SaturationIntent:
2033 intent=LCS_GM_BUSINESS;
2036 case RelativeIntent:
2038 intent=LCS_GM_GRAPHICS;
2041 case PerceptualIntent:
2043 intent=LCS_GM_IMAGES;
2046 case AbsoluteIntent:
2048 intent=LCS_GM_ABS_COLORIMETRIC;
2057 (void) WriteBlobLSBLong(image,(unsigned int) intent);
2058 (void) WriteBlobLSBLong(image,0x00); /* dummy profile data */
2059 (void) WriteBlobLSBLong(image,0x00); /* dummy profile length */
2060 (void) WriteBlobLSBLong(image,0x00); /* reserved */
2063 if (image->storage_class == PseudoClass)
2069 Dump colormap to file.
2071 if (image->debug != MagickFalse)
2072 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
2073 " Colormap: %.20g entries",(double) image->colors);
2074 bmp_colormap=(unsigned char *) AcquireQuantumMemory((size_t) (1UL <<
2075 bmp_info.bits_per_pixel),4*sizeof(*bmp_colormap));
2076 if (bmp_colormap == (unsigned char *) NULL)
2077 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
2079 for (i=0; i < (ssize_t) MagickMin((ssize_t) image->colors,(ssize_t) bmp_info.number_colors); i++)
2081 *q++=ScaleQuantumToChar(ClampToQuantum(image->colormap[i].blue));
2082 *q++=ScaleQuantumToChar(ClampToQuantum(image->colormap[i].green));
2083 *q++=ScaleQuantumToChar(ClampToQuantum(image->colormap[i].red));
2085 *q++=(unsigned char) 0x0;
2087 for ( ; i < (ssize_t) (1UL << bmp_info.bits_per_pixel); i++)
2089 *q++=(unsigned char) 0x00;
2090 *q++=(unsigned char) 0x00;
2091 *q++=(unsigned char) 0x00;
2093 *q++=(unsigned char) 0x00;
2096 (void) WriteBlob(image,(size_t) (3*(1L << bmp_info.bits_per_pixel)),
2099 (void) WriteBlob(image,(size_t) (4*(1L << bmp_info.bits_per_pixel)),
2101 bmp_colormap=(unsigned char *) RelinquishMagickMemory(bmp_colormap);
2103 if (image->debug != MagickFalse)
2104 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
2105 " Pixels: %u bytes",bmp_info.image_size);
2106 (void) WriteBlob(image,(size_t) bmp_info.image_size,pixels);
2107 pixels=(unsigned char *) RelinquishMagickMemory(pixels);
2108 if (GetNextImageInList(image) == (Image *) NULL)
2110 image=SyncNextImageInList(image);
2111 status=SetImageProgress(image,SaveImagesTag,scene++,
2112 GetImageListLength(image));
2113 if (status == MagickFalse)
2115 } while (image_info->adjoin != MagickFalse);
2116 (void) CloseBlob(image);