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,const size_t compression,
195 unsigned char *pixels)
204 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)
361 register const unsigned char
368 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)
513 register unsigned char
536 assert(image_info != (const ImageInfo *) NULL);
537 assert(image_info->signature == MagickSignature);
538 if (image_info->debug != MagickFalse)
539 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
540 image_info->filename);
541 assert(exception != (ExceptionInfo *) NULL);
542 assert(exception->signature == MagickSignature);
543 image=AcquireImage(image_info);
544 status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception);
545 if (status == MagickFalse)
547 image=DestroyImageList(image);
548 return((Image *) NULL);
551 Determine if this a BMP file.
553 (void) ResetMagickMemory(&bmp_info,0,sizeof(bmp_info));
554 bmp_info.ba_offset=0;
556 count=ReadBlob(image,2,magick);
570 Verify BMP identifier.
572 if (bmp_info.ba_offset == 0)
573 start_position=TellBlob(image)-2;
574 bmp_info.ba_offset=0;
575 while (LocaleNCompare((char *) magick,"BA",2) == 0)
577 bmp_info.file_size=ReadBlobLSBLong(image);
578 bmp_info.ba_offset=ReadBlobLSBLong(image);
579 bmp_info.offset_bits=ReadBlobLSBLong(image);
580 count=ReadBlob(image,2,magick);
584 if (image->debug != MagickFalse)
585 (void) LogMagickEvent(CoderEvent,GetMagickModule()," Magick: %c%c",
586 magick[0],magick[1]);
587 if ((count == 0) || ((LocaleNCompare((char *) magick,"BM",2) != 0) &&
588 (LocaleNCompare((char *) magick,"CI",2) != 0)))
589 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
590 bmp_info.file_size=ReadBlobLSBLong(image);
591 (void) ReadBlobLSBLong(image);
592 bmp_info.offset_bits=ReadBlobLSBLong(image);
593 bmp_info.size=ReadBlobLSBLong(image);
594 if (image->debug != MagickFalse)
595 (void) LogMagickEvent(CoderEvent,GetMagickModule()," BMP size: %u",
597 if (bmp_info.size == 12)
602 bmp_info.width=(short) ReadBlobLSBShort(image);
603 bmp_info.height=(short) ReadBlobLSBShort(image);
604 bmp_info.planes=ReadBlobLSBShort(image);
605 bmp_info.bits_per_pixel=ReadBlobLSBShort(image);
608 bmp_info.number_colors=0;
609 bmp_info.compression=BI_RGB;
610 bmp_info.image_size=0;
611 bmp_info.alpha_mask=0;
612 if (image->debug != MagickFalse)
614 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
615 " Format: OS/2 Bitmap");
616 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
617 " Geometry: %.20gx%.20g",(double) bmp_info.width,(double)
624 Microsoft Windows BMP image file.
626 if (bmp_info.size < 40)
627 ThrowReaderException(CorruptImageError,"NonOS2HeaderSizeError");
628 bmp_info.width=(int) ReadBlobLSBLong(image);
629 bmp_info.height=(int) ReadBlobLSBLong(image);
630 bmp_info.planes=ReadBlobLSBShort(image);
631 bmp_info.bits_per_pixel=ReadBlobLSBShort(image);
632 bmp_info.compression=ReadBlobLSBLong(image);
633 bmp_info.image_size=ReadBlobLSBLong(image);
634 bmp_info.x_pixels=ReadBlobLSBLong(image);
635 bmp_info.y_pixels=ReadBlobLSBLong(image);
636 bmp_info.number_colors=ReadBlobLSBLong(image);
637 bmp_info.colors_important=ReadBlobLSBLong(image);
640 if (image->debug != MagickFalse)
642 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
643 " Format: MS Windows bitmap");
644 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
645 " Geometry: %.20gx%.20g",(double) bmp_info.width,(double)
647 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
648 " Bits per pixel: %.20g",(double) bmp_info.bits_per_pixel);
649 switch ((int) bmp_info.compression)
653 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
654 " Compression: BI_RGB");
659 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
660 " Compression: BI_RLE4");
665 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
666 " Compression: BI_RLE8");
671 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
672 " Compression: BI_BITFIELDS");
677 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
678 " Compression: BI_PNG");
683 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
684 " Compression: BI_JPEG");
689 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
690 " Compression: UNKNOWN (%u)",bmp_info.compression);
693 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
694 " Number of colors: %u",bmp_info.number_colors);
696 bmp_info.red_mask=ReadBlobLSBLong(image);
697 bmp_info.green_mask=ReadBlobLSBLong(image);
698 bmp_info.blue_mask=ReadBlobLSBLong(image);
699 if (bmp_info.size > 40)
705 Read color management information.
707 bmp_info.alpha_mask=ReadBlobLSBLong(image);
708 bmp_info.colorspace=(int) ReadBlobLSBLong(image);
710 Decode 2^30 fixed point formatted CIE primaries.
712 bmp_info.red_primary.x=(double) ReadBlobLSBLong(image)/0x3ffffff;
713 bmp_info.red_primary.y=(double) ReadBlobLSBLong(image)/0x3ffffff;
714 bmp_info.red_primary.z=(double) ReadBlobLSBLong(image)/0x3ffffff;
715 bmp_info.green_primary.x=(double) ReadBlobLSBLong(image)/0x3ffffff;
716 bmp_info.green_primary.y=(double) ReadBlobLSBLong(image)/0x3ffffff;
717 bmp_info.green_primary.z=(double) ReadBlobLSBLong(image)/0x3ffffff;
718 bmp_info.blue_primary.x=(double) ReadBlobLSBLong(image)/0x3ffffff;
719 bmp_info.blue_primary.y=(double) ReadBlobLSBLong(image)/0x3ffffff;
720 bmp_info.blue_primary.z=(double) ReadBlobLSBLong(image)/0x3ffffff;
721 sum=bmp_info.red_primary.x+bmp_info.red_primary.y+
722 bmp_info.red_primary.z;
723 image->chromaticity.red_primary.x/=sum;
724 image->chromaticity.red_primary.y/=sum;
725 sum=bmp_info.green_primary.x+bmp_info.green_primary.y+
726 bmp_info.green_primary.z;
727 image->chromaticity.green_primary.x/=sum;
728 image->chromaticity.green_primary.y/=sum;
729 sum=bmp_info.blue_primary.x+bmp_info.blue_primary.y+
730 bmp_info.blue_primary.z;
731 image->chromaticity.blue_primary.x/=sum;
732 image->chromaticity.blue_primary.y/=sum;
734 Decode 16^16 fixed point formatted gamma_scales.
736 bmp_info.gamma_scale.x=(double) ReadBlobLSBLong(image)/0xffff;
737 bmp_info.gamma_scale.y=(double) ReadBlobLSBLong(image)/0xffff;
738 bmp_info.gamma_scale.z=(double) ReadBlobLSBLong(image)/0xffff;
740 Compute a single gamma from the BMP 3-channel gamma.
742 image->gamma=(bmp_info.gamma_scale.x+bmp_info.gamma_scale.y+
743 bmp_info.gamma_scale.z)/3.0;
745 if (bmp_info.size > 108)
751 Read BMP Version 5 color management information.
753 intent=ReadBlobLSBLong(image);
754 switch ((int) intent)
756 case LCS_GM_BUSINESS:
758 image->rendering_intent=SaturationIntent;
761 case LCS_GM_GRAPHICS:
763 image->rendering_intent=RelativeIntent;
768 image->rendering_intent=PerceptualIntent;
771 case LCS_GM_ABS_COLORIMETRIC:
773 image->rendering_intent=AbsoluteIntent;
777 profile_data=ReadBlobLSBLong(image);
778 profile_size=ReadBlobLSBLong(image);
781 (void) ReadBlobLSBLong(image); /* Reserved byte */
784 if ((MagickSizeType) bmp_info.file_size > GetBlobSize(image))
785 (void) ThrowMagickException(exception,GetMagickModule(),CorruptImageError,
786 "LengthAndFilesizeDoNotMatch","`%s'",image->filename);
788 if ((MagickSizeType) bmp_info.file_size < GetBlobSize(image))
789 (void) ThrowMagickException(exception,GetMagickModule(),
790 CorruptImageWarning,"LengthAndFilesizeDoNotMatch","`%s'",
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 SetIndexPixelComponent(indexes+x+bit,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 SetIndexPixelComponent(indexes+x+bit,index);
1027 if (SyncAuthenticPixels(image,exception) == MagickFalse)
1029 if (image->previous == (Image *) NULL)
1031 status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y,
1033 if (status == MagickFalse)
1037 (void) SyncImage(image);
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);
1055 SetIndexPixelComponent(indexes+x,index);
1056 index=ConstrainColormapIndex(image,*p & 0x0f);
1057 SetIndexPixelComponent(indexes+x+1,index);
1060 if ((image->columns % 2) != 0)
1062 index=ConstrainColormapIndex(image,(*p >> 4) & 0xf);
1063 SetIndexPixelComponent(indexes+x,index);
1066 if (SyncAuthenticPixels(image,exception) == MagickFalse)
1068 if (image->previous == (Image *) NULL)
1070 status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y,
1072 if (status == MagickFalse)
1076 (void) SyncImage(image);
1082 Convert PseudoColor scanline.
1084 if ((bmp_info.compression == BI_RLE8) ||
1085 (bmp_info.compression == BI_RLE4))
1086 bytes_per_line=image->columns;
1087 for (y=(ssize_t) image->rows-1; y >= 0; y--)
1089 p=pixels+(image->rows-y-1)*bytes_per_line;
1090 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
1091 if (q == (PixelPacket *) NULL)
1093 indexes=GetAuthenticIndexQueue(image);
1094 for (x = (ssize_t)image->columns; x != 0; --x)
1096 index=ConstrainColormapIndex(image,*p);
1097 SetIndexPixelComponent(indexes,index);
1102 if (SyncAuthenticPixels(image,exception) == MagickFalse)
1104 offset=(MagickOffsetType) (image->rows-y-1);
1105 if (image->previous == (Image *) NULL)
1107 status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y,
1109 if (status == MagickFalse)
1113 (void) SyncImage(image);
1122 Convert bitfield encoded 16-bit PseudoColor scanline.
1124 if (bmp_info.compression != BI_RGB &&
1125 bmp_info.compression != BI_BITFIELDS)
1127 pixels=(unsigned char *) RelinquishMagickMemory(pixels);
1128 ThrowReaderException(CorruptImageError,
1129 "UnrecognizedImageCompression");
1131 bytes_per_line=2*(image->columns+image->columns % 2);
1132 image->storage_class=DirectClass;
1133 for (y=(ssize_t) image->rows-1; y >= 0; y--)
1135 p=pixels+(image->rows-y-1)*bytes_per_line;
1136 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
1137 if (q == (PixelPacket *) NULL)
1139 for (x=0; x < (ssize_t) image->columns; x++)
1141 pixel=(size_t) (*p++);
1143 red=((pixel & bmp_info.red_mask) << shift.red) >> 16;
1144 if (quantum_bits.red == 5)
1145 red|=((red & 0xe000) >> 5);
1146 if (quantum_bits.red <= 8)
1147 red|=((red & 0xff00) >> 8);
1148 green=((pixel & bmp_info.green_mask) << shift.green) >> 16;
1149 if (quantum_bits.green == 5)
1150 green|=((green & 0xe000) >> 5);
1151 if (quantum_bits.green == 6)
1152 green|=((green & 0xc000) >> 6);
1153 if (quantum_bits.green <= 8)
1154 green|=((green & 0xff00) >> 8);
1155 blue=((pixel & bmp_info.blue_mask) << shift.blue) >> 16;
1156 if (quantum_bits.blue == 5)
1157 blue|=((blue & 0xe000) >> 5);
1158 if (quantum_bits.blue <= 8)
1159 blue|=((blue & 0xff00) >> 8);
1160 opacity=((pixel & bmp_info.alpha_mask) << shift.opacity) >> 16;
1161 if (quantum_bits.opacity <= 8)
1162 opacity|=((opacity & 0xff00) >> 8);
1163 SetRedPixelComponent(q,ScaleShortToQuantum((unsigned short) red));
1164 SetGreenPixelComponent(q,ScaleShortToQuantum((unsigned short)
1166 SetBluePixelComponent(q,ScaleShortToQuantum((unsigned short) blue));
1167 SetOpacityPixelComponent(q,OpaqueOpacity);
1168 if (image->matte != MagickFalse)
1169 SetAlphaPixelComponent(q,ScaleShortToQuantum((unsigned short)
1173 if (SyncAuthenticPixels(image,exception) == MagickFalse)
1175 offset=(MagickOffsetType) (image->rows-y-1);
1176 if (image->previous == (Image *) NULL)
1178 status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y,
1180 if (status == MagickFalse)
1189 Convert DirectColor scanline.
1191 bytes_per_line=4*((image->columns*24+31)/32);
1192 for (y=(ssize_t) image->rows-1; y >= 0; y--)
1194 p=pixels+(image->rows-y-1)*bytes_per_line;
1195 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
1196 if (q == (PixelPacket *) NULL)
1198 for (x=0; x < (ssize_t) image->columns; x++)
1200 SetBluePixelComponent(q,ScaleCharToQuantum(*p++));
1201 SetGreenPixelComponent(q,ScaleCharToQuantum(*p++));
1202 SetRedPixelComponent(q,ScaleCharToQuantum(*p++));
1203 SetOpacityPixelComponent(q,OpaqueOpacity);
1206 if (SyncAuthenticPixels(image,exception) == MagickFalse)
1208 offset=(MagickOffsetType) (image->rows-y-1);
1209 if (image->previous == (Image *) NULL)
1211 status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y,
1213 if (status == MagickFalse)
1222 Convert bitfield encoded DirectColor scanline.
1224 if ((bmp_info.compression != BI_RGB) &&
1225 (bmp_info.compression != BI_BITFIELDS))
1227 pixels=(unsigned char *) RelinquishMagickMemory(pixels);
1228 ThrowReaderException(CorruptImageError,
1229 "UnrecognizedImageCompression");
1231 bytes_per_line=4*(image->columns);
1232 for (y=(ssize_t) image->rows-1; y >= 0; y--)
1237 p=pixels+(image->rows-y-1)*bytes_per_line;
1238 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
1239 if (q == (PixelPacket *) NULL)
1241 for (x=0; x < (ssize_t) image->columns; x++)
1243 pixel=(size_t) (*p++);
1245 pixel|=(*p++ << 16);
1246 pixel|=(*p++ << 24);
1247 red=((pixel & bmp_info.red_mask) << shift.red) >> 16;
1248 if (quantum_bits.red == 8)
1250 green=((pixel & bmp_info.green_mask) << shift.green) >> 16;
1251 if (quantum_bits.green == 8)
1252 green|=(green >> 8);
1253 blue=((pixel & bmp_info.blue_mask) << shift.blue) >> 16;
1254 if (quantum_bits.blue == 8)
1256 opacity=((pixel & bmp_info.alpha_mask) << shift.opacity) >> 16;
1257 if (quantum_bits.opacity == 8)
1258 opacity|=(opacity >> 8);
1259 SetRedPixelComponent(q,ScaleShortToQuantum((unsigned short) red));
1260 SetGreenPixelComponent(q,ScaleShortToQuantum((unsigned short)
1262 SetBluePixelComponent(q,ScaleShortToQuantum((unsigned short) blue));
1263 SetOpacityPixelComponent(q,OpaqueOpacity);
1264 if (image->matte != MagickFalse)
1265 SetAlphaPixelComponent(q,ScaleShortToQuantum((unsigned short)
1269 if (SyncAuthenticPixels(image,exception) == MagickFalse)
1271 offset=(MagickOffsetType) (image->rows-y-1);
1272 if (image->previous == (Image *) NULL)
1274 status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y,
1276 if (status == MagickFalse)
1284 pixels=(unsigned char *) RelinquishMagickMemory(pixels);
1285 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
1288 pixels=(unsigned char *) RelinquishMagickMemory(pixels);
1289 if (EOFBlob(image) != MagickFalse)
1291 ThrowFileException(exception,CorruptImageError,"UnexpectedEndOfFile",
1295 if (bmp_info.height < 0)
1301 Correct image orientation.
1303 flipped_image=FlipImage(image,exception);
1304 if (flipped_image != (Image *) NULL)
1306 DuplicateBlob(flipped_image,image);
1307 image=DestroyImage(image);
1308 image=flipped_image;
1312 Proceed to next image.
1314 if (image_info->number_scenes != 0)
1315 if (image->scene >= (image_info->scene+image_info->number_scenes-1))
1318 if (bmp_info.ba_offset != 0)
1320 offset=SeekBlob(image,(MagickOffsetType) bmp_info.ba_offset,SEEK_SET);
1322 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
1324 count=ReadBlob(image,2,magick);
1325 if ((count == 2) && (IsBMP(magick,2) != MagickFalse))
1328 Acquire next image structure.
1330 AcquireNextImage(image_info,image);
1331 if (GetNextImageInList(image) == (Image *) NULL)
1333 image=DestroyImageList(image);
1334 return((Image *) NULL);
1336 image=SyncNextImageInList(image);
1337 status=SetImageProgress(image,LoadImagesTag,TellBlob(image),
1338 GetBlobSize(image));
1339 if (status == MagickFalse)
1342 } while (IsBMP(magick,2) != MagickFalse);
1343 (void) CloseBlob(image);
1344 return(GetFirstImageInList(image));
1348 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1352 % R e g i s t e r B M P I m a g e %
1356 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1358 % RegisterBMPImage() adds attributes for the BMP image format to
1359 % the list of supported formats. The attributes include the image format
1360 % tag, a method to read and/or write the format, whether the format
1361 % supports the saving of more than one frame to the same file or blob,
1362 % whether the format supports native in-memory I/O, and a brief
1363 % description of the format.
1365 % The format of the RegisterBMPImage method is:
1367 % size_t RegisterBMPImage(void)
1370 ModuleExport size_t RegisterBMPImage(void)
1375 entry=SetMagickInfo("BMP");
1376 entry->decoder=(DecodeImageHandler *) ReadBMPImage;
1377 entry->encoder=(EncodeImageHandler *) WriteBMPImage;
1378 entry->magick=(IsImageFormatHandler *) IsBMP;
1379 entry->description=ConstantString("Microsoft Windows bitmap image");
1380 entry->module=ConstantString("BMP");
1381 entry->adjoin=MagickFalse;
1382 entry->seekable_stream=MagickTrue;
1383 (void) RegisterMagickInfo(entry);
1384 entry=SetMagickInfo("BMP2");
1385 entry->encoder=(EncodeImageHandler *) WriteBMPImage;
1386 entry->magick=(IsImageFormatHandler *) IsBMP;
1387 entry->description=ConstantString("Microsoft Windows bitmap image (V2)");
1388 entry->module=ConstantString("BMP");
1389 entry->adjoin=MagickFalse;
1390 entry->seekable_stream=MagickTrue;
1391 (void) RegisterMagickInfo(entry);
1392 entry=SetMagickInfo("BMP3");
1393 entry->encoder=(EncodeImageHandler *) WriteBMPImage;
1394 entry->magick=(IsImageFormatHandler *) IsBMP;
1395 entry->description=ConstantString("Microsoft Windows bitmap image (V3)");
1396 entry->module=ConstantString("BMP");
1397 entry->adjoin=MagickFalse;
1398 entry->seekable_stream=MagickTrue;
1399 (void) RegisterMagickInfo(entry);
1400 return(MagickImageCoderSignature);
1404 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1408 % U n r e g i s t e r B M P I m a g e %
1412 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1414 % UnregisterBMPImage() removes format registrations made by the
1415 % BMP module from the list of supported formats.
1417 % The format of the UnregisterBMPImage method is:
1419 % UnregisterBMPImage(void)
1422 ModuleExport void UnregisterBMPImage(void)
1424 (void) UnregisterMagickInfo("BMP");
1425 (void) UnregisterMagickInfo("BMP2");
1426 (void) UnregisterMagickInfo("BMP3");
1430 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1434 % W r i t e B M P I m a g e %
1438 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1440 % WriteBMPImage() writes an image in Microsoft Windows bitmap encoded
1441 % image format, version 3 for Windows or (if the image has a matte channel)
1444 % The format of the WriteBMPImage method is:
1446 % MagickBooleanType WriteBMPImage(const ImageInfo *image_info,Image *image)
1448 % A description of each parameter follows.
1450 % o image_info: the image info.
1452 % o image: The image.
1455 static MagickBooleanType WriteBMPImage(const ImageInfo *image_info,Image *image)
1470 register const IndexPacket
1473 register const PixelPacket
1480 register unsigned char
1495 Open output image file.
1497 assert(image_info != (const ImageInfo *) NULL);
1498 assert(image_info->signature == MagickSignature);
1499 assert(image != (Image *) NULL);
1500 assert(image->signature == MagickSignature);
1501 if (image->debug != MagickFalse)
1502 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1503 status=OpenBlob(image_info,image,WriteBinaryBlobMode,&image->exception);
1504 if (status == MagickFalse)
1507 if (LocaleCompare(image_info->magick,"BMP2") == 0)
1510 if (LocaleCompare(image_info->magick,"BMP3") == 0)
1516 Initialize BMP raster file header.
1518 if (image->colorspace != RGBColorspace)
1519 (void) TransformImageColorspace(image,RGBColorspace);
1520 (void) ResetMagickMemory(&bmp_info,0,sizeof(bmp_info));
1521 bmp_info.file_size=14+12;
1523 bmp_info.file_size+=28;
1524 bmp_info.offset_bits=bmp_info.file_size;
1525 bmp_info.compression=BI_RGB;
1526 if ((image->storage_class == PseudoClass) && (image->colors > 256))
1527 (void) SetImageStorageClass(image,DirectClass);
1528 if (image->storage_class != DirectClass)
1531 Colormapped BMP raster.
1533 bmp_info.bits_per_pixel=8;
1534 if (image->colors <= 2)
1535 bmp_info.bits_per_pixel=1;
1537 if (image->colors <= 16)
1538 bmp_info.bits_per_pixel=4;
1540 if (image->colors <= 256)
1541 bmp_info.bits_per_pixel=8;
1542 if (image_info->compression == RLECompression)
1543 bmp_info.bits_per_pixel=8;
1544 bmp_info.number_colors=1U << bmp_info.bits_per_pixel;
1545 if (image->matte != MagickFalse)
1546 (void) SetImageStorageClass(image,DirectClass);
1548 if ((size_t) bmp_info.number_colors < image->colors)
1549 (void) SetImageStorageClass(image,DirectClass);
1552 bmp_info.file_size+=3*(1UL << bmp_info.bits_per_pixel);
1553 bmp_info.offset_bits+=3*(1UL << bmp_info.bits_per_pixel);
1556 bmp_info.file_size+=(1UL << bmp_info.bits_per_pixel);
1557 bmp_info.offset_bits+=(1UL << bmp_info.bits_per_pixel);
1561 if (image->storage_class == DirectClass)
1564 Full color BMP raster.
1566 bmp_info.number_colors=0;
1567 bmp_info.bits_per_pixel=(unsigned short)
1568 ((type > 3) && (image->matte != MagickFalse) ? 32 : 24);
1569 bmp_info.compression=(unsigned int) ((type > 3) &&
1570 (image->matte != MagickFalse) ? BI_BITFIELDS : BI_RGB);
1572 bytes_per_line=4*((image->columns*bmp_info.bits_per_pixel+31)/32);
1573 bmp_info.ba_offset=0;
1574 profile=GetImageProfile(image,"icc");
1575 have_color_info=(image->rendering_intent != UndefinedIntent) ||
1576 (profile != (StringInfo *) NULL) || (image->gamma != 0.0) ? MagickTrue :
1581 if ((type == 3) || ((image->matte == MagickFalse) &&
1582 (have_color_info == MagickFalse)))
1594 if ((image->rendering_intent != UndefinedIntent) ||
1595 (profile != (StringInfo *) NULL))
1600 bmp_info.file_size+=extra_size;
1601 bmp_info.offset_bits+=extra_size;
1603 bmp_info.width=(ssize_t) image->columns;
1604 bmp_info.height=(ssize_t) image->rows;
1606 bmp_info.image_size=(unsigned int) (bytes_per_line*image->rows);
1607 bmp_info.file_size+=bmp_info.image_size;
1608 bmp_info.x_pixels=75*39;
1609 bmp_info.y_pixels=75*39;
1610 switch (image->units)
1612 case UndefinedResolution:
1613 case PixelsPerInchResolution:
1615 bmp_info.x_pixels=(unsigned int) (100.0*image->x_resolution/2.54);
1616 bmp_info.y_pixels=(unsigned int) (100.0*image->y_resolution/2.54);
1619 case PixelsPerCentimeterResolution:
1621 bmp_info.x_pixels=(unsigned int) (100.0*image->x_resolution);
1622 bmp_info.y_pixels=(unsigned int) (100.0*image->y_resolution);
1626 bmp_info.colors_important=bmp_info.number_colors;
1628 Convert MIFF to BMP raster pixels.
1630 pixels=(unsigned char *) AcquireQuantumMemory((size_t) bmp_info.image_size,
1632 if (pixels == (unsigned char *) NULL)
1633 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
1634 (void) ResetMagickMemory(pixels,0,(size_t) bmp_info.image_size);
1635 switch (bmp_info.bits_per_pixel)
1644 Convert PseudoClass image to a BMP monochrome image.
1646 for (y=0; y < (ssize_t) image->rows; y++)
1648 p=GetVirtualPixels(image,0,y,image->columns,1,&image->exception);
1649 if (p == (const PixelPacket *) NULL)
1651 indexes=GetVirtualIndexQueue(image);
1652 q=pixels+(image->rows-y-1)*bytes_per_line;
1655 for (x=0; x < (ssize_t) image->columns; x++)
1658 byte|=GetIndexPixelComponent(indexes+x) != 0 ? 0x01 : 0x00;
1662 *q++=(unsigned char) byte;
1669 *q++=(unsigned char) (byte << (8-bit));
1672 for (x=(ssize_t) (image->columns+7)/8; x < (ssize_t) bytes_per_line; x++)
1674 if (image->previous == (Image *) NULL)
1676 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
1678 if (status == MagickFalse)
1691 Convert PseudoClass image to a BMP monochrome image.
1693 for (y=0; y < (ssize_t) image->rows; y++)
1695 p=GetVirtualPixels(image,0,y,image->columns,1,&image->exception);
1696 if (p == (const PixelPacket *) NULL)
1698 indexes=GetVirtualIndexQueue(image);
1699 q=pixels+(image->rows-y-1)*bytes_per_line;
1702 for (x=0; x < (ssize_t) image->columns; x++)
1705 byte|=((size_t) GetIndexPixelComponent(indexes+x) & 0x0f);
1709 *q++=(unsigned char) byte;
1716 *q++=(unsigned char) (byte << 4);
1719 for (x=(ssize_t) (image->columns+1)/2; x < (ssize_t) bytes_per_line; x++)
1721 if (image->previous == (Image *) NULL)
1723 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
1725 if (status == MagickFalse)
1734 Convert PseudoClass packet to BMP pixel.
1736 for (y=0; y < (ssize_t) image->rows; y++)
1738 p=GetVirtualPixels(image,0,y,image->columns,1,&image->exception);
1739 if (p == (const PixelPacket *) NULL)
1741 indexes=GetVirtualIndexQueue(image);
1742 q=pixels+(image->rows-y-1)*bytes_per_line;
1743 for (x=0; x < (ssize_t) image->columns; x++)
1744 *q++=(unsigned char) GetIndexPixelComponent(indexes+x);
1745 for ( ; x < (ssize_t) bytes_per_line; x++)
1747 if (image->previous == (Image *) NULL)
1749 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
1751 if (status == MagickFalse)
1760 Convert DirectClass packet to BMP BGR888.
1762 for (y=0; y < (ssize_t) image->rows; y++)
1764 p=GetVirtualPixels(image,0,y,image->columns,1,&image->exception);
1765 if (p == (const PixelPacket *) NULL)
1767 q=pixels+(image->rows-y-1)*bytes_per_line;
1768 for (x=0; x < (ssize_t) image->columns; x++)
1770 *q++=ScaleQuantumToChar(GetBluePixelComponent(p));
1771 *q++=ScaleQuantumToChar(GetGreenPixelComponent(p));
1772 *q++=ScaleQuantumToChar(GetRedPixelComponent(p));
1775 for (x=3L*(ssize_t) image->columns; x < (ssize_t) bytes_per_line; x++)
1777 if (image->previous == (Image *) NULL)
1779 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
1781 if (status == MagickFalse)
1790 Convert DirectClass packet to ARGB8888 pixel.
1792 for (y=0; y < (ssize_t) image->rows; y++)
1794 p=GetVirtualPixels(image,0,y,image->columns,1,&image->exception);
1795 if (p == (const PixelPacket *) NULL)
1797 q=pixels+(image->rows-y-1)*bytes_per_line;
1798 for (x=0; x < (ssize_t) image->columns; x++)
1800 *q++=ScaleQuantumToChar(GetBluePixelComponent(p));
1801 *q++=ScaleQuantumToChar(GetGreenPixelComponent(p));
1802 *q++=ScaleQuantumToChar(GetRedPixelComponent(p));
1803 *q++=ScaleQuantumToChar(QuantumRange-GetOpacityPixelComponent(p));
1806 if (image->previous == (Image *) NULL)
1808 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
1810 if (status == MagickFalse)
1817 if ((type > 2) && (bmp_info.bits_per_pixel == 8))
1818 if (image_info->compression != NoCompression)
1824 Convert run-length encoded raster pixels.
1826 length=(size_t) (2*(bytes_per_line+2)*(image->rows+2)+2);
1827 bmp_data=(unsigned char *) NULL;
1828 if (~length >= bytes_per_line)
1829 bmp_data=(unsigned char *) AcquireQuantumMemory(length+
1830 bytes_per_line,sizeof(*bmp_data));
1831 if (bmp_data == (unsigned char *) NULL)
1833 pixels=(unsigned char *) RelinquishMagickMemory(pixels);
1834 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
1836 bmp_info.file_size-=bmp_info.image_size;
1837 bmp_info.image_size=(unsigned int) EncodeImage(image,bytes_per_line,
1839 bmp_info.file_size+=bmp_info.image_size;
1840 pixels=(unsigned char *) RelinquishMagickMemory(pixels);
1842 bmp_info.compression=BI_RLE8;
1845 Write BMP for Windows, all versions, 14-byte header.
1847 if (image->debug != MagickFalse)
1849 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1850 " Writing BMP version %.20g datastream",(double) type);
1851 if (image->storage_class == DirectClass)
1852 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1853 " Storage class=DirectClass");
1855 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1856 " Storage class=PseudoClass");
1857 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1858 " Image depth=%.20g",(double) image->depth);
1859 if (image->matte != MagickFalse)
1860 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1863 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1864 " Matte=MagickFalse");
1865 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1866 " BMP bits_per_pixel=%.20g",(double) bmp_info.bits_per_pixel);
1867 switch ((int) bmp_info.compression)
1871 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1872 " Compression=BI_RGB");
1877 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1878 " Compression=BI_RLE8");
1883 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1884 " Compression=BI_BITFIELDS");
1889 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1890 " Compression=UNKNOWN (%u)",bmp_info.compression);
1894 if (bmp_info.number_colors == 0)
1895 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1896 " Number_colors=unspecified");
1898 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1899 " Number_colors=%u",bmp_info.number_colors);
1901 (void) WriteBlob(image,2,(unsigned char *) "BM");
1902 (void) WriteBlobLSBLong(image,bmp_info.file_size);
1903 (void) WriteBlobLSBLong(image,bmp_info.ba_offset); /* always 0 */
1904 (void) WriteBlobLSBLong(image,bmp_info.offset_bits);
1908 Write 12-byte version 2 bitmap header.
1910 (void) WriteBlobLSBLong(image,bmp_info.size);
1911 (void) WriteBlobLSBShort(image,(unsigned short) bmp_info.width);
1912 (void) WriteBlobLSBShort(image,(unsigned short) bmp_info.height);
1913 (void) WriteBlobLSBShort(image,bmp_info.planes);
1914 (void) WriteBlobLSBShort(image,bmp_info.bits_per_pixel);
1919 Write 40-byte version 3+ bitmap header.
1921 (void) WriteBlobLSBLong(image,bmp_info.size);
1922 (void) WriteBlobLSBLong(image,(unsigned int) bmp_info.width);
1923 (void) WriteBlobLSBLong(image,(unsigned int) bmp_info.height);
1924 (void) WriteBlobLSBShort(image,bmp_info.planes);
1925 (void) WriteBlobLSBShort(image,bmp_info.bits_per_pixel);
1926 (void) WriteBlobLSBLong(image,bmp_info.compression);
1927 (void) WriteBlobLSBLong(image,bmp_info.image_size);
1928 (void) WriteBlobLSBLong(image,bmp_info.x_pixels);
1929 (void) WriteBlobLSBLong(image,bmp_info.y_pixels);
1930 (void) WriteBlobLSBLong(image,bmp_info.number_colors);
1931 (void) WriteBlobLSBLong(image,bmp_info.colors_important);
1933 if ((type > 3) && ((image->matte != MagickFalse) ||
1934 (have_color_info != MagickFalse)))
1937 Write the rest of the 108-byte BMP Version 4 header.
1939 (void) WriteBlobLSBLong(image,0x00ff0000U); /* Red mask */
1940 (void) WriteBlobLSBLong(image,0x0000ff00U); /* Green mask */
1941 (void) WriteBlobLSBLong(image,0x000000ffU); /* Blue mask */
1942 (void) WriteBlobLSBLong(image,0xff000000U); /* Alpha mask */
1943 (void) WriteBlobLSBLong(image,0x00000001U); /* CSType==Calib. RGB */
1944 (void) WriteBlobLSBLong(image,(unsigned int)
1945 image->chromaticity.red_primary.x*0x3ffffff);
1946 (void) WriteBlobLSBLong(image,(unsigned int)
1947 image->chromaticity.red_primary.y*0x3ffffff);
1948 (void) WriteBlobLSBLong(image,(unsigned int)
1949 (1.000f-(image->chromaticity.red_primary.x+
1950 image->chromaticity.red_primary.y)*0x3ffffff));
1951 (void) WriteBlobLSBLong(image,(unsigned int)
1952 image->chromaticity.green_primary.x*0x3ffffff);
1953 (void) WriteBlobLSBLong(image,(unsigned int)
1954 image->chromaticity.green_primary.y*0x3ffffff);
1955 (void) WriteBlobLSBLong(image,(unsigned int)
1956 (1.000f-(image->chromaticity.green_primary.x+
1957 image->chromaticity.green_primary.y)*0x3ffffff));
1958 (void) WriteBlobLSBLong(image,(unsigned int)
1959 image->chromaticity.blue_primary.x*0x3ffffff);
1960 (void) WriteBlobLSBLong(image,(unsigned int)
1961 image->chromaticity.blue_primary.y*0x3ffffff);
1962 (void) WriteBlobLSBLong(image,(unsigned int)
1963 (1.000f-(image->chromaticity.blue_primary.x+
1964 image->chromaticity.blue_primary.y)*0x3ffffff));
1965 (void) WriteBlobLSBLong(image,(unsigned int)
1966 bmp_info.gamma_scale.x*0xffff);
1967 (void) WriteBlobLSBLong(image,(unsigned int)
1968 bmp_info.gamma_scale.y*0xffff);
1969 (void) WriteBlobLSBLong(image,(unsigned int)
1970 bmp_info.gamma_scale.z*0xffff);
1971 if ((image->rendering_intent != UndefinedIntent) ||
1972 (profile != (StringInfo *) NULL))
1977 switch ((int) image->rendering_intent)
1979 case SaturationIntent:
1981 intent=LCS_GM_BUSINESS;
1984 case RelativeIntent:
1986 intent=LCS_GM_GRAPHICS;
1989 case PerceptualIntent:
1991 intent=LCS_GM_IMAGES;
1994 case AbsoluteIntent:
1996 intent=LCS_GM_ABS_COLORIMETRIC;
2005 (void) WriteBlobLSBLong(image,(unsigned int) intent);
2006 (void) WriteBlobLSBLong(image,0x00); /* dummy profile data */
2007 (void) WriteBlobLSBLong(image,0x00); /* dummy profile length */
2008 (void) WriteBlobLSBLong(image,0x00); /* reserved */
2011 if (image->storage_class == PseudoClass)
2017 Dump colormap to file.
2019 if (image->debug != MagickFalse)
2020 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
2021 " Colormap: %.20g entries",(double) image->colors);
2022 bmp_colormap=(unsigned char *) AcquireQuantumMemory((size_t) (1UL <<
2023 bmp_info.bits_per_pixel),4*sizeof(*bmp_colormap));
2024 if (bmp_colormap == (unsigned char *) NULL)
2025 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
2027 for (i=0; i < (ssize_t) MagickMin((ssize_t) image->colors,(ssize_t) bmp_info.number_colors); i++)
2029 *q++=ScaleQuantumToChar(image->colormap[i].blue);
2030 *q++=ScaleQuantumToChar(image->colormap[i].green);
2031 *q++=ScaleQuantumToChar(image->colormap[i].red);
2033 *q++=(unsigned char) 0x0;
2035 for ( ; i < (ssize_t) (1UL << bmp_info.bits_per_pixel); i++)
2037 *q++=(unsigned char) 0x00;
2038 *q++=(unsigned char) 0x00;
2039 *q++=(unsigned char) 0x00;
2041 *q++=(unsigned char) 0x00;
2044 (void) WriteBlob(image,(size_t) (3*(1L << bmp_info.bits_per_pixel)),
2047 (void) WriteBlob(image,(size_t) (4*(1L << bmp_info.bits_per_pixel)),
2049 bmp_colormap=(unsigned char *) RelinquishMagickMemory(bmp_colormap);
2051 if (image->debug != MagickFalse)
2052 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
2053 " Pixels: %u bytes",bmp_info.image_size);
2054 (void) WriteBlob(image,(size_t) bmp_info.image_size,pixels);
2055 pixels=(unsigned char *) RelinquishMagickMemory(pixels);
2056 if (GetNextImageInList(image) == (Image *) NULL)
2058 image=SyncNextImageInList(image);
2059 status=SetImageProgress(image,SaveImagesTag,scene++,
2060 GetImageListLength(image));
2061 if (status == MagickFalse)
2063 } while (image_info->adjoin != MagickFalse);
2064 (void) CloseBlob(image);