gimpcurvestool.c 38 KB
Newer Older
Elliot Lee's avatar
Elliot Lee committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
/* The GIMP -- an image manipulation program
 * Copyright (C) 1995 Spencer Kimball and Peter Mattis
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
16
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
Elliot Lee's avatar
Elliot Lee committed
17
 */
Sven Neumann's avatar
Sven Neumann committed
18

19 20 21
#include "config.h"

#include <stdio.h>
22 23
#include <stdlib.h>
#include <string.h>
24

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

27
#include "libgimpcolor/gimpcolor.h"
28
#include "libgimpwidgets/gimpwidgets.h"
29

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

32
#include "config/gimpbaseconfig.h"
33
#include "config/gimpguiconfig.h"
34

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

39
#include "core/gimp.h"
Michael Natterer's avatar
Michael Natterer committed
40
#include "core/gimpdrawable.h"
41
#include "core/gimpdrawable-histogram.h"
Michael Natterer's avatar
Michael Natterer committed
42
#include "core/gimpimage.h"
43
#include "core/gimpimagemap.h"
44
#include "core/gimptoolinfo.h"
Michael Natterer's avatar
Michael Natterer committed
45

46
#include "widgets/gimpcolorbar.h"
Michael Natterer's avatar
Michael Natterer committed
47
#include "widgets/gimpcursor.h"
48
#include "widgets/gimpenumcombobox.h"
Sven Neumann's avatar
Sven Neumann committed
49
#include "widgets/gimpenumstore.h"
50
#include "widgets/gimpenumwidgets.h"
51
#include "widgets/gimphelp-ids.h"
52
#include "widgets/gimphistogramview.h"
53
#include "widgets/gimppropwidgets.h"
Michael Natterer's avatar
Michael Natterer committed
54

55 56
#include "display/gimpdisplay.h"

Michael Natterer's avatar
Michael Natterer committed
57
#include "gimpcurvestool.h"
58
#include "gimphistogramoptions.h"
59
#include "gimptoolcontrol.h"
Sven Neumann's avatar
Sven Neumann committed
60

61
#include "gimp-intl.h"
62

63

64 65
#define XRANGE   (1 << 0)
#define YRANGE   (1 << 1)
66 67
#define GRAPH    (1 << 2)
#define ALL      (XRANGE | YRANGE | GRAPH)
Elliot Lee's avatar
Elliot Lee committed
68

69 70 71 72
#define GRAPH_SIZE    256
#define BAR_SIZE       12
#define RADIUS          3
#define MIN_DISTANCE    8
Elliot Lee's avatar
Elliot Lee committed
73 74


Michael Natterer's avatar
Michael Natterer committed
75 76
/*  local function prototypes  */

77
static void     gimp_curves_tool_class_init     (GimpCurvesToolClass *klass);
78
static void     gimp_curves_tool_init           (GimpCurvesTool      *tool);
79 80 81 82 83 84 85 86 87 88 89 90

static void     gimp_curves_tool_finalize       (GObject          *object);

static gboolean gimp_curves_tool_initialize     (GimpTool         *tool,
                                                 GimpDisplay      *gdisp);
static void     gimp_curves_tool_button_release (GimpTool         *tool,
                                                 GimpCoords       *coords,
                                                 guint32           time,
                                                 GdkModifierType   state,
                                                 GimpDisplay      *gdisp);

static void     gimp_curves_tool_color_picked   (GimpColorTool    *color_tool,
91
                                                 GimpColorPickState pick_state,
92 93 94 95 96 97
                                                 GimpImageType     sample_type,
                                                 GimpRGB          *color,
                                                 gint              color_index);
static void     gimp_curves_tool_map            (GimpImageMapTool *image_map_tool);
static void     gimp_curves_tool_dialog         (GimpImageMapTool *image_map_tool);
static void     gimp_curves_tool_reset          (GimpImageMapTool *image_map_tool);
98 99 100 101
static gboolean gimp_curves_tool_settings_load  (GimpImageMapTool *image_map_tool,
                                                 gpointer          fp);
static gboolean gimp_curves_tool_settings_save  (GimpImageMapTool *image_map_tool,
                                                 gpointer          fp);
102

103
static void     curves_add_point                (GimpCurvesTool   *tool,
104 105
                                                 gint              cchan);

106
static void     curves_update                   (GimpCurvesTool   *tool,
107 108 109
                                                 gint              update);

static void     curves_channel_callback         (GtkWidget        *widget,
110
                                                 GimpCurvesTool   *tool);
111
static void     curves_channel_reset_callback   (GtkWidget        *widget,
112
                                                 GimpCurvesTool   *tool);
113

Sven Neumann's avatar
Sven Neumann committed
114 115 116
static gboolean curves_menu_visible_func        (GtkTreeModel     *model,
                                                 GtkTreeIter      *iter,
                                                 gpointer          data);
117
static void     curves_curve_type_callback      (GtkWidget        *widget,
118
                                                 GimpCurvesTool   *tool);
119 120
static gboolean curves_graph_events             (GtkWidget        *widget,
                                                 GdkEvent         *event,
121
                                                 GimpCurvesTool   *tool);
122 123
static gboolean curves_graph_expose             (GtkWidget        *widget,
                                                 GdkEventExpose   *eevent,
124
                                                 GimpCurvesTool   *tool);
125

Michael Natterer's avatar
Michael Natterer committed
126

127 128
static GimpImageMapToolClass *parent_class = NULL;

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 142
                _("Curves"),
                _("Adjust color curves"),
143
                N_("_Curves..."), NULL,
144
                NULL, GIMP_HELP_TOOL_CURVES,
Nate Summers's avatar
Nate Summers committed
145
                GIMP_STOCK_TOOL_CURVES,
146
                data);
Michael Natterer's avatar
Michael Natterer committed
147 148
}

149
GType
Michael Natterer's avatar
Michael Natterer committed
150 151
gimp_curves_tool_get_type (void)
{
152
  static GType tool_type = 0;
Michael Natterer's avatar
Michael Natterer committed
153 154 155

  if (! tool_type)
    {
156
      static const GTypeInfo tool_info =
Michael Natterer's avatar
Michael Natterer committed
157 158
      {
        sizeof (GimpCurvesToolClass),
159 160 161 162 163 164 165 166
        (GBaseInitFunc) NULL,
        (GBaseFinalizeFunc) NULL,
        (GClassInitFunc) gimp_curves_tool_class_init,
        NULL,           /* class_finalize */
        NULL,           /* class_data     */
        sizeof (GimpCurvesTool),
        0,              /* n_preallocs    */
        (GInstanceInitFunc) gimp_curves_tool_init,
Michael Natterer's avatar
Michael Natterer committed
167 168
      };

169
      tool_type = g_type_register_static (GIMP_TYPE_IMAGE_MAP_TOOL,
170
                                          "GimpCurvesTool",
171
                                          &tool_info, 0);
Michael Natterer's avatar
Michael Natterer committed
172 173 174 175 176
    }

  return tool_type;
}

177 178 179

/*  private functions  */

Michael Natterer's avatar
Michael Natterer committed
180 181 182
static void
gimp_curves_tool_class_init (GimpCurvesToolClass *klass)
{
183 184
  GObjectClass          *object_class;
  GimpToolClass         *tool_class;
185
  GimpColorToolClass    *color_tool_class;
186
  GimpImageMapToolClass *image_map_tool_class;
Michael Natterer's avatar
Michael Natterer committed
187

188 189
  object_class         = G_OBJECT_CLASS (klass);
  tool_class           = GIMP_TOOL_CLASS (klass);
190
  color_tool_class     = GIMP_COLOR_TOOL_CLASS (klass);
191
  image_map_tool_class = GIMP_IMAGE_MAP_TOOL_CLASS (klass);
Michael Natterer's avatar
Michael Natterer committed
192

193
  parent_class = g_type_class_peek_parent (klass);
Michael Natterer's avatar
Michael Natterer committed
194

195 196 197 198 199 200
  object_class->finalize     = gimp_curves_tool_finalize;

  tool_class->initialize     = gimp_curves_tool_initialize;
  tool_class->button_release = gimp_curves_tool_button_release;

  color_tool_class->picked   = gimp_curves_tool_color_picked;
201

202 203 204
  image_map_tool_class->shell_desc        = _("Adjust Color Curves");
  image_map_tool_class->settings_name     = "curves";
  image_map_tool_class->load_dialog_title = _("Load Curves");
205
  image_map_tool_class->load_button_tip   = _("Load curves settings from file");
206
  image_map_tool_class->save_dialog_title = _("Save Curves");
207
  image_map_tool_class->save_button_tip   = _("Save curves settings to file");
208

209 210 211
  image_map_tool_class->map               = gimp_curves_tool_map;
  image_map_tool_class->dialog            = gimp_curves_tool_dialog;
  image_map_tool_class->reset             = gimp_curves_tool_reset;
212

213 214
  image_map_tool_class->settings_load     = gimp_curves_tool_settings_load;
  image_map_tool_class->settings_save     = gimp_curves_tool_settings_save;
Michael Natterer's avatar
Michael Natterer committed
215 216 217
}

static void
218
gimp_curves_tool_init (GimpCurvesTool *tool)
Michael Natterer's avatar
Michael Natterer committed
219
{
220
  gint i;
221

222 223 224
  tool->curves  = g_new0 (Curves, 1);
  tool->lut     = gimp_lut_new ();
  tool->channel = GIMP_HISTOGRAM_VALUE;
225

226
  curves_init (tool->curves);
227

228 229
  for (i = 0; i < G_N_ELEMENTS (tool->col_value); i++)
    tool->col_value[i] = -1;
230

231 232
  tool->cursor_x = -1;
  tool->cursor_y = -1;
Michael Natterer's avatar
Michael Natterer committed
233 234 235
}

static void
236
gimp_curves_tool_finalize (GObject *object)
Michael Natterer's avatar
Michael Natterer committed
237
{
238
  GimpCurvesTool *tool = GIMP_CURVES_TOOL (object);
239

240
  if (tool->curves)
Michael Natterer's avatar
Michael Natterer committed
241
    {
242 243
      g_free (tool->curves);
      tool->curves = NULL;
Michael Natterer's avatar
Michael Natterer committed
244
    }
245
  if (tool->lut)
Michael Natterer's avatar
Michael Natterer committed
246
    {
247 248
      gimp_lut_free (tool->lut);
      tool->lut = NULL;
Michael Natterer's avatar
Michael Natterer committed
249
    }
250
  if (tool->hist)
251
    {
252 253
      gimp_histogram_free (tool->hist);
      tool->hist = NULL;
254
    }
255
  if (tool->cursor_layout)
Michael Natterer's avatar
Michael Natterer committed
256
    {
257 258
      g_object_unref (tool->cursor_layout);
      tool->cursor_layout = NULL;
Michael Natterer's avatar
Michael Natterer committed
259
    }
260
  if (tool->xpos_layout)
261
    {
262 263
      g_object_unref (tool->xpos_layout);
      tool->xpos_layout = NULL;
264
    }
Michael Natterer's avatar
Michael Natterer committed
265

266
  G_OBJECT_CLASS (parent_class)->finalize (object);
Michael Natterer's avatar
Michael Natterer committed
267 268
}

269
static gboolean
270
gimp_curves_tool_initialize (GimpTool    *tool,
271
                             GimpDisplay *gdisp)
Michael Natterer's avatar
Michael Natterer committed
272
{
273
  GimpCurvesTool *c_tool = GIMP_CURVES_TOOL (tool);
274 275
  GimpDrawable   *drawable;

276 277 278
  drawable = gimp_image_active_drawable (gdisp->gimage);

  if (! drawable)
279
    return FALSE;
280

281
  if (gimp_drawable_is_indexed (drawable))
Michael Natterer's avatar
Michael Natterer committed
282
    {
283
      g_message (_("Curves for indexed layers cannot be adjusted."));
284
      return FALSE;
285
    }
Michael Natterer's avatar
Michael Natterer committed
286

287 288 289 290 291 292 293
  if (! c_tool->hist)
    {
      Gimp *gimp = GIMP_TOOL (c_tool)->tool_info->gimp;

      c_tool->hist = gimp_histogram_new (GIMP_BASE_CONFIG (gimp->config));
    }

294
  curves_init (c_tool->curves);
Michael Natterer's avatar
Michael Natterer committed
295

296
  c_tool->channel = GIMP_HISTOGRAM_VALUE;
297 298
  c_tool->color   = gimp_drawable_is_rgb (drawable);
  c_tool->alpha   = gimp_drawable_has_alpha (drawable);
299 300 301 302 303 304

  c_tool->grab_point = -1;
  c_tool->last       = 0;

  GIMP_TOOL_CLASS (parent_class)->initialize (tool, gdisp);

305 306 307 308
  /*  always pick colors  */
  gimp_color_tool_enable (GIMP_COLOR_TOOL (tool),
                          GIMP_COLOR_OPTIONS (tool->tool_info->tool_options));

Sven Neumann's avatar
Sven Neumann committed
309 310
  gimp_enum_combo_box_set_visible (GIMP_ENUM_COMBO_BOX (c_tool->channel_menu),
                                   curves_menu_visible_func, c_tool);
311

312 313
  gimp_int_combo_box_set_active (GIMP_INT_COMBO_BOX (c_tool->channel_menu),
                                 c_tool->channel);
Michael Natterer's avatar
Michael Natterer committed
314

315 316 317
  /* FIXME: hack */
  if (! c_tool->color)
    c_tool->channel = (c_tool->channel == GIMP_HISTOGRAM_ALPHA) ? 1 : 0;
318

319 320 321
  gimp_drawable_calculate_histogram (drawable, c_tool->hist);
  gimp_histogram_view_set_histogram (GIMP_HISTOGRAM_VIEW (c_tool->graph),
                                     c_tool->hist);
322

323 324
  curves_update (c_tool, ALL);

325
  return TRUE;
Michael Natterer's avatar
Michael Natterer committed
326 327 328
}

static void
329 330 331
gimp_curves_tool_button_release (GimpTool        *tool,
                                 GimpCoords      *coords,
                                 guint32          time,
332 333
                                 GdkModifierType  state,
                                 GimpDisplay     *gdisp)
Michael Natterer's avatar
Michael Natterer committed
334
{
335
  GimpCurvesTool *c_tool = GIMP_CURVES_TOOL (tool);
336
  GimpDrawable   *drawable;
Michael Natterer's avatar
Michael Natterer committed
337

338
  drawable = gimp_image_active_drawable (gdisp->gimage);
Michael Natterer's avatar
Michael Natterer committed
339

340
  if (state & GDK_SHIFT_MASK)
Michael Natterer's avatar
Michael Natterer committed
341
    {
342
      curves_add_point (c_tool, c_tool->channel);
343
      curves_calculate_curve (c_tool->curves, c_tool->channel);
Michael Natterer's avatar
Michael Natterer committed
344
    }
345
  else if (state & GDK_CONTROL_MASK)
Michael Natterer's avatar
Michael Natterer committed
346
    {
347
      gint i;
348

349 350
      for (i = 0; i < 5; i++)
        {
351
          curves_add_point (c_tool, i);
352 353
          curves_calculate_curve (c_tool->curves, c_tool->channel);
        }
Michael Natterer's avatar
Michael Natterer committed
354 355
    }

356 357 358
  /*  chain up to halt the tool */
  GIMP_TOOL_CLASS (parent_class)->button_release (tool,
                                                  coords, time, state, gdisp);
Michael Natterer's avatar
Michael Natterer committed
359 360 361
}

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

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

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

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

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

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

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

388
  curves_update (tool, GRAPH);
389
}
BST 1999 Andy Thomas's avatar
BST 1999 Andy Thomas committed
390

391
static void
392
curves_add_point (GimpCurvesTool *tool,
393
                  gint            cchan)
394 395
{
  /* Add point onto the curve */
396 397 398 399
  gint closest_point = 0;
  gint distance;
  gint curvex;
  gint i;
400

401
  switch (tool->curves->curve_type[cchan])
402
    {
403
    case GIMP_CURVE_SMOOTH:
404
      curvex   = tool->col_value[cchan];
405
      distance = G_MAXINT;
406

407
      for (i = 0; i < CURVES_NUM_POINTS; i++)
408 409 410 411 412 413 414 415
        {
          if (tool->curves->points[cchan][i][0] != -1)
            if (abs (curvex - tool->curves->points[cchan][i][0]) < distance)
              {
                distance = abs (curvex - tool->curves->points[cchan][i][0]);
                closest_point = i;
              }
        }
416

417
      if (distance > MIN_DISTANCE)
418
        closest_point = (curvex + 8) / 16;
419

420 421
      tool->curves->points[cchan][closest_point][0] = curvex;
      tool->curves->points[cchan][closest_point][1] = tool->curves->curve[cchan][curvex];
422
      break;
423

424
    case GIMP_CURVE_FREE:
425
      /* do nothing for free form curves */
426
      break;
427
    }
Elliot Lee's avatar
Elliot Lee committed
428 429
}

430 431
static void
gimp_curves_tool_map (GimpImageMapTool *image_map_tool)
Elliot Lee's avatar
Elliot Lee committed
432
{
433
  GimpCurvesTool *tool = GIMP_CURVES_TOOL (image_map_tool);
434

435
  gimp_lut_setup (tool->lut,
436
                  (GimpLutFunc) curves_lut_func,
437
                  tool->curves,
438
                  gimp_drawable_bytes (image_map_tool->drawable));
439

440 441
  gimp_image_map_apply (image_map_tool->image_map,
                        (GimpImageMapApplyFunc) gimp_lut_process_2,
442
                        tool->lut);
Elliot Lee's avatar
Elliot Lee committed
443 444
}

445

446 447 448
/*******************/
/*  Curves dialog  */
/*******************/
Elliot Lee's avatar
Elliot Lee committed
449

450 451
static void
gimp_curves_tool_dialog (GimpImageMapTool *image_map_tool)
Elliot Lee's avatar
Elliot Lee committed
452
{
453
  GimpCurvesTool  *tool = GIMP_CURVES_TOOL (image_map_tool);
454 455
  GimpToolOptions *tool_options;
  GtkWidget       *vbox;
456 457 458 459 460
  GtkWidget       *vbox2;
  GtkWidget       *hbox;
  GtkWidget       *hbox2;
  GtkWidget       *label;
  GtkWidget       *bbox;
461 462 463 464
  GtkWidget       *frame;
  GtkWidget       *menu;
  GtkWidget       *table;
  GtkWidget       *button;
465
  GtkWidget       *bar;
466
  gint             padding;
467

468 469
  tool_options = GIMP_TOOL (tool)->tool_info->tool_options;

470
  vbox = image_map_tool->main_vbox;
Elliot Lee's avatar
Elliot Lee committed
471

472
  /*  The option menu for selecting channels  */
473
  hbox = gtk_hbox_new (FALSE, 6);
474 475 476 477 478 479 480
  gtk_box_pack_start (GTK_BOX (vbox), hbox, FALSE, FALSE, 0);
  gtk_widget_show (hbox);

  label = gtk_label_new (_("Channel:"));
  gtk_box_pack_start (GTK_BOX (hbox), label, FALSE, FALSE, 0);
  gtk_widget_show (label);

481 482 483 484 485 486
  menu = gimp_enum_combo_box_new (GIMP_TYPE_HISTOGRAM_CHANNEL);
  g_signal_connect (menu, "changed",
                    G_CALLBACK (curves_channel_callback),
                    tool);
  gimp_enum_combo_box_set_stock_prefix (GIMP_ENUM_COMBO_BOX (menu),
                                        "gimp-channel");
487 488 489
  gtk_box_pack_start (GTK_BOX (hbox), menu, FALSE, FALSE, 0);
  gtk_widget_show (menu);

490
  tool->channel_menu = menu;
491

492
  button = gtk_button_new_with_mnemonic (_("R_eset channel"));
493
  gtk_box_pack_start (GTK_BOX (hbox), button, FALSE, FALSE, 0);
494
  gtk_widget_show (button);
495

496
  g_signal_connect (button, "clicked",
497
                    G_CALLBACK (curves_channel_reset_callback),
498
                    tool);
Elliot Lee's avatar
Elliot Lee committed
499

500 501 502 503 504
  menu = gimp_prop_enum_stock_box_new (G_OBJECT (tool_options),
                                       "histogram-scale", "gimp-histogram",
                                       0, 0);
  gtk_box_pack_end (GTK_BOX (hbox), menu, FALSE, FALSE, 0);
  gtk_widget_show (menu);
505

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

  /*  The left color bar  */
  vbox2 = gtk_vbox_new (FALSE, 0);
  gtk_table_attach (GTK_TABLE (table), vbox2, 0, 1, 0, 1,
515
                    GTK_FILL, GTK_EXPAND | GTK_FILL, 0, 0);
516
  gtk_widget_show (vbox2);
Elliot Lee's avatar
Elliot Lee committed
517 518

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

523 524 525 526
  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);
527

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

535 536 537 538 539 540 541 542 543
  tool->graph = gimp_histogram_view_new (FALSE);
  gtk_widget_set_size_request (tool->graph,
                               GRAPH_SIZE + RADIUS * 2,
                               GRAPH_SIZE + RADIUS * 2);
  gtk_widget_add_events (tool->graph, (GDK_BUTTON_PRESS_MASK   |
                                       GDK_BUTTON_RELEASE_MASK |
                                       GDK_POINTER_MOTION_MASK |
                                       GDK_LEAVE_NOTIFY_MASK));
  g_object_set (tool->graph,
544 545 546
                "border-width", RADIUS,
                "subdivisions", 1,
                NULL);
547 548 549
  GIMP_HISTOGRAM_VIEW (tool->graph)->light_histogram = TRUE;
  gtk_container_add (GTK_CONTAINER (frame), tool->graph);
  gtk_widget_show (tool->graph);
550

551
  g_signal_connect (tool->graph, "event",
552 553
                    G_CALLBACK (curves_graph_events),
                    tool);
554
  g_signal_connect_after (tool->graph, "expose_event",
555
                          G_CALLBACK (curves_graph_expose),
556
                          tool);
Elliot Lee's avatar
Elliot Lee committed
557

558 559

  gimp_histogram_options_connect_view (GIMP_HISTOGRAM_OPTIONS (tool_options),
560 561 562 563 564
                                       GIMP_HISTOGRAM_VIEW (tool->graph));

  /*  The bottom color bar  */
  hbox2 = gtk_hbox_new (FALSE, 0);
  gtk_table_attach (GTK_TABLE (table), hbox2, 1, 2, 1, 2,
565
                    GTK_EXPAND | GTK_FILL, GTK_FILL, 0, 0);
566
  gtk_widget_show (hbox2);
567

Elliot Lee's avatar
Elliot Lee committed
568
  frame = gtk_frame_new (NULL);
569
  gtk_frame_set_shadow_type (GTK_FRAME (frame), GTK_SHADOW_IN);
570
  gtk_box_pack_start (GTK_BOX (hbox2), frame, TRUE, TRUE, RADIUS);
Elliot Lee's avatar
Elliot Lee committed
571 572
  gtk_widget_show (frame);

573 574 575 576
  vbox2 = gtk_vbox_new (TRUE, 0);
  gtk_container_add (GTK_CONTAINER (frame), vbox2);
  gtk_widget_show (vbox2);

577 578 579 580
  tool->xrange = gimp_color_bar_new (GTK_ORIENTATION_HORIZONTAL);
  gtk_widget_set_size_request (tool->xrange, -1, BAR_SIZE / 2);
  gtk_box_pack_start (GTK_BOX (vbox2), tool->xrange, TRUE, TRUE, 0);
  gtk_widget_show (tool->xrange);
581

582 583 584 585
  bar = gimp_color_bar_new (GTK_ORIENTATION_HORIZONTAL);
  gtk_box_pack_start (GTK_BOX (vbox2), bar, TRUE, TRUE, 0);
  gtk_widget_show (bar);

586
  gtk_widget_show (table);
Elliot Lee's avatar
Elliot Lee committed
587

588

589 590 591
  hbox = gtk_hbox_new (FALSE, 6);
  gtk_box_pack_end (GTK_BOX (vbox), hbox, FALSE, FALSE, 0);
  gtk_widget_show (hbox);
592

593
  /*  Horizontal button box for load / save */
594
  frame = gimp_frame_new (_("All Channels"));
595
  gtk_box_pack_start (GTK_BOX (hbox), frame, FALSE, FALSE, 0);
596 597
  gtk_widget_show (frame);

598 599
  bbox = gtk_hbutton_box_new ();
  gtk_box_set_spacing (GTK_BOX (bbox), 4);
600
  gtk_container_add (GTK_CONTAINER (frame), bbox);
601
  gtk_widget_show (bbox);
602

603 604 605
  gtk_box_pack_start (GTK_BOX (bbox), image_map_tool->load_button,
                      FALSE, FALSE, 0);
  gtk_widget_show (image_map_tool->load_button);
606

607 608 609
  gtk_box_pack_start (GTK_BOX (bbox), image_map_tool->save_button,
                      FALSE, FALSE, 0);
  gtk_widget_show (image_map_tool->save_button);
610 611

  /*  The radio box for selecting the curve type  */
612
  frame = gimp_frame_new (_("Curve Type"));
613 614 615 616
  gtk_box_pack_end (GTK_BOX (hbox), frame, FALSE, FALSE, 0);
  gtk_widget_show (frame);

  hbox = gimp_enum_stock_box_new (GIMP_TYPE_CURVE_TYPE,
617 618 619 620
                                  "gimp-curve", GTK_ICON_SIZE_MENU,
                                  G_CALLBACK (curves_curve_type_callback),
                                  tool,
                                  &tool->curve_type);
621 622 623 624

  gtk_widget_style_get (bbox, "child_internal_pad_x", &padding, NULL);

  gimp_enum_stock_box_set_child_padding (hbox, padding, -1);
625 626 627 628 629 630

  gtk_container_set_border_width (GTK_CONTAINER (hbox), 2);
  gtk_box_set_spacing (GTK_BOX (hbox), 4);

  gtk_container_add (GTK_CONTAINER (frame), hbox);
  gtk_widget_show (hbox);
Elliot Lee's avatar
Elliot Lee committed
631 632
}

633
static void
634
gimp_curves_tool_reset (GimpImageMapTool *image_map_tool)
635
{
636
  GimpCurvesTool       *tool = GIMP_CURVES_TOOL (image_map_tool);
637
  GimpHistogramChannel  channel;
638

639
  tool->grab_point = -1;
640

641 642 643
  for (channel =  GIMP_HISTOGRAM_VALUE;
       channel <= GIMP_HISTOGRAM_ALPHA;
       channel++)
644
    curves_channel_reset (tool->curves, channel);
645

646
  curves_update (tool, XRANGE | GRAPH);
647 648
}

649
static gboolean
650 651
gimp_curves_tool_settings_load (GimpImageMapTool *image_map_tool,
                                gpointer          fp)
652
{
653 654 655 656 657
  GimpCurvesTool *tool = GIMP_CURVES_TOOL (image_map_tool);
  FILE           *file = fp;
  gint            i, j;
  gint            fields;
  gchar           buf[50];
658 659
  gint            index[5][CURVES_NUM_POINTS];
  gint            value[5][CURVES_NUM_POINTS];
660 661 662 663 664 665 666 667 668

  if (! fgets (buf, sizeof (buf), file))
    return FALSE;

  if (strcmp (buf, "# GIMP Curves File\n") != 0)
    return FALSE;

  for (i = 0; i < 5; i++)
    {
669
      for (j = 0; j < CURVES_NUM_POINTS; j++)
670 671 672 673
        {
          fields = fscanf (file, "%d %d ", &index[i][j], &value[i][j]);
          if (fields != 2)
            {
674
              /*  FIXME: should have a helpful error message here  */
675 676 677 678
              g_printerr ("fields != 2");
              return FALSE;
            }
        }
679 680 681 682 683 684
    }

  for (i = 0; i < 5; i++)
    {
      tool->curves->curve_type[i] = GIMP_CURVE_SMOOTH;

685
      for (j = 0; j < CURVES_NUM_POINTS; j++)
686 687 688 689
        {
          tool->curves->points[i][j][0] = index[i][j];
          tool->curves->points[i][j][1] = value[i][j];
        }
690 691 692 693 694 695 696 697
    }

  for (i = 0; i < 5; i++)
    curves_calculate_curve (tool->curves, i);

  curves_update (tool, ALL);

  gimp_int_radio_group_set_active (GTK_RADIO_BUTTON (tool->curve_type),
698
                                   GIMP_CURVE_SMOOTH);
699 700

  return TRUE;
701 702 703
}

static gboolean
704 705
gimp_curves_tool_settings_save (GimpImageMapTool *image_map_tool,
                                gpointer          fp)
706
{
707 708 709 710 711 712 713 714
  GimpCurvesTool *tool = GIMP_CURVES_TOOL (image_map_tool);
  FILE           *file = fp;
  gint            i, j;
  gint32          index;

  for (i = 0; i < 5; i++)
    if (tool->curves->curve_type[i] == GIMP_CURVE_FREE)
      {
715
        /*  pick representative points from the curve
716
            and make them control points  */
717 718 719 720 721 722
        for (j = 0; j <= 8; j++)
          {
            index = CLAMP0255 (j * 32);
            tool->curves->points[i][j * 2][0] = index;
            tool->curves->points[i][j * 2][1] = tool->curves->curve[i][index];
          }
723 724 725 726 727 728
      }

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

  for (i = 0; i < 5; i++)
    {
729
      for (j = 0; j < CURVES_NUM_POINTS; j++)
730
        fprintf (file, "%d %d ",
731 732 733 734 735 736 737
                 tool->curves->points[i][j][0],
                 tool->curves->points[i][j][1]);

      fprintf (file, "\n");
    }

  return TRUE;
738
}
739

740
/* TODO: preview alpha channel stuff correctly.  -- austin, 20/May/99 */
Elliot Lee's avatar
Elliot Lee committed
741
static void
742
curves_update (GimpCurvesTool *tool,
743
               gint            update)
Elliot Lee's avatar
Elliot Lee committed
744
{
745
  GimpHistogramChannel channel;
746

747
  if (tool->color)
748
    {
749
      channel = tool->channel;
750
    }
751
  else
752
    {
753 754
      /* FIXME: hack */
      if (tool->channel == 1)
755
        channel = GIMP_HISTOGRAM_ALPHA;
756
      else
757
        channel = GIMP_HISTOGRAM_VALUE;
758
    }
759

760 761 762
  if (update & GRAPH)
    gtk_widget_queue_draw (tool->graph);

763
  if (update & XRANGE)
Elliot Lee's avatar
Elliot Lee committed
764
    {
765
      switch (channel)
766 767 768
        {
        case GIMP_HISTOGRAM_VALUE:
        case GIMP_HISTOGRAM_ALPHA:
769
        case GIMP_HISTOGRAM_RGB:
770
          gimp_color_bar_set_buffers (GIMP_COLOR_BAR (tool->xrange),
771 772 773
                                      tool->curves->curve[tool->channel],
                                      tool->curves->curve[tool->channel],
                                      tool->curves->curve[tool->channel]);
774
          break;
775

776 777 778
        case GIMP_HISTOGRAM_RED:
        case GIMP_HISTOGRAM_GREEN:
        case GIMP_HISTOGRAM_BLUE:
779 780 781 782
          gimp_color_bar_set_buffers (GIMP_COLOR_BAR (tool->xrange),
                                      tool->curves->curve[GIMP_HISTOGRAM_RED],
                                      tool->curves->curve[GIMP_HISTOGRAM_GREEN],
                                      tool->curves->curve[GIMP_HISTOGRAM_BLUE]);
783 784
          break;
        }
Elliot Lee's avatar
Elliot Lee committed
785
    }
786

Elliot Lee's avatar
Elliot Lee committed
787 788
  if (update & YRANGE)
    {
789
      gimp_color_bar_set_channel (GIMP_COLOR_BAR (tool->yrange), channel);