2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
10 % IIIII CCCC OOO N N %
13 % Read Microsoft Windows Icon Format %
20 % Copyright 1999-2014 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 > 255)
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)
262 register unsigned char
280 assert(image_info != (const ImageInfo *) NULL);
281 assert(image_info->signature == MagickSignature);
282 (void) LogMagickEvent(CoderEvent,GetMagickModule(),"%s",image_info->filename);
283 assert(exception != (ExceptionInfo *) NULL);
284 assert(exception->signature == MagickSignature);
285 image=AcquireImage(image_info,exception);
286 status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception);
287 if (status == MagickFalse)
289 image=DestroyImageList(image);
290 return((Image *) NULL);
292 icon_file.reserved=(short) ReadBlobLSBShort(image);
293 icon_file.resource_type=(short) ReadBlobLSBShort(image);
294 icon_file.count=(short) ReadBlobLSBShort(image);
295 if ((icon_file.reserved != 0) ||
296 ((icon_file.resource_type != 1) && (icon_file.resource_type != 2)) ||
297 (icon_file.count > MaxIcons))
298 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
299 for (i=0; i < icon_file.count; i++)
301 icon_file.directory[i].width=(unsigned char) ReadBlobByte(image);
302 icon_file.directory[i].height=(unsigned char) ReadBlobByte(image);
303 icon_file.directory[i].colors=(unsigned char) ReadBlobByte(image);
304 icon_file.directory[i].reserved=(unsigned char) ReadBlobByte(image);
305 icon_file.directory[i].planes=(unsigned short) ReadBlobLSBShort(image);
306 icon_file.directory[i].bits_per_pixel=(unsigned short)
307 ReadBlobLSBShort(image);
308 icon_file.directory[i].size=ReadBlobLSBLong(image);
309 icon_file.directory[i].offset=ReadBlobLSBLong(image);
312 for (i=0; i < icon_file.count; i++)
315 Verify Icon identifier.
317 offset=(ssize_t) SeekBlob(image,(MagickOffsetType)
318 icon_file.directory[i].offset,SEEK_SET);
320 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
321 icon_info.size=ReadBlobLSBLong(image);
322 icon_info.width=(unsigned char) ((int) ReadBlobLSBLong(image));
323 icon_info.height=(unsigned char) ((int) ReadBlobLSBLong(image)/2);
324 icon_info.planes=ReadBlobLSBShort(image);
325 icon_info.bits_per_pixel=ReadBlobLSBShort(image);
326 if (((icon_info.planes == 18505) && (icon_info.bits_per_pixel == 21060)) ||
327 (icon_info.size == 0x474e5089))
342 Icon image encoded as a compressed PNG image.
344 length=icon_file.directory[i].size;
345 png=(unsigned char *) AcquireQuantumMemory(length+16,sizeof(*png));
346 if (png == (unsigned char *) NULL)
347 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
348 (void) CopyMagickMemory(png,"\211PNG\r\n\032\n\000\000\000\015",12);
349 png[12]=(unsigned char) icon_info.planes;
350 png[13]=(unsigned char) (icon_info.planes >> 8);
351 png[14]=(unsigned char) icon_info.bits_per_pixel;
352 png[15]=(unsigned char) (icon_info.bits_per_pixel >> 8);
353 count=ReadBlob(image,length-16,png+16);
354 icon_image=(Image *) NULL;
357 read_info=CloneImageInfo(image_info);
358 (void) CopyMagickString(read_info->magick,"PNG",MaxTextExtent);
359 icon_image=BlobToImage(read_info,png,length+16,exception);
360 read_info=DestroyImageInfo(read_info);
362 png=(unsigned char *) RelinquishMagickMemory(png);
363 if (icon_image == (Image *) NULL)
365 if (count != (ssize_t) (length-16))
366 ThrowReaderException(CorruptImageError,
367 "InsufficientImageDataInFile");
368 image=DestroyImageList(image);
369 return((Image *) NULL);
371 DestroyBlob(icon_image);
372 icon_image->blob=ReferenceBlob(image->blob);
373 ReplaceImageInList(&image,icon_image);
377 if (icon_info.bits_per_pixel > 32)
378 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
379 icon_info.compression=ReadBlobLSBLong(image);
380 icon_info.image_size=ReadBlobLSBLong(image);
381 icon_info.x_pixels=ReadBlobLSBLong(image);
382 icon_info.y_pixels=ReadBlobLSBLong(image);
383 icon_info.number_colors=ReadBlobLSBLong(image);
384 icon_info.colors_important=ReadBlobLSBLong(image);
385 image->alpha_trait=BlendPixelTrait;
386 image->columns=(size_t) icon_file.directory[i].width;
387 if ((ssize_t) image->columns > icon_info.width)
388 image->columns=(size_t) icon_info.width;
389 if (image->columns == 0)
391 image->rows=(size_t) icon_file.directory[i].height;
392 if ((ssize_t) image->rows > icon_info.height)
393 image->rows=(size_t) icon_info.height;
394 if (image->rows == 0)
396 image->depth=icon_info.bits_per_pixel;
397 if (image->debug != MagickFalse)
399 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
400 " scene = %.20g",(double) i);
401 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
402 " size = %.20g",(double) icon_info.size);
403 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
404 " width = %.20g",(double) icon_file.directory[i].width);
405 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
406 " height = %.20g",(double) icon_file.directory[i].height);
407 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
408 " colors = %.20g",(double ) icon_info.number_colors);
409 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
410 " planes = %.20g",(double) icon_info.planes);
411 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
412 " bpp = %.20g",(double) icon_info.bits_per_pixel);
414 if (icon_info.bits_per_pixel <= 16)
416 image->storage_class=PseudoClass;
417 image->colors=icon_info.number_colors;
418 if (image->colors == 0)
419 image->colors=one << icon_info.bits_per_pixel;
421 if (image->storage_class == PseudoClass)
430 Read Icon raster colormap.
432 if (AcquireImageColormap(image,image->colors,exception) ==
434 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
435 icon_colormap=(unsigned char *) AcquireQuantumMemory((size_t)
436 image->colors,4UL*sizeof(*icon_colormap));
437 if (icon_colormap == (unsigned char *) NULL)
438 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
439 count=ReadBlob(image,(size_t) (4*image->colors),icon_colormap);
440 if (count != (ssize_t) (4*image->colors))
441 ThrowReaderException(CorruptImageError,
442 "InsufficientImageDataInFile");
444 for (i=0; i < (ssize_t) image->colors; i++)
446 image->colormap[i].blue=(Quantum) ScaleCharToQuantum(*p++);
447 image->colormap[i].green=(Quantum) ScaleCharToQuantum(*p++);
448 image->colormap[i].red=(Quantum) ScaleCharToQuantum(*p++);
451 icon_colormap=(unsigned char *) RelinquishMagickMemory(icon_colormap);
454 Convert Icon raster image to pixel packets.
456 if ((image_info->ping != MagickFalse) &&
457 (image_info->number_scenes != 0))
458 if (image->scene >= (image_info->scene+image_info->number_scenes-1))
460 bytes_per_line=(((image->columns*icon_info.bits_per_pixel)+31) &
462 (void) bytes_per_line;
463 scanline_pad=((((image->columns*icon_info.bits_per_pixel)+31) & ~31)-
464 (image->columns*icon_info.bits_per_pixel)) >> 3;
465 switch (icon_info.bits_per_pixel)
470 Convert bitmap scanline.
472 for (y=(ssize_t) image->rows-1; y >= 0; y--)
474 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
475 if (q == (Quantum *) NULL)
477 for (x=0; x < (ssize_t) (image->columns-7); x+=8)
479 byte=(size_t) ReadBlobByte(image);
480 for (bit=0; bit < 8; bit++)
482 SetPixelIndex(image,((byte & (0x80 >> bit)) != 0 ? 0x01 :
484 q+=GetPixelChannels(image);
487 if ((image->columns % 8) != 0)
489 byte=(size_t) ReadBlobByte(image);
490 for (bit=0; bit < (image->columns % 8); bit++)
492 SetPixelIndex(image,((byte & (0x80 >> bit)) != 0 ? 0x01 :
494 q+=GetPixelChannels(image);
497 for (x=0; x < (ssize_t) scanline_pad; x++)
498 (void) ReadBlobByte(image);
499 if (SyncAuthenticPixels(image,exception) == MagickFalse)
501 if (image->previous == (Image *) NULL)
503 status=SetImageProgress(image,LoadImageTag,image->rows-y-1,
505 if (status == MagickFalse)
514 Read 4-bit Icon scanline.
516 for (y=(ssize_t) image->rows-1; y >= 0; y--)
518 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
519 if (q == (Quantum *) NULL)
521 for (x=0; x < ((ssize_t) image->columns-1); x+=2)
523 byte=(size_t) ReadBlobByte(image);
524 SetPixelIndex(image,((byte >> 4) & 0xf),q);
525 q+=GetPixelChannels(image);
526 SetPixelIndex(image,((byte) & 0xf),q);
527 q+=GetPixelChannels(image);
529 if ((image->columns % 2) != 0)
531 byte=(size_t) ReadBlobByte(image);
532 SetPixelIndex(image,((byte >> 4) & 0xf),q);
533 q+=GetPixelChannels(image);
535 for (x=0; x < (ssize_t) scanline_pad; x++)
536 (void) ReadBlobByte(image);
537 if (SyncAuthenticPixels(image,exception) == MagickFalse)
539 if (image->previous == (Image *) NULL)
541 status=SetImageProgress(image,LoadImageTag,image->rows-y-1,
543 if (status == MagickFalse)
552 Convert PseudoColor scanline.
554 for (y=(ssize_t) image->rows-1; y >= 0; y--)
556 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
557 if (q == (Quantum *) NULL)
559 for (x=0; x < (ssize_t) image->columns; x++)
561 byte=(size_t) ReadBlobByte(image);
562 SetPixelIndex(image,byte,q);
563 q+=GetPixelChannels(image);
565 for (x=0; x < (ssize_t) scanline_pad; x++)
566 (void) ReadBlobByte(image);
567 if (SyncAuthenticPixels(image,exception) == MagickFalse)
569 if (image->previous == (Image *) NULL)
571 status=SetImageProgress(image,LoadImageTag,image->rows-y-1,
573 if (status == MagickFalse)
582 Convert PseudoColor scanline.
584 for (y=(ssize_t) image->rows-1; y >= 0; y--)
586 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
587 if (q == (Quantum *) NULL)
589 for (x=0; x < (ssize_t) image->columns; x++)
591 byte=(size_t) ReadBlobByte(image);
592 byte|=(size_t) (ReadBlobByte(image) << 8);
593 SetPixelIndex(image,byte,q);
594 q+=GetPixelChannels(image);
596 for (x=0; x < (ssize_t) scanline_pad; x++)
597 (void) ReadBlobByte(image);
598 if (SyncAuthenticPixels(image,exception) == MagickFalse)
600 if (image->previous == (Image *) NULL)
602 status=SetImageProgress(image,LoadImageTag,image->rows-y-1,
604 if (status == MagickFalse)
614 Convert DirectColor scanline.
616 for (y=(ssize_t) image->rows-1; y >= 0; y--)
618 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
619 if (q == (Quantum *) NULL)
621 for (x=0; x < (ssize_t) image->columns; x++)
623 SetPixelBlue(image,ScaleCharToQuantum((unsigned char)
624 ReadBlobByte(image)),q);
625 SetPixelGreen(image,ScaleCharToQuantum((unsigned char)
626 ReadBlobByte(image)),q);
627 SetPixelRed(image,ScaleCharToQuantum((unsigned char)
628 ReadBlobByte(image)),q);
629 if (icon_info.bits_per_pixel == 32)
630 SetPixelAlpha(image,ScaleCharToQuantum((unsigned char)
631 ReadBlobByte(image)),q);
632 q+=GetPixelChannels(image);
634 if (icon_info.bits_per_pixel == 24)
635 for (x=0; x < (ssize_t) scanline_pad; x++)
636 (void) ReadBlobByte(image);
637 if (SyncAuthenticPixels(image,exception) == MagickFalse)
639 if (image->previous == (Image *) NULL)
641 status=SetImageProgress(image,LoadImageTag,image->rows-y-1,
643 if (status == MagickFalse)
650 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
652 if (image_info->ping == MagickFalse)
653 (void) SyncImage(image,exception);
654 if (icon_info.bits_per_pixel != 32)
657 Read the ICON alpha mask.
659 image->storage_class=DirectClass;
660 for (y=(ssize_t) image->rows-1; y >= 0; y--)
662 q=GetAuthenticPixels(image,0,y,image->columns,1,exception);
663 if (q == (Quantum *) NULL)
665 for (x=0; x < ((ssize_t) image->columns-7); x+=8)
667 byte=(size_t) ReadBlobByte(image);
668 for (bit=0; bit < 8; bit++)
670 SetPixelAlpha(image,(((byte & (0x80 >> bit)) != 0) ?
671 TransparentAlpha : OpaqueAlpha),q);
672 q+=GetPixelChannels(image);
675 if ((image->columns % 8) != 0)
677 byte=(size_t) ReadBlobByte(image);
678 for (bit=0; bit < (image->columns % 8); bit++)
680 SetPixelAlpha(image,(((byte & (0x80 >> bit)) != 0) ?
681 TransparentAlpha : OpaqueAlpha),q);
682 q+=GetPixelChannels(image);
685 if ((image->columns % 32) != 0)
686 for (x=0; x < (ssize_t) ((32-(image->columns % 32))/8); x++)
687 (void) ReadBlobByte(image);
688 if (SyncAuthenticPixels(image,exception) == MagickFalse)
692 if (EOFBlob(image) != MagickFalse)
694 ThrowFileException(exception,CorruptImageError,
695 "UnexpectedEndOfFile",image->filename);
700 Proceed to next image.
702 if (image_info->number_scenes != 0)
703 if (image->scene >= (image_info->scene+image_info->number_scenes-1))
705 if (i < (ssize_t) (icon_file.count-1))
708 Allocate next image structure.
710 AcquireNextImage(image_info,image,exception);
711 if (GetNextImageInList(image) == (Image *) NULL)
713 image=DestroyImageList(image);
714 return((Image *) NULL);
716 image=SyncNextImageInList(image);
717 status=SetImageProgress(image,LoadImagesTag,TellBlob(image),
719 if (status == MagickFalse)
723 (void) CloseBlob(image);
724 return(GetFirstImageInList(image));
728 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
732 % R e g i s t e r I C O N I m a g e %
736 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
738 % RegisterICONImage() adds attributes for the Icon image format to
739 % the list of supported formats. The attributes include the image format
740 % tag, a method to read and/or write the format, whether the format
741 % supports the saving of more than one frame to the same file or blob,
742 % whether the format supports native in-memory I/O, and a brief
743 % description of the format.
745 % The format of the RegisterICONImage method is:
747 % size_t RegisterICONImage(void)
750 ModuleExport size_t RegisterICONImage(void)
755 entry=SetMagickInfo("CUR");
756 entry->decoder=(DecodeImageHandler *) ReadICONImage;
757 entry->encoder=(EncodeImageHandler *) WriteICONImage;
758 entry->adjoin=MagickFalse;
759 entry->seekable_stream=MagickTrue;
760 entry->description=ConstantString("Microsoft icon");
761 entry->module=ConstantString("CUR");
762 (void) RegisterMagickInfo(entry);
763 entry=SetMagickInfo("ICO");
764 entry->decoder=(DecodeImageHandler *) ReadICONImage;
765 entry->encoder=(EncodeImageHandler *) WriteICONImage;
766 entry->adjoin=MagickTrue;
767 entry->seekable_stream=MagickTrue;
768 entry->description=ConstantString("Microsoft icon");
769 entry->module=ConstantString("ICON");
770 (void) RegisterMagickInfo(entry);
771 entry=SetMagickInfo("ICON");
772 entry->decoder=(DecodeImageHandler *) ReadICONImage;
773 entry->encoder=(EncodeImageHandler *) WriteICONImage;
774 entry->adjoin=MagickFalse;
775 entry->seekable_stream=MagickTrue;
776 entry->description=ConstantString("Microsoft icon");
777 entry->module=ConstantString("ICON");
778 (void) RegisterMagickInfo(entry);
779 return(MagickImageCoderSignature);
783 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
787 % U n r e g i s t e r I C O N I m a g e %
791 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
793 % UnregisterICONImage() removes format registrations made by the
794 % ICON module from the list of supported formats.
796 % The format of the UnregisterICONImage method is:
798 % UnregisterICONImage(void)
801 ModuleExport void UnregisterICONImage(void)
803 (void) UnregisterMagickInfo("CUR");
804 (void) UnregisterMagickInfo("ICO");
805 (void) UnregisterMagickInfo("ICON");
809 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
813 % W r i t e I C O N I m a g e %
817 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
819 % WriteICONImage() writes an image in Microsoft Windows bitmap encoded
820 % image format, version 3 for Windows or (if the image has a matte channel)
823 % It encodes any subimage as a compressed PNG image ("BI_PNG)", only when its
824 % dimensions are 256x256 and image->compression is undefined or is defined as
827 % The format of the WriteICONImage method is:
829 % MagickBooleanType WriteICONImage(const ImageInfo *image_info,
830 % Image *image,ExceptionInfo *exception)
832 % A description of each parameter follows.
834 % o image_info: the image info.
836 % o image: The image.
838 % o exception: return any errors or warnings in this structure.
841 static MagickBooleanType WriteICONImage(const ImageInfo *image_info,
842 Image *image,ExceptionInfo *exception)
864 register const Quantum
871 register unsigned char
887 Open output image file.
889 assert(image_info != (const ImageInfo *) NULL);
890 assert(image_info->signature == MagickSignature);
891 assert(image != (Image *) NULL);
892 assert(image->signature == MagickSignature);
893 (void) LogMagickEvent(CoderEvent,GetMagickModule(),"%s",image->filename);
894 assert(exception != (ExceptionInfo *) NULL);
895 assert(exception->signature == MagickSignature);
896 status=OpenBlob(image_info,image,WriteBinaryBlobMode,exception);
897 if (status == MagickFalse)
899 images=(Image *) NULL;
900 option=GetImageOption(image_info,"icon:auto-resize");
901 if (option != (const char *) NULL)
903 images=AutoResizeImage(image,option,&scene,exception);
904 if (images == (Image *) NULL)
905 ThrowWriterException(ImageError,"InvalidDimensions");
913 if ((image->columns > 256L) || (image->rows > 256L))
914 ThrowWriterException(ImageError,"WidthOrHeightExceedsLimit");
916 next=SyncNextImageInList(next);
917 } while ((next != (Image *) NULL) && (image_info->adjoin != MagickFalse));
920 Dump out a ICON header template to be properly initialized later.
922 (void) WriteBlobLSBShort(image,0);
923 (void) WriteBlobLSBShort(image,1);
924 (void) WriteBlobLSBShort(image,(unsigned char) scene);
925 (void) ResetMagickMemory(&icon_file,0,sizeof(icon_file));
926 (void) ResetMagickMemory(&icon_info,0,sizeof(icon_info));
928 next=(images != (Image *) NULL) ? images : image;
931 (void) WriteBlobByte(image,icon_file.directory[scene].width);
932 (void) WriteBlobByte(image,icon_file.directory[scene].height);
933 (void) WriteBlobByte(image,icon_file.directory[scene].colors);
934 (void) WriteBlobByte(image,icon_file.directory[scene].reserved);
935 (void) WriteBlobLSBShort(image,icon_file.directory[scene].planes);
936 (void) WriteBlobLSBShort(image,icon_file.directory[scene].bits_per_pixel);
937 (void) WriteBlobLSBLong(image,(unsigned int)
938 icon_file.directory[scene].size);
939 (void) WriteBlobLSBLong(image,(unsigned int)
940 icon_file.directory[scene].offset);
942 next=SyncNextImageInList(next);
943 } while ((next != (Image *) NULL) && (image_info->adjoin != MagickFalse));
945 next=(images != (Image *) NULL) ? images : image;
948 if ((next->columns > 255L) && (next->rows > 255L) &&
949 ((next->compression == UndefinedCompression) ||
950 (next->compression == ZipCompression)))
964 write_image=CloneImage(next,0,0,MagickTrue,exception);
965 if (write_image == (Image *) NULL)
967 images=DestroyImageList(images);
970 write_info=CloneImageInfo(image_info);
971 (void) CopyMagickString(write_info->filename,"PNG:",MaxTextExtent);
973 /* Don't write any ancillary chunks except for gAMA */
974 (void) SetImageArtifact(write_image,"png:include-chunk","none,gama");
976 /* Only write PNG32 formatted PNG (32-bit RGBA), 8 bits per channel */
977 (void) SetImageArtifact(write_image,"png:format","png32");
979 png=(unsigned char *) ImageToBlob(write_info,write_image,&length,
981 write_image=DestroyImage(write_image);
982 write_info=DestroyImageInfo(write_info);
983 if (png == (unsigned char *) NULL)
985 images=DestroyImageList(images);
988 icon_file.directory[scene].width=0;
989 icon_file.directory[scene].height=0;
990 icon_file.directory[scene].colors=0;
991 icon_file.directory[scene].reserved=0;
992 icon_file.directory[scene].planes=1;
993 icon_file.directory[scene].bits_per_pixel=32;
994 icon_file.directory[scene].size=(size_t) length;
995 icon_file.directory[scene].offset=(size_t) TellBlob(image);
996 (void) WriteBlob(image,(size_t) length,png);
997 png=(unsigned char *) RelinquishMagickMemory(png);
1002 Initialize ICON raster file header.
1004 (void) TransformImageColorspace(next,sRGBColorspace,exception);
1005 icon_info.file_size=14+12+28;
1006 icon_info.offset_bits=icon_info.file_size;
1007 icon_info.compression=BI_RGB;
1008 if ((next->storage_class != DirectClass) && (next->colors > 256))
1009 (void) SetImageStorageClass(next,DirectClass,exception);
1010 if (next->storage_class == DirectClass)
1013 Full color ICON raster.
1015 icon_info.number_colors=0;
1016 icon_info.bits_per_pixel=32;
1017 icon_info.compression=(size_t) BI_RGB;
1025 Colormapped ICON raster.
1027 icon_info.bits_per_pixel=8;
1028 if (next->colors <= 256)
1029 icon_info.bits_per_pixel=8;
1030 if (next->colors <= 16)
1031 icon_info.bits_per_pixel=4;
1032 if (next->colors <= 2)
1033 icon_info.bits_per_pixel=1;
1035 icon_info.number_colors=one << icon_info.bits_per_pixel;
1036 if (icon_info.number_colors < next->colors)
1038 (void) SetImageStorageClass(next,DirectClass,exception);
1039 icon_info.number_colors=0;
1040 icon_info.bits_per_pixel=(unsigned short) 24;
1041 icon_info.compression=(size_t) BI_RGB;
1049 icon_info.file_size+=3*(one << icon_info.bits_per_pixel);
1050 icon_info.offset_bits+=3*(one << icon_info.bits_per_pixel);
1051 icon_info.file_size+=(one << icon_info.bits_per_pixel);
1052 icon_info.offset_bits+=(one << icon_info.bits_per_pixel);
1055 bytes_per_line=(((next->columns*icon_info.bits_per_pixel)+31) &
1057 icon_info.ba_offset=0;
1058 icon_info.width=(ssize_t) next->columns;
1059 icon_info.height=(ssize_t) next->rows;
1061 icon_info.image_size=bytes_per_line*next->rows;
1063 icon_info.size+=(4*icon_info.number_colors);
1064 icon_info.size+=icon_info.image_size;
1065 icon_info.size+=(((icon_info.width+31) & ~31) >> 3)*icon_info.height;
1066 icon_info.file_size+=icon_info.image_size;
1067 icon_info.x_pixels=0;
1068 icon_info.y_pixels=0;
1069 switch (next->units)
1071 case UndefinedResolution:
1072 case PixelsPerInchResolution:
1074 icon_info.x_pixels=(size_t) (100.0*next->resolution.x/2.54);
1075 icon_info.y_pixels=(size_t) (100.0*next->resolution.y/2.54);
1078 case PixelsPerCentimeterResolution:
1080 icon_info.x_pixels=(size_t) (100.0*next->resolution.x);
1081 icon_info.y_pixels=(size_t) (100.0*next->resolution.y);
1085 icon_info.colors_important=icon_info.number_colors;
1087 Convert MIFF to ICON raster pixels.
1089 pixels=(unsigned char *) AcquireQuantumMemory((size_t)
1090 icon_info.image_size,sizeof(*pixels));
1091 if (pixels == (unsigned char *) NULL)
1093 images=DestroyImageList(images);
1094 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
1096 (void) ResetMagickMemory(pixels,0,(size_t) icon_info.image_size);
1097 switch (icon_info.bits_per_pixel)
1106 Convert PseudoClass image to a ICON monochrome image.
1108 for (y=0; y < (ssize_t) next->rows; y++)
1110 p=GetVirtualPixels(next,0,y,next->columns,1,exception);
1111 if (p == (const Quantum *) NULL)
1113 q=pixels+(next->rows-y-1)*bytes_per_line;
1116 for (x=0; x < (ssize_t) next->columns; x++)
1119 byte|=GetPixelIndex(next,p) != 0 ? 0x01 : 0x00;
1123 *q++=(unsigned char) byte;
1127 p+=GetPixelChannels(image);
1130 *q++=(unsigned char) (byte << (8-bit));
1131 if (next->previous == (Image *) NULL)
1133 status=SetImageProgress(next,SaveImageTag,y,next->rows);
1134 if (status == MagickFalse)
1147 Convert PseudoClass image to a ICON monochrome image.
1149 for (y=0; y < (ssize_t) next->rows; y++)
1151 p=GetVirtualPixels(next,0,y,next->columns,1,exception);
1152 if (p == (const Quantum *) NULL)
1154 q=pixels+(next->rows-y-1)*bytes_per_line;
1157 for (x=0; x < (ssize_t) next->columns; x++)
1160 byte|=((size_t) GetPixelIndex(next,p) & 0x0f);
1164 *q++=(unsigned char) byte;
1168 p+=GetPixelChannels(image);
1171 *q++=(unsigned char) (byte << 4);
1172 if (next->previous == (Image *) NULL)
1174 status=SetImageProgress(next,SaveImageTag,y,next->rows);
1175 if (status == MagickFalse)
1184 Convert PseudoClass packet to ICON pixel.
1186 for (y=0; y < (ssize_t) next->rows; y++)
1188 p=GetVirtualPixels(next,0,y,next->columns,1,exception);
1189 if (p == (const Quantum *) NULL)
1191 q=pixels+(next->rows-y-1)*bytes_per_line;
1192 for (x=0; x < (ssize_t) next->columns; x++)
1194 *q++=(unsigned char) GetPixelIndex(next,p);
1195 p+=GetPixelChannels(image);
1197 if (next->previous == (Image *) NULL)
1199 status=SetImageProgress(next,SaveImageTag,y,next->rows);
1200 if (status == MagickFalse)
1210 Convert DirectClass packet to ICON BGR888 or BGRA8888 pixel.
1212 for (y=0; y < (ssize_t) next->rows; y++)
1214 p=GetVirtualPixels(next,0,y,next->columns,1,exception);
1215 if (p == (const Quantum *) NULL)
1217 q=pixels+(next->rows-y-1)*bytes_per_line;
1218 for (x=0; x < (ssize_t) next->columns; x++)
1220 *q++=ScaleQuantumToChar(GetPixelBlue(next,p));
1221 *q++=ScaleQuantumToChar(GetPixelGreen(next,p));
1222 *q++=ScaleQuantumToChar(GetPixelRed(next,p));
1223 if (next->alpha_trait != BlendPixelTrait)
1224 *q++=ScaleQuantumToChar(QuantumRange);
1226 *q++=ScaleQuantumToChar(GetPixelAlpha(next,p));
1227 p+=GetPixelChannels(next);
1229 if (icon_info.bits_per_pixel == 24)
1230 for (x=3L*(ssize_t) next->columns; x < (ssize_t) bytes_per_line; x++)
1232 if (next->previous == (Image *) NULL)
1234 status=SetImageProgress(next,SaveImageTag,y,next->rows);
1235 if (status == MagickFalse)
1243 Write 40-byte version 3+ bitmap header.
1245 icon_file.directory[scene].width=(unsigned char) icon_info.width;
1246 icon_file.directory[scene].height=(unsigned char) icon_info.height;
1247 icon_file.directory[scene].colors=(unsigned char)
1248 icon_info.number_colors;
1249 icon_file.directory[scene].reserved=0;
1250 icon_file.directory[scene].planes=icon_info.planes;
1251 icon_file.directory[scene].bits_per_pixel=icon_info.bits_per_pixel;
1252 icon_file.directory[scene].size=icon_info.size;
1253 icon_file.directory[scene].offset=(size_t) TellBlob(image);
1254 (void) WriteBlobLSBLong(image,(unsigned int) 40);
1255 (void) WriteBlobLSBLong(image,(unsigned int) icon_info.width);
1256 (void) WriteBlobLSBLong(image,(unsigned int) icon_info.height*2);
1257 (void) WriteBlobLSBShort(image,icon_info.planes);
1258 (void) WriteBlobLSBShort(image,icon_info.bits_per_pixel);
1259 (void) WriteBlobLSBLong(image,(unsigned int) icon_info.compression);
1260 (void) WriteBlobLSBLong(image,(unsigned int) icon_info.image_size);
1261 (void) WriteBlobLSBLong(image,(unsigned int) icon_info.x_pixels);
1262 (void) WriteBlobLSBLong(image,(unsigned int) icon_info.y_pixels);
1263 (void) WriteBlobLSBLong(image,(unsigned int) icon_info.number_colors);
1264 (void) WriteBlobLSBLong(image,(unsigned int) icon_info.colors_important);
1265 if (next->storage_class == PseudoClass)
1271 Dump colormap to file.
1273 icon_colormap=(unsigned char *) AcquireQuantumMemory((size_t)
1274 (1UL << icon_info.bits_per_pixel),4UL*sizeof(*icon_colormap));
1275 if (icon_colormap == (unsigned char *) NULL)
1277 images=DestroyImageList(images);
1278 ThrowWriterException(ResourceLimitError,
1279 "MemoryAllocationFailed");
1282 for (i=0; i < (ssize_t) next->colors; i++)
1284 *q++=ScaleQuantumToChar(next->colormap[i].blue);
1285 *q++=ScaleQuantumToChar(next->colormap[i].green);
1286 *q++=ScaleQuantumToChar(next->colormap[i].red);
1287 *q++=(unsigned char) 0x0;
1289 for ( ; i < (ssize_t) (1UL << icon_info.bits_per_pixel); i++)
1291 *q++=(unsigned char) 0x00;
1292 *q++=(unsigned char) 0x00;
1293 *q++=(unsigned char) 0x00;
1294 *q++=(unsigned char) 0x00;
1296 (void) WriteBlob(image,(size_t) (4UL*(1UL <<
1297 icon_info.bits_per_pixel)),icon_colormap);
1298 icon_colormap=(unsigned char *) RelinquishMagickMemory(
1301 (void) WriteBlob(image,(size_t) icon_info.image_size,pixels);
1302 pixels=(unsigned char *) RelinquishMagickMemory(pixels);
1306 scanline_pad=(((next->columns+31) & ~31)-next->columns) >> 3;
1307 for (y=((ssize_t) next->rows - 1); y >= 0; y--)
1309 p=GetVirtualPixels(next,0,y,next->columns,1,exception);
1310 if (p == (const Quantum *) NULL)
1314 for (x=0; x < (ssize_t) next->columns; x++)
1317 if ((next->alpha_trait == BlendPixelTrait) &&
1318 (GetPixelAlpha(next,p) == (Quantum) TransparentAlpha))
1323 (void) WriteBlobByte(image,(unsigned char) byte);
1327 p+=GetPixelChannels(next);
1330 (void) WriteBlobByte(image,(unsigned char) (byte << (8-bit)));
1331 for (i=0; i < (ssize_t) scanline_pad; i++)
1332 (void) WriteBlobByte(image,(unsigned char) 0);
1335 if (GetNextImageInList(next) == (Image *) NULL)
1337 next=SyncNextImageInList(next);
1338 status=SetImageProgress(next,SaveImagesTag,scene++,
1339 GetImageListLength(next));
1340 if (status == MagickFalse)
1342 } while ((next != (Image *) NULL) && (image_info->adjoin != MagickFalse));
1343 offset=SeekBlob(image,0,SEEK_SET);
1345 (void) WriteBlobLSBShort(image,0);
1346 (void) WriteBlobLSBShort(image,1);
1347 (void) WriteBlobLSBShort(image,(unsigned short) (scene+1));
1349 next=(images != (Image *) NULL) ? images : image;
1352 (void) WriteBlobByte(image,icon_file.directory[scene].width);
1353 (void) WriteBlobByte(image,icon_file.directory[scene].height);
1354 (void) WriteBlobByte(image,icon_file.directory[scene].colors);
1355 (void) WriteBlobByte(image,icon_file.directory[scene].reserved);
1356 (void) WriteBlobLSBShort(image,icon_file.directory[scene].planes);
1357 (void) WriteBlobLSBShort(image,icon_file.directory[scene].bits_per_pixel);
1358 (void) WriteBlobLSBLong(image,(unsigned int)
1359 icon_file.directory[scene].size);
1360 (void) WriteBlobLSBLong(image,(unsigned int)
1361 icon_file.directory[scene].offset);
1363 next=SyncNextImageInList(next);
1364 } while ((next != (Image *) NULL) && (image_info->adjoin != MagickFalse));
1365 (void) CloseBlob(image);
1366 images=DestroyImageList(images);