gimpcurvestool.c 36.6 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
#include <gtk/gtk.h>
26
#include <gdk/gdkkeysyms.h>
Sven Neumann's avatar
Sven Neumann committed
27

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

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

35
#include "config/gimpguiconfig.h"
36

37
#include "base/curves.h"
Michael Natterer's avatar
Michael Natterer committed
38 39 40
#include "base/gimphistogram.h"
#include "base/gimplut.h"

41
#include "core/gimp.h"
42
#include "core/gimpcurve.h"
Michael Natterer's avatar
Michael Natterer committed
43
#include "core/gimpdrawable.h"
44
#include "core/gimpdrawable-histogram.h"
Michael Natterer's avatar
Michael Natterer committed
45
#include "core/gimpimage.h"
46
#include "core/gimpimagemap.h"
47
#include "core/gimptoolinfo.h"
Michael Natterer's avatar
Michael Natterer committed
48

49
#include "widgets/gimpcolorbar.h"
Michael Natterer's avatar
Michael Natterer committed
50
#include "widgets/gimpcursor.h"
51
#include "widgets/gimphelp-ids.h"
52
#include "widgets/gimphistogramview.h"
Michael Natterer's avatar
Michael Natterer committed
53

54 55
#include "display/gimpdisplay.h"

Michael Natterer's avatar
Michael Natterer committed
56
#include "gimpcurvestool.h"
57
#include "gimphistogramoptions.h"
58
#include "gimptoolcontrol.h"
Sven Neumann's avatar
Sven Neumann committed
59

60
#include "gimp-intl.h"
61

62

63 64 65
#define GRAPH_SIZE 256
#define BAR_SIZE    12
#define RADIUS       4
Elliot Lee's avatar
Elliot Lee committed
66 67


Michael Natterer's avatar
Michael Natterer committed
68 69
/*  local function prototypes  */

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
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);

static gboolean curves_key_press                (GimpCurvesTool       *tool,
                                                 GdkEventKey          *kevent);

107 108 109
static void     curves_curve_callback           (GimpCurve            *curve,
                                                 const GParamSpec     *pspec,
                                                 GimpCurvesTool       *tool);
110 111 112 113 114 115 116 117 118 119 120 121 122
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);
static gboolean curves_graph_events             (GtkWidget            *widget,
                                                 GdkEvent             *event,
                                                 GimpCurvesTool       *tool);
123

Michael Natterer's avatar
Michael Natterer committed
124

125
G_DEFINE_TYPE (GimpCurvesTool, gimp_curves_tool, GIMP_TYPE_IMAGE_MAP_TOOL)
126 127

#define parent_class gimp_curves_tool_parent_class
128

Michael Natterer's avatar
Michael Natterer committed
129

130
/*  public functions  */
Michael Natterer's avatar
Michael Natterer committed
131 132

void
Nate Summers's avatar
Nate Summers committed
133
gimp_curves_tool_register (GimpToolRegisterCallback  callback,
134
                           gpointer                  data)
Michael Natterer's avatar
Michael Natterer committed
135
{
Nate Summers's avatar
Nate Summers committed
136
  (* callback) (GIMP_TYPE_CURVES_TOOL,
137
                GIMP_TYPE_HISTOGRAM_OPTIONS,
138
                gimp_color_options_gui,
139
                0,
140
                "gimp-curves-tool",
141
                _("Curves"),
142
                _("Curves Tool: Adjust color curves"),
143
                N_("_Curves..."), NULL,
144
                NULL, GIMP_HELP_TOOL_CURVES,
Nate Summers's avatar
Nate Summers committed
145
                GIMP_STOCK_TOOL_CURVES,
146
                data);
Michael Natterer's avatar
Michael Natterer committed
147 148
}

149 150 151

/*  private functions  */

Michael Natterer's avatar
Michael Natterer committed
152 153 154
static void
gimp_curves_tool_class_init (GimpCurvesToolClass *klass)
{
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
  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
181 182 183
}

static void
184
gimp_curves_tool_init (GimpCurvesTool *tool)
Michael Natterer's avatar
Michael Natterer committed
185
{
186
  gint i;
187

188
  for (i = 0; i < G_N_ELEMENTS (tool->curve); i++)
189 190 191 192 193 194 195
    {
      tool->curve[i] = GIMP_CURVE (gimp_curve_new ("curves tool"));

      g_signal_connect_object (tool->curve[i], "notify::curve",
                               G_CALLBACK (curves_curve_callback),
                               tool, 0);
    }
196

197 198
  tool->lut     = gimp_lut_new ();
  tool->channel = GIMP_HISTOGRAM_VALUE;
199

200 201
  for (i = 0; i < G_N_ELEMENTS (tool->col_value); i++)
    tool->col_value[i] = -1;
Michael Natterer's avatar
Michael Natterer committed
202 203 204
}

static void
205
gimp_curves_tool_finalize (GObject *object)
Michael Natterer's avatar
Michael Natterer committed
206
{
207
  GimpCurvesTool *tool = GIMP_CURVES_TOOL (object);
208 209 210 211
  gint            i;

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

213 214
  gimp_lut_free (tool->lut);

215
  if (tool->hist)
216
    {
217 218
      gimp_histogram_free (tool->hist);
      tool->hist = NULL;
219
    }
220

221
  G_OBJECT_CLASS (parent_class)->finalize (object);
Michael Natterer's avatar
Michael Natterer committed
222 223
}

224
static gboolean
225 226 227
gimp_curves_tool_initialize (GimpTool     *tool,
                             GimpDisplay  *display,
                             GError      **error)
Michael Natterer's avatar
Michael Natterer committed
228
{
229 230
  GimpCurvesTool *c_tool   = GIMP_CURVES_TOOL (tool);
  GimpDrawable   *drawable = gimp_image_get_active_drawable (display->image);
231
  gint            i;
232 233

  if (! drawable)
234
    return FALSE;
235

236
  if (gimp_drawable_is_indexed (drawable))
Michael Natterer's avatar
Michael Natterer committed
237
    {
238
      g_set_error (error, 0, 0,
239
                   _("Curves does not operate on indexed layers."));
240
      return FALSE;
241
    }
Michael Natterer's avatar
Michael Natterer committed
242

243 244 245
  for (i = 0; i < G_N_ELEMENTS (c_tool->curve); i++)
    gimp_curve_reset (c_tool->curve[i], TRUE);

246
  if (! c_tool->hist)
247
    c_tool->hist = gimp_histogram_new ();
248

249
  c_tool->channel = GIMP_HISTOGRAM_VALUE;
250 251
  c_tool->color   = gimp_drawable_is_rgb (drawable);
  c_tool->alpha   = gimp_drawable_has_alpha (drawable);
252

253 254 255
  c_tool->selected = 0;
  c_tool->grabbed  = FALSE;
  c_tool->last     = 0;
256

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

259 260
  /*  always pick colors  */
  gimp_color_tool_enable (GIMP_COLOR_TOOL (tool),
261
                          GIMP_COLOR_TOOL_GET_OPTIONS (tool));
262

263 264
  gimp_int_combo_box_set_sensitivity (GIMP_INT_COMBO_BOX (c_tool->channel_menu),
                                      curves_menu_sensitivity, c_tool, NULL);
265

266 267
  gimp_int_combo_box_set_active (GIMP_INT_COMBO_BOX (c_tool->channel_menu),
                                 c_tool->channel);
Michael Natterer's avatar
Michael Natterer committed
268

269 270 271
  /* FIXME: hack */
  if (! c_tool->color)
    c_tool->channel = (c_tool->channel == GIMP_HISTOGRAM_ALPHA) ? 1 : 0;
272

273 274 275
  gimp_drawable_calculate_histogram (drawable, c_tool->hist);
  gimp_histogram_view_set_histogram (GIMP_HISTOGRAM_VIEW (c_tool->graph),
                                     c_tool->hist);
276 277
  gimp_histogram_view_set_curve (GIMP_HISTOGRAM_VIEW (c_tool->graph),
                                 c_tool->curve[c_tool->channel]);
278

279
  return TRUE;
Michael Natterer's avatar
Michael Natterer committed
280 281 282
}

static void
283 284 285 286 287 288
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
289
{
290
  GimpCurvesTool *c_tool = GIMP_CURVES_TOOL (tool);
Michael Natterer's avatar
Michael Natterer committed
291

292
  if (state & GDK_SHIFT_MASK)
Michael Natterer's avatar
Michael Natterer committed
293
    {
294 295 296 297 298 299
      GimpCurve *curve = c_tool->curve[c_tool->channel];

      c_tool->selected =
        gimp_curve_get_closest_point (curve,
                                      c_tool->col_value[c_tool->channel]);

300 301
      gimp_histogram_view_set_selected (GIMP_HISTOGRAM_VIEW (c_tool->graph),
                                        c_tool->selected);
302

303 304 305 306
      gimp_curve_set_point (curve,
                            c_tool->selected,
                            c_tool->col_value[c_tool->channel],
                            curve->curve[c_tool->col_value[c_tool->channel]]);
Michael Natterer's avatar
Michael Natterer committed
307
    }
308
  else if (state & GDK_CONTROL_MASK)
Michael Natterer's avatar
Michael Natterer committed
309
    {
310
      gint i;
311

312 313
      for (i = 0; i < 5; i++)
        {
314 315 316 317 318 319
          GimpCurve *curve = c_tool->curve[i];

          c_tool->selected =
            gimp_curve_get_closest_point (curve,
                                          c_tool->col_value[i]);

320 321
          gimp_histogram_view_set_selected (GIMP_HISTOGRAM_VIEW (c_tool->graph),
                                            c_tool->selected);
322

323 324 325 326
          gimp_curve_set_point (curve,
                                c_tool->selected,
                                c_tool->col_value[i],
                                curve->curve[c_tool->col_value[i]]);
327
        }
Michael Natterer's avatar
Michael Natterer committed
328 329
    }

330
  /*  chain up to halt the tool */
331 332
  GIMP_TOOL_CLASS (parent_class)->button_release (tool, coords, time, state,
                                                  release_type, display);
Michael Natterer's avatar
Michael Natterer committed
333 334
}

335 336 337
gboolean
gimp_curves_tool_key_press (GimpTool    *tool,
                            GdkEventKey *kevent,
338
                            GimpDisplay *display)
339 340 341 342 343 344 345 346
{
  return curves_key_press (GIMP_CURVES_TOOL (tool), kevent);
}

static void
gimp_curves_tool_oper_update (GimpTool        *tool,
                              GimpCoords      *coords,
                              GdkModifierType  state,
347
                              gboolean         proximity,
348
                              GimpDisplay     *display)
349 350 351 352
{
  GimpColorPickMode  mode   = GIMP_COLOR_PICK_MODE_NONE;
  const gchar       *status = NULL;

353
  GIMP_TOOL_CLASS (parent_class)->oper_update (tool, coords, state, proximity,
354
                                               display);
355

356
  gimp_tool_pop_status (tool, display);
357 358 359 360

  if (state & GDK_SHIFT_MASK)
    {
      mode   = GIMP_COLOR_PICK_MODE_PALETTE;
361
      status = _("Click to add a control point");
362 363 364 365
    }
  else if (state & GDK_CONTROL_MASK)
    {
      mode   = GIMP_COLOR_PICK_MODE_PALETTE;
366
      status = _("Click to add control points to all channels");
367 368 369 370
    }

  GIMP_COLOR_TOOL (tool)->pick_mode = mode;

371
  if (status && proximity)
372
    gimp_tool_push_status (tool, display, status);
373 374
}

Michael Natterer's avatar
Michael Natterer committed
375
static void
376 377
gimp_curves_tool_color_picked (GimpColorTool      *color_tool,
                               GimpColorPickState  pick_state,
378 379 380
                               GimpImageType       sample_type,
                               GimpRGB            *color,
                               gint                color_index)
Michael Natterer's avatar
Michael Natterer committed
381
{
382 383 384 385
  GimpCurvesTool       *tool = GIMP_CURVES_TOOL (color_tool);
  GimpDrawable         *drawable;
  guchar                r, g, b, a;
  GimpHistogramChannel  channel;
Michael Natterer's avatar
Michael Natterer committed
386

387
  drawable = GIMP_IMAGE_MAP_TOOL (tool)->drawable;
388

389
  gimp_rgba_get_uchar (color, &r, &g, &b, &a);
390

391 392 393
  tool->col_value[GIMP_HISTOGRAM_RED]   = r;
  tool->col_value[GIMP_HISTOGRAM_GREEN] = g;
  tool->col_value[GIMP_HISTOGRAM_BLUE]  = b;
394

395
  if (gimp_drawable_has_alpha (drawable))
396
    tool->col_value[GIMP_HISTOGRAM_ALPHA] = a;
397

398
  if (gimp_drawable_is_indexed (drawable))
399
    tool->col_value[GIMP_HISTOGRAM_ALPHA] = color_index;
400

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

403 404 405 406 407 408 409
  if (tool->color)
    channel = tool->channel;
  else
    channel = (tool->channel == 1) ? GIMP_HISTOGRAM_ALPHA : GIMP_HISTOGRAM_VALUE;

  gimp_histogram_view_set_xpos (GIMP_HISTOGRAM_VIEW (tool->graph),
                                tool->col_value[channel]);
410
}
BST 1999 Andy Thomas's avatar
BST 1999 Andy Thomas committed
411

412 413 414 415
static gboolean
curves_key_press (GimpCurvesTool *tool,
                  GdkEventKey    *kevent)
{
416 417 418
  GimpCurve *curve  = tool->curve[tool->channel];
  gint       i      = tool->selected;
  gint       y      = curve->points[i][1];
419

420
  if (tool->grabbed || curve->curve_type == GIMP_CURVE_FREE)
421 422 423 424 425
    return FALSE;

  switch (kevent->keyval)
    {
    case GDK_Left:
426
      for (i = i - 1; i >= 0; i--)
427
        {
428
          if (curve->points[i][0] != -1)
429 430
            {
              tool->selected = i;
431 432 433

              gimp_histogram_view_set_selected (GIMP_HISTOGRAM_VIEW (tool->graph),
                                                i);
434 435

              return TRUE;
436 437 438 439 440
            }
        }
      break;

    case GDK_Right:
441
      for (i = i + 1; i < GIMP_CURVE_NUM_POINTS; i++)
442
        {
443
          if (curve->points[i][0] != -1)
444 445
            {
              tool->selected = i;
446 447 448

              gimp_histogram_view_set_selected (GIMP_HISTOGRAM_VIEW (tool->graph),
                                                i);
449 450

              return TRUE;
451 452 453 454 455 456 457 458
            }
        }
      break;

    case GDK_Up:
      if (y < 255)
        {
          y = y + (kevent->state & GDK_SHIFT_MASK ? 16 : 1);
459 460

          gimp_curve_move_point (curve, i, CLAMP0255 (y));
461 462 463 464

          gimp_image_map_tool_preview (GIMP_IMAGE_MAP_TOOL (tool));

          return TRUE;
465 466 467 468 469 470 471
        }
      break;

    case GDK_Down:
      if (y > 0)
        {
          y = y - (kevent->state & GDK_SHIFT_MASK ? 16 : 1);
472 473

          gimp_curve_move_point (curve, i, CLAMP0255 (y));
474 475 476 477

          gimp_image_map_tool_preview (GIMP_IMAGE_MAP_TOOL (tool));

          return TRUE;
478 479 480 481 482 483 484
        }
      break;

    default:
      break;
    }

485
  return FALSE;
486 487
}

488 489
static void
gimp_curves_tool_map (GimpImageMapTool *image_map_tool)
Elliot Lee's avatar
Elliot Lee committed
490
{
491
  GimpCurvesTool *tool = GIMP_CURVES_TOOL (image_map_tool);
492 493 494 495 496
  Curves          curves;
  gint            i;

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

498
  gimp_lut_setup (tool->lut,
499
                  (GimpLutFunc) curves_lut_func,
500
                  &curves,
501
                  gimp_drawable_bytes (image_map_tool->drawable));
502

503 504 505
  gimp_image_map_apply (image_map_tool->image_map,
                        (GimpImageMapApplyFunc) gimp_lut_process,
                        tool->lut);
Elliot Lee's avatar
Elliot Lee committed
506 507
}

508

509 510 511
/*******************/
/*  Curves dialog  */
/*******************/
Elliot Lee's avatar
Elliot Lee committed
512

513 514
static void
gimp_curves_tool_dialog (GimpImageMapTool *image_map_tool)
Elliot Lee's avatar
Elliot Lee committed
515
{
516 517
  GimpCurvesTool  *tool         = GIMP_CURVES_TOOL (image_map_tool);
  GimpToolOptions *tool_options = GIMP_TOOL_GET_OPTIONS (image_map_tool);
518
  GtkListStore    *store;
519
  GtkWidget       *vbox;
520 521 522 523 524
  GtkWidget       *vbox2;
  GtkWidget       *hbox;
  GtkWidget       *hbox2;
  GtkWidget       *label;
  GtkWidget       *bbox;
525 526 527 528
  GtkWidget       *frame;
  GtkWidget       *menu;
  GtkWidget       *table;
  GtkWidget       *button;
529
  GtkWidget       *bar;
530
  gint             padding;
531

532
  vbox = image_map_tool->main_vbox;
Elliot Lee's avatar
Elliot Lee committed
533

534
  /*  The option menu for selecting channels  */
535
  hbox = gtk_hbox_new (FALSE, 6);
536 537 538
  gtk_box_pack_start (GTK_BOX (vbox), hbox, FALSE, FALSE, 0);
  gtk_widget_show (hbox);

539
  label = gtk_label_new_with_mnemonic (_("Cha_nnel:"));
540 541 542
  gtk_box_pack_start (GTK_BOX (hbox), label, FALSE, FALSE, 0);
  gtk_widget_show (label);

543 544 545
  store = gimp_enum_store_new_with_range (GIMP_TYPE_HISTOGRAM_CHANNEL,
                                          GIMP_HISTOGRAM_VALUE,
                                          GIMP_HISTOGRAM_ALPHA);
546
  menu = gimp_enum_combo_box_new_with_model (GIMP_ENUM_STORE (store));
547 548
  g_object_unref (store);

549 550 551 552 553
  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");
554 555 556
  gtk_box_pack_start (GTK_BOX (hbox), menu, FALSE, FALSE, 0);
  gtk_widget_show (menu);

557
  tool->channel_menu = menu;
558

559 560
  gtk_label_set_mnemonic_widget (GTK_LABEL (label), menu);

561
  button = gtk_button_new_with_mnemonic (_("R_eset Channel"));
562
  gtk_box_pack_start (GTK_BOX (hbox), button, FALSE, FALSE, 0);
563
  gtk_widget_show (button);
564

565
  g_signal_connect (button, "clicked",
566
                    G_CALLBACK (curves_channel_reset_callback),
567
                    tool);
Elliot Lee's avatar
Elliot Lee committed
568

569 570 571 572 573
  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);
574

575
  /*  The table for the color bars and the graph  */
Elliot Lee's avatar
Elliot Lee committed
576
  table = gtk_table_new (2, 2, FALSE);
577 578
  gtk_table_set_col_spacings (GTK_TABLE (table), 2);
  gtk_table_set_row_spacings (GTK_TABLE (table), 2);
579 580 581 582 583
  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,
584
                    GTK_FILL, GTK_EXPAND | GTK_FILL, 0, 0);
585
  gtk_widget_show (vbox2);
Elliot Lee's avatar
Elliot Lee committed
586 587

  frame = gtk_frame_new (NULL);
588
  gtk_frame_set_shadow_type (GTK_FRAME (frame), GTK_SHADOW_IN);
589
  gtk_box_pack_start (GTK_BOX (vbox2), frame, TRUE, TRUE, RADIUS);
Elliot Lee's avatar
Elliot Lee committed
590 591
  gtk_widget_show (frame);

592 593 594 595
  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);
596

Elliot Lee's avatar
Elliot Lee committed
597 598
  /*  The curves graph  */
  frame = gtk_frame_new (NULL);
599
  gtk_frame_set_shadow_type (GTK_FRAME (frame), GTK_SHADOW_IN);
Elliot Lee's avatar
Elliot Lee committed
600
  gtk_table_attach (GTK_TABLE (table), frame, 1, 2, 0, 1,
601
                    GTK_EXPAND | GTK_FILL, GTK_EXPAND | GTK_FILL, 0, 0);
602
  gtk_widget_show (frame);
Elliot Lee's avatar
Elliot Lee committed
603

604 605 606 607
  tool->graph = gimp_histogram_view_new (FALSE);
  gtk_widget_set_size_request (tool->graph,
                               GRAPH_SIZE + RADIUS * 2,
                               GRAPH_SIZE + RADIUS * 2);
608
  GTK_WIDGET_SET_FLAGS (tool->graph, GTK_CAN_FOCUS);
609 610 611
  gtk_widget_add_events (tool->graph, (GDK_BUTTON_PRESS_MASK   |
                                       GDK_BUTTON_RELEASE_MASK |
                                       GDK_POINTER_MOTION_MASK |
612
                                       GDK_KEY_PRESS_MASK      |
613 614
                                       GDK_LEAVE_NOTIFY_MASK));
  g_object_set (tool->graph,
615 616 617
                "border-width", RADIUS,
                "subdivisions", 1,
                NULL);
618 619
  gtk_container_add (GTK_CONTAINER (frame), tool->graph);
  gtk_widget_show (tool->graph);
620

621
  g_signal_connect (tool->graph, "event",
622 623
                    G_CALLBACK (curves_graph_events),
                    tool);
624 625

  gimp_histogram_options_connect_view (GIMP_HISTOGRAM_OPTIONS (tool_options),
626 627 628 629 630
                                       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,
631
                    GTK_EXPAND | GTK_FILL, GTK_FILL, 0, 0);
632
  gtk_widget_show (hbox2);
633

Elliot Lee's avatar
Elliot Lee committed
634
  frame = gtk_frame_new (NULL);
635
  gtk_frame_set_shadow_type (GTK_FRAME (frame), GTK_SHADOW_IN);
636
  gtk_box_pack_start (GTK_BOX (hbox2), frame, TRUE, TRUE, RADIUS);
Elliot Lee's avatar
Elliot Lee committed
637 638
  gtk_widget_show (frame);

639 640 641 642
  vbox2 = gtk_vbox_new (TRUE, 0);
  gtk_container_add (GTK_CONTAINER (frame), vbox2);
  gtk_widget_show (vbox2);

643 644 645 646
  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);
647

648 649 650 651
  bar = gimp_color_bar_new (GTK_ORIENTATION_HORIZONTAL);
  gtk_box_pack_start (GTK_BOX (vbox2), bar, TRUE, TRUE, 0);
  gtk_widget_show (bar);

652
  gtk_widget_show (table);
Elliot Lee's avatar
Elliot Lee committed
653

654

655 656 657
  hbox = gtk_hbox_new (FALSE, 6);
  gtk_box_pack_end (GTK_BOX (vbox), hbox, FALSE, FALSE, 0);
  gtk_widget_show (hbox);
658

659
  /*  Horizontal button box for load / save */
660
  frame = gimp_frame_new (_("All Channels"));
661
  gtk_box_pack_start (GTK_BOX (hbox), frame, FALSE, FALSE, 0);
662 663
  gtk_widget_show (frame);

664 665
  bbox = gtk_hbutton_box_new ();
  gtk_box_set_spacing (GTK_BOX (bbox), 4);
666
  gtk_container_add (GTK_CONTAINER (frame), bbox);
667
  gtk_widget_show (bbox);
668

669 670 671
  gtk_box_pack_start (GTK_BOX (bbox), image_map_tool->load_button,
                      FALSE, FALSE, 0);
  gtk_widget_show (image_map_tool->load_button);
672

673 674 675
  gtk_box_pack_start (GTK_BOX (bbox), image_map_tool->save_button,
                      FALSE, FALSE, 0);
  gtk_widget_show (image_map_tool->save_button);
676 677

  /*  The radio box for selecting the curve type  */
678
  frame = gimp_frame_new (_("Curve Type"));
679 680 681 682
  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,
683 684 685 686
                                  "gimp-curve", GTK_ICON_SIZE_MENU,
                                  G_CALLBACK (curves_curve_type_callback),
                                  tool,
                                  &tool->curve_type);
687

688
  gtk_widget_style_get (bbox, "child-internal-pad-x", &padding, NULL);
689 690

  gimp_enum_stock_box_set_child_padding (hbox, padding, -1);
691 692 693 694 695

  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
696 697
}

698
static void
699
gimp_curves_tool_reset (GimpImageMapTool *image_map_tool)
700
{
701
  GimpCurvesTool       *tool = GIMP_CURVES_TOOL (image_map_tool);
702
  GimpHistogramChannel  channel;
703

704
  tool->grabbed = FALSE;
705

706 707 708
  for (channel =  GIMP_HISTOGRAM_VALUE;
       channel <= GIMP_HISTOGRAM_ALPHA;
       channel++)
709 710 711
    {
      gimp_curve_reset (tool->curve[channel], FALSE);
    }
712 713
}

714
static gboolean
715 716 717
gimp_curves_tool_settings_load (GimpImageMapTool  *image_map_tool,
                                gpointer           fp,
                                GError           **error)
718
{
719 720
  GimpCurvesTool *tool = GIMP_CURVES_TOOL (image_map_tool);
  FILE           *file = fp;
721 722 723
  gint            i, j;
  gint            fields;
  gchar           buf[50];
724 725
  gint            index[5][GIMP_CURVE_NUM_POINTS];
  gint            value[5][GIMP_CURVE_NUM_POINTS];
726

727 728 729 730
  if (! fgets (buf, sizeof (buf), file) ||
      strcmp (buf, "# GIMP Curves File\n") != 0)
    {
      g_set_error (error, GIMP_CONFIG_ERROR, GIMP_CONFIG_ERROR_PARSE,
731
                   _("not a GIMP Curves file"));
732 733
      return FALSE;
    }
734 735 736

  for (i = 0; i < 5; i++)
    {
737
      for (j = 0; j < GIMP_CURVE_NUM_POINTS; j++)
738 739 740 741
        {
          fields = fscanf (file, "%d %d ", &index[i][j], &value[i][j]);
          if (fields != 2)
            {
742
              /*  FIXME: should have a helpful error message here  */
743
              g_printerr ("fields != 2");
744 745
              g_set_error (error, GIMP_CONFIG_ERROR, GIMP_CONFIG_ERROR_PARSE,
                           _("parse error"));
746 747 748
              return FALSE;
            }
        }
749 750 751 752
    }

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

755 756 757
      curve->curve_type = GIMP_CURVE_SMOOTH;

      for (j = 0; j < GIMP_CURVE_NUM_POINTS; j++)
758
        {
759 760
          curve->points[j][0] = index[i][j];
          curve->points[j][1] = value[i][j];
761
        }
762

763 764
      gimp_curve_calculate (curve);
    }
765 766

  gimp_int_radio_group_set_active (GTK_RADIO_BUTTON (tool->curve_type),
767
                                   GIMP_CURVE_SMOOTH);
768 769

  return TRUE;
770 771 772
}

static gboolean
773 774
gimp_curves_tool_settings_save (GimpImageMapTool *image_map_tool,
                                gpointer          fp)
775
{
776 777
  GimpCurvesTool *tool = GIMP_CURVES_TOOL (image_map_tool);
  FILE           *file = fp;
778 779 780 781 782 783 784
  gint            i, j;
  gint32          index;

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

  for (i = 0; i < 5; i++)
    {
785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801
      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++)
802
        fprintf (file, "%d %d ",
803 804
                 curve->points[j][0],
                 curve->points[j][1]);
805 806 807 808 809

      fprintf (file, "\n");
    }

  return TRUE;
810
}
811

Elliot Lee's avatar
Elliot Lee committed
812
static void
813 814 815
curves_curve_callback (GimpCurve        *curve,
                       const GParamSpec *pspec,
                       GimpCurvesTool   *tool)
Elliot Lee's avatar
Elliot Lee committed
816
{
817
  if (curve == tool->curve[tool->channel] && tool->xrange)
818
    {
819
      GimpHistogramChannel channel;
820

821 822 823 824 825
      if (tool->color)
        channel = tool->channel;
      else
        channel = ((tool->channel == 1) ?
                   GIMP_HISTOGRAM_ALPHA : GIMP_HISTOGRAM_VALUE);
826

827
      switch (channel)
828 829 830
        {
        case GIMP_HISTOGRAM_VALUE:
        case GIMP_HISTOGRAM_ALPHA:
831
        case GIMP_HISTOGRAM_RGB:
832
          gimp_color_bar_set_buffers (GIMP_COLOR_BAR (tool->xrange),
833 834 835
                                      tool->curve[tool->channel]->curve,
                                      tool->curve[tool->channel]->curve,
                                      tool->curve[tool->channel]->curve);
836
          break;
837

838 839 840
        case GIMP_HISTOGRAM_RED:
        case GIMP_HISTOGRAM_GREEN:
        case GIMP_HISTOGRAM_BLUE:
841
          gimp_color_bar_set_buffers (GIMP_COLOR_BAR (tool->xrange),
842 843 844
                                      tool->curve[GIMP_HISTOGRAM_RED]->curve,
                                      tool->curve[GIMP_HISTOGRAM_GREEN]->curve,
                                      tool->curve[GIMP_HISTOGRAM_BLUE]->curve);
845 846
          break;
        }
Elliot Lee's avatar
Elliot Lee committed
847 848 849 850
    }
}

static void
851
curves_channel_callback (GtkWidget      *widget,
852
                         GimpCurvesTool *tool)
Elliot Lee's avatar
Elliot Lee committed
853
{
854
  gint value;
855

856 857 858 859 860
  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);
861 862 863 864
      gimp_histogram_view_set_xpos (GIMP_HISTOGRAM_VIEW (tool->graph),
                                    tool->col_value[tool->channel]);

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

866 867 868
      /* FIXME: hack */
      if (! tool->color)
        tool->channel = (tool->channel == GIMP_HISTOGRAM_ALPHA) ? 1 : 0;
869