2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
13 % Read/Write Windows DIB Image Format %
20 % Copyright 1999-2016 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.
146 static MagickBooleanType DecodeImage(Image *image,
147 const MagickBooleanType compression,unsigned char *pixels)
149 #if !defined(MAGICKCORE_WINDOWS_SUPPORT) || defined(__MINGW32__) || defined(__MINGW64__)
153 #define BI_BITFIELDS 3
170 register unsigned char
177 assert(image != (Image *) NULL);
178 assert(image->signature == MagickCoreSignature);
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 < (ssize_t) 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 size_t 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 size_t 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 == MagickCoreSignature);
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 < (ssize_t) image->rows; y++)
348 for (x=0; x < (ssize_t) bytes_per_line; x+=i)
353 for (i=1; ((x+i) < (ssize_t) 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.
436 static Image *ReadDIBImage(const ImageInfo *image_info,ExceptionInfo *exception)
462 register unsigned char
481 assert(image_info != (const ImageInfo *) NULL);
482 assert(image_info->signature == MagickCoreSignature);
483 if (image_info->debug != MagickFalse)
484 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
485 image_info->filename);
486 assert(exception != (ExceptionInfo *) NULL);
487 assert(exception->signature == MagickCoreSignature);
488 image=AcquireImage(image_info,exception);
489 status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception);
490 if (status == MagickFalse)
492 image=DestroyImageList(image);
493 return((Image *) NULL);
496 Determine if this a DIB file.
498 (void) ResetMagickMemory(&dib_info,0,sizeof(dib_info));
499 dib_info.size=ReadBlobLSBLong(image);
500 if (dib_info.size != 40)
501 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
503 Microsoft Windows 3.X DIB image file.
505 dib_info.width=ReadBlobLSBSignedLong(image);
506 dib_info.height=ReadBlobLSBSignedLong(image);
507 dib_info.planes=ReadBlobLSBShort(image);
508 dib_info.bits_per_pixel=ReadBlobLSBShort(image);
509 if (dib_info.bits_per_pixel > 32)
510 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
511 dib_info.compression=ReadBlobLSBLong(image);
512 dib_info.image_size=ReadBlobLSBLong(image);
513 dib_info.x_pixels=ReadBlobLSBLong(image);
514 dib_info.y_pixels=ReadBlobLSBLong(image);
515 dib_info.number_colors=ReadBlobLSBLong(image);
516 dib_info.colors_important=ReadBlobLSBLong(image);
517 if ((dib_info.bits_per_pixel != 1) && (dib_info.bits_per_pixel != 4) &&
518 (dib_info.bits_per_pixel != 8) && (dib_info.bits_per_pixel != 16) &&
519 (dib_info.bits_per_pixel != 24) && (dib_info.bits_per_pixel != 32))
520 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
521 if ((dib_info.compression == BI_BITFIELDS) &&
522 ((dib_info.bits_per_pixel == 16) || (dib_info.bits_per_pixel == 32)))
524 dib_info.red_mask=ReadBlobLSBLong(image);
525 dib_info.green_mask=ReadBlobLSBLong(image);
526 dib_info.blue_mask=ReadBlobLSBLong(image);
528 if (EOFBlob(image) != MagickFalse)
529 ThrowReaderException(CorruptImageError,"UnexpectedEndOfFile");
530 if (dib_info.width <= 0)
531 ThrowReaderException(CorruptImageError,"NegativeOrZeroImageSize");
532 if (dib_info.height == 0)
533 ThrowReaderException(CorruptImageError,"NegativeOrZeroImageSize");
534 if (dib_info.planes != 1)
535 ThrowReaderException(CorruptImageError,"StaticPlanesValueNotEqualToOne");
536 if ((dib_info.bits_per_pixel != 1) && (dib_info.bits_per_pixel != 4) &&
537 (dib_info.bits_per_pixel != 8) && (dib_info.bits_per_pixel != 16) &&
538 (dib_info.bits_per_pixel != 24) && (dib_info.bits_per_pixel != 32))
539 ThrowReaderException(CorruptImageError,"UnrecognizedBitsPerPixel");
540 if (dib_info.bits_per_pixel < 16 &&
541 dib_info.number_colors > (size_t) (1UL << dib_info.bits_per_pixel))
542 ThrowReaderException(CorruptImageError,"UnrecognizedNumberOfColors");
543 if ((dib_info.compression == 1) && (dib_info.bits_per_pixel != 8))
544 ThrowReaderException(CorruptImageError,"UnrecognizedBitsPerPixel");
545 if ((dib_info.compression == 2) && (dib_info.bits_per_pixel != 4))
546 ThrowReaderException(CorruptImageError,"UnrecognizedBitsPerPixel");
547 if ((dib_info.compression == 3) && (dib_info.bits_per_pixel < 16))
548 ThrowReaderException(CorruptImageError,"UnrecognizedBitsPerPixel");
549 switch (dib_info.compression)
557 ThrowReaderException(CoderError,"JPEGCompressNotSupported");
559 ThrowReaderException(CoderError,"PNGCompressNotSupported");
561 ThrowReaderException(CorruptImageError,"UnrecognizedImageCompression");
563 image->columns=(size_t) MagickAbsoluteValue(dib_info.width);
564 image->rows=(size_t) MagickAbsoluteValue(dib_info.height);
566 image->alpha_trait=dib_info.bits_per_pixel == 32 ? BlendPixelTrait :
568 if ((dib_info.number_colors > 256) || (dib_info.colors_important > 256))
569 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
570 if ((dib_info.number_colors != 0) || (dib_info.bits_per_pixel < 16))
575 image->storage_class=PseudoClass;
576 image->colors=dib_info.number_colors;
578 if (image->colors == 0)
579 image->colors=one << dib_info.bits_per_pixel;
581 if (image_info->size)
589 flags=ParseAbsoluteGeometry(image_info->size,&geometry);
590 if (flags & WidthValue)
591 if ((geometry.width != 0) && (geometry.width < image->columns))
592 image->columns=geometry.width;
593 if (flags & HeightValue)
594 if ((geometry.height != 0) && (geometry.height < image->rows))
595 image->rows=geometry.height;
597 status=SetImageExtent(image,image->columns,image->rows,exception);
598 if (status == MagickFalse)
599 return(DestroyImageList(image));
600 if (image->storage_class == PseudoClass)
610 Read DIB raster colormap.
612 if (AcquireImageColormap(image,image->colors,exception) == MagickFalse)
613 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
614 length=(size_t) image->colors;
615 dib_colormap=(unsigned char *) AcquireQuantumMemory(length,
616 4*sizeof(*dib_colormap));
617 if (dib_colormap == (unsigned char *) NULL)
618 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
620 count=ReadBlob(image,packet_size*image->colors,dib_colormap);
621 if (count != (ssize_t) (packet_size*image->colors))
622 ThrowReaderException(CorruptImageError,"InsufficientImageDataInFile");
624 for (i=0; i < (ssize_t) image->colors; i++)
626 image->colormap[i].blue=ScaleCharToQuantum(*p++);
627 image->colormap[i].green=ScaleCharToQuantum(*p++);
628 image->colormap[i].red=ScaleCharToQuantum(*p++);
629 if (packet_size == 4)
632 dib_colormap=(unsigned char *) RelinquishMagickMemory(dib_colormap);
637 if (dib_info.compression == BI_RLE4)
638 dib_info.bits_per_pixel<<=1;
639 bytes_per_line=4*((image->columns*dib_info.bits_per_pixel+31)/32);
640 length=bytes_per_line*image->rows;
641 pixel_info=AcquireVirtualMemory((size_t) image->rows,MagickMax(
642 bytes_per_line,image->columns+256UL)*sizeof(*pixels));
643 if (pixel_info == (MemoryInfo *) NULL)
644 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
645 pixels=(unsigned char *) GetVirtualMemoryBlob(pixel_info);
646 if ((dib_info.compression == BI_RGB) ||
647 (dib_info.compression == BI_BITFIELDS))
649 count=ReadBlob(image,length,pixels);
650 if (count != (ssize_t) (length))
652 pixel_info=RelinquishVirtualMemory(pixel_info);
653 ThrowReaderException(CorruptImageError,"InsufficientImageDataInFile");
659 Convert run-length encoded raster pixels.
661 status=DecodeImage(image,dib_info.compression ? MagickTrue : MagickFalse,
663 if (status == MagickFalse)
665 pixel_info=RelinquishVirtualMemory(pixel_info);
666 ThrowReaderException(CorruptImageError,
667 "UnableToRunlengthDecodeImage");
671 Initialize image structure.
673 image->units=PixelsPerCentimeterResolution;
674 image->resolution.x=(double) dib_info.x_pixels/100.0;
675 image->resolution.y=(double) dib_info.y_pixels/100.0;
677 Convert DIB raster image to pixel packets.
679 switch (dib_info.bits_per_pixel)
684 Convert bitmap scanline.
686 for (y=(ssize_t) image->rows-1; y >= 0; y--)
688 p=pixels+(image->rows-y-1)*bytes_per_line;
689 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
690 if (q == (Quantum *) NULL)
692 for (x=0; x < ((ssize_t) image->columns-7); x+=8)
694 for (bit=0; bit < 8; bit++)
696 index=(Quantum) ((*p) & (0x80 >> bit) ? 0x01 : 0x00);
697 SetPixelIndex(image,index,q);
698 q+=GetPixelChannels(image);
702 if ((image->columns % 8) != 0)
704 for (bit=0; bit < (ssize_t) (image->columns % 8); bit++)
706 index=(Quantum) ((*p) & (0x80 >> bit) ? 0x01 : 0x00);
707 SetPixelIndex(image,index,q);
708 q+=GetPixelChannels(image);
712 if (SyncAuthenticPixels(image,exception) == MagickFalse)
714 if (image->previous == (Image *) NULL)
716 status=SetImageProgress(image,LoadImageTag,image->rows-y-1,
718 if (status == MagickFalse)
722 (void) SyncImage(image,exception);
728 Convert PseudoColor scanline.
730 for (y=(ssize_t) image->rows-1; y >= 0; y--)
732 p=pixels+(image->rows-y-1)*bytes_per_line;
733 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
734 if (q == (Quantum *) NULL)
736 for (x=0; x < ((ssize_t) image->columns-1); x+=2)
738 index=ConstrainColormapIndex(image,(*p >> 4) & 0xf,exception);
739 SetPixelIndex(image,index,q);
740 q+=GetPixelChannels(image);
741 index=ConstrainColormapIndex(image,*p & 0xf,exception);
742 SetPixelIndex(image,index,q);
744 q+=GetPixelChannels(image);
746 if ((image->columns % 2) != 0)
748 index=ConstrainColormapIndex(image,(*p >> 4) & 0xf,exception);
749 SetPixelIndex(image,index,q);
750 q+=GetPixelChannels(image);
753 if (SyncAuthenticPixels(image,exception) == MagickFalse)
755 if (image->previous == (Image *) NULL)
757 status=SetImageProgress(image,LoadImageTag,image->rows-y-1,
759 if (status == MagickFalse)
763 (void) SyncImage(image,exception);
769 Convert PseudoColor scanline.
771 if ((dib_info.compression == BI_RLE8) ||
772 (dib_info.compression == BI_RLE4))
773 bytes_per_line=image->columns;
774 for (y=(ssize_t) image->rows-1; y >= 0; y--)
776 p=pixels+(image->rows-y-1)*bytes_per_line;
777 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
778 if (q == (Quantum *) NULL)
780 for (x=0; x < (ssize_t) image->columns; x++)
782 index=ConstrainColormapIndex(image,*p,exception);
783 SetPixelIndex(image,index,q);
785 q+=GetPixelChannels(image);
787 if (SyncAuthenticPixels(image,exception) == MagickFalse)
789 if (image->previous == (Image *) NULL)
791 status=SetImageProgress(image,LoadImageTag,image->rows-y-1,
793 if (status == MagickFalse)
797 (void) SyncImage(image,exception);
806 Convert PseudoColor scanline.
808 image->storage_class=DirectClass;
809 if (dib_info.compression == BI_RLE8)
810 bytes_per_line=2*image->columns;
811 for (y=(ssize_t) image->rows-1; y >= 0; y--)
813 p=pixels+(image->rows-y-1)*bytes_per_line;
814 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
815 if (q == (Quantum *) NULL)
817 for (x=0; x < (ssize_t) image->columns; x++)
821 if (dib_info.red_mask == 0)
823 SetPixelRed(image,ScaleCharToQuantum(ScaleColor5to8(
824 (unsigned char) ((word >> 10) & 0x1f))),q);
825 SetPixelGreen(image,ScaleCharToQuantum(ScaleColor5to8(
826 (unsigned char) ((word >> 5) & 0x1f))),q);
827 SetPixelBlue(image,ScaleCharToQuantum(ScaleColor5to8(
828 (unsigned char) (word & 0x1f))),q);
832 SetPixelRed(image,ScaleCharToQuantum(ScaleColor5to8(
833 (unsigned char) ((word >> 11) & 0x1f))),q);
834 SetPixelGreen(image,ScaleCharToQuantum(ScaleColor6to8(
835 (unsigned char) ((word >> 5) & 0x3f))),q);
836 SetPixelBlue(image,ScaleCharToQuantum(ScaleColor5to8(
837 (unsigned char) (word & 0x1f))),q);
839 q+=GetPixelChannels(image);
841 if (SyncAuthenticPixels(image,exception) == MagickFalse)
843 if (image->previous == (Image *) NULL)
845 status=SetImageProgress(image,LoadImageTag,image->rows-y-1,
847 if (status == MagickFalse)
857 Convert DirectColor scanline.
859 for (y=(ssize_t) image->rows-1; y >= 0; y--)
861 p=pixels+(image->rows-y-1)*bytes_per_line;
862 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
863 if (q == (Quantum *) NULL)
865 for (x=0; x < (ssize_t) image->columns; x++)
867 SetPixelBlue(image,ScaleCharToQuantum(*p++),q);
868 SetPixelGreen(image,ScaleCharToQuantum(*p++),q);
869 SetPixelRed(image,ScaleCharToQuantum(*p++),q);
870 if (image->alpha_trait != UndefinedPixelTrait)
871 SetPixelAlpha(image,ScaleCharToQuantum(*p++),q);
872 q+=GetPixelChannels(image);
874 if (SyncAuthenticPixels(image,exception) == MagickFalse)
876 if (image->previous == (Image *) NULL)
878 status=SetImageProgress(image,LoadImageTag,image->rows-y-1,
880 if (status == MagickFalse)
887 pixel_info=RelinquishVirtualMemory(pixel_info);
888 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
890 pixel_info=RelinquishVirtualMemory(pixel_info);
891 if (EOFBlob(image) != MagickFalse)
892 ThrowFileException(exception,CorruptImageError,"UnexpectedEndOfFile",
894 if (dib_info.height < 0)
900 Correct image orientation.
902 flipped_image=FlipImage(image,exception);
903 if (flipped_image != (Image *) NULL)
905 DuplicateBlob(flipped_image,image);
906 image=DestroyImage(image);
910 (void) CloseBlob(image);
911 return(GetFirstImageInList(image));
915 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
919 % R e g i s t e r D I B I m a g e %
923 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
925 % RegisterDIBImage() adds attributes for the DIB image format to
926 % the list of supported formats. The attributes include the image format
927 % tag, a method to read and/or write the format, whether the format
928 % supports the saving of more than one frame to the same file or blob,
929 % whether the format supports native in-memory I/O, and a brief
930 % description of the format.
932 % The format of the RegisterDIBImage method is:
934 % size_t RegisterDIBImage(void)
937 ModuleExport size_t RegisterDIBImage(void)
942 entry=AcquireMagickInfo("DIB","DIB",
943 "Microsoft Windows 3.X Packed Device-Independent Bitmap");
944 entry->decoder=(DecodeImageHandler *) ReadDIBImage;
945 entry->encoder=(EncodeImageHandler *) WriteDIBImage;
946 entry->magick=(IsImageFormatHandler *) IsDIB;
947 entry->flags^=CoderAdjoinFlag;
948 entry->flags|=CoderStealthFlag;
949 (void) RegisterMagickInfo(entry);
950 return(MagickImageCoderSignature);
954 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
958 % U n r e g i s t e r D I B I m a g e %
962 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
964 % UnregisterDIBImage() removes format registrations made by the
965 % DIB module from the list of supported formats.
967 % The format of the UnregisterDIBImage method is:
969 % UnregisterDIBImage(void)
972 ModuleExport void UnregisterDIBImage(void)
974 (void) UnregisterMagickInfo("DIB");
978 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
982 % W r i t e D I B I m a g e %
986 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
988 % WriteDIBImage() writes an image in Microsoft Windows bitmap encoded
991 % The format of the WriteDIBImage method is:
993 % MagickBooleanType WriteDIBImage(const ImageInfo *image_info,
994 % Image *image,ExceptionInfo *exception)
996 % A description of each parameter follows.
998 % o image_info: the image info.
1000 % o image: The image.
1002 % o exception: return any errors or warnings in this structure.
1005 static MagickBooleanType WriteDIBImage(const ImageInfo *image_info,Image *image,
1006 ExceptionInfo *exception)
1014 register const Quantum
1021 register unsigned char
1035 Open output image file.
1037 assert(image_info != (const ImageInfo *) NULL);
1038 assert(image_info->signature == MagickCoreSignature);
1039 assert(image != (Image *) NULL);
1040 assert(image->signature == MagickCoreSignature);
1041 if (image->debug != MagickFalse)
1042 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1043 assert(exception != (ExceptionInfo *) NULL);
1044 assert(exception->signature == MagickCoreSignature);
1045 status=OpenBlob(image_info,image,WriteBinaryBlobMode,exception);
1046 if (status == MagickFalse)
1049 Initialize DIB raster file header.
1051 (void) TransformImageColorspace(image,sRGBColorspace,exception);
1052 if (image->storage_class == DirectClass)
1055 Full color DIB raster.
1057 dib_info.number_colors=0;
1058 dib_info.bits_per_pixel=(unsigned short) (image->alpha_trait ? 32 : 24);
1063 Colormapped DIB raster.
1065 dib_info.bits_per_pixel=8;
1066 if (image_info->depth > 8)
1067 dib_info.bits_per_pixel=16;
1068 if (SetImageMonochrome(image,exception) != MagickFalse)
1069 dib_info.bits_per_pixel=1;
1070 dib_info.number_colors=(dib_info.bits_per_pixel == 16) ? 0 :
1071 (1UL << dib_info.bits_per_pixel);
1073 bytes_per_line=4*((image->columns*dib_info.bits_per_pixel+31)/32);
1075 dib_info.width=(ssize_t) image->columns;
1076 dib_info.height=(ssize_t) image->rows;
1078 dib_info.compression=(size_t) (dib_info.bits_per_pixel == 16 ?
1079 BI_BITFIELDS : BI_RGB);
1080 dib_info.image_size=bytes_per_line*image->rows;
1081 dib_info.x_pixels=75*39;
1082 dib_info.y_pixels=75*39;
1083 switch (image->units)
1085 case UndefinedResolution:
1086 case PixelsPerInchResolution:
1088 dib_info.x_pixels=(size_t) (100.0*image->resolution.x/2.54);
1089 dib_info.y_pixels=(size_t) (100.0*image->resolution.y/2.54);
1092 case PixelsPerCentimeterResolution:
1094 dib_info.x_pixels=(size_t) (100.0*image->resolution.x);
1095 dib_info.y_pixels=(size_t) (100.0*image->resolution.y);
1099 dib_info.colors_important=dib_info.number_colors;
1101 Convert MIFF to DIB raster pixels.
1103 pixels=(unsigned char *) AcquireQuantumMemory(dib_info.image_size,
1105 if (pixels == (unsigned char *) NULL)
1106 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
1107 (void) ResetMagickMemory(pixels,0,dib_info.image_size);
1108 switch (dib_info.bits_per_pixel)
1112 register unsigned char
1117 Convert PseudoClass image to a DIB monochrome image.
1119 for (y=0; y < (ssize_t) image->rows; y++)
1121 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
1122 if (p == (const Quantum *) NULL)
1124 q=pixels+(image->rows-y-1)*bytes_per_line;
1127 for (x=0; x < (ssize_t) image->columns; x++)
1130 byte|=GetPixelIndex(image,p) != 0 ? 0x01 : 0x00;
1138 p+=GetPixelChannels(image);
1142 *q++=(unsigned char) (byte << (8-bit));
1145 for (x=(ssize_t) (image->columns+7)/8; x < (ssize_t) bytes_per_line; x++)
1147 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
1149 if (status == MagickFalse)
1157 Convert PseudoClass packet to DIB pixel.
1159 for (y=0; y < (ssize_t) image->rows; y++)
1161 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
1162 if (p == (const Quantum *) NULL)
1164 q=pixels+(image->rows-y-1)*bytes_per_line;
1165 for (x=0; x < (ssize_t) image->columns; x++)
1167 *q++=(unsigned char) GetPixelIndex(image,p);
1168 p+=GetPixelChannels(image);
1170 for ( ; x < (ssize_t) bytes_per_line; x++)
1172 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
1174 if (status == MagickFalse)
1184 Convert PseudoClass packet to DIB pixel.
1186 for (y=0; y < (ssize_t) image->rows; y++)
1188 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
1189 if (p == (const Quantum *) NULL)
1191 q=pixels+(image->rows-y-1)*bytes_per_line;
1192 for (x=0; x < (ssize_t) image->columns; x++)
1194 word=(unsigned short) ((ScaleColor8to5((unsigned char)
1195 ScaleQuantumToChar(GetPixelRed(image,p))) << 11) | (ScaleColor8to6(
1196 (unsigned char) ScaleQuantumToChar(GetPixelGreen(image,p))) << 5) |
1197 (ScaleColor8to5((unsigned char) ScaleQuantumToChar((unsigned char)
1198 GetPixelBlue(image,p)) << 0)));
1199 *q++=(unsigned char)(word & 0xff);
1200 *q++=(unsigned char)(word >> 8);
1201 p+=GetPixelChannels(image);
1203 for (x=(ssize_t) (2*image->columns); x < (ssize_t) bytes_per_line; x++)
1205 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
1207 if (status == MagickFalse)
1216 Convert DirectClass packet to DIB RGB pixel.
1218 for (y=0; y < (ssize_t) image->rows; y++)
1220 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
1221 if (p == (const Quantum *) NULL)
1223 q=pixels+(image->rows-y-1)*bytes_per_line;
1224 for (x=0; x < (ssize_t) image->columns; x++)
1226 *q++=ScaleQuantumToChar(GetPixelBlue(image,p));
1227 *q++=ScaleQuantumToChar(GetPixelGreen(image,p));
1228 *q++=ScaleQuantumToChar(GetPixelRed(image,p));
1229 if (image->alpha_trait != UndefinedPixelTrait)
1230 *q++=ScaleQuantumToChar(GetPixelAlpha(image,p));
1231 p+=GetPixelChannels(image);
1233 if (dib_info.bits_per_pixel == 24)
1234 for (x=(ssize_t) (3*image->columns); x < (ssize_t) bytes_per_line; x++)
1236 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
1238 if (status == MagickFalse)
1244 if (dib_info.bits_per_pixel == 8)
1245 if (image_info->compression != NoCompression)
1251 Convert run-length encoded raster pixels.
1253 length=2UL*(bytes_per_line+2UL)+2UL;
1254 dib_data=(unsigned char *) AcquireQuantumMemory(length,
1255 (image->rows+2UL)*sizeof(*dib_data));
1256 if (dib_data == (unsigned char *) NULL)
1258 pixels=(unsigned char *) RelinquishMagickMemory(pixels);
1259 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
1261 dib_info.image_size=(size_t) EncodeImage(image,bytes_per_line,
1263 pixels=(unsigned char *) RelinquishMagickMemory(pixels);
1265 dib_info.compression = BI_RLE8;
1270 (void) WriteBlobLSBLong(image,(unsigned int) dib_info.size);
1271 (void) WriteBlobLSBLong(image,dib_info.width);
1272 (void) WriteBlobLSBLong(image,(unsigned short) dib_info.height);
1273 (void) WriteBlobLSBShort(image,(unsigned short) dib_info.planes);
1274 (void) WriteBlobLSBShort(image,dib_info.bits_per_pixel);
1275 (void) WriteBlobLSBLong(image,(unsigned int) dib_info.compression);
1276 (void) WriteBlobLSBLong(image,(unsigned int) dib_info.image_size);
1277 (void) WriteBlobLSBLong(image,(unsigned int) dib_info.x_pixels);
1278 (void) WriteBlobLSBLong(image,(unsigned int) dib_info.y_pixels);
1279 (void) WriteBlobLSBLong(image,(unsigned int) dib_info.number_colors);
1280 (void) WriteBlobLSBLong(image,(unsigned int) dib_info.colors_important);
1281 if (image->storage_class == PseudoClass)
1283 if (dib_info.bits_per_pixel <= 8)
1289 Dump colormap to file.
1291 dib_colormap=(unsigned char *) AcquireQuantumMemory((size_t)
1292 (1UL << dib_info.bits_per_pixel),4*sizeof(*dib_colormap));
1293 if (dib_colormap == (unsigned char *) NULL)
1294 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
1296 for (i=0; i < (ssize_t) MagickMin(image->colors,dib_info.number_colors); i++)
1298 *q++=ScaleQuantumToChar(image->colormap[i].blue);
1299 *q++=ScaleQuantumToChar(image->colormap[i].green);
1300 *q++=ScaleQuantumToChar(image->colormap[i].red);
1303 for ( ; i < (ssize_t) (1L << dib_info.bits_per_pixel); i++)
1310 (void) WriteBlob(image,(size_t) (4*(1 << dib_info.bits_per_pixel)),
1312 dib_colormap=(unsigned char *) RelinquishMagickMemory(dib_colormap);
1315 if ((dib_info.bits_per_pixel == 16) &&
1316 (dib_info.compression == BI_BITFIELDS))
1318 (void) WriteBlobLSBLong(image,0xf800);
1319 (void) WriteBlobLSBLong(image,0x07e0);
1320 (void) WriteBlobLSBLong(image,0x001f);
1323 (void) WriteBlob(image,dib_info.image_size,pixels);
1324 pixels=(unsigned char *) RelinquishMagickMemory(pixels);
1325 (void) CloseBlob(image);