cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1 | /* |
| 2 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 3 | % % |
| 4 | % % |
| 5 | % % |
| 6 | % SSSSS GGGG IIIII % |
| 7 | % SS G I % |
| 8 | % SSS G GG I % |
| 9 | % SS G G I % |
| 10 | % SSSSS GGG IIIII % |
| 11 | % % |
| 12 | % % |
| 13 | % Read/Write Irix RGB Image Format % |
| 14 | % % |
| 15 | % Software Design % |
| 16 | % John Cristy % |
| 17 | % July 1992 % |
| 18 | % % |
| 19 | % % |
cristy | 16af1cb | 2009-12-11 21:38:29 +0000 | [diff] [blame] | 20 | % Copyright 1999-2010 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 | */ |
| 42 | #include "magick/studio.h" |
| 43 | #include "magick/blob.h" |
| 44 | #include "magick/blob-private.h" |
| 45 | #include "magick/cache.h" |
| 46 | #include "magick/color.h" |
| 47 | #include "magick/color-private.h" |
cristy | e7e4055 | 2010-04-24 21:34:22 +0000 | [diff] [blame] | 48 | #include "magick/colormap.h" |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 49 | #include "magick/colorspace.h" |
| 50 | #include "magick/exception.h" |
| 51 | #include "magick/exception-private.h" |
| 52 | #include "magick/image.h" |
| 53 | #include "magick/image-private.h" |
| 54 | #include "magick/list.h" |
| 55 | #include "magick/magick.h" |
| 56 | #include "magick/memory_.h" |
| 57 | #include "magick/monitor.h" |
| 58 | #include "magick/monitor-private.h" |
| 59 | #include "magick/property.h" |
| 60 | #include "magick/quantum-private.h" |
| 61 | #include "magick/static.h" |
| 62 | #include "magick/string_.h" |
| 63 | #include "magick/module.h" |
| 64 | |
| 65 | /* |
| 66 | Typedef declaractions. |
| 67 | */ |
| 68 | typedef struct _SGIInfo |
| 69 | { |
| 70 | unsigned short |
| 71 | magic; |
| 72 | |
| 73 | unsigned char |
| 74 | storage, |
| 75 | bytes_per_pixel; |
| 76 | |
| 77 | unsigned short |
| 78 | dimension, |
| 79 | columns, |
| 80 | rows, |
| 81 | depth; |
| 82 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame^] | 83 | size_t |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 84 | minimum_value, |
| 85 | maximum_value, |
| 86 | sans; |
| 87 | |
| 88 | char |
| 89 | name[80]; |
| 90 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame^] | 91 | size_t |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 92 | pixel_format; |
| 93 | |
| 94 | unsigned char |
| 95 | filler[404]; |
| 96 | } SGIInfo; |
| 97 | |
| 98 | /* |
| 99 | Forward declarations. |
| 100 | */ |
| 101 | static MagickBooleanType |
| 102 | WriteSGIImage(const ImageInfo *,Image *); |
| 103 | /* |
| 104 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 105 | % % |
| 106 | % % |
| 107 | % % |
| 108 | % I s S G I % |
| 109 | % % |
| 110 | % % |
| 111 | % % |
| 112 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 113 | % |
| 114 | % IsSGI() returns MagickTrue if the image format type, identified by the |
| 115 | % magick string, is SGI. |
| 116 | % |
| 117 | % The format of the IsSGI method is: |
| 118 | % |
| 119 | % MagickBooleanType IsSGI(const unsigned char *magick,const size_t length) |
| 120 | % |
| 121 | % A description of each parameter follows: |
| 122 | % |
| 123 | % o magick: compare image format pattern against these bytes. |
| 124 | % |
| 125 | % o length: Specifies the length of the magick string. |
| 126 | % |
| 127 | % |
| 128 | */ |
| 129 | static MagickBooleanType IsSGI(const unsigned char *magick,const size_t length) |
| 130 | { |
| 131 | if (length < 2) |
| 132 | return(MagickFalse); |
| 133 | if (memcmp(magick,"\001\332",2) == 0) |
| 134 | return(MagickTrue); |
| 135 | return(MagickFalse); |
| 136 | } |
| 137 | |
| 138 | /* |
| 139 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 140 | % % |
| 141 | % % |
| 142 | % % |
| 143 | % R e a d S G I I m a g e % |
| 144 | % % |
| 145 | % % |
| 146 | % % |
| 147 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 148 | % |
| 149 | % ReadSGIImage() reads a SGI RGB image file and returns it. It |
| 150 | % allocates the memory necessary for the new Image structure and returns a |
| 151 | % pointer to the new image. |
| 152 | % |
| 153 | % The format of the ReadSGIImage method is: |
| 154 | % |
| 155 | % Image *ReadSGIImage(const ImageInfo *image_info,ExceptionInfo *exception) |
| 156 | % |
| 157 | % A description of each parameter follows: |
| 158 | % |
| 159 | % o image_info: the image info. |
| 160 | % |
| 161 | % o exception: return any errors or warnings in this structure. |
| 162 | % |
| 163 | */ |
| 164 | |
| 165 | static inline size_t MagickMin(const size_t x,const size_t y) |
| 166 | { |
| 167 | if (x < y) |
| 168 | return(x); |
| 169 | return(y); |
| 170 | } |
| 171 | |
| 172 | static MagickBooleanType SGIDecode(const size_t bytes_per_pixel, |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame^] | 173 | ssize_t number_packets,unsigned char *packets,ssize_t number_pixels, |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 174 | unsigned char *pixels) |
| 175 | { |
| 176 | register unsigned char |
| 177 | *p, |
| 178 | *q; |
| 179 | |
| 180 | ssize_t |
| 181 | count; |
| 182 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame^] | 183 | size_t |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 184 | pixel; |
| 185 | |
| 186 | p=packets; |
| 187 | q=pixels; |
| 188 | if (bytes_per_pixel == 2) |
| 189 | { |
| 190 | for ( ; number_pixels > 0; ) |
| 191 | { |
| 192 | if (number_packets-- == 0) |
| 193 | return(MagickFalse); |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame^] | 194 | pixel=(size_t) (*p++) << 8; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 195 | pixel|=(*p++); |
| 196 | count=(ssize_t) (pixel & 0x7f); |
| 197 | if (count == 0) |
| 198 | break; |
| 199 | if (count > (ssize_t) number_pixels) |
| 200 | return(MagickFalse); |
| 201 | number_pixels-=count; |
| 202 | if ((pixel & 0x80) != 0) |
| 203 | for ( ; count != 0; count--) |
| 204 | { |
| 205 | if (number_packets-- == 0) |
| 206 | return(MagickFalse); |
| 207 | *q=(*p++); |
| 208 | *(q+1)=(*p++); |
| 209 | q+=8; |
| 210 | } |
| 211 | else |
| 212 | { |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame^] | 213 | pixel=(size_t) (*p++) << 8; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 214 | pixel|=(*p++); |
| 215 | for ( ; count != 0; count--) |
| 216 | { |
| 217 | if (number_packets-- == 0) |
| 218 | return(MagickFalse); |
| 219 | *q=(unsigned char) (pixel >> 8); |
| 220 | *(q+1)=(unsigned char) pixel; |
| 221 | q+=8; |
| 222 | } |
| 223 | } |
| 224 | } |
| 225 | return(MagickTrue); |
| 226 | } |
| 227 | for ( ; number_pixels > 0; ) |
| 228 | { |
| 229 | if (number_packets-- == 0) |
| 230 | return(MagickFalse); |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame^] | 231 | pixel=(size_t) (*p++); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 232 | count=(ssize_t) (pixel & 0x7f); |
| 233 | if (count == 0) |
| 234 | break; |
| 235 | if (count > (ssize_t) number_pixels) |
| 236 | return(MagickFalse); |
| 237 | number_pixels-=count; |
| 238 | if ((pixel & 0x80) != 0) |
| 239 | for ( ; count != 0; count--) |
| 240 | { |
| 241 | if (number_packets-- == 0) |
| 242 | return(MagickFalse); |
| 243 | *q=(*p++); |
| 244 | q+=4; |
| 245 | } |
| 246 | else |
| 247 | { |
| 248 | if (number_packets-- == 0) |
| 249 | return(MagickFalse); |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame^] | 250 | pixel=(size_t) (*p++); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 251 | for ( ; count != 0; count--) |
| 252 | { |
| 253 | *q=(unsigned char) pixel; |
| 254 | q+=4; |
| 255 | } |
| 256 | } |
| 257 | } |
| 258 | return(MagickTrue); |
| 259 | } |
| 260 | |
| 261 | static Image *ReadSGIImage(const ImageInfo *image_info,ExceptionInfo *exception) |
| 262 | { |
| 263 | Image |
| 264 | *image; |
| 265 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame^] | 266 | ssize_t |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 267 | y, |
| 268 | z; |
| 269 | |
| 270 | MagickBooleanType |
| 271 | status; |
| 272 | |
| 273 | MagickSizeType |
| 274 | number_pixels; |
| 275 | |
| 276 | register IndexPacket |
| 277 | *indexes; |
| 278 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame^] | 279 | register ssize_t |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 280 | i, |
| 281 | x; |
| 282 | |
| 283 | register PixelPacket |
| 284 | *q; |
| 285 | |
| 286 | register unsigned char |
| 287 | *p; |
| 288 | |
| 289 | ssize_t |
| 290 | count; |
| 291 | |
| 292 | SGIInfo |
| 293 | iris_info; |
| 294 | |
| 295 | size_t |
| 296 | bytes_per_pixel; |
| 297 | |
| 298 | unsigned char |
| 299 | *iris_pixels; |
| 300 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame^] | 301 | size_t |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 302 | quantum; |
| 303 | |
| 304 | /* |
| 305 | Open image file. |
| 306 | */ |
| 307 | assert(image_info != (const ImageInfo *) NULL); |
| 308 | assert(image_info->signature == MagickSignature); |
| 309 | if (image_info->debug != MagickFalse) |
| 310 | (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s", |
| 311 | image_info->filename); |
| 312 | assert(exception != (ExceptionInfo *) NULL); |
| 313 | assert(exception->signature == MagickSignature); |
| 314 | image=AcquireImage(image_info); |
| 315 | status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception); |
| 316 | if (status == MagickFalse) |
| 317 | { |
| 318 | image=DestroyImageList(image); |
| 319 | return((Image *) NULL); |
| 320 | } |
| 321 | /* |
| 322 | Read SGI raster header. |
| 323 | */ |
| 324 | iris_info.magic=ReadBlobMSBShort(image); |
| 325 | do |
| 326 | { |
| 327 | /* |
| 328 | Verify SGI identifier. |
| 329 | */ |
| 330 | if (iris_info.magic != 0x01DA) |
| 331 | ThrowReaderException(CorruptImageError,"ImproperImageHeader"); |
| 332 | iris_info.storage=(unsigned char) ReadBlobByte(image); |
| 333 | switch (iris_info.storage) |
| 334 | { |
| 335 | case 0x00: image->compression=NoCompression; break; |
| 336 | case 0x01: image->compression=RLECompression; break; |
| 337 | default: |
| 338 | ThrowReaderException(CorruptImageError,"ImproperImageHeader"); |
| 339 | } |
| 340 | iris_info.bytes_per_pixel=(unsigned char) ReadBlobByte(image); |
| 341 | if ((iris_info.bytes_per_pixel == 0) || (iris_info.bytes_per_pixel > 2)) |
| 342 | ThrowReaderException(CorruptImageError,"ImproperImageHeader"); |
| 343 | iris_info.dimension=ReadBlobMSBShort(image); |
| 344 | iris_info.columns=ReadBlobMSBShort(image); |
| 345 | iris_info.rows=ReadBlobMSBShort(image); |
| 346 | iris_info.depth=ReadBlobMSBShort(image); |
| 347 | if ((iris_info.depth == 0) || (iris_info.depth > 4)) |
| 348 | ThrowReaderException(CorruptImageError,"ImproperImageHeader"); |
| 349 | iris_info.minimum_value=ReadBlobMSBLong(image); |
| 350 | iris_info.maximum_value=ReadBlobMSBLong(image); |
| 351 | iris_info.sans=ReadBlobMSBLong(image); |
| 352 | (void) ReadBlob(image,sizeof(iris_info.name),(unsigned char *) |
| 353 | iris_info.name); |
| 354 | iris_info.name[sizeof(iris_info.name)-1]='\0'; |
| 355 | if (*iris_info.name != '\0') |
| 356 | (void) SetImageProperty(image,"label",iris_info.name); |
| 357 | iris_info.pixel_format=ReadBlobMSBLong(image); |
| 358 | if (iris_info.pixel_format != 0) |
| 359 | ThrowReaderException(CorruptImageError,"ImproperImageHeader"); |
| 360 | count=ReadBlob(image,sizeof(iris_info.filler),iris_info.filler); |
| 361 | image->columns=iris_info.columns; |
| 362 | image->rows=iris_info.rows; |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame^] | 363 | image->depth=(size_t) MagickMin(iris_info.depth,MAGICKCORE_QUANTUM_DEPTH); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 364 | if (iris_info.pixel_format == 0) |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame^] | 365 | image->depth=(size_t) MagickMin((size_t) 8* |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 366 | iris_info.bytes_per_pixel,MAGICKCORE_QUANTUM_DEPTH); |
| 367 | if (iris_info.depth < 3) |
| 368 | { |
| 369 | image->storage_class=PseudoClass; |
| 370 | image->colors=256; |
| 371 | } |
| 372 | if ((image_info->ping != MagickFalse) && (image_info->number_scenes != 0)) |
| 373 | if (image->scene >= (image_info->scene+image_info->number_scenes-1)) |
| 374 | break; |
| 375 | /* |
| 376 | Allocate SGI pixels. |
| 377 | */ |
| 378 | bytes_per_pixel=(size_t) iris_info.bytes_per_pixel; |
| 379 | number_pixels=(MagickSizeType) iris_info.columns*iris_info.rows; |
| 380 | if ((4*bytes_per_pixel*number_pixels) != ((MagickSizeType) (size_t) |
| 381 | (4*bytes_per_pixel*number_pixels))) |
| 382 | ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed"); |
| 383 | iris_pixels=(unsigned char *) AcquireQuantumMemory(iris_info.columns, |
| 384 | iris_info.rows*4*bytes_per_pixel*sizeof(*iris_pixels)); |
| 385 | if (iris_pixels == (unsigned char *) NULL) |
| 386 | ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed"); |
| 387 | if ((int) iris_info.storage != 0x01) |
| 388 | { |
| 389 | unsigned char |
| 390 | *scanline; |
| 391 | |
| 392 | /* |
| 393 | Read standard image format. |
| 394 | */ |
| 395 | scanline=(unsigned char *) AcquireQuantumMemory(iris_info.columns, |
| 396 | bytes_per_pixel*sizeof(*scanline)); |
| 397 | if (scanline == (unsigned char *) NULL) |
| 398 | ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed"); |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame^] | 399 | for (z=0; z < (ssize_t) iris_info.depth; z++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 400 | { |
| 401 | p=iris_pixels+bytes_per_pixel*z; |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame^] | 402 | for (y=0; y < (ssize_t) iris_info.rows; y++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 403 | { |
| 404 | count=ReadBlob(image,bytes_per_pixel*iris_info.columns,scanline); |
| 405 | if (EOFBlob(image) != MagickFalse) |
| 406 | break; |
| 407 | if (bytes_per_pixel == 2) |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame^] | 408 | for (x=0; x < (ssize_t) iris_info.columns; x++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 409 | { |
| 410 | *p=scanline[2*x]; |
| 411 | *(p+1)=scanline[2*x+1]; |
| 412 | p+=8; |
| 413 | } |
| 414 | else |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame^] | 415 | for (x=0; x < (ssize_t) iris_info.columns; x++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 416 | { |
| 417 | *p=scanline[x]; |
| 418 | p+=4; |
| 419 | } |
| 420 | } |
| 421 | } |
| 422 | scanline=(unsigned char *) RelinquishMagickMemory(scanline); |
| 423 | } |
| 424 | else |
| 425 | { |
| 426 | ssize_t |
| 427 | offset, |
| 428 | *offsets; |
| 429 | |
| 430 | unsigned char |
| 431 | *packets; |
| 432 | |
| 433 | unsigned int |
| 434 | data_order; |
| 435 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame^] | 436 | size_t |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 437 | *runlength; |
| 438 | |
| 439 | /* |
| 440 | Read runlength-encoded image format. |
| 441 | */ |
| 442 | offsets=(ssize_t *) AcquireQuantumMemory((size_t) iris_info.rows, |
| 443 | iris_info.depth*sizeof(*offsets)); |
| 444 | packets=(unsigned char *) AcquireQuantumMemory((size_t) |
| 445 | iris_info.columns+10UL,4UL*sizeof(*packets)); |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame^] | 446 | runlength=(size_t *) AcquireQuantumMemory(iris_info.rows, |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 447 | iris_info.depth*sizeof(*runlength)); |
| 448 | if ((offsets == (ssize_t *) NULL) || |
| 449 | (packets == (unsigned char *) NULL) || |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame^] | 450 | (runlength == (size_t *) NULL)) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 451 | ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed"); |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame^] | 452 | for (i=0; i < (ssize_t) (iris_info.rows*iris_info.depth); i++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 453 | offsets[i]=(ssize_t) ReadBlobMSBLong(image); |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame^] | 454 | for (i=0; i < (ssize_t) (iris_info.rows*iris_info.depth); i++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 455 | { |
| 456 | runlength[i]=ReadBlobMSBLong(image); |
| 457 | if (runlength[i] > (4*(size_t) iris_info.columns+10)) |
| 458 | ThrowReaderException(CorruptImageError,"ImproperImageHeader"); |
| 459 | } |
| 460 | /* |
| 461 | Check data order. |
| 462 | */ |
| 463 | offset=0; |
| 464 | data_order=0; |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame^] | 465 | for (y=0; ((y < (ssize_t) iris_info.rows) && (data_order == 0)); y++) |
| 466 | for (z=0; ((z < (ssize_t) iris_info.depth) && (data_order == 0)); z++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 467 | { |
| 468 | if (offsets[y+z*iris_info.rows] < offset) |
| 469 | data_order=1; |
| 470 | offset=offsets[y+z*iris_info.rows]; |
| 471 | } |
| 472 | offset=(ssize_t) TellBlob(image); |
| 473 | if (data_order == 1) |
| 474 | { |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame^] | 475 | for (z=0; z < (ssize_t) iris_info.depth; z++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 476 | { |
| 477 | p=iris_pixels; |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame^] | 478 | for (y=0; y < (ssize_t) iris_info.rows; y++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 479 | { |
| 480 | if (offset != offsets[y+z*iris_info.rows]) |
| 481 | { |
| 482 | offset=offsets[y+z*iris_info.rows]; |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame^] | 483 | offset=(ssize_t) SeekBlob(image,(ssize_t) offset,SEEK_SET); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 484 | } |
| 485 | count=ReadBlob(image,(size_t) runlength[y+z*iris_info.rows], |
| 486 | packets); |
| 487 | if (EOFBlob(image) != MagickFalse) |
| 488 | break; |
| 489 | offset+=runlength[y+z*iris_info.rows]; |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame^] | 490 | status=SGIDecode(bytes_per_pixel,(ssize_t) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 491 | (runlength[y+z*iris_info.rows]/bytes_per_pixel),packets, |
| 492 | 1L*iris_info.columns,p+bytes_per_pixel*z); |
| 493 | if (status == MagickFalse) |
| 494 | ThrowReaderException(CorruptImageError,"ImproperImageHeader"); |
| 495 | p+=(iris_info.columns*4*bytes_per_pixel); |
| 496 | } |
| 497 | } |
| 498 | } |
| 499 | else |
| 500 | { |
| 501 | MagickOffsetType |
| 502 | position; |
| 503 | |
| 504 | position=TellBlob(image); |
| 505 | p=iris_pixels; |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame^] | 506 | for (y=0; y < (ssize_t) iris_info.rows; y++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 507 | { |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame^] | 508 | for (z=0; z < (ssize_t) iris_info.depth; z++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 509 | { |
| 510 | if (offset != offsets[y+z*iris_info.rows]) |
| 511 | { |
| 512 | offset=offsets[y+z*iris_info.rows]; |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame^] | 513 | offset=(ssize_t) SeekBlob(image,(ssize_t) offset,SEEK_SET); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 514 | } |
| 515 | count=ReadBlob(image,(size_t) runlength[y+z*iris_info.rows], |
| 516 | packets); |
| 517 | if (EOFBlob(image) != MagickFalse) |
| 518 | break; |
| 519 | offset+=runlength[y+z*iris_info.rows]; |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame^] | 520 | status=SGIDecode(bytes_per_pixel,(ssize_t) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 521 | (runlength[y+z*iris_info.rows]/bytes_per_pixel),packets, |
| 522 | 1L*iris_info.columns,p+bytes_per_pixel*z); |
| 523 | if (status == MagickFalse) |
| 524 | ThrowReaderException(CorruptImageError,"ImproperImageHeader"); |
| 525 | } |
| 526 | p+=(iris_info.columns*4*bytes_per_pixel); |
| 527 | } |
| 528 | offset=(ssize_t) SeekBlob(image,position,SEEK_SET); |
| 529 | } |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame^] | 530 | runlength=(size_t *) RelinquishMagickMemory(runlength); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 531 | packets=(unsigned char *) RelinquishMagickMemory(packets); |
| 532 | offsets=(ssize_t *) RelinquishMagickMemory(offsets); |
| 533 | } |
| 534 | /* |
| 535 | Initialize image structure. |
| 536 | */ |
| 537 | image->matte=iris_info.depth == 4 ? MagickTrue : MagickFalse; |
| 538 | image->columns=iris_info.columns; |
| 539 | image->rows=iris_info.rows; |
| 540 | /* |
| 541 | Convert SGI raster image to pixel packets. |
| 542 | */ |
| 543 | if (image->storage_class == DirectClass) |
| 544 | { |
| 545 | /* |
| 546 | Convert SGI image to DirectClass pixel packets. |
| 547 | */ |
| 548 | if (bytes_per_pixel == 2) |
| 549 | { |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame^] | 550 | for (y=0; y < (ssize_t) image->rows; y++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 551 | { |
| 552 | p=iris_pixels+(image->rows-y-1)*8*image->columns; |
| 553 | q=QueueAuthenticPixels(image,0,y,image->columns,1,exception); |
| 554 | if (q == (PixelPacket *) NULL) |
| 555 | break; |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame^] | 556 | for (x=0; x < (ssize_t) image->columns; x++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 557 | { |
| 558 | q->red=ScaleShortToQuantum((unsigned short) |
| 559 | ((*(p+0) << 8) | (*(p+1)))); |
| 560 | q->green=ScaleShortToQuantum((unsigned short) |
| 561 | ((*(p+2) << 8) | (*(p+3)))); |
| 562 | q->blue=ScaleShortToQuantum((unsigned short) |
| 563 | ((*(p+4) << 8) | (*(p+5)))); |
cristy | ce70c17 | 2010-01-07 17:15:30 +0000 | [diff] [blame] | 564 | SetOpacityPixelComponent(q,OpaqueOpacity); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 565 | if (image->matte != MagickFalse) |
| 566 | q->opacity=(Quantum) (QuantumRange-ScaleShortToQuantum( |
| 567 | (unsigned short) ((*(p+6) << 8) | (*(p+7))))); |
| 568 | p+=8; |
| 569 | q++; |
| 570 | } |
| 571 | if (SyncAuthenticPixels(image,exception) == MagickFalse) |
| 572 | break; |
| 573 | if (image->previous == (Image *) NULL) |
| 574 | { |
| 575 | status=SetImageProgress(image,LoadImageTag,y,image->rows); |
| 576 | if (status == MagickFalse) |
| 577 | break; |
| 578 | } |
| 579 | } |
| 580 | } |
| 581 | else |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame^] | 582 | for (y=0; y < (ssize_t) image->rows; y++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 583 | { |
| 584 | p=iris_pixels+(image->rows-y-1)*4*image->columns; |
| 585 | q=QueueAuthenticPixels(image,0,y,image->columns,1,exception); |
| 586 | if (q == (PixelPacket *) NULL) |
| 587 | break; |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame^] | 588 | for (x=0; x < (ssize_t) image->columns; x++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 589 | { |
| 590 | q->red=ScaleCharToQuantum(*p); |
| 591 | q->green=ScaleCharToQuantum(*(p+1)); |
| 592 | q->blue=ScaleCharToQuantum(*(p+2)); |
cristy | ce70c17 | 2010-01-07 17:15:30 +0000 | [diff] [blame] | 593 | SetOpacityPixelComponent(q,OpaqueOpacity); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 594 | if (image->matte != MagickFalse) |
| 595 | q->opacity=(Quantum) (QuantumRange-ScaleCharToQuantum(*(p+3))); |
| 596 | p+=4; |
| 597 | q++; |
| 598 | } |
| 599 | if (SyncAuthenticPixels(image,exception) == MagickFalse) |
| 600 | break; |
| 601 | if (image->previous == (Image *) NULL) |
| 602 | { |
| 603 | status=SetImageProgress(image,LoadImageTag,y,image->rows); |
| 604 | if (status == MagickFalse) |
| 605 | break; |
| 606 | } |
| 607 | } |
| 608 | } |
| 609 | else |
| 610 | { |
| 611 | /* |
| 612 | Create grayscale map. |
| 613 | */ |
| 614 | if (AcquireImageColormap(image,image->colors) == MagickFalse) |
| 615 | ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed"); |
| 616 | /* |
| 617 | Convert SGI image to PseudoClass pixel packets. |
| 618 | */ |
| 619 | if (bytes_per_pixel == 2) |
| 620 | { |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame^] | 621 | for (y=0; y < (ssize_t) image->rows; y++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 622 | { |
| 623 | p=iris_pixels+(image->rows-y-1)*8*image->columns; |
| 624 | q=QueueAuthenticPixels(image,0,y,image->columns,1,exception); |
| 625 | if (q == (PixelPacket *) NULL) |
| 626 | break; |
| 627 | indexes=GetAuthenticIndexQueue(image); |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame^] | 628 | for (x=0; x < (ssize_t) image->columns; x++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 629 | { |
| 630 | quantum=(*p << 8); |
| 631 | quantum|=(*(p+1)); |
| 632 | indexes[x]=(IndexPacket) quantum; |
| 633 | p+=8; |
| 634 | q++; |
| 635 | } |
| 636 | if (SyncAuthenticPixels(image,exception) == MagickFalse) |
| 637 | break; |
| 638 | if (image->previous == (Image *) NULL) |
| 639 | { |
| 640 | status=SetImageProgress(image,LoadImageTag,y,image->rows); |
| 641 | if (status == MagickFalse) |
| 642 | break; |
| 643 | } |
| 644 | } |
| 645 | } |
| 646 | else |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame^] | 647 | for (y=0; y < (ssize_t) image->rows; y++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 648 | { |
| 649 | p=iris_pixels+(image->rows-y-1)*4*image->columns; |
| 650 | q=QueueAuthenticPixels(image,0,y,image->columns,1,exception); |
| 651 | if (q == (PixelPacket *) NULL) |
| 652 | break; |
| 653 | indexes=GetAuthenticIndexQueue(image); |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame^] | 654 | for (x=0; x < (ssize_t) image->columns; x++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 655 | { |
| 656 | indexes[x]=(IndexPacket) (*p); |
| 657 | p+=4; |
| 658 | q++; |
| 659 | } |
| 660 | if (SyncAuthenticPixels(image,exception) == MagickFalse) |
| 661 | break; |
| 662 | if (image->previous == (Image *) NULL) |
| 663 | { |
| 664 | status=SetImageProgress(image,LoadImageTag,y,image->rows); |
| 665 | if (status == MagickFalse) |
| 666 | break; |
| 667 | } |
| 668 | } |
| 669 | (void) SyncImage(image); |
| 670 | } |
| 671 | iris_pixels=(unsigned char *) RelinquishMagickMemory(iris_pixels); |
| 672 | if (EOFBlob(image) != MagickFalse) |
| 673 | { |
| 674 | ThrowFileException(exception,CorruptImageError,"UnexpectedEndOfFile", |
| 675 | image->filename); |
| 676 | break; |
| 677 | } |
| 678 | /* |
| 679 | Proceed to next image. |
| 680 | */ |
| 681 | if (image_info->number_scenes != 0) |
| 682 | if (image->scene >= (image_info->scene+image_info->number_scenes-1)) |
| 683 | break; |
| 684 | iris_info.magic=ReadBlobMSBShort(image); |
| 685 | if (iris_info.magic == 0x01DA) |
| 686 | { |
| 687 | /* |
| 688 | Allocate next image structure. |
| 689 | */ |
| 690 | AcquireNextImage(image_info,image); |
| 691 | if (GetNextImageInList(image) == (Image *) NULL) |
| 692 | { |
| 693 | image=DestroyImageList(image); |
| 694 | return((Image *) NULL); |
| 695 | } |
| 696 | image=SyncNextImageInList(image); |
| 697 | status=SetImageProgress(image,LoadImagesTag,TellBlob(image), |
| 698 | GetBlobSize(image)); |
| 699 | if (status == MagickFalse) |
| 700 | break; |
| 701 | } |
| 702 | } while (iris_info.magic == 0x01DA); |
| 703 | (void) CloseBlob(image); |
| 704 | return(GetFirstImageInList(image)); |
| 705 | } |
| 706 | |
| 707 | /* |
| 708 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 709 | % % |
| 710 | % % |
| 711 | % % |
| 712 | % R e g i s t e r S G I I m a g e % |
| 713 | % % |
| 714 | % % |
| 715 | % % |
| 716 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 717 | % |
| 718 | % RegisterSGIImage() adds properties for the SGI image format to |
| 719 | % the list of supported formats. The properties include the image format |
| 720 | % tag, a method to read and/or write the format, whether the format |
| 721 | % supports the saving of more than one frame to the same file or blob, |
| 722 | % whether the format supports native in-memory I/O, and a brief |
| 723 | % description of the format. |
| 724 | % |
| 725 | % The format of the RegisterSGIImage method is: |
| 726 | % |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame^] | 727 | % size_t RegisterSGIImage(void) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 728 | % |
| 729 | */ |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame^] | 730 | ModuleExport size_t RegisterSGIImage(void) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 731 | { |
| 732 | MagickInfo |
| 733 | *entry; |
| 734 | |
| 735 | entry=SetMagickInfo("SGI"); |
| 736 | entry->decoder=(DecodeImageHandler *) ReadSGIImage; |
| 737 | entry->encoder=(EncodeImageHandler *) WriteSGIImage; |
| 738 | entry->magick=(IsImageFormatHandler *) IsSGI; |
| 739 | entry->description=ConstantString("Irix RGB image"); |
| 740 | entry->module=ConstantString("SGI"); |
| 741 | entry->seekable_stream=MagickTrue; |
| 742 | (void) RegisterMagickInfo(entry); |
| 743 | return(MagickImageCoderSignature); |
| 744 | } |
| 745 | |
| 746 | /* |
| 747 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 748 | % % |
| 749 | % % |
| 750 | % % |
| 751 | % U n r e g i s t e r S G I I m a g e % |
| 752 | % % |
| 753 | % % |
| 754 | % % |
| 755 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 756 | % |
| 757 | % UnregisterSGIImage() removes format registrations made by the |
| 758 | % SGI module from the list of supported formats. |
| 759 | % |
| 760 | % The format of the UnregisterSGIImage method is: |
| 761 | % |
| 762 | % UnregisterSGIImage(void) |
| 763 | % |
| 764 | */ |
| 765 | ModuleExport void UnregisterSGIImage(void) |
| 766 | { |
| 767 | (void) UnregisterMagickInfo("SGI"); |
| 768 | } |
| 769 | |
| 770 | /* |
| 771 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 772 | % % |
| 773 | % % |
| 774 | % % |
| 775 | % W r i t e S G I I m a g e % |
| 776 | % % |
| 777 | % % |
| 778 | % % |
| 779 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 780 | % |
| 781 | % WriteSGIImage() writes an image in SGI RGB encoded image format. |
| 782 | % |
| 783 | % The format of the WriteSGIImage method is: |
| 784 | % |
| 785 | % MagickBooleanType WriteSGIImage(const ImageInfo *image_info,Image *image) |
| 786 | % |
| 787 | % A description of each parameter follows. |
| 788 | % |
| 789 | % o image_info: the image info. |
| 790 | % |
| 791 | % o image: The image. |
| 792 | % |
| 793 | */ |
| 794 | |
| 795 | static size_t SGIEncode(unsigned char *pixels,size_t length, |
| 796 | unsigned char *packets) |
| 797 | { |
| 798 | short |
| 799 | runlength; |
| 800 | |
| 801 | register unsigned char |
| 802 | *p, |
| 803 | *q; |
| 804 | |
| 805 | unsigned char |
| 806 | *limit, |
| 807 | *mark; |
| 808 | |
| 809 | p=pixels; |
| 810 | limit=p+length*4; |
| 811 | q=packets; |
| 812 | while (p < limit) |
| 813 | { |
| 814 | mark=p; |
| 815 | p+=8; |
| 816 | while ((p < limit) && ((*(p-8) != *(p-4)) || (*(p-4) != *p))) |
| 817 | p+=4; |
| 818 | p-=8; |
| 819 | length=(size_t) (p-mark) >> 2; |
| 820 | while (length != 0) |
| 821 | { |
| 822 | runlength=(short) (length > 126 ? 126 : length); |
| 823 | length-=runlength; |
| 824 | *q++=(unsigned char) (0x80 | runlength); |
| 825 | for ( ; runlength > 0; runlength--) |
| 826 | { |
| 827 | *q++=(*mark); |
| 828 | mark+=4; |
| 829 | } |
| 830 | } |
| 831 | mark=p; |
| 832 | p+=4; |
| 833 | while ((p < limit) && (*p == *mark)) |
| 834 | p+=4; |
| 835 | length=(size_t) (p-mark) >> 2; |
| 836 | while (length != 0) |
| 837 | { |
| 838 | runlength=(short) (length > 126 ? 126 : length); |
| 839 | length-=runlength; |
| 840 | *q++=(unsigned char) runlength; |
| 841 | *q++=(*mark); |
| 842 | } |
| 843 | } |
| 844 | *q++='\0'; |
| 845 | return((size_t) (q-packets)); |
| 846 | } |
| 847 | |
| 848 | static MagickBooleanType WriteSGIImage(const ImageInfo *image_info,Image *image) |
| 849 | { |
| 850 | CompressionType |
| 851 | compression; |
| 852 | |
| 853 | const char |
| 854 | *value; |
| 855 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame^] | 856 | ssize_t |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 857 | y, |
| 858 | z; |
| 859 | |
| 860 | MagickBooleanType |
| 861 | status; |
| 862 | |
| 863 | MagickOffsetType |
| 864 | scene; |
| 865 | |
| 866 | MagickSizeType |
| 867 | number_pixels; |
| 868 | |
| 869 | SGIInfo |
| 870 | iris_info; |
| 871 | |
| 872 | register const PixelPacket |
| 873 | *p; |
| 874 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame^] | 875 | register ssize_t |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 876 | i, |
| 877 | x; |
| 878 | |
| 879 | register unsigned char |
| 880 | *q; |
| 881 | |
| 882 | unsigned char |
| 883 | *iris_pixels, |
| 884 | *packets; |
| 885 | |
| 886 | /* |
| 887 | Open output image file. |
| 888 | */ |
| 889 | assert(image_info != (const ImageInfo *) NULL); |
| 890 | assert(image_info->signature == MagickSignature); |
| 891 | assert(image != (Image *) NULL); |
| 892 | assert(image->signature == MagickSignature); |
| 893 | if (image->debug != MagickFalse) |
| 894 | (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename); |
| 895 | if ((image->columns > 65535UL) || (image->rows > 65535UL)) |
| 896 | ThrowWriterException(ImageError,"WidthOrHeightExceedsLimit"); |
| 897 | status=OpenBlob(image_info,image,WriteBinaryBlobMode,&image->exception); |
| 898 | if (status == MagickFalse) |
| 899 | return(status); |
| 900 | scene=0; |
| 901 | do |
| 902 | { |
| 903 | /* |
| 904 | Initialize SGI raster file header. |
| 905 | */ |
| 906 | if (image->colorspace != RGBColorspace) |
| 907 | (void) TransformImageColorspace(image,RGBColorspace); |
| 908 | (void) ResetMagickMemory(&iris_info,0,sizeof(iris_info)); |
| 909 | iris_info.magic=0x01DA; |
| 910 | compression=image->compression; |
| 911 | if (image_info->compression != UndefinedCompression) |
| 912 | compression=image_info->compression; |
| 913 | if (image->depth > 8) |
| 914 | compression=NoCompression; |
| 915 | if (compression == NoCompression) |
| 916 | iris_info.storage=(unsigned char) 0x00; |
| 917 | else |
| 918 | iris_info.storage=(unsigned char) 0x01; |
| 919 | iris_info.bytes_per_pixel=(unsigned char) (image->depth > 8 ? 2 : 1); |
| 920 | iris_info.dimension=3; |
| 921 | iris_info.columns=(unsigned short) image->columns; |
| 922 | iris_info.rows=(unsigned short) image->rows; |
| 923 | if (image->matte != MagickFalse) |
| 924 | iris_info.depth=4; |
| 925 | else |
| 926 | { |
| 927 | if ((image_info->type != TrueColorType) && |
| 928 | (IsGrayImage(image,&image->exception) != MagickFalse)) |
| 929 | { |
| 930 | iris_info.dimension=2; |
| 931 | iris_info.depth=1; |
| 932 | } |
| 933 | else |
| 934 | iris_info.depth=3; |
| 935 | } |
| 936 | iris_info.minimum_value=0; |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame^] | 937 | iris_info.maximum_value=(size_t) (image->depth <= 8 ? |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 938 | 1UL*ScaleQuantumToChar((Quantum) QuantumRange) : |
| 939 | 1UL*ScaleQuantumToShort((Quantum) QuantumRange)); |
| 940 | /* |
| 941 | Write SGI header. |
| 942 | */ |
| 943 | (void) WriteBlobMSBShort(image,iris_info.magic); |
| 944 | (void) WriteBlobByte(image,iris_info.storage); |
| 945 | (void) WriteBlobByte(image,iris_info.bytes_per_pixel); |
| 946 | (void) WriteBlobMSBShort(image,iris_info.dimension); |
| 947 | (void) WriteBlobMSBShort(image,iris_info.columns); |
| 948 | (void) WriteBlobMSBShort(image,iris_info.rows); |
| 949 | (void) WriteBlobMSBShort(image,iris_info.depth); |
| 950 | (void) WriteBlobMSBLong(image,iris_info.minimum_value); |
| 951 | (void) WriteBlobMSBLong(image,iris_info.maximum_value); |
| 952 | (void) WriteBlobMSBLong(image,iris_info.sans); |
| 953 | value=GetImageProperty(image,"label"); |
| 954 | if (value != (const char *) NULL) |
| 955 | (void) CopyMagickString(iris_info.name,value,sizeof(iris_info.name)); |
| 956 | (void) WriteBlob(image,sizeof(iris_info.name),(unsigned char *) |
| 957 | iris_info.name); |
| 958 | (void) WriteBlobMSBLong(image,iris_info.pixel_format); |
| 959 | (void) WriteBlob(image,sizeof(iris_info.filler),iris_info.filler); |
| 960 | /* |
| 961 | Allocate SGI pixels. |
| 962 | */ |
| 963 | number_pixels=(MagickSizeType) image->columns*image->rows; |
| 964 | if ((4*iris_info.bytes_per_pixel*number_pixels) != |
| 965 | ((MagickSizeType) (size_t) (4*iris_info.bytes_per_pixel*number_pixels))) |
| 966 | ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed"); |
| 967 | iris_pixels=(unsigned char *) AcquireQuantumMemory((size_t) number_pixels, |
| 968 | 4*iris_info.bytes_per_pixel*sizeof(*iris_pixels)); |
| 969 | if (iris_pixels == (unsigned char *) NULL) |
| 970 | ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed"); |
| 971 | /* |
| 972 | Convert image pixels to uncompressed SGI pixels. |
| 973 | */ |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame^] | 974 | for (y=0; y < (ssize_t) image->rows; y++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 975 | { |
| 976 | p=GetVirtualPixels(image,0,y,image->columns,1,&image->exception); |
| 977 | if (p == (const PixelPacket *) NULL) |
| 978 | break; |
| 979 | if (image->depth <= 8) |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame^] | 980 | for (x=0; x < (ssize_t) image->columns; x++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 981 | { |
| 982 | register unsigned char |
| 983 | *q; |
| 984 | |
| 985 | q=(unsigned char *) iris_pixels; |
| 986 | q+=((iris_info.rows-1)-y)*(4*iris_info.columns)+4*x; |
cristy | ce70c17 | 2010-01-07 17:15:30 +0000 | [diff] [blame] | 987 | *q++=ScaleQuantumToChar(GetRedPixelComponent(p)); |
| 988 | *q++=ScaleQuantumToChar(GetGreenPixelComponent(p)); |
| 989 | *q++=ScaleQuantumToChar(GetBluePixelComponent(p)); |
cristy | 46f0820 | 2010-01-10 04:04:21 +0000 | [diff] [blame] | 990 | *q++=ScaleQuantumToChar((Quantum) (GetAlphaPixelComponent(p))); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 991 | p++; |
| 992 | } |
| 993 | else |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame^] | 994 | for (x=0; x < (ssize_t) image->columns; x++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 995 | { |
| 996 | register unsigned short |
| 997 | *q; |
| 998 | |
| 999 | q=(unsigned short *) iris_pixels; |
| 1000 | q+=((iris_info.rows-1)-y)*(4*iris_info.columns)+4*x; |
cristy | ce70c17 | 2010-01-07 17:15:30 +0000 | [diff] [blame] | 1001 | *q++=ScaleQuantumToShort(GetRedPixelComponent(p)); |
| 1002 | *q++=ScaleQuantumToShort(GetGreenPixelComponent(p)); |
| 1003 | *q++=ScaleQuantumToShort(GetBluePixelComponent(p)); |
cristy | 46f0820 | 2010-01-10 04:04:21 +0000 | [diff] [blame] | 1004 | *q++=ScaleQuantumToShort((Quantum) (GetAlphaPixelComponent(p))); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1005 | p++; |
| 1006 | } |
| 1007 | if (image->previous == (Image *) NULL) |
| 1008 | { |
| 1009 | status=SetImageProgress(image,SaveImageTag,y,image->rows); |
| 1010 | if (status == MagickFalse) |
| 1011 | break; |
| 1012 | } |
| 1013 | } |
| 1014 | switch (compression) |
| 1015 | { |
| 1016 | case NoCompression: |
| 1017 | { |
| 1018 | /* |
| 1019 | Write uncompressed SGI pixels. |
| 1020 | */ |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame^] | 1021 | for (z=0; z < (ssize_t) iris_info.depth; z++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1022 | { |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame^] | 1023 | for (y=0; y < (ssize_t) iris_info.rows; y++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1024 | { |
| 1025 | if (image->depth <= 8) |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame^] | 1026 | for (x=0; x < (ssize_t) iris_info.columns; x++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1027 | { |
| 1028 | register unsigned char |
| 1029 | *q; |
| 1030 | |
| 1031 | q=(unsigned char *) iris_pixels; |
| 1032 | q+=y*(4*iris_info.columns)+4*x+z; |
| 1033 | (void) WriteBlobByte(image,*q); |
| 1034 | } |
| 1035 | else |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame^] | 1036 | for (x=0; x < (ssize_t) iris_info.columns; x++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1037 | { |
| 1038 | register unsigned short |
| 1039 | *q; |
| 1040 | |
| 1041 | q=(unsigned short *) iris_pixels; |
| 1042 | q+=y*(4*iris_info.columns)+4*x+z; |
| 1043 | (void) WriteBlobMSBShort(image,*q); |
| 1044 | } |
| 1045 | } |
| 1046 | } |
| 1047 | break; |
| 1048 | } |
| 1049 | default: |
| 1050 | { |
| 1051 | ssize_t |
| 1052 | offset, |
| 1053 | *offsets; |
| 1054 | |
| 1055 | size_t |
| 1056 | length, |
| 1057 | number_packets; |
| 1058 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame^] | 1059 | size_t |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1060 | *runlength; |
| 1061 | |
| 1062 | /* |
| 1063 | Convert SGI uncompressed pixels. |
| 1064 | */ |
| 1065 | offsets=(ssize_t *) AcquireQuantumMemory(iris_info.rows*iris_info.depth, |
| 1066 | sizeof(*offsets)); |
| 1067 | packets=(unsigned char *) AcquireQuantumMemory((2*(size_t) |
| 1068 | iris_info.columns+10)*image->rows,4*sizeof(*packets)); |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame^] | 1069 | runlength=(size_t *) AcquireQuantumMemory(iris_info.rows, |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1070 | iris_info.depth*sizeof(*runlength)); |
| 1071 | if ((offsets == (ssize_t *) NULL) || |
| 1072 | (packets == (unsigned char *) NULL) || |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame^] | 1073 | (runlength == (size_t *) NULL)) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1074 | ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed"); |
| 1075 | offset=512+4*2*((ssize_t) iris_info.rows*iris_info.depth); |
| 1076 | number_packets=0; |
| 1077 | q=iris_pixels; |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame^] | 1078 | for (y=0; y < (ssize_t) iris_info.rows; y++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1079 | { |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame^] | 1080 | for (z=0; z < (ssize_t) iris_info.depth; z++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1081 | { |
| 1082 | length=SGIEncode(q+z,(size_t) iris_info.columns,packets+ |
| 1083 | number_packets); |
| 1084 | number_packets+=length; |
| 1085 | offsets[y+z*iris_info.rows]=offset; |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame^] | 1086 | runlength[y+z*iris_info.rows]=(size_t) length; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1087 | offset+=(ssize_t) length; |
| 1088 | } |
| 1089 | q+=(iris_info.columns*4); |
| 1090 | } |
| 1091 | /* |
| 1092 | Write out line start and length tables and runlength-encoded pixels. |
| 1093 | */ |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame^] | 1094 | for (i=0; i < (ssize_t) (iris_info.rows*iris_info.depth); i++) |
| 1095 | (void) WriteBlobMSBLong(image,(size_t) offsets[i]); |
| 1096 | for (i=0; i < (ssize_t) (iris_info.rows*iris_info.depth); i++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1097 | (void) WriteBlobMSBLong(image,runlength[i]); |
| 1098 | (void) WriteBlob(image,number_packets,packets); |
| 1099 | /* |
| 1100 | Relinquish resources. |
| 1101 | */ |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame^] | 1102 | runlength=(size_t *) RelinquishMagickMemory(runlength); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1103 | packets=(unsigned char *) RelinquishMagickMemory(packets); |
| 1104 | offsets=(ssize_t *) RelinquishMagickMemory(offsets); |
| 1105 | break; |
| 1106 | } |
| 1107 | } |
| 1108 | iris_pixels=(unsigned char *) RelinquishMagickMemory(iris_pixels); |
| 1109 | if (GetNextImageInList(image) == (Image *) NULL) |
| 1110 | break; |
| 1111 | image=SyncNextImageInList(image); |
| 1112 | status=SetImageProgress(image,SaveImagesTag,scene++, |
| 1113 | GetImageListLength(image)); |
| 1114 | if (status == MagickFalse) |
| 1115 | break; |
| 1116 | } while (image_info->adjoin != MagickFalse); |
| 1117 | (void) CloseBlob(image); |
| 1118 | return(MagickTrue); |
| 1119 | } |