gimpchannel.c 60.9 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"

Elliot Lee's avatar
Elliot Lee committed
21
#include <string.h>
22

23
#include <glib-object.h>
Sven Neumann's avatar
Sven Neumann committed
24

25
#include "libgimpcolor/gimpcolor.h"
26

27
#include "core-types.h"
Sven Neumann's avatar
Sven Neumann committed
28

29 30
#include "base/boundary.h"
#include "base/gimplut.h"
31
#include "base/lut-funcs.h"
32 33 34 35 36
#include "base/pixel-processor.h"
#include "base/pixel-region.h"
#include "base/tile.h"
#include "base/tile-manager.h"

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

39
#include "paint/gimppaintcore-stroke.h"
40
#include "paint/gimppaintoptions.h"
41

42
#include "gimp.h"
43
#include "gimp-utils.h"
44
#include "gimpcontainer.h"
45
#include "gimpdrawable-convert.h"
46
#include "gimpimage.h"
47
#include "gimpimage-quick-mask.h"
48
#include "gimpimage-undo.h"
49
#include "gimpimage-undo-push.h"
50
#include "gimpchannel.h"
51
#include "gimpcontext.h"
Michael Natterer's avatar
Michael Natterer committed
52
#include "gimpdrawable-stroke.h"
53
#include "gimpmarshal.h"
54
#include "gimppaintinfo.h"
55
#include "gimppickable.h"
56
#include "gimpstrokedesc.h"
57

58
#include "gimp-intl.h"
59 60


61 62 63 64 65 66 67
enum
{
  COLOR_CHANGED,
  LAST_SIGNAL
};


68
static void gimp_channel_pickable_iface_init (GimpPickableInterface *iface);
69 70 71

static void       gimp_channel_finalize      (GObject          *object);

72 73
static gint64     gimp_channel_get_memsize   (GimpObject       *object,
                                              gint64           *gui_size);
74

75 76 77
static gchar  * gimp_channel_get_description (GimpViewable     *viewable,
                                              gchar           **tooltip);

78
static gboolean   gimp_channel_is_attached   (GimpItem         *item);
79 80 81
static GimpItem * gimp_channel_duplicate     (GimpItem         *item,
                                              GType             new_type,
                                              gboolean          add_alpha);
82 83
static void       gimp_channel_convert       (GimpItem         *item,
                                              GimpImage        *dest_image);
84 85 86 87 88 89 90 91 92
static void       gimp_channel_translate     (GimpItem         *item,
                                              gint              off_x,
                                              gint              off_y,
                                              gboolean          push_undo);
static void       gimp_channel_scale         (GimpItem         *item,
                                              gint              new_width,
                                              gint              new_height,
                                              gint              new_offset_x,
                                              gint              new_offset_y,
93
                                              GimpInterpolationType interp_type,
94
                                              GimpProgress     *progress);
95
static void       gimp_channel_resize        (GimpItem         *item,
96
                                              GimpContext      *context,
97 98 99 100 101
                                              gint              new_width,
                                              gint              new_height,
                                              gint              offx,
                                              gint              offy);
static void       gimp_channel_flip          (GimpItem         *item,
102
                                              GimpContext      *context,
103 104 105 106
                                              GimpOrientationType flip_type,
                                              gdouble           axis,
                                              gboolean          flip_result);
static void       gimp_channel_rotate        (GimpItem         *item,
107
                                              GimpContext      *context,
108 109 110 111 112
                                              GimpRotationType  flip_type,
                                              gdouble           center_x,
                                              gdouble           center_y,
                                              gboolean          flip_result);
static void       gimp_channel_transform     (GimpItem         *item,
113
                                              GimpContext      *context,
114 115 116
                                              const GimpMatrix3 *matrix,
                                              GimpTransformDirection direction,
                                              GimpInterpolationType interpolation_type,
117 118
                                              gboolean          supersample,
                                              gint              recursion_level,
119
                                              gboolean          clip_result,
120
                                              GimpProgress     *progress);
121 122
static gboolean   gimp_channel_stroke        (GimpItem         *item,
                                              GimpDrawable     *drawable,
123
                                              GimpStrokeDesc   *stroke_desc);
124

125 126 127 128
static void gimp_channel_invalidate_boundary   (GimpDrawable       *drawable);
static void gimp_channel_get_active_components (const GimpDrawable *drawable,
                                                gboolean           *active);

129
static void      gimp_channel_apply_region   (GimpDrawable     *drawable,
130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145
                                              PixelRegion      *src2PR,
                                              gboolean          push_undo,
                                              const gchar      *undo_desc,
                                              gdouble           opacity,
                                              GimpLayerModeEffects  mode,
                                              TileManager      *src1_tiles,
                                              gint              x,
                                              gint              y);
static void      gimp_channel_replace_region (GimpDrawable     *drawable,
                                              PixelRegion      *src2PR,
                                              gboolean          push_undo,
                                              const gchar      *undo_desc,
                                              gdouble           opacity,
                                              PixelRegion      *maskPR,
                                              gint              x,
                                              gint              y);
146 147 148 149
static void      gimp_channel_set_tiles      (GimpDrawable     *drawable,
                                              gboolean          push_undo,
                                              const gchar      *undo_desc,
                                              TileManager      *tiles,
150 151 152
                                              GimpImageType     type,
                                              gint              offset_x,
                                              gint              offset_y);
153 154 155 156 157 158 159
static void      gimp_channel_swap_pixels    (GimpDrawable     *drawable,
                                              TileManager      *tiles,
                                              gboolean          sparse,
                                              gint              x,
                                              gint              y,
                                              gint              width,
                                              gint              height);
160

161 162 163 164
static gint      gimp_channel_get_opacity_at (GimpPickable     *pickable,
                                              gint              x,
                                              gint              y);

165 166 167 168 169 170 171 172 173 174 175 176 177 178
static gboolean   gimp_channel_real_boundary (GimpChannel      *channel,
                                              const BoundSeg  **segs_in,
                                              const BoundSeg  **segs_out,
                                              gint             *num_segs_in,
                                              gint             *num_segs_out,
                                              gint              x1,
                                              gint              y1,
                                              gint              x2,
                                              gint              y2);
static gboolean   gimp_channel_real_bounds   (GimpChannel      *channel,
                                              gint             *x1,
                                              gint             *y1,
                                              gint             *x2,
                                              gint             *y2);
179
static gboolean   gimp_channel_real_is_empty (GimpChannel      *channel);
180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208
static void       gimp_channel_real_feather  (GimpChannel      *channel,
                                              gdouble           radius_x,
                                              gdouble           radius_y,
                                              gboolean          push_undo);
static void       gimp_channel_real_sharpen  (GimpChannel      *channel,
                                              gboolean          push_undo);
static void       gimp_channel_real_clear    (GimpChannel      *channel,
                                              const gchar      *undo_desc,
                                              gboolean          push_undo);
static void       gimp_channel_real_all      (GimpChannel      *channel,
                                              gboolean          push_undo);
static void       gimp_channel_real_invert   (GimpChannel      *channel,
                                              gboolean          push_undo);
static void       gimp_channel_real_border   (GimpChannel      *channel,
                                              gint              radius_x,
                                              gint              radius_y,
                                              gboolean          push_undo);
static void       gimp_channel_real_grow     (GimpChannel      *channel,
                                              gint              radius_x,
                                              gint              radius_y,
                                              gboolean          push_undo);
static void       gimp_channel_real_shrink   (GimpChannel      *channel,
                                              gint              radius_x,
                                              gint              radius_y,
                                              gboolean          edge_lock,
                                              gboolean          push_undo);

static void       gimp_channel_validate      (TileManager      *tm,
                                              Tile             *tile);
209

210

211 212 213
G_DEFINE_TYPE_WITH_CODE (GimpChannel, gimp_channel, GIMP_TYPE_DRAWABLE,
                         G_IMPLEMENT_INTERFACE (GIMP_TYPE_PICKABLE,
                                                gimp_channel_pickable_iface_init));
214

215
#define parent_class gimp_channel_parent_class
216

217
static guint channel_signals[LAST_SIGNAL] = { 0 };
218 219 220


static void
221
gimp_channel_class_init (GimpChannelClass *klass)
222
{
223 224 225 226 227
  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);
228

229 230 231 232 233 234 235 236 237
  channel_signals[COLOR_CHANGED] =
    g_signal_new ("color-changed",
		  G_TYPE_FROM_CLASS (klass),
		  G_SIGNAL_RUN_FIRST,
		  G_STRUCT_OFFSET (GimpChannelClass, color_changed),
		  NULL, NULL,
		  gimp_marshal_VOID__VOID,
		  G_TYPE_NONE, 0);

238 239 240 241
  object_class->finalize           = gimp_channel_finalize;

  gimp_object_class->get_memsize   = gimp_channel_get_memsize;

242
  viewable_class->get_description  = gimp_channel_get_description;
243 244
  viewable_class->default_stock_id = "gimp-channel";

245 246
  item_class->is_attached    = gimp_channel_is_attached;
  item_class->duplicate      = gimp_channel_duplicate;
247
  item_class->convert        = gimp_channel_convert;
248 249 250 251 252 253 254 255 256 257 258 259 260 261 262
  item_class->translate      = gimp_channel_translate;
  item_class->scale          = gimp_channel_scale;
  item_class->resize         = gimp_channel_resize;
  item_class->flip           = gimp_channel_flip;
  item_class->rotate         = gimp_channel_rotate;
  item_class->transform      = gimp_channel_transform;
  item_class->stroke         = gimp_channel_stroke;
  item_class->default_name   = _("Channel");
  item_class->rename_desc    = _("Rename Channel");
  item_class->translate_desc = _("Move Channel");
  item_class->scale_desc     = _("Scale Channel");
  item_class->resize_desc    = _("Resize Channel");
  item_class->flip_desc      = _("Flip Channel");
  item_class->rotate_desc    = _("Rotate Channel");
  item_class->transform_desc = _("Transform Channel");
263
  item_class->stroke_desc    = _("Stroke Channel");
264 265 266 267 268

  drawable_class->invalidate_boundary   = gimp_channel_invalidate_boundary;
  drawable_class->get_active_components = gimp_channel_get_active_components;
  drawable_class->apply_region          = gimp_channel_apply_region;
  drawable_class->replace_region        = gimp_channel_replace_region;
269
  drawable_class->set_tiles             = gimp_channel_set_tiles;
270
  drawable_class->swap_pixels           = gimp_channel_swap_pixels;
271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291

  klass->boundary       = gimp_channel_real_boundary;
  klass->bounds         = gimp_channel_real_bounds;
  klass->is_empty       = gimp_channel_real_is_empty;
  klass->feather        = gimp_channel_real_feather;
  klass->sharpen        = gimp_channel_real_sharpen;
  klass->clear          = gimp_channel_real_clear;
  klass->all            = gimp_channel_real_all;
  klass->invert         = gimp_channel_real_invert;
  klass->border         = gimp_channel_real_border;
  klass->grow           = gimp_channel_real_grow;
  klass->shrink         = gimp_channel_real_shrink;

  klass->feather_desc   = _("Feather Channel");
  klass->sharpen_desc   = _("Sharpen Channel");
  klass->clear_desc     = _("Clear Channel");
  klass->all_desc       = _("Fill Channel");
  klass->invert_desc    = _("Invert Channel");
  klass->border_desc    = _("Border Channel");
  klass->grow_desc      = _("Grow Channel");
  klass->shrink_desc    = _("Shrink Channel");
292 293 294 295 296
}

static void
gimp_channel_init (GimpChannel *channel)
{
297
  gimp_rgba_set (&channel->color, 0.0, 0.0, 0.0, GIMP_OPACITY_OPAQUE);
298 299 300 301

  channel->show_masked = FALSE;

  /*  Selection mask variables  */
302
  channel->boundary_known = FALSE;
303 304 305 306
  channel->segs_in        = NULL;
  channel->segs_out       = NULL;
  channel->num_segs_in    = 0;
  channel->num_segs_out   = 0;
307 308
  channel->empty          = FALSE;
  channel->bounds_known   = FALSE;
309 310 311 312
  channel->x1             = 0;
  channel->y1             = 0;
  channel->x2             = 0;
  channel->y2             = 0;
313
}
Elliot Lee's avatar
Elliot Lee committed
314

315
static void
316
gimp_channel_pickable_iface_init (GimpPickableInterface *iface)
317
{
318
  iface->get_opacity_at = gimp_channel_get_opacity_at;
319 320
}

321
static void
322
gimp_channel_finalize (GObject *object)
323
{
324
  GimpChannel *channel = GIMP_CHANNEL (object);
325 326

  if (channel->segs_in)
327 328 329 330 331
    {
      g_free (channel->segs_in);
      channel->segs_in = NULL;
    }

332
  if (channel->segs_out)
333 334 335 336
    {
      g_free (channel->segs_out);
      channel->segs_out = NULL;
    }
337

338
  G_OBJECT_CLASS (parent_class)->finalize (object);
339
}
Elliot Lee's avatar
Elliot Lee committed
340

341
static gint64
342
gimp_channel_get_memsize (GimpObject *object,
343
                          gint64     *gui_size)
344
{
345
  GimpChannel *channel = GIMP_CHANNEL (object);
346

347 348
  *gui_size += channel->num_segs_in  * sizeof (BoundSeg);
  *gui_size += channel->num_segs_out * sizeof (BoundSeg);
349

350
  return GIMP_OBJECT_CLASS (parent_class)->get_memsize (object, gui_size);
351 352
}

353 354 355 356
static gchar *
gimp_channel_get_description (GimpViewable  *viewable,
                              gchar        **tooltip)
{
357
  if (! strcmp (GIMP_IMAGE_QUICK_MASK_NAME,
358 359 360 361 362 363 364 365 366
                gimp_object_get_name (GIMP_OBJECT (viewable))))
    {
      return g_strdup (_("Quick Mask"));
    }

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

367 368 369 370 371 372 373
static gboolean
gimp_channel_is_attached (GimpItem *item)
{
  return (GIMP_IS_IMAGE (item->gimage) &&
          gimp_container_have (item->gimage->channels, GIMP_OBJECT (item)));
}

374 375 376 377 378
static GimpItem *
gimp_channel_duplicate (GimpItem *item,
                        GType     new_type,
                        gboolean  add_alpha)
{
379
  GimpItem *new_item;
380 381 382 383 384 385 386 387 388

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

  if (g_type_is_a (new_type, GIMP_TYPE_CHANNEL))
    add_alpha = FALSE;

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

389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404
  if (GIMP_IS_CHANNEL (new_item))
    {
      GimpChannel *channel     = GIMP_CHANNEL (item);
      GimpChannel *new_channel = GIMP_CHANNEL (new_item);

      new_channel->color        = channel->color;
      new_channel->show_masked  = channel->show_masked;

      /*  selection mask variables  */
      new_channel->bounds_known = channel->bounds_known;
      new_channel->empty        = channel->empty;
      new_channel->x1           = channel->x1;
      new_channel->y1           = channel->y1;
      new_channel->x2           = channel->x2;
      new_channel->y2           = channel->y2;
    }
405 406 407 408

  return new_item;
}

409 410 411 412 413 414 415 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 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489
static void
gimp_channel_convert (GimpItem  *item,
                      GimpImage *dest_image)
{
  GimpChannel       *channel  = GIMP_CHANNEL (item);
  GimpDrawable      *drawable = GIMP_DRAWABLE (item);
  GimpImageBaseType  old_base_type;

  old_base_type = GIMP_IMAGE_TYPE_BASE_TYPE (gimp_drawable_type (drawable));

  if (old_base_type != GIMP_GRAY)
    {
      TileManager   *new_tiles;
      GimpImageType  new_type = GIMP_GRAY_IMAGE;

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

      new_tiles = tile_manager_new (gimp_item_width (item),
                                    gimp_item_height (item),
                                    GIMP_IMAGE_TYPE_BYTES (new_type));

      gimp_drawable_convert_grayscale (drawable, new_tiles, old_base_type);

      gimp_drawable_set_tiles_full (drawable, FALSE, NULL,
                                    new_tiles, new_type,
                                    item->offset_x,
                                    item->offset_y);
      tile_manager_unref (new_tiles);
    }

  if (gimp_drawable_has_alpha (drawable))
    {
      TileManager *new_tiles;
      PixelRegion  srcPR;
      PixelRegion  destPR;
      guchar       bg[1] = { 0 };

      new_tiles = tile_manager_new (gimp_item_width (item),
                                    gimp_item_height (item),
                                    GIMP_IMAGE_TYPE_BYTES (GIMP_GRAY_IMAGE));

      pixel_region_init (&srcPR, drawable->tiles,
                         0, 0,
                         gimp_item_width (item),
                         gimp_item_height (item),
                         FALSE);
      pixel_region_init (&destPR, new_tiles,
                         0, 0,
                         gimp_item_width (item),
                         gimp_item_height (item),
                         TRUE);

      flatten_region (&srcPR, &destPR, bg);

      gimp_drawable_set_tiles_full (drawable, FALSE, NULL,
                                    new_tiles, GIMP_GRAY_IMAGE,
                                    item->offset_x,
                                    item->offset_y);
      tile_manager_unref (new_tiles);
    }

  if (G_TYPE_FROM_INSTANCE (channel) == GIMP_TYPE_CHANNEL)
    {
      gint width  = gimp_image_get_width  (dest_image);
      gint height = gimp_image_get_height (dest_image);

      item->offset_x = 0;
      item->offset_y = 0;

      if (gimp_item_width  (item) != width ||
          gimp_item_height (item) != height)
        {
          gimp_item_resize (item, gimp_get_user_context (dest_image->gimp),
                            width, height, 0, 0);
        }
    }

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

490 491
static void
gimp_channel_translate (GimpItem *item,
492 493
                        gint      off_x,
                        gint      off_y,
494 495
                        gboolean  push_undo)
{
496
  GimpChannel *channel  = GIMP_CHANNEL (item);
497 498 499 500 501 502
  GimpChannel *tmp_mask = NULL;
  gint         width, height;
  PixelRegion  srcPR, destPR;
  guchar       empty = TRANSPARENT_OPACITY;
  gint         x1, y1, x2, y2;

503
  gimp_channel_bounds (channel, &x1, &y1, &x2, &y2);
504

505
  /*  update the old area  */
506
  gimp_drawable_update (GIMP_DRAWABLE (item), x1, y1, x2 - x1, y2 - y1);
507 508

  if (push_undo)
509
    gimp_channel_push_undo (channel, NULL);
510
  else
511 512
    gimp_drawable_invalidate_boundary (GIMP_DRAWABLE (channel));

513 514 515 516 517 518 519
  x1 = CLAMP ((x1 + off_x), 0, GIMP_ITEM (channel)->width);
  y1 = CLAMP ((y1 + off_y), 0, GIMP_ITEM (channel)->height);
  x2 = CLAMP ((x2 + off_x), 0, GIMP_ITEM (channel)->width);
  y2 = CLAMP ((y2 + off_y), 0, GIMP_ITEM (channel)->height);

  width  = x2 - x1;
  height = y2 - y1;
520 521 522 523 524 525 526

  /*  make sure width and height are non-zero  */
  if (width != 0 && height != 0)
    {
      /*  copy the portion of the mask we will keep to a
       *  temporary buffer
       */
527
      tmp_mask = gimp_channel_new_mask (gimp_item_get_image (item),
528
                                        width, height);
529

530
      pixel_region_init (&srcPR, GIMP_DRAWABLE (channel)->tiles,
531
                         x1 - off_x, y1 - off_y, width, height, FALSE);
532
      pixel_region_init (&destPR, GIMP_DRAWABLE (tmp_mask)->tiles,
533
                         0, 0, width, height, TRUE);
534 535 536 537
      copy_region (&srcPR, &destPR);
    }

  /*  clear the mask  */
538
  pixel_region_init (&srcPR, GIMP_DRAWABLE (channel)->tiles,
539 540 541
                     0, 0,
                     GIMP_ITEM (channel)->width,
                     GIMP_ITEM (channel)->height, TRUE);
542 543 544 545 546 547
  color_region (&srcPR, &empty);

  if (width != 0 && height != 0)
    {
      /*  copy the temp mask back to the mask  */
      pixel_region_init (&srcPR, GIMP_DRAWABLE (tmp_mask)->tiles,
548
                         0, 0, width, height, FALSE);
549
      pixel_region_init (&destPR, GIMP_DRAWABLE (channel)->tiles,
550
                         x1, y1, width, height, TRUE);
551 552 553 554 555 556 557 558 559
      copy_region (&srcPR, &destPR);

      /*  free the temporary mask  */
      g_object_unref (tmp_mask);
    }

  /*  calculate new bounds  */
  if (width == 0 || height == 0)
    {
560 561 562 563 564
      channel->empty = TRUE;
      channel->x1    = 0;
      channel->y1    = 0;
      channel->x2    = GIMP_ITEM (channel)->width;
      channel->y2    = GIMP_ITEM (channel)->height;
565 566 567
    }
  else
    {
568 569 570 571
      channel->x1 = x1;
      channel->y1 = y1;
      channel->x2 = x2;
      channel->y2 = y2;
572 573
    }

574 575 576 577 578
  /*  update the new area  */
  gimp_drawable_update (GIMP_DRAWABLE (item),
                        channel->x1, channel->y1,
                        channel->x2 - channel->x1,
                        channel->y2 - channel->y1);
579 580
}

581 582
static void
gimp_channel_scale (GimpItem              *item,
583 584
                    gint                   new_width,
                    gint                   new_height,
585 586
                    gint                   new_offset_x,
                    gint                   new_offset_y,
587
                    GimpInterpolationType  interpolation_type,
588
                    GimpProgress          *progress)
589
{
590 591 592 593 594 595
  if (G_TYPE_FROM_INSTANCE (item) == GIMP_TYPE_CHANNEL)
    {
      new_offset_x = 0;
      new_offset_y = 0;
    }

596 597
  GIMP_ITEM_CLASS (parent_class)->scale (item, new_width, new_height,
                                         new_offset_x, new_offset_y,
598
                                         interpolation_type, progress);
599 600
}

601
static void
602 603 604 605 606 607
gimp_channel_resize (GimpItem    *item,
                     GimpContext *context,
                     gint         new_width,
                     gint         new_height,
                     gint         offset_x,
                     gint         offset_y)
608
{
609
  GIMP_ITEM_CLASS (parent_class)->resize (item, context, new_width, new_height,
610 611
                                          offset_x, offset_y);

612 613 614 615 616
  if (G_TYPE_FROM_INSTANCE (item) == GIMP_TYPE_CHANNEL)
    {
      item->offset_x = 0;
      item->offset_y = 0;
    }
617 618
}

619
static void
620
gimp_channel_flip (GimpItem            *item,
621
                   GimpContext         *context,
622
                   GimpOrientationType  flip_type,
623 624
                   gdouble              axis,
                   gboolean             clip_result)
625
{
626 627
  if (G_TYPE_FROM_INSTANCE (item) == GIMP_TYPE_CHANNEL)
    clip_result = TRUE;
628

629 630
  GIMP_ITEM_CLASS (parent_class)->flip (item, context, flip_type, axis,
                                        clip_result);
631 632
}

633 634
static void
gimp_channel_rotate (GimpItem         *item,
635
                     GimpContext      *context,
636 637 638 639 640
                     GimpRotationType  rotate_type,
                     gdouble           center_x,
                     gdouble           center_y,
                     gboolean          clip_result)
{
641
  /*  don't default to clip_result == TRUE here  */
642

643
  GIMP_ITEM_CLASS (parent_class)->rotate (item, context,
644 645 646 647
                                          rotate_type, center_x, center_y,
                                          clip_result);
}

648 649
static void
gimp_channel_transform (GimpItem               *item,
650
                        GimpContext            *context,
651
                        const GimpMatrix3      *matrix,
652 653
                        GimpTransformDirection  direction,
                        GimpInterpolationType   interpolation_type,
654 655
                        gboolean                supersample,
                        gint                    recursion_level,
656
                        gboolean                clip_result,
657
                        GimpProgress           *progress)
658
{
659 660
  if (G_TYPE_FROM_INSTANCE (item) == GIMP_TYPE_CHANNEL)
    clip_result = TRUE;
661

662
  GIMP_ITEM_CLASS (parent_class)->transform (item, context, matrix, direction,
663 664
                                             interpolation_type,
                                             supersample, recursion_level,
665
                                             clip_result, progress);
666 667
}

668
static gboolean
669 670 671
gimp_channel_stroke (GimpItem       *item,
                     GimpDrawable   *drawable,
                     GimpStrokeDesc *stroke_desc)
672

673
{
674
  GimpChannel    *channel = GIMP_CHANNEL (item);
Michael Natterer's avatar
Michael Natterer committed
675 676 677 678 679
  const BoundSeg *segs_in;
  const BoundSeg *segs_out;
  gint            n_segs_in;
  gint            n_segs_out;
  gboolean        retval = FALSE;
680
  gint            offset_x, offset_y;
681

Michael Natterer's avatar
Michael Natterer committed
682 683
  if (! gimp_channel_boundary (channel, &segs_in, &segs_out,
                               &n_segs_in, &n_segs_out,
684 685 686 687 688 689
                               0, 0, 0, 0))
    {
      g_message (_("Cannot stroke empty channel."));
      return FALSE;
    }

690 691
  gimp_item_offsets (GIMP_ITEM (channel), &offset_x, &offset_y);

692
  switch (stroke_desc->method)
Michael Natterer's avatar
Michael Natterer committed
693
    {
694
    case GIMP_STROKE_METHOD_LIBART:
Michael Natterer's avatar
Michael Natterer committed
695
      gimp_drawable_stroke_boundary (drawable,
696
                                     stroke_desc->stroke_options,
697 698
                                     segs_in, n_segs_in,
                                     offset_x, offset_y);
Michael Natterer's avatar
Michael Natterer committed
699
      retval = TRUE;
700 701 702 703 704 705 706
      break;

    case GIMP_STROKE_METHOD_PAINT_CORE:
      {
        GimpPaintCore *core;

        core = g_object_new (stroke_desc->paint_info->paint_type, NULL);
707

708 709 710 711
        retval = gimp_paint_core_stroke_boundary (core, drawable,
                                                  stroke_desc->paint_options,
                                                  segs_in, n_segs_in,
                                                  offset_x, offset_y);
712

713 714 715
        g_object_unref (core);
      }
      break;
716

717 718
    default:
      g_return_val_if_reached (FALSE);
Michael Natterer's avatar
Michael Natterer committed
719
    }
720 721 722 723

  return retval;
}

724 725 726
static void
gimp_channel_invalidate_boundary (GimpDrawable *drawable)
{
727
  GIMP_CHANNEL (drawable)->boundary_known = FALSE;
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 768 769 770 771 772 773 774 775 776 777 778 779 780 781
static void
gimp_channel_get_active_components (const GimpDrawable *drawable,
                                    gboolean           *active)
{
  /*  Make sure that the alpha channel is not valid.  */
  active[GRAY_PIX]    = TRUE;
  active[ALPHA_G_PIX] = FALSE;
}

static void
gimp_channel_apply_region (GimpDrawable         *drawable,
                           PixelRegion          *src2PR,
                           gboolean              push_undo,
                           const gchar          *undo_desc,
                           gdouble               opacity,
                           GimpLayerModeEffects  mode,
                           TileManager          *src1_tiles,
                           gint                  x,
                           gint                  y)
{
  gimp_drawable_invalidate_boundary (drawable);

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

  GIMP_CHANNEL (drawable)->bounds_known = FALSE;
}

static void
gimp_channel_replace_region (GimpDrawable *drawable,
                             PixelRegion  *src2PR,
                             gboolean      push_undo,
                             const gchar  *undo_desc,
                             gdouble       opacity,
                             PixelRegion  *maskPR,
                             gint          x,
                             gint          y)
{
  gimp_drawable_invalidate_boundary (drawable);

  GIMP_DRAWABLE_CLASS (parent_class)->replace_region (drawable, src2PR,
                                                      push_undo, undo_desc,
                                                      opacity,
                                                      maskPR,
                                                      x, y);

  GIMP_CHANNEL (drawable)->bounds_known = FALSE;
}

782 783 784 785 786
static void
gimp_channel_set_tiles (GimpDrawable *drawable,
                        gboolean      push_undo,
                        const gchar  *undo_desc,
                        TileManager  *tiles,
787 788 789
                        GimpImageType type,
                        gint          offset_x,
                        gint          offset_y)
790 791 792
{
  GIMP_DRAWABLE_CLASS (parent_class)->set_tiles (drawable,
                                                 push_undo, undo_desc,
793 794
                                                 tiles, type,
                                                 offset_x, offset_y);
795 796

  GIMP_CHANNEL (drawable)->bounds_known = FALSE;
797 798
}

799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815
static void
gimp_channel_swap_pixels (GimpDrawable *drawable,
                          TileManager  *tiles,
                          gboolean      sparse,
                          gint          x,
                          gint          y,
                          gint          width,
                          gint          height)
{
  gimp_drawable_invalidate_boundary (drawable);

  GIMP_DRAWABLE_CLASS (parent_class)->swap_pixels (drawable, tiles, sparse,
                                                   x, y, width, height);

  GIMP_CHANNEL (drawable)->bounds_known = FALSE;
}

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
static gint
gimp_channel_get_opacity_at (GimpPickable *pickable,
                             gint          x,
                             gint          y)
{
  GimpChannel *channel = GIMP_CHANNEL (pickable);
  Tile        *tile;
  gint         val;

  /*  Some checks to cut back on unnecessary work  */
  if (channel->bounds_known)
    {
      if (channel->empty)
	return 0;
      else if (x < channel->x1 || x >= channel->x2 ||
               y < channel->y1 || y >= channel->y2)
        return 0;
    }
  else
    {
      if (x < 0 || x >= GIMP_ITEM (channel)->width ||
          y < 0 || y >= GIMP_ITEM (channel)->height)
        return 0;
    }

  tile = tile_manager_get_tile (GIMP_DRAWABLE (channel)->tiles, x, y,
                                TRUE, FALSE);
  val = *(guchar *) (tile_data_pointer (tile, x % TILE_WIDTH, y % TILE_HEIGHT));
  tile_release (tile, FALSE);

  return val;
}

849 850 851 852 853 854 855 856 857 858
static gboolean
gimp_channel_real_boundary (GimpChannel     *channel,
                            const BoundSeg **segs_in,
                            const BoundSeg **segs_out,
                            gint            *num_segs_in,
                            gint            *num_segs_out,
                            gint             x1,
                            gint             y1,
                            gint             x2,
                            gint             y2)
859
{
860 861
  gint        x3, y3, x4, y4;
  PixelRegion bPR;
862

863 864 865 866
  if (! channel->boundary_known)
    {
      /* free the out of date boundary segments */
      if (channel->segs_in)
867
        g_free (channel->segs_in);
868
      if (channel->segs_out)
869
        g_free (channel->segs_out);
870

871
      if (gimp_channel_bounds (channel, &x3, &y3, &x4, &y4))
872 873
        {
          pixel_region_init (&bPR, GIMP_DRAWABLE (channel)->tiles,
874 875
                             x3, y3, x4 - x3, y4 - y3, FALSE);

876 877 878 879
          channel->segs_out = boundary_find (&bPR, BOUNDARY_IGNORE_BOUNDS,
                                             x1, y1, x2, y2,
                                             BOUNDARY_HALF_WAY,
                                             &channel->num_segs_out);
880 881 882 883 884 885 886 887 888 889 890
          x1 = MAX (x1, x3);
          y1 = MAX (y1, y3);
          x2 = MIN (x2, x4);
          y2 = MIN (y2, y4);

          if (x2 > x1 && y2 > y1)
            {
              pixel_region_init (&bPR, GIMP_DRAWABLE (channel)->tiles,
                                 0, 0,
                                 GIMP_ITEM (channel)->width,
                                 GIMP_ITEM (channel)->height, FALSE);
891

892 893 894 895
              channel->segs_in = boundary_find (&bPR, BOUNDARY_WITHIN_BOUNDS,
                                                x1, y1, x2, y2,
                                                BOUNDARY_HALF_WAY,
                                                &channel->num_segs_in);
896 897 898 899 900 901 902
            }
          else
            {
              channel->segs_in     = NULL;
              channel->num_segs_in = 0;
            }
        }
903
      else
904 905 906 907 908 909
        {
          channel->segs_in      = NULL;
          channel->segs_out     = NULL;
          channel->num_segs_in  = 0;
          channel->num_segs_out = 0;
        }
910

911 912
      channel->boundary_known = TRUE;
    }
913

914 915 916 917 918 919
  *segs_in      = channel->segs_in;
  *segs_out     = channel->segs_out;
  *num_segs_in  = channel->num_segs_in;
  *num_segs_out = channel->num_segs_out;

  return TRUE;
920 921
}

922 923 924 925 926 927
static gboolean
gimp_channel_real_bounds (GimpChannel *channel,
                          gint        *x1,
                          gint        *y1,
                          gint        *x2,
                          gint        *y2)
Elliot Lee's avatar
Elliot Lee committed
928
{
929 930 931 932 933 934 935
  PixelRegion  maskPR;
  guchar      *data, *data1;
  gint         x, y;
  gint         ex, ey;
  gint         tx1, tx2, ty1, ty2;
  gint         minx, maxx;
  gpointer     pr;
Elliot Lee's avatar
Elliot Lee committed
936

937 938 939 940 941 942 943
  /*  if the channel's bounds have already been reliably calculated...  */
  if (channel->bounds_known)
    {
      *x1 = channel->x1;
      *y1 = channel->y1;
      *x2 = channel->x2;
      *y2 = channel->y2;
944

945 946
      return ! channel->empty;
    }
947

948 949 950 951 952
  /*  go through and calculate the bounds  */
  tx1 = GIMP_ITEM (channel)->width;
  ty1 = GIMP_ITEM (channel)->height;
  tx2 = 0;
  ty2 = 0;
953

954
  pixel_region_init (&maskPR, GIMP_DRAWABLE (channel)->tiles,
955 956 957
                     0, 0,
                     GIMP_ITEM (channel)->width,
                     GIMP_ITEM (channel)->height, FALSE);
Elliot Lee's avatar
Elliot Lee committed
958

959 960 961 962 963 964 965
  for (pr = pixel_regions_register (1, &maskPR);
       pr != NULL;
       pr = pixel_regions_process (pr))
    {
      data1 = data = maskPR.data;
      ex = maskPR.x + maskPR.w;
      ey = maskPR.y + maskPR.h;
966 967 968 969

      /*  only check the pixels if this tile is not fully within the
       *  currently computed bounds
       */
970
      if (maskPR.x < tx1 || ex > tx2 ||
971
          maskPR.y < ty1 || ey > ty2)
972
        {
973 974 975 976
          /* Check upper left and lower right corners to see if we can
           * avoid checking the rest of the pixels in this tile
           */
          if (data[0] && data[maskPR.rowstride*(maskPR.h - 1) + maskPR.w - 1])
977 978 979 980 981 982 983 984 985 986
            {
              if (maskPR.x < tx1)
                tx1 = maskPR.x;
              if (ex > tx2)
                tx2 = ex;
              if (maskPR.y < ty1)
                ty1 = maskPR.y;
              if (ey > ty2)
                ty2 = ey;
            }
987
          else
988 989 990 991 992 993 994 995 996
            {
              for (y = maskPR.y; y < ey; y++, data1 += maskPR.rowstride)
                {
                  for (x = maskPR.x, data = data1; x < ex; x++, data++)
                    {
                      if (*data)
                        {
                          minx = x;
                          maxx = x;
Elliot Lee's avatar
Elliot Lee committed
997

998 999 1000
                          for (; x < ex; x++, data++)
                            if (*data)
                              maxx = x;
Elliot Lee's avatar
Elliot Lee committed
1001

1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015
                          if (minx < tx1)
                            tx1 = minx;
                          if (maxx > tx2)
                            tx2 = maxx;
                          if (y < ty1)
                            ty1 = y;
                          if (y > ty2)
                            ty2 = y;
                        }
                    }
                }
            }
        }
    }
1016

1017 1018
  tx2 = CLAMP (tx2 + 1, 0, GIMP_ITEM (channel)->width);
  ty2 = CLAMP (ty2 + 1, 0, GIMP_ITEM (channel)->height);
Elliot Lee's avatar
Elliot Lee committed
1019

1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035
  if (tx1 == GIMP_ITEM (channel)->width && ty1 == GIMP_ITEM (channel)->height)
    {
      channel->empty = TRUE;
      channel->x1    = 0;
      channel->y1    = 0;
      channel->x2    = GIMP_ITEM (channel)->width;
      channel->y2    = GIMP_ITEM (channel)->height;
    }
  else
    {
      channel->empty = FALSE;
      channel->x1    = tx1;
      channel->y1    = ty1;
      channel->x2    = tx2;
      channel->y2    = ty2;
    }
Elliot Lee's avatar
Elliot Lee committed
1036

1037 1038 1039 1040 1041 1042 1043 1044
  channel->bounds_known = TRUE;

  *x1 = channel->x1;
  *x2 = channel->x2;
  *y1 = channel->y1;
  *y2 = channel->y2;

  return ! channel->empty;
Elliot Lee's avatar
Elliot Lee committed
1045 1046
}

1047 1048 1049 1050 1051 1052 1053
static gboolean
gimp_channel_real_is_empty (GimpChannel *channel)
{
  PixelRegion  maskPR;
  guchar      *data;
  gint         x, y;
  gpointer     pr;
1054

1055 1056
  if (channel->bounds_known)
    return channel->empty;
1057

1058
  pixel_region_init (&maskPR, GIMP_DRAWABLE (channel)->tiles,
1059 1060 1061
                     0, 0,
                     GIMP_ITEM (channel)->width,
                     GIMP_ITEM (channel)->height, FALSE);
1062

1063 1064 1065 1066 1067 1068
  for (pr = pixel_regions_register (1, &maskPR);
       pr != NULL;
       pr = pixel_regions_process (pr))
    {
      /*  check if any pixel in the channel is non-zero  */
      data = maskPR.data;
1069

1070
      for (y = 0; y < maskPR.h; y++)
1071 1072 1073 1074 1075 1076
        for (x = 0; x < maskPR.w; x++)
          if (*data++)
            {
              pixel_regions_process_stop (pr);
              return FALSE;
            }
1077 1078
    }

1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097
  /*  The mask is empty, meaning we can set the bounds as known  */
  if (channel->segs_in)
    g_free (channel->segs_in);
  if (channel->segs_out)
    g_free (channel->segs_out);

  channel->empty          = TRUE;
  channel->segs_in        = NULL;
  channel->segs_out       = NULL;
  channel->num_segs_in    = 0;
  channel->num_segs_out   = 0;
  channel->bounds_known   = TRUE;
  channel->boundary_known = TRUE;
  channel->x1             = 0;
  channel->y1             = 0;
  channel->x2             = GIMP_ITEM (channel)->width;
  channel->y2             = GIMP_ITEM (channel)->height;

  return TRUE;
1098 1099
}

1100 1101 1102 1103 1104
static void
gimp_channel_real_feather (GimpChannel *channel,
                           gdouble      radius_x,
                           gdouble      radius_y,
                           gboolean     push_undo)
1105
{
1106
  PixelRegion srcPR;
1107

1108
  if (push_undo)
1109 1110 1111 1112
    gimp_channel_push_undo (channel,
                            GIMP_CHANNEL_GET_CLASS (channel)->feather_desc);
  else
    gimp_drawable_invalidate_boundary (GIMP_DRAWABLE (channel));
1113

1114
  pixel_region_init (&srcPR, GIMP_DRAWABLE (channel)->tiles,
1115
                     0, 0,
1116 1117
                     gimp_item_width  (GIMP_ITEM (channel)),
                     gimp_item_height (GIMP_ITEM (channel)),
1118
                     TRUE);
1119
  gaussian_blur_region (&srcPR, radius_x, radius_y);
1120

1121
  channel->bounds_known = FALSE;
1122 1123 1124 1125

  gimp_drawable_update (GIMP_DRAWABLE (channel), 0, 0,
                        gimp_item_width  (GIMP_ITEM (channel)),
                        gimp_item_height (GIMP_ITEM (channel)));
1126
}
1127

1128 1129 1130 1131 1132 1133
static void
gimp_channel_real_sharpen (GimpChannel *channel,
                           gboolean     push_undo)
{
  PixelRegion  maskPR;
  GimpLut     *lut;
1134

1135
  if (push_undo)
1136 1137 1138 1139
    gimp_channel_push_undo (channel,
                            GIMP_CHANNEL_GET_CLASS (channel)->sharpen_desc);
  else
    gimp_drawable_invalidate_boundary (GIMP_DRAWABLE (channel));
1140 1141

  pixel_region_init (&maskPR, GIMP_DRAWABLE (channel)->tiles,
1142
                     0, 0,
1143 1144
                     gimp_item_width  (GIMP_ITEM (channel)),
                     gimp_item_height (GIMP_ITEM (channel)),
1145
                     TRUE);
1146 1147
  lut = threshold_lut_new (0.5, 1);

1148
  pixel_regions_process_parallel ((PixelProcessorFunc) gimp_lut_process_inline,
1149
                                  lut, 1, &maskPR);
1150 1151 1152
  gimp_lut_free (lut);

  channel->bounds_known = FALSE;
1153 1154 1155 1156

  gimp_drawable_update (GIMP_DRAWABLE (channel), 0, 0,
                        gimp_item_width  (GIMP_ITEM (channel)),
                        gimp_item_height (GIMP_ITEM (channel)));
1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169
}

static void
gimp_channel_real_clear (GimpChannel *channel,
                         const gchar *undo_desc,
                         gboolean     push_undo)
{
  PixelRegion maskPR;
  guchar      bg = TRANSPARENT_OPACITY;

  if (push_undo)
    {
      if (! undo_desc)
1170
        undo_desc = GIMP_CHANNEL_GET_CLASS (channel)->clear_desc;
1171 1172 1173

      gimp_channel_push_undo (channel, undo_desc);
    }
1174 1175 1176 1177
  else
    {
      gimp_drawable_invalidate_boundary (GIMP_DRAWABLE (channel));
    }
1178 1179 1180 1181

  if (channel->bounds_known && !channel->empty)
    {
      pixel_region_init (&maskPR, GIMP_DRAWABLE (channel)->tiles,
1182 1183 1184
                         channel->x1, channel->y1,
                         (channel->x2 - channel->x1),
                         (channel->y2 - channel->y1), TRUE);
1185 1186 1187 1188 1189 1190
      color_region (&maskPR, &bg);
    }
  else
    {
      /*  clear the mask  */
      pixel_region_init (&maskPR, GIMP_DRAWABLE (channel)->tiles,
1191 1192 1193
                         0, 0,
                         GIMP_ITEM (channel)->width,
                         GIMP_ITEM (channel)->height, TRUE);
1194 1195 1196 1197 1198 1199 1200 1201 1202 1203
      color_region (&maskPR, &bg);
    }

  /*  we know the bounds  */
  channel->bounds_known = TRUE;
  channel->empty        = TRUE;
  channel->x1           = 0;
  channel->y1           = 0;
  channel->x2           = GIMP_ITEM (channel)->width;
  channel->y2           = GIMP_ITEM (channel)->height;
1204 1205 1206 1207

  gimp_drawable_update (GIMP_DRAWABLE (channel), 0, 0,
                        gimp_item_width  (GIMP_ITEM (channel)),
                        gimp_item_height (GIMP_ITEM (channel)));
1208 1209 1210 1211 1212 1213 1214 1215 1216 1217
}

static void
gimp_channel_real_all (GimpChannel *channel,
                       gboolean     push_undo)
{
  PixelRegion maskPR;
  guchar      bg = OPAQUE_OPACITY;

  if (push_undo)
1218 1219 1220 1221
    gimp_channel_push_undo (channel,
                            GIMP_CHANNEL_GET_CLASS (channel)->all_desc);
  else
    gimp_drawable_invalidate_boundary (GIMP_DRAWABLE (channel));
1222 1223 1224

  /*  clear the channel  */
  pixel_region_init (&maskPR, GIMP_DRAWABLE (channel)->tiles,
1225 1226 1227
                     0, 0,
                     GIMP_ITEM (channel)->width,
                     GIMP_ITEM (channel)->height, TRUE);
1228 1229 1230 1231 1232 1233 1234 1235 1236
  color_region (&maskPR, &bg);

  /*  we know the bounds  */
  channel->bounds_known = TRUE;
  channel->empty        = FALSE;
  channel->x1           = 0;
  channel->y1           = 0;
  channel->x2           = GIMP_ITEM (channel)->width;
  channel->y2           = GIMP_ITEM (channel)->height;
1237 1238 1239 1240

  gimp_drawable_update (GIMP_DRAWABLE (channel), 0, 0,
                        gimp_item_width  (GIMP_ITEM (channel)),
                        gimp_item_height (GIMP_ITEM (channel)));
1241 1242 1243 1244 1245 1246 1247
}

static void
gimp_channel_real_invert (GimpChannel *channel,
                          gboolean     push_undo)
{
  if (push_undo)
1248 1249 1250 1251
    gimp_channel_push_undo (channel,
                            GIMP_CHANNEL_GET_CLASS (channel)->invert_desc);
  else
    gimp_drawable_invalidate_boundary (GIMP_DRAWABLE (channel));
1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268

  if (channel->bounds_known && channel->empty)
    {
      gimp_channel_all (channel, FALSE);
    }
  else
    {
      PixelRegion  maskPR;
      GimpLut     *lut;

      pixel_region_init (&maskPR, GIMP_DRAWABLE (channel)->tiles,
                         0, 0,
                         GIMP_ITEM (channel)->width,
                         GIMP_ITEM (channel)->height, TRUE);

      lut = invert_lut_new (1);

1269 1270 1271
      pixel_regions_process_parallel ((PixelProcessorFunc)
                                      gimp_lut_process_inline,
                                      lut, 1, &maskPR);
1272 1273 1274 1275

      gimp_lut_free (lut);

      channel->bounds_known = FALSE;
1276 1277 1278 1279

      gimp_drawable_update (GIMP_DRAWABLE (channel), 0, 0,
                            gimp_item_width  (GIMP_ITEM (channel)),
                            gimp_item_height (GIMP_ITEM (channel)));
1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319
    }
}

static void
gimp_channel_real_border (GimpChannel *channel,
                          gint         radius_x,
                          gint         radius_y,
                          gboolean     push_undo)
{
  PixelRegion bPR;
  gint        x1, y1, x2, y2;

  if (radius_x < 0 || radius_y < 0)
    return;

  if (! gimp_channel_bounds (channel, &x1, &y1, &x2, &y2))
    return;

  if (gimp_channel_is_empty (channel))
    return;

  if (x1 - radius_x < 0)
    x1 = 0;
  else
    x1 -= radius_x;
  if (x2 + radius_x > GIMP_ITEM (channel)->width)
    x2 = GIMP_ITEM (channel)->width;
  else
    x2 += radius_x;

  if (y1 - radius_y < 0)
    y1 = 0;
  else
    y1 -= radius_y;
  if (y2 + radius_y > GIMP_ITEM (channel)->height)
    y2 = GIMP_ITEM (channel)->height;
  else
    y2 += radius_y;

  if (push_undo)
1320 1321 1322 1323
    gimp_channel_push_undo (channel,
                            GIMP_CHANNEL_GET_CLASS (channel)->border_desc);
  else
    gimp_drawable_invalidate_boundary (GIMP_DRAWABLE (channel));
1324 1325 1326 1327 1328 1329 1330

  pixel_region_init (&bPR, GIMP_DRAWABLE (channel)->tiles, x1, y1,
		     (x2-x1), (y2-y1), TRUE);

  border_region (&bPR, radius_x, radius_y);

  channel->bounds_known = FALSE;
1331 1332 1333 1334

  gimp_drawable_update (GIMP_DRAWABLE (channel), 0, 0,
                        gimp_item_width  (GIMP_ITEM (channel)),
                        gimp_item_height (GIMP_ITEM (channel)));
1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381
}

static void
gimp_channel_real_grow (GimpChannel *channel,
                        gint         radius_x,
                        gint         radius_y,
                        gboolean     push_undo)
{
  PixelRegion bPR;
  gint        x1, y1, x2, y2;

  if (radius_x == 0 && radius_y == 0)
    return;

  if (radius_x <= 0 && radius_y <= 0)
    {
      gimp_channel_shrink (channel, -radius_x, -radius_y, FALSE, push_undo);
      return;
    }

  if (radius_x < 0 || radius_y < 0)
    return;

  if (! gimp_channel_bounds (channel, &x1, &y1, &x2, &y2))
    return;

  if (gimp_channel_is_empty (channel))
    return;

  if (x1 - radius_x > 0)
    x1 = x1 - radius_x;
  else
    x1 = 0;
  if (y1 - radius_y > 0)
    y1 = y1 - radius_y;
  else
    y1 = 0;
  if (x2 + radius_x < GIMP_ITEM (channel)->width)
    x2 = x2 + radius_x;
  else
    x2 = GIMP_ITEM (channel)->width;
  if (y2 + radius_y < GIMP_ITEM (channel)->height)
    y2 = y2 + radius_y;
  else
    y2 = GIMP_ITEM (channel)->height;

  if (push_undo)
1382 1383 1384 1385
    gimp_channel_push_undo (channel,
                            GIMP_CHANNEL_GET_CLASS (channel)->grow_desc);
  else
    gimp_drawable_invalidate_boundary (GIMP_DRAWABLE (channel));
1386 1387

  /*  need full extents for grow, not! */
1388 1389
  pixel_region_init (&bPR, GIMP_DRAWABLE (channel)->tiles,
                     x1, y1, (x2 - x1), (y2 - y1), TRUE);
1390 1391 1392 1393

  fatten_region (&bPR, radius_x, radius_y);

  channel->bounds_known = FALSE;
1394 1395 1396 1397

  gimp_drawable_update (GIMP_DRAWABLE (channel), 0, 0,
                        gimp_item_width  (GIMP_ITEM (channel)),
                        gimp_item_height (GIMP_ITEM (channel)));
1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437
}

static void
gimp_channel_real_shrink (GimpChannel *channel,
                          gint         radius_x,
                          gint         radius_y,
                          gboolean     edge_lock,
                          gboolean     push_undo)
{
  PixelRegion bPR;
  gint        x1, y1, x2, y2;

  if (radius_x == 0 && radius_y == 0)
    return;

  if (radius_x <= 0 && radius_y <= 0)
    {
      gimp_channel_grow (channel, -radius_x, -radius_y, push_undo);
      return;
    }

  if (radius_x < 0 || radius_y < 0)
    return;

  if (! gimp_channel_bounds (channel, &x1, &y1, &x2, &y2))
    return;

  if (gimp_channel_is_empty (channel))
    return;

  if (x1 > 0)
    x1--;
  if (y1 > 0)
    y1--;
  if (x2 < GIMP_ITEM (channel)->width)
    x2++;
  if (y2 < GIMP_ITEM (channel)->height)
    y2++;

  if (push_undo)
1438 1439 1440 1441
    gimp_channel_push_undo (channel,
                            GIMP_CHANNEL_GET_CLASS (channel)->shrink_desc);
  else
    gimp_drawable_invalidate_boundary (GIMP_DRAWABLE (channel));
1442

1443 1444
  pixel_region_init (&bPR, GIMP_DRAWABLE (channel)->tiles,
                     x1, y1, (x2 - x1), (y2 - y1), TRUE);
1445 1446 1447 1448

  thin_region (&bPR, radius_x, radius_y, edge_lock);

  channel->bounds_known = FALSE;
1449 1450 1451 1452

  gimp_drawable_update (GIMP_DRAWABLE (channel), 0, 0,
                        gimp_item_width  (GIMP_ITEM (channel)),
                        gimp_item_height (GIMP_ITEM (channel)));
1453 1454 1455 1456
}

static void
gimp_channel_validate (TileManager *tm,
1457
                       Tile        *tile)
1458 1459 1460
{
  /*  Set the contents of the tile to empty  */
  memset (tile_data_pointer (tile, 0, 0),
1461
          TRANSPARENT_OPACITY, tile_size (tile));
1462 1463 1464 1465 1466 1467 1468
}


/*  public functions  */

GimpChannel *
gimp_channel_new (GimpImage     *gimage,
1469 1470 1471 1472
                  gint           width,
                  gint           height,
                  const gchar   *name,
                  const GimpRGB *color)
1473 1474 1475 1476 1477 1478 1479 1480
{
  GimpChannel *channel;

  g_return_val_if_fail (GIMP_IS_IMAGE (gimage), NULL);

  channel = g_object_new (GIMP_TYPE_CHANNEL, NULL);

  gimp_drawable_configure (GIMP_DRAWABLE (channel),
1481
                           gimage,
1482 1483 1484
                           0, 0, width, height,
                           GIMP_GRAY_IMAGE, name);

1485 1486 1487
  if (color)
    channel->color = *color;

1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498
  channel->show_masked = TRUE;

  /*  selection mask variables  */
  channel->x2          = width;
  channel->y2          = height;

  return channel;
}

GimpChannel *
gimp_channel_new_from_alpha (GimpImage     *gimage,
1499
                             GimpDrawable  *drawable,
1500 1501 1502 1503 1504 1505
                             const gchar   *name,
                             const GimpRGB *color)
{
  GimpChannel *channel;
  gint         width;
  gint         height;
1506
  PixelRegion  srcPR, destPR;
1507 1508

  g_return_val_if_fail (GIMP_IS_IMAGE (gimage), NULL);
1509 1510
  g_return_val_if_fail (GIMP_IS_DRAWABLE (drawable), NULL);
  g_return_val_if_fail (gimp_drawable_has_alpha (drawable), NULL);
1511

1512 1513
  width  = gimp_item_width  (GIMP_ITEM (drawable));
  height = gimp_item_height (GIMP_ITEM (drawable));
1514 1515 1516 1517 1518

  channel = gimp_channel_new (gimage, width, height, name, color);

  gimp_channel_clear (channel, NULL, FALSE);

1519
  pixel_region_init (&srcPR, gimp_drawable_data (drawable),
1520 1521 1522
                     0, 0, width, height, FALSE);
  pixel_region_init (&destPR, gimp_drawable_data (GIMP_DRAWABLE (channel)),
                     0, 0, width, height, TRUE);
1523

1524
  extract_alpha_region (&srcPR, NULL, &destPR);
1525

1526
  channel->bounds_known = FALSE;
1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550

  return channel;
}

GimpChannel *
gimp_channel_new_from_component (GimpImage       *gimage,
                                 GimpChannelType  type,
                                 const gchar     *name,
                                 const GimpRGB   *color)
{
  GimpChannel *channel;
  TileManager *projection;
  PixelRegion  src;
  PixelRegion  dest;
  gint         width;
  gint         height;
  gint         pixel;

  g_return_val_if_fail (GIMP_IS_IMAGE (gimage), NULL);

  pixel = gimp_image_get_component_index (gimage, type);

  g_return_val_if_fail (pixel != -1, NULL);

1551 1552 1553
  gimp_pickable_flush (GIMP_PICKABLE (gimage->projection));

  projection = gimp_pickable_get_tiles (GIMP_PICKABLE (gimage->projection));
1554 1555 1556 1557
  width  = tile_manager_width  (projection);
  height = tile_manager_height (projection);

  channel = gimp_channel_new (gimage, width, height, name, color);