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