cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1 | /* |
| 2 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 3 | % % |
| 4 | % % |
| 5 | % % |
| 6 | % X X CCCC FFFFF % |
| 7 | % X X C F % |
| 8 | % X C FFF % |
| 9 | % X X C F % |
| 10 | % X X CCCC F % |
| 11 | % % |
| 12 | % % |
| 13 | % Read GIMP XCF Image Format % |
| 14 | % % |
| 15 | % Software Design % |
| 16 | % Leonard Rosenthol % |
| 17 | % November 2001 % |
| 18 | % % |
| 19 | % % |
cristy | 16af1cb | 2009-12-11 21:38:29 +0000 | [diff] [blame] | 20 | % Copyright 1999-2010 ImageMagick Studio LLC, a non-profit organization % |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 21 | % dedicated to making software imaging solutions freely available. % |
| 22 | % % |
| 23 | % You may not use this file except in compliance with the License. You may % |
| 24 | % obtain a copy of the License at % |
| 25 | % % |
| 26 | % http://www.imagemagick.org/script/license.php % |
| 27 | % % |
| 28 | % Unless required by applicable law or agreed to in writing, software % |
| 29 | % distributed under the License is distributed on an "AS IS" BASIS, % |
| 30 | % WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. % |
| 31 | % See the License for the specific language governing permissions and % |
| 32 | % limitations under the License. % |
| 33 | % % |
| 34 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 35 | % |
| 36 | % |
| 37 | */ |
| 38 | |
| 39 | /* |
| 40 | Include declarations. |
| 41 | */ |
| 42 | #include "magick/studio.h" |
| 43 | #include "magick/blob.h" |
| 44 | #include "magick/blob-private.h" |
| 45 | #include "magick/cache.h" |
| 46 | #include "magick/color.h" |
| 47 | #include "magick/composite.h" |
| 48 | #include "magick/exception.h" |
| 49 | #include "magick/exception-private.h" |
| 50 | #include "magick/image.h" |
| 51 | #include "magick/image-private.h" |
| 52 | #include "magick/list.h" |
| 53 | #include "magick/magick.h" |
| 54 | #include "magick/memory_.h" |
| 55 | #include "magick/quantize.h" |
| 56 | #include "magick/quantum-private.h" |
| 57 | #include "magick/static.h" |
| 58 | #include "magick/string_.h" |
| 59 | #include "magick/module.h" |
| 60 | |
| 61 | /* |
| 62 | Typedef declarations. |
| 63 | */ |
| 64 | typedef enum |
| 65 | { |
| 66 | GIMP_RGB, |
| 67 | GIMP_GRAY, |
| 68 | GIMP_INDEXED |
| 69 | } GimpImageBaseType; |
| 70 | |
| 71 | typedef enum |
| 72 | { |
| 73 | PROP_END = 0, |
| 74 | PROP_COLORMAP = 1, |
| 75 | PROP_ACTIVE_LAYER = 2, |
| 76 | PROP_ACTIVE_CHANNEL = 3, |
| 77 | PROP_SELECTION = 4, |
| 78 | PROP_FLOATING_SELECTION = 5, |
| 79 | PROP_OPACITY = 6, |
| 80 | PROP_MODE = 7, |
| 81 | PROP_VISIBLE = 8, |
| 82 | PROP_LINKED = 9, |
| 83 | PROP_PRESERVE_TRANSPARENCY = 10, |
| 84 | PROP_APPLY_MASK = 11, |
| 85 | PROP_EDIT_MASK = 12, |
| 86 | PROP_SHOW_MASK = 13, |
| 87 | PROP_SHOW_MASKED = 14, |
| 88 | PROP_OFFSETS = 15, |
| 89 | PROP_COLOR = 16, |
| 90 | PROP_COMPRESSION = 17, |
| 91 | PROP_GUIDES = 18, |
| 92 | PROP_RESOLUTION = 19, |
| 93 | PROP_TATTOO = 20, |
| 94 | PROP_PARASITES = 21, |
| 95 | PROP_UNIT = 22, |
| 96 | PROP_PATHS = 23, |
| 97 | PROP_USER_UNIT = 24 |
| 98 | } PropType; |
| 99 | |
| 100 | typedef enum |
| 101 | { |
| 102 | COMPRESS_NONE = 0, |
| 103 | COMPRESS_RLE = 1, |
| 104 | COMPRESS_ZLIB = 2, /* unused */ |
| 105 | COMPRESS_FRACTAL = 3 /* unused */ |
| 106 | } XcfCompressionType; |
| 107 | |
| 108 | typedef struct |
| 109 | { |
| 110 | unsigned long |
| 111 | width, |
| 112 | height, |
| 113 | image_type, |
| 114 | bytes_per_pixel; |
| 115 | |
| 116 | int |
| 117 | compression; |
| 118 | |
| 119 | size_t |
| 120 | file_size; |
| 121 | |
| 122 | ExceptionInfo |
| 123 | *exception; |
| 124 | } XCFDocInfo; |
| 125 | |
| 126 | typedef struct |
| 127 | { |
| 128 | char |
| 129 | name[1024]; |
| 130 | |
| 131 | unsigned int |
| 132 | active; |
| 133 | |
| 134 | unsigned long |
| 135 | width, |
| 136 | height, |
| 137 | type, |
| 138 | opacity, |
| 139 | visible, |
| 140 | linked, |
| 141 | preserve_trans, |
| 142 | apply_mask, |
| 143 | show_mask, |
| 144 | edit_mask, |
| 145 | floating_offset; |
| 146 | |
| 147 | ssize_t |
| 148 | offset_x, |
| 149 | offset_y; |
| 150 | |
| 151 | unsigned long |
| 152 | mode, |
| 153 | tattoo; |
| 154 | |
| 155 | Image |
| 156 | *image; |
| 157 | } XCFLayerInfo; |
| 158 | |
| 159 | #define TILE_WIDTH 64 |
| 160 | #define TILE_HEIGHT 64 |
| 161 | |
| 162 | typedef struct |
| 163 | { |
| 164 | unsigned char |
| 165 | red, |
| 166 | green, |
| 167 | blue, |
| 168 | opacity; |
| 169 | } XCFPixelPacket; |
| 170 | |
| 171 | /* |
| 172 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 173 | % % |
| 174 | % % |
| 175 | % % |
| 176 | % I s X C F % |
| 177 | % % |
| 178 | % % |
| 179 | % % |
| 180 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 181 | % |
| 182 | % IsXCF() returns MagickTrue if the image format type, identified by the |
| 183 | % magick string, is XCF (GIMP native format). |
| 184 | % |
| 185 | % The format of the IsXCF method is: |
| 186 | % |
| 187 | % MagickBooleanType IsXCF(const unsigned char *magick,const size_t length) |
| 188 | % |
| 189 | % A description of each parameter follows: |
| 190 | % |
| 191 | % o magick: compare image format pattern against these bytes. |
| 192 | % |
| 193 | % o length: Specifies the length of the magick string. |
| 194 | % |
| 195 | % |
| 196 | */ |
| 197 | static MagickBooleanType IsXCF(const unsigned char *magick,const size_t length) |
| 198 | { |
| 199 | if (length < 8) |
| 200 | return(MagickFalse); |
| 201 | if (LocaleNCompare((char *) magick,"gimp xcf",8) == 0) |
| 202 | return(MagickTrue); |
| 203 | return(MagickFalse); |
| 204 | } |
| 205 | |
| 206 | typedef enum |
| 207 | { |
| 208 | GIMP_NORMAL_MODE, |
| 209 | GIMP_DISSOLVE_MODE, |
| 210 | GIMP_BEHIND_MODE, |
| 211 | GIMP_MULTIPLY_MODE, |
| 212 | GIMP_SCREEN_MODE, |
| 213 | GIMP_OVERLAY_MODE, |
| 214 | GIMP_DIFFERENCE_MODE, |
| 215 | GIMP_ADDITION_MODE, |
| 216 | GIMP_SUBTRACT_MODE, |
| 217 | GIMP_DARKEN_ONLY_MODE, |
| 218 | GIMP_LIGHTEN_ONLY_MODE, |
| 219 | GIMP_HUE_MODE, |
| 220 | GIMP_SATURATION_MODE, |
| 221 | GIMP_COLOR_MODE, |
| 222 | GIMP_VALUE_MODE, |
| 223 | GIMP_DIVIDE_MODE, |
| 224 | GIMP_DODGE_MODE, |
| 225 | GIMP_BURN_MODE, |
| 226 | GIMP_HARDLIGHT_MODE |
| 227 | } GimpLayerModeEffects; |
| 228 | |
| 229 | /* |
| 230 | Simple utility routine to convert between PSD blending modes and |
| 231 | ImageMagick compositing operators |
| 232 | */ |
| 233 | static CompositeOperator GIMPBlendModeToCompositeOperator( |
| 234 | unsigned long blendMode) |
| 235 | { |
| 236 | switch ( blendMode ) |
| 237 | { |
| 238 | case GIMP_NORMAL_MODE: return( OverCompositeOp ); |
| 239 | case GIMP_DISSOLVE_MODE: return( DissolveCompositeOp ); |
| 240 | case GIMP_MULTIPLY_MODE: return( MultiplyCompositeOp ); |
| 241 | case GIMP_SCREEN_MODE: return( ScreenCompositeOp ); |
| 242 | case GIMP_OVERLAY_MODE: return( OverlayCompositeOp ); |
| 243 | case GIMP_DIFFERENCE_MODE: return( DifferenceCompositeOp ); |
| 244 | case GIMP_ADDITION_MODE: return( AddCompositeOp ); |
| 245 | case GIMP_SUBTRACT_MODE: return( SubtractCompositeOp ); |
| 246 | case GIMP_DARKEN_ONLY_MODE: return( DarkenCompositeOp ); |
| 247 | case GIMP_LIGHTEN_ONLY_MODE:return( LightenCompositeOp ); |
| 248 | case GIMP_HUE_MODE: return( HueCompositeOp ); |
| 249 | case GIMP_SATURATION_MODE: return( SaturateCompositeOp ); |
| 250 | case GIMP_COLOR_MODE: return( ColorizeCompositeOp ); |
| 251 | case GIMP_DODGE_MODE: return( ColorDodgeCompositeOp ); |
| 252 | case GIMP_BURN_MODE: return( ColorBurnCompositeOp ); |
| 253 | case GIMP_HARDLIGHT_MODE: return( HardLightCompositeOp ); |
| 254 | case GIMP_DIVIDE_MODE: return( DivideCompositeOp ); |
| 255 | /* these are the ones we don't support...yet */ |
| 256 | case GIMP_BEHIND_MODE: return( OverCompositeOp ); |
| 257 | case GIMP_VALUE_MODE: return( OverCompositeOp ); |
| 258 | default: |
| 259 | return(OverCompositeOp); |
| 260 | } |
| 261 | } |
| 262 | |
| 263 | /* |
| 264 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 265 | % % |
| 266 | % % |
| 267 | % % |
| 268 | + R e a d B l o b S t r i n g W i t h L o n g S i z e % |
| 269 | % % |
| 270 | % % |
| 271 | % % |
| 272 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 273 | % |
| 274 | % ReadBlobStringWithLongSize reads characters from a blob or file |
| 275 | % starting with a long length byte and then characters to that length |
| 276 | % |
| 277 | % The format of the ReadBlobStringWithLongSize method is: |
| 278 | % |
| 279 | % char *ReadBlobStringWithLongSize(Image *image,char *string) |
| 280 | % |
| 281 | % A description of each parameter follows: |
| 282 | % |
| 283 | % o image: the image. |
| 284 | % |
| 285 | % o string: the address of a character buffer. |
| 286 | % |
| 287 | */ |
| 288 | |
| 289 | static inline size_t MagickMin(const size_t x,const size_t y) |
| 290 | { |
| 291 | if (x < y) |
| 292 | return(x); |
| 293 | return(y); |
| 294 | } |
| 295 | |
| 296 | static char *ReadBlobStringWithLongSize(Image *image,char *string,size_t max) |
| 297 | { |
| 298 | int |
| 299 | c; |
| 300 | |
| 301 | MagickOffsetType |
| 302 | offset; |
| 303 | |
| 304 | register long |
| 305 | i; |
| 306 | |
| 307 | unsigned long |
| 308 | length; |
| 309 | |
| 310 | assert(image != (Image *) NULL); |
| 311 | assert(image->signature == MagickSignature); |
| 312 | assert(max != 0); |
| 313 | if (image->debug != MagickFalse) |
| 314 | (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename); |
| 315 | length=ReadBlobMSBLong(image); |
| 316 | for (i=0; i < (long) MagickMin(length,max-1); i++) |
| 317 | { |
| 318 | c=ReadBlobByte(image); |
| 319 | if (c == EOF) |
| 320 | return((char *) NULL); |
| 321 | string[i]=(char) c; |
| 322 | } |
| 323 | string[i]='\0'; |
| 324 | offset=SeekBlob(image,(MagickOffsetType) (length-i),SEEK_CUR); |
| 325 | if (offset < 0) |
| 326 | (void) ThrowMagickException(&image->exception,GetMagickModule(), |
| 327 | CorruptImageError,"ImproperImageHeader","`%s'",image->filename); |
| 328 | return(string); |
| 329 | } |
| 330 | |
| 331 | static MagickBooleanType load_tile(Image *image,Image *tile_image, |
| 332 | XCFDocInfo *inDocInfo,XCFLayerInfo *inLayerInfo,size_t data_length) |
| 333 | { |
| 334 | ExceptionInfo |
| 335 | *exception; |
| 336 | |
| 337 | long |
| 338 | y; |
| 339 | |
| 340 | register long |
| 341 | x; |
| 342 | |
| 343 | register PixelPacket |
| 344 | *q; |
| 345 | |
| 346 | ssize_t |
| 347 | count; |
| 348 | |
| 349 | unsigned char |
| 350 | *graydata; |
| 351 | |
| 352 | XCFPixelPacket |
| 353 | *xcfdata, |
| 354 | *xcfodata; |
| 355 | |
| 356 | xcfdata=(XCFPixelPacket *) AcquireQuantumMemory(data_length,sizeof(*xcfdata)); |
| 357 | if (xcfdata == (XCFPixelPacket *) NULL) |
| 358 | ThrowBinaryException(ResourceLimitError,"MemoryAllocationFailed", |
| 359 | image->filename); |
| 360 | xcfodata=xcfdata; |
| 361 | graydata=(unsigned char *) xcfdata; /* used by gray and indexed */ |
| 362 | count=ReadBlob(image,data_length,(unsigned char *) xcfdata); |
| 363 | if (count != (ssize_t) data_length) |
| 364 | ThrowBinaryException(CorruptImageError,"NotEnoughPixelData", |
| 365 | image->filename); |
| 366 | exception=(&image->exception); |
| 367 | for (y=0; y < (long) tile_image->rows; y++) |
| 368 | { |
| 369 | q=QueueAuthenticPixels(tile_image,0,y,tile_image->columns,1,exception); |
| 370 | if (q == (PixelPacket *) NULL) |
| 371 | break; |
| 372 | if (inDocInfo->image_type == GIMP_GRAY) |
| 373 | { |
| 374 | for (x=0; x < (long) tile_image->columns; x++) |
| 375 | { |
| 376 | q->red=ScaleCharToQuantum(*graydata); |
| 377 | q->green=q->red; |
| 378 | q->blue=q->red; |
| 379 | q->opacity=ScaleCharToQuantum(255-inLayerInfo->opacity); |
| 380 | graydata++; |
| 381 | q++; |
| 382 | } |
| 383 | } |
| 384 | else |
| 385 | if (inDocInfo->image_type == GIMP_RGB) |
| 386 | { |
| 387 | for (x=0; x < (long) tile_image->columns; x++) |
| 388 | { |
| 389 | q->red=ScaleCharToQuantum(xcfdata->red); |
| 390 | q->green=ScaleCharToQuantum(xcfdata->green); |
| 391 | q->blue=ScaleCharToQuantum(xcfdata->blue); |
| 392 | q->opacity=(Quantum) (xcfdata->opacity == 0U ? TransparentOpacity : |
| 393 | ScaleCharToQuantum(255-inLayerInfo->opacity)); |
| 394 | xcfdata++; |
| 395 | q++; |
| 396 | } |
| 397 | } |
| 398 | if (SyncAuthenticPixels(tile_image,exception) == MagickFalse) |
| 399 | break; |
| 400 | } |
| 401 | xcfodata=(XCFPixelPacket *) RelinquishMagickMemory(xcfodata); |
| 402 | return MagickTrue; |
| 403 | } |
| 404 | |
| 405 | static MagickBooleanType load_tile_rle(Image *image,Image *tile_image, |
| 406 | XCFDocInfo *inDocInfo,XCFLayerInfo *inLayerInfo,size_t data_length) |
| 407 | { |
| 408 | ExceptionInfo |
| 409 | *exception; |
| 410 | |
| 411 | long |
| 412 | i, |
| 413 | j; |
| 414 | |
| 415 | MagickOffsetType |
| 416 | size; |
| 417 | |
| 418 | register PixelPacket |
| 419 | *q; |
| 420 | |
| 421 | ssize_t |
| 422 | bytes_per_pixel, |
| 423 | count; |
| 424 | |
| 425 | size_t |
| 426 | length; |
| 427 | |
| 428 | unsigned char |
| 429 | data, |
| 430 | pixel, |
| 431 | *xcfdata, |
| 432 | *xcfodata, |
| 433 | *xcfdatalimit; |
| 434 | |
| 435 | bytes_per_pixel=(ssize_t) inDocInfo->bytes_per_pixel; |
| 436 | xcfdata=(unsigned char *) AcquireQuantumMemory(data_length,sizeof(*xcfdata)); |
| 437 | if (xcfdata == (unsigned char *) NULL) |
| 438 | ThrowBinaryException(ResourceLimitError,"MemoryAllocationFailed", |
| 439 | image->filename); |
| 440 | xcfodata=xcfdata; |
| 441 | count=ReadBlob(image, (size_t) data_length, xcfdata); |
| 442 | xcfdatalimit = xcfodata+count-1; |
| 443 | exception=(&image->exception); |
| 444 | for (i=0; i < (long) bytes_per_pixel; i++) |
| 445 | { |
| 446 | q=GetAuthenticPixels(tile_image,0,0,tile_image->columns,tile_image->rows,exception); |
| 447 | size=(MagickOffsetType) tile_image->rows*tile_image->columns; |
| 448 | while (size > 0) |
| 449 | { |
| 450 | if (xcfdata > xcfdatalimit) |
| 451 | goto bogus_rle; |
| 452 | pixel=(*xcfdata++); |
| 453 | length=(size_t) pixel; |
| 454 | if (length >= 128) |
| 455 | { |
| 456 | length=255-(length-1); |
| 457 | if (length == 128) |
| 458 | { |
| 459 | if (xcfdata >= xcfdatalimit) |
| 460 | goto bogus_rle; |
| 461 | length=(size_t) ((*xcfdata << 8) + xcfdata[1]); |
| 462 | xcfdata+=2; |
| 463 | } |
| 464 | size-=length; |
| 465 | if (size < 0) |
| 466 | goto bogus_rle; |
| 467 | if (&xcfdata[length-1] > xcfdatalimit) |
| 468 | goto bogus_rle; |
| 469 | while (length-- > 0) |
| 470 | { |
| 471 | data=(*xcfdata++); |
| 472 | switch (i) |
| 473 | { |
| 474 | case 0: |
| 475 | { |
| 476 | q->red=ScaleCharToQuantum(data); |
| 477 | if (inDocInfo->image_type == GIMP_GRAY) |
| 478 | { |
| 479 | q->green=ScaleCharToQuantum(data); |
| 480 | q->blue=ScaleCharToQuantum(data); |
| 481 | q->opacity=ScaleCharToQuantum(255-inLayerInfo->opacity); |
| 482 | } |
| 483 | else |
| 484 | { |
| 485 | q->green= q->red; |
| 486 | q->blue= q->red; |
| 487 | q->opacity=ScaleCharToQuantum(255-inLayerInfo->opacity); |
| 488 | } |
| 489 | break; |
| 490 | } |
| 491 | case 1: |
| 492 | { |
| 493 | q->green=ScaleCharToQuantum(data); |
| 494 | break; |
| 495 | } |
| 496 | case 2: |
| 497 | { |
| 498 | q->blue=ScaleCharToQuantum(data); |
| 499 | break; |
| 500 | } |
| 501 | case 3: |
| 502 | { |
| 503 | q->opacity=(Quantum) (data == 0 ? TransparentOpacity : |
| 504 | ScaleCharToQuantum(255-inLayerInfo->opacity)); |
| 505 | break; |
| 506 | } |
| 507 | } |
| 508 | q++; |
| 509 | } |
| 510 | } |
| 511 | else |
| 512 | { |
| 513 | length+=1; |
| 514 | if (length == 128) |
| 515 | { |
| 516 | if (xcfdata >= xcfdatalimit) |
| 517 | goto bogus_rle; |
| 518 | length=(size_t) ((*xcfdata << 8) + xcfdata[1]); |
| 519 | xcfdata+=2; |
| 520 | } |
| 521 | size-=length; |
| 522 | if (size < 0) |
| 523 | goto bogus_rle; |
| 524 | if (xcfdata > xcfdatalimit) |
| 525 | goto bogus_rle; |
| 526 | pixel=(*xcfdata++); |
| 527 | for (j= 0; j < (long) length; j++) |
| 528 | { |
| 529 | data=pixel; |
| 530 | switch (i) |
| 531 | { |
| 532 | case 0: |
| 533 | { |
| 534 | q->red=ScaleCharToQuantum(data); |
| 535 | if (inDocInfo->image_type == GIMP_GRAY) |
| 536 | { |
| 537 | q->green=ScaleCharToQuantum(data); |
| 538 | q->blue=ScaleCharToQuantum(data); |
| 539 | q->opacity=ScaleCharToQuantum(255-inLayerInfo->opacity); |
| 540 | } |
| 541 | else |
| 542 | { |
| 543 | q->green=q->red; |
| 544 | q->blue=q->red; |
| 545 | q->opacity=ScaleCharToQuantum(255-inLayerInfo->opacity); |
| 546 | } |
| 547 | break; |
| 548 | } |
| 549 | case 1: |
| 550 | { |
| 551 | q->green=ScaleCharToQuantum(data); |
| 552 | break; |
| 553 | } |
| 554 | case 2: |
| 555 | { |
| 556 | q->blue=ScaleCharToQuantum(data); |
| 557 | break; |
| 558 | } |
| 559 | case 3: |
| 560 | { |
| 561 | q->opacity=(Quantum) (data == 0 ? TransparentOpacity : |
| 562 | ScaleCharToQuantum(255-inLayerInfo->opacity)); |
| 563 | break; |
| 564 | } |
| 565 | } |
| 566 | q++; |
| 567 | } |
| 568 | } |
| 569 | } |
| 570 | if (SyncAuthenticPixels(tile_image,exception) == MagickFalse) |
| 571 | break; |
| 572 | } |
| 573 | xcfodata=(unsigned char *) RelinquishMagickMemory(xcfodata); |
| 574 | return(MagickTrue); |
| 575 | |
| 576 | bogus_rle: |
| 577 | if (xcfodata != (unsigned char *) NULL) |
| 578 | xcfodata=(unsigned char *) RelinquishMagickMemory(xcfodata); |
| 579 | return(MagickFalse); |
| 580 | } |
| 581 | |
| 582 | static MagickBooleanType load_level(Image *image,XCFDocInfo *inDocInfo, |
| 583 | XCFLayerInfo *inLayerInfo) |
| 584 | { |
| 585 | ExceptionInfo |
| 586 | *exception; |
| 587 | |
| 588 | int |
| 589 | destLeft = 0, |
| 590 | destTop = 0; |
| 591 | |
| 592 | Image* |
| 593 | tile_image; |
| 594 | |
| 595 | MagickBooleanType |
| 596 | status; |
| 597 | |
| 598 | MagickOffsetType |
| 599 | saved_pos, |
| 600 | offset, |
| 601 | offset2; |
| 602 | |
| 603 | register long |
| 604 | i; |
| 605 | |
| 606 | unsigned long |
| 607 | width, |
| 608 | height, |
| 609 | ntiles, |
| 610 | ntile_rows, |
| 611 | ntile_cols, |
| 612 | tile_image_width, |
| 613 | tile_image_height; |
| 614 | |
| 615 | /* start reading the data */ |
| 616 | exception=inDocInfo->exception; |
| 617 | width=ReadBlobMSBLong(image); |
| 618 | height=ReadBlobMSBLong(image); |
| 619 | |
| 620 | /* read in the first tile offset. |
| 621 | * if it is '0', then this tile level is empty |
| 622 | * and we can simply return. |
| 623 | */ |
| 624 | offset=(MagickOffsetType) ReadBlobMSBLong(image); |
| 625 | if (offset == 0) |
| 626 | return(MagickTrue); |
| 627 | /* Initialise the reference for the in-memory tile-compression |
| 628 | */ |
| 629 | ntile_rows=(height+TILE_HEIGHT-1)/TILE_HEIGHT; |
| 630 | ntile_cols=(width+TILE_WIDTH-1)/TILE_WIDTH; |
| 631 | ntiles=ntile_rows*ntile_cols; |
| 632 | for (i = 0; i < (long) ntiles; i++) |
| 633 | { |
| 634 | status=MagickFalse; |
| 635 | if (offset == 0) |
| 636 | ThrowBinaryException(CorruptImageError,"NotEnoughTiles",image->filename); |
| 637 | /* save the current position as it is where the |
| 638 | * next tile offset is stored. |
| 639 | */ |
| 640 | saved_pos=TellBlob(image); |
| 641 | /* read in the offset of the next tile so we can calculate the amount |
| 642 | of data needed for this tile*/ |
| 643 | offset2=(MagickOffsetType)ReadBlobMSBLong(image); |
| 644 | /* if the offset is 0 then we need to read in the maximum possible |
| 645 | allowing for negative compression */ |
| 646 | if (offset2 == 0) |
| 647 | offset2=(MagickOffsetType) (offset + TILE_WIDTH * TILE_WIDTH * 4* 1.5); |
| 648 | /* seek to the tile offset */ |
| 649 | offset=SeekBlob(image, offset, SEEK_SET); |
| 650 | |
| 651 | /* allocate the image for the tile |
| 652 | NOTE: the last tile in a row or column may not be a full tile! |
| 653 | */ |
| 654 | tile_image_width=(unsigned long) (destLeft == (int) ntile_cols-1 ? |
| 655 | (int) width % TILE_WIDTH : TILE_WIDTH); |
| 656 | if (tile_image_width == 0) tile_image_width=TILE_WIDTH; |
| 657 | tile_image_height = (unsigned long) (destTop == (int) ntile_rows-1 ? |
| 658 | (int) height % TILE_HEIGHT : TILE_HEIGHT); |
| 659 | if (tile_image_height == 0) tile_image_height=TILE_HEIGHT; |
| 660 | tile_image=CloneImage(inLayerInfo->image,tile_image_width, |
| 661 | tile_image_height,MagickTrue,exception); |
| 662 | |
| 663 | /* read in the tile */ |
| 664 | switch (inDocInfo->compression) |
| 665 | { |
| 666 | case COMPRESS_NONE: |
| 667 | if (load_tile(image,tile_image,inDocInfo,inLayerInfo,(size_t) (offset2-offset)) == 0) |
| 668 | status=MagickTrue; |
| 669 | break; |
| 670 | case COMPRESS_RLE: |
| 671 | if (load_tile_rle (image,tile_image,inDocInfo,inLayerInfo, |
| 672 | (int) (offset2-offset)) == 0) |
| 673 | status=MagickTrue; |
| 674 | break; |
| 675 | case COMPRESS_ZLIB: |
| 676 | ThrowBinaryException(CoderError,"ZipCompressNotSupported", |
| 677 | image->filename) |
| 678 | case COMPRESS_FRACTAL: |
| 679 | ThrowBinaryException(CoderError,"FractalCompressNotSupported", |
| 680 | image->filename) |
| 681 | } |
| 682 | |
| 683 | /* composite the tile onto the layer's image, and then destroy it */ |
| 684 | (void) CompositeImage(inLayerInfo->image,CopyCompositeOp,tile_image, |
| 685 | destLeft * TILE_WIDTH,destTop*TILE_HEIGHT); |
| 686 | tile_image=DestroyImage(tile_image); |
| 687 | |
| 688 | /* adjust tile position */ |
| 689 | destLeft++; |
| 690 | if (destLeft >= (int) ntile_cols) |
| 691 | { |
| 692 | destLeft = 0; |
| 693 | destTop++; |
| 694 | } |
| 695 | if (status != MagickFalse) |
| 696 | return(MagickFalse); |
| 697 | /* restore the saved position so we'll be ready to |
| 698 | * read the next offset. |
| 699 | */ |
| 700 | offset=SeekBlob(image, saved_pos, SEEK_SET); |
| 701 | /* read in the offset of the next tile */ |
| 702 | offset=(MagickOffsetType) ReadBlobMSBLong(image); |
| 703 | } |
| 704 | if (offset != 0) |
| 705 | ThrowBinaryException(CorruptImageError,"CorruptImage",image->filename) |
| 706 | return(MagickTrue); |
| 707 | } |
| 708 | |
| 709 | static MagickBooleanType load_hierarchy(Image *image,XCFDocInfo *inDocInfo, |
| 710 | XCFLayerInfo *inLayer) |
| 711 | { |
| 712 | MagickOffsetType |
| 713 | saved_pos, |
| 714 | offset, |
| 715 | junk; |
| 716 | |
| 717 | unsigned long |
| 718 | width, |
| 719 | height, |
| 720 | bytes_per_pixel; |
| 721 | |
| 722 | width=ReadBlobMSBLong(image); |
| 723 | height=ReadBlobMSBLong(image); |
| 724 | bytes_per_pixel=inDocInfo->bytes_per_pixel=ReadBlobMSBLong(image); |
| 725 | |
| 726 | /* load in the levels...we make sure that the number of levels |
| 727 | * calculated when the TileManager was created is the same |
| 728 | * as the number of levels found in the file. |
| 729 | */ |
| 730 | offset=(MagickOffsetType) ReadBlobMSBLong(image); /* top level */ |
| 731 | |
| 732 | /* discard offsets for layers below first, if any. |
| 733 | */ |
| 734 | do |
| 735 | { |
| 736 | junk=(MagickOffsetType) ReadBlobMSBLong(image); |
| 737 | } |
| 738 | while (junk != 0); |
| 739 | |
| 740 | /* save the current position as it is where the |
| 741 | * next level offset is stored. |
| 742 | */ |
| 743 | saved_pos=TellBlob(image); |
| 744 | |
| 745 | /* seek to the level offset */ |
| 746 | offset=SeekBlob(image, offset, SEEK_SET); |
| 747 | |
| 748 | /* read in the level */ |
| 749 | if (load_level (image, inDocInfo, inLayer) == 0) |
| 750 | return(MagickFalse); |
| 751 | /* restore the saved position so we'll be ready to |
| 752 | * read the next offset. |
| 753 | */ |
| 754 | offset=SeekBlob(image, saved_pos, SEEK_SET); |
| 755 | return(MagickTrue); |
| 756 | } |
| 757 | |
| 758 | static MagickBooleanType ReadOneLayer(Image* image,XCFDocInfo* inDocInfo, |
| 759 | XCFLayerInfo *outLayer ) |
| 760 | { |
| 761 | long |
| 762 | i; |
| 763 | |
| 764 | MagickOffsetType |
| 765 | offset; |
| 766 | |
| 767 | unsigned int |
| 768 | foundPropEnd = 0; |
| 769 | |
| 770 | unsigned long |
| 771 | hierarchy_offset, |
| 772 | layer_mask_offset; |
| 773 | |
| 774 | /* clear the block! */ |
| 775 | (void) ResetMagickMemory( outLayer, 0, sizeof( XCFLayerInfo ) ); |
| 776 | /* read in the layer width, height, type and name */ |
| 777 | outLayer->width = ReadBlobMSBLong(image); |
| 778 | outLayer->height = ReadBlobMSBLong(image); |
| 779 | outLayer->type = ReadBlobMSBLong(image); |
| 780 | (void) ReadBlobStringWithLongSize(image, outLayer->name, |
| 781 | sizeof(outLayer->name)); |
| 782 | /* allocate the image for this layer */ |
| 783 | outLayer->image=CloneImage(image,outLayer->width, outLayer->height,MagickTrue, |
| 784 | &image->exception); |
| 785 | if (outLayer->image == (Image *) NULL) |
| 786 | return MagickFalse; |
| 787 | /* read the layer properties! */ |
| 788 | foundPropEnd = 0; |
| 789 | while ( (foundPropEnd == MagickFalse) && (EOFBlob(image) == MagickFalse) ) { |
| 790 | PropType prop_type = (PropType) ReadBlobMSBLong(image); |
| 791 | unsigned long prop_size = ReadBlobMSBLong(image); |
| 792 | switch (prop_type) |
| 793 | { |
| 794 | case PROP_END: |
| 795 | foundPropEnd = 1; |
| 796 | break; |
| 797 | case PROP_ACTIVE_LAYER: |
| 798 | outLayer->active = 1; |
| 799 | break; |
| 800 | case PROP_FLOATING_SELECTION: |
| 801 | outLayer->floating_offset = ReadBlobMSBLong(image); |
| 802 | break; |
| 803 | case PROP_OPACITY: |
| 804 | outLayer->opacity = ReadBlobMSBLong(image); |
| 805 | break; |
| 806 | case PROP_VISIBLE: |
| 807 | outLayer->visible = ReadBlobMSBLong(image); |
| 808 | break; |
| 809 | case PROP_LINKED: |
| 810 | outLayer->linked = ReadBlobMSBLong(image); |
| 811 | break; |
| 812 | case PROP_PRESERVE_TRANSPARENCY: |
| 813 | outLayer->preserve_trans = ReadBlobMSBLong(image); |
| 814 | break; |
| 815 | case PROP_APPLY_MASK: |
| 816 | outLayer->apply_mask = ReadBlobMSBLong(image); |
| 817 | break; |
| 818 | case PROP_EDIT_MASK: |
| 819 | outLayer->edit_mask = ReadBlobMSBLong(image); |
| 820 | break; |
| 821 | case PROP_SHOW_MASK: |
| 822 | outLayer->show_mask = ReadBlobMSBLong(image); |
| 823 | break; |
| 824 | case PROP_OFFSETS: |
| 825 | outLayer->offset_x = (long) ReadBlobMSBLong(image); |
| 826 | outLayer->offset_y = (long) ReadBlobMSBLong(image); |
| 827 | break; |
| 828 | case PROP_MODE: |
| 829 | outLayer->mode = ReadBlobMSBLong(image); |
| 830 | break; |
| 831 | case PROP_TATTOO: |
| 832 | outLayer->preserve_trans = ReadBlobMSBLong(image); |
| 833 | break; |
| 834 | case PROP_PARASITES: |
| 835 | { |
| 836 | for (i=0; i < (long) prop_size; i++ ) |
| 837 | (void) ReadBlobByte(image); |
| 838 | |
| 839 | /* |
| 840 | long base = info->cp; |
| 841 | GimpParasite *p; |
| 842 | while (info->cp - base < prop_size) |
| 843 | { |
| 844 | p = xcf_load_parasite(info); |
| 845 | gimp_drawable_parasite_attach(GIMP_DRAWABLE(layer), p); |
| 846 | gimp_parasite_free(p); |
| 847 | } |
| 848 | if (info->cp - base != prop_size) |
| 849 | g_message ("Error detected while loading a layer's parasites"); |
| 850 | */ |
| 851 | } |
| 852 | break; |
| 853 | default: |
| 854 | /* g_message ("unexpected/unknown layer property: %d (skipping)", |
| 855 | prop_type); */ |
| 856 | |
| 857 | { |
| 858 | int buf[16]; |
| 859 | ssize_t amount; |
| 860 | |
| 861 | /* read over it... */ |
| 862 | while ((prop_size > 0) && (EOFBlob(image) == MagickFalse)) |
| 863 | { |
| 864 | amount = (ssize_t) MagickMin(16, prop_size); |
| 865 | amount = ReadBlob(image, (size_t) amount, (unsigned char *) &buf); |
| 866 | if (!amount) |
| 867 | ThrowBinaryException(CorruptImageError,"CorruptImage", |
| 868 | image->filename); |
| 869 | prop_size -= (unsigned long) MagickMin(16, (size_t) amount); |
| 870 | } |
| 871 | } |
| 872 | break; |
| 873 | } |
| 874 | } |
| 875 | |
| 876 | if (foundPropEnd == MagickFalse) |
| 877 | return(MagickFalse); |
| 878 | /* clear the image based on the layer opacity */ |
| 879 | outLayer->image->background_color.opacity= |
| 880 | ScaleCharToQuantum((unsigned char) (255-outLayer->opacity)); |
| 881 | (void) SetImageBackgroundColor(outLayer->image); |
| 882 | |
| 883 | /* set the compositing mode */ |
| 884 | outLayer->image->compose = GIMPBlendModeToCompositeOperator( outLayer->mode ); |
| 885 | if ( outLayer->visible == MagickFalse ) |
| 886 | { |
| 887 | /* BOGUS: should really be separate member var! */ |
| 888 | outLayer->image->compose = NoCompositeOp; |
| 889 | } |
| 890 | |
| 891 | /* read the hierarchy and layer mask offsets */ |
| 892 | hierarchy_offset = ReadBlobMSBLong(image); |
| 893 | layer_mask_offset = ReadBlobMSBLong(image); |
| 894 | |
| 895 | /* read in the hierarchy */ |
| 896 | offset=SeekBlob(image, (MagickOffsetType) hierarchy_offset, SEEK_SET); |
| 897 | if (offset < 0) |
| 898 | (void) ThrowMagickException(&image->exception,GetMagickModule(), |
| 899 | CorruptImageError,"InvalidImageHeader","`%s'",image->filename); |
| 900 | if (load_hierarchy (image, inDocInfo, outLayer) == 0) |
| 901 | return(MagickFalse); |
| 902 | |
| 903 | /* read in the layer mask */ |
| 904 | if (layer_mask_offset != 0) |
| 905 | { |
| 906 | offset=SeekBlob(image, (MagickOffsetType) layer_mask_offset, SEEK_SET); |
| 907 | |
| 908 | #if 0 /* BOGUS: support layer masks! */ |
| 909 | layer_mask = xcf_load_layer_mask (info, gimage); |
| 910 | if (layer_mask == 0) |
| 911 | goto error; |
| 912 | |
| 913 | /* set the offsets of the layer_mask */ |
| 914 | GIMP_DRAWABLE (layer_mask)->offset_x = GIMP_DRAWABLE (layer)->offset_x; |
| 915 | GIMP_DRAWABLE (layer_mask)->offset_y = GIMP_DRAWABLE (layer)->offset_y; |
| 916 | |
| 917 | gimp_layer_add_mask (layer, layer_mask, MagickFalse); |
| 918 | |
| 919 | layer->mask->apply_mask = apply_mask; |
| 920 | layer->mask->edit_mask = edit_mask; |
| 921 | layer->mask->show_mask = show_mask; |
| 922 | #endif |
| 923 | } |
| 924 | |
| 925 | /* attach the floating selection... */ |
| 926 | #if 0 /* BOGUS: we may need to read this, even if we don't support it! */ |
| 927 | if (add_floating_sel) |
| 928 | { |
| 929 | GimpLayer *floating_sel; |
| 930 | |
| 931 | floating_sel = info->floating_sel; |
| 932 | floating_sel_attach (floating_sel, GIMP_DRAWABLE (layer)); |
| 933 | } |
| 934 | #endif |
| 935 | |
| 936 | return MagickTrue; |
| 937 | } |
| 938 | |
| 939 | /* |
| 940 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 941 | % % |
| 942 | % % |
| 943 | % % |
| 944 | % R e a d X C F I m a g e % |
| 945 | % % |
| 946 | % % |
| 947 | % % |
| 948 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 949 | % |
| 950 | % ReadXCFImage() reads a GIMP (GNU Image Manipulation Program) image |
| 951 | % file and returns it. It allocates the memory necessary for the new Image |
| 952 | % structure and returns a pointer to the new image. |
| 953 | % |
| 954 | % The format of the ReadXCFImage method is: |
| 955 | % |
| 956 | % image=ReadXCFImage(image_info) |
| 957 | % |
| 958 | % A description of each parameter follows: |
| 959 | % |
| 960 | % o image_info: the image info. |
| 961 | % |
| 962 | % o exception: return any errors or warnings in this structure. |
| 963 | % |
| 964 | % |
| 965 | */ |
| 966 | static Image *ReadXCFImage(const ImageInfo *image_info,ExceptionInfo *exception) |
| 967 | { |
| 968 | char |
| 969 | magick[14]; |
| 970 | |
| 971 | Image |
| 972 | *image; |
| 973 | |
| 974 | int |
| 975 | foundPropEnd = 0; |
| 976 | |
| 977 | MagickBooleanType |
| 978 | status; |
| 979 | |
| 980 | MagickOffsetType |
| 981 | offset; |
| 982 | |
| 983 | register long |
| 984 | i; |
| 985 | |
| 986 | size_t |
| 987 | length; |
| 988 | |
| 989 | ssize_t |
| 990 | count; |
| 991 | |
| 992 | unsigned long |
| 993 | image_type; |
| 994 | |
| 995 | XCFDocInfo |
| 996 | doc_info; |
| 997 | |
| 998 | /* |
| 999 | Open image file. |
| 1000 | */ |
| 1001 | assert(image_info != (const ImageInfo *) NULL); |
| 1002 | assert(image_info->signature == MagickSignature); |
| 1003 | if (image_info->debug != MagickFalse) |
| 1004 | (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s", |
| 1005 | image_info->filename); |
| 1006 | assert(exception != (ExceptionInfo *) NULL); |
| 1007 | assert(exception->signature == MagickSignature); |
| 1008 | image=AcquireImage(image_info); |
| 1009 | status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception); |
| 1010 | if (status == MagickFalse) |
| 1011 | { |
| 1012 | image=DestroyImageList(image); |
| 1013 | return((Image *) NULL); |
| 1014 | } |
| 1015 | count=ReadBlob(image,14,(unsigned char *) magick); |
| 1016 | if ((count == 0) || |
| 1017 | (LocaleNCompare((char *) magick,"gimp xcf",8) != 0)) |
| 1018 | ThrowReaderException(CorruptImageError,"ImproperImageHeader"); |
| 1019 | (void) ResetMagickMemory(&doc_info,0,sizeof(XCFDocInfo)); |
| 1020 | doc_info.exception=exception; |
| 1021 | doc_info.width=ReadBlobMSBLong(image); |
| 1022 | doc_info.height=ReadBlobMSBLong(image); |
| 1023 | if ((doc_info.width > 262144) || (doc_info.height > 262144)) |
| 1024 | ThrowReaderException(CorruptImageError,"ImproperImageHeader"); |
| 1025 | doc_info.image_type=ReadBlobMSBLong(image); |
| 1026 | /* |
| 1027 | Initialize image attributes. |
| 1028 | */ |
| 1029 | image->columns=doc_info.width; |
| 1030 | image->rows=doc_info.height; |
| 1031 | image_type=doc_info.image_type; |
| 1032 | doc_info.file_size=GetBlobSize(image); |
| 1033 | image->compression=NoCompression; |
| 1034 | image->depth=8; |
| 1035 | if (image_type == GIMP_RGB) |
| 1036 | image->colorspace=RGBColorspace; |
| 1037 | else |
| 1038 | if (image_type == GIMP_GRAY) |
| 1039 | image->colorspace=GRAYColorspace; |
| 1040 | else |
| 1041 | if (image_type == GIMP_INDEXED) |
| 1042 | ThrowReaderException(CoderError,"ColormapTypeNotSupported"); |
| 1043 | (void) SetImageBackgroundColor(image); |
| 1044 | image->matte=MagickTrue; |
| 1045 | /* |
| 1046 | Read properties. |
| 1047 | */ |
| 1048 | while ((foundPropEnd == MagickFalse) && (EOFBlob(image) == MagickFalse)) |
| 1049 | { |
| 1050 | PropType prop_type = (PropType) ReadBlobMSBLong(image); |
| 1051 | unsigned long prop_size = ReadBlobMSBLong(image); |
| 1052 | |
| 1053 | switch (prop_type) |
| 1054 | { |
| 1055 | case PROP_END: |
| 1056 | foundPropEnd=1; |
| 1057 | break; |
| 1058 | case PROP_COLORMAP: |
| 1059 | { |
| 1060 | /* Cannot rely on prop_size here--the value is set incorrectly |
| 1061 | by some Gimp versions. |
| 1062 | */ |
| 1063 | unsigned long num_colours = ReadBlobMSBLong(image); |
| 1064 | for (i=0; i < (long) (3L*num_colours); i++ ) |
| 1065 | (void) ReadBlobByte(image); |
| 1066 | /* |
| 1067 | if (info->file_version == 0) |
| 1068 | { |
| 1069 | gint i; |
| 1070 | |
| 1071 | g_message (_("XCF warning: version 0 of XCF file format\n" |
| 1072 | "did not save indexed colormaps correctly.\n" |
| 1073 | "Substituting grayscale map.")); |
| 1074 | info->cp += |
| 1075 | xcf_read_int32 (info->fp, (guint32*) &gimage->num_cols, 1); |
| 1076 | gimage->cmap = g_new (guchar, gimage->num_cols*3); |
| 1077 | xcf_seek_pos (info, info->cp + gimage->num_cols); |
| 1078 | for (i = 0; i<gimage->num_cols; i++) |
| 1079 | { |
| 1080 | gimage->cmap[i*3+0] = i; |
| 1081 | gimage->cmap[i*3+1] = i; |
| 1082 | gimage->cmap[i*3+2] = i; |
| 1083 | } |
| 1084 | } |
| 1085 | else |
| 1086 | { |
| 1087 | info->cp += |
| 1088 | xcf_read_int32 (info->fp, (guint32*) &gimage->num_cols, 1); |
| 1089 | gimage->cmap = g_new (guchar, gimage->num_cols*3); |
| 1090 | info->cp += |
| 1091 | xcf_read_int8 (info->fp, |
| 1092 | (guint8*) gimage->cmap, gimage->num_cols*3); |
| 1093 | } |
| 1094 | */ |
| 1095 | break; |
| 1096 | } |
| 1097 | case PROP_COMPRESSION: |
| 1098 | { |
| 1099 | doc_info.compression = ReadBlobByte(image); |
| 1100 | if ((doc_info.compression != COMPRESS_NONE) && |
| 1101 | (doc_info.compression != COMPRESS_RLE) && |
| 1102 | (doc_info.compression != COMPRESS_ZLIB) && |
| 1103 | (doc_info.compression != COMPRESS_FRACTAL)) |
| 1104 | ThrowReaderException(CorruptImageError,"UnrecognizedImageCompression"); |
| 1105 | } |
| 1106 | break; |
| 1107 | |
| 1108 | case PROP_GUIDES: |
| 1109 | { |
| 1110 | /* just skip it - we don't care about guides */ |
| 1111 | for (i=0; i < (long) prop_size; i++ ) |
| 1112 | if (ReadBlobByte(image) == EOF) |
| 1113 | ThrowFileException(exception,CorruptImageError, |
| 1114 | "UnexpectedEndOfFile",image->filename); |
| 1115 | } |
| 1116 | break; |
| 1117 | |
| 1118 | case PROP_RESOLUTION: |
| 1119 | { |
| 1120 | /* float xres = (float) */ (void) ReadBlobMSBLong(image); |
| 1121 | /* float yres = (float) */ (void) ReadBlobMSBLong(image); |
| 1122 | |
| 1123 | /* |
| 1124 | if (xres < GIMP_MIN_RESOLUTION || xres > GIMP_MAX_RESOLUTION || |
| 1125 | yres < GIMP_MIN_RESOLUTION || yres > GIMP_MAX_RESOLUTION) |
| 1126 | { |
| 1127 | g_message ("Warning, resolution out of range in XCF file"); |
| 1128 | xres = gimage->gimp->config->default_xresolution; |
| 1129 | yres = gimage->gimp->config->default_yresolution; |
| 1130 | } |
| 1131 | */ |
| 1132 | |
| 1133 | |
| 1134 | /* BOGUS: we don't write these yet because we aren't |
| 1135 | reading them properly yet :( |
| 1136 | image->x_resolution = xres; |
| 1137 | image->y_resolution = yres; |
| 1138 | */ |
| 1139 | } |
| 1140 | break; |
| 1141 | |
| 1142 | case PROP_TATTOO: |
| 1143 | { |
| 1144 | /* we need to read it, even if we ignore it */ |
| 1145 | /*unsigned long tattoo_state = */ (void) ReadBlobMSBLong(image); |
| 1146 | } |
| 1147 | break; |
| 1148 | |
| 1149 | case PROP_PARASITES: |
| 1150 | { |
| 1151 | /* BOGUS: we may need these for IPTC stuff */ |
| 1152 | for (i=0; i < (long) prop_size; i++ ) |
| 1153 | if (ReadBlobByte(image) == EOF) |
| 1154 | ThrowFileException(exception,CorruptImageError, |
| 1155 | "UnexpectedEndOfFile",image->filename); |
| 1156 | |
| 1157 | /* |
| 1158 | glong base = info->cp; |
| 1159 | GimpParasite *p; |
| 1160 | |
| 1161 | while (info->cp - base < prop_size) |
| 1162 | { |
| 1163 | p = xcf_load_parasite (info); |
| 1164 | gimp_image_parasite_attach (gimage, p); |
| 1165 | gimp_parasite_free (p); |
| 1166 | } |
| 1167 | if (info->cp - base != prop_size) |
| 1168 | g_message ("Error detected while loading an image's parasites"); |
| 1169 | */ |
| 1170 | } |
| 1171 | break; |
| 1172 | |
| 1173 | case PROP_UNIT: |
| 1174 | { |
| 1175 | /* BOGUS: ignore for now... */ |
| 1176 | /*unsigned long unit = */ (void) ReadBlobMSBLong(image); |
| 1177 | } |
| 1178 | break; |
| 1179 | |
| 1180 | case PROP_PATHS: |
| 1181 | { |
| 1182 | /* BOGUS: just skip it for now */ |
| 1183 | for (i=0; i< (long) prop_size; i++ ) |
| 1184 | if (ReadBlobByte(image) == EOF) |
| 1185 | ThrowFileException(exception,CorruptImageError, |
| 1186 | "UnexpectedEndOfFile",image->filename); |
| 1187 | |
| 1188 | /* |
| 1189 | PathList *paths = xcf_load_bzpaths (gimage, info); |
| 1190 | gimp_image_set_paths (gimage, paths); |
| 1191 | */ |
| 1192 | } |
| 1193 | break; |
| 1194 | |
| 1195 | case PROP_USER_UNIT: |
| 1196 | { |
| 1197 | char unit_string[1000]; |
| 1198 | /*BOGUS: ignored for now */ |
| 1199 | /*float factor = (float) */ (void) ReadBlobMSBLong(image); |
| 1200 | /* unsigned long digits = */ (void) ReadBlobMSBLong(image); |
| 1201 | for (i=0; i<5; i++) |
| 1202 | (void) ReadBlobStringWithLongSize(image, unit_string, |
| 1203 | sizeof(unit_string)); |
| 1204 | } |
| 1205 | break; |
| 1206 | |
| 1207 | default: |
| 1208 | { |
| 1209 | int buf[16]; |
| 1210 | long amount; |
| 1211 | |
| 1212 | /* read over it... */ |
| 1213 | while ((prop_size > 0) && (EOFBlob(image) == MagickFalse)) |
| 1214 | { |
| 1215 | amount=(long) MagickMin(16, prop_size); |
| 1216 | amount=(long) ReadBlob(image,(size_t) amount,(unsigned char *) &buf); |
| 1217 | if (!amount) |
| 1218 | ThrowReaderException(CorruptImageError,"CorruptImage"); |
| 1219 | prop_size -= (unsigned long) MagickMin(16,(size_t) amount); |
| 1220 | } |
| 1221 | } |
| 1222 | break; |
| 1223 | } |
| 1224 | } |
| 1225 | if (foundPropEnd == MagickFalse) |
| 1226 | ThrowReaderException(CorruptImageError,"ImproperImageHeader"); |
| 1227 | |
| 1228 | if ((image_info->ping != MagickFalse) && (image_info->number_scenes != 0)) |
| 1229 | { |
| 1230 | ; /* do nothing, were just pinging! */ |
| 1231 | } |
| 1232 | else |
| 1233 | { |
| 1234 | int |
| 1235 | current_layer = 0, |
| 1236 | foundAllLayers = MagickFalse, |
| 1237 | number_layers = 0; |
| 1238 | |
| 1239 | MagickOffsetType |
| 1240 | oldPos=TellBlob(image); |
| 1241 | |
| 1242 | XCFLayerInfo |
| 1243 | *layer_info; |
| 1244 | |
| 1245 | /* |
| 1246 | the read pointer |
| 1247 | */ |
| 1248 | do |
| 1249 | { |
| 1250 | long offset = (long) ReadBlobMSBLong(image); |
| 1251 | if (offset == 0) |
| 1252 | foundAllLayers=MagickTrue; |
| 1253 | else |
| 1254 | number_layers++; |
| 1255 | if (EOFBlob(image) != MagickFalse) |
| 1256 | { |
| 1257 | ThrowFileException(exception,CorruptImageError, |
| 1258 | "UnexpectedEndOfFile",image->filename); |
| 1259 | break; |
| 1260 | } |
| 1261 | } while (foundAllLayers == MagickFalse); |
| 1262 | offset=SeekBlob(image,oldPos,SEEK_SET); /* restore the position! */ |
| 1263 | if (offset < 0) |
| 1264 | ThrowReaderException(CorruptImageError,"ImproperImageHeader"); |
| 1265 | /* allocate our array of layer info blocks */ |
| 1266 | length=(size_t) number_layers; |
| 1267 | layer_info=(XCFLayerInfo *) AcquireQuantumMemory(length, |
| 1268 | sizeof(*layer_info)); |
| 1269 | if (layer_info == (XCFLayerInfo *) NULL) |
| 1270 | ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed"); |
| 1271 | (void) ResetMagickMemory(layer_info,0,number_layers*sizeof(XCFLayerInfo)); |
| 1272 | for ( ; ; ) |
| 1273 | { |
| 1274 | MagickBooleanType |
| 1275 | layer_ok; |
| 1276 | |
| 1277 | MagickOffsetType |
| 1278 | offset, |
| 1279 | saved_pos; |
| 1280 | |
| 1281 | /* read in the offset of the next layer */ |
| 1282 | offset=(MagickOffsetType) ReadBlobMSBLong(image); |
| 1283 | /* if the offset is 0 then we are at the end |
| 1284 | * of the layer list. |
| 1285 | */ |
| 1286 | if (offset == 0) |
| 1287 | break; |
| 1288 | /* save the current position as it is where the |
| 1289 | * next layer offset is stored. |
| 1290 | */ |
| 1291 | saved_pos=TellBlob(image); |
| 1292 | /* seek to the layer offset */ |
| 1293 | offset=SeekBlob(image,offset,SEEK_SET); |
| 1294 | /* read in the layer */ |
| 1295 | layer_ok=ReadOneLayer(image,&doc_info,&layer_info[current_layer]); |
| 1296 | if (layer_ok == MagickFalse) |
| 1297 | { |
| 1298 | int j; |
| 1299 | |
| 1300 | for (j=0; j < current_layer; j++) |
| 1301 | layer_info[j].image=DestroyImage(layer_info[j].image); |
| 1302 | ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed"); |
| 1303 | } |
| 1304 | /* restore the saved position so we'll be ready to |
| 1305 | * read the next offset. |
| 1306 | */ |
| 1307 | offset=SeekBlob(image, saved_pos, SEEK_SET); |
| 1308 | current_layer++; |
| 1309 | } |
| 1310 | if (number_layers == 1) |
| 1311 | { |
| 1312 | /* |
| 1313 | Composite the layer data onto the main image, dispose the layer. |
| 1314 | */ |
| 1315 | (void) CompositeImage(image,OverCompositeOp,layer_info[0].image, |
| 1316 | layer_info[0].offset_x,layer_info[0].offset_y); |
| 1317 | layer_info[0].image =DestroyImage( layer_info[0].image); |
| 1318 | } |
| 1319 | else |
| 1320 | { |
| 1321 | #if 0 |
| 1322 | { |
| 1323 | /* NOTE: XCF layers are REVERSED from composite order! */ |
| 1324 | signed int j; |
| 1325 | for (j=number_layers-1; j>=0; j--) { |
| 1326 | /* BOGUS: need to consider layer blending modes!! */ |
| 1327 | |
| 1328 | if ( layer_info[j].visible ) { /* only visible ones, please! */ |
| 1329 | CompositeImage(image, OverCompositeOp, layer_info[j].image, |
| 1330 | layer_info[j].offset_x, layer_info[j].offset_y ); |
| 1331 | layer_info[j].image =DestroyImage( layer_info[j].image ); |
| 1332 | |
| 1333 | /* Bob says that if we do this, we'll get REAL gray images! */ |
| 1334 | if ( image_type == GIMP_GRAY ) { |
| 1335 | QuantizeInfo qi; |
| 1336 | GetQuantizeInfo(&qi); |
| 1337 | qi.colorspace = GRAYColorspace; |
| 1338 | QuantizeImage( &qi, layer_info[j].image ); |
| 1339 | } |
| 1340 | } |
| 1341 | } |
| 1342 | } |
| 1343 | #else |
| 1344 | { |
| 1345 | /* NOTE: XCF layers are REVERSED from composite order! */ |
| 1346 | signed int j; |
| 1347 | |
| 1348 | /* first we copy the last layer on top of the main image */ |
| 1349 | (void) CompositeImage(image,CopyCompositeOp, |
| 1350 | layer_info[number_layers-1].image, |
| 1351 | layer_info[number_layers-1].offset_x, |
| 1352 | layer_info[number_layers-1].offset_y); |
| 1353 | layer_info[number_layers-1].image=DestroyImage( |
| 1354 | layer_info[number_layers-1].image); |
| 1355 | |
| 1356 | /* now reverse the order of the layers as they are put |
| 1357 | into subimages |
| 1358 | */ |
| 1359 | j=number_layers-2; |
| 1360 | image->next=layer_info[j].image; |
| 1361 | layer_info[j].image->previous=image; |
| 1362 | layer_info[j].image->page.x=layer_info[j].offset_x; |
| 1363 | layer_info[j].image->page.y=layer_info[j].offset_y; |
| 1364 | layer_info[j].image->page.width=layer_info[j].width; |
| 1365 | layer_info[j].image->page.height=layer_info[j].height; |
| 1366 | for (j=number_layers-3; j>=0; j--) |
| 1367 | { |
| 1368 | if (j > 0) |
| 1369 | layer_info[j].image->next=layer_info[j-1].image; |
| 1370 | if (j < (number_layers-1)) |
| 1371 | layer_info[j].image->previous=layer_info[j+1].image; |
| 1372 | layer_info[j].image->page.x=layer_info[j].offset_x; |
| 1373 | layer_info[j].image->page.y=layer_info[j].offset_y; |
| 1374 | layer_info[j].image->page.width=layer_info[j].width; |
| 1375 | layer_info[j].image->page.height=layer_info[j].height; |
| 1376 | } |
| 1377 | } |
| 1378 | #endif |
| 1379 | } |
| 1380 | |
| 1381 | layer_info=(XCFLayerInfo *) RelinquishMagickMemory(layer_info); |
| 1382 | |
| 1383 | #if 0 /* BOGUS: do we need the channels?? */ |
| 1384 | while (MagickTrue) |
| 1385 | { |
| 1386 | /* read in the offset of the next channel */ |
| 1387 | info->cp += xcf_read_int32 (info->fp, &offset, 1); |
| 1388 | |
| 1389 | /* if the offset is 0 then we are at the end |
| 1390 | * of the channel list. |
| 1391 | */ |
| 1392 | if (offset == 0) |
| 1393 | break; |
| 1394 | |
| 1395 | /* save the current position as it is where the |
| 1396 | * next channel offset is stored. |
| 1397 | */ |
| 1398 | saved_pos = info->cp; |
| 1399 | |
| 1400 | /* seek to the channel offset */ |
| 1401 | xcf_seek_pos (info, offset); |
| 1402 | |
| 1403 | /* read in the layer */ |
| 1404 | channel = xcf_load_channel (info, gimage); |
| 1405 | if (channel == 0) |
| 1406 | goto error; |
| 1407 | |
| 1408 | num_successful_elements++; |
| 1409 | |
| 1410 | /* add the channel to the image if its not the selection */ |
| 1411 | if (channel != gimage->selection_mask) |
| 1412 | gimp_image_add_channel (gimage, channel, -1); |
| 1413 | |
| 1414 | /* restore the saved position so we'll be ready to |
| 1415 | * read the next offset. |
| 1416 | */ |
| 1417 | xcf_seek_pos (info, saved_pos); |
| 1418 | } |
| 1419 | #endif |
| 1420 | } |
| 1421 | |
| 1422 | (void) CloseBlob(image); |
| 1423 | if (image_type == GIMP_GRAY) |
| 1424 | image->type=GrayscaleType; |
| 1425 | return(GetFirstImageInList(image)); |
| 1426 | } |
| 1427 | |
| 1428 | /* |
| 1429 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 1430 | % % |
| 1431 | % % |
| 1432 | % % |
| 1433 | % R e g i s t e r X C F I m a g e % |
| 1434 | % % |
| 1435 | % % |
| 1436 | % % |
| 1437 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 1438 | % |
| 1439 | % RegisterXCFImage() adds attributes for the XCF image format to |
| 1440 | % the list of supported formats. The attributes include the image format |
| 1441 | % tag, a method to read and/or write the format, whether the format |
| 1442 | % supports the saving of more than one frame to the same file or blob, |
| 1443 | % whether the format supports native in-memory I/O, and a brief |
| 1444 | % description of the format. |
| 1445 | % |
| 1446 | % The format of the RegisterXCFImage method is: |
| 1447 | % |
| 1448 | % unsigned long RegisterXCFImage(void) |
| 1449 | % |
| 1450 | */ |
| 1451 | ModuleExport unsigned long RegisterXCFImage(void) |
| 1452 | { |
| 1453 | MagickInfo |
| 1454 | *entry; |
| 1455 | |
| 1456 | entry=SetMagickInfo("XCF"); |
| 1457 | entry->decoder=(DecodeImageHandler *) ReadXCFImage; |
| 1458 | entry->magick=(IsImageFormatHandler *) IsXCF; |
| 1459 | entry->description=ConstantString("GIMP image"); |
| 1460 | entry->module=ConstantString("XCF"); |
| 1461 | entry->seekable_stream=MagickTrue; |
| 1462 | (void) RegisterMagickInfo(entry); |
| 1463 | return(MagickImageCoderSignature); |
| 1464 | } |
| 1465 | |
| 1466 | /* |
| 1467 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 1468 | % % |
| 1469 | % % |
| 1470 | % % |
| 1471 | % U n r e g i s t e r X C F I m a g e % |
| 1472 | % % |
| 1473 | % % |
| 1474 | % % |
| 1475 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 1476 | % |
| 1477 | % UnregisterXCFImage() removes format registrations made by the |
| 1478 | % XCF module from the list of supported formats. |
| 1479 | % |
| 1480 | % The format of the UnregisterXCFImage method is: |
| 1481 | % |
| 1482 | % UnregisterXCFImage(void) |
| 1483 | % |
| 1484 | */ |
| 1485 | ModuleExport void UnregisterXCFImage(void) |
| 1486 | { |
| 1487 | (void) UnregisterMagickInfo("XCF"); |
| 1488 | } |