2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
13 % Read/Write Microsoft Windows Bitmap Image Format %
17 % Glenn Randers-Pehrson %
21 % Copyright 1999-2014 ImageMagick Studio LLC, a non-profit organization %
22 % dedicated to making software imaging solutions freely available. %
24 % You may not use this file except in compliance with the License. You may %
25 % obtain a copy of the License at %
27 % http://www.imagemagick.org/script/license.php %
29 % Unless required by applicable law or agreed to in writing, software %
30 % distributed under the License is distributed on an "AS IS" BASIS, %
31 % WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. %
32 % See the License for the specific language governing permissions and %
33 % limitations under the License. %
35 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
43 #include "MagickCore/studio.h"
44 #include "MagickCore/blob.h"
45 #include "MagickCore/blob-private.h"
46 #include "MagickCore/cache.h"
47 #include "MagickCore/colormap-private.h"
48 #include "MagickCore/color-private.h"
49 #include "MagickCore/colormap.h"
50 #include "MagickCore/colorspace.h"
51 #include "MagickCore/colorspace-private.h"
52 #include "MagickCore/exception.h"
53 #include "MagickCore/exception-private.h"
54 #include "MagickCore/image.h"
55 #include "MagickCore/image-private.h"
56 #include "MagickCore/list.h"
57 #include "MagickCore/log.h"
58 #include "MagickCore/magick.h"
59 #include "MagickCore/memory_.h"
60 #include "MagickCore/monitor.h"
61 #include "MagickCore/monitor-private.h"
62 #include "MagickCore/option.h"
63 #include "MagickCore/pixel-accessor.h"
64 #include "MagickCore/profile.h"
65 #include "MagickCore/quantum-private.h"
66 #include "MagickCore/static.h"
67 #include "MagickCore/string_.h"
68 #include "MagickCore/module.h"
69 #include "MagickCore/transform.h"
72 Macro definitions (from Windows wingdi.h).
78 #if !defined(MAGICKCORE_WINDOWS_SUPPORT) || defined(__MINGW32__) || defined(__MINGW64__)
86 #define BI_BITFIELDS 3
88 #undef LCS_CALIBRATED_RBG
89 #define LCS_CALIBRATED_RBG 0
92 #undef LCS_WINDOWS_COLOR_SPACE
93 #define LCS_WINDOWS_COLOR_SPACE 2
95 #define PROFILE_LINKED 3
96 #undef PROFILE_EMBEDDED
97 #define PROFILE_EMBEDDED 4
99 #undef LCS_GM_BUSINESS
100 #define LCS_GM_BUSINESS 1 /* Saturation */
101 #undef LCS_GM_GRAPHICS
102 #define LCS_GM_GRAPHICS 2 /* Relative */
104 #define LCS_GM_IMAGES 4 /* Perceptual */
105 #undef LCS_GM_ABS_COLORIMETRIC
106 #define LCS_GM_ABS_COLORIMETRIC 8 /* Absolute */
110 Typedef declarations.
112 typedef struct _BMPInfo
151 Forward declarations.
153 static MagickBooleanType
154 WriteBMPImage(const ImageInfo *,Image *,ExceptionInfo *);
157 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
161 % D e c o d e I m a g e %
165 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
167 % DecodeImage unpacks the packed image pixels into runlength-encoded
170 % The format of the DecodeImage method is:
172 % MagickBooleanType DecodeImage(Image *image,
173 % const size_t compression,unsigned char *pixels)
175 % A description of each parameter follows:
177 % o image: the address of a structure of type Image.
179 % o compression: Zero means uncompressed. A value of 1 means the
180 % compressed pixels are runlength encoded for a 256-color bitmap.
181 % A value of 2 means a 16-color bitmap. A value of 3 means bitfields
184 % o pixels: The address of a byte (8 bits) array of pixel data created by
185 % the decoding process.
189 static inline ssize_t MagickAbsoluteValue(const ssize_t x)
196 static inline size_t MagickMax(const size_t x,const size_t y)
203 static inline ssize_t MagickMin(const ssize_t x,const ssize_t y)
210 static MagickBooleanType DecodeImage(Image *image,const size_t compression,
211 unsigned char *pixels)
220 register unsigned char
230 assert(image != (Image *) NULL);
231 assert(image->signature == MagickSignature);
232 if (image->debug != MagickFalse)
233 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
234 assert(pixels != (unsigned char *) NULL);
235 (void) ResetMagickMemory(pixels,0,(size_t) image->columns*image->rows*
240 q=pixels+(size_t) image->columns*image->rows;
241 for (y=0; y < (ssize_t) image->rows; )
243 if ((p < pixels) || (p >= q))
245 count=ReadBlobByte(image);
253 count=(int) MagickMin((ssize_t) count,(ssize_t) (q-p));
254 byte=(unsigned char) ReadBlobByte(image);
255 if (compression == BI_RLE8)
257 for (i=0; i < (ssize_t) count; i++)
258 *p++=(unsigned char) byte;
262 for (i=0; i < (ssize_t) count; i++)
264 ((i & 0x01) != 0 ? (byte & 0x0f) : ((byte >> 4) & 0x0f));
273 count=ReadBlobByte(image);
285 p=pixels+y*image->columns;
293 x+=ReadBlobByte(image);
294 y+=ReadBlobByte(image);
295 p=pixels+y*image->columns+x;
303 count=(int) MagickMin((ssize_t) count,(ssize_t) (q-p));
304 if (compression == BI_RLE8)
305 for (i=0; i < (ssize_t) count; i++)
306 *p++=(unsigned char) ReadBlobByte(image);
308 for (i=0; i < (ssize_t) count; i++)
311 byte=(unsigned char) ReadBlobByte(image);
313 ((i & 0x01) != 0 ? (byte & 0x0f) : ((byte >> 4) & 0x0f));
319 if (compression == BI_RLE8)
321 if ((count & 0x01) != 0)
322 (void) ReadBlobByte(image);
325 if (((count & 0x03) == 1) || ((count & 0x03) == 2))
326 (void) ReadBlobByte(image);
331 if (SetImageProgress(image,LoadImageTag,(MagickOffsetType) y,image->rows) == MagickFalse)
334 (void) ReadBlobByte(image); /* end of line */
335 (void) ReadBlobByte(image);
340 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
344 % E n c o d e I m a g e %
348 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
350 % EncodeImage compresses pixels using a runlength encoded format.
352 % The format of the EncodeImage method is:
354 % static MagickBooleanType EncodeImage(Image *image,
355 % const size_t bytes_per_line,const unsigned char *pixels,
356 % unsigned char *compressed_pixels)
358 % A description of each parameter follows:
360 % o image: The image.
362 % o bytes_per_line: the number of bytes in a scanline of compressed pixels
364 % o pixels: The address of a byte (8 bits) array of pixel data created by
365 % the compression process.
367 % o compressed_pixels: The address of a byte (8 bits) array of compressed
371 static size_t EncodeImage(Image *image,const size_t bytes_per_line,
372 const unsigned char *pixels,unsigned char *compressed_pixels)
377 register const unsigned char
384 register unsigned char
391 Runlength encode pixels.
393 assert(image != (Image *) NULL);
394 assert(image->signature == MagickSignature);
395 if (image->debug != MagickFalse)
396 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
397 assert(pixels != (const unsigned char *) NULL);
398 assert(compressed_pixels != (unsigned char *) NULL);
402 for (y=0; y < (ssize_t) image->rows; y++)
404 for (x=0; x < (ssize_t) bytes_per_line; x+=i)
409 for (i=1; ((x+i) < (ssize_t) bytes_per_line); i++)
410 if ((i == 255) || (*(p+i) != *p))
412 *q++=(unsigned char) i;
419 *q++=(unsigned char) 0x00;
420 *q++=(unsigned char) 0x00;
421 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
423 if (status == MagickFalse)
429 *q++=(unsigned char) 0x00;
430 *q++=(unsigned char) 0x01;
431 return((size_t) (q-compressed_pixels));
435 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
443 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
445 % IsBMP() returns MagickTrue if the image format type, identified by the
446 % magick string, is BMP.
448 % The format of the IsBMP method is:
450 % MagickBooleanType IsBMP(const unsigned char *magick,const size_t length)
452 % A description of each parameter follows:
454 % o magick: compare image format pattern against these bytes.
456 % o length: Specifies the length of the magick string.
459 static MagickBooleanType IsBMP(const unsigned char *magick,const size_t length)
463 if ((LocaleNCompare((char *) magick,"BA",2) == 0) ||
464 (LocaleNCompare((char *) magick,"BM",2) == 0) ||
465 (LocaleNCompare((char *) magick,"IC",2) == 0) ||
466 (LocaleNCompare((char *) magick,"PI",2) == 0) ||
467 (LocaleNCompare((char *) magick,"CI",2) == 0) ||
468 (LocaleNCompare((char *) magick,"CP",2) == 0))
474 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
478 % R e a d B M P I m a g e %
482 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
484 % ReadBMPImage() reads a Microsoft Windows bitmap image file, Version
485 % 2, 3 (for Windows or NT), or 4, and returns it. It allocates the memory
486 % necessary for the new Image structure and returns a pointer to the new
489 % The format of the ReadBMPImage method is:
491 % image=ReadBMPImage(image_info)
493 % A description of each parameter follows:
495 % o image_info: the image info.
497 % o exception: return any errors or warnings in this structure.
501 static Image *ReadBMPImage(const ImageInfo *image_info,ExceptionInfo *exception)
529 register unsigned char
552 assert(image_info != (const ImageInfo *) NULL);
553 assert(image_info->signature == MagickSignature);
554 if (image_info->debug != MagickFalse)
555 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
556 image_info->filename);
557 assert(exception != (ExceptionInfo *) NULL);
558 assert(exception->signature == MagickSignature);
559 image=AcquireImage(image_info,exception);
560 status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception);
561 if (status == MagickFalse)
563 image=DestroyImageList(image);
564 return((Image *) NULL);
567 Determine if this a BMP file.
569 (void) ResetMagickMemory(&bmp_info,0,sizeof(bmp_info));
570 bmp_info.ba_offset=0;
572 count=ReadBlob(image,2,magick);
586 Verify BMP identifier.
588 if (bmp_info.ba_offset == 0)
589 start_position=TellBlob(image)-2;
590 bmp_info.ba_offset=0;
591 while (LocaleNCompare((char *) magick,"BA",2) == 0)
593 bmp_info.file_size=ReadBlobLSBLong(image);
594 bmp_info.ba_offset=ReadBlobLSBLong(image);
595 bmp_info.offset_bits=ReadBlobLSBLong(image);
596 count=ReadBlob(image,2,magick);
600 if (image->debug != MagickFalse)
601 (void) LogMagickEvent(CoderEvent,GetMagickModule()," Magick: %c%c",
602 magick[0],magick[1]);
603 if ((count == 0) || ((LocaleNCompare((char *) magick,"BM",2) != 0) &&
604 (LocaleNCompare((char *) magick,"CI",2) != 0)))
605 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
606 bmp_info.file_size=ReadBlobLSBLong(image);
607 (void) ReadBlobLSBLong(image);
608 bmp_info.offset_bits=ReadBlobLSBLong(image);
609 bmp_info.size=ReadBlobLSBLong(image);
610 if (image->debug != MagickFalse)
611 (void) LogMagickEvent(CoderEvent,GetMagickModule()," BMP size: %u",
613 if (bmp_info.size == 12)
618 (void) CopyMagickString(image->magick,"BMP2",MaxTextExtent);
619 bmp_info.width=(ssize_t) ((short) ReadBlobLSBShort(image));
620 bmp_info.height=(ssize_t) ((short) ReadBlobLSBShort(image));
621 bmp_info.planes=ReadBlobLSBShort(image);
622 bmp_info.bits_per_pixel=ReadBlobLSBShort(image);
625 bmp_info.number_colors=0;
626 bmp_info.compression=BI_RGB;
627 bmp_info.image_size=0;
628 bmp_info.alpha_mask=0;
629 if (image->debug != MagickFalse)
631 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
632 " Format: OS/2 Bitmap");
633 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
634 " Geometry: %.20gx%.20g",(double) bmp_info.width,(double)
641 Microsoft Windows BMP image file.
643 if (bmp_info.size < 40)
644 ThrowReaderException(CorruptImageError,"NonOS2HeaderSizeError");
645 bmp_info.width=(ssize_t) ((int) ReadBlobLSBLong(image));
646 bmp_info.height=(ssize_t) ((int) ReadBlobLSBLong(image));
647 bmp_info.planes=ReadBlobLSBShort(image);
648 bmp_info.bits_per_pixel=ReadBlobLSBShort(image);
649 bmp_info.compression=ReadBlobLSBLong(image);
650 bmp_info.image_size=ReadBlobLSBLong(image);
651 bmp_info.x_pixels=ReadBlobLSBLong(image);
652 bmp_info.y_pixels=ReadBlobLSBLong(image);
653 bmp_info.number_colors=ReadBlobLSBLong(image);
654 bmp_info.colors_important=ReadBlobLSBLong(image);
657 if (image->debug != MagickFalse)
659 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
660 " Format: MS Windows bitmap");
661 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
662 " Geometry: %.20gx%.20g",(double) bmp_info.width,(double)
664 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
665 " Bits per pixel: %.20g",(double) bmp_info.bits_per_pixel);
666 switch ((int) bmp_info.compression)
670 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
671 " Compression: BI_RGB");
676 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
677 " Compression: BI_RLE4");
682 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
683 " Compression: BI_RLE8");
688 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
689 " Compression: BI_BITFIELDS");
694 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
695 " Compression: BI_PNG");
700 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
701 " Compression: BI_JPEG");
706 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
707 " Compression: UNKNOWN (%u)",bmp_info.compression);
710 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
711 " Number of colors: %u",bmp_info.number_colors);
713 bmp_info.red_mask=ReadBlobLSBLong(image);
714 bmp_info.green_mask=ReadBlobLSBLong(image);
715 bmp_info.blue_mask=ReadBlobLSBLong(image);
716 if (bmp_info.size > 40)
723 Read color management information.
725 bmp_info.alpha_mask=ReadBlobLSBLong(image);
726 bmp_info.colorspace=(int) ReadBlobLSBLong(image);
728 Decode 2^30 fixed point formatted CIE primaries.
730 # define BMP_DENOM ((double) 0x40000000)
731 bmp_info.red_primary.x=(double) ReadBlobLSBLong(image)/BMP_DENOM;
732 bmp_info.red_primary.y=(double) ReadBlobLSBLong(image)/BMP_DENOM;
733 bmp_info.red_primary.z=(double) ReadBlobLSBLong(image)/BMP_DENOM;
734 bmp_info.green_primary.x=(double) ReadBlobLSBLong(image)/BMP_DENOM;
735 bmp_info.green_primary.y=(double) ReadBlobLSBLong(image)/BMP_DENOM;
736 bmp_info.green_primary.z=(double) ReadBlobLSBLong(image)/BMP_DENOM;
737 bmp_info.blue_primary.x=(double) ReadBlobLSBLong(image)/BMP_DENOM;
738 bmp_info.blue_primary.y=(double) ReadBlobLSBLong(image)/BMP_DENOM;
739 bmp_info.blue_primary.z=(double) ReadBlobLSBLong(image)/BMP_DENOM;
741 sum=bmp_info.red_primary.x+bmp_info.red_primary.y+
742 bmp_info.red_primary.z;
743 bmp_info.red_primary.x/=sum;
744 bmp_info.red_primary.y/=sum;
745 image->chromaticity.red_primary.x=bmp_info.red_primary.x;
746 image->chromaticity.red_primary.y=bmp_info.red_primary.y;
748 sum=bmp_info.green_primary.x+bmp_info.green_primary.y+
749 bmp_info.green_primary.z;
750 bmp_info.green_primary.x/=sum;
751 bmp_info.green_primary.y/=sum;
752 image->chromaticity.green_primary.x=bmp_info.green_primary.x;
753 image->chromaticity.green_primary.y=bmp_info.green_primary.y;
755 sum=bmp_info.blue_primary.x+bmp_info.blue_primary.y+
756 bmp_info.blue_primary.z;
757 bmp_info.blue_primary.x/=sum;
758 bmp_info.blue_primary.y/=sum;
759 image->chromaticity.blue_primary.x=bmp_info.blue_primary.x;
760 image->chromaticity.blue_primary.y=bmp_info.blue_primary.y;
763 Decode 16^16 fixed point formatted gamma_scales.
765 bmp_info.gamma_scale.x=(double) ReadBlobLSBLong(image)/0x10000;
766 bmp_info.gamma_scale.y=(double) ReadBlobLSBLong(image)/0x10000;
767 bmp_info.gamma_scale.z=(double) ReadBlobLSBLong(image)/0x10000;
769 Compute a single gamma from the BMP 3-channel gamma.
771 image->gamma=(bmp_info.gamma_scale.x+bmp_info.gamma_scale.y+
772 bmp_info.gamma_scale.z)/3.0;
775 (void) CopyMagickString(image->magick,"BMP3",MaxTextExtent);
777 if (bmp_info.size > 108)
783 Read BMP Version 5 color management information.
785 intent=ReadBlobLSBLong(image);
786 switch ((int) intent)
788 case LCS_GM_BUSINESS:
790 image->rendering_intent=SaturationIntent;
793 case LCS_GM_GRAPHICS:
795 image->rendering_intent=RelativeIntent;
800 image->rendering_intent=PerceptualIntent;
803 case LCS_GM_ABS_COLORIMETRIC:
805 image->rendering_intent=AbsoluteIntent;
809 profile_data=ReadBlobLSBLong(image);
810 profile_size=ReadBlobLSBLong(image);
813 (void) ReadBlobLSBLong(image); /* Reserved byte */
816 if ((MagickSizeType) bmp_info.file_size > GetBlobSize(image))
817 (void) ThrowMagickException(exception,GetMagickModule(),CorruptImageError,
818 "LengthAndFilesizeDoNotMatch","`%s'",image->filename);
820 if ((MagickSizeType) bmp_info.file_size < GetBlobSize(image))
821 (void) ThrowMagickException(exception,GetMagickModule(),
822 CorruptImageWarning,"LengthAndFilesizeDoNotMatch","`%s'",
824 if (bmp_info.width <= 0)
825 ThrowReaderException(CorruptImageError,"NegativeOrZeroImageSize");
826 if (bmp_info.height == 0)
827 ThrowReaderException(CorruptImageError,"NegativeOrZeroImageSize");
828 if (bmp_info.planes != 1)
829 ThrowReaderException(CorruptImageError,"StaticPlanesValueNotEqualToOne");
830 if ((bmp_info.bits_per_pixel != 1) && (bmp_info.bits_per_pixel != 4) &&
831 (bmp_info.bits_per_pixel != 8) && (bmp_info.bits_per_pixel != 16) &&
832 (bmp_info.bits_per_pixel != 24) && (bmp_info.bits_per_pixel != 32))
833 ThrowReaderException(CorruptImageError,"UnrecognizedBitsPerPixel");
834 if (bmp_info.bits_per_pixel < 16 &&
835 bmp_info.number_colors > (1U << bmp_info.bits_per_pixel))
837 ThrowReaderException(CorruptImageError,
838 "UnrecognizedNumberOfColors");
840 if ((bmp_info.compression == 1) && (bmp_info.bits_per_pixel != 8))
841 ThrowReaderException(CorruptImageError,"UnrecognizedBitsPerPixel");
842 if ((bmp_info.compression == 2) && (bmp_info.bits_per_pixel != 4))
843 ThrowReaderException(CorruptImageError,"UnrecognizedBitsPerPixel");
844 if ((bmp_info.compression == 3) && (bmp_info.bits_per_pixel < 16))
845 ThrowReaderException(CorruptImageError,"UnrecognizedBitsPerPixel");
846 switch (bmp_info.compression)
854 ThrowReaderException(CoderError,"JPEGCompressNotSupported");
856 ThrowReaderException(CoderError,"PNGCompressNotSupported");
858 ThrowReaderException(CorruptImageError,"UnrecognizedImageCompression");
860 image->columns=(size_t) MagickAbsoluteValue(bmp_info.width);
861 image->rows=(size_t) MagickAbsoluteValue(bmp_info.height);
862 image->depth=bmp_info.bits_per_pixel <= 8 ? bmp_info.bits_per_pixel : 8;
863 image->alpha_trait=((bmp_info.alpha_mask != 0) &&
864 (bmp_info.compression == BI_BITFIELDS)) ||
865 (bmp_info.bits_per_pixel == 32) ? BlendPixelTrait : UndefinedPixelTrait;
866 if (bmp_info.bits_per_pixel < 16)
871 image->storage_class=PseudoClass;
872 image->colors=bmp_info.number_colors;
874 if (image->colors == 0)
875 image->colors=one << bmp_info.bits_per_pixel;
877 if (image->storage_class == PseudoClass)
886 Read BMP raster colormap.
888 if (image->debug != MagickFalse)
889 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
890 " Reading colormap of %.20g colors",(double) image->colors);
891 if (AcquireImageColormap(image,image->colors,exception) == MagickFalse)
892 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
893 bmp_colormap=(unsigned char *) AcquireQuantumMemory((size_t)
894 image->colors,4*sizeof(*bmp_colormap));
895 if (bmp_colormap == (unsigned char *) NULL)
896 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
897 if ((bmp_info.size == 12) || (bmp_info.size == 64))
901 offset=SeekBlob(image,start_position+14+bmp_info.size,SEEK_SET);
903 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
904 count=ReadBlob(image,packet_size*image->colors,bmp_colormap);
905 if (count != (ssize_t) (packet_size*image->colors))
906 ThrowReaderException(CorruptImageError,"InsufficientImageDataInFile");
908 for (i=0; i < (ssize_t) image->colors; i++)
910 image->colormap[i].blue=(MagickRealType) ScaleCharToQuantum(*p++);
911 image->colormap[i].green=(MagickRealType) ScaleCharToQuantum(*p++);
912 image->colormap[i].red=(MagickRealType) ScaleCharToQuantum(*p++);
913 if (packet_size == 4)
916 bmp_colormap=(unsigned char *) RelinquishMagickMemory(bmp_colormap);
918 image->resolution.x=(double) bmp_info.x_pixels/100.0;
919 image->resolution.y=(double) bmp_info.y_pixels/100.0;
920 image->units=PixelsPerCentimeterResolution;
921 if ((image_info->ping != MagickFalse) && (image_info->number_scenes != 0))
922 if (image->scene >= (image_info->scene+image_info->number_scenes-1))
927 offset=SeekBlob(image,start_position+bmp_info.offset_bits,SEEK_SET);
929 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
930 if (bmp_info.compression == BI_RLE4)
931 bmp_info.bits_per_pixel<<=1;
932 bytes_per_line=4*((image->columns*bmp_info.bits_per_pixel+31)/32);
933 length=(size_t) bytes_per_line*image->rows;
934 pixel_info=AcquireVirtualMemory((size_t) image->rows,
935 MagickMax(bytes_per_line,image->columns+256UL)*sizeof(*pixels));
936 if (pixel_info == (MemoryInfo *) NULL)
937 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
938 pixels=(unsigned char *) GetVirtualMemoryBlob(pixel_info);
939 if ((bmp_info.compression == BI_RGB) ||
940 (bmp_info.compression == BI_BITFIELDS))
942 if (image->debug != MagickFalse)
943 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
944 " Reading pixels (%.20g bytes)",(double) length);
945 count=ReadBlob(image,length,pixels);
946 if (count != (ssize_t) length)
948 pixel_info=RelinquishVirtualMemory(pixel_info);
949 ThrowReaderException(CorruptImageError,
950 "InsufficientImageDataInFile");
956 Convert run-length encoded raster pixels.
958 status=DecodeImage(image,bmp_info.compression,pixels);
959 if (status == MagickFalse)
961 pixel_info=RelinquishVirtualMemory(pixel_info);
962 ThrowReaderException(CorruptImageError,
963 "UnableToRunlengthDecodeImage");
967 Convert BMP raster image to pixel packets.
969 if (bmp_info.compression == BI_RGB)
971 bmp_info.alpha_mask=image->alpha_trait != UndefinedPixelTrait ?
973 bmp_info.red_mask=0x00ff0000U;
974 bmp_info.green_mask=0x0000ff00U;
975 bmp_info.blue_mask=0x000000ffU;
976 if (bmp_info.bits_per_pixel == 16)
981 bmp_info.red_mask=0x00007c00U;
982 bmp_info.green_mask=0x000003e0U;
983 bmp_info.blue_mask=0x0000001fU;
986 if ((bmp_info.bits_per_pixel == 16) || (bmp_info.bits_per_pixel == 32))
992 Get shift and quantum bits info from bitfield masks.
994 (void) ResetMagickMemory(&shift,0,sizeof(shift));
995 (void) ResetMagickMemory(&quantum_bits,0,sizeof(quantum_bits));
996 if (bmp_info.red_mask != 0)
997 while (((bmp_info.red_mask << shift.red) & 0x80000000UL) == 0)
999 if (bmp_info.green_mask != 0)
1000 while (((bmp_info.green_mask << shift.green) & 0x80000000UL) == 0)
1002 if (bmp_info.blue_mask != 0)
1003 while (((bmp_info.blue_mask << shift.blue) & 0x80000000UL) == 0)
1005 if (bmp_info.alpha_mask != 0)
1006 while (((bmp_info.alpha_mask << shift.alpha) & 0x80000000UL) == 0)
1009 while (((bmp_info.red_mask << sample) & 0x80000000UL) != 0)
1011 quantum_bits.red=(MagickRealType) (sample-shift.red);
1013 while (((bmp_info.green_mask << sample) & 0x80000000UL) != 0)
1015 quantum_bits.green=(MagickRealType) (sample-shift.green);
1017 while (((bmp_info.blue_mask << sample) & 0x80000000UL) != 0)
1019 quantum_bits.blue=(MagickRealType) (sample-shift.blue);
1021 while (((bmp_info.alpha_mask << sample) & 0x80000000UL) != 0)
1023 quantum_bits.alpha=(MagickRealType) (sample-shift.alpha);
1025 switch (bmp_info.bits_per_pixel)
1030 Convert bitmap scanline.
1032 for (y=(ssize_t) image->rows-1; y >= 0; y--)
1034 p=pixels+(image->rows-y-1)*bytes_per_line;
1035 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
1036 if (q == (Quantum *) NULL)
1038 for (x=0; x < ((ssize_t) image->columns-7); x+=8)
1040 for (bit=0; bit < 8; bit++)
1042 index=(Quantum) (((*p) & (0x80 >> bit)) != 0 ? 0x01 : 0x00);
1043 SetPixelIndex(image,index,q);
1044 q+=GetPixelChannels(image);
1048 if ((image->columns % 8) != 0)
1050 for (bit=0; bit < (image->columns % 8); bit++)
1052 index=(Quantum) (((*p) & (0x80 >> bit)) != 0 ? 0x01 : 0x00);
1053 SetPixelIndex(image,index,q);
1054 q+=GetPixelChannels(image);
1058 if (SyncAuthenticPixels(image,exception) == MagickFalse)
1060 if (image->previous == (Image *) NULL)
1062 status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y,
1064 if (status == MagickFalse)
1068 (void) SyncImage(image,exception);
1074 Convert PseudoColor scanline.
1076 for (y=(ssize_t) image->rows-1; y >= 0; y--)
1078 p=pixels+(image->rows-y-1)*bytes_per_line;
1079 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
1080 if (q == (Quantum *) NULL)
1082 for (x=0; x < ((ssize_t) image->columns-1); x+=2)
1084 index=ConstrainColormapIndex(image,(*p >> 4) & 0x0f,exception);
1085 SetPixelIndex(image,index,q);
1086 q+=GetPixelChannels(image);
1087 index=ConstrainColormapIndex(image,*p & 0x0f,exception);
1088 SetPixelIndex(image,index,q);
1089 q+=GetPixelChannels(image);
1092 if ((image->columns % 2) != 0)
1094 index=ConstrainColormapIndex(image,(*p >> 4) & 0xf,exception);
1095 SetPixelIndex(image,index,q);
1096 q+=GetPixelChannels(image);
1099 if (SyncAuthenticPixels(image,exception) == MagickFalse)
1101 if (image->previous == (Image *) NULL)
1103 status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y,
1105 if (status == MagickFalse)
1109 (void) SyncImage(image,exception);
1115 Convert PseudoColor scanline.
1117 if ((bmp_info.compression == BI_RLE8) ||
1118 (bmp_info.compression == BI_RLE4))
1119 bytes_per_line=image->columns;
1120 for (y=(ssize_t) image->rows-1; y >= 0; y--)
1122 p=pixels+(image->rows-y-1)*bytes_per_line;
1123 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
1124 if (q == (Quantum *) NULL)
1126 for (x=(ssize_t) image->columns; x != 0; --x)
1128 index=ConstrainColormapIndex(image,*p++,exception);
1129 SetPixelIndex(image,index,q);
1130 q+=GetPixelChannels(image);
1132 if (SyncAuthenticPixels(image,exception) == MagickFalse)
1134 offset=(MagickOffsetType) (image->rows-y-1);
1135 if (image->previous == (Image *) NULL)
1137 status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y,
1139 if (status == MagickFalse)
1143 (void) SyncImage(image,exception);
1152 Convert bitfield encoded 16-bit PseudoColor scanline.
1154 if (bmp_info.compression != BI_RGB &&
1155 bmp_info.compression != BI_BITFIELDS)
1157 pixel_info=RelinquishVirtualMemory(pixel_info);
1158 ThrowReaderException(CorruptImageError,
1159 "UnrecognizedImageCompression");
1161 bytes_per_line=2*(image->columns+image->columns % 2);
1162 image->storage_class=DirectClass;
1163 for (y=(ssize_t) image->rows-1; y >= 0; y--)
1165 p=pixels+(image->rows-y-1)*bytes_per_line;
1166 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
1167 if (q == (Quantum *) NULL)
1169 for (x=0; x < (ssize_t) image->columns; x++)
1171 pixel=(size_t) (*p++);
1173 red=((pixel & bmp_info.red_mask) << shift.red) >> 16;
1174 if (quantum_bits.red == 5)
1175 red|=((red & 0xe000) >> 5);
1176 if (quantum_bits.red <= 8)
1177 red|=((red & 0xff00) >> 8);
1178 green=((pixel & bmp_info.green_mask) << shift.green) >> 16;
1179 if (quantum_bits.green == 5)
1180 green|=((green & 0xe000) >> 5);
1181 if (quantum_bits.green == 6)
1182 green|=((green & 0xc000) >> 6);
1183 if (quantum_bits.green <= 8)
1184 green|=((green & 0xff00) >> 8);
1185 blue=((pixel & bmp_info.blue_mask) << shift.blue) >> 16;
1186 if (quantum_bits.blue == 5)
1187 blue|=((blue & 0xe000) >> 5);
1188 if (quantum_bits.blue <= 8)
1189 blue|=((blue & 0xff00) >> 8);
1190 opacity=((pixel & bmp_info.alpha_mask) << shift.alpha) >> 16;
1191 if (quantum_bits.alpha <= 8)
1192 opacity|=((opacity & 0xff00) >> 8);
1193 SetPixelRed(image,ScaleShortToQuantum((unsigned short) red),q);
1194 SetPixelGreen(image,ScaleShortToQuantum((unsigned short) green),q);
1195 SetPixelBlue(image,ScaleShortToQuantum((unsigned short) blue),q);
1196 SetPixelAlpha(image,OpaqueAlpha,q);
1197 if (image->alpha_trait != UndefinedPixelTrait)
1198 SetPixelAlpha(image,ScaleShortToQuantum((unsigned short)
1200 q+=GetPixelChannels(image);
1202 if (SyncAuthenticPixels(image,exception) == MagickFalse)
1204 offset=(MagickOffsetType) (image->rows-y-1);
1205 if (image->previous == (Image *) NULL)
1207 status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y,
1209 if (status == MagickFalse)
1218 Convert DirectColor scanline.
1220 bytes_per_line=4*((image->columns*24+31)/32);
1221 for (y=(ssize_t) image->rows-1; y >= 0; y--)
1223 p=pixels+(image->rows-y-1)*bytes_per_line;
1224 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
1225 if (q == (Quantum *) NULL)
1227 for (x=0; x < (ssize_t) image->columns; x++)
1229 SetPixelBlue(image,ScaleCharToQuantum(*p++),q);
1230 SetPixelGreen(image,ScaleCharToQuantum(*p++),q);
1231 SetPixelRed(image,ScaleCharToQuantum(*p++),q);
1232 SetPixelAlpha(image,OpaqueAlpha,q);
1233 q+=GetPixelChannels(image);
1235 if (SyncAuthenticPixels(image,exception) == MagickFalse)
1237 offset=(MagickOffsetType) (image->rows-y-1);
1238 if (image->previous == (Image *) NULL)
1240 status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y,
1242 if (status == MagickFalse)
1251 Convert bitfield encoded DirectColor scanline.
1253 if ((bmp_info.compression != BI_RGB) &&
1254 (bmp_info.compression != BI_BITFIELDS))
1256 pixel_info=RelinquishVirtualMemory(pixel_info);
1257 ThrowReaderException(CorruptImageError,
1258 "UnrecognizedImageCompression");
1260 bytes_per_line=4*(image->columns);
1261 for (y=(ssize_t) image->rows-1; y >= 0; y--)
1266 p=pixels+(image->rows-y-1)*bytes_per_line;
1267 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
1268 if (q == (Quantum *) NULL)
1270 for (x=0; x < (ssize_t) image->columns; x++)
1272 pixel=(size_t) (*p++);
1273 pixel|=((size_t) *p++ << 8);
1274 pixel|=((size_t) *p++ << 16);
1275 pixel|=((size_t) *p++ << 24);
1276 red=((pixel & bmp_info.red_mask) << shift.red) >> 16;
1277 if (quantum_bits.red == 8)
1279 green=((pixel & bmp_info.green_mask) << shift.green) >> 16;
1280 if (quantum_bits.green == 8)
1281 green|=(green >> 8);
1282 blue=((pixel & bmp_info.blue_mask) << shift.blue) >> 16;
1283 if (quantum_bits.blue == 8)
1285 opacity=((pixel & bmp_info.alpha_mask) << shift.alpha) >> 16;
1286 if (quantum_bits.alpha == 8)
1287 opacity|=(opacity >> 8);
1288 SetPixelRed(image,ScaleShortToQuantum((unsigned short) red),q);
1289 SetPixelGreen(image,ScaleShortToQuantum((unsigned short) green),q);
1290 SetPixelBlue(image,ScaleShortToQuantum((unsigned short) blue),q);
1291 SetPixelAlpha(image,OpaqueAlpha,q);
1292 if (image->alpha_trait != UndefinedPixelTrait)
1293 SetPixelAlpha(image,ScaleShortToQuantum((unsigned short)
1295 q+=GetPixelChannels(image);
1297 if (SyncAuthenticPixels(image,exception) == MagickFalse)
1299 offset=(MagickOffsetType) (image->rows-y-1);
1300 if (image->previous == (Image *) NULL)
1302 status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y,
1304 if (status == MagickFalse)
1312 pixel_info=RelinquishVirtualMemory(pixel_info);
1313 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
1316 pixel_info=RelinquishVirtualMemory(pixel_info);
1317 if (EOFBlob(image) != MagickFalse)
1319 ThrowFileException(exception,CorruptImageError,"UnexpectedEndOfFile",
1323 if (bmp_info.height < 0)
1329 Correct image orientation.
1331 flipped_image=FlipImage(image,exception);
1332 if (flipped_image != (Image *) NULL)
1334 DuplicateBlob(flipped_image,image);
1335 image=DestroyImage(image);
1336 image=flipped_image;
1340 Proceed to next image.
1342 if (image_info->number_scenes != 0)
1343 if (image->scene >= (image_info->scene+image_info->number_scenes-1))
1346 if (bmp_info.ba_offset != 0)
1348 offset=SeekBlob(image,(MagickOffsetType) bmp_info.ba_offset,SEEK_SET);
1350 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
1352 count=ReadBlob(image,2,magick);
1353 if ((count == 2) && (IsBMP(magick,2) != MagickFalse))
1356 Acquire next image structure.
1358 AcquireNextImage(image_info,image,exception);
1359 if (GetNextImageInList(image) == (Image *) NULL)
1361 image=DestroyImageList(image);
1362 return((Image *) NULL);
1364 image=SyncNextImageInList(image);
1365 status=SetImageProgress(image,LoadImagesTag,TellBlob(image),
1366 GetBlobSize(image));
1367 if (status == MagickFalse)
1370 } while (IsBMP(magick,2) != MagickFalse);
1371 (void) CloseBlob(image);
1372 return(GetFirstImageInList(image));
1376 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1380 % R e g i s t e r B M P I m a g e %
1384 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1386 % RegisterBMPImage() adds attributes for the BMP image format to
1387 % the list of supported formats. The attributes include the image format
1388 % tag, a method to read and/or write the format, whether the format
1389 % supports the saving of more than one frame to the same file or blob,
1390 % whether the format supports native in-memory I/O, and a brief
1391 % description of the format.
1393 % The format of the RegisterBMPImage method is:
1395 % size_t RegisterBMPImage(void)
1398 ModuleExport size_t RegisterBMPImage(void)
1403 entry=SetMagickInfo("BMP");
1404 entry->decoder=(DecodeImageHandler *) ReadBMPImage;
1405 entry->encoder=(EncodeImageHandler *) WriteBMPImage;
1406 entry->magick=(IsImageFormatHandler *) IsBMP;
1407 entry->description=ConstantString("Microsoft Windows bitmap image");
1408 entry->module=ConstantString("BMP");
1409 entry->adjoin=MagickFalse;
1410 entry->seekable_stream=MagickTrue;
1411 (void) RegisterMagickInfo(entry);
1412 entry=SetMagickInfo("BMP2");
1413 entry->encoder=(EncodeImageHandler *) WriteBMPImage;
1414 entry->magick=(IsImageFormatHandler *) IsBMP;
1415 entry->description=ConstantString("Microsoft Windows bitmap image (V2)");
1416 entry->module=ConstantString("BMP");
1417 entry->adjoin=MagickFalse;
1418 entry->seekable_stream=MagickTrue;
1419 (void) RegisterMagickInfo(entry);
1420 entry=SetMagickInfo("BMP3");
1421 entry->encoder=(EncodeImageHandler *) WriteBMPImage;
1422 entry->magick=(IsImageFormatHandler *) IsBMP;
1423 entry->description=ConstantString("Microsoft Windows bitmap image (V3)");
1424 entry->module=ConstantString("BMP");
1425 entry->adjoin=MagickFalse;
1426 entry->seekable_stream=MagickTrue;
1427 (void) RegisterMagickInfo(entry);
1428 return(MagickImageCoderSignature);
1432 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1436 % U n r e g i s t e r B M P I m a g e %
1440 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1442 % UnregisterBMPImage() removes format registrations made by the
1443 % BMP module from the list of supported formats.
1445 % The format of the UnregisterBMPImage method is:
1447 % UnregisterBMPImage(void)
1450 ModuleExport void UnregisterBMPImage(void)
1452 (void) UnregisterMagickInfo("BMP");
1453 (void) UnregisterMagickInfo("BMP2");
1454 (void) UnregisterMagickInfo("BMP3");
1458 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1462 % W r i t e B M P I m a g e %
1466 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1468 % WriteBMPImage() writes an image in Microsoft Windows bitmap encoded
1469 % image format, version 3 for Windows or (if the image has a matte channel)
1472 % The format of the WriteBMPImage method is:
1474 % MagickBooleanType WriteBMPImage(const ImageInfo *image_info,
1475 % Image *image,ExceptionInfo *exception)
1477 % A description of each parameter follows.
1479 % o image_info: the image info.
1481 % o image: The image.
1483 % o exception: return any errors or warnings in this structure.
1486 static MagickBooleanType WriteBMPImage(const ImageInfo *image_info,Image *image,
1487 ExceptionInfo *exception)
1508 register const Quantum
1515 register unsigned char
1530 Open output image file.
1532 assert(image_info != (const ImageInfo *) NULL);
1533 assert(image_info->signature == MagickSignature);
1534 assert(image != (Image *) NULL);
1535 assert(image->signature == MagickSignature);
1536 if (image->debug != MagickFalse)
1537 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1538 assert(exception != (ExceptionInfo *) NULL);
1539 assert(exception->signature == MagickSignature);
1540 status=OpenBlob(image_info,image,WriteBinaryBlobMode,exception);
1541 if (status == MagickFalse)
1544 if (LocaleCompare(image_info->magick,"BMP2") == 0)
1547 if (LocaleCompare(image_info->magick,"BMP3") == 0)
1550 value=GetImageOption(image_info,"bmp:format");
1552 if (value != (char *) NULL)
1554 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1555 " Format=%s",value);
1557 if (LocaleCompare(value,"bmp2") == 0)
1559 if (LocaleCompare(value,"bmp3") == 0)
1561 if (LocaleCompare(value,"bmp4") == 0)
1569 Initialize BMP raster file header.
1571 (void) TransformImageColorspace(image,sRGBColorspace,exception);
1572 (void) ResetMagickMemory(&bmp_info,0,sizeof(bmp_info));
1573 bmp_info.file_size=14+12;
1575 bmp_info.file_size+=28;
1576 bmp_info.offset_bits=bmp_info.file_size;
1577 bmp_info.compression=BI_RGB;
1578 if ((image->storage_class == PseudoClass) && (image->colors > 256))
1579 (void) SetImageStorageClass(image,DirectClass,exception);
1580 if (image->storage_class != DirectClass)
1583 Colormapped BMP raster.
1585 bmp_info.bits_per_pixel=8;
1586 if (image->colors <= 2)
1587 bmp_info.bits_per_pixel=1;
1589 if (image->colors <= 16)
1590 bmp_info.bits_per_pixel=4;
1592 if (image->colors <= 256)
1593 bmp_info.bits_per_pixel=8;
1594 if (image_info->compression == RLECompression)
1595 bmp_info.bits_per_pixel=8;
1596 bmp_info.number_colors=1U << bmp_info.bits_per_pixel;
1597 if (image->alpha_trait != UndefinedPixelTrait)
1598 (void) SetImageStorageClass(image,DirectClass,exception);
1600 if ((size_t) bmp_info.number_colors < image->colors)
1601 (void) SetImageStorageClass(image,DirectClass,exception);
1604 bmp_info.file_size+=3*(1UL << bmp_info.bits_per_pixel);
1605 bmp_info.offset_bits+=3*(1UL << bmp_info.bits_per_pixel);
1608 bmp_info.file_size+=(1UL << bmp_info.bits_per_pixel);
1609 bmp_info.offset_bits+=(1UL << bmp_info.bits_per_pixel);
1613 if (image->storage_class == DirectClass)
1616 Full color BMP raster.
1618 bmp_info.number_colors=0;
1619 bmp_info.bits_per_pixel=(unsigned short)
1620 ((type > 3) && (image->alpha_trait != UndefinedPixelTrait) ? 32 : 24);
1621 bmp_info.compression=(unsigned int) ((type > 3) &&
1622 (image->alpha_trait != UndefinedPixelTrait) ? BI_BITFIELDS : BI_RGB);
1624 bytes_per_line=4*((image->columns*bmp_info.bits_per_pixel+31)/32);
1625 bmp_info.ba_offset=0;
1626 profile=GetImageProfile(image,"icc");
1627 have_color_info=(image->rendering_intent != UndefinedIntent) ||
1628 (profile != (StringInfo *) NULL) || (image->gamma != 0.0) ? MagickTrue :
1633 if ((type == 3) || ((image->alpha_trait == UndefinedPixelTrait) &&
1634 (have_color_info == MagickFalse)))
1646 if ((image->rendering_intent != UndefinedIntent) ||
1647 (profile != (StringInfo *) NULL))
1652 bmp_info.file_size+=extra_size;
1653 bmp_info.offset_bits+=extra_size;
1655 bmp_info.width=(ssize_t) image->columns;
1656 bmp_info.height=(ssize_t) image->rows;
1658 bmp_info.image_size=(unsigned int) (bytes_per_line*image->rows);
1659 bmp_info.file_size+=bmp_info.image_size;
1660 bmp_info.x_pixels=75*39;
1661 bmp_info.y_pixels=75*39;
1662 switch (image->units)
1664 case UndefinedResolution:
1665 case PixelsPerInchResolution:
1667 bmp_info.x_pixels=(unsigned int) (100.0*image->resolution.x/2.54);
1668 bmp_info.y_pixels=(unsigned int) (100.0*image->resolution.y/2.54);
1671 case PixelsPerCentimeterResolution:
1673 bmp_info.x_pixels=(unsigned int) (100.0*image->resolution.x);
1674 bmp_info.y_pixels=(unsigned int) (100.0*image->resolution.y);
1678 bmp_info.colors_important=bmp_info.number_colors;
1680 Convert MIFF to BMP raster pixels.
1682 pixel_info=AcquireVirtualMemory((size_t) bmp_info.image_size,
1684 if (pixel_info == (MemoryInfo *) NULL)
1685 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
1686 pixels=(unsigned char *) GetVirtualMemoryBlob(pixel_info);
1687 (void) ResetMagickMemory(pixels,0,(size_t) bmp_info.image_size);
1688 switch (bmp_info.bits_per_pixel)
1697 Convert PseudoClass image to a BMP monochrome image.
1699 for (y=0; y < (ssize_t) image->rows; y++)
1704 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
1705 if (p == (const Quantum *) NULL)
1707 q=pixels+(image->rows-y-1)*bytes_per_line;
1710 for (x=0; x < (ssize_t) image->columns; x++)
1713 byte|=GetPixelIndex(image,p) != 0 ? 0x01 : 0x00;
1717 *q++=(unsigned char) byte;
1721 p+=GetPixelChannels(image);
1725 *q++=(unsigned char) (byte << (8-bit));
1728 offset=(ssize_t) (image->columns+7)/8;
1729 for (x=offset; x < (ssize_t) bytes_per_line; x++)
1731 if (image->previous == (Image *) NULL)
1733 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
1735 if (status == MagickFalse)
1751 Convert PseudoClass image to a BMP monochrome image.
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;
1761 for (x=0; x < (ssize_t) image->columns; x++)
1764 byte|=((size_t) GetPixelIndex(image,p) & 0x0f);
1768 *q++=(unsigned char) byte;
1772 p+=GetPixelChannels(image);
1776 *q++=(unsigned char) (byte << 4);
1779 offset=(ssize_t) (image->columns+1)/2;
1780 for (x=offset; x < (ssize_t) bytes_per_line; x++)
1782 if (image->previous == (Image *) NULL)
1784 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
1786 if (status == MagickFalse)
1795 Convert PseudoClass packet to BMP pixel.
1797 for (y=0; y < (ssize_t) image->rows; y++)
1799 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
1800 if (p == (const Quantum *) NULL)
1802 q=pixels+(image->rows-y-1)*bytes_per_line;
1803 for (x=0; x < (ssize_t) image->columns; x++)
1805 *q++=(unsigned char) GetPixelIndex(image,p);
1806 p+=GetPixelChannels(image);
1808 for ( ; x < (ssize_t) bytes_per_line; x++)
1810 if (image->previous == (Image *) NULL)
1812 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
1814 if (status == MagickFalse)
1823 Convert DirectClass packet to BMP BGR888.
1825 for (y=0; y < (ssize_t) image->rows; y++)
1827 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
1828 if (p == (const Quantum *) NULL)
1830 q=pixels+(image->rows-y-1)*bytes_per_line;
1831 for (x=0; x < (ssize_t) image->columns; x++)
1833 *q++=ScaleQuantumToChar(GetPixelBlue(image,p));
1834 *q++=ScaleQuantumToChar(GetPixelGreen(image,p));
1835 *q++=ScaleQuantumToChar(GetPixelRed(image,p));
1836 p+=GetPixelChannels(image);
1838 for (x=3L*(ssize_t) image->columns; x < (ssize_t) bytes_per_line; x++)
1840 if (image->previous == (Image *) NULL)
1842 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
1844 if (status == MagickFalse)
1853 Convert DirectClass packet to ARGB8888 pixel.
1855 for (y=0; y < (ssize_t) image->rows; y++)
1857 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
1858 if (p == (const Quantum *) NULL)
1860 q=pixels+(image->rows-y-1)*bytes_per_line;
1861 for (x=0; x < (ssize_t) image->columns; x++)
1863 *q++=ScaleQuantumToChar(GetPixelBlue(image,p));
1864 *q++=ScaleQuantumToChar(GetPixelGreen(image,p));
1865 *q++=ScaleQuantumToChar(GetPixelRed(image,p));
1866 *q++=ScaleQuantumToChar(GetPixelAlpha(image,p));
1867 p+=GetPixelChannels(image);
1869 if (image->previous == (Image *) NULL)
1871 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
1873 if (status == MagickFalse)
1880 if ((type > 2) && (bmp_info.bits_per_pixel == 8))
1881 if (image_info->compression != NoCompression)
1887 Convert run-length encoded raster pixels.
1889 rle_info=AcquireVirtualMemory((size_t) (2*(bytes_per_line+2)+2),
1890 (image->rows+2)*sizeof(*pixels));
1891 if (rle_info == (MemoryInfo *) NULL)
1893 pixel_info=RelinquishVirtualMemory(pixel_info);
1894 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
1896 bmp_data=(unsigned char *) GetVirtualMemoryBlob(rle_info);
1897 bmp_info.file_size-=bmp_info.image_size;
1898 bmp_info.image_size=(unsigned int) EncodeImage(image,bytes_per_line,
1900 bmp_info.file_size+=bmp_info.image_size;
1901 pixel_info=RelinquishVirtualMemory(pixel_info);
1902 pixel_info=rle_info;
1904 bmp_info.compression=BI_RLE8;
1907 Write BMP for Windows, all versions, 14-byte header.
1909 if (image->debug != MagickFalse)
1911 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1912 " Writing BMP version %.20g datastream",(double) type);
1913 if (image->storage_class == DirectClass)
1914 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1915 " Storage class=DirectClass");
1917 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1918 " Storage class=PseudoClass");
1919 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1920 " Image depth=%.20g",(double) image->depth);
1921 if (image->alpha_trait != UndefinedPixelTrait)
1922 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1925 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1926 " Matte=MagickFalse");
1927 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1928 " BMP bits_per_pixel=%.20g",(double) bmp_info.bits_per_pixel);
1929 switch ((int) bmp_info.compression)
1933 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1934 " Compression=BI_RGB");
1939 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1940 " Compression=BI_RLE8");
1945 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1946 " Compression=BI_BITFIELDS");
1951 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1952 " Compression=UNKNOWN (%u)",bmp_info.compression);
1956 if (bmp_info.number_colors == 0)
1957 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1958 " Number_colors=unspecified");
1960 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1961 " Number_colors=%u",bmp_info.number_colors);
1963 (void) WriteBlob(image,2,(unsigned char *) "BM");
1964 (void) WriteBlobLSBLong(image,bmp_info.file_size);
1965 (void) WriteBlobLSBLong(image,bmp_info.ba_offset); /* always 0 */
1966 (void) WriteBlobLSBLong(image,bmp_info.offset_bits);
1970 Write 12-byte version 2 bitmap header.
1972 (void) WriteBlobLSBLong(image,bmp_info.size);
1973 (void) WriteBlobLSBShort(image,(unsigned short) bmp_info.width);
1974 (void) WriteBlobLSBShort(image,(unsigned short) bmp_info.height);
1975 (void) WriteBlobLSBShort(image,bmp_info.planes);
1976 (void) WriteBlobLSBShort(image,bmp_info.bits_per_pixel);
1981 Write 40-byte version 3+ bitmap header.
1983 (void) WriteBlobLSBLong(image,bmp_info.size);
1984 (void) WriteBlobLSBLong(image,(unsigned int) bmp_info.width);
1985 (void) WriteBlobLSBLong(image,(unsigned int) bmp_info.height);
1986 (void) WriteBlobLSBShort(image,bmp_info.planes);
1987 (void) WriteBlobLSBShort(image,bmp_info.bits_per_pixel);
1988 (void) WriteBlobLSBLong(image,bmp_info.compression);
1989 (void) WriteBlobLSBLong(image,bmp_info.image_size);
1990 (void) WriteBlobLSBLong(image,bmp_info.x_pixels);
1991 (void) WriteBlobLSBLong(image,bmp_info.y_pixels);
1992 (void) WriteBlobLSBLong(image,bmp_info.number_colors);
1993 (void) WriteBlobLSBLong(image,bmp_info.colors_important);
1995 if ((type > 3) && ((image->alpha_trait != UndefinedPixelTrait) ||
1996 (have_color_info != MagickFalse)))
1999 Write the rest of the 108-byte BMP Version 4 header.
2001 (void) WriteBlobLSBLong(image,0x00ff0000U); /* Red mask */
2002 (void) WriteBlobLSBLong(image,0x0000ff00U); /* Green mask */
2003 (void) WriteBlobLSBLong(image,0x000000ffU); /* Blue mask */
2004 (void) WriteBlobLSBLong(image,0xff000000U); /* Alpha mask */
2005 (void) WriteBlobLSBLong(image,0x73524742U); /* sRGB */
2006 (void) WriteBlobLSBLong(image,(unsigned int)
2007 (image->chromaticity.red_primary.x*0x40000000));
2008 (void) WriteBlobLSBLong(image,(unsigned int)
2009 (image->chromaticity.red_primary.y*0x40000000));
2010 (void) WriteBlobLSBLong(image,(unsigned int)
2011 ((1.000f-(image->chromaticity.red_primary.x+
2012 image->chromaticity.red_primary.y))*0x40000000));
2013 (void) WriteBlobLSBLong(image,(unsigned int)
2014 (image->chromaticity.green_primary.x*0x40000000));
2015 (void) WriteBlobLSBLong(image,(unsigned int)
2016 (image->chromaticity.green_primary.y*0x40000000));
2017 (void) WriteBlobLSBLong(image,(unsigned int)
2018 ((1.000f-(image->chromaticity.green_primary.x+
2019 image->chromaticity.green_primary.y))*0x40000000));
2020 (void) WriteBlobLSBLong(image,(unsigned int)
2021 (image->chromaticity.blue_primary.x*0x40000000));
2022 (void) WriteBlobLSBLong(image,(unsigned int)
2023 (image->chromaticity.blue_primary.y*0x40000000));
2024 (void) WriteBlobLSBLong(image,(unsigned int)
2025 ((1.000f-(image->chromaticity.blue_primary.x+
2026 image->chromaticity.blue_primary.y))*0x40000000));
2027 (void) WriteBlobLSBLong(image,(unsigned int)
2028 (bmp_info.gamma_scale.x*0x10000));
2029 (void) WriteBlobLSBLong(image,(unsigned int)
2030 (bmp_info.gamma_scale.y*0x10000));
2031 (void) WriteBlobLSBLong(image,(unsigned int)
2032 (bmp_info.gamma_scale.z*0x10000));
2033 if ((image->rendering_intent != UndefinedIntent) ||
2034 (profile != (StringInfo *) NULL))
2039 switch ((int) image->rendering_intent)
2041 case SaturationIntent:
2043 intent=LCS_GM_BUSINESS;
2046 case RelativeIntent:
2048 intent=LCS_GM_GRAPHICS;
2051 case PerceptualIntent:
2053 intent=LCS_GM_IMAGES;
2056 case AbsoluteIntent:
2058 intent=LCS_GM_ABS_COLORIMETRIC;
2067 (void) WriteBlobLSBLong(image,(unsigned int) intent);
2068 (void) WriteBlobLSBLong(image,0x00); /* dummy profile data */
2069 (void) WriteBlobLSBLong(image,0x00); /* dummy profile length */
2070 (void) WriteBlobLSBLong(image,0x00); /* reserved */
2073 if (image->storage_class == PseudoClass)
2079 Dump colormap to file.
2081 if (image->debug != MagickFalse)
2082 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
2083 " Colormap: %.20g entries",(double) image->colors);
2084 bmp_colormap=(unsigned char *) AcquireQuantumMemory((size_t) (1UL <<
2085 bmp_info.bits_per_pixel),4*sizeof(*bmp_colormap));
2086 if (bmp_colormap == (unsigned char *) NULL)
2087 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
2089 for (i=0; i < (ssize_t) MagickMin((ssize_t) image->colors,(ssize_t) bmp_info.number_colors); i++)
2091 *q++=ScaleQuantumToChar(ClampToQuantum(image->colormap[i].blue));
2092 *q++=ScaleQuantumToChar(ClampToQuantum(image->colormap[i].green));
2093 *q++=ScaleQuantumToChar(ClampToQuantum(image->colormap[i].red));
2095 *q++=(unsigned char) 0x0;
2097 for ( ; i < (ssize_t) (1UL << bmp_info.bits_per_pixel); i++)
2099 *q++=(unsigned char) 0x00;
2100 *q++=(unsigned char) 0x00;
2101 *q++=(unsigned char) 0x00;
2103 *q++=(unsigned char) 0x00;
2106 (void) WriteBlob(image,(size_t) (3*(1L << bmp_info.bits_per_pixel)),
2109 (void) WriteBlob(image,(size_t) (4*(1L << bmp_info.bits_per_pixel)),
2111 bmp_colormap=(unsigned char *) RelinquishMagickMemory(bmp_colormap);
2113 if (image->debug != MagickFalse)
2114 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
2115 " Pixels: %u bytes",bmp_info.image_size);
2116 (void) WriteBlob(image,(size_t) bmp_info.image_size,pixels);
2117 pixel_info=RelinquishVirtualMemory(pixel_info);
2118 if (GetNextImageInList(image) == (Image *) NULL)
2120 image=SyncNextImageInList(image);
2121 status=SetImageProgress(image,SaveImagesTag,scene++,
2122 GetImageListLength(image));
2123 if (status == MagickFalse)
2125 } while (image_info->adjoin != MagickFalse);
2126 (void) CloseBlob(image);