blob: 0b51e4a39aba193c9fcd586899050f556c738bac [file] [log] [blame]
cristy3ed852e2009-09-05 21:47:34 +00001/*
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% %
cristy16af1cb2009-12-11 21:38:29 +000020% Copyright 1999-2010 ImageMagick Studio LLC, a non-profit organization %
cristy3ed852e2009-09-05 21:47:34 +000021% 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
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"
cristye7e40552010-04-24 21:34:22 +000048#include "magick/colormap.h"
cristy3ed852e2009-09-05 21:47:34 +000049#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
64/*
65 Forward declarations.
66*/
67static MagickBooleanType
68 WriteSUNImage(const ImageInfo *,Image *);
69
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*/
95static 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
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*/
137static 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
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*/
207static 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 long
236 y;
237
238 MagickBooleanType
239 status;
240
241 MagickSizeType
242 number_pixels;
243
244 register IndexPacket
245 *indexes;
246
247 register long
248 x;
249
250 register PixelPacket
251 *q;
252
253 register long
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");
cristyacee8122009-11-19 02:04:10 +0000315 image->depth=sun_info.depth <= 8 ? sun_info.depth :
316 MAGICKCORE_QUANTUM_DEPTH;
cristy3ed852e2009-09-05 21:47:34 +0000317 if (sun_info.depth < 24)
318 {
319 image->storage_class=PseudoClass;
320 image->colors=sun_info.maplength;
321 if (sun_info.maptype == RMT_NONE)
322 image->colors=1 << 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 < (long) 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 < (long) 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 < (long) 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 unsigned long
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 < (long) image->rows; y++)
441 {
442 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
443 if (q == (PixelPacket *) NULL)
444 break;
445 indexes=GetAuthenticIndexQueue(image);
446 for (x=0; x < ((long) image->columns-7); x+=8)
447 {
448 for (bit=7; bit >= 0; bit--)
449 indexes[x+7-bit]=(IndexPacket) ((*p) & (0x01 << bit) ? 0x00 : 0x01);
450 p++;
451 }
452 if ((image->columns % 8) != 0)
453 {
454 for (bit=7; bit >= (long) (8-(image->columns % 8)); bit--)
455 indexes[x+7-bit]=(IndexPacket)
456 ((*p) & (0x01 << bit) ? 0x00 : 0x01);
457 p++;
458 }
459 if ((((image->columns/8)+(image->columns % 8 ? 1 : 0)) % 2) != 0)
460 p++;
461 if (SyncAuthenticPixels(image,exception) == MagickFalse)
462 break;
463 if (image->previous == (Image *) NULL)
464 {
465 status=SetImageProgress(image,LoadImageTag,y,image->rows);
466 if (status == MagickFalse)
467 break;
468 }
469 }
470 else
471 if (image->storage_class == PseudoClass)
472 {
473 length=image->rows*(image->columns+image->columns % 2);
474 if (((sun_info.type == RT_ENCODED) &&
475 (length > (bytes_per_line*image->rows))) ||
476 ((sun_info.type != RT_ENCODED) && (length > sun_info.length)))
477 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
478 for (y=0; y < (long) image->rows; y++)
479 {
480 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
481 if (q == (PixelPacket *) NULL)
482 break;
483 indexes=GetAuthenticIndexQueue(image);
484 for (x=0; x < (long) image->columns; x++)
485 indexes[x]=(IndexPacket) (*p++);
486 if ((image->columns % 2) != 0)
487 p++;
488 if (SyncAuthenticPixels(image,exception) == MagickFalse)
489 break;
490 if (image->previous == (Image *) NULL)
491 {
492 status=SetImageProgress(image,LoadImageTag,y,image->rows);
493 if (status == MagickFalse)
494 break;
495 }
496 }
497 }
498 else
499 {
cristyacee8122009-11-19 02:04:10 +0000500 size_t
501 bytes_per_pixel;
502
503 bytes_per_pixel=3;
504 if (image->matte != MagickFalse)
505 bytes_per_pixel++;
506 length=image->rows*((bytes_per_line*image->columns)+
507 image->columns % 2);
cristy3ed852e2009-09-05 21:47:34 +0000508 if (((sun_info.type == RT_ENCODED) &&
509 (length > (bytes_per_line*image->rows))) ||
510 ((sun_info.type != RT_ENCODED) && (length > sun_info.length)))
511 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
512 for (y=0; y < (long) image->rows; y++)
513 {
514 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
515 if (q == (PixelPacket *) NULL)
516 break;
517 for (x=0; x < (long) image->columns; x++)
518 {
519 if (image->matte != MagickFalse)
520 q->opacity=(Quantum) (QuantumRange-ScaleCharToQuantum(*p++));
521 if (sun_info.type == RT_STANDARD)
522 {
523 q->blue=ScaleCharToQuantum(*p++);
524 q->green=ScaleCharToQuantum(*p++);
525 q->red=ScaleCharToQuantum(*p++);
526 }
527 else
528 {
529 q->red=ScaleCharToQuantum(*p++);
530 q->green=ScaleCharToQuantum(*p++);
531 q->blue=ScaleCharToQuantum(*p++);
532 }
533 if (image->colors != 0)
534 {
535 q->red=image->colormap[(long) q->red].red;
536 q->green=image->colormap[(long) q->green].green;
537 q->blue=image->colormap[(long) q->blue].blue;
538 }
539 q++;
540 }
cristyacee8122009-11-19 02:04:10 +0000541 if (((bytes_per_pixel*image->columns) % 2) != 0)
cristy3ed852e2009-09-05 21:47:34 +0000542 p++;
543 if (SyncAuthenticPixels(image,exception) == MagickFalse)
544 break;
545 if (image->previous == (Image *) NULL)
546 {
547 status=SetImageProgress(image,LoadImageTag,y,image->rows);
548 if (status == MagickFalse)
549 break;
550 }
551 }
552 }
553 if (image->storage_class == PseudoClass)
554 (void) SyncImage(image);
555 sun_pixels=(unsigned char *) RelinquishMagickMemory(sun_pixels);
556 if (EOFBlob(image) != MagickFalse)
557 {
558 ThrowFileException(exception,CorruptImageError,"UnexpectedEndOfFile",
559 image->filename);
560 break;
561 }
562 /*
563 Proceed to next image.
564 */
565 if (image_info->number_scenes != 0)
566 if (image->scene >= (image_info->scene+image_info->number_scenes-1))
567 break;
568 sun_info.magic=ReadBlobMSBLong(image);
569 if (sun_info.magic == 0x59a66a95)
570 {
571 /*
572 Allocate next image structure.
573 */
574 AcquireNextImage(image_info,image);
575 if (GetNextImageInList(image) == (Image *) NULL)
576 {
577 image=DestroyImageList(image);
578 return((Image *) NULL);
579 }
580 image=SyncNextImageInList(image);
581 status=SetImageProgress(image,LoadImagesTag,TellBlob(image),
582 GetBlobSize(image));
583 if (status == MagickFalse)
584 break;
585 }
586 } while (sun_info.magic == 0x59a66a95);
587 (void) CloseBlob(image);
588 return(GetFirstImageInList(image));
589}
590
591/*
592%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
593% %
594% %
595% %
596% R e g i s t e r S U N I m a g e %
597% %
598% %
599% %
600%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
601%
602% RegisterSUNImage() adds attributes for the SUN image format to
603% the list of supported formats. The attributes include the image format
604% tag, a method to read and/or write the format, whether the format
605% supports the saving of more than one frame to the same file or blob,
606% whether the format supports native in-memory I/O, and a brief
607% description of the format.
608%
609% The format of the RegisterSUNImage method is:
610%
611% unsigned long RegisterSUNImage(void)
612%
613*/
614ModuleExport unsigned long RegisterSUNImage(void)
615{
616 MagickInfo
617 *entry;
618
619 entry=SetMagickInfo("RAS");
620 entry->decoder=(DecodeImageHandler *) ReadSUNImage;
621 entry->encoder=(EncodeImageHandler *) WriteSUNImage;
622 entry->magick=(IsImageFormatHandler *) IsSUN;
623 entry->description=ConstantString("SUN Rasterfile");
624 entry->module=ConstantString("SUN");
625 (void) RegisterMagickInfo(entry);
626 entry=SetMagickInfo("SUN");
627 entry->decoder=(DecodeImageHandler *) ReadSUNImage;
628 entry->encoder=(EncodeImageHandler *) WriteSUNImage;
629 entry->description=ConstantString("SUN Rasterfile");
630 entry->module=ConstantString("SUN");
631 (void) RegisterMagickInfo(entry);
632 return(MagickImageCoderSignature);
633}
634
635/*
636%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
637% %
638% %
639% %
640% U n r e g i s t e r S U N I m a g e %
641% %
642% %
643% %
644%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
645%
646% UnregisterSUNImage() removes format registrations made by the
647% SUN module from the list of supported formats.
648%
649% The format of the UnregisterSUNImage method is:
650%
651% UnregisterSUNImage(void)
652%
653*/
654ModuleExport void UnregisterSUNImage(void)
655{
656 (void) UnregisterMagickInfo("RAS");
657 (void) UnregisterMagickInfo("SUN");
658}
659
660/*
661%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
662% %
663% %
664% %
665% W r i t e S U N I m a g e %
666% %
667% %
668% %
669%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
670%
671% WriteSUNImage() writes an image in the SUN rasterfile format.
672%
673% The format of the WriteSUNImage method is:
674%
675% MagickBooleanType WriteSUNImage(const ImageInfo *image_info,Image *image)
676%
677% A description of each parameter follows.
678%
679% o image_info: the image info.
680%
681% o image: The image.
682%
683*/
684static MagickBooleanType WriteSUNImage(const ImageInfo *image_info,Image *image)
685{
686#define RMT_EQUAL_RGB 1
687#define RMT_NONE 0
688#define RMT_RAW 2
689#define RT_STANDARD 1
690#define RT_FORMAT_RGB 3
691
692 typedef struct _SUNInfo
693 {
694 unsigned int
695 magic,
696 width,
697 height,
698 depth,
699 length,
700 type,
701 maptype,
702 maplength;
703 } SUNInfo;
704
705 long
706 y;
707
708 MagickBooleanType
709 status;
710
711 MagickOffsetType
712 scene;
713
714 MagickSizeType
715 number_pixels;
716
717 register const IndexPacket
718 *indexes;
719
720 register const PixelPacket
721 *p;
722
723 register long
724 x;
725
726 register long
727 i;
728
729 SUNInfo
730 sun_info;
731
732 /*
733 Open output image file.
734 */
735 assert(image_info != (const ImageInfo *) NULL);
736 assert(image_info->signature == MagickSignature);
737 assert(image != (Image *) NULL);
738 assert(image->signature == MagickSignature);
739 if (image->debug != MagickFalse)
740 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
741 status=OpenBlob(image_info,image,WriteBinaryBlobMode,&image->exception);
742 if (status == MagickFalse)
743 return(status);
744 scene=0;
745 do
746 {
747 /*
748 Initialize SUN raster file header.
749 */
750 if (image->colorspace != RGBColorspace)
751 (void) TransformImageColorspace(image,RGBColorspace);
752 sun_info.magic=0x59a66a95;
753 if ((image->columns != (unsigned int) image->columns) ||
754 (image->rows != (unsigned int) image->rows))
755 ThrowWriterException(ImageError,"WidthOrHeightExceedsLimit");
756 sun_info.width=(unsigned int) image->columns;
757 sun_info.height=(unsigned int) image->rows;
758 sun_info.type=(unsigned int)
759 (image->storage_class == DirectClass ? RT_FORMAT_RGB : RT_STANDARD);
760 sun_info.maptype=RMT_NONE;
761 sun_info.maplength=0;
762 number_pixels=(MagickSizeType) image->columns*image->rows;
763 if ((4*number_pixels) != (size_t) (4*number_pixels))
764 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
765 if (image->storage_class == DirectClass)
766 {
767 /*
768 Full color SUN raster.
769 */
cristyacee8122009-11-19 02:04:10 +0000770 sun_info.depth=image->matte ? 32U : 24U;
cristy3ed852e2009-09-05 21:47:34 +0000771 sun_info.length=(unsigned int) ((image->matte ? 4 : 3)*number_pixels);
cristyacee8122009-11-19 02:04:10 +0000772 sun_info.length+=sun_info.length & 0x01 ? image->rows : 0;
cristy3ed852e2009-09-05 21:47:34 +0000773 }
774 else
775 if (IsMonochromeImage(image,&image->exception))
776 {
777 /*
778 Monochrome SUN raster.
779 */
780 sun_info.depth=1;
781 sun_info.length=(unsigned int) (((image->columns+7) >> 3)*
782 image->rows);
783 sun_info.length+=((image->columns/8)+(image->columns % 8 ? 1 : 0)) %
784 2 ? image->rows : 0;
785 }
786 else
787 {
788 /*
789 Colormapped SUN raster.
790 */
791 sun_info.depth=8;
792 sun_info.length=(unsigned int) number_pixels;
793 sun_info.length+=image->columns & 0x01 ? image->rows : 0;
794 sun_info.maptype=RMT_EQUAL_RGB;
795 sun_info.maplength=(unsigned int) (3*image->colors);
796 }
797 /*
798 Write SUN header.
799 */
800 (void) WriteBlobMSBLong(image,sun_info.magic);
801 (void) WriteBlobMSBLong(image,sun_info.width);
802 (void) WriteBlobMSBLong(image,sun_info.height);
803 (void) WriteBlobMSBLong(image,sun_info.depth);
804 (void) WriteBlobMSBLong(image,sun_info.length);
805 (void) WriteBlobMSBLong(image,sun_info.type);
806 (void) WriteBlobMSBLong(image,sun_info.maptype);
807 (void) WriteBlobMSBLong(image,sun_info.maplength);
808 /*
809 Convert MIFF to SUN raster pixels.
810 */
811 x=0;
812 y=0;
813 if (image->storage_class == DirectClass)
814 {
815 register unsigned char
816 *q;
817
818 size_t
cristyacee8122009-11-19 02:04:10 +0000819 bytes_per_pixel,
cristy3ed852e2009-09-05 21:47:34 +0000820 length;
821
822 unsigned char
823 *pixels;
824
825 /*
826 Allocate memory for pixels.
827 */
cristyacee8122009-11-19 02:04:10 +0000828 bytes_per_pixel=3;
829 if (image->matte != MagickFalse)
830 bytes_per_pixel++;
cristy3ed852e2009-09-05 21:47:34 +0000831 length=image->columns;
832 pixels=(unsigned char *) AcquireQuantumMemory(length,4*sizeof(*pixels));
833 if (pixels == (unsigned char *) NULL)
834 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
835 /*
836 Convert DirectClass packet to SUN RGB pixel.
837 */
838 for (y=0; y < (long) image->rows; y++)
839 {
840 p=GetVirtualPixels(image,0,y,image->columns,1,&image->exception);
841 if (p == (const PixelPacket *) NULL)
842 break;
843 q=pixels;
844 for (x=0; x < (long) image->columns; x++)
845 {
846 if (image->matte != MagickFalse)
cristy46f08202010-01-10 04:04:21 +0000847 *q++=ScaleQuantumToChar((Quantum) (GetAlphaPixelComponent(p)));
cristyce70c172010-01-07 17:15:30 +0000848 *q++=ScaleQuantumToChar(GetRedPixelComponent(p));
849 *q++=ScaleQuantumToChar(GetGreenPixelComponent(p));
850 *q++=ScaleQuantumToChar(GetBluePixelComponent(p));
cristy3ed852e2009-09-05 21:47:34 +0000851 p++;
852 }
cristyacee8122009-11-19 02:04:10 +0000853 if (((bytes_per_pixel*image->columns) & 0x01) != 0)
854 *q++='\0'; /* pad scanline */
cristy3ed852e2009-09-05 21:47:34 +0000855 (void) WriteBlob(image,(size_t) (q-pixels),pixels);
856 if (image->previous == (Image *) NULL)
857 {
858 status=SetImageProgress(image,SaveImageTag,y,image->rows);
859 if (status == MagickFalse)
860 break;
861 }
862 }
863 pixels=(unsigned char *) RelinquishMagickMemory(pixels);
864 }
865 else
866 if (IsMonochromeImage(image,&image->exception))
867 {
868 register unsigned char
869 bit,
870 byte;
871
872 /*
873 Convert PseudoClass image to a SUN monochrome image.
874 */
875 (void) SetImageType(image,BilevelType);
876 for (y=0; y < (long) image->rows; y++)
877 {
878 p=GetVirtualPixels(image,0,y,image->columns,1,&image->exception);
879 if (p == (const PixelPacket *) NULL)
880 break;
881 indexes=GetVirtualIndexQueue(image);
882 bit=0;
883 byte=0;
884 for (x=0; x < (long) image->columns; x++)
885 {
886 byte<<=1;
887 if (PixelIntensity(p) < (MagickRealType) (QuantumRange/2.0))
888 byte|=0x01;
889 bit++;
890 if (bit == 8)
891 {
892 (void) WriteBlobByte(image,byte);
893 bit=0;
894 byte=0;
895 }
896 p++;
897 }
898 if (bit != 0)
899 (void) WriteBlobByte(image,(unsigned char) (byte << (8-bit)));
900 if ((((image->columns/8)+
901 (image->columns % 8 ? 1 : 0)) % 2) != 0)
902 (void) WriteBlobByte(image,0); /* pad scanline */
903 if (image->previous == (Image *) NULL)
904 {
905 status=SetImageProgress(image,SaveImageTag,y,image->rows);
906 if (status == MagickFalse)
907 break;
908 }
909 }
910 }
911 else
912 {
913 /*
914 Dump colormap to file.
915 */
916 for (i=0; i < (long) image->colors; i++)
917 (void) WriteBlobByte(image,
918 ScaleQuantumToChar(image->colormap[i].red));
919 for (i=0; i < (long) image->colors; i++)
920 (void) WriteBlobByte(image,
921 ScaleQuantumToChar(image->colormap[i].green));
922 for (i=0; i < (long) image->colors; i++)
923 (void) WriteBlobByte(image,
924 ScaleQuantumToChar(image->colormap[i].blue));
925 /*
926 Convert PseudoClass packet to SUN colormapped pixel.
927 */
928 for (y=0; y < (long) image->rows; y++)
929 {
930 p=GetVirtualPixels(image,0,y,image->columns,1,&image->exception);
931 if (p == (const PixelPacket *) NULL)
932 break;
933 indexes=GetVirtualIndexQueue(image);
934 for (x=0; x < (long) image->columns; x++)
935 {
936 (void) WriteBlobByte(image,(unsigned char) indexes[x]);
937 p++;
938 }
cristyacee8122009-11-19 02:04:10 +0000939 if (image->columns & 0x01)
cristy3ed852e2009-09-05 21:47:34 +0000940 (void) WriteBlobByte(image,0); /* pad scanline */
941 if (image->previous == (Image *) NULL)
942 {
943 status=SetImageProgress(image,SaveImageTag,y,image->rows);
944 if (status == MagickFalse)
945 break;
946 }
947 }
948 }
949 if (GetNextImageInList(image) == (Image *) NULL)
950 break;
951 image=SyncNextImageInList(image);
952 status=SetImageProgress(image,SaveImagesTag,scene++,
953 GetImageListLength(image));
954 if (status == MagickFalse)
955 break;
956 } while (image_info->adjoin != MagickFalse);
957 (void) CloseBlob(image);
958 return(MagickTrue);
959}