2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
13 % Read/Write Sun Rasterfile Image Format %
20 % Copyright 1999-2015 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/monitor.h"
61 #include "MagickCore/monitor-private.h"
62 #include "MagickCore/pixel-accessor.h"
63 #include "MagickCore/quantum-private.h"
64 #include "MagickCore/static.h"
65 #include "MagickCore/string_.h"
66 #include "MagickCore/module.h"
71 static MagickBooleanType
72 WriteSUNImage(const ImageInfo *,Image *,ExceptionInfo *);
75 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
83 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
85 % IsSUN() returns MagickTrue if the image format type, identified by the
86 % magick string, is SUN.
88 % The format of the IsSUN method is:
90 % MagickBooleanType IsSUN(const unsigned char *magick,const size_t length)
92 % A description of each parameter follows:
94 % o magick: compare image format pattern against these bytes.
96 % o length: Specifies the length of the magick string.
99 static MagickBooleanType IsSUN(const unsigned char *magick,const size_t length)
103 if (memcmp(magick,"\131\246\152\225",4) == 0)
109 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
113 % D e c o d e I m a g e %
117 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
119 % DecodeImage unpacks the packed image pixels into runlength-encoded pixel
122 % The format of the DecodeImage method is:
124 % MagickBooleanType DecodeImage(const unsigned char *compressed_pixels,
125 % const size_t length,unsigned char *pixels)
127 % A description of each parameter follows:
129 % o compressed_pixels: The address of a byte (8 bits) array of compressed
132 % o length: An integer value that is the total number of bytes of the
133 % source image (as just read by ReadBlob)
135 % o pixels: The address of a byte (8 bits) array of pixel data created by
136 % the uncompression process. The number of bytes in this array
137 % must be at least equal to the number columns times the number of rows
138 % of the source pixels.
141 static MagickBooleanType DecodeImage(const unsigned char *compressed_pixels,
142 const size_t length,unsigned char *pixels,size_t maxpixels)
144 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);
163 while (((size_t) (p-compressed_pixels) < length) && (q < l))
171 Runlength-encoded packet: <count><byte>
173 count=(ssize_t) (*p++);
176 while ((count >= 0) && (q < l))
187 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
191 % R e a d S U N I m a g e %
195 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
197 % ReadSUNImage() reads a SUN image file and returns it. It allocates
198 % the memory necessary for the new Image structure and returns a pointer to
201 % The format of the ReadSUNImage method is:
203 % Image *ReadSUNImage(const ImageInfo *image_info,ExceptionInfo *exception)
205 % A description of each parameter follows:
207 % o image_info: the image info.
209 % o exception: return any errors or warnings in this structure.
212 static Image *ReadSUNImage(const ImageInfo *image_info,ExceptionInfo *exception)
214 #define RMT_EQUAL_RGB 1
217 #define RT_STANDARD 1
219 #define RT_FORMAT_RGB 3
221 typedef struct _SUNInfo
253 register unsigned char
275 assert(image_info != (const ImageInfo *) NULL);
276 assert(image_info->signature == MagickSignature);
277 if (image_info->debug != MagickFalse)
278 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
279 image_info->filename);
280 assert(exception != (ExceptionInfo *) NULL);
281 assert(exception->signature == MagickSignature);
282 image=AcquireImage(image_info,exception);
283 status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception);
284 if (status == MagickFalse)
286 image=DestroyImageList(image);
287 return((Image *) NULL);
290 Read SUN raster header.
292 (void) ResetMagickMemory(&sun_info,0,sizeof(sun_info));
293 sun_info.magic=ReadBlobMSBLong(image);
297 Verify SUN identifier.
299 if (sun_info.magic != 0x59a66a95)
300 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
301 sun_info.width=ReadBlobMSBLong(image);
302 sun_info.height=ReadBlobMSBLong(image);
303 sun_info.depth=ReadBlobMSBLong(image);
304 sun_info.length=ReadBlobMSBLong(image);
305 sun_info.type=ReadBlobMSBLong(image);
306 sun_info.maptype=ReadBlobMSBLong(image);
307 sun_info.maplength=ReadBlobMSBLong(image);
308 extent=sun_info.height*sun_info.width;
309 if ((sun_info.height != 0) && (sun_info.width != extent/sun_info.height))
310 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
311 if ((sun_info.type != RT_STANDARD) && (sun_info.type != RT_ENCODED) &&
312 (sun_info.type != RT_FORMAT_RGB))
313 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
314 if ((sun_info.maptype == RMT_NONE) && (sun_info.maplength != 0))
315 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
316 if ((sun_info.depth == 0) || (sun_info.depth > 32))
317 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
318 if ((sun_info.maptype != RMT_NONE) && (sun_info.maptype != RMT_EQUAL_RGB) &&
319 (sun_info.maptype != RMT_RAW))
320 ThrowReaderException(CoderError,"ColormapTypeNotSupported");
321 image->columns=sun_info.width;
322 image->rows=sun_info.height;
323 image->depth=sun_info.depth <= 8 ? sun_info.depth :
324 MAGICKCORE_QUANTUM_DEPTH;
325 if (sun_info.depth < 24)
330 image->colors=sun_info.maplength;
332 if (sun_info.maptype == RMT_NONE)
333 image->colors=one << sun_info.depth;
334 if (sun_info.maptype == RMT_EQUAL_RGB)
335 image->colors=sun_info.maplength/3;
336 if (AcquireImageColormap(image,image->colors,exception) == MagickFalse)
337 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
339 switch (sun_info.maptype)
349 Read SUN raster colormap.
351 sun_colormap=(unsigned char *) AcquireQuantumMemory(image->colors,
352 sizeof(*sun_colormap));
353 if (sun_colormap == (unsigned char *) NULL)
354 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
355 count=ReadBlob(image,image->colors,sun_colormap);
356 if (count != (ssize_t) image->colors)
357 ThrowReaderException(CorruptImageError,"UnexpectedEndOfFile");
358 for (i=0; i < (ssize_t) image->colors; i++)
359 image->colormap[i].red=(MagickRealType) ScaleCharToQuantum(
361 count=ReadBlob(image,image->colors,sun_colormap);
362 if (count != (ssize_t) image->colors)
363 ThrowReaderException(CorruptImageError,"UnexpectedEndOfFile");
364 for (i=0; i < (ssize_t) image->colors; i++)
365 image->colormap[i].green=(MagickRealType) ScaleCharToQuantum(
367 count=ReadBlob(image,image->colors,sun_colormap);
368 if (count != (ssize_t) image->colors)
369 ThrowReaderException(CorruptImageError,"UnexpectedEndOfFile");
370 for (i=0; i < (ssize_t) image->colors; i++)
371 image->colormap[i].blue=(MagickRealType) ScaleCharToQuantum(
373 sun_colormap=(unsigned char *) RelinquishMagickMemory(sun_colormap);
382 Read SUN raster colormap.
384 sun_colormap=(unsigned char *) AcquireQuantumMemory(sun_info.maplength,
385 sizeof(*sun_colormap));
386 if (sun_colormap == (unsigned char *) NULL)
387 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
388 count=ReadBlob(image,sun_info.maplength,sun_colormap);
389 if (count != (ssize_t) sun_info.maplength)
390 ThrowReaderException(CorruptImageError,"UnexpectedEndOfFile");
391 sun_colormap=(unsigned char *) RelinquishMagickMemory(sun_colormap);
395 ThrowReaderException(CoderError,"ColormapTypeNotSupported");
397 image->alpha_trait=sun_info.depth == 32 ? BlendPixelTrait :
399 image->columns=sun_info.width;
400 image->rows=sun_info.height;
401 if (image_info->ping != MagickFalse)
403 (void) CloseBlob(image);
404 return(GetFirstImageInList(image));
406 status=SetImageExtent(image,image->columns,image->rows,exception);
407 if (status == MagickFalse)
408 return(DestroyImageList(image));
409 if ((sun_info.length*sizeof(*sun_data))/sizeof(*sun_data) !=
410 sun_info.length || !sun_info.length)
411 ThrowReaderException(ResourceLimitError,"ImproperImageHeader");
412 number_pixels=(MagickSizeType) (image->columns*image->rows);
413 if ((sun_info.type != RT_ENCODED) &&
414 ((number_pixels*sun_info.depth) > (8UL*sun_info.length)))
415 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
416 bytes_per_line=sun_info.width*sun_info.depth;
417 sun_data=(unsigned char *) AcquireQuantumMemory((size_t) MagickMax(
418 sun_info.length,bytes_per_line*sun_info.width),sizeof(*sun_data));
419 if (sun_data == (unsigned char *) NULL)
420 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
421 count=(ssize_t) ReadBlob(image,sun_info.length,sun_data);
422 if (count != (ssize_t) sun_info.length)
423 ThrowReaderException(CorruptImageError,"UnableToReadImageData");
424 height=sun_info.height;
425 if ((height == 0) || (sun_info.width == 0) || (sun_info.depth == 0) ||
426 ((bytes_per_line/sun_info.depth) != sun_info.width))
427 ThrowReaderException(ResourceLimitError,"ImproperImageHeader");
430 if ((bytes_per_line >> 1) != (sun_info.width*sun_info.depth+15))
431 ThrowReaderException(ResourceLimitError,"ImproperImageHeader");
433 sun_pixels=(unsigned char *) AcquireQuantumMemory(height,
434 bytes_per_line*sizeof(*sun_pixels));
435 if (sun_pixels == (unsigned char *) NULL)
436 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
437 if (sun_info.type == RT_ENCODED)
438 (void) DecodeImage(sun_data,sun_info.length,sun_pixels,bytes_per_line*
442 if (sun_info.length > (height*bytes_per_line))
443 ThrowReaderException(ResourceLimitError,"ImproperImageHeader");
444 (void) CopyMagickMemory(sun_pixels,sun_data,sun_info.length);
446 sun_data=(unsigned char *) RelinquishMagickMemory(sun_data);
448 Convert SUN raster image to pixel packets.
451 if (sun_info.depth == 1)
452 for (y=0; y < (ssize_t) image->rows; y++)
454 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
455 if (q == (Quantum *) NULL)
457 for (x=0; x < ((ssize_t) image->columns-7); x+=8)
459 for (bit=7; bit >= 0; bit--)
461 SetPixelIndex(image,(Quantum) ((*p) & (0x01 << bit) ? 0x00 : 0x01),
463 q+=GetPixelChannels(image);
467 if ((image->columns % 8) != 0)
469 for (bit=7; bit >= (int) (8-(image->columns % 8)); bit--)
471 SetPixelIndex(image,(Quantum) ((*p) & (0x01 << bit) ? 0x00 :
473 q+=GetPixelChannels(image);
477 if ((((image->columns/8)+(image->columns % 8 ? 1 : 0)) % 2) != 0)
479 if (SyncAuthenticPixels(image,exception) == MagickFalse)
481 if (image->previous == (Image *) NULL)
483 status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y,
485 if (status == MagickFalse)
490 if (image->storage_class == PseudoClass)
492 for (y=0; y < (ssize_t) image->rows; y++)
494 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
495 if (q == (Quantum *) NULL)
497 for (x=0; x < (ssize_t) image->columns; x++)
499 SetPixelIndex(image,ConstrainColormapIndex(image,*p,exception),q);
501 q+=GetPixelChannels(image);
503 if ((image->columns % 2) != 0)
505 if (SyncAuthenticPixels(image,exception) == MagickFalse)
507 if (image->previous == (Image *) NULL)
509 status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y,
511 if (status == MagickFalse)
522 if (image->alpha_trait != UndefinedPixelTrait)
524 if (bytes_per_line == 0)
525 bytes_per_line=bytes_per_pixel*image->columns;
526 for (y=0; y < (ssize_t) image->rows; y++)
528 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
529 if (q == (Quantum *) NULL)
531 for (x=0; x < (ssize_t) image->columns; x++)
533 if (image->alpha_trait != UndefinedPixelTrait)
534 SetPixelAlpha(image,ScaleCharToQuantum(*p++),q);
535 if (sun_info.type == RT_STANDARD)
537 SetPixelBlue(image,ScaleCharToQuantum(*p++),q);
538 SetPixelGreen(image,ScaleCharToQuantum(*p++),q);
539 SetPixelRed(image,ScaleCharToQuantum(*p++),q);
543 SetPixelRed(image,ScaleCharToQuantum(*p++),q);
544 SetPixelGreen(image,ScaleCharToQuantum(*p++),q);
545 SetPixelBlue(image,ScaleCharToQuantum(*p++),q);
547 if (image->colors != 0)
549 SetPixelRed(image,ClampToQuantum(image->colormap[(ssize_t)
550 GetPixelRed(image,q)].red),q);
551 SetPixelGreen(image,ClampToQuantum(image->colormap[(ssize_t)
552 GetPixelGreen(image,q)].green),q);
553 SetPixelBlue(image,ClampToQuantum(image->colormap[(ssize_t)
554 GetPixelBlue(image,q)].blue),q);
556 q+=GetPixelChannels(image);
558 if (((bytes_per_pixel*image->columns) % 2) != 0)
560 if (SyncAuthenticPixels(image,exception) == MagickFalse)
562 if (image->previous == (Image *) NULL)
564 status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y,
566 if (status == MagickFalse)
571 if (image->storage_class == PseudoClass)
572 (void) SyncImage(image,exception);
573 sun_pixels=(unsigned char *) RelinquishMagickMemory(sun_pixels);
574 if (EOFBlob(image) != MagickFalse)
576 ThrowFileException(exception,CorruptImageError,"UnexpectedEndOfFile",
581 Proceed to next image.
583 if (image_info->number_scenes != 0)
584 if (image->scene >= (image_info->scene+image_info->number_scenes-1))
586 sun_info.magic=ReadBlobMSBLong(image);
587 if (sun_info.magic == 0x59a66a95)
590 Allocate next image structure.
592 AcquireNextImage(image_info,image,exception);
593 if (GetNextImageInList(image) == (Image *) NULL)
595 image=DestroyImageList(image);
596 return((Image *) NULL);
598 image=SyncNextImageInList(image);
599 status=SetImageProgress(image,LoadImagesTag,TellBlob(image),
601 if (status == MagickFalse)
604 } while (sun_info.magic == 0x59a66a95);
605 (void) CloseBlob(image);
606 return(GetFirstImageInList(image));
610 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
614 % R e g i s t e r S U N I m a g e %
618 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
620 % RegisterSUNImage() adds attributes for the SUN image format to
621 % the list of supported formats. The attributes include the image format
622 % tag, a method to read and/or write the format, whether the format
623 % supports the saving of more than one frame to the same file or blob,
624 % whether the format supports native in-memory I/O, and a brief
625 % description of the format.
627 % The format of the RegisterSUNImage method is:
629 % size_t RegisterSUNImage(void)
632 ModuleExport size_t RegisterSUNImage(void)
637 entry=SetMagickInfo("RAS");
638 entry->decoder=(DecodeImageHandler *) ReadSUNImage;
639 entry->encoder=(EncodeImageHandler *) WriteSUNImage;
640 entry->magick=(IsImageFormatHandler *) IsSUN;
641 entry->description=ConstantString("SUN Rasterfile");
642 entry->module=ConstantString("SUN");
643 (void) RegisterMagickInfo(entry);
644 entry=SetMagickInfo("SUN");
645 entry->decoder=(DecodeImageHandler *) ReadSUNImage;
646 entry->encoder=(EncodeImageHandler *) WriteSUNImage;
647 entry->description=ConstantString("SUN Rasterfile");
648 entry->module=ConstantString("SUN");
649 (void) RegisterMagickInfo(entry);
650 return(MagickImageCoderSignature);
654 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
658 % U n r e g i s t e r S U N I m a g e %
662 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
664 % UnregisterSUNImage() removes format registrations made by the
665 % SUN module from the list of supported formats.
667 % The format of the UnregisterSUNImage method is:
669 % UnregisterSUNImage(void)
672 ModuleExport void UnregisterSUNImage(void)
674 (void) UnregisterMagickInfo("RAS");
675 (void) UnregisterMagickInfo("SUN");
679 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
683 % W r i t e S U N I m a g e %
687 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
689 % WriteSUNImage() writes an image in the SUN rasterfile format.
691 % The format of the WriteSUNImage method is:
693 % MagickBooleanType WriteSUNImage(const ImageInfo *image_info,
694 % Image *image,ExceptionInfo *exception)
696 % A description of each parameter follows.
698 % o image_info: the image info.
700 % o image: The image.
702 % o exception: return any errors or warnings in this structure.
705 static MagickBooleanType WriteSUNImage(const ImageInfo *image_info,Image *image,
706 ExceptionInfo *exception)
708 #define RMT_EQUAL_RGB 1
711 #define RT_STANDARD 1
712 #define RT_FORMAT_RGB 3
714 typedef struct _SUNInfo
736 register const Quantum
750 Open output image file.
752 assert(image_info != (const ImageInfo *) NULL);
753 assert(image_info->signature == MagickSignature);
754 assert(image != (Image *) NULL);
755 assert(image->signature == MagickSignature);
756 if (image->debug != MagickFalse)
757 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
758 assert(exception != (ExceptionInfo *) NULL);
759 assert(exception->signature == MagickSignature);
760 status=OpenBlob(image_info,image,WriteBinaryBlobMode,exception);
761 if (status == MagickFalse)
767 Initialize SUN raster file header.
769 (void) TransformImageColorspace(image,sRGBColorspace,exception);
770 sun_info.magic=0x59a66a95;
771 if ((image->columns != (unsigned int) image->columns) ||
772 (image->rows != (unsigned int) image->rows))
773 ThrowWriterException(ImageError,"WidthOrHeightExceedsLimit");
774 sun_info.width=(unsigned int) image->columns;
775 sun_info.height=(unsigned int) image->rows;
776 sun_info.type=(unsigned int)
777 (image->storage_class == DirectClass ? RT_FORMAT_RGB : RT_STANDARD);
778 sun_info.maptype=RMT_NONE;
779 sun_info.maplength=0;
780 number_pixels=(MagickSizeType) image->columns*image->rows;
781 if ((4*number_pixels) != (size_t) (4*number_pixels))
782 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
783 if (image->storage_class == DirectClass)
786 Full color SUN raster.
788 sun_info.depth=(unsigned int) image->alpha_trait != UndefinedPixelTrait ?
790 sun_info.length=(unsigned int) ((image->alpha_trait != UndefinedPixelTrait ?
791 4 : 3)*number_pixels);
792 sun_info.length+=sun_info.length & 0x01 ? (unsigned int) image->rows :
796 if (IsImageMonochrome(image,exception) != MagickFalse)
799 Monochrome SUN raster.
802 sun_info.length=(unsigned int) (((image->columns+7) >> 3)*
804 sun_info.length+=(unsigned int) (((image->columns/8)+(image->columns %
805 8 ? 1 : 0)) % 2 ? image->rows : 0);
810 Colormapped SUN raster.
813 sun_info.length=(unsigned int) number_pixels;
814 sun_info.length+=(unsigned int) (image->columns & 0x01 ? image->rows :
816 sun_info.maptype=RMT_EQUAL_RGB;
817 sun_info.maplength=(unsigned int) (3*image->colors);
822 (void) WriteBlobMSBLong(image,sun_info.magic);
823 (void) WriteBlobMSBLong(image,sun_info.width);
824 (void) WriteBlobMSBLong(image,sun_info.height);
825 (void) WriteBlobMSBLong(image,sun_info.depth);
826 (void) WriteBlobMSBLong(image,sun_info.length);
827 (void) WriteBlobMSBLong(image,sun_info.type);
828 (void) WriteBlobMSBLong(image,sun_info.maptype);
829 (void) WriteBlobMSBLong(image,sun_info.maplength);
831 Convert MIFF to SUN raster pixels.
835 if (image->storage_class == DirectClass)
837 register unsigned char
848 Allocate memory for pixels.
851 if (image->alpha_trait != UndefinedPixelTrait)
853 length=image->columns;
854 pixels=(unsigned char *) AcquireQuantumMemory(length,4*sizeof(*pixels));
855 if (pixels == (unsigned char *) NULL)
856 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
858 Convert DirectClass packet to SUN RGB pixel.
860 for (y=0; y < (ssize_t) image->rows; y++)
862 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
863 if (p == (const Quantum *) NULL)
866 for (x=0; x < (ssize_t) image->columns; x++)
868 if (image->alpha_trait != UndefinedPixelTrait)
869 *q++=ScaleQuantumToChar(GetPixelAlpha(image,p));
870 *q++=ScaleQuantumToChar(GetPixelRed(image,p));
871 *q++=ScaleQuantumToChar(GetPixelGreen(image,p));
872 *q++=ScaleQuantumToChar(GetPixelBlue(image,p));
873 p+=GetPixelChannels(image);
875 if (((bytes_per_pixel*image->columns) & 0x01) != 0)
876 *q++='\0'; /* pad scanline */
877 (void) WriteBlob(image,(size_t) (q-pixels),pixels);
878 if (image->previous == (Image *) NULL)
880 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
882 if (status == MagickFalse)
886 pixels=(unsigned char *) RelinquishMagickMemory(pixels);
889 if (IsImageMonochrome(image,exception) != MagickFalse)
891 register unsigned char
896 Convert PseudoClass image to a SUN monochrome image.
898 (void) SetImageType(image,BilevelType,exception);
899 for (y=0; y < (ssize_t) image->rows; y++)
901 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
902 if (p == (const Quantum *) NULL)
906 for (x=0; x < (ssize_t) image->columns; x++)
909 if (GetPixelLuma(image,p) < (QuantumRange/2.0))
914 (void) WriteBlobByte(image,byte);
918 p+=GetPixelChannels(image);
921 (void) WriteBlobByte(image,(unsigned char) (byte << (8-bit)));
922 if ((((image->columns/8)+
923 (image->columns % 8 ? 1 : 0)) % 2) != 0)
924 (void) WriteBlobByte(image,0); /* pad scanline */
925 if (image->previous == (Image *) NULL)
927 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
929 if (status == MagickFalse)
937 Dump colormap to file.
939 for (i=0; i < (ssize_t) image->colors; i++)
940 (void) WriteBlobByte(image,ScaleQuantumToChar(
941 ClampToQuantum(image->colormap[i].red)));
942 for (i=0; i < (ssize_t) image->colors; i++)
943 (void) WriteBlobByte(image,ScaleQuantumToChar(
944 ClampToQuantum(image->colormap[i].green)));
945 for (i=0; i < (ssize_t) image->colors; i++)
946 (void) WriteBlobByte(image,ScaleQuantumToChar(
947 ClampToQuantum(image->colormap[i].blue)));
949 Convert PseudoClass packet to SUN colormapped pixel.
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)
956 for (x=0; x < (ssize_t) image->columns; x++)
958 (void) WriteBlobByte(image,(unsigned char)
959 GetPixelIndex(image,p));
960 p+=GetPixelChannels(image);
962 if (image->columns & 0x01)
963 (void) WriteBlobByte(image,0); /* pad scanline */
964 if (image->previous == (Image *) NULL)
966 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
968 if (status == MagickFalse)
973 if (GetNextImageInList(image) == (Image *) NULL)
975 image=SyncNextImageInList(image);
976 status=SetImageProgress(image,SaveImagesTag,scene++,
977 GetImageListLength(image));
978 if (status == MagickFalse)
980 } while (image_info->adjoin != MagickFalse);
981 (void) CloseBlob(image);