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.
188 static MagickBooleanType DecodeImage(Image *image,const size_t compression,
189 unsigned char *pixels)
198 register unsigned char
208 assert(image != (Image *) NULL);
209 assert(image->signature == MagickSignature);
210 if (image->debug != MagickFalse)
211 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
212 assert(pixels != (unsigned char *) NULL);
213 (void) ResetMagickMemory(pixels,0,(size_t) image->columns*image->rows*
218 q=pixels+(size_t) image->columns*image->rows;
219 for (y=0; y < (ssize_t) image->rows; )
221 if ((p < pixels) || (p >= q))
223 count=ReadBlobByte(image);
231 count=(int) MagickMin((ssize_t) count,(ssize_t) (q-p));
232 byte=(unsigned char) ReadBlobByte(image);
233 if (compression == BI_RLE8)
235 for (i=0; i < (ssize_t) count; i++)
236 *p++=(unsigned char) byte;
240 for (i=0; i < (ssize_t) count; i++)
242 ((i & 0x01) != 0 ? (byte & 0x0f) : ((byte >> 4) & 0x0f));
251 count=ReadBlobByte(image);
263 p=pixels+y*image->columns;
271 x+=ReadBlobByte(image);
272 y+=ReadBlobByte(image);
273 p=pixels+y*image->columns+x;
281 count=(int) MagickMin((ssize_t) count,(ssize_t) (q-p));
282 if (compression == BI_RLE8)
283 for (i=0; i < (ssize_t) count; i++)
284 *p++=(unsigned char) ReadBlobByte(image);
286 for (i=0; i < (ssize_t) count; i++)
289 byte=(unsigned char) ReadBlobByte(image);
291 ((i & 0x01) != 0 ? (byte & 0x0f) : ((byte >> 4) & 0x0f));
297 if (compression == BI_RLE8)
299 if ((count & 0x01) != 0)
300 (void) ReadBlobByte(image);
303 if (((count & 0x03) == 1) || ((count & 0x03) == 2))
304 (void) ReadBlobByte(image);
309 if (SetImageProgress(image,LoadImageTag,(MagickOffsetType) y,image->rows) == MagickFalse)
312 (void) ReadBlobByte(image); /* end of line */
313 (void) ReadBlobByte(image);
318 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
322 % E n c o d e I m a g e %
326 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
328 % EncodeImage compresses pixels using a runlength encoded format.
330 % The format of the EncodeImage method is:
332 % static MagickBooleanType EncodeImage(Image *image,
333 % const size_t bytes_per_line,const unsigned char *pixels,
334 % unsigned char *compressed_pixels)
336 % A description of each parameter follows:
338 % o image: The image.
340 % o bytes_per_line: the number of bytes in a scanline of compressed pixels
342 % o pixels: The address of a byte (8 bits) array of pixel data created by
343 % the compression process.
345 % o compressed_pixels: The address of a byte (8 bits) array of compressed
349 static size_t EncodeImage(Image *image,const size_t bytes_per_line,
350 const unsigned char *pixels,unsigned char *compressed_pixels)
355 register const unsigned char
362 register unsigned char
369 Runlength encode pixels.
371 assert(image != (Image *) NULL);
372 assert(image->signature == MagickSignature);
373 if (image->debug != MagickFalse)
374 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
375 assert(pixels != (const unsigned char *) NULL);
376 assert(compressed_pixels != (unsigned char *) NULL);
380 for (y=0; y < (ssize_t) image->rows; y++)
382 for (x=0; x < (ssize_t) bytes_per_line; x+=i)
387 for (i=1; ((x+i) < (ssize_t) bytes_per_line); i++)
388 if ((i == 255) || (*(p+i) != *p))
390 *q++=(unsigned char) i;
397 *q++=(unsigned char) 0x00;
398 *q++=(unsigned char) 0x00;
399 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
401 if (status == MagickFalse)
407 *q++=(unsigned char) 0x00;
408 *q++=(unsigned char) 0x01;
409 return((size_t) (q-compressed_pixels));
413 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
421 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
423 % IsBMP() returns MagickTrue if the image format type, identified by the
424 % magick string, is BMP.
426 % The format of the IsBMP method is:
428 % MagickBooleanType IsBMP(const unsigned char *magick,const size_t length)
430 % A description of each parameter follows:
432 % o magick: compare image format pattern against these bytes.
434 % o length: Specifies the length of the magick string.
437 static MagickBooleanType IsBMP(const unsigned char *magick,const size_t length)
441 if ((LocaleNCompare((char *) magick,"BA",2) == 0) ||
442 (LocaleNCompare((char *) magick,"BM",2) == 0) ||
443 (LocaleNCompare((char *) magick,"IC",2) == 0) ||
444 (LocaleNCompare((char *) magick,"PI",2) == 0) ||
445 (LocaleNCompare((char *) magick,"CI",2) == 0) ||
446 (LocaleNCompare((char *) magick,"CP",2) == 0))
452 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
456 % R e a d B M P I m a g e %
460 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
462 % ReadBMPImage() reads a Microsoft Windows bitmap image file, Version
463 % 2, 3 (for Windows or NT), or 4, and returns it. It allocates the memory
464 % necessary for the new Image structure and returns a pointer to the new
467 % The format of the ReadBMPImage method is:
469 % image=ReadBMPImage(image_info)
471 % A description of each parameter follows:
473 % o image_info: the image info.
475 % o exception: return any errors or warnings in this structure.
479 static Image *ReadBMPImage(const ImageInfo *image_info,ExceptionInfo *exception)
507 register unsigned char
529 assert(image_info != (const ImageInfo *) NULL);
530 assert(image_info->signature == MagickSignature);
531 if (image_info->debug != MagickFalse)
532 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
533 image_info->filename);
534 assert(exception != (ExceptionInfo *) NULL);
535 assert(exception->signature == MagickSignature);
536 image=AcquireImage(image_info,exception);
537 status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception);
538 if (status == MagickFalse)
540 image=DestroyImageList(image);
541 return((Image *) NULL);
544 Determine if this a BMP file.
546 (void) ResetMagickMemory(&bmp_info,0,sizeof(bmp_info));
547 bmp_info.ba_offset=0;
549 count=ReadBlob(image,2,magick);
551 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
565 Verify BMP identifier.
567 if (bmp_info.ba_offset == 0)
568 start_position=TellBlob(image)-2;
569 bmp_info.ba_offset=0;
570 while (LocaleNCompare((char *) magick,"BA",2) == 0)
572 bmp_info.file_size=ReadBlobLSBLong(image);
573 bmp_info.ba_offset=ReadBlobLSBLong(image);
574 bmp_info.offset_bits=ReadBlobLSBLong(image);
575 count=ReadBlob(image,2,magick);
579 if (image->debug != MagickFalse)
580 (void) LogMagickEvent(CoderEvent,GetMagickModule()," Magick: %c%c",
581 magick[0],magick[1]);
582 if ((count != 2) || ((LocaleNCompare((char *) magick,"BM",2) != 0) &&
583 (LocaleNCompare((char *) magick,"CI",2) != 0)))
584 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
585 bmp_info.file_size=ReadBlobLSBLong(image);
586 (void) ReadBlobLSBLong(image);
587 bmp_info.offset_bits=ReadBlobLSBLong(image);
588 bmp_info.size=ReadBlobLSBLong(image);
589 if (image->debug != MagickFalse)
590 (void) LogMagickEvent(CoderEvent,GetMagickModule()," BMP size: %u",
592 if (bmp_info.size == 12)
597 (void) CopyMagickString(image->magick,"BMP2",MaxTextExtent);
598 bmp_info.width=(ssize_t) ((short) ReadBlobLSBShort(image));
599 bmp_info.height=(ssize_t) ((short) ReadBlobLSBShort(image));
600 bmp_info.planes=ReadBlobLSBShort(image);
601 bmp_info.bits_per_pixel=ReadBlobLSBShort(image);
604 bmp_info.number_colors=0;
605 bmp_info.compression=BI_RGB;
606 bmp_info.image_size=0;
607 bmp_info.alpha_mask=0;
608 if (image->debug != MagickFalse)
610 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
611 " Format: OS/2 Bitmap");
612 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
613 " Geometry: %.20gx%.20g",(double) bmp_info.width,(double)
620 Microsoft Windows BMP image file.
622 if (bmp_info.size < 40)
623 ThrowReaderException(CorruptImageError,"NonOS2HeaderSizeError");
624 bmp_info.width=(ssize_t) ((int) ReadBlobLSBLong(image));
625 bmp_info.height=(ssize_t) ((int) ReadBlobLSBLong(image));
626 bmp_info.planes=ReadBlobLSBShort(image);
627 bmp_info.bits_per_pixel=ReadBlobLSBShort(image);
628 bmp_info.compression=ReadBlobLSBLong(image);
629 bmp_info.image_size=ReadBlobLSBLong(image);
630 bmp_info.x_pixels=ReadBlobLSBLong(image);
631 bmp_info.y_pixels=ReadBlobLSBLong(image);
632 bmp_info.number_colors=ReadBlobLSBLong(image);
633 bmp_info.colors_important=ReadBlobLSBLong(image);
636 if (image->debug != MagickFalse)
638 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
639 " Format: MS Windows bitmap");
640 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
641 " Geometry: %.20gx%.20g",(double) bmp_info.width,(double)
643 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
644 " Bits per pixel: %.20g",(double) bmp_info.bits_per_pixel);
645 switch ((int) bmp_info.compression)
649 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
650 " Compression: BI_RGB");
655 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
656 " Compression: BI_RLE4");
661 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
662 " Compression: BI_RLE8");
667 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
668 " Compression: BI_BITFIELDS");
673 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
674 " Compression: BI_PNG");
679 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
680 " Compression: BI_JPEG");
685 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
686 " Compression: UNKNOWN (%u)",bmp_info.compression);
689 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
690 " Number of colors: %u",bmp_info.number_colors);
692 bmp_info.red_mask=ReadBlobLSBLong(image);
693 bmp_info.green_mask=ReadBlobLSBLong(image);
694 bmp_info.blue_mask=ReadBlobLSBLong(image);
695 if (bmp_info.size > 40)
702 Read color management information.
704 bmp_info.alpha_mask=ReadBlobLSBLong(image);
705 bmp_info.colorspace=(int) ReadBlobLSBLong(image);
707 Decode 2^30 fixed point formatted CIE primaries.
709 # define BMP_DENOM ((double) 0x40000000)
710 bmp_info.red_primary.x=(double) ReadBlobLSBLong(image)/BMP_DENOM;
711 bmp_info.red_primary.y=(double) ReadBlobLSBLong(image)/BMP_DENOM;
712 bmp_info.red_primary.z=(double) ReadBlobLSBLong(image)/BMP_DENOM;
713 bmp_info.green_primary.x=(double) ReadBlobLSBLong(image)/BMP_DENOM;
714 bmp_info.green_primary.y=(double) ReadBlobLSBLong(image)/BMP_DENOM;
715 bmp_info.green_primary.z=(double) ReadBlobLSBLong(image)/BMP_DENOM;
716 bmp_info.blue_primary.x=(double) ReadBlobLSBLong(image)/BMP_DENOM;
717 bmp_info.blue_primary.y=(double) ReadBlobLSBLong(image)/BMP_DENOM;
718 bmp_info.blue_primary.z=(double) ReadBlobLSBLong(image)/BMP_DENOM;
720 sum=bmp_info.red_primary.x+bmp_info.red_primary.y+
721 bmp_info.red_primary.z;
722 bmp_info.red_primary.x/=sum;
723 bmp_info.red_primary.y/=sum;
724 image->chromaticity.red_primary.x=bmp_info.red_primary.x;
725 image->chromaticity.red_primary.y=bmp_info.red_primary.y;
727 sum=bmp_info.green_primary.x+bmp_info.green_primary.y+
728 bmp_info.green_primary.z;
729 bmp_info.green_primary.x/=sum;
730 bmp_info.green_primary.y/=sum;
731 image->chromaticity.green_primary.x=bmp_info.green_primary.x;
732 image->chromaticity.green_primary.y=bmp_info.green_primary.y;
734 sum=bmp_info.blue_primary.x+bmp_info.blue_primary.y+
735 bmp_info.blue_primary.z;
736 bmp_info.blue_primary.x/=sum;
737 bmp_info.blue_primary.y/=sum;
738 image->chromaticity.blue_primary.x=bmp_info.blue_primary.x;
739 image->chromaticity.blue_primary.y=bmp_info.blue_primary.y;
742 Decode 16^16 fixed point formatted gamma_scales.
744 bmp_info.gamma_scale.x=(double) ReadBlobLSBLong(image)/0x10000;
745 bmp_info.gamma_scale.y=(double) ReadBlobLSBLong(image)/0x10000;
746 bmp_info.gamma_scale.z=(double) ReadBlobLSBLong(image)/0x10000;
748 Compute a single gamma from the BMP 3-channel gamma.
750 image->gamma=(bmp_info.gamma_scale.x+bmp_info.gamma_scale.y+
751 bmp_info.gamma_scale.z)/3.0;
754 (void) CopyMagickString(image->magick,"BMP3",MaxTextExtent);
756 if (bmp_info.size > 108)
762 Read BMP Version 5 color management information.
764 intent=ReadBlobLSBLong(image);
765 switch ((int) intent)
767 case LCS_GM_BUSINESS:
769 image->rendering_intent=SaturationIntent;
772 case LCS_GM_GRAPHICS:
774 image->rendering_intent=RelativeIntent;
779 image->rendering_intent=PerceptualIntent;
782 case LCS_GM_ABS_COLORIMETRIC:
784 image->rendering_intent=AbsoluteIntent;
788 profile_data=ReadBlobLSBLong(image);
789 profile_size=ReadBlobLSBLong(image);
792 (void) ReadBlobLSBLong(image); /* Reserved byte */
795 if ((MagickSizeType) bmp_info.file_size > GetBlobSize(image))
796 (void) ThrowMagickException(exception,GetMagickModule(),CorruptImageError,
797 "LengthAndFilesizeDoNotMatch","`%s'",image->filename);
799 if ((MagickSizeType) bmp_info.file_size < GetBlobSize(image))
800 (void) ThrowMagickException(exception,GetMagickModule(),
801 CorruptImageWarning,"LengthAndFilesizeDoNotMatch","`%s'",
803 if (bmp_info.width <= 0)
804 ThrowReaderException(CorruptImageError,"NegativeOrZeroImageSize");
805 if (bmp_info.height == 0)
806 ThrowReaderException(CorruptImageError,"NegativeOrZeroImageSize");
807 if (bmp_info.planes != 1)
808 ThrowReaderException(CorruptImageError,"StaticPlanesValueNotEqualToOne");
809 if ((bmp_info.bits_per_pixel != 1) && (bmp_info.bits_per_pixel != 4) &&
810 (bmp_info.bits_per_pixel != 8) && (bmp_info.bits_per_pixel != 16) &&
811 (bmp_info.bits_per_pixel != 24) && (bmp_info.bits_per_pixel != 32))
812 ThrowReaderException(CorruptImageError,"UnrecognizedBitsPerPixel");
813 if (bmp_info.bits_per_pixel < 16 &&
814 bmp_info.number_colors > (1U << bmp_info.bits_per_pixel))
815 ThrowReaderException(CorruptImageError,"UnrecognizedNumberOfColors");
816 if ((bmp_info.compression == 1) && (bmp_info.bits_per_pixel != 8))
817 ThrowReaderException(CorruptImageError,"UnrecognizedBitsPerPixel");
818 if ((bmp_info.compression == 2) && (bmp_info.bits_per_pixel != 4))
819 ThrowReaderException(CorruptImageError,"UnrecognizedBitsPerPixel");
820 if ((bmp_info.compression == 3) && (bmp_info.bits_per_pixel < 16))
821 ThrowReaderException(CorruptImageError,"UnrecognizedBitsPerPixel");
822 switch (bmp_info.compression)
830 ThrowReaderException(CoderError,"JPEGCompressNotSupported");
832 ThrowReaderException(CoderError,"PNGCompressNotSupported");
834 ThrowReaderException(CorruptImageError,"UnrecognizedImageCompression");
836 image->columns=(size_t) MagickAbsoluteValue(bmp_info.width);
837 image->rows=(size_t) MagickAbsoluteValue(bmp_info.height);
838 image->depth=bmp_info.bits_per_pixel <= 8 ? bmp_info.bits_per_pixel : 8;
839 image->alpha_trait=((bmp_info.alpha_mask != 0) &&
840 (bmp_info.compression == BI_BITFIELDS)) ? BlendPixelTrait :
842 if (bmp_info.bits_per_pixel < 16)
847 image->storage_class=PseudoClass;
848 image->colors=bmp_info.number_colors;
850 if (image->colors == 0)
851 image->colors=one << bmp_info.bits_per_pixel;
853 if (image->storage_class == PseudoClass)
862 Read BMP raster colormap.
864 if (image->debug != MagickFalse)
865 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
866 " Reading colormap of %.20g colors",(double) image->colors);
867 if (AcquireImageColormap(image,image->colors,exception) == MagickFalse)
868 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
869 bmp_colormap=(unsigned char *) AcquireQuantumMemory((size_t)
870 image->colors,4*sizeof(*bmp_colormap));
871 if (bmp_colormap == (unsigned char *) NULL)
872 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
873 if ((bmp_info.size == 12) || (bmp_info.size == 64))
877 offset=SeekBlob(image,start_position+14+bmp_info.size,SEEK_SET);
879 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
880 count=ReadBlob(image,packet_size*image->colors,bmp_colormap);
881 if (count != (ssize_t) (packet_size*image->colors))
882 ThrowReaderException(CorruptImageError,"InsufficientImageDataInFile");
884 for (i=0; i < (ssize_t) image->colors; i++)
886 image->colormap[i].blue=(MagickRealType) ScaleCharToQuantum(*p++);
887 image->colormap[i].green=(MagickRealType) ScaleCharToQuantum(*p++);
888 image->colormap[i].red=(MagickRealType) ScaleCharToQuantum(*p++);
889 if (packet_size == 4)
892 bmp_colormap=(unsigned char *) RelinquishMagickMemory(bmp_colormap);
894 image->resolution.x=(double) bmp_info.x_pixels/100.0;
895 image->resolution.y=(double) bmp_info.y_pixels/100.0;
896 image->units=PixelsPerCentimeterResolution;
897 if ((image_info->ping != MagickFalse) && (image_info->number_scenes != 0))
898 if (image->scene >= (image_info->scene+image_info->number_scenes-1))
900 status=SetImageExtent(image,image->columns,image->rows,exception);
901 if (status == MagickFalse)
902 return(DestroyImageList(image));
906 offset=SeekBlob(image,start_position+bmp_info.offset_bits,SEEK_SET);
908 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
909 if (bmp_info.compression == BI_RLE4)
910 bmp_info.bits_per_pixel<<=1;
911 bytes_per_line=4*((image->columns*bmp_info.bits_per_pixel+31)/32);
912 length=(size_t) bytes_per_line*image->rows;
913 pixel_info=AcquireVirtualMemory((size_t) image->rows,
914 MagickMax(bytes_per_line,image->columns+256UL)*sizeof(*pixels));
915 if (pixel_info == (MemoryInfo *) NULL)
916 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
917 pixels=(unsigned char *) GetVirtualMemoryBlob(pixel_info);
918 if ((bmp_info.compression == BI_RGB) ||
919 (bmp_info.compression == BI_BITFIELDS))
921 if (image->debug != MagickFalse)
922 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
923 " Reading pixels (%.20g bytes)",(double) length);
924 count=ReadBlob(image,length,pixels);
925 if (count != (ssize_t) length)
927 pixel_info=RelinquishVirtualMemory(pixel_info);
928 ThrowReaderException(CorruptImageError,
929 "InsufficientImageDataInFile");
935 Convert run-length encoded raster pixels.
937 status=DecodeImage(image,bmp_info.compression,pixels);
938 if (status == MagickFalse)
940 pixel_info=RelinquishVirtualMemory(pixel_info);
941 ThrowReaderException(CorruptImageError,
942 "UnableToRunlengthDecodeImage");
946 Convert BMP raster image to pixel packets.
948 if (bmp_info.compression == BI_RGB)
951 We should ignore the alpha value in BMP3 files but there have been
952 reports about 32 bit files with alpha. We do a quick check to see if
953 the alpha channel contains a value that is not zero (default value).
954 If we find a non zero value we asume the program that wrote the file
955 wants to use the alpha channel.
957 if ((image->alpha_trait == UndefinedPixelTrait) && (bmp_info.size == 40) &&
958 (bmp_info.bits_per_pixel == 32))
960 bytes_per_line=4*(image->columns);
961 for (y=(ssize_t) image->rows-1; y >= 0; y--)
963 p=pixels+(image->rows-y-1)*bytes_per_line;
964 for (x=0; x < (ssize_t) image->columns; x++)
968 image->alpha_trait=BlendPixelTrait;
976 bmp_info.alpha_mask=image->alpha_trait != UndefinedPixelTrait ?
978 bmp_info.red_mask=0x00ff0000U;
979 bmp_info.green_mask=0x0000ff00U;
980 bmp_info.blue_mask=0x000000ffU;
981 if (bmp_info.bits_per_pixel == 16)
986 bmp_info.red_mask=0x00007c00U;
987 bmp_info.green_mask=0x000003e0U;
988 bmp_info.blue_mask=0x0000001fU;
991 (void) ResetMagickMemory(&shift,0,sizeof(shift));
992 (void) ResetMagickMemory(&quantum_bits,0,sizeof(quantum_bits));
993 if ((bmp_info.bits_per_pixel == 16) || (bmp_info.bits_per_pixel == 32))
999 Get shift and quantum bits info from bitfield masks.
1001 if (bmp_info.red_mask != 0)
1002 while (((bmp_info.red_mask << shift.red) & 0x80000000UL) == 0)
1004 if (bmp_info.green_mask != 0)
1005 while (((bmp_info.green_mask << shift.green) & 0x80000000UL) == 0)
1007 if (bmp_info.blue_mask != 0)
1008 while (((bmp_info.blue_mask << shift.blue) & 0x80000000UL) == 0)
1010 if (bmp_info.alpha_mask != 0)
1011 while (((bmp_info.alpha_mask << shift.alpha) & 0x80000000UL) == 0)
1014 while (((bmp_info.red_mask << sample) & 0x80000000UL) != 0)
1016 quantum_bits.red=(MagickRealType) (sample-shift.red);
1018 while (((bmp_info.green_mask << sample) & 0x80000000UL) != 0)
1020 quantum_bits.green=(MagickRealType) (sample-shift.green);
1022 while (((bmp_info.blue_mask << sample) & 0x80000000UL) != 0)
1024 quantum_bits.blue=(MagickRealType) (sample-shift.blue);
1026 while (((bmp_info.alpha_mask << sample) & 0x80000000UL) != 0)
1028 quantum_bits.alpha=(MagickRealType) (sample-shift.alpha);
1030 switch (bmp_info.bits_per_pixel)
1035 Convert bitmap scanline.
1037 for (y=(ssize_t) image->rows-1; y >= 0; y--)
1039 p=pixels+(image->rows-y-1)*bytes_per_line;
1040 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
1041 if (q == (Quantum *) NULL)
1043 for (x=0; x < ((ssize_t) image->columns-7); x+=8)
1045 for (bit=0; bit < 8; bit++)
1047 index=(Quantum) (((*p) & (0x80 >> bit)) != 0 ? 0x01 : 0x00);
1048 SetPixelIndex(image,index,q);
1049 q+=GetPixelChannels(image);
1053 if ((image->columns % 8) != 0)
1055 for (bit=0; bit < (image->columns % 8); bit++)
1057 index=(Quantum) (((*p) & (0x80 >> bit)) != 0 ? 0x01 : 0x00);
1058 SetPixelIndex(image,index,q);
1059 q+=GetPixelChannels(image);
1063 if (SyncAuthenticPixels(image,exception) == MagickFalse)
1065 if (image->previous == (Image *) NULL)
1067 status=SetImageProgress(image,LoadImageTag,(MagickOffsetType)
1068 (image->rows-y),image->rows);
1069 if (status == MagickFalse)
1073 (void) SyncImage(image,exception);
1079 Convert PseudoColor scanline.
1081 for (y=(ssize_t) image->rows-1; y >= 0; y--)
1083 p=pixels+(image->rows-y-1)*bytes_per_line;
1084 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
1085 if (q == (Quantum *) NULL)
1087 for (x=0; x < ((ssize_t) image->columns-1); x+=2)
1089 if (IsValidColormapIndex(image,(*p >> 4) & 0x0f,&index,exception)
1092 SetPixelIndex(image,index,q);
1093 q+=GetPixelChannels(image);
1094 if (IsValidColormapIndex(image,*p & 0x0f,&index,exception) ==
1097 SetPixelIndex(image,index,q);
1098 q+=GetPixelChannels(image);
1101 if ((image->columns % 2) != 0)
1103 if (IsValidColormapIndex(image,(*p >> 4) & 0xf,&index,exception)
1106 SetPixelIndex(image,index,q);
1107 q+=GetPixelChannels(image);
1110 if (x < image->columns)
1112 if (SyncAuthenticPixels(image,exception) == MagickFalse)
1114 if (image->previous == (Image *) NULL)
1116 status=SetImageProgress(image,LoadImageTag,(MagickOffsetType)
1117 (image->rows-y),image->rows);
1118 if (status == MagickFalse)
1122 (void) SyncImage(image,exception);
1128 Convert PseudoColor scanline.
1130 if ((bmp_info.compression == BI_RLE8) ||
1131 (bmp_info.compression == BI_RLE4))
1132 bytes_per_line=image->columns;
1133 for (y=(ssize_t) image->rows-1; y >= 0; y--)
1135 p=pixels+(image->rows-y-1)*bytes_per_line;
1136 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
1137 if (q == (Quantum *) NULL)
1139 for (x=(ssize_t) image->columns; x != 0; --x)
1141 if (IsValidColormapIndex(image,*p++,&index,exception) ==
1144 SetPixelIndex(image,index,q);
1145 q+=GetPixelChannels(image);
1149 if (SyncAuthenticPixels(image,exception) == MagickFalse)
1151 offset=(MagickOffsetType) (image->rows-y-1);
1152 if (image->previous == (Image *) NULL)
1154 status=SetImageProgress(image,LoadImageTag,(MagickOffsetType)
1155 (image->rows-y),image->rows);
1156 if (status == MagickFalse)
1160 (void) SyncImage(image,exception);
1170 Convert bitfield encoded 16-bit PseudoColor scanline.
1172 if (bmp_info.compression != BI_RGB &&
1173 bmp_info.compression != BI_BITFIELDS)
1175 pixel_info=RelinquishVirtualMemory(pixel_info);
1176 ThrowReaderException(CorruptImageError,
1177 "UnrecognizedImageCompression");
1179 bytes_per_line=2*(image->columns+image->columns % 2);
1180 image->storage_class=DirectClass;
1181 for (y=(ssize_t) image->rows-1; y >= 0; y--)
1183 p=pixels+(image->rows-y-1)*bytes_per_line;
1184 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
1185 if (q == (Quantum *) NULL)
1187 for (x=0; x < (ssize_t) image->columns; x++)
1189 pixel=(size_t) (*p++);
1191 red=((pixel & bmp_info.red_mask) << shift.red) >> 16;
1192 if (quantum_bits.red == 5)
1193 red|=((red & 0xe000) >> 5);
1194 if (quantum_bits.red <= 8)
1195 red|=((red & 0xff00) >> 8);
1196 green=((pixel & bmp_info.green_mask) << shift.green) >> 16;
1197 if (quantum_bits.green == 5)
1198 green|=((green & 0xe000) >> 5);
1199 if (quantum_bits.green == 6)
1200 green|=((green & 0xc000) >> 6);
1201 if (quantum_bits.green <= 8)
1202 green|=((green & 0xff00) >> 8);
1203 blue=((pixel & bmp_info.blue_mask) << shift.blue) >> 16;
1204 if (quantum_bits.blue == 5)
1205 blue|=((blue & 0xe000) >> 5);
1206 if (quantum_bits.blue <= 8)
1207 blue|=((blue & 0xff00) >> 8);
1208 SetPixelRed(image,ScaleShortToQuantum((unsigned short) red),q);
1209 SetPixelGreen(image,ScaleShortToQuantum((unsigned short) green),q);
1210 SetPixelBlue(image,ScaleShortToQuantum((unsigned short) blue),q);
1211 SetPixelAlpha(image,OpaqueAlpha,q);
1212 if (image->alpha_trait != UndefinedPixelTrait)
1214 alpha=((pixel & bmp_info.alpha_mask) << shift.alpha) >> 16;
1215 if (quantum_bits.alpha <= 8)
1216 alpha|=((alpha & 0xff00) >> 8);
1217 SetPixelAlpha(image,ScaleShortToQuantum(
1218 (unsigned short) alpha),q);
1220 q+=GetPixelChannels(image);
1222 if (SyncAuthenticPixels(image,exception) == MagickFalse)
1224 offset=(MagickOffsetType) (image->rows-y-1);
1225 if (image->previous == (Image *) NULL)
1227 status=SetImageProgress(image,LoadImageTag,(MagickOffsetType)
1228 (image->rows-y),image->rows);
1229 if (status == MagickFalse)
1238 Convert DirectColor scanline.
1240 bytes_per_line=4*((image->columns*24+31)/32);
1241 for (y=(ssize_t) image->rows-1; y >= 0; y--)
1243 p=pixels+(image->rows-y-1)*bytes_per_line;
1244 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
1245 if (q == (Quantum *) NULL)
1247 for (x=0; x < (ssize_t) image->columns; x++)
1249 SetPixelBlue(image,ScaleCharToQuantum(*p++),q);
1250 SetPixelGreen(image,ScaleCharToQuantum(*p++),q);
1251 SetPixelRed(image,ScaleCharToQuantum(*p++),q);
1252 SetPixelAlpha(image,OpaqueAlpha,q);
1253 q+=GetPixelChannels(image);
1255 if (SyncAuthenticPixels(image,exception) == MagickFalse)
1257 offset=(MagickOffsetType) (image->rows-y-1);
1258 if (image->previous == (Image *) NULL)
1260 status=SetImageProgress(image,LoadImageTag,(MagickOffsetType)
1261 (image->rows-y),image->rows);
1262 if (status == MagickFalse)
1271 Convert bitfield encoded DirectColor scanline.
1273 if ((bmp_info.compression != BI_RGB) &&
1274 (bmp_info.compression != BI_BITFIELDS))
1276 pixel_info=RelinquishVirtualMemory(pixel_info);
1277 ThrowReaderException(CorruptImageError,
1278 "UnrecognizedImageCompression");
1280 bytes_per_line=4*(image->columns);
1281 for (y=(ssize_t) image->rows-1; y >= 0; y--)
1287 p=pixels+(image->rows-y-1)*bytes_per_line;
1288 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
1289 if (q == (Quantum *) NULL)
1291 for (x=0; x < (ssize_t) image->columns; x++)
1293 pixel=(size_t) (*p++);
1294 pixel|=((size_t) *p++ << 8);
1295 pixel|=((size_t) *p++ << 16);
1296 pixel|=((size_t) *p++ << 24);
1297 red=((pixel & bmp_info.red_mask) << shift.red) >> 16;
1298 if (quantum_bits.red == 8)
1300 green=((pixel & bmp_info.green_mask) << shift.green) >> 16;
1301 if (quantum_bits.green == 8)
1302 green|=(green >> 8);
1303 blue=((pixel & bmp_info.blue_mask) << shift.blue) >> 16;
1304 if (quantum_bits.blue == 8)
1306 SetPixelRed(image,ScaleShortToQuantum((unsigned short) red),q);
1307 SetPixelGreen(image,ScaleShortToQuantum((unsigned short) green),q);
1308 SetPixelBlue(image,ScaleShortToQuantum((unsigned short) blue),q);
1309 SetPixelAlpha(image,OpaqueAlpha,q);
1310 if (image->alpha_trait != UndefinedPixelTrait)
1312 alpha=((pixel & bmp_info.alpha_mask) << shift.alpha) >> 16;
1313 if (quantum_bits.alpha == 8)
1314 alpha|=(alpha >> 8);
1315 SetPixelAlpha(image,ScaleShortToQuantum(
1316 (unsigned short) alpha),q);
1318 q+=GetPixelChannels(image);
1320 if (SyncAuthenticPixels(image,exception) == MagickFalse)
1322 offset=(MagickOffsetType) (image->rows-y-1);
1323 if (image->previous == (Image *) NULL)
1325 status=SetImageProgress(image,LoadImageTag,(MagickOffsetType)
1326 (image->rows-y),image->rows);
1327 if (status == MagickFalse)
1335 pixel_info=RelinquishVirtualMemory(pixel_info);
1336 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
1339 pixel_info=RelinquishVirtualMemory(pixel_info);
1342 if (EOFBlob(image) != MagickFalse)
1344 ThrowFileException(exception,CorruptImageError,"UnexpectedEndOfFile",
1348 if (bmp_info.height < 0)
1354 Correct image orientation.
1356 flipped_image=FlipImage(image,exception);
1357 if (flipped_image != (Image *) NULL)
1359 DuplicateBlob(flipped_image,image);
1360 image=DestroyImage(image);
1361 image=flipped_image;
1365 Proceed to next image.
1367 if (image_info->number_scenes != 0)
1368 if (image->scene >= (image_info->scene+image_info->number_scenes-1))
1371 if (bmp_info.ba_offset != 0)
1373 offset=SeekBlob(image,(MagickOffsetType) bmp_info.ba_offset,SEEK_SET);
1375 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
1377 count=ReadBlob(image,2,magick);
1378 if ((count == 2) && (IsBMP(magick,2) != MagickFalse))
1381 Acquire next image structure.
1383 AcquireNextImage(image_info,image,exception);
1384 if (GetNextImageInList(image) == (Image *) NULL)
1386 image=DestroyImageList(image);
1387 return((Image *) NULL);
1389 image=SyncNextImageInList(image);
1390 status=SetImageProgress(image,LoadImagesTag,TellBlob(image),
1391 GetBlobSize(image));
1392 if (status == MagickFalse)
1395 } while (IsBMP(magick,2) != MagickFalse);
1396 (void) CloseBlob(image);
1397 return(GetFirstImageInList(image));
1401 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1405 % R e g i s t e r B M P I m a g e %
1409 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1411 % RegisterBMPImage() adds attributes for the BMP image format to
1412 % the list of supported formats. The attributes include the image format
1413 % tag, a method to read and/or write the format, whether the format
1414 % supports the saving of more than one frame to the same file or blob,
1415 % whether the format supports native in-memory I/O, and a brief
1416 % description of the format.
1418 % The format of the RegisterBMPImage method is:
1420 % size_t RegisterBMPImage(void)
1423 ModuleExport size_t RegisterBMPImage(void)
1428 entry=SetMagickInfo("BMP");
1429 entry->decoder=(DecodeImageHandler *) ReadBMPImage;
1430 entry->encoder=(EncodeImageHandler *) WriteBMPImage;
1431 entry->magick=(IsImageFormatHandler *) IsBMP;
1432 entry->description=ConstantString("Microsoft Windows bitmap image");
1433 entry->module=ConstantString("BMP");
1434 entry->flags^=CoderAdjoinFlag;
1435 entry->flags|=CoderSeekableStreamFlag;
1436 (void) RegisterMagickInfo(entry);
1437 entry=SetMagickInfo("BMP2");
1438 entry->encoder=(EncodeImageHandler *) WriteBMPImage;
1439 entry->magick=(IsImageFormatHandler *) IsBMP;
1440 entry->description=ConstantString("Microsoft Windows bitmap image (V2)");
1441 entry->module=ConstantString("BMP");
1442 entry->flags^=CoderAdjoinFlag;
1443 entry->flags|=CoderSeekableStreamFlag;
1444 (void) RegisterMagickInfo(entry);
1445 entry=SetMagickInfo("BMP3");
1446 entry->encoder=(EncodeImageHandler *) WriteBMPImage;
1447 entry->magick=(IsImageFormatHandler *) IsBMP;
1448 entry->description=ConstantString("Microsoft Windows bitmap image (V3)");
1449 entry->module=ConstantString("BMP");
1450 entry->flags^=CoderAdjoinFlag;
1451 entry->flags|=CoderSeekableStreamFlag;
1452 (void) RegisterMagickInfo(entry);
1453 return(MagickImageCoderSignature);
1457 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1461 % U n r e g i s t e r B M P I m a g e %
1465 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1467 % UnregisterBMPImage() removes format registrations made by the
1468 % BMP module from the list of supported formats.
1470 % The format of the UnregisterBMPImage method is:
1472 % UnregisterBMPImage(void)
1475 ModuleExport void UnregisterBMPImage(void)
1477 (void) UnregisterMagickInfo("BMP");
1478 (void) UnregisterMagickInfo("BMP2");
1479 (void) UnregisterMagickInfo("BMP3");
1483 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1487 % W r i t e B M P I m a g e %
1491 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1493 % WriteBMPImage() writes an image in Microsoft Windows bitmap encoded
1494 % image format, version 3 for Windows or (if the image has a matte channel)
1497 % The format of the WriteBMPImage method is:
1499 % MagickBooleanType WriteBMPImage(const ImageInfo *image_info,
1500 % Image *image,ExceptionInfo *exception)
1502 % A description of each parameter follows.
1504 % o image_info: the image info.
1506 % o image: The image.
1508 % o exception: return any errors or warnings in this structure.
1511 static MagickBooleanType WriteBMPImage(const ImageInfo *image_info,Image *image,
1512 ExceptionInfo *exception)
1533 register const Quantum
1540 register unsigned char
1555 Open output image file.
1557 assert(image_info != (const ImageInfo *) NULL);
1558 assert(image_info->signature == MagickSignature);
1559 assert(image != (Image *) NULL);
1560 assert(image->signature == MagickSignature);
1561 if (image->debug != MagickFalse)
1562 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1563 assert(exception != (ExceptionInfo *) NULL);
1564 assert(exception->signature == MagickSignature);
1565 status=OpenBlob(image_info,image,WriteBinaryBlobMode,exception);
1566 if (status == MagickFalse)
1569 if (LocaleCompare(image_info->magick,"BMP2") == 0)
1572 if (LocaleCompare(image_info->magick,"BMP3") == 0)
1575 value=GetImageOption(image_info,"bmp:format");
1577 if (value != (char *) NULL)
1579 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1580 " Format=%s",value);
1582 if (LocaleCompare(value,"bmp2") == 0)
1584 if (LocaleCompare(value,"bmp3") == 0)
1586 if (LocaleCompare(value,"bmp4") == 0)
1594 Initialize BMP raster file header.
1596 (void) TransformImageColorspace(image,sRGBColorspace,exception);
1597 (void) ResetMagickMemory(&bmp_info,0,sizeof(bmp_info));
1598 bmp_info.file_size=14+12;
1600 bmp_info.file_size+=28;
1601 bmp_info.offset_bits=bmp_info.file_size;
1602 bmp_info.compression=BI_RGB;
1603 if ((image->storage_class == PseudoClass) && (image->colors > 256))
1604 (void) SetImageStorageClass(image,DirectClass,exception);
1605 if (image->storage_class != DirectClass)
1608 Colormapped BMP raster.
1610 bmp_info.bits_per_pixel=8;
1611 if (image->colors <= 2)
1612 bmp_info.bits_per_pixel=1;
1614 if (image->colors <= 16)
1615 bmp_info.bits_per_pixel=4;
1617 if (image->colors <= 256)
1618 bmp_info.bits_per_pixel=8;
1619 if (image_info->compression == RLECompression)
1620 bmp_info.bits_per_pixel=8;
1621 bmp_info.number_colors=1U << bmp_info.bits_per_pixel;
1622 if (image->alpha_trait != UndefinedPixelTrait)
1623 (void) SetImageStorageClass(image,DirectClass,exception);
1625 if ((size_t) bmp_info.number_colors < image->colors)
1626 (void) SetImageStorageClass(image,DirectClass,exception);
1629 bmp_info.file_size+=3*(1UL << bmp_info.bits_per_pixel);
1630 bmp_info.offset_bits+=3*(1UL << bmp_info.bits_per_pixel);
1633 bmp_info.file_size+=(1UL << bmp_info.bits_per_pixel);
1634 bmp_info.offset_bits+=(1UL << bmp_info.bits_per_pixel);
1638 if (image->storage_class == DirectClass)
1641 Full color BMP raster.
1643 bmp_info.number_colors=0;
1644 bmp_info.bits_per_pixel=(unsigned short)
1645 ((type > 3) && (image->alpha_trait != UndefinedPixelTrait) ? 32 : 24);
1646 bmp_info.compression=(unsigned int) ((type > 3) &&
1647 (image->alpha_trait != UndefinedPixelTrait) ? BI_BITFIELDS : BI_RGB);
1649 bytes_per_line=4*((image->columns*bmp_info.bits_per_pixel+31)/32);
1650 bmp_info.ba_offset=0;
1651 profile=GetImageProfile(image,"icc");
1652 have_color_info=(image->rendering_intent != UndefinedIntent) ||
1653 (profile != (StringInfo *) NULL) || (image->gamma != 0.0) ? MagickTrue :
1658 if ((type == 3) || ((image->alpha_trait == UndefinedPixelTrait) &&
1659 (have_color_info == MagickFalse)))
1671 if ((image->rendering_intent != UndefinedIntent) ||
1672 (profile != (StringInfo *) NULL))
1677 bmp_info.file_size+=extra_size;
1678 bmp_info.offset_bits+=extra_size;
1680 bmp_info.width=(ssize_t) image->columns;
1681 bmp_info.height=(ssize_t) image->rows;
1683 bmp_info.image_size=(unsigned int) (bytes_per_line*image->rows);
1684 bmp_info.file_size+=bmp_info.image_size;
1685 bmp_info.x_pixels=75*39;
1686 bmp_info.y_pixels=75*39;
1687 switch (image->units)
1689 case UndefinedResolution:
1690 case PixelsPerInchResolution:
1692 bmp_info.x_pixels=(unsigned int) (100.0*image->resolution.x/2.54);
1693 bmp_info.y_pixels=(unsigned int) (100.0*image->resolution.y/2.54);
1696 case PixelsPerCentimeterResolution:
1698 bmp_info.x_pixels=(unsigned int) (100.0*image->resolution.x);
1699 bmp_info.y_pixels=(unsigned int) (100.0*image->resolution.y);
1703 bmp_info.colors_important=bmp_info.number_colors;
1705 Convert MIFF to BMP raster pixels.
1707 pixel_info=AcquireVirtualMemory((size_t) bmp_info.image_size,
1709 if (pixel_info == (MemoryInfo *) NULL)
1710 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
1711 pixels=(unsigned char *) GetVirtualMemoryBlob(pixel_info);
1712 (void) ResetMagickMemory(pixels,0,(size_t) bmp_info.image_size);
1713 switch (bmp_info.bits_per_pixel)
1722 Convert PseudoClass image to a BMP monochrome image.
1724 for (y=0; y < (ssize_t) image->rows; y++)
1729 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
1730 if (p == (const Quantum *) NULL)
1732 q=pixels+(image->rows-y-1)*bytes_per_line;
1735 for (x=0; x < (ssize_t) image->columns; x++)
1738 byte|=GetPixelIndex(image,p) != 0 ? 0x01 : 0x00;
1742 *q++=(unsigned char) byte;
1746 p+=GetPixelChannels(image);
1750 *q++=(unsigned char) (byte << (8-bit));
1753 offset=(ssize_t) (image->columns+7)/8;
1754 for (x=offset; x < (ssize_t) bytes_per_line; x++)
1756 if (image->previous == (Image *) NULL)
1758 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
1760 if (status == MagickFalse)
1776 Convert PseudoClass image to a BMP monochrome image.
1778 for (y=0; y < (ssize_t) image->rows; y++)
1780 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
1781 if (p == (const Quantum *) NULL)
1783 q=pixels+(image->rows-y-1)*bytes_per_line;
1786 for (x=0; x < (ssize_t) image->columns; x++)
1789 byte|=((size_t) GetPixelIndex(image,p) & 0x0f);
1793 *q++=(unsigned char) byte;
1797 p+=GetPixelChannels(image);
1801 *q++=(unsigned char) (byte << 4);
1804 offset=(ssize_t) (image->columns+1)/2;
1805 for (x=offset; x < (ssize_t) bytes_per_line; x++)
1807 if (image->previous == (Image *) NULL)
1809 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
1811 if (status == MagickFalse)
1820 Convert PseudoClass packet to BMP pixel.
1822 for (y=0; y < (ssize_t) image->rows; y++)
1824 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
1825 if (p == (const Quantum *) NULL)
1827 q=pixels+(image->rows-y-1)*bytes_per_line;
1828 for (x=0; x < (ssize_t) image->columns; x++)
1830 *q++=(unsigned char) GetPixelIndex(image,p);
1831 p+=GetPixelChannels(image);
1833 for ( ; x < (ssize_t) bytes_per_line; x++)
1835 if (image->previous == (Image *) NULL)
1837 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
1839 if (status == MagickFalse)
1848 Convert DirectClass packet to BMP BGR888.
1850 for (y=0; y < (ssize_t) image->rows; y++)
1852 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
1853 if (p == (const Quantum *) NULL)
1855 q=pixels+(image->rows-y-1)*bytes_per_line;
1856 for (x=0; x < (ssize_t) image->columns; x++)
1858 *q++=ScaleQuantumToChar(GetPixelBlue(image,p));
1859 *q++=ScaleQuantumToChar(GetPixelGreen(image,p));
1860 *q++=ScaleQuantumToChar(GetPixelRed(image,p));
1861 p+=GetPixelChannels(image);
1863 for (x=3L*(ssize_t) image->columns; x < (ssize_t) bytes_per_line; x++)
1865 if (image->previous == (Image *) NULL)
1867 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
1869 if (status == MagickFalse)
1878 Convert DirectClass packet to ARGB8888 pixel.
1880 for (y=0; y < (ssize_t) image->rows; y++)
1882 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
1883 if (p == (const Quantum *) NULL)
1885 q=pixels+(image->rows-y-1)*bytes_per_line;
1886 for (x=0; x < (ssize_t) image->columns; x++)
1888 *q++=ScaleQuantumToChar(GetPixelBlue(image,p));
1889 *q++=ScaleQuantumToChar(GetPixelGreen(image,p));
1890 *q++=ScaleQuantumToChar(GetPixelRed(image,p));
1891 *q++=ScaleQuantumToChar(GetPixelAlpha(image,p));
1892 p+=GetPixelChannels(image);
1894 if (image->previous == (Image *) NULL)
1896 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
1898 if (status == MagickFalse)
1905 if ((type > 2) && (bmp_info.bits_per_pixel == 8))
1906 if (image_info->compression != NoCompression)
1912 Convert run-length encoded raster pixels.
1914 rle_info=AcquireVirtualMemory((size_t) (2*(bytes_per_line+2)+2),
1915 (image->rows+2)*sizeof(*pixels));
1916 if (rle_info == (MemoryInfo *) NULL)
1918 pixel_info=RelinquishVirtualMemory(pixel_info);
1919 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
1921 bmp_data=(unsigned char *) GetVirtualMemoryBlob(rle_info);
1922 bmp_info.file_size-=bmp_info.image_size;
1923 bmp_info.image_size=(unsigned int) EncodeImage(image,bytes_per_line,
1925 bmp_info.file_size+=bmp_info.image_size;
1926 pixel_info=RelinquishVirtualMemory(pixel_info);
1927 pixel_info=rle_info;
1929 bmp_info.compression=BI_RLE8;
1932 Write BMP for Windows, all versions, 14-byte header.
1934 if (image->debug != MagickFalse)
1936 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1937 " Writing BMP version %.20g datastream",(double) type);
1938 if (image->storage_class == DirectClass)
1939 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1940 " Storage class=DirectClass");
1942 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1943 " Storage class=PseudoClass");
1944 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1945 " Image depth=%.20g",(double) image->depth);
1946 if (image->alpha_trait != UndefinedPixelTrait)
1947 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1950 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1951 " Matte=MagickFalse");
1952 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1953 " BMP bits_per_pixel=%.20g",(double) bmp_info.bits_per_pixel);
1954 switch ((int) bmp_info.compression)
1958 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1959 " Compression=BI_RGB");
1964 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1965 " Compression=BI_RLE8");
1970 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1971 " Compression=BI_BITFIELDS");
1976 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1977 " Compression=UNKNOWN (%u)",bmp_info.compression);
1981 if (bmp_info.number_colors == 0)
1982 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1983 " Number_colors=unspecified");
1985 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1986 " Number_colors=%u",bmp_info.number_colors);
1988 (void) WriteBlob(image,2,(unsigned char *) "BM");
1989 (void) WriteBlobLSBLong(image,bmp_info.file_size);
1990 (void) WriteBlobLSBLong(image,bmp_info.ba_offset); /* always 0 */
1991 (void) WriteBlobLSBLong(image,bmp_info.offset_bits);
1995 Write 12-byte version 2 bitmap header.
1997 (void) WriteBlobLSBLong(image,bmp_info.size);
1998 (void) WriteBlobLSBShort(image,(unsigned short) bmp_info.width);
1999 (void) WriteBlobLSBShort(image,(unsigned short) bmp_info.height);
2000 (void) WriteBlobLSBShort(image,bmp_info.planes);
2001 (void) WriteBlobLSBShort(image,bmp_info.bits_per_pixel);
2006 Write 40-byte version 3+ bitmap header.
2008 (void) WriteBlobLSBLong(image,bmp_info.size);
2009 (void) WriteBlobLSBLong(image,(unsigned int) bmp_info.width);
2010 (void) WriteBlobLSBLong(image,(unsigned int) bmp_info.height);
2011 (void) WriteBlobLSBShort(image,bmp_info.planes);
2012 (void) WriteBlobLSBShort(image,bmp_info.bits_per_pixel);
2013 (void) WriteBlobLSBLong(image,bmp_info.compression);
2014 (void) WriteBlobLSBLong(image,bmp_info.image_size);
2015 (void) WriteBlobLSBLong(image,bmp_info.x_pixels);
2016 (void) WriteBlobLSBLong(image,bmp_info.y_pixels);
2017 (void) WriteBlobLSBLong(image,bmp_info.number_colors);
2018 (void) WriteBlobLSBLong(image,bmp_info.colors_important);
2020 if ((type > 3) && ((image->alpha_trait != UndefinedPixelTrait) ||
2021 (have_color_info != MagickFalse)))
2024 Write the rest of the 108-byte BMP Version 4 header.
2026 (void) WriteBlobLSBLong(image,0x00ff0000U); /* Red mask */
2027 (void) WriteBlobLSBLong(image,0x0000ff00U); /* Green mask */
2028 (void) WriteBlobLSBLong(image,0x000000ffU); /* Blue mask */
2029 (void) WriteBlobLSBLong(image,0xff000000U); /* Alpha mask */
2030 (void) WriteBlobLSBLong(image,0x73524742U); /* sRGB */
2031 (void) WriteBlobLSBLong(image,(unsigned int)
2032 (image->chromaticity.red_primary.x*0x40000000));
2033 (void) WriteBlobLSBLong(image,(unsigned int)
2034 (image->chromaticity.red_primary.y*0x40000000));
2035 (void) WriteBlobLSBLong(image,(unsigned int)
2036 ((1.000f-(image->chromaticity.red_primary.x+
2037 image->chromaticity.red_primary.y))*0x40000000));
2038 (void) WriteBlobLSBLong(image,(unsigned int)
2039 (image->chromaticity.green_primary.x*0x40000000));
2040 (void) WriteBlobLSBLong(image,(unsigned int)
2041 (image->chromaticity.green_primary.y*0x40000000));
2042 (void) WriteBlobLSBLong(image,(unsigned int)
2043 ((1.000f-(image->chromaticity.green_primary.x+
2044 image->chromaticity.green_primary.y))*0x40000000));
2045 (void) WriteBlobLSBLong(image,(unsigned int)
2046 (image->chromaticity.blue_primary.x*0x40000000));
2047 (void) WriteBlobLSBLong(image,(unsigned int)
2048 (image->chromaticity.blue_primary.y*0x40000000));
2049 (void) WriteBlobLSBLong(image,(unsigned int)
2050 ((1.000f-(image->chromaticity.blue_primary.x+
2051 image->chromaticity.blue_primary.y))*0x40000000));
2052 (void) WriteBlobLSBLong(image,(unsigned int)
2053 (bmp_info.gamma_scale.x*0x10000));
2054 (void) WriteBlobLSBLong(image,(unsigned int)
2055 (bmp_info.gamma_scale.y*0x10000));
2056 (void) WriteBlobLSBLong(image,(unsigned int)
2057 (bmp_info.gamma_scale.z*0x10000));
2058 if ((image->rendering_intent != UndefinedIntent) ||
2059 (profile != (StringInfo *) NULL))
2064 switch ((int) image->rendering_intent)
2066 case SaturationIntent:
2068 intent=LCS_GM_BUSINESS;
2071 case RelativeIntent:
2073 intent=LCS_GM_GRAPHICS;
2076 case PerceptualIntent:
2078 intent=LCS_GM_IMAGES;
2081 case AbsoluteIntent:
2083 intent=LCS_GM_ABS_COLORIMETRIC;
2092 (void) WriteBlobLSBLong(image,(unsigned int) intent);
2093 (void) WriteBlobLSBLong(image,0x00); /* dummy profile data */
2094 (void) WriteBlobLSBLong(image,0x00); /* dummy profile length */
2095 (void) WriteBlobLSBLong(image,0x00); /* reserved */
2098 if (image->storage_class == PseudoClass)
2104 Dump colormap to file.
2106 if (image->debug != MagickFalse)
2107 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
2108 " Colormap: %.20g entries",(double) image->colors);
2109 bmp_colormap=(unsigned char *) AcquireQuantumMemory((size_t) (1UL <<
2110 bmp_info.bits_per_pixel),4*sizeof(*bmp_colormap));
2111 if (bmp_colormap == (unsigned char *) NULL)
2112 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
2114 for (i=0; i < (ssize_t) MagickMin((ssize_t) image->colors,(ssize_t) bmp_info.number_colors); i++)
2116 *q++=ScaleQuantumToChar(ClampToQuantum(image->colormap[i].blue));
2117 *q++=ScaleQuantumToChar(ClampToQuantum(image->colormap[i].green));
2118 *q++=ScaleQuantumToChar(ClampToQuantum(image->colormap[i].red));
2120 *q++=(unsigned char) 0x0;
2122 for ( ; i < (ssize_t) (1UL << bmp_info.bits_per_pixel); i++)
2124 *q++=(unsigned char) 0x00;
2125 *q++=(unsigned char) 0x00;
2126 *q++=(unsigned char) 0x00;
2128 *q++=(unsigned char) 0x00;
2131 (void) WriteBlob(image,(size_t) (3*(1L << bmp_info.bits_per_pixel)),
2134 (void) WriteBlob(image,(size_t) (4*(1L << bmp_info.bits_per_pixel)),
2136 bmp_colormap=(unsigned char *) RelinquishMagickMemory(bmp_colormap);
2138 if (image->debug != MagickFalse)
2139 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
2140 " Pixels: %u bytes",bmp_info.image_size);
2141 (void) WriteBlob(image,(size_t) bmp_info.image_size,pixels);
2142 pixel_info=RelinquishVirtualMemory(pixel_info);
2143 if (GetNextImageInList(image) == (Image *) NULL)
2145 image=SyncNextImageInList(image);
2146 status=SetImageProgress(image,SaveImagesTag,scene++,
2147 GetImageListLength(image));
2148 if (status == MagickFalse)
2150 } while (image_info->adjoin != MagickFalse);
2151 (void) CloseBlob(image);