gimplayer.c 59.1 KB
Newer Older
Elliot Lee's avatar
Elliot Lee committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
/* The GIMP -- an image manipulation program
 * Copyright (C) 1995 Spencer Kimball and Peter Mattis
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (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
 * along with this program; if not, write to the Free Software
16
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
Elliot Lee's avatar
Elliot Lee committed
17
 */
18

19 20
#include "config.h"

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

24 25
#include <glib-object.h>

26 27
#include "libgimpmath/gimpmath.h"

Michael Natterer's avatar
Michael Natterer committed
28
#include "core-types.h"
29

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

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

37
#include "gimp-utils.h"
38
#include "gimpcontext.h"
39
#include "gimpcontainer.h"
40
#include "gimpdrawable-convert.h"
41
#include "gimpdrawable-invert.h"
42
#include "gimpimage-undo-push.h"
43 44
#include "gimpimage-undo.h"
#include "gimpimage.h"
Michael Natterer's avatar
Michael Natterer committed
45
#include "gimplayer-floating-sel.h"
46
#include "gimplayer.h"
47
#include "gimplayermask.h"
48
#include "gimpmarshal.h"
49
#include "gimppickable.h"
50

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


54 55
enum
{
56 57
  OPACITY_CHANGED,
  MODE_CHANGED,
58
  LOCK_ALPHA_CHANGED,
59 60 61 62
  MASK_CHANGED,
  LAST_SIGNAL
};

63 64 65 66 67 68 69 70
enum
{
  PROP_0,
  PROP_OPACITY,
  PROP_MODE,
  PROP_LOCK_ALPHA
};

71

72 73
static void       gimp_layer_class_init         (GimpLayerClass     *klass);
static void       gimp_layer_init               (GimpLayer          *layer);
74
static void       gimp_layer_pickable_iface_init (GimpPickableInterface *pickable_iface);
75

76 77 78 79 80 81 82 83
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);
84
static void       gimp_layer_dispose            (GObject            *object);
85
static void       gimp_layer_finalize           (GObject            *object);
86

87
static void       gimp_layer_name_changed       (GimpObject         *object);
88 89
static gint64     gimp_layer_get_memsize        (GimpObject         *object,
                                                 gint64             *gui_size);
90

91
static void       gimp_layer_invalidate_preview (GimpViewable       *viewable);
92 93
static gchar    * gimp_layer_get_description    (GimpViewable       *viewable,
                                                 gchar             **tooltip);
94

95
static void       gimp_layer_removed            (GimpItem           *item);
96
static gboolean   gimp_layer_is_attached        (GimpItem           *item);
97 98 99
static GimpItem * gimp_layer_duplicate          (GimpItem           *item,
                                                 GType               new_type,
                                                 gboolean            add_alpha);
100 101
static void       gimp_layer_convert            (GimpItem           *item,
                                                 GimpImage          *dest_image);
102
static gboolean   gimp_layer_rename             (GimpItem           *item,
103 104
                                                 const gchar        *new_name,
                                                 const gchar        *undo_desc);
105 106
static void       gimp_layer_translate          (GimpItem           *item,
                                                 gint                offset_x,
107 108
                                                 gint                offset_y,
                                                 gboolean            push_undo);
109 110 111 112 113
static void       gimp_layer_scale              (GimpItem           *item,
                                                 gint                new_width,
                                                 gint                new_height,
                                                 gint                new_offset_x,
                                                 gint                new_offset_y,
114
                                                 GimpInterpolationType  interp_type,
115
                                                 GimpProgress       *progress);
116
static void       gimp_layer_resize             (GimpItem           *item,
117
                                                 GimpContext        *context,
118 119 120 121
                                                 gint                new_width,
                                                 gint                new_height,
                                                 gint                offset_x,
                                                 gint                offset_y);
122
static void       gimp_layer_flip               (GimpItem           *item,
123
                                                 GimpContext        *context,
124
                                                 GimpOrientationType flip_type,
125 126
                                                 gdouble             axis,
                                                 gboolean            clip_result);
127
static void       gimp_layer_rotate             (GimpItem           *item,
128
                                                 GimpContext        *context,
129 130 131 132
                                                 GimpRotationType    rotate_type,
                                                 gdouble             center_x,
                                                 gdouble             center_y,
                                                 gboolean            clip_result);
133
static void       gimp_layer_transform          (GimpItem           *item,
134
                                                 GimpContext        *context,
135
                                                 const GimpMatrix3  *matrix,
136 137
                                                 GimpTransformDirection direction,
                                                 GimpInterpolationType  interpolation_type,
138 139
                                                 gboolean            supersample,
                                                 gint                recursion_level,
140
                                                 gboolean            clip_result,
141
                                                 GimpProgress       *progress);
142 143 144
static void    gimp_layer_invalidate_boundary   (GimpDrawable       *drawable);
static void    gimp_layer_get_active_components (const GimpDrawable *drawable,
                                                 gboolean           *active);
145 146 147 148
static void    gimp_layer_set_tiles             (GimpDrawable       *drawable,
                                                 gboolean            push_undo,
                                                 const gchar        *undo_desc,
                                                 TileManager        *tiles,
149 150 151
                                                 GimpImageType       type,
                                                 gint                offset_x,
                                                 gint                offset_y);
152 153 154
static gint    gimp_layer_get_opacity_at        (GimpPickable       *pickable,
                                                 gint                x,
                                                 gint                y);
155

156 157 158 159
static void       gimp_layer_transform_color    (GimpImage          *gimage,
                                                 PixelRegion        *layerPR,
                                                 PixelRegion        *bufPR,
                                                 GimpDrawable       *drawable,
160
                                                 GimpImageBaseType   src_type);
161

162 163 164 165 166 167 168
static void       gimp_layer_layer_mask_update  (GimpDrawable       *layer_mask,
                                                 gint                x,
                                                 gint                y,
                                                 gint                width,
                                                 gint                height,
                                                 GimpLayer          *layer);

169

170 171 172
static guint  layer_signals[LAST_SIGNAL] = { 0 };

static GimpDrawableClass *parent_class   = NULL;
173

174

175
GType
Manish Singh's avatar
Manish Singh committed
176
gimp_layer_get_type (void)
177
{
178
  static GType layer_type = 0;
179

180
  if (! layer_type)
181
    {
182
      static const GTypeInfo layer_info =
183
      {
184
        sizeof (GimpLayerClass),
185 186 187 188 189 190 191 192
        (GBaseInitFunc) NULL,
        (GBaseFinalizeFunc) NULL,
        (GClassInitFunc) gimp_layer_class_init,
        NULL,           /* class_finalize */
        NULL,           /* class_data     */
        sizeof (GimpLayer),
        0,              /* n_preallocs    */
        (GInstanceInitFunc) gimp_layer_init,
193 194
      };

195 196 197 198 199 200 201
      static const GInterfaceInfo pickable_iface_info =
      {
        (GInterfaceInitFunc) gimp_layer_pickable_iface_init,
        NULL,           /* iface_finalize */
        NULL            /* iface_data     */
      };

202
      layer_type = g_type_register_static (GIMP_TYPE_DRAWABLE,
203 204
                                           "GimpLayer",
                                           &layer_info, 0);
205 206 207

      g_type_add_interface_static (layer_type, GIMP_TYPE_PICKABLE,
                                   &pickable_iface_info);
208 209 210 211 212 213
    }

  return layer_type;
}

static void
214
gimp_layer_class_init (GimpLayerClass *klass)
215
{
216 217 218 219 220
  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);
221

222
  parent_class = g_type_class_peek_parent (klass);
223

224
  layer_signals[OPACITY_CHANGED] =
225
    g_signal_new ("opacity-changed",
226 227 228 229
		  G_TYPE_FROM_CLASS (klass),
		  G_SIGNAL_RUN_FIRST,
		  G_STRUCT_OFFSET (GimpLayerClass, opacity_changed),
		  NULL, NULL,
230
		  gimp_marshal_VOID__VOID,
231
		  G_TYPE_NONE, 0);
232 233

  layer_signals[MODE_CHANGED] =
234
    g_signal_new ("mode-changed",
235 236 237 238
		  G_TYPE_FROM_CLASS (klass),
		  G_SIGNAL_RUN_FIRST,
		  G_STRUCT_OFFSET (GimpLayerClass, mode_changed),
		  NULL, NULL,
239
		  gimp_marshal_VOID__VOID,
240
		  G_TYPE_NONE, 0);
241

242 243
  layer_signals[LOCK_ALPHA_CHANGED] =
    g_signal_new ("lock-alpha-changed",
244 245
		  G_TYPE_FROM_CLASS (klass),
		  G_SIGNAL_RUN_FIRST,
246
		  G_STRUCT_OFFSET (GimpLayerClass, lock_alpha_changed),
247
		  NULL, NULL,
248
		  gimp_marshal_VOID__VOID,
249
		  G_TYPE_NONE, 0);
250

251
  layer_signals[MASK_CHANGED] =
252
    g_signal_new ("mask-changed",
253 254 255 256
		  G_TYPE_FROM_CLASS (klass),
		  G_SIGNAL_RUN_FIRST,
		  G_STRUCT_OFFSET (GimpLayerClass, mask_changed),
		  NULL, NULL,
257
		  gimp_marshal_VOID__VOID,
258
		  G_TYPE_NONE, 0);
259

260 261
  object_class->set_property          = gimp_layer_set_property;
  object_class->get_property          = gimp_layer_get_property;
262
  object_class->dispose               = gimp_layer_dispose;
263 264
  object_class->finalize              = gimp_layer_finalize;

265
  gimp_object_class->name_changed     = gimp_layer_name_changed;
266 267 268 269
  gimp_object_class->get_memsize      = gimp_layer_get_memsize;

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

272
  item_class->removed                 = gimp_layer_removed;
273
  item_class->is_attached             = gimp_layer_is_attached;
274
  item_class->duplicate               = gimp_layer_duplicate;
275
  item_class->convert                 = gimp_layer_convert;
276 277 278 279 280 281 282 283 284
  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;
  item_class->default_name            = _("Layer");
  item_class->rename_desc             = _("Rename Layer");
285 286 287 288 289 290
  item_class->translate_desc          = _("Move Layer");
  item_class->scale_desc              = _("Scale Layer");
  item_class->resize_desc             = _("Resize Layer");
  item_class->flip_desc               = _("Flip Layer");
  item_class->rotate_desc             = _("Rotate Layer");
  item_class->transform_desc          = _("Transform Layer");
291

292 293
  drawable_class->invalidate_boundary   = gimp_layer_invalidate_boundary;
  drawable_class->get_active_components = gimp_layer_get_active_components;
294
  drawable_class->set_tiles             = gimp_layer_set_tiles;
295 296 297

  klass->opacity_changed              = NULL;
  klass->mode_changed                 = NULL;
298
  klass->lock_alpha_changed           = NULL;
299
  klass->mask_changed                 = NULL;
300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318

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

  g_object_class_install_property (object_class, PROP_MODE,
                                   g_param_spec_enum ("mode", NULL, NULL,
                                                      GIMP_TYPE_LAYER_MODE_EFFECTS,
                                                      GIMP_NORMAL_MODE,
                                                      G_PARAM_READABLE));

  g_object_class_install_property (object_class, PROP_LOCK_ALPHA,
                                   g_param_spec_boolean ("lock-alpha",
                                                         NULL, NULL,
                                                         FALSE,
                                                         G_PARAM_READABLE));
319 320 321 322 323
}

static void
gimp_layer_init (GimpLayer *layer)
{
324 325 326
  layer->opacity    = GIMP_OPACITY_OPAQUE;
  layer->mode       = GIMP_NORMAL_MODE;
  layer->lock_alpha = FALSE;
327

328
  layer->mask       = NULL;
329

330 331 332 333 334 335 336
  /*  floating selection  */
  layer->fs.backing_store  = NULL;
  layer->fs.drawable       = NULL;
  layer->fs.initial        = TRUE;
  layer->fs.boundary_known = FALSE;
  layer->fs.segs           = NULL;
  layer->fs.num_segs       = 0;
337 338
}

339 340 341 342 343 344
static void
gimp_layer_pickable_iface_init (GimpPickableInterface *pickable_iface)
{
  pickable_iface->get_opacity_at = gimp_layer_get_opacity_at;
}

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 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388
static void
gimp_layer_set_property (GObject      *object,
                         guint         property_id,
                         const GValue *value,
                         GParamSpec   *pspec)
{
  switch (property_id)
    {
    case PROP_OPACITY:
    case PROP_MODE:
    case PROP_LOCK_ALPHA:
      g_assert_not_reached ();
      break;
    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:
      g_value_set_double (value, layer->opacity);
      break;
    case PROP_MODE:
      g_value_set_enum (value, layer->mode);
      break;
    case PROP_LOCK_ALPHA:
      g_value_set_boolean (value, layer->lock_alpha);
      break;
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
      break;
    }
}

389 390 391 392 393 394 395 396 397 398 399 400 401
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);

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

402 403 404
static void
gimp_layer_finalize (GObject *object)
{
405
  GimpLayer *layer = GIMP_LAYER (object);
406 407 408

  if (layer->mask)
    {
409
      g_object_unref (layer->mask);
410 411 412
      layer->mask = NULL;
    }

413 414 415 416 417 418
  if (layer->fs.backing_store)
    {
      tile_manager_unref (layer->fs.backing_store);
      layer->fs.backing_store = NULL;
    }

419 420 421
  if (layer->fs.segs)
    {
      g_free (layer->fs.segs);
422 423
      layer->fs.segs     = NULL;
      layer->fs.num_segs = 0;
424 425 426 427 428
    }

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

429 430 431 432 433
static void
gimp_layer_name_changed (GimpObject *object)
{
  GimpLayer *layer = GIMP_LAYER (object);

434 435
  if (GIMP_OBJECT_CLASS (parent_class)->name_changed)
    GIMP_OBJECT_CLASS (parent_class)->name_changed (object);
436 437 438 439 440 441 442 443 444 445 446

  if (layer->mask)
    {
      gchar *mask_name;

      mask_name = g_strdup_printf (_("%s mask"), gimp_object_get_name (object));
      gimp_object_set_name (GIMP_OBJECT (layer->mask), mask_name);
      g_free (mask_name);
    }
}

447
static gint64
448
gimp_layer_get_memsize (GimpObject *object,
449
                        gint64     *gui_size)
450
{
451
  GimpLayer *layer   = GIMP_LAYER (object);
452
  gint64     memsize = 0;
453 454

  if (layer->mask)
455
    memsize += gimp_object_get_memsize (GIMP_OBJECT (layer->mask), gui_size);
456 457

  if (layer->fs.backing_store)
458
    *gui_size += tile_manager_get_memsize (layer->fs.backing_store, FALSE);
459

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

462 463
  return memsize + GIMP_OBJECT_CLASS (parent_class)->get_memsize (object,
                                                                  gui_size);
464 465
}

Elliot Lee's avatar
Elliot Lee committed
466
static void
467
gimp_layer_invalidate_preview (GimpViewable *viewable)
468
{
469
  GimpLayer *layer = GIMP_LAYER (viewable);
470

471 472 473
  if (GIMP_VIEWABLE_CLASS (parent_class)->invalidate_preview)
    GIMP_VIEWABLE_CLASS (parent_class)->invalidate_preview (viewable);

474
  if (gimp_layer_is_floating_sel (layer))
475
    floating_sel_invalidate (layer);
476 477
}

478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494
static gchar *
gimp_layer_get_description (GimpViewable  *viewable,
                            gchar        **tooltip)
{
  if (gimp_layer_is_floating_sel (GIMP_LAYER (viewable)))
    {
      if (tooltip)
        *tooltip = NULL;

      return g_strdup_printf (_("Floating Selection\n(%s)"),
                              gimp_object_get_name (GIMP_OBJECT (viewable)));
    }

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

495 496 497 498 499
static void
gimp_layer_get_active_components (const GimpDrawable *drawable,
                                  gboolean           *active)
{
  GimpLayer *layer  = GIMP_LAYER (drawable);
500
  GimpImage *gimage = gimp_item_get_image (GIMP_ITEM (drawable));
501 502 503 504 505 506
  gint       i;

  /*  first copy the gimage active channels  */
  for (i = 0; i < MAX_CHANNELS; i++)
    active[i] = gimage->active[i];

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

511 512 513 514 515
static void
gimp_layer_set_tiles (GimpDrawable *drawable,
                      gboolean      push_undo,
                      const gchar  *undo_desc,
                      TileManager  *tiles,
516 517 518
                      GimpImageType type,
                      gint          offset_x,
                      gint          offset_y)
519
{
520 521
  GimpLayer *layer = GIMP_LAYER (drawable);

522 523 524
  if (gimp_layer_is_floating_sel (layer))
    floating_sel_relax (layer, FALSE);

525 526
  GIMP_DRAWABLE_CLASS (parent_class)->set_tiles (drawable,
                                                 push_undo, undo_desc,
527 528 529
                                                 tiles, type,
                                                 offset_x, offset_y);

530 531
  if (gimp_layer_is_floating_sel (layer))
    floating_sel_rigor (layer, FALSE);
532 533
}

534 535 536 537 538 539 540 541 542 543 544 545
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);
}

546 547 548 549 550 551 552
static gboolean
gimp_layer_is_attached (GimpItem *item)
{
  return (GIMP_IS_IMAGE (item->gimage) &&
          gimp_container_have (item->gimage->layers, GIMP_OBJECT (item)));
}

553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572
static GimpItem *
gimp_layer_duplicate (GimpItem *item,
                      GType     new_type,
                      gboolean  add_alpha)
{
  GimpLayer *layer;
  GimpItem  *new_item;
  GimpLayer *new_layer;

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

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

  if (! GIMP_IS_LAYER (new_item))
    return new_item;

  layer     = GIMP_LAYER (item);
  new_layer = GIMP_LAYER (new_item);

573 574 575
  new_layer->mode       = layer->mode;
  new_layer->opacity    = layer->opacity;
  new_layer->lock_alpha = layer->lock_alpha;
576 577 578 579

  /*  duplicate the layer mask if necessary  */
  if (layer->mask)
    {
Sven Neumann's avatar
Sven Neumann committed
580 581 582 583
      GimpItem *dup = gimp_item_duplicate (GIMP_ITEM (layer->mask),
                                           G_TYPE_FROM_INSTANCE (layer->mask),
                                           FALSE);
      gimp_layer_add_mask (new_layer, GIMP_LAYER_MASK (dup), FALSE);
584 585 586 587 588
    }

  return new_item;
}

589
static void
590 591
gimp_layer_convert (GimpItem  *item,
                    GimpImage *dest_image)
592
{
593 594 595
  GimpLayer         *layer    = GIMP_LAYER (item);
  GimpDrawable      *drawable = GIMP_DRAWABLE (item);
  GimpImageBaseType  old_base_type;
596 597
  GimpImageBaseType  new_base_type;

598
  old_base_type = GIMP_IMAGE_TYPE_BASE_TYPE (gimp_drawable_type (drawable));
599
  new_base_type = gimp_image_base_type (dest_image);
600

601
  if (old_base_type != new_base_type)
602 603 604 605 606 607
    {
      TileManager   *new_tiles;
      GimpImageType  new_type;

      new_type = GIMP_IMAGE_TYPE_FROM_BASE_TYPE (new_base_type);

608
      if (gimp_drawable_has_alpha (drawable))
609 610
        new_type = GIMP_IMAGE_TYPE_WITH_ALPHA (new_type);

611 612
      new_tiles = tile_manager_new (gimp_item_width (item),
                                    gimp_item_height (item),
613 614 615 616 617
                                    GIMP_IMAGE_TYPE_BYTES (new_type));

      switch (new_base_type)
        {
        case GIMP_RGB:
618
          gimp_drawable_convert_rgb (drawable,
619
                                     new_tiles,
620
                                     old_base_type);
621 622 623
          break;

        case GIMP_GRAY:
624
          gimp_drawable_convert_grayscale (drawable,
625
                                           new_tiles,
626
                                           old_base_type);
627 628 629 630 631 632 633
          break;

        case GIMP_INDEXED:
          {
            PixelRegion layerPR;
            PixelRegion newPR;

634
            pixel_region_init (&layerPR, drawable->tiles,
635
                               0, 0,
636 637
                               gimp_item_width (item),
                               gimp_item_height (item),
638 639 640
                               FALSE);
            pixel_region_init (&newPR, new_tiles,
                               0, 0,
641 642
                               gimp_item_width (item),
                               gimp_item_height (item),
643 644
                               TRUE);

645
            gimp_layer_transform_color (gimp_item_get_image (item),
646 647
                                        &newPR, &layerPR,
                                        NULL,
648
                                        old_base_type);
649 650 651 652
          }
          break;
        }

653
      gimp_drawable_set_tiles_full (drawable, FALSE, NULL,
654 655 656
                                    new_tiles, new_type,
                                    GIMP_ITEM (layer)->offset_x,
                                    GIMP_ITEM (layer)->offset_y);
657
      tile_manager_unref (new_tiles);
658 659
    }

660 661 662 663
  if (layer->mask)
    gimp_item_set_image (GIMP_ITEM (layer->mask), dest_image);

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

666
static gboolean
667 668 669 670
gimp_layer_rename (GimpItem    *item,
                   const gchar *new_name,
                   const gchar *undo_desc)
{
671
  GimpLayer *layer = GIMP_LAYER (item);
672
  GimpImage *gimage;
673 674
  gboolean   attached;
  gboolean   floating_sel;
675 676 677

  gimage = gimp_item_get_image (item);

678 679 680 681
  attached     = gimp_item_is_attached (item);
  floating_sel = gimp_layer_is_floating_sel (layer);

  if (floating_sel)
682
    {
683 684 685 686 687 688 689 690
      if (GIMP_IS_CHANNEL (layer->fs.drawable))
        return FALSE;

      if (attached)
        {
          gimp_image_undo_group_start (gimage,
                                       GIMP_UNDO_GROUP_ITEM_PROPERTIES,
                                       undo_desc);
691

692 693
          floating_sel_to_layer (layer);
        }
694 695 696 697
    }

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

698
  if (attached && floating_sel)
699
    gimp_image_undo_group_end (gimage);
700 701

  return TRUE;
702 703
}

704 705
static void
gimp_layer_translate (GimpItem *item,
706 707 708
		      gint      offset_x,
		      gint      offset_y,
                      gboolean  push_undo)
709
{
710
  GimpLayer *layer = GIMP_LAYER (item);
711

712
  if (push_undo)
713
    gimp_image_undo_push_item_displace (gimp_item_get_image (item), NULL, item);
714

715 716 717 718
  /*  update the old region  */
  gimp_drawable_update (GIMP_DRAWABLE (layer), 0, 0, item->width, item->height);

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

721 722 723
  if (gimp_layer_is_floating_sel (layer))
    floating_sel_relax (layer, FALSE);

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

727 728 729
  if (gimp_layer_is_floating_sel (layer))
    floating_sel_rigor (layer, FALSE);

730 731 732
  /*  update the new region  */
  gimp_drawable_update (GIMP_DRAWABLE (layer), 0, 0, item->width, item->height);

733
  if (layer->mask)
734 735 736 737 738 739 740 741
    {
      GIMP_ITEM (layer->mask)->offset_x = item->offset_x;
      GIMP_ITEM (layer->mask)->offset_y = item->offset_y;

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

742 743 744 745 746 747
static void
gimp_layer_scale (GimpItem              *item,
                  gint                   new_width,
                  gint                   new_height,
                  gint                   new_offset_x,
                  gint                   new_offset_y,
748
                  GimpInterpolationType  interpolation_type,
749
                  GimpProgress          *progress)
750
{
751
  GimpLayer *layer = GIMP_LAYER (item);
752 753 754

  GIMP_ITEM_CLASS (parent_class)->scale (item, new_width, new_height,
                                         new_offset_x, new_offset_y,
755
                                         interpolation_type, progress);
756 757

  if (layer->mask)
758 759 760
    gimp_item_scale (GIMP_ITEM (layer->mask),
                     new_width, new_height,
                     new_offset_x, new_offset_y,
761
                     interpolation_type, progress);
762 763
}

764
static void
765 766 767 768 769 770
gimp_layer_resize (GimpItem    *item,
                   GimpContext *context,
		   gint         new_width,
		   gint         new_height,
		   gint         offset_x,
		   gint         offset_y)
771
{
772
  GimpLayer *layer  = GIMP_LAYER (item);
773

774
  GIMP_ITEM_CLASS (parent_class)->resize (item, context, new_width, new_height,
775 776 777
                                          offset_x, offset_y);

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

782 783
static void
gimp_layer_flip (GimpItem            *item,
784
                 GimpContext         *context,
785
                 GimpOrientationType  flip_type,
786 787
                 gdouble              axis,
                 gboolean             clip_result)
788
{
789
  GimpLayer *layer = GIMP_LAYER (item);
790

791 792
  GIMP_ITEM_CLASS (parent_class)->flip (item, context, flip_type, axis,
                                        clip_result);
793 794

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

799 800
static void
gimp_layer_rotate (GimpItem         *item,
801
                   GimpContext      *context,
802 803 804 805 806
                   GimpRotationType  rotate_type,
                   gdouble           center_x,
                   gdouble           center_y,
                   gboolean          clip_result)
{
807
  GimpLayer *layer = GIMP_LAYER (item);
808

809
  GIMP_ITEM_CLASS (parent_class)->rotate (item, context,
810 811 812 813
                                          rotate_type, center_x, center_y,
                                          clip_result);

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

818 819
static void
gimp_layer_transform (GimpItem               *item,
820
                      GimpContext            *context,
821
                      const GimpMatrix3      *matrix,
822 823
                      GimpTransformDirection  direction,
                      GimpInterpolationType   interpolation_type,
824 825
                      gboolean                supersample,
                      gint                    recursion_level,
826
                      gboolean                clip_result,
827
                      GimpProgress           *progress)
828
{
829
  GimpLayer *layer = GIMP_LAYER (item);
830

831
  GIMP_ITEM_CLASS (parent_class)->transform (item, context, matrix, direction,
832 833
                                             interpolation_type,
                                             supersample, recursion_level,
834
                                             clip_result, progress);
835 836

  if (layer->mask)
837
    gimp_item_transform (GIMP_ITEM (layer->mask), context,
838
                         matrix, direction,
839 840
                         interpolation_type,
                         supersample, recursion_level,
841
                         clip_result, progress);
842 843 844 845 846
}

static void
gimp_layer_invalidate_boundary (GimpDrawable *drawable)
{
847
  GimpLayer   *layer = GIMP_LAYER (drawable);
848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871
  GimpImage   *gimage;
  GimpChannel *mask;

  if (! (gimage = gimp_item_get_image (GIMP_ITEM (layer))))
    return;

  /*  Turn the current selection off  */
  gimp_image_selection_control (gimage, GIMP_SELECTION_OFF);

  /*  clear the affected region surrounding the layer  */
  gimp_image_selection_control (gimage, GIMP_SELECTION_LAYER_OFF);

  /*  get the selection mask channel  */
  mask = gimp_image_get_mask (gimage);

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

874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919
static gint
gimp_layer_get_opacity_at (GimpPickable *pickable,
			   gint          x,
			   gint          y)
{
  GimpLayer *layer = GIMP_LAYER (pickable);
  Tile      *tile;
  gint       val   = 0;

  if (x >= 0 && x < GIMP_ITEM (layer)->width &&
      y >= 0 && y < GIMP_ITEM (layer)->height &&
      gimp_item_get_visible (GIMP_ITEM (layer)))
    {
      /*  If the point is inside, and the layer has no
       *  alpha channel, success!
       */
      if (! gimp_drawable_has_alpha (GIMP_DRAWABLE (layer)))
	return OPAQUE_OPACITY;

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

      val = * ((guchar *) tile_data_pointer (tile,
					     x % TILE_WIDTH,
					     y % TILE_HEIGHT) +
	       tile_bpp (tile) - 1);

      if (layer->mask)
	{
	  gint mask_val;

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

	  val = val * mask_val / 255;
	}

      tile_release (tile, FALSE);
    }

  return val;
}

920
static void
921 922 923 924
gimp_layer_transform_color (GimpImage         *gimage,
			    PixelRegion       *layerPR,
			    PixelRegion       *bufPR,
			    GimpDrawable      *drawable,
925
			    GimpImageBaseType  src_type)
Elliot Lee's avatar
Elliot Lee committed
926
{
927
  gpointer  pr;
Elliot Lee's avatar
Elliot Lee committed
928

929 930
  for (pr = pixel_regions_register (2, layerPR, bufPR);
       pr != NULL;
931
       pr = pixel_regions_process (pr))