2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
13 % Read/Write Irix RGB Image Format %
20 % Copyright 1999-2018 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)
278 register unsigned char
299 assert(image_info != (const ImageInfo *) NULL);
300 assert(image_info->signature == MagickCoreSignature);
301 if (image_info->debug != MagickFalse)
302 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
303 image_info->filename);
304 assert(exception != (ExceptionInfo *) NULL);
305 assert(exception->signature == MagickCoreSignature);
306 image=AcquireImage(image_info,exception);
307 status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception);
308 if (status == MagickFalse)
310 image=DestroyImageList(image);
311 return((Image *) NULL);
314 Read SGI raster header.
316 (void) memset(&iris_info,0,sizeof(iris_info));
317 iris_info.magic=ReadBlobMSBShort(image);
321 Verify SGI identifier.
323 if (iris_info.magic != 0x01DA)
324 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
325 iris_info.storage=(unsigned char) ReadBlobByte(image);
326 switch (iris_info.storage)
328 case 0x00: image->compression=NoCompression; break;
329 case 0x01: image->compression=RLECompression; break;
331 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
333 iris_info.bytes_per_pixel=(unsigned char) ReadBlobByte(image);
334 if ((iris_info.bytes_per_pixel == 0) || (iris_info.bytes_per_pixel > 2))
335 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
336 iris_info.dimension=ReadBlobMSBShort(image);
337 if ((iris_info.dimension == 0) || (iris_info.dimension > 3))
338 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
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 count=ReadBlob(image,sizeof(iris_info.name),(unsigned char *)
349 if ((size_t) count != sizeof(iris_info.name))
350 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
351 iris_info.name[sizeof(iris_info.name)-1]='\0';
352 if (*iris_info.name != '\0')
353 (void) SetImageProperty(image,"label",iris_info.name,exception);
354 iris_info.pixel_format=ReadBlobMSBLong(image);
355 if (iris_info.pixel_format != 0)
356 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
357 count=ReadBlob(image,sizeof(iris_info.filler),iris_info.filler);
358 if ((size_t) count != sizeof(iris_info.filler))
359 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
360 image->columns=iris_info.columns;
361 image->rows=iris_info.rows;
362 image->alpha_trait=iris_info.depth == 4 ? BlendPixelTrait :
364 image->depth=(size_t) MagickMin(iris_info.depth,MAGICKCORE_QUANTUM_DEPTH);
365 if (iris_info.pixel_format == 0)
366 image->depth=(size_t) MagickMin((size_t) 8*iris_info.bytes_per_pixel,
367 MAGICKCORE_QUANTUM_DEPTH);
368 if (iris_info.depth < 3)
370 image->storage_class=PseudoClass;
371 image->colors=(size_t) (iris_info.bytes_per_pixel > 1 ? 65535 : 256);
373 if ((image_info->ping != MagickFalse) && (image_info->number_scenes != 0))
374 if (image->scene >= (image_info->scene+image_info->number_scenes-1))
376 status=SetImageExtent(image,image->columns,image->rows,exception);
377 if (status != MagickFalse)
378 status=ResetImagePixels(image,exception);
379 if (status == MagickFalse)
380 return(DestroyImageList(image));
384 bytes_per_pixel=(size_t) iris_info.bytes_per_pixel;
385 number_pixels=(MagickSizeType) iris_info.columns*iris_info.rows;
386 if ((4*bytes_per_pixel*number_pixels) != ((MagickSizeType) (size_t)
387 (4*bytes_per_pixel*number_pixels)))
388 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
389 pixel_info=AcquireVirtualMemory(iris_info.columns,iris_info.rows*4*
390 bytes_per_pixel*sizeof(*pixels));
391 if (pixel_info == (MemoryInfo *) NULL)
392 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
393 pixels=(unsigned char *) GetVirtualMemoryBlob(pixel_info);
394 for (n=0; n < (4*bytes_per_pixel*number_pixels); n++)
396 if ((int) iris_info.storage != 0x01)
402 Read standard image format.
404 scanline=(unsigned char *) AcquireQuantumMemory(iris_info.columns,
405 bytes_per_pixel*sizeof(*scanline));
406 if (scanline == (unsigned char *) NULL)
408 pixel_info=RelinquishVirtualMemory(pixel_info);
409 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
411 for (z=0; z < (ssize_t) iris_info.depth; z++)
413 p=pixels+bytes_per_pixel*z;
414 for (y=0; y < (ssize_t) iris_info.rows; y++)
416 count=ReadBlob(image,bytes_per_pixel*iris_info.columns,scanline);
417 if (count != (bytes_per_pixel*iris_info.columns))
419 if (bytes_per_pixel == 2)
420 for (x=0; x < (ssize_t) iris_info.columns; x++)
423 *(p+1)=scanline[2*x+1];
427 for (x=0; x < (ssize_t) iris_info.columns; x++)
433 if (y < (ssize_t) iris_info.rows)
436 scanline=(unsigned char *) RelinquishMagickMemory(scanline);
457 Read runlength-encoded image format.
459 offsets=(ssize_t *) AcquireQuantumMemory((size_t) iris_info.rows,
460 iris_info.depth*sizeof(*offsets));
461 runlength=(size_t *) AcquireQuantumMemory(iris_info.rows,
462 iris_info.depth*sizeof(*runlength));
463 packet_info=AcquireVirtualMemory((size_t) iris_info.columns+10UL,4UL*
465 if ((offsets == (ssize_t *) NULL) || (runlength == (size_t *) NULL) ||
466 (packet_info == (MemoryInfo *) NULL))
468 offsets=(ssize_t *) RelinquishMagickMemory(offsets);
469 runlength=(size_t *) RelinquishMagickMemory(runlength);
470 if (packet_info != (MemoryInfo *) NULL)
471 packet_info=RelinquishVirtualMemory(packet_info);
472 pixel_info=RelinquishVirtualMemory(pixel_info);
473 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
475 packets=(unsigned char *) GetVirtualMemoryBlob(packet_info);
476 for (i=0; i < (ssize_t) (iris_info.rows*iris_info.depth); i++)
477 offsets[i]=(ssize_t) ReadBlobMSBSignedLong(image);
478 for (i=0; i < (ssize_t) (iris_info.rows*iris_info.depth); i++)
480 runlength[i]=ReadBlobMSBLong(image);
481 if (runlength[i] > (4*(size_t) iris_info.columns+10))
483 packet_info=RelinquishVirtualMemory(packet_info);
484 runlength=(size_t *) RelinquishMagickMemory(runlength);
485 offsets=(ssize_t *) RelinquishMagickMemory(offsets);
486 pixel_info=RelinquishVirtualMemory(pixel_info);
487 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
495 for (y=0; ((y < (ssize_t) iris_info.rows) && (data_order == 0)); y++)
496 for (z=0; ((z < (ssize_t) iris_info.depth) && (data_order == 0)); z++)
498 if (offsets[y+z*iris_info.rows] < offset)
500 offset=offsets[y+z*iris_info.rows];
502 offset=(ssize_t) TellBlob(image);
505 for (z=0; z < (ssize_t) iris_info.depth; z++)
508 for (y=0; y < (ssize_t) iris_info.rows; y++)
510 if (offset != offsets[y+z*iris_info.rows])
512 offset=offsets[y+z*iris_info.rows];
513 offset=(ssize_t) SeekBlob(image,(MagickOffsetType) offset,
516 count=ReadBlob(image,(size_t) runlength[y+z*iris_info.rows],
518 if (count != runlength[y+z*iris_info.rows])
520 offset+=(ssize_t) runlength[y+z*iris_info.rows];
521 status=SGIDecode(bytes_per_pixel,(ssize_t)
522 (runlength[y+z*iris_info.rows]/bytes_per_pixel),packets,
523 (ssize_t) iris_info.columns,p+bytes_per_pixel*z);
524 if (status == MagickFalse)
526 packet_info=RelinquishVirtualMemory(packet_info);
527 runlength=(size_t *) RelinquishMagickMemory(runlength);
528 offsets=(ssize_t *) RelinquishMagickMemory(offsets);
529 pixel_info=RelinquishVirtualMemory(pixel_info);
530 ThrowReaderException(CorruptImageError,
531 "ImproperImageHeader");
533 p+=(iris_info.columns*4*bytes_per_pixel);
535 if (y < (ssize_t) iris_info.rows)
544 position=TellBlob(image);
546 for (y=0; y < (ssize_t) iris_info.rows; y++)
548 for (z=0; z < (ssize_t) iris_info.depth; z++)
550 if (offset != offsets[y+z*iris_info.rows])
552 offset=offsets[y+z*iris_info.rows];
553 offset=(ssize_t) SeekBlob(image,(MagickOffsetType) offset,
556 count=ReadBlob(image,(size_t) runlength[y+z*iris_info.rows],
558 if (count != runlength[y+z*iris_info.rows])
560 offset+=(ssize_t) runlength[y+z*iris_info.rows];
561 status=SGIDecode(bytes_per_pixel,(ssize_t)
562 (runlength[y+z*iris_info.rows]/bytes_per_pixel),packets,
563 (ssize_t) iris_info.columns,p+bytes_per_pixel*z);
564 if (status == MagickFalse)
566 packet_info=RelinquishVirtualMemory(packet_info);
567 runlength=(size_t *) RelinquishMagickMemory(runlength);
568 offsets=(ssize_t *) RelinquishMagickMemory(offsets);
569 pixel_info=RelinquishVirtualMemory(pixel_info);
570 ThrowReaderException(CorruptImageError,
571 "ImproperImageHeader");
574 if (z < (ssize_t) iris_info.depth)
576 p+=(iris_info.columns*4*bytes_per_pixel);
578 offset=(ssize_t) SeekBlob(image,position,SEEK_SET);
580 packet_info=RelinquishVirtualMemory(packet_info);
581 runlength=(size_t *) RelinquishMagickMemory(runlength);
582 offsets=(ssize_t *) RelinquishMagickMemory(offsets);
585 Convert SGI raster image to pixel packets.
587 if (image->storage_class == DirectClass)
590 Convert SGI image to DirectClass pixel packets.
592 if (bytes_per_pixel == 2)
594 for (y=0; y < (ssize_t) image->rows; y++)
596 p=pixels+(image->rows-y-1)*8*image->columns;
597 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
598 if (q == (Quantum *) NULL)
600 for (x=0; x < (ssize_t) image->columns; x++)
602 SetPixelRed(image,ScaleShortToQuantum((unsigned short)
603 ((*(p+0) << 8) | (*(p+1)))),q);
604 SetPixelGreen(image,ScaleShortToQuantum((unsigned short)
605 ((*(p+2) << 8) | (*(p+3)))),q);
606 SetPixelBlue(image,ScaleShortToQuantum((unsigned short)
607 ((*(p+4) << 8) | (*(p+5)))),q);
608 SetPixelAlpha(image,OpaqueAlpha,q);
609 if (image->alpha_trait != UndefinedPixelTrait)
610 SetPixelAlpha(image,ScaleShortToQuantum((unsigned short)
611 ((*(p+6) << 8) | (*(p+7)))),q);
613 q+=GetPixelChannels(image);
615 if (SyncAuthenticPixels(image,exception) == MagickFalse)
617 if (image->previous == (Image *) NULL)
619 status=SetImageProgress(image,LoadImageTag,(MagickOffsetType)
621 if (status == MagickFalse)
627 for (y=0; y < (ssize_t) image->rows; y++)
629 p=pixels+(image->rows-y-1)*4*image->columns;
630 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
631 if (q == (Quantum *) NULL)
633 for (x=0; x < (ssize_t) image->columns; x++)
635 SetPixelRed(image,ScaleCharToQuantum(*p),q);
636 SetPixelGreen(image,ScaleCharToQuantum(*(p+1)),q);
637 SetPixelBlue(image,ScaleCharToQuantum(*(p+2)),q);
638 SetPixelAlpha(image,OpaqueAlpha,q);
639 if (image->alpha_trait != UndefinedPixelTrait)
640 SetPixelAlpha(image,ScaleCharToQuantum(*(p+3)),q);
642 q+=GetPixelChannels(image);
644 if (SyncAuthenticPixels(image,exception) == MagickFalse)
646 if (image->previous == (Image *) NULL)
648 status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y,
650 if (status == MagickFalse)
658 Create grayscale map.
660 if (AcquireImageColormap(image,image->colors,exception) == MagickFalse)
662 pixel_info=RelinquishVirtualMemory(pixel_info);
663 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
666 Convert SGI image to PseudoClass pixel packets.
668 if (bytes_per_pixel == 2)
670 for (y=0; y < (ssize_t) image->rows; y++)
672 p=pixels+(image->rows-y-1)*8*image->columns;
673 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
674 if (q == (Quantum *) NULL)
676 for (x=0; x < (ssize_t) image->columns; x++)
680 SetPixelIndex(image,(Quantum) quantum,q);
682 q+=GetPixelChannels(image);
684 if (SyncAuthenticPixels(image,exception) == MagickFalse)
686 if (image->previous == (Image *) NULL)
688 status=SetImageProgress(image,LoadImageTag,(MagickOffsetType)
690 if (status == MagickFalse)
696 for (y=0; y < (ssize_t) image->rows; y++)
698 p=pixels+(image->rows-y-1)*4*image->columns;
699 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
700 if (q == (Quantum *) NULL)
702 for (x=0; x < (ssize_t) image->columns; x++)
704 SetPixelIndex(image,*p,q);
706 q+=GetPixelChannels(image);
708 if (SyncAuthenticPixels(image,exception) == MagickFalse)
710 if (image->previous == (Image *) NULL)
712 status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y,
714 if (status == MagickFalse)
718 (void) SyncImage(image,exception);
720 pixel_info=RelinquishVirtualMemory(pixel_info);
721 if (EOFBlob(image) != MagickFalse)
723 ThrowFileException(exception,CorruptImageError,"UnexpectedEndOfFile",
728 Proceed to next image.
730 if (image_info->number_scenes != 0)
731 if (image->scene >= (image_info->scene+image_info->number_scenes-1))
733 iris_info.magic=ReadBlobMSBShort(image);
734 if (iris_info.magic == 0x01DA)
737 Allocate next image structure.
739 AcquireNextImage(image_info,image,exception);
740 if (GetNextImageInList(image) == (Image *) NULL)
745 image=SyncNextImageInList(image);
746 status=SetImageProgress(image,LoadImagesTag,TellBlob(image),
748 if (status == MagickFalse)
751 } while (iris_info.magic == 0x01DA);
752 (void) CloseBlob(image);
753 if (status == MagickFalse)
754 return(DestroyImageList(image));
755 return(GetFirstImageInList(image));
759 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
763 % R e g i s t e r S G I I m a g e %
767 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
769 % RegisterSGIImage() adds properties for the SGI image format to
770 % the list of supported formats. The properties include the image format
771 % tag, a method to read and/or write the format, whether the format
772 % supports the saving of more than one frame to the same file or blob,
773 % whether the format supports native in-memory I/O, and a brief
774 % description of the format.
776 % The format of the RegisterSGIImage method is:
778 % size_t RegisterSGIImage(void)
781 ModuleExport size_t RegisterSGIImage(void)
786 entry=AcquireMagickInfo("SGI","SGI","Irix RGB image");
787 entry->decoder=(DecodeImageHandler *) ReadSGIImage;
788 entry->encoder=(EncodeImageHandler *) WriteSGIImage;
789 entry->magick=(IsImageFormatHandler *) IsSGI;
790 entry->flags|=CoderDecoderSeekableStreamFlag;
791 (void) RegisterMagickInfo(entry);
792 return(MagickImageCoderSignature);
796 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
800 % U n r e g i s t e r S G I I m a g e %
804 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
806 % UnregisterSGIImage() removes format registrations made by the
807 % SGI module from the list of supported formats.
809 % The format of the UnregisterSGIImage method is:
811 % UnregisterSGIImage(void)
814 ModuleExport void UnregisterSGIImage(void)
816 (void) UnregisterMagickInfo("SGI");
820 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
824 % W r i t e S G I I m a g e %
828 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
830 % WriteSGIImage() writes an image in SGI RGB encoded image format.
832 % The format of the WriteSGIImage method is:
834 % MagickBooleanType WriteSGIImage(const ImageInfo *image_info,
835 % Image *image,ExceptionInfo *exception)
837 % A description of each parameter follows.
839 % o image_info: the image info.
841 % o image: The image.
843 % o exception: return any errors or warnings in this structure.
847 static size_t SGIEncode(unsigned char *pixels,size_t length,
848 unsigned char *packets)
853 register unsigned char
868 while ((p < limit) && ((*(p-8) != *(p-4)) || (*(p-4) != *p)))
871 length=(size_t) (p-mark) >> 2;
874 runlength=(short) (length > 126 ? 126 : length);
876 *q++=(unsigned char) (0x80 | runlength);
877 for ( ; runlength > 0; runlength--)
885 while ((p < limit) && (*p == *mark))
887 length=(size_t) (p-mark) >> 2;
890 runlength=(short) (length > 126 ? 126 : length);
892 *q++=(unsigned char) runlength;
897 return((size_t) (q-packets));
900 static MagickBooleanType WriteSGIImage(const ImageInfo *image_info,Image *image,
901 ExceptionInfo *exception)
924 register const Quantum
931 register unsigned char
946 Open output image file.
948 assert(image_info != (const ImageInfo *) NULL);
949 assert(image_info->signature == MagickCoreSignature);
950 assert(image != (Image *) NULL);
951 assert(image->signature == MagickCoreSignature);
952 if (image->debug != MagickFalse)
953 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
954 if ((image->columns > 65535UL) || (image->rows > 65535UL))
955 ThrowWriterException(ImageError,"WidthOrHeightExceedsLimit");
956 assert(exception != (ExceptionInfo *) NULL);
957 assert(exception->signature == MagickCoreSignature);
958 status=OpenBlob(image_info,image,WriteBinaryBlobMode,exception);
959 if (status == MagickFalse)
962 imageListLength=GetImageListLength(image);
966 Initialize SGI raster file header.
968 (void) TransformImageColorspace(image,sRGBColorspace,exception);
969 (void) memset(&iris_info,0,sizeof(iris_info));
970 iris_info.magic=0x01DA;
971 compression=image->compression;
972 if (image_info->compression != UndefinedCompression)
973 compression=image_info->compression;
974 if (image->depth > 8)
975 compression=NoCompression;
976 if (compression == NoCompression)
977 iris_info.storage=(unsigned char) 0x00;
979 iris_info.storage=(unsigned char) 0x01;
980 iris_info.bytes_per_pixel=(unsigned char) (image->depth > 8 ? 2 : 1);
981 iris_info.dimension=3;
982 iris_info.columns=(unsigned short) image->columns;
983 iris_info.rows=(unsigned short) image->rows;
984 if (image->alpha_trait != UndefinedPixelTrait)
988 if ((image_info->type != TrueColorType) &&
989 (SetImageGray(image,exception) != MagickFalse))
991 iris_info.dimension=2;
997 iris_info.minimum_value=0;
998 iris_info.maximum_value=(size_t) (image->depth <= 8 ?
999 1UL*ScaleQuantumToChar(QuantumRange) :
1000 1UL*ScaleQuantumToShort(QuantumRange));
1004 (void) WriteBlobMSBShort(image,iris_info.magic);
1005 (void) WriteBlobByte(image,iris_info.storage);
1006 (void) WriteBlobByte(image,iris_info.bytes_per_pixel);
1007 (void) WriteBlobMSBShort(image,iris_info.dimension);
1008 (void) WriteBlobMSBShort(image,iris_info.columns);
1009 (void) WriteBlobMSBShort(image,iris_info.rows);
1010 (void) WriteBlobMSBShort(image,iris_info.depth);
1011 (void) WriteBlobMSBLong(image,(unsigned int) iris_info.minimum_value);
1012 (void) WriteBlobMSBLong(image,(unsigned int) iris_info.maximum_value);
1013 (void) WriteBlobMSBLong(image,(unsigned int) iris_info.sans);
1014 value=GetImageProperty(image,"label",exception);
1015 if (value != (const char *) NULL)
1016 (void) CopyMagickString(iris_info.name,value,sizeof(iris_info.name));
1017 (void) WriteBlob(image,sizeof(iris_info.name),(unsigned char *)
1019 (void) WriteBlobMSBLong(image,(unsigned int) iris_info.pixel_format);
1020 (void) WriteBlob(image,sizeof(iris_info.filler),iris_info.filler);
1022 Allocate SGI pixels.
1024 number_pixels=(MagickSizeType) image->columns*image->rows;
1025 if ((4*iris_info.bytes_per_pixel*number_pixels) !=
1026 ((MagickSizeType) (size_t) (4*iris_info.bytes_per_pixel*number_pixels)))
1027 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
1028 pixel_info=AcquireVirtualMemory((size_t) number_pixels,4*
1029 iris_info.bytes_per_pixel*sizeof(*pixels));
1030 if (pixel_info == (MemoryInfo *) NULL)
1031 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
1032 pixels=(unsigned char *) GetVirtualMemoryBlob(pixel_info);
1034 Convert image pixels to uncompressed SGI pixels.
1036 for (y=0; y < (ssize_t) image->rows; y++)
1038 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
1039 if (p == (const Quantum *) NULL)
1041 if (image->depth <= 8)
1042 for (x=0; x < (ssize_t) image->columns; x++)
1044 register unsigned char
1047 q=(unsigned char *) pixels;
1048 q+=((iris_info.rows-1)-y)*(4*iris_info.columns)+4*x;
1049 *q++=ScaleQuantumToChar(GetPixelRed(image,p));
1050 *q++=ScaleQuantumToChar(GetPixelGreen(image,p));
1051 *q++=ScaleQuantumToChar(GetPixelBlue(image,p));
1052 *q++=ScaleQuantumToChar(GetPixelAlpha(image,p));
1053 p+=GetPixelChannels(image);
1056 for (x=0; x < (ssize_t) image->columns; x++)
1058 register unsigned short
1061 q=(unsigned short *) pixels;
1062 q+=((iris_info.rows-1)-y)*(4*iris_info.columns)+4*x;
1063 *q++=ScaleQuantumToShort(GetPixelRed(image,p));
1064 *q++=ScaleQuantumToShort(GetPixelGreen(image,p));
1065 *q++=ScaleQuantumToShort(GetPixelBlue(image,p));
1066 *q++=ScaleQuantumToShort(GetPixelAlpha(image,p));
1067 p+=GetPixelChannels(image);
1069 if (image->previous == (Image *) NULL)
1071 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
1073 if (status == MagickFalse)
1077 switch (compression)
1082 Write uncompressed SGI pixels.
1084 for (z=0; z < (ssize_t) iris_info.depth; z++)
1086 for (y=0; y < (ssize_t) iris_info.rows; y++)
1088 if (image->depth <= 8)
1089 for (x=0; x < (ssize_t) iris_info.columns; x++)
1091 register unsigned char
1094 q=(unsigned char *) pixels;
1095 q+=y*(4*iris_info.columns)+4*x+z;
1096 (void) WriteBlobByte(image,*q);
1099 for (x=0; x < (ssize_t) iris_info.columns; x++)
1101 register unsigned short
1104 q=(unsigned short *) pixels;
1105 q+=y*(4*iris_info.columns)+4*x+z;
1106 (void) WriteBlobMSBShort(image,*q);
1127 Convert SGI uncompressed pixels.
1129 offsets=(ssize_t *) AcquireQuantumMemory(iris_info.rows,
1130 iris_info.depth*sizeof(*offsets));
1131 runlength=(size_t *) AcquireQuantumMemory(iris_info.rows,
1132 iris_info.depth*sizeof(*runlength));
1133 packet_info=AcquireVirtualMemory((2*(size_t) iris_info.columns+10)*
1134 image->rows,4*sizeof(*packets));
1135 if ((offsets == (ssize_t *) NULL) ||
1136 (runlength == (size_t *) NULL) ||
1137 (packet_info == (MemoryInfo *) NULL))
1139 if (offsets != (ssize_t *) NULL)
1140 offsets=(ssize_t *) RelinquishMagickMemory(offsets);
1141 if (runlength != (size_t *) NULL)
1142 runlength=(size_t *) RelinquishMagickMemory(runlength);
1143 if (packet_info != (MemoryInfo *) NULL)
1144 packet_info=RelinquishVirtualMemory(packet_info);
1145 pixel_info=RelinquishVirtualMemory(pixel_info);
1146 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
1148 packets=(unsigned char *) GetVirtualMemoryBlob(packet_info);
1149 offset=512+4*2*((ssize_t) iris_info.rows*iris_info.depth);
1152 for (y=0; y < (ssize_t) iris_info.rows; y++)
1154 for (z=0; z < (ssize_t) iris_info.depth; z++)
1156 length=SGIEncode(q+z,(size_t) iris_info.columns,packets+
1158 number_packets+=length;
1159 offsets[y+z*iris_info.rows]=offset;
1160 runlength[y+z*iris_info.rows]=(size_t) length;
1161 offset+=(ssize_t) length;
1163 q+=(iris_info.columns*4);
1166 Write out line start and length tables and runlength-encoded pixels.
1168 for (i=0; i < (ssize_t) (iris_info.rows*iris_info.depth); i++)
1169 (void) WriteBlobMSBLong(image,(unsigned int) offsets[i]);
1170 for (i=0; i < (ssize_t) (iris_info.rows*iris_info.depth); i++)
1171 (void) WriteBlobMSBLong(image,(unsigned int) runlength[i]);
1172 (void) WriteBlob(image,number_packets,packets);
1174 Relinquish resources.
1176 offsets=(ssize_t *) RelinquishMagickMemory(offsets);
1177 runlength=(size_t *) RelinquishMagickMemory(runlength);
1178 packet_info=RelinquishVirtualMemory(packet_info);
1182 pixel_info=RelinquishVirtualMemory(pixel_info);
1183 if (GetNextImageInList(image) == (Image *) NULL)
1185 image=SyncNextImageInList(image);
1186 status=SetImageProgress(image,SaveImagesTag,scene++,imageListLength);
1187 if (status == MagickFalse)
1189 } while (image_info->adjoin != MagickFalse);
1190 (void) CloseBlob(image);