2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
13 % Read/Write Windows DIB Image Format %
20 % Copyright 1999-2010 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 < (long) 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 unsigned long 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 unsigned long 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 < (long) image->rows; y++)
349 for (x=0; x < (long) bytes_per_line; x+=i)
354 for (i=1; ((x+i) < (long) 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 long MagickAbsoluteValue(const long 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
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);
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->matte=dib_info.bits_per_pixel == 32 ? MagickTrue : MagickFalse;
542 image->columns=(unsigned long) MagickAbsoluteValue(dib_info.width);
543 image->rows=(unsigned long) MagickAbsoluteValue(dib_info.height);
545 if ((dib_info.number_colors != 0) || (dib_info.bits_per_pixel < 16))
547 image->storage_class=PseudoClass;
548 image->colors=dib_info.number_colors;
549 if (image->colors == 0)
550 image->colors=1L << dib_info.bits_per_pixel;
552 if (image_info->size)
560 flags=ParseAbsoluteGeometry(image_info->size,&geometry);
561 if (flags & WidthValue)
562 if ((geometry.width != 0) && (geometry.width < image->columns))
563 image->columns=geometry.width;
564 if (flags & HeightValue)
565 if ((geometry.height != 0) && (geometry.height < image->rows))
566 image->rows=geometry.height;
568 if (image->storage_class == PseudoClass)
578 Read DIB raster colormap.
580 if (AcquireImageColormap(image,image->colors) == MagickFalse)
581 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
582 length=(size_t) image->colors;
583 dib_colormap=(unsigned char *) AcquireQuantumMemory(length,
584 4*sizeof(*dib_colormap));
585 if (dib_colormap == (unsigned char *) NULL)
586 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
588 count=ReadBlob(image,packet_size*image->colors,dib_colormap);
589 if (count != (ssize_t) (packet_size*image->colors))
590 ThrowReaderException(CorruptImageError,"InsufficientImageDataInFile");
592 for (i=0; i < (long) image->colors; i++)
594 image->colormap[i].blue=ScaleCharToQuantum(*p++);
595 image->colormap[i].green=ScaleCharToQuantum(*p++);
596 image->colormap[i].red=ScaleCharToQuantum(*p++);
597 if (packet_size == 4)
600 dib_colormap=(unsigned char *) RelinquishMagickMemory(dib_colormap);
605 if (dib_info.compression == BI_RLE4)
606 dib_info.bits_per_pixel<<=1;
607 bytes_per_line=4*((image->columns*dib_info.bits_per_pixel+31)/32);
608 length=bytes_per_line*image->rows;
609 pixels=(unsigned char *) AcquireQuantumMemory((size_t) image->rows,
610 MagickMax(bytes_per_line,image->columns+256UL)*sizeof(*pixels));
611 if (pixels == (unsigned char *) NULL)
612 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
613 if ((dib_info.compression == BI_RGB) ||
614 (dib_info.compression == BI_BITFIELDS))
616 count=ReadBlob(image,length,pixels);
617 if (count != (ssize_t) (length))
618 ThrowReaderException(CorruptImageError,"InsufficientImageDataInFile");
623 Convert run-length encoded raster pixels.
625 status=DecodeImage(image,dib_info.compression ? MagickTrue : MagickFalse,
627 if (status == MagickFalse)
628 ThrowReaderException(CorruptImageError,"UnableToRunlengthDecodeImage");
631 Initialize image structure.
633 image->units=PixelsPerCentimeterResolution;
634 image->x_resolution=(double) dib_info.x_pixels/100.0;
635 image->y_resolution=(double) dib_info.y_pixels/100.0;
637 Convert DIB raster image to pixel packets.
639 switch (dib_info.bits_per_pixel)
644 Convert bitmap scanline.
646 for (y=(long) image->rows-1; y >= 0; y--)
648 p=pixels+(image->rows-y-1)*bytes_per_line;
649 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
650 if (q == (PixelPacket *) NULL)
652 indexes=GetAuthenticIndexQueue(image);
653 for (x=0; x < ((long) image->columns-7); x+=8)
655 for (bit=0; bit < 8; bit++)
657 index=(IndexPacket) ((*p) & (0x80 >> bit) ? 0x01 : 0x00);
658 indexes[x+bit]=index;
659 *q++=image->colormap[(long) index];
663 if ((image->columns % 8) != 0)
665 for (bit=0; bit < (long) (image->columns % 8); bit++)
667 index=(IndexPacket) ((*p) & (0x80 >> bit) ? 0x01 : 0x00);
668 indexes[x+bit]=index;
669 *q++=image->colormap[(long) 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)
688 Convert PseudoColor scanline.
690 for (y=(long) image->rows-1; y >= 0; y--)
692 p=pixels+(image->rows-y-1)*bytes_per_line;
693 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
694 if (q == (PixelPacket *) NULL)
696 indexes=GetAuthenticIndexQueue(image);
697 for (x=0; x < ((long) image->columns-1); x+=2)
699 index=ConstrainColormapIndex(image,(*p >> 4) & 0xf);
701 *q++=image->colormap[(long) index];
702 index=ConstrainColormapIndex(image,*p & 0xf);
704 *q++=image->colormap[(long) index];
707 if ((image->columns % 2) != 0)
709 index=ConstrainColormapIndex(image,(*p >> 4) & 0xf);
711 *q++=image->colormap[(long) index];
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)
729 Convert PseudoColor scanline.
731 if ((dib_info.compression == BI_RLE8) ||
732 (dib_info.compression == BI_RLE4))
733 bytes_per_line=image->columns;
734 for (y=(long) image->rows-1; y >= 0; y--)
736 p=pixels+(image->rows-y-1)*bytes_per_line;
737 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
738 if (q == (PixelPacket *) NULL)
740 indexes=GetAuthenticIndexQueue(image);
741 for (x=0; x < (long) image->columns; x++)
743 index=ConstrainColormapIndex(image,*p);
745 *q=image->colormap[(long) index];
749 if (SyncAuthenticPixels(image,exception) == MagickFalse)
751 if (image->previous == (Image *) NULL)
753 status=SetImageProgress(image,LoadImageTag,image->rows-y-1,
755 if (status == MagickFalse)
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=(long) 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 == (PixelPacket *) NULL)
778 for (x=0; x < (long) image->columns; x++)
782 if (dib_info.red_mask == 0)
784 q->red=ScaleCharToQuantum(ScaleColor5to8((unsigned char)
785 ((word >> 10) & 0x1f)));
786 q->green=ScaleCharToQuantum(ScaleColor5to8((unsigned char)
787 ((word >> 5) & 0x1f)));
788 q->blue=ScaleCharToQuantum(ScaleColor5to8((unsigned char)
793 q->red=ScaleCharToQuantum(ScaleColor5to8((unsigned char)
794 ((word >> 11) & 0x1f)));
795 q->green=ScaleCharToQuantum(ScaleColor6to8((unsigned char)
796 ((word >> 5) & 0x3f)));
797 q->blue=ScaleCharToQuantum(ScaleColor5to8((unsigned char)
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=(long) 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 == (PixelPacket *) NULL)
826 for (x=0; x < (long) image->columns; x++)
828 q->blue=ScaleCharToQuantum(*p++);
829 q->green=ScaleCharToQuantum(*p++);
830 q->red=ScaleCharToQuantum(*p++);
831 if (image->matte != MagickFalse)
832 q->opacity=ScaleCharToQuantum(*p++);
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 % unsigned long RegisterDIBImage(void)
897 ModuleExport unsigned long 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,Image *image)
957 % A description of each parameter follows.
959 % o image_info: the image info.
961 % o image: The image.
964 static MagickBooleanType WriteDIBImage(const ImageInfo *image_info,Image *image)
975 register const IndexPacket
978 register const PixelPacket
985 register unsigned char
996 Open output image file.
998 assert(image_info != (const ImageInfo *) NULL);
999 assert(image_info->signature == MagickSignature);
1000 assert(image != (Image *) NULL);
1001 assert(image->signature == MagickSignature);
1002 if (image->debug != MagickFalse)
1003 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1004 status=OpenBlob(image_info,image,WriteBinaryBlobMode,&image->exception);
1005 if (status == MagickFalse)
1008 Initialize DIB raster file header.
1010 if (image->colorspace != RGBColorspace)
1011 (void) TransformImageColorspace(image,RGBColorspace);
1012 if (image->storage_class == DirectClass)
1015 Full color DIB raster.
1017 dib_info.number_colors=0;
1018 dib_info.bits_per_pixel=(unsigned short) (image->matte ? 32 : 24);
1023 Colormapped DIB raster.
1025 dib_info.bits_per_pixel=8;
1026 if (image_info->depth > 8)
1027 dib_info.bits_per_pixel=16;
1028 if (IsMonochromeImage(image,&image->exception) != MagickFalse)
1029 dib_info.bits_per_pixel=1;
1030 dib_info.number_colors=(dib_info.bits_per_pixel == 16) ? 0 :
1031 (1UL << dib_info.bits_per_pixel);
1033 bytes_per_line=4*((image->columns*dib_info.bits_per_pixel+31)/32);
1035 dib_info.width=(long) image->columns;
1036 dib_info.height=(long) image->rows;
1038 dib_info.compression=(unsigned long) (dib_info.bits_per_pixel == 16 ?
1039 BI_BITFIELDS : BI_RGB);
1040 dib_info.image_size=bytes_per_line*image->rows;
1041 dib_info.x_pixels=75*39;
1042 dib_info.y_pixels=75*39;
1043 switch (image->units)
1045 case UndefinedResolution:
1046 case PixelsPerInchResolution:
1048 dib_info.x_pixels=(unsigned long) (100.0*image->x_resolution/2.54);
1049 dib_info.y_pixels=(unsigned long) (100.0*image->y_resolution/2.54);
1052 case PixelsPerCentimeterResolution:
1054 dib_info.x_pixels=(unsigned long) (100.0*image->x_resolution);
1055 dib_info.y_pixels=(unsigned long) (100.0*image->y_resolution);
1059 dib_info.colors_important=dib_info.number_colors;
1061 Convert MIFF to DIB raster pixels.
1063 pixels=(unsigned char *) AcquireQuantumMemory(dib_info.image_size,
1065 if (pixels == (unsigned char *) NULL)
1066 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
1067 (void) ResetMagickMemory(pixels,0,dib_info.image_size);
1068 switch (dib_info.bits_per_pixel)
1072 register unsigned char
1077 Convert PseudoClass image to a DIB monochrome image.
1079 for (y=0; y < (long) image->rows; y++)
1081 p=GetVirtualPixels(image,0,y,image->columns,1,&image->exception);
1082 if (p == (const PixelPacket *) NULL)
1084 indexes=GetVirtualIndexQueue(image);
1085 q=pixels+(image->rows-y-1)*bytes_per_line;
1088 for (x=0; x < (long) image->columns; x++)
1091 byte|=indexes[x] != 0 ? 0x01 : 0x00;
1103 *q++=(unsigned char) (byte << (8-bit));
1106 for (x=(long) (image->columns+7)/8; x < (long) bytes_per_line; x++)
1108 status=SetImageProgress(image,SaveImageTag,y,image->rows);
1109 if (status == MagickFalse)
1117 Convert PseudoClass packet to DIB pixel.
1119 for (y=0; y < (long) 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 < (long) image->columns; x++)
1127 *q++=(unsigned char) indexes[x];
1128 for ( ; x < (long) bytes_per_line; x++)
1130 status=SetImageProgress(image,SaveImageTag,y,image->rows);
1131 if (status == MagickFalse)
1141 Convert PseudoClass packet to DIB pixel.
1143 for (y=0; y < (long) image->rows; y++)
1145 p=GetVirtualPixels(image,0,y,image->columns,1,&image->exception);
1146 if (p == (const PixelPacket *) NULL)
1148 q=pixels+(image->rows-y-1)*bytes_per_line;
1149 for (x=0; x < (long) image->columns; x++)
1151 word=(unsigned short) ((ScaleColor8to5((unsigned char)
1152 ScaleQuantumToChar(GetRedPixelComponent(p))) << 11) | (ScaleColor8to6((unsigned char)
1153 ScaleQuantumToChar(GetGreenPixelComponent(p))) << 5) | (ScaleColor8to5(
1154 (unsigned char) ScaleQuantumToChar((unsigned char) GetBluePixelComponent(p)) << 0)));
1155 *q++=(unsigned char)(word & 0xff);
1156 *q++=(unsigned char)(word >> 8);
1159 for (x=2L*image->columns; x < (long) bytes_per_line; x++)
1161 status=SetImageProgress(image,SaveImageTag,y,image->rows);
1162 if (status == MagickFalse)
1171 Convert DirectClass packet to DIB RGB pixel.
1173 for (y=0; y < (long) image->rows; y++)
1175 p=GetVirtualPixels(image,0,y,image->columns,1,&image->exception);
1176 if (p == (const PixelPacket *) NULL)
1178 q=pixels+(image->rows-y-1)*bytes_per_line;
1179 for (x=0; x < (long) image->columns; x++)
1181 *q++=ScaleQuantumToChar(GetBluePixelComponent(p));
1182 *q++=ScaleQuantumToChar(GetGreenPixelComponent(p));
1183 *q++=ScaleQuantumToChar(GetRedPixelComponent(p));
1184 if (image->matte != MagickFalse)
1185 *q++=ScaleQuantumToChar(GetOpacityPixelComponent(p));
1188 if (dib_info.bits_per_pixel == 24)
1189 for (x=3L*image->columns; x < (long) bytes_per_line; x++)
1191 status=SetImageProgress(image,SaveImageTag,y,image->rows);
1192 if (status == MagickFalse)
1198 if (dib_info.bits_per_pixel == 8)
1199 if (image_info->compression != NoCompression)
1205 Convert run-length encoded raster pixels.
1207 length=2UL*(bytes_per_line+2UL)+2UL;
1208 dib_data=(unsigned char *) AcquireQuantumMemory(length,
1209 (image->rows+2UL)*sizeof(*dib_data));
1210 if (pixels == (unsigned char *) NULL)
1212 pixels=(unsigned char *) RelinquishMagickMemory(pixels);
1213 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
1215 dib_info.image_size=(unsigned long) EncodeImage(image,bytes_per_line,
1217 pixels=(unsigned char *) RelinquishMagickMemory(pixels);
1219 dib_info.compression = BI_RLE8;
1224 (void) WriteBlobLSBLong(image,dib_info.size);
1225 (void) WriteBlobLSBLong(image,(unsigned long) dib_info.width);
1226 (void) WriteBlobLSBLong(image,(unsigned short) dib_info.height);
1227 (void) WriteBlobLSBShort(image,(unsigned short) dib_info.planes);
1228 (void) WriteBlobLSBShort(image,dib_info.bits_per_pixel);
1229 (void) WriteBlobLSBLong(image,dib_info.compression);
1230 (void) WriteBlobLSBLong(image,dib_info.image_size);
1231 (void) WriteBlobLSBLong(image,dib_info.x_pixels);
1232 (void) WriteBlobLSBLong(image,dib_info.y_pixels);
1233 (void) WriteBlobLSBLong(image,dib_info.number_colors);
1234 (void) WriteBlobLSBLong(image,dib_info.colors_important);
1235 if (image->storage_class == PseudoClass)
1237 if (dib_info.bits_per_pixel <= 8)
1243 Dump colormap to file.
1245 dib_colormap=(unsigned char *) AcquireQuantumMemory((size_t)
1246 (1UL << dib_info.bits_per_pixel),4*sizeof(dib_colormap));
1247 if (dib_colormap == (unsigned char *) NULL)
1248 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
1250 for (i=0; i < (long) MagickMin(image->colors,dib_info.number_colors); i++)
1252 *q++=ScaleQuantumToChar(image->colormap[i].blue);
1253 *q++=ScaleQuantumToChar(image->colormap[i].green);
1254 *q++=ScaleQuantumToChar(image->colormap[i].red);
1257 for ( ; i < (long) (1L << dib_info.bits_per_pixel); i++)
1264 (void) WriteBlob(image,(size_t) (4*(1 << dib_info.bits_per_pixel)),
1266 dib_colormap=(unsigned char *) RelinquishMagickMemory(dib_colormap);
1269 if ((dib_info.bits_per_pixel == 16) &&
1270 (dib_info.compression == BI_BITFIELDS))
1272 (void) WriteBlobLSBLong(image,0xf800);
1273 (void) WriteBlobLSBLong(image,0x07e0);
1274 (void) WriteBlobLSBLong(image,0x001f);
1277 (void) WriteBlob(image,dib_info.image_size,pixels);
1278 pixels=(unsigned char *) RelinquishMagickMemory(pixels);
1279 (void) CloseBlob(image);