svg-saturate.c 3.89 KB
Newer Older
1
/* This file is an image processing operation for GEGL
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
 *
 * GEGL is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 3 of the License, or (at your option) any later version.
 *
 * GEGL 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with GEGL; if not, see <http://www.gnu.org/licenses/>.
 *
 * Copyright 2006 Øyvind Kolås <pippin@gimp.org>
 * Copyright 2006 Geert Jordaens <geert.jordaens@telenet.be>
18
 */
19

20 21 22
#include "config.h"
#include <glib/gi18n-lib.h>

23
#ifdef GEGL_PROPERTIES
24

25 26
property_string (values, _("Values"), "")
    description(_("list of <number>s"))
27

28 29
#else

30 31
#define GEGL_OP_POINT_FILTER
#define GEGL_OP_C_FILE          "svg-saturate.c"
32

33
#include "gegl-op.h"
34 35 36
#include <math.h>
#include <stdlib.h>

37

38
static void prepare (GeglOperation *operation)
39
{
40
  const Babl *format = babl_format ("RaGaBaA float");
41 42 43

  gegl_operation_set_format (operation, "input", format);
  gegl_operation_set_format (operation, "output", format);
44 45 46
}

static gboolean
47 48 49 50
process (GeglOperation       *op,
         void                *in_buf,
         void                *out_buf,
         glong                n_pixels,
51 52
         const GeglRectangle *roi,
         gint                 level)
53
{
54
  GeglProperties *o = GEGL_PROPERTIES (op);
55 56 57
  gfloat     *in = in_buf;
  gfloat     *out = out_buf;
  gfloat     *m;
58 59 60 61 62 63

  gfloat ma[25] = { 1.0, 0.0, 0.0, 0.0, 0.0,
                    0.0, 1.0, 0.0, 0.0, 0.0,
                    0.0, 0.0, 1.0, 0.0, 0.0,
                    0.0, 0.0, 0.0, 1.0, 0.0,
                    0.0, 0.0, 0.0, 0.0, 1.0};
64 65
  char         *endptr;
  gfloat        value;
66 67
  const gchar  *delimiter = ",";
  const gchar  *delimiters = " ";
68
  gchar       **values;
69
  glong         i;
70

71 72
  m = ma;

73
  if ( o->values != NULL )
74
    {
75
      g_strstrip(o->values);
76 77
      g_strdelimit (o->values, delimiters, *delimiter);
      values = g_strsplit (o->values, delimiter, 1);
78
      if (values[0] != NULL)
79
        {
80
          value = g_ascii_strtod(values[0], &endptr);
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96
          if (endptr != values[0])
            if ( value >= 0.0 && value <= 1.0 )
              {
                 ma[0]  = 0.213 + 0.787 * value;
                 ma[1]  = 0.715 - 0.715 * value;
                 ma[2]  = 0.072 - 0.072 * value;
                 ma[5]  = 0.213 - 0.213 * value;
                 ma[6]  = 0.715 + 0.285 * value;
                 ma[7]  = 0.072 - 0.072 * value;
                 ma[10] = 0.213 - 0.213 * value;
                 ma[11] = 0.715 - 0.715 * value;
                 ma[12] = 0.072 + 0.928 * value;
              }
        }
      g_strfreev(values);
    }
97

98 99 100 101 102 103 104 105 106
  for (i=0; i<n_pixels; i++)
    {
      out[0] =  m[0]  * in[0] +  m[1]  * in[1] + m[2]  * in[2] + m[3]  * in[3] + m[4];
      out[1] =  m[5]  * in[0] +  m[6]  * in[1] + m[7]  * in[2] + m[8]  * in[3] + m[9];
      out[2] =  m[10] * in[0] +  m[11] * in[1] + m[12] * in[2] + m[13] * in[3] + m[14];
      out[3] =  m[15] * in[0] +  m[16] * in[1] + m[17] * in[2] + m[18] * in[3] + m[19];
      in  += 4;
      out += 4;
    }
107

108 109 110
  return TRUE;
}

111 112

static void
113
gegl_op_class_init (GeglOpClass *klass)
114 115 116 117 118 119 120 121 122 123
{
  GeglOperationClass            *operation_class;
  GeglOperationPointFilterClass *point_filter_class;

  operation_class    = GEGL_OPERATION_CLASS (klass);
  point_filter_class = GEGL_OPERATION_POINT_FILTER_CLASS (klass);

  point_filter_class->process = process;
  operation_class->prepare = prepare;

124 125 126 127 128
  gegl_operation_class_set_keys (operation_class,
    "name"       , "gegl:svg-saturate",
    "categories" , "compositors:svgfilter",
    "description", _("SVG color matrix operation svg_saturate"),
    NULL);
129 130
}

131
#endif