gimplayer.c 66.2 KB
Newer Older
1
/* GIMP - The GNU Image Manipulation Program
Elliot Lee's avatar
Elliot Lee committed
2 3
 * Copyright (C) 1995 Spencer Kimball and Peter Mattis
 *
4
 * This program is free software: you can redistribute it and/or modify
Elliot Lee's avatar
Elliot Lee 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
Elliot Lee's avatar
Elliot Lee 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 <http://www.gnu.org/licenses/>.
Elliot Lee's avatar
Elliot Lee committed
16
 */
17

18 19
#include "config.h"

20
#include <stdlib.h>
21
#include <string.h>
22

23
#include <cairo.h>
24
#include <gegl.h>
25

26
#include "libgimpbase/gimpbase.h"
27
#include "libgimpcolor/gimpcolor.h"
28 29
#include "libgimpmath/gimpmath.h"

Michael Natterer's avatar
Michael Natterer committed
30
#include "core-types.h"
31

Michael Natterer's avatar
Michael Natterer committed
32 33 34 35 36
#include "base/boundary.h"
#include "base/pixel-region.h"
#include "base/tile-manager.h"
#include "base/tile.h"

37 38
#include "paint-funcs/paint-funcs.h"

39
#include "gegl/gimp-gegl-nodes.h"
40 41
#include "gegl/gimp-gegl-utils.h"

42
#include "gimp-apply-operation.h"
43
#include "gimpchannel-select.h"
44
#include "gimpcontext.h"
45
#include "gimpcontainer.h"
46
#include "gimpdrawable-convert.h"
47
#include "gimpdrawable-operation.h"
48
#include "gimperror.h"
49
#include "gimpimage-undo-push.h"
50 51
#include "gimpimage-undo.h"
#include "gimpimage.h"
52
#include "gimplayer-floating-sel.h"
53
#include "gimplayer.h"
54
#include "gimplayer-project.h"
55
#include "gimplayermask.h"
56
#include "gimpmarshal.h"
57
#include "gimppickable.h"
58

59
#include "gimp-intl.h"
60 61


62 63
enum
{
64 65
  OPACITY_CHANGED,
  MODE_CHANGED,
66
  LOCK_ALPHA_CHANGED,
67
  MASK_CHANGED,
68 69 70
  APPLY_MASK_CHANGED,
  EDIT_MASK_CHANGED,
  SHOW_MASK_CHANGED,
71 72 73
  LAST_SIGNAL
};

74 75 76 77 78
enum
{
  PROP_0,
  PROP_OPACITY,
  PROP_MODE,
79
  PROP_LOCK_ALPHA,
80
  PROP_MASK,
81
  PROP_FLOATING_SELECTION
82 83
};

84

85
static void   gimp_layer_pickable_iface_init (GimpPickableInterface *iface);
86

87 88 89 90 91 92 93 94
static void       gimp_layer_set_property       (GObject            *object,
                                                 guint               property_id,
                                                 const GValue       *value,
                                                 GParamSpec         *pspec);
static void       gimp_layer_get_property       (GObject            *object,
                                                 guint               property_id,
                                                 GValue             *value,
                                                 GParamSpec         *pspec);
95
static void       gimp_layer_dispose            (GObject            *object);
96
static void       gimp_layer_finalize           (GObject            *object);
97

98
static void       gimp_layer_name_changed       (GimpObject         *object);
99 100
static gint64     gimp_layer_get_memsize        (GimpObject         *object,
                                                 gint64             *gui_size);
101

102
static void       gimp_layer_invalidate_preview (GimpViewable       *viewable);
103 104
static gchar    * gimp_layer_get_description    (GimpViewable       *viewable,
                                                 gchar             **tooltip);
105

106
static void       gimp_layer_removed            (GimpItem           *item);
107
static void       gimp_layer_unset_removed      (GimpItem           *item);
108
static gboolean   gimp_layer_is_attached        (const GimpItem     *item);
109
static GimpItemTree * gimp_layer_get_tree       (GimpItem           *item);
110
static GimpItem * gimp_layer_duplicate          (GimpItem           *item,
111
                                                 GType               new_type);
112 113
static void       gimp_layer_convert            (GimpItem           *item,
                                                 GimpImage          *dest_image);
114
static gboolean   gimp_layer_rename             (GimpItem           *item,
115
                                                 const gchar        *new_name,
116 117
                                                 const gchar        *undo_desc,
                                                 GError            **error);
118 119
static void       gimp_layer_translate          (GimpItem           *item,
                                                 gint                offset_x,
120 121
                                                 gint                offset_y,
                                                 gboolean            push_undo);
122 123 124 125 126
static void       gimp_layer_scale              (GimpItem           *item,
                                                 gint                new_width,
                                                 gint                new_height,
                                                 gint                new_offset_x,
                                                 gint                new_offset_y,
127
                                                 GimpInterpolationType  interp_type,
128
                                                 GimpProgress       *progress);
129
static void       gimp_layer_resize             (GimpItem           *item,
130
                                                 GimpContext        *context,
131 132 133 134
                                                 gint                new_width,
                                                 gint                new_height,
                                                 gint                offset_x,
                                                 gint                offset_y);
135
static void       gimp_layer_flip               (GimpItem           *item,
136
                                                 GimpContext        *context,
137
                                                 GimpOrientationType flip_type,
138 139
                                                 gdouble             axis,
                                                 gboolean            clip_result);
140
static void       gimp_layer_rotate             (GimpItem           *item,
141
                                                 GimpContext        *context,
142 143 144 145
                                                 GimpRotationType    rotate_type,
                                                 gdouble             center_x,
                                                 gdouble             center_y,
                                                 gboolean            clip_result);
146
static void       gimp_layer_transform          (GimpItem           *item,
147
                                                 GimpContext        *context,
148
                                                 const GimpMatrix3  *matrix,
149 150
                                                 GimpTransformDirection direction,
                                                 GimpInterpolationType  interpolation_type,
151
                                                 gint                recursion_level,
152
                                                 GimpTransformResize clip_result,
153
                                                 GimpProgress       *progress);
154 155 156 157 158 159
static void       gimp_layer_to_selection       (GimpItem           *item,
                                                 GimpChannelOps      op,
                                                 gboolean            antialias,
                                                 gboolean            feather,
                                                 gdouble             feather_radius_x,
                                                 gdouble             feather_radius_y);
160
static GeglNode * gimp_layer_get_node           (GimpItem           *item);
161 162 163 164

static gint64  gimp_layer_estimate_memsize      (const GimpDrawable *drawable,
                                                 gint                width,
                                                 gint                height);
165 166 167
static void    gimp_layer_invalidate_boundary   (GimpDrawable       *drawable);
static void    gimp_layer_get_active_components (const GimpDrawable *drawable,
                                                 gboolean           *active);
168

169 170 171
static gint    gimp_layer_get_opacity_at        (GimpPickable       *pickable,
                                                 gint                x,
                                                 gint                y);
172

173 174 175 176 177 178 179
static void       gimp_layer_layer_mask_update  (GimpDrawable       *layer_mask,
                                                 gint                x,
                                                 gint                y,
                                                 gint                width,
                                                 gint                height,
                                                 GimpLayer          *layer);

180 181
static void       gimp_layer_sync_mode_node     (GimpLayer          *layer);

182

183 184
G_DEFINE_TYPE_WITH_CODE (GimpLayer, gimp_layer, GIMP_TYPE_DRAWABLE,
                         G_IMPLEMENT_INTERFACE (GIMP_TYPE_PICKABLE,
185
                                                gimp_layer_pickable_iface_init))
186

187
#define parent_class gimp_layer_parent_class
188

189
static guint layer_signals[LAST_SIGNAL] = { 0 };
190

191 192

static void
193
gimp_layer_class_init (GimpLayerClass *klass)
194
{
195 196 197 198 199
  GObjectClass      *object_class      = G_OBJECT_CLASS (klass);
  GimpObjectClass   *gimp_object_class = GIMP_OBJECT_CLASS (klass);
  GimpViewableClass *viewable_class    = GIMP_VIEWABLE_CLASS (klass);
  GimpItemClass     *item_class        = GIMP_ITEM_CLASS (klass);
  GimpDrawableClass *drawable_class    = GIMP_DRAWABLE_CLASS (klass);
200

201
  layer_signals[OPACITY_CHANGED] =
202
    g_signal_new ("opacity-changed",
203 204 205 206 207 208
                  G_TYPE_FROM_CLASS (klass),
                  G_SIGNAL_RUN_FIRST,
                  G_STRUCT_OFFSET (GimpLayerClass, opacity_changed),
                  NULL, NULL,
                  gimp_marshal_VOID__VOID,
                  G_TYPE_NONE, 0);
209 210

  layer_signals[MODE_CHANGED] =
211
    g_signal_new ("mode-changed",
212 213 214 215 216 217
                  G_TYPE_FROM_CLASS (klass),
                  G_SIGNAL_RUN_FIRST,
                  G_STRUCT_OFFSET (GimpLayerClass, mode_changed),
                  NULL, NULL,
                  gimp_marshal_VOID__VOID,
                  G_TYPE_NONE, 0);
218

219 220
  layer_signals[LOCK_ALPHA_CHANGED] =
    g_signal_new ("lock-alpha-changed",
221 222 223 224 225 226
                  G_TYPE_FROM_CLASS (klass),
                  G_SIGNAL_RUN_FIRST,
                  G_STRUCT_OFFSET (GimpLayerClass, lock_alpha_changed),
                  NULL, NULL,
                  gimp_marshal_VOID__VOID,
                  G_TYPE_NONE, 0);
227

228
  layer_signals[MASK_CHANGED] =
229
    g_signal_new ("mask-changed",
230 231 232 233 234 235
                  G_TYPE_FROM_CLASS (klass),
                  G_SIGNAL_RUN_FIRST,
                  G_STRUCT_OFFSET (GimpLayerClass, mask_changed),
                  NULL, NULL,
                  gimp_marshal_VOID__VOID,
                  G_TYPE_NONE, 0);
236

237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263
  layer_signals[APPLY_MASK_CHANGED] =
    g_signal_new ("apply-mask-changed",
                  G_TYPE_FROM_CLASS (klass),
                  G_SIGNAL_RUN_FIRST,
                  G_STRUCT_OFFSET (GimpLayerClass, apply_mask_changed),
                  NULL, NULL,
                  gimp_marshal_VOID__VOID,
                  G_TYPE_NONE, 0);

  layer_signals[EDIT_MASK_CHANGED] =
    g_signal_new ("edit-mask-changed",
                  G_TYPE_FROM_CLASS (klass),
                  G_SIGNAL_RUN_FIRST,
                  G_STRUCT_OFFSET (GimpLayerClass, edit_mask_changed),
                  NULL, NULL,
                  gimp_marshal_VOID__VOID,
                  G_TYPE_NONE, 0);

  layer_signals[SHOW_MASK_CHANGED] =
    g_signal_new ("show-mask-changed",
                  G_TYPE_FROM_CLASS (klass),
                  G_SIGNAL_RUN_FIRST,
                  G_STRUCT_OFFSET (GimpLayerClass, show_mask_changed),
                  NULL, NULL,
                  gimp_marshal_VOID__VOID,
                  G_TYPE_NONE, 0);

264 265
  object_class->set_property          = gimp_layer_set_property;
  object_class->get_property          = gimp_layer_get_property;
266
  object_class->dispose               = gimp_layer_dispose;
267 268
  object_class->finalize              = gimp_layer_finalize;

269
  gimp_object_class->name_changed     = gimp_layer_name_changed;
270 271 272 273
  gimp_object_class->get_memsize      = gimp_layer_get_memsize;

  viewable_class->default_stock_id    = "gimp-layer";
  viewable_class->invalidate_preview  = gimp_layer_invalidate_preview;
274
  viewable_class->get_description     = gimp_layer_get_description;
275

276
  item_class->removed                 = gimp_layer_removed;
277
  item_class->unset_removed           = gimp_layer_unset_removed;
278
  item_class->is_attached             = gimp_layer_is_attached;
279
  item_class->get_tree                = gimp_layer_get_tree;
280
  item_class->duplicate               = gimp_layer_duplicate;
281
  item_class->convert                 = gimp_layer_convert;
282 283 284 285 286 287 288
  item_class->rename                  = gimp_layer_rename;
  item_class->translate               = gimp_layer_translate;
  item_class->scale                   = gimp_layer_scale;
  item_class->resize                  = gimp_layer_resize;
  item_class->flip                    = gimp_layer_flip;
  item_class->rotate                  = gimp_layer_rotate;
  item_class->transform               = gimp_layer_transform;
289
  item_class->to_selection            = gimp_layer_to_selection;
290
  item_class->get_node                = gimp_layer_get_node;
291
  item_class->default_name            = _("Layer");
292 293 294 295 296 297 298
  item_class->rename_desc             = C_("undo-type", "Rename Layer");
  item_class->translate_desc          = C_("undo-type", "Move Layer");
  item_class->scale_desc              = C_("undo-type", "Scale Layer");
  item_class->resize_desc             = C_("undo-type", "Resize Layer");
  item_class->flip_desc               = C_("undo-type", "Flip Layer");
  item_class->rotate_desc             = C_("undo-type", "Rotate Layer");
  item_class->transform_desc          = C_("undo-type", "Transform Layer");
299
  item_class->to_selection_desc       = C_("undo-type", "Alpha to Selection");
300 301 302 303 304 305 306
  item_class->reorder_desc            = C_("undo-type", "Reorder Layer");
  item_class->raise_desc              = C_("undo-type", "Raise Layer");
  item_class->raise_to_top_desc       = C_("undo-type", "Raise Layer to Top");
  item_class->lower_desc              = C_("undo-type", "Lower Layer");
  item_class->lower_to_bottom_desc    = C_("undo-type", "Lower Layer to Bottom");
  item_class->raise_failed            = _("Layer cannot be raised higher.");
  item_class->lower_failed            = _("Layer cannot be lowered more.");
307

308
  drawable_class->estimate_memsize      = gimp_layer_estimate_memsize;
309 310
  drawable_class->invalidate_boundary   = gimp_layer_invalidate_boundary;
  drawable_class->get_active_components = gimp_layer_get_active_components;
311
  drawable_class->project_region        = gimp_layer_project_region;
312 313 314

  klass->opacity_changed              = NULL;
  klass->mode_changed                 = NULL;
315
  klass->lock_alpha_changed           = NULL;
316
  klass->mask_changed                 = NULL;
317 318 319
  klass->apply_mask_changed           = NULL;
  klass->edit_mask_changed            = NULL;
  klass->show_mask_changed            = NULL;
320 321 322 323 324 325

  g_object_class_install_property (object_class, PROP_OPACITY,
                                   g_param_spec_double ("opacity", NULL, NULL,
                                                        GIMP_OPACITY_TRANSPARENT,
                                                        GIMP_OPACITY_OPAQUE,
                                                        GIMP_OPACITY_OPAQUE,
326
                                                        GIMP_PARAM_READABLE));
327 328 329 330 331

  g_object_class_install_property (object_class, PROP_MODE,
                                   g_param_spec_enum ("mode", NULL, NULL,
                                                      GIMP_TYPE_LAYER_MODE_EFFECTS,
                                                      GIMP_NORMAL_MODE,
332
                                                      GIMP_PARAM_READABLE));
333 334 335 336 337

  g_object_class_install_property (object_class, PROP_LOCK_ALPHA,
                                   g_param_spec_boolean ("lock-alpha",
                                                         NULL, NULL,
                                                         FALSE,
338
                                                         GIMP_PARAM_READABLE));
339

340 341 342 343 344 345
  g_object_class_install_property (object_class, PROP_MASK,
                                   g_param_spec_object ("mask",
                                                        NULL, NULL,
                                                        GIMP_TYPE_LAYER_MASK,
                                                        GIMP_PARAM_READABLE));

346 347 348 349 350
  g_object_class_install_property (object_class, PROP_FLOATING_SELECTION,
                                   g_param_spec_boolean ("floating-selection",
                                                         NULL, NULL,
                                                         FALSE,
                                                         GIMP_PARAM_READABLE));
351 352 353 354 355
}

static void
gimp_layer_init (GimpLayer *layer)
{
356 357 358
  layer->opacity    = GIMP_OPACITY_OPAQUE;
  layer->mode       = GIMP_NORMAL_MODE;
  layer->lock_alpha = FALSE;
359

360
  layer->mask       = NULL;
361 362 363
  layer->apply_mask = TRUE;
  layer->edit_mask  = TRUE;
  layer->show_mask  = FALSE;
364

365 366 367 368 369
  /*  floating selection  */
  layer->fs.drawable       = NULL;
  layer->fs.boundary_known = FALSE;
  layer->fs.segs           = NULL;
  layer->fs.num_segs       = 0;
370 371
}

372
static void
373
gimp_layer_pickable_iface_init (GimpPickableInterface *iface)
374
{
375
  iface->get_opacity_at = gimp_layer_get_opacity_at;
376 377
}

378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402
static void
gimp_layer_set_property (GObject      *object,
                         guint         property_id,
                         const GValue *value,
                         GParamSpec   *pspec)
{
  switch (property_id)
    {
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
      break;
    }
}

static void
gimp_layer_get_property (GObject    *object,
                         guint       property_id,
                         GValue     *value,
                         GParamSpec *pspec)
{
  GimpLayer *layer = GIMP_LAYER (object);

  switch (property_id)
    {
    case PROP_OPACITY:
403
      g_value_set_double (value, gimp_layer_get_opacity (layer));
404 405
      break;
    case PROP_MODE:
406
      g_value_set_enum (value, gimp_layer_get_mode (layer));
407 408
      break;
    case PROP_LOCK_ALPHA:
409 410
      g_value_set_boolean (value, gimp_layer_get_lock_alpha (layer));
      break;
411 412 413
    case PROP_MASK:
      g_value_set_object (value, gimp_layer_get_mask (layer));
      break;
414 415
    case PROP_FLOATING_SELECTION:
      g_value_set_boolean (value, gimp_layer_is_floating_sel (layer));
416
      break;
417

418 419 420 421 422 423
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
      break;
    }
}

424 425 426 427 428 429 430 431 432 433
static void
gimp_layer_dispose (GObject *object)
{
  GimpLayer *layer = GIMP_LAYER (object);

  if (layer->mask)
    g_signal_handlers_disconnect_by_func (layer->mask,
                                          gimp_layer_layer_mask_update,
                                          layer);

434 435 436 437
  if (gimp_layer_is_floating_sel (layer))
    {
      GimpDrawable *fs_drawable = gimp_layer_get_floating_sel_drawable (layer);

438 439 440 441 442
      /* only detach if this is actually the drawable's fs because the
       * layer might be on the undo stack and not attached to anyhing
       */
      if (gimp_drawable_get_floating_sel (fs_drawable) == layer)
        gimp_drawable_detach_floating_sel (fs_drawable);
443 444
    }

445 446 447
  G_OBJECT_CLASS (parent_class)->dispose (object);
}

448 449 450
static void
gimp_layer_finalize (GObject *object)
{
451
  GimpLayer *layer = GIMP_LAYER (object);
452 453 454

  if (layer->mask)
    {
455
      g_object_unref (layer->mask);
456 457 458 459 460 461
      layer->mask = NULL;
    }

  if (layer->fs.segs)
    {
      g_free (layer->fs.segs);
462 463
      layer->fs.segs     = NULL;
      layer->fs.num_segs = 0;
464 465 466 467 468
    }

  G_OBJECT_CLASS (parent_class)->finalize (object);
}

469 470 471 472 473
static void
gimp_layer_name_changed (GimpObject *object)
{
  GimpLayer *layer = GIMP_LAYER (object);

474 475
  if (GIMP_OBJECT_CLASS (parent_class)->name_changed)
    GIMP_OBJECT_CLASS (parent_class)->name_changed (object);
476 477 478

  if (layer->mask)
    {
479 480
      gchar *mask_name = g_strdup_printf (_("%s mask"),
                                          gimp_object_get_name (object));
481

482
      gimp_object_take_name (GIMP_OBJECT (layer->mask), mask_name);
483 484 485
    }
}

486
static gint64
487
gimp_layer_get_memsize (GimpObject *object,
488
                        gint64     *gui_size)
489
{
490
  GimpLayer *layer   = GIMP_LAYER (object);
491
  gint64     memsize = 0;
492

493
  memsize += gimp_object_get_memsize (GIMP_OBJECT (layer->mask), gui_size);
494

495
  *gui_size += layer->fs.num_segs * sizeof (BoundSeg);
496

497 498
  return memsize + GIMP_OBJECT_CLASS (parent_class)->get_memsize (object,
                                                                  gui_size);
499 500
}

Elliot Lee's avatar
Elliot Lee committed
501
static void
502
gimp_layer_invalidate_preview (GimpViewable *viewable)
503
{
504
  GimpLayer *layer = GIMP_LAYER (viewable);
505

506
  GIMP_VIEWABLE_CLASS (parent_class)->invalidate_preview (viewable);
507

508
  if (gimp_layer_is_floating_sel (layer))
509
    floating_sel_invalidate (layer);
510 511
}

512 513 514 515 516 517 518
static gchar *
gimp_layer_get_description (GimpViewable  *viewable,
                            gchar        **tooltip)
{
  if (gimp_layer_is_floating_sel (GIMP_LAYER (viewable)))
    {
      return g_strdup_printf (_("Floating Selection\n(%s)"),
519
                              gimp_object_get_name (viewable));
520 521 522 523 524 525
    }

  return GIMP_VIEWABLE_CLASS (parent_class)->get_description (viewable,
                                                              tooltip);
}

526 527 528 529 530 531 532 533 534 535 536 537
static void
gimp_layer_removed (GimpItem *item)
{
  GimpLayer *layer = GIMP_LAYER (item);

  if (layer->mask)
    gimp_item_removed (GIMP_ITEM (layer->mask));

  if (GIMP_ITEM_CLASS (parent_class)->removed)
    GIMP_ITEM_CLASS (parent_class)->removed (item);
}

538 539 540 541 542 543 544 545 546 547 548 549
static void
gimp_layer_unset_removed (GimpItem *item)
{
  GimpLayer *layer = GIMP_LAYER (item);

  if (layer->mask)
    gimp_item_unset_removed (GIMP_ITEM (layer->mask));

  if (GIMP_ITEM_CLASS (parent_class)->unset_removed)
    GIMP_ITEM_CLASS (parent_class)->unset_removed (item);
}

550
static gboolean
551
gimp_layer_is_attached (const GimpItem *item)
552
{
553 554 555 556
  GimpImage *image = gimp_item_get_image (item);

  return (GIMP_IS_IMAGE (image) &&
          gimp_container_have (gimp_image_get_layers (image),
557
                               GIMP_OBJECT (item)));
558 559
}

560 561
static GimpItemTree *
gimp_layer_get_tree (GimpItem *item)
562 563 564 565 566
{
  if (gimp_item_is_attached (item))
    {
      GimpImage *image = gimp_item_get_image (item);

567
      return gimp_image_get_layer_tree (image);
568 569 570 571 572
    }

  return NULL;
}

573 574
static GimpItem *
gimp_layer_duplicate (GimpItem *item,
575
                      GType     new_type)
576
{
577
  GimpItem *new_item;
578 579 580

  g_return_val_if_fail (g_type_is_a (new_type, GIMP_TYPE_DRAWABLE), NULL);

581
  new_item = GIMP_ITEM_CLASS (parent_class)->duplicate (item, new_type);
582

583
  if (GIMP_IS_LAYER (new_item))
584
    {
585
      GimpLayer *layer     = GIMP_LAYER (item);
586 587
      GimpLayer *new_layer = GIMP_LAYER (new_item);

588 589 590 591 592 593
      gimp_layer_set_mode    (new_layer, gimp_layer_get_mode (layer),    FALSE);
      gimp_layer_set_opacity (new_layer, gimp_layer_get_opacity (layer), FALSE);

      if (gimp_layer_can_lock_alpha (new_layer))
        gimp_layer_set_lock_alpha (new_layer,
                                   gimp_layer_get_lock_alpha (layer), FALSE);
594 595 596 597

      /*  duplicate the layer mask if necessary  */
      if (layer->mask)
        {
598 599 600 601
          GimpItem *mask;

          mask = gimp_item_duplicate (GIMP_ITEM (layer->mask),
                                      G_TYPE_FROM_INSTANCE (layer->mask));
602
          gimp_layer_add_mask (new_layer, GIMP_LAYER_MASK (mask), FALSE, NULL);
603 604 605 606

          new_layer->apply_mask = layer->apply_mask;
          new_layer->edit_mask  = layer->edit_mask;
          new_layer->show_mask  = layer->show_mask;
607
        }
608 609 610 611 612
    }

  return new_item;
}

613
static void
614 615
gimp_layer_convert (GimpItem  *item,
                    GimpImage *dest_image)
616
{
617 618 619
  GimpLayer         *layer    = GIMP_LAYER (item);
  GimpDrawable      *drawable = GIMP_DRAWABLE (item);
  GimpImageBaseType  old_base_type;
620 621
  GimpImageBaseType  new_base_type;

622
  old_base_type = GIMP_IMAGE_TYPE_BASE_TYPE (gimp_drawable_type (drawable));
623
  new_base_type = gimp_image_base_type (dest_image);
624

625
  if (old_base_type != new_base_type)
626
    gimp_drawable_convert_type (drawable, dest_image, new_base_type, FALSE);
627

628 629 630 631
  if (layer->mask)
    gimp_item_set_image (GIMP_ITEM (layer->mask), dest_image);

  GIMP_ITEM_CLASS (parent_class)->convert (item, dest_image);
632 633
}

634
static gboolean
635 636 637 638
gimp_layer_rename (GimpItem     *item,
                   const gchar  *new_name,
                   const gchar  *undo_desc,
                   GError      **error)
639
{
640
  GimpLayer *layer = GIMP_LAYER (item);
641
  GimpImage *image = gimp_item_get_image (item);
642 643
  gboolean   attached;
  gboolean   floating_sel;
644

645 646 647 648
  attached     = gimp_item_is_attached (item);
  floating_sel = gimp_layer_is_floating_sel (layer);

  if (floating_sel)
649
    {
650
      if (GIMP_IS_CHANNEL (gimp_layer_get_floating_sel_drawable (layer)))
651
        {
652
          g_set_error_literal (error, GIMP_ERROR, GIMP_FAILED,
653 654 655
			       _("Cannot create a new layer from the floating "
				 "selection because it belongs to a layer mask "
				 "or channel."));
656 657
          return FALSE;
        }
658 659 660

      if (attached)
        {
661
          gimp_image_undo_group_start (image,
662 663
                                       GIMP_UNDO_GROUP_ITEM_PROPERTIES,
                                       undo_desc);
664

665
          floating_sel_to_layer (layer, NULL);
666
        }
667 668
    }

669
  GIMP_ITEM_CLASS (parent_class)->rename (item, new_name, undo_desc, error);
670

671
  if (attached && floating_sel)
672
    gimp_image_undo_group_end (image);
673 674

  return TRUE;
675 676
}

677 678
static void
gimp_layer_translate (GimpItem *item,
679 680
                      gint      offset_x,
                      gint      offset_y,
681
                      gboolean  push_undo)
682
{
683
  GimpLayer *layer = GIMP_LAYER (item);
684

685
  if (push_undo)
686
    gimp_image_undo_push_item_displace (gimp_item_get_image (item), NULL, item);
687

688
  /*  update the old region  */
689 690
  gimp_drawable_update (GIMP_DRAWABLE (layer),
                        0, 0,
691 692
                        gimp_item_get_width  (item),
                        gimp_item_get_height (item));
693 694

  /*  invalidate the selection boundary because of a layer modification  */
695
  gimp_drawable_invalidate_boundary (GIMP_DRAWABLE (layer));
696

697 698
  GIMP_ITEM_CLASS (parent_class)->translate (item, offset_x, offset_y,
                                             push_undo);
699 700

  /*  update the new region  */
701 702
  gimp_drawable_update (GIMP_DRAWABLE (layer),
                        0, 0,
703 704
                        gimp_item_get_width  (item),
                        gimp_item_get_height (item));
705

706
  if (layer->mask)
707
    {
708 709 710 711
      gint off_x, off_y;

      gimp_item_get_offset (item, &off_x, &off_y);
      gimp_item_set_offset (GIMP_ITEM (layer->mask), off_x, off_y);
712 713 714 715 716

      gimp_viewable_invalidate_preview (GIMP_VIEWABLE (layer->mask));
    }
}

717 718 719 720 721 722
static void
gimp_layer_scale (GimpItem              *item,
                  gint                   new_width,
                  gint                   new_height,
                  gint                   new_offset_x,
                  gint                   new_offset_y,
723
                  GimpInterpolationType  interpolation_type,
724
                  GimpProgress          *progress)
725
{
726
  GimpLayer *layer = GIMP_LAYER (item);
727 728 729

  GIMP_ITEM_CLASS (parent_class)->scale (item, new_width, new_height,
                                         new_offset_x, new_offset_y,
730
                                         interpolation_type, progress);
731 732

  if (layer->mask)
733 734 735
    gimp_item_scale (GIMP_ITEM (layer->mask),
                     new_width, new_height,
                     new_offset_x, new_offset_y,
736
                     interpolation_type, progress);
737 738
}

739
static void
740 741
gimp_layer_resize (GimpItem    *item,
                   GimpContext *context,
742 743 744 745
                   gint         new_width,
                   gint         new_height,
                   gint         offset_x,
                   gint         offset_y)
746
{
747
  GimpLayer *layer  = GIMP_LAYER (item);
748

749
  GIMP_ITEM_CLASS (parent_class)->resize (item, context, new_width, new_height,
750 751 752
                                          offset_x, offset_y);

  if (layer->mask)
753
    gimp_item_resize (GIMP_ITEM (layer->mask), context,
754
                      new_width, new_height, offset_x, offset_y);
755 756
}

757 758
static void
gimp_layer_flip (GimpItem            *item,
759
                 GimpContext         *context,
760
                 GimpOrientationType  flip_type,
761 762
                 gdouble              axis,
                 gboolean             clip_result)
763
{
764
  GimpLayer *layer = GIMP_LAYER (item);
765

766 767
  GIMP_ITEM_CLASS (parent_class)->flip (item, context, flip_type, axis,
                                        clip_result);
768 769

  if (layer->mask)
770
    gimp_item_flip (GIMP_ITEM (layer->mask), context,
771
                    flip_type, axis, clip_result);
772 773
}

774 775
static void
gimp_layer_rotate (GimpItem         *item,
776
                   GimpContext      *context,
777 778 779 780 781
                   GimpRotationType  rotate_type,
                   gdouble           center_x,
                   gdouble           center_y,
                   gboolean          clip_result)
{
782
  GimpLayer *layer = GIMP_LAYER (item);
783

784
  GIMP_ITEM_CLASS (parent_class)->rotate (item, context,
785 786 787 788
                                          rotate_type, center_x, center_y,
                                          clip_result);

  if (layer->mask)
789
    gimp_item_rotate (GIMP_ITEM (layer->mask), context,
790 791 792
                      rotate_type, center_x, center_y, clip_result);
}

793 794
static void
gimp_layer_transform (GimpItem               *item,
795
                      GimpContext            *context,
796
                      const GimpMatrix3      *matrix,
797 798
                      GimpTransformDirection  direction,
                      GimpInterpolationType   interpolation_type,
799
                      gint                    recursion_level,
800
                      GimpTransformResize     clip_result,
801
                      GimpProgress           *progress)
802
{
803
  GimpLayer *layer = GIMP_LAYER (item);
804

805
  /* FIXME: make interpolated transformations work on layers without alpha */
806 807 808 809
  if (interpolation_type != GIMP_INTERPOLATION_NONE &&
      ! gimp_drawable_has_alpha (GIMP_DRAWABLE (item)))
    gimp_layer_add_alpha (layer);

810
  GIMP_ITEM_CLASS (parent_class)->transform (item, context, matrix, direction,
811
                                             interpolation_type,
812 813 814
                                             recursion_level,
                                             clip_result,
                                             progress);
815 816

  if (layer->mask)
817
    gimp_item_transform (GIMP_ITEM (layer->mask), context,
818
                         matrix, direction,
819
                         interpolation_type, recursion_level,
820
                         clip_result, progress);
821 822
}

823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839
static void
gimp_layer_to_selection (GimpItem       *item,
                         GimpChannelOps  op,
                         gboolean        antialias,
                         gboolean        feather,
                         gdouble         feather_radius_x,
                         gdouble         feather_radius_y)
{
  GimpLayer *layer = GIMP_LAYER (item);
  GimpImage *image = gimp_item_get_image (item);

  gimp_channel_select_alpha (gimp_image_get_mask (image),
                             GIMP_DRAWABLE (layer),
                             op,
                             feather, feather_radius_x, feather_radius_y);
}

840 841 842 843 844 845 846 847 848
static GeglNode *
gimp_layer_get_node (GimpItem *item)
{
  GimpDrawable *drawable = GIMP_DRAWABLE (item);
  GimpLayer    *layer    = GIMP_LAYER (item);
  GeglNode     *node;
  GeglNode     *offset_node;
  GeglNode     *source;
  GeglNode     *mode_node;
849
  gboolean      source_node_hijacked = FALSE;
850 851 852 853

  node = GIMP_ITEM_CLASS (parent_class)->get_node (item);

  source = gimp_drawable_get_source_node (drawable);
854 855 856 857 858 859 860 861 862 863

  /* if the source node already has a parent, we are a floating
   * selection and the source node has been hijacked by the fs'
   * drawable
   */
  if (gegl_node_get_parent (source))
    source_node_hijacked = TRUE;

  if (! source_node_hijacked)
    gegl_node_add_child (node, source);
864

865 866
  g_warn_if_fail (layer->opacity_node == NULL);

867 868 869 870
  layer->opacity_node = gegl_node_new_child (node,
                                             "operation", "gegl:opacity",
                                             "value",     layer->opacity,
                                             NULL);
871 872 873 874

  if (! source_node_hijacked)
    gegl_node_connect_to (source,              "output",
                          layer->opacity_node, "input");
875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890

  if (layer->mask)
    {
      GeglNode *mask;

      mask = gimp_drawable_get_source_node (GIMP_DRAWABLE (layer->mask));

      gegl_node_connect_to (mask,                "output",
                            layer->opacity_node, "aux");
    }

  offset_node = gimp_item_get_offset_node (GIMP_ITEM (layer));

  gegl_node_connect_to (layer->opacity_node, "output",
                        offset_node,         "input");

891
  gimp_layer_sync_mode_node (layer);
892

893
  mode_node = gimp_drawable_get_mode_node (drawable);
894 895 896 897 898 899 900

  gegl_node_connect_to (offset_node, "output",
                        mode_node,   "aux");

  return node;
}

901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917
static gint64
gimp_layer_estimate_memsize (const GimpDrawable *drawable,
                             gint                width,
                             gint                height)
{
  GimpLayer *layer   = GIMP_LAYER (drawable);
  gint64     memsize = 0;

  if (layer->mask)
    memsize += gimp_drawable_estimate_memsize (GIMP_DRAWABLE (layer->mask),
                                               width, height);

  return memsize + GIMP_DRAWABLE_CLASS (parent_class)->estimate_memsize (drawable,
                                                                         width,
                                                                         height);
}

918 919 920
static void
gimp_layer_invalidate_boundary (GimpDrawable *drawable)
{
921
  GimpLayer   *layer = GIMP_LAYER (drawable);
922
  GimpImage   *image;
923 924
  GimpChannel *mask;

925
  if (! (image = gimp_item_get_image (GIMP_ITEM (layer))))
926 927 928
    return;

  /*  Turn the current selection off  */
929
  gimp_image_selection_invalidate (image);
930 931

  /*  get the selection mask channel  */
932
  mask = gimp_image_get_mask (image);
933 934 935 936 937 938 939 940 941 942

  /*  Only bother with the bounds if there is a selection  */
  if (! gimp_channel_is_empty (mask))
    {
      mask->bounds_known   = FALSE;
      mask->boundary_known = FALSE;
    }

  if (gimp_layer_is_floating_sel (layer))
    floating_sel_invalidate (layer);
943 944
}

945 946 947 948 949 950 951 952
static void
gimp_layer_get_active_components (const GimpDrawable *drawable,
                                  gboolean           *active)
{
  GimpLayer *layer = GIMP_LAYER (drawable);
  GimpImage *image = gimp_item_get_image (GIMP_ITEM (drawable));

  /*  first copy the image active channels  */
953
  gimp_image_get_active_array (image, active);
954 955 956 957 958

  if (gimp_drawable_has_alpha (drawable) && layer->lock_alpha)
    active[gimp_drawable_bytes (drawable) - 1] = FALSE;
}

959 960
static gint
gimp_layer_get_opacity_at (GimpPickable *pickable,
961 962
                           gint          x,
                           gint          y)
963 964 965 966 967
{
  GimpLayer *layer = GIMP_LAYER (pickable);
  Tile      *tile;
  gint       val   = 0;

968 969
  if (x >= 0 && x < gimp_item_get_width  (GIMP_ITEM (layer)) &&
      y >= 0 && y < gimp_item_get_height (GIMP_ITEM (layer)) &&
970 971 972 973 974 975
      gimp_item_get_visible (GIMP_ITEM (layer)))
    {
      /*  If the point is inside, and the layer has no
       *  alpha channel, success!
       */
      if (! gimp_drawable_has_alpha (GIMP_DRAWABLE (layer)))
976
        return OPAQUE_OPACITY;
977 978 979 980

      /*  Otherwise, determine if the alpha value at
       *  the given point is non-zero
       */
981
      tile = tile_manager_get_tile (gimp_drawable_get_tiles (GIMP_DRAWABLE (layer)),
982
                                    x, y, TRUE, FALSE);
983

984
      val = * ((const guchar *) tile_data_pointer (tile, x, y) +
985
               tile_bpp (tile) - 1);
986 987

      if (layer->mask)
988 989
        {
          gint mask_val;
990 991 992 993

          mask_val = gimp_pickable_get_opacity_at (GIMP_PICKABLE (layer->mask),
                                                   x, y);

994 995
          val = val * mask_val / 255;
        }
996 997 998 999 1000 1001 1002

      tile_release (tile, FALSE);
    }

  return val;
}

1003 1004 1005 1006 1007 1008 1009 1010
static void
gimp_layer_layer_mask_update (GimpDrawable *drawable,
                              gint          x,
                              gint          y,
                              gint          width,
                              gint          height,
                              GimpLayer    *layer)
{
1011 1012
  if (gimp_layer_get_apply_mask (layer) ||
      gimp_layer_get_show_mask (layer))
1013 1014 1015 1016 1017 1018
    {
      gimp_drawable_update (GIMP_DRAWABLE (layer),
                            x, y, width, height);
    }
}

1019 1020 1021 1022 1023 1024 1025 1026 1027
static void
gimp_layer_sync_mode_node (GimpLayer *layer)
{
  if (layer->opacity_node)
    {
      GeglNode *mode_node;

      mode_node = gimp_drawable_get_mode_node (GIMP_DRAWABLE (layer));

1028 1029
      switch (layer->mode)
        {
1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054
        case GIMP_DISSOLVE_MODE:
        case GIMP_BEHIND_MODE:
        case GIMP_MULTIPLY_MODE:
        case GIMP_SCREEN_MODE:
        case GIMP_OVERLAY_MODE:
        case GIMP_DIFFERENCE_MODE:
        case GIMP_ADDITION_MODE:
        case GIMP_SUBTRACT_MODE:
        case GIMP_DARKEN_ONLY_MODE:
        case GIMP_LIGHTEN_ONLY_MODE:
        case GIMP_HUE_MODE:
        case GIMP_SATURATION_MODE:
        case GIMP_COLOR_MODE:
        case GIMP_VALUE_MODE:
        case GIMP_DIVIDE_MODE:
        case GIMP_DODGE_MODE:
        case GIMP_BURN_MODE:
        case GIMP_HARDLIGHT_MODE:
        case GIMP_SOFTLIGHT_MODE:
        case GIMP_GRAIN_EXTRACT_MODE:
        case GIMP_GRAIN_MERGE_MODE:
        case GIMP_COLOR_ERASE_MODE:
        case GIMP_ERASE_MODE:
        case GIMP_REPLACE_MODE:
        case GIMP_ANTI_ERASE_MODE:
1055
          gegl_node_set (mode_node,
1056 1057
                         "operation",  "gimp:point-layer-mode",
                         "blend-mode", layer->mode,
1058 1059 1060 1061 1062
                         NULL);
          break;

        default:
          gegl_node_set (mode_node,
1063 1064
                         "operation",
                         gimp_layer_mode_to_gegl_operation (layer->mode),
1065 1066 1067
                         NULL);
          break;
        }
1068 1069 1070
    }
}

1071 1072

/*  public functions  */