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