blob: 8ebd7106842976b88f2c6b9e9e986cba722915cd [file] [log] [blame]
cristy3ed852e2009-09-05 21:47:34 +00001/*
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% %
cristy16af1cb2009-12-11 21:38:29 +000020% Copyright 1999-2010 ImageMagick Studio LLC, a non-profit organization %
cristy3ed852e2009-09-05 21:47:34 +000021% 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*/
64typedef enum
65{
66 GIMP_RGB,
67 GIMP_GRAY,
68 GIMP_INDEXED
69} GimpImageBaseType;
70
71typedef 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
100typedef enum
101{
102 COMPRESS_NONE = 0,
103 COMPRESS_RLE = 1,
104 COMPRESS_ZLIB = 2, /* unused */
105 COMPRESS_FRACTAL = 3 /* unused */
106} XcfCompressionType;
107
108typedef struct
109{
cristybb503372010-05-27 20:51:26 +0000110 size_t
cristy3ed852e2009-09-05 21:47:34 +0000111 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
126typedef struct
127{
128 char
129 name[1024];
130
131 unsigned int
132 active;
133
cristybb503372010-05-27 20:51:26 +0000134 size_t
cristy3ed852e2009-09-05 21:47:34 +0000135 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
cristybb503372010-05-27 20:51:26 +0000151 size_t
cristy3ed852e2009-09-05 21:47:34 +0000152 mode,
153 tattoo;
154
155 Image
156 *image;
157} XCFLayerInfo;
158
159#define TILE_WIDTH 64
160#define TILE_HEIGHT 64
161
162typedef 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*/
197static 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
206typedef 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*/
233static CompositeOperator GIMPBlendModeToCompositeOperator(
cristybb503372010-05-27 20:51:26 +0000234 size_t blendMode)
cristy3ed852e2009-09-05 21:47:34 +0000235{
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
cristybb503372010-05-27 20:51:26 +0000275% starting with a ssize_t length byte and then characters to that length
cristy3ed852e2009-09-05 21:47:34 +0000276%
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
289static 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
296static char *ReadBlobStringWithLongSize(Image *image,char *string,size_t max)
297{
298 int
299 c;
300
301 MagickOffsetType
302 offset;
303
cristybb503372010-05-27 20:51:26 +0000304 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000305 i;
306
cristybb503372010-05-27 20:51:26 +0000307 size_t
cristy3ed852e2009-09-05 21:47:34 +0000308 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);
cristybb503372010-05-27 20:51:26 +0000316 for (i=0; i < (ssize_t) MagickMin(length,max-1); i++)
cristy3ed852e2009-09-05 21:47:34 +0000317 {
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
331static MagickBooleanType load_tile(Image *image,Image *tile_image,
332 XCFDocInfo *inDocInfo,XCFLayerInfo *inLayerInfo,size_t data_length)
333{
334 ExceptionInfo
335 *exception;
336
cristybb503372010-05-27 20:51:26 +0000337 ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000338 y;
339
cristybb503372010-05-27 20:51:26 +0000340 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000341 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);
cristybb503372010-05-27 20:51:26 +0000367 for (y=0; y < (ssize_t) tile_image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +0000368 {
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 {
cristybb503372010-05-27 20:51:26 +0000374 for (x=0; x < (ssize_t) tile_image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +0000375 {
376 q->red=ScaleCharToQuantum(*graydata);
377 q->green=q->red;
378 q->blue=q->red;
cristyeaedf062010-05-29 22:36:02 +0000379 q->opacity=ScaleCharToQuantum((unsigned char) (255-
380 inLayerInfo->opacity));
cristy3ed852e2009-09-05 21:47:34 +0000381 graydata++;
382 q++;
383 }
384 }
385 else
386 if (inDocInfo->image_type == GIMP_RGB)
387 {
cristybb503372010-05-27 20:51:26 +0000388 for (x=0; x < (ssize_t) tile_image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +0000389 {
390 q->red=ScaleCharToQuantum(xcfdata->red);
391 q->green=ScaleCharToQuantum(xcfdata->green);
392 q->blue=ScaleCharToQuantum(xcfdata->blue);
393 q->opacity=(Quantum) (xcfdata->opacity == 0U ? TransparentOpacity :
cristyeaedf062010-05-29 22:36:02 +0000394 ScaleCharToQuantum((unsigned char) (255-inLayerInfo->opacity)));
cristy3ed852e2009-09-05 21:47:34 +0000395 xcfdata++;
396 q++;
397 }
398 }
399 if (SyncAuthenticPixels(tile_image,exception) == MagickFalse)
400 break;
401 }
402 xcfodata=(XCFPixelPacket *) RelinquishMagickMemory(xcfodata);
403 return MagickTrue;
404}
405
406static MagickBooleanType load_tile_rle(Image *image,Image *tile_image,
407 XCFDocInfo *inDocInfo,XCFLayerInfo *inLayerInfo,size_t data_length)
408{
409 ExceptionInfo
410 *exception;
411
cristybb503372010-05-27 20:51:26 +0000412 ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000413 i,
414 j;
415
416 MagickOffsetType
417 size;
418
419 register PixelPacket
420 *q;
421
422 ssize_t
423 bytes_per_pixel,
424 count;
425
426 size_t
427 length;
428
429 unsigned char
430 data,
431 pixel,
432 *xcfdata,
433 *xcfodata,
434 *xcfdatalimit;
435
436 bytes_per_pixel=(ssize_t) inDocInfo->bytes_per_pixel;
437 xcfdata=(unsigned char *) AcquireQuantumMemory(data_length,sizeof(*xcfdata));
438 if (xcfdata == (unsigned char *) NULL)
439 ThrowBinaryException(ResourceLimitError,"MemoryAllocationFailed",
440 image->filename);
441 xcfodata=xcfdata;
442 count=ReadBlob(image, (size_t) data_length, xcfdata);
443 xcfdatalimit = xcfodata+count-1;
444 exception=(&image->exception);
cristybb503372010-05-27 20:51:26 +0000445 for (i=0; i < (ssize_t) bytes_per_pixel; i++)
cristy3ed852e2009-09-05 21:47:34 +0000446 {
cristy2e8bde02010-06-22 11:03:25 +0000447 q=GetAuthenticPixels(tile_image,0,0,tile_image->columns,tile_image->rows,
448 exception);
cristy3ed852e2009-09-05 21:47:34 +0000449 size=(MagickOffsetType) tile_image->rows*tile_image->columns;
450 while (size > 0)
451 {
452 if (xcfdata > xcfdatalimit)
453 goto bogus_rle;
454 pixel=(*xcfdata++);
455 length=(size_t) pixel;
456 if (length >= 128)
457 {
458 length=255-(length-1);
459 if (length == 128)
460 {
461 if (xcfdata >= xcfdatalimit)
462 goto bogus_rle;
463 length=(size_t) ((*xcfdata << 8) + xcfdata[1]);
464 xcfdata+=2;
465 }
466 size-=length;
467 if (size < 0)
468 goto bogus_rle;
469 if (&xcfdata[length-1] > xcfdatalimit)
470 goto bogus_rle;
471 while (length-- > 0)
472 {
473 data=(*xcfdata++);
474 switch (i)
475 {
476 case 0:
477 {
478 q->red=ScaleCharToQuantum(data);
479 if (inDocInfo->image_type == GIMP_GRAY)
480 {
481 q->green=ScaleCharToQuantum(data);
482 q->blue=ScaleCharToQuantum(data);
cristyeaedf062010-05-29 22:36:02 +0000483 q->opacity=ScaleCharToQuantum((unsigned char) (255-
484 inLayerInfo->opacity));
cristy3ed852e2009-09-05 21:47:34 +0000485 }
486 else
487 {
488 q->green= q->red;
489 q->blue= q->red;
cristyeaedf062010-05-29 22:36:02 +0000490 q->opacity=ScaleCharToQuantum((unsigned char) (255-
491 inLayerInfo->opacity));
cristy3ed852e2009-09-05 21:47:34 +0000492 }
493 break;
494 }
495 case 1:
496 {
497 q->green=ScaleCharToQuantum(data);
498 break;
499 }
500 case 2:
501 {
502 q->blue=ScaleCharToQuantum(data);
503 break;
504 }
505 case 3:
506 {
507 q->opacity=(Quantum) (data == 0 ? TransparentOpacity :
cristyeaedf062010-05-29 22:36:02 +0000508 ScaleCharToQuantum((unsigned char) (255-
509 inLayerInfo->opacity)));
cristy3ed852e2009-09-05 21:47:34 +0000510 break;
511 }
512 }
513 q++;
514 }
515 }
516 else
517 {
518 length+=1;
519 if (length == 128)
520 {
521 if (xcfdata >= xcfdatalimit)
522 goto bogus_rle;
523 length=(size_t) ((*xcfdata << 8) + xcfdata[1]);
524 xcfdata+=2;
525 }
526 size-=length;
527 if (size < 0)
528 goto bogus_rle;
529 if (xcfdata > xcfdatalimit)
530 goto bogus_rle;
531 pixel=(*xcfdata++);
cristybb503372010-05-27 20:51:26 +0000532 for (j= 0; j < (ssize_t) length; j++)
cristy3ed852e2009-09-05 21:47:34 +0000533 {
534 data=pixel;
535 switch (i)
536 {
537 case 0:
538 {
539 q->red=ScaleCharToQuantum(data);
540 if (inDocInfo->image_type == GIMP_GRAY)
541 {
542 q->green=ScaleCharToQuantum(data);
543 q->blue=ScaleCharToQuantum(data);
cristyeaedf062010-05-29 22:36:02 +0000544 q->opacity=ScaleCharToQuantum((unsigned char) (255-
545 inLayerInfo->opacity));
cristy3ed852e2009-09-05 21:47:34 +0000546 }
547 else
548 {
549 q->green=q->red;
550 q->blue=q->red;
cristyeaedf062010-05-29 22:36:02 +0000551 q->opacity=ScaleCharToQuantum((unsigned char) (255-
552 inLayerInfo->opacity));
cristy3ed852e2009-09-05 21:47:34 +0000553 }
554 break;
555 }
556 case 1:
557 {
558 q->green=ScaleCharToQuantum(data);
559 break;
560 }
561 case 2:
562 {
563 q->blue=ScaleCharToQuantum(data);
564 break;
565 }
566 case 3:
567 {
568 q->opacity=(Quantum) (data == 0 ? TransparentOpacity :
cristyeaedf062010-05-29 22:36:02 +0000569 ScaleCharToQuantum((unsigned char) (255-
570 inLayerInfo->opacity)));
cristy3ed852e2009-09-05 21:47:34 +0000571 break;
572 }
573 }
574 q++;
575 }
576 }
577 }
578 if (SyncAuthenticPixels(tile_image,exception) == MagickFalse)
579 break;
580 }
581 xcfodata=(unsigned char *) RelinquishMagickMemory(xcfodata);
582 return(MagickTrue);
583
584 bogus_rle:
585 if (xcfodata != (unsigned char *) NULL)
586 xcfodata=(unsigned char *) RelinquishMagickMemory(xcfodata);
587 return(MagickFalse);
588}
589
590static MagickBooleanType load_level(Image *image,XCFDocInfo *inDocInfo,
591 XCFLayerInfo *inLayerInfo)
592{
593 ExceptionInfo
594 *exception;
595
596 int
597 destLeft = 0,
598 destTop = 0;
599
600 Image*
601 tile_image;
602
603 MagickBooleanType
604 status;
605
606 MagickOffsetType
607 saved_pos,
608 offset,
609 offset2;
610
cristybb503372010-05-27 20:51:26 +0000611 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000612 i;
613
cristybb503372010-05-27 20:51:26 +0000614 size_t
cristy3ed852e2009-09-05 21:47:34 +0000615 width,
616 height,
617 ntiles,
618 ntile_rows,
619 ntile_cols,
620 tile_image_width,
621 tile_image_height;
622
623 /* start reading the data */
624 exception=inDocInfo->exception;
625 width=ReadBlobMSBLong(image);
626 height=ReadBlobMSBLong(image);
627
628 /* read in the first tile offset.
629 * if it is '0', then this tile level is empty
630 * and we can simply return.
631 */
632 offset=(MagickOffsetType) ReadBlobMSBLong(image);
633 if (offset == 0)
634 return(MagickTrue);
635 /* Initialise the reference for the in-memory tile-compression
636 */
637 ntile_rows=(height+TILE_HEIGHT-1)/TILE_HEIGHT;
638 ntile_cols=(width+TILE_WIDTH-1)/TILE_WIDTH;
639 ntiles=ntile_rows*ntile_cols;
cristybb503372010-05-27 20:51:26 +0000640 for (i = 0; i < (ssize_t) ntiles; i++)
cristy3ed852e2009-09-05 21:47:34 +0000641 {
642 status=MagickFalse;
643 if (offset == 0)
644 ThrowBinaryException(CorruptImageError,"NotEnoughTiles",image->filename);
645 /* save the current position as it is where the
646 * next tile offset is stored.
647 */
648 saved_pos=TellBlob(image);
649 /* read in the offset of the next tile so we can calculate the amount
650 of data needed for this tile*/
651 offset2=(MagickOffsetType)ReadBlobMSBLong(image);
652 /* if the offset is 0 then we need to read in the maximum possible
653 allowing for negative compression */
654 if (offset2 == 0)
655 offset2=(MagickOffsetType) (offset + TILE_WIDTH * TILE_WIDTH * 4* 1.5);
656 /* seek to the tile offset */
657 offset=SeekBlob(image, offset, SEEK_SET);
658
659 /* allocate the image for the tile
660 NOTE: the last tile in a row or column may not be a full tile!
661 */
cristybb503372010-05-27 20:51:26 +0000662 tile_image_width=(size_t) (destLeft == (int) ntile_cols-1 ?
cristy3ed852e2009-09-05 21:47:34 +0000663 (int) width % TILE_WIDTH : TILE_WIDTH);
664 if (tile_image_width == 0) tile_image_width=TILE_WIDTH;
cristybb503372010-05-27 20:51:26 +0000665 tile_image_height = (size_t) (destTop == (int) ntile_rows-1 ?
cristy3ed852e2009-09-05 21:47:34 +0000666 (int) height % TILE_HEIGHT : TILE_HEIGHT);
667 if (tile_image_height == 0) tile_image_height=TILE_HEIGHT;
668 tile_image=CloneImage(inLayerInfo->image,tile_image_width,
669 tile_image_height,MagickTrue,exception);
670
671 /* read in the tile */
672 switch (inDocInfo->compression)
673 {
674 case COMPRESS_NONE:
675 if (load_tile(image,tile_image,inDocInfo,inLayerInfo,(size_t) (offset2-offset)) == 0)
676 status=MagickTrue;
677 break;
678 case COMPRESS_RLE:
679 if (load_tile_rle (image,tile_image,inDocInfo,inLayerInfo,
680 (int) (offset2-offset)) == 0)
681 status=MagickTrue;
682 break;
683 case COMPRESS_ZLIB:
684 ThrowBinaryException(CoderError,"ZipCompressNotSupported",
685 image->filename)
686 case COMPRESS_FRACTAL:
687 ThrowBinaryException(CoderError,"FractalCompressNotSupported",
688 image->filename)
689 }
690
691 /* composite the tile onto the layer's image, and then destroy it */
692 (void) CompositeImage(inLayerInfo->image,CopyCompositeOp,tile_image,
693 destLeft * TILE_WIDTH,destTop*TILE_HEIGHT);
694 tile_image=DestroyImage(tile_image);
695
696 /* adjust tile position */
697 destLeft++;
698 if (destLeft >= (int) ntile_cols)
699 {
700 destLeft = 0;
701 destTop++;
702 }
703 if (status != MagickFalse)
704 return(MagickFalse);
705 /* restore the saved position so we'll be ready to
706 * read the next offset.
707 */
708 offset=SeekBlob(image, saved_pos, SEEK_SET);
709 /* read in the offset of the next tile */
710 offset=(MagickOffsetType) ReadBlobMSBLong(image);
711 }
712 if (offset != 0)
713 ThrowBinaryException(CorruptImageError,"CorruptImage",image->filename)
714 return(MagickTrue);
715}
716
717static MagickBooleanType load_hierarchy(Image *image,XCFDocInfo *inDocInfo,
718 XCFLayerInfo *inLayer)
719{
720 MagickOffsetType
721 saved_pos,
722 offset,
723 junk;
724
cristybb503372010-05-27 20:51:26 +0000725 size_t
cristy3ed852e2009-09-05 21:47:34 +0000726 width,
727 height,
728 bytes_per_pixel;
729
730 width=ReadBlobMSBLong(image);
731 height=ReadBlobMSBLong(image);
732 bytes_per_pixel=inDocInfo->bytes_per_pixel=ReadBlobMSBLong(image);
733
734 /* load in the levels...we make sure that the number of levels
735 * calculated when the TileManager was created is the same
736 * as the number of levels found in the file.
737 */
738 offset=(MagickOffsetType) ReadBlobMSBLong(image); /* top level */
739
740 /* discard offsets for layers below first, if any.
741 */
742 do
743 {
744 junk=(MagickOffsetType) ReadBlobMSBLong(image);
745 }
746 while (junk != 0);
747
748 /* save the current position as it is where the
749 * next level offset is stored.
750 */
751 saved_pos=TellBlob(image);
752
753 /* seek to the level offset */
754 offset=SeekBlob(image, offset, SEEK_SET);
755
756 /* read in the level */
757 if (load_level (image, inDocInfo, inLayer) == 0)
758 return(MagickFalse);
759 /* restore the saved position so we'll be ready to
760 * read the next offset.
761 */
762 offset=SeekBlob(image, saved_pos, SEEK_SET);
763 return(MagickTrue);
764}
765
766static MagickBooleanType ReadOneLayer(Image* image,XCFDocInfo* inDocInfo,
767 XCFLayerInfo *outLayer )
768{
cristybb503372010-05-27 20:51:26 +0000769 ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000770 i;
771
772 MagickOffsetType
773 offset;
774
775 unsigned int
776 foundPropEnd = 0;
777
cristybb503372010-05-27 20:51:26 +0000778 size_t
cristy3ed852e2009-09-05 21:47:34 +0000779 hierarchy_offset,
780 layer_mask_offset;
781
782 /* clear the block! */
783 (void) ResetMagickMemory( outLayer, 0, sizeof( XCFLayerInfo ) );
784 /* read in the layer width, height, type and name */
785 outLayer->width = ReadBlobMSBLong(image);
786 outLayer->height = ReadBlobMSBLong(image);
787 outLayer->type = ReadBlobMSBLong(image);
788 (void) ReadBlobStringWithLongSize(image, outLayer->name,
789 sizeof(outLayer->name));
790 /* allocate the image for this layer */
791 outLayer->image=CloneImage(image,outLayer->width, outLayer->height,MagickTrue,
792 &image->exception);
793 if (outLayer->image == (Image *) NULL)
794 return MagickFalse;
795 /* read the layer properties! */
796 foundPropEnd = 0;
797 while ( (foundPropEnd == MagickFalse) && (EOFBlob(image) == MagickFalse) ) {
798 PropType prop_type = (PropType) ReadBlobMSBLong(image);
cristybb503372010-05-27 20:51:26 +0000799 size_t prop_size = ReadBlobMSBLong(image);
cristy3ed852e2009-09-05 21:47:34 +0000800 switch (prop_type)
801 {
802 case PROP_END:
803 foundPropEnd = 1;
804 break;
805 case PROP_ACTIVE_LAYER:
806 outLayer->active = 1;
807 break;
808 case PROP_FLOATING_SELECTION:
809 outLayer->floating_offset = ReadBlobMSBLong(image);
810 break;
811 case PROP_OPACITY:
812 outLayer->opacity = ReadBlobMSBLong(image);
813 break;
814 case PROP_VISIBLE:
815 outLayer->visible = ReadBlobMSBLong(image);
816 break;
817 case PROP_LINKED:
818 outLayer->linked = ReadBlobMSBLong(image);
819 break;
820 case PROP_PRESERVE_TRANSPARENCY:
821 outLayer->preserve_trans = ReadBlobMSBLong(image);
822 break;
823 case PROP_APPLY_MASK:
824 outLayer->apply_mask = ReadBlobMSBLong(image);
825 break;
826 case PROP_EDIT_MASK:
827 outLayer->edit_mask = ReadBlobMSBLong(image);
828 break;
829 case PROP_SHOW_MASK:
830 outLayer->show_mask = ReadBlobMSBLong(image);
831 break;
832 case PROP_OFFSETS:
cristybb503372010-05-27 20:51:26 +0000833 outLayer->offset_x = (ssize_t) ReadBlobMSBLong(image);
834 outLayer->offset_y = (ssize_t) ReadBlobMSBLong(image);
cristy3ed852e2009-09-05 21:47:34 +0000835 break;
836 case PROP_MODE:
837 outLayer->mode = ReadBlobMSBLong(image);
838 break;
839 case PROP_TATTOO:
840 outLayer->preserve_trans = ReadBlobMSBLong(image);
841 break;
842 case PROP_PARASITES:
843 {
cristybb503372010-05-27 20:51:26 +0000844 for (i=0; i < (ssize_t) prop_size; i++ )
cristy3ed852e2009-09-05 21:47:34 +0000845 (void) ReadBlobByte(image);
846
847 /*
cristybb503372010-05-27 20:51:26 +0000848 ssize_t base = info->cp;
cristy3ed852e2009-09-05 21:47:34 +0000849 GimpParasite *p;
850 while (info->cp - base < prop_size)
851 {
852 p = xcf_load_parasite(info);
853 gimp_drawable_parasite_attach(GIMP_DRAWABLE(layer), p);
854 gimp_parasite_free(p);
855 }
856 if (info->cp - base != prop_size)
857 g_message ("Error detected while loading a layer's parasites");
858 */
859 }
860 break;
861 default:
862 /* g_message ("unexpected/unknown layer property: %d (skipping)",
863 prop_type); */
864
865 {
866 int buf[16];
867 ssize_t amount;
868
869 /* read over it... */
870 while ((prop_size > 0) && (EOFBlob(image) == MagickFalse))
871 {
872 amount = (ssize_t) MagickMin(16, prop_size);
873 amount = ReadBlob(image, (size_t) amount, (unsigned char *) &buf);
874 if (!amount)
875 ThrowBinaryException(CorruptImageError,"CorruptImage",
876 image->filename);
cristybb503372010-05-27 20:51:26 +0000877 prop_size -= (size_t) MagickMin(16, (size_t) amount);
cristy3ed852e2009-09-05 21:47:34 +0000878 }
879 }
880 break;
881 }
882 }
883
884 if (foundPropEnd == MagickFalse)
885 return(MagickFalse);
886 /* clear the image based on the layer opacity */
887 outLayer->image->background_color.opacity=
888 ScaleCharToQuantum((unsigned char) (255-outLayer->opacity));
889 (void) SetImageBackgroundColor(outLayer->image);
890
891 /* set the compositing mode */
892 outLayer->image->compose = GIMPBlendModeToCompositeOperator( outLayer->mode );
893 if ( outLayer->visible == MagickFalse )
894 {
895 /* BOGUS: should really be separate member var! */
896 outLayer->image->compose = NoCompositeOp;
897 }
898
899 /* read the hierarchy and layer mask offsets */
900 hierarchy_offset = ReadBlobMSBLong(image);
901 layer_mask_offset = ReadBlobMSBLong(image);
902
903 /* read in the hierarchy */
904 offset=SeekBlob(image, (MagickOffsetType) hierarchy_offset, SEEK_SET);
905 if (offset < 0)
906 (void) ThrowMagickException(&image->exception,GetMagickModule(),
907 CorruptImageError,"InvalidImageHeader","`%s'",image->filename);
908 if (load_hierarchy (image, inDocInfo, outLayer) == 0)
909 return(MagickFalse);
910
911 /* read in the layer mask */
912 if (layer_mask_offset != 0)
913 {
914 offset=SeekBlob(image, (MagickOffsetType) layer_mask_offset, SEEK_SET);
915
916#if 0 /* BOGUS: support layer masks! */
917 layer_mask = xcf_load_layer_mask (info, gimage);
918 if (layer_mask == 0)
919 goto error;
920
921 /* set the offsets of the layer_mask */
922 GIMP_DRAWABLE (layer_mask)->offset_x = GIMP_DRAWABLE (layer)->offset_x;
923 GIMP_DRAWABLE (layer_mask)->offset_y = GIMP_DRAWABLE (layer)->offset_y;
924
925 gimp_layer_add_mask (layer, layer_mask, MagickFalse);
926
927 layer->mask->apply_mask = apply_mask;
928 layer->mask->edit_mask = edit_mask;
929 layer->mask->show_mask = show_mask;
930#endif
931 }
932
933 /* attach the floating selection... */
934#if 0 /* BOGUS: we may need to read this, even if we don't support it! */
935 if (add_floating_sel)
936 {
937 GimpLayer *floating_sel;
938
939 floating_sel = info->floating_sel;
940 floating_sel_attach (floating_sel, GIMP_DRAWABLE (layer));
941 }
942#endif
943
944 return MagickTrue;
945}
946
947/*
948%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
949% %
950% %
951% %
952% R e a d X C F I m a g e %
953% %
954% %
955% %
956%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
957%
958% ReadXCFImage() reads a GIMP (GNU Image Manipulation Program) image
959% file and returns it. It allocates the memory necessary for the new Image
960% structure and returns a pointer to the new image.
961%
962% The format of the ReadXCFImage method is:
963%
964% image=ReadXCFImage(image_info)
965%
966% A description of each parameter follows:
967%
968% o image_info: the image info.
969%
970% o exception: return any errors or warnings in this structure.
971%
972%
973*/
974static Image *ReadXCFImage(const ImageInfo *image_info,ExceptionInfo *exception)
975{
976 char
977 magick[14];
978
979 Image
980 *image;
981
982 int
983 foundPropEnd = 0;
984
985 MagickBooleanType
986 status;
987
988 MagickOffsetType
989 offset;
990
cristybb503372010-05-27 20:51:26 +0000991 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000992 i;
993
994 size_t
995 length;
996
997 ssize_t
998 count;
999
cristybb503372010-05-27 20:51:26 +00001000 size_t
cristy3ed852e2009-09-05 21:47:34 +00001001 image_type;
1002
1003 XCFDocInfo
1004 doc_info;
1005
1006 /*
1007 Open image file.
1008 */
1009 assert(image_info != (const ImageInfo *) NULL);
1010 assert(image_info->signature == MagickSignature);
1011 if (image_info->debug != MagickFalse)
1012 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
1013 image_info->filename);
1014 assert(exception != (ExceptionInfo *) NULL);
1015 assert(exception->signature == MagickSignature);
1016 image=AcquireImage(image_info);
1017 status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception);
1018 if (status == MagickFalse)
1019 {
1020 image=DestroyImageList(image);
1021 return((Image *) NULL);
1022 }
1023 count=ReadBlob(image,14,(unsigned char *) magick);
1024 if ((count == 0) ||
1025 (LocaleNCompare((char *) magick,"gimp xcf",8) != 0))
1026 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
1027 (void) ResetMagickMemory(&doc_info,0,sizeof(XCFDocInfo));
1028 doc_info.exception=exception;
1029 doc_info.width=ReadBlobMSBLong(image);
1030 doc_info.height=ReadBlobMSBLong(image);
1031 if ((doc_info.width > 262144) || (doc_info.height > 262144))
1032 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
1033 doc_info.image_type=ReadBlobMSBLong(image);
1034 /*
1035 Initialize image attributes.
1036 */
1037 image->columns=doc_info.width;
1038 image->rows=doc_info.height;
1039 image_type=doc_info.image_type;
1040 doc_info.file_size=GetBlobSize(image);
1041 image->compression=NoCompression;
1042 image->depth=8;
1043 if (image_type == GIMP_RGB)
1044 image->colorspace=RGBColorspace;
1045 else
1046 if (image_type == GIMP_GRAY)
1047 image->colorspace=GRAYColorspace;
1048 else
1049 if (image_type == GIMP_INDEXED)
1050 ThrowReaderException(CoderError,"ColormapTypeNotSupported");
1051 (void) SetImageBackgroundColor(image);
1052 image->matte=MagickTrue;
1053 /*
1054 Read properties.
1055 */
1056 while ((foundPropEnd == MagickFalse) && (EOFBlob(image) == MagickFalse))
1057 {
1058 PropType prop_type = (PropType) ReadBlobMSBLong(image);
cristybb503372010-05-27 20:51:26 +00001059 size_t prop_size = ReadBlobMSBLong(image);
cristy3ed852e2009-09-05 21:47:34 +00001060
1061 switch (prop_type)
1062 {
1063 case PROP_END:
1064 foundPropEnd=1;
1065 break;
1066 case PROP_COLORMAP:
1067 {
1068 /* Cannot rely on prop_size here--the value is set incorrectly
1069 by some Gimp versions.
1070 */
cristybb503372010-05-27 20:51:26 +00001071 size_t num_colours = ReadBlobMSBLong(image);
1072 for (i=0; i < (ssize_t) (3L*num_colours); i++ )
cristy3ed852e2009-09-05 21:47:34 +00001073 (void) ReadBlobByte(image);
1074 /*
1075 if (info->file_version == 0)
1076 {
1077 gint i;
1078
1079 g_message (_("XCF warning: version 0 of XCF file format\n"
1080 "did not save indexed colormaps correctly.\n"
1081 "Substituting grayscale map."));
1082 info->cp +=
1083 xcf_read_int32 (info->fp, (guint32*) &gimage->num_cols, 1);
1084 gimage->cmap = g_new (guchar, gimage->num_cols*3);
1085 xcf_seek_pos (info, info->cp + gimage->num_cols);
1086 for (i = 0; i<gimage->num_cols; i++)
1087 {
1088 gimage->cmap[i*3+0] = i;
1089 gimage->cmap[i*3+1] = i;
1090 gimage->cmap[i*3+2] = i;
1091 }
1092 }
1093 else
1094 {
1095 info->cp +=
1096 xcf_read_int32 (info->fp, (guint32*) &gimage->num_cols, 1);
1097 gimage->cmap = g_new (guchar, gimage->num_cols*3);
1098 info->cp +=
1099 xcf_read_int8 (info->fp,
1100 (guint8*) gimage->cmap, gimage->num_cols*3);
1101 }
1102 */
1103 break;
1104 }
1105 case PROP_COMPRESSION:
1106 {
1107 doc_info.compression = ReadBlobByte(image);
1108 if ((doc_info.compression != COMPRESS_NONE) &&
1109 (doc_info.compression != COMPRESS_RLE) &&
1110 (doc_info.compression != COMPRESS_ZLIB) &&
1111 (doc_info.compression != COMPRESS_FRACTAL))
1112 ThrowReaderException(CorruptImageError,"UnrecognizedImageCompression");
1113 }
1114 break;
1115
1116 case PROP_GUIDES:
1117 {
1118 /* just skip it - we don't care about guides */
cristybb503372010-05-27 20:51:26 +00001119 for (i=0; i < (ssize_t) prop_size; i++ )
cristy3ed852e2009-09-05 21:47:34 +00001120 if (ReadBlobByte(image) == EOF)
1121 ThrowFileException(exception,CorruptImageError,
1122 "UnexpectedEndOfFile",image->filename);
1123 }
1124 break;
1125
1126 case PROP_RESOLUTION:
1127 {
1128 /* float xres = (float) */ (void) ReadBlobMSBLong(image);
1129 /* float yres = (float) */ (void) ReadBlobMSBLong(image);
1130
1131 /*
1132 if (xres < GIMP_MIN_RESOLUTION || xres > GIMP_MAX_RESOLUTION ||
1133 yres < GIMP_MIN_RESOLUTION || yres > GIMP_MAX_RESOLUTION)
1134 {
1135 g_message ("Warning, resolution out of range in XCF file");
1136 xres = gimage->gimp->config->default_xresolution;
1137 yres = gimage->gimp->config->default_yresolution;
1138 }
1139 */
1140
1141
1142 /* BOGUS: we don't write these yet because we aren't
1143 reading them properly yet :(
1144 image->x_resolution = xres;
1145 image->y_resolution = yres;
1146 */
1147 }
1148 break;
1149
1150 case PROP_TATTOO:
1151 {
1152 /* we need to read it, even if we ignore it */
cristybb503372010-05-27 20:51:26 +00001153 /*size_t tattoo_state = */ (void) ReadBlobMSBLong(image);
cristy3ed852e2009-09-05 21:47:34 +00001154 }
1155 break;
1156
1157 case PROP_PARASITES:
1158 {
1159 /* BOGUS: we may need these for IPTC stuff */
cristybb503372010-05-27 20:51:26 +00001160 for (i=0; i < (ssize_t) prop_size; i++ )
cristy3ed852e2009-09-05 21:47:34 +00001161 if (ReadBlobByte(image) == EOF)
1162 ThrowFileException(exception,CorruptImageError,
1163 "UnexpectedEndOfFile",image->filename);
1164
1165 /*
cristybb503372010-05-27 20:51:26 +00001166 gssize_t base = info->cp;
cristy3ed852e2009-09-05 21:47:34 +00001167 GimpParasite *p;
1168
1169 while (info->cp - base < prop_size)
1170 {
1171 p = xcf_load_parasite (info);
1172 gimp_image_parasite_attach (gimage, p);
1173 gimp_parasite_free (p);
1174 }
1175 if (info->cp - base != prop_size)
1176 g_message ("Error detected while loading an image's parasites");
1177 */
1178 }
1179 break;
1180
1181 case PROP_UNIT:
1182 {
1183 /* BOGUS: ignore for now... */
cristybb503372010-05-27 20:51:26 +00001184 /*size_t unit = */ (void) ReadBlobMSBLong(image);
cristy3ed852e2009-09-05 21:47:34 +00001185 }
1186 break;
1187
1188 case PROP_PATHS:
1189 {
1190 /* BOGUS: just skip it for now */
cristybb503372010-05-27 20:51:26 +00001191 for (i=0; i< (ssize_t) prop_size; i++ )
cristy3ed852e2009-09-05 21:47:34 +00001192 if (ReadBlobByte(image) == EOF)
1193 ThrowFileException(exception,CorruptImageError,
1194 "UnexpectedEndOfFile",image->filename);
1195
1196 /*
1197 PathList *paths = xcf_load_bzpaths (gimage, info);
1198 gimp_image_set_paths (gimage, paths);
1199 */
1200 }
1201 break;
1202
1203 case PROP_USER_UNIT:
1204 {
1205 char unit_string[1000];
1206 /*BOGUS: ignored for now */
1207 /*float factor = (float) */ (void) ReadBlobMSBLong(image);
cristybb503372010-05-27 20:51:26 +00001208 /* size_t digits = */ (void) ReadBlobMSBLong(image);
cristy3ed852e2009-09-05 21:47:34 +00001209 for (i=0; i<5; i++)
1210 (void) ReadBlobStringWithLongSize(image, unit_string,
1211 sizeof(unit_string));
1212 }
1213 break;
1214
1215 default:
1216 {
1217 int buf[16];
cristybb503372010-05-27 20:51:26 +00001218 ssize_t amount;
cristy3ed852e2009-09-05 21:47:34 +00001219
1220 /* read over it... */
1221 while ((prop_size > 0) && (EOFBlob(image) == MagickFalse))
1222 {
cristybb503372010-05-27 20:51:26 +00001223 amount=(ssize_t) MagickMin(16, prop_size);
1224 amount=(ssize_t) ReadBlob(image,(size_t) amount,(unsigned char *) &buf);
cristy3ed852e2009-09-05 21:47:34 +00001225 if (!amount)
1226 ThrowReaderException(CorruptImageError,"CorruptImage");
cristybb503372010-05-27 20:51:26 +00001227 prop_size -= (size_t) MagickMin(16,(size_t) amount);
cristy3ed852e2009-09-05 21:47:34 +00001228 }
1229 }
1230 break;
1231 }
1232 }
1233 if (foundPropEnd == MagickFalse)
1234 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
1235
1236 if ((image_info->ping != MagickFalse) && (image_info->number_scenes != 0))
1237 {
1238 ; /* do nothing, were just pinging! */
1239 }
1240 else
1241 {
1242 int
1243 current_layer = 0,
1244 foundAllLayers = MagickFalse,
1245 number_layers = 0;
1246
1247 MagickOffsetType
1248 oldPos=TellBlob(image);
1249
1250 XCFLayerInfo
1251 *layer_info;
1252
1253 /*
1254 the read pointer
1255 */
1256 do
1257 {
cristybb503372010-05-27 20:51:26 +00001258 ssize_t offset = (ssize_t) ReadBlobMSBLong(image);
cristy3ed852e2009-09-05 21:47:34 +00001259 if (offset == 0)
1260 foundAllLayers=MagickTrue;
1261 else
1262 number_layers++;
1263 if (EOFBlob(image) != MagickFalse)
1264 {
1265 ThrowFileException(exception,CorruptImageError,
1266 "UnexpectedEndOfFile",image->filename);
1267 break;
1268 }
1269 } while (foundAllLayers == MagickFalse);
1270 offset=SeekBlob(image,oldPos,SEEK_SET); /* restore the position! */
1271 if (offset < 0)
1272 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
1273 /* allocate our array of layer info blocks */
1274 length=(size_t) number_layers;
1275 layer_info=(XCFLayerInfo *) AcquireQuantumMemory(length,
1276 sizeof(*layer_info));
1277 if (layer_info == (XCFLayerInfo *) NULL)
1278 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
1279 (void) ResetMagickMemory(layer_info,0,number_layers*sizeof(XCFLayerInfo));
1280 for ( ; ; )
1281 {
1282 MagickBooleanType
1283 layer_ok;
1284
1285 MagickOffsetType
1286 offset,
1287 saved_pos;
1288
1289 /* read in the offset of the next layer */
1290 offset=(MagickOffsetType) ReadBlobMSBLong(image);
1291 /* if the offset is 0 then we are at the end
1292 * of the layer list.
1293 */
1294 if (offset == 0)
1295 break;
1296 /* save the current position as it is where the
1297 * next layer offset is stored.
1298 */
1299 saved_pos=TellBlob(image);
1300 /* seek to the layer offset */
1301 offset=SeekBlob(image,offset,SEEK_SET);
1302 /* read in the layer */
1303 layer_ok=ReadOneLayer(image,&doc_info,&layer_info[current_layer]);
1304 if (layer_ok == MagickFalse)
1305 {
1306 int j;
1307
1308 for (j=0; j < current_layer; j++)
1309 layer_info[j].image=DestroyImage(layer_info[j].image);
1310 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
1311 }
1312 /* restore the saved position so we'll be ready to
1313 * read the next offset.
1314 */
1315 offset=SeekBlob(image, saved_pos, SEEK_SET);
1316 current_layer++;
1317 }
1318 if (number_layers == 1)
1319 {
1320 /*
1321 Composite the layer data onto the main image, dispose the layer.
1322 */
1323 (void) CompositeImage(image,OverCompositeOp,layer_info[0].image,
1324 layer_info[0].offset_x,layer_info[0].offset_y);
1325 layer_info[0].image =DestroyImage( layer_info[0].image);
1326 }
1327 else
1328 {
1329#if 0
1330 {
1331 /* NOTE: XCF layers are REVERSED from composite order! */
1332 signed int j;
1333 for (j=number_layers-1; j>=0; j--) {
1334 /* BOGUS: need to consider layer blending modes!! */
1335
1336 if ( layer_info[j].visible ) { /* only visible ones, please! */
1337 CompositeImage(image, OverCompositeOp, layer_info[j].image,
1338 layer_info[j].offset_x, layer_info[j].offset_y );
1339 layer_info[j].image =DestroyImage( layer_info[j].image );
1340
1341 /* Bob says that if we do this, we'll get REAL gray images! */
1342 if ( image_type == GIMP_GRAY ) {
1343 QuantizeInfo qi;
1344 GetQuantizeInfo(&qi);
1345 qi.colorspace = GRAYColorspace;
1346 QuantizeImage( &qi, layer_info[j].image );
1347 }
1348 }
1349 }
1350 }
1351#else
1352 {
1353 /* NOTE: XCF layers are REVERSED from composite order! */
1354 signed int j;
1355
1356 /* first we copy the last layer on top of the main image */
1357 (void) CompositeImage(image,CopyCompositeOp,
1358 layer_info[number_layers-1].image,
1359 layer_info[number_layers-1].offset_x,
1360 layer_info[number_layers-1].offset_y);
1361 layer_info[number_layers-1].image=DestroyImage(
1362 layer_info[number_layers-1].image);
1363
1364 /* now reverse the order of the layers as they are put
1365 into subimages
1366 */
1367 j=number_layers-2;
1368 image->next=layer_info[j].image;
1369 layer_info[j].image->previous=image;
1370 layer_info[j].image->page.x=layer_info[j].offset_x;
1371 layer_info[j].image->page.y=layer_info[j].offset_y;
1372 layer_info[j].image->page.width=layer_info[j].width;
1373 layer_info[j].image->page.height=layer_info[j].height;
1374 for (j=number_layers-3; j>=0; j--)
1375 {
1376 if (j > 0)
1377 layer_info[j].image->next=layer_info[j-1].image;
1378 if (j < (number_layers-1))
1379 layer_info[j].image->previous=layer_info[j+1].image;
1380 layer_info[j].image->page.x=layer_info[j].offset_x;
1381 layer_info[j].image->page.y=layer_info[j].offset_y;
1382 layer_info[j].image->page.width=layer_info[j].width;
1383 layer_info[j].image->page.height=layer_info[j].height;
1384 }
1385 }
1386#endif
1387 }
1388
1389 layer_info=(XCFLayerInfo *) RelinquishMagickMemory(layer_info);
1390
1391#if 0 /* BOGUS: do we need the channels?? */
1392 while (MagickTrue)
1393 {
1394 /* read in the offset of the next channel */
1395 info->cp += xcf_read_int32 (info->fp, &offset, 1);
1396
1397 /* if the offset is 0 then we are at the end
1398 * of the channel list.
1399 */
1400 if (offset == 0)
1401 break;
1402
1403 /* save the current position as it is where the
1404 * next channel offset is stored.
1405 */
1406 saved_pos = info->cp;
1407
1408 /* seek to the channel offset */
1409 xcf_seek_pos (info, offset);
1410
1411 /* read in the layer */
1412 channel = xcf_load_channel (info, gimage);
1413 if (channel == 0)
1414 goto error;
1415
1416 num_successful_elements++;
1417
1418 /* add the channel to the image if its not the selection */
1419 if (channel != gimage->selection_mask)
1420 gimp_image_add_channel (gimage, channel, -1);
1421
1422 /* restore the saved position so we'll be ready to
1423 * read the next offset.
1424 */
1425 xcf_seek_pos (info, saved_pos);
1426 }
1427#endif
1428 }
1429
1430 (void) CloseBlob(image);
1431 if (image_type == GIMP_GRAY)
1432 image->type=GrayscaleType;
1433 return(GetFirstImageInList(image));
1434}
1435
1436/*
1437%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1438% %
1439% %
1440% %
1441% R e g i s t e r X C F I m a g e %
1442% %
1443% %
1444% %
1445%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1446%
1447% RegisterXCFImage() adds attributes for the XCF image format to
1448% the list of supported formats. The attributes include the image format
1449% tag, a method to read and/or write the format, whether the format
1450% supports the saving of more than one frame to the same file or blob,
1451% whether the format supports native in-memory I/O, and a brief
1452% description of the format.
1453%
1454% The format of the RegisterXCFImage method is:
1455%
cristybb503372010-05-27 20:51:26 +00001456% size_t RegisterXCFImage(void)
cristy3ed852e2009-09-05 21:47:34 +00001457%
1458*/
cristybb503372010-05-27 20:51:26 +00001459ModuleExport size_t RegisterXCFImage(void)
cristy3ed852e2009-09-05 21:47:34 +00001460{
1461 MagickInfo
1462 *entry;
1463
1464 entry=SetMagickInfo("XCF");
1465 entry->decoder=(DecodeImageHandler *) ReadXCFImage;
1466 entry->magick=(IsImageFormatHandler *) IsXCF;
1467 entry->description=ConstantString("GIMP image");
1468 entry->module=ConstantString("XCF");
1469 entry->seekable_stream=MagickTrue;
1470 (void) RegisterMagickInfo(entry);
1471 return(MagickImageCoderSignature);
1472}
1473
1474/*
1475%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1476% %
1477% %
1478% %
1479% U n r e g i s t e r X C F I m a g e %
1480% %
1481% %
1482% %
1483%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1484%
1485% UnregisterXCFImage() removes format registrations made by the
1486% XCF module from the list of supported formats.
1487%
1488% The format of the UnregisterXCFImage method is:
1489%
1490% UnregisterXCFImage(void)
1491%
1492*/
1493ModuleExport void UnregisterXCFImage(void)
1494{
1495 (void) UnregisterMagickInfo("XCF");
1496}