gimpforegroundselecttool.c 9.4 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
/* The GIMP -- an image manipulation program
 * Copyright (C) 1995 Spencer Kimball and Peter Mattis
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 */

#include "config.h"

#include <gtk/gtk.h>

#include "libgimpwidgets/gimpwidgets.h"

#include "tools-types.h"

27
#include "core/gimp.h"
28
29
30
31
#include "core/gimpchannel.h"
#include "core/gimpchannel-select.h"
#include "core/gimpdrawable-foreground-extract.h"
#include "core/gimpimage.h"
Sven Neumann's avatar
Sven Neumann committed
32
#include "core/gimpprogress.h"
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#include "core/gimpscanconvert.h"
#include "core/gimptoolinfo.h"

#include "widgets/gimphelp-ids.h"

#include "display/gimpdisplay.h"

#include "gimpforegroundselecttool.h"
#include "gimpselectionoptions.h"
#include "gimptoolcontrol.h"

#include "gimp-intl.h"


static void   gimp_foreground_select_tool_class_init (GimpForegroundSelectToolClass *klass);
static void   gimp_foreground_select_tool_init       (GimpForegroundSelectTool      *fg_select);
static void   gimp_foreground_select_tool_finalize       (GObject         *object);

static void   gimp_foreground_select_tool_button_press   (GimpTool        *tool,
                                                          GimpCoords      *coords,
                                                          guint32          time,
                                                          GdkModifierType  state,
                                                          GimpDisplay     *gdisp);
static void   gimp_foreground_select_tool_button_release (GimpTool        *tool,
                                                          GimpCoords      *coords,
                                                          guint32          time,
                                                          GdkModifierType  state,
                                                          GimpDisplay     *gdisp);
static void   gimp_foreground_select_tool_motion         (GimpTool        *tool,
                                                          GimpCoords      *coords,
                                                          guint32          time,
                                                          GdkModifierType  state,
                                                          GimpDisplay     *gdisp);

static void   gimp_foreground_select_tool_draw           (GimpDrawTool    *draw_tool);

static void   gimp_foreground_select_tool_select      (GimpFreeSelectTool *free_sel,
Sven Neumann's avatar
Sven Neumann committed
70
                                                       GimpDisplay        *gdisp);
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87


static GimpFreeSelectToolClass *parent_class = NULL;


/*  public functions  */

void
gimp_foreground_select_tool_register (GimpToolRegisterCallback  callback,
                                      gpointer                  data)
{
  (* callback) (GIMP_TYPE_FOREGROUND_SELECT_TOOL,
                GIMP_TYPE_SELECTION_OPTIONS,
                gimp_selection_options_gui,
                0,
                "gimp-foreground-select-tool",
                _("Foreground Select"),
88
                _("Extract foreground using SIOX algorithm"),
89
90
                N_("_Foreground Select"), NULL,
                NULL, GIMP_HELP_TOOL_FOREGROUND_SELECT,
91
                GIMP_STOCK_TOOL_FOREGROUND_SELECT,
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
                data);
}

GType
gimp_foreground_select_tool_get_type (void)
{
  static GType tool_type = 0;

  if (! tool_type)
    {
      static const GTypeInfo tool_info =
      {
        sizeof (GimpForegroundSelectToolClass),
        (GBaseInitFunc) NULL,
        (GBaseFinalizeFunc) NULL,
        (GClassInitFunc) gimp_foreground_select_tool_class_init,
        NULL,           /* class_finalize */
        NULL,           /* class_data     */
        sizeof (GimpForegroundSelectTool),
        0,              /* n_preallocs    */
        (GInstanceInitFunc) gimp_foreground_select_tool_init,
      };

      tool_type = g_type_register_static (GIMP_TYPE_FREE_SELECT_TOOL,
                                          "GimpForegroundSelectTool",
                                          &tool_info, 0);
    }

  return tool_type;
}

/*  private functions  */

static void
gimp_foreground_select_tool_class_init (GimpForegroundSelectToolClass *klass)
{
  GObjectClass            *object_class    = G_OBJECT_CLASS (klass);
  GimpToolClass           *tool_class      = GIMP_TOOL_CLASS (klass);
  GimpDrawToolClass       *draw_tool_class = GIMP_DRAW_TOOL_CLASS (klass);
  GimpFreeSelectToolClass *free_select_tool_class = GIMP_FREE_SELECT_TOOL_CLASS (klass);

  parent_class = g_type_class_peek_parent (klass);

  object_class->finalize         = gimp_foreground_select_tool_finalize;

  tool_class->button_press       = gimp_foreground_select_tool_button_press;
  tool_class->button_release     = gimp_foreground_select_tool_button_release;
  tool_class->motion             = gimp_foreground_select_tool_motion;

  draw_tool_class->draw          = gimp_foreground_select_tool_draw;

  free_select_tool_class->select = gimp_foreground_select_tool_select;
}

static void
gimp_foreground_select_tool_init (GimpForegroundSelectTool *fg_select)
{
  GimpTool *tool = GIMP_TOOL (fg_select);

  gimp_tool_control_set_scroll_lock (tool->control, TRUE);
  gimp_tool_control_set_tool_cursor (tool->control,
                                     GIMP_TOOL_CURSOR_FREE_SELECT);

  fg_select->mask = NULL;
}

static void
gimp_foreground_select_tool_finalize (GObject *object)
{
  GimpForegroundSelectTool *fg_select = GIMP_FOREGROUND_SELECT_TOOL (object);

  if (fg_select->mask)
    {
      g_object_unref (fg_select->mask);
      fg_select->mask = NULL;
    }

  G_OBJECT_CLASS (parent_class)->finalize (object);
}

static void
gimp_foreground_select_tool_button_press (GimpTool        *tool,
                                          GimpCoords      *coords,
                                          guint32          time,
                                          GdkModifierType  state,
                                          GimpDisplay     *gdisp)
{
  GIMP_TOOL_CLASS (parent_class)->button_press (tool,
                                                coords, time, state, gdisp);
}

static void
gimp_foreground_select_tool_button_release (GimpTool        *tool,
                                            GimpCoords      *coords,
                                            guint32          time,
                                            GdkModifierType  state,
                                            GimpDisplay     *gdisp)
{
  GIMP_TOOL_CLASS (parent_class)->button_release (tool,
                                                  coords, time, state, gdisp);
}

static void
gimp_foreground_select_tool_motion (GimpTool        *tool,
                                    GimpCoords      *coords,
                                    guint32          time,
                                    GdkModifierType  state,
                                    GimpDisplay     *gdisp)
{
  GIMP_TOOL_CLASS (parent_class)->motion (tool,
                                          coords, time, state, gdisp);
}

static void
gimp_foreground_select_tool_draw (GimpDrawTool *draw_tool)
{
  GIMP_DRAW_TOOL_CLASS (parent_class)->draw (draw_tool);
}

static void
gimp_foreground_select_tool_select (GimpFreeSelectTool *free_sel,
Sven Neumann's avatar
Sven Neumann committed
213
                                    GimpDisplay        *gdisp)
214
215
{
  GimpTool             *tool     = GIMP_TOOL (free_sel);
Sven Neumann's avatar
Sven Neumann committed
216
  GimpImage            *gimage   = gdisp->gimage;
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
  GimpDrawable         *drawable = gimp_image_active_drawable (gimage);
  GimpScanConvert      *scan_convert;
  GimpChannel          *mask;
  GimpSelectionOptions *options;

  if (! drawable)
    return;

  options = GIMP_SELECTION_OPTIONS (tool->tool_info->tool_options);

  gimp_set_busy (gimage->gimp);

  scan_convert = gimp_scan_convert_new ();
  gimp_scan_convert_add_polyline (scan_convert,
                                  free_sel->num_points, free_sel->points, TRUE);

  mask = gimp_channel_new (gimage,
                           gimp_image_get_width (gimage),
                           gimp_image_get_height (gimage),
                           "foreground-extraction", NULL);

  gimp_scan_convert_render_value (scan_convert,
                                  gimp_drawable_data (GIMP_DRAWABLE (mask)),
                                  0, 0, 127);
  gimp_scan_convert_free (scan_convert);

243
244
  gimp_drawable_foreground_extract (drawable,
                                    GIMP_FOREGROUND_EXTRACT_SIOX,
Sven Neumann's avatar
Sven Neumann committed
245
246
                                    GIMP_DRAWABLE (mask),
                                    GIMP_PROGRESS (gdisp));
247
248
249
250
251
252
253
254
255
256
257
258
259

  gimp_channel_select_channel (gimp_image_get_mask (gimage),
                               tool->tool_info->blurb,
                               mask, 0, 0,
                               GIMP_SELECTION_TOOL (tool)->op,
                               options->feather,
                               options->feather_radius,
                               options->feather_radius);

  g_object_unref (mask);

  gimp_unset_busy (gimage->gimp);
}