Blame operations/common/contrast-curve.c

Packit bc1512
/* This file is an image processing operation for GEGL
Packit bc1512
 *
Packit bc1512
 * GEGL is free software; you can redistribute it and/or
Packit bc1512
 * modify it under the terms of the GNU Lesser General Public
Packit bc1512
 * License as published by the Free Software Foundation; either
Packit bc1512
 * version 3 of the License, or (at your option) any later version.
Packit bc1512
 *
Packit bc1512
 * GEGL is distributed in the hope that it will be useful,
Packit bc1512
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit bc1512
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit bc1512
 * Lesser General Public License for more details.
Packit bc1512
 *
Packit bc1512
 * You should have received a copy of the GNU Lesser General Public
Packit bc1512
 * License along with GEGL; if not, see <http://www.gnu.org/licenses/>.
Packit bc1512
 *
Packit bc1512
 * Copyright 2007 Mark Probst <mark.probst@gmail.com>
Packit bc1512
 */
Packit bc1512
Packit bc1512
#include "config.h"
Packit bc1512
#include <glib/gi18n-lib.h>
Packit bc1512
Packit bc1512
Packit bc1512
#ifdef GEGL_CHANT_PROPERTIES
Packit bc1512
Packit bc1512
gegl_chant_int (sampling_points, _("Sample points"), 0, 65536, 0,
Packit bc1512
                _("Number of curve sampling points.  0 for exact calculation."))
Packit bc1512
gegl_chant_curve (curve, _("Curve"), _("The contrast curve."))
Packit bc1512
Packit bc1512
#else
Packit bc1512
Packit bc1512
#define GEGL_CHANT_TYPE_POINT_FILTER
Packit bc1512
#define GEGL_CHANT_C_FILE  "contrast-curve.c"
Packit bc1512
Packit bc1512
#include "gegl-chant.h"
Packit bc1512
Packit bc1512
static void prepare (GeglOperation *operation)
Packit bc1512
{
Packit bc1512
  const Babl *format = babl_format ("YA float");
Packit bc1512
Packit bc1512
  gegl_operation_set_format (operation, "input", format);
Packit bc1512
  gegl_operation_set_format (operation, "output", format);
Packit bc1512
}
Packit bc1512
Packit bc1512
static gboolean
Packit bc1512
process (GeglOperation       *op,
Packit bc1512
         void                *in_buf,
Packit bc1512
         void                *out_buf,
Packit bc1512
         glong                samples,
Packit bc1512
         const GeglRectangle *roi,
Packit bc1512
         gint                 level)
Packit bc1512
{
Packit bc1512
  GeglChantO *o = GEGL_CHANT_PROPERTIES (op);
Packit bc1512
  gint        num_sampling_points;
Packit bc1512
  GeglCurve  *curve;
Packit bc1512
  gint i;
Packit bc1512
  gfloat  *in  = in_buf;
Packit bc1512
  gfloat  *out = out_buf;
Packit bc1512
  gdouble *xs, *ys;
Packit bc1512
Packit bc1512
  num_sampling_points = o->sampling_points;
Packit bc1512
  curve = o->curve;
Packit bc1512
Packit bc1512
  if (num_sampling_points > 0)
Packit bc1512
  {
Packit bc1512
    xs = g_new(gdouble, num_sampling_points);
Packit bc1512
    ys = g_new(gdouble, num_sampling_points);
Packit bc1512
Packit bc1512
    gegl_curve_calc_values(o->curve, 0.0, 1.0, num_sampling_points, xs, ys);
Packit bc1512
Packit bc1512
    g_free(xs);
Packit bc1512
Packit bc1512
    for (i=0; i
Packit bc1512
    {
Packit bc1512
      gint x = in[0] * num_sampling_points;
Packit bc1512
      gfloat y;
Packit bc1512
Packit bc1512
      if (x < 0)
Packit bc1512
       y = ys[0];
Packit bc1512
      else if (x >= num_sampling_points)
Packit bc1512
       y = ys[num_sampling_points - 1];
Packit bc1512
      else
Packit bc1512
       y = ys[x];
Packit bc1512
Packit bc1512
      out[0] = y;
Packit bc1512
      out[1]=in[1];
Packit bc1512
Packit bc1512
      in += 2;
Packit bc1512
      out+= 2;
Packit bc1512
    }
Packit bc1512
Packit bc1512
    g_free(ys);
Packit bc1512
  }
Packit bc1512
  else
Packit bc1512
    for (i=0; i
Packit bc1512
    {
Packit bc1512
      gfloat u = in[0];
Packit bc1512
Packit bc1512
      out[0] = gegl_curve_calc_value(curve, u);
Packit bc1512
      out[1]=in[1];
Packit bc1512
Packit bc1512
      in += 2;
Packit bc1512
      out+= 2;
Packit bc1512
    }
Packit bc1512
Packit bc1512
  return TRUE;
Packit bc1512
}
Packit bc1512
Packit bc1512
Packit bc1512
static void
Packit bc1512
gegl_chant_class_init (GeglChantClass *klass)
Packit bc1512
{
Packit bc1512
  GeglOperationClass            *operation_class;
Packit bc1512
  GeglOperationPointFilterClass *point_filter_class;
Packit bc1512
Packit bc1512
  operation_class    = GEGL_OPERATION_CLASS (klass);
Packit bc1512
  point_filter_class = GEGL_OPERATION_POINT_FILTER_CLASS (klass);
Packit bc1512
Packit bc1512
  point_filter_class->process = process;
Packit bc1512
  operation_class->prepare = prepare;
Packit bc1512
Packit bc1512
  gegl_operation_class_set_keys (operation_class,
Packit bc1512
    "name"       , "gegl:contrast-curve",
Packit bc1512
    "categories" , "color",
Packit bc1512
    "description",
Packit bc1512
        _("Adjusts the contrast of the image according to a curve."),
Packit bc1512
        NULL);
Packit bc1512
}
Packit bc1512
Packit bc1512
#endif