2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
10 % IIIII CCCC OOO N N %
13 % Read Microsoft Windows Icon Format %
20 % Copyright 1999-2017 ImageMagick Studio LLC, a non-profit organization %
21 % dedicated to making software imaging solutions freely available. %
23 % You may not use this file except in compliance with the License. You may %
24 % obtain a copy of the License at %
26 % http://www.imagemagick.org/script/license.php %
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. %
34 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
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"
72 #if !defined(MAGICKCORE_WINDOWS_SUPPORT) || defined(__MINGW32__) || defined(__MINGW64__)
75 #define BI_BITFIELDS 3
82 typedef struct _IconEntry
99 typedef struct _IconFile
110 typedef struct _IconInfo
143 Forward declaractions.
146 *AutoResizeImage(const Image *,const char *,MagickOffsetType *,
149 static MagickBooleanType
150 WriteICONImage(const ImageInfo *,Image *,ExceptionInfo *);
152 Image *AutoResizeImage(const Image *image,const char *option,
153 MagickOffsetType *count,ExceptionInfo *exception)
171 sizes[MAX_SIZES]={256,192,128,96,64,48,40,32,24,16};
177 while (*p != '\0' && i < MAX_SIZES)
182 while ((isspace((int) ((unsigned char) *p)) != 0))
185 size=(size_t)strtol(p,&q,10);
186 if ((p == q) || (size < 16) || (size > 256))
187 return((Image *) NULL);
192 while ((isspace((int) ((unsigned char) *p)) != 0) || (*p == ','))
199 for (i=0; i < *count; i++)
201 resized=ResizeImage(image,sizes[i],sizes[i],image->filter,exception);
202 if (resized == (Image *) NULL)
203 return(DestroyImageList(images));
205 if (images == (Image *) NULL)
208 AppendImageToList(&images,resized);
214 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
218 % R e a d I C O N I m a g e %
222 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
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.
228 % The format of the ReadICONImage method is:
230 % Image *ReadICONImage(const ImageInfo *image_info,
231 % ExceptionInfo *exception)
233 % A description of each parameter follows:
235 % o image_info: the image info.
237 % o exception: return any errors or warnings in this structure.
240 static Image *ReadICONImage(const ImageInfo *image_info,
241 ExceptionInfo *exception)
265 register unsigned char
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)
292 image=DestroyImageList(image);
293 return((Image *) NULL);
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");
303 for (i=0; i < icon_file.count; i++)
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)
316 extent=MagickMax(extent,icon_file.directory[i].size);
318 if ((EOFBlob(image) != MagickFalse) || (extent > GetBlobSize(image)))
319 ThrowReaderException(CorruptImageError,"UnexpectedEndOfFile");
321 for (i=0; i < icon_file.count; i++)
324 Verify Icon identifier.
326 offset=(ssize_t) SeekBlob(image,(MagickOffsetType)
327 icon_file.directory[i].offset,SEEK_SET);
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)
337 ThrowFileException(exception,CorruptImageError,"UnexpectedEndOfFile",
341 if (((icon_info.planes == 18505) && (icon_info.bits_per_pixel == 21060)) ||
342 (icon_info.size == 0x474e5089))
357 Icon image encoded as a compressed PNG image.
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) CopyMagickMemory(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;
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);
379 png=(unsigned char *) RelinquishMagickMemory(png);
380 if (icon_image == (Image *) NULL)
382 if (count != (ssize_t) (length-16))
383 ThrowReaderException(CorruptImageError,
384 "InsufficientImageDataInFile");
385 image=DestroyImageList(image);
386 return((Image *) NULL);
388 DestroyBlob(icon_image);
389 icon_image->blob=ReferenceBlob(image->blob);
390 ReplaceImageInList(&image,icon_image);
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 icon_info.colors_important=ReadBlobLSBLong(image);
402 image->alpha_trait=BlendPixelTrait;
403 image->columns=(size_t) icon_file.directory[i].width;
404 if ((ssize_t) image->columns > icon_info.width)
405 image->columns=(size_t) icon_info.width;
406 if (image->columns == 0)
408 image->rows=(size_t) icon_file.directory[i].height;
409 if ((ssize_t) image->rows > icon_info.height)
410 image->rows=(size_t) icon_info.height;
411 if (image->rows == 0)
413 image->depth=icon_info.bits_per_pixel;
414 if (image->debug != MagickFalse)
416 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
417 " scene = %.20g",(double) i);
418 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
419 " size = %.20g",(double) icon_info.size);
420 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
421 " width = %.20g",(double) icon_file.directory[i].width);
422 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
423 " height = %.20g",(double) icon_file.directory[i].height);
424 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
425 " colors = %.20g",(double ) icon_info.number_colors);
426 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
427 " planes = %.20g",(double) icon_info.planes);
428 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
429 " bpp = %.20g",(double) icon_info.bits_per_pixel);
431 if ((icon_info.number_colors != 0) || (icon_info.bits_per_pixel <= 16U))
433 image->storage_class=PseudoClass;
434 image->colors=icon_info.number_colors;
435 if (image->colors == 0)
436 image->colors=one << icon_info.bits_per_pixel;
438 if (image->storage_class == PseudoClass)
447 Read Icon raster colormap.
449 if (AcquireImageColormap(image,image->colors,exception) ==
451 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
452 icon_colormap=(unsigned char *) AcquireQuantumMemory((size_t)
453 image->colors,4UL*sizeof(*icon_colormap));
454 if (icon_colormap == (unsigned char *) NULL)
455 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
456 count=ReadBlob(image,(size_t) (4*image->colors),icon_colormap);
457 if (count != (ssize_t) (4*image->colors))
458 ThrowReaderException(CorruptImageError,
459 "InsufficientImageDataInFile");
461 for (i=0; i < (ssize_t) image->colors; i++)
463 image->colormap[i].blue=(Quantum) ScaleCharToQuantum(*p++);
464 image->colormap[i].green=(Quantum) ScaleCharToQuantum(*p++);
465 image->colormap[i].red=(Quantum) ScaleCharToQuantum(*p++);
468 icon_colormap=(unsigned char *) RelinquishMagickMemory(icon_colormap);
471 Convert Icon raster image to pixel packets.
473 if ((image_info->ping != MagickFalse) &&
474 (image_info->number_scenes != 0))
475 if (image->scene >= (image_info->scene+image_info->number_scenes-1))
477 status=SetImageExtent(image,image->columns,image->rows,exception);
478 if (status == MagickFalse)
479 return(DestroyImageList(image));
480 bytes_per_line=(((image->columns*icon_info.bits_per_pixel)+31) &
482 (void) bytes_per_line;
483 scanline_pad=((((image->columns*icon_info.bits_per_pixel)+31) & ~31)-
484 (image->columns*icon_info.bits_per_pixel)) >> 3;
485 switch (icon_info.bits_per_pixel)
490 Convert bitmap scanline.
492 for (y=(ssize_t) image->rows-1; y >= 0; y--)
494 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
495 if (q == (Quantum *) NULL)
497 for (x=0; x < (ssize_t) (image->columns-7); x+=8)
499 byte=(size_t) ReadBlobByte(image);
500 for (bit=0; bit < 8; bit++)
502 SetPixelIndex(image,((byte & (0x80 >> bit)) != 0 ? 0x01 :
504 q+=GetPixelChannels(image);
507 if ((image->columns % 8) != 0)
509 byte=(size_t) ReadBlobByte(image);
510 for (bit=0; bit < (image->columns % 8); bit++)
512 SetPixelIndex(image,((byte & (0x80 >> bit)) != 0 ? 0x01 :
514 q+=GetPixelChannels(image);
517 for (x=0; x < (ssize_t) scanline_pad; x++)
518 (void) ReadBlobByte(image);
519 if (SyncAuthenticPixels(image,exception) == MagickFalse)
521 if (image->previous == (Image *) NULL)
523 status=SetImageProgress(image,LoadImageTag,image->rows-y-1,
525 if (status == MagickFalse)
534 Read 4-bit Icon scanline.
536 for (y=(ssize_t) image->rows-1; y >= 0; y--)
538 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
539 if (q == (Quantum *) NULL)
541 for (x=0; x < ((ssize_t) image->columns-1); x+=2)
543 byte=(size_t) ReadBlobByte(image);
544 SetPixelIndex(image,((byte >> 4) & 0xf),q);
545 q+=GetPixelChannels(image);
546 SetPixelIndex(image,((byte) & 0xf),q);
547 q+=GetPixelChannels(image);
549 if ((image->columns % 2) != 0)
551 byte=(size_t) ReadBlobByte(image);
552 SetPixelIndex(image,((byte >> 4) & 0xf),q);
553 q+=GetPixelChannels(image);
555 for (x=0; x < (ssize_t) scanline_pad; x++)
556 (void) ReadBlobByte(image);
557 if (SyncAuthenticPixels(image,exception) == MagickFalse)
559 if (image->previous == (Image *) NULL)
561 status=SetImageProgress(image,LoadImageTag,image->rows-y-1,
563 if (status == MagickFalse)
572 Convert PseudoColor scanline.
574 for (y=(ssize_t) image->rows-1; y >= 0; y--)
576 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
577 if (q == (Quantum *) NULL)
579 for (x=0; x < (ssize_t) image->columns; x++)
581 byte=(size_t) ReadBlobByte(image);
582 SetPixelIndex(image,(Quantum) byte,q);
583 q+=GetPixelChannels(image);
585 for (x=0; x < (ssize_t) scanline_pad; x++)
586 (void) ReadBlobByte(image);
587 if (SyncAuthenticPixels(image,exception) == MagickFalse)
589 if (image->previous == (Image *) NULL)
591 status=SetImageProgress(image,LoadImageTag,image->rows-y-1,
593 if (status == MagickFalse)
602 Convert PseudoColor scanline.
604 for (y=(ssize_t) image->rows-1; y >= 0; y--)
606 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
607 if (q == (Quantum *) NULL)
609 for (x=0; x < (ssize_t) image->columns; x++)
611 byte=(size_t) ReadBlobByte(image);
612 byte|=(size_t) (ReadBlobByte(image) << 8);
613 SetPixelIndex(image,(Quantum) byte,q);
614 q+=GetPixelChannels(image);
616 for (x=0; x < (ssize_t) scanline_pad; x++)
617 (void) ReadBlobByte(image);
618 if (SyncAuthenticPixels(image,exception) == MagickFalse)
620 if (image->previous == (Image *) NULL)
622 status=SetImageProgress(image,LoadImageTag,image->rows-y-1,
624 if (status == MagickFalse)
634 Convert DirectColor scanline.
636 for (y=(ssize_t) image->rows-1; y >= 0; y--)
638 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
639 if (q == (Quantum *) NULL)
641 for (x=0; x < (ssize_t) image->columns; x++)
643 SetPixelBlue(image,ScaleCharToQuantum((unsigned char)
644 ReadBlobByte(image)),q);
645 SetPixelGreen(image,ScaleCharToQuantum((unsigned char)
646 ReadBlobByte(image)),q);
647 SetPixelRed(image,ScaleCharToQuantum((unsigned char)
648 ReadBlobByte(image)),q);
649 if (icon_info.bits_per_pixel == 32)
650 SetPixelAlpha(image,ScaleCharToQuantum((unsigned char)
651 ReadBlobByte(image)),q);
652 q+=GetPixelChannels(image);
654 if (icon_info.bits_per_pixel == 24)
655 for (x=0; x < (ssize_t) scanline_pad; x++)
656 (void) ReadBlobByte(image);
657 if (SyncAuthenticPixels(image,exception) == MagickFalse)
659 if (image->previous == (Image *) NULL)
661 status=SetImageProgress(image,LoadImageTag,image->rows-y-1,
663 if (status == MagickFalse)
670 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
672 if ((image_info->ping == MagickFalse) &&
673 (icon_info.bits_per_pixel <= 16))
674 (void) SyncImage(image,exception);
675 if (icon_info.bits_per_pixel != 32)
678 Read the ICON alpha mask.
680 image->storage_class=DirectClass;
681 for (y=(ssize_t) image->rows-1; y >= 0; y--)
683 q=GetAuthenticPixels(image,0,y,image->columns,1,exception);
684 if (q == (Quantum *) NULL)
686 for (x=0; x < ((ssize_t) image->columns-7); x+=8)
688 byte=(size_t) ReadBlobByte(image);
689 for (bit=0; bit < 8; bit++)
691 SetPixelAlpha(image,(((byte & (0x80 >> bit)) != 0) ?
692 TransparentAlpha : OpaqueAlpha),q);
693 q+=GetPixelChannels(image);
696 if ((image->columns % 8) != 0)
698 byte=(size_t) ReadBlobByte(image);
699 for (bit=0; bit < (image->columns % 8); bit++)
701 SetPixelAlpha(image,(((byte & (0x80 >> bit)) != 0) ?
702 TransparentAlpha : OpaqueAlpha),q);
703 q+=GetPixelChannels(image);
706 if ((image->columns % 32) != 0)
707 for (x=0; x < (ssize_t) ((32-(image->columns % 32))/8); x++)
708 (void) ReadBlobByte(image);
709 if (SyncAuthenticPixels(image,exception) == MagickFalse)
713 if (EOFBlob(image) != MagickFalse)
715 ThrowFileException(exception,CorruptImageError,
716 "UnexpectedEndOfFile",image->filename);
721 Proceed to next image.
723 if (image_info->number_scenes != 0)
724 if (image->scene >= (image_info->scene+image_info->number_scenes-1))
726 if (i < (ssize_t) (icon_file.count-1))
729 Allocate next image structure.
731 AcquireNextImage(image_info,image,exception);
732 if (GetNextImageInList(image) == (Image *) NULL)
734 image=DestroyImageList(image);
735 return((Image *) NULL);
737 image=SyncNextImageInList(image);
738 status=SetImageProgress(image,LoadImagesTag,TellBlob(image),
740 if (status == MagickFalse)
744 (void) CloseBlob(image);
745 return(GetFirstImageInList(image));
749 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
753 % R e g i s t e r I C O N I m a g e %
757 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
759 % RegisterICONImage() adds attributes for the Icon image format to
760 % the list of supported formats. The attributes include the image format
761 % tag, a method to read and/or write the format, whether the format
762 % supports the saving of more than one frame to the same file or blob,
763 % whether the format supports native in-memory I/O, and a brief
764 % description of the format.
766 % The format of the RegisterICONImage method is:
768 % size_t RegisterICONImage(void)
771 ModuleExport size_t RegisterICONImage(void)
776 entry=AcquireMagickInfo("ICON","CUR","Microsoft icon");
777 entry->decoder=(DecodeImageHandler *) ReadICONImage;
778 entry->encoder=(EncodeImageHandler *) WriteICONImage;
779 entry->flags^=CoderAdjoinFlag;
780 entry->flags|=CoderDecoderSeekableStreamFlag;
781 entry->flags|=CoderEncoderSeekableStreamFlag;
782 (void) RegisterMagickInfo(entry);
783 entry=AcquireMagickInfo("ICON","ICO","Microsoft icon");
784 entry->decoder=(DecodeImageHandler *) ReadICONImage;
785 entry->encoder=(EncodeImageHandler *) WriteICONImage;
786 entry->flags|=CoderDecoderSeekableStreamFlag;
787 entry->flags|=CoderEncoderSeekableStreamFlag;
788 (void) RegisterMagickInfo(entry);
789 entry=AcquireMagickInfo("ICON","ICON","Microsoft icon");
790 entry->decoder=(DecodeImageHandler *) ReadICONImage;
791 entry->encoder=(EncodeImageHandler *) WriteICONImage;
792 entry->flags^=CoderAdjoinFlag;
793 entry->flags|=CoderDecoderSeekableStreamFlag;
794 entry->flags|=CoderEncoderSeekableStreamFlag;
795 (void) RegisterMagickInfo(entry);
796 return(MagickImageCoderSignature);
800 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
804 % U n r e g i s t e r I C O N I m a g e %
808 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
810 % UnregisterICONImage() removes format registrations made by the
811 % ICON module from the list of supported formats.
813 % The format of the UnregisterICONImage method is:
815 % UnregisterICONImage(void)
818 ModuleExport void UnregisterICONImage(void)
820 (void) UnregisterMagickInfo("CUR");
821 (void) UnregisterMagickInfo("ICO");
822 (void) UnregisterMagickInfo("ICON");
826 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
830 % W r i t e I C O N I m a g e %
834 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
836 % WriteICONImage() writes an image in Microsoft Windows bitmap encoded
837 % image format, version 3 for Windows or (if the image has a matte channel)
840 % It encodes any subimage as a compressed PNG image ("BI_PNG)", only when its
841 % dimensions are 256x256 and image->compression is undefined or is defined as
844 % The format of the WriteICONImage method is:
846 % MagickBooleanType WriteICONImage(const ImageInfo *image_info,
847 % Image *image,ExceptionInfo *exception)
849 % A description of each parameter follows.
851 % o image_info: the image info.
853 % o image: The image.
855 % o exception: return any errors or warnings in this structure.
858 static MagickBooleanType WriteICONImage(const ImageInfo *image_info,
859 Image *image,ExceptionInfo *exception)
881 register const Quantum
888 register unsigned char
904 Open output image file.
906 assert(image_info != (const ImageInfo *) NULL);
907 assert(image_info->signature == MagickCoreSignature);
908 assert(image != (Image *) NULL);
909 assert(image->signature == MagickCoreSignature);
910 (void) LogMagickEvent(CoderEvent,GetMagickModule(),"%s",image->filename);
911 assert(exception != (ExceptionInfo *) NULL);
912 assert(exception->signature == MagickCoreSignature);
913 status=OpenBlob(image_info,image,WriteBinaryBlobMode,exception);
914 if (status == MagickFalse)
916 images=(Image *) NULL;
917 option=GetImageOption(image_info,"icon:auto-resize");
918 if (option != (const char *) NULL)
920 images=AutoResizeImage(image,option,&scene,exception);
921 if (images == (Image *) NULL)
922 ThrowWriterException(ImageError,"InvalidDimensions");
930 if ((image->columns > 256L) || (image->rows > 256L))
931 ThrowWriterException(ImageError,"WidthOrHeightExceedsLimit");
933 next=SyncNextImageInList(next);
934 } while ((next != (Image *) NULL) && (image_info->adjoin != MagickFalse));
937 Dump out a ICON header template to be properly initialized later.
939 (void) WriteBlobLSBShort(image,0);
940 (void) WriteBlobLSBShort(image,1);
941 (void) WriteBlobLSBShort(image,(unsigned char) scene);
942 (void) ResetMagickMemory(&icon_file,0,sizeof(icon_file));
943 (void) ResetMagickMemory(&icon_info,0,sizeof(icon_info));
945 next=(images != (Image *) NULL) ? images : image;
948 (void) WriteBlobByte(image,icon_file.directory[scene].width);
949 (void) WriteBlobByte(image,icon_file.directory[scene].height);
950 (void) WriteBlobByte(image,icon_file.directory[scene].colors);
951 (void) WriteBlobByte(image,icon_file.directory[scene].reserved);
952 (void) WriteBlobLSBShort(image,icon_file.directory[scene].planes);
953 (void) WriteBlobLSBShort(image,icon_file.directory[scene].bits_per_pixel);
954 (void) WriteBlobLSBLong(image,(unsigned int)
955 icon_file.directory[scene].size);
956 (void) WriteBlobLSBLong(image,(unsigned int)
957 icon_file.directory[scene].offset);
959 next=SyncNextImageInList(next);
960 } while ((next != (Image *) NULL) && (image_info->adjoin != MagickFalse));
962 next=(images != (Image *) NULL) ? images : image;
965 if ((next->columns > 255L) && (next->rows > 255L) &&
966 ((next->compression == UndefinedCompression) ||
967 (next->compression == ZipCompression)))
981 write_image=CloneImage(next,0,0,MagickTrue,exception);
982 if (write_image == (Image *) NULL)
984 images=DestroyImageList(images);
987 write_info=CloneImageInfo(image_info);
988 (void) CopyMagickString(write_info->filename,"PNG:",MagickPathExtent);
990 /* Don't write any ancillary chunks except for gAMA */
991 (void) SetImageArtifact(write_image,"png:include-chunk","none,gama");
993 /* Only write PNG32 formatted PNG (32-bit RGBA), 8 bits per channel */
994 (void) SetImageArtifact(write_image,"png:format","png32");
996 png=(unsigned char *) ImageToBlob(write_info,write_image,&length,
998 write_image=DestroyImageList(write_image);
999 write_info=DestroyImageInfo(write_info);
1000 if (png == (unsigned char *) NULL)
1002 images=DestroyImageList(images);
1003 return(MagickFalse);
1005 icon_file.directory[scene].width=0;
1006 icon_file.directory[scene].height=0;
1007 icon_file.directory[scene].colors=0;
1008 icon_file.directory[scene].reserved=0;
1009 icon_file.directory[scene].planes=1;
1010 icon_file.directory[scene].bits_per_pixel=32;
1011 icon_file.directory[scene].size=(size_t) length;
1012 icon_file.directory[scene].offset=(size_t) TellBlob(image);
1013 (void) WriteBlob(image,(size_t) length,png);
1014 png=(unsigned char *) RelinquishMagickMemory(png);
1019 Initialize ICON raster file header.
1021 (void) TransformImageColorspace(next,sRGBColorspace,exception);
1022 icon_info.file_size=14+12+28;
1023 icon_info.offset_bits=icon_info.file_size;
1024 icon_info.compression=BI_RGB;
1025 if ((next->storage_class != DirectClass) && (next->colors > 256))
1026 (void) SetImageStorageClass(next,DirectClass,exception);
1027 if (next->storage_class == DirectClass)
1030 Full color ICON raster.
1032 icon_info.number_colors=0;
1033 icon_info.bits_per_pixel=32;
1034 icon_info.compression=(size_t) BI_RGB;
1042 Colormapped ICON raster.
1044 icon_info.bits_per_pixel=8;
1045 if (next->colors <= 256)
1046 icon_info.bits_per_pixel=8;
1047 if (next->colors <= 16)
1048 icon_info.bits_per_pixel=4;
1049 if (next->colors <= 2)
1050 icon_info.bits_per_pixel=1;
1052 icon_info.number_colors=one << icon_info.bits_per_pixel;
1053 if (icon_info.number_colors < next->colors)
1055 (void) SetImageStorageClass(next,DirectClass,exception);
1056 icon_info.number_colors=0;
1057 icon_info.bits_per_pixel=(unsigned short) 24;
1058 icon_info.compression=(size_t) BI_RGB;
1066 icon_info.file_size+=3*(one << icon_info.bits_per_pixel);
1067 icon_info.offset_bits+=3*(one << icon_info.bits_per_pixel);
1068 icon_info.file_size+=(one << icon_info.bits_per_pixel);
1069 icon_info.offset_bits+=(one << icon_info.bits_per_pixel);
1072 bytes_per_line=(((next->columns*icon_info.bits_per_pixel)+31) &
1074 icon_info.ba_offset=0;
1075 icon_info.width=(ssize_t) next->columns;
1076 icon_info.height=(ssize_t) next->rows;
1078 icon_info.image_size=bytes_per_line*next->rows;
1080 icon_info.size+=(4*icon_info.number_colors);
1081 icon_info.size+=icon_info.image_size;
1082 icon_info.size+=(((icon_info.width+31) & ~31) >> 3)*icon_info.height;
1083 icon_info.file_size+=icon_info.image_size;
1084 icon_info.x_pixels=0;
1085 icon_info.y_pixels=0;
1086 switch (next->units)
1088 case UndefinedResolution:
1089 case PixelsPerInchResolution:
1091 icon_info.x_pixels=(size_t) (100.0*next->resolution.x/2.54);
1092 icon_info.y_pixels=(size_t) (100.0*next->resolution.y/2.54);
1095 case PixelsPerCentimeterResolution:
1097 icon_info.x_pixels=(size_t) (100.0*next->resolution.x);
1098 icon_info.y_pixels=(size_t) (100.0*next->resolution.y);
1102 icon_info.colors_important=icon_info.number_colors;
1104 Convert MIFF to ICON raster pixels.
1106 pixels=(unsigned char *) AcquireQuantumMemory((size_t)
1107 icon_info.image_size,sizeof(*pixels));
1108 if (pixels == (unsigned char *) NULL)
1110 images=DestroyImageList(images);
1111 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
1113 (void) ResetMagickMemory(pixels,0,(size_t) icon_info.image_size);
1114 switch (icon_info.bits_per_pixel)
1123 Convert PseudoClass image to a ICON monochrome image.
1125 for (y=0; y < (ssize_t) next->rows; y++)
1127 p=GetVirtualPixels(next,0,y,next->columns,1,exception);
1128 if (p == (const Quantum *) NULL)
1130 q=pixels+(next->rows-y-1)*bytes_per_line;
1133 for (x=0; x < (ssize_t) next->columns; x++)
1136 byte|=GetPixelIndex(next,p) != 0 ? 0x01 : 0x00;
1140 *q++=(unsigned char) byte;
1144 p+=GetPixelChannels(image);
1147 *q++=(unsigned char) (byte << (8-bit));
1148 if (next->previous == (Image *) NULL)
1150 status=SetImageProgress(next,SaveImageTag,y,next->rows);
1151 if (status == MagickFalse)
1164 Convert PseudoClass image to a ICON monochrome image.
1166 for (y=0; y < (ssize_t) next->rows; y++)
1168 p=GetVirtualPixels(next,0,y,next->columns,1,exception);
1169 if (p == (const Quantum *) NULL)
1171 q=pixels+(next->rows-y-1)*bytes_per_line;
1174 for (x=0; x < (ssize_t) next->columns; x++)
1177 byte|=((size_t) GetPixelIndex(next,p) & 0x0f);
1181 *q++=(unsigned char) byte;
1185 p+=GetPixelChannels(image);
1188 *q++=(unsigned char) (byte << 4);
1189 if (next->previous == (Image *) NULL)
1191 status=SetImageProgress(next,SaveImageTag,y,next->rows);
1192 if (status == MagickFalse)
1201 Convert PseudoClass packet to ICON pixel.
1203 for (y=0; y < (ssize_t) next->rows; y++)
1205 p=GetVirtualPixels(next,0,y,next->columns,1,exception);
1206 if (p == (const Quantum *) NULL)
1208 q=pixels+(next->rows-y-1)*bytes_per_line;
1209 for (x=0; x < (ssize_t) next->columns; x++)
1211 *q++=(unsigned char) GetPixelIndex(next,p);
1212 p+=GetPixelChannels(image);
1214 if (next->previous == (Image *) NULL)
1216 status=SetImageProgress(next,SaveImageTag,y,next->rows);
1217 if (status == MagickFalse)
1227 Convert DirectClass packet to ICON BGR888 or BGRA8888 pixel.
1229 for (y=0; y < (ssize_t) next->rows; y++)
1231 p=GetVirtualPixels(next,0,y,next->columns,1,exception);
1232 if (p == (const Quantum *) NULL)
1234 q=pixels+(next->rows-y-1)*bytes_per_line;
1235 for (x=0; x < (ssize_t) next->columns; x++)
1237 *q++=ScaleQuantumToChar(GetPixelBlue(next,p));
1238 *q++=ScaleQuantumToChar(GetPixelGreen(next,p));
1239 *q++=ScaleQuantumToChar(GetPixelRed(next,p));
1240 if (next->alpha_trait == UndefinedPixelTrait)
1241 *q++=ScaleQuantumToChar(QuantumRange);
1243 *q++=ScaleQuantumToChar(GetPixelAlpha(next,p));
1244 p+=GetPixelChannels(next);
1246 if (icon_info.bits_per_pixel == 24)
1247 for (x=3L*(ssize_t) next->columns; x < (ssize_t) bytes_per_line; x++)
1249 if (next->previous == (Image *) NULL)
1251 status=SetImageProgress(next,SaveImageTag,y,next->rows);
1252 if (status == MagickFalse)
1260 Write 40-byte version 3+ bitmap header.
1262 icon_file.directory[scene].width=(unsigned char) icon_info.width;
1263 icon_file.directory[scene].height=(unsigned char) icon_info.height;
1264 icon_file.directory[scene].colors=(unsigned char)
1265 icon_info.number_colors;
1266 icon_file.directory[scene].reserved=0;
1267 icon_file.directory[scene].planes=icon_info.planes;
1268 icon_file.directory[scene].bits_per_pixel=icon_info.bits_per_pixel;
1269 icon_file.directory[scene].size=icon_info.size;
1270 icon_file.directory[scene].offset=(size_t) TellBlob(image);
1271 (void) WriteBlobLSBLong(image,(unsigned int) 40);
1272 (void) WriteBlobLSBLong(image,(unsigned int) icon_info.width);
1273 (void) WriteBlobLSBLong(image,(unsigned int) icon_info.height*2);
1274 (void) WriteBlobLSBShort(image,icon_info.planes);
1275 (void) WriteBlobLSBShort(image,icon_info.bits_per_pixel);
1276 (void) WriteBlobLSBLong(image,(unsigned int) icon_info.compression);
1277 (void) WriteBlobLSBLong(image,(unsigned int) icon_info.image_size);
1278 (void) WriteBlobLSBLong(image,(unsigned int) icon_info.x_pixels);
1279 (void) WriteBlobLSBLong(image,(unsigned int) icon_info.y_pixels);
1280 (void) WriteBlobLSBLong(image,(unsigned int) icon_info.number_colors);
1281 (void) WriteBlobLSBLong(image,(unsigned int) icon_info.colors_important);
1282 if (next->storage_class == PseudoClass)
1288 Dump colormap to file.
1290 icon_colormap=(unsigned char *) AcquireQuantumMemory((size_t)
1291 (1UL << icon_info.bits_per_pixel),4UL*sizeof(*icon_colormap));
1292 if (icon_colormap == (unsigned char *) NULL)
1294 images=DestroyImageList(images);
1295 ThrowWriterException(ResourceLimitError,
1296 "MemoryAllocationFailed");
1299 for (i=0; i < (ssize_t) next->colors; i++)
1301 *q++=ScaleQuantumToChar(next->colormap[i].blue);
1302 *q++=ScaleQuantumToChar(next->colormap[i].green);
1303 *q++=ScaleQuantumToChar(next->colormap[i].red);
1304 *q++=(unsigned char) 0x0;
1306 for ( ; i < (ssize_t) (1UL << icon_info.bits_per_pixel); i++)
1308 *q++=(unsigned char) 0x00;
1309 *q++=(unsigned char) 0x00;
1310 *q++=(unsigned char) 0x00;
1311 *q++=(unsigned char) 0x00;
1313 (void) WriteBlob(image,(size_t) (4UL*(1UL <<
1314 icon_info.bits_per_pixel)),icon_colormap);
1315 icon_colormap=(unsigned char *) RelinquishMagickMemory(
1318 (void) WriteBlob(image,(size_t) icon_info.image_size,pixels);
1319 pixels=(unsigned char *) RelinquishMagickMemory(pixels);
1323 scanline_pad=(((next->columns+31) & ~31)-next->columns) >> 3;
1324 for (y=((ssize_t) next->rows - 1); y >= 0; y--)
1326 p=GetVirtualPixels(next,0,y,next->columns,1,exception);
1327 if (p == (const Quantum *) NULL)
1331 for (x=0; x < (ssize_t) next->columns; x++)
1334 if ((next->alpha_trait != UndefinedPixelTrait) &&
1335 (GetPixelAlpha(next,p) == (Quantum) TransparentAlpha))
1340 (void) WriteBlobByte(image,(unsigned char) byte);
1344 p+=GetPixelChannels(next);
1347 (void) WriteBlobByte(image,(unsigned char) (byte << (8-bit)));
1348 for (i=0; i < (ssize_t) scanline_pad; i++)
1349 (void) WriteBlobByte(image,(unsigned char) 0);
1352 if (GetNextImageInList(next) == (Image *) NULL)
1354 status=SetImageProgress(next,SaveImagesTag,scene++,
1355 GetImageListLength(next));
1356 if (status == MagickFalse)
1358 next=SyncNextImageInList(next);
1359 } while ((next != (Image *) NULL) && (image_info->adjoin != MagickFalse));
1360 offset=SeekBlob(image,0,SEEK_SET);
1362 (void) WriteBlobLSBShort(image,0);
1363 (void) WriteBlobLSBShort(image,1);
1364 (void) WriteBlobLSBShort(image,(unsigned short) (scene+1));
1366 next=(images != (Image *) NULL) ? images : image;
1369 (void) WriteBlobByte(image,icon_file.directory[scene].width);
1370 (void) WriteBlobByte(image,icon_file.directory[scene].height);
1371 (void) WriteBlobByte(image,icon_file.directory[scene].colors);
1372 (void) WriteBlobByte(image,icon_file.directory[scene].reserved);
1373 (void) WriteBlobLSBShort(image,icon_file.directory[scene].planes);
1374 (void) WriteBlobLSBShort(image,icon_file.directory[scene].bits_per_pixel);
1375 (void) WriteBlobLSBLong(image,(unsigned int)
1376 icon_file.directory[scene].size);
1377 (void) WriteBlobLSBLong(image,(unsigned int)
1378 icon_file.directory[scene].offset);
1380 next=SyncNextImageInList(next);
1381 } while ((next != (Image *) NULL) && (image_info->adjoin != MagickFalse));
1382 (void) CloseBlob(image);
1383 images=DestroyImageList(images);