2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
13 % Read GIMP XCF Image Format %
20 % Copyright 1999-2016 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/property.h"
58 #include "MagickCore/quantize.h"
59 #include "MagickCore/quantum-private.h"
60 #include "MagickCore/static.h"
61 #include "MagickCore/string_.h"
62 #include "MagickCore/module.h"
78 PROP_ACTIVE_LAYER = 2,
79 PROP_ACTIVE_CHANNEL = 3,
81 PROP_FLOATING_SELECTION = 5,
86 PROP_PRESERVE_TRANSPARENCY = 10,
90 PROP_SHOW_MASKED = 14,
93 PROP_COMPRESSION = 17,
107 COMPRESS_ZLIB = 2, /* unused */
108 COMPRESS_FRACTAL = 3 /* unused */
109 } XcfCompressionType;
162 #define TILE_WIDTH 64
163 #define TILE_HEIGHT 64
175 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
183 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
185 % IsXCF() returns MagickTrue if the image format type, identified by the
186 % magick string, is XCF (GIMP native format).
188 % The format of the IsXCF method is:
190 % MagickBooleanType IsXCF(const unsigned char *magick,const size_t length)
192 % A description of each parameter follows:
194 % o magick: compare image format pattern against these bytes.
196 % o length: Specifies the length of the magick string.
200 static MagickBooleanType IsXCF(const unsigned char *magick,const size_t length)
204 if (LocaleNCompare((char *) magick,"gimp xcf",8) == 0)
217 GIMP_DIFFERENCE_MODE,
220 GIMP_DARKEN_ONLY_MODE,
221 GIMP_LIGHTEN_ONLY_MODE,
223 GIMP_SATURATION_MODE,
230 } GimpLayerModeEffects;
233 Simple utility routine to convert between PSD blending modes and
234 ImageMagick compositing operators
236 static CompositeOperator GIMPBlendModeToCompositeOperator(
241 case GIMP_NORMAL_MODE: return(OverCompositeOp);
242 case GIMP_DISSOLVE_MODE: return(DissolveCompositeOp);
243 case GIMP_MULTIPLY_MODE: return(MultiplyCompositeOp);
244 case GIMP_SCREEN_MODE: return(ScreenCompositeOp);
245 case GIMP_OVERLAY_MODE: return(OverlayCompositeOp);
246 case GIMP_DIFFERENCE_MODE: return(DifferenceCompositeOp);
247 case GIMP_ADDITION_MODE: return(ModulusAddCompositeOp);
248 case GIMP_SUBTRACT_MODE: return(ModulusSubtractCompositeOp);
249 case GIMP_DARKEN_ONLY_MODE: return(DarkenCompositeOp);
250 case GIMP_LIGHTEN_ONLY_MODE: return(LightenCompositeOp);
251 case GIMP_HUE_MODE: return(HueCompositeOp);
252 case GIMP_SATURATION_MODE: return(SaturateCompositeOp);
253 case GIMP_COLOR_MODE: return(ColorizeCompositeOp);
254 case GIMP_DODGE_MODE: return(ColorDodgeCompositeOp);
255 case GIMP_BURN_MODE: return(ColorBurnCompositeOp);
256 case GIMP_HARDLIGHT_MODE: return(HardLightCompositeOp);
257 case GIMP_DIVIDE_MODE: return(DivideDstCompositeOp);
258 /* these are the ones we don't support...yet */
259 case GIMP_BEHIND_MODE: return(OverCompositeOp);
260 case GIMP_VALUE_MODE: return(OverCompositeOp);
261 default: return(OverCompositeOp);
266 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
270 + 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 %
274 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
276 % ReadBlobStringWithLongSize reads characters from a blob or file
277 % starting with a ssize_t length byte and then characters to that length
279 % The format of the ReadBlobStringWithLongSize method is:
281 % char *ReadBlobStringWithLongSize(Image *image,char *string)
283 % A description of each parameter follows:
285 % o image: the image.
287 % o string: the address of a character buffer.
291 static char *ReadBlobStringWithLongSize(Image *image,char *string,size_t max,
292 ExceptionInfo *exception)
306 assert(image != (Image *) NULL);
307 assert(image->signature == MagickCoreSignature);
309 if (image->debug != MagickFalse)
310 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
311 length=ReadBlobMSBLong(image);
312 for (i=0; i < (ssize_t) MagickMin(length,max-1); i++)
314 c=ReadBlobByte(image);
316 return((char *) NULL);
320 offset=SeekBlob(image,(MagickOffsetType) (length-i),SEEK_CUR);
322 (void) ThrowMagickException(exception,GetMagickModule(),
323 CorruptImageError,"ImproperImageHeader","`%s'",image->filename);
327 static MagickBooleanType load_tile(Image *image,Image *tile_image,
328 XCFDocInfo *inDocInfo,XCFLayerInfo *inLayerInfo,size_t data_length,
329 ExceptionInfo *exception)
354 if (inDocInfo->image_type == GIMP_GRAY)
355 extent=tile_image->columns*tile_image->rows*sizeof(*graydata);
357 if (inDocInfo->image_type == GIMP_RGB)
358 extent=tile_image->columns*tile_image->rows*sizeof(*xcfdata);
359 if (extent > data_length)
360 ThrowBinaryException(CorruptImageError,"NotEnoughPixelData",
362 xcfdata=(XCFPixelInfo *) AcquireQuantumMemory(MagickMax(data_length,
363 tile_image->columns*tile_image->rows),sizeof(*xcfdata));
364 if (xcfdata == (XCFPixelInfo *) NULL)
365 ThrowBinaryException(ResourceLimitError,"MemoryAllocationFailed",
368 graydata=(unsigned char *) xcfdata; /* used by gray and indexed */
369 count=ReadBlob(image,data_length,(unsigned char *) xcfdata);
370 if (count != (ssize_t) data_length)
371 ThrowBinaryException(CorruptImageError,"NotEnoughPixelData",
373 for (y=0; y < (ssize_t) tile_image->rows; y++)
375 q=GetAuthenticPixels(tile_image,0,y,tile_image->columns,1,exception);
376 if (q == (Quantum *) NULL)
378 if (inDocInfo->image_type == GIMP_GRAY)
380 for (x=0; x < (ssize_t) tile_image->columns; x++)
382 SetPixelGray(tile_image,ScaleCharToQuantum(*graydata),q);
383 SetPixelAlpha(tile_image,ScaleCharToQuantum((unsigned char)
384 inLayerInfo->alpha),q);
386 q+=GetPixelChannels(tile_image);
390 if (inDocInfo->image_type == GIMP_RGB)
392 for (x=0; x < (ssize_t) tile_image->columns; x++)
394 SetPixelRed(tile_image,ScaleCharToQuantum(xcfdata->red),q);
395 SetPixelGreen(tile_image,ScaleCharToQuantum(xcfdata->green),q);
396 SetPixelBlue(tile_image,ScaleCharToQuantum(xcfdata->blue),q);
397 SetPixelAlpha(tile_image,xcfdata->alpha == 255U ? TransparentAlpha :
398 ScaleCharToQuantum((unsigned char) inLayerInfo->alpha),q);
400 q+=GetPixelChannels(tile_image);
403 if (SyncAuthenticPixels(tile_image,exception) == MagickFalse)
406 xcfodata=(XCFPixelInfo *) RelinquishMagickMemory(xcfodata);
410 static MagickBooleanType load_tile_rle(Image *image,Image *tile_image,
411 XCFDocInfo *inDocInfo,XCFLayerInfo *inLayerInfo,size_t data_length,
412 ExceptionInfo *exception)
439 bytes_per_pixel=(ssize_t) inDocInfo->bytes_per_pixel;
440 xcfdata=(unsigned char *) AcquireQuantumMemory(data_length,sizeof(*xcfdata));
441 if (xcfdata == (unsigned char *) NULL)
442 ThrowBinaryException(ResourceLimitError,"MemoryAllocationFailed",
445 count=ReadBlob(image, (size_t) data_length, xcfdata);
446 xcfdatalimit = xcfodata+count-1;
447 alpha=ScaleCharToQuantum((unsigned char) inLayerInfo->alpha);
448 for (i=0; i < (ssize_t) bytes_per_pixel; i++)
450 q=GetAuthenticPixels(tile_image,0,0,tile_image->columns,tile_image->rows,
452 if (q == (Quantum *) NULL)
454 size=(MagickOffsetType) tile_image->rows*tile_image->columns;
457 if (xcfdata > xcfdatalimit)
460 length=(size_t) pixel;
463 length=255-(length-1);
466 if (xcfdata >= xcfdatalimit)
468 length=(size_t) ((*xcfdata << 8) + xcfdata[1]);
474 if (&xcfdata[length-1] > xcfdatalimit)
483 if (inDocInfo->image_type == GIMP_GRAY)
484 SetPixelGray(tile_image,ScaleCharToQuantum(data),q);
487 SetPixelRed(tile_image,ScaleCharToQuantum(data),q);
488 SetPixelGreen(tile_image,ScaleCharToQuantum(data),q);
489 SetPixelBlue(tile_image,ScaleCharToQuantum(data),q);
491 SetPixelAlpha(tile_image,alpha,q);
496 if (inDocInfo->image_type == GIMP_GRAY)
497 SetPixelAlpha(tile_image,ScaleCharToQuantum(data),q);
499 SetPixelGreen(tile_image,ScaleCharToQuantum(data),q);
504 SetPixelBlue(tile_image,ScaleCharToQuantum(data),q);
509 SetPixelAlpha(tile_image,ScaleCharToQuantum(data),q);
513 q+=GetPixelChannels(tile_image);
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 if (inDocInfo->image_type == GIMP_GRAY)
540 SetPixelGray(tile_image,ScaleCharToQuantum(data),q);
543 SetPixelRed(tile_image,ScaleCharToQuantum(data),q);
544 SetPixelGreen(tile_image,ScaleCharToQuantum(data),q);
545 SetPixelBlue(tile_image,ScaleCharToQuantum(data),q);
547 SetPixelAlpha(tile_image,alpha,q);
552 if (inDocInfo->image_type == GIMP_GRAY)
553 SetPixelAlpha(tile_image,ScaleCharToQuantum(data),q);
555 SetPixelGreen(tile_image,ScaleCharToQuantum(data),q);
560 SetPixelBlue(tile_image,ScaleCharToQuantum(data),q);
565 SetPixelAlpha(tile_image,ScaleCharToQuantum(data),q);
569 q+=GetPixelChannels(tile_image);
573 if (SyncAuthenticPixels(tile_image,exception) == MagickFalse)
576 xcfodata=(unsigned char *) RelinquishMagickMemory(xcfodata);
580 if (xcfodata != (unsigned char *) NULL)
581 xcfodata=(unsigned char *) RelinquishMagickMemory(xcfodata);
585 static MagickBooleanType load_level(Image *image,XCFDocInfo *inDocInfo,
586 XCFLayerInfo *inLayerInfo,ExceptionInfo *exception)
615 /* start reading the data */
616 width=ReadBlobMSBLong(image);
617 height=ReadBlobMSBLong(image);
619 /* read in the first tile offset.
620 * if it is '0', then this tile level is empty
621 * and we can simply return.
623 offset=(MagickOffsetType) ReadBlobMSBLong(image);
626 /* Initialise the reference for the in-memory tile-compression
628 ntile_rows=(height+TILE_HEIGHT-1)/TILE_HEIGHT;
629 ntile_cols=(width+TILE_WIDTH-1)/TILE_WIDTH;
630 ntiles=ntile_rows*ntile_cols;
631 for (i = 0; i < (ssize_t) ntiles; i++)
635 ThrowBinaryException(CorruptImageError,"NotEnoughTiles",image->filename);
636 /* save the current position as it is where the
637 * next tile offset is stored.
639 saved_pos=TellBlob(image);
640 /* read in the offset of the next tile so we can calculate the amount
641 of data needed for this tile*/
642 offset2=(MagickOffsetType)ReadBlobMSBLong(image);
643 /* if the offset is 0 then we need to read in the maximum possible
644 allowing for negative compression */
646 offset2=(MagickOffsetType) (offset + TILE_WIDTH * TILE_WIDTH * 4* 1.5);
647 /* seek to the tile offset */
648 if (SeekBlob(image, offset, SEEK_SET) != offset)
649 ThrowBinaryException(CorruptImageError,"InsufficientImageDataInFile",
653 Allocate the image for the tile. NOTE: the last tile in a row or
654 column may not be a full tile!
656 tile_image_width=(size_t) (destLeft == (int) ntile_cols-1 ?
657 (int) width % TILE_WIDTH : TILE_WIDTH);
658 if (tile_image_width == 0)
659 tile_image_width=TILE_WIDTH;
660 tile_image_height = (size_t) (destTop == (int) ntile_rows-1 ?
661 (int) height % TILE_HEIGHT : TILE_HEIGHT);
662 if (tile_image_height == 0)
663 tile_image_height=TILE_HEIGHT;
664 tile_image=CloneImage(inLayerInfo->image,tile_image_width,
665 tile_image_height,MagickTrue,exception);
667 /* read in the tile */
668 switch (inDocInfo->compression)
671 if (load_tile(image,tile_image,inDocInfo,inLayerInfo,(size_t) (offset2-offset),exception) == 0)
675 if (load_tile_rle (image,tile_image,inDocInfo,inLayerInfo,
676 (int) (offset2-offset),exception) == 0)
680 ThrowBinaryException(CoderError,"ZipCompressNotSupported",
682 case COMPRESS_FRACTAL:
683 ThrowBinaryException(CoderError,"FractalCompressNotSupported",
687 /* composite the tile onto the layer's image, and then destroy it */
688 (void) CompositeImage(inLayerInfo->image,tile_image,CopyCompositeOp,
689 MagickTrue,destLeft * TILE_WIDTH,destTop*TILE_HEIGHT,exception);
690 tile_image=DestroyImage(tile_image);
692 /* adjust tile position */
694 if (destLeft >= (int) ntile_cols)
699 if (status != MagickFalse)
701 /* restore the saved position so we'll be ready to
702 * read the next offset.
704 offset=SeekBlob(image, saved_pos, SEEK_SET);
705 /* read in the offset of the next tile */
706 offset=(MagickOffsetType) ReadBlobMSBLong(image);
709 ThrowBinaryException(CorruptImageError,"CorruptImage",image->filename)
713 static MagickBooleanType load_hierarchy(Image *image,XCFDocInfo *inDocInfo,
714 XCFLayerInfo *inLayer, ExceptionInfo *exception)
726 width=ReadBlobMSBLong(image);
728 height=ReadBlobMSBLong(image);
730 bytes_per_pixel=inDocInfo->bytes_per_pixel=ReadBlobMSBLong(image);
731 (void) bytes_per_pixel;
733 /* load in the levels...we make sure that the number of levels
734 * calculated when the TileManager was created is the same
735 * as the number of levels found in the file.
737 offset=(MagickOffsetType) ReadBlobMSBLong(image); /* top level */
739 /* discard offsets for layers below first, if any.
743 junk=(MagickOffsetType) ReadBlobMSBLong(image);
747 /* save the current position as it is where the
748 * next level offset is stored.
750 saved_pos=TellBlob(image);
752 /* seek to the level offset */
753 if (SeekBlob(image, offset, SEEK_SET) != offset)
754 ThrowBinaryException(CorruptImageError,"InsufficientImageDataInFile",
757 /* read in the level */
758 if (load_level (image, inDocInfo, inLayer, exception) == 0)
760 /* restore the saved position so we'll be ready to
761 * read the next offset.
763 offset=SeekBlob(image, saved_pos, SEEK_SET);
767 static void InitXCFImage(XCFLayerInfo *outLayer,ExceptionInfo *exception)
769 outLayer->image->page.x=outLayer->offset_x;
770 outLayer->image->page.y=outLayer->offset_y;
771 outLayer->image->page.width=outLayer->width;
772 outLayer->image->page.height=outLayer->height;
773 (void) SetImageProperty(outLayer->image,"label",(char *)outLayer->name,
777 static MagickBooleanType ReadOneLayer(const ImageInfo *image_info,Image* image,
778 XCFDocInfo* inDocInfo,XCFLayerInfo *outLayer,const ssize_t layer,
779 ExceptionInfo *exception)
791 /* clear the block! */
792 (void) ResetMagickMemory( outLayer, 0, sizeof( XCFLayerInfo ) );
793 /* read in the layer width, height, type and name */
794 outLayer->width = ReadBlobMSBLong(image);
795 outLayer->height = ReadBlobMSBLong(image);
796 outLayer->type = ReadBlobMSBLong(image);
797 (void) ReadBlobStringWithLongSize(image, outLayer->name,
798 sizeof(outLayer->name),exception);
799 if (EOFBlob(image) != MagickFalse)
800 ThrowBinaryException(CorruptImageError,"InsufficientImageDataInFile",
802 /* read the layer properties! */
804 while ( (foundPropEnd == MagickFalse) && (EOFBlob(image) == MagickFalse) ) {
805 PropType prop_type = (PropType) ReadBlobMSBLong(image);
806 size_t prop_size = ReadBlobMSBLong(image);
812 case PROP_ACTIVE_LAYER:
813 outLayer->active = 1;
815 case PROP_FLOATING_SELECTION:
816 outLayer->floating_offset = ReadBlobMSBLong(image);
819 outLayer->alpha = ReadBlobMSBLong(image);
822 outLayer->visible = ReadBlobMSBLong(image);
825 outLayer->linked = ReadBlobMSBLong(image);
827 case PROP_PRESERVE_TRANSPARENCY:
828 outLayer->preserve_trans = ReadBlobMSBLong(image);
830 case PROP_APPLY_MASK:
831 outLayer->apply_mask = ReadBlobMSBLong(image);
834 outLayer->edit_mask = ReadBlobMSBLong(image);
837 outLayer->show_mask = ReadBlobMSBLong(image);
840 outLayer->offset_x = ReadBlobMSBSignedLong(image);
841 outLayer->offset_y = ReadBlobMSBSignedLong(image);
844 outLayer->mode = ReadBlobMSBLong(image);
847 outLayer->preserve_trans = ReadBlobMSBLong(image);
851 if (DiscardBlobBytes(image,prop_size) == MagickFalse)
852 ThrowFileException(exception,CorruptImageError,
853 "UnexpectedEndOfFile",image->filename);
856 ssize_t base = info->cp;
858 while (info->cp - base < prop_size)
860 p = xcf_load_parasite(info);
861 gimp_drawable_parasite_attach(GIMP_DRAWABLE(layer), p);
862 gimp_parasite_free(p);
864 if (info->cp - base != prop_size)
865 g_message ("Error detected while loading a layer's parasites");
870 /* g_message ("unexpected/unknown layer property: %d (skipping)",
877 /* read over it... */
878 while ((prop_size > 0) && (EOFBlob(image) == MagickFalse))
880 amount = (ssize_t) MagickMin(16, prop_size);
881 amount = ReadBlob(image, (size_t) amount, (unsigned char *) &buf);
883 ThrowBinaryException(CorruptImageError,"CorruptImage",
885 prop_size -= (size_t) MagickMin(16, (size_t) amount);
892 if (foundPropEnd == MagickFalse)
894 /* allocate the image for this layer */
895 if (image_info->number_scenes != 0)
900 scene=inDocInfo->number_layers-layer-1;
901 if (scene > (ssize_t) (image_info->scene+image_info->number_scenes-1))
903 outLayer->image=CloneImage(image,0,0,MagickTrue,exception);
904 if (outLayer->image == (Image *) NULL)
906 InitXCFImage(outLayer,exception);
910 outLayer->image=CloneImage(image,outLayer->width, outLayer->height,MagickTrue,
912 if (outLayer->image == (Image *) NULL)
914 /* clear the image based on the layer opacity */
915 outLayer->image->background_color.alpha=
916 ScaleCharToQuantum((unsigned char) outLayer->alpha);
917 (void) SetImageBackgroundColor(outLayer->image,exception);
919 InitXCFImage(outLayer,exception);
921 /* set the compositing mode */
922 outLayer->image->compose = GIMPBlendModeToCompositeOperator( outLayer->mode );
923 if ( outLayer->visible == MagickFalse )
925 /* BOGUS: should really be separate member var! */
926 outLayer->image->compose = NoCompositeOp;
929 /* read the hierarchy and layer mask offsets */
930 hierarchy_offset = ReadBlobMSBLong(image);
931 layer_mask_offset = ReadBlobMSBLong(image);
933 /* read in the hierarchy */
934 offset=SeekBlob(image, (MagickOffsetType) hierarchy_offset, SEEK_SET);
935 if (offset != (MagickOffsetType) hierarchy_offset)
936 (void) ThrowMagickException(exception,GetMagickModule(),
937 CorruptImageError,"InvalidImageHeader","`%s'",image->filename);
938 if (load_hierarchy (image, inDocInfo, outLayer, exception) == 0)
941 /* read in the layer mask */
942 if (layer_mask_offset != 0)
944 offset=SeekBlob(image, (MagickOffsetType) layer_mask_offset, SEEK_SET);
946 #if 0 /* BOGUS: support layer masks! */
947 layer_mask = xcf_load_layer_mask (info, gimage);
951 /* set the offsets of the layer_mask */
952 GIMP_DRAWABLE (layer_mask)->offset_x = GIMP_DRAWABLE (layer)->offset_x;
953 GIMP_DRAWABLE (layer_mask)->offset_y = GIMP_DRAWABLE (layer)->offset_y;
955 gimp_layer_add_mask (layer, layer_mask, MagickFalse);
957 layer->mask->apply_mask = apply_mask;
958 layer->mask->edit_mask = edit_mask;
959 layer->mask->show_mask = show_mask;
963 /* attach the floating selection... */
964 #if 0 /* BOGUS: we may need to read this, even if we don't support it! */
965 if (add_floating_sel)
967 GimpLayer *floating_sel;
969 floating_sel = info->floating_sel;
970 floating_sel_attach (floating_sel, GIMP_DRAWABLE (layer));
978 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
982 % R e a d X C F I m a g e %
986 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
988 % ReadXCFImage() reads a GIMP (GNU Image Manipulation Program) image
989 % file and returns it. It allocates the memory necessary for the new Image
990 % structure and returns a pointer to the new image.
992 % The format of the ReadXCFImage method is:
994 % image=ReadXCFImage(image_info)
996 % A description of each parameter follows:
998 % o image_info: the image info.
1000 % o exception: return any errors or warnings in this structure.
1003 static Image *ReadXCFImage(const ImageInfo *image_info,ExceptionInfo *exception)
1036 assert(image_info != (const ImageInfo *) NULL);
1037 assert(image_info->signature == MagickCoreSignature);
1038 if (image_info->debug != MagickFalse)
1039 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
1040 image_info->filename);
1041 assert(exception != (ExceptionInfo *) NULL);
1042 assert(exception->signature == MagickCoreSignature);
1043 image=AcquireImage(image_info,exception);
1044 status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception);
1045 if (status == MagickFalse)
1047 image=DestroyImageList(image);
1048 return((Image *) NULL);
1050 count=ReadBlob(image,14,(unsigned char *) magick);
1051 if ((count != 14) ||
1052 (LocaleNCompare((char *) magick,"gimp xcf",8) != 0))
1053 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
1054 (void) ResetMagickMemory(&doc_info,0,sizeof(XCFDocInfo));
1055 doc_info.width=ReadBlobMSBLong(image);
1056 doc_info.height=ReadBlobMSBLong(image);
1057 if ((doc_info.width > 262144) || (doc_info.height > 262144))
1058 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
1059 doc_info.image_type=ReadBlobMSBLong(image);
1061 Initialize image attributes.
1063 image->columns=doc_info.width;
1064 image->rows=doc_info.height;
1065 image_type=doc_info.image_type;
1066 doc_info.file_size=GetBlobSize(image);
1067 image->compression=NoCompression;
1069 status=SetImageExtent(image,image->columns,image->rows,exception);
1070 if (status == MagickFalse)
1071 return(DestroyImageList(image));
1072 if (image_type == GIMP_RGB)
1073 SetImageColorspace(image,sRGBColorspace,exception);
1075 if (image_type == GIMP_GRAY)
1076 SetImageColorspace(image,GRAYColorspace,exception);
1078 if (image_type == GIMP_INDEXED)
1079 ThrowReaderException(CoderError,"ColormapTypeNotSupported");
1080 (void) SetImageBackgroundColor(image,exception);
1081 (void) SetImageAlpha(image,OpaqueAlpha,exception);
1085 while ((foundPropEnd == MagickFalse) && (EOFBlob(image) == MagickFalse))
1087 PropType prop_type = (PropType) ReadBlobMSBLong(image);
1088 size_t prop_size = ReadBlobMSBLong(image);
1097 /* Cannot rely on prop_size here--the value is set incorrectly
1098 by some Gimp versions.
1100 size_t num_colours = ReadBlobMSBLong(image);
1101 if (DiscardBlobBytes(image,3*num_colours) == MagickFalse)
1102 ThrowFileException(exception,CorruptImageError,
1103 "UnexpectedEndOfFile",image->filename);
1105 if (info->file_version == 0)
1109 g_message (_("XCF warning: version 0 of XCF file format\n"
1110 "did not save indexed colormaps correctly.\n"
1111 "Substituting grayscale map."));
1113 xcf_read_int32 (info->fp, (guint32*) &gimage->num_cols, 1);
1114 gimage->cmap = g_new (guchar, gimage->num_cols*3);
1115 xcf_seek_pos (info, info->cp + gimage->num_cols);
1116 for (i = 0; i<gimage->num_cols; i++)
1118 gimage->cmap[i*3+0] = i;
1119 gimage->cmap[i*3+1] = i;
1120 gimage->cmap[i*3+2] = i;
1126 xcf_read_int32 (info->fp, (guint32*) &gimage->num_cols, 1);
1127 gimage->cmap = g_new (guchar, gimage->num_cols*3);
1129 xcf_read_int8 (info->fp,
1130 (guint8*) gimage->cmap, gimage->num_cols*3);
1135 case PROP_COMPRESSION:
1137 doc_info.compression = ReadBlobByte(image);
1138 if ((doc_info.compression != COMPRESS_NONE) &&
1139 (doc_info.compression != COMPRESS_RLE) &&
1140 (doc_info.compression != COMPRESS_ZLIB) &&
1141 (doc_info.compression != COMPRESS_FRACTAL))
1142 ThrowReaderException(CorruptImageError,"UnrecognizedImageCompression");
1148 /* just skip it - we don't care about guides */
1149 if (DiscardBlobBytes(image,prop_size) == MagickFalse)
1150 ThrowFileException(exception,CorruptImageError,
1151 "UnexpectedEndOfFile",image->filename);
1155 case PROP_RESOLUTION:
1157 /* float xres = (float) */ (void) ReadBlobMSBLong(image);
1158 /* float yres = (float) */ (void) ReadBlobMSBLong(image);
1161 if (xres < GIMP_MIN_RESOLUTION || xres > GIMP_MAX_RESOLUTION ||
1162 yres < GIMP_MIN_RESOLUTION || yres > GIMP_MAX_RESOLUTION)
1164 g_message ("Warning, resolution out of range in XCF file");
1165 xres = gimage->gimp->config->default_xresolution;
1166 yres = gimage->gimp->config->default_yresolution;
1171 /* BOGUS: we don't write these yet because we aren't
1172 reading them properly yet :(
1173 image->resolution.x = xres;
1174 image->resolution.y = yres;
1181 /* we need to read it, even if we ignore it */
1182 /*size_t tattoo_state = */ (void) ReadBlobMSBLong(image);
1186 case PROP_PARASITES:
1188 /* BOGUS: we may need these for IPTC stuff */
1189 if (DiscardBlobBytes(image,prop_size) == MagickFalse)
1190 ThrowFileException(exception,CorruptImageError,
1191 "UnexpectedEndOfFile",image->filename);
1193 gssize_t base = info->cp;
1196 while (info->cp - base < prop_size)
1198 p = xcf_load_parasite (info);
1199 gimp_image_parasite_attach (gimage, p);
1200 gimp_parasite_free (p);
1202 if (info->cp - base != prop_size)
1203 g_message ("Error detected while loading an image's parasites");
1210 /* BOGUS: ignore for now... */
1211 /*size_t unit = */ (void) ReadBlobMSBLong(image);
1217 /* BOGUS: just skip it for now */
1218 if (DiscardBlobBytes(image,prop_size) == MagickFalse)
1219 ThrowFileException(exception,CorruptImageError,"UnexpectedEndOfFile",
1223 PathList *paths = xcf_load_bzpaths (gimage, info);
1224 gimp_image_set_paths (gimage, paths);
1229 case PROP_USER_UNIT:
1231 char unit_string[1000];
1232 /*BOGUS: ignored for now */
1233 /*float factor = (float) */ (void) ReadBlobMSBLong(image);
1234 /* size_t digits = */ (void) ReadBlobMSBLong(image);
1236 (void) ReadBlobStringWithLongSize(image, unit_string,
1237 sizeof(unit_string),exception);
1246 /* read over it... */
1247 while ((prop_size > 0) && (EOFBlob(image) == MagickFalse))
1249 amount=(ssize_t) MagickMin(16, prop_size);
1250 amount=(ssize_t) ReadBlob(image,(size_t) amount,(unsigned char *) &buf);
1252 ThrowReaderException(CorruptImageError,"CorruptImage");
1253 prop_size -= (size_t) MagickMin(16,(size_t) amount);
1259 if (foundPropEnd == MagickFalse)
1260 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
1262 if ((image_info->ping != MagickFalse) && (image_info->number_scenes != 0))
1264 ; /* do nothing, were just pinging! */
1270 foundAllLayers = MagickFalse,
1274 oldPos=TellBlob(image);
1284 ssize_t offset = ReadBlobMSBSignedLong(image);
1286 foundAllLayers=MagickTrue;
1289 if (EOFBlob(image) != MagickFalse)
1291 ThrowFileException(exception,CorruptImageError,
1292 "UnexpectedEndOfFile",image->filename);
1295 } while (foundAllLayers == MagickFalse);
1296 doc_info.number_layers=number_layers;
1297 offset=SeekBlob(image,oldPos,SEEK_SET); /* restore the position! */
1299 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
1300 /* allocate our array of layer info blocks */
1301 length=(size_t) number_layers;
1302 layer_info=(XCFLayerInfo *) AcquireQuantumMemory(length,
1303 sizeof(*layer_info));
1304 if (layer_info == (XCFLayerInfo *) NULL)
1305 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
1306 (void) ResetMagickMemory(layer_info,0,number_layers*sizeof(XCFLayerInfo));
1316 /* read in the offset of the next layer */
1317 offset=(MagickOffsetType) ReadBlobMSBLong(image);
1318 /* if the offset is 0 then we are at the end
1319 * of the layer list.
1323 /* save the current position as it is where the
1324 * next layer offset is stored.
1326 saved_pos=TellBlob(image);
1327 /* seek to the layer offset */
1328 if (SeekBlob(image,offset,SEEK_SET) != offset)
1329 ThrowReaderException(CorruptImageError,"NotEnoughPixelData");
1330 /* read in the layer */
1331 layer_ok=ReadOneLayer(image_info,image,&doc_info,
1332 &layer_info[current_layer],current_layer,exception);
1333 if (layer_ok == MagickFalse)
1337 for (j=0; j < current_layer; j++)
1338 layer_info[j].image=DestroyImage(layer_info[j].image);
1339 layer_info=(XCFLayerInfo *) RelinquishMagickMemory(layer_info);
1340 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
1342 /* restore the saved position so we'll be ready to
1343 * read the next offset.
1345 offset=SeekBlob(image, saved_pos, SEEK_SET);
1350 /* NOTE: XCF layers are REVERSED from composite order! */
1352 for (j=number_layers-1; j>=0; j--) {
1353 /* BOGUS: need to consider layer blending modes!! */
1355 if ( layer_info[j].visible ) { /* only visible ones, please! */
1356 CompositeImage(image, OverCompositeOp, layer_info[j].image,
1357 layer_info[j].offset_x, layer_info[j].offset_y );
1358 layer_info[j].image =DestroyImage( layer_info[j].image );
1360 /* If we do this, we'll get REAL gray images! */
1361 if ( image_type == GIMP_GRAY ) {
1363 GetQuantizeInfo(&qi);
1364 qi.colorspace = GRAYColorspace;
1365 QuantizeImage( &qi, layer_info[j].image );
1372 /* NOTE: XCF layers are REVERSED from composite order! */
1375 /* now reverse the order of the layers as they are put
1378 for (j=(ssize_t) number_layers-1; j >= 0; j--)
1379 AppendImageToList(&image,layer_info[j].image);
1383 layer_info=(XCFLayerInfo *) RelinquishMagickMemory(layer_info);
1385 #if 0 /* BOGUS: do we need the channels?? */
1388 /* read in the offset of the next channel */
1389 info->cp += xcf_read_int32 (info->fp, &offset, 1);
1391 /* if the offset is 0 then we are at the end
1392 * of the channel list.
1397 /* save the current position as it is where the
1398 * next channel offset is stored.
1400 saved_pos = info->cp;
1402 /* seek to the channel offset */
1403 xcf_seek_pos (info, offset);
1405 /* read in the layer */
1406 channel = xcf_load_channel (info, gimage);
1410 num_successful_elements++;
1412 /* add the channel to the image if its not the selection */
1413 if (channel != gimage->selection_mask)
1414 gimp_image_add_channel (gimage, channel, -1);
1416 /* restore the saved position so we'll be ready to
1417 * read the next offset.
1419 xcf_seek_pos (info, saved_pos);
1424 (void) CloseBlob(image);
1425 DestroyImage(RemoveFirstImageFromList(&image));
1426 if (image_type == GIMP_GRAY)
1427 image->type=GrayscaleType;
1428 return(GetFirstImageInList(image));
1432 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1436 % R e g i s t e r X C F I m a g e %
1440 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1442 % RegisterXCFImage() adds attributes for the XCF image format to
1443 % the list of supported formats. The attributes include the image format
1444 % tag, a method to read and/or write the format, whether the format
1445 % supports the saving of more than one frame to the same file or blob,
1446 % whether the format supports native in-memory I/O, and a brief
1447 % description of the format.
1449 % The format of the RegisterXCFImage method is:
1451 % size_t RegisterXCFImage(void)
1454 ModuleExport size_t RegisterXCFImage(void)
1459 entry=AcquireMagickInfo("XCF","XCF","GIMP image");
1460 entry->decoder=(DecodeImageHandler *) ReadXCFImage;
1461 entry->magick=(IsImageFormatHandler *) IsXCF;
1462 entry->flags|=CoderSeekableStreamFlag;
1463 (void) RegisterMagickInfo(entry);
1464 return(MagickImageCoderSignature);
1468 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1472 % U n r e g i s t e r X C F I m a g e %
1476 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1478 % UnregisterXCFImage() removes format registrations made by the
1479 % XCF module from the list of supported formats.
1481 % The format of the UnregisterXCFImage method is:
1483 % UnregisterXCFImage(void)
1486 ModuleExport void UnregisterXCFImage(void)
1488 (void) UnregisterMagickInfo("XCF");