1
/* The GIMP -- an image manipulation program
2
* Copyright (C) 1995 Spencer Kimball and Peter Mattis
4
* This program is free software; you can redistribute it and/or modify
5
* it under the terms of the GNU General Public License as published by
6
* the Free Software Foundation; either version 2 of the License, or
7
* (at your option) any later version.
9
* This program is distributed in the hope that it will be useful,
10
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12
* GNU General Public License for more details.
14
* You should have received a copy of the GNU General Public License
15
* along with this program; if not, write to the Free Software
16
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
23
#include "libgimpwidgets/gimpwidgets.h"
25
#include "tools-types.h"
27
#include "base/gimphistogram.h"
28
#include "base/threshold.h"
30
#include "config/gimpbaseconfig.h"
32
#include "core/gimp.h"
33
#include "core/gimpdrawable.h"
34
#include "core/gimpdrawable-histogram.h"
35
#include "core/gimpimage.h"
36
#include "core/gimpimagemap.h"
37
#include "core/gimptoolinfo.h"
39
#include "widgets/gimphelp-ids.h"
40
#include "widgets/gimphistogrambox.h"
41
#include "widgets/gimphistogramview.h"
42
#include "widgets/gimppropwidgets.h"
44
#include "display/gimpdisplay.h"
46
#include "gimphistogramoptions.h"
47
#include "gimpthresholdtool.h"
49
#include "gimp-intl.h"
55
#define ALL (LOW | HIGH | HISTOGRAM)
58
/* local function prototypes */
60
static void gimp_threshold_tool_class_init (GimpThresholdToolClass *klass);
61
static void gimp_threshold_tool_init (GimpThresholdTool *threshold_tool);
63
static void gimp_threshold_tool_finalize (GObject *object);
65
static gboolean gimp_threshold_tool_initialize (GimpTool *tool,
68
static void gimp_threshold_tool_map (GimpImageMapTool *image_map_tool);
69
static void gimp_threshold_tool_dialog (GimpImageMapTool *image_map_tool);
70
static void gimp_threshold_tool_reset (GimpImageMapTool *image_map_tool);
72
static void gimp_threshold_tool_histogram_range (GimpHistogramView *view,
75
GimpThresholdTool *t_tool);
78
static GimpImageMapToolClass *parent_class = NULL;
81
/* public functions */
84
gimp_threshold_tool_register (GimpToolRegisterCallback callback,
87
(* callback) (GIMP_TYPE_THRESHOLD_TOOL,
88
GIMP_TYPE_HISTOGRAM_OPTIONS,
89
gimp_histogram_options_gui,
91
"gimp-threshold-tool",
93
_("Reduce image to two colors using a threshold"),
94
N_("_Threshold..."), NULL,
95
NULL, GIMP_HELP_TOOL_THRESHOLD,
96
GIMP_STOCK_TOOL_THRESHOLD,
101
gimp_threshold_tool_get_type (void)
103
static GType tool_type = 0;
107
static const GTypeInfo tool_info =
109
sizeof (GimpThresholdToolClass),
110
(GBaseInitFunc) NULL,
111
(GBaseFinalizeFunc) NULL,
112
(GClassInitFunc) gimp_threshold_tool_class_init,
113
NULL, /* class_finalize */
114
NULL, /* class_data */
115
sizeof (GimpThresholdTool),
117
(GInstanceInitFunc) gimp_threshold_tool_init,
120
tool_type = g_type_register_static (GIMP_TYPE_IMAGE_MAP_TOOL,
129
/* private functions */
132
gimp_threshold_tool_class_init (GimpThresholdToolClass *klass)
134
GObjectClass *object_class;
135
GimpToolClass *tool_class;
136
GimpImageMapToolClass *image_map_tool_class;
138
object_class = G_OBJECT_CLASS (klass);
139
tool_class = GIMP_TOOL_CLASS (klass);
140
image_map_tool_class = GIMP_IMAGE_MAP_TOOL_CLASS (klass);
142
parent_class = g_type_class_peek_parent (klass);
144
object_class->finalize = gimp_threshold_tool_finalize;
146
tool_class->initialize = gimp_threshold_tool_initialize;
148
image_map_tool_class->shell_desc = _("Apply Threshold");
150
image_map_tool_class->map = gimp_threshold_tool_map;
151
image_map_tool_class->dialog = gimp_threshold_tool_dialog;
152
image_map_tool_class->reset = gimp_threshold_tool_reset;
156
gimp_threshold_tool_init (GimpThresholdTool *t_tool)
158
t_tool->threshold = g_new0 (Threshold, 1);
161
t_tool->threshold->low_threshold = 127;
162
t_tool->threshold->high_threshold = 255;
166
gimp_threshold_tool_finalize (GObject *object)
168
GimpThresholdTool *t_tool = GIMP_THRESHOLD_TOOL (object);
170
if (t_tool->threshold)
172
g_free (t_tool->threshold);
173
t_tool->threshold = NULL;
178
gimp_histogram_free (t_tool->hist);
182
G_OBJECT_CLASS (parent_class)->finalize (object);
186
gimp_threshold_tool_initialize (GimpTool *tool,
189
GimpThresholdTool *t_tool = GIMP_THRESHOLD_TOOL (tool);
190
GimpDrawable *drawable;
192
drawable = gimp_image_active_drawable (gdisp->gimage);
197
if (gimp_drawable_is_indexed (drawable))
199
g_message (_("Threshold does not operate on indexed layers."));
205
Gimp *gimp = GIMP_TOOL (t_tool)->tool_info->gimp;
207
t_tool->hist = gimp_histogram_new (GIMP_BASE_CONFIG (gimp->config));
210
t_tool->threshold->color = gimp_drawable_is_rgb (drawable);
211
t_tool->threshold->low_threshold = 127;
212
t_tool->threshold->high_threshold = 255;
214
GIMP_TOOL_CLASS (parent_class)->initialize (tool, gdisp);
216
gimp_drawable_calculate_histogram (drawable, t_tool->hist);
218
g_signal_handlers_block_by_func (t_tool->histogram_box->view,
219
gimp_threshold_tool_histogram_range,
221
gimp_histogram_view_set_histogram (t_tool->histogram_box->view,
223
gimp_histogram_view_set_range (t_tool->histogram_box->view,
224
t_tool->threshold->low_threshold,
225
t_tool->threshold->high_threshold);
226
g_signal_handlers_unblock_by_func (t_tool->histogram_box->view,
227
gimp_threshold_tool_histogram_range,
230
gimp_image_map_tool_preview (GIMP_IMAGE_MAP_TOOL (t_tool));
236
gimp_threshold_tool_map (GimpImageMapTool *image_map_tool)
238
GimpThresholdTool *t_tool = GIMP_THRESHOLD_TOOL (image_map_tool);
240
gimp_image_map_apply (image_map_tool->image_map,
246
/**********************/
247
/* Threshold dialog */
248
/**********************/
251
gimp_threshold_tool_dialog (GimpImageMapTool *image_map_tool)
253
GimpThresholdTool *t_tool = GIMP_THRESHOLD_TOOL (image_map_tool);
254
GimpToolOptions *tool_options;
260
tool_options = GIMP_TOOL (t_tool)->tool_info->tool_options;
262
vbox = image_map_tool->main_vbox;
264
hbox = gtk_hbox_new (FALSE, 6);
265
gtk_box_pack_start (GTK_BOX (vbox), hbox, FALSE, FALSE, 0);
266
gtk_widget_show (hbox);
268
menu = gimp_prop_enum_stock_box_new (G_OBJECT (tool_options),
269
"histogram-scale", "gimp-histogram",
271
gtk_box_pack_end (GTK_BOX (hbox), menu, FALSE, FALSE, 0);
272
gtk_widget_show (menu);
274
box = gimp_histogram_box_new ();
275
gtk_box_pack_start (GTK_BOX (vbox), box, TRUE, TRUE, 0);
276
gtk_widget_show (box);
278
t_tool->histogram_box = GIMP_HISTOGRAM_BOX (box);
280
g_signal_connect (t_tool->histogram_box->view, "range_changed",
281
G_CALLBACK (gimp_threshold_tool_histogram_range),
284
gimp_histogram_options_connect_view (GIMP_HISTOGRAM_OPTIONS (tool_options),
285
t_tool->histogram_box->view);
289
gimp_threshold_tool_reset (GimpImageMapTool *image_map_tool)
291
GimpThresholdTool *t_tool = GIMP_THRESHOLD_TOOL (image_map_tool);
293
t_tool->threshold->low_threshold = 127.0;
294
t_tool->threshold->high_threshold = 255.0;
296
gimp_histogram_view_set_range (t_tool->histogram_box->view,
297
t_tool->threshold->low_threshold,
298
t_tool->threshold->high_threshold);
302
gimp_threshold_tool_histogram_range (GimpHistogramView *widget,
305
GimpThresholdTool *t_tool)
307
if (start != t_tool->threshold->low_threshold ||
308
end != t_tool->threshold->high_threshold)
310
t_tool->threshold->low_threshold = start;
311
t_tool->threshold->high_threshold = end;
313
gimp_image_map_tool_preview (GIMP_IMAGE_MAP_TOOL (t_tool));