2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
13 % Read GIMP XCF Image Format %
20 % Copyright 1999-2011 ImageMagick Studio LLC, a non-profit organization %
21 % dedicated to making software imaging solutions freely available. %
23 % You may not use this file except in compliance with the License. You may %
24 % obtain a copy of the License at %
26 % http://www.imagemagick.org/script/license.php %
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. %
34 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
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"
75 PROP_ACTIVE_LAYER = 2,
76 PROP_ACTIVE_CHANNEL = 3,
78 PROP_FLOATING_SELECTION = 5,
83 PROP_PRESERVE_TRANSPARENCY = 10,
87 PROP_SHOW_MASKED = 14,
90 PROP_COMPRESSION = 17,
104 COMPRESS_ZLIB = 2, /* unused */
105 COMPRESS_FRACTAL = 3 /* unused */
106 } XcfCompressionType;
159 #define TILE_WIDTH 64
160 #define TILE_HEIGHT 64
172 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
180 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
182 % IsXCF() returns MagickTrue if the image format type, identified by the
183 % magick string, is XCF (GIMP native format).
185 % The format of the IsXCF method is:
187 % MagickBooleanType IsXCF(const unsigned char *magick,const size_t length)
189 % A description of each parameter follows:
191 % o magick: compare image format pattern against these bytes.
193 % o length: Specifies the length of the magick string.
197 static MagickBooleanType IsXCF(const unsigned char *magick,const size_t length)
201 if (LocaleNCompare((char *) magick,"gimp xcf",8) == 0)
214 GIMP_DIFFERENCE_MODE,
217 GIMP_DARKEN_ONLY_MODE,
218 GIMP_LIGHTEN_ONLY_MODE,
220 GIMP_SATURATION_MODE,
227 } GimpLayerModeEffects;
230 Simple utility routine to convert between PSD blending modes and
231 ImageMagick compositing operators
233 static CompositeOperator GIMPBlendModeToCompositeOperator(
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 );
259 return(OverCompositeOp);
264 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
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 %
272 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
274 % ReadBlobStringWithLongSize reads characters from a blob or file
275 % starting with a ssize_t length byte and then characters to that length
277 % The format of the ReadBlobStringWithLongSize method is:
279 % char *ReadBlobStringWithLongSize(Image *image,char *string)
281 % A description of each parameter follows:
283 % o image: the image.
285 % o string: the address of a character buffer.
289 static inline size_t MagickMin(const size_t x,const size_t y)
296 static char *ReadBlobStringWithLongSize(Image *image,char *string,size_t max)
310 assert(image != (Image *) NULL);
311 assert(image->signature == MagickSignature);
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++)
318 c=ReadBlobByte(image);
320 return((char *) NULL);
324 offset=SeekBlob(image,(MagickOffsetType) (length-i),SEEK_CUR);
326 (void) ThrowMagickException(&image->exception,GetMagickModule(),
327 CorruptImageError,"ImproperImageHeader","`%s'",image->filename);
331 static MagickBooleanType load_tile(Image *image,Image *tile_image,
332 XCFDocInfo *inDocInfo,XCFLayerInfo *inLayerInfo,size_t data_length)
356 xcfdata=(XCFPixelPacket *) AcquireQuantumMemory(data_length,sizeof(*xcfdata));
357 if (xcfdata == (XCFPixelPacket *) NULL)
358 ThrowBinaryException(ResourceLimitError,"MemoryAllocationFailed",
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",
366 exception=(&image->exception);
367 for (y=0; y < (ssize_t) tile_image->rows; y++)
369 q=QueueAuthenticPixels(tile_image,0,y,tile_image->columns,1,exception);
370 if (q == (PixelPacket *) NULL)
372 if (inDocInfo->image_type == GIMP_GRAY)
374 for (x=0; x < (ssize_t) tile_image->columns; x++)
376 q->red=ScaleCharToQuantum(*graydata);
377 SetGreenPixelComponent(q,GetRedPixelComponent(q));
378 SetBluePixelComponent(q,GetRedPixelComponent(q));
379 q->opacity=ScaleCharToQuantum((unsigned char) (255-
380 inLayerInfo->opacity));
386 if (inDocInfo->image_type == GIMP_RGB)
388 for (x=0; x < (ssize_t) tile_image->columns; x++)
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)));
399 if (SyncAuthenticPixels(tile_image,exception) == MagickFalse)
402 xcfodata=(XCFPixelPacket *) RelinquishMagickMemory(xcfodata);
406 static MagickBooleanType load_tile_rle(Image *image,Image *tile_image,
407 XCFDocInfo *inDocInfo,XCFLayerInfo *inLayerInfo,size_t data_length)
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",
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++)
447 q=GetAuthenticPixels(tile_image,0,0,tile_image->columns,tile_image->rows,
449 size=(MagickOffsetType) tile_image->rows*tile_image->columns;
452 if (xcfdata > xcfdatalimit)
455 length=(size_t) pixel;
458 length=255-(length-1);
461 if (xcfdata >= xcfdatalimit)
463 length=(size_t) ((*xcfdata << 8) + xcfdata[1]);
469 if (&xcfdata[length-1] > xcfdatalimit)
478 q->red=ScaleCharToQuantum(data);
479 if (inDocInfo->image_type == GIMP_GRAY)
481 q->green=ScaleCharToQuantum(data);
482 q->blue=ScaleCharToQuantum(data);
483 q->opacity=ScaleCharToQuantum((unsigned char) (255-
484 inLayerInfo->opacity));
490 q->opacity=ScaleCharToQuantum((unsigned char) (255-
491 inLayerInfo->opacity));
497 q->green=ScaleCharToQuantum(data);
502 q->blue=ScaleCharToQuantum(data);
507 q->opacity=(Quantum) (data == 0 ? TransparentOpacity :
508 ScaleCharToQuantum((unsigned char) (255-
509 inLayerInfo->opacity)));
521 if (xcfdata >= xcfdatalimit)
523 length=(size_t) ((*xcfdata << 8) + xcfdata[1]);
529 if (xcfdata > xcfdatalimit)
532 for (j= 0; j < (ssize_t) length; j++)
539 q->red=ScaleCharToQuantum(data);
540 if (inDocInfo->image_type == GIMP_GRAY)
542 q->green=ScaleCharToQuantum(data);
543 q->blue=ScaleCharToQuantum(data);
544 q->opacity=ScaleCharToQuantum((unsigned char) (255-
545 inLayerInfo->opacity));
549 SetGreenPixelComponent(q,GetRedPixelComponent(q));
550 SetBluePixelComponent(q,GetRedPixelComponent(q));
551 q->opacity=ScaleCharToQuantum((unsigned char) (255-
552 inLayerInfo->opacity));
558 q->green=ScaleCharToQuantum(data);
563 q->blue=ScaleCharToQuantum(data);
568 q->opacity=(Quantum) (data == 0 ? TransparentOpacity :
569 ScaleCharToQuantum((unsigned char) (255-
570 inLayerInfo->opacity)));
578 if (SyncAuthenticPixels(tile_image,exception) == MagickFalse)
581 xcfodata=(unsigned char *) RelinquishMagickMemory(xcfodata);
585 if (xcfodata != (unsigned char *) NULL)
586 xcfodata=(unsigned char *) RelinquishMagickMemory(xcfodata);
590 static MagickBooleanType load_level(Image *image,XCFDocInfo *inDocInfo,
591 XCFLayerInfo *inLayerInfo)
623 /* start reading the data */
624 exception=inDocInfo->exception;
625 width=ReadBlobMSBLong(image);
626 height=ReadBlobMSBLong(image);
628 /* read in the first tile offset.
629 * if it is '0', then this tile level is empty
630 * and we can simply return.
632 offset=(MagickOffsetType) ReadBlobMSBLong(image);
635 /* Initialise the reference for the in-memory tile-compression
637 ntile_rows=(height+TILE_HEIGHT-1)/TILE_HEIGHT;
638 ntile_cols=(width+TILE_WIDTH-1)/TILE_WIDTH;
639 ntiles=ntile_rows*ntile_cols;
640 for (i = 0; i < (ssize_t) ntiles; i++)
644 ThrowBinaryException(CorruptImageError,"NotEnoughTiles",image->filename);
645 /* save the current position as it is where the
646 * next tile offset is stored.
648 saved_pos=TellBlob(image);
649 /* read in the offset of the next tile so we can calculate the amount
650 of data needed for this tile*/
651 offset2=(MagickOffsetType)ReadBlobMSBLong(image);
652 /* if the offset is 0 then we need to read in the maximum possible
653 allowing for negative compression */
655 offset2=(MagickOffsetType) (offset + TILE_WIDTH * TILE_WIDTH * 4* 1.5);
656 /* seek to the tile offset */
657 offset=SeekBlob(image, offset, SEEK_SET);
659 /* allocate the image for the tile
660 NOTE: the last tile in a row or column may not be a full tile!
662 tile_image_width=(size_t) (destLeft == (int) ntile_cols-1 ?
663 (int) width % TILE_WIDTH : TILE_WIDTH);
664 if (tile_image_width == 0) tile_image_width=TILE_WIDTH;
665 tile_image_height = (size_t) (destTop == (int) ntile_rows-1 ?
666 (int) height % TILE_HEIGHT : TILE_HEIGHT);
667 if (tile_image_height == 0) tile_image_height=TILE_HEIGHT;
668 tile_image=CloneImage(inLayerInfo->image,tile_image_width,
669 tile_image_height,MagickTrue,exception);
671 /* read in the tile */
672 switch (inDocInfo->compression)
675 if (load_tile(image,tile_image,inDocInfo,inLayerInfo,(size_t) (offset2-offset)) == 0)
679 if (load_tile_rle (image,tile_image,inDocInfo,inLayerInfo,
680 (int) (offset2-offset)) == 0)
684 ThrowBinaryException(CoderError,"ZipCompressNotSupported",
686 case COMPRESS_FRACTAL:
687 ThrowBinaryException(CoderError,"FractalCompressNotSupported",
691 /* composite the tile onto the layer's image, and then destroy it */
692 (void) CompositeImage(inLayerInfo->image,CopyCompositeOp,tile_image,
693 destLeft * TILE_WIDTH,destTop*TILE_HEIGHT);
694 tile_image=DestroyImage(tile_image);
696 /* adjust tile position */
698 if (destLeft >= (int) ntile_cols)
703 if (status != MagickFalse)
705 /* restore the saved position so we'll be ready to
706 * read the next offset.
708 offset=SeekBlob(image, saved_pos, SEEK_SET);
709 /* read in the offset of the next tile */
710 offset=(MagickOffsetType) ReadBlobMSBLong(image);
713 ThrowBinaryException(CorruptImageError,"CorruptImage",image->filename)
717 static MagickBooleanType load_hierarchy(Image *image,XCFDocInfo *inDocInfo,
718 XCFLayerInfo *inLayer)
730 width=ReadBlobMSBLong(image);
732 height=ReadBlobMSBLong(image);
734 bytes_per_pixel=inDocInfo->bytes_per_pixel=ReadBlobMSBLong(image);
735 (void) bytes_per_pixel;
737 /* load in the levels...we make sure that the number of levels
738 * calculated when the TileManager was created is the same
739 * as the number of levels found in the file.
741 offset=(MagickOffsetType) ReadBlobMSBLong(image); /* top level */
743 /* discard offsets for layers below first, if any.
747 junk=(MagickOffsetType) ReadBlobMSBLong(image);
751 /* save the current position as it is where the
752 * next level offset is stored.
754 saved_pos=TellBlob(image);
756 /* seek to the level offset */
757 offset=SeekBlob(image, offset, SEEK_SET);
759 /* read in the level */
760 if (load_level (image, inDocInfo, inLayer) == 0)
762 /* restore the saved position so we'll be ready to
763 * read the next offset.
765 offset=SeekBlob(image, saved_pos, SEEK_SET);
769 static MagickBooleanType ReadOneLayer(Image* image,XCFDocInfo* inDocInfo,
770 XCFLayerInfo *outLayer )
782 /* clear the block! */
783 (void) ResetMagickMemory( outLayer, 0, sizeof( XCFLayerInfo ) );
784 /* read in the layer width, height, type and name */
785 outLayer->width = ReadBlobMSBLong(image);
786 outLayer->height = ReadBlobMSBLong(image);
787 outLayer->type = ReadBlobMSBLong(image);
788 (void) ReadBlobStringWithLongSize(image, outLayer->name,
789 sizeof(outLayer->name));
790 /* allocate the image for this layer */
791 outLayer->image=CloneImage(image,outLayer->width, outLayer->height,MagickTrue,
793 if (outLayer->image == (Image *) NULL)
795 /* read the layer properties! */
797 while ( (foundPropEnd == MagickFalse) && (EOFBlob(image) == MagickFalse) ) {
798 PropType prop_type = (PropType) ReadBlobMSBLong(image);
799 size_t prop_size = ReadBlobMSBLong(image);
805 case PROP_ACTIVE_LAYER:
806 outLayer->active = 1;
808 case PROP_FLOATING_SELECTION:
809 outLayer->floating_offset = ReadBlobMSBLong(image);
812 outLayer->opacity = ReadBlobMSBLong(image);
815 outLayer->visible = ReadBlobMSBLong(image);
818 outLayer->linked = ReadBlobMSBLong(image);
820 case PROP_PRESERVE_TRANSPARENCY:
821 outLayer->preserve_trans = ReadBlobMSBLong(image);
823 case PROP_APPLY_MASK:
824 outLayer->apply_mask = ReadBlobMSBLong(image);
827 outLayer->edit_mask = ReadBlobMSBLong(image);
830 outLayer->show_mask = ReadBlobMSBLong(image);
833 outLayer->offset_x = (int) ReadBlobMSBLong(image);
834 outLayer->offset_y = (int) ReadBlobMSBLong(image);
837 outLayer->mode = ReadBlobMSBLong(image);
840 outLayer->preserve_trans = ReadBlobMSBLong(image);
844 if (DiscardBlobBytes(image,prop_size) == MagickFalse)
845 ThrowFileException(&image->exception,CorruptImageError,
846 "UnexpectedEndOfFile",image->filename);
849 ssize_t base = info->cp;
851 while (info->cp - base < prop_size)
853 p = xcf_load_parasite(info);
854 gimp_drawable_parasite_attach(GIMP_DRAWABLE(layer), p);
855 gimp_parasite_free(p);
857 if (info->cp - base != prop_size)
858 g_message ("Error detected while loading a layer's parasites");
863 /* g_message ("unexpected/unknown layer property: %d (skipping)",
870 /* read over it... */
871 while ((prop_size > 0) && (EOFBlob(image) == MagickFalse))
873 amount = (ssize_t) MagickMin(16, prop_size);
874 amount = ReadBlob(image, (size_t) amount, (unsigned char *) &buf);
876 ThrowBinaryException(CorruptImageError,"CorruptImage",
878 prop_size -= (size_t) MagickMin(16, (size_t) amount);
885 if (foundPropEnd == MagickFalse)
887 /* clear the image based on the layer opacity */
888 outLayer->image->background_color.opacity=
889 ScaleCharToQuantum((unsigned char) (255-outLayer->opacity));
890 (void) SetImageBackgroundColor(outLayer->image);
892 /* set the compositing mode */
893 outLayer->image->compose = GIMPBlendModeToCompositeOperator( outLayer->mode );
894 if ( outLayer->visible == MagickFalse )
896 /* BOGUS: should really be separate member var! */
897 outLayer->image->compose = NoCompositeOp;
900 /* read the hierarchy and layer mask offsets */
901 hierarchy_offset = ReadBlobMSBLong(image);
902 layer_mask_offset = ReadBlobMSBLong(image);
904 /* read in the hierarchy */
905 offset=SeekBlob(image, (MagickOffsetType) hierarchy_offset, SEEK_SET);
907 (void) ThrowMagickException(&image->exception,GetMagickModule(),
908 CorruptImageError,"InvalidImageHeader","`%s'",image->filename);
909 if (load_hierarchy (image, inDocInfo, outLayer) == 0)
912 /* read in the layer mask */
913 if (layer_mask_offset != 0)
915 offset=SeekBlob(image, (MagickOffsetType) layer_mask_offset, SEEK_SET);
917 #if 0 /* BOGUS: support layer masks! */
918 layer_mask = xcf_load_layer_mask (info, gimage);
922 /* set the offsets of the layer_mask */
923 GIMP_DRAWABLE (layer_mask)->offset_x = GIMP_DRAWABLE (layer)->offset_x;
924 GIMP_DRAWABLE (layer_mask)->offset_y = GIMP_DRAWABLE (layer)->offset_y;
926 gimp_layer_add_mask (layer, layer_mask, MagickFalse);
928 layer->mask->apply_mask = apply_mask;
929 layer->mask->edit_mask = edit_mask;
930 layer->mask->show_mask = show_mask;
934 /* attach the floating selection... */
935 #if 0 /* BOGUS: we may need to read this, even if we don't support it! */
936 if (add_floating_sel)
938 GimpLayer *floating_sel;
940 floating_sel = info->floating_sel;
941 floating_sel_attach (floating_sel, GIMP_DRAWABLE (layer));
949 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
953 % R e a d X C F I m a g e %
957 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
959 % ReadXCFImage() reads a GIMP (GNU Image Manipulation Program) image
960 % file and returns it. It allocates the memory necessary for the new Image
961 % structure and returns a pointer to the new image.
963 % The format of the ReadXCFImage method is:
965 % image=ReadXCFImage(image_info)
967 % A description of each parameter follows:
969 % o image_info: the image info.
971 % o exception: return any errors or warnings in this structure.
975 static Image *ReadXCFImage(const ImageInfo *image_info,ExceptionInfo *exception)
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);
1018 status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception);
1019 if (status == MagickFalse)
1021 image=DestroyImageList(image);
1022 return((Image *) NULL);
1024 count=ReadBlob(image,14,(unsigned char *) magick);
1026 (LocaleNCompare((char *) magick,"gimp xcf",8) != 0))
1027 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
1028 (void) ResetMagickMemory(&doc_info,0,sizeof(XCFDocInfo));
1029 doc_info.exception=exception;
1030 doc_info.width=ReadBlobMSBLong(image);
1031 doc_info.height=ReadBlobMSBLong(image);
1032 if ((doc_info.width > 262144) || (doc_info.height > 262144))
1033 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
1034 doc_info.image_type=ReadBlobMSBLong(image);
1036 Initialize image attributes.
1038 image->columns=doc_info.width;
1039 image->rows=doc_info.height;
1040 image_type=doc_info.image_type;
1041 doc_info.file_size=GetBlobSize(image);
1042 image->compression=NoCompression;
1044 if (image_type == GIMP_RGB)
1045 image->colorspace=RGBColorspace;
1047 if (image_type == GIMP_GRAY)
1048 image->colorspace=GRAYColorspace;
1050 if (image_type == GIMP_INDEXED)
1051 ThrowReaderException(CoderError,"ColormapTypeNotSupported");
1052 (void) SetImageBackgroundColor(image);
1053 image->matte=MagickTrue;
1057 while ((foundPropEnd == MagickFalse) && (EOFBlob(image) == MagickFalse))
1059 PropType prop_type = (PropType) ReadBlobMSBLong(image);
1060 size_t prop_size = ReadBlobMSBLong(image);
1069 /* Cannot rely on prop_size here--the value is set incorrectly
1070 by some Gimp versions.
1072 size_t num_colours = ReadBlobMSBLong(image);
1073 if (DiscardBlobBytes(image,3*num_colours) == MagickFalse)
1074 ThrowFileException(&image->exception,CorruptImageError,
1075 "UnexpectedEndOfFile",image->filename);
1077 if (info->file_version == 0)
1081 g_message (_("XCF warning: version 0 of XCF file format\n"
1082 "did not save indexed colormaps correctly.\n"
1083 "Substituting grayscale map."));
1085 xcf_read_int32 (info->fp, (guint32*) &gimage->num_cols, 1);
1086 gimage->cmap = g_new (guchar, gimage->num_cols*3);
1087 xcf_seek_pos (info, info->cp + gimage->num_cols);
1088 for (i = 0; i<gimage->num_cols; i++)
1090 gimage->cmap[i*3+0] = i;
1091 gimage->cmap[i*3+1] = i;
1092 gimage->cmap[i*3+2] = i;
1098 xcf_read_int32 (info->fp, (guint32*) &gimage->num_cols, 1);
1099 gimage->cmap = g_new (guchar, gimage->num_cols*3);
1101 xcf_read_int8 (info->fp,
1102 (guint8*) gimage->cmap, gimage->num_cols*3);
1107 case PROP_COMPRESSION:
1109 doc_info.compression = ReadBlobByte(image);
1110 if ((doc_info.compression != COMPRESS_NONE) &&
1111 (doc_info.compression != COMPRESS_RLE) &&
1112 (doc_info.compression != COMPRESS_ZLIB) &&
1113 (doc_info.compression != COMPRESS_FRACTAL))
1114 ThrowReaderException(CorruptImageError,"UnrecognizedImageCompression");
1120 /* just skip it - we don't care about guides */
1121 if (DiscardBlobBytes(image,prop_size) == MagickFalse)
1122 ThrowFileException(&image->exception,CorruptImageError,
1123 "UnexpectedEndOfFile",image->filename);
1127 case PROP_RESOLUTION:
1129 /* float xres = (float) */ (void) ReadBlobMSBLong(image);
1130 /* float yres = (float) */ (void) ReadBlobMSBLong(image);
1133 if (xres < GIMP_MIN_RESOLUTION || xres > GIMP_MAX_RESOLUTION ||
1134 yres < GIMP_MIN_RESOLUTION || yres > GIMP_MAX_RESOLUTION)
1136 g_message ("Warning, resolution out of range in XCF file");
1137 xres = gimage->gimp->config->default_xresolution;
1138 yres = gimage->gimp->config->default_yresolution;
1143 /* BOGUS: we don't write these yet because we aren't
1144 reading them properly yet :(
1145 image->x_resolution = xres;
1146 image->y_resolution = yres;
1153 /* we need to read it, even if we ignore it */
1154 /*size_t tattoo_state = */ (void) ReadBlobMSBLong(image);
1158 case PROP_PARASITES:
1160 /* BOGUS: we may need these for IPTC stuff */
1161 if (DiscardBlobBytes(image,prop_size) == MagickFalse)
1162 ThrowFileException(&image->exception,CorruptImageError,
1163 "UnexpectedEndOfFile",image->filename);
1165 gssize_t base = info->cp;
1168 while (info->cp - base < prop_size)
1170 p = xcf_load_parasite (info);
1171 gimp_image_parasite_attach (gimage, p);
1172 gimp_parasite_free (p);
1174 if (info->cp - base != prop_size)
1175 g_message ("Error detected while loading an image's parasites");
1182 /* BOGUS: ignore for now... */
1183 /*size_t unit = */ (void) ReadBlobMSBLong(image);
1189 /* BOGUS: just skip it for now */
1190 if (DiscardBlobBytes(image,prop_size) == MagickFalse)
1191 ThrowFileException(&image->exception,CorruptImageError,
1192 "UnexpectedEndOfFile",image->filename);
1195 PathList *paths = xcf_load_bzpaths (gimage, info);
1196 gimp_image_set_paths (gimage, paths);
1201 case PROP_USER_UNIT:
1203 char unit_string[1000];
1204 /*BOGUS: ignored for now */
1205 /*float factor = (float) */ (void) ReadBlobMSBLong(image);
1206 /* size_t digits = */ (void) ReadBlobMSBLong(image);
1208 (void) ReadBlobStringWithLongSize(image, unit_string,
1209 sizeof(unit_string));
1218 /* read over it... */
1219 while ((prop_size > 0) && (EOFBlob(image) == MagickFalse))
1221 amount=(ssize_t) MagickMin(16, prop_size);
1222 amount=(ssize_t) ReadBlob(image,(size_t) amount,(unsigned char *) &buf);
1224 ThrowReaderException(CorruptImageError,"CorruptImage");
1225 prop_size -= (size_t) MagickMin(16,(size_t) amount);
1231 if (foundPropEnd == MagickFalse)
1232 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
1234 if ((image_info->ping != MagickFalse) && (image_info->number_scenes != 0))
1236 ; /* do nothing, were just pinging! */
1242 foundAllLayers = MagickFalse,
1246 oldPos=TellBlob(image);
1256 ssize_t offset = (int) ReadBlobMSBLong(image);
1258 foundAllLayers=MagickTrue;
1261 if (EOFBlob(image) != MagickFalse)
1263 ThrowFileException(exception,CorruptImageError,
1264 "UnexpectedEndOfFile",image->filename);
1267 } while (foundAllLayers == MagickFalse);
1268 offset=SeekBlob(image,oldPos,SEEK_SET); /* restore the position! */
1270 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
1271 /* allocate our array of layer info blocks */
1272 length=(size_t) number_layers;
1273 layer_info=(XCFLayerInfo *) AcquireQuantumMemory(length,
1274 sizeof(*layer_info));
1275 if (layer_info == (XCFLayerInfo *) NULL)
1276 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
1277 (void) ResetMagickMemory(layer_info,0,number_layers*sizeof(XCFLayerInfo));
1287 /* read in the offset of the next layer */
1288 offset=(MagickOffsetType) ReadBlobMSBLong(image);
1289 /* if the offset is 0 then we are at the end
1290 * of the layer list.
1294 /* save the current position as it is where the
1295 * next layer offset is stored.
1297 saved_pos=TellBlob(image);
1298 /* seek to the layer offset */
1299 offset=SeekBlob(image,offset,SEEK_SET);
1300 /* read in the layer */
1301 layer_ok=ReadOneLayer(image,&doc_info,&layer_info[current_layer]);
1302 if (layer_ok == MagickFalse)
1306 for (j=0; j < current_layer; j++)
1307 layer_info[j].image=DestroyImage(layer_info[j].image);
1308 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
1310 /* restore the saved position so we'll be ready to
1311 * read the next offset.
1313 offset=SeekBlob(image, saved_pos, SEEK_SET);
1316 if (number_layers == 1)
1319 Composite the layer data onto the main image, dispose the layer.
1321 (void) CompositeImage(image,OverCompositeOp,layer_info[0].image,
1322 layer_info[0].offset_x,layer_info[0].offset_y);
1323 layer_info[0].image =DestroyImage( layer_info[0].image);
1329 /* NOTE: XCF layers are REVERSED from composite order! */
1331 for (j=number_layers-1; j>=0; j--) {
1332 /* BOGUS: need to consider layer blending modes!! */
1334 if ( layer_info[j].visible ) { /* only visible ones, please! */
1335 CompositeImage(image, OverCompositeOp, layer_info[j].image,
1336 layer_info[j].offset_x, layer_info[j].offset_y );
1337 layer_info[j].image =DestroyImage( layer_info[j].image );
1339 /* Bob says that if we do this, we'll get REAL gray images! */
1340 if ( image_type == GIMP_GRAY ) {
1342 GetQuantizeInfo(&qi);
1343 qi.colorspace = GRAYColorspace;
1344 QuantizeImage( &qi, layer_info[j].image );
1351 /* NOTE: XCF layers are REVERSED from composite order! */
1354 /* first we copy the last layer on top of the main image */
1355 (void) CompositeImage(image,CopyCompositeOp,
1356 layer_info[number_layers-1].image,
1357 layer_info[number_layers-1].offset_x,
1358 layer_info[number_layers-1].offset_y);
1359 layer_info[number_layers-1].image=DestroyImage(
1360 layer_info[number_layers-1].image);
1362 /* now reverse the order of the layers as they are put
1366 image->next=layer_info[j].image;
1367 layer_info[j].image->previous=image;
1368 layer_info[j].image->page.x=layer_info[j].offset_x;
1369 layer_info[j].image->page.y=layer_info[j].offset_y;
1370 layer_info[j].image->page.width=layer_info[j].width;
1371 layer_info[j].image->page.height=layer_info[j].height;
1372 for (j=number_layers-3; j>=0; j--)
1375 layer_info[j].image->next=layer_info[j-1].image;
1376 if (j < (number_layers-1))
1377 layer_info[j].image->previous=layer_info[j+1].image;
1378 layer_info[j].image->page.x=layer_info[j].offset_x;
1379 layer_info[j].image->page.y=layer_info[j].offset_y;
1380 layer_info[j].image->page.width=layer_info[j].width;
1381 layer_info[j].image->page.height=layer_info[j].height;
1387 layer_info=(XCFLayerInfo *) RelinquishMagickMemory(layer_info);
1389 #if 0 /* BOGUS: do we need the channels?? */
1392 /* read in the offset of the next channel */
1393 info->cp += xcf_read_int32 (info->fp, &offset, 1);
1395 /* if the offset is 0 then we are at the end
1396 * of the channel list.
1401 /* save the current position as it is where the
1402 * next channel offset is stored.
1404 saved_pos = info->cp;
1406 /* seek to the channel offset */
1407 xcf_seek_pos (info, offset);
1409 /* read in the layer */
1410 channel = xcf_load_channel (info, gimage);
1414 num_successful_elements++;
1416 /* add the channel to the image if its not the selection */
1417 if (channel != gimage->selection_mask)
1418 gimp_image_add_channel (gimage, channel, -1);
1420 /* restore the saved position so we'll be ready to
1421 * read the next offset.
1423 xcf_seek_pos (info, saved_pos);
1428 (void) CloseBlob(image);
1429 if (image_type == GIMP_GRAY)
1430 image->type=GrayscaleType;
1431 return(GetFirstImageInList(image));
1435 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1439 % R e g i s t e r X C F I m a g e %
1443 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1445 % RegisterXCFImage() adds attributes for the XCF image format to
1446 % the list of supported formats. The attributes include the image format
1447 % tag, a method to read and/or write the format, whether the format
1448 % supports the saving of more than one frame to the same file or blob,
1449 % whether the format supports native in-memory I/O, and a brief
1450 % description of the format.
1452 % The format of the RegisterXCFImage method is:
1454 % size_t RegisterXCFImage(void)
1457 ModuleExport size_t RegisterXCFImage(void)
1462 entry=SetMagickInfo("XCF");
1463 entry->decoder=(DecodeImageHandler *) ReadXCFImage;
1464 entry->magick=(IsImageFormatHandler *) IsXCF;
1465 entry->description=ConstantString("GIMP image");
1466 entry->module=ConstantString("XCF");
1467 entry->seekable_stream=MagickTrue;
1468 (void) RegisterMagickInfo(entry);
1469 return(MagickImageCoderSignature);
1473 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1477 % U n r e g i s t e r X C F I m a g e %
1481 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1483 % UnregisterXCFImage() removes format registrations made by the
1484 % XCF module from the list of supported formats.
1486 % The format of the UnregisterXCFImage method is:
1488 % UnregisterXCFImage(void)
1491 ModuleExport void UnregisterXCFImage(void)
1493 (void) UnregisterMagickInfo("XCF");