mono-mixer.c 3.44 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 <https://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 52 53
  const Babl *space = gegl_operation_get_source_space (operation, "input");
  gegl_operation_set_format (operation, "input",  babl_format_with_space ("RGBA float", space));
  gegl_operation_set_format (operation, "output", babl_format_with_space ("YA float", space));
54 55
}

56
static gboolean
57 58 59 60 61
process (GeglOperation       *op,
         void                *in_buf,
         void                *out_buf,
         glong                n_pixels,
         const GeglRectangle *roi,
62
         gint                 level)
63
{
64 65 66 67 68 69
  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;
70 71 72
  gfloat     * GEGL_ALIGNED in_pixel;
  gfloat     * GEGL_ALIGNED out_pixel;
  glong       i;
73

74 75
  in_pixel   = in_buf;
  out_pixel  = out_buf;
76

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

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

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

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

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

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

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

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

123
#endif