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/colormap-private.h"
48 #include "magick/color-private.h"
49 #include "magick/colorspace.h"
50 #include "magick/draw.h"
51 #include "magick/exception.h"
52 #include "magick/exception-private.h"
53 #include "magick/geometry.h"
54 #include "magick/image.h"
55 #include "magick/image-private.h"
56 #include "magick/list.h"
57 #include "magick/log.h"
58 #include "magick/magick.h"
59 #include "magick/memory_.h"
60 #include "magick/monitor.h"
61 #include "magick/monitor-private.h"
62 #include "magick/quantum-private.h"
63 #include "magick/static.h"
64 #include "magick/string_.h"
65 #include "magick/module.h"
66 #include "magick/transform.h"
71 typedef struct _DIBInfo
107 Forward declarations.
109 static MagickBooleanType
110 WriteDIBImage(const ImageInfo *,Image *);
113 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
117 % D e c o d e I m a g e %
121 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
123 % DecodeImage unpacks the packed image pixels into runlength-encoded
126 % The format of the DecodeImage method is:
128 % MagickBooleanType DecodeImage(Image *image,
129 % const MagickBooleanType compression,unsigned char *pixels)
131 % A description of each parameter follows:
133 % o image: the address of a structure of type Image.
135 % o compression: A value of 1 means the compressed pixels are runlength
136 % encoded for a 256-color bitmap. A value of 2 means a 16-color bitmap.
138 % o pixels: The address of a byte (8 bits) array of pixel data created by
139 % the decoding process.
143 static inline size_t MagickMin(const size_t x,const size_t y)
150 static MagickBooleanType DecodeImage(Image *image,
151 const MagickBooleanType compression,unsigned char *pixels)
153 #if !defined(__WINDOWS__) || defined(__MINGW32__)
157 #define BI_BITFIELDS 3
170 register unsigned char
177 assert(image != (Image *) NULL);
178 assert(image->signature == MagickSignature);
179 if (image->debug != MagickFalse)
180 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
181 assert(pixels != (unsigned char *) NULL);
182 (void) ResetMagickMemory(pixels,0,(size_t) image->columns*image->rows*
187 q=pixels+(size_t) image->columns*image->rows;
188 for (y=0; y < (long) image->rows; )
190 if ((p < pixels) || (p >= q))
192 count=ReadBlobByte(image);
197 count=(int) MagickMin((size_t) count,(size_t) (q-p));
201 byte=(unsigned char) ReadBlobByte(image);
202 if (compression == BI_RLE8)
204 for (i=0; i < count; i++)
205 *p++=(unsigned char) byte;
209 for (i=0; i < count; i++)
211 ((i & 0x01) != 0 ? (byte & 0x0f) : ((byte >> 4) & 0x0f));
220 count=ReadBlobByte(image);
232 p=pixels+y*image->columns;
240 x+=ReadBlobByte(image);
241 y+=ReadBlobByte(image);
242 p=pixels+y*image->columns+x;
250 count=(int) MagickMin((size_t) count,(size_t) (q-p));
251 if (compression == BI_RLE8)
252 for (i=0; i < count; i++)
253 *p++=(unsigned char) ReadBlobByte(image);
255 for (i=0; i < count; i++)
258 byte=(unsigned char) ReadBlobByte(image);
260 ((i & 0x01) != 0 ? (byte & 0x0f) : ((byte >> 4) & 0x0f));
266 if (compression == BI_RLE8)
268 if ((count & 0x01) != 0)
269 (void) ReadBlobByte(image);
272 if (((count & 0x03) == 1) || ((count & 0x03) == 2))
273 (void) ReadBlobByte(image);
278 if (SetImageProgress(image,LoadImageTag,y,image->rows) == MagickFalse)
281 (void) ReadBlobByte(image); /* end of line */
282 (void) ReadBlobByte(image);
287 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
291 % E n c o d e I m a g e %
295 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
297 % EncodeImage compresses pixels using a runlength encoded format.
299 % The format of the EncodeImage method is:
301 % static MagickBooleanType EncodeImage(Image *image,
302 % const unsigned long bytes_per_line,const unsigned char *pixels,
303 % unsigned char *compressed_pixels)
305 % A description of each parameter follows:
307 % o image: The image.
309 % o bytes_per_line: the number of bytes in a scanline of compressed pixels
311 % o pixels: The address of a byte (8 bits) array of pixel data created by
312 % the compression process.
314 % o compressed_pixels: The address of a byte (8 bits) array of compressed
318 static size_t EncodeImage(Image *image,const unsigned long bytes_per_line,
319 const unsigned char *pixels,unsigned char *compressed_pixels)
324 register const unsigned char
331 register unsigned char
335 Runlength encode pixels.
337 assert(image != (Image *) NULL);
338 assert(image->signature == MagickSignature);
339 if (image->debug != MagickFalse)
340 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
341 assert(pixels != (const unsigned char *) NULL);
342 assert(compressed_pixels != (unsigned char *) NULL);
346 for (y=0; y < (long) image->rows; y++)
348 for (x=0; x < (long) bytes_per_line; x+=i)
353 for (i=1; ((x+i) < (long) bytes_per_line); i++)
354 if ((*(p+i) != *p) || (i == 255))
356 *q++=(unsigned char) i;
365 if (SetImageProgress(image,LoadImageTag,y,image->rows) == MagickFalse)
373 return((size_t) (q-compressed_pixels));
377 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
385 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
387 % IsDIB() returns MagickTrue if the image format type, identified by the
388 % magick string, is DIB.
390 % The format of the IsDIB method is:
392 % MagickBooleanType IsDIB(const unsigned char *magick,const size_t length)
394 % A description of each parameter follows:
396 % o magick: compare image format pattern against these bytes.
398 % o length: Specifies the length of the magick string.
401 static MagickBooleanType IsDIB(const unsigned char *magick,const size_t length)
405 if (memcmp(magick,"\050\000",2) == 0)
411 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
415 % R e a d D I B I m a g e %
419 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
421 % ReadDIBImage() reads a Microsoft Windows bitmap image file and
422 % returns it. It allocates the memory necessary for the new Image structure
423 % and returns a pointer to the new image.
425 % The format of the ReadDIBImage method is:
427 % image=ReadDIBImage(image_info)
429 % A description of each parameter follows:
431 % o image_info: the image info.
433 % o exception: return any errors or warnings in this structure.
437 static inline long MagickAbsoluteValue(const long x)
444 static inline size_t MagickMax(const size_t x,const size_t y)
451 static Image *ReadDIBImage(const ImageInfo *image_info,ExceptionInfo *exception)
481 register unsigned char
499 assert(image_info != (const ImageInfo *) NULL);
500 assert(image_info->signature == MagickSignature);
501 if (image_info->debug != MagickFalse)
502 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
503 image_info->filename);
504 assert(exception != (ExceptionInfo *) NULL);
505 assert(exception->signature == MagickSignature);
506 image=AcquireImage(image_info);
507 status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception);
508 if (status == MagickFalse)
510 image=DestroyImageList(image);
511 return((Image *) NULL);
514 Determine if this a DIB file.
516 (void) ResetMagickMemory(&dib_info,0,sizeof(dib_info));
517 dib_info.size=ReadBlobLSBLong(image);
518 if (dib_info.size!=40)
519 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
521 Microsoft Windows 3.X DIB image file.
523 dib_info.width=(short) ReadBlobLSBLong(image);
524 dib_info.height=(short) ReadBlobLSBLong(image);
525 dib_info.planes=ReadBlobLSBShort(image);
526 dib_info.bits_per_pixel=ReadBlobLSBShort(image);
527 dib_info.compression=ReadBlobLSBLong(image);
528 dib_info.image_size=ReadBlobLSBLong(image);
529 dib_info.x_pixels=ReadBlobLSBLong(image);
530 dib_info.y_pixels=ReadBlobLSBLong(image);
531 dib_info.number_colors=ReadBlobLSBLong(image);
532 dib_info.colors_important=ReadBlobLSBLong(image);
533 if ((dib_info.compression == BI_BITFIELDS) &&
534 ((dib_info.bits_per_pixel == 16) || (dib_info.bits_per_pixel == 32)))
536 dib_info.red_mask=ReadBlobLSBLong(image);
537 dib_info.green_mask=ReadBlobLSBLong(image);
538 dib_info.blue_mask=ReadBlobLSBLong(image);
540 image->matte=dib_info.bits_per_pixel == 32 ? MagickTrue : MagickFalse;
541 image->columns=(unsigned long) MagickAbsoluteValue(dib_info.width);
542 image->rows=(unsigned long) MagickAbsoluteValue(dib_info.height);
544 if ((dib_info.number_colors != 0) || (dib_info.bits_per_pixel < 16))
546 image->storage_class=PseudoClass;
547 image->colors=dib_info.number_colors;
548 if (image->colors == 0)
549 image->colors=1L << dib_info.bits_per_pixel;
551 if (image_info->size)
559 flags=ParseAbsoluteGeometry(image_info->size,&geometry);
560 if (flags & WidthValue)
561 if ((geometry.width != 0) && (geometry.width < image->columns))
562 image->columns=geometry.width;
563 if (flags & HeightValue)
564 if ((geometry.height != 0) && (geometry.height < image->rows))
565 image->rows=geometry.height;
567 if (image->storage_class == PseudoClass)
577 Read DIB raster colormap.
579 if (AcquireImageColormap(image,image->colors) == MagickFalse)
580 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
581 length=(size_t) image->colors;
582 dib_colormap=(unsigned char *) AcquireQuantumMemory(length,
583 4*sizeof(*dib_colormap));
584 if (dib_colormap == (unsigned char *) NULL)
585 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
587 count=ReadBlob(image,packet_size*image->colors,dib_colormap);
588 if (count != (ssize_t) (packet_size*image->colors))
589 ThrowReaderException(CorruptImageError,"InsufficientImageDataInFile");
591 for (i=0; i < (long) image->colors; i++)
593 image->colormap[i].blue=ScaleCharToQuantum(*p++);
594 image->colormap[i].green=ScaleCharToQuantum(*p++);
595 image->colormap[i].red=ScaleCharToQuantum(*p++);
596 if (packet_size == 4)
599 dib_colormap=(unsigned char *) RelinquishMagickMemory(dib_colormap);
604 if (dib_info.compression == BI_RLE4)
605 dib_info.bits_per_pixel<<=1;
606 bytes_per_line=4*((image->columns*dib_info.bits_per_pixel+31)/32);
607 length=bytes_per_line*image->rows;
608 pixels=(unsigned char *) AcquireQuantumMemory((size_t) image->rows,
609 MagickMax(bytes_per_line,image->columns+256UL)*sizeof(*pixels));
610 if (pixels == (unsigned char *) NULL)
611 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
612 if ((dib_info.compression == BI_RGB) ||
613 (dib_info.compression == BI_BITFIELDS))
615 count=ReadBlob(image,length,pixels);
616 if (count != (ssize_t) (length))
617 ThrowReaderException(CorruptImageError,"InsufficientImageDataInFile");
622 Convert run-length encoded raster pixels.
624 status=DecodeImage(image,dib_info.compression ? MagickTrue : MagickFalse,
626 if (status == MagickFalse)
627 ThrowReaderException(CorruptImageError,"UnableToRunlengthDecodeImage");
630 Initialize image structure.
632 image->units=PixelsPerCentimeterResolution;
633 image->x_resolution=(double) dib_info.x_pixels/100.0;
634 image->y_resolution=(double) dib_info.y_pixels/100.0;
636 Convert DIB raster image to pixel packets.
638 switch (dib_info.bits_per_pixel)
643 Convert bitmap scanline.
645 for (y=(long) image->rows-1; y >= 0; y--)
647 p=pixels+(image->rows-y-1)*bytes_per_line;
648 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
649 if (q == (PixelPacket *) NULL)
651 indexes=GetAuthenticIndexQueue(image);
652 for (x=0; x < ((long) image->columns-7); x+=8)
654 for (bit=0; bit < 8; bit++)
656 index=(IndexPacket) ((*p) & (0x80 >> bit) ? 0x01 : 0x00);
657 indexes[x+bit]=index;
658 *q++=image->colormap[(long) index];
662 if ((image->columns % 8) != 0)
664 for (bit=0; bit < (long) (image->columns % 8); bit++)
666 index=(IndexPacket) ((*p) & (0x80 >> bit) ? 0x01 : 0x00);
667 indexes[x+bit]=index;
668 *q++=image->colormap[(long) index];
672 if (SyncAuthenticPixels(image,exception) == MagickFalse)
674 if (image->previous == (Image *) NULL)
676 status=SetImageProgress(image,LoadImageTag,image->rows-y-1,
678 if (status == MagickFalse)
687 Convert PseudoColor scanline.
689 for (y=(long) image->rows-1; y >= 0; y--)
691 p=pixels+(image->rows-y-1)*bytes_per_line;
692 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
693 if (q == (PixelPacket *) NULL)
695 indexes=GetAuthenticIndexQueue(image);
696 for (x=0; x < ((long) image->columns-1); x+=2)
698 index=ConstrainColormapIndex(image,(*p >> 4) & 0xf);
700 *q++=image->colormap[(long) index];
701 index=ConstrainColormapIndex(image,*p & 0xf);
703 *q++=image->colormap[(long) index];
706 if ((image->columns % 2) != 0)
708 index=ConstrainColormapIndex(image,(*p >> 4) & 0xf);
710 *q++=image->colormap[(long) index];
713 if (SyncAuthenticPixels(image,exception) == MagickFalse)
715 if (image->previous == (Image *) NULL)
717 status=SetImageProgress(image,LoadImageTag,image->rows-y-1,
719 if (status == MagickFalse)
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=(long) 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 < (long) image->columns; x++)
742 index=ConstrainColormapIndex(image,*p);
744 *q=image->colormap[(long) index];
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)
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=(long) 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 < (long) image->columns; x++)
781 if (dib_info.red_mask == 0)
783 q->red=ScaleCharToQuantum(ScaleColor5to8((unsigned char)
784 ((word >> 10) & 0x1f)));
785 q->green=ScaleCharToQuantum(ScaleColor5to8((unsigned char)
786 ((word >> 5) & 0x1f)));
787 q->blue=ScaleCharToQuantum(ScaleColor5to8((unsigned char)
792 q->red=ScaleCharToQuantum(ScaleColor5to8((unsigned char)
793 ((word >> 11) & 0x1f)));
794 q->green=ScaleCharToQuantum(ScaleColor6to8((unsigned char)
795 ((word >> 5) & 0x3f)));
796 q->blue=ScaleCharToQuantum(ScaleColor5to8((unsigned char)
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=(long) 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 < (long) image->columns; x++)
827 q->blue=ScaleCharToQuantum(*p++);
828 q->green=ScaleCharToQuantum(*p++);
829 q->red=ScaleCharToQuantum(*p++);
830 if (image->matte != MagickFalse)
831 q->opacity=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 image=DestroyImageList(image);
865 return((Image *) NULL);
867 DuplicateBlob(flipped_image,image);
868 image=DestroyImage(image);
871 (void) CloseBlob(image);
872 return(GetFirstImageInList(image));
876 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
880 % R e g i s t e r D I B I m a g e %
884 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
886 % RegisterDIBImage() adds attributes for the DIB image format to
887 % the list of supported formats. The attributes include the image format
888 % tag, a method to read and/or write the format, whether the format
889 % supports the saving of more than one frame to the same file or blob,
890 % whether the format supports native in-memory I/O, and a brief
891 % description of the format.
893 % The format of the RegisterDIBImage method is:
895 % unsigned long RegisterDIBImage(void)
898 ModuleExport unsigned long RegisterDIBImage(void)
903 entry=SetMagickInfo("DIB");
904 entry->decoder=(DecodeImageHandler *) ReadDIBImage;
905 entry->encoder=(EncodeImageHandler *) WriteDIBImage;
906 entry->magick=(IsImageFormatHandler *) IsDIB;
907 entry->adjoin=MagickFalse;
908 entry->stealth=MagickTrue;
909 entry->description=ConstantString(
910 "Microsoft Windows 3.X Packed Device-Independent Bitmap");
911 entry->module=ConstantString("DIB");
912 (void) RegisterMagickInfo(entry);
913 return(MagickImageCoderSignature);
917 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
921 % U n r e g i s t e r D I B I m a g e %
925 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
927 % UnregisterDIBImage() removes format registrations made by the
928 % DIB module from the list of supported formats.
930 % The format of the UnregisterDIBImage method is:
932 % UnregisterDIBImage(void)
935 ModuleExport void UnregisterDIBImage(void)
937 (void) UnregisterMagickInfo("DIB");
941 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
945 % W r i t e D I B I m a g e %
949 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
951 % WriteDIBImage() writes an image in Microsoft Windows bitmap encoded
954 % The format of the WriteDIBImage method is:
956 % MagickBooleanType WriteDIBImage(const ImageInfo *image_info,Image *image)
958 % A description of each parameter follows.
960 % o image_info: the image info.
962 % o image: The image.
965 static MagickBooleanType WriteDIBImage(const ImageInfo *image_info,Image *image)
976 register const IndexPacket
979 register const PixelPacket
986 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 status=OpenBlob(image_info,image,WriteBinaryBlobMode,&image->exception);
1006 if (status == MagickFalse)
1009 Initialize DIB raster file header.
1011 if (image->colorspace != RGBColorspace)
1012 (void) TransformImageColorspace(image,RGBColorspace);
1013 if (image->storage_class == DirectClass)
1016 Full color DIB raster.
1018 dib_info.number_colors=0;
1019 dib_info.bits_per_pixel=(unsigned short) (image->matte ? 32 : 24);
1024 Colormapped DIB raster.
1026 dib_info.bits_per_pixel=8;
1027 if (image_info->depth > 8)
1028 dib_info.bits_per_pixel=16;
1029 if (IsMonochromeImage(image,&image->exception) != MagickFalse)
1030 dib_info.bits_per_pixel=1;
1031 dib_info.number_colors=(dib_info.bits_per_pixel == 16) ? 0 :
1032 (1UL << dib_info.bits_per_pixel);
1034 bytes_per_line=4*((image->columns*dib_info.bits_per_pixel+31)/32);
1036 dib_info.width=(long) image->columns;
1037 dib_info.height=(long) image->rows;
1039 dib_info.compression=(unsigned long) (dib_info.bits_per_pixel == 16 ?
1040 BI_BITFIELDS : BI_RGB);
1041 dib_info.image_size=bytes_per_line*image->rows;
1042 dib_info.x_pixels=75*39;
1043 dib_info.y_pixels=75*39;
1044 switch (image->units)
1046 case UndefinedResolution:
1047 case PixelsPerInchResolution:
1049 dib_info.x_pixels=(unsigned long) (100.0*image->x_resolution/2.54);
1050 dib_info.y_pixels=(unsigned long) (100.0*image->y_resolution/2.54);
1053 case PixelsPerCentimeterResolution:
1055 dib_info.x_pixels=(unsigned long) (100.0*image->x_resolution);
1056 dib_info.y_pixels=(unsigned long) (100.0*image->y_resolution);
1060 dib_info.colors_important=dib_info.number_colors;
1062 Convert MIFF to DIB raster pixels.
1064 pixels=(unsigned char *) AcquireQuantumMemory(dib_info.image_size,
1066 if (pixels == (unsigned char *) NULL)
1067 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
1068 (void) ResetMagickMemory(pixels,0,dib_info.image_size);
1069 switch (dib_info.bits_per_pixel)
1073 register unsigned char
1078 Convert PseudoClass image to a DIB monochrome image.
1080 for (y=0; y < (long) image->rows; y++)
1082 p=GetVirtualPixels(image,0,y,image->columns,1,&image->exception);
1083 if (p == (const PixelPacket *) NULL)
1085 indexes=GetVirtualIndexQueue(image);
1086 q=pixels+(image->rows-y-1)*bytes_per_line;
1089 for (x=0; x < (long) image->columns; x++)
1092 byte|=indexes[x] != 0 ? 0x01 : 0x00;
1104 *q++=(unsigned char) (byte << (8-bit));
1107 for (x=(long) (image->columns+7)/8; x < (long) bytes_per_line; x++)
1109 status=SetImageProgress(image,SaveImageTag,y,image->rows);
1110 if (status == MagickFalse)
1118 Convert PseudoClass packet to DIB pixel.
1120 for (y=0; y < (long) image->rows; y++)
1122 p=GetVirtualPixels(image,0,y,image->columns,1,&image->exception);
1123 if (p == (const PixelPacket *) NULL)
1125 indexes=GetVirtualIndexQueue(image);
1126 q=pixels+(image->rows-y-1)*bytes_per_line;
1127 for (x=0; x < (long) image->columns; x++)
1128 *q++=(unsigned char) indexes[x];
1129 for ( ; x < (long) bytes_per_line; x++)
1131 status=SetImageProgress(image,SaveImageTag,y,image->rows);
1132 if (status == MagickFalse)
1142 Convert PseudoClass packet to DIB pixel.
1144 for (y=0; y < (long) 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 < (long) image->columns; x++)
1152 word=(unsigned short) ((ScaleColor8to5((unsigned char)
1153 ScaleQuantumToChar(GetRedSample(p))) << 11) | (ScaleColor8to6((unsigned char)
1154 ScaleQuantumToChar(GetGreenSample(p))) << 5) | (ScaleColor8to5(
1155 (unsigned char) ScaleQuantumToChar((unsigned char) GetBlueSample(p)) << 0)));
1156 *q++=(unsigned char)(word & 0xff);
1157 *q++=(unsigned char)(word >> 8);
1160 for (x=2L*image->columns; x < (long) bytes_per_line; x++)
1162 status=SetImageProgress(image,SaveImageTag,y,image->rows);
1163 if (status == MagickFalse)
1172 Convert DirectClass packet to DIB RGB pixel.
1174 for (y=0; y < (long) image->rows; y++)
1176 p=GetVirtualPixels(image,0,y,image->columns,1,&image->exception);
1177 if (p == (const PixelPacket *) NULL)
1179 q=pixels+(image->rows-y-1)*bytes_per_line;
1180 for (x=0; x < (long) image->columns; x++)
1182 *q++=ScaleQuantumToChar(GetBlueSample(p));
1183 *q++=ScaleQuantumToChar(GetGreenSample(p));
1184 *q++=ScaleQuantumToChar(GetRedSample(p));
1185 if (image->matte != MagickFalse)
1186 *q++=ScaleQuantumToChar(GetOpacitySample(p));
1189 if (dib_info.bits_per_pixel == 24)
1190 for (x=3L*image->columns; x < (long) bytes_per_line; x++)
1192 status=SetImageProgress(image,SaveImageTag,y,image->rows);
1193 if (status == MagickFalse)
1199 if (dib_info.bits_per_pixel == 8)
1200 if (image_info->compression != NoCompression)
1206 Convert run-length encoded raster pixels.
1208 length=2UL*(bytes_per_line+2UL)+2UL;
1209 dib_data=(unsigned char *) AcquireQuantumMemory(length,
1210 (image->rows+2UL)*sizeof(*dib_data));
1211 if (pixels == (unsigned char *) NULL)
1213 pixels=(unsigned char *) RelinquishMagickMemory(pixels);
1214 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
1216 dib_info.image_size=(unsigned long) EncodeImage(image,bytes_per_line,
1218 pixels=(unsigned char *) RelinquishMagickMemory(pixels);
1220 dib_info.compression = BI_RLE8;
1225 (void) WriteBlobLSBLong(image,dib_info.size);
1226 (void) WriteBlobLSBLong(image,(unsigned long) dib_info.width);
1227 (void) WriteBlobLSBLong(image,(unsigned short) dib_info.height);
1228 (void) WriteBlobLSBShort(image,(unsigned short) dib_info.planes);
1229 (void) WriteBlobLSBShort(image,dib_info.bits_per_pixel);
1230 (void) WriteBlobLSBLong(image,dib_info.compression);
1231 (void) WriteBlobLSBLong(image,dib_info.image_size);
1232 (void) WriteBlobLSBLong(image,dib_info.x_pixels);
1233 (void) WriteBlobLSBLong(image,dib_info.y_pixels);
1234 (void) WriteBlobLSBLong(image,dib_info.number_colors);
1235 (void) WriteBlobLSBLong(image,dib_info.colors_important);
1236 if (image->storage_class == PseudoClass)
1238 if (dib_info.bits_per_pixel <= 8)
1244 Dump colormap to file.
1246 dib_colormap=(unsigned char *) AcquireQuantumMemory((size_t)
1247 (1UL << dib_info.bits_per_pixel),4*sizeof(dib_colormap));
1248 if (dib_colormap == (unsigned char *) NULL)
1249 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
1251 for (i=0; i < (long) MagickMin(image->colors,dib_info.number_colors); i++)
1253 *q++=ScaleQuantumToChar(image->colormap[i].blue);
1254 *q++=ScaleQuantumToChar(image->colormap[i].green);
1255 *q++=ScaleQuantumToChar(image->colormap[i].red);
1258 for ( ; i < (long) (1L << dib_info.bits_per_pixel); i++)
1265 (void) WriteBlob(image,(size_t) (4*(1 << dib_info.bits_per_pixel)),
1267 dib_colormap=(unsigned char *) RelinquishMagickMemory(dib_colormap);
1270 if ((dib_info.bits_per_pixel == 16) &&
1271 (dib_info.compression == BI_BITFIELDS))
1273 (void) WriteBlobLSBLong(image,0xf800);
1274 (void) WriteBlobLSBLong(image,0x07e0);
1275 (void) WriteBlobLSBLong(image,0x001f);
1278 (void) WriteBlob(image,dib_info.image_size,pixels);
1279 pixels=(unsigned char *) RelinquishMagickMemory(pixels);
1280 (void) CloseBlob(image);