2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
13 % Read/Write Irix RGB Image Format %
20 % Copyright 1999-2017 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 % https://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 MagickBooleanType SGIDecode(const size_t bytes_per_pixel,
168 ssize_t number_packets,unsigned char *packets,ssize_t number_pixels,
169 unsigned char *pixels)
171 register unsigned char
183 if (bytes_per_pixel == 2)
185 for ( ; number_pixels > 0; )
187 if (number_packets-- == 0)
189 pixel=(size_t) (*p++) << 8;
191 count=(ssize_t) (pixel & 0x7f);
194 if (count > (ssize_t) number_pixels)
196 number_pixels-=count;
197 if ((pixel & 0x80) != 0)
198 for ( ; count != 0; count--)
200 if (number_packets-- == 0)
208 if (number_packets-- == 0)
210 pixel=(size_t) (*p++) << 8;
212 for ( ; count != 0; count--)
214 *q=(unsigned char) (pixel >> 8);
215 *(q+1)=(unsigned char) pixel;
222 for ( ; number_pixels > 0; )
224 if (number_packets-- == 0)
226 pixel=(size_t) (*p++);
227 count=(ssize_t) (pixel & 0x7f);
230 if (count > (ssize_t) number_pixels)
232 number_pixels-=count;
233 if ((pixel & 0x80) != 0)
234 for ( ; count != 0; count--)
236 if (number_packets-- == 0)
243 if (number_packets-- == 0)
245 pixel=(size_t) (*p++);
246 for ( ; count != 0; count--)
248 *q=(unsigned char) pixel;
256 static Image *ReadSGIImage(const ImageInfo *image_info,ExceptionInfo *exception)
277 register unsigned char
298 assert(image_info != (const ImageInfo *) NULL);
299 assert(image_info->signature == MagickCoreSignature);
300 if (image_info->debug != MagickFalse)
301 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
302 image_info->filename);
303 assert(exception != (ExceptionInfo *) NULL);
304 assert(exception->signature == MagickCoreSignature);
305 image=AcquireImage(image_info,exception);
306 status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception);
307 if (status == MagickFalse)
309 image=DestroyImageList(image);
310 return((Image *) NULL);
313 Read SGI raster header.
315 iris_info.magic=ReadBlobMSBShort(image);
319 Verify SGI identifier.
321 if (iris_info.magic != 0x01DA)
322 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
323 iris_info.storage=(unsigned char) ReadBlobByte(image);
324 switch (iris_info.storage)
326 case 0x00: image->compression=NoCompression; break;
327 case 0x01: image->compression=RLECompression; break;
329 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
331 iris_info.bytes_per_pixel=(unsigned char) ReadBlobByte(image);
332 if ((iris_info.bytes_per_pixel == 0) || (iris_info.bytes_per_pixel > 2))
333 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
334 iris_info.dimension=ReadBlobMSBShort(image);
335 if ((iris_info.dimension == 0) || (iris_info.dimension > 3))
336 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
337 iris_info.columns=ReadBlobMSBShort(image);
338 iris_info.rows=ReadBlobMSBShort(image);
339 iris_info.depth=ReadBlobMSBShort(image);
340 if ((iris_info.depth == 0) || (iris_info.depth > 4))
341 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
342 iris_info.minimum_value=ReadBlobMSBLong(image);
343 iris_info.maximum_value=ReadBlobMSBLong(image);
344 iris_info.sans=ReadBlobMSBLong(image);
345 count=ReadBlob(image,sizeof(iris_info.name),(unsigned char *)
347 if ((size_t) count != sizeof(iris_info.name))
348 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
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);
356 if ((size_t) count != sizeof(iris_info.filler))
357 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
358 image->columns=iris_info.columns;
359 image->rows=iris_info.rows;
360 image->alpha_trait=iris_info.depth == 4 ? BlendPixelTrait :
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*iris_info.bytes_per_pixel,
365 MAGICKCORE_QUANTUM_DEPTH);
366 if (iris_info.depth < 3)
368 image->storage_class=PseudoClass;
369 image->colors=(size_t) (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))
374 status=SetImageExtent(image,image->columns,image->rows,exception);
375 if (status == MagickFalse)
376 return(DestroyImageList(image));
380 bytes_per_pixel=(size_t) iris_info.bytes_per_pixel;
381 number_pixels=(MagickSizeType) iris_info.columns*iris_info.rows;
382 if ((4*bytes_per_pixel*number_pixels) != ((MagickSizeType) (size_t)
383 (4*bytes_per_pixel*number_pixels)))
384 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
385 pixel_info=AcquireVirtualMemory(iris_info.columns,iris_info.rows*4*
386 bytes_per_pixel*sizeof(*pixels));
387 if (pixel_info == (MemoryInfo *) NULL)
388 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
389 pixels=(unsigned char *) GetVirtualMemoryBlob(pixel_info);
390 if ((int) iris_info.storage != 0x01)
396 Read standard image format.
398 scanline=(unsigned char *) AcquireQuantumMemory(iris_info.columns,
399 bytes_per_pixel*sizeof(*scanline));
400 if (scanline == (unsigned char *) NULL)
402 pixel_info=RelinquishVirtualMemory(pixel_info);
403 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
405 for (z=0; z < (ssize_t) iris_info.depth; z++)
407 p=pixels+bytes_per_pixel*z;
408 for (y=0; y < (ssize_t) iris_info.rows; y++)
410 count=ReadBlob(image,bytes_per_pixel*iris_info.columns,scanline);
411 if (EOFBlob(image) != MagickFalse)
413 if (bytes_per_pixel == 2)
414 for (x=0; x < (ssize_t) iris_info.columns; x++)
417 *(p+1)=scanline[2*x+1];
421 for (x=0; x < (ssize_t) iris_info.columns; x++)
428 scanline=(unsigned char *) RelinquishMagickMemory(scanline);
449 Read runlength-encoded image format.
451 offsets=(ssize_t *) AcquireQuantumMemory((size_t) iris_info.rows,
452 iris_info.depth*sizeof(*offsets));
453 runlength=(size_t *) AcquireQuantumMemory(iris_info.rows,
454 iris_info.depth*sizeof(*runlength));
455 packet_info=AcquireVirtualMemory((size_t) iris_info.columns+10UL,4UL*
457 if ((offsets == (ssize_t *) NULL) ||
458 (runlength == (size_t *) NULL) ||
459 (packet_info == (MemoryInfo *) NULL))
461 offsets=(ssize_t *) RelinquishMagickMemory(offsets);
462 runlength=(size_t *) RelinquishMagickMemory(runlength);
463 if (packet_info != (MemoryInfo *) NULL)
464 packet_info=RelinquishVirtualMemory(packet_info);
465 pixel_info=RelinquishVirtualMemory(pixel_info);
466 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
468 packets=(unsigned char *) GetVirtualMemoryBlob(packet_info);
469 for (i=0; i < (ssize_t) (iris_info.rows*iris_info.depth); i++)
470 offsets[i]=(ssize_t) ReadBlobMSBSignedLong(image);
471 for (i=0; i < (ssize_t) (iris_info.rows*iris_info.depth); i++)
473 runlength[i]=ReadBlobMSBLong(image);
474 if (runlength[i] > (4*(size_t) iris_info.columns+10))
476 packet_info=RelinquishVirtualMemory(packet_info);
477 runlength=(size_t *) RelinquishMagickMemory(runlength);
478 offsets=(ssize_t *) RelinquishMagickMemory(offsets);
479 pixel_info=RelinquishVirtualMemory(pixel_info);
480 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
488 for (y=0; ((y < (ssize_t) iris_info.rows) && (data_order == 0)); y++)
489 for (z=0; ((z < (ssize_t) iris_info.depth) && (data_order == 0)); z++)
491 if (offsets[y+z*iris_info.rows] < offset)
493 offset=offsets[y+z*iris_info.rows];
495 offset=(ssize_t) TellBlob(image);
498 for (z=0; z < (ssize_t) iris_info.depth; z++)
501 for (y=0; y < (ssize_t) iris_info.rows; y++)
503 if (offset != offsets[y+z*iris_info.rows])
505 offset=offsets[y+z*iris_info.rows];
506 offset=(ssize_t) SeekBlob(image,(MagickOffsetType) offset,
509 count=ReadBlob(image,(size_t) runlength[y+z*iris_info.rows],
511 if (EOFBlob(image) != MagickFalse)
513 offset+=(ssize_t) runlength[y+z*iris_info.rows];
514 status=SGIDecode(bytes_per_pixel,(ssize_t)
515 (runlength[y+z*iris_info.rows]/bytes_per_pixel),packets,
516 (ssize_t) iris_info.columns,p+bytes_per_pixel*z);
517 if (status == MagickFalse)
519 packet_info=RelinquishVirtualMemory(packet_info);
520 runlength=(size_t *) RelinquishMagickMemory(runlength);
521 offsets=(ssize_t *) RelinquishMagickMemory(offsets);
522 pixel_info=RelinquishVirtualMemory(pixel_info);
523 ThrowReaderException(CorruptImageError,
524 "ImproperImageHeader");
526 p+=(iris_info.columns*4*bytes_per_pixel);
535 position=TellBlob(image);
537 for (y=0; y < (ssize_t) iris_info.rows; y++)
539 for (z=0; z < (ssize_t) iris_info.depth; z++)
541 if (offset != offsets[y+z*iris_info.rows])
543 offset=offsets[y+z*iris_info.rows];
544 offset=(ssize_t) SeekBlob(image,(MagickOffsetType) offset,
547 count=ReadBlob(image,(size_t) runlength[y+z*iris_info.rows],
549 if (EOFBlob(image) != MagickFalse)
551 offset+=(ssize_t) runlength[y+z*iris_info.rows];
552 status=SGIDecode(bytes_per_pixel,(ssize_t)
553 (runlength[y+z*iris_info.rows]/bytes_per_pixel),packets,
554 (ssize_t) iris_info.columns,p+bytes_per_pixel*z);
555 if (status == MagickFalse)
557 packet_info=RelinquishVirtualMemory(packet_info);
558 runlength=(size_t *) RelinquishMagickMemory(runlength);
559 offsets=(ssize_t *) RelinquishMagickMemory(offsets);
560 pixel_info=RelinquishVirtualMemory(pixel_info);
561 ThrowReaderException(CorruptImageError,
562 "ImproperImageHeader");
565 p+=(iris_info.columns*4*bytes_per_pixel);
567 offset=(ssize_t) SeekBlob(image,position,SEEK_SET);
569 packet_info=RelinquishVirtualMemory(packet_info);
570 runlength=(size_t *) RelinquishMagickMemory(runlength);
571 offsets=(ssize_t *) RelinquishMagickMemory(offsets);
574 Convert SGI raster image to pixel packets.
576 if (image->storage_class == DirectClass)
579 Convert SGI image to DirectClass pixel packets.
581 if (bytes_per_pixel == 2)
583 for (y=0; y < (ssize_t) image->rows; y++)
585 p=pixels+(image->rows-y-1)*8*image->columns;
586 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
587 if (q == (Quantum *) NULL)
589 for (x=0; x < (ssize_t) image->columns; x++)
591 SetPixelRed(image,ScaleShortToQuantum((unsigned short)
592 ((*(p+0) << 8) | (*(p+1)))),q);
593 SetPixelGreen(image,ScaleShortToQuantum((unsigned short)
594 ((*(p+2) << 8) | (*(p+3)))),q);
595 SetPixelBlue(image,ScaleShortToQuantum((unsigned short)
596 ((*(p+4) << 8) | (*(p+5)))),q);
597 SetPixelAlpha(image,OpaqueAlpha,q);
598 if (image->alpha_trait != UndefinedPixelTrait)
599 SetPixelAlpha(image,ScaleShortToQuantum((unsigned short)
600 ((*(p+6) << 8) | (*(p+7)))),q);
602 q+=GetPixelChannels(image);
604 if (SyncAuthenticPixels(image,exception) == MagickFalse)
606 if (image->previous == (Image *) NULL)
608 status=SetImageProgress(image,LoadImageTag,(MagickOffsetType)
610 if (status == MagickFalse)
616 for (y=0; y < (ssize_t) image->rows; y++)
618 p=pixels+(image->rows-y-1)*4*image->columns;
619 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
620 if (q == (Quantum *) NULL)
622 for (x=0; x < (ssize_t) image->columns; x++)
624 SetPixelRed(image,ScaleCharToQuantum(*p),q);
625 SetPixelGreen(image,ScaleCharToQuantum(*(p+1)),q);
626 SetPixelBlue(image,ScaleCharToQuantum(*(p+2)),q);
627 SetPixelAlpha(image,OpaqueAlpha,q);
628 if (image->alpha_trait != UndefinedPixelTrait)
629 SetPixelAlpha(image,ScaleCharToQuantum(*(p+3)),q);
631 q+=GetPixelChannels(image);
633 if (SyncAuthenticPixels(image,exception) == MagickFalse)
635 if (image->previous == (Image *) NULL)
637 status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y,
639 if (status == MagickFalse)
647 Create grayscale map.
649 if (AcquireImageColormap(image,image->colors,exception) == MagickFalse)
650 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
652 Convert SGI image to PseudoClass pixel packets.
654 if (bytes_per_pixel == 2)
656 for (y=0; y < (ssize_t) image->rows; y++)
658 p=pixels+(image->rows-y-1)*8*image->columns;
659 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
660 if (q == (Quantum *) NULL)
662 for (x=0; x < (ssize_t) image->columns; x++)
666 SetPixelIndex(image,(Quantum) quantum,q);
668 q+=GetPixelChannels(image);
670 if (SyncAuthenticPixels(image,exception) == MagickFalse)
672 if (image->previous == (Image *) NULL)
674 status=SetImageProgress(image,LoadImageTag,(MagickOffsetType)
676 if (status == MagickFalse)
682 for (y=0; y < (ssize_t) image->rows; y++)
684 p=pixels+(image->rows-y-1)*4*image->columns;
685 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
686 if (q == (Quantum *) NULL)
688 for (x=0; x < (ssize_t) image->columns; x++)
690 SetPixelIndex(image,*p,q);
692 q+=GetPixelChannels(image);
694 if (SyncAuthenticPixels(image,exception) == MagickFalse)
696 if (image->previous == (Image *) NULL)
698 status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y,
700 if (status == MagickFalse)
704 (void) SyncImage(image,exception);
706 pixel_info=RelinquishVirtualMemory(pixel_info);
707 if (EOFBlob(image) != MagickFalse)
709 ThrowFileException(exception,CorruptImageError,"UnexpectedEndOfFile",
714 Proceed to next image.
716 if (image_info->number_scenes != 0)
717 if (image->scene >= (image_info->scene+image_info->number_scenes-1))
719 iris_info.magic=ReadBlobMSBShort(image);
720 if (iris_info.magic == 0x01DA)
723 Allocate next image structure.
725 AcquireNextImage(image_info,image,exception);
726 if (GetNextImageInList(image) == (Image *) NULL)
728 image=DestroyImageList(image);
729 return((Image *) NULL);
731 image=SyncNextImageInList(image);
732 status=SetImageProgress(image,LoadImagesTag,TellBlob(image),
734 if (status == MagickFalse)
737 } while (iris_info.magic == 0x01DA);
738 (void) CloseBlob(image);
739 return(GetFirstImageInList(image));
743 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
747 % R e g i s t e r S G I I m a g e %
751 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
753 % RegisterSGIImage() adds properties for the SGI image format to
754 % the list of supported formats. The properties include the image format
755 % tag, a method to read and/or write the format, whether the format
756 % supports the saving of more than one frame to the same file or blob,
757 % whether the format supports native in-memory I/O, and a brief
758 % description of the format.
760 % The format of the RegisterSGIImage method is:
762 % size_t RegisterSGIImage(void)
765 ModuleExport size_t RegisterSGIImage(void)
770 entry=AcquireMagickInfo("SGI","SGI","Irix RGB image");
771 entry->decoder=(DecodeImageHandler *) ReadSGIImage;
772 entry->encoder=(EncodeImageHandler *) WriteSGIImage;
773 entry->magick=(IsImageFormatHandler *) IsSGI;
774 entry->flags|=CoderDecoderSeekableStreamFlag;
775 (void) RegisterMagickInfo(entry);
776 return(MagickImageCoderSignature);
780 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
784 % U n r e g i s t e r S G I I m a g e %
788 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
790 % UnregisterSGIImage() removes format registrations made by the
791 % SGI module from the list of supported formats.
793 % The format of the UnregisterSGIImage method is:
795 % UnregisterSGIImage(void)
798 ModuleExport void UnregisterSGIImage(void)
800 (void) UnregisterMagickInfo("SGI");
804 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
808 % W r i t e S G I I m a g e %
812 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
814 % WriteSGIImage() writes an image in SGI RGB encoded image format.
816 % The format of the WriteSGIImage method is:
818 % MagickBooleanType WriteSGIImage(const ImageInfo *image_info,
819 % Image *image,ExceptionInfo *exception)
821 % A description of each parameter follows.
823 % o image_info: the image info.
825 % o image: The image.
827 % o exception: return any errors or warnings in this structure.
831 static size_t SGIEncode(unsigned char *pixels,size_t length,
832 unsigned char *packets)
837 register unsigned char
852 while ((p < limit) && ((*(p-8) != *(p-4)) || (*(p-4) != *p)))
855 length=(size_t) (p-mark) >> 2;
858 runlength=(short) (length > 126 ? 126 : length);
860 *q++=(unsigned char) (0x80 | runlength);
861 for ( ; runlength > 0; runlength--)
869 while ((p < limit) && (*p == *mark))
871 length=(size_t) (p-mark) >> 2;
874 runlength=(short) (length > 126 ? 126 : length);
876 *q++=(unsigned char) runlength;
881 return((size_t) (q-packets));
884 static MagickBooleanType WriteSGIImage(const ImageInfo *image_info,Image *image,
885 ExceptionInfo *exception)
908 register const Quantum
915 register unsigned char
927 Open output image file.
929 assert(image_info != (const ImageInfo *) NULL);
930 assert(image_info->signature == MagickCoreSignature);
931 assert(image != (Image *) NULL);
932 assert(image->signature == MagickCoreSignature);
933 if (image->debug != MagickFalse)
934 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
935 if ((image->columns > 65535UL) || (image->rows > 65535UL))
936 ThrowWriterException(ImageError,"WidthOrHeightExceedsLimit");
937 assert(exception != (ExceptionInfo *) NULL);
938 assert(exception->signature == MagickCoreSignature);
939 status=OpenBlob(image_info,image,WriteBinaryBlobMode,exception);
940 if (status == MagickFalse)
946 Initialize SGI raster file header.
948 (void) TransformImageColorspace(image,sRGBColorspace,exception);
949 (void) ResetMagickMemory(&iris_info,0,sizeof(iris_info));
950 iris_info.magic=0x01DA;
951 compression=image->compression;
952 if (image_info->compression != UndefinedCompression)
953 compression=image_info->compression;
954 if (image->depth > 8)
955 compression=NoCompression;
956 if (compression == NoCompression)
957 iris_info.storage=(unsigned char) 0x00;
959 iris_info.storage=(unsigned char) 0x01;
960 iris_info.bytes_per_pixel=(unsigned char) (image->depth > 8 ? 2 : 1);
961 iris_info.dimension=3;
962 iris_info.columns=(unsigned short) image->columns;
963 iris_info.rows=(unsigned short) image->rows;
964 if (image->alpha_trait != UndefinedPixelTrait)
968 if ((image_info->type != TrueColorType) &&
969 (SetImageGray(image,exception) != MagickFalse))
971 iris_info.dimension=2;
977 iris_info.minimum_value=0;
978 iris_info.maximum_value=(size_t) (image->depth <= 8 ?
979 1UL*ScaleQuantumToChar(QuantumRange) :
980 1UL*ScaleQuantumToShort(QuantumRange));
984 (void) WriteBlobMSBShort(image,iris_info.magic);
985 (void) WriteBlobByte(image,iris_info.storage);
986 (void) WriteBlobByte(image,iris_info.bytes_per_pixel);
987 (void) WriteBlobMSBShort(image,iris_info.dimension);
988 (void) WriteBlobMSBShort(image,iris_info.columns);
989 (void) WriteBlobMSBShort(image,iris_info.rows);
990 (void) WriteBlobMSBShort(image,iris_info.depth);
991 (void) WriteBlobMSBLong(image,(unsigned int) iris_info.minimum_value);
992 (void) WriteBlobMSBLong(image,(unsigned int) iris_info.maximum_value);
993 (void) WriteBlobMSBLong(image,(unsigned int) iris_info.sans);
994 value=GetImageProperty(image,"label",exception);
995 if (value != (const char *) NULL)
996 (void) CopyMagickString(iris_info.name,value,sizeof(iris_info.name));
997 (void) WriteBlob(image,sizeof(iris_info.name),(unsigned char *)
999 (void) WriteBlobMSBLong(image,(unsigned int) iris_info.pixel_format);
1000 (void) WriteBlob(image,sizeof(iris_info.filler),iris_info.filler);
1002 Allocate SGI pixels.
1004 number_pixels=(MagickSizeType) image->columns*image->rows;
1005 if ((4*iris_info.bytes_per_pixel*number_pixels) !=
1006 ((MagickSizeType) (size_t) (4*iris_info.bytes_per_pixel*number_pixels)))
1007 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
1008 pixel_info=AcquireVirtualMemory((size_t) number_pixels,4*
1009 iris_info.bytes_per_pixel*sizeof(*pixels));
1010 if (pixel_info == (MemoryInfo *) NULL)
1011 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
1012 pixels=(unsigned char *) GetVirtualMemoryBlob(pixel_info);
1014 Convert image pixels to uncompressed SGI pixels.
1016 for (y=0; y < (ssize_t) image->rows; y++)
1018 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
1019 if (p == (const Quantum *) NULL)
1021 if (image->depth <= 8)
1022 for (x=0; x < (ssize_t) image->columns; x++)
1024 register unsigned char
1027 q=(unsigned char *) pixels;
1028 q+=((iris_info.rows-1)-y)*(4*iris_info.columns)+4*x;
1029 *q++=ScaleQuantumToChar(GetPixelRed(image,p));
1030 *q++=ScaleQuantumToChar(GetPixelGreen(image,p));
1031 *q++=ScaleQuantumToChar(GetPixelBlue(image,p));
1032 *q++=ScaleQuantumToChar(GetPixelAlpha(image,p));
1033 p+=GetPixelChannels(image);
1036 for (x=0; x < (ssize_t) image->columns; x++)
1038 register unsigned short
1041 q=(unsigned short *) pixels;
1042 q+=((iris_info.rows-1)-y)*(4*iris_info.columns)+4*x;
1043 *q++=ScaleQuantumToShort(GetPixelRed(image,p));
1044 *q++=ScaleQuantumToShort(GetPixelGreen(image,p));
1045 *q++=ScaleQuantumToShort(GetPixelBlue(image,p));
1046 *q++=ScaleQuantumToShort(GetPixelAlpha(image,p));
1047 p+=GetPixelChannels(image);
1049 if (image->previous == (Image *) NULL)
1051 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
1053 if (status == MagickFalse)
1057 switch (compression)
1062 Write uncompressed SGI pixels.
1064 for (z=0; z < (ssize_t) iris_info.depth; z++)
1066 for (y=0; y < (ssize_t) iris_info.rows; y++)
1068 if (image->depth <= 8)
1069 for (x=0; x < (ssize_t) iris_info.columns; x++)
1071 register unsigned char
1074 q=(unsigned char *) pixels;
1075 q+=y*(4*iris_info.columns)+4*x+z;
1076 (void) WriteBlobByte(image,*q);
1079 for (x=0; x < (ssize_t) iris_info.columns; x++)
1081 register unsigned short
1084 q=(unsigned short *) pixels;
1085 q+=y*(4*iris_info.columns)+4*x+z;
1086 (void) WriteBlobMSBShort(image,*q);
1107 Convert SGI uncompressed pixels.
1109 offsets=(ssize_t *) AcquireQuantumMemory(iris_info.rows,
1110 iris_info.depth*sizeof(*offsets));
1111 runlength=(size_t *) AcquireQuantumMemory(iris_info.rows,
1112 iris_info.depth*sizeof(*runlength));
1113 packet_info=AcquireVirtualMemory((2*(size_t) iris_info.columns+10)*
1114 image->rows,4*sizeof(*packets));
1115 if ((offsets == (ssize_t *) NULL) ||
1116 (runlength == (size_t *) NULL) ||
1117 (packet_info == (MemoryInfo *) NULL))
1119 if (offsets != (ssize_t *) NULL)
1120 offsets=(ssize_t *) RelinquishMagickMemory(offsets);
1121 if (runlength != (size_t *) NULL)
1122 runlength=(size_t *) RelinquishMagickMemory(runlength);
1123 if (packet_info != (MemoryInfo *) NULL)
1124 packet_info=RelinquishVirtualMemory(packet_info);
1125 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
1127 packets=(unsigned char *) GetVirtualMemoryBlob(packet_info);
1128 offset=512+4*2*((ssize_t) iris_info.rows*iris_info.depth);
1131 for (y=0; y < (ssize_t) iris_info.rows; y++)
1133 for (z=0; z < (ssize_t) iris_info.depth; z++)
1135 length=SGIEncode(q+z,(size_t) iris_info.columns,packets+
1137 number_packets+=length;
1138 offsets[y+z*iris_info.rows]=offset;
1139 runlength[y+z*iris_info.rows]=(size_t) length;
1140 offset+=(ssize_t) length;
1142 q+=(iris_info.columns*4);
1145 Write out line start and length tables and runlength-encoded pixels.
1147 for (i=0; i < (ssize_t) (iris_info.rows*iris_info.depth); i++)
1148 (void) WriteBlobMSBLong(image,(unsigned int) offsets[i]);
1149 for (i=0; i < (ssize_t) (iris_info.rows*iris_info.depth); i++)
1150 (void) WriteBlobMSBLong(image,(unsigned int) runlength[i]);
1151 (void) WriteBlob(image,number_packets,packets);
1153 Relinquish resources.
1155 offsets=(ssize_t *) RelinquishMagickMemory(offsets);
1156 runlength=(size_t *) RelinquishMagickMemory(runlength);
1157 packet_info=RelinquishVirtualMemory(packet_info);
1161 pixel_info=RelinquishVirtualMemory(pixel_info);
1162 if (GetNextImageInList(image) == (Image *) NULL)
1164 image=SyncNextImageInList(image);
1165 status=SetImageProgress(image,SaveImagesTag,scene++,
1166 GetImageListLength(image));
1167 if (status == MagickFalse)
1169 } while (image_info->adjoin != MagickFalse);
1170 (void) CloseBlob(image);