]> granicus.if.org Git - imagemagick/blob - coders/sun.c
...
[imagemagick] / coders / sun.c
1 /*
2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3 %                                                                             %
4 %                                                                             %
5 %                                                                             %
6 %                            SSSSS  U   U  N   N                              %
7 %                            SS     U   U  NN  N                              %
8 %                             SSS   U   U  N N N                              %
9 %                               SS  U   U  N  NN                              %
10 %                            SSSSS   UUU   N   N                              %
11 %                                                                             %
12 %                                                                             %
13 %                    Read/Write Sun Rasterfile Image Format                   %
14 %                                                                             %
15 %                              Software Design                                %
16 %                                   Cristy                                    %
17 %                                 July 1992                                   %
18 %                                                                             %
19 %                                                                             %
20 %  Copyright 1999-2017 ImageMagick Studio LLC, a non-profit organization      %
21 %  dedicated to making software imaging solutions freely available.           %
22 %                                                                             %
23 %  You may not use this file except in compliance with the License.  You may  %
24 %  obtain a copy of the License at                                            %
25 %                                                                             %
26 %    http://www.imagemagick.org/script/license.php                            %
27 %                                                                             %
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.                                             %
33 %                                                                             %
34 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
35 %
36 %
37 */
38 \f
39 /*
40   Include declarations.
41 */
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"
68 \f
69 /*
70   Forward declarations.
71 */
72 static MagickBooleanType
73   WriteSUNImage(const ImageInfo *,Image *,ExceptionInfo *);
74 \f
75 /*
76 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
77 %                                                                             %
78 %                                                                             %
79 %                                                                             %
80 %   I s S U N                                                                 %
81 %                                                                             %
82 %                                                                             %
83 %                                                                             %
84 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
85 %
86 %  IsSUN() returns MagickTrue if the image format type, identified by the
87 %  magick string, is SUN.
88 %
89 %  The format of the IsSUN method is:
90 %
91 %      MagickBooleanType IsSUN(const unsigned char *magick,const size_t length)
92 %
93 %  A description of each parameter follows:
94 %
95 %    o magick: compare image format pattern against these bytes.
96 %
97 %    o length: Specifies the length of the magick string.
98 %
99 */
100 static MagickBooleanType IsSUN(const unsigned char *magick,const size_t length)
101 {
102   if (length < 4)
103     return(MagickFalse);
104   if (memcmp(magick,"\131\246\152\225",4) == 0)
105     return(MagickTrue);
106   return(MagickFalse);
107 }
108 \f
109 /*
110 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
111 %                                                                             %
112 %                                                                             %
113 %                                                                             %
114 %   D e c o d e I m a g e                                                     %
115 %                                                                             %
116 %                                                                             %
117 %                                                                             %
118 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
119 %
120 %  DecodeImage unpacks the packed image pixels into  runlength-encoded pixel
121 %  packets.
122 %
123 %  The format of the DecodeImage method is:
124 %
125 %      MagickBooleanType DecodeImage(const unsigned char *compressed_pixels,
126 %        const size_t length,unsigned char *pixels)
127 %
128 %  A description of each parameter follows:
129 %
130 %    o compressed_pixels:  The address of a byte (8 bits) array of compressed
131 %      pixel data.
132 %
133 %    o length:  An integer value that is the total number of bytes of the
134 %      source image (as just read by ReadBlob)
135 %
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.
140 %
141 */
142 static MagickBooleanType DecodeImage(const unsigned char *compressed_pixels,
143   const size_t length,unsigned char *pixels,size_t extent)
144 {
145   register const unsigned char
146     *p;
147
148   register unsigned char
149     *q;
150
151   ssize_t
152     count;
153
154   unsigned char
155     byte;
156
157   (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
158   assert(compressed_pixels != (unsigned char *) NULL);
159   assert(pixels != (unsigned char *) NULL);
160   p=compressed_pixels;
161   q=pixels;
162   while (((size_t) (p-compressed_pixels) < length) &&
163          ((size_t) (q-pixels) < extent))
164   {
165     byte=(*p++);
166     if (byte != 128U)
167       *q++=byte;
168     else
169       {
170         /*
171           Runlength-encoded packet: <count><byte>.
172         */
173         if (((size_t) (p-compressed_pixels) >= length))
174           break;
175         count=(*p++);
176         if (count > 0)
177           {
178             if (((size_t) (p-compressed_pixels) >= length))
179               break;
180             byte=(*p++);
181           }
182         while ((count >= 0) && ((size_t) (q-pixels) < extent))
183         {
184           *q++=byte;
185           count--;
186         }
187      }
188   }
189   return(((size_t) (q-pixels) == extent) ? MagickTrue : MagickFalse);
190 }
191 \f
192 /*
193 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
194 %                                                                             %
195 %                                                                             %
196 %                                                                             %
197 %   R e a d S U N I m a g e                                                   %
198 %                                                                             %
199 %                                                                             %
200 %                                                                             %
201 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
202 %
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
205 %  the new image.
206 %
207 %  The format of the ReadSUNImage method is:
208 %
209 %      Image *ReadSUNImage(const ImageInfo *image_info,ExceptionInfo *exception)
210 %
211 %  A description of each parameter follows:
212 %
213 %    o image_info: the image info.
214 %
215 %    o exception: return any errors or warnings in this structure.
216 %
217 */
218 static Image *ReadSUNImage(const ImageInfo *image_info,ExceptionInfo *exception)
219 {
220 #define RMT_EQUAL_RGB  1
221 #define RMT_NONE  0
222 #define RMT_RAW  2
223 #define RT_STANDARD  1
224 #define RT_ENCODED  2
225 #define RT_FORMAT_RGB  3
226
227   typedef struct _SUNInfo
228   {
229     unsigned int
230       magic,
231       width,
232       height,
233       depth,
234       length,
235       type,
236       maptype,
237       maplength;
238   } SUNInfo;
239
240   Image
241     *image;
242
243   int
244     bit;
245
246   MagickBooleanType
247     status;
248
249   MagickSizeType
250     number_pixels;
251
252   register Quantum
253     *q;
254
255   register ssize_t
256     i,
257     x;
258
259   register unsigned char
260     *p;
261
262   size_t
263     bytes_per_line,
264     extent,
265     height,
266     pixels_length,
267     quantum;
268
269   ssize_t
270     count,
271     y;
272
273   SUNInfo
274     sun_info;
275
276   unsigned char
277     *sun_data,
278     *sun_pixels;
279
280   /*
281     Open image file.
282   */
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)
293     {
294       image=DestroyImageList(image);
295       return((Image *) NULL);
296     }
297   /*
298     Read SUN raster header.
299   */
300   (void) ResetMagickMemory(&sun_info,0,sizeof(sun_info));
301   sun_info.magic=ReadBlobMSBLong(image);
302   do
303   {
304     /*
305       Verify SUN identifier.
306     */
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)
335       {
336         size_t
337           one;
338
339         image->colors=sun_info.maplength;
340         one=1;
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");
347       }
348     switch (sun_info.maptype)
349     {
350       case RMT_NONE:
351         break;
352       case RMT_EQUAL_RGB:
353       {
354         unsigned char
355           *sun_colormap;
356
357         /*
358           Read SUN raster colormap.
359         */
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(
369             sun_colormap[i]);
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(
375             sun_colormap[i]);
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(
381             sun_colormap[i]);
382         sun_colormap=(unsigned char *) RelinquishMagickMemory(sun_colormap);
383         break;
384       }
385       case RMT_RAW:
386       {
387         unsigned char
388           *sun_colormap;
389
390         /*
391           Read SUN raster colormap.
392         */
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);
401         break;
402       }
403       default:
404         ThrowReaderException(CoderError,"ColormapTypeNotSupported");
405     }
406     image->alpha_trait=sun_info.depth == 32 ? BlendPixelTrait :
407       UndefinedPixelTrait;
408     image->columns=sun_info.width;
409     image->rows=sun_info.height;
410     if (image_info->ping != MagickFalse)
411       {
412         (void) CloseBlob(image);
413         return(GetFirstImageInList(image));
414       }
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,
428       sizeof(*sun_data));
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)
433       {
434         sun_data=(unsigned char *) RelinquishMagickMemory(sun_data);
435         ThrowReaderException(CorruptImageError,"UnableToReadImageData");
436       }
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))
440       {
441         sun_data=(unsigned char *) RelinquishMagickMemory(sun_data);
442         ThrowReaderException(ResourceLimitError,"ImproperImageHeader");
443       }
444     quantum=sun_info.depth == 1 ? 15 : 7;
445     bytes_per_line+=quantum;
446     bytes_per_line<<=1;
447     if ((bytes_per_line >> 1) != (sun_info.width*sun_info.depth+quantum))
448       {
449         sun_data=(unsigned char *) RelinquishMagickMemory(sun_data);
450         ThrowReaderException(ResourceLimitError,"ImproperImageHeader");
451       }
452     bytes_per_line>>=4;
453     if (HeapOverflowSanityCheck(height,bytes_per_line) != MagickFalse)
454       {
455         sun_data=(unsigned char *) RelinquishMagickMemory(sun_data);
456         ThrowReaderException(ResourceLimitError,"ImproperImageHeader");
457       }
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)
462       {
463         sun_data=(unsigned char *) RelinquishMagickMemory(sun_data);
464         ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
465       }
466     ResetMagickMemory(sun_pixels,0,pixels_length*sizeof(*sun_pixels));
467     if (sun_info.type == RT_ENCODED)
468       {
469         status=DecodeImage(sun_data,sun_info.length,sun_pixels,pixels_length);
470         if (status == MagickFalse)
471           ThrowReaderException(CorruptImageError,"UnableToReadImageData");
472       }
473     else
474       {
475         if (sun_info.length > pixels_length)
476           {
477             sun_data=(unsigned char *) RelinquishMagickMemory(sun_data);
478             sun_pixels=(unsigned char *) RelinquishMagickMemory(sun_pixels);
479             ThrowReaderException(ResourceLimitError,"ImproperImageHeader");
480           }
481         (void) CopyMagickMemory(sun_pixels,sun_data,sun_info.length);
482       }
483     sun_data=(unsigned char *) RelinquishMagickMemory(sun_data);
484     /*
485       Convert SUN raster image to pixel packets.
486     */
487     p=sun_pixels;
488     if (sun_info.depth == 1)
489       for (y=0; y < (ssize_t) image->rows; y++)
490       {
491         q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
492         if (q == (Quantum *) NULL)
493           break;
494         for (x=0; x < ((ssize_t) image->columns-7); x+=8)
495         {
496           for (bit=7; bit >= 0; bit--)
497           {
498             SetPixelIndex(image,(Quantum) ((*p) & (0x01 << bit) ? 0x00 : 0x01),
499               q);
500             q+=GetPixelChannels(image);
501           }
502           p++;
503         }
504         if ((image->columns % 8) != 0)
505           {
506             for (bit=7; bit >= (int) (8-(image->columns % 8)); bit--)
507             {
508               SetPixelIndex(image,(Quantum) ((*p) & (0x01 << bit) ? 0x00 :
509                 0x01),q);
510               q+=GetPixelChannels(image);
511             }
512             p++;
513           }
514         if ((((image->columns/8)+(image->columns % 8 ? 1 : 0)) % 2) != 0)
515           p++;
516         if (SyncAuthenticPixels(image,exception) == MagickFalse)
517           break;
518         if (image->previous == (Image *) NULL)
519           {
520             status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y,
521               image->rows);
522             if (status == MagickFalse)
523               break;
524           }
525       }
526     else
527       if (image->storage_class == PseudoClass)
528         {
529           for (y=0; y < (ssize_t) image->rows; y++)
530           {
531             q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
532             if (q == (Quantum *) NULL)
533               break;
534             for (x=0; x < (ssize_t) image->columns; x++)
535             {
536               SetPixelIndex(image,ConstrainColormapIndex(image,*p,exception),q);
537               p++;
538               q+=GetPixelChannels(image);
539             }
540             if ((image->columns % 2) != 0)
541               p++;
542             if (SyncAuthenticPixels(image,exception) == MagickFalse)
543               break;
544             if (image->previous == (Image *) NULL)
545               {
546                 status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y,
547                 image->rows);
548                 if (status == MagickFalse)
549                   break;
550               }
551           }
552         }
553       else
554         {
555           size_t
556             bytes_per_pixel;
557
558           bytes_per_pixel=3;
559           if (image->alpha_trait != UndefinedPixelTrait)
560             bytes_per_pixel++;
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++)
564           {
565             q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
566             if (q == (Quantum *) NULL)
567               break;
568             for (x=0; x < (ssize_t) image->columns; x++)
569             {
570               if (image->alpha_trait != UndefinedPixelTrait)
571                 SetPixelAlpha(image,ScaleCharToQuantum(*p++),q);
572               if (sun_info.type == RT_STANDARD)
573                 {
574                   SetPixelBlue(image,ScaleCharToQuantum(*p++),q);
575                   SetPixelGreen(image,ScaleCharToQuantum(*p++),q);
576                   SetPixelRed(image,ScaleCharToQuantum(*p++),q);
577                 }
578               else
579                 {
580                   SetPixelRed(image,ScaleCharToQuantum(*p++),q);
581                   SetPixelGreen(image,ScaleCharToQuantum(*p++),q);
582                   SetPixelBlue(image,ScaleCharToQuantum(*p++),q);
583                 }
584               if (image->colors != 0)
585                 {
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);
592                 }
593               q+=GetPixelChannels(image);
594             }
595             if (((bytes_per_pixel*image->columns) % 2) != 0)
596               p++;
597             if (SyncAuthenticPixels(image,exception) == MagickFalse)
598               break;
599             if (image->previous == (Image *) NULL)
600               {
601                 status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y,
602                 image->rows);
603                 if (status == MagickFalse)
604                   break;
605               }
606           }
607         }
608     if (image->storage_class == PseudoClass)
609       (void) SyncImage(image,exception);
610     sun_pixels=(unsigned char *) RelinquishMagickMemory(sun_pixels);
611     if (EOFBlob(image) != MagickFalse)
612       {
613         ThrowFileException(exception,CorruptImageError,"UnexpectedEndOfFile",
614           image->filename);
615         break;
616       }
617     /*
618       Proceed to next image.
619     */
620     if (image_info->number_scenes != 0)
621       if (image->scene >= (image_info->scene+image_info->number_scenes-1))
622         break;
623     sun_info.magic=ReadBlobMSBLong(image);
624     if (sun_info.magic == 0x59a66a95)
625       {
626         /*
627           Allocate next image structure.
628         */
629         AcquireNextImage(image_info,image,exception);
630         if (GetNextImageInList(image) == (Image *) NULL)
631           {
632             image=DestroyImageList(image);
633             return((Image *) NULL);
634           }
635         image=SyncNextImageInList(image);
636         status=SetImageProgress(image,LoadImagesTag,TellBlob(image),
637           GetBlobSize(image));
638         if (status == MagickFalse)
639           break;
640       }
641   } while (sun_info.magic == 0x59a66a95);
642   (void) CloseBlob(image);
643   return(GetFirstImageInList(image));
644 }
645 \f
646 /*
647 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
648 %                                                                             %
649 %                                                                             %
650 %                                                                             %
651 %   R e g i s t e r S U N I m a g e                                           %
652 %                                                                             %
653 %                                                                             %
654 %                                                                             %
655 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
656 %
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.
663 %
664 %  The format of the RegisterSUNImage method is:
665 %
666 %      size_t RegisterSUNImage(void)
667 %
668 */
669 ModuleExport size_t RegisterSUNImage(void)
670 {
671   MagickInfo
672     *entry;
673
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);
684 }
685 \f
686 /*
687 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
688 %                                                                             %
689 %                                                                             %
690 %                                                                             %
691 %   U n r e g i s t e r S U N I m a g e                                       %
692 %                                                                             %
693 %                                                                             %
694 %                                                                             %
695 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
696 %
697 %  UnregisterSUNImage() removes format registrations made by the
698 %  SUN module from the list of supported formats.
699 %
700 %  The format of the UnregisterSUNImage method is:
701 %
702 %      UnregisterSUNImage(void)
703 %
704 */
705 ModuleExport void UnregisterSUNImage(void)
706 {
707   (void) UnregisterMagickInfo("RAS");
708   (void) UnregisterMagickInfo("SUN");
709 }
710 \f
711 /*
712 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
713 %                                                                             %
714 %                                                                             %
715 %                                                                             %
716 %   W r i t e S U N I m a g e                                                 %
717 %                                                                             %
718 %                                                                             %
719 %                                                                             %
720 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
721 %
722 %  WriteSUNImage() writes an image in the SUN rasterfile format.
723 %
724 %  The format of the WriteSUNImage method is:
725 %
726 %      MagickBooleanType WriteSUNImage(const ImageInfo *image_info,
727 %        Image *image,ExceptionInfo *exception)
728 %
729 %  A description of each parameter follows.
730 %
731 %    o image_info: the image info.
732 %
733 %    o image:  The image.
734 %
735 %    o exception: return any errors or warnings in this structure.
736 %
737 */
738 static MagickBooleanType WriteSUNImage(const ImageInfo *image_info,Image *image,
739   ExceptionInfo *exception)
740 {
741 #define RMT_EQUAL_RGB  1
742 #define RMT_NONE  0
743 #define RMT_RAW  2
744 #define RT_STANDARD  1
745 #define RT_FORMAT_RGB  3
746
747   typedef struct _SUNInfo
748   {
749     unsigned int
750       magic,
751       width,
752       height,
753       depth,
754       length,
755       type,
756       maptype,
757       maplength;
758   } SUNInfo;
759
760   MagickBooleanType
761     status;
762
763   MagickOffsetType
764     scene;
765
766   MagickSizeType
767     number_pixels;
768
769   register const Quantum
770     *p;
771
772   register ssize_t
773     i,
774     x;
775
776   ssize_t
777     y;
778
779   SUNInfo
780     sun_info;
781
782   /*
783     Open output image file.
784   */
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)
795     return(status);
796   scene=0;
797   do
798   {
799     /*
800       Initialize SUN raster file header.
801     */
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)
817       {
818         /*
819           Full color SUN raster.
820         */
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 :
826           0;
827       }
828     else
829       if (SetImageMonochrome(image,exception) != MagickFalse)
830         {
831           /*
832             Monochrome SUN raster.
833           */
834           sun_info.depth=1;
835           sun_info.length=(unsigned int) (((image->columns+7) >> 3)*
836             image->rows);
837           sun_info.length+=(unsigned int) (((image->columns/8)+(image->columns %
838             8 ? 1 : 0)) % 2 ? image->rows : 0);
839         }
840       else
841         {
842           /*
843             Colormapped SUN raster.
844           */
845           sun_info.depth=8;
846           sun_info.length=(unsigned int) number_pixels;
847           sun_info.length+=(unsigned int) (image->columns & 0x01 ? image->rows :
848             0);
849           sun_info.maptype=RMT_EQUAL_RGB;
850           sun_info.maplength=(unsigned int) (3*image->colors);
851         }
852     /*
853       Write SUN header.
854     */
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);
863     /*
864       Convert MIFF to SUN raster pixels.
865     */
866     x=0;
867     y=0;
868     if (image->storage_class == DirectClass)
869       {
870         register unsigned char
871           *q;
872
873         size_t
874           bytes_per_pixel,
875           length;
876
877         unsigned char
878           *pixels;
879
880         /*
881           Allocate memory for pixels.
882         */
883         bytes_per_pixel=3;
884         if (image->alpha_trait != UndefinedPixelTrait)
885           bytes_per_pixel++;
886         length=image->columns;
887         pixels=(unsigned char *) AcquireQuantumMemory(length,4*sizeof(*pixels));
888         if (pixels == (unsigned char *) NULL)
889           ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
890         /*
891           Convert DirectClass packet to SUN RGB pixel.
892         */
893         for (y=0; y < (ssize_t) image->rows; y++)
894         {
895           p=GetVirtualPixels(image,0,y,image->columns,1,exception);
896           if (p == (const Quantum *) NULL)
897             break;
898           q=pixels;
899           for (x=0; x < (ssize_t) image->columns; x++)
900           {
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);
907           }
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)
912             {
913               status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
914                 image->rows);
915               if (status == MagickFalse)
916                 break;
917             }
918         }
919         pixels=(unsigned char *) RelinquishMagickMemory(pixels);
920       }
921     else
922       if (SetImageMonochrome(image,exception) != MagickFalse)
923         {
924           register unsigned char
925             bit,
926             byte;
927
928           /*
929             Convert PseudoClass image to a SUN monochrome image.
930           */
931           (void) SetImageType(image,BilevelType,exception);
932           for (y=0; y < (ssize_t) image->rows; y++)
933           {
934             p=GetVirtualPixels(image,0,y,image->columns,1,exception);
935             if (p == (const Quantum *) NULL)
936               break;
937             bit=0;
938             byte=0;
939             for (x=0; x < (ssize_t) image->columns; x++)
940             {
941               byte<<=1;
942               if (GetPixelLuma(image,p) < (QuantumRange/2.0))
943                 byte|=0x01;
944               bit++;
945               if (bit == 8)
946                 {
947                   (void) WriteBlobByte(image,byte);
948                   bit=0;
949                   byte=0;
950                 }
951               p+=GetPixelChannels(image);
952             }
953             if (bit != 0)
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)
959               {
960                 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
961                 image->rows);
962                 if (status == MagickFalse)
963                   break;
964               }
965           }
966         }
967       else
968         {
969           /*
970             Dump colormap to file.
971           */
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)));
981           /*
982             Convert PseudoClass packet to SUN colormapped pixel.
983           */
984           for (y=0; y < (ssize_t) image->rows; y++)
985           {
986             p=GetVirtualPixels(image,0,y,image->columns,1,exception);
987             if (p == (const Quantum *) NULL)
988               break;
989             for (x=0; x < (ssize_t) image->columns; x++)
990             {
991               (void) WriteBlobByte(image,(unsigned char)
992                 GetPixelIndex(image,p));
993               p+=GetPixelChannels(image);
994             }
995             if (image->columns & 0x01)
996               (void) WriteBlobByte(image,0);  /* pad scanline */
997             if (image->previous == (Image *) NULL)
998               {
999                 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
1000                   image->rows);
1001                 if (status == MagickFalse)
1002                   break;
1003               }
1004           }
1005         }
1006     if (GetNextImageInList(image) == (Image *) NULL)
1007       break;
1008     image=SyncNextImageInList(image);
1009     status=SetImageProgress(image,SaveImagesTag,scene++,
1010       GetImageListLength(image));
1011     if (status == MagickFalse)
1012       break;
1013   } while (image_info->adjoin != MagickFalse);
1014   (void) CloseBlob(image);
1015   return(MagickTrue);
1016 }