]> granicus.if.org Git - imagemagick/blob - coders/icon.c
(no commit message)
[imagemagick] / coders / icon.c
1 /*
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 %                                                                             %
20 %  Copyright 1999-2010 ImageMagick Studio LLC, a non-profit organization      %
21 %  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 \f
39 /*
40   Include declarations.
41 */
42 #include "magick/studio.h"
43 #include "magick/blob.h"
44 #include "magick/blob-private.h"
45 #include "magick/cache.h"
46 #include "magick/colormap.h"
47 #include "magick/colorspace.h"
48 #include "magick/exception.h"
49 #include "magick/exception-private.h"
50 #include "magick/image.h"
51 #include "magick/image-private.h"
52 #include "magick/list.h"
53 #include "magick/log.h"
54 #include "magick/magick.h"
55 #include "magick/memory_.h"
56 #include "magick/monitor.h"
57 #include "magick/monitor-private.h"
58 #include "magick/nt-feature.h"
59 #include "magick/quantize.h"
60 #include "magick/quantum-private.h"
61 #include "magick/static.h"
62 #include "magick/string_.h"
63 #include "magick/module.h"
64 \f
65 /*
66   Define declarations.
67 */
68 #if !defined(MAGICKCORE_WINDOWS_SUPPORT) || defined(__MINGW32__)
69 #define BI_RGB  0
70 #define BI_RLE8  1
71 #define BI_BITFIELDS  3
72 #endif
73 #define MaxIcons  1024
74 \f
75 /*
76   Typedef declarations.
77 */
78 typedef struct _IconEntry
79 {
80   unsigned char
81     width,
82     height,
83     colors,
84     reserved;
85
86   unsigned short int
87     planes,
88     bits_per_pixel;
89
90   size_t
91     size,
92     offset;
93 } IconEntry;
94
95 typedef struct _IconFile
96 {
97   short
98     reserved,
99     resource_type,
100     count;
101
102   IconEntry
103     directory[MaxIcons];
104 } IconFile;
105
106 typedef struct _IconInfo
107 {
108   size_t
109     file_size,
110     ba_offset,
111     offset_bits,
112     size;
113
114   ssize_t
115     width,
116     height;
117
118   unsigned short
119     planes,
120     bits_per_pixel;
121
122   size_t
123     compression,
124     image_size,
125     x_pixels,
126     y_pixels,
127     number_colors,
128     red_mask,
129     green_mask,
130     blue_mask,
131     alpha_mask,
132     colors_important;
133
134   ssize_t
135     colorspace;
136 } IconInfo;
137 \f
138 /*
139   Forward declaractions.
140 */
141 static MagickBooleanType
142   WriteICONImage(const ImageInfo *,Image *);
143 \f
144 /*
145 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
146 %                                                                             %
147 %                                                                             %
148 %                                                                             %
149 %   R e a d I C O N I m a g e                                                 %
150 %                                                                             %
151 %                                                                             %
152 %                                                                             %
153 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
154 %
155 %  ReadICONImage() reads a Microsoft icon image file and returns it.  It
156 %  allocates the memory necessary for the new Image structure and returns a
157 %  pointer to the new image.
158 %
159 %  The format of the ReadICONImage method is:
160 %
161 %      Image *ReadICONImage(const ImageInfo *image_info,
162 %        ExceptionInfo *exception)
163 %
164 %  A description of each parameter follows:
165 %
166 %    o image_info: the image info.
167 %
168 %    o exception: return any errors or warnings in this structure.
169 %
170 */
171 static Image *ReadICONImage(const ImageInfo *image_info,
172   ExceptionInfo *exception)
173 {
174   IconFile
175     icon_file;
176
177   IconInfo
178     icon_info;
179
180   Image
181     *image;
182
183   ssize_t
184     y;
185
186   MagickBooleanType
187     status;
188
189   register IndexPacket
190     *indexes;
191
192   register ssize_t
193     i,
194     x;
195
196   register PixelPacket
197     *q;
198
199   register unsigned char
200     *p;
201
202   ssize_t
203     count,
204     offset;
205
206   size_t
207     bit,
208     byte,
209     bytes_per_line,
210     one,
211     scanline_pad;
212
213   /*
214     Open image file.
215   */
216   assert(image_info != (const ImageInfo *) NULL);
217   assert(image_info->signature == MagickSignature);
218   (void) LogMagickEvent(CoderEvent,GetMagickModule(),"%s",image_info->filename);
219   assert(exception != (ExceptionInfo *) NULL);
220   assert(exception->signature == MagickSignature);
221   image=AcquireImage(image_info);
222   status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception);
223   if (status == MagickFalse)
224     {
225       image=DestroyImageList(image);
226       return((Image *) NULL);
227     }
228   icon_file.reserved=(short) ReadBlobLSBShort(image);
229   icon_file.resource_type=(short) ReadBlobLSBShort(image);
230   icon_file.count=(short) ReadBlobLSBShort(image);
231   if ((icon_file.reserved != 0) ||
232       ((icon_file.resource_type != 1) && (icon_file.resource_type != 2)) ||
233       (icon_file.count > MaxIcons))
234     ThrowReaderException(CorruptImageError,"ImproperImageHeader");
235   for (i=0; i < icon_file.count; i++)
236   {
237     icon_file.directory[i].width=(unsigned char) ReadBlobByte(image);
238     icon_file.directory[i].height=(unsigned char) ReadBlobByte(image);
239     icon_file.directory[i].colors=(unsigned char) ReadBlobByte(image);
240     icon_file.directory[i].reserved=(unsigned char) ReadBlobByte(image);
241     icon_file.directory[i].planes=(unsigned short) ReadBlobLSBShort(image);
242     icon_file.directory[i].bits_per_pixel=(unsigned short)
243       ReadBlobLSBShort(image);
244     icon_file.directory[i].size=ReadBlobLSBLong(image);
245     icon_file.directory[i].offset=ReadBlobLSBLong(image);
246   }
247   one=1;
248   for (i=0; i < icon_file.count; i++)
249   {
250     /*
251       Verify Icon identifier.
252     */
253     offset=(ssize_t) SeekBlob(image,(MagickOffsetType)
254       icon_file.directory[i].offset,SEEK_SET);
255     if (offset < 0)
256       ThrowReaderException(CorruptImageError,"ImproperImageHeader");
257     icon_info.size=ReadBlobLSBLong(image);
258     icon_info.width=(unsigned char) ReadBlobLSBLong(image);
259     icon_info.height=(unsigned char) ReadBlobLSBLong(image)/2;
260     if ((icon_file.directory[i].width == 0) && 
261         (icon_file.directory[i].height == 0))
262       {
263         Image
264           *icon_image;
265
266         ImageInfo
267           *read_info;
268
269         size_t
270           length;
271
272         unsigned char
273           *png;
274
275         /*
276           Icon image encoded as a compressed PNG image.
277         */
278         length=icon_file.directory[i].size;
279         png=(unsigned char *) AcquireQuantumMemory(length+12,sizeof(*png));
280         if (png == (unsigned char *) NULL)
281           ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
282         (void) CopyMagickMemory(png,"\211PNG\r\n\032\n\000\000\000\015",12);
283         count=ReadBlob(image,length-12,png+12);
284         if (count != (ssize_t) (length-12))
285           {
286             png=(unsigned char *) RelinquishMagickMemory(png);
287             ThrowReaderException(CorruptImageError,
288               "InsufficientImageDataInFile");
289           }
290         read_info=CloneImageInfo(image_info);
291         (void) CopyMagickString(read_info->magick,"PNG",MaxTextExtent);
292         icon_image=BlobToImage(read_info,png,length+12,exception);
293         read_info=DestroyImageInfo(read_info);
294         png=(unsigned char *) RelinquishMagickMemory(png);
295         if (icon_image == (Image *) NULL)
296           {
297             image=DestroyImageList(image);
298             return((Image *) NULL);
299           }
300         DestroyBlob(icon_image);
301         icon_image->blob=ReferenceBlob(image->blob);
302         ReplaceImageInList(&image,icon_image);
303       }
304     else
305       {
306         icon_info.planes=ReadBlobLSBShort(image);
307         icon_info.bits_per_pixel=ReadBlobLSBShort(image);
308         if (icon_info.bits_per_pixel > 32)
309           ThrowReaderException(CorruptImageError,"ImproperImageHeader");
310         icon_info.compression=ReadBlobLSBLong(image);
311         icon_info.image_size=ReadBlobLSBLong(image);
312         icon_info.x_pixels=ReadBlobLSBLong(image);
313         icon_info.y_pixels=ReadBlobLSBLong(image);
314         icon_info.number_colors=ReadBlobLSBLong(image);
315         icon_info.colors_important=ReadBlobLSBLong(image);
316         image->matte=MagickTrue;
317         image->columns=(size_t) icon_file.directory[i].width;
318         if ((ssize_t) image->columns > icon_info.width)
319           image->columns=(size_t) icon_info.width;
320         image->rows=(size_t) icon_file.directory[i].height;
321         if ((ssize_t) image->rows > icon_info.height)
322           image->rows=(size_t) icon_info.height;
323         image->depth=icon_info.bits_per_pixel;
324         if (image->debug != MagickFalse)
325           {
326             (void) LogMagickEvent(CoderEvent,GetMagickModule(),
327               " scene    = %.20g",(double) i);
328             (void) LogMagickEvent(CoderEvent,GetMagickModule(),
329               "   size   = %.20g",(double) icon_info.size);
330             (void) LogMagickEvent(CoderEvent,GetMagickModule(),
331               "   width  = %.20g",(double) icon_file.directory[i].width);
332             (void) LogMagickEvent(CoderEvent,GetMagickModule(),
333               "   height = %.20g",(double) icon_file.directory[i].height);
334             (void) LogMagickEvent(CoderEvent,GetMagickModule(),
335               "   colors = %.20g",(double ) icon_info.number_colors);
336             (void) LogMagickEvent(CoderEvent,GetMagickModule(),
337               "   planes = %.20g",(double) icon_info.planes);
338             (void) LogMagickEvent(CoderEvent,GetMagickModule(),
339               "   bpp    = %.20g",(double) icon_info.bits_per_pixel);
340           }
341       if ((icon_info.number_colors != 0) || (icon_info.bits_per_pixel <= 16))
342         {
343           image->storage_class=PseudoClass;
344           image->colors=icon_info.number_colors;
345           if (image->colors == 0)
346             image->colors=one << icon_info.bits_per_pixel;
347         }
348       if (image->storage_class == PseudoClass)
349         {
350           register ssize_t
351             i;
352
353           unsigned char
354             *icon_colormap;
355
356           size_t
357             number_colors,
358             one;
359
360           /*
361             Read Icon raster colormap.
362           */
363           one=1;
364           number_colors=one << icon_info.bits_per_pixel;
365           if (AcquireImageColormap(image,number_colors) == MagickFalse)
366             ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
367           icon_colormap=(unsigned char *) AcquireQuantumMemory((size_t)
368             image->colors,4UL*sizeof(*icon_colormap));
369           if (icon_colormap == (unsigned char *) NULL)
370             ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
371           count=ReadBlob(image,(size_t) (4*image->colors),icon_colormap);
372           if (count != (ssize_t) (4*image->colors))
373             ThrowReaderException(CorruptImageError,
374               "InsufficientImageDataInFile");
375           p=icon_colormap;
376           for (i=0; i < (ssize_t) image->colors; i++)
377           {
378             image->colormap[i].blue=(Quantum) ScaleCharToQuantum(*p++);
379             image->colormap[i].green=(Quantum) ScaleCharToQuantum(*p++);
380             image->colormap[i].red=(Quantum) ScaleCharToQuantum(*p++);
381             p++;
382           }
383           icon_colormap=(unsigned char *) RelinquishMagickMemory(icon_colormap);
384         }
385         /*
386           Convert Icon raster image to pixel packets.
387         */
388         if ((image_info->ping != MagickFalse) &&
389             (image_info->number_scenes != 0))
390           if (image->scene >= (image_info->scene+image_info->number_scenes-1))
391             break;
392         bytes_per_line=(((image->columns*icon_info.bits_per_pixel)+31) &
393           ~31) >> 3;
394         scanline_pad=((((image->columns*icon_info.bits_per_pixel)+31) & ~31)-
395           (image->columns*icon_info.bits_per_pixel)) >> 3;
396         switch (icon_info.bits_per_pixel)
397         {
398           case 1:
399           {
400             /*
401               Convert bitmap scanline.
402             */
403             for (y=(ssize_t) image->rows-1; y >= 0; y--)
404             {
405               q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
406               if (q == (PixelPacket *) NULL)
407                 break;
408               indexes=GetAuthenticIndexQueue(image);
409               for (x=0; x < (ssize_t) (image->columns-7); x+=8)
410               {
411                 byte=(size_t) ReadBlobByte(image);
412                 for (bit=0; bit < 8; bit++)
413                   indexes[x+bit]=(IndexPacket)
414                     ((byte & (0x80 >> bit)) != 0 ? 0x01 : 0x00);
415               }
416               if ((image->columns % 8) != 0)
417                 {
418                   byte=(size_t) ReadBlobByte(image);
419                   for (bit=0; bit < (image->columns % 8); bit++)
420                     indexes[x+bit]=(IndexPacket)
421                       ((byte & (0x80 >> bit)) != 0 ? 0x01 : 0x00);
422                 }
423               for (x=0; x < (ssize_t) scanline_pad; x++)
424                 (void) ReadBlobByte(image);
425               if (SyncAuthenticPixels(image,exception) == MagickFalse)
426                 break;
427               if (image->previous == (Image *) NULL)
428                 {
429                   status=SetImageProgress(image,LoadImageTag,image->rows-y-1,
430                     image->rows);
431                   if (status == MagickFalse)
432                     break;
433                 }
434             }
435             break;
436           }
437           case 4:
438           {
439             /*
440               Read 4-bit Icon scanline.
441             */
442             for (y=(ssize_t) image->rows-1; y >= 0; y--)
443             {
444               q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
445               if (q == (PixelPacket *) NULL)
446                 break;
447               indexes=GetAuthenticIndexQueue(image);
448               for (x=0; x < ((ssize_t) image->columns-1); x+=2)
449               {
450                 byte=(size_t) ReadBlobByte(image);
451                 indexes[x]=(IndexPacket) ((byte >> 4) & 0xf);
452                 indexes[x+1]=(IndexPacket) ((byte) & 0xf);
453               }
454               if ((image->columns % 2) != 0)
455                 {
456                   byte=(size_t) ReadBlobByte(image);
457                   indexes[x]=(IndexPacket) ((byte >> 4) & 0xf);
458                 }
459               for (x=0; x < (ssize_t) scanline_pad; x++)
460                 (void) ReadBlobByte(image);
461               if (SyncAuthenticPixels(image,exception) == MagickFalse)
462                 break;
463               if (image->previous == (Image *) NULL)
464                 {
465                   status=SetImageProgress(image,LoadImageTag,image->rows-y-1,
466                     image->rows);
467                   if (status == MagickFalse)
468                     break;
469                 }
470             }
471             break;
472           }
473           case 8:
474           {
475             /*
476               Convert PseudoColor scanline.
477             */
478             for (y=(ssize_t) image->rows-1; y >= 0; y--)
479             {
480               q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
481               if (q == (PixelPacket *) NULL)
482                 break;
483               indexes=GetAuthenticIndexQueue(image);
484               for (x=0; x < (ssize_t) image->columns; x++)
485               {
486                 byte=(size_t) ReadBlobByte(image);
487                 indexes[x]=(IndexPacket) byte;
488               }
489               for (x=0; x < (ssize_t) scanline_pad; x++)
490                 (void) ReadBlobByte(image);
491               if (SyncAuthenticPixels(image,exception) == MagickFalse)
492                 break;
493               if (image->previous == (Image *) NULL)
494                 {
495                   status=SetImageProgress(image,LoadImageTag,image->rows-y-1,
496                     image->rows);
497                   if (status == MagickFalse)
498                     break;
499                 }
500             }
501             break;
502           }
503           case 16:
504           {
505             /*
506               Convert PseudoColor scanline.
507             */
508             for (y=(ssize_t) image->rows-1; y >= 0; y--)
509             {
510               q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
511               if (q == (PixelPacket *) NULL)
512                 break;
513               indexes=GetAuthenticIndexQueue(image);
514               for (x=0; x < (ssize_t) image->columns; x++)
515               {
516                 byte=(size_t) ReadBlobByte(image);
517                 byte|=(size_t) (ReadBlobByte(image) << 8);
518                 indexes[x]=(IndexPacket) byte;
519               }
520               for (x=0; x < (ssize_t) scanline_pad; x++)
521                 (void) ReadBlobByte(image);
522               if (SyncAuthenticPixels(image,exception) == MagickFalse)
523                 break;
524               if (image->previous == (Image *) NULL)
525                 {
526                   status=SetImageProgress(image,LoadImageTag,image->rows-y-1,
527                     image->rows);
528                   if (status == MagickFalse)
529                     break;
530                 }
531             }
532             break;
533           }
534           case 24:
535           case 32:
536           {
537             /*
538               Convert DirectColor scanline.
539             */
540             for (y=(ssize_t) image->rows-1; y >= 0; y--)
541             {
542               q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
543               if (q == (PixelPacket *) NULL)
544                 break;
545               for (x=0; x < (ssize_t) image->columns; x++)
546               {
547                 q->blue=ScaleCharToQuantum((unsigned char) ReadBlobByte(image));
548                 q->green=ScaleCharToQuantum((unsigned char)
549                   ReadBlobByte(image));
550                 q->red=ScaleCharToQuantum((unsigned char) ReadBlobByte(image));
551                 if (icon_info.bits_per_pixel == 32)
552                   q->opacity=(Quantum) (QuantumRange-ScaleCharToQuantum(
553                     (unsigned char) ReadBlobByte(image)));
554                 q++;
555               }
556               if (icon_info.bits_per_pixel == 24)
557                 for (x=0; x < (ssize_t) scanline_pad; x++)
558                   (void) ReadBlobByte(image);
559               if (SyncAuthenticPixels(image,exception) == MagickFalse)
560                 break;
561               if (image->previous == (Image *) NULL)
562                 {
563                   status=SetImageProgress(image,LoadImageTag,image->rows-y-1,
564                     image->rows);
565                   if (status == MagickFalse)
566                     break;
567                 }
568             }
569             break;
570           }
571           default:
572             ThrowReaderException(CorruptImageError,"ImproperImageHeader");
573         }
574         if (image_info->ping == MagickFalse)
575           (void) SyncImage(image);
576         if (icon_info.bits_per_pixel != 32)
577           {
578             /*
579               Read the ICON alpha mask.
580             */
581             image->storage_class=DirectClass;
582             for (y=(ssize_t) image->rows-1; y >= 0; y--)
583             {
584               q=GetAuthenticPixels(image,0,y,image->columns,1,exception);
585               if (q == (PixelPacket *) NULL)
586                 break;
587               for (x=0; x < ((ssize_t) image->columns-7); x+=8)
588               {
589                 byte=(size_t) ReadBlobByte(image);
590                 for (bit=0; bit < 8; bit++)
591                   q[x+bit].opacity=(Quantum) (((byte & (0x80 >> bit)) != 0) ?
592                     TransparentOpacity : OpaqueOpacity);
593               }
594               if ((image->columns % 8) != 0)
595                 {
596                   byte=(size_t) ReadBlobByte(image);
597                   for (bit=0; bit < (image->columns % 8); bit++)
598                     q[x+bit].opacity=(Quantum) (((byte & (0x80 >> bit)) != 0) ?
599                       TransparentOpacity : OpaqueOpacity);
600                 }
601               if ((image->columns % 32) != 0)
602                 for (x=0; x < (ssize_t) ((32-(image->columns % 32))/8); x++)
603                   (void) ReadBlobByte(image);
604               if (SyncAuthenticPixels(image,exception) == MagickFalse)
605                 break;
606             }
607           }
608         if (EOFBlob(image) != MagickFalse)
609           {
610             ThrowFileException(exception,CorruptImageError,
611               "UnexpectedEndOfFile",image->filename);
612             break;
613           }
614       }
615     /*
616       Proceed to next image.
617     */
618     if (image_info->number_scenes != 0)
619       if (image->scene >= (image_info->scene+image_info->number_scenes-1))
620         break;
621     if (i < (ssize_t) (icon_file.count-1))
622       {
623         /*
624           Allocate next image structure.
625         */
626         AcquireNextImage(image_info,image);
627         if (GetNextImageInList(image) == (Image *) NULL)
628           {
629             image=DestroyImageList(image);
630             return((Image *) NULL);
631           }
632         image=SyncNextImageInList(image);
633         status=SetImageProgress(image,LoadImagesTag,TellBlob(image),
634           GetBlobSize(image));
635         if (status == MagickFalse)
636           break;
637       }
638   }
639   (void) CloseBlob(image);
640   return(GetFirstImageInList(image));
641 }
642 \f
643 /*
644 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
645 %                                                                             %
646 %                                                                             %
647 %                                                                             %
648 %   R e g i s t e r I C O N I m a g e                                         %
649 %                                                                             %
650 %                                                                             %
651 %                                                                             %
652 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
653 %
654 %  RegisterICONImage() adds attributes for the Icon image format to
655 %  the list of supported formats.  The attributes include the image format
656 %  tag, a method to read and/or write the format, whether the format
657 %  supports the saving of more than one frame to the same file or blob,
658 %  whether the format supports native in-memory I/O, and a brief
659 %  description of the format.
660 %
661 %  The format of the RegisterICONImage method is:
662 %
663 %      size_t RegisterICONImage(void)
664 %
665 */
666 ModuleExport size_t RegisterICONImage(void)
667 {
668   MagickInfo
669     *entry;
670
671   entry=SetMagickInfo("CUR");
672   entry->decoder=(DecodeImageHandler *) ReadICONImage;
673   entry->encoder=(EncodeImageHandler *) WriteICONImage;
674   entry->adjoin=MagickFalse;
675   entry->seekable_stream=MagickTrue;
676   entry->description=ConstantString("Microsoft icon");
677   entry->module=ConstantString("CUR");
678   (void) RegisterMagickInfo(entry);
679   entry=SetMagickInfo("ICO");
680   entry->decoder=(DecodeImageHandler *) ReadICONImage;
681   entry->encoder=(EncodeImageHandler *) WriteICONImage;
682   entry->adjoin=MagickTrue;
683   entry->seekable_stream=MagickTrue;
684   entry->description=ConstantString("Microsoft icon");
685   entry->module=ConstantString("ICON");
686   (void) RegisterMagickInfo(entry);
687   entry=SetMagickInfo("ICON");
688   entry->decoder=(DecodeImageHandler *) ReadICONImage;
689   entry->encoder=(EncodeImageHandler *) WriteICONImage;
690   entry->adjoin=MagickFalse;
691   entry->seekable_stream=MagickTrue;
692   entry->description=ConstantString("Microsoft icon");
693   entry->module=ConstantString("ICON");
694   (void) RegisterMagickInfo(entry);
695   return(MagickImageCoderSignature);
696 }
697 \f
698 /*
699 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
700 %                                                                             %
701 %                                                                             %
702 %                                                                             %
703 %   U n r e g i s t e r I C O N I m a g e                                     %
704 %                                                                             %
705 %                                                                             %
706 %                                                                             %
707 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
708 %
709 %  UnregisterICONImage() removes format registrations made by the
710 %  ICON module from the list of supported formats.
711 %
712 %  The format of the UnregisterICONImage method is:
713 %
714 %      UnregisterICONImage(void)
715 %
716 */
717 ModuleExport void UnregisterICONImage(void)
718 {
719   (void) UnregisterMagickInfo("CUR");
720   (void) UnregisterMagickInfo("ICO");
721   (void) UnregisterMagickInfo("ICON");
722 }
723 \f
724 /*
725 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
726 %                                                                             %
727 %                                                                             %
728 %                                                                             %
729 %   W r i t e I C O N I m a g e                                               %
730 %                                                                             %
731 %                                                                             %
732 %                                                                             %
733 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
734 %
735 %  WriteICONImage() writes an image in Microsoft Windows bitmap encoded
736 %  image format, version 3 for Windows or (if the image has a matte channel)
737 %  version 4.
738 %
739 %  The format of the WriteICONImage method is:
740 %
741 %      MagickBooleanType WriteICONImage(const ImageInfo *image_info,
742 %        Image *image)
743 %
744 %  A description of each parameter follows.
745 %
746 %    o image_info: the image info.
747 %
748 %    o image:  The image.
749 %
750 */
751 static MagickBooleanType WriteICONImage(const ImageInfo *image_info,
752   Image *image)
753 {
754   IconFile
755     icon_file;
756
757   IconInfo
758     icon_info;
759
760   Image
761     *next;
762   
763   ssize_t
764     y;
765
766   MagickBooleanType
767     status;
768
769   MagickOffsetType
770     offset,
771     scene;
772
773   register const IndexPacket
774     *indexes;
775
776   register const PixelPacket
777     *p;
778
779   register ssize_t
780     i,
781     x;
782
783   register unsigned char
784     *q;
785
786   unsigned char
787     bit,
788     byte,
789     *pixels;
790
791   size_t
792     bytes_per_line,
793     scanline_pad;
794
795   /*
796     Open output image file.
797   */
798   assert(image_info != (const ImageInfo *) NULL);
799   assert(image_info->signature == MagickSignature);
800   assert(image != (Image *) NULL);
801   assert(image->signature == MagickSignature);
802     (void) LogMagickEvent(CoderEvent,GetMagickModule(),"%s",image->filename);
803   status=OpenBlob(image_info,image,WriteBinaryBlobMode,&image->exception);
804   if (status == MagickFalse)
805     return(status);
806   scene=0;
807   next=image;
808   do
809   {
810     if ((image->columns > 256L) || (image->rows > 256L))
811       ThrowWriterException(ImageError,"WidthOrHeightExceedsLimit");
812     scene++;
813     next=SyncNextImageInList(next);
814   } while ((next != (Image *) NULL) && (image_info->adjoin != MagickFalse));
815   /*
816     Dump out a ICON header template to be properly initialized later.
817   */
818   (void) WriteBlobLSBShort(image,0);
819   (void) WriteBlobLSBShort(image,1);
820   (void) WriteBlobLSBShort(image,(unsigned char) scene);
821   (void) ResetMagickMemory(&icon_file,0,sizeof(icon_file));
822   (void) ResetMagickMemory(&icon_info,0,sizeof(icon_info));
823   scene=0;
824   next=image;
825   do
826   {
827     (void) WriteBlobByte(image,icon_file.directory[scene].width);
828     (void) WriteBlobByte(image,icon_file.directory[scene].height);
829     (void) WriteBlobByte(image,icon_file.directory[scene].colors);
830     (void) WriteBlobByte(image,icon_file.directory[scene].reserved);
831     (void) WriteBlobLSBShort(image,icon_file.directory[scene].planes);
832     (void) WriteBlobLSBShort(image,icon_file.directory[scene].bits_per_pixel);
833     (void) WriteBlobLSBLong(image,(unsigned int)
834       icon_file.directory[scene].size);
835     (void) WriteBlobLSBLong(image,(unsigned int)
836       icon_file.directory[scene].offset);
837     scene++;
838     next=SyncNextImageInList(next);
839   } while ((next != (Image *) NULL) && (image_info->adjoin != MagickFalse));
840   scene=0;
841   next=image;
842   do
843   {
844     if ((next->columns == 256) && (next->rows == 256))
845       {
846         Image
847           *write_image;
848
849         ImageInfo
850           *write_info;
851
852         size_t
853           length;
854
855         unsigned char
856           *png;
857
858         /*
859           Icon image encoded as a compressed PNG image.
860         */
861         write_image=CloneImage(next,0,0,MagickTrue,&image->exception);
862         if (write_image == (Image *) NULL)
863           return(MagickFalse);
864         write_info=CloneImageInfo(image_info);
865         (void) CopyMagickString(write_info->filename,"PNG:",MaxTextExtent);
866         png=(unsigned char *) ImageToBlob(write_info,write_image,&length,
867           &image->exception);
868         write_image=DestroyImage(write_image);
869         write_info=DestroyImageInfo(write_info);
870         if (png == (unsigned char *) NULL)
871           return(MagickFalse);
872         icon_file.directory[scene].width=0;
873         icon_file.directory[scene].height=0;
874         icon_file.directory[scene].colors=0;
875         icon_file.directory[scene].reserved=0;
876         icon_file.directory[scene].planes=1;
877         icon_file.directory[scene].bits_per_pixel=32;
878         icon_file.directory[scene].size=(size_t) length;
879         icon_file.directory[scene].offset=(size_t) TellBlob(image);
880         (void) WriteBlob(image,(size_t) length,png);
881         png=(unsigned char *) RelinquishMagickMemory(png);
882       }
883     else
884       {
885         /*
886           Initialize ICON raster file header.
887         */
888         if (next->colorspace != RGBColorspace)
889           (void) TransformImageColorspace(next,RGBColorspace);
890         icon_info.file_size=14+12+28;
891         icon_info.offset_bits=icon_info.file_size;
892         icon_info.compression=BI_RGB;
893         if ((next->storage_class != DirectClass) && (next->colors > 256))
894           (void) SetImageStorageClass(next,DirectClass);
895         if (next->storage_class == DirectClass)
896           {
897             /*
898               Full color ICON raster.
899             */
900             icon_info.number_colors=0;
901             icon_info.bits_per_pixel=32;
902             icon_info.compression=(size_t) BI_RGB;
903           }
904         else
905           {
906             size_t
907               one;
908
909             /*
910               Colormapped ICON raster.
911             */
912             icon_info.bits_per_pixel=8;
913             if (next->colors <= 256)
914               icon_info.bits_per_pixel=8;
915             if (next->colors <= 16)
916               icon_info.bits_per_pixel=4;
917             if (next->colors <= 2)
918               icon_info.bits_per_pixel=1;
919             one=1;
920             icon_info.number_colors=one << icon_info.bits_per_pixel;
921             if (icon_info.number_colors < next->colors)
922               {
923                 (void) SetImageStorageClass(next,DirectClass);
924                 icon_info.number_colors=0;
925                 icon_info.bits_per_pixel=(unsigned short) 24;
926                 icon_info.compression=(size_t) BI_RGB;
927               }
928             else
929               {
930                 size_t
931                   one;
932
933                 one=1;
934                 icon_info.file_size+=3*(one << icon_info.bits_per_pixel);
935                 icon_info.offset_bits+=3*(one << icon_info.bits_per_pixel);
936                 icon_info.file_size+=(one << icon_info.bits_per_pixel);
937                 icon_info.offset_bits+=(one << icon_info.bits_per_pixel);
938               }
939           }
940         bytes_per_line=(((next->columns*icon_info.bits_per_pixel)+31) &
941           ~31) >> 3;
942         icon_info.ba_offset=0;
943         icon_info.width=(ssize_t) next->columns;
944         icon_info.height=(ssize_t) next->rows;
945         icon_info.planes=1;
946         icon_info.image_size=bytes_per_line*next->rows;
947         icon_info.size=40;
948         icon_info.size+=(4*icon_info.number_colors);
949         icon_info.size+=icon_info.image_size;
950         icon_info.size+=(((icon_info.width+31) & ~31) >> 3)*icon_info.height;
951         icon_info.file_size+=icon_info.image_size;
952         icon_info.x_pixels=0;
953         icon_info.y_pixels=0;
954         switch (next->units)
955         {
956           case UndefinedResolution:
957           case PixelsPerInchResolution:
958           {
959             icon_info.x_pixels=(size_t) (100.0*next->x_resolution/2.54);
960             icon_info.y_pixels=(size_t) (100.0*next->y_resolution/2.54);
961             break;
962           }
963           case PixelsPerCentimeterResolution:
964           {
965             icon_info.x_pixels=(size_t) (100.0*next->x_resolution);
966             icon_info.y_pixels=(size_t) (100.0*next->y_resolution);
967             break;
968           }
969         }
970         icon_info.colors_important=icon_info.number_colors;
971         /*
972           Convert MIFF to ICON raster pixels.
973         */
974         pixels=(unsigned char *) AcquireQuantumMemory((size_t)
975           icon_info.image_size,sizeof(*pixels));
976         if (pixels == (unsigned char *) NULL)
977           ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
978         (void) ResetMagickMemory(pixels,0,(size_t) icon_info.image_size);
979         switch (icon_info.bits_per_pixel)
980         {
981           case 1:
982           {
983             size_t
984               bit,
985               byte;
986
987             /*
988               Convert PseudoClass image to a ICON monochrome image.
989             */
990             for (y=0; y < (ssize_t) next->rows; y++)
991             {
992               p=GetVirtualPixels(next,0,y,next->columns,1,&next->exception);
993               if (p == (const PixelPacket *) NULL)
994                 break;
995               indexes=GetVirtualIndexQueue(next);
996               q=pixels+(next->rows-y-1)*bytes_per_line;
997               bit=0;
998               byte=0;
999               for (x=0; x < (ssize_t) next->columns; x++)
1000               {
1001                 byte<<=1;
1002                 byte|=indexes[x] != 0 ? 0x01 : 0x00;
1003                 bit++;
1004                 if (bit == 8)
1005                   {
1006                     *q++=(unsigned char) byte;
1007                     bit=0;
1008                     byte=0;
1009                   }
1010                }
1011               if (bit != 0)
1012                 *q++=(unsigned char) (byte << (8-bit));
1013               if (next->previous == (Image *) NULL)
1014                 {
1015                   status=SetImageProgress(next,SaveImageTag,y,next->rows);
1016                   if (status == MagickFalse)
1017                     break;
1018                 }
1019             }
1020             break;
1021           }
1022           case 4:
1023           {
1024             size_t
1025               nibble,
1026               byte;
1027
1028             /*
1029               Convert PseudoClass image to a ICON monochrome image.
1030             */
1031             for (y=0; y < (ssize_t) next->rows; y++)
1032             {
1033               p=GetVirtualPixels(next,0,y,next->columns,1,&next->exception);
1034               if (p == (const PixelPacket *) NULL)
1035                 break;
1036               indexes=GetVirtualIndexQueue(next);
1037               q=pixels+(next->rows-y-1)*bytes_per_line;
1038               nibble=0;
1039               byte=0;
1040               for (x=0; x < (ssize_t) next->columns; x++)
1041               {
1042                 byte<<=4;
1043                 byte|=((size_t) indexes[x] & 0x0f);
1044                 nibble++;
1045                 if (nibble == 2)
1046                   {
1047                     *q++=(unsigned char) byte;
1048                     nibble=0;
1049                     byte=0;
1050                   }
1051                }
1052               if (nibble != 0)
1053                 *q++=(unsigned char) (byte << 4);
1054               if (next->previous == (Image *) NULL)
1055                 {
1056                   status=SetImageProgress(next,SaveImageTag,y,next->rows);
1057                   if (status == MagickFalse)
1058                     break;
1059                 }
1060             }
1061             break;
1062           }
1063           case 8:
1064           {
1065             /*
1066               Convert PseudoClass packet to ICON pixel.
1067             */
1068             for (y=0; y < (ssize_t) next->rows; y++)
1069             {
1070               p=GetVirtualPixels(next,0,y,next->columns,1,&next->exception);
1071               if (p == (const PixelPacket *) NULL)
1072                 break;
1073               indexes=GetVirtualIndexQueue(next);
1074               q=pixels+(next->rows-y-1)*bytes_per_line;
1075               for (x=0; x < (ssize_t) next->columns; x++)
1076                 *q++=(unsigned char) indexes[x];
1077               if (next->previous == (Image *) NULL)
1078                 {
1079                   status=SetImageProgress(next,SaveImageTag,y,next->rows);
1080                   if (status == MagickFalse)
1081                     break;
1082                 }
1083             }
1084             break;
1085           }
1086           case 24:
1087           case 32:
1088           {
1089             /*
1090               Convert DirectClass packet to ICON BGR888 or BGRA8888 pixel.
1091             */
1092             for (y=0; y < (ssize_t) next->rows; y++)
1093             {
1094               p=GetVirtualPixels(next,0,y,next->columns,1,&next->exception);
1095               if (p == (const PixelPacket *) NULL)
1096                 break;
1097               q=pixels+(next->rows-y-1)*bytes_per_line;
1098               for (x=0; x < (ssize_t) next->columns; x++)
1099               {
1100                 *q++=ScaleQuantumToChar(GetBluePixelComponent(p));
1101                 *q++=ScaleQuantumToChar(GetGreenPixelComponent(p));
1102                 *q++=ScaleQuantumToChar(GetRedPixelComponent(p));
1103                 if (next->matte == MagickFalse)
1104                   *q++=ScaleQuantumToChar(QuantumRange);
1105                 else
1106                   *q++=ScaleQuantumToChar(GetAlphaPixelComponent(p));
1107                 p++;
1108               }
1109               if (icon_info.bits_per_pixel == 24)
1110                 for (x=3L*(ssize_t) next->columns; x < (ssize_t) bytes_per_line; x++)
1111                   *q++=0x00;
1112               if (next->previous == (Image *) NULL)
1113                 {
1114                   status=SetImageProgress(next,SaveImageTag,y,next->rows);
1115                   if (status == MagickFalse)
1116                     break;
1117                 }
1118             }
1119             break;
1120           }
1121         }
1122         /*
1123           Write 40-byte version 3+ bitmap header.
1124         */
1125         icon_file.directory[scene].width=(unsigned char) icon_info.width;
1126         icon_file.directory[scene].height=(unsigned char) icon_info.height;
1127         icon_file.directory[scene].colors=(unsigned char)
1128           icon_info.number_colors;
1129         icon_file.directory[scene].reserved=0;
1130         icon_file.directory[scene].planes=icon_info.planes;
1131         icon_file.directory[scene].bits_per_pixel=icon_info.bits_per_pixel;
1132         icon_file.directory[scene].size=icon_info.size;
1133         icon_file.directory[scene].offset=(size_t) TellBlob(image);
1134         (void) WriteBlobLSBLong(image,(unsigned int) 40);
1135         (void) WriteBlobLSBLong(image,(unsigned int) icon_info.width);
1136         (void) WriteBlobLSBLong(image,(unsigned int) icon_info.height*2);
1137         (void) WriteBlobLSBShort(image,icon_info.planes);
1138         (void) WriteBlobLSBShort(image,icon_info.bits_per_pixel);
1139         (void) WriteBlobLSBLong(image,(unsigned int) icon_info.compression);
1140         (void) WriteBlobLSBLong(image,(unsigned int) icon_info.image_size);
1141         (void) WriteBlobLSBLong(image,(unsigned int) icon_info.x_pixels);
1142         (void) WriteBlobLSBLong(image,(unsigned int) icon_info.y_pixels);
1143         (void) WriteBlobLSBLong(image,(unsigned int) icon_info.number_colors);
1144         (void) WriteBlobLSBLong(image,(unsigned int) icon_info.colors_important);
1145         if (next->storage_class == PseudoClass)
1146           {
1147             unsigned char
1148               *icon_colormap;
1149
1150             /*
1151               Dump colormap to file.
1152             */
1153             icon_colormap=(unsigned char *) AcquireQuantumMemory((size_t)
1154               (1UL << icon_info.bits_per_pixel),4UL*sizeof(*icon_colormap));
1155             if (icon_colormap == (unsigned char *) NULL)
1156               ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
1157             q=icon_colormap;
1158             for (i=0; i < (ssize_t) next->colors; i++)
1159             {
1160               *q++=ScaleQuantumToChar(next->colormap[i].blue);
1161               *q++=ScaleQuantumToChar(next->colormap[i].green);
1162               *q++=ScaleQuantumToChar(next->colormap[i].red);
1163               *q++=(unsigned char) 0x0;
1164             }
1165             for ( ; i < (ssize_t) (1UL << icon_info.bits_per_pixel); i++)
1166             {
1167               *q++=(unsigned char) 0x00;
1168               *q++=(unsigned char) 0x00;
1169               *q++=(unsigned char) 0x00;
1170               *q++=(unsigned char) 0x00;
1171             }
1172             (void) WriteBlob(image,(size_t) (4UL*(1UL <<
1173               icon_info.bits_per_pixel)),icon_colormap);
1174             icon_colormap=(unsigned char *) RelinquishMagickMemory(
1175               icon_colormap);
1176           }
1177         (void) WriteBlob(image,(size_t) icon_info.image_size,pixels);
1178         pixels=(unsigned char *) RelinquishMagickMemory(pixels);
1179         /*
1180           Write matte mask.
1181         */
1182         scanline_pad=(((next->columns+31) & ~31)-next->columns) >> 3;
1183         for (y=((ssize_t) next->rows - 1); y >= 0; y--)
1184         {
1185           p=GetVirtualPixels(next,0,y,next->columns,1,&next->exception);
1186           if (p == (const PixelPacket *) NULL)
1187             break;
1188           bit=0;
1189           byte=0;
1190           for (x=0; x < (ssize_t) next->columns; x++)
1191           {
1192             byte<<=1;
1193             if ((next->matte == MagickTrue) &&
1194                 (p->opacity == (Quantum) TransparentOpacity))
1195               byte|=0x01;
1196             bit++;
1197             if (bit == 8)
1198               {
1199                 (void) WriteBlobByte(image,(unsigned char) byte);
1200                 bit=0;
1201                 byte=0;
1202               }
1203             p++;
1204           }
1205           if (bit != 0)
1206             (void) WriteBlobByte(image,(unsigned char) (byte << (8-bit)));
1207           for (i=0; i < (ssize_t) scanline_pad; i++)
1208             (void) WriteBlobByte(image,(unsigned char) 0);
1209         }
1210       }
1211     if (GetNextImageInList(next) == (Image *) NULL)
1212       break;
1213     next=SyncNextImageInList(next);
1214     status=SetImageProgress(next,SaveImagesTag,scene++,
1215       GetImageListLength(next));
1216     if (status == MagickFalse)
1217       break;
1218   } while ((next != (Image *) NULL) && (image_info->adjoin != MagickFalse));
1219   offset=SeekBlob(image,0,SEEK_SET);
1220   (void) WriteBlobLSBShort(image,0);
1221   (void) WriteBlobLSBShort(image,1);
1222   (void) WriteBlobLSBShort(image,(unsigned short) (scene+1));
1223   scene=0;
1224   next=image;
1225   do
1226   {
1227     (void) WriteBlobByte(image,icon_file.directory[scene].width);
1228     (void) WriteBlobByte(image,icon_file.directory[scene].height);
1229     (void) WriteBlobByte(image,icon_file.directory[scene].colors);
1230     (void) WriteBlobByte(image,icon_file.directory[scene].reserved);
1231     (void) WriteBlobLSBShort(image,icon_file.directory[scene].planes);
1232     (void) WriteBlobLSBShort(image,icon_file.directory[scene].bits_per_pixel);
1233     (void) WriteBlobLSBLong(image,(unsigned int)
1234       icon_file.directory[scene].size);
1235     (void) WriteBlobLSBLong(image,(unsigned int)
1236       icon_file.directory[scene].offset);
1237     scene++;
1238     next=SyncNextImageInList(next);
1239   } while ((next != (Image *) NULL) && (image_info->adjoin != MagickFalse));
1240   (void) CloseBlob(image);
1241   return(MagickTrue);
1242 }