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 % 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/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 image->colors=sun_info.maplength;
341 if (sun_info.maptype == RMT_NONE)
342 image->colors=one << sun_info.depth;
343 if (sun_info.maptype == RMT_EQUAL_RGB)
344 image->colors=sun_info.maplength/3;
345 if (AcquireImageColormap(image,image->colors,exception) == MagickFalse)
346 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
348 switch (sun_info.maptype)
358 Read SUN raster colormap.
360 sun_colormap=(unsigned char *) AcquireQuantumMemory(image->colors,
361 sizeof(*sun_colormap));
362 if (sun_colormap == (unsigned char *) NULL)
363 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
364 count=ReadBlob(image,image->colors,sun_colormap);
365 if (count != (ssize_t) image->colors)
366 ThrowReaderException(CorruptImageError,"UnexpectedEndOfFile");
367 for (i=0; i < (ssize_t) image->colors; i++)
368 image->colormap[i].red=(MagickRealType) ScaleCharToQuantum(
370 count=ReadBlob(image,image->colors,sun_colormap);
371 if (count != (ssize_t) image->colors)
372 ThrowReaderException(CorruptImageError,"UnexpectedEndOfFile");
373 for (i=0; i < (ssize_t) image->colors; i++)
374 image->colormap[i].green=(MagickRealType) ScaleCharToQuantum(
376 count=ReadBlob(image,image->colors,sun_colormap);
377 if (count != (ssize_t) image->colors)
378 ThrowReaderException(CorruptImageError,"UnexpectedEndOfFile");
379 for (i=0; i < (ssize_t) image->colors; i++)
380 image->colormap[i].blue=(MagickRealType) ScaleCharToQuantum(
382 sun_colormap=(unsigned char *) RelinquishMagickMemory(sun_colormap);
391 Read SUN raster colormap.
393 sun_colormap=(unsigned char *) AcquireQuantumMemory(sun_info.maplength,
394 sizeof(*sun_colormap));
395 if (sun_colormap == (unsigned char *) NULL)
396 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
397 count=ReadBlob(image,sun_info.maplength,sun_colormap);
398 if (count != (ssize_t) sun_info.maplength)
399 ThrowReaderException(CorruptImageError,"UnexpectedEndOfFile");
400 sun_colormap=(unsigned char *) RelinquishMagickMemory(sun_colormap);
404 ThrowReaderException(CoderError,"ColormapTypeNotSupported");
406 image->alpha_trait=sun_info.depth == 32 ? BlendPixelTrait :
408 image->columns=sun_info.width;
409 image->rows=sun_info.height;
410 if (image_info->ping != MagickFalse)
412 (void) CloseBlob(image);
413 return(GetFirstImageInList(image));
415 status=SetImageExtent(image,image->columns,image->rows,exception);
416 if (status == MagickFalse)
417 return(DestroyImageList(image));
418 if (sun_info.length == 0)
419 ThrowReaderException(ResourceLimitError,"ImproperImageHeader");
420 number_pixels=(MagickSizeType) (image->columns*image->rows);
421 if ((sun_info.type != RT_ENCODED) &&
422 ((number_pixels*sun_info.depth) > (8UL*sun_info.length)))
423 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
424 if (HeapOverflowSanityCheck(sun_info.width,sun_info.depth) != MagickFalse)
425 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
426 bytes_per_line=sun_info.width*sun_info.depth;
427 sun_data=(unsigned char *) AcquireQuantumMemory(sun_info.length,
429 if (sun_data == (unsigned char *) NULL)
430 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
431 count=(ssize_t) ReadBlob(image,sun_info.length,sun_data);
432 if (count != (ssize_t) sun_info.length)
434 sun_data=(unsigned char *) RelinquishMagickMemory(sun_data);
435 ThrowReaderException(CorruptImageError,"UnableToReadImageData");
437 height=sun_info.height;
438 if ((height == 0) || (sun_info.width == 0) || (sun_info.depth == 0) ||
439 ((bytes_per_line/sun_info.depth) != sun_info.width))
441 sun_data=(unsigned char *) RelinquishMagickMemory(sun_data);
442 ThrowReaderException(ResourceLimitError,"ImproperImageHeader");
444 quantum=sun_info.depth == 1 ? 15 : 7;
445 bytes_per_line+=quantum;
447 if ((bytes_per_line >> 1) != (sun_info.width*sun_info.depth+quantum))
449 sun_data=(unsigned char *) RelinquishMagickMemory(sun_data);
450 ThrowReaderException(ResourceLimitError,"ImproperImageHeader");
453 if (HeapOverflowSanityCheck(height,bytes_per_line) != MagickFalse)
455 sun_data=(unsigned char *) RelinquishMagickMemory(sun_data);
456 ThrowReaderException(ResourceLimitError,"ImproperImageHeader");
458 pixels_length=height*bytes_per_line;
459 sun_pixels=(unsigned char *) AcquireQuantumMemory(pixels_length+image->rows,
460 sizeof(*sun_pixels));
461 if (sun_pixels == (unsigned char *) NULL)
463 sun_data=(unsigned char *) RelinquishMagickMemory(sun_data);
464 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
466 ResetMagickMemory(sun_pixels,0,pixels_length*sizeof(*sun_pixels));
467 if (sun_info.type == RT_ENCODED)
469 status=DecodeImage(sun_data,sun_info.length,sun_pixels,pixels_length);
470 if (status == MagickFalse)
471 ThrowReaderException(CorruptImageError,"UnableToReadImageData");
475 if (sun_info.length > pixels_length)
477 sun_data=(unsigned char *) RelinquishMagickMemory(sun_data);
478 sun_pixels=(unsigned char *) RelinquishMagickMemory(sun_pixels);
479 ThrowReaderException(ResourceLimitError,"ImproperImageHeader");
481 (void) CopyMagickMemory(sun_pixels,sun_data,sun_info.length);
483 sun_data=(unsigned char *) RelinquishMagickMemory(sun_data);
485 Convert SUN raster image to pixel packets.
488 if (sun_info.depth == 1)
489 for (y=0; y < (ssize_t) image->rows; y++)
491 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
492 if (q == (Quantum *) NULL)
494 for (x=0; x < ((ssize_t) image->columns-7); x+=8)
496 for (bit=7; bit >= 0; bit--)
498 SetPixelIndex(image,(Quantum) ((*p) & (0x01 << bit) ? 0x00 : 0x01),
500 q+=GetPixelChannels(image);
504 if ((image->columns % 8) != 0)
506 for (bit=7; bit >= (int) (8-(image->columns % 8)); bit--)
508 SetPixelIndex(image,(Quantum) ((*p) & (0x01 << bit) ? 0x00 :
510 q+=GetPixelChannels(image);
514 if ((((image->columns/8)+(image->columns % 8 ? 1 : 0)) % 2) != 0)
516 if (SyncAuthenticPixels(image,exception) == MagickFalse)
518 if (image->previous == (Image *) NULL)
520 status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y,
522 if (status == MagickFalse)
527 if (image->storage_class == PseudoClass)
529 for (y=0; y < (ssize_t) image->rows; y++)
531 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
532 if (q == (Quantum *) NULL)
534 for (x=0; x < (ssize_t) image->columns; x++)
536 SetPixelIndex(image,ConstrainColormapIndex(image,*p,exception),q);
538 q+=GetPixelChannels(image);
540 if ((image->columns % 2) != 0)
542 if (SyncAuthenticPixels(image,exception) == MagickFalse)
544 if (image->previous == (Image *) NULL)
546 status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y,
548 if (status == MagickFalse)
559 if (image->alpha_trait != UndefinedPixelTrait)
561 if (bytes_per_line == 0)
562 bytes_per_line=bytes_per_pixel*image->columns;
563 for (y=0; y < (ssize_t) image->rows; y++)
565 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
566 if (q == (Quantum *) NULL)
568 for (x=0; x < (ssize_t) image->columns; x++)
570 if (image->alpha_trait != UndefinedPixelTrait)
571 SetPixelAlpha(image,ScaleCharToQuantum(*p++),q);
572 if (sun_info.type == RT_STANDARD)
574 SetPixelBlue(image,ScaleCharToQuantum(*p++),q);
575 SetPixelGreen(image,ScaleCharToQuantum(*p++),q);
576 SetPixelRed(image,ScaleCharToQuantum(*p++),q);
580 SetPixelRed(image,ScaleCharToQuantum(*p++),q);
581 SetPixelGreen(image,ScaleCharToQuantum(*p++),q);
582 SetPixelBlue(image,ScaleCharToQuantum(*p++),q);
584 if (image->colors != 0)
586 SetPixelRed(image,ClampToQuantum(image->colormap[(ssize_t)
587 GetPixelRed(image,q)].red),q);
588 SetPixelGreen(image,ClampToQuantum(image->colormap[(ssize_t)
589 GetPixelGreen(image,q)].green),q);
590 SetPixelBlue(image,ClampToQuantum(image->colormap[(ssize_t)
591 GetPixelBlue(image,q)].blue),q);
593 q+=GetPixelChannels(image);
595 if (((bytes_per_pixel*image->columns) % 2) != 0)
597 if (SyncAuthenticPixels(image,exception) == MagickFalse)
599 if (image->previous == (Image *) NULL)
601 status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y,
603 if (status == MagickFalse)
608 if (image->storage_class == PseudoClass)
609 (void) SyncImage(image,exception);
610 sun_pixels=(unsigned char *) RelinquishMagickMemory(sun_pixels);
611 if (EOFBlob(image) != MagickFalse)
613 ThrowFileException(exception,CorruptImageError,"UnexpectedEndOfFile",
618 Proceed to next image.
620 if (image_info->number_scenes != 0)
621 if (image->scene >= (image_info->scene+image_info->number_scenes-1))
623 sun_info.magic=ReadBlobMSBLong(image);
624 if (sun_info.magic == 0x59a66a95)
627 Allocate next image structure.
629 AcquireNextImage(image_info,image,exception);
630 if (GetNextImageInList(image) == (Image *) NULL)
632 image=DestroyImageList(image);
633 return((Image *) NULL);
635 image=SyncNextImageInList(image);
636 status=SetImageProgress(image,LoadImagesTag,TellBlob(image),
638 if (status == MagickFalse)
641 } while (sun_info.magic == 0x59a66a95);
642 (void) CloseBlob(image);
643 return(GetFirstImageInList(image));
647 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
651 % R e g i s t e r S U N I m a g e %
655 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
657 % RegisterSUNImage() adds attributes for the SUN image format to
658 % the list of supported formats. The attributes include the image format
659 % tag, a method to read and/or write the format, whether the format
660 % supports the saving of more than one frame to the same file or blob,
661 % whether the format supports native in-memory I/O, and a brief
662 % description of the format.
664 % The format of the RegisterSUNImage method is:
666 % size_t RegisterSUNImage(void)
669 ModuleExport size_t RegisterSUNImage(void)
674 entry=AcquireMagickInfo("SUN","RAS","SUN Rasterfile");
675 entry->decoder=(DecodeImageHandler *) ReadSUNImage;
676 entry->encoder=(EncodeImageHandler *) WriteSUNImage;
677 entry->magick=(IsImageFormatHandler *) IsSUN;
678 (void) RegisterMagickInfo(entry);
679 entry=AcquireMagickInfo("SUN","SUN","SUN Rasterfile");
680 entry->decoder=(DecodeImageHandler *) ReadSUNImage;
681 entry->encoder=(EncodeImageHandler *) WriteSUNImage;
682 (void) RegisterMagickInfo(entry);
683 return(MagickImageCoderSignature);
687 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
691 % U n r e g i s t e r S U N I m a g e %
695 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
697 % UnregisterSUNImage() removes format registrations made by the
698 % SUN module from the list of supported formats.
700 % The format of the UnregisterSUNImage method is:
702 % UnregisterSUNImage(void)
705 ModuleExport void UnregisterSUNImage(void)
707 (void) UnregisterMagickInfo("RAS");
708 (void) UnregisterMagickInfo("SUN");
712 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
716 % W r i t e S U N I m a g e %
720 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
722 % WriteSUNImage() writes an image in the SUN rasterfile format.
724 % The format of the WriteSUNImage method is:
726 % MagickBooleanType WriteSUNImage(const ImageInfo *image_info,
727 % Image *image,ExceptionInfo *exception)
729 % A description of each parameter follows.
731 % o image_info: the image info.
733 % o image: The image.
735 % o exception: return any errors or warnings in this structure.
738 static MagickBooleanType WriteSUNImage(const ImageInfo *image_info,Image *image,
739 ExceptionInfo *exception)
741 #define RMT_EQUAL_RGB 1
744 #define RT_STANDARD 1
745 #define RT_FORMAT_RGB 3
747 typedef struct _SUNInfo
769 register const Quantum
783 Open output image file.
785 assert(image_info != (const ImageInfo *) NULL);
786 assert(image_info->signature == MagickCoreSignature);
787 assert(image != (Image *) NULL);
788 assert(image->signature == MagickCoreSignature);
789 if (image->debug != MagickFalse)
790 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
791 assert(exception != (ExceptionInfo *) NULL);
792 assert(exception->signature == MagickCoreSignature);
793 status=OpenBlob(image_info,image,WriteBinaryBlobMode,exception);
794 if (status == MagickFalse)
800 Initialize SUN raster file header.
802 (void) TransformImageColorspace(image,sRGBColorspace,exception);
803 sun_info.magic=0x59a66a95;
804 if ((image->columns != (unsigned int) image->columns) ||
805 (image->rows != (unsigned int) image->rows))
806 ThrowWriterException(ImageError,"WidthOrHeightExceedsLimit");
807 sun_info.width=(unsigned int) image->columns;
808 sun_info.height=(unsigned int) image->rows;
809 sun_info.type=(unsigned int)
810 (image->storage_class == DirectClass ? RT_FORMAT_RGB : RT_STANDARD);
811 sun_info.maptype=RMT_NONE;
812 sun_info.maplength=0;
813 number_pixels=(MagickSizeType) image->columns*image->rows;
814 if ((4*number_pixels) != (size_t) (4*number_pixels))
815 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
816 if (image->storage_class == DirectClass)
819 Full color SUN raster.
821 sun_info.depth=(unsigned int) image->alpha_trait !=
822 UndefinedPixelTrait ? 32U : 24U;
823 sun_info.length=(unsigned int) ((image->alpha_trait !=
824 UndefinedPixelTrait ? 4 : 3)*number_pixels);
825 sun_info.length+=sun_info.length & 0x01 ? (unsigned int) image->rows :
829 if (SetImageMonochrome(image,exception) != MagickFalse)
832 Monochrome SUN raster.
835 sun_info.length=(unsigned int) (((image->columns+7) >> 3)*
837 sun_info.length+=(unsigned int) (((image->columns/8)+(image->columns %
838 8 ? 1 : 0)) % 2 ? image->rows : 0);
843 Colormapped SUN raster.
846 sun_info.length=(unsigned int) number_pixels;
847 sun_info.length+=(unsigned int) (image->columns & 0x01 ? image->rows :
849 sun_info.maptype=RMT_EQUAL_RGB;
850 sun_info.maplength=(unsigned int) (3*image->colors);
855 (void) WriteBlobMSBLong(image,sun_info.magic);
856 (void) WriteBlobMSBLong(image,sun_info.width);
857 (void) WriteBlobMSBLong(image,sun_info.height);
858 (void) WriteBlobMSBLong(image,sun_info.depth);
859 (void) WriteBlobMSBLong(image,sun_info.length);
860 (void) WriteBlobMSBLong(image,sun_info.type);
861 (void) WriteBlobMSBLong(image,sun_info.maptype);
862 (void) WriteBlobMSBLong(image,sun_info.maplength);
864 Convert MIFF to SUN raster pixels.
868 if (image->storage_class == DirectClass)
870 register unsigned char
881 Allocate memory for pixels.
884 if (image->alpha_trait != UndefinedPixelTrait)
886 length=image->columns;
887 pixels=(unsigned char *) AcquireQuantumMemory(length,4*sizeof(*pixels));
888 if (pixels == (unsigned char *) NULL)
889 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
891 Convert DirectClass packet to SUN RGB pixel.
893 for (y=0; y < (ssize_t) image->rows; y++)
895 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
896 if (p == (const Quantum *) NULL)
899 for (x=0; x < (ssize_t) image->columns; x++)
901 if (image->alpha_trait != UndefinedPixelTrait)
902 *q++=ScaleQuantumToChar(GetPixelAlpha(image,p));
903 *q++=ScaleQuantumToChar(GetPixelRed(image,p));
904 *q++=ScaleQuantumToChar(GetPixelGreen(image,p));
905 *q++=ScaleQuantumToChar(GetPixelBlue(image,p));
906 p+=GetPixelChannels(image);
908 if (((bytes_per_pixel*image->columns) & 0x01) != 0)
909 *q++='\0'; /* pad scanline */
910 (void) WriteBlob(image,(size_t) (q-pixels),pixels);
911 if (image->previous == (Image *) NULL)
913 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
915 if (status == MagickFalse)
919 pixels=(unsigned char *) RelinquishMagickMemory(pixels);
922 if (SetImageMonochrome(image,exception) != MagickFalse)
924 register unsigned char
929 Convert PseudoClass image to a SUN monochrome image.
931 (void) SetImageType(image,BilevelType,exception);
932 for (y=0; y < (ssize_t) image->rows; y++)
934 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
935 if (p == (const Quantum *) NULL)
939 for (x=0; x < (ssize_t) image->columns; x++)
942 if (GetPixelLuma(image,p) < (QuantumRange/2.0))
947 (void) WriteBlobByte(image,byte);
951 p+=GetPixelChannels(image);
954 (void) WriteBlobByte(image,(unsigned char) (byte << (8-bit)));
955 if ((((image->columns/8)+
956 (image->columns % 8 ? 1 : 0)) % 2) != 0)
957 (void) WriteBlobByte(image,0); /* pad scanline */
958 if (image->previous == (Image *) NULL)
960 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
962 if (status == MagickFalse)
970 Dump colormap to file.
972 for (i=0; i < (ssize_t) image->colors; i++)
973 (void) WriteBlobByte(image,ScaleQuantumToChar(
974 ClampToQuantum(image->colormap[i].red)));
975 for (i=0; i < (ssize_t) image->colors; i++)
976 (void) WriteBlobByte(image,ScaleQuantumToChar(
977 ClampToQuantum(image->colormap[i].green)));
978 for (i=0; i < (ssize_t) image->colors; i++)
979 (void) WriteBlobByte(image,ScaleQuantumToChar(
980 ClampToQuantum(image->colormap[i].blue)));
982 Convert PseudoClass packet to SUN colormapped pixel.
984 for (y=0; y < (ssize_t) image->rows; y++)
986 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
987 if (p == (const Quantum *) NULL)
989 for (x=0; x < (ssize_t) image->columns; x++)
991 (void) WriteBlobByte(image,(unsigned char)
992 GetPixelIndex(image,p));
993 p+=GetPixelChannels(image);
995 if (image->columns & 0x01)
996 (void) WriteBlobByte(image,0); /* pad scanline */
997 if (image->previous == (Image *) NULL)
999 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
1001 if (status == MagickFalse)
1006 if (GetNextImageInList(image) == (Image *) NULL)
1008 image=SyncNextImageInList(image);
1009 status=SetImageProgress(image,SaveImagesTag,scene++,
1010 GetImageListLength(image));
1011 if (status == MagickFalse)
1013 } while (image_info->adjoin != MagickFalse);
1014 (void) CloseBlob(image);