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 | 7ce65e7 | 2015-12-12 18:03:16 -0500 | [diff] [blame] | 20 | % Copyright 1999-2016 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" |
cristy | ced5eb7 | 2015-01-25 21:25:47 +0000 | [diff] [blame] | 50 | #include "MagickCore/colormap-private.h" |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 51 | #include "MagickCore/colorspace.h" |
cristy | 510d06a | 2011-07-06 23:43:54 +0000 | [diff] [blame] | 52 | #include "MagickCore/colorspace-private.h" |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 53 | #include "MagickCore/exception.h" |
| 54 | #include "MagickCore/exception-private.h" |
| 55 | #include "MagickCore/image.h" |
| 56 | #include "MagickCore/image-private.h" |
| 57 | #include "MagickCore/list.h" |
| 58 | #include "MagickCore/magick.h" |
| 59 | #include "MagickCore/memory_.h" |
| 60 | #include "MagickCore/monitor.h" |
| 61 | #include "MagickCore/monitor-private.h" |
| 62 | #include "MagickCore/pixel-accessor.h" |
| 63 | #include "MagickCore/quantum-private.h" |
| 64 | #include "MagickCore/static.h" |
| 65 | #include "MagickCore/string_.h" |
| 66 | #include "MagickCore/module.h" |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 67 | |
| 68 | /* |
| 69 | Forward declarations. |
| 70 | */ |
| 71 | static MagickBooleanType |
cristy | 3a37efd | 2011-08-28 20:31:03 +0000 | [diff] [blame] | 72 | WriteSUNImage(const ImageInfo *,Image *,ExceptionInfo *); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 73 | |
| 74 | /* |
| 75 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 76 | % % |
| 77 | % % |
| 78 | % % |
| 79 | % I s S U N % |
| 80 | % % |
| 81 | % % |
| 82 | % % |
| 83 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 84 | % |
| 85 | % IsSUN() returns MagickTrue if the image format type, identified by the |
| 86 | % magick string, is SUN. |
| 87 | % |
| 88 | % The format of the IsSUN method is: |
| 89 | % |
| 90 | % MagickBooleanType IsSUN(const unsigned char *magick,const size_t length) |
| 91 | % |
| 92 | % A description of each parameter follows: |
| 93 | % |
| 94 | % o magick: compare image format pattern against these bytes. |
| 95 | % |
| 96 | % o length: Specifies the length of the magick string. |
| 97 | % |
| 98 | */ |
| 99 | static MagickBooleanType IsSUN(const unsigned char *magick,const size_t length) |
| 100 | { |
| 101 | if (length < 4) |
| 102 | return(MagickFalse); |
| 103 | if (memcmp(magick,"\131\246\152\225",4) == 0) |
| 104 | return(MagickTrue); |
| 105 | return(MagickFalse); |
| 106 | } |
| 107 | |
| 108 | /* |
| 109 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 110 | % % |
| 111 | % % |
| 112 | % % |
| 113 | % D e c o d e I m a g e % |
| 114 | % % |
| 115 | % % |
| 116 | % % |
| 117 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 118 | % |
| 119 | % DecodeImage unpacks the packed image pixels into runlength-encoded pixel |
| 120 | % packets. |
| 121 | % |
| 122 | % The format of the DecodeImage method is: |
| 123 | % |
| 124 | % MagickBooleanType DecodeImage(const unsigned char *compressed_pixels, |
| 125 | % const size_t length,unsigned char *pixels) |
| 126 | % |
| 127 | % A description of each parameter follows: |
| 128 | % |
| 129 | % o compressed_pixels: The address of a byte (8 bits) array of compressed |
| 130 | % pixel data. |
| 131 | % |
| 132 | % o length: An integer value that is the total number of bytes of the |
| 133 | % source image (as just read by ReadBlob) |
| 134 | % |
| 135 | % o pixels: The address of a byte (8 bits) array of pixel data created by |
| 136 | % the uncompression process. The number of bytes in this array |
| 137 | % must be at least equal to the number columns times the number of rows |
| 138 | % of the source pixels. |
| 139 | % |
| 140 | */ |
| 141 | static MagickBooleanType DecodeImage(const unsigned char *compressed_pixels, |
| 142 | const size_t length,unsigned char *pixels,size_t maxpixels) |
| 143 | { |
| 144 | register const unsigned char |
cristy | c6da28e | 2011-04-28 01:41:35 +0000 | [diff] [blame] | 145 | *l, |
| 146 | *p; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 147 | |
| 148 | register unsigned char |
| 149 | *q; |
| 150 | |
| 151 | ssize_t |
| 152 | count; |
| 153 | |
| 154 | unsigned char |
| 155 | byte; |
| 156 | |
| 157 | (void) LogMagickEvent(TraceEvent,GetMagickModule(),"..."); |
| 158 | assert(compressed_pixels != (unsigned char *) NULL); |
| 159 | assert(pixels != (unsigned char *) NULL); |
| 160 | p=compressed_pixels; |
| 161 | q=pixels; |
| 162 | l=q+maxpixels; |
| 163 | while (((size_t) (p-compressed_pixels) < length) && (q < l)) |
| 164 | { |
| 165 | byte=(*p++); |
| 166 | if (byte != 128U) |
| 167 | *q++=byte; |
| 168 | else |
| 169 | { |
| 170 | /* |
| 171 | Runlength-encoded packet: <count><byte> |
| 172 | */ |
| 173 | count=(ssize_t) (*p++); |
| 174 | if (count > 0) |
| 175 | byte=(*p++); |
| 176 | while ((count >= 0) && (q < l)) |
| 177 | { |
| 178 | *q++=byte; |
| 179 | count--; |
| 180 | } |
| 181 | } |
| 182 | } |
| 183 | return(MagickTrue); |
| 184 | } |
| 185 | |
| 186 | /* |
| 187 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 188 | % % |
| 189 | % % |
| 190 | % % |
| 191 | % R e a d S U N I m a g e % |
| 192 | % % |
| 193 | % % |
| 194 | % % |
| 195 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 196 | % |
| 197 | % ReadSUNImage() reads a SUN image file and returns it. It allocates |
| 198 | % the memory necessary for the new Image structure and returns a pointer to |
| 199 | % the new image. |
| 200 | % |
| 201 | % The format of the ReadSUNImage method is: |
| 202 | % |
| 203 | % Image *ReadSUNImage(const ImageInfo *image_info,ExceptionInfo *exception) |
| 204 | % |
| 205 | % A description of each parameter follows: |
| 206 | % |
| 207 | % o image_info: the image info. |
| 208 | % |
| 209 | % o exception: return any errors or warnings in this structure. |
| 210 | % |
| 211 | */ |
| 212 | static Image *ReadSUNImage(const ImageInfo *image_info,ExceptionInfo *exception) |
| 213 | { |
| 214 | #define RMT_EQUAL_RGB 1 |
| 215 | #define RMT_NONE 0 |
| 216 | #define RMT_RAW 2 |
| 217 | #define RT_STANDARD 1 |
| 218 | #define RT_ENCODED 2 |
| 219 | #define RT_FORMAT_RGB 3 |
| 220 | |
| 221 | typedef struct _SUNInfo |
| 222 | { |
| 223 | unsigned int |
| 224 | magic, |
| 225 | width, |
| 226 | height, |
| 227 | depth, |
| 228 | length, |
| 229 | type, |
| 230 | maptype, |
| 231 | maplength; |
| 232 | } SUNInfo; |
| 233 | |
| 234 | Image |
| 235 | *image; |
| 236 | |
| 237 | int |
| 238 | bit; |
| 239 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 240 | MagickBooleanType |
| 241 | status; |
| 242 | |
| 243 | MagickSizeType |
| 244 | number_pixels; |
| 245 | |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 246 | register Quantum |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 247 | *q; |
| 248 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 249 | register ssize_t |
cristy | e13c304 | 2011-03-03 01:30:05 +0000 | [diff] [blame] | 250 | i, |
| 251 | x; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 252 | |
| 253 | register unsigned char |
| 254 | *p; |
| 255 | |
| 256 | size_t |
cristy | 01daf94 | 2014-12-01 23:14:00 +0000 | [diff] [blame] | 257 | bytes_per_line, |
cristy | 754c215 | 2014-12-07 18:17:41 +0000 | [diff] [blame] | 258 | extent, |
cristy | 8ea44b4 | 2015-01-13 13:31:27 +0000 | [diff] [blame] | 259 | height; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 260 | |
| 261 | ssize_t |
cristy | c6da28e | 2011-04-28 01:41:35 +0000 | [diff] [blame] | 262 | count, |
| 263 | y; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 264 | |
| 265 | SUNInfo |
| 266 | sun_info; |
| 267 | |
| 268 | unsigned char |
| 269 | *sun_data, |
| 270 | *sun_pixels; |
| 271 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 272 | /* |
| 273 | Open image file. |
| 274 | */ |
| 275 | assert(image_info != (const ImageInfo *) NULL); |
cristy | e1c94d9 | 2015-06-28 12:16:33 +0000 | [diff] [blame] | 276 | assert(image_info->signature == MagickCoreSignature); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 277 | if (image_info->debug != MagickFalse) |
| 278 | (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s", |
| 279 | image_info->filename); |
| 280 | assert(exception != (ExceptionInfo *) NULL); |
cristy | e1c94d9 | 2015-06-28 12:16:33 +0000 | [diff] [blame] | 281 | assert(exception->signature == MagickCoreSignature); |
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); |
cristy | 754c215 | 2014-12-07 18:17:41 +0000 | [diff] [blame] | 308 | extent=sun_info.height*sun_info.width; |
cristy | 62a3eb7 | 2014-12-07 12:52:02 +0000 | [diff] [blame] | 309 | if ((sun_info.height != 0) && (sun_info.width != extent/sun_info.height)) |
| 310 | ThrowReaderException(CorruptImageError,"ImproperImageHeader"); |
cristy | 22a6f21 | 2014-11-30 15:59:52 +0000 | [diff] [blame] | 311 | if ((sun_info.type != RT_STANDARD) && (sun_info.type != RT_ENCODED) && |
| 312 | (sun_info.type != RT_FORMAT_RGB)) |
cristy | 76d045a | 2014-12-02 02:12:04 +0000 | [diff] [blame] | 313 | ThrowReaderException(CorruptImageError,"ImproperImageHeader"); |
cristy | 22a6f21 | 2014-11-30 15:59:52 +0000 | [diff] [blame] | 314 | if ((sun_info.maptype == RMT_NONE) && (sun_info.maplength != 0)) |
cristy | 76d045a | 2014-12-02 02:12:04 +0000 | [diff] [blame] | 315 | ThrowReaderException(CorruptImageError,"ImproperImageHeader"); |
dirk | 6cad3f6 | 2016-01-15 02:11:22 +0100 | [diff] [blame] | 316 | if ((sun_info.depth != 1) && (sun_info.depth != 8) && |
dirk | de9d231 | 2016-01-15 22:29:36 +0100 | [diff] [blame] | 317 | (sun_info.depth != 24) && (sun_info.depth != 32)) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 318 | ThrowReaderException(CorruptImageError,"ImproperImageHeader"); |
cristy | 22a6f21 | 2014-11-30 15:59:52 +0000 | [diff] [blame] | 319 | if ((sun_info.maptype != RMT_NONE) && (sun_info.maptype != RMT_EQUAL_RGB) && |
| 320 | (sun_info.maptype != RMT_RAW)) |
| 321 | ThrowReaderException(CoderError,"ColormapTypeNotSupported"); |
| 322 | image->columns=sun_info.width; |
| 323 | image->rows=sun_info.height; |
cristy | acee812 | 2009-11-19 02:04:10 +0000 | [diff] [blame] | 324 | image->depth=sun_info.depth <= 8 ? sun_info.depth : |
| 325 | MAGICKCORE_QUANTUM_DEPTH; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 326 | if (sun_info.depth < 24) |
| 327 | { |
cristy | eaedf06 | 2010-05-29 22:36:02 +0000 | [diff] [blame] | 328 | size_t |
| 329 | one; |
| 330 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 331 | image->colors=sun_info.maplength; |
cristy | eaedf06 | 2010-05-29 22:36:02 +0000 | [diff] [blame] | 332 | one=1; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 333 | if (sun_info.maptype == RMT_NONE) |
cristy | eaedf06 | 2010-05-29 22:36:02 +0000 | [diff] [blame] | 334 | image->colors=one << sun_info.depth; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 335 | if (sun_info.maptype == RMT_EQUAL_RGB) |
| 336 | image->colors=sun_info.maplength/3; |
cristy | bd96074 | 2015-01-08 15:44:24 +0000 | [diff] [blame] | 337 | if (AcquireImageColormap(image,image->colors,exception) == MagickFalse) |
| 338 | ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed"); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 339 | } |
| 340 | switch (sun_info.maptype) |
| 341 | { |
cristy | 450bd71 | 2015-01-09 20:02:31 +0000 | [diff] [blame] | 342 | case RMT_NONE: |
| 343 | break; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 344 | case RMT_EQUAL_RGB: |
| 345 | { |
| 346 | unsigned char |
| 347 | *sun_colormap; |
| 348 | |
| 349 | /* |
| 350 | Read SUN raster colormap. |
| 351 | */ |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 352 | sun_colormap=(unsigned char *) AcquireQuantumMemory(image->colors, |
| 353 | sizeof(*sun_colormap)); |
| 354 | if (sun_colormap == (unsigned char *) NULL) |
| 355 | ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed"); |
| 356 | count=ReadBlob(image,image->colors,sun_colormap); |
cristy | 22a6f21 | 2014-11-30 15:59:52 +0000 | [diff] [blame] | 357 | if (count != (ssize_t) image->colors) |
| 358 | ThrowReaderException(CorruptImageError,"UnexpectedEndOfFile"); |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 359 | for (i=0; i < (ssize_t) image->colors; i++) |
cristy | 22a6f21 | 2014-11-30 15:59:52 +0000 | [diff] [blame] | 360 | image->colormap[i].red=(MagickRealType) ScaleCharToQuantum( |
| 361 | sun_colormap[i]); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 362 | count=ReadBlob(image,image->colors,sun_colormap); |
cristy | 22a6f21 | 2014-11-30 15:59:52 +0000 | [diff] [blame] | 363 | if (count != (ssize_t) image->colors) |
| 364 | ThrowReaderException(CorruptImageError,"UnexpectedEndOfFile"); |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 365 | for (i=0; i < (ssize_t) image->colors; i++) |
cristy | 22a6f21 | 2014-11-30 15:59:52 +0000 | [diff] [blame] | 366 | image->colormap[i].green=(MagickRealType) ScaleCharToQuantum( |
| 367 | sun_colormap[i]); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 368 | count=ReadBlob(image,image->colors,sun_colormap); |
cristy | 22a6f21 | 2014-11-30 15:59:52 +0000 | [diff] [blame] | 369 | if (count != (ssize_t) image->colors) |
| 370 | ThrowReaderException(CorruptImageError,"UnexpectedEndOfFile"); |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 371 | for (i=0; i < (ssize_t) image->colors; i++) |
cristy | 22a6f21 | 2014-11-30 15:59:52 +0000 | [diff] [blame] | 372 | image->colormap[i].blue=(MagickRealType) ScaleCharToQuantum( |
| 373 | sun_colormap[i]); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 374 | sun_colormap=(unsigned char *) RelinquishMagickMemory(sun_colormap); |
| 375 | break; |
| 376 | } |
| 377 | case RMT_RAW: |
| 378 | { |
| 379 | unsigned char |
| 380 | *sun_colormap; |
| 381 | |
| 382 | /* |
| 383 | Read SUN raster colormap. |
| 384 | */ |
| 385 | sun_colormap=(unsigned char *) AcquireQuantumMemory(sun_info.maplength, |
| 386 | sizeof(*sun_colormap)); |
| 387 | if (sun_colormap == (unsigned char *) NULL) |
| 388 | ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed"); |
| 389 | count=ReadBlob(image,sun_info.maplength,sun_colormap); |
cristy | 22a6f21 | 2014-11-30 15:59:52 +0000 | [diff] [blame] | 390 | if (count != (ssize_t) sun_info.maplength) |
| 391 | ThrowReaderException(CorruptImageError,"UnexpectedEndOfFile"); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 392 | sun_colormap=(unsigned char *) RelinquishMagickMemory(sun_colormap); |
| 393 | break; |
| 394 | } |
| 395 | default: |
| 396 | ThrowReaderException(CoderError,"ColormapTypeNotSupported"); |
| 397 | } |
cristy | 22a6f21 | 2014-11-30 15:59:52 +0000 | [diff] [blame] | 398 | image->alpha_trait=sun_info.depth == 32 ? BlendPixelTrait : |
cristy | b0a657e | 2012-08-29 00:45:37 +0000 | [diff] [blame] | 399 | UndefinedPixelTrait; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 400 | image->columns=sun_info.width; |
| 401 | image->rows=sun_info.height; |
| 402 | if (image_info->ping != MagickFalse) |
| 403 | { |
| 404 | (void) CloseBlob(image); |
| 405 | return(GetFirstImageInList(image)); |
| 406 | } |
cristy | acabb84 | 2014-12-14 23:36:33 +0000 | [diff] [blame] | 407 | status=SetImageExtent(image,image->columns,image->rows,exception); |
| 408 | if (status == MagickFalse) |
| 409 | return(DestroyImageList(image)); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 410 | if ((sun_info.length*sizeof(*sun_data))/sizeof(*sun_data) != |
| 411 | sun_info.length || !sun_info.length) |
cristy | 6b4aff0 | 2015-01-13 12:16:28 +0000 | [diff] [blame] | 412 | ThrowReaderException(ResourceLimitError,"ImproperImageHeader"); |
cristy | 4662a13 | 2015-01-19 19:27:00 +0000 | [diff] [blame] | 413 | number_pixels=(MagickSizeType) (image->columns*image->rows); |
cristy | b8f17d0 | 2015-01-10 14:10:16 +0000 | [diff] [blame] | 414 | if ((sun_info.type != RT_ENCODED) && |
cristy | 386277a | 2015-01-21 20:38:39 +0000 | [diff] [blame] | 415 | ((number_pixels*sun_info.depth) > (8UL*sun_info.length))) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 416 | ThrowReaderException(CorruptImageError,"ImproperImageHeader"); |
cristy | 78f82d9 | 2015-01-06 12:13:31 +0000 | [diff] [blame] | 417 | bytes_per_line=sun_info.width*sun_info.depth; |
| 418 | sun_data=(unsigned char *) AcquireQuantumMemory((size_t) MagickMax( |
Cristy | ffb9daf | 2016-01-24 19:23:27 -0500 | [diff] [blame^] | 419 | sun_info.length,bytes_per_line*sun_info.width)+7,sizeof(*sun_data)); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 420 | if (sun_data == (unsigned char *) NULL) |
| 421 | ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed"); |
| 422 | count=(ssize_t) ReadBlob(image,sun_info.length,sun_data); |
cristy | 22a6f21 | 2014-11-30 15:59:52 +0000 | [diff] [blame] | 423 | if (count != (ssize_t) sun_info.length) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 424 | ThrowReaderException(CorruptImageError,"UnableToReadImageData"); |
cristy | 1aa0c6d | 2015-01-13 02:23:43 +0000 | [diff] [blame] | 425 | height=sun_info.height; |
| 426 | if ((height == 0) || (sun_info.width == 0) || (sun_info.depth == 0) || |
| 427 | ((bytes_per_line/sun_info.depth) != sun_info.width)) |
cristy | 6b4aff0 | 2015-01-13 12:16:28 +0000 | [diff] [blame] | 428 | ThrowReaderException(ResourceLimitError,"ImproperImageHeader"); |
cristy | 1aa0c6d | 2015-01-13 02:23:43 +0000 | [diff] [blame] | 429 | bytes_per_line+=15; |
| 430 | bytes_per_line<<=1; |
| 431 | if ((bytes_per_line >> 1) != (sun_info.width*sun_info.depth+15)) |
cristy | 6b4aff0 | 2015-01-13 12:16:28 +0000 | [diff] [blame] | 432 | ThrowReaderException(ResourceLimitError,"ImproperImageHeader"); |
cristy | 1aa0c6d | 2015-01-13 02:23:43 +0000 | [diff] [blame] | 433 | bytes_per_line>>=4; |
| 434 | sun_pixels=(unsigned char *) AcquireQuantumMemory(height, |
Cristy | 2174484 | 2016-01-13 18:31:21 -0500 | [diff] [blame] | 435 | MagickMax(image->columns,bytes_per_line)*sizeof(*sun_pixels)); |
cristy | 1aa0c6d | 2015-01-13 02:23:43 +0000 | [diff] [blame] | 436 | if (sun_pixels == (unsigned char *) NULL) |
| 437 | ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed"); |
Cristy | 2174484 | 2016-01-13 18:31:21 -0500 | [diff] [blame] | 438 | ResetMagickMemory(sun_pixels,0,height*MagickMax(image->columns, |
| 439 | bytes_per_line)*sizeof(*sun_pixels)); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 440 | if (sun_info.type == RT_ENCODED) |
cristy | 1aa0c6d | 2015-01-13 02:23:43 +0000 | [diff] [blame] | 441 | (void) DecodeImage(sun_data,sun_info.length,sun_pixels,bytes_per_line* |
| 442 | height); |
cristy | 6b4aff0 | 2015-01-13 12:16:28 +0000 | [diff] [blame] | 443 | else |
| 444 | { |
| 445 | if (sun_info.length > (height*bytes_per_line)) |
| 446 | ThrowReaderException(ResourceLimitError,"ImproperImageHeader"); |
| 447 | (void) CopyMagickMemory(sun_pixels,sun_data,sun_info.length); |
| 448 | } |
cristy | 1aa0c6d | 2015-01-13 02:23:43 +0000 | [diff] [blame] | 449 | sun_data=(unsigned char *) RelinquishMagickMemory(sun_data); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 450 | /* |
| 451 | Convert SUN raster image to pixel packets. |
| 452 | */ |
| 453 | p=sun_pixels; |
| 454 | if (sun_info.depth == 1) |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 455 | for (y=0; y < (ssize_t) image->rows; y++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 456 | { |
| 457 | q=QueueAuthenticPixels(image,0,y,image->columns,1,exception); |
cristy | acd2ed2 | 2011-08-30 01:44:23 +0000 | [diff] [blame] | 458 | if (q == (Quantum *) NULL) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 459 | break; |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 460 | for (x=0; x < ((ssize_t) image->columns-7); x+=8) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 461 | { |
| 462 | for (bit=7; bit >= 0; bit--) |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 463 | { |
cristy | 22a6f21 | 2014-11-30 15:59:52 +0000 | [diff] [blame] | 464 | SetPixelIndex(image,(Quantum) ((*p) & (0x01 << bit) ? 0x00 : 0x01), |
| 465 | q); |
cristy | ed23157 | 2011-07-14 02:18:59 +0000 | [diff] [blame] | 466 | q+=GetPixelChannels(image); |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 467 | } |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 468 | p++; |
| 469 | } |
| 470 | if ((image->columns % 8) != 0) |
| 471 | { |
cristy | 22a6f21 | 2014-11-30 15:59:52 +0000 | [diff] [blame] | 472 | for (bit=7; bit >= (int) (8-(image->columns % 8)); bit--) |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 473 | { |
cristy | 22a6f21 | 2014-11-30 15:59:52 +0000 | [diff] [blame] | 474 | SetPixelIndex(image,(Quantum) ((*p) & (0x01 << bit) ? 0x00 : |
| 475 | 0x01),q); |
cristy | ed23157 | 2011-07-14 02:18:59 +0000 | [diff] [blame] | 476 | q+=GetPixelChannels(image); |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 477 | } |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 478 | p++; |
| 479 | } |
| 480 | if ((((image->columns/8)+(image->columns % 8 ? 1 : 0)) % 2) != 0) |
| 481 | p++; |
| 482 | if (SyncAuthenticPixels(image,exception) == MagickFalse) |
| 483 | break; |
| 484 | if (image->previous == (Image *) NULL) |
| 485 | { |
cristy | cee9711 | 2010-05-28 00:44:52 +0000 | [diff] [blame] | 486 | status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y, |
cristy | e13c304 | 2011-03-03 01:30:05 +0000 | [diff] [blame] | 487 | image->rows); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 488 | if (status == MagickFalse) |
| 489 | break; |
| 490 | } |
| 491 | } |
| 492 | else |
| 493 | if (image->storage_class == PseudoClass) |
| 494 | { |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 495 | for (y=0; y < (ssize_t) image->rows; y++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 496 | { |
| 497 | q=QueueAuthenticPixels(image,0,y,image->columns,1,exception); |
cristy | acd2ed2 | 2011-08-30 01:44:23 +0000 | [diff] [blame] | 498 | if (q == (Quantum *) NULL) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 499 | break; |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 500 | for (x=0; x < (ssize_t) image->columns; x++) |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 501 | { |
cristy | ced5eb7 | 2015-01-25 21:25:47 +0000 | [diff] [blame] | 502 | SetPixelIndex(image,ConstrainColormapIndex(image,*p,exception),q); |
| 503 | p++; |
cristy | ed23157 | 2011-07-14 02:18:59 +0000 | [diff] [blame] | 504 | q+=GetPixelChannels(image); |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 505 | } |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 506 | if ((image->columns % 2) != 0) |
| 507 | p++; |
| 508 | if (SyncAuthenticPixels(image,exception) == MagickFalse) |
| 509 | break; |
| 510 | if (image->previous == (Image *) NULL) |
| 511 | { |
cristy | cee9711 | 2010-05-28 00:44:52 +0000 | [diff] [blame] | 512 | status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y, |
| 513 | image->rows); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 514 | if (status == MagickFalse) |
| 515 | break; |
| 516 | } |
| 517 | } |
| 518 | } |
| 519 | else |
| 520 | { |
cristy | acee812 | 2009-11-19 02:04:10 +0000 | [diff] [blame] | 521 | size_t |
| 522 | bytes_per_pixel; |
| 523 | |
| 524 | bytes_per_pixel=3; |
cristy | 17f11b0 | 2014-12-20 19:37:04 +0000 | [diff] [blame] | 525 | if (image->alpha_trait != UndefinedPixelTrait) |
cristy | acee812 | 2009-11-19 02:04:10 +0000 | [diff] [blame] | 526 | bytes_per_pixel++; |
cristy | 1f30ce4 | 2014-12-04 00:59:05 +0000 | [diff] [blame] | 527 | if (bytes_per_line == 0) |
| 528 | bytes_per_line=bytes_per_pixel*image->columns; |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 529 | for (y=0; y < (ssize_t) image->rows; y++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 530 | { |
| 531 | q=QueueAuthenticPixels(image,0,y,image->columns,1,exception); |
cristy | acd2ed2 | 2011-08-30 01:44:23 +0000 | [diff] [blame] | 532 | if (q == (Quantum *) NULL) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 533 | break; |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 534 | for (x=0; x < (ssize_t) image->columns; x++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 535 | { |
cristy | 17f11b0 | 2014-12-20 19:37:04 +0000 | [diff] [blame] | 536 | if (image->alpha_trait != UndefinedPixelTrait) |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 537 | SetPixelAlpha(image,ScaleCharToQuantum(*p++),q); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 538 | if (sun_info.type == RT_STANDARD) |
| 539 | { |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 540 | SetPixelBlue(image,ScaleCharToQuantum(*p++),q); |
| 541 | SetPixelGreen(image,ScaleCharToQuantum(*p++),q); |
| 542 | SetPixelRed(image,ScaleCharToQuantum(*p++),q); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 543 | } |
| 544 | else |
| 545 | { |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 546 | SetPixelRed(image,ScaleCharToQuantum(*p++),q); |
| 547 | SetPixelGreen(image,ScaleCharToQuantum(*p++),q); |
| 548 | SetPixelBlue(image,ScaleCharToQuantum(*p++),q); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 549 | } |
| 550 | if (image->colors != 0) |
| 551 | { |
cristy | 22a6f21 | 2014-11-30 15:59:52 +0000 | [diff] [blame] | 552 | SetPixelRed(image,ClampToQuantum(image->colormap[(ssize_t) |
| 553 | GetPixelRed(image,q)].red),q); |
| 554 | SetPixelGreen(image,ClampToQuantum(image->colormap[(ssize_t) |
| 555 | GetPixelGreen(image,q)].green),q); |
| 556 | SetPixelBlue(image,ClampToQuantum(image->colormap[(ssize_t) |
| 557 | GetPixelBlue(image,q)].blue),q); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 558 | } |
cristy | ed23157 | 2011-07-14 02:18:59 +0000 | [diff] [blame] | 559 | q+=GetPixelChannels(image); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 560 | } |
cristy | acee812 | 2009-11-19 02:04:10 +0000 | [diff] [blame] | 561 | if (((bytes_per_pixel*image->columns) % 2) != 0) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 562 | p++; |
| 563 | if (SyncAuthenticPixels(image,exception) == MagickFalse) |
| 564 | break; |
| 565 | if (image->previous == (Image *) NULL) |
| 566 | { |
cristy | cee9711 | 2010-05-28 00:44:52 +0000 | [diff] [blame] | 567 | status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y, |
| 568 | image->rows); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 569 | if (status == MagickFalse) |
| 570 | break; |
| 571 | } |
| 572 | } |
| 573 | } |
| 574 | if (image->storage_class == PseudoClass) |
cristy | ea1a8aa | 2011-10-20 13:24:06 +0000 | [diff] [blame] | 575 | (void) SyncImage(image,exception); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 576 | sun_pixels=(unsigned char *) RelinquishMagickMemory(sun_pixels); |
| 577 | if (EOFBlob(image) != MagickFalse) |
| 578 | { |
| 579 | ThrowFileException(exception,CorruptImageError,"UnexpectedEndOfFile", |
| 580 | image->filename); |
| 581 | break; |
| 582 | } |
| 583 | /* |
| 584 | Proceed to next image. |
| 585 | */ |
| 586 | if (image_info->number_scenes != 0) |
| 587 | if (image->scene >= (image_info->scene+image_info->number_scenes-1)) |
| 588 | break; |
| 589 | sun_info.magic=ReadBlobMSBLong(image); |
| 590 | if (sun_info.magic == 0x59a66a95) |
| 591 | { |
| 592 | /* |
| 593 | Allocate next image structure. |
| 594 | */ |
cristy | 9950d57 | 2011-10-01 18:22:35 +0000 | [diff] [blame] | 595 | AcquireNextImage(image_info,image,exception); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 596 | if (GetNextImageInList(image) == (Image *) NULL) |
| 597 | { |
| 598 | image=DestroyImageList(image); |
| 599 | return((Image *) NULL); |
| 600 | } |
| 601 | image=SyncNextImageInList(image); |
| 602 | status=SetImageProgress(image,LoadImagesTag,TellBlob(image), |
| 603 | GetBlobSize(image)); |
| 604 | if (status == MagickFalse) |
| 605 | break; |
| 606 | } |
| 607 | } while (sun_info.magic == 0x59a66a95); |
| 608 | (void) CloseBlob(image); |
| 609 | return(GetFirstImageInList(image)); |
| 610 | } |
| 611 | |
| 612 | /* |
| 613 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 614 | % % |
| 615 | % % |
| 616 | % % |
| 617 | % R e g i s t e r S U N I m a g e % |
| 618 | % % |
| 619 | % % |
| 620 | % % |
| 621 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 622 | % |
| 623 | % RegisterSUNImage() adds attributes for the SUN image format to |
| 624 | % the list of supported formats. The attributes include the image format |
| 625 | % tag, a method to read and/or write the format, whether the format |
| 626 | % supports the saving of more than one frame to the same file or blob, |
| 627 | % whether the format supports native in-memory I/O, and a brief |
| 628 | % description of the format. |
| 629 | % |
| 630 | % The format of the RegisterSUNImage method is: |
| 631 | % |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 632 | % size_t RegisterSUNImage(void) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 633 | % |
| 634 | */ |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 635 | ModuleExport size_t RegisterSUNImage(void) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 636 | { |
| 637 | MagickInfo |
| 638 | *entry; |
| 639 | |
dirk | 06b627a | 2015-04-06 18:59:17 +0000 | [diff] [blame] | 640 | entry=AcquireMagickInfo("SUN","RAS","SUN Rasterfile"); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 641 | entry->decoder=(DecodeImageHandler *) ReadSUNImage; |
| 642 | entry->encoder=(EncodeImageHandler *) WriteSUNImage; |
| 643 | entry->magick=(IsImageFormatHandler *) IsSUN; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 644 | (void) RegisterMagickInfo(entry); |
dirk | 06b627a | 2015-04-06 18:59:17 +0000 | [diff] [blame] | 645 | entry=AcquireMagickInfo("SUN","SUN","SUN Rasterfile"); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 646 | entry->decoder=(DecodeImageHandler *) ReadSUNImage; |
| 647 | entry->encoder=(EncodeImageHandler *) WriteSUNImage; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 648 | (void) RegisterMagickInfo(entry); |
| 649 | return(MagickImageCoderSignature); |
| 650 | } |
| 651 | |
| 652 | /* |
| 653 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 654 | % % |
| 655 | % % |
| 656 | % % |
| 657 | % U n r e g i s t e r S U N I m a g e % |
| 658 | % % |
| 659 | % % |
| 660 | % % |
| 661 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 662 | % |
| 663 | % UnregisterSUNImage() removes format registrations made by the |
| 664 | % SUN module from the list of supported formats. |
| 665 | % |
| 666 | % The format of the UnregisterSUNImage method is: |
| 667 | % |
| 668 | % UnregisterSUNImage(void) |
| 669 | % |
| 670 | */ |
| 671 | ModuleExport void UnregisterSUNImage(void) |
| 672 | { |
| 673 | (void) UnregisterMagickInfo("RAS"); |
| 674 | (void) UnregisterMagickInfo("SUN"); |
| 675 | } |
| 676 | |
| 677 | /* |
| 678 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 679 | % % |
| 680 | % % |
| 681 | % % |
| 682 | % W r i t e S U N I m a g e % |
| 683 | % % |
| 684 | % % |
| 685 | % % |
| 686 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 687 | % |
| 688 | % WriteSUNImage() writes an image in the SUN rasterfile format. |
| 689 | % |
| 690 | % The format of the WriteSUNImage method is: |
| 691 | % |
cristy | 3a37efd | 2011-08-28 20:31:03 +0000 | [diff] [blame] | 692 | % MagickBooleanType WriteSUNImage(const ImageInfo *image_info, |
| 693 | % Image *image,ExceptionInfo *exception) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 694 | % |
| 695 | % A description of each parameter follows. |
| 696 | % |
| 697 | % o image_info: the image info. |
| 698 | % |
| 699 | % o image: The image. |
| 700 | % |
cristy | 3a37efd | 2011-08-28 20:31:03 +0000 | [diff] [blame] | 701 | % o exception: return any errors or warnings in this structure. |
| 702 | % |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 703 | */ |
cristy | 3a37efd | 2011-08-28 20:31:03 +0000 | [diff] [blame] | 704 | static MagickBooleanType WriteSUNImage(const ImageInfo *image_info,Image *image, |
| 705 | ExceptionInfo *exception) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 706 | { |
| 707 | #define RMT_EQUAL_RGB 1 |
| 708 | #define RMT_NONE 0 |
| 709 | #define RMT_RAW 2 |
| 710 | #define RT_STANDARD 1 |
| 711 | #define RT_FORMAT_RGB 3 |
| 712 | |
| 713 | typedef struct _SUNInfo |
| 714 | { |
| 715 | unsigned int |
| 716 | magic, |
| 717 | width, |
| 718 | height, |
| 719 | depth, |
| 720 | length, |
| 721 | type, |
| 722 | maptype, |
| 723 | maplength; |
| 724 | } SUNInfo; |
| 725 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 726 | MagickBooleanType |
| 727 | status; |
| 728 | |
| 729 | MagickOffsetType |
| 730 | scene; |
| 731 | |
| 732 | MagickSizeType |
| 733 | number_pixels; |
| 734 | |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 735 | register const Quantum |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 736 | *p; |
| 737 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 738 | register ssize_t |
cristy | e13c304 | 2011-03-03 01:30:05 +0000 | [diff] [blame] | 739 | i, |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 740 | x; |
| 741 | |
cristy | e13c304 | 2011-03-03 01:30:05 +0000 | [diff] [blame] | 742 | ssize_t |
| 743 | y; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 744 | |
| 745 | SUNInfo |
| 746 | sun_info; |
| 747 | |
| 748 | /* |
| 749 | Open output image file. |
| 750 | */ |
| 751 | assert(image_info != (const ImageInfo *) NULL); |
cristy | e1c94d9 | 2015-06-28 12:16:33 +0000 | [diff] [blame] | 752 | assert(image_info->signature == MagickCoreSignature); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 753 | assert(image != (Image *) NULL); |
cristy | e1c94d9 | 2015-06-28 12:16:33 +0000 | [diff] [blame] | 754 | assert(image->signature == MagickCoreSignature); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 755 | if (image->debug != MagickFalse) |
| 756 | (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename); |
cristy | 3a37efd | 2011-08-28 20:31:03 +0000 | [diff] [blame] | 757 | assert(exception != (ExceptionInfo *) NULL); |
cristy | e1c94d9 | 2015-06-28 12:16:33 +0000 | [diff] [blame] | 758 | assert(exception->signature == MagickCoreSignature); |
cristy | 3a37efd | 2011-08-28 20:31:03 +0000 | [diff] [blame] | 759 | status=OpenBlob(image_info,image,WriteBinaryBlobMode,exception); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 760 | if (status == MagickFalse) |
| 761 | return(status); |
| 762 | scene=0; |
| 763 | do |
| 764 | { |
| 765 | /* |
| 766 | Initialize SUN raster file header. |
| 767 | */ |
cristy | af8d391 | 2014-02-21 14:50:33 +0000 | [diff] [blame] | 768 | (void) TransformImageColorspace(image,sRGBColorspace,exception); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 769 | sun_info.magic=0x59a66a95; |
| 770 | if ((image->columns != (unsigned int) image->columns) || |
| 771 | (image->rows != (unsigned int) image->rows)) |
| 772 | ThrowWriterException(ImageError,"WidthOrHeightExceedsLimit"); |
| 773 | sun_info.width=(unsigned int) image->columns; |
| 774 | sun_info.height=(unsigned int) image->rows; |
cristy | 22a6f21 | 2014-11-30 15:59:52 +0000 | [diff] [blame] | 775 | sun_info.type=(unsigned int) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 776 | (image->storage_class == DirectClass ? RT_FORMAT_RGB : RT_STANDARD); |
| 777 | sun_info.maptype=RMT_NONE; |
| 778 | sun_info.maplength=0; |
| 779 | number_pixels=(MagickSizeType) image->columns*image->rows; |
| 780 | if ((4*number_pixels) != (size_t) (4*number_pixels)) |
| 781 | ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed"); |
| 782 | if (image->storage_class == DirectClass) |
| 783 | { |
| 784 | /* |
| 785 | Full color SUN raster. |
| 786 | */ |
cristy | 17f11b0 | 2014-12-20 19:37:04 +0000 | [diff] [blame] | 787 | sun_info.depth=(unsigned int) image->alpha_trait != UndefinedPixelTrait ? |
cristy | dc2d327 | 2013-02-12 14:00:44 +0000 | [diff] [blame] | 788 | 32U : 24U; |
cristy | 17f11b0 | 2014-12-20 19:37:04 +0000 | [diff] [blame] | 789 | sun_info.length=(unsigned int) ((image->alpha_trait != UndefinedPixelTrait ? |
cristy | dc2d327 | 2013-02-12 14:00:44 +0000 | [diff] [blame] | 790 | 4 : 3)*number_pixels); |
cristy | eaedf06 | 2010-05-29 22:36:02 +0000 | [diff] [blame] | 791 | sun_info.length+=sun_info.length & 0x01 ? (unsigned int) image->rows : |
| 792 | 0; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 793 | } |
| 794 | else |
dirk | f1d8548 | 2015-04-06 00:36:00 +0000 | [diff] [blame] | 795 | if (SetImageMonochrome(image,exception) != MagickFalse) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 796 | { |
| 797 | /* |
| 798 | Monochrome SUN raster. |
| 799 | */ |
| 800 | sun_info.depth=1; |
| 801 | sun_info.length=(unsigned int) (((image->columns+7) >> 3)* |
| 802 | image->rows); |
cristy | f9cca6a | 2010-06-04 23:49:28 +0000 | [diff] [blame] | 803 | sun_info.length+=(unsigned int) (((image->columns/8)+(image->columns % |
| 804 | 8 ? 1 : 0)) % 2 ? image->rows : 0); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 805 | } |
| 806 | else |
| 807 | { |
| 808 | /* |
| 809 | Colormapped SUN raster. |
| 810 | */ |
| 811 | sun_info.depth=8; |
| 812 | sun_info.length=(unsigned int) number_pixels; |
cristy | f9cca6a | 2010-06-04 23:49:28 +0000 | [diff] [blame] | 813 | sun_info.length+=(unsigned int) (image->columns & 0x01 ? image->rows : |
| 814 | 0); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 815 | sun_info.maptype=RMT_EQUAL_RGB; |
| 816 | sun_info.maplength=(unsigned int) (3*image->colors); |
| 817 | } |
| 818 | /* |
| 819 | Write SUN header. |
| 820 | */ |
| 821 | (void) WriteBlobMSBLong(image,sun_info.magic); |
| 822 | (void) WriteBlobMSBLong(image,sun_info.width); |
| 823 | (void) WriteBlobMSBLong(image,sun_info.height); |
| 824 | (void) WriteBlobMSBLong(image,sun_info.depth); |
| 825 | (void) WriteBlobMSBLong(image,sun_info.length); |
| 826 | (void) WriteBlobMSBLong(image,sun_info.type); |
| 827 | (void) WriteBlobMSBLong(image,sun_info.maptype); |
| 828 | (void) WriteBlobMSBLong(image,sun_info.maplength); |
| 829 | /* |
| 830 | Convert MIFF to SUN raster pixels. |
| 831 | */ |
| 832 | x=0; |
| 833 | y=0; |
| 834 | if (image->storage_class == DirectClass) |
| 835 | { |
| 836 | register unsigned char |
| 837 | *q; |
| 838 | |
| 839 | size_t |
cristy | acee812 | 2009-11-19 02:04:10 +0000 | [diff] [blame] | 840 | bytes_per_pixel, |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 841 | length; |
| 842 | |
| 843 | unsigned char |
| 844 | *pixels; |
| 845 | |
| 846 | /* |
| 847 | Allocate memory for pixels. |
| 848 | */ |
cristy | acee812 | 2009-11-19 02:04:10 +0000 | [diff] [blame] | 849 | bytes_per_pixel=3; |
cristy | 17f11b0 | 2014-12-20 19:37:04 +0000 | [diff] [blame] | 850 | if (image->alpha_trait != UndefinedPixelTrait) |
cristy | acee812 | 2009-11-19 02:04:10 +0000 | [diff] [blame] | 851 | bytes_per_pixel++; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 852 | length=image->columns; |
| 853 | pixels=(unsigned char *) AcquireQuantumMemory(length,4*sizeof(*pixels)); |
| 854 | if (pixels == (unsigned char *) NULL) |
| 855 | ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed"); |
| 856 | /* |
| 857 | Convert DirectClass packet to SUN RGB pixel. |
| 858 | */ |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 859 | for (y=0; y < (ssize_t) image->rows; y++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 860 | { |
cristy | 3a37efd | 2011-08-28 20:31:03 +0000 | [diff] [blame] | 861 | p=GetVirtualPixels(image,0,y,image->columns,1,exception); |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 862 | if (p == (const Quantum *) NULL) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 863 | break; |
| 864 | q=pixels; |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 865 | for (x=0; x < (ssize_t) image->columns; x++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 866 | { |
cristy | 17f11b0 | 2014-12-20 19:37:04 +0000 | [diff] [blame] | 867 | if (image->alpha_trait != UndefinedPixelTrait) |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 868 | *q++=ScaleQuantumToChar(GetPixelAlpha(image,p)); |
| 869 | *q++=ScaleQuantumToChar(GetPixelRed(image,p)); |
| 870 | *q++=ScaleQuantumToChar(GetPixelGreen(image,p)); |
| 871 | *q++=ScaleQuantumToChar(GetPixelBlue(image,p)); |
cristy | ed23157 | 2011-07-14 02:18:59 +0000 | [diff] [blame] | 872 | p+=GetPixelChannels(image); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 873 | } |
cristy | acee812 | 2009-11-19 02:04:10 +0000 | [diff] [blame] | 874 | if (((bytes_per_pixel*image->columns) & 0x01) != 0) |
| 875 | *q++='\0'; /* pad scanline */ |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 876 | (void) WriteBlob(image,(size_t) (q-pixels),pixels); |
| 877 | if (image->previous == (Image *) NULL) |
| 878 | { |
cristy | cee9711 | 2010-05-28 00:44:52 +0000 | [diff] [blame] | 879 | status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y, |
| 880 | image->rows); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 881 | if (status == MagickFalse) |
| 882 | break; |
| 883 | } |
| 884 | } |
| 885 | pixels=(unsigned char *) RelinquishMagickMemory(pixels); |
| 886 | } |
| 887 | else |
dirk | f1d8548 | 2015-04-06 00:36:00 +0000 | [diff] [blame] | 888 | if (SetImageMonochrome(image,exception) != MagickFalse) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 889 | { |
| 890 | register unsigned char |
| 891 | bit, |
| 892 | byte; |
| 893 | |
| 894 | /* |
| 895 | Convert PseudoClass image to a SUN monochrome image. |
| 896 | */ |
cristy | 018f07f | 2011-09-04 21:15:19 +0000 | [diff] [blame] | 897 | (void) SetImageType(image,BilevelType,exception); |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 898 | for (y=0; y < (ssize_t) image->rows; y++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 899 | { |
cristy | 3a37efd | 2011-08-28 20:31:03 +0000 | [diff] [blame] | 900 | p=GetVirtualPixels(image,0,y,image->columns,1,exception); |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 901 | if (p == (const Quantum *) NULL) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 902 | break; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 903 | bit=0; |
| 904 | byte=0; |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 905 | for (x=0; x < (ssize_t) image->columns; x++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 906 | { |
| 907 | byte<<=1; |
cristy | d032322 | 2013-04-07 16:13:21 +0000 | [diff] [blame] | 908 | if (GetPixelLuma(image,p) < (QuantumRange/2.0)) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 909 | byte|=0x01; |
| 910 | bit++; |
| 911 | if (bit == 8) |
| 912 | { |
| 913 | (void) WriteBlobByte(image,byte); |
| 914 | bit=0; |
| 915 | byte=0; |
| 916 | } |
cristy | ed23157 | 2011-07-14 02:18:59 +0000 | [diff] [blame] | 917 | p+=GetPixelChannels(image); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 918 | } |
| 919 | if (bit != 0) |
| 920 | (void) WriteBlobByte(image,(unsigned char) (byte << (8-bit))); |
| 921 | if ((((image->columns/8)+ |
| 922 | (image->columns % 8 ? 1 : 0)) % 2) != 0) |
| 923 | (void) WriteBlobByte(image,0); /* pad scanline */ |
| 924 | if (image->previous == (Image *) NULL) |
| 925 | { |
cristy | cee9711 | 2010-05-28 00:44:52 +0000 | [diff] [blame] | 926 | status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y, |
| 927 | image->rows); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 928 | if (status == MagickFalse) |
| 929 | break; |
| 930 | } |
| 931 | } |
| 932 | } |
| 933 | else |
| 934 | { |
| 935 | /* |
| 936 | Dump colormap to file. |
| 937 | */ |
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( |
cristy | 22a6f21 | 2014-11-30 15:59:52 +0000 | [diff] [blame] | 940 | ClampToQuantum(image->colormap[i].red))); |
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( |
cristy | 22a6f21 | 2014-11-30 15:59:52 +0000 | [diff] [blame] | 943 | ClampToQuantum(image->colormap[i].green))); |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 944 | for (i=0; i < (ssize_t) image->colors; i++) |
cristy | c6da28e | 2011-04-28 01:41:35 +0000 | [diff] [blame] | 945 | (void) WriteBlobByte(image,ScaleQuantumToChar( |
cristy | 22a6f21 | 2014-11-30 15:59:52 +0000 | [diff] [blame] | 946 | ClampToQuantum(image->colormap[i].blue))); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 947 | /* |
| 948 | Convert PseudoClass packet to SUN colormapped pixel. |
| 949 | */ |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 950 | for (y=0; y < (ssize_t) image->rows; y++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 951 | { |
cristy | 3a37efd | 2011-08-28 20:31:03 +0000 | [diff] [blame] | 952 | p=GetVirtualPixels(image,0,y,image->columns,1,exception); |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 953 | if (p == (const Quantum *) NULL) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 954 | break; |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 955 | for (x=0; x < (ssize_t) image->columns; x++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 956 | { |
cristy | c6da28e | 2011-04-28 01:41:35 +0000 | [diff] [blame] | 957 | (void) WriteBlobByte(image,(unsigned char) |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 958 | GetPixelIndex(image,p)); |
cristy | ed23157 | 2011-07-14 02:18:59 +0000 | [diff] [blame] | 959 | p+=GetPixelChannels(image); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 960 | } |
cristy | acee812 | 2009-11-19 02:04:10 +0000 | [diff] [blame] | 961 | if (image->columns & 0x01) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 962 | (void) WriteBlobByte(image,0); /* pad scanline */ |
| 963 | if (image->previous == (Image *) NULL) |
| 964 | { |
cristy | cee9711 | 2010-05-28 00:44:52 +0000 | [diff] [blame] | 965 | status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y, |
cristy | c6da28e | 2011-04-28 01:41:35 +0000 | [diff] [blame] | 966 | image->rows); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 967 | if (status == MagickFalse) |
| 968 | break; |
| 969 | } |
| 970 | } |
| 971 | } |
| 972 | if (GetNextImageInList(image) == (Image *) NULL) |
| 973 | break; |
| 974 | image=SyncNextImageInList(image); |
| 975 | status=SetImageProgress(image,SaveImagesTag,scene++, |
| 976 | GetImageListLength(image)); |
| 977 | if (status == MagickFalse) |
| 978 | break; |
| 979 | } while (image_info->adjoin != MagickFalse); |
| 980 | (void) CloseBlob(image); |
| 981 | return(MagickTrue); |
| 982 | } |