blob: edc2939c4df7e1a8f764a0fb7484cf80ef750f30 [file] [log] [blame]
cristy3ed852e2009-09-05 21:47:34 +00001/*
2%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3% %
4% %
5% %
6% BBBB M M PPPP %
7% B B MM MM P P %
8% BBBB M M M PPPP %
9% B B M M P %
10% BBBB M M P %
11% %
12% %
13% Read/Write Microsoft Windows Bitmap Image Format %
14% %
15% Software Design %
16% John Cristy %
17% Glenn Randers-Pehrson %
18% December 2001 %
19% %
20% %
cristy16af1cb2009-12-11 21:38:29 +000021% Copyright 1999-2010 ImageMagick Studio LLC, a non-profit organization %
cristy3ed852e2009-09-05 21:47:34 +000022% dedicated to making software imaging solutions freely available. %
23% %
24% You may not use this file except in compliance with the License. You may %
25% obtain a copy of the License at %
26% %
27% http://www.imagemagick.org/script/license.php %
28% %
29% Unless required by applicable law or agreed to in writing, software %
30% distributed under the License is distributed on an "AS IS" BASIS, %
31% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. %
32% See the License for the specific language governing permissions and %
33% limitations under the License. %
34% %
35%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
36%
37%
38*/
39
40/*
41 Include declarations.
42*/
43#include "magick/studio.h"
44#include "magick/blob.h"
45#include "magick/blob-private.h"
46#include "magick/cache.h"
cristy316d5172009-09-17 19:31:25 +000047#include "magick/colormap-private.h"
cristy3ed852e2009-09-05 21:47:34 +000048#include "magick/color-private.h"
cristye7e40552010-04-24 21:34:22 +000049#include "magick/colormap.h"
cristy3ed852e2009-09-05 21:47:34 +000050#include "magick/colorspace.h"
51#include "magick/exception.h"
52#include "magick/exception-private.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/profile.h"
62#include "magick/quantum-private.h"
63#include "magick/static.h"
64#include "magick/string_.h"
65#include "magick/module.h"
66#include "magick/transform.h"
67
68/*
69 Macro definitions (from Windows wingdi.h).
70*/
71#undef BI_JPEG
72#define BI_JPEG 4
73#undef BI_PNG
74#define BI_PNG 5
cristy0157aea2010-04-24 21:12:18 +000075#if !defined(MAGICKCORE_WINDOWS_SUPPORT) || defined(__MINGW32__)
cristy3ed852e2009-09-05 21:47:34 +000076#define BI_RGB 0
77#define BI_RLE8 1
78#define BI_RLE4 2
79#define BI_BITFIELDS 3
80
81#define LCS_CALIBRATED_RBG 0
82#define LCS_sRGB 1
83#define LCS_WINDOWS_COLOR_SPACE 2
84#define PROFILE_LINKED 3
85#define PROFILE_EMBEDDED 4
86
87#define LCS_GM_BUSINESS 1 /* Saturation */
88#define LCS_GM_GRAPHICS 2 /* Relative */
89#define LCS_GM_IMAGES 4 /* Perceptual */
90#define LCS_GM_ABS_COLORIMETRIC 8 /* Absolute */
91#endif
92
93/*
94 Typedef declarations.
95*/
96typedef struct _BMPInfo
97{
98 unsigned int
99 file_size,
100 ba_offset,
101 offset_bits,
102 size;
103
cristybb503372010-05-27 20:51:26 +0000104 ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000105 width,
106 height;
107
108 unsigned short
109 planes,
110 bits_per_pixel;
111
112 unsigned int
113 compression,
114 image_size,
115 x_pixels,
116 y_pixels,
117 number_colors,
118 red_mask,
119 green_mask,
120 blue_mask,
121 alpha_mask,
122 colors_important;
123
124 int
125 colorspace;
126
127 PrimaryInfo
128 red_primary,
129 green_primary,
130 blue_primary,
131 gamma_scale;
132} BMPInfo;
133
134/*
135 Forward declarations.
136*/
137static MagickBooleanType
138 WriteBMPImage(const ImageInfo *,Image *);
139
140/*
141%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
142% %
143% %
144% %
145% D e c o d e I m a g e %
146% %
147% %
148% %
149%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
150%
151% DecodeImage unpacks the packed image pixels into runlength-encoded
152% pixel packets.
153%
154% The format of the DecodeImage method is:
155%
156% MagickBooleanType DecodeImage(Image *image,
cristybb503372010-05-27 20:51:26 +0000157% const size_t compression,unsigned char *pixels)
cristy3ed852e2009-09-05 21:47:34 +0000158%
159% A description of each parameter follows:
160%
161% o image: the address of a structure of type Image.
162%
163% o compression: Zero means uncompressed. A value of 1 means the
164% compressed pixels are runlength encoded for a 256-color bitmap.
165% A value of 2 means a 16-color bitmap. A value of 3 means bitfields
166% encoding.
167%
168% o pixels: The address of a byte (8 bits) array of pixel data created by
169% the decoding process.
170%
171*/
172
cristybb503372010-05-27 20:51:26 +0000173static inline ssize_t MagickAbsoluteValue(const ssize_t x)
cristy3ed852e2009-09-05 21:47:34 +0000174{
175 if (x < 0)
176 return(-x);
177 return(x);
178}
179
180static inline size_t MagickMax(const size_t x,const size_t y)
181{
182 if (x > y)
183 return(x);
184 return(y);
185}
186
cristybb503372010-05-27 20:51:26 +0000187static inline ssize_t MagickMin(const ssize_t x,const ssize_t y)
cristy3ed852e2009-09-05 21:47:34 +0000188{
189 if (x < y)
190 return(x);
191 return(y);
192}
193
194static MagickBooleanType DecodeImage(Image *image,
cristybb503372010-05-27 20:51:26 +0000195 const size_t compression,unsigned char *pixels)
cristy3ed852e2009-09-05 21:47:34 +0000196{
197 int
198 count;
199
cristybb503372010-05-27 20:51:26 +0000200 ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000201 y;
202
cristybb503372010-05-27 20:51:26 +0000203 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000204 i,
205 x;
206
207 register unsigned char
208 *p,
209 *q;
210
211 unsigned char
212 byte;
213
214 assert(image != (Image *) NULL);
215 assert(image->signature == MagickSignature);
216 if (image->debug != MagickFalse)
217 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
218 assert(pixels != (unsigned char *) NULL);
219 (void) ResetMagickMemory(pixels,0,(size_t) image->columns*image->rows*
220 sizeof(*pixels));
221 byte=0;
222 x=0;
223 p=pixels;
224 q=pixels+(size_t) image->columns*image->rows;
cristybb503372010-05-27 20:51:26 +0000225 for (y=0; y < (ssize_t) image->rows; )
cristy3ed852e2009-09-05 21:47:34 +0000226 {
227 if ((p < pixels) || (p >= q))
228 break;
229 count=ReadBlobByte(image);
230 if (count == EOF)
231 break;
232 if (count != 0)
233 {
234 /*
235 Encoded mode.
236 */
237 count=MagickMin(count,(int) (q-p));
238 byte=(unsigned char) ReadBlobByte(image);
239 if (compression == BI_RLE8)
240 {
241 for (i=0; i < count; i++)
242 *p++=(unsigned char) byte;
243 }
244 else
245 {
246 for (i=0; i < count; i++)
247 *p++=(unsigned char)
248 ((i & 0x01) != 0 ? (byte & 0x0f) : ((byte >> 4) & 0x0f));
249 }
250 x+=count;
251 }
252 else
253 {
254 /*
255 Escape mode.
256 */
257 count=ReadBlobByte(image);
258 if (count == 0x01)
259 return(MagickTrue);
260 switch (count)
261 {
262 case 0x00:
263 {
264 /*
265 End of line.
266 */
267 x=0;
268 y++;
269 p=pixels+y*image->columns;
270 break;
271 }
272 case 0x02:
273 {
274 /*
275 Delta mode.
276 */
277 x+=ReadBlobByte(image);
278 y+=ReadBlobByte(image);
279 p=pixels+y*image->columns+x;
280 break;
281 }
282 default:
283 {
284 /*
285 Absolute mode.
286 */
287 count=MagickMin(count,(int) (q-p));
288 if (compression == BI_RLE8)
289 for (i=0; i < count; i++)
290 *p++=(unsigned char) ReadBlobByte(image);
291 else
292 for (i=0; i < count; i++)
293 {
294 if ((i & 0x01) == 0)
295 byte=(unsigned char) ReadBlobByte(image);
296 *p++=(unsigned char)
297 ((i & 0x01) != 0 ? (byte & 0x0f) : ((byte >> 4) & 0x0f));
298 }
299 x+=count;
300 /*
301 Read pad byte.
302 */
303 if (compression == BI_RLE8)
304 {
305 if ((count & 0x01) != 0)
306 (void) ReadBlobByte(image);
307 }
308 else
309 if (((count & 0x03) == 1) || ((count & 0x03) == 2))
310 (void) ReadBlobByte(image);
311 break;
312 }
313 }
314 }
315 if (SetImageProgress(image,LoadImageTag,y,image->rows) == MagickFalse)
316 break;
317 }
318 (void) ReadBlobByte(image); /* end of line */
319 (void) ReadBlobByte(image);
320 return(MagickTrue);
321}
322
323/*
324%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
325% %
326% %
327% %
328% E n c o d e I m a g e %
329% %
330% %
331% %
332%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
333%
334% EncodeImage compresses pixels using a runlength encoded format.
335%
336% The format of the EncodeImage method is:
337%
338% static MagickBooleanType EncodeImage(Image *image,
cristybb503372010-05-27 20:51:26 +0000339% const size_t bytes_per_line,const unsigned char *pixels,
cristy3ed852e2009-09-05 21:47:34 +0000340% unsigned char *compressed_pixels)
341%
342% A description of each parameter follows:
343%
344% o image: The image.
345%
346% o bytes_per_line: the number of bytes in a scanline of compressed pixels
347%
348% o pixels: The address of a byte (8 bits) array of pixel data created by
349% the compression process.
350%
351% o compressed_pixels: The address of a byte (8 bits) array of compressed
352% pixel data.
353%
354*/
cristybb503372010-05-27 20:51:26 +0000355static size_t EncodeImage(Image *image,const size_t bytes_per_line,
cristy3ed852e2009-09-05 21:47:34 +0000356 const unsigned char *pixels,unsigned char *compressed_pixels)
357{
cristybb503372010-05-27 20:51:26 +0000358 ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000359 y;
360
361 MagickBooleanType
362 status;
363
364 register const unsigned char
365 *p;
366
cristybb503372010-05-27 20:51:26 +0000367 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000368 i,
369 x;
370
371 register unsigned char
372 *q;
373
374 /*
375 Runlength encode pixels.
376 */
377 assert(image != (Image *) NULL);
378 assert(image->signature == MagickSignature);
379 if (image->debug != MagickFalse)
380 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
381 assert(pixels != (const unsigned char *) NULL);
382 assert(compressed_pixels != (unsigned char *) NULL);
383 p=pixels;
384 q=compressed_pixels;
385 i=0;
cristybb503372010-05-27 20:51:26 +0000386 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +0000387 {
cristybb503372010-05-27 20:51:26 +0000388 for (x=0; x < (ssize_t) bytes_per_line; x+=i)
cristy3ed852e2009-09-05 21:47:34 +0000389 {
390 /*
391 Determine runlength.
392 */
cristybb503372010-05-27 20:51:26 +0000393 for (i=1; ((x+i) < (ssize_t) bytes_per_line); i++)
cristy3ed852e2009-09-05 21:47:34 +0000394 if ((i == 255) || (*(p+i) != *p))
395 break;
396 *q++=(unsigned char) i;
397 *q++=(*p);
398 p+=i;
399 }
400 /*
401 End of line.
402 */
403 *q++=(unsigned char) 0x00;
404 *q++=(unsigned char) 0x00;
cristycee97112010-05-28 00:44:52 +0000405 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
406 image->rows);
cristy3ed852e2009-09-05 21:47:34 +0000407 if (status == MagickFalse)
408 break;
409 }
410 /*
411 End of bitmap.
412 */
413 *q++=(unsigned char) 0x00;
414 *q++=(unsigned char) 0x01;
415 return((size_t) (q-compressed_pixels));
416}
417
418/*
419%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
420% %
421% %
422% %
423% I s B M P %
424% %
425% %
426% %
427%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
428%
429% IsBMP() returns MagickTrue if the image format type, identified by the
430% magick string, is BMP.
431%
432% The format of the IsBMP method is:
433%
434% MagickBooleanType IsBMP(const unsigned char *magick,const size_t length)
435%
436% A description of each parameter follows:
437%
438% o magick: compare image format pattern against these bytes.
439%
440% o length: Specifies the length of the magick string.
441%
442*/
443static MagickBooleanType IsBMP(const unsigned char *magick,const size_t length)
444{
445 if (length < 2)
446 return(MagickFalse);
447 if ((LocaleNCompare((char *) magick,"BA",2) == 0) ||
448 (LocaleNCompare((char *) magick,"BM",2) == 0) ||
449 (LocaleNCompare((char *) magick,"IC",2) == 0) ||
450 (LocaleNCompare((char *) magick,"PI",2) == 0) ||
451 (LocaleNCompare((char *) magick,"CI",2) == 0) ||
452 (LocaleNCompare((char *) magick,"CP",2) == 0))
453 return(MagickTrue);
454 return(MagickFalse);
455}
456
457/*
458%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
459% %
460% %
461% %
462% R e a d B M P I m a g e %
463% %
464% %
465% %
466%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
467%
468% ReadBMPImage() reads a Microsoft Windows bitmap image file, Version
469% 2, 3 (for Windows or NT), or 4, and returns it. It allocates the memory
470% necessary for the new Image structure and returns a pointer to the new
471% image.
472%
473% The format of the ReadBMPImage method is:
474%
475% image=ReadBMPImage(image_info)
476%
477% A description of each parameter follows:
478%
479% o image_info: the image info.
480%
481% o exception: return any errors or warnings in this structure.
482%
483*/
484
485static Image *ReadBMPImage(const ImageInfo *image_info,ExceptionInfo *exception)
486{
487 BMPInfo
488 bmp_info;
489
490 Image
491 *image;
492
493 IndexPacket
494 index;
495
cristybb503372010-05-27 20:51:26 +0000496 ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000497 y;
498
499 MagickBooleanType
500 status;
501
502 MagickOffsetType
503 offset,
504 start_position;
505
506 register IndexPacket
507 *indexes;
508
cristybb503372010-05-27 20:51:26 +0000509 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000510 x;
511
512 register PixelPacket
513 *q;
514
cristybb503372010-05-27 20:51:26 +0000515 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000516 i;
517
518 register unsigned char
519 *p;
520
521 ssize_t
522 count;
523
524 size_t
525 length;
526
527 unsigned char
528 magick[12],
529 *pixels;
530
cristybb503372010-05-27 20:51:26 +0000531 size_t
cristy3ed852e2009-09-05 21:47:34 +0000532 bit,
533 blue,
534 bytes_per_line,
535 green,
536 opacity,
537 red;
538
539 /*
540 Open image file.
541 */
542 assert(image_info != (const ImageInfo *) NULL);
543 assert(image_info->signature == MagickSignature);
544 if (image_info->debug != MagickFalse)
545 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
546 image_info->filename);
547 assert(exception != (ExceptionInfo *) NULL);
548 assert(exception->signature == MagickSignature);
549 image=AcquireImage(image_info);
550 status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception);
551 if (status == MagickFalse)
552 {
553 image=DestroyImageList(image);
554 return((Image *) NULL);
555 }
556 /*
557 Determine if this a BMP file.
558 */
559 (void) ResetMagickMemory(&bmp_info,0,sizeof(bmp_info));
560 bmp_info.ba_offset=0;
561 start_position=0;
562 count=ReadBlob(image,2,magick);
563 do
564 {
565 LongPixelPacket
566 shift;
567
568 PixelPacket
569 quantum_bits;
570
cristybb503372010-05-27 20:51:26 +0000571 size_t
cristy3ed852e2009-09-05 21:47:34 +0000572 profile_data,
573 profile_size;
574
575 /*
576 Verify BMP identifier.
577 */
578 if (bmp_info.ba_offset == 0)
579 start_position=TellBlob(image)-2;
580 bmp_info.ba_offset=0;
581 while (LocaleNCompare((char *) magick,"BA",2) == 0)
582 {
583 bmp_info.file_size=ReadBlobLSBLong(image);
584 bmp_info.ba_offset=ReadBlobLSBLong(image);
585 bmp_info.offset_bits=ReadBlobLSBLong(image);
586 count=ReadBlob(image,2,magick);
587 if (count != 2)
588 break;
589 }
590 if (image->debug != MagickFalse)
591 (void) LogMagickEvent(CoderEvent,GetMagickModule()," Magick: %c%c",
592 magick[0],magick[1]);
593 if ((count == 0) || ((LocaleNCompare((char *) magick,"BM",2) != 0) &&
594 (LocaleNCompare((char *) magick,"CI",2) != 0)))
595 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
596 bmp_info.file_size=ReadBlobLSBLong(image);
597 (void) ReadBlobLSBLong(image);
598 bmp_info.offset_bits=ReadBlobLSBLong(image);
599 bmp_info.size=ReadBlobLSBLong(image);
600 if (image->debug != MagickFalse)
601 (void) LogMagickEvent(CoderEvent,GetMagickModule()," BMP size: %u",
602 bmp_info.size);
603 if (bmp_info.size == 12)
604 {
605 /*
606 OS/2 BMP image file.
607 */
608 bmp_info.width=(short) ReadBlobLSBShort(image);
609 bmp_info.height=(short) ReadBlobLSBShort(image);
610 bmp_info.planes=ReadBlobLSBShort(image);
611 bmp_info.bits_per_pixel=ReadBlobLSBShort(image);
612 bmp_info.x_pixels=0;
613 bmp_info.y_pixels=0;
614 bmp_info.number_colors=0;
615 bmp_info.compression=BI_RGB;
616 bmp_info.image_size=0;
617 bmp_info.alpha_mask=0;
618 if (image->debug != MagickFalse)
619 {
620 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
621 " Format: OS/2 Bitmap");
622 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
cristye8c25f92010-06-03 00:53:06 +0000623 " Geometry: %.20gx%.20g",(double) bmp_info.width,(double)
624 bmp_info.height);
cristy3ed852e2009-09-05 21:47:34 +0000625 }
626 }
627 else
628 {
629 /*
630 Microsoft Windows BMP image file.
631 */
632 if (bmp_info.size < 40)
633 ThrowReaderException(CorruptImageError,"NonOS2HeaderSizeError");
634 bmp_info.width=(int) ReadBlobLSBLong(image);
635 bmp_info.height=(int) ReadBlobLSBLong(image);
636 bmp_info.planes=ReadBlobLSBShort(image);
637 bmp_info.bits_per_pixel=ReadBlobLSBShort(image);
638 bmp_info.compression=ReadBlobLSBLong(image);
639 bmp_info.image_size=ReadBlobLSBLong(image);
640 bmp_info.x_pixels=ReadBlobLSBLong(image);
641 bmp_info.y_pixels=ReadBlobLSBLong(image);
642 bmp_info.number_colors=ReadBlobLSBLong(image);
643 bmp_info.colors_important=ReadBlobLSBLong(image);
644 profile_data=0;
645 profile_size=0;
646 if (image->debug != MagickFalse)
647 {
648 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
649 " Format: MS Windows bitmap");
650 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
cristye8c25f92010-06-03 00:53:06 +0000651 " Geometry: %.20gx%.20g",(double) bmp_info.width,(double)
652 bmp_info.height);
cristy3ed852e2009-09-05 21:47:34 +0000653 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
cristye8c25f92010-06-03 00:53:06 +0000654 " Bits per pixel: %.20g",(double) bmp_info.bits_per_pixel);
cristy3ed852e2009-09-05 21:47:34 +0000655 switch ((int) bmp_info.compression)
656 {
657 case BI_RGB:
658 {
659 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
660 " Compression: BI_RGB");
661 break;
662 }
663 case BI_RLE4:
664 {
665 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
666 " Compression: BI_RLE4");
667 break;
668 }
669 case BI_RLE8:
670 {
671 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
672 " Compression: BI_RLE8");
673 break;
674 }
675 case BI_BITFIELDS:
676 {
677 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
678 " Compression: BI_BITFIELDS");
679 break;
680 }
681 case BI_PNG:
682 {
683 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
684 " Compression: BI_PNG");
685 break;
686 }
687 case BI_JPEG:
688 {
689 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
690 " Compression: BI_JPEG");
691 break;
692 }
693 default:
694 {
695 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
696 " Compression: UNKNOWN (%u)",bmp_info.compression);
697 }
698 }
699 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
700 " Number of colors: %u",bmp_info.number_colors);
701 }
702 bmp_info.red_mask=ReadBlobLSBLong(image);
703 bmp_info.green_mask=ReadBlobLSBLong(image);
704 bmp_info.blue_mask=ReadBlobLSBLong(image);
705 if (bmp_info.size > 40)
706 {
707 double
708 sum;
709
710 /*
711 Read color management information.
712 */
713 bmp_info.alpha_mask=ReadBlobLSBLong(image);
cristybb503372010-05-27 20:51:26 +0000714 bmp_info.colorspace=(ssize_t) ReadBlobLSBLong(image);
cristy3ed852e2009-09-05 21:47:34 +0000715 /*
716 Decode 2^30 fixed point formatted CIE primaries.
717 */
718 bmp_info.red_primary.x=(double) ReadBlobLSBLong(image)/0x3ffffff;
719 bmp_info.red_primary.y=(double) ReadBlobLSBLong(image)/0x3ffffff;
720 bmp_info.red_primary.z=(double) ReadBlobLSBLong(image)/0x3ffffff;
721 bmp_info.green_primary.x=(double) ReadBlobLSBLong(image)/0x3ffffff;
722 bmp_info.green_primary.y=(double) ReadBlobLSBLong(image)/0x3ffffff;
723 bmp_info.green_primary.z=(double) ReadBlobLSBLong(image)/0x3ffffff;
724 bmp_info.blue_primary.x=(double) ReadBlobLSBLong(image)/0x3ffffff;
725 bmp_info.blue_primary.y=(double) ReadBlobLSBLong(image)/0x3ffffff;
726 bmp_info.blue_primary.z=(double) ReadBlobLSBLong(image)/0x3ffffff;
727 sum=bmp_info.red_primary.x+bmp_info.red_primary.x+
728 bmp_info.red_primary.z;
729 image->chromaticity.red_primary.x/=sum;
730 image->chromaticity.red_primary.y/=sum;
731 sum=bmp_info.green_primary.x+bmp_info.green_primary.x+
732 bmp_info.green_primary.z;
733 image->chromaticity.green_primary.x/=sum;
734 image->chromaticity.green_primary.y/=sum;
735 sum=bmp_info.blue_primary.x+bmp_info.blue_primary.x+
736 bmp_info.blue_primary.z;
737 image->chromaticity.blue_primary.x/=sum;
738 image->chromaticity.blue_primary.y/=sum;
739 /*
740 Decode 16^16 fixed point formatted gamma_scales.
741 */
742 bmp_info.gamma_scale.x=(double) ReadBlobLSBLong(image)/0xffff;
743 bmp_info.gamma_scale.y=(double) ReadBlobLSBLong(image)/0xffff;
744 bmp_info.gamma_scale.z=(double) ReadBlobLSBLong(image)/0xffff;
745 /*
746 Compute a single gamma from the BMP 3-channel gamma.
747 */
748 image->gamma=(bmp_info.gamma_scale.x+bmp_info.gamma_scale.y+
749 bmp_info.gamma_scale.z)/3.0;
750 }
751 if (bmp_info.size > 108)
752 {
cristybb503372010-05-27 20:51:26 +0000753 size_t
cristy3ed852e2009-09-05 21:47:34 +0000754 intent;
755
756 /*
757 Read BMP Version 5 color management information.
758 */
759 intent=ReadBlobLSBLong(image);
760 switch ((int) intent)
761 {
762 case LCS_GM_BUSINESS:
763 {
764 image->rendering_intent=SaturationIntent;
765 break;
766 }
767 case LCS_GM_GRAPHICS:
768 {
769 image->rendering_intent=RelativeIntent;
770 break;
771 }
772 case LCS_GM_IMAGES:
773 {
774 image->rendering_intent=PerceptualIntent;
775 break;
776 }
777 case LCS_GM_ABS_COLORIMETRIC:
778 {
779 image->rendering_intent=AbsoluteIntent;
780 break;
781 }
782 }
783 profile_data=ReadBlobLSBLong(image);
784 profile_size=ReadBlobLSBLong(image);
785 (void) ReadBlobLSBLong(image); /* Reserved byte */
786 }
787 }
788 if ((bmp_info.compression != BI_RGB) &&
789 ((MagickSizeType) bmp_info.file_size != GetBlobSize(image)))
790 (void) ThrowMagickException(exception,GetMagickModule(),CorruptImageError,
791 "LengthAndFilesizeDoNotMatch","`%s'",image->filename);
792 if (bmp_info.width <= 0)
793 ThrowReaderException(CorruptImageError,"NegativeOrZeroImageSize");
794 if (bmp_info.height == 0)
795 ThrowReaderException(CorruptImageError,"NegativeOrZeroImageSize");
796 if (bmp_info.planes != 1)
797 ThrowReaderException(CorruptImageError,"StaticPlanesValueNotEqualToOne");
798 if ((bmp_info.bits_per_pixel != 1) && (bmp_info.bits_per_pixel != 4) &&
799 (bmp_info.bits_per_pixel != 8) && (bmp_info.bits_per_pixel != 16) &&
800 (bmp_info.bits_per_pixel != 24) && (bmp_info.bits_per_pixel != 32))
801 ThrowReaderException(CorruptImageError,"UnrecognizedBitsPerPixel");
802 if (bmp_info.number_colors > (1U << bmp_info.bits_per_pixel))
803 {
804 if (bmp_info.bits_per_pixel < 24)
805 ThrowReaderException(CorruptImageError,"UnrecognizedNumberOfColors");
806 bmp_info.number_colors=0;
807 }
808 if (bmp_info.compression > 3)
809 ThrowReaderException(CorruptImageError,"UnrecognizedImageCompression");
810 if ((bmp_info.compression == 1) && (bmp_info.bits_per_pixel != 8))
811 ThrowReaderException(CorruptImageError,"UnrecognizedBitsPerPixel");
812 if ((bmp_info.compression == 2) && (bmp_info.bits_per_pixel != 4))
813 ThrowReaderException(CorruptImageError,"UnrecognizedBitsPerPixel");
814 if ((bmp_info.compression == 3) && (bmp_info.bits_per_pixel < 16))
815 ThrowReaderException(CorruptImageError,"UnrecognizedBitsPerPixel");
816 switch (bmp_info.compression)
817 {
818 case BI_RGB:
819 case BI_RLE8:
820 case BI_RLE4:
821 case BI_BITFIELDS:
822 break;
823 case BI_JPEG:
824 ThrowReaderException(CoderError,"JPEGCompressNotSupported");
825 case BI_PNG:
826 ThrowReaderException(CoderError,"PNGCompressNotSupported");
827 default:
828 ThrowReaderException(CorruptImageError,"UnrecognizedImageCompression");
829 }
cristybb503372010-05-27 20:51:26 +0000830 image->columns=(size_t) MagickAbsoluteValue(bmp_info.width);
831 image->rows=(size_t) MagickAbsoluteValue(bmp_info.height);
cristy3ed852e2009-09-05 21:47:34 +0000832 image->depth=bmp_info.bits_per_pixel <= 8 ? bmp_info.bits_per_pixel : 8;
cristyaeb2cbc2010-05-07 13:28:58 +0000833 if ((bmp_info.bits_per_pixel == 16) ||
834 (bmp_info.bits_per_pixel == 32))
835 image->matte=bmp_info.alpha_mask != 0 ? MagickTrue : MagickFalse;
cristy6a4e1002010-05-26 12:48:31 +0000836 if ((bmp_info.number_colors != 0) || (bmp_info.bits_per_pixel < 16))
cristy3ed852e2009-09-05 21:47:34 +0000837 {
cristy0b29b252010-05-30 01:59:46 +0000838 size_t
839 one;
840
cristy3ed852e2009-09-05 21:47:34 +0000841 image->storage_class=PseudoClass;
842 image->colors=bmp_info.number_colors;
cristy0b29b252010-05-30 01:59:46 +0000843 one=1;
cristy3ed852e2009-09-05 21:47:34 +0000844 if (image->colors == 0)
cristy0b29b252010-05-30 01:59:46 +0000845 image->colors=one << bmp_info.bits_per_pixel;
cristy3ed852e2009-09-05 21:47:34 +0000846 }
847 if (image->storage_class == PseudoClass)
848 {
849 unsigned char
850 *bmp_colormap;
851
852 size_t
853 packet_size;
854
855 /*
856 Read BMP raster colormap.
857 */
858 if (image->debug != MagickFalse)
859 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
cristye8c25f92010-06-03 00:53:06 +0000860 " Reading colormap of %.20g colors",(double) image->colors);
cristy3ed852e2009-09-05 21:47:34 +0000861 if (AcquireImageColormap(image,image->colors) == MagickFalse)
862 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
863 bmp_colormap=(unsigned char *) AcquireQuantumMemory((size_t)
864 image->colors,4*sizeof(*bmp_colormap));
865 if (bmp_colormap == (unsigned char *) NULL)
866 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
867 if ((bmp_info.size == 12) || (bmp_info.size == 64))
868 packet_size=3;
869 else
870 packet_size=4;
871 offset=SeekBlob(image,start_position+14+bmp_info.size,SEEK_SET);
872 if (offset < 0)
873 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
874 count=ReadBlob(image,packet_size*image->colors,bmp_colormap);
875 if (count != (ssize_t) (packet_size*image->colors))
876 ThrowReaderException(CorruptImageError,"InsufficientImageDataInFile");
877 p=bmp_colormap;
cristybb503372010-05-27 20:51:26 +0000878 for (i=0; i < (ssize_t) image->colors; i++)
cristy3ed852e2009-09-05 21:47:34 +0000879 {
880 image->colormap[i].blue=ScaleCharToQuantum(*p++);
881 image->colormap[i].green=ScaleCharToQuantum(*p++);
882 image->colormap[i].red=ScaleCharToQuantum(*p++);
883 if (packet_size == 4)
884 p++;
885 }
886 bmp_colormap=(unsigned char *) RelinquishMagickMemory(bmp_colormap);
887 }
888 if ((image_info->ping != MagickFalse) && (image_info->number_scenes != 0))
889 if (image->scene >= (image_info->scene+image_info->number_scenes-1))
890 break;
891 /*
892 Read image data.
893 */
894 offset=SeekBlob(image,start_position+bmp_info.offset_bits,SEEK_SET);
895 if (offset < 0)
896 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
897 if (bmp_info.compression == BI_RLE4)
898 bmp_info.bits_per_pixel<<=1;
899 bytes_per_line=4*((image->columns*bmp_info.bits_per_pixel+31)/32);
900 length=(size_t) bytes_per_line*image->rows;
901 pixels=(unsigned char *) AcquireQuantumMemory((size_t) image->rows,
902 MagickMax(bytes_per_line,image->columns+256UL)*sizeof(*pixels));
903 if (pixels == (unsigned char *) NULL)
904 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
905 if ((bmp_info.compression == BI_RGB) ||
906 (bmp_info.compression == BI_BITFIELDS))
907 {
908 if (image->debug != MagickFalse)
909 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
cristye8c25f92010-06-03 00:53:06 +0000910 " Reading pixels (%.20g bytes)",(double) length);
cristy3ed852e2009-09-05 21:47:34 +0000911 count=ReadBlob(image,length,pixels);
912 if (count != (ssize_t) length)
913 ThrowReaderException(CorruptImageError,"InsufficientImageDataInFile");
914 }
915 else
916 {
917 /*
918 Convert run-length encoded raster pixels.
919 */
920 status=DecodeImage(image,bmp_info.compression,pixels);
921 if (status == MagickFalse)
922 ThrowReaderException(CorruptImageError,
923 "UnableToRunlengthDecodeImage");
924 }
925 /*
926 Initialize image structure.
927 */
928 image->x_resolution=(double) bmp_info.x_pixels/100.0;
929 image->y_resolution=(double) bmp_info.y_pixels/100.0;
930 image->units=PixelsPerCentimeterResolution;
931 /*
932 Convert BMP raster image to pixel packets.
933 */
934 if (bmp_info.compression == BI_RGB)
935 {
936 bmp_info.alpha_mask=0;
937 bmp_info.red_mask=0x00ff0000U;
938 bmp_info.green_mask=0x0000ff00U;
939 bmp_info.blue_mask=0x000000ffU;
940 if (bmp_info.bits_per_pixel == 16)
941 {
942 /*
943 RGB555.
944 */
945 bmp_info.red_mask=0x00007c00U;
946 bmp_info.green_mask=0x000003e0U;
947 bmp_info.blue_mask=0x0000001fU;
948 }
949 }
950 if ((bmp_info.bits_per_pixel == 16) || (bmp_info.bits_per_pixel == 32))
951 {
cristybb503372010-05-27 20:51:26 +0000952 register size_t
cristy3ed852e2009-09-05 21:47:34 +0000953 sample;
954
955 /*
956 Get shift and quantum bits info from bitfield masks.
957 */
958 (void) ResetMagickMemory(&shift,0,sizeof(shift));
959 (void) ResetMagickMemory(&quantum_bits,0,sizeof(quantum_bits));
960 if (bmp_info.red_mask != 0)
961 while (((bmp_info.red_mask << shift.red) & 0x80000000UL) == 0)
962 shift.red++;
963 if (bmp_info.green_mask != 0)
964 while (((bmp_info.green_mask << shift.green) & 0x80000000UL) == 0)
965 shift.green++;
966 if (bmp_info.blue_mask != 0)
967 while (((bmp_info.blue_mask << shift.blue) & 0x80000000UL) == 0)
968 shift.blue++;
969 if (bmp_info.alpha_mask != 0)
970 while (((bmp_info.alpha_mask << shift.opacity) & 0x80000000UL) == 0)
971 shift.opacity++;
972 sample=shift.red;
973 while (((bmp_info.red_mask << sample) & 0x80000000UL) != 0)
974 sample++;
975 quantum_bits.red=(Quantum) (sample-shift.red);
976 sample=shift.green;
977 while (((bmp_info.green_mask << sample) & 0x80000000UL) != 0)
978 sample++;
979 quantum_bits.green=(Quantum) (sample-shift.green);
980 sample=shift.blue;
981 while (((bmp_info.blue_mask << sample) & 0x80000000UL) != 0)
982 sample++;
983 quantum_bits.blue=(Quantum) (sample-shift.blue);
984 sample=shift.opacity;
985 while (((bmp_info.alpha_mask << sample) & 0x80000000UL) != 0)
986 sample++;
987 quantum_bits.opacity=(Quantum) (sample-shift.opacity);
988 }
989 switch (bmp_info.bits_per_pixel)
990 {
991 case 1:
992 {
993 /*
994 Convert bitmap scanline.
995 */
cristybb503372010-05-27 20:51:26 +0000996 for (y=(ssize_t) image->rows-1; y >= 0; y--)
cristy3ed852e2009-09-05 21:47:34 +0000997 {
998 p=pixels+(image->rows-y-1)*bytes_per_line;
999 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
1000 if (q == (PixelPacket *) NULL)
1001 break;
1002 indexes=GetAuthenticIndexQueue(image);
cristybb503372010-05-27 20:51:26 +00001003 for (x=0; x < ((ssize_t) image->columns-7); x+=8)
cristy3ed852e2009-09-05 21:47:34 +00001004 {
1005 for (bit=0; bit < 8; bit++)
1006 {
1007 index=(IndexPacket) (((*p) & (0x80 >> bit)) != 0 ? 0x01 : 0x00);
1008 indexes[x+bit]=index;
cristybb503372010-05-27 20:51:26 +00001009 *q++=image->colormap[(ssize_t) index];
cristy3ed852e2009-09-05 21:47:34 +00001010 }
1011 p++;
1012 }
1013 if ((image->columns % 8) != 0)
1014 {
1015 for (bit=0; bit < (image->columns % 8); bit++)
1016 {
1017 index=(IndexPacket) (((*p) & (0x80 >> bit)) != 0 ? 0x01 : 0x00);
1018 indexes[x+bit]=index;
cristybb503372010-05-27 20:51:26 +00001019 *q++=image->colormap[(ssize_t) index];
cristy3ed852e2009-09-05 21:47:34 +00001020 }
1021 p++;
1022 }
1023 if (SyncAuthenticPixels(image,exception) == MagickFalse)
1024 break;
1025 if (image->previous == (Image *) NULL)
1026 {
cristycee97112010-05-28 00:44:52 +00001027 status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y,
1028 image->rows);
cristy3ed852e2009-09-05 21:47:34 +00001029 if (status == MagickFalse)
1030 break;
1031 }
1032 }
1033 break;
1034 }
1035 case 4:
1036 {
1037 /*
1038 Convert PseudoColor scanline.
1039 */
cristybb503372010-05-27 20:51:26 +00001040 for (y=(ssize_t) image->rows-1; y >= 0; y--)
cristy3ed852e2009-09-05 21:47:34 +00001041 {
1042 p=pixels+(image->rows-y-1)*bytes_per_line;
1043 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
1044 if (q == (PixelPacket *) NULL)
1045 break;
1046 indexes=GetAuthenticIndexQueue(image);
cristybb503372010-05-27 20:51:26 +00001047 for (x=0; x < ((ssize_t) image->columns-1); x+=2)
cristy3ed852e2009-09-05 21:47:34 +00001048 {
1049 index=ConstrainColormapIndex(image,(*p >> 4) & 0x0f);
1050 indexes[x]=index;
cristybb503372010-05-27 20:51:26 +00001051 *q++=image->colormap[(ssize_t) index];
cristy3ed852e2009-09-05 21:47:34 +00001052 index=ConstrainColormapIndex(image,*p & 0x0f);
1053 indexes[x+1]=index;
cristybb503372010-05-27 20:51:26 +00001054 *q++=image->colormap[(ssize_t) index];
cristy3ed852e2009-09-05 21:47:34 +00001055 p++;
1056 }
1057 if ((image->columns % 2) != 0)
1058 {
1059 index=ConstrainColormapIndex(image,(*p >> 4) & 0xf);
1060 indexes[x]=index;
cristybb503372010-05-27 20:51:26 +00001061 *q++=image->colormap[(ssize_t) index];
cristy3ed852e2009-09-05 21:47:34 +00001062 p++;
1063 }
1064 if (SyncAuthenticPixels(image,exception) == MagickFalse)
1065 break;
1066 if (image->previous == (Image *) NULL)
1067 {
cristycee97112010-05-28 00:44:52 +00001068 status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y,
1069 image->rows);
cristy3ed852e2009-09-05 21:47:34 +00001070 if (status == MagickFalse)
1071 break;
1072 }
1073 }
1074 break;
1075 }
1076 case 8:
1077 {
1078 /*
1079 Convert PseudoColor scanline.
1080 */
1081 if ((bmp_info.compression == BI_RLE8) ||
1082 (bmp_info.compression == BI_RLE4))
1083 bytes_per_line=image->columns;
cristybb503372010-05-27 20:51:26 +00001084 for (y=(ssize_t) image->rows-1; y >= 0; y--)
cristy3ed852e2009-09-05 21:47:34 +00001085 {
1086 p=pixels+(image->rows-y-1)*bytes_per_line;
1087 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
1088 if (q == (PixelPacket *) NULL)
1089 break;
1090 indexes=GetAuthenticIndexQueue(image);
cristybb503372010-05-27 20:51:26 +00001091 for (x = (ssize_t)image->columns; x != 0; --x)
cristy3ed852e2009-09-05 21:47:34 +00001092 {
1093 index=ConstrainColormapIndex(image,*p);
1094 *indexes++=index;
cristybb503372010-05-27 20:51:26 +00001095 *q=image->colormap[(ssize_t) index];
cristy3ed852e2009-09-05 21:47:34 +00001096 p++;
1097 q++;
1098 }
1099 if (SyncAuthenticPixels(image,exception) == MagickFalse)
1100 break;
1101 offset=(MagickOffsetType) (image->rows-y-1);
1102 if (image->previous == (Image *) NULL)
1103 {
cristycee97112010-05-28 00:44:52 +00001104 status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y,
1105 image->rows);
cristy3ed852e2009-09-05 21:47:34 +00001106 if (status == MagickFalse)
1107 break;
1108 }
1109 }
1110 break;
1111 }
1112 case 16:
1113 {
cristybb503372010-05-27 20:51:26 +00001114 size_t
cristy3ed852e2009-09-05 21:47:34 +00001115 pixel;
1116
1117 /*
1118 Convert bitfield encoded 16-bit PseudoColor scanline.
1119 */
1120 if (bmp_info.compression != BI_RGB &&
1121 bmp_info.compression != BI_BITFIELDS)
1122 ThrowReaderException(CorruptImageError,
1123 "UnrecognizedImageCompression");
1124 bytes_per_line=2*(image->columns+image->columns % 2);
1125 image->storage_class=DirectClass;
cristybb503372010-05-27 20:51:26 +00001126 for (y=(ssize_t) image->rows-1; y >= 0; y--)
cristy3ed852e2009-09-05 21:47:34 +00001127 {
1128 p=pixels+(image->rows-y-1)*bytes_per_line;
1129 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
1130 if (q == (PixelPacket *) NULL)
1131 break;
cristybb503372010-05-27 20:51:26 +00001132 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00001133 {
cristybb503372010-05-27 20:51:26 +00001134 pixel=(size_t) (*p++);
cristy3ed852e2009-09-05 21:47:34 +00001135 pixel|=(*p++) << 8;
1136 red=((pixel & bmp_info.red_mask) << shift.red) >> 16;
1137 if (quantum_bits.red == 5)
1138 red|=((red & 0xe000) >> 5);
1139 if (quantum_bits.red <= 8)
1140 red|=((red & 0xff00) >> 8);
1141 green=((pixel & bmp_info.green_mask) << shift.green) >> 16;
1142 if (quantum_bits.green == 5)
1143 green|=((green & 0xe000) >> 5);
1144 if (quantum_bits.green == 6)
1145 green|=((green & 0xc000) >> 6);
1146 if (quantum_bits.green <= 8)
1147 green|=((green & 0xff00) >> 8);
1148 blue=((pixel & bmp_info.blue_mask) << shift.blue) >> 16;
1149 if (quantum_bits.blue == 5)
1150 blue|=((blue & 0xe000) >> 5);
1151 if (quantum_bits.blue <= 8)
1152 blue|=((blue & 0xff00) >> 8);
1153 opacity=((pixel & bmp_info.alpha_mask) << shift.opacity) >> 16;
1154 if (quantum_bits.opacity <= 8)
1155 opacity|=((opacity & 0xff00) >> 8);
1156 q->red=ScaleShortToQuantum((unsigned short) red);
1157 q->green=ScaleShortToQuantum((unsigned short) green);
1158 q->blue=ScaleShortToQuantum((unsigned short) blue);
cristyce70c172010-01-07 17:15:30 +00001159 SetOpacityPixelComponent(q,OpaqueOpacity);
cristy3ed852e2009-09-05 21:47:34 +00001160 if (image->matte != MagickFalse)
cristyce8c2772010-02-26 23:43:26 +00001161 q->opacity=ScaleShortToQuantum((unsigned short) (65535-opacity));
cristy3ed852e2009-09-05 21:47:34 +00001162 q++;
1163 }
1164 if (SyncAuthenticPixels(image,exception) == MagickFalse)
1165 break;
1166 offset=(MagickOffsetType) (image->rows-y-1);
1167 if (image->previous == (Image *) NULL)
1168 {
cristycee97112010-05-28 00:44:52 +00001169 status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y,
1170 image->rows);
cristy3ed852e2009-09-05 21:47:34 +00001171 if (status == MagickFalse)
1172 break;
1173 }
1174 }
1175 break;
1176 }
1177 case 24:
1178 {
1179 /*
1180 Convert DirectColor scanline.
1181 */
1182 bytes_per_line=4*((image->columns*24+31)/32);
cristybb503372010-05-27 20:51:26 +00001183 for (y=(ssize_t) image->rows-1; y >= 0; y--)
cristy3ed852e2009-09-05 21:47:34 +00001184 {
1185 p=pixels+(image->rows-y-1)*bytes_per_line;
1186 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
1187 if (q == (PixelPacket *) NULL)
1188 break;
cristybb503372010-05-27 20:51:26 +00001189 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00001190 {
1191 q->blue=ScaleCharToQuantum(*p++);
1192 q->green=ScaleCharToQuantum(*p++);
1193 q->red=ScaleCharToQuantum(*p++);
cristyfe12d6c2010-05-07 01:38:41 +00001194 SetOpacityPixelComponent(q,OpaqueOpacity);
cristy3ed852e2009-09-05 21:47:34 +00001195 q++;
1196 }
1197 if (SyncAuthenticPixels(image,exception) == MagickFalse)
1198 break;
1199 offset=(MagickOffsetType) (image->rows-y-1);
1200 if (image->previous == (Image *) NULL)
1201 {
cristycee97112010-05-28 00:44:52 +00001202 status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y,
1203 image->rows);
cristy3ed852e2009-09-05 21:47:34 +00001204 if (status == MagickFalse)
1205 break;
1206 }
1207 }
1208 break;
1209 }
1210 case 32:
1211 {
1212 /*
1213 Convert bitfield encoded DirectColor scanline.
1214 */
1215 if ((bmp_info.compression != BI_RGB) &&
1216 (bmp_info.compression != BI_BITFIELDS))
1217 ThrowReaderException(CorruptImageError,
1218 "UnrecognizedImageCompression");
1219 bytes_per_line=4*(image->columns);
cristybb503372010-05-27 20:51:26 +00001220 for (y=(ssize_t) image->rows-1; y >= 0; y--)
cristy3ed852e2009-09-05 21:47:34 +00001221 {
cristybb503372010-05-27 20:51:26 +00001222 size_t
cristy3ed852e2009-09-05 21:47:34 +00001223 pixel;
1224
1225 p=pixels+(image->rows-y-1)*bytes_per_line;
1226 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
1227 if (q == (PixelPacket *) NULL)
1228 break;
cristybb503372010-05-27 20:51:26 +00001229 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00001230 {
cristybb503372010-05-27 20:51:26 +00001231 pixel=(size_t) (*p++);
cristy3ed852e2009-09-05 21:47:34 +00001232 pixel|=(*p++ << 8);
1233 pixel|=(*p++ << 16);
1234 pixel|=(*p++ << 24);
1235 red=((pixel & bmp_info.red_mask) << shift.red) >> 16;
1236 if (quantum_bits.red == 8)
1237 red|=(red >> 8);
1238 green=((pixel & bmp_info.green_mask) << shift.green) >> 16;
1239 if (quantum_bits.green == 8)
1240 green|=(green >> 8);
1241 blue=((pixel & bmp_info.blue_mask) << shift.blue) >> 16;
1242 if (quantum_bits.blue == 8)
1243 blue|=(blue >> 8);
1244 opacity=((pixel & bmp_info.alpha_mask) << shift.opacity) >> 16;
1245 if (quantum_bits.opacity == 8)
1246 opacity|=(opacity >> 8);
1247 q->red=ScaleShortToQuantum((unsigned short) red);
1248 q->green=ScaleShortToQuantum((unsigned short) green);
1249 q->blue=ScaleShortToQuantum((unsigned short) blue);
cristyce70c172010-01-07 17:15:30 +00001250 SetOpacityPixelComponent(q,OpaqueOpacity);
cristy3ed852e2009-09-05 21:47:34 +00001251 if (image->matte != MagickFalse)
cristy228ab742010-02-24 13:49:34 +00001252 q->opacity=ScaleShortToQuantum((unsigned short) (65535-opacity));
cristy3ed852e2009-09-05 21:47:34 +00001253 q++;
1254 }
1255 if (SyncAuthenticPixels(image,exception) == MagickFalse)
1256 break;
1257 offset=(MagickOffsetType) (image->rows-y-1);
1258 if (image->previous == (Image *) NULL)
1259 {
cristycee97112010-05-28 00:44:52 +00001260 status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y,
1261 image->rows);
cristy3ed852e2009-09-05 21:47:34 +00001262 if (status == MagickFalse)
1263 break;
1264 }
1265 }
1266 break;
1267 }
1268 default:
1269 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
1270 }
1271 pixels=(unsigned char *) RelinquishMagickMemory(pixels);
1272 if (EOFBlob(image) != MagickFalse)
1273 {
1274 ThrowFileException(exception,CorruptImageError,"UnexpectedEndOfFile",
1275 image->filename);
1276 break;
1277 }
1278 if (bmp_info.height < 0)
1279 {
1280 Image
1281 *flipped_image;
1282
1283 /*
1284 Correct image orientation.
1285 */
1286 flipped_image=FlipImage(image,exception);
cristy65e3b002010-04-13 21:20:56 +00001287 if (flipped_image != (Image *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00001288 {
cristy65e3b002010-04-13 21:20:56 +00001289 DuplicateBlob(flipped_image,image);
1290 image=DestroyImage(image);
1291 image=flipped_image;
cristy3ed852e2009-09-05 21:47:34 +00001292 }
cristy3ed852e2009-09-05 21:47:34 +00001293 }
1294 /*
1295 Proceed to next image.
1296 */
1297 if (image_info->number_scenes != 0)
1298 if (image->scene >= (image_info->scene+image_info->number_scenes-1))
1299 break;
1300 *magick='\0';
1301 if (bmp_info.ba_offset != 0)
1302 {
1303 offset=SeekBlob(image,(MagickOffsetType) bmp_info.ba_offset,SEEK_SET);
1304 if (offset < 0)
1305 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
1306 }
1307 count=ReadBlob(image,2,magick);
1308 if ((count == 2) && (IsBMP(magick,2) != MagickFalse))
1309 {
1310 /*
1311 Acquire next image structure.
1312 */
1313 AcquireNextImage(image_info,image);
1314 if (GetNextImageInList(image) == (Image *) NULL)
1315 {
1316 image=DestroyImageList(image);
1317 return((Image *) NULL);
1318 }
1319 image=SyncNextImageInList(image);
1320 status=SetImageProgress(image,LoadImagesTag,TellBlob(image),
1321 GetBlobSize(image));
1322 if (status == MagickFalse)
1323 break;
1324 }
1325 } while (IsBMP(magick,2) != MagickFalse);
1326 (void) CloseBlob(image);
1327 return(GetFirstImageInList(image));
1328}
1329
1330/*
1331%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1332% %
1333% %
1334% %
1335% R e g i s t e r B M P I m a g e %
1336% %
1337% %
1338% %
1339%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1340%
1341% RegisterBMPImage() adds attributes for the BMP image format to
1342% the list of supported formats. The attributes include the image format
1343% tag, a method to read and/or write the format, whether the format
1344% supports the saving of more than one frame to the same file or blob,
1345% whether the format supports native in-memory I/O, and a brief
1346% description of the format.
1347%
1348% The format of the RegisterBMPImage method is:
1349%
cristybb503372010-05-27 20:51:26 +00001350% size_t RegisterBMPImage(void)
cristy3ed852e2009-09-05 21:47:34 +00001351%
1352*/
cristybb503372010-05-27 20:51:26 +00001353ModuleExport size_t RegisterBMPImage(void)
cristy3ed852e2009-09-05 21:47:34 +00001354{
1355 MagickInfo
1356 *entry;
1357
1358 entry=SetMagickInfo("BMP");
1359 entry->decoder=(DecodeImageHandler *) ReadBMPImage;
1360 entry->encoder=(EncodeImageHandler *) WriteBMPImage;
1361 entry->magick=(IsImageFormatHandler *) IsBMP;
cristy2e3d5242010-04-16 23:44:55 +00001362 entry->description=ConstantString("Microsoft Windows bitmap image");
cristy3ed852e2009-09-05 21:47:34 +00001363 entry->module=ConstantString("BMP");
1364 entry->adjoin=MagickFalse;
1365 entry->seekable_stream=MagickTrue;
1366 (void) RegisterMagickInfo(entry);
1367 entry=SetMagickInfo("BMP2");
1368 entry->encoder=(EncodeImageHandler *) WriteBMPImage;
1369 entry->magick=(IsImageFormatHandler *) IsBMP;
cristy2dd92ae2010-04-04 16:10:59 +00001370 entry->description=ConstantString("Microsoft Windows bitmap image (V2)");
cristy3ed852e2009-09-05 21:47:34 +00001371 entry->module=ConstantString("BMP");
1372 entry->adjoin=MagickFalse;
1373 entry->seekable_stream=MagickTrue;
1374 (void) RegisterMagickInfo(entry);
1375 entry=SetMagickInfo("BMP3");
1376 entry->encoder=(EncodeImageHandler *) WriteBMPImage;
1377 entry->magick=(IsImageFormatHandler *) IsBMP;
cristy2dd92ae2010-04-04 16:10:59 +00001378 entry->description=ConstantString("Microsoft Windows bitmap image (V3)");
cristy3ed852e2009-09-05 21:47:34 +00001379 entry->module=ConstantString("BMP");
1380 entry->adjoin=MagickFalse;
1381 entry->seekable_stream=MagickTrue;
1382 (void) RegisterMagickInfo(entry);
1383 return(MagickImageCoderSignature);
1384}
1385
1386/*
1387%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1388% %
1389% %
1390% %
1391% U n r e g i s t e r B M P I m a g e %
1392% %
1393% %
1394% %
1395%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1396%
1397% UnregisterBMPImage() removes format registrations made by the
1398% BMP module from the list of supported formats.
1399%
1400% The format of the UnregisterBMPImage method is:
1401%
1402% UnregisterBMPImage(void)
1403%
1404*/
1405ModuleExport void UnregisterBMPImage(void)
1406{
1407 (void) UnregisterMagickInfo("BMP");
1408 (void) UnregisterMagickInfo("BMP2");
1409 (void) UnregisterMagickInfo("BMP3");
1410}
1411
1412/*
1413%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1414% %
1415% %
1416% %
1417% W r i t e B M P I m a g e %
1418% %
1419% %
1420% %
1421%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1422%
1423% WriteBMPImage() writes an image in Microsoft Windows bitmap encoded
1424% image format, version 3 for Windows or (if the image has a matte channel)
1425% version 4.
1426%
1427% The format of the WriteBMPImage method is:
1428%
1429% MagickBooleanType WriteBMPImage(const ImageInfo *image_info,Image *image)
1430%
1431% A description of each parameter follows.
1432%
1433% o image_info: the image info.
1434%
1435% o image: The image.
1436%
1437*/
1438static MagickBooleanType WriteBMPImage(const ImageInfo *image_info,Image *image)
1439{
1440 BMPInfo
1441 bmp_info;
1442
1443 const StringInfo
1444 *profile;
1445
cristybb503372010-05-27 20:51:26 +00001446 ssize_t
cristy3ed852e2009-09-05 21:47:34 +00001447 y;
1448
1449 MagickBooleanType
1450 have_color_info,
1451 status;
1452
1453 MagickOffsetType
1454 scene;
1455
1456 register const IndexPacket
1457 *indexes;
1458
1459 register const PixelPacket
1460 *p;
1461
cristybb503372010-05-27 20:51:26 +00001462 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00001463 i,
1464 x;
1465
1466 register unsigned char
1467 *q;
1468
1469 unsigned char
1470 *bmp_data,
1471 *pixels;
1472
cristybb503372010-05-27 20:51:26 +00001473 size_t
cristy3ed852e2009-09-05 21:47:34 +00001474 bytes_per_line,
1475 type;
1476
1477 /*
1478 Open output image file.
1479 */
1480 assert(image_info != (const ImageInfo *) NULL);
1481 assert(image_info->signature == MagickSignature);
1482 assert(image != (Image *) NULL);
1483 assert(image->signature == MagickSignature);
1484 if (image->debug != MagickFalse)
1485 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1486 status=OpenBlob(image_info,image,WriteBinaryBlobMode,&image->exception);
1487 if (status == MagickFalse)
1488 return(status);
1489 type=4;
1490 if (LocaleCompare(image_info->magick,"BMP2") == 0)
1491 type=2;
1492 else
1493 if (LocaleCompare(image_info->magick,"BMP3") == 0)
1494 type=3;
1495 scene=0;
1496 do
1497 {
1498 /*
1499 Initialize BMP raster file header.
1500 */
1501 if (image->colorspace != RGBColorspace)
1502 (void) TransformImageColorspace(image,RGBColorspace);
1503 (void) ResetMagickMemory(&bmp_info,0,sizeof(bmp_info));
1504 bmp_info.file_size=14+12;
1505 if (type > 2)
1506 bmp_info.file_size+=28;
1507 bmp_info.offset_bits=bmp_info.file_size;
1508 bmp_info.compression=BI_RGB;
1509 if ((image->storage_class == PseudoClass) && (image->colors > 256))
1510 (void) SetImageStorageClass(image,DirectClass);
1511 if (image->storage_class != DirectClass)
1512 {
1513 /*
1514 Colormapped BMP raster.
1515 */
1516 bmp_info.bits_per_pixel=8;
1517 if (image->colors <= 2)
1518 bmp_info.bits_per_pixel=1;
1519 else
1520 if (image->colors <= 16)
1521 bmp_info.bits_per_pixel=4;
1522 else
1523 if (image->colors <= 256)
1524 bmp_info.bits_per_pixel=8;
1525 if (image_info->compression == RLECompression)
1526 bmp_info.bits_per_pixel=8;
1527 bmp_info.number_colors=1U << bmp_info.bits_per_pixel;
1528 if (image->matte != MagickFalse)
1529 (void) SetImageStorageClass(image,DirectClass);
1530 else
cristybb503372010-05-27 20:51:26 +00001531 if ((size_t) bmp_info.number_colors < image->colors)
cristy3ed852e2009-09-05 21:47:34 +00001532 (void) SetImageStorageClass(image,DirectClass);
1533 else
1534 {
1535 bmp_info.file_size+=3*(1UL << bmp_info.bits_per_pixel);
1536 bmp_info.offset_bits+=3*(1UL << bmp_info.bits_per_pixel);
1537 if (type > 2)
1538 {
1539 bmp_info.file_size+=(1UL << bmp_info.bits_per_pixel);
1540 bmp_info.offset_bits+=(1UL << bmp_info.bits_per_pixel);
1541 }
1542 }
1543 }
1544 if (image->storage_class == DirectClass)
1545 {
1546 /*
1547 Full color BMP raster.
1548 */
1549 bmp_info.number_colors=0;
1550 bmp_info.bits_per_pixel=(unsigned short)
1551 ((type > 3) && (image->matte != MagickFalse) ? 32 : 24);
1552 bmp_info.compression=(unsigned int) ((type > 3) &&
1553 (image->matte != MagickFalse) ? BI_BITFIELDS : BI_RGB);
1554 }
1555 bytes_per_line=4*((image->columns*bmp_info.bits_per_pixel+31)/32);
1556 bmp_info.ba_offset=0;
1557 profile=GetImageProfile(image,"icc");
1558 have_color_info=(image->rendering_intent != UndefinedIntent) ||
1559 (profile != (StringInfo *) NULL) || (image->gamma != 0.0) ? MagickTrue :
1560 MagickFalse;
1561 if (type == 2)
1562 bmp_info.size=12;
1563 else
1564 if ((type == 3) || ((image->matte == MagickFalse) &&
1565 (have_color_info == MagickFalse)))
1566 {
1567 type=3;
1568 bmp_info.size=40;
1569 }
1570 else
1571 {
1572 int
1573 extra_size;
1574
1575 bmp_info.size=108;
1576 extra_size=68;
1577 if ((image->rendering_intent != UndefinedIntent) ||
1578 (profile != (StringInfo *) NULL))
1579 {
1580 bmp_info.size=124;
1581 extra_size+=16;
1582 }
1583 bmp_info.file_size+=extra_size;
1584 bmp_info.offset_bits+=extra_size;
1585 }
cristybb503372010-05-27 20:51:26 +00001586 bmp_info.width=(ssize_t) image->columns;
1587 bmp_info.height=(ssize_t) image->rows;
cristy3ed852e2009-09-05 21:47:34 +00001588 bmp_info.planes=1;
1589 bmp_info.image_size=(unsigned int) (bytes_per_line*image->rows);
1590 bmp_info.file_size+=bmp_info.image_size;
1591 bmp_info.x_pixels=75*39;
1592 bmp_info.y_pixels=75*39;
1593 switch (image->units)
1594 {
1595 case UndefinedResolution:
1596 case PixelsPerInchResolution:
1597 {
1598 bmp_info.x_pixels=(unsigned int) (100.0*image->x_resolution/2.54);
1599 bmp_info.y_pixels=(unsigned int) (100.0*image->y_resolution/2.54);
1600 break;
1601 }
1602 case PixelsPerCentimeterResolution:
1603 {
1604 bmp_info.x_pixels=(unsigned int) (100.0*image->x_resolution);
1605 bmp_info.y_pixels=(unsigned int) (100.0*image->y_resolution);
1606 break;
1607 }
1608 }
1609 bmp_info.colors_important=bmp_info.number_colors;
1610 /*
1611 Convert MIFF to BMP raster pixels.
1612 */
1613 pixels=(unsigned char *) AcquireQuantumMemory((size_t) bmp_info.image_size,
1614 sizeof(*pixels));
1615 if (pixels == (unsigned char *) NULL)
1616 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
1617 (void) ResetMagickMemory(pixels,0,(size_t) bmp_info.image_size);
1618 switch (bmp_info.bits_per_pixel)
1619 {
1620 case 1:
1621 {
cristybb503372010-05-27 20:51:26 +00001622 size_t
cristy3ed852e2009-09-05 21:47:34 +00001623 bit,
1624 byte;
1625
1626 /*
1627 Convert PseudoClass image to a BMP monochrome image.
1628 */
cristybb503372010-05-27 20:51:26 +00001629 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00001630 {
1631 p=GetVirtualPixels(image,0,y,image->columns,1,&image->exception);
1632 if (p == (const PixelPacket *) NULL)
1633 break;
1634 indexes=GetVirtualIndexQueue(image);
1635 q=pixels+(image->rows-y-1)*bytes_per_line;
1636 bit=0;
1637 byte=0;
cristybb503372010-05-27 20:51:26 +00001638 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00001639 {
1640 byte<<=1;
1641 byte|=indexes[x] != 0 ? 0x01 : 0x00;
1642 bit++;
1643 if (bit == 8)
1644 {
1645 *q++=(unsigned char) byte;
1646 bit=0;
1647 byte=0;
1648 }
1649 }
1650 if (bit != 0)
1651 {
1652 *q++=(unsigned char) (byte << (8-bit));
1653 x++;
1654 }
cristybb503372010-05-27 20:51:26 +00001655 for (x=(ssize_t) (image->columns+7)/8; x < (ssize_t) bytes_per_line; x++)
cristy3ed852e2009-09-05 21:47:34 +00001656 *q++=0x00;
1657 if (image->previous == (Image *) NULL)
1658 {
cristycee97112010-05-28 00:44:52 +00001659 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
1660 image->rows);
cristy3ed852e2009-09-05 21:47:34 +00001661 if (status == MagickFalse)
1662 break;
1663 }
1664 }
1665 break;
1666 }
1667 case 4:
1668 {
cristybb503372010-05-27 20:51:26 +00001669 size_t
cristy3ed852e2009-09-05 21:47:34 +00001670 nibble,
1671 byte;
1672
1673 /*
1674 Convert PseudoClass image to a BMP monochrome image.
1675 */
cristybb503372010-05-27 20:51:26 +00001676 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00001677 {
1678 p=GetVirtualPixels(image,0,y,image->columns,1,&image->exception);
1679 if (p == (const PixelPacket *) NULL)
1680 break;
1681 indexes=GetVirtualIndexQueue(image);
1682 q=pixels+(image->rows-y-1)*bytes_per_line;
1683 nibble=0;
1684 byte=0;
cristybb503372010-05-27 20:51:26 +00001685 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00001686 {
1687 byte<<=4;
cristybb503372010-05-27 20:51:26 +00001688 byte|=((size_t) indexes[x] & 0x0f);
cristy3ed852e2009-09-05 21:47:34 +00001689 nibble++;
1690 if (nibble == 2)
1691 {
1692 *q++=(unsigned char) byte;
1693 nibble=0;
1694 byte=0;
1695 }
1696 }
1697 if (nibble != 0)
1698 {
1699 *q++=(unsigned char) (byte << 4);
1700 x++;
1701 }
cristybb503372010-05-27 20:51:26 +00001702 for (x=(ssize_t) (image->columns+1)/2; x < (ssize_t) bytes_per_line; x++)
cristy3ed852e2009-09-05 21:47:34 +00001703 *q++=0x00;
1704 if (image->previous == (Image *) NULL)
1705 {
cristycee97112010-05-28 00:44:52 +00001706 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
1707 image->rows);
cristy3ed852e2009-09-05 21:47:34 +00001708 if (status == MagickFalse)
1709 break;
1710 }
1711 }
1712 break;
1713 }
1714 case 8:
1715 {
1716 /*
1717 Convert PseudoClass packet to BMP pixel.
1718 */
cristybb503372010-05-27 20:51:26 +00001719 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00001720 {
1721 p=GetVirtualPixels(image,0,y,image->columns,1,&image->exception);
1722 if (p == (const PixelPacket *) NULL)
1723 break;
1724 indexes=GetVirtualIndexQueue(image);
1725 q=pixels+(image->rows-y-1)*bytes_per_line;
cristybb503372010-05-27 20:51:26 +00001726 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00001727 *q++=(unsigned char) indexes[x];
cristybb503372010-05-27 20:51:26 +00001728 for ( ; x < (ssize_t) bytes_per_line; x++)
cristy3ed852e2009-09-05 21:47:34 +00001729 *q++=0x00;
1730 if (image->previous == (Image *) NULL)
1731 {
cristycee97112010-05-28 00:44:52 +00001732 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
1733 image->rows);
cristy3ed852e2009-09-05 21:47:34 +00001734 if (status == MagickFalse)
1735 break;
1736 }
1737 }
1738 break;
1739 }
1740 case 24:
1741 {
1742 /*
1743 Convert DirectClass packet to BMP BGR888.
1744 */
cristybb503372010-05-27 20:51:26 +00001745 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00001746 {
1747 p=GetVirtualPixels(image,0,y,image->columns,1,&image->exception);
1748 if (p == (const PixelPacket *) NULL)
1749 break;
1750 q=pixels+(image->rows-y-1)*bytes_per_line;
cristybb503372010-05-27 20:51:26 +00001751 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00001752 {
cristyce70c172010-01-07 17:15:30 +00001753 *q++=ScaleQuantumToChar(GetBluePixelComponent(p));
1754 *q++=ScaleQuantumToChar(GetGreenPixelComponent(p));
1755 *q++=ScaleQuantumToChar(GetRedPixelComponent(p));
cristy3ed852e2009-09-05 21:47:34 +00001756 p++;
1757 }
cristybb503372010-05-27 20:51:26 +00001758 for (x=3L*(ssize_t) image->columns; x < (ssize_t) bytes_per_line; x++)
cristy3ed852e2009-09-05 21:47:34 +00001759 *q++=0x00;
1760 if (image->previous == (Image *) NULL)
1761 {
cristycee97112010-05-28 00:44:52 +00001762 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
1763 image->rows);
cristy3ed852e2009-09-05 21:47:34 +00001764 if (status == MagickFalse)
1765 break;
1766 }
1767 }
1768 break;
1769 }
1770 case 32:
1771 {
1772 /*
1773 Convert DirectClass packet to ARGB8888 pixel.
1774 */
cristybb503372010-05-27 20:51:26 +00001775 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00001776 {
1777 p=GetVirtualPixels(image,0,y,image->columns,1,&image->exception);
1778 if (p == (const PixelPacket *) NULL)
1779 break;
1780 q=pixels+(image->rows-y-1)*bytes_per_line;
cristybb503372010-05-27 20:51:26 +00001781 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00001782 {
cristyce70c172010-01-07 17:15:30 +00001783 *q++=ScaleQuantumToChar(GetBluePixelComponent(p));
1784 *q++=ScaleQuantumToChar(GetGreenPixelComponent(p));
1785 *q++=ScaleQuantumToChar(GetRedPixelComponent(p));
cristy228ab742010-02-24 13:49:34 +00001786 *q++=ScaleQuantumToChar(QuantumRange-GetOpacityPixelComponent(p));
cristy3ed852e2009-09-05 21:47:34 +00001787 p++;
1788 }
1789 if (image->previous == (Image *) NULL)
1790 {
cristycee97112010-05-28 00:44:52 +00001791 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
1792 image->rows);
cristy3ed852e2009-09-05 21:47:34 +00001793 if (status == MagickFalse)
1794 break;
1795 }
1796 }
1797 break;
1798 }
1799 }
1800 if ((type > 2) && (bmp_info.bits_per_pixel == 8))
1801 if (image_info->compression != NoCompression)
1802 {
1803 size_t
1804 length;
1805
1806 /*
1807 Convert run-length encoded raster pixels.
1808 */
1809 length=(size_t) (2*(bytes_per_line+2)*(image->rows+2)+2);
1810 bmp_data=(unsigned char *) NULL;
1811 if (~length >= bytes_per_line)
1812 bmp_data=(unsigned char *) AcquireQuantumMemory(length+
1813 bytes_per_line,sizeof(*bmp_data));
1814 if (bmp_data == (unsigned char *) NULL)
1815 {
1816 pixels=(unsigned char *) RelinquishMagickMemory(pixels);
1817 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
1818 }
1819 bmp_info.file_size-=bmp_info.image_size;
1820 bmp_info.image_size=(unsigned int) EncodeImage(image,bytes_per_line,
1821 pixels,bmp_data);
1822 bmp_info.file_size+=bmp_info.image_size;
1823 pixels=(unsigned char *) RelinquishMagickMemory(pixels);
1824 pixels=bmp_data;
1825 bmp_info.compression=BI_RLE8;
1826 }
1827 /*
1828 Write BMP for Windows, all versions, 14-byte header.
1829 */
1830 if (image->debug != MagickFalse)
1831 {
1832 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
cristye8c25f92010-06-03 00:53:06 +00001833 " Writing BMP version %.20g datastream",(double) type);
cristy3ed852e2009-09-05 21:47:34 +00001834 if (image->storage_class == DirectClass)
1835 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1836 " Storage class=DirectClass");
1837 else
1838 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1839 " Storage class=PseudoClass");
1840 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
cristye8c25f92010-06-03 00:53:06 +00001841 " Image depth=%.20g",(double) image->depth);
cristy3ed852e2009-09-05 21:47:34 +00001842 if (image->matte != MagickFalse)
1843 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1844 " Matte=True");
1845 else
1846 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1847 " Matte=MagickFalse");
1848 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
cristye8c25f92010-06-03 00:53:06 +00001849 " BMP bits_per_pixel=%.20g",(double) bmp_info.bits_per_pixel);
cristy3ed852e2009-09-05 21:47:34 +00001850 switch ((int) bmp_info.compression)
1851 {
1852 case BI_RGB:
1853 {
1854 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1855 " Compression=BI_RGB");
1856 break;
1857 }
1858 case BI_RLE8:
1859 {
1860 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1861 " Compression=BI_RLE8");
1862 break;
1863 }
1864 case BI_BITFIELDS:
1865 {
1866 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1867 " Compression=BI_BITFIELDS");
1868 break;
1869 }
1870 default:
1871 {
1872 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1873 " Compression=UNKNOWN (%u)",bmp_info.compression);
1874 break;
1875 }
1876 }
1877 if (bmp_info.number_colors == 0)
1878 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1879 " Number_colors=unspecified");
1880 else
1881 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1882 " Number_colors=%u",bmp_info.number_colors);
1883 }
1884 (void) WriteBlob(image,2,(unsigned char *) "BM");
1885 (void) WriteBlobLSBLong(image,bmp_info.file_size);
1886 (void) WriteBlobLSBLong(image,bmp_info.ba_offset); /* always 0 */
1887 (void) WriteBlobLSBLong(image,bmp_info.offset_bits);
1888 if (type == 2)
1889 {
1890 /*
1891 Write 12-byte version 2 bitmap header.
1892 */
1893 (void) WriteBlobLSBLong(image,bmp_info.size);
1894 (void) WriteBlobLSBShort(image,(unsigned short) bmp_info.width);
1895 (void) WriteBlobLSBShort(image,(unsigned short) bmp_info.height);
1896 (void) WriteBlobLSBShort(image,bmp_info.planes);
1897 (void) WriteBlobLSBShort(image,bmp_info.bits_per_pixel);
1898 }
1899 else
1900 {
1901 /*
1902 Write 40-byte version 3+ bitmap header.
1903 */
1904 (void) WriteBlobLSBLong(image,bmp_info.size);
1905 (void) WriteBlobLSBLong(image,(unsigned int) bmp_info.width);
1906 (void) WriteBlobLSBLong(image,(unsigned int) bmp_info.height);
1907 (void) WriteBlobLSBShort(image,bmp_info.planes);
1908 (void) WriteBlobLSBShort(image,bmp_info.bits_per_pixel);
1909 (void) WriteBlobLSBLong(image,bmp_info.compression);
1910 (void) WriteBlobLSBLong(image,bmp_info.image_size);
1911 (void) WriteBlobLSBLong(image,bmp_info.x_pixels);
1912 (void) WriteBlobLSBLong(image,bmp_info.y_pixels);
1913 (void) WriteBlobLSBLong(image,bmp_info.number_colors);
1914 (void) WriteBlobLSBLong(image,bmp_info.colors_important);
1915 }
1916 if ((type > 3) && ((image->matte != MagickFalse) ||
1917 (have_color_info != MagickFalse)))
1918 {
1919 /*
1920 Write the rest of the 108-byte BMP Version 4 header.
1921 */
1922 (void) WriteBlobLSBLong(image,0x00ff0000U); /* Red mask */
1923 (void) WriteBlobLSBLong(image,0x0000ff00U); /* Green mask */
1924 (void) WriteBlobLSBLong(image,0x000000ffU); /* Blue mask */
1925 (void) WriteBlobLSBLong(image,0xff000000U); /* Alpha mask */
1926 (void) WriteBlobLSBLong(image,0x00000001U); /* CSType==Calib. RGB */
1927 (void) WriteBlobLSBLong(image,(unsigned int)
1928 image->chromaticity.red_primary.x*0x3ffffff);
1929 (void) WriteBlobLSBLong(image,(unsigned int)
1930 image->chromaticity.red_primary.y*0x3ffffff);
1931 (void) WriteBlobLSBLong(image,(unsigned int)
1932 (1.000f-(image->chromaticity.red_primary.x+
1933 image->chromaticity.red_primary.y)*0x3ffffff));
1934 (void) WriteBlobLSBLong(image,(unsigned int)
1935 image->chromaticity.green_primary.x*0x3ffffff);
1936 (void) WriteBlobLSBLong(image,(unsigned int)
1937 image->chromaticity.green_primary.y*0x3ffffff);
1938 (void) WriteBlobLSBLong(image,(unsigned int)
1939 (1.000f-(image->chromaticity.green_primary.x+
1940 image->chromaticity.green_primary.y)*0x3ffffff));
1941 (void) WriteBlobLSBLong(image,(unsigned int)
1942 image->chromaticity.blue_primary.x*0x3ffffff);
1943 (void) WriteBlobLSBLong(image,(unsigned int)
1944 image->chromaticity.blue_primary.y*0x3ffffff);
1945 (void) WriteBlobLSBLong(image,(unsigned int)
1946 (1.000f-(image->chromaticity.blue_primary.x+
1947 image->chromaticity.blue_primary.y)*0x3ffffff));
1948 (void) WriteBlobLSBLong(image,(unsigned int)
1949 bmp_info.gamma_scale.x*0xffff);
1950 (void) WriteBlobLSBLong(image,(unsigned int)
1951 bmp_info.gamma_scale.y*0xffff);
1952 (void) WriteBlobLSBLong(image,(unsigned int)
1953 bmp_info.gamma_scale.z*0xffff);
1954 if ((image->rendering_intent != UndefinedIntent) ||
1955 (profile != (StringInfo *) NULL))
1956 {
cristybb503372010-05-27 20:51:26 +00001957 ssize_t
cristy3ed852e2009-09-05 21:47:34 +00001958 intent;
1959
1960 switch ((int) image->rendering_intent)
1961 {
1962 case SaturationIntent:
1963 {
1964 intent=LCS_GM_BUSINESS;
1965 break;
1966 }
1967 case RelativeIntent:
1968 {
1969 intent=LCS_GM_GRAPHICS;
1970 break;
1971 }
1972 case PerceptualIntent:
1973 {
1974 intent=LCS_GM_IMAGES;
1975 break;
1976 }
1977 case AbsoluteIntent:
1978 {
1979 intent=LCS_GM_ABS_COLORIMETRIC;
1980 break;
1981 }
1982 default:
1983 {
1984 intent=0;
1985 break;
1986 }
1987 }
1988 (void) WriteBlobLSBLong(image,(unsigned int) intent);
1989 (void) WriteBlobLSBLong(image,0x00); /* dummy profile data */
1990 (void) WriteBlobLSBLong(image,0x00); /* dummy profile length */
1991 (void) WriteBlobLSBLong(image,0x00); /* reserved */
1992 }
1993 }
1994 if (image->storage_class == PseudoClass)
1995 {
1996 unsigned char
1997 *bmp_colormap;
1998
1999 /*
2000 Dump colormap to file.
2001 */
2002 if (image->debug != MagickFalse)
2003 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
cristye8c25f92010-06-03 00:53:06 +00002004 " Colormap: %.20g entries",(double) image->colors);
cristy3ed852e2009-09-05 21:47:34 +00002005 bmp_colormap=(unsigned char *) AcquireQuantumMemory((size_t) (1UL <<
2006 bmp_info.bits_per_pixel),4*sizeof(*bmp_colormap));
2007 if (bmp_colormap == (unsigned char *) NULL)
2008 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
2009 q=bmp_colormap;
cristybb503372010-05-27 20:51:26 +00002010 for (i=0; i < (ssize_t) MagickMin((ssize_t) image->colors,(ssize_t) bmp_info.number_colors); i++)
cristy3ed852e2009-09-05 21:47:34 +00002011 {
2012 *q++=ScaleQuantumToChar(image->colormap[i].blue);
2013 *q++=ScaleQuantumToChar(image->colormap[i].green);
2014 *q++=ScaleQuantumToChar(image->colormap[i].red);
2015 if (type > 2)
2016 *q++=(unsigned char) 0x0;
2017 }
cristybb503372010-05-27 20:51:26 +00002018 for ( ; i < (ssize_t) (1UL << bmp_info.bits_per_pixel); i++)
cristy3ed852e2009-09-05 21:47:34 +00002019 {
2020 *q++=(unsigned char) 0x00;
2021 *q++=(unsigned char) 0x00;
2022 *q++=(unsigned char) 0x00;
2023 if (type > 2)
2024 *q++=(unsigned char) 0x00;
2025 }
2026 if (type <= 2)
2027 (void) WriteBlob(image,(size_t) (3*(1L << bmp_info.bits_per_pixel)),
2028 bmp_colormap);
2029 else
2030 (void) WriteBlob(image,(size_t) (4*(1L << bmp_info.bits_per_pixel)),
2031 bmp_colormap);
2032 bmp_colormap=(unsigned char *) RelinquishMagickMemory(bmp_colormap);
2033 }
2034 if (image->debug != MagickFalse)
2035 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
2036 " Pixels: %u bytes",bmp_info.image_size);
2037 (void) WriteBlob(image,(size_t) bmp_info.image_size,pixels);
2038 pixels=(unsigned char *) RelinquishMagickMemory(pixels);
2039 if (GetNextImageInList(image) == (Image *) NULL)
2040 break;
2041 image=SyncNextImageInList(image);
2042 status=SetImageProgress(image,SaveImagesTag,scene++,
2043 GetImageListLength(image));
2044 if (status == MagickFalse)
2045 break;
2046 } while (image_info->adjoin != MagickFalse);
2047 (void) CloseBlob(image);
2048 return(MagickTrue);
2049}