]> 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         (void) SyncImage(image);
575         if (icon_info.bits_per_pixel != 32)
576           {
577             /*
578               Read the ICON alpha mask.
579             */
580             image->storage_class=DirectClass;
581             for (y=(ssize_t) image->rows-1; y >= 0; y--)
582             {
583               q=GetAuthenticPixels(image,0,y,image->columns,1,exception);
584               if (q == (PixelPacket *) NULL)
585                 break;
586               for (x=0; x < ((ssize_t) image->columns-7); x+=8)
587               {
588                 byte=(size_t) ReadBlobByte(image);
589                 for (bit=0; bit < 8; bit++)
590                   q[x+bit].opacity=(Quantum) (((byte & (0x80 >> bit)) != 0) ?
591                     TransparentOpacity : OpaqueOpacity);
592               }
593               if ((image->columns % 8) != 0)
594                 {
595                   byte=(size_t) ReadBlobByte(image);
596                   for (bit=0; bit < (image->columns % 8); bit++)
597                     q[x+bit].opacity=(Quantum) (((byte & (0x80 >> bit)) != 0) ?
598                       TransparentOpacity : OpaqueOpacity);
599                 }
600               if ((image->columns % 32) != 0)
601                 for (x=0; x < (ssize_t) ((32-(image->columns % 32))/8); x++)
602                   (void) ReadBlobByte(image);
603               if (SyncAuthenticPixels(image,exception) == MagickFalse)
604                 break;
605             }
606           }
607         if (EOFBlob(image) != MagickFalse)
608           {
609             ThrowFileException(exception,CorruptImageError,
610               "UnexpectedEndOfFile",image->filename);
611             break;
612           }
613       }
614     /*
615       Proceed to next image.
616     */
617     if (image_info->number_scenes != 0)
618       if (image->scene >= (image_info->scene+image_info->number_scenes-1))
619         break;
620     if (i < (ssize_t) (icon_file.count-1))
621       {
622         /*
623           Allocate next image structure.
624         */
625         AcquireNextImage(image_info,image);
626         if (GetNextImageInList(image) == (Image *) NULL)
627           {
628             image=DestroyImageList(image);
629             return((Image *) NULL);
630           }
631         image=SyncNextImageInList(image);
632         status=SetImageProgress(image,LoadImagesTag,TellBlob(image),
633           GetBlobSize(image));
634         if (status == MagickFalse)
635           break;
636       }
637   }
638   (void) CloseBlob(image);
639   return(GetFirstImageInList(image));
640 }
641 \f
642 /*
643 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
644 %                                                                             %
645 %                                                                             %
646 %                                                                             %
647 %   R e g i s t e r I C O N I m a g e                                         %
648 %                                                                             %
649 %                                                                             %
650 %                                                                             %
651 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
652 %
653 %  RegisterICONImage() adds attributes for the Icon image format to
654 %  the list of supported formats.  The attributes include the image format
655 %  tag, a method to read and/or write the format, whether the format
656 %  supports the saving of more than one frame to the same file or blob,
657 %  whether the format supports native in-memory I/O, and a brief
658 %  description of the format.
659 %
660 %  The format of the RegisterICONImage method is:
661 %
662 %      size_t RegisterICONImage(void)
663 %
664 */
665 ModuleExport size_t RegisterICONImage(void)
666 {
667   MagickInfo
668     *entry;
669
670   entry=SetMagickInfo("CUR");
671   entry->decoder=(DecodeImageHandler *) ReadICONImage;
672   entry->encoder=(EncodeImageHandler *) WriteICONImage;
673   entry->adjoin=MagickFalse;
674   entry->seekable_stream=MagickTrue;
675   entry->description=ConstantString("Microsoft icon");
676   entry->module=ConstantString("CUR");
677   (void) RegisterMagickInfo(entry);
678   entry=SetMagickInfo("ICO");
679   entry->decoder=(DecodeImageHandler *) ReadICONImage;
680   entry->encoder=(EncodeImageHandler *) WriteICONImage;
681   entry->adjoin=MagickTrue;
682   entry->seekable_stream=MagickTrue;
683   entry->description=ConstantString("Microsoft icon");
684   entry->module=ConstantString("ICON");
685   (void) RegisterMagickInfo(entry);
686   entry=SetMagickInfo("ICON");
687   entry->decoder=(DecodeImageHandler *) ReadICONImage;
688   entry->encoder=(EncodeImageHandler *) WriteICONImage;
689   entry->adjoin=MagickFalse;
690   entry->seekable_stream=MagickTrue;
691   entry->description=ConstantString("Microsoft icon");
692   entry->module=ConstantString("ICON");
693   (void) RegisterMagickInfo(entry);
694   return(MagickImageCoderSignature);
695 }
696 \f
697 /*
698 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
699 %                                                                             %
700 %                                                                             %
701 %                                                                             %
702 %   U n r e g i s t e r I C O N I m a g e                                     %
703 %                                                                             %
704 %                                                                             %
705 %                                                                             %
706 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
707 %
708 %  UnregisterICONImage() removes format registrations made by the
709 %  ICON module from the list of supported formats.
710 %
711 %  The format of the UnregisterICONImage method is:
712 %
713 %      UnregisterICONImage(void)
714 %
715 */
716 ModuleExport void UnregisterICONImage(void)
717 {
718   (void) UnregisterMagickInfo("CUR");
719   (void) UnregisterMagickInfo("ICO");
720   (void) UnregisterMagickInfo("ICON");
721 }
722 \f
723 /*
724 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
725 %                                                                             %
726 %                                                                             %
727 %                                                                             %
728 %   W r i t e I C O N I m a g e                                               %
729 %                                                                             %
730 %                                                                             %
731 %                                                                             %
732 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
733 %
734 %  WriteICONImage() writes an image in Microsoft Windows bitmap encoded
735 %  image format, version 3 for Windows or (if the image has a matte channel)
736 %  version 4.
737 %
738 %  The format of the WriteICONImage method is:
739 %
740 %      MagickBooleanType WriteICONImage(const ImageInfo *image_info,
741 %        Image *image)
742 %
743 %  A description of each parameter follows.
744 %
745 %    o image_info: the image info.
746 %
747 %    o image:  The image.
748 %
749 */
750 static MagickBooleanType WriteICONImage(const ImageInfo *image_info,
751   Image *image)
752 {
753   IconFile
754     icon_file;
755
756   IconInfo
757     icon_info;
758
759   Image
760     *next;
761   
762   ssize_t
763     y;
764
765   MagickBooleanType
766     status;
767
768   MagickOffsetType
769     offset,
770     scene;
771
772   register const IndexPacket
773     *indexes;
774
775   register const PixelPacket
776     *p;
777
778   register ssize_t
779     i,
780     x;
781
782   register unsigned char
783     *q;
784
785   unsigned char
786     bit,
787     byte,
788     *pixels;
789
790   size_t
791     bytes_per_line,
792     scanline_pad;
793
794   /*
795     Open output image file.
796   */
797   assert(image_info != (const ImageInfo *) NULL);
798   assert(image_info->signature == MagickSignature);
799   assert(image != (Image *) NULL);
800   assert(image->signature == MagickSignature);
801     (void) LogMagickEvent(CoderEvent,GetMagickModule(),"%s",image->filename);
802   status=OpenBlob(image_info,image,WriteBinaryBlobMode,&image->exception);
803   if (status == MagickFalse)
804     return(status);
805   scene=0;
806   next=image;
807   do
808   {
809     if ((image->columns > 256L) || (image->rows > 256L))
810       ThrowWriterException(ImageError,"WidthOrHeightExceedsLimit");
811     scene++;
812     next=SyncNextImageInList(next);
813   } while ((next != (Image *) NULL) && (image_info->adjoin != MagickFalse));
814   /*
815     Dump out a ICON header template to be properly initialized later.
816   */
817   (void) WriteBlobLSBShort(image,0);
818   (void) WriteBlobLSBShort(image,1);
819   (void) WriteBlobLSBShort(image,(unsigned char) scene);
820   (void) ResetMagickMemory(&icon_file,0,sizeof(icon_file));
821   (void) ResetMagickMemory(&icon_info,0,sizeof(icon_info));
822   scene=0;
823   next=image;
824   do
825   {
826     (void) WriteBlobByte(image,icon_file.directory[scene].width);
827     (void) WriteBlobByte(image,icon_file.directory[scene].height);
828     (void) WriteBlobByte(image,icon_file.directory[scene].colors);
829     (void) WriteBlobByte(image,icon_file.directory[scene].reserved);
830     (void) WriteBlobLSBShort(image,icon_file.directory[scene].planes);
831     (void) WriteBlobLSBShort(image,icon_file.directory[scene].bits_per_pixel);
832     (void) WriteBlobLSBLong(image,(unsigned int)
833       icon_file.directory[scene].size);
834     (void) WriteBlobLSBLong(image,(unsigned int)
835       icon_file.directory[scene].offset);
836     scene++;
837     next=SyncNextImageInList(next);
838   } while ((next != (Image *) NULL) && (image_info->adjoin != MagickFalse));
839   scene=0;
840   next=image;
841   do
842   {
843     if ((next->columns == 256) && (next->rows == 256))
844       {
845         Image
846           *write_image;
847
848         ImageInfo
849           *write_info;
850
851         size_t
852           length;
853
854         unsigned char
855           *png;
856
857         /*
858           Icon image encoded as a compressed PNG image.
859         */
860         write_image=CloneImage(next,0,0,MagickTrue,&image->exception);
861         if (write_image == (Image *) NULL)
862           return(MagickFalse);
863         write_info=CloneImageInfo(image_info);
864         (void) CopyMagickString(write_info->filename,"PNG:",MaxTextExtent);
865         png=(unsigned char *) ImageToBlob(write_info,write_image,&length,
866           &image->exception);
867         write_image=DestroyImage(write_image);
868         write_info=DestroyImageInfo(write_info);
869         if (png == (unsigned char *) NULL)
870           return(MagickFalse);
871         icon_file.directory[scene].width=0;
872         icon_file.directory[scene].height=0;
873         icon_file.directory[scene].colors=0;
874         icon_file.directory[scene].reserved=0;
875         icon_file.directory[scene].planes=1;
876         icon_file.directory[scene].bits_per_pixel=32;
877         icon_file.directory[scene].size=(size_t) length;
878         icon_file.directory[scene].offset=(size_t) TellBlob(image);
879         (void) WriteBlob(image,(size_t) length,png);
880         png=(unsigned char *) RelinquishMagickMemory(png);
881       }
882     else
883       {
884         /*
885           Initialize ICON raster file header.
886         */
887         if (next->colorspace != RGBColorspace)
888           (void) TransformImageColorspace(next,RGBColorspace);
889         icon_info.file_size=14+12+28;
890         icon_info.offset_bits=icon_info.file_size;
891         icon_info.compression=BI_RGB;
892         if ((next->storage_class != DirectClass) && (next->colors > 256))
893           (void) SetImageStorageClass(next,DirectClass);
894         if (next->storage_class == DirectClass)
895           {
896             /*
897               Full color ICON raster.
898             */
899             icon_info.number_colors=0;
900             icon_info.bits_per_pixel=32;
901             icon_info.compression=(size_t) BI_RGB;
902           }
903         else
904           {
905             size_t
906               one;
907
908             /*
909               Colormapped ICON raster.
910             */
911             icon_info.bits_per_pixel=8;
912             if (next->colors <= 256)
913               icon_info.bits_per_pixel=8;
914             if (next->colors <= 16)
915               icon_info.bits_per_pixel=4;
916             if (next->colors <= 2)
917               icon_info.bits_per_pixel=1;
918             one=1;
919             icon_info.number_colors=one << icon_info.bits_per_pixel;
920             if (icon_info.number_colors < next->colors)
921               {
922                 (void) SetImageStorageClass(next,DirectClass);
923                 icon_info.number_colors=0;
924                 icon_info.bits_per_pixel=(unsigned short) 24;
925                 icon_info.compression=(size_t) BI_RGB;
926               }
927             else
928               {
929                 size_t
930                   one;
931
932                 one=1;
933                 icon_info.file_size+=3*(one << icon_info.bits_per_pixel);
934                 icon_info.offset_bits+=3*(one << icon_info.bits_per_pixel);
935                 icon_info.file_size+=(one << icon_info.bits_per_pixel);
936                 icon_info.offset_bits+=(one << icon_info.bits_per_pixel);
937               }
938           }
939         bytes_per_line=(((next->columns*icon_info.bits_per_pixel)+31) &
940           ~31) >> 3;
941         icon_info.ba_offset=0;
942         icon_info.width=(ssize_t) next->columns;
943         icon_info.height=(ssize_t) next->rows;
944         icon_info.planes=1;
945         icon_info.image_size=bytes_per_line*next->rows;
946         icon_info.size=40;
947         icon_info.size+=(4*icon_info.number_colors);
948         icon_info.size+=icon_info.image_size;
949         icon_info.size+=(((icon_info.width+31) & ~31) >> 3)*icon_info.height;
950         icon_info.file_size+=icon_info.image_size;
951         icon_info.x_pixels=0;
952         icon_info.y_pixels=0;
953         switch (next->units)
954         {
955           case UndefinedResolution:
956           case PixelsPerInchResolution:
957           {
958             icon_info.x_pixels=(size_t) (100.0*next->x_resolution/2.54);
959             icon_info.y_pixels=(size_t) (100.0*next->y_resolution/2.54);
960             break;
961           }
962           case PixelsPerCentimeterResolution:
963           {
964             icon_info.x_pixels=(size_t) (100.0*next->x_resolution);
965             icon_info.y_pixels=(size_t) (100.0*next->y_resolution);
966             break;
967           }
968         }
969         icon_info.colors_important=icon_info.number_colors;
970         /*
971           Convert MIFF to ICON raster pixels.
972         */
973         pixels=(unsigned char *) AcquireQuantumMemory((size_t)
974           icon_info.image_size,sizeof(*pixels));
975         if (pixels == (unsigned char *) NULL)
976           ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
977         (void) ResetMagickMemory(pixels,0,(size_t) icon_info.image_size);
978         switch (icon_info.bits_per_pixel)
979         {
980           case 1:
981           {
982             size_t
983               bit,
984               byte;
985
986             /*
987               Convert PseudoClass image to a ICON monochrome image.
988             */
989             for (y=0; y < (ssize_t) next->rows; y++)
990             {
991               p=GetVirtualPixels(next,0,y,next->columns,1,&next->exception);
992               if (p == (const PixelPacket *) NULL)
993                 break;
994               indexes=GetVirtualIndexQueue(next);
995               q=pixels+(next->rows-y-1)*bytes_per_line;
996               bit=0;
997               byte=0;
998               for (x=0; x < (ssize_t) next->columns; x++)
999               {
1000                 byte<<=1;
1001                 byte|=indexes[x] != 0 ? 0x01 : 0x00;
1002                 bit++;
1003                 if (bit == 8)
1004                   {
1005                     *q++=(unsigned char) byte;
1006                     bit=0;
1007                     byte=0;
1008                   }
1009                }
1010               if (bit != 0)
1011                 *q++=(unsigned char) (byte << (8-bit));
1012               if (next->previous == (Image *) NULL)
1013                 {
1014                   status=SetImageProgress(next,SaveImageTag,y,next->rows);
1015                   if (status == MagickFalse)
1016                     break;
1017                 }
1018             }
1019             break;
1020           }
1021           case 4:
1022           {
1023             size_t
1024               nibble,
1025               byte;
1026
1027             /*
1028               Convert PseudoClass image to a ICON monochrome image.
1029             */
1030             for (y=0; y < (ssize_t) next->rows; y++)
1031             {
1032               p=GetVirtualPixels(next,0,y,next->columns,1,&next->exception);
1033               if (p == (const PixelPacket *) NULL)
1034                 break;
1035               indexes=GetVirtualIndexQueue(next);
1036               q=pixels+(next->rows-y-1)*bytes_per_line;
1037               nibble=0;
1038               byte=0;
1039               for (x=0; x < (ssize_t) next->columns; x++)
1040               {
1041                 byte<<=4;
1042                 byte|=((size_t) indexes[x] & 0x0f);
1043                 nibble++;
1044                 if (nibble == 2)
1045                   {
1046                     *q++=(unsigned char) byte;
1047                     nibble=0;
1048                     byte=0;
1049                   }
1050                }
1051               if (nibble != 0)
1052                 *q++=(unsigned char) (byte << 4);
1053               if (next->previous == (Image *) NULL)
1054                 {
1055                   status=SetImageProgress(next,SaveImageTag,y,next->rows);
1056                   if (status == MagickFalse)
1057                     break;
1058                 }
1059             }
1060             break;
1061           }
1062           case 8:
1063           {
1064             /*
1065               Convert PseudoClass packet to ICON pixel.
1066             */
1067             for (y=0; y < (ssize_t) next->rows; y++)
1068             {
1069               p=GetVirtualPixels(next,0,y,next->columns,1,&next->exception);
1070               if (p == (const PixelPacket *) NULL)
1071                 break;
1072               indexes=GetVirtualIndexQueue(next);
1073               q=pixels+(next->rows-y-1)*bytes_per_line;
1074               for (x=0; x < (ssize_t) next->columns; x++)
1075                 *q++=(unsigned char) indexes[x];
1076               if (next->previous == (Image *) NULL)
1077                 {
1078                   status=SetImageProgress(next,SaveImageTag,y,next->rows);
1079                   if (status == MagickFalse)
1080                     break;
1081                 }
1082             }
1083             break;
1084           }
1085           case 24:
1086           case 32:
1087           {
1088             /*
1089               Convert DirectClass packet to ICON BGR888 or BGRA8888 pixel.
1090             */
1091             for (y=0; y < (ssize_t) next->rows; y++)
1092             {
1093               p=GetVirtualPixels(next,0,y,next->columns,1,&next->exception);
1094               if (p == (const PixelPacket *) NULL)
1095                 break;
1096               q=pixels+(next->rows-y-1)*bytes_per_line;
1097               for (x=0; x < (ssize_t) next->columns; x++)
1098               {
1099                 *q++=ScaleQuantumToChar(GetBluePixelComponent(p));
1100                 *q++=ScaleQuantumToChar(GetGreenPixelComponent(p));
1101                 *q++=ScaleQuantumToChar(GetRedPixelComponent(p));
1102                 if (next->matte == MagickFalse)
1103                   *q++=ScaleQuantumToChar(QuantumRange);
1104                 else
1105                   *q++=ScaleQuantumToChar(GetAlphaPixelComponent(p));
1106                 p++;
1107               }
1108               if (icon_info.bits_per_pixel == 24)
1109                 for (x=3L*(ssize_t) next->columns; x < (ssize_t) bytes_per_line; x++)
1110                   *q++=0x00;
1111               if (next->previous == (Image *) NULL)
1112                 {
1113                   status=SetImageProgress(next,SaveImageTag,y,next->rows);
1114                   if (status == MagickFalse)
1115                     break;
1116                 }
1117             }
1118             break;
1119           }
1120         }
1121         /*
1122           Write 40-byte version 3+ bitmap header.
1123         */
1124         icon_file.directory[scene].width=(unsigned char) icon_info.width;
1125         icon_file.directory[scene].height=(unsigned char) icon_info.height;
1126         icon_file.directory[scene].colors=(unsigned char)
1127           icon_info.number_colors;
1128         icon_file.directory[scene].reserved=0;
1129         icon_file.directory[scene].planes=icon_info.planes;
1130         icon_file.directory[scene].bits_per_pixel=icon_info.bits_per_pixel;
1131         icon_file.directory[scene].size=icon_info.size;
1132         icon_file.directory[scene].offset=(size_t) TellBlob(image);
1133         (void) WriteBlobLSBLong(image,(unsigned int) 40);
1134         (void) WriteBlobLSBLong(image,(unsigned int) icon_info.width);
1135         (void) WriteBlobLSBLong(image,(unsigned int) icon_info.height*2);
1136         (void) WriteBlobLSBShort(image,icon_info.planes);
1137         (void) WriteBlobLSBShort(image,icon_info.bits_per_pixel);
1138         (void) WriteBlobLSBLong(image,(unsigned int) icon_info.compression);
1139         (void) WriteBlobLSBLong(image,(unsigned int) icon_info.image_size);
1140         (void) WriteBlobLSBLong(image,(unsigned int) icon_info.x_pixels);
1141         (void) WriteBlobLSBLong(image,(unsigned int) icon_info.y_pixels);
1142         (void) WriteBlobLSBLong(image,(unsigned int) icon_info.number_colors);
1143         (void) WriteBlobLSBLong(image,(unsigned int) icon_info.colors_important);
1144         if (next->storage_class == PseudoClass)
1145           {
1146             unsigned char
1147               *icon_colormap;
1148
1149             /*
1150               Dump colormap to file.
1151             */
1152             icon_colormap=(unsigned char *) AcquireQuantumMemory((size_t)
1153               (1UL << icon_info.bits_per_pixel),4UL*sizeof(*icon_colormap));
1154             if (icon_colormap == (unsigned char *) NULL)
1155               ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
1156             q=icon_colormap;
1157             for (i=0; i < (ssize_t) next->colors; i++)
1158             {
1159               *q++=ScaleQuantumToChar(next->colormap[i].blue);
1160               *q++=ScaleQuantumToChar(next->colormap[i].green);
1161               *q++=ScaleQuantumToChar(next->colormap[i].red);
1162               *q++=(unsigned char) 0x0;
1163             }
1164             for ( ; i < (ssize_t) (1UL << icon_info.bits_per_pixel); i++)
1165             {
1166               *q++=(unsigned char) 0x00;
1167               *q++=(unsigned char) 0x00;
1168               *q++=(unsigned char) 0x00;
1169               *q++=(unsigned char) 0x00;
1170             }
1171             (void) WriteBlob(image,(size_t) (4UL*(1UL <<
1172               icon_info.bits_per_pixel)),icon_colormap);
1173             icon_colormap=(unsigned char *) RelinquishMagickMemory(
1174               icon_colormap);
1175           }
1176         (void) WriteBlob(image,(size_t) icon_info.image_size,pixels);
1177         pixels=(unsigned char *) RelinquishMagickMemory(pixels);
1178         /*
1179           Write matte mask.
1180         */
1181         scanline_pad=(((next->columns+31) & ~31)-next->columns) >> 3;
1182         for (y=((ssize_t) next->rows - 1); y >= 0; y--)
1183         {
1184           p=GetVirtualPixels(next,0,y,next->columns,1,&next->exception);
1185           if (p == (const PixelPacket *) NULL)
1186             break;
1187           bit=0;
1188           byte=0;
1189           for (x=0; x < (ssize_t) next->columns; x++)
1190           {
1191             byte<<=1;
1192             if ((next->matte == MagickTrue) &&
1193                 (p->opacity == (Quantum) TransparentOpacity))
1194               byte|=0x01;
1195             bit++;
1196             if (bit == 8)
1197               {
1198                 (void) WriteBlobByte(image,(unsigned char) byte);
1199                 bit=0;
1200                 byte=0;
1201               }
1202             p++;
1203           }
1204           if (bit != 0)
1205             (void) WriteBlobByte(image,(unsigned char) (byte << (8-bit)));
1206           for (i=0; i < (ssize_t) scanline_pad; i++)
1207             (void) WriteBlobByte(image,(unsigned char) 0);
1208         }
1209       }
1210     if (GetNextImageInList(next) == (Image *) NULL)
1211       break;
1212     next=SyncNextImageInList(next);
1213     status=SetImageProgress(next,SaveImagesTag,scene++,
1214       GetImageListLength(next));
1215     if (status == MagickFalse)
1216       break;
1217   } while ((next != (Image *) NULL) && (image_info->adjoin != MagickFalse));
1218   offset=SeekBlob(image,0,SEEK_SET);
1219   (void) WriteBlobLSBShort(image,0);
1220   (void) WriteBlobLSBShort(image,1);
1221   (void) WriteBlobLSBShort(image,(unsigned short) (scene+1));
1222   scene=0;
1223   next=image;
1224   do
1225   {
1226     (void) WriteBlobByte(image,icon_file.directory[scene].width);
1227     (void) WriteBlobByte(image,icon_file.directory[scene].height);
1228     (void) WriteBlobByte(image,icon_file.directory[scene].colors);
1229     (void) WriteBlobByte(image,icon_file.directory[scene].reserved);
1230     (void) WriteBlobLSBShort(image,icon_file.directory[scene].planes);
1231     (void) WriteBlobLSBShort(image,icon_file.directory[scene].bits_per_pixel);
1232     (void) WriteBlobLSBLong(image,(unsigned int)
1233       icon_file.directory[scene].size);
1234     (void) WriteBlobLSBLong(image,(unsigned int)
1235       icon_file.directory[scene].offset);
1236     scene++;
1237     next=SyncNextImageInList(next);
1238   } while ((next != (Image *) NULL) && (image_info->adjoin != MagickFalse));
1239   (void) CloseBlob(image);
1240   return(MagickTrue);
1241 }