gimpcurvestool.c 32.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 34
#include "operations/gimpcurvesconfig.h"
#include "operations/gimpoperationcurves.h"
35

36
#include "core/gimp.h"
37
#include "core/gimpcurve.h"
Sven Neumann's avatar
Sven Neumann committed
38
#include "core/gimpcurve-map.h"
Michael Natterer's avatar
Michael Natterer committed
39
#include "core/gimpdrawable.h"
40
#include "core/gimpdrawable-histogram.h"
41
#include "core/gimperror.h"
42
#include "core/gimphistogram.h"
Michael Natterer's avatar
Michael Natterer committed
43 44
#include "core/gimpimage.h"

45
#include "widgets/gimpcolorbar.h"
46
#include "widgets/gimphelp-ids.h"
47
#include "widgets/gimpcurveview.h"
48
#include "widgets/gimpwidgets-utils.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 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127
static void       gimp_curves_tool_constructed     (GObject              *object);

static gboolean   gimp_curves_tool_initialize      (GimpTool             *tool,
                                                    GimpDisplay          *display,
                                                    GError              **error);
static void       gimp_curves_tool_button_release  (GimpTool             *tool,
                                                    const 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,
                                                    const GimpCoords     *coords,
                                                    GdkModifierType       state,
                                                    gboolean              proximity,
                                                    GimpDisplay          *display);

static void       gimp_curves_tool_color_picked    (GimpColorTool        *color_tool,
                                                    GimpColorPickState    pick_state,
                                                    gdouble               x,
                                                    gdouble               y,
                                                    const Babl           *sample_format,
                                                    gpointer              pixel,
                                                    const GimpRGB        *color);

static gchar    * gimp_curves_tool_get_operation   (GimpFilterTool       *filter_tool,
                                                    gchar               **title,
                                                    gchar               **description,
                                                    gchar               **undo_desc,
                                                    gchar               **icon_name,
                                                    gchar               **help_id);
static void       gimp_curves_tool_dialog          (GimpFilterTool       *filter_tool);
static void       gimp_curves_tool_reset           (GimpFilterTool       *filter_tool);
static gboolean   gimp_curves_tool_settings_import (GimpFilterTool       *filter_tool,
                                                    GInputStream         *input,
                                                    GError              **error);
static gboolean   gimp_curves_tool_settings_export (GimpFilterTool       *filter_tool,
                                                    GOutputStream        *output,
                                                    GError              **error);

static void       gimp_curves_tool_export_setup    (GimpSettingsBox      *settings_box,
                                                    GtkFileChooserDialog *dialog,
                                                    gboolean              export,
                                                    GimpCurvesTool       *tool);
static void       gimp_curves_tool_update_channel  (GimpCurvesTool       *tool);
static void       gimp_curves_tool_config_notify   (GObject              *object,
                                                    GParamSpec           *pspec,
                                                    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);

128 129 130
static gboolean   curves_get_channel_color         (GtkWidget            *widget,
                                                    GimpHistogramChannel  channel,
                                                    GimpRGB              *color);
131 132 133


G_DEFINE_TYPE (GimpCurvesTool, gimp_curves_tool, GIMP_TYPE_FILTER_TOOL)
134 135

#define parent_class gimp_curves_tool_parent_class
136

Michael Natterer's avatar
Michael Natterer committed
137

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

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

157 158 159

/*  private functions  */

Michael Natterer's avatar
Michael Natterer committed
160 161 162
static void
gimp_curves_tool_class_init (GimpCurvesToolClass *klass)
{
163 164 165 166
  GObjectClass        *object_class      = G_OBJECT_CLASS (klass);
  GimpToolClass       *tool_class        = GIMP_TOOL_CLASS (klass);
  GimpColorToolClass  *color_tool_class  = GIMP_COLOR_TOOL_CLASS (klass);
  GimpFilterToolClass *filter_tool_class = GIMP_FILTER_TOOL_CLASS (klass);
167

168
  object_class->constructed              = gimp_curves_tool_constructed;
169

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

175
  color_tool_class->picked               = gimp_curves_tool_color_picked;
176

177 178 179
  filter_tool_class->settings_name       = "curves";
  filter_tool_class->import_dialog_title = _("Import Curves");
  filter_tool_class->export_dialog_title = _("Export Curves");
180

181 182 183 184 185
  filter_tool_class->get_operation       = gimp_curves_tool_get_operation;
  filter_tool_class->dialog              = gimp_curves_tool_dialog;
  filter_tool_class->reset               = gimp_curves_tool_reset;
  filter_tool_class->settings_import     = gimp_curves_tool_settings_import;
  filter_tool_class->settings_export     = gimp_curves_tool_settings_export;
Michael Natterer's avatar
Michael Natterer committed
186 187 188
}

static void
189
gimp_curves_tool_init (GimpCurvesTool *tool)
Michael Natterer's avatar
Michael Natterer committed
190
{
191
  gint i;
192

193 194
  for (i = 0; i < G_N_ELEMENTS (tool->picked_color); i++)
    tool->picked_color[i] = -1.0;
Michael Natterer's avatar
Michael Natterer committed
195 196
}

197 198 199 200 201
static void
gimp_curves_tool_constructed (GObject *object)
{
  G_OBJECT_CLASS (parent_class)->constructed (object);

202
  g_signal_connect_object (GIMP_FILTER_TOOL (object)->config, "notify",
203 204 205
                           G_CALLBACK (gimp_curves_tool_config_notify),
                           object, 0);

206 207 208 209 210 211
  /*  always pick colors  */
  gimp_color_tool_enable (GIMP_COLOR_TOOL (object),
                          GIMP_COLOR_TOOL_GET_OPTIONS (object));

}

212
static gboolean
213 214 215
gimp_curves_tool_initialize (GimpTool     *tool,
                             GimpDisplay  *display,
                             GError      **error)
Michael Natterer's avatar
Michael Natterer committed
216
{
217
  GimpCurvesTool *c_tool   = GIMP_CURVES_TOOL (tool);
218 219
  GimpImage      *image    = gimp_display_get_image (display);
  GimpDrawable   *drawable = gimp_image_get_active_drawable (image);
220
  GimpHistogram  *histogram;
221

222 223 224 225
  if (! GIMP_TOOL_CLASS (parent_class)->initialize (tool, display, error))
    {
      return FALSE;
    }
226

227
  histogram = gimp_histogram_new (FALSE);
228
  gimp_drawable_calculate_histogram (drawable, histogram);
229
  gimp_histogram_view_set_background (GIMP_HISTOGRAM_VIEW (c_tool->graph),
230
                                      histogram);
231
  g_object_unref (histogram);
232

233
  if (gimp_drawable_get_component_type (drawable) == GIMP_COMPONENT_TYPE_U8)
234 235 236 237 238 239
    {
      gimp_curve_view_set_range_x (GIMP_CURVE_VIEW (c_tool->graph), 0, 255);
      gimp_curve_view_set_range_y (GIMP_CURVE_VIEW (c_tool->graph), 0, 255);
    }
  else
    {
240 241
      gimp_curve_view_set_range_x (GIMP_CURVE_VIEW (c_tool->graph), 0, 100);
      gimp_curve_view_set_range_y (GIMP_CURVE_VIEW (c_tool->graph), 0, 100);
242 243
    }

244
  return TRUE;
Michael Natterer's avatar
Michael Natterer committed
245 246 247
}

static void
248
gimp_curves_tool_button_release (GimpTool              *tool,
249
                                 const GimpCoords      *coords,
250 251 252 253
                                 guint32                time,
                                 GdkModifierType        state,
                                 GimpButtonReleaseType  release_type,
                                 GimpDisplay           *display)
Michael Natterer's avatar
Michael Natterer committed
254
{
255 256 257
  GimpCurvesTool   *c_tool      = GIMP_CURVES_TOOL (tool);
  GimpFilterTool   *filter_tool = GIMP_FILTER_TOOL (tool);
  GimpCurvesConfig *config      = GIMP_CURVES_CONFIG (filter_tool->config);
Michael Natterer's avatar
Michael Natterer committed
258

259
  if (state & gimp_get_extend_selection_mask ())
Michael Natterer's avatar
Michael Natterer committed
260
    {
261
      GimpCurve *curve = config->curve[config->channel];
262
      gdouble    value = c_tool->picked_color[config->channel];
263
      gint       closest;
264

265
      closest = gimp_curve_get_closest_point (curve, value);
266

267
      gimp_curve_view_set_selected (GIMP_CURVE_VIEW (c_tool->graph),
268
                                    closest);
269

270
      gimp_curve_set_point (curve, closest,
271
                            value, gimp_curve_map_value (curve, value));
Michael Natterer's avatar
Michael Natterer committed
272
    }
273
  else if (state & gimp_get_toggle_behavior_mask ())
Michael Natterer's avatar
Michael Natterer committed
274
    {
275
      GimpHistogramChannel channel;
276

277 278 279
      for (channel = GIMP_HISTOGRAM_VALUE;
           channel <= GIMP_HISTOGRAM_ALPHA;
           channel++)
280
        {
281 282
          GimpCurve *curve = config->curve[channel];
          gdouble    value = c_tool->picked_color[channel];
283
          gint       closest;
284

285 286 287
          if (value != -1)
            {
              closest = gimp_curve_get_closest_point (curve, value);
288

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

292 293 294
              gimp_curve_set_point (curve, closest,
                                    value, gimp_curve_map_value (curve, value));
            }
295
        }
Michael Natterer's avatar
Michael Natterer committed
296 297
    }

298
  /*  chain up to halt the tool */
299 300
  GIMP_TOOL_CLASS (parent_class)->button_release (tool, coords, time, state,
                                                  release_type, display);
Michael Natterer's avatar
Michael Natterer committed
301 302
}

303
static gboolean
304 305
gimp_curves_tool_key_press (GimpTool    *tool,
                            GdkEventKey *kevent,
306
                            GimpDisplay *display)
307
{
308 309
  GimpCurvesTool *c_tool = GIMP_CURVES_TOOL (tool);

310 311 312 313 314
  if (tool->display && c_tool->graph)
    {
      if (gtk_widget_event (c_tool->graph, (GdkEvent *) kevent))
        return TRUE;
    }
315 316

  return GIMP_TOOL_CLASS (parent_class)->key_press (tool, kevent, display);
317 318 319
}

static void
320 321 322 323 324
gimp_curves_tool_oper_update (GimpTool         *tool,
                              const GimpCoords *coords,
                              GdkModifierType   state,
                              gboolean          proximity,
                              GimpDisplay      *display)
325
{
326 327
  if (gimp_filter_tool_on_guide (GIMP_FILTER_TOOL (tool),
                                 coords, display))
328
    {
329 330
      GIMP_TOOL_CLASS (parent_class)->oper_update (tool, coords, state, proximity,
                                                   display);
331
    }
332 333
  else
    {
334 335 336 337
      GimpColorPickMode  mode;
      gchar             *status      = NULL;
      GdkModifierType    extend_mask = gimp_get_extend_selection_mask ();
      GdkModifierType    toggle_mask = gimp_get_toggle_behavior_mask ();
338

339
      gimp_tool_pop_status (tool, display);
340

341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359
      if (state & extend_mask)
        {
          mode   = GIMP_COLOR_PICK_MODE_PALETTE;
          status = g_strdup (_("Click to add a control point"));
        }
      else if (state & toggle_mask)
        {
          mode   = GIMP_COLOR_PICK_MODE_PALETTE;
          status = g_strdup (_("Click to add control points to all channels"));
        }
      else
        {
          mode   = GIMP_COLOR_PICK_MODE_NONE;
          status = gimp_suggest_modifiers (_("Click to locate on curve"),
                                           (extend_mask | toggle_mask) & ~state,
                                           _("%s: add control point"),
                                           _("%s: add control points to all channels"),
                                           NULL);
        }
360

361 362 363 364 365 366 367
      GIMP_COLOR_TOOL (tool)->pick_mode = mode;

      if (proximity)
        gimp_tool_push_status (tool, display, "%s", status);

      g_free (status);
    }
368 369
}

Michael Natterer's avatar
Michael Natterer committed
370
static void
371 372
gimp_curves_tool_color_picked (GimpColorTool      *color_tool,
                               GimpColorPickState  pick_state,
373 374
                               gdouble             x,
                               gdouble             y,
375
                               const Babl         *sample_format,
376 377
                               gpointer            pixel,
                               const GimpRGB      *color)
Michael Natterer's avatar
Michael Natterer committed
378
{
379 380 381
  GimpCurvesTool   *tool        = GIMP_CURVES_TOOL (color_tool);
  GimpFilterTool   *filter_tool = GIMP_FILTER_TOOL (color_tool);
  GimpCurvesConfig *config      = GIMP_CURVES_CONFIG (filter_tool->config);
382
  GimpDrawable     *drawable;
Michael Natterer's avatar
Michael Natterer committed
383

384
  drawable = GIMP_FILTER_TOOL (tool)->drawable;
385

386 387 388
  tool->picked_color[GIMP_HISTOGRAM_RED]   = color->r;
  tool->picked_color[GIMP_HISTOGRAM_GREEN] = color->g;
  tool->picked_color[GIMP_HISTOGRAM_BLUE]  = color->b;
389

390
  if (gimp_drawable_has_alpha (drawable))
391
    tool->picked_color[GIMP_HISTOGRAM_ALPHA] = color->a;
392 393
  else
    tool->picked_color[GIMP_HISTOGRAM_ALPHA] = -1;
394

395 396
  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
397

398
  gimp_curve_view_set_xpos (GIMP_CURVE_VIEW (tool->graph),
399
                            tool->picked_color[config->channel]);
400 401
}

402
static gchar *
403 404 405 406 407 408
gimp_curves_tool_get_operation (GimpFilterTool  *filter_tool,
                                gchar          **title,
                                gchar          **description,
                                gchar          **undo_desc,
                                gchar          **icon_name,
                                gchar          **help_id)
409
{
410
  *description = g_strdup (_("Adjust Color Curves"));
411

412
  return g_strdup ("gimp:curves");
413
}
BST 1999 Andy Thomas's avatar
BST 1999 Andy Thomas committed
414

415

416 417 418
/*******************/
/*  Curves dialog  */
/*******************/
Elliot Lee's avatar
Elliot Lee committed
419

420
static void
421
gimp_curves_tool_dialog (GimpFilterTool *filter_tool)
Elliot Lee's avatar
Elliot Lee committed
422
{
423 424 425
  GimpCurvesTool   *tool         = GIMP_CURVES_TOOL (filter_tool);
  GimpToolOptions  *tool_options = GIMP_TOOL_GET_OPTIONS (filter_tool);
  GimpCurvesConfig *config       = GIMP_CURVES_CONFIG (filter_tool->config);
426
  GtkListStore     *store;
427
  GtkWidget        *main_vbox;
428
  GtkWidget        *frame_vbox;
429 430 431 432
  GtkWidget        *vbox;
  GtkWidget        *hbox;
  GtkWidget        *hbox2;
  GtkWidget        *label;
433
  GtkWidget        *main_frame;
434 435 436 437
  GtkWidget        *frame;
  GtkWidget        *table;
  GtkWidget        *button;
  GtkWidget        *bar;
438
  GtkWidget        *combo;
439

440
  g_signal_connect (filter_tool->settings_box, "file-dialog-setup",
441
                    G_CALLBACK (gimp_curves_tool_export_setup),
442
                    filter_tool);
443

444
  main_vbox = gimp_filter_tool_dialog_get_vbox (filter_tool);
Elliot Lee's avatar
Elliot Lee committed
445

446
  /*  The combo box for selecting channels  */
447 448 449 450
  main_frame = gimp_frame_new (NULL);
  gtk_box_pack_start (GTK_BOX (main_vbox), main_frame, TRUE, TRUE, 0);
  gtk_widget_show (main_frame);

Michael Natterer's avatar
Michael Natterer committed
451
  hbox = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 6);
452
  gtk_frame_set_label_widget (GTK_FRAME (main_frame), hbox);
453 454
  gtk_widget_show (hbox);

455
  label = gtk_label_new_with_mnemonic (_("Cha_nnel:"));
456 457 458
  gimp_label_set_attributes (GTK_LABEL (label),
                             PANGO_ATTR_WEIGHT, PANGO_WEIGHT_BOLD,
                             -1);
459 460 461
  gtk_box_pack_start (GTK_BOX (hbox), label, FALSE, FALSE, 0);
  gtk_widget_show (label);

462 463 464
  store = gimp_enum_store_new_with_range (GIMP_TYPE_HISTOGRAM_CHANNEL,
                                          GIMP_HISTOGRAM_VALUE,
                                          GIMP_HISTOGRAM_ALPHA);
465 466
  tool->channel_menu =
    gimp_enum_combo_box_new_with_model (GIMP_ENUM_STORE (store));
467 468
  g_object_unref (store);

469 470
  gimp_int_combo_box_set_active (GIMP_INT_COMBO_BOX (tool->channel_menu),
                                 config->channel);
471 472
  gimp_enum_combo_box_set_icon_prefix (GIMP_ENUM_COMBO_BOX (tool->channel_menu),
                                       "gimp-channel");
473 474
  gimp_int_combo_box_set_sensitivity (GIMP_INT_COMBO_BOX (tool->channel_menu),
                                      curves_menu_sensitivity, filter_tool, NULL);
475 476
  gtk_box_pack_start (GTK_BOX (hbox), tool->channel_menu, FALSE, FALSE, 0);
  gtk_widget_show (tool->channel_menu);
477

478 479 480
  g_signal_connect (tool->channel_menu, "changed",
                    G_CALLBACK (curves_channel_callback),
                    tool);
481

482
  gtk_label_set_mnemonic_widget (GTK_LABEL (label), tool->channel_menu);
483

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

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

492
  /*  The histogram scale radio buttons  */
493 494 495
  hbox2 = gimp_prop_enum_icon_box_new (G_OBJECT (tool_options),
                                       "histogram-scale", "gimp-histogram",
                                       0, 0);
496 497
  gtk_box_pack_end (GTK_BOX (hbox), hbox2, FALSE, FALSE, 0);
  gtk_widget_show (hbox2);
498

499 500 501 502
  frame_vbox = gtk_box_new (GTK_ORIENTATION_VERTICAL, 4);
  gtk_container_add (GTK_CONTAINER (main_frame), frame_vbox);
  gtk_widget_show (frame_vbox);

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

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

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

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

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

532
  tool->graph = gimp_curve_view_new ();
533 534
  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);
535 536 537 538
  gtk_widget_set_size_request (tool->graph,
                               GRAPH_SIZE + RADIUS * 2,
                               GRAPH_SIZE + RADIUS * 2);
  g_object_set (tool->graph,
539 540 541
                "border-width", RADIUS,
                "subdivisions", 1,
                NULL);
542 543
  gtk_container_add (GTK_CONTAINER (frame), tool->graph);
  gtk_widget_show (tool->graph);
544

545 546 547 548
  g_object_bind_property (G_OBJECT (tool_options), "histogram-scale",
                          G_OBJECT (tool->graph),  "histogram-scale",
                          G_BINDING_SYNC_CREATE |
                          G_BINDING_BIDIRECTIONAL);
549 550

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

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

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

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

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

575
  gtk_widget_show (table);
Elliot Lee's avatar
Elliot Lee committed
576

Michael Natterer's avatar
Michael Natterer committed
577
  hbox = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 6);
578
  gtk_box_pack_end (GTK_BOX (frame_vbox), hbox, FALSE, FALSE, 0);
579
  gtk_widget_show (hbox);
580

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

585
  tool->curve_type = combo = gimp_enum_combo_box_new (GIMP_TYPE_CURVE_TYPE);
586 587
  gimp_enum_combo_box_set_icon_prefix (GIMP_ENUM_COMBO_BOX (combo),
                                       "gimp-curve");
588 589 590 591 592
  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
593 594

  gtk_label_set_mnemonic_widget (GTK_LABEL (label), combo);
595 596

  gimp_curves_tool_update_channel (tool);
Elliot Lee's avatar
Elliot Lee committed
597 598
}

599
static void
600
gimp_curves_tool_reset (GimpFilterTool *filter_tool)
601
{
602
  GimpCurvesConfig     *config = GIMP_CURVES_CONFIG (filter_tool->config);
603
  GimpCurvesConfig     *default_config;
604
  GimpHistogramChannel  channel;
605

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

608 609 610 611
  for (channel = GIMP_HISTOGRAM_VALUE;
       channel <= GIMP_HISTOGRAM_ALPHA;
       channel++)
    {
612 613
      if (default_config)
        {
614
          GimpCurveType curve_type = config->curve[channel]->curve_type;
615

616
          g_object_freeze_notify (G_OBJECT (config->curve[channel]));
617 618

          gimp_config_copy (GIMP_CONFIG (default_config->curve[channel]),
619
                            GIMP_CONFIG (config->curve[channel]),
620 621
                            0);

622
          g_object_set (config->curve[channel],
623 624 625
                        "curve-type", curve_type,
                        NULL);

626
          g_object_thaw_notify (G_OBJECT (config->curve[channel]));
627 628 629
        }
      else
        {
630
          gimp_curve_reset (config->curve[channel], FALSE);
631
        }
632
    }
633 634
}

635
static gboolean
636 637 638
gimp_curves_tool_settings_import (GimpFilterTool  *filter_tool,
                                  GInputStream    *input,
                                  GError         **error)
639
{
640
  GimpCurvesConfig *config = GIMP_CURVES_CONFIG (filter_tool->config);
641 642
  gchar             header[64];
  gsize             bytes_read;
643

644 645 646
  if (! g_input_stream_read_all (input, header, sizeof (header),
                                 &bytes_read, NULL, error) ||
      bytes_read != sizeof (header))
647
    {
648
      g_prefix_error (error, _("Could not read header: "));
649 650 651
      return FALSE;
    }

652
  g_seekable_seek (G_SEEKABLE (input), 0, G_SEEK_SET, NULL, NULL);
653

654
  if (g_str_has_prefix (header, "# GIMP Curves File\n"))
655
    return gimp_curves_config_load_cruft (config, input, error);
656

657 658 659
  return GIMP_FILTER_TOOL_CLASS (parent_class)->settings_import (filter_tool,
                                                                 input,
                                                                 error);
660 661 662
}

static gboolean
663 664 665
gimp_curves_tool_settings_export (GimpFilterTool  *filter_tool,
                                  GOutputStream   *output,
                                  GError         **error)
666
{
667 668
  GimpCurvesTool   *tool   = GIMP_CURVES_TOOL (filter_tool);
  GimpCurvesConfig *config = GIMP_CURVES_CONFIG (filter_tool->config);
669

670
  if (tool->export_old_format)
671
    return gimp_curves_config_save_cruft (config, output, error);
672

673 674 675
  return GIMP_FILTER_TOOL_CLASS (parent_class)->settings_export (filter_tool,
                                                                 output,
                                                                 error);
676 677 678 679 680 681 682 683 684
}

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

686 687 688 689 690 691 692 693
  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);
694

695 696 697
  g_signal_connect (button, "toggled",
                    G_CALLBACK (gimp_toggle_button_update),
                    &tool->export_old_format);
698
}
699

Elliot Lee's avatar
Elliot Lee committed
700
static void
701
gimp_curves_tool_update_channel (GimpCurvesTool *tool)
Elliot Lee's avatar
Elliot Lee committed
702
{
703 704 705
  GimpFilterTool       *filter_tool = GIMP_FILTER_TOOL (tool);
  GimpCurvesConfig     *config      = GIMP_CURVES_CONFIG (filter_tool->config);
  GimpCurve            *curve       = config->curve[config->channel];
706
  GimpHistogramChannel  channel;
707

708 709
  gimp_int_combo_box_set_active (GIMP_INT_COMBO_BOX (tool->channel_menu),
                                 config->channel);
710

711
  switch (config->channel)
712
    {
713 714 715 716 717 718 719
      guchar r[256];
      guchar g[256];
      guchar b[256];

    case GIMP_HISTOGRAM_VALUE:
    case GIMP_HISTOGRAM_ALPHA:
    case GIMP_HISTOGRAM_RGB:
720
    case GIMP_HISTOGRAM_LUMINANCE:
721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748
      gimp_curve_get_uchar (curve, sizeof (r), r);

      gimp_color_bar_set_buffers (GIMP_COLOR_BAR (tool->xrange),
                                  r, r, r);
      break;

    case GIMP_HISTOGRAM_RED:
    case GIMP_HISTOGRAM_GREEN:
    case GIMP_HISTOGRAM_BLUE:
      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);

      gimp_color_bar_set_buffers (GIMP_COLOR_BAR (tool->xrange),
                                  r, g, b);
      break;
    }

  gimp_histogram_view_set_channel (GIMP_HISTOGRAM_VIEW (tool->graph),
                                   config->channel);
  gimp_curve_view_set_xpos (GIMP_CURVE_VIEW (tool->graph),
                            tool->picked_color[config->channel]);

  gimp_color_bar_set_channel (GIMP_COLOR_BAR (tool->yrange),
                              config->channel);
749

750
  gimp_curve_view_remove_all_backgrounds (GIMP_CURVE_VIEW (tool->graph));
751

752 753 754 755
  for (channel = GIMP_HISTOGRAM_VALUE;
       channel <= GIMP_HISTOGRAM_ALPHA;
       channel++)
    {
756 757 758 759 760
      GimpRGB  curve_color;
      gboolean has_color;

      has_color = curves_get_channel_color (tool->graph, channel, &curve_color);

761
      if (channel == config->channel)
762
        {
763
          gimp_curve_view_set_curve (GIMP_CURVE_VIEW (tool->graph), curve,
764
                                     has_color ? &curve_color : NULL);
765
        }
766 767 768 769
      else
        {
          gimp_curve_view_add_background (GIMP_CURVE_VIEW (tool->graph),
                                          config->curve[channel],
770
                                          has_color ? &curve_color : NULL);
771 772
        }
    }
773

774 775 776
  gimp_int_combo_box_set_active (GIMP_INT_COMBO_BOX (tool->curve_type),
                                 curve->curve_type);
}
Elliot Lee's avatar
Elliot Lee committed
777

778 779 780 781 782 783 784
static void
gimp_curves_tool_config_notify (GObject        *object,
                                GParamSpec     *pspec,
                                GimpCurvesTool *tool)
{
  GimpCurvesConfig *config = GIMP_CURVES_CONFIG (object);
  GimpCurve        *curve  = config->curve[config->channel];
785

786 787
  if (! tool->xrange)
    return;
788

789 790 791
  if (! strcmp (pspec->name, "channel"))
    {
      gimp_curves_tool_update_channel (GIMP_CURVES_TOOL (tool));
792 793 794
    }
  else if (! strcmp (pspec->name, "curve"))
    {
795 796
      gimp_int_combo_box_set_active (GIMP_INT_COMBO_BOX (tool->curve_type),
                                     curve->curve_type);
797 798
    }
}
799

800 801 802 803
static void
curves_channel_callback (GtkWidget      *widget,
                         GimpCurvesTool *tool)
{
804 805
  GimpFilterTool   *filter_tool = GIMP_FILTER_TOOL (tool);
  GimpCurvesConfig *config      = GIMP_CURVES_CONFIG (filter_tool->config);
806 807 808 809 810 811 812 813
  gint              value;

  if (gimp_int_combo_box_get_active (GIMP_INT_COMBO_BOX (widget), &value) &&
      config->channel != value)
    {
      g_object_set (config,
                    "channel", value,
                    NULL);
814
    }
Elliot Lee's avatar
Elliot Lee committed
815 816 817
}

static void
818
curves_channel_reset_callback (GtkWidget      *widget,
819
                               GimpCurvesTool *tool)
Elliot Lee's avatar
Elliot Lee committed
820
{
821 822
  GimpFilterTool   *filter_tool = GIMP_FILTER_TOOL (tool);
  GimpCurvesConfig *config      = GIMP_CURVES_CONFIG (filter_tool->config);
823 824

  gimp_curve_reset (config->curve[config->channel], FALSE);
Elliot Lee's avatar
Elliot Lee committed
825 826
}

827
static gboolean
828 829
curves_menu_sensitivity (gint      value,
                         gpointer  data)
830
{
831
  GimpDrawable         *drawable = GIMP_FILTER_TOOL (data)->drawable;
832
  GimpHistogramChannel  channel  = value;
Sven Neumann's avatar
Sven Neumann committed
833

834 835 836
  if (!drawable)
    return FALSE;

837 838 839 840
  switch (channel)
    {
    case GIMP_HISTOGRAM_VALUE:
      return TRUE;
841

842 843 844
    case GIMP_HISTOGRAM_RED:
    case GIMP_HISTOGRAM_GREEN:
    case GIMP_HISTOGRAM_BLUE:
845
      return gimp_drawable_is_rgb (drawable);
Michael Natterer's avatar