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