2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
13 % Read/Write Windows DIB Image Format %
20 % Copyright 1999-2015 ImageMagick Studio LLC, a non-profit organization %
21 % dedicated to making software imaging solutions freely available. %
23 % You may not use this file except in compliance with the License. You may %
24 % obtain a copy of the License at %
26 % http://www.imagemagick.org/script/license.php %
28 % Unless required by applicable law or agreed to in writing, software %
29 % distributed under the License is distributed on an "AS IS" BASIS, %
30 % WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. %
31 % See the License for the specific language governing permissions and %
32 % limitations under the License. %
34 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
42 #include "MagickCore/studio.h"
43 #include "MagickCore/attribute.h"
44 #include "MagickCore/blob.h"
45 #include "MagickCore/blob-private.h"
46 #include "MagickCore/cache.h"
47 #include "MagickCore/color.h"
48 #include "MagickCore/color-private.h"
49 #include "MagickCore/colormap.h"
50 #include "MagickCore/colormap-private.h"
51 #include "MagickCore/colorspace.h"
52 #include "MagickCore/colorspace-private.h"
53 #include "MagickCore/draw.h"
54 #include "MagickCore/exception.h"
55 #include "MagickCore/exception-private.h"
56 #include "MagickCore/geometry.h"
57 #include "MagickCore/image.h"
58 #include "MagickCore/image-private.h"
59 #include "MagickCore/list.h"
60 #include "MagickCore/log.h"
61 #include "MagickCore/magick.h"
62 #include "MagickCore/memory_.h"
63 #include "MagickCore/monitor.h"
64 #include "MagickCore/monitor-private.h"
65 #include "MagickCore/pixel-accessor.h"
66 #include "MagickCore/quantum-private.h"
67 #include "MagickCore/static.h"
68 #include "MagickCore/string_.h"
69 #include "MagickCore/module.h"
70 #include "MagickCore/transform.h"
75 typedef struct _DIBInfo
111 Forward declarations.
113 static MagickBooleanType
114 WriteDIBImage(const ImageInfo *,Image *,ExceptionInfo *);
117 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
121 % D e c o d e I m a g e %
125 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
127 % DecodeImage unpacks the packed image pixels into runlength-encoded
130 % The format of the DecodeImage method is:
132 % MagickBooleanType DecodeImage(Image *image,
133 % const MagickBooleanType compression,unsigned char *pixels)
135 % A description of each parameter follows:
137 % o image: the address of a structure of type Image.
139 % o compression: A value of 1 means the compressed pixels are runlength
140 % encoded for a 256-color bitmap. A value of 2 means a 16-color bitmap.
142 % o pixels: The address of a byte (8 bits) array of pixel data created by
143 % the decoding process.
147 static inline size_t MagickMin(const size_t x,const size_t y)
154 static MagickBooleanType DecodeImage(Image *image,
155 const MagickBooleanType compression,unsigned char *pixels)
157 #if !defined(MAGICKCORE_WINDOWS_SUPPORT) || defined(__MINGW32__) || defined(__MINGW64__)
161 #define BI_BITFIELDS 3
178 register unsigned char
185 assert(image != (Image *) NULL);
186 assert(image->signature == MagickSignature);
187 if (image->debug != MagickFalse)
188 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
189 assert(pixels != (unsigned char *) NULL);
190 (void) ResetMagickMemory(pixels,0,(size_t) image->columns*image->rows*
195 q=pixels+(size_t) image->columns*image->rows;
196 for (y=0; y < (ssize_t) image->rows; )
198 if ((p < pixels) || (p >= q))
200 count=ReadBlobByte(image);
205 count=(int) MagickMin((size_t) count,(size_t) (q-p));
209 byte=(unsigned char) ReadBlobByte(image);
210 if (compression == BI_RLE8)
212 for (i=0; i < count; i++)
213 *p++=(unsigned char) byte;
217 for (i=0; i < count; i++)
219 ((i & 0x01) != 0 ? (byte & 0x0f) : ((byte >> 4) & 0x0f));
228 count=ReadBlobByte(image);
240 p=pixels+y*image->columns;
248 x+=ReadBlobByte(image);
249 y+=ReadBlobByte(image);
250 p=pixels+y*image->columns+x;
258 count=(int) MagickMin((size_t) count,(size_t) (q-p));
259 if (compression == BI_RLE8)
260 for (i=0; i < count; i++)
261 *p++=(unsigned char) ReadBlobByte(image);
263 for (i=0; i < count; i++)
266 byte=(unsigned char) ReadBlobByte(image);
268 ((i & 0x01) != 0 ? (byte & 0x0f) : ((byte >> 4) & 0x0f));
274 if (compression == BI_RLE8)
276 if ((count & 0x01) != 0)
277 (void) ReadBlobByte(image);
280 if (((count & 0x03) == 1) || ((count & 0x03) == 2))
281 (void) ReadBlobByte(image);
286 if (SetImageProgress(image,LoadImageTag,y,image->rows) == MagickFalse)
289 (void) ReadBlobByte(image); /* end of line */
290 (void) ReadBlobByte(image);
295 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
299 % E n c o d e I m a g e %
303 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
305 % EncodeImage compresses pixels using a runlength encoded format.
307 % The format of the EncodeImage method is:
309 % static MagickBooleanType EncodeImage(Image *image,
310 % const size_t bytes_per_line,const unsigned char *pixels,
311 % unsigned char *compressed_pixels)
313 % A description of each parameter follows:
315 % o image: The image.
317 % o bytes_per_line: the number of bytes in a scanline of compressed pixels
319 % o pixels: The address of a byte (8 bits) array of pixel data created by
320 % the compression process.
322 % o compressed_pixels: The address of a byte (8 bits) array of compressed
326 static size_t EncodeImage(Image *image,const size_t bytes_per_line,
327 const unsigned char *pixels,unsigned char *compressed_pixels)
332 register const unsigned char
339 register unsigned char
343 Runlength encode pixels.
345 assert(image != (Image *) NULL);
346 assert(image->signature == MagickSignature);
347 if (image->debug != MagickFalse)
348 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
349 assert(pixels != (const unsigned char *) NULL);
350 assert(compressed_pixels != (unsigned char *) NULL);
354 for (y=0; y < (ssize_t) image->rows; y++)
356 for (x=0; x < (ssize_t) bytes_per_line; x+=i)
361 for (i=1; ((x+i) < (ssize_t) bytes_per_line); i++)
362 if ((*(p+i) != *p) || (i == 255))
364 *q++=(unsigned char) i;
373 if (SetImageProgress(image,LoadImageTag,y,image->rows) == MagickFalse)
381 return((size_t) (q-compressed_pixels));
385 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
393 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
395 % IsDIB() returns MagickTrue if the image format type, identified by the
396 % magick string, is DIB.
398 % The format of the IsDIB method is:
400 % MagickBooleanType IsDIB(const unsigned char *magick,const size_t length)
402 % A description of each parameter follows:
404 % o magick: compare image format pattern against these bytes.
406 % o length: Specifies the length of the magick string.
409 static MagickBooleanType IsDIB(const unsigned char *magick,const size_t length)
413 if (memcmp(magick,"\050\000",2) == 0)
419 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
423 % R e a d D I B I m a g e %
427 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
429 % ReadDIBImage() reads a Microsoft Windows bitmap image file and
430 % returns it. It allocates the memory necessary for the new Image structure
431 % and returns a pointer to the new image.
433 % The format of the ReadDIBImage method is:
435 % image=ReadDIBImage(image_info)
437 % A description of each parameter follows:
439 % o image_info: the image info.
441 % o exception: return any errors or warnings in this structure.
445 static inline ssize_t MagickAbsoluteValue(const ssize_t x)
452 static inline size_t MagickMax(const size_t x,const size_t y)
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 == MagickSignature);
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 == MagickSignature);
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) ResetMagickMemory(&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=(short) ReadBlobLSBLong(image);
529 dib_info.height=(short) ReadBlobLSBLong(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.compression == BI_BITFIELDS) &&
541 ((dib_info.bits_per_pixel == 16) || (dib_info.bits_per_pixel == 32)))
543 dib_info.red_mask=ReadBlobLSBLong(image);
544 dib_info.green_mask=ReadBlobLSBLong(image);
545 dib_info.blue_mask=ReadBlobLSBLong(image);
547 if (dib_info.width <= 0)
548 ThrowReaderException(CorruptImageError,"NegativeOrZeroImageSize");
549 if (dib_info.height == 0)
550 ThrowReaderException(CorruptImageError,"NegativeOrZeroImageSize");
551 if (dib_info.planes != 1)
552 ThrowReaderException(CorruptImageError,"StaticPlanesValueNotEqualToOne");
553 if ((dib_info.bits_per_pixel != 1) && (dib_info.bits_per_pixel != 4) &&
554 (dib_info.bits_per_pixel != 8) && (dib_info.bits_per_pixel != 16) &&
555 (dib_info.bits_per_pixel != 24) && (dib_info.bits_per_pixel != 32))
556 ThrowReaderException(CorruptImageError,"UnrecognizedBitsPerPixel");
557 if (dib_info.bits_per_pixel < 16 &&
558 dib_info.number_colors > (1U << dib_info.bits_per_pixel))
559 ThrowReaderException(CorruptImageError,"UnrecognizedNumberOfColors");
560 if ((dib_info.compression == 1) && (dib_info.bits_per_pixel != 8))
561 ThrowReaderException(CorruptImageError,"UnrecognizedBitsPerPixel");
562 if ((dib_info.compression == 2) && (dib_info.bits_per_pixel != 4))
563 ThrowReaderException(CorruptImageError,"UnrecognizedBitsPerPixel");
564 if ((dib_info.compression == 3) && (dib_info.bits_per_pixel < 16))
565 ThrowReaderException(CorruptImageError,"UnrecognizedBitsPerPixel");
566 switch (dib_info.compression)
574 ThrowReaderException(CoderError,"JPEGCompressNotSupported");
576 ThrowReaderException(CoderError,"PNGCompressNotSupported");
578 ThrowReaderException(CorruptImageError,"UnrecognizedImageCompression");
580 image->columns=(size_t) MagickAbsoluteValue(dib_info.width);
581 image->rows=(size_t) MagickAbsoluteValue(dib_info.height);
583 image->alpha_trait=dib_info.bits_per_pixel == 32 ? BlendPixelTrait :
585 if ((dib_info.number_colors != 0) || (dib_info.bits_per_pixel < 16))
590 image->storage_class=PseudoClass;
591 image->colors=dib_info.number_colors;
593 if (image->colors == 0)
594 image->colors=one << dib_info.bits_per_pixel;
596 if (image_info->size)
604 flags=ParseAbsoluteGeometry(image_info->size,&geometry);
605 if (flags & WidthValue)
606 if ((geometry.width != 0) && (geometry.width < image->columns))
607 image->columns=geometry.width;
608 if (flags & HeightValue)
609 if ((geometry.height != 0) && (geometry.height < image->rows))
610 image->rows=geometry.height;
612 status=SetImageExtent(image,image->columns,image->rows,exception);
613 if (status == MagickFalse)
614 return(DestroyImageList(image));
615 if (image->storage_class == PseudoClass)
625 Read DIB raster colormap.
627 if (AcquireImageColormap(image,image->colors,exception) == MagickFalse)
628 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
629 length=(size_t) image->colors;
630 dib_colormap=(unsigned char *) AcquireQuantumMemory(length,
631 4*sizeof(*dib_colormap));
632 if (dib_colormap == (unsigned char *) NULL)
633 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
635 count=ReadBlob(image,packet_size*image->colors,dib_colormap);
636 if (count != (ssize_t) (packet_size*image->colors))
637 ThrowReaderException(CorruptImageError,"InsufficientImageDataInFile");
639 for (i=0; i < (ssize_t) image->colors; i++)
641 image->colormap[i].blue=ScaleCharToQuantum(*p++);
642 image->colormap[i].green=ScaleCharToQuantum(*p++);
643 image->colormap[i].red=ScaleCharToQuantum(*p++);
644 if (packet_size == 4)
647 dib_colormap=(unsigned char *) RelinquishMagickMemory(dib_colormap);
652 if (dib_info.compression == BI_RLE4)
653 dib_info.bits_per_pixel<<=1;
654 bytes_per_line=4*((image->columns*dib_info.bits_per_pixel+31)/32);
655 length=bytes_per_line*image->rows;
656 pixel_info=AcquireVirtualMemory((size_t) image->rows,MagickMax(
657 bytes_per_line,image->columns+256UL)*sizeof(*pixels));
658 if (pixel_info == (MemoryInfo *) NULL)
659 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
660 pixels=(unsigned char *) GetVirtualMemoryBlob(pixel_info);
661 if ((dib_info.compression == BI_RGB) ||
662 (dib_info.compression == BI_BITFIELDS))
664 count=ReadBlob(image,length,pixels);
665 if (count != (ssize_t) (length))
666 ThrowReaderException(CorruptImageError,"InsufficientImageDataInFile");
671 Convert run-length encoded raster pixels.
673 status=DecodeImage(image,dib_info.compression ? MagickTrue : MagickFalse,
675 if (status == MagickFalse)
676 ThrowReaderException(CorruptImageError,"UnableToRunlengthDecodeImage");
679 Initialize image structure.
681 image->units=PixelsPerCentimeterResolution;
682 image->resolution.x=(double) dib_info.x_pixels/100.0;
683 image->resolution.y=(double) dib_info.y_pixels/100.0;
685 Convert DIB raster image to pixel packets.
687 switch (dib_info.bits_per_pixel)
692 Convert bitmap scanline.
694 for (y=(ssize_t) image->rows-1; y >= 0; y--)
696 p=pixels+(image->rows-y-1)*bytes_per_line;
697 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
698 if (q == (Quantum *) NULL)
700 for (x=0; x < ((ssize_t) image->columns-7); x+=8)
702 for (bit=0; bit < 8; bit++)
704 index=(Quantum) ((*p) & (0x80 >> bit) ? 0x01 : 0x00);
705 SetPixelIndex(image,index,q);
706 q+=GetPixelChannels(image);
710 if ((image->columns % 8) != 0)
712 for (bit=0; bit < (ssize_t) (image->columns % 8); bit++)
714 index=(Quantum) ((*p) & (0x80 >> bit) ? 0x01 : 0x00);
715 SetPixelIndex(image,index,q);
716 q+=GetPixelChannels(image);
720 if (SyncAuthenticPixels(image,exception) == MagickFalse)
722 if (image->previous == (Image *) NULL)
724 status=SetImageProgress(image,LoadImageTag,image->rows-y-1,
726 if (status == MagickFalse)
730 (void) SyncImage(image,exception);
736 Convert PseudoColor scanline.
738 for (y=(ssize_t) image->rows-1; y >= 0; y--)
740 p=pixels+(image->rows-y-1)*bytes_per_line;
741 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
742 if (q == (Quantum *) NULL)
744 for (x=0; x < ((ssize_t) image->columns-1); x+=2)
746 index=ConstrainColormapIndex(image,(*p >> 4) & 0xf,exception);
747 SetPixelIndex(image,index,q);
748 q+=GetPixelChannels(image);
749 index=ConstrainColormapIndex(image,*p & 0xf,exception);
750 SetPixelIndex(image,index,q);
752 q+=GetPixelChannels(image);
754 if ((image->columns % 2) != 0)
756 index=ConstrainColormapIndex(image,(*p >> 4) & 0xf,exception);
757 SetPixelIndex(image,index,q);
758 q+=GetPixelChannels(image);
761 if (SyncAuthenticPixels(image,exception) == MagickFalse)
763 if (image->previous == (Image *) NULL)
765 status=SetImageProgress(image,LoadImageTag,image->rows-y-1,
767 if (status == MagickFalse)
771 (void) SyncImage(image,exception);
777 Convert PseudoColor scanline.
779 if ((dib_info.compression == BI_RLE8) ||
780 (dib_info.compression == BI_RLE4))
781 bytes_per_line=image->columns;
782 for (y=(ssize_t) image->rows-1; y >= 0; y--)
784 p=pixels+(image->rows-y-1)*bytes_per_line;
785 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
786 if (q == (Quantum *) NULL)
788 for (x=0; x < (ssize_t) image->columns; x++)
790 index=ConstrainColormapIndex(image,*p,exception);
791 SetPixelIndex(image,index,q);
793 q+=GetPixelChannels(image);
795 if (SyncAuthenticPixels(image,exception) == MagickFalse)
797 if (image->previous == (Image *) NULL)
799 status=SetImageProgress(image,LoadImageTag,image->rows-y-1,
801 if (status == MagickFalse)
805 (void) SyncImage(image,exception);
814 Convert PseudoColor scanline.
816 image->storage_class=DirectClass;
817 if (dib_info.compression == BI_RLE8)
818 bytes_per_line=2*image->columns;
819 for (y=(ssize_t) image->rows-1; y >= 0; y--)
821 p=pixels+(image->rows-y-1)*bytes_per_line;
822 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
823 if (q == (Quantum *) NULL)
825 for (x=0; x < (ssize_t) image->columns; x++)
829 if (dib_info.red_mask == 0)
831 SetPixelRed(image,ScaleCharToQuantum(ScaleColor5to8(
832 (unsigned char) ((word >> 10) & 0x1f))),q);
833 SetPixelGreen(image,ScaleCharToQuantum(ScaleColor5to8(
834 (unsigned char) ((word >> 5) & 0x1f))),q);
835 SetPixelBlue(image,ScaleCharToQuantum(ScaleColor5to8(
836 (unsigned char) (word & 0x1f))),q);
840 SetPixelRed(image,ScaleCharToQuantum(ScaleColor5to8(
841 (unsigned char) ((word >> 11) & 0x1f))),q);
842 SetPixelGreen(image,ScaleCharToQuantum(ScaleColor6to8(
843 (unsigned char) ((word >> 5) & 0x3f))),q);
844 SetPixelBlue(image,ScaleCharToQuantum(ScaleColor5to8(
845 (unsigned char) (word & 0x1f))),q);
847 q+=GetPixelChannels(image);
849 if (SyncAuthenticPixels(image,exception) == MagickFalse)
851 if (image->previous == (Image *) NULL)
853 status=SetImageProgress(image,LoadImageTag,image->rows-y-1,
855 if (status == MagickFalse)
865 Convert DirectColor scanline.
867 for (y=(ssize_t) image->rows-1; y >= 0; y--)
869 p=pixels+(image->rows-y-1)*bytes_per_line;
870 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
871 if (q == (Quantum *) NULL)
873 for (x=0; x < (ssize_t) image->columns; x++)
875 SetPixelBlue(image,ScaleCharToQuantum(*p++),q);
876 SetPixelGreen(image,ScaleCharToQuantum(*p++),q);
877 SetPixelRed(image,ScaleCharToQuantum(*p++),q);
878 if (image->alpha_trait != UndefinedPixelTrait)
879 SetPixelAlpha(image,ScaleCharToQuantum(*p++),q);
880 q+=GetPixelChannels(image);
882 if (SyncAuthenticPixels(image,exception) == MagickFalse)
884 if (image->previous == (Image *) NULL)
886 status=SetImageProgress(image,LoadImageTag,image->rows-y-1,
888 if (status == MagickFalse)
895 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
897 pixel_info=RelinquishVirtualMemory(pixel_info);
898 if (EOFBlob(image) != MagickFalse)
899 ThrowFileException(exception,CorruptImageError,"UnexpectedEndOfFile",
901 if (dib_info.height < 0)
907 Correct image orientation.
909 flipped_image=FlipImage(image,exception);
910 if (flipped_image != (Image *) NULL)
912 DuplicateBlob(flipped_image,image);
913 image=DestroyImage(image);
917 (void) CloseBlob(image);
918 return(GetFirstImageInList(image));
922 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
926 % R e g i s t e r D I B I m a g e %
930 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
932 % RegisterDIBImage() adds attributes for the DIB image format to
933 % the list of supported formats. The attributes include the image format
934 % tag, a method to read and/or write the format, whether the format
935 % supports the saving of more than one frame to the same file or blob,
936 % whether the format supports native in-memory I/O, and a brief
937 % description of the format.
939 % The format of the RegisterDIBImage method is:
941 % size_t RegisterDIBImage(void)
944 ModuleExport size_t RegisterDIBImage(void)
949 entry=SetMagickInfo("DIB");
950 entry->decoder=(DecodeImageHandler *) ReadDIBImage;
951 entry->encoder=(EncodeImageHandler *) WriteDIBImage;
952 entry->magick=(IsImageFormatHandler *) IsDIB;
953 entry->adjoin=MagickFalse;
954 entry->stealth=MagickTrue;
955 entry->description=ConstantString(
956 "Microsoft Windows 3.X Packed Device-Independent Bitmap");
957 entry->module=ConstantString("DIB");
958 (void) RegisterMagickInfo(entry);
959 return(MagickImageCoderSignature);
963 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
967 % U n r e g i s t e r D I B I m a g e %
971 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
973 % UnregisterDIBImage() removes format registrations made by the
974 % DIB module from the list of supported formats.
976 % The format of the UnregisterDIBImage method is:
978 % UnregisterDIBImage(void)
981 ModuleExport void UnregisterDIBImage(void)
983 (void) UnregisterMagickInfo("DIB");
987 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
991 % W r i t e D I B I m a g e %
995 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
997 % WriteDIBImage() writes an image in Microsoft Windows bitmap encoded
1000 % The format of the WriteDIBImage method is:
1002 % MagickBooleanType WriteDIBImage(const ImageInfo *image_info,
1003 % Image *image,ExceptionInfo *exception)
1005 % A description of each parameter follows.
1007 % o image_info: the image info.
1009 % o image: The image.
1011 % o exception: return any errors or warnings in this structure.
1014 static MagickBooleanType WriteDIBImage(const ImageInfo *image_info,Image *image,
1015 ExceptionInfo *exception)
1023 register const Quantum
1030 register unsigned char
1044 Open output image file.
1046 assert(image_info != (const ImageInfo *) NULL);
1047 assert(image_info->signature == MagickSignature);
1048 assert(image != (Image *) NULL);
1049 assert(image->signature == MagickSignature);
1050 if (image->debug != MagickFalse)
1051 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1052 assert(exception != (ExceptionInfo *) NULL);
1053 assert(exception->signature == MagickSignature);
1054 status=OpenBlob(image_info,image,WriteBinaryBlobMode,exception);
1055 if (status == MagickFalse)
1058 Initialize DIB raster file header.
1060 (void) TransformImageColorspace(image,sRGBColorspace,exception);
1061 if (image->storage_class == DirectClass)
1064 Full color DIB raster.
1066 dib_info.number_colors=0;
1067 dib_info.bits_per_pixel=(unsigned short) (image->alpha_trait ? 32 : 24);
1072 Colormapped DIB raster.
1074 dib_info.bits_per_pixel=8;
1075 if (image_info->depth > 8)
1076 dib_info.bits_per_pixel=16;
1077 if (IsImageMonochrome(image,exception) != MagickFalse)
1078 dib_info.bits_per_pixel=1;
1079 dib_info.number_colors=(dib_info.bits_per_pixel == 16) ? 0 :
1080 (1UL << dib_info.bits_per_pixel);
1082 bytes_per_line=4*((image->columns*dib_info.bits_per_pixel+31)/32);
1084 dib_info.width=(ssize_t) image->columns;
1085 dib_info.height=(ssize_t) image->rows;
1087 dib_info.compression=(size_t) (dib_info.bits_per_pixel == 16 ?
1088 BI_BITFIELDS : BI_RGB);
1089 dib_info.image_size=bytes_per_line*image->rows;
1090 dib_info.x_pixels=75*39;
1091 dib_info.y_pixels=75*39;
1092 switch (image->units)
1094 case UndefinedResolution:
1095 case PixelsPerInchResolution:
1097 dib_info.x_pixels=(size_t) (100.0*image->resolution.x/2.54);
1098 dib_info.y_pixels=(size_t) (100.0*image->resolution.y/2.54);
1101 case PixelsPerCentimeterResolution:
1103 dib_info.x_pixels=(size_t) (100.0*image->resolution.x);
1104 dib_info.y_pixels=(size_t) (100.0*image->resolution.y);
1108 dib_info.colors_important=dib_info.number_colors;
1110 Convert MIFF to DIB raster pixels.
1112 pixels=(unsigned char *) AcquireQuantumMemory(dib_info.image_size,
1114 if (pixels == (unsigned char *) NULL)
1115 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
1116 (void) ResetMagickMemory(pixels,0,dib_info.image_size);
1117 switch (dib_info.bits_per_pixel)
1121 register unsigned char
1126 Convert PseudoClass image to a DIB monochrome image.
1128 for (y=0; y < (ssize_t) image->rows; y++)
1130 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
1131 if (p == (const Quantum *) NULL)
1133 q=pixels+(image->rows-y-1)*bytes_per_line;
1136 for (x=0; x < (ssize_t) image->columns; x++)
1139 byte|=GetPixelIndex(image,p) != 0 ? 0x01 : 0x00;
1147 p+=GetPixelChannels(image);
1151 *q++=(unsigned char) (byte << (8-bit));
1154 for (x=(ssize_t) (image->columns+7)/8; x < (ssize_t) bytes_per_line; x++)
1156 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
1158 if (status == MagickFalse)
1166 Convert PseudoClass packet to DIB pixel.
1168 for (y=0; y < (ssize_t) image->rows; y++)
1170 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
1171 if (p == (const Quantum *) NULL)
1173 q=pixels+(image->rows-y-1)*bytes_per_line;
1174 for (x=0; x < (ssize_t) image->columns; x++)
1176 *q++=(unsigned char) GetPixelIndex(image,p);
1177 p+=GetPixelChannels(image);
1179 for ( ; x < (ssize_t) bytes_per_line; x++)
1181 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
1183 if (status == MagickFalse)
1193 Convert PseudoClass packet to DIB pixel.
1195 for (y=0; y < (ssize_t) image->rows; y++)
1197 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
1198 if (p == (const Quantum *) NULL)
1200 q=pixels+(image->rows-y-1)*bytes_per_line;
1201 for (x=0; x < (ssize_t) image->columns; x++)
1203 word=(unsigned short) ((ScaleColor8to5((unsigned char)
1204 ScaleQuantumToChar(GetPixelRed(image,p))) << 11) | (ScaleColor8to6(
1205 (unsigned char) ScaleQuantumToChar(GetPixelGreen(image,p))) << 5) |
1206 (ScaleColor8to5((unsigned char) ScaleQuantumToChar((unsigned char)
1207 GetPixelBlue(image,p)) << 0)));
1208 *q++=(unsigned char)(word & 0xff);
1209 *q++=(unsigned char)(word >> 8);
1210 p+=GetPixelChannels(image);
1212 for (x=(ssize_t) (2*image->columns); x < (ssize_t) bytes_per_line; x++)
1214 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
1216 if (status == MagickFalse)
1225 Convert DirectClass packet to DIB RGB pixel.
1227 for (y=0; y < (ssize_t) image->rows; y++)
1229 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
1230 if (p == (const Quantum *) NULL)
1232 q=pixels+(image->rows-y-1)*bytes_per_line;
1233 for (x=0; x < (ssize_t) image->columns; x++)
1235 *q++=ScaleQuantumToChar(GetPixelBlue(image,p));
1236 *q++=ScaleQuantumToChar(GetPixelGreen(image,p));
1237 *q++=ScaleQuantumToChar(GetPixelRed(image,p));
1238 if (image->alpha_trait != UndefinedPixelTrait)
1239 *q++=ScaleQuantumToChar(GetPixelAlpha(image,p));
1240 p+=GetPixelChannels(image);
1242 if (dib_info.bits_per_pixel == 24)
1243 for (x=(ssize_t) (3*image->columns); x < (ssize_t) bytes_per_line; x++)
1245 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
1247 if (status == MagickFalse)
1253 if (dib_info.bits_per_pixel == 8)
1254 if (image_info->compression != NoCompression)
1260 Convert run-length encoded raster pixels.
1262 length=2UL*(bytes_per_line+2UL)+2UL;
1263 dib_data=(unsigned char *) AcquireQuantumMemory(length,
1264 (image->rows+2UL)*sizeof(*dib_data));
1265 if (dib_data == (unsigned char *) NULL)
1267 pixels=(unsigned char *) RelinquishMagickMemory(pixels);
1268 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
1270 dib_info.image_size=(size_t) EncodeImage(image,bytes_per_line,
1272 pixels=(unsigned char *) RelinquishMagickMemory(pixels);
1274 dib_info.compression = BI_RLE8;
1279 (void) WriteBlobLSBLong(image,(unsigned int) dib_info.size);
1280 (void) WriteBlobLSBLong(image,dib_info.width);
1281 (void) WriteBlobLSBLong(image,(unsigned short) dib_info.height);
1282 (void) WriteBlobLSBShort(image,(unsigned short) dib_info.planes);
1283 (void) WriteBlobLSBShort(image,dib_info.bits_per_pixel);
1284 (void) WriteBlobLSBLong(image,(unsigned int) dib_info.compression);
1285 (void) WriteBlobLSBLong(image,(unsigned int) dib_info.image_size);
1286 (void) WriteBlobLSBLong(image,(unsigned int) dib_info.x_pixels);
1287 (void) WriteBlobLSBLong(image,(unsigned int) dib_info.y_pixels);
1288 (void) WriteBlobLSBLong(image,(unsigned int) dib_info.number_colors);
1289 (void) WriteBlobLSBLong(image,(unsigned int) dib_info.colors_important);
1290 if (image->storage_class == PseudoClass)
1292 if (dib_info.bits_per_pixel <= 8)
1298 Dump colormap to file.
1300 dib_colormap=(unsigned char *) AcquireQuantumMemory((size_t)
1301 (1UL << dib_info.bits_per_pixel),4*sizeof(*dib_colormap));
1302 if (dib_colormap == (unsigned char *) NULL)
1303 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
1305 for (i=0; i < (ssize_t) MagickMin(image->colors,dib_info.number_colors); i++)
1307 *q++=ScaleQuantumToChar(image->colormap[i].blue);
1308 *q++=ScaleQuantumToChar(image->colormap[i].green);
1309 *q++=ScaleQuantumToChar(image->colormap[i].red);
1312 for ( ; i < (ssize_t) (1L << dib_info.bits_per_pixel); i++)
1319 (void) WriteBlob(image,(size_t) (4*(1 << dib_info.bits_per_pixel)),
1321 dib_colormap=(unsigned char *) RelinquishMagickMemory(dib_colormap);
1324 if ((dib_info.bits_per_pixel == 16) &&
1325 (dib_info.compression == BI_BITFIELDS))
1327 (void) WriteBlobLSBLong(image,0xf800);
1328 (void) WriteBlobLSBLong(image,0x07e0);
1329 (void) WriteBlobLSBLong(image,0x001f);
1332 (void) WriteBlob(image,dib_info.image_size,pixels);
1333 pixels=(unsigned char *) RelinquishMagickMemory(pixels);
1334 (void) CloseBlob(image);