xcf-load.c 95.1 KB
Newer Older
1
/* GIMP - The GNU Image Manipulation Program
Michael Natterer's avatar
Michael Natterer committed
2
3
 * Copyright (C) 1995 Spencer Kimball and Peter Mattis
 *
4
 * This program is free software: you can redistribute it and/or modify
Michael Natterer's avatar
Michael Natterer committed
5
 * it under the terms of the GNU General Public License as published by
6
 * the Free Software Foundation; either version 3 of the License, or
Michael Natterer's avatar
Michael Natterer committed
7
8
9
10
11
12
13
14
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
15
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
Michael Natterer's avatar
Michael Natterer committed
16
17
18
19
 */

#include "config.h"

20
#include <string.h>
21
#include <zlib.h>
Michael Natterer's avatar
Michael Natterer committed
22

23
#include <cairo.h>
24
#include <gegl.h>
25
#include <gdk-pixbuf/gdk-pixbuf.h>
Michael Natterer's avatar
Michael Natterer committed
26
27
28
29
30
31

#include "libgimpbase/gimpbase.h"
#include "libgimpcolor/gimpcolor.h"

#include "core/core-types.h"

32
33
#include "config/gimpcoreconfig.h"

34
#include "gegl/gimp-babl.h"
35
36
#include "gegl/gimp-gegl-tile-compat.h"

37
#include "core/gimp.h"
Michael Natterer's avatar
Michael Natterer committed
38
#include "core/gimpcontainer.h"
39
#include "core/gimpdrawable-private.h" /* eek */
40
#include "core/gimpgrid.h"
41
#include "core/gimpgrouplayer.h"
Michael Natterer's avatar
Michael Natterer committed
42
#include "core/gimpimage.h"
43
#include "core/gimpimage-colormap.h"
44
#include "core/gimpimage-grid.h"
45
#include "core/gimpimage-guides.h"
46
#include "core/gimpimage-metadata.h"
47
#include "core/gimpimage-private.h"
48
#include "core/gimpimage-sample-points.h"
49
#include "core/gimpimage-symmetry.h"
50
#include "core/gimpimage-undo.h"
51
#include "core/gimpitemstack.h"
52
#include "core/gimplayer-floating-selection.h"
53
#include "core/gimplayer-new.h"
Michael Natterer's avatar
Michael Natterer committed
54
#include "core/gimplayermask.h"
55
#include "core/gimpparasitelist.h"
56
#include "core/gimpprogress.h"
57
#include "core/gimpselection.h"
58
#include "core/gimpsymmetry.h"
59
#include "core/gimptemplate.h"
Michael Natterer's avatar
Michael Natterer committed
60

61
62
#include "operations/layer-modes/gimp-layer-modes.h"

63
#include "text/gimptextlayer.h"
64
#include "text/gimptextlayer-xcf.h"
65

66
67
68
#include "vectors/gimpanchor.h"
#include "vectors/gimpstroke.h"
#include "vectors/gimpbezierstroke.h"
69
#include "vectors/gimpvectors.h"
70
#include "vectors/gimpvectors-compat.h"
71

Michael Natterer's avatar
Michael Natterer committed
72
73
74
75
#include "xcf-private.h"
#include "xcf-load.h"
#include "xcf-read.h"
#include "xcf-seek.h"
76
#include "xcf-utils.h"
Michael Natterer's avatar
Michael Natterer committed
77

78
#include "gimp-log.h"
79
#include "gimp-intl.h"
Michael Natterer's avatar
Michael Natterer committed
80

81

82
83
#define MAX_XCF_PARASITE_DATA_LEN (256L * 1024 * 1024)

84
/* #define GIMP_XCF_PATH_DEBUG */
Michael Natterer's avatar
Michael Natterer committed
85

86

87
88
89
90
91
92
93
94
95
96
97
98
static void            xcf_load_add_masks     (GimpImage     *image);
static gboolean        xcf_load_image_props   (XcfInfo       *info,
                                               GimpImage     *image);
static gboolean        xcf_load_layer_props   (XcfInfo       *info,
                                               GimpImage     *image,
                                               GimpLayer    **layer,
                                               GList        **item_path,
                                               gboolean      *apply_mask,
                                               gboolean      *edit_mask,
                                               gboolean      *show_mask,
                                               guint32       *text_layer_flags,
                                               guint32       *group_layer_flags);
99
static gboolean        xcf_check_layer_props  (XcfInfo       *info,
100
101
102
                                               GList        **item_path,
                                               gboolean      *is_group_layer,
                                               gboolean      *is_text_layer);
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
static gboolean        xcf_load_channel_props (XcfInfo       *info,
                                               GimpImage     *image,
                                               GimpChannel  **channel);
static gboolean        xcf_load_prop          (XcfInfo       *info,
                                               PropType      *prop_type,
                                               guint32       *prop_size);
static GimpLayer     * xcf_load_layer         (XcfInfo       *info,
                                               GimpImage     *image,
                                               GList        **item_path);
static GimpChannel   * xcf_load_channel       (XcfInfo       *info,
                                               GimpImage     *image);
static GimpLayerMask * xcf_load_layer_mask    (XcfInfo       *info,
                                               GimpImage     *image);
static gboolean        xcf_load_buffer        (XcfInfo       *info,
                                               GeglBuffer    *buffer);
static gboolean        xcf_load_level         (XcfInfo       *info,
                                               GeglBuffer    *buffer);
static gboolean        xcf_load_tile          (XcfInfo       *info,
                                               GeglBuffer    *buffer,
122
123
                                               GeglRectangle *tile_rect,
                                               const Babl    *format);
124
125
126
static gboolean        xcf_load_tile_rle      (XcfInfo       *info,
                                               GeglBuffer    *buffer,
                                               GeglRectangle *tile_rect,
127
                                               const Babl    *format,
128
                                               gint           data_length);
129
130
131
132
133
static gboolean        xcf_load_tile_zlib     (XcfInfo       *info,
                                               GeglBuffer    *buffer,
                                               GeglRectangle *tile_rect,
                                               const Babl    *format,
                                               gint           data_length);
134
135
136
137
138
139
140
141
142
143
144
145
static GimpParasite  * xcf_load_parasite      (XcfInfo       *info);
static gboolean        xcf_load_old_paths     (XcfInfo       *info,
                                               GimpImage     *image);
static gboolean        xcf_load_old_path      (XcfInfo       *info,
                                               GimpImage     *image);
static gboolean        xcf_load_vectors       (XcfInfo       *info,
                                               GimpImage     *image);
static gboolean        xcf_load_vector        (XcfInfo       *info,
                                               GimpImage     *image);

static gboolean        xcf_skip_unknown_prop  (XcfInfo       *info,
                                               gsize          size);
146

147
148
149
150
151
static gboolean        xcf_item_path_is_parent (GList        *path,
                                                GList        *parent_path);
static void            xcf_fix_item_path       (GimpLayer    *layer,
                                                GList       **path,
                                                GList        *broken_paths);
Michael Natterer's avatar
Michael Natterer committed
152

153
154
155
156
157
158
159
#define xcf_progress_update(info) G_STMT_START  \
  {                                             \
    if (info->progress)                         \
      gimp_progress_pulse (info->progress);     \
  } G_STMT_END


Michael Natterer's avatar
Michael Natterer committed
160
GimpImage *
161
162
163
xcf_load_image (Gimp     *gimp,
                XcfInfo  *info,
                GError  **error)
Michael Natterer's avatar
Michael Natterer committed
164
{
165
  GimpImage          *image = NULL;
166
  const GimpParasite *parasite;
167
  gboolean            has_metadata = FALSE;
168
169
  goffset             saved_pos;
  goffset             offset;
170
171
172
  gint                width;
  gint                height;
  gint                image_type;
173
  GimpPrecision       precision = GIMP_PRECISION_U8_GAMMA;
174
  gint                num_successful_elements = 0;
175
176
177
  gint                n_broken_layers         = 0;
  gint                n_broken_channels       = 0;
  GList              *broken_paths            = NULL;
178
  GList              *group_layers            = NULL;
179
180
  GList              *syms;
  GList              *iter;
Michael Natterer's avatar
Michael Natterer committed
181
182

  /* read in the image width, height and type */
183
184
185
  xcf_read_int32 (info, (guint32 *) &width, 1);
  xcf_read_int32 (info, (guint32 *) &height, 1);
  xcf_read_int32 (info, (guint32 *) &image_type, 1);
186
  if (image_type < GIMP_RGB || image_type > GIMP_INDEXED)
187
    goto hard_error;
Michael Natterer's avatar
Michael Natterer committed
188

189
190
191
192
193
194
195
196
197
198
  /* Be lenient with corrupt image dimensions.
   * Hopefully layer dimensions will be valid. */
  if (width <= 0 || height <= 0 ||
      width > GIMP_MAX_IMAGE_SIZE || height > GIMP_MAX_IMAGE_SIZE)
    {
      GIMP_LOG (XCF, "Invalid image size %d x %d, setting to 1x1.", width, height);
      width  = 1;
      height = 1;
    }

199
  if (info->file_version >= 4)
200
201
202
    {
      gint p;

203
      xcf_read_int32 (info, (guint32 *) &p, 1);
204
205
206
207
208
209
210
211
212
213
214
215
216
217

      if (info->file_version == 4)
        {
          switch (p)
            {
            case 0: precision = GIMP_PRECISION_U8_GAMMA;     break;
            case 1: precision = GIMP_PRECISION_U16_GAMMA;    break;
            case 2: precision = GIMP_PRECISION_U32_LINEAR;   break;
            case 3: precision = GIMP_PRECISION_HALF_LINEAR;  break;
            case 4: precision = GIMP_PRECISION_FLOAT_LINEAR; break;
            default:
              goto hard_error;
            }
        }
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
      else if (info->file_version == 5 ||
               info->file_version == 6)
        {
          switch (p)
            {
            case 100: precision = GIMP_PRECISION_U8_LINEAR; break;
            case 150: precision = GIMP_PRECISION_U8_GAMMA; break;
            case 200: precision = GIMP_PRECISION_U16_LINEAR; break;
            case 250: precision = GIMP_PRECISION_U16_GAMMA; break;
            case 300: precision = GIMP_PRECISION_U32_LINEAR; break;
            case 350: precision = GIMP_PRECISION_U32_GAMMA; break;
            case 400: precision = GIMP_PRECISION_HALF_LINEAR; break;
            case 450: precision = GIMP_PRECISION_HALF_GAMMA; break;
            case 500: precision = GIMP_PRECISION_FLOAT_LINEAR; break;
            case 550: precision = GIMP_PRECISION_FLOAT_GAMMA; break;
            default:
              goto hard_error;
            }
        }
237
238
239
240
241
      else
        {
          precision = p;
        }
    }
242

243
244
245
  GIMP_LOG (XCF, "version=%d, width=%d, height=%d, image_type=%d, precision=%d",
            info->file_version, width, height, image_type, precision);

246
247
248
249
250
251
252
253
  if (! gimp_babl_is_valid (image_type, precision))
    {
      gimp_message_literal (gimp, G_OBJECT (info->progress),
                            GIMP_MESSAGE_ERROR,
                            _("Invalid image mode and precision combination."));
      goto hard_error;
    }

254
  image = gimp_create_image (gimp, width, height, image_type, precision,
255
                             FALSE);
Michael Natterer's avatar
Michael Natterer committed
256

257
  gimp_image_undo_disable (image);
258

259
260
  xcf_progress_update (info);

Michael Natterer's avatar
Michael Natterer committed
261
  /* read the image properties */
262
  if (! xcf_load_image_props (info, image))
Michael Natterer's avatar
Michael Natterer committed
263
264
    goto hard_error;

265
266
  GIMP_LOG (XCF, "image props loaded");

267
  /* check for a GimpGrid parasite */
268
  parasite = gimp_image_parasite_find (GIMP_IMAGE (image),
269
270
271
272
273
274
275
                                       gimp_grid_parasite_name ());
  if (parasite)
    {
      GimpGrid *grid = gimp_grid_from_parasite (parasite);

      if (grid)
        {
276
277
278
          GimpImagePrivate *private = GIMP_IMAGE_GET_PRIVATE (image);

          gimp_parasite_list_remove (private->parasites,
279
280
                                     gimp_parasite_name (parasite));

281
          gimp_image_set_grid (GIMP_IMAGE (image), grid, FALSE);
Sven Neumann's avatar
Sven Neumann committed
282
          g_object_unref (grid);
283
284
285
        }
    }

286
287
288
289
290
  /* check for a metadata parasite */
  parasite = gimp_image_parasite_find (GIMP_IMAGE (image),
                                       "gimp-image-metadata");
  if (parasite)
    {
291
292
      GimpImagePrivate *private  = GIMP_IMAGE_GET_PRIVATE (image);
      GimpMetadata     *metadata = NULL;
293
294
295
      const gchar      *meta_string;

      meta_string = (gchar *) gimp_parasite_data (parasite);
296
297
298

      if (meta_string)
        metadata = gimp_metadata_deserialize (meta_string);
299
300
301
302
303
304
305
306
307
308
309
310
311

      if (metadata)
        {
          has_metadata = TRUE;

          gimp_image_set_metadata (image, metadata, FALSE);
          g_object_unref (metadata);
        }

      gimp_parasite_list_remove (private->parasites,
                                 gimp_parasite_name (parasite));
    }

312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
  /* check for symmetry parasites */
  syms = gimp_image_symmetry_list ();
  for (iter = syms; iter; iter = g_list_next (iter))
    {
      GType  type = (GType) iter->data;
      gchar *parasite_name = gimp_symmetry_parasite_name (type);

      parasite = gimp_image_parasite_find (image,
                                           parasite_name);
      g_free (parasite_name);
      if (parasite)
        {
          GimpSymmetry *sym = gimp_symmetry_from_parasite (parasite,
                                                           image,
                                                           type);

          if (sym)
            {
              GimpImagePrivate *private = GIMP_IMAGE_GET_PRIVATE (image);

              gimp_parasite_list_remove (private->parasites,
                                         gimp_parasite_name (parasite));

              gimp_image_symmetry_add (image, sym);

              g_signal_emit_by_name (sym, "active-changed", NULL);
              if (sym->active)
                gimp_image_set_active_symmetry (image, type);
            }
        }
    }
  g_list_free (syms);

345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
  /* migrate the old "exif-data" parasite */
  parasite = gimp_image_parasite_find (GIMP_IMAGE (image),
                                       "exif-data");
  if (parasite)
    {
      GimpImagePrivate *private = GIMP_IMAGE_GET_PRIVATE (image);

      if (has_metadata)
        {
          g_printerr ("xcf-load: inconsistent metadata discovered: XCF file "
                      "has both 'gimp-image-metadata' and 'exif-data' "
                      "parasites, dropping old 'exif-data'\n");
        }
      else
        {
360
361
          GimpMetadata *metadata = gimp_image_get_metadata (image);
          GError       *my_error = NULL;
362

363
364
365
366
          if (metadata)
            g_object_ref (metadata);
          else
            metadata = gimp_metadata_new ();
367
368
369
370
371
372
373
374
375

          if (! gimp_metadata_set_from_exif (metadata,
                                             gimp_parasite_data (parasite),
                                             gimp_parasite_data_size (parasite),
                                             &my_error))
            {
              gimp_message (gimp, G_OBJECT (info->progress),
                            GIMP_MESSAGE_WARNING,
                            _("Corrupt 'exif-data' parasite discovered.\n"
376
                              "Exif data could not be migrated: %s"),
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
                            my_error->message);
              g_clear_error (&my_error);
            }
          else
            {
              gimp_image_set_metadata (image, metadata, FALSE);
            }

          g_object_unref (metadata);
        }

      gimp_parasite_list_remove (private->parasites,
                                 gimp_parasite_name (parasite));
    }

  /* migrate the old "gimp-metadata" parasite */
  parasite = gimp_image_parasite_find (GIMP_IMAGE (image),
                                       "gimp-metadata");
  if (parasite)
    {
397
398
399
      GimpImagePrivate *private    = GIMP_IMAGE_GET_PRIVATE (image);
      const gchar      *xmp_data   = gimp_parasite_data (parasite);
      gint              xmp_length = gimp_parasite_data_size (parasite);
400
401
402
403
404
405
406

      if (has_metadata)
        {
          g_printerr ("xcf-load: inconsistent metadata discovered: XCF file "
                      "has both 'gimp-image-metadata' and 'gimp-metadata' "
                      "parasites, dropping old 'gimp-metadata'\n");
        }
407
408
409
410
411
412
413
414
      else if (xmp_length < 14 ||
               strncmp (xmp_data, "GIMP_XMP_1", 10) != 0)
        {
          gimp_message (gimp, G_OBJECT (info->progress),
                        GIMP_MESSAGE_WARNING,
                        _("Corrupt 'gimp-metadata' parasite discovered.\n"
                          "XMP data could not be migrated."));
        }
415
416
      else
        {
417
418
          GimpMetadata *metadata = gimp_image_get_metadata (image);
          GError       *my_error = NULL;
419

420
421
422
423
          if (metadata)
            g_object_ref (metadata);
          else
            metadata = gimp_metadata_new ();
424
425

          if (! gimp_metadata_set_from_xmp (metadata,
426
427
                                            (const guint8 *) xmp_data + 10,
                                            xmp_length - 10,
428
429
                                            &my_error))
            {
430
431
432
433
              /* XMP metadata from 2.8.x or earlier can be really messed up.
               * Let's make the message more user friendly so they will
               * understand that we can't do anything about it.
               * See issue #987. */
434
435
              gimp_message (gimp, G_OBJECT (info->progress),
                            GIMP_MESSAGE_WARNING,
436
437
438
439
                            _("Corrupt XMP metadata saved by an older version of "
                              "GIMP could not be converted and will be ignored.\n"
                              "If you don't know what XMP is, you most likely don't "
                              "need it. Reported error: %s."),
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
                            my_error->message);
              g_clear_error (&my_error);
            }
          else
            {
              gimp_image_set_metadata (image, metadata, FALSE);
            }

          g_object_unref (metadata);
        }

      gimp_parasite_list_remove (private->parasites,
                                 gimp_parasite_name (parasite));
    }

455
  /* check for a gimp-xcf-compatibility-mode parasite */
456
  parasite = gimp_image_parasite_find (GIMP_IMAGE (image),
457
                                       "gimp-xcf-compatibility-mode");
458
459
460
461
  if (parasite)
    {
      GimpImagePrivate *private = GIMP_IMAGE_GET_PRIVATE (image);

462
      /* just ditch it, it's unused but shouldn't be re-saved */
463
464
465
      gimp_parasite_list_remove (private->parasites,
                                 gimp_parasite_name (parasite));
    }
466

467
468
  xcf_progress_update (info);

Michael Natterer's avatar
Michael Natterer committed
469
470
  while (TRUE)
    {
471
      GimpLayer *layer;
472
      GList     *item_path = NULL;
473

Michael Natterer's avatar
Michael Natterer committed
474
      /* read in the offset of the next layer */
475
      xcf_read_offset (info, &offset, 1);
Michael Natterer's avatar
Michael Natterer committed
476
477
478
479
480

      /* if the offset is 0 then we are at the end
       *  of the layer list.
       */
      if (offset == 0)
481
        break;
Michael Natterer's avatar
Michael Natterer committed
482
483
484
485
486
487

      /* save the current position as it is where the
       *  next layer offset is stored.
       */
      saved_pos = info->cp;

488
489
490
491
492
493
494
      if (offset < saved_pos)
        {
          GIMP_LOG (XCF, "Invalid layer offset: %" G_GOFFSET_FORMAT
                    " at offset: %" G_GOFFSET_FORMAT, offset, saved_pos);
          goto error;
        }

Michael Natterer's avatar
Michael Natterer committed
495
      /* seek to the layer offset */
496
497
      if (! xcf_seek_pos (info, offset, NULL))
        goto error;
Michael Natterer's avatar
Michael Natterer committed
498
499

      /* read in the layer */
500
      layer = xcf_load_layer (info, image, &item_path);
501
502
503
504
505
      if (! layer)
        {
          n_broken_layers++;

          if (! xcf_seek_pos (info, saved_pos, NULL))
506
507
508
509
510
511
            {
              if (item_path)
                g_list_free (item_path);

              goto error;
            }
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533

          /* Don't just stop at the first broken layer. Load as much as
           * possible.
           */
          if (! item_path)
            {
              GimpContainer *layers = gimp_image_get_layers (image);

              item_path = g_list_prepend (NULL,
                                          GUINT_TO_POINTER (gimp_container_get_n_children (layers)));

              broken_paths = g_list_prepend (broken_paths, item_path);
            }

          continue;
        }

      if (broken_paths && item_path)
        {
          /* Item paths may be a problem when layers are missing. */
          xcf_fix_item_path (layer, &item_path, broken_paths);
        }
Michael Natterer's avatar
Michael Natterer committed
534
535
536

      num_successful_elements++;

537
538
      xcf_progress_update (info);

539
540
541
542
543
544
545
546
547
548
      /* suspend layer-group size updates */
      if (GIMP_IS_GROUP_LAYER (layer))
        {
          GimpGroupLayer *group = GIMP_GROUP_LAYER (layer);

          group_layers = g_list_prepend (group_layers, group);

          gimp_group_layer_suspend_resize (group, FALSE);
        }

Michael Natterer's avatar
Michael Natterer committed
549
550
      /* add the layer to the image if its not the floating selection */
      if (layer != info->floating_sel)
551
552
553
554
555
556
557
        {
          GimpContainer *layers = gimp_image_get_layers (image);
          GimpContainer *container;
          GimpLayer     *parent;

          if (item_path)
            {
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
              if (info->floating_sel)
                {
                  /* there is a floating selection, but it will get
                   * added after all layers are loaded, so toplevel
                   * layer indices are off-by-one. Adjust item paths
                   * accordingly:
                   */
                  gint toplevel_index;

                  toplevel_index = GPOINTER_TO_UINT (item_path->data);

                  toplevel_index--;

                  item_path->data = GUINT_TO_POINTER (toplevel_index);
                }

574
575
576
577
578
579
              parent = GIMP_LAYER
                (gimp_item_stack_get_parent_by_path (GIMP_ITEM_STACK (layers),
                                                     item_path,
                                                     NULL));

              container = gimp_viewable_get_children (GIMP_VIEWABLE (parent));
Massimo Valentini's avatar
Massimo Valentini committed
580
581

              g_list_free (item_path);
582
583
584
585
586
587
588
589
590
591
592
593
            }
          else
            {
              parent    = NULL;
              container = layers;
            }

          gimp_image_add_layer (image, layer,
                                parent,
                                gimp_container_get_n_children (container),
                                FALSE);
        }
Michael Natterer's avatar
Michael Natterer committed
594
595
596
597

      /* restore the saved position so we'll be ready to
       *  read the next offset.
       */
598
599
      if (! xcf_seek_pos (info, saved_pos, NULL))
        goto error;
Michael Natterer's avatar
Michael Natterer committed
600
601
    }

602
603
604
605
606
607
608
609
610
  /* resume layer-group size updates, in reverse order */
  for (iter = group_layers; iter; iter = g_list_next (iter))
    {
      GimpGroupLayer *group = iter->data;

      gimp_group_layer_resume_resize (group, FALSE);
    }
  g_clear_pointer (&group_layers, g_list_free);

611
  if (broken_paths)
612
613
614
615
    {
      g_list_free_full (broken_paths, (GDestroyNotify) g_list_free);
      broken_paths = NULL;
    }
616

Michael Natterer's avatar
Michael Natterer committed
617
618
  while (TRUE)
    {
619
620
      GimpChannel *channel;

Michael Natterer's avatar
Michael Natterer committed
621
      /* read in the offset of the next channel */
622
      xcf_read_offset (info, &offset, 1);
Michael Natterer's avatar
Michael Natterer committed
623
624
625
626
627

      /* if the offset is 0 then we are at the end
       *  of the channel list.
       */
      if (offset == 0)
628
        break;
Michael Natterer's avatar
Michael Natterer committed
629
630
631
632
633
634

      /* save the current position as it is where the
       *  next channel offset is stored.
       */
      saved_pos = info->cp;

635
636
637
638
639
640
641
      if (offset < saved_pos)
        {
          GIMP_LOG (XCF, "Invalid channel offset: %" G_GOFFSET_FORMAT
                    " at offset: % "G_GOFFSET_FORMAT, offset, saved_pos);
          goto error;
        }

Michael Natterer's avatar
Michael Natterer committed
642
      /* seek to the channel offset */
643
644
      if (! xcf_seek_pos (info, offset, NULL))
        goto error;
Michael Natterer's avatar
Michael Natterer committed
645

646
      /* read in the channel */
647
      channel = xcf_load_channel (info, image);
Michael Natterer's avatar
Michael Natterer committed
648
      if (!channel)
649
650
        {
          n_broken_channels++;
651
          GIMP_LOG (XCF, "Failed to load channel.");
652
653
654
655
656
657

          if (! xcf_seek_pos (info, saved_pos, NULL))
            goto error;

          continue;
        }
Michael Natterer's avatar
Michael Natterer committed
658
659
660

      num_successful_elements++;

661
662
      xcf_progress_update (info);

Michael Natterer's avatar
Michael Natterer committed
663
      /* add the channel to the image if its not the selection */
664
      if (channel != gimp_image_get_mask (image))
665
        gimp_image_add_channel (image, channel,
666
                                NULL, /* FIXME tree */
667
                                gimp_container_get_n_children (gimp_image_get_channels (image)),
668
                                FALSE);
Michael Natterer's avatar
Michael Natterer committed
669
670
671
672

      /* restore the saved position so we'll be ready to
       *  read the next offset.
       */
673
674
      if (! xcf_seek_pos (info, saved_pos, NULL))
        goto error;
Michael Natterer's avatar
Michael Natterer committed
675
676
    }

677
678
  if (n_broken_layers == 0 && n_broken_channels == 0)
    xcf_load_add_masks (image);
679

680
681
682
  if (info->floating_sel && info->floating_sel_drawable)
    floating_sel_attach (info->floating_sel, info->floating_sel_drawable);

Michael Natterer's avatar
Michael Natterer committed
683
  if (info->active_layer)
684
    gimp_image_set_active_layer (image, info->active_layer);
Michael Natterer's avatar
Michael Natterer committed
685
686

  if (info->active_channel)
687
    gimp_image_set_active_channel (image, info->active_channel);
Michael Natterer's avatar
Michael Natterer committed
688

689
690
  if (info->file)
    gimp_image_set_file (image, info->file);
Michael Natterer's avatar
Michael Natterer committed
691

692
  if (info->tattoo_state > 0)
693
    gimp_image_set_tattoo_state (image, info->tattoo_state);
694

695
696
697
  if (n_broken_layers > 0 || n_broken_channels > 0)
    goto error;

698
  gimp_image_undo_enable (image);
699

700
  return image;
Michael Natterer's avatar
Michael Natterer committed
701
702
703
704
705

 error:
  if (num_successful_elements == 0)
    goto hard_error;

706
707
708
709
710
711
712
713
  g_clear_pointer (&group_layers, g_list_free);

  if (broken_paths)
    {
      g_list_free_full (broken_paths, (GDestroyNotify) g_list_free);
      broken_paths = NULL;
    }

714
  gimp_message_literal (gimp, G_OBJECT (info->progress), GIMP_MESSAGE_WARNING,
715
716
                        _("This XCF file is corrupt!  I have loaded as much "
                          "of it as I can, but it is incomplete."));
Michael Natterer's avatar
Michael Natterer committed
717

718
719
  xcf_load_add_masks (image);

720
  gimp_image_undo_enable (image);
721

722
  return image;
Michael Natterer's avatar
Michael Natterer committed
723

724
 hard_error:
725
726
727
728
729
730
731
732
  g_clear_pointer (&group_layers, g_list_free);

  if (broken_paths)
    {
      g_list_free_full (broken_paths, (GDestroyNotify) g_list_free);
      broken_paths = NULL;
    }

733
  g_set_error_literal (error, G_FILE_ERROR, G_FILE_ERROR_FAILED,
734
735
                       _("This XCF file is corrupt!  I could not even "
                         "salvage any partial image data from it."));
Michael Natterer's avatar
Michael Natterer committed
736

737
  g_clear_object (&image);
Michael Natterer's avatar
Michael Natterer committed
738
739
740
741

  return NULL;
}

742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
static void
xcf_load_add_masks (GimpImage *image)
{
  GList *layers;
  GList *list;

  layers = gimp_image_get_layer_list (image);

  for (list = layers; list; list = g_list_next (list))
    {
      GimpLayer     *layer = list->data;
      GimpLayerMask *mask;

      mask = g_object_get_data (G_OBJECT (layer), "gimp-layer-mask");

      if (mask)
        {
759
760
761
762
763
764
765
766
767
768
769
          gboolean apply_mask;
          gboolean edit_mask;
          gboolean show_mask;

          apply_mask = GPOINTER_TO_INT (g_object_get_data (G_OBJECT (layer),
                                                           "gimp-layer-mask-apply"));
          edit_mask = GPOINTER_TO_INT (g_object_get_data (G_OBJECT (layer),
                                                          "gimp-layer-mask-edit"));
          show_mask = GPOINTER_TO_INT (g_object_get_data (G_OBJECT (layer),
                                                          "gimp-layer-mask-show"));

770
771
          gimp_layer_add_mask (layer, mask, FALSE, NULL);

772
773
774
775
776
777
778
779
          gimp_layer_set_apply_mask (layer, apply_mask, FALSE);
          gimp_layer_set_edit_mask  (layer, edit_mask);
          gimp_layer_set_show_mask  (layer, show_mask, FALSE);

          g_object_set_data (G_OBJECT (layer), "gimp-layer-mask",       NULL);
          g_object_set_data (G_OBJECT (layer), "gimp-layer-mask-apply", NULL);
          g_object_set_data (G_OBJECT (layer), "gimp-layer-mask-edit",  NULL);
          g_object_set_data (G_OBJECT (layer), "gimp-layer-mask-show",  NULL);
780
781
782
783
784
785
        }
    }

  g_list_free (layers);
}

Michael Natterer's avatar
Michael Natterer committed
786
787
static gboolean
xcf_load_image_props (XcfInfo   *info,
788
                      GimpImage *image)
Michael Natterer's avatar
Michael Natterer committed
789
790
791
792
793
794
{
  PropType prop_type;
  guint32  prop_size;

  while (TRUE)
    {
795
      if (! xcf_load_prop (info, &prop_type, &prop_size))
796
        return FALSE;
Michael Natterer's avatar
Michael Natterer committed
797
798

      switch (prop_type)
799
800
801
802
803
        {
        case PROP_END:
          return TRUE;

        case PROP_COLORMAP:
804
805
806
          {
            guint32 n_colors;
            guchar  cmap[GIMP_IMAGE_COLORMAP_SIZE];
807

808
            xcf_read_int32 (info, &n_colors, 1);
809

810
811
812
813
814
            if (n_colors > (GIMP_IMAGE_COLORMAP_SIZE / 3))
              {
                gimp_message (info->gimp, G_OBJECT (info->progress),
                              GIMP_MESSAGE_ERROR,
                              "Maximum colormap size (%d) exceeded",
815
                              GIMP_IMAGE_COLORMAP_SIZE);
816
817
818
                return FALSE;
              }

819
820
821
            if (info->file_version == 0)
              {
                gint i;
822

823
                gimp_message_literal (info->gimp, G_OBJECT (info->progress),
824
825
826
827
                                      GIMP_MESSAGE_WARNING,
                                      _("XCF warning: version 0 of XCF file format\n"
                                        "did not save indexed colormaps correctly.\n"
                                        "Substituting grayscale map."));
828
829
830
831
832
833
834
835
836
837
838
839
840

                if (! xcf_seek_pos (info, info->cp + n_colors, NULL))
                  return FALSE;

                for (i = 0; i < n_colors; i++)
                  {
                    cmap[i * 3 + 0] = i;
                    cmap[i * 3 + 1] = i;
                    cmap[i * 3 + 2] = i;
                  }
              }
            else
              {
841
                xcf_read_int8 (info, cmap, n_colors * 3);
842
843
              }

844
845
846
            /* only set color map if image is indexed, this is just
             * sanity checking to make sure gimp doesn't end up with
             * an image state that is impossible.
847
             */
848
            if (gimp_image_get_base_type (image) == GIMP_INDEXED)
849
              gimp_image_set_colormap (image, cmap, n_colors, FALSE);
850
851

            GIMP_LOG (XCF, "prop colormap n_colors=%d", n_colors);
852
          }
853
854
855
856
857
858
          break;

        case PROP_COMPRESSION:
          {
            guint8 compression;

859
            xcf_read_int8 (info, (guint8 *) &compression, 1);
860
861
862
863
864
865

            if ((compression != COMPRESS_NONE) &&
                (compression != COMPRESS_RLE) &&
                (compression != COMPRESS_ZLIB) &&
                (compression != COMPRESS_FRACTAL))
              {
866
867
                gimp_message (info->gimp, G_OBJECT (info->progress),
                              GIMP_MESSAGE_ERROR,
868
                              "Unknown compression type: %d",
Jehan's avatar
Jehan committed
869
                              (gint) compression);
870
871
872
873
                return FALSE;
              }

            info->compression = compression;
874

875
876
877
            gimp_image_set_xcf_compression (image,
                                            compression >= COMPRESS_ZLIB);

878
            GIMP_LOG (XCF, "prop compression=%d", compression);
879
880
881
882
883
          }
          break;

        case PROP_GUIDES:
          {
884
885
886
887
            GimpImagePrivate *private = GIMP_IMAGE_GET_PRIVATE (image);
            gint32            position;
            gint8             orientation;
            gint              i, nguides;
888
889
890
891

            nguides = prop_size / (4 + 1);
            for (i = 0; i < nguides; i++)
              {
892
893
                xcf_read_int32 (info, (guint32 *) &position,    1);
                xcf_read_int8  (info, (guint8 *)  &orientation, 1);
894
895
896
897

                /*  skip -1 guides from old XCFs  */
                if (position < 0)
                  continue;
Michael Natterer's avatar
Michael Natterer committed
898

899
900
901
                GIMP_LOG (XCF, "prop guide orientation=%d position=%d",
                          orientation, position);

902
903
904
                switch (orientation)
                  {
                  case XCF_ORIENTATION_HORIZONTAL:
905
                    gimp_image_add_hguide (image, position, FALSE);
906
907
908
                    break;

                  case XCF_ORIENTATION_VERTICAL:
909
                    gimp_image_add_vguide (image, position, FALSE);
910
911
912
                    break;

                  default:
913
                    gimp_message_literal (info->gimp, G_OBJECT (info->progress),
914
915
                                          GIMP_MESSAGE_WARNING,
                                          "Guide orientation out of range in XCF file");
916
917
918
919
920
                    continue;
                  }
              }

            /*  this is silly as the order of guides doesn't really matter,
921
             *  but it restores the list to its original order, which
922
923
             *  cannot be wrong  --Mitch
             */
924
            private->guides = g_list_reverse (private->guides);
925
926
927
          }
          break;

928
        case PROP_SAMPLE_POINTS:
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
          {
            gint n_sample_points, i;

            n_sample_points = prop_size / (5 * 4);
            for (i = 0; i < n_sample_points; i++)
              {
                GimpSamplePoint   *sample_point;
                gint32             x, y;
                GimpColorPickMode  pick_mode;
                guint32            padding[2] = { 0, };

                xcf_read_int32 (info, (guint32 *) &x,         1);
                xcf_read_int32 (info, (guint32 *) &y,         1);
                xcf_read_int32 (info, (guint32 *) &pick_mode, 1);
                xcf_read_int32 (info, (guint32 *) padding,    2);

                GIMP_LOG (XCF, "prop sample point x=%d y=%d mode=%d",
                          x, y, pick_mode);

948
949
950
                if (pick_mode > GIMP_COLOR_PICK_MODE_LAST)
                  pick_mode = GIMP_COLOR_PICK_MODE_PIXEL;

951
952
953
954
955
956
957
958
959
                sample_point = gimp_image_add_sample_point_at_pos (image,
                                                                   x, y, FALSE);
                gimp_image_set_sample_point_pick_mode (image, sample_point,
                                                       pick_mode, FALSE);
              }
          }
          break;

        case PROP_OLD_SAMPLE_POINTS:
960
961
962
963
          {
            gint32 x, y;
            gint   i, n_sample_points;

964
965
966
967
968
969
970
971
972
973
974
            /* if there are already sample points, we loaded the new
             * prop before
             */
            if (gimp_image_get_sample_points (image))
              {
                if (! xcf_skip_unknown_prop (info, prop_size))
                  return FALSE;

                break;
              }

975
976
977
            n_sample_points = prop_size / (4 + 4);
            for (i = 0; i < n_sample_points; i++)
              {
978
979
                xcf_read_int32 (info, (guint32 *) &x, 1);
                xcf_read_int32 (info, (guint32 *) &y, 1);
980

981
                GIMP_LOG (XCF, "prop old sample point x=%d y=%d", x, y);
982

983
984
985
986
987
                gimp_image_add_sample_point_at_pos (image, x, y, FALSE);
              }
          }
          break;

988
989
990
991
        case PROP_RESOLUTION:
          {
            gfloat xres, yres;

992
993
            xcf_read_float (info, &xres, 1);
            xcf_read_float (info, &yres, 1);
994

995
996
            GIMP_LOG (XCF, "prop resolution x=%f y=%f", xres, yres);

997
998
999
            if (xres < GIMP_MIN_RESOLUTION || xres > GIMP_MAX_RESOLUTION ||
                yres < GIMP_MIN_RESOLUTION || yres > GIMP_MAX_RESOLUTION)
              {
1000
                GimpTemplate *template = image->gimp->config->default_image;
For faster browsing, not all history is shown. View entire blame