| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 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 % |
| cristy | de984cd | 2013-12-01 14:49:27 +0000 | [diff] [blame] | 16 | % Cristy % |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 17 | % July 1992 % |
| 18 | % % |
| 19 | % % |
| cristy | fe676ee | 2013-11-18 13:03:38 +0000 | [diff] [blame] | 20 | % Copyright 1999-2014 ImageMagick Studio LLC, a non-profit organization % |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 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 | |
| 39 | /* |
| 40 | Include declarations. |
| 41 | */ |
| cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 42 | #include "MagickCore/studio.h" |
| 43 | #include "MagickCore/attribute.h" |
| 44 | #include "MagickCore/blob.h" |
| 45 | #include "MagickCore/blob-private.h" |
| 46 | #include "MagickCore/cache.h" |
| 47 | #include "MagickCore/color.h" |
| 48 | #include "MagickCore/color-private.h" |
| 49 | #include "MagickCore/colormap.h" |
| 50 | #include "MagickCore/colorspace.h" |
| cristy | 510d06a | 2011-07-06 23:43:54 +0000 | [diff] [blame] | 51 | #include "MagickCore/colorspace-private.h" |
| cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 52 | #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" |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 66 | |
| 67 | /* |
| 68 | Forward declarations. |
| 69 | */ |
| 70 | static MagickBooleanType |
| cristy | 3a37efd | 2011-08-28 20:31:03 +0000 | [diff] [blame] | 71 | WriteSUNImage(const ImageInfo *,Image *,ExceptionInfo *); |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 72 | |
| 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 | */ |
| 98 | static 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 | */ |
| 140 | static MagickBooleanType DecodeImage(const unsigned char *compressed_pixels, |
| 141 | const size_t length,unsigned char *pixels,size_t maxpixels) |
| 142 | { |
| 143 | register const unsigned char |
| cristy | c6da28e | 2011-04-28 01:41:35 +0000 | [diff] [blame] | 144 | *l, |
| 145 | *p; |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 146 | |
| 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 | */ |
| 211 | static 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 | |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 239 | MagickBooleanType |
| 240 | status; |
| 241 | |
| 242 | MagickSizeType |
| 243 | number_pixels; |
| 244 | |
| cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 245 | register Quantum |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 246 | *q; |
| 247 | |
| cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 248 | register ssize_t |
| cristy | e13c304 | 2011-03-03 01:30:05 +0000 | [diff] [blame] | 249 | i, |
| 250 | x; |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 251 | |
| 252 | register unsigned char |
| 253 | *p; |
| 254 | |
| 255 | size_t |
| 256 | length; |
| 257 | |
| 258 | ssize_t |
| cristy | c6da28e | 2011-04-28 01:41:35 +0000 | [diff] [blame] | 259 | count, |
| 260 | y; |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 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); |
| cristy | 9950d57 | 2011-10-01 18:22:35 +0000 | [diff] [blame] | 282 | image=AcquireImage(image_info,exception); |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 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"); |
| cristy | acee812 | 2009-11-19 02:04:10 +0000 | [diff] [blame] | 312 | image->depth=sun_info.depth <= 8 ? sun_info.depth : |
| 313 | MAGICKCORE_QUANTUM_DEPTH; |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 314 | if (sun_info.depth < 24) |
| 315 | { |
| cristy | eaedf06 | 2010-05-29 22:36:02 +0000 | [diff] [blame] | 316 | size_t |
| 317 | one; |
| 318 | |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 319 | image->storage_class=PseudoClass; |
| 320 | image->colors=sun_info.maplength; |
| cristy | eaedf06 | 2010-05-29 22:36:02 +0000 | [diff] [blame] | 321 | one=1; |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 322 | if (sun_info.maptype == RMT_NONE) |
| cristy | eaedf06 | 2010-05-29 22:36:02 +0000 | [diff] [blame] | 323 | image->colors=one << sun_info.depth; |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 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 | */ |
| cristy | 018f07f | 2011-09-04 21:15:19 +0000 | [diff] [blame] | 336 | if (AcquireImageColormap(image,image->colors,exception) == MagickFalse) |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 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 | */ |
| cristy | 018f07f | 2011-09-04 21:15:19 +0000 | [diff] [blame] | 349 | if (AcquireImageColormap(image,image->colors,exception) == MagickFalse) |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 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); |
| cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 356 | for (i=0; i < (ssize_t) image->colors; i++) |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 357 | image->colormap[i].red=ScaleCharToQuantum(sun_colormap[i]); |
| 358 | count=ReadBlob(image,image->colors,sun_colormap); |
| cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 359 | for (i=0; i < (ssize_t) image->colors; i++) |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 360 | image->colormap[i].green=ScaleCharToQuantum(sun_colormap[i]); |
| 361 | count=ReadBlob(image,image->colors,sun_colormap); |
| cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 362 | for (i=0; i < (ssize_t) image->colors; i++) |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 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 | } |
| cristy | b0a657e | 2012-08-29 00:45:37 +0000 | [diff] [blame] | 386 | image->alpha_trait=sun_info.depth == 32 ? BlendPixelTrait : |
| 387 | UndefinedPixelTrait; |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 388 | image->columns=sun_info.width; |
| 389 | image->rows=sun_info.height; |
| 390 | if (image_info->ping != MagickFalse) |
| 391 | { |
| 392 | (void) CloseBlob(image); |
| 393 | return(GetFirstImageInList(image)); |
| 394 | } |
| 395 | if ((sun_info.length*sizeof(*sun_data))/sizeof(*sun_data) != |
| 396 | sun_info.length || !sun_info.length) |
| 397 | ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed"); |
| 398 | number_pixels=(MagickSizeType) image->columns*image->rows; |
| cristy | 815b2dd | 2012-09-21 11:22:29 +0000 | [diff] [blame] | 399 | if ((sun_info.type != RT_ENCODED) && (sun_info.depth >= 8) && |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 400 | ((number_pixels*((sun_info.depth+7)/8)) > sun_info.length)) |
| 401 | ThrowReaderException(CorruptImageError,"ImproperImageHeader"); |
| 402 | sun_data=(unsigned char *) AcquireQuantumMemory((size_t) sun_info.length, |
| 403 | sizeof(*sun_data)); |
| 404 | if (sun_data == (unsigned char *) NULL) |
| 405 | ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed"); |
| 406 | count=(ssize_t) ReadBlob(image,sun_info.length,sun_data); |
| 407 | if ((count == 0) && (sun_info.type != RT_ENCODED)) |
| 408 | ThrowReaderException(CorruptImageError,"UnableToReadImageData"); |
| 409 | sun_pixels=sun_data; |
| 410 | bytes_per_line=0; |
| 411 | if (sun_info.type == RT_ENCODED) |
| 412 | { |
| cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 413 | size_t |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 414 | height; |
| 415 | |
| 416 | /* |
| 417 | Read run-length encoded raster pixels. |
| 418 | */ |
| 419 | height=sun_info.height; |
| 420 | bytes_per_line=sun_info.width*sun_info.depth; |
| 421 | if ((height == 0) || (sun_info.width == 0) || (sun_info.depth == 0) || |
| 422 | ((bytes_per_line/sun_info.depth) != sun_info.width)) |
| 423 | ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed"); |
| 424 | bytes_per_line+=15; |
| 425 | bytes_per_line<<=1; |
| 426 | if ((bytes_per_line >> 1) != (sun_info.width*sun_info.depth+15)) |
| 427 | ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed"); |
| 428 | bytes_per_line>>=4; |
| 429 | sun_pixels=(unsigned char *) AcquireQuantumMemory(height, |
| 430 | bytes_per_line*sizeof(*sun_pixels)); |
| 431 | if (sun_pixels == (unsigned char *) NULL) |
| 432 | ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed"); |
| 433 | (void) DecodeImage(sun_data,sun_info.length,sun_pixels, |
| 434 | bytes_per_line*height); |
| 435 | sun_data=(unsigned char *) RelinquishMagickMemory(sun_data); |
| 436 | } |
| 437 | /* |
| 438 | Convert SUN raster image to pixel packets. |
| 439 | */ |
| 440 | p=sun_pixels; |
| 441 | if (sun_info.depth == 1) |
| cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 442 | for (y=0; y < (ssize_t) image->rows; y++) |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 443 | { |
| 444 | q=QueueAuthenticPixels(image,0,y,image->columns,1,exception); |
| cristy | acd2ed2 | 2011-08-30 01:44:23 +0000 | [diff] [blame] | 445 | if (q == (Quantum *) NULL) |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 446 | break; |
| cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 447 | for (x=0; x < ((ssize_t) image->columns-7); x+=8) |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 448 | { |
| 449 | for (bit=7; bit >= 0; bit--) |
| cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 450 | { |
| 451 | SetPixelIndex(image,((*p) & (0x01 << bit) ? 0x00 : 0x01),q); |
| cristy | ed23157 | 2011-07-14 02:18:59 +0000 | [diff] [blame] | 452 | q+=GetPixelChannels(image); |
| cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 453 | } |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 454 | p++; |
| 455 | } |
| 456 | if ((image->columns % 8) != 0) |
| 457 | { |
| cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 458 | for (bit=7; bit >= (ssize_t) (8-(image->columns % 8)); bit--) |
| cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 459 | { |
| 460 | SetPixelIndex(image,(*p) & (0x01 << bit) ? 0x00 : 0x01,q); |
| cristy | ed23157 | 2011-07-14 02:18:59 +0000 | [diff] [blame] | 461 | q+=GetPixelChannels(image); |
| cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 462 | } |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 463 | p++; |
| 464 | } |
| 465 | if ((((image->columns/8)+(image->columns % 8 ? 1 : 0)) % 2) != 0) |
| 466 | p++; |
| 467 | if (SyncAuthenticPixels(image,exception) == MagickFalse) |
| 468 | break; |
| 469 | if (image->previous == (Image *) NULL) |
| 470 | { |
| cristy | cee9711 | 2010-05-28 00:44:52 +0000 | [diff] [blame] | 471 | status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y, |
| cristy | e13c304 | 2011-03-03 01:30:05 +0000 | [diff] [blame] | 472 | image->rows); |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 473 | if (status == MagickFalse) |
| 474 | break; |
| 475 | } |
| 476 | } |
| 477 | else |
| 478 | if (image->storage_class == PseudoClass) |
| 479 | { |
| 480 | length=image->rows*(image->columns+image->columns % 2); |
| 481 | if (((sun_info.type == RT_ENCODED) && |
| 482 | (length > (bytes_per_line*image->rows))) || |
| 483 | ((sun_info.type != RT_ENCODED) && (length > sun_info.length))) |
| 484 | ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed"); |
| cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 485 | for (y=0; y < (ssize_t) image->rows; y++) |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 486 | { |
| 487 | q=QueueAuthenticPixels(image,0,y,image->columns,1,exception); |
| cristy | acd2ed2 | 2011-08-30 01:44:23 +0000 | [diff] [blame] | 488 | if (q == (Quantum *) NULL) |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 489 | break; |
| cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 490 | for (x=0; x < (ssize_t) image->columns; x++) |
| cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 491 | { |
| 492 | SetPixelIndex(image,*p++,q); |
| cristy | ed23157 | 2011-07-14 02:18:59 +0000 | [diff] [blame] | 493 | q+=GetPixelChannels(image); |
| cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 494 | } |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 495 | if ((image->columns % 2) != 0) |
| 496 | p++; |
| 497 | if (SyncAuthenticPixels(image,exception) == MagickFalse) |
| 498 | break; |
| 499 | if (image->previous == (Image *) NULL) |
| 500 | { |
| cristy | cee9711 | 2010-05-28 00:44:52 +0000 | [diff] [blame] | 501 | status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y, |
| 502 | image->rows); |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 503 | if (status == MagickFalse) |
| 504 | break; |
| 505 | } |
| 506 | } |
| 507 | } |
| 508 | else |
| 509 | { |
| cristy | acee812 | 2009-11-19 02:04:10 +0000 | [diff] [blame] | 510 | size_t |
| 511 | bytes_per_pixel; |
| 512 | |
| 513 | bytes_per_pixel=3; |
| cristy | 7d6d3d8 | 2014-11-09 17:00:16 +0000 | [diff] [blame] | 514 | if (image->alpha_trait != UndefinedPixelTrait) |
| cristy | acee812 | 2009-11-19 02:04:10 +0000 | [diff] [blame] | 515 | bytes_per_pixel++; |
| 516 | length=image->rows*((bytes_per_line*image->columns)+ |
| 517 | image->columns % 2); |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 518 | if (((sun_info.type == RT_ENCODED) && |
| 519 | (length > (bytes_per_line*image->rows))) || |
| 520 | ((sun_info.type != RT_ENCODED) && (length > sun_info.length))) |
| 521 | ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed"); |
| cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 522 | for (y=0; y < (ssize_t) image->rows; y++) |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 523 | { |
| 524 | q=QueueAuthenticPixels(image,0,y,image->columns,1,exception); |
| cristy | acd2ed2 | 2011-08-30 01:44:23 +0000 | [diff] [blame] | 525 | if (q == (Quantum *) NULL) |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 526 | break; |
| cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 527 | for (x=0; x < (ssize_t) image->columns; x++) |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 528 | { |
| cristy | 7d6d3d8 | 2014-11-09 17:00:16 +0000 | [diff] [blame] | 529 | if (image->alpha_trait != UndefinedPixelTrait) |
| cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 530 | SetPixelAlpha(image,ScaleCharToQuantum(*p++),q); |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 531 | if (sun_info.type == RT_STANDARD) |
| 532 | { |
| cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 533 | SetPixelBlue(image,ScaleCharToQuantum(*p++),q); |
| 534 | SetPixelGreen(image,ScaleCharToQuantum(*p++),q); |
| 535 | SetPixelRed(image,ScaleCharToQuantum(*p++),q); |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 536 | } |
| 537 | else |
| 538 | { |
| cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 539 | SetPixelRed(image,ScaleCharToQuantum(*p++),q); |
| 540 | SetPixelGreen(image,ScaleCharToQuantum(*p++),q); |
| 541 | SetPixelBlue(image,ScaleCharToQuantum(*p++),q); |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 542 | } |
| 543 | if (image->colors != 0) |
| 544 | { |
| cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 545 | SetPixelRed(image,image->colormap[(ssize_t) |
| 546 | GetPixelRed(image,q)].red,q); |
| 547 | SetPixelGreen(image,image->colormap[(ssize_t) |
| 548 | GetPixelGreen(image,q)].green,q); |
| 549 | SetPixelBlue(image,image->colormap[(ssize_t) |
| 550 | GetPixelBlue(image,q)].blue,q); |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 551 | } |
| cristy | ed23157 | 2011-07-14 02:18:59 +0000 | [diff] [blame] | 552 | q+=GetPixelChannels(image); |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 553 | } |
| cristy | acee812 | 2009-11-19 02:04:10 +0000 | [diff] [blame] | 554 | if (((bytes_per_pixel*image->columns) % 2) != 0) |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 555 | p++; |
| 556 | if (SyncAuthenticPixels(image,exception) == MagickFalse) |
| 557 | break; |
| 558 | if (image->previous == (Image *) NULL) |
| 559 | { |
| cristy | cee9711 | 2010-05-28 00:44:52 +0000 | [diff] [blame] | 560 | status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y, |
| 561 | image->rows); |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 562 | if (status == MagickFalse) |
| 563 | break; |
| 564 | } |
| 565 | } |
| 566 | } |
| 567 | if (image->storage_class == PseudoClass) |
| cristy | ea1a8aa | 2011-10-20 13:24:06 +0000 | [diff] [blame] | 568 | (void) SyncImage(image,exception); |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 569 | sun_pixels=(unsigned char *) RelinquishMagickMemory(sun_pixels); |
| 570 | if (EOFBlob(image) != MagickFalse) |
| 571 | { |
| 572 | ThrowFileException(exception,CorruptImageError,"UnexpectedEndOfFile", |
| 573 | image->filename); |
| 574 | break; |
| 575 | } |
| 576 | /* |
| 577 | Proceed to next image. |
| 578 | */ |
| 579 | if (image_info->number_scenes != 0) |
| 580 | if (image->scene >= (image_info->scene+image_info->number_scenes-1)) |
| 581 | break; |
| 582 | sun_info.magic=ReadBlobMSBLong(image); |
| 583 | if (sun_info.magic == 0x59a66a95) |
| 584 | { |
| 585 | /* |
| 586 | Allocate next image structure. |
| 587 | */ |
| cristy | 9950d57 | 2011-10-01 18:22:35 +0000 | [diff] [blame] | 588 | AcquireNextImage(image_info,image,exception); |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 589 | if (GetNextImageInList(image) == (Image *) NULL) |
| 590 | { |
| 591 | image=DestroyImageList(image); |
| 592 | return((Image *) NULL); |
| 593 | } |
| 594 | image=SyncNextImageInList(image); |
| 595 | status=SetImageProgress(image,LoadImagesTag,TellBlob(image), |
| 596 | GetBlobSize(image)); |
| 597 | if (status == MagickFalse) |
| 598 | break; |
| 599 | } |
| 600 | } while (sun_info.magic == 0x59a66a95); |
| 601 | (void) CloseBlob(image); |
| 602 | return(GetFirstImageInList(image)); |
| 603 | } |
| 604 | |
| 605 | /* |
| 606 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 607 | % % |
| 608 | % % |
| 609 | % % |
| 610 | % R e g i s t e r S U N I m a g e % |
| 611 | % % |
| 612 | % % |
| 613 | % % |
| 614 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 615 | % |
| 616 | % RegisterSUNImage() adds attributes for the SUN image format to |
| 617 | % the list of supported formats. The attributes include the image format |
| 618 | % tag, a method to read and/or write the format, whether the format |
| 619 | % supports the saving of more than one frame to the same file or blob, |
| 620 | % whether the format supports native in-memory I/O, and a brief |
| 621 | % description of the format. |
| 622 | % |
| 623 | % The format of the RegisterSUNImage method is: |
| 624 | % |
| cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 625 | % size_t RegisterSUNImage(void) |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 626 | % |
| 627 | */ |
| cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 628 | ModuleExport size_t RegisterSUNImage(void) |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 629 | { |
| 630 | MagickInfo |
| 631 | *entry; |
| 632 | |
| 633 | entry=SetMagickInfo("RAS"); |
| 634 | entry->decoder=(DecodeImageHandler *) ReadSUNImage; |
| 635 | entry->encoder=(EncodeImageHandler *) WriteSUNImage; |
| 636 | entry->magick=(IsImageFormatHandler *) IsSUN; |
| 637 | entry->description=ConstantString("SUN Rasterfile"); |
| 638 | entry->module=ConstantString("SUN"); |
| 639 | (void) RegisterMagickInfo(entry); |
| 640 | entry=SetMagickInfo("SUN"); |
| 641 | entry->decoder=(DecodeImageHandler *) ReadSUNImage; |
| 642 | entry->encoder=(EncodeImageHandler *) WriteSUNImage; |
| 643 | entry->description=ConstantString("SUN Rasterfile"); |
| 644 | entry->module=ConstantString("SUN"); |
| 645 | (void) RegisterMagickInfo(entry); |
| 646 | return(MagickImageCoderSignature); |
| 647 | } |
| 648 | |
| 649 | /* |
| 650 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 651 | % % |
| 652 | % % |
| 653 | % % |
| 654 | % U n r e g i s t e r S U N I m a g e % |
| 655 | % % |
| 656 | % % |
| 657 | % % |
| 658 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 659 | % |
| 660 | % UnregisterSUNImage() removes format registrations made by the |
| 661 | % SUN module from the list of supported formats. |
| 662 | % |
| 663 | % The format of the UnregisterSUNImage method is: |
| 664 | % |
| 665 | % UnregisterSUNImage(void) |
| 666 | % |
| 667 | */ |
| 668 | ModuleExport void UnregisterSUNImage(void) |
| 669 | { |
| 670 | (void) UnregisterMagickInfo("RAS"); |
| 671 | (void) UnregisterMagickInfo("SUN"); |
| 672 | } |
| 673 | |
| 674 | /* |
| 675 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 676 | % % |
| 677 | % % |
| 678 | % % |
| 679 | % W r i t e S U N I m a g e % |
| 680 | % % |
| 681 | % % |
| 682 | % % |
| 683 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 684 | % |
| 685 | % WriteSUNImage() writes an image in the SUN rasterfile format. |
| 686 | % |
| 687 | % The format of the WriteSUNImage method is: |
| 688 | % |
| cristy | 3a37efd | 2011-08-28 20:31:03 +0000 | [diff] [blame] | 689 | % MagickBooleanType WriteSUNImage(const ImageInfo *image_info, |
| 690 | % Image *image,ExceptionInfo *exception) |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 691 | % |
| 692 | % A description of each parameter follows. |
| 693 | % |
| 694 | % o image_info: the image info. |
| 695 | % |
| 696 | % o image: The image. |
| 697 | % |
| cristy | 3a37efd | 2011-08-28 20:31:03 +0000 | [diff] [blame] | 698 | % o exception: return any errors or warnings in this structure. |
| 699 | % |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 700 | */ |
| cristy | 3a37efd | 2011-08-28 20:31:03 +0000 | [diff] [blame] | 701 | static MagickBooleanType WriteSUNImage(const ImageInfo *image_info,Image *image, |
| 702 | ExceptionInfo *exception) |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 703 | { |
| 704 | #define RMT_EQUAL_RGB 1 |
| 705 | #define RMT_NONE 0 |
| 706 | #define RMT_RAW 2 |
| 707 | #define RT_STANDARD 1 |
| 708 | #define RT_FORMAT_RGB 3 |
| 709 | |
| 710 | typedef struct _SUNInfo |
| 711 | { |
| 712 | unsigned int |
| 713 | magic, |
| 714 | width, |
| 715 | height, |
| 716 | depth, |
| 717 | length, |
| 718 | type, |
| 719 | maptype, |
| 720 | maplength; |
| 721 | } SUNInfo; |
| 722 | |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 723 | MagickBooleanType |
| 724 | status; |
| 725 | |
| 726 | MagickOffsetType |
| 727 | scene; |
| 728 | |
| 729 | MagickSizeType |
| 730 | number_pixels; |
| 731 | |
| cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 732 | register const Quantum |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 733 | *p; |
| 734 | |
| cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 735 | register ssize_t |
| cristy | e13c304 | 2011-03-03 01:30:05 +0000 | [diff] [blame] | 736 | i, |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 737 | x; |
| 738 | |
| cristy | e13c304 | 2011-03-03 01:30:05 +0000 | [diff] [blame] | 739 | ssize_t |
| 740 | y; |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 741 | |
| 742 | SUNInfo |
| 743 | sun_info; |
| 744 | |
| 745 | /* |
| 746 | Open output image file. |
| 747 | */ |
| 748 | assert(image_info != (const ImageInfo *) NULL); |
| 749 | assert(image_info->signature == MagickSignature); |
| 750 | assert(image != (Image *) NULL); |
| 751 | assert(image->signature == MagickSignature); |
| 752 | if (image->debug != MagickFalse) |
| 753 | (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename); |
| cristy | 3a37efd | 2011-08-28 20:31:03 +0000 | [diff] [blame] | 754 | assert(exception != (ExceptionInfo *) NULL); |
| 755 | assert(exception->signature == MagickSignature); |
| 756 | status=OpenBlob(image_info,image,WriteBinaryBlobMode,exception); |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 757 | if (status == MagickFalse) |
| 758 | return(status); |
| 759 | scene=0; |
| 760 | do |
| 761 | { |
| 762 | /* |
| 763 | Initialize SUN raster file header. |
| 764 | */ |
| cristy | af8d391 | 2014-02-21 14:50:33 +0000 | [diff] [blame] | 765 | (void) TransformImageColorspace(image,sRGBColorspace,exception); |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 766 | 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 | */ |
| cristy | 7d6d3d8 | 2014-11-09 17:00:16 +0000 | [diff] [blame] | 784 | sun_info.depth=(unsigned int) image->alpha_trait != UndefinedPixelTrait ? |
| cristy | dc2d327 | 2013-02-12 14:00:44 +0000 | [diff] [blame] | 785 | 32U : 24U; |
| cristy | 7d6d3d8 | 2014-11-09 17:00:16 +0000 | [diff] [blame] | 786 | sun_info.length=(unsigned int) ((image->alpha_trait != UndefinedPixelTrait ? |
| cristy | dc2d327 | 2013-02-12 14:00:44 +0000 | [diff] [blame] | 787 | 4 : 3)*number_pixels); |
| cristy | eaedf06 | 2010-05-29 22:36:02 +0000 | [diff] [blame] | 788 | sun_info.length+=sun_info.length & 0x01 ? (unsigned int) image->rows : |
| 789 | 0; |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 790 | } |
| 791 | else |
| cristy | 3a37efd | 2011-08-28 20:31:03 +0000 | [diff] [blame] | 792 | if (IsImageMonochrome(image,exception) != MagickFalse) |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 793 | { |
| 794 | /* |
| 795 | Monochrome SUN raster. |
| 796 | */ |
| 797 | sun_info.depth=1; |
| 798 | sun_info.length=(unsigned int) (((image->columns+7) >> 3)* |
| 799 | image->rows); |
| cristy | f9cca6a | 2010-06-04 23:49:28 +0000 | [diff] [blame] | 800 | sun_info.length+=(unsigned int) (((image->columns/8)+(image->columns % |
| 801 | 8 ? 1 : 0)) % 2 ? image->rows : 0); |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 802 | } |
| 803 | else |
| 804 | { |
| 805 | /* |
| 806 | Colormapped SUN raster. |
| 807 | */ |
| 808 | sun_info.depth=8; |
| 809 | sun_info.length=(unsigned int) number_pixels; |
| cristy | f9cca6a | 2010-06-04 23:49:28 +0000 | [diff] [blame] | 810 | sun_info.length+=(unsigned int) (image->columns & 0x01 ? image->rows : |
| 811 | 0); |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 812 | sun_info.maptype=RMT_EQUAL_RGB; |
| 813 | sun_info.maplength=(unsigned int) (3*image->colors); |
| 814 | } |
| 815 | /* |
| 816 | Write SUN header. |
| 817 | */ |
| 818 | (void) WriteBlobMSBLong(image,sun_info.magic); |
| 819 | (void) WriteBlobMSBLong(image,sun_info.width); |
| 820 | (void) WriteBlobMSBLong(image,sun_info.height); |
| 821 | (void) WriteBlobMSBLong(image,sun_info.depth); |
| 822 | (void) WriteBlobMSBLong(image,sun_info.length); |
| 823 | (void) WriteBlobMSBLong(image,sun_info.type); |
| 824 | (void) WriteBlobMSBLong(image,sun_info.maptype); |
| 825 | (void) WriteBlobMSBLong(image,sun_info.maplength); |
| 826 | /* |
| 827 | Convert MIFF to SUN raster pixels. |
| 828 | */ |
| 829 | x=0; |
| 830 | y=0; |
| 831 | if (image->storage_class == DirectClass) |
| 832 | { |
| 833 | register unsigned char |
| 834 | *q; |
| 835 | |
| 836 | size_t |
| cristy | acee812 | 2009-11-19 02:04:10 +0000 | [diff] [blame] | 837 | bytes_per_pixel, |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 838 | length; |
| 839 | |
| 840 | unsigned char |
| 841 | *pixels; |
| 842 | |
| 843 | /* |
| 844 | Allocate memory for pixels. |
| 845 | */ |
| cristy | acee812 | 2009-11-19 02:04:10 +0000 | [diff] [blame] | 846 | bytes_per_pixel=3; |
| cristy | 7d6d3d8 | 2014-11-09 17:00:16 +0000 | [diff] [blame] | 847 | if (image->alpha_trait != UndefinedPixelTrait) |
| cristy | acee812 | 2009-11-19 02:04:10 +0000 | [diff] [blame] | 848 | bytes_per_pixel++; |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 849 | length=image->columns; |
| 850 | pixels=(unsigned char *) AcquireQuantumMemory(length,4*sizeof(*pixels)); |
| 851 | if (pixels == (unsigned char *) NULL) |
| 852 | ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed"); |
| 853 | /* |
| 854 | Convert DirectClass packet to SUN RGB pixel. |
| 855 | */ |
| cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 856 | for (y=0; y < (ssize_t) image->rows; y++) |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 857 | { |
| cristy | 3a37efd | 2011-08-28 20:31:03 +0000 | [diff] [blame] | 858 | p=GetVirtualPixels(image,0,y,image->columns,1,exception); |
| cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 859 | if (p == (const Quantum *) NULL) |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 860 | break; |
| 861 | q=pixels; |
| cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 862 | for (x=0; x < (ssize_t) image->columns; x++) |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 863 | { |
| cristy | 7d6d3d8 | 2014-11-09 17:00:16 +0000 | [diff] [blame] | 864 | if (image->alpha_trait != UndefinedPixelTrait) |
| cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 865 | *q++=ScaleQuantumToChar(GetPixelAlpha(image,p)); |
| 866 | *q++=ScaleQuantumToChar(GetPixelRed(image,p)); |
| 867 | *q++=ScaleQuantumToChar(GetPixelGreen(image,p)); |
| 868 | *q++=ScaleQuantumToChar(GetPixelBlue(image,p)); |
| cristy | ed23157 | 2011-07-14 02:18:59 +0000 | [diff] [blame] | 869 | p+=GetPixelChannels(image); |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 870 | } |
| cristy | acee812 | 2009-11-19 02:04:10 +0000 | [diff] [blame] | 871 | if (((bytes_per_pixel*image->columns) & 0x01) != 0) |
| 872 | *q++='\0'; /* pad scanline */ |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 873 | (void) WriteBlob(image,(size_t) (q-pixels),pixels); |
| 874 | if (image->previous == (Image *) NULL) |
| 875 | { |
| cristy | cee9711 | 2010-05-28 00:44:52 +0000 | [diff] [blame] | 876 | status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y, |
| 877 | image->rows); |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 878 | if (status == MagickFalse) |
| 879 | break; |
| 880 | } |
| 881 | } |
| 882 | pixels=(unsigned char *) RelinquishMagickMemory(pixels); |
| 883 | } |
| 884 | else |
| cristy | 3a37efd | 2011-08-28 20:31:03 +0000 | [diff] [blame] | 885 | if (IsImageMonochrome(image,exception) != MagickFalse) |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 886 | { |
| 887 | register unsigned char |
| 888 | bit, |
| 889 | byte; |
| 890 | |
| 891 | /* |
| 892 | Convert PseudoClass image to a SUN monochrome image. |
| 893 | */ |
| cristy | 018f07f | 2011-09-04 21:15:19 +0000 | [diff] [blame] | 894 | (void) SetImageType(image,BilevelType,exception); |
| cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 895 | for (y=0; y < (ssize_t) image->rows; y++) |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 896 | { |
| cristy | 3a37efd | 2011-08-28 20:31:03 +0000 | [diff] [blame] | 897 | p=GetVirtualPixels(image,0,y,image->columns,1,exception); |
| cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 898 | if (p == (const Quantum *) NULL) |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 899 | break; |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 900 | bit=0; |
| 901 | byte=0; |
| cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 902 | for (x=0; x < (ssize_t) image->columns; x++) |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 903 | { |
| 904 | byte<<=1; |
| cristy | d032322 | 2013-04-07 16:13:21 +0000 | [diff] [blame] | 905 | if (GetPixelLuma(image,p) < (QuantumRange/2.0)) |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 906 | byte|=0x01; |
| 907 | bit++; |
| 908 | if (bit == 8) |
| 909 | { |
| 910 | (void) WriteBlobByte(image,byte); |
| 911 | bit=0; |
| 912 | byte=0; |
| 913 | } |
| cristy | ed23157 | 2011-07-14 02:18:59 +0000 | [diff] [blame] | 914 | p+=GetPixelChannels(image); |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 915 | } |
| 916 | if (bit != 0) |
| 917 | (void) WriteBlobByte(image,(unsigned char) (byte << (8-bit))); |
| 918 | if ((((image->columns/8)+ |
| 919 | (image->columns % 8 ? 1 : 0)) % 2) != 0) |
| 920 | (void) WriteBlobByte(image,0); /* pad scanline */ |
| 921 | if (image->previous == (Image *) NULL) |
| 922 | { |
| cristy | cee9711 | 2010-05-28 00:44:52 +0000 | [diff] [blame] | 923 | status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y, |
| 924 | image->rows); |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 925 | if (status == MagickFalse) |
| 926 | break; |
| 927 | } |
| 928 | } |
| 929 | } |
| 930 | else |
| 931 | { |
| 932 | /* |
| 933 | Dump colormap to file. |
| 934 | */ |
| cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 935 | for (i=0; i < (ssize_t) image->colors; i++) |
| cristy | c6da28e | 2011-04-28 01:41:35 +0000 | [diff] [blame] | 936 | (void) WriteBlobByte(image,ScaleQuantumToChar( |
| 937 | image->colormap[i].red)); |
| cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 938 | for (i=0; i < (ssize_t) image->colors; i++) |
| cristy | c6da28e | 2011-04-28 01:41:35 +0000 | [diff] [blame] | 939 | (void) WriteBlobByte(image,ScaleQuantumToChar( |
| 940 | image->colormap[i].green)); |
| cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 941 | for (i=0; i < (ssize_t) image->colors; i++) |
| cristy | c6da28e | 2011-04-28 01:41:35 +0000 | [diff] [blame] | 942 | (void) WriteBlobByte(image,ScaleQuantumToChar( |
| 943 | image->colormap[i].blue)); |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 944 | /* |
| 945 | Convert PseudoClass packet to SUN colormapped pixel. |
| 946 | */ |
| cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 947 | for (y=0; y < (ssize_t) image->rows; y++) |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 948 | { |
| cristy | 3a37efd | 2011-08-28 20:31:03 +0000 | [diff] [blame] | 949 | p=GetVirtualPixels(image,0,y,image->columns,1,exception); |
| cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 950 | if (p == (const Quantum *) NULL) |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 951 | break; |
| cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 952 | for (x=0; x < (ssize_t) image->columns; x++) |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 953 | { |
| cristy | c6da28e | 2011-04-28 01:41:35 +0000 | [diff] [blame] | 954 | (void) WriteBlobByte(image,(unsigned char) |
| cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 955 | GetPixelIndex(image,p)); |
| cristy | ed23157 | 2011-07-14 02:18:59 +0000 | [diff] [blame] | 956 | p+=GetPixelChannels(image); |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 957 | } |
| cristy | acee812 | 2009-11-19 02:04:10 +0000 | [diff] [blame] | 958 | if (image->columns & 0x01) |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 959 | (void) WriteBlobByte(image,0); /* pad scanline */ |
| 960 | if (image->previous == (Image *) NULL) |
| 961 | { |
| cristy | cee9711 | 2010-05-28 00:44:52 +0000 | [diff] [blame] | 962 | status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y, |
| cristy | c6da28e | 2011-04-28 01:41:35 +0000 | [diff] [blame] | 963 | image->rows); |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 964 | if (status == MagickFalse) |
| 965 | break; |
| 966 | } |
| 967 | } |
| 968 | } |
| 969 | if (GetNextImageInList(image) == (Image *) NULL) |
| 970 | break; |
| 971 | image=SyncNextImageInList(image); |
| 972 | status=SetImageProgress(image,SaveImagesTag,scene++, |
| 973 | GetImageListLength(image)); |
| 974 | if (status == MagickFalse) |
| 975 | break; |
| 976 | } while (image_info->adjoin != MagickFalse); |
| 977 | (void) CloseBlob(image); |
| 978 | return(MagickTrue); |
| 979 | } |