2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
13 % Read/Write Microsoft Windows Bitmap Image Format %
17 % Glenn Randers-Pehrson %
21 % Copyright 1999-2015 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 "MagickCore/studio.h"
44 #include "MagickCore/blob.h"
45 #include "MagickCore/blob-private.h"
46 #include "MagickCore/cache.h"
47 #include "MagickCore/colormap-private.h"
48 #include "MagickCore/color-private.h"
49 #include "MagickCore/colormap.h"
50 #include "MagickCore/colorspace.h"
51 #include "MagickCore/colorspace-private.h"
52 #include "MagickCore/exception.h"
53 #include "MagickCore/exception-private.h"
54 #include "MagickCore/image.h"
55 #include "MagickCore/image-private.h"
56 #include "MagickCore/list.h"
57 #include "MagickCore/log.h"
58 #include "MagickCore/magick.h"
59 #include "MagickCore/memory_.h"
60 #include "MagickCore/monitor.h"
61 #include "MagickCore/monitor-private.h"
62 #include "MagickCore/option.h"
63 #include "MagickCore/pixel-accessor.h"
64 #include "MagickCore/profile.h"
65 #include "MagickCore/quantum-private.h"
66 #include "MagickCore/static.h"
67 #include "MagickCore/string_.h"
68 #include "MagickCore/module.h"
69 #include "MagickCore/transform.h"
72 Macro definitions (from Windows wingdi.h).
78 #if !defined(MAGICKCORE_WINDOWS_SUPPORT) || defined(__MINGW32__) || defined(__MINGW64__)
86 #define BI_BITFIELDS 3
88 #undef LCS_CALIBRATED_RBG
89 #define LCS_CALIBRATED_RBG 0
92 #undef LCS_WINDOWS_COLOR_SPACE
93 #define LCS_WINDOWS_COLOR_SPACE 2
95 #define PROFILE_LINKED 3
96 #undef PROFILE_EMBEDDED
97 #define PROFILE_EMBEDDED 4
99 #undef LCS_GM_BUSINESS
100 #define LCS_GM_BUSINESS 1 /* Saturation */
101 #undef LCS_GM_GRAPHICS
102 #define LCS_GM_GRAPHICS 2 /* Relative */
104 #define LCS_GM_IMAGES 4 /* Perceptual */
105 #undef LCS_GM_ABS_COLORIMETRIC
106 #define LCS_GM_ABS_COLORIMETRIC 8 /* Absolute */
110 Typedef declarations.
112 typedef struct _BMPInfo
151 Forward declarations.
153 static MagickBooleanType
154 WriteBMPImage(const ImageInfo *,Image *,ExceptionInfo *);
157 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
161 % D e c o d e I m a g e %
165 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
167 % DecodeImage unpacks the packed image pixels into runlength-encoded
170 % The format of the DecodeImage method is:
172 % MagickBooleanType DecodeImage(Image *image,
173 % const size_t compression,unsigned char *pixels)
175 % A description of each parameter follows:
177 % o image: the address of a structure of type Image.
179 % o compression: Zero means uncompressed. A value of 1 means the
180 % compressed pixels are runlength encoded for a 256-color bitmap.
181 % A value of 2 means a 16-color bitmap. A value of 3 means bitfields
184 % o pixels: The address of a byte (8 bits) array of pixel data created by
185 % the decoding process.
188 static MagickBooleanType DecodeImage(Image *image,const size_t compression,
189 unsigned char *pixels)
198 register unsigned char
208 assert(image != (Image *) NULL);
209 assert(image->signature == MagickSignature);
210 if (image->debug != MagickFalse)
211 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
212 assert(pixels != (unsigned char *) NULL);
213 (void) ResetMagickMemory(pixels,0,(size_t) image->columns*image->rows*
218 q=pixels+(size_t) image->columns*image->rows;
219 for (y=0; y < (ssize_t) image->rows; )
224 if ((p < pixels) || (p > q))
226 count=ReadBlobByte(image);
234 count=(int) MagickMin((ssize_t) count,(ssize_t) (q-p));
235 byte=(unsigned char) ReadBlobByte(image);
236 if (compression == BI_RLE8)
238 for (i=0; i < (ssize_t) count; i++)
239 *p++=(unsigned char) byte;
243 for (i=0; i < (ssize_t) count; i++)
245 ((i & 0x01) != 0 ? (byte & 0x0f) : ((byte >> 4) & 0x0f));
254 count=ReadBlobByte(image);
268 p=pixels+y*image->columns;
276 x+=ReadBlobByte(image);
277 y+=ReadBlobByte(image);
278 p=pixels+y*image->columns+x;
286 count=(int) MagickMin((ssize_t) count,(ssize_t) (q-p));
287 if (compression == BI_RLE8)
288 for (i=0; i < (ssize_t) count; i++)
289 *p++=(unsigned char) ReadBlobByte(image);
291 for (i=0; i < (ssize_t) count; i++)
294 byte=(unsigned char) ReadBlobByte(image);
296 ((i & 0x01) != 0 ? (byte & 0x0f) : ((byte >> 4) & 0x0f));
302 if (compression == BI_RLE8)
304 if ((count & 0x01) != 0)
305 (void) ReadBlobByte(image);
308 if (((count & 0x03) == 1) || ((count & 0x03) == 2))
309 (void) ReadBlobByte(image);
314 status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y,
316 if (status == MagickFalse)
319 (void) ReadBlobByte(image); /* end of line */
320 (void) ReadBlobByte(image);
321 return(y < (ssize_t) image->rows ? MagickFalse : MagickTrue);
325 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
329 % E n c o d e I m a g e %
333 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
335 % EncodeImage compresses pixels using a runlength encoded format.
337 % The format of the EncodeImage method is:
339 % static MagickBooleanType EncodeImage(Image *image,
340 % const size_t bytes_per_line,const unsigned char *pixels,
341 % unsigned char *compressed_pixels)
343 % A description of each parameter follows:
345 % o image: The image.
347 % o bytes_per_line: the number of bytes in a scanline of compressed pixels
349 % o pixels: The address of a byte (8 bits) array of pixel data created by
350 % the compression process.
352 % o compressed_pixels: The address of a byte (8 bits) array of compressed
356 static size_t EncodeImage(Image *image,const size_t bytes_per_line,
357 const unsigned char *pixels,unsigned char *compressed_pixels)
362 register const unsigned char
369 register unsigned char
376 Runlength encode pixels.
378 assert(image != (Image *) NULL);
379 assert(image->signature == MagickSignature);
380 if (image->debug != MagickFalse)
381 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
382 assert(pixels != (const unsigned char *) NULL);
383 assert(compressed_pixels != (unsigned char *) NULL);
387 for (y=0; y < (ssize_t) image->rows; y++)
389 for (x=0; x < (ssize_t) bytes_per_line; x+=i)
394 for (i=1; ((x+i) < (ssize_t) bytes_per_line); i++)
395 if ((i == 255) || (*(p+i) != *p))
397 *q++=(unsigned char) i;
404 *q++=(unsigned char) 0x00;
405 *q++=(unsigned char) 0x00;
406 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
408 if (status == MagickFalse)
414 *q++=(unsigned char) 0x00;
415 *q++=(unsigned char) 0x01;
416 return((size_t) (q-compressed_pixels));
420 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
428 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
430 % IsBMP() returns MagickTrue if the image format type, identified by the
431 % magick string, is BMP.
433 % The format of the IsBMP method is:
435 % MagickBooleanType IsBMP(const unsigned char *magick,const size_t length)
437 % A description of each parameter follows:
439 % o magick: compare image format pattern against these bytes.
441 % o length: Specifies the length of the magick string.
444 static MagickBooleanType IsBMP(const unsigned char *magick,const size_t length)
448 if ((LocaleNCompare((char *) magick,"BA",2) == 0) ||
449 (LocaleNCompare((char *) magick,"BM",2) == 0) ||
450 (LocaleNCompare((char *) magick,"IC",2) == 0) ||
451 (LocaleNCompare((char *) magick,"PI",2) == 0) ||
452 (LocaleNCompare((char *) magick,"CI",2) == 0) ||
453 (LocaleNCompare((char *) magick,"CP",2) == 0))
459 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
463 % R e a d B M P I m a g e %
467 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
469 % ReadBMPImage() reads a Microsoft Windows bitmap image file, Version
470 % 2, 3 (for Windows or NT), or 4, and returns it. It allocates the memory
471 % necessary for the new Image structure and returns a pointer to the new
474 % The format of the ReadBMPImage method is:
476 % image=ReadBMPImage(image_info)
478 % A description of each parameter follows:
480 % o image_info: the image info.
482 % o exception: return any errors or warnings in this structure.
486 static Image *ReadBMPImage(const ImageInfo *image_info,ExceptionInfo *exception)
514 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,exception);
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);
558 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
572 Verify BMP identifier.
574 if (bmp_info.ba_offset == 0)
575 start_position=TellBlob(image)-2;
576 bmp_info.ba_offset=0;
577 while (LocaleNCompare((char *) magick,"BA",2) == 0)
579 bmp_info.file_size=ReadBlobLSBLong(image);
580 bmp_info.ba_offset=ReadBlobLSBLong(image);
581 bmp_info.offset_bits=ReadBlobLSBLong(image);
582 count=ReadBlob(image,2,magick);
586 if (image->debug != MagickFalse)
587 (void) LogMagickEvent(CoderEvent,GetMagickModule()," Magick: %c%c",
588 magick[0],magick[1]);
589 if ((count != 2) || ((LocaleNCompare((char *) magick,"BM",2) != 0) &&
590 (LocaleNCompare((char *) magick,"CI",2) != 0)))
591 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
592 bmp_info.file_size=ReadBlobLSBLong(image);
593 (void) ReadBlobLSBLong(image);
594 bmp_info.offset_bits=ReadBlobLSBLong(image);
595 bmp_info.size=ReadBlobLSBLong(image);
596 if (image->debug != MagickFalse)
597 (void) LogMagickEvent(CoderEvent,GetMagickModule()," BMP size: %u",
599 if (bmp_info.size == 12)
604 (void) CopyMagickString(image->magick,"BMP2",MagickPathExtent);
605 bmp_info.width=(ssize_t) ((short) ReadBlobLSBShort(image));
606 bmp_info.height=(ssize_t) ((short) ReadBlobLSBShort(image));
607 bmp_info.planes=ReadBlobLSBShort(image);
608 bmp_info.bits_per_pixel=ReadBlobLSBShort(image);
611 bmp_info.number_colors=0;
612 bmp_info.compression=BI_RGB;
613 bmp_info.image_size=0;
614 bmp_info.alpha_mask=0;
615 if (image->debug != MagickFalse)
617 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
618 " Format: OS/2 Bitmap");
619 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
620 " Geometry: %.20gx%.20g",(double) bmp_info.width,(double)
627 Microsoft Windows BMP image file.
629 if (bmp_info.size < 40)
630 ThrowReaderException(CorruptImageError,"NonOS2HeaderSizeError");
631 bmp_info.width=(ssize_t) ((int) ReadBlobLSBLong(image));
632 bmp_info.height=(ssize_t) ((int) ReadBlobLSBLong(image));
633 bmp_info.planes=ReadBlobLSBShort(image);
634 bmp_info.bits_per_pixel=ReadBlobLSBShort(image);
635 bmp_info.compression=ReadBlobLSBLong(image);
636 bmp_info.image_size=ReadBlobLSBLong(image);
637 bmp_info.x_pixels=ReadBlobLSBLong(image);
638 bmp_info.y_pixels=ReadBlobLSBLong(image);
639 bmp_info.number_colors=ReadBlobLSBLong(image);
640 bmp_info.colors_important=ReadBlobLSBLong(image);
643 if (image->debug != MagickFalse)
645 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
646 " Format: MS Windows bitmap");
647 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
648 " Geometry: %.20gx%.20g",(double) bmp_info.width,(double)
650 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
651 " Bits per pixel: %.20g",(double) 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)
709 Read color management information.
711 bmp_info.alpha_mask=ReadBlobLSBLong(image);
712 bmp_info.colorspace=(int) ReadBlobLSBLong(image);
714 Decode 2^30 fixed point formatted CIE primaries.
716 # define BMP_DENOM ((double) 0x40000000)
717 bmp_info.red_primary.x=(double) ReadBlobLSBLong(image)/BMP_DENOM;
718 bmp_info.red_primary.y=(double) ReadBlobLSBLong(image)/BMP_DENOM;
719 bmp_info.red_primary.z=(double) ReadBlobLSBLong(image)/BMP_DENOM;
720 bmp_info.green_primary.x=(double) ReadBlobLSBLong(image)/BMP_DENOM;
721 bmp_info.green_primary.y=(double) ReadBlobLSBLong(image)/BMP_DENOM;
722 bmp_info.green_primary.z=(double) ReadBlobLSBLong(image)/BMP_DENOM;
723 bmp_info.blue_primary.x=(double) ReadBlobLSBLong(image)/BMP_DENOM;
724 bmp_info.blue_primary.y=(double) ReadBlobLSBLong(image)/BMP_DENOM;
725 bmp_info.blue_primary.z=(double) ReadBlobLSBLong(image)/BMP_DENOM;
727 sum=bmp_info.red_primary.x+bmp_info.red_primary.y+
728 bmp_info.red_primary.z;
729 bmp_info.red_primary.x/=sum;
730 bmp_info.red_primary.y/=sum;
731 image->chromaticity.red_primary.x=bmp_info.red_primary.x;
732 image->chromaticity.red_primary.y=bmp_info.red_primary.y;
734 sum=bmp_info.green_primary.x+bmp_info.green_primary.y+
735 bmp_info.green_primary.z;
736 bmp_info.green_primary.x/=sum;
737 bmp_info.green_primary.y/=sum;
738 image->chromaticity.green_primary.x=bmp_info.green_primary.x;
739 image->chromaticity.green_primary.y=bmp_info.green_primary.y;
741 sum=bmp_info.blue_primary.x+bmp_info.blue_primary.y+
742 bmp_info.blue_primary.z;
743 bmp_info.blue_primary.x/=sum;
744 bmp_info.blue_primary.y/=sum;
745 image->chromaticity.blue_primary.x=bmp_info.blue_primary.x;
746 image->chromaticity.blue_primary.y=bmp_info.blue_primary.y;
749 Decode 16^16 fixed point formatted gamma_scales.
751 bmp_info.gamma_scale.x=(double) ReadBlobLSBLong(image)/0x10000;
752 bmp_info.gamma_scale.y=(double) ReadBlobLSBLong(image)/0x10000;
753 bmp_info.gamma_scale.z=(double) ReadBlobLSBLong(image)/0x10000;
755 Compute a single gamma from the BMP 3-channel gamma.
757 image->gamma=(bmp_info.gamma_scale.x+bmp_info.gamma_scale.y+
758 bmp_info.gamma_scale.z)/3.0;
761 (void) CopyMagickString(image->magick,"BMP3",MagickPathExtent);
763 if (bmp_info.size > 108)
769 Read BMP Version 5 color management information.
771 intent=ReadBlobLSBLong(image);
772 switch ((int) intent)
774 case LCS_GM_BUSINESS:
776 image->rendering_intent=SaturationIntent;
779 case LCS_GM_GRAPHICS:
781 image->rendering_intent=RelativeIntent;
786 image->rendering_intent=PerceptualIntent;
789 case LCS_GM_ABS_COLORIMETRIC:
791 image->rendering_intent=AbsoluteIntent;
795 profile_data=ReadBlobLSBLong(image);
796 profile_size=ReadBlobLSBLong(image);
799 (void) ReadBlobLSBLong(image); /* Reserved byte */
802 if ((MagickSizeType) bmp_info.file_size > GetBlobSize(image))
803 (void) ThrowMagickException(exception,GetMagickModule(),CorruptImageError,
804 "LengthAndFilesizeDoNotMatch","`%s'",image->filename);
806 if ((MagickSizeType) bmp_info.file_size < GetBlobSize(image))
807 (void) ThrowMagickException(exception,GetMagickModule(),
808 CorruptImageWarning,"LengthAndFilesizeDoNotMatch","`%s'",
810 if (bmp_info.width <= 0)
811 ThrowReaderException(CorruptImageError,"NegativeOrZeroImageSize");
812 if (bmp_info.height == 0)
813 ThrowReaderException(CorruptImageError,"NegativeOrZeroImageSize");
814 if (bmp_info.planes != 1)
815 ThrowReaderException(CorruptImageError,"StaticPlanesValueNotEqualToOne");
816 if ((bmp_info.bits_per_pixel != 1) && (bmp_info.bits_per_pixel != 4) &&
817 (bmp_info.bits_per_pixel != 8) && (bmp_info.bits_per_pixel != 16) &&
818 (bmp_info.bits_per_pixel != 24) && (bmp_info.bits_per_pixel != 32))
819 ThrowReaderException(CorruptImageError,"UnrecognizedBitsPerPixel");
820 if (bmp_info.bits_per_pixel < 16 &&
821 bmp_info.number_colors > (1U << bmp_info.bits_per_pixel))
822 ThrowReaderException(CorruptImageError,"UnrecognizedNumberOfColors");
823 if ((bmp_info.compression == 1) && (bmp_info.bits_per_pixel != 8))
824 ThrowReaderException(CorruptImageError,"UnrecognizedBitsPerPixel");
825 if ((bmp_info.compression == 2) && (bmp_info.bits_per_pixel != 4))
826 ThrowReaderException(CorruptImageError,"UnrecognizedBitsPerPixel");
827 if ((bmp_info.compression == 3) && (bmp_info.bits_per_pixel < 16))
828 ThrowReaderException(CorruptImageError,"UnrecognizedBitsPerPixel");
829 switch (bmp_info.compression)
837 ThrowReaderException(CoderError,"JPEGCompressNotSupported");
839 ThrowReaderException(CoderError,"PNGCompressNotSupported");
841 ThrowReaderException(CorruptImageError,"UnrecognizedImageCompression");
843 image->columns=(size_t) MagickAbsoluteValue(bmp_info.width);
844 image->rows=(size_t) MagickAbsoluteValue(bmp_info.height);
845 image->depth=bmp_info.bits_per_pixel <= 8 ? bmp_info.bits_per_pixel : 8;
846 image->alpha_trait=((bmp_info.alpha_mask != 0) &&
847 (bmp_info.compression == BI_BITFIELDS)) ? BlendPixelTrait :
849 if (bmp_info.bits_per_pixel < 16)
854 image->storage_class=PseudoClass;
855 image->colors=bmp_info.number_colors;
857 if (image->colors == 0)
858 image->colors=one << bmp_info.bits_per_pixel;
860 if (image->storage_class == PseudoClass)
869 Read BMP raster colormap.
871 if (image->debug != MagickFalse)
872 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
873 " Reading colormap of %.20g colors",(double) image->colors);
874 if (AcquireImageColormap(image,image->colors,exception) == MagickFalse)
875 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
876 bmp_colormap=(unsigned char *) AcquireQuantumMemory((size_t)
877 image->colors,4*sizeof(*bmp_colormap));
878 if (bmp_colormap == (unsigned char *) NULL)
879 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
880 if ((bmp_info.size == 12) || (bmp_info.size == 64))
884 offset=SeekBlob(image,start_position+14+bmp_info.size,SEEK_SET);
886 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
887 count=ReadBlob(image,packet_size*image->colors,bmp_colormap);
888 if (count != (ssize_t) (packet_size*image->colors))
889 ThrowReaderException(CorruptImageError,"InsufficientImageDataInFile");
891 for (i=0; i < (ssize_t) image->colors; i++)
893 image->colormap[i].blue=(MagickRealType) ScaleCharToQuantum(*p++);
894 image->colormap[i].green=(MagickRealType) ScaleCharToQuantum(*p++);
895 image->colormap[i].red=(MagickRealType) ScaleCharToQuantum(*p++);
896 if (packet_size == 4)
899 bmp_colormap=(unsigned char *) RelinquishMagickMemory(bmp_colormap);
901 image->resolution.x=(double) bmp_info.x_pixels/100.0;
902 image->resolution.y=(double) bmp_info.y_pixels/100.0;
903 image->units=PixelsPerCentimeterResolution;
904 if ((image_info->ping != MagickFalse) && (image_info->number_scenes != 0))
905 if (image->scene >= (image_info->scene+image_info->number_scenes-1))
907 status=SetImageExtent(image,image->columns,image->rows,exception);
908 if (status == MagickFalse)
909 return(DestroyImageList(image));
913 offset=SeekBlob(image,start_position+bmp_info.offset_bits,SEEK_SET);
915 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
916 if (bmp_info.compression == BI_RLE4)
917 bmp_info.bits_per_pixel<<=1;
918 bytes_per_line=4*((image->columns*bmp_info.bits_per_pixel+31)/32);
919 length=(size_t) bytes_per_line*image->rows;
920 pixel_info=AcquireVirtualMemory((size_t) image->rows,
921 MagickMax(bytes_per_line,image->columns+256UL)*sizeof(*pixels));
922 if (pixel_info == (MemoryInfo *) NULL)
923 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
924 pixels=(unsigned char *) GetVirtualMemoryBlob(pixel_info);
925 if ((bmp_info.compression == BI_RGB) ||
926 (bmp_info.compression == BI_BITFIELDS))
928 if (image->debug != MagickFalse)
929 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
930 " Reading pixels (%.20g bytes)",(double) length);
931 count=ReadBlob(image,length,pixels);
932 if (count != (ssize_t) length)
934 pixel_info=RelinquishVirtualMemory(pixel_info);
935 ThrowReaderException(CorruptImageError,
936 "InsufficientImageDataInFile");
942 Convert run-length encoded raster pixels.
944 status=DecodeImage(image,bmp_info.compression,pixels);
945 if (status == MagickFalse)
947 pixel_info=RelinquishVirtualMemory(pixel_info);
948 ThrowReaderException(CorruptImageError,
949 "UnableToRunlengthDecodeImage");
953 Convert BMP raster image to pixel packets.
955 if (bmp_info.compression == BI_RGB)
958 We should ignore the alpha value in BMP3 files but there have been
959 reports about 32 bit files with alpha. We do a quick check to see if
960 the alpha channel contains a value that is not zero (default value).
961 If we find a non zero value we asume the program that wrote the file
962 wants to use the alpha channel.
964 if ((image->alpha_trait == UndefinedPixelTrait) && (bmp_info.size == 40) &&
965 (bmp_info.bits_per_pixel == 32))
967 bytes_per_line=4*(image->columns);
968 for (y=(ssize_t) image->rows-1; y >= 0; y--)
970 p=pixels+(image->rows-y-1)*bytes_per_line;
971 for (x=0; x < (ssize_t) image->columns; x++)
975 image->alpha_trait=BlendPixelTrait;
983 bmp_info.alpha_mask=image->alpha_trait != UndefinedPixelTrait ?
985 bmp_info.red_mask=0x00ff0000U;
986 bmp_info.green_mask=0x0000ff00U;
987 bmp_info.blue_mask=0x000000ffU;
988 if (bmp_info.bits_per_pixel == 16)
993 bmp_info.red_mask=0x00007c00U;
994 bmp_info.green_mask=0x000003e0U;
995 bmp_info.blue_mask=0x0000001fU;
998 (void) ResetMagickMemory(&shift,0,sizeof(shift));
999 (void) ResetMagickMemory(&quantum_bits,0,sizeof(quantum_bits));
1000 if ((bmp_info.bits_per_pixel == 16) || (bmp_info.bits_per_pixel == 32))
1006 Get shift and quantum bits info from bitfield masks.
1008 if (bmp_info.red_mask != 0)
1009 while (((bmp_info.red_mask << shift.red) & 0x80000000UL) == 0)
1011 if (bmp_info.green_mask != 0)
1012 while (((bmp_info.green_mask << shift.green) & 0x80000000UL) == 0)
1014 if (bmp_info.blue_mask != 0)
1015 while (((bmp_info.blue_mask << shift.blue) & 0x80000000UL) == 0)
1017 if (bmp_info.alpha_mask != 0)
1018 while (((bmp_info.alpha_mask << shift.alpha) & 0x80000000UL) == 0)
1021 while (((bmp_info.red_mask << sample) & 0x80000000UL) != 0)
1023 quantum_bits.red=(MagickRealType) (sample-shift.red);
1025 while (((bmp_info.green_mask << sample) & 0x80000000UL) != 0)
1027 quantum_bits.green=(MagickRealType) (sample-shift.green);
1029 while (((bmp_info.blue_mask << sample) & 0x80000000UL) != 0)
1031 quantum_bits.blue=(MagickRealType) (sample-shift.blue);
1033 while (((bmp_info.alpha_mask << sample) & 0x80000000UL) != 0)
1035 quantum_bits.alpha=(MagickRealType) (sample-shift.alpha);
1037 switch (bmp_info.bits_per_pixel)
1042 Convert bitmap scanline.
1044 for (y=(ssize_t) image->rows-1; y >= 0; y--)
1046 p=pixels+(image->rows-y-1)*bytes_per_line;
1047 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
1048 if (q == (Quantum *) NULL)
1050 for (x=0; x < ((ssize_t) image->columns-7); x+=8)
1052 for (bit=0; bit < 8; bit++)
1054 index=(Quantum) (((*p) & (0x80 >> bit)) != 0 ? 0x01 : 0x00);
1055 SetPixelIndex(image,index,q);
1056 q+=GetPixelChannels(image);
1060 if ((image->columns % 8) != 0)
1062 for (bit=0; bit < (image->columns % 8); bit++)
1064 index=(Quantum) (((*p) & (0x80 >> bit)) != 0 ? 0x01 : 0x00);
1065 SetPixelIndex(image,index,q);
1066 q+=GetPixelChannels(image);
1070 if (SyncAuthenticPixels(image,exception) == MagickFalse)
1072 if (image->previous == (Image *) NULL)
1074 status=SetImageProgress(image,LoadImageTag,(MagickOffsetType)
1075 (image->rows-y),image->rows);
1076 if (status == MagickFalse)
1080 (void) SyncImage(image,exception);
1086 Convert PseudoColor scanline.
1088 for (y=(ssize_t) image->rows-1; y >= 0; y--)
1090 p=pixels+(image->rows-y-1)*bytes_per_line;
1091 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
1092 if (q == (Quantum *) NULL)
1094 for (x=0; x < ((ssize_t) image->columns-1); x+=2)
1096 if (IsValidColormapIndex(image,(*p >> 4) & 0x0f,&index,exception)
1099 SetPixelIndex(image,index,q);
1100 q+=GetPixelChannels(image);
1101 if (IsValidColormapIndex(image,*p & 0x0f,&index,exception) ==
1104 SetPixelIndex(image,index,q);
1105 q+=GetPixelChannels(image);
1108 if ((image->columns % 2) != 0)
1110 if (IsValidColormapIndex(image,(*p >> 4) & 0xf,&index,exception)
1113 SetPixelIndex(image,index,q);
1114 q+=GetPixelChannels(image);
1117 if (x < image->columns)
1119 if (SyncAuthenticPixels(image,exception) == MagickFalse)
1121 if (image->previous == (Image *) NULL)
1123 status=SetImageProgress(image,LoadImageTag,(MagickOffsetType)
1124 (image->rows-y),image->rows);
1125 if (status == MagickFalse)
1129 (void) SyncImage(image,exception);
1135 Convert PseudoColor scanline.
1137 if ((bmp_info.compression == BI_RLE8) ||
1138 (bmp_info.compression == BI_RLE4))
1139 bytes_per_line=image->columns;
1140 for (y=(ssize_t) image->rows-1; y >= 0; y--)
1142 p=pixels+(image->rows-y-1)*bytes_per_line;
1143 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
1144 if (q == (Quantum *) NULL)
1146 for (x=(ssize_t) image->columns; x != 0; --x)
1148 if (IsValidColormapIndex(image,*p++,&index,exception) ==
1151 SetPixelIndex(image,index,q);
1152 q+=GetPixelChannels(image);
1156 if (SyncAuthenticPixels(image,exception) == MagickFalse)
1158 offset=(MagickOffsetType) (image->rows-y-1);
1159 if (image->previous == (Image *) NULL)
1161 status=SetImageProgress(image,LoadImageTag,(MagickOffsetType)
1162 (image->rows-y),image->rows);
1163 if (status == MagickFalse)
1167 (void) SyncImage(image,exception);
1177 Convert bitfield encoded 16-bit PseudoColor scanline.
1179 if (bmp_info.compression != BI_RGB &&
1180 bmp_info.compression != BI_BITFIELDS)
1182 pixel_info=RelinquishVirtualMemory(pixel_info);
1183 ThrowReaderException(CorruptImageError,
1184 "UnrecognizedImageCompression");
1186 bytes_per_line=2*(image->columns+image->columns % 2);
1187 image->storage_class=DirectClass;
1188 for (y=(ssize_t) image->rows-1; y >= 0; y--)
1190 p=pixels+(image->rows-y-1)*bytes_per_line;
1191 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
1192 if (q == (Quantum *) NULL)
1194 for (x=0; x < (ssize_t) image->columns; x++)
1196 pixel=(size_t) (*p++);
1198 red=((pixel & bmp_info.red_mask) << shift.red) >> 16;
1199 if (quantum_bits.red == 5)
1200 red|=((red & 0xe000) >> 5);
1201 if (quantum_bits.red <= 8)
1202 red|=((red & 0xff00) >> 8);
1203 green=((pixel & bmp_info.green_mask) << shift.green) >> 16;
1204 if (quantum_bits.green == 5)
1205 green|=((green & 0xe000) >> 5);
1206 if (quantum_bits.green == 6)
1207 green|=((green & 0xc000) >> 6);
1208 if (quantum_bits.green <= 8)
1209 green|=((green & 0xff00) >> 8);
1210 blue=((pixel & bmp_info.blue_mask) << shift.blue) >> 16;
1211 if (quantum_bits.blue == 5)
1212 blue|=((blue & 0xe000) >> 5);
1213 if (quantum_bits.blue <= 8)
1214 blue|=((blue & 0xff00) >> 8);
1215 SetPixelRed(image,ScaleShortToQuantum((unsigned short) red),q);
1216 SetPixelGreen(image,ScaleShortToQuantum((unsigned short) green),q);
1217 SetPixelBlue(image,ScaleShortToQuantum((unsigned short) blue),q);
1218 SetPixelAlpha(image,OpaqueAlpha,q);
1219 if (image->alpha_trait != UndefinedPixelTrait)
1221 alpha=((pixel & bmp_info.alpha_mask) << shift.alpha) >> 16;
1222 if (quantum_bits.alpha <= 8)
1223 alpha|=((alpha & 0xff00) >> 8);
1224 SetPixelAlpha(image,ScaleShortToQuantum(
1225 (unsigned short) alpha),q);
1227 q+=GetPixelChannels(image);
1229 if (SyncAuthenticPixels(image,exception) == MagickFalse)
1231 offset=(MagickOffsetType) (image->rows-y-1);
1232 if (image->previous == (Image *) NULL)
1234 status=SetImageProgress(image,LoadImageTag,(MagickOffsetType)
1235 (image->rows-y),image->rows);
1236 if (status == MagickFalse)
1245 Convert DirectColor scanline.
1247 bytes_per_line=4*((image->columns*24+31)/32);
1248 for (y=(ssize_t) image->rows-1; y >= 0; y--)
1250 p=pixels+(image->rows-y-1)*bytes_per_line;
1251 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
1252 if (q == (Quantum *) NULL)
1254 for (x=0; x < (ssize_t) image->columns; x++)
1256 SetPixelBlue(image,ScaleCharToQuantum(*p++),q);
1257 SetPixelGreen(image,ScaleCharToQuantum(*p++),q);
1258 SetPixelRed(image,ScaleCharToQuantum(*p++),q);
1259 SetPixelAlpha(image,OpaqueAlpha,q);
1260 q+=GetPixelChannels(image);
1262 if (SyncAuthenticPixels(image,exception) == MagickFalse)
1264 offset=(MagickOffsetType) (image->rows-y-1);
1265 if (image->previous == (Image *) NULL)
1267 status=SetImageProgress(image,LoadImageTag,(MagickOffsetType)
1268 (image->rows-y),image->rows);
1269 if (status == MagickFalse)
1278 Convert bitfield encoded DirectColor scanline.
1280 if ((bmp_info.compression != BI_RGB) &&
1281 (bmp_info.compression != BI_BITFIELDS))
1283 pixel_info=RelinquishVirtualMemory(pixel_info);
1284 ThrowReaderException(CorruptImageError,
1285 "UnrecognizedImageCompression");
1287 bytes_per_line=4*(image->columns);
1288 for (y=(ssize_t) image->rows-1; y >= 0; y--)
1294 p=pixels+(image->rows-y-1)*bytes_per_line;
1295 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
1296 if (q == (Quantum *) NULL)
1298 for (x=0; x < (ssize_t) image->columns; x++)
1300 pixel=(size_t) (*p++);
1301 pixel|=((size_t) *p++ << 8);
1302 pixel|=((size_t) *p++ << 16);
1303 pixel|=((size_t) *p++ << 24);
1304 red=((pixel & bmp_info.red_mask) << shift.red) >> 16;
1305 if (quantum_bits.red == 8)
1307 green=((pixel & bmp_info.green_mask) << shift.green) >> 16;
1308 if (quantum_bits.green == 8)
1309 green|=(green >> 8);
1310 blue=((pixel & bmp_info.blue_mask) << shift.blue) >> 16;
1311 if (quantum_bits.blue == 8)
1313 SetPixelRed(image,ScaleShortToQuantum((unsigned short) red),q);
1314 SetPixelGreen(image,ScaleShortToQuantum((unsigned short) green),q);
1315 SetPixelBlue(image,ScaleShortToQuantum((unsigned short) blue),q);
1316 SetPixelAlpha(image,OpaqueAlpha,q);
1317 if (image->alpha_trait != UndefinedPixelTrait)
1319 alpha=((pixel & bmp_info.alpha_mask) << shift.alpha) >> 16;
1320 if (quantum_bits.alpha == 8)
1321 alpha|=(alpha >> 8);
1322 SetPixelAlpha(image,ScaleShortToQuantum(
1323 (unsigned short) alpha),q);
1325 q+=GetPixelChannels(image);
1327 if (SyncAuthenticPixels(image,exception) == MagickFalse)
1329 offset=(MagickOffsetType) (image->rows-y-1);
1330 if (image->previous == (Image *) NULL)
1332 status=SetImageProgress(image,LoadImageTag,(MagickOffsetType)
1333 (image->rows-y),image->rows);
1334 if (status == MagickFalse)
1342 pixel_info=RelinquishVirtualMemory(pixel_info);
1343 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
1346 pixel_info=RelinquishVirtualMemory(pixel_info);
1349 if (EOFBlob(image) != MagickFalse)
1351 ThrowFileException(exception,CorruptImageError,"UnexpectedEndOfFile",
1355 if (bmp_info.height < 0)
1361 Correct image orientation.
1363 flipped_image=FlipImage(image,exception);
1364 if (flipped_image != (Image *) NULL)
1366 DuplicateBlob(flipped_image,image);
1367 image=DestroyImage(image);
1368 image=flipped_image;
1372 Proceed to next image.
1374 if (image_info->number_scenes != 0)
1375 if (image->scene >= (image_info->scene+image_info->number_scenes-1))
1378 if (bmp_info.ba_offset != 0)
1380 offset=SeekBlob(image,(MagickOffsetType) bmp_info.ba_offset,SEEK_SET);
1382 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
1384 count=ReadBlob(image,2,magick);
1385 if ((count == 2) && (IsBMP(magick,2) != MagickFalse))
1388 Acquire next image structure.
1390 AcquireNextImage(image_info,image,exception);
1391 if (GetNextImageInList(image) == (Image *) NULL)
1393 image=DestroyImageList(image);
1394 return((Image *) NULL);
1396 image=SyncNextImageInList(image);
1397 status=SetImageProgress(image,LoadImagesTag,TellBlob(image),
1398 GetBlobSize(image));
1399 if (status == MagickFalse)
1402 } while (IsBMP(magick,2) != MagickFalse);
1403 (void) CloseBlob(image);
1404 return(GetFirstImageInList(image));
1408 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1412 % R e g i s t e r B M P I m a g e %
1416 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1418 % RegisterBMPImage() adds attributes for the BMP image format to
1419 % the list of supported formats. The attributes include the image format
1420 % tag, a method to read and/or write the format, whether the format
1421 % supports the saving of more than one frame to the same file or blob,
1422 % whether the format supports native in-memory I/O, and a brief
1423 % description of the format.
1425 % The format of the RegisterBMPImage method is:
1427 % size_t RegisterBMPImage(void)
1430 ModuleExport size_t RegisterBMPImage(void)
1435 entry=AcquireMagickInfo("BMP","BMP","Microsoft Windows bitmap image");
1436 entry->decoder=(DecodeImageHandler *) ReadBMPImage;
1437 entry->encoder=(EncodeImageHandler *) WriteBMPImage;
1438 entry->magick=(IsImageFormatHandler *) IsBMP;
1439 entry->flags^=CoderAdjoinFlag;
1440 entry->flags|=CoderSeekableStreamFlag;
1441 (void) RegisterMagickInfo(entry);
1442 entry=AcquireMagickInfo("BMP","BMP2","Microsoft Windows bitmap image (V2)");
1443 entry->encoder=(EncodeImageHandler *) WriteBMPImage;
1444 entry->magick=(IsImageFormatHandler *) IsBMP;
1445 entry->flags^=CoderAdjoinFlag;
1446 entry->flags|=CoderSeekableStreamFlag;
1447 (void) RegisterMagickInfo(entry);
1448 entry=AcquireMagickInfo("BMP","BMP3","Microsoft Windows bitmap image (V3)");
1449 entry->encoder=(EncodeImageHandler *) WriteBMPImage;
1450 entry->magick=(IsImageFormatHandler *) IsBMP;
1451 entry->flags^=CoderAdjoinFlag;
1452 entry->flags|=CoderSeekableStreamFlag;
1453 (void) RegisterMagickInfo(entry);
1454 return(MagickImageCoderSignature);
1458 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1462 % U n r e g i s t e r B M P I m a g e %
1466 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1468 % UnregisterBMPImage() removes format registrations made by the
1469 % BMP module from the list of supported formats.
1471 % The format of the UnregisterBMPImage method is:
1473 % UnregisterBMPImage(void)
1476 ModuleExport void UnregisterBMPImage(void)
1478 (void) UnregisterMagickInfo("BMP");
1479 (void) UnregisterMagickInfo("BMP2");
1480 (void) UnregisterMagickInfo("BMP3");
1484 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1488 % W r i t e B M P I m a g e %
1492 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1494 % WriteBMPImage() writes an image in Microsoft Windows bitmap encoded
1495 % image format, version 3 for Windows or (if the image has a matte channel)
1498 % The format of the WriteBMPImage method is:
1500 % MagickBooleanType WriteBMPImage(const ImageInfo *image_info,
1501 % Image *image,ExceptionInfo *exception)
1503 % A description of each parameter follows.
1505 % o image_info: the image info.
1507 % o image: The image.
1509 % o exception: return any errors or warnings in this structure.
1512 static MagickBooleanType WriteBMPImage(const ImageInfo *image_info,Image *image,
1513 ExceptionInfo *exception)
1534 register const Quantum
1541 register unsigned char
1556 Open output image file.
1558 assert(image_info != (const ImageInfo *) NULL);
1559 assert(image_info->signature == MagickSignature);
1560 assert(image != (Image *) NULL);
1561 assert(image->signature == MagickSignature);
1562 if (image->debug != MagickFalse)
1563 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1564 assert(exception != (ExceptionInfo *) NULL);
1565 assert(exception->signature == MagickSignature);
1566 status=OpenBlob(image_info,image,WriteBinaryBlobMode,exception);
1567 if (status == MagickFalse)
1570 if (LocaleCompare(image_info->magick,"BMP2") == 0)
1573 if (LocaleCompare(image_info->magick,"BMP3") == 0)
1576 value=GetImageOption(image_info,"bmp:format");
1578 if (value != (char *) NULL)
1580 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1581 " Format=%s",value);
1583 if (LocaleCompare(value,"bmp2") == 0)
1585 if (LocaleCompare(value,"bmp3") == 0)
1587 if (LocaleCompare(value,"bmp4") == 0)
1595 Initialize BMP raster file header.
1597 (void) TransformImageColorspace(image,sRGBColorspace,exception);
1598 (void) ResetMagickMemory(&bmp_info,0,sizeof(bmp_info));
1599 bmp_info.file_size=14+12;
1601 bmp_info.file_size+=28;
1602 bmp_info.offset_bits=bmp_info.file_size;
1603 bmp_info.compression=BI_RGB;
1604 if ((image->storage_class == PseudoClass) && (image->colors > 256))
1605 (void) SetImageStorageClass(image,DirectClass,exception);
1606 if (image->storage_class != DirectClass)
1609 Colormapped BMP raster.
1611 bmp_info.bits_per_pixel=8;
1612 if (image->colors <= 2)
1613 bmp_info.bits_per_pixel=1;
1615 if (image->colors <= 16)
1616 bmp_info.bits_per_pixel=4;
1618 if (image->colors <= 256)
1619 bmp_info.bits_per_pixel=8;
1620 if (image_info->compression == RLECompression)
1621 bmp_info.bits_per_pixel=8;
1622 bmp_info.number_colors=1U << bmp_info.bits_per_pixel;
1623 if (image->alpha_trait != UndefinedPixelTrait)
1624 (void) SetImageStorageClass(image,DirectClass,exception);
1626 if ((size_t) bmp_info.number_colors < image->colors)
1627 (void) SetImageStorageClass(image,DirectClass,exception);
1630 bmp_info.file_size+=3*(1UL << bmp_info.bits_per_pixel);
1631 bmp_info.offset_bits+=3*(1UL << bmp_info.bits_per_pixel);
1634 bmp_info.file_size+=(1UL << bmp_info.bits_per_pixel);
1635 bmp_info.offset_bits+=(1UL << bmp_info.bits_per_pixel);
1639 if (image->storage_class == DirectClass)
1642 Full color BMP raster.
1644 bmp_info.number_colors=0;
1645 bmp_info.bits_per_pixel=(unsigned short)
1646 ((type > 3) && (image->alpha_trait != UndefinedPixelTrait) ? 32 : 24);
1647 bmp_info.compression=(unsigned int) ((type > 3) &&
1648 (image->alpha_trait != UndefinedPixelTrait) ? BI_BITFIELDS : BI_RGB);
1650 bytes_per_line=4*((image->columns*bmp_info.bits_per_pixel+31)/32);
1651 bmp_info.ba_offset=0;
1652 profile=GetImageProfile(image,"icc");
1653 have_color_info=(image->rendering_intent != UndefinedIntent) ||
1654 (profile != (StringInfo *) NULL) || (image->gamma != 0.0) ? MagickTrue :
1659 if ((type == 3) || ((image->alpha_trait == UndefinedPixelTrait) &&
1660 (have_color_info == MagickFalse)))
1672 if ((image->rendering_intent != UndefinedIntent) ||
1673 (profile != (StringInfo *) NULL))
1678 bmp_info.file_size+=extra_size;
1679 bmp_info.offset_bits+=extra_size;
1681 bmp_info.width=(ssize_t) image->columns;
1682 bmp_info.height=(ssize_t) image->rows;
1684 bmp_info.image_size=(unsigned int) (bytes_per_line*image->rows);
1685 bmp_info.file_size+=bmp_info.image_size;
1686 bmp_info.x_pixels=75*39;
1687 bmp_info.y_pixels=75*39;
1688 switch (image->units)
1690 case UndefinedResolution:
1691 case PixelsPerInchResolution:
1693 bmp_info.x_pixels=(unsigned int) (100.0*image->resolution.x/2.54);
1694 bmp_info.y_pixels=(unsigned int) (100.0*image->resolution.y/2.54);
1697 case PixelsPerCentimeterResolution:
1699 bmp_info.x_pixels=(unsigned int) (100.0*image->resolution.x);
1700 bmp_info.y_pixels=(unsigned int) (100.0*image->resolution.y);
1704 bmp_info.colors_important=bmp_info.number_colors;
1706 Convert MIFF to BMP raster pixels.
1708 pixel_info=AcquireVirtualMemory((size_t) bmp_info.image_size,
1710 if (pixel_info == (MemoryInfo *) NULL)
1711 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
1712 pixels=(unsigned char *) GetVirtualMemoryBlob(pixel_info);
1713 (void) ResetMagickMemory(pixels,0,(size_t) bmp_info.image_size);
1714 switch (bmp_info.bits_per_pixel)
1723 Convert PseudoClass image to a BMP monochrome image.
1725 for (y=0; y < (ssize_t) image->rows; y++)
1730 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
1731 if (p == (const Quantum *) NULL)
1733 q=pixels+(image->rows-y-1)*bytes_per_line;
1736 for (x=0; x < (ssize_t) image->columns; x++)
1739 byte|=GetPixelIndex(image,p) != 0 ? 0x01 : 0x00;
1743 *q++=(unsigned char) byte;
1747 p+=GetPixelChannels(image);
1751 *q++=(unsigned char) (byte << (8-bit));
1754 offset=(ssize_t) (image->columns+7)/8;
1755 for (x=offset; x < (ssize_t) bytes_per_line; x++)
1757 if (image->previous == (Image *) NULL)
1759 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
1761 if (status == MagickFalse)
1777 Convert PseudoClass image to a BMP monochrome image.
1779 for (y=0; y < (ssize_t) image->rows; y++)
1781 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
1782 if (p == (const Quantum *) NULL)
1784 q=pixels+(image->rows-y-1)*bytes_per_line;
1787 for (x=0; x < (ssize_t) image->columns; x++)
1790 byte|=((size_t) GetPixelIndex(image,p) & 0x0f);
1794 *q++=(unsigned char) byte;
1798 p+=GetPixelChannels(image);
1802 *q++=(unsigned char) (byte << 4);
1805 offset=(ssize_t) (image->columns+1)/2;
1806 for (x=offset; x < (ssize_t) bytes_per_line; x++)
1808 if (image->previous == (Image *) NULL)
1810 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
1812 if (status == MagickFalse)
1821 Convert PseudoClass packet to BMP pixel.
1823 for (y=0; y < (ssize_t) image->rows; y++)
1825 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
1826 if (p == (const Quantum *) NULL)
1828 q=pixels+(image->rows-y-1)*bytes_per_line;
1829 for (x=0; x < (ssize_t) image->columns; x++)
1831 *q++=(unsigned char) GetPixelIndex(image,p);
1832 p+=GetPixelChannels(image);
1834 for ( ; x < (ssize_t) bytes_per_line; x++)
1836 if (image->previous == (Image *) NULL)
1838 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
1840 if (status == MagickFalse)
1849 Convert DirectClass packet to BMP BGR888.
1851 for (y=0; y < (ssize_t) image->rows; y++)
1853 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
1854 if (p == (const Quantum *) NULL)
1856 q=pixels+(image->rows-y-1)*bytes_per_line;
1857 for (x=0; x < (ssize_t) image->columns; x++)
1859 *q++=ScaleQuantumToChar(GetPixelBlue(image,p));
1860 *q++=ScaleQuantumToChar(GetPixelGreen(image,p));
1861 *q++=ScaleQuantumToChar(GetPixelRed(image,p));
1862 p+=GetPixelChannels(image);
1864 for (x=3L*(ssize_t) image->columns; x < (ssize_t) bytes_per_line; x++)
1866 if (image->previous == (Image *) NULL)
1868 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
1870 if (status == MagickFalse)
1879 Convert DirectClass packet to ARGB8888 pixel.
1881 for (y=0; y < (ssize_t) image->rows; y++)
1883 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
1884 if (p == (const Quantum *) NULL)
1886 q=pixels+(image->rows-y-1)*bytes_per_line;
1887 for (x=0; x < (ssize_t) image->columns; x++)
1889 *q++=ScaleQuantumToChar(GetPixelBlue(image,p));
1890 *q++=ScaleQuantumToChar(GetPixelGreen(image,p));
1891 *q++=ScaleQuantumToChar(GetPixelRed(image,p));
1892 *q++=ScaleQuantumToChar(GetPixelAlpha(image,p));
1893 p+=GetPixelChannels(image);
1895 if (image->previous == (Image *) NULL)
1897 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
1899 if (status == MagickFalse)
1906 if ((type > 2) && (bmp_info.bits_per_pixel == 8))
1907 if (image_info->compression != NoCompression)
1913 Convert run-length encoded raster pixels.
1915 rle_info=AcquireVirtualMemory((size_t) (2*(bytes_per_line+2)+2),
1916 (image->rows+2)*sizeof(*pixels));
1917 if (rle_info == (MemoryInfo *) NULL)
1919 pixel_info=RelinquishVirtualMemory(pixel_info);
1920 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
1922 bmp_data=(unsigned char *) GetVirtualMemoryBlob(rle_info);
1923 bmp_info.file_size-=bmp_info.image_size;
1924 bmp_info.image_size=(unsigned int) EncodeImage(image,bytes_per_line,
1926 bmp_info.file_size+=bmp_info.image_size;
1927 pixel_info=RelinquishVirtualMemory(pixel_info);
1928 pixel_info=rle_info;
1930 bmp_info.compression=BI_RLE8;
1933 Write BMP for Windows, all versions, 14-byte header.
1935 if (image->debug != MagickFalse)
1937 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1938 " Writing BMP version %.20g datastream",(double) type);
1939 if (image->storage_class == DirectClass)
1940 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1941 " Storage class=DirectClass");
1943 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1944 " Storage class=PseudoClass");
1945 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1946 " Image depth=%.20g",(double) image->depth);
1947 if (image->alpha_trait != UndefinedPixelTrait)
1948 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1951 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1952 " Matte=MagickFalse");
1953 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1954 " BMP bits_per_pixel=%.20g",(double) bmp_info.bits_per_pixel);
1955 switch ((int) bmp_info.compression)
1959 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1960 " Compression=BI_RGB");
1965 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1966 " Compression=BI_RLE8");
1971 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1972 " Compression=BI_BITFIELDS");
1977 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1978 " Compression=UNKNOWN (%u)",bmp_info.compression);
1982 if (bmp_info.number_colors == 0)
1983 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1984 " Number_colors=unspecified");
1986 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1987 " Number_colors=%u",bmp_info.number_colors);
1989 (void) WriteBlob(image,2,(unsigned char *) "BM");
1990 (void) WriteBlobLSBLong(image,bmp_info.file_size);
1991 (void) WriteBlobLSBLong(image,bmp_info.ba_offset); /* always 0 */
1992 (void) WriteBlobLSBLong(image,bmp_info.offset_bits);
1996 Write 12-byte version 2 bitmap header.
1998 (void) WriteBlobLSBLong(image,bmp_info.size);
1999 (void) WriteBlobLSBShort(image,(unsigned short) bmp_info.width);
2000 (void) WriteBlobLSBShort(image,(unsigned short) bmp_info.height);
2001 (void) WriteBlobLSBShort(image,bmp_info.planes);
2002 (void) WriteBlobLSBShort(image,bmp_info.bits_per_pixel);
2007 Write 40-byte version 3+ bitmap header.
2009 (void) WriteBlobLSBLong(image,bmp_info.size);
2010 (void) WriteBlobLSBLong(image,(unsigned int) bmp_info.width);
2011 (void) WriteBlobLSBLong(image,(unsigned int) bmp_info.height);
2012 (void) WriteBlobLSBShort(image,bmp_info.planes);
2013 (void) WriteBlobLSBShort(image,bmp_info.bits_per_pixel);
2014 (void) WriteBlobLSBLong(image,bmp_info.compression);
2015 (void) WriteBlobLSBLong(image,bmp_info.image_size);
2016 (void) WriteBlobLSBLong(image,bmp_info.x_pixels);
2017 (void) WriteBlobLSBLong(image,bmp_info.y_pixels);
2018 (void) WriteBlobLSBLong(image,bmp_info.number_colors);
2019 (void) WriteBlobLSBLong(image,bmp_info.colors_important);
2021 if ((type > 3) && ((image->alpha_trait != UndefinedPixelTrait) ||
2022 (have_color_info != MagickFalse)))
2025 Write the rest of the 108-byte BMP Version 4 header.
2027 (void) WriteBlobLSBLong(image,0x00ff0000U); /* Red mask */
2028 (void) WriteBlobLSBLong(image,0x0000ff00U); /* Green mask */
2029 (void) WriteBlobLSBLong(image,0x000000ffU); /* Blue mask */
2030 (void) WriteBlobLSBLong(image,0xff000000U); /* Alpha mask */
2031 (void) WriteBlobLSBLong(image,0x73524742U); /* sRGB */
2032 (void) WriteBlobLSBLong(image,(unsigned int)
2033 (image->chromaticity.red_primary.x*0x40000000));
2034 (void) WriteBlobLSBLong(image,(unsigned int)
2035 (image->chromaticity.red_primary.y*0x40000000));
2036 (void) WriteBlobLSBLong(image,(unsigned int)
2037 ((1.000f-(image->chromaticity.red_primary.x+
2038 image->chromaticity.red_primary.y))*0x40000000));
2039 (void) WriteBlobLSBLong(image,(unsigned int)
2040 (image->chromaticity.green_primary.x*0x40000000));
2041 (void) WriteBlobLSBLong(image,(unsigned int)
2042 (image->chromaticity.green_primary.y*0x40000000));
2043 (void) WriteBlobLSBLong(image,(unsigned int)
2044 ((1.000f-(image->chromaticity.green_primary.x+
2045 image->chromaticity.green_primary.y))*0x40000000));
2046 (void) WriteBlobLSBLong(image,(unsigned int)
2047 (image->chromaticity.blue_primary.x*0x40000000));
2048 (void) WriteBlobLSBLong(image,(unsigned int)
2049 (image->chromaticity.blue_primary.y*0x40000000));
2050 (void) WriteBlobLSBLong(image,(unsigned int)
2051 ((1.000f-(image->chromaticity.blue_primary.x+
2052 image->chromaticity.blue_primary.y))*0x40000000));
2053 (void) WriteBlobLSBLong(image,(unsigned int)
2054 (bmp_info.gamma_scale.x*0x10000));
2055 (void) WriteBlobLSBLong(image,(unsigned int)
2056 (bmp_info.gamma_scale.y*0x10000));
2057 (void) WriteBlobLSBLong(image,(unsigned int)
2058 (bmp_info.gamma_scale.z*0x10000));
2059 if ((image->rendering_intent != UndefinedIntent) ||
2060 (profile != (StringInfo *) NULL))
2065 switch ((int) image->rendering_intent)
2067 case SaturationIntent:
2069 intent=LCS_GM_BUSINESS;
2072 case RelativeIntent:
2074 intent=LCS_GM_GRAPHICS;
2077 case PerceptualIntent:
2079 intent=LCS_GM_IMAGES;
2082 case AbsoluteIntent:
2084 intent=LCS_GM_ABS_COLORIMETRIC;
2093 (void) WriteBlobLSBLong(image,(unsigned int) intent);
2094 (void) WriteBlobLSBLong(image,0x00); /* dummy profile data */
2095 (void) WriteBlobLSBLong(image,0x00); /* dummy profile length */
2096 (void) WriteBlobLSBLong(image,0x00); /* reserved */
2099 if (image->storage_class == PseudoClass)
2105 Dump colormap to file.
2107 if (image->debug != MagickFalse)
2108 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
2109 " Colormap: %.20g entries",(double) image->colors);
2110 bmp_colormap=(unsigned char *) AcquireQuantumMemory((size_t) (1UL <<
2111 bmp_info.bits_per_pixel),4*sizeof(*bmp_colormap));
2112 if (bmp_colormap == (unsigned char *) NULL)
2113 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
2115 for (i=0; i < (ssize_t) MagickMin((ssize_t) image->colors,(ssize_t) bmp_info.number_colors); i++)
2117 *q++=ScaleQuantumToChar(ClampToQuantum(image->colormap[i].blue));
2118 *q++=ScaleQuantumToChar(ClampToQuantum(image->colormap[i].green));
2119 *q++=ScaleQuantumToChar(ClampToQuantum(image->colormap[i].red));
2121 *q++=(unsigned char) 0x0;
2123 for ( ; i < (ssize_t) (1UL << bmp_info.bits_per_pixel); i++)
2125 *q++=(unsigned char) 0x00;
2126 *q++=(unsigned char) 0x00;
2127 *q++=(unsigned char) 0x00;
2129 *q++=(unsigned char) 0x00;
2132 (void) WriteBlob(image,(size_t) (3*(1L << bmp_info.bits_per_pixel)),
2135 (void) WriteBlob(image,(size_t) (4*(1L << bmp_info.bits_per_pixel)),
2137 bmp_colormap=(unsigned char *) RelinquishMagickMemory(bmp_colormap);
2139 if (image->debug != MagickFalse)
2140 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
2141 " Pixels: %u bytes",bmp_info.image_size);
2142 (void) WriteBlob(image,(size_t) bmp_info.image_size,pixels);
2143 pixel_info=RelinquishVirtualMemory(pixel_info);
2144 if (GetNextImageInList(image) == (Image *) NULL)
2146 image=SyncNextImageInList(image);
2147 status=SetImageProgress(image,SaveImagesTag,scene++,
2148 GetImageListLength(image));
2149 if (status == MagickFalse)
2151 } while (image_info->adjoin != MagickFalse);
2152 (void) CloseBlob(image);