2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
13 % Read/Write Microsoft Windows Bitmap Image Format %
17 % Glenn Randers-Pehrson %
21 % Copyright 1999-2009 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 "magick/studio.h"
44 #include "magick/blob.h"
45 #include "magick/blob-private.h"
46 #include "magick/cache.h"
47 #include "magick/colormap-private.h"
48 #include "magick/color-private.h"
49 #include "magick/colorspace.h"
50 #include "magick/exception.h"
51 #include "magick/exception-private.h"
52 #include "magick/image.h"
53 #include "magick/image-private.h"
54 #include "magick/list.h"
55 #include "magick/log.h"
56 #include "magick/magick.h"
57 #include "magick/memory_.h"
58 #include "magick/monitor.h"
59 #include "magick/monitor-private.h"
60 #include "magick/profile.h"
61 #include "magick/quantum-private.h"
62 #include "magick/static.h"
63 #include "magick/string_.h"
64 #include "magick/module.h"
65 #include "magick/transform.h"
68 Macro definitions (from Windows wingdi.h).
74 #if !defined(__WINDOWS__) || defined(__MINGW32__)
78 #define BI_BITFIELDS 3
80 #define LCS_CALIBRATED_RBG 0
82 #define LCS_WINDOWS_COLOR_SPACE 2
83 #define PROFILE_LINKED 3
84 #define PROFILE_EMBEDDED 4
86 #define LCS_GM_BUSINESS 1 /* Saturation */
87 #define LCS_GM_GRAPHICS 2 /* Relative */
88 #define LCS_GM_IMAGES 4 /* Perceptual */
89 #define LCS_GM_ABS_COLORIMETRIC 8 /* Absolute */
95 typedef struct _BMPInfo
134 Forward declarations.
136 static MagickBooleanType
137 WriteBMPImage(const ImageInfo *,Image *);
140 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
144 % D e c o d e I m a g e %
148 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
150 % DecodeImage unpacks the packed image pixels into runlength-encoded
153 % The format of the DecodeImage method is:
155 % MagickBooleanType DecodeImage(Image *image,
156 % const unsigned long compression,unsigned char *pixels)
158 % A description of each parameter follows:
160 % o image: the address of a structure of type Image.
162 % o compression: Zero means uncompressed. A value of 1 means the
163 % compressed pixels are runlength encoded for a 256-color bitmap.
164 % A value of 2 means a 16-color bitmap. A value of 3 means bitfields
167 % o pixels: The address of a byte (8 bits) array of pixel data created by
168 % the decoding process.
172 static inline long MagickAbsoluteValue(const long x)
179 static inline size_t MagickMax(const size_t x,const size_t y)
186 static inline long MagickMin(const long x,const long y)
193 static MagickBooleanType DecodeImage(Image *image,
194 const unsigned long compression,unsigned char *pixels)
206 register unsigned char
213 assert(image != (Image *) NULL);
214 assert(image->signature == MagickSignature);
215 if (image->debug != MagickFalse)
216 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
217 assert(pixels != (unsigned char *) NULL);
218 (void) ResetMagickMemory(pixels,0,(size_t) image->columns*image->rows*
223 q=pixels+(size_t) image->columns*image->rows;
224 for (y=0; y < (long) image->rows; )
226 if ((p < pixels) || (p >= q))
228 count=ReadBlobByte(image);
236 count=MagickMin(count,(int) (q-p));
237 byte=(unsigned char) ReadBlobByte(image);
238 if (compression == BI_RLE8)
240 for (i=0; i < count; i++)
241 *p++=(unsigned char) byte;
245 for (i=0; i < count; i++)
247 ((i & 0x01) != 0 ? (byte & 0x0f) : ((byte >> 4) & 0x0f));
256 count=ReadBlobByte(image);
268 p=pixels+y*image->columns;
276 x+=ReadBlobByte(image);
277 y+=ReadBlobByte(image);
278 p=pixels+y*image->columns+x;
286 count=MagickMin(count,(int) (q-p));
287 if (compression == BI_RLE8)
288 for (i=0; i < count; i++)
289 *p++=(unsigned char) ReadBlobByte(image);
291 for (i=0; i < count; i++)
294 byte=(unsigned char) ReadBlobByte(image);
296 ((i & 0x01) != 0 ? (byte & 0x0f) : ((byte >> 4) & 0x0f));
302 if (compression == BI_RLE8)
304 if ((count & 0x01) != 0)
305 (void) ReadBlobByte(image);
308 if (((count & 0x03) == 1) || ((count & 0x03) == 2))
309 (void) ReadBlobByte(image);
314 if (SetImageProgress(image,LoadImageTag,y,image->rows) == MagickFalse)
317 (void) ReadBlobByte(image); /* end of line */
318 (void) ReadBlobByte(image);
323 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
327 % E n c o d e I m a g e %
331 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
333 % EncodeImage compresses pixels using a runlength encoded format.
335 % The format of the EncodeImage method is:
337 % static MagickBooleanType EncodeImage(Image *image,
338 % const unsigned long bytes_per_line,const unsigned char *pixels,
339 % unsigned char *compressed_pixels)
341 % A description of each parameter follows:
343 % o image: The image.
345 % o bytes_per_line: the number of bytes in a scanline of compressed pixels
347 % o pixels: The address of a byte (8 bits) array of pixel data created by
348 % the compression process.
350 % o compressed_pixels: The address of a byte (8 bits) array of compressed
354 static size_t EncodeImage(Image *image,const unsigned long bytes_per_line,
355 const unsigned char *pixels,unsigned char *compressed_pixels)
363 register const unsigned char
370 register unsigned char
374 Runlength encode pixels.
376 assert(image != (Image *) NULL);
377 assert(image->signature == MagickSignature);
378 if (image->debug != MagickFalse)
379 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
380 assert(pixels != (const unsigned char *) NULL);
381 assert(compressed_pixels != (unsigned char *) NULL);
385 for (y=0; y < (long) image->rows; y++)
387 for (x=0; x < (long) bytes_per_line; x+=i)
392 for (i=1; ((x+i) < (long) bytes_per_line); i++)
393 if ((i == 255) || (*(p+i) != *p))
395 *q++=(unsigned char) i;
402 *q++=(unsigned char) 0x00;
403 *q++=(unsigned char) 0x00;
404 status=SetImageProgress(image,SaveImageTag,y,image->rows);
405 if (status == MagickFalse)
411 *q++=(unsigned char) 0x00;
412 *q++=(unsigned char) 0x01;
413 return((size_t) (q-compressed_pixels));
417 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
425 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
427 % IsBMP() returns MagickTrue if the image format type, identified by the
428 % magick string, is BMP.
430 % The format of the IsBMP method is:
432 % MagickBooleanType IsBMP(const unsigned char *magick,const size_t length)
434 % A description of each parameter follows:
436 % o magick: compare image format pattern against these bytes.
438 % o length: Specifies the length of the magick string.
441 static MagickBooleanType IsBMP(const unsigned char *magick,const size_t length)
445 if ((LocaleNCompare((char *) magick,"BA",2) == 0) ||
446 (LocaleNCompare((char *) magick,"BM",2) == 0) ||
447 (LocaleNCompare((char *) magick,"IC",2) == 0) ||
448 (LocaleNCompare((char *) magick,"PI",2) == 0) ||
449 (LocaleNCompare((char *) magick,"CI",2) == 0) ||
450 (LocaleNCompare((char *) magick,"CP",2) == 0))
456 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
460 % R e a d B M P I m a g e %
464 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
466 % ReadBMPImage() reads a Microsoft Windows bitmap image file, Version
467 % 2, 3 (for Windows or NT), or 4, and returns it. It allocates the memory
468 % necessary for the new Image structure and returns a pointer to the new
471 % The format of the ReadBMPImage method is:
473 % image=ReadBMPImage(image_info)
475 % A description of each parameter follows:
477 % o image_info: the image info.
479 % o exception: return any errors or warnings in this structure.
483 static Image *ReadBMPImage(const ImageInfo *image_info,ExceptionInfo *exception)
516 register unsigned char
540 assert(image_info != (const ImageInfo *) NULL);
541 assert(image_info->signature == MagickSignature);
542 if (image_info->debug != MagickFalse)
543 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
544 image_info->filename);
545 assert(exception != (ExceptionInfo *) NULL);
546 assert(exception->signature == MagickSignature);
547 image=AcquireImage(image_info);
548 status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception);
549 if (status == MagickFalse)
551 image=DestroyImageList(image);
552 return((Image *) NULL);
555 Determine if this a BMP file.
557 (void) ResetMagickMemory(&bmp_info,0,sizeof(bmp_info));
558 bmp_info.ba_offset=0;
560 count=ReadBlob(image,2,magick);
574 Verify BMP identifier.
576 if (bmp_info.ba_offset == 0)
577 start_position=TellBlob(image)-2;
578 bmp_info.ba_offset=0;
579 while (LocaleNCompare((char *) magick,"BA",2) == 0)
581 bmp_info.file_size=ReadBlobLSBLong(image);
582 bmp_info.ba_offset=ReadBlobLSBLong(image);
583 bmp_info.offset_bits=ReadBlobLSBLong(image);
584 count=ReadBlob(image,2,magick);
588 if (image->debug != MagickFalse)
589 (void) LogMagickEvent(CoderEvent,GetMagickModule()," Magick: %c%c",
590 magick[0],magick[1]);
591 if ((count == 0) || ((LocaleNCompare((char *) magick,"BM",2) != 0) &&
592 (LocaleNCompare((char *) magick,"CI",2) != 0)))
593 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
594 bmp_info.file_size=ReadBlobLSBLong(image);
595 (void) ReadBlobLSBLong(image);
596 bmp_info.offset_bits=ReadBlobLSBLong(image);
597 bmp_info.size=ReadBlobLSBLong(image);
598 if (image->debug != MagickFalse)
599 (void) LogMagickEvent(CoderEvent,GetMagickModule()," BMP size: %u",
601 if (bmp_info.size == 12)
606 bmp_info.width=(short) ReadBlobLSBShort(image);
607 bmp_info.height=(short) ReadBlobLSBShort(image);
608 bmp_info.planes=ReadBlobLSBShort(image);
609 bmp_info.bits_per_pixel=ReadBlobLSBShort(image);
612 bmp_info.number_colors=0;
613 bmp_info.compression=BI_RGB;
614 bmp_info.image_size=0;
615 bmp_info.alpha_mask=0;
616 if (image->debug != MagickFalse)
618 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
619 " Format: OS/2 Bitmap");
620 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
621 " Geometry: %ldx%ld",bmp_info.width,bmp_info.height);
627 Microsoft Windows BMP image file.
629 if (bmp_info.size < 40)
630 ThrowReaderException(CorruptImageError,"NonOS2HeaderSizeError");
631 bmp_info.width=(int) ReadBlobLSBLong(image);
632 bmp_info.height=(int) ReadBlobLSBLong(image);
633 bmp_info.planes=ReadBlobLSBShort(image);
634 bmp_info.bits_per_pixel=ReadBlobLSBShort(image);
635 bmp_info.compression=ReadBlobLSBLong(image);
636 bmp_info.image_size=ReadBlobLSBLong(image);
637 bmp_info.x_pixels=ReadBlobLSBLong(image);
638 bmp_info.y_pixels=ReadBlobLSBLong(image);
639 bmp_info.number_colors=ReadBlobLSBLong(image);
640 bmp_info.colors_important=ReadBlobLSBLong(image);
643 if (image->debug != MagickFalse)
645 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
646 " Format: MS Windows bitmap");
647 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
648 " Geometry: %ldx%ld",bmp_info.width,bmp_info.height);
649 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
650 " Bits per pixel: %d",bmp_info.bits_per_pixel);
651 switch ((int) bmp_info.compression)
655 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
656 " Compression: BI_RGB");
661 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
662 " Compression: BI_RLE4");
667 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
668 " Compression: BI_RLE8");
673 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
674 " Compression: BI_BITFIELDS");
679 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
680 " Compression: BI_PNG");
685 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
686 " Compression: BI_JPEG");
691 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
692 " Compression: UNKNOWN (%u)",bmp_info.compression);
695 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
696 " Number of colors: %u",bmp_info.number_colors);
698 bmp_info.red_mask=ReadBlobLSBLong(image);
699 bmp_info.green_mask=ReadBlobLSBLong(image);
700 bmp_info.blue_mask=ReadBlobLSBLong(image);
701 if (bmp_info.size > 40)
707 Read color management information.
709 bmp_info.alpha_mask=ReadBlobLSBLong(image);
710 bmp_info.colorspace=(long) ReadBlobLSBLong(image);
712 Decode 2^30 fixed point formatted CIE primaries.
714 bmp_info.red_primary.x=(double) ReadBlobLSBLong(image)/0x3ffffff;
715 bmp_info.red_primary.y=(double) ReadBlobLSBLong(image)/0x3ffffff;
716 bmp_info.red_primary.z=(double) ReadBlobLSBLong(image)/0x3ffffff;
717 bmp_info.green_primary.x=(double) ReadBlobLSBLong(image)/0x3ffffff;
718 bmp_info.green_primary.y=(double) ReadBlobLSBLong(image)/0x3ffffff;
719 bmp_info.green_primary.z=(double) ReadBlobLSBLong(image)/0x3ffffff;
720 bmp_info.blue_primary.x=(double) ReadBlobLSBLong(image)/0x3ffffff;
721 bmp_info.blue_primary.y=(double) ReadBlobLSBLong(image)/0x3ffffff;
722 bmp_info.blue_primary.z=(double) ReadBlobLSBLong(image)/0x3ffffff;
723 sum=bmp_info.red_primary.x+bmp_info.red_primary.x+
724 bmp_info.red_primary.z;
725 image->chromaticity.red_primary.x/=sum;
726 image->chromaticity.red_primary.y/=sum;
727 sum=bmp_info.green_primary.x+bmp_info.green_primary.x+
728 bmp_info.green_primary.z;
729 image->chromaticity.green_primary.x/=sum;
730 image->chromaticity.green_primary.y/=sum;
731 sum=bmp_info.blue_primary.x+bmp_info.blue_primary.x+
732 bmp_info.blue_primary.z;
733 image->chromaticity.blue_primary.x/=sum;
734 image->chromaticity.blue_primary.y/=sum;
736 Decode 16^16 fixed point formatted gamma_scales.
738 bmp_info.gamma_scale.x=(double) ReadBlobLSBLong(image)/0xffff;
739 bmp_info.gamma_scale.y=(double) ReadBlobLSBLong(image)/0xffff;
740 bmp_info.gamma_scale.z=(double) ReadBlobLSBLong(image)/0xffff;
742 Compute a single gamma from the BMP 3-channel gamma.
744 image->gamma=(bmp_info.gamma_scale.x+bmp_info.gamma_scale.y+
745 bmp_info.gamma_scale.z)/3.0;
747 if (bmp_info.size > 108)
753 Read BMP Version 5 color management information.
755 intent=ReadBlobLSBLong(image);
756 switch ((int) intent)
758 case LCS_GM_BUSINESS:
760 image->rendering_intent=SaturationIntent;
763 case LCS_GM_GRAPHICS:
765 image->rendering_intent=RelativeIntent;
770 image->rendering_intent=PerceptualIntent;
773 case LCS_GM_ABS_COLORIMETRIC:
775 image->rendering_intent=AbsoluteIntent;
779 profile_data=ReadBlobLSBLong(image);
780 profile_size=ReadBlobLSBLong(image);
781 (void) ReadBlobLSBLong(image); /* Reserved byte */
784 if ((bmp_info.compression != BI_RGB) &&
785 ((MagickSizeType) bmp_info.file_size != GetBlobSize(image)))
786 (void) ThrowMagickException(exception,GetMagickModule(),CorruptImageError,
787 "LengthAndFilesizeDoNotMatch","`%s'",image->filename);
788 if (bmp_info.width <= 0)
789 ThrowReaderException(CorruptImageError,"NegativeOrZeroImageSize");
790 if (bmp_info.height == 0)
791 ThrowReaderException(CorruptImageError,"NegativeOrZeroImageSize");
792 if (bmp_info.planes != 1)
793 ThrowReaderException(CorruptImageError,"StaticPlanesValueNotEqualToOne");
794 if ((bmp_info.bits_per_pixel != 1) && (bmp_info.bits_per_pixel != 4) &&
795 (bmp_info.bits_per_pixel != 8) && (bmp_info.bits_per_pixel != 16) &&
796 (bmp_info.bits_per_pixel != 24) && (bmp_info.bits_per_pixel != 32))
797 ThrowReaderException(CorruptImageError,"UnrecognizedBitsPerPixel");
798 if (bmp_info.number_colors > (1U << bmp_info.bits_per_pixel))
800 if (bmp_info.bits_per_pixel < 24)
801 ThrowReaderException(CorruptImageError,"UnrecognizedNumberOfColors");
802 bmp_info.number_colors=0;
804 if (bmp_info.compression > 3)
805 ThrowReaderException(CorruptImageError,"UnrecognizedImageCompression");
806 if ((bmp_info.compression == 1) && (bmp_info.bits_per_pixel != 8))
807 ThrowReaderException(CorruptImageError,"UnrecognizedBitsPerPixel");
808 if ((bmp_info.compression == 2) && (bmp_info.bits_per_pixel != 4))
809 ThrowReaderException(CorruptImageError,"UnrecognizedBitsPerPixel");
810 if ((bmp_info.compression == 3) && (bmp_info.bits_per_pixel < 16))
811 ThrowReaderException(CorruptImageError,"UnrecognizedBitsPerPixel");
812 switch (bmp_info.compression)
820 ThrowReaderException(CoderError,"JPEGCompressNotSupported");
822 ThrowReaderException(CoderError,"PNGCompressNotSupported");
824 ThrowReaderException(CorruptImageError,"UnrecognizedImageCompression");
826 image->columns=(unsigned long) MagickAbsoluteValue(bmp_info.width);
827 image->rows=(unsigned long) MagickAbsoluteValue(bmp_info.height);
828 image->depth=bmp_info.bits_per_pixel <= 8 ? bmp_info.bits_per_pixel : 8;
829 image->matte=bmp_info.alpha_mask != 0 ? MagickTrue : MagickFalse;
830 if ((bmp_info.number_colors != 0) || (bmp_info.bits_per_pixel < 16))
832 image->storage_class=PseudoClass;
833 image->colors=bmp_info.number_colors;
834 if (image->colors == 0)
835 image->colors=1L << bmp_info.bits_per_pixel;
837 if (image->storage_class == PseudoClass)
846 Read BMP raster colormap.
848 if (image->debug != MagickFalse)
849 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
850 " Reading colormap of %ld colors",image->colors);
851 if (AcquireImageColormap(image,image->colors) == MagickFalse)
852 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
853 bmp_colormap=(unsigned char *) AcquireQuantumMemory((size_t)
854 image->colors,4*sizeof(*bmp_colormap));
855 if (bmp_colormap == (unsigned char *) NULL)
856 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
857 if ((bmp_info.size == 12) || (bmp_info.size == 64))
861 offset=SeekBlob(image,start_position+14+bmp_info.size,SEEK_SET);
863 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
864 count=ReadBlob(image,packet_size*image->colors,bmp_colormap);
865 if (count != (ssize_t) (packet_size*image->colors))
866 ThrowReaderException(CorruptImageError,"InsufficientImageDataInFile");
868 for (i=0; i < (long) image->colors; i++)
870 image->colormap[i].blue=ScaleCharToQuantum(*p++);
871 image->colormap[i].green=ScaleCharToQuantum(*p++);
872 image->colormap[i].red=ScaleCharToQuantum(*p++);
873 if (packet_size == 4)
876 bmp_colormap=(unsigned char *) RelinquishMagickMemory(bmp_colormap);
878 if ((image_info->ping != MagickFalse) && (image_info->number_scenes != 0))
879 if (image->scene >= (image_info->scene+image_info->number_scenes-1))
884 offset=SeekBlob(image,start_position+bmp_info.offset_bits,SEEK_SET);
886 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
887 if (bmp_info.compression == BI_RLE4)
888 bmp_info.bits_per_pixel<<=1;
889 bytes_per_line=4*((image->columns*bmp_info.bits_per_pixel+31)/32);
890 length=(size_t) bytes_per_line*image->rows;
891 pixels=(unsigned char *) AcquireQuantumMemory((size_t) image->rows,
892 MagickMax(bytes_per_line,image->columns+256UL)*sizeof(*pixels));
893 if (pixels == (unsigned char *) NULL)
894 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
895 if ((bmp_info.compression == BI_RGB) ||
896 (bmp_info.compression == BI_BITFIELDS))
898 if (image->debug != MagickFalse)
899 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
900 " Reading pixels (%ld bytes)",(long) length);
901 count=ReadBlob(image,length,pixels);
902 if (count != (ssize_t) length)
903 ThrowReaderException(CorruptImageError,"InsufficientImageDataInFile");
908 Convert run-length encoded raster pixels.
910 status=DecodeImage(image,bmp_info.compression,pixels);
911 if (status == MagickFalse)
912 ThrowReaderException(CorruptImageError,
913 "UnableToRunlengthDecodeImage");
916 Initialize image structure.
918 image->x_resolution=(double) bmp_info.x_pixels/100.0;
919 image->y_resolution=(double) bmp_info.y_pixels/100.0;
920 image->units=PixelsPerCentimeterResolution;
922 Convert BMP raster image to pixel packets.
924 if (bmp_info.compression == BI_RGB)
926 bmp_info.alpha_mask=0;
927 bmp_info.red_mask=0x00ff0000U;
928 bmp_info.green_mask=0x0000ff00U;
929 bmp_info.blue_mask=0x000000ffU;
930 if (bmp_info.bits_per_pixel == 16)
935 bmp_info.red_mask=0x00007c00U;
936 bmp_info.green_mask=0x000003e0U;
937 bmp_info.blue_mask=0x0000001fU;
940 if ((bmp_info.bits_per_pixel == 16) || (bmp_info.bits_per_pixel == 32))
942 register unsigned long
946 Get shift and quantum bits info from bitfield masks.
948 (void) ResetMagickMemory(&shift,0,sizeof(shift));
949 (void) ResetMagickMemory(&quantum_bits,0,sizeof(quantum_bits));
950 if (bmp_info.red_mask != 0)
951 while (((bmp_info.red_mask << shift.red) & 0x80000000UL) == 0)
953 if (bmp_info.green_mask != 0)
954 while (((bmp_info.green_mask << shift.green) & 0x80000000UL) == 0)
956 if (bmp_info.blue_mask != 0)
957 while (((bmp_info.blue_mask << shift.blue) & 0x80000000UL) == 0)
959 if (bmp_info.alpha_mask != 0)
960 while (((bmp_info.alpha_mask << shift.opacity) & 0x80000000UL) == 0)
963 while (((bmp_info.red_mask << sample) & 0x80000000UL) != 0)
965 quantum_bits.red=(Quantum) (sample-shift.red);
967 while (((bmp_info.green_mask << sample) & 0x80000000UL) != 0)
969 quantum_bits.green=(Quantum) (sample-shift.green);
971 while (((bmp_info.blue_mask << sample) & 0x80000000UL) != 0)
973 quantum_bits.blue=(Quantum) (sample-shift.blue);
974 sample=shift.opacity;
975 while (((bmp_info.alpha_mask << sample) & 0x80000000UL) != 0)
977 quantum_bits.opacity=(Quantum) (sample-shift.opacity);
979 switch (bmp_info.bits_per_pixel)
984 Convert bitmap scanline.
986 for (y=(long) image->rows-1; y >= 0; y--)
988 p=pixels+(image->rows-y-1)*bytes_per_line;
989 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
990 if (q == (PixelPacket *) NULL)
992 indexes=GetAuthenticIndexQueue(image);
993 for (x=0; x < ((long) image->columns-7); x+=8)
995 for (bit=0; bit < 8; bit++)
997 index=(IndexPacket) (((*p) & (0x80 >> bit)) != 0 ? 0x01 : 0x00);
998 indexes[x+bit]=index;
999 *q++=image->colormap[(long) index];
1003 if ((image->columns % 8) != 0)
1005 for (bit=0; bit < (image->columns % 8); bit++)
1007 index=(IndexPacket) (((*p) & (0x80 >> bit)) != 0 ? 0x01 : 0x00);
1008 indexes[x+bit]=index;
1009 *q++=image->colormap[(long) index];
1013 if (SyncAuthenticPixels(image,exception) == MagickFalse)
1015 if (image->previous == (Image *) NULL)
1017 status=SetImageProgress(image,LoadImageTag,y,image->rows);
1018 if (status == MagickFalse)
1027 Convert PseudoColor scanline.
1029 for (y=(long) image->rows-1; y >= 0; y--)
1031 p=pixels+(image->rows-y-1)*bytes_per_line;
1032 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
1033 if (q == (PixelPacket *) NULL)
1035 indexes=GetAuthenticIndexQueue(image);
1036 for (x=0; x < ((long) image->columns-1); x+=2)
1038 index=ConstrainColormapIndex(image,(*p >> 4) & 0x0f);
1040 *q++=image->colormap[(long) index];
1041 index=ConstrainColormapIndex(image,*p & 0x0f);
1043 *q++=image->colormap[(long) index];
1046 if ((image->columns % 2) != 0)
1048 index=ConstrainColormapIndex(image,(*p >> 4) & 0xf);
1050 *q++=image->colormap[(long) index];
1053 if (SyncAuthenticPixels(image,exception) == MagickFalse)
1055 if (image->previous == (Image *) NULL)
1057 status=SetImageProgress(image,LoadImageTag,y,image->rows);
1058 if (status == MagickFalse)
1067 Convert PseudoColor scanline.
1069 if ((bmp_info.compression == BI_RLE8) ||
1070 (bmp_info.compression == BI_RLE4))
1071 bytes_per_line=image->columns;
1072 for (y=(long) image->rows-1; y >= 0; y--)
1074 p=pixels+(image->rows-y-1)*bytes_per_line;
1075 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
1076 if (q == (PixelPacket *) NULL)
1078 indexes=GetAuthenticIndexQueue(image);
1079 for (x = (long)image->columns; x != 0; --x)
1081 index=ConstrainColormapIndex(image,*p);
1083 *q=image->colormap[(long) index];
1087 if (SyncAuthenticPixels(image,exception) == MagickFalse)
1089 offset=(MagickOffsetType) (image->rows-y-1);
1090 if (image->previous == (Image *) NULL)
1092 status=SetImageProgress(image,LoadImageTag,y,image->rows);
1093 if (status == MagickFalse)
1105 Convert bitfield encoded 16-bit PseudoColor scanline.
1107 if (bmp_info.compression != BI_RGB &&
1108 bmp_info.compression != BI_BITFIELDS)
1109 ThrowReaderException(CorruptImageError,
1110 "UnrecognizedImageCompression");
1111 bytes_per_line=2*(image->columns+image->columns % 2);
1112 image->storage_class=DirectClass;
1113 for (y=(long) image->rows-1; y >= 0; y--)
1115 p=pixels+(image->rows-y-1)*bytes_per_line;
1116 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
1117 if (q == (PixelPacket *) NULL)
1119 for (x=0; x < (long) image->columns; x++)
1121 pixel=(unsigned long) (*p++);
1123 red=((pixel & bmp_info.red_mask) << shift.red) >> 16;
1124 if (quantum_bits.red == 5)
1125 red|=((red & 0xe000) >> 5);
1126 if (quantum_bits.red <= 8)
1127 red|=((red & 0xff00) >> 8);
1128 green=((pixel & bmp_info.green_mask) << shift.green) >> 16;
1129 if (quantum_bits.green == 5)
1130 green|=((green & 0xe000) >> 5);
1131 if (quantum_bits.green == 6)
1132 green|=((green & 0xc000) >> 6);
1133 if (quantum_bits.green <= 8)
1134 green|=((green & 0xff00) >> 8);
1135 blue=((pixel & bmp_info.blue_mask) << shift.blue) >> 16;
1136 if (quantum_bits.blue == 5)
1137 blue|=((blue & 0xe000) >> 5);
1138 if (quantum_bits.blue <= 8)
1139 blue|=((blue & 0xff00) >> 8);
1140 opacity=((pixel & bmp_info.alpha_mask) << shift.opacity) >> 16;
1141 if (quantum_bits.opacity <= 8)
1142 opacity|=((opacity & 0xff00) >> 8);
1143 q->red=ScaleShortToQuantum((unsigned short) red);
1144 q->green=ScaleShortToQuantum((unsigned short) green);
1145 q->blue=ScaleShortToQuantum((unsigned short) blue);
1146 q->opacity=OpaqueOpacity;
1147 if (image->matte != MagickFalse)
1148 q->opacity=ScaleShortToQuantum((unsigned short) opacity);
1151 if (SyncAuthenticPixels(image,exception) == MagickFalse)
1153 offset=(MagickOffsetType) (image->rows-y-1);
1154 if (image->previous == (Image *) NULL)
1156 status=SetImageProgress(image,LoadImageTag,y,image->rows);
1157 if (status == MagickFalse)
1166 Convert DirectColor scanline.
1168 bytes_per_line=4*((image->columns*24+31)/32);
1169 for (y=(long) image->rows-1; y >= 0; y--)
1171 p=pixels+(image->rows-y-1)*bytes_per_line;
1172 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
1173 if (q == (PixelPacket *) NULL)
1175 for (x=0; x < (long) image->columns; x++)
1177 q->blue=ScaleCharToQuantum(*p++);
1178 q->green=ScaleCharToQuantum(*p++);
1179 q->red=ScaleCharToQuantum(*p++);
1182 if (SyncAuthenticPixels(image,exception) == MagickFalse)
1184 offset=(MagickOffsetType) (image->rows-y-1);
1185 if (image->previous == (Image *) NULL)
1187 status=SetImageProgress(image,LoadImageTag,y,image->rows);
1188 if (status == MagickFalse)
1197 Convert bitfield encoded DirectColor scanline.
1199 if ((bmp_info.compression != BI_RGB) &&
1200 (bmp_info.compression != BI_BITFIELDS))
1201 ThrowReaderException(CorruptImageError,
1202 "UnrecognizedImageCompression");
1203 bytes_per_line=4*(image->columns);
1204 for (y=(long) image->rows-1; y >= 0; y--)
1209 p=pixels+(image->rows-y-1)*bytes_per_line;
1210 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
1211 if (q == (PixelPacket *) NULL)
1213 for (x=0; x < (long) image->columns; x++)
1215 pixel=(unsigned long) (*p++);
1217 pixel|=(*p++ << 16);
1218 pixel|=(*p++ << 24);
1219 red=((pixel & bmp_info.red_mask) << shift.red) >> 16;
1220 if (quantum_bits.red == 8)
1222 green=((pixel & bmp_info.green_mask) << shift.green) >> 16;
1223 if (quantum_bits.green == 8)
1224 green|=(green >> 8);
1225 blue=((pixel & bmp_info.blue_mask) << shift.blue) >> 16;
1226 if (quantum_bits.blue == 8)
1228 opacity=((pixel & bmp_info.alpha_mask) << shift.opacity) >> 16;
1229 if (quantum_bits.opacity == 8)
1230 opacity|=(opacity >> 8);
1231 q->red=ScaleShortToQuantum((unsigned short) red);
1232 q->green=ScaleShortToQuantum((unsigned short) green);
1233 q->blue=ScaleShortToQuantum((unsigned short) blue);
1234 q->opacity=OpaqueOpacity;
1235 if (image->matte != MagickFalse)
1236 q->opacity=ScaleShortToQuantum((unsigned short) opacity);
1239 if (SyncAuthenticPixels(image,exception) == MagickFalse)
1241 offset=(MagickOffsetType) (image->rows-y-1);
1242 if (image->previous == (Image *) NULL)
1244 status=SetImageProgress(image,LoadImageTag,y,image->rows);
1245 if (status == MagickFalse)
1252 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
1254 pixels=(unsigned char *) RelinquishMagickMemory(pixels);
1255 if (EOFBlob(image) != MagickFalse)
1257 ThrowFileException(exception,CorruptImageError,"UnexpectedEndOfFile",
1261 if (bmp_info.height < 0)
1267 Correct image orientation.
1269 flipped_image=FlipImage(image,exception);
1270 if (flipped_image == (Image *) NULL)
1272 image=DestroyImageList(image);
1273 return((Image *) NULL);
1275 DuplicateBlob(flipped_image,image);
1276 image=DestroyImage(image);
1277 image=flipped_image;
1280 Proceed to next image.
1282 if (image_info->number_scenes != 0)
1283 if (image->scene >= (image_info->scene+image_info->number_scenes-1))
1286 if (bmp_info.ba_offset != 0)
1288 offset=SeekBlob(image,(MagickOffsetType) bmp_info.ba_offset,SEEK_SET);
1290 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
1292 count=ReadBlob(image,2,magick);
1293 if ((count == 2) && (IsBMP(magick,2) != MagickFalse))
1296 Acquire next image structure.
1298 AcquireNextImage(image_info,image);
1299 if (GetNextImageInList(image) == (Image *) NULL)
1301 image=DestroyImageList(image);
1302 return((Image *) NULL);
1304 image=SyncNextImageInList(image);
1305 status=SetImageProgress(image,LoadImagesTag,TellBlob(image),
1306 GetBlobSize(image));
1307 if (status == MagickFalse)
1310 } while (IsBMP(magick,2) != MagickFalse);
1311 (void) CloseBlob(image);
1312 return(GetFirstImageInList(image));
1316 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1320 % R e g i s t e r B M P I m a g e %
1324 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1326 % RegisterBMPImage() adds attributes for the BMP image format to
1327 % the list of supported formats. The attributes include the image format
1328 % tag, a method to read and/or write the format, whether the format
1329 % supports the saving of more than one frame to the same file or blob,
1330 % whether the format supports native in-memory I/O, and a brief
1331 % description of the format.
1333 % The format of the RegisterBMPImage method is:
1335 % unsigned long RegisterBMPImage(void)
1338 ModuleExport unsigned long RegisterBMPImage(void)
1343 entry=SetMagickInfo("BMP");
1344 entry->decoder=(DecodeImageHandler *) ReadBMPImage;
1345 entry->encoder=(EncodeImageHandler *) WriteBMPImage;
1346 entry->magick=(IsImageFormatHandler *) IsBMP;
1347 entry->description=ConstantString("Microsoft Windows bitmap image");
1348 entry->module=ConstantString("BMP");
1349 entry->adjoin=MagickFalse;
1350 entry->seekable_stream=MagickTrue;
1351 (void) RegisterMagickInfo(entry);
1352 entry=SetMagickInfo("BMP2");
1353 entry->encoder=(EncodeImageHandler *) WriteBMPImage;
1354 entry->magick=(IsImageFormatHandler *) IsBMP;
1355 entry->description=ConstantString("Microsoft Windows bitmap image v2");
1356 entry->module=ConstantString("BMP");
1357 entry->adjoin=MagickFalse;
1358 entry->seekable_stream=MagickTrue;
1359 (void) RegisterMagickInfo(entry);
1360 entry=SetMagickInfo("BMP3");
1361 entry->encoder=(EncodeImageHandler *) WriteBMPImage;
1362 entry->magick=(IsImageFormatHandler *) IsBMP;
1363 entry->description=ConstantString("Microsoft Windows bitmap image v3");
1364 entry->module=ConstantString("BMP");
1365 entry->adjoin=MagickFalse;
1366 entry->seekable_stream=MagickTrue;
1367 (void) RegisterMagickInfo(entry);
1368 return(MagickImageCoderSignature);
1372 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1376 % U n r e g i s t e r B M P I m a g e %
1380 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1382 % UnregisterBMPImage() removes format registrations made by the
1383 % BMP module from the list of supported formats.
1385 % The format of the UnregisterBMPImage method is:
1387 % UnregisterBMPImage(void)
1390 ModuleExport void UnregisterBMPImage(void)
1392 (void) UnregisterMagickInfo("BMP");
1393 (void) UnregisterMagickInfo("BMP2");
1394 (void) UnregisterMagickInfo("BMP3");
1398 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1402 % W r i t e B M P I m a g e %
1406 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1408 % WriteBMPImage() writes an image in Microsoft Windows bitmap encoded
1409 % image format, version 3 for Windows or (if the image has a matte channel)
1412 % The format of the WriteBMPImage method is:
1414 % MagickBooleanType WriteBMPImage(const ImageInfo *image_info,Image *image)
1416 % A description of each parameter follows.
1418 % o image_info: the image info.
1420 % o image: The image.
1423 static MagickBooleanType WriteBMPImage(const ImageInfo *image_info,Image *image)
1441 register const IndexPacket
1444 register const PixelPacket
1451 register unsigned char
1463 Open output image file.
1465 assert(image_info != (const ImageInfo *) NULL);
1466 assert(image_info->signature == MagickSignature);
1467 assert(image != (Image *) NULL);
1468 assert(image->signature == MagickSignature);
1469 if (image->debug != MagickFalse)
1470 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1471 status=OpenBlob(image_info,image,WriteBinaryBlobMode,&image->exception);
1472 if (status == MagickFalse)
1475 if (LocaleCompare(image_info->magick,"BMP2") == 0)
1478 if (LocaleCompare(image_info->magick,"BMP3") == 0)
1484 Initialize BMP raster file header.
1486 if (image->colorspace != RGBColorspace)
1487 (void) TransformImageColorspace(image,RGBColorspace);
1488 (void) ResetMagickMemory(&bmp_info,0,sizeof(bmp_info));
1489 bmp_info.file_size=14+12;
1491 bmp_info.file_size+=28;
1492 bmp_info.offset_bits=bmp_info.file_size;
1493 bmp_info.compression=BI_RGB;
1494 if ((image->storage_class == PseudoClass) && (image->colors > 256))
1495 (void) SetImageStorageClass(image,DirectClass);
1496 if (image->storage_class != DirectClass)
1499 Colormapped BMP raster.
1501 bmp_info.bits_per_pixel=8;
1502 if (image->colors <= 2)
1503 bmp_info.bits_per_pixel=1;
1505 if (image->colors <= 16)
1506 bmp_info.bits_per_pixel=4;
1508 if (image->colors <= 256)
1509 bmp_info.bits_per_pixel=8;
1510 if (image_info->compression == RLECompression)
1511 bmp_info.bits_per_pixel=8;
1512 bmp_info.number_colors=1U << bmp_info.bits_per_pixel;
1513 if (image->matte != MagickFalse)
1514 (void) SetImageStorageClass(image,DirectClass);
1516 if ((unsigned long) bmp_info.number_colors < image->colors)
1517 (void) SetImageStorageClass(image,DirectClass);
1520 bmp_info.file_size+=3*(1UL << bmp_info.bits_per_pixel);
1521 bmp_info.offset_bits+=3*(1UL << bmp_info.bits_per_pixel);
1524 bmp_info.file_size+=(1UL << bmp_info.bits_per_pixel);
1525 bmp_info.offset_bits+=(1UL << bmp_info.bits_per_pixel);
1529 if (image->storage_class == DirectClass)
1532 Full color BMP raster.
1534 bmp_info.number_colors=0;
1535 bmp_info.bits_per_pixel=(unsigned short)
1536 ((type > 3) && (image->matte != MagickFalse) ? 32 : 24);
1537 bmp_info.compression=(unsigned int) ((type > 3) &&
1538 (image->matte != MagickFalse) ? BI_BITFIELDS : BI_RGB);
1540 bytes_per_line=4*((image->columns*bmp_info.bits_per_pixel+31)/32);
1541 bmp_info.ba_offset=0;
1542 profile=GetImageProfile(image,"icc");
1543 have_color_info=(image->rendering_intent != UndefinedIntent) ||
1544 (profile != (StringInfo *) NULL) || (image->gamma != 0.0) ? MagickTrue :
1549 if ((type == 3) || ((image->matte == MagickFalse) &&
1550 (have_color_info == MagickFalse)))
1562 if ((image->rendering_intent != UndefinedIntent) ||
1563 (profile != (StringInfo *) NULL))
1568 bmp_info.file_size+=extra_size;
1569 bmp_info.offset_bits+=extra_size;
1571 bmp_info.width=(long) image->columns;
1572 bmp_info.height=(long) image->rows;
1574 bmp_info.image_size=(unsigned int) (bytes_per_line*image->rows);
1575 bmp_info.file_size+=bmp_info.image_size;
1576 bmp_info.x_pixels=75*39;
1577 bmp_info.y_pixels=75*39;
1578 switch (image->units)
1580 case UndefinedResolution:
1581 case PixelsPerInchResolution:
1583 bmp_info.x_pixels=(unsigned int) (100.0*image->x_resolution/2.54);
1584 bmp_info.y_pixels=(unsigned int) (100.0*image->y_resolution/2.54);
1587 case PixelsPerCentimeterResolution:
1589 bmp_info.x_pixels=(unsigned int) (100.0*image->x_resolution);
1590 bmp_info.y_pixels=(unsigned int) (100.0*image->y_resolution);
1594 bmp_info.colors_important=bmp_info.number_colors;
1596 Convert MIFF to BMP raster pixels.
1598 pixels=(unsigned char *) AcquireQuantumMemory((size_t) bmp_info.image_size,
1600 if (pixels == (unsigned char *) NULL)
1601 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
1602 (void) ResetMagickMemory(pixels,0,(size_t) bmp_info.image_size);
1603 switch (bmp_info.bits_per_pixel)
1612 Convert PseudoClass image to a BMP monochrome image.
1614 for (y=0; y < (long) image->rows; y++)
1616 p=GetVirtualPixels(image,0,y,image->columns,1,&image->exception);
1617 if (p == (const PixelPacket *) NULL)
1619 indexes=GetVirtualIndexQueue(image);
1620 q=pixels+(image->rows-y-1)*bytes_per_line;
1623 for (x=0; x < (long) image->columns; x++)
1626 byte|=indexes[x] != 0 ? 0x01 : 0x00;
1630 *q++=(unsigned char) byte;
1637 *q++=(unsigned char) (byte << (8-bit));
1640 for (x=(long) (image->columns+7)/8; x < (long) bytes_per_line; x++)
1642 if (image->previous == (Image *) NULL)
1644 status=SetImageProgress(image,SaveImageTag,y,image->rows);
1645 if (status == MagickFalse)
1658 Convert PseudoClass image to a BMP monochrome image.
1660 for (y=0; y < (long) image->rows; y++)
1662 p=GetVirtualPixels(image,0,y,image->columns,1,&image->exception);
1663 if (p == (const PixelPacket *) NULL)
1665 indexes=GetVirtualIndexQueue(image);
1666 q=pixels+(image->rows-y-1)*bytes_per_line;
1669 for (x=0; x < (long) image->columns; x++)
1672 byte|=((unsigned long) indexes[x] & 0x0f);
1676 *q++=(unsigned char) byte;
1683 *q++=(unsigned char) (byte << 4);
1686 for (x=(long) (image->columns+1)/2; x < (long) bytes_per_line; x++)
1688 if (image->previous == (Image *) NULL)
1690 status=SetImageProgress(image,SaveImageTag,y,image->rows);
1691 if (status == MagickFalse)
1700 Convert PseudoClass packet to BMP pixel.
1702 for (y=0; y < (long) image->rows; y++)
1704 p=GetVirtualPixels(image,0,y,image->columns,1,&image->exception);
1705 if (p == (const PixelPacket *) NULL)
1707 indexes=GetVirtualIndexQueue(image);
1708 q=pixels+(image->rows-y-1)*bytes_per_line;
1709 for (x=0; x < (long) image->columns; x++)
1710 *q++=(unsigned char) indexes[x];
1711 for ( ; x < (long) bytes_per_line; x++)
1713 if (image->previous == (Image *) NULL)
1715 status=SetImageProgress(image,SaveImageTag,y,image->rows);
1716 if (status == MagickFalse)
1725 Convert DirectClass packet to BMP BGR888.
1727 for (y=0; y < (long) image->rows; y++)
1729 p=GetVirtualPixels(image,0,y,image->columns,1,&image->exception);
1730 if (p == (const PixelPacket *) NULL)
1732 q=pixels+(image->rows-y-1)*bytes_per_line;
1733 for (x=0; x < (long) image->columns; x++)
1735 *q++=ScaleQuantumToChar(p->blue);
1736 *q++=ScaleQuantumToChar(p->green);
1737 *q++=ScaleQuantumToChar(p->red);
1740 for (x=3L*(long) image->columns; x < (long) bytes_per_line; x++)
1742 if (image->previous == (Image *) NULL)
1744 status=SetImageProgress(image,SaveImageTag,y,image->rows);
1745 if (status == MagickFalse)
1754 Convert DirectClass packet to ARGB8888 pixel.
1756 for (y=0; y < (long) image->rows; y++)
1758 p=GetVirtualPixels(image,0,y,image->columns,1,&image->exception);
1759 if (p == (const PixelPacket *) NULL)
1761 q=pixels+(image->rows-y-1)*bytes_per_line;
1762 for (x=0; x < (long) image->columns; x++)
1764 *q++=ScaleQuantumToChar(p->blue);
1765 *q++=ScaleQuantumToChar(p->green);
1766 *q++=ScaleQuantumToChar(p->red);
1767 *q++=ScaleQuantumToChar(p->opacity);
1770 if (image->previous == (Image *) NULL)
1772 status=SetImageProgress(image,SaveImageTag,y,image->rows);
1773 if (status == MagickFalse)
1780 if ((type > 2) && (bmp_info.bits_per_pixel == 8))
1781 if (image_info->compression != NoCompression)
1787 Convert run-length encoded raster pixels.
1789 length=(size_t) (2*(bytes_per_line+2)*(image->rows+2)+2);
1790 bmp_data=(unsigned char *) NULL;
1791 if (~length >= bytes_per_line)
1792 bmp_data=(unsigned char *) AcquireQuantumMemory(length+
1793 bytes_per_line,sizeof(*bmp_data));
1794 if (bmp_data == (unsigned char *) NULL)
1796 pixels=(unsigned char *) RelinquishMagickMemory(pixels);
1797 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
1799 bmp_info.file_size-=bmp_info.image_size;
1800 bmp_info.image_size=(unsigned int) EncodeImage(image,bytes_per_line,
1802 bmp_info.file_size+=bmp_info.image_size;
1803 pixels=(unsigned char *) RelinquishMagickMemory(pixels);
1805 bmp_info.compression=BI_RLE8;
1808 Write BMP for Windows, all versions, 14-byte header.
1810 if (image->debug != MagickFalse)
1812 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1813 " Writing BMP version %ld datastream",type);
1814 if (image->storage_class == DirectClass)
1815 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1816 " Storage class=DirectClass");
1818 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1819 " Storage class=PseudoClass");
1820 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1821 " Image depth=%lu",image->depth);
1822 if (image->matte != MagickFalse)
1823 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1826 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1827 " Matte=MagickFalse");
1828 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1829 " BMP bits_per_pixel=%d",bmp_info.bits_per_pixel);
1830 switch ((int) bmp_info.compression)
1834 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1835 " Compression=BI_RGB");
1840 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1841 " Compression=BI_RLE8");
1846 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1847 " Compression=BI_BITFIELDS");
1852 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1853 " Compression=UNKNOWN (%u)",bmp_info.compression);
1857 if (bmp_info.number_colors == 0)
1858 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1859 " Number_colors=unspecified");
1861 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1862 " Number_colors=%u",bmp_info.number_colors);
1864 (void) WriteBlob(image,2,(unsigned char *) "BM");
1865 (void) WriteBlobLSBLong(image,bmp_info.file_size);
1866 (void) WriteBlobLSBLong(image,bmp_info.ba_offset); /* always 0 */
1867 (void) WriteBlobLSBLong(image,bmp_info.offset_bits);
1871 Write 12-byte version 2 bitmap header.
1873 (void) WriteBlobLSBLong(image,bmp_info.size);
1874 (void) WriteBlobLSBShort(image,(unsigned short) bmp_info.width);
1875 (void) WriteBlobLSBShort(image,(unsigned short) bmp_info.height);
1876 (void) WriteBlobLSBShort(image,bmp_info.planes);
1877 (void) WriteBlobLSBShort(image,bmp_info.bits_per_pixel);
1882 Write 40-byte version 3+ bitmap header.
1884 (void) WriteBlobLSBLong(image,bmp_info.size);
1885 (void) WriteBlobLSBLong(image,(unsigned int) bmp_info.width);
1886 (void) WriteBlobLSBLong(image,(unsigned int) bmp_info.height);
1887 (void) WriteBlobLSBShort(image,bmp_info.planes);
1888 (void) WriteBlobLSBShort(image,bmp_info.bits_per_pixel);
1889 (void) WriteBlobLSBLong(image,bmp_info.compression);
1890 (void) WriteBlobLSBLong(image,bmp_info.image_size);
1891 (void) WriteBlobLSBLong(image,bmp_info.x_pixels);
1892 (void) WriteBlobLSBLong(image,bmp_info.y_pixels);
1893 (void) WriteBlobLSBLong(image,bmp_info.number_colors);
1894 (void) WriteBlobLSBLong(image,bmp_info.colors_important);
1896 if ((type > 3) && ((image->matte != MagickFalse) ||
1897 (have_color_info != MagickFalse)))
1900 Write the rest of the 108-byte BMP Version 4 header.
1902 (void) WriteBlobLSBLong(image,0x00ff0000U); /* Red mask */
1903 (void) WriteBlobLSBLong(image,0x0000ff00U); /* Green mask */
1904 (void) WriteBlobLSBLong(image,0x000000ffU); /* Blue mask */
1905 (void) WriteBlobLSBLong(image,0xff000000U); /* Alpha mask */
1906 (void) WriteBlobLSBLong(image,0x00000001U); /* CSType==Calib. RGB */
1907 (void) WriteBlobLSBLong(image,(unsigned int)
1908 image->chromaticity.red_primary.x*0x3ffffff);
1909 (void) WriteBlobLSBLong(image,(unsigned int)
1910 image->chromaticity.red_primary.y*0x3ffffff);
1911 (void) WriteBlobLSBLong(image,(unsigned int)
1912 (1.000f-(image->chromaticity.red_primary.x+
1913 image->chromaticity.red_primary.y)*0x3ffffff));
1914 (void) WriteBlobLSBLong(image,(unsigned int)
1915 image->chromaticity.green_primary.x*0x3ffffff);
1916 (void) WriteBlobLSBLong(image,(unsigned int)
1917 image->chromaticity.green_primary.y*0x3ffffff);
1918 (void) WriteBlobLSBLong(image,(unsigned int)
1919 (1.000f-(image->chromaticity.green_primary.x+
1920 image->chromaticity.green_primary.y)*0x3ffffff));
1921 (void) WriteBlobLSBLong(image,(unsigned int)
1922 image->chromaticity.blue_primary.x*0x3ffffff);
1923 (void) WriteBlobLSBLong(image,(unsigned int)
1924 image->chromaticity.blue_primary.y*0x3ffffff);
1925 (void) WriteBlobLSBLong(image,(unsigned int)
1926 (1.000f-(image->chromaticity.blue_primary.x+
1927 image->chromaticity.blue_primary.y)*0x3ffffff));
1928 (void) WriteBlobLSBLong(image,(unsigned int)
1929 bmp_info.gamma_scale.x*0xffff);
1930 (void) WriteBlobLSBLong(image,(unsigned int)
1931 bmp_info.gamma_scale.y*0xffff);
1932 (void) WriteBlobLSBLong(image,(unsigned int)
1933 bmp_info.gamma_scale.z*0xffff);
1934 if ((image->rendering_intent != UndefinedIntent) ||
1935 (profile != (StringInfo *) NULL))
1940 switch ((int) image->rendering_intent)
1942 case SaturationIntent:
1944 intent=LCS_GM_BUSINESS;
1947 case RelativeIntent:
1949 intent=LCS_GM_GRAPHICS;
1952 case PerceptualIntent:
1954 intent=LCS_GM_IMAGES;
1957 case AbsoluteIntent:
1959 intent=LCS_GM_ABS_COLORIMETRIC;
1968 (void) WriteBlobLSBLong(image,(unsigned int) intent);
1969 (void) WriteBlobLSBLong(image,0x00); /* dummy profile data */
1970 (void) WriteBlobLSBLong(image,0x00); /* dummy profile length */
1971 (void) WriteBlobLSBLong(image,0x00); /* reserved */
1974 if (image->storage_class == PseudoClass)
1980 Dump colormap to file.
1982 if (image->debug != MagickFalse)
1983 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1984 " Colormap: %ld entries",image->colors);
1985 bmp_colormap=(unsigned char *) AcquireQuantumMemory((size_t) (1UL <<
1986 bmp_info.bits_per_pixel),4*sizeof(*bmp_colormap));
1987 if (bmp_colormap == (unsigned char *) NULL)
1988 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
1990 for (i=0; i < (long) MagickMin((long) image->colors,(long) bmp_info.number_colors); i++)
1992 *q++=ScaleQuantumToChar(image->colormap[i].blue);
1993 *q++=ScaleQuantumToChar(image->colormap[i].green);
1994 *q++=ScaleQuantumToChar(image->colormap[i].red);
1996 *q++=(unsigned char) 0x0;
1998 for ( ; i < (long) (1UL << bmp_info.bits_per_pixel); i++)
2000 *q++=(unsigned char) 0x00;
2001 *q++=(unsigned char) 0x00;
2002 *q++=(unsigned char) 0x00;
2004 *q++=(unsigned char) 0x00;
2007 (void) WriteBlob(image,(size_t) (3*(1L << bmp_info.bits_per_pixel)),
2010 (void) WriteBlob(image,(size_t) (4*(1L << bmp_info.bits_per_pixel)),
2012 bmp_colormap=(unsigned char *) RelinquishMagickMemory(bmp_colormap);
2014 if (image->debug != MagickFalse)
2015 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
2016 " Pixels: %u bytes",bmp_info.image_size);
2017 (void) WriteBlob(image,(size_t) bmp_info.image_size,pixels);
2018 pixels=(unsigned char *) RelinquishMagickMemory(pixels);
2019 if (GetNextImageInList(image) == (Image *) NULL)
2021 image=SyncNextImageInList(image);
2022 status=SetImageProgress(image,SaveImagesTag,scene++,
2023 GetImageListLength(image));
2024 if (status == MagickFalse)
2026 } while (image_info->adjoin != MagickFalse);
2027 (void) CloseBlob(image);