2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
13 % Read/Write Windows DIB Image Format %
20 % Copyright 1999-2012 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__)
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)
478 register unsigned char
497 assert(image_info != (const ImageInfo *) NULL);
498 assert(image_info->signature == MagickSignature);
499 if (image_info->debug != MagickFalse)
500 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
501 image_info->filename);
502 assert(exception != (ExceptionInfo *) NULL);
503 assert(exception->signature == MagickSignature);
504 image=AcquireImage(image_info,exception);
505 status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception);
506 if (status == MagickFalse)
508 image=DestroyImageList(image);
509 return((Image *) NULL);
512 Determine if this a DIB file.
514 (void) ResetMagickMemory(&dib_info,0,sizeof(dib_info));
515 dib_info.size=ReadBlobLSBLong(image);
516 if (dib_info.size!=40)
517 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
519 Microsoft Windows 3.X DIB image file.
521 dib_info.width=(short) ReadBlobLSBLong(image);
522 dib_info.height=(short) ReadBlobLSBLong(image);
523 dib_info.planes=ReadBlobLSBShort(image);
524 dib_info.bits_per_pixel=ReadBlobLSBShort(image);
525 dib_info.compression=ReadBlobLSBLong(image);
526 dib_info.image_size=ReadBlobLSBLong(image);
527 dib_info.x_pixels=ReadBlobLSBLong(image);
528 dib_info.y_pixels=ReadBlobLSBLong(image);
529 dib_info.number_colors=ReadBlobLSBLong(image);
530 dib_info.colors_important=ReadBlobLSBLong(image);
531 if ((dib_info.compression == BI_BITFIELDS) &&
532 ((dib_info.bits_per_pixel == 16) || (dib_info.bits_per_pixel == 32)))
534 dib_info.red_mask=ReadBlobLSBLong(image);
535 dib_info.green_mask=ReadBlobLSBLong(image);
536 dib_info.blue_mask=ReadBlobLSBLong(image);
538 image->matte=dib_info.bits_per_pixel == 32 ? MagickTrue : MagickFalse;
539 image->columns=(size_t) MagickAbsoluteValue(dib_info.width);
540 image->rows=(size_t) MagickAbsoluteValue(dib_info.height);
542 if ((dib_info.number_colors != 0) || (dib_info.bits_per_pixel < 16))
547 image->storage_class=PseudoClass;
548 image->colors=dib_info.number_colors;
550 if (image->colors == 0)
551 image->colors=one << dib_info.bits_per_pixel;
553 if (image_info->size)
561 flags=ParseAbsoluteGeometry(image_info->size,&geometry);
562 if (flags & WidthValue)
563 if ((geometry.width != 0) && (geometry.width < image->columns))
564 image->columns=geometry.width;
565 if (flags & HeightValue)
566 if ((geometry.height != 0) && (geometry.height < image->rows))
567 image->rows=geometry.height;
569 if (image->storage_class == PseudoClass)
579 Read DIB raster colormap.
581 if (AcquireImageColormap(image,image->colors,exception) == MagickFalse)
582 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
583 length=(size_t) image->colors;
584 dib_colormap=(unsigned char *) AcquireQuantumMemory(length,
585 4*sizeof(*dib_colormap));
586 if (dib_colormap == (unsigned char *) NULL)
587 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
589 count=ReadBlob(image,packet_size*image->colors,dib_colormap);
590 if (count != (ssize_t) (packet_size*image->colors))
591 ThrowReaderException(CorruptImageError,"InsufficientImageDataInFile");
593 for (i=0; i < (ssize_t) image->colors; i++)
595 image->colormap[i].blue=ScaleCharToQuantum(*p++);
596 image->colormap[i].green=ScaleCharToQuantum(*p++);
597 image->colormap[i].red=ScaleCharToQuantum(*p++);
598 if (packet_size == 4)
601 dib_colormap=(unsigned char *) RelinquishMagickMemory(dib_colormap);
606 if (dib_info.compression == BI_RLE4)
607 dib_info.bits_per_pixel<<=1;
608 bytes_per_line=4*((image->columns*dib_info.bits_per_pixel+31)/32);
609 length=bytes_per_line*image->rows;
610 pixels=(unsigned char *) AcquireQuantumMemory((size_t) image->rows,
611 MagickMax(bytes_per_line,image->columns+256UL)*sizeof(*pixels));
612 if (pixels == (unsigned char *) NULL)
613 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
614 if ((dib_info.compression == BI_RGB) ||
615 (dib_info.compression == BI_BITFIELDS))
617 count=ReadBlob(image,length,pixels);
618 if (count != (ssize_t) (length))
619 ThrowReaderException(CorruptImageError,"InsufficientImageDataInFile");
624 Convert run-length encoded raster pixels.
626 status=DecodeImage(image,dib_info.compression ? MagickTrue : MagickFalse,
628 if (status == MagickFalse)
629 ThrowReaderException(CorruptImageError,"UnableToRunlengthDecodeImage");
632 Initialize image structure.
634 image->units=PixelsPerCentimeterResolution;
635 image->resolution.x=(double) dib_info.x_pixels/100.0;
636 image->resolution.y=(double) dib_info.y_pixels/100.0;
638 Convert DIB raster image to pixel packets.
640 switch (dib_info.bits_per_pixel)
645 Convert bitmap scanline.
647 for (y=(ssize_t) image->rows-1; y >= 0; y--)
649 p=pixels+(image->rows-y-1)*bytes_per_line;
650 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
651 if (q == (Quantum *) NULL)
653 for (x=0; x < ((ssize_t) image->columns-7); x+=8)
655 for (bit=0; bit < 8; bit++)
657 index=(Quantum) ((*p) & (0x80 >> bit) ? 0x01 : 0x00);
658 SetPixelIndex(image,index,q);
659 q+=GetPixelChannels(image);
663 if ((image->columns % 8) != 0)
665 for (bit=0; bit < (ssize_t) (image->columns % 8); bit++)
667 index=(Quantum) ((*p) & (0x80 >> bit) ? 0x01 : 0x00);
668 SetPixelIndex(image,index,q);
669 q+=GetPixelChannels(image);
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,exception);
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 == (Quantum *) NULL)
697 for (x=0; x < ((ssize_t) image->columns-1); x+=2)
699 index=ConstrainColormapIndex(image,(*p >> 4) & 0xf,exception);
700 SetPixelIndex(image,index,q);
701 q+=GetPixelChannels(image);
702 index=ConstrainColormapIndex(image,*p & 0xf,exception);
703 SetPixelIndex(image,index,q);
705 q+=GetPixelChannels(image);
707 if ((image->columns % 2) != 0)
709 index=ConstrainColormapIndex(image,(*p >> 4) & 0xf,exception);
710 SetPixelIndex(image,index,q);
711 q+=GetPixelChannels(image);
714 if (SyncAuthenticPixels(image,exception) == MagickFalse)
716 if (image->previous == (Image *) NULL)
718 status=SetImageProgress(image,LoadImageTag,image->rows-y-1,
720 if (status == MagickFalse)
724 (void) SyncImage(image,exception);
730 Convert PseudoColor scanline.
732 if ((dib_info.compression == BI_RLE8) ||
733 (dib_info.compression == BI_RLE4))
734 bytes_per_line=image->columns;
735 for (y=(ssize_t) image->rows-1; y >= 0; y--)
737 p=pixels+(image->rows-y-1)*bytes_per_line;
738 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
739 if (q == (Quantum *) NULL)
741 for (x=0; x < (ssize_t) image->columns; x++)
743 index=ConstrainColormapIndex(image,*p,exception);
744 SetPixelIndex(image,index,q);
746 q+=GetPixelChannels(image);
748 if (SyncAuthenticPixels(image,exception) == MagickFalse)
750 if (image->previous == (Image *) NULL)
752 status=SetImageProgress(image,LoadImageTag,image->rows-y-1,
754 if (status == MagickFalse)
758 (void) SyncImage(image,exception);
767 Convert PseudoColor scanline.
769 image->storage_class=DirectClass;
770 if (dib_info.compression == BI_RLE8)
771 bytes_per_line=2*image->columns;
772 for (y=(ssize_t) image->rows-1; y >= 0; y--)
774 p=pixels+(image->rows-y-1)*bytes_per_line;
775 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
776 if (q == (Quantum *) NULL)
778 for (x=0; x < (ssize_t) image->columns; x++)
782 if (dib_info.red_mask == 0)
784 SetPixelRed(image,ScaleCharToQuantum(ScaleColor5to8(
785 (unsigned char) ((word >> 10) & 0x1f))),q);
786 SetPixelGreen(image,ScaleCharToQuantum(ScaleColor5to8(
787 (unsigned char) ((word >> 5) & 0x1f))),q);
788 SetPixelBlue(image,ScaleCharToQuantum(ScaleColor5to8(
789 (unsigned char) (word & 0x1f))),q);
793 SetPixelRed(image,ScaleCharToQuantum(ScaleColor5to8(
794 (unsigned char) ((word >> 11) & 0x1f))),q);
795 SetPixelGreen(image,ScaleCharToQuantum(ScaleColor6to8(
796 (unsigned char) ((word >> 5) & 0x3f))),q);
797 SetPixelBlue(image,ScaleCharToQuantum(ScaleColor5to8(
798 (unsigned char) (word & 0x1f))),q);
800 q+=GetPixelChannels(image);
802 if (SyncAuthenticPixels(image,exception) == MagickFalse)
804 if (image->previous == (Image *) NULL)
806 status=SetImageProgress(image,LoadImageTag,image->rows-y-1,
808 if (status == MagickFalse)
818 Convert DirectColor scanline.
820 for (y=(ssize_t) image->rows-1; y >= 0; y--)
822 p=pixels+(image->rows-y-1)*bytes_per_line;
823 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
824 if (q == (Quantum *) NULL)
826 for (x=0; x < (ssize_t) image->columns; x++)
828 SetPixelBlue(image,ScaleCharToQuantum(*p++),q);
829 SetPixelGreen(image,ScaleCharToQuantum(*p++),q);
830 SetPixelRed(image,ScaleCharToQuantum(*p++),q);
831 if (image->matte != MagickFalse)
832 SetPixelAlpha(image,ScaleCharToQuantum(*p++),q);
833 q+=GetPixelChannels(image);
835 if (SyncAuthenticPixels(image,exception) == MagickFalse)
837 if (image->previous == (Image *) NULL)
839 status=SetImageProgress(image,LoadImageTag,image->rows-y-1,
841 if (status == MagickFalse)
848 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
850 pixels=(unsigned char *) RelinquishMagickMemory(pixels);
851 if (EOFBlob(image) != MagickFalse)
852 ThrowFileException(exception,CorruptImageError,"UnexpectedEndOfFile",
854 if (dib_info.height < 0)
860 Correct image orientation.
862 flipped_image=FlipImage(image,exception);
863 if (flipped_image != (Image *) NULL)
865 DuplicateBlob(flipped_image,image);
866 image=DestroyImage(image);
870 (void) CloseBlob(image);
871 return(GetFirstImageInList(image));
875 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
879 % R e g i s t e r D I B I m a g e %
883 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
885 % RegisterDIBImage() adds attributes for the DIB image format to
886 % the list of supported formats. The attributes include the image format
887 % tag, a method to read and/or write the format, whether the format
888 % supports the saving of more than one frame to the same file or blob,
889 % whether the format supports native in-memory I/O, and a brief
890 % description of the format.
892 % The format of the RegisterDIBImage method is:
894 % size_t RegisterDIBImage(void)
897 ModuleExport size_t RegisterDIBImage(void)
902 entry=SetMagickInfo("DIB");
903 entry->decoder=(DecodeImageHandler *) ReadDIBImage;
904 entry->encoder=(EncodeImageHandler *) WriteDIBImage;
905 entry->magick=(IsImageFormatHandler *) IsDIB;
906 entry->adjoin=MagickFalse;
907 entry->stealth=MagickTrue;
908 entry->description=ConstantString(
909 "Microsoft Windows 3.X Packed Device-Independent Bitmap");
910 entry->module=ConstantString("DIB");
911 (void) RegisterMagickInfo(entry);
912 return(MagickImageCoderSignature);
916 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
920 % U n r e g i s t e r D I B I m a g e %
924 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
926 % UnregisterDIBImage() removes format registrations made by the
927 % DIB module from the list of supported formats.
929 % The format of the UnregisterDIBImage method is:
931 % UnregisterDIBImage(void)
934 ModuleExport void UnregisterDIBImage(void)
936 (void) UnregisterMagickInfo("DIB");
940 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
944 % W r i t e D I B I m a g e %
948 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
950 % WriteDIBImage() writes an image in Microsoft Windows bitmap encoded
953 % The format of the WriteDIBImage method is:
955 % MagickBooleanType WriteDIBImage(const ImageInfo *image_info,
956 % Image *image,ExceptionInfo *exception)
958 % A description of each parameter follows.
960 % o image_info: the image info.
962 % o image: The image.
964 % o exception: return any errors or warnings in this structure.
967 static MagickBooleanType WriteDIBImage(const ImageInfo *image_info,Image *image,
968 ExceptionInfo *exception)
976 register const Quantum
983 register unsigned char
997 Open output image file.
999 assert(image_info != (const ImageInfo *) NULL);
1000 assert(image_info->signature == MagickSignature);
1001 assert(image != (Image *) NULL);
1002 assert(image->signature == MagickSignature);
1003 if (image->debug != MagickFalse)
1004 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1005 assert(exception != (ExceptionInfo *) NULL);
1006 assert(exception->signature == MagickSignature);
1007 status=OpenBlob(image_info,image,WriteBinaryBlobMode,exception);
1008 if (status == MagickFalse)
1011 Initialize DIB raster file header.
1013 if (IssRGBCompatibleColorspace(image->colorspace) == MagickFalse)
1014 (void) TransformImageColorspace(image,sRGBColorspace,exception);
1015 if (image->storage_class == DirectClass)
1018 Full color DIB raster.
1020 dib_info.number_colors=0;
1021 dib_info.bits_per_pixel=(unsigned short) (image->matte ? 32 : 24);
1026 Colormapped DIB raster.
1028 dib_info.bits_per_pixel=8;
1029 if (image_info->depth > 8)
1030 dib_info.bits_per_pixel=16;
1031 if (IsImageMonochrome(image,exception) != MagickFalse)
1032 dib_info.bits_per_pixel=1;
1033 dib_info.number_colors=(dib_info.bits_per_pixel == 16) ? 0 :
1034 (1UL << dib_info.bits_per_pixel);
1036 bytes_per_line=4*((image->columns*dib_info.bits_per_pixel+31)/32);
1038 dib_info.width=(ssize_t) image->columns;
1039 dib_info.height=(ssize_t) image->rows;
1041 dib_info.compression=(size_t) (dib_info.bits_per_pixel == 16 ?
1042 BI_BITFIELDS : BI_RGB);
1043 dib_info.image_size=bytes_per_line*image->rows;
1044 dib_info.x_pixels=75*39;
1045 dib_info.y_pixels=75*39;
1046 switch (image->units)
1048 case UndefinedResolution:
1049 case PixelsPerInchResolution:
1051 dib_info.x_pixels=(size_t) (100.0*image->resolution.x/2.54);
1052 dib_info.y_pixels=(size_t) (100.0*image->resolution.y/2.54);
1055 case PixelsPerCentimeterResolution:
1057 dib_info.x_pixels=(size_t) (100.0*image->resolution.x);
1058 dib_info.y_pixels=(size_t) (100.0*image->resolution.y);
1062 dib_info.colors_important=dib_info.number_colors;
1064 Convert MIFF to DIB raster pixels.
1066 pixels=(unsigned char *) AcquireQuantumMemory(dib_info.image_size,
1068 if (pixels == (unsigned char *) NULL)
1069 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
1070 (void) ResetMagickMemory(pixels,0,dib_info.image_size);
1071 switch (dib_info.bits_per_pixel)
1075 register unsigned char
1080 Convert PseudoClass image to a DIB monochrome image.
1082 for (y=0; y < (ssize_t) image->rows; y++)
1084 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
1085 if (p == (const Quantum *) NULL)
1087 q=pixels+(image->rows-y-1)*bytes_per_line;
1090 for (x=0; x < (ssize_t) image->columns; x++)
1093 byte|=GetPixelIndex(image,p) != 0 ? 0x01 : 0x00;
1101 p+=GetPixelChannels(image);
1105 *q++=(unsigned char) (byte << (8-bit));
1108 for (x=(ssize_t) (image->columns+7)/8; x < (ssize_t) bytes_per_line; x++)
1110 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
1112 if (status == MagickFalse)
1120 Convert PseudoClass packet to DIB pixel.
1122 for (y=0; y < (ssize_t) image->rows; y++)
1124 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
1125 if (p == (const Quantum *) NULL)
1127 q=pixels+(image->rows-y-1)*bytes_per_line;
1128 for (x=0; x < (ssize_t) image->columns; x++)
1130 *q++=(unsigned char) GetPixelIndex(image,p);
1131 p+=GetPixelChannels(image);
1133 for ( ; x < (ssize_t) bytes_per_line; x++)
1135 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
1137 if (status == MagickFalse)
1147 Convert PseudoClass packet to DIB pixel.
1149 for (y=0; y < (ssize_t) image->rows; y++)
1151 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
1152 if (p == (const Quantum *) NULL)
1154 q=pixels+(image->rows-y-1)*bytes_per_line;
1155 for (x=0; x < (ssize_t) image->columns; x++)
1157 word=(unsigned short) ((ScaleColor8to5((unsigned char)
1158 ScaleQuantumToChar(GetPixelRed(image,p))) << 11) | (ScaleColor8to6(
1159 (unsigned char) ScaleQuantumToChar(GetPixelGreen(image,p))) << 5) |
1160 (ScaleColor8to5((unsigned char) ScaleQuantumToChar((unsigned char)
1161 GetPixelBlue(image,p)) << 0)));
1162 *q++=(unsigned char)(word & 0xff);
1163 *q++=(unsigned char)(word >> 8);
1164 p+=GetPixelChannels(image);
1166 for (x=(ssize_t) (2*image->columns); x < (ssize_t) bytes_per_line; x++)
1168 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
1170 if (status == MagickFalse)
1179 Convert DirectClass packet to DIB RGB pixel.
1181 for (y=0; y < (ssize_t) image->rows; y++)
1183 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
1184 if (p == (const Quantum *) NULL)
1186 q=pixels+(image->rows-y-1)*bytes_per_line;
1187 for (x=0; x < (ssize_t) image->columns; x++)
1189 *q++=ScaleQuantumToChar(GetPixelBlue(image,p));
1190 *q++=ScaleQuantumToChar(GetPixelGreen(image,p));
1191 *q++=ScaleQuantumToChar(GetPixelRed(image,p));
1192 if (image->matte != MagickFalse)
1193 *q++=ScaleQuantumToChar(GetPixelAlpha(image,p));
1194 p+=GetPixelChannels(image);
1196 if (dib_info.bits_per_pixel == 24)
1197 for (x=(ssize_t) (3*image->columns); x < (ssize_t) bytes_per_line; x++)
1199 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
1201 if (status == MagickFalse)
1207 if (dib_info.bits_per_pixel == 8)
1208 if (image_info->compression != NoCompression)
1214 Convert run-length encoded raster pixels.
1216 length=2UL*(bytes_per_line+2UL)+2UL;
1217 dib_data=(unsigned char *) AcquireQuantumMemory(length,
1218 (image->rows+2UL)*sizeof(*dib_data));
1219 if (pixels == (unsigned char *) NULL)
1221 pixels=(unsigned char *) RelinquishMagickMemory(pixels);
1222 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
1224 dib_info.image_size=(size_t) EncodeImage(image,bytes_per_line,
1226 pixels=(unsigned char *) RelinquishMagickMemory(pixels);
1228 dib_info.compression = BI_RLE8;
1233 (void) WriteBlobLSBLong(image,(unsigned int) dib_info.size);
1234 (void) WriteBlobLSBLong(image,dib_info.width);
1235 (void) WriteBlobLSBLong(image,(unsigned short) dib_info.height);
1236 (void) WriteBlobLSBShort(image,(unsigned short) dib_info.planes);
1237 (void) WriteBlobLSBShort(image,dib_info.bits_per_pixel);
1238 (void) WriteBlobLSBLong(image,(unsigned int) dib_info.compression);
1239 (void) WriteBlobLSBLong(image,(unsigned int) dib_info.image_size);
1240 (void) WriteBlobLSBLong(image,(unsigned int) dib_info.x_pixels);
1241 (void) WriteBlobLSBLong(image,(unsigned int) dib_info.y_pixels);
1242 (void) WriteBlobLSBLong(image,(unsigned int) dib_info.number_colors);
1243 (void) WriteBlobLSBLong(image,(unsigned int) dib_info.colors_important);
1244 if (image->storage_class == PseudoClass)
1246 if (dib_info.bits_per_pixel <= 8)
1252 Dump colormap to file.
1254 dib_colormap=(unsigned char *) AcquireQuantumMemory((size_t)
1255 (1UL << dib_info.bits_per_pixel),4*sizeof(dib_colormap));
1256 if (dib_colormap == (unsigned char *) NULL)
1257 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
1259 for (i=0; i < (ssize_t) MagickMin(image->colors,dib_info.number_colors); i++)
1261 *q++=ScaleQuantumToChar(image->colormap[i].blue);
1262 *q++=ScaleQuantumToChar(image->colormap[i].green);
1263 *q++=ScaleQuantumToChar(image->colormap[i].red);
1266 for ( ; i < (ssize_t) (1L << dib_info.bits_per_pixel); i++)
1273 (void) WriteBlob(image,(size_t) (4*(1 << dib_info.bits_per_pixel)),
1275 dib_colormap=(unsigned char *) RelinquishMagickMemory(dib_colormap);
1278 if ((dib_info.bits_per_pixel == 16) &&
1279 (dib_info.compression == BI_BITFIELDS))
1281 (void) WriteBlobLSBLong(image,0xf800);
1282 (void) WriteBlobLSBLong(image,0x07e0);
1283 (void) WriteBlobLSBLong(image,0x001f);
1286 (void) WriteBlob(image,dib_info.image_size,pixels);
1287 pixels=(unsigned char *) RelinquishMagickMemory(pixels);
1288 (void) CloseBlob(image);