2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
13 % Read/Write Windows DIB Image Format %
20 % Copyright 1999-2018 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
113 Forward declarations.
115 static MagickBooleanType
116 WriteDIBImage(const ImageInfo *,Image *,ExceptionInfo *);
119 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
123 % D e c o d e I m a g e %
127 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
129 % DecodeImage unpacks the packed image pixels into runlength-encoded
132 % The format of the DecodeImage method is:
134 % MagickBooleanType DecodeImage(Image *image,
135 % const MagickBooleanType compression,unsigned char *pixels,
136 % const size_t number_pixels)
138 % A description of each parameter follows:
140 % o image: the address of a structure of type Image.
142 % o compression: A value of 1 means the compressed pixels are runlength
143 % encoded for a 256-color bitmap. A value of 2 means a 16-color bitmap.
145 % o pixels: The address of a byte (8 bits) array of pixel data created by
146 % the decoding process.
148 % o number_pixels: the number of pixels.
151 static MagickBooleanType DecodeImage(Image *image,
152 const MagickBooleanType compression,unsigned char *pixels,
153 const size_t number_pixels)
155 #if !defined(MAGICKCORE_WINDOWS_SUPPORT) || defined(__MINGW32__)
159 #define BI_BITFIELDS 3
177 register unsigned char
181 assert(image != (Image *) NULL);
182 assert(image->signature == MagickCoreSignature);
183 if (image->debug != MagickFalse)
184 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
185 assert(pixels != (unsigned char *) NULL);
186 (void) memset(pixels,0,number_pixels*sizeof(*pixels));
190 q=pixels+number_pixels;
191 for (y=0; y < (ssize_t) image->rows; )
196 if ((p < pixels) || (p >= q))
198 count=ReadBlobByte(image);
206 count=(int) MagickMin((size_t) count,(size_t) (q-p));
207 byte=ReadBlobByte(image);
210 if (compression == BI_RLE8)
212 for (i=0; i < (ssize_t) count; i++)
213 *p++=(unsigned char) byte;
217 for (i=0; i < (ssize_t) count; i++)
219 ((i & 0x01) != 0 ? (byte & 0x0f) : ((byte >> 4) & 0x0f));
228 count=ReadBlobByte(image);
242 p=pixels+y*image->columns;
250 x+=ReadBlobByte(image);
251 y+=ReadBlobByte(image);
252 p=pixels+y*image->columns+x;
260 count=(int) MagickMin((size_t) count,(size_t) (q-p));
261 if (compression == BI_RLE8)
262 for (i=0; i < (ssize_t) count; i++)
264 byte=ReadBlobByte(image);
267 *p++=(unsigned char) byte;
270 for (i=0; i < (ssize_t) count; i++)
274 byte=ReadBlobByte(image);
279 ((i & 0x01) != 0 ? (byte & 0x0f) : ((byte >> 4) & 0x0f));
285 if (compression == BI_RLE8)
287 if ((count & 0x01) != 0)
288 if (ReadBlobByte(image) == EOF)
292 if (((count & 0x03) == 1) || ((count & 0x03) == 2))
293 if (ReadBlobByte(image) == EOF)
299 status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y,
301 if (status == MagickFalse)
304 (void) ReadBlobByte(image); /* end of line */
305 (void) ReadBlobByte(image);
306 return(y < (ssize_t) image->rows ? MagickFalse : MagickTrue);
310 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
314 % E n c o d e I m a g e %
318 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
320 % EncodeImage compresses pixels using a runlength encoded format.
322 % The format of the EncodeImage method is:
324 % static MagickBooleanType EncodeImage(Image *image,
325 % const size_t bytes_per_line,const unsigned char *pixels,
326 % unsigned char *compressed_pixels)
328 % A description of each parameter follows:
330 % o image: The image.
332 % o bytes_per_line: the number of bytes in a scanline of compressed pixels
334 % o pixels: The address of a byte (8 bits) array of pixel data created by
335 % the compression process.
337 % o compressed_pixels: The address of a byte (8 bits) array of compressed
341 static size_t EncodeImage(Image *image,const size_t bytes_per_line,
342 const unsigned char *pixels,unsigned char *compressed_pixels)
347 register const unsigned char
354 register unsigned char
358 Runlength encode pixels.
360 assert(image != (Image *) NULL);
361 assert(image->signature == MagickCoreSignature);
362 if (image->debug != MagickFalse)
363 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
364 assert(pixels != (const unsigned char *) NULL);
365 assert(compressed_pixels != (unsigned char *) NULL);
369 for (y=0; y < (ssize_t) image->rows; y++)
371 for (x=0; x < (ssize_t) bytes_per_line; x+=i)
376 for (i=1; ((x+i) < (ssize_t) bytes_per_line); i++)
377 if ((*(p+i) != *p) || (i == 255))
379 *q++=(unsigned char) i;
388 if (SetImageProgress(image,LoadImageTag,(MagickOffsetType) y,image->rows) == MagickFalse)
396 return((size_t) (q-compressed_pixels));
400 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
408 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
410 % IsDIB() returns MagickTrue if the image format type, identified by the
411 % magick string, is DIB.
413 % The format of the IsDIB method is:
415 % MagickBooleanType IsDIB(const unsigned char *magick,const size_t length)
417 % A description of each parameter follows:
419 % o magick: compare image format pattern against these bytes.
421 % o length: Specifies the length of the magick string.
424 static MagickBooleanType IsDIB(const unsigned char *magick,const size_t length)
428 if (memcmp(magick,"\050\000",2) == 0)
434 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
438 % R e a d D I B I m a g e %
442 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
444 % ReadDIBImage() reads a Microsoft Windows bitmap image file and
445 % returns it. It allocates the memory necessary for the new Image structure
446 % and returns a pointer to the new image.
448 % The format of the ReadDIBImage method is:
450 % image=ReadDIBImage(image_info)
452 % A description of each parameter follows:
454 % o image_info: the image info.
456 % o exception: return any errors or warnings in this structure.
459 static Image *ReadDIBImage(const ImageInfo *image_info,ExceptionInfo *exception)
485 register unsigned char
504 assert(image_info != (const ImageInfo *) NULL);
505 assert(image_info->signature == MagickCoreSignature);
506 if (image_info->debug != MagickFalse)
507 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
508 image_info->filename);
509 assert(exception != (ExceptionInfo *) NULL);
510 assert(exception->signature == MagickCoreSignature);
511 image=AcquireImage(image_info,exception);
512 status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception);
513 if (status == MagickFalse)
515 image=DestroyImageList(image);
516 return((Image *) NULL);
519 Determine if this a DIB file.
521 (void) memset(&dib_info,0,sizeof(dib_info));
522 dib_info.size=ReadBlobLSBLong(image);
523 if (dib_info.size != 40)
524 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
526 Microsoft Windows 3.X DIB image file.
528 dib_info.width=ReadBlobLSBSignedLong(image);
529 dib_info.height=ReadBlobLSBSignedLong(image);
530 dib_info.planes=ReadBlobLSBShort(image);
531 dib_info.bits_per_pixel=ReadBlobLSBShort(image);
532 if (dib_info.bits_per_pixel > 32)
533 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
534 dib_info.compression=ReadBlobLSBLong(image);
535 dib_info.image_size=ReadBlobLSBLong(image);
536 dib_info.x_pixels=ReadBlobLSBLong(image);
537 dib_info.y_pixels=ReadBlobLSBLong(image);
538 dib_info.number_colors=ReadBlobLSBLong(image);
539 dib_info.colors_important=ReadBlobLSBLong(image);
540 if ((dib_info.bits_per_pixel != 1) && (dib_info.bits_per_pixel != 4) &&
541 (dib_info.bits_per_pixel != 8) && (dib_info.bits_per_pixel != 16) &&
542 (dib_info.bits_per_pixel != 24) && (dib_info.bits_per_pixel != 32))
543 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
544 if ((dib_info.compression == BI_BITFIELDS) &&
545 ((dib_info.bits_per_pixel == 16) || (dib_info.bits_per_pixel == 32)))
547 dib_info.red_mask=(unsigned short) ReadBlobLSBLong(image);
548 dib_info.green_mask=(unsigned short) ReadBlobLSBLong(image);
549 dib_info.blue_mask=(unsigned short) ReadBlobLSBLong(image);
551 if (EOFBlob(image) != MagickFalse)
552 ThrowReaderException(CorruptImageError,"UnexpectedEndOfFile");
553 if (dib_info.width <= 0)
554 ThrowReaderException(CorruptImageError,"NegativeOrZeroImageSize");
555 if (dib_info.height == 0)
556 ThrowReaderException(CorruptImageError,"NegativeOrZeroImageSize");
557 if (dib_info.planes != 1)
558 ThrowReaderException(CorruptImageError,"StaticPlanesValueNotEqualToOne");
559 if ((dib_info.bits_per_pixel != 1) && (dib_info.bits_per_pixel != 4) &&
560 (dib_info.bits_per_pixel != 8) && (dib_info.bits_per_pixel != 16) &&
561 (dib_info.bits_per_pixel != 24) && (dib_info.bits_per_pixel != 32))
562 ThrowReaderException(CorruptImageError,"UnrecognizedBitsPerPixel");
563 if ((dib_info.bits_per_pixel < 16) &&
564 (dib_info.number_colors > (unsigned int) (1UL << dib_info.bits_per_pixel)))
565 ThrowReaderException(CorruptImageError,"UnrecognizedNumberOfColors");
566 if ((dib_info.compression == 1) && (dib_info.bits_per_pixel != 8))
567 ThrowReaderException(CorruptImageError,"UnrecognizedBitsPerPixel");
568 if ((dib_info.compression == 2) && (dib_info.bits_per_pixel != 4))
569 ThrowReaderException(CorruptImageError,"UnrecognizedBitsPerPixel");
570 if ((dib_info.compression == 3) && (dib_info.bits_per_pixel < 16))
571 ThrowReaderException(CorruptImageError,"UnrecognizedBitsPerPixel");
572 switch (dib_info.compression)
580 ThrowReaderException(CoderError,"JPEGCompressNotSupported");
582 ThrowReaderException(CoderError,"PNGCompressNotSupported");
584 ThrowReaderException(CorruptImageError,"UnrecognizedImageCompression");
586 image->columns=(size_t) MagickAbsoluteValue((ssize_t) dib_info.width);
587 image->rows=(size_t) MagickAbsoluteValue((ssize_t) dib_info.height);
589 image->alpha_trait=dib_info.bits_per_pixel == 32 ? BlendPixelTrait :
591 if ((dib_info.number_colors > 256) || (dib_info.colors_important > 256))
592 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
593 if ((dib_info.number_colors != 0) || (dib_info.bits_per_pixel < 16))
598 image->storage_class=PseudoClass;
599 image->colors=dib_info.number_colors;
601 if (image->colors == 0)
602 image->colors=one << dib_info.bits_per_pixel;
604 if (image_info->size)
612 flags=ParseAbsoluteGeometry(image_info->size,&geometry);
613 if (flags & WidthValue)
614 if ((geometry.width != 0) && (geometry.width < image->columns))
615 image->columns=geometry.width;
616 if (flags & HeightValue)
617 if ((geometry.height != 0) && (geometry.height < image->rows))
618 image->rows=geometry.height;
620 status=SetImageExtent(image,image->columns,image->rows,exception);
621 if (status == MagickFalse)
622 return(DestroyImageList(image));
623 if (image->storage_class == PseudoClass)
632 Read DIB raster colormap.
634 if (AcquireImageColormap(image,image->colors,exception) == MagickFalse)
635 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
636 length=(size_t) image->colors;
637 dib_colormap=(unsigned char *) AcquireQuantumMemory(length,
638 4*sizeof(*dib_colormap));
639 if (dib_colormap == (unsigned char *) NULL)
640 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
642 count=ReadBlob(image,packet_size*image->colors,dib_colormap);
643 if (count != (ssize_t) (packet_size*image->colors))
645 dib_colormap=(unsigned char *) RelinquishMagickMemory(dib_colormap);
646 ThrowReaderException(CorruptImageError,"InsufficientImageDataInFile");
649 for (i=0; i < (ssize_t) image->colors; i++)
651 image->colormap[i].blue=(MagickRealType) ScaleCharToQuantum(*p++);
652 image->colormap[i].green=(MagickRealType) ScaleCharToQuantum(*p++);
653 image->colormap[i].red=(MagickRealType) ScaleCharToQuantum(*p++);
654 if (packet_size == 4)
657 dib_colormap=(unsigned char *) RelinquishMagickMemory(dib_colormap);
662 if (dib_info.compression == BI_RLE4)
663 dib_info.bits_per_pixel<<=1;
664 bytes_per_line=4*((image->columns*dib_info.bits_per_pixel+31)/32);
665 length=bytes_per_line*image->rows;
666 pixel_info=AcquireVirtualMemory(image->rows,MagickMax(bytes_per_line,
667 image->columns+256UL)*sizeof(*pixels));
668 if (pixel_info == (MemoryInfo *) NULL)
669 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
670 pixels=(unsigned char *) GetVirtualMemoryBlob(pixel_info);
671 if ((dib_info.compression == BI_RGB) ||
672 (dib_info.compression == BI_BITFIELDS))
674 count=ReadBlob(image,length,pixels);
675 if (count != (ssize_t) (length))
677 pixel_info=RelinquishVirtualMemory(pixel_info);
678 ThrowReaderException(CorruptImageError,"InsufficientImageDataInFile");
684 Convert run-length encoded raster pixels.
686 status=DecodeImage(image,dib_info.compression ? MagickTrue : MagickFalse,
687 pixels,image->columns*image->rows);
688 if (status == MagickFalse)
690 pixel_info=RelinquishVirtualMemory(pixel_info);
691 ThrowReaderException(CorruptImageError,
692 "UnableToRunlengthDecodeImage");
696 Initialize image structure.
698 image->units=PixelsPerCentimeterResolution;
699 image->resolution.x=(double) dib_info.x_pixels/100.0;
700 image->resolution.y=(double) dib_info.y_pixels/100.0;
702 Convert DIB raster image to pixel packets.
704 switch (dib_info.bits_per_pixel)
709 Convert bitmap scanline.
711 for (y=(ssize_t) image->rows-1; y >= 0; y--)
713 p=pixels+(image->rows-y-1)*bytes_per_line;
714 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
715 if (q == (Quantum *) NULL)
717 for (x=0; x < ((ssize_t) image->columns-7); x+=8)
719 for (bit=0; bit < 8; bit++)
721 index=(Quantum) ((*p) & (0x80 >> bit) ? 0x01 : 0x00);
722 SetPixelIndex(image,index,q);
723 q+=GetPixelChannels(image);
727 if ((image->columns % 8) != 0)
729 for (bit=0; bit < (ssize_t) (image->columns % 8); bit++)
731 index=(Quantum) ((*p) & (0x80 >> bit) ? 0x01 : 0x00);
732 SetPixelIndex(image,index,q);
733 q+=GetPixelChannels(image);
737 if (SyncAuthenticPixels(image,exception) == MagickFalse)
739 if (image->previous == (Image *) NULL)
741 status=SetImageProgress(image,LoadImageTag,(MagickOffsetType)
742 image->rows-y-1,image->rows);
743 if (status == MagickFalse)
747 (void) SyncImage(image,exception);
753 Convert PseudoColor scanline.
755 for (y=(ssize_t) image->rows-1; y >= 0; y--)
757 p=pixels+(image->rows-y-1)*bytes_per_line;
758 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
759 if (q == (Quantum *) NULL)
761 for (x=0; x < ((ssize_t) image->columns-1); x+=2)
763 index=(Quantum) ConstrainColormapIndex(image,(ssize_t) (*p >> 4) &
765 SetPixelIndex(image,index,q);
766 q+=GetPixelChannels(image);
767 index=(Quantum) ConstrainColormapIndex(image,(ssize_t) *p & 0xf,
769 SetPixelIndex(image,index,q);
771 q+=GetPixelChannels(image);
773 if ((image->columns % 2) != 0)
775 index=(Quantum) ConstrainColormapIndex(image,(ssize_t) (*p >> 4) &
777 SetPixelIndex(image,index,q);
778 q+=GetPixelChannels(image);
781 if (SyncAuthenticPixels(image,exception) == MagickFalse)
783 if (image->previous == (Image *) NULL)
785 status=SetImageProgress(image,LoadImageTag,(MagickOffsetType)
786 image->rows-y-1,image->rows);
787 if (status == MagickFalse)
791 (void) SyncImage(image,exception);
797 Convert PseudoColor scanline.
799 if ((dib_info.compression == BI_RLE8) ||
800 (dib_info.compression == BI_RLE4))
801 bytes_per_line=image->columns;
802 for (y=(ssize_t) image->rows-1; y >= 0; y--)
804 p=pixels+(image->rows-y-1)*bytes_per_line;
805 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
806 if (q == (Quantum *) NULL)
808 for (x=0; x < (ssize_t) image->columns; x++)
810 index=(Quantum) ConstrainColormapIndex(image,(ssize_t) *p,exception);
811 SetPixelIndex(image,index,q);
813 q+=GetPixelChannels(image);
815 if (SyncAuthenticPixels(image,exception) == MagickFalse)
817 if (image->previous == (Image *) NULL)
819 status=SetImageProgress(image,LoadImageTag,(MagickOffsetType)
820 image->rows-y-1,image->rows);
821 if (status == MagickFalse)
825 (void) SyncImage(image,exception);
834 Convert PseudoColor scanline.
836 image->storage_class=DirectClass;
837 if (dib_info.compression == BI_RLE8)
838 bytes_per_line=2*image->columns;
839 for (y=(ssize_t) image->rows-1; y >= 0; y--)
841 p=pixels+(image->rows-y-1)*bytes_per_line;
842 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
843 if (q == (Quantum *) NULL)
845 for (x=0; x < (ssize_t) image->columns; x++)
849 if (dib_info.red_mask == 0)
851 SetPixelRed(image,ScaleCharToQuantum(ScaleColor5to8(
852 (unsigned char) ((word >> 10) & 0x1f))),q);
853 SetPixelGreen(image,ScaleCharToQuantum(ScaleColor5to8(
854 (unsigned char) ((word >> 5) & 0x1f))),q);
855 SetPixelBlue(image,ScaleCharToQuantum(ScaleColor5to8(
856 (unsigned char) (word & 0x1f))),q);
860 SetPixelRed(image,ScaleCharToQuantum(ScaleColor5to8(
861 (unsigned char) ((word >> 11) & 0x1f))),q);
862 SetPixelGreen(image,ScaleCharToQuantum(ScaleColor6to8(
863 (unsigned char) ((word >> 5) & 0x3f))),q);
864 SetPixelBlue(image,ScaleCharToQuantum(ScaleColor5to8(
865 (unsigned char) (word & 0x1f))),q);
867 q+=GetPixelChannels(image);
869 if (SyncAuthenticPixels(image,exception) == MagickFalse)
871 if (image->previous == (Image *) NULL)
873 status=SetImageProgress(image,LoadImageTag,(MagickOffsetType)
874 image->rows-y-1,image->rows);
875 if (status == MagickFalse)
885 Convert DirectColor scanline.
887 for (y=(ssize_t) image->rows-1; y >= 0; y--)
889 p=pixels+(image->rows-y-1)*bytes_per_line;
890 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
891 if (q == (Quantum *) NULL)
893 for (x=0; x < (ssize_t) image->columns; x++)
895 SetPixelBlue(image,ScaleCharToQuantum(*p++),q);
896 SetPixelGreen(image,ScaleCharToQuantum(*p++),q);
897 SetPixelRed(image,ScaleCharToQuantum(*p++),q);
898 if (image->alpha_trait != UndefinedPixelTrait)
899 SetPixelAlpha(image,ScaleCharToQuantum(*p++),q);
900 q+=GetPixelChannels(image);
902 if (SyncAuthenticPixels(image,exception) == MagickFalse)
904 if (image->previous == (Image *) NULL)
906 status=SetImageProgress(image,LoadImageTag,(MagickOffsetType)
907 image->rows-y-1,image->rows);
908 if (status == MagickFalse)
915 pixel_info=RelinquishVirtualMemory(pixel_info);
916 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
918 pixel_info=RelinquishVirtualMemory(pixel_info);
919 if (strcmp(image_info->magick,"ICODIB") == 0)
927 image->storage_class=DirectClass;
928 image->alpha_trait=BlendPixelTrait;
929 for (y=0; y < (ssize_t) image->rows; y++)
937 q=GetAuthenticPixels(image,0,y,image->columns,1,exception);
938 if (q == (Quantum *) NULL)
940 for (x=0; x < ((ssize_t) image->columns-7); x+=8)
942 c=ReadBlobByte(image);
943 for (bit=0; bit < 8; bit++)
944 SetPixelAlpha(image,c & (0x80 >> bit) ? TransparentAlpha :
945 OpaqueAlpha,q+x*GetPixelChannels(image)+bit);
947 if ((image->columns % 8) != 0)
949 c=ReadBlobByte(image);
950 for (bit=0; bit < (ssize_t) (image->columns % 8); bit++)
951 SetPixelAlpha(image,c & (0x80 >> bit) ? TransparentAlpha :
952 OpaqueAlpha,q+x*GetPixelChannels(image)+bit);
954 if (image->columns % 32)
955 for (x=0; x < (ssize_t) ((32-(image->columns % 32))/8); x++)
956 c=ReadBlobByte(image);
957 if (SyncAuthenticPixels(image,exception) == MagickFalse)
961 if (EOFBlob(image) != MagickFalse)
962 ThrowFileException(exception,CorruptImageError,"UnexpectedEndOfFile",
964 if (dib_info.height < 0)
970 Correct image orientation.
972 flipped_image=FlipImage(image,exception);
973 if (flipped_image != (Image *) NULL)
975 DuplicateBlob(flipped_image,image);
976 image=DestroyImage(image);
980 (void) CloseBlob(image);
981 return(GetFirstImageInList(image));
985 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
989 % R e g i s t e r D I B I m a g e %
993 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
995 % RegisterDIBImage() adds attributes for the DIB image format to
996 % the list of supported formats. The attributes include the image format
997 % tag, a method to read and/or write the format, whether the format
998 % supports the saving of more than one frame to the same file or blob,
999 % whether the format supports native in-memory I/O, and a brief
1000 % description of the format.
1002 % The format of the RegisterDIBImage method is:
1004 % size_t RegisterDIBImage(void)
1007 ModuleExport size_t RegisterDIBImage(void)
1012 entry=AcquireMagickInfo("DIB","DIB",
1013 "Microsoft Windows 3.X Packed Device-Independent Bitmap");
1014 entry->decoder=(DecodeImageHandler *) ReadDIBImage;
1015 entry->encoder=(EncodeImageHandler *) WriteDIBImage;
1016 entry->magick=(IsImageFormatHandler *) IsDIB;
1017 entry->flags^=CoderAdjoinFlag;
1018 entry->flags|=CoderStealthFlag;
1019 (void) RegisterMagickInfo(entry);
1020 entry=AcquireMagickInfo("DIB","ICODIB",
1021 "Microsoft Windows 3.X Packed Device-Independent Bitmap");
1022 entry->decoder=(DecodeImageHandler *) ReadDIBImage;
1023 entry->magick=(IsImageFormatHandler *) IsDIB;
1024 entry->flags^=CoderAdjoinFlag;
1025 entry->flags|=CoderStealthFlag;
1026 (void) RegisterMagickInfo(entry);
1027 return(MagickImageCoderSignature);
1031 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1035 % U n r e g i s t e r D I B I m a g e %
1039 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1041 % UnregisterDIBImage() removes format registrations made by the
1042 % DIB module from the list of supported formats.
1044 % The format of the UnregisterDIBImage method is:
1046 % UnregisterDIBImage(void)
1049 ModuleExport void UnregisterDIBImage(void)
1051 (void) UnregisterMagickInfo("ICODIB");
1052 (void) UnregisterMagickInfo("DIB");
1056 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1060 % W r i t e D I B I m a g e %
1064 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1066 % WriteDIBImage() writes an image in Microsoft Windows bitmap encoded
1069 % The format of the WriteDIBImage method is:
1071 % MagickBooleanType WriteDIBImage(const ImageInfo *image_info,
1072 % Image *image,ExceptionInfo *exception)
1074 % A description of each parameter follows.
1076 % o image_info: the image info.
1078 % o image: The image.
1080 % o exception: return any errors or warnings in this structure.
1083 static MagickBooleanType WriteDIBImage(const ImageInfo *image_info,Image *image,
1084 ExceptionInfo *exception)
1092 register const Quantum
1099 register unsigned char
1113 Open output image file.
1115 assert(image_info != (const ImageInfo *) NULL);
1116 assert(image_info->signature == MagickCoreSignature);
1117 assert(image != (Image *) NULL);
1118 assert(image->signature == MagickCoreSignature);
1119 if (image->debug != MagickFalse)
1120 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1121 assert(exception != (ExceptionInfo *) NULL);
1122 assert(exception->signature == MagickCoreSignature);
1123 status=OpenBlob(image_info,image,WriteBinaryBlobMode,exception);
1124 if (status == MagickFalse)
1127 Initialize DIB raster file header.
1129 (void) TransformImageColorspace(image,sRGBColorspace,exception);
1130 if (image->storage_class == DirectClass)
1133 Full color DIB raster.
1135 dib_info.number_colors=0;
1136 dib_info.bits_per_pixel=(unsigned short) (image->alpha_trait ? 32 : 24);
1141 Colormapped DIB raster.
1143 dib_info.bits_per_pixel=8;
1144 if (image_info->depth > 8)
1145 dib_info.bits_per_pixel=16;
1146 if (SetImageMonochrome(image,exception) != MagickFalse)
1147 dib_info.bits_per_pixel=1;
1148 dib_info.number_colors=(unsigned int) (dib_info.bits_per_pixel == 16 ? 0 :
1149 (1UL << dib_info.bits_per_pixel));
1151 bytes_per_line=4*((image->columns*dib_info.bits_per_pixel+31)/32);
1153 dib_info.width=(int) image->columns;
1154 dib_info.height=(int) image->rows;
1156 dib_info.compression=(unsigned int) (dib_info.bits_per_pixel == 16 ?
1157 BI_BITFIELDS : BI_RGB);
1158 dib_info.image_size=(unsigned int) (bytes_per_line*image->rows);
1159 dib_info.x_pixels=75*39;
1160 dib_info.y_pixels=75*39;
1161 switch (image->units)
1163 case UndefinedResolution:
1164 case PixelsPerInchResolution:
1166 dib_info.x_pixels=(unsigned int) (100.0*image->resolution.x/2.54);
1167 dib_info.y_pixels=(unsigned int) (100.0*image->resolution.y/2.54);
1170 case PixelsPerCentimeterResolution:
1172 dib_info.x_pixels=(unsigned int) (100.0*image->resolution.x);
1173 dib_info.y_pixels=(unsigned int) (100.0*image->resolution.y);
1177 dib_info.colors_important=dib_info.number_colors;
1179 Convert MIFF to DIB raster pixels.
1181 pixels=(unsigned char *) AcquireQuantumMemory(image->rows,MagickMax(
1182 bytes_per_line,image->columns+256UL)*sizeof(*pixels));
1183 if (pixels == (unsigned char *) NULL)
1184 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
1185 (void) memset(pixels,0,dib_info.image_size);
1186 switch (dib_info.bits_per_pixel)
1190 register unsigned char
1195 Convert PseudoClass image to a DIB monochrome image.
1197 for (y=0; y < (ssize_t) image->rows; y++)
1199 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
1200 if (p == (const Quantum *) NULL)
1202 q=pixels+(image->rows-y-1)*bytes_per_line;
1205 for (x=0; x < (ssize_t) image->columns; x++)
1208 byte|=GetPixelIndex(image,p) != 0 ? 0x01 : 0x00;
1216 p+=GetPixelChannels(image);
1220 *q++=(unsigned char) (byte << (8-bit));
1223 for (x=(ssize_t) (image->columns+7)/8; x < (ssize_t) bytes_per_line; x++)
1225 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
1227 if (status == MagickFalse)
1235 Convert PseudoClass packet to DIB pixel.
1237 for (y=0; y < (ssize_t) image->rows; y++)
1239 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
1240 if (p == (const Quantum *) NULL)
1242 q=pixels+(image->rows-y-1)*bytes_per_line;
1243 for (x=0; x < (ssize_t) image->columns; x++)
1245 *q++=(unsigned char) GetPixelIndex(image,p);
1246 p+=GetPixelChannels(image);
1248 for ( ; x < (ssize_t) bytes_per_line; x++)
1250 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
1252 if (status == MagickFalse)
1262 Convert PseudoClass packet to DIB pixel.
1264 for (y=0; y < (ssize_t) image->rows; y++)
1266 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
1267 if (p == (const Quantum *) NULL)
1269 q=pixels+(image->rows-y-1)*bytes_per_line;
1270 for (x=0; x < (ssize_t) image->columns; x++)
1272 word=(unsigned short) ((ScaleColor8to5((unsigned char)
1273 ScaleQuantumToChar(GetPixelRed(image,p))) << 11) | (ScaleColor8to6(
1274 (unsigned char) ScaleQuantumToChar(GetPixelGreen(image,p))) << 5) |
1275 (ScaleColor8to5((unsigned char) ScaleQuantumToChar((unsigned char)
1276 GetPixelBlue(image,p)) << 0)));
1277 *q++=(unsigned char)(word & 0xff);
1278 *q++=(unsigned char)(word >> 8);
1279 p+=GetPixelChannels(image);
1281 for (x=(ssize_t) (2*image->columns); x < (ssize_t) bytes_per_line; x++)
1283 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
1285 if (status == MagickFalse)
1294 Convert DirectClass packet to DIB RGB pixel.
1296 for (y=0; y < (ssize_t) image->rows; y++)
1298 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
1299 if (p == (const Quantum *) NULL)
1301 q=pixels+(image->rows-y-1)*bytes_per_line;
1302 for (x=0; x < (ssize_t) image->columns; x++)
1304 *q++=ScaleQuantumToChar(GetPixelBlue(image,p));
1305 *q++=ScaleQuantumToChar(GetPixelGreen(image,p));
1306 *q++=ScaleQuantumToChar(GetPixelRed(image,p));
1307 if (image->alpha_trait != UndefinedPixelTrait)
1308 *q++=ScaleQuantumToChar(GetPixelAlpha(image,p));
1309 p+=GetPixelChannels(image);
1311 if (dib_info.bits_per_pixel == 24)
1312 for (x=(ssize_t) (3*image->columns); x < (ssize_t) bytes_per_line; x++)
1314 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
1316 if (status == MagickFalse)
1322 if (dib_info.bits_per_pixel == 8)
1323 if (image_info->compression != NoCompression)
1329 Convert run-length encoded raster pixels.
1331 length=2UL*(bytes_per_line+2UL)+2UL;
1332 dib_data=(unsigned char *) AcquireQuantumMemory(length,
1333 (image->rows+2UL)*sizeof(*dib_data));
1334 if (dib_data == (unsigned char *) NULL)
1336 pixels=(unsigned char *) RelinquishMagickMemory(pixels);
1337 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
1339 dib_info.image_size=(unsigned int) EncodeImage(image,bytes_per_line,
1341 pixels=(unsigned char *) RelinquishMagickMemory(pixels);
1343 dib_info.compression = BI_RLE8;
1348 (void) WriteBlobLSBLong(image,dib_info.size);
1349 (void) WriteBlobLSBLong(image,(unsigned int) dib_info.width);
1350 (void) WriteBlobLSBLong(image,(unsigned int) dib_info.height);
1351 (void) WriteBlobLSBShort(image,(unsigned short) dib_info.planes);
1352 (void) WriteBlobLSBShort(image,dib_info.bits_per_pixel);
1353 (void) WriteBlobLSBLong(image,dib_info.compression);
1354 (void) WriteBlobLSBLong(image,dib_info.image_size);
1355 (void) WriteBlobLSBLong(image,dib_info.x_pixels);
1356 (void) WriteBlobLSBLong(image,dib_info.y_pixels);
1357 (void) WriteBlobLSBLong(image,dib_info.number_colors);
1358 (void) WriteBlobLSBLong(image,dib_info.colors_important);
1359 if (image->storage_class == PseudoClass)
1361 if (dib_info.bits_per_pixel <= 8)
1367 Dump colormap to file.
1369 dib_colormap=(unsigned char *) AcquireQuantumMemory((size_t)
1370 (1UL << dib_info.bits_per_pixel),4*sizeof(*dib_colormap));
1371 if (dib_colormap == (unsigned char *) NULL)
1372 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
1374 for (i=0; i < (ssize_t) MagickMin(image->colors,dib_info.number_colors); i++)
1376 *q++=ScaleQuantumToChar(ClampToQuantum(image->colormap[i].blue));
1377 *q++=ScaleQuantumToChar(ClampToQuantum(image->colormap[i].green));
1378 *q++=ScaleQuantumToChar(ClampToQuantum(image->colormap[i].red));
1381 for ( ; i < (ssize_t) (1L << dib_info.bits_per_pixel); i++)
1388 (void) WriteBlob(image,(size_t) (4*(1 << dib_info.bits_per_pixel)),
1390 dib_colormap=(unsigned char *) RelinquishMagickMemory(dib_colormap);
1393 if ((dib_info.bits_per_pixel == 16) &&
1394 (dib_info.compression == BI_BITFIELDS))
1396 (void) WriteBlobLSBLong(image,0xf800);
1397 (void) WriteBlobLSBLong(image,0x07e0);
1398 (void) WriteBlobLSBLong(image,0x001f);
1401 (void) WriteBlob(image,dib_info.image_size,pixels);
1402 pixels=(unsigned char *) RelinquishMagickMemory(pixels);
1403 (void) CloseBlob(image);