gimpchannel.c 61.7 KB
Newer Older
1
/* GIMP - The GNU Image Manipulation Program
Elliot Lee's avatar
Elliot Lee committed
2 3 4 5 6 7 8 9 10 11 12 13 14 15
 * 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

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

Michael Natterer's avatar
Michael Natterer committed
29 30
#include "base/boundary.h"
#include "base/gimplut.h"
31
#include "base/lut-funcs.h"
Michael Natterer's avatar
Michael Natterer committed
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"
Michael Natterer's avatar
Michael Natterer committed
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
                                              gint              recursion_level,
118
                                              GimpTransformResize clip_result,
119
                                              GimpProgress     *progress);
120 121
static gboolean   gimp_channel_stroke        (GimpItem         *item,
                                              GimpDrawable     *drawable,
122 123
                                              GimpStrokeDesc   *stroke_desc,
                                              GimpProgress     *progress);
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
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,
196
                                              gboolean          feather,
197
                                              gboolean          edge_lock,
198 199 200 201 202 203 204 205 206 207 208
                                              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);

209
static void       gimp_channel_validate_tile (TileManager      *tm,
210
                                              Tile             *tile);
211

212

213 214
G_DEFINE_TYPE_WITH_CODE (GimpChannel, gimp_channel, GIMP_TYPE_DRAWABLE,
                         G_IMPLEMENT_INTERFACE (GIMP_TYPE_PICKABLE,
215
                                                gimp_channel_pickable_iface_init))
216

217
#define parent_class gimp_channel_parent_class
218

219
static guint channel_signals[LAST_SIGNAL] = { 0 };
220 221 222


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

231 232
  channel_signals[COLOR_CHANGED] =
    g_signal_new ("color-changed",
233 234 235 236 237 238
                  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);
239

240 241 242 243
  object_class->finalize           = gimp_channel_finalize;

  gimp_object_class->get_memsize   = gimp_channel_get_memsize;

244
  viewable_class->get_description  = gimp_channel_get_description;
245 246
  viewable_class->default_stock_id = "gimp-channel";

247 248
  item_class->is_attached    = gimp_channel_is_attached;
  item_class->duplicate      = gimp_channel_duplicate;
249
  item_class->convert        = gimp_channel_convert;
250 251 252 253 254 255 256 257 258 259 260 261 262 263 264
  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");
265
  item_class->stroke_desc    = _("Stroke Channel");
266 267 268 269 270

  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;
271
  drawable_class->set_tiles             = gimp_channel_set_tiles;
272
  drawable_class->swap_pixels           = gimp_channel_swap_pixels;
273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293

  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");
294 295 296 297 298
}

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

  channel->show_masked = FALSE;

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

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

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

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

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

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

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

349 350
  *gui_size += channel->num_segs_in  * sizeof (BoundSeg);
  *gui_size += channel->num_segs_out * sizeof (BoundSeg);
351

352
  return GIMP_OBJECT_CLASS (parent_class)->get_memsize (object, gui_size);
353 354
}

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

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

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

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

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

391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406
  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;
    }
407 408 409 410

  return new_item;
}

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 490 491
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);
}

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

505
  gimp_channel_bounds (channel, &x1, &y1, &x2, &y2);
506

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

  if (push_undo)
511
    gimp_channel_push_undo (channel, NULL);
512
  else
513 514
    gimp_drawable_invalidate_boundary (GIMP_DRAWABLE (channel));

515 516 517 518 519 520 521
  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;
522 523 524 525 526 527 528

  /*  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
       */
529
      tmp_mask = gimp_channel_new_mask (gimp_item_get_image (item),
530
                                        width, height);
531

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

  /*  clear the mask  */
540
  pixel_region_init (&srcPR, GIMP_DRAWABLE (channel)->tiles,
541 542 543
                     0, 0,
                     GIMP_ITEM (channel)->width,
                     GIMP_ITEM (channel)->height, TRUE);
544 545 546 547 548 549
  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,
550
                         0, 0, width, height, FALSE);
551
      pixel_region_init (&destPR, GIMP_DRAWABLE (channel)->tiles,
552
                         x1, y1, width, height, TRUE);
553 554 555 556 557 558 559 560 561
      copy_region (&srcPR, &destPR);

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

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

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

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

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

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

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

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

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

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

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

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

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

670
static gboolean
671 672
gimp_channel_stroke (GimpItem       *item,
                     GimpDrawable   *drawable,
673 674
                     GimpStrokeDesc *stroke_desc,
                     GimpProgress   *progress)
675

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

Michael Natterer's avatar
Michael Natterer committed
685 686
  if (! gimp_channel_boundary (channel, &segs_in, &segs_out,
                               &n_segs_in, &n_segs_out,
687 688
                               0, 0, 0, 0))
    {
689
      gimp_message (gimp_item_get_image (item)->gimp, G_OBJECT (progress),
690 691
                    GIMP_MESSAGE_WARNING,
                    _("Cannot stroke empty channel."));
692 693 694
      return FALSE;
    }

695 696
  gimp_item_offsets (GIMP_ITEM (channel), &offset_x, &offset_y);

697
  switch (stroke_desc->method)
Michael Natterer's avatar
Michael Natterer committed
698
    {
699
    case GIMP_STROKE_METHOD_LIBART:
Michael Natterer's avatar
Michael Natterer committed
700
      gimp_drawable_stroke_boundary (drawable,
701
                                     stroke_desc->stroke_options,
Simon Budig's avatar
Simon Budig committed
702 703
                                     segs_in, n_segs_in,
                                     offset_x, offset_y);
Michael Natterer's avatar
Michael Natterer committed
704
      retval = TRUE;
705 706 707 708 709 710 711
      break;

    case GIMP_STROKE_METHOD_PAINT_CORE:
      {
        GimpPaintCore *core;

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

713 714 715 716
        retval = gimp_paint_core_stroke_boundary (core, drawable,
                                                  stroke_desc->paint_options,
                                                  segs_in, n_segs_in,
                                                  offset_x, offset_y);
717

718 719 720
        g_object_unref (core);
      }
      break;
721

722 723
    default:
      g_return_val_if_reached (FALSE);
Michael Natterer's avatar
Michael Natterer committed
724
    }
725 726 727 728

  return retval;
}

729 730 731
static void
gimp_channel_invalidate_boundary (GimpDrawable *drawable)
{
732
  GIMP_CHANNEL (drawable)->boundary_known = FALSE;
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 782 783 784 785 786
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;
}

787 788 789 790 791
static void
gimp_channel_set_tiles (GimpDrawable *drawable,
                        gboolean      push_undo,
                        const gchar  *undo_desc,
                        TileManager  *tiles,
792 793 794
                        GimpImageType type,
                        gint          offset_x,
                        gint          offset_y)
795 796 797
{
  GIMP_DRAWABLE_CLASS (parent_class)->set_tiles (drawable,
                                                 push_undo, undo_desc,
798 799
                                                 tiles, type,
                                                 offset_x, offset_y);
800 801

  GIMP_CHANNEL (drawable)->bounds_known = FALSE;
802 803
}

804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820
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;
}

821 822 823 824 825 826 827 828 829 830 831 832 833
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)
834
        return 0;
835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853
      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;
}

854 855 856 857 858 859 860 861 862 863
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)
864
{
865 866
  gint         x3, y3, x4, y4;
  PixelRegion  bPR;
867

868 869 870
  if (! channel->boundary_known)
    {
      /* free the out of date boundary segments */
871 872
      g_free (channel->segs_in);
      g_free (channel->segs_out);
873

874
      if (gimp_channel_bounds (channel, &x3, &y3, &x4, &y4))
875 876
        {
          pixel_region_init (&bPR, GIMP_DRAWABLE (channel)->tiles,
877 878
                             x3, y3, x4 - x3, y4 - y3, FALSE);

879 880 881 882
          channel->segs_out = boundary_find (&bPR, BOUNDARY_IGNORE_BOUNDS,
                                             x1, y1, x2, y2,
                                             BOUNDARY_HALF_WAY,
                                             &channel->num_segs_out);
883 884 885 886 887 888 889 890 891 892 893
          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);
894

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

914 915
      channel->boundary_known = TRUE;
    }
916

917 918 919 920 921
  *segs_in      = channel->segs_in;
  *segs_out     = channel->segs_out;
  *num_segs_in  = channel->num_segs_in;
  *num_segs_out = channel->num_segs_out;

922
  return (! channel->empty);
923 924
}

925 926 927 928 929 930
static gboolean
gimp_channel_real_bounds (GimpChannel *channel,
                          gint        *x1,
                          gint        *y1,
                          gint        *x2,
                          gint        *y2)
Elliot Lee's avatar
Elliot Lee committed
931
{
932 933 934 935 936 937 938
  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
939

940 941 942 943 944 945 946
  /*  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;
947

948 949
      return ! channel->empty;
    }
950

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

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

962 963 964 965 966 967 968
  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;
969 970 971 972

      /*  only check the pixels if this tile is not fully within the
       *  currently computed bounds
       */
973
      if (maskPR.x < tx1 || ex > tx2 ||
974
          maskPR.y < ty1 || ey > ty2)
975
        {
976 977 978 979
          /* 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])
980 981 982 983 984 985 986 987 988 989
            {
              if (maskPR.x < tx1)
                tx1 = maskPR.x;
              if (ex > tx2)
                tx2 = ex;
              if (maskPR.y < ty1)
                ty1 = maskPR.y;
              if (ey > ty2)
                ty2 = ey;
            }
990
          else
991 992 993 994 995 996 997 998 999
            {
              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
1000

1001 1002 1003
                          for (; x < ex; x++, data++)
                            if (*data)
                              maxx = x;
Elliot Lee's avatar
Elliot Lee committed
1004

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

1020 1021
  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
1022

1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038
  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
1039

1040 1041 1042 1043 1044 1045 1046 1047
  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
1048 1049
}

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

1058 1059
  if (channel->bounds_known)
    return channel->empty;
1060

1061
  pixel_region_init (&maskPR, GIMP_DRAWABLE (channel)->tiles,
1062 1063 1064
                     0, 0,
                     GIMP_ITEM (channel)->width,
                     GIMP_ITEM (channel)->height, FALSE);
1065

1066 1067 1068 1069 1070 1071
  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;
1072

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

1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100
  /*  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;
1101 1102
}

1103 1104 1105 1106 1107
static void
gimp_channel_real_feather (GimpChannel *channel,
                           gdouble      radius_x,
                           gdouble      radius_y,
                           gboolean     push_undo)
1108
{
1109
  PixelRegion srcPR;
1110

1111
  if (push_undo)
1112 1113 1114 1115
    gimp_channel_push_undo (channel,
                            GIMP_CHANNEL_GET_CLASS (channel)->feather_desc);
  else
    gimp_drawable_invalidate_boundary (GIMP_DRAWABLE (channel));
1116

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

1124
  channel->bounds_known = FALSE;
1125 1126 1127 1128

  gimp_drawable_update (GIMP_DRAWABLE (channel), 0, 0,
                        gimp_item_width  (GIMP_ITEM (channel)),
                        gimp_item_height (GIMP_ITEM (channel)));
1129
}
1130

1131 1132 1133 1134 1135 1136
static void
gimp_channel_real_sharpen (GimpChannel *channel,
                           gboolean     push_undo)
{
  PixelRegion  maskPR;
  GimpLut     *lut;
1137

1138
  if (push_undo)
1139 1140 1141 1142
    gimp_channel_push_undo (channel,
                            GIMP_CHANNEL_GET_CLASS (channel)->sharpen_desc);
  else
    gimp_drawable_invalidate_boundary (GIMP_DRAWABLE (channel));
1143 1144

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

1151
  pixel_regions_process_parallel ((PixelProcessorFunc) gimp_lut_process_inline,
1152
                                  lut, 1, &maskPR);
1153 1154 1155
  gimp_lut_free (lut);

  channel->bounds_known = FALSE;
1156 1157 1158 1159

  gimp_drawable_update (GIMP_DRAWABLE (channel), 0, 0,
                        gimp_item_width  (GIMP_ITEM (channel)),
                        gimp_item_height (GIMP_ITEM (channel)));
1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172
}

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)
1173
        undo_desc = GIMP_CHANNEL_GET_CLASS (channel)->clear_desc;
1174 1175 1176

      gimp_channel_push_undo (channel, undo_desc);
    }
1177 1178 1179 1180
  else
    {
      gimp_drawable_invalidate_boundary (GIMP_DRAWABLE (channel));
    }
1181 1182 1183 1184

  if (channel->bounds_known && !channel->empty)
    {
      pixel_region_init (&maskPR, GIMP_DRAWABLE (channel)->tiles,
1185 1186 1187
                         channel->x1, channel->y1,
                         (channel->x2 - channel->x1),
                         (channel->y2 - channel->y1), TRUE);
1188 1189 1190 1191 1192 1193
      color_region (&maskPR, &bg);
    }
  else
    {
      /*  clear the mask  */
      pixel_region_init (&maskPR, GIMP_DRAWABLE (channel)->tiles,
1194 1195 1196
                         0, 0,
                         GIMP_ITEM (channel)->width,
                         GIMP_ITEM (channel)->height, TRUE);
1197 1198 1199 1200 1201 1202 1203 1204 1205 1206
      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;
1207 1208 1209 1210

  gimp_drawable_update (GIMP_DRAWABLE (channel), 0, 0,
                        gimp_item_width  (GIMP_ITEM (channel)),
                        gimp_item_height (GIMP_ITEM (channel)));
1211 1212 1213 1214 1215 1216 1217 1218 1219 1220
}

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

  if (push_undo)
1221 1222 1223 1224
    gimp_channel_push_undo (channel,
                            GIMP_CHANNEL_GET_CLASS (channel)->all_desc);
  else
    gimp_drawable_invalidate_boundary (GIMP_DRAWABLE (channel));
1225 1226 1227

  /*  clear the channel  */
  pixel_region_init (&maskPR, GIMP_DRAWABLE (channel)->tiles,
1228 1229 1230
                     0, 0,
                     GIMP_ITEM (channel)->width,
                     GIMP_ITEM (channel)->height, TRUE);
1231 1232 1233 1234 1235 1236 1237 1238 1239
  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;
1240 1241 1242 1243

  gimp_drawable_update (GIMP_DRAWABLE (channel), 0, 0,
                        gimp_item_width  (GIMP_ITEM (channel)),
                        gimp_item_height (GIMP_ITEM (channel)));
1244 1245 1246 1247 1248 1249 1250
}

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

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

1272 1273 1274
      pixel_regions_process_parallel ((PixelProcessorFunc)
                                      gimp_lut_process_inline,
                                      lut, 1, &maskPR);
1275 1276 1277 1278

      gimp_lut_free (lut);

      channel->bounds_known = FALSE;