blob: afa3062c5e39372b04271e9c6fcda698bdbf8f64 [file] [log] [blame]
cristy3ed852e2009-09-05 21:47:34 +00001/*
2%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3% %
4% %
5% %
6% DDDD IIIII BBBB %
7% D D I B B %
8% D D I BBBB %
9% D D I B B %
10% DDDD IIIII BBBB %
11% %
12% %
13% Read/Write Windows DIB Image Format %
14% %
15% Software Design %
16% John Cristy %
17% July 1992 %
18% %
19% %
20% Copyright 1999-2009 ImageMagick Studio LLC, a non-profit organization %
21% dedicated to making software imaging solutions freely available. %
22% %
23% You may not use this file except in compliance with the License. You may %
24% obtain a copy of the License at %
25% %
26% http://www.imagemagick.org/script/license.php %
27% %
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. %
33% %
34%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
35%
36%
37*/
38
39/*
40 Include declarations.
41*/
42#include "magick/studio.h"
43#include "magick/blob.h"
44#include "magick/blob-private.h"
45#include "magick/cache.h"
46#include "magick/color.h"
47#include "magick/color-private.h"
48#include "magick/colorspace.h"
49#include "magick/draw.h"
50#include "magick/exception.h"
51#include "magick/exception-private.h"
52#include "magick/geometry.h"
53#include "magick/image.h"
54#include "magick/image-private.h"
55#include "magick/list.h"
56#include "magick/log.h"
57#include "magick/magick.h"
58#include "magick/memory_.h"
59#include "magick/monitor.h"
60#include "magick/monitor-private.h"
61#include "magick/quantum-private.h"
62#include "magick/static.h"
63#include "magick/string_.h"
64#include "magick/module.h"
65#include "magick/transform.h"
66
67/*
68 Typedef declarations.
69*/
70typedef struct _DIBInfo
71{
72 unsigned long
73 size;
74
75 long
76 width,
77 height;
78
79 unsigned short
80 planes,
81 bits_per_pixel;
82
83 unsigned long
84 compression,
85 image_size,
86 x_pixels,
87 y_pixels,
88 number_colors,
89 red_mask,
90 green_mask,
91 blue_mask,
92 alpha_mask,
93 colors_important;
94
95 long
96 colorspace;
97
98 PointInfo
99 red_primary,
100 green_primary,
101 blue_primary,
102 gamma_scale;
103} DIBInfo;
104
105/*
106 Forward declarations.
107*/
108static MagickBooleanType
109 WriteDIBImage(const ImageInfo *,Image *);
110
111/*
112%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
113% %
114% %
115% %
116% D e c o d e I m a g e %
117% %
118% %
119% %
120%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
121%
122% DecodeImage unpacks the packed image pixels into runlength-encoded
123% pixel packets.
124%
125% The format of the DecodeImage method is:
126%
127% MagickBooleanType DecodeImage(Image *image,
128% const MagickBooleanType compression,unsigned char *pixels)
129%
130% A description of each parameter follows:
131%
132% o image: the address of a structure of type Image.
133%
134% o compression: A value of 1 means the compressed pixels are runlength
135% encoded for a 256-color bitmap. A value of 2 means a 16-color bitmap.
136%
137% o pixels: The address of a byte (8 bits) array of pixel data created by
138% the decoding process.
139%
140*/
141
142static inline size_t MagickMin(const size_t x,const size_t y)
143{
144 if (x < y)
145 return(x);
146 return(y);
147}
148
149static MagickBooleanType DecodeImage(Image *image,
150 const MagickBooleanType compression,unsigned char *pixels)
151{
152#if !defined(__WINDOWS__) || defined(__MINGW32__)
153#define BI_RGB 0
154#define BI_RLE8 1
155#define BI_RLE4 2
156#define BI_BITFIELDS 3
157#endif
158
159 int
160 count;
161
162 long
163 y;
164
165 register long
166 i,
167 x;
168
169 register unsigned char
170 *p,
171 *q;
172
173 unsigned char
174 byte;
175
176 assert(image != (Image *) NULL);
177 assert(image->signature == MagickSignature);
178 if (image->debug != MagickFalse)
179 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
180 assert(pixels != (unsigned char *) NULL);
181 (void) ResetMagickMemory(pixels,0,(size_t) image->columns*image->rows*
182 sizeof(*pixels));
183 byte=0;
184 x=0;
185 p=pixels;
186 q=pixels+(size_t) image->columns*image->rows;
187 for (y=0; y < (long) image->rows; )
188 {
189 if ((p < pixels) || (p >= q))
190 break;
191 count=ReadBlobByte(image);
192 if (count == EOF)
193 break;
194 if (count != 0)
195 {
196 count=(int) MagickMin((size_t) count,(size_t) (q-p));
197 /*
198 Encoded mode.
199 */
200 byte=(unsigned char) ReadBlobByte(image);
201 if (compression == BI_RLE8)
202 {
203 for (i=0; i < count; i++)
204 *p++=(unsigned char) byte;
205 }
206 else
207 {
208 for (i=0; i < count; i++)
209 *p++=(unsigned char)
210 ((i & 0x01) != 0 ? (byte & 0x0f) : ((byte >> 4) & 0x0f));
211 }
212 x+=count;
213 }
214 else
215 {
216 /*
217 Escape mode.
218 */
219 count=ReadBlobByte(image);
220 if (count == 0x01)
221 return(MagickTrue);
222 switch (count)
223 {
224 case 0x00:
225 {
226 /*
227 End of line.
228 */
229 x=0;
230 y++;
231 p=pixels+y*image->columns;
232 break;
233 }
234 case 0x02:
235 {
236 /*
237 Delta mode.
238 */
239 x+=ReadBlobByte(image);
240 y+=ReadBlobByte(image);
241 p=pixels+y*image->columns+x;
242 break;
243 }
244 default:
245 {
246 /*
247 Absolute mode.
248 */
249 count=(int) MagickMin((size_t) count,(size_t) (q-p));
250 if (compression == BI_RLE8)
251 for (i=0; i < count; i++)
252 *p++=(unsigned char) ReadBlobByte(image);
253 else
254 for (i=0; i < count; i++)
255 {
256 if ((i & 0x01) == 0)
257 byte=(unsigned char) ReadBlobByte(image);
258 *p++=(unsigned char)
259 ((i & 0x01) != 0 ? (byte & 0x0f) : ((byte >> 4) & 0x0f));
260 }
261 x+=count;
262 /*
263 Read pad byte.
264 */
265 if (compression == BI_RLE8)
266 {
267 if ((count & 0x01) != 0)
268 (void) ReadBlobByte(image);
269 }
270 else
271 if (((count & 0x03) == 1) || ((count & 0x03) == 2))
272 (void) ReadBlobByte(image);
273 break;
274 }
275 }
276 }
277 if (SetImageProgress(image,LoadImageTag,y,image->rows) == MagickFalse)
278 break;
279 }
280 (void) ReadBlobByte(image); /* end of line */
281 (void) ReadBlobByte(image);
282 return(MagickTrue);
283}
284
285/*
286%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
287% %
288% %
289% %
290% E n c o d e I m a g e %
291% %
292% %
293% %
294%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
295%
296% EncodeImage compresses pixels using a runlength encoded format.
297%
298% The format of the EncodeImage method is:
299%
300% static MagickBooleanType EncodeImage(Image *image,
301% const unsigned long bytes_per_line,const unsigned char *pixels,
302% unsigned char *compressed_pixels)
303%
304% A description of each parameter follows:
305%
306% o image: The image.
307%
308% o bytes_per_line: the number of bytes in a scanline of compressed pixels
309%
310% o pixels: The address of a byte (8 bits) array of pixel data created by
311% the compression process.
312%
313% o compressed_pixels: The address of a byte (8 bits) array of compressed
314% pixel data.
315%
316*/
317static size_t EncodeImage(Image *image,const unsigned long bytes_per_line,
318 const unsigned char *pixels,unsigned char *compressed_pixels)
319{
320 long
321 y;
322
323 register const unsigned char
324 *p;
325
326 register long
327 i,
328 x;
329
330 register unsigned char
331 *q;
332
333 /*
334 Runlength encode pixels.
335 */
336 assert(image != (Image *) NULL);
337 assert(image->signature == MagickSignature);
338 if (image->debug != MagickFalse)
339 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
340 assert(pixels != (const unsigned char *) NULL);
341 assert(compressed_pixels != (unsigned char *) NULL);
342 p=pixels;
343 q=compressed_pixels;
344 i=0;
345 for (y=0; y < (long) image->rows; y++)
346 {
347 for (x=0; x < (long) bytes_per_line; x+=i)
348 {
349 /*
350 Determine runlength.
351 */
352 for (i=1; ((x+i) < (long) bytes_per_line); i++)
353 if ((*(p+i) != *p) || (i == 255))
354 break;
355 *q++=(unsigned char) i;
356 *q++=(*p);
357 p+=i;
358 }
359 /*
360 End of line.
361 */
362 *q++=0x00;
363 *q++=0x00;
364 if (SetImageProgress(image,LoadImageTag,y,image->rows) == MagickFalse)
365 break;
366 }
367 /*
368 End of bitmap.
369 */
370 *q++=0;
371 *q++=0x01;
372 return((size_t) (q-compressed_pixels));
373}
374
375/*
376%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
377% %
378% %
379% %
380% I s D I B %
381% %
382% %
383% %
384%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
385%
386% IsDIB() returns MagickTrue if the image format type, identified by the
387% magick string, is DIB.
388%
389% The format of the IsDIB method is:
390%
391% MagickBooleanType IsDIB(const unsigned char *magick,const size_t length)
392%
393% A description of each parameter follows:
394%
395% o magick: compare image format pattern against these bytes.
396%
397% o length: Specifies the length of the magick string.
398%
399*/
400static MagickBooleanType IsDIB(const unsigned char *magick,const size_t length)
401{
402 if (length < 2)
403 return(MagickFalse);
404 if (memcmp(magick,"\050\000",2) == 0)
405 return(MagickTrue);
406 return(MagickFalse);
407}
408
409/*
410%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
411% %
412% %
413% %
414% R e a d D I B I m a g e %
415% %
416% %
417% %
418%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
419%
420% ReadDIBImage() reads a Microsoft Windows bitmap image file and
421% returns it. It allocates the memory necessary for the new Image structure
422% and returns a pointer to the new image.
423%
424% The format of the ReadDIBImage method is:
425%
426% image=ReadDIBImage(image_info)
427%
428% A description of each parameter follows:
429%
430% o image_info: the image info.
431%
432% o exception: return any errors or warnings in this structure.
433%
434*/
435
436static inline long MagickAbsoluteValue(const long x)
437{
438 if (x < 0)
439 return(-x);
440 return(x);
441}
442
443static inline size_t MagickMax(const size_t x,const size_t y)
444{
445 if (x > y)
446 return(x);
447 return(y);
448}
449
450static Image *ReadDIBImage(const ImageInfo *image_info,ExceptionInfo *exception)
451{
452 DIBInfo
453 dib_info;
454
455 Image
456 *image;
457
458 IndexPacket
459 index;
460
461 long
462 bit,
463 y;
464
465 MagickBooleanType
466 status;
467
468 register IndexPacket
469 *indexes;
470
471 register long
472 x;
473
474 register PixelPacket
475 *q;
476
477 register long
478 i;
479
480 register unsigned char
481 *p;
482
483 size_t
484 length;
485
486 ssize_t
487 count;
488
489 unsigned char
490 *pixels;
491
492 unsigned long
493 bytes_per_line;
494
495 /*
496 Open image file.
497 */
498 assert(image_info != (const ImageInfo *) NULL);
499 assert(image_info->signature == MagickSignature);
500 if (image_info->debug != MagickFalse)
501 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
502 image_info->filename);
503 assert(exception != (ExceptionInfo *) NULL);
504 assert(exception->signature == MagickSignature);
505 image=AcquireImage(image_info);
506 status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception);
507 if (status == MagickFalse)
508 {
509 image=DestroyImageList(image);
510 return((Image *) NULL);
511 }
512 /*
513 Determine if this a DIB file.
514 */
515 (void) ResetMagickMemory(&dib_info,0,sizeof(dib_info));
516 dib_info.size=ReadBlobLSBLong(image);
517 if (dib_info.size!=40)
518 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
519 /*
520 Microsoft Windows 3.X DIB image file.
521 */
522 dib_info.width=(short) ReadBlobLSBLong(image);
523 dib_info.height=(short) ReadBlobLSBLong(image);
524 dib_info.planes=ReadBlobLSBShort(image);
525 dib_info.bits_per_pixel=ReadBlobLSBShort(image);
526 dib_info.compression=ReadBlobLSBLong(image);
527 dib_info.image_size=ReadBlobLSBLong(image);
528 dib_info.x_pixels=ReadBlobLSBLong(image);
529 dib_info.y_pixels=ReadBlobLSBLong(image);
530 dib_info.number_colors=ReadBlobLSBLong(image);
531 dib_info.colors_important=ReadBlobLSBLong(image);
532 if ((dib_info.compression == BI_BITFIELDS) &&
533 ((dib_info.bits_per_pixel == 16) || (dib_info.bits_per_pixel == 32)))
534 {
535 dib_info.red_mask=ReadBlobLSBLong(image);
536 dib_info.green_mask=ReadBlobLSBLong(image);
537 dib_info.blue_mask=ReadBlobLSBLong(image);
538 }
539 image->matte=dib_info.bits_per_pixel == 32 ? MagickTrue : MagickFalse;
540 image->columns=(unsigned long) MagickAbsoluteValue(dib_info.width);
541 image->rows=(unsigned long) MagickAbsoluteValue(dib_info.height);
542 image->depth=8;
543 if ((dib_info.number_colors != 0) || (dib_info.bits_per_pixel < 16))
544 {
545 image->storage_class=PseudoClass;
546 image->colors=dib_info.number_colors;
547 if (image->colors == 0)
548 image->colors=1L << dib_info.bits_per_pixel;
549 }
550 if (image_info->size)
551 {
552 RectangleInfo
553 geometry;
554
555 MagickStatusType
556 flags;
557
558 flags=ParseAbsoluteGeometry(image_info->size,&geometry);
559 if (flags & WidthValue)
560 if ((geometry.width != 0) && (geometry.width < image->columns))
561 image->columns=geometry.width;
562 if (flags & HeightValue)
563 if ((geometry.height != 0) && (geometry.height < image->rows))
564 image->rows=geometry.height;
565 }
566 if (image->storage_class == PseudoClass)
567 {
568 size_t
569 length,
570 packet_size;
571
572 unsigned char
573 *dib_colormap;
574
575 /*
576 Read DIB raster colormap.
577 */
578 if (AcquireImageColormap(image,image->colors) == MagickFalse)
579 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
580 length=(size_t) image->colors;
581 dib_colormap=(unsigned char *) AcquireQuantumMemory(length,
582 4*sizeof(*dib_colormap));
583 if (dib_colormap == (unsigned char *) NULL)
584 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
585 packet_size=4;
586 count=ReadBlob(image,packet_size*image->colors,dib_colormap);
587 if (count != (ssize_t) (packet_size*image->colors))
588 ThrowReaderException(CorruptImageError,"InsufficientImageDataInFile");
589 p=dib_colormap;
590 for (i=0; i < (long) image->colors; i++)
591 {
592 image->colormap[i].blue=ScaleCharToQuantum(*p++);
593 image->colormap[i].green=ScaleCharToQuantum(*p++);
594 image->colormap[i].red=ScaleCharToQuantum(*p++);
595 if (packet_size == 4)
596 p++;
597 }
598 dib_colormap=(unsigned char *) RelinquishMagickMemory(dib_colormap);
599 }
600 /*
601 Read image data.
602 */
603 if (dib_info.compression == BI_RLE4)
604 dib_info.bits_per_pixel<<=1;
605 bytes_per_line=4*((image->columns*dib_info.bits_per_pixel+31)/32);
606 length=bytes_per_line*image->rows;
607 pixels=(unsigned char *) AcquireQuantumMemory((size_t) image->rows,
608 MagickMax(bytes_per_line,image->columns+256UL)*sizeof(*pixels));
609 if (pixels == (unsigned char *) NULL)
610 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
611 if ((dib_info.compression == BI_RGB) ||
612 (dib_info.compression == BI_BITFIELDS))
613 {
614 count=ReadBlob(image,length,pixels);
615 if (count != (ssize_t) (length))
616 ThrowReaderException(CorruptImageError,"InsufficientImageDataInFile");
617 }
618 else
619 {
620 /*
621 Convert run-length encoded raster pixels.
622 */
623 status=DecodeImage(image,dib_info.compression ? MagickTrue : MagickFalse,
624 pixels);
625 if (status == MagickFalse)
626 ThrowReaderException(CorruptImageError,"UnableToRunlengthDecodeImage");
627 }
628 /*
629 Initialize image structure.
630 */
631 image->units=PixelsPerCentimeterResolution;
632 image->x_resolution=(double) dib_info.x_pixels/100.0;
633 image->y_resolution=(double) dib_info.y_pixels/100.0;
634 /*
635 Convert DIB raster image to pixel packets.
636 */
637 switch (dib_info.bits_per_pixel)
638 {
639 case 1:
640 {
641 /*
642 Convert bitmap scanline.
643 */
644 for (y=(long) image->rows-1; y >= 0; y--)
645 {
646 p=pixels+(image->rows-y-1)*bytes_per_line;
647 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
648 if (q == (PixelPacket *) NULL)
649 break;
650 indexes=GetAuthenticIndexQueue(image);
651 for (x=0; x < ((long) image->columns-7); x+=8)
652 {
653 for (bit=0; bit < 8; bit++)
654 {
655 index=(IndexPacket) ((*p) & (0x80 >> bit) ? 0x01 : 0x00);
656 indexes[x+bit]=index;
657 *q++=image->colormap[(long) index];
658 }
659 p++;
660 }
661 if ((image->columns % 8) != 0)
662 {
663 for (bit=0; bit < (long) (image->columns % 8); bit++)
664 {
665 index=(IndexPacket) ((*p) & (0x80 >> bit) ? 0x01 : 0x00);
666 indexes[x+bit]=index;
667 *q++=image->colormap[(long) index];
668 }
669 p++;
670 }
671 if (SyncAuthenticPixels(image,exception) == MagickFalse)
672 break;
673 if (image->previous == (Image *) NULL)
674 {
675 status=SetImageProgress(image,LoadImageTag,image->rows-y-1,
676 image->rows);
677 if (status == MagickFalse)
678 break;
679 }
680 }
681 break;
682 }
683 case 4:
684 {
685 /*
686 Convert PseudoColor scanline.
687 */
688 for (y=(long) image->rows-1; y >= 0; y--)
689 {
690 p=pixels+(image->rows-y-1)*bytes_per_line;
691 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
692 if (q == (PixelPacket *) NULL)
693 break;
694 indexes=GetAuthenticIndexQueue(image);
695 for (x=0; x < ((long) image->columns-1); x+=2)
696 {
697 index=ConstrainColormapIndex(image,(*p >> 4) & 0xf);
698 indexes[x]=index;
699 *q++=image->colormap[(long) index];
700 index=ConstrainColormapIndex(image,*p & 0xf);
701 indexes[x+1]=index;
702 *q++=image->colormap[(long) index];
703 p++;
704 }
705 if ((image->columns % 2) != 0)
706 {
707 index=ConstrainColormapIndex(image,(*p >> 4) & 0xf);
708 indexes[x]=index;
709 *q++=image->colormap[(long) index];
710 p++;
711 }
712 if (SyncAuthenticPixels(image,exception) == MagickFalse)
713 break;
714 if (image->previous == (Image *) NULL)
715 {
716 status=SetImageProgress(image,LoadImageTag,image->rows-y-1,
717 image->rows);
718 if (status == MagickFalse)
719 break;
720 }
721 }
722 break;
723 }
724 case 8:
725 {
726 /*
727 Convert PseudoColor scanline.
728 */
729 if ((dib_info.compression == BI_RLE8) ||
730 (dib_info.compression == BI_RLE4))
731 bytes_per_line=image->columns;
732 for (y=(long) image->rows-1; y >= 0; y--)
733 {
734 p=pixels+(image->rows-y-1)*bytes_per_line;
735 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
736 if (q == (PixelPacket *) NULL)
737 break;
738 indexes=GetAuthenticIndexQueue(image);
739 for (x=0; x < (long) image->columns; x++)
740 {
741 index=ConstrainColormapIndex(image,*p);
742 indexes[x]=index;
743 *q=image->colormap[(long) index];
744 p++;
745 q++;
746 }
747 if (SyncAuthenticPixels(image,exception) == MagickFalse)
748 break;
749 if (image->previous == (Image *) NULL)
750 {
751 status=SetImageProgress(image,LoadImageTag,image->rows-y-1,
752 image->rows);
753 if (status == MagickFalse)
754 break;
755 }
756 }
757 break;
758 }
759 case 16:
760 {
761 unsigned short
762 word;
763
764 /*
765 Convert PseudoColor scanline.
766 */
767 image->storage_class=DirectClass;
768 if (dib_info.compression == BI_RLE8)
769 bytes_per_line=2*image->columns;
770 for (y=(long) image->rows-1; y >= 0; y--)
771 {
772 p=pixels+(image->rows-y-1)*bytes_per_line;
773 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
774 if (q == (PixelPacket *) NULL)
775 break;
776 for (x=0; x < (long) image->columns; x++)
777 {
778 word=(*p++);
779 word|=(*p++ << 8);
780 if (dib_info.red_mask == 0)
781 {
782 q->red=ScaleCharToQuantum(ScaleColor5to8((unsigned char)
783 ((word >> 10) & 0x1f)));
784 q->green=ScaleCharToQuantum(ScaleColor5to8((unsigned char)
785 ((word >> 5) & 0x1f)));
786 q->blue=ScaleCharToQuantum(ScaleColor5to8((unsigned char)
787 (word & 0x1f)));
788 }
789 else
790 {
791 q->red=ScaleCharToQuantum(ScaleColor5to8((unsigned char)
792 ((word >> 11) & 0x1f)));
793 q->green=ScaleCharToQuantum(ScaleColor6to8((unsigned char)
794 ((word >> 5) & 0x3f)));
795 q->blue=ScaleCharToQuantum(ScaleColor5to8((unsigned char)
796 (word & 0x1f)));
797 }
798 q++;
799 }
800 if (SyncAuthenticPixels(image,exception) == MagickFalse)
801 break;
802 if (image->previous == (Image *) NULL)
803 {
804 status=SetImageProgress(image,LoadImageTag,image->rows-y-1,
805 image->rows);
806 if (status == MagickFalse)
807 break;
808 }
809 }
810 break;
811 }
812 case 24:
813 case 32:
814 {
815 /*
816 Convert DirectColor scanline.
817 */
818 for (y=(long) image->rows-1; y >= 0; y--)
819 {
820 p=pixels+(image->rows-y-1)*bytes_per_line;
821 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
822 if (q == (PixelPacket *) NULL)
823 break;
824 for (x=0; x < (long) image->columns; x++)
825 {
826 q->blue=ScaleCharToQuantum(*p++);
827 q->green=ScaleCharToQuantum(*p++);
828 q->red=ScaleCharToQuantum(*p++);
829 if (image->matte != MagickFalse)
830 q->opacity=ScaleCharToQuantum(*p++);
831 q++;
832 }
833 if (SyncAuthenticPixels(image,exception) == MagickFalse)
834 break;
835 if (image->previous == (Image *) NULL)
836 {
837 status=SetImageProgress(image,LoadImageTag,image->rows-y-1,
838 image->rows);
839 if (status == MagickFalse)
840 break;
841 }
842 }
843 break;
844 }
845 default:
846 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
847 }
848 pixels=(unsigned char *) RelinquishMagickMemory(pixels);
849 if (EOFBlob(image) != MagickFalse)
850 ThrowFileException(exception,CorruptImageError,"UnexpectedEndOfFile",
851 image->filename);
852 if (dib_info.height < 0)
853 {
854 Image
855 *flipped_image;
856
857 /*
858 Correct image orientation.
859 */
860 flipped_image=FlipImage(image,exception);
861 if (flipped_image == (Image *) NULL)
862 {
863 image=DestroyImageList(image);
864 return((Image *) NULL);
865 }
866 DuplicateBlob(flipped_image,image);
867 image=DestroyImage(image);
868 image=flipped_image;
869 }
870 (void) CloseBlob(image);
871 return(GetFirstImageInList(image));
872}
873
874/*
875%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
876% %
877% %
878% %
879% R e g i s t e r D I B I m a g e %
880% %
881% %
882% %
883%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
884%
885% RegisterDIBImage() adds attributes for the DIB image format to
886% the list of supported formats. The attributes include the image format
887% tag, a method to read and/or write the format, whether the format
888% supports the saving of more than one frame to the same file or blob,
889% whether the format supports native in-memory I/O, and a brief
890% description of the format.
891%
892% The format of the RegisterDIBImage method is:
893%
894% unsigned long RegisterDIBImage(void)
895%
896*/
897ModuleExport unsigned long RegisterDIBImage(void)
898{
899 MagickInfo
900 *entry;
901
902 entry=SetMagickInfo("DIB");
903 entry->decoder=(DecodeImageHandler *) ReadDIBImage;
904 entry->encoder=(EncodeImageHandler *) WriteDIBImage;
905 entry->magick=(IsImageFormatHandler *) IsDIB;
906 entry->adjoin=MagickFalse;
907 entry->stealth=MagickTrue;
908 entry->description=ConstantString(
909 "Microsoft Windows 3.X Packed Device-Independent Bitmap");
910 entry->module=ConstantString("DIB");
911 (void) RegisterMagickInfo(entry);
912 return(MagickImageCoderSignature);
913}
914
915/*
916%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
917% %
918% %
919% %
920% U n r e g i s t e r D I B I m a g e %
921% %
922% %
923% %
924%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
925%
926% UnregisterDIBImage() removes format registrations made by the
927% DIB module from the list of supported formats.
928%
929% The format of the UnregisterDIBImage method is:
930%
931% UnregisterDIBImage(void)
932%
933*/
934ModuleExport void UnregisterDIBImage(void)
935{
936 (void) UnregisterMagickInfo("DIB");
937}
938
939/*
940%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
941% %
942% %
943% %
944% W r i t e D I B I m a g e %
945% %
946% %
947% %
948%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
949%
950% WriteDIBImage() writes an image in Microsoft Windows bitmap encoded
951% image format.
952%
953% The format of the WriteDIBImage method is:
954%
955% MagickBooleanType WriteDIBImage(const ImageInfo *image_info,Image *image)
956%
957% A description of each parameter follows.
958%
959% o image_info: the image info.
960%
961% o image: The image.
962%
963*/
964static MagickBooleanType WriteDIBImage(const ImageInfo *image_info,Image *image)
965{
966 DIBInfo
967 dib_info;
968
969 long
970 y;
971
972 MagickBooleanType
973 status;
974
975 register const IndexPacket
976 *indexes;
977
978 register const PixelPacket
979 *p;
980
981 register long
982 i,
983 x;
984
985 register unsigned char
986 *q;
987
988 unsigned char
989 *dib_data,
990 *pixels;
991
992 unsigned long
993 bytes_per_line;
994
995 /*
996 Open output image file.
997 */
998 assert(image_info != (const ImageInfo *) NULL);
999 assert(image_info->signature == MagickSignature);
1000 assert(image != (Image *) NULL);
1001 assert(image->signature == MagickSignature);
1002 if (image->debug != MagickFalse)
1003 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1004 status=OpenBlob(image_info,image,WriteBinaryBlobMode,&image->exception);
1005 if (status == MagickFalse)
1006 return(status);
1007 /*
1008 Initialize DIB raster file header.
1009 */
1010 if (image->colorspace != RGBColorspace)
1011 (void) TransformImageColorspace(image,RGBColorspace);
1012 if (image->storage_class == DirectClass)
1013 {
1014 /*
1015 Full color DIB raster.
1016 */
1017 dib_info.number_colors=0;
1018 dib_info.bits_per_pixel=(unsigned short) (image->matte ? 32 : 24);
1019 }
1020 else
1021 {
1022 /*
1023 Colormapped DIB raster.
1024 */
1025 dib_info.bits_per_pixel=8;
1026 if (image_info->depth > 8)
1027 dib_info.bits_per_pixel=16;
1028 if (IsMonochromeImage(image,&image->exception) != MagickFalse)
1029 dib_info.bits_per_pixel=1;
1030 dib_info.number_colors=(dib_info.bits_per_pixel == 16) ? 0 :
1031 (1UL << dib_info.bits_per_pixel);
1032 }
1033 bytes_per_line=4*((image->columns*dib_info.bits_per_pixel+31)/32);
1034 dib_info.size=40;
1035 dib_info.width=(long) image->columns;
1036 dib_info.height=(long) image->rows;
1037 dib_info.planes=1;
1038 dib_info.compression=(unsigned long) (dib_info.bits_per_pixel == 16 ?
1039 BI_BITFIELDS : BI_RGB);
1040 dib_info.image_size=bytes_per_line*image->rows;
1041 dib_info.x_pixels=75*39;
1042 dib_info.y_pixels=75*39;
1043 switch (image->units)
1044 {
1045 case UndefinedResolution:
1046 case PixelsPerInchResolution:
1047 {
1048 dib_info.x_pixels=(unsigned long) (100.0*image->x_resolution/2.54);
1049 dib_info.y_pixels=(unsigned long) (100.0*image->y_resolution/2.54);
1050 break;
1051 }
1052 case PixelsPerCentimeterResolution:
1053 {
1054 dib_info.x_pixels=(unsigned long) (100.0*image->x_resolution);
1055 dib_info.y_pixels=(unsigned long) (100.0*image->y_resolution);
1056 break;
1057 }
1058 }
1059 dib_info.colors_important=dib_info.number_colors;
1060 /*
1061 Convert MIFF to DIB raster pixels.
1062 */
1063 pixels=(unsigned char *) AcquireQuantumMemory(dib_info.image_size,
1064 sizeof(*pixels));
1065 if (pixels == (unsigned char *) NULL)
1066 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
1067 (void) ResetMagickMemory(pixels,0,dib_info.image_size);
1068 switch (dib_info.bits_per_pixel)
1069 {
1070 case 1:
1071 {
1072 register unsigned char
1073 bit,
1074 byte;
1075
1076 /*
1077 Convert PseudoClass image to a DIB monochrome image.
1078 */
1079 for (y=0; y < (long) image->rows; y++)
1080 {
1081 p=GetVirtualPixels(image,0,y,image->columns,1,&image->exception);
1082 if (p == (const PixelPacket *) NULL)
1083 break;
1084 indexes=GetVirtualIndexQueue(image);
1085 q=pixels+(image->rows-y-1)*bytes_per_line;
1086 bit=0;
1087 byte=0;
1088 for (x=0; x < (long) image->columns; x++)
1089 {
1090 byte<<=1;
1091 byte|=indexes[x] != 0 ? 0x01 : 0x00;
1092 bit++;
1093 if (bit == 8)
1094 {
1095 *q++=byte;
1096 bit=0;
1097 byte=0;
1098 }
1099 p++;
1100 }
1101 if (bit != 0)
1102 {
1103 *q++=(unsigned char) (byte << (8-bit));
1104 x++;
1105 }
1106 for (x=(long) (image->columns+7)/8; x < (long) bytes_per_line; x++)
1107 *q++=0x00;
1108 status=SetImageProgress(image,SaveImageTag,y,image->rows);
1109 if (status == MagickFalse)
1110 break;
1111 }
1112 break;
1113 }
1114 case 8:
1115 {
1116 /*
1117 Convert PseudoClass packet to DIB pixel.
1118 */
1119 for (y=0; y < (long) image->rows; y++)
1120 {
1121 p=GetVirtualPixels(image,0,y,image->columns,1,&image->exception);
1122 if (p == (const PixelPacket *) NULL)
1123 break;
1124 indexes=GetVirtualIndexQueue(image);
1125 q=pixels+(image->rows-y-1)*bytes_per_line;
1126 for (x=0; x < (long) image->columns; x++)
1127 *q++=(unsigned char) indexes[x];
1128 for ( ; x < (long) bytes_per_line; x++)
1129 *q++=0x00;
1130 status=SetImageProgress(image,SaveImageTag,y,image->rows);
1131 if (status == MagickFalse)
1132 break;
1133 }
1134 break;
1135 }
1136 case 16:
1137 {
1138 unsigned short
1139 word;
1140 /*
1141 Convert PseudoClass packet to DIB pixel.
1142 */
1143 for (y=0; y < (long) image->rows; y++)
1144 {
1145 p=GetVirtualPixels(image,0,y,image->columns,1,&image->exception);
1146 if (p == (const PixelPacket *) NULL)
1147 break;
1148 q=pixels+(image->rows-y-1)*bytes_per_line;
1149 for (x=0; x < (long) image->columns; x++)
1150 {
1151 word=(unsigned short) ((ScaleColor8to5((unsigned char)
1152 ScaleQuantumToChar(p->red)) << 11) | (ScaleColor8to6((unsigned char)
1153 ScaleQuantumToChar(p->green)) << 5) | (ScaleColor8to5(
1154 (unsigned char) ScaleQuantumToChar((unsigned char) p->blue) << 0)));
1155 *q++=(unsigned char)(word & 0xff);
1156 *q++=(unsigned char)(word >> 8);
1157 p++;
1158 }
1159 for (x=2L*image->columns; x < (long) bytes_per_line; x++)
1160 *q++=0x00;
1161 status=SetImageProgress(image,SaveImageTag,y,image->rows);
1162 if (status == MagickFalse)
1163 break;
1164 }
1165 break;
1166 }
1167 case 24:
1168 case 32:
1169 {
1170 /*
1171 Convert DirectClass packet to DIB RGB pixel.
1172 */
1173 for (y=0; y < (long) image->rows; y++)
1174 {
1175 p=GetVirtualPixels(image,0,y,image->columns,1,&image->exception);
1176 if (p == (const PixelPacket *) NULL)
1177 break;
1178 q=pixels+(image->rows-y-1)*bytes_per_line;
1179 for (x=0; x < (long) image->columns; x++)
1180 {
1181 *q++=ScaleQuantumToChar(p->blue);
1182 *q++=ScaleQuantumToChar(p->green);
1183 *q++=ScaleQuantumToChar(p->red);
1184 if (image->matte != MagickFalse)
1185 *q++=ScaleQuantumToChar(p->opacity);
1186 p++;
1187 }
1188 if (dib_info.bits_per_pixel == 24)
1189 for (x=3L*image->columns; x < (long) bytes_per_line; x++)
1190 *q++=0x00;
1191 status=SetImageProgress(image,SaveImageTag,y,image->rows);
1192 if (status == MagickFalse)
1193 break;
1194 }
1195 break;
1196 }
1197 }
1198 if (dib_info.bits_per_pixel == 8)
1199 if (image_info->compression != NoCompression)
1200 {
1201 size_t
1202 length;
1203
1204 /*
1205 Convert run-length encoded raster pixels.
1206 */
1207 length=2UL*(bytes_per_line+2UL)+2UL;
1208 dib_data=(unsigned char *) AcquireQuantumMemory(length,
1209 (image->rows+2UL)*sizeof(*dib_data));
1210 if (pixels == (unsigned char *) NULL)
1211 {
1212 pixels=(unsigned char *) RelinquishMagickMemory(pixels);
1213 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
1214 }
1215 dib_info.image_size=(unsigned long) EncodeImage(image,bytes_per_line,
1216 pixels,dib_data);
1217 pixels=(unsigned char *) RelinquishMagickMemory(pixels);
1218 pixels=dib_data;
1219 dib_info.compression = BI_RLE8;
1220 }
1221 /*
1222 Write DIB header.
1223 */
1224 (void) WriteBlobLSBLong(image,dib_info.size);
1225 (void) WriteBlobLSBLong(image,(unsigned long) dib_info.width);
1226 (void) WriteBlobLSBLong(image,(unsigned short) dib_info.height);
1227 (void) WriteBlobLSBShort(image,(unsigned short) dib_info.planes);
1228 (void) WriteBlobLSBShort(image,dib_info.bits_per_pixel);
1229 (void) WriteBlobLSBLong(image,dib_info.compression);
1230 (void) WriteBlobLSBLong(image,dib_info.image_size);
1231 (void) WriteBlobLSBLong(image,dib_info.x_pixels);
1232 (void) WriteBlobLSBLong(image,dib_info.y_pixels);
1233 (void) WriteBlobLSBLong(image,dib_info.number_colors);
1234 (void) WriteBlobLSBLong(image,dib_info.colors_important);
1235 if (image->storage_class == PseudoClass)
1236 {
1237 if (dib_info.bits_per_pixel <= 8)
1238 {
1239 unsigned char
1240 *dib_colormap;
1241
1242 /*
1243 Dump colormap to file.
1244 */
1245 dib_colormap=(unsigned char *) AcquireQuantumMemory((size_t)
1246 (1UL << dib_info.bits_per_pixel),4*sizeof(dib_colormap));
1247 if (dib_colormap == (unsigned char *) NULL)
1248 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
1249 q=dib_colormap;
1250 for (i=0; i < (long) MagickMin(image->colors,dib_info.number_colors); i++)
1251 {
1252 *q++=ScaleQuantumToChar(image->colormap[i].blue);
1253 *q++=ScaleQuantumToChar(image->colormap[i].green);
1254 *q++=ScaleQuantumToChar(image->colormap[i].red);
1255 *q++=(Quantum) 0x0;
1256 }
1257 for ( ; i < (long) (1L << dib_info.bits_per_pixel); i++)
1258 {
1259 *q++=(Quantum) 0x0;
1260 *q++=(Quantum) 0x0;
1261 *q++=(Quantum) 0x0;
1262 *q++=(Quantum) 0x0;
1263 }
1264 (void) WriteBlob(image,(size_t) (4*(1 << dib_info.bits_per_pixel)),
1265 dib_colormap);
1266 dib_colormap=(unsigned char *) RelinquishMagickMemory(dib_colormap);
1267 }
1268 else
1269 if ((dib_info.bits_per_pixel == 16) &&
1270 (dib_info.compression == BI_BITFIELDS))
1271 {
1272 (void) WriteBlobLSBLong(image,0xf800);
1273 (void) WriteBlobLSBLong(image,0x07e0);
1274 (void) WriteBlobLSBLong(image,0x001f);
1275 }
1276 }
1277 (void) WriteBlob(image,dib_info.image_size,pixels);
1278 pixels=(unsigned char *) RelinquishMagickMemory(pixels);
1279 (void) CloseBlob(image);
1280 return(MagickTrue);
1281}