blob: 8f0c3d110cecfdcf2c4f17d39bb7579a39ad6f1b [file] [log] [blame]
cristy3ed852e2009-09-05 21:47:34 +00001/*
2%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3% %
4% %
5% %
6% IIIII CCCC OOO N N %
7% I C O O NN N %
8% I C O O N N N %
9% I C O O N NN %
10% IIIII CCCC OOO N N %
11% %
12% %
13% Read Microsoft Windows Icon Format %
14% %
15% Software Design %
16% John Cristy %
17% July 1992 %
18% %
19% %
cristy7e41fe82010-12-04 23:12:08 +000020% Copyright 1999-2011 ImageMagick Studio LLC, a non-profit organization %
cristy3ed852e2009-09-05 21:47:34 +000021% dedicated to making software imaging solutions freely available. %
22% %
23% You may not use this file except in compliance with the License. You may %
24% obtain a copy of the License at %
25% %
26% http://www.imagemagick.org/script/license.php %
27% %
28% Unless required by applicable law or agreed to in writing, software %
29% distributed under the License is distributed on an "AS IS" BASIS, %
30% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. %
31% See the License for the specific language governing permissions and %
32% limitations under the License. %
33% %
34%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
35%
36%
37*/
38
39/*
40 Include declarations.
41*/
42#include "magick/studio.h"
43#include "magick/blob.h"
44#include "magick/blob-private.h"
45#include "magick/cache.h"
cristye7e40552010-04-24 21:34:22 +000046#include "magick/colormap.h"
cristy3ed852e2009-09-05 21:47:34 +000047#include "magick/colorspace.h"
48#include "magick/exception.h"
49#include "magick/exception-private.h"
50#include "magick/image.h"
51#include "magick/image-private.h"
52#include "magick/list.h"
53#include "magick/log.h"
54#include "magick/magick.h"
55#include "magick/memory_.h"
56#include "magick/monitor.h"
57#include "magick/monitor-private.h"
58#include "magick/nt-feature.h"
59#include "magick/quantize.h"
60#include "magick/quantum-private.h"
61#include "magick/static.h"
62#include "magick/string_.h"
63#include "magick/module.h"
64
65/*
66 Define declarations.
67*/
cristy0157aea2010-04-24 21:12:18 +000068#if !defined(MAGICKCORE_WINDOWS_SUPPORT) || defined(__MINGW32__)
cristy3ed852e2009-09-05 21:47:34 +000069#define BI_RGB 0
70#define BI_RLE8 1
71#define BI_BITFIELDS 3
72#endif
73#define MaxIcons 1024
74
75/*
76 Typedef declarations.
77*/
78typedef struct _IconEntry
79{
80 unsigned char
81 width,
82 height,
83 colors,
84 reserved;
85
86 unsigned short int
87 planes,
88 bits_per_pixel;
89
cristybb503372010-05-27 20:51:26 +000090 size_t
cristy3ed852e2009-09-05 21:47:34 +000091 size,
92 offset;
93} IconEntry;
94
95typedef struct _IconFile
96{
97 short
98 reserved,
99 resource_type,
100 count;
101
102 IconEntry
103 directory[MaxIcons];
104} IconFile;
105
106typedef struct _IconInfo
107{
cristybb503372010-05-27 20:51:26 +0000108 size_t
cristy3ed852e2009-09-05 21:47:34 +0000109 file_size,
110 ba_offset,
111 offset_bits,
112 size;
113
cristybb503372010-05-27 20:51:26 +0000114 ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000115 width,
116 height;
117
118 unsigned short
119 planes,
120 bits_per_pixel;
121
cristybb503372010-05-27 20:51:26 +0000122 size_t
cristy3ed852e2009-09-05 21:47:34 +0000123 compression,
124 image_size,
125 x_pixels,
126 y_pixels,
127 number_colors,
128 red_mask,
129 green_mask,
130 blue_mask,
131 alpha_mask,
132 colors_important;
133
cristybb503372010-05-27 20:51:26 +0000134 ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000135 colorspace;
136} IconInfo;
137
138/*
139 Forward declaractions.
140*/
141static MagickBooleanType
142 WriteICONImage(const ImageInfo *,Image *);
143
144/*
145%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
146% %
147% %
148% %
149% R e a d I C O N I m a g e %
150% %
151% %
152% %
153%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
154%
155% ReadICONImage() reads a Microsoft icon image file and returns it. It
156% allocates the memory necessary for the new Image structure and returns a
157% pointer to the new image.
158%
159% The format of the ReadICONImage method is:
160%
161% Image *ReadICONImage(const ImageInfo *image_info,
162% ExceptionInfo *exception)
163%
164% A description of each parameter follows:
165%
166% o image_info: the image info.
167%
168% o exception: return any errors or warnings in this structure.
169%
170*/
171static Image *ReadICONImage(const ImageInfo *image_info,
172 ExceptionInfo *exception)
173{
174 IconFile
175 icon_file;
176
177 IconInfo
178 icon_info;
179
180 Image
181 *image;
182
cristy3ed852e2009-09-05 21:47:34 +0000183 MagickBooleanType
184 status;
185
186 register IndexPacket
187 *indexes;
188
cristybb503372010-05-27 20:51:26 +0000189 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000190 i,
191 x;
192
193 register PixelPacket
194 *q;
195
196 register unsigned char
197 *p;
198
cristybb503372010-05-27 20:51:26 +0000199 size_t
cristy3ed852e2009-09-05 21:47:34 +0000200 bit,
cristyeaedf062010-05-29 22:36:02 +0000201 byte,
cristy3ed852e2009-09-05 21:47:34 +0000202 bytes_per_line,
cristyeaedf062010-05-29 22:36:02 +0000203 one,
cristy3ed852e2009-09-05 21:47:34 +0000204 scanline_pad;
205
cristyebc891a2011-04-24 23:04:16 +0000206 ssize_t
207 count,
208 offset,
209 y;
210
cristy3ed852e2009-09-05 21:47:34 +0000211 /*
212 Open image file.
213 */
214 assert(image_info != (const ImageInfo *) NULL);
215 assert(image_info->signature == MagickSignature);
216 (void) LogMagickEvent(CoderEvent,GetMagickModule(),"%s",image_info->filename);
217 assert(exception != (ExceptionInfo *) NULL);
218 assert(exception->signature == MagickSignature);
219 image=AcquireImage(image_info);
220 status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception);
221 if (status == MagickFalse)
222 {
223 image=DestroyImageList(image);
224 return((Image *) NULL);
225 }
226 icon_file.reserved=(short) ReadBlobLSBShort(image);
227 icon_file.resource_type=(short) ReadBlobLSBShort(image);
228 icon_file.count=(short) ReadBlobLSBShort(image);
229 if ((icon_file.reserved != 0) ||
230 ((icon_file.resource_type != 1) && (icon_file.resource_type != 2)) ||
231 (icon_file.count > MaxIcons))
232 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
233 for (i=0; i < icon_file.count; i++)
234 {
235 icon_file.directory[i].width=(unsigned char) ReadBlobByte(image);
236 icon_file.directory[i].height=(unsigned char) ReadBlobByte(image);
237 icon_file.directory[i].colors=(unsigned char) ReadBlobByte(image);
238 icon_file.directory[i].reserved=(unsigned char) ReadBlobByte(image);
239 icon_file.directory[i].planes=(unsigned short) ReadBlobLSBShort(image);
240 icon_file.directory[i].bits_per_pixel=(unsigned short)
241 ReadBlobLSBShort(image);
242 icon_file.directory[i].size=ReadBlobLSBLong(image);
243 icon_file.directory[i].offset=ReadBlobLSBLong(image);
244 }
cristyeaedf062010-05-29 22:36:02 +0000245 one=1;
cristy3ed852e2009-09-05 21:47:34 +0000246 for (i=0; i < icon_file.count; i++)
247 {
248 /*
249 Verify Icon identifier.
250 */
251 offset=(ssize_t) SeekBlob(image,(MagickOffsetType)
252 icon_file.directory[i].offset,SEEK_SET);
253 if (offset < 0)
254 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
255 icon_info.size=ReadBlobLSBLong(image);
cristyd63c0502011-01-03 15:45:11 +0000256 icon_info.width=(unsigned char) ((int) ReadBlobLSBLong(image));
257 icon_info.height=(unsigned char) ((int) ReadBlobLSBLong(image)/2);
cristy3ed852e2009-09-05 21:47:34 +0000258 if ((icon_file.directory[i].width == 0) &&
259 (icon_file.directory[i].height == 0))
260 {
261 Image
262 *icon_image;
263
264 ImageInfo
265 *read_info;
266
267 size_t
268 length;
269
270 unsigned char
271 *png;
272
273 /*
274 Icon image encoded as a compressed PNG image.
275 */
276 length=icon_file.directory[i].size;
277 png=(unsigned char *) AcquireQuantumMemory(length+12,sizeof(*png));
278 if (png == (unsigned char *) NULL)
279 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
280 (void) CopyMagickMemory(png,"\211PNG\r\n\032\n\000\000\000\015",12);
cristyc8817fe2010-08-01 13:43:59 +0000281 count=ReadBlob(image,length-12,png+12);
282 if (count != (ssize_t) (length-12))
cristy3ed852e2009-09-05 21:47:34 +0000283 {
284 png=(unsigned char *) RelinquishMagickMemory(png);
285 ThrowReaderException(CorruptImageError,
286 "InsufficientImageDataInFile");
287 }
288 read_info=CloneImageInfo(image_info);
289 (void) CopyMagickString(read_info->magick,"PNG",MaxTextExtent);
290 icon_image=BlobToImage(read_info,png,length+12,exception);
291 read_info=DestroyImageInfo(read_info);
292 png=(unsigned char *) RelinquishMagickMemory(png);
293 if (icon_image == (Image *) NULL)
294 {
295 image=DestroyImageList(image);
296 return((Image *) NULL);
297 }
298 DestroyBlob(icon_image);
299 icon_image->blob=ReferenceBlob(image->blob);
300 ReplaceImageInList(&image,icon_image);
301 }
302 else
303 {
304 icon_info.planes=ReadBlobLSBShort(image);
305 icon_info.bits_per_pixel=ReadBlobLSBShort(image);
306 if (icon_info.bits_per_pixel > 32)
307 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
308 icon_info.compression=ReadBlobLSBLong(image);
309 icon_info.image_size=ReadBlobLSBLong(image);
310 icon_info.x_pixels=ReadBlobLSBLong(image);
311 icon_info.y_pixels=ReadBlobLSBLong(image);
312 icon_info.number_colors=ReadBlobLSBLong(image);
313 icon_info.colors_important=ReadBlobLSBLong(image);
314 image->matte=MagickTrue;
cristybb503372010-05-27 20:51:26 +0000315 image->columns=(size_t) icon_file.directory[i].width;
316 if ((ssize_t) image->columns > icon_info.width)
317 image->columns=(size_t) icon_info.width;
318 image->rows=(size_t) icon_file.directory[i].height;
319 if ((ssize_t) image->rows > icon_info.height)
320 image->rows=(size_t) icon_info.height;
cristyc8817fe2010-08-01 13:43:59 +0000321 image->depth=icon_info.bits_per_pixel;
cristy3ed852e2009-09-05 21:47:34 +0000322 if (image->debug != MagickFalse)
323 {
324 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
cristye8c25f92010-06-03 00:53:06 +0000325 " scene = %.20g",(double) i);
cristy3ed852e2009-09-05 21:47:34 +0000326 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
cristye8c25f92010-06-03 00:53:06 +0000327 " size = %.20g",(double) icon_info.size);
cristy3ed852e2009-09-05 21:47:34 +0000328 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
cristye8c25f92010-06-03 00:53:06 +0000329 " width = %.20g",(double) icon_file.directory[i].width);
cristy3ed852e2009-09-05 21:47:34 +0000330 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
cristye8c25f92010-06-03 00:53:06 +0000331 " height = %.20g",(double) icon_file.directory[i].height);
cristy3ed852e2009-09-05 21:47:34 +0000332 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
cristye8c25f92010-06-03 00:53:06 +0000333 " colors = %.20g",(double ) icon_info.number_colors);
cristy3ed852e2009-09-05 21:47:34 +0000334 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
cristye8c25f92010-06-03 00:53:06 +0000335 " planes = %.20g",(double) icon_info.planes);
cristy3ed852e2009-09-05 21:47:34 +0000336 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
cristye8c25f92010-06-03 00:53:06 +0000337 " bpp = %.20g",(double) icon_info.bits_per_pixel);
cristy3ed852e2009-09-05 21:47:34 +0000338 }
339 if ((icon_info.number_colors != 0) || (icon_info.bits_per_pixel <= 16))
340 {
341 image->storage_class=PseudoClass;
342 image->colors=icon_info.number_colors;
343 if (image->colors == 0)
cristyeaedf062010-05-29 22:36:02 +0000344 image->colors=one << icon_info.bits_per_pixel;
cristy3ed852e2009-09-05 21:47:34 +0000345 }
346 if (image->storage_class == PseudoClass)
347 {
cristybb503372010-05-27 20:51:26 +0000348 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000349 i;
350
cristybb503372010-05-27 20:51:26 +0000351 size_t
cristy0b29b252010-05-30 01:59:46 +0000352 number_colors,
353 one;
cristy3ed852e2009-09-05 21:47:34 +0000354
cristyebc891a2011-04-24 23:04:16 +0000355 unsigned char
356 *icon_colormap;
357
cristy3ed852e2009-09-05 21:47:34 +0000358 /*
359 Read Icon raster colormap.
360 */
cristy0b29b252010-05-30 01:59:46 +0000361 one=1;
362 number_colors=one << icon_info.bits_per_pixel;
cristy3ed852e2009-09-05 21:47:34 +0000363 if (AcquireImageColormap(image,number_colors) == MagickFalse)
364 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
365 icon_colormap=(unsigned char *) AcquireQuantumMemory((size_t)
366 image->colors,4UL*sizeof(*icon_colormap));
367 if (icon_colormap == (unsigned char *) NULL)
368 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
369 count=ReadBlob(image,(size_t) (4*image->colors),icon_colormap);
370 if (count != (ssize_t) (4*image->colors))
371 ThrowReaderException(CorruptImageError,
372 "InsufficientImageDataInFile");
373 p=icon_colormap;
cristybb503372010-05-27 20:51:26 +0000374 for (i=0; i < (ssize_t) image->colors; i++)
cristy3ed852e2009-09-05 21:47:34 +0000375 {
376 image->colormap[i].blue=(Quantum) ScaleCharToQuantum(*p++);
377 image->colormap[i].green=(Quantum) ScaleCharToQuantum(*p++);
378 image->colormap[i].red=(Quantum) ScaleCharToQuantum(*p++);
379 p++;
380 }
381 icon_colormap=(unsigned char *) RelinquishMagickMemory(icon_colormap);
382 }
383 /*
384 Convert Icon raster image to pixel packets.
385 */
386 if ((image_info->ping != MagickFalse) &&
387 (image_info->number_scenes != 0))
388 if (image->scene >= (image_info->scene+image_info->number_scenes-1))
389 break;
390 bytes_per_line=(((image->columns*icon_info.bits_per_pixel)+31) &
391 ~31) >> 3;
cristyda16f162011-02-19 23:52:17 +0000392 (void) bytes_per_line;
cristy3ed852e2009-09-05 21:47:34 +0000393 scanline_pad=((((image->columns*icon_info.bits_per_pixel)+31) & ~31)-
394 (image->columns*icon_info.bits_per_pixel)) >> 3;
395 switch (icon_info.bits_per_pixel)
396 {
397 case 1:
398 {
399 /*
400 Convert bitmap scanline.
401 */
cristybb503372010-05-27 20:51:26 +0000402 for (y=(ssize_t) image->rows-1; y >= 0; y--)
cristy3ed852e2009-09-05 21:47:34 +0000403 {
404 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
405 if (q == (PixelPacket *) NULL)
406 break;
407 indexes=GetAuthenticIndexQueue(image);
cristybb503372010-05-27 20:51:26 +0000408 for (x=0; x < (ssize_t) (image->columns-7); x+=8)
cristy3ed852e2009-09-05 21:47:34 +0000409 {
cristybb503372010-05-27 20:51:26 +0000410 byte=(size_t) ReadBlobByte(image);
cristy3ed852e2009-09-05 21:47:34 +0000411 for (bit=0; bit < 8; bit++)
cristyebc891a2011-04-24 23:04:16 +0000412 SetIndexPixelComponent(indexes+x+bit,
413 ((byte & (0x80 >> bit)) != 0 ? 0x01 : 0x00));
cristy3ed852e2009-09-05 21:47:34 +0000414 }
415 if ((image->columns % 8) != 0)
416 {
cristybb503372010-05-27 20:51:26 +0000417 byte=(size_t) ReadBlobByte(image);
cristy3ed852e2009-09-05 21:47:34 +0000418 for (bit=0; bit < (image->columns % 8); bit++)
cristyebc891a2011-04-24 23:04:16 +0000419 SetIndexPixelComponent(indexes+x+bit,
420 ((byte & (0x80 >> bit)) != 0 ? 0x01 : 0x00));
cristy3ed852e2009-09-05 21:47:34 +0000421 }
cristybb503372010-05-27 20:51:26 +0000422 for (x=0; x < (ssize_t) scanline_pad; x++)
cristy3ed852e2009-09-05 21:47:34 +0000423 (void) ReadBlobByte(image);
424 if (SyncAuthenticPixels(image,exception) == MagickFalse)
425 break;
426 if (image->previous == (Image *) NULL)
427 {
428 status=SetImageProgress(image,LoadImageTag,image->rows-y-1,
429 image->rows);
430 if (status == MagickFalse)
431 break;
432 }
433 }
434 break;
435 }
436 case 4:
437 {
438 /*
439 Read 4-bit Icon scanline.
440 */
cristybb503372010-05-27 20:51:26 +0000441 for (y=(ssize_t) image->rows-1; y >= 0; y--)
cristy3ed852e2009-09-05 21:47:34 +0000442 {
443 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
444 if (q == (PixelPacket *) NULL)
445 break;
446 indexes=GetAuthenticIndexQueue(image);
cristybb503372010-05-27 20:51:26 +0000447 for (x=0; x < ((ssize_t) image->columns-1); x+=2)
cristy3ed852e2009-09-05 21:47:34 +0000448 {
cristybb503372010-05-27 20:51:26 +0000449 byte=(size_t) ReadBlobByte(image);
cristyebc891a2011-04-24 23:04:16 +0000450 SetIndexPixelComponent(indexes+x,((byte >> 4) & 0xf));
451 SetIndexPixelComponent(indexes+x+1,((byte) & 0xf));
cristy3ed852e2009-09-05 21:47:34 +0000452 }
453 if ((image->columns % 2) != 0)
454 {
cristybb503372010-05-27 20:51:26 +0000455 byte=(size_t) ReadBlobByte(image);
cristyebc891a2011-04-24 23:04:16 +0000456 SetIndexPixelComponent(indexes+x,((byte >> 4) & 0xf));
cristy3ed852e2009-09-05 21:47:34 +0000457 }
cristybb503372010-05-27 20:51:26 +0000458 for (x=0; x < (ssize_t) scanline_pad; x++)
cristy3ed852e2009-09-05 21:47:34 +0000459 (void) ReadBlobByte(image);
460 if (SyncAuthenticPixels(image,exception) == MagickFalse)
461 break;
462 if (image->previous == (Image *) NULL)
463 {
464 status=SetImageProgress(image,LoadImageTag,image->rows-y-1,
465 image->rows);
466 if (status == MagickFalse)
467 break;
468 }
469 }
470 break;
471 }
472 case 8:
473 {
474 /*
475 Convert PseudoColor scanline.
476 */
cristybb503372010-05-27 20:51:26 +0000477 for (y=(ssize_t) image->rows-1; y >= 0; y--)
cristy3ed852e2009-09-05 21:47:34 +0000478 {
479 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
480 if (q == (PixelPacket *) NULL)
481 break;
482 indexes=GetAuthenticIndexQueue(image);
cristybb503372010-05-27 20:51:26 +0000483 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +0000484 {
cristybb503372010-05-27 20:51:26 +0000485 byte=(size_t) ReadBlobByte(image);
cristyebc891a2011-04-24 23:04:16 +0000486 SetIndexPixelComponent(indexes+x,byte);
cristy3ed852e2009-09-05 21:47:34 +0000487 }
cristybb503372010-05-27 20:51:26 +0000488 for (x=0; x < (ssize_t) scanline_pad; x++)
cristy3ed852e2009-09-05 21:47:34 +0000489 (void) ReadBlobByte(image);
490 if (SyncAuthenticPixels(image,exception) == MagickFalse)
491 break;
492 if (image->previous == (Image *) NULL)
493 {
494 status=SetImageProgress(image,LoadImageTag,image->rows-y-1,
495 image->rows);
496 if (status == MagickFalse)
497 break;
498 }
499 }
500 break;
501 }
502 case 16:
503 {
504 /*
505 Convert PseudoColor scanline.
506 */
cristybb503372010-05-27 20:51:26 +0000507 for (y=(ssize_t) image->rows-1; y >= 0; y--)
cristy3ed852e2009-09-05 21:47:34 +0000508 {
509 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
510 if (q == (PixelPacket *) NULL)
511 break;
512 indexes=GetAuthenticIndexQueue(image);
cristybb503372010-05-27 20:51:26 +0000513 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +0000514 {
cristybb503372010-05-27 20:51:26 +0000515 byte=(size_t) ReadBlobByte(image);
516 byte|=(size_t) (ReadBlobByte(image) << 8);
cristyebc891a2011-04-24 23:04:16 +0000517 SetIndexPixelComponent(indexes+x,byte);
cristy3ed852e2009-09-05 21:47:34 +0000518 }
cristybb503372010-05-27 20:51:26 +0000519 for (x=0; x < (ssize_t) scanline_pad; x++)
cristy3ed852e2009-09-05 21:47:34 +0000520 (void) ReadBlobByte(image);
521 if (SyncAuthenticPixels(image,exception) == MagickFalse)
522 break;
523 if (image->previous == (Image *) NULL)
524 {
525 status=SetImageProgress(image,LoadImageTag,image->rows-y-1,
526 image->rows);
527 if (status == MagickFalse)
528 break;
529 }
530 }
531 break;
532 }
533 case 24:
534 case 32:
535 {
536 /*
537 Convert DirectColor scanline.
538 */
cristybb503372010-05-27 20:51:26 +0000539 for (y=(ssize_t) image->rows-1; y >= 0; y--)
cristy3ed852e2009-09-05 21:47:34 +0000540 {
541 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
542 if (q == (PixelPacket *) NULL)
543 break;
cristybb503372010-05-27 20:51:26 +0000544 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +0000545 {
cristyebc891a2011-04-24 23:04:16 +0000546 SetBluePixelComponent(q,ScaleCharToQuantum((unsigned char)
547 ReadBlobByte(image)));
548 SetGreenPixelComponent(q,ScaleCharToQuantum((unsigned char)
549 ReadBlobByte(image)));
550 SetRedPixelComponent(q,ScaleCharToQuantum((unsigned char)
551 ReadBlobByte(image)));
cristy3ed852e2009-09-05 21:47:34 +0000552 if (icon_info.bits_per_pixel == 32)
cristyebc891a2011-04-24 23:04:16 +0000553 SetOpacityPixelComponent(q,(QuantumRange-ScaleCharToQuantum(
554 (unsigned char) ReadBlobByte(image))));
cristy3ed852e2009-09-05 21:47:34 +0000555 q++;
556 }
557 if (icon_info.bits_per_pixel == 24)
cristybb503372010-05-27 20:51:26 +0000558 for (x=0; x < (ssize_t) scanline_pad; x++)
cristy3ed852e2009-09-05 21:47:34 +0000559 (void) ReadBlobByte(image);
560 if (SyncAuthenticPixels(image,exception) == MagickFalse)
561 break;
562 if (image->previous == (Image *) NULL)
563 {
564 status=SetImageProgress(image,LoadImageTag,image->rows-y-1,
565 image->rows);
566 if (status == MagickFalse)
567 break;
568 }
569 }
570 break;
571 }
572 default:
573 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
574 }
cristy5dd71882010-08-01 20:53:13 +0000575 if (image_info->ping == MagickFalse)
576 (void) SyncImage(image);
cristy3ed852e2009-09-05 21:47:34 +0000577 if (icon_info.bits_per_pixel != 32)
578 {
579 /*
580 Read the ICON alpha mask.
581 */
582 image->storage_class=DirectClass;
cristybb503372010-05-27 20:51:26 +0000583 for (y=(ssize_t) image->rows-1; y >= 0; y--)
cristy3ed852e2009-09-05 21:47:34 +0000584 {
585 q=GetAuthenticPixels(image,0,y,image->columns,1,exception);
586 if (q == (PixelPacket *) NULL)
587 break;
cristybb503372010-05-27 20:51:26 +0000588 for (x=0; x < ((ssize_t) image->columns-7); x+=8)
cristy3ed852e2009-09-05 21:47:34 +0000589 {
cristybb503372010-05-27 20:51:26 +0000590 byte=(size_t) ReadBlobByte(image);
cristy3ed852e2009-09-05 21:47:34 +0000591 for (bit=0; bit < 8; bit++)
cristyebc891a2011-04-24 23:04:16 +0000592 SetOpacityPixelComponent(q+x+bit,(((byte & (0x80 >> bit)) !=
593 0) ? TransparentOpacity : OpaqueOpacity));
cristy3ed852e2009-09-05 21:47:34 +0000594 }
595 if ((image->columns % 8) != 0)
596 {
cristybb503372010-05-27 20:51:26 +0000597 byte=(size_t) ReadBlobByte(image);
cristy3ed852e2009-09-05 21:47:34 +0000598 for (bit=0; bit < (image->columns % 8); bit++)
cristyebc891a2011-04-24 23:04:16 +0000599 SetOpacityPixelComponent(q+x+bit,(((byte & (0x80 >> bit)) !=
600 0) ? TransparentOpacity : OpaqueOpacity));
cristy3ed852e2009-09-05 21:47:34 +0000601 }
cristy0e8200f2009-09-13 21:10:06 +0000602 if ((image->columns % 32) != 0)
cristybb503372010-05-27 20:51:26 +0000603 for (x=0; x < (ssize_t) ((32-(image->columns % 32))/8); x++)
cristy0e8200f2009-09-13 21:10:06 +0000604 (void) ReadBlobByte(image);
cristy3ed852e2009-09-05 21:47:34 +0000605 if (SyncAuthenticPixels(image,exception) == MagickFalse)
606 break;
607 }
608 }
609 if (EOFBlob(image) != MagickFalse)
610 {
611 ThrowFileException(exception,CorruptImageError,
612 "UnexpectedEndOfFile",image->filename);
613 break;
614 }
615 }
616 /*
617 Proceed to next image.
618 */
619 if (image_info->number_scenes != 0)
620 if (image->scene >= (image_info->scene+image_info->number_scenes-1))
621 break;
cristybb503372010-05-27 20:51:26 +0000622 if (i < (ssize_t) (icon_file.count-1))
cristy3ed852e2009-09-05 21:47:34 +0000623 {
624 /*
625 Allocate next image structure.
626 */
627 AcquireNextImage(image_info,image);
628 if (GetNextImageInList(image) == (Image *) NULL)
629 {
630 image=DestroyImageList(image);
631 return((Image *) NULL);
632 }
633 image=SyncNextImageInList(image);
634 status=SetImageProgress(image,LoadImagesTag,TellBlob(image),
635 GetBlobSize(image));
636 if (status == MagickFalse)
637 break;
638 }
639 }
640 (void) CloseBlob(image);
641 return(GetFirstImageInList(image));
642}
643
644/*
645%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
646% %
647% %
648% %
649% R e g i s t e r I C O N I m a g e %
650% %
651% %
652% %
653%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
654%
655% RegisterICONImage() adds attributes for the Icon image format to
656% the list of supported formats. The attributes include the image format
657% tag, a method to read and/or write the format, whether the format
658% supports the saving of more than one frame to the same file or blob,
659% whether the format supports native in-memory I/O, and a brief
660% description of the format.
661%
662% The format of the RegisterICONImage method is:
663%
cristybb503372010-05-27 20:51:26 +0000664% size_t RegisterICONImage(void)
cristy3ed852e2009-09-05 21:47:34 +0000665%
666*/
cristybb503372010-05-27 20:51:26 +0000667ModuleExport size_t RegisterICONImage(void)
cristy3ed852e2009-09-05 21:47:34 +0000668{
669 MagickInfo
670 *entry;
671
672 entry=SetMagickInfo("CUR");
673 entry->decoder=(DecodeImageHandler *) ReadICONImage;
674 entry->encoder=(EncodeImageHandler *) WriteICONImage;
675 entry->adjoin=MagickFalse;
676 entry->seekable_stream=MagickTrue;
677 entry->description=ConstantString("Microsoft icon");
678 entry->module=ConstantString("CUR");
679 (void) RegisterMagickInfo(entry);
680 entry=SetMagickInfo("ICO");
681 entry->decoder=(DecodeImageHandler *) ReadICONImage;
682 entry->encoder=(EncodeImageHandler *) WriteICONImage;
683 entry->adjoin=MagickTrue;
684 entry->seekable_stream=MagickTrue;
685 entry->description=ConstantString("Microsoft icon");
686 entry->module=ConstantString("ICON");
687 (void) RegisterMagickInfo(entry);
688 entry=SetMagickInfo("ICON");
689 entry->decoder=(DecodeImageHandler *) ReadICONImage;
690 entry->encoder=(EncodeImageHandler *) WriteICONImage;
691 entry->adjoin=MagickFalse;
692 entry->seekable_stream=MagickTrue;
693 entry->description=ConstantString("Microsoft icon");
694 entry->module=ConstantString("ICON");
695 (void) RegisterMagickInfo(entry);
696 return(MagickImageCoderSignature);
697}
698
699/*
700%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
701% %
702% %
703% %
704% U n r e g i s t e r I C O N I m a g e %
705% %
706% %
707% %
708%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
709%
710% UnregisterICONImage() removes format registrations made by the
711% ICON module from the list of supported formats.
712%
713% The format of the UnregisterICONImage method is:
714%
715% UnregisterICONImage(void)
716%
717*/
718ModuleExport void UnregisterICONImage(void)
719{
720 (void) UnregisterMagickInfo("CUR");
721 (void) UnregisterMagickInfo("ICO");
722 (void) UnregisterMagickInfo("ICON");
723}
724
725/*
726%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
727% %
728% %
729% %
730% W r i t e I C O N I m a g e %
731% %
732% %
733% %
734%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
735%
736% WriteICONImage() writes an image in Microsoft Windows bitmap encoded
737% image format, version 3 for Windows or (if the image has a matte channel)
738% version 4.
739%
740% The format of the WriteICONImage method is:
741%
742% MagickBooleanType WriteICONImage(const ImageInfo *image_info,
743% Image *image)
744%
745% A description of each parameter follows.
746%
747% o image_info: the image info.
748%
749% o image: The image.
750%
751*/
752static MagickBooleanType WriteICONImage(const ImageInfo *image_info,
753 Image *image)
754{
755 IconFile
756 icon_file;
757
758 IconInfo
759 icon_info;
760
761 Image
762 *next;
763
cristy3ed852e2009-09-05 21:47:34 +0000764 MagickBooleanType
765 status;
766
767 MagickOffsetType
768 offset,
769 scene;
770
771 register const IndexPacket
772 *indexes;
773
774 register const PixelPacket
775 *p;
776
cristybb503372010-05-27 20:51:26 +0000777 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000778 i,
779 x;
780
781 register unsigned char
782 *q;
783
cristyebc891a2011-04-24 23:04:16 +0000784 size_t
785 bytes_per_line,
786 scanline_pad;
787
788 ssize_t
789 y;
790
cristy3ed852e2009-09-05 21:47:34 +0000791 unsigned char
792 bit,
793 byte,
794 *pixels;
795
cristy3ed852e2009-09-05 21:47:34 +0000796 /*
797 Open output image file.
798 */
799 assert(image_info != (const ImageInfo *) NULL);
800 assert(image_info->signature == MagickSignature);
801 assert(image != (Image *) NULL);
802 assert(image->signature == MagickSignature);
803 (void) LogMagickEvent(CoderEvent,GetMagickModule(),"%s",image->filename);
804 status=OpenBlob(image_info,image,WriteBinaryBlobMode,&image->exception);
805 if (status == MagickFalse)
806 return(status);
807 scene=0;
808 next=image;
809 do
810 {
811 if ((image->columns > 256L) || (image->rows > 256L))
812 ThrowWriterException(ImageError,"WidthOrHeightExceedsLimit");
813 scene++;
814 next=SyncNextImageInList(next);
815 } while ((next != (Image *) NULL) && (image_info->adjoin != MagickFalse));
816 /*
817 Dump out a ICON header template to be properly initialized later.
818 */
819 (void) WriteBlobLSBShort(image,0);
820 (void) WriteBlobLSBShort(image,1);
821 (void) WriteBlobLSBShort(image,(unsigned char) scene);
822 (void) ResetMagickMemory(&icon_file,0,sizeof(icon_file));
823 (void) ResetMagickMemory(&icon_info,0,sizeof(icon_info));
824 scene=0;
825 next=image;
826 do
827 {
828 (void) WriteBlobByte(image,icon_file.directory[scene].width);
829 (void) WriteBlobByte(image,icon_file.directory[scene].height);
830 (void) WriteBlobByte(image,icon_file.directory[scene].colors);
831 (void) WriteBlobByte(image,icon_file.directory[scene].reserved);
832 (void) WriteBlobLSBShort(image,icon_file.directory[scene].planes);
833 (void) WriteBlobLSBShort(image,icon_file.directory[scene].bits_per_pixel);
cristy0b29b252010-05-30 01:59:46 +0000834 (void) WriteBlobLSBLong(image,(unsigned int)
835 icon_file.directory[scene].size);
836 (void) WriteBlobLSBLong(image,(unsigned int)
837 icon_file.directory[scene].offset);
cristy3ed852e2009-09-05 21:47:34 +0000838 scene++;
839 next=SyncNextImageInList(next);
840 } while ((next != (Image *) NULL) && (image_info->adjoin != MagickFalse));
841 scene=0;
842 next=image;
843 do
844 {
845 if ((next->columns == 256) && (next->rows == 256))
846 {
847 Image
848 *write_image;
849
850 ImageInfo
851 *write_info;
852
853 size_t
854 length;
855
856 unsigned char
857 *png;
858
859 /*
860 Icon image encoded as a compressed PNG image.
861 */
862 write_image=CloneImage(next,0,0,MagickTrue,&image->exception);
863 if (write_image == (Image *) NULL)
864 return(MagickFalse);
865 write_info=CloneImageInfo(image_info);
866 (void) CopyMagickString(write_info->filename,"PNG:",MaxTextExtent);
867 png=(unsigned char *) ImageToBlob(write_info,write_image,&length,
868 &image->exception);
869 write_image=DestroyImage(write_image);
870 write_info=DestroyImageInfo(write_info);
871 if (png == (unsigned char *) NULL)
872 return(MagickFalse);
873 icon_file.directory[scene].width=0;
874 icon_file.directory[scene].height=0;
875 icon_file.directory[scene].colors=0;
876 icon_file.directory[scene].reserved=0;
877 icon_file.directory[scene].planes=1;
878 icon_file.directory[scene].bits_per_pixel=32;
cristybb503372010-05-27 20:51:26 +0000879 icon_file.directory[scene].size=(size_t) length;
880 icon_file.directory[scene].offset=(size_t) TellBlob(image);
cristy3ed852e2009-09-05 21:47:34 +0000881 (void) WriteBlob(image,(size_t) length,png);
882 png=(unsigned char *) RelinquishMagickMemory(png);
883 }
884 else
885 {
886 /*
887 Initialize ICON raster file header.
888 */
889 if (next->colorspace != RGBColorspace)
890 (void) TransformImageColorspace(next,RGBColorspace);
891 icon_info.file_size=14+12+28;
892 icon_info.offset_bits=icon_info.file_size;
893 icon_info.compression=BI_RGB;
894 if ((next->storage_class != DirectClass) && (next->colors > 256))
895 (void) SetImageStorageClass(next,DirectClass);
896 if (next->storage_class == DirectClass)
897 {
898 /*
899 Full color ICON raster.
900 */
901 icon_info.number_colors=0;
902 icon_info.bits_per_pixel=32;
cristybb503372010-05-27 20:51:26 +0000903 icon_info.compression=(size_t) BI_RGB;
cristy3ed852e2009-09-05 21:47:34 +0000904 }
905 else
906 {
cristy35ef8242010-06-03 16:24:13 +0000907 size_t
908 one;
909
cristy3ed852e2009-09-05 21:47:34 +0000910 /*
911 Colormapped ICON raster.
912 */
913 icon_info.bits_per_pixel=8;
914 if (next->colors <= 256)
915 icon_info.bits_per_pixel=8;
916 if (next->colors <= 16)
917 icon_info.bits_per_pixel=4;
918 if (next->colors <= 2)
919 icon_info.bits_per_pixel=1;
cristy35ef8242010-06-03 16:24:13 +0000920 one=1;
921 icon_info.number_colors=one << icon_info.bits_per_pixel;
cristy3ed852e2009-09-05 21:47:34 +0000922 if (icon_info.number_colors < next->colors)
923 {
924 (void) SetImageStorageClass(next,DirectClass);
925 icon_info.number_colors=0;
926 icon_info.bits_per_pixel=(unsigned short) 24;
cristybb503372010-05-27 20:51:26 +0000927 icon_info.compression=(size_t) BI_RGB;
cristy3ed852e2009-09-05 21:47:34 +0000928 }
929 else
930 {
cristy0b29b252010-05-30 01:59:46 +0000931 size_t
932 one;
933
934 one=1;
935 icon_info.file_size+=3*(one << icon_info.bits_per_pixel);
936 icon_info.offset_bits+=3*(one << icon_info.bits_per_pixel);
937 icon_info.file_size+=(one << icon_info.bits_per_pixel);
938 icon_info.offset_bits+=(one << icon_info.bits_per_pixel);
cristy3ed852e2009-09-05 21:47:34 +0000939 }
940 }
941 bytes_per_line=(((next->columns*icon_info.bits_per_pixel)+31) &
942 ~31) >> 3;
943 icon_info.ba_offset=0;
cristybb503372010-05-27 20:51:26 +0000944 icon_info.width=(ssize_t) next->columns;
945 icon_info.height=(ssize_t) next->rows;
cristy3ed852e2009-09-05 21:47:34 +0000946 icon_info.planes=1;
947 icon_info.image_size=bytes_per_line*next->rows;
948 icon_info.size=40;
949 icon_info.size+=(4*icon_info.number_colors);
950 icon_info.size+=icon_info.image_size;
951 icon_info.size+=(((icon_info.width+31) & ~31) >> 3)*icon_info.height;
952 icon_info.file_size+=icon_info.image_size;
953 icon_info.x_pixels=0;
954 icon_info.y_pixels=0;
955 switch (next->units)
956 {
957 case UndefinedResolution:
958 case PixelsPerInchResolution:
959 {
cristybb503372010-05-27 20:51:26 +0000960 icon_info.x_pixels=(size_t) (100.0*next->x_resolution/2.54);
961 icon_info.y_pixels=(size_t) (100.0*next->y_resolution/2.54);
cristy3ed852e2009-09-05 21:47:34 +0000962 break;
963 }
964 case PixelsPerCentimeterResolution:
965 {
cristybb503372010-05-27 20:51:26 +0000966 icon_info.x_pixels=(size_t) (100.0*next->x_resolution);
967 icon_info.y_pixels=(size_t) (100.0*next->y_resolution);
cristy3ed852e2009-09-05 21:47:34 +0000968 break;
969 }
970 }
971 icon_info.colors_important=icon_info.number_colors;
972 /*
973 Convert MIFF to ICON raster pixels.
974 */
975 pixels=(unsigned char *) AcquireQuantumMemory((size_t)
976 icon_info.image_size,sizeof(*pixels));
977 if (pixels == (unsigned char *) NULL)
978 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
979 (void) ResetMagickMemory(pixels,0,(size_t) icon_info.image_size);
980 switch (icon_info.bits_per_pixel)
981 {
982 case 1:
983 {
cristybb503372010-05-27 20:51:26 +0000984 size_t
cristy3ed852e2009-09-05 21:47:34 +0000985 bit,
986 byte;
987
988 /*
989 Convert PseudoClass image to a ICON monochrome image.
990 */
cristybb503372010-05-27 20:51:26 +0000991 for (y=0; y < (ssize_t) next->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +0000992 {
993 p=GetVirtualPixels(next,0,y,next->columns,1,&next->exception);
994 if (p == (const PixelPacket *) NULL)
995 break;
996 indexes=GetVirtualIndexQueue(next);
997 q=pixels+(next->rows-y-1)*bytes_per_line;
998 bit=0;
999 byte=0;
cristybb503372010-05-27 20:51:26 +00001000 for (x=0; x < (ssize_t) next->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00001001 {
1002 byte<<=1;
cristyebc891a2011-04-24 23:04:16 +00001003 byte|=GetIndexPixelComponent(indexes+x) != 0 ? 0x01 : 0x00;
cristy3ed852e2009-09-05 21:47:34 +00001004 bit++;
1005 if (bit == 8)
1006 {
1007 *q++=(unsigned char) byte;
1008 bit=0;
1009 byte=0;
1010 }
1011 }
1012 if (bit != 0)
1013 *q++=(unsigned char) (byte << (8-bit));
1014 if (next->previous == (Image *) NULL)
1015 {
1016 status=SetImageProgress(next,SaveImageTag,y,next->rows);
1017 if (status == MagickFalse)
1018 break;
1019 }
1020 }
1021 break;
1022 }
1023 case 4:
1024 {
cristybb503372010-05-27 20:51:26 +00001025 size_t
cristy3ed852e2009-09-05 21:47:34 +00001026 nibble,
1027 byte;
1028
1029 /*
1030 Convert PseudoClass image to a ICON monochrome image.
1031 */
cristybb503372010-05-27 20:51:26 +00001032 for (y=0; y < (ssize_t) next->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00001033 {
1034 p=GetVirtualPixels(next,0,y,next->columns,1,&next->exception);
1035 if (p == (const PixelPacket *) NULL)
1036 break;
1037 indexes=GetVirtualIndexQueue(next);
1038 q=pixels+(next->rows-y-1)*bytes_per_line;
1039 nibble=0;
1040 byte=0;
cristybb503372010-05-27 20:51:26 +00001041 for (x=0; x < (ssize_t) next->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00001042 {
1043 byte<<=4;
cristyebc891a2011-04-24 23:04:16 +00001044 byte|=((size_t) GetIndexPixelComponent(indexes+x) & 0x0f);
cristy3ed852e2009-09-05 21:47:34 +00001045 nibble++;
1046 if (nibble == 2)
1047 {
1048 *q++=(unsigned char) byte;
1049 nibble=0;
1050 byte=0;
1051 }
1052 }
1053 if (nibble != 0)
1054 *q++=(unsigned char) (byte << 4);
1055 if (next->previous == (Image *) NULL)
1056 {
1057 status=SetImageProgress(next,SaveImageTag,y,next->rows);
1058 if (status == MagickFalse)
1059 break;
1060 }
1061 }
1062 break;
1063 }
1064 case 8:
1065 {
1066 /*
1067 Convert PseudoClass packet to ICON pixel.
1068 */
cristybb503372010-05-27 20:51:26 +00001069 for (y=0; y < (ssize_t) next->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00001070 {
1071 p=GetVirtualPixels(next,0,y,next->columns,1,&next->exception);
1072 if (p == (const PixelPacket *) NULL)
1073 break;
1074 indexes=GetVirtualIndexQueue(next);
1075 q=pixels+(next->rows-y-1)*bytes_per_line;
cristybb503372010-05-27 20:51:26 +00001076 for (x=0; x < (ssize_t) next->columns; x++)
cristy4e82e512011-04-24 01:33:42 +00001077 *q++=(unsigned char) GetIndexPixelComponent(indexes+x);
cristy3ed852e2009-09-05 21:47:34 +00001078 if (next->previous == (Image *) NULL)
1079 {
1080 status=SetImageProgress(next,SaveImageTag,y,next->rows);
1081 if (status == MagickFalse)
1082 break;
1083 }
1084 }
1085 break;
1086 }
1087 case 24:
1088 case 32:
1089 {
1090 /*
1091 Convert DirectClass packet to ICON BGR888 or BGRA8888 pixel.
1092 */
cristybb503372010-05-27 20:51:26 +00001093 for (y=0; y < (ssize_t) next->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00001094 {
1095 p=GetVirtualPixels(next,0,y,next->columns,1,&next->exception);
1096 if (p == (const PixelPacket *) NULL)
1097 break;
1098 q=pixels+(next->rows-y-1)*bytes_per_line;
cristybb503372010-05-27 20:51:26 +00001099 for (x=0; x < (ssize_t) next->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00001100 {
cristyce70c172010-01-07 17:15:30 +00001101 *q++=ScaleQuantumToChar(GetBluePixelComponent(p));
1102 *q++=ScaleQuantumToChar(GetGreenPixelComponent(p));
1103 *q++=ScaleQuantumToChar(GetRedPixelComponent(p));
cristy3ed852e2009-09-05 21:47:34 +00001104 if (next->matte == MagickFalse)
1105 *q++=ScaleQuantumToChar(QuantumRange);
1106 else
cristy46f08202010-01-10 04:04:21 +00001107 *q++=ScaleQuantumToChar(GetAlphaPixelComponent(p));
cristy3ed852e2009-09-05 21:47:34 +00001108 p++;
1109 }
1110 if (icon_info.bits_per_pixel == 24)
cristybb503372010-05-27 20:51:26 +00001111 for (x=3L*(ssize_t) next->columns; x < (ssize_t) bytes_per_line; x++)
cristy3ed852e2009-09-05 21:47:34 +00001112 *q++=0x00;
1113 if (next->previous == (Image *) NULL)
1114 {
1115 status=SetImageProgress(next,SaveImageTag,y,next->rows);
1116 if (status == MagickFalse)
1117 break;
1118 }
1119 }
1120 break;
1121 }
1122 }
1123 /*
1124 Write 40-byte version 3+ bitmap header.
1125 */
1126 icon_file.directory[scene].width=(unsigned char) icon_info.width;
1127 icon_file.directory[scene].height=(unsigned char) icon_info.height;
1128 icon_file.directory[scene].colors=(unsigned char)
1129 icon_info.number_colors;
1130 icon_file.directory[scene].reserved=0;
1131 icon_file.directory[scene].planes=icon_info.planes;
1132 icon_file.directory[scene].bits_per_pixel=icon_info.bits_per_pixel;
1133 icon_file.directory[scene].size=icon_info.size;
cristybb503372010-05-27 20:51:26 +00001134 icon_file.directory[scene].offset=(size_t) TellBlob(image);
cristy35ef8242010-06-03 16:24:13 +00001135 (void) WriteBlobLSBLong(image,(unsigned int) 40);
1136 (void) WriteBlobLSBLong(image,(unsigned int) icon_info.width);
1137 (void) WriteBlobLSBLong(image,(unsigned int) icon_info.height*2);
cristy3ed852e2009-09-05 21:47:34 +00001138 (void) WriteBlobLSBShort(image,icon_info.planes);
1139 (void) WriteBlobLSBShort(image,icon_info.bits_per_pixel);
cristy35ef8242010-06-03 16:24:13 +00001140 (void) WriteBlobLSBLong(image,(unsigned int) icon_info.compression);
1141 (void) WriteBlobLSBLong(image,(unsigned int) icon_info.image_size);
1142 (void) WriteBlobLSBLong(image,(unsigned int) icon_info.x_pixels);
1143 (void) WriteBlobLSBLong(image,(unsigned int) icon_info.y_pixels);
1144 (void) WriteBlobLSBLong(image,(unsigned int) icon_info.number_colors);
1145 (void) WriteBlobLSBLong(image,(unsigned int) icon_info.colors_important);
cristy3ed852e2009-09-05 21:47:34 +00001146 if (next->storage_class == PseudoClass)
1147 {
1148 unsigned char
1149 *icon_colormap;
1150
1151 /*
1152 Dump colormap to file.
1153 */
1154 icon_colormap=(unsigned char *) AcquireQuantumMemory((size_t)
1155 (1UL << icon_info.bits_per_pixel),4UL*sizeof(*icon_colormap));
1156 if (icon_colormap == (unsigned char *) NULL)
1157 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
1158 q=icon_colormap;
cristybb503372010-05-27 20:51:26 +00001159 for (i=0; i < (ssize_t) next->colors; i++)
cristy3ed852e2009-09-05 21:47:34 +00001160 {
1161 *q++=ScaleQuantumToChar(next->colormap[i].blue);
1162 *q++=ScaleQuantumToChar(next->colormap[i].green);
1163 *q++=ScaleQuantumToChar(next->colormap[i].red);
1164 *q++=(unsigned char) 0x0;
1165 }
cristybb503372010-05-27 20:51:26 +00001166 for ( ; i < (ssize_t) (1UL << icon_info.bits_per_pixel); i++)
cristy3ed852e2009-09-05 21:47:34 +00001167 {
1168 *q++=(unsigned char) 0x00;
1169 *q++=(unsigned char) 0x00;
1170 *q++=(unsigned char) 0x00;
1171 *q++=(unsigned char) 0x00;
1172 }
1173 (void) WriteBlob(image,(size_t) (4UL*(1UL <<
1174 icon_info.bits_per_pixel)),icon_colormap);
1175 icon_colormap=(unsigned char *) RelinquishMagickMemory(
1176 icon_colormap);
1177 }
1178 (void) WriteBlob(image,(size_t) icon_info.image_size,pixels);
1179 pixels=(unsigned char *) RelinquishMagickMemory(pixels);
1180 /*
1181 Write matte mask.
1182 */
1183 scanline_pad=(((next->columns+31) & ~31)-next->columns) >> 3;
cristybb503372010-05-27 20:51:26 +00001184 for (y=((ssize_t) next->rows - 1); y >= 0; y--)
cristy3ed852e2009-09-05 21:47:34 +00001185 {
1186 p=GetVirtualPixels(next,0,y,next->columns,1,&next->exception);
1187 if (p == (const PixelPacket *) NULL)
1188 break;
1189 bit=0;
1190 byte=0;
cristybb503372010-05-27 20:51:26 +00001191 for (x=0; x < (ssize_t) next->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00001192 {
1193 byte<<=1;
1194 if ((next->matte == MagickTrue) &&
cristyd05ecd12011-04-22 20:44:42 +00001195 (GetOpacityPixelComponent(p) == (Quantum) TransparentOpacity))
cristy3ed852e2009-09-05 21:47:34 +00001196 byte|=0x01;
1197 bit++;
1198 if (bit == 8)
1199 {
1200 (void) WriteBlobByte(image,(unsigned char) byte);
1201 bit=0;
1202 byte=0;
1203 }
1204 p++;
1205 }
1206 if (bit != 0)
1207 (void) WriteBlobByte(image,(unsigned char) (byte << (8-bit)));
cristybb503372010-05-27 20:51:26 +00001208 for (i=0; i < (ssize_t) scanline_pad; i++)
cristy3ed852e2009-09-05 21:47:34 +00001209 (void) WriteBlobByte(image,(unsigned char) 0);
1210 }
1211 }
1212 if (GetNextImageInList(next) == (Image *) NULL)
1213 break;
1214 next=SyncNextImageInList(next);
1215 status=SetImageProgress(next,SaveImagesTag,scene++,
1216 GetImageListLength(next));
1217 if (status == MagickFalse)
1218 break;
1219 } while ((next != (Image *) NULL) && (image_info->adjoin != MagickFalse));
1220 offset=SeekBlob(image,0,SEEK_SET);
cristyda16f162011-02-19 23:52:17 +00001221 (void) offset;
cristy3ed852e2009-09-05 21:47:34 +00001222 (void) WriteBlobLSBShort(image,0);
1223 (void) WriteBlobLSBShort(image,1);
1224 (void) WriteBlobLSBShort(image,(unsigned short) (scene+1));
1225 scene=0;
1226 next=image;
1227 do
1228 {
1229 (void) WriteBlobByte(image,icon_file.directory[scene].width);
1230 (void) WriteBlobByte(image,icon_file.directory[scene].height);
1231 (void) WriteBlobByte(image,icon_file.directory[scene].colors);
1232 (void) WriteBlobByte(image,icon_file.directory[scene].reserved);
1233 (void) WriteBlobLSBShort(image,icon_file.directory[scene].planes);
1234 (void) WriteBlobLSBShort(image,icon_file.directory[scene].bits_per_pixel);
cristy0b29b252010-05-30 01:59:46 +00001235 (void) WriteBlobLSBLong(image,(unsigned int)
1236 icon_file.directory[scene].size);
1237 (void) WriteBlobLSBLong(image,(unsigned int)
1238 icon_file.directory[scene].offset);
cristy3ed852e2009-09-05 21:47:34 +00001239 scene++;
1240 next=SyncNextImageInList(next);
1241 } while ((next != (Image *) NULL) && (image_info->adjoin != MagickFalse));
1242 (void) CloseBlob(image);
1243 return(MagickTrue);
1244}