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
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,
134 % const size_t number_pixels)
136 % A description of each parameter follows:
138 % o image: the address of a structure of type Image.
140 % o compression: A value of 1 means the compressed pixels are runlength
141 % encoded for a 256-color bitmap. A value of 2 means a 16-color bitmap.
143 % o pixels: The address of a byte (8 bits) array of pixel data created by
144 % the decoding process.
146 % o number_pixels: the number of pixels.
149 static MagickBooleanType DecodeImage(Image *image,
150 const MagickBooleanType compression,unsigned char *pixels,
151 const size_t number_pixels)
153 #if !defined(MAGICKCORE_WINDOWS_SUPPORT) || defined(__MINGW32__)
157 #define BI_BITFIELDS 3
175 register unsigned char
179 assert(image != (Image *) NULL);
180 assert(image->signature == MagickCoreSignature);
181 if (image->debug != MagickFalse)
182 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
183 assert(pixels != (unsigned char *) NULL);
184 (void) ResetMagickMemory(pixels,0,number_pixels*sizeof(*pixels));
188 q=pixels+number_pixels;
189 for (y=0; y < (ssize_t) image->rows; )
191 if ((p < pixels) || (p >= q))
193 count=ReadBlobByte(image);
198 count=(int) MagickMin((size_t) count,(size_t) (q-p));
202 byte=ReadBlobByte(image);
205 if (compression == BI_RLE8)
207 for (i=0; i < count; i++)
208 *p++=(unsigned char) byte;
212 for (i=0; i < count; i++)
214 ((i & 0x01) != 0 ? (byte & 0x0f) : ((byte >> 4) & 0x0f));
223 count=ReadBlobByte(image);
235 p=pixels+y*image->columns;
243 x+=ReadBlobByte(image);
244 y+=ReadBlobByte(image);
245 p=pixels+y*image->columns+x;
253 count=(int) MagickMin((size_t) count,(size_t) (q-p));
254 if (compression == BI_RLE8)
255 for (i=0; i < count; i++)
257 byte=ReadBlobByte(image);
260 *p++=(unsigned char) byte;
263 for (i=0; i < count; i++)
267 byte=ReadBlobByte(image);
272 ((i & 0x01) != 0 ? (byte & 0x0f) : ((byte >> 4) & 0x0f));
278 if (compression == BI_RLE8)
280 if ((count & 0x01) != 0)
281 if (ReadBlobByte(image) == EOF)
285 if (((count & 0x03) == 1) || ((count & 0x03) == 2))
286 if (ReadBlobByte(image) == EOF)
292 if (SetImageProgress(image,LoadImageTag,y,image->rows) == MagickFalse)
295 (void) ReadBlobByte(image); /* end of line */
296 (void) ReadBlobByte(image);
301 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
305 % E n c o d e I m a g e %
309 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
311 % EncodeImage compresses pixels using a runlength encoded format.
313 % The format of the EncodeImage method is:
315 % static MagickBooleanType EncodeImage(Image *image,
316 % const size_t bytes_per_line,const unsigned char *pixels,
317 % unsigned char *compressed_pixels)
319 % A description of each parameter follows:
321 % o image: The image.
323 % o bytes_per_line: the number of bytes in a scanline of compressed pixels
325 % o pixels: The address of a byte (8 bits) array of pixel data created by
326 % the compression process.
328 % o compressed_pixels: The address of a byte (8 bits) array of compressed
332 static size_t EncodeImage(Image *image,const size_t bytes_per_line,
333 const unsigned char *pixels,unsigned char *compressed_pixels)
338 register const unsigned char
345 register unsigned char
349 Runlength encode pixels.
351 assert(image != (Image *) NULL);
352 assert(image->signature == MagickCoreSignature);
353 if (image->debug != MagickFalse)
354 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
355 assert(pixels != (const unsigned char *) NULL);
356 assert(compressed_pixels != (unsigned char *) NULL);
360 for (y=0; y < (ssize_t) image->rows; y++)
362 for (x=0; x < (ssize_t) bytes_per_line; x+=i)
367 for (i=1; ((x+i) < (ssize_t) bytes_per_line); i++)
368 if ((*(p+i) != *p) || (i == 255))
370 *q++=(unsigned char) i;
379 if (SetImageProgress(image,LoadImageTag,y,image->rows) == MagickFalse)
387 return((size_t) (q-compressed_pixels));
391 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
399 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
401 % IsDIB() returns MagickTrue if the image format type, identified by the
402 % magick string, is DIB.
404 % The format of the IsDIB method is:
406 % MagickBooleanType IsDIB(const unsigned char *magick,const size_t length)
408 % A description of each parameter follows:
410 % o magick: compare image format pattern against these bytes.
412 % o length: Specifies the length of the magick string.
415 static MagickBooleanType IsDIB(const unsigned char *magick,const size_t length)
419 if (memcmp(magick,"\050\000",2) == 0)
425 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
429 % R e a d D I B I m a g e %
433 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
435 % ReadDIBImage() reads a Microsoft Windows bitmap image file and
436 % returns it. It allocates the memory necessary for the new Image structure
437 % and returns a pointer to the new image.
439 % The format of the ReadDIBImage method is:
441 % image=ReadDIBImage(image_info)
443 % A description of each parameter follows:
445 % o image_info: the image info.
447 % o exception: return any errors or warnings in this structure.
450 static Image *ReadDIBImage(const ImageInfo *image_info,ExceptionInfo *exception)
476 register unsigned char
495 assert(image_info != (const ImageInfo *) NULL);
496 assert(image_info->signature == MagickCoreSignature);
497 if (image_info->debug != MagickFalse)
498 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
499 image_info->filename);
500 assert(exception != (ExceptionInfo *) NULL);
501 assert(exception->signature == MagickCoreSignature);
502 image=AcquireImage(image_info,exception);
503 status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception);
504 if (status == MagickFalse)
506 image=DestroyImageList(image);
507 return((Image *) NULL);
510 Determine if this a DIB file.
512 (void) ResetMagickMemory(&dib_info,0,sizeof(dib_info));
513 dib_info.size=ReadBlobLSBLong(image);
514 if (dib_info.size != 40)
515 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
517 Microsoft Windows 3.X DIB image file.
519 dib_info.width=ReadBlobLSBSignedLong(image);
520 dib_info.height=ReadBlobLSBSignedLong(image);
521 dib_info.planes=ReadBlobLSBShort(image);
522 dib_info.bits_per_pixel=ReadBlobLSBShort(image);
523 if (dib_info.bits_per_pixel > 32)
524 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
525 dib_info.compression=ReadBlobLSBLong(image);
526 dib_info.image_size=ReadBlobLSBLong(image);
527 dib_info.x_pixels=ReadBlobLSBLong(image);
528 dib_info.y_pixels=ReadBlobLSBLong(image);
529 dib_info.number_colors=ReadBlobLSBLong(image);
530 dib_info.colors_important=ReadBlobLSBLong(image);
531 if ((dib_info.bits_per_pixel != 1) && (dib_info.bits_per_pixel != 4) &&
532 (dib_info.bits_per_pixel != 8) && (dib_info.bits_per_pixel != 16) &&
533 (dib_info.bits_per_pixel != 24) && (dib_info.bits_per_pixel != 32))
534 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
535 if ((dib_info.compression == BI_BITFIELDS) &&
536 ((dib_info.bits_per_pixel == 16) || (dib_info.bits_per_pixel == 32)))
538 dib_info.red_mask=ReadBlobLSBLong(image);
539 dib_info.green_mask=ReadBlobLSBLong(image);
540 dib_info.blue_mask=ReadBlobLSBLong(image);
542 if (EOFBlob(image) != MagickFalse)
543 ThrowReaderException(CorruptImageError,"UnexpectedEndOfFile");
544 if (dib_info.width <= 0)
545 ThrowReaderException(CorruptImageError,"NegativeOrZeroImageSize");
546 if (dib_info.height == 0)
547 ThrowReaderException(CorruptImageError,"NegativeOrZeroImageSize");
548 if (dib_info.planes != 1)
549 ThrowReaderException(CorruptImageError,"StaticPlanesValueNotEqualToOne");
550 if ((dib_info.bits_per_pixel != 1) && (dib_info.bits_per_pixel != 4) &&
551 (dib_info.bits_per_pixel != 8) && (dib_info.bits_per_pixel != 16) &&
552 (dib_info.bits_per_pixel != 24) && (dib_info.bits_per_pixel != 32))
553 ThrowReaderException(CorruptImageError,"UnrecognizedBitsPerPixel");
554 if (dib_info.bits_per_pixel < 16 &&
555 dib_info.number_colors > (size_t) (1UL << dib_info.bits_per_pixel))
556 ThrowReaderException(CorruptImageError,"UnrecognizedNumberOfColors");
557 if ((dib_info.compression == 1) && (dib_info.bits_per_pixel != 8))
558 ThrowReaderException(CorruptImageError,"UnrecognizedBitsPerPixel");
559 if ((dib_info.compression == 2) && (dib_info.bits_per_pixel != 4))
560 ThrowReaderException(CorruptImageError,"UnrecognizedBitsPerPixel");
561 if ((dib_info.compression == 3) && (dib_info.bits_per_pixel < 16))
562 ThrowReaderException(CorruptImageError,"UnrecognizedBitsPerPixel");
563 switch (dib_info.compression)
571 ThrowReaderException(CoderError,"JPEGCompressNotSupported");
573 ThrowReaderException(CoderError,"PNGCompressNotSupported");
575 ThrowReaderException(CorruptImageError,"UnrecognizedImageCompression");
577 image->columns=(size_t) MagickAbsoluteValue(dib_info.width);
578 image->rows=(size_t) MagickAbsoluteValue(dib_info.height);
580 image->alpha_trait=dib_info.bits_per_pixel == 32 ? BlendPixelTrait :
582 if ((dib_info.number_colors > 256) || (dib_info.colors_important > 256))
583 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
584 if ((dib_info.number_colors != 0) || (dib_info.bits_per_pixel < 16))
589 image->storage_class=PseudoClass;
590 image->colors=dib_info.number_colors;
592 if (image->colors == 0)
593 image->colors=one << dib_info.bits_per_pixel;
595 if (image_info->size)
603 flags=ParseAbsoluteGeometry(image_info->size,&geometry);
604 if (flags & WidthValue)
605 if ((geometry.width != 0) && (geometry.width < image->columns))
606 image->columns=geometry.width;
607 if (flags & HeightValue)
608 if ((geometry.height != 0) && (geometry.height < image->rows))
609 image->rows=geometry.height;
611 status=SetImageExtent(image,image->columns,image->rows,exception);
612 if (status == MagickFalse)
613 return(DestroyImageList(image));
614 if (image->storage_class == PseudoClass)
623 Read DIB raster colormap.
625 if (AcquireImageColormap(image,image->colors,exception) == MagickFalse)
626 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
627 length=(size_t) image->colors;
628 dib_colormap=(unsigned char *) AcquireQuantumMemory(length,
629 4*sizeof(*dib_colormap));
630 if (dib_colormap == (unsigned char *) NULL)
631 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
633 count=ReadBlob(image,packet_size*image->colors,dib_colormap);
634 if (count != (ssize_t) (packet_size*image->colors))
636 dib_colormap=(unsigned char *) RelinquishMagickMemory(dib_colormap);
637 ThrowReaderException(CorruptImageError,"InsufficientImageDataInFile");
640 for (i=0; i < (ssize_t) image->colors; i++)
642 image->colormap[i].blue=ScaleCharToQuantum(*p++);
643 image->colormap[i].green=ScaleCharToQuantum(*p++);
644 image->colormap[i].red=ScaleCharToQuantum(*p++);
645 if (packet_size == 4)
648 dib_colormap=(unsigned char *) RelinquishMagickMemory(dib_colormap);
653 if (dib_info.compression == BI_RLE4)
654 dib_info.bits_per_pixel<<=1;
655 bytes_per_line=4*((image->columns*dib_info.bits_per_pixel+31)/32);
656 length=bytes_per_line*image->rows;
657 pixel_info=AcquireVirtualMemory((size_t) image->rows,MagickMax(
658 bytes_per_line,image->columns+256UL)*sizeof(*pixels));
659 if (pixel_info == (MemoryInfo *) NULL)
660 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
661 pixels=(unsigned char *) GetVirtualMemoryBlob(pixel_info);
662 if ((dib_info.compression == BI_RGB) ||
663 (dib_info.compression == BI_BITFIELDS))
665 count=ReadBlob(image,length,pixels);
666 if (count != (ssize_t) (length))
668 pixel_info=RelinquishVirtualMemory(pixel_info);
669 ThrowReaderException(CorruptImageError,"InsufficientImageDataInFile");
675 Convert run-length encoded raster pixels.
677 status=DecodeImage(image,dib_info.compression ? MagickTrue : MagickFalse,
678 pixels,image->columns*image->rows);
679 if (status == MagickFalse)
681 pixel_info=RelinquishVirtualMemory(pixel_info);
682 ThrowReaderException(CorruptImageError,
683 "UnableToRunlengthDecodeImage");
687 Initialize image structure.
689 image->units=PixelsPerCentimeterResolution;
690 image->resolution.x=(double) dib_info.x_pixels/100.0;
691 image->resolution.y=(double) dib_info.y_pixels/100.0;
693 Convert DIB raster image to pixel packets.
695 switch (dib_info.bits_per_pixel)
700 Convert bitmap scanline.
702 for (y=(ssize_t) image->rows-1; y >= 0; y--)
704 p=pixels+(image->rows-y-1)*bytes_per_line;
705 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
706 if (q == (Quantum *) NULL)
708 for (x=0; x < ((ssize_t) image->columns-7); x+=8)
710 for (bit=0; bit < 8; bit++)
712 index=(Quantum) ((*p) & (0x80 >> bit) ? 0x01 : 0x00);
713 SetPixelIndex(image,index,q);
714 q+=GetPixelChannels(image);
718 if ((image->columns % 8) != 0)
720 for (bit=0; bit < (ssize_t) (image->columns % 8); bit++)
722 index=(Quantum) ((*p) & (0x80 >> bit) ? 0x01 : 0x00);
723 SetPixelIndex(image,index,q);
724 q+=GetPixelChannels(image);
728 if (SyncAuthenticPixels(image,exception) == MagickFalse)
730 if (image->previous == (Image *) NULL)
732 status=SetImageProgress(image,LoadImageTag,image->rows-y-1,
734 if (status == MagickFalse)
738 (void) SyncImage(image,exception);
744 Convert PseudoColor scanline.
746 for (y=(ssize_t) image->rows-1; y >= 0; y--)
748 p=pixels+(image->rows-y-1)*bytes_per_line;
749 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
750 if (q == (Quantum *) NULL)
752 for (x=0; x < ((ssize_t) image->columns-1); x+=2)
754 index=ConstrainColormapIndex(image,(*p >> 4) & 0xf,exception);
755 SetPixelIndex(image,index,q);
756 q+=GetPixelChannels(image);
757 index=ConstrainColormapIndex(image,*p & 0xf,exception);
758 SetPixelIndex(image,index,q);
760 q+=GetPixelChannels(image);
762 if ((image->columns % 2) != 0)
764 index=ConstrainColormapIndex(image,(*p >> 4) & 0xf,exception);
765 SetPixelIndex(image,index,q);
766 q+=GetPixelChannels(image);
769 if (SyncAuthenticPixels(image,exception) == MagickFalse)
771 if (image->previous == (Image *) NULL)
773 status=SetImageProgress(image,LoadImageTag,image->rows-y-1,
775 if (status == MagickFalse)
779 (void) SyncImage(image,exception);
785 Convert PseudoColor scanline.
787 if ((dib_info.compression == BI_RLE8) ||
788 (dib_info.compression == BI_RLE4))
789 bytes_per_line=image->columns;
790 for (y=(ssize_t) image->rows-1; y >= 0; y--)
792 p=pixels+(image->rows-y-1)*bytes_per_line;
793 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
794 if (q == (Quantum *) NULL)
796 for (x=0; x < (ssize_t) image->columns; x++)
798 index=ConstrainColormapIndex(image,*p,exception);
799 SetPixelIndex(image,index,q);
801 q+=GetPixelChannels(image);
803 if (SyncAuthenticPixels(image,exception) == MagickFalse)
805 if (image->previous == (Image *) NULL)
807 status=SetImageProgress(image,LoadImageTag,image->rows-y-1,
809 if (status == MagickFalse)
813 (void) SyncImage(image,exception);
822 Convert PseudoColor scanline.
824 image->storage_class=DirectClass;
825 if (dib_info.compression == BI_RLE8)
826 bytes_per_line=2*image->columns;
827 for (y=(ssize_t) image->rows-1; y >= 0; y--)
829 p=pixels+(image->rows-y-1)*bytes_per_line;
830 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
831 if (q == (Quantum *) NULL)
833 for (x=0; x < (ssize_t) image->columns; x++)
837 if (dib_info.red_mask == 0)
839 SetPixelRed(image,ScaleCharToQuantum(ScaleColor5to8(
840 (unsigned char) ((word >> 10) & 0x1f))),q);
841 SetPixelGreen(image,ScaleCharToQuantum(ScaleColor5to8(
842 (unsigned char) ((word >> 5) & 0x1f))),q);
843 SetPixelBlue(image,ScaleCharToQuantum(ScaleColor5to8(
844 (unsigned char) (word & 0x1f))),q);
848 SetPixelRed(image,ScaleCharToQuantum(ScaleColor5to8(
849 (unsigned char) ((word >> 11) & 0x1f))),q);
850 SetPixelGreen(image,ScaleCharToQuantum(ScaleColor6to8(
851 (unsigned char) ((word >> 5) & 0x3f))),q);
852 SetPixelBlue(image,ScaleCharToQuantum(ScaleColor5to8(
853 (unsigned char) (word & 0x1f))),q);
855 q+=GetPixelChannels(image);
857 if (SyncAuthenticPixels(image,exception) == MagickFalse)
859 if (image->previous == (Image *) NULL)
861 status=SetImageProgress(image,LoadImageTag,image->rows-y-1,
863 if (status == MagickFalse)
873 Convert DirectColor scanline.
875 for (y=(ssize_t) image->rows-1; y >= 0; y--)
877 p=pixels+(image->rows-y-1)*bytes_per_line;
878 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
879 if (q == (Quantum *) NULL)
881 for (x=0; x < (ssize_t) image->columns; x++)
883 SetPixelBlue(image,ScaleCharToQuantum(*p++),q);
884 SetPixelGreen(image,ScaleCharToQuantum(*p++),q);
885 SetPixelRed(image,ScaleCharToQuantum(*p++),q);
886 if (image->alpha_trait != UndefinedPixelTrait)
887 SetPixelAlpha(image,ScaleCharToQuantum(*p++),q);
888 q+=GetPixelChannels(image);
890 if (SyncAuthenticPixels(image,exception) == MagickFalse)
892 if (image->previous == (Image *) NULL)
894 status=SetImageProgress(image,LoadImageTag,image->rows-y-1,
896 if (status == MagickFalse)
903 pixel_info=RelinquishVirtualMemory(pixel_info);
904 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
906 pixel_info=RelinquishVirtualMemory(pixel_info);
907 if (EOFBlob(image) != MagickFalse)
908 ThrowFileException(exception,CorruptImageError,"UnexpectedEndOfFile",
910 if (dib_info.height < 0)
916 Correct image orientation.
918 flipped_image=FlipImage(image,exception);
919 if (flipped_image != (Image *) NULL)
921 DuplicateBlob(flipped_image,image);
922 image=DestroyImage(image);
926 (void) CloseBlob(image);
927 return(GetFirstImageInList(image));
931 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
935 % R e g i s t e r D I B I m a g e %
939 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
941 % RegisterDIBImage() adds attributes for the DIB image format to
942 % the list of supported formats. The attributes include the image format
943 % tag, a method to read and/or write the format, whether the format
944 % supports the saving of more than one frame to the same file or blob,
945 % whether the format supports native in-memory I/O, and a brief
946 % description of the format.
948 % The format of the RegisterDIBImage method is:
950 % size_t RegisterDIBImage(void)
953 ModuleExport size_t RegisterDIBImage(void)
958 entry=AcquireMagickInfo("DIB","DIB",
959 "Microsoft Windows 3.X Packed Device-Independent Bitmap");
960 entry->decoder=(DecodeImageHandler *) ReadDIBImage;
961 entry->encoder=(EncodeImageHandler *) WriteDIBImage;
962 entry->magick=(IsImageFormatHandler *) IsDIB;
963 entry->flags^=CoderAdjoinFlag;
964 entry->flags|=CoderStealthFlag;
965 (void) RegisterMagickInfo(entry);
966 return(MagickImageCoderSignature);
970 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
974 % U n r e g i s t e r D I B I m a g e %
978 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
980 % UnregisterDIBImage() removes format registrations made by the
981 % DIB module from the list of supported formats.
983 % The format of the UnregisterDIBImage method is:
985 % UnregisterDIBImage(void)
988 ModuleExport void UnregisterDIBImage(void)
990 (void) UnregisterMagickInfo("DIB");
994 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
998 % W r i t e D I B I m a g e %
1002 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1004 % WriteDIBImage() writes an image in Microsoft Windows bitmap encoded
1007 % The format of the WriteDIBImage method is:
1009 % MagickBooleanType WriteDIBImage(const ImageInfo *image_info,
1010 % Image *image,ExceptionInfo *exception)
1012 % A description of each parameter follows.
1014 % o image_info: the image info.
1016 % o image: The image.
1018 % o exception: return any errors or warnings in this structure.
1021 static MagickBooleanType WriteDIBImage(const ImageInfo *image_info,Image *image,
1022 ExceptionInfo *exception)
1030 register const Quantum
1037 register unsigned char
1051 Open output image file.
1053 assert(image_info != (const ImageInfo *) NULL);
1054 assert(image_info->signature == MagickCoreSignature);
1055 assert(image != (Image *) NULL);
1056 assert(image->signature == MagickCoreSignature);
1057 if (image->debug != MagickFalse)
1058 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1059 assert(exception != (ExceptionInfo *) NULL);
1060 assert(exception->signature == MagickCoreSignature);
1061 status=OpenBlob(image_info,image,WriteBinaryBlobMode,exception);
1062 if (status == MagickFalse)
1065 Initialize DIB raster file header.
1067 (void) TransformImageColorspace(image,sRGBColorspace,exception);
1068 if (image->storage_class == DirectClass)
1071 Full color DIB raster.
1073 dib_info.number_colors=0;
1074 dib_info.bits_per_pixel=(unsigned short) (image->alpha_trait ? 32 : 24);
1079 Colormapped DIB raster.
1081 dib_info.bits_per_pixel=8;
1082 if (image_info->depth > 8)
1083 dib_info.bits_per_pixel=16;
1084 if (SetImageMonochrome(image,exception) != MagickFalse)
1085 dib_info.bits_per_pixel=1;
1086 dib_info.number_colors=(dib_info.bits_per_pixel == 16) ? 0 :
1087 (1UL << dib_info.bits_per_pixel);
1089 bytes_per_line=4*((image->columns*dib_info.bits_per_pixel+31)/32);
1091 dib_info.width=(ssize_t) image->columns;
1092 dib_info.height=(ssize_t) image->rows;
1094 dib_info.compression=(size_t) (dib_info.bits_per_pixel == 16 ?
1095 BI_BITFIELDS : BI_RGB);
1096 dib_info.image_size=bytes_per_line*image->rows;
1097 dib_info.x_pixels=75*39;
1098 dib_info.y_pixels=75*39;
1099 switch (image->units)
1101 case UndefinedResolution:
1102 case PixelsPerInchResolution:
1104 dib_info.x_pixels=(size_t) (100.0*image->resolution.x/2.54);
1105 dib_info.y_pixels=(size_t) (100.0*image->resolution.y/2.54);
1108 case PixelsPerCentimeterResolution:
1110 dib_info.x_pixels=(size_t) (100.0*image->resolution.x);
1111 dib_info.y_pixels=(size_t) (100.0*image->resolution.y);
1115 dib_info.colors_important=dib_info.number_colors;
1117 Convert MIFF to DIB raster pixels.
1119 pixels=(unsigned char *) AcquireQuantumMemory(dib_info.image_size,
1121 if (pixels == (unsigned char *) NULL)
1122 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
1123 (void) ResetMagickMemory(pixels,0,dib_info.image_size);
1124 switch (dib_info.bits_per_pixel)
1128 register unsigned char
1133 Convert PseudoClass image to a DIB monochrome image.
1135 for (y=0; y < (ssize_t) image->rows; y++)
1137 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
1138 if (p == (const Quantum *) NULL)
1140 q=pixels+(image->rows-y-1)*bytes_per_line;
1143 for (x=0; x < (ssize_t) image->columns; x++)
1146 byte|=GetPixelIndex(image,p) != 0 ? 0x01 : 0x00;
1154 p+=GetPixelChannels(image);
1158 *q++=(unsigned char) (byte << (8-bit));
1161 for (x=(ssize_t) (image->columns+7)/8; x < (ssize_t) bytes_per_line; x++)
1163 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
1165 if (status == MagickFalse)
1173 Convert PseudoClass packet to DIB pixel.
1175 for (y=0; y < (ssize_t) image->rows; y++)
1177 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
1178 if (p == (const Quantum *) NULL)
1180 q=pixels+(image->rows-y-1)*bytes_per_line;
1181 for (x=0; x < (ssize_t) image->columns; x++)
1183 *q++=(unsigned char) GetPixelIndex(image,p);
1184 p+=GetPixelChannels(image);
1186 for ( ; x < (ssize_t) bytes_per_line; x++)
1188 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
1190 if (status == MagickFalse)
1200 Convert PseudoClass packet to DIB pixel.
1202 for (y=0; y < (ssize_t) image->rows; y++)
1204 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
1205 if (p == (const Quantum *) NULL)
1207 q=pixels+(image->rows-y-1)*bytes_per_line;
1208 for (x=0; x < (ssize_t) image->columns; x++)
1210 word=(unsigned short) ((ScaleColor8to5((unsigned char)
1211 ScaleQuantumToChar(GetPixelRed(image,p))) << 11) | (ScaleColor8to6(
1212 (unsigned char) ScaleQuantumToChar(GetPixelGreen(image,p))) << 5) |
1213 (ScaleColor8to5((unsigned char) ScaleQuantumToChar((unsigned char)
1214 GetPixelBlue(image,p)) << 0)));
1215 *q++=(unsigned char)(word & 0xff);
1216 *q++=(unsigned char)(word >> 8);
1217 p+=GetPixelChannels(image);
1219 for (x=(ssize_t) (2*image->columns); x < (ssize_t) bytes_per_line; x++)
1221 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
1223 if (status == MagickFalse)
1232 Convert DirectClass packet to DIB RGB pixel.
1234 for (y=0; y < (ssize_t) image->rows; y++)
1236 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
1237 if (p == (const Quantum *) NULL)
1239 q=pixels+(image->rows-y-1)*bytes_per_line;
1240 for (x=0; x < (ssize_t) image->columns; x++)
1242 *q++=ScaleQuantumToChar(GetPixelBlue(image,p));
1243 *q++=ScaleQuantumToChar(GetPixelGreen(image,p));
1244 *q++=ScaleQuantumToChar(GetPixelRed(image,p));
1245 if (image->alpha_trait != UndefinedPixelTrait)
1246 *q++=ScaleQuantumToChar(GetPixelAlpha(image,p));
1247 p+=GetPixelChannels(image);
1249 if (dib_info.bits_per_pixel == 24)
1250 for (x=(ssize_t) (3*image->columns); x < (ssize_t) bytes_per_line; x++)
1252 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
1254 if (status == MagickFalse)
1260 if (dib_info.bits_per_pixel == 8)
1261 if (image_info->compression != NoCompression)
1267 Convert run-length encoded raster pixels.
1269 length=2UL*(bytes_per_line+2UL)+2UL;
1270 dib_data=(unsigned char *) AcquireQuantumMemory(length,
1271 (image->rows+2UL)*sizeof(*dib_data));
1272 if (dib_data == (unsigned char *) NULL)
1274 pixels=(unsigned char *) RelinquishMagickMemory(pixels);
1275 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
1277 dib_info.image_size=(size_t) EncodeImage(image,bytes_per_line,
1279 pixels=(unsigned char *) RelinquishMagickMemory(pixels);
1281 dib_info.compression = BI_RLE8;
1286 (void) WriteBlobLSBLong(image,(unsigned int) dib_info.size);
1287 (void) WriteBlobLSBLong(image,dib_info.width);
1288 (void) WriteBlobLSBLong(image,(unsigned short) dib_info.height);
1289 (void) WriteBlobLSBShort(image,(unsigned short) dib_info.planes);
1290 (void) WriteBlobLSBShort(image,dib_info.bits_per_pixel);
1291 (void) WriteBlobLSBLong(image,(unsigned int) dib_info.compression);
1292 (void) WriteBlobLSBLong(image,(unsigned int) dib_info.image_size);
1293 (void) WriteBlobLSBLong(image,(unsigned int) dib_info.x_pixels);
1294 (void) WriteBlobLSBLong(image,(unsigned int) dib_info.y_pixels);
1295 (void) WriteBlobLSBLong(image,(unsigned int) dib_info.number_colors);
1296 (void) WriteBlobLSBLong(image,(unsigned int) dib_info.colors_important);
1297 if (image->storage_class == PseudoClass)
1299 if (dib_info.bits_per_pixel <= 8)
1305 Dump colormap to file.
1307 dib_colormap=(unsigned char *) AcquireQuantumMemory((size_t)
1308 (1UL << dib_info.bits_per_pixel),4*sizeof(*dib_colormap));
1309 if (dib_colormap == (unsigned char *) NULL)
1310 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
1312 for (i=0; i < (ssize_t) MagickMin(image->colors,dib_info.number_colors); i++)
1314 *q++=ScaleQuantumToChar(image->colormap[i].blue);
1315 *q++=ScaleQuantumToChar(image->colormap[i].green);
1316 *q++=ScaleQuantumToChar(image->colormap[i].red);
1319 for ( ; i < (ssize_t) (1L << dib_info.bits_per_pixel); i++)
1326 (void) WriteBlob(image,(size_t) (4*(1 << dib_info.bits_per_pixel)),
1328 dib_colormap=(unsigned char *) RelinquishMagickMemory(dib_colormap);
1331 if ((dib_info.bits_per_pixel == 16) &&
1332 (dib_info.compression == BI_BITFIELDS))
1334 (void) WriteBlobLSBLong(image,0xf800);
1335 (void) WriteBlobLSBLong(image,0x07e0);
1336 (void) WriteBlobLSBLong(image,0x001f);
1339 (void) WriteBlob(image,dib_info.image_size,pixels);
1340 pixels=(unsigned char *) RelinquishMagickMemory(pixels);
1341 (void) CloseBlob(image);