gimplayer.c 65.1 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-operation.h"
41
#include "gimperror.h"
42
#include "gimpimage-undo-push.h"
43 44
#include "gimpimage-undo.h"
#include "gimpimage.h"
45
#include "gimplayer-floating-sel.h"
46
#include "gimplayer.h"
47
#include "gimplayer-project.h"
48
#include "gimplayermask.h"
49
#include "gimpmarshal.h"
50
#include "gimppickable.h"
51

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


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

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

77

78
static void   gimp_layer_pickable_iface_init (GimpPickableInterface *iface);
79

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

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

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

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

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

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

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

173

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

178
#define parent_class gimp_layer_parent_class
179

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

182 183

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

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

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

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

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

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
  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);

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

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

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

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

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

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

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

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

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

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

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

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

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

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

363
static void
364
gimp_layer_pickable_iface_init (GimpPickableInterface *iface)
365
{
366
  iface->get_opacity_at = gimp_layer_get_opacity_at;
367 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
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:
394
      g_value_set_double (value, gimp_layer_get_opacity (layer));
395 396
      break;
    case PROP_MODE:
397
      g_value_set_enum (value, gimp_layer_get_mode (layer));
398 399
      break;
    case PROP_LOCK_ALPHA:
400 401
      g_value_set_boolean (value, gimp_layer_get_lock_alpha (layer));
      break;
402 403 404
    case PROP_MASK:
      g_value_set_object (value, gimp_layer_get_mask (layer));
      break;
405 406
    case PROP_FLOATING_SELECTION:
      g_value_set_boolean (value, gimp_layer_is_floating_sel (layer));
407
      break;
408

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

  return NULL;
}

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

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

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

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

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

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

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

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

  return new_item;
}

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

615
  old_base_type = gimp_drawable_get_base_type (drawable);
616
  new_base_type = gimp_image_base_type (dest_image);
617

618 619 620 621 622
  old_precision = gimp_drawable_get_precision (drawable);
  new_precision = gimp_image_get_precision (dest_image);

  if (old_base_type != new_base_type ||
      old_precision != new_precision)
623 624 625 626 627
    {
      gimp_drawable_convert_type (drawable, dest_image,
                                  new_base_type, new_precision,
                                  FALSE);
    }
628

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

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

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

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

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

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

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

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

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

  return TRUE;
676 677
}

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

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

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

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

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

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

707
  if (layer->mask)
708
    {
709 710 711 712
      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);
713 714 715 716 717

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840
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);
}

841 842 843 844 845 846 847 848 849
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;
850
  gboolean      source_node_hijacked = FALSE;
851 852 853 854

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

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

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

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

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

873 874 875 876 877 878
  if (! source_node_hijacked &&
      (! layer->mask || ! gimp_layer_get_show_mask (layer)))
    {
      gegl_node_connect_to (source,              "output",
                            layer->opacity_node, "input");
    }
879 880 881 882 883 884 885

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

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

886 887 888 889 890
      if (gimp_layer_get_show_mask (layer))
        {
          gegl_node_connect_to (mask,                "output",
                                layer->opacity_node, "input");
        }
891
      else if (gimp_layer_get_apply_mask (layer))
892 893 894 895
        {
          gegl_node_connect_to (mask,                "output",
                                layer->opacity_node, "aux");
        }
896 897 898 899 900 901 902
    }

  offset_node = gimp_item_get_offset_node (GIMP_ITEM (layer));

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

903
  mode_node = gimp_drawable_get_mode_node (drawable);
904

905 906 907 908
  gimp_gegl_node_set_layer_mode (mode_node,
                                 layer->mode,
                                 TRUE);

909 910 911 912 913 914
  gegl_node_connect_to (offset_node, "output",
                        mode_node,   "aux");

  return node;
}

915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931
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);
}

932 933 934
static void
gimp_layer_invalidate_boundary (GimpDrawable *drawable)
{
935
  GimpLayer   *layer = GIMP_LAYER (drawable);
936
  GimpImage   *image;
937 938
  GimpChannel *mask;

939
  if (! (image = gimp_item_get_image (GIMP_ITEM (layer))))
940 941 942
    return;

  /*  Turn the current selection off  */
943
  gimp_image_selection_invalidate (image);
944 945

  /*  get the selection mask channel  */
946
  mask = gimp_image_get_mask (image);
947 948 949 950 951 952 953 954 955 956

  /*  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);
957 958
}

959 960 961 962
static void
gimp_layer_get_active_components (const GimpDrawable *drawable,
                                  gboolean           *active)
{
963 964 965
  GimpLayer  *layer  = GIMP_LAYER (drawable);
  GimpImage  *image  = gimp_item_get_image (GIMP_ITEM (drawable));
  const Babl *format = gimp_drawable_get_format (drawable);
966 967

  /*  first copy the image active channels  */
968
  gimp_image_get_active_array (image, active);
969 970

  if (gimp_drawable_has_alpha (drawable) && layer->lock_alpha)
971
    active[babl_format_get_n_components (format) - 1] = FALSE;
972 973
}

974
static gdouble
975
gimp_layer_get_opacity_at (GimpPickable *pickable,
976 977
                           gint          x,
                           gint          y)
978 979
{
  GimpLayer *layer = GIMP_LAYER (pickable);
980
  gdouble    value = GIMP_OPACITY_TRANSPARENT;
981

982 983
  if (x >= 0 && x < gimp_item_get_width  (GIMP_ITEM (layer)) &&
      y >= 0 && y < gimp_item_get_height (GIMP_ITEM (layer)) &&
984 985 986
      gimp_item_get_visible (GIMP_ITEM (layer)))
    {
      if (! gimp_drawable_has_alpha (GIMP_DRAWABLE (layer)))
987 988 989 990 991 992 993 994 995
        {
          value = GIMP_OPACITY_OPAQUE;
        }
      else
        {
          gegl_buffer_sample (gimp_drawable_get_buffer (GIMP_DRAWABLE (layer)),
                              x, y, NULL, &value, babl_format ("A double"),
                              GEGL_SAMPLER_NEAREST, GEGL_ABYSS_NONE);
        }
996 997

      if (layer->mask)
998
        {
999
          gdouble mask_value;
1000

1001 1002
          mask_value = gimp_pickable_get_opacity_at (GIMP_PICKABLE (layer->mask),
                                                     x, y);
1003

1004
          value *= mask_value;
1005
        }
1006 1007
    }

1008
  return value;
1009 1010
}

1011 1012 1013 1014 1015 1016 1017 1018
static void
gimp_layer_layer_mask_update (GimpDrawable *drawable,
                              gint          x,
                              gint          y,
                              gint          width,
                              gint          height,
                              GimpLayer    *layer)
{
1019 1020
  if (gimp_layer_get_apply_mask (layer) ||
      gimp_layer_get_show_mask (layer))
1021 1022 1023 1024 1025 1026 1027 1028
    {
      gimp_drawable_update (GIMP_DRAWABLE (layer),
                            x, y, width, height);
    }
}


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

1030
GimpLayer *
1031
gimp_layer_new (GimpImage            *image,
1032 1033
                gint                  width,
                gint                  height,
1034
                const Babl           *format,
1035 1036 1037
                const gchar          *name,
                gdouble               opacity,
                GimpLayerModeEffects  mode)
Elliot Lee's avatar
Elliot Lee committed
1038
{