2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
13 % Read/Write Microsoft Windows Bitmap Image Format %
17 % Glenn Randers-Pehrson %
21 % Copyright 1999-2011 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/colormap.h"
50 #include "magick/colorspace.h"
51 #include "magick/exception.h"
52 #include "magick/exception-private.h"
53 #include "magick/image.h"
54 #include "magick/image-private.h"
55 #include "magick/list.h"
56 #include "magick/log.h"
57 #include "magick/magick.h"
58 #include "magick/memory_.h"
59 #include "magick/monitor.h"
60 #include "magick/monitor-private.h"
61 #include "magick/profile.h"
62 #include "magick/quantum-private.h"
63 #include "magick/static.h"
64 #include "magick/string_.h"
65 #include "magick/module.h"
66 #include "magick/transform.h"
69 Macro definitions (from Windows wingdi.h).
75 #if !defined(MAGICKCORE_WINDOWS_SUPPORT) || defined(__MINGW32__)
79 #define BI_BITFIELDS 3
81 #define LCS_CALIBRATED_RBG 0
83 #define LCS_WINDOWS_COLOR_SPACE 2
84 #define PROFILE_LINKED 3
85 #define PROFILE_EMBEDDED 4
87 #define LCS_GM_BUSINESS 1 /* Saturation */
88 #define LCS_GM_GRAPHICS 2 /* Relative */
89 #define LCS_GM_IMAGES 4 /* Perceptual */
90 #define LCS_GM_ABS_COLORIMETRIC 8 /* Absolute */
96 typedef struct _BMPInfo
135 Forward declarations.
137 static MagickBooleanType
138 WriteBMPImage(const ImageInfo *,Image *);
141 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
145 % D e c o d e I m a g e %
149 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
151 % DecodeImage unpacks the packed image pixels into runlength-encoded
154 % The format of the DecodeImage method is:
156 % MagickBooleanType DecodeImage(Image *image,
157 % const size_t compression,unsigned char *pixels)
159 % A description of each parameter follows:
161 % o image: the address of a structure of type Image.
163 % o compression: Zero means uncompressed. A value of 1 means the
164 % compressed pixels are runlength encoded for a 256-color bitmap.
165 % A value of 2 means a 16-color bitmap. A value of 3 means bitfields
168 % o pixels: The address of a byte (8 bits) array of pixel data created by
169 % the decoding process.
173 static inline ssize_t MagickAbsoluteValue(const ssize_t x)
180 static inline size_t MagickMax(const size_t x,const size_t y)
187 static inline ssize_t MagickMin(const ssize_t x,const ssize_t y)
194 static MagickBooleanType DecodeImage(Image *image,
195 const size_t compression,unsigned char *pixels)
207 register unsigned char
214 assert(image != (Image *) NULL);
215 assert(image->signature == MagickSignature);
216 if (image->debug != MagickFalse)
217 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
218 assert(pixels != (unsigned char *) NULL);
219 (void) ResetMagickMemory(pixels,0,(size_t) image->columns*image->rows*
224 q=pixels+(size_t) image->columns*image->rows;
225 for (y=0; y < (ssize_t) image->rows; )
227 if ((p < pixels) || (p >= q))
229 count=ReadBlobByte(image);
237 count=MagickMin(count,(int) (q-p));
238 byte=(unsigned char) ReadBlobByte(image);
239 if (compression == BI_RLE8)
241 for (i=0; i < count; i++)
242 *p++=(unsigned char) byte;
246 for (i=0; i < count; i++)
248 ((i & 0x01) != 0 ? (byte & 0x0f) : ((byte >> 4) & 0x0f));
257 count=ReadBlobByte(image);
269 p=pixels+y*image->columns;
277 x+=ReadBlobByte(image);
278 y+=ReadBlobByte(image);
279 p=pixels+y*image->columns+x;
287 count=MagickMin(count,(int) (q-p));
288 if (compression == BI_RLE8)
289 for (i=0; i < count; i++)
290 *p++=(unsigned char) ReadBlobByte(image);
292 for (i=0; i < count; i++)
295 byte=(unsigned char) ReadBlobByte(image);
297 ((i & 0x01) != 0 ? (byte & 0x0f) : ((byte >> 4) & 0x0f));
303 if (compression == BI_RLE8)
305 if ((count & 0x01) != 0)
306 (void) ReadBlobByte(image);
309 if (((count & 0x03) == 1) || ((count & 0x03) == 2))
310 (void) ReadBlobByte(image);
315 if (SetImageProgress(image,LoadImageTag,y,image->rows) == MagickFalse)
318 (void) ReadBlobByte(image); /* end of line */
319 (void) ReadBlobByte(image);
324 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
328 % E n c o d e I m a g e %
332 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
334 % EncodeImage compresses pixels using a runlength encoded format.
336 % The format of the EncodeImage method is:
338 % static MagickBooleanType EncodeImage(Image *image,
339 % const size_t bytes_per_line,const unsigned char *pixels,
340 % unsigned char *compressed_pixels)
342 % A description of each parameter follows:
344 % o image: The image.
346 % o bytes_per_line: the number of bytes in a scanline of compressed pixels
348 % o pixels: The address of a byte (8 bits) array of pixel data created by
349 % the compression process.
351 % o compressed_pixels: The address of a byte (8 bits) array of compressed
355 static size_t EncodeImage(Image *image,const size_t bytes_per_line,
356 const unsigned char *pixels,unsigned char *compressed_pixels)
364 register const unsigned char
371 register unsigned char
375 Runlength encode pixels.
377 assert(image != (Image *) NULL);
378 assert(image->signature == MagickSignature);
379 if (image->debug != MagickFalse)
380 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
381 assert(pixels != (const unsigned char *) NULL);
382 assert(compressed_pixels != (unsigned char *) NULL);
386 for (y=0; y < (ssize_t) image->rows; y++)
388 for (x=0; x < (ssize_t) bytes_per_line; x+=i)
393 for (i=1; ((x+i) < (ssize_t) bytes_per_line); i++)
394 if ((i == 255) || (*(p+i) != *p))
396 *q++=(unsigned char) i;
403 *q++=(unsigned char) 0x00;
404 *q++=(unsigned char) 0x00;
405 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
407 if (status == MagickFalse)
413 *q++=(unsigned char) 0x00;
414 *q++=(unsigned char) 0x01;
415 return((size_t) (q-compressed_pixels));
419 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
427 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
429 % IsBMP() returns MagickTrue if the image format type, identified by the
430 % magick string, is BMP.
432 % The format of the IsBMP method is:
434 % MagickBooleanType IsBMP(const unsigned char *magick,const size_t length)
436 % A description of each parameter follows:
438 % o magick: compare image format pattern against these bytes.
440 % o length: Specifies the length of the magick string.
443 static MagickBooleanType IsBMP(const unsigned char *magick,const size_t length)
447 if ((LocaleNCompare((char *) magick,"BA",2) == 0) ||
448 (LocaleNCompare((char *) magick,"BM",2) == 0) ||
449 (LocaleNCompare((char *) magick,"IC",2) == 0) ||
450 (LocaleNCompare((char *) magick,"PI",2) == 0) ||
451 (LocaleNCompare((char *) magick,"CI",2) == 0) ||
452 (LocaleNCompare((char *) magick,"CP",2) == 0))
458 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
462 % R e a d B M P I m a g e %
466 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
468 % ReadBMPImage() reads a Microsoft Windows bitmap image file, Version
469 % 2, 3 (for Windows or NT), or 4, and returns it. It allocates the memory
470 % necessary for the new Image structure and returns a pointer to the new
473 % The format of the ReadBMPImage method is:
475 % image=ReadBMPImage(image_info)
477 % A description of each parameter follows:
479 % o image_info: the image info.
481 % o exception: return any errors or warnings in this structure.
485 static Image *ReadBMPImage(const ImageInfo *image_info,ExceptionInfo *exception)
518 register unsigned char
542 assert(image_info != (const ImageInfo *) NULL);
543 assert(image_info->signature == MagickSignature);
544 if (image_info->debug != MagickFalse)
545 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
546 image_info->filename);
547 assert(exception != (ExceptionInfo *) NULL);
548 assert(exception->signature == MagickSignature);
549 image=AcquireImage(image_info);
550 status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception);
551 if (status == MagickFalse)
553 image=DestroyImageList(image);
554 return((Image *) NULL);
557 Determine if this a BMP file.
559 (void) ResetMagickMemory(&bmp_info,0,sizeof(bmp_info));
560 bmp_info.ba_offset=0;
562 count=ReadBlob(image,2,magick);
576 Verify BMP identifier.
578 if (bmp_info.ba_offset == 0)
579 start_position=TellBlob(image)-2;
580 bmp_info.ba_offset=0;
581 while (LocaleNCompare((char *) magick,"BA",2) == 0)
583 bmp_info.file_size=ReadBlobLSBLong(image);
584 bmp_info.ba_offset=ReadBlobLSBLong(image);
585 bmp_info.offset_bits=ReadBlobLSBLong(image);
586 count=ReadBlob(image,2,magick);
590 if (image->debug != MagickFalse)
591 (void) LogMagickEvent(CoderEvent,GetMagickModule()," Magick: %c%c",
592 magick[0],magick[1]);
593 if ((count == 0) || ((LocaleNCompare((char *) magick,"BM",2) != 0) &&
594 (LocaleNCompare((char *) magick,"CI",2) != 0)))
595 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
596 bmp_info.file_size=ReadBlobLSBLong(image);
597 (void) ReadBlobLSBLong(image);
598 bmp_info.offset_bits=ReadBlobLSBLong(image);
599 bmp_info.size=ReadBlobLSBLong(image);
600 if (image->debug != MagickFalse)
601 (void) LogMagickEvent(CoderEvent,GetMagickModule()," BMP size: %u",
603 if (bmp_info.size == 12)
608 bmp_info.width=(short) ReadBlobLSBShort(image);
609 bmp_info.height=(short) ReadBlobLSBShort(image);
610 bmp_info.planes=ReadBlobLSBShort(image);
611 bmp_info.bits_per_pixel=ReadBlobLSBShort(image);
614 bmp_info.number_colors=0;
615 bmp_info.compression=BI_RGB;
616 bmp_info.image_size=0;
617 bmp_info.alpha_mask=0;
618 if (image->debug != MagickFalse)
620 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
621 " Format: OS/2 Bitmap");
622 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
623 " Geometry: %.20gx%.20g",(double) bmp_info.width,(double)
630 Microsoft Windows BMP image file.
632 if (bmp_info.size < 40)
633 ThrowReaderException(CorruptImageError,"NonOS2HeaderSizeError");
634 bmp_info.width=(int) ReadBlobLSBLong(image);
635 bmp_info.height=(int) ReadBlobLSBLong(image);
636 bmp_info.planes=ReadBlobLSBShort(image);
637 bmp_info.bits_per_pixel=ReadBlobLSBShort(image);
638 bmp_info.compression=ReadBlobLSBLong(image);
639 bmp_info.image_size=ReadBlobLSBLong(image);
640 bmp_info.x_pixels=ReadBlobLSBLong(image);
641 bmp_info.y_pixels=ReadBlobLSBLong(image);
642 bmp_info.number_colors=ReadBlobLSBLong(image);
643 bmp_info.colors_important=ReadBlobLSBLong(image);
646 if (image->debug != MagickFalse)
648 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
649 " Format: MS Windows bitmap");
650 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
651 " Geometry: %.20gx%.20g",(double) bmp_info.width,(double)
653 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
654 " Bits per pixel: %.20g",(double) bmp_info.bits_per_pixel);
655 switch ((int) bmp_info.compression)
659 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
660 " Compression: BI_RGB");
665 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
666 " Compression: BI_RLE4");
671 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
672 " Compression: BI_RLE8");
677 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
678 " Compression: BI_BITFIELDS");
683 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
684 " Compression: BI_PNG");
689 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
690 " Compression: BI_JPEG");
695 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
696 " Compression: UNKNOWN (%u)",bmp_info.compression);
699 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
700 " Number of colors: %u",bmp_info.number_colors);
702 bmp_info.red_mask=ReadBlobLSBLong(image);
703 bmp_info.green_mask=ReadBlobLSBLong(image);
704 bmp_info.blue_mask=ReadBlobLSBLong(image);
705 if (bmp_info.size > 40)
711 Read color management information.
713 bmp_info.alpha_mask=ReadBlobLSBLong(image);
714 bmp_info.colorspace=(int) ReadBlobLSBLong(image);
716 Decode 2^30 fixed point formatted CIE primaries.
718 bmp_info.red_primary.x=(double) ReadBlobLSBLong(image)/0x3ffffff;
719 bmp_info.red_primary.y=(double) ReadBlobLSBLong(image)/0x3ffffff;
720 bmp_info.red_primary.z=(double) ReadBlobLSBLong(image)/0x3ffffff;
721 bmp_info.green_primary.x=(double) ReadBlobLSBLong(image)/0x3ffffff;
722 bmp_info.green_primary.y=(double) ReadBlobLSBLong(image)/0x3ffffff;
723 bmp_info.green_primary.z=(double) ReadBlobLSBLong(image)/0x3ffffff;
724 bmp_info.blue_primary.x=(double) ReadBlobLSBLong(image)/0x3ffffff;
725 bmp_info.blue_primary.y=(double) ReadBlobLSBLong(image)/0x3ffffff;
726 bmp_info.blue_primary.z=(double) ReadBlobLSBLong(image)/0x3ffffff;
727 sum=bmp_info.red_primary.x+bmp_info.red_primary.y+
728 bmp_info.red_primary.z;
729 image->chromaticity.red_primary.x/=sum;
730 image->chromaticity.red_primary.y/=sum;
731 sum=bmp_info.green_primary.x+bmp_info.green_primary.y+
732 bmp_info.green_primary.z;
733 image->chromaticity.green_primary.x/=sum;
734 image->chromaticity.green_primary.y/=sum;
735 sum=bmp_info.blue_primary.x+bmp_info.blue_primary.y+
736 bmp_info.blue_primary.z;
737 image->chromaticity.blue_primary.x/=sum;
738 image->chromaticity.blue_primary.y/=sum;
740 Decode 16^16 fixed point formatted gamma_scales.
742 bmp_info.gamma_scale.x=(double) ReadBlobLSBLong(image)/0xffff;
743 bmp_info.gamma_scale.y=(double) ReadBlobLSBLong(image)/0xffff;
744 bmp_info.gamma_scale.z=(double) ReadBlobLSBLong(image)/0xffff;
746 Compute a single gamma from the BMP 3-channel gamma.
748 image->gamma=(bmp_info.gamma_scale.x+bmp_info.gamma_scale.y+
749 bmp_info.gamma_scale.z)/3.0;
751 if (bmp_info.size > 108)
757 Read BMP Version 5 color management information.
759 intent=ReadBlobLSBLong(image);
760 switch ((int) intent)
762 case LCS_GM_BUSINESS:
764 image->rendering_intent=SaturationIntent;
767 case LCS_GM_GRAPHICS:
769 image->rendering_intent=RelativeIntent;
774 image->rendering_intent=PerceptualIntent;
777 case LCS_GM_ABS_COLORIMETRIC:
779 image->rendering_intent=AbsoluteIntent;
783 profile_data=ReadBlobLSBLong(image);
784 profile_size=ReadBlobLSBLong(image);
785 (void) ReadBlobLSBLong(image); /* Reserved byte */
788 if ((bmp_info.compression != BI_RGB) &&
789 ((MagickSizeType) bmp_info.file_size != GetBlobSize(image)))
790 (void) ThrowMagickException(exception,GetMagickModule(),CorruptImageError,
791 "LengthAndFilesizeDoNotMatch","`%s'",image->filename);
792 if (bmp_info.width <= 0)
793 ThrowReaderException(CorruptImageError,"NegativeOrZeroImageSize");
794 if (bmp_info.height == 0)
795 ThrowReaderException(CorruptImageError,"NegativeOrZeroImageSize");
796 if (bmp_info.planes != 1)
797 ThrowReaderException(CorruptImageError,"StaticPlanesValueNotEqualToOne");
798 if ((bmp_info.bits_per_pixel != 1) && (bmp_info.bits_per_pixel != 4) &&
799 (bmp_info.bits_per_pixel != 8) && (bmp_info.bits_per_pixel != 16) &&
800 (bmp_info.bits_per_pixel != 24) && (bmp_info.bits_per_pixel != 32))
801 ThrowReaderException(CorruptImageError,"UnrecognizedBitsPerPixel");
802 if (bmp_info.number_colors > (1U << bmp_info.bits_per_pixel))
804 if (bmp_info.bits_per_pixel < 24)
805 ThrowReaderException(CorruptImageError,"UnrecognizedNumberOfColors");
806 bmp_info.number_colors=0;
808 if ((bmp_info.compression == 1) && (bmp_info.bits_per_pixel != 8))
809 ThrowReaderException(CorruptImageError,"UnrecognizedBitsPerPixel");
810 if ((bmp_info.compression == 2) && (bmp_info.bits_per_pixel != 4))
811 ThrowReaderException(CorruptImageError,"UnrecognizedBitsPerPixel");
812 if ((bmp_info.compression == 3) && (bmp_info.bits_per_pixel < 16))
813 ThrowReaderException(CorruptImageError,"UnrecognizedBitsPerPixel");
814 switch (bmp_info.compression)
822 ThrowReaderException(CoderError,"JPEGCompressNotSupported");
824 ThrowReaderException(CoderError,"PNGCompressNotSupported");
826 ThrowReaderException(CorruptImageError,"UnrecognizedImageCompression");
828 image->columns=(size_t) MagickAbsoluteValue(bmp_info.width);
829 image->rows=(size_t) MagickAbsoluteValue(bmp_info.height);
830 image->depth=bmp_info.bits_per_pixel <= 8 ? bmp_info.bits_per_pixel : 8;
831 if ((bmp_info.bits_per_pixel == 16) ||
832 (bmp_info.bits_per_pixel == 32))
833 image->matte=bmp_info.alpha_mask != 0 ? MagickTrue : MagickFalse;
834 if ((bmp_info.number_colors != 0) || (bmp_info.bits_per_pixel < 16))
839 image->storage_class=PseudoClass;
840 image->colors=bmp_info.number_colors;
842 if (image->colors == 0)
843 image->colors=one << bmp_info.bits_per_pixel;
845 if (image->storage_class == PseudoClass)
854 Read BMP raster colormap.
856 if (image->debug != MagickFalse)
857 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
858 " Reading colormap of %.20g colors",(double) image->colors);
859 if (AcquireImageColormap(image,image->colors) == MagickFalse)
860 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
861 bmp_colormap=(unsigned char *) AcquireQuantumMemory((size_t)
862 image->colors,4*sizeof(*bmp_colormap));
863 if (bmp_colormap == (unsigned char *) NULL)
864 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
865 if ((bmp_info.size == 12) || (bmp_info.size == 64))
869 offset=SeekBlob(image,start_position+14+bmp_info.size,SEEK_SET);
871 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
872 count=ReadBlob(image,packet_size*image->colors,bmp_colormap);
873 if (count != (ssize_t) (packet_size*image->colors))
874 ThrowReaderException(CorruptImageError,"InsufficientImageDataInFile");
876 for (i=0; i < (ssize_t) image->colors; i++)
878 image->colormap[i].blue=ScaleCharToQuantum(*p++);
879 image->colormap[i].green=ScaleCharToQuantum(*p++);
880 image->colormap[i].red=ScaleCharToQuantum(*p++);
881 if (packet_size == 4)
884 bmp_colormap=(unsigned char *) RelinquishMagickMemory(bmp_colormap);
886 if ((image_info->ping != MagickFalse) && (image_info->number_scenes != 0))
887 if (image->scene >= (image_info->scene+image_info->number_scenes-1))
892 offset=SeekBlob(image,start_position+bmp_info.offset_bits,SEEK_SET);
894 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
895 if (bmp_info.compression == BI_RLE4)
896 bmp_info.bits_per_pixel<<=1;
897 bytes_per_line=4*((image->columns*bmp_info.bits_per_pixel+31)/32);
898 length=(size_t) bytes_per_line*image->rows;
899 pixels=(unsigned char *) AcquireQuantumMemory((size_t) image->rows,
900 MagickMax(bytes_per_line,image->columns+256UL)*sizeof(*pixels));
901 if (pixels == (unsigned char *) NULL)
902 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
903 if ((bmp_info.compression == BI_RGB) ||
904 (bmp_info.compression == BI_BITFIELDS))
906 if (image->debug != MagickFalse)
907 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
908 " Reading pixels (%.20g bytes)",(double) length);
909 count=ReadBlob(image,length,pixels);
910 if (count != (ssize_t) length)
912 pixels=(unsigned char *) RelinquishMagickMemory(pixels);
913 ThrowReaderException(CorruptImageError,
914 "InsufficientImageDataInFile");
920 Convert run-length encoded raster pixels.
922 status=DecodeImage(image,bmp_info.compression,pixels);
923 if (status == MagickFalse)
925 pixels=(unsigned char *) RelinquishMagickMemory(pixels);
926 ThrowReaderException(CorruptImageError,
927 "UnableToRunlengthDecodeImage");
931 Initialize image structure.
933 image->x_resolution=(double) bmp_info.x_pixels/100.0;
934 image->y_resolution=(double) bmp_info.y_pixels/100.0;
935 image->units=PixelsPerCentimeterResolution;
937 Convert BMP raster image to pixel packets.
939 if (bmp_info.compression == BI_RGB)
941 bmp_info.alpha_mask=0;
942 bmp_info.red_mask=0x00ff0000U;
943 bmp_info.green_mask=0x0000ff00U;
944 bmp_info.blue_mask=0x000000ffU;
945 if (bmp_info.bits_per_pixel == 16)
950 bmp_info.red_mask=0x00007c00U;
951 bmp_info.green_mask=0x000003e0U;
952 bmp_info.blue_mask=0x0000001fU;
955 if ((bmp_info.bits_per_pixel == 16) || (bmp_info.bits_per_pixel == 32))
961 Get shift and quantum bits info from bitfield masks.
963 (void) ResetMagickMemory(&shift,0,sizeof(shift));
964 (void) ResetMagickMemory(&quantum_bits,0,sizeof(quantum_bits));
965 if (bmp_info.red_mask != 0)
966 while (((bmp_info.red_mask << shift.red) & 0x80000000UL) == 0)
968 if (bmp_info.green_mask != 0)
969 while (((bmp_info.green_mask << shift.green) & 0x80000000UL) == 0)
971 if (bmp_info.blue_mask != 0)
972 while (((bmp_info.blue_mask << shift.blue) & 0x80000000UL) == 0)
974 if (bmp_info.alpha_mask != 0)
975 while (((bmp_info.alpha_mask << shift.opacity) & 0x80000000UL) == 0)
978 while (((bmp_info.red_mask << sample) & 0x80000000UL) != 0)
980 quantum_bits.red=(Quantum) (sample-shift.red);
982 while (((bmp_info.green_mask << sample) & 0x80000000UL) != 0)
984 quantum_bits.green=(Quantum) (sample-shift.green);
986 while (((bmp_info.blue_mask << sample) & 0x80000000UL) != 0)
988 quantum_bits.blue=(Quantum) (sample-shift.blue);
989 sample=shift.opacity;
990 while (((bmp_info.alpha_mask << sample) & 0x80000000UL) != 0)
992 quantum_bits.opacity=(Quantum) (sample-shift.opacity);
994 switch (bmp_info.bits_per_pixel)
999 Convert bitmap scanline.
1001 for (y=(ssize_t) image->rows-1; y >= 0; y--)
1003 p=pixels+(image->rows-y-1)*bytes_per_line;
1004 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
1005 if (q == (PixelPacket *) NULL)
1007 indexes=GetAuthenticIndexQueue(image);
1008 for (x=0; x < ((ssize_t) image->columns-7); x+=8)
1010 for (bit=0; bit < 8; bit++)
1012 index=(IndexPacket) (((*p) & (0x80 >> bit)) != 0 ? 0x01 : 0x00);
1013 indexes[x+bit]=index;
1014 *q++=image->colormap[(ssize_t) index];
1018 if ((image->columns % 8) != 0)
1020 for (bit=0; bit < (image->columns % 8); bit++)
1022 index=(IndexPacket) (((*p) & (0x80 >> bit)) != 0 ? 0x01 : 0x00);
1023 indexes[x+bit]=index;
1024 *q++=image->colormap[(ssize_t) index];
1028 if (SyncAuthenticPixels(image,exception) == MagickFalse)
1030 if (image->previous == (Image *) NULL)
1032 status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y,
1034 if (status == MagickFalse)
1043 Convert PseudoColor scanline.
1045 for (y=(ssize_t) image->rows-1; y >= 0; y--)
1047 p=pixels+(image->rows-y-1)*bytes_per_line;
1048 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
1049 if (q == (PixelPacket *) NULL)
1051 indexes=GetAuthenticIndexQueue(image);
1052 for (x=0; x < ((ssize_t) image->columns-1); x+=2)
1054 index=ConstrainColormapIndex(image,(*p >> 4) & 0x0f);
1056 *q++=image->colormap[(ssize_t) index];
1057 index=ConstrainColormapIndex(image,*p & 0x0f);
1059 *q++=image->colormap[(ssize_t) index];
1062 if ((image->columns % 2) != 0)
1064 index=ConstrainColormapIndex(image,(*p >> 4) & 0xf);
1066 *q++=image->colormap[(ssize_t) index];
1069 if (SyncAuthenticPixels(image,exception) == MagickFalse)
1071 if (image->previous == (Image *) NULL)
1073 status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y,
1075 if (status == MagickFalse)
1084 Convert PseudoColor scanline.
1086 if ((bmp_info.compression == BI_RLE8) ||
1087 (bmp_info.compression == BI_RLE4))
1088 bytes_per_line=image->columns;
1089 for (y=(ssize_t) image->rows-1; y >= 0; y--)
1091 p=pixels+(image->rows-y-1)*bytes_per_line;
1092 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
1093 if (q == (PixelPacket *) NULL)
1095 indexes=GetAuthenticIndexQueue(image);
1096 for (x = (ssize_t)image->columns; x != 0; --x)
1098 index=ConstrainColormapIndex(image,*p);
1100 *q=image->colormap[(ssize_t) index];
1104 if (SyncAuthenticPixels(image,exception) == MagickFalse)
1106 offset=(MagickOffsetType) (image->rows-y-1);
1107 if (image->previous == (Image *) NULL)
1109 status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y,
1111 if (status == MagickFalse)
1123 Convert bitfield encoded 16-bit PseudoColor scanline.
1125 if (bmp_info.compression != BI_RGB &&
1126 bmp_info.compression != BI_BITFIELDS)
1128 pixels=(unsigned char *) RelinquishMagickMemory(pixels);
1129 ThrowReaderException(CorruptImageError,
1130 "UnrecognizedImageCompression");
1132 bytes_per_line=2*(image->columns+image->columns % 2);
1133 image->storage_class=DirectClass;
1134 for (y=(ssize_t) image->rows-1; y >= 0; y--)
1136 p=pixels+(image->rows-y-1)*bytes_per_line;
1137 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
1138 if (q == (PixelPacket *) NULL)
1140 for (x=0; x < (ssize_t) image->columns; x++)
1142 pixel=(size_t) (*p++);
1144 red=((pixel & bmp_info.red_mask) << shift.red) >> 16;
1145 if (quantum_bits.red == 5)
1146 red|=((red & 0xe000) >> 5);
1147 if (quantum_bits.red <= 8)
1148 red|=((red & 0xff00) >> 8);
1149 green=((pixel & bmp_info.green_mask) << shift.green) >> 16;
1150 if (quantum_bits.green == 5)
1151 green|=((green & 0xe000) >> 5);
1152 if (quantum_bits.green == 6)
1153 green|=((green & 0xc000) >> 6);
1154 if (quantum_bits.green <= 8)
1155 green|=((green & 0xff00) >> 8);
1156 blue=((pixel & bmp_info.blue_mask) << shift.blue) >> 16;
1157 if (quantum_bits.blue == 5)
1158 blue|=((blue & 0xe000) >> 5);
1159 if (quantum_bits.blue <= 8)
1160 blue|=((blue & 0xff00) >> 8);
1161 opacity=((pixel & bmp_info.alpha_mask) << shift.opacity) >> 16;
1162 if (quantum_bits.opacity <= 8)
1163 opacity|=((opacity & 0xff00) >> 8);
1164 q->red=ScaleShortToQuantum((unsigned short) red);
1165 q->green=ScaleShortToQuantum((unsigned short) green);
1166 q->blue=ScaleShortToQuantum((unsigned short) blue);
1167 SetOpacityPixelComponent(q,OpaqueOpacity);
1168 if (image->matte != MagickFalse)
1169 q->opacity=ScaleShortToQuantum((unsigned short) (65535-opacity));
1172 if (SyncAuthenticPixels(image,exception) == MagickFalse)
1174 offset=(MagickOffsetType) (image->rows-y-1);
1175 if (image->previous == (Image *) NULL)
1177 status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y,
1179 if (status == MagickFalse)
1188 Convert DirectColor scanline.
1190 bytes_per_line=4*((image->columns*24+31)/32);
1191 for (y=(ssize_t) image->rows-1; y >= 0; y--)
1193 p=pixels+(image->rows-y-1)*bytes_per_line;
1194 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
1195 if (q == (PixelPacket *) NULL)
1197 for (x=0; x < (ssize_t) image->columns; x++)
1199 q->blue=ScaleCharToQuantum(*p++);
1200 q->green=ScaleCharToQuantum(*p++);
1201 q->red=ScaleCharToQuantum(*p++);
1202 SetOpacityPixelComponent(q,OpaqueOpacity);
1205 if (SyncAuthenticPixels(image,exception) == MagickFalse)
1207 offset=(MagickOffsetType) (image->rows-y-1);
1208 if (image->previous == (Image *) NULL)
1210 status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y,
1212 if (status == MagickFalse)
1221 Convert bitfield encoded DirectColor scanline.
1223 if ((bmp_info.compression != BI_RGB) &&
1224 (bmp_info.compression != BI_BITFIELDS))
1226 pixels=(unsigned char *) RelinquishMagickMemory(pixels);
1227 ThrowReaderException(CorruptImageError,
1228 "UnrecognizedImageCompression");
1230 bytes_per_line=4*(image->columns);
1231 for (y=(ssize_t) image->rows-1; y >= 0; y--)
1236 p=pixels+(image->rows-y-1)*bytes_per_line;
1237 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
1238 if (q == (PixelPacket *) NULL)
1240 for (x=0; x < (ssize_t) image->columns; x++)
1242 pixel=(size_t) (*p++);
1244 pixel|=(*p++ << 16);
1245 pixel|=(*p++ << 24);
1246 red=((pixel & bmp_info.red_mask) << shift.red) >> 16;
1247 if (quantum_bits.red == 8)
1249 green=((pixel & bmp_info.green_mask) << shift.green) >> 16;
1250 if (quantum_bits.green == 8)
1251 green|=(green >> 8);
1252 blue=((pixel & bmp_info.blue_mask) << shift.blue) >> 16;
1253 if (quantum_bits.blue == 8)
1255 opacity=((pixel & bmp_info.alpha_mask) << shift.opacity) >> 16;
1256 if (quantum_bits.opacity == 8)
1257 opacity|=(opacity >> 8);
1258 q->red=ScaleShortToQuantum((unsigned short) red);
1259 q->green=ScaleShortToQuantum((unsigned short) green);
1260 q->blue=ScaleShortToQuantum((unsigned short) blue);
1261 SetOpacityPixelComponent(q,OpaqueOpacity);
1262 if (image->matte != MagickFalse)
1263 q->opacity=ScaleShortToQuantum((unsigned short) (65535-opacity));
1266 if (SyncAuthenticPixels(image,exception) == MagickFalse)
1268 offset=(MagickOffsetType) (image->rows-y-1);
1269 if (image->previous == (Image *) NULL)
1271 status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y,
1273 if (status == MagickFalse)
1281 pixels=(unsigned char *) RelinquishMagickMemory(pixels);
1282 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
1285 pixels=(unsigned char *) RelinquishMagickMemory(pixels);
1286 if (EOFBlob(image) != MagickFalse)
1288 ThrowFileException(exception,CorruptImageError,"UnexpectedEndOfFile",
1292 if (bmp_info.height < 0)
1298 Correct image orientation.
1300 flipped_image=FlipImage(image,exception);
1301 if (flipped_image != (Image *) NULL)
1303 DuplicateBlob(flipped_image,image);
1304 image=DestroyImage(image);
1305 image=flipped_image;
1309 Proceed to next image.
1311 if (image_info->number_scenes != 0)
1312 if (image->scene >= (image_info->scene+image_info->number_scenes-1))
1315 if (bmp_info.ba_offset != 0)
1317 offset=SeekBlob(image,(MagickOffsetType) bmp_info.ba_offset,SEEK_SET);
1319 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
1321 count=ReadBlob(image,2,magick);
1322 if ((count == 2) && (IsBMP(magick,2) != MagickFalse))
1325 Acquire next image structure.
1327 AcquireNextImage(image_info,image);
1328 if (GetNextImageInList(image) == (Image *) NULL)
1330 image=DestroyImageList(image);
1331 return((Image *) NULL);
1333 image=SyncNextImageInList(image);
1334 status=SetImageProgress(image,LoadImagesTag,TellBlob(image),
1335 GetBlobSize(image));
1336 if (status == MagickFalse)
1339 } while (IsBMP(magick,2) != MagickFalse);
1340 (void) CloseBlob(image);
1341 return(GetFirstImageInList(image));
1345 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1349 % R e g i s t e r B M P I m a g e %
1353 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1355 % RegisterBMPImage() adds attributes for the BMP image format to
1356 % the list of supported formats. The attributes include the image format
1357 % tag, a method to read and/or write the format, whether the format
1358 % supports the saving of more than one frame to the same file or blob,
1359 % whether the format supports native in-memory I/O, and a brief
1360 % description of the format.
1362 % The format of the RegisterBMPImage method is:
1364 % size_t RegisterBMPImage(void)
1367 ModuleExport size_t RegisterBMPImage(void)
1372 entry=SetMagickInfo("BMP");
1373 entry->decoder=(DecodeImageHandler *) ReadBMPImage;
1374 entry->encoder=(EncodeImageHandler *) WriteBMPImage;
1375 entry->magick=(IsImageFormatHandler *) IsBMP;
1376 entry->description=ConstantString("Microsoft Windows bitmap image");
1377 entry->module=ConstantString("BMP");
1378 entry->adjoin=MagickFalse;
1379 entry->seekable_stream=MagickTrue;
1380 (void) RegisterMagickInfo(entry);
1381 entry=SetMagickInfo("BMP2");
1382 entry->encoder=(EncodeImageHandler *) WriteBMPImage;
1383 entry->magick=(IsImageFormatHandler *) IsBMP;
1384 entry->description=ConstantString("Microsoft Windows bitmap image (V2)");
1385 entry->module=ConstantString("BMP");
1386 entry->adjoin=MagickFalse;
1387 entry->seekable_stream=MagickTrue;
1388 (void) RegisterMagickInfo(entry);
1389 entry=SetMagickInfo("BMP3");
1390 entry->encoder=(EncodeImageHandler *) WriteBMPImage;
1391 entry->magick=(IsImageFormatHandler *) IsBMP;
1392 entry->description=ConstantString("Microsoft Windows bitmap image (V3)");
1393 entry->module=ConstantString("BMP");
1394 entry->adjoin=MagickFalse;
1395 entry->seekable_stream=MagickTrue;
1396 (void) RegisterMagickInfo(entry);
1397 return(MagickImageCoderSignature);
1401 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1405 % U n r e g i s t e r B M P I m a g e %
1409 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1411 % UnregisterBMPImage() removes format registrations made by the
1412 % BMP module from the list of supported formats.
1414 % The format of the UnregisterBMPImage method is:
1416 % UnregisterBMPImage(void)
1419 ModuleExport void UnregisterBMPImage(void)
1421 (void) UnregisterMagickInfo("BMP");
1422 (void) UnregisterMagickInfo("BMP2");
1423 (void) UnregisterMagickInfo("BMP3");
1427 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1431 % W r i t e B M P I m a g e %
1435 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1437 % WriteBMPImage() writes an image in Microsoft Windows bitmap encoded
1438 % image format, version 3 for Windows or (if the image has a matte channel)
1441 % The format of the WriteBMPImage method is:
1443 % MagickBooleanType WriteBMPImage(const ImageInfo *image_info,Image *image)
1445 % A description of each parameter follows.
1447 % o image_info: the image info.
1449 % o image: The image.
1452 static MagickBooleanType WriteBMPImage(const ImageInfo *image_info,Image *image)
1470 register const IndexPacket
1473 register const PixelPacket
1480 register unsigned char
1492 Open output image file.
1494 assert(image_info != (const ImageInfo *) NULL);
1495 assert(image_info->signature == MagickSignature);
1496 assert(image != (Image *) NULL);
1497 assert(image->signature == MagickSignature);
1498 if (image->debug != MagickFalse)
1499 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1500 status=OpenBlob(image_info,image,WriteBinaryBlobMode,&image->exception);
1501 if (status == MagickFalse)
1504 if (LocaleCompare(image_info->magick,"BMP2") == 0)
1507 if (LocaleCompare(image_info->magick,"BMP3") == 0)
1513 Initialize BMP raster file header.
1515 if (image->colorspace != RGBColorspace)
1516 (void) TransformImageColorspace(image,RGBColorspace);
1517 (void) ResetMagickMemory(&bmp_info,0,sizeof(bmp_info));
1518 bmp_info.file_size=14+12;
1520 bmp_info.file_size+=28;
1521 bmp_info.offset_bits=bmp_info.file_size;
1522 bmp_info.compression=BI_RGB;
1523 if ((image->storage_class == PseudoClass) && (image->colors > 256))
1524 (void) SetImageStorageClass(image,DirectClass);
1525 if (image->storage_class != DirectClass)
1528 Colormapped BMP raster.
1530 bmp_info.bits_per_pixel=8;
1531 if (image->colors <= 2)
1532 bmp_info.bits_per_pixel=1;
1534 if (image->colors <= 16)
1535 bmp_info.bits_per_pixel=4;
1537 if (image->colors <= 256)
1538 bmp_info.bits_per_pixel=8;
1539 if (image_info->compression == RLECompression)
1540 bmp_info.bits_per_pixel=8;
1541 bmp_info.number_colors=1U << bmp_info.bits_per_pixel;
1542 if (image->matte != MagickFalse)
1543 (void) SetImageStorageClass(image,DirectClass);
1545 if ((size_t) bmp_info.number_colors < image->colors)
1546 (void) SetImageStorageClass(image,DirectClass);
1549 bmp_info.file_size+=3*(1UL << bmp_info.bits_per_pixel);
1550 bmp_info.offset_bits+=3*(1UL << bmp_info.bits_per_pixel);
1553 bmp_info.file_size+=(1UL << bmp_info.bits_per_pixel);
1554 bmp_info.offset_bits+=(1UL << bmp_info.bits_per_pixel);
1558 if (image->storage_class == DirectClass)
1561 Full color BMP raster.
1563 bmp_info.number_colors=0;
1564 bmp_info.bits_per_pixel=(unsigned short)
1565 ((type > 3) && (image->matte != MagickFalse) ? 32 : 24);
1566 bmp_info.compression=(unsigned int) ((type > 3) &&
1567 (image->matte != MagickFalse) ? BI_BITFIELDS : BI_RGB);
1569 bytes_per_line=4*((image->columns*bmp_info.bits_per_pixel+31)/32);
1570 bmp_info.ba_offset=0;
1571 profile=GetImageProfile(image,"icc");
1572 have_color_info=(image->rendering_intent != UndefinedIntent) ||
1573 (profile != (StringInfo *) NULL) || (image->gamma != 0.0) ? MagickTrue :
1578 if ((type == 3) || ((image->matte == MagickFalse) &&
1579 (have_color_info == MagickFalse)))
1591 if ((image->rendering_intent != UndefinedIntent) ||
1592 (profile != (StringInfo *) NULL))
1597 bmp_info.file_size+=extra_size;
1598 bmp_info.offset_bits+=extra_size;
1600 bmp_info.width=(ssize_t) image->columns;
1601 bmp_info.height=(ssize_t) image->rows;
1603 bmp_info.image_size=(unsigned int) (bytes_per_line*image->rows);
1604 bmp_info.file_size+=bmp_info.image_size;
1605 bmp_info.x_pixels=75*39;
1606 bmp_info.y_pixels=75*39;
1607 switch (image->units)
1609 case UndefinedResolution:
1610 case PixelsPerInchResolution:
1612 bmp_info.x_pixels=(unsigned int) (100.0*image->x_resolution/2.54);
1613 bmp_info.y_pixels=(unsigned int) (100.0*image->y_resolution/2.54);
1616 case PixelsPerCentimeterResolution:
1618 bmp_info.x_pixels=(unsigned int) (100.0*image->x_resolution);
1619 bmp_info.y_pixels=(unsigned int) (100.0*image->y_resolution);
1623 bmp_info.colors_important=bmp_info.number_colors;
1625 Convert MIFF to BMP raster pixels.
1627 pixels=(unsigned char *) AcquireQuantumMemory((size_t) bmp_info.image_size,
1629 if (pixels == (unsigned char *) NULL)
1630 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
1631 (void) ResetMagickMemory(pixels,0,(size_t) bmp_info.image_size);
1632 switch (bmp_info.bits_per_pixel)
1641 Convert PseudoClass image to a BMP monochrome image.
1643 for (y=0; y < (ssize_t) image->rows; y++)
1645 p=GetVirtualPixels(image,0,y,image->columns,1,&image->exception);
1646 if (p == (const PixelPacket *) NULL)
1648 indexes=GetVirtualIndexQueue(image);
1649 q=pixels+(image->rows-y-1)*bytes_per_line;
1652 for (x=0; x < (ssize_t) image->columns; x++)
1655 byte|=indexes[x] != 0 ? 0x01 : 0x00;
1659 *q++=(unsigned char) byte;
1666 *q++=(unsigned char) (byte << (8-bit));
1669 for (x=(ssize_t) (image->columns+7)/8; x < (ssize_t) bytes_per_line; x++)
1671 if (image->previous == (Image *) NULL)
1673 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
1675 if (status == MagickFalse)
1688 Convert PseudoClass image to a BMP monochrome image.
1690 for (y=0; y < (ssize_t) image->rows; y++)
1692 p=GetVirtualPixels(image,0,y,image->columns,1,&image->exception);
1693 if (p == (const PixelPacket *) NULL)
1695 indexes=GetVirtualIndexQueue(image);
1696 q=pixels+(image->rows-y-1)*bytes_per_line;
1699 for (x=0; x < (ssize_t) image->columns; x++)
1702 byte|=((size_t) indexes[x] & 0x0f);
1706 *q++=(unsigned char) byte;
1713 *q++=(unsigned char) (byte << 4);
1716 for (x=(ssize_t) (image->columns+1)/2; x < (ssize_t) bytes_per_line; x++)
1718 if (image->previous == (Image *) NULL)
1720 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
1722 if (status == MagickFalse)
1731 Convert PseudoClass packet to BMP pixel.
1733 for (y=0; y < (ssize_t) image->rows; y++)
1735 p=GetVirtualPixels(image,0,y,image->columns,1,&image->exception);
1736 if (p == (const PixelPacket *) NULL)
1738 indexes=GetVirtualIndexQueue(image);
1739 q=pixels+(image->rows-y-1)*bytes_per_line;
1740 for (x=0; x < (ssize_t) image->columns; x++)
1741 *q++=(unsigned char) indexes[x];
1742 for ( ; x < (ssize_t) bytes_per_line; x++)
1744 if (image->previous == (Image *) NULL)
1746 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
1748 if (status == MagickFalse)
1757 Convert DirectClass packet to BMP BGR888.
1759 for (y=0; y < (ssize_t) image->rows; y++)
1761 p=GetVirtualPixels(image,0,y,image->columns,1,&image->exception);
1762 if (p == (const PixelPacket *) NULL)
1764 q=pixels+(image->rows-y-1)*bytes_per_line;
1765 for (x=0; x < (ssize_t) image->columns; x++)
1767 *q++=ScaleQuantumToChar(GetBluePixelComponent(p));
1768 *q++=ScaleQuantumToChar(GetGreenPixelComponent(p));
1769 *q++=ScaleQuantumToChar(GetRedPixelComponent(p));
1772 for (x=3L*(ssize_t) image->columns; x < (ssize_t) bytes_per_line; x++)
1774 if (image->previous == (Image *) NULL)
1776 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
1778 if (status == MagickFalse)
1787 Convert DirectClass packet to ARGB8888 pixel.
1789 for (y=0; y < (ssize_t) image->rows; y++)
1791 p=GetVirtualPixels(image,0,y,image->columns,1,&image->exception);
1792 if (p == (const PixelPacket *) NULL)
1794 q=pixels+(image->rows-y-1)*bytes_per_line;
1795 for (x=0; x < (ssize_t) image->columns; x++)
1797 *q++=ScaleQuantumToChar(GetBluePixelComponent(p));
1798 *q++=ScaleQuantumToChar(GetGreenPixelComponent(p));
1799 *q++=ScaleQuantumToChar(GetRedPixelComponent(p));
1800 *q++=ScaleQuantumToChar(QuantumRange-GetOpacityPixelComponent(p));
1803 if (image->previous == (Image *) NULL)
1805 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
1807 if (status == MagickFalse)
1814 if ((type > 2) && (bmp_info.bits_per_pixel == 8))
1815 if (image_info->compression != NoCompression)
1821 Convert run-length encoded raster pixels.
1823 length=(size_t) (2*(bytes_per_line+2)*(image->rows+2)+2);
1824 bmp_data=(unsigned char *) NULL;
1825 if (~length >= bytes_per_line)
1826 bmp_data=(unsigned char *) AcquireQuantumMemory(length+
1827 bytes_per_line,sizeof(*bmp_data));
1828 if (bmp_data == (unsigned char *) NULL)
1830 pixels=(unsigned char *) RelinquishMagickMemory(pixels);
1831 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
1833 bmp_info.file_size-=bmp_info.image_size;
1834 bmp_info.image_size=(unsigned int) EncodeImage(image,bytes_per_line,
1836 bmp_info.file_size+=bmp_info.image_size;
1837 pixels=(unsigned char *) RelinquishMagickMemory(pixels);
1839 bmp_info.compression=BI_RLE8;
1842 Write BMP for Windows, all versions, 14-byte header.
1844 if (image->debug != MagickFalse)
1846 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1847 " Writing BMP version %.20g datastream",(double) type);
1848 if (image->storage_class == DirectClass)
1849 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1850 " Storage class=DirectClass");
1852 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1853 " Storage class=PseudoClass");
1854 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1855 " Image depth=%.20g",(double) image->depth);
1856 if (image->matte != MagickFalse)
1857 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1860 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1861 " Matte=MagickFalse");
1862 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1863 " BMP bits_per_pixel=%.20g",(double) bmp_info.bits_per_pixel);
1864 switch ((int) bmp_info.compression)
1868 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1869 " Compression=BI_RGB");
1874 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1875 " Compression=BI_RLE8");
1880 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1881 " Compression=BI_BITFIELDS");
1886 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1887 " Compression=UNKNOWN (%u)",bmp_info.compression);
1891 if (bmp_info.number_colors == 0)
1892 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1893 " Number_colors=unspecified");
1895 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1896 " Number_colors=%u",bmp_info.number_colors);
1898 (void) WriteBlob(image,2,(unsigned char *) "BM");
1899 (void) WriteBlobLSBLong(image,bmp_info.file_size);
1900 (void) WriteBlobLSBLong(image,bmp_info.ba_offset); /* always 0 */
1901 (void) WriteBlobLSBLong(image,bmp_info.offset_bits);
1905 Write 12-byte version 2 bitmap header.
1907 (void) WriteBlobLSBLong(image,bmp_info.size);
1908 (void) WriteBlobLSBShort(image,(unsigned short) bmp_info.width);
1909 (void) WriteBlobLSBShort(image,(unsigned short) bmp_info.height);
1910 (void) WriteBlobLSBShort(image,bmp_info.planes);
1911 (void) WriteBlobLSBShort(image,bmp_info.bits_per_pixel);
1916 Write 40-byte version 3+ bitmap header.
1918 (void) WriteBlobLSBLong(image,bmp_info.size);
1919 (void) WriteBlobLSBLong(image,(unsigned int) bmp_info.width);
1920 (void) WriteBlobLSBLong(image,(unsigned int) bmp_info.height);
1921 (void) WriteBlobLSBShort(image,bmp_info.planes);
1922 (void) WriteBlobLSBShort(image,bmp_info.bits_per_pixel);
1923 (void) WriteBlobLSBLong(image,bmp_info.compression);
1924 (void) WriteBlobLSBLong(image,bmp_info.image_size);
1925 (void) WriteBlobLSBLong(image,bmp_info.x_pixels);
1926 (void) WriteBlobLSBLong(image,bmp_info.y_pixels);
1927 (void) WriteBlobLSBLong(image,bmp_info.number_colors);
1928 (void) WriteBlobLSBLong(image,bmp_info.colors_important);
1930 if ((type > 3) && ((image->matte != MagickFalse) ||
1931 (have_color_info != MagickFalse)))
1934 Write the rest of the 108-byte BMP Version 4 header.
1936 (void) WriteBlobLSBLong(image,0x00ff0000U); /* Red mask */
1937 (void) WriteBlobLSBLong(image,0x0000ff00U); /* Green mask */
1938 (void) WriteBlobLSBLong(image,0x000000ffU); /* Blue mask */
1939 (void) WriteBlobLSBLong(image,0xff000000U); /* Alpha mask */
1940 (void) WriteBlobLSBLong(image,0x00000001U); /* CSType==Calib. RGB */
1941 (void) WriteBlobLSBLong(image,(unsigned int)
1942 image->chromaticity.red_primary.x*0x3ffffff);
1943 (void) WriteBlobLSBLong(image,(unsigned int)
1944 image->chromaticity.red_primary.y*0x3ffffff);
1945 (void) WriteBlobLSBLong(image,(unsigned int)
1946 (1.000f-(image->chromaticity.red_primary.x+
1947 image->chromaticity.red_primary.y)*0x3ffffff));
1948 (void) WriteBlobLSBLong(image,(unsigned int)
1949 image->chromaticity.green_primary.x*0x3ffffff);
1950 (void) WriteBlobLSBLong(image,(unsigned int)
1951 image->chromaticity.green_primary.y*0x3ffffff);
1952 (void) WriteBlobLSBLong(image,(unsigned int)
1953 (1.000f-(image->chromaticity.green_primary.x+
1954 image->chromaticity.green_primary.y)*0x3ffffff));
1955 (void) WriteBlobLSBLong(image,(unsigned int)
1956 image->chromaticity.blue_primary.x*0x3ffffff);
1957 (void) WriteBlobLSBLong(image,(unsigned int)
1958 image->chromaticity.blue_primary.y*0x3ffffff);
1959 (void) WriteBlobLSBLong(image,(unsigned int)
1960 (1.000f-(image->chromaticity.blue_primary.x+
1961 image->chromaticity.blue_primary.y)*0x3ffffff));
1962 (void) WriteBlobLSBLong(image,(unsigned int)
1963 bmp_info.gamma_scale.x*0xffff);
1964 (void) WriteBlobLSBLong(image,(unsigned int)
1965 bmp_info.gamma_scale.y*0xffff);
1966 (void) WriteBlobLSBLong(image,(unsigned int)
1967 bmp_info.gamma_scale.z*0xffff);
1968 if ((image->rendering_intent != UndefinedIntent) ||
1969 (profile != (StringInfo *) NULL))
1974 switch ((int) image->rendering_intent)
1976 case SaturationIntent:
1978 intent=LCS_GM_BUSINESS;
1981 case RelativeIntent:
1983 intent=LCS_GM_GRAPHICS;
1986 case PerceptualIntent:
1988 intent=LCS_GM_IMAGES;
1991 case AbsoluteIntent:
1993 intent=LCS_GM_ABS_COLORIMETRIC;
2002 (void) WriteBlobLSBLong(image,(unsigned int) intent);
2003 (void) WriteBlobLSBLong(image,0x00); /* dummy profile data */
2004 (void) WriteBlobLSBLong(image,0x00); /* dummy profile length */
2005 (void) WriteBlobLSBLong(image,0x00); /* reserved */
2008 if (image->storage_class == PseudoClass)
2014 Dump colormap to file.
2016 if (image->debug != MagickFalse)
2017 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
2018 " Colormap: %.20g entries",(double) image->colors);
2019 bmp_colormap=(unsigned char *) AcquireQuantumMemory((size_t) (1UL <<
2020 bmp_info.bits_per_pixel),4*sizeof(*bmp_colormap));
2021 if (bmp_colormap == (unsigned char *) NULL)
2022 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
2024 for (i=0; i < (ssize_t) MagickMin((ssize_t) image->colors,(ssize_t) bmp_info.number_colors); i++)
2026 *q++=ScaleQuantumToChar(image->colormap[i].blue);
2027 *q++=ScaleQuantumToChar(image->colormap[i].green);
2028 *q++=ScaleQuantumToChar(image->colormap[i].red);
2030 *q++=(unsigned char) 0x0;
2032 for ( ; i < (ssize_t) (1UL << bmp_info.bits_per_pixel); i++)
2034 *q++=(unsigned char) 0x00;
2035 *q++=(unsigned char) 0x00;
2036 *q++=(unsigned char) 0x00;
2038 *q++=(unsigned char) 0x00;
2041 (void) WriteBlob(image,(size_t) (3*(1L << bmp_info.bits_per_pixel)),
2044 (void) WriteBlob(image,(size_t) (4*(1L << bmp_info.bits_per_pixel)),
2046 bmp_colormap=(unsigned char *) RelinquishMagickMemory(bmp_colormap);
2048 if (image->debug != MagickFalse)
2049 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
2050 " Pixels: %u bytes",bmp_info.image_size);
2051 (void) WriteBlob(image,(size_t) bmp_info.image_size,pixels);
2052 pixels=(unsigned char *) RelinquishMagickMemory(pixels);
2053 if (GetNextImageInList(image) == (Image *) NULL)
2055 image=SyncNextImageInList(image);
2056 status=SetImageProgress(image,SaveImagesTag,scene++,
2057 GetImageListLength(image));
2058 if (status == MagickFalse)
2060 } while (image_info->adjoin != MagickFalse);
2061 (void) CloseBlob(image);