]> 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-2011 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 "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"
65 \f
66 /*
67   Define declarations.
68 */
69 #if !defined(MAGICKCORE_WINDOWS_SUPPORT) || defined(__MINGW32__)
70 #define BI_RGB  0
71 #define BI_RLE8  1
72 #define BI_BITFIELDS  3
73 #endif
74 #define MaxIcons  1024
75 \f
76 /*
77   Typedef declarations.
78 */
79 typedef 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
91   size_t
92     size,
93     offset;
94 } IconEntry;
95
96 typedef struct _IconFile
97 {
98   short
99     reserved,
100     resource_type,
101     count;
102
103   IconEntry
104     directory[MaxIcons];
105 } IconFile;
106
107 typedef struct _IconInfo
108 {
109   size_t
110     file_size,
111     ba_offset,
112     offset_bits,
113     size;
114
115   ssize_t
116     width,
117     height;
118
119   unsigned short
120     planes,
121     bits_per_pixel;
122
123   size_t
124     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
135   ssize_t
136     colorspace;
137 } IconInfo;
138 \f
139 /*
140   Forward declaractions.
141 */
142 static MagickBooleanType
143   WriteICONImage(const ImageInfo *,Image *);
144 \f
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 */
172 static 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
184   MagickBooleanType
185     status;
186
187   register ssize_t
188     i,
189     x;
190
191   register Quantum
192     *q;
193
194   register unsigned char
195     *p;
196
197   size_t
198     bit,
199     byte,
200     bytes_per_line,
201     one,
202     scanline_pad;
203
204   ssize_t
205     count,
206     offset,
207     y;
208
209   /*
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   }
243   one=1;
244   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);
254     icon_info.width=(unsigned char) ((int) ReadBlobLSBLong(image));
255     icon_info.height=(unsigned char) ((int) ReadBlobLSBLong(image)/2);
256     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);
279         count=ReadBlob(image,length-12,png+12);
280         if (count != (ssize_t) (length-12))
281           {
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;
313         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;
319         image->depth=icon_info.bits_per_pixel;
320         if (image->debug != MagickFalse)
321           {
322             (void) LogMagickEvent(CoderEvent,GetMagickModule(),
323               " scene    = %.20g",(double) i);
324             (void) LogMagickEvent(CoderEvent,GetMagickModule(),
325               "   size   = %.20g",(double) icon_info.size);
326             (void) LogMagickEvent(CoderEvent,GetMagickModule(),
327               "   width  = %.20g",(double) icon_file.directory[i].width);
328             (void) LogMagickEvent(CoderEvent,GetMagickModule(),
329               "   height = %.20g",(double) icon_file.directory[i].height);
330             (void) LogMagickEvent(CoderEvent,GetMagickModule(),
331               "   colors = %.20g",(double ) icon_info.number_colors);
332             (void) LogMagickEvent(CoderEvent,GetMagickModule(),
333               "   planes = %.20g",(double) icon_info.planes);
334             (void) LogMagickEvent(CoderEvent,GetMagickModule(),
335               "   bpp    = %.20g",(double) icon_info.bits_per_pixel);
336           }
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)
342             image->colors=one << icon_info.bits_per_pixel;
343         }
344       if (image->storage_class == PseudoClass)
345         {
346           register ssize_t
347             i;
348
349           size_t
350             number_colors,
351             one;
352
353           unsigned char
354             *icon_colormap;
355
356           /*
357             Read Icon raster colormap.
358           */
359           one=1;
360           number_colors=one << icon_info.bits_per_pixel;
361           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;
372           for (i=0; i < (ssize_t) image->colors; i++)
373           {
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;
390         (void) bytes_per_line;
391         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             */
400             for (y=(ssize_t) image->rows-1; y >= 0; y--)
401             {
402               q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
403               if (q == (const Quantum *) NULL)
404                 break;
405               for (x=0; x < (ssize_t) (image->columns-7); x+=8)
406               {
407                 byte=(size_t) ReadBlobByte(image);
408                 for (bit=0; bit < 8; bit++)
409                 {
410                   SetPixelIndex(image,((byte & (0x80 >> bit)) != 0 ? 0x01 :
411                     0x00),q);
412                   q+=GetPixelChannels(image);
413                 }
414               }
415               if ((image->columns % 8) != 0)
416                 {
417                   byte=(size_t) ReadBlobByte(image);
418                   for (bit=0; bit < (image->columns % 8); bit++)
419                   {
420                     SetPixelIndex(image,((byte & (0x80 >> bit)) != 0 ? 0x01 :
421                       0x00),q);
422                     q+=GetPixelChannels(image);
423                   }
424                 }
425               for (x=0; x < (ssize_t) scanline_pad; x++)
426                 (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             */
444             for (y=(ssize_t) image->rows-1; y >= 0; y--)
445             {
446               q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
447               if (q == (const Quantum *) NULL)
448                 break;
449               for (x=0; x < ((ssize_t) image->columns-1); x+=2)
450               {
451                 byte=(size_t) ReadBlobByte(image);
452                 SetPixelIndex(image,((byte >> 4) & 0xf),q);
453                 q+=GetPixelChannels(image);
454                 SetPixelIndex(image,((byte) & 0xf),q);
455                 q+=GetPixelChannels(image);
456               }
457               if ((image->columns % 2) != 0)
458                 {
459                   byte=(size_t) ReadBlobByte(image);
460                   SetPixelIndex(image,((byte >> 4) & 0xf),q);
461                   q+=GetPixelChannels(image);
462                 }
463               for (x=0; x < (ssize_t) scanline_pad; x++)
464                 (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             */
482             for (y=(ssize_t) image->rows-1; y >= 0; y--)
483             {
484               q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
485               if (q == (const Quantum *) NULL)
486                 break;
487               for (x=0; x < (ssize_t) image->columns; x++)
488               {
489                 byte=(size_t) ReadBlobByte(image);
490                 SetPixelIndex(image,byte,q);
491                 q+=GetPixelChannels(image);
492               }
493               for (x=0; x < (ssize_t) scanline_pad; x++)
494                 (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             */
512             for (y=(ssize_t) image->rows-1; y >= 0; y--)
513             {
514               q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
515               if (q == (const Quantum *) NULL)
516                 break;
517               for (x=0; x < (ssize_t) image->columns; x++)
518               {
519                 byte=(size_t) ReadBlobByte(image);
520                 byte|=(size_t) (ReadBlobByte(image) << 8);
521                 SetPixelIndex(image,byte,q);
522                 q+=GetPixelChannels(image);
523               }
524               for (x=0; x < (ssize_t) scanline_pad; x++)
525                 (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             */
544             for (y=(ssize_t) image->rows-1; y >= 0; y--)
545             {
546               q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
547               if (q == (const Quantum *) NULL)
548                 break;
549               for (x=0; x < (ssize_t) image->columns; x++)
550               {
551                 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);
557                 if (icon_info.bits_per_pixel == 32)
558                   SetPixelAlpha(image,ScaleCharToQuantum((unsigned char)
559                     ReadBlobByte(image)),q);
560                 q+=GetPixelChannels(image);
561               }
562               if (icon_info.bits_per_pixel == 24)
563                 for (x=0; x < (ssize_t) scanline_pad; x++)
564                   (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         }
580         if (image_info->ping == MagickFalse)
581           (void) SyncImage(image);
582         if (icon_info.bits_per_pixel != 32)
583           {
584             /*
585               Read the ICON alpha mask.
586             */
587             image->storage_class=DirectClass;
588             for (y=(ssize_t) image->rows-1; y >= 0; y--)
589             {
590               q=GetAuthenticPixels(image,0,y,image->columns,1,exception);
591               if (q == (const Quantum *) NULL)
592                 break;
593               for (x=0; x < ((ssize_t) image->columns-7); x+=8)
594               {
595                 byte=(size_t) ReadBlobByte(image);
596                 for (bit=0; bit < 8; bit++)
597                 {
598                   SetPixelAlpha(image,(((byte & (0x80 >> bit)) != 0) ?
599                     TransparentAlpha : OpaqueAlpha),q);
600                   q+=GetPixelChannels(image);
601                 }
602               }
603               if ((image->columns % 8) != 0)
604                 {
605                   byte=(size_t) ReadBlobByte(image);
606                   for (bit=0; bit < (image->columns % 8); bit++)
607                   {
608                     SetPixelAlpha(image,(((byte & (0x80 >> bit)) != 0) ?
609                       TransparentAlpha : OpaqueAlpha),q);
610                     q+=GetPixelChannels(image);
611                   }
612                 }
613               if ((image->columns % 32) != 0)
614                 for (x=0; x < (ssize_t) ((32-(image->columns % 32))/8); x++)
615                   (void) ReadBlobByte(image);
616               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;
633     if (i < (ssize_t) (icon_file.count-1))
634       {
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 \f
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 %
675 %      size_t RegisterICONImage(void)
676 %
677 */
678 ModuleExport size_t RegisterICONImage(void)
679 {
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 \f
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 */
729 ModuleExport void UnregisterICONImage(void)
730 {
731   (void) UnregisterMagickInfo("CUR");
732   (void) UnregisterMagickInfo("ICO");
733   (void) UnregisterMagickInfo("ICON");
734 }
735 \f
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,
754 %        Image *image)
755 %
756 %  A description of each parameter follows.
757 %
758 %    o image_info: the image info.
759 %
760 %    o image:  The image.
761 %
762 */
763 static MagickBooleanType WriteICONImage(const ImageInfo *image_info,
764   Image *image)
765 {
766   IconFile
767     icon_file;
768
769   IconInfo
770     icon_info;
771
772   Image
773     *next;
774   
775   MagickBooleanType
776     status;
777
778   MagickOffsetType
779     offset,
780     scene;
781
782   register const Quantum
783     *p;
784
785   register ssize_t
786     i,
787     x;
788
789   register unsigned char
790     *q;
791
792   size_t
793     bytes_per_line,
794     scanline_pad;
795
796   ssize_t
797     y;
798
799   unsigned char
800     bit,
801     byte,
802     *pixels;
803
804   /*
805     Open output image file.
806   */
807   assert(image_info != (const ImageInfo *) NULL);
808   assert(image_info->signature == MagickSignature);
809   assert(image != (Image *) NULL);
810   assert(image->signature == MagickSignature);
811     (void) LogMagickEvent(CoderEvent,GetMagickModule(),"%s",image->filename);
812   status=OpenBlob(image_info,image,WriteBinaryBlobMode,&image->exception);
813   if (status == MagickFalse)
814     return(status);
815   scene=0;
816   next=image;
817   do
818   {
819     if ((image->columns > 256L) || (image->rows > 256L))
820       ThrowWriterException(ImageError,"WidthOrHeightExceedsLimit");
821     scene++;
822     next=SyncNextImageInList(next);
823   } while ((next != (Image *) NULL) && (image_info->adjoin != MagickFalse));
824   /*
825     Dump out a ICON header template to be properly initialized later.
826   */
827   (void) WriteBlobLSBShort(image,0);
828   (void) WriteBlobLSBShort(image,1);
829   (void) WriteBlobLSBShort(image,(unsigned char) scene);
830   (void) ResetMagickMemory(&icon_file,0,sizeof(icon_file));
831   (void) ResetMagickMemory(&icon_info,0,sizeof(icon_info));
832   scene=0;
833   next=image;
834   do
835   {
836     (void) WriteBlobByte(image,icon_file.directory[scene].width);
837     (void) WriteBlobByte(image,icon_file.directory[scene].height);
838     (void) WriteBlobByte(image,icon_file.directory[scene].colors);
839     (void) WriteBlobByte(image,icon_file.directory[scene].reserved);
840     (void) WriteBlobLSBShort(image,icon_file.directory[scene].planes);
841     (void) WriteBlobLSBShort(image,icon_file.directory[scene].bits_per_pixel);
842     (void) WriteBlobLSBLong(image,(unsigned int)
843       icon_file.directory[scene].size);
844     (void) WriteBlobLSBLong(image,(unsigned int)
845       icon_file.directory[scene].offset);
846     scene++;
847     next=SyncNextImageInList(next);
848   } while ((next != (Image *) NULL) && (image_info->adjoin != MagickFalse));
849   scene=0;
850   next=image;
851   do
852   {
853     if ((next->columns == 256) && (next->rows == 256))
854       {
855         Image
856           *write_image;
857
858         ImageInfo
859           *write_info;
860
861         size_t
862           length;
863
864         unsigned char
865           *png;
866
867         /*
868           Icon image encoded as a compressed PNG image.
869         */
870         write_image=CloneImage(next,0,0,MagickTrue,&image->exception);
871         if (write_image == (Image *) NULL)
872           return(MagickFalse);
873         write_info=CloneImageInfo(image_info);
874         (void) CopyMagickString(write_info->filename,"PNG:",MaxTextExtent);
875         png=(unsigned char *) ImageToBlob(write_info,write_image,&length,
876           &image->exception);
877         write_image=DestroyImage(write_image);
878         write_info=DestroyImageInfo(write_info);
879         if (png == (unsigned char *) NULL)
880           return(MagickFalse);
881         icon_file.directory[scene].width=0;
882         icon_file.directory[scene].height=0;
883         icon_file.directory[scene].colors=0;
884         icon_file.directory[scene].reserved=0;
885         icon_file.directory[scene].planes=1;
886         icon_file.directory[scene].bits_per_pixel=32;
887         icon_file.directory[scene].size=(size_t) length;
888         icon_file.directory[scene].offset=(size_t) TellBlob(image);
889         (void) WriteBlob(image,(size_t) length,png);
890         png=(unsigned char *) RelinquishMagickMemory(png);
891       }
892     else
893       {
894         /*
895           Initialize ICON raster file header.
896         */
897         if (next->colorspace != RGBColorspace)
898           (void) TransformImageColorspace(next,RGBColorspace);
899         icon_info.file_size=14+12+28;
900         icon_info.offset_bits=icon_info.file_size;
901         icon_info.compression=BI_RGB;
902         if ((next->storage_class != DirectClass) && (next->colors > 256))
903           (void) SetImageStorageClass(next,DirectClass);
904         if (next->storage_class == DirectClass)
905           {
906             /*
907               Full color ICON raster.
908             */
909             icon_info.number_colors=0;
910             icon_info.bits_per_pixel=32;
911             icon_info.compression=(size_t) BI_RGB;
912           }
913         else
914           {
915             size_t
916               one;
917
918             /*
919               Colormapped ICON raster.
920             */
921             icon_info.bits_per_pixel=8;
922             if (next->colors <= 256)
923               icon_info.bits_per_pixel=8;
924             if (next->colors <= 16)
925               icon_info.bits_per_pixel=4;
926             if (next->colors <= 2)
927               icon_info.bits_per_pixel=1;
928             one=1;
929             icon_info.number_colors=one << icon_info.bits_per_pixel;
930             if (icon_info.number_colors < next->colors)
931               {
932                 (void) SetImageStorageClass(next,DirectClass);
933                 icon_info.number_colors=0;
934                 icon_info.bits_per_pixel=(unsigned short) 24;
935                 icon_info.compression=(size_t) BI_RGB;
936               }
937             else
938               {
939                 size_t
940                   one;
941
942                 one=1;
943                 icon_info.file_size+=3*(one << icon_info.bits_per_pixel);
944                 icon_info.offset_bits+=3*(one << icon_info.bits_per_pixel);
945                 icon_info.file_size+=(one << icon_info.bits_per_pixel);
946                 icon_info.offset_bits+=(one << icon_info.bits_per_pixel);
947               }
948           }
949         bytes_per_line=(((next->columns*icon_info.bits_per_pixel)+31) &
950           ~31) >> 3;
951         icon_info.ba_offset=0;
952         icon_info.width=(ssize_t) next->columns;
953         icon_info.height=(ssize_t) next->rows;
954         icon_info.planes=1;
955         icon_info.image_size=bytes_per_line*next->rows;
956         icon_info.size=40;
957         icon_info.size+=(4*icon_info.number_colors);
958         icon_info.size+=icon_info.image_size;
959         icon_info.size+=(((icon_info.width+31) & ~31) >> 3)*icon_info.height;
960         icon_info.file_size+=icon_info.image_size;
961         icon_info.x_pixels=0;
962         icon_info.y_pixels=0;
963         switch (next->units)
964         {
965           case UndefinedResolution:
966           case PixelsPerInchResolution:
967           {
968             icon_info.x_pixels=(size_t) (100.0*next->x_resolution/2.54);
969             icon_info.y_pixels=(size_t) (100.0*next->y_resolution/2.54);
970             break;
971           }
972           case PixelsPerCentimeterResolution:
973           {
974             icon_info.x_pixels=(size_t) (100.0*next->x_resolution);
975             icon_info.y_pixels=(size_t) (100.0*next->y_resolution);
976             break;
977           }
978         }
979         icon_info.colors_important=icon_info.number_colors;
980         /*
981           Convert MIFF to ICON raster pixels.
982         */
983         pixels=(unsigned char *) AcquireQuantumMemory((size_t)
984           icon_info.image_size,sizeof(*pixels));
985         if (pixels == (unsigned char *) NULL)
986           ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
987         (void) ResetMagickMemory(pixels,0,(size_t) icon_info.image_size);
988         switch (icon_info.bits_per_pixel)
989         {
990           case 1:
991           {
992             size_t
993               bit,
994               byte;
995
996             /*
997               Convert PseudoClass image to a ICON monochrome image.
998             */
999             for (y=0; y < (ssize_t) next->rows; y++)
1000             {
1001               p=GetVirtualPixels(next,0,y,next->columns,1,&next->exception);
1002               if (p == (const Quantum *) NULL)
1003                 break;
1004               q=pixels+(next->rows-y-1)*bytes_per_line;
1005               bit=0;
1006               byte=0;
1007               for (x=0; x < (ssize_t) next->columns; x++)
1008               {
1009                 byte<<=1;
1010                 byte|=GetPixelIndex(next,p) != 0 ? 0x01 : 0x00;
1011                 bit++;
1012                 if (bit == 8)
1013                   {
1014                     *q++=(unsigned char) byte;
1015                     bit=0;
1016                     byte=0;
1017                   }
1018                 p+=GetPixelChannels(image);
1019               }
1020               if (bit != 0)
1021                 *q++=(unsigned char) (byte << (8-bit));
1022               if (next->previous == (Image *) NULL)
1023                 {
1024                   status=SetImageProgress(next,SaveImageTag,y,next->rows);
1025                   if (status == MagickFalse)
1026                     break;
1027                 }
1028             }
1029             break;
1030           }
1031           case 4:
1032           {
1033             size_t
1034               nibble,
1035               byte;
1036
1037             /*
1038               Convert PseudoClass image to a ICON monochrome image.
1039             */
1040             for (y=0; y < (ssize_t) next->rows; y++)
1041             {
1042               p=GetVirtualPixels(next,0,y,next->columns,1,&next->exception);
1043               if (p == (const Quantum *) NULL)
1044                 break;
1045               q=pixels+(next->rows-y-1)*bytes_per_line;
1046               nibble=0;
1047               byte=0;
1048               for (x=0; x < (ssize_t) next->columns; x++)
1049               {
1050                 byte<<=4;
1051                 byte|=((size_t) GetPixelIndex(next,p) & 0x0f);
1052                 nibble++;
1053                 if (nibble == 2)
1054                   {
1055                     *q++=(unsigned char) byte;
1056                     nibble=0;
1057                     byte=0;
1058                   }
1059                 p+=GetPixelChannels(image);
1060               }
1061               if (nibble != 0)
1062                 *q++=(unsigned char) (byte << 4);
1063               if (next->previous == (Image *) NULL)
1064                 {
1065                   status=SetImageProgress(next,SaveImageTag,y,next->rows);
1066                   if (status == MagickFalse)
1067                     break;
1068                 }
1069             }
1070             break;
1071           }
1072           case 8:
1073           {
1074             /*
1075               Convert PseudoClass packet to ICON pixel.
1076             */
1077             for (y=0; y < (ssize_t) next->rows; y++)
1078             {
1079               p=GetVirtualPixels(next,0,y,next->columns,1,&next->exception);
1080               if (p == (const Quantum *) NULL)
1081                 break;
1082               q=pixels+(next->rows-y-1)*bytes_per_line;
1083               for (x=0; x < (ssize_t) next->columns; x++)
1084               {
1085                 *q++=(unsigned char) GetPixelIndex(next,p);
1086                 p+=GetPixelChannels(image);
1087               }
1088               if (next->previous == (Image *) NULL)
1089                 {
1090                   status=SetImageProgress(next,SaveImageTag,y,next->rows);
1091                   if (status == MagickFalse)
1092                     break;
1093                 }
1094             }
1095             break;
1096           }
1097           case 24:
1098           case 32:
1099           {
1100             /*
1101               Convert DirectClass packet to ICON BGR888 or BGRA8888 pixel.
1102             */
1103             for (y=0; y < (ssize_t) next->rows; y++)
1104             {
1105               p=GetVirtualPixels(next,0,y,next->columns,1,&next->exception);
1106               if (p == (const Quantum *) NULL)
1107                 break;
1108               q=pixels+(next->rows-y-1)*bytes_per_line;
1109               for (x=0; x < (ssize_t) next->columns; x++)
1110               {
1111                 *q++=ScaleQuantumToChar(GetPixelBlue(next,p));
1112                 *q++=ScaleQuantumToChar(GetPixelGreen(next,p));
1113                 *q++=ScaleQuantumToChar(GetPixelRed(next,p));
1114                 if (next->matte == MagickFalse)
1115                   *q++=ScaleQuantumToChar(QuantumRange);
1116                 else
1117                   *q++=ScaleQuantumToChar(GetPixelAlpha(next,p));
1118                 p+=GetPixelChannels(next);
1119               }
1120               if (icon_info.bits_per_pixel == 24)
1121                 for (x=3L*(ssize_t) next->columns; x < (ssize_t) bytes_per_line; x++)
1122                   *q++=0x00;
1123               if (next->previous == (Image *) NULL)
1124                 {
1125                   status=SetImageProgress(next,SaveImageTag,y,next->rows);
1126                   if (status == MagickFalse)
1127                     break;
1128                 }
1129             }
1130             break;
1131           }
1132         }
1133         /*
1134           Write 40-byte version 3+ bitmap header.
1135         */
1136         icon_file.directory[scene].width=(unsigned char) icon_info.width;
1137         icon_file.directory[scene].height=(unsigned char) icon_info.height;
1138         icon_file.directory[scene].colors=(unsigned char)
1139           icon_info.number_colors;
1140         icon_file.directory[scene].reserved=0;
1141         icon_file.directory[scene].planes=icon_info.planes;
1142         icon_file.directory[scene].bits_per_pixel=icon_info.bits_per_pixel;
1143         icon_file.directory[scene].size=icon_info.size;
1144         icon_file.directory[scene].offset=(size_t) TellBlob(image);
1145         (void) WriteBlobLSBLong(image,(unsigned int) 40);
1146         (void) WriteBlobLSBLong(image,(unsigned int) icon_info.width);
1147         (void) WriteBlobLSBLong(image,(unsigned int) icon_info.height*2);
1148         (void) WriteBlobLSBShort(image,icon_info.planes);
1149         (void) WriteBlobLSBShort(image,icon_info.bits_per_pixel);
1150         (void) WriteBlobLSBLong(image,(unsigned int) icon_info.compression);
1151         (void) WriteBlobLSBLong(image,(unsigned int) icon_info.image_size);
1152         (void) WriteBlobLSBLong(image,(unsigned int) icon_info.x_pixels);
1153         (void) WriteBlobLSBLong(image,(unsigned int) icon_info.y_pixels);
1154         (void) WriteBlobLSBLong(image,(unsigned int) icon_info.number_colors);
1155         (void) WriteBlobLSBLong(image,(unsigned int) icon_info.colors_important);
1156         if (next->storage_class == PseudoClass)
1157           {
1158             unsigned char
1159               *icon_colormap;
1160
1161             /*
1162               Dump colormap to file.
1163             */
1164             icon_colormap=(unsigned char *) AcquireQuantumMemory((size_t)
1165               (1UL << icon_info.bits_per_pixel),4UL*sizeof(*icon_colormap));
1166             if (icon_colormap == (unsigned char *) NULL)
1167               ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
1168             q=icon_colormap;
1169             for (i=0; i < (ssize_t) next->colors; i++)
1170             {
1171               *q++=ScaleQuantumToChar(next->colormap[i].blue);
1172               *q++=ScaleQuantumToChar(next->colormap[i].green);
1173               *q++=ScaleQuantumToChar(next->colormap[i].red);
1174               *q++=(unsigned char) 0x0;
1175             }
1176             for ( ; i < (ssize_t) (1UL << icon_info.bits_per_pixel); i++)
1177             {
1178               *q++=(unsigned char) 0x00;
1179               *q++=(unsigned char) 0x00;
1180               *q++=(unsigned char) 0x00;
1181               *q++=(unsigned char) 0x00;
1182             }
1183             (void) WriteBlob(image,(size_t) (4UL*(1UL <<
1184               icon_info.bits_per_pixel)),icon_colormap);
1185             icon_colormap=(unsigned char *) RelinquishMagickMemory(
1186               icon_colormap);
1187           }
1188         (void) WriteBlob(image,(size_t) icon_info.image_size,pixels);
1189         pixels=(unsigned char *) RelinquishMagickMemory(pixels);
1190         /*
1191           Write matte mask.
1192         */
1193         scanline_pad=(((next->columns+31) & ~31)-next->columns) >> 3;
1194         for (y=((ssize_t) next->rows - 1); y >= 0; y--)
1195         {
1196           p=GetVirtualPixels(next,0,y,next->columns,1,&next->exception);
1197           if (p == (const Quantum *) NULL)
1198             break;
1199           bit=0;
1200           byte=0;
1201           for (x=0; x < (ssize_t) next->columns; x++)
1202           {
1203             byte<<=1;
1204             if ((next->matte == MagickTrue) &&
1205                 (GetPixelAlpha(next,p) == (Quantum) TransparentAlpha))
1206               byte|=0x01;
1207             bit++;
1208             if (bit == 8)
1209               {
1210                 (void) WriteBlobByte(image,(unsigned char) byte);
1211                 bit=0;
1212                 byte=0;
1213               }
1214             p+=GetPixelChannels(next);
1215           }
1216           if (bit != 0)
1217             (void) WriteBlobByte(image,(unsigned char) (byte << (8-bit)));
1218           for (i=0; i < (ssize_t) scanline_pad; i++)
1219             (void) WriteBlobByte(image,(unsigned char) 0);
1220         }
1221       }
1222     if (GetNextImageInList(next) == (Image *) NULL)
1223       break;
1224     next=SyncNextImageInList(next);
1225     status=SetImageProgress(next,SaveImagesTag,scene++,
1226       GetImageListLength(next));
1227     if (status == MagickFalse)
1228       break;
1229   } while ((next != (Image *) NULL) && (image_info->adjoin != MagickFalse));
1230   offset=SeekBlob(image,0,SEEK_SET);
1231   (void) offset;
1232   (void) WriteBlobLSBShort(image,0);
1233   (void) WriteBlobLSBShort(image,1);
1234   (void) WriteBlobLSBShort(image,(unsigned short) (scene+1));
1235   scene=0;
1236   next=image;
1237   do
1238   {
1239     (void) WriteBlobByte(image,icon_file.directory[scene].width);
1240     (void) WriteBlobByte(image,icon_file.directory[scene].height);
1241     (void) WriteBlobByte(image,icon_file.directory[scene].colors);
1242     (void) WriteBlobByte(image,icon_file.directory[scene].reserved);
1243     (void) WriteBlobLSBShort(image,icon_file.directory[scene].planes);
1244     (void) WriteBlobLSBShort(image,icon_file.directory[scene].bits_per_pixel);
1245     (void) WriteBlobLSBLong(image,(unsigned int)
1246       icon_file.directory[scene].size);
1247     (void) WriteBlobLSBLong(image,(unsigned int)
1248       icon_file.directory[scene].offset);
1249     scene++;
1250     next=SyncNextImageInList(next);
1251   } while ((next != (Image *) NULL) && (image_info->adjoin != MagickFalse));
1252   (void) CloseBlob(image);
1253   return(MagickTrue);
1254 }