2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
13 % Read/Write Microsoft Windows Bitmap Image Format %
17 % Glenn Randers-Pehrson %
21 % Copyright 1999-2016 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);
1155 if (SyncAuthenticPixels(image,exception) == MagickFalse)
1157 offset=(MagickOffsetType) (image->rows-y-1);
1158 if (image->previous == (Image *) NULL)
1160 status=SetImageProgress(image,LoadImageTag,(MagickOffsetType)
1161 (image->rows-y),image->rows);
1162 if (status == MagickFalse)
1166 (void) SyncImage(image,exception);
1176 Convert bitfield encoded 16-bit PseudoColor scanline.
1178 if (bmp_info.compression != BI_RGB &&
1179 bmp_info.compression != BI_BITFIELDS)
1181 pixel_info=RelinquishVirtualMemory(pixel_info);
1182 ThrowReaderException(CorruptImageError,
1183 "UnrecognizedImageCompression");
1185 bytes_per_line=2*(image->columns+image->columns % 2);
1186 image->storage_class=DirectClass;
1187 for (y=(ssize_t) image->rows-1; y >= 0; y--)
1189 p=pixels+(image->rows-y-1)*bytes_per_line;
1190 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
1191 if (q == (Quantum *) NULL)
1193 for (x=0; x < (ssize_t) image->columns; x++)
1195 pixel=(size_t) (*p++);
1197 red=((pixel & bmp_info.red_mask) << shift.red) >> 16;
1198 if (quantum_bits.red == 5)
1199 red|=((red & 0xe000) >> 5);
1200 if (quantum_bits.red <= 8)
1201 red|=((red & 0xff00) >> 8);
1202 green=((pixel & bmp_info.green_mask) << shift.green) >> 16;
1203 if (quantum_bits.green == 5)
1204 green|=((green & 0xe000) >> 5);
1205 if (quantum_bits.green == 6)
1206 green|=((green & 0xc000) >> 6);
1207 if (quantum_bits.green <= 8)
1208 green|=((green & 0xff00) >> 8);
1209 blue=((pixel & bmp_info.blue_mask) << shift.blue) >> 16;
1210 if (quantum_bits.blue == 5)
1211 blue|=((blue & 0xe000) >> 5);
1212 if (quantum_bits.blue <= 8)
1213 blue|=((blue & 0xff00) >> 8);
1214 SetPixelRed(image,ScaleShortToQuantum((unsigned short) red),q);
1215 SetPixelGreen(image,ScaleShortToQuantum((unsigned short) green),q);
1216 SetPixelBlue(image,ScaleShortToQuantum((unsigned short) blue),q);
1217 SetPixelAlpha(image,OpaqueAlpha,q);
1218 if (image->alpha_trait != UndefinedPixelTrait)
1220 alpha=((pixel & bmp_info.alpha_mask) << shift.alpha) >> 16;
1221 if (quantum_bits.alpha <= 8)
1222 alpha|=((alpha & 0xff00) >> 8);
1223 SetPixelAlpha(image,ScaleShortToQuantum(
1224 (unsigned short) alpha),q);
1226 q+=GetPixelChannels(image);
1228 if (SyncAuthenticPixels(image,exception) == MagickFalse)
1230 offset=(MagickOffsetType) (image->rows-y-1);
1231 if (image->previous == (Image *) NULL)
1233 status=SetImageProgress(image,LoadImageTag,(MagickOffsetType)
1234 (image->rows-y),image->rows);
1235 if (status == MagickFalse)
1244 Convert DirectColor scanline.
1246 bytes_per_line=4*((image->columns*24+31)/32);
1247 for (y=(ssize_t) image->rows-1; y >= 0; y--)
1249 p=pixels+(image->rows-y-1)*bytes_per_line;
1250 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
1251 if (q == (Quantum *) NULL)
1253 for (x=0; x < (ssize_t) image->columns; x++)
1255 SetPixelBlue(image,ScaleCharToQuantum(*p++),q);
1256 SetPixelGreen(image,ScaleCharToQuantum(*p++),q);
1257 SetPixelRed(image,ScaleCharToQuantum(*p++),q);
1258 SetPixelAlpha(image,OpaqueAlpha,q);
1259 q+=GetPixelChannels(image);
1261 if (SyncAuthenticPixels(image,exception) == MagickFalse)
1263 offset=(MagickOffsetType) (image->rows-y-1);
1264 if (image->previous == (Image *) NULL)
1266 status=SetImageProgress(image,LoadImageTag,(MagickOffsetType)
1267 (image->rows-y),image->rows);
1268 if (status == MagickFalse)
1277 Convert bitfield encoded DirectColor scanline.
1279 if ((bmp_info.compression != BI_RGB) &&
1280 (bmp_info.compression != BI_BITFIELDS))
1282 pixel_info=RelinquishVirtualMemory(pixel_info);
1283 ThrowReaderException(CorruptImageError,
1284 "UnrecognizedImageCompression");
1286 bytes_per_line=4*(image->columns);
1287 for (y=(ssize_t) image->rows-1; y >= 0; y--)
1293 p=pixels+(image->rows-y-1)*bytes_per_line;
1294 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
1295 if (q == (Quantum *) NULL)
1297 for (x=0; x < (ssize_t) image->columns; x++)
1299 pixel=(size_t) (*p++);
1300 pixel|=((size_t) *p++ << 8);
1301 pixel|=((size_t) *p++ << 16);
1302 pixel|=((size_t) *p++ << 24);
1303 red=((pixel & bmp_info.red_mask) << shift.red) >> 16;
1304 if (quantum_bits.red == 8)
1306 green=((pixel & bmp_info.green_mask) << shift.green) >> 16;
1307 if (quantum_bits.green == 8)
1308 green|=(green >> 8);
1309 blue=((pixel & bmp_info.blue_mask) << shift.blue) >> 16;
1310 if (quantum_bits.blue == 8)
1312 SetPixelRed(image,ScaleShortToQuantum((unsigned short) red),q);
1313 SetPixelGreen(image,ScaleShortToQuantum((unsigned short) green),q);
1314 SetPixelBlue(image,ScaleShortToQuantum((unsigned short) blue),q);
1315 SetPixelAlpha(image,OpaqueAlpha,q);
1316 if (image->alpha_trait != UndefinedPixelTrait)
1318 alpha=((pixel & bmp_info.alpha_mask) << shift.alpha) >> 16;
1319 if (quantum_bits.alpha == 8)
1320 alpha|=(alpha >> 8);
1321 SetPixelAlpha(image,ScaleShortToQuantum(
1322 (unsigned short) alpha),q);
1324 q+=GetPixelChannels(image);
1326 if (SyncAuthenticPixels(image,exception) == MagickFalse)
1328 offset=(MagickOffsetType) (image->rows-y-1);
1329 if (image->previous == (Image *) NULL)
1331 status=SetImageProgress(image,LoadImageTag,(MagickOffsetType)
1332 (image->rows-y),image->rows);
1333 if (status == MagickFalse)
1341 pixel_info=RelinquishVirtualMemory(pixel_info);
1342 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
1345 pixel_info=RelinquishVirtualMemory(pixel_info);
1348 if (EOFBlob(image) != MagickFalse)
1350 ThrowFileException(exception,CorruptImageError,"UnexpectedEndOfFile",
1354 if (bmp_info.height < 0)
1360 Correct image orientation.
1362 flipped_image=FlipImage(image,exception);
1363 if (flipped_image != (Image *) NULL)
1365 DuplicateBlob(flipped_image,image);
1366 image=DestroyImage(image);
1367 image=flipped_image;
1371 Proceed to next image.
1373 if (image_info->number_scenes != 0)
1374 if (image->scene >= (image_info->scene+image_info->number_scenes-1))
1377 if (bmp_info.ba_offset != 0)
1379 offset=SeekBlob(image,(MagickOffsetType) bmp_info.ba_offset,SEEK_SET);
1381 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
1383 count=ReadBlob(image,2,magick);
1384 if ((count == 2) && (IsBMP(magick,2) != MagickFalse))
1387 Acquire next image structure.
1389 AcquireNextImage(image_info,image,exception);
1390 if (GetNextImageInList(image) == (Image *) NULL)
1392 image=DestroyImageList(image);
1393 return((Image *) NULL);
1395 image=SyncNextImageInList(image);
1396 status=SetImageProgress(image,LoadImagesTag,TellBlob(image),
1397 GetBlobSize(image));
1398 if (status == MagickFalse)
1401 } while (IsBMP(magick,2) != MagickFalse);
1402 (void) CloseBlob(image);
1403 return(GetFirstImageInList(image));
1407 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1411 % R e g i s t e r B M P I m a g e %
1415 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1417 % RegisterBMPImage() adds attributes for the BMP image format to
1418 % the list of supported formats. The attributes include the image format
1419 % tag, a method to read and/or write the format, whether the format
1420 % supports the saving of more than one frame to the same file or blob,
1421 % whether the format supports native in-memory I/O, and a brief
1422 % description of the format.
1424 % The format of the RegisterBMPImage method is:
1426 % size_t RegisterBMPImage(void)
1429 ModuleExport size_t RegisterBMPImage(void)
1434 entry=AcquireMagickInfo("BMP","BMP","Microsoft Windows bitmap image");
1435 entry->decoder=(DecodeImageHandler *) ReadBMPImage;
1436 entry->encoder=(EncodeImageHandler *) WriteBMPImage;
1437 entry->magick=(IsImageFormatHandler *) IsBMP;
1438 entry->flags^=CoderAdjoinFlag;
1439 entry->flags|=CoderSeekableStreamFlag;
1440 (void) RegisterMagickInfo(entry);
1441 entry=AcquireMagickInfo("BMP","BMP2","Microsoft Windows bitmap image (V2)");
1442 entry->encoder=(EncodeImageHandler *) WriteBMPImage;
1443 entry->magick=(IsImageFormatHandler *) IsBMP;
1444 entry->flags^=CoderAdjoinFlag;
1445 entry->flags|=CoderSeekableStreamFlag;
1446 (void) RegisterMagickInfo(entry);
1447 entry=AcquireMagickInfo("BMP","BMP3","Microsoft Windows bitmap image (V3)");
1448 entry->encoder=(EncodeImageHandler *) WriteBMPImage;
1449 entry->magick=(IsImageFormatHandler *) IsBMP;
1450 entry->flags^=CoderAdjoinFlag;
1451 entry->flags|=CoderSeekableStreamFlag;
1452 (void) RegisterMagickInfo(entry);
1453 return(MagickImageCoderSignature);
1457 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1461 % U n r e g i s t e r B M P I m a g e %
1465 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1467 % UnregisterBMPImage() removes format registrations made by the
1468 % BMP module from the list of supported formats.
1470 % The format of the UnregisterBMPImage method is:
1472 % UnregisterBMPImage(void)
1475 ModuleExport void UnregisterBMPImage(void)
1477 (void) UnregisterMagickInfo("BMP");
1478 (void) UnregisterMagickInfo("BMP2");
1479 (void) UnregisterMagickInfo("BMP3");
1483 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1487 % W r i t e B M P I m a g e %
1491 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1493 % WriteBMPImage() writes an image in Microsoft Windows bitmap encoded
1494 % image format, version 3 for Windows or (if the image has a matte channel)
1497 % The format of the WriteBMPImage method is:
1499 % MagickBooleanType WriteBMPImage(const ImageInfo *image_info,
1500 % Image *image,ExceptionInfo *exception)
1502 % A description of each parameter follows.
1504 % o image_info: the image info.
1506 % o image: The image.
1508 % o exception: return any errors or warnings in this structure.
1511 static MagickBooleanType WriteBMPImage(const ImageInfo *image_info,Image *image,
1512 ExceptionInfo *exception)
1533 register const Quantum
1540 register unsigned char
1555 Open output image file.
1557 assert(image_info != (const ImageInfo *) NULL);
1558 assert(image_info->signature == MagickCoreSignature);
1559 assert(image != (Image *) NULL);
1560 assert(image->signature == MagickCoreSignature);
1561 if (image->debug != MagickFalse)
1562 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1563 assert(exception != (ExceptionInfo *) NULL);
1564 assert(exception->signature == MagickCoreSignature);
1565 status=OpenBlob(image_info,image,WriteBinaryBlobMode,exception);
1566 if (status == MagickFalse)
1569 if (LocaleCompare(image_info->magick,"BMP2") == 0)
1572 if (LocaleCompare(image_info->magick,"BMP3") == 0)
1575 option=GetImageOption(image_info,"bmp:format");
1576 if (option != (char *) NULL)
1578 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1579 " Format=%s",option);
1581 if (LocaleCompare(option,"bmp2") == 0)
1583 if (LocaleCompare(option,"bmp3") == 0)
1585 if (LocaleCompare(option,"bmp4") == 0)
1593 Initialize BMP raster file header.
1595 (void) TransformImageColorspace(image,sRGBColorspace,exception);
1596 (void) ResetMagickMemory(&bmp_info,0,sizeof(bmp_info));
1597 bmp_info.file_size=14+12;
1599 bmp_info.file_size+=28;
1600 bmp_info.offset_bits=bmp_info.file_size;
1601 bmp_info.compression=BI_RGB;
1602 if ((image->storage_class == PseudoClass) && (image->colors > 256))
1603 (void) SetImageStorageClass(image,DirectClass,exception);
1604 if (image->storage_class != DirectClass)
1607 Colormapped BMP raster.
1609 bmp_info.bits_per_pixel=8;
1610 if (image->colors <= 2)
1611 bmp_info.bits_per_pixel=1;
1613 if (image->colors <= 16)
1614 bmp_info.bits_per_pixel=4;
1616 if (image->colors <= 256)
1617 bmp_info.bits_per_pixel=8;
1618 if (image_info->compression == RLECompression)
1619 bmp_info.bits_per_pixel=8;
1620 bmp_info.number_colors=1U << bmp_info.bits_per_pixel;
1621 if (image->alpha_trait != UndefinedPixelTrait)
1622 (void) SetImageStorageClass(image,DirectClass,exception);
1624 if ((size_t) bmp_info.number_colors < image->colors)
1625 (void) SetImageStorageClass(image,DirectClass,exception);
1628 bmp_info.file_size+=3*(1UL << bmp_info.bits_per_pixel);
1629 bmp_info.offset_bits+=3*(1UL << bmp_info.bits_per_pixel);
1632 bmp_info.file_size+=(1UL << bmp_info.bits_per_pixel);
1633 bmp_info.offset_bits+=(1UL << bmp_info.bits_per_pixel);
1637 if (image->storage_class == DirectClass)
1640 Full color BMP raster.
1642 bmp_info.number_colors=0;
1643 bmp_info.bits_per_pixel=(unsigned short)
1644 ((type > 3) && (image->alpha_trait != UndefinedPixelTrait) ? 32 : 24);
1645 bmp_info.compression=(unsigned int) ((type > 3) &&
1646 (image->alpha_trait != UndefinedPixelTrait) ? BI_BITFIELDS : BI_RGB);
1647 if ((type == 3) && (image->alpha_trait != UndefinedPixelTrait))
1649 option=GetImageOption(image_info,"bmp3:alpha");
1650 if (IsStringTrue(option))
1651 bmp_info.bits_per_pixel=32;
1654 bytes_per_line=4*((image->columns*bmp_info.bits_per_pixel+31)/32);
1655 bmp_info.ba_offset=0;
1656 profile=GetImageProfile(image,"icc");
1657 have_color_info=(image->rendering_intent != UndefinedIntent) ||
1658 (profile != (StringInfo *) NULL) || (image->gamma != 0.0) ? MagickTrue :
1663 if ((type == 3) || ((image->alpha_trait == UndefinedPixelTrait) &&
1664 (have_color_info == MagickFalse)))
1676 if ((image->rendering_intent != UndefinedIntent) ||
1677 (profile != (StringInfo *) NULL))
1682 bmp_info.file_size+=extra_size;
1683 bmp_info.offset_bits+=extra_size;
1685 bmp_info.width=(ssize_t) image->columns;
1686 bmp_info.height=(ssize_t) image->rows;
1688 bmp_info.image_size=(unsigned int) (bytes_per_line*image->rows);
1689 bmp_info.file_size+=bmp_info.image_size;
1690 bmp_info.x_pixels=75*39;
1691 bmp_info.y_pixels=75*39;
1692 switch (image->units)
1694 case UndefinedResolution:
1695 case PixelsPerInchResolution:
1697 bmp_info.x_pixels=(unsigned int) (100.0*image->resolution.x/2.54);
1698 bmp_info.y_pixels=(unsigned int) (100.0*image->resolution.y/2.54);
1701 case PixelsPerCentimeterResolution:
1703 bmp_info.x_pixels=(unsigned int) (100.0*image->resolution.x);
1704 bmp_info.y_pixels=(unsigned int) (100.0*image->resolution.y);
1708 bmp_info.colors_important=bmp_info.number_colors;
1710 Convert MIFF to BMP raster pixels.
1712 pixel_info=AcquireVirtualMemory((size_t) bmp_info.image_size,
1714 if (pixel_info == (MemoryInfo *) NULL)
1715 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
1716 pixels=(unsigned char *) GetVirtualMemoryBlob(pixel_info);
1717 (void) ResetMagickMemory(pixels,0,(size_t) bmp_info.image_size);
1718 switch (bmp_info.bits_per_pixel)
1727 Convert PseudoClass image to a BMP monochrome image.
1729 for (y=0; y < (ssize_t) image->rows; y++)
1734 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
1735 if (p == (const Quantum *) NULL)
1737 q=pixels+(image->rows-y-1)*bytes_per_line;
1740 for (x=0; x < (ssize_t) image->columns; x++)
1743 byte|=GetPixelIndex(image,p) != 0 ? 0x01 : 0x00;
1747 *q++=(unsigned char) byte;
1751 p+=GetPixelChannels(image);
1755 *q++=(unsigned char) (byte << (8-bit));
1758 offset=(ssize_t) (image->columns+7)/8;
1759 for (x=offset; x < (ssize_t) bytes_per_line; x++)
1761 if (image->previous == (Image *) NULL)
1763 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
1765 if (status == MagickFalse)
1781 Convert PseudoClass image to a BMP monochrome image.
1783 for (y=0; y < (ssize_t) image->rows; y++)
1785 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
1786 if (p == (const Quantum *) NULL)
1788 q=pixels+(image->rows-y-1)*bytes_per_line;
1791 for (x=0; x < (ssize_t) image->columns; x++)
1794 byte|=((size_t) GetPixelIndex(image,p) & 0x0f);
1798 *q++=(unsigned char) byte;
1802 p+=GetPixelChannels(image);
1806 *q++=(unsigned char) (byte << 4);
1809 offset=(ssize_t) (image->columns+1)/2;
1810 for (x=offset; x < (ssize_t) bytes_per_line; x++)
1812 if (image->previous == (Image *) NULL)
1814 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
1816 if (status == MagickFalse)
1825 Convert PseudoClass packet to BMP pixel.
1827 for (y=0; y < (ssize_t) image->rows; y++)
1829 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
1830 if (p == (const Quantum *) NULL)
1832 q=pixels+(image->rows-y-1)*bytes_per_line;
1833 for (x=0; x < (ssize_t) image->columns; x++)
1835 *q++=(unsigned char) GetPixelIndex(image,p);
1836 p+=GetPixelChannels(image);
1838 for ( ; x < (ssize_t) bytes_per_line; x++)
1840 if (image->previous == (Image *) NULL)
1842 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
1844 if (status == MagickFalse)
1853 Convert DirectClass packet to BMP BGR888.
1855 for (y=0; y < (ssize_t) image->rows; y++)
1857 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
1858 if (p == (const Quantum *) NULL)
1860 q=pixels+(image->rows-y-1)*bytes_per_line;
1861 for (x=0; x < (ssize_t) image->columns; x++)
1863 *q++=ScaleQuantumToChar(GetPixelBlue(image,p));
1864 *q++=ScaleQuantumToChar(GetPixelGreen(image,p));
1865 *q++=ScaleQuantumToChar(GetPixelRed(image,p));
1866 p+=GetPixelChannels(image);
1868 for (x=3L*(ssize_t) image->columns; x < (ssize_t) bytes_per_line; x++)
1870 if (image->previous == (Image *) NULL)
1872 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
1874 if (status == MagickFalse)
1883 Convert DirectClass packet to ARGB8888 pixel.
1885 for (y=0; y < (ssize_t) image->rows; y++)
1887 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
1888 if (p == (const Quantum *) NULL)
1890 q=pixels+(image->rows-y-1)*bytes_per_line;
1891 for (x=0; x < (ssize_t) image->columns; x++)
1893 *q++=ScaleQuantumToChar(GetPixelBlue(image,p));
1894 *q++=ScaleQuantumToChar(GetPixelGreen(image,p));
1895 *q++=ScaleQuantumToChar(GetPixelRed(image,p));
1896 *q++=ScaleQuantumToChar(GetPixelAlpha(image,p));
1897 p+=GetPixelChannels(image);
1899 if (image->previous == (Image *) NULL)
1901 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
1903 if (status == MagickFalse)
1910 if ((type > 2) && (bmp_info.bits_per_pixel == 8))
1911 if (image_info->compression != NoCompression)
1917 Convert run-length encoded raster pixels.
1919 rle_info=AcquireVirtualMemory((size_t) (2*(bytes_per_line+2)+2),
1920 (image->rows+2)*sizeof(*pixels));
1921 if (rle_info == (MemoryInfo *) NULL)
1923 pixel_info=RelinquishVirtualMemory(pixel_info);
1924 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
1926 bmp_data=(unsigned char *) GetVirtualMemoryBlob(rle_info);
1927 bmp_info.file_size-=bmp_info.image_size;
1928 bmp_info.image_size=(unsigned int) EncodeImage(image,bytes_per_line,
1930 bmp_info.file_size+=bmp_info.image_size;
1931 pixel_info=RelinquishVirtualMemory(pixel_info);
1932 pixel_info=rle_info;
1934 bmp_info.compression=BI_RLE8;
1937 Write BMP for Windows, all versions, 14-byte header.
1939 if (image->debug != MagickFalse)
1941 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1942 " Writing BMP version %.20g datastream",(double) type);
1943 if (image->storage_class == DirectClass)
1944 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1945 " Storage class=DirectClass");
1947 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1948 " Storage class=PseudoClass");
1949 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1950 " Image depth=%.20g",(double) image->depth);
1951 if (image->alpha_trait != UndefinedPixelTrait)
1952 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1955 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1956 " Matte=MagickFalse");
1957 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1958 " BMP bits_per_pixel=%.20g",(double) bmp_info.bits_per_pixel);
1959 switch ((int) bmp_info.compression)
1963 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1964 " Compression=BI_RGB");
1969 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1970 " Compression=BI_RLE8");
1975 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1976 " Compression=BI_BITFIELDS");
1981 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1982 " Compression=UNKNOWN (%lu)",bmp_info.compression);
1986 if (bmp_info.number_colors == 0)
1987 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1988 " Number_colors=unspecified");
1990 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1991 " Number_colors=%lu",bmp_info.number_colors);
1993 (void) WriteBlob(image,2,(unsigned char *) "BM");
1994 (void) WriteBlobLSBLong(image,bmp_info.file_size);
1995 (void) WriteBlobLSBLong(image,bmp_info.ba_offset); /* always 0 */
1996 (void) WriteBlobLSBLong(image,bmp_info.offset_bits);
2000 Write 12-byte version 2 bitmap header.
2002 (void) WriteBlobLSBLong(image,bmp_info.size);
2003 (void) WriteBlobLSBSignedShort(image,bmp_info.width);
2004 (void) WriteBlobLSBSignedShort(image,bmp_info.height);
2005 (void) WriteBlobLSBShort(image,bmp_info.planes);
2006 (void) WriteBlobLSBShort(image,bmp_info.bits_per_pixel);
2011 Write 40-byte version 3+ bitmap header.
2013 (void) WriteBlobLSBLong(image,bmp_info.size);
2014 (void) WriteBlobLSBSignedLong(image,bmp_info.width);
2015 (void) WriteBlobLSBSignedLong(image,bmp_info.height);
2016 (void) WriteBlobLSBShort(image,bmp_info.planes);
2017 (void) WriteBlobLSBShort(image,bmp_info.bits_per_pixel);
2018 (void) WriteBlobLSBLong(image,bmp_info.compression);
2019 (void) WriteBlobLSBLong(image,bmp_info.image_size);
2020 (void) WriteBlobLSBLong(image,bmp_info.x_pixels);
2021 (void) WriteBlobLSBLong(image,bmp_info.y_pixels);
2022 (void) WriteBlobLSBLong(image,bmp_info.number_colors);
2023 (void) WriteBlobLSBLong(image,bmp_info.colors_important);
2025 if ((type > 3) && ((image->alpha_trait != UndefinedPixelTrait) ||
2026 (have_color_info != MagickFalse)))
2029 Write the rest of the 108-byte BMP Version 4 header.
2031 (void) WriteBlobLSBLong(image,0x00ff0000U); /* Red mask */
2032 (void) WriteBlobLSBLong(image,0x0000ff00U); /* Green mask */
2033 (void) WriteBlobLSBLong(image,0x000000ffU); /* Blue mask */
2034 (void) WriteBlobLSBLong(image,0xff000000U); /* Alpha mask */
2035 (void) WriteBlobLSBLong(image,0x73524742U); /* sRGB */
2036 (void) WriteBlobLSBLong(image,(unsigned int)
2037 (image->chromaticity.red_primary.x*0x40000000));
2038 (void) WriteBlobLSBLong(image,(unsigned int)
2039 (image->chromaticity.red_primary.y*0x40000000));
2040 (void) WriteBlobLSBLong(image,(unsigned int)
2041 ((1.000f-(image->chromaticity.red_primary.x+
2042 image->chromaticity.red_primary.y))*0x40000000));
2043 (void) WriteBlobLSBLong(image,(unsigned int)
2044 (image->chromaticity.green_primary.x*0x40000000));
2045 (void) WriteBlobLSBLong(image,(unsigned int)
2046 (image->chromaticity.green_primary.y*0x40000000));
2047 (void) WriteBlobLSBLong(image,(unsigned int)
2048 ((1.000f-(image->chromaticity.green_primary.x+
2049 image->chromaticity.green_primary.y))*0x40000000));
2050 (void) WriteBlobLSBLong(image,(unsigned int)
2051 (image->chromaticity.blue_primary.x*0x40000000));
2052 (void) WriteBlobLSBLong(image,(unsigned int)
2053 (image->chromaticity.blue_primary.y*0x40000000));
2054 (void) WriteBlobLSBLong(image,(unsigned int)
2055 ((1.000f-(image->chromaticity.blue_primary.x+
2056 image->chromaticity.blue_primary.y))*0x40000000));
2057 (void) WriteBlobLSBLong(image,(unsigned int)
2058 (bmp_info.gamma_scale.x*0x10000));
2059 (void) WriteBlobLSBLong(image,(unsigned int)
2060 (bmp_info.gamma_scale.y*0x10000));
2061 (void) WriteBlobLSBLong(image,(unsigned int)
2062 (bmp_info.gamma_scale.z*0x10000));
2063 if ((image->rendering_intent != UndefinedIntent) ||
2064 (profile != (StringInfo *) NULL))
2069 switch ((int) image->rendering_intent)
2071 case SaturationIntent:
2073 intent=LCS_GM_BUSINESS;
2076 case RelativeIntent:
2078 intent=LCS_GM_GRAPHICS;
2081 case PerceptualIntent:
2083 intent=LCS_GM_IMAGES;
2086 case AbsoluteIntent:
2088 intent=LCS_GM_ABS_COLORIMETRIC;
2097 (void) WriteBlobLSBLong(image,(unsigned int) intent);
2098 (void) WriteBlobLSBLong(image,0x00); /* dummy profile data */
2099 (void) WriteBlobLSBLong(image,0x00); /* dummy profile length */
2100 (void) WriteBlobLSBLong(image,0x00); /* reserved */
2103 if (image->storage_class == PseudoClass)
2109 Dump colormap to file.
2111 if (image->debug != MagickFalse)
2112 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
2113 " Colormap: %.20g entries",(double) image->colors);
2114 bmp_colormap=(unsigned char *) AcquireQuantumMemory((size_t) (1UL <<
2115 bmp_info.bits_per_pixel),4*sizeof(*bmp_colormap));
2116 if (bmp_colormap == (unsigned char *) NULL)
2117 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
2119 for (i=0; i < (ssize_t) MagickMin((ssize_t) image->colors,(ssize_t) bmp_info.number_colors); i++)
2121 *q++=ScaleQuantumToChar(ClampToQuantum(image->colormap[i].blue));
2122 *q++=ScaleQuantumToChar(ClampToQuantum(image->colormap[i].green));
2123 *q++=ScaleQuantumToChar(ClampToQuantum(image->colormap[i].red));
2125 *q++=(unsigned char) 0x0;
2127 for ( ; i < (ssize_t) (1UL << bmp_info.bits_per_pixel); i++)
2129 *q++=(unsigned char) 0x00;
2130 *q++=(unsigned char) 0x00;
2131 *q++=(unsigned char) 0x00;
2133 *q++=(unsigned char) 0x00;
2136 (void) WriteBlob(image,(size_t) (3*(1L << bmp_info.bits_per_pixel)),
2139 (void) WriteBlob(image,(size_t) (4*(1L << bmp_info.bits_per_pixel)),
2141 bmp_colormap=(unsigned char *) RelinquishMagickMemory(bmp_colormap);
2143 if (image->debug != MagickFalse)
2144 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
2145 " Pixels: %lu bytes",bmp_info.image_size);
2146 (void) WriteBlob(image,(size_t) bmp_info.image_size,pixels);
2147 pixel_info=RelinquishVirtualMemory(pixel_info);
2148 if (GetNextImageInList(image) == (Image *) NULL)
2150 image=SyncNextImageInList(image);
2151 status=SetImageProgress(image,SaveImagesTag,scene++,
2152 GetImageListLength(image));
2153 if (status == MagickFalse)
2155 } while (image_info->adjoin != MagickFalse);
2156 (void) CloseBlob(image);