blob: 2a34e9afc8920156bade15c1419e75ec3a200bad [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% %
Cristy7ce65e72015-12-12 18:03:16 -050020% Copyright 1999-2016 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*/
cristy4c08aed2011-07-01 19:47:50 +000042#include "MagickCore/studio.h"
43#include "MagickCore/blob.h"
44#include "MagickCore/blob-private.h"
45#include "MagickCore/cache.h"
46#include "MagickCore/color.h"
47#include "MagickCore/composite.h"
48#include "MagickCore/exception.h"
49#include "MagickCore/exception-private.h"
50#include "MagickCore/image.h"
51#include "MagickCore/image-private.h"
52#include "MagickCore/list.h"
53#include "MagickCore/magick.h"
54#include "MagickCore/memory_.h"
55#include "MagickCore/pixel.h"
56#include "MagickCore/pixel-accessor.h"
57#include "MagickCore/quantize.h"
58#include "MagickCore/quantum-private.h"
59#include "MagickCore/static.h"
60#include "MagickCore/string_.h"
61#include "MagickCore/module.h"
cristy3ed852e2009-09-05 21:47:34 +000062
63/*
64 Typedef declarations.
65*/
66typedef enum
67{
68 GIMP_RGB,
69 GIMP_GRAY,
70 GIMP_INDEXED
71} GimpImageBaseType;
72
73typedef enum
74{
75 PROP_END = 0,
76 PROP_COLORMAP = 1,
77 PROP_ACTIVE_LAYER = 2,
78 PROP_ACTIVE_CHANNEL = 3,
79 PROP_SELECTION = 4,
80 PROP_FLOATING_SELECTION = 5,
81 PROP_OPACITY = 6,
82 PROP_MODE = 7,
83 PROP_VISIBLE = 8,
84 PROP_LINKED = 9,
85 PROP_PRESERVE_TRANSPARENCY = 10,
86 PROP_APPLY_MASK = 11,
87 PROP_EDIT_MASK = 12,
88 PROP_SHOW_MASK = 13,
89 PROP_SHOW_MASKED = 14,
90 PROP_OFFSETS = 15,
91 PROP_COLOR = 16,
92 PROP_COMPRESSION = 17,
93 PROP_GUIDES = 18,
94 PROP_RESOLUTION = 19,
95 PROP_TATTOO = 20,
96 PROP_PARASITES = 21,
97 PROP_UNIT = 22,
98 PROP_PATHS = 23,
99 PROP_USER_UNIT = 24
100} PropType;
101
102typedef enum
103{
104 COMPRESS_NONE = 0,
105 COMPRESS_RLE = 1,
106 COMPRESS_ZLIB = 2, /* unused */
107 COMPRESS_FRACTAL = 3 /* unused */
108} XcfCompressionType;
109
110typedef struct
111{
cristybb503372010-05-27 20:51:26 +0000112 size_t
cristy3ed852e2009-09-05 21:47:34 +0000113 width,
114 height,
115 image_type,
116 bytes_per_pixel;
117
118 int
119 compression;
120
121 size_t
122 file_size;
123
cristy2cbb3b32012-04-01 19:11:10 +0000124 size_t
125 number_layers;
cristy3ed852e2009-09-05 21:47:34 +0000126} XCFDocInfo;
127
128typedef struct
129{
130 char
131 name[1024];
132
133 unsigned int
134 active;
135
cristybb503372010-05-27 20:51:26 +0000136 size_t
cristy3ed852e2009-09-05 21:47:34 +0000137 width,
138 height,
139 type,
cristy4c08aed2011-07-01 19:47:50 +0000140 alpha,
cristy3ed852e2009-09-05 21:47:34 +0000141 visible,
142 linked,
143 preserve_trans,
144 apply_mask,
145 show_mask,
146 edit_mask,
147 floating_offset;
148
149 ssize_t
150 offset_x,
151 offset_y;
152
cristybb503372010-05-27 20:51:26 +0000153 size_t
cristy3ed852e2009-09-05 21:47:34 +0000154 mode,
155 tattoo;
156
157 Image
158 *image;
159} XCFLayerInfo;
160
161#define TILE_WIDTH 64
162#define TILE_HEIGHT 64
163
164typedef struct
165{
166 unsigned char
167 red,
168 green,
169 blue,
cristy4c08aed2011-07-01 19:47:50 +0000170 alpha;
cristy101ab702011-10-13 13:06:32 +0000171} XCFPixelInfo;
cristy3ed852e2009-09-05 21:47:34 +0000172
173/*
174%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
175% %
176% %
177% %
178% I s X C F %
179% %
180% %
181% %
182%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
183%
184% IsXCF() returns MagickTrue if the image format type, identified by the
185% magick string, is XCF (GIMP native format).
186%
187% The format of the IsXCF method is:
188%
189% MagickBooleanType IsXCF(const unsigned char *magick,const size_t length)
190%
191% A description of each parameter follows:
192%
193% o magick: compare image format pattern against these bytes.
194%
195% o length: Specifies the length of the magick string.
196%
197%
198*/
199static MagickBooleanType IsXCF(const unsigned char *magick,const size_t length)
200{
201 if (length < 8)
202 return(MagickFalse);
203 if (LocaleNCompare((char *) magick,"gimp xcf",8) == 0)
204 return(MagickTrue);
205 return(MagickFalse);
206}
207
208typedef enum
209{
210 GIMP_NORMAL_MODE,
211 GIMP_DISSOLVE_MODE,
212 GIMP_BEHIND_MODE,
213 GIMP_MULTIPLY_MODE,
214 GIMP_SCREEN_MODE,
215 GIMP_OVERLAY_MODE,
216 GIMP_DIFFERENCE_MODE,
217 GIMP_ADDITION_MODE,
218 GIMP_SUBTRACT_MODE,
219 GIMP_DARKEN_ONLY_MODE,
220 GIMP_LIGHTEN_ONLY_MODE,
221 GIMP_HUE_MODE,
222 GIMP_SATURATION_MODE,
223 GIMP_COLOR_MODE,
224 GIMP_VALUE_MODE,
225 GIMP_DIVIDE_MODE,
226 GIMP_DODGE_MODE,
227 GIMP_BURN_MODE,
228 GIMP_HARDLIGHT_MODE
229} GimpLayerModeEffects;
230
231/*
232 Simple utility routine to convert between PSD blending modes and
233 ImageMagick compositing operators
234*/
235static CompositeOperator GIMPBlendModeToCompositeOperator(
cristybb503372010-05-27 20:51:26 +0000236 size_t blendMode)
cristy3ed852e2009-09-05 21:47:34 +0000237{
238 switch ( blendMode )
239 {
cristye47116d2014-04-12 23:23:02 +0000240 case GIMP_NORMAL_MODE: return(OverCompositeOp);
241 case GIMP_DISSOLVE_MODE: return(DissolveCompositeOp);
242 case GIMP_MULTIPLY_MODE: return(MultiplyCompositeOp);
243 case GIMP_SCREEN_MODE: return(ScreenCompositeOp);
244 case GIMP_OVERLAY_MODE: return(OverlayCompositeOp);
245 case GIMP_DIFFERENCE_MODE: return(DifferenceCompositeOp);
246 case GIMP_ADDITION_MODE: return(ModulusAddCompositeOp);
247 case GIMP_SUBTRACT_MODE: return(ModulusSubtractCompositeOp);
248 case GIMP_DARKEN_ONLY_MODE: return(DarkenCompositeOp);
249 case GIMP_LIGHTEN_ONLY_MODE: return(LightenCompositeOp);
250 case GIMP_HUE_MODE: return(HueCompositeOp);
251 case GIMP_SATURATION_MODE: return(SaturateCompositeOp);
252 case GIMP_COLOR_MODE: return(ColorizeCompositeOp);
253 case GIMP_DODGE_MODE: return(ColorDodgeCompositeOp);
254 case GIMP_BURN_MODE: return(ColorBurnCompositeOp);
255 case GIMP_HARDLIGHT_MODE: return(HardLightCompositeOp);
256 case GIMP_DIVIDE_MODE: return(DivideDstCompositeOp);
cristy3ed852e2009-09-05 21:47:34 +0000257 /* these are the ones we don't support...yet */
cristye47116d2014-04-12 23:23:02 +0000258 case GIMP_BEHIND_MODE: return(OverCompositeOp);
259 case GIMP_VALUE_MODE: return(OverCompositeOp);
260 default: return(OverCompositeOp);
cristy3ed852e2009-09-05 21:47:34 +0000261 }
262}
263
264/*
265%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
266% %
267% %
268% %
269+ 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 %
270% %
271% %
272% %
273%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
274%
275% ReadBlobStringWithLongSize reads characters from a blob or file
cristybb503372010-05-27 20:51:26 +0000276% starting with a ssize_t length byte and then characters to that length
cristy3ed852e2009-09-05 21:47:34 +0000277%
278% The format of the ReadBlobStringWithLongSize method is:
279%
280% char *ReadBlobStringWithLongSize(Image *image,char *string)
281%
282% A description of each parameter follows:
283%
284% o image: the image.
285%
286% o string: the address of a character buffer.
287%
288*/
289
cristyc82a27b2011-10-21 01:07:16 +0000290static char *ReadBlobStringWithLongSize(Image *image,char *string,size_t max,
291 ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000292{
293 int
294 c;
295
296 MagickOffsetType
297 offset;
298
cristybb503372010-05-27 20:51:26 +0000299 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000300 i;
301
cristybb503372010-05-27 20:51:26 +0000302 size_t
cristy3ed852e2009-09-05 21:47:34 +0000303 length;
304
305 assert(image != (Image *) NULL);
cristye1c94d92015-06-28 12:16:33 +0000306 assert(image->signature == MagickCoreSignature);
cristy3ed852e2009-09-05 21:47:34 +0000307 assert(max != 0);
308 if (image->debug != MagickFalse)
309 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
310 length=ReadBlobMSBLong(image);
cristybb503372010-05-27 20:51:26 +0000311 for (i=0; i < (ssize_t) MagickMin(length,max-1); i++)
cristy3ed852e2009-09-05 21:47:34 +0000312 {
313 c=ReadBlobByte(image);
314 if (c == EOF)
315 return((char *) NULL);
316 string[i]=(char) c;
317 }
318 string[i]='\0';
319 offset=SeekBlob(image,(MagickOffsetType) (length-i),SEEK_CUR);
320 if (offset < 0)
cristyc82a27b2011-10-21 01:07:16 +0000321 (void) ThrowMagickException(exception,GetMagickModule(),
cristy3ed852e2009-09-05 21:47:34 +0000322 CorruptImageError,"ImproperImageHeader","`%s'",image->filename);
323 return(string);
324}
325
326static MagickBooleanType load_tile(Image *image,Image *tile_image,
cristy018f07f2011-09-04 21:15:19 +0000327 XCFDocInfo *inDocInfo,XCFLayerInfo *inLayerInfo,size_t data_length,
328 ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000329{
cristybb503372010-05-27 20:51:26 +0000330 ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000331 y;
332
cristybb503372010-05-27 20:51:26 +0000333 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000334 x;
335
cristy4c08aed2011-07-01 19:47:50 +0000336 register Quantum
cristy3ed852e2009-09-05 21:47:34 +0000337 *q;
338
339 ssize_t
340 count;
341
342 unsigned char
343 *graydata;
344
cristy101ab702011-10-13 13:06:32 +0000345 XCFPixelInfo
cristy3ed852e2009-09-05 21:47:34 +0000346 *xcfdata,
347 *xcfodata;
348
cristy101ab702011-10-13 13:06:32 +0000349 xcfdata=(XCFPixelInfo *) AcquireQuantumMemory(data_length,sizeof(*xcfdata));
350 if (xcfdata == (XCFPixelInfo *) NULL)
cristy3ed852e2009-09-05 21:47:34 +0000351 ThrowBinaryException(ResourceLimitError,"MemoryAllocationFailed",
352 image->filename);
353 xcfodata=xcfdata;
354 graydata=(unsigned char *) xcfdata; /* used by gray and indexed */
355 count=ReadBlob(image,data_length,(unsigned char *) xcfdata);
356 if (count != (ssize_t) data_length)
357 ThrowBinaryException(CorruptImageError,"NotEnoughPixelData",
358 image->filename);
cristybb503372010-05-27 20:51:26 +0000359 for (y=0; y < (ssize_t) tile_image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +0000360 {
cristy8415f062012-04-03 00:12:53 +0000361 q=GetAuthenticPixels(tile_image,0,y,tile_image->columns,1,exception);
cristyacd2ed22011-08-30 01:44:23 +0000362 if (q == (Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +0000363 break;
364 if (inDocInfo->image_type == GIMP_GRAY)
365 {
cristybb503372010-05-27 20:51:26 +0000366 for (x=0; x < (ssize_t) tile_image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +0000367 {
cristyaf6eb932012-01-01 01:22:59 +0000368 SetPixelGray(tile_image,ScaleCharToQuantum(*graydata),q);
cristy4c08aed2011-07-01 19:47:50 +0000369 SetPixelAlpha(tile_image,ScaleCharToQuantum((unsigned char)
370 inLayerInfo->alpha),q);
cristy3ed852e2009-09-05 21:47:34 +0000371 graydata++;
cristyed231572011-07-14 02:18:59 +0000372 q+=GetPixelChannels(tile_image);
cristy3ed852e2009-09-05 21:47:34 +0000373 }
374 }
375 else
376 if (inDocInfo->image_type == GIMP_RGB)
377 {
cristybb503372010-05-27 20:51:26 +0000378 for (x=0; x < (ssize_t) tile_image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +0000379 {
cristy4c08aed2011-07-01 19:47:50 +0000380 SetPixelRed(tile_image,ScaleCharToQuantum(xcfdata->red),q);
381 SetPixelGreen(tile_image,ScaleCharToQuantum(xcfdata->green),q);
382 SetPixelBlue(tile_image,ScaleCharToQuantum(xcfdata->blue),q);
cristyff08add2012-04-02 00:33:26 +0000383 SetPixelAlpha(tile_image,xcfdata->alpha == 255U ? TransparentAlpha :
cristy4c08aed2011-07-01 19:47:50 +0000384 ScaleCharToQuantum((unsigned char) inLayerInfo->alpha),q);
cristy3ed852e2009-09-05 21:47:34 +0000385 xcfdata++;
cristyed231572011-07-14 02:18:59 +0000386 q+=GetPixelChannels(tile_image);
cristy3ed852e2009-09-05 21:47:34 +0000387 }
388 }
389 if (SyncAuthenticPixels(tile_image,exception) == MagickFalse)
390 break;
391 }
cristy101ab702011-10-13 13:06:32 +0000392 xcfodata=(XCFPixelInfo *) RelinquishMagickMemory(xcfodata);
cristy3ed852e2009-09-05 21:47:34 +0000393 return MagickTrue;
394}
395
396static MagickBooleanType load_tile_rle(Image *image,Image *tile_image,
cristy018f07f2011-09-04 21:15:19 +0000397 XCFDocInfo *inDocInfo,XCFLayerInfo *inLayerInfo,size_t data_length,
398 ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000399{
cristy3ed852e2009-09-05 21:47:34 +0000400 MagickOffsetType
401 size;
402
cristy2cbb3b32012-04-01 19:11:10 +0000403 Quantum
404 alpha;
405
cristy4c08aed2011-07-01 19:47:50 +0000406 register Quantum
cristy3ed852e2009-09-05 21:47:34 +0000407 *q;
408
cristy3ed852e2009-09-05 21:47:34 +0000409 size_t
410 length;
411
cristyc6da28e2011-04-28 01:41:35 +0000412 ssize_t
413 bytes_per_pixel,
414 count,
415 i,
416 j;
417
cristy3ed852e2009-09-05 21:47:34 +0000418 unsigned char
419 data,
420 pixel,
421 *xcfdata,
422 *xcfodata,
423 *xcfdatalimit;
424
425 bytes_per_pixel=(ssize_t) inDocInfo->bytes_per_pixel;
426 xcfdata=(unsigned char *) AcquireQuantumMemory(data_length,sizeof(*xcfdata));
427 if (xcfdata == (unsigned char *) NULL)
428 ThrowBinaryException(ResourceLimitError,"MemoryAllocationFailed",
429 image->filename);
430 xcfodata=xcfdata;
431 count=ReadBlob(image, (size_t) data_length, xcfdata);
432 xcfdatalimit = xcfodata+count-1;
cristy2cbb3b32012-04-01 19:11:10 +0000433 alpha=ScaleCharToQuantum((unsigned char) inLayerInfo->alpha);
cristybb503372010-05-27 20:51:26 +0000434 for (i=0; i < (ssize_t) bytes_per_pixel; i++)
cristy3ed852e2009-09-05 21:47:34 +0000435 {
cristy8415f062012-04-03 00:12:53 +0000436 q=GetAuthenticPixels(tile_image,0,0,tile_image->columns,tile_image->rows,
cristy2e8bde02010-06-22 11:03:25 +0000437 exception);
cristy2cbb3b32012-04-01 19:11:10 +0000438 if (q == (Quantum *) NULL)
439 continue;
cristy3ed852e2009-09-05 21:47:34 +0000440 size=(MagickOffsetType) tile_image->rows*tile_image->columns;
441 while (size > 0)
442 {
443 if (xcfdata > xcfdatalimit)
444 goto bogus_rle;
445 pixel=(*xcfdata++);
446 length=(size_t) pixel;
447 if (length >= 128)
448 {
449 length=255-(length-1);
450 if (length == 128)
451 {
452 if (xcfdata >= xcfdatalimit)
453 goto bogus_rle;
454 length=(size_t) ((*xcfdata << 8) + xcfdata[1]);
455 xcfdata+=2;
456 }
cristyc72dd362014-06-07 22:12:02 +0000457 size-=length;
458 if (size < 0)
459 goto bogus_rle;
460 if (&xcfdata[length-1] > xcfdatalimit)
461 goto bogus_rle;
462 while (length-- > 0)
cristy3ed852e2009-09-05 21:47:34 +0000463 {
cristyc72dd362014-06-07 22:12:02 +0000464 data=(*xcfdata++);
465 switch (i)
cristy3ed852e2009-09-05 21:47:34 +0000466 {
cristyc72dd362014-06-07 22:12:02 +0000467 case 0:
cristy3ed852e2009-09-05 21:47:34 +0000468 {
cristyc72dd362014-06-07 22:12:02 +0000469 if (inDocInfo->image_type == GIMP_GRAY)
470 SetPixelGray(tile_image,ScaleCharToQuantum(data),q);
471 else
472 {
473 SetPixelRed(tile_image,ScaleCharToQuantum(data),q);
cristy60baf162014-06-07 20:42:33 +0000474 SetPixelGreen(tile_image,ScaleCharToQuantum(data),q);
cristyc72dd362014-06-07 22:12:02 +0000475 SetPixelBlue(tile_image,ScaleCharToQuantum(data),q);
476 }
477 SetPixelAlpha(tile_image,alpha,q);
478 break;
cristy3ed852e2009-09-05 21:47:34 +0000479 }
cristyc72dd362014-06-07 22:12:02 +0000480 case 1:
481 {
482 if (inDocInfo->image_type == GIMP_GRAY)
483 SetPixelAlpha(tile_image,ScaleCharToQuantum(data),q);
484 else
485 SetPixelGreen(tile_image,ScaleCharToQuantum(data),q);
486 break;
487 }
488 case 2:
489 {
490 SetPixelBlue(tile_image,ScaleCharToQuantum(data),q);
491 break;
492 }
493 case 3:
494 {
495 SetPixelAlpha(tile_image,ScaleCharToQuantum(data),q);
496 break;
497 }
cristy3ed852e2009-09-05 21:47:34 +0000498 }
cristyc72dd362014-06-07 22:12:02 +0000499 q+=GetPixelChannels(tile_image);
cristy3ed852e2009-09-05 21:47:34 +0000500 }
cristyc72dd362014-06-07 22:12:02 +0000501 }
502 else
503 {
504 length+=1;
505 if (length == 128)
506 {
507 if (xcfdata >= xcfdatalimit)
508 goto bogus_rle;
509 length=(size_t) ((*xcfdata << 8) + xcfdata[1]);
510 xcfdata+=2;
511 }
512 size-=length;
513 if (size < 0)
514 goto bogus_rle;
515 if (xcfdata > xcfdatalimit)
516 goto bogus_rle;
517 pixel=(*xcfdata++);
518 for (j=0; j < (ssize_t) length; j++)
519 {
520 data=pixel;
521 switch (i)
522 {
523 case 0:
524 {
525 if (inDocInfo->image_type == GIMP_GRAY)
526 SetPixelGray(tile_image,ScaleCharToQuantum(data),q);
527 else
528 {
529 SetPixelRed(tile_image,ScaleCharToQuantum(data),q);
530 SetPixelGreen(tile_image,ScaleCharToQuantum(data),q);
531 SetPixelBlue(tile_image,ScaleCharToQuantum(data),q);
532 }
533 SetPixelAlpha(tile_image,alpha,q);
534 break;
535 }
536 case 1:
537 {
538 if (inDocInfo->image_type == GIMP_GRAY)
539 SetPixelAlpha(tile_image,ScaleCharToQuantum(data),q);
540 else
541 SetPixelGreen(tile_image,ScaleCharToQuantum(data),q);
542 break;
543 }
544 case 2:
545 {
546 SetPixelBlue(tile_image,ScaleCharToQuantum(data),q);
547 break;
548 }
549 case 3:
550 {
551 SetPixelAlpha(tile_image,ScaleCharToQuantum(data),q);
552 break;
553 }
554 }
555 q+=GetPixelChannels(tile_image);
556 }
557 }
cristy3ed852e2009-09-05 21:47:34 +0000558 }
cristyc72dd362014-06-07 22:12:02 +0000559 if (SyncAuthenticPixels(tile_image,exception) == MagickFalse)
560 break;
561 }
cristy3ed852e2009-09-05 21:47:34 +0000562 xcfodata=(unsigned char *) RelinquishMagickMemory(xcfodata);
563 return(MagickTrue);
564
565 bogus_rle:
566 if (xcfodata != (unsigned char *) NULL)
567 xcfodata=(unsigned char *) RelinquishMagickMemory(xcfodata);
568 return(MagickFalse);
569}
570
571static MagickBooleanType load_level(Image *image,XCFDocInfo *inDocInfo,
cristy2cbb3b32012-04-01 19:11:10 +0000572 XCFLayerInfo *inLayerInfo,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000573{
cristy3ed852e2009-09-05 21:47:34 +0000574 int
575 destLeft = 0,
576 destTop = 0;
577
578 Image*
579 tile_image;
580
581 MagickBooleanType
582 status;
583
584 MagickOffsetType
585 saved_pos,
586 offset,
587 offset2;
588
cristybb503372010-05-27 20:51:26 +0000589 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000590 i;
591
cristybb503372010-05-27 20:51:26 +0000592 size_t
cristy3ed852e2009-09-05 21:47:34 +0000593 width,
594 height,
595 ntiles,
596 ntile_rows,
597 ntile_cols,
598 tile_image_width,
599 tile_image_height;
600
601 /* start reading the data */
cristy3ed852e2009-09-05 21:47:34 +0000602 width=ReadBlobMSBLong(image);
603 height=ReadBlobMSBLong(image);
604
605 /* read in the first tile offset.
606 * if it is '0', then this tile level is empty
607 * and we can simply return.
608 */
609 offset=(MagickOffsetType) ReadBlobMSBLong(image);
610 if (offset == 0)
611 return(MagickTrue);
612 /* Initialise the reference for the in-memory tile-compression
613 */
614 ntile_rows=(height+TILE_HEIGHT-1)/TILE_HEIGHT;
615 ntile_cols=(width+TILE_WIDTH-1)/TILE_WIDTH;
616 ntiles=ntile_rows*ntile_cols;
cristybb503372010-05-27 20:51:26 +0000617 for (i = 0; i < (ssize_t) ntiles; i++)
cristy3ed852e2009-09-05 21:47:34 +0000618 {
619 status=MagickFalse;
620 if (offset == 0)
621 ThrowBinaryException(CorruptImageError,"NotEnoughTiles",image->filename);
622 /* save the current position as it is where the
623 * next tile offset is stored.
624 */
625 saved_pos=TellBlob(image);
626 /* read in the offset of the next tile so we can calculate the amount
627 of data needed for this tile*/
628 offset2=(MagickOffsetType)ReadBlobMSBLong(image);
629 /* if the offset is 0 then we need to read in the maximum possible
630 allowing for negative compression */
631 if (offset2 == 0)
632 offset2=(MagickOffsetType) (offset + TILE_WIDTH * TILE_WIDTH * 4* 1.5);
633 /* seek to the tile offset */
634 offset=SeekBlob(image, offset, SEEK_SET);
635
cristy46e350e2012-02-11 18:56:14 +0000636 /*
637 Allocate the image for the tile. NOTE: the last tile in a row or
638 column may not be a full tile!
cristy3ed852e2009-09-05 21:47:34 +0000639 */
cristybb503372010-05-27 20:51:26 +0000640 tile_image_width=(size_t) (destLeft == (int) ntile_cols-1 ?
cristy3ed852e2009-09-05 21:47:34 +0000641 (int) width % TILE_WIDTH : TILE_WIDTH);
cristy46e350e2012-02-11 18:56:14 +0000642 if (tile_image_width == 0)
643 tile_image_width=TILE_WIDTH;
cristybb503372010-05-27 20:51:26 +0000644 tile_image_height = (size_t) (destTop == (int) ntile_rows-1 ?
cristy3ed852e2009-09-05 21:47:34 +0000645 (int) height % TILE_HEIGHT : TILE_HEIGHT);
cristy46e350e2012-02-11 18:56:14 +0000646 if (tile_image_height == 0)
647 tile_image_height=TILE_HEIGHT;
cristy3ed852e2009-09-05 21:47:34 +0000648 tile_image=CloneImage(inLayerInfo->image,tile_image_width,
649 tile_image_height,MagickTrue,exception);
650
651 /* read in the tile */
652 switch (inDocInfo->compression)
653 {
654 case COMPRESS_NONE:
cristy018f07f2011-09-04 21:15:19 +0000655 if (load_tile(image,tile_image,inDocInfo,inLayerInfo,(size_t) (offset2-offset),exception) == 0)
cristy3ed852e2009-09-05 21:47:34 +0000656 status=MagickTrue;
657 break;
658 case COMPRESS_RLE:
659 if (load_tile_rle (image,tile_image,inDocInfo,inLayerInfo,
cristy018f07f2011-09-04 21:15:19 +0000660 (int) (offset2-offset),exception) == 0)
cristy3ed852e2009-09-05 21:47:34 +0000661 status=MagickTrue;
662 break;
663 case COMPRESS_ZLIB:
664 ThrowBinaryException(CoderError,"ZipCompressNotSupported",
665 image->filename)
666 case COMPRESS_FRACTAL:
667 ThrowBinaryException(CoderError,"FractalCompressNotSupported",
668 image->filename)
669 }
670
671 /* composite the tile onto the layer's image, and then destroy it */
cristyfeb3e962012-03-29 17:25:55 +0000672 (void) CompositeImage(inLayerInfo->image,tile_image,CopyCompositeOp,
cristy547ca552012-03-31 16:15:43 +0000673 MagickTrue,destLeft * TILE_WIDTH,destTop*TILE_HEIGHT,exception);
cristy3ed852e2009-09-05 21:47:34 +0000674 tile_image=DestroyImage(tile_image);
675
676 /* adjust tile position */
677 destLeft++;
678 if (destLeft >= (int) ntile_cols)
679 {
680 destLeft = 0;
681 destTop++;
682 }
683 if (status != MagickFalse)
684 return(MagickFalse);
685 /* restore the saved position so we'll be ready to
686 * read the next offset.
687 */
688 offset=SeekBlob(image, saved_pos, SEEK_SET);
689 /* read in the offset of the next tile */
690 offset=(MagickOffsetType) ReadBlobMSBLong(image);
691 }
692 if (offset != 0)
693 ThrowBinaryException(CorruptImageError,"CorruptImage",image->filename)
694 return(MagickTrue);
695}
696
697static MagickBooleanType load_hierarchy(Image *image,XCFDocInfo *inDocInfo,
cristy2cbb3b32012-04-01 19:11:10 +0000698 XCFLayerInfo *inLayer, ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000699{
700 MagickOffsetType
701 saved_pos,
702 offset,
703 junk;
704
cristybb503372010-05-27 20:51:26 +0000705 size_t
cristy3ed852e2009-09-05 21:47:34 +0000706 width,
707 height,
708 bytes_per_pixel;
709
710 width=ReadBlobMSBLong(image);
cristyda16f162011-02-19 23:52:17 +0000711 (void) width;
cristy3ed852e2009-09-05 21:47:34 +0000712 height=ReadBlobMSBLong(image);
cristyda16f162011-02-19 23:52:17 +0000713 (void) height;
cristy3ed852e2009-09-05 21:47:34 +0000714 bytes_per_pixel=inDocInfo->bytes_per_pixel=ReadBlobMSBLong(image);
cristyda16f162011-02-19 23:52:17 +0000715 (void) bytes_per_pixel;
cristy3ed852e2009-09-05 21:47:34 +0000716
717 /* load in the levels...we make sure that the number of levels
718 * calculated when the TileManager was created is the same
719 * as the number of levels found in the file.
720 */
721 offset=(MagickOffsetType) ReadBlobMSBLong(image); /* top level */
722
723 /* discard offsets for layers below first, if any.
724 */
725 do
726 {
727 junk=(MagickOffsetType) ReadBlobMSBLong(image);
728 }
729 while (junk != 0);
730
731 /* save the current position as it is where the
732 * next level offset is stored.
733 */
734 saved_pos=TellBlob(image);
735
736 /* seek to the level offset */
737 offset=SeekBlob(image, offset, SEEK_SET);
738
739 /* read in the level */
cristy2cbb3b32012-04-01 19:11:10 +0000740 if (load_level (image, inDocInfo, inLayer, exception) == 0)
cristy3ed852e2009-09-05 21:47:34 +0000741 return(MagickFalse);
742 /* restore the saved position so we'll be ready to
743 * read the next offset.
744 */
745 offset=SeekBlob(image, saved_pos, SEEK_SET);
746 return(MagickTrue);
747}
748
cristy2cbb3b32012-04-01 19:11:10 +0000749static MagickBooleanType ReadOneLayer(const ImageInfo *image_info,Image* image,
750 XCFDocInfo* inDocInfo,XCFLayerInfo *outLayer,const ssize_t layer,
751 ExceptionInfo *exception )
cristy3ed852e2009-09-05 21:47:34 +0000752{
cristy3ed852e2009-09-05 21:47:34 +0000753 MagickOffsetType
754 offset;
755
756 unsigned int
757 foundPropEnd = 0;
758
cristybb503372010-05-27 20:51:26 +0000759 size_t
cristy3ed852e2009-09-05 21:47:34 +0000760 hierarchy_offset,
761 layer_mask_offset;
762
763 /* clear the block! */
764 (void) ResetMagickMemory( outLayer, 0, sizeof( XCFLayerInfo ) );
765 /* read in the layer width, height, type and name */
766 outLayer->width = ReadBlobMSBLong(image);
767 outLayer->height = ReadBlobMSBLong(image);
768 outLayer->type = ReadBlobMSBLong(image);
769 (void) ReadBlobStringWithLongSize(image, outLayer->name,
cristyc82a27b2011-10-21 01:07:16 +0000770 sizeof(outLayer->name),exception);
cristy3ed852e2009-09-05 21:47:34 +0000771 /* read the layer properties! */
772 foundPropEnd = 0;
773 while ( (foundPropEnd == MagickFalse) && (EOFBlob(image) == MagickFalse) ) {
774 PropType prop_type = (PropType) ReadBlobMSBLong(image);
cristybb503372010-05-27 20:51:26 +0000775 size_t prop_size = ReadBlobMSBLong(image);
cristy3ed852e2009-09-05 21:47:34 +0000776 switch (prop_type)
777 {
778 case PROP_END:
779 foundPropEnd = 1;
780 break;
781 case PROP_ACTIVE_LAYER:
782 outLayer->active = 1;
783 break;
784 case PROP_FLOATING_SELECTION:
785 outLayer->floating_offset = ReadBlobMSBLong(image);
786 break;
787 case PROP_OPACITY:
cristy4c08aed2011-07-01 19:47:50 +0000788 outLayer->alpha = ReadBlobMSBLong(image);
cristy3ed852e2009-09-05 21:47:34 +0000789 break;
790 case PROP_VISIBLE:
791 outLayer->visible = ReadBlobMSBLong(image);
792 break;
793 case PROP_LINKED:
794 outLayer->linked = ReadBlobMSBLong(image);
795 break;
796 case PROP_PRESERVE_TRANSPARENCY:
797 outLayer->preserve_trans = ReadBlobMSBLong(image);
798 break;
799 case PROP_APPLY_MASK:
800 outLayer->apply_mask = ReadBlobMSBLong(image);
801 break;
802 case PROP_EDIT_MASK:
803 outLayer->edit_mask = ReadBlobMSBLong(image);
804 break;
805 case PROP_SHOW_MASK:
806 outLayer->show_mask = ReadBlobMSBLong(image);
807 break;
808 case PROP_OFFSETS:
cristy6cff05d2010-09-02 11:22:46 +0000809 outLayer->offset_x = (int) ReadBlobMSBLong(image);
810 outLayer->offset_y = (int) ReadBlobMSBLong(image);
cristy3ed852e2009-09-05 21:47:34 +0000811 break;
812 case PROP_MODE:
813 outLayer->mode = ReadBlobMSBLong(image);
814 break;
815 case PROP_TATTOO:
816 outLayer->preserve_trans = ReadBlobMSBLong(image);
817 break;
818 case PROP_PARASITES:
819 {
cristyd4297022010-09-16 22:59:09 +0000820 if (DiscardBlobBytes(image,prop_size) == MagickFalse)
cristyc82a27b2011-10-21 01:07:16 +0000821 ThrowFileException(exception,CorruptImageError,
cristyd4297022010-09-16 22:59:09 +0000822 "UnexpectedEndOfFile",image->filename);
cristy3ed852e2009-09-05 21:47:34 +0000823
824 /*
cristybb503372010-05-27 20:51:26 +0000825 ssize_t base = info->cp;
cristy3ed852e2009-09-05 21:47:34 +0000826 GimpParasite *p;
827 while (info->cp - base < prop_size)
828 {
829 p = xcf_load_parasite(info);
830 gimp_drawable_parasite_attach(GIMP_DRAWABLE(layer), p);
831 gimp_parasite_free(p);
832 }
833 if (info->cp - base != prop_size)
834 g_message ("Error detected while loading a layer's parasites");
835 */
836 }
837 break;
838 default:
839 /* g_message ("unexpected/unknown layer property: %d (skipping)",
840 prop_type); */
841
842 {
843 int buf[16];
844 ssize_t amount;
845
846 /* read over it... */
847 while ((prop_size > 0) && (EOFBlob(image) == MagickFalse))
848 {
849 amount = (ssize_t) MagickMin(16, prop_size);
850 amount = ReadBlob(image, (size_t) amount, (unsigned char *) &buf);
851 if (!amount)
852 ThrowBinaryException(CorruptImageError,"CorruptImage",
853 image->filename);
cristybb503372010-05-27 20:51:26 +0000854 prop_size -= (size_t) MagickMin(16, (size_t) amount);
cristy3ed852e2009-09-05 21:47:34 +0000855 }
856 }
857 break;
858 }
859 }
860
861 if (foundPropEnd == MagickFalse)
862 return(MagickFalse);
cristy2cbb3b32012-04-01 19:11:10 +0000863 /* allocate the image for this layer */
864 if (image_info->number_scenes != 0)
865 {
866 ssize_t
867 scene;
868
869 scene=inDocInfo->number_layers-layer-1;
cristyd9ecd042012-06-17 18:26:12 +0000870 if (scene > (ssize_t) (image_info->scene+image_info->number_scenes-1))
cristy2cbb3b32012-04-01 19:11:10 +0000871 {
872 outLayer->image=CloneImage(image,0,0,MagickTrue,exception);
cristy8415f062012-04-03 00:12:53 +0000873 if (outLayer->image == (Image *) NULL)
874 return(MagickFalse);
875 outLayer->image->page.x=outLayer->offset_x;
876 outLayer->image->page.y=outLayer->offset_y;
877 outLayer->image->page.width=outLayer->width;
878 outLayer->image->page.height=outLayer->height;
cristy2cbb3b32012-04-01 19:11:10 +0000879 return(MagickTrue);
880 }
881 }
882 outLayer->image=CloneImage(image,outLayer->width, outLayer->height,MagickTrue,
883 exception);
884 if (outLayer->image == (Image *) NULL)
cristy8415f062012-04-03 00:12:53 +0000885 return(MagickFalse);
cristy3ed852e2009-09-05 21:47:34 +0000886 /* clear the image based on the layer opacity */
cristy4c08aed2011-07-01 19:47:50 +0000887 outLayer->image->background_color.alpha=
cristy547ca552012-03-31 16:15:43 +0000888 ScaleCharToQuantum((unsigned char) outLayer->alpha);
cristyea1a8aa2011-10-20 13:24:06 +0000889 (void) SetImageBackgroundColor(outLayer->image,exception);
cristy3ed852e2009-09-05 21:47:34 +0000890
cristy8415f062012-04-03 00:12:53 +0000891 outLayer->image->page.x=outLayer->offset_x;
892 outLayer->image->page.y=outLayer->offset_y;
893 outLayer->image->page.width=outLayer->width;
894 outLayer->image->page.height=outLayer->height;
cristy3ed852e2009-09-05 21:47:34 +0000895 /* set the compositing mode */
896 outLayer->image->compose = GIMPBlendModeToCompositeOperator( outLayer->mode );
897 if ( outLayer->visible == MagickFalse )
898 {
899 /* BOGUS: should really be separate member var! */
900 outLayer->image->compose = NoCompositeOp;
901 }
902
903 /* read the hierarchy and layer mask offsets */
904 hierarchy_offset = ReadBlobMSBLong(image);
905 layer_mask_offset = ReadBlobMSBLong(image);
906
907 /* read in the hierarchy */
908 offset=SeekBlob(image, (MagickOffsetType) hierarchy_offset, SEEK_SET);
909 if (offset < 0)
cristyc82a27b2011-10-21 01:07:16 +0000910 (void) ThrowMagickException(exception,GetMagickModule(),
cristy3ed852e2009-09-05 21:47:34 +0000911 CorruptImageError,"InvalidImageHeader","`%s'",image->filename);
cristy2cbb3b32012-04-01 19:11:10 +0000912 if (load_hierarchy (image, inDocInfo, outLayer, exception) == 0)
cristy3ed852e2009-09-05 21:47:34 +0000913 return(MagickFalse);
914
915 /* read in the layer mask */
916 if (layer_mask_offset != 0)
917 {
918 offset=SeekBlob(image, (MagickOffsetType) layer_mask_offset, SEEK_SET);
919
920#if 0 /* BOGUS: support layer masks! */
921 layer_mask = xcf_load_layer_mask (info, gimage);
922 if (layer_mask == 0)
923 goto error;
924
925 /* set the offsets of the layer_mask */
926 GIMP_DRAWABLE (layer_mask)->offset_x = GIMP_DRAWABLE (layer)->offset_x;
927 GIMP_DRAWABLE (layer_mask)->offset_y = GIMP_DRAWABLE (layer)->offset_y;
928
929 gimp_layer_add_mask (layer, layer_mask, MagickFalse);
930
931 layer->mask->apply_mask = apply_mask;
932 layer->mask->edit_mask = edit_mask;
933 layer->mask->show_mask = show_mask;
934#endif
935 }
936
937 /* attach the floating selection... */
938#if 0 /* BOGUS: we may need to read this, even if we don't support it! */
939 if (add_floating_sel)
940 {
941 GimpLayer *floating_sel;
942
943 floating_sel = info->floating_sel;
944 floating_sel_attach (floating_sel, GIMP_DRAWABLE (layer));
945 }
946#endif
947
948 return MagickTrue;
949}
cristy8f0fbc02012-03-31 16:31:42 +0000950
cristy3ed852e2009-09-05 21:47:34 +0000951/*
952%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
953% %
954% %
955% %
956% R e a d X C F I m a g e %
957% %
958% %
959% %
960%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
961%
962% ReadXCFImage() reads a GIMP (GNU Image Manipulation Program) image
963% file and returns it. It allocates the memory necessary for the new Image
964% structure and returns a pointer to the new image.
965%
966% The format of the ReadXCFImage method is:
967%
968% image=ReadXCFImage(image_info)
969%
970% A description of each parameter follows:
971%
972% o image_info: the image info.
973%
974% o exception: return any errors or warnings in this structure.
975%
cristy3ed852e2009-09-05 21:47:34 +0000976*/
977static Image *ReadXCFImage(const ImageInfo *image_info,ExceptionInfo *exception)
978{
979 char
980 magick[14];
981
982 Image
983 *image;
984
985 int
986 foundPropEnd = 0;
987
988 MagickBooleanType
989 status;
990
991 MagickOffsetType
992 offset;
993
cristybb503372010-05-27 20:51:26 +0000994 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000995 i;
996
997 size_t
cristyc6da28e2011-04-28 01:41:35 +0000998 image_type,
cristy3ed852e2009-09-05 21:47:34 +0000999 length;
1000
1001 ssize_t
1002 count;
1003
cristy3ed852e2009-09-05 21:47:34 +00001004 XCFDocInfo
1005 doc_info;
1006
1007 /*
1008 Open image file.
1009 */
1010 assert(image_info != (const ImageInfo *) NULL);
cristye1c94d92015-06-28 12:16:33 +00001011 assert(image_info->signature == MagickCoreSignature);
cristy3ed852e2009-09-05 21:47:34 +00001012 if (image_info->debug != MagickFalse)
1013 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
1014 image_info->filename);
1015 assert(exception != (ExceptionInfo *) NULL);
cristye1c94d92015-06-28 12:16:33 +00001016 assert(exception->signature == MagickCoreSignature);
cristy9950d572011-10-01 18:22:35 +00001017 image=AcquireImage(image_info,exception);
cristy3ed852e2009-09-05 21:47:34 +00001018 status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception);
1019 if (status == MagickFalse)
1020 {
1021 image=DestroyImageList(image);
1022 return((Image *) NULL);
1023 }
1024 count=ReadBlob(image,14,(unsigned char *) magick);
cristy771c8842015-01-09 12:13:22 +00001025 if ((count != 14) ||
cristy3ed852e2009-09-05 21:47:34 +00001026 (LocaleNCompare((char *) magick,"gimp xcf",8) != 0))
1027 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
1028 (void) ResetMagickMemory(&doc_info,0,sizeof(XCFDocInfo));
cristy3ed852e2009-09-05 21:47:34 +00001029 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)
cristye2c4f182012-05-12 14:11:53 +00001044 SetImageColorspace(image,sRGBColorspace,exception);
cristy3ed852e2009-09-05 21:47:34 +00001045 else
1046 if (image_type == GIMP_GRAY)
cristye2c4f182012-05-12 14:11:53 +00001047 SetImageColorspace(image,GRAYColorspace,exception);
cristy3ed852e2009-09-05 21:47:34 +00001048 else
1049 if (image_type == GIMP_INDEXED)
1050 ThrowReaderException(CoderError,"ColormapTypeNotSupported");
cristy547ca552012-03-31 16:15:43 +00001051 (void) SetImageBackgroundColor(image,exception);
cristy59856fd2013-10-30 23:44:18 +00001052 (void) SetImageAlpha(image,OpaqueAlpha,exception);
cristy3ed852e2009-09-05 21:47:34 +00001053 /*
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);
cristyd4297022010-09-16 22:59:09 +00001072 if (DiscardBlobBytes(image,3*num_colours) == MagickFalse)
cristyc82a27b2011-10-21 01:07:16 +00001073 ThrowFileException(exception,CorruptImageError,
cristyd4297022010-09-16 22:59:09 +00001074 "UnexpectedEndOfFile",image->filename);
cristy3ed852e2009-09-05 21:47:34 +00001075 /*
1076 if (info->file_version == 0)
1077 {
1078 gint i;
1079
1080 g_message (_("XCF warning: version 0 of XCF file format\n"
1081 "did not save indexed colormaps correctly.\n"
1082 "Substituting grayscale map."));
1083 info->cp +=
1084 xcf_read_int32 (info->fp, (guint32*) &gimage->num_cols, 1);
1085 gimage->cmap = g_new (guchar, gimage->num_cols*3);
1086 xcf_seek_pos (info, info->cp + gimage->num_cols);
1087 for (i = 0; i<gimage->num_cols; i++)
1088 {
1089 gimage->cmap[i*3+0] = i;
1090 gimage->cmap[i*3+1] = i;
1091 gimage->cmap[i*3+2] = i;
1092 }
1093 }
1094 else
1095 {
1096 info->cp +=
1097 xcf_read_int32 (info->fp, (guint32*) &gimage->num_cols, 1);
1098 gimage->cmap = g_new (guchar, gimage->num_cols*3);
1099 info->cp +=
1100 xcf_read_int8 (info->fp,
1101 (guint8*) gimage->cmap, gimage->num_cols*3);
1102 }
1103 */
1104 break;
1105 }
1106 case PROP_COMPRESSION:
1107 {
1108 doc_info.compression = ReadBlobByte(image);
1109 if ((doc_info.compression != COMPRESS_NONE) &&
1110 (doc_info.compression != COMPRESS_RLE) &&
1111 (doc_info.compression != COMPRESS_ZLIB) &&
1112 (doc_info.compression != COMPRESS_FRACTAL))
1113 ThrowReaderException(CorruptImageError,"UnrecognizedImageCompression");
1114 }
1115 break;
1116
1117 case PROP_GUIDES:
1118 {
1119 /* just skip it - we don't care about guides */
cristyd4297022010-09-16 22:59:09 +00001120 if (DiscardBlobBytes(image,prop_size) == MagickFalse)
cristyc82a27b2011-10-21 01:07:16 +00001121 ThrowFileException(exception,CorruptImageError,
cristyd4297022010-09-16 22:59:09 +00001122 "UnexpectedEndOfFile",image->filename);
cristy3ed852e2009-09-05 21:47:34 +00001123 }
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 :(
cristy2a11bef2011-10-28 18:33:11 +00001144 image->resolution.x = xres;
1145 image->resolution.y = yres;
cristy3ed852e2009-09-05 21:47:34 +00001146 */
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 */
cristyd4297022010-09-16 22:59:09 +00001160 if (DiscardBlobBytes(image,prop_size) == MagickFalse)
cristyc82a27b2011-10-21 01:07:16 +00001161 ThrowFileException(exception,CorruptImageError,
cristyd4297022010-09-16 22:59:09 +00001162 "UnexpectedEndOfFile",image->filename);
cristy3ed852e2009-09-05 21:47:34 +00001163 /*
cristybb503372010-05-27 20:51:26 +00001164 gssize_t base = info->cp;
cristy3ed852e2009-09-05 21:47:34 +00001165 GimpParasite *p;
1166
1167 while (info->cp - base < prop_size)
1168 {
1169 p = xcf_load_parasite (info);
1170 gimp_image_parasite_attach (gimage, p);
1171 gimp_parasite_free (p);
1172 }
1173 if (info->cp - base != prop_size)
1174 g_message ("Error detected while loading an image's parasites");
1175 */
1176 }
1177 break;
1178
1179 case PROP_UNIT:
1180 {
1181 /* BOGUS: ignore for now... */
cristybb503372010-05-27 20:51:26 +00001182 /*size_t unit = */ (void) ReadBlobMSBLong(image);
cristy3ed852e2009-09-05 21:47:34 +00001183 }
1184 break;
1185
1186 case PROP_PATHS:
1187 {
1188 /* BOGUS: just skip it for now */
cristyd4297022010-09-16 22:59:09 +00001189 if (DiscardBlobBytes(image,prop_size) == MagickFalse)
cristyc82a27b2011-10-21 01:07:16 +00001190 ThrowFileException(exception,CorruptImageError,"UnexpectedEndOfFile",
1191 image->filename);
cristy3ed852e2009-09-05 21:47:34 +00001192
1193 /*
1194 PathList *paths = xcf_load_bzpaths (gimage, info);
1195 gimp_image_set_paths (gimage, paths);
1196 */
1197 }
1198 break;
1199
1200 case PROP_USER_UNIT:
1201 {
1202 char unit_string[1000];
1203 /*BOGUS: ignored for now */
1204 /*float factor = (float) */ (void) ReadBlobMSBLong(image);
cristybb503372010-05-27 20:51:26 +00001205 /* size_t digits = */ (void) ReadBlobMSBLong(image);
cristy547ca552012-03-31 16:15:43 +00001206 for (i=0; i<5; i++)
1207 (void) ReadBlobStringWithLongSize(image, unit_string,
cristyc82a27b2011-10-21 01:07:16 +00001208 sizeof(unit_string),exception);
cristy3ed852e2009-09-05 21:47:34 +00001209 }
1210 break;
1211
1212 default:
1213 {
1214 int buf[16];
cristybb503372010-05-27 20:51:26 +00001215 ssize_t amount;
cristy3ed852e2009-09-05 21:47:34 +00001216
1217 /* read over it... */
1218 while ((prop_size > 0) && (EOFBlob(image) == MagickFalse))
1219 {
cristybb503372010-05-27 20:51:26 +00001220 amount=(ssize_t) MagickMin(16, prop_size);
1221 amount=(ssize_t) ReadBlob(image,(size_t) amount,(unsigned char *) &buf);
cristy3ed852e2009-09-05 21:47:34 +00001222 if (!amount)
1223 ThrowReaderException(CorruptImageError,"CorruptImage");
cristybb503372010-05-27 20:51:26 +00001224 prop_size -= (size_t) MagickMin(16,(size_t) amount);
cristy3ed852e2009-09-05 21:47:34 +00001225 }
1226 }
1227 break;
1228 }
1229 }
1230 if (foundPropEnd == MagickFalse)
1231 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
1232
1233 if ((image_info->ping != MagickFalse) && (image_info->number_scenes != 0))
1234 {
1235 ; /* do nothing, were just pinging! */
1236 }
1237 else
1238 {
1239 int
1240 current_layer = 0,
cristy547ca552012-03-31 16:15:43 +00001241 foundAllLayers = MagickFalse,
1242 number_layers = 0;
cristy3ed852e2009-09-05 21:47:34 +00001243
1244 MagickOffsetType
cristy547ca552012-03-31 16:15:43 +00001245 oldPos=TellBlob(image);
cristy3ed852e2009-09-05 21:47:34 +00001246
1247 XCFLayerInfo
1248 *layer_info;
1249
cristyacabb842014-12-14 23:36:33 +00001250 status=SetImageExtent(image,image->columns,image->rows,exception);
1251 if (status == MagickFalse)
1252 return(DestroyImageList(image));
cristy3ed852e2009-09-05 21:47:34 +00001253 /*
1254 the read pointer
1255 */
1256 do
1257 {
cristy6cff05d2010-09-02 11:22:46 +00001258 ssize_t offset = (int) 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);
cristy2cbb3b32012-04-01 19:11:10 +00001270 doc_info.number_layers=number_layers;
cristy3ed852e2009-09-05 21:47:34 +00001271 offset=SeekBlob(image,oldPos,SEEK_SET); /* restore the position! */
1272 if (offset < 0)
1273 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
1274 /* allocate our array of layer info blocks */
1275 length=(size_t) number_layers;
1276 layer_info=(XCFLayerInfo *) AcquireQuantumMemory(length,
1277 sizeof(*layer_info));
1278 if (layer_info == (XCFLayerInfo *) NULL)
1279 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
1280 (void) ResetMagickMemory(layer_info,0,number_layers*sizeof(XCFLayerInfo));
1281 for ( ; ; )
1282 {
1283 MagickBooleanType
1284 layer_ok;
1285
1286 MagickOffsetType
1287 offset,
1288 saved_pos;
1289
1290 /* read in the offset of the next layer */
1291 offset=(MagickOffsetType) ReadBlobMSBLong(image);
1292 /* if the offset is 0 then we are at the end
1293 * of the layer list.
1294 */
1295 if (offset == 0)
1296 break;
1297 /* save the current position as it is where the
1298 * next layer offset is stored.
1299 */
1300 saved_pos=TellBlob(image);
cristy547ca552012-03-31 16:15:43 +00001301 /* seek to the layer offset */
1302 offset=SeekBlob(image,offset,SEEK_SET);
1303 /* read in the layer */
cristy2cbb3b32012-04-01 19:11:10 +00001304 layer_ok=ReadOneLayer(image_info,image,&doc_info,
1305 &layer_info[current_layer],current_layer,exception);
cristy547ca552012-03-31 16:15:43 +00001306 if (layer_ok == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +00001307 {
cristy547ca552012-03-31 16:15:43 +00001308 int j;
1309
1310 for (j=0; j < current_layer; j++)
1311 layer_info[j].image=DestroyImage(layer_info[j].image);
cristy2cbb3b32012-04-01 19:11:10 +00001312 layer_info=(XCFLayerInfo *) RelinquishMagickMemory(layer_info);
1313 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
1314 }
cristy547ca552012-03-31 16:15:43 +00001315 /* restore the saved position so we'll be ready to
1316 * read the next offset.
1317 */
1318 offset=SeekBlob(image, saved_pos, SEEK_SET);
1319 current_layer++;
1320 }
cristy3ed852e2009-09-05 21:47:34 +00001321 if (number_layers == 1)
1322 {
1323 /*
1324 Composite the layer data onto the main image, dispose the layer.
1325 */
cristy59856fd2013-10-30 23:44:18 +00001326 (void) CompositeImage(image,layer_info[0].image,CopyCompositeOp,
cristy39172402012-03-30 13:04:39 +00001327 MagickTrue,layer_info[0].offset_x,layer_info[0].offset_y,exception);
cristy547ca552012-03-31 16:15:43 +00001328 layer_info[0].image =DestroyImage( layer_info[0].image);
cristy3ed852e2009-09-05 21:47:34 +00001329 }
1330 else
1331 {
1332#if 0
1333 {
1334 /* NOTE: XCF layers are REVERSED from composite order! */
1335 signed int j;
1336 for (j=number_layers-1; j>=0; j--) {
1337 /* BOGUS: need to consider layer blending modes!! */
1338
1339 if ( layer_info[j].visible ) { /* only visible ones, please! */
cristy547ca552012-03-31 16:15:43 +00001340 CompositeImage(image, OverCompositeOp, layer_info[j].image,
1341 layer_info[j].offset_x, layer_info[j].offset_y );
cristy3ed852e2009-09-05 21:47:34 +00001342 layer_info[j].image =DestroyImage( layer_info[j].image );
1343
cristy8f0fbc02012-03-31 16:31:42 +00001344 /* If we do this, we'll get REAL gray images! */
cristy3ed852e2009-09-05 21:47:34 +00001345 if ( image_type == GIMP_GRAY ) {
1346 QuantizeInfo qi;
1347 GetQuantizeInfo(&qi);
1348 qi.colorspace = GRAYColorspace;
1349 QuantizeImage( &qi, layer_info[j].image );
1350 }
1351 }
1352 }
1353 }
1354#else
1355 {
1356 /* NOTE: XCF layers are REVERSED from composite order! */
cristy8415f062012-04-03 00:12:53 +00001357 ssize_t j;
cristy3ed852e2009-09-05 21:47:34 +00001358
1359 /* first we copy the last layer on top of the main image */
cristyfeb3e962012-03-29 17:25:55 +00001360 (void) CompositeImage(image,layer_info[number_layers-1].image,
cristy39172402012-03-30 13:04:39 +00001361 CopyCompositeOp,MagickTrue,layer_info[number_layers-1].offset_x,
cristye941a752011-10-15 01:52:48 +00001362 layer_info[number_layers-1].offset_y,exception);
cristy547ca552012-03-31 16:15:43 +00001363 layer_info[number_layers-1].image=DestroyImage(
1364 layer_info[number_layers-1].image);
cristy3ed852e2009-09-05 21:47:34 +00001365
1366 /* now reverse the order of the layers as they are put
1367 into subimages
1368 */
cristy8415f062012-04-03 00:12:53 +00001369 for (j=(ssize_t) number_layers-2; j >= 0; j--)
1370 AppendImageToList(&image,layer_info[j].image);
cristy3ed852e2009-09-05 21:47:34 +00001371 }
1372#endif
1373 }
1374
1375 layer_info=(XCFLayerInfo *) RelinquishMagickMemory(layer_info);
1376
1377#if 0 /* BOGUS: do we need the channels?? */
1378 while (MagickTrue)
1379 {
1380 /* read in the offset of the next channel */
1381 info->cp += xcf_read_int32 (info->fp, &offset, 1);
1382
1383 /* if the offset is 0 then we are at the end
1384 * of the channel list.
1385 */
1386 if (offset == 0)
1387 break;
1388
1389 /* save the current position as it is where the
1390 * next channel offset is stored.
1391 */
1392 saved_pos = info->cp;
1393
1394 /* seek to the channel offset */
1395 xcf_seek_pos (info, offset);
1396
1397 /* read in the layer */
1398 channel = xcf_load_channel (info, gimage);
1399 if (channel == 0)
1400 goto error;
1401
1402 num_successful_elements++;
1403
1404 /* add the channel to the image if its not the selection */
1405 if (channel != gimage->selection_mask)
1406 gimp_image_add_channel (gimage, channel, -1);
1407
1408 /* restore the saved position so we'll be ready to
1409 * read the next offset.
1410 */
1411 xcf_seek_pos (info, saved_pos);
1412 }
1413#endif
1414 }
1415
1416 (void) CloseBlob(image);
1417 if (image_type == GIMP_GRAY)
1418 image->type=GrayscaleType;
1419 return(GetFirstImageInList(image));
1420}
1421
1422/*
1423%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1424% %
1425% %
1426% %
1427% R e g i s t e r X C F I m a g e %
1428% %
1429% %
1430% %
1431%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1432%
1433% RegisterXCFImage() adds attributes for the XCF image format to
1434% the list of supported formats. The attributes include the image format
1435% tag, a method to read and/or write the format, whether the format
1436% supports the saving of more than one frame to the same file or blob,
1437% whether the format supports native in-memory I/O, and a brief
1438% description of the format.
1439%
1440% The format of the RegisterXCFImage method is:
1441%
cristybb503372010-05-27 20:51:26 +00001442% size_t RegisterXCFImage(void)
cristy3ed852e2009-09-05 21:47:34 +00001443%
1444*/
cristybb503372010-05-27 20:51:26 +00001445ModuleExport size_t RegisterXCFImage(void)
cristy3ed852e2009-09-05 21:47:34 +00001446{
1447 MagickInfo
1448 *entry;
1449
dirk06b627a2015-04-06 18:59:17 +00001450 entry=AcquireMagickInfo("XCF","XCF","GIMP image");
cristy3ed852e2009-09-05 21:47:34 +00001451 entry->decoder=(DecodeImageHandler *) ReadXCFImage;
1452 entry->magick=(IsImageFormatHandler *) IsXCF;
dirk08e9a112015-02-22 01:51:41 +00001453 entry->flags|=CoderSeekableStreamFlag;
cristy3ed852e2009-09-05 21:47:34 +00001454 (void) RegisterMagickInfo(entry);
1455 return(MagickImageCoderSignature);
1456}
1457
1458/*
1459%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1460% %
1461% %
1462% %
1463% U n r e g i s t e r X C F I m a g e %
1464% %
1465% %
1466% %
1467%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1468%
1469% UnregisterXCFImage() removes format registrations made by the
1470% XCF module from the list of supported formats.
1471%
1472% The format of the UnregisterXCFImage method is:
1473%
1474% UnregisterXCFImage(void)
1475%
1476*/
1477ModuleExport void UnregisterXCFImage(void)
1478{
1479 (void) UnregisterMagickInfo("XCF");
1480}