2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
10 % IIIII CCCC OOO N N %
13 % Read Microsoft Windows Icon Format %
20 % Copyright 1999-2012 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/exception.h"
50 #include "MagickCore/exception-private.h"
51 #include "MagickCore/image.h"
52 #include "MagickCore/image-private.h"
53 #include "MagickCore/list.h"
54 #include "MagickCore/log.h"
55 #include "MagickCore/magick.h"
56 #include "MagickCore/memory_.h"
57 #include "MagickCore/monitor.h"
58 #include "MagickCore/monitor-private.h"
59 #include "MagickCore/nt-base-private.h"
60 #include "MagickCore/pixel-accessor.h"
61 #include "MagickCore/quantize.h"
62 #include "MagickCore/quantum-private.h"
63 #include "MagickCore/static.h"
64 #include "MagickCore/string_.h"
65 #include "MagickCore/module.h"
70 #if !defined(MAGICKCORE_WINDOWS_SUPPORT) || defined(__MINGW32__)
73 #define BI_BITFIELDS 3
80 typedef struct _IconEntry
97 typedef struct _IconFile
108 typedef struct _IconInfo
141 Forward declaractions.
143 static MagickBooleanType
144 WriteICONImage(const ImageInfo *,Image *,ExceptionInfo *);
147 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
151 % R e a d I C O N I m a g e %
155 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
157 % ReadICONImage() reads a Microsoft icon image file and returns it. It
158 % allocates the memory necessary for the new Image structure and returns a
159 % pointer to the new image.
161 % The format of the ReadICONImage method is:
163 % Image *ReadICONImage(const ImageInfo *image_info,
164 % ExceptionInfo *exception)
166 % A description of each parameter follows:
168 % o image_info: the image info.
170 % o exception: return any errors or warnings in this structure.
173 static Image *ReadICONImage(const ImageInfo *image_info,
174 ExceptionInfo *exception)
195 register unsigned char
213 assert(image_info != (const ImageInfo *) NULL);
214 assert(image_info->signature == MagickSignature);
215 (void) LogMagickEvent(CoderEvent,GetMagickModule(),"%s",image_info->filename);
216 assert(exception != (ExceptionInfo *) NULL);
217 assert(exception->signature == MagickSignature);
218 image=AcquireImage(image_info,exception);
219 status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception);
220 if (status == MagickFalse)
222 image=DestroyImageList(image);
223 return((Image *) NULL);
225 icon_file.reserved=(short) ReadBlobLSBShort(image);
226 icon_file.resource_type=(short) ReadBlobLSBShort(image);
227 icon_file.count=(short) ReadBlobLSBShort(image);
228 if ((icon_file.reserved != 0) ||
229 ((icon_file.resource_type != 1) && (icon_file.resource_type != 2)) ||
230 (icon_file.count > MaxIcons))
231 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
232 for (i=0; i < icon_file.count; i++)
234 icon_file.directory[i].width=(unsigned char) ReadBlobByte(image);
235 icon_file.directory[i].height=(unsigned char) ReadBlobByte(image);
236 icon_file.directory[i].colors=(unsigned char) ReadBlobByte(image);
237 icon_file.directory[i].reserved=(unsigned char) ReadBlobByte(image);
238 icon_file.directory[i].planes=(unsigned short) ReadBlobLSBShort(image);
239 icon_file.directory[i].bits_per_pixel=(unsigned short)
240 ReadBlobLSBShort(image);
241 icon_file.directory[i].size=ReadBlobLSBLong(image);
242 icon_file.directory[i].offset=ReadBlobLSBLong(image);
245 for (i=0; i < icon_file.count; i++)
248 Verify Icon identifier.
250 offset=(ssize_t) SeekBlob(image,(MagickOffsetType)
251 icon_file.directory[i].offset,SEEK_SET);
253 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
254 icon_info.size=ReadBlobLSBLong(image);
255 icon_info.width=(unsigned char) ((int) ReadBlobLSBLong(image));
256 icon_info.height=(unsigned char) ((int) ReadBlobLSBLong(image)/2);
257 icon_info.planes=ReadBlobLSBShort(image);
258 icon_info.bits_per_pixel=ReadBlobLSBShort(image);
259 if ((icon_info.planes == 18505) && (icon_info.bits_per_pixel == 21060))
274 Icon image encoded as a compressed PNG image.
276 length=icon_file.directory[i].size;
277 png=(unsigned char *) AcquireQuantumMemory(length+16,sizeof(*png));
278 if (png == (unsigned char *) NULL)
279 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
280 (void) CopyMagickMemory(png,"\211PNG\r\n\032\n\000\000\000\015",12);
281 png[12]=(unsigned char) icon_info.planes;
282 png[13]=(unsigned char) (icon_info.planes >> 8);
283 png[14]=(unsigned char) icon_info.bits_per_pixel;
284 png[15]=(unsigned char) (icon_info.bits_per_pixel >> 8);
285 count=ReadBlob(image,length-16,png+16);
286 if (count != (ssize_t) (length-16))
288 png=(unsigned char *) RelinquishMagickMemory(png);
289 ThrowReaderException(CorruptImageError,
290 "InsufficientImageDataInFile");
292 read_info=CloneImageInfo(image_info);
293 (void) CopyMagickString(read_info->magick,"PNG",MaxTextExtent);
294 icon_image=BlobToImage(read_info,png,length+16,exception);
295 read_info=DestroyImageInfo(read_info);
296 png=(unsigned char *) RelinquishMagickMemory(png);
297 if (icon_image == (Image *) NULL)
299 image=DestroyImageList(image);
300 return((Image *) NULL);
302 DestroyBlob(icon_image);
303 icon_image->blob=ReferenceBlob(image->blob);
304 ReplaceImageInList(&image,icon_image);
308 if (icon_info.bits_per_pixel > 32)
309 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
310 icon_info.compression=ReadBlobLSBLong(image);
311 icon_info.image_size=ReadBlobLSBLong(image);
312 icon_info.x_pixels=ReadBlobLSBLong(image);
313 icon_info.y_pixels=ReadBlobLSBLong(image);
314 icon_info.number_colors=ReadBlobLSBLong(image);
315 icon_info.colors_important=ReadBlobLSBLong(image);
316 image->matte=MagickTrue;
317 image->columns=(size_t) icon_file.directory[i].width;
318 if ((ssize_t) image->columns > icon_info.width)
319 image->columns=(size_t) icon_info.width;
320 if (image->columns == 0)
322 image->rows=(size_t) icon_file.directory[i].height;
323 if ((ssize_t) image->rows > icon_info.height)
324 image->rows=(size_t) icon_info.height;
325 if (image->rows == 0)
327 image->depth=icon_info.bits_per_pixel;
328 if (image->debug != MagickFalse)
330 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
331 " scene = %.20g",(double) i);
332 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
333 " size = %.20g",(double) icon_info.size);
334 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
335 " width = %.20g",(double) icon_file.directory[i].width);
336 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
337 " height = %.20g",(double) icon_file.directory[i].height);
338 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
339 " colors = %.20g",(double ) icon_info.number_colors);
340 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
341 " planes = %.20g",(double) icon_info.planes);
342 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
343 " bpp = %.20g",(double) icon_info.bits_per_pixel);
345 if ((icon_info.number_colors != 0) || (icon_info.bits_per_pixel <= 16))
347 image->storage_class=PseudoClass;
348 image->colors=icon_info.number_colors;
349 if (image->colors == 0)
350 image->colors=one << icon_info.bits_per_pixel;
352 if (image->storage_class == PseudoClass)
365 Read Icon raster colormap.
368 number_colors=one << icon_info.bits_per_pixel;
369 if (AcquireImageColormap(image,number_colors,exception) == MagickFalse)
370 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
371 icon_colormap=(unsigned char *) AcquireQuantumMemory((size_t)
372 image->colors,4UL*sizeof(*icon_colormap));
373 if (icon_colormap == (unsigned char *) NULL)
374 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
375 count=ReadBlob(image,(size_t) (4*image->colors),icon_colormap);
376 if (count != (ssize_t) (4*image->colors))
377 ThrowReaderException(CorruptImageError,
378 "InsufficientImageDataInFile");
380 for (i=0; i < (ssize_t) image->colors; i++)
382 image->colormap[i].blue=(Quantum) ScaleCharToQuantum(*p++);
383 image->colormap[i].green=(Quantum) ScaleCharToQuantum(*p++);
384 image->colormap[i].red=(Quantum) ScaleCharToQuantum(*p++);
387 icon_colormap=(unsigned char *) RelinquishMagickMemory(icon_colormap);
390 Convert Icon raster image to pixel packets.
392 if ((image_info->ping != MagickFalse) &&
393 (image_info->number_scenes != 0))
394 if (image->scene >= (image_info->scene+image_info->number_scenes-1))
396 bytes_per_line=(((image->columns*icon_info.bits_per_pixel)+31) &
398 (void) bytes_per_line;
399 scanline_pad=((((image->columns*icon_info.bits_per_pixel)+31) & ~31)-
400 (image->columns*icon_info.bits_per_pixel)) >> 3;
401 switch (icon_info.bits_per_pixel)
406 Convert bitmap scanline.
408 for (y=(ssize_t) image->rows-1; y >= 0; y--)
410 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
411 if (q == (Quantum *) NULL)
413 for (x=0; x < (ssize_t) (image->columns-7); x+=8)
415 byte=(size_t) ReadBlobByte(image);
416 for (bit=0; bit < 8; bit++)
418 SetPixelIndex(image,((byte & (0x80 >> bit)) != 0 ? 0x01 :
420 q+=GetPixelChannels(image);
423 if ((image->columns % 8) != 0)
425 byte=(size_t) ReadBlobByte(image);
426 for (bit=0; bit < (image->columns % 8); bit++)
428 SetPixelIndex(image,((byte & (0x80 >> bit)) != 0 ? 0x01 :
430 q+=GetPixelChannels(image);
433 for (x=0; x < (ssize_t) scanline_pad; x++)
434 (void) ReadBlobByte(image);
435 if (SyncAuthenticPixels(image,exception) == MagickFalse)
437 if (image->previous == (Image *) NULL)
439 status=SetImageProgress(image,LoadImageTag,image->rows-y-1,
441 if (status == MagickFalse)
450 Read 4-bit Icon scanline.
452 for (y=(ssize_t) image->rows-1; y >= 0; y--)
454 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
455 if (q == (Quantum *) NULL)
457 for (x=0; x < ((ssize_t) image->columns-1); x+=2)
459 byte=(size_t) ReadBlobByte(image);
460 SetPixelIndex(image,((byte >> 4) & 0xf),q);
461 q+=GetPixelChannels(image);
462 SetPixelIndex(image,((byte) & 0xf),q);
463 q+=GetPixelChannels(image);
465 if ((image->columns % 2) != 0)
467 byte=(size_t) ReadBlobByte(image);
468 SetPixelIndex(image,((byte >> 4) & 0xf),q);
469 q+=GetPixelChannels(image);
471 for (x=0; x < (ssize_t) scanline_pad; x++)
472 (void) ReadBlobByte(image);
473 if (SyncAuthenticPixels(image,exception) == MagickFalse)
475 if (image->previous == (Image *) NULL)
477 status=SetImageProgress(image,LoadImageTag,image->rows-y-1,
479 if (status == MagickFalse)
488 Convert PseudoColor scanline.
490 for (y=(ssize_t) image->rows-1; y >= 0; y--)
492 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
493 if (q == (Quantum *) NULL)
495 for (x=0; x < (ssize_t) image->columns; x++)
497 byte=(size_t) ReadBlobByte(image);
498 SetPixelIndex(image,byte,q);
499 q+=GetPixelChannels(image);
501 for (x=0; x < (ssize_t) scanline_pad; x++)
502 (void) ReadBlobByte(image);
503 if (SyncAuthenticPixels(image,exception) == MagickFalse)
505 if (image->previous == (Image *) NULL)
507 status=SetImageProgress(image,LoadImageTag,image->rows-y-1,
509 if (status == MagickFalse)
518 Convert PseudoColor scanline.
520 for (y=(ssize_t) image->rows-1; y >= 0; y--)
522 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
523 if (q == (Quantum *) NULL)
525 for (x=0; x < (ssize_t) image->columns; x++)
527 byte=(size_t) ReadBlobByte(image);
528 byte|=(size_t) (ReadBlobByte(image) << 8);
529 SetPixelIndex(image,byte,q);
530 q+=GetPixelChannels(image);
532 for (x=0; x < (ssize_t) scanline_pad; x++)
533 (void) ReadBlobByte(image);
534 if (SyncAuthenticPixels(image,exception) == MagickFalse)
536 if (image->previous == (Image *) NULL)
538 status=SetImageProgress(image,LoadImageTag,image->rows-y-1,
540 if (status == MagickFalse)
550 Convert DirectColor scanline.
552 for (y=(ssize_t) image->rows-1; y >= 0; y--)
554 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
555 if (q == (Quantum *) NULL)
557 for (x=0; x < (ssize_t) image->columns; x++)
559 SetPixelBlue(image,ScaleCharToQuantum((unsigned char)
560 ReadBlobByte(image)),q);
561 SetPixelGreen(image,ScaleCharToQuantum((unsigned char)
562 ReadBlobByte(image)),q);
563 SetPixelRed(image,ScaleCharToQuantum((unsigned char)
564 ReadBlobByte(image)),q);
565 if (icon_info.bits_per_pixel == 32)
566 SetPixelAlpha(image,ScaleCharToQuantum((unsigned char)
567 ReadBlobByte(image)),q);
568 q+=GetPixelChannels(image);
570 if (icon_info.bits_per_pixel == 24)
571 for (x=0; x < (ssize_t) scanline_pad; x++)
572 (void) ReadBlobByte(image);
573 if (SyncAuthenticPixels(image,exception) == MagickFalse)
575 if (image->previous == (Image *) NULL)
577 status=SetImageProgress(image,LoadImageTag,image->rows-y-1,
579 if (status == MagickFalse)
586 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
588 if (image_info->ping == MagickFalse)
589 (void) SyncImage(image,exception);
590 if (icon_info.bits_per_pixel != 32)
593 Read the ICON alpha mask.
595 image->storage_class=DirectClass;
596 for (y=(ssize_t) image->rows-1; y >= 0; y--)
598 q=GetAuthenticPixels(image,0,y,image->columns,1,exception);
599 if (q == (Quantum *) NULL)
601 for (x=0; x < ((ssize_t) image->columns-7); x+=8)
603 byte=(size_t) ReadBlobByte(image);
604 for (bit=0; bit < 8; bit++)
606 SetPixelAlpha(image,(((byte & (0x80 >> bit)) != 0) ?
607 TransparentAlpha : OpaqueAlpha),q);
608 q+=GetPixelChannels(image);
611 if ((image->columns % 8) != 0)
613 byte=(size_t) ReadBlobByte(image);
614 for (bit=0; bit < (image->columns % 8); bit++)
616 SetPixelAlpha(image,(((byte & (0x80 >> bit)) != 0) ?
617 TransparentAlpha : OpaqueAlpha),q);
618 q+=GetPixelChannels(image);
621 if ((image->columns % 32) != 0)
622 for (x=0; x < (ssize_t) ((32-(image->columns % 32))/8); x++)
623 (void) ReadBlobByte(image);
624 if (SyncAuthenticPixels(image,exception) == MagickFalse)
628 if (EOFBlob(image) != MagickFalse)
630 ThrowFileException(exception,CorruptImageError,
631 "UnexpectedEndOfFile",image->filename);
636 Proceed to next image.
638 if (image_info->number_scenes != 0)
639 if (image->scene >= (image_info->scene+image_info->number_scenes-1))
641 if (i < (ssize_t) (icon_file.count-1))
644 Allocate next image structure.
646 AcquireNextImage(image_info,image,exception);
647 if (GetNextImageInList(image) == (Image *) NULL)
649 image=DestroyImageList(image);
650 return((Image *) NULL);
652 image=SyncNextImageInList(image);
653 status=SetImageProgress(image,LoadImagesTag,TellBlob(image),
655 if (status == MagickFalse)
659 (void) CloseBlob(image);
660 return(GetFirstImageInList(image));
664 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
668 % R e g i s t e r I C O N I m a g e %
672 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
674 % RegisterICONImage() adds attributes for the Icon image format to
675 % the list of supported formats. The attributes include the image format
676 % tag, a method to read and/or write the format, whether the format
677 % supports the saving of more than one frame to the same file or blob,
678 % whether the format supports native in-memory I/O, and a brief
679 % description of the format.
681 % The format of the RegisterICONImage method is:
683 % size_t RegisterICONImage(void)
686 ModuleExport size_t RegisterICONImage(void)
691 entry=SetMagickInfo("CUR");
692 entry->decoder=(DecodeImageHandler *) ReadICONImage;
693 entry->encoder=(EncodeImageHandler *) WriteICONImage;
694 entry->adjoin=MagickFalse;
695 entry->seekable_stream=MagickTrue;
696 entry->description=ConstantString("Microsoft icon");
697 entry->module=ConstantString("CUR");
698 (void) RegisterMagickInfo(entry);
699 entry=SetMagickInfo("ICO");
700 entry->decoder=(DecodeImageHandler *) ReadICONImage;
701 entry->encoder=(EncodeImageHandler *) WriteICONImage;
702 entry->adjoin=MagickTrue;
703 entry->seekable_stream=MagickTrue;
704 entry->description=ConstantString("Microsoft icon");
705 entry->module=ConstantString("ICON");
706 (void) RegisterMagickInfo(entry);
707 entry=SetMagickInfo("ICON");
708 entry->decoder=(DecodeImageHandler *) ReadICONImage;
709 entry->encoder=(EncodeImageHandler *) WriteICONImage;
710 entry->adjoin=MagickFalse;
711 entry->seekable_stream=MagickTrue;
712 entry->description=ConstantString("Microsoft icon");
713 entry->module=ConstantString("ICON");
714 (void) RegisterMagickInfo(entry);
715 return(MagickImageCoderSignature);
719 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
723 % U n r e g i s t e r I C O N I m a g e %
727 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
729 % UnregisterICONImage() removes format registrations made by the
730 % ICON module from the list of supported formats.
732 % The format of the UnregisterICONImage method is:
734 % UnregisterICONImage(void)
737 ModuleExport void UnregisterICONImage(void)
739 (void) UnregisterMagickInfo("CUR");
740 (void) UnregisterMagickInfo("ICO");
741 (void) UnregisterMagickInfo("ICON");
745 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
749 % W r i t e I C O N I m a g e %
753 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
755 % WriteICONImage() writes an image in Microsoft Windows bitmap encoded
756 % image format, version 3 for Windows or (if the image has a matte channel)
759 % The format of the WriteICONImage method is:
761 % MagickBooleanType WriteICONImage(const ImageInfo *image_info,
762 % Image *image,ExceptionInfo *exception)
764 % A description of each parameter follows.
766 % o image_info: the image info.
768 % o image: The image.
770 % o exception: return any errors or warnings in this structure.
773 static MagickBooleanType WriteICONImage(const ImageInfo *image_info,
774 Image *image,ExceptionInfo *exception)
792 register const Quantum
799 register unsigned char
815 Open output image file.
817 assert(image_info != (const ImageInfo *) NULL);
818 assert(image_info->signature == MagickSignature);
819 assert(image != (Image *) NULL);
820 assert(image->signature == MagickSignature);
821 (void) LogMagickEvent(CoderEvent,GetMagickModule(),"%s",image->filename);
822 assert(exception != (ExceptionInfo *) NULL);
823 assert(exception->signature == MagickSignature);
824 status=OpenBlob(image_info,image,WriteBinaryBlobMode,exception);
825 if (status == MagickFalse)
831 if ((image->columns > 256L) || (image->rows > 256L))
832 ThrowWriterException(ImageError,"WidthOrHeightExceedsLimit");
834 next=SyncNextImageInList(next);
835 } while ((next != (Image *) NULL) && (image_info->adjoin != MagickFalse));
837 Dump out a ICON header template to be properly initialized later.
839 (void) WriteBlobLSBShort(image,0);
840 (void) WriteBlobLSBShort(image,1);
841 (void) WriteBlobLSBShort(image,(unsigned char) scene);
842 (void) ResetMagickMemory(&icon_file,0,sizeof(icon_file));
843 (void) ResetMagickMemory(&icon_info,0,sizeof(icon_info));
848 (void) WriteBlobByte(image,icon_file.directory[scene].width);
849 (void) WriteBlobByte(image,icon_file.directory[scene].height);
850 (void) WriteBlobByte(image,icon_file.directory[scene].colors);
851 (void) WriteBlobByte(image,icon_file.directory[scene].reserved);
852 (void) WriteBlobLSBShort(image,icon_file.directory[scene].planes);
853 (void) WriteBlobLSBShort(image,icon_file.directory[scene].bits_per_pixel);
854 (void) WriteBlobLSBLong(image,(unsigned int)
855 icon_file.directory[scene].size);
856 (void) WriteBlobLSBLong(image,(unsigned int)
857 icon_file.directory[scene].offset);
859 next=SyncNextImageInList(next);
860 } while ((next != (Image *) NULL) && (image_info->adjoin != MagickFalse));
865 if ((next->columns > 256L) && (next->rows > 256L) &&
866 (next->compression == ZipCompression))
881 Icon image encoded as a compressed PNG image.
883 write_image=CloneImage(next,0,0,MagickTrue,exception);
884 if (write_image == (Image *) NULL)
886 write_info=CloneImageInfo(image_info);
887 (void) CopyMagickString(write_info->filename,"PNG:",MaxTextExtent);
889 Don't write any ancillary chunks except for gAMA and tRNS.
891 (void) SetImageArtifact(write_image,"png:include-chunk",
893 png=(unsigned char *) ImageToBlob(write_info,write_image,&length,
895 write_image=DestroyImage(write_image);
896 write_info=DestroyImageInfo(write_info);
897 if (png == (unsigned char *) NULL)
899 icon_file.directory[scene].width=0;
900 icon_file.directory[scene].height=0;
901 icon_file.directory[scene].colors=0;
902 icon_file.directory[scene].reserved=0;
903 icon_file.directory[scene].planes=1;
904 icon_file.directory[scene].bits_per_pixel=32;
905 icon_file.directory[scene].size=(size_t) length;
906 icon_file.directory[scene].offset=(size_t) TellBlob(image);
907 (void) WriteBlob(image,(size_t) length,png);
908 png=(unsigned char *) RelinquishMagickMemory(png);
913 Initialize ICON raster file header.
915 if (next->colorspace != RGBColorspace)
916 (void) TransformImageColorspace(next,RGBColorspace,exception);
917 icon_info.file_size=14+12+28;
918 icon_info.offset_bits=icon_info.file_size;
919 icon_info.compression=BI_RGB;
920 if ((next->storage_class != DirectClass) && (next->colors > 256))
921 (void) SetImageStorageClass(next,DirectClass,exception);
922 if (next->storage_class == DirectClass)
925 Full color ICON raster.
927 icon_info.number_colors=0;
928 icon_info.bits_per_pixel=32;
929 icon_info.compression=(size_t) BI_RGB;
937 Colormapped ICON raster.
939 icon_info.bits_per_pixel=8;
940 if (next->colors <= 256)
941 icon_info.bits_per_pixel=8;
942 if (next->colors <= 16)
943 icon_info.bits_per_pixel=4;
944 if (next->colors <= 2)
945 icon_info.bits_per_pixel=1;
947 icon_info.number_colors=one << icon_info.bits_per_pixel;
948 if (icon_info.number_colors < next->colors)
950 (void) SetImageStorageClass(next,DirectClass,exception);
951 icon_info.number_colors=0;
952 icon_info.bits_per_pixel=(unsigned short) 24;
953 icon_info.compression=(size_t) BI_RGB;
961 icon_info.file_size+=3*(one << icon_info.bits_per_pixel);
962 icon_info.offset_bits+=3*(one << icon_info.bits_per_pixel);
963 icon_info.file_size+=(one << icon_info.bits_per_pixel);
964 icon_info.offset_bits+=(one << icon_info.bits_per_pixel);
967 bytes_per_line=(((next->columns*icon_info.bits_per_pixel)+31) &
969 icon_info.ba_offset=0;
970 icon_info.width=(ssize_t) next->columns;
971 icon_info.height=(ssize_t) next->rows;
973 icon_info.image_size=bytes_per_line*next->rows;
975 icon_info.size+=(4*icon_info.number_colors);
976 icon_info.size+=icon_info.image_size;
977 icon_info.size+=(((icon_info.width+31) & ~31) >> 3)*icon_info.height;
978 icon_info.file_size+=icon_info.image_size;
979 icon_info.x_pixels=0;
980 icon_info.y_pixels=0;
983 case UndefinedResolution:
984 case PixelsPerInchResolution:
986 icon_info.x_pixels=(size_t) (100.0*next->resolution.x/2.54);
987 icon_info.y_pixels=(size_t) (100.0*next->resolution.y/2.54);
990 case PixelsPerCentimeterResolution:
992 icon_info.x_pixels=(size_t) (100.0*next->resolution.x);
993 icon_info.y_pixels=(size_t) (100.0*next->resolution.y);
997 icon_info.colors_important=icon_info.number_colors;
999 Convert MIFF to ICON raster pixels.
1001 pixels=(unsigned char *) AcquireQuantumMemory((size_t)
1002 icon_info.image_size,sizeof(*pixels));
1003 if (pixels == (unsigned char *) NULL)
1004 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
1005 (void) ResetMagickMemory(pixels,0,(size_t) icon_info.image_size);
1006 switch (icon_info.bits_per_pixel)
1015 Convert PseudoClass image to a ICON monochrome image.
1017 for (y=0; y < (ssize_t) next->rows; y++)
1019 p=GetVirtualPixels(next,0,y,next->columns,1,exception);
1020 if (p == (const Quantum *) NULL)
1022 q=pixels+(next->rows-y-1)*bytes_per_line;
1025 for (x=0; x < (ssize_t) next->columns; x++)
1028 byte|=GetPixelIndex(next,p) != 0 ? 0x01 : 0x00;
1032 *q++=(unsigned char) byte;
1036 p+=GetPixelChannels(image);
1039 *q++=(unsigned char) (byte << (8-bit));
1040 if (next->previous == (Image *) NULL)
1042 status=SetImageProgress(next,SaveImageTag,y,next->rows);
1043 if (status == MagickFalse)
1056 Convert PseudoClass image to a ICON monochrome image.
1058 for (y=0; y < (ssize_t) next->rows; y++)
1060 p=GetVirtualPixels(next,0,y,next->columns,1,exception);
1061 if (p == (const Quantum *) NULL)
1063 q=pixels+(next->rows-y-1)*bytes_per_line;
1066 for (x=0; x < (ssize_t) next->columns; x++)
1069 byte|=((size_t) GetPixelIndex(next,p) & 0x0f);
1073 *q++=(unsigned char) byte;
1077 p+=GetPixelChannels(image);
1080 *q++=(unsigned char) (byte << 4);
1081 if (next->previous == (Image *) NULL)
1083 status=SetImageProgress(next,SaveImageTag,y,next->rows);
1084 if (status == MagickFalse)
1093 Convert PseudoClass packet to ICON pixel.
1095 for (y=0; y < (ssize_t) next->rows; y++)
1097 p=GetVirtualPixels(next,0,y,next->columns,1,exception);
1098 if (p == (const Quantum *) NULL)
1100 q=pixels+(next->rows-y-1)*bytes_per_line;
1101 for (x=0; x < (ssize_t) next->columns; x++)
1103 *q++=(unsigned char) GetPixelIndex(next,p);
1104 p+=GetPixelChannels(image);
1106 if (next->previous == (Image *) NULL)
1108 status=SetImageProgress(next,SaveImageTag,y,next->rows);
1109 if (status == MagickFalse)
1119 Convert DirectClass packet to ICON BGR888 or BGRA8888 pixel.
1121 for (y=0; y < (ssize_t) next->rows; y++)
1123 p=GetVirtualPixels(next,0,y,next->columns,1,exception);
1124 if (p == (const Quantum *) NULL)
1126 q=pixels+(next->rows-y-1)*bytes_per_line;
1127 for (x=0; x < (ssize_t) next->columns; x++)
1129 *q++=ScaleQuantumToChar(GetPixelBlue(next,p));
1130 *q++=ScaleQuantumToChar(GetPixelGreen(next,p));
1131 *q++=ScaleQuantumToChar(GetPixelRed(next,p));
1132 if (next->matte == MagickFalse)
1133 *q++=ScaleQuantumToChar(QuantumRange);
1135 *q++=ScaleQuantumToChar(GetPixelAlpha(next,p));
1136 p+=GetPixelChannels(next);
1138 if (icon_info.bits_per_pixel == 24)
1139 for (x=3L*(ssize_t) next->columns; x < (ssize_t) bytes_per_line; x++)
1141 if (next->previous == (Image *) NULL)
1143 status=SetImageProgress(next,SaveImageTag,y,next->rows);
1144 if (status == MagickFalse)
1152 Write 40-byte version 3+ bitmap header.
1154 icon_file.directory[scene].width=(unsigned char) icon_info.width;
1155 icon_file.directory[scene].height=(unsigned char) icon_info.height;
1156 icon_file.directory[scene].colors=(unsigned char)
1157 icon_info.number_colors;
1158 icon_file.directory[scene].reserved=0;
1159 icon_file.directory[scene].planes=icon_info.planes;
1160 icon_file.directory[scene].bits_per_pixel=icon_info.bits_per_pixel;
1161 icon_file.directory[scene].size=icon_info.size;
1162 icon_file.directory[scene].offset=(size_t) TellBlob(image);
1163 (void) WriteBlobLSBLong(image,(unsigned int) 40);
1164 (void) WriteBlobLSBLong(image,(unsigned int) icon_info.width);
1165 (void) WriteBlobLSBLong(image,(unsigned int) icon_info.height*2);
1166 (void) WriteBlobLSBShort(image,icon_info.planes);
1167 (void) WriteBlobLSBShort(image,icon_info.bits_per_pixel);
1168 (void) WriteBlobLSBLong(image,(unsigned int) icon_info.compression);
1169 (void) WriteBlobLSBLong(image,(unsigned int) icon_info.image_size);
1170 (void) WriteBlobLSBLong(image,(unsigned int) icon_info.x_pixels);
1171 (void) WriteBlobLSBLong(image,(unsigned int) icon_info.y_pixels);
1172 (void) WriteBlobLSBLong(image,(unsigned int) icon_info.number_colors);
1173 (void) WriteBlobLSBLong(image,(unsigned int) icon_info.colors_important);
1174 if (next->storage_class == PseudoClass)
1180 Dump colormap to file.
1182 icon_colormap=(unsigned char *) AcquireQuantumMemory((size_t)
1183 (1UL << icon_info.bits_per_pixel),4UL*sizeof(*icon_colormap));
1184 if (icon_colormap == (unsigned char *) NULL)
1185 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
1187 for (i=0; i < (ssize_t) next->colors; i++)
1189 *q++=ScaleQuantumToChar(next->colormap[i].blue);
1190 *q++=ScaleQuantumToChar(next->colormap[i].green);
1191 *q++=ScaleQuantumToChar(next->colormap[i].red);
1192 *q++=(unsigned char) 0x0;
1194 for ( ; i < (ssize_t) (1UL << icon_info.bits_per_pixel); i++)
1196 *q++=(unsigned char) 0x00;
1197 *q++=(unsigned char) 0x00;
1198 *q++=(unsigned char) 0x00;
1199 *q++=(unsigned char) 0x00;
1201 (void) WriteBlob(image,(size_t) (4UL*(1UL <<
1202 icon_info.bits_per_pixel)),icon_colormap);
1203 icon_colormap=(unsigned char *) RelinquishMagickMemory(
1206 (void) WriteBlob(image,(size_t) icon_info.image_size,pixels);
1207 pixels=(unsigned char *) RelinquishMagickMemory(pixels);
1211 scanline_pad=(((next->columns+31) & ~31)-next->columns) >> 3;
1212 for (y=((ssize_t) next->rows - 1); y >= 0; y--)
1214 p=GetVirtualPixels(next,0,y,next->columns,1,exception);
1215 if (p == (const Quantum *) NULL)
1219 for (x=0; x < (ssize_t) next->columns; x++)
1222 if ((next->matte == MagickTrue) &&
1223 (GetPixelAlpha(next,p) == (Quantum) TransparentAlpha))
1228 (void) WriteBlobByte(image,(unsigned char) byte);
1232 p+=GetPixelChannels(next);
1235 (void) WriteBlobByte(image,(unsigned char) (byte << (8-bit)));
1236 for (i=0; i < (ssize_t) scanline_pad; i++)
1237 (void) WriteBlobByte(image,(unsigned char) 0);
1240 if (GetNextImageInList(next) == (Image *) NULL)
1242 next=SyncNextImageInList(next);
1243 status=SetImageProgress(next,SaveImagesTag,scene++,
1244 GetImageListLength(next));
1245 if (status == MagickFalse)
1247 } while ((next != (Image *) NULL) && (image_info->adjoin != MagickFalse));
1248 offset=SeekBlob(image,0,SEEK_SET);
1250 (void) WriteBlobLSBShort(image,0);
1251 (void) WriteBlobLSBShort(image,1);
1252 (void) WriteBlobLSBShort(image,(unsigned short) (scene+1));
1257 (void) WriteBlobByte(image,icon_file.directory[scene].width);
1258 (void) WriteBlobByte(image,icon_file.directory[scene].height);
1259 (void) WriteBlobByte(image,icon_file.directory[scene].colors);
1260 (void) WriteBlobByte(image,icon_file.directory[scene].reserved);
1261 (void) WriteBlobLSBShort(image,icon_file.directory[scene].planes);
1262 (void) WriteBlobLSBShort(image,icon_file.directory[scene].bits_per_pixel);
1263 (void) WriteBlobLSBLong(image,(unsigned int)
1264 icon_file.directory[scene].size);
1265 (void) WriteBlobLSBLong(image,(unsigned int)
1266 icon_file.directory[scene].offset);
1268 next=SyncNextImageInList(next);
1269 } while ((next != (Image *) NULL) && (image_info->adjoin != MagickFalse));
1270 (void) CloseBlob(image);