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)
516 register unsigned char
538 assert(image_info != (const ImageInfo *) NULL);
539 assert(image_info->signature == MagickSignature);
540 if (image_info->debug != MagickFalse)
541 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
542 image_info->filename);
543 assert(exception != (ExceptionInfo *) NULL);
544 assert(exception->signature == MagickSignature);
545 image=AcquireImage(image_info);
546 status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception);
547 if (status == MagickFalse)
549 image=DestroyImageList(image);
550 return((Image *) NULL);
553 Determine if this a BMP file.
555 (void) ResetMagickMemory(&bmp_info,0,sizeof(bmp_info));
556 bmp_info.ba_offset=0;
558 count=ReadBlob(image,2,magick);
572 Verify BMP identifier.
574 if (bmp_info.ba_offset == 0)
575 start_position=TellBlob(image)-2;
576 bmp_info.ba_offset=0;
577 while (LocaleNCompare((char *) magick,"BA",2) == 0)
579 bmp_info.file_size=ReadBlobLSBLong(image);
580 bmp_info.ba_offset=ReadBlobLSBLong(image);
581 bmp_info.offset_bits=ReadBlobLSBLong(image);
582 count=ReadBlob(image,2,magick);
586 if (image->debug != MagickFalse)
587 (void) LogMagickEvent(CoderEvent,GetMagickModule()," Magick: %c%c",
588 magick[0],magick[1]);
589 if ((count == 0) || ((LocaleNCompare((char *) magick,"BM",2) != 0) &&
590 (LocaleNCompare((char *) magick,"CI",2) != 0)))
591 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
592 bmp_info.file_size=ReadBlobLSBLong(image);
593 (void) ReadBlobLSBLong(image);
594 bmp_info.offset_bits=ReadBlobLSBLong(image);
595 bmp_info.size=ReadBlobLSBLong(image);
596 if (image->debug != MagickFalse)
597 (void) LogMagickEvent(CoderEvent,GetMagickModule()," BMP size: %u",
599 if (bmp_info.size == 12)
604 bmp_info.width=(short) ReadBlobLSBShort(image);
605 bmp_info.height=(short) ReadBlobLSBShort(image);
606 bmp_info.planes=ReadBlobLSBShort(image);
607 bmp_info.bits_per_pixel=ReadBlobLSBShort(image);
610 bmp_info.number_colors=0;
611 bmp_info.compression=BI_RGB;
612 bmp_info.image_size=0;
613 bmp_info.alpha_mask=0;
614 if (image->debug != MagickFalse)
616 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
617 " Format: OS/2 Bitmap");
618 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
619 " Geometry: %.20gx%.20g",(double) bmp_info.width,(double)
626 Microsoft Windows BMP image file.
628 if (bmp_info.size < 40)
629 ThrowReaderException(CorruptImageError,"NonOS2HeaderSizeError");
630 bmp_info.width=(int) ReadBlobLSBLong(image);
631 bmp_info.height=(int) ReadBlobLSBLong(image);
632 bmp_info.planes=ReadBlobLSBShort(image);
633 bmp_info.bits_per_pixel=ReadBlobLSBShort(image);
634 bmp_info.compression=ReadBlobLSBLong(image);
635 bmp_info.image_size=ReadBlobLSBLong(image);
636 bmp_info.x_pixels=ReadBlobLSBLong(image);
637 bmp_info.y_pixels=ReadBlobLSBLong(image);
638 bmp_info.number_colors=ReadBlobLSBLong(image);
639 bmp_info.colors_important=ReadBlobLSBLong(image);
642 if (image->debug != MagickFalse)
644 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
645 " Format: MS Windows bitmap");
646 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
647 " Geometry: %.20gx%.20g",(double) bmp_info.width,(double)
649 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
650 " Bits per pixel: %.20g",(double) 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=(int) 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.y+
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.y+
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.y+
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);
783 (void) ReadBlobLSBLong(image); /* Reserved byte */
786 if ((MagickSizeType) bmp_info.file_size > GetBlobSize(image))
787 (void) ThrowMagickException(exception,GetMagickModule(),CorruptImageError,
788 "LengthAndFilesizeDoNotMatch","`%s'",image->filename);
790 if ((MagickSizeType) bmp_info.file_size < GetBlobSize(image))
791 (void) ThrowMagickException(exception,GetMagickModule(),
792 CorruptImageWarning,"LengthAndFilesizeDoNotMatch","`%s'",
794 if (bmp_info.width <= 0)
795 ThrowReaderException(CorruptImageError,"NegativeOrZeroImageSize");
796 if (bmp_info.height == 0)
797 ThrowReaderException(CorruptImageError,"NegativeOrZeroImageSize");
798 if (bmp_info.planes != 1)
799 ThrowReaderException(CorruptImageError,"StaticPlanesValueNotEqualToOne");
800 if ((bmp_info.bits_per_pixel != 1) && (bmp_info.bits_per_pixel != 4) &&
801 (bmp_info.bits_per_pixel != 8) && (bmp_info.bits_per_pixel != 16) &&
802 (bmp_info.bits_per_pixel != 24) && (bmp_info.bits_per_pixel != 32))
803 ThrowReaderException(CorruptImageError,"UnrecognizedBitsPerPixel");
804 if (bmp_info.number_colors > (1U << bmp_info.bits_per_pixel))
806 if (bmp_info.bits_per_pixel < 24)
807 ThrowReaderException(CorruptImageError,"UnrecognizedNumberOfColors");
808 bmp_info.number_colors=0;
810 if ((bmp_info.compression == 1) && (bmp_info.bits_per_pixel != 8))
811 ThrowReaderException(CorruptImageError,"UnrecognizedBitsPerPixel");
812 if ((bmp_info.compression == 2) && (bmp_info.bits_per_pixel != 4))
813 ThrowReaderException(CorruptImageError,"UnrecognizedBitsPerPixel");
814 if ((bmp_info.compression == 3) && (bmp_info.bits_per_pixel < 16))
815 ThrowReaderException(CorruptImageError,"UnrecognizedBitsPerPixel");
816 switch (bmp_info.compression)
824 ThrowReaderException(CoderError,"JPEGCompressNotSupported");
826 ThrowReaderException(CoderError,"PNGCompressNotSupported");
828 ThrowReaderException(CorruptImageError,"UnrecognizedImageCompression");
830 image->columns=(size_t) MagickAbsoluteValue(bmp_info.width);
831 image->rows=(size_t) MagickAbsoluteValue(bmp_info.height);
832 image->depth=bmp_info.bits_per_pixel <= 8 ? bmp_info.bits_per_pixel : 8;
833 if ((bmp_info.bits_per_pixel == 16) ||
834 (bmp_info.bits_per_pixel == 32))
835 image->matte=bmp_info.alpha_mask != 0 ? MagickTrue : MagickFalse;
836 if ((bmp_info.number_colors != 0) || (bmp_info.bits_per_pixel < 16))
841 image->storage_class=PseudoClass;
842 image->colors=bmp_info.number_colors;
844 if (image->colors == 0)
845 image->colors=one << bmp_info.bits_per_pixel;
847 if (image->storage_class == PseudoClass)
856 Read BMP raster colormap.
858 if (image->debug != MagickFalse)
859 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
860 " Reading colormap of %.20g colors",(double) image->colors);
861 if (AcquireImageColormap(image,image->colors) == MagickFalse)
862 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
863 bmp_colormap=(unsigned char *) AcquireQuantumMemory((size_t)
864 image->colors,4*sizeof(*bmp_colormap));
865 if (bmp_colormap == (unsigned char *) NULL)
866 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
867 if ((bmp_info.size == 12) || (bmp_info.size == 64))
871 offset=SeekBlob(image,start_position+14+bmp_info.size,SEEK_SET);
873 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
874 count=ReadBlob(image,packet_size*image->colors,bmp_colormap);
875 if (count != (ssize_t) (packet_size*image->colors))
876 ThrowReaderException(CorruptImageError,"InsufficientImageDataInFile");
878 for (i=0; i < (ssize_t) image->colors; i++)
880 image->colormap[i].blue=ScaleCharToQuantum(*p++);
881 image->colormap[i].green=ScaleCharToQuantum(*p++);
882 image->colormap[i].red=ScaleCharToQuantum(*p++);
883 if (packet_size == 4)
886 bmp_colormap=(unsigned char *) RelinquishMagickMemory(bmp_colormap);
888 if ((image_info->ping != MagickFalse) && (image_info->number_scenes != 0))
889 if (image->scene >= (image_info->scene+image_info->number_scenes-1))
894 offset=SeekBlob(image,start_position+bmp_info.offset_bits,SEEK_SET);
896 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
897 if (bmp_info.compression == BI_RLE4)
898 bmp_info.bits_per_pixel<<=1;
899 bytes_per_line=4*((image->columns*bmp_info.bits_per_pixel+31)/32);
900 length=(size_t) bytes_per_line*image->rows;
901 pixels=(unsigned char *) AcquireQuantumMemory((size_t) image->rows,
902 MagickMax(bytes_per_line,image->columns+256UL)*sizeof(*pixels));
903 if (pixels == (unsigned char *) NULL)
904 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
905 if ((bmp_info.compression == BI_RGB) ||
906 (bmp_info.compression == BI_BITFIELDS))
908 if (image->debug != MagickFalse)
909 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
910 " Reading pixels (%.20g bytes)",(double) length);
911 count=ReadBlob(image,length,pixels);
912 if (count != (ssize_t) length)
914 pixels=(unsigned char *) RelinquishMagickMemory(pixels);
915 ThrowReaderException(CorruptImageError,
916 "InsufficientImageDataInFile");
922 Convert run-length encoded raster pixels.
924 status=DecodeImage(image,bmp_info.compression,pixels);
925 if (status == MagickFalse)
927 pixels=(unsigned char *) RelinquishMagickMemory(pixels);
928 ThrowReaderException(CorruptImageError,
929 "UnableToRunlengthDecodeImage");
933 Initialize image structure.
935 image->x_resolution=(double) bmp_info.x_pixels/100.0;
936 image->y_resolution=(double) bmp_info.y_pixels/100.0;
937 image->units=PixelsPerCentimeterResolution;
939 Convert BMP raster image to pixel packets.
941 if (bmp_info.compression == BI_RGB)
943 bmp_info.alpha_mask=0;
944 bmp_info.red_mask=0x00ff0000U;
945 bmp_info.green_mask=0x0000ff00U;
946 bmp_info.blue_mask=0x000000ffU;
947 if (bmp_info.bits_per_pixel == 16)
952 bmp_info.red_mask=0x00007c00U;
953 bmp_info.green_mask=0x000003e0U;
954 bmp_info.blue_mask=0x0000001fU;
957 if ((bmp_info.bits_per_pixel == 16) || (bmp_info.bits_per_pixel == 32))
963 Get shift and quantum bits info from bitfield masks.
965 (void) ResetMagickMemory(&shift,0,sizeof(shift));
966 (void) ResetMagickMemory(&quantum_bits,0,sizeof(quantum_bits));
967 if (bmp_info.red_mask != 0)
968 while (((bmp_info.red_mask << shift.red) & 0x80000000UL) == 0)
970 if (bmp_info.green_mask != 0)
971 while (((bmp_info.green_mask << shift.green) & 0x80000000UL) == 0)
973 if (bmp_info.blue_mask != 0)
974 while (((bmp_info.blue_mask << shift.blue) & 0x80000000UL) == 0)
976 if (bmp_info.alpha_mask != 0)
977 while (((bmp_info.alpha_mask << shift.opacity) & 0x80000000UL) == 0)
980 while (((bmp_info.red_mask << sample) & 0x80000000UL) != 0)
982 quantum_bits.red=(Quantum) (sample-shift.red);
984 while (((bmp_info.green_mask << sample) & 0x80000000UL) != 0)
986 quantum_bits.green=(Quantum) (sample-shift.green);
988 while (((bmp_info.blue_mask << sample) & 0x80000000UL) != 0)
990 quantum_bits.blue=(Quantum) (sample-shift.blue);
991 sample=shift.opacity;
992 while (((bmp_info.alpha_mask << sample) & 0x80000000UL) != 0)
994 quantum_bits.opacity=(Quantum) (sample-shift.opacity);
996 switch (bmp_info.bits_per_pixel)
1001 Convert bitmap scanline.
1003 for (y=(ssize_t) image->rows-1; y >= 0; y--)
1005 p=pixels+(image->rows-y-1)*bytes_per_line;
1006 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
1007 if (q == (PixelPacket *) NULL)
1009 indexes=GetAuthenticIndexQueue(image);
1010 for (x=0; x < ((ssize_t) image->columns-7); x+=8)
1012 for (bit=0; bit < 8; bit++)
1014 index=(IndexPacket) (((*p) & (0x80 >> bit)) != 0 ? 0x01 : 0x00);
1015 indexes[x+bit]=index;
1016 *q++=image->colormap[(ssize_t) index];
1020 if ((image->columns % 8) != 0)
1022 for (bit=0; bit < (image->columns % 8); bit++)
1024 index=(IndexPacket) (((*p) & (0x80 >> bit)) != 0 ? 0x01 : 0x00);
1025 indexes[x+bit]=index;
1026 *q++=image->colormap[(ssize_t) index];
1030 if (SyncAuthenticPixels(image,exception) == MagickFalse)
1032 if (image->previous == (Image *) NULL)
1034 status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y,
1036 if (status == MagickFalse)
1045 Convert PseudoColor scanline.
1047 for (y=(ssize_t) image->rows-1; y >= 0; y--)
1049 p=pixels+(image->rows-y-1)*bytes_per_line;
1050 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
1051 if (q == (PixelPacket *) NULL)
1053 indexes=GetAuthenticIndexQueue(image);
1054 for (x=0; x < ((ssize_t) image->columns-1); x+=2)
1056 index=ConstrainColormapIndex(image,(*p >> 4) & 0x0f);
1058 *q++=image->colormap[(ssize_t) index];
1059 index=ConstrainColormapIndex(image,*p & 0x0f);
1061 *q++=image->colormap[(ssize_t) index];
1064 if ((image->columns % 2) != 0)
1066 index=ConstrainColormapIndex(image,(*p >> 4) & 0xf);
1068 *q++=image->colormap[(ssize_t) index];
1071 if (SyncAuthenticPixels(image,exception) == MagickFalse)
1073 if (image->previous == (Image *) NULL)
1075 status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y,
1077 if (status == MagickFalse)
1086 Convert PseudoColor scanline.
1088 if ((bmp_info.compression == BI_RLE8) ||
1089 (bmp_info.compression == BI_RLE4))
1090 bytes_per_line=image->columns;
1091 for (y=(ssize_t) image->rows-1; y >= 0; y--)
1093 p=pixels+(image->rows-y-1)*bytes_per_line;
1094 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
1095 if (q == (PixelPacket *) NULL)
1097 indexes=GetAuthenticIndexQueue(image);
1098 for (x = (ssize_t)image->columns; x != 0; --x)
1100 index=ConstrainColormapIndex(image,*p);
1101 *q=image->colormap[(ssize_t) index];
1102 SetIndexPixelComponent(indexes,index);
1107 if (SyncAuthenticPixels(image,exception) == MagickFalse)
1109 offset=(MagickOffsetType) (image->rows-y-1);
1110 if (image->previous == (Image *) NULL)
1112 status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y,
1114 if (status == MagickFalse)
1126 Convert bitfield encoded 16-bit PseudoColor scanline.
1128 if (bmp_info.compression != BI_RGB &&
1129 bmp_info.compression != BI_BITFIELDS)
1131 pixels=(unsigned char *) RelinquishMagickMemory(pixels);
1132 ThrowReaderException(CorruptImageError,
1133 "UnrecognizedImageCompression");
1135 bytes_per_line=2*(image->columns+image->columns % 2);
1136 image->storage_class=DirectClass;
1137 for (y=(ssize_t) image->rows-1; y >= 0; y--)
1139 p=pixels+(image->rows-y-1)*bytes_per_line;
1140 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
1141 if (q == (PixelPacket *) NULL)
1143 for (x=0; x < (ssize_t) image->columns; x++)
1145 pixel=(size_t) (*p++);
1147 red=((pixel & bmp_info.red_mask) << shift.red) >> 16;
1148 if (quantum_bits.red == 5)
1149 red|=((red & 0xe000) >> 5);
1150 if (quantum_bits.red <= 8)
1151 red|=((red & 0xff00) >> 8);
1152 green=((pixel & bmp_info.green_mask) << shift.green) >> 16;
1153 if (quantum_bits.green == 5)
1154 green|=((green & 0xe000) >> 5);
1155 if (quantum_bits.green == 6)
1156 green|=((green & 0xc000) >> 6);
1157 if (quantum_bits.green <= 8)
1158 green|=((green & 0xff00) >> 8);
1159 blue=((pixel & bmp_info.blue_mask) << shift.blue) >> 16;
1160 if (quantum_bits.blue == 5)
1161 blue|=((blue & 0xe000) >> 5);
1162 if (quantum_bits.blue <= 8)
1163 blue|=((blue & 0xff00) >> 8);
1164 opacity=((pixel & bmp_info.alpha_mask) << shift.opacity) >> 16;
1165 if (quantum_bits.opacity <= 8)
1166 opacity|=((opacity & 0xff00) >> 8);
1167 SetRedPixelComponent(q,ScaleShortToQuantum((unsigned short) red));
1168 SetGreenPixelComponent(q,ScaleShortToQuantum((unsigned short)
1170 SetBluePixelComponent(q,ScaleShortToQuantum((unsigned short) blue));
1171 SetOpacityPixelComponent(q,OpaqueOpacity);
1172 if (image->matte != MagickFalse)
1173 SetOpacityPixelComponent(q,ScaleShortToQuantum((unsigned short)
1177 if (SyncAuthenticPixels(image,exception) == MagickFalse)
1179 offset=(MagickOffsetType) (image->rows-y-1);
1180 if (image->previous == (Image *) NULL)
1182 status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y,
1184 if (status == MagickFalse)
1193 Convert DirectColor scanline.
1195 bytes_per_line=4*((image->columns*24+31)/32);
1196 for (y=(ssize_t) image->rows-1; y >= 0; y--)
1198 p=pixels+(image->rows-y-1)*bytes_per_line;
1199 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
1200 if (q == (PixelPacket *) NULL)
1202 for (x=0; x < (ssize_t) image->columns; x++)
1204 SetBluePixelComponent(q,ScaleCharToQuantum(*p++));
1205 SetGreenPixelComponent(q,ScaleCharToQuantum(*p++));
1206 SetRedPixelComponent(q,ScaleCharToQuantum(*p++));
1207 SetOpacityPixelComponent(q,OpaqueOpacity);
1210 if (SyncAuthenticPixels(image,exception) == MagickFalse)
1212 offset=(MagickOffsetType) (image->rows-y-1);
1213 if (image->previous == (Image *) NULL)
1215 status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y,
1217 if (status == MagickFalse)
1226 Convert bitfield encoded DirectColor scanline.
1228 if ((bmp_info.compression != BI_RGB) &&
1229 (bmp_info.compression != BI_BITFIELDS))
1231 pixels=(unsigned char *) RelinquishMagickMemory(pixels);
1232 ThrowReaderException(CorruptImageError,
1233 "UnrecognizedImageCompression");
1235 bytes_per_line=4*(image->columns);
1236 for (y=(ssize_t) image->rows-1; y >= 0; y--)
1241 p=pixels+(image->rows-y-1)*bytes_per_line;
1242 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
1243 if (q == (PixelPacket *) NULL)
1245 for (x=0; x < (ssize_t) image->columns; x++)
1247 pixel=(size_t) (*p++);
1249 pixel|=(*p++ << 16);
1250 pixel|=(*p++ << 24);
1251 red=((pixel & bmp_info.red_mask) << shift.red) >> 16;
1252 if (quantum_bits.red == 8)
1254 green=((pixel & bmp_info.green_mask) << shift.green) >> 16;
1255 if (quantum_bits.green == 8)
1256 green|=(green >> 8);
1257 blue=((pixel & bmp_info.blue_mask) << shift.blue) >> 16;
1258 if (quantum_bits.blue == 8)
1260 opacity=((pixel & bmp_info.alpha_mask) << shift.opacity) >> 16;
1261 if (quantum_bits.opacity == 8)
1262 opacity|=(opacity >> 8);
1263 SetRedPixelComponent(q,ScaleShortToQuantum((unsigned short) red));
1264 SetGreenPixelComponent(q,ScaleShortToQuantum((unsigned short) green));
1265 SetBluePixelComponent(q,ScaleShortToQuantum((unsigned short) blue));
1266 SetOpacityPixelComponent(q,OpaqueOpacity);
1267 if (image->matte != MagickFalse)
1268 q->opacity=ScaleShortToQuantum((unsigned short) (65535-opacity));
1271 if (SyncAuthenticPixels(image,exception) == MagickFalse)
1273 offset=(MagickOffsetType) (image->rows-y-1);
1274 if (image->previous == (Image *) NULL)
1276 status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y,
1278 if (status == MagickFalse)
1286 pixels=(unsigned char *) RelinquishMagickMemory(pixels);
1287 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
1290 pixels=(unsigned char *) RelinquishMagickMemory(pixels);
1291 if (EOFBlob(image) != MagickFalse)
1293 ThrowFileException(exception,CorruptImageError,"UnexpectedEndOfFile",
1297 if (bmp_info.height < 0)
1303 Correct image orientation.
1305 flipped_image=FlipImage(image,exception);
1306 if (flipped_image != (Image *) NULL)
1308 DuplicateBlob(flipped_image,image);
1309 image=DestroyImage(image);
1310 image=flipped_image;
1314 Proceed to next image.
1316 if (image_info->number_scenes != 0)
1317 if (image->scene >= (image_info->scene+image_info->number_scenes-1))
1320 if (bmp_info.ba_offset != 0)
1322 offset=SeekBlob(image,(MagickOffsetType) bmp_info.ba_offset,SEEK_SET);
1324 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
1326 count=ReadBlob(image,2,magick);
1327 if ((count == 2) && (IsBMP(magick,2) != MagickFalse))
1330 Acquire next image structure.
1332 AcquireNextImage(image_info,image);
1333 if (GetNextImageInList(image) == (Image *) NULL)
1335 image=DestroyImageList(image);
1336 return((Image *) NULL);
1338 image=SyncNextImageInList(image);
1339 status=SetImageProgress(image,LoadImagesTag,TellBlob(image),
1340 GetBlobSize(image));
1341 if (status == MagickFalse)
1344 } while (IsBMP(magick,2) != MagickFalse);
1345 (void) CloseBlob(image);
1346 return(GetFirstImageInList(image));
1350 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1354 % R e g i s t e r B M P I m a g e %
1358 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1360 % RegisterBMPImage() adds attributes for the BMP image format to
1361 % the list of supported formats. The attributes include the image format
1362 % tag, a method to read and/or write the format, whether the format
1363 % supports the saving of more than one frame to the same file or blob,
1364 % whether the format supports native in-memory I/O, and a brief
1365 % description of the format.
1367 % The format of the RegisterBMPImage method is:
1369 % size_t RegisterBMPImage(void)
1372 ModuleExport size_t RegisterBMPImage(void)
1377 entry=SetMagickInfo("BMP");
1378 entry->decoder=(DecodeImageHandler *) ReadBMPImage;
1379 entry->encoder=(EncodeImageHandler *) WriteBMPImage;
1380 entry->magick=(IsImageFormatHandler *) IsBMP;
1381 entry->description=ConstantString("Microsoft Windows bitmap image");
1382 entry->module=ConstantString("BMP");
1383 entry->adjoin=MagickFalse;
1384 entry->seekable_stream=MagickTrue;
1385 (void) RegisterMagickInfo(entry);
1386 entry=SetMagickInfo("BMP2");
1387 entry->encoder=(EncodeImageHandler *) WriteBMPImage;
1388 entry->magick=(IsImageFormatHandler *) IsBMP;
1389 entry->description=ConstantString("Microsoft Windows bitmap image (V2)");
1390 entry->module=ConstantString("BMP");
1391 entry->adjoin=MagickFalse;
1392 entry->seekable_stream=MagickTrue;
1393 (void) RegisterMagickInfo(entry);
1394 entry=SetMagickInfo("BMP3");
1395 entry->encoder=(EncodeImageHandler *) WriteBMPImage;
1396 entry->magick=(IsImageFormatHandler *) IsBMP;
1397 entry->description=ConstantString("Microsoft Windows bitmap image (V3)");
1398 entry->module=ConstantString("BMP");
1399 entry->adjoin=MagickFalse;
1400 entry->seekable_stream=MagickTrue;
1401 (void) RegisterMagickInfo(entry);
1402 return(MagickImageCoderSignature);
1406 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1410 % U n r e g i s t e r B M P I m a g e %
1414 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1416 % UnregisterBMPImage() removes format registrations made by the
1417 % BMP module from the list of supported formats.
1419 % The format of the UnregisterBMPImage method is:
1421 % UnregisterBMPImage(void)
1424 ModuleExport void UnregisterBMPImage(void)
1426 (void) UnregisterMagickInfo("BMP");
1427 (void) UnregisterMagickInfo("BMP2");
1428 (void) UnregisterMagickInfo("BMP3");
1432 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1436 % W r i t e B M P I m a g e %
1440 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1442 % WriteBMPImage() writes an image in Microsoft Windows bitmap encoded
1443 % image format, version 3 for Windows or (if the image has a matte channel)
1446 % The format of the WriteBMPImage method is:
1448 % MagickBooleanType WriteBMPImage(const ImageInfo *image_info,Image *image)
1450 % A description of each parameter follows.
1452 % o image_info: the image info.
1454 % o image: The image.
1457 static MagickBooleanType WriteBMPImage(const ImageInfo *image_info,Image *image)
1475 register const IndexPacket
1478 register const PixelPacket
1485 register unsigned char
1497 Open output image file.
1499 assert(image_info != (const ImageInfo *) NULL);
1500 assert(image_info->signature == MagickSignature);
1501 assert(image != (Image *) NULL);
1502 assert(image->signature == MagickSignature);
1503 if (image->debug != MagickFalse)
1504 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1505 status=OpenBlob(image_info,image,WriteBinaryBlobMode,&image->exception);
1506 if (status == MagickFalse)
1509 if (LocaleCompare(image_info->magick,"BMP2") == 0)
1512 if (LocaleCompare(image_info->magick,"BMP3") == 0)
1518 Initialize BMP raster file header.
1520 if (image->colorspace != RGBColorspace)
1521 (void) TransformImageColorspace(image,RGBColorspace);
1522 (void) ResetMagickMemory(&bmp_info,0,sizeof(bmp_info));
1523 bmp_info.file_size=14+12;
1525 bmp_info.file_size+=28;
1526 bmp_info.offset_bits=bmp_info.file_size;
1527 bmp_info.compression=BI_RGB;
1528 if ((image->storage_class == PseudoClass) && (image->colors > 256))
1529 (void) SetImageStorageClass(image,DirectClass);
1530 if (image->storage_class != DirectClass)
1533 Colormapped BMP raster.
1535 bmp_info.bits_per_pixel=8;
1536 if (image->colors <= 2)
1537 bmp_info.bits_per_pixel=1;
1539 if (image->colors <= 16)
1540 bmp_info.bits_per_pixel=4;
1542 if (image->colors <= 256)
1543 bmp_info.bits_per_pixel=8;
1544 if (image_info->compression == RLECompression)
1545 bmp_info.bits_per_pixel=8;
1546 bmp_info.number_colors=1U << bmp_info.bits_per_pixel;
1547 if (image->matte != MagickFalse)
1548 (void) SetImageStorageClass(image,DirectClass);
1550 if ((size_t) bmp_info.number_colors < image->colors)
1551 (void) SetImageStorageClass(image,DirectClass);
1554 bmp_info.file_size+=3*(1UL << bmp_info.bits_per_pixel);
1555 bmp_info.offset_bits+=3*(1UL << bmp_info.bits_per_pixel);
1558 bmp_info.file_size+=(1UL << bmp_info.bits_per_pixel);
1559 bmp_info.offset_bits+=(1UL << bmp_info.bits_per_pixel);
1563 if (image->storage_class == DirectClass)
1566 Full color BMP raster.
1568 bmp_info.number_colors=0;
1569 bmp_info.bits_per_pixel=(unsigned short)
1570 ((type > 3) && (image->matte != MagickFalse) ? 32 : 24);
1571 bmp_info.compression=(unsigned int) ((type > 3) &&
1572 (image->matte != MagickFalse) ? BI_BITFIELDS : BI_RGB);
1574 bytes_per_line=4*((image->columns*bmp_info.bits_per_pixel+31)/32);
1575 bmp_info.ba_offset=0;
1576 profile=GetImageProfile(image,"icc");
1577 have_color_info=(image->rendering_intent != UndefinedIntent) ||
1578 (profile != (StringInfo *) NULL) || (image->gamma != 0.0) ? MagickTrue :
1583 if ((type == 3) || ((image->matte == MagickFalse) &&
1584 (have_color_info == MagickFalse)))
1596 if ((image->rendering_intent != UndefinedIntent) ||
1597 (profile != (StringInfo *) NULL))
1602 bmp_info.file_size+=extra_size;
1603 bmp_info.offset_bits+=extra_size;
1605 bmp_info.width=(ssize_t) image->columns;
1606 bmp_info.height=(ssize_t) image->rows;
1608 bmp_info.image_size=(unsigned int) (bytes_per_line*image->rows);
1609 bmp_info.file_size+=bmp_info.image_size;
1610 bmp_info.x_pixels=75*39;
1611 bmp_info.y_pixels=75*39;
1612 switch (image->units)
1614 case UndefinedResolution:
1615 case PixelsPerInchResolution:
1617 bmp_info.x_pixels=(unsigned int) (100.0*image->x_resolution/2.54);
1618 bmp_info.y_pixels=(unsigned int) (100.0*image->y_resolution/2.54);
1621 case PixelsPerCentimeterResolution:
1623 bmp_info.x_pixels=(unsigned int) (100.0*image->x_resolution);
1624 bmp_info.y_pixels=(unsigned int) (100.0*image->y_resolution);
1628 bmp_info.colors_important=bmp_info.number_colors;
1630 Convert MIFF to BMP raster pixels.
1632 pixels=(unsigned char *) AcquireQuantumMemory((size_t) bmp_info.image_size,
1634 if (pixels == (unsigned char *) NULL)
1635 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
1636 (void) ResetMagickMemory(pixels,0,(size_t) bmp_info.image_size);
1637 switch (bmp_info.bits_per_pixel)
1646 Convert PseudoClass image to a BMP monochrome image.
1648 for (y=0; y < (ssize_t) image->rows; y++)
1650 p=GetVirtualPixels(image,0,y,image->columns,1,&image->exception);
1651 if (p == (const PixelPacket *) NULL)
1653 indexes=GetVirtualIndexQueue(image);
1654 q=pixels+(image->rows-y-1)*bytes_per_line;
1657 for (x=0; x < (ssize_t) image->columns; x++)
1660 byte|=indexes[x] != 0 ? 0x01 : 0x00;
1664 *q++=(unsigned char) byte;
1671 *q++=(unsigned char) (byte << (8-bit));
1674 for (x=(ssize_t) (image->columns+7)/8; x < (ssize_t) bytes_per_line; x++)
1676 if (image->previous == (Image *) NULL)
1678 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
1680 if (status == MagickFalse)
1693 Convert PseudoClass image to a BMP monochrome image.
1695 for (y=0; y < (ssize_t) image->rows; y++)
1697 p=GetVirtualPixels(image,0,y,image->columns,1,&image->exception);
1698 if (p == (const PixelPacket *) NULL)
1700 indexes=GetVirtualIndexQueue(image);
1701 q=pixels+(image->rows-y-1)*bytes_per_line;
1704 for (x=0; x < (ssize_t) image->columns; x++)
1707 byte|=((size_t) indexes[x] & 0x0f);
1711 *q++=(unsigned char) byte;
1718 *q++=(unsigned char) (byte << 4);
1721 for (x=(ssize_t) (image->columns+1)/2; x < (ssize_t) bytes_per_line; x++)
1723 if (image->previous == (Image *) NULL)
1725 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
1727 if (status == MagickFalse)
1736 Convert PseudoClass packet to BMP pixel.
1738 for (y=0; y < (ssize_t) image->rows; y++)
1740 p=GetVirtualPixels(image,0,y,image->columns,1,&image->exception);
1741 if (p == (const PixelPacket *) NULL)
1743 indexes=GetVirtualIndexQueue(image);
1744 q=pixels+(image->rows-y-1)*bytes_per_line;
1745 for (x=0; x < (ssize_t) image->columns; x++)
1746 *q++=(unsigned char) indexes[x];
1747 for ( ; x < (ssize_t) bytes_per_line; x++)
1749 if (image->previous == (Image *) NULL)
1751 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
1753 if (status == MagickFalse)
1762 Convert DirectClass packet to BMP BGR888.
1764 for (y=0; y < (ssize_t) image->rows; y++)
1766 p=GetVirtualPixels(image,0,y,image->columns,1,&image->exception);
1767 if (p == (const PixelPacket *) NULL)
1769 q=pixels+(image->rows-y-1)*bytes_per_line;
1770 for (x=0; x < (ssize_t) image->columns; x++)
1772 *q++=ScaleQuantumToChar(GetBluePixelComponent(p));
1773 *q++=ScaleQuantumToChar(GetGreenPixelComponent(p));
1774 *q++=ScaleQuantumToChar(GetRedPixelComponent(p));
1777 for (x=3L*(ssize_t) image->columns; x < (ssize_t) bytes_per_line; x++)
1779 if (image->previous == (Image *) NULL)
1781 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
1783 if (status == MagickFalse)
1792 Convert DirectClass packet to ARGB8888 pixel.
1794 for (y=0; y < (ssize_t) image->rows; y++)
1796 p=GetVirtualPixels(image,0,y,image->columns,1,&image->exception);
1797 if (p == (const PixelPacket *) NULL)
1799 q=pixels+(image->rows-y-1)*bytes_per_line;
1800 for (x=0; x < (ssize_t) image->columns; x++)
1802 *q++=ScaleQuantumToChar(GetBluePixelComponent(p));
1803 *q++=ScaleQuantumToChar(GetGreenPixelComponent(p));
1804 *q++=ScaleQuantumToChar(GetRedPixelComponent(p));
1805 *q++=ScaleQuantumToChar(QuantumRange-GetOpacityPixelComponent(p));
1808 if (image->previous == (Image *) NULL)
1810 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
1812 if (status == MagickFalse)
1819 if ((type > 2) && (bmp_info.bits_per_pixel == 8))
1820 if (image_info->compression != NoCompression)
1826 Convert run-length encoded raster pixels.
1828 length=(size_t) (2*(bytes_per_line+2)*(image->rows+2)+2);
1829 bmp_data=(unsigned char *) NULL;
1830 if (~length >= bytes_per_line)
1831 bmp_data=(unsigned char *) AcquireQuantumMemory(length+
1832 bytes_per_line,sizeof(*bmp_data));
1833 if (bmp_data == (unsigned char *) NULL)
1835 pixels=(unsigned char *) RelinquishMagickMemory(pixels);
1836 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
1838 bmp_info.file_size-=bmp_info.image_size;
1839 bmp_info.image_size=(unsigned int) EncodeImage(image,bytes_per_line,
1841 bmp_info.file_size+=bmp_info.image_size;
1842 pixels=(unsigned char *) RelinquishMagickMemory(pixels);
1844 bmp_info.compression=BI_RLE8;
1847 Write BMP for Windows, all versions, 14-byte header.
1849 if (image->debug != MagickFalse)
1851 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1852 " Writing BMP version %.20g datastream",(double) type);
1853 if (image->storage_class == DirectClass)
1854 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1855 " Storage class=DirectClass");
1857 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1858 " Storage class=PseudoClass");
1859 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1860 " Image depth=%.20g",(double) image->depth);
1861 if (image->matte != MagickFalse)
1862 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1865 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1866 " Matte=MagickFalse");
1867 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1868 " BMP bits_per_pixel=%.20g",(double) bmp_info.bits_per_pixel);
1869 switch ((int) bmp_info.compression)
1873 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1874 " Compression=BI_RGB");
1879 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1880 " Compression=BI_RLE8");
1885 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1886 " Compression=BI_BITFIELDS");
1891 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1892 " Compression=UNKNOWN (%u)",bmp_info.compression);
1896 if (bmp_info.number_colors == 0)
1897 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1898 " Number_colors=unspecified");
1900 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1901 " Number_colors=%u",bmp_info.number_colors);
1903 (void) WriteBlob(image,2,(unsigned char *) "BM");
1904 (void) WriteBlobLSBLong(image,bmp_info.file_size);
1905 (void) WriteBlobLSBLong(image,bmp_info.ba_offset); /* always 0 */
1906 (void) WriteBlobLSBLong(image,bmp_info.offset_bits);
1910 Write 12-byte version 2 bitmap header.
1912 (void) WriteBlobLSBLong(image,bmp_info.size);
1913 (void) WriteBlobLSBShort(image,(unsigned short) bmp_info.width);
1914 (void) WriteBlobLSBShort(image,(unsigned short) bmp_info.height);
1915 (void) WriteBlobLSBShort(image,bmp_info.planes);
1916 (void) WriteBlobLSBShort(image,bmp_info.bits_per_pixel);
1921 Write 40-byte version 3+ bitmap header.
1923 (void) WriteBlobLSBLong(image,bmp_info.size);
1924 (void) WriteBlobLSBLong(image,(unsigned int) bmp_info.width);
1925 (void) WriteBlobLSBLong(image,(unsigned int) bmp_info.height);
1926 (void) WriteBlobLSBShort(image,bmp_info.planes);
1927 (void) WriteBlobLSBShort(image,bmp_info.bits_per_pixel);
1928 (void) WriteBlobLSBLong(image,bmp_info.compression);
1929 (void) WriteBlobLSBLong(image,bmp_info.image_size);
1930 (void) WriteBlobLSBLong(image,bmp_info.x_pixels);
1931 (void) WriteBlobLSBLong(image,bmp_info.y_pixels);
1932 (void) WriteBlobLSBLong(image,bmp_info.number_colors);
1933 (void) WriteBlobLSBLong(image,bmp_info.colors_important);
1935 if ((type > 3) && ((image->matte != MagickFalse) ||
1936 (have_color_info != MagickFalse)))
1939 Write the rest of the 108-byte BMP Version 4 header.
1941 (void) WriteBlobLSBLong(image,0x00ff0000U); /* Red mask */
1942 (void) WriteBlobLSBLong(image,0x0000ff00U); /* Green mask */
1943 (void) WriteBlobLSBLong(image,0x000000ffU); /* Blue mask */
1944 (void) WriteBlobLSBLong(image,0xff000000U); /* Alpha mask */
1945 (void) WriteBlobLSBLong(image,0x00000001U); /* CSType==Calib. RGB */
1946 (void) WriteBlobLSBLong(image,(unsigned int)
1947 image->chromaticity.red_primary.x*0x3ffffff);
1948 (void) WriteBlobLSBLong(image,(unsigned int)
1949 image->chromaticity.red_primary.y*0x3ffffff);
1950 (void) WriteBlobLSBLong(image,(unsigned int)
1951 (1.000f-(image->chromaticity.red_primary.x+
1952 image->chromaticity.red_primary.y)*0x3ffffff));
1953 (void) WriteBlobLSBLong(image,(unsigned int)
1954 image->chromaticity.green_primary.x*0x3ffffff);
1955 (void) WriteBlobLSBLong(image,(unsigned int)
1956 image->chromaticity.green_primary.y*0x3ffffff);
1957 (void) WriteBlobLSBLong(image,(unsigned int)
1958 (1.000f-(image->chromaticity.green_primary.x+
1959 image->chromaticity.green_primary.y)*0x3ffffff));
1960 (void) WriteBlobLSBLong(image,(unsigned int)
1961 image->chromaticity.blue_primary.x*0x3ffffff);
1962 (void) WriteBlobLSBLong(image,(unsigned int)
1963 image->chromaticity.blue_primary.y*0x3ffffff);
1964 (void) WriteBlobLSBLong(image,(unsigned int)
1965 (1.000f-(image->chromaticity.blue_primary.x+
1966 image->chromaticity.blue_primary.y)*0x3ffffff));
1967 (void) WriteBlobLSBLong(image,(unsigned int)
1968 bmp_info.gamma_scale.x*0xffff);
1969 (void) WriteBlobLSBLong(image,(unsigned int)
1970 bmp_info.gamma_scale.y*0xffff);
1971 (void) WriteBlobLSBLong(image,(unsigned int)
1972 bmp_info.gamma_scale.z*0xffff);
1973 if ((image->rendering_intent != UndefinedIntent) ||
1974 (profile != (StringInfo *) NULL))
1979 switch ((int) image->rendering_intent)
1981 case SaturationIntent:
1983 intent=LCS_GM_BUSINESS;
1986 case RelativeIntent:
1988 intent=LCS_GM_GRAPHICS;
1991 case PerceptualIntent:
1993 intent=LCS_GM_IMAGES;
1996 case AbsoluteIntent:
1998 intent=LCS_GM_ABS_COLORIMETRIC;
2007 (void) WriteBlobLSBLong(image,(unsigned int) intent);
2008 (void) WriteBlobLSBLong(image,0x00); /* dummy profile data */
2009 (void) WriteBlobLSBLong(image,0x00); /* dummy profile length */
2010 (void) WriteBlobLSBLong(image,0x00); /* reserved */
2013 if (image->storage_class == PseudoClass)
2019 Dump colormap to file.
2021 if (image->debug != MagickFalse)
2022 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
2023 " Colormap: %.20g entries",(double) image->colors);
2024 bmp_colormap=(unsigned char *) AcquireQuantumMemory((size_t) (1UL <<
2025 bmp_info.bits_per_pixel),4*sizeof(*bmp_colormap));
2026 if (bmp_colormap == (unsigned char *) NULL)
2027 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
2029 for (i=0; i < (ssize_t) MagickMin((ssize_t) image->colors,(ssize_t) bmp_info.number_colors); i++)
2031 *q++=ScaleQuantumToChar(image->colormap[i].blue);
2032 *q++=ScaleQuantumToChar(image->colormap[i].green);
2033 *q++=ScaleQuantumToChar(image->colormap[i].red);
2035 *q++=(unsigned char) 0x0;
2037 for ( ; i < (ssize_t) (1UL << bmp_info.bits_per_pixel); i++)
2039 *q++=(unsigned char) 0x00;
2040 *q++=(unsigned char) 0x00;
2041 *q++=(unsigned char) 0x00;
2043 *q++=(unsigned char) 0x00;
2046 (void) WriteBlob(image,(size_t) (3*(1L << bmp_info.bits_per_pixel)),
2049 (void) WriteBlob(image,(size_t) (4*(1L << bmp_info.bits_per_pixel)),
2051 bmp_colormap=(unsigned char *) RelinquishMagickMemory(bmp_colormap);
2053 if (image->debug != MagickFalse)
2054 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
2055 " Pixels: %u bytes",bmp_info.image_size);
2056 (void) WriteBlob(image,(size_t) bmp_info.image_size,pixels);
2057 pixels=(unsigned char *) RelinquishMagickMemory(pixels);
2058 if (GetNextImageInList(image) == (Image *) NULL)
2060 image=SyncNextImageInList(image);
2061 status=SetImageProgress(image,SaveImagesTag,scene++,
2062 GetImageListLength(image));
2063 if (status == MagickFalse)
2065 } while (image_info->adjoin != MagickFalse);
2066 (void) CloseBlob(image);