2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
13 % Read GIMP XCF Image Format %
20 % Copyright 1999-2015 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 "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"
77 PROP_ACTIVE_LAYER = 2,
78 PROP_ACTIVE_CHANNEL = 3,
80 PROP_FLOATING_SELECTION = 5,
85 PROP_PRESERVE_TRANSPARENCY = 10,
89 PROP_SHOW_MASKED = 14,
92 PROP_COMPRESSION = 17,
106 COMPRESS_ZLIB = 2, /* unused */
107 COMPRESS_FRACTAL = 3 /* unused */
108 } XcfCompressionType;
161 #define TILE_WIDTH 64
162 #define TILE_HEIGHT 64
174 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
182 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
184 % IsXCF() returns MagickTrue if the image format type, identified by the
185 % magick string, is XCF (GIMP native format).
187 % The format of the IsXCF method is:
189 % MagickBooleanType IsXCF(const unsigned char *magick,const size_t length)
191 % A description of each parameter follows:
193 % o magick: compare image format pattern against these bytes.
195 % o length: Specifies the length of the magick string.
199 static MagickBooleanType IsXCF(const unsigned char *magick,const size_t length)
203 if (LocaleNCompare((char *) magick,"gimp xcf",8) == 0)
216 GIMP_DIFFERENCE_MODE,
219 GIMP_DARKEN_ONLY_MODE,
220 GIMP_LIGHTEN_ONLY_MODE,
222 GIMP_SATURATION_MODE,
229 } GimpLayerModeEffects;
232 Simple utility routine to convert between PSD blending modes and
233 ImageMagick compositing operators
235 static CompositeOperator GIMPBlendModeToCompositeOperator(
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);
265 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
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 %
273 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
275 % ReadBlobStringWithLongSize reads characters from a blob or file
276 % starting with a ssize_t length byte and then characters to that length
278 % The format of the ReadBlobStringWithLongSize method is:
280 % char *ReadBlobStringWithLongSize(Image *image,char *string)
282 % A description of each parameter follows:
284 % o image: the image.
286 % o string: the address of a character buffer.
290 static char *ReadBlobStringWithLongSize(Image *image,char *string,size_t max,
291 ExceptionInfo *exception)
305 assert(image != (Image *) NULL);
306 assert(image->signature == MagickSignature);
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++)
313 c=ReadBlobByte(image);
315 return((char *) NULL);
319 offset=SeekBlob(image,(MagickOffsetType) (length-i),SEEK_CUR);
321 (void) ThrowMagickException(exception,GetMagickModule(),
322 CorruptImageError,"ImproperImageHeader","`%s'",image->filename);
326 static MagickBooleanType load_tile(Image *image,Image *tile_image,
327 XCFDocInfo *inDocInfo,XCFLayerInfo *inLayerInfo,size_t data_length,
328 ExceptionInfo *exception)
349 xcfdata=(XCFPixelInfo *) AcquireQuantumMemory(data_length,sizeof(*xcfdata));
350 if (xcfdata == (XCFPixelInfo *) NULL)
351 ThrowBinaryException(ResourceLimitError,"MemoryAllocationFailed",
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",
359 for (y=0; y < (ssize_t) tile_image->rows; y++)
361 q=GetAuthenticPixels(tile_image,0,y,tile_image->columns,1,exception);
362 if (q == (Quantum *) NULL)
364 if (inDocInfo->image_type == GIMP_GRAY)
366 for (x=0; x < (ssize_t) tile_image->columns; x++)
368 SetPixelGray(tile_image,ScaleCharToQuantum(*graydata),q);
369 SetPixelAlpha(tile_image,ScaleCharToQuantum((unsigned char)
370 inLayerInfo->alpha),q);
372 q+=GetPixelChannels(tile_image);
376 if (inDocInfo->image_type == GIMP_RGB)
378 for (x=0; x < (ssize_t) tile_image->columns; x++)
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);
386 q+=GetPixelChannels(tile_image);
389 if (SyncAuthenticPixels(tile_image,exception) == MagickFalse)
392 xcfodata=(XCFPixelInfo *) RelinquishMagickMemory(xcfodata);
396 static MagickBooleanType load_tile_rle(Image *image,Image *tile_image,
397 XCFDocInfo *inDocInfo,XCFLayerInfo *inLayerInfo,size_t data_length,
398 ExceptionInfo *exception)
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",
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++)
436 q=GetAuthenticPixels(tile_image,0,0,tile_image->columns,tile_image->rows,
438 if (q == (Quantum *) NULL)
440 size=(MagickOffsetType) tile_image->rows*tile_image->columns;
443 if (xcfdata > xcfdatalimit)
446 length=(size_t) pixel;
449 length=255-(length-1);
452 if (xcfdata >= xcfdatalimit)
454 length=(size_t) ((*xcfdata << 8) + xcfdata[1]);
460 if (&xcfdata[length-1] > xcfdatalimit)
469 if (inDocInfo->image_type == GIMP_GRAY)
470 SetPixelGray(tile_image,ScaleCharToQuantum(data),q);
473 SetPixelRed(tile_image,ScaleCharToQuantum(data),q);
474 SetPixelGreen(tile_image,ScaleCharToQuantum(data),q);
475 SetPixelBlue(tile_image,ScaleCharToQuantum(data),q);
477 SetPixelAlpha(tile_image,alpha,q);
482 if (inDocInfo->image_type == GIMP_GRAY)
483 SetPixelAlpha(tile_image,ScaleCharToQuantum(data),q);
485 SetPixelGreen(tile_image,ScaleCharToQuantum(data),q);
490 SetPixelBlue(tile_image,ScaleCharToQuantum(data),q);
495 SetPixelAlpha(tile_image,ScaleCharToQuantum(data),q);
499 q+=GetPixelChannels(tile_image);
507 if (xcfdata >= xcfdatalimit)
509 length=(size_t) ((*xcfdata << 8) + xcfdata[1]);
515 if (xcfdata > xcfdatalimit)
518 for (j=0; j < (ssize_t) length; j++)
525 if (inDocInfo->image_type == GIMP_GRAY)
526 SetPixelGray(tile_image,ScaleCharToQuantum(data),q);
529 SetPixelRed(tile_image,ScaleCharToQuantum(data),q);
530 SetPixelGreen(tile_image,ScaleCharToQuantum(data),q);
531 SetPixelBlue(tile_image,ScaleCharToQuantum(data),q);
533 SetPixelAlpha(tile_image,alpha,q);
538 if (inDocInfo->image_type == GIMP_GRAY)
539 SetPixelAlpha(tile_image,ScaleCharToQuantum(data),q);
541 SetPixelGreen(tile_image,ScaleCharToQuantum(data),q);
546 SetPixelBlue(tile_image,ScaleCharToQuantum(data),q);
551 SetPixelAlpha(tile_image,ScaleCharToQuantum(data),q);
555 q+=GetPixelChannels(tile_image);
559 if (SyncAuthenticPixels(tile_image,exception) == MagickFalse)
562 xcfodata=(unsigned char *) RelinquishMagickMemory(xcfodata);
566 if (xcfodata != (unsigned char *) NULL)
567 xcfodata=(unsigned char *) RelinquishMagickMemory(xcfodata);
571 static MagickBooleanType load_level(Image *image,XCFDocInfo *inDocInfo,
572 XCFLayerInfo *inLayerInfo,ExceptionInfo *exception)
601 /* start reading the data */
602 width=ReadBlobMSBLong(image);
603 height=ReadBlobMSBLong(image);
605 /* read in the first tile offset.
606 * if it is '0', then this tile level is empty
607 * and we can simply return.
609 offset=(MagickOffsetType) ReadBlobMSBLong(image);
612 /* Initialise the reference for the in-memory tile-compression
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++)
621 ThrowBinaryException(CorruptImageError,"NotEnoughTiles",image->filename);
622 /* save the current position as it is where the
623 * next tile offset is stored.
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 */
632 offset2=(MagickOffsetType) (offset + TILE_WIDTH * TILE_WIDTH * 4* 1.5);
633 /* seek to the tile offset */
634 offset=SeekBlob(image, offset, SEEK_SET);
637 Allocate the image for the tile. NOTE: the last tile in a row or
638 column may not be a full tile!
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);
651 /* read in the tile */
652 switch (inDocInfo->compression)
655 if (load_tile(image,tile_image,inDocInfo,inLayerInfo,(size_t) (offset2-offset),exception) == 0)
659 if (load_tile_rle (image,tile_image,inDocInfo,inLayerInfo,
660 (int) (offset2-offset),exception) == 0)
664 ThrowBinaryException(CoderError,"ZipCompressNotSupported",
666 case COMPRESS_FRACTAL:
667 ThrowBinaryException(CoderError,"FractalCompressNotSupported",
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);
676 /* adjust tile position */
678 if (destLeft >= (int) ntile_cols)
683 if (status != MagickFalse)
685 /* restore the saved position so we'll be ready to
686 * read the next offset.
688 offset=SeekBlob(image, saved_pos, SEEK_SET);
689 /* read in the offset of the next tile */
690 offset=(MagickOffsetType) ReadBlobMSBLong(image);
693 ThrowBinaryException(CorruptImageError,"CorruptImage",image->filename)
697 static MagickBooleanType load_hierarchy(Image *image,XCFDocInfo *inDocInfo,
698 XCFLayerInfo *inLayer, ExceptionInfo *exception)
710 width=ReadBlobMSBLong(image);
712 height=ReadBlobMSBLong(image);
714 bytes_per_pixel=inDocInfo->bytes_per_pixel=ReadBlobMSBLong(image);
715 (void) bytes_per_pixel;
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.
721 offset=(MagickOffsetType) ReadBlobMSBLong(image); /* top level */
723 /* discard offsets for layers below first, if any.
727 junk=(MagickOffsetType) ReadBlobMSBLong(image);
731 /* save the current position as it is where the
732 * next level offset is stored.
734 saved_pos=TellBlob(image);
736 /* seek to the level offset */
737 offset=SeekBlob(image, offset, SEEK_SET);
739 /* read in the level */
740 if (load_level (image, inDocInfo, inLayer, exception) == 0)
742 /* restore the saved position so we'll be ready to
743 * read the next offset.
745 offset=SeekBlob(image, saved_pos, SEEK_SET);
749 static MagickBooleanType ReadOneLayer(const ImageInfo *image_info,Image* image,
750 XCFDocInfo* inDocInfo,XCFLayerInfo *outLayer,const ssize_t layer,
751 ExceptionInfo *exception )
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! */
773 while ( (foundPropEnd == MagickFalse) && (EOFBlob(image) == MagickFalse) ) {
774 PropType prop_type = (PropType) ReadBlobMSBLong(image);
775 size_t prop_size = ReadBlobMSBLong(image);
781 case PROP_ACTIVE_LAYER:
782 outLayer->active = 1;
784 case PROP_FLOATING_SELECTION:
785 outLayer->floating_offset = ReadBlobMSBLong(image);
788 outLayer->alpha = ReadBlobMSBLong(image);
791 outLayer->visible = ReadBlobMSBLong(image);
794 outLayer->linked = ReadBlobMSBLong(image);
796 case PROP_PRESERVE_TRANSPARENCY:
797 outLayer->preserve_trans = ReadBlobMSBLong(image);
799 case PROP_APPLY_MASK:
800 outLayer->apply_mask = ReadBlobMSBLong(image);
803 outLayer->edit_mask = ReadBlobMSBLong(image);
806 outLayer->show_mask = ReadBlobMSBLong(image);
809 outLayer->offset_x = (int) ReadBlobMSBLong(image);
810 outLayer->offset_y = (int) ReadBlobMSBLong(image);
813 outLayer->mode = ReadBlobMSBLong(image);
816 outLayer->preserve_trans = ReadBlobMSBLong(image);
820 if (DiscardBlobBytes(image,prop_size) == MagickFalse)
821 ThrowFileException(exception,CorruptImageError,
822 "UnexpectedEndOfFile",image->filename);
825 ssize_t base = info->cp;
827 while (info->cp - base < prop_size)
829 p = xcf_load_parasite(info);
830 gimp_drawable_parasite_attach(GIMP_DRAWABLE(layer), p);
831 gimp_parasite_free(p);
833 if (info->cp - base != prop_size)
834 g_message ("Error detected while loading a layer's parasites");
839 /* g_message ("unexpected/unknown layer property: %d (skipping)",
846 /* read over it... */
847 while ((prop_size > 0) && (EOFBlob(image) == MagickFalse))
849 amount = (ssize_t) MagickMin(16, prop_size);
850 amount = ReadBlob(image, (size_t) amount, (unsigned char *) &buf);
852 ThrowBinaryException(CorruptImageError,"CorruptImage",
854 prop_size -= (size_t) MagickMin(16, (size_t) amount);
861 if (foundPropEnd == MagickFalse)
863 /* allocate the image for this layer */
864 if (image_info->number_scenes != 0)
869 scene=inDocInfo->number_layers-layer-1;
870 if (scene > (ssize_t) (image_info->scene+image_info->number_scenes-1))
872 outLayer->image=CloneImage(image,0,0,MagickTrue,exception);
873 if (outLayer->image == (Image *) NULL)
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;
882 outLayer->image=CloneImage(image,outLayer->width, outLayer->height,MagickTrue,
884 if (outLayer->image == (Image *) NULL)
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);
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 )
899 /* BOGUS: should really be separate member var! */
900 outLayer->image->compose = NoCompositeOp;
903 /* read the hierarchy and layer mask offsets */
904 hierarchy_offset = ReadBlobMSBLong(image);
905 layer_mask_offset = ReadBlobMSBLong(image);
907 /* read in the hierarchy */
908 offset=SeekBlob(image, (MagickOffsetType) hierarchy_offset, SEEK_SET);
910 (void) ThrowMagickException(exception,GetMagickModule(),
911 CorruptImageError,"InvalidImageHeader","`%s'",image->filename);
912 if (load_hierarchy (image, inDocInfo, outLayer, exception) == 0)
915 /* read in the layer mask */
916 if (layer_mask_offset != 0)
918 offset=SeekBlob(image, (MagickOffsetType) layer_mask_offset, SEEK_SET);
920 #if 0 /* BOGUS: support layer masks! */
921 layer_mask = xcf_load_layer_mask (info, gimage);
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;
929 gimp_layer_add_mask (layer, layer_mask, MagickFalse);
931 layer->mask->apply_mask = apply_mask;
932 layer->mask->edit_mask = edit_mask;
933 layer->mask->show_mask = show_mask;
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)
941 GimpLayer *floating_sel;
943 floating_sel = info->floating_sel;
944 floating_sel_attach (floating_sel, GIMP_DRAWABLE (layer));
952 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
956 % R e a d X C F I m a g e %
960 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
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.
966 % The format of the ReadXCFImage method is:
968 % image=ReadXCFImage(image_info)
970 % A description of each parameter follows:
972 % o image_info: the image info.
974 % o exception: return any errors or warnings in this structure.
977 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,exception);
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);
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);
1035 Initialize image attributes.
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;
1043 if (image_type == GIMP_RGB)
1044 SetImageColorspace(image,sRGBColorspace,exception);
1046 if (image_type == GIMP_GRAY)
1047 SetImageColorspace(image,GRAYColorspace,exception);
1049 if (image_type == GIMP_INDEXED)
1050 ThrowReaderException(CoderError,"ColormapTypeNotSupported");
1051 (void) SetImageBackgroundColor(image,exception);
1052 (void) SetImageAlpha(image,OpaqueAlpha,exception);
1056 while ((foundPropEnd == MagickFalse) && (EOFBlob(image) == MagickFalse))
1058 PropType prop_type = (PropType) ReadBlobMSBLong(image);
1059 size_t prop_size = ReadBlobMSBLong(image);
1068 /* Cannot rely on prop_size here--the value is set incorrectly
1069 by some Gimp versions.
1071 size_t num_colours = ReadBlobMSBLong(image);
1072 if (DiscardBlobBytes(image,3*num_colours) == MagickFalse)
1073 ThrowFileException(exception,CorruptImageError,
1074 "UnexpectedEndOfFile",image->filename);
1076 if (info->file_version == 0)
1080 g_message (_("XCF warning: version 0 of XCF file format\n"
1081 "did not save indexed colormaps correctly.\n"
1082 "Substituting grayscale map."));
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++)
1089 gimage->cmap[i*3+0] = i;
1090 gimage->cmap[i*3+1] = i;
1091 gimage->cmap[i*3+2] = i;
1097 xcf_read_int32 (info->fp, (guint32*) &gimage->num_cols, 1);
1098 gimage->cmap = g_new (guchar, gimage->num_cols*3);
1100 xcf_read_int8 (info->fp,
1101 (guint8*) gimage->cmap, gimage->num_cols*3);
1106 case PROP_COMPRESSION:
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");
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);
1126 case PROP_RESOLUTION:
1128 /* float xres = (float) */ (void) ReadBlobMSBLong(image);
1129 /* float yres = (float) */ (void) ReadBlobMSBLong(image);
1132 if (xres < GIMP_MIN_RESOLUTION || xres > GIMP_MAX_RESOLUTION ||
1133 yres < GIMP_MIN_RESOLUTION || yres > GIMP_MAX_RESOLUTION)
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;
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;
1152 /* we need to read it, even if we ignore it */
1153 /*size_t tattoo_state = */ (void) ReadBlobMSBLong(image);
1157 case PROP_PARASITES:
1159 /* BOGUS: we may need these for IPTC stuff */
1160 if (DiscardBlobBytes(image,prop_size) == MagickFalse)
1161 ThrowFileException(exception,CorruptImageError,
1162 "UnexpectedEndOfFile",image->filename);
1164 gssize_t base = info->cp;
1167 while (info->cp - base < prop_size)
1169 p = xcf_load_parasite (info);
1170 gimp_image_parasite_attach (gimage, p);
1171 gimp_parasite_free (p);
1173 if (info->cp - base != prop_size)
1174 g_message ("Error detected while loading an image's parasites");
1181 /* BOGUS: ignore for now... */
1182 /*size_t unit = */ (void) ReadBlobMSBLong(image);
1188 /* BOGUS: just skip it for now */
1189 if (DiscardBlobBytes(image,prop_size) == MagickFalse)
1190 ThrowFileException(exception,CorruptImageError,"UnexpectedEndOfFile",
1194 PathList *paths = xcf_load_bzpaths (gimage, info);
1195 gimp_image_set_paths (gimage, paths);
1200 case PROP_USER_UNIT:
1202 char unit_string[1000];
1203 /*BOGUS: ignored for now */
1204 /*float factor = (float) */ (void) ReadBlobMSBLong(image);
1205 /* size_t digits = */ (void) ReadBlobMSBLong(image);
1207 (void) ReadBlobStringWithLongSize(image, unit_string,
1208 sizeof(unit_string),exception);
1217 /* read over it... */
1218 while ((prop_size > 0) && (EOFBlob(image) == MagickFalse))
1220 amount=(ssize_t) MagickMin(16, prop_size);
1221 amount=(ssize_t) ReadBlob(image,(size_t) amount,(unsigned char *) &buf);
1223 ThrowReaderException(CorruptImageError,"CorruptImage");
1224 prop_size -= (size_t) MagickMin(16,(size_t) amount);
1230 if (foundPropEnd == MagickFalse)
1231 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
1233 if ((image_info->ping != MagickFalse) && (image_info->number_scenes != 0))
1235 ; /* do nothing, were just pinging! */
1241 foundAllLayers = MagickFalse,
1245 oldPos=TellBlob(image);
1250 status=SetImageExtent(image,image->columns,image->rows,exception);
1251 if (status == MagickFalse)
1252 return(DestroyImageList(image));
1258 ssize_t offset = (int) ReadBlobMSBLong(image);
1260 foundAllLayers=MagickTrue;
1263 if (EOFBlob(image) != MagickFalse)
1265 ThrowFileException(exception,CorruptImageError,
1266 "UnexpectedEndOfFile",image->filename);
1269 } while (foundAllLayers == MagickFalse);
1270 doc_info.number_layers=number_layers;
1271 offset=SeekBlob(image,oldPos,SEEK_SET); /* restore the position! */
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));
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.
1297 /* save the current position as it is where the
1298 * next layer offset is stored.
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)
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");
1315 /* restore the saved position so we'll be ready to
1316 * read the next offset.
1318 offset=SeekBlob(image, saved_pos, SEEK_SET);
1321 if (number_layers == 1)
1324 Composite the layer data onto the main image, dispose the layer.
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);
1334 /* NOTE: XCF layers are REVERSED from composite order! */
1336 for (j=number_layers-1; j>=0; j--) {
1337 /* BOGUS: need to consider layer blending modes!! */
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 );
1344 /* If we do this, we'll get REAL gray images! */
1345 if ( image_type == GIMP_GRAY ) {
1347 GetQuantizeInfo(&qi);
1348 qi.colorspace = GRAYColorspace;
1349 QuantizeImage( &qi, layer_info[j].image );
1356 /* NOTE: XCF layers are REVERSED from composite order! */
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);
1366 /* now reverse the order of the layers as they are put
1369 for (j=(ssize_t) number_layers-2; j >= 0; j--)
1370 AppendImageToList(&image,layer_info[j].image);
1375 layer_info=(XCFLayerInfo *) RelinquishMagickMemory(layer_info);
1377 #if 0 /* BOGUS: do we need the channels?? */
1380 /* read in the offset of the next channel */
1381 info->cp += xcf_read_int32 (info->fp, &offset, 1);
1383 /* if the offset is 0 then we are at the end
1384 * of the channel list.
1389 /* save the current position as it is where the
1390 * next channel offset is stored.
1392 saved_pos = info->cp;
1394 /* seek to the channel offset */
1395 xcf_seek_pos (info, offset);
1397 /* read in the layer */
1398 channel = xcf_load_channel (info, gimage);
1402 num_successful_elements++;
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);
1408 /* restore the saved position so we'll be ready to
1409 * read the next offset.
1411 xcf_seek_pos (info, saved_pos);
1416 (void) CloseBlob(image);
1417 if (image_type == GIMP_GRAY)
1418 image->type=GrayscaleType;
1419 return(GetFirstImageInList(image));
1423 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1427 % R e g i s t e r X C F I m a g e %
1431 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
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.
1440 % The format of the RegisterXCFImage method is:
1442 % size_t RegisterXCFImage(void)
1445 ModuleExport size_t RegisterXCFImage(void)
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);
1461 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1465 % U n r e g i s t e r X C F I m a g e %
1469 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1471 % UnregisterXCFImage() removes format registrations made by the
1472 % XCF module from the list of supported formats.
1474 % The format of the UnregisterXCFImage method is:
1476 % UnregisterXCFImage(void)
1479 ModuleExport void UnregisterXCFImage(void)
1481 (void) UnregisterMagickInfo("XCF");