gimpcurvestool.c 43 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 "libgimpwidgets/gimpwidgets.h"
36

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

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

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

#include "widgets/gimpcursor.h"
48
#include "widgets/gimpenummenu.h"
Michael Natterer's avatar
Michael Natterer committed
49

50 51
#include "display/gimpdisplay.h"

Michael Natterer's avatar
Michael Natterer committed
52 53
#include "gimpcurvestool.h"
#include "tool_manager.h"
Sven Neumann's avatar
Sven Neumann committed
54

55 56
#include "libgimp/gimpintl.h"

57

58 59 60 61
#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
62

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

75 76 77 78 79
#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
80 81


Michael Natterer's avatar
Michael Natterer committed
82 83 84
/*  local function prototypes  */

static void   gimp_curves_tool_class_init     (GimpCurvesToolClass *klass);
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105
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_press   (GimpTool         *tool,
                                               GimpCoords       *coords,
                                               guint32           time,
					       GdkModifierType   state,
					       GimpDisplay      *gdisp);
static void   gimp_curves_tool_button_release (GimpTool         *tool,
                                               GimpCoords       *coords,
                                               guint32           time,
					       GdkModifierType   state,
					       GimpDisplay      *gdisp);
static void   gimp_curves_tool_motion         (GimpTool         *tool,
                                               GimpCoords       *coords,
                                               guint32           time,
					       GdkModifierType   state,
					       GimpDisplay      *gdisp);
106 107 108 109
static void   gimp_curves_tool_cursor_update  (GimpTool         *tool,
                                               GimpCoords       *coords,
                                               GdkModifierType   state,
                                               GimpDisplay      *gdisp);
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_color_update             (GimpTool         *tool,
					       GimpDisplay      *gdisp,
					       GimpDrawable     *drawable,
118
					       GimpCoords       *coords);
119 120 121 122 123 124 125 126 127
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,
128
                                               GimpCurvesTool   *c_tool);
129
static void   curves_channel_reset_callback   (GtkWidget        *widget,
130
                                               GimpCurvesTool   *c_tool);
131 132 133

static gboolean curves_set_sensitive_callback (gpointer          item_data,
                                               GimpCurvesTool   *c_tool);
134
static void   curves_curve_type_callback      (GtkWidget        *widget,
135
                                               GimpCurvesTool   *c_tool);
136
static void   curves_load_callback            (GtkWidget        *widget,
137
                                               GimpCurvesTool   *c_tool);
138
static void   curves_save_callback            (GtkWidget        *widget,
139
                                               GimpCurvesTool   *c_tool);
140 141 142
static gint   curves_graph_events             (GtkWidget        *widget,
                                               GdkEvent         *event,
                                               GimpCurvesTool   *c_tool);
143 144 145
static void   curves_graph_expose             (GtkWidget        *widget,
                                               GdkRectangle     *area,
                                               GimpCurvesTool   *c_tool);
146 147 148 149 150 151 152 153 154

static void       file_dialog_create          (GimpCurvesTool   *c_tool);
static void       file_dialog_ok_callback     (GimpCurvesTool   *c_tool);

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
155

156 157
static GimpImageMapToolClass *parent_class = NULL;

Michael Natterer's avatar
Michael Natterer committed
158

159
/*  public functions  */
Michael Natterer's avatar
Michael Natterer committed
160 161

void
Nate Summers's avatar
Nate Summers committed
162
gimp_curves_tool_register (GimpToolRegisterCallback  callback,
163
                           gpointer                  data)
Michael Natterer's avatar
Michael Natterer committed
164
{
Nate Summers's avatar
Nate Summers committed
165
  (* callback) (GIMP_TYPE_CURVES_TOOL,
166
                G_TYPE_NONE, NULL,
167
                FALSE,
168
                "gimp-curves-tool",
169 170
                _("Curves"),
                _("Adjust color curves"),
171
                N_("/Layer/Colors/Curves..."), NULL,
172
                NULL, "tools/curves.html",
Nate Summers's avatar
Nate Summers committed
173
                GIMP_STOCK_TOOL_CURVES,
174
                data);
Michael Natterer's avatar
Michael Natterer committed
175 176
}

177
GType
Michael Natterer's avatar
Michael Natterer committed
178 179
gimp_curves_tool_get_type (void)
{
180
  static GType tool_type = 0;
Michael Natterer's avatar
Michael Natterer committed
181 182 183

  if (! tool_type)
    {
184
      static const GTypeInfo tool_info =
Michael Natterer's avatar
Michael Natterer committed
185 186
      {
        sizeof (GimpCurvesToolClass),
187 188 189 190 191 192 193 194
	(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
195 196
      };

197 198 199
      tool_type = g_type_register_static (GIMP_TYPE_IMAGE_MAP_TOOL,
					  "GimpCurvesTool", 
                                          &tool_info, 0);
Michael Natterer's avatar
Michael Natterer committed
200 201 202 203 204
    }

  return tool_type;
}

205 206 207

/*  private functions  */

Michael Natterer's avatar
Michael Natterer committed
208 209 210
static void
gimp_curves_tool_class_init (GimpCurvesToolClass *klass)
{
211 212 213
  GObjectClass          *object_class;
  GimpToolClass         *tool_class;
  GimpImageMapToolClass *image_map_tool_class;
Michael Natterer's avatar
Michael Natterer committed
214

215 216 217
  object_class         = G_OBJECT_CLASS (klass);
  tool_class           = GIMP_TOOL_CLASS (klass);
  image_map_tool_class = GIMP_IMAGE_MAP_TOOL_CLASS (klass);
Michael Natterer's avatar
Michael Natterer committed
218

219
  parent_class = g_type_class_peek_parent (klass);
Michael Natterer's avatar
Michael Natterer committed
220

221
  object_class->finalize = gimp_curves_tool_finalize;
222

223 224 225 226 227
  tool_class->initialize     = gimp_curves_tool_initialize;
  tool_class->button_press   = gimp_curves_tool_button_press;
  tool_class->button_release = gimp_curves_tool_button_release;
  tool_class->motion         = gimp_curves_tool_motion;
  tool_class->cursor_update  = gimp_curves_tool_cursor_update;
228 229 230 231

  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
232 233 234
}

static void
235
gimp_curves_tool_init (GimpCurvesTool *c_tool)
Michael Natterer's avatar
Michael Natterer committed
236
{
237 238 239 240 241
  GimpImageMapTool *image_map_tool;
  gint              i;

  image_map_tool = GIMP_IMAGE_MAP_TOOL (c_tool);

242
  image_map_tool->shell_desc = _("Adjust Color Curves");
243

244 245 246
  c_tool->curves  = g_new0 (Curves, 1);
  c_tool->lut     = gimp_lut_new ();
  c_tool->channel = GIMP_HISTOGRAM_VALUE;
247 248 249

  curves_init (c_tool->curves);

250
  for (i = 0; i < G_N_ELEMENTS (c_tool->col_value); i++)
251
    c_tool->col_value[i] = -1;
252 253 254

  c_tool->cursor_x = -1;
  c_tool->cursor_y = -1;
Michael Natterer's avatar
Michael Natterer committed
255 256 257
}

static void
258
gimp_curves_tool_finalize (GObject *object)
Michael Natterer's avatar
Michael Natterer committed
259
{
260
  GimpCurvesTool *c_tool;
Michael Natterer's avatar
Michael Natterer committed
261

262 263 264
  c_tool = GIMP_CURVES_TOOL (object);

  if (c_tool->curves)
Michael Natterer's avatar
Michael Natterer committed
265
    {
266
      g_free (c_tool->curves);
267
      c_tool->curves = NULL;
Michael Natterer's avatar
Michael Natterer committed
268
    }
269
  if (c_tool->lut)
Michael Natterer's avatar
Michael Natterer committed
270
    {
271 272
      gimp_lut_free (c_tool->lut);
      c_tool->lut = NULL;
Michael Natterer's avatar
Michael Natterer committed
273
    }
274
  if (c_tool->cursor_layout)
Michael Natterer's avatar
Michael Natterer committed
275
    {
276 277
      g_object_unref (c_tool->cursor_layout);
      c_tool->cursor_layout = NULL;
Michael Natterer's avatar
Michael Natterer committed
278
    }
279 280 281 282 283
  if (c_tool->xpos_layout)
    {
      g_object_unref (c_tool->xpos_layout);
      c_tool->xpos_layout = NULL;
    }
Michael Natterer's avatar
Michael Natterer committed
284

285
  G_OBJECT_CLASS (parent_class)->finalize (object);
Michael Natterer's avatar
Michael Natterer committed
286 287 288
}

static void
289 290
gimp_curves_tool_initialize (GimpTool    *tool,
			     GimpDisplay *gdisp)
Michael Natterer's avatar
Michael Natterer committed
291
{
292 293 294 295
  GimpCurvesTool *c_tool;
  GimpDrawable   *drawable;

  c_tool = GIMP_CURVES_TOOL (tool);
296 297

  if (gimp_drawable_is_indexed (gimp_image_active_drawable (gdisp->gimage)))
Michael Natterer's avatar
Michael Natterer committed
298
    {
299 300 301
      g_message (_("Curves for indexed drawables cannot be adjusted."));
      return;
    }
Michael Natterer's avatar
Michael Natterer committed
302

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

305
  curves_init (c_tool->curves);
Michael Natterer's avatar
Michael Natterer committed
306

307 308 309 310 311 312 313 314 315 316 317 318 319 320
  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);

  /* set the sensitivity of the channel menu based on the drawable type */
  gimp_option_menu_set_sensitive (GTK_OPTION_MENU (c_tool->channel_menu),
                                  (GimpOptionMenuSensitivityCallback) curves_set_sensitive_callback,
                                  c_tool);

  /* set the current selection */
321 322
  gimp_option_menu_set_history (GTK_OPTION_MENU (c_tool->channel_menu),
                                GINT_TO_POINTER (c_tool->channel));
Michael Natterer's avatar
Michael Natterer committed
323

324
  curves_update (c_tool, ALL);
Michael Natterer's avatar
Michael Natterer committed
325 326 327
}

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

  c_tool = GIMP_CURVES_TOOL (tool);
Michael Natterer's avatar
Michael Natterer committed
338 339 340 341 342

  drawable = gimp_image_active_drawable (gdisp->gimage);

  tool->gdisp = gdisp;

343 344 345
  g_assert (drawable == tool->drawable);

#if 0
Michael Natterer's avatar
Michael Natterer committed
346 347
  if (drawable != tool->drawable)
    {
348
      gimp_tool_control_set_preserve (tool->control, TRUE);
349
      gimp_image_map_abort (GIMP_IMAGE_MAP_TOOL (tool)->image_map);
350
      gimp_tool_control_set_preserve (tool->control, FALSE);
Michael Natterer's avatar
Michael Natterer committed
351 352 353

      tool->drawable = drawable;

354 355 356
      c_tool->color  = gimp_drawable_is_rgb (drawable);

      GIMP_IMAGE_MAP_TOOL (tool)->drawable  = drawable;
357 358
      GIMP_IMAGE_MAP_TOOL (tool)->image_map = gimp_image_map_new (TRUE,
                                                                  drawable);
359 360

      gimp_option_menu_set_sensitive 
361
        (GTK_OPTION_MENU (c_tool->channel_menu),
362
         (GimpOptionMenuSensitivityCallback) curves_set_sensitive_callback,
363
         c_tool);
Michael Natterer's avatar
Michael Natterer committed
364
    }
365
#endif
Michael Natterer's avatar
Michael Natterer committed
366

367
  gimp_tool_control_activate (tool->control);
Michael Natterer's avatar
Michael Natterer committed
368

369
  curves_color_update (tool, gdisp, drawable, coords);
370
  gtk_widget_queue_draw (c_tool->graph);
Michael Natterer's avatar
Michael Natterer committed
371 372 373
}

static void
374 375 376 377 378
gimp_curves_tool_button_release (GimpTool        *tool,
                                 GimpCoords      *coords,
                                 guint32          time,
				 GdkModifierType  state,
				 GimpDisplay     *gdisp)
Michael Natterer's avatar
Michael Natterer committed
379
{
380 381
  GimpCurvesTool *c_tool;
  GimpDrawable   *drawable;
Michael Natterer's avatar
Michael Natterer committed
382

383 384 385
  c_tool = GIMP_CURVES_TOOL (tool);

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

387
  curves_color_update (tool, gdisp, drawable, coords);
Michael Natterer's avatar
Michael Natterer committed
388

389
  if (state & GDK_SHIFT_MASK)
Michael Natterer's avatar
Michael Natterer committed
390
    {
391
      curves_add_point (c_tool, coords->x, coords->y, c_tool->channel);
392
      curves_calculate_curve (c_tool->curves, c_tool->channel);
Michael Natterer's avatar
Michael Natterer committed
393
    }
394
  else if (state & GDK_CONTROL_MASK)
Michael Natterer's avatar
Michael Natterer committed
395
    {
396
      gint i;
397

398 399 400 401 402
      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
403 404
    }

405 406
  gimp_tool_control_halt (tool->control);

407
  gtk_widget_queue_draw (c_tool->graph);
Michael Natterer's avatar
Michael Natterer committed
408 409 410
}

static void
411 412 413 414 415
gimp_curves_tool_motion (GimpTool        *tool,
                         GimpCoords      *coords,
                         guint32          time,
			 GdkModifierType  state,
			 GimpDisplay     *gdisp)
Michael Natterer's avatar
Michael Natterer committed
416
{
417 418
  GimpCurvesTool *c_tool;
  GimpDrawable   *drawable;
Michael Natterer's avatar
Michael Natterer committed
419

420
  c_tool = GIMP_CURVES_TOOL (tool);
Elliot Lee's avatar
Elliot Lee committed
421

422
  drawable = gimp_image_active_drawable (gdisp->gimage);
Elliot Lee's avatar
Elliot Lee committed
423

424
  curves_color_update (tool, gdisp, drawable, coords);
425
  gtk_widget_queue_draw (c_tool->graph);
Elliot Lee's avatar
Elliot Lee committed
426 427
}

428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454
static void
gimp_curves_tool_cursor_update (GimpTool        *tool,
                                GimpCoords      *coords,
                                GdkModifierType  state,
                                GimpDisplay     *gdisp)
{
  if (gimp_display_coords_in_active_drawable (gdisp, coords))
    {
      gimp_tool_control_set_tool_cursor (tool->control,
                                         GIMP_COLOR_PICKER_TOOL_CURSOR);
      gimp_tool_control_set_cursor_modifier (tool->control,
                                             (state & (GDK_SHIFT_MASK |
                                                       GDK_CONTROL_MASK) ?
                                              GIMP_CURSOR_MODIFIER_PLUS  :
                                              GIMP_CURSOR_MODIFIER_NONE));
    }
  else
    {
      gimp_tool_control_set_tool_cursor (tool->control,
                                         GIMP_TOOL_CURSOR_NONE);
      gimp_tool_control_set_cursor_modifier (tool->control,
                                             GIMP_CURSOR_MODIFIER_NONE);
    }

  GIMP_TOOL_CLASS (parent_class)->cursor_update (tool, coords, state, gdisp);
}

BST 1999 Andy Thomas's avatar
BST 1999 Andy Thomas committed
455
static void
456 457 458
curves_color_update (GimpTool       *tool,
                     GimpDisplay    *gdisp,
                     GimpDrawable   *drawable,
459
                     GimpCoords     *coords)
BST 1999 Andy Thomas's avatar
BST 1999 Andy Thomas committed
460
{
461 462
  GimpCurvesTool *c_tool;
  guchar         *color;
463 464
  gint            x;
  gint            y;
465 466

  if (! (tool && gimp_tool_control_is_active (tool->control)))
BST 1999 Andy Thomas's avatar
BST 1999 Andy Thomas committed
467 468
    return;

469
  c_tool = GIMP_CURVES_TOOL (tool);
BST 1999 Andy Thomas's avatar
BST 1999 Andy Thomas committed
470

471
  gimp_drawable_offsets (drawable, &x, &y);
BST 1999 Andy Thomas's avatar
BST 1999 Andy Thomas committed
472

473 474
  x = RINT (coords->x) - x;
  y = RINT (coords->y) - y;
BST 1999 Andy Thomas's avatar
BST 1999 Andy Thomas committed
475

476 477 478 479 480
  color = gimp_image_map_get_color_at (GIMP_IMAGE_MAP_TOOL (tool)->image_map,
                                       x, y);
  if (color)
    {
      gint maxval;
BST 1999 Andy Thomas's avatar
BST 1999 Andy Thomas committed
481

482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499
      c_tool->col_value[GIMP_HISTOGRAM_RED]   = color[RED_PIX];
      c_tool->col_value[GIMP_HISTOGRAM_GREEN] = color[GREEN_PIX];
      c_tool->col_value[GIMP_HISTOGRAM_BLUE]  = color[BLUE_PIX];
      
      if (gimp_drawable_has_alpha (drawable))
        c_tool->col_value[GIMP_HISTOGRAM_ALPHA] = color[3];
      
      if (gimp_drawable_is_indexed (drawable))
        c_tool->col_value[GIMP_HISTOGRAM_ALPHA] = color[4];
      
      maxval = MAX (color[RED_PIX], color[GREEN_PIX]);
      c_tool->col_value[GIMP_HISTOGRAM_VALUE] = MAX (maxval, color[BLUE_PIX]);
      
      g_free (color);
    }
  else
    {
      gint i;
BST 1999 Andy Thomas's avatar
BST 1999 Andy Thomas committed
500

501 502 503 504 505
      for (i = 0; i <= GIMP_HISTOGRAM_ALPHA; i++)
        c_tool->col_value[i] = -1;
      
      return;
    }
506
}
BST 1999 Andy Thomas's avatar
BST 1999 Andy Thomas committed
507

508
static void
509 510 511 512
curves_add_point (GimpCurvesTool *c_tool,
		  gint            x,
		  gint            y,
		  gint            cchan)
513 514
{
  /* Add point onto the curve */
515 516 517 518
  gint closest_point = 0;
  gint distance;
  gint curvex;
  gint i;
519

520
  switch (c_tool->curves->curve_type[cchan])
521
    {
522
    case GIMP_CURVE_SMOOTH:
523
      curvex   = c_tool->col_value[cchan];
524
      distance = G_MAXINT;
525

526 527
      for (i = 0; i < 17; i++)
	{
528 529
	  if (c_tool->curves->points[cchan][i][0] != -1)
	    if (abs (curvex - c_tool->curves->points[cchan][i][0]) < distance)
530
	      {
531
		distance = abs (curvex - c_tool->curves->points[cchan][i][0]);
532 533 534 535 536 537 538
		closest_point = i;
	      }
	}
      
      if (distance > MIN_DISTANCE)
	closest_point = (curvex + 8) / 16;
      
539 540
      c_tool->curves->points[cchan][closest_point][0] = curvex;
      c_tool->curves->points[cchan][closest_point][1] = c_tool->curves->curve[cchan][curvex];
541 542
      break;
      
543
    case GIMP_CURVE_FREE:
544
      c_tool->curves->curve[cchan][x] = 255 - y;
545
      break;
546
    }
Elliot Lee's avatar
Elliot Lee committed
547 548
}

549 550
static void
gimp_curves_tool_map (GimpImageMapTool *image_map_tool)
Elliot Lee's avatar
Elliot Lee committed
551
{
552
  GimpCurvesTool *c_tool;
553

554
  c_tool = GIMP_CURVES_TOOL (image_map_tool);
555

556 557 558 559
  gimp_lut_setup (c_tool->lut, 
		  (GimpLutFunc) curves_lut_func,
                  c_tool->curves,
		  gimp_drawable_bytes (image_map_tool->drawable));
560

561 562 563
  gimp_image_map_apply (image_map_tool->image_map,
                        (GimpImageMapApplyFunc) gimp_lut_process_2,
                        c_tool->lut);
Elliot Lee's avatar
Elliot Lee committed
564 565
}

566

567 568 569
/*******************/
/*  Curves dialog  */
/*******************/
Elliot Lee's avatar
Elliot Lee committed
570

571 572
static void
gimp_curves_tool_dialog (GimpImageMapTool *image_map_tool)
Elliot Lee's avatar
Elliot Lee committed
573
{
574 575
  GimpCurvesTool *c_tool;
  GtkWidget      *hbox;
576
  GtkWidget      *vbox;
577 578
  GtkWidget      *hbbox;
  GtkWidget      *frame;
579
  GtkWidget      *menu;
580 581
  GtkWidget      *table;
  GtkWidget      *button;
582

583
  c_tool = GIMP_CURVES_TOOL (image_map_tool);
BST 1999 Andy Thomas's avatar
BST 1999 Andy Thomas committed
584

585 586 587 588 589 590 591 592 593
  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);

594 595 596
  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);
597
  gtk_box_pack_start (GTK_BOX (vbox), table, FALSE, FALSE, 0);
598
  gtk_widget_show (table);
Elliot Lee's avatar
Elliot Lee committed
599

600 601
  /*  The option menu for selecting channels  */
  hbox = gtk_hbox_new (FALSE, 4);
602 603 604 605 606 607 608 609 610
  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;
611

612
  button = gtk_button_new_with_mnemonic (_("R_eset Channel"));
613 614
  gtk_box_pack_start (GTK_BOX (hbox), button, FALSE, FALSE, 0);
  gtk_widget_show (button);
615

616
  g_signal_connect (button, "clicked",
617 618
                    G_CALLBACK (curves_channel_reset_callback),
                    c_tool);
619

620 621 622
  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
623 624

  /*  The table for the yrange and the graph  */
625 626 627 628
  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
629
  table = gtk_table_new (2, 2, FALSE);
630 631
  gtk_table_set_col_spacings (GTK_TABLE (table), 2);
  gtk_table_set_row_spacings (GTK_TABLE (table), 2);
632
  gtk_box_pack_start (GTK_BOX (hbox), table, TRUE, FALSE, 0);
Elliot Lee's avatar
Elliot Lee committed
633 634 635

  /*  The range drawing area  */
  frame = gtk_frame_new (NULL);
636
  gtk_frame_set_shadow_type (GTK_FRAME (frame), GTK_SHADOW_IN);
Elliot Lee's avatar
Elliot Lee committed
637 638 639 640
  gtk_table_attach (GTK_TABLE (table), frame, 0, 1, 0, 1,
		    GTK_EXPAND, GTK_EXPAND, 0, 0);
  gtk_widget_show (frame);

641 642 643 644 645
  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
646 647
  /*  The curves graph  */
  frame = gtk_frame_new (NULL);
648
  gtk_frame_set_shadow_type (GTK_FRAME (frame), GTK_SHADOW_IN);
Elliot Lee's avatar
Elliot Lee committed
649
  gtk_table_attach (GTK_TABLE (table), frame, 1, 2, 0, 1,
650 651
		    GTK_SHRINK | GTK_FILL,
		    GTK_SHRINK | GTK_FILL, 0, 0);
652
  gtk_widget_show (frame);
Elliot Lee's avatar
Elliot Lee committed
653

654 655 656
  c_tool->graph = gtk_drawing_area_new ();
  gtk_widget_set_size_request (c_tool->graph,
                               GRAPH_WIDTH  + RADIUS * 2,
657
                               GRAPH_HEIGHT + RADIUS * 2);
658 659 660
  gtk_widget_set_events (c_tool->graph, GRAPH_MASK);
  gtk_container_add (GTK_CONTAINER (frame), c_tool->graph);
  gtk_widget_show (c_tool->graph);
661

662
  g_signal_connect (c_tool->graph, "event",
663
		    G_CALLBACK (curves_graph_events),
664
		    c_tool);
Elliot Lee's avatar
Elliot Lee committed
665 666 667

  /*  The range drawing area  */
  frame = gtk_frame_new (NULL);
668
  gtk_frame_set_shadow_type (GTK_FRAME (frame), GTK_SHADOW_IN);
Elliot Lee's avatar
Elliot Lee committed
669 670 671 672
  gtk_table_attach (GTK_TABLE (table), frame, 1, 2, 1, 2,
		    GTK_EXPAND, GTK_EXPAND, 0, 0);
  gtk_widget_show (frame);

673 674 675 676
  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);
677

678
  gtk_widget_show (table);
Elliot Lee's avatar
Elliot Lee committed
679

680 681 682 683 684
  hbox = gtk_hbox_new (FALSE, 6);
  gtk_box_pack_end (GTK_BOX (vbox), hbox, FALSE, FALSE, 0);
  gtk_widget_show (hbox);
  
  /*  Horizontal button box for load / save */
685
  frame = gtk_frame_new (_("All Channels"));
686
  gtk_box_pack_start (GTK_BOX (hbox), frame, FALSE, FALSE, 0);
687 688
  gtk_widget_show (frame);

689
  hbbox = gtk_hbutton_box_new ();
690
  gtk_container_set_border_width (GTK_CONTAINER (hbbox), 2);
691
  gtk_box_set_spacing (GTK_BOX (hbbox), 4);
692
  gtk_button_box_set_layout (GTK_BUTTON_BOX (hbbox), GTK_BUTTONBOX_SPREAD);
693
  gtk_container_add (GTK_CONTAINER (frame), hbbox);
694
  gtk_widget_show (hbbox);
695

696
  button = gtk_button_new_from_stock (GTK_STOCK_OPEN);
697 698
  GTK_WIDGET_SET_FLAGS (button, GTK_CAN_DEFAULT);
  gtk_box_pack_start (GTK_BOX (hbbox), button, FALSE, FALSE, 0);
699
  gimp_help_set_help_data (button, _("Read curves settings from file"), NULL);
700 701
  gtk_widget_show (button);

702
  g_signal_connect (button, "clicked",
703
		    G_CALLBACK (curves_load_callback),
704
		    c_tool);
705

706
  button = gtk_button_new_from_stock (GTK_STOCK_SAVE);
707 708
  GTK_WIDGET_SET_FLAGS (button, GTK_CAN_DEFAULT);
  gtk_box_pack_start (GTK_BOX (hbbox), button, FALSE, FALSE, 0);
709
  gimp_help_set_help_data (button, _("Save curves settings to file"), NULL);
710 711
  gtk_widget_show (button);

712
  g_signal_connect (button, "clicked",
713
		    G_CALLBACK (curves_save_callback),
714
		    c_tool);
715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731

  /*  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,
				  "gimp-curve",
				  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
732 733
}

734
static void
735
gimp_curves_tool_reset (GimpImageMapTool *image_map_tool)
736
{
737 738
  GimpCurvesTool       *c_tool;
  GimpHistogramChannel  channel;
739 740 741 742 743

  c_tool = GIMP_CURVES_TOOL (image_map_tool);

  c_tool->grab_point = -1;

744 745 746 747
  for (channel =  GIMP_HISTOGRAM_VALUE;
       channel <= GIMP_HISTOGRAM_ALPHA;
       channel++)
    curves_channel_reset (c_tool->curves, channel);
748

749 750
  curves_update (c_tool, XRANGE_TOP);
  gtk_widget_queue_draw (c_tool->graph);
751 752
}

753

754
/* TODO: preview alpha channel stuff correctly.  -- austin, 20/May/99 */
Elliot Lee's avatar
Elliot Lee committed
755
static void
756 757
curves_update (GimpCurvesTool *c_tool,
	       gint            update)
Elliot Lee's avatar
Elliot Lee committed
758
{
759 760
  GimpHistogramChannel sel_channel;
  gint                 i, j;
761
  
762
  if (c_tool->color)
763
    {
764
      sel_channel = c_tool->channel;
765
    }
766 767
  else 
    {
768
      if (c_tool->channel == 2)
769 770 771 772
        sel_channel = GIMP_HISTOGRAM_ALPHA;
      else
        sel_channel = GIMP_HISTOGRAM_VALUE;
    }
773

Elliot Lee's avatar
Elliot Lee committed
774 775
  if (update & XRANGE_TOP)
    {
776
      guchar buf[XRANGE_WIDTH * 3];
777

778
      switch (sel_channel)
779
	{
Michael Natterer's avatar
Michael Natterer committed
780 781
	case GIMP_HISTOGRAM_VALUE:
	case GIMP_HISTOGRAM_ALPHA:
782
	  for (i = 0; i < XRANGE_HEIGHT / 2; i++)
783
	    {
784
	      for (j = 0; j < XRANGE_WIDTH ; j++)
785
		{
786 787 788
		  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];
789
		}
790
	      gtk_preview_draw_row (GTK_PREVIEW (c_tool->xrange),
791
				    buf, 0, i, XRANGE_WIDTH);
792
	    }
793 794
	  break;

Michael Natterer's avatar
Michael Natterer committed
795 796 797
	case GIMP_HISTOGRAM_RED:
	case GIMP_HISTOGRAM_GREEN:
	case GIMP_HISTOGRAM_BLUE:
798
	  {
799
	    for (i = 0; i < XRANGE_HEIGHT / 2; i++)
800
	      {
801 802
		for (j = 0; j < XRANGE_WIDTH; j++)
		  {
803 804 805
		    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];
806
		  }
807
		gtk_preview_draw_row (GTK_PREVIEW (c_tool->xrange),
808
				      buf, 0, i, XRANGE_WIDTH);
809
	      }
810
	    break;
811 812
	  }

813
	default:
814
	  g_warning ("unknown channel type %d, can't happen!?!?",
815
		     c_tool->channel);
816
	  break;
817
	}
Elliot Lee's avatar
Elliot Lee committed
818

819 820 821
      gtk_widget_queue_draw_area (c_tool->xrange,
                                  0, 0,
                                  XRANGE_WIDTH, XRANGE_HEIGHT / 2);
Elliot Lee's avatar
Elliot Lee committed
822
    }
823

Elliot Lee's avatar
Elliot Lee committed
824 825
  if (update & XRANGE_BOTTOM)
    {
826
      guchar buf[XRANGE_WIDTH * 3];
Elliot Lee's avatar
Elliot Lee committed
827 828

      for (i = 0; i < XRANGE_WIDTH; i++)
829 830 831 832 833
        {
          buf[i * 3 + 0] = i;
          buf[i * 3 + 1] = i;
          buf[i * 3 + 2] = i;
        }
Elliot Lee's avatar
Elliot Lee committed
834 835

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

839 840 841
      gtk_widget_queue_draw_area (c_tool->xrange,
                                  0, XRANGE_HEIGHT / 2,
                                  XRANGE_WIDTH, XRANGE_HEIGHT / 2);
Elliot Lee's avatar
Elliot Lee committed
842
    }
843

Elliot Lee's avatar
Elliot Lee committed
844 845
  if (update & YRANGE)
    {
846 847
      guchar buf[YRANGE_WIDTH * 3];
      guchar pix[3];
Elliot Lee's avatar
Elliot Lee committed
848 849 850

      for (i = 0; i < YRANGE_HEIGHT; i++)
	{
851
	  switch (sel_channel)
852
	    {
Michael Natterer's avatar
Michael Natterer committed
853 854
	    case GIMP_HISTOGRAM_VALUE:
	    case GIMP_HISTOGRAM_ALPHA:
855 856 857
	      pix[0] = pix[1] = pix[2] = (255 - i);
	      break;

Michael Natterer's avatar
Michael Natterer committed
858 859 860
	    case GIMP_HISTOGRAM_RED:
	    case GIMP_HISTOGRAM_GREEN:
	    case GIMP_HISTOGRAM_BLUE:
861
	      pix[0] = pix[1] = pix[2] = 0;
862
	      pix[sel_channel - 1] = (255 - i);
863 864
	      break;

865
	    default:
866
	      g_warning ("unknown channel type %d, can't happen!?!?",
867
			 c_tool->channel);
868
	      break;
869
	    }
870 871

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

874
	  gtk_preview_draw_row (GTK_PREVIEW (c_tool->yrange),
875
				buf, 0, i, YRANGE_WIDTH);
Elliot Lee's avatar
Elliot Lee committed
876 877
	}

878
      gtk_widget_queue_draw (c_tool->yrange);
Elliot Lee's avatar
Elliot Lee committed
879 880 881 882
    }
}

static void
883 884
curves_channel_callback (GtkWidget      *widget,
			 GimpCurvesTool *c_tool)
Elliot Lee's avatar
Elliot Lee committed
885
{
886
  gimp_menu_item_update (widget, &c_tool->channel);