gimplayer.c 69.3 KB
Newer Older
1
/* GIMP - The GNU Image Manipulation Program
2 3
 * Copyright (C) 1995 Spencer Kimball and Peter Mattis
 *
4
 * This program is free software: you can redistribute it and/or modify
5
 * it under the terms of the GNU General Public License as published by
6
 * the Free Software Foundation; either version 3 of the License, or
7 8 9 10 11 12 13 14
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
15
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
16 17 18 19 20 21 22
 */

#include "config.h"

#include <stdlib.h>
#include <string.h>

23
#include <gegl.h>
24

25
#include "libgimpbase/gimpbase.h"
26 27 28 29 30 31 32 33 34 35 36
#include "libgimpmath/gimpmath.h"

#include "core-types.h"

#include "base/boundary.h"
#include "base/pixel-region.h"
#include "base/tile-manager.h"
#include "base/tile.h"

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

37 38 39
#include "gegl/gimp-gegl-utils.h"

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

#include "gimp-intl.h"


enum
{
  OPACITY_CHANGED,
  MODE_CHANGED,
62
  LOCK_ALPHA_CHANGED,
63 64 65 66
  MASK_CHANGED,
  LAST_SIGNAL
};

67 68 69 70 71
enum
{
  PROP_0,
  PROP_OPACITY,
  PROP_MODE,
72 73 74
  PROP_LOCK_ALPHA,
  PROP_MASK,
  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 89 90 91 92 93 94 95 96 97 98 99
static void       gimp_layer_dispose            (GObject            *object);
static void       gimp_layer_finalize           (GObject            *object);

static void       gimp_layer_name_changed       (GimpObject         *object);
static gint64     gimp_layer_get_memsize        (GimpObject         *object,
                                                 gint64             *gui_size);

static void       gimp_layer_invalidate_preview (GimpViewable       *viewable);
static gchar    * gimp_layer_get_description    (GimpViewable       *viewable,
                                                 gchar             **tooltip);

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

166 167 168
static gint    gimp_layer_get_opacity_at        (GimpPickable       *pickable,
                                                 gint                x,
                                                 gint                y);
169

170
static void       gimp_layer_transform_color    (GimpImage          *image,
171 172 173 174
                                                 PixelRegion        *srcPR,
                                                 GimpImageType       src_type,
                                                 PixelRegion        *destPR,
                                                 GimpImageType       dest_type);
175 176 177 178 179 180 181 182

static void       gimp_layer_layer_mask_update  (GimpDrawable       *layer_mask,
                                                 gint                x,
                                                 gint                y,
                                                 gint                width,
                                                 gint                height,
                                                 GimpLayer          *layer);

183 184
static void       gimp_layer_sync_mode_node     (GimpLayer          *layer);

185

186 187
G_DEFINE_TYPE_WITH_CODE (GimpLayer, gimp_layer, GIMP_TYPE_DRAWABLE,
                         G_IMPLEMENT_INTERFACE (GIMP_TYPE_PICKABLE,
188
                                                gimp_layer_pickable_iface_init))
189

190
#define parent_class gimp_layer_parent_class
191

192
static guint layer_signals[LAST_SIGNAL] = { 0 };
193 194 195 196 197 198 199 200 201 202 203 204


static void
gimp_layer_class_init (GimpLayerClass *klass)
{
  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);

  layer_signals[OPACITY_CHANGED] =
205
    g_signal_new ("opacity-changed",
206 207 208 209 210 211
                  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);
212 213

  layer_signals[MODE_CHANGED] =
214
    g_signal_new ("mode-changed",
215 216 217 218 219 220
                  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);
221

222 223
  layer_signals[LOCK_ALPHA_CHANGED] =
    g_signal_new ("lock-alpha-changed",
224 225 226 227 228 229
                  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);
230 231

  layer_signals[MASK_CHANGED] =
232
    g_signal_new ("mask-changed",
233 234 235 236 237 238
                  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);
239

240 241
  object_class->set_property          = gimp_layer_set_property;
  object_class->get_property          = gimp_layer_get_property;
242 243 244 245 246 247 248 249 250 251 252
  object_class->dispose               = gimp_layer_dispose;
  object_class->finalize              = gimp_layer_finalize;

  gimp_object_class->name_changed     = gimp_layer_name_changed;
  gimp_object_class->get_memsize      = gimp_layer_get_memsize;

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

  item_class->removed                 = gimp_layer_removed;
253
  item_class->unset_removed           = gimp_layer_unset_removed;
254
  item_class->is_attached             = gimp_layer_is_attached;
255
  item_class->get_tree                = gimp_layer_get_tree;
256 257 258 259 260 261 262 263 264
  item_class->duplicate               = gimp_layer_duplicate;
  item_class->convert                 = gimp_layer_convert;
  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;
265 266
  item_class->to_selection            = gimp_layer_to_selection;
  item_class->get_node                = gimp_layer_get_node;
267
  item_class->default_name            = _("Layer");
268 269 270 271 272 273 274 275 276 277 278 279 280 281 282
  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");
  item_class->to_selection_desc       = C_("undo-type", "Alpha to Selection");
  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.");
283

284
  drawable_class->estimate_memsize      = gimp_layer_estimate_memsize;
285 286
  drawable_class->invalidate_boundary   = gimp_layer_invalidate_boundary;
  drawable_class->get_active_components = gimp_layer_get_active_components;
287 288
  drawable_class->convert_type          = gimp_layer_convert_type;
  drawable_class->project_region        = gimp_layer_project_region;
289 290 291

  klass->opacity_changed              = NULL;
  klass->mode_changed                 = NULL;
292
  klass->lock_alpha_changed           = NULL;
293
  klass->mask_changed                 = NULL;
294 295 296 297 298 299

  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,
300
                                                        GIMP_PARAM_READABLE));
301 302 303 304 305

  g_object_class_install_property (object_class, PROP_MODE,
                                   g_param_spec_enum ("mode", NULL, NULL,
                                                      GIMP_TYPE_LAYER_MODE_EFFECTS,
                                                      GIMP_NORMAL_MODE,
306
                                                      GIMP_PARAM_READABLE));
307 308 309 310 311

  g_object_class_install_property (object_class, PROP_LOCK_ALPHA,
                                   g_param_spec_boolean ("lock-alpha",
                                                         NULL, NULL,
                                                         FALSE,
312
                                                         GIMP_PARAM_READABLE));
313 314 315 316 317 318 319 320 321 322 323 324

  g_object_class_install_property (object_class, PROP_MASK,
                                   g_param_spec_object ("mask",
                                                        NULL, NULL,
                                                        GIMP_TYPE_LAYER_MASK,
                                                        GIMP_PARAM_READABLE));

  g_object_class_install_property (object_class, PROP_FLOATING_SELECTION,
                                   g_param_spec_boolean ("floating-selection",
                                                         NULL, NULL,
                                                         FALSE,
                                                         GIMP_PARAM_READABLE));
325 326 327 328 329
}

static void
gimp_layer_init (GimpLayer *layer)
{
330 331 332
  layer->opacity    = GIMP_OPACITY_OPAQUE;
  layer->mode       = GIMP_NORMAL_MODE;
  layer->lock_alpha = FALSE;
333

334
  layer->mask       = NULL;
335 336 337 338 339 340 341 342

  /*  floating selection  */
  layer->fs.drawable       = NULL;
  layer->fs.boundary_known = FALSE;
  layer->fs.segs           = NULL;
  layer->fs.num_segs       = 0;
}

343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373
static void
gimp_layer_pickable_iface_init (GimpPickableInterface *iface)
{
  iface->get_opacity_at = gimp_layer_get_opacity_at;
}

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:
374
      g_value_set_double (value, gimp_layer_get_opacity (layer));
375 376
      break;
    case PROP_MODE:
377
      g_value_set_enum (value, gimp_layer_get_mode (layer));
378 379
      break;
    case PROP_LOCK_ALPHA:
380 381 382 383 384 385 386
      g_value_set_boolean (value, gimp_layer_get_lock_alpha (layer));
      break;
    case PROP_MASK:
      g_value_set_object (value, gimp_layer_get_mask (layer));
      break;
    case PROP_FLOATING_SELECTION:
      g_value_set_boolean (value, gimp_layer_is_floating_sel (layer));
387
      break;
388

389 390 391 392 393 394
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
      break;
    }
}

395 396 397 398 399 400 401 402 403 404
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);

405 406 407 408 409 410 411 412 413 414 415
  if (gimp_layer_is_floating_sel (layer))
    {
      GimpDrawable *fs_drawable = gimp_layer_get_floating_sel_drawable (layer);

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

416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449
  G_OBJECT_CLASS (parent_class)->dispose (object);
}

static void
gimp_layer_finalize (GObject *object)
{
  GimpLayer *layer = GIMP_LAYER (object);

  if (layer->mask)
    {
      g_object_unref (layer->mask);
      layer->mask = NULL;
    }

  if (layer->fs.segs)
    {
      g_free (layer->fs.segs);
      layer->fs.segs     = NULL;
      layer->fs.num_segs = 0;
    }

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

static void
gimp_layer_name_changed (GimpObject *object)
{
  GimpLayer *layer = GIMP_LAYER (object);

  if (GIMP_OBJECT_CLASS (parent_class)->name_changed)
    GIMP_OBJECT_CLASS (parent_class)->name_changed (object);

  if (layer->mask)
    {
450 451
      gchar *mask_name = g_strdup_printf (_("%s mask"),
                                          gimp_object_get_name (object));
452

453
      gimp_object_take_name (GIMP_OBJECT (layer->mask), mask_name);
454 455 456 457 458 459 460 461 462 463
    }
}

static gint64
gimp_layer_get_memsize (GimpObject *object,
                        gint64     *gui_size)
{
  GimpLayer *layer   = GIMP_LAYER (object);
  gint64     memsize = 0;

464
  memsize += gimp_object_get_memsize (GIMP_OBJECT (layer->mask), gui_size);
465 466 467 468 469 470 471 472 473 474 475 476

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

  return memsize + GIMP_OBJECT_CLASS (parent_class)->get_memsize (object,
                                                                  gui_size);
}

static void
gimp_layer_invalidate_preview (GimpViewable *viewable)
{
  GimpLayer *layer = GIMP_LAYER (viewable);

477
  GIMP_VIEWABLE_CLASS (parent_class)->invalidate_preview (viewable);
478 479 480 481 482 483 484 485 486 487 488 489

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

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)"),
490
                              gimp_object_get_name (viewable));
491 492 493 494 495 496 497
    }

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

static void
498
gimp_layer_removed (GimpItem *item)
499
{
500
  GimpLayer *layer = GIMP_LAYER (item);
501

502 503
  if (layer->mask)
    gimp_item_removed (GIMP_ITEM (layer->mask));
504

505 506
  if (GIMP_ITEM_CLASS (parent_class)->removed)
    GIMP_ITEM_CLASS (parent_class)->removed (item);
507 508 509
}

static void
510
gimp_layer_unset_removed (GimpItem *item)
511
{
512
  GimpLayer *layer = GIMP_LAYER (item);
513

514 515
  if (layer->mask)
    gimp_item_unset_removed (GIMP_ITEM (layer->mask));
516

517 518
  if (GIMP_ITEM_CLASS (parent_class)->unset_removed)
    GIMP_ITEM_CLASS (parent_class)->unset_removed (item);
519 520
}

521 522
static gboolean
gimp_layer_is_attached (const GimpItem *item)
523
{
524
  GimpImage *image = gimp_item_get_image (item);
525

526 527 528
  return (GIMP_IS_IMAGE (image) &&
          gimp_container_have (gimp_image_get_layers (image),
                               GIMP_OBJECT (item)));
529 530
}

531 532
static GimpItemTree *
gimp_layer_get_tree (GimpItem *item)
533
{
534 535 536 537 538 539 540 541
  if (gimp_item_is_attached (item))
    {
      GimpImage *image = gimp_item_get_image (item);

      return gimp_image_get_layer_tree (image);
    }

  return NULL;
542 543 544 545
}

static GimpItem *
gimp_layer_duplicate (GimpItem *item,
546
                      GType     new_type)
547
{
548
  GimpItem *new_item;
549 550 551

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

552
  new_item = GIMP_ITEM_CLASS (parent_class)->duplicate (item, new_type);
553

554
  if (GIMP_IS_LAYER (new_item))
555
    {
556
      GimpLayer *layer     = GIMP_LAYER (item);
557
      GimpLayer *new_layer = GIMP_LAYER (new_item);
558

559 560 561 562 563 564
      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);
565 566 567 568

      /*  duplicate the layer mask if necessary  */
      if (layer->mask)
        {
569 570 571 572 573
          GimpItem *mask;

          mask = gimp_item_duplicate (GIMP_ITEM (layer->mask),
                                      G_TYPE_FROM_INSTANCE (layer->mask));
          gimp_layer_add_mask (new_layer, GIMP_LAYER_MASK (mask), FALSE, NULL);
574
        }
575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592
    }

  return new_item;
}

static void
gimp_layer_convert (GimpItem  *item,
                    GimpImage *dest_image)
{
  GimpLayer         *layer    = GIMP_LAYER (item);
  GimpDrawable      *drawable = GIMP_DRAWABLE (item);
  GimpImageBaseType  old_base_type;
  GimpImageBaseType  new_base_type;

  old_base_type = GIMP_IMAGE_TYPE_BASE_TYPE (gimp_drawable_type (drawable));
  new_base_type = gimp_image_base_type (dest_image);

  if (old_base_type != new_base_type)
593
    gimp_drawable_convert_type (drawable, dest_image, new_base_type, FALSE);
594 595 596 597 598 599 600 601

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

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

static gboolean
602 603 604 605
gimp_layer_rename (GimpItem     *item,
                   const gchar  *new_name,
                   const gchar  *undo_desc,
                   GError      **error)
606 607
{
  GimpLayer *layer = GIMP_LAYER (item);
608
  GimpImage *image = gimp_item_get_image (item);
609 610 611 612 613 614 615 616
  gboolean   attached;
  gboolean   floating_sel;

  attached     = gimp_item_is_attached (item);
  floating_sel = gimp_layer_is_floating_sel (layer);

  if (floating_sel)
    {
617
      if (GIMP_IS_CHANNEL (gimp_layer_get_floating_sel_drawable (layer)))
618
        {
619 620 621 622
          g_set_error_literal (error, GIMP_ERROR, GIMP_FAILED,
			       _("Cannot create a new layer from the floating "
				 "selection because it belongs to a layer mask "
				 "or channel."));
623 624
          return FALSE;
        }
625 626 627

      if (attached)
        {
628
          gimp_image_undo_group_start (image,
629 630 631
                                       GIMP_UNDO_GROUP_ITEM_PROPERTIES,
                                       undo_desc);

632
          floating_sel_to_layer (layer, NULL);
633 634 635
        }
    }

636
  GIMP_ITEM_CLASS (parent_class)->rename (item, new_name, undo_desc, error);
637 638

  if (attached && floating_sel)
639
    gimp_image_undo_group_end (image);
640 641 642 643 644 645

  return TRUE;
}

static void
gimp_layer_translate (GimpItem *item,
646 647
                      gint      offset_x,
                      gint      offset_y,
648 649 650 651 652 653 654 655
                      gboolean  push_undo)
{
  GimpLayer *layer = GIMP_LAYER (item);

  if (push_undo)
    gimp_image_undo_push_item_displace (gimp_item_get_image (item), NULL, item);

  /*  update the old region  */
656 657
  gimp_drawable_update (GIMP_DRAWABLE (layer),
                        0, 0,
658 659
                        gimp_item_get_width  (item),
                        gimp_item_get_height (item));
660 661 662 663 664 665 666 667

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

  GIMP_ITEM_CLASS (parent_class)->translate (item, offset_x, offset_y,
                                             push_undo);

  /*  update the new region  */
668 669
  gimp_drawable_update (GIMP_DRAWABLE (layer),
                        0, 0,
670 671
                        gimp_item_get_width  (item),
                        gimp_item_get_height (item));
672 673 674

  if (layer->mask)
    {
675 676 677 678
      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);
679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694

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

static void
gimp_layer_scale (GimpItem              *item,
                  gint                   new_width,
                  gint                   new_height,
                  gint                   new_offset_x,
                  gint                   new_offset_y,
                  GimpInterpolationType  interpolation_type,
                  GimpProgress          *progress)
{
  GimpLayer *layer = GIMP_LAYER (item);

695
  /* scale mask first, see bug 733839 */
696 697 698 699 700
  if (layer->mask)
    gimp_item_scale (GIMP_ITEM (layer->mask),
                     new_width, new_height,
                     new_offset_x, new_offset_y,
                     interpolation_type, progress);
701 702 703 704

  GIMP_ITEM_CLASS (parent_class)->scale (item, new_width, new_height,
                                         new_offset_x, new_offset_y,
                                         interpolation_type, progress);
705 706 707 708 709
}

static void
gimp_layer_resize (GimpItem    *item,
                   GimpContext *context,
710 711 712 713
                   gint         new_width,
                   gint         new_height,
                   gint         offset_x,
                   gint         offset_y)
714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767
{
  GimpLayer *layer  = GIMP_LAYER (item);

  GIMP_ITEM_CLASS (parent_class)->resize (item, context, new_width, new_height,
                                          offset_x, offset_y);

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

static void
gimp_layer_flip (GimpItem            *item,
                 GimpContext         *context,
                 GimpOrientationType  flip_type,
                 gdouble              axis,
                 gboolean             clip_result)
{
  GimpLayer *layer = GIMP_LAYER (item);

  GIMP_ITEM_CLASS (parent_class)->flip (item, context, flip_type, axis,
                                        clip_result);

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

static void
gimp_layer_rotate (GimpItem         *item,
                   GimpContext      *context,
                   GimpRotationType  rotate_type,
                   gdouble           center_x,
                   gdouble           center_y,
                   gboolean          clip_result)
{
  GimpLayer *layer = GIMP_LAYER (item);

  GIMP_ITEM_CLASS (parent_class)->rotate (item, context,
                                          rotate_type, center_x, center_y,
                                          clip_result);

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

static void
gimp_layer_transform (GimpItem               *item,
                      GimpContext            *context,
                      const GimpMatrix3      *matrix,
                      GimpTransformDirection  direction,
                      GimpInterpolationType   interpolation_type,
                      gint                    recursion_level,
768
                      GimpTransformResize     clip_result,
769 770 771 772
                      GimpProgress           *progress)
{
  GimpLayer *layer = GIMP_LAYER (item);

773
  /* FIXME: make interpolated transformations work on layers without alpha */
774 775 776 777
  if (interpolation_type != GIMP_INTERPOLATION_NONE &&
      ! gimp_drawable_has_alpha (GIMP_DRAWABLE (item)))
    gimp_layer_add_alpha (layer);

778 779
  GIMP_ITEM_CLASS (parent_class)->transform (item, context, matrix, direction,
                                             interpolation_type,
780 781 782
                                             recursion_level,
                                             clip_result,
                                             progress);
783 784 785 786

  if (layer->mask)
    gimp_item_transform (GIMP_ITEM (layer->mask), context,
                         matrix, direction,
787
                         interpolation_type, recursion_level,
788 789 790
                         clip_result, progress);
}

791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 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
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);
}

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;
  gboolean      source_node_hijacked = FALSE;

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

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

  g_warn_if_fail (layer->opacity_node == NULL);

  layer->opacity_node = gegl_node_new_child (node,
                                             "operation", "gegl:opacity",
                                             "value",     layer->opacity,
                                             NULL);

  if (! source_node_hijacked)
    gegl_node_connect_to (source,              "output",
                          layer->opacity_node, "input");

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

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

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

  offset_node = gimp_item_get_offset_node (GIMP_ITEM (layer));

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

  gimp_layer_sync_mode_node (layer);

  mode_node = gimp_drawable_get_mode_node (drawable);

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

  return node;
}

869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885
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);
}

886 887 888 889
static void
gimp_layer_invalidate_boundary (GimpDrawable *drawable)
{
  GimpLayer   *layer = GIMP_LAYER (drawable);
890
  GimpImage   *image;
891 892
  GimpChannel *mask;

893
  if (! (image = gimp_item_get_image (GIMP_ITEM (layer))))
894 895 896
    return;

  /*  Turn the current selection off  */
897
  gimp_image_selection_invalidate (image);
898 899

  /*  get the selection mask channel  */
900
  mask = gimp_image_get_mask (image);
901 902 903 904 905 906 907 908 909 910 911 912

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

913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980
static void
gimp_layer_get_active_components (const GimpDrawable *drawable,
                                  gboolean           *active)
{
  GimpLayer *layer = GIMP_LAYER (drawable);
  GimpImage *image = gimp_item_get_image (GIMP_ITEM (drawable));

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

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

static void
gimp_layer_convert_type (GimpDrawable      *drawable,
                         GimpImage         *dest_image,
                         GimpImageBaseType  new_base_type,
                         gboolean           push_undo)
{
  switch (new_base_type)
    {
    case GIMP_RGB:
    case GIMP_GRAY:
      GIMP_DRAWABLE_CLASS (parent_class)->convert_type (drawable, dest_image,
                                                        new_base_type,
                                                        push_undo);
      break;

    case GIMP_INDEXED:
      {
        GimpItem      *item = GIMP_ITEM (drawable);
        TileManager   *new_tiles;
        GimpImageType  new_type;
        PixelRegion    layerPR;
        PixelRegion    newPR;

        new_type = GIMP_IMAGE_TYPE_FROM_BASE_TYPE (new_base_type);

        if (gimp_drawable_has_alpha (drawable))
          new_type = GIMP_IMAGE_TYPE_WITH_ALPHA (new_type);

        new_tiles = tile_manager_new (gimp_item_get_width  (item),
                                      gimp_item_get_height (item),
                                      GIMP_IMAGE_TYPE_BYTES (new_type));

        pixel_region_init (&layerPR, gimp_drawable_get_tiles (drawable),
                           0, 0,
                           gimp_item_get_width  (item),
                           gimp_item_get_height (item),
                           FALSE);
        pixel_region_init (&newPR, new_tiles,
                           0, 0,
                           gimp_item_get_width  (item),
                           gimp_item_get_height (item),
                           TRUE);

        gimp_layer_transform_color (dest_image,
                                    &layerPR, gimp_drawable_type (drawable),
                                    &newPR,   new_type);

        gimp_drawable_set_tiles (drawable, push_undo, NULL,
                                 new_tiles, new_type);
        tile_manager_unref (new_tiles);
      }
    }
}

981 982
static gint
gimp_layer_get_opacity_at (GimpPickable *pickable,
983 984
                           gint          x,
                           gint          y)
985 986 987 988
{
  GimpLayer *layer = GIMP_LAYER (pickable);
  gint       val   = 0;

989 990
  if (x >= 0 && x < gimp_item_get_width  (GIMP_ITEM (layer)) &&
      y >= 0 && y < gimp_item_get_height (GIMP_ITEM (layer)) &&
991
      gimp_item_is_visible (GIMP_ITEM (layer)))
992
    {
993 994
      GimpLayerMask *mask;

995
      if (! gimp_drawable_has_alpha (GIMP_DRAWABLE (layer)))
996 997 998 999 1000 1001
        {
          val = OPAQUE_OPACITY;
        }
      else
        {
          Tile *tile;
1002

1003 1004
          tile = tile_manager_get_tile (gimp_drawable_get_tiles (GIMP_DRAWABLE (layer)),
                                        x, y, TRUE, FALSE);
1005

1006 1007
          val = * ((const guchar *) tile_data_pointer (tile, x, y) +
                   tile_bpp (tile) - 1);
1008

1009 1010 1011 1012 1013
          tile_release (tile, FALSE);
        }

      if ((mask = gimp_layer_get_mask (layer)) &&
          gimp_layer_mask_get_apply (mask))
1014 1015
        {
          gint mask_val;
1016

1017
          mask_val = gimp_pickable_get_opacity_at (GIMP_PICKABLE (mask),
1018 1019
                                                   x, y);

1020 1021
          val = val * mask_val / 255;
        }
1022 1023 1024 1025 1026 1027

    }

  return val;
}

1028
static void
1029
gimp_layer_transform_color (GimpImage     *image,
1030 1031 1032 1033
                            PixelRegion   *srcPR,
                            GimpImageType  src_type,
                            PixelRegion   *destPR,
                            GimpImageType  dest_type)
1034
{
1035 1036 1037
  GimpImageBaseType base_type  = GIMP_IMAGE_TYPE_BASE_TYPE (src_type);
  gboolean          src_alpha  = GIMP_IMAGE_TYPE_HAS_ALPHA (src_type);
  gboolean          dest_alpha = GIMP_IMAGE_TYPE_HAS_ALPHA (dest_type);
1038
  gpointer          pr;
1039

1040
  for (pr = pixel_regions_register (2, srcPR, destPR);
1041 1042 1043
       pr != NULL;
       pr = pixel_regions_process (pr))
    {
1044 1045 1046
      const guchar *src  = srcPR->data;
      guchar       *dest = destPR->data;
      gint          h    = destPR->h;
1047 1048

      while (h--)
1049
        {
1050 1051
          const guchar *s = src;
          guchar       *d = dest;
1052
          gint          i;
1053

1054
          for (i = 0; i < destPR->w; i++)
1055
            {
1056
              gimp_image_transform_color (image, dest_type, d, base_type, s);
1057

1058 1059 1060
              if (dest_alpha)
                d[destPR->bytes - 1] = (src_alpha ?
                                        s[srcPR->bytes - 1] : OPAQUE_OPACITY);
1061

1062 1063
              s += srcPR->bytes;
              d += destPR->bytes;
1064
            }
1065

1066 1067
          src  += srcPR->rowstride;
          dest += destPR->rowstride;
1068
        }
1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081
    }
}

static void
gimp_layer_layer_mask_update (GimpDrawable *drawable,
                              gint          x,
                              gint          y,
                              gint          width,
                              gint          height,
                              GimpLayer    *layer)
{
  GimpLayerMask *layer_mask = GIMP_LAYER_MASK (drawable);

1082 1083
  if (gimp_layer_mask_get_apply (layer_mask) ||
      gimp_layer_mask_get_show (layer_mask))
1084 1085 1086 1087 1088 1089
    {
      gimp_drawable_update (GIMP_DRAWABLE (layer),
                            x, y, width, height);
    }
}

1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141
static void
gimp_layer_sync_mode_node (GimpLayer *layer)
{
  if (layer->opacity_node)
    {
      GeglNode *mode_node;

      mode_node = gimp_drawable_get_mode_node (GIMP_DRAWABLE (layer));

      switch (layer->mode)
        {
        case GIMP_DISSOLVE_MODE:
        case GIMP_BEHIND_MODE:
        case GIMP_MULTIPLY_MODE:
        case GIMP_SCREEN_MODE:
        case GIMP_OVERLAY_MODE:
        case GIMP_DIFFERENCE_MODE:
        case GIMP_ADDITION_MODE:
        case GIMP_SUBTRACT_MODE:
        case GIMP_DARKEN_ONLY_MODE:
        case GIMP_LIGHTEN_ONLY_MODE:
        case GIMP_HUE_MODE:
        case GIMP_SATURATION_MODE:
        case GIMP_COLOR_MODE:
        case GIMP_VALUE_MODE:
        case GIMP_DIVIDE_MODE:
        case GIMP_DODGE_MODE:
        case GIMP_BURN_MODE:
        case GIMP_HARDLIGHT_MODE:
        case GIMP_SOFTLIGHT_MODE:
        case GIMP_GRAIN_EXTRACT_MODE:
        case GIMP_GRAIN_MERGE_MODE:
        case GIMP_COLOR_ERASE_MODE:
        case GIMP_ERASE_MODE:
        case GIMP_REPLACE_MODE:
        case GIMP_ANTI_ERASE_MODE:
          gegl_node_set (mode_node,
                         "operation",  "gimp:point-layer-mode",
                         "blend-mode", layer->mode,
                         NULL);
          break;

        default:
          gegl_node_set (mode_node,
                         "operation",
                         gimp_layer_mode_to_gegl_operation (layer->mode),
                         NULL);
          break;
        }
    }
}

1142 1143 1144 1145

/*  public functions  */

GimpLayer *
1146 1147 1148 1149 1150 1151 1152
gimp_layer_new (GimpImage            *image,
                gint                  width,
                gint                  height,
                GimpImageType         type,
                const gchar          *name,
                gdouble               opacity,
                GimpLayerModeEffects  mode)
1153 1154 1155
{
  GimpLayer *layer;

1156
  g_return_val_if_fail (GIMP_IS_IMAGE (image), NULL);
1157 1158 1159
  g_return_val_if_fail (width > 0, NULL);
  g_return_val_if_fail (height > 0, NULL);

1160 1161 1162 1163
  layer = GIMP_LAYER (gimp_drawable_new (GIMP_TYPE_LAYER,
                                         image, name,
                                         0, 0, width, height,
                                         type));
1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174

  opacity = CLAMP (opacity, GIMP_OPACITY_TRANSPARENT, GIMP_OPACITY_OPAQUE);

  layer->opacity = opacity;
  layer->mode    = mode;

  return layer;
}

/**
 * gimp_layer_new_from_tiles:
1175
 * @tiles:      The buffer to make the new layer from.
1176
 * @dest_image: The image the new layer will be added to.
1177 1178 1179 1180
 * @type:       The #GimpImageType of the new layer.
 * @name:       The new layer's name.
 * @opacity:    The new layer's opacity.
 * @mode:       The new layer's mode.
1181 1182 1183 1184 1185 1186 1187 1188 1189
 *
 * Copies %tiles to a layer taking into consideration the
 * possibility of transforming the contents to meet the requirements
 * of the target image type
 *
 * Return value: The new layer.
 **/
GimpLayer *
gimp_layer_new_from_tiles (TileManager          *tiles,
1190
                           GimpImage            *dest_image,
1191
                           GimpImageType         type,
1192 1193 1194
                           const gchar          *name,
                           gdouble               opacity,
                           GimpLayerModeEffects  mode)
1195 1196 1197 1198
{
  PixelRegion bufPR;

  g_return_val_if_fail (tiles != NULL, NULL);
1199
  g_return_val_if_fail (GIMP_IS_IMAGE (dest_image), NULL);
1200 1201

  pixel_region_init (&bufPR, tiles,
1202
                     0, 0,
1203 1204 1205 1206
                     tile_manager_width (tiles),
                     tile_manager_height (tiles),
                     FALSE);

1207
  return gimp_layer_new_from_region (&bufPR, dest_image, type,
1208 1209 1210 1211 1212
                                     name, opacity, mode);
}

/**
 * gimp_layer_new_from_pixbuf:
1213
 * @pixbuf:     The pixbuf to make the new layer from.
1214
 * @dest_image: The image the new layer will be added to.
1215 1216 1217 1218
 * @type:       The #GimpImageType of the new layer.
 * @name:       The new layer's name.
 * @opacity:    The new layer's opacity.
 * @mode:       The new layer's mode.
1219 1220 1221 1222 1223 1224 1225 1226 1227
 *
 * Copies %pixbuf to a layer taking into consideration the
 * possibility of transforming the contents to meet the requirements
 * of the target image type
 *
 * Return value: The new layer.
 **/
GimpLayer *
gimp_layer_new_from_pixbuf (GdkPixbuf            *pixbuf,
1228
                            GimpImage            *dest_image,
1229 1230 1231 1232 1233 1234 1235 1236
                            GimpImageType         type,
                            const gchar          *name,
                            gdouble               opacity,
                            GimpLayerModeEffects  mode)
{
  PixelRegion bufPR = { 0, };

  g_return_val_if_fail (GDK_IS_PIXBUF (pixbuf), NULL);
1237
  g_return_val_if_fail (GIMP_IS_IMAGE (dest_image), NULL);
1238 1239
  g_return_val_if_fail (GIMP_IMAGE_TYPE_BASE_TYPE (type) ==
                        gimp_image_base_type (dest_image), NULL);
1240 1241 1242 1243 1244 1245 1246 1247

  pixel_region_init_data (&bufPR, gdk_pixbuf_get_pixels (pixbuf),
                          gdk_pixbuf_get_n_channels (pixbuf),
                          gdk_pixbuf_get_rowstride (pixbuf),
                          0, 0,
                          gdk_pixbuf_get_width (pixbuf),
                          gdk_pixbuf_get_height (pixbuf));

1248
  return gimp_layer_new_from_region (&bufPR, dest_image, type,
1249 1250 1251 1252 1253
                                     name, opacity, mode);
}

/**
 * gimp_layer_new_from_region:
1254
 * @region:     A readable pixel region.
1255
 * @dest_image: The image the new layer will be added to.
1256 1257 1258 1259
 * @type:       The #GimpImageType of the new layer.
 * @name:       The new layer's name.
 * @opacity:    The new layer's opacity.
 * @mode:       The new layer's mode.
1260 1261 1262 1263 1264 1265 1266 1267 1268
 *
 * Copies %region to a layer taking into consideration the
 * possibility of transforming the contents to meet the requirements
 * of the target image type
 *
 * Return value: The new layer.
 **/
GimpLayer *
gimp_layer_new_from_region (PixelRegion          *region,
1269
                            GimpImage            *dest_image,
1270 1271 1272 1273
                            GimpImageType         type,
                            const gchar          *name,
                            gdouble               opacity,
                            GimpLayerModeEffects  mode)
1274 1275 1276 1277 1278 1279 1280
{
  GimpLayer     *new_layer;
  PixelRegion    layerPR;
  GimpImageType  src_type;
  gint           width;
  gint           height;

1281
  g_return_val_if_fail (region != NULL, NULL);
1282
  g_return_val_if_fail (GIMP_IS_IMAGE (dest_image), NULL);
1283

1284 1285
  width  = region->w;
  height = region->h;
1286

1287
  switch (region->bytes)
1288 1289 1290 1291 1292 1293 1294 1295 1296 1297
    {
    case 1: src_type = GIMP_GRAY_IMAGE;  break;
    case 2: src_type = GIMP_GRAYA_IMAGE; break;
    case 3: src_type = GIMP_RGB_IMAGE;   break;
    case 4: src_type = GIMP_RGBA_IMAGE;  break;
    default:
      g_return_val_if_reached (NULL);
      break;
    }

1298 1299
  new_layer = gimp_layer_new (dest_image, width, height, type, name,
                              opacity, mode);
1300 1301 1302

  if (! new_layer)
    {
1303
      g_warning ("%s: could not allocate new layer", G_STRFUNC);
1304 1305 1306
      return NULL;
    }

1307 1308
  pixel_region_init (&layerPR,
                     gimp_drawable_get_tiles (GIMP_DRAWABLE (new_layer)),
1309 1310
                     0, 0, width, height,
                     TRUE);
1311 1312 1313 1314 1315 1316 1317

  switch (type)
    {
    case GIMP_RGB_IMAGE:
      switch (src_type)
        {
       case GIMP_RGB_IMAGE:
1318
          copy_region (region, &layerPR);
1319
          break;
1320

1321 1322 1323 1324 1325 1326 1327 1328 1329 1330
        default:
          g_warning ("%s: unhandled type conversion", G_STRFUNC);
          break;
         }
      break;

    case GIMP_RGBA_IMAGE:
      switch (src_type)
        {
        case GIMP_RGBA_IMAGE:
1331
          copy_region (region, &layerPR);
1332
          break;
1333

1334
        case GIMP_RGB_IMAGE:
1335
          add_alpha_region (region, &layerPR);
1336
          break;
1337

1338
        case GIMP_GRAY_IMAGE:
1339
        case GIMP_GRAYA_IMAGE:
1340 1341 1342
          gimp_layer_transform_color (dest_image,
                                      region,   src_type,
                                      &layerPR, type);
1343
          break;
1344

1345 1346 1347 1348 1349 1350 1351 1352 1353 1354
        default:
          g_warning ("%s: unhandled type conversion", G_STRFUNC);
          break;
        }
      break;

    case GIMP_GRAY_IMAGE:
      switch (src_type)
        {
        case GIMP_GRAY_IMAGE:
1355
          copy_region (region, &layerPR);
1356
          break;
1357

1358 1359 1360 1361 1362 1363 1364 1365 1366
        default:
          g_warning ("%s: unhandled type conversion", G_STRFUNC);
          break;
        }
      break;

    case GIMP_GRAYA_IMAGE:
      switch (src_type)
        {
1367
        case GIMP_RGB_IMAGE:
1368
        case GIMP_RGBA_IMAGE:
1369 1370 1371
          gimp_layer_transform_color (dest_image,
                                      region,   src_type,
                                      &layerPR, type);
1372
          break;
1373

1374
        case GIMP_GRAYA_IMAGE:
1375
          copy_region (region, &layerPR);
1376
          break;
1377

1378
        case GIMP_GRAY_IMAGE:
1379
          add_alpha_region (region, &layerPR);
1380
          break;
1381

1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394
        default:
          g_warning ("%s: unhandled type conversion", G_STRFUNC);
          break;
        }
      break;

    case GIMP_INDEXED_IMAGE:
      g_warning ("%s: unhandled type conversion", G_STRFUNC);
      break;

    case GIMP_INDEXEDA_IMAGE:
      switch (src_type)
        {
1395
        case GIMP_RGB_IMAGE:
1396
        case GIMP_RGBA_IMAGE:
1397
        case GIMP_GRAY_IMAGE:
1398
        case GIMP_GRAYA_IMAGE:
1399 1400 1401
          gimp_layer_transform_color (dest_image,
                                      region,   src_type,
                                      &layerPR, type);
1402
          break;
1403

1404 1405 1406 1407 1408 1409 1410 1411 1412 1413
        default:
          g_warning ("%s: unhandled type conversion", G_STRFUNC);
          break;
        }
      break;
    }

  return new_layer;
}

1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429
GimpLayer *
gimp_layer_get_parent (GimpLayer *layer)
{
  g_return_val_if_fail (GIMP_IS_LAYER (layer), NULL);

  return GIMP_LAYER (gimp_viewable_get_parent (GIMP_VIEWABLE (layer)));
}

GimpLayerMask *
gimp_layer_get_mask (const GimpLayer *layer)
{
  g_return_val_if_fail (GIMP_IS_LAYER (layer), NULL);

  return layer->mask;
}

1430
GimpLayerMask *
1431 1432 1433 1434
gimp_layer_add_mask (GimpLayer      *layer,
                     GimpLayerMask  *mask,
                     gboolean        push_undo,
                     GError        **error)
1435
{
1436
  GimpImage *image;
1437 1438 1439

  g_return_val_if_fail (GIMP_IS_LAYER (layer), NULL);
  g_return_val_if_fail (GIMP_IS_LAYER_MASK (mask), NULL);
1440 1441
  g_return_val_if_fail (gimp_item_get_image (GIMP_ITEM (layer)) ==
                        gimp_item_get_image (GIMP_ITEM (mask)), NULL);
1442
  g_return_val_if_fail (error == NULL || *error == NULL, NULL);
1443 1444 1445 1446

  if (! gimp_item_is_attached (GIMP_ITEM (layer)))
    push_undo = FALSE;

1447
  image = gimp_item_get_image (GIMP_ITEM (layer));
1448 1449 1450

  if (layer->mask)
    {
1451 1452 1453
      g_set_error_literal (error, GIMP_ERROR, GIMP_FAILED,
			   _("Unable to add a layer mask since "
			     "the layer already has one."));
1454 1455 1456
      return NULL;
    }

1457 1458 1459 1460
  if ((gimp_item_get_width (GIMP_ITEM (layer)) !=
       gimp_item_get_width (GIMP_ITEM (mask))) ||
      (gimp_item_get_height (GIMP_ITEM (layer)) !=
       gimp_item_get_height (GIMP_ITEM (mask))))
1461
    {
1462 1463 1464
      g_set_error_literal (error, GIMP_ERROR, GIMP_FAILED,
			   _("Cannot add layer mask of different "
			     "dimensions than specified layer."));
1465 1466 1467 1468
      return NULL;
    }

  if (push_undo)
1469
    gimp_image_undo_push_layer_mask_add (image, C_("undo-type", "Add Layer Mask"),
1470 1471
                                         layer, mask);

1472
  layer->mask = g_object_ref_sink (mask);
1473 1474 1475

  gimp_layer_mask_set_layer (mask, layer);

1476 1477 1478 1479 1480 1481 1482 1483 1484 1485
  if (layer->opacity_node)
    {
      GeglNode *mask;

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

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

1486 1487
  if (gimp_layer_mask_get_apply (mask) ||
      gimp_layer_mask_get_show (mask))
1488 1489 1490
    {
      gimp_drawable_update (GIMP_DRAWABLE (layer),
                            0, 0,
1491 1492
                            gimp_item_get_width  (GIMP_ITEM (layer)),
                            gimp_item_get_height (GIMP_ITEM (layer)));
1493 1494 1495 1496 1497 1498 1499 1500
    }

  g_signal_connect (mask, "update",
                    G_CALLBACK (gimp_layer_layer_mask_update),
                    layer);

  g_signal_emit (layer, layer_signals[MASK_CHANGED], 0);

1501 1502 1503 1504 1505 1506
  g_object_notify (G_OBJECT (layer), "mask");

  /*  if the mask came from the undo stack, reset its "removed" state  */
  if (gimp_item_is_removed (GIMP_ITEM (mask)))
    gimp_item_unset_removed (GIMP_ITEM (mask));

1507 1508 1509 1510 1511
  return layer->mask;
}

GimpLayerMask *
gimp_layer_create_mask (const GimpLayer *layer,
1512 1513
                        GimpAddMaskType  add_mask_type,
                        GimpChannel     *channel)
1514 1515 1516 1517 1518 1519
{
  GimpDrawable  *drawable;
  GimpItem      *item;
  PixelRegion    srcPR;
  PixelRegion    destPR;
  GimpLayerMask *mask;
1520
  GimpImage     *image;
1521 1522 1523 1524
  gchar         *mask_name;
  GimpRGB        black = { 0.0, 0.0, 0.0, GIMP_OPACITY_OPAQUE };

  g_return_val_if_fail (GIMP_IS_LAYER (layer), NULL);
1525 1526
  g_return_val_if_fail (add_mask_type != GIMP_ADD_CHANNEL_MASK ||
                        GIMP_IS_CHANNEL (channel), NULL);
1527 1528 1529

  drawable = GIMP_DRAWABLE (layer);
  item     = GIMP_ITEM (layer);
1530
  image    = gimp_item_get_image (item);
1531 1532

  mask_name = g_strdup_printf (_("%s mask"),
1533
                               gimp_object_get_name (layer));
1534

1535
  mask = gimp_layer_mask_new (image,
1536 1537
                              gimp_item_get_width  (item),
                              gimp_item_get_height (item),
1538
                              mask_name, &black);
1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555

  g_free (mask_name);

  switch (add_mask_type)
    {
    case GIMP_ADD_WHITE_MASK:
      gimp_channel_all (GIMP_CHANNEL (mask), FALSE);
      return mask;

    case GIMP_ADD_BLACK_MASK:
      gimp_channel_clear (GIMP_CHANNEL (mask), NULL, FALSE);
      return mask;

    default:
      break;
    }

1556
  pixel_region_init (&destPR, gimp_drawable_get_tiles (GIMP_DRAWABLE (mask)),
1557
                     0, 0,
1558 1559
                     gimp_item_get_width  (GIMP_ITEM (mask)),
                     gimp_item_get_height (GIMP_ITEM (mask)),
1560
                     TRUE);
1561 1562 1563 1564 1565 1566 1567 1568 1569 1570

  switch (add_mask_type)
    {
    case GIMP_ADD_WHITE_MASK:
    case GIMP_ADD_BLACK_MASK:
      break;

    case GIMP_ADD_ALPHA_MASK:
    case GIMP_ADD_ALPHA_TRANSFER_MASK:
      if (gimp_drawable_has_alpha (drawable))
1571
        {
1572
          pixel_region_init (&srcPR, gimp_drawable_get_tiles (drawable),
1573
                             0, 0,
1574 1575
                             gimp_item_get_width  (item),
                             gimp_item_get_height (item),
1576
                             FALSE);
1577

1578
          extract_alpha_region (&srcPR, NULL, &destPR);
1579 1580 1581 1582 1583 1584 1585

          if (add_mask_type == GIMP_ADD_ALPHA_TRANSFER_MASK)
            {
              void   *pr;
              gint    w, h;
              guchar *alpha_ptr;

1586
              gimp_drawable_push_undo (drawable,
1587
                                       C_("undo-type", "Transfer Alpha to Mask"),
1588
                                       0, 0,
1589 1590
                                       gimp_item_get_width  (item),
                                       gimp_item_get_height (item),
1591 1592
                                       NULL, FALSE);

1593
              pixel_region_init (&srcPR, gimp_drawable_get_tiles (drawable),
1594
                                 0, 0,
1595 1596
                                 gimp_item_get_width  (item),
                                 gimp_item_get_height (item),
1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618
                                 TRUE);

              for (pr = pixel_regions_register (1, &srcPR);
                   pr != NULL;
                   pr = pixel_regions_process (pr))
                {
                  h = srcPR.h;

                  while (h--)
                    {
                      w = srcPR.w;
                      alpha_ptr = (srcPR.data + h * srcPR.rowstride +
                                   srcPR.bytes - 1);

                      while (w--)
                        {
                          *alpha_ptr = OPAQUE_OPACITY;
                          alpha_ptr += srcPR.bytes;
                        }
                    }
                }
            }
1619
        }
1620 1621 1622
      break;

    case GIMP_ADD_SELECTION_MASK:
1623
    case GIMP_ADD_CHANNEL_MASK:
1624
      {
1625
        gboolean channel_empty;
1626
        gint     offset_x, offset_y;
1627 1628 1629 1630 1631
        gint     copy_x, copy_y;
        gint     copy_width, copy_height;

        if (add_mask_type == GIMP_ADD_SELECTION_MASK)
          channel = GIMP_CHANNEL (gimp_image_get_mask (image));
1632

1633
        channel_empty = gimp_channel_is_empty (channel);
1634

1635 1636
        gimp_item_get_offset (item, &offset_x, &offset_y);

1637 1638 1639
        gimp_rectangle_intersect (0, 0,
                                  gimp_image_get_width  (image),
                                  gimp_image_get_height (image),
1640 1641 1642
                                  offset_x, offset_y,
                                  gimp_item_get_width  (item),
                                  gimp_item_get_height (item),
1643 1644
                                  &copy_x, &copy_y,
                                  &copy_width, &copy_height);
1645

1646 1647
        if (copy_width  < gimp_item_get_width  (item) ||
            copy_height < gimp_item_get_height (item) ||
1648
            channel_empty)
1649 1650
          gimp_channel_clear (GIMP_CHANNEL (mask), NULL, FALSE);

1651
        if ((copy_width || copy_height) && ! channel_empty)
1652
          {
1653 1654
            pixel_region_init (&srcPR,
                               gimp_drawable_get_tiles (GIMP_DRAWABLE (channel)),
1655 1656 1657
                               copy_x, copy_y,
                               copy_width, copy_height,
                               FALSE);
1658 1659
            pixel_region_init (&destPR,
                               gimp_drawable_get_tiles (GIMP_DRAWABLE (mask)),
1660