2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
13 % Read/Write Windows DIB Image Format %
20 % Copyright 1999-2011 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 "magick/studio.h"
43 #include "magick/blob.h"
44 #include "magick/blob-private.h"
45 #include "magick/cache.h"
46 #include "magick/color.h"
47 #include "magick/color-private.h"
48 #include "magick/colormap.h"
49 #include "magick/colormap-private.h"
50 #include "magick/colorspace.h"
51 #include "magick/draw.h"
52 #include "magick/exception.h"
53 #include "magick/exception-private.h"
54 #include "magick/geometry.h"
55 #include "magick/image.h"
56 #include "magick/image-private.h"
57 #include "magick/list.h"
58 #include "magick/log.h"
59 #include "magick/magick.h"
60 #include "magick/memory_.h"
61 #include "magick/monitor.h"
62 #include "magick/monitor-private.h"
63 #include "magick/quantum-private.h"
64 #include "magick/static.h"
65 #include "magick/string_.h"
66 #include "magick/module.h"
67 #include "magick/transform.h"
72 typedef struct _DIBInfo
108 Forward declarations.
110 static MagickBooleanType
111 WriteDIBImage(const ImageInfo *,Image *);
114 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
118 % D e c o d e I m a g e %
122 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
124 % DecodeImage unpacks the packed image pixels into runlength-encoded
127 % The format of the DecodeImage method is:
129 % MagickBooleanType DecodeImage(Image *image,
130 % const MagickBooleanType compression,unsigned char *pixels)
132 % A description of each parameter follows:
134 % o image: the address of a structure of type Image.
136 % o compression: A value of 1 means the compressed pixels are runlength
137 % encoded for a 256-color bitmap. A value of 2 means a 16-color bitmap.
139 % o pixels: The address of a byte (8 bits) array of pixel data created by
140 % the decoding process.
144 static inline size_t MagickMin(const size_t x,const size_t y)
151 static MagickBooleanType DecodeImage(Image *image,
152 const MagickBooleanType compression,unsigned char *pixels)
154 #if !defined(MAGICKCORE_WINDOWS_SUPPORT) || defined(__MINGW32__)
158 #define BI_BITFIELDS 3
171 register unsigned char
178 assert(image != (Image *) NULL);
179 assert(image->signature == MagickSignature);
180 if (image->debug != MagickFalse)
181 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
182 assert(pixels != (unsigned char *) NULL);
183 (void) ResetMagickMemory(pixels,0,(size_t) image->columns*image->rows*
188 q=pixels+(size_t) image->columns*image->rows;
189 for (y=0; y < (ssize_t) image->rows; )
191 if ((p < pixels) || (p >= q))
193 count=ReadBlobByte(image);
198 count=(int) MagickMin((size_t) count,(size_t) (q-p));
202 byte=(unsigned char) ReadBlobByte(image);
203 if (compression == BI_RLE8)
205 for (i=0; i < count; i++)
206 *p++=(unsigned char) byte;
210 for (i=0; i < count; i++)
212 ((i & 0x01) != 0 ? (byte & 0x0f) : ((byte >> 4) & 0x0f));
221 count=ReadBlobByte(image);
233 p=pixels+y*image->columns;
241 x+=ReadBlobByte(image);
242 y+=ReadBlobByte(image);
243 p=pixels+y*image->columns+x;
251 count=(int) MagickMin((size_t) count,(size_t) (q-p));
252 if (compression == BI_RLE8)
253 for (i=0; i < count; i++)
254 *p++=(unsigned char) ReadBlobByte(image);
256 for (i=0; i < count; i++)
259 byte=(unsigned char) ReadBlobByte(image);
261 ((i & 0x01) != 0 ? (byte & 0x0f) : ((byte >> 4) & 0x0f));
267 if (compression == BI_RLE8)
269 if ((count & 0x01) != 0)
270 (void) ReadBlobByte(image);
273 if (((count & 0x03) == 1) || ((count & 0x03) == 2))
274 (void) ReadBlobByte(image);
279 if (SetImageProgress(image,LoadImageTag,y,image->rows) == MagickFalse)
282 (void) ReadBlobByte(image); /* end of line */
283 (void) ReadBlobByte(image);
288 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
292 % E n c o d e I m a g e %
296 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
298 % EncodeImage compresses pixels using a runlength encoded format.
300 % The format of the EncodeImage method is:
302 % static MagickBooleanType EncodeImage(Image *image,
303 % const size_t bytes_per_line,const unsigned char *pixels,
304 % unsigned char *compressed_pixels)
306 % A description of each parameter follows:
308 % o image: The image.
310 % o bytes_per_line: the number of bytes in a scanline of compressed pixels
312 % o pixels: The address of a byte (8 bits) array of pixel data created by
313 % the compression process.
315 % o compressed_pixels: The address of a byte (8 bits) array of compressed
319 static size_t EncodeImage(Image *image,const size_t bytes_per_line,
320 const unsigned char *pixels,unsigned char *compressed_pixels)
325 register const unsigned char
332 register unsigned char
336 Runlength encode pixels.
338 assert(image != (Image *) NULL);
339 assert(image->signature == MagickSignature);
340 if (image->debug != MagickFalse)
341 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
342 assert(pixels != (const unsigned char *) NULL);
343 assert(compressed_pixels != (unsigned char *) NULL);
347 for (y=0; y < (ssize_t) image->rows; y++)
349 for (x=0; x < (ssize_t) bytes_per_line; x+=i)
354 for (i=1; ((x+i) < (ssize_t) bytes_per_line); i++)
355 if ((*(p+i) != *p) || (i == 255))
357 *q++=(unsigned char) i;
366 if (SetImageProgress(image,LoadImageTag,y,image->rows) == MagickFalse)
374 return((size_t) (q-compressed_pixels));
378 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
386 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
388 % IsDIB() returns MagickTrue if the image format type, identified by the
389 % magick string, is DIB.
391 % The format of the IsDIB method is:
393 % MagickBooleanType IsDIB(const unsigned char *magick,const size_t length)
395 % A description of each parameter follows:
397 % o magick: compare image format pattern against these bytes.
399 % o length: Specifies the length of the magick string.
402 static MagickBooleanType IsDIB(const unsigned char *magick,const size_t length)
406 if (memcmp(magick,"\050\000",2) == 0)
412 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
416 % R e a d D I B I m a g e %
420 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
422 % ReadDIBImage() reads a Microsoft Windows bitmap image file and
423 % returns it. It allocates the memory necessary for the new Image structure
424 % and returns a pointer to the new image.
426 % The format of the ReadDIBImage method is:
428 % image=ReadDIBImage(image_info)
430 % A description of each parameter follows:
432 % o image_info: the image info.
434 % o exception: return any errors or warnings in this structure.
438 static inline ssize_t MagickAbsoluteValue(const ssize_t x)
445 static inline size_t MagickMax(const size_t x,const size_t y)
452 static Image *ReadDIBImage(const ImageInfo *image_info,ExceptionInfo *exception)
482 register unsigned char
498 assert(image_info != (const ImageInfo *) NULL);
499 assert(image_info->signature == MagickSignature);
500 if (image_info->debug != MagickFalse)
501 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
502 image_info->filename);
503 assert(exception != (ExceptionInfo *) NULL);
504 assert(exception->signature == MagickSignature);
505 image=AcquireImage(image_info);
506 status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception);
507 if (status == MagickFalse)
509 image=DestroyImageList(image);
510 return((Image *) NULL);
513 Determine if this a DIB file.
515 (void) ResetMagickMemory(&dib_info,0,sizeof(dib_info));
516 dib_info.size=ReadBlobLSBLong(image);
517 if (dib_info.size!=40)
518 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
520 Microsoft Windows 3.X DIB image file.
522 dib_info.width=(short) ReadBlobLSBLong(image);
523 dib_info.height=(short) ReadBlobLSBLong(image);
524 dib_info.planes=ReadBlobLSBShort(image);
525 dib_info.bits_per_pixel=ReadBlobLSBShort(image);
526 dib_info.compression=ReadBlobLSBLong(image);
527 dib_info.image_size=ReadBlobLSBLong(image);
528 dib_info.x_pixels=ReadBlobLSBLong(image);
529 dib_info.y_pixels=ReadBlobLSBLong(image);
530 dib_info.number_colors=ReadBlobLSBLong(image);
531 dib_info.colors_important=ReadBlobLSBLong(image);
532 if ((dib_info.compression == BI_BITFIELDS) &&
533 ((dib_info.bits_per_pixel == 16) || (dib_info.bits_per_pixel == 32)))
535 dib_info.red_mask=ReadBlobLSBLong(image);
536 dib_info.green_mask=ReadBlobLSBLong(image);
537 dib_info.blue_mask=ReadBlobLSBLong(image);
539 image->matte=dib_info.bits_per_pixel == 32 ? MagickTrue : MagickFalse;
540 image->columns=(size_t) MagickAbsoluteValue(dib_info.width);
541 image->rows=(size_t) MagickAbsoluteValue(dib_info.height);
543 if ((dib_info.number_colors != 0) || (dib_info.bits_per_pixel < 16))
548 image->storage_class=PseudoClass;
549 image->colors=dib_info.number_colors;
551 if (image->colors == 0)
552 image->colors=one << dib_info.bits_per_pixel;
554 if (image_info->size)
562 flags=ParseAbsoluteGeometry(image_info->size,&geometry);
563 if (flags & WidthValue)
564 if ((geometry.width != 0) && (geometry.width < image->columns))
565 image->columns=geometry.width;
566 if (flags & HeightValue)
567 if ((geometry.height != 0) && (geometry.height < image->rows))
568 image->rows=geometry.height;
570 if (image->storage_class == PseudoClass)
580 Read DIB raster colormap.
582 if (AcquireImageColormap(image,image->colors) == MagickFalse)
583 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
584 length=(size_t) image->colors;
585 dib_colormap=(unsigned char *) AcquireQuantumMemory(length,
586 4*sizeof(*dib_colormap));
587 if (dib_colormap == (unsigned char *) NULL)
588 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
590 count=ReadBlob(image,packet_size*image->colors,dib_colormap);
591 if (count != (ssize_t) (packet_size*image->colors))
592 ThrowReaderException(CorruptImageError,"InsufficientImageDataInFile");
594 for (i=0; i < (ssize_t) image->colors; i++)
596 image->colormap[i].blue=ScaleCharToQuantum(*p++);
597 image->colormap[i].green=ScaleCharToQuantum(*p++);
598 image->colormap[i].red=ScaleCharToQuantum(*p++);
599 if (packet_size == 4)
602 dib_colormap=(unsigned char *) RelinquishMagickMemory(dib_colormap);
607 if (dib_info.compression == BI_RLE4)
608 dib_info.bits_per_pixel<<=1;
609 bytes_per_line=4*((image->columns*dib_info.bits_per_pixel+31)/32);
610 length=bytes_per_line*image->rows;
611 pixels=(unsigned char *) AcquireQuantumMemory((size_t) image->rows,
612 MagickMax(bytes_per_line,image->columns+256UL)*sizeof(*pixels));
613 if (pixels == (unsigned char *) NULL)
614 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
615 if ((dib_info.compression == BI_RGB) ||
616 (dib_info.compression == BI_BITFIELDS))
618 count=ReadBlob(image,length,pixels);
619 if (count != (ssize_t) (length))
620 ThrowReaderException(CorruptImageError,"InsufficientImageDataInFile");
625 Convert run-length encoded raster pixels.
627 status=DecodeImage(image,dib_info.compression ? MagickTrue : MagickFalse,
629 if (status == MagickFalse)
630 ThrowReaderException(CorruptImageError,"UnableToRunlengthDecodeImage");
633 Initialize image structure.
635 image->units=PixelsPerCentimeterResolution;
636 image->x_resolution=(double) dib_info.x_pixels/100.0;
637 image->y_resolution=(double) dib_info.y_pixels/100.0;
639 Convert DIB raster image to pixel packets.
641 switch (dib_info.bits_per_pixel)
646 Convert bitmap scanline.
648 for (y=(ssize_t) image->rows-1; y >= 0; y--)
650 p=pixels+(image->rows-y-1)*bytes_per_line;
651 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
652 if (q == (PixelPacket *) NULL)
654 indexes=GetAuthenticIndexQueue(image);
655 for (x=0; x < ((ssize_t) image->columns-7); x+=8)
657 for (bit=0; bit < 8; bit++)
659 index=(IndexPacket) ((*p) & (0x80 >> bit) ? 0x01 : 0x00);
660 SetIndexPixelComponent(indexes+x+bit,index);
664 if ((image->columns % 8) != 0)
666 for (bit=0; bit < (ssize_t) (image->columns % 8); bit++)
668 index=(IndexPacket) ((*p) & (0x80 >> bit) ? 0x01 : 0x00);
669 SetIndexPixelComponent(indexes+x+bit,index);
673 if (SyncAuthenticPixels(image,exception) == MagickFalse)
675 if (image->previous == (Image *) NULL)
677 status=SetImageProgress(image,LoadImageTag,image->rows-y-1,
679 if (status == MagickFalse)
683 (void) SyncImage(image);
689 Convert PseudoColor scanline.
691 for (y=(ssize_t) image->rows-1; y >= 0; y--)
693 p=pixels+(image->rows-y-1)*bytes_per_line;
694 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
695 if (q == (PixelPacket *) NULL)
697 indexes=GetAuthenticIndexQueue(image);
698 for (x=0; x < ((ssize_t) image->columns-1); x+=2)
700 index=ConstrainColormapIndex(image,(*p >> 4) & 0xf);
701 SetIndexPixelComponent(indexes+x,index);
702 index=ConstrainColormapIndex(image,*p & 0xf);
703 SetIndexPixelComponent(indexes+x+1,index);
706 if ((image->columns % 2) != 0)
708 index=ConstrainColormapIndex(image,(*p >> 4) & 0xf);
709 SetIndexPixelComponent(indexes+x,index);
712 if (SyncAuthenticPixels(image,exception) == MagickFalse)
714 if (image->previous == (Image *) NULL)
716 status=SetImageProgress(image,LoadImageTag,image->rows-y-1,
718 if (status == MagickFalse)
722 (void) SyncImage(image);
728 Convert PseudoColor scanline.
730 if ((dib_info.compression == BI_RLE8) ||
731 (dib_info.compression == BI_RLE4))
732 bytes_per_line=image->columns;
733 for (y=(ssize_t) image->rows-1; y >= 0; y--)
735 p=pixels+(image->rows-y-1)*bytes_per_line;
736 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
737 if (q == (PixelPacket *) NULL)
739 indexes=GetAuthenticIndexQueue(image);
740 for (x=0; x < (ssize_t) image->columns; x++)
742 index=ConstrainColormapIndex(image,*p);
743 SetIndexPixelComponent(indexes+x,index);
747 if (SyncAuthenticPixels(image,exception) == MagickFalse)
749 if (image->previous == (Image *) NULL)
751 status=SetImageProgress(image,LoadImageTag,image->rows-y-1,
753 if (status == MagickFalse)
757 (void) SyncImage(image);
766 Convert PseudoColor scanline.
768 image->storage_class=DirectClass;
769 if (dib_info.compression == BI_RLE8)
770 bytes_per_line=2*image->columns;
771 for (y=(ssize_t) image->rows-1; y >= 0; y--)
773 p=pixels+(image->rows-y-1)*bytes_per_line;
774 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
775 if (q == (PixelPacket *) NULL)
777 for (x=0; x < (ssize_t) image->columns; x++)
781 if (dib_info.red_mask == 0)
783 SetRedPixelComponent(q,ScaleCharToQuantum(ScaleColor5to8(
784 (unsigned char) ((word >> 10) & 0x1f))));
785 SetGreenPixelComponent(q,ScaleCharToQuantum(ScaleColor5to8(
786 (unsigned char) ((word >> 5) & 0x1f))));
787 SetBluePixelComponent(q,ScaleCharToQuantum(ScaleColor5to8(
788 (unsigned char) (word & 0x1f))));
792 SetRedPixelComponent(q,ScaleCharToQuantum(ScaleColor5to8(
793 (unsigned char) ((word >> 11) & 0x1f))));
794 SetGreenPixelComponent(q,ScaleCharToQuantum(ScaleColor6to8(
795 (unsigned char) ((word >> 5) & 0x3f))));
796 SetBluePixelComponent(q,ScaleCharToQuantum(ScaleColor5to8(
797 (unsigned char) (word & 0x1f))));
801 if (SyncAuthenticPixels(image,exception) == MagickFalse)
803 if (image->previous == (Image *) NULL)
805 status=SetImageProgress(image,LoadImageTag,image->rows-y-1,
807 if (status == MagickFalse)
817 Convert DirectColor scanline.
819 for (y=(ssize_t) image->rows-1; y >= 0; y--)
821 p=pixels+(image->rows-y-1)*bytes_per_line;
822 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
823 if (q == (PixelPacket *) NULL)
825 for (x=0; x < (ssize_t) image->columns; x++)
827 SetBluePixelComponent(q,ScaleCharToQuantum(*p++));
828 SetGreenPixelComponent(q,ScaleCharToQuantum(*p++));
829 SetRedPixelComponent(q,ScaleCharToQuantum(*p++));
830 if (image->matte != MagickFalse)
831 SetOpacityPixelComponent(q,ScaleCharToQuantum(*p++));
834 if (SyncAuthenticPixels(image,exception) == MagickFalse)
836 if (image->previous == (Image *) NULL)
838 status=SetImageProgress(image,LoadImageTag,image->rows-y-1,
840 if (status == MagickFalse)
847 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
849 pixels=(unsigned char *) RelinquishMagickMemory(pixels);
850 if (EOFBlob(image) != MagickFalse)
851 ThrowFileException(exception,CorruptImageError,"UnexpectedEndOfFile",
853 if (dib_info.height < 0)
859 Correct image orientation.
861 flipped_image=FlipImage(image,exception);
862 if (flipped_image != (Image *) NULL)
864 DuplicateBlob(flipped_image,image);
865 image=DestroyImage(image);
869 (void) CloseBlob(image);
870 return(GetFirstImageInList(image));
874 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
878 % R e g i s t e r D I B I m a g e %
882 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
884 % RegisterDIBImage() adds attributes for the DIB image format to
885 % the list of supported formats. The attributes include the image format
886 % tag, a method to read and/or write the format, whether the format
887 % supports the saving of more than one frame to the same file or blob,
888 % whether the format supports native in-memory I/O, and a brief
889 % description of the format.
891 % The format of the RegisterDIBImage method is:
893 % size_t RegisterDIBImage(void)
896 ModuleExport size_t RegisterDIBImage(void)
901 entry=SetMagickInfo("DIB");
902 entry->decoder=(DecodeImageHandler *) ReadDIBImage;
903 entry->encoder=(EncodeImageHandler *) WriteDIBImage;
904 entry->magick=(IsImageFormatHandler *) IsDIB;
905 entry->adjoin=MagickFalse;
906 entry->stealth=MagickTrue;
907 entry->description=ConstantString(
908 "Microsoft Windows 3.X Packed Device-Independent Bitmap");
909 entry->module=ConstantString("DIB");
910 (void) RegisterMagickInfo(entry);
911 return(MagickImageCoderSignature);
915 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
919 % U n r e g i s t e r D I B I m a g e %
923 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
925 % UnregisterDIBImage() removes format registrations made by the
926 % DIB module from the list of supported formats.
928 % The format of the UnregisterDIBImage method is:
930 % UnregisterDIBImage(void)
933 ModuleExport void UnregisterDIBImage(void)
935 (void) UnregisterMagickInfo("DIB");
939 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
943 % W r i t e D I B I m a g e %
947 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
949 % WriteDIBImage() writes an image in Microsoft Windows bitmap encoded
952 % The format of the WriteDIBImage method is:
954 % MagickBooleanType WriteDIBImage(const ImageInfo *image_info,Image *image)
956 % A description of each parameter follows.
958 % o image_info: the image info.
960 % o image: The image.
963 static MagickBooleanType WriteDIBImage(const ImageInfo *image_info,Image *image)
971 register const IndexPacket
974 register const PixelPacket
981 register unsigned char
995 Open output image file.
997 assert(image_info != (const ImageInfo *) NULL);
998 assert(image_info->signature == MagickSignature);
999 assert(image != (Image *) NULL);
1000 assert(image->signature == MagickSignature);
1001 if (image->debug != MagickFalse)
1002 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1003 status=OpenBlob(image_info,image,WriteBinaryBlobMode,&image->exception);
1004 if (status == MagickFalse)
1007 Initialize DIB raster file header.
1009 if (image->colorspace != RGBColorspace)
1010 (void) TransformImageColorspace(image,RGBColorspace);
1011 if (image->storage_class == DirectClass)
1014 Full color DIB raster.
1016 dib_info.number_colors=0;
1017 dib_info.bits_per_pixel=(unsigned short) (image->matte ? 32 : 24);
1022 Colormapped DIB raster.
1024 dib_info.bits_per_pixel=8;
1025 if (image_info->depth > 8)
1026 dib_info.bits_per_pixel=16;
1027 if (IsMonochromeImage(image,&image->exception) != MagickFalse)
1028 dib_info.bits_per_pixel=1;
1029 dib_info.number_colors=(dib_info.bits_per_pixel == 16) ? 0 :
1030 (1UL << dib_info.bits_per_pixel);
1032 bytes_per_line=4*((image->columns*dib_info.bits_per_pixel+31)/32);
1034 dib_info.width=(ssize_t) image->columns;
1035 dib_info.height=(ssize_t) image->rows;
1037 dib_info.compression=(size_t) (dib_info.bits_per_pixel == 16 ?
1038 BI_BITFIELDS : BI_RGB);
1039 dib_info.image_size=bytes_per_line*image->rows;
1040 dib_info.x_pixels=75*39;
1041 dib_info.y_pixels=75*39;
1042 switch (image->units)
1044 case UndefinedResolution:
1045 case PixelsPerInchResolution:
1047 dib_info.x_pixels=(size_t) (100.0*image->x_resolution/2.54);
1048 dib_info.y_pixels=(size_t) (100.0*image->y_resolution/2.54);
1051 case PixelsPerCentimeterResolution:
1053 dib_info.x_pixels=(size_t) (100.0*image->x_resolution);
1054 dib_info.y_pixels=(size_t) (100.0*image->y_resolution);
1058 dib_info.colors_important=dib_info.number_colors;
1060 Convert MIFF to DIB raster pixels.
1062 pixels=(unsigned char *) AcquireQuantumMemory(dib_info.image_size,
1064 if (pixels == (unsigned char *) NULL)
1065 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
1066 (void) ResetMagickMemory(pixels,0,dib_info.image_size);
1067 switch (dib_info.bits_per_pixel)
1071 register unsigned char
1076 Convert PseudoClass image to a DIB monochrome image.
1078 for (y=0; y < (ssize_t) image->rows; y++)
1080 p=GetVirtualPixels(image,0,y,image->columns,1,&image->exception);
1081 if (p == (const PixelPacket *) NULL)
1083 indexes=GetVirtualIndexQueue(image);
1084 q=pixels+(image->rows-y-1)*bytes_per_line;
1087 for (x=0; x < (ssize_t) image->columns; x++)
1090 byte|=GetIndexPixelComponent(indexes+x) != 0 ? 0x01 : 0x00;
1102 *q++=(unsigned char) (byte << (8-bit));
1105 for (x=(ssize_t) (image->columns+7)/8; x < (ssize_t) bytes_per_line; x++)
1107 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
1109 if (status == MagickFalse)
1117 Convert PseudoClass packet to DIB pixel.
1119 for (y=0; y < (ssize_t) image->rows; y++)
1121 p=GetVirtualPixels(image,0,y,image->columns,1,&image->exception);
1122 if (p == (const PixelPacket *) NULL)
1124 indexes=GetVirtualIndexQueue(image);
1125 q=pixels+(image->rows-y-1)*bytes_per_line;
1126 for (x=0; x < (ssize_t) image->columns; x++)
1127 *q++=(unsigned char) GetIndexPixelComponent(indexes+x);
1128 for ( ; x < (ssize_t) bytes_per_line; x++)
1130 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
1132 if (status == MagickFalse)
1142 Convert PseudoClass packet to DIB pixel.
1144 for (y=0; y < (ssize_t) image->rows; y++)
1146 p=GetVirtualPixels(image,0,y,image->columns,1,&image->exception);
1147 if (p == (const PixelPacket *) NULL)
1149 q=pixels+(image->rows-y-1)*bytes_per_line;
1150 for (x=0; x < (ssize_t) image->columns; x++)
1152 word=(unsigned short) ((ScaleColor8to5((unsigned char)
1153 ScaleQuantumToChar(GetRedPixelComponent(p))) << 11) |
1154 (ScaleColor8to6((unsigned char) ScaleQuantumToChar(
1155 GetGreenPixelComponent(p))) << 5) | (ScaleColor8to5((unsigned char)
1156 ScaleQuantumToChar((unsigned char) GetBluePixelComponent(p)) <<
1158 *q++=(unsigned char)(word & 0xff);
1159 *q++=(unsigned char)(word >> 8);
1162 for (x=(ssize_t) (2*image->columns); x < (ssize_t) bytes_per_line; x++)
1164 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
1166 if (status == MagickFalse)
1175 Convert DirectClass packet to DIB RGB pixel.
1177 for (y=0; y < (ssize_t) image->rows; y++)
1179 p=GetVirtualPixels(image,0,y,image->columns,1,&image->exception);
1180 if (p == (const PixelPacket *) NULL)
1182 q=pixels+(image->rows-y-1)*bytes_per_line;
1183 for (x=0; x < (ssize_t) image->columns; x++)
1185 *q++=ScaleQuantumToChar(GetBluePixelComponent(p));
1186 *q++=ScaleQuantumToChar(GetGreenPixelComponent(p));
1187 *q++=ScaleQuantumToChar(GetRedPixelComponent(p));
1188 if (image->matte != MagickFalse)
1189 *q++=ScaleQuantumToChar(GetOpacityPixelComponent(p));
1192 if (dib_info.bits_per_pixel == 24)
1193 for (x=(ssize_t) (3*image->columns); x < (ssize_t) bytes_per_line; x++)
1195 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
1197 if (status == MagickFalse)
1203 if (dib_info.bits_per_pixel == 8)
1204 if (image_info->compression != NoCompression)
1210 Convert run-length encoded raster pixels.
1212 length=2UL*(bytes_per_line+2UL)+2UL;
1213 dib_data=(unsigned char *) AcquireQuantumMemory(length,
1214 (image->rows+2UL)*sizeof(*dib_data));
1215 if (pixels == (unsigned char *) NULL)
1217 pixels=(unsigned char *) RelinquishMagickMemory(pixels);
1218 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
1220 dib_info.image_size=(size_t) EncodeImage(image,bytes_per_line,
1222 pixels=(unsigned char *) RelinquishMagickMemory(pixels);
1224 dib_info.compression = BI_RLE8;
1229 (void) WriteBlobLSBLong(image,(unsigned int) dib_info.size);
1230 (void) WriteBlobLSBLong(image,dib_info.width);
1231 (void) WriteBlobLSBLong(image,(unsigned short) dib_info.height);
1232 (void) WriteBlobLSBShort(image,(unsigned short) dib_info.planes);
1233 (void) WriteBlobLSBShort(image,dib_info.bits_per_pixel);
1234 (void) WriteBlobLSBLong(image,(unsigned int) dib_info.compression);
1235 (void) WriteBlobLSBLong(image,(unsigned int) dib_info.image_size);
1236 (void) WriteBlobLSBLong(image,(unsigned int) dib_info.x_pixels);
1237 (void) WriteBlobLSBLong(image,(unsigned int) dib_info.y_pixels);
1238 (void) WriteBlobLSBLong(image,(unsigned int) dib_info.number_colors);
1239 (void) WriteBlobLSBLong(image,(unsigned int) dib_info.colors_important);
1240 if (image->storage_class == PseudoClass)
1242 if (dib_info.bits_per_pixel <= 8)
1248 Dump colormap to file.
1250 dib_colormap=(unsigned char *) AcquireQuantumMemory((size_t)
1251 (1UL << dib_info.bits_per_pixel),4*sizeof(dib_colormap));
1252 if (dib_colormap == (unsigned char *) NULL)
1253 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
1255 for (i=0; i < (ssize_t) MagickMin(image->colors,dib_info.number_colors); i++)
1257 *q++=ScaleQuantumToChar(image->colormap[i].blue);
1258 *q++=ScaleQuantumToChar(image->colormap[i].green);
1259 *q++=ScaleQuantumToChar(image->colormap[i].red);
1262 for ( ; i < (ssize_t) (1L << dib_info.bits_per_pixel); i++)
1269 (void) WriteBlob(image,(size_t) (4*(1 << dib_info.bits_per_pixel)),
1271 dib_colormap=(unsigned char *) RelinquishMagickMemory(dib_colormap);
1274 if ((dib_info.bits_per_pixel == 16) &&
1275 (dib_info.compression == BI_BITFIELDS))
1277 (void) WriteBlobLSBLong(image,0xf800);
1278 (void) WriteBlobLSBLong(image,0x07e0);
1279 (void) WriteBlobLSBLong(image,0x001f);
1282 (void) WriteBlob(image,dib_info.image_size,pixels);
1283 pixels=(unsigned char *) RelinquishMagickMemory(pixels);
1284 (void) CloseBlob(image);