gimpdrawable.c 46.7 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
/* 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
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 */
Sven Neumann's avatar
Sven Neumann committed
18

19 20
#include "config.h"

21
#include <glib-object.h>
Sven Neumann's avatar
Sven Neumann committed
22

23 24
#include "libgimpcolor/gimpcolor.h"

Michael Natterer's avatar
Michael Natterer committed
25
#include "core-types.h"
26

Michael Natterer's avatar
Michael Natterer committed
27
#include "base/pixel-region.h"
28
#include "base/temp-buf.h"
Michael Natterer's avatar
Michael Natterer committed
29 30 31
#include "base/tile.h"
#include "base/tile-manager.h"

32
#include "paint-funcs/paint-funcs.h"
33
#include "paint-funcs/scale-funcs.h"
34

35
#include "gimp.h"
36
#include "gimp-utils.h"
37
#include "gimpchannel.h"
Michael Natterer's avatar
Michael Natterer committed
38
#include "gimpcontext.h"
39
#include "gimpdrawable-combine.h"
40
#include "gimpdrawable-preview.h"
41
#include "gimpdrawable-transform.h"
42
#include "gimpimage.h"
43
#include "gimpimage-undo-push.h"
44 45
#include "gimplayer.h"
#include "gimplayer-floating-sel.h"
46
#include "gimpmarshal.h"
47
#include "gimppattern.h"
48
#include "gimppickable.h"
49
#include "gimppreviewcache.h"
50
#include "gimpprogress.h"
51

52
#include "gimp-intl.h"
53

54 55 56

enum
{
57
  UPDATE,
58
  ALPHA_CHANGED,
59 60 61
  LAST_SIGNAL
};

62

63 64
/*  local function prototypes  */

65
static void  gimp_drawable_pickable_iface_init (GimpPickableInterface *iface);
66

67
static void       gimp_drawable_finalize           (GObject           *object);
68

69 70
static gint64     gimp_drawable_get_memsize        (GimpObject        *object,
                                                    gint64            *gui_size);
71

72 73 74
static gboolean   gimp_drawable_get_size           (GimpViewable      *viewable,
                                                    gint              *width,
                                                    gint              *height);
75 76 77 78 79
static void       gimp_drawable_invalidate_preview (GimpViewable      *viewable);

static GimpItem * gimp_drawable_duplicate          (GimpItem          *item,
                                                    GType              new_type,
                                                    gboolean           add_alpha);
80 81 82 83
static void       gimp_drawable_translate          (GimpItem          *item,
                                                    gint               offset_x,
                                                    gint               offset_y,
                                                    gboolean           push_undo);
84 85 86 87 88
static void       gimp_drawable_scale              (GimpItem          *item,
                                                    gint               new_width,
                                                    gint               new_height,
                                                    gint               new_offset_x,
                                                    gint               new_offset_y,
89
                                                    GimpInterpolationType interp_type,
90
                                                    GimpProgress       *progress);
91
static void       gimp_drawable_resize             (GimpItem          *item,
92
                                                    GimpContext       *context,
93 94 95 96
                                                    gint               new_width,
                                                    gint               new_height,
                                                    gint               offset_x,
                                                    gint               offset_y);
97
static void       gimp_drawable_flip               (GimpItem          *item,
98
                                                    GimpContext       *context,
99 100 101
                                                    GimpOrientationType  flip_type,
                                                    gdouble            axis,
                                                    gboolean           clip_result);
102
static void       gimp_drawable_rotate             (GimpItem          *item,
103
                                                    GimpContext       *context,
104 105 106 107
                                                    GimpRotationType   rotate_type,
                                                    gdouble            center_x,
                                                    gdouble            center_y,
                                                    gboolean           clip_result);
108
static void       gimp_drawable_transform          (GimpItem          *item,
109
                                                    GimpContext       *context,
110
                                                    const GimpMatrix3 *matrix,
111 112
                                                    GimpTransformDirection  direction,
                                                    GimpInterpolationType   interpolation_type,
113
                                                    gboolean           supersample,
114
                                                    gint               recursion_level,
115
                                                    gboolean           clip_result,
116
                                                    GimpProgress      *progress);
117

118 119 120 121
static guchar   * gimp_drawable_get_color_at       (GimpPickable      *pickable,
                                                    gint               x,
                                                    gint               y);

122 123 124 125 126 127
static void       gimp_drawable_real_update        (GimpDrawable      *drawable,
                                                    gint               x,
                                                    gint               y,
                                                    gint               width,
                                                    gint               height);

128 129 130 131
static void       gimp_drawable_real_set_tiles     (GimpDrawable      *drawable,
                                                    gboolean           push_undo,
                                                    const gchar       *undo_desc,
                                                    TileManager       *tiles,
132 133 134
                                                    GimpImageType      type,
                                                    gint               offset_x,
                                                    gint               offset_y);
135

136 137 138 139 140 141 142 143
static void       gimp_drawable_real_push_undo     (GimpDrawable      *drawable,
                                                    const gchar       *undo_desc,
                                                    TileManager       *tiles,
                                                    gboolean           sparse,
                                                    gint               x,
                                                    gint               y,
                                                    gint               width,
                                                    gint               height);
144 145 146 147 148 149 150 151
static void       gimp_drawable_real_swap_pixels   (GimpDrawable      *drawable,
                                                    TileManager       *tiles,
                                                    gboolean           sparse,
                                                    gint               x,
                                                    gint               y,
                                                    gint               width,
                                                    gint               height);

152

153 154 155
G_DEFINE_TYPE_WITH_CODE (GimpDrawable, gimp_drawable, GIMP_TYPE_ITEM,
                         G_IMPLEMENT_INTERFACE (GIMP_TYPE_PICKABLE,
                                                gimp_drawable_pickable_iface_init));
156

157
#define parent_class gimp_drawable_parent_class
158

159
static guint gimp_drawable_signals[LAST_SIGNAL] = { 0 };
160

161 162

static void
163
gimp_drawable_class_init (GimpDrawableClass *klass)
164
{
Michael Natterer's avatar
Michael Natterer committed
165 166 167 168
  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);
169

170 171
  gimp_drawable_signals[UPDATE] =
    g_signal_new ("update",
Michael Natterer's avatar
Michael Natterer committed
172 173 174 175 176 177 178 179 180 181
                  G_TYPE_FROM_CLASS (klass),
                  G_SIGNAL_RUN_FIRST,
                  G_STRUCT_OFFSET (GimpDrawableClass, update),
                  NULL, NULL,
                  gimp_marshal_VOID__INT_INT_INT_INT,
                  G_TYPE_NONE, 4,
                  G_TYPE_INT,
                  G_TYPE_INT,
                  G_TYPE_INT,
                  G_TYPE_INT);
182

183
  gimp_drawable_signals[ALPHA_CHANGED] =
184
    g_signal_new ("alpha-changed",
Michael Natterer's avatar
Michael Natterer committed
185 186 187 188 189 190
                  G_TYPE_FROM_CLASS (klass),
                  G_SIGNAL_RUN_FIRST,
                  G_STRUCT_OFFSET (GimpDrawableClass, alpha_changed),
                  NULL, NULL,
                  gimp_marshal_VOID__VOID,
                  G_TYPE_NONE, 0);
191

192
  object_class->finalize             = gimp_drawable_finalize;
193

194
  gimp_object_class->get_memsize     = gimp_drawable_get_memsize;
195

196
  viewable_class->get_size           = gimp_drawable_get_size;
197
  viewable_class->invalidate_preview = gimp_drawable_invalidate_preview;
198
  viewable_class->get_preview        = gimp_drawable_get_preview;
199

200
  item_class->duplicate              = gimp_drawable_duplicate;
201
  item_class->translate              = gimp_drawable_translate;
202
  item_class->scale                  = gimp_drawable_scale;
203
  item_class->resize                 = gimp_drawable_resize;
204
  item_class->flip                   = gimp_drawable_flip;
205
  item_class->rotate                 = gimp_drawable_rotate;
206
  item_class->transform              = gimp_drawable_transform;
207

208
  klass->update                      = gimp_drawable_real_update;
209
  klass->alpha_changed               = NULL;
210
  klass->invalidate_boundary         = NULL;
211 212 213
  klass->get_active_components       = NULL;
  klass->apply_region                = gimp_drawable_real_apply_region;
  klass->replace_region              = gimp_drawable_real_replace_region;
214
  klass->set_tiles                   = gimp_drawable_real_set_tiles;
215
  klass->push_undo                   = gimp_drawable_real_push_undo;
216
  klass->swap_pixels                 = gimp_drawable_real_swap_pixels;
217 218
}

219 220 221 222 223 224 225 226 227 228 229
static void
gimp_drawable_init (GimpDrawable *drawable)
{
  drawable->tiles         = NULL;
  drawable->bytes         = 0;
  drawable->type          = -1;
  drawable->has_alpha     = FALSE;
  drawable->preview_cache = NULL;
  drawable->preview_valid = FALSE;
}

230
static void
231
gimp_drawable_pickable_iface_init (GimpPickableInterface *iface)
232
{
233 234 235 236
  iface->get_image      = gimp_item_get_image;
  iface->get_image_type = gimp_drawable_type;
  iface->get_tiles      = gimp_drawable_data;
  iface->get_color_at   = gimp_drawable_get_color_at;
237 238
}

239
static void
240
gimp_drawable_finalize (GObject *object)
241
{
242
  GimpDrawable *drawable = GIMP_DRAWABLE (object);
243 244

  if (drawable->tiles)
245
    {
246
      tile_manager_unref (drawable->tiles);
247 248
      drawable->tiles = NULL;
    }
249 250 251 252

  if (drawable->preview_cache)
    gimp_preview_cache_invalidate (&drawable->preview_cache);

253
  G_OBJECT_CLASS (parent_class)->finalize (object);
254 255
}

256
static gint64
257
gimp_drawable_get_memsize (GimpObject *object,
258
                           gint64     *gui_size)
259
{
Michael Natterer's avatar
Michael Natterer committed
260 261
  GimpDrawable *drawable = GIMP_DRAWABLE (object);
  gint64        memsize  = 0;
262 263

  if (drawable->tiles)
264
    memsize += tile_manager_get_memsize (drawable->tiles, FALSE);
265 266

  if (drawable->preview_cache)
267
    *gui_size += gimp_preview_cache_get_memsize (drawable->preview_cache);
268

269 270
  return memsize + GIMP_OBJECT_CLASS (parent_class)->get_memsize (object,
                                                                  gui_size);
271 272
}

273 274 275 276 277 278 279 280 281 282 283 284 285
static gboolean
gimp_drawable_get_size (GimpViewable *viewable,
                        gint         *width,
                        gint         *height)
{
  GimpItem *item = GIMP_ITEM (viewable);

  *width  = item->width;
  *height = item->height;

  return TRUE;
}

286 287 288
static void
gimp_drawable_invalidate_preview (GimpViewable *viewable)
{
Michael Natterer's avatar
Michael Natterer committed
289
  GimpDrawable *drawable = GIMP_DRAWABLE (viewable);
290

291 292 293
  if (GIMP_VIEWABLE_CLASS (parent_class)->invalidate_preview)
    GIMP_VIEWABLE_CLASS (parent_class)->invalidate_preview (viewable);

294 295
  drawable->preview_valid = FALSE;

296 297
  if (drawable->preview_cache)
    gimp_preview_cache_invalidate (&drawable->preview_cache);
298 299
}

300 301 302 303
static GimpItem *
gimp_drawable_duplicate (GimpItem *item,
                         GType     new_type,
                         gboolean  add_alpha)
304
{
305
  GimpItem *new_item;
306 307 308

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

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

312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349
  if (GIMP_IS_DRAWABLE (new_item))
    {
      GimpDrawable  *drawable     = GIMP_DRAWABLE (item);
      GimpDrawable  *new_drawable = GIMP_DRAWABLE (new_item);
      GimpImageType  new_image_type;
      PixelRegion    srcPR;
      PixelRegion    destPR;

      if (add_alpha)
        new_image_type = gimp_drawable_type_with_alpha (drawable);
      else
        new_image_type = gimp_drawable_type (drawable);

      gimp_drawable_configure (new_drawable,
                               gimp_item_get_image (item),
                               item->offset_x,
                               item->offset_y,
                               item->width,
                               item->height,
                               new_image_type,
                               GIMP_OBJECT (new_drawable)->name);

      pixel_region_init (&srcPR, drawable->tiles,
                         0, 0,
                         item->width,
                         item->height,
                         FALSE);
      pixel_region_init (&destPR, new_drawable->tiles,
                         0, 0,
                         new_item->width,
                         new_item->height,
                         TRUE);

      if (new_image_type == drawable->type)
        copy_region (&srcPR, &destPR);
      else
        add_alpha_region (&srcPR, &destPR);
    }
350

351 352 353
  return new_item;
}

354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372
static void
gimp_drawable_translate (GimpItem *item,
                         gint      offset_x,
                         gint      offset_y,
                         gboolean  push_undo)
{
  GimpDrawable *drawable = GIMP_DRAWABLE (item);
  GimpImage    *gimage   = gimp_item_get_image (item);

  if (gimp_drawable_has_floating_sel (drawable))
    floating_sel_relax (gimp_image_floating_sel (gimage), FALSE);

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

  if (gimp_drawable_has_floating_sel (drawable))
    floating_sel_rigor (gimp_image_floating_sel (gimage), FALSE);
}

373 374 375 376 377 378
static void
gimp_drawable_scale (GimpItem              *item,
                     gint                   new_width,
                     gint                   new_height,
                     gint                   new_offset_x,
                     gint                   new_offset_y,
379
                     GimpInterpolationType  interpolation_type,
380
                     GimpProgress          *progress)
381
{
382
  GimpDrawable *drawable = GIMP_DRAWABLE (item);
383 384 385 386 387 388
  PixelRegion   srcPR, destPR;
  TileManager  *new_tiles;

  new_tiles = tile_manager_new (new_width, new_height, drawable->bytes);

  pixel_region_init (&srcPR, drawable->tiles,
Michael Natterer's avatar
Michael Natterer committed
389
                     0, 0, item->width, item->height,
390 391
                     FALSE);
  pixel_region_init (&destPR, new_tiles,
392
                     0, 0, new_width, new_height,
393 394 395 396 397 398 399
                     TRUE);

  /*  Scale the drawable -
   *   If the drawable is indexed, then we don't use pixel-value
   *   resampling because that doesn't necessarily make sense for indexed
   *   images.
   */
400 401
  scale_region (&srcPR, &destPR,
                gimp_drawable_is_indexed (drawable) ?
402
                GIMP_INTERPOLATION_NONE : interpolation_type,
403 404
                progress ? gimp_progress_update_and_flush : NULL,
                progress);
405

406
  gimp_drawable_set_tiles_full (drawable, gimp_item_is_attached (item), NULL,
407 408
                                new_tiles, gimp_drawable_type (drawable),
                                new_offset_x, new_offset_y);
409
  tile_manager_unref (new_tiles);
410 411
}

412
static void
413 414 415 416 417 418
gimp_drawable_resize (GimpItem    *item,
                      GimpContext *context,
                      gint         new_width,
                      gint         new_height,
                      gint         offset_x,
                      gint         offset_y)
419
{
420
  GimpDrawable *drawable = GIMP_DRAWABLE (item);
421 422
  PixelRegion   srcPR, destPR;
  TileManager  *new_tiles;
423 424 425 426
  gint          new_offset_x;
  gint          new_offset_y;
  gint          copy_x, copy_y;
  gint          copy_width, copy_height;
427

428
  /*  if the size doesn't change, this is a nop  */
429 430 431 432
  if (new_width  == item->width  &&
      new_height == item->height &&
      offset_x   == 0            &&
      offset_y   == 0)
433 434
    return;

435 436
  new_offset_x = item->offset_x - offset_x;
  new_offset_y = item->offset_y - offset_y;
437

438 439 440 441 442 443
  gimp_rectangle_intersect (item->offset_x, item->offset_y,
                            item->width, item->height,
                            new_offset_x, new_offset_y,
                            new_width, new_height,
                            &copy_x, &copy_y,
                            &copy_width, &copy_height);
444

445
  new_tiles = tile_manager_new (new_width, new_height, drawable->bytes);
446 447

  /*  Determine whether the new tiles need to be initially cleared  */
448 449
  if (copy_width  != new_width ||
      copy_height != new_height)
450
    {
451 452
      guchar bg[MAX_CHANNELS] = { 0, };

453 454 455 456 457
      pixel_region_init (&destPR, new_tiles,
                         0, 0,
                         new_width, new_height,
                         TRUE);

458
      if (! gimp_drawable_has_alpha (drawable) && ! GIMP_IS_CHANNEL (drawable))
459 460
        gimp_image_get_background (gimp_item_get_image (item), drawable,
                                   context, bg);
461 462

      color_region (&destPR, bg);
463 464
    }

465 466
  /*  Determine whether anything needs to be copied  */
  if (copy_width && copy_height)
467
    {
468 469 470 471 472
      pixel_region_init (&srcPR, drawable->tiles,
                         copy_x - item->offset_x, copy_y - item->offset_y,
                         copy_width, copy_height,
                         FALSE);

473
      pixel_region_init (&destPR, new_tiles,
474 475
                         copy_x - new_offset_x, copy_y - new_offset_y,
                         copy_width, copy_height,
476 477 478 479 480
                         TRUE);

      copy_region (&srcPR, &destPR);
    }

481
  gimp_drawable_set_tiles_full (drawable, gimp_item_is_attached (item), NULL,
482
                                new_tiles, gimp_drawable_type (drawable),
483
                                new_offset_x, new_offset_y);
484
  tile_manager_unref (new_tiles);
485 486
}

487 488
static void
gimp_drawable_flip (GimpItem            *item,
489
                    GimpContext         *context,
490 491 492 493
                    GimpOrientationType  flip_type,
                    gdouble              axis,
                    gboolean             clip_result)
{
494
  GimpDrawable *drawable = GIMP_DRAWABLE (item);
495 496 497 498 499 500 501 502 503
  TileManager  *tiles;
  gint          off_x, off_y;
  gint          old_off_x, old_off_y;

  gimp_item_offsets (item, &off_x, &off_y);

  tile_manager_get_offsets (drawable->tiles, &old_off_x, &old_off_y);
  tile_manager_set_offsets (drawable->tiles, off_x, off_y);

504
  tiles = gimp_drawable_transform_tiles_flip (drawable, context,
505 506 507 508 509 510 511
                                              drawable->tiles,
                                              flip_type, axis,
                                              clip_result);

  tile_manager_set_offsets (drawable->tiles, old_off_x, old_off_y);

  if (tiles)
512 513 514 515
    {
      gimp_drawable_transform_paste (drawable, tiles, FALSE);
      tile_manager_unref (tiles);
    }
516 517
}

518 519
static void
gimp_drawable_rotate (GimpItem         *item,
520
                      GimpContext      *context,
521 522 523 524 525
                      GimpRotationType  rotate_type,
                      gdouble           center_x,
                      gdouble           center_y,
                      gboolean          clip_result)
{
526
  GimpDrawable *drawable = GIMP_DRAWABLE (item);
527 528 529 530 531 532 533 534 535
  TileManager  *tiles;
  gint          off_x, off_y;
  gint          old_off_x, old_off_y;

  gimp_item_offsets (item, &off_x, &off_y);

  tile_manager_get_offsets (drawable->tiles, &old_off_x, &old_off_y);
  tile_manager_set_offsets (drawable->tiles, off_x, off_y);

536
  tiles = gimp_drawable_transform_tiles_rotate (drawable, context,
537 538 539 540 541 542 543
                                                drawable->tiles,
                                                rotate_type, center_x, center_y,
                                                clip_result);

  tile_manager_set_offsets (drawable->tiles, old_off_x, old_off_y);

  if (tiles)
544 545 546 547
    {
      gimp_drawable_transform_paste (drawable, tiles, FALSE);
      tile_manager_unref (tiles);
    }
548 549
}

550 551
static void
gimp_drawable_transform (GimpItem               *item,
552
                         GimpContext            *context,
553
                         const GimpMatrix3      *matrix,
554 555
                         GimpTransformDirection  direction,
                         GimpInterpolationType   interpolation_type,
556
                         gboolean                supersample,
557
                         gint                    recursion_level,
558
                         gboolean                clip_result,
559
                         GimpProgress           *progress)
560
{
561
  GimpDrawable *drawable = GIMP_DRAWABLE (item);
562 563 564 565 566 567 568 569 570
  TileManager  *tiles;
  gint          off_x, off_y;
  gint          old_off_x, old_off_y;

  gimp_item_offsets (item, &off_x, &off_y);

  tile_manager_get_offsets (drawable->tiles, &old_off_x, &old_off_y);
  tile_manager_set_offsets (drawable->tiles, off_x, off_y);

571
  tiles = gimp_drawable_transform_tiles_affine (drawable, context,
572 573 574
                                                drawable->tiles,
                                                matrix, direction,
                                                interpolation_type,
575
                                                supersample, recursion_level,
576
                                                clip_result,
577
                                                progress);
578 579 580 581

  tile_manager_set_offsets (drawable->tiles, old_off_x, old_off_y);

  if (tiles)
582 583 584 585
    {
      gimp_drawable_transform_paste (drawable, tiles, FALSE);
      tile_manager_unref (tiles);
    }
586 587
}

588 589
static guchar *
gimp_drawable_get_color_at (GimpPickable *pickable,
Michael Natterer's avatar
Michael Natterer committed
590 591
                            gint          x,
                            gint          y)
592 593
{
  GimpDrawable *drawable = GIMP_DRAWABLE (pickable);
594
  GimpImage    *gimage   = gimp_item_get_image (GIMP_ITEM (drawable));
595 596 597 598 599 600 601 602 603 604 605 606
  Tile         *tile;
  guchar       *src;
  guchar       *dest;

  /* do not make this a g_return_if_fail() */
  if (x < 0 || x >= GIMP_ITEM (drawable)->width ||
      y < 0 || y >= GIMP_ITEM (drawable)->height)
    return NULL;

  dest = g_new (guchar, 5);

  tile = tile_manager_get_tile (gimp_drawable_data (drawable), x, y,
Michael Natterer's avatar
Michael Natterer committed
607
                                TRUE, FALSE);
608 609 610 611 612 613 614 615 616 617 618 619 620 621
  src = tile_data_pointer (tile, x % TILE_WIDTH, y % TILE_HEIGHT);

  gimp_image_get_color (gimage, gimp_drawable_type (drawable), src, dest);

  if (gimp_drawable_is_indexed (drawable))
    dest[4] = src[0];
  else
    dest[4] = 0;

  tile_release (tile, FALSE);

  return dest;
}

622 623 624 625 626 627 628 629 630 631
static void
gimp_drawable_real_update (GimpDrawable *drawable,
                           gint          x,
                           gint          y,
                           gint          width,
                           gint          height)
{
  gimp_viewable_invalidate_preview (GIMP_VIEWABLE (drawable));
}

632 633 634 635 636
static void
gimp_drawable_real_set_tiles (GimpDrawable *drawable,
                              gboolean      push_undo,
                              const gchar  *undo_desc,
                              TileManager  *tiles,
637 638 639
                              GimpImageType type,
                              gint          offset_x,
                              gint          offset_y)
640
{
641 642
  GimpItem *item;
  gboolean  old_has_alpha;
643

644 645
  g_return_if_fail (tile_manager_bpp (tiles) == GIMP_IMAGE_TYPE_BYTES (type));

646 647
  item = GIMP_ITEM (drawable);

648 649
  old_has_alpha = gimp_drawable_has_alpha (drawable);

650 651
  gimp_drawable_invalidate_boundary (drawable);

652 653 654 655
  if (push_undo)
    gimp_image_undo_push_drawable_mod (gimp_item_get_image (item), undo_desc,
                                       drawable);

656 657 658
  /*  ref new before unrefing old, they might be the same  */
  tile_manager_ref (tiles);

659 660 661
  if (drawable->tiles)
    tile_manager_unref (drawable->tiles);

662
  drawable->tiles     = tiles;
663 664 665
  drawable->type      = type;
  drawable->bytes     = tile_manager_bpp (tiles);
  drawable->has_alpha = GIMP_IMAGE_TYPE_HAS_ALPHA (type);
666

667 668 669 670 671 672 673 674 675 676 677 678
  item->offset_x = offset_x;
  item->offset_y = offset_y;

  if (item->width  != tile_manager_width (tiles) ||
      item->height != tile_manager_height (tiles))
    {
      item->width  = tile_manager_width (tiles);
      item->height = tile_manager_height (tiles);

      gimp_viewable_size_changed (GIMP_VIEWABLE (drawable));
    }

679 680
  if (old_has_alpha != gimp_drawable_has_alpha (drawable))
    gimp_drawable_alpha_changed (drawable);
681 682
}

683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717
static void
gimp_drawable_real_push_undo (GimpDrawable *drawable,
                              const gchar  *undo_desc,
                              TileManager  *tiles,
                              gboolean      sparse,
                              gint          x,
                              gint          y,
                              gint          width,
                              gint          height)
{
  gboolean new_tiles = FALSE;

  if (! tiles)
    {
      PixelRegion srcPR, destPR;

      tiles = tile_manager_new (width, height, gimp_drawable_bytes (drawable));
      pixel_region_init (&srcPR, gimp_drawable_data (drawable),
                         x, y, width, height, FALSE);
      pixel_region_init (&destPR, tiles,
                         0, 0, width, height, TRUE);
      copy_region (&srcPR, &destPR);

      new_tiles = TRUE;
    }

  gimp_image_undo_push_drawable (gimp_item_get_image (GIMP_ITEM (drawable)),
                                 undo_desc, drawable,
                                 tiles, sparse,
                                 x, y, width, height);

  if (new_tiles)
    tile_manager_unref (tiles);
}

718 719 720 721 722 723 724 725 726 727 728 729 730 731
static void
gimp_drawable_real_swap_pixels (GimpDrawable      *drawable,
                                TileManager       *tiles,
                                gboolean           sparse,
                                gint               x,
                                gint               y,
                                gint               width,
                                gint               height)
{
  if (sparse)
    {
      gint i, j;

      for (i = y; i < (y + height); i += (TILE_HEIGHT - (i % TILE_HEIGHT)))
Michael Natterer's avatar
Michael Natterer committed
732 733 734
        {
          for (j = x; j < (x + width); j += (TILE_WIDTH - (j % TILE_WIDTH)))
            {
735 736 737
              Tile *src_tile;
              Tile *dest_tile;

Michael Natterer's avatar
Michael Natterer committed
738
              src_tile = tile_manager_get_tile (tiles, j, i, FALSE, FALSE);
739

Michael Natterer's avatar
Michael Natterer committed
740 741 742
              if (tile_is_valid (src_tile))
                {
                  /* swap tiles, not pixels! */
743

Michael Natterer's avatar
Michael Natterer committed
744
                  src_tile = tile_manager_get_tile (tiles,
745
                                                    j, i, TRUE, FALSE /*TRUE*/);
Michael Natterer's avatar
Michael Natterer committed
746
                  dest_tile = tile_manager_get_tile (gimp_drawable_data (drawable), j, i, TRUE, FALSE /* TRUE */);
747

Michael Natterer's avatar
Michael Natterer committed
748
                  tile_manager_map_tile (tiles,
749
                                         j, i, dest_tile);
Michael Natterer's avatar
Michael Natterer committed
750
                  tile_manager_map_tile (gimp_drawable_data (drawable),
751 752
                                         j, i, src_tile);
#if 0
Michael Natterer's avatar
Michael Natterer committed
753 754 755
                  swap_pixels (tile_data_pointer (src_tile, 0, 0),
                               tile_data_pointer (dest_tile, 0, 0),
                               tile_size (src_tile));
756 757
#endif

Michael Natterer's avatar
Michael Natterer committed
758 759 760 761 762
                  tile_release (dest_tile, FALSE /* TRUE */);
                  tile_release (src_tile, FALSE /* TRUE */);
                }
            }
        }
763 764 765 766 767 768
    }
  else
    {
      PixelRegion PR1, PR2;

      pixel_region_init (&PR1, tiles,
Michael Natterer's avatar
Michael Natterer committed
769
                         0, 0, width, height, TRUE);
770
      pixel_region_init (&PR2, gimp_drawable_data (drawable),
Michael Natterer's avatar
Michael Natterer committed
771
                         x, y, width, height, TRUE);
772 773 774 775 776 777 778 779 780 781

      swap_region (&PR1, &PR2);
    }

  gimp_drawable_update (drawable, x, y, width, height);
}


/*  public functions  */

782 783
void
gimp_drawable_configure (GimpDrawable  *drawable,
Michael Natterer's avatar
Michael Natterer committed
784
                         GimpImage     *gimage,
785 786
                         gint           offset_x,
                         gint           offset_y,
Michael Natterer's avatar
Michael Natterer committed
787 788 789 790
                         gint           width,
                         gint           height,
                         GimpImageType  type,
                         const gchar   *name)
791 792 793
{
  g_return_if_fail (GIMP_IS_DRAWABLE (drawable));
  g_return_if_fail (GIMP_IS_IMAGE (gimage));
794
  g_return_if_fail (width > 0 && height > 0);
795

796 797
  gimp_item_configure (GIMP_ITEM (drawable), gimage,
                       offset_x, offset_y, width, height, name);
798

799 800 801 802 803
  drawable->type      = type;
  drawable->bytes     = GIMP_IMAGE_TYPE_BYTES (type);
  drawable->has_alpha = GIMP_IMAGE_TYPE_HAS_ALPHA (type);

  if (drawable->tiles)
804
    tile_manager_unref (drawable->tiles);
805

806
  drawable->tiles = tile_manager_new (width, height, drawable->bytes);
807 808 809 810

  /*  preview variables  */
  drawable->preview_cache = NULL;
  drawable->preview_valid = FALSE;
811 812
}

Michael Natterer's avatar
Michael Natterer committed
813 814
void
gimp_drawable_update (GimpDrawable *drawable,
Michael Natterer's avatar
Michael Natterer committed
815 816 817 818
                      gint          x,
                      gint          y,
                      gint          width,
                      gint          height)
Michael Natterer's avatar
Michael Natterer committed
819 820 821
{
  g_return_if_fail (GIMP_IS_DRAWABLE (drawable));

822 823
  g_signal_emit (drawable, gimp_drawable_signals[UPDATE], 0,
                 x, y, width, height);
Michael Natterer's avatar
Michael Natterer committed
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 869 870 871 872 873
void
gimp_drawable_alpha_changed (GimpDrawable *drawable)
{
  g_return_if_fail (GIMP_IS_DRAWABLE (drawable));

  g_signal_emit (drawable, gimp_drawable_signals[ALPHA_CHANGED], 0);
}

void
gimp_drawable_invalidate_boundary (GimpDrawable *drawable)
{
  GimpDrawableClass *drawable_class;

  g_return_if_fail (GIMP_IS_DRAWABLE (drawable));

  drawable_class = GIMP_DRAWABLE_GET_CLASS (drawable);

  if (drawable_class->invalidate_boundary)
    drawable_class->invalidate_boundary (drawable);
}

void
gimp_drawable_get_active_components (const GimpDrawable *drawable,
                                     gboolean           *active)
{
  GimpDrawableClass *drawable_class;

  g_return_if_fail (GIMP_IS_DRAWABLE (drawable));
  g_return_if_fail (active != NULL);

  drawable_class = GIMP_DRAWABLE_GET_CLASS (drawable);

  if (drawable_class->get_active_components)
    drawable_class->get_active_components (drawable, active);
}

void
gimp_drawable_apply_region (GimpDrawable         *drawable,
                            PixelRegion          *src2PR,
                            gboolean              push_undo,
                            const gchar          *undo_desc,
                            gdouble               opacity,
                            GimpLayerModeEffects  mode,
                            TileManager          *src1_tiles,
                            gint                  x,
                            gint                  y)
{
  g_return_if_fail (GIMP_IS_DRAWABLE (drawable));
874
  g_return_if_fail (gimp_item_is_attached (GIMP_ITEM (drawable)));
875
  g_return_if_fail (src2PR != NULL);
876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894

  GIMP_DRAWABLE_GET_CLASS (drawable)->apply_region (drawable, src2PR,
                                                    push_undo, undo_desc,
                                                    opacity, mode,
                                                    src1_tiles,
                                                    x, y);
}

void
gimp_drawable_replace_region (GimpDrawable *drawable,
                              PixelRegion  *src2PR,
                              gboolean      push_undo,
                              const gchar  *undo_desc,
                              gdouble       opacity,
                              PixelRegion  *maskPR,
                              gint          x,
                              gint          y)
{
  g_return_if_fail (GIMP_IS_DRAWABLE (drawable));
895
  g_return_if_fail (gimp_item_is_attached (GIMP_ITEM (drawable)));
896 897
  g_return_if_fail (src2PR != NULL);
  g_return_if_fail (maskPR != NULL);
898 899 900 901 902 903 904

  GIMP_DRAWABLE_GET_CLASS (drawable)->replace_region (drawable, src2PR,
                                                      push_undo, undo_desc,
                                                      opacity, maskPR,
                                                      x, y);
}

905 906 907 908
void
gimp_drawable_set_tiles (GimpDrawable *drawable,
                         gboolean      push_undo,
                         const gchar  *undo_desc,
909 910 911 912 913 914 915
                         TileManager  *tiles)
{
  gint offset_x, offset_y;

  g_return_if_fail (GIMP_IS_DRAWABLE (drawable));
  g_return_if_fail (tiles != NULL);

916 917 918
  if (! gimp_item_is_attached (GIMP_ITEM (drawable)))
    push_undo = FALSE;

919 920 921 922 923 924 925 926 927 928 929 930 931 932 933
  gimp_item_offsets (GIMP_ITEM (drawable), &offset_x, &offset_y);

  gimp_drawable_set_tiles_full (drawable, push_undo, undo_desc, tiles,
                                gimp_drawable_type (drawable),
                                offset_x, offset_y);
}

void
gimp_drawable_set_tiles_full (GimpDrawable       *drawable,
                              gboolean            push_undo,
                              const gchar        *undo_desc,
                              TileManager        *tiles,
                              GimpImageType       type,
                              gint                offset_x,
                              gint                offset_y)
934
{
935 936
  GimpItem  *item;
  GimpImage *gimage;
937

938 939
  g_return_if_fail (GIMP_IS_DRAWABLE (drawable));
  g_return_if_fail (tiles != NULL);
940
  g_return_if_fail (tile_manager_bpp (tiles) == GIMP_IMAGE_TYPE_BYTES (type));
941

942
  item   = GIMP_ITEM (drawable);
943 944
  gimage = gimp_item_get_image (item);

945 946 947
  if (! gimp_item_is_attached (GIMP_ITEM (drawable)))
    push_undo = FALSE;

948 949 950 951 952 953 954 955
  if (item->width    != tile_manager_width (tiles)  ||
      item->height   != tile_manager_height (tiles) ||
      item->offset_x != offset_x                    ||
      item->offset_y != offset_y)
    {
      gimp_drawable_update (drawable, 0, 0, item->width, item->height);
    }

956 957 958
  if (gimp_drawable_has_floating_sel (drawable))
    floating_sel_relax (gimp_image_floating_sel (gimage), FALSE);

959 960
  GIMP_DRAWABLE_GET_CLASS (drawable)->set_tiles (drawable,
                                                 push_undo, undo_desc,
961 962
                                                 tiles, type,
                                                 offset_x, offset_y);
963

964 965 966
  if (gimp_drawable_has_floating_sel (drawable))
    floating_sel_rigor (gimp_image_floating_sel (gimage), FALSE);

967
  gimp_drawable_update (drawable, 0, 0, item->width, item->height);
968 969
}

970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985
void
gimp_drawable_swap_pixels (GimpDrawable *drawable,
                           TileManager  *tiles,
                           gboolean      sparse,
                           gint          x,
                           gint          y,
                           gint          width,
                           gint          height)
{
  g_return_if_fail (GIMP_IS_DRAWABLE (drawable));
  g_return_if_fail (tiles != NULL);

  GIMP_DRAWABLE_GET_CLASS (drawable)->swap_pixels (drawable, tiles, sparse,
                                                   x, y, width, height);
}

Michael Natterer's avatar
Michael Natterer committed
986
void
987 988
gimp_drawable_push_undo (GimpDrawable *drawable,
                         const gchar  *undo_desc,
989 990 991
                         gint          x1,
                         gint          y1,
                         gint          x2,
992
                         gint          y2,
993 994
                         TileManager  *tiles,
                         gboolean      sparse)
Michael Natterer's avatar
Michael Natterer committed
995
{
996
  GimpItem *item;
997 998
  gint      x, y;
  gint      width, height;
999

Michael Natterer's avatar
Michael Natterer committed
1000
  g_return_if_fail (GIMP_IS_DRAWABLE (drawable));
1001 1002 1003 1004
  g_return_if_fail (sparse == FALSE || tiles != NULL);

  item = GIMP_ITEM (drawable);

1005
  g_return_if_fail (gimp_item_is_attached (item));
1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022
  g_return_if_fail (sparse == FALSE ||
                    tile_manager_width (tiles) == gimp_item_width (item));
  g_return_if_fail (sparse == FALSE ||
                    tile_manager_height (tiles) == gimp_item_height (item));

#if 0
  g_printerr ("gimp_drawable_push_undo (%s, %d, %d, %d, %d)\n",
              sparse ? "TRUE" : "FALSE", x1, y1, x2 - x1, y2 - y1);
#endif

  if (! gimp_rectangle_intersect (x1, y1,
                                  x2 - x1, y2 - y1,
                                  0, 0,
                                  gimp_item_width (item),
                                  gimp_item_height (item),
                                  &x, &y, &width, &height))
    {
1023
      g_warning ("%s: tried to push empty region", G_STRFUNC);
1024 1025
      return;
    }
Michael Natterer's avatar
Michael Natterer committed
1026

1027 1028 1029
  GIMP_DRAWABLE_GET_CLASS (drawable)->push_undo (drawable, undo_desc,
                                                 tiles, sparse,
                                                 x, y, width, height);
Michael Natterer's avatar
Michael Natterer committed
1030 1031
}

1032 1033 1034
TileManager *
gimp_drawable_shadow (GimpDrawable *drawable)
{
1035
  GimpItem *item;
1036 1037 1038

  g_return_val_if_fail (GIMP_IS_DRAWABLE (drawable), NULL);

1039
  item = GIMP_ITEM (drawable);
1040

1041
  g_return_val_if_fail (gimp_item_is_attached (item), NULL);
1042

1043 1044
  return gimp_image_shadow (gimp_item_get_image (item),
                            item->width, item->height,
Michael Natterer's avatar
Michael Natterer committed
1045
                            drawable->bytes);
1046 1047
}

1048
void
1049
gimp_drawable_merge_shadow (GimpDrawable *drawable,
Michael Natterer's avatar
Michael Natterer committed
1050
                            gboolean      push_undo,
1051
                            const gchar  *undo_desc)
1052
{
1053 1054
  GimpImage *gimage;
  gint       x, y, width, height;
1055

Manish Singh's avatar
Manish Singh committed
1056
  g_return_if_fail (GIMP_IS_DRAWABLE (drawable));
1057
  g_return_if_fail (gimp_item_is_attached (GIMP_ITEM (drawable)));
1058

1059
  gimage = gimp_item_get_image (GIMP_ITEM (drawable));
1060

Manish Singh's avatar
Manish Singh committed
1061
  g_return_if_fail (gimage->shadow != NULL);
1062

1063 1064 1065 1066
  /*  A useful optimization here is to limit the update to the
   *  extents of the selection mask, as it cannot extend beyond
   *  them.
   */
1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077
  if (gimp_drawable_mask_intersect (drawable, &x, &y, &width, &height))
    {
      PixelRegion shadowPR;

      pixel_region_init (&shadowPR, gimage->shadow,
                         x, y, width, height, FALSE);
      gimp_drawable_apply_region (drawable, &shadowPR,
                                  push_undo, undo_desc,
                                  GIMP_OPACITY_OPAQUE, GIMP_REPLACE_MODE,
                                  NULL, x, y);
    }
1078 1079 1080
}

void
1081
gimp_drawable_fill (GimpDrawable      *drawable,
Michael Natterer's avatar
Michael Natterer committed
1082
                    const GimpRGB     *color,
1083
                    const GimpPattern *pattern)
1084
{
1085 1086 1087 1088
  GimpItem      *item;
  GimpImage     *gimage;
  GimpImageType  drawable_type;
  PixelRegion    destPR;
1089

Manish Singh's avatar
Manish Singh committed
1090
  g_return_if_fail (GIMP_IS_DRAWABLE (drawable));
1091 1092
  g_return_if_fail (color != NULL || pattern != NULL);
  g_return_if_fail (pattern == NULL || GIMP_IS_PATTERN (pattern));
Manish Singh's avatar
Manish Singh committed
1093

1094 1095
  item   = GIMP_ITEM (drawable);
  gimage = gimp_item_get_image (item);
1096

1097 1098
  drawable_type = gimp_drawable_type (drawable);

1099 1100 1101
  pixel_region_init (&destPR, gimp_drawable_data (drawable),
                     0, 0, gimp_item_width  (item), gimp_item_height (item),
                     TRUE);
1102

1103
  if (color)
1104
    {
1105 1106
      guchar tmp[MAX_CHANNELS];
      guchar c[MAX_CHANNELS];
1107

1108 1109 1110 1111 1112
      gimp_rgba_get_uchar (color,
                           &tmp[RED_PIX],
                           &tmp[GREEN_PIX],
                           &tmp[BLUE_PIX],
                           &tmp[ALPHA_PIX]);
1113

1114
      gimp_image_transform_color (gimage, drawable, c, GIMP_RGB, tmp);
1115

1116 1117 1118 1119
      if (GIMP_IMAGE_TYPE_HAS_ALPHA (drawable_type))
        c[GIMP_IMAGE_TYPE_BYTES (drawable_type) - 1] = tmp[ALPHA_PIX];
      else
        c[GIMP_IMAGE_TYPE_BYTES (drawable_type)] = OPAQUE_OPACITY;
1120

1121 1122 1123 1124 1125 1126
      color_region (&destPR, c);
    }
  else
    {
      TempBuf  *pat_buf;
      gboolean  new_buf;
1127

1128 1129
      pat_buf = gimp_image_transform_temp_buf (gimage, drawable,
                                               pattern->mask, &new_buf);
1130

1131
      pattern_region (&destPR, NULL, pat_buf, 0, 0);
1132

1133 1134 1135
      if (new_buf)
        temp_buf_free (pat_buf);
    }
Michael Natterer's avatar
Michael Natterer committed
1136 1137

  gimp_drawable_update (drawable,
Michael Natterer's avatar
Michael Natterer committed
1138 1139 1140
                        0, 0,
                        gimp_item_width  (item),
                        gimp_item_height (item));
Michael Natterer's avatar
Michael Natterer committed
1141 1142 1143 1144
}

void
gimp_drawable_fill_by_type (GimpDrawable *drawable,
Michael Natterer's avatar
Michael Natterer committed
1145 1146
                            GimpContext  *context,
                            GimpFillType  fill_type)
Michael Natterer's avatar
Michael Natterer committed
1147
{
1148 1149
  GimpRGB      color;
  GimpPattern *pattern = NULL;
Michael Natterer's avatar
Michael Natterer committed
1150 1151 1152 1153 1154

  g_return_if_fail (GIMP_IS_DRAWABLE (drawable));

  switch (fill_type)
    {
1155
    case GIMP_FOREGROUND_FILL:
Michael Natterer's avatar
Michael Natterer committed
1156 1157 1158
      gimp_context_get_foreground (context, &color);
      break;

1159
    case GIMP_BACKGROUND_FILL:
Michael Natterer's avatar
Michael Natterer committed
1160 1161 1162
      gimp_context_get_background (context, &color);
      break;

1163
    case GIMP_WHITE_FILL:
1164