gimpchannel.c 39.8 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 21 22 23 24
 */
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include "appenv.h"
#include "channel.h"
#include "drawable.h"
#include "errors.h"
25
#include "gdisplay.h"
Elliot Lee's avatar
Elliot Lee committed
26 27 28
#include "gimage_mask.h"
#include "layer.h"
#include "paint_funcs.h"
29
#include "parasitelist.h"
Elliot Lee's avatar
Elliot Lee committed
30 31
#include "temp_buf.h"
#include "undo.h"
32
#include "gimppreviewcache.h"
Elliot Lee's avatar
Elliot Lee committed
33

34 35
#include "libgimp/gimpintl.h"

36
#include "channel_pvt.h"
scott's avatar
scott committed
37
#include "tile.h"
38

39 40 41
#include "gimplut.h"
#include "lut_funcs.h"

scott's avatar
scott committed
42
/*
43 44 45
enum {
  LAST_SIGNAL
};
scott's avatar
scott committed
46
*/
47 48 49 50 51

static void gimp_channel_class_init (GimpChannelClass *klass);
static void gimp_channel_init       (GimpChannel      *channel);
static void gimp_channel_destroy    (GtkObject        *object);

scott's avatar
scott committed
52
/*
53
static gint channel_signals[LAST_SIGNAL] = { 0 };
scott's avatar
scott committed
54
*/
55 56 57

static GimpDrawableClass *parent_class = NULL;

58
GtkType
59 60
gimp_channel_get_type ()
{
61
  static GtkType channel_type = 0;
62 63 64 65 66 67 68 69 70 71

  if (!channel_type)
    {
      GtkTypeInfo channel_info =
      {
	"GimpChannel",
	sizeof (GimpChannel),
	sizeof (GimpChannelClass),
	(GtkClassInitFunc) gimp_channel_class_init,
	(GtkObjectInitFunc) gimp_channel_init,
72 73
        /* reserved_1 */ NULL,
	/* reserved_2 */ NULL,
74
	(GtkClassInitFunc) NULL,
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
      };

      channel_type = gtk_type_unique (gimp_drawable_get_type (), &channel_info);
    }

  return channel_type;
}

static void
gimp_channel_class_init (GimpChannelClass *class)
{
  GtkObjectClass *object_class;

  object_class = (GtkObjectClass*) class;
  parent_class = gtk_type_class (gimp_drawable_get_type ());

scott's avatar
scott committed
91
  /*
92
  gtk_object_class_add_signals (object_class, channel_signals, LAST_SIGNAL);
scott's avatar
scott committed
93
  */
94 95 96 97 98 99 100 101

  object_class->destroy = gimp_channel_destroy;
}

static void
gimp_channel_init (GimpChannel *channel)
{
}
Elliot Lee's avatar
Elliot Lee committed
102

103 104 105
/**********************/
/*  Static variables  */
/**********************/
Elliot Lee's avatar
Elliot Lee committed
106

107
static gint channel_get_count = 0;
Elliot Lee's avatar
Elliot Lee committed
108 109 110

/**************************/
/*  Function definitions  */
111
/**************************/
Elliot Lee's avatar
Elliot Lee committed
112 113

static void
114 115
channel_validate (TileManager *tm,
		  Tile        *tile)
Elliot Lee's avatar
Elliot Lee committed
116 117
{
  /*  Set the contents of the tile to empty  */
scott's avatar
scott committed
118
  memset (tile_data_pointer (tile, 0, 0), 
119
	  TRANSPARENT_OPACITY, tile_size (tile));
Elliot Lee's avatar
Elliot Lee committed
120 121 122
}

Channel *
123 124 125 126 127 128
channel_new (GimpImage *gimage,
	     gint       width,
	     gint       height,
	     gchar     *name,
	     gint       opacity,
	     guchar    *col)
Elliot Lee's avatar
Elliot Lee committed
129 130
{
  Channel * channel;
131
  gint i;
Elliot Lee's avatar
Elliot Lee committed
132

133
  channel = gtk_type_new (gimp_channel_get_type ());
Elliot Lee's avatar
Elliot Lee committed
134

135
  gimp_drawable_configure (GIMP_DRAWABLE (channel), 
136
			   gimage, width, height, GRAY_GIMAGE, name);
Elliot Lee's avatar
Elliot Lee committed
137 138 139 140

  /*  set the channel color and opacity  */
  for (i = 0; i < 3; i++)
    channel->col[i] = col[i];
141 142 143

  channel->opacity     = opacity;
  channel->show_masked = TRUE;
Elliot Lee's avatar
Elliot Lee committed
144 145

  /*  selection mask variables  */
146 147 148 149 150 151
  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;
Elliot Lee's avatar
Elliot Lee committed
152
  channel->boundary_known = TRUE;
153 154 155 156
  channel->x1             = 0;
  channel->y1             = 0;
  channel->x2             = width;
  channel->y2             = height;
Elliot Lee's avatar
Elliot Lee committed
157 158 159 160

  return channel;
}

scott's avatar
scott committed
161 162 163 164 165
Channel *
channel_ref (Channel *channel)
{
  gtk_object_ref  (GTK_OBJECT (channel));
  gtk_object_sink (GTK_OBJECT (channel));
166

scott's avatar
scott committed
167 168 169 170 171 172 173 174 175
  return channel;
}

void
channel_unref (Channel *channel)
{
  gtk_object_unref (GTK_OBJECT (channel));
}

Elliot Lee's avatar
Elliot Lee committed
176 177 178
Channel *
channel_copy (Channel *channel)
{
179 180
  gchar *channel_name;
  Channel *new_channel;
Elliot Lee's avatar
Elliot Lee committed
181
  PixelRegion srcPR, destPR;
182 183 184 185
  gchar *ext;
  gint number;
  gchar *name;
  gint len;
Elliot Lee's avatar
Elliot Lee committed
186 187

  /*  formulate the new channel name  */
188 189
  name = channel_get_name (channel);
  ext = strrchr (name, '#');
190
  len = strlen (_("copy"));
191 192 193 194
  if ((strlen (name) >= len &&
       strcmp (&name[strlen (name) - len], _("copy")) == 0) ||
      (ext && (number = atoi (ext + 1)) > 0 && 
       ((int)(log10 (number) + 1)) == strlen (ext + 1)))
195 196
    /* don't have redundant "copy"s */
    channel_name = g_strdup (name);
197
  else
198
    channel_name = g_strdup_printf (_("%s copy"), name);
Elliot Lee's avatar
Elliot Lee committed
199 200

  /*  allocate a new channel object  */
201 202 203
  new_channel = channel_new (GIMP_DRAWABLE (channel)->gimage, 
			     GIMP_DRAWABLE (channel)->width, 
			     GIMP_DRAWABLE (channel)->height, 
204
			     channel_name, channel->opacity, channel->col);
205
  GIMP_DRAWABLE (new_channel)->visible = GIMP_DRAWABLE (channel)->visible;
Elliot Lee's avatar
Elliot Lee committed
206 207 208
  new_channel->show_masked = channel->show_masked;

  /*  copy the contents across channels  */
209 210 211 212 213 214 215
  pixel_region_init (&srcPR, GIMP_DRAWABLE (channel)->tiles, 0, 0, 
		     GIMP_DRAWABLE (channel)->width, 
		     GIMP_DRAWABLE (channel)->height, FALSE);
  pixel_region_init (&destPR, GIMP_DRAWABLE (new_channel)->tiles,
		     0, 0,
		     GIMP_DRAWABLE (channel)->width,
		     GIMP_DRAWABLE (channel)->height, TRUE);
Elliot Lee's avatar
Elliot Lee committed
216 217
  copy_region (&srcPR, &destPR);

218
  /* copy the parasites */
219 220
  GIMP_DRAWABLE (new_channel)->parasites 
    = parasite_list_copy (GIMP_DRAWABLE (channel)->parasites);
221

Elliot Lee's avatar
Elliot Lee committed
222 223 224 225 226 227
  /*  free up the channel_name memory  */
  g_free (channel_name);

  return new_channel;
}

228
void
229 230
channel_set_name (Channel *channel,
		  gchar   *name)
231
{
232
  gimp_drawable_set_name (GIMP_DRAWABLE (channel), name);
233 234
}

235
gchar *
236 237
channel_get_name (Channel *channel)
{
238
  return gimp_drawable_get_name (GIMP_DRAWABLE (channel));
239 240
}

241
void 
242 243
channel_set_color (Channel *channel,
		   gchar   *color)
244
{
245 246
  gint i;

247 248
  if (color)
    {  
249 250
      for (i = 0; i < 3; i++)
	channel->col[i] = color[i];
251
    }
252
}
253

254
gchar *
255 256
channel_get_color (Channel *channel)
{
257
  return (GIMP_CHANNEL (channel)->col); 
258 259 260 261 262 263 264 265
}
 
int
channel_get_opacity (Channel *channel)
{ 
  return channel->opacity;
}

266
void 
267 268
channel_set_opacity (Channel *channel,
		     gint     opacity)
269 270
{
  if (opacity >=0 && opacity <= 100)
271
    channel->opacity = (gint) (opacity * 255) / 100;
272 273
}

Elliot Lee's avatar
Elliot Lee committed
274
Channel *
275
channel_get_ID (gint ID)
Elliot Lee's avatar
Elliot Lee committed
276
{
277
  GimpDrawable *drawable;
278

279 280 281 282 283
  drawable = drawable_get_ID (ID);
  if (drawable && GIMP_IS_CHANNEL (drawable)) 
    return GIMP_CHANNEL (drawable);
  else
    return NULL;
Elliot Lee's avatar
Elliot Lee committed
284 285 286 287 288
}

void
channel_delete (Channel *channel)
{
scott's avatar
scott committed
289
  gtk_object_unref (GTK_OBJECT (channel));
290 291 292 293 294 295 296 297 298 299 300 301
}

static void
gimp_channel_destroy (GtkObject *object)
{
  GimpChannel *channel;

  g_return_if_fail (object != NULL);
  g_return_if_fail (GIMP_IS_CHANNEL (object));

  channel = GIMP_CHANNEL (object);

Elliot Lee's avatar
Elliot Lee committed
302 303 304 305 306 307
  /* free the segments?  */
  if (channel->segs_in)
    g_free (channel->segs_in);
  if (channel->segs_out)
    g_free (channel->segs_out);

308
  if (GTK_OBJECT_CLASS (parent_class)->destroy)
309
    (* GTK_OBJECT_CLASS (parent_class)->destroy) (object);
310
  
Elliot Lee's avatar
Elliot Lee committed
311 312 313
}

void
314 315 316
channel_scale (Channel *channel,
	       gint     new_width,
	       gint     new_height)
Elliot Lee's avatar
Elliot Lee committed
317 318 319 320 321 322 323 324
{
  PixelRegion srcPR, destPR;
  TileManager *new_tiles;

  if (new_width == 0 || new_height == 0)
    return;

  /*  Update the old channel position  */
325 326 327 328
  drawable_update (GIMP_DRAWABLE (channel),
		   0, 0,
		   GIMP_DRAWABLE (channel)->width,
		   GIMP_DRAWABLE (channel)->height);
Elliot Lee's avatar
Elliot Lee committed
329 330

  /*  Configure the pixel regions  */
331 332 333 334
  pixel_region_init (&srcPR, GIMP_DRAWABLE (channel)->tiles,
		     0, 0,
		     GIMP_DRAWABLE (channel)->width,
		     GIMP_DRAWABLE (channel)->height, FALSE);
Elliot Lee's avatar
Elliot Lee committed
335 336 337 338 339 340 341 342 343

  /*  Allocate the new channel, configure dest region  */
  new_tiles = tile_manager_new (new_width, new_height, 1);
  pixel_region_init (&destPR, new_tiles, 0, 0, new_width, new_height, TRUE);

  /*  Add an alpha channel  */
  scale_region (&srcPR, &destPR);

  /*  Push the channel on the undo stack  */
344
  undo_push_channel_mod (GIMP_DRAWABLE (channel)->gimage, channel);
Elliot Lee's avatar
Elliot Lee committed
345 346

  /*  Configure the new channel  */
347 348 349
  GIMP_DRAWABLE (channel)->tiles = new_tiles;
  GIMP_DRAWABLE (channel)->width = new_width;
  GIMP_DRAWABLE (channel)->height = new_height;
Elliot Lee's avatar
Elliot Lee committed
350 351 352 353 354

  /*  bounds are now unknown  */
  channel->bounds_known = FALSE;

  /*  Update the new channel position  */
355 356 357 358
  drawable_update (GIMP_DRAWABLE (channel),
		   0, 0,
		   GIMP_DRAWABLE (channel)->width,
		   GIMP_DRAWABLE (channel)->height);
Elliot Lee's avatar
Elliot Lee committed
359 360 361
}

void
362 363 364 365 366
channel_resize (Channel *channel,
		gint     new_width,
		gint     new_height,
		gint     offx,
		gint     offy)
Elliot Lee's avatar
Elliot Lee committed
367 368 369
{
  PixelRegion srcPR, destPR;
  TileManager *new_tiles;
370 371 372 373
  guchar bg = 0;
  gint clear;
  gint w, h;
  gint x1, y1, x2, y2;
Elliot Lee's avatar
Elliot Lee committed
374 375 376 377 378 379

  if (!new_width || !new_height)
    return;

  x1 = BOUNDS (offx, 0, new_width);
  y1 = BOUNDS (offy, 0, new_height);
380 381
  x2 = BOUNDS ((offx + GIMP_DRAWABLE (channel)->width), 0, new_width);
  y2 = BOUNDS ((offy + GIMP_DRAWABLE (channel)->height), 0, new_height);
Elliot Lee's avatar
Elliot Lee committed
382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407
  w = x2 - x1;
  h = y2 - y1;

  if (offx > 0)
    {
      x1 = 0;
      x2 = offx;
    }
  else
    {
      x1 = -offx;
      x2 = 0;
    }

  if (offy > 0)
    {
      y1 = 0;
      y2 = offy;
    }
  else
    {
      y1 = -offy;
      y2 = 0;
    }

  /*  Update the old channel position  */
408 409 410 411
  drawable_update (GIMP_DRAWABLE (channel),
		   0, 0,
		   GIMP_DRAWABLE (channel)->width,
		   GIMP_DRAWABLE (channel)->height);
Elliot Lee's avatar
Elliot Lee committed
412 413

  /*  Configure the pixel regions  */
414 415
  pixel_region_init (&srcPR, GIMP_DRAWABLE (channel)->tiles,
		     x1, y1, w, h, FALSE);
Elliot Lee's avatar
Elliot Lee committed
416 417

  /*  Determine whether the new channel needs to be initially cleared  */
418 419
  if ((new_width  > GIMP_DRAWABLE (channel)->width) ||
      (new_height > GIMP_DRAWABLE (channel)->height) ||
Elliot Lee's avatar
Elliot Lee committed
420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440
      (x2 || y2))
    clear = TRUE;
  else
    clear = FALSE;

  /*  Allocate the new channel, configure dest region  */
  new_tiles = tile_manager_new (new_width, new_height, 1);

  /*  Set to black (empty--for selections)  */
  if (clear)
    {
      pixel_region_init (&destPR, new_tiles, 0, 0, new_width, new_height, TRUE);
      color_region (&destPR, &bg);
    }

  /*  copy from the old to the new  */
  pixel_region_init (&destPR, new_tiles, x2, y2, w, h, TRUE);
  if (w && h)
    copy_region (&srcPR, &destPR);

  /*  Push the channel on the undo stack  */
441
  undo_push_channel_mod (GIMP_DRAWABLE (channel)->gimage, channel);
Elliot Lee's avatar
Elliot Lee committed
442 443

  /*  Configure the new channel  */
444 445 446
  GIMP_DRAWABLE (channel)->tiles  = new_tiles;
  GIMP_DRAWABLE (channel)->width  = new_width;
  GIMP_DRAWABLE (channel)->height = new_height;
Elliot Lee's avatar
Elliot Lee committed
447 448 449 450 451

  /*  bounds are now unknown  */
  channel->bounds_known = FALSE;

  /*  update the new channel area  */
452 453 454 455
  drawable_update (GIMP_DRAWABLE (channel),
		   0, 0,
		   GIMP_DRAWABLE (channel)->width,
		   GIMP_DRAWABLE (channel)->height);
Elliot Lee's avatar
Elliot Lee committed
456 457
}

458 459 460
void            
channel_update (Channel *channel)
{
461 462 463 464 465
  drawable_update (GIMP_DRAWABLE (channel),
		   0, 0,
		   GIMP_DRAWABLE (channel)->width,
		   GIMP_DRAWABLE (channel)->height);
  gdisplays_flush ();
466 467
}

468 469 470
/**********************/
/*  access functions  */
/**********************/
Elliot Lee's avatar
Elliot Lee committed
471

472
gboolean
Elliot Lee's avatar
Elliot Lee committed
473 474
channel_toggle_visibility (Channel *channel)
{
475
  GIMP_DRAWABLE (channel)->visible = !GIMP_DRAWABLE (channel)->visible;
Elliot Lee's avatar
Elliot Lee committed
476

477
  return GIMP_DRAWABLE (channel)->visible;
Elliot Lee's avatar
Elliot Lee committed
478 479
}

480 481 482 483
static TempBuf *
channel_preview_private (Channel *channel,
			 gint     width,
			 gint     height)
Elliot Lee's avatar
Elliot Lee committed
484 485 486
{
  MaskBuf * preview_buf;
  PixelRegion srcPR, destPR;
487
  gint subsample;
488
  TempBuf *ret_buf;
Elliot Lee's avatar
Elliot Lee committed
489 490

  /*  The easy way  */
491 492 493 494
  if (GIMP_DRAWABLE (channel)->preview_valid &&
      (ret_buf =
       gimp_preview_cache_get (& (GIMP_DRAWABLE (channel)->preview_cache),
			       width, height)))
495
    return ret_buf;
Elliot Lee's avatar
Elliot Lee committed
496 497 498 499 500
  /*  The hard way  */
  else
    {
      /*  calculate 'acceptable' subsample  */
      subsample = 1;
501 502
      if (width < 1) width = 1;
      if (height < 1) height = 1;
503 504
      while ((width  * (subsample + 1) * 2 < GIMP_DRAWABLE (channel)->width) &&
	     (height * (subsample + 1) * 2 < GIMP_DRAWABLE (channel)->height))
Elliot Lee's avatar
Elliot Lee committed
505 506
	subsample = subsample + 1;

507 508 509 510
      pixel_region_init (&srcPR, GIMP_DRAWABLE (channel)->tiles,
			 0, 0,
			 GIMP_DRAWABLE (channel)->width,
			 GIMP_DRAWABLE (channel)->height, FALSE);
Elliot Lee's avatar
Elliot Lee committed
511 512

      preview_buf = mask_buf_new (width, height);
513 514 515 516 517
      destPR.bytes     = 1;
      destPR.x         = 0;
      destPR.y         = 0;
      destPR.w         = width;
      destPR.h         = height;
Elliot Lee's avatar
Elliot Lee committed
518
      destPR.rowstride = width;
519
      destPR.data      = mask_buf_data (preview_buf);
Elliot Lee's avatar
Elliot Lee committed
520 521 522

      subsample_region (&srcPR, &destPR, subsample);

523 524 525 526 527 528
      if (!GIMP_DRAWABLE (channel)->preview_valid)
	gimp_preview_cache_invalidate (&(GIMP_DRAWABLE(channel)->preview_cache));

      GIMP_DRAWABLE (channel)->preview_valid = TRUE;
      gimp_preview_cache_add (&(GIMP_DRAWABLE (channel)->preview_cache),
			      preview_buf);
529
      return preview_buf;
Elliot Lee's avatar
Elliot Lee committed
530 531 532
    }
}

533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557
TempBuf *
channel_preview (Channel *channel,
		 gint     width,
		 gint     height)
{
  /* Ok prime the cache with a large preview if the cache is invalid */
  if(!GIMP_DRAWABLE(channel)->preview_valid && 
     width <= PREVIEW_CACHE_PRIME_WIDTH &&
     height <= PREVIEW_CACHE_PRIME_HEIGHT)
    {
      TempBuf * tb = channel_preview_private(channel,
					     PREVIEW_CACHE_PRIME_WIDTH,
					     PREVIEW_CACHE_PRIME_HEIGHT);
      
      /* Save the 2nd call */
      if(width == PREVIEW_CACHE_PRIME_WIDTH &&
	 height == PREVIEW_CACHE_PRIME_HEIGHT)
	return tb;
    }

  /* Second call - should NOT visit the tile cache...*/
  return channel_preview_private(channel,width,height);
}


Elliot Lee's avatar
Elliot Lee committed
558
void
559
channel_invalidate_previews (GimpImage* gimage)
Elliot Lee's avatar
Elliot Lee committed
560
{
561
  GSList * tmp;
Elliot Lee's avatar
Elliot Lee committed
562
  Channel * channel;
563 564

  tmp = gimage->channels;
Elliot Lee's avatar
Elliot Lee committed
565 566 567 568

  while (tmp)
    {
      channel = (Channel *) tmp->data;
569
      drawable_invalidate_preview (GIMP_DRAWABLE (channel));
570
      tmp = g_slist_next (tmp);
Elliot Lee's avatar
Elliot Lee committed
571 572 573
    }
}

574
Tattoo
575
channel_get_tattoo (const Channel *channel)
576
{
577
  return (gimp_drawable_get_tattoo (GIMP_DRAWABLE (channel)));
578 579
}

580 581 582
/******************************/
/*  selection mask functions  */
/******************************/
Elliot Lee's avatar
Elliot Lee committed
583 584

Channel *
585 586 587
channel_new_mask (GimpImage* gimage,
		  gint       width,
		  gint       height)
Elliot Lee's avatar
Elliot Lee committed
588
{
589
  guchar black[3] = {0, 0, 0};
Elliot Lee's avatar
Elliot Lee committed
590 591 592
  Channel *new_channel;

  /*  Create the new channel  */
593 594
  new_channel = channel_new (gimage, width, height,
			     _("Selection Mask"), 127, black);
Elliot Lee's avatar
Elliot Lee committed
595 596

  /*  Set the validate procedure  */
597 598
  tile_manager_set_validate_proc (GIMP_DRAWABLE (new_channel)->tiles,
				  channel_validate);
Elliot Lee's avatar
Elliot Lee committed
599 600 601 602

  return new_channel;
}

603 604 605 606 607 608 609 610 611 612
gboolean
channel_boundary (Channel   *mask,
		  BoundSeg **segs_in,
		  BoundSeg **segs_out,
		  gint      *num_segs_in,
		  gint      *num_segs_out,
		  gint       x1,
		  gint       y1,
		  gint       x2,
		  gint       y2)
Elliot Lee's avatar
Elliot Lee committed
613
{
614
  gint x3, y3, x4, y4;
Elliot Lee's avatar
Elliot Lee committed
615 616 617 618 619 620 621 622 623 624 625 626
  PixelRegion bPR;

  if (! mask->boundary_known)
    {
      /* free the out of date boundary segments */
      if (mask->segs_in)
	g_free (mask->segs_in);
      if (mask->segs_out)
	g_free (mask->segs_out);

      if (channel_bounds (mask, &x3, &y3, &x4, &y4))
	{
627 628
	  pixel_region_init (&bPR, GIMP_DRAWABLE (mask)->tiles,
			     x3, y3, (x4 - x3), (y4 - y3), FALSE);
Elliot Lee's avatar
Elliot Lee committed
629 630 631 632 633 634 635 636 637 638 639
	  mask->segs_out = find_mask_boundary (&bPR, &mask->num_segs_out,
					       IgnoreBounds,
					       x1, y1,
					       x2, y2);
	  x1 = MAXIMUM (x1, x3);
	  y1 = MAXIMUM (y1, y3);
	  x2 = MINIMUM (x2, x4);
	  y2 = MINIMUM (y2, y4);

	  if (x2 > x1 && y2 > y1)
	    {
640 641 642 643
	      pixel_region_init (&bPR, GIMP_DRAWABLE (mask)->tiles,
				 0, 0,
				 GIMP_DRAWABLE (mask)->width,
				 GIMP_DRAWABLE (mask)->height, FALSE);
Elliot Lee's avatar
Elliot Lee committed
644 645 646 647 648 649 650
	      mask->segs_in =  find_mask_boundary (&bPR, &mask->num_segs_in,
						   WithinBounds,
						   x1, y1,
						   x2, y2);
	    }
	  else
	    {
651
	      mask->segs_in     = NULL;
Elliot Lee's avatar
Elliot Lee committed
652 653 654 655 656
	      mask->num_segs_in = 0;
	    }
	}
      else
	{
657 658 659
	  mask->segs_in      = NULL;
	  mask->segs_out     = NULL;
	  mask->num_segs_in  = 0;
Elliot Lee's avatar
Elliot Lee committed
660 661 662 663 664
	  mask->num_segs_out = 0;
	}
      mask->boundary_known = TRUE;
    }

665 666 667
  *segs_in      = mask->segs_in;
  *segs_out     = mask->segs_out;
  *num_segs_in  = mask->num_segs_in;
Elliot Lee's avatar
Elliot Lee committed
668 669 670 671 672
  *num_segs_out = mask->num_segs_out;

  return TRUE;
}

673 674 675 676
gint
channel_value (Channel *mask,
	       gint     x,
	       gint     y)
Elliot Lee's avatar
Elliot Lee committed
677 678
{
  Tile *tile;
679
  gint val;
Elliot Lee's avatar
Elliot Lee committed
680 681 682 683 684 685 686 687 688 689 690

  /*  Some checks to cut back on unnecessary work  */
  if (mask->bounds_known)
    {
      if (mask->empty)
	return 0;
      else if (x < mask->x1 || x >= mask->x2 || y < mask->y1 || y >= mask->y2)
	return 0;
    }
  else
    {
691 692
      if (x < 0 || x >= GIMP_DRAWABLE (mask)->width ||
	  y < 0 || y >= GIMP_DRAWABLE (mask)->height)
Elliot Lee's avatar
Elliot Lee committed
693 694 695
	return 0;
    }

696 697
  tile = tile_manager_get_tile (GIMP_DRAWABLE (mask)->tiles, x, y, TRUE, FALSE);
  val = *(guchar *) (tile_data_pointer (tile, x % TILE_WIDTH, y % TILE_HEIGHT));
698
  tile_release (tile, FALSE);
Elliot Lee's avatar
Elliot Lee committed
699 700 701 702

  return val;
}

703 704 705 706 707 708
gboolean
channel_bounds (Channel *mask,
		gint    *x1,
		gint    *y1,
		gint    *x2,
		gint    *y2)
Elliot Lee's avatar
Elliot Lee committed
709 710
{
  PixelRegion maskPR;
711 712 713
  guchar *data, *data1;
  gint x, y;
  gint ex, ey;
Elliot Lee's avatar
Elliot Lee committed
714
  void *pr;
715 716 717
  gint tx1, tx2, ty1, ty2;
  gint minx, maxx;

Elliot Lee's avatar
Elliot Lee committed
718 719 720 721 722 723 724 725
  /*  if the mask's bounds have already been reliably calculated...  */
  if (mask->bounds_known)
    {
      *x1 = mask->x1;
      *y1 = mask->y1;
      *x2 = mask->x2;
      *y2 = mask->y2;

726
      return !mask->empty;
Elliot Lee's avatar
Elliot Lee committed
727 728 729
    }

  /*  go through and calculate the bounds  */
730 731
  tx1 = GIMP_DRAWABLE (mask)->width;
  ty1 = GIMP_DRAWABLE (mask)->height;
732 733
  tx2 = 0;
  ty2 = 0;
Elliot Lee's avatar
Elliot Lee committed
734

735 736 737 738 739 740 741
  pixel_region_init (&maskPR, GIMP_DRAWABLE (mask)->tiles,
		     0, 0,
		     GIMP_DRAWABLE (mask)->width,
		     GIMP_DRAWABLE (mask)->height, FALSE);
  for (pr = pixel_regions_register (1, &maskPR);
       pr != NULL;
       pr = pixel_regions_process (pr))
Elliot Lee's avatar
Elliot Lee committed
742
    {
743
      data1 = data = maskPR.data;
Elliot Lee's avatar
Elliot Lee committed
744 745
      ex = maskPR.x + maskPR.w;
      ey = maskPR.y + maskPR.h;
746 747
      /* only check the pixels if this tile is not fully within the currently
	 computed bounds */
748 749
      if (maskPR.x < tx1 || ex > tx2 ||
	  maskPR.y < ty1 || ey > ty2)
750
        {
751 752 753 754 755 756 757 758 759 760 761 762 763 764
	  /* 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])
	  {
	    if (maskPR.x < tx1)
	      tx1 = maskPR.x;
	    if (ex > tx2)
	      tx2 = ex;
	    if (maskPR.y < ty1)
	      ty1 = maskPR.y;
	    if (ey > ty2)
	      ty2 = ey;
	  }
	  else
765
	    for (y = maskPR.y; y < ey; y++, data1 += maskPR.rowstride)
Elliot Lee's avatar
Elliot Lee committed
766
	    {
767
	      for (x = maskPR.x, data = data1; x < ex; x++, data++)
768
		if (*data)
769 770 771 772 773 774 775 776 777 778 779 780 781 782 783
		{
		  minx = x;
		  maxx = x;
		  for (; x < ex; x++, data++)
		    if (*data)
		      maxx = x;
		  if (minx < tx1)
		    tx1 = minx;
		  if (maxx > tx2)
		    tx2 = maxx;
		  if (y < ty1)
		    ty1 = y;
		  if (y > ty2)
		    ty2 = y;
	      }
Elliot Lee's avatar
Elliot Lee committed
784 785 786 787
	    }
	}
    }

788 789
  tx2 = BOUNDS (tx2 + 1, 0, GIMP_DRAWABLE (mask)->width);
  ty2 = BOUNDS (ty2 + 1, 0, GIMP_DRAWABLE (mask)->height);
Elliot Lee's avatar
Elliot Lee committed
790

791
  if (tx1 == GIMP_DRAWABLE (mask)->width && ty1 == GIMP_DRAWABLE (mask)->height)
Elliot Lee's avatar
Elliot Lee committed
792 793
    {
      mask->empty = TRUE;
794 795 796 797
      mask->x1    = 0;
      mask->y1    = 0;
      mask->x2    = GIMP_DRAWABLE (mask)->width;
      mask->y2    = GIMP_DRAWABLE (mask)->height;
Elliot Lee's avatar
Elliot Lee committed
798 799 800 801
    }
  else
    {
      mask->empty = FALSE;
802 803 804 805
      mask->x1    = tx1;
      mask->y1    = ty1;
      mask->x2    = tx2;
      mask->y2    = ty2;
Elliot Lee's avatar
Elliot Lee committed
806 807 808
    }
  mask->bounds_known = TRUE;

809 810 811 812 813
  *x1 = tx1;
  *x2 = tx2;
  *y1 = ty1;
  *y2 = ty2;

814
  return !mask->empty;
Elliot Lee's avatar
Elliot Lee committed
815 816
}

817
gboolean
Elliot Lee's avatar
Elliot Lee committed
818 819 820
channel_is_empty (Channel *mask)
{
  PixelRegion maskPR;
821 822
  guchar * data;
  gint x, y;
Elliot Lee's avatar
Elliot Lee committed
823 824 825 826 827
  void * pr;

  if (mask->bounds_known)
    return mask->empty;

828 829 830 831 832 833 834
  pixel_region_init (&maskPR, GIMP_DRAWABLE (mask)->tiles,
		     0, 0,
		     GIMP_DRAWABLE (mask)->width,
		     GIMP_DRAWABLE (mask)->height, FALSE);
  for (pr = pixel_regions_register (1, &maskPR);
       pr != NULL;
       pr = pixel_regions_process (pr))
Elliot Lee's avatar
Elliot Lee committed
835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852
    {
      /*  check if any pixel in the mask is non-zero  */
      data = maskPR.data;
      for (y = 0; y < maskPR.h; y++)
	for (x = 0; x < maskPR.w; x++)
	  if (*data++)
	    {
	      pixel_regions_process_stop (pr);
	      return FALSE;
	    }
    }

  /*  The mask is empty, meaning we can set the bounds as known  */
  if (mask->segs_in)
    g_free (mask->segs_in);
  if (mask->segs_out)
    g_free (mask->segs_out);

853 854 855 856 857 858
  mask->empty          = TRUE;
  mask->segs_in        = NULL;
  mask->segs_out       = NULL;
  mask->num_segs_in    = 0;
  mask->num_segs_out   = 0;
  mask->bounds_known   = TRUE;
Elliot Lee's avatar
Elliot Lee committed
859
  mask->boundary_known = TRUE;
860 861 862 863
  mask->x1             = 0;
  mask->y1             = 0;
  mask->x2             = GIMP_DRAWABLE (mask)->width;
  mask->y2             = GIMP_DRAWABLE (mask)->height;
Elliot Lee's avatar
Elliot Lee committed
864 865 866 867 868

  return TRUE;
}

void
869 870 871 872 873
channel_add_segment (Channel *mask,
		     gint     x,
		     gint     y,
		     gint     width,
		     gint     value)
Elliot Lee's avatar
Elliot Lee committed
874 875
{
  PixelRegion maskPR;
876 877 878
  guchar *data;
  gint val;
  gint x2;
Elliot Lee's avatar
Elliot Lee committed
879 880 881 882 883
  void * pr;

  /*  check horizontal extents...  */
  x2 = x + width;
  if (x2 < 0) x2 = 0;
884
  if (x2 > GIMP_DRAWABLE (mask)->width) x2 = GIMP_DRAWABLE (mask)->width;
Elliot Lee's avatar
Elliot Lee committed
885
  if (x < 0) x = 0;
886
  if (x > GIMP_DRAWABLE (mask)->width) x = GIMP_DRAWABLE (mask)->width;
Elliot Lee's avatar
Elliot Lee committed
887 888 889
  width = x2 - x;
  if (!width) return;

890
  if (y < 0 || y > GIMP_DRAWABLE (mask)->height)
Elliot Lee's avatar
Elliot Lee committed
891 892
    return;

893 894 895 896 897
  pixel_region_init (&maskPR, GIMP_DRAWABLE (mask)->tiles,
		     x, y, width, 1, TRUE);
  for (pr = pixel_regions_register (1, &maskPR);
       pr != NULL;
       pr = pixel_regions_process (pr))
Elliot Lee's avatar
Elliot Lee committed
898 899 900 901 902 903 904 905 906 907 908 909 910 911
    {
      data = maskPR.data;
      width = maskPR.w;
      while (width--)
	{
	  val = *data + value;
	  if (val > 255)
	    val = 255;
	  *data++ = val;
	}
    }
}

void
912 913 914 915 916
channel_sub_segment (Channel *mask,
		     gint     x,
		     gint     y,
		     gint     width,
		     gint     value)
Elliot Lee's avatar
Elliot Lee committed
917 918
{
  PixelRegion maskPR;
919 920 921
  guchar *data;
  gint val;
  gint x2;
Elliot Lee's avatar
Elliot Lee committed
922 923 924 925 926
  void * pr;

  /*  check horizontal extents...  */
  x2 = x + width;
  if (x2 < 0) x2 = 0;
927
  if (x2 > GIMP_DRAWABLE (mask)->width) x2 = GIMP_DRAWABLE (mask)->width;
Elliot Lee's avatar
Elliot Lee committed
928
  if (x < 0) x = 0;
929
  if (x > GIMP_DRAWABLE (mask)->width) x = GIMP_DRAWABLE (mask)->width;
Elliot Lee's avatar
Elliot Lee committed
930 931 932
  width = x2 - x;
  if (!width) return;

933
  if (y < 0 || y > GIMP_DRAWABLE (mask)->height)
Elliot Lee's avatar
Elliot Lee committed
934 935
    return;

936 937 938 939
  pixel_region_init (&maskPR, GIMP_DRAWABLE (mask)->tiles, x, y, width, 1, TRUE);
  for (pr = pixel_regions_register (1, &maskPR);
       pr != NULL;
       pr = pixel_regions_process (pr))
Elliot Lee's avatar
Elliot Lee committed
940 941 942 943 944 945 946 947 948 949 950 951 952 953
    {
      data = maskPR.data;
      width = maskPR.w;
      while (width--)
	{
	  val = *data - value;
	  if (val < 0)
	    val = 0;
	  *data++ = val;
	}
    }
}

void
954 955 956 957 958 959
channel_combine_rect (Channel    *mask,
		      ChannelOps  op,
		      gint        x,
		      gint        y,
		      gint        w,
		      gint        h)
Elliot Lee's avatar
Elliot Lee committed
960
{
961
  gint x2, y2;
Elliot Lee's avatar
Elliot Lee committed
962
  PixelRegion maskPR;
963 964
  guchar color;

965 966
  y2 = y + h;
  x2 = x + w;
Elliot Lee's avatar
Elliot Lee committed
967

968 969 970 971
  x  = BOUNDS (x,  0, GIMP_DRAWABLE (mask)->width);
  y  = BOUNDS (y,  0, GIMP_DRAWABLE (mask)->height);
  x2 = BOUNDS (x2, 0, GIMP_DRAWABLE (mask)->width);
  y2 = BOUNDS (y2, 0, GIMP_DRAWABLE (mask)->height);
Elliot Lee's avatar
Elliot Lee committed
972

973
  if (x2 - x <= 0 || y2 - y <= 0)
Elliot Lee's avatar
Elliot Lee committed
974 975
    return;

976 977
  pixel_region_init (&maskPR, GIMP_DRAWABLE (mask)->tiles,
		     x, y, x2 - x, y2 - y, TRUE);
978 979 980 981
  if (op == ADD  || op == REPLACE)
    color = 255;
  else
    color = 0;
982
  color_region (&maskPR, &color);
Elliot Lee's avatar
Elliot Lee committed
983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006

  /*  Determine new boundary  */
  if (mask->bounds_known && (op == ADD) && !mask->empty)
    {
      if (x < mask->x1)
	mask->x1 = x;
      if (y < mask->y1)
	mask->y1 = y;
      if ((x + w) > mask->x2)
	mask->x2 = (x + w);
      if ((y + h) > mask->y2)
	mask->y2 = (y + h);
    }
  else if (op == REPLACE || mask->empty)
    {
      mask->empty = FALSE;
      mask->x1 = x;
      mask->y1 = y;
      mask->x2 = x + w;
      mask->y2 = y + h;
    }
  else
    mask->bounds_known = FALSE;

1007 1008 1009 1010
  mask->x1 = BOUNDS (mask->x1, 0, GIMP_DRAWABLE (mask)->width);
  mask->y1 = BOUNDS (mask->y1, 0, GIMP_DRAWABLE (mask)->height);
  mask->x2 = BOUNDS (mask->x2, 0, GIMP_DRAWABLE (mask)->width);
  mask->y2 = BOUNDS (mask->y2, 0, GIMP_DRAWABLE (mask)->height);
Elliot Lee's avatar
Elliot Lee committed
1011 1012 1013
}

void
1014 1015 1016 1017 1018 1019 1020
channel_combine_ellipse (Channel    *mask,
			 ChannelOps  op,
			 gint        x,
			 gint        y,
			 gint        w,
			 gint        h,
			 gboolean    aa /*  antialias selection?  */)
Elliot Lee's avatar
Elliot Lee committed
1021
{
1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032
  gint i, j;
  gint x0, x1, x2;
  gint val, last;
  gfloat a_sqr, b_sqr, aob_sqr;
  gfloat w_sqr, h_sqr;
  gfloat y_sqr;
  gfloat t0, t1;
  gfloat r;
  gfloat cx, cy;
  gfloat rad;
  gfloat dist;
Elliot Lee's avatar
Elliot Lee committed
1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045

  if (!w || !h)
    return;

  a_sqr = (w * w / 4.0);
  b_sqr = (h * h / 4.0);
  aob_sqr = a_sqr / b_sqr;

  cx = x + w / 2.0;
  cy = y + h / 2.0;

  for (i = y; i < (y + h); i++)
    {
1046
      if (i >= 0 && i < GIMP_DRAWABLE (mask)->height)
Elliot Lee's avatar
Elliot Lee committed
1047 1048 1049 1050 1051 1052 1053 1054