2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
13 % Read/Write Irix RGB Image Format %
20 % Copyright 1999-2013 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->alpha_trait=iris_info.depth == 4 ? BlendPixelTrait :
535 image->columns=iris_info.columns;
536 image->rows=iris_info.rows;
538 Convert SGI raster image to pixel packets.
540 if (image->storage_class == DirectClass)
543 Convert SGI image to DirectClass pixel packets.
545 if (bytes_per_pixel == 2)
547 for (y=0; y < (ssize_t) image->rows; y++)
549 p=iris_pixels+(image->rows-y-1)*8*image->columns;
550 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
551 if (q == (Quantum *) NULL)
553 for (x=0; x < (ssize_t) image->columns; x++)
555 SetPixelRed(image,ScaleShortToQuantum((unsigned short)
556 ((*(p+0) << 8) | (*(p+1)))),q);
557 SetPixelGreen(image,ScaleShortToQuantum((unsigned short)
558 ((*(p+2) << 8) | (*(p+3)))),q);
559 SetPixelBlue(image,ScaleShortToQuantum((unsigned short)
560 ((*(p+4) << 8) | (*(p+5)))),q);
561 SetPixelAlpha(image,OpaqueAlpha,q);
562 if (image->alpha_trait == BlendPixelTrait)
563 SetPixelAlpha(image,ScaleShortToQuantum((unsigned short)
564 ((*(p+6) << 8) | (*(p+7)))),q);
566 q+=GetPixelChannels(image);
568 if (SyncAuthenticPixels(image,exception) == MagickFalse)
570 if (image->previous == (Image *) NULL)
572 status=SetImageProgress(image,LoadImageTag,(MagickOffsetType)
574 if (status == MagickFalse)
580 for (y=0; y < (ssize_t) image->rows; y++)
582 p=iris_pixels+(image->rows-y-1)*4*image->columns;
583 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
584 if (q == (Quantum *) NULL)
586 for (x=0; x < (ssize_t) image->columns; x++)
588 SetPixelRed(image,ScaleCharToQuantum(*p),q);
589 SetPixelGreen(image,ScaleCharToQuantum(*(p+1)),q);
590 SetPixelBlue(image,ScaleCharToQuantum(*(p+2)),q);
591 SetPixelAlpha(image,OpaqueAlpha,q);
592 if (image->alpha_trait == BlendPixelTrait)
593 SetPixelAlpha(image,ScaleCharToQuantum(*(p+3)),q);
595 q+=GetPixelChannels(image);
597 if (SyncAuthenticPixels(image,exception) == MagickFalse)
599 if (image->previous == (Image *) NULL)
601 status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y,
603 if (status == MagickFalse)
611 Create grayscale map.
613 if (AcquireImageColormap(image,image->colors,exception) == MagickFalse)
614 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
616 Convert SGI image to PseudoClass pixel packets.
618 if (bytes_per_pixel == 2)
620 for (y=0; y < (ssize_t) image->rows; y++)
622 p=iris_pixels+(image->rows-y-1)*8*image->columns;
623 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
624 if (q == (Quantum *) NULL)
626 for (x=0; x < (ssize_t) image->columns; x++)
630 SetPixelIndex(image,quantum,q);
632 q+=GetPixelChannels(image);
634 if (SyncAuthenticPixels(image,exception) == MagickFalse)
636 if (image->previous == (Image *) NULL)
638 status=SetImageProgress(image,LoadImageTag,(MagickOffsetType)
640 if (status == MagickFalse)
646 for (y=0; y < (ssize_t) image->rows; y++)
648 p=iris_pixels+(image->rows-y-1)*4*image->columns;
649 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
650 if (q == (Quantum *) NULL)
652 for (x=0; x < (ssize_t) image->columns; x++)
654 SetPixelIndex(image,*p,q);
656 q+=GetPixelChannels(image);
658 if (SyncAuthenticPixels(image,exception) == MagickFalse)
660 if (image->previous == (Image *) NULL)
662 status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y,
664 if (status == MagickFalse)
668 (void) SyncImage(image,exception);
670 iris_pixels=(unsigned char *) RelinquishMagickMemory(iris_pixels);
671 if (EOFBlob(image) != MagickFalse)
673 ThrowFileException(exception,CorruptImageError,"UnexpectedEndOfFile",
678 Proceed to next image.
680 if (image_info->number_scenes != 0)
681 if (image->scene >= (image_info->scene+image_info->number_scenes-1))
683 iris_info.magic=ReadBlobMSBShort(image);
684 if (iris_info.magic == 0x01DA)
687 Allocate next image structure.
689 AcquireNextImage(image_info,image,exception);
690 if (GetNextImageInList(image) == (Image *) NULL)
692 image=DestroyImageList(image);
693 return((Image *) NULL);
695 image=SyncNextImageInList(image);
696 status=SetImageProgress(image,LoadImagesTag,TellBlob(image),
698 if (status == MagickFalse)
701 } while (iris_info.magic == 0x01DA);
702 (void) CloseBlob(image);
703 return(GetFirstImageInList(image));
707 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
711 % R e g i s t e r S G I I m a g e %
715 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
717 % RegisterSGIImage() adds properties for the SGI image format to
718 % the list of supported formats. The properties include the image format
719 % tag, a method to read and/or write the format, whether the format
720 % supports the saving of more than one frame to the same file or blob,
721 % whether the format supports native in-memory I/O, and a brief
722 % description of the format.
724 % The format of the RegisterSGIImage method is:
726 % size_t RegisterSGIImage(void)
729 ModuleExport size_t RegisterSGIImage(void)
734 entry=SetMagickInfo("SGI");
735 entry->decoder=(DecodeImageHandler *) ReadSGIImage;
736 entry->encoder=(EncodeImageHandler *) WriteSGIImage;
737 entry->magick=(IsImageFormatHandler *) IsSGI;
738 entry->description=ConstantString("Irix RGB image");
739 entry->module=ConstantString("SGI");
740 entry->seekable_stream=MagickTrue;
741 (void) RegisterMagickInfo(entry);
742 return(MagickImageCoderSignature);
746 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
750 % U n r e g i s t e r S G I I m a g e %
754 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
756 % UnregisterSGIImage() removes format registrations made by the
757 % SGI module from the list of supported formats.
759 % The format of the UnregisterSGIImage method is:
761 % UnregisterSGIImage(void)
764 ModuleExport void UnregisterSGIImage(void)
766 (void) UnregisterMagickInfo("SGI");
770 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
774 % W r i t e S G I I m a g e %
778 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
780 % WriteSGIImage() writes an image in SGI RGB encoded image format.
782 % The format of the WriteSGIImage method is:
784 % MagickBooleanType WriteSGIImage(const ImageInfo *image_info,
785 % Image *image,ExceptionInfo *exception)
787 % A description of each parameter follows.
789 % o image_info: the image info.
791 % o image: The image.
793 % o exception: return any errors or warnings in this structure.
797 static size_t SGIEncode(unsigned char *pixels,size_t length,
798 unsigned char *packets)
803 register unsigned char
818 while ((p < limit) && ((*(p-8) != *(p-4)) || (*(p-4) != *p)))
821 length=(size_t) (p-mark) >> 2;
824 runlength=(short) (length > 126 ? 126 : length);
826 *q++=(unsigned char) (0x80 | runlength);
827 for ( ; runlength > 0; runlength--)
835 while ((p < limit) && (*p == *mark))
837 length=(size_t) (p-mark) >> 2;
840 runlength=(short) (length > 126 ? 126 : length);
842 *q++=(unsigned char) runlength;
847 return((size_t) (q-packets));
850 static MagickBooleanType WriteSGIImage(const ImageInfo *image_info,Image *image,
851 ExceptionInfo *exception)
871 register const Quantum
878 register unsigned char
890 Open output image file.
892 assert(image_info != (const ImageInfo *) NULL);
893 assert(image_info->signature == MagickSignature);
894 assert(image != (Image *) NULL);
895 assert(image->signature == MagickSignature);
896 if (image->debug != MagickFalse)
897 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
898 if ((image->columns > 65535UL) || (image->rows > 65535UL))
899 ThrowWriterException(ImageError,"WidthOrHeightExceedsLimit");
900 assert(exception != (ExceptionInfo *) NULL);
901 assert(exception->signature == MagickSignature);
902 status=OpenBlob(image_info,image,WriteBinaryBlobMode,exception);
903 if (status == MagickFalse)
909 Initialize SGI raster file header.
911 if (IssRGBCompatibleColorspace(image->colorspace) == MagickFalse)
912 (void) TransformImageColorspace(image,sRGBColorspace,exception);
913 (void) ResetMagickMemory(&iris_info,0,sizeof(iris_info));
914 iris_info.magic=0x01DA;
915 compression=image->compression;
916 if (image_info->compression != UndefinedCompression)
917 compression=image_info->compression;
918 if (image->depth > 8)
919 compression=NoCompression;
920 if (compression == NoCompression)
921 iris_info.storage=(unsigned char) 0x00;
923 iris_info.storage=(unsigned char) 0x01;
924 iris_info.bytes_per_pixel=(unsigned char) (image->depth > 8 ? 2 : 1);
925 iris_info.dimension=3;
926 iris_info.columns=(unsigned short) image->columns;
927 iris_info.rows=(unsigned short) image->rows;
928 if (image->alpha_trait == BlendPixelTrait)
932 if ((image_info->type != TrueColorType) &&
933 (IsImageGray(image,exception) != MagickFalse))
935 iris_info.dimension=2;
941 iris_info.minimum_value=0;
942 iris_info.maximum_value=(size_t) (image->depth <= 8 ?
943 1UL*ScaleQuantumToChar(QuantumRange) :
944 1UL*ScaleQuantumToShort(QuantumRange));
948 (void) WriteBlobMSBShort(image,iris_info.magic);
949 (void) WriteBlobByte(image,iris_info.storage);
950 (void) WriteBlobByte(image,iris_info.bytes_per_pixel);
951 (void) WriteBlobMSBShort(image,iris_info.dimension);
952 (void) WriteBlobMSBShort(image,iris_info.columns);
953 (void) WriteBlobMSBShort(image,iris_info.rows);
954 (void) WriteBlobMSBShort(image,iris_info.depth);
955 (void) WriteBlobMSBLong(image,(unsigned int) iris_info.minimum_value);
956 (void) WriteBlobMSBLong(image,(unsigned int) iris_info.maximum_value);
957 (void) WriteBlobMSBLong(image,(unsigned int) iris_info.sans);
958 value=GetImageProperty(image,"label",exception);
959 if (value != (const char *) NULL)
960 (void) CopyMagickString(iris_info.name,value,sizeof(iris_info.name));
961 (void) WriteBlob(image,sizeof(iris_info.name),(unsigned char *)
963 (void) WriteBlobMSBLong(image,(unsigned int) iris_info.pixel_format);
964 (void) WriteBlob(image,sizeof(iris_info.filler),iris_info.filler);
968 number_pixels=(MagickSizeType) image->columns*image->rows;
969 if ((4*iris_info.bytes_per_pixel*number_pixels) !=
970 ((MagickSizeType) (size_t) (4*iris_info.bytes_per_pixel*number_pixels)))
971 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
972 iris_pixels=(unsigned char *) AcquireQuantumMemory((size_t) number_pixels,
973 4*iris_info.bytes_per_pixel*sizeof(*iris_pixels));
974 if (iris_pixels == (unsigned char *) NULL)
975 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
977 Convert image pixels to uncompressed SGI pixels.
979 for (y=0; y < (ssize_t) image->rows; y++)
981 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
982 if (p == (const Quantum *) NULL)
984 if (image->depth <= 8)
985 for (x=0; x < (ssize_t) image->columns; x++)
987 register unsigned char
990 q=(unsigned char *) iris_pixels;
991 q+=((iris_info.rows-1)-y)*(4*iris_info.columns)+4*x;
992 *q++=ScaleQuantumToChar(GetPixelRed(image,p));
993 *q++=ScaleQuantumToChar(GetPixelGreen(image,p));
994 *q++=ScaleQuantumToChar(GetPixelBlue(image,p));
995 *q++=ScaleQuantumToChar(GetPixelAlpha(image,p));
996 p+=GetPixelChannels(image);
999 for (x=0; x < (ssize_t) image->columns; x++)
1001 register unsigned short
1004 q=(unsigned short *) iris_pixels;
1005 q+=((iris_info.rows-1)-y)*(4*iris_info.columns)+4*x;
1006 *q++=ScaleQuantumToShort(GetPixelRed(image,p));
1007 *q++=ScaleQuantumToShort(GetPixelGreen(image,p));
1008 *q++=ScaleQuantumToShort(GetPixelBlue(image,p));
1009 *q++=ScaleQuantumToShort(GetPixelAlpha(image,p));
1010 p+=GetPixelChannels(image);
1012 if (image->previous == (Image *) NULL)
1014 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
1016 if (status == MagickFalse)
1020 switch (compression)
1025 Write uncompressed SGI pixels.
1027 for (z=0; z < (ssize_t) iris_info.depth; z++)
1029 for (y=0; y < (ssize_t) iris_info.rows; y++)
1031 if (image->depth <= 8)
1032 for (x=0; x < (ssize_t) iris_info.columns; x++)
1034 register unsigned char
1037 q=(unsigned char *) iris_pixels;
1038 q+=y*(4*iris_info.columns)+4*x+z;
1039 (void) WriteBlobByte(image,*q);
1042 for (x=0; x < (ssize_t) iris_info.columns; x++)
1044 register unsigned short
1047 q=(unsigned short *) iris_pixels;
1048 q+=y*(4*iris_info.columns)+4*x+z;
1049 (void) WriteBlobMSBShort(image,*q);
1067 Convert SGI uncompressed pixels.
1069 offsets=(ssize_t *) AcquireQuantumMemory(iris_info.rows*iris_info.depth,
1071 packets=(unsigned char *) AcquireQuantumMemory((2*(size_t)
1072 iris_info.columns+10)*image->rows,4*sizeof(*packets));
1073 runlength=(size_t *) AcquireQuantumMemory(iris_info.rows,
1074 iris_info.depth*sizeof(*runlength));
1075 if ((offsets == (ssize_t *) NULL) ||
1076 (packets == (unsigned char *) NULL) ||
1077 (runlength == (size_t *) NULL))
1078 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
1079 offset=512+4*2*((ssize_t) iris_info.rows*iris_info.depth);
1082 for (y=0; y < (ssize_t) iris_info.rows; y++)
1084 for (z=0; z < (ssize_t) iris_info.depth; z++)
1086 length=SGIEncode(q+z,(size_t) iris_info.columns,packets+
1088 number_packets+=length;
1089 offsets[y+z*iris_info.rows]=offset;
1090 runlength[y+z*iris_info.rows]=(size_t) length;
1091 offset+=(ssize_t) length;
1093 q+=(iris_info.columns*4);
1096 Write out line start and length tables and runlength-encoded pixels.
1098 for (i=0; i < (ssize_t) (iris_info.rows*iris_info.depth); i++)
1099 (void) WriteBlobMSBLong(image,(unsigned int) offsets[i]);
1100 for (i=0; i < (ssize_t) (iris_info.rows*iris_info.depth); i++)
1101 (void) WriteBlobMSBLong(image,(unsigned int) runlength[i]);
1102 (void) WriteBlob(image,number_packets,packets);
1104 Relinquish resources.
1106 runlength=(size_t *) RelinquishMagickMemory(runlength);
1107 packets=(unsigned char *) RelinquishMagickMemory(packets);
1108 offsets=(ssize_t *) RelinquishMagickMemory(offsets);
1112 iris_pixels=(unsigned char *) RelinquishMagickMemory(iris_pixels);
1113 if (GetNextImageInList(image) == (Image *) NULL)
1115 image=SyncNextImageInList(image);
1116 status=SetImageProgress(image,SaveImagesTag,scene++,
1117 GetImageListLength(image));
1118 if (status == MagickFalse)
1120 } while (image_info->adjoin != MagickFalse);
1121 (void) CloseBlob(image);