~ubuntu-branches/ubuntu/breezy/gimp/breezy

« back to all changes in this revision

Viewing changes to app/tools/gimpthresholdtool.c

  • Committer: Bazaar Package Importer
  • Author(s): Sebastien Bacher
  • Date: 2005-10-04 19:04:46 UTC
  • Revision ID: james.westby@ubuntu.com-20051004190446-ukh32kwk56s4sjhu
Tags: upstream-2.2.8
ImportĀ upstreamĀ versionĀ 2.2.8

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* The GIMP -- an image manipulation program
 
2
 * Copyright (C) 1995 Spencer Kimball and Peter Mattis
 
3
 *
 
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.
 
8
 *
 
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.
 
13
 *
 
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.
 
17
 */
 
18
 
 
19
#include "config.h"
 
20
 
 
21
#include <gtk/gtk.h>
 
22
 
 
23
#include "libgimpwidgets/gimpwidgets.h"
 
24
 
 
25
#include "tools-types.h"
 
26
 
 
27
#include "base/gimphistogram.h"
 
28
#include "base/threshold.h"
 
29
 
 
30
#include "config/gimpbaseconfig.h"
 
31
 
 
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"
 
38
 
 
39
#include "widgets/gimphelp-ids.h"
 
40
#include "widgets/gimphistogrambox.h"
 
41
#include "widgets/gimphistogramview.h"
 
42
#include "widgets/gimppropwidgets.h"
 
43
 
 
44
#include "display/gimpdisplay.h"
 
45
 
 
46
#include "gimphistogramoptions.h"
 
47
#include "gimpthresholdtool.h"
 
48
 
 
49
#include "gimp-intl.h"
 
50
 
 
51
 
 
52
#define LOW        0x1
 
53
#define HIGH       0x2
 
54
#define HISTOGRAM  0x4
 
55
#define ALL       (LOW | HIGH | HISTOGRAM)
 
56
 
 
57
 
 
58
/*  local function prototypes  */
 
59
 
 
60
static void     gimp_threshold_tool_class_init (GimpThresholdToolClass *klass);
 
61
static void     gimp_threshold_tool_init       (GimpThresholdTool      *threshold_tool);
 
62
 
 
63
static void     gimp_threshold_tool_finalize   (GObject          *object);
 
64
 
 
65
static gboolean gimp_threshold_tool_initialize (GimpTool         *tool,
 
66
                                                GimpDisplay      *gdisp);
 
67
 
 
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);
 
71
 
 
72
static void     gimp_threshold_tool_histogram_range (GimpHistogramView *view,
 
73
                                                     gint               start,
 
74
                                                     gint               end,
 
75
                                                     GimpThresholdTool *t_tool);
 
76
 
 
77
 
 
78
static GimpImageMapToolClass *parent_class = NULL;
 
79
 
 
80
 
 
81
/*  public functions  */
 
82
 
 
83
void
 
84
gimp_threshold_tool_register (GimpToolRegisterCallback  callback,
 
85
                              gpointer                  data)
 
86
{
 
87
  (* callback) (GIMP_TYPE_THRESHOLD_TOOL,
 
88
                GIMP_TYPE_HISTOGRAM_OPTIONS,
 
89
                gimp_histogram_options_gui,
 
90
                0,
 
91
                "gimp-threshold-tool",
 
92
                _("Threshold"),
 
93
                _("Reduce image to two colors using a threshold"),
 
94
                N_("_Threshold..."), NULL,
 
95
                NULL, GIMP_HELP_TOOL_THRESHOLD,
 
96
                GIMP_STOCK_TOOL_THRESHOLD,
 
97
                data);
 
98
}
 
99
 
 
100
GType
 
101
gimp_threshold_tool_get_type (void)
 
102
{
 
103
  static GType tool_type = 0;
 
104
 
 
105
  if (! tool_type)
 
106
    {
 
107
      static const GTypeInfo tool_info =
 
108
      {
 
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),
 
116
        0,              /* n_preallocs    */
 
117
        (GInstanceInitFunc) gimp_threshold_tool_init,
 
118
      };
 
119
 
 
120
      tool_type = g_type_register_static (GIMP_TYPE_IMAGE_MAP_TOOL,
 
121
                                          "GimpThresholdTool",
 
122
                                          &tool_info, 0);
 
123
    }
 
124
 
 
125
  return tool_type;
 
126
}
 
127
 
 
128
 
 
129
/*  private functions  */
 
130
 
 
131
static void
 
132
gimp_threshold_tool_class_init (GimpThresholdToolClass *klass)
 
133
{
 
134
  GObjectClass          *object_class;
 
135
  GimpToolClass         *tool_class;
 
136
  GimpImageMapToolClass *image_map_tool_class;
 
137
 
 
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);
 
141
 
 
142
  parent_class = g_type_class_peek_parent (klass);
 
143
 
 
144
  object_class->finalize       = gimp_threshold_tool_finalize;
 
145
 
 
146
  tool_class->initialize       = gimp_threshold_tool_initialize;
 
147
 
 
148
  image_map_tool_class->shell_desc = _("Apply Threshold");
 
149
 
 
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;
 
153
}
 
154
 
 
155
static void
 
156
gimp_threshold_tool_init (GimpThresholdTool *t_tool)
 
157
{
 
158
  t_tool->threshold = g_new0 (Threshold, 1);
 
159
  t_tool->hist      = NULL;
 
160
 
 
161
  t_tool->threshold->low_threshold  = 127;
 
162
  t_tool->threshold->high_threshold = 255;
 
163
}
 
164
 
 
165
static void
 
166
gimp_threshold_tool_finalize (GObject *object)
 
167
{
 
168
  GimpThresholdTool *t_tool = GIMP_THRESHOLD_TOOL (object);
 
169
 
 
170
  if (t_tool->threshold)
 
171
    {
 
172
      g_free (t_tool->threshold);
 
173
      t_tool->threshold = NULL;
 
174
    }
 
175
 
 
176
  if (t_tool->hist)
 
177
    {
 
178
      gimp_histogram_free (t_tool->hist);
 
179
      t_tool->hist = NULL;
 
180
    }
 
181
 
 
182
  G_OBJECT_CLASS (parent_class)->finalize (object);
 
183
}
 
184
 
 
185
static gboolean
 
186
gimp_threshold_tool_initialize (GimpTool    *tool,
 
187
                                GimpDisplay *gdisp)
 
188
{
 
189
  GimpThresholdTool *t_tool = GIMP_THRESHOLD_TOOL (tool);
 
190
  GimpDrawable      *drawable;
 
191
 
 
192
  drawable = gimp_image_active_drawable (gdisp->gimage);
 
193
 
 
194
  if (! drawable)
 
195
    return FALSE;
 
196
 
 
197
  if (gimp_drawable_is_indexed (drawable))
 
198
    {
 
199
      g_message (_("Threshold does not operate on indexed layers."));
 
200
      return FALSE;
 
201
    }
 
202
 
 
203
  if (!t_tool->hist)
 
204
    {
 
205
      Gimp *gimp = GIMP_TOOL (t_tool)->tool_info->gimp;
 
206
 
 
207
      t_tool->hist = gimp_histogram_new (GIMP_BASE_CONFIG (gimp->config));
 
208
    }
 
209
 
 
210
  t_tool->threshold->color          = gimp_drawable_is_rgb (drawable);
 
211
  t_tool->threshold->low_threshold  = 127;
 
212
  t_tool->threshold->high_threshold = 255;
 
213
 
 
214
  GIMP_TOOL_CLASS (parent_class)->initialize (tool, gdisp);
 
215
 
 
216
  gimp_drawable_calculate_histogram (drawable, t_tool->hist);
 
217
 
 
218
  g_signal_handlers_block_by_func (t_tool->histogram_box->view,
 
219
                                   gimp_threshold_tool_histogram_range,
 
220
                                   t_tool);
 
221
  gimp_histogram_view_set_histogram (t_tool->histogram_box->view,
 
222
                                     t_tool->hist);
 
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,
 
228
                                     t_tool);
 
229
 
 
230
  gimp_image_map_tool_preview (GIMP_IMAGE_MAP_TOOL (t_tool));
 
231
 
 
232
  return TRUE;
 
233
}
 
234
 
 
235
static void
 
236
gimp_threshold_tool_map (GimpImageMapTool *image_map_tool)
 
237
{
 
238
  GimpThresholdTool *t_tool = GIMP_THRESHOLD_TOOL (image_map_tool);
 
239
 
 
240
  gimp_image_map_apply (image_map_tool->image_map,
 
241
                        threshold,
 
242
                        t_tool->threshold);
 
243
}
 
244
 
 
245
 
 
246
/**********************/
 
247
/*  Threshold dialog  */
 
248
/**********************/
 
249
 
 
250
static void
 
251
gimp_threshold_tool_dialog (GimpImageMapTool *image_map_tool)
 
252
{
 
253
  GimpThresholdTool *t_tool = GIMP_THRESHOLD_TOOL (image_map_tool);
 
254
  GimpToolOptions   *tool_options;
 
255
  GtkWidget         *vbox;
 
256
  GtkWidget         *hbox;
 
257
  GtkWidget         *menu;
 
258
  GtkWidget         *box;
 
259
 
 
260
  tool_options = GIMP_TOOL (t_tool)->tool_info->tool_options;
 
261
 
 
262
  vbox = image_map_tool->main_vbox;
 
263
 
 
264
  hbox = gtk_hbox_new (FALSE, 6);
 
265
  gtk_box_pack_start (GTK_BOX (vbox), hbox, FALSE, FALSE, 0);
 
266
  gtk_widget_show (hbox);
 
267
 
 
268
  menu = gimp_prop_enum_stock_box_new (G_OBJECT (tool_options),
 
269
                                       "histogram-scale", "gimp-histogram",
 
270
                                       0, 0);
 
271
  gtk_box_pack_end (GTK_BOX (hbox), menu, FALSE, FALSE, 0);
 
272
  gtk_widget_show (menu);
 
273
 
 
274
  box = gimp_histogram_box_new ();
 
275
  gtk_box_pack_start (GTK_BOX (vbox), box, TRUE, TRUE, 0);
 
276
  gtk_widget_show (box);
 
277
 
 
278
  t_tool->histogram_box = GIMP_HISTOGRAM_BOX (box);
 
279
 
 
280
  g_signal_connect (t_tool->histogram_box->view, "range_changed",
 
281
                    G_CALLBACK (gimp_threshold_tool_histogram_range),
 
282
                    t_tool);
 
283
 
 
284
  gimp_histogram_options_connect_view (GIMP_HISTOGRAM_OPTIONS (tool_options),
 
285
                                       t_tool->histogram_box->view);
 
286
}
 
287
 
 
288
static void
 
289
gimp_threshold_tool_reset (GimpImageMapTool *image_map_tool)
 
290
{
 
291
  GimpThresholdTool *t_tool = GIMP_THRESHOLD_TOOL (image_map_tool);
 
292
 
 
293
  t_tool->threshold->low_threshold  = 127.0;
 
294
  t_tool->threshold->high_threshold = 255.0;
 
295
 
 
296
  gimp_histogram_view_set_range (t_tool->histogram_box->view,
 
297
                                 t_tool->threshold->low_threshold,
 
298
                                 t_tool->threshold->high_threshold);
 
299
}
 
300
 
 
301
static void
 
302
gimp_threshold_tool_histogram_range (GimpHistogramView *widget,
 
303
                                     gint               start,
 
304
                                     gint               end,
 
305
                                     GimpThresholdTool *t_tool)
 
306
{
 
307
  if (start != t_tool->threshold->low_threshold ||
 
308
      end   != t_tool->threshold->high_threshold)
 
309
    {
 
310
      t_tool->threshold->low_threshold  = start;
 
311
      t_tool->threshold->high_threshold = end;
 
312
 
 
313
      gimp_image_map_tool_preview (GIMP_IMAGE_MAP_TOOL (t_tool));
 
314
    }
 
315
}