File: lut-funcs.c

package info (click to toggle)
gimp 2.2.13-1etch4
  • links: PTS
  • area: main
  • in suites: etch
  • size: 94,832 kB
  • ctags: 47,113
  • sloc: ansic: 524,858; xml: 36,798; lisp: 9,870; sh: 9,409; makefile: 7,923; python: 2,674; perl: 2,589; yacc: 520; lex: 334
file content (427 lines) | stat: -rw-r--r-- 8,724 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
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
157
158
159
160
161
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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
/* 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
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 */

#include "config.h"

#include <glib-object.h>

#include "libgimpmath/gimpmath.h"

#include "base-types.h"

#include "gimphistogram.h"
#include "gimplut.h"
#include "lut-funcs.h"


/* ---------- Brightness/Contrast -----------*/

typedef struct B_C_struct
{
  gdouble brightness;
  gdouble contrast;
} B_C_struct;

static gfloat
brightness_contrast_lut_func (B_C_struct *data,
			      gint        nchannels,
			      gint        channel,
			      gfloat      value)
{
  gfloat  nvalue;
  gdouble power;

  /* return the original value for the alpha channel */
  if ((nchannels == 2 || nchannels == 4) && channel == nchannels -1)
    return value;

  /* apply brightness */
  if (data->brightness < 0.0)
    value = value * (1.0 + data->brightness);
  else
    value = value + ((1.0 - value) * data->brightness);

  /* apply contrast */
  if (data->contrast < 0.0)
    {
      if (value > 0.5)
	nvalue = 1.0 - value;
      else
	nvalue = value;

      if (nvalue < 0.0)
	nvalue = 0.0;

      nvalue = 0.5 * pow (nvalue * 2.0 , (double) (1.0 + data->contrast));

      if (value > 0.5)
	value = 1.0 - nvalue;
      else
	value = nvalue;
    }
  else
    {
      if (value > 0.5)
	nvalue = 1.0 - value;
      else
	nvalue = value;

      if (nvalue < 0.0)
	nvalue = 0.0;

      power = (data->contrast == 1.0) ? 127 : 1.0 / (1.0 - data->contrast);
      nvalue = 0.5 * pow (2.0 * nvalue, power);

      if (value > 0.5)
	value = 1.0 - nvalue;
      else
	value = nvalue;
    }

  return value;
}

GimpLut *
brightness_contrast_lut_new (gdouble brightness,
			     gdouble contrast,
			     gint    n_channels)
{
  GimpLut *lut;

  lut = gimp_lut_new ();

  brightness_contrast_lut_setup (lut, brightness, contrast, n_channels);

  return lut;
}

void
brightness_contrast_lut_setup (GimpLut *lut,
			       gdouble  brightness,
			       gdouble  contrast,
			       gint     n_channels)
{
  B_C_struct data;

  g_return_if_fail (lut != NULL);

  data.brightness = brightness;
  data.contrast   = contrast;

  gimp_lut_setup (lut, (GimpLutFunc) brightness_contrast_lut_func,
		  (gpointer) &data, n_channels);
}

/* ---------------- invert ------------------ */

static gfloat
invert_lut_func (gpointer  unused,
		 gint      n_channels,
		 gint      channel,
		 gfloat    value)
{
  /* don't invert the alpha channel */
  if ((n_channels == 2 || n_channels == 4) && channel == n_channels -1)
    return value;

  return 1.0 - value;
}

GimpLut *
invert_lut_new (gint n_channels)
{
  GimpLut *lut;

  lut = gimp_lut_new ();

  invert_lut_setup (lut, n_channels);

  return lut;
}

void
invert_lut_setup (GimpLut *lut,
		  gint     n_channels)
{
  g_return_if_fail (lut != NULL);

  gimp_lut_setup_exact (lut, (GimpLutFunc) invert_lut_func,
			NULL , n_channels);
}

/* ---------------- add (or subract)------------------ */

static gfloat
add_lut_func (gdouble *amount,
	      gint     n_channels,
	      gint     channel,
	      gfloat   value)
{
  /* don't change the alpha channel */
  if ((n_channels == 2 || n_channels == 4) && channel == n_channels -1)
    return value;

  return (value + *amount);
}

GimpLut *
add_lut_new (gdouble amount,
	     gint    n_channels)
{
  GimpLut *lut;

  lut = gimp_lut_new ();

  add_lut_setup (lut, amount, n_channels);

  return lut;
}

void
add_lut_setup (GimpLut *lut,
	       gdouble  amount,
	       gint     n_channels)
{
  g_return_if_fail (lut != NULL);

  gimp_lut_setup (lut, (GimpLutFunc) add_lut_func,
		  (gpointer) &amount, n_channels);
}

/* ---------------- intersect (MIN (pixel, value)) ------------------ */

static gfloat
intersect_lut_func (gdouble *min,
		    gint     n_channels,
		    gint     channel,
		    gfloat   value)
{
  /* don't change the alpha channel */
  if ((n_channels == 2 || n_channels == 4) && channel == n_channels -1)
    return value;

  return MIN (value, *min);
}

GimpLut *
intersect_lut_new (gdouble value,
		   gint    n_channels)
{
  GimpLut *lut;

  lut = gimp_lut_new ();

  intersect_lut_setup (lut, value, n_channels);

  return lut;
}

void
intersect_lut_setup (GimpLut *lut,
		     gdouble  value,
		     gint     n_channels)
{
  g_return_if_fail (lut != NULL);

  gimp_lut_setup_exact (lut, (GimpLutFunc) intersect_lut_func,
			(gpointer) &value , n_channels);
}

/* ---------------- Threshold ------------------ */

static gfloat
threshold_lut_func (gdouble *min,
		    gint     n_channels,
		    gint     channel,
		    gfloat   value)
{
  /* don't change the alpha channel */
  if ((n_channels == 2 || n_channels == 4) && channel == n_channels -1)
    return value;

  if (value < *min)
    return 0.0;

  return 1.0;
}

GimpLut *
threshold_lut_new (gdouble value,
		   gint    n_channels)
{
  GimpLut *lut;

  lut = gimp_lut_new ();

  threshold_lut_setup (lut, value, n_channels);

  return lut;
}

void
threshold_lut_setup (GimpLut *lut,
		     gdouble  value,
		     gint     n_channels)
{
  g_return_if_fail (lut != NULL);

  gimp_lut_setup_exact (lut, (GimpLutFunc) threshold_lut_func,
			(gpointer) &value , n_channels);
}

/* --------------- posterize ---------------- */

static gfloat
posterize_lut_func (gint   *ilevels,
		    gint    n_channels,
		    gint    channel,
		    gfloat  value)
{
  gint levels;

  /* don't posterize the alpha channel */
  if ((n_channels == 2 || n_channels == 4) && channel == n_channels -1)
    return value;

  if (*ilevels < 2)
    levels = 2;
  else
    levels = *ilevels;

  value = RINT (value * (levels - 1.0)) / (levels - 1.0);

  return value;
}

GimpLut *
posterize_lut_new (gint levels,
		   gint n_channels)
{
  GimpLut *lut;

  lut = gimp_lut_new ();

  posterize_lut_setup (lut, levels, n_channels);

  return lut;
}

void
posterize_lut_setup (GimpLut *lut,
		     gint     levels,
		     gint     n_channels)
{
  g_return_if_fail (lut != NULL);

  gimp_lut_setup_exact (lut, (GimpLutFunc) posterize_lut_func,
			(gpointer) &levels, n_channels);
}

/* --------------- equalize ------------- */

typedef struct
{
  GimpHistogram *histogram;
  gint           part[5][257];
} hist_lut_struct;

static gfloat
equalize_lut_func (hist_lut_struct *hlut,
		   gint             n_channels,
		   gint             channel,
		   gfloat           value)
{
  gint i = 0;
  gint j;

  j = (gint) (value * 255.0 + 0.5);

  while (hlut->part[channel][i + 1] <= j)
    i++;

  return i / 255.0;
}

GimpLut *
eq_histogram_lut_new (GimpHistogram *histogram,
		      gint           n_channels)
{
  GimpLut *lut;

  g_return_val_if_fail (histogram != NULL, NULL);

  lut = gimp_lut_new ();

  eq_histogram_lut_setup (lut, histogram, n_channels);

  return lut;
}

void
eq_histogram_lut_setup (GimpLut       *lut,
			GimpHistogram *hist,
			gint           n_channels)
{
  gint            i, k, j;
  hist_lut_struct hlut;
  gdouble         pixels_per_value;
  gdouble         desired;
  gdouble         sum, dif;

  g_return_if_fail (lut != NULL);
  g_return_if_fail (hist != NULL);

  /* Find partition points */
  pixels_per_value = gimp_histogram_get_count (hist,
                                               GIMP_HISTOGRAM_VALUE,
                                               0, 255) / 256.0;

  for (k = 0; k < n_channels; k++)
    {
      /* First and last points in partition */
      hlut.part[k][0]   = 0;
      hlut.part[k][256] = 256;

      /* Find intermediate points */
      j   = 0;
      sum = (gimp_histogram_get_channel (hist, k, 0) +
	     gimp_histogram_get_channel (hist, k, 1));

      for (i = 1; i < 256; i++)
	{
	  desired = i * pixels_per_value;

	  while (sum <= desired)
	    {
	      j++;
	      sum += gimp_histogram_get_channel (hist, k, j + 1);
	    }

	  /* Nearest sum */
	  dif = sum - gimp_histogram_get_channel (hist, k, j);

	  if ((sum - desired) > (dif / 2.0))
	    hlut.part[k][i] = j;
	  else
	    hlut.part[k][i] = j + 1;
	}
    }

  gimp_lut_setup (lut, (GimpLutFunc) equalize_lut_func,
		  (gpointer) &hlut, n_channels);
}