2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
10 % IIIII CCCC OOO N N %
13 % Read Microsoft Windows Icon Format %
20 % Copyright 1999-2011 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/blob.h"
44 #include "MagickCore/blob-private.h"
45 #include "MagickCore/cache.h"
46 #include "MagickCore/colormap.h"
47 #include "MagickCore/colorspace.h"
48 #include "MagickCore/exception.h"
49 #include "MagickCore/exception-private.h"
50 #include "MagickCore/image.h"
51 #include "MagickCore/image-private.h"
52 #include "MagickCore/list.h"
53 #include "MagickCore/log.h"
54 #include "MagickCore/magick.h"
55 #include "MagickCore/memory_.h"
56 #include "MagickCore/monitor.h"
57 #include "MagickCore/monitor-private.h"
58 #include "MagickCore/nt-feature.h"
59 #include "MagickCore/pixel-accessor.h"
60 #include "MagickCore/quantize.h"
61 #include "MagickCore/quantum-private.h"
62 #include "MagickCore/static.h"
63 #include "MagickCore/string_.h"
64 #include "MagickCore/module.h"
69 #if !defined(MAGICKCORE_WINDOWS_SUPPORT) || defined(__MINGW32__)
72 #define BI_BITFIELDS 3
79 typedef struct _IconEntry
96 typedef struct _IconFile
107 typedef struct _IconInfo
140 Forward declaractions.
142 static MagickBooleanType
143 WriteICONImage(const ImageInfo *,Image *,ExceptionInfo *);
146 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
150 % R e a d I C O N I m a g e %
154 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
156 % ReadICONImage() reads a Microsoft icon image file and returns it. It
157 % allocates the memory necessary for the new Image structure and returns a
158 % pointer to the new image.
160 % The format of the ReadICONImage method is:
162 % Image *ReadICONImage(const ImageInfo *image_info,
163 % ExceptionInfo *exception)
165 % A description of each parameter follows:
167 % o image_info: the image info.
169 % o exception: return any errors or warnings in this structure.
172 static Image *ReadICONImage(const ImageInfo *image_info,
173 ExceptionInfo *exception)
194 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,exception);
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) ((int) ReadBlobLSBLong(image));
259 icon_info.height=(unsigned char) ((int) ReadBlobLSBLong(image)/2);
260 icon_info.planes=ReadBlobLSBShort(image);
261 icon_info.bits_per_pixel=ReadBlobLSBShort(image);
262 if (icon_info.bits_per_pixel > 32)
263 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
264 icon_info.compression=ReadBlobLSBLong(image);
265 icon_info.image_size=ReadBlobLSBLong(image);
266 icon_info.x_pixels=ReadBlobLSBLong(image);
267 icon_info.y_pixels=ReadBlobLSBLong(image);
268 icon_info.number_colors=ReadBlobLSBLong(image);
269 icon_info.colors_important=ReadBlobLSBLong(image);
270 image->matte=MagickTrue;
271 image->columns=(size_t) icon_file.directory[i].width;
272 if ((ssize_t) image->columns > icon_info.width)
273 image->columns=(size_t) icon_info.width;
274 if (image->columns == 0)
276 image->rows=(size_t) icon_file.directory[i].height;
277 if ((ssize_t) image->rows > icon_info.height)
278 image->rows=(size_t) icon_info.height;
279 if (image->rows == 0)
281 image->depth=icon_info.bits_per_pixel;
282 if (image->debug != MagickFalse)
284 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
285 " scene = %.20g",(double) i);
286 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
287 " size = %.20g",(double) icon_info.size);
288 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
289 " width = %.20g",(double) icon_file.directory[i].width);
290 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
291 " height = %.20g",(double) icon_file.directory[i].height);
292 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
293 " colors = %.20g",(double ) icon_info.number_colors);
294 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
295 " planes = %.20g",(double) icon_info.planes);
296 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
297 " bpp = %.20g",(double) icon_info.bits_per_pixel);
299 if ((icon_info.number_colors != 0) || (icon_info.bits_per_pixel <= 16))
301 image->storage_class=PseudoClass;
302 image->colors=icon_info.number_colors;
303 if (image->colors == 0)
304 image->colors=one << icon_info.bits_per_pixel;
306 if (image->storage_class == PseudoClass)
319 Read Icon raster colormap.
322 number_colors=one << icon_info.bits_per_pixel;
323 if (AcquireImageColormap(image,number_colors,exception) == MagickFalse)
324 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
325 icon_colormap=(unsigned char *) AcquireQuantumMemory((size_t)
326 image->colors,4UL*sizeof(*icon_colormap));
327 if (icon_colormap == (unsigned char *) NULL)
328 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
329 count=ReadBlob(image,(size_t) (4*image->colors),icon_colormap);
330 if (count != (ssize_t) (4*image->colors))
331 ThrowReaderException(CorruptImageError,"InsufficientImageDataInFile");
333 for (i=0; i < (ssize_t) image->colors; i++)
335 image->colormap[i].blue=(Quantum) ScaleCharToQuantum(*p++);
336 image->colormap[i].green=(Quantum) ScaleCharToQuantum(*p++);
337 image->colormap[i].red=(Quantum) ScaleCharToQuantum(*p++);
340 icon_colormap=(unsigned char *) RelinquishMagickMemory(icon_colormap);
342 if ((image_info->ping != MagickFalse) && (image_info->number_scenes != 0))
343 if (image->scene >= (image_info->scene+image_info->number_scenes-1))
345 length=icon_file.directory[i].size-40;
346 pixels=(unsigned char *) AcquireQuantumMemory(length,sizeof(*pixels));
347 if (pixels == (unsigned char *) NULL)
348 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
349 count=ReadBlob(image,length,pixels);
350 if (count != (ssize_t) length)
352 pixels=(unsigned char *) RelinquishMagickMemory(pixels);
353 ThrowReaderException(CorruptImageError,"InsufficientImageDataInFile");
355 if (memcmp(pixels,"\211PNG\r\n\032\n",8) == 0)
364 Icon image encoded as a compressed PNG image.
366 read_info=CloneImageInfo(image_info);
367 (void) CopyMagickString(read_info->magick,"PNG",MaxTextExtent);
368 icon_image=BlobToImage(read_info,pixels,length,exception);
369 read_info=DestroyImageInfo(read_info);
370 pixels=(unsigned char *) RelinquishMagickMemory(pixels);
371 if (icon_image == (Image *) NULL)
373 image=DestroyImageList(image);
374 return((Image *) NULL);
376 DestroyBlob(icon_image);
377 icon_image->blob=ReferenceBlob(image->blob);
378 ReplaceImageInList(&image,icon_image);
382 register unsigned char
386 Convert Icon raster image to pixel packets.
388 bytes_per_line=(((image->columns*icon_info.bits_per_pixel)+31) &
390 (void) bytes_per_line;
391 scanline_pad=((((image->columns*icon_info.bits_per_pixel)+31) & ~31)-
392 (image->columns*icon_info.bits_per_pixel)) >> 3;
394 switch (icon_info.bits_per_pixel)
399 Convert bitmap scanline.
401 for (y=(ssize_t) image->rows-1; y >= 0; y--)
403 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
404 if (q == (Quantum *) NULL)
406 for (x=0; x < (ssize_t) (image->columns-7); x+=8)
409 for (bit=0; bit < 8; bit++)
411 SetPixelIndex(image,((byte & (0x80 >> bit)) != 0 ? 0x01 :
413 q+=GetPixelChannels(image);
416 if ((image->columns % 8) != 0)
419 for (bit=0; bit < (image->columns % 8); bit++)
421 SetPixelIndex(image,((byte & (0x80 >> bit)) != 0 ? 0x01 :
423 q+=GetPixelChannels(image);
426 for (x=0; x < (ssize_t) scanline_pad; x++)
428 if (SyncAuthenticPixels(image,exception) == MagickFalse)
430 if (image->previous == (Image *) NULL)
432 status=SetImageProgress(image,LoadImageTag,image->rows-y-1,
434 if (status == MagickFalse)
443 Read 4-bit Icon scanline.
445 for (y=(ssize_t) image->rows-1; y >= 0; y--)
447 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
448 if (q == (Quantum *) NULL)
450 for (x=0; x < ((ssize_t) image->columns-1); x+=2)
453 SetPixelIndex(image,((byte >> 4) & 0xf),q);
454 q+=GetPixelChannels(image);
455 SetPixelIndex(image,((byte) & 0xf),q);
456 q+=GetPixelChannels(image);
458 if ((image->columns % 2) != 0)
461 SetPixelIndex(image,((byte >> 4) & 0xf),q);
462 q+=GetPixelChannels(image);
464 for (x=0; x < (ssize_t) scanline_pad; x++)
466 if (SyncAuthenticPixels(image,exception) == MagickFalse)
468 if (image->previous == (Image *) NULL)
470 status=SetImageProgress(image,LoadImageTag,image->rows-y-1,
472 if (status == MagickFalse)
481 Convert PseudoColor scanline.
483 for (y=(ssize_t) image->rows-1; y >= 0; y--)
485 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
486 if (q == (Quantum *) NULL)
488 for (x=0; x < (ssize_t) image->columns; x++)
491 SetPixelIndex(image,byte,q);
492 q+=GetPixelChannels(image);
494 for (x=0; x < (ssize_t) scanline_pad; x++)
496 if (SyncAuthenticPixels(image,exception) == MagickFalse)
498 if (image->previous == (Image *) NULL)
500 status=SetImageProgress(image,LoadImageTag,image->rows-y-1,
502 if (status == MagickFalse)
511 Convert PseudoColor scanline.
513 for (y=(ssize_t) image->rows-1; y >= 0; y--)
515 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
516 if (q == (Quantum *) NULL)
518 for (x=0; x < (ssize_t) image->columns; x++)
521 byte|=(size_t) (*p++ << 8);
522 SetPixelIndex(image,byte,q);
523 q+=GetPixelChannels(image);
525 for (x=0; x < (ssize_t) scanline_pad; x++)
527 if (SyncAuthenticPixels(image,exception) == MagickFalse)
529 if (image->previous == (Image *) NULL)
531 status=SetImageProgress(image,LoadImageTag,image->rows-y-1,
533 if (status == MagickFalse)
543 Convert DirectColor scanline.
545 for (y=(ssize_t) image->rows-1; y >= 0; y--)
547 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
548 if (q == (Quantum *) NULL)
550 for (x=0; x < (ssize_t) image->columns; x++)
552 SetPixelBlue(image,ScaleCharToQuantum(*p++),q);
553 SetPixelGreen(image,ScaleCharToQuantum(*p++),q);
554 SetPixelBlue(image,ScaleCharToQuantum(*p++),q);
555 if (icon_info.bits_per_pixel == 32)
556 SetPixelAlpha(image,ScaleCharToQuantum(*p++),q);
557 q+=GetPixelChannels(image);
559 if (icon_info.bits_per_pixel == 24)
560 for (x=0; x < (ssize_t) scanline_pad; x++)
562 if (SyncAuthenticPixels(image,exception) == MagickFalse)
564 if (image->previous == (Image *) NULL)
566 status=SetImageProgress(image,LoadImageTag,image->rows-y-1,
568 if (status == MagickFalse)
575 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
577 if (image_info->ping == MagickFalse)
578 (void) SyncImage(image);
579 if (icon_info.bits_per_pixel != 32)
582 Read the ICON alpha mask.
584 image->storage_class=DirectClass;
585 for (y=(ssize_t) image->rows-1; y >= 0; y--)
587 q=GetAuthenticPixels(image,0,y,image->columns,1,exception);
588 if (q == (Quantum *) NULL)
590 for (x=0; x < ((ssize_t) image->columns-7); x+=8)
593 for (bit=0; bit < 8; bit++)
595 SetPixelAlpha(image,(((byte & (0x80 >> bit)) != 0) ?
596 TransparentAlpha : OpaqueAlpha),q);
597 q+=GetPixelChannels(image);
600 if ((image->columns % 8) != 0)
603 for (bit=0; bit < (image->columns % 8); bit++)
605 SetPixelAlpha(image,(((byte & (0x80 >> bit)) != 0) ?
606 TransparentAlpha : OpaqueAlpha),q);
607 q+=GetPixelChannels(image);
610 if ((image->columns % 32) != 0)
611 for (x=0; x < (ssize_t) ((32-(image->columns % 32))/8); x++)
613 if (SyncAuthenticPixels(image,exception) == MagickFalse)
617 if (EOFBlob(image) != MagickFalse)
619 ThrowFileException(exception,CorruptImageError,
620 "UnexpectedEndOfFile",image->filename);
624 pixels=(unsigned char *) RelinquishMagickMemory(pixels);
626 Proceed to next image.
628 if (image_info->number_scenes != 0)
629 if (image->scene >= (image_info->scene+image_info->number_scenes-1))
631 if (i < (ssize_t) (icon_file.count-1))
634 Allocate next image structure.
636 AcquireNextImage(image_info,image,exception);
637 if (GetNextImageInList(image) == (Image *) NULL)
639 image=DestroyImageList(image);
640 return((Image *) NULL);
642 image=SyncNextImageInList(image);
643 status=SetImageProgress(image,LoadImagesTag,TellBlob(image),
645 if (status == MagickFalse)
649 (void) CloseBlob(image);
650 return(GetFirstImageInList(image));
654 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
658 % R e g i s t e r I C O N I m a g e %
662 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
664 % RegisterICONImage() adds attributes for the Icon image format to
665 % the list of supported formats. The attributes include the image format
666 % tag, a method to read and/or write the format, whether the format
667 % supports the saving of more than one frame to the same file or blob,
668 % whether the format supports native in-memory I/O, and a brief
669 % description of the format.
671 % The format of the RegisterICONImage method is:
673 % size_t RegisterICONImage(void)
676 ModuleExport size_t RegisterICONImage(void)
681 entry=SetMagickInfo("CUR");
682 entry->decoder=(DecodeImageHandler *) ReadICONImage;
683 entry->encoder=(EncodeImageHandler *) WriteICONImage;
684 entry->adjoin=MagickFalse;
685 entry->seekable_stream=MagickTrue;
686 entry->description=ConstantString("Microsoft icon");
687 entry->module=ConstantString("CUR");
688 (void) RegisterMagickInfo(entry);
689 entry=SetMagickInfo("ICO");
690 entry->decoder=(DecodeImageHandler *) ReadICONImage;
691 entry->encoder=(EncodeImageHandler *) WriteICONImage;
692 entry->adjoin=MagickTrue;
693 entry->seekable_stream=MagickTrue;
694 entry->description=ConstantString("Microsoft icon");
695 entry->module=ConstantString("ICON");
696 (void) RegisterMagickInfo(entry);
697 entry=SetMagickInfo("ICON");
698 entry->decoder=(DecodeImageHandler *) ReadICONImage;
699 entry->encoder=(EncodeImageHandler *) WriteICONImage;
700 entry->adjoin=MagickFalse;
701 entry->seekable_stream=MagickTrue;
702 entry->description=ConstantString("Microsoft icon");
703 entry->module=ConstantString("ICON");
704 (void) RegisterMagickInfo(entry);
705 return(MagickImageCoderSignature);
709 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
713 % U n r e g i s t e r I C O N I m a g e %
717 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
719 % UnregisterICONImage() removes format registrations made by the
720 % ICON module from the list of supported formats.
722 % The format of the UnregisterICONImage method is:
724 % UnregisterICONImage(void)
727 ModuleExport void UnregisterICONImage(void)
729 (void) UnregisterMagickInfo("CUR");
730 (void) UnregisterMagickInfo("ICO");
731 (void) UnregisterMagickInfo("ICON");
735 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
739 % W r i t e I C O N I m a g e %
743 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
745 % WriteICONImage() writes an image in Microsoft Windows bitmap encoded
746 % image format, version 3 for Windows or (if the image has a matte channel)
749 % The format of the WriteICONImage method is:
751 % MagickBooleanType WriteICONImage(const ImageInfo *image_info,
752 % Image *image,ExceptionInfo *exception)
754 % A description of each parameter follows.
756 % o image_info: the image info.
758 % o image: The image.
760 % o exception: return any errors or warnings in this structure.
763 static MagickBooleanType WriteICONImage(const ImageInfo *image_info,
764 Image *image,ExceptionInfo *exception)
782 register const Quantum
789 register unsigned char
805 Open output image file.
807 assert(image_info != (const ImageInfo *) NULL);
808 assert(image_info->signature == MagickSignature);
809 assert(image != (Image *) NULL);
810 assert(image->signature == MagickSignature);
811 (void) LogMagickEvent(CoderEvent,GetMagickModule(),"%s",image->filename);
812 assert(exception != (ExceptionInfo *) NULL);
813 assert(exception->signature == MagickSignature);
814 status=OpenBlob(image_info,image,WriteBinaryBlobMode,exception);
815 if (status == MagickFalse)
821 if ((image->columns > 256L) || (image->rows > 256L))
822 ThrowWriterException(ImageError,"WidthOrHeightExceedsLimit");
824 next=SyncNextImageInList(next);
825 } while ((next != (Image *) NULL) && (image_info->adjoin != MagickFalse));
827 Dump out a ICON header template to be properly initialized later.
829 (void) WriteBlobLSBShort(image,0);
830 (void) WriteBlobLSBShort(image,1);
831 (void) WriteBlobLSBShort(image,(unsigned char) scene);
832 (void) ResetMagickMemory(&icon_file,0,sizeof(icon_file));
833 (void) ResetMagickMemory(&icon_info,0,sizeof(icon_info));
838 (void) WriteBlobByte(image,icon_file.directory[scene].width);
839 (void) WriteBlobByte(image,icon_file.directory[scene].height);
840 (void) WriteBlobByte(image,icon_file.directory[scene].colors);
841 (void) WriteBlobByte(image,icon_file.directory[scene].reserved);
842 (void) WriteBlobLSBShort(image,icon_file.directory[scene].planes);
843 (void) WriteBlobLSBShort(image,icon_file.directory[scene].bits_per_pixel);
844 (void) WriteBlobLSBLong(image,(unsigned int)
845 icon_file.directory[scene].size);
846 (void) WriteBlobLSBLong(image,(unsigned int)
847 icon_file.directory[scene].offset);
849 next=SyncNextImageInList(next);
850 } while ((next != (Image *) NULL) && (image_info->adjoin != MagickFalse));
855 if ((next->columns == 256) && (next->rows == 256))
870 Icon image encoded as a compressed PNG image.
872 write_image=CloneImage(next,0,0,MagickTrue,exception);
873 if (write_image == (Image *) NULL)
875 write_info=CloneImageInfo(image_info);
876 (void) CopyMagickString(write_info->filename,"PNG:",MaxTextExtent);
877 png=(unsigned char *) ImageToBlob(write_info,write_image,&length,
879 write_image=DestroyImage(write_image);
880 write_info=DestroyImageInfo(write_info);
881 if (png == (unsigned char *) NULL)
883 icon_file.directory[scene].width=0;
884 icon_file.directory[scene].height=0;
885 icon_file.directory[scene].colors=0;
886 icon_file.directory[scene].reserved=0;
887 icon_file.directory[scene].planes=1;
888 icon_file.directory[scene].bits_per_pixel=32;
889 icon_file.directory[scene].size=(size_t) length;
890 icon_file.directory[scene].offset=(size_t) TellBlob(image);
891 (void) WriteBlob(image,(size_t) length,png);
892 png=(unsigned char *) RelinquishMagickMemory(png);
897 Initialize ICON raster file header.
899 if (next->colorspace != RGBColorspace)
900 (void) TransformImageColorspace(next,RGBColorspace);
901 icon_info.file_size=14+12+28;
902 icon_info.offset_bits=icon_info.file_size;
903 icon_info.compression=BI_RGB;
904 if ((next->storage_class != DirectClass) && (next->colors > 256))
905 (void) SetImageStorageClass(next,DirectClass,exception);
906 if (next->storage_class == DirectClass)
909 Full color ICON raster.
911 icon_info.number_colors=0;
912 icon_info.bits_per_pixel=32;
913 icon_info.compression=(size_t) BI_RGB;
921 Colormapped ICON raster.
923 icon_info.bits_per_pixel=8;
924 if (next->colors <= 256)
925 icon_info.bits_per_pixel=8;
926 if (next->colors <= 16)
927 icon_info.bits_per_pixel=4;
928 if (next->colors <= 2)
929 icon_info.bits_per_pixel=1;
931 icon_info.number_colors=one << icon_info.bits_per_pixel;
932 if (icon_info.number_colors < next->colors)
934 (void) SetImageStorageClass(next,DirectClass,exception);
935 icon_info.number_colors=0;
936 icon_info.bits_per_pixel=(unsigned short) 24;
937 icon_info.compression=(size_t) BI_RGB;
945 icon_info.file_size+=3*(one << icon_info.bits_per_pixel);
946 icon_info.offset_bits+=3*(one << icon_info.bits_per_pixel);
947 icon_info.file_size+=(one << icon_info.bits_per_pixel);
948 icon_info.offset_bits+=(one << icon_info.bits_per_pixel);
951 bytes_per_line=(((next->columns*icon_info.bits_per_pixel)+31) &
953 icon_info.ba_offset=0;
954 icon_info.width=(ssize_t) next->columns;
955 icon_info.height=(ssize_t) next->rows;
957 icon_info.image_size=bytes_per_line*next->rows;
959 icon_info.size+=(4*icon_info.number_colors);
960 icon_info.size+=icon_info.image_size;
961 icon_info.size+=(((icon_info.width+31) & ~31) >> 3)*icon_info.height;
962 icon_info.file_size+=icon_info.image_size;
963 icon_info.x_pixels=0;
964 icon_info.y_pixels=0;
967 case UndefinedResolution:
968 case PixelsPerInchResolution:
970 icon_info.x_pixels=(size_t) (100.0*next->x_resolution/2.54);
971 icon_info.y_pixels=(size_t) (100.0*next->y_resolution/2.54);
974 case PixelsPerCentimeterResolution:
976 icon_info.x_pixels=(size_t) (100.0*next->x_resolution);
977 icon_info.y_pixels=(size_t) (100.0*next->y_resolution);
981 icon_info.colors_important=icon_info.number_colors;
983 Convert MIFF to ICON raster pixels.
985 pixels=(unsigned char *) AcquireQuantumMemory((size_t)
986 icon_info.image_size,sizeof(*pixels));
987 if (pixels == (unsigned char *) NULL)
988 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
989 (void) ResetMagickMemory(pixels,0,(size_t) icon_info.image_size);
990 switch (icon_info.bits_per_pixel)
999 Convert PseudoClass image to a ICON monochrome image.
1001 for (y=0; y < (ssize_t) next->rows; y++)
1003 p=GetVirtualPixels(next,0,y,next->columns,1,exception);
1004 if (p == (const Quantum *) NULL)
1006 q=pixels+(next->rows-y-1)*bytes_per_line;
1009 for (x=0; x < (ssize_t) next->columns; x++)
1012 byte|=GetPixelIndex(next,p) != 0 ? 0x01 : 0x00;
1016 *q++=(unsigned char) byte;
1020 p+=GetPixelChannels(image);
1023 *q++=(unsigned char) (byte << (8-bit));
1024 if (next->previous == (Image *) NULL)
1026 status=SetImageProgress(next,SaveImageTag,y,next->rows);
1027 if (status == MagickFalse)
1040 Convert PseudoClass image to a ICON monochrome image.
1042 for (y=0; y < (ssize_t) next->rows; y++)
1044 p=GetVirtualPixels(next,0,y,next->columns,1,exception);
1045 if (p == (const Quantum *) NULL)
1047 q=pixels+(next->rows-y-1)*bytes_per_line;
1050 for (x=0; x < (ssize_t) next->columns; x++)
1053 byte|=((size_t) GetPixelIndex(next,p) & 0x0f);
1057 *q++=(unsigned char) byte;
1061 p+=GetPixelChannels(image);
1064 *q++=(unsigned char) (byte << 4);
1065 if (next->previous == (Image *) NULL)
1067 status=SetImageProgress(next,SaveImageTag,y,next->rows);
1068 if (status == MagickFalse)
1077 Convert PseudoClass packet to ICON pixel.
1079 for (y=0; y < (ssize_t) next->rows; y++)
1081 p=GetVirtualPixels(next,0,y,next->columns,1,exception);
1082 if (p == (const Quantum *) NULL)
1084 q=pixels+(next->rows-y-1)*bytes_per_line;
1085 for (x=0; x < (ssize_t) next->columns; x++)
1087 *q++=(unsigned char) GetPixelIndex(next,p);
1088 p+=GetPixelChannels(image);
1090 if (next->previous == (Image *) NULL)
1092 status=SetImageProgress(next,SaveImageTag,y,next->rows);
1093 if (status == MagickFalse)
1103 Convert DirectClass packet to ICON BGR888 or BGRA8888 pixel.
1105 for (y=0; y < (ssize_t) next->rows; y++)
1107 p=GetVirtualPixels(next,0,y,next->columns,1,exception);
1108 if (p == (const Quantum *) NULL)
1110 q=pixels+(next->rows-y-1)*bytes_per_line;
1111 for (x=0; x < (ssize_t) next->columns; x++)
1113 *q++=ScaleQuantumToChar(GetPixelBlue(next,p));
1114 *q++=ScaleQuantumToChar(GetPixelGreen(next,p));
1115 *q++=ScaleQuantumToChar(GetPixelRed(next,p));
1116 if (next->matte == MagickFalse)
1117 *q++=ScaleQuantumToChar(QuantumRange);
1119 *q++=ScaleQuantumToChar(GetPixelAlpha(next,p));
1120 p+=GetPixelChannels(next);
1122 if (icon_info.bits_per_pixel == 24)
1123 for (x=3L*(ssize_t) next->columns; x < (ssize_t) bytes_per_line; x++)
1125 if (next->previous == (Image *) NULL)
1127 status=SetImageProgress(next,SaveImageTag,y,next->rows);
1128 if (status == MagickFalse)
1136 Write 40-byte version 3+ bitmap header.
1138 icon_file.directory[scene].width=(unsigned char) icon_info.width;
1139 icon_file.directory[scene].height=(unsigned char) icon_info.height;
1140 icon_file.directory[scene].colors=(unsigned char)
1141 icon_info.number_colors;
1142 icon_file.directory[scene].reserved=0;
1143 icon_file.directory[scene].planes=icon_info.planes;
1144 icon_file.directory[scene].bits_per_pixel=icon_info.bits_per_pixel;
1145 icon_file.directory[scene].size=icon_info.size;
1146 icon_file.directory[scene].offset=(size_t) TellBlob(image);
1147 (void) WriteBlobLSBLong(image,(unsigned int) 40);
1148 (void) WriteBlobLSBLong(image,(unsigned int) icon_info.width);
1149 (void) WriteBlobLSBLong(image,(unsigned int) icon_info.height*2);
1150 (void) WriteBlobLSBShort(image,icon_info.planes);
1151 (void) WriteBlobLSBShort(image,icon_info.bits_per_pixel);
1152 (void) WriteBlobLSBLong(image,(unsigned int) icon_info.compression);
1153 (void) WriteBlobLSBLong(image,(unsigned int) icon_info.image_size);
1154 (void) WriteBlobLSBLong(image,(unsigned int) icon_info.x_pixels);
1155 (void) WriteBlobLSBLong(image,(unsigned int) icon_info.y_pixels);
1156 (void) WriteBlobLSBLong(image,(unsigned int) icon_info.number_colors);
1157 (void) WriteBlobLSBLong(image,(unsigned int) icon_info.colors_important);
1158 if (next->storage_class == PseudoClass)
1164 Dump colormap to file.
1166 icon_colormap=(unsigned char *) AcquireQuantumMemory((size_t)
1167 (1UL << icon_info.bits_per_pixel),4UL*sizeof(*icon_colormap));
1168 if (icon_colormap == (unsigned char *) NULL)
1169 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
1171 for (i=0; i < (ssize_t) next->colors; i++)
1173 *q++=ScaleQuantumToChar(next->colormap[i].blue);
1174 *q++=ScaleQuantumToChar(next->colormap[i].green);
1175 *q++=ScaleQuantumToChar(next->colormap[i].red);
1176 *q++=(unsigned char) 0x0;
1178 for ( ; i < (ssize_t) (1UL << icon_info.bits_per_pixel); i++)
1180 *q++=(unsigned char) 0x00;
1181 *q++=(unsigned char) 0x00;
1182 *q++=(unsigned char) 0x00;
1183 *q++=(unsigned char) 0x00;
1185 (void) WriteBlob(image,(size_t) (4UL*(1UL <<
1186 icon_info.bits_per_pixel)),icon_colormap);
1187 icon_colormap=(unsigned char *) RelinquishMagickMemory(
1190 (void) WriteBlob(image,(size_t) icon_info.image_size,pixels);
1191 pixels=(unsigned char *) RelinquishMagickMemory(pixels);
1195 scanline_pad=(((next->columns+31) & ~31)-next->columns) >> 3;
1196 for (y=((ssize_t) next->rows - 1); y >= 0; y--)
1198 p=GetVirtualPixels(next,0,y,next->columns,1,exception);
1199 if (p == (const Quantum *) NULL)
1203 for (x=0; x < (ssize_t) next->columns; x++)
1206 if ((next->matte == MagickTrue) &&
1207 (GetPixelAlpha(next,p) == (Quantum) TransparentAlpha))
1212 (void) WriteBlobByte(image,(unsigned char) byte);
1216 p+=GetPixelChannels(next);
1219 (void) WriteBlobByte(image,(unsigned char) (byte << (8-bit)));
1220 for (i=0; i < (ssize_t) scanline_pad; i++)
1221 (void) WriteBlobByte(image,(unsigned char) 0);
1224 if (GetNextImageInList(next) == (Image *) NULL)
1226 next=SyncNextImageInList(next);
1227 status=SetImageProgress(next,SaveImagesTag,scene++,
1228 GetImageListLength(next));
1229 if (status == MagickFalse)
1231 } while ((next != (Image *) NULL) && (image_info->adjoin != MagickFalse));
1232 offset=SeekBlob(image,0,SEEK_SET);
1234 (void) WriteBlobLSBShort(image,0);
1235 (void) WriteBlobLSBShort(image,1);
1236 (void) WriteBlobLSBShort(image,(unsigned short) (scene+1));
1241 (void) WriteBlobByte(image,icon_file.directory[scene].width);
1242 (void) WriteBlobByte(image,icon_file.directory[scene].height);
1243 (void) WriteBlobByte(image,icon_file.directory[scene].colors);
1244 (void) WriteBlobByte(image,icon_file.directory[scene].reserved);
1245 (void) WriteBlobLSBShort(image,icon_file.directory[scene].planes);
1246 (void) WriteBlobLSBShort(image,icon_file.directory[scene].bits_per_pixel);
1247 (void) WriteBlobLSBLong(image,(unsigned int)
1248 icon_file.directory[scene].size);
1249 (void) WriteBlobLSBLong(image,(unsigned int)
1250 icon_file.directory[scene].offset);
1252 next=SyncNextImageInList(next);
1253 } while ((next != (Image *) NULL) && (image_info->adjoin != MagickFalse));
1254 (void) CloseBlob(image);