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

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
#include "core/gimp.h"
40
#include "core/gimpcurve.h"
Michael Natterer's avatar
Michael Natterer committed
41
#include "core/gimpdrawable.h"
42
#include "core/gimpdrawable-histogram.h"
Michael Natterer's avatar
Michael Natterer committed
43
#include "core/gimpimage.h"
44
#include "core/gimpimagemap.h"
Michael Natterer's avatar
Michael Natterer committed
45

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

50 51
#include "display/gimpdisplay.h"

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

55
#include "gimp-intl.h"
56

57

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


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

65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98
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);

99 100
static void     curves_curve_callback           (GimpCurve            *curve,
                                                 GimpCurvesTool       *tool);
101 102 103 104 105 106 107 108 109 110
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);
111

Michael Natterer's avatar
Michael Natterer committed
112

113
G_DEFINE_TYPE (GimpCurvesTool, gimp_curves_tool, GIMP_TYPE_IMAGE_MAP_TOOL)
114 115

#define parent_class gimp_curves_tool_parent_class
116

Michael Natterer's avatar
Michael Natterer committed
117

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

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

137 138 139

/*  private functions  */

Michael Natterer's avatar
Michael Natterer committed
140 141 142
static void
gimp_curves_tool_class_init (GimpCurvesToolClass *klass)
{
143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168
  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
169 170 171
}

static void
172
gimp_curves_tool_init (GimpCurvesTool *tool)
Michael Natterer's avatar
Michael Natterer committed
173
{
174 175
  GimpImageMapTool *im_tool = GIMP_IMAGE_MAP_TOOL (tool);
  gint              i;
176

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

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

186 187
  tool->lut     = gimp_lut_new ();
  tool->channel = GIMP_HISTOGRAM_VALUE;
188

189 190
  for (i = 0; i < G_N_ELEMENTS (tool->col_value); i++)
    tool->col_value[i] = -1;
191 192 193

  im_tool->apply_func = (GimpImageMapApplyFunc) gimp_lut_process;
  im_tool->apply_data = tool->lut;
Michael Natterer's avatar
Michael Natterer committed
194 195 196
}

static void
197
gimp_curves_tool_finalize (GObject *object)
Michael Natterer's avatar
Michael Natterer committed
198
{
199
  GimpCurvesTool *tool = GIMP_CURVES_TOOL (object);
200 201 202 203
  gint            i;

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

205 206
  gimp_lut_free (tool->lut);

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

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

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

  if (! drawable)
226
    return FALSE;
227

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

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

238
  if (! c_tool->hist)
239
    c_tool->hist = gimp_histogram_new ();
240

241
  c_tool->channel = GIMP_HISTOGRAM_VALUE;
242 243
  c_tool->color   = gimp_drawable_is_rgb (drawable);
  c_tool->alpha   = gimp_drawable_has_alpha (drawable);
244

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

247 248
  /*  always pick colors  */
  gimp_color_tool_enable (GIMP_COLOR_TOOL (tool),
249
                          GIMP_COLOR_TOOL_GET_OPTIONS (tool));
250

251 252
  gimp_int_combo_box_set_sensitivity (GIMP_INT_COMBO_BOX (c_tool->channel_menu),
                                      curves_menu_sensitivity, c_tool, NULL);
253

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

257 258 259
  /* FIXME: hack */
  if (! c_tool->color)
    c_tool->channel = (c_tool->channel == GIMP_HISTOGRAM_ALPHA) ? 1 : 0;
260

261
  gimp_drawable_calculate_histogram (drawable, c_tool->hist);
262 263
  gimp_histogram_view_set_background (GIMP_HISTOGRAM_VIEW (c_tool->graph),
                                      c_tool->hist);
264 265
  gimp_curve_view_set_curve (GIMP_CURVE_VIEW (c_tool->graph),
                             c_tool->curve[c_tool->channel]);
266

267
  return TRUE;
Michael Natterer's avatar
Michael Natterer committed
268 269 270
}

static void
271 272 273 274 275 276
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
277
{
278
  GimpCurvesTool *c_tool = GIMP_CURVES_TOOL (tool);
Michael Natterer's avatar
Michael Natterer committed
279

280
  if (state & GDK_SHIFT_MASK)
Michael Natterer's avatar
Michael Natterer committed
281
    {
282
      GimpCurve *curve = c_tool->curve[c_tool->channel];
283
      gint       closest;
284

285
      closest =
286 287 288
        gimp_curve_get_closest_point (curve,
                                      c_tool->col_value[c_tool->channel]);

289
      gimp_curve_view_set_selected (GIMP_CURVE_VIEW (c_tool->graph),
290
                                    closest);
291

292
      gimp_curve_set_point (curve,
293
                            closest,
294 295
                            c_tool->col_value[c_tool->channel],
                            curve->curve[c_tool->col_value[c_tool->channel]]);
Michael Natterer's avatar
Michael Natterer committed
296
    }
297
  else if (state & GDK_CONTROL_MASK)
Michael Natterer's avatar
Michael Natterer committed
298
    {
299
      gint i;
300

301 302
      for (i = 0; i < 5; i++)
        {
303
          GimpCurve *curve = c_tool->curve[i];
304
          gint       closest;
305

306
          closest =
307 308 309
            gimp_curve_get_closest_point (curve,
                                          c_tool->col_value[i]);

310
          gimp_curve_view_set_selected (GIMP_CURVE_VIEW (c_tool->graph),
311
                                        closest);
312

313
          gimp_curve_set_point (curve,
314
                                closest,
315 316
                                c_tool->col_value[i],
                                curve->curve[c_tool->col_value[i]]);
317
        }
Michael Natterer's avatar
Michael Natterer committed
318 319
    }

320
  /*  chain up to halt the tool */
321 322
  GIMP_TOOL_CLASS (parent_class)->button_release (tool, coords, time, state,
                                                  release_type, display);
Michael Natterer's avatar
Michael Natterer committed
323 324
}

325 326 327
gboolean
gimp_curves_tool_key_press (GimpTool    *tool,
                            GdkEventKey *kevent,
328
                            GimpDisplay *display)
329
{
330 331 332
  GimpCurvesTool *c_tool = GIMP_CURVES_TOOL (tool);

  return gtk_widget_event (c_tool->graph, (GdkEvent *) kevent);
333 334 335 336 337 338
}

static void
gimp_curves_tool_oper_update (GimpTool        *tool,
                              GimpCoords      *coords,
                              GdkModifierType  state,
339
                              gboolean         proximity,
340
                              GimpDisplay     *display)
341 342 343 344
{
  GimpColorPickMode  mode   = GIMP_COLOR_PICK_MODE_NONE;
  const gchar       *status = NULL;

345
  GIMP_TOOL_CLASS (parent_class)->oper_update (tool, coords, state, proximity,
346
                                               display);
347

348
  gimp_tool_pop_status (tool, display);
349 350 351 352

  if (state & GDK_SHIFT_MASK)
    {
      mode   = GIMP_COLOR_PICK_MODE_PALETTE;
353
      status = _("Click to add a control point");
354 355 356 357
    }
  else if (state & GDK_CONTROL_MASK)
    {
      mode   = GIMP_COLOR_PICK_MODE_PALETTE;
358
      status = _("Click to add control points to all channels");
359 360 361 362
    }

  GIMP_COLOR_TOOL (tool)->pick_mode = mode;

363
  if (status && proximity)
364
    gimp_tool_push_status (tool, display, status);
365 366
}

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

379
  drawable = GIMP_IMAGE_MAP_TOOL (tool)->drawable;
380

381
  gimp_rgba_get_uchar (color, &r, &g, &b, &a);
382

383 384 385
  tool->col_value[GIMP_HISTOGRAM_RED]   = r;
  tool->col_value[GIMP_HISTOGRAM_GREEN] = g;
  tool->col_value[GIMP_HISTOGRAM_BLUE]  = b;
386

387
  if (gimp_drawable_has_alpha (drawable))
388
    tool->col_value[GIMP_HISTOGRAM_ALPHA] = a;
389

390
  if (gimp_drawable_is_indexed (drawable))
391
    tool->col_value[GIMP_HISTOGRAM_ALPHA] = color_index;
392

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

395 396 397 398 399
  if (tool->color)
    channel = tool->channel;
  else
    channel = (tool->channel == 1) ? GIMP_HISTOGRAM_ALPHA : GIMP_HISTOGRAM_VALUE;

400 401
  gimp_curve_view_set_xpos (GIMP_CURVE_VIEW (tool->graph),
                            tool->col_value[channel]);
402
}
BST 1999 Andy Thomas's avatar
BST 1999 Andy Thomas committed
403

404 405
static void
gimp_curves_tool_map (GimpImageMapTool *image_map_tool)
Elliot Lee's avatar
Elliot Lee committed
406
{
407
  GimpCurvesTool *tool = GIMP_CURVES_TOOL (image_map_tool);
408 409 410 411 412
  Curves          curves;
  gint            i;

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

414
  gimp_lut_setup (tool->lut,
415
                  (GimpLutFunc) curves_lut_func,
416
                  &curves,
417
                  gimp_drawable_bytes (image_map_tool->drawable));
Elliot Lee's avatar
Elliot Lee committed
418 419
}

420

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

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

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

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

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

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

461 462 463 464 465
  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");
466 467 468
  gtk_box_pack_start (GTK_BOX (hbox), menu, FALSE, FALSE, 0);
  gtk_widget_show (menu);

469
  tool->channel_menu = menu;
470

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

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

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

481 482 483 484 485
  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);
486

487
  /*  The table for the color bars and the graph  */
Elliot Lee's avatar
Elliot Lee committed
488
  table = gtk_table_new (2, 2, FALSE);
489 490
  gtk_table_set_col_spacings (GTK_TABLE (table), 2);
  gtk_table_set_row_spacings (GTK_TABLE (table), 2);
491 492 493 494 495
  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,
496
                    GTK_FILL, GTK_EXPAND | GTK_FILL, 0, 0);
497
  gtk_widget_show (vbox2);
Elliot Lee's avatar
Elliot Lee committed
498 499

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

504 505 506 507
  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);
508

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

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

527
  gimp_histogram_options_connect_view (GIMP_HISTOGRAM_OPTIONS (tool_options),
528 529 530 531 532
                                       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,
533
                    GTK_EXPAND | GTK_FILL, GTK_FILL, 0, 0);
534
  gtk_widget_show (hbox2);
535

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

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

545 546 547 548
  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);
549

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

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

556

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

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

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

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

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

  /*  The radio box for selecting the curve type  */
580
  frame = gimp_frame_new (_("Curve Type"));
581 582 583 584
  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,
585 586 587 588
                                  "gimp-curve", GTK_ICON_SIZE_MENU,
                                  G_CALLBACK (curves_curve_type_callback),
                                  tool,
                                  &tool->curve_type);
589

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

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

  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
598 599
}

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

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

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

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

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

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

655 656 657
      gimp_data_freeze (GIMP_DATA (curve));

      gimp_curve_set_curve_type (curve, GIMP_CURVE_SMOOTH);
658 659

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

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

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

  return TRUE;
669 670 671
}

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

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

  for (i = 0; i < 5; i++)
    {
684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700
      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++)
701
        fprintf (file, "%d %d ",
702 703
                 curve->points[j][0],
                 curve->points[j][1]);
704 705 706 707 708

      fprintf (file, "\n");
    }

  return TRUE;
709
}
710

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

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

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

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

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

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

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

760 761 762 763 764
  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);
765 766
      gimp_curve_view_set_xpos (GIMP_CURVE_VIEW (tool->graph),
                                tool->col_value[tool->channel]);
767 768

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

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

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

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

780
      curves_curve_callback (tool->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_curve_reset (tool->curve[tool->channel], FALSE);
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;