blob: 268e444e7e153878f5d6eca9b072a3136491681f [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% %
cristy1454be72011-12-19 01:52:48 +000020% Copyright 1999-2012 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*/
cristy4c08aed2011-07-01 19:47:50 +000042#include "MagickCore/studio.h"
43#include "MagickCore/attribute.h"
44#include "MagickCore/blob.h"
45#include "MagickCore/blob-private.h"
46#include "MagickCore/cache.h"
47#include "MagickCore/color.h"
48#include "MagickCore/color-private.h"
49#include "MagickCore/colormap.h"
50#include "MagickCore/colorspace.h"
cristy510d06a2011-07-06 23:43:54 +000051#include "MagickCore/colorspace-private.h"
cristy4c08aed2011-07-01 19:47:50 +000052#include "MagickCore/exception.h"
53#include "MagickCore/exception-private.h"
54#include "MagickCore/image.h"
55#include "MagickCore/image-private.h"
56#include "MagickCore/list.h"
57#include "MagickCore/magick.h"
58#include "MagickCore/memory_.h"
59#include "MagickCore/monitor.h"
60#include "MagickCore/monitor-private.h"
61#include "MagickCore/pixel-accessor.h"
62#include "MagickCore/quantum-private.h"
63#include "MagickCore/static.h"
64#include "MagickCore/string_.h"
65#include "MagickCore/module.h"
cristy3ed852e2009-09-05 21:47:34 +000066
67/*
68 Forward declarations.
69*/
70static MagickBooleanType
cristy3a37efd2011-08-28 20:31:03 +000071 WriteSUNImage(const ImageInfo *,Image *,ExceptionInfo *);
cristy3ed852e2009-09-05 21:47:34 +000072
73/*
74%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
75% %
76% %
77% %
78% I s S U N %
79% %
80% %
81% %
82%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
83%
84% IsSUN() returns MagickTrue if the image format type, identified by the
85% magick string, is SUN.
86%
87% The format of the IsSUN method is:
88%
89% MagickBooleanType IsSUN(const unsigned char *magick,const size_t length)
90%
91% A description of each parameter follows:
92%
93% o magick: compare image format pattern against these bytes.
94%
95% o length: Specifies the length of the magick string.
96%
97*/
98static MagickBooleanType IsSUN(const unsigned char *magick,const size_t length)
99{
100 if (length < 4)
101 return(MagickFalse);
102 if (memcmp(magick,"\131\246\152\225",4) == 0)
103 return(MagickTrue);
104 return(MagickFalse);
105}
106
107/*
108%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
109% %
110% %
111% %
112% D e c o d e I m a g e %
113% %
114% %
115% %
116%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
117%
118% DecodeImage unpacks the packed image pixels into runlength-encoded pixel
119% packets.
120%
121% The format of the DecodeImage method is:
122%
123% MagickBooleanType DecodeImage(const unsigned char *compressed_pixels,
124% const size_t length,unsigned char *pixels)
125%
126% A description of each parameter follows:
127%
128% o compressed_pixels: The address of a byte (8 bits) array of compressed
129% pixel data.
130%
131% o length: An integer value that is the total number of bytes of the
132% source image (as just read by ReadBlob)
133%
134% o pixels: The address of a byte (8 bits) array of pixel data created by
135% the uncompression process. The number of bytes in this array
136% must be at least equal to the number columns times the number of rows
137% of the source pixels.
138%
139*/
140static MagickBooleanType DecodeImage(const unsigned char *compressed_pixels,
141 const size_t length,unsigned char *pixels,size_t maxpixels)
142{
143 register const unsigned char
cristyc6da28e2011-04-28 01:41:35 +0000144 *l,
145 *p;
cristy3ed852e2009-09-05 21:47:34 +0000146
147 register unsigned char
148 *q;
149
150 ssize_t
151 count;
152
153 unsigned char
154 byte;
155
156 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
157 assert(compressed_pixels != (unsigned char *) NULL);
158 assert(pixels != (unsigned char *) NULL);
159 p=compressed_pixels;
160 q=pixels;
161 l=q+maxpixels;
162 while (((size_t) (p-compressed_pixels) < length) && (q < l))
163 {
164 byte=(*p++);
165 if (byte != 128U)
166 *q++=byte;
167 else
168 {
169 /*
170 Runlength-encoded packet: <count><byte>
171 */
172 count=(ssize_t) (*p++);
173 if (count > 0)
174 byte=(*p++);
175 while ((count >= 0) && (q < l))
176 {
177 *q++=byte;
178 count--;
179 }
180 }
181 }
182 return(MagickTrue);
183}
184
185/*
186%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
187% %
188% %
189% %
190% R e a d S U N I m a g e %
191% %
192% %
193% %
194%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
195%
196% ReadSUNImage() reads a SUN image file and returns it. It allocates
197% the memory necessary for the new Image structure and returns a pointer to
198% the new image.
199%
200% The format of the ReadSUNImage method is:
201%
202% Image *ReadSUNImage(const ImageInfo *image_info,ExceptionInfo *exception)
203%
204% A description of each parameter follows:
205%
206% o image_info: the image info.
207%
208% o exception: return any errors or warnings in this structure.
209%
210*/
211static Image *ReadSUNImage(const ImageInfo *image_info,ExceptionInfo *exception)
212{
213#define RMT_EQUAL_RGB 1
214#define RMT_NONE 0
215#define RMT_RAW 2
216#define RT_STANDARD 1
217#define RT_ENCODED 2
218#define RT_FORMAT_RGB 3
219
220 typedef struct _SUNInfo
221 {
222 unsigned int
223 magic,
224 width,
225 height,
226 depth,
227 length,
228 type,
229 maptype,
230 maplength;
231 } SUNInfo;
232
233 Image
234 *image;
235
236 int
237 bit;
238
cristy3ed852e2009-09-05 21:47:34 +0000239 MagickBooleanType
240 status;
241
242 MagickSizeType
243 number_pixels;
244
cristy4c08aed2011-07-01 19:47:50 +0000245 register Quantum
cristy3ed852e2009-09-05 21:47:34 +0000246 *q;
247
cristybb503372010-05-27 20:51:26 +0000248 register ssize_t
cristye13c3042011-03-03 01:30:05 +0000249 i,
250 x;
cristy3ed852e2009-09-05 21:47:34 +0000251
252 register unsigned char
253 *p;
254
255 size_t
256 length;
257
258 ssize_t
cristyc6da28e2011-04-28 01:41:35 +0000259 count,
260 y;
cristy3ed852e2009-09-05 21:47:34 +0000261
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);
cristy9950d572011-10-01 18:22:35 +0000282 image=AcquireImage(image_info,exception);
cristy3ed852e2009-09-05 21:47:34 +0000283 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");
cristyacee8122009-11-19 02:04:10 +0000312 image->depth=sun_info.depth <= 8 ? sun_info.depth :
313 MAGICKCORE_QUANTUM_DEPTH;
cristy3ed852e2009-09-05 21:47:34 +0000314 if (sun_info.depth < 24)
315 {
cristyeaedf062010-05-29 22:36:02 +0000316 size_t
317 one;
318
cristy3ed852e2009-09-05 21:47:34 +0000319 image->storage_class=PseudoClass;
320 image->colors=sun_info.maplength;
cristyeaedf062010-05-29 22:36:02 +0000321 one=1;
cristy3ed852e2009-09-05 21:47:34 +0000322 if (sun_info.maptype == RMT_NONE)
cristyeaedf062010-05-29 22:36:02 +0000323 image->colors=one << sun_info.depth;
cristy3ed852e2009-09-05 21:47:34 +0000324 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 */
cristy018f07f2011-09-04 21:15:19 +0000336 if (AcquireImageColormap(image,image->colors,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +0000337 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 */
cristy018f07f2011-09-04 21:15:19 +0000349 if (AcquireImageColormap(image,image->colors,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +0000350 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);
cristybb503372010-05-27 20:51:26 +0000356 for (i=0; i < (ssize_t) image->colors; i++)
cristy3ed852e2009-09-05 21:47:34 +0000357 image->colormap[i].red=ScaleCharToQuantum(sun_colormap[i]);
358 count=ReadBlob(image,image->colors,sun_colormap);
cristybb503372010-05-27 20:51:26 +0000359 for (i=0; i < (ssize_t) image->colors; i++)
cristy3ed852e2009-09-05 21:47:34 +0000360 image->colormap[i].green=ScaleCharToQuantum(sun_colormap[i]);
361 count=ReadBlob(image,image->colors,sun_colormap);
cristybb503372010-05-27 20:51:26 +0000362 for (i=0; i < (ssize_t) image->colors; i++)
cristy3ed852e2009-09-05 21:47:34 +0000363 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 {
cristybb503372010-05-27 20:51:26 +0000412 size_t
cristy3ed852e2009-09-05 21:47:34 +0000413 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)
cristybb503372010-05-27 20:51:26 +0000441 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +0000442 {
443 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
cristyacd2ed22011-08-30 01:44:23 +0000444 if (q == (Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +0000445 break;
cristybb503372010-05-27 20:51:26 +0000446 for (x=0; x < ((ssize_t) image->columns-7); x+=8)
cristy3ed852e2009-09-05 21:47:34 +0000447 {
448 for (bit=7; bit >= 0; bit--)
cristy4c08aed2011-07-01 19:47:50 +0000449 {
450 SetPixelIndex(image,((*p) & (0x01 << bit) ? 0x00 : 0x01),q);
cristyed231572011-07-14 02:18:59 +0000451 q+=GetPixelChannels(image);
cristy4c08aed2011-07-01 19:47:50 +0000452 }
cristy3ed852e2009-09-05 21:47:34 +0000453 p++;
454 }
455 if ((image->columns % 8) != 0)
456 {
cristybb503372010-05-27 20:51:26 +0000457 for (bit=7; bit >= (ssize_t) (8-(image->columns % 8)); bit--)
cristy4c08aed2011-07-01 19:47:50 +0000458 {
459 SetPixelIndex(image,(*p) & (0x01 << bit) ? 0x00 : 0x01,q);
cristyed231572011-07-14 02:18:59 +0000460 q+=GetPixelChannels(image);
cristy4c08aed2011-07-01 19:47:50 +0000461 }
cristy3ed852e2009-09-05 21:47:34 +0000462 p++;
463 }
464 if ((((image->columns/8)+(image->columns % 8 ? 1 : 0)) % 2) != 0)
465 p++;
466 if (SyncAuthenticPixels(image,exception) == MagickFalse)
467 break;
468 if (image->previous == (Image *) NULL)
469 {
cristycee97112010-05-28 00:44:52 +0000470 status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y,
cristye13c3042011-03-03 01:30:05 +0000471 image->rows);
cristy3ed852e2009-09-05 21:47:34 +0000472 if (status == MagickFalse)
473 break;
474 }
475 }
476 else
477 if (image->storage_class == PseudoClass)
478 {
479 length=image->rows*(image->columns+image->columns % 2);
480 if (((sun_info.type == RT_ENCODED) &&
481 (length > (bytes_per_line*image->rows))) ||
482 ((sun_info.type != RT_ENCODED) && (length > sun_info.length)))
483 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
cristybb503372010-05-27 20:51:26 +0000484 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +0000485 {
486 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
cristyacd2ed22011-08-30 01:44:23 +0000487 if (q == (Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +0000488 break;
cristybb503372010-05-27 20:51:26 +0000489 for (x=0; x < (ssize_t) image->columns; x++)
cristy4c08aed2011-07-01 19:47:50 +0000490 {
491 SetPixelIndex(image,*p++,q);
cristyed231572011-07-14 02:18:59 +0000492 q+=GetPixelChannels(image);
cristy4c08aed2011-07-01 19:47:50 +0000493 }
cristy3ed852e2009-09-05 21:47:34 +0000494 if ((image->columns % 2) != 0)
495 p++;
496 if (SyncAuthenticPixels(image,exception) == MagickFalse)
497 break;
498 if (image->previous == (Image *) NULL)
499 {
cristycee97112010-05-28 00:44:52 +0000500 status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y,
501 image->rows);
cristy3ed852e2009-09-05 21:47:34 +0000502 if (status == MagickFalse)
503 break;
504 }
505 }
506 }
507 else
508 {
cristyacee8122009-11-19 02:04:10 +0000509 size_t
510 bytes_per_pixel;
511
512 bytes_per_pixel=3;
513 if (image->matte != MagickFalse)
514 bytes_per_pixel++;
515 length=image->rows*((bytes_per_line*image->columns)+
516 image->columns % 2);
cristy3ed852e2009-09-05 21:47:34 +0000517 if (((sun_info.type == RT_ENCODED) &&
518 (length > (bytes_per_line*image->rows))) ||
519 ((sun_info.type != RT_ENCODED) && (length > sun_info.length)))
520 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
cristybb503372010-05-27 20:51:26 +0000521 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +0000522 {
523 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
cristyacd2ed22011-08-30 01:44:23 +0000524 if (q == (Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +0000525 break;
cristybb503372010-05-27 20:51:26 +0000526 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +0000527 {
528 if (image->matte != MagickFalse)
cristy4c08aed2011-07-01 19:47:50 +0000529 SetPixelAlpha(image,ScaleCharToQuantum(*p++),q);
cristy3ed852e2009-09-05 21:47:34 +0000530 if (sun_info.type == RT_STANDARD)
531 {
cristy4c08aed2011-07-01 19:47:50 +0000532 SetPixelBlue(image,ScaleCharToQuantum(*p++),q);
533 SetPixelGreen(image,ScaleCharToQuantum(*p++),q);
534 SetPixelRed(image,ScaleCharToQuantum(*p++),q);
cristy3ed852e2009-09-05 21:47:34 +0000535 }
536 else
537 {
cristy4c08aed2011-07-01 19:47:50 +0000538 SetPixelRed(image,ScaleCharToQuantum(*p++),q);
539 SetPixelGreen(image,ScaleCharToQuantum(*p++),q);
540 SetPixelBlue(image,ScaleCharToQuantum(*p++),q);
cristy3ed852e2009-09-05 21:47:34 +0000541 }
542 if (image->colors != 0)
543 {
cristy4c08aed2011-07-01 19:47:50 +0000544 SetPixelRed(image,image->colormap[(ssize_t)
545 GetPixelRed(image,q)].red,q);
546 SetPixelGreen(image,image->colormap[(ssize_t)
547 GetPixelGreen(image,q)].green,q);
548 SetPixelBlue(image,image->colormap[(ssize_t)
549 GetPixelBlue(image,q)].blue,q);
cristy3ed852e2009-09-05 21:47:34 +0000550 }
cristyed231572011-07-14 02:18:59 +0000551 q+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +0000552 }
cristyacee8122009-11-19 02:04:10 +0000553 if (((bytes_per_pixel*image->columns) % 2) != 0)
cristy3ed852e2009-09-05 21:47:34 +0000554 p++;
555 if (SyncAuthenticPixels(image,exception) == MagickFalse)
556 break;
557 if (image->previous == (Image *) NULL)
558 {
cristycee97112010-05-28 00:44:52 +0000559 status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y,
560 image->rows);
cristy3ed852e2009-09-05 21:47:34 +0000561 if (status == MagickFalse)
562 break;
563 }
564 }
565 }
566 if (image->storage_class == PseudoClass)
cristyea1a8aa2011-10-20 13:24:06 +0000567 (void) SyncImage(image,exception);
cristy3ed852e2009-09-05 21:47:34 +0000568 sun_pixels=(unsigned char *) RelinquishMagickMemory(sun_pixels);
569 if (EOFBlob(image) != MagickFalse)
570 {
571 ThrowFileException(exception,CorruptImageError,"UnexpectedEndOfFile",
572 image->filename);
573 break;
574 }
575 /*
576 Proceed to next image.
577 */
578 if (image_info->number_scenes != 0)
579 if (image->scene >= (image_info->scene+image_info->number_scenes-1))
580 break;
581 sun_info.magic=ReadBlobMSBLong(image);
582 if (sun_info.magic == 0x59a66a95)
583 {
584 /*
585 Allocate next image structure.
586 */
cristy9950d572011-10-01 18:22:35 +0000587 AcquireNextImage(image_info,image,exception);
cristy3ed852e2009-09-05 21:47:34 +0000588 if (GetNextImageInList(image) == (Image *) NULL)
589 {
590 image=DestroyImageList(image);
591 return((Image *) NULL);
592 }
593 image=SyncNextImageInList(image);
594 status=SetImageProgress(image,LoadImagesTag,TellBlob(image),
595 GetBlobSize(image));
596 if (status == MagickFalse)
597 break;
598 }
599 } while (sun_info.magic == 0x59a66a95);
600 (void) CloseBlob(image);
601 return(GetFirstImageInList(image));
602}
603
604/*
605%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
606% %
607% %
608% %
609% R e g i s t e r S U N I m a g e %
610% %
611% %
612% %
613%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
614%
615% RegisterSUNImage() adds attributes for the SUN image format to
616% the list of supported formats. The attributes include the image format
617% tag, a method to read and/or write the format, whether the format
618% supports the saving of more than one frame to the same file or blob,
619% whether the format supports native in-memory I/O, and a brief
620% description of the format.
621%
622% The format of the RegisterSUNImage method is:
623%
cristybb503372010-05-27 20:51:26 +0000624% size_t RegisterSUNImage(void)
cristy3ed852e2009-09-05 21:47:34 +0000625%
626*/
cristybb503372010-05-27 20:51:26 +0000627ModuleExport size_t RegisterSUNImage(void)
cristy3ed852e2009-09-05 21:47:34 +0000628{
629 MagickInfo
630 *entry;
631
632 entry=SetMagickInfo("RAS");
633 entry->decoder=(DecodeImageHandler *) ReadSUNImage;
634 entry->encoder=(EncodeImageHandler *) WriteSUNImage;
635 entry->magick=(IsImageFormatHandler *) IsSUN;
636 entry->description=ConstantString("SUN Rasterfile");
637 entry->module=ConstantString("SUN");
638 (void) RegisterMagickInfo(entry);
639 entry=SetMagickInfo("SUN");
640 entry->decoder=(DecodeImageHandler *) ReadSUNImage;
641 entry->encoder=(EncodeImageHandler *) WriteSUNImage;
642 entry->description=ConstantString("SUN Rasterfile");
643 entry->module=ConstantString("SUN");
644 (void) RegisterMagickInfo(entry);
645 return(MagickImageCoderSignature);
646}
647
648/*
649%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
650% %
651% %
652% %
653% U n r e g i s t e r S U N I m a g e %
654% %
655% %
656% %
657%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
658%
659% UnregisterSUNImage() removes format registrations made by the
660% SUN module from the list of supported formats.
661%
662% The format of the UnregisterSUNImage method is:
663%
664% UnregisterSUNImage(void)
665%
666*/
667ModuleExport void UnregisterSUNImage(void)
668{
669 (void) UnregisterMagickInfo("RAS");
670 (void) UnregisterMagickInfo("SUN");
671}
672
673/*
674%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
675% %
676% %
677% %
678% W r i t e S U N I m a g e %
679% %
680% %
681% %
682%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
683%
684% WriteSUNImage() writes an image in the SUN rasterfile format.
685%
686% The format of the WriteSUNImage method is:
687%
cristy3a37efd2011-08-28 20:31:03 +0000688% MagickBooleanType WriteSUNImage(const ImageInfo *image_info,
689% Image *image,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000690%
691% A description of each parameter follows.
692%
693% o image_info: the image info.
694%
695% o image: The image.
696%
cristy3a37efd2011-08-28 20:31:03 +0000697% o exception: return any errors or warnings in this structure.
698%
cristy3ed852e2009-09-05 21:47:34 +0000699*/
cristy3a37efd2011-08-28 20:31:03 +0000700static MagickBooleanType WriteSUNImage(const ImageInfo *image_info,Image *image,
701 ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000702{
703#define RMT_EQUAL_RGB 1
704#define RMT_NONE 0
705#define RMT_RAW 2
706#define RT_STANDARD 1
707#define RT_FORMAT_RGB 3
708
709 typedef struct _SUNInfo
710 {
711 unsigned int
712 magic,
713 width,
714 height,
715 depth,
716 length,
717 type,
718 maptype,
719 maplength;
720 } SUNInfo;
721
cristy3ed852e2009-09-05 21:47:34 +0000722 MagickBooleanType
723 status;
724
725 MagickOffsetType
726 scene;
727
728 MagickSizeType
729 number_pixels;
730
cristy4c08aed2011-07-01 19:47:50 +0000731 register const Quantum
cristy3ed852e2009-09-05 21:47:34 +0000732 *p;
733
cristybb503372010-05-27 20:51:26 +0000734 register ssize_t
cristye13c3042011-03-03 01:30:05 +0000735 i,
cristy3ed852e2009-09-05 21:47:34 +0000736 x;
737
cristye13c3042011-03-03 01:30:05 +0000738 ssize_t
739 y;
cristy3ed852e2009-09-05 21:47:34 +0000740
741 SUNInfo
742 sun_info;
743
744 /*
745 Open output image file.
746 */
747 assert(image_info != (const ImageInfo *) NULL);
748 assert(image_info->signature == MagickSignature);
749 assert(image != (Image *) NULL);
750 assert(image->signature == MagickSignature);
751 if (image->debug != MagickFalse)
752 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
cristy3a37efd2011-08-28 20:31:03 +0000753 assert(exception != (ExceptionInfo *) NULL);
754 assert(exception->signature == MagickSignature);
755 status=OpenBlob(image_info,image,WriteBinaryBlobMode,exception);
cristy3ed852e2009-09-05 21:47:34 +0000756 if (status == MagickFalse)
757 return(status);
758 scene=0;
759 do
760 {
761 /*
762 Initialize SUN raster file header.
763 */
cristy510d06a2011-07-06 23:43:54 +0000764 if (IsRGBColorspace(image->colorspace) == MagickFalse)
cristy8d951092012-02-08 18:54:56 +0000765 (void) TransformImageColorspace(image,sRGBColorspace,exception);
cristy3ed852e2009-09-05 21:47:34 +0000766 sun_info.magic=0x59a66a95;
767 if ((image->columns != (unsigned int) image->columns) ||
768 (image->rows != (unsigned int) image->rows))
769 ThrowWriterException(ImageError,"WidthOrHeightExceedsLimit");
770 sun_info.width=(unsigned int) image->columns;
771 sun_info.height=(unsigned int) image->rows;
772 sun_info.type=(unsigned int)
773 (image->storage_class == DirectClass ? RT_FORMAT_RGB : RT_STANDARD);
774 sun_info.maptype=RMT_NONE;
775 sun_info.maplength=0;
776 number_pixels=(MagickSizeType) image->columns*image->rows;
777 if ((4*number_pixels) != (size_t) (4*number_pixels))
778 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
779 if (image->storage_class == DirectClass)
780 {
781 /*
782 Full color SUN raster.
783 */
cristyeaedf062010-05-29 22:36:02 +0000784 sun_info.depth=(unsigned int) image->matte ? 32U : 24U;
cristy3ed852e2009-09-05 21:47:34 +0000785 sun_info.length=(unsigned int) ((image->matte ? 4 : 3)*number_pixels);
cristyeaedf062010-05-29 22:36:02 +0000786 sun_info.length+=sun_info.length & 0x01 ? (unsigned int) image->rows :
787 0;
cristy3ed852e2009-09-05 21:47:34 +0000788 }
789 else
cristy3a37efd2011-08-28 20:31:03 +0000790 if (IsImageMonochrome(image,exception) != MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +0000791 {
792 /*
793 Monochrome SUN raster.
794 */
795 sun_info.depth=1;
796 sun_info.length=(unsigned int) (((image->columns+7) >> 3)*
797 image->rows);
cristyf9cca6a2010-06-04 23:49:28 +0000798 sun_info.length+=(unsigned int) (((image->columns/8)+(image->columns %
799 8 ? 1 : 0)) % 2 ? image->rows : 0);
cristy3ed852e2009-09-05 21:47:34 +0000800 }
801 else
802 {
803 /*
804 Colormapped SUN raster.
805 */
806 sun_info.depth=8;
807 sun_info.length=(unsigned int) number_pixels;
cristyf9cca6a2010-06-04 23:49:28 +0000808 sun_info.length+=(unsigned int) (image->columns & 0x01 ? image->rows :
809 0);
cristy3ed852e2009-09-05 21:47:34 +0000810 sun_info.maptype=RMT_EQUAL_RGB;
811 sun_info.maplength=(unsigned int) (3*image->colors);
812 }
813 /*
814 Write SUN header.
815 */
816 (void) WriteBlobMSBLong(image,sun_info.magic);
817 (void) WriteBlobMSBLong(image,sun_info.width);
818 (void) WriteBlobMSBLong(image,sun_info.height);
819 (void) WriteBlobMSBLong(image,sun_info.depth);
820 (void) WriteBlobMSBLong(image,sun_info.length);
821 (void) WriteBlobMSBLong(image,sun_info.type);
822 (void) WriteBlobMSBLong(image,sun_info.maptype);
823 (void) WriteBlobMSBLong(image,sun_info.maplength);
824 /*
825 Convert MIFF to SUN raster pixels.
826 */
827 x=0;
828 y=0;
829 if (image->storage_class == DirectClass)
830 {
831 register unsigned char
832 *q;
833
834 size_t
cristyacee8122009-11-19 02:04:10 +0000835 bytes_per_pixel,
cristy3ed852e2009-09-05 21:47:34 +0000836 length;
837
838 unsigned char
839 *pixels;
840
841 /*
842 Allocate memory for pixels.
843 */
cristyacee8122009-11-19 02:04:10 +0000844 bytes_per_pixel=3;
845 if (image->matte != MagickFalse)
846 bytes_per_pixel++;
cristy3ed852e2009-09-05 21:47:34 +0000847 length=image->columns;
848 pixels=(unsigned char *) AcquireQuantumMemory(length,4*sizeof(*pixels));
849 if (pixels == (unsigned char *) NULL)
850 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
851 /*
852 Convert DirectClass packet to SUN RGB pixel.
853 */
cristybb503372010-05-27 20:51:26 +0000854 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +0000855 {
cristy3a37efd2011-08-28 20:31:03 +0000856 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
cristy4c08aed2011-07-01 19:47:50 +0000857 if (p == (const Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +0000858 break;
859 q=pixels;
cristybb503372010-05-27 20:51:26 +0000860 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +0000861 {
862 if (image->matte != MagickFalse)
cristy4c08aed2011-07-01 19:47:50 +0000863 *q++=ScaleQuantumToChar(GetPixelAlpha(image,p));
864 *q++=ScaleQuantumToChar(GetPixelRed(image,p));
865 *q++=ScaleQuantumToChar(GetPixelGreen(image,p));
866 *q++=ScaleQuantumToChar(GetPixelBlue(image,p));
cristyed231572011-07-14 02:18:59 +0000867 p+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +0000868 }
cristyacee8122009-11-19 02:04:10 +0000869 if (((bytes_per_pixel*image->columns) & 0x01) != 0)
870 *q++='\0'; /* pad scanline */
cristy3ed852e2009-09-05 21:47:34 +0000871 (void) WriteBlob(image,(size_t) (q-pixels),pixels);
872 if (image->previous == (Image *) NULL)
873 {
cristycee97112010-05-28 00:44:52 +0000874 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
875 image->rows);
cristy3ed852e2009-09-05 21:47:34 +0000876 if (status == MagickFalse)
877 break;
878 }
879 }
880 pixels=(unsigned char *) RelinquishMagickMemory(pixels);
881 }
882 else
cristy3a37efd2011-08-28 20:31:03 +0000883 if (IsImageMonochrome(image,exception) != MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +0000884 {
885 register unsigned char
886 bit,
887 byte;
888
889 /*
890 Convert PseudoClass image to a SUN monochrome image.
891 */
cristy018f07f2011-09-04 21:15:19 +0000892 (void) SetImageType(image,BilevelType,exception);
cristybb503372010-05-27 20:51:26 +0000893 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +0000894 {
cristy3a37efd2011-08-28 20:31:03 +0000895 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
cristy4c08aed2011-07-01 19:47:50 +0000896 if (p == (const Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +0000897 break;
cristy3ed852e2009-09-05 21:47:34 +0000898 bit=0;
899 byte=0;
cristybb503372010-05-27 20:51:26 +0000900 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +0000901 {
902 byte<<=1;
cristy4c08aed2011-07-01 19:47:50 +0000903 if (GetPixelIntensity(image,p) < (MagickRealType) (QuantumRange/2.0))
cristy3ed852e2009-09-05 21:47:34 +0000904 byte|=0x01;
905 bit++;
906 if (bit == 8)
907 {
908 (void) WriteBlobByte(image,byte);
909 bit=0;
910 byte=0;
911 }
cristyed231572011-07-14 02:18:59 +0000912 p+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +0000913 }
914 if (bit != 0)
915 (void) WriteBlobByte(image,(unsigned char) (byte << (8-bit)));
916 if ((((image->columns/8)+
917 (image->columns % 8 ? 1 : 0)) % 2) != 0)
918 (void) WriteBlobByte(image,0); /* pad scanline */
919 if (image->previous == (Image *) NULL)
920 {
cristycee97112010-05-28 00:44:52 +0000921 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
922 image->rows);
cristy3ed852e2009-09-05 21:47:34 +0000923 if (status == MagickFalse)
924 break;
925 }
926 }
927 }
928 else
929 {
930 /*
931 Dump colormap to file.
932 */
cristybb503372010-05-27 20:51:26 +0000933 for (i=0; i < (ssize_t) image->colors; i++)
cristyc6da28e2011-04-28 01:41:35 +0000934 (void) WriteBlobByte(image,ScaleQuantumToChar(
935 image->colormap[i].red));
cristybb503372010-05-27 20:51:26 +0000936 for (i=0; i < (ssize_t) image->colors; i++)
cristyc6da28e2011-04-28 01:41:35 +0000937 (void) WriteBlobByte(image,ScaleQuantumToChar(
938 image->colormap[i].green));
cristybb503372010-05-27 20:51:26 +0000939 for (i=0; i < (ssize_t) image->colors; i++)
cristyc6da28e2011-04-28 01:41:35 +0000940 (void) WriteBlobByte(image,ScaleQuantumToChar(
941 image->colormap[i].blue));
cristy3ed852e2009-09-05 21:47:34 +0000942 /*
943 Convert PseudoClass packet to SUN colormapped pixel.
944 */
cristybb503372010-05-27 20:51:26 +0000945 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +0000946 {
cristy3a37efd2011-08-28 20:31:03 +0000947 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
cristy4c08aed2011-07-01 19:47:50 +0000948 if (p == (const Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +0000949 break;
cristybb503372010-05-27 20:51:26 +0000950 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +0000951 {
cristyc6da28e2011-04-28 01:41:35 +0000952 (void) WriteBlobByte(image,(unsigned char)
cristy4c08aed2011-07-01 19:47:50 +0000953 GetPixelIndex(image,p));
cristyed231572011-07-14 02:18:59 +0000954 p+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +0000955 }
cristyacee8122009-11-19 02:04:10 +0000956 if (image->columns & 0x01)
cristy3ed852e2009-09-05 21:47:34 +0000957 (void) WriteBlobByte(image,0); /* pad scanline */
958 if (image->previous == (Image *) NULL)
959 {
cristycee97112010-05-28 00:44:52 +0000960 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
cristyc6da28e2011-04-28 01:41:35 +0000961 image->rows);
cristy3ed852e2009-09-05 21:47:34 +0000962 if (status == MagickFalse)
963 break;
964 }
965 }
966 }
967 if (GetNextImageInList(image) == (Image *) NULL)
968 break;
969 image=SyncNextImageInList(image);
970 status=SetImageProgress(image,SaveImagesTag,scene++,
971 GetImageListLength(image));
972 if (status == MagickFalse)
973 break;
974 } while (image_info->adjoin != MagickFalse);
975 (void) CloseBlob(image);
976 return(MagickTrue);
977}