2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
13 % Read/Write Microsoft Windows Bitmap Image Format %
17 % Glenn Randers-Pehrson %
21 % Copyright 1999-2017 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 == MagickCoreSignature);
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 == MagickCoreSignature);
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 == MagickCoreSignature);
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 == MagickCoreSignature);
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: %lu",
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) ReadBlobLSBSignedLong(image);
632 bmp_info.height=(ssize_t) ReadBlobLSBSignedLong(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 (%lu)",bmp_info.compression);
696 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
697 " Number of colors: %lu",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=ReadBlobLSBSignedLong(image);
713 Decode 2^30 fixed point formatted CIE primaries.
715 # define BMP_DENOM ((double) 0x40000000)
716 bmp_info.red_primary.x=(double) ReadBlobLSBLong(image)/BMP_DENOM;
717 bmp_info.red_primary.y=(double) ReadBlobLSBLong(image)/BMP_DENOM;
718 bmp_info.red_primary.z=(double) ReadBlobLSBLong(image)/BMP_DENOM;
719 bmp_info.green_primary.x=(double) ReadBlobLSBLong(image)/BMP_DENOM;
720 bmp_info.green_primary.y=(double) ReadBlobLSBLong(image)/BMP_DENOM;
721 bmp_info.green_primary.z=(double) ReadBlobLSBLong(image)/BMP_DENOM;
722 bmp_info.blue_primary.x=(double) ReadBlobLSBLong(image)/BMP_DENOM;
723 bmp_info.blue_primary.y=(double) ReadBlobLSBLong(image)/BMP_DENOM;
724 bmp_info.blue_primary.z=(double) ReadBlobLSBLong(image)/BMP_DENOM;
726 gamma=bmp_info.red_primary.x+bmp_info.red_primary.y+
727 bmp_info.red_primary.z;
728 gamma=PerceptibleReciprocal(gamma);
729 bmp_info.red_primary.x*=gamma;
730 bmp_info.red_primary.y*=gamma;
731 image->chromaticity.red_primary.x=bmp_info.red_primary.x;
732 image->chromaticity.red_primary.y=bmp_info.red_primary.y;
734 gamma=bmp_info.green_primary.x+bmp_info.green_primary.y+
735 bmp_info.green_primary.z;
736 gamma=PerceptibleReciprocal(gamma);
737 bmp_info.green_primary.x*=gamma;
738 bmp_info.green_primary.y*=gamma;
739 image->chromaticity.green_primary.x=bmp_info.green_primary.x;
740 image->chromaticity.green_primary.y=bmp_info.green_primary.y;
742 gamma=bmp_info.blue_primary.x+bmp_info.blue_primary.y+
743 bmp_info.blue_primary.z;
744 gamma=PerceptibleReciprocal(gamma);
745 bmp_info.blue_primary.x*=gamma;
746 bmp_info.blue_primary.y*=gamma;
747 image->chromaticity.blue_primary.x=bmp_info.blue_primary.x;
748 image->chromaticity.blue_primary.y=bmp_info.blue_primary.y;
751 Decode 16^16 fixed point formatted gamma_scales.
753 bmp_info.gamma_scale.x=(double) ReadBlobLSBLong(image)/0x10000;
754 bmp_info.gamma_scale.y=(double) ReadBlobLSBLong(image)/0x10000;
755 bmp_info.gamma_scale.z=(double) ReadBlobLSBLong(image)/0x10000;
757 Compute a single gamma from the BMP 3-channel gamma.
759 image->gamma=(bmp_info.gamma_scale.x+bmp_info.gamma_scale.y+
760 bmp_info.gamma_scale.z)/3.0;
763 (void) CopyMagickString(image->magick,"BMP3",MagickPathExtent);
765 if (bmp_info.size > 108)
771 Read BMP Version 5 color management information.
773 intent=ReadBlobLSBLong(image);
774 switch ((int) intent)
776 case LCS_GM_BUSINESS:
778 image->rendering_intent=SaturationIntent;
781 case LCS_GM_GRAPHICS:
783 image->rendering_intent=RelativeIntent;
788 image->rendering_intent=PerceptualIntent;
791 case LCS_GM_ABS_COLORIMETRIC:
793 image->rendering_intent=AbsoluteIntent;
797 profile_data=ReadBlobLSBLong(image);
798 profile_size=ReadBlobLSBLong(image);
801 (void) ReadBlobLSBLong(image); /* Reserved byte */
804 if ((MagickSizeType) bmp_info.file_size > GetBlobSize(image))
805 (void) ThrowMagickException(exception,GetMagickModule(),CorruptImageError,
806 "LengthAndFilesizeDoNotMatch","`%s'",image->filename);
808 if ((MagickSizeType) bmp_info.file_size < GetBlobSize(image))
809 (void) ThrowMagickException(exception,GetMagickModule(),
810 CorruptImageWarning,"LengthAndFilesizeDoNotMatch","`%s'",
812 if (bmp_info.width <= 0)
813 ThrowReaderException(CorruptImageError,"NegativeOrZeroImageSize");
814 if (bmp_info.height == 0)
815 ThrowReaderException(CorruptImageError,"NegativeOrZeroImageSize");
816 if (bmp_info.planes != 1)
817 ThrowReaderException(CorruptImageError,"StaticPlanesValueNotEqualToOne");
818 if ((bmp_info.bits_per_pixel != 1) && (bmp_info.bits_per_pixel != 4) &&
819 (bmp_info.bits_per_pixel != 8) && (bmp_info.bits_per_pixel != 16) &&
820 (bmp_info.bits_per_pixel != 24) && (bmp_info.bits_per_pixel != 32))
821 ThrowReaderException(CorruptImageError,"UnrecognizedBitsPerPixel");
822 if (bmp_info.bits_per_pixel < 16 &&
823 bmp_info.number_colors > (1U << bmp_info.bits_per_pixel))
824 ThrowReaderException(CorruptImageError,"UnrecognizedNumberOfColors");
825 if ((bmp_info.compression == 1) && (bmp_info.bits_per_pixel != 8))
826 ThrowReaderException(CorruptImageError,"UnrecognizedBitsPerPixel");
827 if ((bmp_info.compression == 2) && (bmp_info.bits_per_pixel != 4))
828 ThrowReaderException(CorruptImageError,"UnrecognizedBitsPerPixel");
829 if ((bmp_info.compression == 3) && (bmp_info.bits_per_pixel < 16))
830 ThrowReaderException(CorruptImageError,"UnrecognizedBitsPerPixel");
831 switch (bmp_info.compression)
834 image->compression=NoCompression;
838 image->compression=RLECompression;
843 ThrowReaderException(CoderError,"JPEGCompressNotSupported");
845 ThrowReaderException(CoderError,"PNGCompressNotSupported");
847 ThrowReaderException(CorruptImageError,"UnrecognizedImageCompression");
849 image->columns=(size_t) MagickAbsoluteValue(bmp_info.width);
850 image->rows=(size_t) MagickAbsoluteValue(bmp_info.height);
851 image->depth=bmp_info.bits_per_pixel <= 8 ? bmp_info.bits_per_pixel : 8;
852 image->alpha_trait=((bmp_info.alpha_mask != 0) &&
853 (bmp_info.compression == BI_BITFIELDS)) ? BlendPixelTrait :
855 if (bmp_info.bits_per_pixel < 16)
860 image->storage_class=PseudoClass;
861 image->colors=bmp_info.number_colors;
863 if (image->colors == 0)
864 image->colors=one << bmp_info.bits_per_pixel;
866 if (image->storage_class == PseudoClass)
875 Read BMP raster colormap.
877 if (image->debug != MagickFalse)
878 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
879 " Reading colormap of %.20g colors",(double) image->colors);
880 if (AcquireImageColormap(image,image->colors,exception) == MagickFalse)
881 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
882 bmp_colormap=(unsigned char *) AcquireQuantumMemory((size_t)
883 image->colors,4*sizeof(*bmp_colormap));
884 if (bmp_colormap == (unsigned char *) NULL)
885 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
886 if ((bmp_info.size == 12) || (bmp_info.size == 64))
890 offset=SeekBlob(image,start_position+14+bmp_info.size,SEEK_SET);
892 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
893 count=ReadBlob(image,packet_size*image->colors,bmp_colormap);
894 if (count != (ssize_t) (packet_size*image->colors))
895 ThrowReaderException(CorruptImageError,"InsufficientImageDataInFile");
897 for (i=0; i < (ssize_t) image->colors; i++)
899 image->colormap[i].blue=(MagickRealType) ScaleCharToQuantum(*p++);
900 image->colormap[i].green=(MagickRealType) ScaleCharToQuantum(*p++);
901 image->colormap[i].red=(MagickRealType) ScaleCharToQuantum(*p++);
902 if (packet_size == 4)
905 bmp_colormap=(unsigned char *) RelinquishMagickMemory(bmp_colormap);
907 image->resolution.x=(double) bmp_info.x_pixels/100.0;
908 image->resolution.y=(double) bmp_info.y_pixels/100.0;
909 image->units=PixelsPerCentimeterResolution;
910 if ((image_info->ping != MagickFalse) && (image_info->number_scenes != 0))
911 if (image->scene >= (image_info->scene+image_info->number_scenes-1))
913 status=SetImageExtent(image,image->columns,image->rows,exception);
914 if (status == MagickFalse)
915 return(DestroyImageList(image));
919 offset=SeekBlob(image,start_position+bmp_info.offset_bits,SEEK_SET);
921 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
922 if (bmp_info.compression == BI_RLE4)
923 bmp_info.bits_per_pixel<<=1;
924 bytes_per_line=4*((image->columns*bmp_info.bits_per_pixel+31)/32);
925 length=(size_t) bytes_per_line*image->rows;
926 pixel_info=AcquireVirtualMemory((size_t) image->rows,
927 MagickMax(bytes_per_line,image->columns+256UL)*sizeof(*pixels));
928 if (pixel_info == (MemoryInfo *) NULL)
929 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
930 pixels=(unsigned char *) GetVirtualMemoryBlob(pixel_info);
931 if ((bmp_info.compression == BI_RGB) ||
932 (bmp_info.compression == BI_BITFIELDS))
934 if (image->debug != MagickFalse)
935 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
936 " Reading pixels (%.20g bytes)",(double) length);
937 count=ReadBlob(image,length,pixels);
938 if (count != (ssize_t) length)
940 pixel_info=RelinquishVirtualMemory(pixel_info);
941 ThrowReaderException(CorruptImageError,
942 "InsufficientImageDataInFile");
948 Convert run-length encoded raster pixels.
950 status=DecodeImage(image,bmp_info.compression,pixels);
951 if (status == MagickFalse)
953 pixel_info=RelinquishVirtualMemory(pixel_info);
954 ThrowReaderException(CorruptImageError,
955 "UnableToRunlengthDecodeImage");
959 Convert BMP raster image to pixel packets.
961 if (bmp_info.compression == BI_RGB)
964 We should ignore the alpha value in BMP3 files but there have been
965 reports about 32 bit files with alpha. We do a quick check to see if
966 the alpha channel contains a value that is not zero (default value).
967 If we find a non zero value we asume the program that wrote the file
968 wants to use the alpha channel.
970 if ((image->alpha_trait == UndefinedPixelTrait) && (bmp_info.size == 40) &&
971 (bmp_info.bits_per_pixel == 32))
973 bytes_per_line=4*(image->columns);
974 for (y=(ssize_t) image->rows-1; y >= 0; y--)
976 p=pixels+(image->rows-y-1)*bytes_per_line;
977 for (x=0; x < (ssize_t) image->columns; x++)
981 image->alpha_trait=BlendPixelTrait;
989 bmp_info.alpha_mask=image->alpha_trait != UndefinedPixelTrait ?
991 bmp_info.red_mask=0x00ff0000U;
992 bmp_info.green_mask=0x0000ff00U;
993 bmp_info.blue_mask=0x000000ffU;
994 if (bmp_info.bits_per_pixel == 16)
999 bmp_info.red_mask=0x00007c00U;
1000 bmp_info.green_mask=0x000003e0U;
1001 bmp_info.blue_mask=0x0000001fU;
1004 (void) ResetMagickMemory(&shift,0,sizeof(shift));
1005 (void) ResetMagickMemory(&quantum_bits,0,sizeof(quantum_bits));
1006 if ((bmp_info.bits_per_pixel == 16) || (bmp_info.bits_per_pixel == 32))
1012 Get shift and quantum bits info from bitfield masks.
1014 if (bmp_info.red_mask != 0)
1015 while (((bmp_info.red_mask << shift.red) & 0x80000000UL) == 0)
1017 if (bmp_info.green_mask != 0)
1018 while (((bmp_info.green_mask << shift.green) & 0x80000000UL) == 0)
1020 if (bmp_info.blue_mask != 0)
1021 while (((bmp_info.blue_mask << shift.blue) & 0x80000000UL) == 0)
1023 if (bmp_info.alpha_mask != 0)
1024 while (((bmp_info.alpha_mask << shift.alpha) & 0x80000000UL) == 0)
1027 while (((bmp_info.red_mask << sample) & 0x80000000UL) != 0)
1029 quantum_bits.red=(MagickRealType) (sample-shift.red);
1031 while (((bmp_info.green_mask << sample) & 0x80000000UL) != 0)
1033 quantum_bits.green=(MagickRealType) (sample-shift.green);
1035 while (((bmp_info.blue_mask << sample) & 0x80000000UL) != 0)
1037 quantum_bits.blue=(MagickRealType) (sample-shift.blue);
1039 while (((bmp_info.alpha_mask << sample) & 0x80000000UL) != 0)
1041 quantum_bits.alpha=(MagickRealType) (sample-shift.alpha);
1043 switch (bmp_info.bits_per_pixel)
1048 Convert bitmap scanline.
1050 for (y=(ssize_t) image->rows-1; y >= 0; y--)
1052 p=pixels+(image->rows-y-1)*bytes_per_line;
1053 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
1054 if (q == (Quantum *) NULL)
1056 for (x=0; x < ((ssize_t) image->columns-7); x+=8)
1058 for (bit=0; bit < 8; bit++)
1060 index=(Quantum) (((*p) & (0x80 >> bit)) != 0 ? 0x01 : 0x00);
1061 SetPixelIndex(image,index,q);
1062 q+=GetPixelChannels(image);
1066 if ((image->columns % 8) != 0)
1068 for (bit=0; bit < (image->columns % 8); bit++)
1070 index=(Quantum) (((*p) & (0x80 >> bit)) != 0 ? 0x01 : 0x00);
1071 SetPixelIndex(image,index,q);
1072 q+=GetPixelChannels(image);
1076 if (SyncAuthenticPixels(image,exception) == MagickFalse)
1078 if (image->previous == (Image *) NULL)
1080 status=SetImageProgress(image,LoadImageTag,(MagickOffsetType)
1081 (image->rows-y),image->rows);
1082 if (status == MagickFalse)
1086 (void) SyncImage(image,exception);
1092 Convert PseudoColor scanline.
1094 for (y=(ssize_t) image->rows-1; y >= 0; y--)
1096 p=pixels+(image->rows-y-1)*bytes_per_line;
1097 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
1098 if (q == (Quantum *) NULL)
1100 for (x=0; x < ((ssize_t) image->columns-1); x+=2)
1102 ValidateColormapValue(image,(*p >> 4) & 0x0f,&index,exception);
1103 SetPixelIndex(image,index,q);
1104 q+=GetPixelChannels(image);
1105 ValidateColormapValue(image,*p & 0x0f,&index,exception);
1106 SetPixelIndex(image,index,q);
1107 q+=GetPixelChannels(image);
1110 if ((image->columns % 2) != 0)
1112 ValidateColormapValue(image,(*p >> 4) & 0xf,&index,exception);
1113 SetPixelIndex(image,index,q);
1114 q+=GetPixelChannels(image);
1118 if (x < (ssize_t) image->columns)
1120 if (SyncAuthenticPixels(image,exception) == MagickFalse)
1122 if (image->previous == (Image *) NULL)
1124 status=SetImageProgress(image,LoadImageTag,(MagickOffsetType)
1125 (image->rows-y),image->rows);
1126 if (status == MagickFalse)
1130 (void) SyncImage(image,exception);
1136 Convert PseudoColor scanline.
1138 if ((bmp_info.compression == BI_RLE8) ||
1139 (bmp_info.compression == BI_RLE4))
1140 bytes_per_line=image->columns;
1141 for (y=(ssize_t) image->rows-1; y >= 0; y--)
1143 p=pixels+(image->rows-y-1)*bytes_per_line;
1144 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
1145 if (q == (Quantum *) NULL)
1147 for (x=(ssize_t) image->columns; x != 0; --x)
1149 ValidateColormapValue(image,*p++,&index,exception);
1150 SetPixelIndex(image,index,q);
1151 q+=GetPixelChannels(image);
1153 if (SyncAuthenticPixels(image,exception) == MagickFalse)
1155 offset=(MagickOffsetType) (image->rows-y-1);
1156 if (image->previous == (Image *) NULL)
1158 status=SetImageProgress(image,LoadImageTag,(MagickOffsetType)
1159 (image->rows-y),image->rows);
1160 if (status == MagickFalse)
1164 (void) SyncImage(image,exception);
1174 Convert bitfield encoded 16-bit PseudoColor scanline.
1176 if (bmp_info.compression != BI_RGB &&
1177 bmp_info.compression != BI_BITFIELDS)
1179 pixel_info=RelinquishVirtualMemory(pixel_info);
1180 ThrowReaderException(CorruptImageError,
1181 "UnrecognizedImageCompression");
1183 bytes_per_line=2*(image->columns+image->columns % 2);
1184 image->storage_class=DirectClass;
1185 for (y=(ssize_t) image->rows-1; y >= 0; y--)
1187 p=pixels+(image->rows-y-1)*bytes_per_line;
1188 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
1189 if (q == (Quantum *) NULL)
1191 for (x=0; x < (ssize_t) image->columns; x++)
1193 pixel=(size_t) (*p++);
1195 red=((pixel & bmp_info.red_mask) << shift.red) >> 16;
1196 if (quantum_bits.red == 5)
1197 red|=((red & 0xe000) >> 5);
1198 if (quantum_bits.red <= 8)
1199 red|=((red & 0xff00) >> 8);
1200 green=((pixel & bmp_info.green_mask) << shift.green) >> 16;
1201 if (quantum_bits.green == 5)
1202 green|=((green & 0xe000) >> 5);
1203 if (quantum_bits.green == 6)
1204 green|=((green & 0xc000) >> 6);
1205 if (quantum_bits.green <= 8)
1206 green|=((green & 0xff00) >> 8);
1207 blue=((pixel & bmp_info.blue_mask) << shift.blue) >> 16;
1208 if (quantum_bits.blue == 5)
1209 blue|=((blue & 0xe000) >> 5);
1210 if (quantum_bits.blue <= 8)
1211 blue|=((blue & 0xff00) >> 8);
1212 SetPixelRed(image,ScaleShortToQuantum((unsigned short) red),q);
1213 SetPixelGreen(image,ScaleShortToQuantum((unsigned short) green),q);
1214 SetPixelBlue(image,ScaleShortToQuantum((unsigned short) blue),q);
1215 SetPixelAlpha(image,OpaqueAlpha,q);
1216 if (image->alpha_trait != UndefinedPixelTrait)
1218 alpha=((pixel & bmp_info.alpha_mask) << shift.alpha) >> 16;
1219 if (quantum_bits.alpha <= 8)
1220 alpha|=((alpha & 0xff00) >> 8);
1221 SetPixelAlpha(image,ScaleShortToQuantum(
1222 (unsigned short) alpha),q);
1224 q+=GetPixelChannels(image);
1226 if (SyncAuthenticPixels(image,exception) == MagickFalse)
1228 offset=(MagickOffsetType) (image->rows-y-1);
1229 if (image->previous == (Image *) NULL)
1231 status=SetImageProgress(image,LoadImageTag,(MagickOffsetType)
1232 (image->rows-y),image->rows);
1233 if (status == MagickFalse)
1242 Convert DirectColor scanline.
1244 bytes_per_line=4*((image->columns*24+31)/32);
1245 for (y=(ssize_t) image->rows-1; y >= 0; y--)
1247 p=pixels+(image->rows-y-1)*bytes_per_line;
1248 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
1249 if (q == (Quantum *) NULL)
1251 for (x=0; x < (ssize_t) image->columns; x++)
1253 SetPixelBlue(image,ScaleCharToQuantum(*p++),q);
1254 SetPixelGreen(image,ScaleCharToQuantum(*p++),q);
1255 SetPixelRed(image,ScaleCharToQuantum(*p++),q);
1256 SetPixelAlpha(image,OpaqueAlpha,q);
1257 q+=GetPixelChannels(image);
1259 if (SyncAuthenticPixels(image,exception) == MagickFalse)
1261 offset=(MagickOffsetType) (image->rows-y-1);
1262 if (image->previous == (Image *) NULL)
1264 status=SetImageProgress(image,LoadImageTag,(MagickOffsetType)
1265 (image->rows-y),image->rows);
1266 if (status == MagickFalse)
1275 Convert bitfield encoded DirectColor scanline.
1277 if ((bmp_info.compression != BI_RGB) &&
1278 (bmp_info.compression != BI_BITFIELDS))
1280 pixel_info=RelinquishVirtualMemory(pixel_info);
1281 ThrowReaderException(CorruptImageError,
1282 "UnrecognizedImageCompression");
1284 bytes_per_line=4*(image->columns);
1285 for (y=(ssize_t) image->rows-1; y >= 0; y--)
1291 p=pixels+(image->rows-y-1)*bytes_per_line;
1292 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
1293 if (q == (Quantum *) NULL)
1295 for (x=0; x < (ssize_t) image->columns; x++)
1297 pixel=(size_t) (*p++);
1298 pixel|=((size_t) *p++ << 8);
1299 pixel|=((size_t) *p++ << 16);
1300 pixel|=((size_t) *p++ << 24);
1301 red=((pixel & bmp_info.red_mask) << shift.red) >> 16;
1302 if (quantum_bits.red == 8)
1304 green=((pixel & bmp_info.green_mask) << shift.green) >> 16;
1305 if (quantum_bits.green == 8)
1306 green|=(green >> 8);
1307 blue=((pixel & bmp_info.blue_mask) << shift.blue) >> 16;
1308 if (quantum_bits.blue == 8)
1310 SetPixelRed(image,ScaleShortToQuantum((unsigned short) red),q);
1311 SetPixelGreen(image,ScaleShortToQuantum((unsigned short) green),q);
1312 SetPixelBlue(image,ScaleShortToQuantum((unsigned short) blue),q);
1313 SetPixelAlpha(image,OpaqueAlpha,q);
1314 if (image->alpha_trait != UndefinedPixelTrait)
1316 alpha=((pixel & bmp_info.alpha_mask) << shift.alpha) >> 16;
1317 if (quantum_bits.alpha == 8)
1318 alpha|=(alpha >> 8);
1319 SetPixelAlpha(image,ScaleShortToQuantum(
1320 (unsigned short) alpha),q);
1322 q+=GetPixelChannels(image);
1324 if (SyncAuthenticPixels(image,exception) == MagickFalse)
1326 offset=(MagickOffsetType) (image->rows-y-1);
1327 if (image->previous == (Image *) NULL)
1329 status=SetImageProgress(image,LoadImageTag,(MagickOffsetType)
1330 (image->rows-y),image->rows);
1331 if (status == MagickFalse)
1339 pixel_info=RelinquishVirtualMemory(pixel_info);
1340 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
1343 pixel_info=RelinquishVirtualMemory(pixel_info);
1346 if (EOFBlob(image) != MagickFalse)
1348 ThrowFileException(exception,CorruptImageError,"UnexpectedEndOfFile",
1352 if (bmp_info.height < 0)
1358 Correct image orientation.
1360 flipped_image=FlipImage(image,exception);
1361 if (flipped_image != (Image *) NULL)
1363 DuplicateBlob(flipped_image,image);
1364 image=DestroyImage(image);
1365 image=flipped_image;
1369 Proceed to next image.
1371 if (image_info->number_scenes != 0)
1372 if (image->scene >= (image_info->scene+image_info->number_scenes-1))
1375 if (bmp_info.ba_offset != 0)
1377 offset=SeekBlob(image,(MagickOffsetType) bmp_info.ba_offset,SEEK_SET);
1379 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
1381 count=ReadBlob(image,2,magick);
1382 if ((count == 2) && (IsBMP(magick,2) != MagickFalse))
1385 Acquire next image structure.
1387 AcquireNextImage(image_info,image,exception);
1388 if (GetNextImageInList(image) == (Image *) NULL)
1390 image=DestroyImageList(image);
1391 return((Image *) NULL);
1393 image=SyncNextImageInList(image);
1394 status=SetImageProgress(image,LoadImagesTag,TellBlob(image),
1395 GetBlobSize(image));
1396 if (status == MagickFalse)
1399 } while (IsBMP(magick,2) != MagickFalse);
1400 (void) CloseBlob(image);
1401 return(GetFirstImageInList(image));
1405 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1409 % R e g i s t e r B M P I m a g e %
1413 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1415 % RegisterBMPImage() adds attributes for the BMP image format to
1416 % the list of supported formats. The attributes include the image format
1417 % tag, a method to read and/or write the format, whether the format
1418 % supports the saving of more than one frame to the same file or blob,
1419 % whether the format supports native in-memory I/O, and a brief
1420 % description of the format.
1422 % The format of the RegisterBMPImage method is:
1424 % size_t RegisterBMPImage(void)
1427 ModuleExport size_t RegisterBMPImage(void)
1432 entry=AcquireMagickInfo("BMP","BMP","Microsoft Windows bitmap image");
1433 entry->decoder=(DecodeImageHandler *) ReadBMPImage;
1434 entry->encoder=(EncodeImageHandler *) WriteBMPImage;
1435 entry->magick=(IsImageFormatHandler *) IsBMP;
1436 entry->flags^=CoderAdjoinFlag;
1437 entry->flags|=CoderSeekableStreamFlag;
1438 (void) RegisterMagickInfo(entry);
1439 entry=AcquireMagickInfo("BMP","BMP2","Microsoft Windows bitmap image (V2)");
1440 entry->encoder=(EncodeImageHandler *) WriteBMPImage;
1441 entry->magick=(IsImageFormatHandler *) IsBMP;
1442 entry->flags^=CoderAdjoinFlag;
1443 entry->flags|=CoderSeekableStreamFlag;
1444 (void) RegisterMagickInfo(entry);
1445 entry=AcquireMagickInfo("BMP","BMP3","Microsoft Windows bitmap image (V3)");
1446 entry->encoder=(EncodeImageHandler *) WriteBMPImage;
1447 entry->magick=(IsImageFormatHandler *) IsBMP;
1448 entry->flags^=CoderAdjoinFlag;
1449 entry->flags|=CoderSeekableStreamFlag;
1450 (void) RegisterMagickInfo(entry);
1451 return(MagickImageCoderSignature);
1455 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1459 % U n r e g i s t e r B M P I m a g e %
1463 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1465 % UnregisterBMPImage() removes format registrations made by the
1466 % BMP module from the list of supported formats.
1468 % The format of the UnregisterBMPImage method is:
1470 % UnregisterBMPImage(void)
1473 ModuleExport void UnregisterBMPImage(void)
1475 (void) UnregisterMagickInfo("BMP");
1476 (void) UnregisterMagickInfo("BMP2");
1477 (void) UnregisterMagickInfo("BMP3");
1481 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1485 % W r i t e B M P I m a g e %
1489 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1491 % WriteBMPImage() writes an image in Microsoft Windows bitmap encoded
1492 % image format, version 3 for Windows or (if the image has a matte channel)
1495 % The format of the WriteBMPImage method is:
1497 % MagickBooleanType WriteBMPImage(const ImageInfo *image_info,
1498 % Image *image,ExceptionInfo *exception)
1500 % A description of each parameter follows.
1502 % o image_info: the image info.
1504 % o image: The image.
1506 % o exception: return any errors or warnings in this structure.
1509 static MagickBooleanType WriteBMPImage(const ImageInfo *image_info,Image *image,
1510 ExceptionInfo *exception)
1531 register const Quantum
1538 register unsigned char
1553 Open output image file.
1555 assert(image_info != (const ImageInfo *) NULL);
1556 assert(image_info->signature == MagickCoreSignature);
1557 assert(image != (Image *) NULL);
1558 assert(image->signature == MagickCoreSignature);
1559 if (image->debug != MagickFalse)
1560 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1561 assert(exception != (ExceptionInfo *) NULL);
1562 assert(exception->signature == MagickCoreSignature);
1563 status=OpenBlob(image_info,image,WriteBinaryBlobMode,exception);
1564 if (status == MagickFalse)
1567 if (LocaleCompare(image_info->magick,"BMP2") == 0)
1570 if (LocaleCompare(image_info->magick,"BMP3") == 0)
1573 option=GetImageOption(image_info,"bmp:format");
1574 if (option != (char *) NULL)
1576 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1577 " Format=%s",option);
1579 if (LocaleCompare(option,"bmp2") == 0)
1581 if (LocaleCompare(option,"bmp3") == 0)
1583 if (LocaleCompare(option,"bmp4") == 0)
1591 Initialize BMP raster file header.
1593 (void) TransformImageColorspace(image,sRGBColorspace,exception);
1594 (void) ResetMagickMemory(&bmp_info,0,sizeof(bmp_info));
1595 bmp_info.file_size=14+12;
1597 bmp_info.file_size+=28;
1598 bmp_info.offset_bits=bmp_info.file_size;
1599 bmp_info.compression=BI_RGB;
1600 if ((image->storage_class == PseudoClass) && (image->colors > 256))
1601 (void) SetImageStorageClass(image,DirectClass,exception);
1602 if (image->storage_class != DirectClass)
1605 Colormapped BMP raster.
1607 bmp_info.bits_per_pixel=8;
1608 if (image->colors <= 2)
1609 bmp_info.bits_per_pixel=1;
1611 if (image->colors <= 16)
1612 bmp_info.bits_per_pixel=4;
1614 if (image->colors <= 256)
1615 bmp_info.bits_per_pixel=8;
1616 if (image_info->compression == RLECompression)
1617 bmp_info.bits_per_pixel=8;
1618 bmp_info.number_colors=1U << bmp_info.bits_per_pixel;
1619 if (image->alpha_trait != UndefinedPixelTrait)
1620 (void) SetImageStorageClass(image,DirectClass,exception);
1622 if ((size_t) bmp_info.number_colors < image->colors)
1623 (void) SetImageStorageClass(image,DirectClass,exception);
1626 bmp_info.file_size+=3*(1UL << bmp_info.bits_per_pixel);
1627 bmp_info.offset_bits+=3*(1UL << bmp_info.bits_per_pixel);
1630 bmp_info.file_size+=(1UL << bmp_info.bits_per_pixel);
1631 bmp_info.offset_bits+=(1UL << bmp_info.bits_per_pixel);
1635 if (image->storage_class == DirectClass)
1638 Full color BMP raster.
1640 bmp_info.number_colors=0;
1641 bmp_info.bits_per_pixel=(unsigned short)
1642 ((type > 3) && (image->alpha_trait != UndefinedPixelTrait) ? 32 : 24);
1643 bmp_info.compression=(unsigned int) ((type > 3) &&
1644 (image->alpha_trait != UndefinedPixelTrait) ? BI_BITFIELDS : BI_RGB);
1645 if ((type == 3) && (image->alpha_trait != UndefinedPixelTrait))
1647 option=GetImageOption(image_info,"bmp3:alpha");
1648 if (IsStringTrue(option))
1649 bmp_info.bits_per_pixel=32;
1652 bytes_per_line=4*((image->columns*bmp_info.bits_per_pixel+31)/32);
1653 bmp_info.ba_offset=0;
1654 profile=GetImageProfile(image,"icc");
1655 have_color_info=(image->rendering_intent != UndefinedIntent) ||
1656 (profile != (StringInfo *) NULL) || (image->gamma != 0.0) ? MagickTrue :
1661 if ((type == 3) || ((image->alpha_trait == UndefinedPixelTrait) &&
1662 (have_color_info == MagickFalse)))
1674 if ((image->rendering_intent != UndefinedIntent) ||
1675 (profile != (StringInfo *) NULL))
1680 bmp_info.file_size+=extra_size;
1681 bmp_info.offset_bits+=extra_size;
1683 if (((ssize_t) image->columns != (signed int) image->columns) ||
1684 ((ssize_t) image->rows != (signed int) image->rows))
1685 ThrowWriterException(ImageError,"WidthOrHeightExceedsLimit");
1686 bmp_info.width=(ssize_t) image->columns;
1687 bmp_info.height=(ssize_t) image->rows;
1689 bmp_info.image_size=(unsigned long) (bytes_per_line*image->rows);
1690 bmp_info.file_size+=bmp_info.image_size;
1691 bmp_info.x_pixels=75*39;
1692 bmp_info.y_pixels=75*39;
1693 switch (image->units)
1695 case UndefinedResolution:
1696 case PixelsPerInchResolution:
1698 bmp_info.x_pixels=(unsigned int) (100.0*image->resolution.x/2.54);
1699 bmp_info.y_pixels=(unsigned int) (100.0*image->resolution.y/2.54);
1702 case PixelsPerCentimeterResolution:
1704 bmp_info.x_pixels=(unsigned int) (100.0*image->resolution.x);
1705 bmp_info.y_pixels=(unsigned int) (100.0*image->resolution.y);
1709 bmp_info.colors_important=bmp_info.number_colors;
1711 Convert MIFF to BMP raster pixels.
1713 pixel_info=AcquireVirtualMemory((size_t) bmp_info.image_size,
1715 if (pixel_info == (MemoryInfo *) NULL)
1716 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
1717 pixels=(unsigned char *) GetVirtualMemoryBlob(pixel_info);
1718 (void) ResetMagickMemory(pixels,0,(size_t) bmp_info.image_size);
1719 switch (bmp_info.bits_per_pixel)
1728 Convert PseudoClass image to a BMP monochrome image.
1730 for (y=0; y < (ssize_t) image->rows; y++)
1735 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
1736 if (p == (const Quantum *) NULL)
1738 q=pixels+(image->rows-y-1)*bytes_per_line;
1741 for (x=0; x < (ssize_t) image->columns; x++)
1744 byte|=GetPixelIndex(image,p) != 0 ? 0x01 : 0x00;
1748 *q++=(unsigned char) byte;
1752 p+=GetPixelChannels(image);
1756 *q++=(unsigned char) (byte << (8-bit));
1759 offset=(ssize_t) (image->columns+7)/8;
1760 for (x=offset; x < (ssize_t) bytes_per_line; x++)
1762 if (image->previous == (Image *) NULL)
1764 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
1766 if (status == MagickFalse)
1782 Convert PseudoClass image to a BMP monochrome image.
1784 for (y=0; y < (ssize_t) image->rows; y++)
1786 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
1787 if (p == (const Quantum *) NULL)
1789 q=pixels+(image->rows-y-1)*bytes_per_line;
1792 for (x=0; x < (ssize_t) image->columns; x++)
1795 byte|=((size_t) GetPixelIndex(image,p) & 0x0f);
1799 *q++=(unsigned char) byte;
1803 p+=GetPixelChannels(image);
1807 *q++=(unsigned char) (byte << 4);
1810 offset=(ssize_t) (image->columns+1)/2;
1811 for (x=offset; x < (ssize_t) bytes_per_line; x++)
1813 if (image->previous == (Image *) NULL)
1815 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
1817 if (status == MagickFalse)
1826 Convert PseudoClass packet to BMP pixel.
1828 for (y=0; y < (ssize_t) image->rows; y++)
1830 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
1831 if (p == (const Quantum *) NULL)
1833 q=pixels+(image->rows-y-1)*bytes_per_line;
1834 for (x=0; x < (ssize_t) image->columns; x++)
1836 *q++=(unsigned char) GetPixelIndex(image,p);
1837 p+=GetPixelChannels(image);
1839 for ( ; x < (ssize_t) bytes_per_line; x++)
1841 if (image->previous == (Image *) NULL)
1843 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
1845 if (status == MagickFalse)
1854 Convert DirectClass packet to BMP BGR888.
1856 for (y=0; y < (ssize_t) image->rows; y++)
1858 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
1859 if (p == (const Quantum *) NULL)
1861 q=pixels+(image->rows-y-1)*bytes_per_line;
1862 for (x=0; x < (ssize_t) image->columns; x++)
1864 *q++=ScaleQuantumToChar(GetPixelBlue(image,p));
1865 *q++=ScaleQuantumToChar(GetPixelGreen(image,p));
1866 *q++=ScaleQuantumToChar(GetPixelRed(image,p));
1867 p+=GetPixelChannels(image);
1869 for (x=3L*(ssize_t) image->columns; x < (ssize_t) bytes_per_line; x++)
1871 if (image->previous == (Image *) NULL)
1873 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
1875 if (status == MagickFalse)
1884 Convert DirectClass packet to ARGB8888 pixel.
1886 for (y=0; y < (ssize_t) image->rows; y++)
1888 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
1889 if (p == (const Quantum *) NULL)
1891 q=pixels+(image->rows-y-1)*bytes_per_line;
1892 for (x=0; x < (ssize_t) image->columns; x++)
1894 *q++=ScaleQuantumToChar(GetPixelBlue(image,p));
1895 *q++=ScaleQuantumToChar(GetPixelGreen(image,p));
1896 *q++=ScaleQuantumToChar(GetPixelRed(image,p));
1897 *q++=ScaleQuantumToChar(GetPixelAlpha(image,p));
1898 p+=GetPixelChannels(image);
1900 if (image->previous == (Image *) NULL)
1902 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
1904 if (status == MagickFalse)
1911 if ((type > 2) && (bmp_info.bits_per_pixel == 8))
1912 if (image_info->compression != NoCompression)
1918 Convert run-length encoded raster pixels.
1920 rle_info=AcquireVirtualMemory((size_t) (2*(bytes_per_line+2)+2),
1921 (image->rows+2)*sizeof(*pixels));
1922 if (rle_info == (MemoryInfo *) NULL)
1924 pixel_info=RelinquishVirtualMemory(pixel_info);
1925 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
1927 bmp_data=(unsigned char *) GetVirtualMemoryBlob(rle_info);
1928 bmp_info.file_size-=bmp_info.image_size;
1929 bmp_info.image_size=(unsigned int) EncodeImage(image,bytes_per_line,
1931 bmp_info.file_size+=bmp_info.image_size;
1932 pixel_info=RelinquishVirtualMemory(pixel_info);
1933 pixel_info=rle_info;
1935 bmp_info.compression=BI_RLE8;
1938 Write BMP for Windows, all versions, 14-byte header.
1940 if (image->debug != MagickFalse)
1942 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1943 " Writing BMP version %.20g datastream",(double) type);
1944 if (image->storage_class == DirectClass)
1945 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1946 " Storage class=DirectClass");
1948 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1949 " Storage class=PseudoClass");
1950 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1951 " Image depth=%.20g",(double) image->depth);
1952 if (image->alpha_trait != UndefinedPixelTrait)
1953 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1956 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1957 " Matte=MagickFalse");
1958 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1959 " BMP bits_per_pixel=%.20g",(double) bmp_info.bits_per_pixel);
1960 switch ((int) bmp_info.compression)
1964 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1965 " Compression=BI_RGB");
1970 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1971 " Compression=BI_RLE8");
1976 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1977 " Compression=BI_BITFIELDS");
1982 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1983 " Compression=UNKNOWN (%lu)",bmp_info.compression);
1987 if (bmp_info.number_colors == 0)
1988 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1989 " Number_colors=unspecified");
1991 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1992 " Number_colors=%lu",bmp_info.number_colors);
1994 (void) WriteBlob(image,2,(unsigned char *) "BM");
1995 (void) WriteBlobLSBLong(image,bmp_info.file_size);
1996 (void) WriteBlobLSBLong(image,bmp_info.ba_offset); /* always 0 */
1997 (void) WriteBlobLSBLong(image,bmp_info.offset_bits);
2001 Write 12-byte version 2 bitmap header.
2003 (void) WriteBlobLSBLong(image,bmp_info.size);
2004 (void) WriteBlobLSBSignedShort(image,(signed short) bmp_info.width);
2005 (void) WriteBlobLSBSignedShort(image,(signed short) bmp_info.height);
2006 (void) WriteBlobLSBShort(image,bmp_info.planes);
2007 (void) WriteBlobLSBShort(image,bmp_info.bits_per_pixel);
2012 Write 40-byte version 3+ bitmap header.
2014 (void) WriteBlobLSBLong(image,bmp_info.size);
2015 (void) WriteBlobLSBSignedLong(image,(signed int) bmp_info.width);
2016 (void) WriteBlobLSBSignedLong(image,(signed int) bmp_info.height);
2017 (void) WriteBlobLSBShort(image,bmp_info.planes);
2018 (void) WriteBlobLSBShort(image,bmp_info.bits_per_pixel);
2019 (void) WriteBlobLSBLong(image,bmp_info.compression);
2020 (void) WriteBlobLSBLong(image,bmp_info.image_size);
2021 (void) WriteBlobLSBLong(image,bmp_info.x_pixels);
2022 (void) WriteBlobLSBLong(image,bmp_info.y_pixels);
2023 (void) WriteBlobLSBLong(image,bmp_info.number_colors);
2024 (void) WriteBlobLSBLong(image,bmp_info.colors_important);
2026 if ((type > 3) && ((image->alpha_trait != UndefinedPixelTrait) ||
2027 (have_color_info != MagickFalse)))
2030 Write the rest of the 108-byte BMP Version 4 header.
2032 (void) WriteBlobLSBLong(image,0x00ff0000U); /* Red mask */
2033 (void) WriteBlobLSBLong(image,0x0000ff00U); /* Green mask */
2034 (void) WriteBlobLSBLong(image,0x000000ffU); /* Blue mask */
2035 (void) WriteBlobLSBLong(image,0xff000000U); /* Alpha mask */
2036 (void) WriteBlobLSBLong(image,0x73524742U); /* sRGB */
2037 (void) WriteBlobLSBLong(image,(unsigned int)
2038 (image->chromaticity.red_primary.x*0x40000000));
2039 (void) WriteBlobLSBLong(image,(unsigned int)
2040 (image->chromaticity.red_primary.y*0x40000000));
2041 (void) WriteBlobLSBLong(image,(unsigned int)
2042 ((1.000f-(image->chromaticity.red_primary.x+
2043 image->chromaticity.red_primary.y))*0x40000000));
2044 (void) WriteBlobLSBLong(image,(unsigned int)
2045 (image->chromaticity.green_primary.x*0x40000000));
2046 (void) WriteBlobLSBLong(image,(unsigned int)
2047 (image->chromaticity.green_primary.y*0x40000000));
2048 (void) WriteBlobLSBLong(image,(unsigned int)
2049 ((1.000f-(image->chromaticity.green_primary.x+
2050 image->chromaticity.green_primary.y))*0x40000000));
2051 (void) WriteBlobLSBLong(image,(unsigned int)
2052 (image->chromaticity.blue_primary.x*0x40000000));
2053 (void) WriteBlobLSBLong(image,(unsigned int)
2054 (image->chromaticity.blue_primary.y*0x40000000));
2055 (void) WriteBlobLSBLong(image,(unsigned int)
2056 ((1.000f-(image->chromaticity.blue_primary.x+
2057 image->chromaticity.blue_primary.y))*0x40000000));
2058 (void) WriteBlobLSBLong(image,(unsigned int)
2059 (bmp_info.gamma_scale.x*0x10000));
2060 (void) WriteBlobLSBLong(image,(unsigned int)
2061 (bmp_info.gamma_scale.y*0x10000));
2062 (void) WriteBlobLSBLong(image,(unsigned int)
2063 (bmp_info.gamma_scale.z*0x10000));
2064 if ((image->rendering_intent != UndefinedIntent) ||
2065 (profile != (StringInfo *) NULL))
2070 switch ((int) image->rendering_intent)
2072 case SaturationIntent:
2074 intent=LCS_GM_BUSINESS;
2077 case RelativeIntent:
2079 intent=LCS_GM_GRAPHICS;
2082 case PerceptualIntent:
2084 intent=LCS_GM_IMAGES;
2087 case AbsoluteIntent:
2089 intent=LCS_GM_ABS_COLORIMETRIC;
2098 (void) WriteBlobLSBLong(image,(unsigned int) intent);
2099 (void) WriteBlobLSBLong(image,0x00); /* dummy profile data */
2100 (void) WriteBlobLSBLong(image,0x00); /* dummy profile length */
2101 (void) WriteBlobLSBLong(image,0x00); /* reserved */
2104 if (image->storage_class == PseudoClass)
2110 Dump colormap to file.
2112 if (image->debug != MagickFalse)
2113 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
2114 " Colormap: %.20g entries",(double) image->colors);
2115 bmp_colormap=(unsigned char *) AcquireQuantumMemory((size_t) (1UL <<
2116 bmp_info.bits_per_pixel),4*sizeof(*bmp_colormap));
2117 if (bmp_colormap == (unsigned char *) NULL)
2118 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
2120 for (i=0; i < (ssize_t) MagickMin((ssize_t) image->colors,(ssize_t) bmp_info.number_colors); i++)
2122 *q++=ScaleQuantumToChar(ClampToQuantum(image->colormap[i].blue));
2123 *q++=ScaleQuantumToChar(ClampToQuantum(image->colormap[i].green));
2124 *q++=ScaleQuantumToChar(ClampToQuantum(image->colormap[i].red));
2126 *q++=(unsigned char) 0x0;
2128 for ( ; i < (ssize_t) (1UL << bmp_info.bits_per_pixel); i++)
2130 *q++=(unsigned char) 0x00;
2131 *q++=(unsigned char) 0x00;
2132 *q++=(unsigned char) 0x00;
2134 *q++=(unsigned char) 0x00;
2137 (void) WriteBlob(image,(size_t) (3*(1L << bmp_info.bits_per_pixel)),
2140 (void) WriteBlob(image,(size_t) (4*(1L << bmp_info.bits_per_pixel)),
2142 bmp_colormap=(unsigned char *) RelinquishMagickMemory(bmp_colormap);
2144 if (image->debug != MagickFalse)
2145 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
2146 " Pixels: %lu bytes",bmp_info.image_size);
2147 (void) WriteBlob(image,(size_t) bmp_info.image_size,pixels);
2148 pixel_info=RelinquishVirtualMemory(pixel_info);
2149 if (GetNextImageInList(image) == (Image *) NULL)
2151 image=SyncNextImageInList(image);
2152 status=SetImageProgress(image,SaveImagesTag,scene++,
2153 GetImageListLength(image));
2154 if (status == MagickFalse)
2156 } while (image_info->adjoin != MagickFalse);
2157 (void) CloseBlob(image);