gimplayer.c 64.8 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

32
#include "gegl/gimp-gegl-nodes.h"
33 34
#include "gegl/gimp-gegl-utils.h"

35
#include "gimp-apply-operation.h"
36
#include "gimpboundary.h"
37
#include "gimpchannel-select.h"
38
#include "gimpcontext.h"
39
#include "gimpcontainer.h"
40
#include "gimpdrawable-convert.h"
41
#include "gimpdrawable-operation.h"
42
#include "gimperror.h"
43
#include "gimpimage-undo-push.h"
44 45
#include "gimpimage-undo.h"
#include "gimpimage.h"
46
#include "gimplayer-floating-sel.h"
47
#include "gimplayer.h"
48
#include "gimplayer-project.h"
49
#include "gimplayermask.h"
50
#include "gimpmarshal.h"
51
#include "gimppickable.h"
52

53
#include "gimp-intl.h"
54 55


56 57
enum
{
58 59
  OPACITY_CHANGED,
  MODE_CHANGED,
60
  LOCK_ALPHA_CHANGED,
61
  MASK_CHANGED,
62 63 64
  APPLY_MASK_CHANGED,
  EDIT_MASK_CHANGED,
  SHOW_MASK_CHANGED,
65 66 67
  LAST_SIGNAL
};

68 69 70 71 72
enum
{
  PROP_0,
  PROP_OPACITY,
  PROP_MODE,
73
  PROP_LOCK_ALPHA,
74
  PROP_MASK,
75
  PROP_FLOATING_SELECTION
76 77
};

78

79
static void   gimp_layer_pickable_iface_init (GimpPickableInterface *iface);
80

81 82 83 84 85 86 87 88
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);
89
static void       gimp_layer_dispose            (GObject            *object);
90
static void       gimp_layer_finalize           (GObject            *object);
91

92
static void       gimp_layer_name_changed       (GimpObject         *object);
93 94
static gint64     gimp_layer_get_memsize        (GimpObject         *object,
                                                 gint64             *gui_size);
95

96
static void       gimp_layer_invalidate_preview (GimpViewable       *viewable);
97 98
static gchar    * gimp_layer_get_description    (GimpViewable       *viewable,
                                                 gchar             **tooltip);
99

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

static gint64  gimp_layer_estimate_memsize      (const GimpDrawable *drawable,
                                                 gint                width,
                                                 gint                height);
159 160 161
static void    gimp_layer_invalidate_boundary   (GimpDrawable       *drawable);
static void    gimp_layer_get_active_components (const GimpDrawable *drawable,
                                                 gboolean           *active);
162

163 164 165
static gint    gimp_layer_get_opacity_at        (GimpPickable       *pickable,
                                                 gint                x,
                                                 gint                y);
166

167 168 169 170 171 172 173
static void       gimp_layer_layer_mask_update  (GimpDrawable       *layer_mask,
                                                 gint                x,
                                                 gint                y,
                                                 gint                width,
                                                 gint                height,
                                                 GimpLayer          *layer);

174

175 176
G_DEFINE_TYPE_WITH_CODE (GimpLayer, gimp_layer, GIMP_TYPE_DRAWABLE,
                         G_IMPLEMENT_INTERFACE (GIMP_TYPE_PICKABLE,
177
                                                gimp_layer_pickable_iface_init))
178

179
#define parent_class gimp_layer_parent_class
180

181
static guint layer_signals[LAST_SIGNAL] = { 0 };
182

183 184

static void
185
gimp_layer_class_init (GimpLayerClass *klass)
186
{
187 188 189 190 191
  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);
192

193
  layer_signals[OPACITY_CHANGED] =
194
    g_signal_new ("opacity-changed",
195 196 197 198 199 200
                  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);
201 202

  layer_signals[MODE_CHANGED] =
203
    g_signal_new ("mode-changed",
204 205 206 207 208 209
                  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);
210

211 212
  layer_signals[LOCK_ALPHA_CHANGED] =
    g_signal_new ("lock-alpha-changed",
213 214 215 216 217 218
                  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);
219

220
  layer_signals[MASK_CHANGED] =
221
    g_signal_new ("mask-changed",
222 223 224 225 226 227
                  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);
228

229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255
  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);

256 257
  object_class->set_property          = gimp_layer_set_property;
  object_class->get_property          = gimp_layer_get_property;
258
  object_class->dispose               = gimp_layer_dispose;
259 260
  object_class->finalize              = gimp_layer_finalize;

261
  gimp_object_class->name_changed     = gimp_layer_name_changed;
262 263 264 265
  gimp_object_class->get_memsize      = gimp_layer_get_memsize;

  viewable_class->default_stock_id    = "gimp-layer";
  viewable_class->invalidate_preview  = gimp_layer_invalidate_preview;
266
  viewable_class->get_description     = gimp_layer_get_description;
267

268
  item_class->removed                 = gimp_layer_removed;
269
  item_class->unset_removed           = gimp_layer_unset_removed;
270
  item_class->is_attached             = gimp_layer_is_attached;
271
  item_class->get_tree                = gimp_layer_get_tree;
272
  item_class->duplicate               = gimp_layer_duplicate;
273
  item_class->convert                 = gimp_layer_convert;
274 275 276 277 278 279 280
  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;
281
  item_class->to_selection            = gimp_layer_to_selection;
282
  item_class->get_node                = gimp_layer_get_node;
283
  item_class->default_name            = _("Layer");
284 285 286 287 288 289 290
  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");
291
  item_class->to_selection_desc       = C_("undo-type", "Alpha to Selection");
292 293 294 295 296 297 298
  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.");
299

300
  drawable_class->estimate_memsize      = gimp_layer_estimate_memsize;
301 302
  drawable_class->invalidate_boundary   = gimp_layer_invalidate_boundary;
  drawable_class->get_active_components = gimp_layer_get_active_components;
303
  drawable_class->project_region        = gimp_layer_project_region;
304 305 306

  klass->opacity_changed              = NULL;
  klass->mode_changed                 = NULL;
307
  klass->lock_alpha_changed           = NULL;
308
  klass->mask_changed                 = NULL;
309 310 311
  klass->apply_mask_changed           = NULL;
  klass->edit_mask_changed            = NULL;
  klass->show_mask_changed            = NULL;
312 313 314 315 316 317

  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,
318
                                                        GIMP_PARAM_READABLE));
319 320 321 322 323

  g_object_class_install_property (object_class, PROP_MODE,
                                   g_param_spec_enum ("mode", NULL, NULL,
                                                      GIMP_TYPE_LAYER_MODE_EFFECTS,
                                                      GIMP_NORMAL_MODE,
324
                                                      GIMP_PARAM_READABLE));
325 326 327 328 329

  g_object_class_install_property (object_class, PROP_LOCK_ALPHA,
                                   g_param_spec_boolean ("lock-alpha",
                                                         NULL, NULL,
                                                         FALSE,
330
                                                         GIMP_PARAM_READABLE));
331

332 333 334 335 336 337
  g_object_class_install_property (object_class, PROP_MASK,
                                   g_param_spec_object ("mask",
                                                        NULL, NULL,
                                                        GIMP_TYPE_LAYER_MASK,
                                                        GIMP_PARAM_READABLE));

338 339 340 341 342
  g_object_class_install_property (object_class, PROP_FLOATING_SELECTION,
                                   g_param_spec_boolean ("floating-selection",
                                                         NULL, NULL,
                                                         FALSE,
                                                         GIMP_PARAM_READABLE));
343 344 345 346 347
}

static void
gimp_layer_init (GimpLayer *layer)
{
348 349 350
  layer->opacity    = GIMP_OPACITY_OPAQUE;
  layer->mode       = GIMP_NORMAL_MODE;
  layer->lock_alpha = FALSE;
351

352
  layer->mask       = NULL;
353 354 355
  layer->apply_mask = TRUE;
  layer->edit_mask  = TRUE;
  layer->show_mask  = FALSE;
356

357 358 359 360 361
  /*  floating selection  */
  layer->fs.drawable       = NULL;
  layer->fs.boundary_known = FALSE;
  layer->fs.segs           = NULL;
  layer->fs.num_segs       = 0;
362 363
}

364
static void
365
gimp_layer_pickable_iface_init (GimpPickableInterface *iface)
366
{
367
  iface->get_opacity_at = gimp_layer_get_opacity_at;
368 369
}

370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394
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:
395
      g_value_set_double (value, gimp_layer_get_opacity (layer));
396 397
      break;
    case PROP_MODE:
398
      g_value_set_enum (value, gimp_layer_get_mode (layer));
399 400
      break;
    case PROP_LOCK_ALPHA:
401 402
      g_value_set_boolean (value, gimp_layer_get_lock_alpha (layer));
      break;
403 404 405
    case PROP_MASK:
      g_value_set_object (value, gimp_layer_get_mask (layer));
      break;
406 407
    case PROP_FLOATING_SELECTION:
      g_value_set_boolean (value, gimp_layer_is_floating_sel (layer));
408
      break;
409

410 411 412 413 414 415
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
      break;
    }
}

416 417 418 419 420 421 422 423 424 425
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);

426 427 428 429
  if (gimp_layer_is_floating_sel (layer))
    {
      GimpDrawable *fs_drawable = gimp_layer_get_floating_sel_drawable (layer);

430 431 432 433 434
      /* 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);
435 436
    }

437 438 439
  G_OBJECT_CLASS (parent_class)->dispose (object);
}

440 441 442
static void
gimp_layer_finalize (GObject *object)
{
443
  GimpLayer *layer = GIMP_LAYER (object);
444 445 446

  if (layer->mask)
    {
447
      g_object_unref (layer->mask);
448 449 450 451 452 453
      layer->mask = NULL;
    }

  if (layer->fs.segs)
    {
      g_free (layer->fs.segs);
454 455
      layer->fs.segs     = NULL;
      layer->fs.num_segs = 0;
456 457 458 459 460
    }

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

461 462 463 464 465
static void
gimp_layer_name_changed (GimpObject *object)
{
  GimpLayer *layer = GIMP_LAYER (object);

466 467
  if (GIMP_OBJECT_CLASS (parent_class)->name_changed)
    GIMP_OBJECT_CLASS (parent_class)->name_changed (object);
468 469 470

  if (layer->mask)
    {
471 472
      gchar *mask_name = g_strdup_printf (_("%s mask"),
                                          gimp_object_get_name (object));
473

474
      gimp_object_take_name (GIMP_OBJECT (layer->mask), mask_name);
475 476 477
    }
}

478
static gint64
479
gimp_layer_get_memsize (GimpObject *object,
480
                        gint64     *gui_size)
481
{
482
  GimpLayer *layer   = GIMP_LAYER (object);
483
  gint64     memsize = 0;
484

485
  memsize += gimp_object_get_memsize (GIMP_OBJECT (layer->mask), gui_size);
486

487
  *gui_size += layer->fs.num_segs * sizeof (GimpBoundSeg);
488

489 490
  return memsize + GIMP_OBJECT_CLASS (parent_class)->get_memsize (object,
                                                                  gui_size);
491 492
}

Elliot Lee's avatar
Elliot Lee committed
493
static void
494
gimp_layer_invalidate_preview (GimpViewable *viewable)
495
{
496
  GimpLayer *layer = GIMP_LAYER (viewable);
497

498
  GIMP_VIEWABLE_CLASS (parent_class)->invalidate_preview (viewable);
499

500
  if (gimp_layer_is_floating_sel (layer))
501
    floating_sel_invalidate (layer);
502 503
}

504 505 506 507 508 509 510
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)"),
511
                              gimp_object_get_name (viewable));
512 513 514 515 516 517
    }

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

518 519 520 521 522 523 524 525 526 527 528 529
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);
}

530 531 532 533 534 535 536 537 538 539 540 541
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);
}

542
static gboolean
543
gimp_layer_is_attached (const GimpItem *item)
544
{
545 546 547 548
  GimpImage *image = gimp_item_get_image (item);

  return (GIMP_IS_IMAGE (image) &&
          gimp_container_have (gimp_image_get_layers (image),
549
                               GIMP_OBJECT (item)));
550 551
}

552 553
static GimpItemTree *
gimp_layer_get_tree (GimpItem *item)
554 555 556 557 558
{
  if (gimp_item_is_attached (item))
    {
      GimpImage *image = gimp_item_get_image (item);

559
      return gimp_image_get_layer_tree (image);
560 561 562 563 564
    }

  return NULL;
}

565 566
static GimpItem *
gimp_layer_duplicate (GimpItem *item,
567
                      GType     new_type)
568
{
569
  GimpItem *new_item;
570 571 572

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

573
  new_item = GIMP_ITEM_CLASS (parent_class)->duplicate (item, new_type);
574

575
  if (GIMP_IS_LAYER (new_item))
576
    {
577
      GimpLayer *layer     = GIMP_LAYER (item);
578 579
      GimpLayer *new_layer = GIMP_LAYER (new_item);

580 581 582 583 584 585
      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);
586 587 588 589

      /*  duplicate the layer mask if necessary  */
      if (layer->mask)
        {
590 591 592 593
          GimpItem *mask;

          mask = gimp_item_duplicate (GIMP_ITEM (layer->mask),
                                      G_TYPE_FROM_INSTANCE (layer->mask));
594
          gimp_layer_add_mask (new_layer, GIMP_LAYER_MASK (mask), FALSE, NULL);
595 596 597 598

          new_layer->apply_mask = layer->apply_mask;
          new_layer->edit_mask  = layer->edit_mask;
          new_layer->show_mask  = layer->show_mask;
599
        }
600 601 602 603 604
    }

  return new_item;
}

605
static void
606 607
gimp_layer_convert (GimpItem  *item,
                    GimpImage *dest_image)
608
{
609 610 611
  GimpLayer         *layer    = GIMP_LAYER (item);
  GimpDrawable      *drawable = GIMP_DRAWABLE (item);
  GimpImageBaseType  old_base_type;
612 613
  GimpImageBaseType  new_base_type;

614
  old_base_type = GIMP_IMAGE_TYPE_BASE_TYPE (gimp_drawable_type (drawable));
615
  new_base_type = gimp_image_base_type (dest_image);
616

617
  if (old_base_type != new_base_type)
618
    gimp_drawable_convert_type (drawable, dest_image, new_base_type, FALSE);
619

620 621 622 623
  if (layer->mask)
    gimp_item_set_image (GIMP_ITEM (layer->mask), dest_image);

  GIMP_ITEM_CLASS (parent_class)->convert (item, dest_image);
624 625
}

626
static gboolean
627 628 629 630
gimp_layer_rename (GimpItem     *item,
                   const gchar  *new_name,
                   const gchar  *undo_desc,
                   GError      **error)
631
{
632
  GimpLayer *layer = GIMP_LAYER (item);
633
  GimpImage *image = gimp_item_get_image (item);
634 635
  gboolean   attached;
  gboolean   floating_sel;
636

637 638 639 640
  attached     = gimp_item_is_attached (item);
  floating_sel = gimp_layer_is_floating_sel (layer);

  if (floating_sel)
641
    {
642
      if (GIMP_IS_CHANNEL (gimp_layer_get_floating_sel_drawable (layer)))
643
        {
644
          g_set_error_literal (error, GIMP_ERROR, GIMP_FAILED,
645 646 647
			       _("Cannot create a new layer from the floating "
				 "selection because it belongs to a layer mask "
				 "or channel."));
648 649
          return FALSE;
        }
650 651 652

      if (attached)
        {
653
          gimp_image_undo_group_start (image,
654 655
                                       GIMP_UNDO_GROUP_ITEM_PROPERTIES,
                                       undo_desc);
656

657
          floating_sel_to_layer (layer, NULL);
658
        }
659 660
    }

661
  GIMP_ITEM_CLASS (parent_class)->rename (item, new_name, undo_desc, error);
662

663
  if (attached && floating_sel)
664
    gimp_image_undo_group_end (image);
665 666

  return TRUE;
667 668
}

669 670
static void
gimp_layer_translate (GimpItem *item,
671 672
                      gint      offset_x,
                      gint      offset_y,
673
                      gboolean  push_undo)
674
{
675
  GimpLayer *layer = GIMP_LAYER (item);
676

677
  if (push_undo)
678
    gimp_image_undo_push_item_displace (gimp_item_get_image (item), NULL, item);
679

680
  /*  update the old region  */
681 682
  gimp_drawable_update (GIMP_DRAWABLE (layer),
                        0, 0,
683 684
                        gimp_item_get_width  (item),
                        gimp_item_get_height (item));
685 686

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

689 690
  GIMP_ITEM_CLASS (parent_class)->translate (item, offset_x, offset_y,
                                             push_undo);
691 692

  /*  update the new region  */
693 694
  gimp_drawable_update (GIMP_DRAWABLE (layer),
                        0, 0,
695 696
                        gimp_item_get_width  (item),
                        gimp_item_get_height (item));
697

698
  if (layer->mask)
699
    {
700 701 702 703
      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);
704 705 706 707 708

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

709 710 711 712 713 714
static void
gimp_layer_scale (GimpItem              *item,
                  gint                   new_width,
                  gint                   new_height,
                  gint                   new_offset_x,
                  gint                   new_offset_y,
715
                  GimpInterpolationType  interpolation_type,
716
                  GimpProgress          *progress)
717
{
718
  GimpLayer *layer = GIMP_LAYER (item);
719 720 721

  GIMP_ITEM_CLASS (parent_class)->scale (item, new_width, new_height,
                                         new_offset_x, new_offset_y,
722
                                         interpolation_type, progress);
723 724

  if (layer->mask)
725 726 727
    gimp_item_scale (GIMP_ITEM (layer->mask),
                     new_width, new_height,
                     new_offset_x, new_offset_y,
728
                     interpolation_type, progress);
729 730
}

731
static void
732 733
gimp_layer_resize (GimpItem    *item,
                   GimpContext *context,
734 735 736 737
                   gint         new_width,
                   gint         new_height,
                   gint         offset_x,
                   gint         offset_y)
738
{
739
  GimpLayer *layer  = GIMP_LAYER (item);
740

741
  GIMP_ITEM_CLASS (parent_class)->resize (item, context, new_width, new_height,
742 743 744
                                          offset_x, offset_y);

  if (layer->mask)
745
    gimp_item_resize (GIMP_ITEM (layer->mask), context,
746
                      new_width, new_height, offset_x, offset_y);
747 748
}

749 750
static void
gimp_layer_flip (GimpItem            *item,
751
                 GimpContext         *context,
752
                 GimpOrientationType  flip_type,
753 754
                 gdouble              axis,
                 gboolean             clip_result)
755
{
756
  GimpLayer *layer = GIMP_LAYER (item);
757

758 759
  GIMP_ITEM_CLASS (parent_class)->flip (item, context, flip_type, axis,
                                        clip_result);
760 761

  if (layer->mask)
762
    gimp_item_flip (GIMP_ITEM (layer->mask), context,
763
                    flip_type, axis, clip_result);
764 765
}

766 767
static void
gimp_layer_rotate (GimpItem         *item,
768
                   GimpContext      *context,
769 770 771 772 773
                   GimpRotationType  rotate_type,
                   gdouble           center_x,
                   gdouble           center_y,
                   gboolean          clip_result)
{
774
  GimpLayer *layer = GIMP_LAYER (item);
775

776
  GIMP_ITEM_CLASS (parent_class)->rotate (item, context,
777 778 779 780
                                          rotate_type, center_x, center_y,
                                          clip_result);

  if (layer->mask)
781
    gimp_item_rotate (GIMP_ITEM (layer->mask), context,
782 783 784
                      rotate_type, center_x, center_y, clip_result);
}

785 786
static void
gimp_layer_transform (GimpItem               *item,
787
                      GimpContext            *context,
788
                      const GimpMatrix3      *matrix,
789 790
                      GimpTransformDirection  direction,
                      GimpInterpolationType   interpolation_type,
791
                      gint                    recursion_level,
792
                      GimpTransformResize     clip_result,
793
                      GimpProgress           *progress)
794
{
795
  GimpLayer *layer = GIMP_LAYER (item);
796

797
  /* FIXME: make interpolated transformations work on layers without alpha */
798 799 800 801
  if (interpolation_type != GIMP_INTERPOLATION_NONE &&
      ! gimp_drawable_has_alpha (GIMP_DRAWABLE (item)))
    gimp_layer_add_alpha (layer);

802
  GIMP_ITEM_CLASS (parent_class)->transform (item, context, matrix, direction,
803
                                             interpolation_type,
804 805 806
                                             recursion_level,
                                             clip_result,
                                             progress);
807 808

  if (layer->mask)
809
    gimp_item_transform (GIMP_ITEM (layer->mask), context,
810
                         matrix, direction,
811
                         interpolation_type, recursion_level,
812
                         clip_result, progress);
813 814
}

815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831
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);
}

832 833 834 835 836 837 838 839 840
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;
841
  gboolean      source_node_hijacked = FALSE;
842 843 844 845

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

  source = gimp_drawable_get_source_node (drawable);
846 847 848 849 850 851 852 853 854 855

  /* 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);
856

857 858
  g_warn_if_fail (layer->opacity_node == NULL);

859 860 861 862
  layer->opacity_node = gegl_node_new_child (node,
                                             "operation", "gegl:opacity",
                                             "value",     layer->opacity,
                                             NULL);
863

864 865 866 867 868 869
  if (! source_node_hijacked &&
      (! layer->mask || ! gimp_layer_get_show_mask (layer)))
    {
      gegl_node_connect_to (source,              "output",
                            layer->opacity_node, "input");
    }
870 871 872 873 874 875 876

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

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

877 878 879 880 881
      if (gimp_layer_get_show_mask (layer))
        {
          gegl_node_connect_to (mask,                "output",
                                layer->opacity_node, "input");
        }
882
      else if (gimp_layer_get_apply_mask (layer))
883 884 885 886
        {
          gegl_node_connect_to (mask,                "output",
                                layer->opacity_node, "aux");
        }
887 888 889 890 891 892 893
    }

  offset_node = gimp_item_get_offset_node (GIMP_ITEM (layer));

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

894
  mode_node = gimp_drawable_get_mode_node (drawable);
895

896 897 898 899
  gimp_gegl_node_set_layer_mode (mode_node,
                                 layer->mode,
                                 TRUE);

900 901 902 903 904 905
  gegl_node_connect_to (offset_node, "output",
                        mode_node,   "aux");

  return node;
}

906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922
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);
}

923 924 925
static void
gimp_layer_invalidate_boundary (GimpDrawable *drawable)
{
926
  GimpLayer   *layer = GIMP_LAYER (drawable);
927
  GimpImage   *image;
928 929
  GimpChannel *mask;

930
  if (! (image = gimp_item_get_image (GIMP_ITEM (layer))))
931 932 933
    return;

  /*  Turn the current selection off  */
934
  gimp_image_selection_invalidate (image);
935 936

  /*  get the selection mask channel  */
937
  mask = gimp_image_get_mask (image);
938 939 940 941 942 943 944 945 946 947

  /*  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);
948 949
}

950 951 952 953 954 955 956 957
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  */
958
  gimp_image_get_active_array (image, active);
959 960 961 962 963

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

964 965
static gint
gimp_layer_get_opacity_at (GimpPickable *pickable,
966 967
                           gint          x,
                           gint          y)
968 969
{
  GimpLayer *layer = GIMP_LAYER (pickable);
970
  guchar     value = 0;
971

972 973
  if (x >= 0 && x < gimp_item_get_width  (GIMP_ITEM (layer)) &&
      y >= 0 && y < gimp_item_get_height (GIMP_ITEM (layer)) &&
974 975 976 977 978 979
      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)))
980
        return OPAQUE_OPACITY;
981 982 983 984

      /*  Otherwise, determine if the alpha value at
       *  the given point is non-zero
       */
985
      gegl_buffer_sample (gimp_drawable_get_buffer (GIMP_DRAWABLE (layer)),
986
                          x, y, NULL, &value, babl_format ("A u8"),
987
                          GEGL_SAMPLER_NEAREST, GEGL_ABYSS_NONE);
988 989

      if (layer->mask)
990
        {
991
          gint mask_value;
992

993 994
          mask_value = gimp_pickable_get_opacity_at (GIMP_PICKABLE (layer->mask),
                                                     x, y);
995

996
          value = value * mask_value / 255;
997
        }
998 999
    }

1000
  return value;
1001 1002
}

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 1019 1020
    {
      gimp_drawable_update (GIMP_DRAWABLE (layer),
                            x, y, width, height);
    }
}


/*  public functions  */
Elliot Lee's avatar
Elliot Lee committed
1021

1022
GimpLayer *
1023
gimp_layer_new (GimpImage            *image,
1024 1025
                gint                  width,
                gint                  height,
1026
                const Babl           *format,
1027 1028 1029
                const gchar          *name,
                gdouble               opacity,
                GimpLayerModeEffects  mode)
Elliot Lee's avatar
Elliot Lee committed
1030
{
1031
  GimpLayer *layer;
Elliot Lee's avatar
Elliot Lee committed
1032