2008-05-11 22:56:57 +08:00
|
|
|
/* GIMP - The GNU Image Manipulation Program
|
|
|
|
* Copyright (C) 1995 Spencer Kimball and Peter Mattis
|
|
|
|
*
|
2009-01-18 06:28:01 +08:00
|
|
|
* This program is free software: you can redistribute it and/or modify
|
2008-05-11 22:56:57 +08:00
|
|
|
* it under the terms of the GNU General Public License as published by
|
2009-01-18 06:28:01 +08:00
|
|
|
* the Free Software Foundation; either version 3 of the License, or
|
2008-05-11 22:56:57 +08:00
|
|
|
* (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
|
2009-01-18 06:28:01 +08:00
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
2008-05-11 22:56:57 +08:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include "config.h"
|
|
|
|
|
2008-05-14 03:42:20 +08:00
|
|
|
#include <string.h>
|
|
|
|
|
2013-10-15 07:58:39 +08:00
|
|
|
#include <gdk-pixbuf/gdk-pixbuf.h>
|
2012-04-22 03:41:36 +08:00
|
|
|
#include <gegl.h>
|
2008-05-11 22:56:57 +08:00
|
|
|
|
|
|
|
#include "libgimpmath/gimpmath.h"
|
|
|
|
|
|
|
|
#include "core-types.h"
|
|
|
|
|
|
|
|
#include "gimpcurve.h"
|
|
|
|
#include "gimpcurve-map.h"
|
|
|
|
|
|
|
|
|
2012-09-05 17:29:45 +08:00
|
|
|
#if defined (HAVE_FINITE)
|
|
|
|
#define FINITE(x) finite(x)
|
|
|
|
#elif defined (HAVE_ISFINITE)
|
|
|
|
#define FINITE(x) isfinite(x)
|
|
|
|
#elif defined (G_OS_WIN32)
|
|
|
|
#define FINITE(x) _finite(x)
|
|
|
|
#else
|
|
|
|
#error "no FINITE() implementation available?!"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
2008-05-13 19:32:38 +08:00
|
|
|
enum
|
|
|
|
{
|
|
|
|
CURVE_NONE = 0,
|
|
|
|
CURVE_COLORS = 1 << 0,
|
|
|
|
CURVE_RED = 1 << 1,
|
|
|
|
CURVE_GREEN = 1 << 2,
|
|
|
|
CURVE_BLUE = 1 << 3,
|
|
|
|
CURVE_ALPHA = 1 << 4
|
|
|
|
};
|
|
|
|
|
2009-12-29 03:50:26 +08:00
|
|
|
static guint gimp_curve_get_apply_mask (GimpCurve *curve_colors,
|
|
|
|
GimpCurve *curve_red,
|
|
|
|
GimpCurve *curve_green,
|
|
|
|
GimpCurve *curve_blue,
|
|
|
|
GimpCurve *curve_alpha);
|
|
|
|
static inline gdouble gimp_curve_map_value_inline (GimpCurve *curve,
|
|
|
|
gdouble value);
|
2008-05-13 21:46:46 +08:00
|
|
|
|
2008-05-13 19:32:38 +08:00
|
|
|
|
2008-05-11 22:56:57 +08:00
|
|
|
gdouble
|
|
|
|
gimp_curve_map_value (GimpCurve *curve,
|
|
|
|
gdouble value)
|
|
|
|
{
|
|
|
|
g_return_val_if_fail (GIMP_IS_CURVE (curve), 0.0);
|
|
|
|
|
2009-12-29 03:50:26 +08:00
|
|
|
return gimp_curve_map_value_inline (curve, value);
|
2008-05-11 22:56:57 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2008-05-13 19:32:38 +08:00
|
|
|
gimp_curve_map_pixels (GimpCurve *curve_colors,
|
2008-05-11 22:56:57 +08:00
|
|
|
GimpCurve *curve_red,
|
|
|
|
GimpCurve *curve_green,
|
|
|
|
GimpCurve *curve_blue,
|
|
|
|
GimpCurve *curve_alpha,
|
|
|
|
gfloat *src,
|
|
|
|
gfloat *dest,
|
|
|
|
glong samples)
|
|
|
|
{
|
2008-05-13 19:32:38 +08:00
|
|
|
g_return_if_fail (GIMP_IS_CURVE (curve_colors));
|
2008-05-11 22:56:57 +08:00
|
|
|
g_return_if_fail (GIMP_IS_CURVE (curve_red));
|
|
|
|
g_return_if_fail (GIMP_IS_CURVE (curve_green));
|
|
|
|
g_return_if_fail (GIMP_IS_CURVE (curve_blue));
|
|
|
|
g_return_if_fail (GIMP_IS_CURVE (curve_alpha));
|
|
|
|
|
2008-05-13 21:46:46 +08:00
|
|
|
switch (gimp_curve_get_apply_mask (curve_colors,
|
|
|
|
curve_red,
|
|
|
|
curve_green,
|
|
|
|
curve_blue,
|
|
|
|
curve_alpha))
|
2008-05-11 22:56:57 +08:00
|
|
|
{
|
2008-05-13 19:32:38 +08:00
|
|
|
case CURVE_NONE:
|
2008-05-19 17:02:30 +08:00
|
|
|
memcpy (dest, src, samples * 4 * sizeof (gfloat));
|
2008-05-13 17:39:14 +08:00
|
|
|
break;
|
|
|
|
|
2008-05-13 19:32:38 +08:00
|
|
|
case CURVE_COLORS:
|
2008-05-13 17:39:14 +08:00
|
|
|
while (samples--)
|
|
|
|
{
|
2009-12-29 03:50:26 +08:00
|
|
|
dest[0] = gimp_curve_map_value_inline (curve_colors, src[0]);
|
|
|
|
dest[1] = gimp_curve_map_value_inline (curve_colors, src[1]);
|
|
|
|
dest[2] = gimp_curve_map_value_inline (curve_colors, src[2]);
|
2008-05-13 19:32:38 +08:00
|
|
|
/* don't apply the colors curve to the alpha channel */
|
2008-05-13 17:39:14 +08:00
|
|
|
dest[3] = src[3];
|
|
|
|
|
|
|
|
src += 4;
|
|
|
|
dest += 4;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2008-05-13 19:32:38 +08:00
|
|
|
case CURVE_RED:
|
2008-05-13 17:39:14 +08:00
|
|
|
while (samples--)
|
|
|
|
{
|
2009-12-29 03:50:26 +08:00
|
|
|
dest[0] = gimp_curve_map_value_inline (curve_red, src[0]);
|
2008-05-13 17:39:14 +08:00
|
|
|
dest[1] = src[1];
|
|
|
|
dest[2] = src[2];
|
|
|
|
dest[3] = src[3];
|
|
|
|
|
|
|
|
src += 4;
|
|
|
|
dest += 4;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2008-05-13 19:32:38 +08:00
|
|
|
case CURVE_GREEN:
|
2008-05-13 17:39:14 +08:00
|
|
|
while (samples--)
|
|
|
|
{
|
|
|
|
dest[0] = src[0];
|
2009-12-29 03:50:26 +08:00
|
|
|
dest[1] = gimp_curve_map_value_inline (curve_green, src[1]);
|
2008-05-13 17:39:14 +08:00
|
|
|
dest[2] = src[2];
|
|
|
|
dest[3] = src[3];
|
|
|
|
|
|
|
|
src += 4;
|
|
|
|
dest += 4;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2008-05-13 19:32:38 +08:00
|
|
|
case CURVE_BLUE:
|
2008-05-13 17:39:14 +08:00
|
|
|
while (samples--)
|
|
|
|
{
|
|
|
|
dest[0] = src[0];
|
|
|
|
dest[1] = src[1];
|
2009-12-29 03:50:26 +08:00
|
|
|
dest[2] = gimp_curve_map_value_inline (curve_blue, src[2]);
|
2008-05-13 17:39:14 +08:00
|
|
|
dest[3] = src[3];
|
|
|
|
|
|
|
|
src += 4;
|
|
|
|
dest += 4;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2008-05-13 19:32:38 +08:00
|
|
|
case CURVE_ALPHA:
|
2008-05-13 17:39:14 +08:00
|
|
|
while (samples--)
|
|
|
|
{
|
|
|
|
dest[0] = src[0];
|
|
|
|
dest[1] = src[1];
|
|
|
|
dest[2] = src[2];
|
2009-12-29 03:50:26 +08:00
|
|
|
dest[3] = gimp_curve_map_value_inline (curve_alpha, src[3]);
|
2008-05-13 17:39:14 +08:00
|
|
|
|
|
|
|
src += 4;
|
|
|
|
dest += 4;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2008-05-13 19:32:38 +08:00
|
|
|
case (CURVE_RED | CURVE_GREEN | CURVE_BLUE):
|
|
|
|
while (samples--)
|
|
|
|
{
|
2009-12-29 03:50:26 +08:00
|
|
|
dest[0] = gimp_curve_map_value_inline (curve_red, src[0]);
|
|
|
|
dest[1] = gimp_curve_map_value_inline (curve_green, src[1]);
|
|
|
|
dest[2] = gimp_curve_map_value_inline (curve_blue, src[2]);
|
2008-05-13 19:32:38 +08:00
|
|
|
dest[3] = src[3];
|
|
|
|
|
|
|
|
src += 4;
|
|
|
|
dest += 4;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2008-05-13 21:46:46 +08:00
|
|
|
default:
|
2008-05-13 17:39:14 +08:00
|
|
|
while (samples--)
|
|
|
|
{
|
2009-12-29 03:50:26 +08:00
|
|
|
dest[0] = gimp_curve_map_value_inline (curve_colors,
|
|
|
|
gimp_curve_map_value_inline (curve_red,
|
|
|
|
src[0]));
|
|
|
|
dest[1] = gimp_curve_map_value_inline (curve_colors,
|
|
|
|
gimp_curve_map_value_inline (curve_green,
|
|
|
|
src[1]));
|
|
|
|
dest[2] = gimp_curve_map_value_inline (curve_colors,
|
|
|
|
gimp_curve_map_value_inline (curve_blue,
|
|
|
|
src[2]));
|
2008-05-13 19:32:38 +08:00
|
|
|
/* don't apply the colors curve to the alpha channel */
|
2009-12-29 03:50:26 +08:00
|
|
|
dest[3] = gimp_curve_map_value_inline (curve_alpha, src[3]);
|
2008-05-13 17:39:14 +08:00
|
|
|
|
|
|
|
src += 4;
|
|
|
|
dest += 4;
|
|
|
|
}
|
2008-05-13 21:46:46 +08:00
|
|
|
break;
|
2008-05-11 22:56:57 +08:00
|
|
|
}
|
|
|
|
}
|
2008-05-13 21:46:46 +08:00
|
|
|
|
|
|
|
static guint
|
|
|
|
gimp_curve_get_apply_mask (GimpCurve *curve_colors,
|
|
|
|
GimpCurve *curve_red,
|
|
|
|
GimpCurve *curve_green,
|
|
|
|
GimpCurve *curve_blue,
|
|
|
|
GimpCurve *curve_alpha)
|
|
|
|
{
|
|
|
|
return ((gimp_curve_is_identity (curve_colors) ? 0 : CURVE_COLORS) |
|
|
|
|
(gimp_curve_is_identity (curve_red) ? 0 : CURVE_RED) |
|
|
|
|
(gimp_curve_is_identity (curve_green) ? 0 : CURVE_GREEN) |
|
|
|
|
(gimp_curve_is_identity (curve_blue) ? 0 : CURVE_BLUE) |
|
|
|
|
(gimp_curve_is_identity (curve_alpha) ? 0 : CURVE_ALPHA));
|
|
|
|
}
|
|
|
|
|
2009-12-29 03:50:26 +08:00
|
|
|
static inline gdouble
|
|
|
|
gimp_curve_map_value_inline (GimpCurve *curve,
|
|
|
|
gdouble value)
|
|
|
|
{
|
|
|
|
if (curve->identity)
|
|
|
|
{
|
2012-09-05 17:29:45 +08:00
|
|
|
if (FINITE (value))
|
|
|
|
return CLAMP (value, 0.0, 1.0);
|
2009-12-29 03:50:26 +08:00
|
|
|
|
2012-09-05 17:29:45 +08:00
|
|
|
return 0.0;
|
2009-12-29 03:50:26 +08:00
|
|
|
}
|
2012-09-05 17:29:45 +08:00
|
|
|
|
|
|
|
/* check for known values first, so broken values like NaN
|
|
|
|
* delivered by broken drivers don't run into the interpolation
|
|
|
|
* code
|
|
|
|
*/
|
|
|
|
if (value > 0.0 && value < 1.0) /* interpolate the curve */
|
2009-12-29 03:50:26 +08:00
|
|
|
{
|
|
|
|
gdouble f;
|
|
|
|
gint index;
|
|
|
|
|
|
|
|
/* map value to the sample space */
|
|
|
|
value = value * (curve->n_samples - 1);
|
|
|
|
|
|
|
|
/* determine the indices of the closest sample points */
|
|
|
|
index = (gint) value;
|
|
|
|
|
|
|
|
/* calculate the position between the sample points */
|
|
|
|
f = value - index;
|
|
|
|
|
|
|
|
return (1.0 - f) * curve->samples[index] + f * curve->samples[index + 1];
|
|
|
|
}
|
2012-09-05 17:29:45 +08:00
|
|
|
else if (value >= 1.0)
|
|
|
|
{
|
|
|
|
return curve->samples[curve->n_samples - 1];
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return curve->samples[0];
|
|
|
|
}
|
2009-12-29 03:50:26 +08:00
|
|
|
}
|