gimpcurvestool.c 30.6 KB
Newer Older
1
/* GIMP - The GNU Image Manipulation Program
Elliot Lee's avatar
Elliot Lee committed
2 3
 * Copyright (C) 1995 Spencer Kimball and Peter Mattis
 *
4
 * This program is free software: you can redistribute it and/or modify
Elliot Lee's avatar
Elliot Lee committed
5
 * it under the terms of the GNU General Public License as published by
6
 * the Free Software Foundation; either version 3 of the License, or
Elliot Lee's avatar
Elliot Lee committed
7 8 9 10 11 12 13 14
 * (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
15
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
Elliot Lee's avatar
Elliot Lee committed
16
 */
Sven Neumann's avatar
Sven Neumann committed
17

18 19
#include "config.h"

20 21 22
#include <errno.h>

#include <glib/gstdio.h>
23
#include <gegl.h>
Sven Neumann's avatar
Sven Neumann committed
24 25
#include <gtk/gtk.h>

26
#include "libgimpbase/gimpbase.h"
27
#include "libgimpcolor/gimpcolor.h"
28
#include "libgimpconfig/gimpconfig.h"
29
#include "libgimpwidgets/gimpwidgets.h"
30

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

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

37 38 39
#include "gegl/gimpcurvesconfig.h"
#include "gegl/gimpoperationcurves.h"

40
#include "core/gimp.h"
41
#include "core/gimpcurve.h"
Sven Neumann's avatar
Sven Neumann committed
42
#include "core/gimpcurve-map.h"
Michael Natterer's avatar
Michael Natterer committed
43
#include "core/gimpdrawable.h"
44
#include "core/gimpdrawable-histogram.h"
45
#include "core/gimperror.h"
Michael Natterer's avatar
Michael Natterer committed
46 47
#include "core/gimpimage.h"

48
#include "widgets/gimpcolorbar.h"
49
#include "widgets/gimphelp-ids.h"
50
#include "widgets/gimpcurveview.h"
51
#include "widgets/gimpwidgets-utils.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
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,
74
                                                   const GimpCoords     *coords,
75 76 77 78 79 80 81 82
                                                   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,
83
                                                   const GimpCoords     *coords,
84 85 86 87 88 89 90
                                                   GdkModifierType       state,
                                                   gboolean              proximity,
                                                   GimpDisplay          *display);

static void       gimp_curves_tool_color_picked   (GimpColorTool        *color_tool,
                                                   GimpColorPickState    pick_state,
                                                   GimpImageType         sample_type,
91
                                                   const GimpRGB        *color,
92
                                                   gint                  color_index);
93 94
static GeglNode * gimp_curves_tool_get_operation  (GimpImageMapTool     *image_map_tool,
                                                   GObject             **config);
95 96 97
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);
98
static gboolean   gimp_curves_tool_settings_import(GimpImageMapTool     *image_map_tool,
99
                                                   const gchar          *filename,
100
                                                   GError              **error);
101
static gboolean   gimp_curves_tool_settings_export(GimpImageMapTool     *image_map_tool,
102 103
                                                   const gchar          *filename,
                                                   GError              **error);
104

105 106 107 108
static void       gimp_curves_tool_export_setup   (GimpSettingsBox      *settings_box,
                                                   GtkFileChooserDialog *dialog,
                                                   gboolean              export,
                                                   GimpCurvesTool       *tool);
109 110
static void       gimp_curves_tool_config_notify  (GObject              *object,
                                                   GParamSpec           *pspec,
111
                                                   GimpCurvesTool       *tool);
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);
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

129 130 131 132 133 134 135 136 137
static GimpRGB channel_colors[GIMP_HISTOGRAM_RGB] =
{
  { 0.0, 0.0, 0.0, 1.0 },
  { 1.0, 0.0, 0.0, 1.0 },
  { 0.0, 1.0, 0.0, 1.0 },
  { 0.0, 0.0, 1.0, 1.0 },
  { 0.5, 0.5, 0.5, 1.0 }
};

Michael Natterer's avatar
Michael Natterer committed
138

139
/*  public functions  */
Michael Natterer's avatar
Michael Natterer committed
140 141

void
Nate Summers's avatar
Nate Summers committed
142
gimp_curves_tool_register (GimpToolRegisterCallback  callback,
143
                           gpointer                  data)
Michael Natterer's avatar
Michael Natterer committed
144
{
Nate Summers's avatar
Nate Summers committed
145
  (* callback) (GIMP_TYPE_CURVES_TOOL,
146
                GIMP_TYPE_HISTOGRAM_OPTIONS,
147
                gimp_color_options_gui,
148
                0,
149
                "gimp-curves-tool",
150
                _("Curves"),
151
                _("Curves Tool: Adjust color curves"),
152
                N_("_Curves..."), NULL,
153
                NULL, GIMP_HELP_TOOL_CURVES,
Nate Summers's avatar
Nate Summers committed
154
                GIMP_STOCK_TOOL_CURVES,
155
                data);
Michael Natterer's avatar
Michael Natterer committed
156 157
}

158 159 160

/*  private functions  */

Michael Natterer's avatar
Michael Natterer committed
161 162 163
static void
gimp_curves_tool_class_init (GimpCurvesToolClass *klass)
{
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);

169
  object_class->finalize             = gimp_curves_tool_finalize;
170

171 172 173 174
  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;
175

176
  color_tool_class->picked           = gimp_curves_tool_color_picked;
177

178
  im_tool_class->dialog_desc         = _("Adjust Color Curves");
179 180 181
  im_tool_class->settings_name       = "curves";
  im_tool_class->import_dialog_title = _("Import Curves");
  im_tool_class->export_dialog_title = _("Export Curves");
182

183 184 185 186 187 188
  im_tool_class->get_operation       = gimp_curves_tool_get_operation;
  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_import     = gimp_curves_tool_settings_import;
  im_tool_class->settings_export     = gimp_curves_tool_settings_export;
Michael Natterer's avatar
Michael Natterer committed
189 190 191
}

static void
192
gimp_curves_tool_init (GimpCurvesTool *tool)
Michael Natterer's avatar
Michael Natterer committed
193
{
194 195
  GimpImageMapTool *im_tool = GIMP_IMAGE_MAP_TOOL (tool);
  gint              i;
196

197
  tool->lut = gimp_lut_new ();
198

199 200
  for (i = 0; i < G_N_ELEMENTS (tool->picked_color); i++)
    tool->picked_color[i] = -1.0;
201 202 203

  im_tool->apply_func = (GimpImageMapApplyFunc) gimp_lut_process;
  im_tool->apply_data = tool->lut;
Michael Natterer's avatar
Michael Natterer committed
204 205 206
}

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

211 212
  gimp_lut_free (tool->lut);

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
  GimpCurvesTool *c_tool   = GIMP_CURVES_TOOL (tool);
222 223
  GimpImage      *image    = gimp_display_get_image (display);
  GimpDrawable   *drawable = gimp_image_get_active_drawable (image);
224
  GimpHistogram  *histogram;
225 226

  if (! drawable)
227
    return FALSE;
228

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

236
  gimp_config_reset (GIMP_CONFIG (c_tool->config));
237

238 239 240 241
  if (! GIMP_TOOL_CLASS (parent_class)->initialize (tool, display, error))
    {
      return FALSE;
    }
242

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

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

250 251
  histogram = gimp_histogram_new ();
  gimp_drawable_calculate_histogram (drawable, histogram);
252
  gimp_histogram_view_set_background (GIMP_HISTOGRAM_VIEW (c_tool->graph),
253 254
                                      histogram);
  gimp_histogram_unref (histogram);
255

256
  return TRUE;
Michael Natterer's avatar
Michael Natterer committed
257 258 259
}

static void
260
gimp_curves_tool_button_release (GimpTool              *tool,
261
                                 const GimpCoords      *coords,
262 263 264 265
                                 guint32                time,
                                 GdkModifierType        state,
                                 GimpButtonReleaseType  release_type,
                                 GimpDisplay           *display)
Michael Natterer's avatar
Michael Natterer committed
266
{
267 268
  GimpCurvesTool   *c_tool = GIMP_CURVES_TOOL (tool);
  GimpCurvesConfig *config = c_tool->config;
Michael Natterer's avatar
Michael Natterer committed
269

270
  if (state & GDK_SHIFT_MASK)
Michael Natterer's avatar
Michael Natterer committed
271
    {
272
      GimpCurve *curve = config->curve[config->channel];
273
      gdouble    value = c_tool->picked_color[config->channel];
274
      gint       closest;
275

276
      closest = gimp_curve_get_closest_point (curve, value);
277

278
      gimp_curve_view_set_selected (GIMP_CURVE_VIEW (c_tool->graph),
279
                                    closest);
280

281
      gimp_curve_set_point (curve, closest,
282
                            value, gimp_curve_map_value (curve, value));
Michael Natterer's avatar
Michael Natterer committed
283
    }
284
  else if (state & gimp_get_toggle_behavior_mask ())
Michael Natterer's avatar
Michael Natterer committed
285
    {
286
      gint i;
287

288 289
      for (i = 0; i < 5; i++)
        {
290
          GimpCurve *curve = config->curve[i];
291
          gdouble    value = c_tool->picked_color[i];
292
          gint       closest;
293

294
          closest = gimp_curve_get_closest_point (curve, value);
295

296
          gimp_curve_view_set_selected (GIMP_CURVE_VIEW (c_tool->graph),
297
                                        closest);
298

299
          gimp_curve_set_point (curve, closest,
300
                                value, gimp_curve_map_value (curve, value));
301
        }
Michael Natterer's avatar
Michael Natterer committed
302 303
    }

304
  /*  chain up to halt the tool */
305 306
  GIMP_TOOL_CLASS (parent_class)->button_release (tool, coords, time, state,
                                                  release_type, display);
Michael Natterer's avatar
Michael Natterer committed
307 308
}

309
static gboolean
310 311
gimp_curves_tool_key_press (GimpTool    *tool,
                            GdkEventKey *kevent,
312
                            GimpDisplay *display)
313
{
314 315
  GimpCurvesTool *c_tool = GIMP_CURVES_TOOL (tool);

316 317 318 319
  if (gtk_widget_event (c_tool->graph, (GdkEvent *) kevent))
    return TRUE;

  return GIMP_TOOL_CLASS (parent_class)->key_press (tool, kevent, display);
320 321 322
}

static void
323 324 325 326 327
gimp_curves_tool_oper_update (GimpTool         *tool,
                              const GimpCoords *coords,
                              GdkModifierType   state,
                              gboolean          proximity,
                              GimpDisplay      *display)
328
{
329 330
  GimpColorPickMode  mode;
  const gchar       *status;
331

332
  GIMP_TOOL_CLASS (parent_class)->oper_update (tool, coords, state, proximity,
333
                                               display);
334

335
  gimp_tool_pop_status (tool, display);
336 337 338 339

  if (state & GDK_SHIFT_MASK)
    {
      mode   = GIMP_COLOR_PICK_MODE_PALETTE;
340
      status = _("Click to add a control point");
341
    }
342
  else if (state & gimp_get_toggle_behavior_mask ())
343 344
    {
      mode   = GIMP_COLOR_PICK_MODE_PALETTE;
345
      status = _("Click to add control points to all channels");
346
    }
347 348 349 350 351
  else
    {
      mode   = GIMP_COLOR_PICK_MODE_NONE;
      status = _("Click to locate on curve (try Shift, Ctrl)");
    }
352 353 354

  GIMP_COLOR_TOOL (tool)->pick_mode = mode;

355
  if (proximity)
356
    gimp_tool_push_status (tool, display, "%s", status);
357 358
}

Michael Natterer's avatar
Michael Natterer committed
359
static void
360 361
gimp_curves_tool_color_picked (GimpColorTool      *color_tool,
                               GimpColorPickState  pick_state,
362
                               GimpImageType       sample_type,
363
                               const GimpRGB      *color,
364
                               gint                color_index)
Michael Natterer's avatar
Michael Natterer committed
365
{
366 367
  GimpCurvesTool *tool = GIMP_CURVES_TOOL (color_tool);
  GimpDrawable   *drawable;
Michael Natterer's avatar
Michael Natterer committed
368

369
  drawable = GIMP_IMAGE_MAP_TOOL (tool)->drawable;
370

371 372 373
  tool->picked_color[GIMP_HISTOGRAM_RED]   = color->r;
  tool->picked_color[GIMP_HISTOGRAM_GREEN] = color->g;
  tool->picked_color[GIMP_HISTOGRAM_BLUE]  = color->b;
374

375
  if (gimp_drawable_has_alpha (drawable))
376
    tool->picked_color[GIMP_HISTOGRAM_ALPHA] = color->a;
377

378 379
  tool->picked_color[GIMP_HISTOGRAM_VALUE] = MAX (MAX (color->r, color->g),
                                                  color->b);
BST 1999 Andy Thomas's avatar
BST 1999 Andy Thomas committed
380

381
  gimp_curve_view_set_xpos (GIMP_CURVE_VIEW (tool->graph),
382
                            tool->picked_color[tool->config->channel]);
383 384 385
}

static GeglNode *
386 387
gimp_curves_tool_get_operation (GimpImageMapTool  *image_map_tool,
                                GObject          **config)
388 389 390 391 392
{
  GimpCurvesTool *tool = GIMP_CURVES_TOOL (image_map_tool);
  GeglNode       *node;

  node = g_object_new (GEGL_TYPE_NODE,
393
                       "operation", "gimp:curves",
394 395 396 397
                       NULL);

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

398 399 400 401 402
  *config = G_OBJECT (tool->config);

  g_signal_connect_object (tool->config, "notify",
                           G_CALLBACK (gimp_curves_tool_config_notify),
                           tool, 0);
403 404 405 406 407 408

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

  return node;
409
}
BST 1999 Andy Thomas's avatar
BST 1999 Andy Thomas committed
410

411 412
static void
gimp_curves_tool_map (GimpImageMapTool *image_map_tool)
Elliot Lee's avatar
Elliot Lee committed
413
{
414 415
  GimpCurvesTool *tool     = GIMP_CURVES_TOOL (image_map_tool);
  GimpDrawable   *drawable = image_map_tool->drawable;
416 417
  Curves          curves;

418 419
  gimp_curves_config_to_cruft (tool->config, &curves,
                               gimp_drawable_is_rgb (drawable));
420

421
  gimp_lut_setup (tool->lut,
422
                  (GimpLutFunc) curves_lut_func,
423
                  &curves,
424
                  gimp_drawable_bytes (drawable));
Elliot Lee's avatar
Elliot Lee committed
425 426
}

427

428 429 430
/*******************/
/*  Curves dialog  */
/*******************/
Elliot Lee's avatar
Elliot Lee committed
431

432 433
static void
gimp_curves_tool_dialog (GimpImageMapTool *image_map_tool)
Elliot Lee's avatar
Elliot Lee committed
434
{
435 436 437 438
  GimpCurvesTool   *tool         = GIMP_CURVES_TOOL (image_map_tool);
  GimpToolOptions  *tool_options = GIMP_TOOL_GET_OPTIONS (image_map_tool);
  GimpCurvesConfig *config       = tool->config;
  GtkListStore     *store;
439 440
  GtkSizeGroup     *label_group;
  GtkWidget        *main_vbox;
441 442 443 444 445 446 447 448
  GtkWidget        *vbox;
  GtkWidget        *hbox;
  GtkWidget        *hbox2;
  GtkWidget        *label;
  GtkWidget        *frame;
  GtkWidget        *table;
  GtkWidget        *button;
  GtkWidget        *bar;
449
  GtkWidget        *combo;
450

451 452 453 454
  g_signal_connect (image_map_tool->settings_box, "file-dialog-setup",
                    G_CALLBACK (gimp_curves_tool_export_setup),
                    image_map_tool);

455 456
  main_vbox   = gimp_image_map_tool_dialog_get_vbox (image_map_tool);
  label_group = gimp_image_map_tool_dialog_get_label_group (image_map_tool);
Elliot Lee's avatar
Elliot Lee committed
457

458
  /*  The combo box for selecting channels  */
Michael Natterer's avatar
Michael Natterer committed
459
  hbox = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 6);
460
  gtk_box_pack_start (GTK_BOX (main_vbox), hbox, FALSE, FALSE, 0);
461 462
  gtk_widget_show (hbox);

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

467 468
  gtk_size_group_add_widget (label_group, label);

469 470 471
  store = gimp_enum_store_new_with_range (GIMP_TYPE_HISTOGRAM_CHANNEL,
                                          GIMP_HISTOGRAM_VALUE,
                                          GIMP_HISTOGRAM_ALPHA);
472 473
  tool->channel_menu =
    gimp_enum_combo_box_new_with_model (GIMP_ENUM_STORE (store));
474 475
  g_object_unref (store);

476 477 478
  gimp_int_combo_box_set_active (GIMP_INT_COMBO_BOX (tool->channel_menu),
                                 config->channel);
  gimp_enum_combo_box_set_stock_prefix (GIMP_ENUM_COMBO_BOX (tool->channel_menu),
479
                                        "gimp-channel");
480 481
  gtk_box_pack_start (GTK_BOX (hbox), tool->channel_menu, FALSE, FALSE, 0);
  gtk_widget_show (tool->channel_menu);
482

483 484 485
  g_signal_connect (tool->channel_menu, "changed",
                    G_CALLBACK (curves_channel_callback),
                    tool);
486

487
  gtk_label_set_mnemonic_widget (GTK_LABEL (label), tool->channel_menu);
488

489
  button = gtk_button_new_with_mnemonic (_("R_eset Channel"));
490
  gtk_box_pack_start (GTK_BOX (hbox), button, FALSE, FALSE, 0);
491
  gtk_widget_show (button);
492

493
  g_signal_connect (button, "clicked",
494
                    G_CALLBACK (curves_channel_reset_callback),
495
                    tool);
Elliot Lee's avatar
Elliot Lee committed
496

497 498 499 500 501 502
  /*  The histogram scale radio buttons  */
  hbox2 = gimp_prop_enum_stock_box_new (G_OBJECT (tool_options),
                                        "histogram-scale", "gimp-histogram",
                                        0, 0);
  gtk_box_pack_end (GTK_BOX (hbox), hbox2, FALSE, FALSE, 0);
  gtk_widget_show (hbox2);
503

504
  /*  The table for the color bars and the graph  */
Elliot Lee's avatar
Elliot Lee committed
505
  table = gtk_table_new (2, 2, FALSE);
506 507
  gtk_table_set_col_spacings (GTK_TABLE (table), 2);
  gtk_table_set_row_spacings (GTK_TABLE (table), 2);
508
  gtk_box_pack_start (GTK_BOX (main_vbox), table, TRUE, TRUE, 0);
509 510

  /*  The left color bar  */
Michael Natterer's avatar
Michael Natterer committed
511
  vbox = gtk_box_new (GTK_ORIENTATION_VERTICAL, 0);
512
  gtk_table_attach (GTK_TABLE (table), vbox, 0, 1, 0, 1,
513
                    GTK_FILL, GTK_EXPAND | GTK_FILL, 0, 0);
514
  gtk_widget_show (vbox);
Elliot Lee's avatar
Elliot Lee committed
515 516

  frame = gtk_frame_new (NULL);
517
  gtk_frame_set_shadow_type (GTK_FRAME (frame), GTK_SHADOW_IN);
518
  gtk_box_pack_start (GTK_BOX (vbox), frame, TRUE, TRUE, RADIUS);
Elliot Lee's avatar
Elliot Lee committed
519 520
  gtk_widget_show (frame);

521 522 523 524
  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);
525

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

533
  tool->graph = gimp_curve_view_new ();
534 535
  gimp_curve_view_set_range_x (GIMP_CURVE_VIEW (tool->graph), 0, 255);
  gimp_curve_view_set_range_y (GIMP_CURVE_VIEW (tool->graph), 0, 255);
536 537 538 539
  gtk_widget_set_size_request (tool->graph,
                               GRAPH_SIZE + RADIUS * 2,
                               GRAPH_SIZE + RADIUS * 2);
  g_object_set (tool->graph,
540 541 542
                "border-width", RADIUS,
                "subdivisions", 1,
                NULL);
543
  gimp_curve_view_set_curve (GIMP_CURVE_VIEW (tool->graph),
544 545
                             config->curve[config->channel],
                             &channel_colors[config->channel]);
546 547
  gtk_container_add (GTK_CONTAINER (frame), tool->graph);
  gtk_widget_show (tool->graph);
548

549
  gimp_histogram_options_connect_view (GIMP_HISTOGRAM_OPTIONS (tool_options),
550 551 552
                                       GIMP_HISTOGRAM_VIEW (tool->graph));

  /*  The bottom color bar  */
Michael Natterer's avatar
Michael Natterer committed
553
  hbox2 = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 0);
554
  gtk_table_attach (GTK_TABLE (table), hbox2, 1, 2, 1, 2,
555
                    GTK_EXPAND | GTK_FILL, GTK_FILL, 0, 0);
556
  gtk_widget_show (hbox2);
557

Elliot Lee's avatar
Elliot Lee committed
558
  frame = gtk_frame_new (NULL);
559
  gtk_frame_set_shadow_type (GTK_FRAME (frame), GTK_SHADOW_IN);
560
  gtk_box_pack_start (GTK_BOX (hbox2), frame, TRUE, TRUE, RADIUS);
Elliot Lee's avatar
Elliot Lee committed
561 562
  gtk_widget_show (frame);

Michael Natterer's avatar
Michael Natterer committed
563 564
  vbox = gtk_box_new (GTK_ORIENTATION_VERTICAL, 0);
  gtk_box_set_homogeneous (GTK_BOX (vbox), TRUE);
565 566
  gtk_container_add (GTK_CONTAINER (frame), vbox);
  gtk_widget_show (vbox);
567

568 569
  tool->xrange = gimp_color_bar_new (GTK_ORIENTATION_HORIZONTAL);
  gtk_widget_set_size_request (tool->xrange, -1, BAR_SIZE / 2);
570
  gtk_box_pack_start (GTK_BOX (vbox), tool->xrange, TRUE, TRUE, 0);
571
  gtk_widget_show (tool->xrange);
572

573
  bar = gimp_color_bar_new (GTK_ORIENTATION_HORIZONTAL);
574
  gtk_box_pack_start (GTK_BOX (vbox), bar, TRUE, TRUE, 0);
575 576
  gtk_widget_show (bar);

577
  gtk_widget_show (table);
Elliot Lee's avatar
Elliot Lee committed
578

Michael Natterer's avatar
Michael Natterer committed
579
  hbox = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 6);
580
  gtk_box_pack_end (GTK_BOX (main_vbox), hbox, FALSE, FALSE, 0);
581
  gtk_widget_show (hbox);
582

583 584 585
  label = gtk_label_new_with_mnemonic (_("Curve _type:"));
  gtk_box_pack_start (GTK_BOX (hbox), label, FALSE, FALSE, 0);
  gtk_widget_show (label);
586

587 588 589 590 591 592 593 594
  tool->curve_type = combo = gimp_enum_combo_box_new (GIMP_TYPE_CURVE_TYPE);
  gimp_enum_combo_box_set_stock_prefix (GIMP_ENUM_COMBO_BOX (combo),
                                        "gimp-curve");
  gimp_int_combo_box_connect (GIMP_INT_COMBO_BOX (combo), 0,
                              G_CALLBACK (curves_curve_type_callback),
                              tool);
  gtk_box_pack_start (GTK_BOX (hbox), combo, TRUE, TRUE, 0);
  gtk_widget_show (combo);
Sven Neumann's avatar
Sven Neumann committed
595 596

  gtk_label_set_mnemonic_widget (GTK_LABEL (label), combo);
Elliot Lee's avatar
Elliot Lee committed
597 598
}

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

606 607
  default_config = GIMP_CURVES_CONFIG (image_map_tool->default_config);

608 609 610 611
  for (channel = GIMP_HISTOGRAM_VALUE;
       channel <= GIMP_HISTOGRAM_ALPHA;
       channel++)
    {
612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631
      if (default_config)
        {
          GimpCurveType curve_type = tool->config->curve[channel]->curve_type;

          g_object_freeze_notify (G_OBJECT (tool->config->curve[channel]));

          gimp_config_copy (GIMP_CONFIG (default_config->curve[channel]),
                            GIMP_CONFIG (tool->config->curve[channel]),
                            0);

          g_object_set (tool->config->curve[channel],
                        "curve-type", curve_type,
                        NULL);

          g_object_thaw_notify (G_OBJECT (tool->config->curve[channel]));
        }
      else
        {
          gimp_curve_reset (tool->config->curve[channel], FALSE);
        }
632
    }
633 634
}

635
static gboolean
636 637 638
gimp_curves_tool_settings_import (GimpImageMapTool  *image_map_tool,
                                  const gchar       *filename,
                                  GError           **error)
639
{
640
  GimpCurvesTool *tool = GIMP_CURVES_TOOL (image_map_tool);
641
  FILE           *file;
642
  gchar           header[64];
643 644 645 646 647 648 649 650 651 652 653 654

  file = g_fopen (filename, "rt");

  if (! file)
    {
      g_set_error (error, G_FILE_ERROR, g_file_error_from_errno (errno),
                   _("Could not open '%s' for reading: %s"),
                   gimp_filename_to_utf8 (filename),
                   g_strerror (errno));
      return FALSE;
    }

655 656 657 658 659 660 661 662 663 664 665 666
  if (! fgets (header, sizeof (header), file))
    {
      g_set_error (error, G_FILE_ERROR, g_file_error_from_errno (errno),
                   _("Could not read header from '%s': %s"),
                   gimp_filename_to_utf8 (filename),
                   g_strerror (errno));
      fclose (file);
      return FALSE;
    }

  if (g_str_has_prefix (header, "# GIMP Curves File\n"))
    {
667 668
      gboolean success;

669 670 671 672 673 674 675 676
      rewind (file);

      success = gimp_curves_config_load_cruft (tool->config, file, error);

      fclose (file);

      return success;
    }
677 678

  fclose (file);
679

680 681 682
  return GIMP_IMAGE_MAP_TOOL_CLASS (parent_class)->settings_import (image_map_tool,
                                                                    filename,
                                                                    error);
683 684 685
}

static gboolean
686 687 688
gimp_curves_tool_settings_export (GimpImageMapTool  *image_map_tool,
                                  const gchar       *filename,
                                  GError           **error)
689
{
690
  GimpCurvesTool *tool = GIMP_CURVES_TOOL (image_map_tool);
691

692
  if (tool->export_old_format)
693
    {
694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712
      FILE     *file;
      gboolean  success;

      file = g_fopen (filename, "wt");

      if (! file)
        {
          g_set_error (error, G_FILE_ERROR, g_file_error_from_errno (errno),
                       _("Could not open '%s' for writing: %s"),
                       gimp_filename_to_utf8 (filename),
                       g_strerror (errno));
          return FALSE;
        }

      success = gimp_curves_config_save_cruft (tool->config, file, error);

      fclose (file);

      return success;
713 714
    }

715 716 717 718 719 720 721 722 723 724 725 726
  return GIMP_IMAGE_MAP_TOOL_CLASS (parent_class)->settings_export (image_map_tool,
                                                                    filename,
                                                                    error);
}

static void
gimp_curves_tool_export_setup (GimpSettingsBox      *settings_box,
                               GtkFileChooserDialog *dialog,
                               gboolean              export,
                               GimpCurvesTool       *tool)
{
  GtkWidget *button;
727

728 729 730 731 732 733 734 735
  if (! export)
    return;

  button = gtk_check_button_new_with_mnemonic (_("Use _old curves file format"));
  gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (button),
                                tool->export_old_format);
  gtk_file_chooser_set_extra_widget (GTK_FILE_CHOOSER (dialog), button);
  gtk_widget_show (button);
736

737 738 739
  g_signal_connect (button, "toggled",
                    G_CALLBACK (gimp_toggle_button_update),
                    &tool->export_old_format);
740
}
741

Elliot Lee's avatar
Elliot Lee committed
742
static void
743 744 745
gimp_curves_tool_config_notify (GObject        *object,
                                GParamSpec     *pspec,
                                GimpCurvesTool *tool)
Elliot Lee's avatar
Elliot Lee committed
746
{
747
  GimpCurvesConfig *config = GIMP_CURVES_CONFIG (object);
748
  GimpCurve        *curve  = config->curve[config->channel];
749

750
  if (! tool->xrange)
751 752
    return;

753
  if (! strcmp (pspec->name, "channel"))
754
    {
755
      GimpHistogramChannel channel;
756

757 758 759
      gimp_int_combo_box_set_active (GIMP_INT_COMBO_BOX (tool->channel_menu),
                                     config->channel);

760
      switch (config->channel)
761
        {
762 763 764 765
          guchar r[256];
          guchar g[256];
          guchar b[256];

766 767
        case GIMP_HISTOGRAM_VALUE:
        case GIMP_HISTOGRAM_ALPHA:
768
        case GIMP_HISTOGRAM_RGB:
769
          gimp_curve_get_uchar (curve, sizeof (r), r);
770

771
          gimp_color_bar_set_buffers (GIMP_COLOR_BAR (tool->xrange),
772
                                      r, r, r);
773
          break;
774

775 776 777
        case GIMP_HISTOGRAM_RED:
        case GIMP_HISTOGRAM_GREEN:
        case GIMP_HISTOGRAM_BLUE:
778 779 780 781 782 783
          gimp_curve_get_uchar (config->curve[GIMP_HISTOGRAM_RED],
                                sizeof (r), r);
          gimp_curve_get_uchar (config->curve[GIMP_HISTOGRAM_GREEN],
                                sizeof (g), g);
          gimp_curve_get_uchar (config->curve[GIMP_HISTOGRAM_BLUE],
                                sizeof (b), b);
784

785
          gimp_color_bar_set_buffers (GIMP_COLOR_BAR (tool->xrange),
786
                                      r, g, b);
787 788
          break;
        }
789

790
      gimp_histogram_view_set_channel (GIMP_HISTOGRAM_VIEW (tool->graph),
791
                                       config->channel);
792
      gimp_curve_view_set_xpos (GIMP_CURVE_VIEW (tool->graph),
793
                                tool->picked_color[config->channel]);
794

795 796
      gimp_color_bar_set_channel (GIMP_COLOR_BAR (tool->yrange),
                                  config->channel);
Elliot Lee's avatar
Elliot Lee committed
797

798
      gimp_curve_view_remove_all_backgrounds (GIMP_CURVE_VIEW (tool->graph));
799

800 801 802
      for (channel = GIMP_HISTOGRAM_VALUE;
           channel <= GIMP_HISTOGRAM_ALPHA;
           channel++)
803
        {
804 805 806 807 808 809 810 811 812 813 814
          if (channel == config->channel)
            {
              gimp_curve_view_set_curve (GIMP_CURVE_VIEW (tool->graph), curve,
                                         &channel_colors[channel]);
            }
          else
            {
              gimp_curve_view_add_background (GIMP_CURVE_VIEW (tool->graph),
                                              config->curve[channel],
                                              &channel_colors[channel]);
            }
815 816
        }

817 818
      gimp_int_combo_box_set_active (GIMP_INT_COMBO_BOX (tool->curve_type),
                                     curve->curve_type);