2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
13 % Read/Write Microsoft Windows Bitmap Image Format %
17 % Glenn Randers-Pehrson %
21 % Copyright 1999-2010 ImageMagick Studio LLC, a non-profit organization %
22 % dedicated to making software imaging solutions freely available. %
24 % You may not use this file except in compliance with the License. You may %
25 % obtain a copy of the License at %
27 % http://www.imagemagick.org/script/license.php %
29 % Unless required by applicable law or agreed to in writing, software %
30 % distributed under the License is distributed on an "AS IS" BASIS, %
31 % WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. %
32 % See the License for the specific language governing permissions and %
33 % limitations under the License. %
35 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
43 #include "magick/studio.h"
44 #include "magick/blob.h"
45 #include "magick/blob-private.h"
46 #include "magick/cache.h"
47 #include "magick/colormap-private.h"
48 #include "magick/color-private.h"
49 #include "magick/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 unsigned long 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 long MagickAbsoluteValue(const long x)
180 static inline size_t MagickMax(const size_t x,const size_t y)
187 static inline long MagickMin(const long x,const long y)
194 static MagickBooleanType DecodeImage(Image *image,
195 const unsigned long 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 < (long) 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 unsigned long 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 unsigned long 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 < (long) image->rows; y++)
388 for (x=0; x < (long) bytes_per_line; x+=i)
393 for (i=1; ((x+i) < (long) 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,y,image->rows);
406 if (status == MagickFalse)
412 *q++=(unsigned char) 0x00;
413 *q++=(unsigned char) 0x01;
414 return((size_t) (q-compressed_pixels));
418 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
426 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
428 % IsBMP() returns MagickTrue if the image format type, identified by the
429 % magick string, is BMP.
431 % The format of the IsBMP method is:
433 % MagickBooleanType IsBMP(const unsigned char *magick,const size_t length)
435 % A description of each parameter follows:
437 % o magick: compare image format pattern against these bytes.
439 % o length: Specifies the length of the magick string.
442 static MagickBooleanType IsBMP(const unsigned char *magick,const size_t length)
446 if ((LocaleNCompare((char *) magick,"BA",2) == 0) ||
447 (LocaleNCompare((char *) magick,"BM",2) == 0) ||
448 (LocaleNCompare((char *) magick,"IC",2) == 0) ||
449 (LocaleNCompare((char *) magick,"PI",2) == 0) ||
450 (LocaleNCompare((char *) magick,"CI",2) == 0) ||
451 (LocaleNCompare((char *) magick,"CP",2) == 0))
457 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
461 % R e a d B M P I m a g e %
465 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
467 % ReadBMPImage() reads a Microsoft Windows bitmap image file, Version
468 % 2, 3 (for Windows or NT), or 4, and returns it. It allocates the memory
469 % necessary for the new Image structure and returns a pointer to the new
472 % The format of the ReadBMPImage method is:
474 % image=ReadBMPImage(image_info)
476 % A description of each parameter follows:
478 % o image_info: the image info.
480 % o exception: return any errors or warnings in this structure.
484 static Image *ReadBMPImage(const ImageInfo *image_info,ExceptionInfo *exception)
517 register unsigned char
541 assert(image_info != (const ImageInfo *) NULL);
542 assert(image_info->signature == MagickSignature);
543 if (image_info->debug != MagickFalse)
544 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
545 image_info->filename);
546 assert(exception != (ExceptionInfo *) NULL);
547 assert(exception->signature == MagickSignature);
548 image=AcquireImage(image_info);
549 status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception);
550 if (status == MagickFalse)
552 image=DestroyImageList(image);
553 return((Image *) NULL);
556 Determine if this a BMP file.
558 (void) ResetMagickMemory(&bmp_info,0,sizeof(bmp_info));
559 bmp_info.ba_offset=0;
561 count=ReadBlob(image,2,magick);
575 Verify BMP identifier.
577 if (bmp_info.ba_offset == 0)
578 start_position=TellBlob(image)-2;
579 bmp_info.ba_offset=0;
580 while (LocaleNCompare((char *) magick,"BA",2) == 0)
582 bmp_info.file_size=ReadBlobLSBLong(image);
583 bmp_info.ba_offset=ReadBlobLSBLong(image);
584 bmp_info.offset_bits=ReadBlobLSBLong(image);
585 count=ReadBlob(image,2,magick);
589 if (image->debug != MagickFalse)
590 (void) LogMagickEvent(CoderEvent,GetMagickModule()," Magick: %c%c",
591 magick[0],magick[1]);
592 if ((count == 0) || ((LocaleNCompare((char *) magick,"BM",2) != 0) &&
593 (LocaleNCompare((char *) magick,"CI",2) != 0)))
594 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
595 bmp_info.file_size=ReadBlobLSBLong(image);
596 (void) ReadBlobLSBLong(image);
597 bmp_info.offset_bits=ReadBlobLSBLong(image);
598 bmp_info.size=ReadBlobLSBLong(image);
599 if (image->debug != MagickFalse)
600 (void) LogMagickEvent(CoderEvent,GetMagickModule()," BMP size: %u",
602 if (bmp_info.size == 12)
607 bmp_info.width=(short) ReadBlobLSBShort(image);
608 bmp_info.height=(short) ReadBlobLSBShort(image);
609 bmp_info.planes=ReadBlobLSBShort(image);
610 bmp_info.bits_per_pixel=ReadBlobLSBShort(image);
613 bmp_info.number_colors=0;
614 bmp_info.compression=BI_RGB;
615 bmp_info.image_size=0;
616 bmp_info.alpha_mask=0;
617 if (image->debug != MagickFalse)
619 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
620 " Format: OS/2 Bitmap");
621 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
622 " Geometry: %ldx%ld",bmp_info.width,bmp_info.height);
628 Microsoft Windows BMP image file.
630 if (bmp_info.size < 40)
631 ThrowReaderException(CorruptImageError,"NonOS2HeaderSizeError");
632 bmp_info.width=(int) ReadBlobLSBLong(image);
633 bmp_info.height=(int) ReadBlobLSBLong(image);
634 bmp_info.planes=ReadBlobLSBShort(image);
635 bmp_info.bits_per_pixel=ReadBlobLSBShort(image);
636 bmp_info.compression=ReadBlobLSBLong(image);
637 bmp_info.image_size=ReadBlobLSBLong(image);
638 bmp_info.x_pixels=ReadBlobLSBLong(image);
639 bmp_info.y_pixels=ReadBlobLSBLong(image);
640 bmp_info.number_colors=ReadBlobLSBLong(image);
641 bmp_info.colors_important=ReadBlobLSBLong(image);
644 if (image->debug != MagickFalse)
646 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
647 " Format: MS Windows bitmap");
648 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
649 " Geometry: %ldx%ld",bmp_info.width,bmp_info.height);
650 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
651 " Bits per pixel: %d",bmp_info.bits_per_pixel);
652 switch ((int) bmp_info.compression)
656 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
657 " Compression: BI_RGB");
662 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
663 " Compression: BI_RLE4");
668 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
669 " Compression: BI_RLE8");
674 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
675 " Compression: BI_BITFIELDS");
680 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
681 " Compression: BI_PNG");
686 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
687 " Compression: BI_JPEG");
692 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
693 " Compression: UNKNOWN (%u)",bmp_info.compression);
696 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
697 " Number of colors: %u",bmp_info.number_colors);
699 bmp_info.red_mask=ReadBlobLSBLong(image);
700 bmp_info.green_mask=ReadBlobLSBLong(image);
701 bmp_info.blue_mask=ReadBlobLSBLong(image);
702 if (bmp_info.size > 40)
708 Read color management information.
710 bmp_info.alpha_mask=ReadBlobLSBLong(image);
711 bmp_info.colorspace=(long) ReadBlobLSBLong(image);
713 Decode 2^30 fixed point formatted CIE primaries.
715 bmp_info.red_primary.x=(double) ReadBlobLSBLong(image)/0x3ffffff;
716 bmp_info.red_primary.y=(double) ReadBlobLSBLong(image)/0x3ffffff;
717 bmp_info.red_primary.z=(double) ReadBlobLSBLong(image)/0x3ffffff;
718 bmp_info.green_primary.x=(double) ReadBlobLSBLong(image)/0x3ffffff;
719 bmp_info.green_primary.y=(double) ReadBlobLSBLong(image)/0x3ffffff;
720 bmp_info.green_primary.z=(double) ReadBlobLSBLong(image)/0x3ffffff;
721 bmp_info.blue_primary.x=(double) ReadBlobLSBLong(image)/0x3ffffff;
722 bmp_info.blue_primary.y=(double) ReadBlobLSBLong(image)/0x3ffffff;
723 bmp_info.blue_primary.z=(double) ReadBlobLSBLong(image)/0x3ffffff;
724 sum=bmp_info.red_primary.x+bmp_info.red_primary.x+
725 bmp_info.red_primary.z;
726 image->chromaticity.red_primary.x/=sum;
727 image->chromaticity.red_primary.y/=sum;
728 sum=bmp_info.green_primary.x+bmp_info.green_primary.x+
729 bmp_info.green_primary.z;
730 image->chromaticity.green_primary.x/=sum;
731 image->chromaticity.green_primary.y/=sum;
732 sum=bmp_info.blue_primary.x+bmp_info.blue_primary.x+
733 bmp_info.blue_primary.z;
734 image->chromaticity.blue_primary.x/=sum;
735 image->chromaticity.blue_primary.y/=sum;
737 Decode 16^16 fixed point formatted gamma_scales.
739 bmp_info.gamma_scale.x=(double) ReadBlobLSBLong(image)/0xffff;
740 bmp_info.gamma_scale.y=(double) ReadBlobLSBLong(image)/0xffff;
741 bmp_info.gamma_scale.z=(double) ReadBlobLSBLong(image)/0xffff;
743 Compute a single gamma from the BMP 3-channel gamma.
745 image->gamma=(bmp_info.gamma_scale.x+bmp_info.gamma_scale.y+
746 bmp_info.gamma_scale.z)/3.0;
748 if (bmp_info.size > 108)
754 Read BMP Version 5 color management information.
756 intent=ReadBlobLSBLong(image);
757 switch ((int) intent)
759 case LCS_GM_BUSINESS:
761 image->rendering_intent=SaturationIntent;
764 case LCS_GM_GRAPHICS:
766 image->rendering_intent=RelativeIntent;
771 image->rendering_intent=PerceptualIntent;
774 case LCS_GM_ABS_COLORIMETRIC:
776 image->rendering_intent=AbsoluteIntent;
780 profile_data=ReadBlobLSBLong(image);
781 profile_size=ReadBlobLSBLong(image);
782 (void) ReadBlobLSBLong(image); /* Reserved byte */
785 if ((bmp_info.compression != BI_RGB) &&
786 ((MagickSizeType) bmp_info.file_size != GetBlobSize(image)))
787 (void) ThrowMagickException(exception,GetMagickModule(),CorruptImageError,
788 "LengthAndFilesizeDoNotMatch","`%s'",image->filename);
789 if (bmp_info.width <= 0)
790 ThrowReaderException(CorruptImageError,"NegativeOrZeroImageSize");
791 if (bmp_info.height == 0)
792 ThrowReaderException(CorruptImageError,"NegativeOrZeroImageSize");
793 if (bmp_info.planes != 1)
794 ThrowReaderException(CorruptImageError,"StaticPlanesValueNotEqualToOne");
795 if ((bmp_info.bits_per_pixel != 1) && (bmp_info.bits_per_pixel != 4) &&
796 (bmp_info.bits_per_pixel != 8) && (bmp_info.bits_per_pixel != 16) &&
797 (bmp_info.bits_per_pixel != 24) && (bmp_info.bits_per_pixel != 32))
798 ThrowReaderException(CorruptImageError,"UnrecognizedBitsPerPixel");
799 if (bmp_info.number_colors > (1U << bmp_info.bits_per_pixel))
801 if (bmp_info.bits_per_pixel < 24)
802 ThrowReaderException(CorruptImageError,"UnrecognizedNumberOfColors");
803 bmp_info.number_colors=0;
805 if (bmp_info.compression > 3)
806 ThrowReaderException(CorruptImageError,"UnrecognizedImageCompression");
807 if ((bmp_info.compression == 1) && (bmp_info.bits_per_pixel != 8))
808 ThrowReaderException(CorruptImageError,"UnrecognizedBitsPerPixel");
809 if ((bmp_info.compression == 2) && (bmp_info.bits_per_pixel != 4))
810 ThrowReaderException(CorruptImageError,"UnrecognizedBitsPerPixel");
811 if ((bmp_info.compression == 3) && (bmp_info.bits_per_pixel < 16))
812 ThrowReaderException(CorruptImageError,"UnrecognizedBitsPerPixel");
813 switch (bmp_info.compression)
821 ThrowReaderException(CoderError,"JPEGCompressNotSupported");
823 ThrowReaderException(CoderError,"PNGCompressNotSupported");
825 ThrowReaderException(CorruptImageError,"UnrecognizedImageCompression");
827 image->columns=(unsigned long) MagickAbsoluteValue(bmp_info.width);
828 image->rows=(unsigned long) MagickAbsoluteValue(bmp_info.height);
829 image->depth=bmp_info.bits_per_pixel <= 8 ? bmp_info.bits_per_pixel : 8;
830 if ((bmp_info.bits_per_pixel == 16) ||
831 (bmp_info.bits_per_pixel == 32))
832 image->matte=bmp_info.alpha_mask != 0 ? MagickTrue : MagickFalse;
833 if ((bmp_info.number_colors != 0) || (bmp_info.bits_per_pixel < 16))
835 image->storage_class=PseudoClass;
836 image->colors=bmp_info.number_colors;
837 if (image->colors == 0)
838 image->colors=1L << bmp_info.bits_per_pixel;
840 if (image->storage_class == PseudoClass)
849 Read BMP raster colormap.
851 if (image->debug != MagickFalse)
852 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
853 " Reading colormap of %ld colors",image->colors);
854 if (AcquireImageColormap(image,image->colors) == MagickFalse)
855 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
856 bmp_colormap=(unsigned char *) AcquireQuantumMemory((size_t)
857 image->colors,4*sizeof(*bmp_colormap));
858 if (bmp_colormap == (unsigned char *) NULL)
859 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
860 if ((bmp_info.size == 12) || (bmp_info.size == 64))
864 offset=SeekBlob(image,start_position+14+bmp_info.size,SEEK_SET);
866 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
867 count=ReadBlob(image,packet_size*image->colors,bmp_colormap);
868 if (count != (ssize_t) (packet_size*image->colors))
869 ThrowReaderException(CorruptImageError,"InsufficientImageDataInFile");
871 for (i=0; i < (long) image->colors; i++)
873 image->colormap[i].blue=ScaleCharToQuantum(*p++);
874 image->colormap[i].green=ScaleCharToQuantum(*p++);
875 image->colormap[i].red=ScaleCharToQuantum(*p++);
876 if (packet_size == 4)
879 bmp_colormap=(unsigned char *) RelinquishMagickMemory(bmp_colormap);
881 if ((image_info->ping != MagickFalse) && (image_info->number_scenes != 0))
882 if (image->scene >= (image_info->scene+image_info->number_scenes-1))
887 offset=SeekBlob(image,start_position+bmp_info.offset_bits,SEEK_SET);
889 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
890 if (bmp_info.compression == BI_RLE4)
891 bmp_info.bits_per_pixel<<=1;
892 bytes_per_line=4*((image->columns*bmp_info.bits_per_pixel+31)/32);
893 length=(size_t) bytes_per_line*image->rows;
894 pixels=(unsigned char *) AcquireQuantumMemory((size_t) image->rows,
895 MagickMax(bytes_per_line,image->columns+256UL)*sizeof(*pixels));
896 if (pixels == (unsigned char *) NULL)
897 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
898 if ((bmp_info.compression == BI_RGB) ||
899 (bmp_info.compression == BI_BITFIELDS))
901 if (image->debug != MagickFalse)
902 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
903 " Reading pixels (%ld bytes)",(long) length);
904 count=ReadBlob(image,length,pixels);
905 if (count != (ssize_t) length)
906 ThrowReaderException(CorruptImageError,"InsufficientImageDataInFile");
911 Convert run-length encoded raster pixels.
913 status=DecodeImage(image,bmp_info.compression,pixels);
914 if (status == MagickFalse)
915 ThrowReaderException(CorruptImageError,
916 "UnableToRunlengthDecodeImage");
919 Initialize image structure.
921 image->x_resolution=(double) bmp_info.x_pixels/100.0;
922 image->y_resolution=(double) bmp_info.y_pixels/100.0;
923 image->units=PixelsPerCentimeterResolution;
925 Convert BMP raster image to pixel packets.
927 if (bmp_info.compression == BI_RGB)
929 bmp_info.alpha_mask=0;
930 bmp_info.red_mask=0x00ff0000U;
931 bmp_info.green_mask=0x0000ff00U;
932 bmp_info.blue_mask=0x000000ffU;
933 if (bmp_info.bits_per_pixel == 16)
938 bmp_info.red_mask=0x00007c00U;
939 bmp_info.green_mask=0x000003e0U;
940 bmp_info.blue_mask=0x0000001fU;
943 if ((bmp_info.bits_per_pixel == 16) || (bmp_info.bits_per_pixel == 32))
945 register unsigned long
949 Get shift and quantum bits info from bitfield masks.
951 (void) ResetMagickMemory(&shift,0,sizeof(shift));
952 (void) ResetMagickMemory(&quantum_bits,0,sizeof(quantum_bits));
953 if (bmp_info.red_mask != 0)
954 while (((bmp_info.red_mask << shift.red) & 0x80000000UL) == 0)
956 if (bmp_info.green_mask != 0)
957 while (((bmp_info.green_mask << shift.green) & 0x80000000UL) == 0)
959 if (bmp_info.blue_mask != 0)
960 while (((bmp_info.blue_mask << shift.blue) & 0x80000000UL) == 0)
962 if (bmp_info.alpha_mask != 0)
963 while (((bmp_info.alpha_mask << shift.opacity) & 0x80000000UL) == 0)
966 while (((bmp_info.red_mask << sample) & 0x80000000UL) != 0)
968 quantum_bits.red=(Quantum) (sample-shift.red);
970 while (((bmp_info.green_mask << sample) & 0x80000000UL) != 0)
972 quantum_bits.green=(Quantum) (sample-shift.green);
974 while (((bmp_info.blue_mask << sample) & 0x80000000UL) != 0)
976 quantum_bits.blue=(Quantum) (sample-shift.blue);
977 sample=shift.opacity;
978 while (((bmp_info.alpha_mask << sample) & 0x80000000UL) != 0)
980 quantum_bits.opacity=(Quantum) (sample-shift.opacity);
982 switch (bmp_info.bits_per_pixel)
987 Convert bitmap scanline.
989 for (y=(long) image->rows-1; y >= 0; y--)
991 p=pixels+(image->rows-y-1)*bytes_per_line;
992 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
993 if (q == (PixelPacket *) NULL)
995 indexes=GetAuthenticIndexQueue(image);
996 for (x=0; x < ((long) image->columns-7); x+=8)
998 for (bit=0; bit < 8; bit++)
1000 index=(IndexPacket) (((*p) & (0x80 >> bit)) != 0 ? 0x01 : 0x00);
1001 indexes[x+bit]=index;
1002 *q++=image->colormap[(long) index];
1006 if ((image->columns % 8) != 0)
1008 for (bit=0; bit < (image->columns % 8); bit++)
1010 index=(IndexPacket) (((*p) & (0x80 >> bit)) != 0 ? 0x01 : 0x00);
1011 indexes[x+bit]=index;
1012 *q++=image->colormap[(long) index];
1016 if (SyncAuthenticPixels(image,exception) == MagickFalse)
1018 if (image->previous == (Image *) NULL)
1020 status=SetImageProgress(image,LoadImageTag,y,image->rows);
1021 if (status == MagickFalse)
1030 Convert PseudoColor scanline.
1032 for (y=(long) image->rows-1; y >= 0; y--)
1034 p=pixels+(image->rows-y-1)*bytes_per_line;
1035 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
1036 if (q == (PixelPacket *) NULL)
1038 indexes=GetAuthenticIndexQueue(image);
1039 for (x=0; x < ((long) image->columns-1); x+=2)
1041 index=ConstrainColormapIndex(image,(*p >> 4) & 0x0f);
1043 *q++=image->colormap[(long) index];
1044 index=ConstrainColormapIndex(image,*p & 0x0f);
1046 *q++=image->colormap[(long) index];
1049 if ((image->columns % 2) != 0)
1051 index=ConstrainColormapIndex(image,(*p >> 4) & 0xf);
1053 *q++=image->colormap[(long) index];
1056 if (SyncAuthenticPixels(image,exception) == MagickFalse)
1058 if (image->previous == (Image *) NULL)
1060 status=SetImageProgress(image,LoadImageTag,y,image->rows);
1061 if (status == MagickFalse)
1070 Convert PseudoColor scanline.
1072 if ((bmp_info.compression == BI_RLE8) ||
1073 (bmp_info.compression == BI_RLE4))
1074 bytes_per_line=image->columns;
1075 for (y=(long) image->rows-1; y >= 0; y--)
1077 p=pixels+(image->rows-y-1)*bytes_per_line;
1078 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
1079 if (q == (PixelPacket *) NULL)
1081 indexes=GetAuthenticIndexQueue(image);
1082 for (x = (long)image->columns; x != 0; --x)
1084 index=ConstrainColormapIndex(image,*p);
1086 *q=image->colormap[(long) index];
1090 if (SyncAuthenticPixels(image,exception) == MagickFalse)
1092 offset=(MagickOffsetType) (image->rows-y-1);
1093 if (image->previous == (Image *) NULL)
1095 status=SetImageProgress(image,LoadImageTag,y,image->rows);
1096 if (status == MagickFalse)
1108 Convert bitfield encoded 16-bit PseudoColor scanline.
1110 if (bmp_info.compression != BI_RGB &&
1111 bmp_info.compression != BI_BITFIELDS)
1112 ThrowReaderException(CorruptImageError,
1113 "UnrecognizedImageCompression");
1114 bytes_per_line=2*(image->columns+image->columns % 2);
1115 image->storage_class=DirectClass;
1116 for (y=(long) image->rows-1; y >= 0; y--)
1118 p=pixels+(image->rows-y-1)*bytes_per_line;
1119 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
1120 if (q == (PixelPacket *) NULL)
1122 for (x=0; x < (long) image->columns; x++)
1124 pixel=(unsigned long) (*p++);
1126 red=((pixel & bmp_info.red_mask) << shift.red) >> 16;
1127 if (quantum_bits.red == 5)
1128 red|=((red & 0xe000) >> 5);
1129 if (quantum_bits.red <= 8)
1130 red|=((red & 0xff00) >> 8);
1131 green=((pixel & bmp_info.green_mask) << shift.green) >> 16;
1132 if (quantum_bits.green == 5)
1133 green|=((green & 0xe000) >> 5);
1134 if (quantum_bits.green == 6)
1135 green|=((green & 0xc000) >> 6);
1136 if (quantum_bits.green <= 8)
1137 green|=((green & 0xff00) >> 8);
1138 blue=((pixel & bmp_info.blue_mask) << shift.blue) >> 16;
1139 if (quantum_bits.blue == 5)
1140 blue|=((blue & 0xe000) >> 5);
1141 if (quantum_bits.blue <= 8)
1142 blue|=((blue & 0xff00) >> 8);
1143 opacity=((pixel & bmp_info.alpha_mask) << shift.opacity) >> 16;
1144 if (quantum_bits.opacity <= 8)
1145 opacity|=((opacity & 0xff00) >> 8);
1146 q->red=ScaleShortToQuantum((unsigned short) red);
1147 q->green=ScaleShortToQuantum((unsigned short) green);
1148 q->blue=ScaleShortToQuantum((unsigned short) blue);
1149 SetOpacityPixelComponent(q,OpaqueOpacity);
1150 if (image->matte != MagickFalse)
1151 q->opacity=ScaleShortToQuantum((unsigned short) (65535-opacity));
1154 if (SyncAuthenticPixels(image,exception) == MagickFalse)
1156 offset=(MagickOffsetType) (image->rows-y-1);
1157 if (image->previous == (Image *) NULL)
1159 status=SetImageProgress(image,LoadImageTag,y,image->rows);
1160 if (status == MagickFalse)
1169 Convert DirectColor scanline.
1171 bytes_per_line=4*((image->columns*24+31)/32);
1172 for (y=(long) image->rows-1; y >= 0; y--)
1174 p=pixels+(image->rows-y-1)*bytes_per_line;
1175 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
1176 if (q == (PixelPacket *) NULL)
1178 for (x=0; x < (long) image->columns; x++)
1180 q->blue=ScaleCharToQuantum(*p++);
1181 q->green=ScaleCharToQuantum(*p++);
1182 q->red=ScaleCharToQuantum(*p++);
1183 SetOpacityPixelComponent(q,OpaqueOpacity);
1186 if (SyncAuthenticPixels(image,exception) == MagickFalse)
1188 offset=(MagickOffsetType) (image->rows-y-1);
1189 if (image->previous == (Image *) NULL)
1191 status=SetImageProgress(image,LoadImageTag,y,image->rows);
1192 if (status == MagickFalse)
1201 Convert bitfield encoded DirectColor scanline.
1203 if ((bmp_info.compression != BI_RGB) &&
1204 (bmp_info.compression != BI_BITFIELDS))
1205 ThrowReaderException(CorruptImageError,
1206 "UnrecognizedImageCompression");
1207 bytes_per_line=4*(image->columns);
1208 for (y=(long) image->rows-1; y >= 0; y--)
1213 p=pixels+(image->rows-y-1)*bytes_per_line;
1214 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
1215 if (q == (PixelPacket *) NULL)
1217 for (x=0; x < (long) image->columns; x++)
1219 pixel=(unsigned long) (*p++);
1221 pixel|=(*p++ << 16);
1222 pixel|=(*p++ << 24);
1223 red=((pixel & bmp_info.red_mask) << shift.red) >> 16;
1224 if (quantum_bits.red == 8)
1226 green=((pixel & bmp_info.green_mask) << shift.green) >> 16;
1227 if (quantum_bits.green == 8)
1228 green|=(green >> 8);
1229 blue=((pixel & bmp_info.blue_mask) << shift.blue) >> 16;
1230 if (quantum_bits.blue == 8)
1232 opacity=((pixel & bmp_info.alpha_mask) << shift.opacity) >> 16;
1233 if (quantum_bits.opacity == 8)
1234 opacity|=(opacity >> 8);
1235 q->red=ScaleShortToQuantum((unsigned short) red);
1236 q->green=ScaleShortToQuantum((unsigned short) green);
1237 q->blue=ScaleShortToQuantum((unsigned short) blue);
1238 SetOpacityPixelComponent(q,OpaqueOpacity);
1239 if (image->matte != MagickFalse)
1240 q->opacity=ScaleShortToQuantum((unsigned short) (65535-opacity));
1243 if (SyncAuthenticPixels(image,exception) == MagickFalse)
1245 offset=(MagickOffsetType) (image->rows-y-1);
1246 if (image->previous == (Image *) NULL)
1248 status=SetImageProgress(image,LoadImageTag,y,image->rows);
1249 if (status == MagickFalse)
1256 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
1258 pixels=(unsigned char *) RelinquishMagickMemory(pixels);
1259 if (EOFBlob(image) != MagickFalse)
1261 ThrowFileException(exception,CorruptImageError,"UnexpectedEndOfFile",
1265 if (bmp_info.height < 0)
1271 Correct image orientation.
1273 flipped_image=FlipImage(image,exception);
1274 if (flipped_image != (Image *) NULL)
1276 DuplicateBlob(flipped_image,image);
1277 image=DestroyImage(image);
1278 image=flipped_image;
1282 Proceed to next image.
1284 if (image_info->number_scenes != 0)
1285 if (image->scene >= (image_info->scene+image_info->number_scenes-1))
1288 if (bmp_info.ba_offset != 0)
1290 offset=SeekBlob(image,(MagickOffsetType) bmp_info.ba_offset,SEEK_SET);
1292 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
1294 count=ReadBlob(image,2,magick);
1295 if ((count == 2) && (IsBMP(magick,2) != MagickFalse))
1298 Acquire next image structure.
1300 AcquireNextImage(image_info,image);
1301 if (GetNextImageInList(image) == (Image *) NULL)
1303 image=DestroyImageList(image);
1304 return((Image *) NULL);
1306 image=SyncNextImageInList(image);
1307 status=SetImageProgress(image,LoadImagesTag,TellBlob(image),
1308 GetBlobSize(image));
1309 if (status == MagickFalse)
1312 } while (IsBMP(magick,2) != MagickFalse);
1313 (void) CloseBlob(image);
1314 return(GetFirstImageInList(image));
1318 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1322 % R e g i s t e r B M P I m a g e %
1326 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1328 % RegisterBMPImage() adds attributes for the BMP image format to
1329 % the list of supported formats. The attributes include the image format
1330 % tag, a method to read and/or write the format, whether the format
1331 % supports the saving of more than one frame to the same file or blob,
1332 % whether the format supports native in-memory I/O, and a brief
1333 % description of the format.
1335 % The format of the RegisterBMPImage method is:
1337 % unsigned long RegisterBMPImage(void)
1340 ModuleExport unsigned long RegisterBMPImage(void)
1345 entry=SetMagickInfo("BMP");
1346 entry->decoder=(DecodeImageHandler *) ReadBMPImage;
1347 entry->encoder=(EncodeImageHandler *) WriteBMPImage;
1348 entry->magick=(IsImageFormatHandler *) IsBMP;
1349 entry->description=ConstantString("Microsoft Windows bitmap image");
1350 entry->module=ConstantString("BMP");
1351 entry->adjoin=MagickFalse;
1352 entry->seekable_stream=MagickTrue;
1353 (void) RegisterMagickInfo(entry);
1354 entry=SetMagickInfo("BMP2");
1355 entry->encoder=(EncodeImageHandler *) WriteBMPImage;
1356 entry->magick=(IsImageFormatHandler *) IsBMP;
1357 entry->description=ConstantString("Microsoft Windows bitmap image (V2)");
1358 entry->module=ConstantString("BMP");
1359 entry->adjoin=MagickFalse;
1360 entry->seekable_stream=MagickTrue;
1361 (void) RegisterMagickInfo(entry);
1362 entry=SetMagickInfo("BMP3");
1363 entry->encoder=(EncodeImageHandler *) WriteBMPImage;
1364 entry->magick=(IsImageFormatHandler *) IsBMP;
1365 entry->description=ConstantString("Microsoft Windows bitmap image (V3)");
1366 entry->module=ConstantString("BMP");
1367 entry->adjoin=MagickFalse;
1368 entry->seekable_stream=MagickTrue;
1369 (void) RegisterMagickInfo(entry);
1370 return(MagickImageCoderSignature);
1374 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1378 % U n r e g i s t e r B M P I m a g e %
1382 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1384 % UnregisterBMPImage() removes format registrations made by the
1385 % BMP module from the list of supported formats.
1387 % The format of the UnregisterBMPImage method is:
1389 % UnregisterBMPImage(void)
1392 ModuleExport void UnregisterBMPImage(void)
1394 (void) UnregisterMagickInfo("BMP");
1395 (void) UnregisterMagickInfo("BMP2");
1396 (void) UnregisterMagickInfo("BMP3");
1400 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1404 % W r i t e B M P I m a g e %
1408 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1410 % WriteBMPImage() writes an image in Microsoft Windows bitmap encoded
1411 % image format, version 3 for Windows or (if the image has a matte channel)
1414 % The format of the WriteBMPImage method is:
1416 % MagickBooleanType WriteBMPImage(const ImageInfo *image_info,Image *image)
1418 % A description of each parameter follows.
1420 % o image_info: the image info.
1422 % o image: The image.
1425 static MagickBooleanType WriteBMPImage(const ImageInfo *image_info,Image *image)
1443 register const IndexPacket
1446 register const PixelPacket
1453 register unsigned char
1465 Open output image file.
1467 assert(image_info != (const ImageInfo *) NULL);
1468 assert(image_info->signature == MagickSignature);
1469 assert(image != (Image *) NULL);
1470 assert(image->signature == MagickSignature);
1471 if (image->debug != MagickFalse)
1472 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1473 status=OpenBlob(image_info,image,WriteBinaryBlobMode,&image->exception);
1474 if (status == MagickFalse)
1477 if (LocaleCompare(image_info->magick,"BMP2") == 0)
1480 if (LocaleCompare(image_info->magick,"BMP3") == 0)
1486 Initialize BMP raster file header.
1488 if (image->colorspace != RGBColorspace)
1489 (void) TransformImageColorspace(image,RGBColorspace);
1490 (void) ResetMagickMemory(&bmp_info,0,sizeof(bmp_info));
1491 bmp_info.file_size=14+12;
1493 bmp_info.file_size+=28;
1494 bmp_info.offset_bits=bmp_info.file_size;
1495 bmp_info.compression=BI_RGB;
1496 if ((image->storage_class == PseudoClass) && (image->colors > 256))
1497 (void) SetImageStorageClass(image,DirectClass);
1498 if (image->storage_class != DirectClass)
1501 Colormapped BMP raster.
1503 bmp_info.bits_per_pixel=8;
1504 if (image->colors <= 2)
1505 bmp_info.bits_per_pixel=1;
1507 if (image->colors <= 16)
1508 bmp_info.bits_per_pixel=4;
1510 if (image->colors <= 256)
1511 bmp_info.bits_per_pixel=8;
1512 if (image_info->compression == RLECompression)
1513 bmp_info.bits_per_pixel=8;
1514 bmp_info.number_colors=1U << bmp_info.bits_per_pixel;
1515 if (image->matte != MagickFalse)
1516 (void) SetImageStorageClass(image,DirectClass);
1518 if ((unsigned long) bmp_info.number_colors < image->colors)
1519 (void) SetImageStorageClass(image,DirectClass);
1522 bmp_info.file_size+=3*(1UL << bmp_info.bits_per_pixel);
1523 bmp_info.offset_bits+=3*(1UL << bmp_info.bits_per_pixel);
1526 bmp_info.file_size+=(1UL << bmp_info.bits_per_pixel);
1527 bmp_info.offset_bits+=(1UL << bmp_info.bits_per_pixel);
1531 if (image->storage_class == DirectClass)
1534 Full color BMP raster.
1536 bmp_info.number_colors=0;
1537 bmp_info.bits_per_pixel=(unsigned short)
1538 ((type > 3) && (image->matte != MagickFalse) ? 32 : 24);
1539 bmp_info.compression=(unsigned int) ((type > 3) &&
1540 (image->matte != MagickFalse) ? BI_BITFIELDS : BI_RGB);
1542 bytes_per_line=4*((image->columns*bmp_info.bits_per_pixel+31)/32);
1543 bmp_info.ba_offset=0;
1544 profile=GetImageProfile(image,"icc");
1545 have_color_info=(image->rendering_intent != UndefinedIntent) ||
1546 (profile != (StringInfo *) NULL) || (image->gamma != 0.0) ? MagickTrue :
1551 if ((type == 3) || ((image->matte == MagickFalse) &&
1552 (have_color_info == MagickFalse)))
1564 if ((image->rendering_intent != UndefinedIntent) ||
1565 (profile != (StringInfo *) NULL))
1570 bmp_info.file_size+=extra_size;
1571 bmp_info.offset_bits+=extra_size;
1573 bmp_info.width=(long) image->columns;
1574 bmp_info.height=(long) image->rows;
1576 bmp_info.image_size=(unsigned int) (bytes_per_line*image->rows);
1577 bmp_info.file_size+=bmp_info.image_size;
1578 bmp_info.x_pixels=75*39;
1579 bmp_info.y_pixels=75*39;
1580 switch (image->units)
1582 case UndefinedResolution:
1583 case PixelsPerInchResolution:
1585 bmp_info.x_pixels=(unsigned int) (100.0*image->x_resolution/2.54);
1586 bmp_info.y_pixels=(unsigned int) (100.0*image->y_resolution/2.54);
1589 case PixelsPerCentimeterResolution:
1591 bmp_info.x_pixels=(unsigned int) (100.0*image->x_resolution);
1592 bmp_info.y_pixels=(unsigned int) (100.0*image->y_resolution);
1596 bmp_info.colors_important=bmp_info.number_colors;
1598 Convert MIFF to BMP raster pixels.
1600 pixels=(unsigned char *) AcquireQuantumMemory((size_t) bmp_info.image_size,
1602 if (pixels == (unsigned char *) NULL)
1603 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
1604 (void) ResetMagickMemory(pixels,0,(size_t) bmp_info.image_size);
1605 switch (bmp_info.bits_per_pixel)
1614 Convert PseudoClass image to a BMP monochrome image.
1616 for (y=0; y < (long) image->rows; y++)
1618 p=GetVirtualPixels(image,0,y,image->columns,1,&image->exception);
1619 if (p == (const PixelPacket *) NULL)
1621 indexes=GetVirtualIndexQueue(image);
1622 q=pixels+(image->rows-y-1)*bytes_per_line;
1625 for (x=0; x < (long) image->columns; x++)
1628 byte|=indexes[x] != 0 ? 0x01 : 0x00;
1632 *q++=(unsigned char) byte;
1639 *q++=(unsigned char) (byte << (8-bit));
1642 for (x=(long) (image->columns+7)/8; x < (long) bytes_per_line; x++)
1644 if (image->previous == (Image *) NULL)
1646 status=SetImageProgress(image,SaveImageTag,y,image->rows);
1647 if (status == MagickFalse)
1660 Convert PseudoClass image to a BMP monochrome image.
1662 for (y=0; y < (long) image->rows; y++)
1664 p=GetVirtualPixels(image,0,y,image->columns,1,&image->exception);
1665 if (p == (const PixelPacket *) NULL)
1667 indexes=GetVirtualIndexQueue(image);
1668 q=pixels+(image->rows-y-1)*bytes_per_line;
1671 for (x=0; x < (long) image->columns; x++)
1674 byte|=((unsigned long) indexes[x] & 0x0f);
1678 *q++=(unsigned char) byte;
1685 *q++=(unsigned char) (byte << 4);
1688 for (x=(long) (image->columns+1)/2; x < (long) bytes_per_line; x++)
1690 if (image->previous == (Image *) NULL)
1692 status=SetImageProgress(image,SaveImageTag,y,image->rows);
1693 if (status == MagickFalse)
1702 Convert PseudoClass packet to BMP pixel.
1704 for (y=0; y < (long) image->rows; y++)
1706 p=GetVirtualPixels(image,0,y,image->columns,1,&image->exception);
1707 if (p == (const PixelPacket *) NULL)
1709 indexes=GetVirtualIndexQueue(image);
1710 q=pixels+(image->rows-y-1)*bytes_per_line;
1711 for (x=0; x < (long) image->columns; x++)
1712 *q++=(unsigned char) indexes[x];
1713 for ( ; x < (long) bytes_per_line; x++)
1715 if (image->previous == (Image *) NULL)
1717 status=SetImageProgress(image,SaveImageTag,y,image->rows);
1718 if (status == MagickFalse)
1727 Convert DirectClass packet to BMP BGR888.
1729 for (y=0; y < (long) image->rows; y++)
1731 p=GetVirtualPixels(image,0,y,image->columns,1,&image->exception);
1732 if (p == (const PixelPacket *) NULL)
1734 q=pixels+(image->rows-y-1)*bytes_per_line;
1735 for (x=0; x < (long) image->columns; x++)
1737 *q++=ScaleQuantumToChar(GetBluePixelComponent(p));
1738 *q++=ScaleQuantumToChar(GetGreenPixelComponent(p));
1739 *q++=ScaleQuantumToChar(GetRedPixelComponent(p));
1742 for (x=3L*(long) image->columns; x < (long) bytes_per_line; x++)
1744 if (image->previous == (Image *) NULL)
1746 status=SetImageProgress(image,SaveImageTag,y,image->rows);
1747 if (status == MagickFalse)
1756 Convert DirectClass packet to ARGB8888 pixel.
1758 for (y=0; y < (long) image->rows; y++)
1760 p=GetVirtualPixels(image,0,y,image->columns,1,&image->exception);
1761 if (p == (const PixelPacket *) NULL)
1763 q=pixels+(image->rows-y-1)*bytes_per_line;
1764 for (x=0; x < (long) image->columns; x++)
1766 *q++=ScaleQuantumToChar(GetBluePixelComponent(p));
1767 *q++=ScaleQuantumToChar(GetGreenPixelComponent(p));
1768 *q++=ScaleQuantumToChar(GetRedPixelComponent(p));
1769 *q++=ScaleQuantumToChar(QuantumRange-GetOpacityPixelComponent(p));
1772 if (image->previous == (Image *) NULL)
1774 status=SetImageProgress(image,SaveImageTag,y,image->rows);
1775 if (status == MagickFalse)
1782 if ((type > 2) && (bmp_info.bits_per_pixel == 8))
1783 if (image_info->compression != NoCompression)
1789 Convert run-length encoded raster pixels.
1791 length=(size_t) (2*(bytes_per_line+2)*(image->rows+2)+2);
1792 bmp_data=(unsigned char *) NULL;
1793 if (~length >= bytes_per_line)
1794 bmp_data=(unsigned char *) AcquireQuantumMemory(length+
1795 bytes_per_line,sizeof(*bmp_data));
1796 if (bmp_data == (unsigned char *) NULL)
1798 pixels=(unsigned char *) RelinquishMagickMemory(pixels);
1799 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
1801 bmp_info.file_size-=bmp_info.image_size;
1802 bmp_info.image_size=(unsigned int) EncodeImage(image,bytes_per_line,
1804 bmp_info.file_size+=bmp_info.image_size;
1805 pixels=(unsigned char *) RelinquishMagickMemory(pixels);
1807 bmp_info.compression=BI_RLE8;
1810 Write BMP for Windows, all versions, 14-byte header.
1812 if (image->debug != MagickFalse)
1814 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1815 " Writing BMP version %ld datastream",type);
1816 if (image->storage_class == DirectClass)
1817 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1818 " Storage class=DirectClass");
1820 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1821 " Storage class=PseudoClass");
1822 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1823 " Image depth=%lu",image->depth);
1824 if (image->matte != MagickFalse)
1825 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1828 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1829 " Matte=MagickFalse");
1830 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1831 " BMP bits_per_pixel=%d",bmp_info.bits_per_pixel);
1832 switch ((int) bmp_info.compression)
1836 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1837 " Compression=BI_RGB");
1842 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1843 " Compression=BI_RLE8");
1848 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1849 " Compression=BI_BITFIELDS");
1854 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1855 " Compression=UNKNOWN (%u)",bmp_info.compression);
1859 if (bmp_info.number_colors == 0)
1860 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1861 " Number_colors=unspecified");
1863 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1864 " Number_colors=%u",bmp_info.number_colors);
1866 (void) WriteBlob(image,2,(unsigned char *) "BM");
1867 (void) WriteBlobLSBLong(image,bmp_info.file_size);
1868 (void) WriteBlobLSBLong(image,bmp_info.ba_offset); /* always 0 */
1869 (void) WriteBlobLSBLong(image,bmp_info.offset_bits);
1873 Write 12-byte version 2 bitmap header.
1875 (void) WriteBlobLSBLong(image,bmp_info.size);
1876 (void) WriteBlobLSBShort(image,(unsigned short) bmp_info.width);
1877 (void) WriteBlobLSBShort(image,(unsigned short) bmp_info.height);
1878 (void) WriteBlobLSBShort(image,bmp_info.planes);
1879 (void) WriteBlobLSBShort(image,bmp_info.bits_per_pixel);
1884 Write 40-byte version 3+ bitmap header.
1886 (void) WriteBlobLSBLong(image,bmp_info.size);
1887 (void) WriteBlobLSBLong(image,(unsigned int) bmp_info.width);
1888 (void) WriteBlobLSBLong(image,(unsigned int) bmp_info.height);
1889 (void) WriteBlobLSBShort(image,bmp_info.planes);
1890 (void) WriteBlobLSBShort(image,bmp_info.bits_per_pixel);
1891 (void) WriteBlobLSBLong(image,bmp_info.compression);
1892 (void) WriteBlobLSBLong(image,bmp_info.image_size);
1893 (void) WriteBlobLSBLong(image,bmp_info.x_pixels);
1894 (void) WriteBlobLSBLong(image,bmp_info.y_pixels);
1895 (void) WriteBlobLSBLong(image,bmp_info.number_colors);
1896 (void) WriteBlobLSBLong(image,bmp_info.colors_important);
1898 if ((type > 3) && ((image->matte != MagickFalse) ||
1899 (have_color_info != MagickFalse)))
1902 Write the rest of the 108-byte BMP Version 4 header.
1904 (void) WriteBlobLSBLong(image,0x00ff0000U); /* Red mask */
1905 (void) WriteBlobLSBLong(image,0x0000ff00U); /* Green mask */
1906 (void) WriteBlobLSBLong(image,0x000000ffU); /* Blue mask */
1907 (void) WriteBlobLSBLong(image,0xff000000U); /* Alpha mask */
1908 (void) WriteBlobLSBLong(image,0x00000001U); /* CSType==Calib. RGB */
1909 (void) WriteBlobLSBLong(image,(unsigned int)
1910 image->chromaticity.red_primary.x*0x3ffffff);
1911 (void) WriteBlobLSBLong(image,(unsigned int)
1912 image->chromaticity.red_primary.y*0x3ffffff);
1913 (void) WriteBlobLSBLong(image,(unsigned int)
1914 (1.000f-(image->chromaticity.red_primary.x+
1915 image->chromaticity.red_primary.y)*0x3ffffff));
1916 (void) WriteBlobLSBLong(image,(unsigned int)
1917 image->chromaticity.green_primary.x*0x3ffffff);
1918 (void) WriteBlobLSBLong(image,(unsigned int)
1919 image->chromaticity.green_primary.y*0x3ffffff);
1920 (void) WriteBlobLSBLong(image,(unsigned int)
1921 (1.000f-(image->chromaticity.green_primary.x+
1922 image->chromaticity.green_primary.y)*0x3ffffff));
1923 (void) WriteBlobLSBLong(image,(unsigned int)
1924 image->chromaticity.blue_primary.x*0x3ffffff);
1925 (void) WriteBlobLSBLong(image,(unsigned int)
1926 image->chromaticity.blue_primary.y*0x3ffffff);
1927 (void) WriteBlobLSBLong(image,(unsigned int)
1928 (1.000f-(image->chromaticity.blue_primary.x+
1929 image->chromaticity.blue_primary.y)*0x3ffffff));
1930 (void) WriteBlobLSBLong(image,(unsigned int)
1931 bmp_info.gamma_scale.x*0xffff);
1932 (void) WriteBlobLSBLong(image,(unsigned int)
1933 bmp_info.gamma_scale.y*0xffff);
1934 (void) WriteBlobLSBLong(image,(unsigned int)
1935 bmp_info.gamma_scale.z*0xffff);
1936 if ((image->rendering_intent != UndefinedIntent) ||
1937 (profile != (StringInfo *) NULL))
1942 switch ((int) image->rendering_intent)
1944 case SaturationIntent:
1946 intent=LCS_GM_BUSINESS;
1949 case RelativeIntent:
1951 intent=LCS_GM_GRAPHICS;
1954 case PerceptualIntent:
1956 intent=LCS_GM_IMAGES;
1959 case AbsoluteIntent:
1961 intent=LCS_GM_ABS_COLORIMETRIC;
1970 (void) WriteBlobLSBLong(image,(unsigned int) intent);
1971 (void) WriteBlobLSBLong(image,0x00); /* dummy profile data */
1972 (void) WriteBlobLSBLong(image,0x00); /* dummy profile length */
1973 (void) WriteBlobLSBLong(image,0x00); /* reserved */
1976 if (image->storage_class == PseudoClass)
1982 Dump colormap to file.
1984 if (image->debug != MagickFalse)
1985 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1986 " Colormap: %ld entries",image->colors);
1987 bmp_colormap=(unsigned char *) AcquireQuantumMemory((size_t) (1UL <<
1988 bmp_info.bits_per_pixel),4*sizeof(*bmp_colormap));
1989 if (bmp_colormap == (unsigned char *) NULL)
1990 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
1992 for (i=0; i < (long) MagickMin((long) image->colors,(long) bmp_info.number_colors); i++)
1994 *q++=ScaleQuantumToChar(image->colormap[i].blue);
1995 *q++=ScaleQuantumToChar(image->colormap[i].green);
1996 *q++=ScaleQuantumToChar(image->colormap[i].red);
1998 *q++=(unsigned char) 0x0;
2000 for ( ; i < (long) (1UL << bmp_info.bits_per_pixel); i++)
2002 *q++=(unsigned char) 0x00;
2003 *q++=(unsigned char) 0x00;
2004 *q++=(unsigned char) 0x00;
2006 *q++=(unsigned char) 0x00;
2009 (void) WriteBlob(image,(size_t) (3*(1L << bmp_info.bits_per_pixel)),
2012 (void) WriteBlob(image,(size_t) (4*(1L << bmp_info.bits_per_pixel)),
2014 bmp_colormap=(unsigned char *) RelinquishMagickMemory(bmp_colormap);
2016 if (image->debug != MagickFalse)
2017 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
2018 " Pixels: %u bytes",bmp_info.image_size);
2019 (void) WriteBlob(image,(size_t) bmp_info.image_size,pixels);
2020 pixels=(unsigned char *) RelinquishMagickMemory(pixels);
2021 if (GetNextImageInList(image) == (Image *) NULL)
2023 image=SyncNextImageInList(image);
2024 status=SetImageProgress(image,SaveImagesTag,scene++,
2025 GetImageListLength(image));
2026 if (status == MagickFalse)
2028 } while (image_info->adjoin != MagickFalse);
2029 (void) CloseBlob(image);