2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
13 % Read/Write Microsoft Windows Bitmap Image Format %
17 % Glenn Randers-Pehrson %
21 % Copyright 1999-2015 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
551 assert(image_info != (const ImageInfo *) NULL);
552 assert(image_info->signature == MagickSignature);
553 if (image_info->debug != MagickFalse)
554 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
555 image_info->filename);
556 assert(exception != (ExceptionInfo *) NULL);
557 assert(exception->signature == MagickSignature);
558 image=AcquireImage(image_info,exception);
559 status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception);
560 if (status == MagickFalse)
562 image=DestroyImageList(image);
563 return((Image *) NULL);
566 Determine if this a BMP file.
568 (void) ResetMagickMemory(&bmp_info,0,sizeof(bmp_info));
569 bmp_info.ba_offset=0;
571 count=ReadBlob(image,2,magick);
585 Verify BMP identifier.
587 if (bmp_info.ba_offset == 0)
588 start_position=TellBlob(image)-2;
589 bmp_info.ba_offset=0;
590 while (LocaleNCompare((char *) magick,"BA",2) == 0)
592 bmp_info.file_size=ReadBlobLSBLong(image);
593 bmp_info.ba_offset=ReadBlobLSBLong(image);
594 bmp_info.offset_bits=ReadBlobLSBLong(image);
595 count=ReadBlob(image,2,magick);
599 if (image->debug != MagickFalse)
600 (void) LogMagickEvent(CoderEvent,GetMagickModule()," Magick: %c%c",
601 magick[0],magick[1]);
602 if ((count == 0) || ((LocaleNCompare((char *) magick,"BM",2) != 0) &&
603 (LocaleNCompare((char *) magick,"CI",2) != 0)))
604 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
605 bmp_info.file_size=ReadBlobLSBLong(image);
606 (void) ReadBlobLSBLong(image);
607 bmp_info.offset_bits=ReadBlobLSBLong(image);
608 bmp_info.size=ReadBlobLSBLong(image);
609 if (image->debug != MagickFalse)
610 (void) LogMagickEvent(CoderEvent,GetMagickModule()," BMP size: %u",
612 if (bmp_info.size == 12)
617 (void) CopyMagickString(image->magick,"BMP2",MaxTextExtent);
618 bmp_info.width=(ssize_t) ((short) ReadBlobLSBShort(image));
619 bmp_info.height=(ssize_t) ((short) ReadBlobLSBShort(image));
620 bmp_info.planes=ReadBlobLSBShort(image);
621 bmp_info.bits_per_pixel=ReadBlobLSBShort(image);
624 bmp_info.number_colors=0;
625 bmp_info.compression=BI_RGB;
626 bmp_info.image_size=0;
627 bmp_info.alpha_mask=0;
628 if (image->debug != MagickFalse)
630 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
631 " Format: OS/2 Bitmap");
632 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
633 " Geometry: %.20gx%.20g",(double) bmp_info.width,(double)
640 Microsoft Windows BMP image file.
642 if (bmp_info.size < 40)
643 ThrowReaderException(CorruptImageError,"NonOS2HeaderSizeError");
644 bmp_info.width=(ssize_t) ((int) ReadBlobLSBLong(image));
645 bmp_info.height=(ssize_t) ((int) ReadBlobLSBLong(image));
646 bmp_info.planes=ReadBlobLSBShort(image);
647 bmp_info.bits_per_pixel=ReadBlobLSBShort(image);
648 bmp_info.compression=ReadBlobLSBLong(image);
649 bmp_info.image_size=ReadBlobLSBLong(image);
650 bmp_info.x_pixels=ReadBlobLSBLong(image);
651 bmp_info.y_pixels=ReadBlobLSBLong(image);
652 bmp_info.number_colors=ReadBlobLSBLong(image);
653 bmp_info.colors_important=ReadBlobLSBLong(image);
656 if (image->debug != MagickFalse)
658 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
659 " Format: MS Windows bitmap");
660 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
661 " Geometry: %.20gx%.20g",(double) bmp_info.width,(double)
663 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
664 " Bits per pixel: %.20g",(double) bmp_info.bits_per_pixel);
665 switch ((int) bmp_info.compression)
669 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
670 " Compression: BI_RGB");
675 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
676 " Compression: BI_RLE4");
681 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
682 " Compression: BI_RLE8");
687 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
688 " Compression: BI_BITFIELDS");
693 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
694 " Compression: BI_PNG");
699 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
700 " Compression: BI_JPEG");
705 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
706 " Compression: UNKNOWN (%u)",bmp_info.compression);
709 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
710 " Number of colors: %u",bmp_info.number_colors);
712 bmp_info.red_mask=ReadBlobLSBLong(image);
713 bmp_info.green_mask=ReadBlobLSBLong(image);
714 bmp_info.blue_mask=ReadBlobLSBLong(image);
715 if (bmp_info.size > 40)
722 Read color management information.
724 bmp_info.alpha_mask=ReadBlobLSBLong(image);
725 bmp_info.colorspace=(int) ReadBlobLSBLong(image);
727 Decode 2^30 fixed point formatted CIE primaries.
729 # define BMP_DENOM ((double) 0x40000000)
730 bmp_info.red_primary.x=(double) ReadBlobLSBLong(image)/BMP_DENOM;
731 bmp_info.red_primary.y=(double) ReadBlobLSBLong(image)/BMP_DENOM;
732 bmp_info.red_primary.z=(double) ReadBlobLSBLong(image)/BMP_DENOM;
733 bmp_info.green_primary.x=(double) ReadBlobLSBLong(image)/BMP_DENOM;
734 bmp_info.green_primary.y=(double) ReadBlobLSBLong(image)/BMP_DENOM;
735 bmp_info.green_primary.z=(double) ReadBlobLSBLong(image)/BMP_DENOM;
736 bmp_info.blue_primary.x=(double) ReadBlobLSBLong(image)/BMP_DENOM;
737 bmp_info.blue_primary.y=(double) ReadBlobLSBLong(image)/BMP_DENOM;
738 bmp_info.blue_primary.z=(double) ReadBlobLSBLong(image)/BMP_DENOM;
740 sum=bmp_info.red_primary.x+bmp_info.red_primary.y+
741 bmp_info.red_primary.z;
742 bmp_info.red_primary.x/=sum;
743 bmp_info.red_primary.y/=sum;
744 image->chromaticity.red_primary.x=bmp_info.red_primary.x;
745 image->chromaticity.red_primary.y=bmp_info.red_primary.y;
747 sum=bmp_info.green_primary.x+bmp_info.green_primary.y+
748 bmp_info.green_primary.z;
749 bmp_info.green_primary.x/=sum;
750 bmp_info.green_primary.y/=sum;
751 image->chromaticity.green_primary.x=bmp_info.green_primary.x;
752 image->chromaticity.green_primary.y=bmp_info.green_primary.y;
754 sum=bmp_info.blue_primary.x+bmp_info.blue_primary.y+
755 bmp_info.blue_primary.z;
756 bmp_info.blue_primary.x/=sum;
757 bmp_info.blue_primary.y/=sum;
758 image->chromaticity.blue_primary.x=bmp_info.blue_primary.x;
759 image->chromaticity.blue_primary.y=bmp_info.blue_primary.y;
762 Decode 16^16 fixed point formatted gamma_scales.
764 bmp_info.gamma_scale.x=(double) ReadBlobLSBLong(image)/0x10000;
765 bmp_info.gamma_scale.y=(double) ReadBlobLSBLong(image)/0x10000;
766 bmp_info.gamma_scale.z=(double) ReadBlobLSBLong(image)/0x10000;
768 Compute a single gamma from the BMP 3-channel gamma.
770 image->gamma=(bmp_info.gamma_scale.x+bmp_info.gamma_scale.y+
771 bmp_info.gamma_scale.z)/3.0;
774 (void) CopyMagickString(image->magick,"BMP3",MaxTextExtent);
776 if (bmp_info.size > 108)
782 Read BMP Version 5 color management information.
784 intent=ReadBlobLSBLong(image);
785 switch ((int) intent)
787 case LCS_GM_BUSINESS:
789 image->rendering_intent=SaturationIntent;
792 case LCS_GM_GRAPHICS:
794 image->rendering_intent=RelativeIntent;
799 image->rendering_intent=PerceptualIntent;
802 case LCS_GM_ABS_COLORIMETRIC:
804 image->rendering_intent=AbsoluteIntent;
808 profile_data=ReadBlobLSBLong(image);
809 profile_size=ReadBlobLSBLong(image);
812 (void) ReadBlobLSBLong(image); /* Reserved byte */
815 if ((MagickSizeType) bmp_info.file_size > GetBlobSize(image))
816 (void) ThrowMagickException(exception,GetMagickModule(),CorruptImageError,
817 "LengthAndFilesizeDoNotMatch","`%s'",image->filename);
819 if ((MagickSizeType) bmp_info.file_size < GetBlobSize(image))
820 (void) ThrowMagickException(exception,GetMagickModule(),
821 CorruptImageWarning,"LengthAndFilesizeDoNotMatch","`%s'",
823 if (bmp_info.width <= 0)
824 ThrowReaderException(CorruptImageError,"NegativeOrZeroImageSize");
825 if (bmp_info.height == 0)
826 ThrowReaderException(CorruptImageError,"NegativeOrZeroImageSize");
827 if (bmp_info.planes != 1)
828 ThrowReaderException(CorruptImageError,"StaticPlanesValueNotEqualToOne");
829 if ((bmp_info.bits_per_pixel != 1) && (bmp_info.bits_per_pixel != 4) &&
830 (bmp_info.bits_per_pixel != 8) && (bmp_info.bits_per_pixel != 16) &&
831 (bmp_info.bits_per_pixel != 24) && (bmp_info.bits_per_pixel != 32))
832 ThrowReaderException(CorruptImageError,"UnrecognizedBitsPerPixel");
833 if (bmp_info.bits_per_pixel < 16 &&
834 bmp_info.number_colors > (1U << bmp_info.bits_per_pixel))
835 ThrowReaderException(CorruptImageError,"UnrecognizedNumberOfColors");
836 if ((bmp_info.compression == 1) && (bmp_info.bits_per_pixel != 8))
837 ThrowReaderException(CorruptImageError,"UnrecognizedBitsPerPixel");
838 if ((bmp_info.compression == 2) && (bmp_info.bits_per_pixel != 4))
839 ThrowReaderException(CorruptImageError,"UnrecognizedBitsPerPixel");
840 if ((bmp_info.compression == 3) && (bmp_info.bits_per_pixel < 16))
841 ThrowReaderException(CorruptImageError,"UnrecognizedBitsPerPixel");
842 switch (bmp_info.compression)
850 ThrowReaderException(CoderError,"JPEGCompressNotSupported");
852 ThrowReaderException(CoderError,"PNGCompressNotSupported");
854 ThrowReaderException(CorruptImageError,"UnrecognizedImageCompression");
856 image->columns=(size_t) MagickAbsoluteValue(bmp_info.width);
857 image->rows=(size_t) MagickAbsoluteValue(bmp_info.height);
858 image->depth=bmp_info.bits_per_pixel <= 8 ? bmp_info.bits_per_pixel : 8;
859 image->alpha_trait=((bmp_info.alpha_mask != 0) &&
860 (bmp_info.compression == BI_BITFIELDS)) ? BlendPixelTrait :
862 if (bmp_info.bits_per_pixel < 16)
867 image->storage_class=PseudoClass;
868 image->colors=bmp_info.number_colors;
870 if (image->colors == 0)
871 image->colors=one << bmp_info.bits_per_pixel;
873 if (image->storage_class == PseudoClass)
882 Read BMP raster colormap.
884 if (image->debug != MagickFalse)
885 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
886 " Reading colormap of %.20g colors",(double) image->colors);
887 if (AcquireImageColormap(image,image->colors,exception) == MagickFalse)
888 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
889 bmp_colormap=(unsigned char *) AcquireQuantumMemory((size_t)
890 image->colors,4*sizeof(*bmp_colormap));
891 if (bmp_colormap == (unsigned char *) NULL)
892 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
893 if ((bmp_info.size == 12) || (bmp_info.size == 64))
897 offset=SeekBlob(image,start_position+14+bmp_info.size,SEEK_SET);
899 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
900 count=ReadBlob(image,packet_size*image->colors,bmp_colormap);
901 if (count != (ssize_t) (packet_size*image->colors))
902 ThrowReaderException(CorruptImageError,"InsufficientImageDataInFile");
904 for (i=0; i < (ssize_t) image->colors; i++)
906 image->colormap[i].blue=(MagickRealType) ScaleCharToQuantum(*p++);
907 image->colormap[i].green=(MagickRealType) ScaleCharToQuantum(*p++);
908 image->colormap[i].red=(MagickRealType) ScaleCharToQuantum(*p++);
909 if (packet_size == 4)
912 bmp_colormap=(unsigned char *) RelinquishMagickMemory(bmp_colormap);
914 image->resolution.x=(double) bmp_info.x_pixels/100.0;
915 image->resolution.y=(double) bmp_info.y_pixels/100.0;
916 image->units=PixelsPerCentimeterResolution;
917 if ((image_info->ping != MagickFalse) && (image_info->number_scenes != 0))
918 if (image->scene >= (image_info->scene+image_info->number_scenes-1))
920 status=SetImageExtent(image,image->columns,image->rows,exception);
921 if (status == MagickFalse)
922 return(DestroyImageList(image));
926 offset=SeekBlob(image,start_position+bmp_info.offset_bits,SEEK_SET);
928 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
929 if (bmp_info.compression == BI_RLE4)
930 bmp_info.bits_per_pixel<<=1;
931 bytes_per_line=4*((image->columns*bmp_info.bits_per_pixel+31)/32);
932 length=(size_t) bytes_per_line*image->rows;
933 pixel_info=AcquireVirtualMemory((size_t) image->rows,
934 MagickMax(bytes_per_line,image->columns+256UL)*sizeof(*pixels));
935 if (pixel_info == (MemoryInfo *) NULL)
936 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
937 pixels=(unsigned char *) GetVirtualMemoryBlob(pixel_info);
938 if ((bmp_info.compression == BI_RGB) ||
939 (bmp_info.compression == BI_BITFIELDS))
941 if (image->debug != MagickFalse)
942 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
943 " Reading pixels (%.20g bytes)",(double) length);
944 count=ReadBlob(image,length,pixels);
945 if (count != (ssize_t) length)
947 pixel_info=RelinquishVirtualMemory(pixel_info);
948 ThrowReaderException(CorruptImageError,
949 "InsufficientImageDataInFile");
955 Convert run-length encoded raster pixels.
957 status=DecodeImage(image,bmp_info.compression,pixels);
958 if (status == MagickFalse)
960 pixel_info=RelinquishVirtualMemory(pixel_info);
961 ThrowReaderException(CorruptImageError,
962 "UnableToRunlengthDecodeImage");
966 Convert BMP raster image to pixel packets.
968 if (bmp_info.compression == BI_RGB)
971 We should ignore the alpha value in BMP3 files but there have been
972 reports about 32 bit files with alpha. We do a quick check to see if
973 the alpha channel contains a value that is not zero (default value).
974 If we find a non zero value we asume the program that wrote the file
975 wants to use the alpha channel.
977 if ((image->alpha_trait == UndefinedPixelTrait) && (bmp_info.size == 40) &&
978 (bmp_info.bits_per_pixel == 32))
980 bytes_per_line=4*(image->columns);
981 for (y=(ssize_t) image->rows-1; y >= 0; y--)
983 p=pixels+(image->rows-y-1)*bytes_per_line;
984 for (x=0; x < (ssize_t) image->columns; x++)
988 image->alpha_trait=BlendPixelTrait;
996 bmp_info.alpha_mask=image->alpha_trait != UndefinedPixelTrait ?
998 bmp_info.red_mask=0x00ff0000U;
999 bmp_info.green_mask=0x0000ff00U;
1000 bmp_info.blue_mask=0x000000ffU;
1001 if (bmp_info.bits_per_pixel == 16)
1006 bmp_info.red_mask=0x00007c00U;
1007 bmp_info.green_mask=0x000003e0U;
1008 bmp_info.blue_mask=0x0000001fU;
1011 if ((bmp_info.bits_per_pixel == 16) || (bmp_info.bits_per_pixel == 32))
1017 Get shift and quantum bits info from bitfield masks.
1019 (void) ResetMagickMemory(&shift,0,sizeof(shift));
1020 (void) ResetMagickMemory(&quantum_bits,0,sizeof(quantum_bits));
1021 if (bmp_info.red_mask != 0)
1022 while (((bmp_info.red_mask << shift.red) & 0x80000000UL) == 0)
1024 if (bmp_info.green_mask != 0)
1025 while (((bmp_info.green_mask << shift.green) & 0x80000000UL) == 0)
1027 if (bmp_info.blue_mask != 0)
1028 while (((bmp_info.blue_mask << shift.blue) & 0x80000000UL) == 0)
1030 if (bmp_info.alpha_mask != 0)
1031 while (((bmp_info.alpha_mask << shift.alpha) & 0x80000000UL) == 0)
1034 while (((bmp_info.red_mask << sample) & 0x80000000UL) != 0)
1036 quantum_bits.red=(MagickRealType) (sample-shift.red);
1038 while (((bmp_info.green_mask << sample) & 0x80000000UL) != 0)
1040 quantum_bits.green=(MagickRealType) (sample-shift.green);
1042 while (((bmp_info.blue_mask << sample) & 0x80000000UL) != 0)
1044 quantum_bits.blue=(MagickRealType) (sample-shift.blue);
1046 while (((bmp_info.alpha_mask << sample) & 0x80000000UL) != 0)
1048 quantum_bits.alpha=(MagickRealType) (sample-shift.alpha);
1050 switch (bmp_info.bits_per_pixel)
1055 Convert bitmap scanline.
1057 for (y=(ssize_t) image->rows-1; y >= 0; y--)
1059 p=pixels+(image->rows-y-1)*bytes_per_line;
1060 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
1061 if (q == (Quantum *) NULL)
1063 for (x=0; x < ((ssize_t) image->columns-7); x+=8)
1065 for (bit=0; bit < 8; bit++)
1067 index=(Quantum) (((*p) & (0x80 >> bit)) != 0 ? 0x01 : 0x00);
1068 SetPixelIndex(image,index,q);
1069 q+=GetPixelChannels(image);
1073 if ((image->columns % 8) != 0)
1075 for (bit=0; bit < (image->columns % 8); bit++)
1077 index=(Quantum) (((*p) & (0x80 >> bit)) != 0 ? 0x01 : 0x00);
1078 SetPixelIndex(image,index,q);
1079 q+=GetPixelChannels(image);
1083 if (SyncAuthenticPixels(image,exception) == MagickFalse)
1085 if (image->previous == (Image *) NULL)
1087 status=SetImageProgress(image,LoadImageTag,(MagickOffsetType)
1088 (image->rows-y),image->rows);
1089 if (status == MagickFalse)
1093 (void) SyncImage(image,exception);
1099 Convert PseudoColor scanline.
1101 for (y=(ssize_t) image->rows-1; y >= 0; y--)
1103 p=pixels+(image->rows-y-1)*bytes_per_line;
1104 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
1105 if (q == (Quantum *) NULL)
1107 for (x=0; x < ((ssize_t) image->columns-1); x+=2)
1109 index=ConstrainColormapIndex(image,(*p >> 4) & 0x0f,exception);
1110 SetPixelIndex(image,index,q);
1111 q+=GetPixelChannels(image);
1112 index=ConstrainColormapIndex(image,*p & 0x0f,exception);
1113 SetPixelIndex(image,index,q);
1114 q+=GetPixelChannels(image);
1117 if ((image->columns % 2) != 0)
1119 index=ConstrainColormapIndex(image,(*p >> 4) & 0xf,exception);
1120 SetPixelIndex(image,index,q);
1121 q+=GetPixelChannels(image);
1124 if (SyncAuthenticPixels(image,exception) == MagickFalse)
1126 if (image->previous == (Image *) NULL)
1128 status=SetImageProgress(image,LoadImageTag,(MagickOffsetType)
1129 (image->rows-y),image->rows);
1130 if (status == MagickFalse)
1134 (void) SyncImage(image,exception);
1140 Convert PseudoColor scanline.
1142 if ((bmp_info.compression == BI_RLE8) ||
1143 (bmp_info.compression == BI_RLE4))
1144 bytes_per_line=image->columns;
1145 for (y=(ssize_t) image->rows-1; y >= 0; y--)
1147 p=pixels+(image->rows-y-1)*bytes_per_line;
1148 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
1149 if (q == (Quantum *) NULL)
1151 for (x=(ssize_t) image->columns; x != 0; --x)
1153 index=ConstrainColormapIndex(image,*p++,exception);
1154 SetPixelIndex(image,index,q);
1155 q+=GetPixelChannels(image);
1157 if (SyncAuthenticPixels(image,exception) == MagickFalse)
1159 offset=(MagickOffsetType) (image->rows-y-1);
1160 if (image->previous == (Image *) NULL)
1162 status=SetImageProgress(image,LoadImageTag,(MagickOffsetType)
1163 (image->rows-y),image->rows);
1164 if (status == MagickFalse)
1168 (void) SyncImage(image,exception);
1178 Convert bitfield encoded 16-bit PseudoColor scanline.
1180 if (bmp_info.compression != BI_RGB &&
1181 bmp_info.compression != BI_BITFIELDS)
1183 pixel_info=RelinquishVirtualMemory(pixel_info);
1184 ThrowReaderException(CorruptImageError,
1185 "UnrecognizedImageCompression");
1187 bytes_per_line=2*(image->columns+image->columns % 2);
1188 image->storage_class=DirectClass;
1189 for (y=(ssize_t) image->rows-1; y >= 0; y--)
1191 p=pixels+(image->rows-y-1)*bytes_per_line;
1192 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
1193 if (q == (Quantum *) NULL)
1195 for (x=0; x < (ssize_t) image->columns; x++)
1197 pixel=(size_t) (*p++);
1199 red=((pixel & bmp_info.red_mask) << shift.red) >> 16;
1200 if (quantum_bits.red == 5)
1201 red|=((red & 0xe000) >> 5);
1202 if (quantum_bits.red <= 8)
1203 red|=((red & 0xff00) >> 8);
1204 green=((pixel & bmp_info.green_mask) << shift.green) >> 16;
1205 if (quantum_bits.green == 5)
1206 green|=((green & 0xe000) >> 5);
1207 if (quantum_bits.green == 6)
1208 green|=((green & 0xc000) >> 6);
1209 if (quantum_bits.green <= 8)
1210 green|=((green & 0xff00) >> 8);
1211 blue=((pixel & bmp_info.blue_mask) << shift.blue) >> 16;
1212 if (quantum_bits.blue == 5)
1213 blue|=((blue & 0xe000) >> 5);
1214 if (quantum_bits.blue <= 8)
1215 blue|=((blue & 0xff00) >> 8);
1216 SetPixelRed(image,ScaleShortToQuantum((unsigned short) red),q);
1217 SetPixelGreen(image,ScaleShortToQuantum((unsigned short) green),q);
1218 SetPixelBlue(image,ScaleShortToQuantum((unsigned short) blue),q);
1219 SetPixelAlpha(image,OpaqueAlpha,q);
1220 if (image->alpha_trait != UndefinedPixelTrait)
1222 alpha=((pixel & bmp_info.alpha_mask) << shift.alpha) >> 16;
1223 if (quantum_bits.alpha <= 8)
1224 alpha|=((alpha & 0xff00) >> 8);
1225 SetPixelAlpha(image,ScaleShortToQuantum(
1226 (unsigned short) alpha),q);
1228 q+=GetPixelChannels(image);
1230 if (SyncAuthenticPixels(image,exception) == MagickFalse)
1232 offset=(MagickOffsetType) (image->rows-y-1);
1233 if (image->previous == (Image *) NULL)
1235 status=SetImageProgress(image,LoadImageTag,(MagickOffsetType)
1236 (image->rows-y),image->rows);
1237 if (status == MagickFalse)
1246 Convert DirectColor scanline.
1248 bytes_per_line=4*((image->columns*24+31)/32);
1249 for (y=(ssize_t) image->rows-1; y >= 0; y--)
1251 p=pixels+(image->rows-y-1)*bytes_per_line;
1252 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
1253 if (q == (Quantum *) NULL)
1255 for (x=0; x < (ssize_t) image->columns; x++)
1257 SetPixelBlue(image,ScaleCharToQuantum(*p++),q);
1258 SetPixelGreen(image,ScaleCharToQuantum(*p++),q);
1259 SetPixelRed(image,ScaleCharToQuantum(*p++),q);
1260 SetPixelAlpha(image,OpaqueAlpha,q);
1261 q+=GetPixelChannels(image);
1263 if (SyncAuthenticPixels(image,exception) == MagickFalse)
1265 offset=(MagickOffsetType) (image->rows-y-1);
1266 if (image->previous == (Image *) NULL)
1268 status=SetImageProgress(image,LoadImageTag,(MagickOffsetType)
1269 (image->rows-y),image->rows);
1270 if (status == MagickFalse)
1279 Convert bitfield encoded DirectColor scanline.
1281 if ((bmp_info.compression != BI_RGB) &&
1282 (bmp_info.compression != BI_BITFIELDS))
1284 pixel_info=RelinquishVirtualMemory(pixel_info);
1285 ThrowReaderException(CorruptImageError,
1286 "UnrecognizedImageCompression");
1288 bytes_per_line=4*(image->columns);
1289 for (y=(ssize_t) image->rows-1; y >= 0; y--)
1295 p=pixels+(image->rows-y-1)*bytes_per_line;
1296 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
1297 if (q == (Quantum *) NULL)
1299 for (x=0; x < (ssize_t) image->columns; x++)
1301 pixel=(size_t) (*p++);
1302 pixel|=((size_t) *p++ << 8);
1303 pixel|=((size_t) *p++ << 16);
1304 pixel|=((size_t) *p++ << 24);
1305 red=((pixel & bmp_info.red_mask) << shift.red) >> 16;
1306 if (quantum_bits.red == 8)
1308 green=((pixel & bmp_info.green_mask) << shift.green) >> 16;
1309 if (quantum_bits.green == 8)
1310 green|=(green >> 8);
1311 blue=((pixel & bmp_info.blue_mask) << shift.blue) >> 16;
1312 if (quantum_bits.blue == 8)
1314 SetPixelRed(image,ScaleShortToQuantum((unsigned short) red),q);
1315 SetPixelGreen(image,ScaleShortToQuantum((unsigned short) green),q);
1316 SetPixelBlue(image,ScaleShortToQuantum((unsigned short) blue),q);
1317 SetPixelAlpha(image,OpaqueAlpha,q);
1318 if (image->alpha_trait != UndefinedPixelTrait)
1320 alpha=((pixel & bmp_info.alpha_mask) << shift.alpha) >> 16;
1321 if (quantum_bits.alpha == 8)
1322 alpha|=(alpha >> 8);
1323 SetPixelAlpha(image,ScaleShortToQuantum(
1324 (unsigned short) alpha),q);
1326 q+=GetPixelChannels(image);
1328 if (SyncAuthenticPixels(image,exception) == MagickFalse)
1330 offset=(MagickOffsetType) (image->rows-y-1);
1331 if (image->previous == (Image *) NULL)
1333 status=SetImageProgress(image,LoadImageTag,(MagickOffsetType)
1334 (image->rows-y),image->rows);
1335 if (status == MagickFalse)
1343 pixel_info=RelinquishVirtualMemory(pixel_info);
1344 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
1347 pixel_info=RelinquishVirtualMemory(pixel_info);
1348 if (EOFBlob(image) != MagickFalse)
1350 ThrowFileException(exception,CorruptImageError,"UnexpectedEndOfFile",
1354 if (bmp_info.height < 0)
1360 Correct image orientation.
1362 flipped_image=FlipImage(image,exception);
1363 if (flipped_image != (Image *) NULL)
1365 DuplicateBlob(flipped_image,image);
1366 image=DestroyImage(image);
1367 image=flipped_image;
1371 Proceed to next image.
1373 if (image_info->number_scenes != 0)
1374 if (image->scene >= (image_info->scene+image_info->number_scenes-1))
1377 if (bmp_info.ba_offset != 0)
1379 offset=SeekBlob(image,(MagickOffsetType) bmp_info.ba_offset,SEEK_SET);
1381 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
1383 count=ReadBlob(image,2,magick);
1384 if ((count == 2) && (IsBMP(magick,2) != MagickFalse))
1387 Acquire next image structure.
1389 AcquireNextImage(image_info,image,exception);
1390 if (GetNextImageInList(image) == (Image *) NULL)
1392 image=DestroyImageList(image);
1393 return((Image *) NULL);
1395 image=SyncNextImageInList(image);
1396 status=SetImageProgress(image,LoadImagesTag,TellBlob(image),
1397 GetBlobSize(image));
1398 if (status == MagickFalse)
1401 } while (IsBMP(magick,2) != MagickFalse);
1402 (void) CloseBlob(image);
1403 return(GetFirstImageInList(image));
1407 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1411 % R e g i s t e r B M P I m a g e %
1415 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1417 % RegisterBMPImage() adds attributes for the BMP image format to
1418 % the list of supported formats. The attributes include the image format
1419 % tag, a method to read and/or write the format, whether the format
1420 % supports the saving of more than one frame to the same file or blob,
1421 % whether the format supports native in-memory I/O, and a brief
1422 % description of the format.
1424 % The format of the RegisterBMPImage method is:
1426 % size_t RegisterBMPImage(void)
1429 ModuleExport size_t RegisterBMPImage(void)
1434 entry=SetMagickInfo("BMP");
1435 entry->decoder=(DecodeImageHandler *) ReadBMPImage;
1436 entry->encoder=(EncodeImageHandler *) WriteBMPImage;
1437 entry->magick=(IsImageFormatHandler *) IsBMP;
1438 entry->description=ConstantString("Microsoft Windows bitmap image");
1439 entry->module=ConstantString("BMP");
1440 entry->adjoin=MagickFalse;
1441 entry->seekable_stream=MagickTrue;
1442 (void) RegisterMagickInfo(entry);
1443 entry=SetMagickInfo("BMP2");
1444 entry->encoder=(EncodeImageHandler *) WriteBMPImage;
1445 entry->magick=(IsImageFormatHandler *) IsBMP;
1446 entry->description=ConstantString("Microsoft Windows bitmap image (V2)");
1447 entry->module=ConstantString("BMP");
1448 entry->adjoin=MagickFalse;
1449 entry->seekable_stream=MagickTrue;
1450 (void) RegisterMagickInfo(entry);
1451 entry=SetMagickInfo("BMP3");
1452 entry->encoder=(EncodeImageHandler *) WriteBMPImage;
1453 entry->magick=(IsImageFormatHandler *) IsBMP;
1454 entry->description=ConstantString("Microsoft Windows bitmap image (V3)");
1455 entry->module=ConstantString("BMP");
1456 entry->adjoin=MagickFalse;
1457 entry->seekable_stream=MagickTrue;
1458 (void) RegisterMagickInfo(entry);
1459 return(MagickImageCoderSignature);
1463 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1467 % U n r e g i s t e r B M P I m a g e %
1471 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1473 % UnregisterBMPImage() removes format registrations made by the
1474 % BMP module from the list of supported formats.
1476 % The format of the UnregisterBMPImage method is:
1478 % UnregisterBMPImage(void)
1481 ModuleExport void UnregisterBMPImage(void)
1483 (void) UnregisterMagickInfo("BMP");
1484 (void) UnregisterMagickInfo("BMP2");
1485 (void) UnregisterMagickInfo("BMP3");
1489 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1493 % W r i t e B M P I m a g e %
1497 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1499 % WriteBMPImage() writes an image in Microsoft Windows bitmap encoded
1500 % image format, version 3 for Windows or (if the image has a matte channel)
1503 % The format of the WriteBMPImage method is:
1505 % MagickBooleanType WriteBMPImage(const ImageInfo *image_info,
1506 % Image *image,ExceptionInfo *exception)
1508 % A description of each parameter follows.
1510 % o image_info: the image info.
1512 % o image: The image.
1514 % o exception: return any errors or warnings in this structure.
1517 static MagickBooleanType WriteBMPImage(const ImageInfo *image_info,Image *image,
1518 ExceptionInfo *exception)
1539 register const Quantum
1546 register unsigned char
1561 Open output image file.
1563 assert(image_info != (const ImageInfo *) NULL);
1564 assert(image_info->signature == MagickSignature);
1565 assert(image != (Image *) NULL);
1566 assert(image->signature == MagickSignature);
1567 if (image->debug != MagickFalse)
1568 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1569 assert(exception != (ExceptionInfo *) NULL);
1570 assert(exception->signature == MagickSignature);
1571 status=OpenBlob(image_info,image,WriteBinaryBlobMode,exception);
1572 if (status == MagickFalse)
1575 if (LocaleCompare(image_info->magick,"BMP2") == 0)
1578 if (LocaleCompare(image_info->magick,"BMP3") == 0)
1581 value=GetImageOption(image_info,"bmp:format");
1583 if (value != (char *) NULL)
1585 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1586 " Format=%s",value);
1588 if (LocaleCompare(value,"bmp2") == 0)
1590 if (LocaleCompare(value,"bmp3") == 0)
1592 if (LocaleCompare(value,"bmp4") == 0)
1600 Initialize BMP raster file header.
1602 (void) TransformImageColorspace(image,sRGBColorspace,exception);
1603 (void) ResetMagickMemory(&bmp_info,0,sizeof(bmp_info));
1604 bmp_info.file_size=14+12;
1606 bmp_info.file_size+=28;
1607 bmp_info.offset_bits=bmp_info.file_size;
1608 bmp_info.compression=BI_RGB;
1609 if ((image->storage_class == PseudoClass) && (image->colors > 256))
1610 (void) SetImageStorageClass(image,DirectClass,exception);
1611 if (image->storage_class != DirectClass)
1614 Colormapped BMP raster.
1616 bmp_info.bits_per_pixel=8;
1617 if (image->colors <= 2)
1618 bmp_info.bits_per_pixel=1;
1620 if (image->colors <= 16)
1621 bmp_info.bits_per_pixel=4;
1623 if (image->colors <= 256)
1624 bmp_info.bits_per_pixel=8;
1625 if (image_info->compression == RLECompression)
1626 bmp_info.bits_per_pixel=8;
1627 bmp_info.number_colors=1U << bmp_info.bits_per_pixel;
1628 if (image->alpha_trait != UndefinedPixelTrait)
1629 (void) SetImageStorageClass(image,DirectClass,exception);
1631 if ((size_t) bmp_info.number_colors < image->colors)
1632 (void) SetImageStorageClass(image,DirectClass,exception);
1635 bmp_info.file_size+=3*(1UL << bmp_info.bits_per_pixel);
1636 bmp_info.offset_bits+=3*(1UL << bmp_info.bits_per_pixel);
1639 bmp_info.file_size+=(1UL << bmp_info.bits_per_pixel);
1640 bmp_info.offset_bits+=(1UL << bmp_info.bits_per_pixel);
1644 if (image->storage_class == DirectClass)
1647 Full color BMP raster.
1649 bmp_info.number_colors=0;
1650 bmp_info.bits_per_pixel=(unsigned short)
1651 ((type > 3) && (image->alpha_trait != UndefinedPixelTrait) ? 32 : 24);
1652 bmp_info.compression=(unsigned int) ((type > 3) &&
1653 (image->alpha_trait != UndefinedPixelTrait) ? BI_BITFIELDS : BI_RGB);
1655 bytes_per_line=4*((image->columns*bmp_info.bits_per_pixel+31)/32);
1656 bmp_info.ba_offset=0;
1657 profile=GetImageProfile(image,"icc");
1658 have_color_info=(image->rendering_intent != UndefinedIntent) ||
1659 (profile != (StringInfo *) NULL) || (image->gamma != 0.0) ? MagickTrue :
1664 if ((type == 3) || ((image->alpha_trait == UndefinedPixelTrait) &&
1665 (have_color_info == MagickFalse)))
1677 if ((image->rendering_intent != UndefinedIntent) ||
1678 (profile != (StringInfo *) NULL))
1683 bmp_info.file_size+=extra_size;
1684 bmp_info.offset_bits+=extra_size;
1686 bmp_info.width=(ssize_t) image->columns;
1687 bmp_info.height=(ssize_t) image->rows;
1689 bmp_info.image_size=(unsigned int) (bytes_per_line*image->rows);
1690 bmp_info.file_size+=bmp_info.image_size;
1691 bmp_info.x_pixels=75*39;
1692 bmp_info.y_pixels=75*39;
1693 switch (image->units)
1695 case UndefinedResolution:
1696 case PixelsPerInchResolution:
1698 bmp_info.x_pixels=(unsigned int) (100.0*image->resolution.x/2.54);
1699 bmp_info.y_pixels=(unsigned int) (100.0*image->resolution.y/2.54);
1702 case PixelsPerCentimeterResolution:
1704 bmp_info.x_pixels=(unsigned int) (100.0*image->resolution.x);
1705 bmp_info.y_pixels=(unsigned int) (100.0*image->resolution.y);
1709 bmp_info.colors_important=bmp_info.number_colors;
1711 Convert MIFF to BMP raster pixels.
1713 pixel_info=AcquireVirtualMemory((size_t) bmp_info.image_size,
1715 if (pixel_info == (MemoryInfo *) NULL)
1716 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
1717 pixels=(unsigned char *) GetVirtualMemoryBlob(pixel_info);
1718 (void) ResetMagickMemory(pixels,0,(size_t) bmp_info.image_size);
1719 switch (bmp_info.bits_per_pixel)
1728 Convert PseudoClass image to a BMP monochrome image.
1730 for (y=0; y < (ssize_t) image->rows; y++)
1735 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
1736 if (p == (const Quantum *) NULL)
1738 q=pixels+(image->rows-y-1)*bytes_per_line;
1741 for (x=0; x < (ssize_t) image->columns; x++)
1744 byte|=GetPixelIndex(image,p) != 0 ? 0x01 : 0x00;
1748 *q++=(unsigned char) byte;
1752 p+=GetPixelChannels(image);
1756 *q++=(unsigned char) (byte << (8-bit));
1759 offset=(ssize_t) (image->columns+7)/8;
1760 for (x=offset; x < (ssize_t) bytes_per_line; x++)
1762 if (image->previous == (Image *) NULL)
1764 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
1766 if (status == MagickFalse)
1782 Convert PseudoClass image to a BMP monochrome image.
1784 for (y=0; y < (ssize_t) image->rows; y++)
1786 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
1787 if (p == (const Quantum *) NULL)
1789 q=pixels+(image->rows-y-1)*bytes_per_line;
1792 for (x=0; x < (ssize_t) image->columns; x++)
1795 byte|=((size_t) GetPixelIndex(image,p) & 0x0f);
1799 *q++=(unsigned char) byte;
1803 p+=GetPixelChannels(image);
1807 *q++=(unsigned char) (byte << 4);
1810 offset=(ssize_t) (image->columns+1)/2;
1811 for (x=offset; x < (ssize_t) bytes_per_line; x++)
1813 if (image->previous == (Image *) NULL)
1815 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
1817 if (status == MagickFalse)
1826 Convert PseudoClass packet to BMP pixel.
1828 for (y=0; y < (ssize_t) image->rows; y++)
1830 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
1831 if (p == (const Quantum *) NULL)
1833 q=pixels+(image->rows-y-1)*bytes_per_line;
1834 for (x=0; x < (ssize_t) image->columns; x++)
1836 *q++=(unsigned char) GetPixelIndex(image,p);
1837 p+=GetPixelChannels(image);
1839 for ( ; x < (ssize_t) bytes_per_line; x++)
1841 if (image->previous == (Image *) NULL)
1843 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
1845 if (status == MagickFalse)
1854 Convert DirectClass packet to BMP BGR888.
1856 for (y=0; y < (ssize_t) image->rows; y++)
1858 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
1859 if (p == (const Quantum *) NULL)
1861 q=pixels+(image->rows-y-1)*bytes_per_line;
1862 for (x=0; x < (ssize_t) image->columns; x++)
1864 *q++=ScaleQuantumToChar(GetPixelBlue(image,p));
1865 *q++=ScaleQuantumToChar(GetPixelGreen(image,p));
1866 *q++=ScaleQuantumToChar(GetPixelRed(image,p));
1867 p+=GetPixelChannels(image);
1869 for (x=3L*(ssize_t) image->columns; x < (ssize_t) bytes_per_line; x++)
1871 if (image->previous == (Image *) NULL)
1873 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
1875 if (status == MagickFalse)
1884 Convert DirectClass packet to ARGB8888 pixel.
1886 for (y=0; y < (ssize_t) image->rows; y++)
1888 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
1889 if (p == (const Quantum *) NULL)
1891 q=pixels+(image->rows-y-1)*bytes_per_line;
1892 for (x=0; x < (ssize_t) image->columns; x++)
1894 *q++=ScaleQuantumToChar(GetPixelBlue(image,p));
1895 *q++=ScaleQuantumToChar(GetPixelGreen(image,p));
1896 *q++=ScaleQuantumToChar(GetPixelRed(image,p));
1897 *q++=ScaleQuantumToChar(GetPixelAlpha(image,p));
1898 p+=GetPixelChannels(image);
1900 if (image->previous == (Image *) NULL)
1902 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
1904 if (status == MagickFalse)
1911 if ((type > 2) && (bmp_info.bits_per_pixel == 8))
1912 if (image_info->compression != NoCompression)
1918 Convert run-length encoded raster pixels.
1920 rle_info=AcquireVirtualMemory((size_t) (2*(bytes_per_line+2)+2),
1921 (image->rows+2)*sizeof(*pixels));
1922 if (rle_info == (MemoryInfo *) NULL)
1924 pixel_info=RelinquishVirtualMemory(pixel_info);
1925 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
1927 bmp_data=(unsigned char *) GetVirtualMemoryBlob(rle_info);
1928 bmp_info.file_size-=bmp_info.image_size;
1929 bmp_info.image_size=(unsigned int) EncodeImage(image,bytes_per_line,
1931 bmp_info.file_size+=bmp_info.image_size;
1932 pixel_info=RelinquishVirtualMemory(pixel_info);
1933 pixel_info=rle_info;
1935 bmp_info.compression=BI_RLE8;
1938 Write BMP for Windows, all versions, 14-byte header.
1940 if (image->debug != MagickFalse)
1942 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1943 " Writing BMP version %.20g datastream",(double) type);
1944 if (image->storage_class == DirectClass)
1945 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1946 " Storage class=DirectClass");
1948 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1949 " Storage class=PseudoClass");
1950 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1951 " Image depth=%.20g",(double) image->depth);
1952 if (image->alpha_trait != UndefinedPixelTrait)
1953 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1956 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1957 " Matte=MagickFalse");
1958 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1959 " BMP bits_per_pixel=%.20g",(double) bmp_info.bits_per_pixel);
1960 switch ((int) bmp_info.compression)
1964 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1965 " Compression=BI_RGB");
1970 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1971 " Compression=BI_RLE8");
1976 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1977 " Compression=BI_BITFIELDS");
1982 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1983 " Compression=UNKNOWN (%u)",bmp_info.compression);
1987 if (bmp_info.number_colors == 0)
1988 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1989 " Number_colors=unspecified");
1991 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1992 " Number_colors=%u",bmp_info.number_colors);
1994 (void) WriteBlob(image,2,(unsigned char *) "BM");
1995 (void) WriteBlobLSBLong(image,bmp_info.file_size);
1996 (void) WriteBlobLSBLong(image,bmp_info.ba_offset); /* always 0 */
1997 (void) WriteBlobLSBLong(image,bmp_info.offset_bits);
2001 Write 12-byte version 2 bitmap header.
2003 (void) WriteBlobLSBLong(image,bmp_info.size);
2004 (void) WriteBlobLSBShort(image,(unsigned short) bmp_info.width);
2005 (void) WriteBlobLSBShort(image,(unsigned short) bmp_info.height);
2006 (void) WriteBlobLSBShort(image,bmp_info.planes);
2007 (void) WriteBlobLSBShort(image,bmp_info.bits_per_pixel);
2012 Write 40-byte version 3+ bitmap header.
2014 (void) WriteBlobLSBLong(image,bmp_info.size);
2015 (void) WriteBlobLSBLong(image,(unsigned int) bmp_info.width);
2016 (void) WriteBlobLSBLong(image,(unsigned int) bmp_info.height);
2017 (void) WriteBlobLSBShort(image,bmp_info.planes);
2018 (void) WriteBlobLSBShort(image,bmp_info.bits_per_pixel);
2019 (void) WriteBlobLSBLong(image,bmp_info.compression);
2020 (void) WriteBlobLSBLong(image,bmp_info.image_size);
2021 (void) WriteBlobLSBLong(image,bmp_info.x_pixels);
2022 (void) WriteBlobLSBLong(image,bmp_info.y_pixels);
2023 (void) WriteBlobLSBLong(image,bmp_info.number_colors);
2024 (void) WriteBlobLSBLong(image,bmp_info.colors_important);
2026 if ((type > 3) && ((image->alpha_trait != UndefinedPixelTrait) ||
2027 (have_color_info != MagickFalse)))
2030 Write the rest of the 108-byte BMP Version 4 header.
2032 (void) WriteBlobLSBLong(image,0x00ff0000U); /* Red mask */
2033 (void) WriteBlobLSBLong(image,0x0000ff00U); /* Green mask */
2034 (void) WriteBlobLSBLong(image,0x000000ffU); /* Blue mask */
2035 (void) WriteBlobLSBLong(image,0xff000000U); /* Alpha mask */
2036 (void) WriteBlobLSBLong(image,0x73524742U); /* sRGB */
2037 (void) WriteBlobLSBLong(image,(unsigned int)
2038 (image->chromaticity.red_primary.x*0x40000000));
2039 (void) WriteBlobLSBLong(image,(unsigned int)
2040 (image->chromaticity.red_primary.y*0x40000000));
2041 (void) WriteBlobLSBLong(image,(unsigned int)
2042 ((1.000f-(image->chromaticity.red_primary.x+
2043 image->chromaticity.red_primary.y))*0x40000000));
2044 (void) WriteBlobLSBLong(image,(unsigned int)
2045 (image->chromaticity.green_primary.x*0x40000000));
2046 (void) WriteBlobLSBLong(image,(unsigned int)
2047 (image->chromaticity.green_primary.y*0x40000000));
2048 (void) WriteBlobLSBLong(image,(unsigned int)
2049 ((1.000f-(image->chromaticity.green_primary.x+
2050 image->chromaticity.green_primary.y))*0x40000000));
2051 (void) WriteBlobLSBLong(image,(unsigned int)
2052 (image->chromaticity.blue_primary.x*0x40000000));
2053 (void) WriteBlobLSBLong(image,(unsigned int)
2054 (image->chromaticity.blue_primary.y*0x40000000));
2055 (void) WriteBlobLSBLong(image,(unsigned int)
2056 ((1.000f-(image->chromaticity.blue_primary.x+
2057 image->chromaticity.blue_primary.y))*0x40000000));
2058 (void) WriteBlobLSBLong(image,(unsigned int)
2059 (bmp_info.gamma_scale.x*0x10000));
2060 (void) WriteBlobLSBLong(image,(unsigned int)
2061 (bmp_info.gamma_scale.y*0x10000));
2062 (void) WriteBlobLSBLong(image,(unsigned int)
2063 (bmp_info.gamma_scale.z*0x10000));
2064 if ((image->rendering_intent != UndefinedIntent) ||
2065 (profile != (StringInfo *) NULL))
2070 switch ((int) image->rendering_intent)
2072 case SaturationIntent:
2074 intent=LCS_GM_BUSINESS;
2077 case RelativeIntent:
2079 intent=LCS_GM_GRAPHICS;
2082 case PerceptualIntent:
2084 intent=LCS_GM_IMAGES;
2087 case AbsoluteIntent:
2089 intent=LCS_GM_ABS_COLORIMETRIC;
2098 (void) WriteBlobLSBLong(image,(unsigned int) intent);
2099 (void) WriteBlobLSBLong(image,0x00); /* dummy profile data */
2100 (void) WriteBlobLSBLong(image,0x00); /* dummy profile length */
2101 (void) WriteBlobLSBLong(image,0x00); /* reserved */
2104 if (image->storage_class == PseudoClass)
2110 Dump colormap to file.
2112 if (image->debug != MagickFalse)
2113 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
2114 " Colormap: %.20g entries",(double) image->colors);
2115 bmp_colormap=(unsigned char *) AcquireQuantumMemory((size_t) (1UL <<
2116 bmp_info.bits_per_pixel),4*sizeof(*bmp_colormap));
2117 if (bmp_colormap == (unsigned char *) NULL)
2118 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
2120 for (i=0; i < (ssize_t) MagickMin((ssize_t) image->colors,(ssize_t) bmp_info.number_colors); i++)
2122 *q++=ScaleQuantumToChar(ClampToQuantum(image->colormap[i].blue));
2123 *q++=ScaleQuantumToChar(ClampToQuantum(image->colormap[i].green));
2124 *q++=ScaleQuantumToChar(ClampToQuantum(image->colormap[i].red));
2126 *q++=(unsigned char) 0x0;
2128 for ( ; i < (ssize_t) (1UL << bmp_info.bits_per_pixel); i++)
2130 *q++=(unsigned char) 0x00;
2131 *q++=(unsigned char) 0x00;
2132 *q++=(unsigned char) 0x00;
2134 *q++=(unsigned char) 0x00;
2137 (void) WriteBlob(image,(size_t) (3*(1L << bmp_info.bits_per_pixel)),
2140 (void) WriteBlob(image,(size_t) (4*(1L << bmp_info.bits_per_pixel)),
2142 bmp_colormap=(unsigned char *) RelinquishMagickMemory(bmp_colormap);
2144 if (image->debug != MagickFalse)
2145 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
2146 " Pixels: %u bytes",bmp_info.image_size);
2147 (void) WriteBlob(image,(size_t) bmp_info.image_size,pixels);
2148 pixel_info=RelinquishVirtualMemory(pixel_info);
2149 if (GetNextImageInList(image) == (Image *) NULL)
2151 image=SyncNextImageInList(image);
2152 status=SetImageProgress(image,SaveImagesTag,scene++,
2153 GetImageListLength(image));
2154 if (status == MagickFalse)
2156 } while (image_info->adjoin != MagickFalse);
2157 (void) CloseBlob(image);