gimpcurvestool.c 27.4 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

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

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 "base/curves.h"
Michael Natterer's avatar
Michael Natterer committed
36 37 38
#include "base/gimphistogram.h"
#include "base/gimplut.h"

39 40 41
#include "gegl/gimpcurvesconfig.h"
#include "gegl/gimpoperationcurves.h"

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

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

53 54
#include "display/gimpdisplay.h"

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

58
#include "gimp-intl.h"
59

60

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


Michael Natterer's avatar
Michael Natterer committed
66 67
/*  local function prototypes  */

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
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 GeglNode * gimp_curves_tool_get_operation  (GimpImageMapTool     *image_map_tool);
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 void       curves_curve_callback           (GimpCurve            *curve,
                                                   GimpCurvesTool       *tool);
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);
115

Michael Natterer's avatar
Michael Natterer committed
116

117
G_DEFINE_TYPE (GimpCurvesTool, gimp_curves_tool, GIMP_TYPE_IMAGE_MAP_TOOL)
118 119

#define parent_class gimp_curves_tool_parent_class
120

Michael Natterer's avatar
Michael Natterer committed
121

122
/*  public functions  */
Michael Natterer's avatar
Michael Natterer committed
123 124

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

141 142 143

/*  private functions  */

Michael Natterer's avatar
Michael Natterer committed
144 145 146
static void
gimp_curves_tool_class_init (GimpCurvesToolClass *klass)
{
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");

168
  im_tool_class->get_operation     = gimp_curves_tool_get_operation;
169 170 171 172 173
  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
174 175 176
}

static void
177
gimp_curves_tool_init (GimpCurvesTool *tool)
Michael Natterer's avatar
Michael Natterer committed
178
{
179 180
  GimpImageMapTool *im_tool = GIMP_IMAGE_MAP_TOOL (tool);
  gint              i;
181

182 183
  tool->lut     = gimp_lut_new ();
  tool->channel = GIMP_HISTOGRAM_VALUE;
184

185 186
  for (i = 0; i < G_N_ELEMENTS (tool->col_value); i++)
    tool->col_value[i] = -1;
187 188 189

  im_tool->apply_func = (GimpImageMapApplyFunc) gimp_lut_process;
  im_tool->apply_data = tool->lut;
Michael Natterer's avatar
Michael Natterer committed
190 191 192
}

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

197 198 199 200 201
  if (tool->config)
    {
      g_object_unref (tool->config);
      tool->config = NULL;
    }
202

203 204
  gimp_lut_free (tool->lut);

205
  if (tool->hist)
206
    {
207 208
      gimp_histogram_free (tool->hist);
      tool->hist = NULL;
209
    }
210

211
  G_OBJECT_CLASS (parent_class)->finalize (object);
Michael Natterer's avatar
Michael Natterer committed
212 213
}

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

  if (! drawable)
224
    return FALSE;
225

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

233 234
  for (i = 0; i < G_N_ELEMENTS (c_tool->config->curve); i++)
    gimp_curve_reset (c_tool->config->curve[i], TRUE);
235

236
  if (! c_tool->hist)
237
    c_tool->hist = gimp_histogram_new ();
238

239
  c_tool->channel = c_tool->config->channel;
240 241
  c_tool->color   = gimp_drawable_is_rgb (drawable);
  c_tool->alpha   = gimp_drawable_has_alpha (drawable);
242

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

245 246
  /*  always pick colors  */
  gimp_color_tool_enable (GIMP_COLOR_TOOL (tool),
247
                          GIMP_COLOR_TOOL_GET_OPTIONS (tool));
248

249 250
  gimp_int_combo_box_set_sensitivity (GIMP_INT_COMBO_BOX (c_tool->channel_menu),
                                      curves_menu_sensitivity, c_tool, NULL);
251

252 253
  gimp_int_combo_box_set_active (GIMP_INT_COMBO_BOX (c_tool->channel_menu),
                                 c_tool->channel);
Michael Natterer's avatar
Michael Natterer committed
254

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

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

static void
265 266 267 268 269 270
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
271
{
272
  GimpCurvesTool *c_tool = GIMP_CURVES_TOOL (tool);
Michael Natterer's avatar
Michael Natterer committed
273

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

  GIMP_COLOR_TOOL (tool)->pick_mode = mode;

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

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

372
  drawable = GIMP_IMAGE_MAP_TOOL (tool)->drawable;
373

374
  gimp_rgba_get_uchar (color, &r, &g, &b, &a);
375

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

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

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

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

388
  gimp_curve_view_set_xpos (GIMP_CURVE_VIEW (tool->graph),
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
                            tool->col_value[tool->channel]);
}

static GeglNode *
gimp_curves_tool_get_operation (GimpImageMapTool *image_map_tool)
{
  GimpCurvesTool *tool = GIMP_CURVES_TOOL (image_map_tool);
  GeglNode       *node;
  gint            i;

  node = g_object_new (GEGL_TYPE_NODE,
                       "operation", "gimp-curves",
                       NULL);

  tool->config = g_object_new (GIMP_TYPE_CURVES_CONFIG, NULL);

  for (i = 0; i < G_N_ELEMENTS (tool->config->curve); i++)
    {
      g_signal_connect_object (tool->config->curve[i], "dirty",
                               G_CALLBACK (curves_curve_callback),
                               tool, 0);
    }

  gegl_node_set (node,
                 "config", tool->config,
                 NULL);

  return node;
417
}
BST 1999 Andy Thomas's avatar
BST 1999 Andy Thomas committed
418

419 420
static void
gimp_curves_tool_map (GimpImageMapTool *image_map_tool)
Elliot Lee's avatar
Elliot Lee committed
421
{
422
  GimpCurvesTool *tool = GIMP_CURVES_TOOL (image_map_tool);
423 424
  Curves          curves;

425
  gimp_curves_config_to_cruft (tool->config, &curves, tool->color);
426

427
  gimp_lut_setup (tool->lut,
428
                  (GimpLutFunc) curves_lut_func,
429
                  &curves,
430
                  gimp_drawable_bytes (image_map_tool->drawable));
Elliot Lee's avatar
Elliot Lee committed
431 432
}

433

434 435 436
/*******************/
/*  Curves dialog  */
/*******************/
Elliot Lee's avatar
Elliot Lee committed
437

438 439
static void
gimp_curves_tool_dialog (GimpImageMapTool *image_map_tool)
Elliot Lee's avatar
Elliot Lee committed
440
{
441 442
  GimpCurvesTool  *tool         = GIMP_CURVES_TOOL (image_map_tool);
  GimpToolOptions *tool_options = GIMP_TOOL_GET_OPTIONS (image_map_tool);
443
  GtkListStore    *store;
444
  GtkWidget       *vbox;
445 446 447 448 449
  GtkWidget       *vbox2;
  GtkWidget       *hbox;
  GtkWidget       *hbox2;
  GtkWidget       *label;
  GtkWidget       *bbox;
450 451 452 453
  GtkWidget       *frame;
  GtkWidget       *menu;
  GtkWidget       *table;
  GtkWidget       *button;
454
  GtkWidget       *bar;
455
  gint             padding;
456

457
  vbox = image_map_tool->main_vbox;
Elliot Lee's avatar
Elliot Lee committed
458

459
  /*  The option menu for selecting channels  */
460
  hbox = gtk_hbox_new (FALSE, 6);
461 462 463
  gtk_box_pack_start (GTK_BOX (vbox), hbox, FALSE, FALSE, 0);
  gtk_widget_show (hbox);

464
  label = gtk_label_new_with_mnemonic (_("Cha_nnel:"));
465 466 467
  gtk_box_pack_start (GTK_BOX (hbox), label, FALSE, FALSE, 0);
  gtk_widget_show (label);

468 469 470
  store = gimp_enum_store_new_with_range (GIMP_TYPE_HISTOGRAM_CHANNEL,
                                          GIMP_HISTOGRAM_VALUE,
                                          GIMP_HISTOGRAM_ALPHA);
471
  menu = gimp_enum_combo_box_new_with_model (GIMP_ENUM_STORE (store));
472 473
  g_object_unref (store);

474 475 476 477 478
  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");
479 480 481
  gtk_box_pack_start (GTK_BOX (hbox), menu, FALSE, FALSE, 0);
  gtk_widget_show (menu);

482
  tool->channel_menu = menu;
483

484 485
  gtk_label_set_mnemonic_widget (GTK_LABEL (label), menu);

486
  button = gtk_button_new_with_mnemonic (_("R_eset Channel"));
487
  gtk_box_pack_start (GTK_BOX (hbox), button, FALSE, FALSE, 0);
488
  gtk_widget_show (button);
489

490
  g_signal_connect (button, "clicked",
491
                    G_CALLBACK (curves_channel_reset_callback),
492
                    tool);
Elliot Lee's avatar
Elliot Lee committed
493

494 495 496 497 498
  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);
499

500
  /*  The table for the color bars and the graph  */
Elliot Lee's avatar
Elliot Lee committed
501
  table = gtk_table_new (2, 2, FALSE);
502 503
  gtk_table_set_col_spacings (GTK_TABLE (table), 2);
  gtk_table_set_row_spacings (GTK_TABLE (table), 2);
504 505 506 507 508
  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,
509
                    GTK_FILL, GTK_EXPAND | GTK_FILL, 0, 0);
510
  gtk_widget_show (vbox2);
Elliot Lee's avatar
Elliot Lee committed
511 512

  frame = gtk_frame_new (NULL);
513
  gtk_frame_set_shadow_type (GTK_FRAME (frame), GTK_SHADOW_IN);
514
  gtk_box_pack_start (GTK_BOX (vbox2), frame, TRUE, TRUE, RADIUS);
Elliot Lee's avatar
Elliot Lee committed
515 516
  gtk_widget_show (frame);

517 518 519 520
  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);
521

Elliot Lee's avatar
Elliot Lee committed
522 523
  /*  The curves graph  */
  frame = gtk_frame_new (NULL);
524
  gtk_frame_set_shadow_type (GTK_FRAME (frame), GTK_SHADOW_IN);
Elliot Lee's avatar
Elliot Lee committed
525
  gtk_table_attach (GTK_TABLE (table), frame, 1, 2, 0, 1,
526
                    GTK_EXPAND | GTK_FILL, GTK_EXPAND | GTK_FILL, 0, 0);
527
  gtk_widget_show (frame);
Elliot Lee's avatar
Elliot Lee committed
528

529
  tool->graph = gimp_curve_view_new ();
530 531 532 533
  gtk_widget_set_size_request (tool->graph,
                               GRAPH_SIZE + RADIUS * 2,
                               GRAPH_SIZE + RADIUS * 2);
  g_object_set (tool->graph,
534 535 536
                "border-width", RADIUS,
                "subdivisions", 1,
                NULL);
537 538
  gtk_container_add (GTK_CONTAINER (frame), tool->graph);
  gtk_widget_show (tool->graph);
539

540
  gimp_histogram_options_connect_view (GIMP_HISTOGRAM_OPTIONS (tool_options),
541 542 543 544 545
                                       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,
546
                    GTK_EXPAND | GTK_FILL, GTK_FILL, 0, 0);
547
  gtk_widget_show (hbox2);
548

Elliot Lee's avatar
Elliot Lee committed
549
  frame = gtk_frame_new (NULL);
550
  gtk_frame_set_shadow_type (GTK_FRAME (frame), GTK_SHADOW_IN);
551
  gtk_box_pack_start (GTK_BOX (hbox2), frame, TRUE, TRUE, RADIUS);
Elliot Lee's avatar
Elliot Lee committed
552 553
  gtk_widget_show (frame);

554 555 556 557
  vbox2 = gtk_vbox_new (TRUE, 0);
  gtk_container_add (GTK_CONTAINER (frame), vbox2);
  gtk_widget_show (vbox2);

558 559 560 561
  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);
562

563 564 565 566
  bar = gimp_color_bar_new (GTK_ORIENTATION_HORIZONTAL);
  gtk_box_pack_start (GTK_BOX (vbox2), bar, TRUE, TRUE, 0);
  gtk_widget_show (bar);

567
  gtk_widget_show (table);
Elliot Lee's avatar
Elliot Lee committed
568

569

570 571 572
  hbox = gtk_hbox_new (FALSE, 6);
  gtk_box_pack_end (GTK_BOX (vbox), hbox, FALSE, FALSE, 0);
  gtk_widget_show (hbox);
573

574
  /*  Horizontal button box for load / save */
575
  frame = gimp_frame_new (_("All Channels"));
576
  gtk_box_pack_start (GTK_BOX (hbox), frame, FALSE, FALSE, 0);
577 578
  gtk_widget_show (frame);

579 580
  bbox = gtk_hbutton_box_new ();
  gtk_box_set_spacing (GTK_BOX (bbox), 4);
581
  gtk_container_add (GTK_CONTAINER (frame), bbox);
582
  gtk_widget_show (bbox);
583

584 585 586
  gtk_box_pack_start (GTK_BOX (bbox), image_map_tool->load_button,
                      FALSE, FALSE, 0);
  gtk_widget_show (image_map_tool->load_button);
587

588 589 590
  gtk_box_pack_start (GTK_BOX (bbox), image_map_tool->save_button,
                      FALSE, FALSE, 0);
  gtk_widget_show (image_map_tool->save_button);
591 592

  /*  The radio box for selecting the curve type  */
593
  frame = gimp_frame_new (_("Curve Type"));
594 595 596 597
  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,
598 599 600 601
                                  "gimp-curve", GTK_ICON_SIZE_MENU,
                                  G_CALLBACK (curves_curve_type_callback),
                                  tool,
                                  &tool->curve_type);
602

603
  gtk_widget_style_get (bbox, "child-internal-pad-x", &padding, NULL);
604 605

  gimp_enum_stock_box_set_child_padding (hbox, padding, -1);
606 607 608 609 610

  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
611 612
}

613
static void
614
gimp_curves_tool_reset (GimpImageMapTool *image_map_tool)
615
{
616
  GimpCurvesTool *tool = GIMP_CURVES_TOOL (image_map_tool);
617

618
  gimp_curves_config_reset (tool->config);
619 620
}

621
static gboolean
622 623 624
gimp_curves_tool_settings_load (GimpImageMapTool  *image_map_tool,
                                gpointer           fp,
                                GError           **error)
625
{
626 627
  GimpCurvesTool *tool = GIMP_CURVES_TOOL (image_map_tool);
  FILE           *file = fp;
628 629 630
  gint            i, j;
  gint            fields;
  gchar           buf[50];
631 632
  gint            index[5][GIMP_CURVE_NUM_POINTS];
  gint            value[5][GIMP_CURVE_NUM_POINTS];
633

634 635 636 637
  if (! fgets (buf, sizeof (buf), file) ||
      strcmp (buf, "# GIMP Curves File\n") != 0)
    {
      g_set_error (error, GIMP_CONFIG_ERROR, GIMP_CONFIG_ERROR_PARSE,
638
                   _("not a GIMP Curves file"));
639 640
      return FALSE;
    }
641 642 643

  for (i = 0; i < 5; i++)
    {
644
      for (j = 0; j < GIMP_CURVE_NUM_POINTS; j++)
645 646 647 648
        {
          fields = fscanf (file, "%d %d ", &index[i][j], &value[i][j]);
          if (fields != 2)
            {
649
              /*  FIXME: should have a helpful error message here  */
650
              g_printerr ("fields != 2");
651 652
              g_set_error (error, GIMP_CONFIG_ERROR, GIMP_CONFIG_ERROR_PARSE,
                           _("parse error"));
653 654 655
              return FALSE;
            }
        }
656 657 658 659
    }

  for (i = 0; i < 5; i++)
    {
660
      GimpCurve *curve = tool->config->curve[i];
661

662 663 664
      gimp_data_freeze (GIMP_DATA (curve));

      gimp_curve_set_curve_type (curve, GIMP_CURVE_SMOOTH);
665 666

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

669
      gimp_data_thaw (GIMP_DATA (curve));
670
    }
671 672

  gimp_int_radio_group_set_active (GTK_RADIO_BUTTON (tool->curve_type),
673
                                   GIMP_CURVE_SMOOTH);
674 675

  return TRUE;
676 677 678
}

static gboolean
679 680
gimp_curves_tool_settings_save (GimpImageMapTool *image_map_tool,
                                gpointer          fp)
681
{
682 683
  GimpCurvesTool *tool = GIMP_CURVES_TOOL (image_map_tool);
  FILE           *file = fp;
684 685 686 687 688 689 690
  gint            i, j;
  gint32          index;

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

  for (i = 0; i < 5; i++)
    {
691
      GimpCurve *curve = tool->config->curve[i];
692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707

      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++)
708
        fprintf (file, "%d %d ",
709 710
                 curve->points[j][0],
                 curve->points[j][1]);
711 712 713 714 715

      fprintf (file, "\n");
    }

  return TRUE;
716
}
717

Elliot Lee's avatar
Elliot Lee committed
718
static void
719 720
curves_curve_callback (GimpCurve      *curve,
                       GimpCurvesTool *tool)
Elliot Lee's avatar
Elliot Lee committed
721
{
722
  if (curve != tool->config->curve[tool->channel])
723 724 725
    return;

  if (tool->xrange)
726
    {
727
      switch (tool->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->config->curve[tool->channel]->curve,
                                      tool->config->curve[tool->channel]->curve,
                                      tool->config->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->config->curve[GIMP_HISTOGRAM_RED]->curve,
                                      tool->config->curve[GIMP_HISTOGRAM_GREEN]->curve,
                                      tool->config->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
  if (gimp_int_combo_box_get_active (GIMP_INT_COMBO_BOX (widget), &value))
    {
      tool->channel = value;
762 763 764 765 766

      g_object_set (tool->config,
                    "channel", tool->channel,
                    NULL);

767 768
      gimp_histogram_view_set_channel (GIMP_HISTOGRAM_VIEW (tool->graph),
                                       tool->channel);
769 770
      gimp_curve_view_set_xpos (GIMP_CURVE_VIEW (tool->graph),
                                tool->col_value[tool->channel]);
771 772

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

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

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

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

static void
785
curves_channel_reset_callback (GtkWidget      *widget,
786
                               GimpCurvesTool *tool)
Elliot Lee's avatar
Elliot Lee committed
787
{
788
  gimp_curves_config_reset_channel (tool->config, tool->channel);
Elliot Lee's avatar
Elliot Lee committed
789 790
}

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

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

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

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

    case GIMP_HISTOGRAM_RGB:
      return FALSE;
813
    }
814

815 816 817
  return FALSE;
}

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

824
  gimp_radio_button_update (widget, &curve_type);
Elliot Lee's avatar
Elliot Lee committed
825

826
  if (tool->config->curve[tool->channel]->curve_type != curve_type)