27
27
#include "base/gimphistogram.h"
28
28
#include "base/threshold.h"
30
#include "config/gimpbaseconfig.h"
32
#include "core/gimp.h"
33
30
#include "core/gimpdrawable.h"
34
31
#include "core/gimpdrawable-histogram.h"
35
32
#include "core/gimpimage.h"
36
33
#include "core/gimpimagemap.h"
37
#include "core/gimptoolinfo.h"
39
35
#include "widgets/gimphelp-ids.h"
40
36
#include "widgets/gimphistogrambox.h"
41
37
#include "widgets/gimphistogramview.h"
42
#include "widgets/gimppropwidgets.h"
44
39
#include "display/gimpdisplay.h"
49
44
#include "gimp-intl.h"
55
#define ALL (LOW | HIGH | HISTOGRAM)
58
47
/* 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);
49
static void gimp_threshold_tool_finalize (GObject *object);
51
static gboolean gimp_threshold_tool_initialize (GimpTool *tool,
55
static void gimp_threshold_tool_map (GimpImageMapTool *im_tool);
56
static void gimp_threshold_tool_dialog (GimpImageMapTool *im_tool);
57
static void gimp_threshold_tool_reset (GimpImageMapTool *im_tool);
72
59
static void gimp_threshold_tool_histogram_range (GimpHistogramView *view,
75
62
GimpThresholdTool *t_tool);
78
static GimpImageMapToolClass *parent_class = NULL;
81
/* public functions */
63
static void gimp_threshold_tool_auto_clicked (GtkWidget *button,
64
GimpThresholdTool *t_tool);
67
G_DEFINE_TYPE (GimpThresholdTool, gimp_threshold_tool,
68
GIMP_TYPE_IMAGE_MAP_TOOL)
70
#define parent_class gimp_threshold_tool_parent_class
84
74
gimp_threshold_tool_register (GimpToolRegisterCallback callback,
91
81
"gimp-threshold-tool",
93
_("Reduce image to two colors using a threshold"),
83
_("Threshold Tool: Reduce image to two colors using a threshold"),
94
84
N_("_Threshold..."), NULL,
95
85
NULL, GIMP_HELP_TOOL_THRESHOLD,
96
86
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
91
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;
93
GObjectClass *object_class = G_OBJECT_CLASS (klass);
94
GimpToolClass *tool_class = GIMP_TOOL_CLASS (klass);
95
GimpImageMapToolClass *im_tool_class = GIMP_IMAGE_MAP_TOOL_CLASS (klass);
97
object_class->finalize = gimp_threshold_tool_finalize;
99
tool_class->initialize = gimp_threshold_tool_initialize;
101
im_tool_class->shell_desc = _("Apply Threshold");
103
im_tool_class->map = gimp_threshold_tool_map;
104
im_tool_class->dialog = gimp_threshold_tool_dialog;
105
im_tool_class->reset = gimp_threshold_tool_reset;
186
gimp_threshold_tool_initialize (GimpTool *tool,
139
gimp_threshold_tool_initialize (GimpTool *tool,
140
GimpDisplay *display,
189
143
GimpThresholdTool *t_tool = GIMP_THRESHOLD_TOOL (tool);
190
144
GimpDrawable *drawable;
192
drawable = gimp_image_active_drawable (gdisp->gimage);
146
drawable = gimp_image_active_drawable (display->image);
197
151
if (gimp_drawable_is_indexed (drawable))
199
g_message (_("Threshold does not operate on indexed layers."));
153
g_set_error (error, 0, 0,
154
_("Threshold does not operate on indexed layers."));
203
158
if (!t_tool->hist)
205
Gimp *gimp = GIMP_TOOL (t_tool)->tool_info->gimp;
207
t_tool->hist = gimp_histogram_new (GIMP_BASE_CONFIG (gimp->config));
159
t_tool->hist = gimp_histogram_new ();
210
161
t_tool->threshold->color = gimp_drawable_is_rgb (drawable);
211
162
t_tool->threshold->low_threshold = 127;
212
163
t_tool->threshold->high_threshold = 255;
214
GIMP_TOOL_CLASS (parent_class)->initialize (tool, gdisp);
165
GIMP_TOOL_CLASS (parent_class)->initialize (tool, display, error);
216
167
gimp_drawable_calculate_histogram (drawable, t_tool->hist);
251
202
gimp_threshold_tool_dialog (GimpImageMapTool *image_map_tool)
253
GimpThresholdTool *t_tool = GIMP_THRESHOLD_TOOL (image_map_tool);
254
GimpToolOptions *tool_options;
204
GimpThresholdTool *t_tool = GIMP_THRESHOLD_TOOL (image_map_tool);
205
GimpToolOptions *tool_options = GIMP_TOOL_GET_OPTIONS (image_map_tool);
260
tool_options = GIMP_TOOL (t_tool)->tool_info->tool_options;
262
212
vbox = image_map_tool->main_vbox;
278
228
t_tool->histogram_box = GIMP_HISTOGRAM_BOX (box);
280
g_signal_connect (t_tool->histogram_box->view, "range_changed",
230
g_signal_connect (t_tool->histogram_box->view, "range-changed",
281
231
G_CALLBACK (gimp_threshold_tool_histogram_range),
284
234
gimp_histogram_options_connect_view (GIMP_HISTOGRAM_OPTIONS (tool_options),
285
235
t_tool->histogram_box->view);
237
hbox = gtk_hbox_new (FALSE, 6);
238
gtk_box_pack_start (GTK_BOX (vbox), hbox, FALSE, FALSE, 0);
239
gtk_widget_show (hbox);
241
button = gtk_button_new_with_mnemonic (_("_Auto"));
242
gtk_box_pack_end (GTK_BOX (hbox), button, FALSE, FALSE, 0);
243
gimp_help_set_help_data (button, _("Automatically adjust to optimal "
244
"binarization threshold"), NULL);
245
gtk_widget_show (button);
247
g_signal_connect (button, "clicked",
248
G_CALLBACK (gimp_threshold_tool_auto_clicked),
291
255
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);
257
gimp_histogram_view_set_range (t_tool->histogram_box->view, 127.0, 255.0);
313
272
gimp_image_map_tool_preview (GIMP_IMAGE_MAP_TOOL (t_tool));
277
gimp_threshold_tool_auto_clicked (GtkWidget *button,
278
GimpThresholdTool *t_tool)
280
gdouble low_threshold;
283
gimp_histogram_get_threshold (t_tool->hist,
284
(t_tool->threshold->color ?
285
GIMP_HISTOGRAM_RGB : GIMP_HISTOGRAM_VALUE),
288
gimp_histogram_view_set_range (t_tool->histogram_box->view,
289
low_threshold, 255.0);