blob: 81879ddee0c10dc1b2b8fb0340137adb4b6ba19a [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*/
cristy4c08aed2011-07-01 19:47:50 +000042#include "MagickCore/studio.h"
43#include "MagickCore/blob.h"
44#include "MagickCore/blob-private.h"
45#include "MagickCore/cache.h"
46#include "MagickCore/colormap.h"
47#include "MagickCore/colorspace.h"
48#include "MagickCore/exception.h"
49#include "MagickCore/exception-private.h"
50#include "MagickCore/image.h"
51#include "MagickCore/image-private.h"
52#include "MagickCore/list.h"
53#include "MagickCore/log.h"
54#include "MagickCore/magick.h"
55#include "MagickCore/memory_.h"
56#include "MagickCore/monitor.h"
57#include "MagickCore/monitor-private.h"
58#include "MagickCore/nt-feature.h"
59#include "MagickCore/pixel-accessor.h"
60#include "MagickCore/quantize.h"
61#include "MagickCore/quantum-private.h"
62#include "MagickCore/static.h"
63#include "MagickCore/string_.h"
64#include "MagickCore/module.h"
cristy3ed852e2009-09-05 21:47:34 +000065
66/*
67 Define declarations.
68*/
cristy0157aea2010-04-24 21:12:18 +000069#if !defined(MAGICKCORE_WINDOWS_SUPPORT) || defined(__MINGW32__)
cristy3ed852e2009-09-05 21:47:34 +000070#define BI_RGB 0
71#define BI_RLE8 1
72#define BI_BITFIELDS 3
73#endif
74#define MaxIcons 1024
75
76/*
77 Typedef declarations.
78*/
79typedef struct _IconEntry
80{
81 unsigned char
82 width,
83 height,
84 colors,
85 reserved;
86
87 unsigned short int
88 planes,
89 bits_per_pixel;
90
cristybb503372010-05-27 20:51:26 +000091 size_t
cristy3ed852e2009-09-05 21:47:34 +000092 size,
93 offset;
94} IconEntry;
95
96typedef struct _IconFile
97{
98 short
99 reserved,
100 resource_type,
101 count;
102
103 IconEntry
104 directory[MaxIcons];
105} IconFile;
106
107typedef struct _IconInfo
108{
cristybb503372010-05-27 20:51:26 +0000109 size_t
cristy3ed852e2009-09-05 21:47:34 +0000110 file_size,
111 ba_offset,
112 offset_bits,
113 size;
114
cristybb503372010-05-27 20:51:26 +0000115 ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000116 width,
117 height;
118
119 unsigned short
120 planes,
121 bits_per_pixel;
122
cristybb503372010-05-27 20:51:26 +0000123 size_t
cristy3ed852e2009-09-05 21:47:34 +0000124 compression,
125 image_size,
126 x_pixels,
127 y_pixels,
128 number_colors,
129 red_mask,
130 green_mask,
131 blue_mask,
132 alpha_mask,
133 colors_important;
134
cristybb503372010-05-27 20:51:26 +0000135 ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000136 colorspace;
137} IconInfo;
138
139/*
140 Forward declaractions.
141*/
142static MagickBooleanType
cristy1e178e72011-08-28 19:44:34 +0000143 WriteICONImage(const ImageInfo *,Image *,ExceptionInfo *);
cristy3ed852e2009-09-05 21:47:34 +0000144
145/*
146%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
147% %
148% %
149% %
150% R e a d I C O N I m a g e %
151% %
152% %
153% %
154%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
155%
156% ReadICONImage() reads a Microsoft icon image file and returns it. It
157% allocates the memory necessary for the new Image structure and returns a
158% pointer to the new image.
159%
160% The format of the ReadICONImage method is:
161%
162% Image *ReadICONImage(const ImageInfo *image_info,
163% ExceptionInfo *exception)
164%
165% A description of each parameter follows:
166%
167% o image_info: the image info.
168%
169% o exception: return any errors or warnings in this structure.
170%
171*/
172static Image *ReadICONImage(const ImageInfo *image_info,
173 ExceptionInfo *exception)
174{
175 IconFile
176 icon_file;
177
178 IconInfo
179 icon_info;
180
181 Image
182 *image;
183
cristy3ed852e2009-09-05 21:47:34 +0000184 MagickBooleanType
185 status;
186
cristybb503372010-05-27 20:51:26 +0000187 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000188 i,
189 x;
190
cristy4c08aed2011-07-01 19:47:50 +0000191 register Quantum
cristy3ed852e2009-09-05 21:47:34 +0000192 *q;
193
194 register unsigned char
195 *p;
196
cristybb503372010-05-27 20:51:26 +0000197 size_t
cristy3ed852e2009-09-05 21:47:34 +0000198 bit,
cristyeaedf062010-05-29 22:36:02 +0000199 byte,
cristy3ed852e2009-09-05 21:47:34 +0000200 bytes_per_line,
cristyeaedf062010-05-29 22:36:02 +0000201 one,
cristy3ed852e2009-09-05 21:47:34 +0000202 scanline_pad;
203
cristyebc891a2011-04-24 23:04:16 +0000204 ssize_t
205 count,
206 offset,
207 y;
208
cristy3ed852e2009-09-05 21:47:34 +0000209 /*
210 Open image file.
211 */
212 assert(image_info != (const ImageInfo *) NULL);
213 assert(image_info->signature == MagickSignature);
214 (void) LogMagickEvent(CoderEvent,GetMagickModule(),"%s",image_info->filename);
215 assert(exception != (ExceptionInfo *) NULL);
216 assert(exception->signature == MagickSignature);
cristy1f1de182011-10-05 18:41:34 +0000217 image=AcquireImage(image_info,exception);
cristy3ed852e2009-09-05 21:47:34 +0000218 status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception);
219 if (status == MagickFalse)
220 {
221 image=DestroyImageList(image);
222 return((Image *) NULL);
223 }
224 icon_file.reserved=(short) ReadBlobLSBShort(image);
225 icon_file.resource_type=(short) ReadBlobLSBShort(image);
226 icon_file.count=(short) ReadBlobLSBShort(image);
227 if ((icon_file.reserved != 0) ||
228 ((icon_file.resource_type != 1) && (icon_file.resource_type != 2)) ||
229 (icon_file.count > MaxIcons))
230 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
231 for (i=0; i < icon_file.count; i++)
232 {
233 icon_file.directory[i].width=(unsigned char) ReadBlobByte(image);
234 icon_file.directory[i].height=(unsigned char) ReadBlobByte(image);
235 icon_file.directory[i].colors=(unsigned char) ReadBlobByte(image);
236 icon_file.directory[i].reserved=(unsigned char) ReadBlobByte(image);
237 icon_file.directory[i].planes=(unsigned short) ReadBlobLSBShort(image);
238 icon_file.directory[i].bits_per_pixel=(unsigned short)
239 ReadBlobLSBShort(image);
240 icon_file.directory[i].size=ReadBlobLSBLong(image);
241 icon_file.directory[i].offset=ReadBlobLSBLong(image);
242 }
cristyeaedf062010-05-29 22:36:02 +0000243 one=1;
cristy3ed852e2009-09-05 21:47:34 +0000244 for (i=0; i < icon_file.count; i++)
245 {
246 /*
247 Verify Icon identifier.
248 */
249 offset=(ssize_t) SeekBlob(image,(MagickOffsetType)
250 icon_file.directory[i].offset,SEEK_SET);
251 if (offset < 0)
252 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
253 icon_info.size=ReadBlobLSBLong(image);
cristyd63c0502011-01-03 15:45:11 +0000254 icon_info.width=(unsigned char) ((int) ReadBlobLSBLong(image));
255 icon_info.height=(unsigned char) ((int) ReadBlobLSBLong(image)/2);
cristy4e09e032011-10-05 19:42:10 +0000256 icon_info.planes=ReadBlobLSBShort(image);
257 icon_info.bits_per_pixel=ReadBlobLSBShort(image);
258 if ((icon_info.planes == 18505) && (icon_info.bits_per_pixel == 21060))
cristy3ed852e2009-09-05 21:47:34 +0000259 {
260 Image
261 *icon_image;
262
263 ImageInfo
264 *read_info;
265
cristyf054ee72011-10-05 17:04:08 +0000266 size_t
267 length;
268
269 unsigned char
270 *png;
271
cristy3ed852e2009-09-05 21:47:34 +0000272 /*
273 Icon image encoded as a compressed PNG image.
274 */
cristyf054ee72011-10-05 17:04:08 +0000275 length=icon_file.directory[i].size;
cristy4e09e032011-10-05 19:42:10 +0000276 png=(unsigned char *) AcquireQuantumMemory(length+16,sizeof(*png));
cristyf054ee72011-10-05 17:04:08 +0000277 if (png == (unsigned char *) NULL)
278 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
279 (void) CopyMagickMemory(png,"\211PNG\r\n\032\n\000\000\000\015",12);
cristy4e09e032011-10-05 19:42:10 +0000280 png[12]=(unsigned char) icon_info.planes;
281 png[13]=(unsigned char) (icon_info.planes >> 8);
282 png[14]=(unsigned char) icon_info.bits_per_pixel;
283 png[15]=(unsigned char) (icon_info.bits_per_pixel >> 8);
284 count=ReadBlob(image,length-16,png+16);
285 if (count != (ssize_t) (length-16))
cristyf054ee72011-10-05 17:04:08 +0000286 {
287 png=(unsigned char *) RelinquishMagickMemory(png);
288 ThrowReaderException(CorruptImageError,
289 "InsufficientImageDataInFile");
290 }
cristy3ed852e2009-09-05 21:47:34 +0000291 read_info=CloneImageInfo(image_info);
292 (void) CopyMagickString(read_info->magick,"PNG",MaxTextExtent);
cristy4e09e032011-10-05 19:42:10 +0000293 icon_image=BlobToImage(read_info,png,length+16,exception);
cristy3ed852e2009-09-05 21:47:34 +0000294 read_info=DestroyImageInfo(read_info);
cristyf054ee72011-10-05 17:04:08 +0000295 png=(unsigned char *) RelinquishMagickMemory(png);
cristy47de5a92011-10-05 01:47:42 +0000296 if (icon_image == (Image *) NULL)
cristy3ed852e2009-09-05 21:47:34 +0000297 {
cristy47de5a92011-10-05 01:47:42 +0000298 image=DestroyImageList(image);
299 return((Image *) NULL);
cristy3ed852e2009-09-05 21:47:34 +0000300 }
cristy47de5a92011-10-05 01:47:42 +0000301 DestroyBlob(icon_image);
302 icon_image->blob=ReferenceBlob(image->blob);
303 ReplaceImageInList(&image,icon_image);
cristy3ed852e2009-09-05 21:47:34 +0000304 }
305 else
306 {
cristyf054ee72011-10-05 17:04:08 +0000307 if (icon_info.bits_per_pixel > 32)
308 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
309 icon_info.compression=ReadBlobLSBLong(image);
310 icon_info.image_size=ReadBlobLSBLong(image);
311 icon_info.x_pixels=ReadBlobLSBLong(image);
312 icon_info.y_pixels=ReadBlobLSBLong(image);
313 icon_info.number_colors=ReadBlobLSBLong(image);
314 icon_info.colors_important=ReadBlobLSBLong(image);
315 image->matte=MagickTrue;
316 image->columns=(size_t) icon_file.directory[i].width;
317 if ((ssize_t) image->columns > icon_info.width)
318 image->columns=(size_t) icon_info.width;
cristy4e09e032011-10-05 19:42:10 +0000319 if (image->columns == 0)
320 image->columns=256;
cristyf054ee72011-10-05 17:04:08 +0000321 image->rows=(size_t) icon_file.directory[i].height;
322 if ((ssize_t) image->rows > icon_info.height)
323 image->rows=(size_t) icon_info.height;
cristy4e09e032011-10-05 19:42:10 +0000324 if (image->rows == 0)
325 image->rows=256;
cristyf054ee72011-10-05 17:04:08 +0000326 image->depth=icon_info.bits_per_pixel;
327 if (image->debug != MagickFalse)
328 {
329 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
330 " scene = %.20g",(double) i);
331 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
332 " size = %.20g",(double) icon_info.size);
333 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
334 " width = %.20g",(double) icon_file.directory[i].width);
335 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
336 " height = %.20g",(double) icon_file.directory[i].height);
337 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
338 " colors = %.20g",(double ) icon_info.number_colors);
339 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
340 " planes = %.20g",(double) icon_info.planes);
341 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
342 " bpp = %.20g",(double) icon_info.bits_per_pixel);
343 }
344 if ((icon_info.number_colors != 0) || (icon_info.bits_per_pixel <= 16))
345 {
346 image->storage_class=PseudoClass;
347 image->colors=icon_info.number_colors;
348 if (image->colors == 0)
349 image->colors=one << icon_info.bits_per_pixel;
350 }
351 if (image->storage_class == PseudoClass)
352 {
353 register ssize_t
354 i;
cristy3ed852e2009-09-05 21:47:34 +0000355
cristyf054ee72011-10-05 17:04:08 +0000356 size_t
357 number_colors,
358 one;
359
360 unsigned char
361 *icon_colormap;
362
363 /*
364 Read Icon raster colormap.
365 */
366 one=1;
367 number_colors=one << icon_info.bits_per_pixel;
368 if (AcquireImageColormap(image,number_colors,exception) == MagickFalse)
369 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
370 icon_colormap=(unsigned char *) AcquireQuantumMemory((size_t)
371 image->colors,4UL*sizeof(*icon_colormap));
372 if (icon_colormap == (unsigned char *) NULL)
373 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
374 count=ReadBlob(image,(size_t) (4*image->colors),icon_colormap);
375 if (count != (ssize_t) (4*image->colors))
376 ThrowReaderException(CorruptImageError,
377 "InsufficientImageDataInFile");
378 p=icon_colormap;
379 for (i=0; i < (ssize_t) image->colors; i++)
380 {
381 image->colormap[i].blue=(Quantum) ScaleCharToQuantum(*p++);
382 image->colormap[i].green=(Quantum) ScaleCharToQuantum(*p++);
383 image->colormap[i].red=(Quantum) ScaleCharToQuantum(*p++);
384 p++;
385 }
386 icon_colormap=(unsigned char *) RelinquishMagickMemory(icon_colormap);
387 }
cristy3ed852e2009-09-05 21:47:34 +0000388 /*
389 Convert Icon raster image to pixel packets.
390 */
cristyf054ee72011-10-05 17:04:08 +0000391 if ((image_info->ping != MagickFalse) &&
392 (image_info->number_scenes != 0))
393 if (image->scene >= (image_info->scene+image_info->number_scenes-1))
394 break;
cristy3ed852e2009-09-05 21:47:34 +0000395 bytes_per_line=(((image->columns*icon_info.bits_per_pixel)+31) &
396 ~31) >> 3;
cristyda16f162011-02-19 23:52:17 +0000397 (void) bytes_per_line;
cristy3ed852e2009-09-05 21:47:34 +0000398 scanline_pad=((((image->columns*icon_info.bits_per_pixel)+31) & ~31)-
399 (image->columns*icon_info.bits_per_pixel)) >> 3;
400 switch (icon_info.bits_per_pixel)
401 {
402 case 1:
403 {
404 /*
405 Convert bitmap scanline.
406 */
cristybb503372010-05-27 20:51:26 +0000407 for (y=(ssize_t) image->rows-1; y >= 0; y--)
cristy3ed852e2009-09-05 21:47:34 +0000408 {
409 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
cristyacd2ed22011-08-30 01:44:23 +0000410 if (q == (Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +0000411 break;
cristybb503372010-05-27 20:51:26 +0000412 for (x=0; x < (ssize_t) (image->columns-7); x+=8)
cristy3ed852e2009-09-05 21:47:34 +0000413 {
cristyf054ee72011-10-05 17:04:08 +0000414 byte=(size_t) ReadBlobByte(image);
cristy3ed852e2009-09-05 21:47:34 +0000415 for (bit=0; bit < 8; bit++)
cristy4c08aed2011-07-01 19:47:50 +0000416 {
417 SetPixelIndex(image,((byte & (0x80 >> bit)) != 0 ? 0x01 :
418 0x00),q);
cristyed231572011-07-14 02:18:59 +0000419 q+=GetPixelChannels(image);
cristy4c08aed2011-07-01 19:47:50 +0000420 }
cristy3ed852e2009-09-05 21:47:34 +0000421 }
422 if ((image->columns % 8) != 0)
423 {
cristyf054ee72011-10-05 17:04:08 +0000424 byte=(size_t) ReadBlobByte(image);
cristy3ed852e2009-09-05 21:47:34 +0000425 for (bit=0; bit < (image->columns % 8); bit++)
cristy4c08aed2011-07-01 19:47:50 +0000426 {
427 SetPixelIndex(image,((byte & (0x80 >> bit)) != 0 ? 0x01 :
428 0x00),q);
cristyed231572011-07-14 02:18:59 +0000429 q+=GetPixelChannels(image);
cristy4c08aed2011-07-01 19:47:50 +0000430 }
cristy3ed852e2009-09-05 21:47:34 +0000431 }
cristybb503372010-05-27 20:51:26 +0000432 for (x=0; x < (ssize_t) scanline_pad; x++)
cristyf054ee72011-10-05 17:04:08 +0000433 (void) ReadBlobByte(image);
cristy3ed852e2009-09-05 21:47:34 +0000434 if (SyncAuthenticPixels(image,exception) == MagickFalse)
435 break;
436 if (image->previous == (Image *) NULL)
437 {
438 status=SetImageProgress(image,LoadImageTag,image->rows-y-1,
439 image->rows);
440 if (status == MagickFalse)
441 break;
442 }
443 }
444 break;
445 }
446 case 4:
447 {
448 /*
449 Read 4-bit Icon scanline.
450 */
cristybb503372010-05-27 20:51:26 +0000451 for (y=(ssize_t) image->rows-1; y >= 0; y--)
cristy3ed852e2009-09-05 21:47:34 +0000452 {
453 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
cristyacd2ed22011-08-30 01:44:23 +0000454 if (q == (Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +0000455 break;
cristybb503372010-05-27 20:51:26 +0000456 for (x=0; x < ((ssize_t) image->columns-1); x+=2)
cristy3ed852e2009-09-05 21:47:34 +0000457 {
cristyf054ee72011-10-05 17:04:08 +0000458 byte=(size_t) ReadBlobByte(image);
cristy4c08aed2011-07-01 19:47:50 +0000459 SetPixelIndex(image,((byte >> 4) & 0xf),q);
cristyed231572011-07-14 02:18:59 +0000460 q+=GetPixelChannels(image);
cristy4c08aed2011-07-01 19:47:50 +0000461 SetPixelIndex(image,((byte) & 0xf),q);
cristyed231572011-07-14 02:18:59 +0000462 q+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +0000463 }
464 if ((image->columns % 2) != 0)
465 {
cristyf054ee72011-10-05 17:04:08 +0000466 byte=(size_t) ReadBlobByte(image);
cristy4c08aed2011-07-01 19:47:50 +0000467 SetPixelIndex(image,((byte >> 4) & 0xf),q);
cristyed231572011-07-14 02:18:59 +0000468 q+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +0000469 }
cristybb503372010-05-27 20:51:26 +0000470 for (x=0; x < (ssize_t) scanline_pad; x++)
cristyf054ee72011-10-05 17:04:08 +0000471 (void) ReadBlobByte(image);
cristy3ed852e2009-09-05 21:47:34 +0000472 if (SyncAuthenticPixels(image,exception) == MagickFalse)
473 break;
474 if (image->previous == (Image *) NULL)
475 {
476 status=SetImageProgress(image,LoadImageTag,image->rows-y-1,
477 image->rows);
478 if (status == MagickFalse)
479 break;
480 }
481 }
482 break;
483 }
484 case 8:
485 {
486 /*
487 Convert PseudoColor scanline.
488 */
cristybb503372010-05-27 20:51:26 +0000489 for (y=(ssize_t) image->rows-1; y >= 0; y--)
cristy3ed852e2009-09-05 21:47:34 +0000490 {
491 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
cristyacd2ed22011-08-30 01:44:23 +0000492 if (q == (Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +0000493 break;
cristybb503372010-05-27 20:51:26 +0000494 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +0000495 {
cristyf054ee72011-10-05 17:04:08 +0000496 byte=(size_t) ReadBlobByte(image);
cristy4c08aed2011-07-01 19:47:50 +0000497 SetPixelIndex(image,byte,q);
cristyed231572011-07-14 02:18:59 +0000498 q+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +0000499 }
cristybb503372010-05-27 20:51:26 +0000500 for (x=0; x < (ssize_t) scanline_pad; x++)
cristyf054ee72011-10-05 17:04:08 +0000501 (void) ReadBlobByte(image);
cristy3ed852e2009-09-05 21:47:34 +0000502 if (SyncAuthenticPixels(image,exception) == MagickFalse)
503 break;
504 if (image->previous == (Image *) NULL)
505 {
506 status=SetImageProgress(image,LoadImageTag,image->rows-y-1,
507 image->rows);
508 if (status == MagickFalse)
509 break;
510 }
511 }
512 break;
513 }
514 case 16:
515 {
516 /*
517 Convert PseudoColor scanline.
518 */
cristybb503372010-05-27 20:51:26 +0000519 for (y=(ssize_t) image->rows-1; y >= 0; y--)
cristy3ed852e2009-09-05 21:47:34 +0000520 {
521 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
cristyacd2ed22011-08-30 01:44:23 +0000522 if (q == (Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +0000523 break;
cristybb503372010-05-27 20:51:26 +0000524 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +0000525 {
cristyf054ee72011-10-05 17:04:08 +0000526 byte=(size_t) ReadBlobByte(image);
527 byte|=(size_t) (ReadBlobByte(image) << 8);
cristy4c08aed2011-07-01 19:47:50 +0000528 SetPixelIndex(image,byte,q);
cristyed231572011-07-14 02:18:59 +0000529 q+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +0000530 }
cristybb503372010-05-27 20:51:26 +0000531 for (x=0; x < (ssize_t) scanline_pad; x++)
cristyf054ee72011-10-05 17:04:08 +0000532 (void) ReadBlobByte(image);
cristy3ed852e2009-09-05 21:47:34 +0000533 if (SyncAuthenticPixels(image,exception) == MagickFalse)
534 break;
535 if (image->previous == (Image *) NULL)
536 {
537 status=SetImageProgress(image,LoadImageTag,image->rows-y-1,
538 image->rows);
539 if (status == MagickFalse)
540 break;
541 }
542 }
543 break;
544 }
545 case 24:
546 case 32:
547 {
548 /*
549 Convert DirectColor scanline.
550 */
cristybb503372010-05-27 20:51:26 +0000551 for (y=(ssize_t) image->rows-1; y >= 0; y--)
cristy3ed852e2009-09-05 21:47:34 +0000552 {
553 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
cristyacd2ed22011-08-30 01:44:23 +0000554 if (q == (Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +0000555 break;
cristybb503372010-05-27 20:51:26 +0000556 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +0000557 {
cristyf054ee72011-10-05 17:04:08 +0000558 SetPixelBlue(image,ScaleCharToQuantum((unsigned char)
559 ReadBlobByte(image)),q);
560 SetPixelGreen(image,ScaleCharToQuantum((unsigned char)
561 ReadBlobByte(image)),q);
562 SetPixelRed(image,ScaleCharToQuantum((unsigned char)
563 ReadBlobByte(image)),q);
cristy3ed852e2009-09-05 21:47:34 +0000564 if (icon_info.bits_per_pixel == 32)
cristyf054ee72011-10-05 17:04:08 +0000565 SetPixelAlpha(image,ScaleCharToQuantum((unsigned char)
566 ReadBlobByte(image)),q);
cristyed231572011-07-14 02:18:59 +0000567 q+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +0000568 }
569 if (icon_info.bits_per_pixel == 24)
cristybb503372010-05-27 20:51:26 +0000570 for (x=0; x < (ssize_t) scanline_pad; x++)
cristyf054ee72011-10-05 17:04:08 +0000571 (void) ReadBlobByte(image);
cristy3ed852e2009-09-05 21:47:34 +0000572 if (SyncAuthenticPixels(image,exception) == MagickFalse)
573 break;
574 if (image->previous == (Image *) NULL)
575 {
576 status=SetImageProgress(image,LoadImageTag,image->rows-y-1,
577 image->rows);
578 if (status == MagickFalse)
579 break;
580 }
581 }
582 break;
583 }
584 default:
585 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
586 }
cristy5dd71882010-08-01 20:53:13 +0000587 if (image_info->ping == MagickFalse)
588 (void) SyncImage(image);
cristy3ed852e2009-09-05 21:47:34 +0000589 if (icon_info.bits_per_pixel != 32)
590 {
591 /*
592 Read the ICON alpha mask.
593 */
594 image->storage_class=DirectClass;
cristybb503372010-05-27 20:51:26 +0000595 for (y=(ssize_t) image->rows-1; y >= 0; y--)
cristy3ed852e2009-09-05 21:47:34 +0000596 {
597 q=GetAuthenticPixels(image,0,y,image->columns,1,exception);
cristyacd2ed22011-08-30 01:44:23 +0000598 if (q == (Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +0000599 break;
cristybb503372010-05-27 20:51:26 +0000600 for (x=0; x < ((ssize_t) image->columns-7); x+=8)
cristy3ed852e2009-09-05 21:47:34 +0000601 {
cristyf054ee72011-10-05 17:04:08 +0000602 byte=(size_t) ReadBlobByte(image);
cristy3ed852e2009-09-05 21:47:34 +0000603 for (bit=0; bit < 8; bit++)
cristy4c08aed2011-07-01 19:47:50 +0000604 {
605 SetPixelAlpha(image,(((byte & (0x80 >> bit)) != 0) ?
606 TransparentAlpha : OpaqueAlpha),q);
cristyed231572011-07-14 02:18:59 +0000607 q+=GetPixelChannels(image);
cristy4c08aed2011-07-01 19:47:50 +0000608 }
cristy3ed852e2009-09-05 21:47:34 +0000609 }
610 if ((image->columns % 8) != 0)
611 {
cristyf054ee72011-10-05 17:04:08 +0000612 byte=(size_t) ReadBlobByte(image);
cristy3ed852e2009-09-05 21:47:34 +0000613 for (bit=0; bit < (image->columns % 8); bit++)
cristy4c08aed2011-07-01 19:47:50 +0000614 {
615 SetPixelAlpha(image,(((byte & (0x80 >> bit)) != 0) ?
616 TransparentAlpha : OpaqueAlpha),q);
cristyed231572011-07-14 02:18:59 +0000617 q+=GetPixelChannels(image);
cristy4c08aed2011-07-01 19:47:50 +0000618 }
cristy3ed852e2009-09-05 21:47:34 +0000619 }
cristy0e8200f2009-09-13 21:10:06 +0000620 if ((image->columns % 32) != 0)
cristybb503372010-05-27 20:51:26 +0000621 for (x=0; x < (ssize_t) ((32-(image->columns % 32))/8); x++)
cristyf054ee72011-10-05 17:04:08 +0000622 (void) ReadBlobByte(image);
cristy3ed852e2009-09-05 21:47:34 +0000623 if (SyncAuthenticPixels(image,exception) == MagickFalse)
624 break;
625 }
626 }
627 if (EOFBlob(image) != MagickFalse)
628 {
629 ThrowFileException(exception,CorruptImageError,
630 "UnexpectedEndOfFile",image->filename);
631 break;
632 }
633 }
634 /*
635 Proceed to next image.
636 */
637 if (image_info->number_scenes != 0)
638 if (image->scene >= (image_info->scene+image_info->number_scenes-1))
639 break;
cristybb503372010-05-27 20:51:26 +0000640 if (i < (ssize_t) (icon_file.count-1))
cristy3ed852e2009-09-05 21:47:34 +0000641 {
642 /*
643 Allocate next image structure.
644 */
cristy1f1de182011-10-05 18:41:34 +0000645 AcquireNextImage(image_info,image,exception);
cristy3ed852e2009-09-05 21:47:34 +0000646 if (GetNextImageInList(image) == (Image *) NULL)
647 {
648 image=DestroyImageList(image);
649 return((Image *) NULL);
650 }
651 image=SyncNextImageInList(image);
652 status=SetImageProgress(image,LoadImagesTag,TellBlob(image),
653 GetBlobSize(image));
654 if (status == MagickFalse)
655 break;
656 }
657 }
658 (void) CloseBlob(image);
659 return(GetFirstImageInList(image));
660}
661
662/*
663%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
664% %
665% %
666% %
667% R e g i s t e r I C O N I m a g e %
668% %
669% %
670% %
671%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
672%
673% RegisterICONImage() adds attributes for the Icon image format to
674% the list of supported formats. The attributes include the image format
675% tag, a method to read and/or write the format, whether the format
676% supports the saving of more than one frame to the same file or blob,
677% whether the format supports native in-memory I/O, and a brief
678% description of the format.
679%
680% The format of the RegisterICONImage method is:
681%
cristybb503372010-05-27 20:51:26 +0000682% size_t RegisterICONImage(void)
cristy3ed852e2009-09-05 21:47:34 +0000683%
684*/
cristybb503372010-05-27 20:51:26 +0000685ModuleExport size_t RegisterICONImage(void)
cristy3ed852e2009-09-05 21:47:34 +0000686{
687 MagickInfo
688 *entry;
689
690 entry=SetMagickInfo("CUR");
691 entry->decoder=(DecodeImageHandler *) ReadICONImage;
692 entry->encoder=(EncodeImageHandler *) WriteICONImage;
693 entry->adjoin=MagickFalse;
694 entry->seekable_stream=MagickTrue;
695 entry->description=ConstantString("Microsoft icon");
696 entry->module=ConstantString("CUR");
697 (void) RegisterMagickInfo(entry);
698 entry=SetMagickInfo("ICO");
699 entry->decoder=(DecodeImageHandler *) ReadICONImage;
700 entry->encoder=(EncodeImageHandler *) WriteICONImage;
701 entry->adjoin=MagickTrue;
702 entry->seekable_stream=MagickTrue;
703 entry->description=ConstantString("Microsoft icon");
704 entry->module=ConstantString("ICON");
705 (void) RegisterMagickInfo(entry);
706 entry=SetMagickInfo("ICON");
707 entry->decoder=(DecodeImageHandler *) ReadICONImage;
708 entry->encoder=(EncodeImageHandler *) WriteICONImage;
709 entry->adjoin=MagickFalse;
710 entry->seekable_stream=MagickTrue;
711 entry->description=ConstantString("Microsoft icon");
712 entry->module=ConstantString("ICON");
713 (void) RegisterMagickInfo(entry);
714 return(MagickImageCoderSignature);
715}
716
717/*
718%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
719% %
720% %
721% %
722% U n r e g i s t e r I C O N I m a g e %
723% %
724% %
725% %
726%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
727%
728% UnregisterICONImage() removes format registrations made by the
729% ICON module from the list of supported formats.
730%
731% The format of the UnregisterICONImage method is:
732%
733% UnregisterICONImage(void)
734%
735*/
736ModuleExport void UnregisterICONImage(void)
737{
738 (void) UnregisterMagickInfo("CUR");
739 (void) UnregisterMagickInfo("ICO");
740 (void) UnregisterMagickInfo("ICON");
741}
742
743/*
744%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
745% %
746% %
747% %
748% W r i t e I C O N I m a g e %
749% %
750% %
751% %
752%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
753%
754% WriteICONImage() writes an image in Microsoft Windows bitmap encoded
755% image format, version 3 for Windows or (if the image has a matte channel)
756% version 4.
757%
758% The format of the WriteICONImage method is:
759%
760% MagickBooleanType WriteICONImage(const ImageInfo *image_info,
cristy1e178e72011-08-28 19:44:34 +0000761% Image *image,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000762%
763% A description of each parameter follows.
764%
765% o image_info: the image info.
766%
767% o image: The image.
768%
cristy1e178e72011-08-28 19:44:34 +0000769% o exception: return any errors or warnings in this structure.
770%
cristy3ed852e2009-09-05 21:47:34 +0000771*/
772static MagickBooleanType WriteICONImage(const ImageInfo *image_info,
cristy1e178e72011-08-28 19:44:34 +0000773 Image *image,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000774{
775 IconFile
776 icon_file;
777
778 IconInfo
779 icon_info;
780
781 Image
782 *next;
783
cristy3ed852e2009-09-05 21:47:34 +0000784 MagickBooleanType
785 status;
786
787 MagickOffsetType
788 offset,
789 scene;
790
cristy4c08aed2011-07-01 19:47:50 +0000791 register const Quantum
cristy3ed852e2009-09-05 21:47:34 +0000792 *p;
793
cristybb503372010-05-27 20:51:26 +0000794 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000795 i,
796 x;
797
798 register unsigned char
799 *q;
800
cristyebc891a2011-04-24 23:04:16 +0000801 size_t
802 bytes_per_line,
803 scanline_pad;
804
805 ssize_t
806 y;
807
cristy3ed852e2009-09-05 21:47:34 +0000808 unsigned char
809 bit,
810 byte,
811 *pixels;
812
cristy3ed852e2009-09-05 21:47:34 +0000813 /*
814 Open output image file.
815 */
816 assert(image_info != (const ImageInfo *) NULL);
817 assert(image_info->signature == MagickSignature);
818 assert(image != (Image *) NULL);
819 assert(image->signature == MagickSignature);
820 (void) LogMagickEvent(CoderEvent,GetMagickModule(),"%s",image->filename);
cristy3a37efd2011-08-28 20:31:03 +0000821 assert(exception != (ExceptionInfo *) NULL);
822 assert(exception->signature == MagickSignature);
cristy1e178e72011-08-28 19:44:34 +0000823 status=OpenBlob(image_info,image,WriteBinaryBlobMode,exception);
cristy3ed852e2009-09-05 21:47:34 +0000824 if (status == MagickFalse)
825 return(status);
826 scene=0;
827 next=image;
828 do
829 {
830 if ((image->columns > 256L) || (image->rows > 256L))
831 ThrowWriterException(ImageError,"WidthOrHeightExceedsLimit");
832 scene++;
833 next=SyncNextImageInList(next);
834 } while ((next != (Image *) NULL) && (image_info->adjoin != MagickFalse));
835 /*
836 Dump out a ICON header template to be properly initialized later.
837 */
838 (void) WriteBlobLSBShort(image,0);
839 (void) WriteBlobLSBShort(image,1);
840 (void) WriteBlobLSBShort(image,(unsigned char) scene);
841 (void) ResetMagickMemory(&icon_file,0,sizeof(icon_file));
842 (void) ResetMagickMemory(&icon_info,0,sizeof(icon_info));
843 scene=0;
844 next=image;
845 do
846 {
847 (void) WriteBlobByte(image,icon_file.directory[scene].width);
848 (void) WriteBlobByte(image,icon_file.directory[scene].height);
849 (void) WriteBlobByte(image,icon_file.directory[scene].colors);
850 (void) WriteBlobByte(image,icon_file.directory[scene].reserved);
851 (void) WriteBlobLSBShort(image,icon_file.directory[scene].planes);
852 (void) WriteBlobLSBShort(image,icon_file.directory[scene].bits_per_pixel);
cristy0b29b252010-05-30 01:59:46 +0000853 (void) WriteBlobLSBLong(image,(unsigned int)
854 icon_file.directory[scene].size);
855 (void) WriteBlobLSBLong(image,(unsigned int)
856 icon_file.directory[scene].offset);
cristy3ed852e2009-09-05 21:47:34 +0000857 scene++;
858 next=SyncNextImageInList(next);
859 } while ((next != (Image *) NULL) && (image_info->adjoin != MagickFalse));
860 scene=0;
861 next=image;
862 do
863 {
864 if ((next->columns == 256) && (next->rows == 256))
865 {
866 Image
867 *write_image;
868
869 ImageInfo
870 *write_info;
871
872 size_t
873 length;
874
875 unsigned char
876 *png;
877
878 /*
879 Icon image encoded as a compressed PNG image.
880 */
cristy1e178e72011-08-28 19:44:34 +0000881 write_image=CloneImage(next,0,0,MagickTrue,exception);
cristy3ed852e2009-09-05 21:47:34 +0000882 if (write_image == (Image *) NULL)
883 return(MagickFalse);
884 write_info=CloneImageInfo(image_info);
885 (void) CopyMagickString(write_info->filename,"PNG:",MaxTextExtent);
886 png=(unsigned char *) ImageToBlob(write_info,write_image,&length,
cristy1e178e72011-08-28 19:44:34 +0000887 exception);
cristy3ed852e2009-09-05 21:47:34 +0000888 write_image=DestroyImage(write_image);
889 write_info=DestroyImageInfo(write_info);
890 if (png == (unsigned char *) NULL)
891 return(MagickFalse);
892 icon_file.directory[scene].width=0;
893 icon_file.directory[scene].height=0;
894 icon_file.directory[scene].colors=0;
895 icon_file.directory[scene].reserved=0;
896 icon_file.directory[scene].planes=1;
897 icon_file.directory[scene].bits_per_pixel=32;
cristybb503372010-05-27 20:51:26 +0000898 icon_file.directory[scene].size=(size_t) length;
899 icon_file.directory[scene].offset=(size_t) TellBlob(image);
cristy3ed852e2009-09-05 21:47:34 +0000900 (void) WriteBlob(image,(size_t) length,png);
901 png=(unsigned char *) RelinquishMagickMemory(png);
902 }
903 else
904 {
905 /*
906 Initialize ICON raster file header.
907 */
908 if (next->colorspace != RGBColorspace)
909 (void) TransformImageColorspace(next,RGBColorspace);
910 icon_info.file_size=14+12+28;
911 icon_info.offset_bits=icon_info.file_size;
912 icon_info.compression=BI_RGB;
913 if ((next->storage_class != DirectClass) && (next->colors > 256))
cristy1e178e72011-08-28 19:44:34 +0000914 (void) SetImageStorageClass(next,DirectClass,exception);
cristy3ed852e2009-09-05 21:47:34 +0000915 if (next->storage_class == DirectClass)
916 {
917 /*
918 Full color ICON raster.
919 */
920 icon_info.number_colors=0;
921 icon_info.bits_per_pixel=32;
cristybb503372010-05-27 20:51:26 +0000922 icon_info.compression=(size_t) BI_RGB;
cristy3ed852e2009-09-05 21:47:34 +0000923 }
924 else
925 {
cristy35ef8242010-06-03 16:24:13 +0000926 size_t
927 one;
928
cristy3ed852e2009-09-05 21:47:34 +0000929 /*
930 Colormapped ICON raster.
931 */
932 icon_info.bits_per_pixel=8;
933 if (next->colors <= 256)
934 icon_info.bits_per_pixel=8;
935 if (next->colors <= 16)
936 icon_info.bits_per_pixel=4;
937 if (next->colors <= 2)
938 icon_info.bits_per_pixel=1;
cristy35ef8242010-06-03 16:24:13 +0000939 one=1;
940 icon_info.number_colors=one << icon_info.bits_per_pixel;
cristy3ed852e2009-09-05 21:47:34 +0000941 if (icon_info.number_colors < next->colors)
942 {
cristy1e178e72011-08-28 19:44:34 +0000943 (void) SetImageStorageClass(next,DirectClass,exception);
cristy3ed852e2009-09-05 21:47:34 +0000944 icon_info.number_colors=0;
945 icon_info.bits_per_pixel=(unsigned short) 24;
cristybb503372010-05-27 20:51:26 +0000946 icon_info.compression=(size_t) BI_RGB;
cristy3ed852e2009-09-05 21:47:34 +0000947 }
948 else
949 {
cristy0b29b252010-05-30 01:59:46 +0000950 size_t
951 one;
952
953 one=1;
954 icon_info.file_size+=3*(one << icon_info.bits_per_pixel);
955 icon_info.offset_bits+=3*(one << icon_info.bits_per_pixel);
956 icon_info.file_size+=(one << icon_info.bits_per_pixel);
957 icon_info.offset_bits+=(one << icon_info.bits_per_pixel);
cristy3ed852e2009-09-05 21:47:34 +0000958 }
959 }
960 bytes_per_line=(((next->columns*icon_info.bits_per_pixel)+31) &
961 ~31) >> 3;
962 icon_info.ba_offset=0;
cristybb503372010-05-27 20:51:26 +0000963 icon_info.width=(ssize_t) next->columns;
964 icon_info.height=(ssize_t) next->rows;
cristy3ed852e2009-09-05 21:47:34 +0000965 icon_info.planes=1;
966 icon_info.image_size=bytes_per_line*next->rows;
967 icon_info.size=40;
968 icon_info.size+=(4*icon_info.number_colors);
969 icon_info.size+=icon_info.image_size;
970 icon_info.size+=(((icon_info.width+31) & ~31) >> 3)*icon_info.height;
971 icon_info.file_size+=icon_info.image_size;
972 icon_info.x_pixels=0;
973 icon_info.y_pixels=0;
974 switch (next->units)
975 {
976 case UndefinedResolution:
977 case PixelsPerInchResolution:
978 {
cristybb503372010-05-27 20:51:26 +0000979 icon_info.x_pixels=(size_t) (100.0*next->x_resolution/2.54);
980 icon_info.y_pixels=(size_t) (100.0*next->y_resolution/2.54);
cristy3ed852e2009-09-05 21:47:34 +0000981 break;
982 }
983 case PixelsPerCentimeterResolution:
984 {
cristybb503372010-05-27 20:51:26 +0000985 icon_info.x_pixels=(size_t) (100.0*next->x_resolution);
986 icon_info.y_pixels=(size_t) (100.0*next->y_resolution);
cristy3ed852e2009-09-05 21:47:34 +0000987 break;
988 }
989 }
990 icon_info.colors_important=icon_info.number_colors;
991 /*
992 Convert MIFF to ICON raster pixels.
993 */
994 pixels=(unsigned char *) AcquireQuantumMemory((size_t)
995 icon_info.image_size,sizeof(*pixels));
996 if (pixels == (unsigned char *) NULL)
997 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
998 (void) ResetMagickMemory(pixels,0,(size_t) icon_info.image_size);
999 switch (icon_info.bits_per_pixel)
1000 {
1001 case 1:
1002 {
cristybb503372010-05-27 20:51:26 +00001003 size_t
cristy3ed852e2009-09-05 21:47:34 +00001004 bit,
1005 byte;
1006
1007 /*
1008 Convert PseudoClass image to a ICON monochrome image.
1009 */
cristybb503372010-05-27 20:51:26 +00001010 for (y=0; y < (ssize_t) next->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00001011 {
cristy1e178e72011-08-28 19:44:34 +00001012 p=GetVirtualPixels(next,0,y,next->columns,1,exception);
cristy4c08aed2011-07-01 19:47:50 +00001013 if (p == (const Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00001014 break;
cristy3ed852e2009-09-05 21:47:34 +00001015 q=pixels+(next->rows-y-1)*bytes_per_line;
1016 bit=0;
1017 byte=0;
cristybb503372010-05-27 20:51:26 +00001018 for (x=0; x < (ssize_t) next->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00001019 {
1020 byte<<=1;
cristy4c08aed2011-07-01 19:47:50 +00001021 byte|=GetPixelIndex(next,p) != 0 ? 0x01 : 0x00;
cristy3ed852e2009-09-05 21:47:34 +00001022 bit++;
1023 if (bit == 8)
1024 {
1025 *q++=(unsigned char) byte;
1026 bit=0;
1027 byte=0;
1028 }
cristyed231572011-07-14 02:18:59 +00001029 p+=GetPixelChannels(image);
cristy4c08aed2011-07-01 19:47:50 +00001030 }
cristy3ed852e2009-09-05 21:47:34 +00001031 if (bit != 0)
1032 *q++=(unsigned char) (byte << (8-bit));
1033 if (next->previous == (Image *) NULL)
1034 {
1035 status=SetImageProgress(next,SaveImageTag,y,next->rows);
1036 if (status == MagickFalse)
1037 break;
1038 }
1039 }
1040 break;
1041 }
1042 case 4:
1043 {
cristybb503372010-05-27 20:51:26 +00001044 size_t
cristy3ed852e2009-09-05 21:47:34 +00001045 nibble,
1046 byte;
1047
1048 /*
1049 Convert PseudoClass image to a ICON monochrome image.
1050 */
cristybb503372010-05-27 20:51:26 +00001051 for (y=0; y < (ssize_t) next->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00001052 {
cristy1e178e72011-08-28 19:44:34 +00001053 p=GetVirtualPixels(next,0,y,next->columns,1,exception);
cristy4c08aed2011-07-01 19:47:50 +00001054 if (p == (const Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00001055 break;
cristy3ed852e2009-09-05 21:47:34 +00001056 q=pixels+(next->rows-y-1)*bytes_per_line;
1057 nibble=0;
1058 byte=0;
cristybb503372010-05-27 20:51:26 +00001059 for (x=0; x < (ssize_t) next->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00001060 {
1061 byte<<=4;
cristy4c08aed2011-07-01 19:47:50 +00001062 byte|=((size_t) GetPixelIndex(next,p) & 0x0f);
cristy3ed852e2009-09-05 21:47:34 +00001063 nibble++;
1064 if (nibble == 2)
1065 {
1066 *q++=(unsigned char) byte;
1067 nibble=0;
1068 byte=0;
1069 }
cristyed231572011-07-14 02:18:59 +00001070 p+=GetPixelChannels(image);
cristy4c08aed2011-07-01 19:47:50 +00001071 }
cristy3ed852e2009-09-05 21:47:34 +00001072 if (nibble != 0)
1073 *q++=(unsigned char) (byte << 4);
1074 if (next->previous == (Image *) NULL)
1075 {
1076 status=SetImageProgress(next,SaveImageTag,y,next->rows);
1077 if (status == MagickFalse)
1078 break;
1079 }
1080 }
1081 break;
1082 }
1083 case 8:
1084 {
1085 /*
1086 Convert PseudoClass packet to ICON pixel.
1087 */
cristybb503372010-05-27 20:51:26 +00001088 for (y=0; y < (ssize_t) next->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00001089 {
cristy1e178e72011-08-28 19:44:34 +00001090 p=GetVirtualPixels(next,0,y,next->columns,1,exception);
cristy4c08aed2011-07-01 19:47:50 +00001091 if (p == (const Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00001092 break;
cristy3ed852e2009-09-05 21:47:34 +00001093 q=pixels+(next->rows-y-1)*bytes_per_line;
cristybb503372010-05-27 20:51:26 +00001094 for (x=0; x < (ssize_t) next->columns; x++)
cristy4c08aed2011-07-01 19:47:50 +00001095 {
1096 *q++=(unsigned char) GetPixelIndex(next,p);
cristyed231572011-07-14 02:18:59 +00001097 p+=GetPixelChannels(image);
cristy4c08aed2011-07-01 19:47:50 +00001098 }
cristy3ed852e2009-09-05 21:47:34 +00001099 if (next->previous == (Image *) NULL)
1100 {
1101 status=SetImageProgress(next,SaveImageTag,y,next->rows);
1102 if (status == MagickFalse)
1103 break;
1104 }
1105 }
1106 break;
1107 }
1108 case 24:
1109 case 32:
1110 {
1111 /*
1112 Convert DirectClass packet to ICON BGR888 or BGRA8888 pixel.
1113 */
cristybb503372010-05-27 20:51:26 +00001114 for (y=0; y < (ssize_t) next->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00001115 {
cristy1e178e72011-08-28 19:44:34 +00001116 p=GetVirtualPixels(next,0,y,next->columns,1,exception);
cristy4c08aed2011-07-01 19:47:50 +00001117 if (p == (const Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00001118 break;
1119 q=pixels+(next->rows-y-1)*bytes_per_line;
cristybb503372010-05-27 20:51:26 +00001120 for (x=0; x < (ssize_t) next->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00001121 {
cristy4c08aed2011-07-01 19:47:50 +00001122 *q++=ScaleQuantumToChar(GetPixelBlue(next,p));
1123 *q++=ScaleQuantumToChar(GetPixelGreen(next,p));
1124 *q++=ScaleQuantumToChar(GetPixelRed(next,p));
cristy3ed852e2009-09-05 21:47:34 +00001125 if (next->matte == MagickFalse)
1126 *q++=ScaleQuantumToChar(QuantumRange);
1127 else
cristy4c08aed2011-07-01 19:47:50 +00001128 *q++=ScaleQuantumToChar(GetPixelAlpha(next,p));
cristyed231572011-07-14 02:18:59 +00001129 p+=GetPixelChannels(next);
cristy3ed852e2009-09-05 21:47:34 +00001130 }
1131 if (icon_info.bits_per_pixel == 24)
cristybb503372010-05-27 20:51:26 +00001132 for (x=3L*(ssize_t) next->columns; x < (ssize_t) bytes_per_line; x++)
cristy3ed852e2009-09-05 21:47:34 +00001133 *q++=0x00;
1134 if (next->previous == (Image *) NULL)
1135 {
1136 status=SetImageProgress(next,SaveImageTag,y,next->rows);
1137 if (status == MagickFalse)
1138 break;
1139 }
1140 }
1141 break;
1142 }
1143 }
1144 /*
1145 Write 40-byte version 3+ bitmap header.
1146 */
1147 icon_file.directory[scene].width=(unsigned char) icon_info.width;
1148 icon_file.directory[scene].height=(unsigned char) icon_info.height;
1149 icon_file.directory[scene].colors=(unsigned char)
1150 icon_info.number_colors;
1151 icon_file.directory[scene].reserved=0;
1152 icon_file.directory[scene].planes=icon_info.planes;
1153 icon_file.directory[scene].bits_per_pixel=icon_info.bits_per_pixel;
1154 icon_file.directory[scene].size=icon_info.size;
cristybb503372010-05-27 20:51:26 +00001155 icon_file.directory[scene].offset=(size_t) TellBlob(image);
cristy35ef8242010-06-03 16:24:13 +00001156 (void) WriteBlobLSBLong(image,(unsigned int) 40);
1157 (void) WriteBlobLSBLong(image,(unsigned int) icon_info.width);
1158 (void) WriteBlobLSBLong(image,(unsigned int) icon_info.height*2);
cristy3ed852e2009-09-05 21:47:34 +00001159 (void) WriteBlobLSBShort(image,icon_info.planes);
1160 (void) WriteBlobLSBShort(image,icon_info.bits_per_pixel);
cristy35ef8242010-06-03 16:24:13 +00001161 (void) WriteBlobLSBLong(image,(unsigned int) icon_info.compression);
1162 (void) WriteBlobLSBLong(image,(unsigned int) icon_info.image_size);
1163 (void) WriteBlobLSBLong(image,(unsigned int) icon_info.x_pixels);
1164 (void) WriteBlobLSBLong(image,(unsigned int) icon_info.y_pixels);
1165 (void) WriteBlobLSBLong(image,(unsigned int) icon_info.number_colors);
1166 (void) WriteBlobLSBLong(image,(unsigned int) icon_info.colors_important);
cristy3ed852e2009-09-05 21:47:34 +00001167 if (next->storage_class == PseudoClass)
1168 {
1169 unsigned char
1170 *icon_colormap;
1171
1172 /*
1173 Dump colormap to file.
1174 */
1175 icon_colormap=(unsigned char *) AcquireQuantumMemory((size_t)
1176 (1UL << icon_info.bits_per_pixel),4UL*sizeof(*icon_colormap));
1177 if (icon_colormap == (unsigned char *) NULL)
1178 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
1179 q=icon_colormap;
cristybb503372010-05-27 20:51:26 +00001180 for (i=0; i < (ssize_t) next->colors; i++)
cristy3ed852e2009-09-05 21:47:34 +00001181 {
1182 *q++=ScaleQuantumToChar(next->colormap[i].blue);
1183 *q++=ScaleQuantumToChar(next->colormap[i].green);
1184 *q++=ScaleQuantumToChar(next->colormap[i].red);
1185 *q++=(unsigned char) 0x0;
1186 }
cristybb503372010-05-27 20:51:26 +00001187 for ( ; i < (ssize_t) (1UL << icon_info.bits_per_pixel); i++)
cristy3ed852e2009-09-05 21:47:34 +00001188 {
1189 *q++=(unsigned char) 0x00;
1190 *q++=(unsigned char) 0x00;
1191 *q++=(unsigned char) 0x00;
1192 *q++=(unsigned char) 0x00;
1193 }
1194 (void) WriteBlob(image,(size_t) (4UL*(1UL <<
1195 icon_info.bits_per_pixel)),icon_colormap);
1196 icon_colormap=(unsigned char *) RelinquishMagickMemory(
1197 icon_colormap);
1198 }
1199 (void) WriteBlob(image,(size_t) icon_info.image_size,pixels);
1200 pixels=(unsigned char *) RelinquishMagickMemory(pixels);
1201 /*
1202 Write matte mask.
1203 */
1204 scanline_pad=(((next->columns+31) & ~31)-next->columns) >> 3;
cristybb503372010-05-27 20:51:26 +00001205 for (y=((ssize_t) next->rows - 1); y >= 0; y--)
cristy3ed852e2009-09-05 21:47:34 +00001206 {
cristy1e178e72011-08-28 19:44:34 +00001207 p=GetVirtualPixels(next,0,y,next->columns,1,exception);
cristy4c08aed2011-07-01 19:47:50 +00001208 if (p == (const Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00001209 break;
1210 bit=0;
1211 byte=0;
cristybb503372010-05-27 20:51:26 +00001212 for (x=0; x < (ssize_t) next->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00001213 {
1214 byte<<=1;
1215 if ((next->matte == MagickTrue) &&
cristy4c08aed2011-07-01 19:47:50 +00001216 (GetPixelAlpha(next,p) == (Quantum) TransparentAlpha))
cristy3ed852e2009-09-05 21:47:34 +00001217 byte|=0x01;
1218 bit++;
1219 if (bit == 8)
1220 {
1221 (void) WriteBlobByte(image,(unsigned char) byte);
1222 bit=0;
1223 byte=0;
1224 }
cristyed231572011-07-14 02:18:59 +00001225 p+=GetPixelChannels(next);
cristy3ed852e2009-09-05 21:47:34 +00001226 }
1227 if (bit != 0)
1228 (void) WriteBlobByte(image,(unsigned char) (byte << (8-bit)));
cristybb503372010-05-27 20:51:26 +00001229 for (i=0; i < (ssize_t) scanline_pad; i++)
cristy3ed852e2009-09-05 21:47:34 +00001230 (void) WriteBlobByte(image,(unsigned char) 0);
1231 }
1232 }
1233 if (GetNextImageInList(next) == (Image *) NULL)
1234 break;
1235 next=SyncNextImageInList(next);
1236 status=SetImageProgress(next,SaveImagesTag,scene++,
1237 GetImageListLength(next));
1238 if (status == MagickFalse)
1239 break;
1240 } while ((next != (Image *) NULL) && (image_info->adjoin != MagickFalse));
1241 offset=SeekBlob(image,0,SEEK_SET);
cristyda16f162011-02-19 23:52:17 +00001242 (void) offset;
cristy3ed852e2009-09-05 21:47:34 +00001243 (void) WriteBlobLSBShort(image,0);
1244 (void) WriteBlobLSBShort(image,1);
1245 (void) WriteBlobLSBShort(image,(unsigned short) (scene+1));
1246 scene=0;
1247 next=image;
1248 do
1249 {
1250 (void) WriteBlobByte(image,icon_file.directory[scene].width);
1251 (void) WriteBlobByte(image,icon_file.directory[scene].height);
1252 (void) WriteBlobByte(image,icon_file.directory[scene].colors);
1253 (void) WriteBlobByte(image,icon_file.directory[scene].reserved);
1254 (void) WriteBlobLSBShort(image,icon_file.directory[scene].planes);
1255 (void) WriteBlobLSBShort(image,icon_file.directory[scene].bits_per_pixel);
cristy0b29b252010-05-30 01:59:46 +00001256 (void) WriteBlobLSBLong(image,(unsigned int)
1257 icon_file.directory[scene].size);
1258 (void) WriteBlobLSBLong(image,(unsigned int)
1259 icon_file.directory[scene].offset);
cristy3ed852e2009-09-05 21:47:34 +00001260 scene++;
1261 next=SyncNextImageInList(next);
1262 } while ((next != (Image *) NULL) && (image_info->adjoin != MagickFalse));
1263 (void) CloseBlob(image);
1264 return(MagickTrue);
1265}