]> granicus.if.org Git - imagemagick/blob - coders/sun.c
(no commit message)
[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 %                                John Cristy                                  %
17 %                                 July 1992                                   %
18 %                                                                             %
19 %                                                                             %
20 %  Copyright 1999-2011 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/colorspace.h"
51 #include "MagickCore/exception.h"
52 #include "MagickCore/exception-private.h"
53 #include "MagickCore/image.h"
54 #include "MagickCore/image-private.h"
55 #include "MagickCore/list.h"
56 #include "MagickCore/magick.h"
57 #include "MagickCore/memory_.h"
58 #include "MagickCore/monitor.h"
59 #include "MagickCore/monitor-private.h"
60 #include "MagickCore/pixel-accessor.h"
61 #include "MagickCore/quantum-private.h"
62 #include "MagickCore/static.h"
63 #include "MagickCore/string_.h"
64 #include "MagickCore/module.h"
65 \f
66 /*
67   Forward declarations.
68 */
69 static MagickBooleanType
70   WriteSUNImage(const ImageInfo *,Image *);
71 \f
72 /*
73 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
74 %                                                                             %
75 %                                                                             %
76 %                                                                             %
77 %   I s S U N                                                                 %
78 %                                                                             %
79 %                                                                             %
80 %                                                                             %
81 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
82 %
83 %  IsSUN() returns MagickTrue if the image format type, identified by the
84 %  magick string, is SUN.
85 %
86 %  The format of the IsSUN method is:
87 %
88 %      MagickBooleanType IsSUN(const unsigned char *magick,const size_t length)
89 %
90 %  A description of each parameter follows:
91 %
92 %    o magick: compare image format pattern against these bytes.
93 %
94 %    o length: Specifies the length of the magick string.
95 %
96 */
97 static MagickBooleanType IsSUN(const unsigned char *magick,const size_t length)
98 {
99   if (length < 4)
100     return(MagickFalse);
101   if (memcmp(magick,"\131\246\152\225",4) == 0)
102     return(MagickTrue);
103   return(MagickFalse);
104 }
105 \f
106 /*
107 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
108 %                                                                             %
109 %                                                                             %
110 %                                                                             %
111 %   D e c o d e I m a g e                                                     %
112 %                                                                             %
113 %                                                                             %
114 %                                                                             %
115 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
116 %
117 %  DecodeImage unpacks the packed image pixels into  runlength-encoded pixel
118 %  packets.
119 %
120 %  The format of the DecodeImage method is:
121 %
122 %      MagickBooleanType DecodeImage(const unsigned char *compressed_pixels,
123 %        const size_t length,unsigned char *pixels)
124 %
125 %  A description of each parameter follows:
126 %
127 %    o compressed_pixels:  The address of a byte (8 bits) array of compressed
128 %      pixel data.
129 %
130 %    o length:  An integer value that is the total number of bytes of the
131 %      source image (as just read by ReadBlob)
132 %
133 %    o pixels:  The address of a byte (8 bits) array of pixel data created by
134 %      the uncompression process.  The number of bytes in this array
135 %      must be at least equal to the number columns times the number of rows
136 %      of the source pixels.
137 %
138 */
139 static MagickBooleanType DecodeImage(const unsigned char *compressed_pixels,
140   const size_t length,unsigned char *pixels,size_t maxpixels)
141 {
142   register const unsigned char
143     *l,
144     *p;
145
146   register unsigned char
147     *q;
148
149   ssize_t
150     count;
151
152   unsigned char
153     byte;
154
155   (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
156   assert(compressed_pixels != (unsigned char *) NULL);
157   assert(pixels != (unsigned char *) NULL);
158   p=compressed_pixels;
159   q=pixels;
160   l=q+maxpixels;
161   while (((size_t) (p-compressed_pixels) < length) && (q < l))
162   {
163     byte=(*p++);
164     if (byte != 128U)
165       *q++=byte;
166     else
167       {
168         /*
169           Runlength-encoded packet: <count><byte>
170         */
171         count=(ssize_t) (*p++);
172         if (count > 0)
173           byte=(*p++);
174         while ((count >= 0) && (q < l))
175         {
176           *q++=byte;
177           count--;
178         }
179      }
180   }
181   return(MagickTrue);
182 }
183 \f
184 /*
185 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
186 %                                                                             %
187 %                                                                             %
188 %                                                                             %
189 %   R e a d S U N I m a g e                                                   %
190 %                                                                             %
191 %                                                                             %
192 %                                                                             %
193 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
194 %
195 %  ReadSUNImage() reads a SUN image file and returns it.  It allocates
196 %  the memory necessary for the new Image structure and returns a pointer to
197 %  the new image.
198 %
199 %  The format of the ReadSUNImage method is:
200 %
201 %      Image *ReadSUNImage(const ImageInfo *image_info,ExceptionInfo *exception)
202 %
203 %  A description of each parameter follows:
204 %
205 %    o image_info: the image info.
206 %
207 %    o exception: return any errors or warnings in this structure.
208 %
209 */
210 static Image *ReadSUNImage(const ImageInfo *image_info,ExceptionInfo *exception)
211 {
212 #define RMT_EQUAL_RGB  1
213 #define RMT_NONE  0
214 #define RMT_RAW  2
215 #define RT_STANDARD  1
216 #define RT_ENCODED  2
217 #define RT_FORMAT_RGB  3
218
219   typedef struct _SUNInfo
220   {
221     unsigned int
222       magic,
223       width,
224       height,
225       depth,
226       length,
227       type,
228       maptype,
229       maplength;
230   } SUNInfo;
231
232   Image
233     *image;
234
235   int
236     bit;
237
238   MagickBooleanType
239     status;
240
241   MagickSizeType
242     number_pixels;
243
244   register Quantum
245     *q;
246
247   register ssize_t
248     i,
249     x;
250
251   register unsigned char
252     *p;
253
254   size_t
255     length;
256
257   ssize_t
258     count,
259     y;
260
261   SUNInfo
262     sun_info;
263
264   unsigned char
265     *sun_data,
266     *sun_pixels;
267
268   unsigned int
269     bytes_per_line;
270
271   /*
272     Open image file.
273   */
274   assert(image_info != (const ImageInfo *) NULL);
275   assert(image_info->signature == MagickSignature);
276   if (image_info->debug != MagickFalse)
277     (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
278       image_info->filename);
279   assert(exception != (ExceptionInfo *) NULL);
280   assert(exception->signature == MagickSignature);
281   image=AcquireImage(image_info);
282   status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception);
283   if (status == MagickFalse)
284     {
285       image=DestroyImageList(image);
286       return((Image *) NULL);
287     }
288   /*
289     Read SUN raster header.
290   */
291   (void) ResetMagickMemory(&sun_info,0,sizeof(sun_info));
292   sun_info.magic=ReadBlobMSBLong(image);
293   do
294   {
295     /*
296       Verify SUN identifier.
297     */
298     if (sun_info.magic != 0x59a66a95)
299       ThrowReaderException(CorruptImageError,"ImproperImageHeader");
300     sun_info.width=ReadBlobMSBLong(image);
301     sun_info.height=ReadBlobMSBLong(image);
302     sun_info.depth=ReadBlobMSBLong(image);
303     sun_info.length=ReadBlobMSBLong(image);
304     sun_info.type=ReadBlobMSBLong(image);
305     sun_info.maptype=ReadBlobMSBLong(image);
306     sun_info.maplength=ReadBlobMSBLong(image);
307     image->columns=sun_info.width;
308     image->rows=sun_info.height;
309     if ((sun_info.depth == 0) || (sun_info.depth > 32))
310       ThrowReaderException(CorruptImageError,"ImproperImageHeader");
311     image->depth=sun_info.depth <= 8 ? sun_info.depth :
312       MAGICKCORE_QUANTUM_DEPTH;
313     if (sun_info.depth < 24)
314       {
315         size_t
316           one;
317
318         image->storage_class=PseudoClass;
319         image->colors=sun_info.maplength;
320         one=1;
321         if (sun_info.maptype == RMT_NONE)
322           image->colors=one << sun_info.depth;
323         if (sun_info.maptype == RMT_EQUAL_RGB)
324           image->colors=sun_info.maplength/3;
325       }
326     switch (sun_info.maptype)
327     {
328       case RMT_NONE:
329       {
330         if (sun_info.depth < 24)
331           {
332             /*
333               Create linear color ramp.
334             */
335             if (AcquireImageColormap(image,image->colors) == MagickFalse)
336               ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
337           }
338         break;
339       }
340       case RMT_EQUAL_RGB:
341       {
342         unsigned char
343           *sun_colormap;
344
345         /*
346           Read SUN raster colormap.
347         */
348         if (AcquireImageColormap(image,image->colors) == MagickFalse)
349           ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
350         sun_colormap=(unsigned char *) AcquireQuantumMemory(image->colors,
351           sizeof(*sun_colormap));
352         if (sun_colormap == (unsigned char *) NULL)
353           ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
354         count=ReadBlob(image,image->colors,sun_colormap);
355         for (i=0; i < (ssize_t) image->colors; i++)
356           image->colormap[i].red=ScaleCharToQuantum(sun_colormap[i]);
357         count=ReadBlob(image,image->colors,sun_colormap);
358         for (i=0; i < (ssize_t) image->colors; i++)
359           image->colormap[i].green=ScaleCharToQuantum(sun_colormap[i]);
360         count=ReadBlob(image,image->colors,sun_colormap);
361         for (i=0; i < (ssize_t) image->colors; i++)
362           image->colormap[i].blue=ScaleCharToQuantum(sun_colormap[i]);
363         sun_colormap=(unsigned char *) RelinquishMagickMemory(sun_colormap);
364         break;
365       }
366       case RMT_RAW:
367       {
368         unsigned char
369           *sun_colormap;
370
371         /*
372           Read SUN raster colormap.
373         */
374         sun_colormap=(unsigned char *) AcquireQuantumMemory(sun_info.maplength,
375           sizeof(*sun_colormap));
376         if (sun_colormap == (unsigned char *) NULL)
377           ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
378         count=ReadBlob(image,sun_info.maplength,sun_colormap);
379         sun_colormap=(unsigned char *) RelinquishMagickMemory(sun_colormap);
380         break;
381       }
382       default:
383         ThrowReaderException(CoderError,"ColormapTypeNotSupported");
384     }
385     image->matte=sun_info.depth == 32 ? MagickTrue : MagickFalse;
386     image->columns=sun_info.width;
387     image->rows=sun_info.height;
388     if (image_info->ping != MagickFalse)
389       {
390         (void) CloseBlob(image);
391         return(GetFirstImageInList(image));
392       }
393     if ((sun_info.length*sizeof(*sun_data))/sizeof(*sun_data) !=
394         sun_info.length || !sun_info.length)
395       ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
396     number_pixels=(MagickSizeType) image->columns*image->rows;
397     if ((sun_info.depth >= 8) &&
398         ((number_pixels*((sun_info.depth+7)/8)) > sun_info.length))
399       ThrowReaderException(CorruptImageError,"ImproperImageHeader");
400     sun_data=(unsigned char *) AcquireQuantumMemory((size_t) sun_info.length,
401       sizeof(*sun_data));
402     if (sun_data == (unsigned char *) NULL)
403       ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
404     count=(ssize_t) ReadBlob(image,sun_info.length,sun_data);
405     if ((count == 0) && (sun_info.type != RT_ENCODED))
406       ThrowReaderException(CorruptImageError,"UnableToReadImageData");
407     sun_pixels=sun_data;
408     bytes_per_line=0;
409     if (sun_info.type == RT_ENCODED)
410       {
411         size_t
412           height;
413
414         /*
415           Read run-length encoded raster pixels.
416         */
417         height=sun_info.height;
418         bytes_per_line=sun_info.width*sun_info.depth;
419         if ((height == 0) || (sun_info.width == 0) || (sun_info.depth == 0) ||
420             ((bytes_per_line/sun_info.depth) != sun_info.width))
421           ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
422         bytes_per_line+=15;
423         bytes_per_line<<=1;
424         if ((bytes_per_line >> 1) != (sun_info.width*sun_info.depth+15))
425           ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
426         bytes_per_line>>=4;
427         sun_pixels=(unsigned char *) AcquireQuantumMemory(height,
428           bytes_per_line*sizeof(*sun_pixels));
429         if (sun_pixels == (unsigned char *) NULL)
430           ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
431         (void) DecodeImage(sun_data,sun_info.length,sun_pixels,
432           bytes_per_line*height);
433         sun_data=(unsigned char *) RelinquishMagickMemory(sun_data);
434       }
435     /*
436       Convert SUN raster image to pixel packets.
437     */
438     p=sun_pixels;
439     if (sun_info.depth == 1)
440       for (y=0; y < (ssize_t) image->rows; y++)
441       {
442         q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
443         if (q == (const Quantum *) NULL)
444           break;
445         for (x=0; x < ((ssize_t) image->columns-7); x+=8)
446         {
447           for (bit=7; bit >= 0; bit--)
448           {
449             SetPixelIndex(image,((*p) & (0x01 << bit) ? 0x00 : 0x01),q);
450             q+=GetPixelChannels(image);
451           }
452           p++;
453         }
454         if ((image->columns % 8) != 0)
455           {
456             for (bit=7; bit >= (ssize_t) (8-(image->columns % 8)); bit--)
457             {
458               SetPixelIndex(image,(*p) & (0x01 << bit) ? 0x00 : 0x01,q);
459               q+=GetPixelChannels(image);
460             }
461             p++;
462           }
463         if ((((image->columns/8)+(image->columns % 8 ? 1 : 0)) % 2) != 0)
464           p++;
465         if (SyncAuthenticPixels(image,exception) == MagickFalse)
466           break;
467         if (image->previous == (Image *) NULL)
468           {
469             status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y,
470               image->rows);
471             if (status == MagickFalse)
472               break;
473           }
474       }
475     else
476       if (image->storage_class == PseudoClass)
477         {
478           length=image->rows*(image->columns+image->columns % 2);
479           if (((sun_info.type == RT_ENCODED) &&
480                (length > (bytes_per_line*image->rows))) ||
481               ((sun_info.type != RT_ENCODED) && (length > sun_info.length)))
482             ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
483           for (y=0; y < (ssize_t) image->rows; y++)
484           {
485             q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
486             if (q == (const Quantum *) NULL)
487               break;
488             for (x=0; x < (ssize_t) image->columns; x++)
489             {
490               SetPixelIndex(image,*p++,q);
491               q+=GetPixelChannels(image);
492             }
493             if ((image->columns % 2) != 0)
494               p++;
495             if (SyncAuthenticPixels(image,exception) == MagickFalse)
496               break;
497             if (image->previous == (Image *) NULL)
498               {
499                 status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y,
500                 image->rows);
501                 if (status == MagickFalse)
502                   break;
503               }
504           }
505         }
506       else
507         {
508           size_t
509             bytes_per_pixel;
510
511           bytes_per_pixel=3;
512           if (image->matte != MagickFalse)
513             bytes_per_pixel++;
514           length=image->rows*((bytes_per_line*image->columns)+
515             image->columns % 2);
516           if (((sun_info.type == RT_ENCODED) &&
517                (length > (bytes_per_line*image->rows))) ||
518               ((sun_info.type != RT_ENCODED) && (length > sun_info.length)))
519             ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
520           for (y=0; y < (ssize_t) image->rows; y++)
521           {
522             q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
523             if (q == (const Quantum *) NULL)
524               break;
525             for (x=0; x < (ssize_t) image->columns; x++)
526             {
527               if (image->matte != MagickFalse)
528                 SetPixelAlpha(image,ScaleCharToQuantum(*p++),q);
529               if (sun_info.type == RT_STANDARD)
530                 {
531                   SetPixelBlue(image,ScaleCharToQuantum(*p++),q);
532                   SetPixelGreen(image,ScaleCharToQuantum(*p++),q);
533                   SetPixelRed(image,ScaleCharToQuantum(*p++),q);
534                 }
535               else
536                 {
537                   SetPixelRed(image,ScaleCharToQuantum(*p++),q);
538                   SetPixelGreen(image,ScaleCharToQuantum(*p++),q);
539                   SetPixelBlue(image,ScaleCharToQuantum(*p++),q);
540                 }
541               if (image->colors != 0)
542                 {
543                   SetPixelRed(image,image->colormap[(ssize_t)
544                     GetPixelRed(image,q)].red,q);
545                   SetPixelGreen(image,image->colormap[(ssize_t)
546                     GetPixelGreen(image,q)].green,q);
547                   SetPixelBlue(image,image->colormap[(ssize_t)
548                     GetPixelBlue(image,q)].blue,q);
549                 }
550               q+=GetPixelChannels(image);
551             }
552             if (((bytes_per_pixel*image->columns) % 2) != 0)
553               p++;
554             if (SyncAuthenticPixels(image,exception) == MagickFalse)
555               break;
556             if (image->previous == (Image *) NULL)
557               {
558                 status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y,
559                 image->rows);
560                 if (status == MagickFalse)
561                   break;
562               }
563           }
564         }
565     if (image->storage_class == PseudoClass)
566       (void) SyncImage(image);
567     sun_pixels=(unsigned char *) RelinquishMagickMemory(sun_pixels);
568     if (EOFBlob(image) != MagickFalse)
569       {
570         ThrowFileException(exception,CorruptImageError,"UnexpectedEndOfFile",
571           image->filename);
572         break;
573       }
574     /*
575       Proceed to next image.
576     */
577     if (image_info->number_scenes != 0)
578       if (image->scene >= (image_info->scene+image_info->number_scenes-1))
579         break;
580     sun_info.magic=ReadBlobMSBLong(image);
581     if (sun_info.magic == 0x59a66a95)
582       {
583         /*
584           Allocate next image structure.
585         */
586         AcquireNextImage(image_info,image);
587         if (GetNextImageInList(image) == (Image *) NULL)
588           {
589             image=DestroyImageList(image);
590             return((Image *) NULL);
591           }
592         image=SyncNextImageInList(image);
593         status=SetImageProgress(image,LoadImagesTag,TellBlob(image),
594           GetBlobSize(image));
595         if (status == MagickFalse)
596           break;
597       }
598   } while (sun_info.magic == 0x59a66a95);
599   (void) CloseBlob(image);
600   return(GetFirstImageInList(image));
601 }
602 \f
603 /*
604 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
605 %                                                                             %
606 %                                                                             %
607 %                                                                             %
608 %   R e g i s t e r S U N I m a g e                                           %
609 %                                                                             %
610 %                                                                             %
611 %                                                                             %
612 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
613 %
614 %  RegisterSUNImage() adds attributes for the SUN image format to
615 %  the list of supported formats.  The attributes include the image format
616 %  tag, a method to read and/or write the format, whether the format
617 %  supports the saving of more than one frame to the same file or blob,
618 %  whether the format supports native in-memory I/O, and a brief
619 %  description of the format.
620 %
621 %  The format of the RegisterSUNImage method is:
622 %
623 %      size_t RegisterSUNImage(void)
624 %
625 */
626 ModuleExport size_t RegisterSUNImage(void)
627 {
628   MagickInfo
629     *entry;
630
631   entry=SetMagickInfo("RAS");
632   entry->decoder=(DecodeImageHandler *) ReadSUNImage;
633   entry->encoder=(EncodeImageHandler *) WriteSUNImage;
634   entry->magick=(IsImageFormatHandler *) IsSUN;
635   entry->description=ConstantString("SUN Rasterfile");
636   entry->module=ConstantString("SUN");
637   (void) RegisterMagickInfo(entry);
638   entry=SetMagickInfo("SUN");
639   entry->decoder=(DecodeImageHandler *) ReadSUNImage;
640   entry->encoder=(EncodeImageHandler *) WriteSUNImage;
641   entry->description=ConstantString("SUN Rasterfile");
642   entry->module=ConstantString("SUN");
643   (void) RegisterMagickInfo(entry);
644   return(MagickImageCoderSignature);
645 }
646 \f
647 /*
648 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
649 %                                                                             %
650 %                                                                             %
651 %                                                                             %
652 %   U n r e g i s t e r S U N I m a g e                                       %
653 %                                                                             %
654 %                                                                             %
655 %                                                                             %
656 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
657 %
658 %  UnregisterSUNImage() removes format registrations made by the
659 %  SUN module from the list of supported formats.
660 %
661 %  The format of the UnregisterSUNImage method is:
662 %
663 %      UnregisterSUNImage(void)
664 %
665 */
666 ModuleExport void UnregisterSUNImage(void)
667 {
668   (void) UnregisterMagickInfo("RAS");
669   (void) UnregisterMagickInfo("SUN");
670 }
671 \f
672 /*
673 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
674 %                                                                             %
675 %                                                                             %
676 %                                                                             %
677 %   W r i t e S U N I m a g e                                                 %
678 %                                                                             %
679 %                                                                             %
680 %                                                                             %
681 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
682 %
683 %  WriteSUNImage() writes an image in the SUN rasterfile format.
684 %
685 %  The format of the WriteSUNImage method is:
686 %
687 %      MagickBooleanType WriteSUNImage(const ImageInfo *image_info,Image *image)
688 %
689 %  A description of each parameter follows.
690 %
691 %    o image_info: the image info.
692 %
693 %    o image:  The image.
694 %
695 */
696 static MagickBooleanType WriteSUNImage(const ImageInfo *image_info,Image *image)
697 {
698 #define RMT_EQUAL_RGB  1
699 #define RMT_NONE  0
700 #define RMT_RAW  2
701 #define RT_STANDARD  1
702 #define RT_FORMAT_RGB  3
703
704   typedef struct _SUNInfo
705   {
706     unsigned int
707       magic,
708       width,
709       height,
710       depth,
711       length,
712       type,
713       maptype,
714       maplength;
715   } SUNInfo;
716
717   MagickBooleanType
718     status;
719
720   MagickOffsetType
721     scene;
722
723   MagickSizeType
724     number_pixels;
725
726   register const Quantum
727     *p;
728
729   register ssize_t
730     i,
731     x;
732
733   ssize_t
734     y;
735
736   SUNInfo
737     sun_info;
738
739   /*
740     Open output image file.
741   */
742   assert(image_info != (const ImageInfo *) NULL);
743   assert(image_info->signature == MagickSignature);
744   assert(image != (Image *) NULL);
745   assert(image->signature == MagickSignature);
746   if (image->debug != MagickFalse)
747     (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
748   status=OpenBlob(image_info,image,WriteBinaryBlobMode,&image->exception);
749   if (status == MagickFalse)
750     return(status);
751   scene=0;
752   do
753   {
754     /*
755       Initialize SUN raster file header.
756     */
757     if (image->colorspace != RGBColorspace)
758       (void) TransformImageColorspace(image,RGBColorspace);
759     sun_info.magic=0x59a66a95;
760     if ((image->columns != (unsigned int) image->columns) ||
761         (image->rows != (unsigned int) image->rows))
762       ThrowWriterException(ImageError,"WidthOrHeightExceedsLimit");
763     sun_info.width=(unsigned int) image->columns;
764     sun_info.height=(unsigned int) image->rows;
765     sun_info.type=(unsigned int) 
766       (image->storage_class == DirectClass ? RT_FORMAT_RGB : RT_STANDARD);
767     sun_info.maptype=RMT_NONE;
768     sun_info.maplength=0;
769     number_pixels=(MagickSizeType) image->columns*image->rows;
770     if ((4*number_pixels) != (size_t) (4*number_pixels))
771       ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
772     if (image->storage_class == DirectClass)
773       {
774         /*
775           Full color SUN raster.
776         */
777         sun_info.depth=(unsigned int) image->matte ? 32U : 24U;
778         sun_info.length=(unsigned int) ((image->matte ? 4 : 3)*number_pixels);
779         sun_info.length+=sun_info.length & 0x01 ? (unsigned int) image->rows :
780           0;
781       }
782     else
783       if (IsImageMonochrome(image,&image->exception))
784         {
785           /*
786             Monochrome SUN raster.
787           */
788           sun_info.depth=1;
789           sun_info.length=(unsigned int) (((image->columns+7) >> 3)*
790             image->rows);
791           sun_info.length+=(unsigned int) (((image->columns/8)+(image->columns %
792             8 ? 1 : 0)) % 2 ? image->rows : 0);
793         }
794       else
795         {
796           /*
797             Colormapped SUN raster.
798           */
799           sun_info.depth=8;
800           sun_info.length=(unsigned int) number_pixels;
801           sun_info.length+=(unsigned int) (image->columns & 0x01 ? image->rows :
802             0);
803           sun_info.maptype=RMT_EQUAL_RGB;
804           sun_info.maplength=(unsigned int) (3*image->colors);
805         }
806     /*
807       Write SUN header.
808     */
809     (void) WriteBlobMSBLong(image,sun_info.magic);
810     (void) WriteBlobMSBLong(image,sun_info.width);
811     (void) WriteBlobMSBLong(image,sun_info.height);
812     (void) WriteBlobMSBLong(image,sun_info.depth);
813     (void) WriteBlobMSBLong(image,sun_info.length);
814     (void) WriteBlobMSBLong(image,sun_info.type);
815     (void) WriteBlobMSBLong(image,sun_info.maptype);
816     (void) WriteBlobMSBLong(image,sun_info.maplength);
817     /*
818       Convert MIFF to SUN raster pixels.
819     */
820     x=0;
821     y=0;
822     if (image->storage_class == DirectClass)
823       {
824         register unsigned char
825           *q;
826
827         size_t
828           bytes_per_pixel,
829           length;
830
831         unsigned char
832           *pixels;
833
834         /*
835           Allocate memory for pixels.
836         */
837         bytes_per_pixel=3;
838         if (image->matte != MagickFalse)
839           bytes_per_pixel++;
840         length=image->columns;
841         pixels=(unsigned char *) AcquireQuantumMemory(length,4*sizeof(*pixels));
842         if (pixels == (unsigned char *) NULL)
843           ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
844         /*
845           Convert DirectClass packet to SUN RGB pixel.
846         */
847         for (y=0; y < (ssize_t) image->rows; y++)
848         {
849           p=GetVirtualPixels(image,0,y,image->columns,1,&image->exception);
850           if (p == (const Quantum *) NULL)
851             break;
852           q=pixels;
853           for (x=0; x < (ssize_t) image->columns; x++)
854           {
855             if (image->matte != MagickFalse)
856               *q++=ScaleQuantumToChar(GetPixelAlpha(image,p));
857             *q++=ScaleQuantumToChar(GetPixelRed(image,p));
858             *q++=ScaleQuantumToChar(GetPixelGreen(image,p));
859             *q++=ScaleQuantumToChar(GetPixelBlue(image,p));
860             p+=GetPixelChannels(image);
861           }
862           if (((bytes_per_pixel*image->columns) & 0x01) != 0)
863             *q++='\0';  /* pad scanline */
864           (void) WriteBlob(image,(size_t) (q-pixels),pixels);
865           if (image->previous == (Image *) NULL)
866             {
867               status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
868                 image->rows);
869               if (status == MagickFalse)
870                 break;
871             }
872         }
873         pixels=(unsigned char *) RelinquishMagickMemory(pixels);
874       }
875     else
876       if (IsImageMonochrome(image,&image->exception))
877         {
878           register unsigned char
879             bit,
880             byte;
881
882           /*
883             Convert PseudoClass image to a SUN monochrome image.
884           */
885           (void) SetImageType(image,BilevelType);
886           for (y=0; y < (ssize_t) image->rows; y++)
887           {
888             p=GetVirtualPixels(image,0,y,image->columns,1,&image->exception);
889             if (p == (const Quantum *) NULL)
890               break;
891             bit=0;
892             byte=0;
893             for (x=0; x < (ssize_t) image->columns; x++)
894             {
895               byte<<=1;
896               if (GetPixelIntensity(image,p) < (MagickRealType) (QuantumRange/2.0))
897                 byte|=0x01;
898               bit++;
899               if (bit == 8)
900                 {
901                   (void) WriteBlobByte(image,byte);
902                   bit=0;
903                   byte=0;
904                 }
905               p+=GetPixelChannels(image);
906             }
907             if (bit != 0)
908               (void) WriteBlobByte(image,(unsigned char) (byte << (8-bit)));
909             if ((((image->columns/8)+
910                 (image->columns % 8 ? 1 : 0)) % 2) != 0)
911               (void) WriteBlobByte(image,0);  /* pad scanline */
912             if (image->previous == (Image *) NULL)
913               {
914                 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
915                 image->rows);
916                 if (status == MagickFalse)
917                   break;
918               }
919           }
920         }
921       else
922         {
923           /*
924             Dump colormap to file.
925           */
926           for (i=0; i < (ssize_t) image->colors; i++)
927             (void) WriteBlobByte(image,ScaleQuantumToChar(
928               image->colormap[i].red));
929           for (i=0; i < (ssize_t) image->colors; i++)
930             (void) WriteBlobByte(image,ScaleQuantumToChar(
931               image->colormap[i].green));
932           for (i=0; i < (ssize_t) image->colors; i++)
933             (void) WriteBlobByte(image,ScaleQuantumToChar(
934               image->colormap[i].blue));
935           /*
936             Convert PseudoClass packet to SUN colormapped pixel.
937           */
938           for (y=0; y < (ssize_t) image->rows; y++)
939           {
940             p=GetVirtualPixels(image,0,y,image->columns,1,&image->exception);
941             if (p == (const Quantum *) NULL)
942               break;
943             for (x=0; x < (ssize_t) image->columns; x++)
944             {
945               (void) WriteBlobByte(image,(unsigned char)
946                 GetPixelIndex(image,p));
947               p+=GetPixelChannels(image);
948             }
949             if (image->columns & 0x01)
950               (void) WriteBlobByte(image,0);  /* pad scanline */
951             if (image->previous == (Image *) NULL)
952               {
953                 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
954                   image->rows);
955                 if (status == MagickFalse)
956                   break;
957               }
958           }
959         }
960     if (GetNextImageInList(image) == (Image *) NULL)
961       break;
962     image=SyncNextImageInList(image);
963     status=SetImageProgress(image,SaveImagesTag,scene++,
964       GetImageListLength(image));
965     if (status == MagickFalse)
966       break;
967   } while (image_info->adjoin != MagickFalse);
968   (void) CloseBlob(image);
969   return(MagickTrue);
970 }