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

« back to all changes in this revision

Viewing changes to app/tools/gimpcolorbalancetool.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 "libgimpcolor/gimpcolor.h"
 
24
#include "libgimpmath/gimpmath.h"
 
25
#include "libgimpwidgets/gimpwidgets.h"
 
26
 
 
27
#include "tools-types.h"
 
28
 
 
29
#include "base/color-balance.h"
 
30
 
 
31
#include "core/gimpdrawable.h"
 
32
#include "core/gimpimage.h"
 
33
#include "core/gimpimagemap.h"
 
34
 
 
35
#include "widgets/gimphelp-ids.h"
 
36
#include "widgets/gimpenumwidgets.h"
 
37
 
 
38
#include "display/gimpdisplay.h"
 
39
 
 
40
#include "gimpcolorbalancetool.h"
 
41
#include "gimpimagemapoptions.h"
 
42
 
 
43
#include "gimp-intl.h"
 
44
 
 
45
 
 
46
#define CYAN_RED       (1 << 0)
 
47
#define MAGENTA_GREEN  (1 << 1)
 
48
#define YELLOW_BLUE    (1 << 2)
 
49
#define PRESERVE       (1 << 3)
 
50
#define ALL           (CYAN_RED | MAGENTA_GREEN | YELLOW_BLUE | PRESERVE)
 
51
 
 
52
 
 
53
/*  local function prototypes  */
 
54
 
 
55
static void   gimp_color_balance_tool_class_init (GimpColorBalanceToolClass *klass);
 
56
static void   gimp_color_balance_tool_init       (GimpColorBalanceTool      *cb_tool);
 
57
 
 
58
static void     gimp_color_balance_tool_finalize   (GObject          *object);
 
59
 
 
60
static gboolean gimp_color_balance_tool_initialize (GimpTool         *tool,
 
61
                                                    GimpDisplay      *gdisp);
 
62
 
 
63
static void     gimp_color_balance_tool_map        (GimpImageMapTool *image_map_tool);
 
64
static void     gimp_color_balance_tool_dialog     (GimpImageMapTool *image_map_tool);
 
65
static void     gimp_color_balance_tool_reset      (GimpImageMapTool *image_map_tool);
 
66
 
 
67
static void     color_balance_update               (GimpColorBalanceTool *cb_tool,
 
68
                                                    gint                  update);
 
69
static void     color_balance_range_callback       (GtkWidget            *widget,
 
70
                                                    GimpColorBalanceTool *cb_tool);
 
71
static void     color_balance_range_reset_callback (GtkWidget            *widget,
 
72
                                                    GimpColorBalanceTool *cb_tool);
 
73
static void     color_balance_preserve_update      (GtkWidget            *widget,
 
74
                                                    GimpColorBalanceTool *cb_tool);
 
75
static void     color_balance_cr_adjustment_update (GtkAdjustment        *adj,
 
76
                                                    GimpColorBalanceTool *cb_tool);
 
77
static void     color_balance_mg_adjustment_update (GtkAdjustment        *adj,
 
78
                                                    GimpColorBalanceTool *cb_tool);
 
79
static void     color_balance_yb_adjustment_update (GtkAdjustment        *adj,
 
80
                                                    GimpColorBalanceTool *cb_tool);
 
81
 
 
82
 
 
83
static GimpImageMapToolClass *parent_class = NULL;
 
84
 
 
85
 
 
86
/*  functions  */
 
87
 
 
88
void
 
89
gimp_color_balance_tool_register (GimpToolRegisterCallback  callback,
 
90
                                  gpointer                  data)
 
91
{
 
92
  (* callback) (GIMP_TYPE_COLOR_BALANCE_TOOL,
 
93
                GIMP_TYPE_IMAGE_MAP_OPTIONS, NULL,
 
94
                0,
 
95
                "gimp-color-balance-tool",
 
96
                _("Color Balance"),
 
97
                _("Adjust color balance"),
 
98
                N_("Color _Balance..."), NULL,
 
99
                NULL, GIMP_HELP_TOOL_COLOR_BALANCE,
 
100
                GIMP_STOCK_TOOL_COLOR_BALANCE,
 
101
                data);
 
102
}
 
103
 
 
104
GType
 
105
gimp_color_balance_tool_get_type (void)
 
106
{
 
107
  static GType tool_type = 0;
 
108
 
 
109
  if (! tool_type)
 
110
    {
 
111
      static const GTypeInfo tool_info =
 
112
      {
 
113
        sizeof (GimpColorBalanceToolClass),
 
114
        (GBaseInitFunc) NULL,
 
115
        (GBaseFinalizeFunc) NULL,
 
116
        (GClassInitFunc) gimp_color_balance_tool_class_init,
 
117
        NULL,           /* class_finalize */
 
118
        NULL,           /* class_data     */
 
119
        sizeof (GimpColorBalanceTool),
 
120
        0,              /* n_preallocs    */
 
121
        (GInstanceInitFunc) gimp_color_balance_tool_init,
 
122
      };
 
123
 
 
124
      tool_type = g_type_register_static (GIMP_TYPE_IMAGE_MAP_TOOL,
 
125
                                          "GimpColorBalanceTool",
 
126
                                          &tool_info, 0);
 
127
    }
 
128
 
 
129
  return tool_type;
 
130
}
 
131
 
 
132
static void
 
133
gimp_color_balance_tool_class_init (GimpColorBalanceToolClass *klass)
 
134
{
 
135
  GObjectClass          *object_class;
 
136
  GimpToolClass         *tool_class;
 
137
  GimpImageMapToolClass *image_map_tool_class;
 
138
 
 
139
  object_class         = G_OBJECT_CLASS (klass);
 
140
  tool_class           = GIMP_TOOL_CLASS (klass);
 
141
  image_map_tool_class = GIMP_IMAGE_MAP_TOOL_CLASS (klass);
 
142
 
 
143
  parent_class = g_type_class_peek_parent (klass);
 
144
 
 
145
  object_class->finalize       = gimp_color_balance_tool_finalize;
 
146
 
 
147
  tool_class->initialize       = gimp_color_balance_tool_initialize;
 
148
 
 
149
  image_map_tool_class->shell_desc = _("Adjust Color Balance");
 
150
 
 
151
  image_map_tool_class->map    = gimp_color_balance_tool_map;
 
152
  image_map_tool_class->dialog = gimp_color_balance_tool_dialog;
 
153
  image_map_tool_class->reset  = gimp_color_balance_tool_reset;
 
154
}
 
155
 
 
156
static void
 
157
gimp_color_balance_tool_init (GimpColorBalanceTool *cb_tool)
 
158
{
 
159
  cb_tool->color_balance = g_new0 (ColorBalance, 1);
 
160
  cb_tool->transfer_mode = GIMP_MIDTONES;
 
161
 
 
162
  color_balance_init (cb_tool->color_balance);
 
163
}
 
164
 
 
165
static void
 
166
gimp_color_balance_tool_finalize (GObject *object)
 
167
{
 
168
  GimpColorBalanceTool *cb_tool = GIMP_COLOR_BALANCE_TOOL (object);
 
169
 
 
170
  if (cb_tool->color_balance)
 
171
    {
 
172
      g_free (cb_tool->color_balance);
 
173
      cb_tool->color_balance = NULL;
 
174
    }
 
175
 
 
176
  G_OBJECT_CLASS (parent_class)->finalize (object);
 
177
}
 
178
 
 
179
static gboolean
 
180
gimp_color_balance_tool_initialize (GimpTool    *tool,
 
181
                                    GimpDisplay *gdisp)
 
182
{
 
183
  GimpColorBalanceTool *cb_tool = GIMP_COLOR_BALANCE_TOOL (tool);
 
184
  GimpDrawable         *drawable;
 
185
 
 
186
  drawable = gimp_image_active_drawable (gdisp->gimage);
 
187
 
 
188
  if (! drawable)
 
189
    return FALSE;
 
190
 
 
191
  if (! gimp_drawable_is_rgb (drawable))
 
192
    {
 
193
      g_message (_("Color balance operates only on RGB color layers."));
 
194
      return FALSE;
 
195
    }
 
196
 
 
197
  color_balance_init (cb_tool->color_balance);
 
198
 
 
199
  cb_tool->transfer_mode = GIMP_MIDTONES;
 
200
 
 
201
  GIMP_TOOL_CLASS (parent_class)->initialize (tool, gdisp);
 
202
 
 
203
  color_balance_update (cb_tool, ALL);
 
204
 
 
205
  return TRUE;
 
206
}
 
207
 
 
208
static void
 
209
gimp_color_balance_tool_map (GimpImageMapTool *image_map_tool)
 
210
{
 
211
  GimpColorBalanceTool *cb_tool = GIMP_COLOR_BALANCE_TOOL (image_map_tool);
 
212
 
 
213
  color_balance_create_lookup_tables (cb_tool->color_balance);
 
214
  gimp_image_map_apply (image_map_tool->image_map,
 
215
                        color_balance,
 
216
                        cb_tool->color_balance);
 
217
}
 
218
 
 
219
 
 
220
/**************************/
 
221
/*  Color Balance dialog  */
 
222
/**************************/
 
223
 
 
224
static GtkAdjustment *
 
225
create_levels_scale (const gchar   *left,
 
226
                     const gchar   *right,
 
227
                     GtkWidget     *table,
 
228
                     gint           col)
 
229
{
 
230
  GtkWidget     *label;
 
231
  GtkWidget     *slider;
 
232
  GtkWidget     *spinbutton;
 
233
  GtkAdjustment *adj;
 
234
 
 
235
  adj = GTK_ADJUSTMENT (gtk_adjustment_new (0, -100.0, 100.0, 1.0, 10.0, 0.0));
 
236
 
 
237
  label = gtk_label_new (left);
 
238
  gtk_misc_set_alignment (GTK_MISC (label), 1.0, 0.5);
 
239
  gtk_table_attach (GTK_TABLE (table), label, 0, 1, col, col + 1,
 
240
                    GTK_SHRINK | GTK_FILL, GTK_SHRINK | GTK_FILL, 0, 0);
 
241
  gtk_widget_show (label);
 
242
 
 
243
  slider = gtk_hscale_new (adj);
 
244
  gtk_scale_set_draw_value (GTK_SCALE (slider), FALSE);
 
245
  gtk_range_set_update_policy (GTK_RANGE (slider), GTK_UPDATE_DELAYED);
 
246
  gtk_widget_set_size_request (slider, 100, -1);
 
247
  gtk_table_attach_defaults (GTK_TABLE (table), slider, 1, 2, col, col + 1);
 
248
  gtk_widget_show (slider);
 
249
 
 
250
  label = gtk_label_new (right);
 
251
  gtk_misc_set_alignment (GTK_MISC (label), 0.0, 0.5);
 
252
  gtk_table_attach (GTK_TABLE (table), label, 2, 3, col, col + 1,
 
253
                    GTK_SHRINK | GTK_FILL, GTK_SHRINK | GTK_FILL, 0, 0);
 
254
  gtk_widget_show (label);
 
255
 
 
256
  spinbutton = gtk_spin_button_new (adj, 1.0, 0);
 
257
  gtk_table_attach (GTK_TABLE (table), spinbutton, 3, 4, col, col + 1,
 
258
                    GTK_SHRINK | GTK_FILL, GTK_SHRINK | GTK_FILL, 0, 0);
 
259
  gtk_widget_show (spinbutton);
 
260
 
 
261
  return adj;
 
262
}
 
263
 
 
264
static void
 
265
gimp_color_balance_tool_dialog (GimpImageMapTool *image_map_tool)
 
266
{
 
267
  GimpColorBalanceTool *cb_tool = GIMP_COLOR_BALANCE_TOOL (image_map_tool);
 
268
  GtkWidget            *vbox;
 
269
  GtkWidget            *hbox;
 
270
  GtkWidget            *table;
 
271
  GtkWidget            *toggle;
 
272
  GtkWidget            *button;
 
273
  GtkWidget            *frame;
 
274
 
 
275
  frame = gimp_enum_radio_frame_new (GIMP_TYPE_TRANSFER_MODE,
 
276
                                     gtk_label_new (_("Select Range to Modify")),
 
277
                                     G_CALLBACK (color_balance_range_callback),
 
278
                                     cb_tool,
 
279
                                     &toggle);
 
280
  gtk_box_pack_start (GTK_BOX (image_map_tool->main_vbox), frame,
 
281
                      FALSE, FALSE, 0);
 
282
  gtk_widget_show (frame);
 
283
 
 
284
  frame = gimp_frame_new (_("Modify Selected Range's Color Levels"));
 
285
  gtk_box_pack_start (GTK_BOX (image_map_tool->main_vbox), frame,
 
286
                      FALSE, FALSE, 0);
 
287
  gtk_widget_show (frame);
 
288
 
 
289
  vbox = gtk_vbox_new (FALSE, 4);
 
290
  gtk_container_add (GTK_CONTAINER (frame), vbox);
 
291
  gtk_widget_show (vbox);
 
292
 
 
293
  /*  The table containing sliders  */
 
294
  table = gtk_table_new (3, 4, FALSE);
 
295
  gtk_table_set_col_spacings (GTK_TABLE (table), 4);
 
296
  gtk_table_set_row_spacings (GTK_TABLE (table), 2);
 
297
  gtk_box_pack_start (GTK_BOX (vbox), table, FALSE, FALSE, 0);
 
298
  gtk_widget_show (table);
 
299
 
 
300
  cb_tool->cyan_red_adj =
 
301
    create_levels_scale (_("Cyan"), _("Red"), table, 0);
 
302
 
 
303
  g_signal_connect (cb_tool->cyan_red_adj, "value_changed",
 
304
                    G_CALLBACK (color_balance_cr_adjustment_update),
 
305
                    cb_tool);
 
306
 
 
307
  cb_tool->magenta_green_adj =
 
308
    create_levels_scale (_("Magenta"), _("Green"), table, 1);
 
309
 
 
310
  g_signal_connect (cb_tool->magenta_green_adj, "value_changed",
 
311
                    G_CALLBACK (color_balance_mg_adjustment_update),
 
312
                    cb_tool);
 
313
 
 
314
  cb_tool->yellow_blue_adj =
 
315
    create_levels_scale (_("Yellow"), _("Blue"), table, 2);
 
316
 
 
317
  g_signal_connect (cb_tool->yellow_blue_adj, "value_changed",
 
318
                    G_CALLBACK (color_balance_yb_adjustment_update),
 
319
                    cb_tool);
 
320
 
 
321
  hbox = gtk_hbox_new (FALSE, 6);
 
322
  gtk_box_pack_start (GTK_BOX (vbox), hbox, FALSE, FALSE, 0);
 
323
  gtk_widget_show (hbox);
 
324
 
 
325
  button = gtk_button_new_with_mnemonic (_("R_eset range"));
 
326
  gtk_box_pack_end (GTK_BOX (hbox), button, FALSE, FALSE, 0);
 
327
  gtk_widget_show (button);
 
328
 
 
329
  g_signal_connect (button, "clicked",
 
330
                    G_CALLBACK (color_balance_range_reset_callback),
 
331
                    cb_tool);
 
332
 
 
333
  cb_tool->preserve_toggle =
 
334
    gtk_check_button_new_with_mnemonic (_("Preserve _luminosity"));
 
335
  gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (cb_tool->preserve_toggle),
 
336
                                cb_tool->color_balance->preserve_luminosity);
 
337
  gtk_box_pack_end (GTK_BOX (image_map_tool->main_vbox),
 
338
                    cb_tool->preserve_toggle, FALSE, FALSE, 0);
 
339
  gtk_widget_show (cb_tool->preserve_toggle);
 
340
 
 
341
  g_signal_connect (cb_tool->preserve_toggle, "toggled",
 
342
                    G_CALLBACK (color_balance_preserve_update),
 
343
                    cb_tool);
 
344
 
 
345
  /*  set range after everything is in place  */
 
346
  gimp_int_radio_group_set_active (GTK_RADIO_BUTTON (toggle),
 
347
                                   cb_tool->transfer_mode);
 
348
}
 
349
 
 
350
static void
 
351
gimp_color_balance_tool_reset (GimpImageMapTool *image_map_tool)
 
352
{
 
353
  GimpColorBalanceTool *cb_tool = GIMP_COLOR_BALANCE_TOOL (image_map_tool);
 
354
 
 
355
  color_balance_init (cb_tool->color_balance);
 
356
  color_balance_update (cb_tool, ALL);
 
357
}
 
358
 
 
359
static void
 
360
color_balance_update (GimpColorBalanceTool *cb_tool,
 
361
                      gint                  update)
 
362
{
 
363
  GimpTransferMode tm;
 
364
 
 
365
  tm = cb_tool->transfer_mode;
 
366
 
 
367
  if (update & CYAN_RED)
 
368
    gtk_adjustment_set_value (cb_tool->cyan_red_adj,
 
369
                              cb_tool->color_balance->cyan_red[tm]);
 
370
 
 
371
  if (update & MAGENTA_GREEN)
 
372
    gtk_adjustment_set_value (cb_tool->magenta_green_adj,
 
373
                              cb_tool->color_balance->magenta_green[tm]);
 
374
 
 
375
  if (update & YELLOW_BLUE)
 
376
    gtk_adjustment_set_value (cb_tool->yellow_blue_adj,
 
377
                              cb_tool->color_balance->yellow_blue[tm]);
 
378
 
 
379
  if (update & PRESERVE)
 
380
    gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (cb_tool->preserve_toggle),
 
381
                                  cb_tool->color_balance->preserve_luminosity);
 
382
}
 
383
 
 
384
static void
 
385
color_balance_range_callback (GtkWidget            *widget,
 
386
                              GimpColorBalanceTool *cb_tool)
 
387
{
 
388
  gimp_radio_button_update (widget, &cb_tool->transfer_mode);
 
389
  color_balance_update (cb_tool, ALL);
 
390
}
 
391
 
 
392
static void
 
393
color_balance_range_reset_callback (GtkWidget            *widget,
 
394
                                    GimpColorBalanceTool *cb_tool)
 
395
{
 
396
  color_balance_range_reset (cb_tool->color_balance,
 
397
                             cb_tool->transfer_mode);
 
398
  color_balance_update (cb_tool, ALL);
 
399
 
 
400
  gimp_image_map_tool_preview (GIMP_IMAGE_MAP_TOOL (cb_tool));
 
401
}
 
402
 
 
403
static void
 
404
color_balance_preserve_update (GtkWidget            *widget,
 
405
                               GimpColorBalanceTool *cb_tool)
 
406
{
 
407
  gboolean active;
 
408
 
 
409
  active = GTK_TOGGLE_BUTTON (widget)->active;
 
410
 
 
411
  if (cb_tool->color_balance->preserve_luminosity != active)
 
412
    {
 
413
      cb_tool->color_balance->preserve_luminosity = active;
 
414
 
 
415
      gimp_image_map_tool_preview (GIMP_IMAGE_MAP_TOOL (cb_tool));
 
416
    }
 
417
}
 
418
 
 
419
static void
 
420
color_balance_cr_adjustment_update (GtkAdjustment        *adjustment,
 
421
                                    GimpColorBalanceTool *cb_tool)
 
422
{
 
423
  GimpTransferMode tm;
 
424
 
 
425
  tm = cb_tool->transfer_mode;
 
426
 
 
427
  if (cb_tool->color_balance->cyan_red[tm] != adjustment->value)
 
428
    {
 
429
      cb_tool->color_balance->cyan_red[tm] = adjustment->value;
 
430
 
 
431
      gimp_image_map_tool_preview (GIMP_IMAGE_MAP_TOOL (cb_tool));
 
432
    }
 
433
}
 
434
 
 
435
static void
 
436
color_balance_mg_adjustment_update (GtkAdjustment        *adjustment,
 
437
                                    GimpColorBalanceTool *cb_tool)
 
438
{
 
439
  GimpTransferMode tm;
 
440
 
 
441
  tm = cb_tool->transfer_mode;
 
442
 
 
443
  if (cb_tool->color_balance->magenta_green[tm] != adjustment->value)
 
444
    {
 
445
      cb_tool->color_balance->magenta_green[tm] = adjustment->value;
 
446
 
 
447
      gimp_image_map_tool_preview (GIMP_IMAGE_MAP_TOOL (cb_tool));
 
448
    }
 
449
}
 
450
 
 
451
static void
 
452
color_balance_yb_adjustment_update (GtkAdjustment        *adjustment,
 
453
                                    GimpColorBalanceTool *cb_tool)
 
454
{
 
455
  GimpTransferMode tm;
 
456
 
 
457
  tm = cb_tool->transfer_mode;
 
458
 
 
459
  if (cb_tool->color_balance->yellow_blue[tm] != adjustment->value)
 
460
    {
 
461
      cb_tool->color_balance->yellow_blue[tm] = adjustment->value;
 
462
 
 
463
      gimp_image_map_tool_preview (GIMP_IMAGE_MAP_TOOL (cb_tool));
 
464
    }
 
465
}