blob: 085215e9b69e903a2d13307f554ddc222f45602a [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% %
cristy1454be72011-12-19 01:52:48 +000020% Copyright 1999-2012 ImageMagick Studio LLC, a non-profit organization %
cristy3ed852e2009-09-05 21:47:34 +000021% 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*/
cristy4c08aed2011-07-01 19:47:50 +000042#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"
cristy510d06a2011-07-06 23:43:54 +000052#include "MagickCore/colorspace-private.h"
cristy4c08aed2011-07-01 19:47:50 +000053#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"
cristy3ed852e2009-09-05 21:47:34 +000071
72/*
73 Typedef declarations.
74*/
75typedef struct _DIBInfo
76{
cristyf6fe0a12010-05-30 00:44:47 +000077 size_t
cristy3ed852e2009-09-05 21:47:34 +000078 size;
79
cristyf6fe0a12010-05-30 00:44:47 +000080 ssize_t
cristy3ed852e2009-09-05 21:47:34 +000081 width,
82 height;
83
84 unsigned short
85 planes,
86 bits_per_pixel;
87
cristyf6fe0a12010-05-30 00:44:47 +000088 size_t
cristy3ed852e2009-09-05 21:47:34 +000089 compression,
90 image_size,
91 x_pixels,
92 y_pixels,
93 number_colors,
94 red_mask,
95 green_mask,
96 blue_mask,
97 alpha_mask,
98 colors_important;
99
cristyf6fe0a12010-05-30 00:44:47 +0000100 ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000101 colorspace;
102
103 PointInfo
104 red_primary,
105 green_primary,
106 blue_primary,
107 gamma_scale;
108} DIBInfo;
109
110/*
111 Forward declarations.
112*/
113static MagickBooleanType
cristy1e178e72011-08-28 19:44:34 +0000114 WriteDIBImage(const ImageInfo *,Image *,ExceptionInfo *);
cristy3ed852e2009-09-05 21:47:34 +0000115
116/*
117%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
118% %
119% %
120% %
121% D e c o d e I m a g e %
122% %
123% %
124% %
125%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
126%
127% DecodeImage unpacks the packed image pixels into runlength-encoded
128% pixel packets.
129%
130% The format of the DecodeImage method is:
131%
132% MagickBooleanType DecodeImage(Image *image,
133% const MagickBooleanType compression,unsigned char *pixels)
134%
135% A description of each parameter follows:
136%
137% o image: the address of a structure of type Image.
138%
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.
141%
142% o pixels: The address of a byte (8 bits) array of pixel data created by
143% the decoding process.
144%
145*/
146
147static inline size_t MagickMin(const size_t x,const size_t y)
148{
149 if (x < y)
150 return(x);
151 return(y);
152}
153
154static MagickBooleanType DecodeImage(Image *image,
155 const MagickBooleanType compression,unsigned char *pixels)
156{
cristy0157aea2010-04-24 21:12:18 +0000157#if !defined(MAGICKCORE_WINDOWS_SUPPORT) || defined(__MINGW32__)
cristy3ed852e2009-09-05 21:47:34 +0000158#define BI_RGB 0
159#define BI_RLE8 1
160#define BI_RLE4 2
161#define BI_BITFIELDS 3
162#endif
163
164 int
165 count;
166
cristybb503372010-05-27 20:51:26 +0000167 ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000168 y;
169
cristybb503372010-05-27 20:51:26 +0000170 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000171 i,
172 x;
173
174 register unsigned char
175 *p,
176 *q;
177
178 unsigned char
179 byte;
180
181 assert(image != (Image *) NULL);
182 assert(image->signature == MagickSignature);
183 if (image->debug != MagickFalse)
184 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
185 assert(pixels != (unsigned char *) NULL);
186 (void) ResetMagickMemory(pixels,0,(size_t) image->columns*image->rows*
187 sizeof(*pixels));
188 byte=0;
189 x=0;
190 p=pixels;
191 q=pixels+(size_t) image->columns*image->rows;
cristybb503372010-05-27 20:51:26 +0000192 for (y=0; y < (ssize_t) image->rows; )
cristy3ed852e2009-09-05 21:47:34 +0000193 {
194 if ((p < pixels) || (p >= q))
195 break;
196 count=ReadBlobByte(image);
197 if (count == EOF)
198 break;
199 if (count != 0)
200 {
201 count=(int) MagickMin((size_t) count,(size_t) (q-p));
202 /*
203 Encoded mode.
204 */
205 byte=(unsigned char) ReadBlobByte(image);
206 if (compression == BI_RLE8)
207 {
208 for (i=0; i < count; i++)
209 *p++=(unsigned char) byte;
210 }
211 else
212 {
213 for (i=0; i < count; i++)
214 *p++=(unsigned char)
215 ((i & 0x01) != 0 ? (byte & 0x0f) : ((byte >> 4) & 0x0f));
216 }
217 x+=count;
218 }
219 else
220 {
221 /*
222 Escape mode.
223 */
224 count=ReadBlobByte(image);
225 if (count == 0x01)
226 return(MagickTrue);
227 switch (count)
228 {
229 case 0x00:
230 {
231 /*
232 End of line.
233 */
234 x=0;
235 y++;
236 p=pixels+y*image->columns;
237 break;
238 }
239 case 0x02:
240 {
241 /*
242 Delta mode.
243 */
244 x+=ReadBlobByte(image);
245 y+=ReadBlobByte(image);
246 p=pixels+y*image->columns+x;
247 break;
248 }
249 default:
250 {
251 /*
252 Absolute mode.
253 */
254 count=(int) MagickMin((size_t) count,(size_t) (q-p));
255 if (compression == BI_RLE8)
256 for (i=0; i < count; i++)
257 *p++=(unsigned char) ReadBlobByte(image);
258 else
259 for (i=0; i < count; i++)
260 {
261 if ((i & 0x01) == 0)
262 byte=(unsigned char) ReadBlobByte(image);
263 *p++=(unsigned char)
264 ((i & 0x01) != 0 ? (byte & 0x0f) : ((byte >> 4) & 0x0f));
265 }
266 x+=count;
267 /*
268 Read pad byte.
269 */
270 if (compression == BI_RLE8)
271 {
272 if ((count & 0x01) != 0)
273 (void) ReadBlobByte(image);
274 }
275 else
276 if (((count & 0x03) == 1) || ((count & 0x03) == 2))
277 (void) ReadBlobByte(image);
278 break;
279 }
280 }
281 }
282 if (SetImageProgress(image,LoadImageTag,y,image->rows) == MagickFalse)
283 break;
284 }
285 (void) ReadBlobByte(image); /* end of line */
286 (void) ReadBlobByte(image);
287 return(MagickTrue);
288}
289
290/*
291%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
292% %
293% %
294% %
295% E n c o d e I m a g e %
296% %
297% %
298% %
299%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
300%
301% EncodeImage compresses pixels using a runlength encoded format.
302%
303% The format of the EncodeImage method is:
304%
305% static MagickBooleanType EncodeImage(Image *image,
cristybb503372010-05-27 20:51:26 +0000306% const size_t bytes_per_line,const unsigned char *pixels,
cristy3ed852e2009-09-05 21:47:34 +0000307% unsigned char *compressed_pixels)
308%
309% A description of each parameter follows:
310%
311% o image: The image.
312%
313% o bytes_per_line: the number of bytes in a scanline of compressed pixels
314%
315% o pixels: The address of a byte (8 bits) array of pixel data created by
316% the compression process.
317%
318% o compressed_pixels: The address of a byte (8 bits) array of compressed
319% pixel data.
320%
321*/
cristybb503372010-05-27 20:51:26 +0000322static size_t EncodeImage(Image *image,const size_t bytes_per_line,
cristy3ed852e2009-09-05 21:47:34 +0000323 const unsigned char *pixels,unsigned char *compressed_pixels)
324{
cristybb503372010-05-27 20:51:26 +0000325 ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000326 y;
327
328 register const unsigned char
329 *p;
330
cristybb503372010-05-27 20:51:26 +0000331 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000332 i,
333 x;
334
335 register unsigned char
336 *q;
337
338 /*
339 Runlength encode pixels.
340 */
341 assert(image != (Image *) NULL);
342 assert(image->signature == MagickSignature);
343 if (image->debug != MagickFalse)
344 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
345 assert(pixels != (const unsigned char *) NULL);
346 assert(compressed_pixels != (unsigned char *) NULL);
347 p=pixels;
348 q=compressed_pixels;
349 i=0;
cristybb503372010-05-27 20:51:26 +0000350 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +0000351 {
cristybb503372010-05-27 20:51:26 +0000352 for (x=0; x < (ssize_t) bytes_per_line; x+=i)
cristy3ed852e2009-09-05 21:47:34 +0000353 {
354 /*
355 Determine runlength.
356 */
cristybb503372010-05-27 20:51:26 +0000357 for (i=1; ((x+i) < (ssize_t) bytes_per_line); i++)
cristy3ed852e2009-09-05 21:47:34 +0000358 if ((*(p+i) != *p) || (i == 255))
359 break;
360 *q++=(unsigned char) i;
361 *q++=(*p);
362 p+=i;
363 }
364 /*
365 End of line.
366 */
367 *q++=0x00;
368 *q++=0x00;
369 if (SetImageProgress(image,LoadImageTag,y,image->rows) == MagickFalse)
370 break;
371 }
372 /*
373 End of bitmap.
374 */
375 *q++=0;
376 *q++=0x01;
377 return((size_t) (q-compressed_pixels));
378}
379
380/*
381%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
382% %
383% %
384% %
385% I s D I B %
386% %
387% %
388% %
389%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
390%
391% IsDIB() returns MagickTrue if the image format type, identified by the
392% magick string, is DIB.
393%
394% The format of the IsDIB method is:
395%
396% MagickBooleanType IsDIB(const unsigned char *magick,const size_t length)
397%
398% A description of each parameter follows:
399%
400% o magick: compare image format pattern against these bytes.
401%
402% o length: Specifies the length of the magick string.
403%
404*/
405static MagickBooleanType IsDIB(const unsigned char *magick,const size_t length)
406{
407 if (length < 2)
408 return(MagickFalse);
409 if (memcmp(magick,"\050\000",2) == 0)
410 return(MagickTrue);
411 return(MagickFalse);
412}
413
414/*
415%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
416% %
417% %
418% %
419% R e a d D I B I m a g e %
420% %
421% %
422% %
423%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
424%
425% ReadDIBImage() reads a Microsoft Windows bitmap image file and
426% returns it. It allocates the memory necessary for the new Image structure
427% and returns a pointer to the new image.
428%
429% The format of the ReadDIBImage method is:
430%
431% image=ReadDIBImage(image_info)
432%
433% A description of each parameter follows:
434%
435% o image_info: the image info.
436%
437% o exception: return any errors or warnings in this structure.
438%
439*/
440
cristybb503372010-05-27 20:51:26 +0000441static inline ssize_t MagickAbsoluteValue(const ssize_t x)
cristy3ed852e2009-09-05 21:47:34 +0000442{
443 if (x < 0)
444 return(-x);
445 return(x);
446}
447
448static inline size_t MagickMax(const size_t x,const size_t y)
449{
450 if (x > y)
451 return(x);
452 return(y);
453}
454
455static Image *ReadDIBImage(const ImageInfo *image_info,ExceptionInfo *exception)
456{
457 DIBInfo
458 dib_info;
459
460 Image
461 *image;
462
cristy3ed852e2009-09-05 21:47:34 +0000463 MagickBooleanType
464 status;
465
cristy4c08aed2011-07-01 19:47:50 +0000466 Quantum
467 index;
cristy3ed852e2009-09-05 21:47:34 +0000468
cristybb503372010-05-27 20:51:26 +0000469 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000470 x;
471
cristy4c08aed2011-07-01 19:47:50 +0000472 register Quantum
cristy3ed852e2009-09-05 21:47:34 +0000473 *q;
474
cristybb503372010-05-27 20:51:26 +0000475 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000476 i;
477
478 register unsigned char
479 *p;
480
481 size_t
cristyddbc41b2011-04-24 14:27:48 +0000482 bytes_per_line,
cristy3ed852e2009-09-05 21:47:34 +0000483 length;
484
485 ssize_t
cristy4c08aed2011-07-01 19:47:50 +0000486 bit,
487 count,
488 y;
489
cristy3ed852e2009-09-05 21:47:34 +0000490
491 unsigned char
492 *pixels;
493
cristy3ed852e2009-09-05 21:47:34 +0000494 /*
495 Open image file.
496 */
497 assert(image_info != (const ImageInfo *) NULL);
498 assert(image_info->signature == MagickSignature);
499 if (image_info->debug != MagickFalse)
500 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
501 image_info->filename);
502 assert(exception != (ExceptionInfo *) NULL);
503 assert(exception->signature == MagickSignature);
cristy9950d572011-10-01 18:22:35 +0000504 image=AcquireImage(image_info,exception);
cristy3ed852e2009-09-05 21:47:34 +0000505 status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception);
506 if (status == MagickFalse)
507 {
508 image=DestroyImageList(image);
509 return((Image *) NULL);
510 }
511 /*
512 Determine if this a DIB file.
513 */
514 (void) ResetMagickMemory(&dib_info,0,sizeof(dib_info));
515 dib_info.size=ReadBlobLSBLong(image);
516 if (dib_info.size!=40)
517 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
518 /*
519 Microsoft Windows 3.X DIB image file.
520 */
521 dib_info.width=(short) ReadBlobLSBLong(image);
522 dib_info.height=(short) ReadBlobLSBLong(image);
523 dib_info.planes=ReadBlobLSBShort(image);
524 dib_info.bits_per_pixel=ReadBlobLSBShort(image);
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.compression == BI_BITFIELDS) &&
532 ((dib_info.bits_per_pixel == 16) || (dib_info.bits_per_pixel == 32)))
533 {
534 dib_info.red_mask=ReadBlobLSBLong(image);
535 dib_info.green_mask=ReadBlobLSBLong(image);
536 dib_info.blue_mask=ReadBlobLSBLong(image);
537 }
538 image->matte=dib_info.bits_per_pixel == 32 ? MagickTrue : MagickFalse;
cristybb503372010-05-27 20:51:26 +0000539 image->columns=(size_t) MagickAbsoluteValue(dib_info.width);
540 image->rows=(size_t) MagickAbsoluteValue(dib_info.height);
cristy3ed852e2009-09-05 21:47:34 +0000541 image->depth=8;
542 if ((dib_info.number_colors != 0) || (dib_info.bits_per_pixel < 16))
543 {
cristyeaedf062010-05-29 22:36:02 +0000544 size_t
545 one;
546
cristy3ed852e2009-09-05 21:47:34 +0000547 image->storage_class=PseudoClass;
548 image->colors=dib_info.number_colors;
cristyeaedf062010-05-29 22:36:02 +0000549 one=1;
cristy3ed852e2009-09-05 21:47:34 +0000550 if (image->colors == 0)
cristyeaedf062010-05-29 22:36:02 +0000551 image->colors=one << dib_info.bits_per_pixel;
cristy3ed852e2009-09-05 21:47:34 +0000552 }
553 if (image_info->size)
554 {
555 RectangleInfo
556 geometry;
557
558 MagickStatusType
559 flags;
560
561 flags=ParseAbsoluteGeometry(image_info->size,&geometry);
562 if (flags & WidthValue)
563 if ((geometry.width != 0) && (geometry.width < image->columns))
564 image->columns=geometry.width;
565 if (flags & HeightValue)
566 if ((geometry.height != 0) && (geometry.height < image->rows))
567 image->rows=geometry.height;
568 }
569 if (image->storage_class == PseudoClass)
570 {
571 size_t
572 length,
573 packet_size;
574
575 unsigned char
576 *dib_colormap;
577
578 /*
579 Read DIB raster colormap.
580 */
cristy018f07f2011-09-04 21:15:19 +0000581 if (AcquireImageColormap(image,image->colors,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +0000582 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
583 length=(size_t) image->colors;
584 dib_colormap=(unsigned char *) AcquireQuantumMemory(length,
585 4*sizeof(*dib_colormap));
586 if (dib_colormap == (unsigned char *) NULL)
587 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
588 packet_size=4;
589 count=ReadBlob(image,packet_size*image->colors,dib_colormap);
590 if (count != (ssize_t) (packet_size*image->colors))
591 ThrowReaderException(CorruptImageError,"InsufficientImageDataInFile");
592 p=dib_colormap;
cristybb503372010-05-27 20:51:26 +0000593 for (i=0; i < (ssize_t) image->colors; i++)
cristy3ed852e2009-09-05 21:47:34 +0000594 {
595 image->colormap[i].blue=ScaleCharToQuantum(*p++);
596 image->colormap[i].green=ScaleCharToQuantum(*p++);
597 image->colormap[i].red=ScaleCharToQuantum(*p++);
598 if (packet_size == 4)
599 p++;
600 }
601 dib_colormap=(unsigned char *) RelinquishMagickMemory(dib_colormap);
602 }
603 /*
604 Read image data.
605 */
606 if (dib_info.compression == BI_RLE4)
607 dib_info.bits_per_pixel<<=1;
608 bytes_per_line=4*((image->columns*dib_info.bits_per_pixel+31)/32);
609 length=bytes_per_line*image->rows;
610 pixels=(unsigned char *) AcquireQuantumMemory((size_t) image->rows,
611 MagickMax(bytes_per_line,image->columns+256UL)*sizeof(*pixels));
612 if (pixels == (unsigned char *) NULL)
613 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
614 if ((dib_info.compression == BI_RGB) ||
615 (dib_info.compression == BI_BITFIELDS))
616 {
617 count=ReadBlob(image,length,pixels);
618 if (count != (ssize_t) (length))
619 ThrowReaderException(CorruptImageError,"InsufficientImageDataInFile");
620 }
621 else
622 {
623 /*
624 Convert run-length encoded raster pixels.
625 */
626 status=DecodeImage(image,dib_info.compression ? MagickTrue : MagickFalse,
627 pixels);
628 if (status == MagickFalse)
629 ThrowReaderException(CorruptImageError,"UnableToRunlengthDecodeImage");
630 }
631 /*
632 Initialize image structure.
633 */
634 image->units=PixelsPerCentimeterResolution;
cristy2a11bef2011-10-28 18:33:11 +0000635 image->resolution.x=(double) dib_info.x_pixels/100.0;
636 image->resolution.y=(double) dib_info.y_pixels/100.0;
cristy3ed852e2009-09-05 21:47:34 +0000637 /*
638 Convert DIB raster image to pixel packets.
639 */
640 switch (dib_info.bits_per_pixel)
641 {
642 case 1:
643 {
644 /*
645 Convert bitmap scanline.
646 */
cristybb503372010-05-27 20:51:26 +0000647 for (y=(ssize_t) image->rows-1; y >= 0; y--)
cristy3ed852e2009-09-05 21:47:34 +0000648 {
649 p=pixels+(image->rows-y-1)*bytes_per_line;
650 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
cristyacd2ed22011-08-30 01:44:23 +0000651 if (q == (Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +0000652 break;
cristybb503372010-05-27 20:51:26 +0000653 for (x=0; x < ((ssize_t) image->columns-7); x+=8)
cristy3ed852e2009-09-05 21:47:34 +0000654 {
655 for (bit=0; bit < 8; bit++)
656 {
cristy4c08aed2011-07-01 19:47:50 +0000657 index=(Quantum) ((*p) & (0x80 >> bit) ? 0x01 : 0x00);
658 SetPixelIndex(image,index,q);
cristyed231572011-07-14 02:18:59 +0000659 q+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +0000660 }
661 p++;
662 }
663 if ((image->columns % 8) != 0)
664 {
cristybb503372010-05-27 20:51:26 +0000665 for (bit=0; bit < (ssize_t) (image->columns % 8); bit++)
cristy3ed852e2009-09-05 21:47:34 +0000666 {
cristy4c08aed2011-07-01 19:47:50 +0000667 index=(Quantum) ((*p) & (0x80 >> bit) ? 0x01 : 0x00);
668 SetPixelIndex(image,index,q);
cristyed231572011-07-14 02:18:59 +0000669 q+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +0000670 }
671 p++;
672 }
673 if (SyncAuthenticPixels(image,exception) == MagickFalse)
674 break;
675 if (image->previous == (Image *) NULL)
676 {
677 status=SetImageProgress(image,LoadImageTag,image->rows-y-1,
678 image->rows);
679 if (status == MagickFalse)
680 break;
681 }
682 }
cristyea1a8aa2011-10-20 13:24:06 +0000683 (void) SyncImage(image,exception);
cristy3ed852e2009-09-05 21:47:34 +0000684 break;
685 }
686 case 4:
687 {
688 /*
689 Convert PseudoColor scanline.
690 */
cristybb503372010-05-27 20:51:26 +0000691 for (y=(ssize_t) image->rows-1; y >= 0; y--)
cristy3ed852e2009-09-05 21:47:34 +0000692 {
693 p=pixels+(image->rows-y-1)*bytes_per_line;
694 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
cristyacd2ed22011-08-30 01:44:23 +0000695 if (q == (Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +0000696 break;
cristybb503372010-05-27 20:51:26 +0000697 for (x=0; x < ((ssize_t) image->columns-1); x+=2)
cristy3ed852e2009-09-05 21:47:34 +0000698 {
cristyc82a27b2011-10-21 01:07:16 +0000699 index=ConstrainColormapIndex(image,(*p >> 4) & 0xf,exception);
cristy4c08aed2011-07-01 19:47:50 +0000700 SetPixelIndex(image,index,q);
cristyed231572011-07-14 02:18:59 +0000701 q+=GetPixelChannels(image);
cristyc82a27b2011-10-21 01:07:16 +0000702 index=ConstrainColormapIndex(image,*p & 0xf,exception);
cristy4c08aed2011-07-01 19:47:50 +0000703 SetPixelIndex(image,index,q);
cristy3ed852e2009-09-05 21:47:34 +0000704 p++;
cristyed231572011-07-14 02:18:59 +0000705 q+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +0000706 }
707 if ((image->columns % 2) != 0)
708 {
cristyc82a27b2011-10-21 01:07:16 +0000709 index=ConstrainColormapIndex(image,(*p >> 4) & 0xf,exception);
cristy4c08aed2011-07-01 19:47:50 +0000710 SetPixelIndex(image,index,q);
cristyed231572011-07-14 02:18:59 +0000711 q+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +0000712 p++;
713 }
714 if (SyncAuthenticPixels(image,exception) == MagickFalse)
715 break;
716 if (image->previous == (Image *) NULL)
717 {
718 status=SetImageProgress(image,LoadImageTag,image->rows-y-1,
719 image->rows);
720 if (status == MagickFalse)
721 break;
722 }
723 }
cristyea1a8aa2011-10-20 13:24:06 +0000724 (void) SyncImage(image,exception);
cristy3ed852e2009-09-05 21:47:34 +0000725 break;
726 }
727 case 8:
728 {
729 /*
730 Convert PseudoColor scanline.
731 */
732 if ((dib_info.compression == BI_RLE8) ||
733 (dib_info.compression == BI_RLE4))
734 bytes_per_line=image->columns;
cristybb503372010-05-27 20:51:26 +0000735 for (y=(ssize_t) image->rows-1; y >= 0; y--)
cristy3ed852e2009-09-05 21:47:34 +0000736 {
737 p=pixels+(image->rows-y-1)*bytes_per_line;
738 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
cristyacd2ed22011-08-30 01:44:23 +0000739 if (q == (Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +0000740 break;
cristybb503372010-05-27 20:51:26 +0000741 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +0000742 {
cristyc82a27b2011-10-21 01:07:16 +0000743 index=ConstrainColormapIndex(image,*p,exception);
cristy4c08aed2011-07-01 19:47:50 +0000744 SetPixelIndex(image,index,q);
cristy3ed852e2009-09-05 21:47:34 +0000745 p++;
cristyed231572011-07-14 02:18:59 +0000746 q+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +0000747 }
748 if (SyncAuthenticPixels(image,exception) == MagickFalse)
749 break;
750 if (image->previous == (Image *) NULL)
751 {
752 status=SetImageProgress(image,LoadImageTag,image->rows-y-1,
753 image->rows);
754 if (status == MagickFalse)
755 break;
756 }
757 }
cristyea1a8aa2011-10-20 13:24:06 +0000758 (void) SyncImage(image,exception);
cristy3ed852e2009-09-05 21:47:34 +0000759 break;
760 }
761 case 16:
762 {
763 unsigned short
764 word;
765
766 /*
767 Convert PseudoColor scanline.
768 */
769 image->storage_class=DirectClass;
770 if (dib_info.compression == BI_RLE8)
771 bytes_per_line=2*image->columns;
cristybb503372010-05-27 20:51:26 +0000772 for (y=(ssize_t) image->rows-1; y >= 0; y--)
cristy3ed852e2009-09-05 21:47:34 +0000773 {
774 p=pixels+(image->rows-y-1)*bytes_per_line;
775 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
cristyacd2ed22011-08-30 01:44:23 +0000776 if (q == (Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +0000777 break;
cristybb503372010-05-27 20:51:26 +0000778 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +0000779 {
780 word=(*p++);
781 word|=(*p++ << 8);
782 if (dib_info.red_mask == 0)
783 {
cristy4c08aed2011-07-01 19:47:50 +0000784 SetPixelRed(image,ScaleCharToQuantum(ScaleColor5to8(
785 (unsigned char) ((word >> 10) & 0x1f))),q);
786 SetPixelGreen(image,ScaleCharToQuantum(ScaleColor5to8(
787 (unsigned char) ((word >> 5) & 0x1f))),q);
788 SetPixelBlue(image,ScaleCharToQuantum(ScaleColor5to8(
789 (unsigned char) (word & 0x1f))),q);
cristy3ed852e2009-09-05 21:47:34 +0000790 }
791 else
792 {
cristy4c08aed2011-07-01 19:47:50 +0000793 SetPixelRed(image,ScaleCharToQuantum(ScaleColor5to8(
794 (unsigned char) ((word >> 11) & 0x1f))),q);
795 SetPixelGreen(image,ScaleCharToQuantum(ScaleColor6to8(
796 (unsigned char) ((word >> 5) & 0x3f))),q);
797 SetPixelBlue(image,ScaleCharToQuantum(ScaleColor5to8(
798 (unsigned char) (word & 0x1f))),q);
cristy3ed852e2009-09-05 21:47:34 +0000799 }
cristyed231572011-07-14 02:18:59 +0000800 q+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +0000801 }
802 if (SyncAuthenticPixels(image,exception) == MagickFalse)
803 break;
804 if (image->previous == (Image *) NULL)
805 {
806 status=SetImageProgress(image,LoadImageTag,image->rows-y-1,
807 image->rows);
808 if (status == MagickFalse)
809 break;
810 }
811 }
812 break;
813 }
814 case 24:
815 case 32:
816 {
817 /*
818 Convert DirectColor scanline.
819 */
cristybb503372010-05-27 20:51:26 +0000820 for (y=(ssize_t) image->rows-1; y >= 0; y--)
cristy3ed852e2009-09-05 21:47:34 +0000821 {
822 p=pixels+(image->rows-y-1)*bytes_per_line;
823 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
cristyacd2ed22011-08-30 01:44:23 +0000824 if (q == (Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +0000825 break;
cristybb503372010-05-27 20:51:26 +0000826 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +0000827 {
cristy4c08aed2011-07-01 19:47:50 +0000828 SetPixelBlue(image,ScaleCharToQuantum(*p++),q);
829 SetPixelGreen(image,ScaleCharToQuantum(*p++),q);
830 SetPixelRed(image,ScaleCharToQuantum(*p++),q);
cristy3ed852e2009-09-05 21:47:34 +0000831 if (image->matte != MagickFalse)
cristy4c08aed2011-07-01 19:47:50 +0000832 SetPixelAlpha(image,ScaleCharToQuantum(*p++),q);
cristyed231572011-07-14 02:18:59 +0000833 q+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +0000834 }
835 if (SyncAuthenticPixels(image,exception) == MagickFalse)
836 break;
837 if (image->previous == (Image *) NULL)
838 {
839 status=SetImageProgress(image,LoadImageTag,image->rows-y-1,
840 image->rows);
841 if (status == MagickFalse)
842 break;
843 }
844 }
845 break;
846 }
847 default:
848 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
849 }
850 pixels=(unsigned char *) RelinquishMagickMemory(pixels);
851 if (EOFBlob(image) != MagickFalse)
852 ThrowFileException(exception,CorruptImageError,"UnexpectedEndOfFile",
853 image->filename);
854 if (dib_info.height < 0)
855 {
856 Image
857 *flipped_image;
858
859 /*
860 Correct image orientation.
861 */
862 flipped_image=FlipImage(image,exception);
cristybbfd4cd2010-04-13 21:54:39 +0000863 if (flipped_image != (Image *) NULL)
cristy3ed852e2009-09-05 21:47:34 +0000864 {
cristybbfd4cd2010-04-13 21:54:39 +0000865 DuplicateBlob(flipped_image,image);
866 image=DestroyImage(image);
867 image=flipped_image;
cristy3ed852e2009-09-05 21:47:34 +0000868 }
cristy3ed852e2009-09-05 21:47:34 +0000869 }
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%
cristybb503372010-05-27 20:51:26 +0000894% size_t RegisterDIBImage(void)
cristy3ed852e2009-09-05 21:47:34 +0000895%
896*/
cristybb503372010-05-27 20:51:26 +0000897ModuleExport size_t RegisterDIBImage(void)
cristy3ed852e2009-09-05 21:47:34 +0000898{
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%
cristy1e178e72011-08-28 19:44:34 +0000955% MagickBooleanType WriteDIBImage(const ImageInfo *image_info,
956% Image *image,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000957%
958% A description of each parameter follows.
959%
960% o image_info: the image info.
961%
962% o image: The image.
963%
cristy1e178e72011-08-28 19:44:34 +0000964% o exception: return any errors or warnings in this structure.
965%
cristy3ed852e2009-09-05 21:47:34 +0000966*/
cristy1e178e72011-08-28 19:44:34 +0000967static MagickBooleanType WriteDIBImage(const ImageInfo *image_info,Image *image,
968 ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000969{
970 DIBInfo
971 dib_info;
972
cristy3ed852e2009-09-05 21:47:34 +0000973 MagickBooleanType
974 status;
975
cristy4c08aed2011-07-01 19:47:50 +0000976 register const Quantum
cristy3ed852e2009-09-05 21:47:34 +0000977 *p;
978
cristybb503372010-05-27 20:51:26 +0000979 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000980 i,
981 x;
982
983 register unsigned char
984 *q;
985
cristyddbc41b2011-04-24 14:27:48 +0000986 size_t
987 bytes_per_line;
988
989 ssize_t
990 y;
991
cristy3ed852e2009-09-05 21:47:34 +0000992 unsigned char
993 *dib_data,
994 *pixels;
995
cristy3ed852e2009-09-05 21:47:34 +0000996 /*
997 Open output image file.
998 */
999 assert(image_info != (const ImageInfo *) NULL);
1000 assert(image_info->signature == MagickSignature);
1001 assert(image != (Image *) NULL);
1002 assert(image->signature == MagickSignature);
1003 if (image->debug != MagickFalse)
1004 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
cristy3a37efd2011-08-28 20:31:03 +00001005 assert(exception != (ExceptionInfo *) NULL);
1006 assert(exception->signature == MagickSignature);
cristy1e178e72011-08-28 19:44:34 +00001007 status=OpenBlob(image_info,image,WriteBinaryBlobMode,exception);
cristy3ed852e2009-09-05 21:47:34 +00001008 if (status == MagickFalse)
1009 return(status);
1010 /*
1011 Initialize DIB raster file header.
1012 */
cristy510d06a2011-07-06 23:43:54 +00001013 if (IsRGBColorspace(image->colorspace) == MagickFalse)
cristye941a752011-10-15 01:52:48 +00001014 (void) TransformImageColorspace(image,RGBColorspace,exception);
cristy3ed852e2009-09-05 21:47:34 +00001015 if (image->storage_class == DirectClass)
1016 {
1017 /*
1018 Full color DIB raster.
1019 */
1020 dib_info.number_colors=0;
1021 dib_info.bits_per_pixel=(unsigned short) (image->matte ? 32 : 24);
1022 }
1023 else
1024 {
1025 /*
1026 Colormapped DIB raster.
1027 */
1028 dib_info.bits_per_pixel=8;
1029 if (image_info->depth > 8)
1030 dib_info.bits_per_pixel=16;
cristy1e178e72011-08-28 19:44:34 +00001031 if (IsImageMonochrome(image,exception) != MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +00001032 dib_info.bits_per_pixel=1;
1033 dib_info.number_colors=(dib_info.bits_per_pixel == 16) ? 0 :
1034 (1UL << dib_info.bits_per_pixel);
1035 }
1036 bytes_per_line=4*((image->columns*dib_info.bits_per_pixel+31)/32);
1037 dib_info.size=40;
cristybb503372010-05-27 20:51:26 +00001038 dib_info.width=(ssize_t) image->columns;
1039 dib_info.height=(ssize_t) image->rows;
cristy3ed852e2009-09-05 21:47:34 +00001040 dib_info.planes=1;
cristybb503372010-05-27 20:51:26 +00001041 dib_info.compression=(size_t) (dib_info.bits_per_pixel == 16 ?
cristy3ed852e2009-09-05 21:47:34 +00001042 BI_BITFIELDS : BI_RGB);
1043 dib_info.image_size=bytes_per_line*image->rows;
1044 dib_info.x_pixels=75*39;
1045 dib_info.y_pixels=75*39;
1046 switch (image->units)
1047 {
1048 case UndefinedResolution:
1049 case PixelsPerInchResolution:
1050 {
cristy2a11bef2011-10-28 18:33:11 +00001051 dib_info.x_pixels=(size_t) (100.0*image->resolution.x/2.54);
1052 dib_info.y_pixels=(size_t) (100.0*image->resolution.y/2.54);
cristy3ed852e2009-09-05 21:47:34 +00001053 break;
1054 }
1055 case PixelsPerCentimeterResolution:
1056 {
cristy2a11bef2011-10-28 18:33:11 +00001057 dib_info.x_pixels=(size_t) (100.0*image->resolution.x);
1058 dib_info.y_pixels=(size_t) (100.0*image->resolution.y);
cristy3ed852e2009-09-05 21:47:34 +00001059 break;
1060 }
1061 }
1062 dib_info.colors_important=dib_info.number_colors;
1063 /*
1064 Convert MIFF to DIB raster pixels.
1065 */
1066 pixels=(unsigned char *) AcquireQuantumMemory(dib_info.image_size,
1067 sizeof(*pixels));
1068 if (pixels == (unsigned char *) NULL)
1069 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
1070 (void) ResetMagickMemory(pixels,0,dib_info.image_size);
1071 switch (dib_info.bits_per_pixel)
1072 {
1073 case 1:
1074 {
1075 register unsigned char
1076 bit,
1077 byte;
1078
1079 /*
1080 Convert PseudoClass image to a DIB monochrome image.
1081 */
cristybb503372010-05-27 20:51:26 +00001082 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00001083 {
cristy1e178e72011-08-28 19:44:34 +00001084 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
cristy4c08aed2011-07-01 19:47:50 +00001085 if (p == (const Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00001086 break;
cristy3ed852e2009-09-05 21:47:34 +00001087 q=pixels+(image->rows-y-1)*bytes_per_line;
1088 bit=0;
1089 byte=0;
cristybb503372010-05-27 20:51:26 +00001090 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00001091 {
1092 byte<<=1;
cristy4c08aed2011-07-01 19:47:50 +00001093 byte|=GetPixelIndex(image,p) != 0 ? 0x01 : 0x00;
cristy3ed852e2009-09-05 21:47:34 +00001094 bit++;
1095 if (bit == 8)
1096 {
1097 *q++=byte;
1098 bit=0;
1099 byte=0;
1100 }
cristyed231572011-07-14 02:18:59 +00001101 p+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +00001102 }
1103 if (bit != 0)
1104 {
1105 *q++=(unsigned char) (byte << (8-bit));
1106 x++;
1107 }
cristybb503372010-05-27 20:51:26 +00001108 for (x=(ssize_t) (image->columns+7)/8; x < (ssize_t) bytes_per_line; x++)
cristy3ed852e2009-09-05 21:47:34 +00001109 *q++=0x00;
cristycee97112010-05-28 00:44:52 +00001110 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
cristyddbc41b2011-04-24 14:27:48 +00001111 image->rows);
cristy3ed852e2009-09-05 21:47:34 +00001112 if (status == MagickFalse)
1113 break;
1114 }
1115 break;
1116 }
1117 case 8:
1118 {
1119 /*
1120 Convert PseudoClass packet to DIB pixel.
1121 */
cristybb503372010-05-27 20:51:26 +00001122 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00001123 {
cristy1e178e72011-08-28 19:44:34 +00001124 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
cristy4c08aed2011-07-01 19:47:50 +00001125 if (p == (const Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00001126 break;
cristy3ed852e2009-09-05 21:47:34 +00001127 q=pixels+(image->rows-y-1)*bytes_per_line;
cristybb503372010-05-27 20:51:26 +00001128 for (x=0; x < (ssize_t) image->columns; x++)
cristy4c08aed2011-07-01 19:47:50 +00001129 {
1130 *q++=(unsigned char) GetPixelIndex(image,p);
cristyed231572011-07-14 02:18:59 +00001131 p+=GetPixelChannels(image);
cristy4c08aed2011-07-01 19:47:50 +00001132 }
cristybb503372010-05-27 20:51:26 +00001133 for ( ; x < (ssize_t) bytes_per_line; x++)
cristy3ed852e2009-09-05 21:47:34 +00001134 *q++=0x00;
cristycee97112010-05-28 00:44:52 +00001135 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
cristyddbc41b2011-04-24 14:27:48 +00001136 image->rows);
cristy3ed852e2009-09-05 21:47:34 +00001137 if (status == MagickFalse)
1138 break;
1139 }
1140 break;
1141 }
1142 case 16:
1143 {
1144 unsigned short
1145 word;
1146 /*
1147 Convert PseudoClass packet to DIB pixel.
1148 */
cristybb503372010-05-27 20:51:26 +00001149 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00001150 {
cristy1e178e72011-08-28 19:44:34 +00001151 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
cristy4c08aed2011-07-01 19:47:50 +00001152 if (p == (const Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00001153 break;
1154 q=pixels+(image->rows-y-1)*bytes_per_line;
cristybb503372010-05-27 20:51:26 +00001155 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00001156 {
1157 word=(unsigned short) ((ScaleColor8to5((unsigned char)
cristy4c08aed2011-07-01 19:47:50 +00001158 ScaleQuantumToChar(GetPixelRed(image,p))) << 11) | (ScaleColor8to6(
1159 (unsigned char) ScaleQuantumToChar(GetPixelGreen(image,p))) << 5) |
1160 (ScaleColor8to5((unsigned char) ScaleQuantumToChar((unsigned char)
1161 GetPixelBlue(image,p)) << 0)));
cristy3ed852e2009-09-05 21:47:34 +00001162 *q++=(unsigned char)(word & 0xff);
1163 *q++=(unsigned char)(word >> 8);
cristyed231572011-07-14 02:18:59 +00001164 p+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +00001165 }
cristyf6fe0a12010-05-30 00:44:47 +00001166 for (x=(ssize_t) (2*image->columns); x < (ssize_t) bytes_per_line; x++)
cristy3ed852e2009-09-05 21:47:34 +00001167 *q++=0x00;
cristycee97112010-05-28 00:44:52 +00001168 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
cristyddbc41b2011-04-24 14:27:48 +00001169 image->rows);
cristy3ed852e2009-09-05 21:47:34 +00001170 if (status == MagickFalse)
1171 break;
1172 }
1173 break;
1174 }
1175 case 24:
1176 case 32:
1177 {
1178 /*
1179 Convert DirectClass packet to DIB RGB pixel.
1180 */
cristybb503372010-05-27 20:51:26 +00001181 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00001182 {
cristy1e178e72011-08-28 19:44:34 +00001183 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
cristy4c08aed2011-07-01 19:47:50 +00001184 if (p == (const Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00001185 break;
1186 q=pixels+(image->rows-y-1)*bytes_per_line;
cristybb503372010-05-27 20:51:26 +00001187 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00001188 {
cristy4c08aed2011-07-01 19:47:50 +00001189 *q++=ScaleQuantumToChar(GetPixelBlue(image,p));
1190 *q++=ScaleQuantumToChar(GetPixelGreen(image,p));
1191 *q++=ScaleQuantumToChar(GetPixelRed(image,p));
cristy3ed852e2009-09-05 21:47:34 +00001192 if (image->matte != MagickFalse)
cristy4c08aed2011-07-01 19:47:50 +00001193 *q++=ScaleQuantumToChar(GetPixelAlpha(image,p));
cristyed231572011-07-14 02:18:59 +00001194 p+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +00001195 }
1196 if (dib_info.bits_per_pixel == 24)
cristyf6fe0a12010-05-30 00:44:47 +00001197 for (x=(ssize_t) (3*image->columns); x < (ssize_t) bytes_per_line; x++)
cristy3ed852e2009-09-05 21:47:34 +00001198 *q++=0x00;
cristycee97112010-05-28 00:44:52 +00001199 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
cristyddbc41b2011-04-24 14:27:48 +00001200 image->rows);
cristy3ed852e2009-09-05 21:47:34 +00001201 if (status == MagickFalse)
1202 break;
1203 }
1204 break;
1205 }
1206 }
1207 if (dib_info.bits_per_pixel == 8)
1208 if (image_info->compression != NoCompression)
1209 {
1210 size_t
1211 length;
1212
1213 /*
1214 Convert run-length encoded raster pixels.
1215 */
1216 length=2UL*(bytes_per_line+2UL)+2UL;
1217 dib_data=(unsigned char *) AcquireQuantumMemory(length,
1218 (image->rows+2UL)*sizeof(*dib_data));
1219 if (pixels == (unsigned char *) NULL)
1220 {
1221 pixels=(unsigned char *) RelinquishMagickMemory(pixels);
1222 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
1223 }
cristybb503372010-05-27 20:51:26 +00001224 dib_info.image_size=(size_t) EncodeImage(image,bytes_per_line,
cristy3ed852e2009-09-05 21:47:34 +00001225 pixels,dib_data);
1226 pixels=(unsigned char *) RelinquishMagickMemory(pixels);
1227 pixels=dib_data;
1228 dib_info.compression = BI_RLE8;
1229 }
1230 /*
1231 Write DIB header.
1232 */
cristyf6fe0a12010-05-30 00:44:47 +00001233 (void) WriteBlobLSBLong(image,(unsigned int) dib_info.size);
cristyeaedf062010-05-29 22:36:02 +00001234 (void) WriteBlobLSBLong(image,dib_info.width);
cristy3ed852e2009-09-05 21:47:34 +00001235 (void) WriteBlobLSBLong(image,(unsigned short) dib_info.height);
1236 (void) WriteBlobLSBShort(image,(unsigned short) dib_info.planes);
1237 (void) WriteBlobLSBShort(image,dib_info.bits_per_pixel);
cristyf6fe0a12010-05-30 00:44:47 +00001238 (void) WriteBlobLSBLong(image,(unsigned int) dib_info.compression);
1239 (void) WriteBlobLSBLong(image,(unsigned int) dib_info.image_size);
1240 (void) WriteBlobLSBLong(image,(unsigned int) dib_info.x_pixels);
1241 (void) WriteBlobLSBLong(image,(unsigned int) dib_info.y_pixels);
1242 (void) WriteBlobLSBLong(image,(unsigned int) dib_info.number_colors);
1243 (void) WriteBlobLSBLong(image,(unsigned int) dib_info.colors_important);
cristy3ed852e2009-09-05 21:47:34 +00001244 if (image->storage_class == PseudoClass)
1245 {
1246 if (dib_info.bits_per_pixel <= 8)
1247 {
1248 unsigned char
1249 *dib_colormap;
1250
1251 /*
1252 Dump colormap to file.
1253 */
1254 dib_colormap=(unsigned char *) AcquireQuantumMemory((size_t)
1255 (1UL << dib_info.bits_per_pixel),4*sizeof(dib_colormap));
1256 if (dib_colormap == (unsigned char *) NULL)
1257 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
1258 q=dib_colormap;
cristybb503372010-05-27 20:51:26 +00001259 for (i=0; i < (ssize_t) MagickMin(image->colors,dib_info.number_colors); i++)
cristy3ed852e2009-09-05 21:47:34 +00001260 {
1261 *q++=ScaleQuantumToChar(image->colormap[i].blue);
1262 *q++=ScaleQuantumToChar(image->colormap[i].green);
1263 *q++=ScaleQuantumToChar(image->colormap[i].red);
1264 *q++=(Quantum) 0x0;
1265 }
cristybb503372010-05-27 20:51:26 +00001266 for ( ; i < (ssize_t) (1L << dib_info.bits_per_pixel); i++)
cristy3ed852e2009-09-05 21:47:34 +00001267 {
1268 *q++=(Quantum) 0x0;
1269 *q++=(Quantum) 0x0;
1270 *q++=(Quantum) 0x0;
1271 *q++=(Quantum) 0x0;
1272 }
1273 (void) WriteBlob(image,(size_t) (4*(1 << dib_info.bits_per_pixel)),
1274 dib_colormap);
1275 dib_colormap=(unsigned char *) RelinquishMagickMemory(dib_colormap);
1276 }
1277 else
1278 if ((dib_info.bits_per_pixel == 16) &&
1279 (dib_info.compression == BI_BITFIELDS))
1280 {
1281 (void) WriteBlobLSBLong(image,0xf800);
1282 (void) WriteBlobLSBLong(image,0x07e0);
1283 (void) WriteBlobLSBLong(image,0x001f);
1284 }
1285 }
1286 (void) WriteBlob(image,dib_info.image_size,pixels);
1287 pixels=(unsigned char *) RelinquishMagickMemory(pixels);
1288 (void) CloseBlob(image);
1289 return(MagickTrue);
1290}