gimpcurvestool.c 39.6 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
#include <errno.h>
25

26 27 28 29 30
#ifdef __GNUC__
#warning GTK_DISABLE_DEPRECATED
#endif
#undef GTK_DISABLE_DEPRECATED

Sven Neumann's avatar
Sven Neumann committed
31 32
#include <gtk/gtk.h>

33
#include "libgimpmath/gimpmath.h"
34
#include "libgimpbase/gimpbase.h"
35
#include "libgimpcolor/gimpcolor.h"
36
#include "libgimpwidgets/gimpwidgets.h"
37

38
#include "tools-types.h"
Michael Natterer's avatar
Michael Natterer committed
39

40
#include "base/curves.h"
Michael Natterer's avatar
Michael Natterer committed
41 42 43 44 45
#include "base/gimphistogram.h"
#include "base/gimplut.h"

#include "core/gimpdrawable.h"
#include "core/gimpimage.h"
46
#include "core/gimpimagemap.h"
47
#include "core/gimptoolinfo.h"
Michael Natterer's avatar
Michael Natterer committed
48 49

#include "widgets/gimpcursor.h"
50
#include "widgets/gimpenummenu.h"
51
#include "widgets/gimphelp-ids.h"
Michael Natterer's avatar
Michael Natterer committed
52

53 54
#include "display/gimpdisplay.h"

55
#include "gimpcoloroptions.h"
Michael Natterer's avatar
Michael Natterer committed
56
#include "gimpcurvestool.h"
57
#include "gimptoolcontrol.h"
Sven Neumann's avatar
Sven Neumann committed
58

59
#include "gimp-intl.h"
60

61

62 63 64 65
#define XRANGE_TOP     (1 << 0)
#define XRANGE_BOTTOM  (1 << 1)
#define YRANGE         (1 << 2)
#define ALL            (XRANGE_TOP | XRANGE_BOTTOM | YRANGE)
Elliot Lee's avatar
Elliot Lee committed
66

67
/*  NB: take care when changing these values: make sure the curve[] array in
68
 *  base/curves.h is large enough.
69
 */
70 71 72
#define GRAPH_WIDTH    256
#define GRAPH_HEIGHT   256
#define XRANGE_WIDTH   256
73 74
#define XRANGE_HEIGHT   12
#define YRANGE_WIDTH    12
75
#define YRANGE_HEIGHT  256
Elliot Lee's avatar
Elliot Lee committed
76 77 78
#define RADIUS           3
#define MIN_DISTANCE     8

79 80 81 82 83
#define GRAPH_MASK  (GDK_EXPOSURE_MASK       | \
                     GDK_LEAVE_NOTIFY_MASK   | \
		     GDK_POINTER_MOTION_MASK | \
		     GDK_BUTTON_PRESS_MASK   | \
		     GDK_BUTTON_RELEASE_MASK)
Elliot Lee's avatar
Elliot Lee committed
84 85


Michael Natterer's avatar
Michael Natterer committed
86 87 88
/*  local function prototypes  */

static void   gimp_curves_tool_class_init     (GimpCurvesToolClass *klass);
89 90 91 92 93 94 95 96 97 98 99 100
static void   gimp_curves_tool_init           (GimpCurvesTool      *c_tool);

static void   gimp_curves_tool_finalize       (GObject          *object);

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

101 102 103 104
static void   gimp_curves_tool_color_picked   (GimpColorTool    *color_tool,
					       GimpImageType     sample_type,
					       GimpRGB          *color,
					       gint              color_index);
105 106 107 108 109 110 111 112 113 114 115 116 117
static void   gimp_curves_tool_map            (GimpImageMapTool *image_map_tool);
static void   gimp_curves_tool_dialog         (GimpImageMapTool *image_map_tool);
static void   gimp_curves_tool_reset          (GimpImageMapTool *image_map_tool);

static void   curves_add_point                (GimpCurvesTool   *c_tool,
					       gint              x,
					       gint              y,
					       gint              cchan);

static void   curves_update                   (GimpCurvesTool   *c_tool,
                                               gint              update);

static void   curves_channel_callback         (GtkWidget        *widget,
118
                                               GimpCurvesTool   *c_tool);
119
static void   curves_channel_reset_callback   (GtkWidget        *widget,
120
                                               GimpCurvesTool   *c_tool);
121

122
static gboolean curves_set_sensitive_callback (GimpHistogramChannel channel,
123
                                               GimpCurvesTool   *c_tool);
124
static void   curves_curve_type_callback      (GtkWidget        *widget,
125
                                               GimpCurvesTool   *c_tool);
126
static void   curves_load_callback            (GtkWidget        *widget,
127
                                               GimpCurvesTool   *c_tool);
128
static void   curves_save_callback            (GtkWidget        *widget,
129
                                               GimpCurvesTool   *c_tool);
130 131 132
static gint   curves_graph_events             (GtkWidget        *widget,
                                               GdkEvent         *event,
                                               GimpCurvesTool   *c_tool);
133 134 135
static void   curves_graph_expose             (GtkWidget        *widget,
                                               GdkRectangle     *area,
                                               GimpCurvesTool   *c_tool);
136 137

static void       file_dialog_create          (GimpCurvesTool   *c_tool);
138 139 140
static void       file_dialog_response        (GtkWidget        *dialog,
                                               gint              response_id,
                                               GimpCurvesTool   *c_tool);
141 142 143 144 145 146

static gboolean   curves_read_from_file       (GimpCurvesTool   *c_tool,
                                               FILE             *file);
static void       curves_write_to_file        (GimpCurvesTool   *c_tool,
                                               FILE             *file);

Michael Natterer's avatar
Michael Natterer committed
147

148 149
static GimpImageMapToolClass *parent_class = NULL;

Michael Natterer's avatar
Michael Natterer committed
150

151
/*  public functions  */
Michael Natterer's avatar
Michael Natterer committed
152 153

void
Nate Summers's avatar
Nate Summers committed
154
gimp_curves_tool_register (GimpToolRegisterCallback  callback,
155
                           gpointer                  data)
Michael Natterer's avatar
Michael Natterer committed
156
{
Nate Summers's avatar
Nate Summers committed
157
  (* callback) (GIMP_TYPE_CURVES_TOOL,
158 159
                GIMP_TYPE_COLOR_OPTIONS,
                gimp_color_options_gui,
160
                0,
161
                "gimp-curves-tool",
162 163
                _("Curves"),
                _("Adjust color curves"),
164
                N_("/Tools/Color Tools/_Curves..."), NULL,
165
                NULL, GIMP_HELP_TOOL_CURVES,
Nate Summers's avatar
Nate Summers committed
166
                GIMP_STOCK_TOOL_CURVES,
167
                data);
Michael Natterer's avatar
Michael Natterer committed
168 169
}

170
GType
Michael Natterer's avatar
Michael Natterer committed
171 172
gimp_curves_tool_get_type (void)
{
173
  static GType tool_type = 0;
Michael Natterer's avatar
Michael Natterer committed
174 175 176

  if (! tool_type)
    {
177
      static const GTypeInfo tool_info =
Michael Natterer's avatar
Michael Natterer committed
178 179
      {
        sizeof (GimpCurvesToolClass),
180 181 182 183 184 185 186 187
	(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
188 189
      };

190
      tool_type = g_type_register_static (GIMP_TYPE_IMAGE_MAP_TOOL,
191
					  "GimpCurvesTool",
192
                                          &tool_info, 0);
Michael Natterer's avatar
Michael Natterer committed
193 194 195 196 197
    }

  return tool_type;
}

198 199 200

/*  private functions  */

Michael Natterer's avatar
Michael Natterer committed
201 202 203
static void
gimp_curves_tool_class_init (GimpCurvesToolClass *klass)
{
204 205
  GObjectClass          *object_class;
  GimpToolClass         *tool_class;
206
  GimpColorToolClass    *color_tool_class;
207
  GimpImageMapToolClass *image_map_tool_class;
Michael Natterer's avatar
Michael Natterer committed
208

209 210
  object_class         = G_OBJECT_CLASS (klass);
  tool_class           = GIMP_TOOL_CLASS (klass);
211
  color_tool_class     = GIMP_COLOR_TOOL_CLASS (klass);
212
  image_map_tool_class = GIMP_IMAGE_MAP_TOOL_CLASS (klass);
Michael Natterer's avatar
Michael Natterer committed
213

214
  parent_class = g_type_class_peek_parent (klass);
Michael Natterer's avatar
Michael Natterer committed
215

216
  object_class->finalize       = gimp_curves_tool_finalize;
217

218 219 220 221
  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;
222 223 224 225

  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;
Michael Natterer's avatar
Michael Natterer committed
226 227 228
}

static void
229
gimp_curves_tool_init (GimpCurvesTool *c_tool)
Michael Natterer's avatar
Michael Natterer committed
230
{
231
  GimpImageMapTool *image_map_tool = GIMP_IMAGE_MAP_TOOL (c_tool);
232 233
  gint              i;

234
  image_map_tool->shell_desc = _("Adjust Color Curves");
235

236 237 238
  c_tool->curves  = g_new0 (Curves, 1);
  c_tool->lut     = gimp_lut_new ();
  c_tool->channel = GIMP_HISTOGRAM_VALUE;
239 240 241

  curves_init (c_tool->curves);

242
  for (i = 0; i < G_N_ELEMENTS (c_tool->col_value); i++)
243
    c_tool->col_value[i] = -1;
244 245 246

  c_tool->cursor_x = -1;
  c_tool->cursor_y = -1;
Michael Natterer's avatar
Michael Natterer committed
247 248 249
}

static void
250
gimp_curves_tool_finalize (GObject *object)
Michael Natterer's avatar
Michael Natterer committed
251
{
252
  GimpCurvesTool *c_tool = GIMP_CURVES_TOOL (object);
253 254

  if (c_tool->curves)
Michael Natterer's avatar
Michael Natterer committed
255
    {
256
      g_free (c_tool->curves);
257
      c_tool->curves = NULL;
Michael Natterer's avatar
Michael Natterer committed
258
    }
259
  if (c_tool->lut)
Michael Natterer's avatar
Michael Natterer committed
260
    {
261 262
      gimp_lut_free (c_tool->lut);
      c_tool->lut = NULL;
Michael Natterer's avatar
Michael Natterer committed
263
    }
264
  if (c_tool->cursor_layout)
Michael Natterer's avatar
Michael Natterer committed
265
    {
266 267
      g_object_unref (c_tool->cursor_layout);
      c_tool->cursor_layout = NULL;
Michael Natterer's avatar
Michael Natterer committed
268
    }
269 270 271 272 273
  if (c_tool->xpos_layout)
    {
      g_object_unref (c_tool->xpos_layout);
      c_tool->xpos_layout = NULL;
    }
Michael Natterer's avatar
Michael Natterer committed
274

275
  G_OBJECT_CLASS (parent_class)->finalize (object);
Michael Natterer's avatar
Michael Natterer committed
276 277 278
}

static void
279 280
gimp_curves_tool_initialize (GimpTool    *tool,
			     GimpDisplay *gdisp)
Michael Natterer's avatar
Michael Natterer committed
281
{
282
  GimpCurvesTool *c_tool = GIMP_CURVES_TOOL (tool);
283 284
  GimpDrawable   *drawable;

285 286 287 288
  drawable = gimp_image_active_drawable (gdisp->gimage);

  if (! drawable)
    return;
289

290
  if (gimp_drawable_is_indexed (drawable))
Michael Natterer's avatar
Michael Natterer committed
291
    {
292
      g_message (_("Curves for indexed layers cannot be adjusted."));
293 294
      return;
    }
Michael Natterer's avatar
Michael Natterer committed
295

296
  curves_init (c_tool->curves);
Michael Natterer's avatar
Michael Natterer committed
297

298 299 300 301 302 303 304 305
  c_tool->color   = gimp_drawable_is_rgb (drawable);
  c_tool->channel = GIMP_HISTOGRAM_VALUE;

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

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

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

310
  /* set the sensitivity of the channel menu based on the drawable type */
311 312 313
  gimp_int_option_menu_set_sensitive (GTK_OPTION_MENU (c_tool->channel_menu),
                                      (GimpIntOptionMenuSensitivityCallback) curves_set_sensitive_callback,
                                      c_tool);
314 315

  /* set the current selection */
316 317
  gimp_int_option_menu_set_history (GTK_OPTION_MENU (c_tool->channel_menu),
                                    c_tool->channel);
Michael Natterer's avatar
Michael Natterer committed
318

319
  curves_update (c_tool, ALL);
Michael Natterer's avatar
Michael Natterer committed
320 321 322
}

static void
323 324 325 326 327
gimp_curves_tool_button_release (GimpTool        *tool,
                                 GimpCoords      *coords,
                                 guint32          time,
				 GdkModifierType  state,
				 GimpDisplay     *gdisp)
Michael Natterer's avatar
Michael Natterer committed
328
{
329
  GimpCurvesTool *c_tool = GIMP_CURVES_TOOL (tool);
330
  GimpDrawable   *drawable;
Michael Natterer's avatar
Michael Natterer committed
331

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

334
  if (state & GDK_SHIFT_MASK)
Michael Natterer's avatar
Michael Natterer committed
335
    {
336
      curves_add_point (c_tool, coords->x, coords->y, c_tool->channel);
337
      curves_calculate_curve (c_tool->curves, c_tool->channel);
Michael Natterer's avatar
Michael Natterer committed
338
    }
339
  else if (state & GDK_CONTROL_MASK)
Michael Natterer's avatar
Michael Natterer committed
340
    {
341
      gint i;
342

343 344 345 346 347
      for (i = 0; i < 5; i++)
        {
          curves_add_point (c_tool, coords->x, coords->y, i);
          curves_calculate_curve (c_tool->curves, c_tool->channel);
        }
Michael Natterer's avatar
Michael Natterer committed
348 349
    }

350 351 352
  /*  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
353 354 355
}

static void
356 357 358 359
gimp_curves_tool_color_picked (GimpColorTool *color_tool,
			       GimpImageType  sample_type,
			       GimpRGB       *color,
			       gint           color_index)
Michael Natterer's avatar
Michael Natterer committed
360
{
361 362
  GimpCurvesTool *c_tool;
  GimpDrawable   *drawable;
363
  guchar          r, g, b, a;
Michael Natterer's avatar
Michael Natterer committed
364

365 366
  c_tool = GIMP_CURVES_TOOL (color_tool);
  drawable = GIMP_IMAGE_MAP_TOOL (c_tool)->drawable;
367

368
  gimp_rgba_get_uchar (color, &r, &g, &b, &a);
369

370 371 372
  c_tool->col_value[GIMP_HISTOGRAM_RED]   = r;
  c_tool->col_value[GIMP_HISTOGRAM_GREEN] = g;
  c_tool->col_value[GIMP_HISTOGRAM_BLUE]  = b;
373

374 375
  if (gimp_drawable_has_alpha (drawable))
    c_tool->col_value[GIMP_HISTOGRAM_ALPHA] = a;
376

377 378
  if (gimp_drawable_is_indexed (drawable))
    c_tool->col_value[GIMP_HISTOGRAM_ALPHA] = color_index;
379

380
  c_tool->col_value[GIMP_HISTOGRAM_VALUE] = MAX (MAX (r, g), b);
BST 1999 Andy Thomas's avatar
BST 1999 Andy Thomas committed
381

382
  gtk_widget_queue_draw (c_tool->graph);
383
}
BST 1999 Andy Thomas's avatar
BST 1999 Andy Thomas committed
384

385
static void
386 387 388 389
curves_add_point (GimpCurvesTool *c_tool,
		  gint            x,
		  gint            y,
		  gint            cchan)
390 391
{
  /* Add point onto the curve */
392 393 394 395
  gint closest_point = 0;
  gint distance;
  gint curvex;
  gint i;
396

397
  switch (c_tool->curves->curve_type[cchan])
398
    {
399
    case GIMP_CURVE_SMOOTH:
400
      curvex   = c_tool->col_value[cchan];
401
      distance = G_MAXINT;
402

403 404
      for (i = 0; i < 17; i++)
	{
405 406
	  if (c_tool->curves->points[cchan][i][0] != -1)
	    if (abs (curvex - c_tool->curves->points[cchan][i][0]) < distance)
407
	      {
408
		distance = abs (curvex - c_tool->curves->points[cchan][i][0]);
409 410 411
		closest_point = i;
	      }
	}
412

413 414
      if (distance > MIN_DISTANCE)
	closest_point = (curvex + 8) / 16;
415

416 417
      c_tool->curves->points[cchan][closest_point][0] = curvex;
      c_tool->curves->points[cchan][closest_point][1] = c_tool->curves->curve[cchan][curvex];
418
      break;
419

420
    case GIMP_CURVE_FREE:
421
      c_tool->curves->curve[cchan][x] = 255 - y;
422
      break;
423
    }
Elliot Lee's avatar
Elliot Lee committed
424 425
}

426 427
static void
gimp_curves_tool_map (GimpImageMapTool *image_map_tool)
Elliot Lee's avatar
Elliot Lee committed
428
{
429
  GimpCurvesTool *c_tool = GIMP_CURVES_TOOL (image_map_tool);
430

431
  gimp_lut_setup (c_tool->lut,
432 433 434
		  (GimpLutFunc) curves_lut_func,
                  c_tool->curves,
		  gimp_drawable_bytes (image_map_tool->drawable));
435

436 437 438
  gimp_image_map_apply (image_map_tool->image_map,
                        (GimpImageMapApplyFunc) gimp_lut_process_2,
                        c_tool->lut);
Elliot Lee's avatar
Elliot Lee committed
439 440
}

441

442 443 444
/*******************/
/*  Curves dialog  */
/*******************/
Elliot Lee's avatar
Elliot Lee committed
445

446 447
static void
gimp_curves_tool_dialog (GimpImageMapTool *image_map_tool)
Elliot Lee's avatar
Elliot Lee committed
448
{
449
  GimpCurvesTool *c_tool = GIMP_CURVES_TOOL (image_map_tool);
450
  GtkWidget      *hbox;
451
  GtkWidget      *vbox;
452 453
  GtkWidget      *hbbox;
  GtkWidget      *frame;
454
  GtkWidget      *menu;
455 456
  GtkWidget      *table;
  GtkWidget      *button;
457

458 459 460 461 462 463 464 465 466
  hbox = gtk_hbox_new (FALSE, 0);
  gtk_box_pack_start (GTK_BOX (image_map_tool->main_vbox), hbox,
                      FALSE, FALSE, 0);
  gtk_widget_show (hbox);

  vbox = gtk_vbox_new (FALSE, 4);
  gtk_box_pack_start (GTK_BOX (hbox), vbox, TRUE, FALSE, 0);
  gtk_widget_show (vbox);

467 468 469
  table = gtk_table_new (2, 2, FALSE);
  gtk_table_set_col_spacings (GTK_TABLE (table), 4);
  gtk_table_set_row_spacings (GTK_TABLE (table), 2);
470
  gtk_box_pack_start (GTK_BOX (vbox), table, FALSE, FALSE, 0);
471
  gtk_widget_show (table);
Elliot Lee's avatar
Elliot Lee committed
472

473 474
  /*  The option menu for selecting channels  */
  hbox = gtk_hbox_new (FALSE, 4);
475 476 477 478 479 480 481 482 483
  menu = gimp_enum_option_menu_new (GIMP_TYPE_HISTOGRAM_CHANNEL,
                                    G_CALLBACK (curves_channel_callback),
                                    c_tool);
  gimp_enum_option_menu_set_stock_prefix (GTK_OPTION_MENU (menu),
                                          "gimp-channel");
  gtk_box_pack_start (GTK_BOX (hbox), menu, FALSE, FALSE, 0);
  gtk_widget_show (menu);

  c_tool->channel_menu = menu;
484

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

489
  g_signal_connect (button, "clicked",
490 491
                    G_CALLBACK (curves_channel_reset_callback),
                    c_tool);
492

493 494 495
  gimp_table_attach_aligned (GTK_TABLE (table), 0, 0,
                             _("Modify Curves for Channel:"), 1.0, 0.5,
                             hbox, 1, FALSE);
Elliot Lee's avatar
Elliot Lee committed
496 497

  /*  The table for the yrange and the graph  */
498 499 500 501
  hbox = gtk_hbox_new (FALSE, 0);
  gtk_box_pack_start (GTK_BOX (vbox), hbox, FALSE, FALSE, 0);
  gtk_widget_show (hbox);

Elliot Lee's avatar
Elliot Lee committed
502
  table = gtk_table_new (2, 2, FALSE);
503 504
  gtk_table_set_col_spacings (GTK_TABLE (table), 2);
  gtk_table_set_row_spacings (GTK_TABLE (table), 2);
505
  gtk_box_pack_start (GTK_BOX (hbox), table, TRUE, FALSE, 0);
Elliot Lee's avatar
Elliot Lee committed
506 507 508

  /*  The range drawing area  */
  frame = gtk_frame_new (NULL);
509
  gtk_frame_set_shadow_type (GTK_FRAME (frame), GTK_SHADOW_IN);
Elliot Lee's avatar
Elliot Lee committed
510 511 512 513
  gtk_table_attach (GTK_TABLE (table), frame, 0, 1, 0, 1,
		    GTK_EXPAND, GTK_EXPAND, 0, 0);
  gtk_widget_show (frame);

514 515 516 517 518
  c_tool->yrange = gtk_preview_new (GTK_PREVIEW_COLOR);
  gtk_preview_size (GTK_PREVIEW (c_tool->yrange), YRANGE_WIDTH, YRANGE_HEIGHT);
  gtk_container_add (GTK_CONTAINER (frame), c_tool->yrange);
  gtk_widget_show (c_tool->yrange);

Elliot Lee's avatar
Elliot Lee committed
519 520
  /*  The curves graph  */
  frame = gtk_frame_new (NULL);
521
  gtk_frame_set_shadow_type (GTK_FRAME (frame), GTK_SHADOW_IN);
Elliot Lee's avatar
Elliot Lee committed
522
  gtk_table_attach (GTK_TABLE (table), frame, 1, 2, 0, 1,
523 524
		    GTK_SHRINK | GTK_FILL,
		    GTK_SHRINK | GTK_FILL, 0, 0);
525
  gtk_widget_show (frame);
Elliot Lee's avatar
Elliot Lee committed
526

527 528 529
  c_tool->graph = gtk_drawing_area_new ();
  gtk_widget_set_size_request (c_tool->graph,
                               GRAPH_WIDTH  + RADIUS * 2,
530
                               GRAPH_HEIGHT + RADIUS * 2);
531 532 533
  gtk_widget_set_events (c_tool->graph, GRAPH_MASK);
  gtk_container_add (GTK_CONTAINER (frame), c_tool->graph);
  gtk_widget_show (c_tool->graph);
534

535
  g_signal_connect (c_tool->graph, "event",
536
		    G_CALLBACK (curves_graph_events),
537
		    c_tool);
Elliot Lee's avatar
Elliot Lee committed
538 539 540

  /*  The range drawing area  */
  frame = gtk_frame_new (NULL);
541
  gtk_frame_set_shadow_type (GTK_FRAME (frame), GTK_SHADOW_IN);
Elliot Lee's avatar
Elliot Lee committed
542 543 544 545
  gtk_table_attach (GTK_TABLE (table), frame, 1, 2, 1, 2,
		    GTK_EXPAND, GTK_EXPAND, 0, 0);
  gtk_widget_show (frame);

546 547 548 549
  c_tool->xrange = gtk_preview_new (GTK_PREVIEW_COLOR);
  gtk_preview_size (GTK_PREVIEW (c_tool->xrange), XRANGE_WIDTH, XRANGE_HEIGHT);
  gtk_container_add (GTK_CONTAINER (frame), c_tool->xrange);
  gtk_widget_show (c_tool->xrange);
550

551
  gtk_widget_show (table);
Elliot Lee's avatar
Elliot Lee committed
552

553 554 555
  hbox = gtk_hbox_new (FALSE, 6);
  gtk_box_pack_end (GTK_BOX (vbox), hbox, FALSE, FALSE, 0);
  gtk_widget_show (hbox);
556

557
  /*  Horizontal button box for load / save */
558
  frame = gtk_frame_new (_("All Channels"));
559
  gtk_box_pack_start (GTK_BOX (hbox), frame, FALSE, FALSE, 0);
560 561
  gtk_widget_show (frame);

562
  hbbox = gtk_hbutton_box_new ();
563
  gtk_container_set_border_width (GTK_CONTAINER (hbbox), 2);
564
  gtk_box_set_spacing (GTK_BOX (hbbox), 4);
565
  gtk_button_box_set_layout (GTK_BUTTON_BOX (hbbox), GTK_BUTTONBOX_SPREAD);
566
  gtk_container_add (GTK_CONTAINER (frame), hbbox);
567
  gtk_widget_show (hbbox);
568

569
  button = gtk_button_new_from_stock (GTK_STOCK_OPEN);
570 571
  GTK_WIDGET_SET_FLAGS (button, GTK_CAN_DEFAULT);
  gtk_box_pack_start (GTK_BOX (hbbox), button, FALSE, FALSE, 0);
572
  gimp_help_set_help_data (button, _("Read curves settings from file"), NULL);
573 574
  gtk_widget_show (button);

575
  g_signal_connect (button, "clicked",
576
		    G_CALLBACK (curves_load_callback),
577
		    c_tool);
578

579
  button = gtk_button_new_from_stock (GTK_STOCK_SAVE);
580 581
  GTK_WIDGET_SET_FLAGS (button, GTK_CAN_DEFAULT);
  gtk_box_pack_start (GTK_BOX (hbbox), button, FALSE, FALSE, 0);
582
  gimp_help_set_help_data (button, _("Save curves settings to file"), NULL);
583 584
  gtk_widget_show (button);

585
  g_signal_connect (button, "clicked",
586
		    G_CALLBACK (curves_save_callback),
587
		    c_tool);
588 589 590 591 592 593 594

  /*  The radio box for selecting the curve type  */
  frame = gtk_frame_new (_("Curve Type"));
  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,
595
				  "gimp-curve", GTK_ICON_SIZE_MENU,
596 597 598 599 600 601 602 603 604
				  G_CALLBACK (curves_curve_type_callback),
				  c_tool,
				  &c_tool->curve_type);

  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
605 606
}

607
static void
608
gimp_curves_tool_reset (GimpImageMapTool *image_map_tool)
609
{
610
  GimpCurvesTool       *c_tool = GIMP_CURVES_TOOL (image_map_tool);
611
  GimpHistogramChannel  channel;
612 613 614

  c_tool->grab_point = -1;

615 616 617 618
  for (channel =  GIMP_HISTOGRAM_VALUE;
       channel <= GIMP_HISTOGRAM_ALPHA;
       channel++)
    curves_channel_reset (c_tool->curves, channel);
619

620 621
  curves_update (c_tool, XRANGE_TOP);
  gtk_widget_queue_draw (c_tool->graph);
622 623
}

624

625
/* TODO: preview alpha channel stuff correctly.  -- austin, 20/May/99 */
Elliot Lee's avatar
Elliot Lee committed
626
static void
627 628
curves_update (GimpCurvesTool *c_tool,
	       gint            update)
Elliot Lee's avatar
Elliot Lee committed
629
{
630 631
  GimpHistogramChannel sel_channel;
  gint                 i, j;
632

633
  if (c_tool->color)
634
    {
635
      sel_channel = c_tool->channel;
636
    }
637
  else
638
    {
639
      if (c_tool->channel == 2)
640 641 642 643
        sel_channel = GIMP_HISTOGRAM_ALPHA;
      else
        sel_channel = GIMP_HISTOGRAM_VALUE;
    }
644

Elliot Lee's avatar
Elliot Lee committed
645 646
  if (update & XRANGE_TOP)
    {
647
      guchar buf[XRANGE_WIDTH * 3];
648

649
      switch (sel_channel)
650
	{
Michael Natterer's avatar
Michael Natterer committed
651 652
	case GIMP_HISTOGRAM_VALUE:
	case GIMP_HISTOGRAM_ALPHA:
653
	  for (i = 0; i < XRANGE_HEIGHT / 2; i++)
654
	    {
655
	      for (j = 0; j < XRANGE_WIDTH ; j++)
656
		{
657 658 659
		  buf[j * 3 + 0] = c_tool->curves->curve[sel_channel][j];
		  buf[j * 3 + 1] = c_tool->curves->curve[sel_channel][j];
		  buf[j * 3 + 2] = c_tool->curves->curve[sel_channel][j];
660
		}
661
	      gtk_preview_draw_row (GTK_PREVIEW (c_tool->xrange),
662
				    buf, 0, i, XRANGE_WIDTH);
663
	    }
664 665
	  break;

Michael Natterer's avatar
Michael Natterer committed
666 667 668
	case GIMP_HISTOGRAM_RED:
	case GIMP_HISTOGRAM_GREEN:
	case GIMP_HISTOGRAM_BLUE:
669
	  {
670
	    for (i = 0; i < XRANGE_HEIGHT / 2; i++)
671
	      {
672 673
		for (j = 0; j < XRANGE_WIDTH; j++)
		  {
674 675 676
		    buf[j * 3 + 0] = c_tool->curves->curve[GIMP_HISTOGRAM_RED][j];
		    buf[j * 3 + 1] = c_tool->curves->curve[GIMP_HISTOGRAM_GREEN][j];
		    buf[j * 3 + 2] = c_tool->curves->curve[GIMP_HISTOGRAM_BLUE][j];
677
		  }
678
		gtk_preview_draw_row (GTK_PREVIEW (c_tool->xrange),
679
				      buf, 0, i, XRANGE_WIDTH);
680
	      }
681
	    break;
682 683
	  }

684
	default:
685
	  g_warning ("unknown channel type %d, can't happen!?!?",
686
		     c_tool->channel);
687
	  break;
688
	}
Elliot Lee's avatar
Elliot Lee committed
689

690 691 692
      gtk_widget_queue_draw_area (c_tool->xrange,
                                  0, 0,
                                  XRANGE_WIDTH, XRANGE_HEIGHT / 2);
Elliot Lee's avatar
Elliot Lee committed
693
    }
694

Elliot Lee's avatar
Elliot Lee committed
695 696
  if (update & XRANGE_BOTTOM)
    {
697
      guchar buf[XRANGE_WIDTH * 3];
Elliot Lee's avatar
Elliot Lee committed
698 699

      for (i = 0; i < XRANGE_WIDTH; i++)
700 701 702 703 704
        {
          buf[i * 3 + 0] = i;
          buf[i * 3 + 1] = i;
          buf[i * 3 + 2] = i;
        }
Elliot Lee's avatar
Elliot Lee committed
705 706

      for (i = XRANGE_HEIGHT / 2; i < XRANGE_HEIGHT; i++)
707 708
	gtk_preview_draw_row (GTK_PREVIEW (c_tool->xrange),
                              buf, 0, i, XRANGE_WIDTH);
Elliot Lee's avatar
Elliot Lee committed
709

710 711 712
      gtk_widget_queue_draw_area (c_tool->xrange,
                                  0, XRANGE_HEIGHT / 2,
                                  XRANGE_WIDTH, XRANGE_HEIGHT / 2);
Elliot Lee's avatar
Elliot Lee committed
713
    }
714

Elliot Lee's avatar
Elliot Lee committed
715 716
  if (update & YRANGE)
    {
717 718
      guchar buf[YRANGE_WIDTH * 3];
      guchar pix[3];
Elliot Lee's avatar
Elliot Lee committed
719 720 721

      for (i = 0; i < YRANGE_HEIGHT; i++)
	{
722
	  switch (sel_channel)
723
	    {
Michael Natterer's avatar
Michael Natterer committed
724 725
	    case GIMP_HISTOGRAM_VALUE:
	    case GIMP_HISTOGRAM_ALPHA:
726 727 728
	      pix[0] = pix[1] = pix[2] = (255 - i);
	      break;

Michael Natterer's avatar
Michael Natterer committed
729 730 731
	    case GIMP_HISTOGRAM_RED:
	    case GIMP_HISTOGRAM_GREEN:
	    case GIMP_HISTOGRAM_BLUE:
732
	      pix[0] = pix[1] = pix[2] = 0;
733
	      pix[sel_channel - 1] = (255 - i);
734 735
	      break;

736
	    default:
737
	      g_warning ("unknown channel type %d, can't happen!?!?",
738
			 c_tool->channel);
739
	      break;
740
	    }
741 742

	  for (j = 0; j < YRANGE_WIDTH * 3; j++)
743
	    buf[j] = pix[j%3];
Elliot Lee's avatar
Elliot Lee committed
744

745
	  gtk_preview_draw_row (GTK_PREVIEW (c_tool->yrange),
746
				buf, 0, i, YRANGE_WIDTH);
Elliot Lee's avatar
Elliot Lee committed
747 748
	}

749
      gtk_widget_queue_draw (c_tool->yrange);
Elliot Lee's avatar
Elliot Lee committed
750 751 752 753
    }
}

static void
754 755
curves_channel_callback (GtkWidget      *widget,
			 GimpCurvesTool *c_tool)
Elliot Lee's avatar
Elliot Lee committed
756
{
757
  gimp_menu_item_update (widget, &c_tool->channel);
Elliot Lee's avatar
Elliot Lee committed
758

759
  if (! c_tool->color)
Elliot Lee's avatar
Elliot Lee committed
760
    {
761 762
      if (c_tool->channel > 1)
        c_tool->channel = 2;
763
      else
764
        c_tool->channel = 1;
Elliot Lee's avatar
Elliot Lee committed
765 766
    }

767 768
  gimp_int_radio_group_set_active (GTK_RADIO_BUTTON (c_tool->curve_type),
			           c_tool->curves->curve_type[c_tool->channel]);
769

770 771
  curves_update (c_tool, XRANGE_TOP | YRANGE);
  gtk_widget_queue_draw (c_tool->graph);
Elliot Lee's avatar
Elliot Lee committed
772 773 774
}

static void
775 776
curves_channel_reset_callback (GtkWidget      *widget,
                               GimpCurvesTool *c_tool)
Elliot Lee's avatar
Elliot Lee committed
777
{
778
  c_tool->grab_point = -1;
Elliot Lee's avatar
Elliot Lee committed
779

780
  curves_channel_reset (c_tool->curves, c_tool->channel);
Elliot Lee's avatar
Elliot Lee committed
781

782 783
  curves_update (c_tool, XRANGE_TOP);
  gtk_widget_queue_draw (c_tool->graph);
Elliot Lee's avatar
Elliot Lee committed
784

785
  gimp_image_map_tool_preview (GIMP_IMAGE_MAP_TOOL (c_tool));
Elliot Lee's avatar
Elliot Lee committed
786 787
}

788
static gboolean
789 790
curves_set_sensitive_callback (GimpHistogramChannel  channel,
                               GimpCurvesTool       *c_tool)
791 792 793 794 795 796 797 798
{
  switch (channel)
    {
    case GIMP_HISTOGRAM_VALUE:
      return TRUE;
    case GIMP_HISTOGRAM_RED:
    case GIMP_HISTOGRAM_GREEN:
    case GIMP_HISTOGRAM_BLUE:
799
      return c_tool->color;
800
    case GIMP_HISTOGRAM_ALPHA:
801
      return gimp_drawable_has_alpha (GIMP_IMAGE_MAP_TOOL (c_tool)->drawable);
802
    }
803