2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
10 % IIIII CCCC OOO N N %
13 % Read Microsoft Windows Icon Format %
20 % Copyright 1999-2010 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 "magick/studio.h"
43 #include "magick/blob.h"
44 #include "magick/blob-private.h"
45 #include "magick/cache.h"
46 #include "magick/colormap.h"
47 #include "magick/colorspace.h"
48 #include "magick/exception.h"
49 #include "magick/exception-private.h"
50 #include "magick/image.h"
51 #include "magick/image-private.h"
52 #include "magick/list.h"
53 #include "magick/log.h"
54 #include "magick/magick.h"
55 #include "magick/memory_.h"
56 #include "magick/monitor.h"
57 #include "magick/monitor-private.h"
58 #include "magick/nt-feature.h"
59 #include "magick/quantize.h"
60 #include "magick/quantum-private.h"
61 #include "magick/static.h"
62 #include "magick/string_.h"
63 #include "magick/module.h"
68 #if !defined(MAGICKCORE_WINDOWS_SUPPORT) || defined(__MINGW32__)
71 #define BI_BITFIELDS 3
78 typedef struct _IconEntry
95 typedef struct _IconFile
106 typedef struct _IconInfo
139 Forward declaractions.
141 static MagickBooleanType
142 WriteICONImage(const ImageInfo *,Image *);
145 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
149 % R e a d I C O N I m a g e %
153 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
155 % ReadICONImage() reads a Microsoft icon image file and returns it. It
156 % allocates the memory necessary for the new Image structure and returns a
157 % pointer to the new image.
159 % The format of the ReadICONImage method is:
161 % Image *ReadICONImage(const ImageInfo *image_info,
162 % ExceptionInfo *exception)
164 % A description of each parameter follows:
166 % o image_info: the image info.
168 % o exception: return any errors or warnings in this structure.
171 static Image *ReadICONImage(const ImageInfo *image_info,
172 ExceptionInfo *exception)
199 register unsigned char
216 assert(image_info != (const ImageInfo *) NULL);
217 assert(image_info->signature == MagickSignature);
218 (void) LogMagickEvent(CoderEvent,GetMagickModule(),"%s",image_info->filename);
219 assert(exception != (ExceptionInfo *) NULL);
220 assert(exception->signature == MagickSignature);
221 image=AcquireImage(image_info);
222 status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception);
223 if (status == MagickFalse)
225 image=DestroyImageList(image);
226 return((Image *) NULL);
228 icon_file.reserved=(short) ReadBlobLSBShort(image);
229 icon_file.resource_type=(short) ReadBlobLSBShort(image);
230 icon_file.count=(short) ReadBlobLSBShort(image);
231 if ((icon_file.reserved != 0) ||
232 ((icon_file.resource_type != 1) && (icon_file.resource_type != 2)) ||
233 (icon_file.count > MaxIcons))
234 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
235 for (i=0; i < icon_file.count; i++)
237 icon_file.directory[i].width=(unsigned char) ReadBlobByte(image);
238 icon_file.directory[i].height=(unsigned char) ReadBlobByte(image);
239 icon_file.directory[i].colors=(unsigned char) ReadBlobByte(image);
240 icon_file.directory[i].reserved=(unsigned char) ReadBlobByte(image);
241 icon_file.directory[i].planes=(unsigned short) ReadBlobLSBShort(image);
242 icon_file.directory[i].bits_per_pixel=(unsigned short)
243 ReadBlobLSBShort(image);
244 icon_file.directory[i].size=ReadBlobLSBLong(image);
245 icon_file.directory[i].offset=ReadBlobLSBLong(image);
248 for (i=0; i < icon_file.count; i++)
251 Verify Icon identifier.
253 offset=(ssize_t) SeekBlob(image,(MagickOffsetType)
254 icon_file.directory[i].offset,SEEK_SET);
256 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
257 icon_info.size=ReadBlobLSBLong(image);
258 icon_info.width=(unsigned char) ReadBlobLSBLong(image);
259 icon_info.height=(unsigned char) ReadBlobLSBLong(image)/2;
260 if ((icon_file.directory[i].width == 0) &&
261 (icon_file.directory[i].height == 0))
276 Icon image encoded as a compressed PNG image.
278 length=icon_file.directory[i].size;
279 png=(unsigned char *) AcquireQuantumMemory(length+12,sizeof(*png));
280 if (png == (unsigned char *) NULL)
281 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
282 (void) CopyMagickMemory(png,"\211PNG\r\n\032\n\000\000\000\015",12);
283 count=ReadBlob(image,length-12,png+12);
284 if (count != (ssize_t) (length-12))
286 png=(unsigned char *) RelinquishMagickMemory(png);
287 ThrowReaderException(CorruptImageError,
288 "InsufficientImageDataInFile");
290 read_info=CloneImageInfo(image_info);
291 (void) CopyMagickString(read_info->magick,"PNG",MaxTextExtent);
292 icon_image=BlobToImage(read_info,png,length+12,exception);
293 read_info=DestroyImageInfo(read_info);
294 png=(unsigned char *) RelinquishMagickMemory(png);
295 if (icon_image == (Image *) NULL)
297 image=DestroyImageList(image);
298 return((Image *) NULL);
300 DestroyBlob(icon_image);
301 icon_image->blob=ReferenceBlob(image->blob);
302 ReplaceImageInList(&image,icon_image);
306 icon_info.planes=ReadBlobLSBShort(image);
307 icon_info.bits_per_pixel=ReadBlobLSBShort(image);
308 if (icon_info.bits_per_pixel > 32)
309 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
310 icon_info.compression=ReadBlobLSBLong(image);
311 icon_info.image_size=ReadBlobLSBLong(image);
312 icon_info.x_pixels=ReadBlobLSBLong(image);
313 icon_info.y_pixels=ReadBlobLSBLong(image);
314 icon_info.number_colors=ReadBlobLSBLong(image);
315 icon_info.colors_important=ReadBlobLSBLong(image);
316 image->matte=MagickTrue;
317 image->columns=(size_t) icon_file.directory[i].width;
318 if ((ssize_t) image->columns > icon_info.width)
319 image->columns=(size_t) icon_info.width;
320 image->rows=(size_t) icon_file.directory[i].height;
321 if ((ssize_t) image->rows > icon_info.height)
322 image->rows=(size_t) icon_info.height;
323 image->depth=icon_info.bits_per_pixel;
324 if (image->debug != MagickFalse)
326 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
327 " scene = %.20g",(double) i);
328 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
329 " size = %.20g",(double) icon_info.size);
330 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
331 " width = %.20g",(double) icon_file.directory[i].width);
332 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
333 " height = %.20g",(double) icon_file.directory[i].height);
334 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
335 " colors = %.20g",(double ) icon_info.number_colors);
336 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
337 " planes = %.20g",(double) icon_info.planes);
338 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
339 " bpp = %.20g",(double) icon_info.bits_per_pixel);
341 if ((icon_info.number_colors != 0) || (icon_info.bits_per_pixel <= 16))
343 image->storage_class=PseudoClass;
344 image->colors=icon_info.number_colors;
345 if (image->colors == 0)
346 image->colors=one << icon_info.bits_per_pixel;
348 if (image->storage_class == PseudoClass)
361 Read Icon raster colormap.
364 number_colors=one << icon_info.bits_per_pixel;
365 if (AcquireImageColormap(image,number_colors) == MagickFalse)
366 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
367 icon_colormap=(unsigned char *) AcquireQuantumMemory((size_t)
368 image->colors,4UL*sizeof(*icon_colormap));
369 if (icon_colormap == (unsigned char *) NULL)
370 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
371 count=ReadBlob(image,(size_t) (4*image->colors),icon_colormap);
372 if (count != (ssize_t) (4*image->colors))
373 ThrowReaderException(CorruptImageError,
374 "InsufficientImageDataInFile");
376 for (i=0; i < (ssize_t) image->colors; i++)
378 image->colormap[i].blue=(Quantum) ScaleCharToQuantum(*p++);
379 image->colormap[i].green=(Quantum) ScaleCharToQuantum(*p++);
380 image->colormap[i].red=(Quantum) ScaleCharToQuantum(*p++);
383 icon_colormap=(unsigned char *) RelinquishMagickMemory(icon_colormap);
386 Convert Icon raster image to pixel packets.
388 if ((image_info->ping != MagickFalse) &&
389 (image_info->number_scenes != 0))
390 if (image->scene >= (image_info->scene+image_info->number_scenes-1))
392 bytes_per_line=(((image->columns*icon_info.bits_per_pixel)+31) &
394 scanline_pad=((((image->columns*icon_info.bits_per_pixel)+31) & ~31)-
395 (image->columns*icon_info.bits_per_pixel)) >> 3;
396 switch (icon_info.bits_per_pixel)
401 Convert bitmap scanline.
403 for (y=(ssize_t) image->rows-1; y >= 0; y--)
405 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
406 if (q == (PixelPacket *) NULL)
408 indexes=GetAuthenticIndexQueue(image);
409 for (x=0; x < (ssize_t) (image->columns-7); x+=8)
411 byte=(size_t) ReadBlobByte(image);
412 for (bit=0; bit < 8; bit++)
413 indexes[x+bit]=(IndexPacket)
414 ((byte & (0x80 >> bit)) != 0 ? 0x01 : 0x00);
416 if ((image->columns % 8) != 0)
418 byte=(size_t) ReadBlobByte(image);
419 for (bit=0; bit < (image->columns % 8); bit++)
420 indexes[x+bit]=(IndexPacket)
421 ((byte & (0x80 >> bit)) != 0 ? 0x01 : 0x00);
423 for (x=0; x < (ssize_t) scanline_pad; x++)
424 (void) ReadBlobByte(image);
425 if (SyncAuthenticPixels(image,exception) == MagickFalse)
427 if (image->previous == (Image *) NULL)
429 status=SetImageProgress(image,LoadImageTag,image->rows-y-1,
431 if (status == MagickFalse)
440 Read 4-bit Icon scanline.
442 for (y=(ssize_t) image->rows-1; y >= 0; y--)
444 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
445 if (q == (PixelPacket *) NULL)
447 indexes=GetAuthenticIndexQueue(image);
448 for (x=0; x < ((ssize_t) image->columns-1); x+=2)
450 byte=(size_t) ReadBlobByte(image);
451 indexes[x]=(IndexPacket) ((byte >> 4) & 0xf);
452 indexes[x+1]=(IndexPacket) ((byte) & 0xf);
454 if ((image->columns % 2) != 0)
456 byte=(size_t) ReadBlobByte(image);
457 indexes[x]=(IndexPacket) ((byte >> 4) & 0xf);
459 for (x=0; x < (ssize_t) scanline_pad; x++)
460 (void) ReadBlobByte(image);
461 if (SyncAuthenticPixels(image,exception) == MagickFalse)
463 if (image->previous == (Image *) NULL)
465 status=SetImageProgress(image,LoadImageTag,image->rows-y-1,
467 if (status == MagickFalse)
476 Convert PseudoColor scanline.
478 for (y=(ssize_t) image->rows-1; y >= 0; y--)
480 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
481 if (q == (PixelPacket *) NULL)
483 indexes=GetAuthenticIndexQueue(image);
484 for (x=0; x < (ssize_t) image->columns; x++)
486 byte=(size_t) ReadBlobByte(image);
487 indexes[x]=(IndexPacket) byte;
489 for (x=0; x < (ssize_t) scanline_pad; x++)
490 (void) ReadBlobByte(image);
491 if (SyncAuthenticPixels(image,exception) == MagickFalse)
493 if (image->previous == (Image *) NULL)
495 status=SetImageProgress(image,LoadImageTag,image->rows-y-1,
497 if (status == MagickFalse)
506 Convert PseudoColor scanline.
508 for (y=(ssize_t) image->rows-1; y >= 0; y--)
510 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
511 if (q == (PixelPacket *) NULL)
513 indexes=GetAuthenticIndexQueue(image);
514 for (x=0; x < (ssize_t) image->columns; x++)
516 byte=(size_t) ReadBlobByte(image);
517 byte|=(size_t) (ReadBlobByte(image) << 8);
518 indexes[x]=(IndexPacket) byte;
520 for (x=0; x < (ssize_t) scanline_pad; x++)
521 (void) ReadBlobByte(image);
522 if (SyncAuthenticPixels(image,exception) == MagickFalse)
524 if (image->previous == (Image *) NULL)
526 status=SetImageProgress(image,LoadImageTag,image->rows-y-1,
528 if (status == MagickFalse)
538 Convert DirectColor scanline.
540 for (y=(ssize_t) image->rows-1; y >= 0; y--)
542 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
543 if (q == (PixelPacket *) NULL)
545 for (x=0; x < (ssize_t) image->columns; x++)
547 q->blue=ScaleCharToQuantum((unsigned char) ReadBlobByte(image));
548 q->green=ScaleCharToQuantum((unsigned char)
549 ReadBlobByte(image));
550 q->red=ScaleCharToQuantum((unsigned char) ReadBlobByte(image));
551 if (icon_info.bits_per_pixel == 32)
552 q->opacity=(Quantum) (QuantumRange-ScaleCharToQuantum(
553 (unsigned char) ReadBlobByte(image)));
556 if (icon_info.bits_per_pixel == 24)
557 for (x=0; x < (ssize_t) scanline_pad; x++)
558 (void) ReadBlobByte(image);
559 if (SyncAuthenticPixels(image,exception) == MagickFalse)
561 if (image->previous == (Image *) NULL)
563 status=SetImageProgress(image,LoadImageTag,image->rows-y-1,
565 if (status == MagickFalse)
572 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
574 if (image_info->ping == MagickFalse)
575 (void) SyncImage(image);
576 if (icon_info.bits_per_pixel != 32)
579 Read the ICON alpha mask.
581 image->storage_class=DirectClass;
582 for (y=(ssize_t) image->rows-1; y >= 0; y--)
584 q=GetAuthenticPixels(image,0,y,image->columns,1,exception);
585 if (q == (PixelPacket *) NULL)
587 for (x=0; x < ((ssize_t) image->columns-7); x+=8)
589 byte=(size_t) ReadBlobByte(image);
590 for (bit=0; bit < 8; bit++)
591 q[x+bit].opacity=(Quantum) (((byte & (0x80 >> bit)) != 0) ?
592 TransparentOpacity : OpaqueOpacity);
594 if ((image->columns % 8) != 0)
596 byte=(size_t) ReadBlobByte(image);
597 for (bit=0; bit < (image->columns % 8); bit++)
598 q[x+bit].opacity=(Quantum) (((byte & (0x80 >> bit)) != 0) ?
599 TransparentOpacity : OpaqueOpacity);
601 if ((image->columns % 32) != 0)
602 for (x=0; x < (ssize_t) ((32-(image->columns % 32))/8); x++)
603 (void) ReadBlobByte(image);
604 if (SyncAuthenticPixels(image,exception) == MagickFalse)
608 if (EOFBlob(image) != MagickFalse)
610 ThrowFileException(exception,CorruptImageError,
611 "UnexpectedEndOfFile",image->filename);
616 Proceed to next image.
618 if (image_info->number_scenes != 0)
619 if (image->scene >= (image_info->scene+image_info->number_scenes-1))
621 if (i < (ssize_t) (icon_file.count-1))
624 Allocate next image structure.
626 AcquireNextImage(image_info,image);
627 if (GetNextImageInList(image) == (Image *) NULL)
629 image=DestroyImageList(image);
630 return((Image *) NULL);
632 image=SyncNextImageInList(image);
633 status=SetImageProgress(image,LoadImagesTag,TellBlob(image),
635 if (status == MagickFalse)
639 (void) CloseBlob(image);
640 return(GetFirstImageInList(image));
644 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
648 % R e g i s t e r I C O N I m a g e %
652 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
654 % RegisterICONImage() adds attributes for the Icon image format to
655 % the list of supported formats. The attributes include the image format
656 % tag, a method to read and/or write the format, whether the format
657 % supports the saving of more than one frame to the same file or blob,
658 % whether the format supports native in-memory I/O, and a brief
659 % description of the format.
661 % The format of the RegisterICONImage method is:
663 % size_t RegisterICONImage(void)
666 ModuleExport size_t RegisterICONImage(void)
671 entry=SetMagickInfo("CUR");
672 entry->decoder=(DecodeImageHandler *) ReadICONImage;
673 entry->encoder=(EncodeImageHandler *) WriteICONImage;
674 entry->adjoin=MagickFalse;
675 entry->seekable_stream=MagickTrue;
676 entry->description=ConstantString("Microsoft icon");
677 entry->module=ConstantString("CUR");
678 (void) RegisterMagickInfo(entry);
679 entry=SetMagickInfo("ICO");
680 entry->decoder=(DecodeImageHandler *) ReadICONImage;
681 entry->encoder=(EncodeImageHandler *) WriteICONImage;
682 entry->adjoin=MagickTrue;
683 entry->seekable_stream=MagickTrue;
684 entry->description=ConstantString("Microsoft icon");
685 entry->module=ConstantString("ICON");
686 (void) RegisterMagickInfo(entry);
687 entry=SetMagickInfo("ICON");
688 entry->decoder=(DecodeImageHandler *) ReadICONImage;
689 entry->encoder=(EncodeImageHandler *) WriteICONImage;
690 entry->adjoin=MagickFalse;
691 entry->seekable_stream=MagickTrue;
692 entry->description=ConstantString("Microsoft icon");
693 entry->module=ConstantString("ICON");
694 (void) RegisterMagickInfo(entry);
695 return(MagickImageCoderSignature);
699 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
703 % U n r e g i s t e r I C O N I m a g e %
707 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
709 % UnregisterICONImage() removes format registrations made by the
710 % ICON module from the list of supported formats.
712 % The format of the UnregisterICONImage method is:
714 % UnregisterICONImage(void)
717 ModuleExport void UnregisterICONImage(void)
719 (void) UnregisterMagickInfo("CUR");
720 (void) UnregisterMagickInfo("ICO");
721 (void) UnregisterMagickInfo("ICON");
725 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
729 % W r i t e I C O N I m a g e %
733 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
735 % WriteICONImage() writes an image in Microsoft Windows bitmap encoded
736 % image format, version 3 for Windows or (if the image has a matte channel)
739 % The format of the WriteICONImage method is:
741 % MagickBooleanType WriteICONImage(const ImageInfo *image_info,
744 % A description of each parameter follows.
746 % o image_info: the image info.
748 % o image: The image.
751 static MagickBooleanType WriteICONImage(const ImageInfo *image_info,
773 register const IndexPacket
776 register const PixelPacket
783 register unsigned char
796 Open output image file.
798 assert(image_info != (const ImageInfo *) NULL);
799 assert(image_info->signature == MagickSignature);
800 assert(image != (Image *) NULL);
801 assert(image->signature == MagickSignature);
802 (void) LogMagickEvent(CoderEvent,GetMagickModule(),"%s",image->filename);
803 status=OpenBlob(image_info,image,WriteBinaryBlobMode,&image->exception);
804 if (status == MagickFalse)
810 if ((image->columns > 256L) || (image->rows > 256L))
811 ThrowWriterException(ImageError,"WidthOrHeightExceedsLimit");
813 next=SyncNextImageInList(next);
814 } while ((next != (Image *) NULL) && (image_info->adjoin != MagickFalse));
816 Dump out a ICON header template to be properly initialized later.
818 (void) WriteBlobLSBShort(image,0);
819 (void) WriteBlobLSBShort(image,1);
820 (void) WriteBlobLSBShort(image,(unsigned char) scene);
821 (void) ResetMagickMemory(&icon_file,0,sizeof(icon_file));
822 (void) ResetMagickMemory(&icon_info,0,sizeof(icon_info));
827 (void) WriteBlobByte(image,icon_file.directory[scene].width);
828 (void) WriteBlobByte(image,icon_file.directory[scene].height);
829 (void) WriteBlobByte(image,icon_file.directory[scene].colors);
830 (void) WriteBlobByte(image,icon_file.directory[scene].reserved);
831 (void) WriteBlobLSBShort(image,icon_file.directory[scene].planes);
832 (void) WriteBlobLSBShort(image,icon_file.directory[scene].bits_per_pixel);
833 (void) WriteBlobLSBLong(image,(unsigned int)
834 icon_file.directory[scene].size);
835 (void) WriteBlobLSBLong(image,(unsigned int)
836 icon_file.directory[scene].offset);
838 next=SyncNextImageInList(next);
839 } while ((next != (Image *) NULL) && (image_info->adjoin != MagickFalse));
844 if ((next->columns == 256) && (next->rows == 256))
859 Icon image encoded as a compressed PNG image.
861 write_image=CloneImage(next,0,0,MagickTrue,&image->exception);
862 if (write_image == (Image *) NULL)
864 write_info=CloneImageInfo(image_info);
865 (void) CopyMagickString(write_info->filename,"PNG:",MaxTextExtent);
866 png=(unsigned char *) ImageToBlob(write_info,write_image,&length,
868 write_image=DestroyImage(write_image);
869 write_info=DestroyImageInfo(write_info);
870 if (png == (unsigned char *) NULL)
872 icon_file.directory[scene].width=0;
873 icon_file.directory[scene].height=0;
874 icon_file.directory[scene].colors=0;
875 icon_file.directory[scene].reserved=0;
876 icon_file.directory[scene].planes=1;
877 icon_file.directory[scene].bits_per_pixel=32;
878 icon_file.directory[scene].size=(size_t) length;
879 icon_file.directory[scene].offset=(size_t) TellBlob(image);
880 (void) WriteBlob(image,(size_t) length,png);
881 png=(unsigned char *) RelinquishMagickMemory(png);
886 Initialize ICON raster file header.
888 if (next->colorspace != RGBColorspace)
889 (void) TransformImageColorspace(next,RGBColorspace);
890 icon_info.file_size=14+12+28;
891 icon_info.offset_bits=icon_info.file_size;
892 icon_info.compression=BI_RGB;
893 if ((next->storage_class != DirectClass) && (next->colors > 256))
894 (void) SetImageStorageClass(next,DirectClass);
895 if (next->storage_class == DirectClass)
898 Full color ICON raster.
900 icon_info.number_colors=0;
901 icon_info.bits_per_pixel=32;
902 icon_info.compression=(size_t) BI_RGB;
910 Colormapped ICON raster.
912 icon_info.bits_per_pixel=8;
913 if (next->colors <= 256)
914 icon_info.bits_per_pixel=8;
915 if (next->colors <= 16)
916 icon_info.bits_per_pixel=4;
917 if (next->colors <= 2)
918 icon_info.bits_per_pixel=1;
920 icon_info.number_colors=one << icon_info.bits_per_pixel;
921 if (icon_info.number_colors < next->colors)
923 (void) SetImageStorageClass(next,DirectClass);
924 icon_info.number_colors=0;
925 icon_info.bits_per_pixel=(unsigned short) 24;
926 icon_info.compression=(size_t) BI_RGB;
934 icon_info.file_size+=3*(one << icon_info.bits_per_pixel);
935 icon_info.offset_bits+=3*(one << icon_info.bits_per_pixel);
936 icon_info.file_size+=(one << icon_info.bits_per_pixel);
937 icon_info.offset_bits+=(one << icon_info.bits_per_pixel);
940 bytes_per_line=(((next->columns*icon_info.bits_per_pixel)+31) &
942 icon_info.ba_offset=0;
943 icon_info.width=(ssize_t) next->columns;
944 icon_info.height=(ssize_t) next->rows;
946 icon_info.image_size=bytes_per_line*next->rows;
948 icon_info.size+=(4*icon_info.number_colors);
949 icon_info.size+=icon_info.image_size;
950 icon_info.size+=(((icon_info.width+31) & ~31) >> 3)*icon_info.height;
951 icon_info.file_size+=icon_info.image_size;
952 icon_info.x_pixels=0;
953 icon_info.y_pixels=0;
956 case UndefinedResolution:
957 case PixelsPerInchResolution:
959 icon_info.x_pixels=(size_t) (100.0*next->x_resolution/2.54);
960 icon_info.y_pixels=(size_t) (100.0*next->y_resolution/2.54);
963 case PixelsPerCentimeterResolution:
965 icon_info.x_pixels=(size_t) (100.0*next->x_resolution);
966 icon_info.y_pixels=(size_t) (100.0*next->y_resolution);
970 icon_info.colors_important=icon_info.number_colors;
972 Convert MIFF to ICON raster pixels.
974 pixels=(unsigned char *) AcquireQuantumMemory((size_t)
975 icon_info.image_size,sizeof(*pixels));
976 if (pixels == (unsigned char *) NULL)
977 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
978 (void) ResetMagickMemory(pixels,0,(size_t) icon_info.image_size);
979 switch (icon_info.bits_per_pixel)
988 Convert PseudoClass image to a ICON monochrome image.
990 for (y=0; y < (ssize_t) next->rows; y++)
992 p=GetVirtualPixels(next,0,y,next->columns,1,&next->exception);
993 if (p == (const PixelPacket *) NULL)
995 indexes=GetVirtualIndexQueue(next);
996 q=pixels+(next->rows-y-1)*bytes_per_line;
999 for (x=0; x < (ssize_t) next->columns; x++)
1002 byte|=indexes[x] != 0 ? 0x01 : 0x00;
1006 *q++=(unsigned char) byte;
1012 *q++=(unsigned char) (byte << (8-bit));
1013 if (next->previous == (Image *) NULL)
1015 status=SetImageProgress(next,SaveImageTag,y,next->rows);
1016 if (status == MagickFalse)
1029 Convert PseudoClass image to a ICON monochrome image.
1031 for (y=0; y < (ssize_t) next->rows; y++)
1033 p=GetVirtualPixels(next,0,y,next->columns,1,&next->exception);
1034 if (p == (const PixelPacket *) NULL)
1036 indexes=GetVirtualIndexQueue(next);
1037 q=pixels+(next->rows-y-1)*bytes_per_line;
1040 for (x=0; x < (ssize_t) next->columns; x++)
1043 byte|=((size_t) indexes[x] & 0x0f);
1047 *q++=(unsigned char) byte;
1053 *q++=(unsigned char) (byte << 4);
1054 if (next->previous == (Image *) NULL)
1056 status=SetImageProgress(next,SaveImageTag,y,next->rows);
1057 if (status == MagickFalse)
1066 Convert PseudoClass packet to ICON pixel.
1068 for (y=0; y < (ssize_t) next->rows; y++)
1070 p=GetVirtualPixels(next,0,y,next->columns,1,&next->exception);
1071 if (p == (const PixelPacket *) NULL)
1073 indexes=GetVirtualIndexQueue(next);
1074 q=pixels+(next->rows-y-1)*bytes_per_line;
1075 for (x=0; x < (ssize_t) next->columns; x++)
1076 *q++=(unsigned char) indexes[x];
1077 if (next->previous == (Image *) NULL)
1079 status=SetImageProgress(next,SaveImageTag,y,next->rows);
1080 if (status == MagickFalse)
1090 Convert DirectClass packet to ICON BGR888 or BGRA8888 pixel.
1092 for (y=0; y < (ssize_t) next->rows; y++)
1094 p=GetVirtualPixels(next,0,y,next->columns,1,&next->exception);
1095 if (p == (const PixelPacket *) NULL)
1097 q=pixels+(next->rows-y-1)*bytes_per_line;
1098 for (x=0; x < (ssize_t) next->columns; x++)
1100 *q++=ScaleQuantumToChar(GetBluePixelComponent(p));
1101 *q++=ScaleQuantumToChar(GetGreenPixelComponent(p));
1102 *q++=ScaleQuantumToChar(GetRedPixelComponent(p));
1103 if (next->matte == MagickFalse)
1104 *q++=ScaleQuantumToChar(QuantumRange);
1106 *q++=ScaleQuantumToChar(GetAlphaPixelComponent(p));
1109 if (icon_info.bits_per_pixel == 24)
1110 for (x=3L*(ssize_t) next->columns; x < (ssize_t) bytes_per_line; x++)
1112 if (next->previous == (Image *) NULL)
1114 status=SetImageProgress(next,SaveImageTag,y,next->rows);
1115 if (status == MagickFalse)
1123 Write 40-byte version 3+ bitmap header.
1125 icon_file.directory[scene].width=(unsigned char) icon_info.width;
1126 icon_file.directory[scene].height=(unsigned char) icon_info.height;
1127 icon_file.directory[scene].colors=(unsigned char)
1128 icon_info.number_colors;
1129 icon_file.directory[scene].reserved=0;
1130 icon_file.directory[scene].planes=icon_info.planes;
1131 icon_file.directory[scene].bits_per_pixel=icon_info.bits_per_pixel;
1132 icon_file.directory[scene].size=icon_info.size;
1133 icon_file.directory[scene].offset=(size_t) TellBlob(image);
1134 (void) WriteBlobLSBLong(image,(unsigned int) 40);
1135 (void) WriteBlobLSBLong(image,(unsigned int) icon_info.width);
1136 (void) WriteBlobLSBLong(image,(unsigned int) icon_info.height*2);
1137 (void) WriteBlobLSBShort(image,icon_info.planes);
1138 (void) WriteBlobLSBShort(image,icon_info.bits_per_pixel);
1139 (void) WriteBlobLSBLong(image,(unsigned int) icon_info.compression);
1140 (void) WriteBlobLSBLong(image,(unsigned int) icon_info.image_size);
1141 (void) WriteBlobLSBLong(image,(unsigned int) icon_info.x_pixels);
1142 (void) WriteBlobLSBLong(image,(unsigned int) icon_info.y_pixels);
1143 (void) WriteBlobLSBLong(image,(unsigned int) icon_info.number_colors);
1144 (void) WriteBlobLSBLong(image,(unsigned int) icon_info.colors_important);
1145 if (next->storage_class == PseudoClass)
1151 Dump colormap to file.
1153 icon_colormap=(unsigned char *) AcquireQuantumMemory((size_t)
1154 (1UL << icon_info.bits_per_pixel),4UL*sizeof(*icon_colormap));
1155 if (icon_colormap == (unsigned char *) NULL)
1156 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
1158 for (i=0; i < (ssize_t) next->colors; i++)
1160 *q++=ScaleQuantumToChar(next->colormap[i].blue);
1161 *q++=ScaleQuantumToChar(next->colormap[i].green);
1162 *q++=ScaleQuantumToChar(next->colormap[i].red);
1163 *q++=(unsigned char) 0x0;
1165 for ( ; i < (ssize_t) (1UL << icon_info.bits_per_pixel); i++)
1167 *q++=(unsigned char) 0x00;
1168 *q++=(unsigned char) 0x00;
1169 *q++=(unsigned char) 0x00;
1170 *q++=(unsigned char) 0x00;
1172 (void) WriteBlob(image,(size_t) (4UL*(1UL <<
1173 icon_info.bits_per_pixel)),icon_colormap);
1174 icon_colormap=(unsigned char *) RelinquishMagickMemory(
1177 (void) WriteBlob(image,(size_t) icon_info.image_size,pixels);
1178 pixels=(unsigned char *) RelinquishMagickMemory(pixels);
1182 scanline_pad=(((next->columns+31) & ~31)-next->columns) >> 3;
1183 for (y=((ssize_t) next->rows - 1); y >= 0; y--)
1185 p=GetVirtualPixels(next,0,y,next->columns,1,&next->exception);
1186 if (p == (const PixelPacket *) NULL)
1190 for (x=0; x < (ssize_t) next->columns; x++)
1193 if ((next->matte == MagickTrue) &&
1194 (p->opacity == (Quantum) TransparentOpacity))
1199 (void) WriteBlobByte(image,(unsigned char) byte);
1206 (void) WriteBlobByte(image,(unsigned char) (byte << (8-bit)));
1207 for (i=0; i < (ssize_t) scanline_pad; i++)
1208 (void) WriteBlobByte(image,(unsigned char) 0);
1211 if (GetNextImageInList(next) == (Image *) NULL)
1213 next=SyncNextImageInList(next);
1214 status=SetImageProgress(next,SaveImagesTag,scene++,
1215 GetImageListLength(next));
1216 if (status == MagickFalse)
1218 } while ((next != (Image *) NULL) && (image_info->adjoin != MagickFalse));
1219 offset=SeekBlob(image,0,SEEK_SET);
1220 (void) WriteBlobLSBShort(image,0);
1221 (void) WriteBlobLSBShort(image,1);
1222 (void) WriteBlobLSBShort(image,(unsigned short) (scene+1));
1227 (void) WriteBlobByte(image,icon_file.directory[scene].width);
1228 (void) WriteBlobByte(image,icon_file.directory[scene].height);
1229 (void) WriteBlobByte(image,icon_file.directory[scene].colors);
1230 (void) WriteBlobByte(image,icon_file.directory[scene].reserved);
1231 (void) WriteBlobLSBShort(image,icon_file.directory[scene].planes);
1232 (void) WriteBlobLSBShort(image,icon_file.directory[scene].bits_per_pixel);
1233 (void) WriteBlobLSBLong(image,(unsigned int)
1234 icon_file.directory[scene].size);
1235 (void) WriteBlobLSBLong(image,(unsigned int)
1236 icon_file.directory[scene].offset);
1238 next=SyncNextImageInList(next);
1239 } while ((next != (Image *) NULL) && (image_info->adjoin != MagickFalse));
1240 (void) CloseBlob(image);