gimpimage-merge.c 22.2 KB
Newer Older
1
/* GIMP - The GNU Image Manipulation Program
2 3
 * 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 3 of the License, or
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/>.
16
 */
17

18 19
#include "config.h"

20
#include <cairo.h>
21
#include <gegl.h>
22
#include <gdk-pixbuf/gdk-pixbuf.h>
Sven Neumann's avatar
Sven Neumann committed
23

24
#include "libgimpcolor/gimpcolor.h"
25
#include "libgimpmath/gimpmath.h"
26
#include "libgimpbase/gimpbase.h"
27

28 29
#include "core-types.h"

30
#include "gegl/gimp-babl-compat.h"
31
#include "gegl/gimp-gegl-apply-operation.h"
32
#include "gegl/gimp-gegl-nodes.h"
33 34
#include "gegl/gimp-gegl-utils.h"

35 36
#include "vectors/gimpvectors.h"

37
#include "gimp.h"
38
#include "gimpcontext.h"
39
#include "gimperror.h"
40
#include "gimpgrouplayer.h"
41
#include "gimpimage.h"
42
#include "gimpimage-merge.h"
43
#include "gimpimage-undo.h"
44
#include "gimpitemstack.h"
45
#include "gimplayer-floating-selection.h"
46
#include "gimplayer-new.h"
47
#include "gimplayermask.h"
48
#include "gimpmarshal.h"
49
#include "gimpparasitelist.h"
50
#include "gimppickable.h"
51
#include "gimpprojectable.h"
52
#include "gimpundostack.h"
53

54
#include "gimp-intl.h"
55

56

57
static GimpLayer * gimp_image_merge_layers (GimpImage     *image,
58
                                            GimpContainer *container,
59 60
                                            GSList        *merge_list,
                                            GimpContext   *context,
61
                                            GimpMergeType  merge_type);
62

63

64
/*  public functions  */
65

66
GimpLayer *
67
gimp_image_merge_visible_layers (GimpImage     *image,
68
                                 GimpContext   *context,
69
                                 GimpMergeType  merge_type,
70
                                 gboolean       merge_active_group,
71
                                 gboolean       discard_invisible)
Sven Neumann's avatar
Sven Neumann committed
72
{
73 74 75 76
  GimpContainer *container;
  GList         *list;
  GSList        *merge_list     = NULL;
  GSList        *invisible_list = NULL;
77

78
  g_return_val_if_fail (GIMP_IS_IMAGE (image), NULL);
79
  g_return_val_if_fail (GIMP_IS_CONTEXT (context), NULL);
80

81
  if (merge_active_group)
82
    {
83 84 85 86 87 88 89 90
      GimpLayer *active_layer = gimp_image_get_active_layer (image);

      /*  if the active layer is the floating selection, get the
       *  underlying drawable, but only if it is a layer
       */
      if (active_layer && gimp_layer_is_floating_sel (active_layer))
        {
          GimpDrawable *fs_drawable;
91

92
          fs_drawable = gimp_layer_get_floating_sel_drawable (active_layer);
93

94 95 96
          if (GIMP_IS_LAYER (fs_drawable))
            active_layer = GIMP_LAYER (fs_drawable);
        }
97

98 99 100 101 102
      if (active_layer)
        container = gimp_item_get_container (GIMP_ITEM (active_layer));
      else
        container = gimp_image_get_layers (image);
    }
103
  else
104 105 106
    {
      container = gimp_image_get_layers (image);
    }
107 108

  for (list = gimp_item_stack_get_item_iter (GIMP_ITEM_STACK (container));
109 110
       list;
       list = g_list_next (list))
111
    {
112 113 114 115
      GimpLayer *layer = list->data;

      if (gimp_layer_is_floating_sel (layer))
        continue;
116

117
      if (gimp_item_get_visible (GIMP_ITEM (layer)))
118 119 120 121 122 123 124
        {
          merge_list = g_slist_append (merge_list, layer);
        }
      else if (discard_invisible)
        {
          invisible_list = g_slist_append (invisible_list, layer);
        }
125
    }
126

127
  if (merge_list)
128
    {
129
      GimpLayer *layer;
130

131
      gimp_set_busy (image->gimp);
132

133 134 135 136 137 138 139
      gimp_image_undo_group_start (image,
                                   GIMP_UNDO_GROUP_IMAGE_LAYERS_MERGE,
                                   C_("undo-type", "Merge Visible Layers"));

      /* if there's a floating selection, anchor it */
      if (gimp_image_get_floating_selection (image))
        floating_sel_anchor (gimp_image_get_floating_selection (image));
140

141
      layer = gimp_image_merge_layers (image,
142
                                       container,
143
                                       merge_list, context, merge_type);
144
      g_slist_free (merge_list);
145

146 147 148 149 150
      if (invisible_list)
        {
          GSList *list;

          for (list = invisible_list; list; list = g_slist_next (list))
151
            gimp_image_remove_layer (image, list->data, TRUE, NULL);
152 153 154 155

          g_slist_free (invisible_list);
        }

156
      gimp_image_undo_group_end (image);
157

158
      gimp_unset_busy (image->gimp);
159

160
      return layer;
161
    }
162

163
  return gimp_image_get_active_layer (image);
164
}
165

166
GimpLayer *
167 168 169
gimp_image_flatten (GimpImage    *image,
                    GimpContext  *context,
                    GError      **error)
170
{
171 172 173
  GList     *list;
  GSList    *merge_list = NULL;
  GimpLayer *layer;
174

175
  g_return_val_if_fail (GIMP_IS_IMAGE (image), NULL);
176
  g_return_val_if_fail (GIMP_IS_CONTEXT (context), NULL);
177
  g_return_val_if_fail (error == NULL || *error == NULL, NULL);
178

179
  for (list = gimp_image_get_layer_iter (image);
180 181 182
       list;
       list = g_list_next (list))
    {
Sven Neumann's avatar
Sven Neumann committed
183
      layer = list->data;
184

185 186 187
      if (gimp_layer_is_floating_sel (layer))
        continue;

188
      if (gimp_item_get_visible (GIMP_ITEM (layer)))
189
        merge_list = g_slist_append (merge_list, layer);
190
    }
191

192 193 194
  if (merge_list)
    {
      gimp_set_busy (image->gimp);
195

196 197 198
      gimp_image_undo_group_start (image,
                                   GIMP_UNDO_GROUP_IMAGE_LAYERS_MERGE,
                                   C_("undo-type", "Flatten Image"));
199

200 201 202 203 204 205 206 207 208
      /* if there's a floating selection, anchor it */
      if (gimp_image_get_floating_selection (image))
        floating_sel_anchor (gimp_image_get_floating_selection (image));

      layer = gimp_image_merge_layers (image,
                                       gimp_image_get_layers (image),
                                       merge_list, context,
                                       GIMP_FLATTEN_IMAGE);
      g_slist_free (merge_list);
209

210
      gimp_image_alpha_changed (image);
211

212 213 214
      gimp_image_undo_group_end (image);

      gimp_unset_busy (image->gimp);
215 216

      return layer;
217 218
    }

219 220 221
  g_set_error_literal (error, GIMP_ERROR, GIMP_FAILED,
                       _("Cannot flatten an image without any visible layer."));
  return NULL;
222 223
}

224
GimpLayer *
225 226 227 228 229
gimp_image_merge_down (GimpImage      *image,
                       GimpLayer      *current_layer,
                       GimpContext    *context,
                       GimpMergeType   merge_type,
                       GError        **error)
230
{
231 232
  GimpLayer *layer;
  GList     *list;
233 234
  GList     *layer_list = NULL;
  GSList    *merge_list = NULL;
235

236
  g_return_val_if_fail (GIMP_IS_IMAGE (image), NULL);
237 238
  g_return_val_if_fail (GIMP_IS_LAYER (current_layer), NULL);
  g_return_val_if_fail (gimp_item_is_attached (GIMP_ITEM (current_layer)), NULL);
239
  g_return_val_if_fail (GIMP_IS_CONTEXT (context), NULL);
240 241
  g_return_val_if_fail (error == NULL || *error == NULL, NULL);

242 243 244 245 246 247 248
  if (gimp_layer_is_floating_sel (current_layer))
    {
      g_set_error_literal (error, GIMP_ERROR, GIMP_FAILED,
                           _("Cannot merge down a floating selection."));
      return NULL;
    }

249 250 251 252 253 254 255
  if (! gimp_item_get_visible (GIMP_ITEM (current_layer)))
    {
      g_set_error_literal (error, GIMP_ERROR, GIMP_FAILED,
                           _("Cannot merge down an invisible layer."));
      return NULL;
    }

256 257
  for (list = gimp_item_get_container_iter (GIMP_ITEM (current_layer));
       list;
258
       list = g_list_next (list))
259
    {
260
      layer = list->data;
261

262
      if (layer == current_layer)
263
        break;
264
    }
265

266 267
  for (layer_list = g_list_next (list);
       layer_list;
268
       layer_list = g_list_next (layer_list))
269
    {
270
      layer = layer_list->data;
271

272
      if (gimp_item_get_visible (GIMP_ITEM (layer)))
273
        {
274 275
          if (gimp_viewable_get_children (GIMP_VIEWABLE (layer)))
            {
276
              g_set_error_literal (error, GIMP_ERROR, GIMP_FAILED,
277
                                   _("Cannot merge down to a layer group."));
278 279 280
              return NULL;
            }

281
          if (gimp_item_is_content_locked (GIMP_ITEM (layer)))
282
            {
283
              g_set_error_literal (error, GIMP_ERROR, GIMP_FAILED,
284 285 286
                                   _("The layer to merge down to is locked."));
              return NULL;
            }
287 288 289 290

          merge_list = g_slist_append (NULL, layer);
          break;
        }
291 292
    }

293 294
  if (! merge_list)
    {
295
      g_set_error_literal (error, GIMP_ERROR, GIMP_FAILED,
296 297 298 299
                           _("There is no visible layer to merge down to."));
      return NULL;
    }

300
  merge_list = g_slist_prepend (merge_list, current_layer);
301

302
  gimp_set_busy (image->gimp);
303

304 305 306 307
  gimp_image_undo_group_start (image,
                               GIMP_UNDO_GROUP_IMAGE_LAYERS_MERGE,
                               C_("undo-type", "Merge Down"));

308 309 310
  layer = gimp_image_merge_layers (image,
                                   gimp_item_get_container (GIMP_ITEM (current_layer)),
                                   merge_list, context, merge_type);
311
  g_slist_free (merge_list);
312

313 314
  gimp_image_undo_group_end (image);

315
  gimp_unset_busy (image->gimp);
316

317
  return layer;
318 319
}

320 321 322 323 324 325 326 327 328 329 330 331 332 333
GimpLayer *
gimp_image_merge_group_layer (GimpImage      *image,
                              GimpGroupLayer *group)
{
  GimpLayer *parent;
  GimpLayer *layer;
  gint       index;

  g_return_val_if_fail (GIMP_IS_IMAGE (image), NULL);
  g_return_val_if_fail (GIMP_IS_GROUP_LAYER (group), NULL);
  g_return_val_if_fail (gimp_item_is_attached (GIMP_ITEM (group)), NULL);
  g_return_val_if_fail (gimp_item_get_image (GIMP_ITEM (group)) == image, NULL);

  gimp_image_undo_group_start (image, GIMP_UNDO_GROUP_IMAGE_LAYERS_MERGE,
334
                               C_("undo-type", "Merge Layer Group"));
335

336
  parent = gimp_layer_get_parent (GIMP_LAYER (group));
337 338
  index  = gimp_item_get_index (GIMP_ITEM (group));

339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361
  /* if this is a pass-through group, change its mode to NORMAL *before*
   * duplicating it, since PASS_THROUGH mode is invalid for regular layers.
   * see bug #793714.
   */
  if (gimp_layer_get_mode (GIMP_LAYER (group)) == GIMP_LAYER_MODE_PASS_THROUGH)
    {
      GimpLayerColorSpace    blend_space;
      GimpLayerColorSpace    composite_space;
      GimpLayerCompositeMode composite_mode;

      /* keep the group's current blend space, composite space, and composite
       * mode.
       */
      blend_space     = gimp_layer_get_blend_space     (GIMP_LAYER (group));
      composite_space = gimp_layer_get_composite_space (GIMP_LAYER (group));
      composite_mode  = gimp_layer_get_composite_mode  (GIMP_LAYER (group));

      gimp_layer_set_mode            (GIMP_LAYER (group), GIMP_LAYER_MODE_NORMAL, TRUE);
      gimp_layer_set_blend_space     (GIMP_LAYER (group), blend_space,            TRUE);
      gimp_layer_set_composite_space (GIMP_LAYER (group), composite_space,        TRUE);
      gimp_layer_set_composite_mode  (GIMP_LAYER (group), composite_mode,         TRUE);
    }

362 363 364 365 366 367 368 369 370 371 372 373 374 375
  layer = GIMP_LAYER (gimp_item_duplicate (GIMP_ITEM (group),
                                           GIMP_TYPE_LAYER));

  gimp_object_set_name (GIMP_OBJECT (layer), gimp_object_get_name (group));

  gimp_image_remove_layer (image, GIMP_LAYER (group), TRUE, NULL);
  gimp_image_add_layer (image, layer, parent, index, TRUE);

  gimp_image_undo_group_end (image);

  return layer;
}


376 377 378
/* merging vectors */

GimpVectors *
379 380
gimp_image_merge_visible_vectors (GimpImage  *image,
                                  GError    **error)
381
{
382 383 384
  GList       *list;
  GList       *merge_list = NULL;
  GimpVectors *vectors;
385 386

  g_return_val_if_fail (GIMP_IS_IMAGE (image), NULL);
387
  g_return_val_if_fail (error == NULL || *error == NULL, NULL);
388

389
  for (list = gimp_image_get_vectors_iter (image);
390 391 392 393 394 395
       list;
       list = g_list_next (list))
    {
      vectors = list->data;

      if (gimp_item_get_visible (GIMP_ITEM (vectors)))
396
        merge_list = g_list_prepend (merge_list, vectors);
397 398
    }

399 400
  merge_list = g_list_reverse (merge_list);

401 402
  if (merge_list && merge_list->next)
    {
403 404 405 406
      GimpVectors *target_vectors;
      gchar       *name;
      gint         pos;

407 408 409
      gimp_set_busy (image->gimp);

      gimp_image_undo_group_start (image, GIMP_UNDO_GROUP_IMAGE_VECTORS_MERGE,
410
                                   C_("undo-type", "Merge Visible Paths"));
411

412
      vectors = GIMP_VECTORS (merge_list->data);
413

414
      name = g_strdup (gimp_object_get_name (vectors));
415 416
      pos = gimp_item_get_index (GIMP_ITEM (vectors));

417 418
      target_vectors = GIMP_VECTORS (gimp_item_duplicate (GIMP_ITEM (vectors),
                                                          GIMP_TYPE_VECTORS));
419
      gimp_image_remove_vectors (image, vectors, TRUE, NULL);
420

421 422 423
      for (list = g_list_next (merge_list);
           list;
           list = g_list_next (list))
424
        {
425 426
          vectors = list->data;

427
          gimp_vectors_add_strokes (vectors, target_vectors);
428
          gimp_image_remove_vectors (image, vectors, TRUE, NULL);
429 430 431 432
        }

      gimp_object_take_name (GIMP_OBJECT (target_vectors), name);

433
      g_list_free (merge_list);
434

435 436
      /* FIXME tree */
      gimp_image_add_vectors (image, target_vectors, NULL, pos, TRUE);
437 438 439 440 441 442 443 444
      gimp_unset_busy (image->gimp);

      gimp_image_undo_group_end (image);

      return target_vectors;
    }
  else
    {
445
      g_set_error_literal (error, GIMP_ERROR, GIMP_FAILED,
446 447
                           _("Not enough visible paths for a merge. "
                             "There must be at least two."));
448 449 450 451 452 453 454 455
      return NULL;
    }
}


/*  private functions  */

static GimpLayer *
456
gimp_image_merge_layers (GimpImage     *image,
457
                         GimpContainer *container,
458
                         GSList        *merge_list,
459
                         GimpContext   *context,
460
                         GimpMergeType  merge_type)
461
{
462 463 464
  GimpLayer        *parent;
  gint              x1, y1;
  gint              x2, y2;
Massimo Valentini's avatar
Massimo Valentini committed
465
  GSList           *layers;
466
  GimpLayer        *layer;
467
  GimpLayer        *top_layer;
468
  GimpLayer        *bottom_layer;
469
  GimpLayer        *merge_layer;
470
  gint              position;
471 472 473 474 475 476 477
  GeglNode         *node;
  GeglNode         *source_node;
  GeglNode         *flatten_node;
  GeglNode         *offset_node;
  GeglNode         *last_node;
  GeglNode         *last_node_source;
  GimpParasiteList *parasites;
478

479
  g_return_val_if_fail (GIMP_IS_IMAGE (image), NULL);
480
  g_return_val_if_fail (GIMP_IS_CONTEXT (context), NULL);
481

482 483 484
  top_layer = merge_list->data;
  parent    = gimp_layer_get_parent (top_layer);

485 486 487 488 489
  /*  Make sure the image's graph is constructed, so that top-level layers have
   *  a parent node.
   */
  (void) gimp_projectable_get_graph (GIMP_PROJECTABLE (image));

490 491 492 493 494 495 496 497
  /*  Make sure the parent's graph is constructed, so that the top layer has a
   *  parent node, even if it is the child of a group layer (in particular, of
   *  an invisible group layer, whose graph may not have been constructed as a
   *  result of the above call.  see issue #2095.)
   */
  if (parent)
    (void) gimp_filter_get_node (GIMP_FILTER (parent));

498 499 500 501 502
  /*  Build our graph inside the top-layer's parent node  */
  source_node = gimp_filter_get_node (GIMP_FILTER (top_layer));
  node = gegl_node_get_parent (source_node);

  g_return_val_if_fail (node, NULL);
503

504
  /*  Get the layer extents  */
505 506 507
  x1 = y1 = 0;
  x2 = y2 = 0;
  for (layers = merge_list; layers; layers = g_slist_next (layers))
508
    {
509 510 511
      gint off_x, off_y;

      layer = layers->data;
512

513
      gimp_item_get_offset (GIMP_ITEM (layer), &off_x, &off_y);
514

515
      switch (merge_type)
516 517 518
        {
        case GIMP_EXPAND_AS_NECESSARY:
        case GIMP_CLIP_TO_IMAGE:
519
          if (layers == merge_list)
520 521 522
            {
              x1 = off_x;
              y1 = off_y;
523 524
              x2 = off_x + gimp_item_get_width  (GIMP_ITEM (layer));
              y2 = off_y + gimp_item_get_height (GIMP_ITEM (layer));
525 526 527 528 529 530 531
            }
          else
            {
              if (off_x < x1)
                x1 = off_x;
              if (off_y < y1)
                y1 = off_y;
532 533 534 535
              if ((off_x + gimp_item_get_width (GIMP_ITEM (layer))) > x2)
                x2 = (off_x + gimp_item_get_width (GIMP_ITEM (layer)));
              if ((off_y + gimp_item_get_height (GIMP_ITEM (layer))) > y2)
                y2 = (off_y + gimp_item_get_height (GIMP_ITEM (layer)));
536
            }
537

538 539
          if (merge_type == GIMP_CLIP_TO_IMAGE)
            {
540 541 542 543
              x1 = CLAMP (x1, 0, gimp_image_get_width  (image));
              y1 = CLAMP (y1, 0, gimp_image_get_height (image));
              x2 = CLAMP (x2, 0, gimp_image_get_width  (image));
              y2 = CLAMP (y2, 0, gimp_image_get_height (image));
544 545 546 547
            }
          break;

        case GIMP_CLIP_TO_BOTTOM_LAYER:
548
          if (layers->next == NULL)
549 550 551
            {
              x1 = off_x;
              y1 = off_y;
552 553
              x2 = off_x + gimp_item_get_width  (GIMP_ITEM (layer));
              y2 = off_y + gimp_item_get_height (GIMP_ITEM (layer));
554 555 556 557
            }
          break;

        case GIMP_FLATTEN_IMAGE:
558
          if (layers->next == NULL)
559 560 561
            {
              x1 = 0;
              y1 = 0;
562 563
              x2 = gimp_image_get_width  (image);
              y2 = gimp_image_get_height (image);
564 565 566
            }
          break;
        }
567
    }
568

569
  if ((x2 - x1) == 0 || (y2 - y1) == 0)
570
    return NULL;
571

572
  bottom_layer = layer;
573

574
  flatten_node = NULL;
575

576
  if (merge_type == GIMP_FLATTEN_IMAGE ||
577 578
      (gimp_drawable_is_indexed (GIMP_DRAWABLE (layer)) &&
       ! gimp_drawable_has_alpha (GIMP_DRAWABLE (layer))))
579
    {
580
      GimpRGB bg;
581

582
      merge_layer = gimp_layer_new (image, (x2 - x1), (y2 - y1),
583
                                    gimp_image_get_layer_format (image, FALSE),
584
                                    gimp_object_get_name (bottom_layer),
585
                                    GIMP_OPACITY_OPAQUE,
586
                                    gimp_image_get_default_new_layer_mode (image));
587

588
      if (! merge_layer)
589
        {
590 591
          g_warning ("%s: could not allocate merge layer", G_STRFUNC);

592 593
          return NULL;
        }
594

595
      /*  get the background for compositing  */
596
      gimp_context_get_background (context, &bg);
597 598
      gimp_pickable_srgb_to_image_color (GIMP_PICKABLE (layer),
                                         &bg, &bg);
599

600 601 602 603
      flatten_node = gimp_gegl_create_flatten_node
        (&bg,
         gimp_drawable_get_space (GIMP_DRAWABLE (layer)),
         gimp_layer_get_real_composite_space (bottom_layer));
604

605
      position = 0;
606 607 608
    }
  else
    {
609
      /*  The final merged layer inherits the name of the bottom most layer
610 611
       *  and the resulting layer has an alpha channel whether or not the
       *  original did. Opacity is set to 100% and the MODE is set to normal.
612 613
       */

614
      merge_layer =
615
        gimp_layer_new (image, (x2 - x1), (y2 - y1),
616 617
                        gimp_drawable_get_format_with_alpha (GIMP_DRAWABLE (bottom_layer)),
                        gimp_object_get_name (bottom_layer),
618
                        GIMP_OPACITY_OPAQUE,
619
                        gimp_image_get_default_new_layer_mode (image));
620

621
      if (! merge_layer)
622 623
        {
          g_warning ("%s: could not allocate merge layer", G_STRFUNC);
624

625 626
          return NULL;
        }
627

628 629 630
      /*  Find the index in the layer list of the bottom layer--we need this
       *  in order to add the final, merged layer to the layer list correctly
       */
631
      position =
632
        gimp_container_get_n_children (container) -
633
        gimp_container_get_child_index (container, GIMP_OBJECT (bottom_layer));
634
    }
635

636
  gimp_item_set_offset (GIMP_ITEM (merge_layer), x1, y1);
637

638 639 640 641 642
  offset_node = gegl_node_new_child (node,
                                     "operation", "gegl:translate",
                                     "x",         (gdouble) -x1,
                                     "y",         (gdouble) -y1,
                                     NULL);
643

644 645 646 647
  if (flatten_node)
    {
      gegl_node_add_child (node, flatten_node);
      g_object_unref (flatten_node);
648

649 650 651 652 653 654
      gegl_node_link_many (source_node, flatten_node, offset_node, NULL);
    }
  else
    {
      gegl_node_link_many (source_node, offset_node, NULL);
    }
655

656 657 658
  /*  Disconnect the bottom-layer node's input  */
  last_node        = gimp_filter_get_node (GIMP_FILTER (bottom_layer));
  last_node_source = gegl_node_get_producer (last_node, "input", NULL);
659

660
  gegl_node_disconnect (last_node, "input");
661

662 663 664 665
  /*  Render the graph into the merge layer  */
  gegl_node_blit_buffer (offset_node,
                         gimp_drawable_get_buffer (GIMP_DRAWABLE (merge_layer)),
                         NULL, 0, GEGL_ABYSS_NONE);
666

667 668 669
  /*  Reconnect the bottom-layer node's input  */
  if (last_node_source)
    gegl_node_link (last_node_source, last_node);
670

671 672
  /*  Clean up the graph  */
  gegl_node_remove_child (node, offset_node);
673

674 675
  if (flatten_node)
    gegl_node_remove_child (node, flatten_node);
676

677 678 679
  /* Copy the tattoo and parasites of the bottom layer to the new layer */
  gimp_item_set_tattoo (GIMP_ITEM (merge_layer),
                        gimp_item_get_tattoo (GIMP_ITEM (bottom_layer)));
680

681 682 683 684
  parasites = gimp_item_get_parasites (GIMP_ITEM (bottom_layer));
  parasites = gimp_parasite_list_copy (parasites);
  gimp_item_set_parasites (GIMP_ITEM (merge_layer), parasites);
  g_object_unref (parasites);
685

686 687 688
  /*  Remove the merged layers from the image  */
  for (layers = merge_list; layers; layers = g_slist_next (layers))
    gimp_image_remove_layer (image, layers->data, TRUE, NULL);
689

690 691
  gimp_item_set_visible (GIMP_ITEM (merge_layer), TRUE, FALSE);

692
  /*  if the type is flatten, remove all the remaining layers  */
693
  if (merge_type == GIMP_FLATTEN_IMAGE)
694
    {
695 696
      GList *list = gimp_image_get_layer_iter (image);

697
      while (list)
698
        {
Sven Neumann's avatar
Sven Neumann committed
699
          layer = list->data;
700

701
          list = g_list_next (list);
702
          gimp_image_remove_layer (image, layer, TRUE, NULL);
703
        }
704

705 706
      gimp_image_add_layer (image, merge_layer, parent,
                            position, TRUE);
707 708 709
    }
  else
    {
710
      /*  Add the layer to the image  */
711 712 713 714
      gimp_image_add_layer (image, merge_layer, parent,
                            gimp_container_get_n_children (container) -
                            position + 1,
                            TRUE);
715 716
    }

717
  gimp_drawable_update (GIMP_DRAWABLE (merge_layer), 0, 0, -1, -1);
718 719

  return merge_layer;
720
}