color-balance.c 22.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

#include "config.h"

21
#include <gtk/gtk.h>
Sven Neumann's avatar
Sven Neumann committed
22

23 24
#include "libgimpcolor/gimpcolor.h"
#include "libgimpmath/gimpmath.h"
25
#include "libgimpwidgets/gimpwidgets.h"
26

Michael Natterer's avatar
Michael Natterer committed
27 28 29 30 31 32 33
#include "tools-types.h"

#include "base/pixel-region.h"

#include "core/gimpdrawable.h"
#include "core/gimpimage.h"

Michael Natterer's avatar
Michael Natterer committed
34 35
#include "widgets/gimpwidgets-utils.h"

Michael Natterer's avatar
Michael Natterer committed
36
#include "gimpcolorbalancetool.h"
Michael Natterer's avatar
Michael Natterer committed
37
#include "gimpcolorbalancetool-transfer.h"
Michael Natterer's avatar
Michael Natterer committed
38 39
#include "tool_manager.h"
#include "tool_options.h"
Sven Neumann's avatar
Sven Neumann committed
40

Elliot Lee's avatar
Elliot Lee committed
41 42 43 44
#include "drawable.h"
#include "gdisplay.h"
#include "image_map.h"

45 46
#include "libgimp/gimpintl.h"

Michael Natterer's avatar
Michael Natterer committed
47 48 49
#define WANT_ADJUSTMENT_BITS
#include "icons.h"

50

Michael Natterer's avatar
Michael Natterer committed
51 52 53 54
#define CYAN_RED       0x1
#define MAGENTA_GREEN  0x2
#define YELLOW_BLUE    0x4
#define ALL           (CYAN_RED | MAGENTA_GREEN | YELLOW_BLUE)
Elliot Lee's avatar
Elliot Lee committed
55

56

Michael Natterer's avatar
Michael Natterer committed
57
/*  local function prototypes  */
58

Michael Natterer's avatar
Michael Natterer committed
59 60
static void   gimp_color_balance_tool_class_init (GimpColorBalanceToolClass *klass);
static void   gimp_color_balance_tool_init       (GimpColorBalanceTool      *bc_tool);
Elliot Lee's avatar
Elliot Lee committed
61

Michael Natterer's avatar
Michael Natterer committed
62
static void   gimp_color_balance_tool_destroy    (GtkObject  *object);
Michael Natterer's avatar
Michael Natterer committed
63

Michael Natterer's avatar
Michael Natterer committed
64 65 66 67 68
static void   gimp_color_balance_tool_initialize (GimpTool   *tool,
						  GDisplay   *gdisp);
static void   gimp_color_balance_tool_control    (GimpTool   *tool,
						  ToolAction  action,
						  GDisplay   *gdisp);
Michael Natterer's avatar
Michael Natterer committed
69

70
static ColorBalanceDialog * color_balance_dialog_new (void);
Michael Natterer's avatar
Michael Natterer committed
71

72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92
static void   color_balance_update               (ColorBalanceDialog *cbd,
						  gint                );
static void   color_balance_preview              (ColorBalanceDialog *cbd);
static void   color_balance_reset_callback       (GtkWidget          *widget,
						  gpointer            data);
static void   color_balance_ok_callback          (GtkWidget          *widget,
						  gpointer            data);
static void   color_balance_cancel_callback      (GtkWidget          *widget,
						  gpointer            data);
static void   color_balance_range_callback       (GtkWidget          *widget,
						  gpointer            data);
static void   color_balance_preserve_update      (GtkWidget          *widget,
						  gpointer            data);
static void   color_balance_preview_update       (GtkWidget          *widget,
						  gpointer            data);
static void   color_balance_cr_adjustment_update (GtkAdjustment      *adj,
						  gpointer            data);
static void   color_balance_mg_adjustment_update (GtkAdjustment      *adj,
						  gpointer            data);
static void   color_balance_yb_adjustment_update (GtkAdjustment      *adj,
						  gpointer            data);
93

Michael Natterer's avatar
Michael Natterer committed
94 95 96 97 98 99 100 101 102 103 104 105 106

/*  the color balance tool options  */
static ToolOptions *color_balance_options = NULL;

/*  the color balance dialog  */
static ColorBalanceDialog *color_balance_dialog = NULL;

static GimpImageMapToolClass *parent_class = NULL;


/*  functions  */

void
Michael Natterer's avatar
Michael Natterer committed
107
gimp_color_balance_tool_register (Gimp *gimp)
Michael Natterer's avatar
Michael Natterer committed
108
{
Michael Natterer's avatar
Michael Natterer committed
109 110
  tool_manager_register_tool (gimp,
			      GIMP_TYPE_COLOR_BALANCE_TOOL,
Michael Natterer's avatar
Michael Natterer committed
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 157 158 159
                              FALSE,
			      "gimp:color_balance_tool",
			      _("Color Balance"),
			      _("Adjust color balance"),
			      N_("/Image/Colors/Color Balance..."), NULL,
			      NULL, "tools/color_balance.html",
			      (const gchar **) adjustment_bits);
}

GtkType
gimp_color_balance_tool_get_type (void)
{
  static GtkType tool_type = 0;

  if (! tool_type)
    {
      GtkTypeInfo tool_info =
      {
        "GimpColorBalanceTool",
        sizeof (GimpColorBalanceTool),
        sizeof (GimpColorBalanceToolClass),
        (GtkClassInitFunc) gimp_color_balance_tool_class_init,
        (GtkObjectInitFunc) gimp_color_balance_tool_init,
        /* reserved_1 */ NULL,
        /* reserved_2 */ NULL,
        (GtkClassInitFunc) NULL,
      };

      tool_type = gtk_type_unique (GIMP_TYPE_IMAGE_MAP_TOOL, &tool_info);
    }

  return tool_type;
}

static void
gimp_color_balance_tool_class_init (GimpColorBalanceToolClass *klass)
{
  GtkObjectClass    *object_class;
  GimpToolClass     *tool_class;

  object_class = (GtkObjectClass *) klass;
  tool_class   = (GimpToolClass *) klass;

  parent_class = gtk_type_class (GIMP_TYPE_IMAGE_MAP_TOOL);

  object_class->destroy  = gimp_color_balance_tool_destroy;

  tool_class->initialize = gimp_color_balance_tool_initialize;
  tool_class->control    = gimp_color_balance_tool_control;
Michael Natterer's avatar
Michael Natterer committed
160 161

  gimp_color_balance_tool_transfer_init ();
Michael Natterer's avatar
Michael Natterer committed
162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253
}

static void
gimp_color_balance_tool_init (GimpColorBalanceTool *bc_tool)
{
  GimpTool *tool;

  tool = GIMP_TOOL (bc_tool);

  if (! color_balance_options)
    {
      color_balance_options = tool_options_new ();

      tool_manager_register_tool_options (GIMP_TYPE_COLOR_BALANCE_TOOL,
					  (ToolOptions *) color_balance_options);
    }
}

static void
gimp_color_balance_tool_destroy (GtkObject *object)
{
  color_balance_dialog_hide ();

  if (GTK_OBJECT_CLASS (parent_class)->destroy)
    GTK_OBJECT_CLASS (parent_class)->destroy (object);
}

static void
gimp_color_balance_tool_initialize (GimpTool *tool,
				    GDisplay *gdisp)
{
  gint i;

  if (! gdisp)
    {
      color_balance_dialog_hide ();
      return;
    }

  if (! gimp_drawable_is_rgb (gimp_image_active_drawable (gdisp->gimage)))
    {
      g_message (_("Color balance operates only on RGB color drawables."));
      return;
    }

  /*  The color balance dialog  */
  if (!color_balance_dialog)
    color_balance_dialog = color_balance_dialog_new ();
  else
    if (!GTK_WIDGET_VISIBLE (color_balance_dialog->shell))
      gtk_widget_show (color_balance_dialog->shell);

  for (i = 0; i < 3; i++)
    {
      color_balance_dialog->cyan_red[i]      = 0.0;
      color_balance_dialog->magenta_green[i] = 0.0;
      color_balance_dialog->yellow_blue[i]   = 0.0;
    }

  color_balance_dialog->drawable = gimp_image_active_drawable (gdisp->gimage);
  color_balance_dialog->image_map =
    image_map_create (gdisp, color_balance_dialog->drawable);

  color_balance_update (color_balance_dialog, ALL);
}

static void
gimp_color_balance_tool_control (GimpTool   *tool,
				 ToolAction  action,
				 GDisplay   *gdisp)
{
  switch (action)
    {
    case PAUSE:
      break;

    case RESUME:
      break;

    case HALT:
      color_balance_dialog_hide ();
      break;

    default:
      break;
    }

  if (GIMP_TOOL_CLASS (parent_class)->control)
    GIMP_TOOL_CLASS (parent_class)->control (tool, action, gdisp);
}


Elliot Lee's avatar
Elliot Lee committed
254 255
/*  color balance machinery  */

Manish Singh's avatar
Manish Singh committed
256
void
Elliot Lee's avatar
Elliot Lee committed
257 258
color_balance (PixelRegion *srcPR,
	       PixelRegion *destPR,
Michael Natterer's avatar
Michael Natterer committed
259
	       void        *data)
Elliot Lee's avatar
Elliot Lee committed
260 261
{
  ColorBalanceDialog *cbd;
262 263 264 265 266 267
  guchar             *src, *s;
  guchar             *dest, *d;
  gboolean            alpha;
  gint                r, g, b;
  gint                r_n, g_n, b_n;
  gint                w, h;
Elliot Lee's avatar
Elliot Lee committed
268

Michael Natterer's avatar
Michael Natterer committed
269
  cbd = (ColorBalanceDialog *) data;
Elliot Lee's avatar
Elliot Lee committed
270 271 272 273 274 275 276 277 278 279 280 281 282

  h = srcPR->h;
  src = srcPR->data;
  dest = destPR->data;
  alpha = (srcPR->bytes == 4) ? TRUE : FALSE;

  while (h--)
    {
      w = srcPR->w;
      s = src;
      d = dest;
      while (w--)
	{
jaycox's avatar
jaycox committed
283 284 285 286 287 288 289 290
	  r = s[RED_PIX];
	  g = s[GREEN_PIX];
	  b = s[BLUE_PIX];

	  r_n = cbd->r_lookup[r];
	  g_n = cbd->g_lookup[g];
	  b_n = cbd->b_lookup[b];

Elliot Lee's avatar
Elliot Lee committed
291 292
	  if (cbd->preserve_luminosity)
	    {
293 294 295
	      gimp_rgb_to_hls_int (&r_n, &g_n, &b_n);
	      g_n = gimp_rgb_to_l_int (r, g, b);
	      gimp_hls_to_rgb_int (&r_n, &g_n, &b_n);
Elliot Lee's avatar
Elliot Lee committed
296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313
	    }

	  d[RED_PIX] = r_n;
	  d[GREEN_PIX] = g_n;
 	  d[BLUE_PIX] = b_n;

	  if (alpha)
	    d[ALPHA_PIX] = s[ALPHA_PIX];

	  s += srcPR->bytes;
	  d += destPR->bytes;
	}

      src += srcPR->rowstride;
      dest += destPR->rowstride;
    }
}

314 315 316 317 318 319 320
void
color_balance_dialog_hide (void)
{
  if (color_balance_dialog)
    color_balance_cancel_callback (NULL, (gpointer) color_balance_dialog);
} 

321 322 323
/**************************/
/*  Color Balance dialog  */
/**************************/
Elliot Lee's avatar
Elliot Lee committed
324

325
static ColorBalanceDialog *
326
color_balance_dialog_new (void)
Elliot Lee's avatar
Elliot Lee committed
327 328 329 330 331 332 333 334
{
  ColorBalanceDialog *cbd;
  GtkWidget *vbox;
  GtkWidget *hbox;
  GtkWidget *table;
  GtkWidget *start_label;
  GtkWidget *end_label;
  GtkWidget *label;
Michael Natterer's avatar
Michael Natterer committed
335
  GtkWidget *spinbutton;
Elliot Lee's avatar
Elliot Lee committed
336 337 338 339 340
  GtkWidget *slider;
  GtkWidget *toggle;
  GtkWidget *radio_button;
  GtkObject *data;
  GSList *group = NULL;
341
  gint i;
342

343
  gchar *appl_mode_names[] =
Elliot Lee's avatar
Elliot Lee committed
344
  {
345 346 347
    N_("Shadows"),
    N_("Midtones"),
    N_("Highlights")
Elliot Lee's avatar
Elliot Lee committed
348
  };
349

Michael Natterer's avatar
Michael Natterer committed
350
  cbd = g_new (ColorBalanceDialog, 1);
Elliot Lee's avatar
Elliot Lee committed
351
  cbd->preserve_luminosity = TRUE;
Michael Natterer's avatar
Michael Natterer committed
352 353
  cbd->preview             = TRUE;
  cbd->application_mode    = SHADOWS;
Elliot Lee's avatar
Elliot Lee committed
354 355

  /*  The shell and main vbox  */
356
  cbd->shell = gimp_dialog_new (_("Color Balance"), "color_balance",
Michael Natterer's avatar
Michael Natterer committed
357
				tool_manager_help_func, NULL,
358 359 360 361
				GTK_WIN_POS_NONE,
				FALSE, TRUE, FALSE,

				_("OK"), color_balance_ok_callback,
362
				cbd, NULL, NULL, TRUE, FALSE,
363
				_("Reset"), color_balance_reset_callback,
364
				cbd, NULL, NULL, TRUE, FALSE,
365
				_("Cancel"), color_balance_cancel_callback,
366
				cbd, NULL, NULL, FALSE, TRUE,
367 368

				NULL);
Elliot Lee's avatar
Elliot Lee committed
369

370
  vbox = gtk_vbox_new (FALSE, 4);
Michael Natterer's avatar
Michael Natterer committed
371
  gtk_container_set_border_width (GTK_CONTAINER (vbox), 4);
372
  gtk_container_add (GTK_CONTAINER (GTK_DIALOG (cbd->shell)->vbox), vbox);
Elliot Lee's avatar
Elliot Lee committed
373 374

  /*  Horizontal box for application mode  */
375
  hbox = gtk_hbox_new (FALSE, 4);
Elliot Lee's avatar
Elliot Lee committed
376 377
  gtk_box_pack_start (GTK_BOX (vbox), hbox, FALSE, FALSE, 0);

Michael Natterer's avatar
Michael Natterer committed
378 379
  label = gtk_label_new (_("Color Levels:"));
  gtk_box_pack_start (GTK_BOX (hbox), label, FALSE, FALSE, 0);
Elliot Lee's avatar
Elliot Lee committed
380 381
  gtk_widget_show (label);

Michael Natterer's avatar
Michael Natterer committed
382
  /*  cyan-red spinbutton  */
383
  data = gtk_adjustment_new (0, -100.0, 100.0, 1.0, 10.0, 0.0);
Michael Natterer's avatar
Michael Natterer committed
384 385 386 387 388 389 390 391
  cbd->cyan_red_data = GTK_ADJUSTMENT (data);

  spinbutton = gtk_spin_button_new (cbd->cyan_red_data, 1.0, 0);
  gtk_widget_set_usize (spinbutton, 75, -1);
  gtk_box_pack_start (GTK_BOX (hbox), spinbutton, FALSE, FALSE, 0);
  gtk_widget_show (spinbutton);

  /*  magenta-green spinbutton  */
392
  data = gtk_adjustment_new (0, -100.0, 100.0, 1.0, 10.0, 0.0);
Michael Natterer's avatar
Michael Natterer committed
393 394 395 396 397 398 399 400
  cbd->magenta_green_data = GTK_ADJUSTMENT (data);

  spinbutton = gtk_spin_button_new (cbd->magenta_green_data, 1.0, 0);
  gtk_widget_set_usize (spinbutton, 75, -1);
  gtk_box_pack_start (GTK_BOX (hbox), spinbutton, FALSE, FALSE, 0);
  gtk_widget_show (spinbutton);

  /*  yellow-blue spinbutton  */
401
  data = gtk_adjustment_new (0, -100.0, 100.0, 1.0, 10.0, 0.0);
Michael Natterer's avatar
Michael Natterer committed
402
  cbd->yellow_blue_data = GTK_ADJUSTMENT (data);
Elliot Lee's avatar
Elliot Lee committed
403

Michael Natterer's avatar
Michael Natterer committed
404 405 406 407 408 409
  spinbutton = gtk_spin_button_new (cbd->yellow_blue_data, 1.0, 0);
  gtk_widget_set_usize (spinbutton, 75, -1);
  gtk_box_pack_start (GTK_BOX (hbox), spinbutton, FALSE, FALSE, 0);
  gtk_widget_show (spinbutton);

  gtk_widget_show (hbox);
Elliot Lee's avatar
Elliot Lee committed
410 411 412

  /*  The table containing sliders  */
  table = gtk_table_new (3, 3, FALSE);
413 414
  gtk_table_set_col_spacings (GTK_TABLE (table), 4);
  gtk_table_set_row_spacings (GTK_TABLE (table), 2);
Elliot Lee's avatar
Elliot Lee committed
415 416 417
  gtk_box_pack_start (GTK_BOX (vbox), table, FALSE, FALSE, 0);

  /*  Create the cyan-red scale widget  */
418
  start_label = gtk_label_new (_("Cyan"));
Elliot Lee's avatar
Elliot Lee committed
419 420
  gtk_misc_set_alignment (GTK_MISC (start_label), 1.0, 1.0);
  gtk_table_attach (GTK_TABLE (table), start_label, 0, 1, 0, 1,
421
		    GTK_SHRINK | GTK_FILL, GTK_SHRINK | GTK_FILL, 0, 0);
Michael Natterer's avatar
Michael Natterer committed
422 423

  slider = gtk_hscale_new (cbd->cyan_red_data);
Elliot Lee's avatar
Elliot Lee committed
424 425 426
  gtk_scale_set_digits (GTK_SCALE (slider), 0);
  gtk_scale_set_value_pos (GTK_SCALE (slider), GTK_POS_TOP);
  gtk_range_set_update_policy (GTK_RANGE (slider), GTK_UPDATE_DELAYED);
427 428
  gtk_table_attach_defaults (GTK_TABLE (table), slider, 1, 2, 0, 1);

Michael Natterer's avatar
Michael Natterer committed
429 430
  gtk_signal_connect (GTK_OBJECT (cbd->cyan_red_data), "value_changed",
		      GTK_SIGNAL_FUNC (color_balance_cr_adjustment_update),
Elliot Lee's avatar
Elliot Lee committed
431
		      cbd);
Michael Natterer's avatar
Michael Natterer committed
432

433
  end_label = gtk_label_new (_("Red"));
Elliot Lee's avatar
Elliot Lee committed
434 435
  gtk_misc_set_alignment (GTK_MISC (end_label), 0.0, 1.0);
  gtk_table_attach (GTK_TABLE (table), end_label, 2, 3, 0, 1,
436
		    GTK_SHRINK | GTK_FILL, GTK_SHRINK | GTK_FILL, 0, 0);
Elliot Lee's avatar
Elliot Lee committed
437 438 439 440 441 442

  gtk_widget_show (start_label);
  gtk_widget_show (end_label);
  gtk_widget_show (slider);

  /*  Create the magenta-green scale widget  */
443
  start_label = gtk_label_new (_("Magenta"));
Elliot Lee's avatar
Elliot Lee committed
444 445
  gtk_misc_set_alignment (GTK_MISC (start_label), 1.0, 1.0);
  gtk_table_attach (GTK_TABLE (table), start_label, 0, 1, 1, 2,
446
		    GTK_SHRINK | GTK_FILL, GTK_SHRINK | GTK_FILL, 0, 0);
Michael Natterer's avatar
Michael Natterer committed
447 448

  slider = gtk_hscale_new (cbd->magenta_green_data);
Elliot Lee's avatar
Elliot Lee committed
449 450 451
  gtk_scale_set_digits (GTK_SCALE (slider), 0);
  gtk_scale_set_value_pos (GTK_SCALE (slider), GTK_POS_TOP);
  gtk_range_set_update_policy (GTK_RANGE (slider), GTK_UPDATE_DELAYED);
452 453
  gtk_table_attach_defaults (GTK_TABLE (table), slider, 1, 2, 1, 2);

Michael Natterer's avatar
Michael Natterer committed
454 455
  gtk_signal_connect (GTK_OBJECT (cbd->magenta_green_data), "value_changed",
		      GTK_SIGNAL_FUNC (color_balance_mg_adjustment_update),
Elliot Lee's avatar
Elliot Lee committed
456
		      cbd);
Michael Natterer's avatar
Michael Natterer committed
457

458
  end_label = gtk_label_new (_("Green"));
Elliot Lee's avatar
Elliot Lee committed
459 460
  gtk_misc_set_alignment (GTK_MISC (end_label), 0.0, 1.0);
  gtk_table_attach (GTK_TABLE (table), end_label, 2, 3, 1, 2,
461
		    GTK_SHRINK | GTK_FILL, GTK_SHRINK | GTK_FILL, 0, 0);
Elliot Lee's avatar
Elliot Lee committed
462 463 464 465 466 467

  gtk_widget_show (start_label);
  gtk_widget_show (end_label);
  gtk_widget_show (slider);

  /*  Create the yellow-blue scale widget  */
468
  start_label = gtk_label_new (_("Yellow"));
Elliot Lee's avatar
Elliot Lee committed
469 470
  gtk_misc_set_alignment (GTK_MISC (start_label), 1.0, 1.0);
  gtk_table_attach (GTK_TABLE (table), start_label, 0, 1, 2, 3,
471
		    GTK_SHRINK | GTK_FILL, GTK_SHRINK | GTK_FILL, 0, 0);
Michael Natterer's avatar
Michael Natterer committed
472 473

  slider = gtk_hscale_new (cbd->yellow_blue_data);
Elliot Lee's avatar
Elliot Lee committed
474 475 476
  gtk_scale_set_digits (GTK_SCALE (slider), 0);
  gtk_scale_set_value_pos (GTK_SCALE (slider), GTK_POS_TOP);
  gtk_range_set_update_policy (GTK_RANGE (slider), GTK_UPDATE_DELAYED);
477 478
  gtk_table_attach_defaults (GTK_TABLE (table), slider, 1, 2, 2, 3);

Michael Natterer's avatar
Michael Natterer committed
479 480
  gtk_signal_connect (GTK_OBJECT (cbd->yellow_blue_data), "value_changed",
		      GTK_SIGNAL_FUNC (color_balance_yb_adjustment_update),
Elliot Lee's avatar
Elliot Lee committed
481
		      cbd);
Michael Natterer's avatar
Michael Natterer committed
482

483
  end_label = gtk_label_new (_("Blue"));
Elliot Lee's avatar
Elliot Lee committed
484 485
  gtk_misc_set_alignment (GTK_MISC (end_label), 0.0, 1.0);
  gtk_table_attach (GTK_TABLE (table), end_label, 2, 3, 2, 3,
486
		    GTK_SHRINK | GTK_FILL, GTK_SHRINK | GTK_FILL, 0, 0);
Elliot Lee's avatar
Elliot Lee committed
487 488 489 490 491

  gtk_widget_show (start_label);
  gtk_widget_show (end_label);
  gtk_widget_show (slider);

492
  gtk_widget_show (table);
Elliot Lee's avatar
Elliot Lee committed
493 494

  /*  Horizontal box for application mode  */
Michael Natterer's avatar
Michael Natterer committed
495
  hbox = gtk_hbox_new (TRUE, 4);
Elliot Lee's avatar
Elliot Lee committed
496 497 498 499 500
  gtk_box_pack_start (GTK_BOX (vbox), hbox, FALSE, FALSE, 0);

  /*  the radio buttons for application mode  */
  for (i = 0; i < 3; i++)
    {
Michael Natterer's avatar
Michael Natterer committed
501 502
      radio_button =
	gtk_radio_button_new_with_label (group, gettext (appl_mode_names[i]));
Elliot Lee's avatar
Elliot Lee committed
503
      group = gtk_radio_button_group (GTK_RADIO_BUTTON (radio_button));
504 505 506
      gtk_box_pack_start (GTK_BOX (hbox), radio_button, TRUE, FALSE, 0);

      gtk_object_set_user_data (GTK_OBJECT (radio_button), (gpointer) i);
Elliot Lee's avatar
Elliot Lee committed
507
      gtk_signal_connect (GTK_OBJECT (radio_button), "toggled",
508
			  GTK_SIGNAL_FUNC (color_balance_range_callback),
Elliot Lee's avatar
Elliot Lee committed
509
			  cbd);
510

Elliot Lee's avatar
Elliot Lee committed
511 512
      gtk_widget_show (radio_button);
    }
513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538

  gtk_widget_show (hbox);

  /*  Horizontal box for preview and preserve luminosity toggle buttons  */
  hbox = gtk_hbox_new (FALSE, 4);
  gtk_box_pack_end (GTK_BOX (vbox), hbox, FALSE, FALSE, 0);

  /*  The preserve luminosity toggle  */
  toggle = gtk_check_button_new_with_label (_("Preserve Luminosity"));
  gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (toggle),
				cbd->preserve_luminosity);
  gtk_box_pack_start (GTK_BOX (hbox), toggle, FALSE, FALSE, 0);
  gtk_signal_connect (GTK_OBJECT (toggle), "toggled",
		      GTK_SIGNAL_FUNC (color_balance_preserve_update),
		      cbd);
  gtk_widget_show (toggle);

  /*  The preview toggle  */
  toggle = gtk_check_button_new_with_label (_("Preview"));
  gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (toggle), cbd->preview);
  gtk_box_pack_end (GTK_BOX (hbox), toggle, FALSE, FALSE, 0);
  gtk_signal_connect (GTK_OBJECT (toggle), "toggled",
		      GTK_SIGNAL_FUNC (color_balance_preview_update),
		      cbd);
  gtk_widget_show (toggle);

Elliot Lee's avatar
Elliot Lee committed
539 540 541 542 543 544 545 546 547 548
  gtk_widget_show (hbox);

  gtk_widget_show (vbox);
  gtk_widget_show (cbd->shell);

  return cbd;
}

static void
color_balance_update (ColorBalanceDialog *cbd,
Michael Natterer's avatar
Michael Natterer committed
549
		      gint                update)
Elliot Lee's avatar
Elliot Lee committed
550
{
Michael Natterer's avatar
Michael Natterer committed
551
  if (update & CYAN_RED)
Elliot Lee's avatar
Elliot Lee committed
552
    {
Michael Natterer's avatar
Michael Natterer committed
553 554
      gtk_adjustment_set_value (cbd->cyan_red_data,
				cbd->cyan_red[cbd->application_mode]);
Elliot Lee's avatar
Elliot Lee committed
555
    }
Michael Natterer's avatar
Michael Natterer committed
556
  if (update & MAGENTA_GREEN)
Elliot Lee's avatar
Elliot Lee committed
557
    {
Michael Natterer's avatar
Michael Natterer committed
558 559
      gtk_adjustment_set_value (cbd->magenta_green_data,
				cbd->magenta_green[cbd->application_mode]);
Elliot Lee's avatar
Elliot Lee committed
560
    }
Michael Natterer's avatar
Michael Natterer committed
561
  if (update & YELLOW_BLUE)
Elliot Lee's avatar
Elliot Lee committed
562
    {
Michael Natterer's avatar
Michael Natterer committed
563 564
      gtk_adjustment_set_value (cbd->yellow_blue_data,
				cbd->yellow_blue[cbd->application_mode]);
Elliot Lee's avatar
Elliot Lee committed
565 566 567
    }
}

Manish Singh's avatar
Manish Singh committed
568
void
jaycox's avatar
jaycox committed
569 570
color_balance_create_lookup_tables (ColorBalanceDialog *cbd)
{
Michael Natterer's avatar
Michael Natterer committed
571 572 573 574
  gdouble *cyan_red_transfer[3];
  gdouble *magenta_green_transfer[3];
  gdouble *yellow_blue_transfer[3];
  gint i;
575
  gint32 r_n, g_n, b_n;
Michael Natterer's avatar
Michael Natterer committed
576

jaycox's avatar
jaycox committed
577 578 579 580 581 582 583 584 585 586 587 588
  /*  Set the transfer arrays  (for speed)  */
  cyan_red_transfer[SHADOWS] = (cbd->cyan_red[SHADOWS] > 0) ? shadows_add : shadows_sub;
  cyan_red_transfer[MIDTONES] = (cbd->cyan_red[MIDTONES] > 0) ? midtones_add : midtones_sub;
  cyan_red_transfer[HIGHLIGHTS] = (cbd->cyan_red[HIGHLIGHTS] > 0) ? highlights_add : highlights_sub;
  magenta_green_transfer[SHADOWS] = (cbd->magenta_green[SHADOWS] > 0) ? shadows_add : shadows_sub;
  magenta_green_transfer[MIDTONES] = (cbd->magenta_green[MIDTONES] > 0) ? midtones_add : midtones_sub;
  magenta_green_transfer[HIGHLIGHTS] = (cbd->magenta_green[HIGHLIGHTS] > 0) ? highlights_add : highlights_sub;
  yellow_blue_transfer[SHADOWS] = (cbd->yellow_blue[SHADOWS] > 0) ? shadows_add : shadows_sub;
  yellow_blue_transfer[MIDTONES] = (cbd->yellow_blue[MIDTONES] > 0) ? midtones_add : midtones_sub;
  yellow_blue_transfer[HIGHLIGHTS] = (cbd->yellow_blue[HIGHLIGHTS] > 0) ? highlights_add : highlights_sub;

  for (i = 0; i < 256; i++)
Michael Natterer's avatar
Michael Natterer committed
589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618
    {
      r_n = i;
      g_n = i;
      b_n = i;

      r_n += cbd->cyan_red[SHADOWS] * cyan_red_transfer[SHADOWS][r_n];
      r_n = CLAMP0255 (r_n);
      r_n += cbd->cyan_red[MIDTONES] * cyan_red_transfer[MIDTONES][r_n];
      r_n = CLAMP0255 (r_n);
      r_n += cbd->cyan_red[HIGHLIGHTS] * cyan_red_transfer[HIGHLIGHTS][r_n];
      r_n = CLAMP0255 (r_n);

      g_n += cbd->magenta_green[SHADOWS] * magenta_green_transfer[SHADOWS][g_n];
      g_n = CLAMP0255 (g_n);
      g_n += cbd->magenta_green[MIDTONES] * magenta_green_transfer[MIDTONES][g_n];
      g_n = CLAMP0255 (g_n);
      g_n += cbd->magenta_green[HIGHLIGHTS] * magenta_green_transfer[HIGHLIGHTS][g_n];
      g_n = CLAMP0255 (g_n);

      b_n += cbd->yellow_blue[SHADOWS] * yellow_blue_transfer[SHADOWS][b_n];
      b_n = CLAMP0255 (b_n);
      b_n += cbd->yellow_blue[MIDTONES] * yellow_blue_transfer[MIDTONES][b_n];
      b_n = CLAMP0255 (b_n);
      b_n += cbd->yellow_blue[HIGHLIGHTS] * yellow_blue_transfer[HIGHLIGHTS][b_n];
      b_n = CLAMP0255 (b_n);

      cbd->r_lookup[i] = r_n;
      cbd->g_lookup[i] = g_n;
      cbd->b_lookup[i] = b_n;
    }
jaycox's avatar
jaycox committed
619 620
}

Elliot Lee's avatar
Elliot Lee committed
621 622 623 624
static void
color_balance_preview (ColorBalanceDialog *cbd)
{
  if (!cbd->image_map)
625 626 627 628
    {
      g_message ("color_balance_preview(): No image map");
      return;
    }
Michael Natterer's avatar
Michael Natterer committed
629

630
  active_tool->preserve = TRUE;
Michael Natterer's avatar
Michael Natterer committed
631
  color_balance_create_lookup_tables (cbd);
Elliot Lee's avatar
Elliot Lee committed
632
  image_map_apply (cbd->image_map, color_balance, (void *) cbd);
633
  active_tool->preserve = FALSE;
Elliot Lee's avatar
Elliot Lee committed
634 635
}

636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653
static void
color_balance_reset_callback (GtkWidget *widget,
			      gpointer   data)
{
  ColorBalanceDialog *cbd;

  cbd = (ColorBalanceDialog *) data;

  cbd->cyan_red[cbd->application_mode]      = 0.0;
  cbd->magenta_green[cbd->application_mode] = 0.0;
  cbd->yellow_blue[cbd->application_mode]   = 0.0;

  color_balance_update (cbd, ALL);

  if (cbd->preview)
    color_balance_preview (cbd);
}

Elliot Lee's avatar
Elliot Lee committed
654 655
static void
color_balance_ok_callback (GtkWidget *widget,
Michael Natterer's avatar
Michael Natterer committed
656
			   gpointer   data)
Elliot Lee's avatar
Elliot Lee committed
657 658 659
{
  ColorBalanceDialog *cbd;

Michael Natterer's avatar
Michael Natterer committed
660
  cbd = (ColorBalanceDialog *) data;
Elliot Lee's avatar
Elliot Lee committed
661

662
  gimp_dialog_hide (cbd->shell);
663 664
  
  active_tool->preserve = TRUE;
Elliot Lee's avatar
Elliot Lee committed
665 666 667 668 669 670 671

  if (!cbd->preview)
    image_map_apply (cbd->image_map, color_balance, (void *) cbd);

  if (cbd->image_map)
    image_map_commit (cbd->image_map);

672 673
  active_tool->preserve = FALSE;

Elliot Lee's avatar
Elliot Lee committed
674
  cbd->image_map = NULL;
675

676
  active_tool->gdisp    = NULL;
677
  active_tool->drawable = NULL;
Elliot Lee's avatar
Elliot Lee committed
678 679 680 681
}

static void
color_balance_cancel_callback (GtkWidget *widget,
Michael Natterer's avatar
Michael Natterer committed
682
			       gpointer   data)
Elliot Lee's avatar
Elliot Lee committed
683 684 685
{
  ColorBalanceDialog *cbd;

Michael Natterer's avatar
Michael Natterer committed
686 687
  cbd = (ColorBalanceDialog *) data;

688
  gimp_dialog_hide (cbd->shell);
Elliot Lee's avatar
Elliot Lee committed
689 690 691

  if (cbd->image_map)
    {
692
      active_tool->preserve = TRUE;
Elliot Lee's avatar
Elliot Lee committed
693
      image_map_abort (cbd->image_map);
694
      active_tool->preserve = FALSE;
695

Elliot Lee's avatar
Elliot Lee committed
696
      gdisplays_flush ();
697
      cbd->image_map = NULL;
Elliot Lee's avatar
Elliot Lee committed
698 699
    }

700
  active_tool->gdisp    = NULL;
701
  active_tool->drawable = NULL;
Elliot Lee's avatar
Elliot Lee committed
702 703 704
}

static void
705 706
color_balance_range_callback (GtkWidget *widget,
			      gpointer   data)
Elliot Lee's avatar
Elliot Lee committed
707 708
{
  ColorBalanceDialog *cbd;
709
  TransferMode range;
Elliot Lee's avatar
Elliot Lee committed
710

Michael Natterer's avatar
Michael Natterer committed
711 712
  cbd = (ColorBalanceDialog *) data;

713 714
  range = (TransferMode) gtk_object_get_user_data (GTK_OBJECT (widget));
  cbd->application_mode = range;
Michael Natterer's avatar
Michael Natterer committed
715

Elliot Lee's avatar
Elliot Lee committed
716 717 718 719
  color_balance_update (cbd, ALL);
}

static void
Michael Natterer's avatar
Michael Natterer committed
720
color_balance_preserve_update (GtkWidget *widget,
Elliot Lee's avatar
Elliot Lee committed
721 722 723 724 725 726
			       gpointer   data)
{
  ColorBalanceDialog *cbd;

  cbd = (ColorBalanceDialog *) data;

Michael Natterer's avatar
Michael Natterer committed
727 728
  if (GTK_TOGGLE_BUTTON (widget)->active)
    cbd->preserve_luminosity = TRUE;
Elliot Lee's avatar
Elliot Lee committed
729
  else
Michael Natterer's avatar
Michael Natterer committed
730 731 732 733
    cbd->preserve_luminosity = FALSE;

  if (cbd->preview)
    color_balance_preview (cbd);
Elliot Lee's avatar
Elliot Lee committed
734 735 736
}

static void
Michael Natterer's avatar
Michael Natterer committed
737
color_balance_preview_update (GtkWidget *widget,
Elliot Lee's avatar
Elliot Lee committed
738 739 740 741 742 743
			      gpointer   data)
{
  ColorBalanceDialog *cbd;

  cbd = (ColorBalanceDialog *) data;

Michael Natterer's avatar
Michael Natterer committed
744
  if (GTK_TOGGLE_BUTTON (widget)->active)
Elliot Lee's avatar
Elliot Lee committed
745 746 747 748 749
    {
      cbd->preview = TRUE;
      color_balance_preview (cbd);
    }
  else
750 751 752 753 754 755 756 757 758 759
    {
      cbd->preview = FALSE;
      if (cbd->image_map)
	{
	  active_tool->preserve = TRUE;
	  image_map_clear (cbd->image_map);
	  active_tool->preserve = FALSE;
	  gdisplays_flush ();
	}
    }
Elliot Lee's avatar
Elliot Lee committed
760 761 762
}

static void
Michael Natterer's avatar
Michael Natterer committed
763 764
color_balance_cr_adjustment_update (GtkAdjustment *adjustment,
				    gpointer       data)
Elliot Lee's avatar
Elliot Lee committed
765 766 767 768 769 770 771 772 773 774 775 776 777 778 779
{
  ColorBalanceDialog *cbd;

  cbd = (ColorBalanceDialog *) data;

  if (cbd->cyan_red[cbd->application_mode] != adjustment->value)
    {
      cbd->cyan_red[cbd->application_mode] = adjustment->value;

      if (cbd->preview)
	color_balance_preview (cbd);
    }
}

static void
Michael Natterer's avatar
Michael Natterer committed
780 781
color_balance_mg_adjustment_update (GtkAdjustment *adjustment,
				    gpointer       data)
Elliot Lee's avatar
Elliot Lee committed
782 783 784 785 786 787 788 789 790 791 792 793 794 795 796
{
  ColorBalanceDialog *cbd;

  cbd = (ColorBalanceDialog *) data;

  if (cbd->magenta_green[cbd->application_mode] != adjustment->value)
    {
      cbd->magenta_green[cbd->application_mode] = adjustment->value;

      if (cbd->preview)
	color_balance_preview (cbd);
    }
}

static void
Michael Natterer's avatar
Michael Natterer committed
797 798
color_balance_yb_adjustment_update (GtkAdjustment *adjustment,
				    gpointer       data)
Elliot Lee's avatar
Elliot Lee committed
799 800 801 802 803 804 805 806 807 808 809 810 811
{
  ColorBalanceDialog *cbd;

  cbd = (ColorBalanceDialog *) data;

  if (cbd->yellow_blue[cbd->application_mode] != adjustment->value)
    {
      cbd->yellow_blue[cbd->application_mode] = adjustment->value;

      if (cbd->preview)
	color_balance_preview (cbd);
    }
}