gimplayer.c 102 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 <https://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
#include <gdk-pixbuf/gdk-pixbuf.h>
26

27
#include "libgimpbase/gimpbase.h"
28
#include "libgimpcolor/gimpcolor.h"
29
#include "libgimpconfig/gimpconfig.h"
30 31
#include "libgimpmath/gimpmath.h"

32
#include "core-types.h"
33

34 35
#include "operations/layer-modes/gimp-layer-modes.h"

36
#include "gegl/gimp-babl.h"
37
#include "gegl/gimp-gegl-apply-operation.h"
38
#include "gegl/gimp-gegl-loops.h"
39
#include "gegl/gimp-gegl-nodes.h"
40

41
#include "gimpboundary.h"
42
#include "gimpchannel-select.h"
43
#include "gimpcontext.h"
44
#include "gimpcontainer.h"
45
#include "gimpdrawable-floating-selection.h"
46
#include "gimperror.h"
47
#include "gimpgrouplayer.h"
48
#include "gimpimage-undo-push.h"
49 50
#include "gimpimage-undo.h"
#include "gimpimage.h"
51
#include "gimpimage-color-profile.h"
52
#include "gimplayer-floating-selection.h"
53
#include "gimplayer.h"
54
#include "gimplayermask.h"
55
#include "gimpmarshal.h"
56
#include "gimpobjectqueue.h"
57
#include "gimppickable.h"
58
#include "gimpprogress.h"
59

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


63 64
enum
{
65 66
  OPACITY_CHANGED,
  MODE_CHANGED,
67 68 69
  BLEND_SPACE_CHANGED,
  COMPOSITE_SPACE_CHANGED,
  COMPOSITE_MODE_CHANGED,
70
  EFFECTIVE_MODE_CHANGED,
71
  EXCLUDES_BACKDROP_CHANGED,
72
  LOCK_ALPHA_CHANGED,
73
  MASK_CHANGED,
74 75 76
  APPLY_MASK_CHANGED,
  EDIT_MASK_CHANGED,
  SHOW_MASK_CHANGED,
77 78 79
  LAST_SIGNAL
};

80 81 82 83 84
enum
{
  PROP_0,
  PROP_OPACITY,
  PROP_MODE,
85 86 87
  PROP_BLEND_SPACE,
  PROP_COMPOSITE_SPACE,
  PROP_COMPOSITE_MODE,
88
  PROP_EXCLUDES_BACKDROP,
89
  PROP_LOCK_ALPHA,
90
  PROP_MASK,
91
  PROP_FLOATING_SELECTION
92 93
};

94

95 96
static void       gimp_color_managed_iface_init (GimpColorManagedInterface *iface);
static void       gimp_pickable_iface_init      (GimpPickableInterface     *iface);
97

98 99 100 101 102 103 104 105
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);
106
static void       gimp_layer_dispose            (GObject            *object);
107
static void       gimp_layer_finalize           (GObject            *object);
108 109
static void       gimp_layer_notify             (GObject            *object,
                                                 GParamSpec         *pspec);
110

111
static void       gimp_layer_name_changed       (GimpObject         *object);
112 113
static gint64     gimp_layer_get_memsize        (GimpObject         *object,
                                                 gint64             *gui_size);
114

115
static void       gimp_layer_invalidate_preview (GimpViewable       *viewable);
116 117
static gchar    * gimp_layer_get_description    (GimpViewable       *viewable,
                                                 gchar             **tooltip);
118

119 120
static GeglNode * gimp_layer_get_node           (GimpFilter         *filter);

121
static void       gimp_layer_removed            (GimpItem           *item);
122
static void       gimp_layer_unset_removed      (GimpItem           *item);
123
static gboolean   gimp_layer_is_attached        (GimpItem           *item);
124
static GimpItemTree * gimp_layer_get_tree       (GimpItem           *item);
125
static GimpItem * gimp_layer_duplicate          (GimpItem           *item,
126
                                                 GType               new_type);
127
static void       gimp_layer_convert            (GimpItem           *item,
128 129
                                                 GimpImage          *dest_image,
                                                 GType               old_type);
130
static gboolean   gimp_layer_rename             (GimpItem           *item,
131
                                                 const gchar        *new_name,
132 133
                                                 const gchar        *undo_desc,
                                                 GError            **error);
134 135 136 137
static void       gimp_layer_start_move         (GimpItem           *item,
                                                 gboolean            push_undo);
static void       gimp_layer_end_move           (GimpItem           *item,
                                                 gboolean            push_undo);
138
static void       gimp_layer_translate          (GimpItem           *item,
139 140
                                                 gdouble             offset_x,
                                                 gdouble             offset_y,
141
                                                 gboolean            push_undo);
142 143 144 145 146
static void       gimp_layer_scale              (GimpItem           *item,
                                                 gint                new_width,
                                                 gint                new_height,
                                                 gint                new_offset_x,
                                                 gint                new_offset_y,
147
                                                 GimpInterpolationType  interp_type,
148
                                                 GimpProgress       *progress);
149
static void       gimp_layer_resize             (GimpItem           *item,
150
                                                 GimpContext        *context,
151
                                                 GimpFillType        fill_type,
152 153 154 155
                                                 gint                new_width,
                                                 gint                new_height,
                                                 gint                offset_x,
                                                 gint                offset_y);
156
static void       gimp_layer_flip               (GimpItem           *item,
157
                                                 GimpContext        *context,
158
                                                 GimpOrientationType flip_type,
159 160
                                                 gdouble             axis,
                                                 gboolean            clip_result);
161
static void       gimp_layer_rotate             (GimpItem           *item,
162
                                                 GimpContext        *context,
163 164 165 166
                                                 GimpRotationType    rotate_type,
                                                 gdouble             center_x,
                                                 gdouble             center_y,
                                                 gboolean            clip_result);
167
static void       gimp_layer_transform          (GimpItem           *item,
168
                                                 GimpContext        *context,
169
                                                 const GimpMatrix3  *matrix,
170 171
                                                 GimpTransformDirection direction,
                                                 GimpInterpolationType  interpolation_type,
172
                                                 GimpTransformResize clip_result,
173
                                                 GimpProgress       *progress);
174 175 176 177 178 179
static void       gimp_layer_to_selection       (GimpItem           *item,
                                                 GimpChannelOps      op,
                                                 gboolean            antialias,
                                                 gboolean            feather,
                                                 gdouble             feather_radius_x,
                                                 gdouble             feather_radius_y);
180

181
static void       gimp_layer_alpha_changed      (GimpDrawable       *drawable);
182
static gint64     gimp_layer_estimate_memsize   (GimpDrawable       *drawable,
183
                                                 GimpComponentType   component_type,
184 185
                                                 gint                width,
                                                 gint                height);
186
static void       gimp_layer_convert_type       (GimpDrawable       *drawable,
187
                                                 GimpImage          *dest_image,
188
                                                 const Babl         *new_format,
189
                                                 GimpColorProfile   *src_profile,
190
                                                 GimpColorProfile   *dest_profile,
191 192
                                                 GeglDitherMethod    layer_dither_type,
                                                 GeglDitherMethod    mask_dither_type,
193 194
                                                 gboolean            push_undo,
                                                 GimpProgress       *progress);
195
static void    gimp_layer_invalidate_boundary   (GimpDrawable       *drawable);
196
static void    gimp_layer_get_active_components (GimpDrawable       *drawable,
197
                                                 gboolean           *active);
198
static GimpComponentMask
199
               gimp_layer_get_active_mask       (GimpDrawable       *drawable);
200 201 202 203 204 205
static void    gimp_layer_set_buffer            (GimpDrawable       *drawable,
                                                 gboolean            push_undo,
                                                 const gchar        *undo_desc,
                                                 GeglBuffer         *buffer,
                                                 gint                offset_x,
                                                 gint                offset_y);
206

207 208 209
static GimpColorProfile *
               gimp_layer_get_color_profile     (GimpColorManaged   *managed);

210
static gdouble gimp_layer_get_opacity_at        (GimpPickable       *pickable,
211 212
                                                 gint                x,
                                                 gint                y);
213 214 215 216
static void    gimp_layer_pixel_to_srgb         (GimpPickable       *pickable,
                                                 const Babl         *format,
                                                 gpointer            pixel,
                                                 GimpRGB            *color);
217 218 219 220
static void    gimp_layer_srgb_to_pixel         (GimpPickable       *pickable,
                                                 const GimpRGB      *color,
                                                 const Babl         *format,
                                                 gpointer            pixel);
221

222 223 224 225 226 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 255 256 257 258 259
static void       gimp_layer_real_translate     (GimpLayer          *layer,
                                                 gint                offset_x,
                                                 gint                offset_y);
static void       gimp_layer_real_scale         (GimpLayer          *layer,
                                                 gint                new_width,
                                                 gint                new_height,
                                                 gint                new_offset_x,
                                                 gint                new_offset_y,
                                                 GimpInterpolationType  interp_type,
                                                 GimpProgress       *progress);
static void       gimp_layer_real_resize        (GimpLayer          *layer,
                                                 GimpContext        *context,
                                                 GimpFillType        fill_type,
                                                 gint                new_width,
                                                 gint                new_height,
                                                 gint                offset_x,
                                                 gint                offset_y);
static void       gimp_layer_real_flip          (GimpLayer          *layer,
                                                 GimpContext        *context,
                                                 GimpOrientationType flip_type,
                                                 gdouble             axis,
                                                 gboolean            clip_result);
static void       gimp_layer_real_rotate        (GimpLayer          *layer,
                                                 GimpContext        *context,
                                                 GimpRotationType    rotate_type,
                                                 gdouble             center_x,
                                                 gdouble             center_y,
                                                 gboolean            clip_result);
static void       gimp_layer_real_transform     (GimpLayer          *layer,
                                                 GimpContext        *context,
                                                 const GimpMatrix3  *matrix,
                                                 GimpTransformDirection direction,
                                                 GimpInterpolationType  interpolation_type,
                                                 GimpTransformResize clip_result,
                                                 GimpProgress       *progress);
static void       gimp_layer_real_convert_type  (GimpLayer          *layer,
                                                 GimpImage          *dest_image,
                                                 const Babl         *new_format,
260
                                                 GimpColorProfile   *src_profile,
261 262 263 264 265
                                                 GimpColorProfile   *dest_profile,
                                                 GeglDitherMethod    layer_dither_type,
                                                 GeglDitherMethod    mask_dither_type,
                                                 gboolean            push_undo,
                                                 GimpProgress       *progress);
266 267 268 269 270
static void  gimp_layer_real_get_effective_mode (GimpLayer          *layer,
                                                 GimpLayerMode          *mode,
                                                 GimpLayerColorSpace    *blend_space,
                                                 GimpLayerColorSpace    *composite_space,
                                                 GimpLayerCompositeMode *composite_mode);
271 272 273
static gboolean
          gimp_layer_real_get_excludes_backdrop (GimpLayer          *layer);

274 275 276 277 278 279 280
static void       gimp_layer_layer_mask_update  (GimpDrawable       *layer_mask,
                                                 gint                x,
                                                 gint                y,
                                                 gint                width,
                                                 gint                height,
                                                 GimpLayer          *layer);

281

282
G_DEFINE_TYPE_WITH_CODE (GimpLayer, gimp_layer, GIMP_TYPE_DRAWABLE,
283 284
                         G_IMPLEMENT_INTERFACE (GIMP_TYPE_COLOR_MANAGED,
                                                gimp_color_managed_iface_init)
285
                         G_IMPLEMENT_INTERFACE (GIMP_TYPE_PICKABLE,
286
                                                gimp_pickable_iface_init))
287

288
#define parent_class gimp_layer_parent_class
289

290
static guint layer_signals[LAST_SIGNAL] = { 0 };
291

292 293

static void
294
gimp_layer_class_init (GimpLayerClass *klass)
295
{
296 297 298
  GObjectClass      *object_class      = G_OBJECT_CLASS (klass);
  GimpObjectClass   *gimp_object_class = GIMP_OBJECT_CLASS (klass);
  GimpViewableClass *viewable_class    = GIMP_VIEWABLE_CLASS (klass);
299
  GimpFilterClass   *filter_class      = GIMP_FILTER_CLASS (klass);
300 301
  GimpItemClass     *item_class        = GIMP_ITEM_CLASS (klass);
  GimpDrawableClass *drawable_class    = GIMP_DRAWABLE_CLASS (klass);
302

303
  layer_signals[OPACITY_CHANGED] =
304
    g_signal_new ("opacity-changed",
305 306 307 308 309 310
                  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);
311 312

  layer_signals[MODE_CHANGED] =
313
    g_signal_new ("mode-changed",
314 315 316 317 318 319
                  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);
320

321 322
  layer_signals[BLEND_SPACE_CHANGED] =
    g_signal_new ("blend-space-changed",
323 324
                  G_TYPE_FROM_CLASS (klass),
                  G_SIGNAL_RUN_FIRST,
325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343
                  G_STRUCT_OFFSET (GimpLayerClass, blend_space_changed),
                  NULL, NULL,
                  gimp_marshal_VOID__VOID,
                  G_TYPE_NONE, 0);

  layer_signals[COMPOSITE_SPACE_CHANGED] =
    g_signal_new ("composite-space-changed",
                  G_TYPE_FROM_CLASS (klass),
                  G_SIGNAL_RUN_FIRST,
                  G_STRUCT_OFFSET (GimpLayerClass, composite_space_changed),
                  NULL, NULL,
                  gimp_marshal_VOID__VOID,
                  G_TYPE_NONE, 0);

  layer_signals[COMPOSITE_MODE_CHANGED] =
    g_signal_new ("composite-mode-changed",
                  G_TYPE_FROM_CLASS (klass),
                  G_SIGNAL_RUN_FIRST,
                  G_STRUCT_OFFSET (GimpLayerClass, composite_mode_changed),
344 345 346 347
                  NULL, NULL,
                  gimp_marshal_VOID__VOID,
                  G_TYPE_NONE, 0);

348 349 350 351 352 353 354 355 356
  layer_signals[EFFECTIVE_MODE_CHANGED] =
    g_signal_new ("effective-mode-changed",
                  G_TYPE_FROM_CLASS (klass),
                  G_SIGNAL_RUN_FIRST,
                  G_STRUCT_OFFSET (GimpLayerClass, effective_mode_changed),
                  NULL, NULL,
                  gimp_marshal_VOID__VOID,
                  G_TYPE_NONE, 0);

357 358 359 360 361 362 363 364 365
  layer_signals[EXCLUDES_BACKDROP_CHANGED] =
    g_signal_new ("excludes-backdrop-changed",
                  G_TYPE_FROM_CLASS (klass),
                  G_SIGNAL_RUN_FIRST,
                  G_STRUCT_OFFSET (GimpLayerClass, excludes_backdrop_changed),
                  NULL, NULL,
                  gimp_marshal_VOID__VOID,
                  G_TYPE_NONE, 0);

366 367
  layer_signals[LOCK_ALPHA_CHANGED] =
    g_signal_new ("lock-alpha-changed",
368 369 370 371 372 373
                  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);
374

375
  layer_signals[MASK_CHANGED] =
376
    g_signal_new ("mask-changed",
377 378 379 380 381 382
                  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);
383

384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410
  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);

411 412
  object_class->set_property          = gimp_layer_set_property;
  object_class->get_property          = gimp_layer_get_property;
413
  object_class->dispose               = gimp_layer_dispose;
414
  object_class->finalize              = gimp_layer_finalize;
415
  object_class->notify                = gimp_layer_notify;
416

417
  gimp_object_class->name_changed     = gimp_layer_name_changed;
418 419
  gimp_object_class->get_memsize      = gimp_layer_get_memsize;

420
  viewable_class->default_icon_name   = "gimp-layer";
421
  viewable_class->invalidate_preview  = gimp_layer_invalidate_preview;
422
  viewable_class->get_description     = gimp_layer_get_description;
423

424 425
  filter_class->get_node              = gimp_layer_get_node;

426
  item_class->removed                 = gimp_layer_removed;
427
  item_class->unset_removed           = gimp_layer_unset_removed;
428
  item_class->is_attached             = gimp_layer_is_attached;
429
  item_class->get_tree                = gimp_layer_get_tree;
430
  item_class->duplicate               = gimp_layer_duplicate;
431
  item_class->convert                 = gimp_layer_convert;
432
  item_class->rename                  = gimp_layer_rename;
433 434
  item_class->start_move              = gimp_layer_start_move;
  item_class->end_move                = gimp_layer_end_move;
435 436 437 438 439 440
  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;
441
  item_class->to_selection            = gimp_layer_to_selection;
442
  item_class->default_name            = _("Layer");
443 444 445 446 447 448 449
  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");
450
  item_class->to_selection_desc       = C_("undo-type", "Alpha to Selection");
451 452 453 454 455 456 457
  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.");
458

459
  drawable_class->alpha_changed         = gimp_layer_alpha_changed;
460
  drawable_class->estimate_memsize      = gimp_layer_estimate_memsize;
461
  drawable_class->convert_type          = gimp_layer_convert_type;
462 463
  drawable_class->invalidate_boundary   = gimp_layer_invalidate_boundary;
  drawable_class->get_active_components = gimp_layer_get_active_components;
464
  drawable_class->get_active_mask       = gimp_layer_get_active_mask;
465
  drawable_class->set_buffer            = gimp_layer_set_buffer;
466 467 468

  klass->opacity_changed              = NULL;
  klass->mode_changed                 = NULL;
469 470 471
  klass->blend_space_changed          = NULL;
  klass->composite_space_changed      = NULL;
  klass->composite_mode_changed       = NULL;
472
  klass->excludes_backdrop_changed    = NULL;
473
  klass->lock_alpha_changed           = NULL;
474
  klass->mask_changed                 = NULL;
475 476 477
  klass->apply_mask_changed           = NULL;
  klass->edit_mask_changed            = NULL;
  klass->show_mask_changed            = NULL;
478 479 480 481 482 483 484
  klass->translate                    = gimp_layer_real_translate;
  klass->scale                        = gimp_layer_real_scale;
  klass->resize                       = gimp_layer_real_resize;
  klass->flip                         = gimp_layer_real_flip;
  klass->rotate                       = gimp_layer_real_rotate;
  klass->transform                    = gimp_layer_real_transform;
  klass->convert_type                 = gimp_layer_real_convert_type;
485
  klass->get_effective_mode           = gimp_layer_real_get_effective_mode;
486
  klass->get_excludes_backdrop        = gimp_layer_real_get_excludes_backdrop;
487 488 489 490 491 492

  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,
493
                                                        GIMP_PARAM_READABLE));
494 495 496

  g_object_class_install_property (object_class, PROP_MODE,
                                   g_param_spec_enum ("mode", NULL, NULL,
497
                                                      GIMP_TYPE_LAYER_MODE,
498
                                                      GIMP_LAYER_MODE_NORMAL,
499
                                                      GIMP_PARAM_READABLE));
500

501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517
  g_object_class_install_property (object_class, PROP_BLEND_SPACE,
                                   g_param_spec_enum ("blend-space",
                                                      NULL, NULL,
                                                      GIMP_TYPE_LAYER_COLOR_SPACE,
                                                      GIMP_LAYER_COLOR_SPACE_AUTO,
                                                      GIMP_PARAM_READABLE));

  g_object_class_install_property (object_class, PROP_COMPOSITE_SPACE,
                                   g_param_spec_enum ("composite-space",
                                                      NULL, NULL,
                                                      GIMP_TYPE_LAYER_COLOR_SPACE,
                                                      GIMP_LAYER_COLOR_SPACE_AUTO,
                                                      GIMP_PARAM_READABLE));

  g_object_class_install_property (object_class, PROP_COMPOSITE_MODE,
                                   g_param_spec_enum ("composite-mode",
                                                      NULL, NULL,
518 519 520 521
                                                      GIMP_TYPE_LAYER_COMPOSITE_MODE,
                                                      GIMP_LAYER_COMPOSITE_AUTO,
                                                      GIMP_PARAM_READABLE));

522 523 524 525 526 527
  g_object_class_install_property (object_class, PROP_EXCLUDES_BACKDROP,
                                   g_param_spec_boolean ("excludes-backdrop",
                                                         NULL, NULL,
                                                         FALSE,
                                                         GIMP_PARAM_READABLE));

528 529 530 531
  g_object_class_install_property (object_class, PROP_LOCK_ALPHA,
                                   g_param_spec_boolean ("lock-alpha",
                                                         NULL, NULL,
                                                         FALSE,
532
                                                         GIMP_PARAM_READABLE));
533

534 535 536 537 538 539
  g_object_class_install_property (object_class, PROP_MASK,
                                   g_param_spec_object ("mask",
                                                        NULL, NULL,
                                                        GIMP_TYPE_LAYER_MASK,
                                                        GIMP_PARAM_READABLE));

540 541 542 543 544
  g_object_class_install_property (object_class, PROP_FLOATING_SELECTION,
                                   g_param_spec_boolean ("floating-selection",
                                                         NULL, NULL,
                                                         FALSE,
                                                         GIMP_PARAM_READABLE));
545 546 547 548 549
}

static void
gimp_layer_init (GimpLayer *layer)
{
550 551 552 553 554 555 556 557 558 559 560
  layer->opacity                   = GIMP_OPACITY_OPAQUE;
  layer->mode                      = GIMP_LAYER_MODE_NORMAL;
  layer->blend_space               = GIMP_LAYER_COLOR_SPACE_AUTO;
  layer->composite_space           = GIMP_LAYER_COLOR_SPACE_AUTO;
  layer->composite_mode            = GIMP_LAYER_COMPOSITE_AUTO;
  layer->effective_mode            = layer->mode;
  layer->effective_blend_space     = gimp_layer_get_real_blend_space (layer);
  layer->effective_composite_space = gimp_layer_get_real_composite_space (layer);
  layer->effective_composite_mode  = gimp_layer_get_real_composite_mode (layer);
  layer->excludes_backdrop         = FALSE;
  layer->lock_alpha                = FALSE;
561

562
  layer->mask       = NULL;
563 564 565
  layer->apply_mask = TRUE;
  layer->edit_mask  = TRUE;
  layer->show_mask  = FALSE;
566

567 568 569 570 571
  /*  floating selection  */
  layer->fs.drawable       = NULL;
  layer->fs.boundary_known = FALSE;
  layer->fs.segs           = NULL;
  layer->fs.num_segs       = 0;
572 573
}

574
static void
575 576 577 578 579 580 581
gimp_color_managed_iface_init (GimpColorManagedInterface *iface)
{
  iface->get_color_profile = gimp_layer_get_color_profile;
}

static void
gimp_pickable_iface_init (GimpPickableInterface *iface)
582
{
583
  iface->get_opacity_at = gimp_layer_get_opacity_at;
584
  iface->pixel_to_srgb  = gimp_layer_pixel_to_srgb;
585
  iface->srgb_to_pixel  = gimp_layer_srgb_to_pixel;
586 587
}

588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612
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:
613
      g_value_set_double (value, gimp_layer_get_opacity (layer));
614 615
      break;
    case PROP_MODE:
616
      g_value_set_enum (value, gimp_layer_get_mode (layer));
617
      break;
618 619 620 621 622 623 624 625
    case PROP_BLEND_SPACE:
      g_value_set_enum (value, gimp_layer_get_blend_space (layer));
      break;
    case PROP_COMPOSITE_SPACE:
      g_value_set_enum (value, gimp_layer_get_composite_space (layer));
      break;
    case PROP_COMPOSITE_MODE:
      g_value_set_enum (value, gimp_layer_get_composite_mode (layer));
626
      break;
627 628 629
    case PROP_EXCLUDES_BACKDROP:
      g_value_set_boolean (value, gimp_layer_get_excludes_backdrop (layer));
      break;
630
    case PROP_LOCK_ALPHA:
631 632
      g_value_set_boolean (value, gimp_layer_get_lock_alpha (layer));
      break;
633 634 635
    case PROP_MASK:
      g_value_set_object (value, gimp_layer_get_mask (layer));
      break;
636 637
    case PROP_FLOATING_SELECTION:
      g_value_set_boolean (value, gimp_layer_is_floating_sel (layer));
638
      break;
639

640 641 642 643 644 645
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
      break;
    }
}

646 647 648 649 650 651 652 653 654 655
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);

656 657 658 659
  if (gimp_layer_is_floating_sel (layer))
    {
      GimpDrawable *fs_drawable = gimp_layer_get_floating_sel_drawable (layer);

660
      /* only detach if this is actually the drawable's fs because the
luz.paz's avatar
luz.paz committed
661
       * layer might be on the undo stack and not attached to anything
662 663 664
       */
      if (gimp_drawable_get_floating_sel (fs_drawable) == layer)
        gimp_drawable_detach_floating_sel (fs_drawable);
665 666

      gimp_layer_set_floating_sel_drawable (layer, NULL);
667 668
    }

669 670 671
  G_OBJECT_CLASS (parent_class)->dispose (object);
}

672 673 674
static void
gimp_layer_finalize (GObject *object)
{
675
  GimpLayer *layer = GIMP_LAYER (object);
676

677
  g_clear_object (&layer->mask);
678 679 680 681

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

682 683
static void
gimp_layer_update_mode_node (GimpLayer *layer)
684
{
685 686
  GeglNode               *mode_node;
  GimpLayerMode           visible_mode;
687 688 689
  GimpLayerColorSpace     visible_blend_space;
  GimpLayerColorSpace     visible_composite_space;
  GimpLayerCompositeMode  visible_composite_mode;
690

691 692
  mode_node = gimp_drawable_get_mode_node (GIMP_DRAWABLE (layer));

693
  if (layer->mask && layer->show_mask)
694
    {
695
      visible_mode            = GIMP_LAYER_MODE_NORMAL;
696
      visible_blend_space     = GIMP_LAYER_COLOR_SPACE_AUTO;
697
      visible_composite_space = GIMP_LAYER_COLOR_SPACE_AUTO;
698
      visible_composite_mode  = GIMP_LAYER_COMPOSITE_AUTO;
699 700 701 702 703 704

      /* This makes sure that masks of LEGACY-mode layers are
       * composited in PERCEPTUAL space, and non-LEGACY layers in
       * LINEAR space, or whatever composite space was chosen in the
       * layer attributes dialog
       */
705
      visible_composite_space = gimp_layer_get_real_composite_space (layer);
706 707 708
    }
  else
    {
709 710 711 712
      visible_mode            = layer->effective_mode;
      visible_blend_space     = layer->effective_blend_space;
      visible_composite_space = layer->effective_composite_space;
      visible_composite_mode  = layer->effective_composite_mode;
713 714
    }

715 716 717 718 719
  gimp_gegl_mode_node_set_mode (mode_node,
                                visible_mode,
                                visible_blend_space,
                                visible_composite_space,
                                visible_composite_mode);
720
  gimp_gegl_mode_node_set_opacity (mode_node, layer->opacity);
721 722 723 724 725 726 727
}

static void
gimp_layer_notify (GObject    *object,
                   GParamSpec *pspec)
{
  if (! strcmp (pspec->name, "is-last-node") &&
728
      gimp_filter_peek_node (GIMP_FILTER (object)))
729
    {
730
      gimp_layer_update_mode_node (GIMP_LAYER (object));
731

732
      gimp_drawable_update (GIMP_DRAWABLE (object), 0, 0, -1, -1);
733 734 735
    }
}

736 737 738 739 740
static void
gimp_layer_name_changed (GimpObject *object)
{
  GimpLayer *layer = GIMP_LAYER (object);

741 742
  if (GIMP_OBJECT_CLASS (parent_class)->name_changed)
    GIMP_OBJECT_CLASS (parent_class)->name_changed (object);
743 744 745

  if (layer->mask)
    {
746 747
      gchar *mask_name = g_strdup_printf (_("%s mask"),
                                          gimp_object_get_name (object));
748

749
      gimp_object_take_name (GIMP_OBJECT (layer->mask), mask_name);
750 751 752
    }
}

753
static gint64
754
gimp_layer_get_memsize (GimpObject *object,
755
                        gint64     *gui_size)
756
{
757
  GimpLayer *layer   = GIMP_LAYER (object);
758
  gint64     memsize = 0;
759

760
  memsize += gimp_object_get_memsize (GIMP_OBJECT (layer->mask), gui_size);
761

762
  *gui_size += layer->fs.num_segs * sizeof (GimpBoundSeg);
763

764 765
  return memsize + GIMP_OBJECT_CLASS (parent_class)->get_memsize (object,
                                                                  gui_size);
766 767
}

Elliot Lee's avatar
Elliot Lee committed
768
static void
769
gimp_layer_invalidate_preview (GimpViewable *viewable)
770
{
771
  GimpLayer *layer = GIMP_LAYER (viewable);
772

773
  GIMP_VIEWABLE_CLASS (parent_class)->invalidate_preview (viewable);
774

775
  if (gimp_layer_is_floating_sel (layer))
776
    floating_sel_invalidate (layer);
777 778
}

779 780 781 782 783 784 785
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)"),
786
                              gimp_object_get_name (viewable));
787 788 789 790 791 792
    }

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

793 794 795 796 797 798
static GeglNode *
gimp_layer_get_node (GimpFilter *filter)
{
  GimpDrawable *drawable = GIMP_DRAWABLE (filter);
  GimpLayer    *layer    = GIMP_LAYER (filter);
  GeglNode     *node;
799
  GeglNode     *input;
800 801 802 803 804 805
  GeglNode     *source;
  GeglNode     *mode_node;
  gboolean      source_node_hijacked = FALSE;

  node = GIMP_FILTER_CLASS (parent_class)->get_node (filter);

806 807
  input = gegl_node_get_input_proxy (node, "input");

808 809 810 811 812 813 814 815 816 817 818 819
  source = gimp_drawable_get_source_node (drawable);

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

820 821 822
  gegl_node_connect_to (input,  "output",
                        source, "input");

823 824 825 826 827 828 829
  g_warn_if_fail (layer->layer_offset_node == NULL);
  g_warn_if_fail (layer->mask_offset_node == NULL);

  /* the mode node connects it all, and has aux and aux2 inputs for
   * the layer and its mask
   */
  mode_node = gimp_drawable_get_mode_node (drawable);
830
  gimp_layer_update_mode_node (layer);
831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879

  /* the layer's offset node */
  layer->layer_offset_node = gegl_node_new_child (node,
                                                  "operation", "gegl:translate",
                                                  NULL);
  gimp_item_add_offset_node (GIMP_ITEM (layer), layer->layer_offset_node);

  /* the layer mask's offset node */
  layer->mask_offset_node = gegl_node_new_child (node,
                                                 "operation", "gegl:translate",
                                                  NULL);
  gimp_item_add_offset_node (GIMP_ITEM (layer), layer->mask_offset_node);

  if (! source_node_hijacked)
    {
      gegl_node_connect_to (source,                   "output",
                            layer->layer_offset_node, "input");
    }

  if (! (layer->mask && gimp_layer_get_show_mask (layer)))
    {
      gegl_node_connect_to (layer->layer_offset_node, "output",
                            mode_node,                "aux");
    }

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

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

      gegl_node_connect_to (mask,                    "output",
                            layer->mask_offset_node, "input");

      if (gimp_layer_get_show_mask (layer))
        {
          gegl_node_connect_to (layer->mask_offset_node, "output",
                                mode_node,               "aux");
        }
      else if (gimp_layer_get_apply_mask (layer))
        {
          gegl_node_connect_to (layer->mask_offset_node, "output",
                                mode_node,               "aux2");
        }
    }

  return node;
}

880 881 882 883 884 885 886 887 888 889 890 891
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);
}

892 893 894 895 896 897 898 899 900 901 902 903
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);
}

904
static gboolean
905
gimp_layer_is_attached (GimpItem *item)
906
{
907 908 909 910
  GimpImage *image = gimp_item_get_image (item);

  return (GIMP_IS_IMAGE (image) &&
          gimp_container_have (gimp_image_get_layers (image),
911
                               GIMP_OBJECT (item)));
912 913
}

914 915
static GimpItemTree *
gimp_layer_get_tree (GimpItem *item)
916 917 918 919 920
{
  if (gimp_item_is_attached (item))
    {
      GimpImage *image = gimp_item_get_image (item);

921
      return gimp_image_get_layer_tree (image);
922 923 924 925 926
    }

  return NULL;
}

927 928
static GimpItem *
gimp_layer_duplicate (GimpItem *item,
929
                      GType     new_type)
930
{
931
  GimpItem *new_item;
932 933 934

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

935
  new_item = GIMP_ITEM_CLASS (parent_class)->duplicate (item, new_type);
936

937
  if (GIMP_IS_LAYER (new_item))
938
    {
939
      GimpLayer *layer     = GIMP_LAYER (item);
940 941
      GimpLayer *new_layer = GIMP_LAYER (new_item);

942 943 944 945 946 947 948 949 950 951
      gimp_layer_set_mode            (new_layer,
                                      gimp_layer_get_mode (layer), FALSE);
      gimp_layer_set_blend_space     (new_layer,
                                      gimp_layer_get_blend_space (layer), FALSE);
      gimp_layer_set_composite_space (new_layer,
                                      gimp_layer_get_composite_space (layer), FALSE);
      gimp_layer_set_composite_mode  (new_layer,
                                      gimp_layer_get_composite_mode (layer), FALSE);
      gimp_layer_set_opacity         (new_layer,
                                      gimp_layer_get_opacity (layer), FALSE);
952 953 954 955

      if (gimp_layer_can_lock_alpha (new_layer))
        gimp_layer_set_lock_alpha (new_layer,
                                   gimp_layer_get_lock_alpha (layer), FALSE);
956 957 958 959

      /*  duplicate the layer mask if necessary  */
      if (layer->mask)
        {
960 961 962 963
          GimpItem *mask;

          mask = gimp_item_duplicate (GIMP_ITEM (layer->mask),
                                      G_TYPE_FROM_INSTANCE (layer->mask));
964
          gimp_layer_add_mask (new_layer, GIMP_LAYER_MASK (mask), FALSE, NULL);
965 966 967 968

          new_layer->apply_mask = layer->apply_mask;
          new_layer->edit_mask  = layer->edit_mask;
          new_layer->show_mask  = layer->show_mask;
969
        }
970 971 972 973 974
    }

  return new_item;
}

975
static void
976
gimp_layer_convert (GimpItem  *item,
977 978
                    GimpImage *dest_image,
                    GType      old_type)
979
{
980 981 982
  GimpLayer         *layer    = GIMP_LAYER (item);
  GimpDrawable      *drawable = GIMP_DRAWABLE (item);
  GimpImageBaseType  old_base_type;
983
  GimpImageBaseType  new_base_type;
984 985
  GimpPrecision      old_precision;
  GimpPrecision      new_precision;
986
  GimpColorProfile  *src_profile  = NULL;
987
  GimpColorProfile  *dest_profile = NULL;
988

989
  old_base_type = gimp_drawable_get_base_type (drawable);
990
  new_base_type = gimp_image_get_base_type (dest_image);
991

992 993 994
  old_precision = gimp_drawable_get_precision (drawable);
  new_precision = gimp_image_get_precision (dest_image);

995
  if (g_type_is_a (old_type, GIMP_TYPE_LAYER))
996
    {
997
      src_profile =
998 999
        gimp_color_managed_get_color_profile (GIMP_COLOR_MANAGED (item));

1000 1001
      dest_profile =
        gimp_color_managed_get_color_profile (GIMP_COLOR_MANAGED (dest_image));
1002 1003

      if (gimp_color_profile_is_equal (dest_profile, src_profile))
1004 1005 1006 1007
        {
          src_profile  = NULL;
          dest_profile = NULL;
        }
1008
    }
1009

1010
  if (old_base_type != new_base_type ||
1011
      old_precision != new_precision ||
1012
      dest_profile)
1013 1014
    {
      gimp_drawable_convert_type (drawable, dest_image,
1015 1016 1017
                                  new_base_type,
                                  new_precision,
                                  gimp_drawable_has_alpha (drawable),
1018
                                  src_profile,
1019
                                  dest_profile,
1020
                                  GEGL_DITHER_NONE, GEGL_DITHER_NONE,
1021
                                  FALSE, NULL);
1022
    }
1023

1024 1025 1026
  if (layer->mask)
    gimp_item_set_image (GIMP_ITEM (layer->mask), dest_image);

1027
  GIMP_ITEM_CLASS (parent_class)->convert (item, dest_image, old_type);
1028 1029
}

1030
static gboolean
1031 1032 1033 1034
gimp_layer_rename (GimpItem     *item,
                   const gchar  *new_name,
                   const gchar  *undo_desc,
                   GError      **error)
1035
{
1036
  GimpLayer *layer = GIMP_LAYER (item);
1037
  GimpImage *image = gimp_item_get_image (item);
1038 1039
  gboolean   attached;
  gboolean   floating_sel;
1040

1041 1042 1043 1044
  attached     = gimp_item_is_attached (item);
  floating_sel = gimp_layer_is_floating_sel (layer);

  if (floating_sel)
1045
    {
1046
      if (GIMP_IS_CHANNEL (gimp_layer_get_floating_sel_drawable (layer)))
1047
        {
1048
          g_set_error_literal (error, GIMP_ERROR, GIMP_FAILED,
1049 1050 1051
                               _("Cannot create a new layer from the floating "
                                 "selection because it belongs to a layer mask "
                                 "or channel."));
1052 1053
          return FALSE;
        }
1054 1055 1056

      if (attached)
        {
1057
          gimp_image_undo_group_start (image,
1058 1059
                                       GIMP_UNDO_GROUP_ITEM_PROPERTIES,
                                       undo_desc);
1060

1061
          floating_sel_to_layer (layer, NULL);
1062
        }
1063 1064
    }

1065
  GIMP_ITEM_CLASS (parent_class)->rename (item, new_name, undo_desc, error);
1066

1067
  if (attached && floating_sel)
1068
    gimp_image_undo_group_end (image);
1069 1070

  return TRUE;
1071 1072
}

1073 1074 1075 1076
static void
gimp_layer_start_move (GimpItem *item,
                       gboolean  push_undo)
{
1077 1078 1079
  GimpLayer *layer     = GIMP_LAYER (item);
  GimpLayer *ancestor  = layer;
  GSList    *ancestors = NULL;
1080 1081

  /* suspend mask cropping for all of the layer's ancestors */
1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096
  while ((ancestor = gimp_layer_get_parent (ancestor)))
    {
      gimp_group_layer_suspend_mask (GIMP_GROUP_LAYER (ancestor), push_undo);

      ancestors = g_slist_prepend (ancestors, g_object_ref (ancestor));
    }

  /* we keep the ancestor list around, so that we can resume mask cropping for
   * the same set of groups in gimp_layer_end_move().  note that
   * gimp_image_remove_layer() calls start_move() before removing the layer,
   * while it's still part of the layer tree, and end_move() afterwards, when
   * it's no longer part of the layer tree, and hence we can't use get_parent()
   * in end_move() to get the same set of ancestors.
   */
  layer->move_stack = g_slist_prepend (layer->move_stack, ancestors);
1097 1098 1099 1100 1101 1102 1103 1104 1105 1106

  if (GIMP_ITEM_CLASS (parent_class)->start_move)
    GIMP_ITEM_CLASS (parent_class)->start_move (item, push_undo);
}

static void
gimp_layer_end_move (GimpItem *item,
                     gboolean  push_undo)
{
  GimpLayer *layer = GIMP_LAYER (item);
1107 1108 1109 1110
  GSList    *ancestors;
  GSList    *iter;

  g_return_if_fail (layer->move_stack != NULL);
1111 1112 1113 1114

  if (GIMP_ITEM_CLASS (parent_class)->end_move)
    GIMP_ITEM_CLASS (parent_class)->end_move (item, push_undo);

1115
  ancestors = layer->move_stack->data;
1116

1117
  layer->move_stack = g_slist_remove (layer->move_stack, ancestors);
1118

1119 1120 1121 1122
  /* resume mask cropping for all of the layer's ancestors */
  for (iter = ancestors; iter; iter = g_slist_next (iter))
    {
      GimpGroupLayer *ancestor = iter->data;
1123

1124
      gimp_group_layer_resume_mask (ancestor, push_undo);
1125

1126
      g_object_unref (ancestor);
1127
    }
1128 1129

  g_slist_free (ancestors);
1130 1131
}

1132 1133
static void
gimp_layer_translate (GimpItem *item,
1134 1135
                      gdouble   offset_x,
                      gdouble   offset_y,
1136
                      gboolean  push_undo)
1137
{
1138
  GimpLayer *layer = GIMP_LAYER (item);
1139

1140
  if (push_undo)
1141
    gimp_image_undo_push_item_displace (gimp_item_get_image (item), NULL, item);
1142

1143 1144 1145
  GIMP_LAYER_GET_CLASS (layer)->translate (layer,
                                           SIGNED_ROUND (offset_x),
                                           SIGNED_ROUND (offset_y));
1146

1147
  if (layer->mask)
1148
    {
1149 1150 1151 1152
      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);
1153 1154 1155 1156 1157

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

1158 1159 1160 1161 1162 1163
static void
gimp_layer_scale (GimpItem              *item,
                  gint                   new_width,
                  gint                   new_height,
                  gint                   new_offset_x,
                  gint                   new_offset_y,
1164
                  GimpInterpolationType  interpolation_type,
1165
                  GimpProgress          *progress)
1166
{
1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190
  GimpLayer       *layer = GIMP_LAYER (item);
  GimpObjectQueue *queue = NULL;

  if (progress && layer->mask)
    {
      GimpLayerMask *mask;

      queue    = gimp_object_queue_new (progress);
      progress = GIMP_PROGRESS (queue);

      /* temporarily set layer->mask to NULL, so that its size won't be counted
       * when pushing the layer to the queue.
       */
      mask        = layer->mask;
      layer->mask = NULL;

      gimp_object_queue_push (queue, layer);
      gimp_object_queue_push (queue, mask);

      layer->mask = mask;
    }

  if (queue)
    gimp_object_queue_pop (queue);
1191

1192 1193 1194
  GIMP_LAYER_GET_CLASS (layer)->scale (layer, new_width, new_height,
                                       new_offset_x, new_offset_y,
                                       interpolation_type, progress);
1195 1196

  if (layer->mask)
1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207
    {
      if (queue)
        gimp_object_queue_pop (queue);

      gimp_item_scale (GIMP_ITEM (layer->mask),
                       new_width, new_height,
                       new_offset_x, new_offset_y,
                       interpolation_type, progress);
    }

  g_clear_object (&queue);
1208 1209
}

1210
static void
1211 1212 1213 1214 1215 1216 1217
gimp_layer_resize (GimpItem     *item,
                   GimpContext  *context,
                   GimpFillType  fill_type,
                   gint          new_width,
                   gint          new_height,
                   gint          offset_x,
                   gint          offset_y)
1218
{
1219
  GimpLayer *layer  = GIMP_LAYER (item);
1220

1221 1222 1223
  GIMP_LAYER_GET_CLASS (layer)->resize (layer, context, fill_type,
                                        new_width, new_height,
                                        offset_x, offset_y);
1224 1225

  if (layer->mask)
1226
    gimp_item_resize (GIMP_ITEM (layer->mask), context, GIMP_FILL_TRANSPARENT,
1227
                      new_width, new_height, offset_x, offset_y);
1228 1229
}

1230 1231
static void
gimp_layer_flip (GimpItem            *item,
1232
                 GimpContext         *context,
1233
                 GimpOrientationType  flip_type,
1234 1235
                 gdouble              axis,
                 gboolean             clip_result)
1236
{
1237
  GimpLayer *layer = GIMP_LAYER (item);
1238

1239 1240
  GIMP_LAYER_GET_CLASS (layer)->flip (layer, context, flip_type, axis,
                                      clip_result);
1241 1242

  if (layer->mask)
1243
    gimp_item_flip (GIMP_ITEM (layer->mask), context,
1244
                    flip_type, axis, clip_result);
1245 1246
}

1247 1248
static void
gimp_layer_rotate (GimpItem         *item,
1249
                   GimpContext      *context,
1250 1251 1252 1253 1254
                   GimpRotationType  rotate_type,
                   gdouble           center_x,
                   gdouble           center_y,
                   gboolean          clip_result)
{
1255
  GimpLayer *layer = GIMP_LAYER (item);
1256

1257 1258 1259
  GIMP_LAYER_GET_CLASS (layer)->rotate (layer, context,
                                        rotate_type, center_x, center_y,
                                        clip_result);
1260 1261

  if (layer->mask)
1262