2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
10 % IIIII CCCC OOO N N %
13 % Read Microsoft Windows Icon Format %
20 % Copyright 1999-2009 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/colorspace.h"
47 #include "magick/exception.h"
48 #include "magick/exception-private.h"
49 #include "magick/image.h"
50 #include "magick/image-private.h"
51 #include "magick/list.h"
52 #include "magick/log.h"
53 #include "magick/magick.h"
54 #include "magick/memory_.h"
55 #include "magick/monitor.h"
56 #include "magick/monitor-private.h"
57 #include "magick/nt-feature.h"
58 #include "magick/quantize.h"
59 #include "magick/quantum-private.h"
60 #include "magick/static.h"
61 #include "magick/string_.h"
62 #include "magick/module.h"
67 #if !defined(__WINDOWS__) || defined(__MINGW32__)
70 #define BI_BITFIELDS 3
77 typedef struct _IconEntry
94 typedef struct _IconFile
105 typedef struct _IconInfo
138 Forward declaractions.
140 static MagickBooleanType
141 WriteICONImage(const ImageInfo *,Image *);
144 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
148 % R e a d I C O N I m a g e %
152 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
154 % ReadICONImage() reads a Microsoft icon image file and returns it. It
155 % allocates the memory necessary for the new Image structure and returns a
156 % pointer to the new image.
158 % The format of the ReadICONImage method is:
160 % Image *ReadICONImage(const ImageInfo *image_info,
161 % ExceptionInfo *exception)
163 % A description of each parameter follows:
165 % o image_info: the image info.
167 % o exception: return any errors or warnings in this structure.
170 static Image *ReadICONImage(const ImageInfo *image_info,
171 ExceptionInfo *exception)
198 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);
247 for (i=0; i < icon_file.count; i++)
250 Verify Icon identifier.
252 offset=(ssize_t) SeekBlob(image,(MagickOffsetType)
253 icon_file.directory[i].offset,SEEK_SET);
255 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
256 icon_info.size=ReadBlobLSBLong(image);
257 icon_info.width=(unsigned char) ReadBlobLSBLong(image);
258 icon_info.height=(unsigned char) ReadBlobLSBLong(image)/2;
259 if ((icon_file.directory[i].width == 0) &&
260 (icon_file.directory[i].height == 0))
275 Icon image encoded as a compressed PNG image.
277 length=icon_file.directory[i].size;
278 png=(unsigned char *) AcquireQuantumMemory(length+12,sizeof(*png));
279 if (png == (unsigned char *) NULL)
280 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
281 (void) CopyMagickMemory(png,"\211PNG\r\n\032\n\000\000\000\015",12);
282 count=ReadBlob(image,length,png+12);
283 if (count != (ssize_t) length)
285 png=(unsigned char *) RelinquishMagickMemory(png);
286 ThrowReaderException(CorruptImageError,
287 "InsufficientImageDataInFile");
289 read_info=CloneImageInfo(image_info);
290 (void) CopyMagickString(read_info->magick,"PNG",MaxTextExtent);
291 icon_image=BlobToImage(read_info,png,length+12,exception);
292 read_info=DestroyImageInfo(read_info);
293 png=(unsigned char *) RelinquishMagickMemory(png);
294 if (icon_image == (Image *) NULL)
296 image=DestroyImageList(image);
297 return((Image *) NULL);
299 DestroyBlob(icon_image);
300 icon_image->blob=ReferenceBlob(image->blob);
301 ReplaceImageInList(&image,icon_image);
305 icon_info.planes=ReadBlobLSBShort(image);
306 icon_info.bits_per_pixel=ReadBlobLSBShort(image);
307 if (icon_info.bits_per_pixel > 32)
308 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
309 icon_info.compression=ReadBlobLSBLong(image);
310 icon_info.image_size=ReadBlobLSBLong(image);
311 icon_info.x_pixels=ReadBlobLSBLong(image);
312 icon_info.y_pixels=ReadBlobLSBLong(image);
313 icon_info.number_colors=ReadBlobLSBLong(image);
314 icon_info.colors_important=ReadBlobLSBLong(image);
315 image->matte=MagickTrue;
316 image->columns=(unsigned long) icon_file.directory[i].width;
317 image->rows=(unsigned long) icon_file.directory[i].height;
319 if (image->debug != MagickFalse)
321 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
323 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
324 " size = %ld",icon_info.size);
325 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
326 " width = %d",icon_file.directory[i].width);
327 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
328 " height = %d",icon_file.directory[i].height);
329 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
330 " colors = %ld",icon_info.number_colors);
331 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
332 " planes = %d",icon_info.planes);
333 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
334 " bpp = %d",icon_info.bits_per_pixel);
336 if ((icon_info.number_colors != 0) || (icon_info.bits_per_pixel <= 16))
338 image->storage_class=PseudoClass;
339 image->colors=icon_info.number_colors;
340 if (image->colors == 0)
341 image->colors=1 << icon_info.bits_per_pixel;
343 if (image->storage_class == PseudoClass)
355 Read Icon raster colormap.
357 number_colors=1UL << icon_info.bits_per_pixel;
358 if (AcquireImageColormap(image,number_colors) == MagickFalse)
359 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
360 icon_colormap=(unsigned char *) AcquireQuantumMemory((size_t)
361 image->colors,4UL*sizeof(*icon_colormap));
362 if (icon_colormap == (unsigned char *) NULL)
363 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
364 count=ReadBlob(image,(size_t) (4*image->colors),icon_colormap);
365 if (count != (ssize_t) (4*image->colors))
366 ThrowReaderException(CorruptImageError,
367 "InsufficientImageDataInFile");
369 for (i=0; i < (long) image->colors; i++)
371 image->colormap[i].blue=(Quantum) ScaleCharToQuantum(*p++);
372 image->colormap[i].green=(Quantum) ScaleCharToQuantum(*p++);
373 image->colormap[i].red=(Quantum) ScaleCharToQuantum(*p++);
376 icon_colormap=(unsigned char *) RelinquishMagickMemory(icon_colormap);
379 Convert Icon raster image to pixel packets.
381 if ((image_info->ping != MagickFalse) &&
382 (image_info->number_scenes != 0))
383 if (image->scene >= (image_info->scene+image_info->number_scenes-1))
385 bytes_per_line=(((image->columns*icon_info.bits_per_pixel)+31) &
387 scanline_pad=((((image->columns*icon_info.bits_per_pixel)+31) & ~31)-
388 (image->columns*icon_info.bits_per_pixel)) >> 3;
389 switch (icon_info.bits_per_pixel)
394 Convert bitmap scanline.
396 for (y=(long) image->rows-1; y >= 0; y--)
398 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
399 if (q == (PixelPacket *) NULL)
401 indexes=GetAuthenticIndexQueue(image);
402 for (x=0; x < (long) (image->columns-7); x+=8)
404 byte=(unsigned long) ReadBlobByte(image);
405 for (bit=0; bit < 8; bit++)
406 indexes[x+bit]=(IndexPacket)
407 ((byte & (0x80 >> bit)) != 0 ? 0x01 : 0x00);
409 if ((image->columns % 8) != 0)
411 byte=(unsigned long) ReadBlobByte(image);
412 for (bit=0; bit < (image->columns % 8); bit++)
413 indexes[x+bit]=(IndexPacket)
414 ((byte & (0x80 >> bit)) != 0 ? 0x01 : 0x00);
416 for (x=0; x < (long) scanline_pad; x++)
417 (void) ReadBlobByte(image);
418 if (SyncAuthenticPixels(image,exception) == MagickFalse)
420 if (image->previous == (Image *) NULL)
422 status=SetImageProgress(image,LoadImageTag,image->rows-y-1,
424 if (status == MagickFalse)
433 Read 4-bit Icon scanline.
435 for (y=(long) image->rows-1; y >= 0; y--)
437 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
438 if (q == (PixelPacket *) NULL)
440 indexes=GetAuthenticIndexQueue(image);
441 for (x=0; x < ((long) image->columns-1); x+=2)
443 byte=(unsigned long) ReadBlobByte(image);
444 indexes[x]=(IndexPacket) ((byte >> 4) & 0xf);
445 indexes[x+1]=(IndexPacket) ((byte) & 0xf);
447 if ((image->columns % 2) != 0)
449 byte=(unsigned long) ReadBlobByte(image);
450 indexes[x]=(IndexPacket) ((byte >> 4) & 0xf);
452 for (x=0; x < (long) scanline_pad; x++)
453 (void) ReadBlobByte(image);
454 if (SyncAuthenticPixels(image,exception) == MagickFalse)
456 if (image->previous == (Image *) NULL)
458 status=SetImageProgress(image,LoadImageTag,image->rows-y-1,
460 if (status == MagickFalse)
469 Convert PseudoColor scanline.
471 for (y=(long) image->rows-1; y >= 0; y--)
473 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
474 if (q == (PixelPacket *) NULL)
476 indexes=GetAuthenticIndexQueue(image);
477 for (x=0; x < (long) image->columns; x++)
479 byte=(unsigned long) ReadBlobByte(image);
480 indexes[x]=(IndexPacket) byte;
482 for (x=0; x < (long) scanline_pad; x++)
483 (void) ReadBlobByte(image);
484 if (SyncAuthenticPixels(image,exception) == MagickFalse)
486 if (image->previous == (Image *) NULL)
488 status=SetImageProgress(image,LoadImageTag,image->rows-y-1,
490 if (status == MagickFalse)
499 Convert PseudoColor scanline.
501 for (y=(long) image->rows-1; y >= 0; y--)
503 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
504 if (q == (PixelPacket *) NULL)
506 indexes=GetAuthenticIndexQueue(image);
507 for (x=0; x < (long) image->columns; x++)
509 byte=(unsigned long) ReadBlobByte(image);
510 byte|=(unsigned long) (ReadBlobByte(image) << 8);
511 indexes[x]=(IndexPacket) byte;
513 for (x=0; x < (long) scanline_pad; x++)
514 (void) ReadBlobByte(image);
515 if (SyncAuthenticPixels(image,exception) == MagickFalse)
517 if (image->previous == (Image *) NULL)
519 status=SetImageProgress(image,LoadImageTag,image->rows-y-1,
521 if (status == MagickFalse)
531 Convert DirectColor scanline.
533 for (y=(long) image->rows-1; y >= 0; y--)
535 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
536 if (q == (PixelPacket *) NULL)
538 for (x=0; x < (long) image->columns; x++)
540 q->blue=ScaleCharToQuantum((unsigned char) ReadBlobByte(image));
541 q->green=ScaleCharToQuantum((unsigned char)
542 ReadBlobByte(image));
543 q->red=ScaleCharToQuantum((unsigned char) ReadBlobByte(image));
544 if (icon_info.bits_per_pixel == 32)
545 q->opacity=(Quantum) (QuantumRange-ScaleCharToQuantum(
546 (unsigned char) ReadBlobByte(image)));
549 if (icon_info.bits_per_pixel == 24)
550 for (x=0; x < (long) scanline_pad; x++)
551 (void) ReadBlobByte(image);
552 if (SyncAuthenticPixels(image,exception) == MagickFalse)
554 if (image->previous == (Image *) NULL)
556 status=SetImageProgress(image,LoadImageTag,image->rows-y-1,
558 if (status == MagickFalse)
565 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
567 (void) SyncImage(image);
568 if (icon_info.bits_per_pixel != 32)
571 Read the ICON alpha mask.
573 image->storage_class=DirectClass;
574 for (y=(long) image->rows-1; y >= 0; y--)
576 q=GetAuthenticPixels(image,0,y,image->columns,1,exception);
577 if (q == (PixelPacket *) NULL)
579 for (x=0; x < ((long) image->columns-7); x+=8)
581 byte=(unsigned long) ReadBlobByte(image);
582 for (bit=0; bit < 8; bit++)
583 q[x+bit].opacity=(Quantum) (((byte & (0x80 >> bit)) != 0) ?
584 TransparentOpacity : OpaqueOpacity);
586 if ((image->columns % 8) != 0)
588 byte=(unsigned long) ReadBlobByte(image);
589 for (bit=0; bit < (image->columns % 8); bit++)
590 q[x+bit].opacity=(Quantum) (((byte & (0x80 >> bit)) != 0) ?
591 TransparentOpacity : OpaqueOpacity);
593 for (x=0; x < (long) scanline_pad; x++)
594 (void) ReadBlobByte(image);
595 if (SyncAuthenticPixels(image,exception) == MagickFalse)
599 if (EOFBlob(image) != MagickFalse)
601 ThrowFileException(exception,CorruptImageError,
602 "UnexpectedEndOfFile",image->filename);
607 Proceed to next image.
609 if (image_info->number_scenes != 0)
610 if (image->scene >= (image_info->scene+image_info->number_scenes-1))
612 if (i < (long) (icon_file.count-1))
615 Allocate next image structure.
617 AcquireNextImage(image_info,image);
618 if (GetNextImageInList(image) == (Image *) NULL)
620 image=DestroyImageList(image);
621 return((Image *) NULL);
623 image=SyncNextImageInList(image);
624 status=SetImageProgress(image,LoadImagesTag,TellBlob(image),
626 if (status == MagickFalse)
630 (void) CloseBlob(image);
631 return(GetFirstImageInList(image));
635 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
639 % R e g i s t e r I C O N I m a g e %
643 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
645 % RegisterICONImage() adds attributes for the Icon image format to
646 % the list of supported formats. The attributes include the image format
647 % tag, a method to read and/or write the format, whether the format
648 % supports the saving of more than one frame to the same file or blob,
649 % whether the format supports native in-memory I/O, and a brief
650 % description of the format.
652 % The format of the RegisterICONImage method is:
654 % unsigned long RegisterICONImage(void)
657 ModuleExport unsigned long RegisterICONImage(void)
662 entry=SetMagickInfo("CUR");
663 entry->decoder=(DecodeImageHandler *) ReadICONImage;
664 entry->encoder=(EncodeImageHandler *) WriteICONImage;
665 entry->adjoin=MagickFalse;
666 entry->seekable_stream=MagickTrue;
667 entry->description=ConstantString("Microsoft icon");
668 entry->module=ConstantString("CUR");
669 (void) RegisterMagickInfo(entry);
670 entry=SetMagickInfo("ICO");
671 entry->decoder=(DecodeImageHandler *) ReadICONImage;
672 entry->encoder=(EncodeImageHandler *) WriteICONImage;
673 entry->adjoin=MagickTrue;
674 entry->seekable_stream=MagickTrue;
675 entry->description=ConstantString("Microsoft icon");
676 entry->module=ConstantString("ICON");
677 (void) RegisterMagickInfo(entry);
678 entry=SetMagickInfo("ICON");
679 entry->decoder=(DecodeImageHandler *) ReadICONImage;
680 entry->encoder=(EncodeImageHandler *) WriteICONImage;
681 entry->adjoin=MagickFalse;
682 entry->seekable_stream=MagickTrue;
683 entry->description=ConstantString("Microsoft icon");
684 entry->module=ConstantString("ICON");
685 (void) RegisterMagickInfo(entry);
686 return(MagickImageCoderSignature);
690 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
694 % U n r e g i s t e r I C O N I m a g e %
698 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
700 % UnregisterICONImage() removes format registrations made by the
701 % ICON module from the list of supported formats.
703 % The format of the UnregisterICONImage method is:
705 % UnregisterICONImage(void)
708 ModuleExport void UnregisterICONImage(void)
710 (void) UnregisterMagickInfo("CUR");
711 (void) UnregisterMagickInfo("ICO");
712 (void) UnregisterMagickInfo("ICON");
716 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
720 % W r i t e I C O N I m a g e %
724 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
726 % WriteICONImage() writes an image in Microsoft Windows bitmap encoded
727 % image format, version 3 for Windows or (if the image has a matte channel)
730 % The format of the WriteICONImage method is:
732 % MagickBooleanType WriteICONImage(const ImageInfo *image_info,
735 % A description of each parameter follows.
737 % o image_info: the image info.
739 % o image: The image.
742 static MagickBooleanType WriteICONImage(const ImageInfo *image_info,
764 register const IndexPacket
767 register const PixelPacket
774 register unsigned char
787 Open output image file.
789 assert(image_info != (const ImageInfo *) NULL);
790 assert(image_info->signature == MagickSignature);
791 assert(image != (Image *) NULL);
792 assert(image->signature == MagickSignature);
793 (void) LogMagickEvent(CoderEvent,GetMagickModule(),"%s",image->filename);
794 status=OpenBlob(image_info,image,WriteBinaryBlobMode,&image->exception);
795 if (status == MagickFalse)
801 if ((image->columns > 256L) || (image->rows > 256L))
802 ThrowWriterException(ImageError,"WidthOrHeightExceedsLimit");
804 next=SyncNextImageInList(next);
805 } while ((next != (Image *) NULL) && (image_info->adjoin != MagickFalse));
807 Dump out a ICON header template to be properly initialized later.
809 (void) WriteBlobLSBShort(image,0);
810 (void) WriteBlobLSBShort(image,1);
811 (void) WriteBlobLSBShort(image,(unsigned char) scene);
812 (void) ResetMagickMemory(&icon_file,0,sizeof(icon_file));
813 (void) ResetMagickMemory(&icon_info,0,sizeof(icon_info));
818 (void) WriteBlobByte(image,icon_file.directory[scene].width);
819 (void) WriteBlobByte(image,icon_file.directory[scene].height);
820 (void) WriteBlobByte(image,icon_file.directory[scene].colors);
821 (void) WriteBlobByte(image,icon_file.directory[scene].reserved);
822 (void) WriteBlobLSBShort(image,icon_file.directory[scene].planes);
823 (void) WriteBlobLSBShort(image,icon_file.directory[scene].bits_per_pixel);
824 (void) WriteBlobLSBLong(image,icon_file.directory[scene].size);
825 (void) WriteBlobLSBLong(image,icon_file.directory[scene].offset);
827 next=SyncNextImageInList(next);
828 } while ((next != (Image *) NULL) && (image_info->adjoin != MagickFalse));
833 if ((next->columns == 256) && (next->rows == 256))
848 Icon image encoded as a compressed PNG image.
850 write_image=CloneImage(next,0,0,MagickTrue,&image->exception);
851 if (write_image == (Image *) NULL)
853 write_info=CloneImageInfo(image_info);
854 (void) CopyMagickString(write_info->filename,"PNG:",MaxTextExtent);
855 png=(unsigned char *) ImageToBlob(write_info,write_image,&length,
857 write_image=DestroyImage(write_image);
858 write_info=DestroyImageInfo(write_info);
859 if (png == (unsigned char *) NULL)
861 icon_file.directory[scene].width=0;
862 icon_file.directory[scene].height=0;
863 icon_file.directory[scene].colors=0;
864 icon_file.directory[scene].reserved=0;
865 icon_file.directory[scene].planes=1;
866 icon_file.directory[scene].bits_per_pixel=32;
867 icon_file.directory[scene].size=(unsigned long) length;
868 icon_file.directory[scene].offset=(unsigned long) TellBlob(image);
869 (void) WriteBlob(image,(size_t) length,png);
870 png=(unsigned char *) RelinquishMagickMemory(png);
875 Initialize ICON raster file header.
877 if (next->colorspace != RGBColorspace)
878 (void) TransformImageColorspace(next,RGBColorspace);
879 icon_info.file_size=14+12+28;
880 icon_info.offset_bits=icon_info.file_size;
881 icon_info.compression=BI_RGB;
882 if ((next->storage_class != DirectClass) && (next->colors > 256))
883 (void) SetImageStorageClass(next,DirectClass);
884 if (next->storage_class == DirectClass)
887 Full color ICON raster.
889 icon_info.number_colors=0;
890 icon_info.bits_per_pixel=32;
891 icon_info.compression=(unsigned long) BI_RGB;
896 Colormapped ICON raster.
898 icon_info.bits_per_pixel=8;
899 if (next->colors <= 256)
900 icon_info.bits_per_pixel=8;
901 if (next->colors <= 16)
902 icon_info.bits_per_pixel=4;
903 if (next->colors <= 2)
904 icon_info.bits_per_pixel=1;
905 icon_info.number_colors=1 << icon_info.bits_per_pixel;
906 if (icon_info.number_colors < next->colors)
908 (void) SetImageStorageClass(next,DirectClass);
909 icon_info.number_colors=0;
910 icon_info.bits_per_pixel=(unsigned short) 24;
911 icon_info.compression=(unsigned long) BI_RGB;
915 icon_info.file_size+=3*(1UL << icon_info.bits_per_pixel);
916 icon_info.offset_bits+=3*(1UL << icon_info.bits_per_pixel);
917 icon_info.file_size+=(1UL << icon_info.bits_per_pixel);
918 icon_info.offset_bits+=(1UL << icon_info.bits_per_pixel);
921 bytes_per_line=(((next->columns*icon_info.bits_per_pixel)+31) &
923 icon_info.ba_offset=0;
924 icon_info.width=(long) next->columns;
925 icon_info.height=(long) next->rows;
927 icon_info.image_size=bytes_per_line*next->rows;
929 icon_info.size+=(4*icon_info.number_colors);
930 icon_info.size+=icon_info.image_size;
931 icon_info.size+=(((icon_info.width+31) & ~31) >> 3)*icon_info.height;
932 icon_info.file_size+=icon_info.image_size;
933 icon_info.x_pixels=0;
934 icon_info.y_pixels=0;
937 case UndefinedResolution:
938 case PixelsPerInchResolution:
940 icon_info.x_pixels=(unsigned long) (100.0*next->x_resolution/2.54);
941 icon_info.y_pixels=(unsigned long) (100.0*next->y_resolution/2.54);
944 case PixelsPerCentimeterResolution:
946 icon_info.x_pixels=(unsigned long) (100.0*next->x_resolution);
947 icon_info.y_pixels=(unsigned long) (100.0*next->y_resolution);
951 icon_info.colors_important=icon_info.number_colors;
953 Convert MIFF to ICON raster pixels.
955 pixels=(unsigned char *) AcquireQuantumMemory((size_t)
956 icon_info.image_size,sizeof(*pixels));
957 if (pixels == (unsigned char *) NULL)
958 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
959 (void) ResetMagickMemory(pixels,0,(size_t) icon_info.image_size);
960 switch (icon_info.bits_per_pixel)
969 Convert PseudoClass image to a ICON monochrome image.
971 for (y=0; y < (long) next->rows; y++)
973 p=GetVirtualPixels(next,0,y,next->columns,1,&next->exception);
974 if (p == (const PixelPacket *) NULL)
976 indexes=GetVirtualIndexQueue(next);
977 q=pixels+(next->rows-y-1)*bytes_per_line;
980 for (x=0; x < (long) next->columns; x++)
983 byte|=indexes[x] != 0 ? 0x01 : 0x00;
987 *q++=(unsigned char) byte;
993 *q++=(unsigned char) (byte << (8-bit));
994 if (next->previous == (Image *) NULL)
996 status=SetImageProgress(next,SaveImageTag,y,next->rows);
997 if (status == MagickFalse)
1010 Convert PseudoClass image to a ICON monochrome image.
1012 for (y=0; y < (long) next->rows; y++)
1014 p=GetVirtualPixels(next,0,y,next->columns,1,&next->exception);
1015 if (p == (const PixelPacket *) NULL)
1017 indexes=GetVirtualIndexQueue(next);
1018 q=pixels+(next->rows-y-1)*bytes_per_line;
1021 for (x=0; x < (long) next->columns; x++)
1024 byte|=((unsigned long) indexes[x] & 0x0f);
1028 *q++=(unsigned char) byte;
1034 *q++=(unsigned char) (byte << 4);
1035 if (next->previous == (Image *) NULL)
1037 status=SetImageProgress(next,SaveImageTag,y,next->rows);
1038 if (status == MagickFalse)
1047 Convert PseudoClass packet to ICON pixel.
1049 for (y=0; y < (long) next->rows; y++)
1051 p=GetVirtualPixels(next,0,y,next->columns,1,&next->exception);
1052 if (p == (const PixelPacket *) NULL)
1054 indexes=GetVirtualIndexQueue(next);
1055 q=pixels+(next->rows-y-1)*bytes_per_line;
1056 for (x=0; x < (long) next->columns; x++)
1057 *q++=(unsigned char) indexes[x];
1058 if (next->previous == (Image *) NULL)
1060 status=SetImageProgress(next,SaveImageTag,y,next->rows);
1061 if (status == MagickFalse)
1071 Convert DirectClass packet to ICON BGR888 or BGRA8888 pixel.
1073 for (y=0; y < (long) next->rows; y++)
1075 p=GetVirtualPixels(next,0,y,next->columns,1,&next->exception);
1076 if (p == (const PixelPacket *) NULL)
1078 q=pixels+(next->rows-y-1)*bytes_per_line;
1079 for (x=0; x < (long) next->columns; x++)
1081 *q++=ScaleQuantumToChar(p->blue);
1082 *q++=ScaleQuantumToChar(p->green);
1083 *q++=ScaleQuantumToChar(p->red);
1084 if (next->matte == MagickFalse)
1085 *q++=ScaleQuantumToChar(QuantumRange);
1087 *q++=ScaleQuantumToChar(QuantumRange-p->opacity);
1090 if (icon_info.bits_per_pixel == 24)
1091 for (x=3L*(long) next->columns; x < (long) bytes_per_line; x++)
1093 if (next->previous == (Image *) NULL)
1095 status=SetImageProgress(next,SaveImageTag,y,next->rows);
1096 if (status == MagickFalse)
1104 Write 40-byte version 3+ bitmap header.
1106 icon_file.directory[scene].width=(unsigned char) icon_info.width;
1107 icon_file.directory[scene].height=(unsigned char) icon_info.height;
1108 icon_file.directory[scene].colors=(unsigned char)
1109 icon_info.number_colors;
1110 icon_file.directory[scene].reserved=0;
1111 icon_file.directory[scene].planes=icon_info.planes;
1112 icon_file.directory[scene].bits_per_pixel=icon_info.bits_per_pixel;
1113 icon_file.directory[scene].size=icon_info.size;
1114 icon_file.directory[scene].offset=(unsigned long) TellBlob(image);
1115 (void) WriteBlobLSBLong(image,(unsigned long) 40);
1116 (void) WriteBlobLSBLong(image,(unsigned long) icon_info.width);
1117 (void) WriteBlobLSBLong(image,(unsigned long) icon_info.height*2);
1118 (void) WriteBlobLSBShort(image,icon_info.planes);
1119 (void) WriteBlobLSBShort(image,icon_info.bits_per_pixel);
1120 (void) WriteBlobLSBLong(image,icon_info.compression);
1121 (void) WriteBlobLSBLong(image,icon_info.image_size);
1122 (void) WriteBlobLSBLong(image,icon_info.x_pixels);
1123 (void) WriteBlobLSBLong(image,icon_info.y_pixels);
1124 (void) WriteBlobLSBLong(image,icon_info.number_colors);
1125 (void) WriteBlobLSBLong(image,icon_info.colors_important);
1126 if (next->storage_class == PseudoClass)
1132 Dump colormap to file.
1134 icon_colormap=(unsigned char *) AcquireQuantumMemory((size_t)
1135 (1UL << icon_info.bits_per_pixel),4UL*sizeof(*icon_colormap));
1136 if (icon_colormap == (unsigned char *) NULL)
1137 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
1139 for (i=0; i < (long) next->colors; i++)
1141 *q++=ScaleQuantumToChar(next->colormap[i].blue);
1142 *q++=ScaleQuantumToChar(next->colormap[i].green);
1143 *q++=ScaleQuantumToChar(next->colormap[i].red);
1144 *q++=(unsigned char) 0x0;
1146 for ( ; i < (long) (1UL << icon_info.bits_per_pixel); i++)
1148 *q++=(unsigned char) 0x00;
1149 *q++=(unsigned char) 0x00;
1150 *q++=(unsigned char) 0x00;
1151 *q++=(unsigned char) 0x00;
1153 (void) WriteBlob(image,(size_t) (4UL*(1UL <<
1154 icon_info.bits_per_pixel)),icon_colormap);
1155 icon_colormap=(unsigned char *) RelinquishMagickMemory(
1158 (void) WriteBlob(image,(size_t) icon_info.image_size,pixels);
1159 pixels=(unsigned char *) RelinquishMagickMemory(pixels);
1163 scanline_pad=(((next->columns+31) & ~31)-next->columns) >> 3;
1164 for (y=((long) next->rows - 1); y >= 0; y--)
1166 p=GetVirtualPixels(next,0,y,next->columns,1,&next->exception);
1167 if (p == (const PixelPacket *) NULL)
1171 for (x=0; x < (long) next->columns; x++)
1174 if ((next->matte == MagickTrue) &&
1175 (p->opacity == (Quantum) TransparentOpacity))
1180 (void) WriteBlobByte(image,(unsigned char) byte);
1187 (void) WriteBlobByte(image,(unsigned char) (byte << (8-bit)));
1188 for (i=0; i < (long) scanline_pad; i++)
1189 (void) WriteBlobByte(image,(unsigned char) 0);
1192 if (GetNextImageInList(next) == (Image *) NULL)
1194 next=SyncNextImageInList(next);
1195 status=SetImageProgress(next,SaveImagesTag,scene++,
1196 GetImageListLength(next));
1197 if (status == MagickFalse)
1199 } while ((next != (Image *) NULL) && (image_info->adjoin != MagickFalse));
1200 offset=SeekBlob(image,0,SEEK_SET);
1201 (void) WriteBlobLSBShort(image,0);
1202 (void) WriteBlobLSBShort(image,1);
1203 (void) WriteBlobLSBShort(image,(unsigned short) (scene+1));
1208 (void) WriteBlobByte(image,icon_file.directory[scene].width);
1209 (void) WriteBlobByte(image,icon_file.directory[scene].height);
1210 (void) WriteBlobByte(image,icon_file.directory[scene].colors);
1211 (void) WriteBlobByte(image,icon_file.directory[scene].reserved);
1212 (void) WriteBlobLSBShort(image,icon_file.directory[scene].planes);
1213 (void) WriteBlobLSBShort(image,icon_file.directory[scene].bits_per_pixel);
1214 (void) WriteBlobLSBLong(image,icon_file.directory[scene].size);
1215 (void) WriteBlobLSBLong(image,icon_file.directory[scene].offset);
1217 next=SyncNextImageInList(next);
1218 } while ((next != (Image *) NULL) && (image_info->adjoin != MagickFalse));
1219 (void) CloseBlob(image);