gimpcurvestool.c 31.5 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
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 gchar    * gimp_curves_tool_get_operation   (GimpFilterTool       *filter_tool,
84
                                                    gchar               **description);
85 86
static void       gimp_curves_tool_dialog          (GimpFilterTool       *filter_tool);
static void       gimp_curves_tool_reset           (GimpFilterTool       *filter_tool);
87 88 89
static void       gimp_curves_tool_config_notify   (GimpFilterTool       *filter_tool,
                                                    GimpConfig           *config,
                                                    const GParamSpec     *pspec);
90 91 92 93 94 95
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);
Ell's avatar
Ell committed
96 97 98 99 100 101
static void       gimp_curves_tool_color_picked    (GimpFilterTool       *filter_tool,
                                                    gpointer              identifier,
                                                    gdouble               x,
                                                    gdouble               y,
                                                    const Babl           *sample_format,
                                                    const GimpRGB        *color);
102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119

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

120 121 122
static gboolean   curves_get_channel_color         (GtkWidget            *widget,
                                                    GimpHistogramChannel  channel,
                                                    GimpRGB              *color);
123 124 125


G_DEFINE_TYPE (GimpCurvesTool, gimp_curves_tool, GIMP_TYPE_FILTER_TOOL)
126 127

#define parent_class gimp_curves_tool_parent_class
128

Michael Natterer's avatar
Michael Natterer committed
129

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

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

149 150 151

/*  private functions  */

Michael Natterer's avatar
Michael Natterer committed
152 153 154
static void
gimp_curves_tool_class_init (GimpCurvesToolClass *klass)
{
155 156
  GimpToolClass       *tool_class        = GIMP_TOOL_CLASS (klass);
  GimpFilterToolClass *filter_tool_class = GIMP_FILTER_TOOL_CLASS (klass);
157

158 159 160 161
  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;
162

163 164 165
  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;
166
  filter_tool_class->config_notify   = gimp_curves_tool_config_notify;
167 168
  filter_tool_class->settings_import = gimp_curves_tool_settings_import;
  filter_tool_class->settings_export = gimp_curves_tool_settings_export;
Ell's avatar
Ell committed
169
  filter_tool_class->color_picked    = gimp_curves_tool_color_picked;
Michael Natterer's avatar
Michael Natterer committed
170 171 172
}

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

177 178
  for (i = 0; i < G_N_ELEMENTS (tool->picked_color); i++)
    tool->picked_color[i] = -1.0;
Michael Natterer's avatar
Michael Natterer committed
179 180
}

181
static gboolean
182 183 184
gimp_curves_tool_initialize (GimpTool     *tool,
                             GimpDisplay  *display,
                             GError      **error)
Michael Natterer's avatar
Michael Natterer committed
185
{
Ell's avatar
Ell committed
186 187 188 189
  GimpCurvesTool *c_tool      = GIMP_CURVES_TOOL (tool);
  GimpFilterTool *filter_tool = GIMP_FILTER_TOOL (tool);
  GimpImage      *image       = gimp_display_get_image (display);
  GimpDrawable   *drawable    = gimp_image_get_active_drawable (image);
190
  GimpHistogram  *histogram;
191

192 193 194 195
  if (! GIMP_TOOL_CLASS (parent_class)->initialize (tool, display, error))
    {
      return FALSE;
    }
196

197
  histogram = gimp_histogram_new (FALSE);
198
  gimp_drawable_calculate_histogram (drawable, histogram, FALSE);
199
  gimp_histogram_view_set_background (GIMP_HISTOGRAM_VIEW (c_tool->graph),
200
                                      histogram);
201
  g_object_unref (histogram);
202

203
  if (gimp_drawable_get_component_type (drawable) == GIMP_COMPONENT_TYPE_U8)
204 205 206 207 208 209
    {
      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
    {
210 211
      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);
212 213
    }

Ell's avatar
Ell committed
214 215 216
  /*  always pick colors  */
  gimp_filter_tool_enable_color_picking (filter_tool, NULL, FALSE);

217
  return TRUE;
Michael Natterer's avatar
Michael Natterer committed
218 219 220
}

static void
221
gimp_curves_tool_button_release (GimpTool              *tool,
222
                                 const GimpCoords      *coords,
223 224 225 226
                                 guint32                time,
                                 GdkModifierType        state,
                                 GimpButtonReleaseType  release_type,
                                 GimpDisplay           *display)
Michael Natterer's avatar
Michael Natterer committed
227
{
228 229 230
  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
231

232
  if (state & gimp_get_extend_selection_mask ())
Michael Natterer's avatar
Michael Natterer committed
233
    {
234
      GimpCurve *curve = config->curve[config->channel];
235
      gdouble    value = c_tool->picked_color[config->channel];
236
      gint       closest;
237

238
      closest = gimp_curve_get_closest_point (curve, value);
239

240
      gimp_curve_view_set_selected (GIMP_CURVE_VIEW (c_tool->graph),
241
                                    closest);
242

243
      gimp_curve_set_point (curve, closest,
244
                            value, gimp_curve_map_value (curve, value));
Michael Natterer's avatar
Michael Natterer committed
245
    }
246
  else if (state & gimp_get_toggle_behavior_mask ())
Michael Natterer's avatar
Michael Natterer committed
247
    {
248
      GimpHistogramChannel channel;
249

250 251 252
      for (channel = GIMP_HISTOGRAM_VALUE;
           channel <= GIMP_HISTOGRAM_ALPHA;
           channel++)
253
        {
254 255
          GimpCurve *curve = config->curve[channel];
          gdouble    value = c_tool->picked_color[channel];
256
          gint       closest;
257

258 259 260
          if (value != -1)
            {
              closest = gimp_curve_get_closest_point (curve, value);
261

262 263
              gimp_curve_view_set_selected (GIMP_CURVE_VIEW (c_tool->graph),
                                            closest);
264

265 266 267
              gimp_curve_set_point (curve, closest,
                                    value, gimp_curve_map_value (curve, value));
            }
268
        }
Michael Natterer's avatar
Michael Natterer committed
269 270
    }

271
  /*  chain up to halt the tool */
272 273
  GIMP_TOOL_CLASS (parent_class)->button_release (tool, coords, time, state,
                                                  release_type, display);
Michael Natterer's avatar
Michael Natterer committed
274 275
}

276
static gboolean
277 278
gimp_curves_tool_key_press (GimpTool    *tool,
                            GdkEventKey *kevent,
279
                            GimpDisplay *display)
280
{
281 282
  GimpCurvesTool *c_tool = GIMP_CURVES_TOOL (tool);

283 284 285 286 287
  if (tool->display && c_tool->graph)
    {
      if (gtk_widget_event (c_tool->graph, (GdkEvent *) kevent))
        return TRUE;
    }
288 289

  return GIMP_TOOL_CLASS (parent_class)->key_press (tool, kevent, display);
290 291 292
}

static void
293 294 295 296 297
gimp_curves_tool_oper_update (GimpTool         *tool,
                              const GimpCoords *coords,
                              GdkModifierType   state,
                              gboolean          proximity,
                              GimpDisplay      *display)
298
{
299 300
  if (gimp_filter_tool_on_guide (GIMP_FILTER_TOOL (tool),
                                 coords, display))
301
    {
302 303
      GIMP_TOOL_CLASS (parent_class)->oper_update (tool, coords, state, proximity,
                                                   display);
304
    }
305 306
  else
    {
307 308 309 310
      GimpColorPickMode  mode;
      gchar             *status      = NULL;
      GdkModifierType    extend_mask = gimp_get_extend_selection_mask ();
      GdkModifierType    toggle_mask = gimp_get_toggle_behavior_mask ();
311

312
      gimp_tool_pop_status (tool, display);
313

314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332
      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);
        }
333

334 335 336 337 338 339 340
      GIMP_COLOR_TOOL (tool)->pick_mode = mode;

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

      g_free (status);
    }
341 342
}

343
static gchar *
344
gimp_curves_tool_get_operation (GimpFilterTool  *filter_tool,
345
                                gchar          **description)
346
{
347
  *description = g_strdup (_("Adjust Color Curves"));
348

349
  return g_strdup ("gimp:curves");
350
}
BST 1999 Andy Thomas's avatar
BST 1999 Andy Thomas committed
351

352

353 354 355
/*******************/
/*  Curves dialog  */
/*******************/
Elliot Lee's avatar
Elliot Lee committed
356

357
static void
358
gimp_curves_tool_dialog (GimpFilterTool *filter_tool)
Elliot Lee's avatar
Elliot Lee committed
359
{
360 361 362
  GimpCurvesTool   *tool         = GIMP_CURVES_TOOL (filter_tool);
  GimpToolOptions  *tool_options = GIMP_TOOL_GET_OPTIONS (filter_tool);
  GimpCurvesConfig *config       = GIMP_CURVES_CONFIG (filter_tool->config);
363
  GtkListStore     *store;
364
  GtkWidget        *main_vbox;
365
  GtkWidget        *frame_vbox;
366 367 368 369
  GtkWidget        *vbox;
  GtkWidget        *hbox;
  GtkWidget        *hbox2;
  GtkWidget        *label;
370
  GtkWidget        *main_frame;
371 372 373 374
  GtkWidget        *frame;
  GtkWidget        *table;
  GtkWidget        *button;
  GtkWidget        *bar;
375
  GtkWidget        *combo;
376

377
  g_signal_connect (filter_tool->settings_box, "file-dialog-setup",
378
                    G_CALLBACK (gimp_curves_tool_export_setup),
379
                    filter_tool);
380

381
  main_vbox = gimp_filter_tool_dialog_get_vbox (filter_tool);
Elliot Lee's avatar
Elliot Lee committed
382

383
  /*  The combo box for selecting channels  */
384 385 386 387
  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
388
  hbox = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 6);
389
  gtk_frame_set_label_widget (GTK_FRAME (main_frame), hbox);
390 391
  gtk_widget_show (hbox);

392
  label = gtk_label_new_with_mnemonic (_("Cha_nnel:"));
393 394 395
  gimp_label_set_attributes (GTK_LABEL (label),
                             PANGO_ATTR_WEIGHT, PANGO_WEIGHT_BOLD,
                             -1);
396 397 398
  gtk_box_pack_start (GTK_BOX (hbox), label, FALSE, FALSE, 0);
  gtk_widget_show (label);

399 400 401
  store = gimp_enum_store_new_with_range (GIMP_TYPE_HISTOGRAM_CHANNEL,
                                          GIMP_HISTOGRAM_VALUE,
                                          GIMP_HISTOGRAM_ALPHA);
402 403
  tool->channel_menu =
    gimp_enum_combo_box_new_with_model (GIMP_ENUM_STORE (store));
404 405
  g_object_unref (store);

406 407 408
  g_object_add_weak_pointer (G_OBJECT (tool->channel_menu),
                             (gpointer) &tool->channel_menu);

409 410
  gimp_int_combo_box_set_active (GIMP_INT_COMBO_BOX (tool->channel_menu),
                                 config->channel);
411 412
  gimp_enum_combo_box_set_icon_prefix (GIMP_ENUM_COMBO_BOX (tool->channel_menu),
                                       "gimp-channel");
413 414
  gimp_int_combo_box_set_sensitivity (GIMP_INT_COMBO_BOX (tool->channel_menu),
                                      curves_menu_sensitivity, filter_tool, NULL);
415 416
  gtk_box_pack_start (GTK_BOX (hbox), tool->channel_menu, FALSE, FALSE, 0);
  gtk_widget_show (tool->channel_menu);
417

418 419 420
  g_signal_connect (tool->channel_menu, "changed",
                    G_CALLBACK (curves_channel_callback),
                    tool);
421

422
  gtk_label_set_mnemonic_widget (GTK_LABEL (label), tool->channel_menu);
423

424
  button = gtk_button_new_with_mnemonic (_("R_eset Channel"));
425
  gtk_box_pack_start (GTK_BOX (hbox), button, FALSE, FALSE, 0);
426
  gtk_widget_show (button);
427

428
  g_signal_connect (button, "clicked",
429
                    G_CALLBACK (curves_channel_reset_callback),
430
                    tool);
Elliot Lee's avatar
Elliot Lee committed
431

432
  /*  The histogram scale radio buttons  */
433 434 435
  hbox2 = gimp_prop_enum_icon_box_new (G_OBJECT (tool_options),
                                       "histogram-scale", "gimp-histogram",
                                       0, 0);
436 437
  gtk_box_pack_end (GTK_BOX (hbox), hbox2, FALSE, FALSE, 0);
  gtk_widget_show (hbox2);
438

439 440 441 442
  frame_vbox = gtk_box_new (GTK_ORIENTATION_VERTICAL, 4);
  gtk_container_add (GTK_CONTAINER (main_frame), frame_vbox);
  gtk_widget_show (frame_vbox);

443
  /*  The table for the color bars and the graph  */
Elliot Lee's avatar
Elliot Lee committed
444
  table = gtk_table_new (2, 2, FALSE);
445 446
  gtk_table_set_col_spacings (GTK_TABLE (table), 2);
  gtk_table_set_row_spacings (GTK_TABLE (table), 2);
447
  gtk_box_pack_start (GTK_BOX (frame_vbox), table, TRUE, TRUE, 0);
448 449

  /*  The left color bar  */
Michael Natterer's avatar
Michael Natterer committed
450
  vbox = gtk_box_new (GTK_ORIENTATION_VERTICAL, 0);
451
  gtk_table_attach (GTK_TABLE (table), vbox, 0, 1, 0, 1,
452
                    GTK_FILL, GTK_EXPAND | GTK_FILL, 0, 0);
453
  gtk_widget_show (vbox);
Elliot Lee's avatar
Elliot Lee committed
454 455

  frame = gtk_frame_new (NULL);
456
  gtk_frame_set_shadow_type (GTK_FRAME (frame), GTK_SHADOW_IN);
457
  gtk_box_pack_start (GTK_BOX (vbox), frame, TRUE, TRUE, RADIUS);
Elliot Lee's avatar
Elliot Lee committed
458 459
  gtk_widget_show (frame);

460 461 462 463
  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);
464

Elliot Lee's avatar
Elliot Lee committed
465 466
  /*  The curves graph  */
  frame = gtk_frame_new (NULL);
467
  gtk_frame_set_shadow_type (GTK_FRAME (frame), GTK_SHADOW_IN);
Elliot Lee's avatar
Elliot Lee committed
468
  gtk_table_attach (GTK_TABLE (table), frame, 1, 2, 0, 1,
469
                    GTK_EXPAND | GTK_FILL, GTK_EXPAND | GTK_FILL, 0, 0);
470
  gtk_widget_show (frame);
Elliot Lee's avatar
Elliot Lee committed
471

472
  tool->graph = gimp_curve_view_new ();
473 474
  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);
475 476 477 478
  gtk_widget_set_size_request (tool->graph,
                               GRAPH_SIZE + RADIUS * 2,
                               GRAPH_SIZE + RADIUS * 2);
  g_object_set (tool->graph,
479 480 481
                "border-width", RADIUS,
                "subdivisions", 1,
                NULL);
482 483
  gtk_container_add (GTK_CONTAINER (frame), tool->graph);
  gtk_widget_show (tool->graph);
484

485 486 487 488
  g_object_bind_property (G_OBJECT (tool_options), "histogram-scale",
                          G_OBJECT (tool->graph),  "histogram-scale",
                          G_BINDING_SYNC_CREATE |
                          G_BINDING_BIDIRECTIONAL);
489 490

  /*  The bottom color bar  */
Michael Natterer's avatar
Michael Natterer committed
491
  hbox2 = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 0);
492
  gtk_table_attach (GTK_TABLE (table), hbox2, 1, 2, 1, 2,
493
                    GTK_EXPAND | GTK_FILL, GTK_FILL, 0, 0);
494
  gtk_widget_show (hbox2);
495

Elliot Lee's avatar
Elliot Lee committed
496
  frame = gtk_frame_new (NULL);
497
  gtk_frame_set_shadow_type (GTK_FRAME (frame), GTK_SHADOW_IN);
498
  gtk_box_pack_start (GTK_BOX (hbox2), frame, TRUE, TRUE, RADIUS);
Elliot Lee's avatar
Elliot Lee committed
499 500
  gtk_widget_show (frame);

Michael Natterer's avatar
Michael Natterer committed
501 502
  vbox = gtk_box_new (GTK_ORIENTATION_VERTICAL, 0);
  gtk_box_set_homogeneous (GTK_BOX (vbox), TRUE);
503 504
  gtk_container_add (GTK_CONTAINER (frame), vbox);
  gtk_widget_show (vbox);
505

506 507
  tool->xrange = gimp_color_bar_new (GTK_ORIENTATION_HORIZONTAL);
  gtk_widget_set_size_request (tool->xrange, -1, BAR_SIZE / 2);
508
  gtk_box_pack_start (GTK_BOX (vbox), tool->xrange, TRUE, TRUE, 0);
509
  gtk_widget_show (tool->xrange);
510

511
  bar = gimp_color_bar_new (GTK_ORIENTATION_HORIZONTAL);
512
  gtk_box_pack_start (GTK_BOX (vbox), bar, TRUE, TRUE, 0);
513 514
  gtk_widget_show (bar);

515
  gtk_widget_show (table);
Elliot Lee's avatar
Elliot Lee committed
516

Michael Natterer's avatar
Michael Natterer committed
517
  hbox = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 6);
518
  gtk_box_pack_end (GTK_BOX (frame_vbox), hbox, FALSE, FALSE, 0);
519
  gtk_widget_show (hbox);
520

521 522 523
  label = gtk_label_new_with_mnemonic (_("Curve _type:"));
  gtk_box_pack_start (GTK_BOX (hbox), label, FALSE, FALSE, 0);
  gtk_widget_show (label);
524

525
  tool->curve_type = combo = gimp_enum_combo_box_new (GIMP_TYPE_CURVE_TYPE);
526 527
  gimp_enum_combo_box_set_icon_prefix (GIMP_ENUM_COMBO_BOX (combo),
                                       "gimp-curve");
528 529 530 531 532
  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
533 534

  gtk_label_set_mnemonic_widget (GTK_LABEL (label), combo);
535 536

  gimp_curves_tool_update_channel (tool);
Elliot Lee's avatar
Elliot Lee committed
537 538
}

539
static void
540
gimp_curves_tool_reset (GimpFilterTool *filter_tool)
541
{
542
  GimpCurvesConfig     *config = GIMP_CURVES_CONFIG (filter_tool->config);
543
  GimpCurvesConfig     *default_config;
544
  GimpHistogramChannel  channel;
545

546
  default_config = GIMP_CURVES_CONFIG (filter_tool->default_config);
547

548 549 550 551
  for (channel = GIMP_HISTOGRAM_VALUE;
       channel <= GIMP_HISTOGRAM_ALPHA;
       channel++)
    {
552 553
      if (default_config)
        {
554
          GimpCurveType curve_type = config->curve[channel]->curve_type;
555

556
          g_object_freeze_notify (G_OBJECT (config->curve[channel]));
557 558

          gimp_config_copy (GIMP_CONFIG (default_config->curve[channel]),
559
                            GIMP_CONFIG (config->curve[channel]),
560 561
                            0);

562
          g_object_set (config->curve[channel],
563 564 565
                        "curve-type", curve_type,
                        NULL);

566
          g_object_thaw_notify (G_OBJECT (config->curve[channel]));
567 568 569
        }
      else
        {
570
          gimp_curve_reset (config->curve[channel], FALSE);
571
        }
572
    }
573 574
}

575 576 577 578 579 580 581 582 583 584 585 586
static void
gimp_curves_tool_config_notify (GimpFilterTool   *filter_tool,
                                GimpConfig       *config,
                                const GParamSpec *pspec)
{
  GimpCurvesTool   *curves_tool   = GIMP_CURVES_TOOL (filter_tool);
  GimpCurvesConfig *curves_config = GIMP_CURVES_CONFIG (config);
  GimpCurve        *curve         = curves_config->curve[curves_config->channel];

  GIMP_FILTER_TOOL_CLASS (parent_class)->config_notify (filter_tool,
                                                        config, pspec);

587
  if (! curves_tool->channel_menu)
588 589 590 591 592 593 594 595 596 597 598 599 600
    return;

  if (! strcmp (pspec->name, "channel"))
    {
      gimp_curves_tool_update_channel (GIMP_CURVES_TOOL (filter_tool));
    }
  else if (! strcmp (pspec->name, "curve"))
    {
      gimp_int_combo_box_set_active (GIMP_INT_COMBO_BOX (curves_tool->curve_type),
                                     curve->curve_type);
    }
}

601
static gboolean
602 603 604
gimp_curves_tool_settings_import (GimpFilterTool  *filter_tool,
                                  GInputStream    *input,
                                  GError         **error)
605
{
606
  GimpCurvesConfig *config = GIMP_CURVES_CONFIG (filter_tool->config);
607 608
  gchar             header[64];
  gsize             bytes_read;
609

610 611 612
  if (! g_input_stream_read_all (input, header, sizeof (header),
                                 &bytes_read, NULL, error) ||
      bytes_read != sizeof (header))
613
    {
614
      g_prefix_error (error, _("Could not read header: "));
615 616 617
      return FALSE;
    }

618
  g_seekable_seek (G_SEEKABLE (input), 0, G_SEEK_SET, NULL, NULL);
619

620
  if (g_str_has_prefix (header, "# GIMP Curves File\n"))
621
    return gimp_curves_config_load_cruft (config, input, error);
622

623 624 625
  return GIMP_FILTER_TOOL_CLASS (parent_class)->settings_import (filter_tool,
                                                                 input,
                                                                 error);
626 627 628
}

static gboolean
629 630 631
gimp_curves_tool_settings_export (GimpFilterTool  *filter_tool,
                                  GOutputStream   *output,
                                  GError         **error)
632
{
633 634
  GimpCurvesTool   *tool   = GIMP_CURVES_TOOL (filter_tool);
  GimpCurvesConfig *config = GIMP_CURVES_CONFIG (filter_tool->config);
635

636
  if (tool->export_old_format)
637
    return gimp_curves_config_save_cruft (config, output, error);
638

639 640 641
  return GIMP_FILTER_TOOL_CLASS (parent_class)->settings_export (filter_tool,
                                                                 output,
                                                                 error);
642 643
}

644 645 646 647 648 649 650 651
static void
gimp_curves_tool_color_picked (GimpFilterTool *filter_tool,
                               gpointer        identifier,
                               gdouble         x,
                               gdouble         y,
                               const Babl     *sample_format,
                               const GimpRGB  *color)
{
652 653 654
  GimpCurvesTool   *tool     = GIMP_CURVES_TOOL (filter_tool);
  GimpCurvesConfig *config   = GIMP_CURVES_CONFIG (filter_tool->config);
  GimpDrawable     *drawable = GIMP_TOOL (tool)->drawable;
655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671

  tool->picked_color[GIMP_HISTOGRAM_RED]   = color->r;
  tool->picked_color[GIMP_HISTOGRAM_GREEN] = color->g;
  tool->picked_color[GIMP_HISTOGRAM_BLUE]  = color->b;

  if (gimp_drawable_has_alpha (drawable))
    tool->picked_color[GIMP_HISTOGRAM_ALPHA] = color->a;
  else
    tool->picked_color[GIMP_HISTOGRAM_ALPHA] = -1;

  tool->picked_color[GIMP_HISTOGRAM_VALUE] = MAX (MAX (color->r, color->g),
                                                  color->b);

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

672 673 674 675 676 677 678
static void
gimp_curves_tool_export_setup (GimpSettingsBox      *settings_box,
                               GtkFileChooserDialog *dialog,
                               gboolean              export,
                               GimpCurvesTool       *tool)
{
  GtkWidget *button;
679

680 681 682 683 684 685 686 687
  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);
688

689 690 691
  g_signal_connect (button, "toggled",
                    G_CALLBACK (gimp_toggle_button_update),
                    &tool->export_old_format);
692
}
693

Elliot Lee's avatar
Elliot Lee committed
694
static void
695
gimp_curves_tool_update_channel (GimpCurvesTool *tool)
Elliot Lee's avatar
Elliot Lee committed
696
{
697 698 699
  GimpFilterTool       *filter_tool = GIMP_FILTER_TOOL (tool);
  GimpCurvesConfig     *config      = GIMP_CURVES_CONFIG (filter_tool->config);
  GimpCurve            *curve       = config->curve[config->channel];
700
  GimpHistogramChannel  channel;
701

702 703
  gimp_int_combo_box_set_active (GIMP_INT_COMBO_BOX (tool->channel_menu),
                                 config->channel);
704

705
  switch (config->channel)
706
    {
707 708 709 710 711 712 713
      guchar r[256];
      guchar g[256];
      guchar b[256];

    case GIMP_HISTOGRAM_VALUE:
    case GIMP_HISTOGRAM_ALPHA:
    case GIMP_HISTOGRAM_RGB:
714
    case GIMP_HISTOGRAM_LUMINANCE:
715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742
      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);
743

744
  gimp_curve_view_remove_all_backgrounds (GIMP_CURVE_VIEW (tool->graph));
745

746 747 748 749
  for (channel = GIMP_HISTOGRAM_VALUE;
       channel <= GIMP_HISTOGRAM_ALPHA;
       channel++)
    {
750 751 752 753 754
      GimpRGB  curve_color;
      gboolean has_color;

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

755
      if (channel == config->channel)
756
        {
757
          gimp_curve_view_set_curve (GIMP_CURVE_VIEW (tool->graph), curve,
758
                                     has_color ? &curve_color : NULL);
759
        }
760 761 762 763
      else
        {
          gimp_curve_view_add_background (GIMP_CURVE_VIEW (tool->graph),
                                          config->curve[channel],
764
                                          has_color ? &curve_color : NULL);
765 766
        }
    }
767

768 769 770
  gimp_int_combo_box_set_active (GIMP_INT_COMBO_BOX (tool->curve_type),
                                 curve->curve_type);
}
Elliot Lee's avatar
Elliot Lee committed
771

772 773 774 775
static void
curves_channel_callback (GtkWidget      *widget,
                         GimpCurvesTool *tool)
{
776 777
  GimpFilterTool   *filter_tool = GIMP_FILTER_TOOL (tool);
  GimpCurvesConfig *config      = GIMP_CURVES_CONFIG (filter_tool->config);
778 779 780 781 782 783 784 785
  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);
786
    }
Elliot Lee's avatar
Elliot Lee committed
787 788 789
}

static void
790
curves_channel_reset_callback (GtkWidget      *widget,
791
                               GimpCurvesTool *tool)
Elliot Lee's avatar
Elliot Lee committed
792
{
793 794
  GimpFilterTool   *filter_tool = GIMP_FILTER_TOOL (tool);
  GimpCurvesConfig *config      = GIMP_CURVES_CONFIG (filter_tool->config);
795 796

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

799
static gboolean
800 801
curves_menu_sensitivity (gint      value,
                         gpointer  data)
802
{
803
  GimpDrawable         *drawable = GIMP_TOOL (data)->drawable;
804
  GimpHistogramChannel  channel  = value;
Sven Neumann's avatar
Sven Neumann committed
805

806 807 808
  if (!drawable)
    return FALSE;

809 810 811 812
  switch (channel)
    {
    case GIMP_HISTOGRAM_VALUE:
      return TRUE;
813

814 815 816
    case GIMP_HISTOGRAM_RED:
    case GIMP_HISTOGRAM_GREEN:
    case GIMP_HISTOGRAM_BLUE:
817
      return gimp_drawable_is_rgb (drawable);
818

819
    case GIMP_HISTOGRAM_ALPHA:
820
      return gimp_drawable_has_alpha (drawable);
821 822 823

    case GIMP_HISTOGRAM_RGB:
      return FALSE;
824 825 826

    case GIMP_HISTOGRAM_LUMINANCE:
      return FALSE;
827
    }
828

829 830 831
  return FALSE;
}

Elliot Lee's avatar
Elliot Lee committed
832
static void
833
curves_curve_type_callback (GtkWidget      *widget,
834
                            GimpCurvesTool *tool)
Elliot Lee's avatar
Elliot Lee committed
835
{
836
  gint value;
Elliot Lee's avatar
Elliot Lee committed
837

838 839
  if (gimp_int_combo_box_get_active (GIMP_INT_COMBO_BOX (widget), &value))
    {
840 841 842
      GimpFilterTool   *filter_tool = GIMP_FILTER_TOOL (tool);
      GimpCurvesConfig *config      = GIMP_CURVES_CONFIG (filter_tool->config);
      GimpCurveType     curve_type  = value;
Elliot Lee's avatar
Elliot Lee committed
843

844
      if (config->curve[config->channel]->curve_type != curve_type)
845
        gimp_curve_set_curve_type (config->curve[config->channel], curve_type);
846
    }
Elliot Lee's avatar
Elliot Lee committed
847
}
848

849 850 851 852
static gboolean
curves_get_channel_color (GtkWidget            *widget,
                          GimpHistogramChannel  channel,
                          GimpRGB              *color)
853 854 855 856 857 858 859 860 861 862 863
{
  static const 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 }
  };

  if (channel == GIMP_HISTOGRAM_VALUE)
864 865 866 867 868
    return FALSE;

  if (channel == GIMP_HISTOGRAM_ALPHA)
    {
      GtkStyle *style = gtk_widget_get_style (widget);
869

870 871 872 873 874 875 876 877 878 879
      gimp_rgba_set (color,
                     style->text_aa[GTK_STATE_NORMAL].red / 65535.0,
                     style->text_aa[GTK_STATE_NORMAL].green / 65535.0,
                     style->text_aa[GTK_STATE_NORMAL].blue / 65535.0,
                     1.0);
      return TRUE;
    }

  *color = channel_colors[channel];
  return TRUE;
880
}