blob: e3e4c52c41d7d8fd627cc9fb2fa039f8d4488c6d [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)
cristyde60ea72010-07-09 23:12:09 +0000913 {
914 pixels=(unsigned char *) RelinquishMagickMemory(pixels);
915 ThrowReaderException(CorruptImageError,
916 "InsufficientImageDataInFile");
917 }
cristy3ed852e2009-09-05 21:47:34 +0000918 }
919 else
920 {
921 /*
922 Convert run-length encoded raster pixels.
923 */
924 status=DecodeImage(image,bmp_info.compression,pixels);
925 if (status == MagickFalse)
cristyde60ea72010-07-09 23:12:09 +0000926 {
927 pixels=(unsigned char *) RelinquishMagickMemory(pixels);
928 ThrowReaderException(CorruptImageError,
929 "UnableToRunlengthDecodeImage");
930 }
cristy3ed852e2009-09-05 21:47:34 +0000931 }
932 /*
933 Initialize image structure.
934 */
935 image->x_resolution=(double) bmp_info.x_pixels/100.0;
936 image->y_resolution=(double) bmp_info.y_pixels/100.0;
937 image->units=PixelsPerCentimeterResolution;
938 /*
939 Convert BMP raster image to pixel packets.
940 */
941 if (bmp_info.compression == BI_RGB)
942 {
943 bmp_info.alpha_mask=0;
944 bmp_info.red_mask=0x00ff0000U;
945 bmp_info.green_mask=0x0000ff00U;
946 bmp_info.blue_mask=0x000000ffU;
947 if (bmp_info.bits_per_pixel == 16)
948 {
949 /*
950 RGB555.
951 */
952 bmp_info.red_mask=0x00007c00U;
953 bmp_info.green_mask=0x000003e0U;
954 bmp_info.blue_mask=0x0000001fU;
955 }
956 }
957 if ((bmp_info.bits_per_pixel == 16) || (bmp_info.bits_per_pixel == 32))
958 {
cristybb503372010-05-27 20:51:26 +0000959 register size_t
cristy3ed852e2009-09-05 21:47:34 +0000960 sample;
961
962 /*
963 Get shift and quantum bits info from bitfield masks.
964 */
965 (void) ResetMagickMemory(&shift,0,sizeof(shift));
966 (void) ResetMagickMemory(&quantum_bits,0,sizeof(quantum_bits));
967 if (bmp_info.red_mask != 0)
968 while (((bmp_info.red_mask << shift.red) & 0x80000000UL) == 0)
969 shift.red++;
970 if (bmp_info.green_mask != 0)
971 while (((bmp_info.green_mask << shift.green) & 0x80000000UL) == 0)
972 shift.green++;
973 if (bmp_info.blue_mask != 0)
974 while (((bmp_info.blue_mask << shift.blue) & 0x80000000UL) == 0)
975 shift.blue++;
976 if (bmp_info.alpha_mask != 0)
977 while (((bmp_info.alpha_mask << shift.opacity) & 0x80000000UL) == 0)
978 shift.opacity++;
979 sample=shift.red;
980 while (((bmp_info.red_mask << sample) & 0x80000000UL) != 0)
981 sample++;
982 quantum_bits.red=(Quantum) (sample-shift.red);
983 sample=shift.green;
984 while (((bmp_info.green_mask << sample) & 0x80000000UL) != 0)
985 sample++;
986 quantum_bits.green=(Quantum) (sample-shift.green);
987 sample=shift.blue;
988 while (((bmp_info.blue_mask << sample) & 0x80000000UL) != 0)
989 sample++;
990 quantum_bits.blue=(Quantum) (sample-shift.blue);
991 sample=shift.opacity;
992 while (((bmp_info.alpha_mask << sample) & 0x80000000UL) != 0)
993 sample++;
994 quantum_bits.opacity=(Quantum) (sample-shift.opacity);
995 }
996 switch (bmp_info.bits_per_pixel)
997 {
998 case 1:
999 {
1000 /*
1001 Convert bitmap scanline.
1002 */
cristybb503372010-05-27 20:51:26 +00001003 for (y=(ssize_t) image->rows-1; y >= 0; y--)
cristy3ed852e2009-09-05 21:47:34 +00001004 {
1005 p=pixels+(image->rows-y-1)*bytes_per_line;
1006 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
1007 if (q == (PixelPacket *) NULL)
1008 break;
1009 indexes=GetAuthenticIndexQueue(image);
cristybb503372010-05-27 20:51:26 +00001010 for (x=0; x < ((ssize_t) image->columns-7); x+=8)
cristy3ed852e2009-09-05 21:47:34 +00001011 {
1012 for (bit=0; bit < 8; bit++)
1013 {
1014 index=(IndexPacket) (((*p) & (0x80 >> bit)) != 0 ? 0x01 : 0x00);
1015 indexes[x+bit]=index;
cristybb503372010-05-27 20:51:26 +00001016 *q++=image->colormap[(ssize_t) index];
cristy3ed852e2009-09-05 21:47:34 +00001017 }
1018 p++;
1019 }
1020 if ((image->columns % 8) != 0)
1021 {
1022 for (bit=0; bit < (image->columns % 8); bit++)
1023 {
1024 index=(IndexPacket) (((*p) & (0x80 >> bit)) != 0 ? 0x01 : 0x00);
1025 indexes[x+bit]=index;
cristybb503372010-05-27 20:51:26 +00001026 *q++=image->colormap[(ssize_t) index];
cristy3ed852e2009-09-05 21:47:34 +00001027 }
1028 p++;
1029 }
1030 if (SyncAuthenticPixels(image,exception) == MagickFalse)
1031 break;
1032 if (image->previous == (Image *) NULL)
1033 {
cristycee97112010-05-28 00:44:52 +00001034 status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y,
1035 image->rows);
cristy3ed852e2009-09-05 21:47:34 +00001036 if (status == MagickFalse)
1037 break;
1038 }
1039 }
1040 break;
1041 }
1042 case 4:
1043 {
1044 /*
1045 Convert PseudoColor scanline.
1046 */
cristybb503372010-05-27 20:51:26 +00001047 for (y=(ssize_t) image->rows-1; y >= 0; y--)
cristy3ed852e2009-09-05 21:47:34 +00001048 {
1049 p=pixels+(image->rows-y-1)*bytes_per_line;
1050 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
1051 if (q == (PixelPacket *) NULL)
1052 break;
1053 indexes=GetAuthenticIndexQueue(image);
cristybb503372010-05-27 20:51:26 +00001054 for (x=0; x < ((ssize_t) image->columns-1); x+=2)
cristy3ed852e2009-09-05 21:47:34 +00001055 {
1056 index=ConstrainColormapIndex(image,(*p >> 4) & 0x0f);
1057 indexes[x]=index;
cristybb503372010-05-27 20:51:26 +00001058 *q++=image->colormap[(ssize_t) index];
cristy3ed852e2009-09-05 21:47:34 +00001059 index=ConstrainColormapIndex(image,*p & 0x0f);
1060 indexes[x+1]=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 ((image->columns % 2) != 0)
1065 {
1066 index=ConstrainColormapIndex(image,(*p >> 4) & 0xf);
1067 indexes[x]=index;
cristybb503372010-05-27 20:51:26 +00001068 *q++=image->colormap[(ssize_t) index];
cristy3ed852e2009-09-05 21:47:34 +00001069 p++;
1070 }
1071 if (SyncAuthenticPixels(image,exception) == MagickFalse)
1072 break;
1073 if (image->previous == (Image *) NULL)
1074 {
cristycee97112010-05-28 00:44:52 +00001075 status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y,
1076 image->rows);
cristy3ed852e2009-09-05 21:47:34 +00001077 if (status == MagickFalse)
1078 break;
1079 }
1080 }
1081 break;
1082 }
1083 case 8:
1084 {
1085 /*
1086 Convert PseudoColor scanline.
1087 */
1088 if ((bmp_info.compression == BI_RLE8) ||
1089 (bmp_info.compression == BI_RLE4))
1090 bytes_per_line=image->columns;
cristybb503372010-05-27 20:51:26 +00001091 for (y=(ssize_t) image->rows-1; y >= 0; y--)
cristy3ed852e2009-09-05 21:47:34 +00001092 {
1093 p=pixels+(image->rows-y-1)*bytes_per_line;
1094 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
1095 if (q == (PixelPacket *) NULL)
1096 break;
1097 indexes=GetAuthenticIndexQueue(image);
cristybb503372010-05-27 20:51:26 +00001098 for (x = (ssize_t)image->columns; x != 0; --x)
cristy3ed852e2009-09-05 21:47:34 +00001099 {
1100 index=ConstrainColormapIndex(image,*p);
1101 *indexes++=index;
cristybb503372010-05-27 20:51:26 +00001102 *q=image->colormap[(ssize_t) index];
cristy3ed852e2009-09-05 21:47:34 +00001103 p++;
1104 q++;
1105 }
1106 if (SyncAuthenticPixels(image,exception) == MagickFalse)
1107 break;
1108 offset=(MagickOffsetType) (image->rows-y-1);
1109 if (image->previous == (Image *) NULL)
1110 {
cristycee97112010-05-28 00:44:52 +00001111 status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y,
1112 image->rows);
cristy3ed852e2009-09-05 21:47:34 +00001113 if (status == MagickFalse)
1114 break;
1115 }
1116 }
1117 break;
1118 }
1119 case 16:
1120 {
cristybb503372010-05-27 20:51:26 +00001121 size_t
cristy3ed852e2009-09-05 21:47:34 +00001122 pixel;
1123
1124 /*
1125 Convert bitfield encoded 16-bit PseudoColor scanline.
1126 */
1127 if (bmp_info.compression != BI_RGB &&
1128 bmp_info.compression != BI_BITFIELDS)
cristyde60ea72010-07-09 23:12:09 +00001129 {
1130 pixels=(unsigned char *) RelinquishMagickMemory(pixels);
1131 ThrowReaderException(CorruptImageError,
1132 "UnrecognizedImageCompression");
1133 }
cristy3ed852e2009-09-05 21:47:34 +00001134 bytes_per_line=2*(image->columns+image->columns % 2);
1135 image->storage_class=DirectClass;
cristybb503372010-05-27 20:51:26 +00001136 for (y=(ssize_t) image->rows-1; y >= 0; y--)
cristy3ed852e2009-09-05 21:47:34 +00001137 {
1138 p=pixels+(image->rows-y-1)*bytes_per_line;
1139 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
1140 if (q == (PixelPacket *) NULL)
1141 break;
cristybb503372010-05-27 20:51:26 +00001142 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00001143 {
cristybb503372010-05-27 20:51:26 +00001144 pixel=(size_t) (*p++);
cristy3ed852e2009-09-05 21:47:34 +00001145 pixel|=(*p++) << 8;
1146 red=((pixel & bmp_info.red_mask) << shift.red) >> 16;
1147 if (quantum_bits.red == 5)
1148 red|=((red & 0xe000) >> 5);
1149 if (quantum_bits.red <= 8)
1150 red|=((red & 0xff00) >> 8);
1151 green=((pixel & bmp_info.green_mask) << shift.green) >> 16;
1152 if (quantum_bits.green == 5)
1153 green|=((green & 0xe000) >> 5);
1154 if (quantum_bits.green == 6)
1155 green|=((green & 0xc000) >> 6);
1156 if (quantum_bits.green <= 8)
1157 green|=((green & 0xff00) >> 8);
1158 blue=((pixel & bmp_info.blue_mask) << shift.blue) >> 16;
1159 if (quantum_bits.blue == 5)
1160 blue|=((blue & 0xe000) >> 5);
1161 if (quantum_bits.blue <= 8)
1162 blue|=((blue & 0xff00) >> 8);
1163 opacity=((pixel & bmp_info.alpha_mask) << shift.opacity) >> 16;
1164 if (quantum_bits.opacity <= 8)
1165 opacity|=((opacity & 0xff00) >> 8);
1166 q->red=ScaleShortToQuantum((unsigned short) red);
1167 q->green=ScaleShortToQuantum((unsigned short) green);
1168 q->blue=ScaleShortToQuantum((unsigned short) blue);
cristyce70c172010-01-07 17:15:30 +00001169 SetOpacityPixelComponent(q,OpaqueOpacity);
cristy3ed852e2009-09-05 21:47:34 +00001170 if (image->matte != MagickFalse)
cristyce8c2772010-02-26 23:43:26 +00001171 q->opacity=ScaleShortToQuantum((unsigned short) (65535-opacity));
cristy3ed852e2009-09-05 21:47:34 +00001172 q++;
1173 }
1174 if (SyncAuthenticPixels(image,exception) == MagickFalse)
1175 break;
1176 offset=(MagickOffsetType) (image->rows-y-1);
1177 if (image->previous == (Image *) NULL)
1178 {
cristycee97112010-05-28 00:44:52 +00001179 status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y,
1180 image->rows);
cristy3ed852e2009-09-05 21:47:34 +00001181 if (status == MagickFalse)
1182 break;
1183 }
1184 }
1185 break;
1186 }
1187 case 24:
1188 {
1189 /*
1190 Convert DirectColor scanline.
1191 */
1192 bytes_per_line=4*((image->columns*24+31)/32);
cristybb503372010-05-27 20:51:26 +00001193 for (y=(ssize_t) image->rows-1; y >= 0; y--)
cristy3ed852e2009-09-05 21:47:34 +00001194 {
1195 p=pixels+(image->rows-y-1)*bytes_per_line;
1196 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
1197 if (q == (PixelPacket *) NULL)
1198 break;
cristybb503372010-05-27 20:51:26 +00001199 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00001200 {
1201 q->blue=ScaleCharToQuantum(*p++);
1202 q->green=ScaleCharToQuantum(*p++);
1203 q->red=ScaleCharToQuantum(*p++);
cristyfe12d6c2010-05-07 01:38:41 +00001204 SetOpacityPixelComponent(q,OpaqueOpacity);
cristy3ed852e2009-09-05 21:47:34 +00001205 q++;
1206 }
1207 if (SyncAuthenticPixels(image,exception) == MagickFalse)
1208 break;
1209 offset=(MagickOffsetType) (image->rows-y-1);
1210 if (image->previous == (Image *) NULL)
1211 {
cristycee97112010-05-28 00:44:52 +00001212 status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y,
1213 image->rows);
cristy3ed852e2009-09-05 21:47:34 +00001214 if (status == MagickFalse)
1215 break;
1216 }
1217 }
1218 break;
1219 }
1220 case 32:
1221 {
1222 /*
1223 Convert bitfield encoded DirectColor scanline.
1224 */
1225 if ((bmp_info.compression != BI_RGB) &&
1226 (bmp_info.compression != BI_BITFIELDS))
cristyde60ea72010-07-09 23:12:09 +00001227 {
1228 pixels=(unsigned char *) RelinquishMagickMemory(pixels);
1229 ThrowReaderException(CorruptImageError,
1230 "UnrecognizedImageCompression");
1231 }
cristy3ed852e2009-09-05 21:47:34 +00001232 bytes_per_line=4*(image->columns);
cristybb503372010-05-27 20:51:26 +00001233 for (y=(ssize_t) image->rows-1; y >= 0; y--)
cristy3ed852e2009-09-05 21:47:34 +00001234 {
cristybb503372010-05-27 20:51:26 +00001235 size_t
cristy3ed852e2009-09-05 21:47:34 +00001236 pixel;
1237
1238 p=pixels+(image->rows-y-1)*bytes_per_line;
1239 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
1240 if (q == (PixelPacket *) NULL)
1241 break;
cristybb503372010-05-27 20:51:26 +00001242 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00001243 {
cristybb503372010-05-27 20:51:26 +00001244 pixel=(size_t) (*p++);
cristy3ed852e2009-09-05 21:47:34 +00001245 pixel|=(*p++ << 8);
1246 pixel|=(*p++ << 16);
1247 pixel|=(*p++ << 24);
1248 red=((pixel & bmp_info.red_mask) << shift.red) >> 16;
1249 if (quantum_bits.red == 8)
1250 red|=(red >> 8);
1251 green=((pixel & bmp_info.green_mask) << shift.green) >> 16;
1252 if (quantum_bits.green == 8)
1253 green|=(green >> 8);
1254 blue=((pixel & bmp_info.blue_mask) << shift.blue) >> 16;
1255 if (quantum_bits.blue == 8)
1256 blue|=(blue >> 8);
1257 opacity=((pixel & bmp_info.alpha_mask) << shift.opacity) >> 16;
1258 if (quantum_bits.opacity == 8)
1259 opacity|=(opacity >> 8);
1260 q->red=ScaleShortToQuantum((unsigned short) red);
1261 q->green=ScaleShortToQuantum((unsigned short) green);
1262 q->blue=ScaleShortToQuantum((unsigned short) blue);
cristyce70c172010-01-07 17:15:30 +00001263 SetOpacityPixelComponent(q,OpaqueOpacity);
cristy3ed852e2009-09-05 21:47:34 +00001264 if (image->matte != MagickFalse)
cristy228ab742010-02-24 13:49:34 +00001265 q->opacity=ScaleShortToQuantum((unsigned short) (65535-opacity));
cristy3ed852e2009-09-05 21:47:34 +00001266 q++;
1267 }
1268 if (SyncAuthenticPixels(image,exception) == MagickFalse)
1269 break;
1270 offset=(MagickOffsetType) (image->rows-y-1);
1271 if (image->previous == (Image *) NULL)
1272 {
cristycee97112010-05-28 00:44:52 +00001273 status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y,
1274 image->rows);
cristy3ed852e2009-09-05 21:47:34 +00001275 if (status == MagickFalse)
1276 break;
1277 }
1278 }
1279 break;
1280 }
1281 default:
cristyde60ea72010-07-09 23:12:09 +00001282 {
1283 pixels=(unsigned char *) RelinquishMagickMemory(pixels);
cristy3ed852e2009-09-05 21:47:34 +00001284 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
cristyde60ea72010-07-09 23:12:09 +00001285 }
cristy3ed852e2009-09-05 21:47:34 +00001286 }
1287 pixels=(unsigned char *) RelinquishMagickMemory(pixels);
1288 if (EOFBlob(image) != MagickFalse)
1289 {
1290 ThrowFileException(exception,CorruptImageError,"UnexpectedEndOfFile",
1291 image->filename);
1292 break;
1293 }
1294 if (bmp_info.height < 0)
1295 {
1296 Image
1297 *flipped_image;
1298
1299 /*
1300 Correct image orientation.
1301 */
1302 flipped_image=FlipImage(image,exception);
cristy65e3b002010-04-13 21:20:56 +00001303 if (flipped_image != (Image *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00001304 {
cristy65e3b002010-04-13 21:20:56 +00001305 DuplicateBlob(flipped_image,image);
1306 image=DestroyImage(image);
1307 image=flipped_image;
cristy3ed852e2009-09-05 21:47:34 +00001308 }
cristy3ed852e2009-09-05 21:47:34 +00001309 }
1310 /*
1311 Proceed to next image.
1312 */
1313 if (image_info->number_scenes != 0)
1314 if (image->scene >= (image_info->scene+image_info->number_scenes-1))
1315 break;
1316 *magick='\0';
1317 if (bmp_info.ba_offset != 0)
1318 {
1319 offset=SeekBlob(image,(MagickOffsetType) bmp_info.ba_offset,SEEK_SET);
1320 if (offset < 0)
1321 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
1322 }
1323 count=ReadBlob(image,2,magick);
1324 if ((count == 2) && (IsBMP(magick,2) != MagickFalse))
1325 {
1326 /*
1327 Acquire next image structure.
1328 */
1329 AcquireNextImage(image_info,image);
1330 if (GetNextImageInList(image) == (Image *) NULL)
1331 {
1332 image=DestroyImageList(image);
1333 return((Image *) NULL);
1334 }
1335 image=SyncNextImageInList(image);
1336 status=SetImageProgress(image,LoadImagesTag,TellBlob(image),
1337 GetBlobSize(image));
1338 if (status == MagickFalse)
1339 break;
1340 }
1341 } while (IsBMP(magick,2) != MagickFalse);
1342 (void) CloseBlob(image);
1343 return(GetFirstImageInList(image));
1344}
1345
1346/*
1347%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1348% %
1349% %
1350% %
1351% R e g i s t e r B M P I m a g e %
1352% %
1353% %
1354% %
1355%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1356%
1357% RegisterBMPImage() adds attributes for the BMP image format to
1358% the list of supported formats. The attributes include the image format
1359% tag, a method to read and/or write the format, whether the format
1360% supports the saving of more than one frame to the same file or blob,
1361% whether the format supports native in-memory I/O, and a brief
1362% description of the format.
1363%
1364% The format of the RegisterBMPImage method is:
1365%
cristybb503372010-05-27 20:51:26 +00001366% size_t RegisterBMPImage(void)
cristy3ed852e2009-09-05 21:47:34 +00001367%
1368*/
cristybb503372010-05-27 20:51:26 +00001369ModuleExport size_t RegisterBMPImage(void)
cristy3ed852e2009-09-05 21:47:34 +00001370{
1371 MagickInfo
1372 *entry;
1373
1374 entry=SetMagickInfo("BMP");
1375 entry->decoder=(DecodeImageHandler *) ReadBMPImage;
1376 entry->encoder=(EncodeImageHandler *) WriteBMPImage;
1377 entry->magick=(IsImageFormatHandler *) IsBMP;
cristy2e3d5242010-04-16 23:44:55 +00001378 entry->description=ConstantString("Microsoft Windows bitmap image");
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 entry=SetMagickInfo("BMP2");
1384 entry->encoder=(EncodeImageHandler *) WriteBMPImage;
1385 entry->magick=(IsImageFormatHandler *) IsBMP;
cristy2dd92ae2010-04-04 16:10:59 +00001386 entry->description=ConstantString("Microsoft Windows bitmap image (V2)");
cristy3ed852e2009-09-05 21:47:34 +00001387 entry->module=ConstantString("BMP");
1388 entry->adjoin=MagickFalse;
1389 entry->seekable_stream=MagickTrue;
1390 (void) RegisterMagickInfo(entry);
1391 entry=SetMagickInfo("BMP3");
1392 entry->encoder=(EncodeImageHandler *) WriteBMPImage;
1393 entry->magick=(IsImageFormatHandler *) IsBMP;
cristy2dd92ae2010-04-04 16:10:59 +00001394 entry->description=ConstantString("Microsoft Windows bitmap image (V3)");
cristy3ed852e2009-09-05 21:47:34 +00001395 entry->module=ConstantString("BMP");
1396 entry->adjoin=MagickFalse;
1397 entry->seekable_stream=MagickTrue;
1398 (void) RegisterMagickInfo(entry);
1399 return(MagickImageCoderSignature);
1400}
1401
1402/*
1403%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1404% %
1405% %
1406% %
1407% U n r e g i s t e r B M P I m a g e %
1408% %
1409% %
1410% %
1411%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1412%
1413% UnregisterBMPImage() removes format registrations made by the
1414% BMP module from the list of supported formats.
1415%
1416% The format of the UnregisterBMPImage method is:
1417%
1418% UnregisterBMPImage(void)
1419%
1420*/
1421ModuleExport void UnregisterBMPImage(void)
1422{
1423 (void) UnregisterMagickInfo("BMP");
1424 (void) UnregisterMagickInfo("BMP2");
1425 (void) UnregisterMagickInfo("BMP3");
1426}
1427
1428/*
1429%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1430% %
1431% %
1432% %
1433% W r i t e B M P I m a g e %
1434% %
1435% %
1436% %
1437%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1438%
1439% WriteBMPImage() writes an image in Microsoft Windows bitmap encoded
1440% image format, version 3 for Windows or (if the image has a matte channel)
1441% version 4.
1442%
1443% The format of the WriteBMPImage method is:
1444%
1445% MagickBooleanType WriteBMPImage(const ImageInfo *image_info,Image *image)
1446%
1447% A description of each parameter follows.
1448%
1449% o image_info: the image info.
1450%
1451% o image: The image.
1452%
1453*/
1454static MagickBooleanType WriteBMPImage(const ImageInfo *image_info,Image *image)
1455{
1456 BMPInfo
1457 bmp_info;
1458
1459 const StringInfo
1460 *profile;
1461
cristybb503372010-05-27 20:51:26 +00001462 ssize_t
cristy3ed852e2009-09-05 21:47:34 +00001463 y;
1464
1465 MagickBooleanType
1466 have_color_info,
1467 status;
1468
1469 MagickOffsetType
1470 scene;
1471
1472 register const IndexPacket
1473 *indexes;
1474
1475 register const PixelPacket
1476 *p;
1477
cristybb503372010-05-27 20:51:26 +00001478 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00001479 i,
1480 x;
1481
1482 register unsigned char
1483 *q;
1484
1485 unsigned char
1486 *bmp_data,
1487 *pixels;
1488
cristybb503372010-05-27 20:51:26 +00001489 size_t
cristy3ed852e2009-09-05 21:47:34 +00001490 bytes_per_line,
1491 type;
1492
1493 /*
1494 Open output image file.
1495 */
1496 assert(image_info != (const ImageInfo *) NULL);
1497 assert(image_info->signature == MagickSignature);
1498 assert(image != (Image *) NULL);
1499 assert(image->signature == MagickSignature);
1500 if (image->debug != MagickFalse)
1501 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1502 status=OpenBlob(image_info,image,WriteBinaryBlobMode,&image->exception);
1503 if (status == MagickFalse)
1504 return(status);
1505 type=4;
1506 if (LocaleCompare(image_info->magick,"BMP2") == 0)
1507 type=2;
1508 else
1509 if (LocaleCompare(image_info->magick,"BMP3") == 0)
1510 type=3;
1511 scene=0;
1512 do
1513 {
1514 /*
1515 Initialize BMP raster file header.
1516 */
1517 if (image->colorspace != RGBColorspace)
1518 (void) TransformImageColorspace(image,RGBColorspace);
1519 (void) ResetMagickMemory(&bmp_info,0,sizeof(bmp_info));
1520 bmp_info.file_size=14+12;
1521 if (type > 2)
1522 bmp_info.file_size+=28;
1523 bmp_info.offset_bits=bmp_info.file_size;
1524 bmp_info.compression=BI_RGB;
1525 if ((image->storage_class == PseudoClass) && (image->colors > 256))
1526 (void) SetImageStorageClass(image,DirectClass);
1527 if (image->storage_class != DirectClass)
1528 {
1529 /*
1530 Colormapped BMP raster.
1531 */
1532 bmp_info.bits_per_pixel=8;
1533 if (image->colors <= 2)
1534 bmp_info.bits_per_pixel=1;
1535 else
1536 if (image->colors <= 16)
1537 bmp_info.bits_per_pixel=4;
1538 else
1539 if (image->colors <= 256)
1540 bmp_info.bits_per_pixel=8;
1541 if (image_info->compression == RLECompression)
1542 bmp_info.bits_per_pixel=8;
1543 bmp_info.number_colors=1U << bmp_info.bits_per_pixel;
1544 if (image->matte != MagickFalse)
1545 (void) SetImageStorageClass(image,DirectClass);
1546 else
cristybb503372010-05-27 20:51:26 +00001547 if ((size_t) bmp_info.number_colors < image->colors)
cristy3ed852e2009-09-05 21:47:34 +00001548 (void) SetImageStorageClass(image,DirectClass);
1549 else
1550 {
1551 bmp_info.file_size+=3*(1UL << bmp_info.bits_per_pixel);
1552 bmp_info.offset_bits+=3*(1UL << bmp_info.bits_per_pixel);
1553 if (type > 2)
1554 {
1555 bmp_info.file_size+=(1UL << bmp_info.bits_per_pixel);
1556 bmp_info.offset_bits+=(1UL << bmp_info.bits_per_pixel);
1557 }
1558 }
1559 }
1560 if (image->storage_class == DirectClass)
1561 {
1562 /*
1563 Full color BMP raster.
1564 */
1565 bmp_info.number_colors=0;
1566 bmp_info.bits_per_pixel=(unsigned short)
1567 ((type > 3) && (image->matte != MagickFalse) ? 32 : 24);
1568 bmp_info.compression=(unsigned int) ((type > 3) &&
1569 (image->matte != MagickFalse) ? BI_BITFIELDS : BI_RGB);
1570 }
1571 bytes_per_line=4*((image->columns*bmp_info.bits_per_pixel+31)/32);
1572 bmp_info.ba_offset=0;
1573 profile=GetImageProfile(image,"icc");
1574 have_color_info=(image->rendering_intent != UndefinedIntent) ||
1575 (profile != (StringInfo *) NULL) || (image->gamma != 0.0) ? MagickTrue :
1576 MagickFalse;
1577 if (type == 2)
1578 bmp_info.size=12;
1579 else
1580 if ((type == 3) || ((image->matte == MagickFalse) &&
1581 (have_color_info == MagickFalse)))
1582 {
1583 type=3;
1584 bmp_info.size=40;
1585 }
1586 else
1587 {
1588 int
1589 extra_size;
1590
1591 bmp_info.size=108;
1592 extra_size=68;
1593 if ((image->rendering_intent != UndefinedIntent) ||
1594 (profile != (StringInfo *) NULL))
1595 {
1596 bmp_info.size=124;
1597 extra_size+=16;
1598 }
1599 bmp_info.file_size+=extra_size;
1600 bmp_info.offset_bits+=extra_size;
1601 }
cristybb503372010-05-27 20:51:26 +00001602 bmp_info.width=(ssize_t) image->columns;
1603 bmp_info.height=(ssize_t) image->rows;
cristy3ed852e2009-09-05 21:47:34 +00001604 bmp_info.planes=1;
1605 bmp_info.image_size=(unsigned int) (bytes_per_line*image->rows);
1606 bmp_info.file_size+=bmp_info.image_size;
1607 bmp_info.x_pixels=75*39;
1608 bmp_info.y_pixels=75*39;
1609 switch (image->units)
1610 {
1611 case UndefinedResolution:
1612 case PixelsPerInchResolution:
1613 {
1614 bmp_info.x_pixels=(unsigned int) (100.0*image->x_resolution/2.54);
1615 bmp_info.y_pixels=(unsigned int) (100.0*image->y_resolution/2.54);
1616 break;
1617 }
1618 case PixelsPerCentimeterResolution:
1619 {
1620 bmp_info.x_pixels=(unsigned int) (100.0*image->x_resolution);
1621 bmp_info.y_pixels=(unsigned int) (100.0*image->y_resolution);
1622 break;
1623 }
1624 }
1625 bmp_info.colors_important=bmp_info.number_colors;
1626 /*
1627 Convert MIFF to BMP raster pixels.
1628 */
1629 pixels=(unsigned char *) AcquireQuantumMemory((size_t) bmp_info.image_size,
1630 sizeof(*pixels));
1631 if (pixels == (unsigned char *) NULL)
1632 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
1633 (void) ResetMagickMemory(pixels,0,(size_t) bmp_info.image_size);
1634 switch (bmp_info.bits_per_pixel)
1635 {
1636 case 1:
1637 {
cristybb503372010-05-27 20:51:26 +00001638 size_t
cristy3ed852e2009-09-05 21:47:34 +00001639 bit,
1640 byte;
1641
1642 /*
1643 Convert PseudoClass image to a BMP monochrome image.
1644 */
cristybb503372010-05-27 20:51:26 +00001645 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00001646 {
1647 p=GetVirtualPixels(image,0,y,image->columns,1,&image->exception);
1648 if (p == (const PixelPacket *) NULL)
1649 break;
1650 indexes=GetVirtualIndexQueue(image);
1651 q=pixels+(image->rows-y-1)*bytes_per_line;
1652 bit=0;
1653 byte=0;
cristybb503372010-05-27 20:51:26 +00001654 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00001655 {
1656 byte<<=1;
1657 byte|=indexes[x] != 0 ? 0x01 : 0x00;
1658 bit++;
1659 if (bit == 8)
1660 {
1661 *q++=(unsigned char) byte;
1662 bit=0;
1663 byte=0;
1664 }
1665 }
1666 if (bit != 0)
1667 {
1668 *q++=(unsigned char) (byte << (8-bit));
1669 x++;
1670 }
cristybb503372010-05-27 20:51:26 +00001671 for (x=(ssize_t) (image->columns+7)/8; x < (ssize_t) bytes_per_line; x++)
cristy3ed852e2009-09-05 21:47:34 +00001672 *q++=0x00;
1673 if (image->previous == (Image *) NULL)
1674 {
cristycee97112010-05-28 00:44:52 +00001675 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
1676 image->rows);
cristy3ed852e2009-09-05 21:47:34 +00001677 if (status == MagickFalse)
1678 break;
1679 }
1680 }
1681 break;
1682 }
1683 case 4:
1684 {
cristybb503372010-05-27 20:51:26 +00001685 size_t
cristy3ed852e2009-09-05 21:47:34 +00001686 nibble,
1687 byte;
1688
1689 /*
1690 Convert PseudoClass image to a BMP monochrome image.
1691 */
cristybb503372010-05-27 20:51:26 +00001692 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00001693 {
1694 p=GetVirtualPixels(image,0,y,image->columns,1,&image->exception);
1695 if (p == (const PixelPacket *) NULL)
1696 break;
1697 indexes=GetVirtualIndexQueue(image);
1698 q=pixels+(image->rows-y-1)*bytes_per_line;
1699 nibble=0;
1700 byte=0;
cristybb503372010-05-27 20:51:26 +00001701 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00001702 {
1703 byte<<=4;
cristybb503372010-05-27 20:51:26 +00001704 byte|=((size_t) indexes[x] & 0x0f);
cristy3ed852e2009-09-05 21:47:34 +00001705 nibble++;
1706 if (nibble == 2)
1707 {
1708 *q++=(unsigned char) byte;
1709 nibble=0;
1710 byte=0;
1711 }
1712 }
1713 if (nibble != 0)
1714 {
1715 *q++=(unsigned char) (byte << 4);
1716 x++;
1717 }
cristybb503372010-05-27 20:51:26 +00001718 for (x=(ssize_t) (image->columns+1)/2; x < (ssize_t) bytes_per_line; x++)
cristy3ed852e2009-09-05 21:47:34 +00001719 *q++=0x00;
1720 if (image->previous == (Image *) NULL)
1721 {
cristycee97112010-05-28 00:44:52 +00001722 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
1723 image->rows);
cristy3ed852e2009-09-05 21:47:34 +00001724 if (status == MagickFalse)
1725 break;
1726 }
1727 }
1728 break;
1729 }
1730 case 8:
1731 {
1732 /*
1733 Convert PseudoClass packet to BMP pixel.
1734 */
cristybb503372010-05-27 20:51:26 +00001735 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00001736 {
1737 p=GetVirtualPixels(image,0,y,image->columns,1,&image->exception);
1738 if (p == (const PixelPacket *) NULL)
1739 break;
1740 indexes=GetVirtualIndexQueue(image);
1741 q=pixels+(image->rows-y-1)*bytes_per_line;
cristybb503372010-05-27 20:51:26 +00001742 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00001743 *q++=(unsigned char) indexes[x];
cristybb503372010-05-27 20:51:26 +00001744 for ( ; x < (ssize_t) bytes_per_line; x++)
cristy3ed852e2009-09-05 21:47:34 +00001745 *q++=0x00;
1746 if (image->previous == (Image *) NULL)
1747 {
cristycee97112010-05-28 00:44:52 +00001748 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
1749 image->rows);
cristy3ed852e2009-09-05 21:47:34 +00001750 if (status == MagickFalse)
1751 break;
1752 }
1753 }
1754 break;
1755 }
1756 case 24:
1757 {
1758 /*
1759 Convert DirectClass packet to BMP BGR888.
1760 */
cristybb503372010-05-27 20:51:26 +00001761 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00001762 {
1763 p=GetVirtualPixels(image,0,y,image->columns,1,&image->exception);
1764 if (p == (const PixelPacket *) NULL)
1765 break;
1766 q=pixels+(image->rows-y-1)*bytes_per_line;
cristybb503372010-05-27 20:51:26 +00001767 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00001768 {
cristyce70c172010-01-07 17:15:30 +00001769 *q++=ScaleQuantumToChar(GetBluePixelComponent(p));
1770 *q++=ScaleQuantumToChar(GetGreenPixelComponent(p));
1771 *q++=ScaleQuantumToChar(GetRedPixelComponent(p));
cristy3ed852e2009-09-05 21:47:34 +00001772 p++;
1773 }
cristybb503372010-05-27 20:51:26 +00001774 for (x=3L*(ssize_t) image->columns; x < (ssize_t) bytes_per_line; x++)
cristy3ed852e2009-09-05 21:47:34 +00001775 *q++=0x00;
1776 if (image->previous == (Image *) NULL)
1777 {
cristycee97112010-05-28 00:44:52 +00001778 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
1779 image->rows);
cristy3ed852e2009-09-05 21:47:34 +00001780 if (status == MagickFalse)
1781 break;
1782 }
1783 }
1784 break;
1785 }
1786 case 32:
1787 {
1788 /*
1789 Convert DirectClass packet to ARGB8888 pixel.
1790 */
cristybb503372010-05-27 20:51:26 +00001791 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00001792 {
1793 p=GetVirtualPixels(image,0,y,image->columns,1,&image->exception);
1794 if (p == (const PixelPacket *) NULL)
1795 break;
1796 q=pixels+(image->rows-y-1)*bytes_per_line;
cristybb503372010-05-27 20:51:26 +00001797 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00001798 {
cristyce70c172010-01-07 17:15:30 +00001799 *q++=ScaleQuantumToChar(GetBluePixelComponent(p));
1800 *q++=ScaleQuantumToChar(GetGreenPixelComponent(p));
1801 *q++=ScaleQuantumToChar(GetRedPixelComponent(p));
cristy228ab742010-02-24 13:49:34 +00001802 *q++=ScaleQuantumToChar(QuantumRange-GetOpacityPixelComponent(p));
cristy3ed852e2009-09-05 21:47:34 +00001803 p++;
1804 }
1805 if (image->previous == (Image *) NULL)
1806 {
cristycee97112010-05-28 00:44:52 +00001807 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
1808 image->rows);
cristy3ed852e2009-09-05 21:47:34 +00001809 if (status == MagickFalse)
1810 break;
1811 }
1812 }
1813 break;
1814 }
1815 }
1816 if ((type > 2) && (bmp_info.bits_per_pixel == 8))
1817 if (image_info->compression != NoCompression)
1818 {
1819 size_t
1820 length;
1821
1822 /*
1823 Convert run-length encoded raster pixels.
1824 */
1825 length=(size_t) (2*(bytes_per_line+2)*(image->rows+2)+2);
1826 bmp_data=(unsigned char *) NULL;
1827 if (~length >= bytes_per_line)
1828 bmp_data=(unsigned char *) AcquireQuantumMemory(length+
1829 bytes_per_line,sizeof(*bmp_data));
1830 if (bmp_data == (unsigned char *) NULL)
1831 {
1832 pixels=(unsigned char *) RelinquishMagickMemory(pixels);
1833 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
1834 }
1835 bmp_info.file_size-=bmp_info.image_size;
1836 bmp_info.image_size=(unsigned int) EncodeImage(image,bytes_per_line,
1837 pixels,bmp_data);
1838 bmp_info.file_size+=bmp_info.image_size;
1839 pixels=(unsigned char *) RelinquishMagickMemory(pixels);
1840 pixels=bmp_data;
1841 bmp_info.compression=BI_RLE8;
1842 }
1843 /*
1844 Write BMP for Windows, all versions, 14-byte header.
1845 */
1846 if (image->debug != MagickFalse)
1847 {
1848 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
cristye8c25f92010-06-03 00:53:06 +00001849 " Writing BMP version %.20g datastream",(double) type);
cristy3ed852e2009-09-05 21:47:34 +00001850 if (image->storage_class == DirectClass)
1851 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1852 " Storage class=DirectClass");
1853 else
1854 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1855 " Storage class=PseudoClass");
1856 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
cristye8c25f92010-06-03 00:53:06 +00001857 " Image depth=%.20g",(double) image->depth);
cristy3ed852e2009-09-05 21:47:34 +00001858 if (image->matte != MagickFalse)
1859 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1860 " Matte=True");
1861 else
1862 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1863 " Matte=MagickFalse");
1864 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
cristye8c25f92010-06-03 00:53:06 +00001865 " BMP bits_per_pixel=%.20g",(double) bmp_info.bits_per_pixel);
cristy3ed852e2009-09-05 21:47:34 +00001866 switch ((int) bmp_info.compression)
1867 {
1868 case BI_RGB:
1869 {
1870 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1871 " Compression=BI_RGB");
1872 break;
1873 }
1874 case BI_RLE8:
1875 {
1876 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1877 " Compression=BI_RLE8");
1878 break;
1879 }
1880 case BI_BITFIELDS:
1881 {
1882 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1883 " Compression=BI_BITFIELDS");
1884 break;
1885 }
1886 default:
1887 {
1888 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1889 " Compression=UNKNOWN (%u)",bmp_info.compression);
1890 break;
1891 }
1892 }
1893 if (bmp_info.number_colors == 0)
1894 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1895 " Number_colors=unspecified");
1896 else
1897 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1898 " Number_colors=%u",bmp_info.number_colors);
1899 }
1900 (void) WriteBlob(image,2,(unsigned char *) "BM");
1901 (void) WriteBlobLSBLong(image,bmp_info.file_size);
1902 (void) WriteBlobLSBLong(image,bmp_info.ba_offset); /* always 0 */
1903 (void) WriteBlobLSBLong(image,bmp_info.offset_bits);
1904 if (type == 2)
1905 {
1906 /*
1907 Write 12-byte version 2 bitmap header.
1908 */
1909 (void) WriteBlobLSBLong(image,bmp_info.size);
1910 (void) WriteBlobLSBShort(image,(unsigned short) bmp_info.width);
1911 (void) WriteBlobLSBShort(image,(unsigned short) bmp_info.height);
1912 (void) WriteBlobLSBShort(image,bmp_info.planes);
1913 (void) WriteBlobLSBShort(image,bmp_info.bits_per_pixel);
1914 }
1915 else
1916 {
1917 /*
1918 Write 40-byte version 3+ bitmap header.
1919 */
1920 (void) WriteBlobLSBLong(image,bmp_info.size);
1921 (void) WriteBlobLSBLong(image,(unsigned int) bmp_info.width);
1922 (void) WriteBlobLSBLong(image,(unsigned int) bmp_info.height);
1923 (void) WriteBlobLSBShort(image,bmp_info.planes);
1924 (void) WriteBlobLSBShort(image,bmp_info.bits_per_pixel);
1925 (void) WriteBlobLSBLong(image,bmp_info.compression);
1926 (void) WriteBlobLSBLong(image,bmp_info.image_size);
1927 (void) WriteBlobLSBLong(image,bmp_info.x_pixels);
1928 (void) WriteBlobLSBLong(image,bmp_info.y_pixels);
1929 (void) WriteBlobLSBLong(image,bmp_info.number_colors);
1930 (void) WriteBlobLSBLong(image,bmp_info.colors_important);
1931 }
1932 if ((type > 3) && ((image->matte != MagickFalse) ||
1933 (have_color_info != MagickFalse)))
1934 {
1935 /*
1936 Write the rest of the 108-byte BMP Version 4 header.
1937 */
1938 (void) WriteBlobLSBLong(image,0x00ff0000U); /* Red mask */
1939 (void) WriteBlobLSBLong(image,0x0000ff00U); /* Green mask */
1940 (void) WriteBlobLSBLong(image,0x000000ffU); /* Blue mask */
1941 (void) WriteBlobLSBLong(image,0xff000000U); /* Alpha mask */
1942 (void) WriteBlobLSBLong(image,0x00000001U); /* CSType==Calib. RGB */
1943 (void) WriteBlobLSBLong(image,(unsigned int)
1944 image->chromaticity.red_primary.x*0x3ffffff);
1945 (void) WriteBlobLSBLong(image,(unsigned int)
1946 image->chromaticity.red_primary.y*0x3ffffff);
1947 (void) WriteBlobLSBLong(image,(unsigned int)
1948 (1.000f-(image->chromaticity.red_primary.x+
1949 image->chromaticity.red_primary.y)*0x3ffffff));
1950 (void) WriteBlobLSBLong(image,(unsigned int)
1951 image->chromaticity.green_primary.x*0x3ffffff);
1952 (void) WriteBlobLSBLong(image,(unsigned int)
1953 image->chromaticity.green_primary.y*0x3ffffff);
1954 (void) WriteBlobLSBLong(image,(unsigned int)
1955 (1.000f-(image->chromaticity.green_primary.x+
1956 image->chromaticity.green_primary.y)*0x3ffffff));
1957 (void) WriteBlobLSBLong(image,(unsigned int)
1958 image->chromaticity.blue_primary.x*0x3ffffff);
1959 (void) WriteBlobLSBLong(image,(unsigned int)
1960 image->chromaticity.blue_primary.y*0x3ffffff);
1961 (void) WriteBlobLSBLong(image,(unsigned int)
1962 (1.000f-(image->chromaticity.blue_primary.x+
1963 image->chromaticity.blue_primary.y)*0x3ffffff));
1964 (void) WriteBlobLSBLong(image,(unsigned int)
1965 bmp_info.gamma_scale.x*0xffff);
1966 (void) WriteBlobLSBLong(image,(unsigned int)
1967 bmp_info.gamma_scale.y*0xffff);
1968 (void) WriteBlobLSBLong(image,(unsigned int)
1969 bmp_info.gamma_scale.z*0xffff);
1970 if ((image->rendering_intent != UndefinedIntent) ||
1971 (profile != (StringInfo *) NULL))
1972 {
cristybb503372010-05-27 20:51:26 +00001973 ssize_t
cristy3ed852e2009-09-05 21:47:34 +00001974 intent;
1975
1976 switch ((int) image->rendering_intent)
1977 {
1978 case SaturationIntent:
1979 {
1980 intent=LCS_GM_BUSINESS;
1981 break;
1982 }
1983 case RelativeIntent:
1984 {
1985 intent=LCS_GM_GRAPHICS;
1986 break;
1987 }
1988 case PerceptualIntent:
1989 {
1990 intent=LCS_GM_IMAGES;
1991 break;
1992 }
1993 case AbsoluteIntent:
1994 {
1995 intent=LCS_GM_ABS_COLORIMETRIC;
1996 break;
1997 }
1998 default:
1999 {
2000 intent=0;
2001 break;
2002 }
2003 }
2004 (void) WriteBlobLSBLong(image,(unsigned int) intent);
2005 (void) WriteBlobLSBLong(image,0x00); /* dummy profile data */
2006 (void) WriteBlobLSBLong(image,0x00); /* dummy profile length */
2007 (void) WriteBlobLSBLong(image,0x00); /* reserved */
2008 }
2009 }
2010 if (image->storage_class == PseudoClass)
2011 {
2012 unsigned char
2013 *bmp_colormap;
2014
2015 /*
2016 Dump colormap to file.
2017 */
2018 if (image->debug != MagickFalse)
2019 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
cristye8c25f92010-06-03 00:53:06 +00002020 " Colormap: %.20g entries",(double) image->colors);
cristy3ed852e2009-09-05 21:47:34 +00002021 bmp_colormap=(unsigned char *) AcquireQuantumMemory((size_t) (1UL <<
2022 bmp_info.bits_per_pixel),4*sizeof(*bmp_colormap));
2023 if (bmp_colormap == (unsigned char *) NULL)
2024 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
2025 q=bmp_colormap;
cristybb503372010-05-27 20:51:26 +00002026 for (i=0; i < (ssize_t) MagickMin((ssize_t) image->colors,(ssize_t) bmp_info.number_colors); i++)
cristy3ed852e2009-09-05 21:47:34 +00002027 {
2028 *q++=ScaleQuantumToChar(image->colormap[i].blue);
2029 *q++=ScaleQuantumToChar(image->colormap[i].green);
2030 *q++=ScaleQuantumToChar(image->colormap[i].red);
2031 if (type > 2)
2032 *q++=(unsigned char) 0x0;
2033 }
cristybb503372010-05-27 20:51:26 +00002034 for ( ; i < (ssize_t) (1UL << bmp_info.bits_per_pixel); i++)
cristy3ed852e2009-09-05 21:47:34 +00002035 {
2036 *q++=(unsigned char) 0x00;
2037 *q++=(unsigned char) 0x00;
2038 *q++=(unsigned char) 0x00;
2039 if (type > 2)
2040 *q++=(unsigned char) 0x00;
2041 }
2042 if (type <= 2)
2043 (void) WriteBlob(image,(size_t) (3*(1L << bmp_info.bits_per_pixel)),
2044 bmp_colormap);
2045 else
2046 (void) WriteBlob(image,(size_t) (4*(1L << bmp_info.bits_per_pixel)),
2047 bmp_colormap);
2048 bmp_colormap=(unsigned char *) RelinquishMagickMemory(bmp_colormap);
2049 }
2050 if (image->debug != MagickFalse)
2051 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
2052 " Pixels: %u bytes",bmp_info.image_size);
2053 (void) WriteBlob(image,(size_t) bmp_info.image_size,pixels);
2054 pixels=(unsigned char *) RelinquishMagickMemory(pixels);
2055 if (GetNextImageInList(image) == (Image *) NULL)
2056 break;
2057 image=SyncNextImageInList(image);
2058 status=SetImageProgress(image,SaveImagesTag,scene++,
2059 GetImageListLength(image));
2060 if (status == MagickFalse)
2061 break;
2062 } while (image_info->adjoin != MagickFalse);
2063 (void) CloseBlob(image);
2064 return(MagickTrue);
2065}