change the LUT function for the contrast. Fixes the buggy behaviour as

2006-02-24  Simon Budig  <simon@gimp.org>

	* app/base/lut-funcs.c: change the LUT function for the contrast.
	Fixes the buggy behaviour as described in bug #319872 and
	bug #332068 and makes the behaviour consistent with the standard
	contrast formula. However, I am leaving the bug open to discuss
	further improvements.

	Untabbified.
This commit is contained in:
Simon Budig 2006-02-24 01:04:48 +00:00 committed by Simon Budig
parent 3343baa84c
commit 78c41aa68f
2 changed files with 77 additions and 102 deletions

View file

@ -1,3 +1,13 @@
2006-02-24 Simon Budig <simon@gimp.org>
* app/base/lut-funcs.c: change the LUT function for the contrast.
Fixes the buggy behaviour as described in bug #319872 and
bug #332068 and makes the behaviour consistent with the standard
contrast formula. However, I am leaving the bug open to discuss
further improvements.
Untabbified.
2006-02-23 Sven Neumann <sven@gimp.org> 2006-02-23 Sven Neumann <sven@gimp.org>
* libgimpbase/gimpwire.[ch]: added utility functions to send * libgimpbase/gimpwire.[ch]: added utility functions to send

View file

@ -39,12 +39,11 @@ typedef struct B_C_struct
static gfloat static gfloat
brightness_contrast_lut_func (B_C_struct *data, brightness_contrast_lut_func (B_C_struct *data,
gint nchannels, gint nchannels,
gint channel, gint channel,
gfloat value) gfloat value)
{ {
gfloat nvalue; gdouble slant;
gdouble power;
/* return the original value for the alpha channel */ /* return the original value for the alpha channel */
if ((nchannels == 2 || nchannels == 4) && channel == nchannels -1) if ((nchannels == 2 || nchannels == 4) && channel == nchannels -1)
@ -56,50 +55,16 @@ brightness_contrast_lut_func (B_C_struct *data,
else else
value = value + ((1.0 - value) * data->brightness); value = value + ((1.0 - value) * data->brightness);
/* apply contrast */ slant = tan ((data->contrast + 1) * G_PI_4);
if (data->contrast < 0.0) value = (value - 0.5) * slant + 0.5;
{
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; return value;
} }
GimpLut * GimpLut *
brightness_contrast_lut_new (gdouble brightness, brightness_contrast_lut_new (gdouble brightness,
gdouble contrast, gdouble contrast,
gint n_channels) gint n_channels)
{ {
GimpLut *lut; GimpLut *lut;
@ -112,9 +77,9 @@ brightness_contrast_lut_new (gdouble brightness,
void void
brightness_contrast_lut_setup (GimpLut *lut, brightness_contrast_lut_setup (GimpLut *lut,
gdouble brightness, gdouble brightness,
gdouble contrast, gdouble contrast,
gint n_channels) gint n_channels)
{ {
B_C_struct data; B_C_struct data;
@ -124,16 +89,16 @@ brightness_contrast_lut_setup (GimpLut *lut,
data.contrast = contrast; data.contrast = contrast;
gimp_lut_setup (lut, (GimpLutFunc) brightness_contrast_lut_func, gimp_lut_setup (lut, (GimpLutFunc) brightness_contrast_lut_func,
(gpointer) &data, n_channels); (gpointer) &data, n_channels);
} }
/* ---------------- invert ------------------ */ /* ---------------- invert ------------------ */
static gfloat static gfloat
invert_lut_func (gpointer unused, invert_lut_func (gpointer unused,
gint n_channels, gint n_channels,
gint channel, gint channel,
gfloat value) gfloat value)
{ {
/* don't invert the alpha channel */ /* don't invert the alpha channel */
if ((n_channels == 2 || n_channels == 4) && channel == n_channels -1) if ((n_channels == 2 || n_channels == 4) && channel == n_channels -1)
@ -156,21 +121,21 @@ invert_lut_new (gint n_channels)
void void
invert_lut_setup (GimpLut *lut, invert_lut_setup (GimpLut *lut,
gint n_channels) gint n_channels)
{ {
g_return_if_fail (lut != NULL); g_return_if_fail (lut != NULL);
gimp_lut_setup_exact (lut, (GimpLutFunc) invert_lut_func, gimp_lut_setup_exact (lut, (GimpLutFunc) invert_lut_func,
NULL , n_channels); NULL , n_channels);
} }
/* ---------------- add (or subract)------------------ */ /* ---------------- add (or subract)------------------ */
static gfloat static gfloat
add_lut_func (gdouble *amount, add_lut_func (gdouble *amount,
gint n_channels, gint n_channels,
gint channel, gint channel,
gfloat value) gfloat value)
{ {
/* don't change the alpha channel */ /* don't change the alpha channel */
if ((n_channels == 2 || n_channels == 4) && channel == n_channels -1) if ((n_channels == 2 || n_channels == 4) && channel == n_channels -1)
@ -181,7 +146,7 @@ add_lut_func (gdouble *amount,
GimpLut * GimpLut *
add_lut_new (gdouble amount, add_lut_new (gdouble amount,
gint n_channels) gint n_channels)
{ {
GimpLut *lut; GimpLut *lut;
@ -194,22 +159,22 @@ add_lut_new (gdouble amount,
void void
add_lut_setup (GimpLut *lut, add_lut_setup (GimpLut *lut,
gdouble amount, gdouble amount,
gint n_channels) gint n_channels)
{ {
g_return_if_fail (lut != NULL); g_return_if_fail (lut != NULL);
gimp_lut_setup (lut, (GimpLutFunc) add_lut_func, gimp_lut_setup (lut, (GimpLutFunc) add_lut_func,
(gpointer) &amount, n_channels); (gpointer) &amount, n_channels);
} }
/* ---------------- intersect (MIN (pixel, value)) ------------------ */ /* ---------------- intersect (MIN (pixel, value)) ------------------ */
static gfloat static gfloat
intersect_lut_func (gdouble *min, intersect_lut_func (gdouble *min,
gint n_channels, gint n_channels,
gint channel, gint channel,
gfloat value) gfloat value)
{ {
/* don't change the alpha channel */ /* don't change the alpha channel */
if ((n_channels == 2 || n_channels == 4) && channel == n_channels -1) if ((n_channels == 2 || n_channels == 4) && channel == n_channels -1)
@ -220,7 +185,7 @@ intersect_lut_func (gdouble *min,
GimpLut * GimpLut *
intersect_lut_new (gdouble value, intersect_lut_new (gdouble value,
gint n_channels) gint n_channels)
{ {
GimpLut *lut; GimpLut *lut;
@ -233,22 +198,22 @@ intersect_lut_new (gdouble value,
void void
intersect_lut_setup (GimpLut *lut, intersect_lut_setup (GimpLut *lut,
gdouble value, gdouble value,
gint n_channels) gint n_channels)
{ {
g_return_if_fail (lut != NULL); g_return_if_fail (lut != NULL);
gimp_lut_setup_exact (lut, (GimpLutFunc) intersect_lut_func, gimp_lut_setup_exact (lut, (GimpLutFunc) intersect_lut_func,
(gpointer) &value , n_channels); (gpointer) &value , n_channels);
} }
/* ---------------- Threshold ------------------ */ /* ---------------- Threshold ------------------ */
static gfloat static gfloat
threshold_lut_func (gdouble *min, threshold_lut_func (gdouble *min,
gint n_channels, gint n_channels,
gint channel, gint channel,
gfloat value) gfloat value)
{ {
/* don't change the alpha channel */ /* don't change the alpha channel */
if ((n_channels == 2 || n_channels == 4) && channel == n_channels -1) if ((n_channels == 2 || n_channels == 4) && channel == n_channels -1)
@ -262,7 +227,7 @@ threshold_lut_func (gdouble *min,
GimpLut * GimpLut *
threshold_lut_new (gdouble value, threshold_lut_new (gdouble value,
gint n_channels) gint n_channels)
{ {
GimpLut *lut; GimpLut *lut;
@ -275,22 +240,22 @@ threshold_lut_new (gdouble value,
void void
threshold_lut_setup (GimpLut *lut, threshold_lut_setup (GimpLut *lut,
gdouble value, gdouble value,
gint n_channels) gint n_channels)
{ {
g_return_if_fail (lut != NULL); g_return_if_fail (lut != NULL);
gimp_lut_setup_exact (lut, (GimpLutFunc) threshold_lut_func, gimp_lut_setup_exact (lut, (GimpLutFunc) threshold_lut_func,
(gpointer) &value , n_channels); (gpointer) &value , n_channels);
} }
/* --------------- posterize ---------------- */ /* --------------- posterize ---------------- */
static gfloat static gfloat
posterize_lut_func (gint *ilevels, posterize_lut_func (gint *ilevels,
gint n_channels, gint n_channels,
gint channel, gint channel,
gfloat value) gfloat value)
{ {
gint levels; gint levels;
@ -310,7 +275,7 @@ posterize_lut_func (gint *ilevels,
GimpLut * GimpLut *
posterize_lut_new (gint levels, posterize_lut_new (gint levels,
gint n_channels) gint n_channels)
{ {
GimpLut *lut; GimpLut *lut;
@ -323,13 +288,13 @@ posterize_lut_new (gint levels,
void void
posterize_lut_setup (GimpLut *lut, posterize_lut_setup (GimpLut *lut,
gint levels, gint levels,
gint n_channels) gint n_channels)
{ {
g_return_if_fail (lut != NULL); g_return_if_fail (lut != NULL);
gimp_lut_setup_exact (lut, (GimpLutFunc) posterize_lut_func, gimp_lut_setup_exact (lut, (GimpLutFunc) posterize_lut_func,
(gpointer) &levels, n_channels); (gpointer) &levels, n_channels);
} }
/* --------------- equalize ------------- */ /* --------------- equalize ------------- */
@ -342,9 +307,9 @@ typedef struct
static gfloat static gfloat
equalize_lut_func (hist_lut_struct *hlut, equalize_lut_func (hist_lut_struct *hlut,
gint n_channels, gint n_channels,
gint channel, gint channel,
gfloat value) gfloat value)
{ {
gint i = 0; gint i = 0;
gint j; gint j;
@ -359,7 +324,7 @@ equalize_lut_func (hist_lut_struct *hlut,
GimpLut * GimpLut *
eq_histogram_lut_new (GimpHistogram *histogram, eq_histogram_lut_new (GimpHistogram *histogram,
gint n_channels) gint n_channels)
{ {
GimpLut *lut; GimpLut *lut;
@ -374,8 +339,8 @@ eq_histogram_lut_new (GimpHistogram *histogram,
void void
eq_histogram_lut_setup (GimpLut *lut, eq_histogram_lut_setup (GimpLut *lut,
GimpHistogram *hist, GimpHistogram *hist,
gint n_channels) gint n_channels)
{ {
gint i, k, j; gint i, k, j;
hist_lut_struct hlut; hist_lut_struct hlut;
@ -400,28 +365,28 @@ eq_histogram_lut_setup (GimpLut *lut,
/* Find intermediate points */ /* Find intermediate points */
j = 0; j = 0;
sum = (gimp_histogram_get_channel (hist, k, 0) + sum = (gimp_histogram_get_channel (hist, k, 0) +
gimp_histogram_get_channel (hist, k, 1)); gimp_histogram_get_channel (hist, k, 1));
for (i = 1; i < 256; i++) for (i = 1; i < 256; i++)
{ {
desired = i * pixels_per_value; desired = i * pixels_per_value;
while (sum <= desired) while (sum <= desired)
{ {
j++; j++;
sum += gimp_histogram_get_channel (hist, k, j + 1); sum += gimp_histogram_get_channel (hist, k, j + 1);
} }
/* Nearest sum */ /* Nearest sum */
dif = sum - gimp_histogram_get_channel (hist, k, j); dif = sum - gimp_histogram_get_channel (hist, k, j);
if ((sum - desired) > (dif / 2.0)) if ((sum - desired) > (dif / 2.0))
hlut.part[k][i] = j; hlut.part[k][i] = j;
else else
hlut.part[k][i] = j + 1; hlut.part[k][i] = j + 1;
} }
} }
gimp_lut_setup (lut, (GimpLutFunc) equalize_lut_func, gimp_lut_setup (lut, (GimpLutFunc) equalize_lut_func,
(gpointer) &hlut, n_channels); (gpointer) &hlut, n_channels);
} }