~ubuntu-branches/ubuntu/hoary/gimp/hoary

« back to all changes in this revision

Viewing changes to app/tools/gimpposterizetool.c

  • Committer: Bazaar Package Importer
  • Author(s): Sebastien Bacher
  • Date: 2005-04-04 14:51:23 UTC
  • Revision ID: james.westby@ubuntu.com-20050404145123-9py049eeelfymur8
Tags: upstream-2.2.2
ImportĀ upstreamĀ versionĀ 2.2.2

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/gimplut.h"
 
28
#include "base/lut-funcs.h"
 
29
 
 
30
#include "core/gimpdrawable.h"
 
31
#include "core/gimpimage.h"
 
32
#include "core/gimpimagemap.h"
 
33
 
 
34
#include "widgets/gimphelp-ids.h"
 
35
 
 
36
#include "display/gimpdisplay.h"
 
37
 
 
38
#include "gimpimagemapoptions.h"
 
39
#include "gimpposterizetool.h"
 
40
 
 
41
#include "gimp-intl.h"
 
42
 
 
43
 
 
44
#define POSTERIZE_DEFAULT_LEVELS 3
 
45
 
 
46
#define SLIDER_WIDTH 200
 
47
 
 
48
 
 
49
static void     gimp_posterize_tool_class_init (GimpPosterizeToolClass *klass);
 
50
static void     gimp_posterize_tool_init       (GimpPosterizeTool      *bc_tool);
 
51
 
 
52
static void     gimp_posterize_tool_finalize   (GObject          *object);
 
53
 
 
54
static gboolean gimp_posterize_tool_initialize (GimpTool         *tool,
 
55
                                                GimpDisplay      *gdisp);
 
56
 
 
57
static void     gimp_posterize_tool_map        (GimpImageMapTool *image_map_tool);
 
58
static void     gimp_posterize_tool_dialog     (GimpImageMapTool *image_map_tool);
 
59
static void     gimp_posterize_tool_reset      (GimpImageMapTool *image_map_tool);
 
60
 
 
61
static void     posterize_levels_adjustment_update (GtkAdjustment     *adjustment,
 
62
                                                    GimpPosterizeTool *posterize_tool);
 
63
 
 
64
 
 
65
static GimpImageMapToolClass *parent_class = NULL;
 
66
 
 
67
 
 
68
/*  functions  */
 
69
 
 
70
void
 
71
gimp_posterize_tool_register (GimpToolRegisterCallback  callback,
 
72
                              gpointer                  data)
 
73
{
 
74
  (* callback) (GIMP_TYPE_POSTERIZE_TOOL,
 
75
                GIMP_TYPE_IMAGE_MAP_OPTIONS, NULL,
 
76
                0,
 
77
                "gimp-posterize-tool",
 
78
                _("Posterize"),
 
79
                _("Reduce image to a fixed number of colors"),
 
80
                N_("_Posterize..."), NULL,
 
81
                NULL, GIMP_HELP_TOOL_POSTERIZE,
 
82
                GIMP_STOCK_TOOL_POSTERIZE,
 
83
                data);
 
84
}
 
85
 
 
86
GType
 
87
gimp_posterize_tool_get_type (void)
 
88
{
 
89
  static GType tool_type = 0;
 
90
 
 
91
  if (! tool_type)
 
92
    {
 
93
      static const GTypeInfo tool_info =
 
94
      {
 
95
        sizeof (GimpPosterizeToolClass),
 
96
        (GBaseInitFunc) NULL,
 
97
        (GBaseFinalizeFunc) NULL,
 
98
        (GClassInitFunc) gimp_posterize_tool_class_init,
 
99
        NULL,           /* class_finalize */
 
100
        NULL,           /* class_data     */
 
101
        sizeof (GimpPosterizeTool),
 
102
        0,              /* n_preallocs    */
 
103
        (GInstanceInitFunc) gimp_posterize_tool_init,
 
104
      };
 
105
 
 
106
      tool_type = g_type_register_static (GIMP_TYPE_IMAGE_MAP_TOOL,
 
107
                                          "GimpPosterizeTool",
 
108
                                          &tool_info, 0);
 
109
    }
 
110
 
 
111
  return tool_type;
 
112
}
 
113
 
 
114
static void
 
115
gimp_posterize_tool_class_init (GimpPosterizeToolClass *klass)
 
116
{
 
117
  GObjectClass          *object_class;
 
118
  GimpToolClass         *tool_class;
 
119
  GimpImageMapToolClass *image_map_tool_class;
 
120
 
 
121
  object_class         = G_OBJECT_CLASS (klass);
 
122
  tool_class           = GIMP_TOOL_CLASS (klass);
 
123
  image_map_tool_class = GIMP_IMAGE_MAP_TOOL_CLASS (klass);
 
124
 
 
125
  parent_class = g_type_class_peek_parent (klass);
 
126
 
 
127
  object_class->finalize       = gimp_posterize_tool_finalize;
 
128
 
 
129
  tool_class->initialize       = gimp_posterize_tool_initialize;
 
130
 
 
131
  image_map_tool_class->shell_desc = _("Posterize (Reduce Number of Colors)");
 
132
 
 
133
  image_map_tool_class->map    = gimp_posterize_tool_map;
 
134
  image_map_tool_class->dialog = gimp_posterize_tool_dialog;
 
135
  image_map_tool_class->reset  = gimp_posterize_tool_reset;
 
136
}
 
137
 
 
138
static void
 
139
gimp_posterize_tool_init (GimpPosterizeTool *posterize_tool)
 
140
{
 
141
  posterize_tool->levels = POSTERIZE_DEFAULT_LEVELS;
 
142
  posterize_tool->lut    = gimp_lut_new ();
 
143
}
 
144
 
 
145
static void
 
146
gimp_posterize_tool_finalize (GObject *object)
 
147
{
 
148
  GimpPosterizeTool *posterize_tool = GIMP_POSTERIZE_TOOL (object);
 
149
 
 
150
  if (posterize_tool->lut)
 
151
    {
 
152
      gimp_lut_free (posterize_tool->lut);
 
153
      posterize_tool->lut = NULL;
 
154
    }
 
155
 
 
156
  G_OBJECT_CLASS (parent_class)->finalize (object);
 
157
}
 
158
 
 
159
static gboolean
 
160
gimp_posterize_tool_initialize (GimpTool    *tool,
 
161
                                GimpDisplay *gdisp)
 
162
{
 
163
  GimpPosterizeTool *posterize_tool = GIMP_POSTERIZE_TOOL (tool);
 
164
  GimpDrawable      *drawable;
 
165
 
 
166
  drawable = gimp_image_active_drawable (gdisp->gimage);
 
167
 
 
168
  if (! drawable)
 
169
    return FALSE;
 
170
 
 
171
  if (gimp_drawable_is_indexed (drawable))
 
172
    {
 
173
      g_message (_("Posterize does not operate on indexed layers."));
 
174
      return FALSE;
 
175
    }
 
176
 
 
177
  posterize_tool->levels = POSTERIZE_DEFAULT_LEVELS;
 
178
 
 
179
  GIMP_TOOL_CLASS (parent_class)->initialize (tool, gdisp);
 
180
 
 
181
  gtk_adjustment_set_value (GTK_ADJUSTMENT (posterize_tool->levels_data),
 
182
                            posterize_tool->levels);
 
183
 
 
184
  gimp_image_map_tool_preview (GIMP_IMAGE_MAP_TOOL (posterize_tool));
 
185
 
 
186
  return TRUE;
 
187
}
 
188
 
 
189
static void
 
190
gimp_posterize_tool_map (GimpImageMapTool *image_map_tool)
 
191
{
 
192
  GimpPosterizeTool *posterize_tool = GIMP_POSTERIZE_TOOL (image_map_tool);
 
193
 
 
194
  posterize_lut_setup (posterize_tool->lut,
 
195
                       posterize_tool->levels,
 
196
                       gimp_drawable_bytes (image_map_tool->drawable));
 
197
  gimp_image_map_apply (image_map_tool->image_map,
 
198
                        (GimpImageMapApplyFunc) gimp_lut_process_2,
 
199
                        posterize_tool->lut);
 
200
}
 
201
 
 
202
 
 
203
/**********************/
 
204
/*  Posterize dialog  */
 
205
/**********************/
 
206
 
 
207
static void
 
208
gimp_posterize_tool_dialog (GimpImageMapTool *image_map_tool)
 
209
{
 
210
  GimpPosterizeTool *posterize_tool = GIMP_POSTERIZE_TOOL (image_map_tool);
 
211
  GtkWidget         *table;
 
212
  GtkWidget         *slider;
 
213
  GtkObject         *data;
 
214
 
 
215
  /*  The table containing sliders  */
 
216
  table = gtk_table_new (1, 3, FALSE);
 
217
  gtk_table_set_col_spacings (GTK_TABLE (table), 4);
 
218
  gtk_box_pack_start (GTK_BOX (image_map_tool->main_vbox), table,
 
219
                      FALSE, FALSE, 0);
 
220
  gtk_widget_show (table);
 
221
 
 
222
  data = gimp_scale_entry_new (GTK_TABLE (table), 0, 0,
 
223
                               _("Posterize _levels:"), SLIDER_WIDTH, -1,
 
224
                               posterize_tool->levels,
 
225
                               2.0, 256.0, 1.0, 10.0, 0,
 
226
                               TRUE, 0.0, 0.0,
 
227
                               NULL, NULL);
 
228
  posterize_tool->levels_data = GTK_ADJUSTMENT (data);
 
229
  slider = GIMP_SCALE_ENTRY_SCALE (data);
 
230
  gtk_range_set_update_policy (GTK_RANGE (slider), GTK_UPDATE_DELAYED);
 
231
 
 
232
  g_signal_connect (posterize_tool->levels_data, "value_changed",
 
233
                    G_CALLBACK (posterize_levels_adjustment_update),
 
234
                    posterize_tool);
 
235
}
 
236
 
 
237
static void
 
238
gimp_posterize_tool_reset (GimpImageMapTool *image_map_tool)
 
239
{
 
240
  GimpPosterizeTool *posterize_tool = GIMP_POSTERIZE_TOOL (image_map_tool);
 
241
 
 
242
  posterize_tool->levels = POSTERIZE_DEFAULT_LEVELS;
 
243
 
 
244
  gtk_adjustment_set_value (GTK_ADJUSTMENT (posterize_tool->levels_data),
 
245
                            posterize_tool->levels);
 
246
}
 
247
 
 
248
static void
 
249
posterize_levels_adjustment_update (GtkAdjustment     *adjustment,
 
250
                                    GimpPosterizeTool *posterize_tool)
 
251
{
 
252
  if (posterize_tool->levels != adjustment->value)
 
253
    {
 
254
      posterize_tool->levels = adjustment->value;
 
255
 
 
256
      gimp_image_map_tool_preview (GIMP_IMAGE_MAP_TOOL (posterize_tool));
 
257
    }
 
258
}