2008-01-08 04:37:25 +08:00
|
|
|
/* GIMP - The GNU Image Manipulation Program
|
|
|
|
* Copyright (C) 1995 Spencer Kimball and Peter Mattis
|
|
|
|
*
|
|
|
|
* gimpoperationlevels.c
|
|
|
|
* Copyright (C) 2007 Michael Natterer <mitch@gimp.org>
|
|
|
|
*
|
|
|
|
* 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 "libgimpcolor/gimpcolor.h"
|
|
|
|
#include "libgimpmath/gimpmath.h"
|
|
|
|
|
|
|
|
#include "gegl/gegl-types.h"
|
|
|
|
#include <gegl/buffer/gegl-buffer.h>
|
|
|
|
|
|
|
|
#include "gegl-types.h"
|
|
|
|
|
|
|
|
#include "gimpoperationlevels.h"
|
|
|
|
|
|
|
|
|
|
|
|
enum
|
|
|
|
{
|
|
|
|
PROP_0,
|
2008-01-08 05:39:05 +08:00
|
|
|
PROP_CHANNEL,
|
|
|
|
PROP_GAMMA,
|
|
|
|
PROP_LOW_INPUT,
|
|
|
|
PROP_HIGH_INPUT,
|
|
|
|
PROP_LOW_OUTPUT,
|
|
|
|
PROP_HIGH_OUTPUT
|
2008-01-08 04:37:25 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
static void gimp_operation_levels_get_property (GObject *object,
|
|
|
|
guint property_id,
|
|
|
|
GValue *value,
|
|
|
|
GParamSpec *pspec);
|
|
|
|
static void gimp_operation_levels_set_property (GObject *object,
|
|
|
|
guint property_id,
|
|
|
|
const GValue *value,
|
|
|
|
GParamSpec *pspec);
|
|
|
|
|
|
|
|
static gboolean gimp_operation_levels_process (GeglOperation *operation,
|
|
|
|
void *in_buf,
|
|
|
|
void *out_buf,
|
|
|
|
glong samples);
|
|
|
|
|
|
|
|
|
|
|
|
G_DEFINE_TYPE (GimpOperationLevels, gimp_operation_levels,
|
|
|
|
GEGL_TYPE_OPERATION_POINT_FILTER)
|
|
|
|
|
|
|
|
#define parent_class gimp_operation_levels_parent_class
|
|
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
gimp_operation_levels_class_init (GimpOperationLevelsClass * klass)
|
|
|
|
{
|
|
|
|
GObjectClass *object_class = G_OBJECT_CLASS (klass);
|
|
|
|
GeglOperationClass *operation_class = GEGL_OPERATION_CLASS (klass);
|
|
|
|
GeglOperationPointFilterClass *point_class = GEGL_OPERATION_POINT_FILTER_CLASS (klass);
|
|
|
|
|
|
|
|
object_class->set_property = gimp_operation_levels_set_property;
|
|
|
|
object_class->get_property = gimp_operation_levels_get_property;
|
|
|
|
|
|
|
|
point_class->process = gimp_operation_levels_process;
|
|
|
|
|
|
|
|
gegl_operation_class_set_name (operation_class, "gimp-levels");
|
2008-01-08 05:39:05 +08:00
|
|
|
|
|
|
|
g_object_class_install_property (object_class, PROP_CHANNEL,
|
|
|
|
g_param_spec_enum ("channel",
|
|
|
|
"Channel",
|
|
|
|
"The affected channel",
|
|
|
|
GIMP_TYPE_HISTOGRAM_CHANNEL,
|
|
|
|
GIMP_HISTOGRAM_VALUE,
|
|
|
|
G_PARAM_READWRITE |
|
|
|
|
G_PARAM_CONSTRUCT));
|
|
|
|
|
|
|
|
g_object_class_install_property (object_class, PROP_GAMMA,
|
2008-01-16 01:23:45 +08:00
|
|
|
g_param_spec_double ("gamma",
|
|
|
|
"Gamma",
|
|
|
|
"Gamma",
|
|
|
|
0.1, 10.0, 1.0,
|
|
|
|
G_PARAM_READWRITE |
|
|
|
|
G_PARAM_CONSTRUCT));
|
2008-01-08 05:39:05 +08:00
|
|
|
|
|
|
|
g_object_class_install_property (object_class, PROP_LOW_INPUT,
|
2008-01-16 01:23:45 +08:00
|
|
|
g_param_spec_double ("low-input",
|
|
|
|
"Low Input",
|
|
|
|
"Low Input",
|
|
|
|
0.0, 1.0, 0.0,
|
|
|
|
G_PARAM_READWRITE |
|
|
|
|
G_PARAM_CONSTRUCT));
|
2008-01-08 05:39:05 +08:00
|
|
|
|
|
|
|
g_object_class_install_property (object_class, PROP_HIGH_INPUT,
|
2008-01-16 01:23:45 +08:00
|
|
|
g_param_spec_double ("high-input",
|
|
|
|
"High Input",
|
|
|
|
"High Input",
|
|
|
|
0.0, 1.0, 1.0,
|
|
|
|
G_PARAM_READWRITE |
|
|
|
|
G_PARAM_CONSTRUCT));
|
2008-01-08 05:39:05 +08:00
|
|
|
|
|
|
|
g_object_class_install_property (object_class, PROP_LOW_OUTPUT,
|
2008-01-16 01:23:45 +08:00
|
|
|
g_param_spec_double ("low-output",
|
|
|
|
"Low Output",
|
|
|
|
"Low Output",
|
|
|
|
0.0, 1.0, 0.0,
|
|
|
|
G_PARAM_READWRITE |
|
|
|
|
G_PARAM_CONSTRUCT));
|
2008-01-08 05:39:05 +08:00
|
|
|
|
|
|
|
g_object_class_install_property (object_class, PROP_HIGH_OUTPUT,
|
2008-01-16 01:23:45 +08:00
|
|
|
g_param_spec_double ("high-output",
|
|
|
|
"High Output",
|
|
|
|
"High Output",
|
|
|
|
0.0, 1.0, 1.0,
|
|
|
|
G_PARAM_READWRITE |
|
|
|
|
G_PARAM_CONSTRUCT));
|
2008-01-08 04:37:25 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gimp_operation_levels_init (GimpOperationLevels *self)
|
|
|
|
{
|
2008-01-08 05:39:05 +08:00
|
|
|
GimpHistogramChannel channel;
|
|
|
|
|
|
|
|
self->channel = GIMP_HISTOGRAM_VALUE;
|
|
|
|
|
|
|
|
for (channel = GIMP_HISTOGRAM_VALUE;
|
|
|
|
channel <= GIMP_HISTOGRAM_ALPHA;
|
|
|
|
channel++)
|
|
|
|
{
|
|
|
|
self->gamma[channel] = 1.0;
|
|
|
|
self->low_input[channel] = 0.0;
|
|
|
|
self->high_input[channel] = 1.0;
|
|
|
|
self->low_output[channel] = 0.0;
|
|
|
|
self->high_output[channel] = 1.0;
|
|
|
|
}
|
2008-01-08 04:37:25 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gimp_operation_levels_get_property (GObject *object,
|
|
|
|
guint property_id,
|
|
|
|
GValue *value,
|
|
|
|
GParamSpec *pspec)
|
|
|
|
{
|
2008-01-08 05:39:05 +08:00
|
|
|
GimpOperationLevels *self = GIMP_OPERATION_LEVELS (object);
|
2008-01-08 04:37:25 +08:00
|
|
|
|
|
|
|
switch (property_id)
|
|
|
|
{
|
2008-01-08 05:39:05 +08:00
|
|
|
case PROP_CHANNEL:
|
|
|
|
g_value_set_enum (value, self->channel);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case PROP_GAMMA:
|
2008-01-16 01:23:45 +08:00
|
|
|
g_value_set_double (value, self->gamma[self->channel]);
|
2008-01-08 05:39:05 +08:00
|
|
|
break;
|
|
|
|
|
|
|
|
case PROP_LOW_INPUT:
|
2008-01-16 01:23:45 +08:00
|
|
|
g_value_set_double (value, self->low_input[self->channel]);
|
2008-01-08 05:39:05 +08:00
|
|
|
break;
|
|
|
|
|
|
|
|
case PROP_HIGH_INPUT:
|
2008-01-16 01:23:45 +08:00
|
|
|
g_value_set_double (value, self->high_input[self->channel]);
|
2008-01-08 05:39:05 +08:00
|
|
|
break;
|
|
|
|
|
|
|
|
case PROP_LOW_OUTPUT:
|
2008-01-16 01:23:45 +08:00
|
|
|
g_value_set_double (value, self->low_output[self->channel]);
|
2008-01-08 05:39:05 +08:00
|
|
|
break;
|
|
|
|
|
|
|
|
case PROP_HIGH_OUTPUT:
|
2008-01-16 01:23:45 +08:00
|
|
|
g_value_set_double (value, self->high_output[self->channel]);
|
2008-01-08 05:39:05 +08:00
|
|
|
break;
|
|
|
|
|
2008-01-08 04:37:25 +08:00
|
|
|
default:
|
|
|
|
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gimp_operation_levels_set_property (GObject *object,
|
|
|
|
guint property_id,
|
|
|
|
const GValue *value,
|
|
|
|
GParamSpec *pspec)
|
|
|
|
{
|
2008-01-08 05:39:05 +08:00
|
|
|
GimpOperationLevels *self = GIMP_OPERATION_LEVELS (object);
|
2008-01-08 04:37:25 +08:00
|
|
|
|
|
|
|
switch (property_id)
|
|
|
|
{
|
2008-01-08 05:39:05 +08:00
|
|
|
case PROP_CHANNEL:
|
|
|
|
self->channel = g_value_get_enum (value);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case PROP_GAMMA:
|
2008-01-16 01:23:45 +08:00
|
|
|
self->gamma[self->channel] = g_value_get_double (value);
|
2008-01-08 05:39:05 +08:00
|
|
|
break;
|
|
|
|
|
|
|
|
case PROP_LOW_INPUT:
|
2008-01-16 01:23:45 +08:00
|
|
|
self->low_input[self->channel] = g_value_get_double (value);
|
2008-01-08 05:39:05 +08:00
|
|
|
break;
|
|
|
|
|
|
|
|
case PROP_HIGH_INPUT:
|
2008-01-16 01:23:45 +08:00
|
|
|
self->high_input[self->channel] = g_value_get_double (value);
|
2008-01-08 05:39:05 +08:00
|
|
|
break;
|
|
|
|
|
|
|
|
case PROP_LOW_OUTPUT:
|
2008-01-16 01:23:45 +08:00
|
|
|
self->low_output[self->channel] = g_value_get_double (value);
|
2008-01-08 05:39:05 +08:00
|
|
|
break;
|
|
|
|
|
|
|
|
case PROP_HIGH_OUTPUT:
|
2008-01-16 01:23:45 +08:00
|
|
|
self->high_output[self->channel] = g_value_get_double (value);
|
2008-01-08 05:39:05 +08:00
|
|
|
break;
|
|
|
|
|
2008-01-08 04:37:25 +08:00
|
|
|
default:
|
|
|
|
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-01-16 01:23:45 +08:00
|
|
|
static inline gdouble
|
|
|
|
gimp_operation_levels_map (gdouble value,
|
|
|
|
gdouble gamma,
|
|
|
|
gdouble low_input,
|
|
|
|
gdouble high_input,
|
|
|
|
gdouble low_output,
|
|
|
|
gdouble high_output)
|
2008-01-08 04:37:25 +08:00
|
|
|
{
|
|
|
|
/* determine input intensity */
|
|
|
|
if (high_input != low_input)
|
2008-01-08 05:39:05 +08:00
|
|
|
value = (value - low_input) / (high_input - low_input);
|
2008-01-08 04:37:25 +08:00
|
|
|
else
|
2008-01-08 05:39:05 +08:00
|
|
|
value = (value - low_input);
|
2008-01-08 04:37:25 +08:00
|
|
|
|
|
|
|
if (gamma != 0.0)
|
|
|
|
{
|
|
|
|
if (value >= 0.0)
|
|
|
|
value = pow ( value, 1.0 / gamma);
|
|
|
|
else
|
|
|
|
value = -pow (-value, 1.0 / gamma);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* determine the output intensity */
|
|
|
|
if (high_output >= low_output)
|
|
|
|
value = value * (high_output - low_output) + low_output;
|
|
|
|
else if (high_output < low_output)
|
|
|
|
value = low_output - value * (low_output - high_output);
|
|
|
|
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean
|
|
|
|
gimp_operation_levels_process (GeglOperation *operation,
|
|
|
|
void *in_buf,
|
|
|
|
void *out_buf,
|
|
|
|
glong samples)
|
|
|
|
{
|
|
|
|
GimpOperationLevels *self = GIMP_OPERATION_LEVELS (operation);
|
|
|
|
gfloat *src = in_buf;
|
|
|
|
gfloat *dest = out_buf;
|
|
|
|
glong sample;
|
|
|
|
|
|
|
|
for (sample = 0; sample < samples; sample++)
|
|
|
|
{
|
|
|
|
gint channel;
|
|
|
|
|
|
|
|
for (channel = 0; channel < 4; channel++)
|
|
|
|
{
|
2008-01-16 01:23:45 +08:00
|
|
|
gdouble value;
|
2008-01-08 04:37:25 +08:00
|
|
|
|
|
|
|
value = gimp_operation_levels_map (src[channel],
|
2008-01-08 05:39:05 +08:00
|
|
|
self->gamma[channel + 1],
|
2008-01-08 04:37:25 +08:00
|
|
|
self->low_input[channel + 1],
|
|
|
|
self->high_input[channel + 1],
|
|
|
|
self->low_output[channel + 1],
|
|
|
|
self->high_output[channel + 1]);
|
|
|
|
|
|
|
|
/* don't apply the overall curve to the alpha channel */
|
2008-01-16 01:23:45 +08:00
|
|
|
if (channel != ALPHA_PIX)
|
2008-01-08 04:37:25 +08:00
|
|
|
value = gimp_operation_levels_map (value,
|
2008-01-08 05:39:05 +08:00
|
|
|
self->gamma[0],
|
2008-01-08 04:37:25 +08:00
|
|
|
self->low_input[0],
|
|
|
|
self->high_input[0],
|
|
|
|
self->low_output[0],
|
|
|
|
self->high_output[0]);
|
|
|
|
|
|
|
|
dest[channel] = value;
|
|
|
|
}
|
|
|
|
|
|
|
|
src += 4;
|
|
|
|
dest += 4;
|
|
|
|
}
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|