1997-11-25 06:05:25 +08:00
|
|
|
/* 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
|
1998-04-13 13:44:11 +08:00
|
|
|
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
|
1997-11-25 06:05:25 +08:00
|
|
|
*/
|
2001-03-10 01:39:18 +08:00
|
|
|
|
1999-07-27 08:14:14 +08:00
|
|
|
#include "config.h"
|
2000-03-26 02:17:01 +08:00
|
|
|
|
1997-11-25 06:05:25 +08:00
|
|
|
#include <stdlib.h>
|
2000-03-26 02:17:01 +08:00
|
|
|
|
2000-12-17 05:37:03 +08:00
|
|
|
#include <gtk/gtk.h>
|
|
|
|
|
2001-01-24 07:56:18 +08:00
|
|
|
#include "libgimpmath/gimpmath.h"
|
|
|
|
|
2001-05-10 06:34:59 +08:00
|
|
|
#include "tools-types.h"
|
2000-12-17 05:37:03 +08:00
|
|
|
|
2001-05-09 10:32:03 +08:00
|
|
|
#include "core/gimpchannel.h"
|
|
|
|
#include "core/gimpdrawable.h"
|
|
|
|
#include "core/gimpimage.h"
|
|
|
|
#include "core/gimpimage-mask.h"
|
|
|
|
|
2001-05-14 05:51:20 +08:00
|
|
|
#include "app_procs.h"
|
1997-11-25 06:05:25 +08:00
|
|
|
#include "boundary.h"
|
2001-02-02 02:44:22 +08:00
|
|
|
#include "gdisplay.h"
|
1997-11-25 06:05:25 +08:00
|
|
|
#include "gimprc.h"
|
2000-12-29 23:22:01 +08:00
|
|
|
#include "pixel_region.h"
|
|
|
|
#include "tile_manager.h"
|
|
|
|
#include "tile.h"
|
2001-01-25 06:36:18 +08:00
|
|
|
|
2001-03-08 09:07:03 +08:00
|
|
|
#include "gimpeditselectiontool.h"
|
|
|
|
#include "gimpfuzzyselecttool.h"
|
|
|
|
#include "gimptool.h"
|
2001-01-25 06:36:18 +08:00
|
|
|
#include "selection_options.h"
|
2000-12-29 23:22:01 +08:00
|
|
|
#include "tool_options.h"
|
2001-02-28 09:05:22 +08:00
|
|
|
#include "tool_manager.h"
|
1998-07-08 14:41:58 +08:00
|
|
|
|
1999-07-07 02:13:59 +08:00
|
|
|
#include "libgimp/gimpintl.h"
|
|
|
|
|
2001-05-14 08:04:29 +08:00
|
|
|
#define WANT_FUZZY_SELECT_BITS
|
|
|
|
#include "icons.h"
|
1997-11-25 06:05:25 +08:00
|
|
|
|
2000-12-29 23:22:01 +08:00
|
|
|
|
2001-03-10 01:39:18 +08:00
|
|
|
static void gimp_fuzzy_select_tool_class_init (GimpFuzzySelectToolClass *klass);
|
|
|
|
static void gimp_fuzzy_select_tool_init (GimpFuzzySelectTool *fuzzy_select);
|
|
|
|
static void gimp_fuzzy_select_tool_destroy (GtkObject *object);
|
1997-11-25 06:05:25 +08:00
|
|
|
|
2001-03-10 01:39:18 +08:00
|
|
|
static void gimp_fuzzy_select_tool_button_press (GimpTool *tool,
|
|
|
|
GdkEventButton *bevent,
|
|
|
|
GDisplay *gdisp);
|
|
|
|
static void gimp_fuzzy_select_tool_button_release (GimpTool *tool,
|
|
|
|
GdkEventButton *bevent,
|
|
|
|
GDisplay *gdisp);
|
|
|
|
static void gimp_fuzzy_select_tool_motion (GimpTool *tool,
|
|
|
|
GdkEventMotion *mevent,
|
|
|
|
GDisplay *gdisp);
|
1999-05-05 17:10:35 +08:00
|
|
|
|
2001-03-10 01:39:18 +08:00
|
|
|
static void gimp_fuzzy_select_tool_draw (GimpDrawTool *draw_tool);
|
2000-03-02 07:22:43 +08:00
|
|
|
|
2001-03-10 01:39:18 +08:00
|
|
|
static GdkSegment * fuzzy_select_calculate (GimpFuzzySelectTool *fuzzy_sel,
|
|
|
|
GDisplay *gdisp,
|
|
|
|
gint *nsegs);
|
1999-04-13 01:55:06 +08:00
|
|
|
|
2000-12-31 12:07:42 +08:00
|
|
|
|
2001-03-10 01:39:18 +08:00
|
|
|
static GimpSelectionToolClass *parent_class = NULL;
|
2000-12-31 12:07:42 +08:00
|
|
|
|
1999-04-09 06:25:54 +08:00
|
|
|
/* the fuzzy selection tool options */
|
2000-04-20 23:57:13 +08:00
|
|
|
static SelectionOptions *fuzzy_options = NULL;
|
1999-04-09 06:25:54 +08:00
|
|
|
|
|
|
|
/* XSegments which make up the fuzzy selection boundary */
|
2000-03-26 02:17:01 +08:00
|
|
|
static GdkSegment *segs = NULL;
|
|
|
|
static gint num_segs = 0;
|
1999-04-19 05:22:41 +08:00
|
|
|
|
2001-01-29 10:45:02 +08:00
|
|
|
GimpChannel * fuzzy_mask = NULL;
|
1997-11-25 06:05:25 +08:00
|
|
|
|
2001-03-10 01:39:18 +08:00
|
|
|
|
|
|
|
/* public functions */
|
|
|
|
|
|
|
|
void
|
|
|
|
gimp_fuzzy_select_tool_register (void)
|
|
|
|
{
|
|
|
|
tool_manager_register_tool (GIMP_TYPE_FUZZY_SELECT_TOOL, FALSE,
|
|
|
|
"gimp:fuzzy_select_tool",
|
|
|
|
_("Fuzzy Select"),
|
|
|
|
_("Select contiguous regions"),
|
|
|
|
_("/Tools/Selection Tools/Fuzzy Select"), "Z",
|
|
|
|
NULL, "tools/fuzzy_select.html",
|
2001-05-14 08:04:29 +08:00
|
|
|
(const gchar **) fuzzy_select_bits);
|
2001-03-10 01:39:18 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
GtkType
|
|
|
|
gimp_fuzzy_select_tool_get_type (void)
|
|
|
|
{
|
|
|
|
static GtkType fuzzy_select_type = 0;
|
|
|
|
|
|
|
|
if (! fuzzy_select_type)
|
|
|
|
{
|
|
|
|
GtkTypeInfo fuzzy_select_info =
|
|
|
|
{
|
|
|
|
"GimpFuzzySelectTool",
|
|
|
|
sizeof (GimpFuzzySelectTool),
|
|
|
|
sizeof (GimpFuzzySelectToolClass),
|
|
|
|
(GtkClassInitFunc) gimp_fuzzy_select_tool_class_init,
|
|
|
|
(GtkObjectInitFunc) gimp_fuzzy_select_tool_init,
|
|
|
|
/* reserved_1 */ NULL,
|
|
|
|
/* reserved_2 */ NULL
|
|
|
|
};
|
|
|
|
|
|
|
|
fuzzy_select_type = gtk_type_unique (GIMP_TYPE_SELECTION_TOOL,
|
|
|
|
&fuzzy_select_info);
|
|
|
|
}
|
|
|
|
|
|
|
|
return fuzzy_select_type;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*******************************/
|
1997-11-25 06:05:25 +08:00
|
|
|
/* Fuzzy selection apparatus */
|
|
|
|
|
2000-03-26 02:17:01 +08:00
|
|
|
static gint
|
|
|
|
is_pixel_sufficiently_different (guchar *col1,
|
|
|
|
guchar *col2,
|
|
|
|
gboolean antialias,
|
|
|
|
gint threshold,
|
|
|
|
gint bytes,
|
|
|
|
gboolean has_alpha)
|
1997-11-25 06:05:25 +08:00
|
|
|
{
|
2000-03-26 02:17:01 +08:00
|
|
|
gint diff;
|
|
|
|
gint max;
|
|
|
|
gint b;
|
|
|
|
gint alpha;
|
1997-11-25 06:05:25 +08:00
|
|
|
|
|
|
|
max = 0;
|
|
|
|
alpha = (has_alpha) ? bytes - 1 : bytes;
|
|
|
|
|
|
|
|
/* if there is an alpha channel, never select transparent regions */
|
|
|
|
if (has_alpha && col2[alpha] == 0)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
for (b = 0; b < bytes; b++)
|
|
|
|
{
|
|
|
|
diff = col1[b] - col2[b];
|
|
|
|
diff = abs (diff);
|
|
|
|
if (diff > max)
|
|
|
|
max = diff;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (antialias)
|
|
|
|
{
|
|
|
|
float aa;
|
|
|
|
|
|
|
|
aa = 1.5 - ((float) max / threshold);
|
|
|
|
if (aa <= 0)
|
|
|
|
return 0;
|
|
|
|
else if (aa < 0.5)
|
|
|
|
return (unsigned char) (aa * 512);
|
|
|
|
else
|
|
|
|
return 255;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (max > threshold)
|
|
|
|
return 0;
|
|
|
|
else
|
|
|
|
return 255;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2000-03-26 02:17:01 +08:00
|
|
|
ref_tiles (TileManager *src,
|
|
|
|
TileManager *mask,
|
|
|
|
Tile **s_tile,
|
|
|
|
Tile **m_tile,
|
|
|
|
gint x,
|
|
|
|
gint y,
|
|
|
|
guchar **s,
|
|
|
|
guchar **m)
|
1997-11-25 06:05:25 +08:00
|
|
|
{
|
|
|
|
if (*s_tile != NULL)
|
1998-07-10 10:43:12 +08:00
|
|
|
tile_release (*s_tile, FALSE);
|
1997-11-25 06:05:25 +08:00
|
|
|
if (*m_tile != NULL)
|
1998-07-10 10:43:12 +08:00
|
|
|
tile_release (*m_tile, TRUE);
|
1997-11-25 06:05:25 +08:00
|
|
|
|
1998-08-16 03:17:36 +08:00
|
|
|
*s_tile = tile_manager_get_tile (src, x, y, TRUE, FALSE);
|
|
|
|
*m_tile = tile_manager_get_tile (mask, x, y, TRUE, TRUE);
|
1997-11-25 06:05:25 +08:00
|
|
|
|
1998-08-12 01:35:34 +08:00
|
|
|
*s = tile_data_pointer (*s_tile, x % TILE_WIDTH, y % TILE_HEIGHT);
|
|
|
|
*m = tile_data_pointer (*m_tile, x % TILE_WIDTH, y % TILE_HEIGHT);
|
1997-11-25 06:05:25 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2000-03-26 02:17:01 +08:00
|
|
|
find_contiguous_segment (guchar *col,
|
|
|
|
PixelRegion *src,
|
|
|
|
PixelRegion *mask,
|
|
|
|
gint width,
|
|
|
|
gint bytes,
|
|
|
|
gboolean has_alpha,
|
|
|
|
gboolean antialias,
|
|
|
|
gint threshold,
|
|
|
|
gint initial,
|
|
|
|
gint *start,
|
|
|
|
gint *end)
|
1997-11-25 06:05:25 +08:00
|
|
|
{
|
2000-03-26 02:17:01 +08:00
|
|
|
guchar *s;
|
|
|
|
guchar *m;
|
|
|
|
guchar diff;
|
|
|
|
Tile *s_tile = NULL;
|
|
|
|
Tile *m_tile = NULL;
|
1997-11-25 06:05:25 +08:00
|
|
|
|
|
|
|
ref_tiles (src->tiles, mask->tiles, &s_tile, &m_tile, src->x, src->y, &s, &m);
|
|
|
|
|
|
|
|
/* check the starting pixel */
|
|
|
|
if (! (diff = is_pixel_sufficiently_different (col, s, antialias,
|
|
|
|
threshold, bytes, has_alpha)))
|
|
|
|
{
|
1998-07-10 10:43:12 +08:00
|
|
|
tile_release (s_tile, FALSE);
|
|
|
|
tile_release (m_tile, TRUE);
|
1997-11-25 06:05:25 +08:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
*m-- = diff;
|
|
|
|
s -= bytes;
|
|
|
|
*start = initial - 1;
|
|
|
|
|
|
|
|
while (*start >= 0 && diff)
|
|
|
|
{
|
|
|
|
if (! ((*start + 1) % TILE_WIDTH))
|
|
|
|
ref_tiles (src->tiles, mask->tiles, &s_tile, &m_tile, *start, src->y, &s, &m);
|
|
|
|
|
|
|
|
diff = is_pixel_sufficiently_different (col, s, antialias,
|
|
|
|
threshold, bytes, has_alpha);
|
|
|
|
if ((*m-- = diff))
|
|
|
|
{
|
|
|
|
s -= bytes;
|
|
|
|
(*start)--;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
diff = 1;
|
|
|
|
*end = initial + 1;
|
|
|
|
if (*end % TILE_WIDTH && *end < width)
|
|
|
|
ref_tiles (src->tiles, mask->tiles, &s_tile, &m_tile, *end, src->y, &s, &m);
|
|
|
|
|
|
|
|
while (*end < width && diff)
|
|
|
|
{
|
|
|
|
if (! (*end % TILE_WIDTH))
|
|
|
|
ref_tiles (src->tiles, mask->tiles, &s_tile, &m_tile, *end, src->y, &s, &m);
|
|
|
|
|
|
|
|
diff = is_pixel_sufficiently_different (col, s, antialias,
|
|
|
|
threshold, bytes, has_alpha);
|
|
|
|
if ((*m++ = diff))
|
|
|
|
{
|
|
|
|
s += bytes;
|
|
|
|
(*end)++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1998-07-10 10:43:12 +08:00
|
|
|
tile_release (s_tile, FALSE);
|
|
|
|
tile_release (m_tile, TRUE);
|
1997-11-25 06:05:25 +08:00
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2000-03-26 02:17:01 +08:00
|
|
|
find_contiguous_region_helper (PixelRegion *mask,
|
|
|
|
PixelRegion *src,
|
|
|
|
gboolean has_alpha,
|
|
|
|
gboolean antialias,
|
|
|
|
gint threshold,
|
|
|
|
gboolean indexed,
|
|
|
|
gint x,
|
|
|
|
gint y,
|
|
|
|
guchar *col)
|
1997-11-25 06:05:25 +08:00
|
|
|
{
|
2000-03-26 02:17:01 +08:00
|
|
|
gint start, end, i;
|
|
|
|
gint val;
|
|
|
|
gint bytes;
|
1998-05-02 15:44:58 +08:00
|
|
|
|
1997-11-25 06:05:25 +08:00
|
|
|
Tile *tile;
|
|
|
|
|
|
|
|
if (threshold == 0) threshold = 1;
|
|
|
|
if (x < 0 || x >= src->w) return;
|
|
|
|
if (y < 0 || y >= src->h) return;
|
|
|
|
|
1998-08-16 03:17:36 +08:00
|
|
|
tile = tile_manager_get_tile (mask->tiles, x, y, TRUE, FALSE);
|
2000-03-26 02:17:01 +08:00
|
|
|
val = *(guchar *)(tile_data_pointer (tile,
|
|
|
|
x % TILE_WIDTH, y % TILE_HEIGHT));
|
1998-07-10 10:43:12 +08:00
|
|
|
tile_release (tile, FALSE);
|
1997-11-25 06:05:25 +08:00
|
|
|
if (val != 0)
|
|
|
|
return;
|
|
|
|
|
|
|
|
src->x = x;
|
|
|
|
src->y = y;
|
|
|
|
|
1998-05-02 15:44:58 +08:00
|
|
|
bytes = src->bytes;
|
|
|
|
if(indexed)
|
|
|
|
{
|
|
|
|
bytes = has_alpha ? 4 : 3;
|
|
|
|
}
|
|
|
|
|
2000-03-26 02:17:01 +08:00
|
|
|
if (! find_contiguous_segment (col, src, mask, src->w,
|
|
|
|
src->bytes, has_alpha,
|
|
|
|
antialias, threshold, x, &start, &end))
|
1997-11-25 06:05:25 +08:00
|
|
|
return;
|
|
|
|
|
1998-01-04 08:43:51 +08:00
|
|
|
for (i = start + 1; i < end; i++)
|
1997-11-25 06:05:25 +08:00
|
|
|
{
|
1999-07-07 02:13:59 +08:00
|
|
|
find_contiguous_region_helper (mask, src, has_alpha, antialias,
|
|
|
|
threshold, indexed, i, y - 1, col);
|
|
|
|
find_contiguous_region_helper (mask, src, has_alpha, antialias,
|
|
|
|
threshold, indexed, i, y + 1, col);
|
1997-11-25 06:05:25 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2001-01-29 10:45:02 +08:00
|
|
|
GimpChannel *
|
2001-03-08 10:01:52 +08:00
|
|
|
find_contiguous_region (GimpImage *gimage,
|
1999-07-07 02:13:59 +08:00
|
|
|
GimpDrawable *drawable,
|
2000-03-26 02:17:01 +08:00
|
|
|
gboolean antialias,
|
|
|
|
gint threshold,
|
|
|
|
gint x,
|
|
|
|
gint y,
|
|
|
|
gboolean sample_merged)
|
1997-11-25 06:05:25 +08:00
|
|
|
{
|
2001-01-29 10:45:02 +08:00
|
|
|
PixelRegion srcPR, maskPR;
|
|
|
|
GimpChannel *mask;
|
|
|
|
guchar *start;
|
|
|
|
gboolean has_alpha;
|
|
|
|
gboolean indexed;
|
|
|
|
gint type;
|
|
|
|
gint bytes;
|
|
|
|
Tile *tile;
|
1997-11-25 06:05:25 +08:00
|
|
|
|
|
|
|
if (sample_merged)
|
|
|
|
{
|
2000-12-29 23:22:01 +08:00
|
|
|
pixel_region_init (&srcPR, gimp_image_composite (gimage), 0, 0,
|
1997-11-25 06:05:25 +08:00
|
|
|
gimage->width, gimage->height, FALSE);
|
2000-12-29 23:22:01 +08:00
|
|
|
type = gimp_image_composite_type (gimage);
|
1997-11-25 06:05:25 +08:00
|
|
|
has_alpha = (type == RGBA_GIMAGE ||
|
|
|
|
type == GRAYA_GIMAGE ||
|
|
|
|
type == INDEXEDA_GIMAGE);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2001-01-15 05:11:52 +08:00
|
|
|
pixel_region_init (&srcPR, gimp_drawable_data (drawable),
|
|
|
|
0, 0,
|
|
|
|
gimp_drawable_width (drawable),
|
|
|
|
gimp_drawable_height (drawable),
|
2000-03-26 02:17:01 +08:00
|
|
|
FALSE);
|
2001-01-15 05:11:52 +08:00
|
|
|
has_alpha = gimp_drawable_has_alpha (drawable);
|
1997-11-25 06:05:25 +08:00
|
|
|
}
|
2001-01-15 05:11:52 +08:00
|
|
|
indexed = gimp_drawable_is_indexed (drawable);
|
|
|
|
bytes = gimp_drawable_bytes (drawable);
|
1998-05-02 15:44:58 +08:00
|
|
|
|
2000-03-26 02:17:01 +08:00
|
|
|
if (indexed)
|
1998-05-02 15:44:58 +08:00
|
|
|
{
|
|
|
|
bytes = has_alpha ? 4 : 3;
|
|
|
|
}
|
2001-01-29 10:45:02 +08:00
|
|
|
mask = gimp_channel_new_mask (gimage, srcPR.w, srcPR.h);
|
2001-01-15 05:11:52 +08:00
|
|
|
pixel_region_init (&maskPR, gimp_drawable_data (GIMP_DRAWABLE(mask)),
|
|
|
|
0, 0,
|
|
|
|
gimp_drawable_width (GIMP_DRAWABLE(mask)),
|
|
|
|
gimp_drawable_height (GIMP_DRAWABLE(mask)),
|
1999-07-07 02:13:59 +08:00
|
|
|
TRUE);
|
1997-11-25 06:05:25 +08:00
|
|
|
|
1998-08-16 03:17:36 +08:00
|
|
|
tile = tile_manager_get_tile (srcPR.tiles, x, y, TRUE, FALSE);
|
1997-11-25 06:05:25 +08:00
|
|
|
if (tile)
|
|
|
|
{
|
1998-08-12 01:35:34 +08:00
|
|
|
start = tile_data_pointer (tile, x%TILE_WIDTH, y%TILE_HEIGHT);
|
1997-11-25 06:05:25 +08:00
|
|
|
|
1999-07-07 02:13:59 +08:00
|
|
|
find_contiguous_region_helper (&maskPR, &srcPR, has_alpha, antialias,
|
|
|
|
threshold, bytes, x, y, start);
|
1997-11-25 06:05:25 +08:00
|
|
|
|
1998-07-10 10:43:12 +08:00
|
|
|
tile_release (tile, FALSE);
|
1997-11-25 06:05:25 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return mask;
|
|
|
|
}
|
2001-03-10 01:39:18 +08:00
|
|
|
|
1999-04-19 05:22:41 +08:00
|
|
|
void
|
2001-03-10 01:39:18 +08:00
|
|
|
fuzzy_select (GimpImage *gimage,
|
1999-07-07 02:13:59 +08:00
|
|
|
GimpDrawable *drawable,
|
2000-03-26 02:17:01 +08:00
|
|
|
gint op,
|
|
|
|
gboolean feather,
|
|
|
|
gdouble feather_radius)
|
1997-11-25 06:05:25 +08:00
|
|
|
{
|
2000-03-26 02:17:01 +08:00
|
|
|
gint off_x, off_y;
|
1997-11-25 06:05:25 +08:00
|
|
|
|
|
|
|
/* if applicable, replace the current selection */
|
2000-12-29 23:22:01 +08:00
|
|
|
if (op == CHANNEL_OP_REPLACE)
|
1997-11-25 06:05:25 +08:00
|
|
|
gimage_mask_clear (gimage);
|
|
|
|
else
|
|
|
|
gimage_mask_undo (gimage);
|
|
|
|
|
2000-03-09 19:58:03 +08:00
|
|
|
if (drawable) /* NULL if sample_merged is active */
|
2001-01-15 05:11:52 +08:00
|
|
|
gimp_drawable_offsets (drawable, &off_x, &off_y);
|
2000-03-09 19:58:03 +08:00
|
|
|
else
|
|
|
|
off_x = off_y = 0;
|
|
|
|
|
1997-11-25 06:05:25 +08:00
|
|
|
if (feather)
|
2001-01-29 10:45:02 +08:00
|
|
|
gimp_channel_feather (fuzzy_mask, gimp_image_get_mask (gimage),
|
|
|
|
feather_radius,
|
|
|
|
feather_radius,
|
|
|
|
op, off_x, off_y);
|
1997-11-25 06:05:25 +08:00
|
|
|
else
|
2001-01-29 10:45:02 +08:00
|
|
|
gimp_channel_combine_mask (gimp_image_get_mask (gimage),
|
|
|
|
fuzzy_mask, op, off_x, off_y);
|
1997-11-25 06:05:25 +08:00
|
|
|
|
2001-01-29 08:46:04 +08:00
|
|
|
gtk_object_unref (GTK_OBJECT (fuzzy_mask));
|
1997-11-25 06:05:25 +08:00
|
|
|
fuzzy_mask = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* fuzzy select action functions */
|
|
|
|
|
|
|
|
static void
|
2001-03-10 01:39:18 +08:00
|
|
|
gimp_fuzzy_select_tool_class_init (GimpFuzzySelectToolClass *klass)
|
1997-11-25 06:05:25 +08:00
|
|
|
{
|
2001-03-10 01:39:18 +08:00
|
|
|
GtkObjectClass *object_class;
|
|
|
|
GimpToolClass *tool_class;
|
|
|
|
GimpDrawToolClass *draw_tool_class;
|
1997-11-25 06:05:25 +08:00
|
|
|
|
2001-03-10 01:39:18 +08:00
|
|
|
object_class = (GtkObjectClass *) klass;
|
|
|
|
tool_class = (GimpToolClass *) klass;
|
|
|
|
draw_tool_class = (GimpDrawToolClass *) klass;
|
1997-11-25 06:05:25 +08:00
|
|
|
|
2001-03-10 01:39:18 +08:00
|
|
|
parent_class = gtk_type_class (GIMP_TYPE_SELECTION_TOOL);
|
|
|
|
|
|
|
|
object_class->destroy = gimp_fuzzy_select_tool_destroy;
|
|
|
|
|
|
|
|
tool_class->button_press = gimp_fuzzy_select_tool_button_press;
|
|
|
|
tool_class->button_release = gimp_fuzzy_select_tool_button_release;
|
|
|
|
tool_class->motion = gimp_fuzzy_select_tool_motion;
|
|
|
|
|
|
|
|
draw_tool_class->draw = gimp_fuzzy_select_tool_draw;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gimp_fuzzy_select_tool_init (GimpFuzzySelectTool *fuzzy_select)
|
|
|
|
{
|
|
|
|
GimpTool *tool;
|
|
|
|
GimpSelectionTool *select_tool;
|
|
|
|
|
|
|
|
tool = GIMP_TOOL (fuzzy_select);
|
|
|
|
select_tool = GIMP_SELECTION_TOOL (fuzzy_select);
|
|
|
|
|
|
|
|
if (! fuzzy_options)
|
|
|
|
{
|
2001-04-01 04:41:39 +08:00
|
|
|
fuzzy_options = selection_options_new (GIMP_TYPE_FUZZY_SELECT_TOOL,
|
|
|
|
selection_options_reset);
|
2001-03-10 01:39:18 +08:00
|
|
|
|
|
|
|
tool_manager_register_tool_options (GIMP_TYPE_FUZZY_SELECT_TOOL,
|
|
|
|
(ToolOptions *) fuzzy_options);
|
|
|
|
}
|
|
|
|
|
|
|
|
tool->tool_cursor = GIMP_FUZZY_SELECT_TOOL_CURSOR;
|
|
|
|
tool->scroll_lock = TRUE; /* Do not allow scrolling */
|
|
|
|
|
|
|
|
fuzzy_select->x = 0;
|
|
|
|
fuzzy_select->y = 0;
|
|
|
|
fuzzy_select->first_x = 0;
|
|
|
|
fuzzy_select->first_y = 0;
|
|
|
|
fuzzy_select->first_threshold = 0.0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gimp_fuzzy_select_tool_destroy (GtkObject *object)
|
|
|
|
{
|
|
|
|
if (GTK_OBJECT_CLASS (parent_class)->destroy)
|
|
|
|
GTK_OBJECT_CLASS (parent_class)->destroy (object);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gimp_fuzzy_select_tool_button_press (GimpTool *tool,
|
|
|
|
GdkEventButton *bevent,
|
|
|
|
GDisplay *gdisp)
|
|
|
|
{
|
|
|
|
GimpFuzzySelectTool *fuzzy_sel;
|
|
|
|
|
|
|
|
fuzzy_sel = GIMP_FUZZY_SELECT_TOOL (tool);
|
1997-11-25 06:05:25 +08:00
|
|
|
|
|
|
|
gdk_pointer_grab (gdisp->canvas->window, FALSE,
|
1999-06-26 19:16:47 +08:00
|
|
|
GDK_POINTER_MOTION_HINT_MASK |
|
|
|
|
GDK_BUTTON1_MOTION_MASK |
|
|
|
|
GDK_BUTTON_RELEASE_MASK,
|
1997-11-25 06:05:25 +08:00
|
|
|
NULL, NULL, bevent->time);
|
|
|
|
|
|
|
|
tool->state = ACTIVE;
|
2000-12-31 12:07:42 +08:00
|
|
|
tool->gdisp = gdisp;
|
1997-11-25 06:05:25 +08:00
|
|
|
|
2001-03-10 01:39:18 +08:00
|
|
|
fuzzy_sel->x = bevent->x;
|
|
|
|
fuzzy_sel->y = bevent->y;
|
|
|
|
fuzzy_sel->first_x = fuzzy_sel->x;
|
|
|
|
fuzzy_sel->first_y = fuzzy_sel->y;
|
|
|
|
fuzzy_sel->first_threshold = fuzzy_options->threshold;
|
|
|
|
|
|
|
|
switch (GIMP_SELECTION_TOOL (tool)->op)
|
1997-11-25 06:05:25 +08:00
|
|
|
{
|
2001-03-10 01:39:18 +08:00
|
|
|
case SELECTION_MOVE_MASK:
|
2000-12-31 12:07:42 +08:00
|
|
|
init_edit_selection (tool, gdisp, bevent, EDIT_MASK_TRANSLATE);
|
1997-11-25 06:05:25 +08:00
|
|
|
return;
|
2001-03-10 01:39:18 +08:00
|
|
|
case SELECTION_MOVE:
|
2000-12-31 12:07:42 +08:00
|
|
|
init_edit_selection (tool, gdisp, bevent, EDIT_MASK_TO_LAYER_TRANSLATE);
|
1999-05-05 17:10:35 +08:00
|
|
|
return;
|
2001-03-10 01:39:18 +08:00
|
|
|
default:
|
|
|
|
break;
|
1997-11-25 06:05:25 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* calculate the region boundary */
|
2001-03-10 01:39:18 +08:00
|
|
|
segs = fuzzy_select_calculate (fuzzy_sel, gdisp, &num_segs);
|
1997-11-25 06:05:25 +08:00
|
|
|
|
2001-03-10 01:39:18 +08:00
|
|
|
gimp_draw_tool_start (GIMP_DRAW_TOOL (tool),
|
|
|
|
gdisp->canvas->window);
|
1997-11-25 06:05:25 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2001-03-10 01:39:18 +08:00
|
|
|
gimp_fuzzy_select_tool_button_release (GimpTool *tool,
|
|
|
|
GdkEventButton *bevent,
|
|
|
|
GDisplay *gdisp)
|
1997-11-25 06:05:25 +08:00
|
|
|
{
|
2001-03-10 01:39:18 +08:00
|
|
|
GimpFuzzySelectTool *fuzzy_sel;
|
|
|
|
GimpDrawable *drawable;
|
1997-11-25 06:05:25 +08:00
|
|
|
|
2001-03-10 01:39:18 +08:00
|
|
|
fuzzy_sel = GIMP_FUZZY_SELECT_TOOL (tool);
|
1997-11-25 06:05:25 +08:00
|
|
|
|
|
|
|
gdk_pointer_ungrab (bevent->time);
|
|
|
|
gdk_flush ();
|
|
|
|
|
2001-03-10 01:39:18 +08:00
|
|
|
gimp_draw_tool_stop (GIMP_DRAW_TOOL (tool));
|
1997-11-25 06:05:25 +08:00
|
|
|
tool->state = INACTIVE;
|
|
|
|
|
|
|
|
/* First take care of the case where the user "cancels" the action */
|
|
|
|
if (! (bevent->state & GDK_BUTTON3_MASK))
|
|
|
|
{
|
2000-04-20 23:57:13 +08:00
|
|
|
drawable = (fuzzy_options->sample_merged ?
|
2000-12-29 23:22:01 +08:00
|
|
|
NULL : gimp_image_active_drawable (gdisp->gimage));
|
1999-06-26 19:16:47 +08:00
|
|
|
|
2001-03-10 01:39:18 +08:00
|
|
|
fuzzy_select (gdisp->gimage,
|
|
|
|
drawable,
|
|
|
|
GIMP_SELECTION_TOOL (tool)->op,
|
|
|
|
fuzzy_options->feather,
|
2000-04-20 23:57:13 +08:00
|
|
|
fuzzy_options->feather_radius);
|
2001-03-10 01:39:18 +08:00
|
|
|
|
1997-11-25 06:05:25 +08:00
|
|
|
gdisplays_flush ();
|
|
|
|
}
|
|
|
|
|
|
|
|
/* If the segment array is allocated, free it */
|
|
|
|
if (segs)
|
|
|
|
g_free (segs);
|
|
|
|
segs = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2001-03-10 01:39:18 +08:00
|
|
|
gimp_fuzzy_select_tool_motion (GimpTool *tool,
|
|
|
|
GdkEventMotion *mevent,
|
|
|
|
GDisplay *gdisp)
|
1997-11-25 06:05:25 +08:00
|
|
|
{
|
2001-03-10 01:39:18 +08:00
|
|
|
GimpFuzzySelectTool *fuzzy_sel;
|
|
|
|
GimpSelectionTool *sel_tool;
|
|
|
|
GdkSegment *new_segs;
|
|
|
|
gint num_new_segs;
|
|
|
|
gint diff_x, diff_y;
|
|
|
|
gdouble diff;
|
1997-11-25 06:05:25 +08:00
|
|
|
|
2000-02-19 21:19:08 +08:00
|
|
|
static guint last_time = 0;
|
|
|
|
|
2001-03-10 01:39:18 +08:00
|
|
|
fuzzy_sel = GIMP_FUZZY_SELECT_TOOL (tool);
|
|
|
|
sel_tool = GIMP_SELECTION_TOOL (tool);
|
2000-03-02 07:22:43 +08:00
|
|
|
|
|
|
|
/* needed for immediate cursor update on modifier event */
|
2001-03-10 01:39:18 +08:00
|
|
|
sel_tool->current_x = mevent->x;
|
|
|
|
sel_tool->current_y = mevent->y;
|
2000-03-02 07:22:43 +08:00
|
|
|
|
1997-11-25 06:05:25 +08:00
|
|
|
if (tool->state != ACTIVE)
|
|
|
|
return;
|
|
|
|
|
2000-02-19 21:19:08 +08:00
|
|
|
/* don't let the events come in too fast, ignore below a delay of 100 ms */
|
|
|
|
if (ABS (mevent->time - last_time) < 100)
|
|
|
|
return;
|
2001-03-10 01:39:18 +08:00
|
|
|
|
2000-02-19 21:19:08 +08:00
|
|
|
last_time = mevent->time;
|
|
|
|
|
2000-01-23 06:26:20 +08:00
|
|
|
diff_x = mevent->x - fuzzy_sel->first_x;
|
|
|
|
diff_y = mevent->y - fuzzy_sel->first_y;
|
1997-11-25 06:05:25 +08:00
|
|
|
|
2000-01-23 06:26:20 +08:00
|
|
|
diff = ((ABS (diff_x) > ABS (diff_y)) ? diff_x : diff_y) / 2.0;
|
1997-11-25 06:05:25 +08:00
|
|
|
|
1999-07-07 02:13:59 +08:00
|
|
|
gtk_adjustment_set_value (GTK_ADJUSTMENT (fuzzy_options->threshold_w),
|
2000-01-23 06:26:20 +08:00
|
|
|
fuzzy_sel->first_threshold + diff);
|
2001-03-10 01:39:18 +08:00
|
|
|
|
1997-11-25 06:05:25 +08:00
|
|
|
/* calculate the new fuzzy boundary */
|
2001-03-10 01:39:18 +08:00
|
|
|
new_segs = fuzzy_select_calculate (fuzzy_sel, gdisp, &num_new_segs);
|
1997-11-25 06:05:25 +08:00
|
|
|
|
|
|
|
/* stop the current boundary */
|
2001-03-10 01:39:18 +08:00
|
|
|
gimp_draw_tool_pause (GIMP_DRAW_TOOL (tool));
|
1997-11-25 06:05:25 +08:00
|
|
|
|
|
|
|
/* make sure the XSegment array is freed before we assign the new one */
|
|
|
|
if (segs)
|
|
|
|
g_free (segs);
|
|
|
|
segs = new_segs;
|
|
|
|
num_segs = num_new_segs;
|
|
|
|
|
|
|
|
/* start the new boundary */
|
2001-03-10 01:39:18 +08:00
|
|
|
gimp_draw_tool_resume (GIMP_DRAW_TOOL (tool));
|
1997-11-25 06:05:25 +08:00
|
|
|
}
|
|
|
|
|
1999-07-07 02:13:59 +08:00
|
|
|
|
1997-11-25 06:05:25 +08:00
|
|
|
static GdkSegment *
|
2001-03-10 01:39:18 +08:00
|
|
|
fuzzy_select_calculate (GimpFuzzySelectTool *fuzzy_sel,
|
|
|
|
GDisplay *gdisp,
|
|
|
|
gint *nsegs)
|
1997-11-25 06:05:25 +08:00
|
|
|
{
|
2000-03-26 02:17:01 +08:00
|
|
|
PixelRegion maskPR;
|
2001-01-29 10:45:02 +08:00
|
|
|
GimpChannel *new;
|
2000-03-26 02:17:01 +08:00
|
|
|
GdkSegment *segs;
|
|
|
|
BoundSeg *bsegs;
|
1998-01-22 15:02:57 +08:00
|
|
|
GimpDrawable *drawable;
|
2000-12-31 12:07:42 +08:00
|
|
|
gint i;
|
|
|
|
gint x, y;
|
|
|
|
gboolean use_offsets;
|
1999-01-11 07:36:29 +08:00
|
|
|
|
2000-12-31 12:07:42 +08:00
|
|
|
drawable = gimp_image_active_drawable (gdisp->gimage);
|
1997-11-25 06:05:25 +08:00
|
|
|
|
2001-05-14 05:51:20 +08:00
|
|
|
gimp_set_busy ();
|
2000-03-26 02:17:01 +08:00
|
|
|
|
2000-04-20 23:57:13 +08:00
|
|
|
use_offsets = fuzzy_options->sample_merged ? FALSE : TRUE;
|
1997-11-25 06:05:25 +08:00
|
|
|
|
|
|
|
gdisplay_untransform_coords (gdisp, fuzzy_sel->x,
|
|
|
|
fuzzy_sel->y, &x, &y, FALSE, use_offsets);
|
|
|
|
|
1999-07-07 02:13:59 +08:00
|
|
|
new = find_contiguous_region (gdisp->gimage, drawable,
|
2000-04-20 23:57:13 +08:00
|
|
|
fuzzy_options->antialias,
|
1999-07-07 02:13:59 +08:00
|
|
|
fuzzy_options->threshold, x, y,
|
2000-04-20 23:57:13 +08:00
|
|
|
fuzzy_options->sample_merged);
|
1997-11-25 06:05:25 +08:00
|
|
|
|
|
|
|
if (fuzzy_mask)
|
2000-12-29 00:19:55 +08:00
|
|
|
gtk_object_unref (GTK_OBJECT (fuzzy_mask));
|
|
|
|
|
|
|
|
fuzzy_mask = new;
|
|
|
|
|
|
|
|
gtk_object_ref (GTK_OBJECT (fuzzy_mask));
|
|
|
|
gtk_object_sink (GTK_OBJECT (fuzzy_mask));
|
1997-11-25 06:05:25 +08:00
|
|
|
|
|
|
|
/* calculate and allocate a new XSegment array which represents the boundary
|
|
|
|
* of the color-contiguous region
|
|
|
|
*/
|
2001-01-15 05:11:52 +08:00
|
|
|
pixel_region_init (&maskPR, gimp_drawable_data (GIMP_DRAWABLE (fuzzy_mask)),
|
|
|
|
0, 0,
|
|
|
|
gimp_drawable_width (GIMP_DRAWABLE (fuzzy_mask)),
|
|
|
|
gimp_drawable_height (GIMP_DRAWABLE (fuzzy_mask)),
|
1999-07-07 02:13:59 +08:00
|
|
|
FALSE);
|
1997-11-25 06:05:25 +08:00
|
|
|
bsegs = find_mask_boundary (&maskPR, nsegs, WithinBounds,
|
|
|
|
0, 0,
|
2001-01-15 05:11:52 +08:00
|
|
|
gimp_drawable_width (GIMP_DRAWABLE (fuzzy_mask)),
|
|
|
|
gimp_drawable_height (GIMP_DRAWABLE (fuzzy_mask)));
|
1997-11-25 06:05:25 +08:00
|
|
|
|
2000-03-26 02:17:01 +08:00
|
|
|
segs = g_new (GdkSegment, *nsegs);
|
1997-11-25 06:05:25 +08:00
|
|
|
|
|
|
|
for (i = 0; i < *nsegs; i++)
|
|
|
|
{
|
|
|
|
gdisplay_transform_coords (gdisp, bsegs[i].x1, bsegs[i].y1, &x, &y, use_offsets);
|
|
|
|
segs[i].x1 = x; segs[i].y1 = y;
|
|
|
|
gdisplay_transform_coords (gdisp, bsegs[i].x2, bsegs[i].y2, &x, &y, use_offsets);
|
|
|
|
segs[i].x2 = x; segs[i].y2 = y;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* free boundary segments */
|
|
|
|
g_free (bsegs);
|
|
|
|
|
2001-05-14 05:51:20 +08:00
|
|
|
gimp_unset_busy ();
|
1999-01-11 07:36:29 +08:00
|
|
|
|
1997-11-25 06:05:25 +08:00
|
|
|
return segs;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2001-03-10 01:39:18 +08:00
|
|
|
gimp_fuzzy_select_tool_draw (GimpDrawTool *draw_tool)
|
1997-11-25 06:05:25 +08:00
|
|
|
{
|
2001-03-10 01:39:18 +08:00
|
|
|
GimpFuzzySelectTool *fuzzy_sel;
|
1997-11-25 06:05:25 +08:00
|
|
|
|
2001-03-10 01:39:18 +08:00
|
|
|
fuzzy_sel = GIMP_FUZZY_SELECT_TOOL (draw_tool);
|
1997-11-25 06:05:25 +08:00
|
|
|
|
|
|
|
if (segs)
|
2001-03-10 01:39:18 +08:00
|
|
|
gdk_draw_segments (draw_tool->win,
|
|
|
|
draw_tool->gc,
|
|
|
|
segs, num_segs);
|
1997-11-25 06:05:25 +08:00
|
|
|
}
|