mono-mixer.c 3.27 KB
Newer Older
1 2 3 4 5
/* This file is an image processing operation for GEGL
 *
 * 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
6
 * version 3 of the License, or (at your option) any later version.
7 8 9 10 11 12 13
 *
 * 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
14
 * License along with GEGL; if not, see <http://www.gnu.org/licenses/>.
15 16 17 18
 *
 * Copyright 2006 Mark Probst <mark.probst@gmail.com>
 */

19 20 21

#include "config.h"
#include <glib/gi18n-lib.h>
22
#include <math.h>
23 24


25
#ifdef GEGL_PROPERTIES
26
property_boolean (preserve_luminosity, _("Preserve luminosity"), FALSE)
27

28 29 30
property_double (red, _("Red Channel Multiplier"), 0.333)
    value_range (-5.0, 5.0)
    ui_range    (-2.0, 2.0)
31

32 33 34
property_double (green, _("Green Channel Multiplier"), 0.333)
    value_range (-5.0, 5.0)
    ui_range    (-2.0, 2.0)
35

36 37 38
property_double (blue, _("Blue Channel Multiplier"), 0.333)
    value_range (-5.0, 5.0)
    ui_range    (-2.0, 2.0)
39

40 41
#else

42
#define GEGL_OP_POINT_FILTER
43
#define GEGL_OP_NAME     mono_mixer
44
#define GEGL_OP_C_SOURCE mono-mixer.c
45

46
#include "gegl-op.h"
47

48 49
static void
prepare (GeglOperation *operation)
50
{
51
  gegl_operation_set_format (operation, "input",  babl_format ("RGBA float"));
52
  gegl_operation_set_format (operation, "output", babl_format ("YA float"));
53 54
}

55
static gboolean
56 57 58 59 60
process (GeglOperation       *op,
         void                *in_buf,
         void                *out_buf,
         glong                n_pixels,
         const GeglRectangle *roi,
61
         gint                 level)
62
{
63 64 65 66 67 68
  GeglProperties *o       = GEGL_PROPERTIES (op);
  gfloat      red         = o->red;
  gfloat      green       = o->green;
  gfloat      blue        = o->blue;
  gboolean    normalize   = o->preserve_luminosity;
  gfloat      norm_factor = 1.0;
69 70 71
  gfloat     * GEGL_ALIGNED in_pixel;
  gfloat     * GEGL_ALIGNED out_pixel;
  glong       i;
72

73 74
  in_pixel   = in_buf;
  out_pixel  = out_buf;
75

76 77 78 79 80 81 82 83 84 85
  if (normalize)
   {
     gdouble sum = red + green + blue;

     if (sum == 0.0)
       norm_factor = 1.0;
     else
       norm_factor = fabs (1 / sum);
   }

86 87
  for (i=0; i<n_pixels; i++)
    {
88 89 90
      out_pixel[0] = (in_pixel[0] * red +
                      in_pixel[1] * green +
                      in_pixel[2] * blue) * norm_factor;
91 92 93 94 95
      out_pixel[1] = in_pixel[3];
      in_pixel  += 4;
      out_pixel += 2;
    }
  return TRUE;
96
}
97

98
#include "opencl/mono-mixer.cl.h"
99

100
static void
101
gegl_op_class_init (GeglOpClass *klass)
102
{
103 104
  GeglOperationClass            *operation_class;
  GeglOperationPointFilterClass *point_filter_class;
105

106 107
  operation_class    = GEGL_OPERATION_CLASS (klass);
  point_filter_class = GEGL_OPERATION_POINT_FILTER_CLASS (klass);
108

109
  operation_class->prepare    = prepare;
110
  point_filter_class->process = process;
111

112
  gegl_operation_class_set_keys (operation_class,
113
    "name",        "gegl:mono-mixer",
114
    "title",       _("Mono Mixer"),
115
    "categories",  "color",
116
    "description", _("Monochrome channel mixer"),
117
    "cl-source",   mono_mixer_cl_source,
118
    NULL);
119 120
}

121
#endif