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 offset=SeekBlob(image, offset, SEEK_SET);
651 Allocate the image for the tile. NOTE: the last tile in a row or
652 column may not be a full tile!
654 tile_image_width=(size_t) (destLeft == (int) ntile_cols-1 ?
655 (int) width % TILE_WIDTH : TILE_WIDTH);
656 if (tile_image_width == 0)
657 tile_image_width=TILE_WIDTH;
658 tile_image_height = (size_t) (destTop == (int) ntile_rows-1 ?
659 (int) height % TILE_HEIGHT : TILE_HEIGHT);
660 if (tile_image_height == 0)
661 tile_image_height=TILE_HEIGHT;
662 tile_image=CloneImage(inLayerInfo->image,tile_image_width,
663 tile_image_height,MagickTrue,exception);
665 /* read in the tile */
666 switch (inDocInfo->compression)
669 if (load_tile(image,tile_image,inDocInfo,inLayerInfo,(size_t) (offset2-offset),exception) == 0)
673 if (load_tile_rle (image,tile_image,inDocInfo,inLayerInfo,
674 (int) (offset2-offset),exception) == 0)
678 ThrowBinaryException(CoderError,"ZipCompressNotSupported",
680 case COMPRESS_FRACTAL:
681 ThrowBinaryException(CoderError,"FractalCompressNotSupported",
685 /* composite the tile onto the layer's image, and then destroy it */
686 (void) CompositeImage(inLayerInfo->image,tile_image,CopyCompositeOp,
687 MagickTrue,destLeft * TILE_WIDTH,destTop*TILE_HEIGHT,exception);
688 tile_image=DestroyImage(tile_image);
690 /* adjust tile position */
692 if (destLeft >= (int) ntile_cols)
697 if (status != MagickFalse)
699 /* restore the saved position so we'll be ready to
700 * read the next offset.
702 offset=SeekBlob(image, saved_pos, SEEK_SET);
703 /* read in the offset of the next tile */
704 offset=(MagickOffsetType) ReadBlobMSBLong(image);
707 ThrowBinaryException(CorruptImageError,"CorruptImage",image->filename)
711 static MagickBooleanType load_hierarchy(Image *image,XCFDocInfo *inDocInfo,
712 XCFLayerInfo *inLayer, ExceptionInfo *exception)
724 width=ReadBlobMSBLong(image);
726 height=ReadBlobMSBLong(image);
728 bytes_per_pixel=inDocInfo->bytes_per_pixel=ReadBlobMSBLong(image);
729 (void) bytes_per_pixel;
731 /* load in the levels...we make sure that the number of levels
732 * calculated when the TileManager was created is the same
733 * as the number of levels found in the file.
735 offset=(MagickOffsetType) ReadBlobMSBLong(image); /* top level */
737 /* discard offsets for layers below first, if any.
741 junk=(MagickOffsetType) ReadBlobMSBLong(image);
745 /* save the current position as it is where the
746 * next level offset is stored.
748 saved_pos=TellBlob(image);
750 /* seek to the level offset */
751 offset=SeekBlob(image, offset, SEEK_SET);
753 /* read in the level */
754 if (load_level (image, inDocInfo, inLayer, exception) == 0)
756 /* restore the saved position so we'll be ready to
757 * read the next offset.
759 offset=SeekBlob(image, saved_pos, SEEK_SET);
763 static void InitXCFImage(XCFLayerInfo *outLayer,ExceptionInfo *exception)
765 outLayer->image->page.x=outLayer->offset_x;
766 outLayer->image->page.y=outLayer->offset_y;
767 outLayer->image->page.width=outLayer->width;
768 outLayer->image->page.height=outLayer->height;
769 (void) SetImageProperty(outLayer->image,"label",(char *)outLayer->name,
773 static MagickBooleanType ReadOneLayer(const ImageInfo *image_info,Image* image,
774 XCFDocInfo* inDocInfo,XCFLayerInfo *outLayer,const ssize_t layer,
775 ExceptionInfo *exception)
787 /* clear the block! */
788 (void) ResetMagickMemory( outLayer, 0, sizeof( XCFLayerInfo ) );
789 /* read in the layer width, height, type and name */
790 outLayer->width = ReadBlobMSBLong(image);
791 outLayer->height = ReadBlobMSBLong(image);
792 outLayer->type = ReadBlobMSBLong(image);
793 (void) ReadBlobStringWithLongSize(image, outLayer->name,
794 sizeof(outLayer->name),exception);
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->alpha = 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(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 /* allocate the image for this layer */
888 if (image_info->number_scenes != 0)
893 scene=inDocInfo->number_layers-layer-1;
894 if (scene > (ssize_t) (image_info->scene+image_info->number_scenes-1))
896 outLayer->image=CloneImage(image,0,0,MagickTrue,exception);
897 if (outLayer->image == (Image *) NULL)
899 InitXCFImage(outLayer,exception);
903 outLayer->image=CloneImage(image,outLayer->width, outLayer->height,MagickTrue,
905 if (outLayer->image == (Image *) NULL)
907 /* clear the image based on the layer opacity */
908 outLayer->image->background_color.alpha=
909 ScaleCharToQuantum((unsigned char) outLayer->alpha);
910 (void) SetImageBackgroundColor(outLayer->image,exception);
912 InitXCFImage(outLayer,exception);
914 /* set the compositing mode */
915 outLayer->image->compose = GIMPBlendModeToCompositeOperator( outLayer->mode );
916 if ( outLayer->visible == MagickFalse )
918 /* BOGUS: should really be separate member var! */
919 outLayer->image->compose = NoCompositeOp;
922 /* read the hierarchy and layer mask offsets */
923 hierarchy_offset = ReadBlobMSBLong(image);
924 layer_mask_offset = ReadBlobMSBLong(image);
926 /* read in the hierarchy */
927 offset=SeekBlob(image, (MagickOffsetType) hierarchy_offset, SEEK_SET);
929 (void) ThrowMagickException(exception,GetMagickModule(),
930 CorruptImageError,"InvalidImageHeader","`%s'",image->filename);
931 if (load_hierarchy (image, inDocInfo, outLayer, exception) == 0)
934 /* read in the layer mask */
935 if (layer_mask_offset != 0)
937 offset=SeekBlob(image, (MagickOffsetType) layer_mask_offset, SEEK_SET);
939 #if 0 /* BOGUS: support layer masks! */
940 layer_mask = xcf_load_layer_mask (info, gimage);
944 /* set the offsets of the layer_mask */
945 GIMP_DRAWABLE (layer_mask)->offset_x = GIMP_DRAWABLE (layer)->offset_x;
946 GIMP_DRAWABLE (layer_mask)->offset_y = GIMP_DRAWABLE (layer)->offset_y;
948 gimp_layer_add_mask (layer, layer_mask, MagickFalse);
950 layer->mask->apply_mask = apply_mask;
951 layer->mask->edit_mask = edit_mask;
952 layer->mask->show_mask = show_mask;
956 /* attach the floating selection... */
957 #if 0 /* BOGUS: we may need to read this, even if we don't support it! */
958 if (add_floating_sel)
960 GimpLayer *floating_sel;
962 floating_sel = info->floating_sel;
963 floating_sel_attach (floating_sel, GIMP_DRAWABLE (layer));
971 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
975 % R e a d X C F I m a g e %
979 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
981 % ReadXCFImage() reads a GIMP (GNU Image Manipulation Program) image
982 % file and returns it. It allocates the memory necessary for the new Image
983 % structure and returns a pointer to the new image.
985 % The format of the ReadXCFImage method is:
987 % image=ReadXCFImage(image_info)
989 % A description of each parameter follows:
991 % o image_info: the image info.
993 % o exception: return any errors or warnings in this structure.
996 static Image *ReadXCFImage(const ImageInfo *image_info,ExceptionInfo *exception)
1029 assert(image_info != (const ImageInfo *) NULL);
1030 assert(image_info->signature == MagickCoreSignature);
1031 if (image_info->debug != MagickFalse)
1032 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
1033 image_info->filename);
1034 assert(exception != (ExceptionInfo *) NULL);
1035 assert(exception->signature == MagickCoreSignature);
1036 image=AcquireImage(image_info,exception);
1037 status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception);
1038 if (status == MagickFalse)
1040 image=DestroyImageList(image);
1041 return((Image *) NULL);
1043 count=ReadBlob(image,14,(unsigned char *) magick);
1044 if ((count != 14) ||
1045 (LocaleNCompare((char *) magick,"gimp xcf",8) != 0))
1046 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
1047 (void) ResetMagickMemory(&doc_info,0,sizeof(XCFDocInfo));
1048 doc_info.width=ReadBlobMSBLong(image);
1049 doc_info.height=ReadBlobMSBLong(image);
1050 if ((doc_info.width > 262144) || (doc_info.height > 262144))
1051 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
1052 doc_info.image_type=ReadBlobMSBLong(image);
1054 Initialize image attributes.
1056 image->columns=doc_info.width;
1057 image->rows=doc_info.height;
1058 image_type=doc_info.image_type;
1059 doc_info.file_size=GetBlobSize(image);
1060 image->compression=NoCompression;
1062 if (image_type == GIMP_RGB)
1063 SetImageColorspace(image,sRGBColorspace,exception);
1065 if (image_type == GIMP_GRAY)
1066 SetImageColorspace(image,GRAYColorspace,exception);
1068 if (image_type == GIMP_INDEXED)
1069 ThrowReaderException(CoderError,"ColormapTypeNotSupported");
1070 (void) SetImageBackgroundColor(image,exception);
1071 (void) SetImageAlpha(image,OpaqueAlpha,exception);
1075 while ((foundPropEnd == MagickFalse) && (EOFBlob(image) == MagickFalse))
1077 PropType prop_type = (PropType) ReadBlobMSBLong(image);
1078 size_t prop_size = ReadBlobMSBLong(image);
1087 /* Cannot rely on prop_size here--the value is set incorrectly
1088 by some Gimp versions.
1090 size_t num_colours = ReadBlobMSBLong(image);
1091 if (DiscardBlobBytes(image,3*num_colours) == MagickFalse)
1092 ThrowFileException(exception,CorruptImageError,
1093 "UnexpectedEndOfFile",image->filename);
1095 if (info->file_version == 0)
1099 g_message (_("XCF warning: version 0 of XCF file format\n"
1100 "did not save indexed colormaps correctly.\n"
1101 "Substituting grayscale map."));
1103 xcf_read_int32 (info->fp, (guint32*) &gimage->num_cols, 1);
1104 gimage->cmap = g_new (guchar, gimage->num_cols*3);
1105 xcf_seek_pos (info, info->cp + gimage->num_cols);
1106 for (i = 0; i<gimage->num_cols; i++)
1108 gimage->cmap[i*3+0] = i;
1109 gimage->cmap[i*3+1] = i;
1110 gimage->cmap[i*3+2] = i;
1116 xcf_read_int32 (info->fp, (guint32*) &gimage->num_cols, 1);
1117 gimage->cmap = g_new (guchar, gimage->num_cols*3);
1119 xcf_read_int8 (info->fp,
1120 (guint8*) gimage->cmap, gimage->num_cols*3);
1125 case PROP_COMPRESSION:
1127 doc_info.compression = ReadBlobByte(image);
1128 if ((doc_info.compression != COMPRESS_NONE) &&
1129 (doc_info.compression != COMPRESS_RLE) &&
1130 (doc_info.compression != COMPRESS_ZLIB) &&
1131 (doc_info.compression != COMPRESS_FRACTAL))
1132 ThrowReaderException(CorruptImageError,"UnrecognizedImageCompression");
1138 /* just skip it - we don't care about guides */
1139 if (DiscardBlobBytes(image,prop_size) == MagickFalse)
1140 ThrowFileException(exception,CorruptImageError,
1141 "UnexpectedEndOfFile",image->filename);
1145 case PROP_RESOLUTION:
1147 /* float xres = (float) */ (void) ReadBlobMSBLong(image);
1148 /* float yres = (float) */ (void) ReadBlobMSBLong(image);
1151 if (xres < GIMP_MIN_RESOLUTION || xres > GIMP_MAX_RESOLUTION ||
1152 yres < GIMP_MIN_RESOLUTION || yres > GIMP_MAX_RESOLUTION)
1154 g_message ("Warning, resolution out of range in XCF file");
1155 xres = gimage->gimp->config->default_xresolution;
1156 yres = gimage->gimp->config->default_yresolution;
1161 /* BOGUS: we don't write these yet because we aren't
1162 reading them properly yet :(
1163 image->resolution.x = xres;
1164 image->resolution.y = yres;
1171 /* we need to read it, even if we ignore it */
1172 /*size_t tattoo_state = */ (void) ReadBlobMSBLong(image);
1176 case PROP_PARASITES:
1178 /* BOGUS: we may need these for IPTC stuff */
1179 if (DiscardBlobBytes(image,prop_size) == MagickFalse)
1180 ThrowFileException(exception,CorruptImageError,
1181 "UnexpectedEndOfFile",image->filename);
1183 gssize_t base = info->cp;
1186 while (info->cp - base < prop_size)
1188 p = xcf_load_parasite (info);
1189 gimp_image_parasite_attach (gimage, p);
1190 gimp_parasite_free (p);
1192 if (info->cp - base != prop_size)
1193 g_message ("Error detected while loading an image's parasites");
1200 /* BOGUS: ignore for now... */
1201 /*size_t unit = */ (void) ReadBlobMSBLong(image);
1207 /* BOGUS: just skip it for now */
1208 if (DiscardBlobBytes(image,prop_size) == MagickFalse)
1209 ThrowFileException(exception,CorruptImageError,"UnexpectedEndOfFile",
1213 PathList *paths = xcf_load_bzpaths (gimage, info);
1214 gimp_image_set_paths (gimage, paths);
1219 case PROP_USER_UNIT:
1221 char unit_string[1000];
1222 /*BOGUS: ignored for now */
1223 /*float factor = (float) */ (void) ReadBlobMSBLong(image);
1224 /* size_t digits = */ (void) ReadBlobMSBLong(image);
1226 (void) ReadBlobStringWithLongSize(image, unit_string,
1227 sizeof(unit_string),exception);
1236 /* read over it... */
1237 while ((prop_size > 0) && (EOFBlob(image) == MagickFalse))
1239 amount=(ssize_t) MagickMin(16, prop_size);
1240 amount=(ssize_t) ReadBlob(image,(size_t) amount,(unsigned char *) &buf);
1242 ThrowReaderException(CorruptImageError,"CorruptImage");
1243 prop_size -= (size_t) MagickMin(16,(size_t) amount);
1249 if (foundPropEnd == MagickFalse)
1250 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
1252 if ((image_info->ping != MagickFalse) && (image_info->number_scenes != 0))
1254 ; /* do nothing, were just pinging! */
1260 foundAllLayers = MagickFalse,
1264 oldPos=TellBlob(image);
1269 status=SetImageExtent(image,image->columns,image->rows,exception);
1270 if (status == MagickFalse)
1271 return(DestroyImageList(image));
1277 ssize_t offset = (int) ReadBlobMSBLong(image);
1279 foundAllLayers=MagickTrue;
1282 if (EOFBlob(image) != MagickFalse)
1284 ThrowFileException(exception,CorruptImageError,
1285 "UnexpectedEndOfFile",image->filename);
1288 } while (foundAllLayers == MagickFalse);
1289 doc_info.number_layers=number_layers;
1290 offset=SeekBlob(image,oldPos,SEEK_SET); /* restore the position! */
1292 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
1293 /* allocate our array of layer info blocks */
1294 length=(size_t) number_layers;
1295 layer_info=(XCFLayerInfo *) AcquireQuantumMemory(length,
1296 sizeof(*layer_info));
1297 if (layer_info == (XCFLayerInfo *) NULL)
1298 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
1299 (void) ResetMagickMemory(layer_info,0,number_layers*sizeof(XCFLayerInfo));
1309 /* read in the offset of the next layer */
1310 offset=(MagickOffsetType) ReadBlobMSBLong(image);
1311 /* if the offset is 0 then we are at the end
1312 * of the layer list.
1316 /* save the current position as it is where the
1317 * next layer offset is stored.
1319 saved_pos=TellBlob(image);
1320 /* seek to the layer offset */
1321 offset=SeekBlob(image,offset,SEEK_SET);
1322 /* read in the layer */
1323 layer_ok=ReadOneLayer(image_info,image,&doc_info,
1324 &layer_info[current_layer],current_layer,exception);
1325 if (layer_ok == MagickFalse)
1329 for (j=0; j < current_layer; j++)
1330 layer_info[j].image=DestroyImage(layer_info[j].image);
1331 layer_info=(XCFLayerInfo *) RelinquishMagickMemory(layer_info);
1332 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
1334 /* restore the saved position so we'll be ready to
1335 * read the next offset.
1337 offset=SeekBlob(image, saved_pos, SEEK_SET);
1342 /* NOTE: XCF layers are REVERSED from composite order! */
1344 for (j=number_layers-1; j>=0; j--) {
1345 /* BOGUS: need to consider layer blending modes!! */
1347 if ( layer_info[j].visible ) { /* only visible ones, please! */
1348 CompositeImage(image, OverCompositeOp, layer_info[j].image,
1349 layer_info[j].offset_x, layer_info[j].offset_y );
1350 layer_info[j].image =DestroyImage( layer_info[j].image );
1352 /* If we do this, we'll get REAL gray images! */
1353 if ( image_type == GIMP_GRAY ) {
1355 GetQuantizeInfo(&qi);
1356 qi.colorspace = GRAYColorspace;
1357 QuantizeImage( &qi, layer_info[j].image );
1364 /* NOTE: XCF layers are REVERSED from composite order! */
1367 /* now reverse the order of the layers as they are put
1370 for (j=(ssize_t) number_layers-1; j >= 0; j--)
1371 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 DestroyImage(RemoveFirstImageFromList(&image));
1418 if (image_type == GIMP_GRAY)
1419 image->type=GrayscaleType;
1420 return(GetFirstImageInList(image));
1424 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1428 % R e g i s t e r X C F I m a g e %
1432 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1434 % RegisterXCFImage() adds attributes for the XCF image format to
1435 % the list of supported formats. The attributes include the image format
1436 % tag, a method to read and/or write the format, whether the format
1437 % supports the saving of more than one frame to the same file or blob,
1438 % whether the format supports native in-memory I/O, and a brief
1439 % description of the format.
1441 % The format of the RegisterXCFImage method is:
1443 % size_t RegisterXCFImage(void)
1446 ModuleExport size_t RegisterXCFImage(void)
1451 entry=AcquireMagickInfo("XCF","XCF","GIMP image");
1452 entry->decoder=(DecodeImageHandler *) ReadXCFImage;
1453 entry->magick=(IsImageFormatHandler *) IsXCF;
1454 entry->flags|=CoderSeekableStreamFlag;
1455 (void) RegisterMagickInfo(entry);
1456 return(MagickImageCoderSignature);
1460 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1464 % U n r e g i s t e r X C F I m a g e %
1468 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1470 % UnregisterXCFImage() removes format registrations made by the
1471 % XCF module from the list of supported formats.
1473 % The format of the UnregisterXCFImage method is:
1475 % UnregisterXCFImage(void)
1478 ModuleExport void UnregisterXCFImage(void)
1480 (void) UnregisterMagickInfo("XCF");