cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1 | /* |
| 2 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 3 | % % |
| 4 | % % |
| 5 | % % |
| 6 | % M M IIIII FFFFF FFFFF % |
| 7 | % MM MM I F F % |
| 8 | % M M M I FFF FFF % |
| 9 | % M M I F F % |
| 10 | % M M IIIII F F % |
| 11 | % % |
| 12 | % % |
| 13 | % Read/Write MIFF Image Format % |
| 14 | % % |
| 15 | % Software Design % |
| 16 | % John Cristy % |
| 17 | % July 1992 % |
| 18 | % % |
| 19 | % % |
cristy | 7e41fe8 | 2010-12-04 23:12:08 +0000 | [diff] [blame] | 20 | % Copyright 1999-2011 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" |
cristy | 5a2ca48 | 2009-10-14 18:24:56 +0000 | [diff] [blame] | 43 | #include "magick/attribute.h" |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 44 | #include "magick/blob.h" |
| 45 | #include "magick/blob-private.h" |
| 46 | #include "magick/cache.h" |
| 47 | #include "magick/color.h" |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 48 | #include "magick/color-private.h" |
cristy | e7e4055 | 2010-04-24 21:34:22 +0000 | [diff] [blame] | 49 | #include "magick/colormap.h" |
| 50 | #include "magick/colormap-private.h" |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 51 | #include "magick/colorspace.h" |
| 52 | #include "magick/constitute.h" |
| 53 | #include "magick/exception.h" |
| 54 | #include "magick/exception-private.h" |
| 55 | #include "magick/hashmap.h" |
| 56 | #include "magick/geometry.h" |
| 57 | #include "magick/image.h" |
| 58 | #include "magick/image-private.h" |
| 59 | #include "magick/list.h" |
| 60 | #include "magick/magick.h" |
| 61 | #include "magick/memory_.h" |
cristy | f2f2727 | 2009-12-17 14:48:46 +0000 | [diff] [blame] | 62 | #include "magick/module.h" |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 63 | #include "magick/monitor.h" |
| 64 | #include "magick/monitor-private.h" |
| 65 | #include "magick/option.h" |
| 66 | #include "magick/pixel.h" |
| 67 | #include "magick/profile.h" |
| 68 | #include "magick/property.h" |
| 69 | #include "magick/quantum-private.h" |
| 70 | #include "magick/static.h" |
| 71 | #include "magick/statistic.h" |
| 72 | #include "magick/string_.h" |
cristy | f2f2727 | 2009-12-17 14:48:46 +0000 | [diff] [blame] | 73 | #include "magick/string-private.h" |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 74 | #if defined(MAGICKCORE_BZLIB_DELEGATE) |
| 75 | #include "bzlib.h" |
| 76 | #endif |
cristy | 2637717 | 2010-12-20 19:01:58 +0000 | [diff] [blame] | 77 | #if defined(MAGICKCORE_LZMA_DELEGATE) |
| 78 | #include "lzma.h" |
| 79 | #endif |
| 80 | #if defined(MAGICKCORE_ZLIB_DELEGATE) |
| 81 | #include "zlib.h" |
| 82 | #endif |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 83 | |
| 84 | /* |
cristy | a7a341e | 2010-12-22 20:28:51 +0000 | [diff] [blame] | 85 | Define declarations. |
| 86 | */ |
| 87 | #if !defined(LZMA_OK) |
| 88 | #define LZMA_OK 0 |
| 89 | #endif |
| 90 | |
| 91 | /* |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 92 | Forward declarations. |
| 93 | */ |
| 94 | static MagickBooleanType |
| 95 | WriteMIFFImage(const ImageInfo *,Image *); |
| 96 | |
| 97 | /* |
| 98 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 99 | % % |
| 100 | % % |
| 101 | % % |
| 102 | % I s M I F F % |
| 103 | % % |
| 104 | % % |
| 105 | % % |
| 106 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 107 | % |
| 108 | % IsMIFF() returns MagickTrue if the image format type, identified by the |
| 109 | % magick string, is MIFF. |
| 110 | % |
| 111 | % The format of the IsMIFF method is: |
| 112 | % |
| 113 | % MagickBooleanType IsMIFF(const unsigned char *magick,const size_t length) |
| 114 | % |
| 115 | % A description of each parameter follows: |
| 116 | % |
| 117 | % o magick: compare image format pattern against these bytes. |
| 118 | % |
| 119 | % o length: Specifies the length of the magick string. |
| 120 | % |
| 121 | */ |
| 122 | static MagickBooleanType IsMIFF(const unsigned char *magick,const size_t length) |
| 123 | { |
| 124 | if (length < 14) |
| 125 | return(MagickFalse); |
| 126 | if (LocaleNCompare((const char *) magick,"id=ImageMagick",14) == 0) |
| 127 | return(MagickTrue); |
| 128 | return(MagickFalse); |
| 129 | } |
| 130 | |
| 131 | /* |
| 132 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 133 | % % |
| 134 | % % |
| 135 | % % |
| 136 | % R e a d M I F F I m a g e % |
| 137 | % % |
| 138 | % % |
| 139 | % % |
| 140 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 141 | % |
| 142 | % ReadMIFFImage() reads a MIFF image file and returns it. It allocates the |
| 143 | % memory necessary for the new Image structure and returns a pointer to the |
| 144 | % new image. |
| 145 | % |
| 146 | % The format of the ReadMIFFImage method is: |
| 147 | % |
| 148 | % Image *ReadMIFFImage(const ImageInfo *image_info, |
| 149 | % ExceptionInfo *exception) |
| 150 | % |
| 151 | % Decompression code contributed by Kyle Shorter. |
| 152 | % |
| 153 | % A description of each parameter follows: |
| 154 | % |
| 155 | % o image_info: the image info. |
| 156 | % |
| 157 | % o exception: return any errors or warnings in this structure. |
| 158 | % |
| 159 | */ |
| 160 | |
| 161 | #if defined(MAGICKCORE_BZLIB_DELEGATE) |
| 162 | static void *AcquireBZIPMemory(void *context,int items,int size) |
| 163 | { |
| 164 | (void) context; |
| 165 | return((void *) AcquireQuantumMemory((size_t) items,(size_t) size)); |
| 166 | } |
| 167 | #endif |
| 168 | |
cristy | 4b46dba | 2010-12-20 19:18:20 +0000 | [diff] [blame] | 169 | #if defined(MAGICKCORE_LZMA_DELEGATE) |
| 170 | static void *AcquireLZMAMemory(void *context,size_t items,size_t size) |
| 171 | { |
| 172 | (void) context; |
| 173 | return((void *) AcquireQuantumMemory((size_t) items,(size_t) size)); |
| 174 | } |
| 175 | #endif |
| 176 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 177 | #if defined(MAGICKCORE_ZLIB_DELEGATE) |
| 178 | static voidpf AcquireZIPMemory(voidpf context,unsigned int items, |
| 179 | unsigned int size) |
| 180 | { |
| 181 | (void) context; |
| 182 | return((voidpf) AcquireQuantumMemory(items,size)); |
| 183 | } |
| 184 | #endif |
| 185 | |
| 186 | static inline size_t MagickMax(const size_t x,const size_t y) |
| 187 | { |
| 188 | if (x > y) |
| 189 | return(x); |
| 190 | return(y); |
| 191 | } |
| 192 | |
| 193 | static inline size_t MagickMin(const size_t x,const size_t y) |
| 194 | { |
| 195 | if (x < y) |
| 196 | return(x); |
| 197 | return(y); |
| 198 | } |
| 199 | |
| 200 | static void PushRunlengthPacket(Image *image,const unsigned char *pixels, |
| 201 | size_t *length,PixelPacket *pixel,IndexPacket *index) |
| 202 | { |
| 203 | const unsigned char |
| 204 | *p; |
| 205 | |
| 206 | p=pixels; |
| 207 | if (image->storage_class == PseudoClass) |
| 208 | { |
| 209 | *index=(IndexPacket) 0; |
| 210 | switch (image->depth) |
| 211 | { |
| 212 | case 32: |
| 213 | { |
| 214 | *index=ConstrainColormapIndex(image, |
| 215 | (*p << 24) | (*(p+1) << 16) | (*(p+2) << 8) | *(p+3)); |
| 216 | p+=4; |
| 217 | break; |
| 218 | } |
| 219 | case 16: |
| 220 | { |
| 221 | *index=ConstrainColormapIndex(image,(*p << 8) | *(p+1)); |
| 222 | p+=2; |
| 223 | break; |
| 224 | } |
| 225 | case 8: |
| 226 | { |
| 227 | *index=ConstrainColormapIndex(image,*p); |
| 228 | p++; |
| 229 | break; |
| 230 | } |
| 231 | default: |
| 232 | (void) ThrowMagickException(&image->exception,GetMagickModule(), |
| 233 | CorruptImageError,"ImageDepthNotSupported","`%s'",image->filename); |
| 234 | } |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 235 | *pixel=image->colormap[(ssize_t) *index]; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 236 | switch (image->depth) |
| 237 | { |
| 238 | case 8: |
| 239 | { |
| 240 | unsigned char |
| 241 | quantum; |
| 242 | |
| 243 | if (image->matte != MagickFalse) |
| 244 | { |
| 245 | p=PushCharPixel(p,&quantum); |
| 246 | pixel->opacity=ScaleCharToQuantum(quantum); |
| 247 | } |
| 248 | break; |
| 249 | } |
| 250 | case 16: |
| 251 | { |
| 252 | unsigned short |
| 253 | quantum; |
| 254 | |
| 255 | if (image->matte != MagickFalse) |
| 256 | { |
| 257 | p=PushShortPixel(MSBEndian,p,&quantum); |
| 258 | pixel->opacity=(Quantum) (quantum >> (image->depth- |
| 259 | MAGICKCORE_QUANTUM_DEPTH)); |
| 260 | } |
| 261 | break; |
| 262 | } |
| 263 | case 32: |
| 264 | { |
cristy | 4cb162a | 2010-05-30 03:04:47 +0000 | [diff] [blame] | 265 | unsigned int |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 266 | quantum; |
| 267 | |
| 268 | if (image->matte != MagickFalse) |
| 269 | { |
| 270 | p=PushLongPixel(MSBEndian,p,&quantum); |
| 271 | pixel->opacity=(Quantum) (quantum >> (image->depth- |
| 272 | MAGICKCORE_QUANTUM_DEPTH)); |
| 273 | } |
| 274 | break; |
| 275 | } |
| 276 | default: |
| 277 | (void) ThrowMagickException(&image->exception,GetMagickModule(), |
| 278 | CorruptImageError,"ImageDepthNotSupported","`%s'",image->filename); |
| 279 | } |
| 280 | *length=(size_t) (*p++)+1; |
| 281 | return; |
| 282 | } |
| 283 | switch (image->depth) |
| 284 | { |
| 285 | case 8: |
| 286 | { |
| 287 | unsigned char |
| 288 | quantum; |
| 289 | |
| 290 | p=PushCharPixel(p,&quantum); |
| 291 | pixel->red=ScaleCharToQuantum(quantum); |
| 292 | p=PushCharPixel(p,&quantum); |
| 293 | pixel->green=ScaleCharToQuantum(quantum); |
| 294 | p=PushCharPixel(p,&quantum); |
| 295 | pixel->blue=ScaleCharToQuantum(quantum); |
| 296 | if (image->matte != MagickFalse) |
| 297 | { |
| 298 | p=PushCharPixel(p,&quantum); |
| 299 | pixel->opacity=ScaleCharToQuantum(quantum); |
| 300 | } |
| 301 | if (image->colorspace == CMYKColorspace) |
| 302 | { |
| 303 | p=PushCharPixel(p,&quantum); |
| 304 | *index=ScaleCharToQuantum(quantum); |
| 305 | } |
| 306 | break; |
| 307 | } |
| 308 | case 16: |
| 309 | { |
| 310 | unsigned short |
| 311 | quantum; |
| 312 | |
| 313 | p=PushShortPixel(MSBEndian,p,&quantum); |
| 314 | pixel->red=(Quantum) (quantum >> (image->depth-MAGICKCORE_QUANTUM_DEPTH)); |
| 315 | p=PushShortPixel(MSBEndian,p,&quantum); |
| 316 | pixel->green=(Quantum) (quantum >> (image->depth- |
| 317 | MAGICKCORE_QUANTUM_DEPTH)); |
| 318 | p=PushShortPixel(MSBEndian,p,&quantum); |
| 319 | pixel->blue=(Quantum) (quantum >> (image->depth- |
| 320 | MAGICKCORE_QUANTUM_DEPTH)); |
| 321 | if (image->matte != MagickFalse) |
| 322 | { |
| 323 | p=PushShortPixel(MSBEndian,p,&quantum); |
| 324 | pixel->opacity=(Quantum) (quantum >> (image->depth- |
| 325 | MAGICKCORE_QUANTUM_DEPTH)); |
| 326 | } |
| 327 | if (image->colorspace == CMYKColorspace) |
| 328 | { |
| 329 | p=PushShortPixel(MSBEndian,p,&quantum); |
| 330 | *index=(IndexPacket) (quantum >> (image->depth- |
| 331 | MAGICKCORE_QUANTUM_DEPTH)); |
| 332 | } |
| 333 | break; |
| 334 | } |
| 335 | case 32: |
| 336 | { |
cristy | 4cb162a | 2010-05-30 03:04:47 +0000 | [diff] [blame] | 337 | unsigned int |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 338 | quantum; |
| 339 | |
| 340 | p=PushLongPixel(MSBEndian,p,&quantum); |
| 341 | pixel->red=(Quantum) (quantum >> (image->depth-MAGICKCORE_QUANTUM_DEPTH)); |
| 342 | p=PushLongPixel(MSBEndian,p,&quantum); |
| 343 | pixel->green=(Quantum) (quantum >> (image->depth- |
| 344 | MAGICKCORE_QUANTUM_DEPTH)); |
| 345 | p=PushLongPixel(MSBEndian,p,&quantum); |
| 346 | pixel->blue=(Quantum) (quantum >> (image->depth- |
| 347 | MAGICKCORE_QUANTUM_DEPTH)); |
| 348 | if (image->matte != MagickFalse) |
| 349 | { |
| 350 | p=PushLongPixel(MSBEndian,p,&quantum); |
| 351 | pixel->opacity=(Quantum) (quantum >> (image->depth- |
| 352 | MAGICKCORE_QUANTUM_DEPTH)); |
| 353 | } |
| 354 | if (image->colorspace == CMYKColorspace) |
| 355 | { |
| 356 | p=PushLongPixel(MSBEndian,p,&quantum); |
| 357 | *index=(IndexPacket) (quantum >> (image->depth- |
| 358 | MAGICKCORE_QUANTUM_DEPTH)); |
| 359 | } |
| 360 | break; |
| 361 | } |
| 362 | default: |
| 363 | (void) ThrowMagickException(&image->exception,GetMagickModule(), |
| 364 | CorruptImageError,"ImageDepthNotSupported","`%s'",image->filename); |
| 365 | } |
| 366 | *length=(size_t) (*p++)+1; |
| 367 | } |
| 368 | |
cristy | 4b46dba | 2010-12-20 19:18:20 +0000 | [diff] [blame] | 369 | #if defined(MAGICKCORE_BZLIB_DELEGATE) |
| 370 | static void RelinquishBZIPMemory(void *context,void *memory) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 371 | { |
| 372 | (void) context; |
| 373 | memory=RelinquishMagickMemory(memory); |
| 374 | } |
| 375 | #endif |
| 376 | |
cristy | 4b46dba | 2010-12-20 19:18:20 +0000 | [diff] [blame] | 377 | #if defined(MAGICKCORE_LZMA_DELEGATE) |
| 378 | static void RelinquishLZMAMemory(void *context,void *memory) |
| 379 | { |
| 380 | (void) context; |
| 381 | memory=RelinquishMagickMemory(memory); |
| 382 | } |
| 383 | #endif |
| 384 | |
| 385 | #if defined(MAGICKCORE_ZLIB_DELEGATE) |
| 386 | static void RelinquishZIPMemory(voidpf context,voidpf memory) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 387 | { |
| 388 | (void) context; |
| 389 | memory=RelinquishMagickMemory(memory); |
| 390 | } |
| 391 | #endif |
| 392 | |
| 393 | static Image *ReadMIFFImage(const ImageInfo *image_info, |
| 394 | ExceptionInfo *exception) |
| 395 | { |
| 396 | #define BZipMaxExtent(x) ((x)+((x)/100)+600) |
cristy | 2637717 | 2010-12-20 19:01:58 +0000 | [diff] [blame] | 397 | #define LZMAMaxExtent(x) ((x)+((x)/3)+128) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 398 | #define ZipMaxExtent(x) ((x)+(((x)+7) >> 3)+(((x)+63) >> 6)+11) |
| 399 | |
| 400 | #if defined(MAGICKCORE_BZLIB_DELEGATE) |
| 401 | bz_stream |
| 402 | bzip_info; |
| 403 | #endif |
| 404 | |
| 405 | char |
| 406 | id[MaxTextExtent], |
| 407 | keyword[MaxTextExtent], |
| 408 | *options; |
| 409 | |
| 410 | const unsigned char |
| 411 | *p; |
| 412 | |
| 413 | double |
| 414 | version; |
| 415 | |
| 416 | GeometryInfo |
| 417 | geometry_info; |
| 418 | |
| 419 | Image |
| 420 | *image; |
| 421 | |
| 422 | IndexPacket |
| 423 | index; |
| 424 | |
| 425 | int |
| 426 | c, |
| 427 | code; |
| 428 | |
cristy | 2637717 | 2010-12-20 19:01:58 +0000 | [diff] [blame] | 429 | #if defined(MAGICKCORE_LZMA_DELEGATE) |
| 430 | lzma_stream |
cristy | 330af6c | 2010-12-21 14:36:06 +0000 | [diff] [blame] | 431 | initialize_lzma = LZMA_STREAM_INIT, |
| 432 | lzma_info; |
cristy | 4b46dba | 2010-12-20 19:18:20 +0000 | [diff] [blame] | 433 | |
| 434 | lzma_allocator |
| 435 | allocator; |
cristy | 2637717 | 2010-12-20 19:01:58 +0000 | [diff] [blame] | 436 | #endif |
| 437 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 438 | LinkedListInfo |
| 439 | *profiles; |
| 440 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 441 | MagickBooleanType |
| 442 | status; |
| 443 | |
| 444 | MagickStatusType |
| 445 | flags; |
| 446 | |
| 447 | PixelPacket |
| 448 | pixel; |
| 449 | |
| 450 | QuantumFormatType |
| 451 | quantum_format; |
| 452 | |
| 453 | QuantumInfo |
| 454 | *quantum_info; |
| 455 | |
| 456 | QuantumType |
| 457 | quantum_type; |
| 458 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 459 | register ssize_t |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 460 | i; |
| 461 | |
| 462 | size_t |
| 463 | length, |
| 464 | packet_size; |
| 465 | |
| 466 | ssize_t |
| 467 | count; |
| 468 | |
| 469 | unsigned char |
| 470 | *compress_pixels, |
| 471 | *pixels; |
| 472 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 473 | size_t |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 474 | colors; |
| 475 | |
cristy | 2637717 | 2010-12-20 19:01:58 +0000 | [diff] [blame] | 476 | ssize_t |
| 477 | y; |
| 478 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 479 | #if defined(MAGICKCORE_ZLIB_DELEGATE) |
| 480 | z_stream |
| 481 | zip_info; |
| 482 | #endif |
| 483 | |
| 484 | /* |
| 485 | Open image file. |
| 486 | */ |
| 487 | assert(image_info != (const ImageInfo *) NULL); |
| 488 | assert(image_info->signature == MagickSignature); |
| 489 | if (image_info->debug != MagickFalse) |
| 490 | (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s", |
| 491 | image_info->filename); |
| 492 | assert(exception != (ExceptionInfo *) NULL); |
| 493 | assert(exception->signature == MagickSignature); |
| 494 | image=AcquireImage(image_info); |
| 495 | status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception); |
| 496 | if (status == MagickFalse) |
| 497 | { |
| 498 | image=DestroyImageList(image); |
| 499 | return((Image *) NULL); |
| 500 | } |
| 501 | /* |
| 502 | Decode image header; header terminates one character beyond a ':'. |
| 503 | */ |
| 504 | c=ReadBlobByte(image); |
| 505 | if (c == EOF) |
| 506 | ThrowReaderException(CorruptImageError,"ImproperImageHeader"); |
| 507 | code=0; |
| 508 | *id='\0'; |
| 509 | (void) ResetMagickMemory(keyword,0,sizeof(keyword)); |
| 510 | version=0.0; |
| 511 | do |
| 512 | { |
| 513 | /* |
| 514 | Decode image header; header terminates one character beyond a ':'. |
| 515 | */ |
| 516 | length=MaxTextExtent; |
| 517 | options=AcquireString((char *) NULL); |
| 518 | quantum_format=UndefinedQuantumFormat; |
| 519 | profiles=(LinkedListInfo *) NULL; |
| 520 | colors=0; |
| 521 | image->depth=8UL; |
| 522 | image->compression=NoCompression; |
| 523 | while ((isgraph(c) != MagickFalse) && (c != (int) ':')) |
| 524 | { |
| 525 | register char |
| 526 | *p; |
| 527 | |
| 528 | if (c == (int) '{') |
| 529 | { |
| 530 | char |
| 531 | *comment; |
| 532 | |
| 533 | /* |
| 534 | Read comment-- any text between { }. |
| 535 | */ |
| 536 | length=MaxTextExtent; |
| 537 | comment=AcquireString((char *) NULL); |
| 538 | for (p=comment; comment != (char *) NULL; p++) |
| 539 | { |
| 540 | c=ReadBlobByte(image); |
| 541 | if ((c == EOF) || (c == (int) '}')) |
| 542 | break; |
| 543 | if ((size_t) (p-comment+1) >= length) |
| 544 | { |
| 545 | *p='\0'; |
| 546 | length<<=1; |
| 547 | comment=(char *) ResizeQuantumMemory(comment,length+ |
| 548 | MaxTextExtent,sizeof(*comment)); |
| 549 | if (comment == (char *) NULL) |
| 550 | break; |
| 551 | p=comment+strlen(comment); |
| 552 | } |
| 553 | *p=(char) c; |
| 554 | } |
| 555 | if (comment == (char *) NULL) |
| 556 | ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed"); |
| 557 | *p='\0'; |
| 558 | (void) SetImageProperty(image,"comment",comment); |
| 559 | comment=DestroyString(comment); |
| 560 | c=ReadBlobByte(image); |
| 561 | } |
| 562 | else |
| 563 | if (isalnum(c) != MagickFalse) |
| 564 | { |
| 565 | /* |
| 566 | Get the keyword. |
| 567 | */ |
| 568 | p=keyword; |
| 569 | do |
| 570 | { |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 571 | if (c == (int) '=') |
| 572 | break; |
| 573 | if ((size_t) (p-keyword) < (MaxTextExtent-1)) |
| 574 | *p++=(char) c; |
| 575 | c=ReadBlobByte(image); |
| 576 | } while (c != EOF); |
| 577 | *p='\0'; |
| 578 | p=options; |
cristy | 93505cf | 2010-08-10 21:37:49 +0000 | [diff] [blame] | 579 | while ((isspace((int) ((unsigned char) c)) != 0) && (c != EOF)) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 580 | c=ReadBlobByte(image); |
| 581 | if (c == (int) '=') |
| 582 | { |
| 583 | /* |
| 584 | Get the keyword value. |
| 585 | */ |
| 586 | c=ReadBlobByte(image); |
| 587 | while ((c != (int) '}') && (c != EOF)) |
| 588 | { |
| 589 | if ((size_t) (p-options+1) >= length) |
| 590 | { |
| 591 | *p='\0'; |
| 592 | length<<=1; |
| 593 | options=(char *) ResizeQuantumMemory(options,length+ |
| 594 | MaxTextExtent,sizeof(*options)); |
| 595 | if (options == (char *) NULL) |
| 596 | break; |
| 597 | p=options+strlen(options); |
| 598 | } |
| 599 | if (options == (char *) NULL) |
| 600 | ThrowReaderException(ResourceLimitError, |
| 601 | "MemoryAllocationFailed"); |
| 602 | *p++=(char) c; |
| 603 | c=ReadBlobByte(image); |
| 604 | if (*options != '{') |
| 605 | if (isspace((int) ((unsigned char) c)) != 0) |
| 606 | break; |
| 607 | } |
| 608 | } |
| 609 | *p='\0'; |
| 610 | if (*options == '{') |
| 611 | (void) CopyMagickString(options,options+1,MaxTextExtent); |
| 612 | /* |
| 613 | Assign a value to the specified keyword. |
| 614 | */ |
| 615 | switch (*keyword) |
| 616 | { |
| 617 | case 'b': |
| 618 | case 'B': |
| 619 | { |
| 620 | if (LocaleCompare(keyword,"background-color") == 0) |
| 621 | { |
| 622 | (void) QueryColorDatabase(options,&image->background_color, |
| 623 | exception); |
| 624 | break; |
| 625 | } |
| 626 | if (LocaleCompare(keyword,"blue-primary") == 0) |
| 627 | { |
| 628 | flags=ParseGeometry(options,&geometry_info); |
| 629 | image->chromaticity.blue_primary.x=geometry_info.rho; |
| 630 | image->chromaticity.blue_primary.y=geometry_info.sigma; |
| 631 | if ((flags & SigmaValue) == 0) |
| 632 | image->chromaticity.blue_primary.y= |
| 633 | image->chromaticity.blue_primary.x; |
| 634 | break; |
| 635 | } |
| 636 | if (LocaleCompare(keyword,"border-color") == 0) |
| 637 | { |
| 638 | (void) QueryColorDatabase(options,&image->border_color, |
| 639 | exception); |
| 640 | break; |
| 641 | } |
| 642 | (void) SetImageProperty(image,keyword,options); |
| 643 | break; |
| 644 | } |
| 645 | case 'c': |
| 646 | case 'C': |
| 647 | { |
| 648 | if (LocaleCompare(keyword,"class") == 0) |
| 649 | { |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 650 | ssize_t |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 651 | storage_class; |
| 652 | |
cristy | 042ee78 | 2011-04-22 18:48:30 +0000 | [diff] [blame] | 653 | storage_class=ParseCommandOption(MagickClassOptions, |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 654 | MagickFalse,options); |
| 655 | if (storage_class < 0) |
| 656 | break; |
| 657 | image->storage_class=(ClassType) storage_class; |
| 658 | break; |
| 659 | } |
| 660 | if (LocaleCompare(keyword,"colors") == 0) |
| 661 | { |
cristy | e27293e | 2009-12-18 02:53:20 +0000 | [diff] [blame] | 662 | colors=StringToUnsignedLong(options); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 663 | break; |
| 664 | } |
| 665 | if (LocaleCompare(keyword,"colorspace") == 0) |
| 666 | { |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 667 | ssize_t |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 668 | colorspace; |
| 669 | |
cristy | 042ee78 | 2011-04-22 18:48:30 +0000 | [diff] [blame] | 670 | colorspace=ParseCommandOption(MagickColorspaceOptions, |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 671 | MagickFalse,options); |
| 672 | if (colorspace < 0) |
| 673 | break; |
| 674 | image->colorspace=(ColorspaceType) colorspace; |
| 675 | break; |
| 676 | } |
| 677 | if (LocaleCompare(keyword,"compression") == 0) |
| 678 | { |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 679 | ssize_t |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 680 | compression; |
| 681 | |
cristy | 042ee78 | 2011-04-22 18:48:30 +0000 | [diff] [blame] | 682 | compression=ParseCommandOption(MagickCompressOptions, |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 683 | MagickFalse,options); |
| 684 | if (compression < 0) |
| 685 | break; |
| 686 | image->compression=(CompressionType) compression; |
| 687 | break; |
| 688 | } |
| 689 | if (LocaleCompare(keyword,"columns") == 0) |
| 690 | { |
cristy | e27293e | 2009-12-18 02:53:20 +0000 | [diff] [blame] | 691 | image->columns=StringToUnsignedLong(options); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 692 | break; |
| 693 | } |
| 694 | (void) SetImageProperty(image,keyword,options); |
| 695 | break; |
| 696 | } |
| 697 | case 'd': |
| 698 | case 'D': |
| 699 | { |
| 700 | if (LocaleCompare(keyword,"delay") == 0) |
| 701 | { |
cristy | e27293e | 2009-12-18 02:53:20 +0000 | [diff] [blame] | 702 | image->delay=StringToUnsignedLong(options); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 703 | break; |
| 704 | } |
| 705 | if (LocaleCompare(keyword,"depth") == 0) |
| 706 | { |
cristy | e27293e | 2009-12-18 02:53:20 +0000 | [diff] [blame] | 707 | image->depth=StringToUnsignedLong(options); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 708 | break; |
| 709 | } |
| 710 | if (LocaleCompare(keyword,"dispose") == 0) |
| 711 | { |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 712 | ssize_t |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 713 | dispose; |
| 714 | |
cristy | 042ee78 | 2011-04-22 18:48:30 +0000 | [diff] [blame] | 715 | dispose=ParseCommandOption(MagickDisposeOptions,MagickFalse, |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 716 | options); |
| 717 | if (dispose < 0) |
| 718 | break; |
| 719 | image->dispose=(DisposeType) dispose; |
| 720 | break; |
| 721 | } |
| 722 | (void) SetImageProperty(image,keyword,options); |
| 723 | break; |
| 724 | } |
| 725 | case 'e': |
| 726 | case 'E': |
| 727 | { |
| 728 | if (LocaleCompare(keyword,"endian") == 0) |
| 729 | { |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 730 | ssize_t |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 731 | endian; |
| 732 | |
cristy | 042ee78 | 2011-04-22 18:48:30 +0000 | [diff] [blame] | 733 | endian=ParseCommandOption(MagickEndianOptions,MagickFalse, |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 734 | options); |
| 735 | if (endian < 0) |
| 736 | break; |
| 737 | image->endian=(EndianType) endian; |
| 738 | break; |
| 739 | } |
| 740 | (void) SetImageProperty(image,keyword,options); |
| 741 | break; |
| 742 | } |
| 743 | case 'g': |
| 744 | case 'G': |
| 745 | { |
| 746 | if (LocaleCompare(keyword,"gamma") == 0) |
| 747 | { |
cristy | f2f2727 | 2009-12-17 14:48:46 +0000 | [diff] [blame] | 748 | image->gamma=StringToDouble(options); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 749 | break; |
| 750 | } |
| 751 | if (LocaleCompare(keyword,"gravity") == 0) |
| 752 | { |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 753 | ssize_t |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 754 | gravity; |
| 755 | |
cristy | 042ee78 | 2011-04-22 18:48:30 +0000 | [diff] [blame] | 756 | gravity=ParseCommandOption(MagickGravityOptions,MagickFalse, |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 757 | options); |
| 758 | if (gravity < 0) |
| 759 | break; |
| 760 | image->gravity=(GravityType) gravity; |
| 761 | break; |
| 762 | } |
| 763 | if (LocaleCompare(keyword,"green-primary") == 0) |
| 764 | { |
| 765 | flags=ParseGeometry(options,&geometry_info); |
| 766 | image->chromaticity.green_primary.x=geometry_info.rho; |
| 767 | image->chromaticity.green_primary.y=geometry_info.sigma; |
| 768 | if ((flags & SigmaValue) == 0) |
| 769 | image->chromaticity.green_primary.y= |
| 770 | image->chromaticity.green_primary.x; |
| 771 | break; |
| 772 | } |
| 773 | (void) SetImageProperty(image,keyword,options); |
| 774 | break; |
| 775 | } |
| 776 | case 'i': |
| 777 | case 'I': |
| 778 | { |
| 779 | if (LocaleCompare(keyword,"id") == 0) |
| 780 | { |
| 781 | (void) CopyMagickString(id,options,MaxTextExtent); |
| 782 | break; |
| 783 | } |
| 784 | if (LocaleCompare(keyword,"iterations") == 0) |
| 785 | { |
cristy | e27293e | 2009-12-18 02:53:20 +0000 | [diff] [blame] | 786 | image->iterations=StringToUnsignedLong(options); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 787 | break; |
| 788 | } |
| 789 | (void) SetImageProperty(image,keyword,options); |
| 790 | break; |
| 791 | } |
| 792 | case 'm': |
| 793 | case 'M': |
| 794 | { |
| 795 | if (LocaleCompare(keyword,"matte") == 0) |
| 796 | { |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 797 | ssize_t |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 798 | matte; |
| 799 | |
cristy | 042ee78 | 2011-04-22 18:48:30 +0000 | [diff] [blame] | 800 | matte=ParseCommandOption(MagickBooleanOptions,MagickFalse, |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 801 | options); |
| 802 | if (matte < 0) |
| 803 | break; |
| 804 | image->matte=(MagickBooleanType) matte; |
| 805 | break; |
| 806 | } |
| 807 | if (LocaleCompare(keyword,"matte-color") == 0) |
| 808 | { |
| 809 | (void) QueryColorDatabase(options,&image->matte_color, |
| 810 | exception); |
| 811 | break; |
| 812 | } |
| 813 | if (LocaleCompare(keyword,"montage") == 0) |
| 814 | { |
| 815 | (void) CloneString(&image->montage,options); |
| 816 | break; |
| 817 | } |
| 818 | (void) SetImageProperty(image,keyword,options); |
| 819 | break; |
| 820 | } |
| 821 | case 'o': |
| 822 | case 'O': |
| 823 | { |
| 824 | if (LocaleCompare(keyword,"opaque") == 0) |
| 825 | { |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 826 | ssize_t |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 827 | matte; |
| 828 | |
cristy | 042ee78 | 2011-04-22 18:48:30 +0000 | [diff] [blame] | 829 | matte=ParseCommandOption(MagickBooleanOptions,MagickFalse, |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 830 | options); |
| 831 | if (matte < 0) |
| 832 | break; |
| 833 | image->matte=(MagickBooleanType) matte; |
| 834 | break; |
| 835 | } |
| 836 | if (LocaleCompare(keyword,"orientation") == 0) |
| 837 | { |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 838 | ssize_t |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 839 | orientation; |
| 840 | |
cristy | 042ee78 | 2011-04-22 18:48:30 +0000 | [diff] [blame] | 841 | orientation=ParseCommandOption(MagickOrientationOptions, |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 842 | MagickFalse,options); |
| 843 | if (orientation < 0) |
| 844 | break; |
| 845 | image->orientation=(OrientationType) orientation; |
| 846 | break; |
| 847 | } |
| 848 | (void) SetImageProperty(image,keyword,options); |
| 849 | break; |
| 850 | } |
| 851 | case 'p': |
| 852 | case 'P': |
| 853 | { |
| 854 | if (LocaleCompare(keyword,"page") == 0) |
| 855 | { |
| 856 | char |
| 857 | *geometry; |
| 858 | |
| 859 | geometry=GetPageGeometry(options); |
| 860 | (void) ParseAbsoluteGeometry(geometry,&image->page); |
| 861 | geometry=DestroyString(geometry); |
| 862 | break; |
| 863 | } |
| 864 | if ((LocaleNCompare(keyword,"profile:",8) == 0) || |
| 865 | (LocaleNCompare(keyword,"profile-",8) == 0)) |
| 866 | { |
| 867 | StringInfo |
| 868 | *profile; |
| 869 | |
| 870 | if (profiles == (LinkedListInfo *) NULL) |
| 871 | profiles=NewLinkedList(0); |
| 872 | (void) AppendValueToLinkedList(profiles, |
| 873 | AcquireString(keyword+8)); |
cristy | f2f2727 | 2009-12-17 14:48:46 +0000 | [diff] [blame] | 874 | profile=AcquireStringInfo((size_t) StringToLong(options)); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 875 | (void) SetImageProfile(image,keyword+8,profile); |
| 876 | profile=DestroyStringInfo(profile); |
| 877 | break; |
| 878 | } |
| 879 | (void) SetImageProperty(image,keyword,options); |
| 880 | break; |
| 881 | } |
| 882 | case 'q': |
| 883 | case 'Q': |
| 884 | { |
| 885 | if (LocaleCompare(keyword,"quality") == 0) |
| 886 | { |
cristy | e27293e | 2009-12-18 02:53:20 +0000 | [diff] [blame] | 887 | image->quality=StringToUnsignedLong(options); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 888 | break; |
| 889 | } |
| 890 | if ((LocaleCompare(keyword,"quantum-format") == 0) || |
| 891 | (LocaleCompare(keyword,"quantum:format") == 0)) |
| 892 | { |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 893 | ssize_t |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 894 | format; |
| 895 | |
cristy | 042ee78 | 2011-04-22 18:48:30 +0000 | [diff] [blame] | 896 | format=ParseCommandOption(MagickQuantumFormatOptions, |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 897 | MagickFalse,options); |
| 898 | if (format < 0) |
| 899 | break; |
| 900 | quantum_format=(QuantumFormatType) format; |
| 901 | break; |
| 902 | } |
| 903 | (void) SetImageProperty(image,keyword,options); |
| 904 | break; |
| 905 | } |
| 906 | case 'r': |
| 907 | case 'R': |
| 908 | { |
| 909 | if (LocaleCompare(keyword,"red-primary") == 0) |
| 910 | { |
| 911 | flags=ParseGeometry(options,&geometry_info); |
| 912 | image->chromaticity.red_primary.x=geometry_info.rho; |
| 913 | image->chromaticity.red_primary.y=geometry_info.sigma; |
| 914 | if ((flags & SigmaValue) == 0) |
| 915 | image->chromaticity.red_primary.y= |
| 916 | image->chromaticity.red_primary.x; |
| 917 | break; |
| 918 | } |
| 919 | if (LocaleCompare(keyword,"rendering-intent") == 0) |
| 920 | { |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 921 | ssize_t |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 922 | rendering_intent; |
| 923 | |
cristy | 042ee78 | 2011-04-22 18:48:30 +0000 | [diff] [blame] | 924 | rendering_intent=ParseCommandOption(MagickIntentOptions, |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 925 | MagickFalse,options); |
| 926 | if (rendering_intent < 0) |
| 927 | break; |
| 928 | image->rendering_intent=(RenderingIntent) rendering_intent; |
| 929 | break; |
| 930 | } |
| 931 | if (LocaleCompare(keyword,"resolution") == 0) |
| 932 | { |
| 933 | flags=ParseGeometry(options,&geometry_info); |
| 934 | image->x_resolution=geometry_info.rho; |
| 935 | image->y_resolution=geometry_info.sigma; |
| 936 | if ((flags & SigmaValue) == 0) |
| 937 | image->y_resolution=image->x_resolution; |
| 938 | break; |
| 939 | } |
| 940 | if (LocaleCompare(keyword,"rows") == 0) |
| 941 | { |
cristy | e27293e | 2009-12-18 02:53:20 +0000 | [diff] [blame] | 942 | image->rows=StringToUnsignedLong(options); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 943 | break; |
| 944 | } |
| 945 | (void) SetImageProperty(image,keyword,options); |
| 946 | break; |
| 947 | } |
| 948 | case 's': |
| 949 | case 'S': |
| 950 | { |
| 951 | if (LocaleCompare(keyword,"scene") == 0) |
| 952 | { |
cristy | e27293e | 2009-12-18 02:53:20 +0000 | [diff] [blame] | 953 | image->scene=StringToUnsignedLong(options); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 954 | break; |
| 955 | } |
| 956 | (void) SetImageProperty(image,keyword,options); |
| 957 | break; |
| 958 | } |
| 959 | case 't': |
| 960 | case 'T': |
| 961 | { |
| 962 | if (LocaleCompare(keyword,"ticks-per-second") == 0) |
| 963 | { |
cristy | 15893a4 | 2010-11-20 18:57:15 +0000 | [diff] [blame] | 964 | image->ticks_per_second=(ssize_t) StringToLong(options); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 965 | break; |
| 966 | } |
| 967 | if (LocaleCompare(keyword,"tile-offset") == 0) |
| 968 | { |
| 969 | char |
| 970 | *geometry; |
| 971 | |
| 972 | geometry=GetPageGeometry(options); |
| 973 | (void) ParseAbsoluteGeometry(geometry,&image->tile_offset); |
| 974 | geometry=DestroyString(geometry); |
| 975 | break; |
| 976 | } |
| 977 | if (LocaleCompare(keyword,"type") == 0) |
| 978 | { |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 979 | ssize_t |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 980 | type; |
| 981 | |
cristy | 042ee78 | 2011-04-22 18:48:30 +0000 | [diff] [blame] | 982 | type=ParseCommandOption(MagickTypeOptions,MagickFalse, |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 983 | options); |
| 984 | if (type < 0) |
| 985 | break; |
| 986 | image->type=(ImageType) type; |
| 987 | break; |
| 988 | } |
| 989 | (void) SetImageProperty(image,keyword,options); |
| 990 | break; |
| 991 | } |
| 992 | case 'u': |
| 993 | case 'U': |
| 994 | { |
| 995 | if (LocaleCompare(keyword,"units") == 0) |
| 996 | { |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 997 | ssize_t |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 998 | units; |
| 999 | |
cristy | 042ee78 | 2011-04-22 18:48:30 +0000 | [diff] [blame] | 1000 | units=ParseCommandOption(MagickResolutionOptions,MagickFalse, |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1001 | options); |
| 1002 | if (units < 0) |
| 1003 | break; |
| 1004 | image->units=(ResolutionType) units; |
| 1005 | break; |
| 1006 | } |
| 1007 | (void) SetImageProperty(image,keyword,options); |
| 1008 | break; |
| 1009 | } |
| 1010 | case 'v': |
| 1011 | case 'V': |
| 1012 | { |
| 1013 | if (LocaleCompare(keyword,"version") == 0) |
| 1014 | { |
cristy | f2f2727 | 2009-12-17 14:48:46 +0000 | [diff] [blame] | 1015 | version=StringToDouble(options); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1016 | break; |
| 1017 | } |
| 1018 | (void) SetImageProperty(image,keyword,options); |
| 1019 | break; |
| 1020 | } |
| 1021 | case 'w': |
| 1022 | case 'W': |
| 1023 | { |
| 1024 | if (LocaleCompare(keyword,"white-point") == 0) |
| 1025 | { |
| 1026 | flags=ParseGeometry(options,&geometry_info); |
| 1027 | image->chromaticity.white_point.x=geometry_info.rho; |
| 1028 | image->chromaticity.white_point.y=geometry_info.rho; |
| 1029 | if ((flags & SigmaValue) != 0) |
| 1030 | image->chromaticity.white_point.y= |
| 1031 | image->chromaticity.white_point.x; |
| 1032 | break; |
| 1033 | } |
| 1034 | (void) SetImageProperty(image,keyword,options); |
| 1035 | break; |
| 1036 | } |
| 1037 | default: |
| 1038 | { |
| 1039 | (void) SetImageProperty(image,keyword,options); |
| 1040 | break; |
| 1041 | } |
| 1042 | } |
| 1043 | } |
| 1044 | else |
| 1045 | c=ReadBlobByte(image); |
| 1046 | while (isspace((int) ((unsigned char) c)) != 0) |
| 1047 | c=ReadBlobByte(image); |
| 1048 | } |
| 1049 | options=DestroyString(options); |
| 1050 | (void) ReadBlobByte(image); |
| 1051 | /* |
| 1052 | Verify that required image information is defined. |
| 1053 | */ |
| 1054 | if ((LocaleCompare(id,"ImageMagick") != 0) || |
| 1055 | (image->storage_class == UndefinedClass) || |
| 1056 | (image->columns == 0) || (image->rows == 0)) |
| 1057 | ThrowReaderException(CorruptImageError,"ImproperImageHeader"); |
| 1058 | if (image->montage != (char *) NULL) |
| 1059 | { |
| 1060 | register char |
| 1061 | *p; |
| 1062 | |
| 1063 | /* |
| 1064 | Image directory. |
| 1065 | */ |
| 1066 | length=MaxTextExtent; |
| 1067 | image->directory=AcquireString((char *) NULL); |
| 1068 | p=image->directory; |
| 1069 | do |
| 1070 | { |
| 1071 | *p='\0'; |
| 1072 | if ((strlen(image->directory)+MaxTextExtent) >= length) |
| 1073 | { |
| 1074 | /* |
| 1075 | Allocate more memory for the image directory. |
| 1076 | */ |
| 1077 | length<<=1; |
| 1078 | image->directory=(char *) ResizeQuantumMemory(image->directory, |
| 1079 | length+MaxTextExtent,sizeof(*image->directory)); |
| 1080 | if (image->directory == (char *) NULL) |
| 1081 | ThrowReaderException(CorruptImageError,"UnableToReadImageData"); |
| 1082 | p=image->directory+strlen(image->directory); |
| 1083 | } |
| 1084 | c=ReadBlobByte(image); |
| 1085 | *p++=(char) c; |
| 1086 | } while (c != (int) '\0'); |
| 1087 | } |
| 1088 | if (profiles != (LinkedListInfo *) NULL) |
| 1089 | { |
| 1090 | const char |
| 1091 | *name; |
| 1092 | |
| 1093 | const StringInfo |
| 1094 | *profile; |
| 1095 | |
| 1096 | /* |
| 1097 | Read image profiles. |
| 1098 | */ |
| 1099 | ResetLinkedListIterator(profiles); |
| 1100 | name=(const char *) GetNextValueInLinkedList(profiles); |
| 1101 | while (name != (const char *) NULL) |
| 1102 | { |
| 1103 | profile=GetImageProfile(image,name); |
| 1104 | if (profile != (StringInfo *) NULL) |
| 1105 | { |
| 1106 | register unsigned char |
| 1107 | *p; |
| 1108 | |
| 1109 | p=GetStringInfoDatum(profile); |
| 1110 | count=ReadBlob(image,GetStringInfoLength(profile),p); |
cristy | da16f16 | 2011-02-19 23:52:17 +0000 | [diff] [blame] | 1111 | (void) count; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1112 | } |
| 1113 | name=(const char *) GetNextValueInLinkedList(profiles); |
| 1114 | } |
| 1115 | profiles=DestroyLinkedList(profiles,RelinquishMagickMemory); |
| 1116 | } |
| 1117 | image->depth=GetImageQuantumDepth(image,MagickFalse); |
| 1118 | if (image->storage_class == PseudoClass) |
| 1119 | { |
| 1120 | /* |
| 1121 | Create image colormap. |
| 1122 | */ |
| 1123 | status=AcquireImageColormap(image,colors != 0 ? colors : 256); |
| 1124 | if (status == MagickFalse) |
| 1125 | ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed"); |
| 1126 | if (colors != 0) |
| 1127 | { |
| 1128 | size_t |
| 1129 | packet_size; |
| 1130 | |
| 1131 | unsigned char |
| 1132 | *colormap; |
| 1133 | |
| 1134 | /* |
| 1135 | Read image colormap from file. |
| 1136 | */ |
| 1137 | packet_size=(size_t) (3UL*image->depth/8UL); |
| 1138 | colormap=(unsigned char *) AcquireQuantumMemory(image->colors, |
| 1139 | packet_size*sizeof(*colormap)); |
| 1140 | if (colormap == (unsigned char *) NULL) |
| 1141 | ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed"); |
| 1142 | count=ReadBlob(image,packet_size*image->colors,colormap); |
| 1143 | p=colormap; |
| 1144 | switch (image->depth) |
| 1145 | { |
| 1146 | default: |
| 1147 | ThrowReaderException(CorruptImageError, |
| 1148 | "ImageDepthNotSupported"); |
| 1149 | case 8: |
| 1150 | { |
| 1151 | unsigned char |
| 1152 | pixel; |
| 1153 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1154 | for (i=0; i < (ssize_t) image->colors; i++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1155 | { |
| 1156 | p=PushCharPixel(p,&pixel); |
| 1157 | image->colormap[i].red=ScaleCharToQuantum(pixel); |
| 1158 | p=PushCharPixel(p,&pixel); |
| 1159 | image->colormap[i].green=ScaleCharToQuantum(pixel); |
| 1160 | p=PushCharPixel(p,&pixel); |
| 1161 | image->colormap[i].blue=ScaleCharToQuantum(pixel); |
| 1162 | } |
| 1163 | break; |
| 1164 | } |
| 1165 | case 16: |
| 1166 | { |
| 1167 | unsigned short |
| 1168 | pixel; |
| 1169 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1170 | for (i=0; i < (ssize_t) image->colors; i++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1171 | { |
| 1172 | p=PushShortPixel(MSBEndian,p,&pixel); |
| 1173 | image->colormap[i].red=ScaleShortToQuantum(pixel); |
| 1174 | p=PushShortPixel(MSBEndian,p,&pixel); |
| 1175 | image->colormap[i].green=ScaleShortToQuantum(pixel); |
| 1176 | p=PushShortPixel(MSBEndian,p,&pixel); |
| 1177 | image->colormap[i].blue=ScaleShortToQuantum(pixel); |
| 1178 | } |
| 1179 | break; |
| 1180 | } |
| 1181 | case 32: |
| 1182 | { |
cristy | 4cb162a | 2010-05-30 03:04:47 +0000 | [diff] [blame] | 1183 | unsigned int |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1184 | pixel; |
| 1185 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1186 | for (i=0; i < (ssize_t) image->colors; i++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1187 | { |
| 1188 | p=PushLongPixel(MSBEndian,p,&pixel); |
| 1189 | image->colormap[i].red=ScaleLongToQuantum(pixel); |
| 1190 | p=PushLongPixel(MSBEndian,p,&pixel); |
| 1191 | image->colormap[i].green=ScaleLongToQuantum(pixel); |
| 1192 | p=PushLongPixel(MSBEndian,p,&pixel); |
| 1193 | image->colormap[i].blue=ScaleLongToQuantum(pixel); |
| 1194 | } |
| 1195 | break; |
| 1196 | } |
| 1197 | } |
| 1198 | colormap=(unsigned char *) RelinquishMagickMemory(colormap); |
| 1199 | } |
| 1200 | } |
| 1201 | if ((image_info->ping != MagickFalse) && (image_info->number_scenes != 0)) |
| 1202 | if (image->scene >= (image_info->scene+image_info->number_scenes-1)) |
| 1203 | break; |
| 1204 | /* |
| 1205 | Allocate image pixels. |
| 1206 | */ |
| 1207 | quantum_info=AcquireQuantumInfo(image_info,image); |
| 1208 | if (quantum_info == (QuantumInfo *) NULL) |
| 1209 | ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed"); |
| 1210 | if (quantum_format != UndefinedQuantumFormat) |
| 1211 | { |
| 1212 | status=SetQuantumFormat(image,quantum_info,quantum_format); |
| 1213 | if (status == MagickFalse) |
| 1214 | ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed"); |
| 1215 | } |
| 1216 | packet_size=(size_t) (quantum_info->depth/8); |
| 1217 | if (image->storage_class == DirectClass) |
| 1218 | packet_size=(size_t) (3*quantum_info->depth/8); |
| 1219 | if (image->matte != MagickFalse) |
| 1220 | packet_size+=quantum_info->depth/8; |
| 1221 | if (image->colorspace == CMYKColorspace) |
| 1222 | packet_size+=quantum_info->depth/8; |
| 1223 | if (image->compression == RLECompression) |
| 1224 | packet_size++; |
| 1225 | length=image->columns; |
cristy | 2637717 | 2010-12-20 19:01:58 +0000 | [diff] [blame] | 1226 | length=MagickMax(MagickMax(BZipMaxExtent(packet_size*image->columns), |
| 1227 | LZMAMaxExtent(packet_size*image->columns)),ZipMaxExtent(packet_size* |
| 1228 | image->columns)); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1229 | compress_pixels=(unsigned char *) AcquireQuantumMemory(length, |
| 1230 | sizeof(*compress_pixels)); |
| 1231 | if (compress_pixels == (unsigned char *) NULL) |
| 1232 | ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed"); |
| 1233 | /* |
| 1234 | Read image pixels. |
| 1235 | */ |
| 1236 | quantum_type=RGBQuantum; |
| 1237 | if (image->matte != MagickFalse) |
| 1238 | quantum_type=RGBAQuantum; |
| 1239 | if (image->colorspace == CMYKColorspace) |
| 1240 | { |
| 1241 | quantum_type=CMYKQuantum; |
| 1242 | if (image->matte != MagickFalse) |
| 1243 | quantum_type=CMYKAQuantum; |
| 1244 | } |
| 1245 | if (image->storage_class == PseudoClass) |
| 1246 | { |
| 1247 | quantum_type=IndexQuantum; |
| 1248 | if (image->matte != MagickFalse) |
| 1249 | quantum_type=IndexAlphaQuantum; |
| 1250 | } |
| 1251 | if (image->colorspace == GRAYColorspace) |
| 1252 | { |
| 1253 | quantum_type=GrayQuantum; |
| 1254 | if (image->matte != MagickFalse) |
| 1255 | quantum_type=GrayAlphaQuantum; |
| 1256 | } |
cristy | 330af6c | 2010-12-21 14:36:06 +0000 | [diff] [blame] | 1257 | status=MagickTrue; |
| 1258 | switch (image->compression) |
| 1259 | { |
| 1260 | #if defined(MAGICKCORE_BZLIB_DELEGATE) |
| 1261 | case BZipCompression: |
| 1262 | { |
| 1263 | (void) ResetMagickMemory(&bzip_info,0,sizeof(bzip_info)); |
| 1264 | bzip_info.bzalloc=AcquireBZIPMemory; |
| 1265 | bzip_info.bzfree=RelinquishBZIPMemory; |
| 1266 | bzip_info.opaque=(void *) NULL; |
| 1267 | code=BZ2_bzDecompressInit(&bzip_info,(int) image_info->verbose, |
| 1268 | MagickFalse); |
| 1269 | if (code != BZ_OK) |
| 1270 | status=MagickFalse; |
cristy | 330af6c | 2010-12-21 14:36:06 +0000 | [diff] [blame] | 1271 | break; |
| 1272 | } |
| 1273 | #endif |
| 1274 | #if defined(MAGICKCORE_LZMA_DELEGATE) |
| 1275 | case LZMACompression: |
| 1276 | { |
cristy | 9d72f1a | 2010-12-21 20:46:59 +0000 | [diff] [blame] | 1277 | (void) ResetMagickMemory(&allocator,0,sizeof(allocator)); |
| 1278 | allocator.alloc=AcquireLZMAMemory; |
| 1279 | allocator.free=RelinquishLZMAMemory; |
cristy | 330af6c | 2010-12-21 14:36:06 +0000 | [diff] [blame] | 1280 | lzma_info=initialize_lzma; |
cristy | 9d72f1a | 2010-12-21 20:46:59 +0000 | [diff] [blame] | 1281 | lzma_info.allocator=(&allocator); |
cristy | 330af6c | 2010-12-21 14:36:06 +0000 | [diff] [blame] | 1282 | code=lzma_auto_decoder(&lzma_info,-1,0); |
| 1283 | if (code != LZMA_OK) |
| 1284 | status=MagickFalse; |
cristy | 330af6c | 2010-12-21 14:36:06 +0000 | [diff] [blame] | 1285 | break; |
| 1286 | } |
| 1287 | #endif |
| 1288 | #if defined(MAGICKCORE_ZLIB_DELEGATE) |
| 1289 | case LZWCompression: |
| 1290 | case ZipCompression: |
| 1291 | { |
| 1292 | (void) ResetMagickMemory(&zip_info,0,sizeof(zip_info)); |
| 1293 | zip_info.zalloc=AcquireZIPMemory; |
| 1294 | zip_info.zfree=RelinquishZIPMemory; |
| 1295 | zip_info.opaque=(voidpf) NULL; |
| 1296 | code=inflateInit(&zip_info); |
| 1297 | if (code != Z_OK) |
| 1298 | status=MagickFalse; |
cristy | 330af6c | 2010-12-21 14:36:06 +0000 | [diff] [blame] | 1299 | break; |
| 1300 | } |
| 1301 | #endif |
| 1302 | case RLECompression: |
| 1303 | { |
| 1304 | (void) ResetMagickMemory(&pixel,0,sizeof(pixel)); |
| 1305 | pixel.opacity=(Quantum) TransparentOpacity; |
| 1306 | index=(IndexPacket) 0; |
| 1307 | break; |
| 1308 | } |
| 1309 | default: |
| 1310 | break; |
| 1311 | } |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1312 | pixels=GetQuantumPixels(quantum_info); |
| 1313 | index=(IndexPacket) 0; |
| 1314 | length=0; |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1315 | for (y=0; y < (ssize_t) image->rows; y++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1316 | { |
| 1317 | register IndexPacket |
cristy | c47d1f8 | 2009-11-26 01:44:43 +0000 | [diff] [blame] | 1318 | *restrict indexes; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1319 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1320 | register ssize_t |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1321 | x; |
| 1322 | |
| 1323 | register PixelPacket |
cristy | c47d1f8 | 2009-11-26 01:44:43 +0000 | [diff] [blame] | 1324 | *restrict q; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1325 | |
cristy | 330af6c | 2010-12-21 14:36:06 +0000 | [diff] [blame] | 1326 | if (status == MagickFalse) |
| 1327 | break; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1328 | q=QueueAuthenticPixels(image,0,y,image->columns,1,exception); |
| 1329 | if (q == (PixelPacket *) NULL) |
| 1330 | break; |
| 1331 | indexes=GetAuthenticIndexQueue(image); |
| 1332 | switch (image->compression) |
| 1333 | { |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1334 | #if defined(MAGICKCORE_BZLIB_DELEGATE) |
| 1335 | case BZipCompression: |
| 1336 | { |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1337 | bzip_info.next_out=(char *) pixels; |
| 1338 | bzip_info.avail_out=(unsigned int) (packet_size*image->columns); |
| 1339 | do |
| 1340 | { |
| 1341 | if (bzip_info.avail_in == 0) |
| 1342 | { |
| 1343 | bzip_info.next_in=(char *) compress_pixels; |
| 1344 | length=(size_t) BZipMaxExtent(packet_size*image->columns); |
| 1345 | if (version != 0) |
| 1346 | length=(size_t) ReadBlobMSBLong(image); |
| 1347 | bzip_info.avail_in=(unsigned int) ReadBlob(image,length, |
| 1348 | (unsigned char *) bzip_info.next_in); |
| 1349 | } |
| 1350 | if (BZ2_bzDecompress(&bzip_info) == BZ_STREAM_END) |
| 1351 | break; |
| 1352 | } while (bzip_info.avail_out != 0); |
cristy | 2637717 | 2010-12-20 19:01:58 +0000 | [diff] [blame] | 1353 | (void) ImportQuantumPixels(image,(CacheView *) NULL,quantum_info, |
| 1354 | quantum_type,pixels,exception); |
| 1355 | break; |
| 1356 | } |
| 1357 | #endif |
| 1358 | #if defined(MAGICKCORE_LZMA_DELEGATE) |
| 1359 | case LZMACompression: |
| 1360 | { |
cristy | 2637717 | 2010-12-20 19:01:58 +0000 | [diff] [blame] | 1361 | lzma_info.next_out=pixels; |
cristy | 330af6c | 2010-12-21 14:36:06 +0000 | [diff] [blame] | 1362 | lzma_info.avail_out=packet_size*image->columns; |
cristy | 2637717 | 2010-12-20 19:01:58 +0000 | [diff] [blame] | 1363 | do |
| 1364 | { |
| 1365 | if (lzma_info.avail_in == 0) |
| 1366 | { |
| 1367 | lzma_info.next_in=compress_pixels; |
| 1368 | length=(size_t) ReadBlobMSBLong(image); |
| 1369 | lzma_info.avail_in=(unsigned int) ReadBlob(image,length, |
| 1370 | (unsigned char *) lzma_info.next_in); |
| 1371 | } |
| 1372 | code=lzma_code(&lzma_info,LZMA_RUN); |
cristy | 330af6c | 2010-12-21 14:36:06 +0000 | [diff] [blame] | 1373 | if (code < 0) |
| 1374 | { |
| 1375 | status=MagickFalse; |
| 1376 | break; |
| 1377 | } |
| 1378 | if (code == LZMA_STREAM_END) |
cristy | 2637717 | 2010-12-20 19:01:58 +0000 | [diff] [blame] | 1379 | break; |
| 1380 | } while (lzma_info.avail_out != 0); |
cristy | 2637717 | 2010-12-20 19:01:58 +0000 | [diff] [blame] | 1381 | (void) ImportQuantumPixels(image,(CacheView *) NULL,quantum_info, |
| 1382 | quantum_type,pixels,exception); |
| 1383 | break; |
| 1384 | } |
| 1385 | #endif |
| 1386 | #if defined(MAGICKCORE_ZLIB_DELEGATE) |
| 1387 | case LZWCompression: |
| 1388 | case ZipCompression: |
| 1389 | { |
cristy | 2637717 | 2010-12-20 19:01:58 +0000 | [diff] [blame] | 1390 | zip_info.next_out=pixels; |
| 1391 | zip_info.avail_out=(uInt) (packet_size*image->columns); |
| 1392 | do |
| 1393 | { |
| 1394 | if (zip_info.avail_in == 0) |
| 1395 | { |
| 1396 | zip_info.next_in=compress_pixels; |
| 1397 | length=(size_t) ZipMaxExtent(packet_size*image->columns); |
| 1398 | if (version != 0) |
| 1399 | length=(size_t) ReadBlobMSBLong(image); |
| 1400 | zip_info.avail_in=(unsigned int) ReadBlob(image,length, |
| 1401 | zip_info.next_in); |
| 1402 | } |
| 1403 | if (inflate(&zip_info,Z_SYNC_FLUSH) == Z_STREAM_END) |
| 1404 | break; |
| 1405 | } while (zip_info.avail_out != 0); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1406 | (void) ImportQuantumPixels(image,(CacheView *) NULL,quantum_info, |
| 1407 | quantum_type,pixels,exception); |
| 1408 | break; |
| 1409 | } |
| 1410 | #endif |
| 1411 | case RLECompression: |
| 1412 | { |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1413 | for (x=0; x < (ssize_t) image->columns; x++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1414 | { |
| 1415 | if (length == 0) |
| 1416 | { |
| 1417 | count=ReadBlob(image,packet_size,pixels); |
| 1418 | PushRunlengthPacket(image,pixels,&length,&pixel,&index); |
| 1419 | } |
| 1420 | length--; |
| 1421 | if ((image->storage_class == PseudoClass) || |
| 1422 | (image->colorspace == CMYKColorspace)) |
| 1423 | indexes[x]=index; |
| 1424 | *q++=pixel; |
| 1425 | } |
| 1426 | break; |
| 1427 | } |
| 1428 | default: |
| 1429 | { |
| 1430 | count=ReadBlob(image,packet_size*image->columns,pixels); |
| 1431 | (void) ImportQuantumPixels(image,(CacheView *) NULL,quantum_info, |
| 1432 | quantum_type,pixels,exception); |
| 1433 | break; |
| 1434 | } |
| 1435 | } |
| 1436 | if (SyncAuthenticPixels(image,exception) == MagickFalse) |
| 1437 | break; |
| 1438 | } |
| 1439 | SetQuantumImageType(image,quantum_type); |
cristy | 330af6c | 2010-12-21 14:36:06 +0000 | [diff] [blame] | 1440 | switch (image->compression) |
| 1441 | { |
| 1442 | #if defined(MAGICKCORE_BZLIB_DELEGATE) |
| 1443 | case BZipCompression: |
| 1444 | { |
| 1445 | if (version == 0) |
| 1446 | { |
| 1447 | MagickOffsetType |
| 1448 | offset; |
| 1449 | |
| 1450 | offset=SeekBlob(image,-((MagickOffsetType) |
| 1451 | bzip_info.avail_in),SEEK_CUR); |
| 1452 | if (offset < 0) |
| 1453 | ThrowReaderException(CorruptImageError, |
| 1454 | "ImproperImageHeader"); |
| 1455 | } |
| 1456 | code=BZ2_bzDecompressEnd(&bzip_info); |
| 1457 | if (code != BZ_OK) |
| 1458 | status=MagickFalse; |
| 1459 | break; |
| 1460 | } |
| 1461 | #endif |
| 1462 | #if defined(MAGICKCORE_LZMA_DELEGATE) |
| 1463 | case LZMACompression: |
| 1464 | { |
cristy | 3b788a0 | 2010-12-27 15:59:54 +0000 | [diff] [blame] | 1465 | code=lzma_code(&lzma_info,LZMA_FINISH); |
| 1466 | if ((code != LZMA_STREAM_END) && (code != LZMA_OK)) |
cristy | b977da5 | 2010-12-22 15:55:53 +0000 | [diff] [blame] | 1467 | status=MagickFalse; |
cristy | 330af6c | 2010-12-21 14:36:06 +0000 | [diff] [blame] | 1468 | lzma_end(&lzma_info); |
| 1469 | break; |
| 1470 | } |
| 1471 | #endif |
| 1472 | #if defined(MAGICKCORE_ZLIB_DELEGATE) |
| 1473 | case LZWCompression: |
| 1474 | case ZipCompression: |
| 1475 | { |
| 1476 | if (version == 0) |
| 1477 | { |
| 1478 | MagickOffsetType |
| 1479 | offset; |
| 1480 | |
| 1481 | offset=SeekBlob(image,-((MagickOffsetType) zip_info.avail_in), |
| 1482 | SEEK_CUR); |
| 1483 | if (offset < 0) |
| 1484 | ThrowReaderException(CorruptImageError, |
| 1485 | "ImproperImageHeader"); |
| 1486 | } |
| 1487 | code=inflateEnd(&zip_info); |
| 1488 | if (code != LZMA_OK) |
| 1489 | status=MagickFalse; |
| 1490 | break; |
| 1491 | } |
| 1492 | #endif |
| 1493 | default: |
| 1494 | break; |
| 1495 | } |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1496 | quantum_info=DestroyQuantumInfo(quantum_info); |
| 1497 | compress_pixels=(unsigned char *) RelinquishMagickMemory(compress_pixels); |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1498 | if (((y != (ssize_t) image->rows)) || (status == MagickFalse)) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1499 | { |
| 1500 | image=DestroyImageList(image); |
| 1501 | return((Image *) NULL); |
| 1502 | } |
| 1503 | if (EOFBlob(image) != MagickFalse) |
| 1504 | { |
| 1505 | ThrowFileException(exception,CorruptImageError,"UnexpectedEndOfFile", |
| 1506 | image->filename); |
| 1507 | break; |
| 1508 | } |
| 1509 | /* |
| 1510 | Proceed to next image. |
| 1511 | */ |
| 1512 | if (image_info->number_scenes != 0) |
| 1513 | if (image->scene >= (image_info->scene+image_info->number_scenes-1)) |
| 1514 | break; |
| 1515 | do |
| 1516 | { |
| 1517 | c=ReadBlobByte(image); |
| 1518 | } while ((isgraph(c) == MagickFalse) && (c != EOF)); |
| 1519 | if (c != EOF) |
| 1520 | { |
| 1521 | /* |
| 1522 | Allocate next image structure. |
| 1523 | */ |
| 1524 | AcquireNextImage(image_info,image); |
| 1525 | if (GetNextImageInList(image) == (Image *) NULL) |
| 1526 | { |
| 1527 | image=DestroyImageList(image); |
| 1528 | return((Image *) NULL); |
| 1529 | } |
| 1530 | image=SyncNextImageInList(image); |
| 1531 | status=SetImageProgress(image,LoadImagesTag,TellBlob(image), |
| 1532 | GetBlobSize(image)); |
| 1533 | if (status == MagickFalse) |
| 1534 | break; |
| 1535 | } |
| 1536 | } while (c != EOF); |
| 1537 | (void) CloseBlob(image); |
| 1538 | return(GetFirstImageInList(image)); |
| 1539 | } |
| 1540 | |
| 1541 | /* |
| 1542 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 1543 | % % |
| 1544 | % % |
| 1545 | % % |
| 1546 | % R e g i s t e r M I F F I m a g e % |
| 1547 | % % |
| 1548 | % % |
| 1549 | % % |
| 1550 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 1551 | % |
| 1552 | % RegisterMIFFImage() adds properties for the MIFF image format to the list of |
| 1553 | % supported formats. The properties include the image format tag, a method to |
| 1554 | % read and/or write the format, whether the format supports the saving of more |
| 1555 | % than one frame to the same file or blob, whether the format supports native |
| 1556 | % in-memory I/O, and a brief description of the format. |
| 1557 | % |
| 1558 | % The format of the RegisterMIFFImage method is: |
| 1559 | % |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1560 | % size_t RegisterMIFFImage(void) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1561 | % |
| 1562 | */ |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1563 | ModuleExport size_t RegisterMIFFImage(void) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1564 | { |
| 1565 | char |
| 1566 | version[MaxTextExtent]; |
| 1567 | |
| 1568 | MagickInfo |
| 1569 | *entry; |
| 1570 | |
| 1571 | *version='\0'; |
| 1572 | #if defined(MagickImageCoderSignatureText) |
| 1573 | (void) CopyMagickString(version,MagickLibVersionText,MaxTextExtent); |
| 1574 | #if defined(ZLIB_VERSION) |
| 1575 | (void) ConcatenateMagickString(version," with Zlib ",MaxTextExtent); |
| 1576 | (void) ConcatenateMagickString(version,ZLIB_VERSION,MaxTextExtent); |
| 1577 | #endif |
| 1578 | #if defined(MAGICKCORE_BZLIB_DELEGATE) |
| 1579 | (void) ConcatenateMagickString(version," and BZlib",MaxTextExtent); |
| 1580 | #endif |
| 1581 | #endif |
| 1582 | entry=SetMagickInfo("MIFF"); |
| 1583 | entry->decoder=(DecodeImageHandler *) ReadMIFFImage; |
| 1584 | entry->encoder=(EncodeImageHandler *) WriteMIFFImage; |
| 1585 | entry->magick=(IsImageFormatHandler *) IsMIFF; |
cristy | ffaf978 | 2011-04-13 19:50:51 +0000 | [diff] [blame] | 1586 | entry->seekable_stream=MagickTrue; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1587 | entry->description=ConstantString("Magick Image File Format"); |
| 1588 | if (*version != '\0') |
| 1589 | entry->version=ConstantString(version); |
| 1590 | entry->module=ConstantString("MIFF"); |
| 1591 | (void) RegisterMagickInfo(entry); |
| 1592 | return(MagickImageCoderSignature); |
| 1593 | } |
| 1594 | |
| 1595 | /* |
| 1596 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 1597 | % % |
| 1598 | % % |
| 1599 | % % |
| 1600 | % U n r e g i s t e r M I F F I m a g e % |
| 1601 | % % |
| 1602 | % % |
| 1603 | % % |
| 1604 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 1605 | % |
| 1606 | % UnregisterMIFFImage() removes format registrations made by the MIFF module |
| 1607 | % from the list of supported formats. |
| 1608 | % |
| 1609 | % The format of the UnregisterMIFFImage method is: |
| 1610 | % |
| 1611 | % UnregisterMIFFImage(void) |
| 1612 | % |
| 1613 | */ |
| 1614 | ModuleExport void UnregisterMIFFImage(void) |
| 1615 | { |
| 1616 | (void) UnregisterMagickInfo("MIFF"); |
| 1617 | } |
| 1618 | |
| 1619 | /* |
| 1620 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 1621 | % % |
| 1622 | % % |
| 1623 | % % |
| 1624 | % W r i t e M I F F I m a g e % |
| 1625 | % % |
| 1626 | % % |
| 1627 | % % |
| 1628 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 1629 | % |
| 1630 | % WriteMIFFImage() writes a MIFF image to a file. |
| 1631 | % |
| 1632 | % The format of the WriteMIFFImage method is: |
| 1633 | % |
| 1634 | % MagickBooleanType WriteMIFFImage(const ImageInfo *image_info, |
| 1635 | % Image *image) |
| 1636 | % |
| 1637 | % Compression code contributed by Kyle Shorter. |
| 1638 | % |
| 1639 | % A description of each parameter follows: |
| 1640 | % |
| 1641 | % o image_info: the image info. |
| 1642 | % |
| 1643 | % o image: the image. |
| 1644 | % |
| 1645 | */ |
| 1646 | |
| 1647 | static unsigned char *PopRunlengthPacket(Image *image,unsigned char *pixels, |
| 1648 | size_t length,PixelPacket pixel,IndexPacket index) |
| 1649 | { |
| 1650 | if (image->storage_class != DirectClass) |
| 1651 | { |
| 1652 | switch (image->depth) |
| 1653 | { |
| 1654 | case 32: |
| 1655 | { |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1656 | *pixels++=(unsigned char) ((size_t) index >> 24); |
| 1657 | *pixels++=(unsigned char) ((size_t) index >> 16); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1658 | } |
| 1659 | case 16: |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1660 | *pixels++=(unsigned char) ((size_t) index >> 8); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1661 | case 8: |
| 1662 | { |
| 1663 | *pixels++=(unsigned char) index; |
| 1664 | break; |
| 1665 | } |
| 1666 | default: |
| 1667 | (void) ThrowMagickException(&image->exception,GetMagickModule(), |
| 1668 | CorruptImageError,"ImageDepthNotSupported","`%s'",image->filename); |
| 1669 | } |
| 1670 | switch (image->depth) |
| 1671 | { |
| 1672 | case 32: |
| 1673 | { |
cristy | 4cb162a | 2010-05-30 03:04:47 +0000 | [diff] [blame] | 1674 | unsigned int |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1675 | value; |
| 1676 | |
| 1677 | if (image->matte != MagickFalse) |
| 1678 | { |
| 1679 | value=ScaleQuantumToLong(pixel.opacity); |
| 1680 | pixels=PopLongPixel(MSBEndian,value,pixels); |
| 1681 | } |
| 1682 | break; |
| 1683 | } |
| 1684 | case 16: |
| 1685 | { |
| 1686 | unsigned short |
| 1687 | value; |
| 1688 | |
| 1689 | if (image->matte != MagickFalse) |
| 1690 | { |
| 1691 | value=ScaleQuantumToShort(pixel.opacity); |
| 1692 | pixels=PopShortPixel(MSBEndian,value,pixels); |
| 1693 | } |
| 1694 | break; |
| 1695 | } |
| 1696 | case 8: |
| 1697 | { |
| 1698 | unsigned char |
| 1699 | value; |
| 1700 | |
| 1701 | if (image->matte != MagickFalse) |
| 1702 | { |
| 1703 | value=(unsigned char) ScaleQuantumToChar(pixel.opacity); |
| 1704 | pixels=PopCharPixel(value,pixels); |
| 1705 | } |
| 1706 | break; |
| 1707 | } |
| 1708 | default: |
| 1709 | (void) ThrowMagickException(&image->exception,GetMagickModule(), |
| 1710 | CorruptImageError,"ImageDepthNotSupported","`%s'",image->filename); |
| 1711 | } |
| 1712 | *pixels++=(unsigned char) length; |
| 1713 | return(pixels); |
| 1714 | } |
| 1715 | switch (image->depth) |
| 1716 | { |
| 1717 | case 32: |
| 1718 | { |
cristy | 4cb162a | 2010-05-30 03:04:47 +0000 | [diff] [blame] | 1719 | unsigned int |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1720 | value; |
| 1721 | |
| 1722 | value=ScaleQuantumToLong(pixel.red); |
| 1723 | pixels=PopLongPixel(MSBEndian,value,pixels); |
| 1724 | value=ScaleQuantumToLong(pixel.green); |
| 1725 | pixels=PopLongPixel(MSBEndian,value,pixels); |
| 1726 | value=ScaleQuantumToLong(pixel.blue); |
| 1727 | pixels=PopLongPixel(MSBEndian,value,pixels); |
| 1728 | if (image->matte != MagickFalse) |
| 1729 | { |
| 1730 | value=ScaleQuantumToLong(pixel.opacity); |
| 1731 | pixels=PopLongPixel(MSBEndian,value,pixels); |
| 1732 | } |
| 1733 | if (image->colorspace == CMYKColorspace) |
| 1734 | { |
| 1735 | value=ScaleQuantumToLong(index); |
| 1736 | pixels=PopLongPixel(MSBEndian,value,pixels); |
| 1737 | } |
| 1738 | break; |
| 1739 | } |
| 1740 | case 16: |
| 1741 | { |
| 1742 | unsigned short |
| 1743 | value; |
| 1744 | |
| 1745 | value=ScaleQuantumToShort(pixel.red); |
| 1746 | pixels=PopShortPixel(MSBEndian,value,pixels); |
| 1747 | value=ScaleQuantumToShort(pixel.green); |
| 1748 | pixels=PopShortPixel(MSBEndian,value,pixels); |
| 1749 | value=ScaleQuantumToShort(pixel.blue); |
| 1750 | pixels=PopShortPixel(MSBEndian,value,pixels); |
| 1751 | if (image->matte != MagickFalse) |
| 1752 | { |
| 1753 | value=ScaleQuantumToShort(pixel.opacity); |
| 1754 | pixels=PopShortPixel(MSBEndian,value,pixels); |
| 1755 | } |
| 1756 | if (image->colorspace == CMYKColorspace) |
| 1757 | { |
| 1758 | value=ScaleQuantumToShort(index); |
| 1759 | pixels=PopShortPixel(MSBEndian,value,pixels); |
| 1760 | } |
| 1761 | break; |
| 1762 | } |
| 1763 | case 8: |
| 1764 | { |
| 1765 | unsigned char |
| 1766 | value; |
| 1767 | |
| 1768 | value=(unsigned char) ScaleQuantumToChar(pixel.red); |
| 1769 | pixels=PopCharPixel(value,pixels); |
| 1770 | value=(unsigned char) ScaleQuantumToChar(pixel.green); |
| 1771 | pixels=PopCharPixel(value,pixels); |
| 1772 | value=(unsigned char) ScaleQuantumToChar(pixel.blue); |
| 1773 | pixels=PopCharPixel(value,pixels); |
| 1774 | if (image->matte != MagickFalse) |
| 1775 | { |
| 1776 | value=(unsigned char) ScaleQuantumToChar(pixel.opacity); |
| 1777 | pixels=PopCharPixel(value,pixels); |
| 1778 | } |
| 1779 | if (image->colorspace == CMYKColorspace) |
| 1780 | { |
| 1781 | value=(unsigned char) ScaleQuantumToChar(index); |
| 1782 | pixels=PopCharPixel(value,pixels); |
| 1783 | } |
| 1784 | break; |
| 1785 | } |
| 1786 | default: |
| 1787 | (void) ThrowMagickException(&image->exception,GetMagickModule(), |
| 1788 | CorruptImageError,"ImageDepthNotSupported","`%s'",image->filename); |
| 1789 | } |
| 1790 | *pixels++=(unsigned char) length; |
| 1791 | return(pixels); |
| 1792 | } |
| 1793 | |
| 1794 | static MagickBooleanType WriteMIFFImage(const ImageInfo *image_info, |
| 1795 | Image *image) |
| 1796 | { |
| 1797 | #if defined(MAGICKCORE_BZLIB_DELEGATE) |
| 1798 | bz_stream |
| 1799 | bzip_info; |
| 1800 | #endif |
| 1801 | |
| 1802 | char |
| 1803 | buffer[MaxTextExtent]; |
| 1804 | |
| 1805 | CompressionType |
| 1806 | compression; |
| 1807 | |
| 1808 | const char |
| 1809 | *property, |
| 1810 | *value; |
| 1811 | |
| 1812 | IndexPacket |
| 1813 | index; |
| 1814 | |
| 1815 | int |
| 1816 | code; |
| 1817 | |
cristy | 2637717 | 2010-12-20 19:01:58 +0000 | [diff] [blame] | 1818 | #if defined(MAGICKCORE_LZMA_DELEGATE) |
cristy | 4b46dba | 2010-12-20 19:18:20 +0000 | [diff] [blame] | 1819 | lzma_allocator |
| 1820 | allocator; |
| 1821 | |
cristy | 2637717 | 2010-12-20 19:01:58 +0000 | [diff] [blame] | 1822 | lzma_stream |
cristy | 330af6c | 2010-12-21 14:36:06 +0000 | [diff] [blame] | 1823 | initialize_lzma = LZMA_STREAM_INIT, |
| 1824 | lzma_info; |
cristy | 2637717 | 2010-12-20 19:01:58 +0000 | [diff] [blame] | 1825 | #endif |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1826 | |
| 1827 | MagickBooleanType |
| 1828 | status; |
| 1829 | |
| 1830 | MagickOffsetType |
| 1831 | scene; |
| 1832 | |
| 1833 | PixelPacket |
| 1834 | pixel; |
| 1835 | |
| 1836 | QuantumInfo |
| 1837 | *quantum_info; |
| 1838 | |
| 1839 | QuantumType |
| 1840 | quantum_type; |
| 1841 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1842 | register ssize_t |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1843 | i; |
| 1844 | |
| 1845 | size_t |
| 1846 | length, |
| 1847 | packet_size; |
| 1848 | |
cristy | 2637717 | 2010-12-20 19:01:58 +0000 | [diff] [blame] | 1849 | ssize_t |
| 1850 | y; |
| 1851 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1852 | unsigned char |
| 1853 | *compress_pixels, |
| 1854 | *pixels, |
| 1855 | *q; |
| 1856 | |
| 1857 | #if defined(MAGICKCORE_ZLIB_DELEGATE) |
| 1858 | z_stream |
| 1859 | zip_info; |
| 1860 | #endif |
| 1861 | |
| 1862 | /* |
| 1863 | Open output image file. |
| 1864 | */ |
| 1865 | assert(image_info != (const ImageInfo *) NULL); |
| 1866 | assert(image_info->signature == MagickSignature); |
| 1867 | assert(image != (Image *) NULL); |
| 1868 | assert(image->signature == MagickSignature); |
| 1869 | if (image->debug != MagickFalse) |
| 1870 | (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename); |
| 1871 | status=OpenBlob(image_info,image,WriteBinaryBlobMode,&image->exception); |
| 1872 | if (status == MagickFalse) |
| 1873 | return(status); |
| 1874 | code=0; |
| 1875 | scene=0; |
| 1876 | do |
| 1877 | { |
| 1878 | /* |
| 1879 | Allocate image pixels. |
| 1880 | */ |
| 1881 | image->depth=image->depth <= 8 ? 8UL : image->depth <= 16 ? 16UL : |
| 1882 | image->depth <= 32 ? 32UL : 64UL; |
| 1883 | quantum_info=AcquireQuantumInfo(image_info,image); |
| 1884 | if (quantum_info == (QuantumInfo *) NULL) |
| 1885 | ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed"); |
| 1886 | if ((image->storage_class != PseudoClass) && (image->depth >= 32) && |
| 1887 | (quantum_info->format == UndefinedQuantumFormat) && |
| 1888 | (IsHighDynamicRangeImage(image,&image->exception) != MagickFalse)) |
| 1889 | { |
| 1890 | status=SetQuantumFormat(image,quantum_info,FloatingPointQuantumFormat); |
| 1891 | if (status == MagickFalse) |
| 1892 | ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed"); |
| 1893 | } |
| 1894 | if ((image->storage_class == PseudoClass) && |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1895 | (image->colors > (size_t) (GetQuantumRange(image->depth)+1))) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1896 | (void) SetImageStorageClass(image,DirectClass); |
| 1897 | if (IsGrayImage(image,&image->exception) != MagickFalse) |
| 1898 | { |
| 1899 | image->storage_class=DirectClass; |
| 1900 | (void) SetImageColorspace(image,GRAYColorspace); |
| 1901 | } |
| 1902 | compression=image->compression; |
| 1903 | if (image_info->compression != UndefinedCompression) |
| 1904 | compression=image_info->compression; |
| 1905 | switch (compression) |
| 1906 | { |
| 1907 | #if !defined(MAGICKCORE_ZLIB_DELEGATE) |
cristy | 2637717 | 2010-12-20 19:01:58 +0000 | [diff] [blame] | 1908 | case LZMACompression: compression=NoCompression; break; |
| 1909 | #endif |
| 1910 | #if !defined(MAGICKCORE_ZLIB_DELEGATE) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1911 | case LZWCompression: |
| 1912 | case ZipCompression: compression=NoCompression; break; |
| 1913 | #endif |
| 1914 | #if !defined(MAGICKCORE_BZLIB_DELEGATE) |
| 1915 | case BZipCompression: compression=NoCompression; break; |
| 1916 | #endif |
| 1917 | case RLECompression: |
| 1918 | { |
| 1919 | if (quantum_info->format == FloatingPointQuantumFormat) |
| 1920 | compression=NoCompression; |
| 1921 | break; |
| 1922 | } |
| 1923 | default: |
| 1924 | break; |
| 1925 | } |
| 1926 | packet_size=(size_t) (quantum_info->depth/8); |
| 1927 | if (image->storage_class == DirectClass) |
| 1928 | packet_size=(size_t) (3*quantum_info->depth/8); |
| 1929 | if (image->matte != MagickFalse) |
| 1930 | packet_size+=quantum_info->depth/8; |
| 1931 | if (image->colorspace == CMYKColorspace) |
| 1932 | packet_size+=quantum_info->depth/8; |
| 1933 | if (compression == RLECompression) |
| 1934 | packet_size++; |
| 1935 | length=image->columns; |
| 1936 | length=MagickMax(BZipMaxExtent(packet_size*image->columns),ZipMaxExtent( |
| 1937 | packet_size*image->columns)); |
| 1938 | if ((compression == BZipCompression) || (compression == ZipCompression)) |
| 1939 | if (length != (size_t) ((unsigned int) length)) |
| 1940 | compression=NoCompression; |
| 1941 | compress_pixels=(unsigned char *) AcquireQuantumMemory(length, |
| 1942 | sizeof(*compress_pixels)); |
| 1943 | if (compress_pixels == (unsigned char *) NULL) |
| 1944 | ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed"); |
| 1945 | /* |
| 1946 | Write MIFF header. |
| 1947 | */ |
| 1948 | (void) WriteBlobString(image,"id=ImageMagick version=1.0\n"); |
| 1949 | (void) FormatMagickString(buffer,MaxTextExtent, |
cristy | 042ee78 | 2011-04-22 18:48:30 +0000 | [diff] [blame] | 1950 | "class=%s colors=%.20g matte=%s\n",CommandOptionToMnemonic( |
cristy | e8c25f9 | 2010-06-03 00:53:06 +0000 | [diff] [blame] | 1951 | MagickClassOptions,image->storage_class),(double) image->colors, |
cristy | 042ee78 | 2011-04-22 18:48:30 +0000 | [diff] [blame] | 1952 | CommandOptionToMnemonic(MagickBooleanOptions,(ssize_t) image->matte)); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1953 | (void) WriteBlobString(image,buffer); |
cristy | e8c25f9 | 2010-06-03 00:53:06 +0000 | [diff] [blame] | 1954 | (void) FormatMagickString(buffer,MaxTextExtent,"columns=%.20g rows=%.20g " |
| 1955 | "depth=%.20g\n",(double) image->columns,(double) image->rows,(double) |
| 1956 | image->depth); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1957 | (void) WriteBlobString(image,buffer); |
cristy | 5f1c1ff | 2010-12-23 21:38:06 +0000 | [diff] [blame] | 1958 | if (image->type != UndefinedType) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1959 | { |
| 1960 | (void) FormatMagickString(buffer,MaxTextExtent,"type=%s\n", |
cristy | 042ee78 | 2011-04-22 18:48:30 +0000 | [diff] [blame] | 1961 | CommandOptionToMnemonic(MagickTypeOptions,image->type)); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1962 | (void) WriteBlobString(image,buffer); |
| 1963 | } |
| 1964 | if (image->colorspace != UndefinedColorspace) |
| 1965 | { |
| 1966 | (void) FormatMagickString(buffer,MaxTextExtent,"colorspace=%s\n", |
cristy | 042ee78 | 2011-04-22 18:48:30 +0000 | [diff] [blame] | 1967 | CommandOptionToMnemonic(MagickColorspaceOptions,image->colorspace)); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1968 | (void) WriteBlobString(image,buffer); |
| 1969 | } |
| 1970 | if (compression != UndefinedCompression) |
| 1971 | { |
| 1972 | (void) FormatMagickString(buffer,MaxTextExtent,"compression=%s " |
cristy | 042ee78 | 2011-04-22 18:48:30 +0000 | [diff] [blame] | 1973 | "quality=%.20g\n",CommandOptionToMnemonic(MagickCompressOptions, |
cristy | e8c25f9 | 2010-06-03 00:53:06 +0000 | [diff] [blame] | 1974 | compression),(double) image->quality); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1975 | (void) WriteBlobString(image,buffer); |
| 1976 | } |
| 1977 | if (image->units != UndefinedResolution) |
| 1978 | { |
| 1979 | (void) FormatMagickString(buffer,MaxTextExtent,"units=%s\n", |
cristy | 042ee78 | 2011-04-22 18:48:30 +0000 | [diff] [blame] | 1980 | CommandOptionToMnemonic(MagickResolutionOptions,image->units)); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1981 | (void) WriteBlobString(image,buffer); |
| 1982 | } |
| 1983 | if ((image->x_resolution != 0) || (image->y_resolution != 0)) |
| 1984 | { |
cristy | 8cd5b31 | 2010-01-07 01:10:24 +0000 | [diff] [blame] | 1985 | (void) FormatMagickString(buffer,MaxTextExtent, |
cristy | e7f5109 | 2010-01-17 00:39:37 +0000 | [diff] [blame] | 1986 | "resolution=%gx%g\n",image->x_resolution,image->y_resolution); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1987 | (void) WriteBlobString(image,buffer); |
| 1988 | } |
| 1989 | if ((image->page.width != 0) || (image->page.height != 0)) |
| 1990 | { |
cristy | e8c25f9 | 2010-06-03 00:53:06 +0000 | [diff] [blame] | 1991 | (void) FormatMagickString(buffer,MaxTextExtent, |
| 1992 | "page=%.20gx%.20g%+.20g%+.20g\n",(double) image->page.width,(double) |
| 1993 | image->page.height,(double) image->page.x,(double) image->page.y); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1994 | (void) WriteBlobString(image,buffer); |
| 1995 | } |
| 1996 | else |
| 1997 | if ((image->page.x != 0) || (image->page.y != 0)) |
| 1998 | { |
| 1999 | (void) FormatMagickString(buffer,MaxTextExtent,"page=%+ld%+ld\n", |
cristy | f2faecf | 2010-05-28 19:19:36 +0000 | [diff] [blame] | 2000 | (long) image->page.x,(long) image->page.y); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2001 | (void) WriteBlobString(image,buffer); |
| 2002 | } |
| 2003 | if ((image->tile_offset.x != 0) || (image->tile_offset.y != 0)) |
| 2004 | { |
| 2005 | (void) FormatMagickString(buffer,MaxTextExtent,"tile-offset=%+ld%+ld\n", |
cristy | f2faecf | 2010-05-28 19:19:36 +0000 | [diff] [blame] | 2006 | (long) image->tile_offset.x,(long) image->tile_offset.y); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2007 | (void) WriteBlobString(image,buffer); |
| 2008 | } |
| 2009 | if ((GetNextImageInList(image) != (Image *) NULL) || |
| 2010 | (GetPreviousImageInList(image) != (Image *) NULL)) |
| 2011 | { |
| 2012 | if (image->scene == 0) |
cristy | e8c25f9 | 2010-06-03 00:53:06 +0000 | [diff] [blame] | 2013 | (void) FormatMagickString(buffer,MaxTextExtent,"iterations=%.20g " |
| 2014 | "delay=%.20g ticks-per-second=%.20g\n",(double) image->iterations, |
| 2015 | (double) image->delay,(double) image->ticks_per_second); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2016 | else |
cristy | e8c25f9 | 2010-06-03 00:53:06 +0000 | [diff] [blame] | 2017 | (void) FormatMagickString(buffer,MaxTextExtent,"scene=%.20g " |
| 2018 | "iterations=%.20g delay=%.20g ticks-per-second=%.20g\n",(double) |
| 2019 | image->scene,(double) image->iterations,(double) image->delay, |
| 2020 | (double) image->ticks_per_second); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2021 | (void) WriteBlobString(image,buffer); |
| 2022 | } |
| 2023 | else |
| 2024 | { |
| 2025 | if (image->scene != 0) |
| 2026 | { |
cristy | e8c25f9 | 2010-06-03 00:53:06 +0000 | [diff] [blame] | 2027 | (void) FormatMagickString(buffer,MaxTextExtent,"scene=%.20g\n", |
| 2028 | (double) image->scene); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2029 | (void) WriteBlobString(image,buffer); |
| 2030 | } |
| 2031 | if (image->iterations != 0) |
| 2032 | { |
cristy | e8c25f9 | 2010-06-03 00:53:06 +0000 | [diff] [blame] | 2033 | (void) FormatMagickString(buffer,MaxTextExtent,"iterations=%.20g\n", |
| 2034 | (double) image->iterations); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2035 | (void) WriteBlobString(image,buffer); |
| 2036 | } |
| 2037 | if (image->delay != 0) |
| 2038 | { |
cristy | e8c25f9 | 2010-06-03 00:53:06 +0000 | [diff] [blame] | 2039 | (void) FormatMagickString(buffer,MaxTextExtent,"delay=%.20g\n", |
| 2040 | (double) image->delay); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2041 | (void) WriteBlobString(image,buffer); |
| 2042 | } |
| 2043 | if (image->ticks_per_second != UndefinedTicksPerSecond) |
| 2044 | { |
| 2045 | (void) FormatMagickString(buffer,MaxTextExtent, |
cristy | e8c25f9 | 2010-06-03 00:53:06 +0000 | [diff] [blame] | 2046 | "ticks-per-second=%.20g\n",(double) image->ticks_per_second); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2047 | (void) WriteBlobString(image,buffer); |
| 2048 | } |
| 2049 | } |
| 2050 | if (image->gravity != UndefinedGravity) |
| 2051 | { |
| 2052 | (void) FormatMagickString(buffer,MaxTextExtent,"gravity=%s\n", |
cristy | 042ee78 | 2011-04-22 18:48:30 +0000 | [diff] [blame] | 2053 | CommandOptionToMnemonic(MagickGravityOptions,image->gravity)); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2054 | (void) WriteBlobString(image,buffer); |
| 2055 | } |
| 2056 | if (image->dispose != UndefinedDispose) |
| 2057 | { |
| 2058 | (void) FormatMagickString(buffer,MaxTextExtent,"dispose=%s\n", |
cristy | 042ee78 | 2011-04-22 18:48:30 +0000 | [diff] [blame] | 2059 | CommandOptionToMnemonic(MagickDisposeOptions,image->dispose)); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2060 | (void) WriteBlobString(image,buffer); |
| 2061 | } |
| 2062 | if (image->rendering_intent != UndefinedIntent) |
| 2063 | { |
| 2064 | (void) FormatMagickString(buffer,MaxTextExtent, |
| 2065 | "rendering-intent=%s\n", |
cristy | 042ee78 | 2011-04-22 18:48:30 +0000 | [diff] [blame] | 2066 | CommandOptionToMnemonic(MagickIntentOptions,image->rendering_intent)); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2067 | (void) WriteBlobString(image,buffer); |
| 2068 | } |
| 2069 | if (image->gamma != 0.0) |
| 2070 | { |
cristy | e7f5109 | 2010-01-17 00:39:37 +0000 | [diff] [blame] | 2071 | (void) FormatMagickString(buffer,MaxTextExtent,"gamma=%g\n", |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2072 | image->gamma); |
| 2073 | (void) WriteBlobString(image,buffer); |
| 2074 | } |
| 2075 | if (image->chromaticity.white_point.x != 0.0) |
| 2076 | { |
| 2077 | /* |
| 2078 | Note chomaticity points. |
| 2079 | */ |
cristy | e7f5109 | 2010-01-17 00:39:37 +0000 | [diff] [blame] | 2080 | (void) FormatMagickString(buffer,MaxTextExtent,"red-primary=%g," |
| 2081 | "%g green-primary=%g,%g blue-primary=%g,%g\n", |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2082 | image->chromaticity.red_primary.x,image->chromaticity.red_primary.y, |
| 2083 | image->chromaticity.green_primary.x, |
| 2084 | image->chromaticity.green_primary.y, |
| 2085 | image->chromaticity.blue_primary.x, |
| 2086 | image->chromaticity.blue_primary.y); |
| 2087 | (void) WriteBlobString(image,buffer); |
cristy | 8cd5b31 | 2010-01-07 01:10:24 +0000 | [diff] [blame] | 2088 | (void) FormatMagickString(buffer,MaxTextExtent, |
cristy | e7f5109 | 2010-01-17 00:39:37 +0000 | [diff] [blame] | 2089 | "white-point=%g,%g\n",image->chromaticity.white_point.x, |
cristy | 8cd5b31 | 2010-01-07 01:10:24 +0000 | [diff] [blame] | 2090 | image->chromaticity.white_point.y); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2091 | (void) WriteBlobString(image,buffer); |
| 2092 | } |
| 2093 | if (image->orientation != UndefinedOrientation) |
| 2094 | { |
| 2095 | (void) FormatMagickString(buffer,MaxTextExtent,"orientation=%s\n", |
cristy | 042ee78 | 2011-04-22 18:48:30 +0000 | [diff] [blame] | 2096 | CommandOptionToMnemonic(MagickOrientationOptions,image->orientation)); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2097 | (void) WriteBlobString(image,buffer); |
| 2098 | } |
| 2099 | if (image->profiles != (void *) NULL) |
| 2100 | { |
| 2101 | const char |
| 2102 | *name; |
| 2103 | |
| 2104 | const StringInfo |
| 2105 | *profile; |
| 2106 | |
| 2107 | /* |
| 2108 | Write image profiles. |
| 2109 | */ |
| 2110 | ResetImageProfileIterator(image); |
| 2111 | name=GetNextImageProfile(image); |
| 2112 | while (name != (const char *) NULL) |
| 2113 | { |
| 2114 | profile=GetImageProfile(image,name); |
| 2115 | if (profile != (StringInfo *) NULL) |
| 2116 | { |
cristy | e8c25f9 | 2010-06-03 00:53:06 +0000 | [diff] [blame] | 2117 | (void) FormatMagickString(buffer,MaxTextExtent, |
| 2118 | "profile:%s=%.20g\n",name,(double) |
| 2119 | GetStringInfoLength(profile)); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2120 | (void) WriteBlobString(image,buffer); |
| 2121 | } |
| 2122 | name=GetNextImageProfile(image); |
| 2123 | } |
| 2124 | } |
| 2125 | if (image->montage != (char *) NULL) |
| 2126 | { |
| 2127 | (void) FormatMagickString(buffer,MaxTextExtent,"montage=%s\n", |
| 2128 | image->montage); |
| 2129 | (void) WriteBlobString(image,buffer); |
| 2130 | } |
| 2131 | if (quantum_info->format == FloatingPointQuantumFormat) |
cristy | 0c2fcca | 2009-10-16 01:37:20 +0000 | [diff] [blame] | 2132 | (void) SetImageProperty(image,"quantum:format","floating-point"); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2133 | ResetImagePropertyIterator(image); |
| 2134 | property=GetNextImageProperty(image); |
| 2135 | while (property != (const char *) NULL) |
| 2136 | { |
| 2137 | (void) FormatMagickString(buffer,MaxTextExtent,"%s=",property); |
| 2138 | (void) WriteBlobString(image,buffer); |
| 2139 | value=GetImageProperty(image,property); |
| 2140 | if (value != (const char *) NULL) |
| 2141 | { |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 2142 | for (i=0; i < (ssize_t) strlen(value); i++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2143 | if (isspace((int) ((unsigned char) value[i])) != 0) |
| 2144 | break; |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 2145 | if (i <= (ssize_t) strlen(value)) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2146 | (void) WriteBlobByte(image,'{'); |
| 2147 | (void) WriteBlob(image,strlen(value),(const unsigned char *) value); |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 2148 | if (i <= (ssize_t) strlen(value)) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2149 | (void) WriteBlobByte(image,'}'); |
| 2150 | } |
| 2151 | (void) WriteBlobByte(image,'\n'); |
| 2152 | property=GetNextImageProperty(image); |
| 2153 | } |
| 2154 | (void) WriteBlobString(image,"\f\n:\032"); |
| 2155 | if (image->montage != (char *) NULL) |
| 2156 | { |
| 2157 | /* |
| 2158 | Write montage tile directory. |
| 2159 | */ |
| 2160 | if (image->directory != (char *) NULL) |
| 2161 | (void) WriteBlob(image,strlen(image->directory),(unsigned char *) |
| 2162 | image->directory); |
| 2163 | (void) WriteBlobByte(image,'\0'); |
| 2164 | } |
| 2165 | if (image->profiles != (void *) NULL) |
| 2166 | { |
| 2167 | const char |
| 2168 | *name; |
| 2169 | |
| 2170 | const StringInfo |
| 2171 | *profile; |
| 2172 | |
| 2173 | /* |
| 2174 | Generic profile. |
| 2175 | */ |
| 2176 | ResetImageProfileIterator(image); |
| 2177 | name=GetNextImageProfile(image); |
| 2178 | while (name != (const char *) NULL) |
| 2179 | { |
| 2180 | profile=GetImageProfile(image,name); |
| 2181 | (void) WriteBlob(image,GetStringInfoLength(profile), |
| 2182 | GetStringInfoDatum(profile)); |
| 2183 | name=GetNextImageProfile(image); |
| 2184 | } |
| 2185 | } |
| 2186 | if (image->storage_class == PseudoClass) |
| 2187 | { |
| 2188 | size_t |
| 2189 | packet_size; |
| 2190 | |
| 2191 | unsigned char |
| 2192 | *colormap, |
| 2193 | *q; |
| 2194 | |
| 2195 | /* |
| 2196 | Allocate colormap. |
| 2197 | */ |
| 2198 | packet_size=(size_t) (3*quantum_info->depth/8); |
| 2199 | colormap=(unsigned char *) AcquireQuantumMemory(image->colors, |
| 2200 | packet_size*sizeof(*colormap)); |
| 2201 | if (colormap == (unsigned char *) NULL) |
| 2202 | ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed"); |
| 2203 | /* |
| 2204 | Write colormap to file. |
| 2205 | */ |
| 2206 | q=colormap; |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 2207 | for (i=0; i < (ssize_t) image->colors; i++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2208 | { |
| 2209 | switch (quantum_info->depth) |
| 2210 | { |
| 2211 | default: |
| 2212 | ThrowWriterException(CorruptImageError,"ImageDepthNotSupported"); |
| 2213 | case 32: |
| 2214 | { |
cristy | 4cb162a | 2010-05-30 03:04:47 +0000 | [diff] [blame] | 2215 | register unsigned int |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2216 | pixel; |
| 2217 | |
| 2218 | pixel=ScaleQuantumToLong(image->colormap[i].red); |
| 2219 | q=PopLongPixel(MSBEndian,pixel,q); |
| 2220 | pixel=ScaleQuantumToLong(image->colormap[i].green); |
| 2221 | q=PopLongPixel(MSBEndian,pixel,q); |
| 2222 | pixel=ScaleQuantumToLong(image->colormap[i].blue); |
| 2223 | q=PopLongPixel(MSBEndian,pixel,q); |
| 2224 | break; |
| 2225 | } |
| 2226 | case 16: |
| 2227 | { |
| 2228 | register unsigned short |
| 2229 | pixel; |
| 2230 | |
| 2231 | pixel=ScaleQuantumToShort(image->colormap[i].red); |
| 2232 | q=PopShortPixel(MSBEndian,pixel,q); |
| 2233 | pixel=ScaleQuantumToShort(image->colormap[i].green); |
| 2234 | q=PopShortPixel(MSBEndian,pixel,q); |
| 2235 | pixel=ScaleQuantumToShort(image->colormap[i].blue); |
| 2236 | q=PopShortPixel(MSBEndian,pixel,q); |
| 2237 | break; |
| 2238 | } |
| 2239 | case 8: |
| 2240 | { |
| 2241 | register unsigned char |
| 2242 | pixel; |
| 2243 | |
| 2244 | pixel=(unsigned char) ScaleQuantumToChar(image->colormap[i].red); |
| 2245 | q=PopCharPixel(pixel,q); |
| 2246 | pixel=(unsigned char) ScaleQuantumToChar( |
| 2247 | image->colormap[i].green); |
| 2248 | q=PopCharPixel(pixel,q); |
| 2249 | pixel=(unsigned char) ScaleQuantumToChar(image->colormap[i].blue); |
| 2250 | q=PopCharPixel(pixel,q); |
| 2251 | break; |
| 2252 | } |
| 2253 | } |
| 2254 | } |
| 2255 | (void) WriteBlob(image,packet_size*image->colors,colormap); |
| 2256 | colormap=(unsigned char *) RelinquishMagickMemory(colormap); |
| 2257 | } |
| 2258 | /* |
| 2259 | Write image pixels to file. |
| 2260 | */ |
cristy | 330af6c | 2010-12-21 14:36:06 +0000 | [diff] [blame] | 2261 | status=MagickTrue; |
| 2262 | switch (compression) |
| 2263 | { |
| 2264 | #if defined(MAGICKCORE_BZLIB_DELEGATE) |
| 2265 | case BZipCompression: |
| 2266 | { |
| 2267 | (void) ResetMagickMemory(&bzip_info,0,sizeof(bzip_info)); |
| 2268 | bzip_info.bzalloc=AcquireBZIPMemory; |
| 2269 | bzip_info.bzfree=RelinquishBZIPMemory; |
| 2270 | code=BZ2_bzCompressInit(&bzip_info,(int) (image->quality == |
| 2271 | UndefinedCompressionQuality ? 7 : MagickMin(image->quality/10, |
| 2272 | 9)),(int) image_info->verbose,0); |
| 2273 | if (code != BZ_OK) |
| 2274 | status=MagickFalse; |
| 2275 | break; |
| 2276 | } |
| 2277 | #endif |
| 2278 | #if defined(MAGICKCORE_LZMA_DELEGATE) |
| 2279 | case LZMACompression: |
| 2280 | { |
cristy | 330af6c | 2010-12-21 14:36:06 +0000 | [diff] [blame] | 2281 | (void) ResetMagickMemory(&allocator,0,sizeof(allocator)); |
| 2282 | allocator.alloc=AcquireLZMAMemory; |
| 2283 | allocator.free=RelinquishLZMAMemory; |
cristy | 9d72f1a | 2010-12-21 20:46:59 +0000 | [diff] [blame] | 2284 | lzma_info=initialize_lzma; |
cristy | 330af6c | 2010-12-21 14:36:06 +0000 | [diff] [blame] | 2285 | lzma_info.allocator=&allocator; |
cristy | 9d72f1a | 2010-12-21 20:46:59 +0000 | [diff] [blame] | 2286 | code=lzma_easy_encoder(&lzma_info,image->quality/10,LZMA_CHECK_SHA256); |
| 2287 | if (code != LZMA_OK) |
| 2288 | status=MagickTrue; |
cristy | 330af6c | 2010-12-21 14:36:06 +0000 | [diff] [blame] | 2289 | break; |
| 2290 | } |
| 2291 | #endif |
| 2292 | #if defined(MAGICKCORE_ZLIB_DELEGATE) |
| 2293 | case LZWCompression: |
| 2294 | case ZipCompression: |
| 2295 | { |
| 2296 | (void) ResetMagickMemory(&zip_info,0,sizeof(zip_info)); |
| 2297 | zip_info.zalloc=AcquireZIPMemory; |
| 2298 | zip_info.zfree=RelinquishZIPMemory; |
| 2299 | code=deflateInit(&zip_info,(int) (image->quality == |
| 2300 | UndefinedCompressionQuality ? 7 : MagickMin(image->quality/10,9))); |
| 2301 | if (code != Z_OK) |
| 2302 | status=MagickFalse; |
| 2303 | break; |
| 2304 | } |
| 2305 | #endif |
| 2306 | default: |
| 2307 | break; |
| 2308 | } |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2309 | quantum_type=GetQuantumType(image,&image->exception); |
| 2310 | pixels=GetQuantumPixels(quantum_info); |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 2311 | for (y=0; y < (ssize_t) image->rows; y++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2312 | { |
| 2313 | register const IndexPacket |
cristy | c47d1f8 | 2009-11-26 01:44:43 +0000 | [diff] [blame] | 2314 | *restrict indexes; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2315 | |
| 2316 | register const PixelPacket |
cristy | c47d1f8 | 2009-11-26 01:44:43 +0000 | [diff] [blame] | 2317 | *restrict p; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2318 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 2319 | register ssize_t |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2320 | x; |
| 2321 | |
cristy | 330af6c | 2010-12-21 14:36:06 +0000 | [diff] [blame] | 2322 | if (status == MagickFalse) |
| 2323 | break; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2324 | p=GetVirtualPixels(image,0,y,image->columns,1,&image->exception); |
| 2325 | if (p == (const PixelPacket *) NULL) |
| 2326 | break; |
| 2327 | indexes=GetVirtualIndexQueue(image); |
| 2328 | q=pixels; |
| 2329 | switch (compression) |
| 2330 | { |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2331 | #if defined(MAGICKCORE_BZLIB_DELEGATE) |
| 2332 | case BZipCompression: |
| 2333 | { |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2334 | bzip_info.next_in=(char *) pixels; |
| 2335 | bzip_info.avail_in=(unsigned int) (packet_size*image->columns); |
cristy | 74fe8f1 | 2009-10-03 19:09:01 +0000 | [diff] [blame] | 2336 | (void) ExportQuantumPixels(image,(const CacheView *) NULL, |
| 2337 | quantum_info,quantum_type,pixels,&image->exception); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2338 | do |
| 2339 | { |
| 2340 | bzip_info.next_out=(char *) compress_pixels; |
| 2341 | bzip_info.avail_out=(unsigned int) BZipMaxExtent(packet_size* |
| 2342 | image->columns); |
| 2343 | code=BZ2_bzCompress(&bzip_info,BZ_FLUSH); |
cristy | 2637717 | 2010-12-20 19:01:58 +0000 | [diff] [blame] | 2344 | if (code != BZ_OK) |
| 2345 | status=MagickFalse; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2346 | length=(size_t) (bzip_info.next_out-(char *) compress_pixels); |
| 2347 | if (length != 0) |
| 2348 | { |
| 2349 | (void) WriteBlobMSBLong(image,(unsigned int) length); |
| 2350 | (void) WriteBlob(image,length,compress_pixels); |
| 2351 | } |
| 2352 | } while (bzip_info.avail_in != 0); |
cristy | 2637717 | 2010-12-20 19:01:58 +0000 | [diff] [blame] | 2353 | break; |
| 2354 | } |
| 2355 | #endif |
| 2356 | #if defined(MAGICKCORE_LZMA_DELEGATE) |
| 2357 | case LZMACompression: |
| 2358 | { |
cristy | 2637717 | 2010-12-20 19:01:58 +0000 | [diff] [blame] | 2359 | lzma_info.next_in=pixels; |
cristy | 330af6c | 2010-12-21 14:36:06 +0000 | [diff] [blame] | 2360 | lzma_info.avail_in=packet_size*image->columns; |
cristy | 2637717 | 2010-12-20 19:01:58 +0000 | [diff] [blame] | 2361 | (void) ExportQuantumPixels(image,(const CacheView *) NULL, |
| 2362 | quantum_info,quantum_type,pixels,&image->exception); |
| 2363 | do |
| 2364 | { |
| 2365 | lzma_info.next_out=compress_pixels; |
| 2366 | lzma_info.avail_out=packet_size*image->columns; |
| 2367 | code=lzma_code(&lzma_info,LZMA_RUN); |
| 2368 | if (code != LZMA_OK) |
| 2369 | status=MagickFalse; |
| 2370 | length=(size_t) (lzma_info.next_out-compress_pixels); |
| 2371 | if (length != 0) |
| 2372 | { |
| 2373 | (void) WriteBlobMSBLong(image,(unsigned int) length); |
| 2374 | (void) WriteBlob(image,length,compress_pixels); |
| 2375 | } |
| 2376 | } while (lzma_info.avail_in != 0); |
cristy | 2637717 | 2010-12-20 19:01:58 +0000 | [diff] [blame] | 2377 | break; |
| 2378 | } |
| 2379 | #endif |
| 2380 | #if defined(MAGICKCORE_ZLIB_DELEGATE) |
| 2381 | case LZWCompression: |
| 2382 | case ZipCompression: |
| 2383 | { |
cristy | 2637717 | 2010-12-20 19:01:58 +0000 | [diff] [blame] | 2384 | zip_info.next_in=pixels; |
| 2385 | zip_info.avail_in=(uInt) (packet_size*image->columns); |
| 2386 | (void) ExportQuantumPixels(image,(const CacheView *) NULL, |
| 2387 | quantum_info,quantum_type,pixels,&image->exception); |
| 2388 | do |
| 2389 | { |
| 2390 | zip_info.next_out=compress_pixels; |
| 2391 | zip_info.avail_out=(uInt) ZipMaxExtent(packet_size*image->columns); |
| 2392 | code=deflate(&zip_info,Z_SYNC_FLUSH); |
| 2393 | if (code != Z_OK) |
| 2394 | status=MagickFalse; |
| 2395 | length=(size_t) (zip_info.next_out-compress_pixels); |
| 2396 | if (length != 0) |
| 2397 | { |
| 2398 | (void) WriteBlobMSBLong(image,(unsigned int) length); |
| 2399 | (void) WriteBlob(image,length,compress_pixels); |
| 2400 | } |
| 2401 | } while (zip_info.avail_in != 0); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2402 | break; |
| 2403 | } |
| 2404 | #endif |
| 2405 | case RLECompression: |
| 2406 | { |
| 2407 | pixel=(*p); |
| 2408 | index=(IndexPacket) 0; |
| 2409 | if (indexes != (IndexPacket *) NULL) |
| 2410 | index=(*indexes); |
| 2411 | length=255; |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 2412 | for (x=0; x < (ssize_t) image->columns; x++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2413 | { |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 2414 | if ((length < 255) && (x < (ssize_t) (image->columns-1)) && |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2415 | (IsColorEqual(p,&pixel) != MagickFalse) && |
| 2416 | ((image->matte == MagickFalse) || |
cristy | d05ecd1 | 2011-04-22 20:44:42 +0000 | [diff] [blame] | 2417 | (GetOpacityPixelComponent(p) == pixel.opacity)) && |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2418 | ((indexes == (IndexPacket *) NULL) || (index == indexes[x]))) |
| 2419 | length++; |
| 2420 | else |
| 2421 | { |
| 2422 | if (x > 0) |
| 2423 | q=PopRunlengthPacket(image,q,length,pixel,index); |
| 2424 | length=0; |
| 2425 | } |
| 2426 | pixel=(*p); |
| 2427 | if (indexes != (IndexPacket *) NULL) |
| 2428 | index=indexes[x]; |
| 2429 | p++; |
| 2430 | } |
| 2431 | q=PopRunlengthPacket(image,q,length,pixel,index); |
| 2432 | (void) WriteBlob(image,(size_t) (q-pixels),pixels); |
| 2433 | break; |
| 2434 | } |
| 2435 | default: |
| 2436 | { |
| 2437 | (void) ExportQuantumPixels(image,(const CacheView *) NULL, |
| 2438 | quantum_info,quantum_type,pixels,&image->exception); |
| 2439 | (void) WriteBlob(image,packet_size*image->columns,pixels); |
| 2440 | break; |
| 2441 | } |
| 2442 | } |
cristy | cee9711 | 2010-05-28 00:44:52 +0000 | [diff] [blame] | 2443 | if (image->previous == (Image *) NULL) |
| 2444 | { |
| 2445 | status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y, |
| 2446 | image->rows); |
| 2447 | if (status == MagickFalse) |
| 2448 | break; |
| 2449 | } |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2450 | } |
cristy | 330af6c | 2010-12-21 14:36:06 +0000 | [diff] [blame] | 2451 | switch (compression) |
| 2452 | { |
| 2453 | #if defined(MAGICKCORE_BZLIB_DELEGATE) |
| 2454 | case BZipCompression: |
| 2455 | { |
| 2456 | for ( ; ; ) |
| 2457 | { |
| 2458 | if (status == MagickFalse) |
| 2459 | break; |
| 2460 | bzip_info.next_out=(char *) compress_pixels; |
| 2461 | bzip_info.avail_out=(unsigned int) BZipMaxExtent(packet_size* |
| 2462 | image->columns); |
| 2463 | code=BZ2_bzCompress(&bzip_info,BZ_FINISH); |
| 2464 | if (code != BZ_OK) |
| 2465 | status=MagickFalse; |
| 2466 | length=(size_t) (bzip_info.next_out-(char *) compress_pixels); |
| 2467 | if (length != 0) |
| 2468 | { |
| 2469 | (void) WriteBlobMSBLong(image,(unsigned int) length); |
| 2470 | (void) WriteBlob(image,length,compress_pixels); |
| 2471 | } |
| 2472 | if (code == BZ_STREAM_END) |
| 2473 | break; |
| 2474 | } |
| 2475 | code=BZ2_bzCompressEnd(&bzip_info); |
| 2476 | if (code != BZ_OK) |
| 2477 | status=MagickFalse; |
| 2478 | break; |
| 2479 | } |
| 2480 | #endif |
| 2481 | #if defined(MAGICKCORE_LZMA_DELEGATE) |
| 2482 | case LZMACompression: |
| 2483 | { |
| 2484 | for ( ; ; ) |
| 2485 | { |
| 2486 | if (status == MagickFalse) |
| 2487 | break; |
| 2488 | lzma_info.next_out=compress_pixels; |
| 2489 | lzma_info.avail_out=packet_size*image->columns; |
| 2490 | code=lzma_code(&lzma_info,LZMA_FINISH); |
| 2491 | if (code != LZMA_OK) |
| 2492 | status=MagickFalse; |
| 2493 | length=(size_t) (lzma_info.next_out-compress_pixels); |
| 2494 | if (length > 6) |
| 2495 | { |
| 2496 | (void) WriteBlobMSBLong(image,(unsigned int) length); |
| 2497 | (void) WriteBlob(image,length,compress_pixels); |
| 2498 | } |
| 2499 | if (code == LZMA_STREAM_END) |
| 2500 | break; |
| 2501 | } |
| 2502 | lzma_end(&lzma_info); |
| 2503 | break; |
| 2504 | } |
| 2505 | #endif |
| 2506 | #if defined(MAGICKCORE_ZLIB_DELEGATE) |
| 2507 | case LZWCompression: |
| 2508 | case ZipCompression: |
| 2509 | { |
| 2510 | for ( ; ; ) |
| 2511 | { |
| 2512 | if (status == MagickFalse) |
| 2513 | break; |
| 2514 | zip_info.next_out=compress_pixels; |
| 2515 | zip_info.avail_out=(uInt) ZipMaxExtent(packet_size* |
| 2516 | image->columns); |
| 2517 | code=deflate(&zip_info,Z_FINISH); |
| 2518 | if (code != Z_OK) |
| 2519 | status=MagickFalse; |
| 2520 | length=(size_t) (zip_info.next_out-compress_pixels); |
| 2521 | if (length > 6) |
| 2522 | { |
| 2523 | (void) WriteBlobMSBLong(image,(unsigned int) length); |
| 2524 | (void) WriteBlob(image,length,compress_pixels); |
| 2525 | } |
| 2526 | if (code == Z_STREAM_END) |
| 2527 | break; |
| 2528 | } |
| 2529 | code=deflateEnd(&zip_info); |
| 2530 | if (code != Z_OK) |
| 2531 | status=MagickFalse; |
| 2532 | break; |
| 2533 | } |
| 2534 | #endif |
| 2535 | default: |
| 2536 | break; |
| 2537 | } |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2538 | quantum_info=DestroyQuantumInfo(quantum_info); |
| 2539 | compress_pixels=(unsigned char *) RelinquishMagickMemory(compress_pixels); |
| 2540 | if (GetNextImageInList(image) == (Image *) NULL) |
| 2541 | break; |
| 2542 | image=SyncNextImageInList(image); |
| 2543 | status=SetImageProgress(image,SaveImagesTag,scene++, |
| 2544 | GetImageListLength(image)); |
| 2545 | if (status == MagickFalse) |
| 2546 | break; |
| 2547 | } while (image_info->adjoin != MagickFalse); |
| 2548 | (void) CloseBlob(image); |
| 2549 | return(status); |
| 2550 | } |