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/gimplut.h"
28
#include "base/lut-funcs.h"
30
#include "core/gimpdrawable.h"
31
#include "core/gimpimage.h"
32
#include "core/gimpimagemap.h"
34
#include "widgets/gimphelp-ids.h"
36
#include "display/gimpdisplay.h"
38
#include "gimpimagemapoptions.h"
39
#include "gimpposterizetool.h"
41
#include "gimp-intl.h"
44
#define POSTERIZE_DEFAULT_LEVELS 3
46
#define SLIDER_WIDTH 200
49
static void gimp_posterize_tool_class_init (GimpPosterizeToolClass *klass);
50
static void gimp_posterize_tool_init (GimpPosterizeTool *bc_tool);
52
static void gimp_posterize_tool_finalize (GObject *object);
54
static gboolean gimp_posterize_tool_initialize (GimpTool *tool,
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);
61
static void posterize_levels_adjustment_update (GtkAdjustment *adjustment,
62
GimpPosterizeTool *posterize_tool);
65
static GimpImageMapToolClass *parent_class = NULL;
71
gimp_posterize_tool_register (GimpToolRegisterCallback callback,
74
(* callback) (GIMP_TYPE_POSTERIZE_TOOL,
75
GIMP_TYPE_IMAGE_MAP_OPTIONS, NULL,
77
"gimp-posterize-tool",
79
_("Reduce image to a fixed number of colors"),
80
N_("_Posterize..."), NULL,
81
NULL, GIMP_HELP_TOOL_POSTERIZE,
82
GIMP_STOCK_TOOL_POSTERIZE,
87
gimp_posterize_tool_get_type (void)
89
static GType tool_type = 0;
93
static const GTypeInfo tool_info =
95
sizeof (GimpPosterizeToolClass),
97
(GBaseFinalizeFunc) NULL,
98
(GClassInitFunc) gimp_posterize_tool_class_init,
99
NULL, /* class_finalize */
100
NULL, /* class_data */
101
sizeof (GimpPosterizeTool),
103
(GInstanceInitFunc) gimp_posterize_tool_init,
106
tool_type = g_type_register_static (GIMP_TYPE_IMAGE_MAP_TOOL,
115
gimp_posterize_tool_class_init (GimpPosterizeToolClass *klass)
117
GObjectClass *object_class;
118
GimpToolClass *tool_class;
119
GimpImageMapToolClass *image_map_tool_class;
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);
125
parent_class = g_type_class_peek_parent (klass);
127
object_class->finalize = gimp_posterize_tool_finalize;
129
tool_class->initialize = gimp_posterize_tool_initialize;
131
image_map_tool_class->shell_desc = _("Posterize (Reduce Number of Colors)");
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;
139
gimp_posterize_tool_init (GimpPosterizeTool *posterize_tool)
141
posterize_tool->levels = POSTERIZE_DEFAULT_LEVELS;
142
posterize_tool->lut = gimp_lut_new ();
146
gimp_posterize_tool_finalize (GObject *object)
148
GimpPosterizeTool *posterize_tool = GIMP_POSTERIZE_TOOL (object);
150
if (posterize_tool->lut)
152
gimp_lut_free (posterize_tool->lut);
153
posterize_tool->lut = NULL;
156
G_OBJECT_CLASS (parent_class)->finalize (object);
160
gimp_posterize_tool_initialize (GimpTool *tool,
163
GimpPosterizeTool *posterize_tool = GIMP_POSTERIZE_TOOL (tool);
164
GimpDrawable *drawable;
166
drawable = gimp_image_active_drawable (gdisp->gimage);
171
if (gimp_drawable_is_indexed (drawable))
173
g_message (_("Posterize does not operate on indexed layers."));
177
posterize_tool->levels = POSTERIZE_DEFAULT_LEVELS;
179
GIMP_TOOL_CLASS (parent_class)->initialize (tool, gdisp);
181
gtk_adjustment_set_value (GTK_ADJUSTMENT (posterize_tool->levels_data),
182
posterize_tool->levels);
184
gimp_image_map_tool_preview (GIMP_IMAGE_MAP_TOOL (posterize_tool));
190
gimp_posterize_tool_map (GimpImageMapTool *image_map_tool)
192
GimpPosterizeTool *posterize_tool = GIMP_POSTERIZE_TOOL (image_map_tool);
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);
203
/**********************/
204
/* Posterize dialog */
205
/**********************/
208
gimp_posterize_tool_dialog (GimpImageMapTool *image_map_tool)
210
GimpPosterizeTool *posterize_tool = GIMP_POSTERIZE_TOOL (image_map_tool);
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,
220
gtk_widget_show (table);
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,
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);
232
g_signal_connect (posterize_tool->levels_data, "value_changed",
233
G_CALLBACK (posterize_levels_adjustment_update),
238
gimp_posterize_tool_reset (GimpImageMapTool *image_map_tool)
240
GimpPosterizeTool *posterize_tool = GIMP_POSTERIZE_TOOL (image_map_tool);
242
posterize_tool->levels = POSTERIZE_DEFAULT_LEVELS;
244
gtk_adjustment_set_value (GTK_ADJUSTMENT (posterize_tool->levels_data),
245
posterize_tool->levels);
249
posterize_levels_adjustment_update (GtkAdjustment *adjustment,
250
GimpPosterizeTool *posterize_tool)
252
if (posterize_tool->levels != adjustment->value)
254
posterize_tool->levels = adjustment->value;
256
gimp_image_map_tool_preview (GIMP_IMAGE_MAP_TOOL (posterize_tool));