blob: 92d1ee68ca1ea57452f0b529eed28b23d237294c [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);
217 image=AcquireImage(image_info);
218 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);
cristy3ed852e2009-09-05 21:47:34 +0000256 if ((icon_file.directory[i].width == 0) &&
257 (icon_file.directory[i].height == 0))
258 {
259 Image
260 *icon_image;
261
262 ImageInfo
263 *read_info;
264
265 size_t
266 length;
267
268 unsigned char
269 *png;
270
271 /*
272 Icon image encoded as a compressed PNG image.
273 */
274 length=icon_file.directory[i].size;
275 png=(unsigned char *) AcquireQuantumMemory(length+12,sizeof(*png));
276 if (png == (unsigned char *) NULL)
277 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
278 (void) CopyMagickMemory(png,"\211PNG\r\n\032\n\000\000\000\015",12);
cristyc8817fe2010-08-01 13:43:59 +0000279 count=ReadBlob(image,length-12,png+12);
280 if (count != (ssize_t) (length-12))
cristy3ed852e2009-09-05 21:47:34 +0000281 {
282 png=(unsigned char *) RelinquishMagickMemory(png);
283 ThrowReaderException(CorruptImageError,
284 "InsufficientImageDataInFile");
285 }
286 read_info=CloneImageInfo(image_info);
287 (void) CopyMagickString(read_info->magick,"PNG",MaxTextExtent);
288 icon_image=BlobToImage(read_info,png,length+12,exception);
289 read_info=DestroyImageInfo(read_info);
290 png=(unsigned char *) RelinquishMagickMemory(png);
291 if (icon_image == (Image *) NULL)
292 {
293 image=DestroyImageList(image);
294 return((Image *) NULL);
295 }
296 DestroyBlob(icon_image);
297 icon_image->blob=ReferenceBlob(image->blob);
298 ReplaceImageInList(&image,icon_image);
299 }
300 else
301 {
302 icon_info.planes=ReadBlobLSBShort(image);
303 icon_info.bits_per_pixel=ReadBlobLSBShort(image);
304 if (icon_info.bits_per_pixel > 32)
305 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
306 icon_info.compression=ReadBlobLSBLong(image);
307 icon_info.image_size=ReadBlobLSBLong(image);
308 icon_info.x_pixels=ReadBlobLSBLong(image);
309 icon_info.y_pixels=ReadBlobLSBLong(image);
310 icon_info.number_colors=ReadBlobLSBLong(image);
311 icon_info.colors_important=ReadBlobLSBLong(image);
312 image->matte=MagickTrue;
cristybb503372010-05-27 20:51:26 +0000313 image->columns=(size_t) icon_file.directory[i].width;
314 if ((ssize_t) image->columns > icon_info.width)
315 image->columns=(size_t) icon_info.width;
316 image->rows=(size_t) icon_file.directory[i].height;
317 if ((ssize_t) image->rows > icon_info.height)
318 image->rows=(size_t) icon_info.height;
cristyc8817fe2010-08-01 13:43:59 +0000319 image->depth=icon_info.bits_per_pixel;
cristy3ed852e2009-09-05 21:47:34 +0000320 if (image->debug != MagickFalse)
321 {
322 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
cristye8c25f92010-06-03 00:53:06 +0000323 " scene = %.20g",(double) i);
cristy3ed852e2009-09-05 21:47:34 +0000324 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
cristye8c25f92010-06-03 00:53:06 +0000325 " size = %.20g",(double) icon_info.size);
cristy3ed852e2009-09-05 21:47:34 +0000326 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
cristye8c25f92010-06-03 00:53:06 +0000327 " width = %.20g",(double) icon_file.directory[i].width);
cristy3ed852e2009-09-05 21:47:34 +0000328 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
cristye8c25f92010-06-03 00:53:06 +0000329 " height = %.20g",(double) icon_file.directory[i].height);
cristy3ed852e2009-09-05 21:47:34 +0000330 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
cristye8c25f92010-06-03 00:53:06 +0000331 " colors = %.20g",(double ) icon_info.number_colors);
cristy3ed852e2009-09-05 21:47:34 +0000332 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
cristye8c25f92010-06-03 00:53:06 +0000333 " planes = %.20g",(double) icon_info.planes);
cristy3ed852e2009-09-05 21:47:34 +0000334 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
cristye8c25f92010-06-03 00:53:06 +0000335 " bpp = %.20g",(double) icon_info.bits_per_pixel);
cristy3ed852e2009-09-05 21:47:34 +0000336 }
337 if ((icon_info.number_colors != 0) || (icon_info.bits_per_pixel <= 16))
338 {
339 image->storage_class=PseudoClass;
340 image->colors=icon_info.number_colors;
341 if (image->colors == 0)
cristyeaedf062010-05-29 22:36:02 +0000342 image->colors=one << icon_info.bits_per_pixel;
cristy3ed852e2009-09-05 21:47:34 +0000343 }
344 if (image->storage_class == PseudoClass)
345 {
cristybb503372010-05-27 20:51:26 +0000346 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000347 i;
348
cristybb503372010-05-27 20:51:26 +0000349 size_t
cristy0b29b252010-05-30 01:59:46 +0000350 number_colors,
351 one;
cristy3ed852e2009-09-05 21:47:34 +0000352
cristyebc891a2011-04-24 23:04:16 +0000353 unsigned char
354 *icon_colormap;
355
cristy3ed852e2009-09-05 21:47:34 +0000356 /*
357 Read Icon raster colormap.
358 */
cristy0b29b252010-05-30 01:59:46 +0000359 one=1;
360 number_colors=one << icon_info.bits_per_pixel;
cristy3ed852e2009-09-05 21:47:34 +0000361 if (AcquireImageColormap(image,number_colors) == MagickFalse)
362 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
363 icon_colormap=(unsigned char *) AcquireQuantumMemory((size_t)
364 image->colors,4UL*sizeof(*icon_colormap));
365 if (icon_colormap == (unsigned char *) NULL)
366 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
367 count=ReadBlob(image,(size_t) (4*image->colors),icon_colormap);
368 if (count != (ssize_t) (4*image->colors))
369 ThrowReaderException(CorruptImageError,
370 "InsufficientImageDataInFile");
371 p=icon_colormap;
cristybb503372010-05-27 20:51:26 +0000372 for (i=0; i < (ssize_t) image->colors; i++)
cristy3ed852e2009-09-05 21:47:34 +0000373 {
374 image->colormap[i].blue=(Quantum) ScaleCharToQuantum(*p++);
375 image->colormap[i].green=(Quantum) ScaleCharToQuantum(*p++);
376 image->colormap[i].red=(Quantum) ScaleCharToQuantum(*p++);
377 p++;
378 }
379 icon_colormap=(unsigned char *) RelinquishMagickMemory(icon_colormap);
380 }
381 /*
382 Convert Icon raster image to pixel packets.
383 */
384 if ((image_info->ping != MagickFalse) &&
385 (image_info->number_scenes != 0))
386 if (image->scene >= (image_info->scene+image_info->number_scenes-1))
387 break;
388 bytes_per_line=(((image->columns*icon_info.bits_per_pixel)+31) &
389 ~31) >> 3;
cristyda16f162011-02-19 23:52:17 +0000390 (void) bytes_per_line;
cristy3ed852e2009-09-05 21:47:34 +0000391 scanline_pad=((((image->columns*icon_info.bits_per_pixel)+31) & ~31)-
392 (image->columns*icon_info.bits_per_pixel)) >> 3;
393 switch (icon_info.bits_per_pixel)
394 {
395 case 1:
396 {
397 /*
398 Convert bitmap scanline.
399 */
cristybb503372010-05-27 20:51:26 +0000400 for (y=(ssize_t) image->rows-1; y >= 0; y--)
cristy3ed852e2009-09-05 21:47:34 +0000401 {
402 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
cristyacd2ed22011-08-30 01:44:23 +0000403 if (q == (Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +0000404 break;
cristybb503372010-05-27 20:51:26 +0000405 for (x=0; x < (ssize_t) (image->columns-7); x+=8)
cristy3ed852e2009-09-05 21:47:34 +0000406 {
cristybb503372010-05-27 20:51:26 +0000407 byte=(size_t) ReadBlobByte(image);
cristy3ed852e2009-09-05 21:47:34 +0000408 for (bit=0; bit < 8; bit++)
cristy4c08aed2011-07-01 19:47:50 +0000409 {
410 SetPixelIndex(image,((byte & (0x80 >> bit)) != 0 ? 0x01 :
411 0x00),q);
cristyed231572011-07-14 02:18:59 +0000412 q+=GetPixelChannels(image);
cristy4c08aed2011-07-01 19:47:50 +0000413 }
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++)
cristy4c08aed2011-07-01 19:47:50 +0000419 {
420 SetPixelIndex(image,((byte & (0x80 >> bit)) != 0 ? 0x01 :
421 0x00),q);
cristyed231572011-07-14 02:18:59 +0000422 q+=GetPixelChannels(image);
cristy4c08aed2011-07-01 19:47:50 +0000423 }
cristy3ed852e2009-09-05 21:47:34 +0000424 }
cristybb503372010-05-27 20:51:26 +0000425 for (x=0; x < (ssize_t) scanline_pad; x++)
cristy3ed852e2009-09-05 21:47:34 +0000426 (void) ReadBlobByte(image);
427 if (SyncAuthenticPixels(image,exception) == MagickFalse)
428 break;
429 if (image->previous == (Image *) NULL)
430 {
431 status=SetImageProgress(image,LoadImageTag,image->rows-y-1,
432 image->rows);
433 if (status == MagickFalse)
434 break;
435 }
436 }
437 break;
438 }
439 case 4:
440 {
441 /*
442 Read 4-bit Icon scanline.
443 */
cristybb503372010-05-27 20:51:26 +0000444 for (y=(ssize_t) image->rows-1; y >= 0; y--)
cristy3ed852e2009-09-05 21:47:34 +0000445 {
446 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
cristyacd2ed22011-08-30 01:44:23 +0000447 if (q == (Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +0000448 break;
cristybb503372010-05-27 20:51:26 +0000449 for (x=0; x < ((ssize_t) image->columns-1); x+=2)
cristy3ed852e2009-09-05 21:47:34 +0000450 {
cristybb503372010-05-27 20:51:26 +0000451 byte=(size_t) ReadBlobByte(image);
cristy4c08aed2011-07-01 19:47:50 +0000452 SetPixelIndex(image,((byte >> 4) & 0xf),q);
cristyed231572011-07-14 02:18:59 +0000453 q+=GetPixelChannels(image);
cristy4c08aed2011-07-01 19:47:50 +0000454 SetPixelIndex(image,((byte) & 0xf),q);
cristyed231572011-07-14 02:18:59 +0000455 q+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +0000456 }
457 if ((image->columns % 2) != 0)
458 {
cristybb503372010-05-27 20:51:26 +0000459 byte=(size_t) ReadBlobByte(image);
cristy4c08aed2011-07-01 19:47:50 +0000460 SetPixelIndex(image,((byte >> 4) & 0xf),q);
cristyed231572011-07-14 02:18:59 +0000461 q+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +0000462 }
cristybb503372010-05-27 20:51:26 +0000463 for (x=0; x < (ssize_t) scanline_pad; x++)
cristy3ed852e2009-09-05 21:47:34 +0000464 (void) ReadBlobByte(image);
465 if (SyncAuthenticPixels(image,exception) == MagickFalse)
466 break;
467 if (image->previous == (Image *) NULL)
468 {
469 status=SetImageProgress(image,LoadImageTag,image->rows-y-1,
470 image->rows);
471 if (status == MagickFalse)
472 break;
473 }
474 }
475 break;
476 }
477 case 8:
478 {
479 /*
480 Convert PseudoColor scanline.
481 */
cristybb503372010-05-27 20:51:26 +0000482 for (y=(ssize_t) image->rows-1; y >= 0; y--)
cristy3ed852e2009-09-05 21:47:34 +0000483 {
484 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
cristyacd2ed22011-08-30 01:44:23 +0000485 if (q == (Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +0000486 break;
cristybb503372010-05-27 20:51:26 +0000487 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +0000488 {
cristybb503372010-05-27 20:51:26 +0000489 byte=(size_t) ReadBlobByte(image);
cristy4c08aed2011-07-01 19:47:50 +0000490 SetPixelIndex(image,byte,q);
cristyed231572011-07-14 02:18:59 +0000491 q+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +0000492 }
cristybb503372010-05-27 20:51:26 +0000493 for (x=0; x < (ssize_t) scanline_pad; x++)
cristy3ed852e2009-09-05 21:47:34 +0000494 (void) ReadBlobByte(image);
495 if (SyncAuthenticPixels(image,exception) == MagickFalse)
496 break;
497 if (image->previous == (Image *) NULL)
498 {
499 status=SetImageProgress(image,LoadImageTag,image->rows-y-1,
500 image->rows);
501 if (status == MagickFalse)
502 break;
503 }
504 }
505 break;
506 }
507 case 16:
508 {
509 /*
510 Convert PseudoColor scanline.
511 */
cristybb503372010-05-27 20:51:26 +0000512 for (y=(ssize_t) image->rows-1; y >= 0; y--)
cristy3ed852e2009-09-05 21:47:34 +0000513 {
514 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
cristyacd2ed22011-08-30 01:44:23 +0000515 if (q == (Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +0000516 break;
cristybb503372010-05-27 20:51:26 +0000517 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +0000518 {
cristybb503372010-05-27 20:51:26 +0000519 byte=(size_t) ReadBlobByte(image);
520 byte|=(size_t) (ReadBlobByte(image) << 8);
cristy4c08aed2011-07-01 19:47:50 +0000521 SetPixelIndex(image,byte,q);
cristyed231572011-07-14 02:18:59 +0000522 q+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +0000523 }
cristybb503372010-05-27 20:51:26 +0000524 for (x=0; x < (ssize_t) scanline_pad; x++)
cristy3ed852e2009-09-05 21:47:34 +0000525 (void) ReadBlobByte(image);
526 if (SyncAuthenticPixels(image,exception) == MagickFalse)
527 break;
528 if (image->previous == (Image *) NULL)
529 {
530 status=SetImageProgress(image,LoadImageTag,image->rows-y-1,
531 image->rows);
532 if (status == MagickFalse)
533 break;
534 }
535 }
536 break;
537 }
538 case 24:
539 case 32:
540 {
541 /*
542 Convert DirectColor scanline.
543 */
cristybb503372010-05-27 20:51:26 +0000544 for (y=(ssize_t) image->rows-1; y >= 0; y--)
cristy3ed852e2009-09-05 21:47:34 +0000545 {
546 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
cristyacd2ed22011-08-30 01:44:23 +0000547 if (q == (Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +0000548 break;
cristybb503372010-05-27 20:51:26 +0000549 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +0000550 {
cristy4c08aed2011-07-01 19:47:50 +0000551 SetPixelBlue(image,ScaleCharToQuantum((unsigned char)
552 ReadBlobByte(image)),q);
553 SetPixelGreen(image,ScaleCharToQuantum((unsigned char)
554 ReadBlobByte(image)),q);
555 SetPixelRed(image,ScaleCharToQuantum((unsigned char)
556 ReadBlobByte(image)),q);
cristy3ed852e2009-09-05 21:47:34 +0000557 if (icon_info.bits_per_pixel == 32)
cristy4c08aed2011-07-01 19:47:50 +0000558 SetPixelAlpha(image,ScaleCharToQuantum((unsigned char)
559 ReadBlobByte(image)),q);
cristyed231572011-07-14 02:18:59 +0000560 q+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +0000561 }
562 if (icon_info.bits_per_pixel == 24)
cristybb503372010-05-27 20:51:26 +0000563 for (x=0; x < (ssize_t) scanline_pad; x++)
cristy3ed852e2009-09-05 21:47:34 +0000564 (void) ReadBlobByte(image);
565 if (SyncAuthenticPixels(image,exception) == MagickFalse)
566 break;
567 if (image->previous == (Image *) NULL)
568 {
569 status=SetImageProgress(image,LoadImageTag,image->rows-y-1,
570 image->rows);
571 if (status == MagickFalse)
572 break;
573 }
574 }
575 break;
576 }
577 default:
578 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
579 }
cristy5dd71882010-08-01 20:53:13 +0000580 if (image_info->ping == MagickFalse)
581 (void) SyncImage(image);
cristy3ed852e2009-09-05 21:47:34 +0000582 if (icon_info.bits_per_pixel != 32)
583 {
584 /*
585 Read the ICON alpha mask.
586 */
587 image->storage_class=DirectClass;
cristybb503372010-05-27 20:51:26 +0000588 for (y=(ssize_t) image->rows-1; y >= 0; y--)
cristy3ed852e2009-09-05 21:47:34 +0000589 {
590 q=GetAuthenticPixels(image,0,y,image->columns,1,exception);
cristyacd2ed22011-08-30 01:44:23 +0000591 if (q == (Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +0000592 break;
cristybb503372010-05-27 20:51:26 +0000593 for (x=0; x < ((ssize_t) image->columns-7); x+=8)
cristy3ed852e2009-09-05 21:47:34 +0000594 {
cristybb503372010-05-27 20:51:26 +0000595 byte=(size_t) ReadBlobByte(image);
cristy3ed852e2009-09-05 21:47:34 +0000596 for (bit=0; bit < 8; bit++)
cristy4c08aed2011-07-01 19:47:50 +0000597 {
598 SetPixelAlpha(image,(((byte & (0x80 >> bit)) != 0) ?
599 TransparentAlpha : OpaqueAlpha),q);
cristyed231572011-07-14 02:18:59 +0000600 q+=GetPixelChannels(image);
cristy4c08aed2011-07-01 19:47:50 +0000601 }
cristy3ed852e2009-09-05 21:47:34 +0000602 }
603 if ((image->columns % 8) != 0)
604 {
cristybb503372010-05-27 20:51:26 +0000605 byte=(size_t) ReadBlobByte(image);
cristy3ed852e2009-09-05 21:47:34 +0000606 for (bit=0; bit < (image->columns % 8); bit++)
cristy4c08aed2011-07-01 19:47:50 +0000607 {
608 SetPixelAlpha(image,(((byte & (0x80 >> bit)) != 0) ?
609 TransparentAlpha : OpaqueAlpha),q);
cristyed231572011-07-14 02:18:59 +0000610 q+=GetPixelChannels(image);
cristy4c08aed2011-07-01 19:47:50 +0000611 }
cristy3ed852e2009-09-05 21:47:34 +0000612 }
cristy0e8200f2009-09-13 21:10:06 +0000613 if ((image->columns % 32) != 0)
cristybb503372010-05-27 20:51:26 +0000614 for (x=0; x < (ssize_t) ((32-(image->columns % 32))/8); x++)
cristy0e8200f2009-09-13 21:10:06 +0000615 (void) ReadBlobByte(image);
cristy3ed852e2009-09-05 21:47:34 +0000616 if (SyncAuthenticPixels(image,exception) == MagickFalse)
617 break;
618 }
619 }
620 if (EOFBlob(image) != MagickFalse)
621 {
622 ThrowFileException(exception,CorruptImageError,
623 "UnexpectedEndOfFile",image->filename);
624 break;
625 }
626 }
627 /*
628 Proceed to next image.
629 */
630 if (image_info->number_scenes != 0)
631 if (image->scene >= (image_info->scene+image_info->number_scenes-1))
632 break;
cristybb503372010-05-27 20:51:26 +0000633 if (i < (ssize_t) (icon_file.count-1))
cristy3ed852e2009-09-05 21:47:34 +0000634 {
635 /*
636 Allocate next image structure.
637 */
638 AcquireNextImage(image_info,image);
639 if (GetNextImageInList(image) == (Image *) NULL)
640 {
641 image=DestroyImageList(image);
642 return((Image *) NULL);
643 }
644 image=SyncNextImageInList(image);
645 status=SetImageProgress(image,LoadImagesTag,TellBlob(image),
646 GetBlobSize(image));
647 if (status == MagickFalse)
648 break;
649 }
650 }
651 (void) CloseBlob(image);
652 return(GetFirstImageInList(image));
653}
654
655/*
656%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
657% %
658% %
659% %
660% R e g i s t e r I C O N I m a g e %
661% %
662% %
663% %
664%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
665%
666% RegisterICONImage() adds attributes for the Icon image format to
667% the list of supported formats. The attributes include the image format
668% tag, a method to read and/or write the format, whether the format
669% supports the saving of more than one frame to the same file or blob,
670% whether the format supports native in-memory I/O, and a brief
671% description of the format.
672%
673% The format of the RegisterICONImage method is:
674%
cristybb503372010-05-27 20:51:26 +0000675% size_t RegisterICONImage(void)
cristy3ed852e2009-09-05 21:47:34 +0000676%
677*/
cristybb503372010-05-27 20:51:26 +0000678ModuleExport size_t RegisterICONImage(void)
cristy3ed852e2009-09-05 21:47:34 +0000679{
680 MagickInfo
681 *entry;
682
683 entry=SetMagickInfo("CUR");
684 entry->decoder=(DecodeImageHandler *) ReadICONImage;
685 entry->encoder=(EncodeImageHandler *) WriteICONImage;
686 entry->adjoin=MagickFalse;
687 entry->seekable_stream=MagickTrue;
688 entry->description=ConstantString("Microsoft icon");
689 entry->module=ConstantString("CUR");
690 (void) RegisterMagickInfo(entry);
691 entry=SetMagickInfo("ICO");
692 entry->decoder=(DecodeImageHandler *) ReadICONImage;
693 entry->encoder=(EncodeImageHandler *) WriteICONImage;
694 entry->adjoin=MagickTrue;
695 entry->seekable_stream=MagickTrue;
696 entry->description=ConstantString("Microsoft icon");
697 entry->module=ConstantString("ICON");
698 (void) RegisterMagickInfo(entry);
699 entry=SetMagickInfo("ICON");
700 entry->decoder=(DecodeImageHandler *) ReadICONImage;
701 entry->encoder=(EncodeImageHandler *) WriteICONImage;
702 entry->adjoin=MagickFalse;
703 entry->seekable_stream=MagickTrue;
704 entry->description=ConstantString("Microsoft icon");
705 entry->module=ConstantString("ICON");
706 (void) RegisterMagickInfo(entry);
707 return(MagickImageCoderSignature);
708}
709
710/*
711%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
712% %
713% %
714% %
715% U n r e g i s t e r I C O N I m a g e %
716% %
717% %
718% %
719%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
720%
721% UnregisterICONImage() removes format registrations made by the
722% ICON module from the list of supported formats.
723%
724% The format of the UnregisterICONImage method is:
725%
726% UnregisterICONImage(void)
727%
728*/
729ModuleExport void UnregisterICONImage(void)
730{
731 (void) UnregisterMagickInfo("CUR");
732 (void) UnregisterMagickInfo("ICO");
733 (void) UnregisterMagickInfo("ICON");
734}
735
736/*
737%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
738% %
739% %
740% %
741% W r i t e I C O N I m a g e %
742% %
743% %
744% %
745%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
746%
747% WriteICONImage() writes an image in Microsoft Windows bitmap encoded
748% image format, version 3 for Windows or (if the image has a matte channel)
749% version 4.
750%
751% The format of the WriteICONImage method is:
752%
753% MagickBooleanType WriteICONImage(const ImageInfo *image_info,
cristy1e178e72011-08-28 19:44:34 +0000754% Image *image,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000755%
756% A description of each parameter follows.
757%
758% o image_info: the image info.
759%
760% o image: The image.
761%
cristy1e178e72011-08-28 19:44:34 +0000762% o exception: return any errors or warnings in this structure.
763%
cristy3ed852e2009-09-05 21:47:34 +0000764*/
765static MagickBooleanType WriteICONImage(const ImageInfo *image_info,
cristy1e178e72011-08-28 19:44:34 +0000766 Image *image,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000767{
768 IconFile
769 icon_file;
770
771 IconInfo
772 icon_info;
773
774 Image
775 *next;
776
cristy3ed852e2009-09-05 21:47:34 +0000777 MagickBooleanType
778 status;
779
780 MagickOffsetType
781 offset,
782 scene;
783
cristy4c08aed2011-07-01 19:47:50 +0000784 register const Quantum
cristy3ed852e2009-09-05 21:47:34 +0000785 *p;
786
cristybb503372010-05-27 20:51:26 +0000787 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000788 i,
789 x;
790
791 register unsigned char
792 *q;
793
cristyebc891a2011-04-24 23:04:16 +0000794 size_t
795 bytes_per_line,
796 scanline_pad;
797
798 ssize_t
799 y;
800
cristy3ed852e2009-09-05 21:47:34 +0000801 unsigned char
802 bit,
803 byte,
804 *pixels;
805
cristy3ed852e2009-09-05 21:47:34 +0000806 /*
807 Open output image file.
808 */
809 assert(image_info != (const ImageInfo *) NULL);
810 assert(image_info->signature == MagickSignature);
811 assert(image != (Image *) NULL);
812 assert(image->signature == MagickSignature);
813 (void) LogMagickEvent(CoderEvent,GetMagickModule(),"%s",image->filename);
cristy3a37efd2011-08-28 20:31:03 +0000814 assert(exception != (ExceptionInfo *) NULL);
815 assert(exception->signature == MagickSignature);
cristy1e178e72011-08-28 19:44:34 +0000816 status=OpenBlob(image_info,image,WriteBinaryBlobMode,exception);
cristy3ed852e2009-09-05 21:47:34 +0000817 if (status == MagickFalse)
818 return(status);
819 scene=0;
820 next=image;
821 do
822 {
823 if ((image->columns > 256L) || (image->rows > 256L))
824 ThrowWriterException(ImageError,"WidthOrHeightExceedsLimit");
825 scene++;
826 next=SyncNextImageInList(next);
827 } while ((next != (Image *) NULL) && (image_info->adjoin != MagickFalse));
828 /*
829 Dump out a ICON header template to be properly initialized later.
830 */
831 (void) WriteBlobLSBShort(image,0);
832 (void) WriteBlobLSBShort(image,1);
833 (void) WriteBlobLSBShort(image,(unsigned char) scene);
834 (void) ResetMagickMemory(&icon_file,0,sizeof(icon_file));
835 (void) ResetMagickMemory(&icon_info,0,sizeof(icon_info));
836 scene=0;
837 next=image;
838 do
839 {
840 (void) WriteBlobByte(image,icon_file.directory[scene].width);
841 (void) WriteBlobByte(image,icon_file.directory[scene].height);
842 (void) WriteBlobByte(image,icon_file.directory[scene].colors);
843 (void) WriteBlobByte(image,icon_file.directory[scene].reserved);
844 (void) WriteBlobLSBShort(image,icon_file.directory[scene].planes);
845 (void) WriteBlobLSBShort(image,icon_file.directory[scene].bits_per_pixel);
cristy0b29b252010-05-30 01:59:46 +0000846 (void) WriteBlobLSBLong(image,(unsigned int)
847 icon_file.directory[scene].size);
848 (void) WriteBlobLSBLong(image,(unsigned int)
849 icon_file.directory[scene].offset);
cristy3ed852e2009-09-05 21:47:34 +0000850 scene++;
851 next=SyncNextImageInList(next);
852 } while ((next != (Image *) NULL) && (image_info->adjoin != MagickFalse));
853 scene=0;
854 next=image;
855 do
856 {
857 if ((next->columns == 256) && (next->rows == 256))
858 {
859 Image
860 *write_image;
861
862 ImageInfo
863 *write_info;
864
865 size_t
866 length;
867
868 unsigned char
869 *png;
870
871 /*
872 Icon image encoded as a compressed PNG image.
873 */
cristy1e178e72011-08-28 19:44:34 +0000874 write_image=CloneImage(next,0,0,MagickTrue,exception);
cristy3ed852e2009-09-05 21:47:34 +0000875 if (write_image == (Image *) NULL)
876 return(MagickFalse);
877 write_info=CloneImageInfo(image_info);
878 (void) CopyMagickString(write_info->filename,"PNG:",MaxTextExtent);
879 png=(unsigned char *) ImageToBlob(write_info,write_image,&length,
cristy1e178e72011-08-28 19:44:34 +0000880 exception);
cristy3ed852e2009-09-05 21:47:34 +0000881 write_image=DestroyImage(write_image);
882 write_info=DestroyImageInfo(write_info);
883 if (png == (unsigned char *) NULL)
884 return(MagickFalse);
885 icon_file.directory[scene].width=0;
886 icon_file.directory[scene].height=0;
887 icon_file.directory[scene].colors=0;
888 icon_file.directory[scene].reserved=0;
889 icon_file.directory[scene].planes=1;
890 icon_file.directory[scene].bits_per_pixel=32;
cristybb503372010-05-27 20:51:26 +0000891 icon_file.directory[scene].size=(size_t) length;
892 icon_file.directory[scene].offset=(size_t) TellBlob(image);
cristy3ed852e2009-09-05 21:47:34 +0000893 (void) WriteBlob(image,(size_t) length,png);
894 png=(unsigned char *) RelinquishMagickMemory(png);
895 }
896 else
897 {
898 /*
899 Initialize ICON raster file header.
900 */
901 if (next->colorspace != RGBColorspace)
902 (void) TransformImageColorspace(next,RGBColorspace);
903 icon_info.file_size=14+12+28;
904 icon_info.offset_bits=icon_info.file_size;
905 icon_info.compression=BI_RGB;
906 if ((next->storage_class != DirectClass) && (next->colors > 256))
cristy1e178e72011-08-28 19:44:34 +0000907 (void) SetImageStorageClass(next,DirectClass,exception);
cristy3ed852e2009-09-05 21:47:34 +0000908 if (next->storage_class == DirectClass)
909 {
910 /*
911 Full color ICON raster.
912 */
913 icon_info.number_colors=0;
914 icon_info.bits_per_pixel=32;
cristybb503372010-05-27 20:51:26 +0000915 icon_info.compression=(size_t) BI_RGB;
cristy3ed852e2009-09-05 21:47:34 +0000916 }
917 else
918 {
cristy35ef8242010-06-03 16:24:13 +0000919 size_t
920 one;
921
cristy3ed852e2009-09-05 21:47:34 +0000922 /*
923 Colormapped ICON raster.
924 */
925 icon_info.bits_per_pixel=8;
926 if (next->colors <= 256)
927 icon_info.bits_per_pixel=8;
928 if (next->colors <= 16)
929 icon_info.bits_per_pixel=4;
930 if (next->colors <= 2)
931 icon_info.bits_per_pixel=1;
cristy35ef8242010-06-03 16:24:13 +0000932 one=1;
933 icon_info.number_colors=one << icon_info.bits_per_pixel;
cristy3ed852e2009-09-05 21:47:34 +0000934 if (icon_info.number_colors < next->colors)
935 {
cristy1e178e72011-08-28 19:44:34 +0000936 (void) SetImageStorageClass(next,DirectClass,exception);
cristy3ed852e2009-09-05 21:47:34 +0000937 icon_info.number_colors=0;
938 icon_info.bits_per_pixel=(unsigned short) 24;
cristybb503372010-05-27 20:51:26 +0000939 icon_info.compression=(size_t) BI_RGB;
cristy3ed852e2009-09-05 21:47:34 +0000940 }
941 else
942 {
cristy0b29b252010-05-30 01:59:46 +0000943 size_t
944 one;
945
946 one=1;
947 icon_info.file_size+=3*(one << icon_info.bits_per_pixel);
948 icon_info.offset_bits+=3*(one << icon_info.bits_per_pixel);
949 icon_info.file_size+=(one << icon_info.bits_per_pixel);
950 icon_info.offset_bits+=(one << icon_info.bits_per_pixel);
cristy3ed852e2009-09-05 21:47:34 +0000951 }
952 }
953 bytes_per_line=(((next->columns*icon_info.bits_per_pixel)+31) &
954 ~31) >> 3;
955 icon_info.ba_offset=0;
cristybb503372010-05-27 20:51:26 +0000956 icon_info.width=(ssize_t) next->columns;
957 icon_info.height=(ssize_t) next->rows;
cristy3ed852e2009-09-05 21:47:34 +0000958 icon_info.planes=1;
959 icon_info.image_size=bytes_per_line*next->rows;
960 icon_info.size=40;
961 icon_info.size+=(4*icon_info.number_colors);
962 icon_info.size+=icon_info.image_size;
963 icon_info.size+=(((icon_info.width+31) & ~31) >> 3)*icon_info.height;
964 icon_info.file_size+=icon_info.image_size;
965 icon_info.x_pixels=0;
966 icon_info.y_pixels=0;
967 switch (next->units)
968 {
969 case UndefinedResolution:
970 case PixelsPerInchResolution:
971 {
cristybb503372010-05-27 20:51:26 +0000972 icon_info.x_pixels=(size_t) (100.0*next->x_resolution/2.54);
973 icon_info.y_pixels=(size_t) (100.0*next->y_resolution/2.54);
cristy3ed852e2009-09-05 21:47:34 +0000974 break;
975 }
976 case PixelsPerCentimeterResolution:
977 {
cristybb503372010-05-27 20:51:26 +0000978 icon_info.x_pixels=(size_t) (100.0*next->x_resolution);
979 icon_info.y_pixels=(size_t) (100.0*next->y_resolution);
cristy3ed852e2009-09-05 21:47:34 +0000980 break;
981 }
982 }
983 icon_info.colors_important=icon_info.number_colors;
984 /*
985 Convert MIFF to ICON raster pixels.
986 */
987 pixels=(unsigned char *) AcquireQuantumMemory((size_t)
988 icon_info.image_size,sizeof(*pixels));
989 if (pixels == (unsigned char *) NULL)
990 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
991 (void) ResetMagickMemory(pixels,0,(size_t) icon_info.image_size);
992 switch (icon_info.bits_per_pixel)
993 {
994 case 1:
995 {
cristybb503372010-05-27 20:51:26 +0000996 size_t
cristy3ed852e2009-09-05 21:47:34 +0000997 bit,
998 byte;
999
1000 /*
1001 Convert PseudoClass image to a ICON monochrome image.
1002 */
cristybb503372010-05-27 20:51:26 +00001003 for (y=0; y < (ssize_t) next->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00001004 {
cristy1e178e72011-08-28 19:44:34 +00001005 p=GetVirtualPixels(next,0,y,next->columns,1,exception);
cristy4c08aed2011-07-01 19:47:50 +00001006 if (p == (const Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00001007 break;
cristy3ed852e2009-09-05 21:47:34 +00001008 q=pixels+(next->rows-y-1)*bytes_per_line;
1009 bit=0;
1010 byte=0;
cristybb503372010-05-27 20:51:26 +00001011 for (x=0; x < (ssize_t) next->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00001012 {
1013 byte<<=1;
cristy4c08aed2011-07-01 19:47:50 +00001014 byte|=GetPixelIndex(next,p) != 0 ? 0x01 : 0x00;
cristy3ed852e2009-09-05 21:47:34 +00001015 bit++;
1016 if (bit == 8)
1017 {
1018 *q++=(unsigned char) byte;
1019 bit=0;
1020 byte=0;
1021 }
cristyed231572011-07-14 02:18:59 +00001022 p+=GetPixelChannels(image);
cristy4c08aed2011-07-01 19:47:50 +00001023 }
cristy3ed852e2009-09-05 21:47:34 +00001024 if (bit != 0)
1025 *q++=(unsigned char) (byte << (8-bit));
1026 if (next->previous == (Image *) NULL)
1027 {
1028 status=SetImageProgress(next,SaveImageTag,y,next->rows);
1029 if (status == MagickFalse)
1030 break;
1031 }
1032 }
1033 break;
1034 }
1035 case 4:
1036 {
cristybb503372010-05-27 20:51:26 +00001037 size_t
cristy3ed852e2009-09-05 21:47:34 +00001038 nibble,
1039 byte;
1040
1041 /*
1042 Convert PseudoClass image to a ICON monochrome image.
1043 */
cristybb503372010-05-27 20:51:26 +00001044 for (y=0; y < (ssize_t) next->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00001045 {
cristy1e178e72011-08-28 19:44:34 +00001046 p=GetVirtualPixels(next,0,y,next->columns,1,exception);
cristy4c08aed2011-07-01 19:47:50 +00001047 if (p == (const Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00001048 break;
cristy3ed852e2009-09-05 21:47:34 +00001049 q=pixels+(next->rows-y-1)*bytes_per_line;
1050 nibble=0;
1051 byte=0;
cristybb503372010-05-27 20:51:26 +00001052 for (x=0; x < (ssize_t) next->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00001053 {
1054 byte<<=4;
cristy4c08aed2011-07-01 19:47:50 +00001055 byte|=((size_t) GetPixelIndex(next,p) & 0x0f);
cristy3ed852e2009-09-05 21:47:34 +00001056 nibble++;
1057 if (nibble == 2)
1058 {
1059 *q++=(unsigned char) byte;
1060 nibble=0;
1061 byte=0;
1062 }
cristyed231572011-07-14 02:18:59 +00001063 p+=GetPixelChannels(image);
cristy4c08aed2011-07-01 19:47:50 +00001064 }
cristy3ed852e2009-09-05 21:47:34 +00001065 if (nibble != 0)
1066 *q++=(unsigned char) (byte << 4);
1067 if (next->previous == (Image *) NULL)
1068 {
1069 status=SetImageProgress(next,SaveImageTag,y,next->rows);
1070 if (status == MagickFalse)
1071 break;
1072 }
1073 }
1074 break;
1075 }
1076 case 8:
1077 {
1078 /*
1079 Convert PseudoClass packet to ICON pixel.
1080 */
cristybb503372010-05-27 20:51:26 +00001081 for (y=0; y < (ssize_t) next->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00001082 {
cristy1e178e72011-08-28 19:44:34 +00001083 p=GetVirtualPixels(next,0,y,next->columns,1,exception);
cristy4c08aed2011-07-01 19:47:50 +00001084 if (p == (const Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00001085 break;
cristy3ed852e2009-09-05 21:47:34 +00001086 q=pixels+(next->rows-y-1)*bytes_per_line;
cristybb503372010-05-27 20:51:26 +00001087 for (x=0; x < (ssize_t) next->columns; x++)
cristy4c08aed2011-07-01 19:47:50 +00001088 {
1089 *q++=(unsigned char) GetPixelIndex(next,p);
cristyed231572011-07-14 02:18:59 +00001090 p+=GetPixelChannels(image);
cristy4c08aed2011-07-01 19:47:50 +00001091 }
cristy3ed852e2009-09-05 21:47:34 +00001092 if (next->previous == (Image *) NULL)
1093 {
1094 status=SetImageProgress(next,SaveImageTag,y,next->rows);
1095 if (status == MagickFalse)
1096 break;
1097 }
1098 }
1099 break;
1100 }
1101 case 24:
1102 case 32:
1103 {
1104 /*
1105 Convert DirectClass packet to ICON BGR888 or BGRA8888 pixel.
1106 */
cristybb503372010-05-27 20:51:26 +00001107 for (y=0; y < (ssize_t) next->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00001108 {
cristy1e178e72011-08-28 19:44:34 +00001109 p=GetVirtualPixels(next,0,y,next->columns,1,exception);
cristy4c08aed2011-07-01 19:47:50 +00001110 if (p == (const Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00001111 break;
1112 q=pixels+(next->rows-y-1)*bytes_per_line;
cristybb503372010-05-27 20:51:26 +00001113 for (x=0; x < (ssize_t) next->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00001114 {
cristy4c08aed2011-07-01 19:47:50 +00001115 *q++=ScaleQuantumToChar(GetPixelBlue(next,p));
1116 *q++=ScaleQuantumToChar(GetPixelGreen(next,p));
1117 *q++=ScaleQuantumToChar(GetPixelRed(next,p));
cristy3ed852e2009-09-05 21:47:34 +00001118 if (next->matte == MagickFalse)
1119 *q++=ScaleQuantumToChar(QuantumRange);
1120 else
cristy4c08aed2011-07-01 19:47:50 +00001121 *q++=ScaleQuantumToChar(GetPixelAlpha(next,p));
cristyed231572011-07-14 02:18:59 +00001122 p+=GetPixelChannels(next);
cristy3ed852e2009-09-05 21:47:34 +00001123 }
1124 if (icon_info.bits_per_pixel == 24)
cristybb503372010-05-27 20:51:26 +00001125 for (x=3L*(ssize_t) next->columns; x < (ssize_t) bytes_per_line; x++)
cristy3ed852e2009-09-05 21:47:34 +00001126 *q++=0x00;
1127 if (next->previous == (Image *) NULL)
1128 {
1129 status=SetImageProgress(next,SaveImageTag,y,next->rows);
1130 if (status == MagickFalse)
1131 break;
1132 }
1133 }
1134 break;
1135 }
1136 }
1137 /*
1138 Write 40-byte version 3+ bitmap header.
1139 */
1140 icon_file.directory[scene].width=(unsigned char) icon_info.width;
1141 icon_file.directory[scene].height=(unsigned char) icon_info.height;
1142 icon_file.directory[scene].colors=(unsigned char)
1143 icon_info.number_colors;
1144 icon_file.directory[scene].reserved=0;
1145 icon_file.directory[scene].planes=icon_info.planes;
1146 icon_file.directory[scene].bits_per_pixel=icon_info.bits_per_pixel;
1147 icon_file.directory[scene].size=icon_info.size;
cristybb503372010-05-27 20:51:26 +00001148 icon_file.directory[scene].offset=(size_t) TellBlob(image);
cristy35ef8242010-06-03 16:24:13 +00001149 (void) WriteBlobLSBLong(image,(unsigned int) 40);
1150 (void) WriteBlobLSBLong(image,(unsigned int) icon_info.width);
1151 (void) WriteBlobLSBLong(image,(unsigned int) icon_info.height*2);
cristy3ed852e2009-09-05 21:47:34 +00001152 (void) WriteBlobLSBShort(image,icon_info.planes);
1153 (void) WriteBlobLSBShort(image,icon_info.bits_per_pixel);
cristy35ef8242010-06-03 16:24:13 +00001154 (void) WriteBlobLSBLong(image,(unsigned int) icon_info.compression);
1155 (void) WriteBlobLSBLong(image,(unsigned int) icon_info.image_size);
1156 (void) WriteBlobLSBLong(image,(unsigned int) icon_info.x_pixels);
1157 (void) WriteBlobLSBLong(image,(unsigned int) icon_info.y_pixels);
1158 (void) WriteBlobLSBLong(image,(unsigned int) icon_info.number_colors);
1159 (void) WriteBlobLSBLong(image,(unsigned int) icon_info.colors_important);
cristy3ed852e2009-09-05 21:47:34 +00001160 if (next->storage_class == PseudoClass)
1161 {
1162 unsigned char
1163 *icon_colormap;
1164
1165 /*
1166 Dump colormap to file.
1167 */
1168 icon_colormap=(unsigned char *) AcquireQuantumMemory((size_t)
1169 (1UL << icon_info.bits_per_pixel),4UL*sizeof(*icon_colormap));
1170 if (icon_colormap == (unsigned char *) NULL)
1171 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
1172 q=icon_colormap;
cristybb503372010-05-27 20:51:26 +00001173 for (i=0; i < (ssize_t) next->colors; i++)
cristy3ed852e2009-09-05 21:47:34 +00001174 {
1175 *q++=ScaleQuantumToChar(next->colormap[i].blue);
1176 *q++=ScaleQuantumToChar(next->colormap[i].green);
1177 *q++=ScaleQuantumToChar(next->colormap[i].red);
1178 *q++=(unsigned char) 0x0;
1179 }
cristybb503372010-05-27 20:51:26 +00001180 for ( ; i < (ssize_t) (1UL << icon_info.bits_per_pixel); i++)
cristy3ed852e2009-09-05 21:47:34 +00001181 {
1182 *q++=(unsigned char) 0x00;
1183 *q++=(unsigned char) 0x00;
1184 *q++=(unsigned char) 0x00;
1185 *q++=(unsigned char) 0x00;
1186 }
1187 (void) WriteBlob(image,(size_t) (4UL*(1UL <<
1188 icon_info.bits_per_pixel)),icon_colormap);
1189 icon_colormap=(unsigned char *) RelinquishMagickMemory(
1190 icon_colormap);
1191 }
1192 (void) WriteBlob(image,(size_t) icon_info.image_size,pixels);
1193 pixels=(unsigned char *) RelinquishMagickMemory(pixels);
1194 /*
1195 Write matte mask.
1196 */
1197 scanline_pad=(((next->columns+31) & ~31)-next->columns) >> 3;
cristybb503372010-05-27 20:51:26 +00001198 for (y=((ssize_t) next->rows - 1); y >= 0; y--)
cristy3ed852e2009-09-05 21:47:34 +00001199 {
cristy1e178e72011-08-28 19:44:34 +00001200 p=GetVirtualPixels(next,0,y,next->columns,1,exception);
cristy4c08aed2011-07-01 19:47:50 +00001201 if (p == (const Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00001202 break;
1203 bit=0;
1204 byte=0;
cristybb503372010-05-27 20:51:26 +00001205 for (x=0; x < (ssize_t) next->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00001206 {
1207 byte<<=1;
1208 if ((next->matte == MagickTrue) &&
cristy4c08aed2011-07-01 19:47:50 +00001209 (GetPixelAlpha(next,p) == (Quantum) TransparentAlpha))
cristy3ed852e2009-09-05 21:47:34 +00001210 byte|=0x01;
1211 bit++;
1212 if (bit == 8)
1213 {
1214 (void) WriteBlobByte(image,(unsigned char) byte);
1215 bit=0;
1216 byte=0;
1217 }
cristyed231572011-07-14 02:18:59 +00001218 p+=GetPixelChannels(next);
cristy3ed852e2009-09-05 21:47:34 +00001219 }
1220 if (bit != 0)
1221 (void) WriteBlobByte(image,(unsigned char) (byte << (8-bit)));
cristybb503372010-05-27 20:51:26 +00001222 for (i=0; i < (ssize_t) scanline_pad; i++)
cristy3ed852e2009-09-05 21:47:34 +00001223 (void) WriteBlobByte(image,(unsigned char) 0);
1224 }
1225 }
1226 if (GetNextImageInList(next) == (Image *) NULL)
1227 break;
1228 next=SyncNextImageInList(next);
1229 status=SetImageProgress(next,SaveImagesTag,scene++,
1230 GetImageListLength(next));
1231 if (status == MagickFalse)
1232 break;
1233 } while ((next != (Image *) NULL) && (image_info->adjoin != MagickFalse));
1234 offset=SeekBlob(image,0,SEEK_SET);
cristyda16f162011-02-19 23:52:17 +00001235 (void) offset;
cristy3ed852e2009-09-05 21:47:34 +00001236 (void) WriteBlobLSBShort(image,0);
1237 (void) WriteBlobLSBShort(image,1);
1238 (void) WriteBlobLSBShort(image,(unsigned short) (scene+1));
1239 scene=0;
1240 next=image;
1241 do
1242 {
1243 (void) WriteBlobByte(image,icon_file.directory[scene].width);
1244 (void) WriteBlobByte(image,icon_file.directory[scene].height);
1245 (void) WriteBlobByte(image,icon_file.directory[scene].colors);
1246 (void) WriteBlobByte(image,icon_file.directory[scene].reserved);
1247 (void) WriteBlobLSBShort(image,icon_file.directory[scene].planes);
1248 (void) WriteBlobLSBShort(image,icon_file.directory[scene].bits_per_pixel);
cristy0b29b252010-05-30 01:59:46 +00001249 (void) WriteBlobLSBLong(image,(unsigned int)
1250 icon_file.directory[scene].size);
1251 (void) WriteBlobLSBLong(image,(unsigned int)
1252 icon_file.directory[scene].offset);
cristy3ed852e2009-09-05 21:47:34 +00001253 scene++;
1254 next=SyncNextImageInList(next);
1255 } while ((next != (Image *) NULL) && (image_info->adjoin != MagickFalse));
1256 (void) CloseBlob(image);
1257 return(MagickTrue);
1258}