]> granicus.if.org Git - imagemagick/blob - coders/xcf.c
Renamed new enumeration.
[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-2015 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 "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"
62 \f
63 /*
64   Typedef declarations.
65 */
66 typedef enum
67 {
68   GIMP_RGB,
69   GIMP_GRAY,
70   GIMP_INDEXED
71 } GimpImageBaseType;
72
73 typedef 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
102 typedef enum
103 {
104   COMPRESS_NONE              =  0,
105   COMPRESS_RLE               =  1,
106   COMPRESS_ZLIB              =  2,  /* unused */
107   COMPRESS_FRACTAL           =  3   /* unused */
108 } XcfCompressionType;
109
110 typedef struct
111 {
112   size_t
113     width,
114     height,
115     image_type,
116     bytes_per_pixel;
117
118   int
119     compression;
120
121   size_t
122     file_size;
123
124   size_t
125     number_layers;
126 } XCFDocInfo;
127
128 typedef struct
129 {
130   char
131     name[1024];
132
133   unsigned int
134     active;
135
136   size_t
137     width,
138     height,
139     type,
140     alpha,
141     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
153   size_t
154     mode,
155     tattoo;
156
157   Image
158     *image;
159 } XCFLayerInfo;
160
161 #define TILE_WIDTH   64
162 #define TILE_HEIGHT  64
163
164 typedef struct
165 {
166   unsigned char
167     red,
168     green,
169     blue,
170     alpha;
171 } XCFPixelInfo;
172 \f
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 */
199 static 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 \f
208 typedef 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 */
235 static CompositeOperator GIMPBlendModeToCompositeOperator(
236   size_t blendMode)
237 {
238   switch ( blendMode )
239   {
240     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);
257     /* these are the ones we don't support...yet */
258     case GIMP_BEHIND_MODE:       return(OverCompositeOp);
259     case GIMP_VALUE_MODE:        return(OverCompositeOp);
260     default:                     return(OverCompositeOp);
261   }
262 }
263 \f
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
276 %  starting with a ssize_t length byte and then characters to that length
277 %
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
290 static char *ReadBlobStringWithLongSize(Image *image,char *string,size_t max,
291   ExceptionInfo *exception)
292 {
293   int
294     c;
295
296   MagickOffsetType
297     offset;
298
299   register ssize_t
300     i;
301
302   size_t
303     length;
304
305   assert(image != (Image *) NULL);
306   assert(image->signature == MagickSignature);
307   assert(max != 0);
308   if (image->debug != MagickFalse)
309     (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
310   length=ReadBlobMSBLong(image);
311   for (i=0; i < (ssize_t) MagickMin(length,max-1); i++)
312   {
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)
321     (void) ThrowMagickException(exception,GetMagickModule(),
322       CorruptImageError,"ImproperImageHeader","`%s'",image->filename);
323   return(string);
324 }
325
326 static MagickBooleanType load_tile(Image *image,Image *tile_image,
327   XCFDocInfo *inDocInfo,XCFLayerInfo *inLayerInfo,size_t data_length,
328   ExceptionInfo *exception)
329 {
330   ssize_t
331     y;
332
333   register ssize_t
334     x;
335
336   register Quantum
337     *q;
338
339   ssize_t
340     count;
341
342   unsigned char
343     *graydata;
344
345   XCFPixelInfo
346     *xcfdata,
347     *xcfodata;
348
349   xcfdata=(XCFPixelInfo *) AcquireQuantumMemory(data_length,sizeof(*xcfdata));
350   if (xcfdata == (XCFPixelInfo *) NULL)
351     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);
359   for (y=0; y < (ssize_t) tile_image->rows; y++)
360   {
361     q=GetAuthenticPixels(tile_image,0,y,tile_image->columns,1,exception);
362     if (q == (Quantum *) NULL)
363       break;
364     if (inDocInfo->image_type == GIMP_GRAY)
365       {
366         for (x=0; x < (ssize_t) tile_image->columns; x++)
367         {
368           SetPixelGray(tile_image,ScaleCharToQuantum(*graydata),q);
369           SetPixelAlpha(tile_image,ScaleCharToQuantum((unsigned char)
370             inLayerInfo->alpha),q);
371           graydata++;
372           q+=GetPixelChannels(tile_image);
373         }
374       }
375     else
376       if (inDocInfo->image_type == GIMP_RGB)
377         {
378           for (x=0; x < (ssize_t) tile_image->columns; x++)
379           {
380             SetPixelRed(tile_image,ScaleCharToQuantum(xcfdata->red),q);
381             SetPixelGreen(tile_image,ScaleCharToQuantum(xcfdata->green),q);
382             SetPixelBlue(tile_image,ScaleCharToQuantum(xcfdata->blue),q);
383             SetPixelAlpha(tile_image,xcfdata->alpha == 255U ? TransparentAlpha :
384               ScaleCharToQuantum((unsigned char) inLayerInfo->alpha),q);
385             xcfdata++;
386             q+=GetPixelChannels(tile_image);
387           }
388         }
389      if (SyncAuthenticPixels(tile_image,exception) == MagickFalse)
390        break;
391   }
392   xcfodata=(XCFPixelInfo *) RelinquishMagickMemory(xcfodata);
393   return MagickTrue;
394 }
395
396 static MagickBooleanType load_tile_rle(Image *image,Image *tile_image,
397   XCFDocInfo *inDocInfo,XCFLayerInfo *inLayerInfo,size_t data_length,
398   ExceptionInfo *exception)
399 {
400   MagickOffsetType
401     size;
402
403   Quantum
404     alpha;
405
406   register Quantum
407     *q;
408
409   size_t
410     length;
411
412   ssize_t
413     bytes_per_pixel,
414     count,
415     i,
416     j;
417
418   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;
433   alpha=ScaleCharToQuantum((unsigned char) inLayerInfo->alpha);
434   for (i=0; i < (ssize_t) bytes_per_pixel; i++)
435   {
436     q=GetAuthenticPixels(tile_image,0,0,tile_image->columns,tile_image->rows,
437       exception);
438     if (q == (Quantum *) NULL)
439       continue;
440     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             }
457           size-=length;
458           if (size < 0)
459             goto bogus_rle;
460           if (&xcfdata[length-1] > xcfdatalimit)
461             goto bogus_rle;
462           while (length-- > 0)
463           {
464             data=(*xcfdata++);
465             switch (i)
466             {
467               case 0:
468               {
469                 if (inDocInfo->image_type == GIMP_GRAY)
470                   SetPixelGray(tile_image,ScaleCharToQuantum(data),q);
471                 else
472                   {
473                     SetPixelRed(tile_image,ScaleCharToQuantum(data),q);
474                     SetPixelGreen(tile_image,ScaleCharToQuantum(data),q);
475                     SetPixelBlue(tile_image,ScaleCharToQuantum(data),q);
476                   }
477                 SetPixelAlpha(tile_image,alpha,q);
478                 break;
479               }
480               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               }
498             }
499             q+=GetPixelChannels(tile_image);
500           }
501         }
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         }
558     }
559     if (SyncAuthenticPixels(tile_image,exception) == MagickFalse)
560       break;
561   }
562   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
571 static MagickBooleanType load_level(Image *image,XCFDocInfo *inDocInfo,
572   XCFLayerInfo *inLayerInfo,ExceptionInfo *exception)
573 {
574   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
589   register ssize_t
590     i;
591
592   size_t
593     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 */
602   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;
617   for (i = 0; i < (ssize_t) ntiles; i++)
618   {
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
636       /*
637         Allocate the image for the tile.  NOTE: the last tile in a row or
638         column may not be a full tile!
639       */
640       tile_image_width=(size_t) (destLeft == (int) ntile_cols-1 ?
641         (int) width % TILE_WIDTH : TILE_WIDTH);
642       if (tile_image_width == 0)
643         tile_image_width=TILE_WIDTH;
644       tile_image_height = (size_t) (destTop == (int) ntile_rows-1 ?
645         (int) height % TILE_HEIGHT : TILE_HEIGHT);
646       if (tile_image_height == 0)
647         tile_image_height=TILE_HEIGHT;
648       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:
655           if (load_tile(image,tile_image,inDocInfo,inLayerInfo,(size_t) (offset2-offset),exception) == 0)
656             status=MagickTrue;
657           break;
658         case COMPRESS_RLE:
659           if (load_tile_rle (image,tile_image,inDocInfo,inLayerInfo,
660               (int) (offset2-offset),exception) == 0)
661             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 */
672       (void) CompositeImage(inLayerInfo->image,tile_image,CopyCompositeOp,
673         MagickTrue,destLeft * TILE_WIDTH,destTop*TILE_HEIGHT,exception);
674       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
697 static MagickBooleanType load_hierarchy(Image *image,XCFDocInfo *inDocInfo,
698    XCFLayerInfo *inLayer, ExceptionInfo *exception)
699 {
700   MagickOffsetType
701     saved_pos,
702     offset,
703     junk;
704
705   size_t
706     width,
707     height,
708     bytes_per_pixel;
709
710   width=ReadBlobMSBLong(image);
711   (void) width;
712   height=ReadBlobMSBLong(image);
713   (void) height;
714   bytes_per_pixel=inDocInfo->bytes_per_pixel=ReadBlobMSBLong(image);
715   (void) bytes_per_pixel;
716
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 */
740   if (load_level (image, inDocInfo, inLayer, exception) == 0)
741     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
749 static MagickBooleanType ReadOneLayer(const ImageInfo *image_info,Image* image,
750   XCFDocInfo* inDocInfo,XCFLayerInfo *outLayer,const ssize_t layer,
751   ExceptionInfo *exception )
752 {
753   MagickOffsetType
754     offset;
755
756   unsigned int
757     foundPropEnd = 0;
758
759   size_t
760     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,
770     sizeof(outLayer->name),exception);
771   /* read the layer properties! */
772   foundPropEnd = 0;
773   while ( (foundPropEnd == MagickFalse) && (EOFBlob(image) == MagickFalse) ) {
774   PropType    prop_type = (PropType) ReadBlobMSBLong(image);
775   size_t  prop_size = ReadBlobMSBLong(image);
776     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:
788       outLayer->alpha = ReadBlobMSBLong(image);
789       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:
809       outLayer->offset_x = (int) ReadBlobMSBLong(image);
810       outLayer->offset_y = (int) ReadBlobMSBLong(image);
811       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      {
820        if (DiscardBlobBytes(image,prop_size) == MagickFalse)
821          ThrowFileException(exception,CorruptImageError,
822            "UnexpectedEndOfFile",image->filename);
823
824         /*
825        ssize_t base = info->cp;
826        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);
854         prop_size -= (size_t) MagickMin(16, (size_t) amount);
855         }
856       }
857       break;
858     }
859   }
860
861   if (foundPropEnd == MagickFalse)
862     return(MagickFalse);
863   /* 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;
870       if (scene > (ssize_t) (image_info->scene+image_info->number_scenes-1))
871         {
872           outLayer->image=CloneImage(image,0,0,MagickTrue,exception);
873           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;
879           return(MagickTrue);
880         }
881     }
882   outLayer->image=CloneImage(image,outLayer->width, outLayer->height,MagickTrue,
883     exception);
884   if (outLayer->image == (Image *) NULL)
885     return(MagickFalse);
886   /* clear the image based on the layer opacity */
887   outLayer->image->background_color.alpha=
888     ScaleCharToQuantum((unsigned char) outLayer->alpha);
889   (void) SetImageBackgroundColor(outLayer->image,exception);
890
891   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;
895   /* 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)
910     (void) ThrowMagickException(exception,GetMagickModule(),
911       CorruptImageError,"InvalidImageHeader","`%s'",image->filename);
912   if (load_hierarchy (image, inDocInfo, outLayer, exception) == 0)
913     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 }
950 \f
951 /*
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 %
976 */
977 static 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
994   register ssize_t
995     i;
996
997   size_t
998     image_type,
999     length;
1000
1001   ssize_t
1002     count;
1003
1004   XCFDocInfo
1005     doc_info;
1006
1007   /*
1008     Open image file.
1009   */
1010   assert(image_info != (const ImageInfo *) NULL);
1011   assert(image_info->signature == MagickSignature);
1012   if (image_info->debug != MagickFalse)
1013     (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
1014       image_info->filename);
1015   assert(exception != (ExceptionInfo *) NULL);
1016   assert(exception->signature == MagickSignature);
1017   image=AcquireImage(image_info,exception);
1018   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);
1025   if ((count != 14) ||
1026       (LocaleNCompare((char *) magick,"gimp xcf",8) != 0))
1027     ThrowReaderException(CorruptImageError,"ImproperImageHeader");
1028   (void) ResetMagickMemory(&doc_info,0,sizeof(XCFDocInfo));
1029   doc_info.width=ReadBlobMSBLong(image);
1030   doc_info.height=ReadBlobMSBLong(image);
1031   if ((doc_info.width > 262144) || (doc_info.height > 262144))
1032     ThrowReaderException(CorruptImageError,"ImproperImageHeader");
1033   doc_info.image_type=ReadBlobMSBLong(image);
1034   /*
1035     Initialize image attributes.
1036   */
1037   image->columns=doc_info.width;
1038   image->rows=doc_info.height;
1039   image_type=doc_info.image_type;
1040   doc_info.file_size=GetBlobSize(image);
1041   image->compression=NoCompression;
1042   image->depth=8;
1043   if (image_type == GIMP_RGB)
1044     SetImageColorspace(image,sRGBColorspace,exception);
1045   else
1046     if (image_type == GIMP_GRAY)
1047       SetImageColorspace(image,GRAYColorspace,exception);
1048     else
1049       if (image_type == GIMP_INDEXED)
1050         ThrowReaderException(CoderError,"ColormapTypeNotSupported");
1051   (void) SetImageBackgroundColor(image,exception);
1052   (void) SetImageAlpha(image,OpaqueAlpha,exception);
1053   /*
1054     Read properties.
1055   */
1056   while ((foundPropEnd == MagickFalse) && (EOFBlob(image) == MagickFalse))
1057   {
1058     PropType prop_type = (PropType) ReadBlobMSBLong(image);
1059     size_t prop_size = ReadBlobMSBLong(image);
1060
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         */
1071         size_t num_colours = ReadBlobMSBLong(image);
1072         if (DiscardBlobBytes(image,3*num_colours) == MagickFalse)
1073           ThrowFileException(exception,CorruptImageError,
1074             "UnexpectedEndOfFile",image->filename);
1075     /*
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 */
1120         if (DiscardBlobBytes(image,prop_size) == MagickFalse)
1121           ThrowFileException(exception,CorruptImageError,
1122             "UnexpectedEndOfFile",image->filename);
1123       }
1124       break;
1125
1126     case PROP_RESOLUTION:
1127       {
1128         /* float xres = (float) */ (void) ReadBlobMSBLong(image);
1129         /* float yres = (float) */ (void) ReadBlobMSBLong(image);
1130
1131         /*
1132         if (xres < GIMP_MIN_RESOLUTION || xres > GIMP_MAX_RESOLUTION ||
1133             yres < GIMP_MIN_RESOLUTION || yres > GIMP_MAX_RESOLUTION)
1134         {
1135         g_message ("Warning, resolution out of range in XCF file");
1136         xres = gimage->gimp->config->default_xresolution;
1137         yres = gimage->gimp->config->default_yresolution;
1138         }
1139         */
1140
1141
1142         /* BOGUS: we don't write these yet because we aren't
1143               reading them properly yet :(
1144               image->resolution.x = xres;
1145               image->resolution.y = yres;
1146         */
1147       }
1148       break;
1149
1150     case PROP_TATTOO:
1151       {
1152         /* we need to read it, even if we ignore it */
1153         /*size_t  tattoo_state = */ (void) ReadBlobMSBLong(image);
1154       }
1155       break;
1156
1157     case PROP_PARASITES:
1158       {
1159         /* BOGUS: we may need these for IPTC stuff */
1160         if (DiscardBlobBytes(image,prop_size) == MagickFalse)
1161           ThrowFileException(exception,CorruptImageError,
1162             "UnexpectedEndOfFile",image->filename);
1163         /*
1164       gssize_t         base = info->cp;
1165       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... */
1182       /*size_t unit =  */ (void) ReadBlobMSBLong(image);
1183       }
1184       break;
1185
1186     case PROP_PATHS:
1187       {
1188       /* BOGUS: just skip it for now */
1189         if (DiscardBlobBytes(image,prop_size) == MagickFalse)
1190           ThrowFileException(exception,CorruptImageError,"UnexpectedEndOfFile",
1191             image->filename);
1192
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);
1205         /* size_t digits =  */ (void) ReadBlobMSBLong(image);
1206         for (i=0; i<5; i++)
1207          (void) ReadBlobStringWithLongSize(image, unit_string,
1208            sizeof(unit_string),exception);
1209       }
1210      break;
1211
1212       default:
1213       {
1214         int buf[16];
1215         ssize_t amount;
1216
1217       /* read over it... */
1218       while ((prop_size > 0) && (EOFBlob(image) == MagickFalse))
1219       {
1220         amount=(ssize_t) MagickMin(16, prop_size);
1221         amount=(ssize_t) ReadBlob(image,(size_t) amount,(unsigned char *) &buf);
1222         if (!amount)
1223           ThrowReaderException(CorruptImageError,"CorruptImage");
1224         prop_size -= (size_t) MagickMin(16,(size_t) amount);
1225       }
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,
1241         foundAllLayers = MagickFalse,
1242         number_layers = 0;
1243
1244       MagickOffsetType
1245         oldPos=TellBlob(image);
1246
1247       XCFLayerInfo
1248         *layer_info;
1249
1250       status=SetImageExtent(image,image->columns,image->rows,exception);
1251       if (status == MagickFalse)
1252         return(DestroyImageList(image));
1253       /* 
1254         the read pointer
1255       */
1256       do
1257       {
1258         ssize_t offset = (int) ReadBlobMSBLong(image);
1259         if (offset == 0)
1260           foundAllLayers=MagickTrue;
1261         else
1262           number_layers++;
1263         if (EOFBlob(image) != MagickFalse)
1264           {
1265             ThrowFileException(exception,CorruptImageError,
1266               "UnexpectedEndOfFile",image->filename);
1267             break;
1268           }
1269     } while (foundAllLayers == MagickFalse);
1270     doc_info.number_layers=number_layers;
1271     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);
1301       /* seek to the layer offset */
1302       offset=SeekBlob(image,offset,SEEK_SET);
1303       /* read in the layer */
1304       layer_ok=ReadOneLayer(image_info,image,&doc_info,
1305         &layer_info[current_layer],current_layer,exception);
1306       if (layer_ok == MagickFalse)
1307         {
1308           int j;
1309
1310           for (j=0; j < current_layer; j++)
1311             layer_info[j].image=DestroyImage(layer_info[j].image);
1312           layer_info=(XCFLayerInfo *) RelinquishMagickMemory(layer_info);
1313           ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
1314         }
1315       /* 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     }
1321     if (number_layers == 1)
1322       {
1323         /*
1324           Composite the layer data onto the main image, dispose the layer.
1325         */
1326         (void) CompositeImage(image,layer_info[0].image,CopyCompositeOp,
1327           MagickTrue,layer_info[0].offset_x,layer_info[0].offset_y,exception);
1328         layer_info[0].image =DestroyImage( layer_info[0].image);
1329       }
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! */
1340             CompositeImage(image, OverCompositeOp, layer_info[j].image,
1341                      layer_info[j].offset_x, layer_info[j].offset_y );
1342              layer_info[j].image =DestroyImage( layer_info[j].image );
1343
1344             /* If we do this, we'll get REAL gray images! */
1345             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! */
1357         ssize_t  j;
1358
1359         /* first we copy the last layer on top of the main image */
1360         (void) CompositeImage(image,layer_info[number_layers-1].image,
1361           CopyCompositeOp,MagickTrue,layer_info[number_layers-1].offset_x,
1362           layer_info[number_layers-1].offset_y,exception);
1363           layer_info[number_layers-1].image=DestroyImage(
1364             layer_info[number_layers-1].image);
1365
1366         /* now reverse the order of the layers as they are put
1367            into subimages
1368         */
1369         for (j=(ssize_t) number_layers-2; j >= 0; j--)
1370           AppendImageToList(&image,layer_info[j].image);
1371       }
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 \f
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 %
1442 %      size_t RegisterXCFImage(void)
1443 %
1444 */
1445 ModuleExport size_t RegisterXCFImage(void)
1446 {
1447   MagickInfo
1448     *entry;
1449
1450   entry=SetMagickInfo("XCF");
1451   entry->decoder=(DecodeImageHandler *) ReadXCFImage;
1452   entry->magick=(IsImageFormatHandler *) IsXCF;
1453   entry->description=ConstantString("GIMP image");
1454   entry->module=ConstantString("XCF");
1455   entry->flags|=CoderSeekableStreamFlag;
1456   (void) RegisterMagickInfo(entry);
1457   return(MagickImageCoderSignature);
1458 }
1459 \f
1460 /*
1461 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1462 %                                                                             %
1463 %                                                                             %
1464 %                                                                             %
1465 %   U n r e g i s t e r X C F I m a g e                                       %
1466 %                                                                             %
1467 %                                                                             %
1468 %                                                                             %
1469 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1470 %
1471 %  UnregisterXCFImage() removes format registrations made by the
1472 %  XCF module from the list of supported formats.
1473 %
1474 %  The format of the UnregisterXCFImage method is:
1475 %
1476 %      UnregisterXCFImage(void)
1477 %
1478 */
1479 ModuleExport void UnregisterXCFImage(void)
1480 {
1481   (void) UnregisterMagickInfo("XCF");
1482 }