2008-05-11 22:56:57 +08:00
|
|
|
/* GIMP - The GNU 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 "core-types.h"
|
|
|
|
|
|
|
|
#include "gimpcurve.h"
|
|
|
|
#include "gimpcurve-map.h"
|
|
|
|
|
|
|
|
|
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
|
|
|
|
};
|
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
2008-05-13 15:10:48 +08:00
|
|
|
if (curve->identity)
|
|
|
|
{
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
|
2008-05-11 22:56:57 +08:00
|
|
|
if (value < 0.0)
|
|
|
|
{
|
|
|
|
return curve->samples[0];
|
|
|
|
}
|
|
|
|
else if (value >= 1.0)
|
|
|
|
{
|
|
|
|
return curve->samples[curve->n_samples - 1];
|
|
|
|
}
|
2008-05-13 15:10:48 +08:00
|
|
|
else /* interpolate the curve */
|
2008-05-11 22:56:57 +08:00
|
|
|
{
|
2008-05-13 15:20:23 +08:00
|
|
|
gdouble f;
|
|
|
|
gint index;
|
2008-05-11 22:56:57 +08:00
|
|
|
|
2008-05-13 15:20:23 +08:00
|
|
|
/* 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];
|
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
|
|
|
guint mask = CURVE_NONE;
|
2008-05-13 17:39:14 +08:00
|
|
|
|
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 19:32:38 +08:00
|
|
|
if (! gimp_curve_is_identity (curve_colors)) mask |= CURVE_COLORS;
|
|
|
|
if (! gimp_curve_is_identity (curve_red)) mask |= CURVE_RED;
|
|
|
|
if (! gimp_curve_is_identity (curve_green)) mask |= CURVE_GREEN;
|
|
|
|
if (! gimp_curve_is_identity (curve_blue)) mask |= CURVE_BLUE;
|
|
|
|
if (! gimp_curve_is_identity (curve_alpha)) mask |= CURVE_ALPHA;
|
2008-05-13 17:39:14 +08:00
|
|
|
|
|
|
|
switch (mask)
|
2008-05-11 22:56:57 +08:00
|
|
|
{
|
2008-05-13 19:32:38 +08:00
|
|
|
case CURVE_NONE:
|
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--)
|
|
|
|
{
|
2008-05-13 19:32:38 +08:00
|
|
|
dest[0] = gimp_curve_map_value (curve_colors, src[0]);
|
|
|
|
dest[1] = gimp_curve_map_value (curve_colors, src[1]);
|
|
|
|
dest[2] = gimp_curve_map_value (curve_colors, src[2]);
|
|
|
|
/* 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--)
|
|
|
|
{
|
|
|
|
dest[0] = gimp_curve_map_value (curve_red, src[0]);
|
|
|
|
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];
|
|
|
|
dest[1] = gimp_curve_map_value (curve_green, 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_BLUE:
|
2008-05-13 17:39:14 +08:00
|
|
|
while (samples--)
|
|
|
|
{
|
|
|
|
dest[0] = src[0];
|
|
|
|
dest[1] = src[1];
|
|
|
|
dest[2] = gimp_curve_map_value (curve_green, src[2]);
|
|
|
|
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];
|
|
|
|
dest[3] = gimp_curve_map_value (curve_alpha, src[3]);
|
|
|
|
|
|
|
|
src += 4;
|
|
|
|
dest += 4;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2008-05-13 19:32:38 +08:00
|
|
|
case (CURVE_RED | CURVE_GREEN | CURVE_BLUE):
|
|
|
|
while (samples--)
|
|
|
|
{
|
|
|
|
dest[0] = gimp_curve_map_value (curve_red, src[0]);
|
|
|
|
dest[1] = gimp_curve_map_value (curve_green, src[1]);
|
|
|
|
dest[2] = gimp_curve_map_value (curve_blue, src[2]);
|
|
|
|
dest[3] = src[3];
|
|
|
|
|
|
|
|
src += 4;
|
|
|
|
dest += 4;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2008-05-13 17:39:14 +08:00
|
|
|
default: /* apply all curves */
|
|
|
|
while (samples--)
|
|
|
|
{
|
2008-05-13 19:32:38 +08:00
|
|
|
dest[0] = gimp_curve_map_value (curve_colors,
|
2008-05-13 17:39:14 +08:00
|
|
|
gimp_curve_map_value (curve_red,
|
|
|
|
src[0]));
|
2008-05-13 19:32:38 +08:00
|
|
|
dest[1] = gimp_curve_map_value (curve_colors,
|
2008-05-13 17:39:14 +08:00
|
|
|
gimp_curve_map_value (curve_green,
|
|
|
|
src[1]));
|
2008-05-13 19:32:38 +08:00
|
|
|
dest[2] = gimp_curve_map_value (curve_colors,
|
2008-05-13 17:39:14 +08:00
|
|
|
gimp_curve_map_value (curve_blue,
|
|
|
|
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] = gimp_curve_map_value (curve_alpha, src[3]);
|
|
|
|
|
|
|
|
src += 4;
|
|
|
|
dest += 4;
|
|
|
|
}
|
2008-05-11 22:56:57 +08:00
|
|
|
}
|
|
|
|
}
|