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
217 assert(image_info != (const ImageInfo *) NULL);
218 assert(image_info->signature == MagickSignature);
219 (void) LogMagickEvent(CoderEvent,GetMagickModule(),"%s",image_info->filename);
220 assert(exception != (ExceptionInfo *) NULL);
221 assert(exception->signature == MagickSignature);
222 image=AcquireImage(image_info);
223 status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception);
224 if (status == MagickFalse)
226 image=DestroyImageList(image);
227 return((Image *) NULL);
229 icon_file.reserved=(short) ReadBlobLSBShort(image);
230 icon_file.resource_type=(short) ReadBlobLSBShort(image);
231 icon_file.count=(short) ReadBlobLSBShort(image);
232 if ((icon_file.reserved != 0) ||
233 ((icon_file.resource_type != 1) && (icon_file.resource_type != 2)) ||
234 (icon_file.count > MaxIcons))
235 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
236 for (i=0; i < icon_file.count; i++)
238 icon_file.directory[i].width=(unsigned char) ReadBlobByte(image);
239 icon_file.directory[i].height=(unsigned char) ReadBlobByte(image);
240 icon_file.directory[i].colors=(unsigned char) ReadBlobByte(image);
241 icon_file.directory[i].reserved=(unsigned char) ReadBlobByte(image);
242 icon_file.directory[i].planes=(unsigned short) ReadBlobLSBShort(image);
243 icon_file.directory[i].bits_per_pixel=(unsigned short)
244 ReadBlobLSBShort(image);
245 icon_file.directory[i].size=ReadBlobLSBLong(image);
246 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,png+12);
284 if (count != (ssize_t) length)
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;
324 if (image->debug != MagickFalse)
326 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
328 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
329 " size = %ld",icon_info.size);
330 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
331 " width = %d",icon_file.directory[i].width);
332 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
333 " height = %d",icon_file.directory[i].height);
334 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
335 " colors = %ld",icon_info.number_colors);
336 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
337 " planes = %d",icon_info.planes);
338 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
339 " bpp = %d",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=1 << icon_info.bits_per_pixel;
348 if (image->storage_class == PseudoClass)
360 Read Icon raster colormap.
362 number_colors=1UL << icon_info.bits_per_pixel;
363 if (AcquireImageColormap(image,number_colors) == MagickFalse)
364 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
365 icon_colormap=(unsigned char *) AcquireQuantumMemory((size_t)
366 image->colors,4UL*sizeof(*icon_colormap));
367 if (icon_colormap == (unsigned char *) NULL)
368 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
369 count=ReadBlob(image,(size_t) (4*image->colors),icon_colormap);
370 if (count != (ssize_t) (4*image->colors))
371 ThrowReaderException(CorruptImageError,
372 "InsufficientImageDataInFile");
374 for (i=0; i < (ssize_t) image->colors; i++)
376 image->colormap[i].blue=(Quantum) ScaleCharToQuantum(*p++);
377 image->colormap[i].green=(Quantum) ScaleCharToQuantum(*p++);
378 image->colormap[i].red=(Quantum) ScaleCharToQuantum(*p++);
381 icon_colormap=(unsigned char *) RelinquishMagickMemory(icon_colormap);
384 Convert Icon raster image to pixel packets.
386 if ((image_info->ping != MagickFalse) &&
387 (image_info->number_scenes != 0))
388 if (image->scene >= (image_info->scene+image_info->number_scenes-1))
390 bytes_per_line=(((image->columns*icon_info.bits_per_pixel)+31) &
392 scanline_pad=((((image->columns*icon_info.bits_per_pixel)+31) & ~31)-
393 (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 == (PixelPacket *) NULL)
406 indexes=GetAuthenticIndexQueue(image);
407 for (x=0; x < (ssize_t) (image->columns-7); x+=8)
409 byte=(size_t) ReadBlobByte(image);
410 for (bit=0; bit < 8; bit++)
411 indexes[x+bit]=(IndexPacket)
412 ((byte & (0x80 >> bit)) != 0 ? 0x01 : 0x00);
414 if ((image->columns % 8) != 0)
416 byte=(size_t) ReadBlobByte(image);
417 for (bit=0; bit < (image->columns % 8); bit++)
418 indexes[x+bit]=(IndexPacket)
419 ((byte & (0x80 >> bit)) != 0 ? 0x01 : 0x00);
421 for (x=0; x < (ssize_t) scanline_pad; x++)
422 (void) ReadBlobByte(image);
423 if (SyncAuthenticPixels(image,exception) == MagickFalse)
425 if (image->previous == (Image *) NULL)
427 status=SetImageProgress(image,LoadImageTag,image->rows-y-1,
429 if (status == MagickFalse)
438 Read 4-bit Icon scanline.
440 for (y=(ssize_t) image->rows-1; y >= 0; y--)
442 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
443 if (q == (PixelPacket *) NULL)
445 indexes=GetAuthenticIndexQueue(image);
446 for (x=0; x < ((ssize_t) image->columns-1); x+=2)
448 byte=(size_t) ReadBlobByte(image);
449 indexes[x]=(IndexPacket) ((byte >> 4) & 0xf);
450 indexes[x+1]=(IndexPacket) ((byte) & 0xf);
452 if ((image->columns % 2) != 0)
454 byte=(size_t) ReadBlobByte(image);
455 indexes[x]=(IndexPacket) ((byte >> 4) & 0xf);
457 for (x=0; x < (ssize_t) scanline_pad; x++)
458 (void) ReadBlobByte(image);
459 if (SyncAuthenticPixels(image,exception) == MagickFalse)
461 if (image->previous == (Image *) NULL)
463 status=SetImageProgress(image,LoadImageTag,image->rows-y-1,
465 if (status == MagickFalse)
474 Convert PseudoColor scanline.
476 for (y=(ssize_t) image->rows-1; y >= 0; y--)
478 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
479 if (q == (PixelPacket *) NULL)
481 indexes=GetAuthenticIndexQueue(image);
482 for (x=0; x < (ssize_t) image->columns; x++)
484 byte=(size_t) ReadBlobByte(image);
485 indexes[x]=(IndexPacket) byte;
487 for (x=0; x < (ssize_t) scanline_pad; x++)
488 (void) ReadBlobByte(image);
489 if (SyncAuthenticPixels(image,exception) == MagickFalse)
491 if (image->previous == (Image *) NULL)
493 status=SetImageProgress(image,LoadImageTag,image->rows-y-1,
495 if (status == MagickFalse)
504 Convert PseudoColor scanline.
506 for (y=(ssize_t) image->rows-1; y >= 0; y--)
508 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
509 if (q == (PixelPacket *) NULL)
511 indexes=GetAuthenticIndexQueue(image);
512 for (x=0; x < (ssize_t) image->columns; x++)
514 byte=(size_t) ReadBlobByte(image);
515 byte|=(size_t) (ReadBlobByte(image) << 8);
516 indexes[x]=(IndexPacket) byte;
518 for (x=0; x < (ssize_t) scanline_pad; x++)
519 (void) ReadBlobByte(image);
520 if (SyncAuthenticPixels(image,exception) == MagickFalse)
522 if (image->previous == (Image *) NULL)
524 status=SetImageProgress(image,LoadImageTag,image->rows-y-1,
526 if (status == MagickFalse)
536 Convert DirectColor scanline.
538 for (y=(ssize_t) image->rows-1; y >= 0; y--)
540 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
541 if (q == (PixelPacket *) NULL)
543 for (x=0; x < (ssize_t) image->columns; x++)
545 q->blue=ScaleCharToQuantum((unsigned char) ReadBlobByte(image));
546 q->green=ScaleCharToQuantum((unsigned char)
547 ReadBlobByte(image));
548 q->red=ScaleCharToQuantum((unsigned char) ReadBlobByte(image));
549 if (icon_info.bits_per_pixel == 32)
550 q->opacity=(Quantum) (QuantumRange-ScaleCharToQuantum(
551 (unsigned char) ReadBlobByte(image)));
554 if (icon_info.bits_per_pixel == 24)
555 for (x=0; x < (ssize_t) scanline_pad; x++)
556 (void) ReadBlobByte(image);
557 if (SyncAuthenticPixels(image,exception) == MagickFalse)
559 if (image->previous == (Image *) NULL)
561 status=SetImageProgress(image,LoadImageTag,image->rows-y-1,
563 if (status == MagickFalse)
570 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
572 (void) SyncImage(image);
573 if (icon_info.bits_per_pixel != 32)
576 Read the ICON alpha mask.
578 image->storage_class=DirectClass;
579 for (y=(ssize_t) image->rows-1; y >= 0; y--)
581 q=GetAuthenticPixels(image,0,y,image->columns,1,exception);
582 if (q == (PixelPacket *) NULL)
584 for (x=0; x < ((ssize_t) image->columns-7); x+=8)
586 byte=(size_t) ReadBlobByte(image);
587 for (bit=0; bit < 8; bit++)
588 q[x+bit].opacity=(Quantum) (((byte & (0x80 >> bit)) != 0) ?
589 TransparentOpacity : OpaqueOpacity);
591 if ((image->columns % 8) != 0)
593 byte=(size_t) ReadBlobByte(image);
594 for (bit=0; bit < (image->columns % 8); bit++)
595 q[x+bit].opacity=(Quantum) (((byte & (0x80 >> bit)) != 0) ?
596 TransparentOpacity : OpaqueOpacity);
598 if ((image->columns % 32) != 0)
599 for (x=0; x < (ssize_t) ((32-(image->columns % 32))/8); x++)
600 (void) ReadBlobByte(image);
601 if (SyncAuthenticPixels(image,exception) == MagickFalse)
605 if (EOFBlob(image) != MagickFalse)
607 ThrowFileException(exception,CorruptImageError,
608 "UnexpectedEndOfFile",image->filename);
613 Proceed to next image.
615 if (image_info->number_scenes != 0)
616 if (image->scene >= (image_info->scene+image_info->number_scenes-1))
618 if (i < (ssize_t) (icon_file.count-1))
621 Allocate next image structure.
623 AcquireNextImage(image_info,image);
624 if (GetNextImageInList(image) == (Image *) NULL)
626 image=DestroyImageList(image);
627 return((Image *) NULL);
629 image=SyncNextImageInList(image);
630 status=SetImageProgress(image,LoadImagesTag,TellBlob(image),
632 if (status == MagickFalse)
636 (void) CloseBlob(image);
637 return(GetFirstImageInList(image));
641 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
645 % R e g i s t e r I C O N I m a g e %
649 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
651 % RegisterICONImage() adds attributes for the Icon image format to
652 % the list of supported formats. The attributes include the image format
653 % tag, a method to read and/or write the format, whether the format
654 % supports the saving of more than one frame to the same file or blob,
655 % whether the format supports native in-memory I/O, and a brief
656 % description of the format.
658 % The format of the RegisterICONImage method is:
660 % size_t RegisterICONImage(void)
663 ModuleExport size_t RegisterICONImage(void)
668 entry=SetMagickInfo("CUR");
669 entry->decoder=(DecodeImageHandler *) ReadICONImage;
670 entry->encoder=(EncodeImageHandler *) WriteICONImage;
671 entry->adjoin=MagickFalse;
672 entry->seekable_stream=MagickTrue;
673 entry->description=ConstantString("Microsoft icon");
674 entry->module=ConstantString("CUR");
675 (void) RegisterMagickInfo(entry);
676 entry=SetMagickInfo("ICO");
677 entry->decoder=(DecodeImageHandler *) ReadICONImage;
678 entry->encoder=(EncodeImageHandler *) WriteICONImage;
679 entry->adjoin=MagickTrue;
680 entry->seekable_stream=MagickTrue;
681 entry->description=ConstantString("Microsoft icon");
682 entry->module=ConstantString("ICON");
683 (void) RegisterMagickInfo(entry);
684 entry=SetMagickInfo("ICON");
685 entry->decoder=(DecodeImageHandler *) ReadICONImage;
686 entry->encoder=(EncodeImageHandler *) WriteICONImage;
687 entry->adjoin=MagickFalse;
688 entry->seekable_stream=MagickTrue;
689 entry->description=ConstantString("Microsoft icon");
690 entry->module=ConstantString("ICON");
691 (void) RegisterMagickInfo(entry);
692 return(MagickImageCoderSignature);
696 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
700 % U n r e g i s t e r I C O N I m a g e %
704 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
706 % UnregisterICONImage() removes format registrations made by the
707 % ICON module from the list of supported formats.
709 % The format of the UnregisterICONImage method is:
711 % UnregisterICONImage(void)
714 ModuleExport void UnregisterICONImage(void)
716 (void) UnregisterMagickInfo("CUR");
717 (void) UnregisterMagickInfo("ICO");
718 (void) UnregisterMagickInfo("ICON");
722 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
726 % W r i t e I C O N I m a g e %
730 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
732 % WriteICONImage() writes an image in Microsoft Windows bitmap encoded
733 % image format, version 3 for Windows or (if the image has a matte channel)
736 % The format of the WriteICONImage method is:
738 % MagickBooleanType WriteICONImage(const ImageInfo *image_info,
741 % A description of each parameter follows.
743 % o image_info: the image info.
745 % o image: The image.
748 static MagickBooleanType WriteICONImage(const ImageInfo *image_info,
770 register const IndexPacket
773 register const PixelPacket
780 register unsigned char
793 Open output image file.
795 assert(image_info != (const ImageInfo *) NULL);
796 assert(image_info->signature == MagickSignature);
797 assert(image != (Image *) NULL);
798 assert(image->signature == MagickSignature);
799 (void) LogMagickEvent(CoderEvent,GetMagickModule(),"%s",image->filename);
800 status=OpenBlob(image_info,image,WriteBinaryBlobMode,&image->exception);
801 if (status == MagickFalse)
807 if ((image->columns > 256L) || (image->rows > 256L))
808 ThrowWriterException(ImageError,"WidthOrHeightExceedsLimit");
810 next=SyncNextImageInList(next);
811 } while ((next != (Image *) NULL) && (image_info->adjoin != MagickFalse));
813 Dump out a ICON header template to be properly initialized later.
815 (void) WriteBlobLSBShort(image,0);
816 (void) WriteBlobLSBShort(image,1);
817 (void) WriteBlobLSBShort(image,(unsigned char) scene);
818 (void) ResetMagickMemory(&icon_file,0,sizeof(icon_file));
819 (void) ResetMagickMemory(&icon_info,0,sizeof(icon_info));
824 (void) WriteBlobByte(image,icon_file.directory[scene].width);
825 (void) WriteBlobByte(image,icon_file.directory[scene].height);
826 (void) WriteBlobByte(image,icon_file.directory[scene].colors);
827 (void) WriteBlobByte(image,icon_file.directory[scene].reserved);
828 (void) WriteBlobLSBShort(image,icon_file.directory[scene].planes);
829 (void) WriteBlobLSBShort(image,icon_file.directory[scene].bits_per_pixel);
830 (void) WriteBlobLSBLong(image,icon_file.directory[scene].size);
831 (void) WriteBlobLSBLong(image,icon_file.directory[scene].offset);
833 next=SyncNextImageInList(next);
834 } while ((next != (Image *) NULL) && (image_info->adjoin != MagickFalse));
839 if ((next->columns == 256) && (next->rows == 256))
854 Icon image encoded as a compressed PNG image.
856 write_image=CloneImage(next,0,0,MagickTrue,&image->exception);
857 if (write_image == (Image *) NULL)
859 write_info=CloneImageInfo(image_info);
860 (void) CopyMagickString(write_info->filename,"PNG:",MaxTextExtent);
861 png=(unsigned char *) ImageToBlob(write_info,write_image,&length,
863 write_image=DestroyImage(write_image);
864 write_info=DestroyImageInfo(write_info);
865 if (png == (unsigned char *) NULL)
867 icon_file.directory[scene].width=0;
868 icon_file.directory[scene].height=0;
869 icon_file.directory[scene].colors=0;
870 icon_file.directory[scene].reserved=0;
871 icon_file.directory[scene].planes=1;
872 icon_file.directory[scene].bits_per_pixel=32;
873 icon_file.directory[scene].size=(size_t) length;
874 icon_file.directory[scene].offset=(size_t) TellBlob(image);
875 (void) WriteBlob(image,(size_t) length,png);
876 png=(unsigned char *) RelinquishMagickMemory(png);
881 Initialize ICON raster file header.
883 if (next->colorspace != RGBColorspace)
884 (void) TransformImageColorspace(next,RGBColorspace);
885 icon_info.file_size=14+12+28;
886 icon_info.offset_bits=icon_info.file_size;
887 icon_info.compression=BI_RGB;
888 if ((next->storage_class != DirectClass) && (next->colors > 256))
889 (void) SetImageStorageClass(next,DirectClass);
890 if (next->storage_class == DirectClass)
893 Full color ICON raster.
895 icon_info.number_colors=0;
896 icon_info.bits_per_pixel=32;
897 icon_info.compression=(size_t) BI_RGB;
902 Colormapped ICON raster.
904 icon_info.bits_per_pixel=8;
905 if (next->colors <= 256)
906 icon_info.bits_per_pixel=8;
907 if (next->colors <= 16)
908 icon_info.bits_per_pixel=4;
909 if (next->colors <= 2)
910 icon_info.bits_per_pixel=1;
911 icon_info.number_colors=1 << icon_info.bits_per_pixel;
912 if (icon_info.number_colors < next->colors)
914 (void) SetImageStorageClass(next,DirectClass);
915 icon_info.number_colors=0;
916 icon_info.bits_per_pixel=(unsigned short) 24;
917 icon_info.compression=(size_t) BI_RGB;
921 icon_info.file_size+=3*(1UL << icon_info.bits_per_pixel);
922 icon_info.offset_bits+=3*(1UL << icon_info.bits_per_pixel);
923 icon_info.file_size+=(1UL << icon_info.bits_per_pixel);
924 icon_info.offset_bits+=(1UL << icon_info.bits_per_pixel);
927 bytes_per_line=(((next->columns*icon_info.bits_per_pixel)+31) &
929 icon_info.ba_offset=0;
930 icon_info.width=(ssize_t) next->columns;
931 icon_info.height=(ssize_t) next->rows;
933 icon_info.image_size=bytes_per_line*next->rows;
935 icon_info.size+=(4*icon_info.number_colors);
936 icon_info.size+=icon_info.image_size;
937 icon_info.size+=(((icon_info.width+31) & ~31) >> 3)*icon_info.height;
938 icon_info.file_size+=icon_info.image_size;
939 icon_info.x_pixels=0;
940 icon_info.y_pixels=0;
943 case UndefinedResolution:
944 case PixelsPerInchResolution:
946 icon_info.x_pixels=(size_t) (100.0*next->x_resolution/2.54);
947 icon_info.y_pixels=(size_t) (100.0*next->y_resolution/2.54);
950 case PixelsPerCentimeterResolution:
952 icon_info.x_pixels=(size_t) (100.0*next->x_resolution);
953 icon_info.y_pixels=(size_t) (100.0*next->y_resolution);
957 icon_info.colors_important=icon_info.number_colors;
959 Convert MIFF to ICON raster pixels.
961 pixels=(unsigned char *) AcquireQuantumMemory((size_t)
962 icon_info.image_size,sizeof(*pixels));
963 if (pixels == (unsigned char *) NULL)
964 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
965 (void) ResetMagickMemory(pixels,0,(size_t) icon_info.image_size);
966 switch (icon_info.bits_per_pixel)
975 Convert PseudoClass image to a ICON monochrome image.
977 for (y=0; y < (ssize_t) next->rows; y++)
979 p=GetVirtualPixels(next,0,y,next->columns,1,&next->exception);
980 if (p == (const PixelPacket *) NULL)
982 indexes=GetVirtualIndexQueue(next);
983 q=pixels+(next->rows-y-1)*bytes_per_line;
986 for (x=0; x < (ssize_t) next->columns; x++)
989 byte|=indexes[x] != 0 ? 0x01 : 0x00;
993 *q++=(unsigned char) byte;
999 *q++=(unsigned char) (byte << (8-bit));
1000 if (next->previous == (Image *) NULL)
1002 status=SetImageProgress(next,SaveImageTag,y,next->rows);
1003 if (status == MagickFalse)
1016 Convert PseudoClass image to a ICON monochrome image.
1018 for (y=0; y < (ssize_t) next->rows; y++)
1020 p=GetVirtualPixels(next,0,y,next->columns,1,&next->exception);
1021 if (p == (const PixelPacket *) NULL)
1023 indexes=GetVirtualIndexQueue(next);
1024 q=pixels+(next->rows-y-1)*bytes_per_line;
1027 for (x=0; x < (ssize_t) next->columns; x++)
1030 byte|=((size_t) indexes[x] & 0x0f);
1034 *q++=(unsigned char) byte;
1040 *q++=(unsigned char) (byte << 4);
1041 if (next->previous == (Image *) NULL)
1043 status=SetImageProgress(next,SaveImageTag,y,next->rows);
1044 if (status == MagickFalse)
1053 Convert PseudoClass packet to ICON pixel.
1055 for (y=0; y < (ssize_t) next->rows; y++)
1057 p=GetVirtualPixels(next,0,y,next->columns,1,&next->exception);
1058 if (p == (const PixelPacket *) NULL)
1060 indexes=GetVirtualIndexQueue(next);
1061 q=pixels+(next->rows-y-1)*bytes_per_line;
1062 for (x=0; x < (ssize_t) next->columns; x++)
1063 *q++=(unsigned char) indexes[x];
1064 if (next->previous == (Image *) NULL)
1066 status=SetImageProgress(next,SaveImageTag,y,next->rows);
1067 if (status == MagickFalse)
1077 Convert DirectClass packet to ICON BGR888 or BGRA8888 pixel.
1079 for (y=0; y < (ssize_t) next->rows; y++)
1081 p=GetVirtualPixels(next,0,y,next->columns,1,&next->exception);
1082 if (p == (const PixelPacket *) NULL)
1084 q=pixels+(next->rows-y-1)*bytes_per_line;
1085 for (x=0; x < (ssize_t) next->columns; x++)
1087 *q++=ScaleQuantumToChar(GetBluePixelComponent(p));
1088 *q++=ScaleQuantumToChar(GetGreenPixelComponent(p));
1089 *q++=ScaleQuantumToChar(GetRedPixelComponent(p));
1090 if (next->matte == MagickFalse)
1091 *q++=ScaleQuantumToChar(QuantumRange);
1093 *q++=ScaleQuantumToChar(GetAlphaPixelComponent(p));
1096 if (icon_info.bits_per_pixel == 24)
1097 for (x=3L*(ssize_t) next->columns; x < (ssize_t) bytes_per_line; x++)
1099 if (next->previous == (Image *) NULL)
1101 status=SetImageProgress(next,SaveImageTag,y,next->rows);
1102 if (status == MagickFalse)
1110 Write 40-byte version 3+ bitmap header.
1112 icon_file.directory[scene].width=(unsigned char) icon_info.width;
1113 icon_file.directory[scene].height=(unsigned char) icon_info.height;
1114 icon_file.directory[scene].colors=(unsigned char)
1115 icon_info.number_colors;
1116 icon_file.directory[scene].reserved=0;
1117 icon_file.directory[scene].planes=icon_info.planes;
1118 icon_file.directory[scene].bits_per_pixel=icon_info.bits_per_pixel;
1119 icon_file.directory[scene].size=icon_info.size;
1120 icon_file.directory[scene].offset=(size_t) TellBlob(image);
1121 (void) WriteBlobLSBLong(image,(size_t) 40);
1122 (void) WriteBlobLSBLong(image,(size_t) icon_info.width);
1123 (void) WriteBlobLSBLong(image,(size_t) icon_info.height*2);
1124 (void) WriteBlobLSBShort(image,icon_info.planes);
1125 (void) WriteBlobLSBShort(image,icon_info.bits_per_pixel);
1126 (void) WriteBlobLSBLong(image,icon_info.compression);
1127 (void) WriteBlobLSBLong(image,icon_info.image_size);
1128 (void) WriteBlobLSBLong(image,icon_info.x_pixels);
1129 (void) WriteBlobLSBLong(image,icon_info.y_pixels);
1130 (void) WriteBlobLSBLong(image,icon_info.number_colors);
1131 (void) WriteBlobLSBLong(image,icon_info.colors_important);
1132 if (next->storage_class == PseudoClass)
1138 Dump colormap to file.
1140 icon_colormap=(unsigned char *) AcquireQuantumMemory((size_t)
1141 (1UL << icon_info.bits_per_pixel),4UL*sizeof(*icon_colormap));
1142 if (icon_colormap == (unsigned char *) NULL)
1143 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
1145 for (i=0; i < (ssize_t) next->colors; i++)
1147 *q++=ScaleQuantumToChar(next->colormap[i].blue);
1148 *q++=ScaleQuantumToChar(next->colormap[i].green);
1149 *q++=ScaleQuantumToChar(next->colormap[i].red);
1150 *q++=(unsigned char) 0x0;
1152 for ( ; i < (ssize_t) (1UL << icon_info.bits_per_pixel); i++)
1154 *q++=(unsigned char) 0x00;
1155 *q++=(unsigned char) 0x00;
1156 *q++=(unsigned char) 0x00;
1157 *q++=(unsigned char) 0x00;
1159 (void) WriteBlob(image,(size_t) (4UL*(1UL <<
1160 icon_info.bits_per_pixel)),icon_colormap);
1161 icon_colormap=(unsigned char *) RelinquishMagickMemory(
1164 (void) WriteBlob(image,(size_t) icon_info.image_size,pixels);
1165 pixels=(unsigned char *) RelinquishMagickMemory(pixels);
1169 scanline_pad=(((next->columns+31) & ~31)-next->columns) >> 3;
1170 for (y=((ssize_t) next->rows - 1); y >= 0; y--)
1172 p=GetVirtualPixels(next,0,y,next->columns,1,&next->exception);
1173 if (p == (const PixelPacket *) NULL)
1177 for (x=0; x < (ssize_t) next->columns; x++)
1180 if ((next->matte == MagickTrue) &&
1181 (p->opacity == (Quantum) TransparentOpacity))
1186 (void) WriteBlobByte(image,(unsigned char) byte);
1193 (void) WriteBlobByte(image,(unsigned char) (byte << (8-bit)));
1194 for (i=0; i < (ssize_t) scanline_pad; i++)
1195 (void) WriteBlobByte(image,(unsigned char) 0);
1198 if (GetNextImageInList(next) == (Image *) NULL)
1200 next=SyncNextImageInList(next);
1201 status=SetImageProgress(next,SaveImagesTag,scene++,
1202 GetImageListLength(next));
1203 if (status == MagickFalse)
1205 } while ((next != (Image *) NULL) && (image_info->adjoin != MagickFalse));
1206 offset=SeekBlob(image,0,SEEK_SET);
1207 (void) WriteBlobLSBShort(image,0);
1208 (void) WriteBlobLSBShort(image,1);
1209 (void) WriteBlobLSBShort(image,(unsigned short) (scene+1));
1214 (void) WriteBlobByte(image,icon_file.directory[scene].width);
1215 (void) WriteBlobByte(image,icon_file.directory[scene].height);
1216 (void) WriteBlobByte(image,icon_file.directory[scene].colors);
1217 (void) WriteBlobByte(image,icon_file.directory[scene].reserved);
1218 (void) WriteBlobLSBShort(image,icon_file.directory[scene].planes);
1219 (void) WriteBlobLSBShort(image,icon_file.directory[scene].bits_per_pixel);
1220 (void) WriteBlobLSBLong(image,icon_file.directory[scene].size);
1221 (void) WriteBlobLSBLong(image,icon_file.directory[scene].offset);
1223 next=SyncNextImageInList(next);
1224 } while ((next != (Image *) NULL) && (image_info->adjoin != MagickFalse));
1225 (void) CloseBlob(image);