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(MAGICKCORE_WINDOWS_SUPPORT) || 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 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 % unsigned long RegisterDIBImage(void)
896 ModuleExport unsigned long 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)
974 register const IndexPacket
977 register const PixelPacket
984 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=(long) image->columns;
1035 dib_info.height=(long) image->rows;
1037 dib_info.compression=(unsigned long) (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=(unsigned long) (100.0*image->x_resolution/2.54);
1048 dib_info.y_pixels=(unsigned long) (100.0*image->y_resolution/2.54);
1051 case PixelsPerCentimeterResolution:
1053 dib_info.x_pixels=(unsigned long) (100.0*image->x_resolution);
1054 dib_info.y_pixels=(unsigned long) (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 < (long) 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 < (long) image->columns; x++)
1090 byte|=indexes[x] != 0 ? 0x01 : 0x00;
1102 *q++=(unsigned char) (byte << (8-bit));
1105 for (x=(long) (image->columns+7)/8; x < (long) bytes_per_line; x++)
1107 status=SetImageProgress(image,SaveImageTag,y,image->rows);
1108 if (status == MagickFalse)
1116 Convert PseudoClass packet to DIB pixel.
1118 for (y=0; y < (long) image->rows; y++)
1120 p=GetVirtualPixels(image,0,y,image->columns,1,&image->exception);
1121 if (p == (const PixelPacket *) NULL)
1123 indexes=GetVirtualIndexQueue(image);
1124 q=pixels+(image->rows-y-1)*bytes_per_line;
1125 for (x=0; x < (long) image->columns; x++)
1126 *q++=(unsigned char) indexes[x];
1127 for ( ; x < (long) bytes_per_line; x++)
1129 status=SetImageProgress(image,SaveImageTag,y,image->rows);
1130 if (status == MagickFalse)
1140 Convert PseudoClass packet to DIB pixel.
1142 for (y=0; y < (long) image->rows; y++)
1144 p=GetVirtualPixels(image,0,y,image->columns,1,&image->exception);
1145 if (p == (const PixelPacket *) NULL)
1147 q=pixels+(image->rows-y-1)*bytes_per_line;
1148 for (x=0; x < (long) image->columns; x++)
1150 word=(unsigned short) ((ScaleColor8to5((unsigned char)
1151 ScaleQuantumToChar(GetRedPixelComponent(p))) << 11) | (ScaleColor8to6((unsigned char)
1152 ScaleQuantumToChar(GetGreenPixelComponent(p))) << 5) | (ScaleColor8to5(
1153 (unsigned char) ScaleQuantumToChar((unsigned char) GetBluePixelComponent(p)) << 0)));
1154 *q++=(unsigned char)(word & 0xff);
1155 *q++=(unsigned char)(word >> 8);
1158 for (x=2L*image->columns; x < (long) bytes_per_line; x++)
1160 status=SetImageProgress(image,SaveImageTag,y,image->rows);
1161 if (status == MagickFalse)
1170 Convert DirectClass packet to DIB RGB pixel.
1172 for (y=0; y < (long) image->rows; y++)
1174 p=GetVirtualPixels(image,0,y,image->columns,1,&image->exception);
1175 if (p == (const PixelPacket *) NULL)
1177 q=pixels+(image->rows-y-1)*bytes_per_line;
1178 for (x=0; x < (long) image->columns; x++)
1180 *q++=ScaleQuantumToChar(GetBluePixelComponent(p));
1181 *q++=ScaleQuantumToChar(GetGreenPixelComponent(p));
1182 *q++=ScaleQuantumToChar(GetRedPixelComponent(p));
1183 if (image->matte != MagickFalse)
1184 *q++=ScaleQuantumToChar(GetOpacityPixelComponent(p));
1187 if (dib_info.bits_per_pixel == 24)
1188 for (x=3L*image->columns; x < (long) bytes_per_line; x++)
1190 status=SetImageProgress(image,SaveImageTag,y,image->rows);
1191 if (status == MagickFalse)
1197 if (dib_info.bits_per_pixel == 8)
1198 if (image_info->compression != NoCompression)
1204 Convert run-length encoded raster pixels.
1206 length=2UL*(bytes_per_line+2UL)+2UL;
1207 dib_data=(unsigned char *) AcquireQuantumMemory(length,
1208 (image->rows+2UL)*sizeof(*dib_data));
1209 if (pixels == (unsigned char *) NULL)
1211 pixels=(unsigned char *) RelinquishMagickMemory(pixels);
1212 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
1214 dib_info.image_size=(unsigned long) EncodeImage(image,bytes_per_line,
1216 pixels=(unsigned char *) RelinquishMagickMemory(pixels);
1218 dib_info.compression = BI_RLE8;
1223 (void) WriteBlobLSBLong(image,dib_info.size);
1224 (void) WriteBlobLSBLong(image,(unsigned long) dib_info.width);
1225 (void) WriteBlobLSBLong(image,(unsigned short) dib_info.height);
1226 (void) WriteBlobLSBShort(image,(unsigned short) dib_info.planes);
1227 (void) WriteBlobLSBShort(image,dib_info.bits_per_pixel);
1228 (void) WriteBlobLSBLong(image,dib_info.compression);
1229 (void) WriteBlobLSBLong(image,dib_info.image_size);
1230 (void) WriteBlobLSBLong(image,dib_info.x_pixels);
1231 (void) WriteBlobLSBLong(image,dib_info.y_pixels);
1232 (void) WriteBlobLSBLong(image,dib_info.number_colors);
1233 (void) WriteBlobLSBLong(image,dib_info.colors_important);
1234 if (image->storage_class == PseudoClass)
1236 if (dib_info.bits_per_pixel <= 8)
1242 Dump colormap to file.
1244 dib_colormap=(unsigned char *) AcquireQuantumMemory((size_t)
1245 (1UL << dib_info.bits_per_pixel),4*sizeof(dib_colormap));
1246 if (dib_colormap == (unsigned char *) NULL)
1247 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
1249 for (i=0; i < (long) MagickMin(image->colors,dib_info.number_colors); i++)
1251 *q++=ScaleQuantumToChar(image->colormap[i].blue);
1252 *q++=ScaleQuantumToChar(image->colormap[i].green);
1253 *q++=ScaleQuantumToChar(image->colormap[i].red);
1256 for ( ; i < (long) (1L << dib_info.bits_per_pixel); i++)
1263 (void) WriteBlob(image,(size_t) (4*(1 << dib_info.bits_per_pixel)),
1265 dib_colormap=(unsigned char *) RelinquishMagickMemory(dib_colormap);
1268 if ((dib_info.bits_per_pixel == 16) &&
1269 (dib_info.compression == BI_BITFIELDS))
1271 (void) WriteBlobLSBLong(image,0xf800);
1272 (void) WriteBlobLSBLong(image,0x07e0);
1273 (void) WriteBlobLSBLong(image,0x001f);
1276 (void) WriteBlob(image,dib_info.image_size,pixels);
1277 pixels=(unsigned char *) RelinquishMagickMemory(pixels);
1278 (void) CloseBlob(image);