2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
13 % Read/Write Irix RGB Image Format %
20 % Copyright 1999-2012 ImageMagick Studio LLC, a non-profit organization %
21 % dedicated to making software imaging solutions freely available. %
23 % You may not use this file except in compliance with the License. You may %
24 % obtain a copy of the License at %
26 % http://www.imagemagick.org/script/license.php %
28 % Unless required by applicable law or agreed to in writing, software %
29 % distributed under the License is distributed on an "AS IS" BASIS, %
30 % WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. %
31 % See the License for the specific language governing permissions and %
32 % limitations under the License. %
34 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
42 #include "MagickCore/studio.h"
43 #include "MagickCore/attribute.h"
44 #include "MagickCore/blob.h"
45 #include "MagickCore/blob-private.h"
46 #include "MagickCore/cache.h"
47 #include "MagickCore/color.h"
48 #include "MagickCore/color-private.h"
49 #include "MagickCore/colormap.h"
50 #include "MagickCore/colorspace.h"
51 #include "MagickCore/colorspace-private.h"
52 #include "MagickCore/exception.h"
53 #include "MagickCore/exception-private.h"
54 #include "MagickCore/image.h"
55 #include "MagickCore/image-private.h"
56 #include "MagickCore/list.h"
57 #include "MagickCore/magick.h"
58 #include "MagickCore/memory_.h"
59 #include "MagickCore/monitor.h"
60 #include "MagickCore/monitor-private.h"
61 #include "MagickCore/pixel-accessor.h"
62 #include "MagickCore/property.h"
63 #include "MagickCore/quantum-private.h"
64 #include "MagickCore/static.h"
65 #include "MagickCore/string_.h"
66 #include "MagickCore/module.h"
69 Typedef declaractions.
71 typedef struct _SGIInfo
102 Forward declarations.
104 static MagickBooleanType
105 WriteSGIImage(const ImageInfo *,Image *,ExceptionInfo *);
107 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
115 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
117 % IsSGI() returns MagickTrue if the image format type, identified by the
118 % magick string, is SGI.
120 % The format of the IsSGI method is:
122 % MagickBooleanType IsSGI(const unsigned char *magick,const size_t length)
124 % A description of each parameter follows:
126 % o magick: compare image format pattern against these bytes.
128 % o length: Specifies the length of the magick string.
131 static MagickBooleanType IsSGI(const unsigned char *magick,const size_t length)
135 if (memcmp(magick,"\001\332",2) == 0)
141 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
145 % R e a d S G I I m a g e %
149 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
151 % ReadSGIImage() reads a SGI RGB image file and returns it. It
152 % allocates the memory necessary for the new Image structure and returns a
153 % pointer to the new image.
155 % The format of the ReadSGIImage method is:
157 % Image *ReadSGIImage(const ImageInfo *image_info,ExceptionInfo *exception)
159 % A description of each parameter follows:
161 % o image_info: the image info.
163 % o exception: return any errors or warnings in this structure.
167 static inline size_t MagickMin(const size_t x,const size_t y)
174 static MagickBooleanType SGIDecode(const size_t bytes_per_pixel,
175 ssize_t number_packets,unsigned char *packets,ssize_t number_pixels,
176 unsigned char *pixels)
178 register unsigned char
190 if (bytes_per_pixel == 2)
192 for ( ; number_pixels > 0; )
194 if (number_packets-- == 0)
196 pixel=(size_t) (*p++) << 8;
198 count=(ssize_t) (pixel & 0x7f);
201 if (count > (ssize_t) number_pixels)
203 number_pixels-=count;
204 if ((pixel & 0x80) != 0)
205 for ( ; count != 0; count--)
207 if (number_packets-- == 0)
215 pixel=(size_t) (*p++) << 8;
217 for ( ; count != 0; count--)
219 if (number_packets-- == 0)
221 *q=(unsigned char) (pixel >> 8);
222 *(q+1)=(unsigned char) pixel;
229 for ( ; number_pixels > 0; )
231 if (number_packets-- == 0)
233 pixel=(size_t) (*p++);
234 count=(ssize_t) (pixel & 0x7f);
237 if (count > (ssize_t) number_pixels)
239 number_pixels-=count;
240 if ((pixel & 0x80) != 0)
241 for ( ; count != 0; count--)
243 if (number_packets-- == 0)
250 if (number_packets-- == 0)
252 pixel=(size_t) (*p++);
253 for ( ; count != 0; count--)
255 *q=(unsigned char) pixel;
263 static Image *ReadSGIImage(const ImageInfo *image_info,ExceptionInfo *exception)
281 register unsigned char
302 assert(image_info != (const ImageInfo *) NULL);
303 assert(image_info->signature == MagickSignature);
304 if (image_info->debug != MagickFalse)
305 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
306 image_info->filename);
307 assert(exception != (ExceptionInfo *) NULL);
308 assert(exception->signature == MagickSignature);
309 image=AcquireImage(image_info,exception);
310 status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception);
311 if (status == MagickFalse)
313 image=DestroyImageList(image);
314 return((Image *) NULL);
317 Read SGI raster header.
319 iris_info.magic=ReadBlobMSBShort(image);
323 Verify SGI identifier.
325 if (iris_info.magic != 0x01DA)
326 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
327 iris_info.storage=(unsigned char) ReadBlobByte(image);
328 switch (iris_info.storage)
330 case 0x00: image->compression=NoCompression; break;
331 case 0x01: image->compression=RLECompression; break;
333 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
335 iris_info.bytes_per_pixel=(unsigned char) ReadBlobByte(image);
336 if ((iris_info.bytes_per_pixel == 0) || (iris_info.bytes_per_pixel > 2))
337 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
338 iris_info.dimension=ReadBlobMSBShort(image);
339 iris_info.columns=ReadBlobMSBShort(image);
340 iris_info.rows=ReadBlobMSBShort(image);
341 iris_info.depth=ReadBlobMSBShort(image);
342 if ((iris_info.depth == 0) || (iris_info.depth > 4))
343 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
344 iris_info.minimum_value=ReadBlobMSBLong(image);
345 iris_info.maximum_value=ReadBlobMSBLong(image);
346 iris_info.sans=ReadBlobMSBLong(image);
347 (void) ReadBlob(image,sizeof(iris_info.name),(unsigned char *)
349 iris_info.name[sizeof(iris_info.name)-1]='\0';
350 if (*iris_info.name != '\0')
351 (void) SetImageProperty(image,"label",iris_info.name,exception);
352 iris_info.pixel_format=ReadBlobMSBLong(image);
353 if (iris_info.pixel_format != 0)
354 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
355 count=ReadBlob(image,sizeof(iris_info.filler),iris_info.filler);
357 image->columns=iris_info.columns;
358 image->rows=iris_info.rows;
359 image->depth=(size_t) MagickMin(iris_info.depth,MAGICKCORE_QUANTUM_DEPTH);
360 if (iris_info.pixel_format == 0)
361 image->depth=(size_t) MagickMin((size_t) 8*
362 iris_info.bytes_per_pixel,MAGICKCORE_QUANTUM_DEPTH);
363 if (iris_info.depth < 3)
365 image->storage_class=PseudoClass;
366 image->colors=iris_info.bytes_per_pixel > 1 ? 65535 : 256;
368 if ((image_info->ping != MagickFalse) && (image_info->number_scenes != 0))
369 if (image->scene >= (image_info->scene+image_info->number_scenes-1))
374 bytes_per_pixel=(size_t) iris_info.bytes_per_pixel;
375 number_pixels=(MagickSizeType) iris_info.columns*iris_info.rows;
376 if ((4*bytes_per_pixel*number_pixels) != ((MagickSizeType) (size_t)
377 (4*bytes_per_pixel*number_pixels)))
378 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
379 iris_pixels=(unsigned char *) AcquireQuantumMemory(iris_info.columns,
380 iris_info.rows*4*bytes_per_pixel*sizeof(*iris_pixels));
381 if (iris_pixels == (unsigned char *) NULL)
382 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
383 if ((int) iris_info.storage != 0x01)
389 Read standard image format.
391 scanline=(unsigned char *) AcquireQuantumMemory(iris_info.columns,
392 bytes_per_pixel*sizeof(*scanline));
393 if (scanline == (unsigned char *) NULL)
394 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
395 for (z=0; z < (ssize_t) iris_info.depth; z++)
397 p=iris_pixels+bytes_per_pixel*z;
398 for (y=0; y < (ssize_t) iris_info.rows; y++)
400 count=ReadBlob(image,bytes_per_pixel*iris_info.columns,scanline);
401 if (EOFBlob(image) != MagickFalse)
403 if (bytes_per_pixel == 2)
404 for (x=0; x < (ssize_t) iris_info.columns; x++)
407 *(p+1)=scanline[2*x+1];
411 for (x=0; x < (ssize_t) iris_info.columns; x++)
418 scanline=(unsigned char *) RelinquishMagickMemory(scanline);
436 Read runlength-encoded image format.
438 offsets=(ssize_t *) AcquireQuantumMemory((size_t) iris_info.rows,
439 iris_info.depth*sizeof(*offsets));
440 packets=(unsigned char *) AcquireQuantumMemory((size_t)
441 iris_info.columns+10UL,4UL*sizeof(*packets));
442 runlength=(size_t *) AcquireQuantumMemory(iris_info.rows,
443 iris_info.depth*sizeof(*runlength));
444 if ((offsets == (ssize_t *) NULL) ||
445 (packets == (unsigned char *) NULL) ||
446 (runlength == (size_t *) NULL))
447 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
448 for (i=0; i < (ssize_t) (iris_info.rows*iris_info.depth); i++)
449 offsets[i]=(int) ReadBlobMSBLong(image);
450 for (i=0; i < (ssize_t) (iris_info.rows*iris_info.depth); i++)
452 runlength[i]=ReadBlobMSBLong(image);
453 if (runlength[i] > (4*(size_t) iris_info.columns+10))
454 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
461 for (y=0; ((y < (ssize_t) iris_info.rows) && (data_order == 0)); y++)
462 for (z=0; ((z < (ssize_t) iris_info.depth) && (data_order == 0)); z++)
464 if (offsets[y+z*iris_info.rows] < offset)
466 offset=offsets[y+z*iris_info.rows];
468 offset=(ssize_t) TellBlob(image);
471 for (z=0; z < (ssize_t) iris_info.depth; z++)
474 for (y=0; y < (ssize_t) iris_info.rows; y++)
476 if (offset != offsets[y+z*iris_info.rows])
478 offset=offsets[y+z*iris_info.rows];
479 offset=(ssize_t) SeekBlob(image,(ssize_t) offset,SEEK_SET);
481 count=ReadBlob(image,(size_t) runlength[y+z*iris_info.rows],
483 if (EOFBlob(image) != MagickFalse)
485 offset+=(ssize_t) runlength[y+z*iris_info.rows];
486 status=SGIDecode(bytes_per_pixel,(ssize_t)
487 (runlength[y+z*iris_info.rows]/bytes_per_pixel),packets,
488 1L*iris_info.columns,p+bytes_per_pixel*z);
489 if (status == MagickFalse)
490 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
491 p+=(iris_info.columns*4*bytes_per_pixel);
500 position=TellBlob(image);
502 for (y=0; y < (ssize_t) iris_info.rows; y++)
504 for (z=0; z < (ssize_t) iris_info.depth; z++)
506 if (offset != offsets[y+z*iris_info.rows])
508 offset=offsets[y+z*iris_info.rows];
509 offset=(ssize_t) SeekBlob(image,(ssize_t) offset,SEEK_SET);
511 count=ReadBlob(image,(size_t) runlength[y+z*iris_info.rows],
513 if (EOFBlob(image) != MagickFalse)
515 offset+=(ssize_t) runlength[y+z*iris_info.rows];
516 status=SGIDecode(bytes_per_pixel,(ssize_t)
517 (runlength[y+z*iris_info.rows]/bytes_per_pixel),packets,
518 1L*iris_info.columns,p+bytes_per_pixel*z);
519 if (status == MagickFalse)
520 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
522 p+=(iris_info.columns*4*bytes_per_pixel);
524 offset=(ssize_t) SeekBlob(image,position,SEEK_SET);
526 runlength=(size_t *) RelinquishMagickMemory(runlength);
527 packets=(unsigned char *) RelinquishMagickMemory(packets);
528 offsets=(ssize_t *) RelinquishMagickMemory(offsets);
531 Initialize image structure.
533 image->matte=iris_info.depth == 4 ? MagickTrue : MagickFalse;
534 image->columns=iris_info.columns;
535 image->rows=iris_info.rows;
537 Convert SGI raster image to pixel packets.
539 if (image->storage_class == DirectClass)
542 Convert SGI image to DirectClass pixel packets.
544 if (bytes_per_pixel == 2)
546 for (y=0; y < (ssize_t) image->rows; y++)
548 p=iris_pixels+(image->rows-y-1)*8*image->columns;
549 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
550 if (q == (Quantum *) NULL)
552 for (x=0; x < (ssize_t) image->columns; x++)
554 SetPixelRed(image,ScaleShortToQuantum((unsigned short)
555 ((*(p+0) << 8) | (*(p+1)))),q);
556 SetPixelGreen(image,ScaleShortToQuantum((unsigned short)
557 ((*(p+2) << 8) | (*(p+3)))),q);
558 SetPixelBlue(image,ScaleShortToQuantum((unsigned short)
559 ((*(p+4) << 8) | (*(p+5)))),q);
560 SetPixelAlpha(image,OpaqueAlpha,q);
561 if (image->matte != MagickFalse)
562 SetPixelAlpha(image,ScaleShortToQuantum((unsigned short)
563 ((*(p+6) << 8) | (*(p+7)))),q);
565 q+=GetPixelChannels(image);
567 if (SyncAuthenticPixels(image,exception) == MagickFalse)
569 if (image->previous == (Image *) NULL)
571 status=SetImageProgress(image,LoadImageTag,(MagickOffsetType)
573 if (status == MagickFalse)
579 for (y=0; y < (ssize_t) image->rows; y++)
581 p=iris_pixels+(image->rows-y-1)*4*image->columns;
582 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
583 if (q == (Quantum *) NULL)
585 for (x=0; x < (ssize_t) image->columns; x++)
587 SetPixelRed(image,ScaleCharToQuantum(*p),q);
588 SetPixelGreen(image,ScaleCharToQuantum(*(p+1)),q);
589 SetPixelBlue(image,ScaleCharToQuantum(*(p+2)),q);
590 SetPixelAlpha(image,OpaqueAlpha,q);
591 if (image->matte != MagickFalse)
592 SetPixelAlpha(image,ScaleCharToQuantum(*(p+3)),q);
594 q+=GetPixelChannels(image);
596 if (SyncAuthenticPixels(image,exception) == MagickFalse)
598 if (image->previous == (Image *) NULL)
600 status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y,
602 if (status == MagickFalse)
610 Create grayscale map.
612 if (AcquireImageColormap(image,image->colors,exception) == MagickFalse)
613 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
615 Convert SGI image to PseudoClass pixel packets.
617 if (bytes_per_pixel == 2)
619 for (y=0; y < (ssize_t) image->rows; y++)
621 p=iris_pixels+(image->rows-y-1)*8*image->columns;
622 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
623 if (q == (Quantum *) NULL)
625 for (x=0; x < (ssize_t) image->columns; x++)
629 SetPixelIndex(image,quantum,q);
631 q+=GetPixelChannels(image);
633 if (SyncAuthenticPixels(image,exception) == MagickFalse)
635 if (image->previous == (Image *) NULL)
637 status=SetImageProgress(image,LoadImageTag,(MagickOffsetType)
639 if (status == MagickFalse)
645 for (y=0; y < (ssize_t) image->rows; y++)
647 p=iris_pixels+(image->rows-y-1)*4*image->columns;
648 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
649 if (q == (Quantum *) NULL)
651 for (x=0; x < (ssize_t) image->columns; x++)
653 SetPixelIndex(image,*p,q);
655 q+=GetPixelChannels(image);
657 if (SyncAuthenticPixels(image,exception) == MagickFalse)
659 if (image->previous == (Image *) NULL)
661 status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y,
663 if (status == MagickFalse)
667 (void) SyncImage(image,exception);
669 iris_pixels=(unsigned char *) RelinquishMagickMemory(iris_pixels);
670 if (EOFBlob(image) != MagickFalse)
672 ThrowFileException(exception,CorruptImageError,"UnexpectedEndOfFile",
677 Proceed to next image.
679 if (image_info->number_scenes != 0)
680 if (image->scene >= (image_info->scene+image_info->number_scenes-1))
682 iris_info.magic=ReadBlobMSBShort(image);
683 if (iris_info.magic == 0x01DA)
686 Allocate next image structure.
688 AcquireNextImage(image_info,image,exception);
689 if (GetNextImageInList(image) == (Image *) NULL)
691 image=DestroyImageList(image);
692 return((Image *) NULL);
694 image=SyncNextImageInList(image);
695 status=SetImageProgress(image,LoadImagesTag,TellBlob(image),
697 if (status == MagickFalse)
700 } while (iris_info.magic == 0x01DA);
701 (void) CloseBlob(image);
702 return(GetFirstImageInList(image));
706 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
710 % R e g i s t e r S G I I m a g e %
714 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
716 % RegisterSGIImage() adds properties for the SGI image format to
717 % the list of supported formats. The properties include the image format
718 % tag, a method to read and/or write the format, whether the format
719 % supports the saving of more than one frame to the same file or blob,
720 % whether the format supports native in-memory I/O, and a brief
721 % description of the format.
723 % The format of the RegisterSGIImage method is:
725 % size_t RegisterSGIImage(void)
728 ModuleExport size_t RegisterSGIImage(void)
733 entry=SetMagickInfo("SGI");
734 entry->decoder=(DecodeImageHandler *) ReadSGIImage;
735 entry->encoder=(EncodeImageHandler *) WriteSGIImage;
736 entry->magick=(IsImageFormatHandler *) IsSGI;
737 entry->description=ConstantString("Irix RGB image");
738 entry->module=ConstantString("SGI");
739 entry->seekable_stream=MagickTrue;
740 (void) RegisterMagickInfo(entry);
741 return(MagickImageCoderSignature);
745 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
749 % U n r e g i s t e r S G I I m a g e %
753 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
755 % UnregisterSGIImage() removes format registrations made by the
756 % SGI module from the list of supported formats.
758 % The format of the UnregisterSGIImage method is:
760 % UnregisterSGIImage(void)
763 ModuleExport void UnregisterSGIImage(void)
765 (void) UnregisterMagickInfo("SGI");
769 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
773 % W r i t e S G I I m a g e %
777 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
779 % WriteSGIImage() writes an image in SGI RGB encoded image format.
781 % The format of the WriteSGIImage method is:
783 % MagickBooleanType WriteSGIImage(const ImageInfo *image_info,
784 % Image *image,ExceptionInfo *exception)
786 % A description of each parameter follows.
788 % o image_info: the image info.
790 % o image: The image.
792 % o exception: return any errors or warnings in this structure.
796 static size_t SGIEncode(unsigned char *pixels,size_t length,
797 unsigned char *packets)
802 register unsigned char
817 while ((p < limit) && ((*(p-8) != *(p-4)) || (*(p-4) != *p)))
820 length=(size_t) (p-mark) >> 2;
823 runlength=(short) (length > 126 ? 126 : length);
825 *q++=(unsigned char) (0x80 | runlength);
826 for ( ; runlength > 0; runlength--)
834 while ((p < limit) && (*p == *mark))
836 length=(size_t) (p-mark) >> 2;
839 runlength=(short) (length > 126 ? 126 : length);
841 *q++=(unsigned char) runlength;
846 return((size_t) (q-packets));
849 static MagickBooleanType WriteSGIImage(const ImageInfo *image_info,Image *image,
850 ExceptionInfo *exception)
870 register const Quantum
877 register unsigned char
889 Open output image file.
891 assert(image_info != (const ImageInfo *) NULL);
892 assert(image_info->signature == MagickSignature);
893 assert(image != (Image *) NULL);
894 assert(image->signature == MagickSignature);
895 if (image->debug != MagickFalse)
896 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
897 if ((image->columns > 65535UL) || (image->rows > 65535UL))
898 ThrowWriterException(ImageError,"WidthOrHeightExceedsLimit");
899 assert(exception != (ExceptionInfo *) NULL);
900 assert(exception->signature == MagickSignature);
901 status=OpenBlob(image_info,image,WriteBinaryBlobMode,exception);
902 if (status == MagickFalse)
908 Initialize SGI raster file header.
910 if (IsRGBColorspace(image->colorspace) == MagickFalse)
911 (void) TransformImageColorspace(image,sRGBColorspace,exception);
912 (void) ResetMagickMemory(&iris_info,0,sizeof(iris_info));
913 iris_info.magic=0x01DA;
914 compression=image->compression;
915 if (image_info->compression != UndefinedCompression)
916 compression=image_info->compression;
917 if (image->depth > 8)
918 compression=NoCompression;
919 if (compression == NoCompression)
920 iris_info.storage=(unsigned char) 0x00;
922 iris_info.storage=(unsigned char) 0x01;
923 iris_info.bytes_per_pixel=(unsigned char) (image->depth > 8 ? 2 : 1);
924 iris_info.dimension=3;
925 iris_info.columns=(unsigned short) image->columns;
926 iris_info.rows=(unsigned short) image->rows;
927 if (image->matte != MagickFalse)
931 if ((image_info->type != TrueColorType) &&
932 (IsImageGray(image,exception) != MagickFalse))
934 iris_info.dimension=2;
940 iris_info.minimum_value=0;
941 iris_info.maximum_value=(size_t) (image->depth <= 8 ?
942 1UL*ScaleQuantumToChar((Quantum) QuantumRange) :
943 1UL*ScaleQuantumToShort((Quantum) QuantumRange));
947 (void) WriteBlobMSBShort(image,iris_info.magic);
948 (void) WriteBlobByte(image,iris_info.storage);
949 (void) WriteBlobByte(image,iris_info.bytes_per_pixel);
950 (void) WriteBlobMSBShort(image,iris_info.dimension);
951 (void) WriteBlobMSBShort(image,iris_info.columns);
952 (void) WriteBlobMSBShort(image,iris_info.rows);
953 (void) WriteBlobMSBShort(image,iris_info.depth);
954 (void) WriteBlobMSBLong(image,(unsigned int) iris_info.minimum_value);
955 (void) WriteBlobMSBLong(image,(unsigned int) iris_info.maximum_value);
956 (void) WriteBlobMSBLong(image,(unsigned int) iris_info.sans);
957 value=GetImageProperty(image,"label",exception);
958 if (value != (const char *) NULL)
959 (void) CopyMagickString(iris_info.name,value,sizeof(iris_info.name));
960 (void) WriteBlob(image,sizeof(iris_info.name),(unsigned char *)
962 (void) WriteBlobMSBLong(image,(unsigned int) iris_info.pixel_format);
963 (void) WriteBlob(image,sizeof(iris_info.filler),iris_info.filler);
967 number_pixels=(MagickSizeType) image->columns*image->rows;
968 if ((4*iris_info.bytes_per_pixel*number_pixels) !=
969 ((MagickSizeType) (size_t) (4*iris_info.bytes_per_pixel*number_pixels)))
970 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
971 iris_pixels=(unsigned char *) AcquireQuantumMemory((size_t) number_pixels,
972 4*iris_info.bytes_per_pixel*sizeof(*iris_pixels));
973 if (iris_pixels == (unsigned char *) NULL)
974 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
976 Convert image pixels to uncompressed SGI pixels.
978 for (y=0; y < (ssize_t) image->rows; y++)
980 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
981 if (p == (const Quantum *) NULL)
983 if (image->depth <= 8)
984 for (x=0; x < (ssize_t) image->columns; x++)
986 register unsigned char
989 q=(unsigned char *) iris_pixels;
990 q+=((iris_info.rows-1)-y)*(4*iris_info.columns)+4*x;
991 *q++=ScaleQuantumToChar(GetPixelRed(image,p));
992 *q++=ScaleQuantumToChar(GetPixelGreen(image,p));
993 *q++=ScaleQuantumToChar(GetPixelBlue(image,p));
994 *q++=ScaleQuantumToChar(GetPixelAlpha(image,p));
995 p+=GetPixelChannels(image);
998 for (x=0; x < (ssize_t) image->columns; x++)
1000 register unsigned short
1003 q=(unsigned short *) iris_pixels;
1004 q+=((iris_info.rows-1)-y)*(4*iris_info.columns)+4*x;
1005 *q++=ScaleQuantumToShort(GetPixelRed(image,p));
1006 *q++=ScaleQuantumToShort(GetPixelGreen(image,p));
1007 *q++=ScaleQuantumToShort(GetPixelBlue(image,p));
1008 *q++=ScaleQuantumToShort(GetPixelAlpha(image,p));
1009 p+=GetPixelChannels(image);
1011 if (image->previous == (Image *) NULL)
1013 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
1015 if (status == MagickFalse)
1019 switch (compression)
1024 Write uncompressed SGI pixels.
1026 for (z=0; z < (ssize_t) iris_info.depth; z++)
1028 for (y=0; y < (ssize_t) iris_info.rows; y++)
1030 if (image->depth <= 8)
1031 for (x=0; x < (ssize_t) iris_info.columns; x++)
1033 register unsigned char
1036 q=(unsigned char *) iris_pixels;
1037 q+=y*(4*iris_info.columns)+4*x+z;
1038 (void) WriteBlobByte(image,*q);
1041 for (x=0; x < (ssize_t) iris_info.columns; x++)
1043 register unsigned short
1046 q=(unsigned short *) iris_pixels;
1047 q+=y*(4*iris_info.columns)+4*x+z;
1048 (void) WriteBlobMSBShort(image,*q);
1066 Convert SGI uncompressed pixels.
1068 offsets=(ssize_t *) AcquireQuantumMemory(iris_info.rows*iris_info.depth,
1070 packets=(unsigned char *) AcquireQuantumMemory((2*(size_t)
1071 iris_info.columns+10)*image->rows,4*sizeof(*packets));
1072 runlength=(size_t *) AcquireQuantumMemory(iris_info.rows,
1073 iris_info.depth*sizeof(*runlength));
1074 if ((offsets == (ssize_t *) NULL) ||
1075 (packets == (unsigned char *) NULL) ||
1076 (runlength == (size_t *) NULL))
1077 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
1078 offset=512+4*2*((ssize_t) iris_info.rows*iris_info.depth);
1081 for (y=0; y < (ssize_t) iris_info.rows; y++)
1083 for (z=0; z < (ssize_t) iris_info.depth; z++)
1085 length=SGIEncode(q+z,(size_t) iris_info.columns,packets+
1087 number_packets+=length;
1088 offsets[y+z*iris_info.rows]=offset;
1089 runlength[y+z*iris_info.rows]=(size_t) length;
1090 offset+=(ssize_t) length;
1092 q+=(iris_info.columns*4);
1095 Write out line start and length tables and runlength-encoded pixels.
1097 for (i=0; i < (ssize_t) (iris_info.rows*iris_info.depth); i++)
1098 (void) WriteBlobMSBLong(image,(unsigned int) offsets[i]);
1099 for (i=0; i < (ssize_t) (iris_info.rows*iris_info.depth); i++)
1100 (void) WriteBlobMSBLong(image,(unsigned int) runlength[i]);
1101 (void) WriteBlob(image,number_packets,packets);
1103 Relinquish resources.
1105 runlength=(size_t *) RelinquishMagickMemory(runlength);
1106 packets=(unsigned char *) RelinquishMagickMemory(packets);
1107 offsets=(ssize_t *) RelinquishMagickMemory(offsets);
1111 iris_pixels=(unsigned char *) RelinquishMagickMemory(iris_pixels);
1112 if (GetNextImageInList(image) == (Image *) NULL)
1114 image=SyncNextImageInList(image);
1115 status=SetImageProgress(image,SaveImagesTag,scene++,
1116 GetImageListLength(image));
1117 if (status == MagickFalse)
1119 } while (image_info->adjoin != MagickFalse);
1120 (void) CloseBlob(image);