gimpcurvestool.c 42.1 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 62 63
#define GRAPH          0x1
#define XRANGE_TOP     0x2
#define XRANGE_BOTTOM  0x4
#define YRANGE         0x8
#define DRAW          0x10
#define ALL           0xFF
Elliot Lee's avatar
Elliot Lee committed
64

65
/*  NB: take care when changing these values: make sure the curve[] array in
66
 *  base/curves.h is large enough.
67
 */
68 69 70 71 72 73
#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
74 75 76 77 78 79 80 81 82 83 84 85
#define RADIUS           3
#define MIN_DISTANCE     8

#define GRAPH_MASK  GDK_EXPOSURE_MASK | \
		    GDK_POINTER_MOTION_MASK | \
		    GDK_POINTER_MOTION_HINT_MASK | \
                    GDK_ENTER_NOTIFY_MASK | \
		    GDK_BUTTON_PRESS_MASK | \
		    GDK_BUTTON_RELEASE_MASK | \
		    GDK_BUTTON1_MOTION_MASK


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 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156
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);

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,
					       gint              x,
					       gint              y);
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,
                                               gpointer          data);
static void   curves_channel_reset_callback   (GtkWidget        *widget,
                                               gpointer          data);

static gboolean curves_set_sensitive_callback (gpointer          item_data,
                                               GimpCurvesTool   *c_tool);
static void   curves_smooth_callback          (GtkWidget        *widget,
                                               gpointer          data);
static void   curves_free_callback            (GtkWidget        *widget,
                                               gpointer          data);

static void   curves_load_callback            (GtkWidget        *widget,
                                               gpointer          data);
static void   curves_save_callback            (GtkWidget        *widget,
                                               gpointer          data);
static gint   curves_graph_events             (GtkWidget        *widget,
                                               GdkEvent         *event,
                                               GimpCurvesTool   *c_tool);

static void       file_dialog_create          (GimpCurvesTool   *c_tool);
static void       file_dialog_ok_callback     (GimpCurvesTool   *c_tool);
static gboolean   file_dialog_cancel_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
157

158 159
static GimpImageMapToolClass *parent_class = NULL;

Michael Natterer's avatar
Michael Natterer committed
160

161
/*  public functions  */
Michael Natterer's avatar
Michael Natterer committed
162 163

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

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

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

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

  return tool_type;
}

207 208 209

/*  private functions  */

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

217 218 219
  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
220

221
  parent_class = g_type_class_peek_parent (klass);
Michael Natterer's avatar
Michael Natterer committed
222

223 224 225 226 227 228 229 230 231 232
  object_class->finalize       = gimp_curves_tool_finalize;

  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;

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

static void
236
gimp_curves_tool_init (GimpCurvesTool *c_tool)
Michael Natterer's avatar
Michael Natterer committed
237
{
238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256
  GimpImageMapTool *image_map_tool;
  gint              i;

  image_map_tool = GIMP_IMAGE_MAP_TOOL (c_tool);

  image_map_tool->shell_title = _("Curves");
  image_map_tool->shell_name  = "curves";

  c_tool->curves  = g_new0 (Curves, 1);
  c_tool->lut     = gimp_lut_new ();
  c_tool->channel = GIMP_HISTOGRAM_VALUE;

  curves_init (c_tool->curves);

  for (i = 0;
       i < (sizeof (c_tool->col_value) /
            sizeof (c_tool->col_value[0]));
       i++)
    c_tool->col_value[i] = 0;
Michael Natterer's avatar
Michael Natterer committed
257 258 259
}

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

264 265 266
  c_tool = GIMP_CURVES_TOOL (object);

  if (c_tool->curves)
Michael Natterer's avatar
Michael Natterer committed
267
    {
268 269
      g_free(c_tool->curves);
      c_tool->curves = NULL;
Michael Natterer's avatar
Michael Natterer committed
270 271
    }

272
  if (c_tool->lut)
Michael Natterer's avatar
Michael Natterer committed
273
    {
274 275
      gimp_lut_free (c_tool->lut);
      c_tool->lut = NULL;
Michael Natterer's avatar
Michael Natterer committed
276 277
    }

278
  if (c_tool->pixmap)
Michael Natterer's avatar
Michael Natterer committed
279
    {
280 281
      g_object_unref (c_tool->pixmap);
      c_tool->pixmap = NULL;
Michael Natterer's avatar
Michael Natterer committed
282
    }
283 284

  if (c_tool->cursor_layout)
Michael Natterer's avatar
Michael Natterer committed
285
    {
286 287
      g_object_unref (c_tool->cursor_layout);
      c_tool->cursor_layout = NULL;
Michael Natterer's avatar
Michael Natterer committed
288 289
    }

290 291 292 293 294
  if (c_tool->xpos_layout)
    {
      g_object_unref (c_tool->xpos_layout);
      c_tool->xpos_layout = NULL;
    }
Michael Natterer's avatar
Michael Natterer committed
295

296
  G_OBJECT_CLASS (parent_class)->finalize (object);
Michael Natterer's avatar
Michael Natterer committed
297 298 299
}

static void
300 301
gimp_curves_tool_initialize (GimpTool    *tool,
			     GimpDisplay *gdisp)
Michael Natterer's avatar
Michael Natterer committed
302
{
303 304 305 306 307 308 309
  GimpCurvesTool *c_tool;
  GimpDrawable   *drawable;

  c_tool = GIMP_CURVES_TOOL (tool);

  if (gdisp &&
      gimp_drawable_is_indexed (gimp_image_active_drawable (gdisp->gimage)))
Michael Natterer's avatar
Michael Natterer committed
310
    {
311 312 313
      g_message (_("Curves for indexed drawables cannot be adjusted."));
      return;
    }
Michael Natterer's avatar
Michael Natterer committed
314

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

317
  curves_init (c_tool->curves);
Michael Natterer's avatar
Michael Natterer committed
318

319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334
  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 */
  gtk_option_menu_set_history (GTK_OPTION_MENU (c_tool->channel_menu),
			       c_tool->channel);
Michael Natterer's avatar
Michael Natterer committed
335

336
  curves_update (c_tool, ALL);
Michael Natterer's avatar
Michael Natterer committed
337 338 339
}

static void
340 341 342 343 344
gimp_curves_tool_button_press (GimpTool        *tool,
                               GimpCoords      *coords,
                               guint32          time,
			       GdkModifierType  state,
			       GimpDisplay     *gdisp)
Michael Natterer's avatar
Michael Natterer committed
345
{
346 347 348 349
  GimpCurvesTool *c_tool;
  GimpDrawable   *drawable;

  c_tool = GIMP_CURVES_TOOL (tool);
Michael Natterer's avatar
Michael Natterer committed
350 351 352 353 354

  drawable = gimp_image_active_drawable (gdisp->gimage);

  tool->gdisp = gdisp;

355 356 357
  g_assert (drawable == tool->drawable);

#if 0
Michael Natterer's avatar
Michael Natterer committed
358 359
  if (drawable != tool->drawable)
    {
360
      gimp_tool_control_set_preserve (tool->control, TRUE);
361
      gimp_image_map_abort (GIMP_IMAGE_MAP_TOOL (tool)->image_map);
362
      gimp_tool_control_set_preserve (tool->control, FALSE);
Michael Natterer's avatar
Michael Natterer committed
363 364 365

      tool->drawable = drawable;

366 367 368 369
      c_tool->color  = gimp_drawable_is_rgb (drawable);

      GIMP_IMAGE_MAP_TOOL (tool)->drawable  = drawable;
      GIMP_IMAGE_MAP_TOOL (tool)->image_map = gimp_image_map_new (TRUE, drawable);
370 371

      gimp_option_menu_set_sensitive 
372
        (GTK_OPTION_MENU (c_tool->channel_menu),
373
         (GimpOptionMenuSensitivityCallback) curves_set_sensitive_callback,
374
         c_tool);
Michael Natterer's avatar
Michael Natterer committed
375
    }
376
#endif
Michael Natterer's avatar
Michael Natterer committed
377

378
  gimp_tool_control_activate (tool->control);
Michael Natterer's avatar
Michael Natterer committed
379

380
  curves_color_update (tool, gdisp, drawable, coords->x, coords->y);
381
  curves_update (c_tool, GRAPH | DRAW);
Michael Natterer's avatar
Michael Natterer committed
382 383 384
}

static void
385 386 387 388 389
gimp_curves_tool_button_release (GimpTool        *tool,
                                 GimpCoords      *coords,
                                 guint32          time,
				 GdkModifierType  state,
				 GimpDisplay     *gdisp)
Michael Natterer's avatar
Michael Natterer committed
390
{
391 392
  GimpCurvesTool *c_tool;
  GimpDrawable   *drawable;
Michael Natterer's avatar
Michael Natterer committed
393

394 395 396
  c_tool = GIMP_CURVES_TOOL (tool);

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

398
  curves_color_update (tool, gdisp, drawable, coords->x, coords->y);
Michael Natterer's avatar
Michael Natterer committed
399

400
  if (state & GDK_SHIFT_MASK)
Michael Natterer's avatar
Michael Natterer committed
401
    {
402 403 404 405
      curves_add_point (c_tool,
                        coords->x, coords->y, c_tool->channel);

      curves_calculate_curve (c_tool->curves, c_tool->channel);
Michael Natterer's avatar
Michael Natterer committed
406
    }
407
  else if (state & GDK_CONTROL_MASK)
Michael Natterer's avatar
Michael Natterer committed
408
    {
409 410 411 412 413 414 415 416 417 418 419 420
      curves_add_point (c_tool,
                        coords->x, coords->y, GIMP_HISTOGRAM_VALUE);
      curves_add_point (c_tool,
                        coords->x, coords->y, GIMP_HISTOGRAM_RED);
      curves_add_point (c_tool,
                        coords->x, coords->y, GIMP_HISTOGRAM_GREEN);
      curves_add_point (c_tool,
                        coords->x, coords->y, GIMP_HISTOGRAM_BLUE);
      curves_add_point (c_tool,
                        coords->x, coords->y, GIMP_HISTOGRAM_ALPHA);

      curves_calculate_curve (c_tool->curves, c_tool->channel);
Michael Natterer's avatar
Michael Natterer committed
421 422
    }

423 424 425
  gimp_tool_control_halt (tool->control);

  curves_update (c_tool, GRAPH | DRAW);
Michael Natterer's avatar
Michael Natterer committed
426 427 428
}

static void
429 430 431 432 433
gimp_curves_tool_motion (GimpTool        *tool,
                         GimpCoords      *coords,
                         guint32          time,
			 GdkModifierType  state,
			 GimpDisplay     *gdisp)
Michael Natterer's avatar
Michael Natterer committed
434
{
435 436
  GimpCurvesTool *c_tool;
  GimpDrawable   *drawable;
Michael Natterer's avatar
Michael Natterer committed
437

438
  c_tool = GIMP_CURVES_TOOL (tool);
Elliot Lee's avatar
Elliot Lee committed
439

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

442 443
  curves_color_update (tool, gdisp, drawable, coords->x, coords->y);
  curves_update (c_tool, GRAPH | DRAW);
Elliot Lee's avatar
Elliot Lee committed
444 445
}

BST 1999 Andy Thomas's avatar
BST 1999 Andy Thomas committed
446
static void
447 448 449 450 451
curves_color_update (GimpTool       *tool,
                     GimpDisplay    *gdisp,
                     GimpDrawable   *drawable,
                     gint            x,
                     gint            y)
BST 1999 Andy Thomas's avatar
BST 1999 Andy Thomas committed
452
{
453 454 455 456 457 458 459 460 461 462 463 464
  GimpCurvesTool *c_tool;
  guchar         *color;
  gint            offx;
  gint            offy;
  gint            maxval;
  gboolean        has_alpha;
  gboolean        is_indexed;
  GimpImageType   sample_type;

  c_tool = GIMP_CURVES_TOOL (tool);

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

467
  gimp_drawable_offsets (drawable, &offx, &offy);
BST 1999 Andy Thomas's avatar
BST 1999 Andy Thomas committed
468 469 470 471

  x -= offx;
  y -= offy;

472 473
  if (! (color = gimp_image_map_get_color_at (GIMP_IMAGE_MAP_TOOL (tool)->image_map,
                                              x, y)))
BST 1999 Andy Thomas's avatar
BST 1999 Andy Thomas committed
474 475
    return;

476
  sample_type = gimp_drawable_type (drawable);
477 478
  is_indexed  = gimp_drawable_is_indexed (drawable);
  has_alpha   = GIMP_IMAGE_TYPE_HAS_ALPHA (sample_type);
BST 1999 Andy Thomas's avatar
BST 1999 Andy Thomas committed
479

480 481 482
  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];
BST 1999 Andy Thomas's avatar
BST 1999 Andy Thomas committed
483 484 485

  if (has_alpha)
    {
486
      c_tool->col_value[GIMP_HISTOGRAM_ALPHA] = color[3];
BST 1999 Andy Thomas's avatar
BST 1999 Andy Thomas committed
487 488 489
    }

  if (is_indexed)
490
    c_tool->col_value[GIMP_HISTOGRAM_ALPHA] = color[4];
BST 1999 Andy Thomas's avatar
BST 1999 Andy Thomas committed
491

492
  maxval = MAX (color[RED_PIX], color[GREEN_PIX]);
493
  c_tool->col_value[GIMP_HISTOGRAM_VALUE] = MAX (maxval, color[BLUE_PIX]);
BST 1999 Andy Thomas's avatar
BST 1999 Andy Thomas committed
494

495
  g_free (color);
496
}
BST 1999 Andy Thomas's avatar
BST 1999 Andy Thomas committed
497

498
static void
499 500 501 502
curves_add_point (GimpCurvesTool *c_tool,
		  gint            x,
		  gint            y,
		  gint            cchan)
503 504
{
  /* Add point onto the curve */
505 506 507 508
  gint closest_point = 0;
  gint distance;
  gint curvex;
  gint i;
509

510
  switch (c_tool->curves->curve_type[cchan])
511
    {
512 513
    case CURVES_SMOOTH:
      curvex   = c_tool->col_value[cchan];
514
      distance = G_MAXINT;
515

516 517
      for (i = 0; i < 17; i++)
	{
518 519
	  if (c_tool->curves->points[cchan][i][0] != -1)
	    if (abs (curvex - c_tool->curves->points[cchan][i][0]) < distance)
520
	      {
521
		distance = abs (curvex - c_tool->curves->points[cchan][i][0]);
522 523 524 525 526 527 528
		closest_point = i;
	      }
	}
      
      if (distance > MIN_DISTANCE)
	closest_point = (curvex + 8) / 16;
      
529 530
      c_tool->curves->points[cchan][closest_point][0] = curvex;
      c_tool->curves->points[cchan][closest_point][1] = c_tool->curves->curve[cchan][curvex];
531 532
      break;
      
533 534
    case CURVES_FREE:
      c_tool->curves->curve[cchan][x] = 255 - y;
535
      break;
536
    }
Elliot Lee's avatar
Elliot Lee committed
537 538
}

539 540
static void
gimp_curves_tool_map (GimpImageMapTool *image_map_tool)
Elliot Lee's avatar
Elliot Lee committed
541
{
542
  GimpCurvesTool *c_tool;
543

544
  c_tool = GIMP_CURVES_TOOL (image_map_tool);
545

546 547 548 549 550 551 552
  gimp_lut_setup (c_tool->lut, 
		  (GimpLutFunc) curves_lut_func,
                  c_tool->curves,
		  gimp_drawable_bytes (image_map_tool->drawable));
  gimp_image_map_apply (image_map_tool->image_map,
                        (GimpImageMapApplyFunc) gimp_lut_process_2,
                        c_tool->lut);
Elliot Lee's avatar
Elliot Lee committed
553 554
}

555

556 557 558
/*******************/
/*  Curves dialog  */
/*******************/
Elliot Lee's avatar
Elliot Lee committed
559

560 561
static void
gimp_curves_tool_dialog (GimpImageMapTool *image_map_tool)
Elliot Lee's avatar
Elliot Lee committed
562
{
563 564 565 566 567 568
  GimpCurvesTool *c_tool;
  GtkWidget      *hbox;
  GtkWidget      *hbbox;
  GtkWidget      *frame;
  GtkWidget      *table;
  GtkWidget      *button;
569

570
  c_tool = GIMP_CURVES_TOOL (image_map_tool);
BST 1999 Andy Thomas's avatar
BST 1999 Andy Thomas committed
571

572 573 574 575 576 577
  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);
  gtk_box_pack_start (GTK_BOX (image_map_tool->main_vbox), table,
                      FALSE, FALSE, 0);
  gtk_widget_show (table);
Elliot Lee's avatar
Elliot Lee committed
578

579 580
  /*  The option menu for selecting channels  */
  hbox = gtk_hbox_new (FALSE, 4);
581

582 583 584 585 586 587
  c_tool->channel_menu =
    gimp_enum_option_menu_new (GIMP_TYPE_HISTOGRAM_CHANNEL,
                               G_CALLBACK (curves_channel_callback),
                               c_tool);
  gtk_box_pack_start (GTK_BOX (hbox), c_tool->channel_menu, FALSE, FALSE, 0);
  gtk_widget_show (c_tool->channel_menu);
588

589 590 591
  button = gtk_button_new_with_label (_("Reset Channel"));
  gtk_box_pack_start (GTK_BOX (hbox), button, FALSE, FALSE, 0);
  gtk_widget_show (button);
592

593 594 595
  g_signal_connect (G_OBJECT (button), "clicked",
                    G_CALLBACK (curves_channel_reset_callback),
                    c_tool);
596

597 598 599
  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
600

601 602 603
  /*  The option menu for selecting the drawing method  */
  c_tool->curve_type_menu =
    gimp_option_menu_new (FALSE,
Elliot Lee's avatar
Elliot Lee committed
604

605 606
                          _("Smooth"), curves_smooth_callback,
                          c_tool, NULL, NULL, TRUE,
Elliot Lee's avatar
Elliot Lee committed
607

608 609
                          _("Free"), curves_free_callback,
                          c_tool, NULL, NULL, FALSE,
Elliot Lee's avatar
Elliot Lee committed
610

611
                          NULL);
Elliot Lee's avatar
Elliot Lee committed
612

613 614 615
  gimp_table_attach_aligned (GTK_TABLE (table), 0, 1,
                             _("Curve Type:"), 1.0, 0.5,
                             c_tool->curve_type_menu, 1, TRUE);
Elliot Lee's avatar
Elliot Lee committed
616 617 618

  /*  The table for the yrange and the graph  */
  table = gtk_table_new (2, 2, FALSE);
619 620
  gtk_table_set_col_spacings (GTK_TABLE (table), 2);
  gtk_table_set_row_spacings (GTK_TABLE (table), 2);
621 622
  gtk_box_pack_start (GTK_BOX (image_map_tool->main_vbox), table,
                      FALSE, FALSE, 0);
Elliot Lee's avatar
Elliot Lee committed
623 624 625

  /*  The range drawing area  */
  frame = gtk_frame_new (NULL);
626
  gtk_frame_set_shadow_type (GTK_FRAME (frame), GTK_SHADOW_IN);
Elliot Lee's avatar
Elliot Lee committed
627 628 629 630
  gtk_table_attach (GTK_TABLE (table), frame, 0, 1, 0, 1,
		    GTK_EXPAND, GTK_EXPAND, 0, 0);
  gtk_widget_show (frame);

631 632 633 634 635
  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
636 637
  /*  The curves graph  */
  frame = gtk_frame_new (NULL);
638
  gtk_frame_set_shadow_type (GTK_FRAME (frame), GTK_SHADOW_IN);
Elliot Lee's avatar
Elliot Lee committed
639
  gtk_table_attach (GTK_TABLE (table), frame, 1, 2, 0, 1,
640 641
		    GTK_SHRINK | GTK_FILL,
		    GTK_SHRINK | GTK_FILL, 0, 0);
642
  gtk_widget_show (frame);
Elliot Lee's avatar
Elliot Lee committed
643

644 645 646
  c_tool->graph = gtk_drawing_area_new ();
  gtk_widget_set_size_request (c_tool->graph,
                               GRAPH_WIDTH  + RADIUS * 2,
647
                               GRAPH_HEIGHT + RADIUS * 2);
648 649 650
  gtk_widget_set_events (c_tool->graph, GRAPH_MASK);
  gtk_container_add (GTK_CONTAINER (frame), c_tool->graph);
  gtk_widget_show (c_tool->graph);
651

652
  g_signal_connect (G_OBJECT (c_tool->graph), "event",
653
		    G_CALLBACK (curves_graph_events),
654
		    c_tool);
Elliot Lee's avatar
Elliot Lee committed
655 656 657

  /*  The range drawing area  */
  frame = gtk_frame_new (NULL);
658
  gtk_frame_set_shadow_type (GTK_FRAME (frame), GTK_SHADOW_IN);
Elliot Lee's avatar
Elliot Lee committed
659 660 661 662
  gtk_table_attach (GTK_TABLE (table), frame, 1, 2, 1, 2,
		    GTK_EXPAND, GTK_EXPAND, 0, 0);
  gtk_widget_show (frame);

663 664 665 666
  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);
667

668
  gtk_widget_show (table);
Elliot Lee's avatar
Elliot Lee committed
669

670 671
  /*  Horizontal button box for load / save  */
  hbbox = gtk_hbutton_box_new ();
672
  gtk_box_set_spacing (GTK_BOX (hbbox), 4);
673
  gtk_button_box_set_layout (GTK_BUTTON_BOX (hbbox), GTK_BUTTONBOX_SPREAD);
674 675
  gtk_box_pack_end (GTK_BOX (image_map_tool->main_vbox), hbbox, FALSE, FALSE, 0);
  gtk_widget_show (hbbox);
676 677 678 679 680 681

  button = gtk_button_new_with_label (_("Load"));
  GTK_WIDGET_SET_FLAGS (button, GTK_CAN_DEFAULT);
  gtk_box_pack_start (GTK_BOX (hbbox), button, FALSE, FALSE, 0);
  gtk_widget_show (button);

682 683
  g_signal_connect (G_OBJECT (button), "clicked",
		    G_CALLBACK (curves_load_callback),
684
		    c_tool);
685

686 687 688 689 690
  button = gtk_button_new_with_label (_("Save"));
  GTK_WIDGET_SET_FLAGS (button, GTK_CAN_DEFAULT);
  gtk_box_pack_start (GTK_BOX (hbbox), button, FALSE, FALSE, 0);
  gtk_widget_show (button);

691 692
  g_signal_connect (G_OBJECT (button), "clicked",
		    G_CALLBACK (curves_save_callback),
693
		    c_tool);
Elliot Lee's avatar
Elliot Lee committed
694 695
}

696
static void
697
gimp_curves_tool_reset (GimpImageMapTool *image_map_tool)
698
{
699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714
  GimpCurvesTool *c_tool;
  gint            i, j;

  c_tool = GIMP_CURVES_TOOL (image_map_tool);

  c_tool->grab_point = -1;

  for (i = 0; i < 5; i++)
    {
      for (j = 0; j < 256; j++)
        c_tool->curves->curve[i][j] = j;

      curves_channel_reset (c_tool->curves, i);
    }

  curves_update (c_tool, GRAPH | XRANGE_TOP | DRAW);
715 716
}

717
static void
718 719 720
curve_print_loc (GimpCurvesTool *c_tool,
		 gint            xpos,
		 gint            ypos)
721
{
722
  gchar buf[32];
723

724
  if (! c_tool->cursor_layout)
725
    {
726 727 728 729
      c_tool->cursor_layout = gtk_widget_create_pango_layout (c_tool->graph,
                                                              "x:888 y:888");
      pango_layout_get_pixel_extents (c_tool->cursor_layout, 
                                      NULL, &c_tool->cursor_rect);
730 731
    }
  
732
  if (xpos >= 0 && xpos <= 255 && ypos >=0 && ypos <= 255)
733
    {
734 735
      gdk_draw_rectangle (c_tool->graph->window, 
			  c_tool->graph->style->bg_gc[GTK_STATE_ACTIVE],
736
			  TRUE, 
737 738 739 740
                          RADIUS * 2 + 2, 
                          RADIUS * 2 + 2, 
			  c_tool->cursor_rect.width  + 4,
			  c_tool->cursor_rect.height + 5);
741

742 743
      gdk_draw_rectangle (c_tool->graph->window, 
			  c_tool->graph->style->black_gc,
744
			  FALSE, 
745 746 747 748
                          RADIUS * 2 + 2, 
                          RADIUS * 2 + 2, 
			  c_tool->cursor_rect.width  + 3, 
			  c_tool->cursor_rect.height + 4);
749 750
      
      g_snprintf (buf, sizeof (buf), "x:%3d y:%3d",xpos, ypos);
751 752 753 754 755 756
      pango_layout_set_text (c_tool->cursor_layout, buf, 11);
      gdk_draw_layout (c_tool->graph->window, 
                       c_tool->graph->style->black_gc,
		       RADIUS * 2 + 4 + c_tool->cursor_rect.x,
		       RADIUS * 2 + 5 + c_tool->cursor_rect.y,
		       c_tool->cursor_layout);
757 758 759
    }
}

760
/* TODO: preview alpha channel stuff correctly.  -- austin, 20/May/99 */
Elliot Lee's avatar
Elliot Lee committed
761
static void
762 763
curves_update (GimpCurvesTool *c_tool,
	       gint            update)
Elliot Lee's avatar
Elliot Lee committed
764
{
765 766 767 768 769
  gint  i, j;
  gchar buf[32];
  gint  offset;
  gint  height;
  gint  sel_channel;
770
  
771
  if (c_tool->color)
772
    {
773
      sel_channel = c_tool->channel;
774 775 776
    } 
  else 
    {
777
      if (c_tool->channel == 2)
778 779 780 781
        sel_channel = GIMP_HISTOGRAM_ALPHA;
      else
        sel_channel = GIMP_HISTOGRAM_VALUE;
    }
782

Elliot Lee's avatar
Elliot Lee committed
783 784
  if (update & XRANGE_TOP)
    {
785
      guchar buf[XRANGE_WIDTH * 3];
786

787
      switch (sel_channel)
788
	{
Michael Natterer's avatar
Michael Natterer committed
789 790
	case GIMP_HISTOGRAM_VALUE:
	case GIMP_HISTOGRAM_ALPHA:
791
	  for (i = 0; i < XRANGE_HEIGHT / 2; i++)
792
	    {
793
	      for (j = 0; j < XRANGE_WIDTH ; j++)
794
		{
795 796 797
		  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];
798
		}
799
	      gtk_preview_draw_row (GTK_PREVIEW (c_tool->xrange),
800
				    buf, 0, i, XRANGE_WIDTH);
801
	    }
802 803
	  break;

Michael Natterer's avatar
Michael Natterer committed
804 805 806
	case GIMP_HISTOGRAM_RED:
	case GIMP_HISTOGRAM_GREEN:
	case GIMP_HISTOGRAM_BLUE:
807
	  {
808
	    for (i = 0; i < XRANGE_HEIGHT / 2; i++)
809
	      {
810 811
		for (j = 0; j < XRANGE_WIDTH; j++)
		  {
812 813 814
		    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];
815
		  }
816
		gtk_preview_draw_row (GTK_PREVIEW (c_tool->xrange),
817
				      buf, 0, i, XRANGE_WIDTH);
818
	      }
819
	    break;
820 821
	  }

822
	default:
823
	  g_warning ("unknown channel type %d, can't happen!?!?",
824
		     c_tool->channel);
825
	  break;
826
	}
Elliot Lee's avatar
Elliot Lee committed
827 828

      if (update & DRAW)
829 830 831
        gtk_widget_queue_draw_area (c_tool->xrange,
                                    0, 0,
                                    XRANGE_WIDTH, XRANGE_HEIGHT / 2);
Elliot Lee's avatar
Elliot Lee committed
832
    }
833

Elliot Lee's avatar
Elliot Lee committed
834 835
  if (update & XRANGE_BOTTOM)
    {
836
      guchar buf[XRANGE_WIDTH * 3];
Elliot Lee's avatar
Elliot Lee committed
837 838

      for (i = 0; i < XRANGE_WIDTH; i++)
839 840 841 842 843
        {
          buf[i * 3 + 0] = i;
          buf[i * 3 + 1] = i;
          buf[i * 3 + 2] = i;
        }
Elliot Lee's avatar
Elliot Lee committed
844 845

      for (i = XRANGE_HEIGHT / 2; i < XRANGE_HEIGHT; i++)
846 847
	gtk_preview_draw_row (GTK_PREVIEW (c_tool->xrange),
                              buf, 0, i, XRANGE_WIDTH);
Elliot Lee's avatar
Elliot Lee committed
848 849

      if (update & DRAW)
850 851 852
        gtk_widget_queue_draw_area (c_tool->xrange,
                                    0, XRANGE_HEIGHT / 2,
                                    XRANGE_WIDTH, XRANGE_HEIGHT / 2);
Elliot Lee's avatar
Elliot Lee committed
853
    }
854

Elliot Lee's avatar
Elliot Lee committed
855 856
  if (update & YRANGE)
    {
857 858
      guchar buf[YRANGE_WIDTH * 3];
      guchar pix[3];
Elliot Lee's avatar
Elliot Lee committed
859 860 861

      for (i = 0; i < YRANGE_HEIGHT; i++)
	{
862
	  switch (sel_channel)
863
	    {
Michael Natterer's avatar
Michael Natterer committed
864 865
	    case GIMP_HISTOGRAM_VALUE:
	    case GIMP_HISTOGRAM_ALPHA:
866 867 868
	      pix[0] = pix[1] = pix[2] = (255 - i);
	      break;

Michael Natterer's avatar
Michael Natterer committed
869 870 871
	    case GIMP_HISTOGRAM_RED:
	    case GIMP_HISTOGRAM_GREEN:
	    case GIMP_HISTOGRAM_BLUE:
872
	      pix[0] = pix[1] = pix[2] = 0;
873
	      pix[sel_channel - 1] = (255 - i);
874 875
	      break;

876
	    default:
877
	      g_warning ("unknown channel type %d, can't happen!?!?",
878
			 c_tool->channel);
879
	      break;
880
	    }
881 882

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

885
	  gtk_preview_draw_row (GTK_PREVIEW (c_tool->yrange),
886
				buf, 0, i, YRANGE_WIDTH);
Elliot Lee's avatar
Elliot Lee committed
887 888 889
	}

      if (update & DRAW)
890
	gtk_widget_queue_draw (c_tool->yrange);
Elliot Lee's avatar
Elliot Lee committed
891
    }
892 893

  if ((update & GRAPH) && (update & DRAW) && c_tool->pixmap)
Elliot Lee's avatar
Elliot Lee committed
894 895 896 897
    {
      GdkPoint points[256];

      /*  Clear the pixmap  */
898 899
      gdk_draw_rectangle (c_tool->pixmap,
                          c_tool->graph->style->bg_gc[GTK_STATE_NORMAL],
900 901
			  TRUE, 0, 0,
			  GRAPH_WIDTH + RADIUS * 2, GRAPH_HEIGHT + RADIUS * 2);
Elliot Lee's avatar
Elliot Lee committed
902 903 904 905

      /*  Draw the grid lines  */
      for (i = 0; i < 5; i++)
	{
906 907
	  gdk_draw_line (c_tool->pixmap,
                         c_tool->graph->style->dark_gc[GTK_STATE_NORMAL],
Elliot Lee's avatar
Elliot Lee committed
908 909
			 RADIUS, i * (GRAPH_HEIGHT / 4) + RADIUS,
			 GRAPH_WIDTH + RADIUS, i * (GRAPH_HEIGHT / 4) + RADIUS);
910 911
	  gdk_draw_line (c_tool->pixmap,
                         c_tool->graph->style->dark_gc[GTK_STATE_NORMAL],
Elliot Lee's avatar
Elliot Lee committed
912 913 914 915 916 917 918 919
			 i * (GRAPH_WIDTH / 4) + RADIUS, RADIUS,
			 i * (GRAPH_WIDTH / 4) + RADIUS, GRAPH_HEIGHT + RADIUS);
	}

      /*  Draw the curve  */
      for (i = 0; i < 256; i++)
	{
	  points[i].x = i + RADIUS;
920
	  points[i].y = 255 - c_tool->curves->curve[c_tool->channel][i] + RADIUS;
Elliot Lee's avatar
Elliot Lee committed
921 922
	}

923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946
      if (c_tool->curves->curve_type[c_tool->channel] == CURVES_FREE)
        {
          gdk_draw_points (c_tool->pixmap,
                           c_tool->graph->style->black_gc,
                           points, 256);
        }
      else
        {
          gdk_draw_lines (c_tool->pixmap,
                          c_tool->graph->style->black_gc,
                          points, 256);

          /*  Draw the points  */
          for (i = 0; i < 17; i++)
            {
              if (c_tool->curves->points[c_tool->channel][i][0] != -1)
                gdk_draw_arc (c_tool->pixmap,
                              c_tool->graph->style->black_gc,
                              TRUE,
                              c_tool->curves->points[c_tool->channel][i][0],
                              255 - c_tool->curves->points[c_tool->channel][i][1],
                              RADIUS * 2, RADIUS * 2, 0, 23040);
            }
        }
Elliot Lee's avatar
Elliot Lee committed
947

948
      /* draw the color line */
949 950 951 952 953 954
      gdk_draw_line (c_tool->pixmap,
                     c_tool->graph->style->black_gc,
		     c_tool->col_value[sel_channel] + RADIUS,
                     RADIUS,
		     c_tool->col_value[sel_channel] + RADIUS,
                     GRAPH_HEIGHT + RADIUS);
BST 1999 Andy Thomas's avatar
BST 1999 Andy Thomas committed
955

956
      /* and xpos indicator */
957
      g_snprintf (buf, sizeof (buf), "x:%d", c_tool->col_value[sel_channel]);
958

959 960 961
      if (! c_tool->xpos_layout)
        c_tool->xpos_layout = gtk_widget_create_pango_layout (c_tool->graph,
                                                              buf);