brightness_contrast.c 12.9 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
18
19
20
 */
#include "appenv.h"
#include "brightness_contrast.h"
#include "drawable.h"
21
#include "gimplut.h"
22
#include "gimpui.h"
Elliot Lee's avatar
Elliot Lee committed
23
24
#include "gdisplay.h"
#include "image_map.h"
25
#include "lut_funcs.h"
Elliot Lee's avatar
Elliot Lee committed
26

Sven Neumann's avatar
Sven Neumann committed
27
#include "config.h"
28
29
#include "libgimp/gimpintl.h"

Elliot Lee's avatar
Elliot Lee committed
30
31
#define SLIDER_WIDTH 200

32
33
34
#define BRIGHTNESS  0x1
#define CONTRAST    0x2
#define ALL        (BRIGHTNESS | CONTRAST)
Elliot Lee's avatar
Elliot Lee committed
35

36
/*  the brightness-contrast structures  */
Elliot Lee's avatar
Elliot Lee committed
37

38
typedef struct _BrightnessContrast BrightnessContrast;
39

Elliot Lee's avatar
Elliot Lee committed
40
41
struct _BrightnessContrast
{
42
  gint x, y;    /*  coords for last mouse click  */
Elliot Lee's avatar
Elliot Lee committed
43
44
45
};

typedef struct _BrightnessContrastDialog BrightnessContrastDialog;
46

Elliot Lee's avatar
Elliot Lee committed
47
48
struct _BrightnessContrastDialog
{
49
50
  GtkWidget     *shell;
  GtkWidget     *gimage_name;
Elliot Lee's avatar
Elliot Lee committed
51

52
53
  GtkAdjustment *brightness_data;
  GtkAdjustment *contrast_data;
Elliot Lee's avatar
Elliot Lee committed
54

55
56
  GimpDrawable  *drawable;
  ImageMap       image_map;
Elliot Lee's avatar
Elliot Lee committed
57

58
59
  gdouble        brightness;
  gdouble        contrast;
60

61
62
63
  gboolean       preview;

  GimpLut       *lut;
Elliot Lee's avatar
Elliot Lee committed
64
65
};

66
67
68
69
70
71
72
73

/*  the brightness-contrast tool options  */
static ToolOptions *brightness_contrast_options = NULL;

/*  the brightness-contrast dialog  */
static BrightnessContrastDialog *brightness_contrast_dialog = NULL;


Elliot Lee's avatar
Elliot Lee committed
74
75
/*  brightness contrast action functions  */

76
static void   brightness_contrast_control (Tool *, ToolAction, gpointer);
Elliot Lee's avatar
Elliot Lee committed
77

78
static BrightnessContrastDialog * brightness_contrast_dialog_new (void);
Elliot Lee's avatar
Elliot Lee committed
79

80
81
82
83
84
85
86
87
88
89
90
static void   brightness_contrast_update          (BrightnessContrastDialog *,
						   gint);
static void   brightness_contrast_preview         (BrightnessContrastDialog *);
static void   brightness_contrast_reset_callback  (GtkWidget *, gpointer);
static void   brightness_contrast_ok_callback     (GtkWidget *, gpointer);
static void   brightness_contrast_cancel_callback (GtkWidget *, gpointer);
static void   brightness_contrast_preview_update  (GtkWidget *, gpointer);
static void   brightness_contrast_brightness_adjustment_update (GtkAdjustment *,
								gpointer);
static void   brightness_contrast_contrast_adjustment_update   (GtkAdjustment *,
								gpointer);
Elliot Lee's avatar
Elliot Lee committed
91

92
/*  brightness-contrast select action functions  */
Elliot Lee's avatar
Elliot Lee committed
93
94

static void
Michael Natterer's avatar
Michael Natterer committed
95
96
97
brightness_contrast_control (Tool       *tool,
			     ToolAction  action,
			     gpointer    gdisp_ptr)
Elliot Lee's avatar
Elliot Lee committed
98
99
100
{
  switch (action)
    {
Michael Natterer's avatar
Michael Natterer committed
101
    case PAUSE:
Elliot Lee's avatar
Elliot Lee committed
102
      break;
Michael Natterer's avatar
Michael Natterer committed
103
104

    case RESUME:
Elliot Lee's avatar
Elliot Lee committed
105
      break;
Michael Natterer's avatar
Michael Natterer committed
106
107

    case HALT:
Elliot Lee's avatar
Elliot Lee committed
108
      if (brightness_contrast_dialog)
109
	brightness_contrast_cancel_callback (NULL, (gpointer) brightness_contrast_dialog);
Elliot Lee's avatar
Elliot Lee committed
110
      break;
Michael Natterer's avatar
Michael Natterer committed
111
112
113

    default:
      break;
Elliot Lee's avatar
Elliot Lee committed
114
115
116
117
    }
}

Tool *
118
tools_new_brightness_contrast (void)
Elliot Lee's avatar
Elliot Lee committed
119
120
121
122
123
124
{
  Tool * tool;
  BrightnessContrast * private;

  /*  The tool options  */
  if (!brightness_contrast_options)
125
    {
126
      brightness_contrast_options =
127
	tool_options_new (_("Brightness-Contrast"));
128
129
      tools_register (BRIGHTNESS_CONTRAST, brightness_contrast_options);
   }
Elliot Lee's avatar
Elliot Lee committed
130

131
132
  tool = tools_new_tool (BRIGHTNESS_CONTRAST);
  private = g_new (BrightnessContrast, 1);
Elliot Lee's avatar
Elliot Lee committed
133

134
135
  tool->scroll_lock = TRUE;   /*  Disallow scrolling  */
  tool->preserve    = FALSE;  /*  Don't preserve on drawable change  */
136

137
  tool->private = (void *) private;
138

Elliot Lee's avatar
Elliot Lee committed
139
140
141
142
143
144
145
146
147
148
149
150
  tool->control_func = brightness_contrast_control;

  return tool;
}

void
tools_free_brightness_contrast (Tool *tool)
{
  BrightnessContrast * bc;

  bc = (BrightnessContrast *) tool->private;

151
  /*  Close the brightness-contrast dialog  */
Elliot Lee's avatar
Elliot Lee committed
152
  if (brightness_contrast_dialog)
153
    brightness_contrast_cancel_callback (NULL, (gpointer) brightness_contrast_dialog);
Elliot Lee's avatar
Elliot Lee committed
154
155
156
157
158

  g_free (bc);
}

void
159
brightness_contrast_initialize (GDisplay *gdisp)
Elliot Lee's avatar
Elliot Lee committed
160
161
162
{
  if (drawable_indexed (gimage_active_drawable (gdisp->gimage)))
    {
163
      g_message (_("Brightness-Contrast does not operate on indexed drawables."));
Elliot Lee's avatar
Elliot Lee committed
164
165
166
167
168
      return;
    }

  /*  The brightness-contrast dialog  */
  if (!brightness_contrast_dialog)
169
    brightness_contrast_dialog = brightness_contrast_dialog_new ();
Elliot Lee's avatar
Elliot Lee committed
170
171
172
173
174
  else
    if (!GTK_WIDGET_VISIBLE (brightness_contrast_dialog->shell))
      gtk_widget_show (brightness_contrast_dialog->shell);

  brightness_contrast_dialog->brightness = 0.0;
175
  brightness_contrast_dialog->contrast   = 0.0;
Elliot Lee's avatar
Elliot Lee committed
176

177
  brightness_contrast_dialog->drawable = gimage_active_drawable (gdisp->gimage);
178
179
  brightness_contrast_dialog->image_map =
    image_map_create (gdisp, brightness_contrast_dialog->drawable);
Elliot Lee's avatar
Elliot Lee committed
180
181
182
183
184
185
186
187

  brightness_contrast_update (brightness_contrast_dialog, ALL);
}

/********************************/
/*  Brightness Contrast dialog  */
/********************************/

188
static BrightnessContrastDialog *
189
brightness_contrast_dialog_new (void)
Elliot Lee's avatar
Elliot Lee committed
190
191
192
193
194
195
{
  BrightnessContrastDialog *bcd;
  GtkWidget *vbox;
  GtkWidget *hbox;
  GtkWidget *table;
  GtkWidget *label;
196
197
  GtkWidget *abox;
  GtkWidget *spinbutton;
Elliot Lee's avatar
Elliot Lee committed
198
199
200
201
  GtkWidget *slider;
  GtkWidget *toggle;
  GtkObject *data;

202
  bcd = g_new (BrightnessContrastDialog, 1);
Elliot Lee's avatar
Elliot Lee committed
203
204
  bcd->preview = TRUE;

205
  bcd->lut = gimp_lut_new ();
206

Elliot Lee's avatar
Elliot Lee committed
207
  /*  The shell and main vbox  */
208
209
210
211
212
  bcd->shell =
    gimp_dialog_new (_("Brightness-Contrast"), "brightness_contrast",
		     tools_help_func, NULL,
		     GTK_WIN_POS_NONE,
		     FALSE, TRUE, FALSE,
213

214
		     _("OK"), brightness_contrast_ok_callback,
215
		     bcd, NULL, NULL, TRUE, FALSE,
216
		     _("Reset"), brightness_contrast_reset_callback,
217
		     bcd, NULL, NULL, TRUE, FALSE,
218
		     _("Cancel"), brightness_contrast_cancel_callback,
219
		     bcd, NULL, NULL, FALSE, TRUE,
220
221

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

223
224
  vbox = gtk_vbox_new (FALSE, 4);
  gtk_container_set_border_width (GTK_CONTAINER (vbox), 4);
225
  gtk_container_add (GTK_CONTAINER (GTK_DIALOG (bcd->shell)->vbox), vbox);
Elliot Lee's avatar
Elliot Lee committed
226
227
228

  /*  The table containing sliders  */
  table = gtk_table_new (2, 3, FALSE);
229
230
  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
231
232
233
  gtk_box_pack_start (GTK_BOX (vbox), table, FALSE, FALSE, 0);

  /*  Create the brightness scale widget  */
234
  label = gtk_label_new (_("Brightness"));
235
  gtk_misc_set_alignment (GTK_MISC (label), 1.0, 1.0);
Elliot Lee's avatar
Elliot Lee committed
236
  gtk_table_attach (GTK_TABLE (table), label, 0, 1, 0, 1,
237
		    GTK_SHRINK | GTK_FILL, GTK_SHRINK | GTK_FILL, 0, 0);
Elliot Lee's avatar
Elliot Lee committed
238

239
  data = gtk_adjustment_new (0, -127, 127.0, 1.0, 10.0, 0.0);
Elliot Lee's avatar
Elliot Lee committed
240
241
  bcd->brightness_data = GTK_ADJUSTMENT (data);
  slider = gtk_hscale_new (GTK_ADJUSTMENT (data));
242
  gtk_widget_set_usize (slider, SLIDER_WIDTH, -1);
Elliot Lee's avatar
Elliot Lee committed
243
244
245
  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);
246
  gtk_table_attach_defaults (GTK_TABLE (table), slider, 1, 2, 0, 1);
Elliot Lee's avatar
Elliot Lee committed
247

248
249
250
251
252
253
254
255
256
  abox = gtk_vbox_new (FALSE, 0);
  spinbutton = gtk_spin_button_new (bcd->brightness_data, 1.0, 0);
  gtk_widget_set_usize (spinbutton, 75, -1);
  gtk_box_pack_end (GTK_BOX (abox), spinbutton, FALSE, FALSE, 0);
  gtk_table_attach (GTK_TABLE (table), abox, 2, 3, 0, 1,
		    GTK_SHRINK | GTK_FILL, GTK_SHRINK | GTK_FILL, 0, 0);

  gtk_signal_connect (GTK_OBJECT (data), "value_changed",
		      GTK_SIGNAL_FUNC (brightness_contrast_brightness_adjustment_update),
Elliot Lee's avatar
Elliot Lee committed
257
258
259
260
		      bcd);

  gtk_widget_show (label);
  gtk_widget_show (slider);
261
262
  gtk_widget_show (spinbutton);
  gtk_widget_show (abox);
Elliot Lee's avatar
Elliot Lee committed
263
264

  /*  Create the contrast scale widget  */
265
  label = gtk_label_new (_("Contrast"));
266
  gtk_misc_set_alignment (GTK_MISC (label), 1.0, 1.0);
Elliot Lee's avatar
Elliot Lee committed
267
  gtk_table_attach (GTK_TABLE (table), label, 0, 1, 1, 2,
268
		    GTK_SHRINK | GTK_FILL, GTK_SHRINK | GTK_FILL, 0, 0);
Elliot Lee's avatar
Elliot Lee committed
269

270
  data = gtk_adjustment_new (0, -127.0, 127.0, 1.0, 10.0, 0.0);
Elliot Lee's avatar
Elliot Lee committed
271
272
  bcd->contrast_data = GTK_ADJUSTMENT (data);
  slider = gtk_hscale_new (GTK_ADJUSTMENT (data));
273
  gtk_widget_set_usize (slider, SLIDER_WIDTH, -1);
Elliot Lee's avatar
Elliot Lee committed
274
275
276
  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);
277
  gtk_table_attach_defaults (GTK_TABLE (table), slider, 1, 2, 1, 2);
Elliot Lee's avatar
Elliot Lee committed
278

279
280
281
282
283
284
285
286
287
  abox = gtk_vbox_new (FALSE, 0);
  spinbutton = gtk_spin_button_new (bcd->contrast_data, 1.0, 0);
  gtk_widget_set_usize (spinbutton, 75, -1);
  gtk_box_pack_end (GTK_BOX (abox), spinbutton, FALSE, FALSE, 0);
  gtk_table_attach (GTK_TABLE (table), abox, 2, 3, 1, 2,
		    GTK_SHRINK | GTK_FILL, GTK_SHRINK | GTK_FILL, 0, 0);

  gtk_signal_connect (GTK_OBJECT (data), "value_changed",
		      GTK_SIGNAL_FUNC (brightness_contrast_contrast_adjustment_update),
Elliot Lee's avatar
Elliot Lee committed
288
289
290
291
		      bcd);

  gtk_widget_show (label);
  gtk_widget_show (slider);
292
293
  gtk_widget_show (spinbutton);
  gtk_widget_show (abox);
Elliot Lee's avatar
Elliot Lee committed
294

295
296
297
  /*  Horizontal box for preview toggle button  */
  hbox = gtk_hbox_new (FALSE, 4);
  gtk_box_pack_end (GTK_BOX (vbox), hbox, FALSE, FALSE, 0);
Elliot Lee's avatar
Elliot Lee committed
298
299

  /*  The preview toggle  */
300
  toggle = gtk_check_button_new_with_label (_("Preview"));
301
  gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (toggle), bcd->preview);
302
  gtk_box_pack_end (GTK_BOX (hbox), toggle, FALSE, FALSE, 0);
Elliot Lee's avatar
Elliot Lee committed
303
  gtk_signal_connect (GTK_OBJECT (toggle), "toggled",
304
		      GTK_SIGNAL_FUNC (brightness_contrast_preview_update),
Elliot Lee's avatar
Elliot Lee committed
305
306
307
308
309
310
311
312
313
314
315
316
317
318
		      bcd);

  gtk_widget_show (toggle);
  gtk_widget_show (hbox);

  gtk_widget_show (table);
  gtk_widget_show (vbox);
  gtk_widget_show (bcd->shell);

  return bcd;
}

static void
brightness_contrast_update (BrightnessContrastDialog *bcd,
319
			    gint                      update)
Elliot Lee's avatar
Elliot Lee committed
320
{
321
  if (update & BRIGHTNESS)
Elliot Lee's avatar
Elliot Lee committed
322
    {
323
      gtk_adjustment_set_value (bcd->brightness_data, bcd->brightness);
Elliot Lee's avatar
Elliot Lee committed
324
    }
325
  if (update & CONTRAST)
Elliot Lee's avatar
Elliot Lee committed
326
    {
327
      gtk_adjustment_set_value (bcd->contrast_data, bcd->contrast);
Elliot Lee's avatar
Elliot Lee committed
328
329
330
331
332
333
334
    }
}

static void
brightness_contrast_preview (BrightnessContrastDialog *bcd)
{
  if (!bcd->image_map)
335
336
337
338
339
    {
      g_message ("brightness_contrast_preview(): No image map");
      return;
    }

340
  active_tool->preserve = TRUE;
341
342
343
344
  brightness_contrast_lut_setup (bcd->lut, bcd->brightness / 255.0,
				 bcd->contrast / 127.0,
				 gimp_drawable_bytes (bcd->drawable));
  image_map_apply (bcd->image_map, (ImageMapApplyFunc) gimp_lut_process_2,
345
		   (void *) bcd->lut);
346
  active_tool->preserve = FALSE;
Elliot Lee's avatar
Elliot Lee committed
347
348
}

349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
static void
brightness_contrast_reset_callback (GtkWidget *widget,
				    gpointer   data)
{
  BrightnessContrastDialog *bcd;

  bcd = (BrightnessContrastDialog *) data;

  bcd->brightness = 0.0;
  bcd->contrast   = 0.0;

  brightness_contrast_update (bcd, ALL);

  if (bcd->preview)
    brightness_contrast_preview (bcd);
}

Elliot Lee's avatar
Elliot Lee committed
366
367
static void
brightness_contrast_ok_callback (GtkWidget *widget,
368
				 gpointer   data)
Elliot Lee's avatar
Elliot Lee committed
369
370
371
{
  BrightnessContrastDialog *bcd;

372
  bcd = (BrightnessContrastDialog *) data;
Elliot Lee's avatar
Elliot Lee committed
373
374
375
376

  if (GTK_WIDGET_VISIBLE (bcd->shell))
    gtk_widget_hide (bcd->shell);

377
378
  active_tool->preserve = TRUE;

Elliot Lee's avatar
Elliot Lee committed
379
  if (!bcd->preview)
380
  {
381
382
383
384
    brightness_contrast_lut_setup (bcd->lut, bcd->brightness / 255.0,
				   bcd->contrast / 127.0,
				   gimp_drawable_bytes (bcd->drawable));
    image_map_apply (bcd->image_map, (ImageMapApplyFunc) gimp_lut_process_2,
385
386
		     (void *) bcd->lut);
  }
Elliot Lee's avatar
Elliot Lee committed
387
388
389
390

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

391
392
  active_tool->preserve = FALSE;

Elliot Lee's avatar
Elliot Lee committed
393
  bcd->image_map = NULL;
394
395
396

  active_tool->gdisp_ptr = NULL;
  active_tool->drawable = NULL;
Elliot Lee's avatar
Elliot Lee committed
397
398
399
400
}

static void
brightness_contrast_cancel_callback (GtkWidget *widget,
401
				     gpointer   data)
Elliot Lee's avatar
Elliot Lee committed
402
403
404
{
  BrightnessContrastDialog *bcd;

405
406
  bcd = (BrightnessContrastDialog *) data;

Elliot Lee's avatar
Elliot Lee committed
407
408
409
410
411
  if (GTK_WIDGET_VISIBLE (bcd->shell))
    gtk_widget_hide (bcd->shell);

  if (bcd->image_map)
    {
412
      active_tool->preserve = TRUE;
Elliot Lee's avatar
Elliot Lee committed
413
      image_map_abort (bcd->image_map);
414
      active_tool->preserve = FALSE;
415
416

      bcd->image_map = NULL;
Elliot Lee's avatar
Elliot Lee committed
417
418
      gdisplays_flush ();
    }
419
420
421

  active_tool->gdisp_ptr = NULL;
  active_tool->drawable = NULL;
Elliot Lee's avatar
Elliot Lee committed
422
423
424
}

static void
425
brightness_contrast_preview_update (GtkWidget *widget,
Elliot Lee's avatar
Elliot Lee committed
426
427
428
429
430
431
				    gpointer   data)
{
  BrightnessContrastDialog *bcd;

  bcd = (BrightnessContrastDialog *) data;

432
  if (GTK_TOGGLE_BUTTON (widget)->active)
Elliot Lee's avatar
Elliot Lee committed
433
434
435
436
437
438
439
440
441
    {
      bcd->preview = TRUE;
      brightness_contrast_preview (bcd);
    }
  else
    bcd->preview = FALSE;
}

static void
442
443
brightness_contrast_brightness_adjustment_update (GtkAdjustment *adjustment,
						  gpointer       data)
Elliot Lee's avatar
Elliot Lee committed
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
{
  BrightnessContrastDialog *bcd;

  bcd = (BrightnessContrastDialog *) data;

  if (bcd->brightness != adjustment->value)
    {
      bcd->brightness = adjustment->value;

      if (bcd->preview)
	brightness_contrast_preview (bcd);
    }
}

static void
459
460
brightness_contrast_contrast_adjustment_update (GtkAdjustment *adjustment,
						gpointer       data)
Elliot Lee's avatar
Elliot Lee committed
461
462
463
464
465
466
467
468
469
470
471
472
473
{
  BrightnessContrastDialog *bcd;

  bcd = (BrightnessContrastDialog *) data;

  if (bcd->contrast != adjustment->value)
    {
      bcd->contrast = adjustment->value;

      if (bcd->preview)
	brightness_contrast_preview (bcd);
    }
}