gimpcurvestool.c 27.3 KB
Newer Older
1
/* GIMP - The GNU Image Manipulation Program
Elliot Lee's avatar
Elliot Lee committed
2 3 4 5 6 7 8 9 10 11 12 13 14 15
 * 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
16
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
Elliot Lee's avatar
Elliot Lee committed
17
 */
Sven Neumann's avatar
Sven Neumann committed
18

19 20 21
#include "config.h"

#include <stdio.h>
22 23
#include <stdlib.h>
#include <string.h>
24

Sven Neumann's avatar
Sven Neumann committed
25 26
#include <gtk/gtk.h>

27
#include "libgimpmath/gimpmath.h"
28
#include "libgimpcolor/gimpcolor.h"
29
#include "libgimpconfig/gimpconfig.h"
30
#include "libgimpwidgets/gimpwidgets.h"
31

32
#include "tools-types.h"
Michael Natterer's avatar
Michael Natterer committed
33

34
#include "base/curves.h"
Michael Natterer's avatar
Michael Natterer committed
35 36 37
#include "base/gimphistogram.h"
#include "base/gimplut.h"

38
#include "core/gimp.h"
39
#include "core/gimpcurve.h"
Michael Natterer's avatar
Michael Natterer committed
40
#include "core/gimpdrawable.h"
41
#include "core/gimpdrawable-histogram.h"
Michael Natterer's avatar
Michael Natterer committed
42
#include "core/gimpimage.h"
43
#include "core/gimpimagemap.h"
Michael Natterer's avatar
Michael Natterer committed
44

45
#include "widgets/gimpcolorbar.h"
46
#include "widgets/gimphelp-ids.h"
47
#include "widgets/gimpcurveview.h"
Michael Natterer's avatar
Michael Natterer committed
48

49 50
#include "display/gimpdisplay.h"

Michael Natterer's avatar
Michael Natterer committed
51
#include "gimpcurvestool.h"
52
#include "gimphistogramoptions.h"
Sven Neumann's avatar
Sven Neumann committed
53

54
#include "gimp-intl.h"
55

56

57 58 59
#define GRAPH_SIZE 256
#define BAR_SIZE    12
#define RADIUS       4
Elliot Lee's avatar
Elliot Lee committed
60 61


Michael Natterer's avatar
Michael Natterer committed
62 63
/*  local function prototypes  */

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
static void     gimp_curves_tool_finalize       (GObject              *object);

static gboolean gimp_curves_tool_initialize     (GimpTool             *tool,
                                                 GimpDisplay          *display,
                                                 GError              **error);
static void     gimp_curves_tool_button_release (GimpTool             *tool,
                                                 GimpCoords           *coords,
                                                 guint32               time,
                                                 GdkModifierType       state,
                                                 GimpButtonReleaseType release_type,
                                                 GimpDisplay          *display);
static gboolean gimp_curves_tool_key_press      (GimpTool             *tool,
                                                 GdkEventKey          *kevent,
                                                 GimpDisplay          *display);
static void     gimp_curves_tool_oper_update    (GimpTool             *tool,
                                                 GimpCoords           *coords,
                                                 GdkModifierType       state,
                                                 gboolean              proximity,
                                                 GimpDisplay          *display);

static void     gimp_curves_tool_color_picked   (GimpColorTool        *color_tool,
                                                 GimpColorPickState    pick_state,
                                                 GimpImageType         sample_type,
                                                 GimpRGB              *color,
                                                 gint                  color_index);
static void     gimp_curves_tool_map            (GimpImageMapTool     *image_map_tool);
static void     gimp_curves_tool_dialog         (GimpImageMapTool     *image_map_tool);
static void     gimp_curves_tool_reset          (GimpImageMapTool     *image_map_tool);
static gboolean gimp_curves_tool_settings_load  (GimpImageMapTool     *image_map_tool,
                                                 gpointer              fp,
                                                 GError              **error);
static gboolean gimp_curves_tool_settings_save  (GimpImageMapTool     *image_map_tool,
                                                 gpointer              fp);

98 99
static void     curves_curve_callback           (GimpCurve            *curve,
                                                 GimpCurvesTool       *tool);
100 101 102 103 104 105 106 107 108 109
static void     curves_channel_callback         (GtkWidget            *widget,
                                                 GimpCurvesTool       *tool);
static void     curves_channel_reset_callback   (GtkWidget            *widget,
                                                 GimpCurvesTool       *tool);

static gboolean curves_menu_sensitivity         (gint                  value,
                                                 gpointer              data);

static void     curves_curve_type_callback      (GtkWidget            *widget,
                                                 GimpCurvesTool       *tool);
110

Michael Natterer's avatar
Michael Natterer committed
111

112
G_DEFINE_TYPE (GimpCurvesTool, gimp_curves_tool, GIMP_TYPE_IMAGE_MAP_TOOL)
113 114

#define parent_class gimp_curves_tool_parent_class
115

Michael Natterer's avatar
Michael Natterer committed
116

117
/*  public functions  */
Michael Natterer's avatar
Michael Natterer committed
118 119

void
Nate Summers's avatar
Nate Summers committed
120
gimp_curves_tool_register (GimpToolRegisterCallback  callback,
121
                           gpointer                  data)
Michael Natterer's avatar
Michael Natterer committed
122
{
Nate Summers's avatar
Nate Summers committed
123
  (* callback) (GIMP_TYPE_CURVES_TOOL,
124
                GIMP_TYPE_HISTOGRAM_OPTIONS,
125
                gimp_color_options_gui,
126
                0,
127
                "gimp-curves-tool",
128
                _("Curves"),
129
                _("Curves Tool: Adjust color curves"),
130
                N_("_Curves..."), NULL,
131
                NULL, GIMP_HELP_TOOL_CURVES,
Nate Summers's avatar
Nate Summers committed
132
                GIMP_STOCK_TOOL_CURVES,
133
                data);
Michael Natterer's avatar
Michael Natterer committed
134 135
}

136 137 138

/*  private functions  */

Michael Natterer's avatar
Michael Natterer committed
139 140 141
static void
gimp_curves_tool_class_init (GimpCurvesToolClass *klass)
{
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
  GObjectClass          *object_class     = G_OBJECT_CLASS (klass);
  GimpToolClass         *tool_class       = GIMP_TOOL_CLASS (klass);
  GimpColorToolClass    *color_tool_class = GIMP_COLOR_TOOL_CLASS (klass);
  GimpImageMapToolClass *im_tool_class    = GIMP_IMAGE_MAP_TOOL_CLASS (klass);

  object_class->finalize           = gimp_curves_tool_finalize;

  tool_class->initialize           = gimp_curves_tool_initialize;
  tool_class->button_release       = gimp_curves_tool_button_release;
  tool_class->key_press            = gimp_curves_tool_key_press;
  tool_class->oper_update          = gimp_curves_tool_oper_update;

  color_tool_class->picked         = gimp_curves_tool_color_picked;

  im_tool_class->shell_desc        = _("Adjust Color Curves");
  im_tool_class->settings_name     = "curves";
  im_tool_class->load_dialog_title = _("Load Curves");
  im_tool_class->load_button_tip   = _("Load curves settings from file");
  im_tool_class->save_dialog_title = _("Save Curves");
  im_tool_class->save_button_tip   = _("Save curves settings to file");

  im_tool_class->map               = gimp_curves_tool_map;
  im_tool_class->dialog            = gimp_curves_tool_dialog;
  im_tool_class->reset             = gimp_curves_tool_reset;
  im_tool_class->settings_load     = gimp_curves_tool_settings_load;
  im_tool_class->settings_save     = gimp_curves_tool_settings_save;
Michael Natterer's avatar
Michael Natterer committed
168 169 170
}

static void
171
gimp_curves_tool_init (GimpCurvesTool *tool)
Michael Natterer's avatar
Michael Natterer committed
172
{
173
  gint i;
174

175
  for (i = 0; i < G_N_ELEMENTS (tool->curve); i++)
176 177 178
    {
      tool->curve[i] = GIMP_CURVE (gimp_curve_new ("curves tool"));

179
      g_signal_connect_object (tool->curve[i], "dirty",
180 181 182
                               G_CALLBACK (curves_curve_callback),
                               tool, 0);
    }
183

184 185
  tool->lut     = gimp_lut_new ();
  tool->channel = GIMP_HISTOGRAM_VALUE;
186

187 188
  for (i = 0; i < G_N_ELEMENTS (tool->col_value); i++)
    tool->col_value[i] = -1;
Michael Natterer's avatar
Michael Natterer committed
189 190 191
}

static void
192
gimp_curves_tool_finalize (GObject *object)
Michael Natterer's avatar
Michael Natterer committed
193
{
194
  GimpCurvesTool *tool = GIMP_CURVES_TOOL (object);
195 196 197 198
  gint            i;

  for (i = 0; i < G_N_ELEMENTS (tool->curve); i++)
    g_object_unref (tool->curve[i]);
199

200 201
  gimp_lut_free (tool->lut);

202
  if (tool->hist)
203
    {
204 205
      gimp_histogram_free (tool->hist);
      tool->hist = NULL;
206
    }
207

208
  G_OBJECT_CLASS (parent_class)->finalize (object);
Michael Natterer's avatar
Michael Natterer committed
209 210
}

211
static gboolean
212 213 214
gimp_curves_tool_initialize (GimpTool     *tool,
                             GimpDisplay  *display,
                             GError      **error)
Michael Natterer's avatar
Michael Natterer committed
215
{
216 217
  GimpCurvesTool *c_tool   = GIMP_CURVES_TOOL (tool);
  GimpDrawable   *drawable = gimp_image_get_active_drawable (display->image);
218
  gint            i;
219 220

  if (! drawable)
221
    return FALSE;
222

223
  if (gimp_drawable_is_indexed (drawable))
Michael Natterer's avatar
Michael Natterer committed
224
    {
225
      g_set_error (error, 0, 0,
226
                   _("Curves does not operate on indexed layers."));
227
      return FALSE;
228
    }
Michael Natterer's avatar
Michael Natterer committed
229

230 231 232
  for (i = 0; i < G_N_ELEMENTS (c_tool->curve); i++)
    gimp_curve_reset (c_tool->curve[i], TRUE);

233
  if (! c_tool->hist)
234
    c_tool->hist = gimp_histogram_new ();
235

236
  c_tool->channel = GIMP_HISTOGRAM_VALUE;
237 238
  c_tool->color   = gimp_drawable_is_rgb (drawable);
  c_tool->alpha   = gimp_drawable_has_alpha (drawable);
239

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

242 243
  /*  always pick colors  */
  gimp_color_tool_enable (GIMP_COLOR_TOOL (tool),
244
                          GIMP_COLOR_TOOL_GET_OPTIONS (tool));
245

246 247
  gimp_int_combo_box_set_sensitivity (GIMP_INT_COMBO_BOX (c_tool->channel_menu),
                                      curves_menu_sensitivity, c_tool, NULL);
248

249 250
  gimp_int_combo_box_set_active (GIMP_INT_COMBO_BOX (c_tool->channel_menu),
                                 c_tool->channel);
Michael Natterer's avatar
Michael Natterer committed
251

252 253 254
  /* FIXME: hack */
  if (! c_tool->color)
    c_tool->channel = (c_tool->channel == GIMP_HISTOGRAM_ALPHA) ? 1 : 0;
255

256 257 258
  gimp_drawable_calculate_histogram (drawable, c_tool->hist);
  gimp_histogram_view_set_histogram (GIMP_HISTOGRAM_VIEW (c_tool->graph),
                                     c_tool->hist);
259 260
  gimp_curve_view_set_curve (GIMP_CURVE_VIEW (c_tool->graph),
                             c_tool->curve[c_tool->channel]);
261

262
  return TRUE;
Michael Natterer's avatar
Michael Natterer committed
263 264 265
}

static void
266 267 268 269 270 271
gimp_curves_tool_button_release (GimpTool              *tool,
                                 GimpCoords            *coords,
                                 guint32                time,
                                 GdkModifierType        state,
                                 GimpButtonReleaseType  release_type,
                                 GimpDisplay           *display)
Michael Natterer's avatar
Michael Natterer committed
272
{
273
  GimpCurvesTool *c_tool = GIMP_CURVES_TOOL (tool);
Michael Natterer's avatar
Michael Natterer committed
274

275
  if (state & GDK_SHIFT_MASK)
Michael Natterer's avatar
Michael Natterer committed
276
    {
277
      GimpCurve *curve = c_tool->curve[c_tool->channel];
278
      gint       closest;
279

280
      closest =
281 282 283
        gimp_curve_get_closest_point (curve,
                                      c_tool->col_value[c_tool->channel]);

284
      gimp_curve_view_set_selected (GIMP_CURVE_VIEW (c_tool->graph),
285
                                    closest);
286

287
      gimp_curve_set_point (curve,
288
                            closest,
289 290
                            c_tool->col_value[c_tool->channel],
                            curve->curve[c_tool->col_value[c_tool->channel]]);
Michael Natterer's avatar
Michael Natterer committed
291
    }
292
  else if (state & GDK_CONTROL_MASK)
Michael Natterer's avatar
Michael Natterer committed
293
    {
294
      gint i;
295

296 297
      for (i = 0; i < 5; i++)
        {
298
          GimpCurve *curve = c_tool->curve[i];
299
          gint       closest;
300

301
          closest =
302 303 304
            gimp_curve_get_closest_point (curve,
                                          c_tool->col_value[i]);

305
          gimp_curve_view_set_selected (GIMP_CURVE_VIEW (c_tool->graph),
306
                                        closest);
307

308
          gimp_curve_set_point (curve,
309
                                closest,
310 311
                                c_tool->col_value[i],
                                curve->curve[c_tool->col_value[i]]);
312
        }
Michael Natterer's avatar
Michael Natterer committed
313 314
    }

315
  /*  chain up to halt the tool */
316 317
  GIMP_TOOL_CLASS (parent_class)->button_release (tool, coords, time, state,
                                                  release_type, display);
Michael Natterer's avatar
Michael Natterer committed
318 319
}

320 321 322
gboolean
gimp_curves_tool_key_press (GimpTool    *tool,
                            GdkEventKey *kevent,
323
                            GimpDisplay *display)
324
{
325 326 327
  GimpCurvesTool *c_tool = GIMP_CURVES_TOOL (tool);

  return gtk_widget_event (c_tool->graph, (GdkEvent *) kevent);
328 329 330 331 332 333
}

static void
gimp_curves_tool_oper_update (GimpTool        *tool,
                              GimpCoords      *coords,
                              GdkModifierType  state,
334
                              gboolean         proximity,
335
                              GimpDisplay     *display)
336 337 338 339
{
  GimpColorPickMode  mode   = GIMP_COLOR_PICK_MODE_NONE;
  const gchar       *status = NULL;

340
  GIMP_TOOL_CLASS (parent_class)->oper_update (tool, coords, state, proximity,
341
                                               display);
342

343
  gimp_tool_pop_status (tool, display);
344 345 346 347

  if (state & GDK_SHIFT_MASK)
    {
      mode   = GIMP_COLOR_PICK_MODE_PALETTE;
348
      status = _("Click to add a control point");
349 350 351 352
    }
  else if (state & GDK_CONTROL_MASK)
    {
      mode   = GIMP_COLOR_PICK_MODE_PALETTE;
353
      status = _("Click to add control points to all channels");
354 355 356 357
    }

  GIMP_COLOR_TOOL (tool)->pick_mode = mode;

358
  if (status && proximity)
359
    gimp_tool_push_status (tool, display, status);
360 361
}

Michael Natterer's avatar
Michael Natterer committed
362
static void
363 364
gimp_curves_tool_color_picked (GimpColorTool      *color_tool,
                               GimpColorPickState  pick_state,
365 366 367
                               GimpImageType       sample_type,
                               GimpRGB            *color,
                               gint                color_index)
Michael Natterer's avatar
Michael Natterer committed
368
{
369 370 371 372
  GimpCurvesTool       *tool = GIMP_CURVES_TOOL (color_tool);
  GimpDrawable         *drawable;
  guchar                r, g, b, a;
  GimpHistogramChannel  channel;
Michael Natterer's avatar
Michael Natterer committed
373

374
  drawable = GIMP_IMAGE_MAP_TOOL (tool)->drawable;
375

376
  gimp_rgba_get_uchar (color, &r, &g, &b, &a);
377

378 379 380
  tool->col_value[GIMP_HISTOGRAM_RED]   = r;
  tool->col_value[GIMP_HISTOGRAM_GREEN] = g;
  tool->col_value[GIMP_HISTOGRAM_BLUE]  = b;
381

382
  if (gimp_drawable_has_alpha (drawable))
383
    tool->col_value[GIMP_HISTOGRAM_ALPHA] = a;
384

385
  if (gimp_drawable_is_indexed (drawable))
386
    tool->col_value[GIMP_HISTOGRAM_ALPHA] = color_index;
387

388
  tool->col_value[GIMP_HISTOGRAM_VALUE] = MAX (MAX (r, g), b);
BST 1999 Andy Thomas's avatar
BST 1999 Andy Thomas committed
389

390 391 392 393 394
  if (tool->color)
    channel = tool->channel;
  else
    channel = (tool->channel == 1) ? GIMP_HISTOGRAM_ALPHA : GIMP_HISTOGRAM_VALUE;

395 396
  gimp_curve_view_set_xpos (GIMP_CURVE_VIEW (tool->graph),
                            tool->col_value[channel]);
397
}
BST 1999 Andy Thomas's avatar
BST 1999 Andy Thomas committed
398

399 400
static void
gimp_curves_tool_map (GimpImageMapTool *image_map_tool)
Elliot Lee's avatar
Elliot Lee committed
401
{
402
  GimpCurvesTool *tool = GIMP_CURVES_TOOL (image_map_tool);
403 404 405 406 407
  Curves          curves;
  gint            i;

  for (i = 0; i < G_N_ELEMENTS (tool->curve); i++)
    gimp_curve_get_uchar (tool->curve[i], curves.curve[i]);
408

409
  gimp_lut_setup (tool->lut,
410
                  (GimpLutFunc) curves_lut_func,
411
                  &curves,
412
                  gimp_drawable_bytes (image_map_tool->drawable));
413

414 415 416
  gimp_image_map_apply (image_map_tool->image_map,
                        (GimpImageMapApplyFunc) gimp_lut_process,
                        tool->lut);
Elliot Lee's avatar
Elliot Lee committed
417 418
}

419

420 421 422
/*******************/
/*  Curves dialog  */
/*******************/
Elliot Lee's avatar
Elliot Lee committed
423

424 425
static void
gimp_curves_tool_dialog (GimpImageMapTool *image_map_tool)
Elliot Lee's avatar
Elliot Lee committed
426
{
427 428
  GimpCurvesTool  *tool         = GIMP_CURVES_TOOL (image_map_tool);
  GimpToolOptions *tool_options = GIMP_TOOL_GET_OPTIONS (image_map_tool);
429
  GtkListStore    *store;
430
  GtkWidget       *vbox;
431 432 433 434 435
  GtkWidget       *vbox2;
  GtkWidget       *hbox;
  GtkWidget       *hbox2;
  GtkWidget       *label;
  GtkWidget       *bbox;
436 437 438 439
  GtkWidget       *frame;
  GtkWidget       *menu;
  GtkWidget       *table;
  GtkWidget       *button;
440
  GtkWidget       *bar;
441
  gint             padding;
442

443
  vbox = image_map_tool->main_vbox;
Elliot Lee's avatar
Elliot Lee committed
444

445
  /*  The option menu for selecting channels  */
446
  hbox = gtk_hbox_new (FALSE, 6);
447 448 449
  gtk_box_pack_start (GTK_BOX (vbox), hbox, FALSE, FALSE, 0);
  gtk_widget_show (hbox);

450
  label = gtk_label_new_with_mnemonic (_("Cha_nnel:"));
451 452 453
  gtk_box_pack_start (GTK_BOX (hbox), label, FALSE, FALSE, 0);
  gtk_widget_show (label);

454 455 456
  store = gimp_enum_store_new_with_range (GIMP_TYPE_HISTOGRAM_CHANNEL,
                                          GIMP_HISTOGRAM_VALUE,
                                          GIMP_HISTOGRAM_ALPHA);
457
  menu = gimp_enum_combo_box_new_with_model (GIMP_ENUM_STORE (store));
458 459
  g_object_unref (store);

460 461 462 463 464
  g_signal_connect (menu, "changed",
                    G_CALLBACK (curves_channel_callback),
                    tool);
  gimp_enum_combo_box_set_stock_prefix (GIMP_ENUM_COMBO_BOX (menu),
                                        "gimp-channel");
465 466 467
  gtk_box_pack_start (GTK_BOX (hbox), menu, FALSE, FALSE, 0);
  gtk_widget_show (menu);

468
  tool->channel_menu = menu;
469

470 471
  gtk_label_set_mnemonic_widget (GTK_LABEL (label), menu);

472
  button = gtk_button_new_with_mnemonic (_("R_eset Channel"));
473
  gtk_box_pack_start (GTK_BOX (hbox), button, FALSE, FALSE, 0);
474
  gtk_widget_show (button);
475

476
  g_signal_connect (button, "clicked",
477
                    G_CALLBACK (curves_channel_reset_callback),
478
                    tool);
Elliot Lee's avatar
Elliot Lee committed
479

480 481 482 483 484
  menu = gimp_prop_enum_stock_box_new (G_OBJECT (tool_options),
                                       "histogram-scale", "gimp-histogram",
                                       0, 0);
  gtk_box_pack_end (GTK_BOX (hbox), menu, FALSE, FALSE, 0);
  gtk_widget_show (menu);
485

486
  /*  The table for the color bars and the graph  */
Elliot Lee's avatar
Elliot Lee committed
487
  table = gtk_table_new (2, 2, FALSE);
488 489
  gtk_table_set_col_spacings (GTK_TABLE (table), 2);
  gtk_table_set_row_spacings (GTK_TABLE (table), 2);
490 491 492 493 494
  gtk_box_pack_start (GTK_BOX (vbox), table, TRUE, TRUE, 0);

  /*  The left color bar  */
  vbox2 = gtk_vbox_new (FALSE, 0);
  gtk_table_attach (GTK_TABLE (table), vbox2, 0, 1, 0, 1,
495
                    GTK_FILL, GTK_EXPAND | GTK_FILL, 0, 0);
496
  gtk_widget_show (vbox2);
Elliot Lee's avatar
Elliot Lee committed
497 498

  frame = gtk_frame_new (NULL);
499
  gtk_frame_set_shadow_type (GTK_FRAME (frame), GTK_SHADOW_IN);
500
  gtk_box_pack_start (GTK_BOX (vbox2), frame, TRUE, TRUE, RADIUS);
Elliot Lee's avatar
Elliot Lee committed
501 502
  gtk_widget_show (frame);

503 504 505 506
  tool->yrange = gimp_color_bar_new (GTK_ORIENTATION_VERTICAL);
  gtk_widget_set_size_request (tool->yrange, BAR_SIZE, -1);
  gtk_container_add (GTK_CONTAINER (frame), tool->yrange);
  gtk_widget_show (tool->yrange);
507

Elliot Lee's avatar
Elliot Lee committed
508 509
  /*  The curves graph  */
  frame = gtk_frame_new (NULL);
510
  gtk_frame_set_shadow_type (GTK_FRAME (frame), GTK_SHADOW_IN);
Elliot Lee's avatar
Elliot Lee committed
511
  gtk_table_attach (GTK_TABLE (table), frame, 1, 2, 0, 1,
512
                    GTK_EXPAND | GTK_FILL, GTK_EXPAND | GTK_FILL, 0, 0);
513
  gtk_widget_show (frame);
Elliot Lee's avatar
Elliot Lee committed
514

515
  tool->graph = gimp_curve_view_new ();
516 517 518 519
  gtk_widget_set_size_request (tool->graph,
                               GRAPH_SIZE + RADIUS * 2,
                               GRAPH_SIZE + RADIUS * 2);
  g_object_set (tool->graph,
520 521 522
                "border-width", RADIUS,
                "subdivisions", 1,
                NULL);
523 524
  gtk_container_add (GTK_CONTAINER (frame), tool->graph);
  gtk_widget_show (tool->graph);
525

526
  gimp_histogram_options_connect_view (GIMP_HISTOGRAM_OPTIONS (tool_options),
527 528 529 530 531
                                       GIMP_HISTOGRAM_VIEW (tool->graph));

  /*  The bottom color bar  */
  hbox2 = gtk_hbox_new (FALSE, 0);
  gtk_table_attach (GTK_TABLE (table), hbox2, 1, 2, 1, 2,
532
                    GTK_EXPAND | GTK_FILL, GTK_FILL, 0, 0);
533
  gtk_widget_show (hbox2);
534

Elliot Lee's avatar
Elliot Lee committed
535
  frame = gtk_frame_new (NULL);
536
  gtk_frame_set_shadow_type (GTK_FRAME (frame), GTK_SHADOW_IN);
537
  gtk_box_pack_start (GTK_BOX (hbox2), frame, TRUE, TRUE, RADIUS);
Elliot Lee's avatar
Elliot Lee committed
538 539
  gtk_widget_show (frame);

540 541 542 543
  vbox2 = gtk_vbox_new (TRUE, 0);
  gtk_container_add (GTK_CONTAINER (frame), vbox2);
  gtk_widget_show (vbox2);

544 545 546 547
  tool->xrange = gimp_color_bar_new (GTK_ORIENTATION_HORIZONTAL);
  gtk_widget_set_size_request (tool->xrange, -1, BAR_SIZE / 2);
  gtk_box_pack_start (GTK_BOX (vbox2), tool->xrange, TRUE, TRUE, 0);
  gtk_widget_show (tool->xrange);
548

549 550 551 552
  bar = gimp_color_bar_new (GTK_ORIENTATION_HORIZONTAL);
  gtk_box_pack_start (GTK_BOX (vbox2), bar, TRUE, TRUE, 0);
  gtk_widget_show (bar);

553
  gtk_widget_show (table);
Elliot Lee's avatar
Elliot Lee committed
554

555

556 557 558
  hbox = gtk_hbox_new (FALSE, 6);
  gtk_box_pack_end (GTK_BOX (vbox), hbox, FALSE, FALSE, 0);
  gtk_widget_show (hbox);
559

560
  /*  Horizontal button box for load / save */
561
  frame = gimp_frame_new (_("All Channels"));
562
  gtk_box_pack_start (GTK_BOX (hbox), frame, FALSE, FALSE, 0);
563 564
  gtk_widget_show (frame);

565 566
  bbox = gtk_hbutton_box_new ();
  gtk_box_set_spacing (GTK_BOX (bbox), 4);
567
  gtk_container_add (GTK_CONTAINER (frame), bbox);
568
  gtk_widget_show (bbox);
569

570 571 572
  gtk_box_pack_start (GTK_BOX (bbox), image_map_tool->load_button,
                      FALSE, FALSE, 0);
  gtk_widget_show (image_map_tool->load_button);
573

574 575 576
  gtk_box_pack_start (GTK_BOX (bbox), image_map_tool->save_button,
                      FALSE, FALSE, 0);
  gtk_widget_show (image_map_tool->save_button);
577 578

  /*  The radio box for selecting the curve type  */
579
  frame = gimp_frame_new (_("Curve Type"));
580 581 582 583
  gtk_box_pack_end (GTK_BOX (hbox), frame, FALSE, FALSE, 0);
  gtk_widget_show (frame);

  hbox = gimp_enum_stock_box_new (GIMP_TYPE_CURVE_TYPE,
584 585 586 587
                                  "gimp-curve", GTK_ICON_SIZE_MENU,
                                  G_CALLBACK (curves_curve_type_callback),
                                  tool,
                                  &tool->curve_type);
588

589
  gtk_widget_style_get (bbox, "child-internal-pad-x", &padding, NULL);
590 591

  gimp_enum_stock_box_set_child_padding (hbox, padding, -1);
592 593 594 595 596

  gtk_container_set_border_width (GTK_CONTAINER (hbox), 2);

  gtk_container_add (GTK_CONTAINER (frame), hbox);
  gtk_widget_show (hbox);
Elliot Lee's avatar
Elliot Lee committed
597 598
}

599
static void
600
gimp_curves_tool_reset (GimpImageMapTool *image_map_tool)
601
{
602
  GimpCurvesTool       *tool = GIMP_CURVES_TOOL (image_map_tool);
603
  GimpHistogramChannel  channel;
604

605 606 607
  for (channel =  GIMP_HISTOGRAM_VALUE;
       channel <= GIMP_HISTOGRAM_ALPHA;
       channel++)
608 609 610
    {
      gimp_curve_reset (tool->curve[channel], FALSE);
    }
611 612
}

613
static gboolean
614 615 616
gimp_curves_tool_settings_load (GimpImageMapTool  *image_map_tool,
                                gpointer           fp,
                                GError           **error)
617
{
618 619
  GimpCurvesTool *tool = GIMP_CURVES_TOOL (image_map_tool);
  FILE           *file = fp;
620 621 622
  gint            i, j;
  gint            fields;
  gchar           buf[50];
623 624
  gint            index[5][GIMP_CURVE_NUM_POINTS];
  gint            value[5][GIMP_CURVE_NUM_POINTS];
625

626 627 628 629
  if (! fgets (buf, sizeof (buf), file) ||
      strcmp (buf, "# GIMP Curves File\n") != 0)
    {
      g_set_error (error, GIMP_CONFIG_ERROR, GIMP_CONFIG_ERROR_PARSE,
630
                   _("not a GIMP Curves file"));
631 632
      return FALSE;
    }
633 634 635

  for (i = 0; i < 5; i++)
    {
636
      for (j = 0; j < GIMP_CURVE_NUM_POINTS; j++)
637 638 639 640
        {
          fields = fscanf (file, "%d %d ", &index[i][j], &value[i][j]);
          if (fields != 2)
            {
641
              /*  FIXME: should have a helpful error message here  */
642
              g_printerr ("fields != 2");
643 644
              g_set_error (error, GIMP_CONFIG_ERROR, GIMP_CONFIG_ERROR_PARSE,
                           _("parse error"));
645 646 647
              return FALSE;
            }
        }
648 649 650 651
    }

  for (i = 0; i < 5; i++)
    {
652
      GimpCurve *curve = tool->curve[i];
653

654 655 656
      gimp_data_freeze (GIMP_DATA (curve));

      gimp_curve_set_curve_type (curve, GIMP_CURVE_SMOOTH);
657 658

      for (j = 0; j < GIMP_CURVE_NUM_POINTS; j++)
659
        gimp_curve_set_point (curve, j, index[i][j], value[i][j]);
660

661
      gimp_data_thaw (GIMP_DATA (curve));
662
    }
663 664

  gimp_int_radio_group_set_active (GTK_RADIO_BUTTON (tool->curve_type),
665
                                   GIMP_CURVE_SMOOTH);
666 667

  return TRUE;
668 669 670
}

static gboolean
671 672
gimp_curves_tool_settings_save (GimpImageMapTool *image_map_tool,
                                gpointer          fp)
673
{
674 675
  GimpCurvesTool *tool = GIMP_CURVES_TOOL (image_map_tool);
  FILE           *file = fp;
676 677 678 679 680 681 682
  gint            i, j;
  gint32          index;

  fprintf (file, "# GIMP Curves File\n");

  for (i = 0; i < 5; i++)
    {
683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699
      GimpCurve *curve = tool->curve[i];

      if (curve->curve_type == GIMP_CURVE_FREE)
        {
          /* pick representative points from the curve and make them
           * control points
           */
          for (j = 0; j <= 8; j++)
            {
              index = CLAMP0255 (j * 32);

              curve->points[j * 2][0] = index;
              curve->points[j * 2][1] = curve->curve[index];
            }
        }

      for (j = 0; j < GIMP_CURVE_NUM_POINTS; j++)
700
        fprintf (file, "%d %d ",
701 702
                 curve->points[j][0],
                 curve->points[j][1]);
703 704 705 706 707

      fprintf (file, "\n");
    }

  return TRUE;
708
}
709

Elliot Lee's avatar
Elliot Lee committed
710
static void
711 712
curves_curve_callback (GimpCurve      *curve,
                       GimpCurvesTool *tool)
Elliot Lee's avatar
Elliot Lee committed
713
{
714 715 716 717
  if (curve != tool->curve[tool->channel])
    return;

  if (tool->xrange)
718
    {
719
      GimpHistogramChannel channel;
720

721 722 723 724 725
      if (tool->color)
        channel = tool->channel;
      else
        channel = ((tool->channel == 1) ?
                   GIMP_HISTOGRAM_ALPHA : GIMP_HISTOGRAM_VALUE);
726

727
      switch (channel)
728 729 730
        {
        case GIMP_HISTOGRAM_VALUE:
        case GIMP_HISTOGRAM_ALPHA:
731
        case GIMP_HISTOGRAM_RGB:
732
          gimp_color_bar_set_buffers (GIMP_COLOR_BAR (tool->xrange),
733 734 735
                                      tool->curve[tool->channel]->curve,
                                      tool->curve[tool->channel]->curve,
                                      tool->curve[tool->channel]->curve);
736
          break;
737

738 739 740
        case GIMP_HISTOGRAM_RED:
        case GIMP_HISTOGRAM_GREEN:
        case GIMP_HISTOGRAM_BLUE:
741
          gimp_color_bar_set_buffers (GIMP_COLOR_BAR (tool->xrange),
742 743 744
                                      tool->curve[GIMP_HISTOGRAM_RED]->curve,
                                      tool->curve[GIMP_HISTOGRAM_GREEN]->curve,
                                      tool->curve[GIMP_HISTOGRAM_BLUE]->curve);
745 746
          break;
        }
Elliot Lee's avatar
Elliot Lee committed
747
    }
748 749 750

  if (GIMP_IMAGE_MAP_TOOL (tool)->drawable)
    gimp_image_map_tool_preview (GIMP_IMAGE_MAP_TOOL (tool));
Elliot Lee's avatar
Elliot Lee committed
751 752 753
}

static void
754
curves_channel_callback (GtkWidget      *widget,
755
                         GimpCurvesTool *tool)
Elliot Lee's avatar
Elliot Lee committed
756
{
757
  gint value;
758

759 760 761 762 763
  if (gimp_int_combo_box_get_active (GIMP_INT_COMBO_BOX (widget), &value))
    {
      tool->channel = value;
      gimp_histogram_view_set_channel (GIMP_HISTOGRAM_VIEW (tool->graph),
                                       tool->channel);
764 765
      gimp_curve_view_set_xpos (GIMP_CURVE_VIEW (tool->graph),
                                tool->col_value[tool->channel]);
766 767

      gimp_color_bar_set_channel (GIMP_COLOR_BAR (tool->yrange), tool->channel);
Elliot Lee's avatar
Elliot Lee committed
768

769 770 771
      /* FIXME: hack */
      if (! tool->color)
        tool->channel = (tool->channel == GIMP_HISTOGRAM_ALPHA) ? 1 : 0;
772

773 774
      gimp_curve_view_set_curve (GIMP_CURVE_VIEW (tool->graph),
                                 tool->curve[tool->channel]);
775

776
      gimp_int_radio_group_set_active (GTK_RADIO_BUTTON (tool->curve_type),
777
                                       tool->curve[tool->channel]->curve_type);
778

779
      curves_curve_callback (tool->curve[tool->channel], tool);
780
    }
Elliot Lee's avatar
Elliot Lee committed
781 782 783
}

static void
784
curves_channel_reset_callback (GtkWidget      *widget,
785
                               GimpCurvesTool *tool)
Elliot Lee's avatar
Elliot Lee committed
786
{
787
  gimp_curve_reset (tool->curve[tool->channel], FALSE);
Elliot Lee's avatar
Elliot Lee committed
788 789
}

790
static gboolean
791 792
curves_menu_sensitivity (gint      value,
                         gpointer  data)
793
{
794 795
  GimpCurvesTool       *tool    = GIMP_CURVES_TOOL (data);
  GimpHistogramChannel  channel = value;
Sven Neumann's avatar
Sven Neumann committed
796

797 798 799 800
  switch (channel)
    {
    case GIMP_HISTOGRAM_VALUE:
      return TRUE;
801

802 803 804
    case GIMP_HISTOGRAM_RED:
    case GIMP_HISTOGRAM_GREEN:
    case GIMP_HISTOGRAM_BLUE:
805
      return tool->color;
806

807
    case GIMP_HISTOGRAM_ALPHA:
Sven Neumann's avatar
Sven Neumann committed
808
      return tool->alpha;
809 810 811

    case GIMP_HISTOGRAM_RGB:
      return FALSE;
812
    }
813

814 815 816
  return FALSE;
}

Elliot Lee's avatar
Elliot Lee committed
817
static void
818
curves_curve_type_callback (GtkWidget      *widget,
819
                            GimpCurvesTool *tool)
Elliot Lee's avatar
Elliot Lee committed
820
{
821
  GimpCurveType curve_type;
Elliot Lee's avatar
Elliot Lee committed
822