2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
13 % Read/Write Windows DIB Image Format %
20 % Copyright 1999-2019 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://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,"UnsupportedBitsPerPixel");
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,"UnsupportedBitsPerPixel");
568 if ((dib_info.compression == 2) && (dib_info.bits_per_pixel != 4))
569 ThrowReaderException(CorruptImageError,"UnsupportedBitsPerPixel");
570 if ((dib_info.compression == 3) && (dib_info.bits_per_pixel < 16))
571 ThrowReaderException(CorruptImageError,"UnsupportedBitsPerPixel");
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 > 8))
594 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
595 if ((dib_info.image_size != 0U) && (dib_info.image_size > GetBlobSize(image)))
596 ThrowReaderException(CorruptImageError,"UnexpectedEndOfFile");
597 if ((dib_info.number_colors != 0) || (dib_info.bits_per_pixel < 8))
602 image->storage_class=PseudoClass;
603 image->colors=dib_info.number_colors;
605 if (image->colors == 0)
606 image->colors=one << dib_info.bits_per_pixel;
608 if (image_info->size)
616 flags=ParseAbsoluteGeometry(image_info->size,&geometry);
617 if (flags & WidthValue)
618 if ((geometry.width != 0) && (geometry.width < image->columns))
619 image->columns=geometry.width;
620 if (flags & HeightValue)
621 if ((geometry.height != 0) && (geometry.height < image->rows))
622 image->rows=geometry.height;
624 status=SetImageExtent(image,image->columns,image->rows,exception);
625 if (status == MagickFalse)
626 return(DestroyImageList(image));
627 if (image->storage_class == PseudoClass)
636 Read DIB raster colormap.
638 if (AcquireImageColormap(image,image->colors,exception) == MagickFalse)
639 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
640 length=(size_t) image->colors;
641 dib_colormap=(unsigned char *) AcquireQuantumMemory(length,
642 4*sizeof(*dib_colormap));
643 if (dib_colormap == (unsigned char *) NULL)
644 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
646 count=ReadBlob(image,packet_size*image->colors,dib_colormap);
647 if (count != (ssize_t) (packet_size*image->colors))
649 dib_colormap=(unsigned char *) RelinquishMagickMemory(dib_colormap);
650 ThrowReaderException(CorruptImageError,"InsufficientImageDataInFile");
653 for (i=0; i < (ssize_t) image->colors; i++)
655 image->colormap[i].blue=(MagickRealType) ScaleCharToQuantum(*p++);
656 image->colormap[i].green=(MagickRealType) ScaleCharToQuantum(*p++);
657 image->colormap[i].red=(MagickRealType) ScaleCharToQuantum(*p++);
658 if (packet_size == 4)
661 dib_colormap=(unsigned char *) RelinquishMagickMemory(dib_colormap);
666 if (dib_info.compression == BI_RLE4)
667 dib_info.bits_per_pixel<<=1;
668 bytes_per_line=4*((image->columns*dib_info.bits_per_pixel+31)/32);
669 length=bytes_per_line*image->rows;
670 if ((MagickSizeType) length > (256*GetBlobSize(image)))
671 ThrowReaderException(CorruptImageError,"InsufficientImageDataInFile");
672 pixel_info=AcquireVirtualMemory(image->rows,MagickMax(bytes_per_line,
673 image->columns+256UL)*sizeof(*pixels));
674 if (pixel_info == (MemoryInfo *) NULL)
675 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
676 pixels=(unsigned char *) GetVirtualMemoryBlob(pixel_info);
677 if ((dib_info.compression == BI_RGB) ||
678 (dib_info.compression == BI_BITFIELDS))
680 count=ReadBlob(image,length,pixels);
681 if (count != (ssize_t) (length))
683 pixel_info=RelinquishVirtualMemory(pixel_info);
684 ThrowReaderException(CorruptImageError,"InsufficientImageDataInFile");
690 Convert run-length encoded raster pixels.
692 status=DecodeImage(image,dib_info.compression ? MagickTrue : MagickFalse,
693 pixels,image->columns*image->rows);
694 if (status == MagickFalse)
696 pixel_info=RelinquishVirtualMemory(pixel_info);
697 ThrowReaderException(CorruptImageError,
698 "UnableToRunlengthDecodeImage");
702 Initialize image structure.
704 image->units=PixelsPerCentimeterResolution;
705 image->resolution.x=(double) dib_info.x_pixels/100.0;
706 image->resolution.y=(double) dib_info.y_pixels/100.0;
708 Convert DIB raster image to pixel packets.
710 switch (dib_info.bits_per_pixel)
715 Convert bitmap scanline.
717 for (y=(ssize_t) image->rows-1; y >= 0; y--)
719 p=pixels+(image->rows-y-1)*bytes_per_line;
720 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
721 if (q == (Quantum *) NULL)
723 for (x=0; x < ((ssize_t) image->columns-7); x+=8)
725 for (bit=0; bit < 8; bit++)
727 index=(Quantum) ((*p) & (0x80 >> bit) ? 0x01 : 0x00);
728 SetPixelIndex(image,index,q);
729 q+=GetPixelChannels(image);
733 if ((image->columns % 8) != 0)
735 for (bit=0; bit < (ssize_t) (image->columns % 8); bit++)
737 index=(Quantum) ((*p) & (0x80 >> bit) ? 0x01 : 0x00);
738 SetPixelIndex(image,index,q);
739 q+=GetPixelChannels(image);
743 if (SyncAuthenticPixels(image,exception) == MagickFalse)
745 if (image->previous == (Image *) NULL)
747 status=SetImageProgress(image,LoadImageTag,(MagickOffsetType)
748 image->rows-y-1,image->rows);
749 if (status == MagickFalse)
753 (void) SyncImage(image,exception);
759 Convert PseudoColor scanline.
761 for (y=(ssize_t) image->rows-1; y >= 0; y--)
763 p=pixels+(image->rows-y-1)*bytes_per_line;
764 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
765 if (q == (Quantum *) NULL)
767 for (x=0; x < ((ssize_t) image->columns-1); x+=2)
769 index=(Quantum) ConstrainColormapIndex(image,(ssize_t) (*p >> 4) &
771 SetPixelIndex(image,index,q);
772 q+=GetPixelChannels(image);
773 index=(Quantum) ConstrainColormapIndex(image,(ssize_t) *p & 0xf,
775 SetPixelIndex(image,index,q);
777 q+=GetPixelChannels(image);
779 if ((image->columns % 2) != 0)
781 index=(Quantum) ConstrainColormapIndex(image,(ssize_t) (*p >> 4) &
783 SetPixelIndex(image,index,q);
784 q+=GetPixelChannels(image);
787 if (SyncAuthenticPixels(image,exception) == MagickFalse)
789 if (image->previous == (Image *) NULL)
791 status=SetImageProgress(image,LoadImageTag,(MagickOffsetType)
792 image->rows-y-1,image->rows);
793 if (status == MagickFalse)
797 (void) SyncImage(image,exception);
803 Convert PseudoColor scanline.
805 if ((dib_info.compression == BI_RLE8) ||
806 (dib_info.compression == BI_RLE4))
807 bytes_per_line=image->columns;
808 for (y=(ssize_t) image->rows-1; y >= 0; y--)
810 p=pixels+(image->rows-y-1)*bytes_per_line;
811 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
812 if (q == (Quantum *) NULL)
814 for (x=0; x < (ssize_t) image->columns; x++)
816 index=(Quantum) ConstrainColormapIndex(image,(ssize_t) *p,exception);
817 SetPixelIndex(image,index,q);
819 q+=GetPixelChannels(image);
821 if (SyncAuthenticPixels(image,exception) == MagickFalse)
823 if (image->previous == (Image *) NULL)
825 status=SetImageProgress(image,LoadImageTag,(MagickOffsetType)
826 image->rows-y-1,image->rows);
827 if (status == MagickFalse)
831 (void) SyncImage(image,exception);
840 Convert PseudoColor scanline.
842 image->storage_class=DirectClass;
843 if (dib_info.compression == BI_RLE8)
844 bytes_per_line=2*image->columns;
845 for (y=(ssize_t) image->rows-1; y >= 0; y--)
847 p=pixels+(image->rows-y-1)*bytes_per_line;
848 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
849 if (q == (Quantum *) NULL)
851 for (x=0; x < (ssize_t) image->columns; x++)
855 if (dib_info.red_mask == 0)
857 SetPixelRed(image,ScaleCharToQuantum(ScaleColor5to8(
858 (unsigned char) ((word >> 10) & 0x1f))),q);
859 SetPixelGreen(image,ScaleCharToQuantum(ScaleColor5to8(
860 (unsigned char) ((word >> 5) & 0x1f))),q);
861 SetPixelBlue(image,ScaleCharToQuantum(ScaleColor5to8(
862 (unsigned char) (word & 0x1f))),q);
866 SetPixelRed(image,ScaleCharToQuantum(ScaleColor5to8(
867 (unsigned char) ((word >> 11) & 0x1f))),q);
868 SetPixelGreen(image,ScaleCharToQuantum(ScaleColor6to8(
869 (unsigned char) ((word >> 5) & 0x3f))),q);
870 SetPixelBlue(image,ScaleCharToQuantum(ScaleColor5to8(
871 (unsigned char) (word & 0x1f))),q);
873 q+=GetPixelChannels(image);
875 if (SyncAuthenticPixels(image,exception) == MagickFalse)
877 if (image->previous == (Image *) NULL)
879 status=SetImageProgress(image,LoadImageTag,(MagickOffsetType)
880 image->rows-y-1,image->rows);
881 if (status == MagickFalse)
891 Convert DirectColor scanline.
893 for (y=(ssize_t) image->rows-1; y >= 0; y--)
895 p=pixels+(image->rows-y-1)*bytes_per_line;
896 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
897 if (q == (Quantum *) NULL)
899 for (x=0; x < (ssize_t) image->columns; x++)
901 SetPixelBlue(image,ScaleCharToQuantum(*p++),q);
902 SetPixelGreen(image,ScaleCharToQuantum(*p++),q);
903 SetPixelRed(image,ScaleCharToQuantum(*p++),q);
904 if (image->alpha_trait != UndefinedPixelTrait)
905 SetPixelAlpha(image,ScaleCharToQuantum(*p++),q);
906 q+=GetPixelChannels(image);
908 if (SyncAuthenticPixels(image,exception) == MagickFalse)
910 if (image->previous == (Image *) NULL)
912 status=SetImageProgress(image,LoadImageTag,(MagickOffsetType)
913 image->rows-y-1,image->rows);
914 if (status == MagickFalse)
921 pixel_info=RelinquishVirtualMemory(pixel_info);
922 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
924 pixel_info=RelinquishVirtualMemory(pixel_info);
925 if (strcmp(image_info->magick,"ICODIB") == 0)
933 for (y=0; y < (ssize_t) image->rows; y++)
941 q=GetAuthenticPixels(image,0,y,image->columns,1,exception);
942 if (q == (Quantum *) NULL)
944 for (x=0; x < ((ssize_t) image->columns-7); x+=8)
946 c=ReadBlobByte(image);
947 for (bit=0; bit < 8; bit++)
949 if (c & (0x80 >> bit))
950 image->alpha_trait=BlendPixelTrait;
951 SetPixelAlpha(image,c & (0x80 >> bit) ? TransparentAlpha :
952 OpaqueAlpha,q+x*GetPixelChannels(image)+bit);
955 if ((image->columns % 8) != 0)
957 c=ReadBlobByte(image);
958 for (bit=0; bit < (ssize_t) (image->columns % 8); bit++)
960 if (c & (0x80 >> bit))
961 image->alpha_trait=BlendPixelTrait;
962 SetPixelAlpha(image,c & (0x80 >> bit) ? TransparentAlpha :
963 OpaqueAlpha,q+x*GetPixelChannels(image)+bit);
966 if (image->columns % 32)
967 for (x=0; x < (ssize_t) ((32-(image->columns % 32))/8); x++)
968 c=ReadBlobByte(image);
969 if (SyncAuthenticPixels(image,exception) == MagickFalse)
973 if (EOFBlob(image) != MagickFalse)
974 ThrowFileException(exception,CorruptImageError,"UnexpectedEndOfFile",
976 if (dib_info.height < 0)
982 Correct image orientation.
984 flipped_image=FlipImage(image,exception);
985 if (flipped_image != (Image *) NULL)
987 DuplicateBlob(flipped_image,image);
988 image=DestroyImage(image);
992 (void) CloseBlob(image);
993 return(GetFirstImageInList(image));
997 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1001 % R e g i s t e r D I B I m a g e %
1005 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1007 % RegisterDIBImage() adds attributes for the DIB image format to
1008 % the list of supported formats. The attributes include the image format
1009 % tag, a method to read and/or write the format, whether the format
1010 % supports the saving of more than one frame to the same file or blob,
1011 % whether the format supports native in-memory I/O, and a brief
1012 % description of the format.
1014 % The format of the RegisterDIBImage method is:
1016 % size_t RegisterDIBImage(void)
1019 ModuleExport size_t RegisterDIBImage(void)
1024 entry=AcquireMagickInfo("DIB","DIB",
1025 "Microsoft Windows 3.X Packed Device-Independent Bitmap");
1026 entry->decoder=(DecodeImageHandler *) ReadDIBImage;
1027 entry->encoder=(EncodeImageHandler *) WriteDIBImage;
1028 entry->magick=(IsImageFormatHandler *) IsDIB;
1029 entry->flags^=CoderAdjoinFlag;
1030 entry->flags|=CoderStealthFlag;
1031 entry->flags|=CoderDecoderSeekableStreamFlag;
1032 (void) RegisterMagickInfo(entry);
1033 entry=AcquireMagickInfo("DIB","ICODIB",
1034 "Microsoft Windows 3.X Packed Device-Independent Bitmap");
1035 entry->decoder=(DecodeImageHandler *) ReadDIBImage;
1036 entry->magick=(IsImageFormatHandler *) IsDIB;
1037 entry->flags^=CoderAdjoinFlag;
1038 entry->flags|=CoderStealthFlag;
1039 entry->flags|=CoderDecoderSeekableStreamFlag;
1040 (void) RegisterMagickInfo(entry);
1041 return(MagickImageCoderSignature);
1045 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1049 % U n r e g i s t e r D I B I m a g e %
1053 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1055 % UnregisterDIBImage() removes format registrations made by the
1056 % DIB module from the list of supported formats.
1058 % The format of the UnregisterDIBImage method is:
1060 % UnregisterDIBImage(void)
1063 ModuleExport void UnregisterDIBImage(void)
1065 (void) UnregisterMagickInfo("ICODIB");
1066 (void) UnregisterMagickInfo("DIB");
1070 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1074 % W r i t e D I B I m a g e %
1078 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1080 % WriteDIBImage() writes an image in Microsoft Windows bitmap encoded
1083 % The format of the WriteDIBImage method is:
1085 % MagickBooleanType WriteDIBImage(const ImageInfo *image_info,
1086 % Image *image,ExceptionInfo *exception)
1088 % A description of each parameter follows.
1090 % o image_info: the image info.
1092 % o image: The image.
1094 % o exception: return any errors or warnings in this structure.
1097 static MagickBooleanType WriteDIBImage(const ImageInfo *image_info,Image *image,
1098 ExceptionInfo *exception)
1106 register const Quantum
1113 register unsigned char
1127 Open output image file.
1129 assert(image_info != (const ImageInfo *) NULL);
1130 assert(image_info->signature == MagickCoreSignature);
1131 assert(image != (Image *) NULL);
1132 assert(image->signature == MagickCoreSignature);
1133 if (image->debug != MagickFalse)
1134 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1135 assert(exception != (ExceptionInfo *) NULL);
1136 assert(exception->signature == MagickCoreSignature);
1137 status=OpenBlob(image_info,image,WriteBinaryBlobMode,exception);
1138 if (status == MagickFalse)
1140 if (((image->columns << 3) != (int) (image->columns << 3)) ||
1141 ((image->rows << 3) != (int) (image->rows << 3)))
1142 ThrowWriterException(ImageError,"WidthOrHeightExceedsLimit");
1144 Initialize DIB raster file header.
1146 (void) TransformImageColorspace(image,sRGBColorspace,exception);
1147 if (image->storage_class == DirectClass)
1150 Full color DIB raster.
1152 dib_info.number_colors=0;
1153 dib_info.bits_per_pixel=(unsigned short) (image->alpha_trait ? 32 : 24);
1158 Colormapped DIB raster.
1160 dib_info.bits_per_pixel=8;
1161 if (image_info->depth > 8)
1162 dib_info.bits_per_pixel=16;
1163 if (SetImageMonochrome(image,exception) != MagickFalse)
1164 dib_info.bits_per_pixel=1;
1165 dib_info.number_colors=(unsigned int) (dib_info.bits_per_pixel == 16 ? 0 :
1166 (1UL << dib_info.bits_per_pixel));
1168 bytes_per_line=4*((image->columns*dib_info.bits_per_pixel+31)/32);
1170 dib_info.width=(int) image->columns;
1171 dib_info.height=(int) image->rows;
1173 dib_info.compression=(unsigned int) (dib_info.bits_per_pixel == 16 ?
1174 BI_BITFIELDS : BI_RGB);
1175 dib_info.image_size=(unsigned int) (bytes_per_line*image->rows);
1176 dib_info.x_pixels=75*39;
1177 dib_info.y_pixels=75*39;
1178 switch (image->units)
1180 case UndefinedResolution:
1181 case PixelsPerInchResolution:
1183 dib_info.x_pixels=(unsigned int) (100.0*image->resolution.x/2.54);
1184 dib_info.y_pixels=(unsigned int) (100.0*image->resolution.y/2.54);
1187 case PixelsPerCentimeterResolution:
1189 dib_info.x_pixels=(unsigned int) (100.0*image->resolution.x);
1190 dib_info.y_pixels=(unsigned int) (100.0*image->resolution.y);
1194 dib_info.colors_important=dib_info.number_colors;
1196 Convert MIFF to DIB raster pixels.
1198 pixels=(unsigned char *) AcquireQuantumMemory(image->rows,MagickMax(
1199 bytes_per_line,image->columns+256UL)*sizeof(*pixels));
1200 if (pixels == (unsigned char *) NULL)
1201 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
1202 (void) memset(pixels,0,dib_info.image_size);
1203 switch (dib_info.bits_per_pixel)
1207 register unsigned char
1212 Convert PseudoClass image to a DIB monochrome image.
1214 for (y=0; y < (ssize_t) image->rows; y++)
1216 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
1217 if (p == (const Quantum *) NULL)
1219 q=pixels+(image->rows-y-1)*bytes_per_line;
1222 for (x=0; x < (ssize_t) image->columns; x++)
1225 byte|=GetPixelIndex(image,p) != 0 ? 0x01 : 0x00;
1233 p+=GetPixelChannels(image);
1237 *q++=(unsigned char) (byte << (8-bit));
1240 for (x=(ssize_t) (image->columns+7)/8; x < (ssize_t) bytes_per_line; x++)
1242 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
1244 if (status == MagickFalse)
1252 Convert PseudoClass packet to DIB pixel.
1254 for (y=0; y < (ssize_t) image->rows; y++)
1256 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
1257 if (p == (const Quantum *) NULL)
1259 q=pixels+(image->rows-y-1)*bytes_per_line;
1260 for (x=0; x < (ssize_t) image->columns; x++)
1262 *q++=(unsigned char) GetPixelIndex(image,p);
1263 p+=GetPixelChannels(image);
1265 for ( ; x < (ssize_t) bytes_per_line; x++)
1267 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
1269 if (status == MagickFalse)
1279 Convert PseudoClass packet to DIB pixel.
1281 for (y=0; y < (ssize_t) image->rows; y++)
1283 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
1284 if (p == (const Quantum *) NULL)
1286 q=pixels+(image->rows-y-1)*bytes_per_line;
1287 for (x=0; x < (ssize_t) image->columns; x++)
1289 word=(unsigned short) ((ScaleColor8to5((unsigned char)
1290 ScaleQuantumToChar(GetPixelRed(image,p))) << 11) | (ScaleColor8to6(
1291 (unsigned char) ScaleQuantumToChar(GetPixelGreen(image,p))) << 5) |
1292 (ScaleColor8to5((unsigned char) ScaleQuantumToChar((unsigned char)
1293 GetPixelBlue(image,p)) << 0)));
1294 *q++=(unsigned char)(word & 0xff);
1295 *q++=(unsigned char)(word >> 8);
1296 p+=GetPixelChannels(image);
1298 for (x=(ssize_t) (2*image->columns); x < (ssize_t) bytes_per_line; x++)
1300 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
1302 if (status == MagickFalse)
1311 Convert DirectClass packet to DIB RGB pixel.
1313 for (y=0; y < (ssize_t) image->rows; y++)
1315 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
1316 if (p == (const Quantum *) NULL)
1318 q=pixels+(image->rows-y-1)*bytes_per_line;
1319 for (x=0; x < (ssize_t) image->columns; x++)
1321 *q++=ScaleQuantumToChar(GetPixelBlue(image,p));
1322 *q++=ScaleQuantumToChar(GetPixelGreen(image,p));
1323 *q++=ScaleQuantumToChar(GetPixelRed(image,p));
1324 if (image->alpha_trait != UndefinedPixelTrait)
1325 *q++=ScaleQuantumToChar(GetPixelAlpha(image,p));
1326 p+=GetPixelChannels(image);
1328 if (dib_info.bits_per_pixel == 24)
1329 for (x=(ssize_t) (3*image->columns); x < (ssize_t) bytes_per_line; x++)
1331 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
1333 if (status == MagickFalse)
1339 if (dib_info.bits_per_pixel == 8)
1340 if (image_info->compression != NoCompression)
1346 Convert run-length encoded raster pixels.
1348 length=2UL*(bytes_per_line+2UL)+2UL;
1349 dib_data=(unsigned char *) AcquireQuantumMemory(length,
1350 (image->rows+2UL)*sizeof(*dib_data));
1351 if (dib_data == (unsigned char *) NULL)
1353 pixels=(unsigned char *) RelinquishMagickMemory(pixels);
1354 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
1356 dib_info.image_size=(unsigned int) EncodeImage(image,bytes_per_line,
1358 pixels=(unsigned char *) RelinquishMagickMemory(pixels);
1360 dib_info.compression = BI_RLE8;
1365 (void) WriteBlobLSBLong(image,dib_info.size);
1366 (void) WriteBlobLSBLong(image,(unsigned int) dib_info.width);
1367 (void) WriteBlobLSBLong(image,(unsigned int) dib_info.height);
1368 (void) WriteBlobLSBShort(image,(unsigned short) dib_info.planes);
1369 (void) WriteBlobLSBShort(image,dib_info.bits_per_pixel);
1370 (void) WriteBlobLSBLong(image,dib_info.compression);
1371 (void) WriteBlobLSBLong(image,dib_info.image_size);
1372 (void) WriteBlobLSBLong(image,dib_info.x_pixels);
1373 (void) WriteBlobLSBLong(image,dib_info.y_pixels);
1374 (void) WriteBlobLSBLong(image,dib_info.number_colors);
1375 (void) WriteBlobLSBLong(image,dib_info.colors_important);
1376 if (image->storage_class == PseudoClass)
1378 if (dib_info.bits_per_pixel <= 8)
1384 Dump colormap to file.
1386 dib_colormap=(unsigned char *) AcquireQuantumMemory((size_t)
1387 (1UL << dib_info.bits_per_pixel),4*sizeof(*dib_colormap));
1388 if (dib_colormap == (unsigned char *) NULL)
1390 pixels=(unsigned char *) RelinquishMagickMemory(pixels);
1391 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
1394 for (i=0; i < (ssize_t) MagickMin(image->colors,dib_info.number_colors); i++)
1396 *q++=ScaleQuantumToChar(ClampToQuantum(image->colormap[i].blue));
1397 *q++=ScaleQuantumToChar(ClampToQuantum(image->colormap[i].green));
1398 *q++=ScaleQuantumToChar(ClampToQuantum(image->colormap[i].red));
1401 for ( ; i < (ssize_t) (1L << dib_info.bits_per_pixel); i++)
1408 (void) WriteBlob(image,(size_t) (4*(1 << dib_info.bits_per_pixel)),
1410 dib_colormap=(unsigned char *) RelinquishMagickMemory(dib_colormap);
1413 if ((dib_info.bits_per_pixel == 16) &&
1414 (dib_info.compression == BI_BITFIELDS))
1416 (void) WriteBlobLSBLong(image,0xf800);
1417 (void) WriteBlobLSBLong(image,0x07e0);
1418 (void) WriteBlobLSBLong(image,0x001f);
1421 (void) WriteBlob(image,dib_info.image_size,pixels);
1422 pixels=(unsigned char *) RelinquishMagickMemory(pixels);
1423 (void) CloseBlob(image);