2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
13 % Read/Write Sun Rasterfile 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/colormap-private.h"
51 #include "MagickCore/colorspace.h"
52 #include "MagickCore/colorspace-private.h"
53 #include "MagickCore/exception.h"
54 #include "MagickCore/exception-private.h"
55 #include "MagickCore/image.h"
56 #include "MagickCore/image-private.h"
57 #include "MagickCore/list.h"
58 #include "MagickCore/magick.h"
59 #include "MagickCore/memory_.h"
60 #include "MagickCore/memory-private.h"
61 #include "MagickCore/monitor.h"
62 #include "MagickCore/monitor-private.h"
63 #include "MagickCore/pixel-accessor.h"
64 #include "MagickCore/quantum-private.h"
65 #include "MagickCore/static.h"
66 #include "MagickCore/string_.h"
67 #include "MagickCore/module.h"
72 static MagickBooleanType
73 WriteSUNImage(const ImageInfo *,Image *,ExceptionInfo *);
76 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
84 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
86 % IsSUN() returns MagickTrue if the image format type, identified by the
87 % magick string, is SUN.
89 % The format of the IsSUN method is:
91 % MagickBooleanType IsSUN(const unsigned char *magick,const size_t length)
93 % A description of each parameter follows:
95 % o magick: compare image format pattern against these bytes.
97 % o length: Specifies the length of the magick string.
100 static MagickBooleanType IsSUN(const unsigned char *magick,const size_t length)
104 if (memcmp(magick,"\131\246\152\225",4) == 0)
110 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
114 % D e c o d e I m a g e %
118 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
120 % DecodeImage unpacks the packed image pixels into runlength-encoded pixel
123 % The format of the DecodeImage method is:
125 % MagickBooleanType DecodeImage(const unsigned char *compressed_pixels,
126 % const size_t length,unsigned char *pixels)
128 % A description of each parameter follows:
130 % o compressed_pixels: The address of a byte (8 bits) array of compressed
133 % o length: An integer value that is the total number of bytes of the
134 % source image (as just read by ReadBlob)
136 % o pixels: The address of a byte (8 bits) array of pixel data created by
137 % the uncompression process. The number of bytes in this array
138 % must be at least equal to the number columns times the number of rows
139 % of the source pixels.
142 static MagickBooleanType DecodeImage(const unsigned char *compressed_pixels,
143 const size_t length,unsigned char *pixels,size_t extent)
145 register const unsigned char
148 register unsigned char
157 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
158 assert(compressed_pixels != (unsigned char *) NULL);
159 assert(pixels != (unsigned char *) NULL);
162 while (((size_t) (p-compressed_pixels) < length) &&
163 ((size_t) (q-pixels) < extent))
171 Runlength-encoded packet: <count><byte>.
173 if (((size_t) (p-compressed_pixels) >= length))
178 if (((size_t) (p-compressed_pixels) >= length))
182 while ((count >= 0) && ((size_t) (q-pixels) < extent))
189 return(((size_t) (q-pixels) == extent) ? MagickTrue : MagickFalse);
193 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
197 % R e a d S U N I m a g e %
201 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
203 % ReadSUNImage() reads a SUN image file and returns it. It allocates
204 % the memory necessary for the new Image structure and returns a pointer to
207 % The format of the ReadSUNImage method is:
209 % Image *ReadSUNImage(const ImageInfo *image_info,ExceptionInfo *exception)
211 % A description of each parameter follows:
213 % o image_info: the image info.
215 % o exception: return any errors or warnings in this structure.
218 static Image *ReadSUNImage(const ImageInfo *image_info,ExceptionInfo *exception)
220 #define RMT_EQUAL_RGB 1
223 #define RT_STANDARD 1
225 #define RT_FORMAT_RGB 3
227 typedef struct _SUNInfo
259 register unsigned char
283 assert(image_info != (const ImageInfo *) NULL);
284 assert(image_info->signature == MagickCoreSignature);
285 if (image_info->debug != MagickFalse)
286 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
287 image_info->filename);
288 assert(exception != (ExceptionInfo *) NULL);
289 assert(exception->signature == MagickCoreSignature);
290 image=AcquireImage(image_info,exception);
291 status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception);
292 if (status == MagickFalse)
294 image=DestroyImageList(image);
295 return((Image *) NULL);
298 Read SUN raster header.
300 (void) ResetMagickMemory(&sun_info,0,sizeof(sun_info));
301 sun_info.magic=ReadBlobMSBLong(image);
305 Verify SUN identifier.
307 if (sun_info.magic != 0x59a66a95)
308 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
309 sun_info.width=ReadBlobMSBLong(image);
310 sun_info.height=ReadBlobMSBLong(image);
311 sun_info.depth=ReadBlobMSBLong(image);
312 sun_info.length=ReadBlobMSBLong(image);
313 sun_info.type=ReadBlobMSBLong(image);
314 sun_info.maptype=ReadBlobMSBLong(image);
315 sun_info.maplength=ReadBlobMSBLong(image);
316 extent=sun_info.height*sun_info.width;
317 if ((sun_info.height != 0) && (sun_info.width != extent/sun_info.height))
318 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
319 if ((sun_info.type != RT_STANDARD) && (sun_info.type != RT_ENCODED) &&
320 (sun_info.type != RT_FORMAT_RGB))
321 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
322 if ((sun_info.maptype == RMT_NONE) && (sun_info.maplength != 0))
323 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
324 if ((sun_info.depth != 1) && (sun_info.depth != 8) &&
325 (sun_info.depth != 24) && (sun_info.depth != 32))
326 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
327 if ((sun_info.maptype != RMT_NONE) && (sun_info.maptype != RMT_EQUAL_RGB) &&
328 (sun_info.maptype != RMT_RAW))
329 ThrowReaderException(CoderError,"ColormapTypeNotSupported");
330 image->columns=sun_info.width;
331 image->rows=sun_info.height;
332 image->depth=sun_info.depth <= 8 ? sun_info.depth :
333 MAGICKCORE_QUANTUM_DEPTH;
334 if (sun_info.depth < 24)
339 if (sun_info.maplength > GetBlobSize(image))
340 ThrowReaderException(CorruptImageError,"InsufficientImageDataInFile");
341 image->colors=sun_info.maplength;
343 if (sun_info.maptype == RMT_NONE)
344 image->colors=one << sun_info.depth;
345 if (sun_info.maptype == RMT_EQUAL_RGB)
346 image->colors=sun_info.maplength/3;
347 if (AcquireImageColormap(image,image->colors,exception) == MagickFalse)
348 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
350 switch (sun_info.maptype)
360 Read SUN raster colormap.
362 sun_colormap=(unsigned char *) AcquireQuantumMemory(image->colors,
363 sizeof(*sun_colormap));
364 if (sun_colormap == (unsigned char *) NULL)
365 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
366 count=ReadBlob(image,image->colors,sun_colormap);
367 if (count != (ssize_t) image->colors)
369 sun_colormap=(unsigned char *) RelinquishMagickMemory(sun_colormap);
370 ThrowReaderException(CorruptImageError,"UnexpectedEndOfFile");
372 for (i=0; i < (ssize_t) image->colors; i++)
373 image->colormap[i].red=(MagickRealType) ScaleCharToQuantum(
375 count=ReadBlob(image,image->colors,sun_colormap);
376 if (count != (ssize_t) image->colors)
378 sun_colormap=(unsigned char *) RelinquishMagickMemory(sun_colormap);
379 ThrowReaderException(CorruptImageError,"UnexpectedEndOfFile");
381 for (i=0; i < (ssize_t) image->colors; i++)
382 image->colormap[i].green=(MagickRealType) ScaleCharToQuantum(
384 count=ReadBlob(image,image->colors,sun_colormap);
385 if (count != (ssize_t) image->colors)
387 sun_colormap=(unsigned char *) RelinquishMagickMemory(sun_colormap);
388 ThrowReaderException(CorruptImageError,"UnexpectedEndOfFile");
390 for (i=0; i < (ssize_t) image->colors; i++)
391 image->colormap[i].blue=(MagickRealType) ScaleCharToQuantum(
393 sun_colormap=(unsigned char *) RelinquishMagickMemory(sun_colormap);
402 Read SUN raster colormap.
404 sun_colormap=(unsigned char *) AcquireQuantumMemory(sun_info.maplength,
405 sizeof(*sun_colormap));
406 if (sun_colormap == (unsigned char *) NULL)
407 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
408 count=ReadBlob(image,sun_info.maplength,sun_colormap);
409 sun_colormap=(unsigned char *) RelinquishMagickMemory(sun_colormap);
410 if (count != (ssize_t) sun_info.maplength)
411 ThrowReaderException(CorruptImageError,"UnexpectedEndOfFile");
415 ThrowReaderException(CoderError,"ColormapTypeNotSupported");
417 image->alpha_trait=sun_info.depth == 32 ? BlendPixelTrait :
419 image->columns=sun_info.width;
420 image->rows=sun_info.height;
421 if (image_info->ping != MagickFalse)
423 (void) CloseBlob(image);
424 return(GetFirstImageInList(image));
426 status=SetImageExtent(image,image->columns,image->rows,exception);
427 if (status == MagickFalse)
428 return(DestroyImageList(image));
429 if (sun_info.length == 0)
430 ThrowReaderException(ResourceLimitError,"ImproperImageHeader");
431 number_pixels=(MagickSizeType) (image->columns*image->rows);
432 if ((sun_info.type != RT_ENCODED) &&
433 ((number_pixels*sun_info.depth) > (8UL*sun_info.length)))
434 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
435 if (HeapOverflowSanityCheck(sun_info.width,sun_info.depth) != MagickFalse)
436 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
437 bytes_per_line=sun_info.width*sun_info.depth;
438 if (sun_info.length > GetBlobSize(image))
439 ThrowReaderException(CorruptImageError,"InsufficientImageDataInFile");
440 sun_data=(unsigned char *) AcquireQuantumMemory(sun_info.length,
442 if (sun_data == (unsigned char *) NULL)
443 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
444 count=(ssize_t) ReadBlob(image,sun_info.length,sun_data);
445 if (count != (ssize_t) sun_info.length)
447 sun_data=(unsigned char *) RelinquishMagickMemory(sun_data);
448 ThrowReaderException(CorruptImageError,"UnableToReadImageData");
450 height=sun_info.height;
451 if ((height == 0) || (sun_info.width == 0) || (sun_info.depth == 0) ||
452 ((bytes_per_line/sun_info.depth) != sun_info.width))
454 sun_data=(unsigned char *) RelinquishMagickMemory(sun_data);
455 ThrowReaderException(ResourceLimitError,"ImproperImageHeader");
457 quantum=sun_info.depth == 1 ? 15 : 7;
458 bytes_per_line+=quantum;
460 if ((bytes_per_line >> 1) != (sun_info.width*sun_info.depth+quantum))
462 sun_data=(unsigned char *) RelinquishMagickMemory(sun_data);
463 ThrowReaderException(ResourceLimitError,"ImproperImageHeader");
466 if (HeapOverflowSanityCheck(height,bytes_per_line) != MagickFalse)
468 sun_data=(unsigned char *) RelinquishMagickMemory(sun_data);
469 ThrowReaderException(ResourceLimitError,"ImproperImageHeader");
471 pixels_length=height*bytes_per_line;
472 sun_pixels=(unsigned char *) AcquireQuantumMemory(pixels_length+image->rows,
473 sizeof(*sun_pixels));
474 if (sun_pixels == (unsigned char *) NULL)
476 sun_data=(unsigned char *) RelinquishMagickMemory(sun_data);
477 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
479 ResetMagickMemory(sun_pixels,0,pixels_length*sizeof(*sun_pixels));
480 if (sun_info.type == RT_ENCODED)
482 status=DecodeImage(sun_data,sun_info.length,sun_pixels,pixels_length);
483 if (status == MagickFalse)
485 sun_data=(unsigned char *) RelinquishMagickMemory(sun_data);
486 sun_pixels=(unsigned char *) RelinquishMagickMemory(sun_pixels);
487 ThrowReaderException(CorruptImageError,"UnableToReadImageData");
492 if (sun_info.length > pixels_length)
494 sun_data=(unsigned char *) RelinquishMagickMemory(sun_data);
495 sun_pixels=(unsigned char *) RelinquishMagickMemory(sun_pixels);
496 ThrowReaderException(ResourceLimitError,"ImproperImageHeader");
498 (void) CopyMagickMemory(sun_pixels,sun_data,sun_info.length);
500 sun_data=(unsigned char *) RelinquishMagickMemory(sun_data);
502 Convert SUN raster image to pixel packets.
505 if (sun_info.depth == 1)
506 for (y=0; y < (ssize_t) image->rows; y++)
508 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
509 if (q == (Quantum *) NULL)
511 for (x=0; x < ((ssize_t) image->columns-7); x+=8)
513 for (bit=7; bit >= 0; bit--)
515 SetPixelIndex(image,(Quantum) ((*p) & (0x01 << bit) ? 0x00 : 0x01),
517 q+=GetPixelChannels(image);
521 if ((image->columns % 8) != 0)
523 for (bit=7; bit >= (int) (8-(image->columns % 8)); bit--)
525 SetPixelIndex(image,(Quantum) ((*p) & (0x01 << bit) ? 0x00 :
527 q+=GetPixelChannels(image);
531 if ((((image->columns/8)+(image->columns % 8 ? 1 : 0)) % 2) != 0)
533 if (SyncAuthenticPixels(image,exception) == MagickFalse)
535 if (image->previous == (Image *) NULL)
537 status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y,
539 if (status == MagickFalse)
544 if (image->storage_class == PseudoClass)
546 for (y=0; y < (ssize_t) image->rows; y++)
548 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
549 if (q == (Quantum *) NULL)
551 for (x=0; x < (ssize_t) image->columns; x++)
553 SetPixelIndex(image,ConstrainColormapIndex(image,*p,exception),q);
555 q+=GetPixelChannels(image);
557 if ((image->columns % 2) != 0)
559 if (SyncAuthenticPixels(image,exception) == MagickFalse)
561 if (image->previous == (Image *) NULL)
563 status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y,
565 if (status == MagickFalse)
576 if (image->alpha_trait != UndefinedPixelTrait)
578 if (bytes_per_line == 0)
579 bytes_per_line=bytes_per_pixel*image->columns;
580 for (y=0; y < (ssize_t) image->rows; y++)
582 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
583 if (q == (Quantum *) NULL)
585 for (x=0; x < (ssize_t) image->columns; x++)
587 if (image->alpha_trait != UndefinedPixelTrait)
588 SetPixelAlpha(image,ScaleCharToQuantum(*p++),q);
589 if (sun_info.type == RT_STANDARD)
591 SetPixelBlue(image,ScaleCharToQuantum(*p++),q);
592 SetPixelGreen(image,ScaleCharToQuantum(*p++),q);
593 SetPixelRed(image,ScaleCharToQuantum(*p++),q);
597 SetPixelRed(image,ScaleCharToQuantum(*p++),q);
598 SetPixelGreen(image,ScaleCharToQuantum(*p++),q);
599 SetPixelBlue(image,ScaleCharToQuantum(*p++),q);
601 if (image->colors != 0)
603 SetPixelRed(image,ClampToQuantum(image->colormap[(ssize_t)
604 GetPixelRed(image,q)].red),q);
605 SetPixelGreen(image,ClampToQuantum(image->colormap[(ssize_t)
606 GetPixelGreen(image,q)].green),q);
607 SetPixelBlue(image,ClampToQuantum(image->colormap[(ssize_t)
608 GetPixelBlue(image,q)].blue),q);
610 q+=GetPixelChannels(image);
612 if (((bytes_per_pixel*image->columns) % 2) != 0)
614 if (SyncAuthenticPixels(image,exception) == MagickFalse)
616 if (image->previous == (Image *) NULL)
618 status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y,
620 if (status == MagickFalse)
625 if (image->storage_class == PseudoClass)
626 (void) SyncImage(image,exception);
627 sun_pixels=(unsigned char *) RelinquishMagickMemory(sun_pixels);
628 if (EOFBlob(image) != MagickFalse)
630 ThrowFileException(exception,CorruptImageError,"UnexpectedEndOfFile",
635 Proceed to next image.
637 if (image_info->number_scenes != 0)
638 if (image->scene >= (image_info->scene+image_info->number_scenes-1))
640 sun_info.magic=ReadBlobMSBLong(image);
641 if (sun_info.magic == 0x59a66a95)
644 Allocate next image structure.
646 AcquireNextImage(image_info,image,exception);
647 if (GetNextImageInList(image) == (Image *) NULL)
649 image=DestroyImageList(image);
650 return((Image *) NULL);
652 image=SyncNextImageInList(image);
653 status=SetImageProgress(image,LoadImagesTag,TellBlob(image),
655 if (status == MagickFalse)
658 } while (sun_info.magic == 0x59a66a95);
659 (void) CloseBlob(image);
660 return(GetFirstImageInList(image));
664 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
668 % R e g i s t e r S U N I m a g e %
672 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
674 % RegisterSUNImage() adds attributes for the SUN image format to
675 % the list of supported formats. The attributes include the image format
676 % tag, a method to read and/or write the format, whether the format
677 % supports the saving of more than one frame to the same file or blob,
678 % whether the format supports native in-memory I/O, and a brief
679 % description of the format.
681 % The format of the RegisterSUNImage method is:
683 % size_t RegisterSUNImage(void)
686 ModuleExport size_t RegisterSUNImage(void)
691 entry=AcquireMagickInfo("SUN","RAS","SUN Rasterfile");
692 entry->decoder=(DecodeImageHandler *) ReadSUNImage;
693 entry->encoder=(EncodeImageHandler *) WriteSUNImage;
694 entry->magick=(IsImageFormatHandler *) IsSUN;
695 entry->flags|=CoderDecoderSeekableStreamFlag;
696 (void) RegisterMagickInfo(entry);
697 entry=AcquireMagickInfo("SUN","SUN","SUN Rasterfile");
698 entry->decoder=(DecodeImageHandler *) ReadSUNImage;
699 entry->encoder=(EncodeImageHandler *) WriteSUNImage;
700 entry->flags|=CoderDecoderSeekableStreamFlag;
701 (void) RegisterMagickInfo(entry);
702 return(MagickImageCoderSignature);
706 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
710 % U n r e g i s t e r S U N I m a g e %
714 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
716 % UnregisterSUNImage() removes format registrations made by the
717 % SUN module from the list of supported formats.
719 % The format of the UnregisterSUNImage method is:
721 % UnregisterSUNImage(void)
724 ModuleExport void UnregisterSUNImage(void)
726 (void) UnregisterMagickInfo("RAS");
727 (void) UnregisterMagickInfo("SUN");
731 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
735 % W r i t e S U N I m a g e %
739 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
741 % WriteSUNImage() writes an image in the SUN rasterfile format.
743 % The format of the WriteSUNImage method is:
745 % MagickBooleanType WriteSUNImage(const ImageInfo *image_info,
746 % Image *image,ExceptionInfo *exception)
748 % A description of each parameter follows.
750 % o image_info: the image info.
752 % o image: The image.
754 % o exception: return any errors or warnings in this structure.
757 static MagickBooleanType WriteSUNImage(const ImageInfo *image_info,Image *image,
758 ExceptionInfo *exception)
760 #define RMT_EQUAL_RGB 1
763 #define RT_STANDARD 1
764 #define RT_FORMAT_RGB 3
766 typedef struct _SUNInfo
788 register const Quantum
802 Open output image file.
804 assert(image_info != (const ImageInfo *) NULL);
805 assert(image_info->signature == MagickCoreSignature);
806 assert(image != (Image *) NULL);
807 assert(image->signature == MagickCoreSignature);
808 if (image->debug != MagickFalse)
809 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
810 assert(exception != (ExceptionInfo *) NULL);
811 assert(exception->signature == MagickCoreSignature);
812 status=OpenBlob(image_info,image,WriteBinaryBlobMode,exception);
813 if (status == MagickFalse)
819 Initialize SUN raster file header.
821 (void) TransformImageColorspace(image,sRGBColorspace,exception);
822 sun_info.magic=0x59a66a95;
823 if ((image->columns != (unsigned int) image->columns) ||
824 (image->rows != (unsigned int) image->rows))
825 ThrowWriterException(ImageError,"WidthOrHeightExceedsLimit");
826 sun_info.width=(unsigned int) image->columns;
827 sun_info.height=(unsigned int) image->rows;
828 sun_info.type=(unsigned int)
829 (image->storage_class == DirectClass ? RT_FORMAT_RGB : RT_STANDARD);
830 sun_info.maptype=RMT_NONE;
831 sun_info.maplength=0;
832 number_pixels=(MagickSizeType) image->columns*image->rows;
833 if ((4*number_pixels) != (size_t) (4*number_pixels))
834 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
835 if (image->storage_class == DirectClass)
838 Full color SUN raster.
840 sun_info.depth=(unsigned int) image->alpha_trait !=
841 UndefinedPixelTrait ? 32U : 24U;
842 sun_info.length=(unsigned int) ((image->alpha_trait !=
843 UndefinedPixelTrait ? 4 : 3)*number_pixels);
844 sun_info.length+=sun_info.length & 0x01 ? (unsigned int) image->rows :
848 if (SetImageMonochrome(image,exception) != MagickFalse)
851 Monochrome SUN raster.
854 sun_info.length=(unsigned int) (((image->columns+7) >> 3)*
856 sun_info.length+=(unsigned int) (((image->columns/8)+(image->columns %
857 8 ? 1 : 0)) % 2 ? image->rows : 0);
862 Colormapped SUN raster.
865 sun_info.length=(unsigned int) number_pixels;
866 sun_info.length+=(unsigned int) (image->columns & 0x01 ? image->rows :
868 sun_info.maptype=RMT_EQUAL_RGB;
869 sun_info.maplength=(unsigned int) (3*image->colors);
874 (void) WriteBlobMSBLong(image,sun_info.magic);
875 (void) WriteBlobMSBLong(image,sun_info.width);
876 (void) WriteBlobMSBLong(image,sun_info.height);
877 (void) WriteBlobMSBLong(image,sun_info.depth);
878 (void) WriteBlobMSBLong(image,sun_info.length);
879 (void) WriteBlobMSBLong(image,sun_info.type);
880 (void) WriteBlobMSBLong(image,sun_info.maptype);
881 (void) WriteBlobMSBLong(image,sun_info.maplength);
883 Convert MIFF to SUN raster pixels.
887 if (image->storage_class == DirectClass)
889 register unsigned char
900 Allocate memory for pixels.
903 if (image->alpha_trait != UndefinedPixelTrait)
905 length=image->columns;
906 pixels=(unsigned char *) AcquireQuantumMemory(length,4*sizeof(*pixels));
907 if (pixels == (unsigned char *) NULL)
908 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
910 Convert DirectClass packet to SUN RGB pixel.
912 for (y=0; y < (ssize_t) image->rows; y++)
914 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
915 if (p == (const Quantum *) NULL)
918 for (x=0; x < (ssize_t) image->columns; x++)
920 if (image->alpha_trait != UndefinedPixelTrait)
921 *q++=ScaleQuantumToChar(GetPixelAlpha(image,p));
922 *q++=ScaleQuantumToChar(GetPixelRed(image,p));
923 *q++=ScaleQuantumToChar(GetPixelGreen(image,p));
924 *q++=ScaleQuantumToChar(GetPixelBlue(image,p));
925 p+=GetPixelChannels(image);
927 if (((bytes_per_pixel*image->columns) & 0x01) != 0)
928 *q++='\0'; /* pad scanline */
929 (void) WriteBlob(image,(size_t) (q-pixels),pixels);
930 if (image->previous == (Image *) NULL)
932 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
934 if (status == MagickFalse)
938 pixels=(unsigned char *) RelinquishMagickMemory(pixels);
941 if (SetImageMonochrome(image,exception) != MagickFalse)
943 register unsigned char
948 Convert PseudoClass image to a SUN monochrome image.
950 (void) SetImageType(image,BilevelType,exception);
951 for (y=0; y < (ssize_t) image->rows; y++)
953 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
954 if (p == (const Quantum *) NULL)
958 for (x=0; x < (ssize_t) image->columns; x++)
961 if (GetPixelLuma(image,p) < (QuantumRange/2.0))
966 (void) WriteBlobByte(image,byte);
970 p+=GetPixelChannels(image);
973 (void) WriteBlobByte(image,(unsigned char) (byte << (8-bit)));
974 if ((((image->columns/8)+
975 (image->columns % 8 ? 1 : 0)) % 2) != 0)
976 (void) WriteBlobByte(image,0); /* pad scanline */
977 if (image->previous == (Image *) NULL)
979 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
981 if (status == MagickFalse)
989 Dump colormap to file.
991 for (i=0; i < (ssize_t) image->colors; i++)
992 (void) WriteBlobByte(image,ScaleQuantumToChar(
993 ClampToQuantum(image->colormap[i].red)));
994 for (i=0; i < (ssize_t) image->colors; i++)
995 (void) WriteBlobByte(image,ScaleQuantumToChar(
996 ClampToQuantum(image->colormap[i].green)));
997 for (i=0; i < (ssize_t) image->colors; i++)
998 (void) WriteBlobByte(image,ScaleQuantumToChar(
999 ClampToQuantum(image->colormap[i].blue)));
1001 Convert PseudoClass packet to SUN colormapped pixel.
1003 for (y=0; y < (ssize_t) image->rows; y++)
1005 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
1006 if (p == (const Quantum *) NULL)
1008 for (x=0; x < (ssize_t) image->columns; x++)
1010 (void) WriteBlobByte(image,(unsigned char)
1011 GetPixelIndex(image,p));
1012 p+=GetPixelChannels(image);
1014 if (image->columns & 0x01)
1015 (void) WriteBlobByte(image,0); /* pad scanline */
1016 if (image->previous == (Image *) NULL)
1018 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
1020 if (status == MagickFalse)
1025 if (GetNextImageInList(image) == (Image *) NULL)
1027 image=SyncNextImageInList(image);
1028 status=SetImageProgress(image,SaveImagesTag,scene++,
1029 GetImageListLength(image));
1030 if (status == MagickFalse)
1032 } while (image_info->adjoin != MagickFalse);
1033 (void) CloseBlob(image);