~ubuntu-branches/ubuntu/gutsy/gimp/gutsy-backports

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
/* GIMP - The GNU Image Manipulation Program
 * Copyright (C) 1995 Spencer Kimball and Peter Mattis
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 */

#include "config.h"

#include <gtk/gtk.h>

#include "libgimpwidgets/gimpwidgets.h"

#include "tools-types.h"

#include "base/color-balance.h"

#include "core/gimpdrawable.h"
#include "core/gimpimage.h"
#include "core/gimpimagemap.h"

#include "widgets/gimphelp-ids.h"

#include "display/gimpdisplay.h"

#include "gimpcolorbalancetool.h"
#include "gimpimagemapoptions.h"

#include "gimp-intl.h"


#define CYAN_RED       (1 << 0)
#define MAGENTA_GREEN  (1 << 1)
#define YELLOW_BLUE    (1 << 2)
#define PRESERVE       (1 << 3)
#define ALL           (CYAN_RED | MAGENTA_GREEN | YELLOW_BLUE | PRESERVE)


/*  local function prototypes  */

static void     gimp_color_balance_tool_finalize   (GObject          *object);

static gboolean gimp_color_balance_tool_initialize (GimpTool         *tool,
                                                    GimpDisplay      *display,
                                                    GError          **error);

static void     gimp_color_balance_tool_map        (GimpImageMapTool *im_tool);
static void     gimp_color_balance_tool_dialog     (GimpImageMapTool *im_tool);
static void     gimp_color_balance_tool_reset      (GimpImageMapTool *im_tool);

static void     color_balance_update               (GimpColorBalanceTool *cb_tool,
                                                    gint                  update);
static void     color_balance_range_callback       (GtkWidget            *widget,
                                                    GimpColorBalanceTool *cb_tool);
static void     color_balance_range_reset_callback (GtkWidget            *widget,
                                                    GimpColorBalanceTool *cb_tool);
static void     color_balance_preserve_update      (GtkWidget            *widget,
                                                    GimpColorBalanceTool *cb_tool);
static void     color_balance_cr_adjustment_update (GtkAdjustment        *adj,
                                                    GimpColorBalanceTool *cb_tool);
static void     color_balance_mg_adjustment_update (GtkAdjustment        *adj,
                                                    GimpColorBalanceTool *cb_tool);
static void     color_balance_yb_adjustment_update (GtkAdjustment        *adj,
                                                    GimpColorBalanceTool *cb_tool);


G_DEFINE_TYPE (GimpColorBalanceTool, gimp_color_balance_tool,
               GIMP_TYPE_IMAGE_MAP_TOOL)

#define parent_class gimp_color_balance_tool_parent_class


void
gimp_color_balance_tool_register (GimpToolRegisterCallback  callback,
                                  gpointer                  data)
{
  (* callback) (GIMP_TYPE_COLOR_BALANCE_TOOL,
                GIMP_TYPE_IMAGE_MAP_OPTIONS, NULL,
                0,
                "gimp-color-balance-tool",
                _("Color Balance"),
                _("Color Balance Tool: Adjust color distribution"),
                N_("Color _Balance..."), NULL,
                NULL, GIMP_HELP_TOOL_COLOR_BALANCE,
                GIMP_STOCK_TOOL_COLOR_BALANCE,
                data);
}

static void
gimp_color_balance_tool_class_init (GimpColorBalanceToolClass *klass)
{
  GObjectClass          *object_class  = G_OBJECT_CLASS (klass);
  GimpToolClass         *tool_class    = GIMP_TOOL_CLASS (klass);
  GimpImageMapToolClass *im_tool_class = GIMP_IMAGE_MAP_TOOL_CLASS (klass);

  object_class->finalize    = gimp_color_balance_tool_finalize;

  tool_class->initialize    = gimp_color_balance_tool_initialize;

  im_tool_class->shell_desc = _("Adjust Color Balance");

  im_tool_class->map        = gimp_color_balance_tool_map;
  im_tool_class->dialog     = gimp_color_balance_tool_dialog;
  im_tool_class->reset      = gimp_color_balance_tool_reset;
}

static void
gimp_color_balance_tool_init (GimpColorBalanceTool *cb_tool)
{
  cb_tool->color_balance = g_slice_new0 (ColorBalance);
  cb_tool->transfer_mode = GIMP_MIDTONES;

  color_balance_init (cb_tool->color_balance);
}

static void
gimp_color_balance_tool_finalize (GObject *object)
{
  GimpColorBalanceTool *cb_tool = GIMP_COLOR_BALANCE_TOOL (object);

  g_slice_free (ColorBalance, cb_tool->color_balance);

  G_OBJECT_CLASS (parent_class)->finalize (object);
}

static gboolean
gimp_color_balance_tool_initialize (GimpTool     *tool,
                                    GimpDisplay  *display,
                                    GError      **error)
{
  GimpColorBalanceTool *cb_tool = GIMP_COLOR_BALANCE_TOOL (tool);
  GimpDrawable         *drawable;

  drawable = gimp_image_get_active_drawable (display->image);

  if (! drawable)
    return FALSE;

  if (! gimp_drawable_is_rgb (drawable))
    {
      g_set_error (error, 0, 0,
                   _("Color Balance operates only on RGB color layers."));
      return FALSE;
    }

  color_balance_init (cb_tool->color_balance);

  cb_tool->transfer_mode = GIMP_MIDTONES;

  GIMP_TOOL_CLASS (parent_class)->initialize (tool, display, error);

  color_balance_update (cb_tool, ALL);

  return TRUE;
}

static void
gimp_color_balance_tool_map (GimpImageMapTool *im_tool)
{
  GimpColorBalanceTool *cb_tool = GIMP_COLOR_BALANCE_TOOL (im_tool);

  color_balance_create_lookup_tables (cb_tool->color_balance);
  gimp_image_map_apply (im_tool->image_map,
                        (GimpImageMapApplyFunc) color_balance,
                        cb_tool->color_balance);
}


/**************************/
/*  Color Balance dialog  */
/**************************/

static GtkAdjustment *
create_levels_scale (const gchar   *left,
                     const gchar   *right,
                     GtkWidget     *table,
                     gint           col)
{
  GtkWidget *label;
  GtkWidget *slider;
  GtkWidget *spinbutton;
  GtkObject *adj;

  label = gtk_label_new (left);
  gtk_misc_set_alignment (GTK_MISC (label), 1.0, 0.5);
  gtk_table_attach (GTK_TABLE (table), label, 0, 1, col, col + 1,
                    GTK_SHRINK | GTK_FILL, GTK_SHRINK | GTK_FILL, 0, 0);
  gtk_widget_show (label);

  spinbutton = gimp_spin_button_new (&adj,
                                     0, -100.0, 100.0, 1.0, 10.0, 0.0, 1.0, 0);

  slider = gtk_hscale_new (GTK_ADJUSTMENT (adj));
  gtk_scale_set_draw_value (GTK_SCALE (slider), FALSE);
  gtk_range_set_update_policy (GTK_RANGE (slider), GTK_UPDATE_CONTINUOUS);
  gtk_widget_set_size_request (slider, 100, -1);
  gtk_table_attach_defaults (GTK_TABLE (table), slider, 1, 2, col, col + 1);
  gtk_widget_show (slider);

  label = gtk_label_new (right);
  gtk_misc_set_alignment (GTK_MISC (label), 0.0, 0.5);
  gtk_table_attach (GTK_TABLE (table), label, 2, 3, col, col + 1,
                    GTK_SHRINK | GTK_FILL, GTK_SHRINK | GTK_FILL, 0, 0);
  gtk_widget_show (label);

  gtk_table_attach (GTK_TABLE (table), spinbutton, 3, 4, col, col + 1,
                    GTK_SHRINK | GTK_FILL, GTK_SHRINK | GTK_FILL, 0, 0);
  gtk_widget_show (spinbutton);

  return GTK_ADJUSTMENT (adj);
}

static void
gimp_color_balance_tool_dialog (GimpImageMapTool *im_tool)
{
  GimpColorBalanceTool *cb_tool = GIMP_COLOR_BALANCE_TOOL (im_tool);
  GtkWidget            *vbox;
  GtkWidget            *hbox;
  GtkWidget            *table;
  GtkWidget            *toggle;
  GtkWidget            *button;
  GtkWidget            *frame;

  frame = gimp_enum_radio_frame_new (GIMP_TYPE_TRANSFER_MODE,
                                     gtk_label_new (_("Select Range to Adjust")),
                                     G_CALLBACK (color_balance_range_callback),
                                     cb_tool,
                                     &toggle);
  gtk_box_pack_start (GTK_BOX (im_tool->main_vbox), frame, FALSE, FALSE, 0);
  gtk_widget_show (frame);

  frame = gimp_frame_new (_("Adjust Color Levels"));
  gtk_box_pack_start (GTK_BOX (im_tool->main_vbox), frame, FALSE, FALSE, 0);
  gtk_widget_show (frame);

  vbox = gtk_vbox_new (FALSE, 4);
  gtk_container_add (GTK_CONTAINER (frame), vbox);
  gtk_widget_show (vbox);

  /*  The table containing sliders  */
  table = gtk_table_new (3, 4, FALSE);
  gtk_table_set_col_spacings (GTK_TABLE (table), 4);
  gtk_table_set_row_spacings (GTK_TABLE (table), 2);
  gtk_box_pack_start (GTK_BOX (vbox), table, FALSE, FALSE, 0);
  gtk_widget_show (table);

  cb_tool->cyan_red_adj =
    create_levels_scale (_("Cyan"), _("Red"), table, 0);

  g_signal_connect (cb_tool->cyan_red_adj, "value-changed",
                    G_CALLBACK (color_balance_cr_adjustment_update),
                    cb_tool);

  cb_tool->magenta_green_adj =
    create_levels_scale (_("Magenta"), _("Green"), table, 1);

  g_signal_connect (cb_tool->magenta_green_adj, "value-changed",
                    G_CALLBACK (color_balance_mg_adjustment_update),
                    cb_tool);

  cb_tool->yellow_blue_adj =
    create_levels_scale (_("Yellow"), _("Blue"), table, 2);

  g_signal_connect (cb_tool->yellow_blue_adj, "value-changed",
                    G_CALLBACK (color_balance_yb_adjustment_update),
                    cb_tool);

  hbox = gtk_hbox_new (FALSE, 6);
  gtk_box_pack_start (GTK_BOX (vbox), hbox, FALSE, FALSE, 0);
  gtk_widget_show (hbox);

  button = gtk_button_new_with_mnemonic (_("R_eset Range"));
  gtk_box_pack_end (GTK_BOX (hbox), button, FALSE, FALSE, 0);
  gtk_widget_show (button);

  g_signal_connect (button, "clicked",
                    G_CALLBACK (color_balance_range_reset_callback),
                    cb_tool);

  cb_tool->preserve_toggle =
    gtk_check_button_new_with_mnemonic (_("Preserve _luminosity"));
  gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (cb_tool->preserve_toggle),
                                cb_tool->color_balance->preserve_luminosity);
  gtk_box_pack_end (GTK_BOX (im_tool->main_vbox), cb_tool->preserve_toggle,
                    FALSE, FALSE, 0);
  gtk_widget_show (cb_tool->preserve_toggle);

  g_signal_connect (cb_tool->preserve_toggle, "toggled",
                    G_CALLBACK (color_balance_preserve_update),
                    cb_tool);

  /*  set range after everything is in place  */
  gimp_int_radio_group_set_active (GTK_RADIO_BUTTON (toggle),
                                   cb_tool->transfer_mode);
}

static void
gimp_color_balance_tool_reset (GimpImageMapTool *im_tool)
{
  GimpColorBalanceTool *cb_tool = GIMP_COLOR_BALANCE_TOOL (im_tool);

  color_balance_init (cb_tool->color_balance);
  color_balance_update (cb_tool, ALL);
}

static void
color_balance_update (GimpColorBalanceTool *cb_tool,
                      gint                  update)
{
  GimpTransferMode tm;

  tm = cb_tool->transfer_mode;

  if (update & CYAN_RED)
    gtk_adjustment_set_value (cb_tool->cyan_red_adj,
                              cb_tool->color_balance->cyan_red[tm]);

  if (update & MAGENTA_GREEN)
    gtk_adjustment_set_value (cb_tool->magenta_green_adj,
                              cb_tool->color_balance->magenta_green[tm]);

  if (update & YELLOW_BLUE)
    gtk_adjustment_set_value (cb_tool->yellow_blue_adj,
                              cb_tool->color_balance->yellow_blue[tm]);

  if (update & PRESERVE)
    gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (cb_tool->preserve_toggle),
                                  cb_tool->color_balance->preserve_luminosity);
}

static void
color_balance_range_callback (GtkWidget            *widget,
                              GimpColorBalanceTool *cb_tool)
{
  gimp_radio_button_update (widget, &cb_tool->transfer_mode);
  color_balance_update (cb_tool, ALL);
}

static void
color_balance_range_reset_callback (GtkWidget            *widget,
                                    GimpColorBalanceTool *cb_tool)
{
  color_balance_range_reset (cb_tool->color_balance,
                             cb_tool->transfer_mode);
  color_balance_update (cb_tool, ALL);

  gimp_image_map_tool_preview (GIMP_IMAGE_MAP_TOOL (cb_tool));
}

static void
color_balance_preserve_update (GtkWidget            *widget,
                               GimpColorBalanceTool *cb_tool)
{
  gboolean active;

  active = GTK_TOGGLE_BUTTON (widget)->active;

  if (cb_tool->color_balance->preserve_luminosity != active)
    {
      cb_tool->color_balance->preserve_luminosity = active;

      gimp_image_map_tool_preview (GIMP_IMAGE_MAP_TOOL (cb_tool));
    }
}

static void
color_balance_cr_adjustment_update (GtkAdjustment        *adjustment,
                                    GimpColorBalanceTool *cb_tool)
{
  GimpTransferMode tm;

  tm = cb_tool->transfer_mode;

  if (cb_tool->color_balance->cyan_red[tm] != adjustment->value)
    {
      cb_tool->color_balance->cyan_red[tm] = adjustment->value;

      gimp_image_map_tool_preview (GIMP_IMAGE_MAP_TOOL (cb_tool));
    }
}

static void
color_balance_mg_adjustment_update (GtkAdjustment        *adjustment,
                                    GimpColorBalanceTool *cb_tool)
{
  GimpTransferMode tm;

  tm = cb_tool->transfer_mode;

  if (cb_tool->color_balance->magenta_green[tm] != adjustment->value)
    {
      cb_tool->color_balance->magenta_green[tm] = adjustment->value;

      gimp_image_map_tool_preview (GIMP_IMAGE_MAP_TOOL (cb_tool));
    }
}

static void
color_balance_yb_adjustment_update (GtkAdjustment        *adjustment,
                                    GimpColorBalanceTool *cb_tool)
{
  GimpTransferMode tm;

  tm = cb_tool->transfer_mode;

  if (cb_tool->color_balance->yellow_blue[tm] != adjustment->value)
    {
      cb_tool->color_balance->yellow_blue[tm] = adjustment->value;

      gimp_image_map_tool_preview (GIMP_IMAGE_MAP_TOOL (cb_tool));
    }
}