2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
13 % Read/Write Windows DIB Image Format %
20 % Copyright 1999-2017 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 % https://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__)
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)
609 Read DIB raster colormap.
611 if (AcquireImageColormap(image,image->colors,exception) == MagickFalse)
612 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
613 length=(size_t) image->colors;
614 dib_colormap=(unsigned char *) AcquireQuantumMemory(length,
615 4*sizeof(*dib_colormap));
616 if (dib_colormap == (unsigned char *) NULL)
617 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
619 count=ReadBlob(image,packet_size*image->colors,dib_colormap);
620 if (count != (ssize_t) (packet_size*image->colors))
622 dib_colormap=(unsigned char *) RelinquishMagickMemory(dib_colormap);
623 ThrowReaderException(CorruptImageError,"InsufficientImageDataInFile");
626 for (i=0; i < (ssize_t) image->colors; i++)
628 image->colormap[i].blue=ScaleCharToQuantum(*p++);
629 image->colormap[i].green=ScaleCharToQuantum(*p++);
630 image->colormap[i].red=ScaleCharToQuantum(*p++);
631 if (packet_size == 4)
634 dib_colormap=(unsigned char *) RelinquishMagickMemory(dib_colormap);
639 if (dib_info.compression == BI_RLE4)
640 dib_info.bits_per_pixel<<=1;
641 bytes_per_line=4*((image->columns*dib_info.bits_per_pixel+31)/32);
642 length=bytes_per_line*image->rows;
643 pixel_info=AcquireVirtualMemory((size_t) image->rows,MagickMax(
644 bytes_per_line,image->columns+256UL)*sizeof(*pixels));
645 if (pixel_info == (MemoryInfo *) NULL)
646 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
647 pixels=(unsigned char *) GetVirtualMemoryBlob(pixel_info);
648 if ((dib_info.compression == BI_RGB) ||
649 (dib_info.compression == BI_BITFIELDS))
651 count=ReadBlob(image,length,pixels);
652 if (count != (ssize_t) (length))
654 pixel_info=RelinquishVirtualMemory(pixel_info);
655 ThrowReaderException(CorruptImageError,"InsufficientImageDataInFile");
661 Convert run-length encoded raster pixels.
663 status=DecodeImage(image,dib_info.compression ? MagickTrue : MagickFalse,
665 if (status == MagickFalse)
667 pixel_info=RelinquishVirtualMemory(pixel_info);
668 ThrowReaderException(CorruptImageError,
669 "UnableToRunlengthDecodeImage");
673 Initialize image structure.
675 image->units=PixelsPerCentimeterResolution;
676 image->resolution.x=(double) dib_info.x_pixels/100.0;
677 image->resolution.y=(double) dib_info.y_pixels/100.0;
679 Convert DIB raster image to pixel packets.
681 switch (dib_info.bits_per_pixel)
686 Convert bitmap scanline.
688 for (y=(ssize_t) image->rows-1; y >= 0; y--)
690 p=pixels+(image->rows-y-1)*bytes_per_line;
691 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
692 if (q == (Quantum *) NULL)
694 for (x=0; x < ((ssize_t) image->columns-7); x+=8)
696 for (bit=0; bit < 8; bit++)
698 index=(Quantum) ((*p) & (0x80 >> bit) ? 0x01 : 0x00);
699 SetPixelIndex(image,index,q);
700 q+=GetPixelChannels(image);
704 if ((image->columns % 8) != 0)
706 for (bit=0; bit < (ssize_t) (image->columns % 8); bit++)
708 index=(Quantum) ((*p) & (0x80 >> bit) ? 0x01 : 0x00);
709 SetPixelIndex(image,index,q);
710 q+=GetPixelChannels(image);
714 if (SyncAuthenticPixels(image,exception) == MagickFalse)
716 if (image->previous == (Image *) NULL)
718 status=SetImageProgress(image,LoadImageTag,image->rows-y-1,
720 if (status == MagickFalse)
724 (void) SyncImage(image,exception);
730 Convert PseudoColor scanline.
732 for (y=(ssize_t) image->rows-1; y >= 0; y--)
734 p=pixels+(image->rows-y-1)*bytes_per_line;
735 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
736 if (q == (Quantum *) NULL)
738 for (x=0; x < ((ssize_t) image->columns-1); x+=2)
740 index=ConstrainColormapIndex(image,(*p >> 4) & 0xf,exception);
741 SetPixelIndex(image,index,q);
742 q+=GetPixelChannels(image);
743 index=ConstrainColormapIndex(image,*p & 0xf,exception);
744 SetPixelIndex(image,index,q);
746 q+=GetPixelChannels(image);
748 if ((image->columns % 2) != 0)
750 index=ConstrainColormapIndex(image,(*p >> 4) & 0xf,exception);
751 SetPixelIndex(image,index,q);
752 q+=GetPixelChannels(image);
755 if (SyncAuthenticPixels(image,exception) == MagickFalse)
757 if (image->previous == (Image *) NULL)
759 status=SetImageProgress(image,LoadImageTag,image->rows-y-1,
761 if (status == MagickFalse)
765 (void) SyncImage(image,exception);
771 Convert PseudoColor scanline.
773 if ((dib_info.compression == BI_RLE8) ||
774 (dib_info.compression == BI_RLE4))
775 bytes_per_line=image->columns;
776 for (y=(ssize_t) image->rows-1; y >= 0; y--)
778 p=pixels+(image->rows-y-1)*bytes_per_line;
779 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
780 if (q == (Quantum *) NULL)
782 for (x=0; x < (ssize_t) image->columns; x++)
784 index=ConstrainColormapIndex(image,*p,exception);
785 SetPixelIndex(image,index,q);
787 q+=GetPixelChannels(image);
789 if (SyncAuthenticPixels(image,exception) == MagickFalse)
791 if (image->previous == (Image *) NULL)
793 status=SetImageProgress(image,LoadImageTag,image->rows-y-1,
795 if (status == MagickFalse)
799 (void) SyncImage(image,exception);
808 Convert PseudoColor scanline.
810 image->storage_class=DirectClass;
811 if (dib_info.compression == BI_RLE8)
812 bytes_per_line=2*image->columns;
813 for (y=(ssize_t) image->rows-1; y >= 0; y--)
815 p=pixels+(image->rows-y-1)*bytes_per_line;
816 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
817 if (q == (Quantum *) NULL)
819 for (x=0; x < (ssize_t) image->columns; x++)
823 if (dib_info.red_mask == 0)
825 SetPixelRed(image,ScaleCharToQuantum(ScaleColor5to8(
826 (unsigned char) ((word >> 10) & 0x1f))),q);
827 SetPixelGreen(image,ScaleCharToQuantum(ScaleColor5to8(
828 (unsigned char) ((word >> 5) & 0x1f))),q);
829 SetPixelBlue(image,ScaleCharToQuantum(ScaleColor5to8(
830 (unsigned char) (word & 0x1f))),q);
834 SetPixelRed(image,ScaleCharToQuantum(ScaleColor5to8(
835 (unsigned char) ((word >> 11) & 0x1f))),q);
836 SetPixelGreen(image,ScaleCharToQuantum(ScaleColor6to8(
837 (unsigned char) ((word >> 5) & 0x3f))),q);
838 SetPixelBlue(image,ScaleCharToQuantum(ScaleColor5to8(
839 (unsigned char) (word & 0x1f))),q);
841 q+=GetPixelChannels(image);
843 if (SyncAuthenticPixels(image,exception) == MagickFalse)
845 if (image->previous == (Image *) NULL)
847 status=SetImageProgress(image,LoadImageTag,image->rows-y-1,
849 if (status == MagickFalse)
859 Convert DirectColor scanline.
861 for (y=(ssize_t) image->rows-1; y >= 0; y--)
863 p=pixels+(image->rows-y-1)*bytes_per_line;
864 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
865 if (q == (Quantum *) NULL)
867 for (x=0; x < (ssize_t) image->columns; x++)
869 SetPixelBlue(image,ScaleCharToQuantum(*p++),q);
870 SetPixelGreen(image,ScaleCharToQuantum(*p++),q);
871 SetPixelRed(image,ScaleCharToQuantum(*p++),q);
872 if (image->alpha_trait != UndefinedPixelTrait)
873 SetPixelAlpha(image,ScaleCharToQuantum(*p++),q);
874 q+=GetPixelChannels(image);
876 if (SyncAuthenticPixels(image,exception) == MagickFalse)
878 if (image->previous == (Image *) NULL)
880 status=SetImageProgress(image,LoadImageTag,image->rows-y-1,
882 if (status == MagickFalse)
889 pixel_info=RelinquishVirtualMemory(pixel_info);
890 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
892 pixel_info=RelinquishVirtualMemory(pixel_info);
893 if (EOFBlob(image) != MagickFalse)
894 ThrowFileException(exception,CorruptImageError,"UnexpectedEndOfFile",
896 if (dib_info.height < 0)
902 Correct image orientation.
904 flipped_image=FlipImage(image,exception);
905 if (flipped_image != (Image *) NULL)
907 DuplicateBlob(flipped_image,image);
908 image=DestroyImage(image);
912 (void) CloseBlob(image);
913 return(GetFirstImageInList(image));
917 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
921 % R e g i s t e r D I B I m a g e %
925 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
927 % RegisterDIBImage() adds attributes for the DIB image format to
928 % the list of supported formats. The attributes include the image format
929 % tag, a method to read and/or write the format, whether the format
930 % supports the saving of more than one frame to the same file or blob,
931 % whether the format supports native in-memory I/O, and a brief
932 % description of the format.
934 % The format of the RegisterDIBImage method is:
936 % size_t RegisterDIBImage(void)
939 ModuleExport size_t RegisterDIBImage(void)
944 entry=AcquireMagickInfo("DIB","DIB",
945 "Microsoft Windows 3.X Packed Device-Independent Bitmap");
946 entry->decoder=(DecodeImageHandler *) ReadDIBImage;
947 entry->encoder=(EncodeImageHandler *) WriteDIBImage;
948 entry->magick=(IsImageFormatHandler *) IsDIB;
949 entry->flags^=CoderAdjoinFlag;
950 entry->flags|=CoderStealthFlag;
951 (void) RegisterMagickInfo(entry);
952 return(MagickImageCoderSignature);
956 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
960 % U n r e g i s t e r D I B I m a g e %
964 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
966 % UnregisterDIBImage() removes format registrations made by the
967 % DIB module from the list of supported formats.
969 % The format of the UnregisterDIBImage method is:
971 % UnregisterDIBImage(void)
974 ModuleExport void UnregisterDIBImage(void)
976 (void) UnregisterMagickInfo("DIB");
980 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
984 % W r i t e D I B I m a g e %
988 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
990 % WriteDIBImage() writes an image in Microsoft Windows bitmap encoded
993 % The format of the WriteDIBImage method is:
995 % MagickBooleanType WriteDIBImage(const ImageInfo *image_info,
996 % Image *image,ExceptionInfo *exception)
998 % A description of each parameter follows.
1000 % o image_info: the image info.
1002 % o image: The image.
1004 % o exception: return any errors or warnings in this structure.
1007 static MagickBooleanType WriteDIBImage(const ImageInfo *image_info,Image *image,
1008 ExceptionInfo *exception)
1016 register const Quantum
1023 register unsigned char
1037 Open output image file.
1039 assert(image_info != (const ImageInfo *) NULL);
1040 assert(image_info->signature == MagickCoreSignature);
1041 assert(image != (Image *) NULL);
1042 assert(image->signature == MagickCoreSignature);
1043 if (image->debug != MagickFalse)
1044 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1045 assert(exception != (ExceptionInfo *) NULL);
1046 assert(exception->signature == MagickCoreSignature);
1047 status=OpenBlob(image_info,image,WriteBinaryBlobMode,exception);
1048 if (status == MagickFalse)
1051 Initialize DIB raster file header.
1053 (void) TransformImageColorspace(image,sRGBColorspace,exception);
1054 if (image->storage_class == DirectClass)
1057 Full color DIB raster.
1059 dib_info.number_colors=0;
1060 dib_info.bits_per_pixel=(unsigned short) (image->alpha_trait ? 32 : 24);
1065 Colormapped DIB raster.
1067 dib_info.bits_per_pixel=8;
1068 if (image_info->depth > 8)
1069 dib_info.bits_per_pixel=16;
1070 if (SetImageMonochrome(image,exception) != MagickFalse)
1071 dib_info.bits_per_pixel=1;
1072 dib_info.number_colors=(dib_info.bits_per_pixel == 16) ? 0 :
1073 (1UL << dib_info.bits_per_pixel);
1075 bytes_per_line=4*((image->columns*dib_info.bits_per_pixel+31)/32);
1077 dib_info.width=(ssize_t) image->columns;
1078 dib_info.height=(ssize_t) image->rows;
1080 dib_info.compression=(size_t) (dib_info.bits_per_pixel == 16 ?
1081 BI_BITFIELDS : BI_RGB);
1082 dib_info.image_size=bytes_per_line*image->rows;
1083 dib_info.x_pixels=75*39;
1084 dib_info.y_pixels=75*39;
1085 switch (image->units)
1087 case UndefinedResolution:
1088 case PixelsPerInchResolution:
1090 dib_info.x_pixels=(size_t) (100.0*image->resolution.x/2.54);
1091 dib_info.y_pixels=(size_t) (100.0*image->resolution.y/2.54);
1094 case PixelsPerCentimeterResolution:
1096 dib_info.x_pixels=(size_t) (100.0*image->resolution.x);
1097 dib_info.y_pixels=(size_t) (100.0*image->resolution.y);
1101 dib_info.colors_important=dib_info.number_colors;
1103 Convert MIFF to DIB raster pixels.
1105 pixels=(unsigned char *) AcquireQuantumMemory(dib_info.image_size,
1107 if (pixels == (unsigned char *) NULL)
1108 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
1109 (void) ResetMagickMemory(pixels,0,dib_info.image_size);
1110 switch (dib_info.bits_per_pixel)
1114 register unsigned char
1119 Convert PseudoClass image to a DIB monochrome image.
1121 for (y=0; y < (ssize_t) image->rows; y++)
1123 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
1124 if (p == (const Quantum *) NULL)
1126 q=pixels+(image->rows-y-1)*bytes_per_line;
1129 for (x=0; x < (ssize_t) image->columns; x++)
1132 byte|=GetPixelIndex(image,p) != 0 ? 0x01 : 0x00;
1140 p+=GetPixelChannels(image);
1144 *q++=(unsigned char) (byte << (8-bit));
1147 for (x=(ssize_t) (image->columns+7)/8; x < (ssize_t) bytes_per_line; x++)
1149 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
1151 if (status == MagickFalse)
1159 Convert PseudoClass packet to DIB pixel.
1161 for (y=0; y < (ssize_t) image->rows; y++)
1163 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
1164 if (p == (const Quantum *) NULL)
1166 q=pixels+(image->rows-y-1)*bytes_per_line;
1167 for (x=0; x < (ssize_t) image->columns; x++)
1169 *q++=(unsigned char) GetPixelIndex(image,p);
1170 p+=GetPixelChannels(image);
1172 for ( ; x < (ssize_t) bytes_per_line; x++)
1174 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
1176 if (status == MagickFalse)
1186 Convert PseudoClass packet to DIB pixel.
1188 for (y=0; y < (ssize_t) image->rows; y++)
1190 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
1191 if (p == (const Quantum *) NULL)
1193 q=pixels+(image->rows-y-1)*bytes_per_line;
1194 for (x=0; x < (ssize_t) image->columns; x++)
1196 word=(unsigned short) ((ScaleColor8to5((unsigned char)
1197 ScaleQuantumToChar(GetPixelRed(image,p))) << 11) | (ScaleColor8to6(
1198 (unsigned char) ScaleQuantumToChar(GetPixelGreen(image,p))) << 5) |
1199 (ScaleColor8to5((unsigned char) ScaleQuantumToChar((unsigned char)
1200 GetPixelBlue(image,p)) << 0)));
1201 *q++=(unsigned char)(word & 0xff);
1202 *q++=(unsigned char)(word >> 8);
1203 p+=GetPixelChannels(image);
1205 for (x=(ssize_t) (2*image->columns); x < (ssize_t) bytes_per_line; x++)
1207 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
1209 if (status == MagickFalse)
1218 Convert DirectClass packet to DIB RGB pixel.
1220 for (y=0; y < (ssize_t) image->rows; y++)
1222 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
1223 if (p == (const Quantum *) NULL)
1225 q=pixels+(image->rows-y-1)*bytes_per_line;
1226 for (x=0; x < (ssize_t) image->columns; x++)
1228 *q++=ScaleQuantumToChar(GetPixelBlue(image,p));
1229 *q++=ScaleQuantumToChar(GetPixelGreen(image,p));
1230 *q++=ScaleQuantumToChar(GetPixelRed(image,p));
1231 if (image->alpha_trait != UndefinedPixelTrait)
1232 *q++=ScaleQuantumToChar(GetPixelAlpha(image,p));
1233 p+=GetPixelChannels(image);
1235 if (dib_info.bits_per_pixel == 24)
1236 for (x=(ssize_t) (3*image->columns); x < (ssize_t) bytes_per_line; x++)
1238 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
1240 if (status == MagickFalse)
1246 if (dib_info.bits_per_pixel == 8)
1247 if (image_info->compression != NoCompression)
1253 Convert run-length encoded raster pixels.
1255 length=2UL*(bytes_per_line+2UL)+2UL;
1256 dib_data=(unsigned char *) AcquireQuantumMemory(length,
1257 (image->rows+2UL)*sizeof(*dib_data));
1258 if (dib_data == (unsigned char *) NULL)
1260 pixels=(unsigned char *) RelinquishMagickMemory(pixels);
1261 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
1263 dib_info.image_size=(size_t) EncodeImage(image,bytes_per_line,
1265 pixels=(unsigned char *) RelinquishMagickMemory(pixels);
1267 dib_info.compression = BI_RLE8;
1272 (void) WriteBlobLSBLong(image,(unsigned int) dib_info.size);
1273 (void) WriteBlobLSBLong(image,dib_info.width);
1274 (void) WriteBlobLSBLong(image,(unsigned short) dib_info.height);
1275 (void) WriteBlobLSBShort(image,(unsigned short) dib_info.planes);
1276 (void) WriteBlobLSBShort(image,dib_info.bits_per_pixel);
1277 (void) WriteBlobLSBLong(image,(unsigned int) dib_info.compression);
1278 (void) WriteBlobLSBLong(image,(unsigned int) dib_info.image_size);
1279 (void) WriteBlobLSBLong(image,(unsigned int) dib_info.x_pixels);
1280 (void) WriteBlobLSBLong(image,(unsigned int) dib_info.y_pixels);
1281 (void) WriteBlobLSBLong(image,(unsigned int) dib_info.number_colors);
1282 (void) WriteBlobLSBLong(image,(unsigned int) dib_info.colors_important);
1283 if (image->storage_class == PseudoClass)
1285 if (dib_info.bits_per_pixel <= 8)
1291 Dump colormap to file.
1293 dib_colormap=(unsigned char *) AcquireQuantumMemory((size_t)
1294 (1UL << dib_info.bits_per_pixel),4*sizeof(*dib_colormap));
1295 if (dib_colormap == (unsigned char *) NULL)
1296 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
1298 for (i=0; i < (ssize_t) MagickMin(image->colors,dib_info.number_colors); i++)
1300 *q++=ScaleQuantumToChar(image->colormap[i].blue);
1301 *q++=ScaleQuantumToChar(image->colormap[i].green);
1302 *q++=ScaleQuantumToChar(image->colormap[i].red);
1305 for ( ; i < (ssize_t) (1L << dib_info.bits_per_pixel); i++)
1312 (void) WriteBlob(image,(size_t) (4*(1 << dib_info.bits_per_pixel)),
1314 dib_colormap=(unsigned char *) RelinquishMagickMemory(dib_colormap);
1317 if ((dib_info.bits_per_pixel == 16) &&
1318 (dib_info.compression == BI_BITFIELDS))
1320 (void) WriteBlobLSBLong(image,0xf800);
1321 (void) WriteBlobLSBLong(image,0x07e0);
1322 (void) WriteBlobLSBLong(image,0x001f);
1325 (void) WriteBlob(image,dib_info.image_size,pixels);
1326 pixels=(unsigned char *) RelinquishMagickMemory(pixels);
1327 (void) CloseBlob(image);