1
/* The GIMP -- an image manipulation program
2
* Copyright (C) 1995 Spencer Kimball and Peter Mattis
4
* This program is free software; you can redistribute it and/or modify
5
* it under the terms of the GNU General Public License as published by
6
* the Free Software Foundation; either version 2 of the License, or
7
* (at your option) any later version.
9
* This program is distributed in the hope that it will be useful,
10
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12
* GNU General Public License for more details.
14
* You should have received a copy of the GNU General Public License
15
* along with this program; if not, write to the Free Software
16
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
22
#include <string.h> /* strcmp, memcmp */
24
#include <glib-object.h>
26
#include "libgimpbase/gimpbase.h"
27
#include "libgimpcolor/gimpcolor.h"
29
#include "core/core-types.h"
31
#include "base/tile.h"
32
#include "base/tile-manager.h"
33
#include "base/tile-manager-private.h"
35
#include "config/gimpcoreconfig.h"
37
#include "core/gimp.h"
38
#include "core/gimpcontainer.h"
39
#include "core/gimpdrawable.h"
40
#include "core/gimpgrid.h"
41
#include "core/gimpimage.h"
42
#include "core/gimpimage-grid.h"
43
#include "core/gimpimage-guides.h"
44
#include "core/gimplayer.h"
45
#include "core/gimplayer-floating-sel.h"
46
#include "core/gimplayermask.h"
47
#include "core/gimpparasitelist.h"
48
#include "core/gimpselection.h"
49
#include "core/gimptemplate.h"
50
#include "core/gimpunit.h"
52
#include "text/gimptextlayer.h"
53
#include "text/gimptextlayer-xcf.h"
55
#include "vectors/gimpanchor.h"
56
#include "vectors/gimpstroke.h"
57
#include "vectors/gimpbezierstroke.h"
58
#include "vectors/gimpvectors.h"
59
#include "vectors/gimpvectors-compat.h"
61
#include "xcf-private.h"
66
#include "gimp-intl.h"
68
/* #define GIMP_XCF_PATH_DEBUG */
70
static gboolean xcf_load_image_props (XcfInfo *info,
72
static gboolean xcf_load_layer_props (XcfInfo *info,
78
guint32 *text_layer_flags);
79
static gboolean xcf_load_channel_props (XcfInfo *info,
81
GimpChannel **channel);
82
static gboolean xcf_load_prop (XcfInfo *info,
85
static GimpLayer * xcf_load_layer (XcfInfo *info,
87
static GimpChannel * xcf_load_channel (XcfInfo *info,
89
static GimpLayerMask * xcf_load_layer_mask (XcfInfo *info,
91
static gboolean xcf_load_hierarchy (XcfInfo *info,
93
static gboolean xcf_load_level (XcfInfo *info,
95
static gboolean xcf_load_tile (XcfInfo *info,
97
static gboolean xcf_load_tile_rle (XcfInfo *info,
100
static GimpParasite * xcf_load_parasite (XcfInfo *info);
101
static gboolean xcf_load_old_paths (XcfInfo *info,
103
static gboolean xcf_load_old_path (XcfInfo *info,
105
static gboolean xcf_load_vectors (XcfInfo *info,
107
static gboolean xcf_load_vector (XcfInfo *info,
110
#ifdef SWAP_FROM_FILE
111
static gboolean xcf_swap_func (gint fd,
119
xcf_load_image (Gimp *gimp,
124
GimpChannel *channel;
125
GimpParasite *parasite;
131
gint num_successful_elements = 0;
133
/* read in the image width, height and type */
134
info->cp += xcf_read_int32 (info->fp, (guint32 *) &width, 1);
135
info->cp += xcf_read_int32 (info->fp, (guint32 *) &height, 1);
136
info->cp += xcf_read_int32 (info->fp, (guint32 *) &image_type, 1);
138
gimage = gimp_create_image (gimp,
143
/* read the image properties */
144
if (!xcf_load_image_props (info, gimage))
147
/* check for a GimpGrid parasite */
148
parasite = gimp_image_parasite_find (GIMP_IMAGE (gimage),
149
gimp_grid_parasite_name ());
152
GimpGrid *grid = gimp_grid_from_parasite (parasite);
156
gimp_parasite_list_remove (GIMP_IMAGE (gimage)->parasites,
157
gimp_parasite_name (parasite));
159
gimp_image_set_grid (GIMP_IMAGE (gimage), grid, FALSE);
166
/* read in the offset of the next layer */
167
info->cp += xcf_read_int32 (info->fp, &offset, 1);
169
/* if the offset is 0 then we are at the end
175
/* save the current position as it is where the
176
* next layer offset is stored.
178
saved_pos = info->cp;
180
/* seek to the layer offset */
181
if (! xcf_seek_pos (info, offset, NULL))
184
/* read in the layer */
185
layer = xcf_load_layer (info, gimage);
189
num_successful_elements++;
191
/* add the layer to the image if its not the floating selection */
192
if (layer != info->floating_sel)
193
gimp_image_add_layer (gimage, layer,
194
gimp_container_num_children (gimage->layers));
196
/* restore the saved position so we'll be ready to
197
* read the next offset.
199
if (! xcf_seek_pos (info, saved_pos, NULL))
205
/* read in the offset of the next channel */
206
info->cp += xcf_read_int32 (info->fp, &offset, 1);
208
/* if the offset is 0 then we are at the end
209
* of the channel list.
214
/* save the current position as it is where the
215
* next channel offset is stored.
217
saved_pos = info->cp;
219
/* seek to the channel offset */
220
if (! xcf_seek_pos (info, offset, NULL))
223
/* read in the layer */
224
channel = xcf_load_channel (info, gimage);
228
num_successful_elements++;
230
/* add the channel to the image if its not the selection */
231
if (channel != gimage->selection_mask)
232
gimp_image_add_channel (gimage, channel, -1);
234
/* restore the saved position so we'll be ready to
235
* read the next offset.
237
if (! xcf_seek_pos (info, saved_pos, NULL))
241
if (info->floating_sel && info->floating_sel_drawable)
242
floating_sel_attach (info->floating_sel, info->floating_sel_drawable);
244
if (info->active_layer)
245
gimp_image_set_active_layer (gimage, info->active_layer);
247
if (info->active_channel)
248
gimp_image_set_active_channel (gimage, info->active_channel);
250
gimp_image_set_filename (gimage, info->filename);
252
if (info->tattoo_state > 0)
253
gimp_image_set_tattoo_state (gimage, info->tattoo_state);
258
if (num_successful_elements == 0)
261
g_message ("XCF: This file is corrupt! I have loaded as much\n"
262
"of it as I can, but it is incomplete.");
267
g_message ("XCF: This file is corrupt! I could not even\n"
268
"salvage any partial image data from it.");
270
g_object_unref (gimage);
276
xcf_load_image_props (XcfInfo *info,
284
if (!xcf_load_prop (info, &prop_type, &prop_size))
293
if (info->file_version == 0)
297
g_message (_("XCF warning: version 0 of XCF file format\n"
298
"did not save indexed colormaps correctly.\n"
299
"Substituting grayscale map."));
301
xcf_read_int32 (info->fp, (guint32 *) &gimage->num_cols, 1);
302
gimage->cmap = g_new (guchar, gimage->num_cols * 3);
303
if (!xcf_seek_pos (info, info->cp + gimage->num_cols, NULL))
306
for (i = 0; i<gimage->num_cols; i++)
308
gimage->cmap[i*3+0] = i;
309
gimage->cmap[i*3+1] = i;
310
gimage->cmap[i*3+2] = i;
316
xcf_read_int32 (info->fp, (guint32 *) &gimage->num_cols, 1);
317
gimage->cmap = g_new (guchar, gimage->num_cols * 3);
319
xcf_read_int8 (info->fp,
320
(guint8 *) gimage->cmap, gimage->num_cols * 3);
323
/* discard color map, if image is not indexed, this is just
324
* sanity checking to make sure gimp doesn't end up with an
325
* image state that is impossible.
327
if (gimp_image_base_type (gimage) != GIMP_INDEXED)
329
g_free (gimage->cmap);
331
gimage->num_cols = 0;
335
case PROP_COMPRESSION:
339
info->cp += xcf_read_int8 (info->fp, (guint8 *) &compression, 1);
341
if ((compression != COMPRESS_NONE) &&
342
(compression != COMPRESS_RLE) &&
343
(compression != COMPRESS_ZLIB) &&
344
(compression != COMPRESS_FRACTAL))
346
g_message ("unknown compression type: %d", (int) compression);
350
info->compression = compression;
360
nguides = prop_size / (4 + 1);
361
for (i = 0; i < nguides; i++)
363
info->cp += xcf_read_int32 (info->fp, (guint32 *) &position, 1);
364
info->cp += xcf_read_int8 (info->fp, (guint8 *) &orientation, 1);
366
/* skip -1 guides from old XCFs */
372
case XCF_ORIENTATION_HORIZONTAL:
373
gimp_image_add_hguide (gimage, position, FALSE);
376
case XCF_ORIENTATION_VERTICAL:
377
gimp_image_add_vguide (gimage, position, FALSE);
381
g_message ("guide orientation out of range in XCF file");
386
/* this is silly as the order of guides doesn't really matter,
387
* but it restores the list to it's original order, which
388
* cannot be wrong --Mitch
390
gimage->guides = g_list_reverse (gimage->guides);
394
case PROP_RESOLUTION:
398
info->cp += xcf_read_float (info->fp, &xres, 1);
399
info->cp += xcf_read_float (info->fp, &yres, 1);
400
if (xres < GIMP_MIN_RESOLUTION || xres > GIMP_MAX_RESOLUTION ||
401
yres < GIMP_MIN_RESOLUTION || yres > GIMP_MAX_RESOLUTION)
403
g_message ("Warning, resolution out of range in XCF file");
404
xres = gimage->gimp->config->default_image->xresolution;
405
yres = gimage->gimp->config->default_image->yresolution;
407
gimage->xresolution = xres;
408
gimage->yresolution = yres;
414
info->cp += xcf_read_int32 (info->fp, &info->tattoo_state, 1);
420
glong base = info->cp;
423
while (info->cp - base < prop_size)
425
p = xcf_load_parasite (info);
426
gimp_image_parasite_attach (gimage, p);
427
gimp_parasite_free (p);
429
if (info->cp - base != prop_size)
430
g_message ("Error while loading an image's parasites");
438
info->cp += xcf_read_int32 (info->fp, &unit, 1);
440
if ((unit <= GIMP_UNIT_PIXEL) ||
441
(unit >= _gimp_unit_get_number_of_built_in_units (gimage->gimp)))
443
g_message ("Warning, unit out of range in XCF file, "
444
"falling back to inches");
445
unit = GIMP_UNIT_INCH;
448
gimage->resolution_unit = unit;
453
xcf_load_old_paths (info, gimage);
458
gchar *unit_strings[5];
465
info->cp += xcf_read_float (info->fp, &factor, 1);
466
info->cp += xcf_read_int32 (info->fp, &digits, 1);
467
info->cp += xcf_read_string (info->fp, unit_strings, 5);
469
for (i = 0; i < 5; i++)
470
if (unit_strings[i] == NULL)
471
unit_strings[i] = g_strdup ("");
473
num_units = _gimp_unit_get_number_of_units (gimage->gimp);
475
for (unit = _gimp_unit_get_number_of_built_in_units (gimage->gimp);
476
unit < num_units; unit++)
478
/* if the factor and the identifier match some unit
479
* in unitrc, use the unitrc unit
481
if ((ABS (_gimp_unit_get_factor (gimage->gimp,
482
unit) - factor) < 1e-5) &&
483
(strcmp (unit_strings[0],
484
_gimp_unit_get_identifier (gimage->gimp,
492
if (unit == num_units)
493
unit = _gimp_unit_new (gimage->gimp,
502
gimage->resolution_unit = unit;
504
for (i = 0; i < 5; i++)
505
g_free (unit_strings[i]);
511
guint32 base = info->cp;
513
if (xcf_load_vectors (info, gimage))
515
if (base + prop_size != info->cp)
517
g_warning ("Mismatch in PROP_VECTORS size: skipping %d bytes.",
518
base + prop_size - info->cp);
519
xcf_seek_pos (info, base + prop_size, NULL);
524
/* skip silently since we don't understand the format and
525
* xcf_load_vectors already explained what was wrong
527
xcf_seek_pos (info, base + prop_size, NULL);
534
g_printerr ("unexpected/unknown image property: %d (skipping)",
541
while (prop_size > 0)
543
amount = MIN (16, prop_size);
544
info->cp += xcf_read_int8 (info->fp, buf, amount);
545
prop_size -= MIN (16, amount);
556
xcf_load_layer_props (XcfInfo *info,
559
gboolean *apply_mask,
562
guint32 *text_layer_flags)
569
if (!xcf_load_prop (info, &prop_type, &prop_size))
577
case PROP_ACTIVE_LAYER:
578
info->active_layer = layer;
581
case PROP_FLOATING_SELECTION:
582
info->floating_sel = layer;
584
xcf_read_int32 (info->fp,
585
(guint32 *) &info->floating_sel_offset, 1);
592
info->cp += xcf_read_int32 (info->fp, &opacity, 1);
593
layer->opacity = CLAMP ((gdouble) opacity / 255.0,
594
GIMP_OPACITY_TRANSPARENT,
595
GIMP_OPACITY_OPAQUE);
603
info->cp += xcf_read_int32 (info->fp, (guint32 *) &visible, 1);
604
gimp_item_set_visible (GIMP_ITEM (layer),
605
visible ? TRUE : FALSE, FALSE);
613
info->cp += xcf_read_int32 (info->fp, (guint32 *) &linked, 1);
614
gimp_item_set_linked (GIMP_ITEM (layer),
615
linked ? TRUE : FALSE, FALSE);
619
case PROP_PRESERVE_TRANSPARENCY:
621
xcf_read_int32 (info->fp, (guint32 *) &layer->preserve_trans, 1);
624
case PROP_APPLY_MASK:
625
info->cp += xcf_read_int32 (info->fp, (guint32 *) apply_mask, 1);
629
info->cp += xcf_read_int32 (info->fp, (guint32 *) edit_mask, 1);
633
info->cp += xcf_read_int32 (info->fp, (guint32 *) show_mask, 1);
638
xcf_read_int32 (info->fp,
639
(guint32 *) &GIMP_ITEM (layer)->offset_x, 1);
641
xcf_read_int32 (info->fp,
642
(guint32 *) &GIMP_ITEM (layer)->offset_y, 1);
646
info->cp += xcf_read_int32 (info->fp, (guint32 *) &layer->mode, 1);
650
info->cp += xcf_read_int32 (info->fp,
651
(guint32 *) &GIMP_ITEM (layer)->tattoo,
657
glong base = info->cp;
660
while (info->cp - base < prop_size)
662
p = xcf_load_parasite (info);
663
gimp_item_parasite_attach (GIMP_ITEM (layer), p);
664
gimp_parasite_free (p);
666
if (info->cp - base != prop_size)
667
g_message ("Error while loading a layer's parasites");
671
case PROP_TEXT_LAYER_FLAGS:
672
info->cp += xcf_read_int32 (info->fp, text_layer_flags, 1);
681
g_printerr ("unexpected/unknown layer property: %d (skipping)",
684
while (prop_size > 0)
686
amount = MIN (16, prop_size);
687
info->cp += xcf_read_int8 (info->fp, buf, amount);
688
prop_size -= MIN (16, amount);
699
xcf_load_channel_props (XcfInfo *info,
701
GimpChannel **channel)
708
if (!xcf_load_prop (info, &prop_type, &prop_size))
716
case PROP_ACTIVE_CHANNEL:
717
info->active_channel = *channel;
721
g_object_unref (gimage->selection_mask);
722
gimage->selection_mask =
723
gimp_selection_new (gimage,
724
gimp_item_width (GIMP_ITEM (*channel)),
725
gimp_item_height (GIMP_ITEM (*channel)));
726
g_object_ref (gimage->selection_mask);
727
gimp_item_sink (GIMP_ITEM (gimage->selection_mask));
729
tile_manager_unref (GIMP_DRAWABLE (gimage->selection_mask)->tiles);
730
GIMP_DRAWABLE (gimage->selection_mask)->tiles =
731
GIMP_DRAWABLE (*channel)->tiles;
732
GIMP_DRAWABLE (*channel)->tiles = NULL;
733
g_object_unref (*channel);
734
*channel = gimage->selection_mask;
735
(*channel)->boundary_known = FALSE;
736
(*channel)->bounds_known = FALSE;
743
info->cp += xcf_read_int32 (info->fp, &opacity, 1);
744
(*channel)->color.a = opacity / 255.0;
752
info->cp += xcf_read_int32 (info->fp, (guint32 *) &visible, 1);
753
gimp_item_set_visible (GIMP_ITEM (*channel),
754
visible ? TRUE : FALSE, FALSE);
762
info->cp += xcf_read_int32 (info->fp, (guint32 *) &linked, 1);
763
gimp_item_set_linked (GIMP_ITEM (*channel),
764
linked ? TRUE : FALSE, FALSE);
768
case PROP_SHOW_MASKED:
770
gboolean show_masked;
772
info->cp += xcf_read_int32 (info->fp, (guint32 *) &show_masked, 1);
773
gimp_channel_set_show_masked (*channel, show_masked);
781
info->cp += xcf_read_int8 (info->fp, (guint8 *) col, 3);
783
gimp_rgb_set_uchar (&(*channel)->color, col[0], col[1], col[2]);
789
xcf_read_int32 (info->fp, &GIMP_ITEM (*channel)->tattoo, 1);
794
glong base = info->cp;
797
while ((info->cp - base) < prop_size)
799
p = xcf_load_parasite (info);
800
gimp_item_parasite_attach (GIMP_ITEM (*channel), p);
801
gimp_parasite_free (p);
803
if (info->cp - base != prop_size)
804
g_message ("Error while loading a channel's parasites");
810
g_printerr ("unexpected/unknown channel property: %d (skipping)",
818
while (prop_size > 0)
820
amount = MIN (16, prop_size);
821
info->cp += xcf_read_int8 (info->fp, buf, amount);
822
prop_size -= MIN (16, amount);
833
xcf_load_prop (XcfInfo *info,
837
info->cp += xcf_read_int32 (info->fp, (guint32 *) prop_type, 1);
838
info->cp += xcf_read_int32 (info->fp, (guint32 *) prop_size, 1);
843
xcf_load_layer (XcfInfo *info,
847
GimpLayerMask *layer_mask;
848
guint32 hierarchy_offset;
849
guint32 layer_mask_offset;
850
gboolean apply_mask = TRUE;
851
gboolean edit_mask = FALSE;
852
gboolean show_mask = FALSE;
855
guint32 text_layer_flags = 0;
859
gboolean is_fs_drawable;
862
/* check and see if this is the drawable the floating selection
863
* is attached to. if it is then we'll do the attachment in our caller.
865
is_fs_drawable = (info->cp == info->floating_sel_offset);
867
/* read in the layer width, height, type and name */
868
info->cp += xcf_read_int32 (info->fp, (guint32 *) &width, 1);
869
info->cp += xcf_read_int32 (info->fp, (guint32 *) &height, 1);
870
info->cp += xcf_read_int32 (info->fp, (guint32 *) &type, 1);
871
info->cp += xcf_read_string (info->fp, &name, 1);
873
/* create a new layer */
874
layer = gimp_layer_new (gimage, width, height,
875
type, name, 255, GIMP_NORMAL_MODE);
880
/* read in the layer properties */
881
if (! xcf_load_layer_props (info, gimage, layer,
882
&apply_mask, &edit_mask, &show_mask,
886
/* call the evil text layer hack that might change our layer pointer */
887
active = (info->active_layer == layer);
888
floating = (info->floating_sel == layer);
890
if (gimp_text_layer_xcf_load_hack (&layer))
892
gimp_text_layer_set_xcf_flags (GIMP_TEXT_LAYER (layer),
896
info->active_layer = layer;
898
info->floating_sel = layer;
901
/* read the hierarchy and layer mask offsets */
902
info->cp += xcf_read_int32 (info->fp, &hierarchy_offset, 1);
903
info->cp += xcf_read_int32 (info->fp, &layer_mask_offset, 1);
905
/* read in the hierarchy */
906
if (! xcf_seek_pos (info, hierarchy_offset, NULL))
909
if (! xcf_load_hierarchy (info, GIMP_DRAWABLE (layer)->tiles))
912
/* read in the layer mask */
913
if (layer_mask_offset != 0)
915
if (! xcf_seek_pos (info, layer_mask_offset, NULL))
918
layer_mask = xcf_load_layer_mask (info, gimage);
922
layer_mask->apply_mask = apply_mask;
923
layer_mask->edit_mask = edit_mask;
924
layer_mask->show_mask = show_mask;
926
gimp_layer_add_mask (layer, layer_mask, FALSE);
929
/* attach the floating selection... */
931
info->floating_sel_drawable = GIMP_DRAWABLE (layer);
936
g_object_unref (layer);
941
xcf_load_channel (XcfInfo *info,
944
GimpChannel *channel;
945
guint32 hierarchy_offset;
948
gboolean is_fs_drawable;
950
GimpRGB color = { 0.0, 0.0, 0.0, GIMP_OPACITY_OPAQUE };
952
/* check and see if this is the drawable the floating selection
953
* is attached to. if it is then we'll do the attachment in our caller.
955
is_fs_drawable = (info->cp == info->floating_sel_offset);
957
/* read in the layer width, height and name */
958
info->cp += xcf_read_int32 (info->fp, (guint32 *) &width, 1);
959
info->cp += xcf_read_int32 (info->fp, (guint32 *) &height, 1);
960
info->cp += xcf_read_string (info->fp, &name, 1);
962
/* create a new channel */
963
channel = gimp_channel_new (gimage, width, height, name, &color);
968
/* read in the channel properties */
969
if (!xcf_load_channel_props (info, gimage, &channel))
972
/* read the hierarchy and layer mask offsets */
973
info->cp += xcf_read_int32 (info->fp, &hierarchy_offset, 1);
975
/* read in the hierarchy */
976
if (!xcf_seek_pos (info, hierarchy_offset, NULL))
979
if (!xcf_load_hierarchy (info, GIMP_DRAWABLE (channel)->tiles))
983
info->floating_sel_drawable = GIMP_DRAWABLE (channel);
988
g_object_unref (channel);
992
static GimpLayerMask *
993
xcf_load_layer_mask (XcfInfo *info,
996
GimpLayerMask *layer_mask;
997
GimpChannel *channel;
998
guint32 hierarchy_offset;
1001
gboolean is_fs_drawable;
1003
GimpRGB color = { 0.0, 0.0, 0.0, GIMP_OPACITY_OPAQUE };
1005
/* check and see if this is the drawable the floating selection
1006
* is attached to. if it is then we'll do the attachment in our caller.
1008
is_fs_drawable = (info->cp == info->floating_sel_offset);
1010
/* read in the layer width, height and name */
1011
info->cp += xcf_read_int32 (info->fp, (guint32 *) &width, 1);
1012
info->cp += xcf_read_int32 (info->fp, (guint32 *) &height, 1);
1013
info->cp += xcf_read_string (info->fp, &name, 1);
1015
/* create a new layer mask */
1016
layer_mask = gimp_layer_mask_new (gimage, width, height, name, &color);
1021
/* read in the layer_mask properties */
1022
channel = GIMP_CHANNEL (layer_mask);
1023
if (!xcf_load_channel_props (info, gimage, &channel))
1026
/* read the hierarchy and layer mask offsets */
1027
info->cp += xcf_read_int32 (info->fp, &hierarchy_offset, 1);
1029
/* read in the hierarchy */
1030
if (! xcf_seek_pos (info, hierarchy_offset, NULL))
1033
if (!xcf_load_hierarchy (info, GIMP_DRAWABLE (layer_mask)->tiles))
1036
/* attach the floating selection... */
1038
info->floating_sel_drawable = GIMP_DRAWABLE (layer_mask);
1043
g_object_unref (layer_mask);
1048
xcf_load_hierarchy (XcfInfo *info,
1058
info->cp += xcf_read_int32 (info->fp, (guint32 *) &width, 1);
1059
info->cp += xcf_read_int32 (info->fp, (guint32 *) &height, 1);
1060
info->cp += xcf_read_int32 (info->fp, (guint32 *) &bpp, 1);
1062
/* make sure the values in the file correspond to the values
1063
* calculated when the TileManager was created.
1065
if (width != tile_manager_width (tiles) ||
1066
height != tile_manager_height (tiles) ||
1067
bpp != tile_manager_bpp (tiles))
1070
/* load in the levels...we make sure that the number of levels
1071
* calculated when the TileManager was created is the same
1072
* as the number of levels found in the file.
1075
info->cp += xcf_read_int32 (info->fp, &offset, 1); /* top level */
1077
/* discard offsets for layers below first, if any.
1081
info->cp += xcf_read_int32 (info->fp, &junk, 1);
1085
/* save the current position as it is where the
1086
* next level offset is stored.
1088
saved_pos = info->cp;
1090
/* seek to the level offset */
1091
if (!xcf_seek_pos (info, offset, NULL))
1094
/* read in the level */
1095
if (!xcf_load_level (info, tiles))
1098
/* restore the saved position so we'll be ready to
1099
* read the next offset.
1101
if (!xcf_seek_pos (info, saved_pos, NULL))
1109
xcf_load_level (XcfInfo *info,
1113
guint32 offset, offset2;
1122
info->cp += xcf_read_int32 (info->fp, (guint32 *) &width, 1);
1123
info->cp += xcf_read_int32 (info->fp, (guint32 *) &height, 1);
1125
if (width != tile_manager_width (tiles) ||
1126
height != tile_manager_height (tiles))
1129
/* read in the first tile offset.
1130
* if it is '0', then this tile level is empty
1131
* and we can simply return.
1133
info->cp += xcf_read_int32 (info->fp, &offset, 1);
1137
/* Initialise the reference for the in-memory tile-compression
1141
ntiles = tiles->ntile_rows * tiles->ntile_cols;
1142
for (i = 0; i < ntiles; i++)
1148
g_message ("not enough tiles found in level");
1152
/* save the current position as it is where the
1153
* next tile offset is stored.
1155
saved_pos = info->cp;
1157
/* read in the offset of the next tile so we can calculate the amount
1158
of data needed for this tile*/
1159
info->cp += xcf_read_int32 (info->fp, &offset2, 1);
1161
/* if the offset is 0 then we need to read in the maximum possible
1162
allowing for negative compression */
1164
offset2 = offset + TILE_WIDTH * TILE_WIDTH * 4 * 1.5;
1165
/* 1.5 is probably more
1166
than we need to allow */
1168
/* seek to the tile offset */
1169
if (! xcf_seek_pos (info, offset, NULL))
1172
/* get the tile from the tile manager */
1173
tile = tile_manager_get (tiles, i, TRUE, TRUE);
1175
/* read in the tile */
1176
switch (info->compression)
1179
if (!xcf_load_tile (info, tile))
1183
if (!xcf_load_tile_rle (info, tile, offset2 - offset))
1187
g_error ("xcf: zlib compression unimplemented");
1190
case COMPRESS_FRACTAL:
1191
g_error ("xcf: fractal compression unimplemented");
1198
tile_release (tile, TRUE);
1202
/* To potentially save memory, we compare the
1203
* newly-fetched tile against the last one, and
1204
* if they're the same we copy-on-write mirror one against
1207
if (previous != NULL)
1209
tile_lock (previous);
1210
if (tile_ewidth (tile) == tile_ewidth (previous) &&
1211
tile_eheight (tile) == tile_eheight (previous) &&
1212
tile_bpp (tile) == tile_bpp (previous) &&
1213
memcmp (tile_data_pointer (tile, 0, 0),
1214
tile_data_pointer (previous, 0, 0),
1215
tile_size (tile)) == 0)
1216
tile_manager_map (tiles, i, previous);
1217
tile_release (previous, FALSE);
1219
tile_release (tile, TRUE);
1220
previous = tile_manager_get (tiles, i, FALSE, FALSE);
1222
/* restore the saved position so we'll be ready to
1223
* read the next offset.
1225
if (!xcf_seek_pos (info, saved_pos, NULL))
1228
/* read in the offset of the next tile */
1229
info->cp += xcf_read_int32 (info->fp, &offset, 1);
1234
g_message ("encountered garbage after reading level: %d", offset);
1242
xcf_load_tile (XcfInfo *info,
1245
#ifdef SWAP_FROM_FILE
1247
if (!info->swap_num)
1249
info->ref_count = g_new (int, 1);
1250
info->swap_num = tile_swap_add (info->filename,
1255
tile->swap_num = info->swap_num;
1256
tile->swap_offset = info->cp;
1257
*info->ref_count += 1;
1261
info->cp += xcf_read_int8 (info->fp, tile_data_pointer(tile, 0, 0),
1270
xcf_load_tile_rle (XcfInfo *info,
1281
gint nmemb_read_successfully;
1282
guchar *xcfdata, *xcfodata, *xcfdatalimit;
1284
data = tile_data_pointer (tile, 0, 0);
1285
bpp = tile_bpp (tile);
1287
xcfdata = xcfodata = g_malloc (data_length);
1289
/* we have to use fread instead of xcf_read_* because we may be
1290
reading past the end of the file here */
1291
nmemb_read_successfully = fread ((gchar *) xcfdata, sizeof (gchar),
1292
data_length, info->fp);
1293
info->cp += nmemb_read_successfully;
1295
xcfdatalimit = &xcfodata[nmemb_read_successfully - 1];
1297
for (i = 0; i < bpp; i++)
1299
data = (guchar *) tile_data_pointer (tile, 0, 0) + i;
1300
size = tile_ewidth (tile) * tile_eheight (tile);
1305
if (xcfdata > xcfdatalimit)
1315
length = 255 - (length - 1);
1318
if (xcfdata >= xcfdatalimit)
1323
length = (*xcfdata << 8) + xcfdata[1];
1335
if (&xcfdata[length-1] > xcfdatalimit)
1340
while (length-- > 0)
1351
if (xcfdata >= xcfdatalimit)
1356
length = (*xcfdata << 8) + xcfdata[1];
1368
if (xcfdata > xcfdatalimit)
1375
for (j = 0; j < length; j++)
1392
static GimpParasite *
1393
xcf_load_parasite (XcfInfo *info)
1397
p = g_new (GimpParasite, 1);
1398
info->cp += xcf_read_string (info->fp, &p->name, 1);
1399
info->cp += xcf_read_int32 (info->fp, &p->flags, 1);
1400
info->cp += xcf_read_int32 (info->fp, &p->size, 1);
1401
p->data = g_new (gchar, p->size);
1402
info->cp += xcf_read_int8 (info->fp, p->data, p->size);
1408
xcf_load_old_paths (XcfInfo *info,
1412
guint32 last_selected_row;
1413
GimpVectors *active_vectors;
1415
info->cp += xcf_read_int32 (info->fp, &last_selected_row, 1);
1416
info->cp += xcf_read_int32 (info->fp, &num_paths, 1);
1418
while (num_paths-- > 0)
1419
xcf_load_old_path (info, gimage);
1421
active_vectors = (GimpVectors *)
1422
gimp_container_get_child_by_index (gimage->vectors, last_selected_row);
1425
gimp_image_set_active_vectors (gimage, active_vectors);
1431
xcf_load_old_path (XcfInfo *info,
1439
guint32 version; /* changed from num_paths */
1440
GimpTattoo tattoo = 0;
1441
GimpVectors *vectors;
1442
GimpVectorsCompatPoint *points;
1445
info->cp += xcf_read_string (info->fp, &name, 1);
1446
info->cp += xcf_read_int32 (info->fp, &locked, 1);
1447
info->cp += xcf_read_int8 (info->fp, &state, 1);
1448
info->cp += xcf_read_int32 (info->fp, &closed, 1);
1449
info->cp += xcf_read_int32 (info->fp, &num_points, 1);
1450
info->cp += xcf_read_int32 (info->fp, &version, 1);
1456
/* Had extra type field and points are stored as doubles */
1457
info->cp += xcf_read_int32 (info->fp, (guint32 *) &dummy, 1);
1459
else if (version == 3)
1463
/* Has extra tatto field */
1464
info->cp += xcf_read_int32 (info->fp, (guint32 *) &dummy, 1);
1465
info->cp += xcf_read_int32 (info->fp, (guint32 *) &tattoo, 1);
1467
else if (version != 1)
1469
g_warning ("Unknown path type. Possibly corrupt XCF file");
1474
/* skip empty compatibility paths */
1475
if (num_points == 0)
1478
points = g_new0 (GimpVectorsCompatPoint, num_points);
1480
for (i = 0; i < num_points; i++)
1487
info->cp += xcf_read_int32 (info->fp, &points[i].type, 1);
1488
info->cp += xcf_read_int32 (info->fp, (guint32 *) &x, 1);
1489
info->cp += xcf_read_int32 (info->fp, (guint32 *) &y, 1);
1499
info->cp += xcf_read_int32 (info->fp, &points[i].type, 1);
1500
info->cp += xcf_read_float (info->fp, &x, 1);
1501
info->cp += xcf_read_float (info->fp, &y, 1);
1508
vectors = gimp_vectors_compat_new (gimage, name, points, num_points, closed);
1513
GIMP_ITEM (vectors)->linked = locked;
1516
GIMP_ITEM (vectors)->tattoo = tattoo;
1518
gimp_image_add_vectors (gimage, vectors,
1519
gimp_container_num_children (gimage->vectors));
1525
xcf_load_vectors (XcfInfo *info,
1529
guint32 active_index;
1531
GimpVectors *active_vectors;
1534
#ifdef GIMP_XCF_PATH_DEBUG
1535
g_printerr ("xcf_load_vectors\n");
1540
info->cp += xcf_read_int32 (info->fp, &version, 1);
1544
g_message ("Unknown vectors version: %d (skipping)", version);
1548
info->cp += xcf_read_int32 (info->fp, &active_index, 1);
1549
info->cp += xcf_read_int32 (info->fp, &num_paths, 1);
1551
#ifdef GIMP_XCF_PATH_DEBUG
1552
g_printerr ("%d paths (active: %d)\n", num_paths, active_index);
1555
while (num_paths-- > 0)
1556
if (! xcf_load_vector (info, gimage))
1559
active_vectors = (GimpVectors *)
1560
gimp_container_get_child_by_index (gimage->vectors, active_index);
1563
gimp_image_set_active_vectors (gimage, active_vectors);
1565
#ifdef GIMP_XCF_PATH_DEBUG
1566
g_printerr ("xcf_load_vectors: loaded %d bytes\n", info->cp - base);
1572
xcf_load_vector (XcfInfo *info,
1576
GimpTattoo tattoo = 0;
1579
guint32 num_parasites;
1580
guint32 num_strokes;
1581
GimpVectors *vectors;
1584
#ifdef GIMP_XCF_PATH_DEBUG
1585
g_printerr ("xcf_load_vector\n");
1588
info->cp += xcf_read_string (info->fp, &name, 1);
1589
info->cp += xcf_read_int32 (info->fp, &tattoo, 1);
1590
info->cp += xcf_read_int32 (info->fp, &visible, 1);
1591
info->cp += xcf_read_int32 (info->fp, &linked, 1);
1592
info->cp += xcf_read_int32 (info->fp, &num_parasites, 1);
1593
info->cp += xcf_read_int32 (info->fp, &num_strokes, 1);
1595
#ifdef GIMP_XCF_PATH_DEBUG
1596
g_printerr ("name: %s, tattoo: %d, visible: %d, linked: %d, num_parasites %d, "
1598
name, tattoo, visible, linked, num_parasites, num_strokes);
1601
vectors = gimp_vectors_new (gimage, name);
1603
GIMP_ITEM (vectors)->visible = visible ? TRUE : FALSE;
1604
GIMP_ITEM (vectors)->linked = linked ? TRUE : FALSE;
1607
GIMP_ITEM (vectors)->tattoo = tattoo;
1609
for (i = 0; i < num_parasites; i++)
1611
GimpParasite *parasite;
1613
parasite = xcf_load_parasite (info);
1618
gimp_item_parasite_attach (GIMP_ITEM (vectors), parasite);
1619
gimp_parasite_free (parasite);
1622
for (i = 0; i < num_strokes; i++)
1624
gint stroke_type_id;
1627
gint num_control_points;
1629
gfloat coords[6] = { 0.0, 0.0, 1.0, 0.5, 0.5, 0.5 };
1633
GValueArray *control_points;
1634
GValue value = { 0, };
1638
g_value_init (&value, GIMP_TYPE_ANCHOR);
1640
info->cp += xcf_read_int32 (info->fp, &stroke_type_id, 1);
1641
info->cp += xcf_read_int32 (info->fp, &closed, 1);
1642
info->cp += xcf_read_int32 (info->fp, &num_axes, 1);
1643
info->cp += xcf_read_int32 (info->fp, &num_control_points, 1);
1645
#ifdef GIMP_XCF_PATH_DEBUG
1646
g_printerr ("stroke_type: %d, closed: %d, num_axes %d, len %d\n",
1647
stroke_type_id, closed, num_axes, num_control_points);
1650
switch (stroke_type_id)
1652
case XCF_STROKETYPE_BEZIER_STROKE:
1653
stroke_type = GIMP_TYPE_BEZIER_STROKE;
1657
g_printerr ("skipping unknown stroke type\n");
1659
info->cp + 4 * num_axes * num_control_points,
1664
control_points = g_value_array_new (num_control_points);
1666
anchor.selected = FALSE;
1668
for (j = 0; j < num_control_points; j++)
1670
info->cp += xcf_read_int32 (info->fp, &type, 1);
1671
info->cp += xcf_read_float (info->fp, coords, num_axes);
1674
anchor.position.x = coords[0];
1675
anchor.position.y = coords[1];
1676
anchor.position.pressure = coords[2];
1677
anchor.position.xtilt = coords[3];
1678
anchor.position.ytilt = coords[4];
1679
anchor.position.wheel = coords[5];
1681
g_value_set_boxed (&value, &anchor);
1682
g_value_array_append (control_points, &value);
1684
#ifdef GIMP_XCF_PATH_DEBUG
1685
g_printerr ("Anchor: %d, (%f, %f, %f, %f, %f, %f)\n", type,
1686
coords[0], coords[1], coords[2], coords[3],
1687
coords[4], coords[5]);
1691
g_value_unset (&value);
1693
stroke = g_object_new (stroke_type,
1695
"control-points", control_points,
1698
gimp_vectors_stroke_add (vectors, stroke);
1701
gimp_image_add_vectors (gimage, vectors,
1702
gimp_container_num_children (gimage->vectors));
1707
#ifdef SWAP_FROM_FILE
1710
xcf_swap_func (gint fd,
1723
lseek (fd, tile->swap_offset, SEEK_SET);
1725
bytes = tile_size (tile);
1732
err = read (fd, tile->data + bytes - nleft, nleft);
1733
} while ((err == -1) && ((errno == EAGAIN) || (errno == EINTR)));
1737
g_message ("unable to read tile data from xcf file: "
1738
"%d ( %d ) bytes read", err, nleft);
1749
ref_count = user_data;
1751
if (*ref_count == 0)
1753
tile_swap_remove (tile->swap_num);
1758
tile->swap_offset = -1;