]> 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 "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     *l,
142     *p;
143
144   register unsigned char
145     *q;
146
147   ssize_t
148     count;
149
150   unsigned char
151     byte;
152
153   (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
154   assert(compressed_pixels != (unsigned char *) NULL);
155   assert(pixels != (unsigned char *) NULL);
156   p=compressed_pixels;
157   q=pixels;
158   l=q+maxpixels;
159   while (((size_t) (p-compressed_pixels) < length) && (q < l))
160   {
161     byte=(*p++);
162     if (byte != 128U)
163       *q++=byte;
164     else
165       {
166         /*
167           Runlength-encoded packet: <count><byte>
168         */
169         count=(ssize_t) (*p++);
170         if (count > 0)
171           byte=(*p++);
172         while ((count >= 0) && (q < l))
173         {
174           *q++=byte;
175           count--;
176         }
177      }
178   }
179   return(MagickTrue);
180 }
181 \f
182 /*
183 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
184 %                                                                             %
185 %                                                                             %
186 %                                                                             %
187 %   R e a d S U N I m a g e                                                   %
188 %                                                                             %
189 %                                                                             %
190 %                                                                             %
191 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
192 %
193 %  ReadSUNImage() reads a SUN image file and returns it.  It allocates
194 %  the memory necessary for the new Image structure and returns a pointer to
195 %  the new image.
196 %
197 %  The format of the ReadSUNImage method is:
198 %
199 %      Image *ReadSUNImage(const ImageInfo *image_info,ExceptionInfo *exception)
200 %
201 %  A description of each parameter follows:
202 %
203 %    o image_info: the image info.
204 %
205 %    o exception: return any errors or warnings in this structure.
206 %
207 */
208 static Image *ReadSUNImage(const ImageInfo *image_info,ExceptionInfo *exception)
209 {
210 #define RMT_EQUAL_RGB  1
211 #define RMT_NONE  0
212 #define RMT_RAW  2
213 #define RT_STANDARD  1
214 #define RT_ENCODED  2
215 #define RT_FORMAT_RGB  3
216
217   typedef struct _SUNInfo
218   {
219     unsigned int
220       magic,
221       width,
222       height,
223       depth,
224       length,
225       type,
226       maptype,
227       maplength;
228   } SUNInfo;
229
230   Image
231     *image;
232
233   int
234     bit;
235
236   MagickBooleanType
237     status;
238
239   MagickSizeType
240     number_pixels;
241
242   register IndexPacket
243     *indexes;
244
245   register PixelPacket
246     *q;
247
248   register ssize_t
249     i,
250     x;
251
252   register unsigned char
253     *p;
254
255   size_t
256     length;
257
258   ssize_t
259     count,
260     y;
261
262   SUNInfo
263     sun_info;
264
265   unsigned char
266     *sun_data,
267     *sun_pixels;
268
269   unsigned int
270     bytes_per_line;
271
272   /*
273     Open image file.
274   */
275   assert(image_info != (const ImageInfo *) NULL);
276   assert(image_info->signature == MagickSignature);
277   if (image_info->debug != MagickFalse)
278     (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
279       image_info->filename);
280   assert(exception != (ExceptionInfo *) NULL);
281   assert(exception->signature == MagickSignature);
282   image=AcquireImage(image_info);
283   status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception);
284   if (status == MagickFalse)
285     {
286       image=DestroyImageList(image);
287       return((Image *) NULL);
288     }
289   /*
290     Read SUN raster header.
291   */
292   (void) ResetMagickMemory(&sun_info,0,sizeof(sun_info));
293   sun_info.magic=ReadBlobMSBLong(image);
294   do
295   {
296     /*
297       Verify SUN identifier.
298     */
299     if (sun_info.magic != 0x59a66a95)
300       ThrowReaderException(CorruptImageError,"ImproperImageHeader");
301     sun_info.width=ReadBlobMSBLong(image);
302     sun_info.height=ReadBlobMSBLong(image);
303     sun_info.depth=ReadBlobMSBLong(image);
304     sun_info.length=ReadBlobMSBLong(image);
305     sun_info.type=ReadBlobMSBLong(image);
306     sun_info.maptype=ReadBlobMSBLong(image);
307     sun_info.maplength=ReadBlobMSBLong(image);
308     image->columns=sun_info.width;
309     image->rows=sun_info.height;
310     if ((sun_info.depth == 0) || (sun_info.depth > 32))
311       ThrowReaderException(CorruptImageError,"ImproperImageHeader");
312     image->depth=sun_info.depth <= 8 ? sun_info.depth :
313       MAGICKCORE_QUANTUM_DEPTH;
314     if (sun_info.depth < 24)
315       {
316         size_t
317           one;
318
319         image->storage_class=PseudoClass;
320         image->colors=sun_info.maplength;
321         one=1;
322         if (sun_info.maptype == RMT_NONE)
323           image->colors=one << sun_info.depth;
324         if (sun_info.maptype == RMT_EQUAL_RGB)
325           image->colors=sun_info.maplength/3;
326       }
327     switch (sun_info.maptype)
328     {
329       case RMT_NONE:
330       {
331         if (sun_info.depth < 24)
332           {
333             /*
334               Create linear color ramp.
335             */
336             if (AcquireImageColormap(image,image->colors) == MagickFalse)
337               ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
338           }
339         break;
340       }
341       case RMT_EQUAL_RGB:
342       {
343         unsigned char
344           *sun_colormap;
345
346         /*
347           Read SUN raster colormap.
348         */
349         if (AcquireImageColormap(image,image->colors) == MagickFalse)
350           ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
351         sun_colormap=(unsigned char *) AcquireQuantumMemory(image->colors,
352           sizeof(*sun_colormap));
353         if (sun_colormap == (unsigned char *) NULL)
354           ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
355         count=ReadBlob(image,image->colors,sun_colormap);
356         for (i=0; i < (ssize_t) image->colors; i++)
357           image->colormap[i].red=ScaleCharToQuantum(sun_colormap[i]);
358         count=ReadBlob(image,image->colors,sun_colormap);
359         for (i=0; i < (ssize_t) image->colors; i++)
360           image->colormap[i].green=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].blue=ScaleCharToQuantum(sun_colormap[i]);
364         sun_colormap=(unsigned char *) RelinquishMagickMemory(sun_colormap);
365         break;
366       }
367       case RMT_RAW:
368       {
369         unsigned char
370           *sun_colormap;
371
372         /*
373           Read SUN raster colormap.
374         */
375         sun_colormap=(unsigned char *) AcquireQuantumMemory(sun_info.maplength,
376           sizeof(*sun_colormap));
377         if (sun_colormap == (unsigned char *) NULL)
378           ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
379         count=ReadBlob(image,sun_info.maplength,sun_colormap);
380         sun_colormap=(unsigned char *) RelinquishMagickMemory(sun_colormap);
381         break;
382       }
383       default:
384         ThrowReaderException(CoderError,"ColormapTypeNotSupported");
385     }
386     image->matte=sun_info.depth == 32 ? MagickTrue : MagickFalse;
387     image->columns=sun_info.width;
388     image->rows=sun_info.height;
389     if (image_info->ping != MagickFalse)
390       {
391         (void) CloseBlob(image);
392         return(GetFirstImageInList(image));
393       }
394     if ((sun_info.length*sizeof(*sun_data))/sizeof(*sun_data) !=
395         sun_info.length || !sun_info.length)
396       ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
397     number_pixels=(MagickSizeType) image->columns*image->rows;
398     if ((sun_info.depth >= 8) &&
399         ((number_pixels*((sun_info.depth+7)/8)) > sun_info.length))
400       ThrowReaderException(CorruptImageError,"ImproperImageHeader");
401     sun_data=(unsigned char *) AcquireQuantumMemory((size_t) sun_info.length,
402       sizeof(*sun_data));
403     if (sun_data == (unsigned char *) NULL)
404       ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
405     count=(ssize_t) ReadBlob(image,sun_info.length,sun_data);
406     if ((count == 0) && (sun_info.type != RT_ENCODED))
407       ThrowReaderException(CorruptImageError,"UnableToReadImageData");
408     sun_pixels=sun_data;
409     bytes_per_line=0;
410     if (sun_info.type == RT_ENCODED)
411       {
412         size_t
413           height;
414
415         /*
416           Read run-length encoded raster pixels.
417         */
418         height=sun_info.height;
419         bytes_per_line=sun_info.width*sun_info.depth;
420         if ((height == 0) || (sun_info.width == 0) || (sun_info.depth == 0) ||
421             ((bytes_per_line/sun_info.depth) != sun_info.width))
422           ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
423         bytes_per_line+=15;
424         bytes_per_line<<=1;
425         if ((bytes_per_line >> 1) != (sun_info.width*sun_info.depth+15))
426           ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
427         bytes_per_line>>=4;
428         sun_pixels=(unsigned char *) AcquireQuantumMemory(height,
429           bytes_per_line*sizeof(*sun_pixels));
430         if (sun_pixels == (unsigned char *) NULL)
431           ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
432         (void) DecodeImage(sun_data,sun_info.length,sun_pixels,
433           bytes_per_line*height);
434         sun_data=(unsigned char *) RelinquishMagickMemory(sun_data);
435       }
436     /*
437       Convert SUN raster image to pixel packets.
438     */
439     p=sun_pixels;
440     if (sun_info.depth == 1)
441       for (y=0; y < (ssize_t) image->rows; y++)
442       {
443         q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
444         if (q == (PixelPacket *) NULL)
445           break;
446         indexes=GetAuthenticIndexQueue(image);
447         for (x=0; x < ((ssize_t) image->columns-7); x+=8)
448         {
449           for (bit=7; bit >= 0; bit--)
450             SetIndexPixelComponent(indexes+x+7-bit,((*p) & (0x01 << bit) ?
451               0x00 : 0x01));
452           p++;
453         }
454         if ((image->columns % 8) != 0)
455           {
456             for (bit=7; bit >= (ssize_t) (8-(image->columns % 8)); bit--)
457               SetIndexPixelComponent(indexes+x+7-bit,(*p) & (0x01 << bit) ?
458                 0x00 : 0x01);
459             p++;
460           }
461         if ((((image->columns/8)+(image->columns % 8 ? 1 : 0)) % 2) != 0)
462           p++;
463         if (SyncAuthenticPixels(image,exception) == MagickFalse)
464           break;
465         if (image->previous == (Image *) NULL)
466           {
467             status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y,
468               image->rows);
469             if (status == MagickFalse)
470               break;
471           }
472       }
473     else
474       if (image->storage_class == PseudoClass)
475         {
476           length=image->rows*(image->columns+image->columns % 2);
477           if (((sun_info.type == RT_ENCODED) &&
478                (length > (bytes_per_line*image->rows))) ||
479               ((sun_info.type != RT_ENCODED) && (length > sun_info.length)))
480             ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
481           for (y=0; y < (ssize_t) image->rows; y++)
482           {
483             q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
484             if (q == (PixelPacket *) NULL)
485               break;
486             indexes=GetAuthenticIndexQueue(image);
487             for (x=0; x < (ssize_t) image->columns; x++)
488               SetIndexPixelComponent(indexes+x,*p++);
489             if ((image->columns % 2) != 0)
490               p++;
491             if (SyncAuthenticPixels(image,exception) == MagickFalse)
492               break;
493             if (image->previous == (Image *) NULL)
494               {
495                 status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y,
496                 image->rows);
497                 if (status == MagickFalse)
498                   break;
499               }
500           }
501         }
502       else
503         {
504           size_t
505             bytes_per_pixel;
506
507           bytes_per_pixel=3;
508           if (image->matte != MagickFalse)
509             bytes_per_pixel++;
510           length=image->rows*((bytes_per_line*image->columns)+
511             image->columns % 2);
512           if (((sun_info.type == RT_ENCODED) &&
513                (length > (bytes_per_line*image->rows))) ||
514               ((sun_info.type != RT_ENCODED) && (length > sun_info.length)))
515             ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
516           for (y=0; y < (ssize_t) image->rows; y++)
517           {
518             q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
519             if (q == (PixelPacket *) NULL)
520               break;
521             for (x=0; x < (ssize_t) image->columns; x++)
522             {
523               if (image->matte != MagickFalse)
524                 SetOpacityPixelComponent(q,(QuantumRange-
525                   ScaleCharToQuantum(*p++)));
526               if (sun_info.type == RT_STANDARD)
527                 {
528                   SetBluePixelComponent(q,ScaleCharToQuantum(*p++));
529                   SetGreenPixelComponent(q,ScaleCharToQuantum(*p++));
530                   SetRedPixelComponent(q,ScaleCharToQuantum(*p++));
531                 }
532               else
533                 {
534                   SetRedPixelComponent(q,ScaleCharToQuantum(*p++));
535                   SetGreenPixelComponent(q,ScaleCharToQuantum(*p++));
536                   SetBluePixelComponent(q,ScaleCharToQuantum(*p++));
537                 }
538               if (image->colors != 0)
539                 {
540                   SetRedPixelComponent(q,image->colormap[(ssize_t)
541                     q->red].red);
542                   SetGreenPixelComponent(q,image->colormap[(ssize_t)
543                     q->green].green);
544                   SetBluePixelComponent(q,image->colormap[(ssize_t)
545                     q->blue].blue);
546                 }
547               q++;
548             }
549             if (((bytes_per_pixel*image->columns) % 2) != 0)
550               p++;
551             if (SyncAuthenticPixels(image,exception) == MagickFalse)
552               break;
553             if (image->previous == (Image *) NULL)
554               {
555                 status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y,
556                 image->rows);
557                 if (status == MagickFalse)
558                   break;
559               }
560           }
561         }
562     if (image->storage_class == PseudoClass)
563       (void) SyncImage(image);
564     sun_pixels=(unsigned char *) RelinquishMagickMemory(sun_pixels);
565     if (EOFBlob(image) != MagickFalse)
566       {
567         ThrowFileException(exception,CorruptImageError,"UnexpectedEndOfFile",
568           image->filename);
569         break;
570       }
571     /*
572       Proceed to next image.
573     */
574     if (image_info->number_scenes != 0)
575       if (image->scene >= (image_info->scene+image_info->number_scenes-1))
576         break;
577     sun_info.magic=ReadBlobMSBLong(image);
578     if (sun_info.magic == 0x59a66a95)
579       {
580         /*
581           Allocate next image structure.
582         */
583         AcquireNextImage(image_info,image);
584         if (GetNextImageInList(image) == (Image *) NULL)
585           {
586             image=DestroyImageList(image);
587             return((Image *) NULL);
588           }
589         image=SyncNextImageInList(image);
590         status=SetImageProgress(image,LoadImagesTag,TellBlob(image),
591           GetBlobSize(image));
592         if (status == MagickFalse)
593           break;
594       }
595   } while (sun_info.magic == 0x59a66a95);
596   (void) CloseBlob(image);
597   return(GetFirstImageInList(image));
598 }
599 \f
600 /*
601 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
602 %                                                                             %
603 %                                                                             %
604 %                                                                             %
605 %   R e g i s t e r S U N I m a g e                                           %
606 %                                                                             %
607 %                                                                             %
608 %                                                                             %
609 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
610 %
611 %  RegisterSUNImage() adds attributes for the SUN image format to
612 %  the list of supported formats.  The attributes include the image format
613 %  tag, a method to read and/or write the format, whether the format
614 %  supports the saving of more than one frame to the same file or blob,
615 %  whether the format supports native in-memory I/O, and a brief
616 %  description of the format.
617 %
618 %  The format of the RegisterSUNImage method is:
619 %
620 %      size_t RegisterSUNImage(void)
621 %
622 */
623 ModuleExport size_t RegisterSUNImage(void)
624 {
625   MagickInfo
626     *entry;
627
628   entry=SetMagickInfo("RAS");
629   entry->decoder=(DecodeImageHandler *) ReadSUNImage;
630   entry->encoder=(EncodeImageHandler *) WriteSUNImage;
631   entry->magick=(IsImageFormatHandler *) IsSUN;
632   entry->description=ConstantString("SUN Rasterfile");
633   entry->module=ConstantString("SUN");
634   (void) RegisterMagickInfo(entry);
635   entry=SetMagickInfo("SUN");
636   entry->decoder=(DecodeImageHandler *) ReadSUNImage;
637   entry->encoder=(EncodeImageHandler *) WriteSUNImage;
638   entry->description=ConstantString("SUN Rasterfile");
639   entry->module=ConstantString("SUN");
640   (void) RegisterMagickInfo(entry);
641   return(MagickImageCoderSignature);
642 }
643 \f
644 /*
645 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
646 %                                                                             %
647 %                                                                             %
648 %                                                                             %
649 %   U n r e g i s t e r S U N I m a g e                                       %
650 %                                                                             %
651 %                                                                             %
652 %                                                                             %
653 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
654 %
655 %  UnregisterSUNImage() removes format registrations made by the
656 %  SUN module from the list of supported formats.
657 %
658 %  The format of the UnregisterSUNImage method is:
659 %
660 %      UnregisterSUNImage(void)
661 %
662 */
663 ModuleExport void UnregisterSUNImage(void)
664 {
665   (void) UnregisterMagickInfo("RAS");
666   (void) UnregisterMagickInfo("SUN");
667 }
668 \f
669 /*
670 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
671 %                                                                             %
672 %                                                                             %
673 %                                                                             %
674 %   W r i t e S U N I m a g e                                                 %
675 %                                                                             %
676 %                                                                             %
677 %                                                                             %
678 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
679 %
680 %  WriteSUNImage() writes an image in the SUN rasterfile format.
681 %
682 %  The format of the WriteSUNImage method is:
683 %
684 %      MagickBooleanType WriteSUNImage(const ImageInfo *image_info,Image *image)
685 %
686 %  A description of each parameter follows.
687 %
688 %    o image_info: the image info.
689 %
690 %    o image:  The image.
691 %
692 */
693 static MagickBooleanType WriteSUNImage(const ImageInfo *image_info,Image *image)
694 {
695 #define RMT_EQUAL_RGB  1
696 #define RMT_NONE  0
697 #define RMT_RAW  2
698 #define RT_STANDARD  1
699 #define RT_FORMAT_RGB  3
700
701   typedef struct _SUNInfo
702   {
703     unsigned int
704       magic,
705       width,
706       height,
707       depth,
708       length,
709       type,
710       maptype,
711       maplength;
712   } SUNInfo;
713
714   MagickBooleanType
715     status;
716
717   MagickOffsetType
718     scene;
719
720   MagickSizeType
721     number_pixels;
722
723   register const IndexPacket
724     *indexes;
725
726   register const PixelPacket
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 (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(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,ScaleQuantumToChar(
929               image->colormap[i].red));
930           for (i=0; i < (ssize_t) image->colors; i++)
931             (void) WriteBlobByte(image,ScaleQuantumToChar(
932               image->colormap[i].green));
933           for (i=0; i < (ssize_t) image->colors; i++)
934             (void) WriteBlobByte(image,ScaleQuantumToChar(
935               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)
948                 GetIndexPixelComponent(indexes+x));
949               p++;
950             }
951             if (image->columns & 0x01)
952               (void) WriteBlobByte(image,0);  /* pad scanline */
953             if (image->previous == (Image *) NULL)
954               {
955                 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
956                   image->rows);
957                 if (status == MagickFalse)
958                   break;
959               }
960           }
961         }
962     if (GetNextImageInList(image) == (Image *) NULL)
963       break;
964     image=SyncNextImageInList(image);
965     status=SetImageProgress(image,SaveImagesTag,scene++,
966       GetImageListLength(image));
967     if (status == MagickFalse)
968       break;
969   } while (image_info->adjoin != MagickFalse);
970   (void) CloseBlob(image);
971   return(MagickTrue);
972 }