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