2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
13 % Read/Write Microsoft Windows Bitmap Image Format %
17 % Glenn Randers-Pehrson %
21 % Copyright 1999-2010 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 SetOpacityPixelComponent(q,OpaqueOpacity);
1147 if (image->matte != MagickFalse)
1148 q->opacity=ScaleShortToQuantum((unsigned short) (65535-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 SetOpacityPixelComponent(q,OpaqueOpacity);
1235 if (image->matte != MagickFalse)
1236 q->opacity=ScaleShortToQuantum((unsigned short) (65535-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 DuplicateBlob(flipped_image,image);
1273 image=DestroyImage(image);
1274 image=flipped_image;
1278 Proceed to next image.
1280 if (image_info->number_scenes != 0)
1281 if (image->scene >= (image_info->scene+image_info->number_scenes-1))
1284 if (bmp_info.ba_offset != 0)
1286 offset=SeekBlob(image,(MagickOffsetType) bmp_info.ba_offset,SEEK_SET);
1288 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
1290 count=ReadBlob(image,2,magick);
1291 if ((count == 2) && (IsBMP(magick,2) != MagickFalse))
1294 Acquire next image structure.
1296 AcquireNextImage(image_info,image);
1297 if (GetNextImageInList(image) == (Image *) NULL)
1299 image=DestroyImageList(image);
1300 return((Image *) NULL);
1302 image=SyncNextImageInList(image);
1303 status=SetImageProgress(image,LoadImagesTag,TellBlob(image),
1304 GetBlobSize(image));
1305 if (status == MagickFalse)
1308 } while (IsBMP(magick,2) != MagickFalse);
1309 (void) CloseBlob(image);
1310 return(GetFirstImageInList(image));
1314 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1318 % R e g i s t e r B M P I m a g e %
1322 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1324 % RegisterBMPImage() adds attributes for the BMP image format to
1325 % the list of supported formats. The attributes include the image format
1326 % tag, a method to read and/or write the format, whether the format
1327 % supports the saving of more than one frame to the same file or blob,
1328 % whether the format supports native in-memory I/O, and a brief
1329 % description of the format.
1331 % The format of the RegisterBMPImage method is:
1333 % unsigned long RegisterBMPImage(void)
1336 ModuleExport unsigned long RegisterBMPImage(void)
1341 entry=SetMagickInfo("BMP");
1342 entry->decoder=(DecodeImageHandler *) ReadBMPImage;
1343 entry->encoder=(EncodeImageHandler *) WriteBMPImage;
1344 entry->magick=(IsImageFormatHandler *) IsBMP;
1345 entry->description=ConstantString("Microsoft Windows bitmap image");
1346 entry->module=ConstantString("BMP");
1347 entry->adjoin=MagickFalse;
1348 entry->seekable_stream=MagickTrue;
1349 (void) RegisterMagickInfo(entry);
1350 entry=SetMagickInfo("BMP2");
1351 entry->encoder=(EncodeImageHandler *) WriteBMPImage;
1352 entry->magick=(IsImageFormatHandler *) IsBMP;
1353 entry->description=ConstantString("Microsoft Windows bitmap image (V2)");
1354 entry->module=ConstantString("BMP");
1355 entry->adjoin=MagickFalse;
1356 entry->seekable_stream=MagickTrue;
1357 (void) RegisterMagickInfo(entry);
1358 entry=SetMagickInfo("BMP3");
1359 entry->encoder=(EncodeImageHandler *) WriteBMPImage;
1360 entry->magick=(IsImageFormatHandler *) IsBMP;
1361 entry->description=ConstantString("Microsoft Windows bitmap image (V3)");
1362 entry->module=ConstantString("BMP");
1363 entry->adjoin=MagickFalse;
1364 entry->seekable_stream=MagickTrue;
1365 (void) RegisterMagickInfo(entry);
1366 return(MagickImageCoderSignature);
1370 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1374 % U n r e g i s t e r B M P I m a g e %
1378 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1380 % UnregisterBMPImage() removes format registrations made by the
1381 % BMP module from the list of supported formats.
1383 % The format of the UnregisterBMPImage method is:
1385 % UnregisterBMPImage(void)
1388 ModuleExport void UnregisterBMPImage(void)
1390 (void) UnregisterMagickInfo("BMP");
1391 (void) UnregisterMagickInfo("BMP2");
1392 (void) UnregisterMagickInfo("BMP3");
1396 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1400 % W r i t e B M P I m a g e %
1404 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1406 % WriteBMPImage() writes an image in Microsoft Windows bitmap encoded
1407 % image format, version 3 for Windows or (if the image has a matte channel)
1410 % The format of the WriteBMPImage method is:
1412 % MagickBooleanType WriteBMPImage(const ImageInfo *image_info,Image *image)
1414 % A description of each parameter follows.
1416 % o image_info: the image info.
1418 % o image: The image.
1421 static MagickBooleanType WriteBMPImage(const ImageInfo *image_info,Image *image)
1439 register const IndexPacket
1442 register const PixelPacket
1449 register unsigned char
1461 Open output image file.
1463 assert(image_info != (const ImageInfo *) NULL);
1464 assert(image_info->signature == MagickSignature);
1465 assert(image != (Image *) NULL);
1466 assert(image->signature == MagickSignature);
1467 if (image->debug != MagickFalse)
1468 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1469 status=OpenBlob(image_info,image,WriteBinaryBlobMode,&image->exception);
1470 if (status == MagickFalse)
1473 if (LocaleCompare(image_info->magick,"BMP2") == 0)
1476 if (LocaleCompare(image_info->magick,"BMP3") == 0)
1482 Initialize BMP raster file header.
1484 if (image->colorspace != RGBColorspace)
1485 (void) TransformImageColorspace(image,RGBColorspace);
1486 (void) ResetMagickMemory(&bmp_info,0,sizeof(bmp_info));
1487 bmp_info.file_size=14+12;
1489 bmp_info.file_size+=28;
1490 bmp_info.offset_bits=bmp_info.file_size;
1491 bmp_info.compression=BI_RGB;
1492 if ((image->storage_class == PseudoClass) && (image->colors > 256))
1493 (void) SetImageStorageClass(image,DirectClass);
1494 if (image->storage_class != DirectClass)
1497 Colormapped BMP raster.
1499 bmp_info.bits_per_pixel=8;
1500 if (image->colors <= 2)
1501 bmp_info.bits_per_pixel=1;
1503 if (image->colors <= 16)
1504 bmp_info.bits_per_pixel=4;
1506 if (image->colors <= 256)
1507 bmp_info.bits_per_pixel=8;
1508 if (image_info->compression == RLECompression)
1509 bmp_info.bits_per_pixel=8;
1510 bmp_info.number_colors=1U << bmp_info.bits_per_pixel;
1511 if (image->matte != MagickFalse)
1512 (void) SetImageStorageClass(image,DirectClass);
1514 if ((unsigned long) bmp_info.number_colors < image->colors)
1515 (void) SetImageStorageClass(image,DirectClass);
1518 bmp_info.file_size+=3*(1UL << bmp_info.bits_per_pixel);
1519 bmp_info.offset_bits+=3*(1UL << bmp_info.bits_per_pixel);
1522 bmp_info.file_size+=(1UL << bmp_info.bits_per_pixel);
1523 bmp_info.offset_bits+=(1UL << bmp_info.bits_per_pixel);
1527 if (image->storage_class == DirectClass)
1530 Full color BMP raster.
1532 bmp_info.number_colors=0;
1533 bmp_info.bits_per_pixel=(unsigned short)
1534 ((type > 3) && (image->matte != MagickFalse) ? 32 : 24);
1535 bmp_info.compression=(unsigned int) ((type > 3) &&
1536 (image->matte != MagickFalse) ? BI_BITFIELDS : BI_RGB);
1538 bytes_per_line=4*((image->columns*bmp_info.bits_per_pixel+31)/32);
1539 bmp_info.ba_offset=0;
1540 profile=GetImageProfile(image,"icc");
1541 have_color_info=(image->rendering_intent != UndefinedIntent) ||
1542 (profile != (StringInfo *) NULL) || (image->gamma != 0.0) ? MagickTrue :
1547 if ((type == 3) || ((image->matte == MagickFalse) &&
1548 (have_color_info == MagickFalse)))
1560 if ((image->rendering_intent != UndefinedIntent) ||
1561 (profile != (StringInfo *) NULL))
1566 bmp_info.file_size+=extra_size;
1567 bmp_info.offset_bits+=extra_size;
1569 bmp_info.width=(long) image->columns;
1570 bmp_info.height=(long) image->rows;
1572 bmp_info.image_size=(unsigned int) (bytes_per_line*image->rows);
1573 bmp_info.file_size+=bmp_info.image_size;
1574 bmp_info.x_pixels=75*39;
1575 bmp_info.y_pixels=75*39;
1576 switch (image->units)
1578 case UndefinedResolution:
1579 case PixelsPerInchResolution:
1581 bmp_info.x_pixels=(unsigned int) (100.0*image->x_resolution/2.54);
1582 bmp_info.y_pixels=(unsigned int) (100.0*image->y_resolution/2.54);
1585 case PixelsPerCentimeterResolution:
1587 bmp_info.x_pixels=(unsigned int) (100.0*image->x_resolution);
1588 bmp_info.y_pixels=(unsigned int) (100.0*image->y_resolution);
1592 bmp_info.colors_important=bmp_info.number_colors;
1594 Convert MIFF to BMP raster pixels.
1596 pixels=(unsigned char *) AcquireQuantumMemory((size_t) bmp_info.image_size,
1598 if (pixels == (unsigned char *) NULL)
1599 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
1600 (void) ResetMagickMemory(pixels,0,(size_t) bmp_info.image_size);
1601 switch (bmp_info.bits_per_pixel)
1610 Convert PseudoClass image to a BMP monochrome image.
1612 for (y=0; y < (long) image->rows; y++)
1614 p=GetVirtualPixels(image,0,y,image->columns,1,&image->exception);
1615 if (p == (const PixelPacket *) NULL)
1617 indexes=GetVirtualIndexQueue(image);
1618 q=pixels+(image->rows-y-1)*bytes_per_line;
1621 for (x=0; x < (long) image->columns; x++)
1624 byte|=indexes[x] != 0 ? 0x01 : 0x00;
1628 *q++=(unsigned char) byte;
1635 *q++=(unsigned char) (byte << (8-bit));
1638 for (x=(long) (image->columns+7)/8; x < (long) bytes_per_line; x++)
1640 if (image->previous == (Image *) NULL)
1642 status=SetImageProgress(image,SaveImageTag,y,image->rows);
1643 if (status == MagickFalse)
1656 Convert PseudoClass image to a BMP monochrome image.
1658 for (y=0; y < (long) image->rows; y++)
1660 p=GetVirtualPixels(image,0,y,image->columns,1,&image->exception);
1661 if (p == (const PixelPacket *) NULL)
1663 indexes=GetVirtualIndexQueue(image);
1664 q=pixels+(image->rows-y-1)*bytes_per_line;
1667 for (x=0; x < (long) image->columns; x++)
1670 byte|=((unsigned long) indexes[x] & 0x0f);
1674 *q++=(unsigned char) byte;
1681 *q++=(unsigned char) (byte << 4);
1684 for (x=(long) (image->columns+1)/2; x < (long) bytes_per_line; x++)
1686 if (image->previous == (Image *) NULL)
1688 status=SetImageProgress(image,SaveImageTag,y,image->rows);
1689 if (status == MagickFalse)
1698 Convert PseudoClass packet to BMP pixel.
1700 for (y=0; y < (long) image->rows; y++)
1702 p=GetVirtualPixels(image,0,y,image->columns,1,&image->exception);
1703 if (p == (const PixelPacket *) NULL)
1705 indexes=GetVirtualIndexQueue(image);
1706 q=pixels+(image->rows-y-1)*bytes_per_line;
1707 for (x=0; x < (long) image->columns; x++)
1708 *q++=(unsigned char) indexes[x];
1709 for ( ; x < (long) bytes_per_line; x++)
1711 if (image->previous == (Image *) NULL)
1713 status=SetImageProgress(image,SaveImageTag,y,image->rows);
1714 if (status == MagickFalse)
1723 Convert DirectClass packet to BMP BGR888.
1725 for (y=0; y < (long) image->rows; y++)
1727 p=GetVirtualPixels(image,0,y,image->columns,1,&image->exception);
1728 if (p == (const PixelPacket *) NULL)
1730 q=pixels+(image->rows-y-1)*bytes_per_line;
1731 for (x=0; x < (long) image->columns; x++)
1733 *q++=ScaleQuantumToChar(GetBluePixelComponent(p));
1734 *q++=ScaleQuantumToChar(GetGreenPixelComponent(p));
1735 *q++=ScaleQuantumToChar(GetRedPixelComponent(p));
1738 for (x=3L*(long) image->columns; x < (long) bytes_per_line; x++)
1740 if (image->previous == (Image *) NULL)
1742 status=SetImageProgress(image,SaveImageTag,y,image->rows);
1743 if (status == MagickFalse)
1752 Convert DirectClass packet to ARGB8888 pixel.
1754 for (y=0; y < (long) image->rows; y++)
1756 p=GetVirtualPixels(image,0,y,image->columns,1,&image->exception);
1757 if (p == (const PixelPacket *) NULL)
1759 q=pixels+(image->rows-y-1)*bytes_per_line;
1760 for (x=0; x < (long) image->columns; x++)
1762 *q++=ScaleQuantumToChar(GetBluePixelComponent(p));
1763 *q++=ScaleQuantumToChar(GetGreenPixelComponent(p));
1764 *q++=ScaleQuantumToChar(GetRedPixelComponent(p));
1765 *q++=ScaleQuantumToChar(QuantumRange-GetOpacityPixelComponent(p));
1768 if (image->previous == (Image *) NULL)
1770 status=SetImageProgress(image,SaveImageTag,y,image->rows);
1771 if (status == MagickFalse)
1778 if ((type > 2) && (bmp_info.bits_per_pixel == 8))
1779 if (image_info->compression != NoCompression)
1785 Convert run-length encoded raster pixels.
1787 length=(size_t) (2*(bytes_per_line+2)*(image->rows+2)+2);
1788 bmp_data=(unsigned char *) NULL;
1789 if (~length >= bytes_per_line)
1790 bmp_data=(unsigned char *) AcquireQuantumMemory(length+
1791 bytes_per_line,sizeof(*bmp_data));
1792 if (bmp_data == (unsigned char *) NULL)
1794 pixels=(unsigned char *) RelinquishMagickMemory(pixels);
1795 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
1797 bmp_info.file_size-=bmp_info.image_size;
1798 bmp_info.image_size=(unsigned int) EncodeImage(image,bytes_per_line,
1800 bmp_info.file_size+=bmp_info.image_size;
1801 pixels=(unsigned char *) RelinquishMagickMemory(pixels);
1803 bmp_info.compression=BI_RLE8;
1806 Write BMP for Windows, all versions, 14-byte header.
1808 if (image->debug != MagickFalse)
1810 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1811 " Writing BMP version %ld datastream",type);
1812 if (image->storage_class == DirectClass)
1813 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1814 " Storage class=DirectClass");
1816 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1817 " Storage class=PseudoClass");
1818 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1819 " Image depth=%lu",image->depth);
1820 if (image->matte != MagickFalse)
1821 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1824 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1825 " Matte=MagickFalse");
1826 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1827 " BMP bits_per_pixel=%d",bmp_info.bits_per_pixel);
1828 switch ((int) bmp_info.compression)
1832 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1833 " Compression=BI_RGB");
1838 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1839 " Compression=BI_RLE8");
1844 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1845 " Compression=BI_BITFIELDS");
1850 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1851 " Compression=UNKNOWN (%u)",bmp_info.compression);
1855 if (bmp_info.number_colors == 0)
1856 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1857 " Number_colors=unspecified");
1859 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1860 " Number_colors=%u",bmp_info.number_colors);
1862 (void) WriteBlob(image,2,(unsigned char *) "BM");
1863 (void) WriteBlobLSBLong(image,bmp_info.file_size);
1864 (void) WriteBlobLSBLong(image,bmp_info.ba_offset); /* always 0 */
1865 (void) WriteBlobLSBLong(image,bmp_info.offset_bits);
1869 Write 12-byte version 2 bitmap header.
1871 (void) WriteBlobLSBLong(image,bmp_info.size);
1872 (void) WriteBlobLSBShort(image,(unsigned short) bmp_info.width);
1873 (void) WriteBlobLSBShort(image,(unsigned short) bmp_info.height);
1874 (void) WriteBlobLSBShort(image,bmp_info.planes);
1875 (void) WriteBlobLSBShort(image,bmp_info.bits_per_pixel);
1880 Write 40-byte version 3+ bitmap header.
1882 (void) WriteBlobLSBLong(image,bmp_info.size);
1883 (void) WriteBlobLSBLong(image,(unsigned int) bmp_info.width);
1884 (void) WriteBlobLSBLong(image,(unsigned int) bmp_info.height);
1885 (void) WriteBlobLSBShort(image,bmp_info.planes);
1886 (void) WriteBlobLSBShort(image,bmp_info.bits_per_pixel);
1887 (void) WriteBlobLSBLong(image,bmp_info.compression);
1888 (void) WriteBlobLSBLong(image,bmp_info.image_size);
1889 (void) WriteBlobLSBLong(image,bmp_info.x_pixels);
1890 (void) WriteBlobLSBLong(image,bmp_info.y_pixels);
1891 (void) WriteBlobLSBLong(image,bmp_info.number_colors);
1892 (void) WriteBlobLSBLong(image,bmp_info.colors_important);
1894 if ((type > 3) && ((image->matte != MagickFalse) ||
1895 (have_color_info != MagickFalse)))
1898 Write the rest of the 108-byte BMP Version 4 header.
1900 (void) WriteBlobLSBLong(image,0x00ff0000U); /* Red mask */
1901 (void) WriteBlobLSBLong(image,0x0000ff00U); /* Green mask */
1902 (void) WriteBlobLSBLong(image,0x000000ffU); /* Blue mask */
1903 (void) WriteBlobLSBLong(image,0xff000000U); /* Alpha mask */
1904 (void) WriteBlobLSBLong(image,0x00000001U); /* CSType==Calib. RGB */
1905 (void) WriteBlobLSBLong(image,(unsigned int)
1906 image->chromaticity.red_primary.x*0x3ffffff);
1907 (void) WriteBlobLSBLong(image,(unsigned int)
1908 image->chromaticity.red_primary.y*0x3ffffff);
1909 (void) WriteBlobLSBLong(image,(unsigned int)
1910 (1.000f-(image->chromaticity.red_primary.x+
1911 image->chromaticity.red_primary.y)*0x3ffffff));
1912 (void) WriteBlobLSBLong(image,(unsigned int)
1913 image->chromaticity.green_primary.x*0x3ffffff);
1914 (void) WriteBlobLSBLong(image,(unsigned int)
1915 image->chromaticity.green_primary.y*0x3ffffff);
1916 (void) WriteBlobLSBLong(image,(unsigned int)
1917 (1.000f-(image->chromaticity.green_primary.x+
1918 image->chromaticity.green_primary.y)*0x3ffffff));
1919 (void) WriteBlobLSBLong(image,(unsigned int)
1920 image->chromaticity.blue_primary.x*0x3ffffff);
1921 (void) WriteBlobLSBLong(image,(unsigned int)
1922 image->chromaticity.blue_primary.y*0x3ffffff);
1923 (void) WriteBlobLSBLong(image,(unsigned int)
1924 (1.000f-(image->chromaticity.blue_primary.x+
1925 image->chromaticity.blue_primary.y)*0x3ffffff));
1926 (void) WriteBlobLSBLong(image,(unsigned int)
1927 bmp_info.gamma_scale.x*0xffff);
1928 (void) WriteBlobLSBLong(image,(unsigned int)
1929 bmp_info.gamma_scale.y*0xffff);
1930 (void) WriteBlobLSBLong(image,(unsigned int)
1931 bmp_info.gamma_scale.z*0xffff);
1932 if ((image->rendering_intent != UndefinedIntent) ||
1933 (profile != (StringInfo *) NULL))
1938 switch ((int) image->rendering_intent)
1940 case SaturationIntent:
1942 intent=LCS_GM_BUSINESS;
1945 case RelativeIntent:
1947 intent=LCS_GM_GRAPHICS;
1950 case PerceptualIntent:
1952 intent=LCS_GM_IMAGES;
1955 case AbsoluteIntent:
1957 intent=LCS_GM_ABS_COLORIMETRIC;
1966 (void) WriteBlobLSBLong(image,(unsigned int) intent);
1967 (void) WriteBlobLSBLong(image,0x00); /* dummy profile data */
1968 (void) WriteBlobLSBLong(image,0x00); /* dummy profile length */
1969 (void) WriteBlobLSBLong(image,0x00); /* reserved */
1972 if (image->storage_class == PseudoClass)
1978 Dump colormap to file.
1980 if (image->debug != MagickFalse)
1981 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1982 " Colormap: %ld entries",image->colors);
1983 bmp_colormap=(unsigned char *) AcquireQuantumMemory((size_t) (1UL <<
1984 bmp_info.bits_per_pixel),4*sizeof(*bmp_colormap));
1985 if (bmp_colormap == (unsigned char *) NULL)
1986 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
1988 for (i=0; i < (long) MagickMin((long) image->colors,(long) bmp_info.number_colors); i++)
1990 *q++=ScaleQuantumToChar(image->colormap[i].blue);
1991 *q++=ScaleQuantumToChar(image->colormap[i].green);
1992 *q++=ScaleQuantumToChar(image->colormap[i].red);
1994 *q++=(unsigned char) 0x0;
1996 for ( ; i < (long) (1UL << bmp_info.bits_per_pixel); i++)
1998 *q++=(unsigned char) 0x00;
1999 *q++=(unsigned char) 0x00;
2000 *q++=(unsigned char) 0x00;
2002 *q++=(unsigned char) 0x00;
2005 (void) WriteBlob(image,(size_t) (3*(1L << bmp_info.bits_per_pixel)),
2008 (void) WriteBlob(image,(size_t) (4*(1L << bmp_info.bits_per_pixel)),
2010 bmp_colormap=(unsigned char *) RelinquishMagickMemory(bmp_colormap);
2012 if (image->debug != MagickFalse)
2013 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
2014 " Pixels: %u bytes",bmp_info.image_size);
2015 (void) WriteBlob(image,(size_t) bmp_info.image_size,pixels);
2016 pixels=(unsigned char *) RelinquishMagickMemory(pixels);
2017 if (GetNextImageInList(image) == (Image *) NULL)
2019 image=SyncNextImageInList(image);
2020 status=SetImageProgress(image,SaveImagesTag,scene++,
2021 GetImageListLength(image));
2022 if (status == MagickFalse)
2024 } while (image_info->adjoin != MagickFalse);
2025 (void) CloseBlob(image);