2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
13 % Read/Write Irix RGB Image Format %
20 % Copyright 1999-2014 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)
284 register unsigned char
305 assert(image_info != (const ImageInfo *) NULL);
306 assert(image_info->signature == MagickSignature);
307 if (image_info->debug != MagickFalse)
308 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
309 image_info->filename);
310 assert(exception != (ExceptionInfo *) NULL);
311 assert(exception->signature == MagickSignature);
312 image=AcquireImage(image_info,exception);
313 status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception);
314 if (status == MagickFalse)
316 image=DestroyImageList(image);
317 return((Image *) NULL);
320 Read SGI raster header.
322 iris_info.magic=ReadBlobMSBShort(image);
326 Verify SGI identifier.
328 if (iris_info.magic != 0x01DA)
329 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
330 iris_info.storage=(unsigned char) ReadBlobByte(image);
331 switch (iris_info.storage)
333 case 0x00: image->compression=NoCompression; break;
334 case 0x01: image->compression=RLECompression; break;
336 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
338 iris_info.bytes_per_pixel=(unsigned char) ReadBlobByte(image);
339 if ((iris_info.bytes_per_pixel == 0) || (iris_info.bytes_per_pixel > 2))
340 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
341 iris_info.dimension=ReadBlobMSBShort(image);
342 iris_info.columns=ReadBlobMSBShort(image);
343 iris_info.rows=ReadBlobMSBShort(image);
344 iris_info.depth=ReadBlobMSBShort(image);
345 if ((iris_info.depth == 0) || (iris_info.depth > 4))
346 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
347 iris_info.minimum_value=ReadBlobMSBLong(image);
348 iris_info.maximum_value=ReadBlobMSBLong(image);
349 iris_info.sans=ReadBlobMSBLong(image);
350 (void) ReadBlob(image,sizeof(iris_info.name),(unsigned char *)
352 iris_info.name[sizeof(iris_info.name)-1]='\0';
353 if (*iris_info.name != '\0')
354 (void) SetImageProperty(image,"label",iris_info.name,exception);
355 iris_info.pixel_format=ReadBlobMSBLong(image);
356 if (iris_info.pixel_format != 0)
357 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
358 count=ReadBlob(image,sizeof(iris_info.filler),iris_info.filler);
360 image->columns=iris_info.columns;
361 image->rows=iris_info.rows;
362 image->depth=(size_t) MagickMin(iris_info.depth,MAGICKCORE_QUANTUM_DEPTH);
363 if (iris_info.pixel_format == 0)
364 image->depth=(size_t) MagickMin((size_t) 8*
365 iris_info.bytes_per_pixel,MAGICKCORE_QUANTUM_DEPTH);
366 if (iris_info.depth < 3)
368 image->storage_class=PseudoClass;
369 image->colors=iris_info.bytes_per_pixel > 1 ? 65535 : 256;
371 if ((image_info->ping != MagickFalse) && (image_info->number_scenes != 0))
372 if (image->scene >= (image_info->scene+image_info->number_scenes-1))
377 bytes_per_pixel=(size_t) iris_info.bytes_per_pixel;
378 number_pixels=(MagickSizeType) iris_info.columns*iris_info.rows;
379 if ((4*bytes_per_pixel*number_pixels) != ((MagickSizeType) (size_t)
380 (4*bytes_per_pixel*number_pixels)))
381 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
382 pixel_info=AcquireVirtualMemory(iris_info.columns,iris_info.rows*4*
383 bytes_per_pixel*sizeof(*pixels));
384 if (pixel_info == (MemoryInfo *) NULL)
385 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
386 pixels=(unsigned char *) GetVirtualMemoryBlob(pixel_info);
387 if ((int) iris_info.storage != 0x01)
393 Read standard image format.
395 scanline=(unsigned char *) AcquireQuantumMemory(iris_info.columns,
396 bytes_per_pixel*sizeof(*scanline));
397 if (scanline == (unsigned char *) NULL)
398 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
399 for (z=0; z < (ssize_t) iris_info.depth; z++)
401 p=pixels+bytes_per_pixel*z;
402 for (y=0; y < (ssize_t) iris_info.rows; y++)
404 count=ReadBlob(image,bytes_per_pixel*iris_info.columns,scanline);
405 if (EOFBlob(image) != MagickFalse)
407 if (bytes_per_pixel == 2)
408 for (x=0; x < (ssize_t) iris_info.columns; x++)
411 *(p+1)=scanline[2*x+1];
415 for (x=0; x < (ssize_t) iris_info.columns; x++)
422 scanline=(unsigned char *) RelinquishMagickMemory(scanline);
443 Read runlength-encoded image format.
445 offsets=(ssize_t *) AcquireQuantumMemory((size_t) iris_info.rows,
446 iris_info.depth*sizeof(*offsets));
447 runlength=(size_t *) AcquireQuantumMemory(iris_info.rows,
448 iris_info.depth*sizeof(*runlength));
449 packet_info=AcquireVirtualMemory((size_t) iris_info.columns+10UL,4UL*
451 if ((offsets == (ssize_t *) NULL) ||
452 (runlength == (size_t *) NULL) ||
453 (packet_info == (MemoryInfo *) NULL))
455 if (offsets == (ssize_t *) NULL)
456 offsets=(ssize_t *) RelinquishMagickMemory(offsets);
457 if (runlength == (size_t *) NULL)
458 runlength=(size_t *) RelinquishMagickMemory(runlength);
459 if (packet_info == (MemoryInfo *) NULL)
460 packet_info=RelinquishVirtualMemory(packet_info);
461 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
463 packets=(unsigned char *) GetVirtualMemoryBlob(packet_info);
464 for (i=0; i < (ssize_t) (iris_info.rows*iris_info.depth); i++)
465 offsets[i]=(int) ReadBlobMSBLong(image);
466 for (i=0; i < (ssize_t) (iris_info.rows*iris_info.depth); i++)
468 runlength[i]=ReadBlobMSBLong(image);
469 if (runlength[i] > (4*(size_t) iris_info.columns+10))
470 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
477 for (y=0; ((y < (ssize_t) iris_info.rows) && (data_order == 0)); y++)
478 for (z=0; ((z < (ssize_t) iris_info.depth) && (data_order == 0)); z++)
480 if (offsets[y+z*iris_info.rows] < offset)
482 offset=offsets[y+z*iris_info.rows];
484 offset=(ssize_t) TellBlob(image);
487 for (z=0; z < (ssize_t) iris_info.depth; z++)
490 for (y=0; y < (ssize_t) iris_info.rows; y++)
492 if (offset != offsets[y+z*iris_info.rows])
494 offset=offsets[y+z*iris_info.rows];
495 offset=(ssize_t) SeekBlob(image,(ssize_t) offset,SEEK_SET);
497 count=ReadBlob(image,(size_t) runlength[y+z*iris_info.rows],
499 if (EOFBlob(image) != MagickFalse)
501 offset+=(ssize_t) runlength[y+z*iris_info.rows];
502 status=SGIDecode(bytes_per_pixel,(ssize_t)
503 (runlength[y+z*iris_info.rows]/bytes_per_pixel),packets,
504 1L*iris_info.columns,p+bytes_per_pixel*z);
505 if (status == MagickFalse)
506 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
507 p+=(iris_info.columns*4*bytes_per_pixel);
516 position=TellBlob(image);
518 for (y=0; y < (ssize_t) iris_info.rows; y++)
520 for (z=0; z < (ssize_t) iris_info.depth; z++)
522 if (offset != offsets[y+z*iris_info.rows])
524 offset=offsets[y+z*iris_info.rows];
525 offset=(ssize_t) SeekBlob(image,(ssize_t) offset,SEEK_SET);
527 count=ReadBlob(image,(size_t) runlength[y+z*iris_info.rows],
529 if (EOFBlob(image) != MagickFalse)
531 offset+=(ssize_t) runlength[y+z*iris_info.rows];
532 status=SGIDecode(bytes_per_pixel,(ssize_t)
533 (runlength[y+z*iris_info.rows]/bytes_per_pixel),packets,
534 1L*iris_info.columns,p+bytes_per_pixel*z);
535 if (status == MagickFalse)
536 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
538 p+=(iris_info.columns*4*bytes_per_pixel);
540 offset=(ssize_t) SeekBlob(image,position,SEEK_SET);
542 packet_info=RelinquishVirtualMemory(packet_info);
543 runlength=(size_t *) RelinquishMagickMemory(runlength);
544 offsets=(ssize_t *) RelinquishMagickMemory(offsets);
547 Initialize image structure.
549 image->alpha_trait=iris_info.depth == 4 ? BlendPixelTrait :
551 image->columns=iris_info.columns;
552 image->rows=iris_info.rows;
554 Convert SGI raster image to pixel packets.
556 if (image->storage_class == DirectClass)
559 Convert SGI image to DirectClass pixel packets.
561 if (bytes_per_pixel == 2)
563 for (y=0; y < (ssize_t) image->rows; y++)
565 p=pixels+(image->rows-y-1)*8*image->columns;
566 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
567 if (q == (Quantum *) NULL)
569 for (x=0; x < (ssize_t) image->columns; x++)
571 SetPixelRed(image,ScaleShortToQuantum((unsigned short)
572 ((*(p+0) << 8) | (*(p+1)))),q);
573 SetPixelGreen(image,ScaleShortToQuantum((unsigned short)
574 ((*(p+2) << 8) | (*(p+3)))),q);
575 SetPixelBlue(image,ScaleShortToQuantum((unsigned short)
576 ((*(p+4) << 8) | (*(p+5)))),q);
577 SetPixelAlpha(image,OpaqueAlpha,q);
578 if (image->alpha_trait == BlendPixelTrait)
579 SetPixelAlpha(image,ScaleShortToQuantum((unsigned short)
580 ((*(p+6) << 8) | (*(p+7)))),q);
582 q+=GetPixelChannels(image);
584 if (SyncAuthenticPixels(image,exception) == MagickFalse)
586 if (image->previous == (Image *) NULL)
588 status=SetImageProgress(image,LoadImageTag,(MagickOffsetType)
590 if (status == MagickFalse)
596 for (y=0; y < (ssize_t) image->rows; y++)
598 p=pixels+(image->rows-y-1)*4*image->columns;
599 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
600 if (q == (Quantum *) NULL)
602 for (x=0; x < (ssize_t) image->columns; x++)
604 SetPixelRed(image,ScaleCharToQuantum(*p),q);
605 SetPixelGreen(image,ScaleCharToQuantum(*(p+1)),q);
606 SetPixelBlue(image,ScaleCharToQuantum(*(p+2)),q);
607 SetPixelAlpha(image,OpaqueAlpha,q);
608 if (image->alpha_trait == BlendPixelTrait)
609 SetPixelAlpha(image,ScaleCharToQuantum(*(p+3)),q);
611 q+=GetPixelChannels(image);
613 if (SyncAuthenticPixels(image,exception) == MagickFalse)
615 if (image->previous == (Image *) NULL)
617 status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y,
619 if (status == MagickFalse)
627 Create grayscale map.
629 if (AcquireImageColormap(image,image->colors,exception) == MagickFalse)
630 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
632 Convert SGI image to PseudoClass pixel packets.
634 if (bytes_per_pixel == 2)
636 for (y=0; y < (ssize_t) image->rows; y++)
638 p=pixels+(image->rows-y-1)*8*image->columns;
639 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
640 if (q == (Quantum *) NULL)
642 for (x=0; x < (ssize_t) image->columns; x++)
646 SetPixelIndex(image,quantum,q);
648 q+=GetPixelChannels(image);
650 if (SyncAuthenticPixels(image,exception) == MagickFalse)
652 if (image->previous == (Image *) NULL)
654 status=SetImageProgress(image,LoadImageTag,(MagickOffsetType)
656 if (status == MagickFalse)
662 for (y=0; y < (ssize_t) image->rows; y++)
664 p=pixels+(image->rows-y-1)*4*image->columns;
665 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
666 if (q == (Quantum *) NULL)
668 for (x=0; x < (ssize_t) image->columns; x++)
670 SetPixelIndex(image,*p,q);
672 q+=GetPixelChannels(image);
674 if (SyncAuthenticPixels(image,exception) == MagickFalse)
676 if (image->previous == (Image *) NULL)
678 status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y,
680 if (status == MagickFalse)
684 (void) SyncImage(image,exception);
686 pixel_info=RelinquishVirtualMemory(pixel_info);
687 if (EOFBlob(image) != MagickFalse)
689 ThrowFileException(exception,CorruptImageError,"UnexpectedEndOfFile",
694 Proceed to next image.
696 if (image_info->number_scenes != 0)
697 if (image->scene >= (image_info->scene+image_info->number_scenes-1))
699 iris_info.magic=ReadBlobMSBShort(image);
700 if (iris_info.magic == 0x01DA)
703 Allocate next image structure.
705 AcquireNextImage(image_info,image,exception);
706 if (GetNextImageInList(image) == (Image *) NULL)
708 image=DestroyImageList(image);
709 return((Image *) NULL);
711 image=SyncNextImageInList(image);
712 status=SetImageProgress(image,LoadImagesTag,TellBlob(image),
714 if (status == MagickFalse)
717 } while (iris_info.magic == 0x01DA);
718 (void) CloseBlob(image);
719 return(GetFirstImageInList(image));
723 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
727 % R e g i s t e r S G I I m a g e %
731 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
733 % RegisterSGIImage() adds properties for the SGI image format to
734 % the list of supported formats. The properties include the image format
735 % tag, a method to read and/or write the format, whether the format
736 % supports the saving of more than one frame to the same file or blob,
737 % whether the format supports native in-memory I/O, and a brief
738 % description of the format.
740 % The format of the RegisterSGIImage method is:
742 % size_t RegisterSGIImage(void)
745 ModuleExport size_t RegisterSGIImage(void)
750 entry=SetMagickInfo("SGI");
751 entry->decoder=(DecodeImageHandler *) ReadSGIImage;
752 entry->encoder=(EncodeImageHandler *) WriteSGIImage;
753 entry->magick=(IsImageFormatHandler *) IsSGI;
754 entry->description=ConstantString("Irix RGB image");
755 entry->module=ConstantString("SGI");
756 entry->seekable_stream=MagickTrue;
757 (void) RegisterMagickInfo(entry);
758 return(MagickImageCoderSignature);
762 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
766 % U n r e g i s t e r S G I I m a g e %
770 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
772 % UnregisterSGIImage() removes format registrations made by the
773 % SGI module from the list of supported formats.
775 % The format of the UnregisterSGIImage method is:
777 % UnregisterSGIImage(void)
780 ModuleExport void UnregisterSGIImage(void)
782 (void) UnregisterMagickInfo("SGI");
786 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
790 % W r i t e S G I I m a g e %
794 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
796 % WriteSGIImage() writes an image in SGI RGB encoded image format.
798 % The format of the WriteSGIImage method is:
800 % MagickBooleanType WriteSGIImage(const ImageInfo *image_info,
801 % Image *image,ExceptionInfo *exception)
803 % A description of each parameter follows.
805 % o image_info: the image info.
807 % o image: The image.
809 % o exception: return any errors or warnings in this structure.
813 static size_t SGIEncode(unsigned char *pixels,size_t length,
814 unsigned char *packets)
819 register unsigned char
834 while ((p < limit) && ((*(p-8) != *(p-4)) || (*(p-4) != *p)))
837 length=(size_t) (p-mark) >> 2;
840 runlength=(short) (length > 126 ? 126 : length);
842 *q++=(unsigned char) (0x80 | runlength);
843 for ( ; runlength > 0; runlength--)
851 while ((p < limit) && (*p == *mark))
853 length=(size_t) (p-mark) >> 2;
856 runlength=(short) (length > 126 ? 126 : length);
858 *q++=(unsigned char) runlength;
863 return((size_t) (q-packets));
866 static MagickBooleanType WriteSGIImage(const ImageInfo *image_info,Image *image,
867 ExceptionInfo *exception)
890 register const Quantum
897 register unsigned char
909 Open output image file.
911 assert(image_info != (const ImageInfo *) NULL);
912 assert(image_info->signature == MagickSignature);
913 assert(image != (Image *) NULL);
914 assert(image->signature == MagickSignature);
915 if (image->debug != MagickFalse)
916 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
917 if ((image->columns > 65535UL) || (image->rows > 65535UL))
918 ThrowWriterException(ImageError,"WidthOrHeightExceedsLimit");
919 assert(exception != (ExceptionInfo *) NULL);
920 assert(exception->signature == MagickSignature);
921 status=OpenBlob(image_info,image,WriteBinaryBlobMode,exception);
922 if (status == MagickFalse)
928 Initialize SGI raster file header.
930 if (IssRGBCompatibleColorspace(image->colorspace) == MagickFalse)
931 (void) TransformImageColorspace(image,sRGBColorspace,exception);
932 (void) ResetMagickMemory(&iris_info,0,sizeof(iris_info));
933 iris_info.magic=0x01DA;
934 compression=image->compression;
935 if (image_info->compression != UndefinedCompression)
936 compression=image_info->compression;
937 if (image->depth > 8)
938 compression=NoCompression;
939 if (compression == NoCompression)
940 iris_info.storage=(unsigned char) 0x00;
942 iris_info.storage=(unsigned char) 0x01;
943 iris_info.bytes_per_pixel=(unsigned char) (image->depth > 8 ? 2 : 1);
944 iris_info.dimension=3;
945 iris_info.columns=(unsigned short) image->columns;
946 iris_info.rows=(unsigned short) image->rows;
947 if (image->alpha_trait == BlendPixelTrait)
951 if ((image_info->type != TrueColorType) &&
952 (IsImageGray(image,exception) != MagickFalse))
954 iris_info.dimension=2;
960 iris_info.minimum_value=0;
961 iris_info.maximum_value=(size_t) (image->depth <= 8 ?
962 1UL*ScaleQuantumToChar(QuantumRange) :
963 1UL*ScaleQuantumToShort(QuantumRange));
967 (void) WriteBlobMSBShort(image,iris_info.magic);
968 (void) WriteBlobByte(image,iris_info.storage);
969 (void) WriteBlobByte(image,iris_info.bytes_per_pixel);
970 (void) WriteBlobMSBShort(image,iris_info.dimension);
971 (void) WriteBlobMSBShort(image,iris_info.columns);
972 (void) WriteBlobMSBShort(image,iris_info.rows);
973 (void) WriteBlobMSBShort(image,iris_info.depth);
974 (void) WriteBlobMSBLong(image,(unsigned int) iris_info.minimum_value);
975 (void) WriteBlobMSBLong(image,(unsigned int) iris_info.maximum_value);
976 (void) WriteBlobMSBLong(image,(unsigned int) iris_info.sans);
977 value=GetImageProperty(image,"label",exception);
978 if (value != (const char *) NULL)
979 (void) CopyMagickString(iris_info.name,value,sizeof(iris_info.name));
980 (void) WriteBlob(image,sizeof(iris_info.name),(unsigned char *)
982 (void) WriteBlobMSBLong(image,(unsigned int) iris_info.pixel_format);
983 (void) WriteBlob(image,sizeof(iris_info.filler),iris_info.filler);
987 number_pixels=(MagickSizeType) image->columns*image->rows;
988 if ((4*iris_info.bytes_per_pixel*number_pixels) !=
989 ((MagickSizeType) (size_t) (4*iris_info.bytes_per_pixel*number_pixels)))
990 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
991 pixel_info=AcquireVirtualMemory((size_t) number_pixels,4*
992 iris_info.bytes_per_pixel*sizeof(*pixels));
993 if (pixel_info == (MemoryInfo *) NULL)
994 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
995 pixels=(unsigned char *) GetVirtualMemoryBlob(pixel_info);
997 Convert image pixels to uncompressed SGI pixels.
999 for (y=0; y < (ssize_t) image->rows; y++)
1001 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
1002 if (p == (const Quantum *) NULL)
1004 if (image->depth <= 8)
1005 for (x=0; x < (ssize_t) image->columns; x++)
1007 register unsigned char
1010 q=(unsigned char *) pixels;
1011 q+=((iris_info.rows-1)-y)*(4*iris_info.columns)+4*x;
1012 *q++=ScaleQuantumToChar(GetPixelRed(image,p));
1013 *q++=ScaleQuantumToChar(GetPixelGreen(image,p));
1014 *q++=ScaleQuantumToChar(GetPixelBlue(image,p));
1015 *q++=ScaleQuantumToChar(GetPixelAlpha(image,p));
1016 p+=GetPixelChannels(image);
1019 for (x=0; x < (ssize_t) image->columns; x++)
1021 register unsigned short
1024 q=(unsigned short *) pixels;
1025 q+=((iris_info.rows-1)-y)*(4*iris_info.columns)+4*x;
1026 *q++=ScaleQuantumToShort(GetPixelRed(image,p));
1027 *q++=ScaleQuantumToShort(GetPixelGreen(image,p));
1028 *q++=ScaleQuantumToShort(GetPixelBlue(image,p));
1029 *q++=ScaleQuantumToShort(GetPixelAlpha(image,p));
1030 p+=GetPixelChannels(image);
1032 if (image->previous == (Image *) NULL)
1034 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
1036 if (status == MagickFalse)
1040 switch (compression)
1045 Write uncompressed SGI pixels.
1047 for (z=0; z < (ssize_t) iris_info.depth; z++)
1049 for (y=0; y < (ssize_t) iris_info.rows; y++)
1051 if (image->depth <= 8)
1052 for (x=0; x < (ssize_t) iris_info.columns; x++)
1054 register unsigned char
1057 q=(unsigned char *) pixels;
1058 q+=y*(4*iris_info.columns)+4*x+z;
1059 (void) WriteBlobByte(image,*q);
1062 for (x=0; x < (ssize_t) iris_info.columns; x++)
1064 register unsigned short
1067 q=(unsigned short *) pixels;
1068 q+=y*(4*iris_info.columns)+4*x+z;
1069 (void) WriteBlobMSBShort(image,*q);
1090 Convert SGI uncompressed pixels.
1092 offsets=(ssize_t *) AcquireQuantumMemory(iris_info.rows*iris_info.depth,
1094 runlength=(size_t *) AcquireQuantumMemory(iris_info.rows,
1095 iris_info.depth*sizeof(*runlength));
1096 packet_info=AcquireVirtualMemory((2*(size_t) iris_info.columns+10)*
1097 image->rows,4*sizeof(*packets));
1098 if ((offsets == (ssize_t *) NULL) ||
1099 (runlength == (size_t *) NULL) ||
1100 (packet_info == (MemoryInfo *) NULL))
1102 if (offsets != (ssize_t *) NULL)
1103 offsets=(ssize_t *) RelinquishMagickMemory(offsets);
1104 if (runlength != (size_t *) NULL)
1105 runlength=(size_t *) RelinquishMagickMemory(runlength);
1106 if (packet_info != (MemoryInfo *) NULL)
1107 packet_info=RelinquishVirtualMemory(packet_info);
1108 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
1110 packets=(unsigned char *) GetVirtualMemoryBlob(packet_info);
1111 offset=512+4*2*((ssize_t) iris_info.rows*iris_info.depth);
1114 for (y=0; y < (ssize_t) iris_info.rows; y++)
1116 for (z=0; z < (ssize_t) iris_info.depth; z++)
1118 length=SGIEncode(q+z,(size_t) iris_info.columns,packets+
1120 number_packets+=length;
1121 offsets[y+z*iris_info.rows]=offset;
1122 runlength[y+z*iris_info.rows]=(size_t) length;
1123 offset+=(ssize_t) length;
1125 q+=(iris_info.columns*4);
1128 Write out line start and length tables and runlength-encoded pixels.
1130 for (i=0; i < (ssize_t) (iris_info.rows*iris_info.depth); i++)
1131 (void) WriteBlobMSBLong(image,(unsigned int) offsets[i]);
1132 for (i=0; i < (ssize_t) (iris_info.rows*iris_info.depth); i++)
1133 (void) WriteBlobMSBLong(image,(unsigned int) runlength[i]);
1134 (void) WriteBlob(image,number_packets,packets);
1136 Relinquish resources.
1138 offsets=(ssize_t *) RelinquishMagickMemory(offsets);
1139 runlength=(size_t *) RelinquishMagickMemory(runlength);
1140 packet_info=RelinquishVirtualMemory(packet_info);
1144 pixel_info=RelinquishVirtualMemory(pixel_info);
1145 if (GetNextImageInList(image) == (Image *) NULL)
1147 image=SyncNextImageInList(image);
1148 status=SetImageProgress(image,SaveImagesTag,scene++,
1149 GetImageListLength(image));
1150 if (status == MagickFalse)
1152 } while (image_info->adjoin != MagickFalse);
1153 (void) CloseBlob(image);