2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
13 % Read/Write Windows DIB Image Format %
20 % Copyright 1999-2014 ImageMagick Studio LLC, a non-profit organization %
21 % dedicated to making software imaging solutions freely available. %
23 % You may not use this file except in compliance with the License. You may %
24 % obtain a copy of the License at %
26 % http://www.imagemagick.org/script/license.php %
28 % Unless required by applicable law or agreed to in writing, software %
29 % distributed under the License is distributed on an "AS IS" BASIS, %
30 % WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. %
31 % See the License for the specific language governing permissions and %
32 % limitations under the License. %
34 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
42 #include "MagickCore/studio.h"
43 #include "MagickCore/attribute.h"
44 #include "MagickCore/blob.h"
45 #include "MagickCore/blob-private.h"
46 #include "MagickCore/cache.h"
47 #include "MagickCore/color.h"
48 #include "MagickCore/color-private.h"
49 #include "MagickCore/colormap.h"
50 #include "MagickCore/colormap-private.h"
51 #include "MagickCore/colorspace.h"
52 #include "MagickCore/colorspace-private.h"
53 #include "MagickCore/draw.h"
54 #include "MagickCore/exception.h"
55 #include "MagickCore/exception-private.h"
56 #include "MagickCore/geometry.h"
57 #include "MagickCore/image.h"
58 #include "MagickCore/image-private.h"
59 #include "MagickCore/list.h"
60 #include "MagickCore/log.h"
61 #include "MagickCore/magick.h"
62 #include "MagickCore/memory_.h"
63 #include "MagickCore/monitor.h"
64 #include "MagickCore/monitor-private.h"
65 #include "MagickCore/pixel-accessor.h"
66 #include "MagickCore/quantum-private.h"
67 #include "MagickCore/static.h"
68 #include "MagickCore/string_.h"
69 #include "MagickCore/module.h"
70 #include "MagickCore/transform.h"
75 typedef struct _DIBInfo
111 Forward declarations.
113 static MagickBooleanType
114 WriteDIBImage(const ImageInfo *,Image *,ExceptionInfo *);
117 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
121 % D e c o d e I m a g e %
125 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
127 % DecodeImage unpacks the packed image pixels into runlength-encoded
130 % The format of the DecodeImage method is:
132 % MagickBooleanType DecodeImage(Image *image,
133 % const MagickBooleanType compression,unsigned char *pixels)
135 % A description of each parameter follows:
137 % o image: the address of a structure of type Image.
139 % o compression: A value of 1 means the compressed pixels are runlength
140 % encoded for a 256-color bitmap. A value of 2 means a 16-color bitmap.
142 % o pixels: The address of a byte (8 bits) array of pixel data created by
143 % the decoding process.
147 static inline size_t MagickMin(const size_t x,const size_t y)
154 static MagickBooleanType DecodeImage(Image *image,
155 const MagickBooleanType compression,unsigned char *pixels)
157 #if !defined(MAGICKCORE_WINDOWS_SUPPORT) || defined(__MINGW32__) || defined(__MINGW64__)
161 #define BI_BITFIELDS 3
174 register unsigned char
181 assert(image != (Image *) NULL);
182 assert(image->signature == MagickSignature);
183 if (image->debug != MagickFalse)
184 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
185 assert(pixels != (unsigned char *) NULL);
186 (void) ResetMagickMemory(pixels,0,(size_t) image->columns*image->rows*
191 q=pixels+(size_t) image->columns*image->rows;
192 for (y=0; y < (ssize_t) image->rows; )
194 if ((p < pixels) || (p >= q))
196 count=ReadBlobByte(image);
201 count=(int) MagickMin((size_t) count,(size_t) (q-p));
205 byte=(unsigned char) ReadBlobByte(image);
206 if (compression == BI_RLE8)
208 for (i=0; i < count; i++)
209 *p++=(unsigned char) byte;
213 for (i=0; i < count; i++)
215 ((i & 0x01) != 0 ? (byte & 0x0f) : ((byte >> 4) & 0x0f));
224 count=ReadBlobByte(image);
236 p=pixels+y*image->columns;
244 x+=ReadBlobByte(image);
245 y+=ReadBlobByte(image);
246 p=pixels+y*image->columns+x;
254 count=(int) MagickMin((size_t) count,(size_t) (q-p));
255 if (compression == BI_RLE8)
256 for (i=0; i < count; i++)
257 *p++=(unsigned char) ReadBlobByte(image);
259 for (i=0; i < count; i++)
262 byte=(unsigned char) ReadBlobByte(image);
264 ((i & 0x01) != 0 ? (byte & 0x0f) : ((byte >> 4) & 0x0f));
270 if (compression == BI_RLE8)
272 if ((count & 0x01) != 0)
273 (void) ReadBlobByte(image);
276 if (((count & 0x03) == 1) || ((count & 0x03) == 2))
277 (void) ReadBlobByte(image);
282 if (SetImageProgress(image,LoadImageTag,y,image->rows) == MagickFalse)
285 (void) ReadBlobByte(image); /* end of line */
286 (void) ReadBlobByte(image);
291 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
295 % E n c o d e I m a g e %
299 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
301 % EncodeImage compresses pixels using a runlength encoded format.
303 % The format of the EncodeImage method is:
305 % static MagickBooleanType EncodeImage(Image *image,
306 % const size_t bytes_per_line,const unsigned char *pixels,
307 % unsigned char *compressed_pixels)
309 % A description of each parameter follows:
311 % o image: The image.
313 % o bytes_per_line: the number of bytes in a scanline of compressed pixels
315 % o pixels: The address of a byte (8 bits) array of pixel data created by
316 % the compression process.
318 % o compressed_pixels: The address of a byte (8 bits) array of compressed
322 static size_t EncodeImage(Image *image,const size_t bytes_per_line,
323 const unsigned char *pixels,unsigned char *compressed_pixels)
328 register const unsigned char
335 register unsigned char
339 Runlength encode pixels.
341 assert(image != (Image *) NULL);
342 assert(image->signature == MagickSignature);
343 if (image->debug != MagickFalse)
344 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
345 assert(pixels != (const unsigned char *) NULL);
346 assert(compressed_pixels != (unsigned char *) NULL);
350 for (y=0; y < (ssize_t) image->rows; y++)
352 for (x=0; x < (ssize_t) bytes_per_line; x+=i)
357 for (i=1; ((x+i) < (ssize_t) bytes_per_line); i++)
358 if ((*(p+i) != *p) || (i == 255))
360 *q++=(unsigned char) i;
369 if (SetImageProgress(image,LoadImageTag,y,image->rows) == MagickFalse)
377 return((size_t) (q-compressed_pixels));
381 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
389 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
391 % IsDIB() returns MagickTrue if the image format type, identified by the
392 % magick string, is DIB.
394 % The format of the IsDIB method is:
396 % MagickBooleanType IsDIB(const unsigned char *magick,const size_t length)
398 % A description of each parameter follows:
400 % o magick: compare image format pattern against these bytes.
402 % o length: Specifies the length of the magick string.
405 static MagickBooleanType IsDIB(const unsigned char *magick,const size_t length)
409 if (memcmp(magick,"\050\000",2) == 0)
415 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
419 % R e a d D I B I m a g e %
423 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
425 % ReadDIBImage() reads a Microsoft Windows bitmap image file and
426 % returns it. It allocates the memory necessary for the new Image structure
427 % and returns a pointer to the new image.
429 % The format of the ReadDIBImage method is:
431 % image=ReadDIBImage(image_info)
433 % A description of each parameter follows:
435 % o image_info: the image info.
437 % o exception: return any errors or warnings in this structure.
441 static inline ssize_t MagickAbsoluteValue(const ssize_t x)
448 static inline size_t MagickMax(const size_t x,const size_t y)
455 static Image *ReadDIBImage(const ImageInfo *image_info,ExceptionInfo *exception)
481 register unsigned char
500 assert(image_info != (const ImageInfo *) NULL);
501 assert(image_info->signature == MagickSignature);
502 if (image_info->debug != MagickFalse)
503 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
504 image_info->filename);
505 assert(exception != (ExceptionInfo *) NULL);
506 assert(exception->signature == MagickSignature);
507 image=AcquireImage(image_info,exception);
508 status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception);
509 if (status == MagickFalse)
511 image=DestroyImageList(image);
512 return((Image *) NULL);
515 Determine if this a DIB file.
517 (void) ResetMagickMemory(&dib_info,0,sizeof(dib_info));
518 dib_info.size=ReadBlobLSBLong(image);
519 if (dib_info.size!=40)
520 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
522 Microsoft Windows 3.X DIB image file.
524 dib_info.width=(short) ReadBlobLSBLong(image);
525 dib_info.height=(short) ReadBlobLSBLong(image);
526 dib_info.planes=ReadBlobLSBShort(image);
527 dib_info.bits_per_pixel=ReadBlobLSBShort(image);
528 dib_info.compression=ReadBlobLSBLong(image);
529 dib_info.image_size=ReadBlobLSBLong(image);
530 dib_info.x_pixels=ReadBlobLSBLong(image);
531 dib_info.y_pixels=ReadBlobLSBLong(image);
532 dib_info.number_colors=ReadBlobLSBLong(image);
533 dib_info.colors_important=ReadBlobLSBLong(image);
534 if ((dib_info.compression == BI_BITFIELDS) &&
535 ((dib_info.bits_per_pixel == 16) || (dib_info.bits_per_pixel == 32)))
537 dib_info.red_mask=ReadBlobLSBLong(image);
538 dib_info.green_mask=ReadBlobLSBLong(image);
539 dib_info.blue_mask=ReadBlobLSBLong(image);
541 image->alpha_trait=dib_info.bits_per_pixel == 32 ? BlendPixelTrait :
543 image->columns=(size_t) MagickAbsoluteValue(dib_info.width);
544 image->rows=(size_t) MagickAbsoluteValue(dib_info.height);
546 if ((dib_info.number_colors != 0) || (dib_info.bits_per_pixel < 16))
551 image->storage_class=PseudoClass;
552 image->colors=dib_info.number_colors;
554 if (image->colors == 0)
555 image->colors=one << dib_info.bits_per_pixel;
557 if (image_info->size)
565 flags=ParseAbsoluteGeometry(image_info->size,&geometry);
566 if (flags & WidthValue)
567 if ((geometry.width != 0) && (geometry.width < image->columns))
568 image->columns=geometry.width;
569 if (flags & HeightValue)
570 if ((geometry.height != 0) && (geometry.height < image->rows))
571 image->rows=geometry.height;
573 if (image->storage_class == PseudoClass)
583 Read DIB raster colormap.
585 if (AcquireImageColormap(image,image->colors,exception) == MagickFalse)
586 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
587 length=(size_t) image->colors;
588 dib_colormap=(unsigned char *) AcquireQuantumMemory(length,
589 4*sizeof(*dib_colormap));
590 if (dib_colormap == (unsigned char *) NULL)
591 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
593 count=ReadBlob(image,packet_size*image->colors,dib_colormap);
594 if (count != (ssize_t) (packet_size*image->colors))
595 ThrowReaderException(CorruptImageError,"InsufficientImageDataInFile");
597 for (i=0; i < (ssize_t) image->colors; i++)
599 image->colormap[i].blue=ScaleCharToQuantum(*p++);
600 image->colormap[i].green=ScaleCharToQuantum(*p++);
601 image->colormap[i].red=ScaleCharToQuantum(*p++);
602 if (packet_size == 4)
605 dib_colormap=(unsigned char *) RelinquishMagickMemory(dib_colormap);
610 if (dib_info.compression == BI_RLE4)
611 dib_info.bits_per_pixel<<=1;
612 bytes_per_line=4*((image->columns*dib_info.bits_per_pixel+31)/32);
613 length=bytes_per_line*image->rows;
614 pixel_info=AcquireVirtualMemory((size_t) image->rows,MagickMax(
615 bytes_per_line,image->columns+256UL)*sizeof(*pixels));
616 if (pixel_info == (MemoryInfo *) NULL)
617 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
618 pixels=(unsigned char *) GetVirtualMemoryBlob(pixel_info);
619 if ((dib_info.compression == BI_RGB) ||
620 (dib_info.compression == BI_BITFIELDS))
622 count=ReadBlob(image,length,pixels);
623 if (count != (ssize_t) (length))
624 ThrowReaderException(CorruptImageError,"InsufficientImageDataInFile");
629 Convert run-length encoded raster pixels.
631 status=DecodeImage(image,dib_info.compression ? MagickTrue : MagickFalse,
633 if (status == MagickFalse)
634 ThrowReaderException(CorruptImageError,"UnableToRunlengthDecodeImage");
637 Initialize image structure.
639 image->units=PixelsPerCentimeterResolution;
640 image->resolution.x=(double) dib_info.x_pixels/100.0;
641 image->resolution.y=(double) dib_info.y_pixels/100.0;
643 Convert DIB raster image to pixel packets.
645 switch (dib_info.bits_per_pixel)
650 Convert bitmap scanline.
652 for (y=(ssize_t) image->rows-1; y >= 0; y--)
654 p=pixels+(image->rows-y-1)*bytes_per_line;
655 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
656 if (q == (Quantum *) NULL)
658 for (x=0; x < ((ssize_t) image->columns-7); x+=8)
660 for (bit=0; bit < 8; bit++)
662 index=(Quantum) ((*p) & (0x80 >> bit) ? 0x01 : 0x00);
663 SetPixelIndex(image,index,q);
664 q+=GetPixelChannels(image);
668 if ((image->columns % 8) != 0)
670 for (bit=0; bit < (ssize_t) (image->columns % 8); bit++)
672 index=(Quantum) ((*p) & (0x80 >> bit) ? 0x01 : 0x00);
673 SetPixelIndex(image,index,q);
674 q+=GetPixelChannels(image);
678 if (SyncAuthenticPixels(image,exception) == MagickFalse)
680 if (image->previous == (Image *) NULL)
682 status=SetImageProgress(image,LoadImageTag,image->rows-y-1,
684 if (status == MagickFalse)
688 (void) SyncImage(image,exception);
694 Convert PseudoColor scanline.
696 for (y=(ssize_t) image->rows-1; y >= 0; y--)
698 p=pixels+(image->rows-y-1)*bytes_per_line;
699 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
700 if (q == (Quantum *) NULL)
702 for (x=0; x < ((ssize_t) image->columns-1); x+=2)
704 index=ConstrainColormapIndex(image,(*p >> 4) & 0xf,exception);
705 SetPixelIndex(image,index,q);
706 q+=GetPixelChannels(image);
707 index=ConstrainColormapIndex(image,*p & 0xf,exception);
708 SetPixelIndex(image,index,q);
710 q+=GetPixelChannels(image);
712 if ((image->columns % 2) != 0)
714 index=ConstrainColormapIndex(image,(*p >> 4) & 0xf,exception);
715 SetPixelIndex(image,index,q);
716 q+=GetPixelChannels(image);
719 if (SyncAuthenticPixels(image,exception) == MagickFalse)
721 if (image->previous == (Image *) NULL)
723 status=SetImageProgress(image,LoadImageTag,image->rows-y-1,
725 if (status == MagickFalse)
729 (void) SyncImage(image,exception);
735 Convert PseudoColor scanline.
737 if ((dib_info.compression == BI_RLE8) ||
738 (dib_info.compression == BI_RLE4))
739 bytes_per_line=image->columns;
740 for (y=(ssize_t) image->rows-1; y >= 0; y--)
742 p=pixels+(image->rows-y-1)*bytes_per_line;
743 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
744 if (q == (Quantum *) NULL)
746 for (x=0; x < (ssize_t) image->columns; x++)
748 index=ConstrainColormapIndex(image,*p,exception);
749 SetPixelIndex(image,index,q);
751 q+=GetPixelChannels(image);
753 if (SyncAuthenticPixels(image,exception) == MagickFalse)
755 if (image->previous == (Image *) NULL)
757 status=SetImageProgress(image,LoadImageTag,image->rows-y-1,
759 if (status == MagickFalse)
763 (void) SyncImage(image,exception);
772 Convert PseudoColor scanline.
774 image->storage_class=DirectClass;
775 if (dib_info.compression == BI_RLE8)
776 bytes_per_line=2*image->columns;
777 for (y=(ssize_t) image->rows-1; y >= 0; y--)
779 p=pixels+(image->rows-y-1)*bytes_per_line;
780 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
781 if (q == (Quantum *) NULL)
783 for (x=0; x < (ssize_t) image->columns; x++)
787 if (dib_info.red_mask == 0)
789 SetPixelRed(image,ScaleCharToQuantum(ScaleColor5to8(
790 (unsigned char) ((word >> 10) & 0x1f))),q);
791 SetPixelGreen(image,ScaleCharToQuantum(ScaleColor5to8(
792 (unsigned char) ((word >> 5) & 0x1f))),q);
793 SetPixelBlue(image,ScaleCharToQuantum(ScaleColor5to8(
794 (unsigned char) (word & 0x1f))),q);
798 SetPixelRed(image,ScaleCharToQuantum(ScaleColor5to8(
799 (unsigned char) ((word >> 11) & 0x1f))),q);
800 SetPixelGreen(image,ScaleCharToQuantum(ScaleColor6to8(
801 (unsigned char) ((word >> 5) & 0x3f))),q);
802 SetPixelBlue(image,ScaleCharToQuantum(ScaleColor5to8(
803 (unsigned char) (word & 0x1f))),q);
805 q+=GetPixelChannels(image);
807 if (SyncAuthenticPixels(image,exception) == MagickFalse)
809 if (image->previous == (Image *) NULL)
811 status=SetImageProgress(image,LoadImageTag,image->rows-y-1,
813 if (status == MagickFalse)
823 Convert DirectColor scanline.
825 for (y=(ssize_t) image->rows-1; y >= 0; y--)
827 p=pixels+(image->rows-y-1)*bytes_per_line;
828 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
829 if (q == (Quantum *) NULL)
831 for (x=0; x < (ssize_t) image->columns; x++)
833 SetPixelBlue(image,ScaleCharToQuantum(*p++),q);
834 SetPixelGreen(image,ScaleCharToQuantum(*p++),q);
835 SetPixelRed(image,ScaleCharToQuantum(*p++),q);
836 if (image->alpha_trait == BlendPixelTrait)
837 SetPixelAlpha(image,ScaleCharToQuantum(*p++),q);
838 q+=GetPixelChannels(image);
840 if (SyncAuthenticPixels(image,exception) == MagickFalse)
842 if (image->previous == (Image *) NULL)
844 status=SetImageProgress(image,LoadImageTag,image->rows-y-1,
846 if (status == MagickFalse)
853 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
855 pixel_info=RelinquishVirtualMemory(pixel_info);
856 if (EOFBlob(image) != MagickFalse)
857 ThrowFileException(exception,CorruptImageError,"UnexpectedEndOfFile",
859 if (dib_info.height < 0)
865 Correct image orientation.
867 flipped_image=FlipImage(image,exception);
868 if (flipped_image != (Image *) NULL)
870 DuplicateBlob(flipped_image,image);
871 image=DestroyImage(image);
875 (void) CloseBlob(image);
876 return(GetFirstImageInList(image));
880 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
884 % R e g i s t e r D I B I m a g e %
888 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
890 % RegisterDIBImage() adds attributes for the DIB image format to
891 % the list of supported formats. The attributes include the image format
892 % tag, a method to read and/or write the format, whether the format
893 % supports the saving of more than one frame to the same file or blob,
894 % whether the format supports native in-memory I/O, and a brief
895 % description of the format.
897 % The format of the RegisterDIBImage method is:
899 % size_t RegisterDIBImage(void)
902 ModuleExport size_t RegisterDIBImage(void)
907 entry=SetMagickInfo("DIB");
908 entry->decoder=(DecodeImageHandler *) ReadDIBImage;
909 entry->encoder=(EncodeImageHandler *) WriteDIBImage;
910 entry->magick=(IsImageFormatHandler *) IsDIB;
911 entry->adjoin=MagickFalse;
912 entry->stealth=MagickTrue;
913 entry->description=ConstantString(
914 "Microsoft Windows 3.X Packed Device-Independent Bitmap");
915 entry->module=ConstantString("DIB");
916 (void) RegisterMagickInfo(entry);
917 return(MagickImageCoderSignature);
921 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
925 % U n r e g i s t e r D I B I m a g e %
929 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
931 % UnregisterDIBImage() removes format registrations made by the
932 % DIB module from the list of supported formats.
934 % The format of the UnregisterDIBImage method is:
936 % UnregisterDIBImage(void)
939 ModuleExport void UnregisterDIBImage(void)
941 (void) UnregisterMagickInfo("DIB");
945 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
949 % W r i t e D I B I m a g e %
953 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
955 % WriteDIBImage() writes an image in Microsoft Windows bitmap encoded
958 % The format of the WriteDIBImage method is:
960 % MagickBooleanType WriteDIBImage(const ImageInfo *image_info,
961 % Image *image,ExceptionInfo *exception)
963 % A description of each parameter follows.
965 % o image_info: the image info.
967 % o image: The image.
969 % o exception: return any errors or warnings in this structure.
972 static MagickBooleanType WriteDIBImage(const ImageInfo *image_info,Image *image,
973 ExceptionInfo *exception)
981 register const Quantum
988 register unsigned char
1002 Open output image file.
1004 assert(image_info != (const ImageInfo *) NULL);
1005 assert(image_info->signature == MagickSignature);
1006 assert(image != (Image *) NULL);
1007 assert(image->signature == MagickSignature);
1008 if (image->debug != MagickFalse)
1009 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1010 assert(exception != (ExceptionInfo *) NULL);
1011 assert(exception->signature == MagickSignature);
1012 status=OpenBlob(image_info,image,WriteBinaryBlobMode,exception);
1013 if (status == MagickFalse)
1016 Initialize DIB raster file header.
1018 if (IssRGBCompatibleColorspace(image->colorspace) == MagickFalse)
1019 (void) TransformImageColorspace(image,sRGBColorspace,exception);
1020 if (image->storage_class == DirectClass)
1023 Full color DIB raster.
1025 dib_info.number_colors=0;
1026 dib_info.bits_per_pixel=(unsigned short) (image->alpha_trait ? 32 : 24);
1031 Colormapped DIB raster.
1033 dib_info.bits_per_pixel=8;
1034 if (image_info->depth > 8)
1035 dib_info.bits_per_pixel=16;
1036 if (IsImageMonochrome(image,exception) != MagickFalse)
1037 dib_info.bits_per_pixel=1;
1038 dib_info.number_colors=(dib_info.bits_per_pixel == 16) ? 0 :
1039 (1UL << dib_info.bits_per_pixel);
1041 bytes_per_line=4*((image->columns*dib_info.bits_per_pixel+31)/32);
1043 dib_info.width=(ssize_t) image->columns;
1044 dib_info.height=(ssize_t) image->rows;
1046 dib_info.compression=(size_t) (dib_info.bits_per_pixel == 16 ?
1047 BI_BITFIELDS : BI_RGB);
1048 dib_info.image_size=bytes_per_line*image->rows;
1049 dib_info.x_pixels=75*39;
1050 dib_info.y_pixels=75*39;
1051 switch (image->units)
1053 case UndefinedResolution:
1054 case PixelsPerInchResolution:
1056 dib_info.x_pixels=(size_t) (100.0*image->resolution.x/2.54);
1057 dib_info.y_pixels=(size_t) (100.0*image->resolution.y/2.54);
1060 case PixelsPerCentimeterResolution:
1062 dib_info.x_pixels=(size_t) (100.0*image->resolution.x);
1063 dib_info.y_pixels=(size_t) (100.0*image->resolution.y);
1067 dib_info.colors_important=dib_info.number_colors;
1069 Convert MIFF to DIB raster pixels.
1071 pixels=(unsigned char *) AcquireQuantumMemory(dib_info.image_size,
1073 if (pixels == (unsigned char *) NULL)
1074 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
1075 (void) ResetMagickMemory(pixels,0,dib_info.image_size);
1076 switch (dib_info.bits_per_pixel)
1080 register unsigned char
1085 Convert PseudoClass image to a DIB monochrome image.
1087 for (y=0; y < (ssize_t) image->rows; y++)
1089 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
1090 if (p == (const Quantum *) NULL)
1092 q=pixels+(image->rows-y-1)*bytes_per_line;
1095 for (x=0; x < (ssize_t) image->columns; x++)
1098 byte|=GetPixelIndex(image,p) != 0 ? 0x01 : 0x00;
1106 p+=GetPixelChannels(image);
1110 *q++=(unsigned char) (byte << (8-bit));
1113 for (x=(ssize_t) (image->columns+7)/8; x < (ssize_t) bytes_per_line; x++)
1115 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
1117 if (status == MagickFalse)
1125 Convert PseudoClass packet to DIB pixel.
1127 for (y=0; y < (ssize_t) image->rows; y++)
1129 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
1130 if (p == (const Quantum *) NULL)
1132 q=pixels+(image->rows-y-1)*bytes_per_line;
1133 for (x=0; x < (ssize_t) image->columns; x++)
1135 *q++=(unsigned char) GetPixelIndex(image,p);
1136 p+=GetPixelChannels(image);
1138 for ( ; x < (ssize_t) bytes_per_line; x++)
1140 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
1142 if (status == MagickFalse)
1152 Convert PseudoClass packet to DIB pixel.
1154 for (y=0; y < (ssize_t) image->rows; y++)
1156 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
1157 if (p == (const Quantum *) NULL)
1159 q=pixels+(image->rows-y-1)*bytes_per_line;
1160 for (x=0; x < (ssize_t) image->columns; x++)
1162 word=(unsigned short) ((ScaleColor8to5((unsigned char)
1163 ScaleQuantumToChar(GetPixelRed(image,p))) << 11) | (ScaleColor8to6(
1164 (unsigned char) ScaleQuantumToChar(GetPixelGreen(image,p))) << 5) |
1165 (ScaleColor8to5((unsigned char) ScaleQuantumToChar((unsigned char)
1166 GetPixelBlue(image,p)) << 0)));
1167 *q++=(unsigned char)(word & 0xff);
1168 *q++=(unsigned char)(word >> 8);
1169 p+=GetPixelChannels(image);
1171 for (x=(ssize_t) (2*image->columns); x < (ssize_t) bytes_per_line; x++)
1173 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
1175 if (status == MagickFalse)
1184 Convert DirectClass packet to DIB RGB pixel.
1186 for (y=0; y < (ssize_t) image->rows; y++)
1188 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
1189 if (p == (const Quantum *) NULL)
1191 q=pixels+(image->rows-y-1)*bytes_per_line;
1192 for (x=0; x < (ssize_t) image->columns; x++)
1194 *q++=ScaleQuantumToChar(GetPixelBlue(image,p));
1195 *q++=ScaleQuantumToChar(GetPixelGreen(image,p));
1196 *q++=ScaleQuantumToChar(GetPixelRed(image,p));
1197 if (image->alpha_trait == BlendPixelTrait)
1198 *q++=ScaleQuantumToChar(GetPixelAlpha(image,p));
1199 p+=GetPixelChannels(image);
1201 if (dib_info.bits_per_pixel == 24)
1202 for (x=(ssize_t) (3*image->columns); x < (ssize_t) bytes_per_line; x++)
1204 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
1206 if (status == MagickFalse)
1212 if (dib_info.bits_per_pixel == 8)
1213 if (image_info->compression != NoCompression)
1219 Convert run-length encoded raster pixels.
1221 length=2UL*(bytes_per_line+2UL)+2UL;
1222 dib_data=(unsigned char *) AcquireQuantumMemory(length,
1223 (image->rows+2UL)*sizeof(*dib_data));
1224 if (pixels == (unsigned char *) NULL)
1226 pixels=(unsigned char *) RelinquishMagickMemory(pixels);
1227 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
1229 dib_info.image_size=(size_t) EncodeImage(image,bytes_per_line,
1231 pixels=(unsigned char *) RelinquishMagickMemory(pixels);
1233 dib_info.compression = BI_RLE8;
1238 (void) WriteBlobLSBLong(image,(unsigned int) dib_info.size);
1239 (void) WriteBlobLSBLong(image,dib_info.width);
1240 (void) WriteBlobLSBLong(image,(unsigned short) dib_info.height);
1241 (void) WriteBlobLSBShort(image,(unsigned short) dib_info.planes);
1242 (void) WriteBlobLSBShort(image,dib_info.bits_per_pixel);
1243 (void) WriteBlobLSBLong(image,(unsigned int) dib_info.compression);
1244 (void) WriteBlobLSBLong(image,(unsigned int) dib_info.image_size);
1245 (void) WriteBlobLSBLong(image,(unsigned int) dib_info.x_pixels);
1246 (void) WriteBlobLSBLong(image,(unsigned int) dib_info.y_pixels);
1247 (void) WriteBlobLSBLong(image,(unsigned int) dib_info.number_colors);
1248 (void) WriteBlobLSBLong(image,(unsigned int) dib_info.colors_important);
1249 if (image->storage_class == PseudoClass)
1251 if (dib_info.bits_per_pixel <= 8)
1257 Dump colormap to file.
1259 dib_colormap=(unsigned char *) AcquireQuantumMemory((size_t)
1260 (1UL << dib_info.bits_per_pixel),4*sizeof(dib_colormap));
1261 if (dib_colormap == (unsigned char *) NULL)
1262 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
1264 for (i=0; i < (ssize_t) MagickMin(image->colors,dib_info.number_colors); i++)
1266 *q++=ScaleQuantumToChar(image->colormap[i].blue);
1267 *q++=ScaleQuantumToChar(image->colormap[i].green);
1268 *q++=ScaleQuantumToChar(image->colormap[i].red);
1271 for ( ; i < (ssize_t) (1L << dib_info.bits_per_pixel); i++)
1278 (void) WriteBlob(image,(size_t) (4*(1 << dib_info.bits_per_pixel)),
1280 dib_colormap=(unsigned char *) RelinquishMagickMemory(dib_colormap);
1283 if ((dib_info.bits_per_pixel == 16) &&
1284 (dib_info.compression == BI_BITFIELDS))
1286 (void) WriteBlobLSBLong(image,0xf800);
1287 (void) WriteBlobLSBLong(image,0x07e0);
1288 (void) WriteBlobLSBLong(image,0x001f);
1291 (void) WriteBlob(image,dib_info.image_size,pixels);
1292 pixels=(unsigned char *) RelinquishMagickMemory(pixels);
1293 (void) CloseBlob(image);