threshold.c 3.74 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
 *
 * Copyright 2006 Øyvind Kolås <pippin@gimp.org>
 */
18 19 20 21 22

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


23 24
#ifdef GEGL_PROPERTIES

25 26 27
property_double (value, _("Threshold"), 0.5)
    value_range (-200, 200)
    ui_range    (-1, 2)
Anders Jonsson's avatar
Anders Jonsson committed
28
    description(_("Scalar threshold level (overridden if an auxiliary input buffer is provided.)."))
29 30 31

#else

32
#define GEGL_OP_POINT_COMPOSER
33
#define GEGL_OP_NAME     threshold
34
#define GEGL_OP_C_SOURCE threshold.c
35

36
#include "gegl-op.h"
37

38
static void prepare (GeglOperation *operation)
39
{
40 41 42 43
  const Babl *space = gegl_operation_get_source_space (operation, "input");
  gegl_operation_set_format (operation, "input",  babl_format_with_space ("Y'A float", space));
  gegl_operation_set_format (operation, "aux",    babl_format_with_space ("Y' float", space));
  gegl_operation_set_format (operation, "output", babl_format_with_space ("Y'A float", space));
44 45 46
}

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

  if (aux == NULL)
    {
62
      gfloat value = GEGL_PROPERTIES (op)->value;
63 64
      for (i=0; i<n_pixels; i++)
        {
65 66 67 68 69 70
          gfloat c;

          c = in[0];
          c = c>=value?1.0:0.0;
          out[0] = c;

71 72 73 74 75 76 77 78 79
          out[1] = in[1];
          in  += 2;
          out += 2;
        }
    }
  else
    {
      for (i=0; i<n_pixels; i++)
        {
80 81 82 83 84 85 86
          gfloat value = *aux;
          gfloat c;

          c = in[0];
          c = c>=value?1.0:0.0;
          out[0] = c;

87 88 89 90 91 92 93 94
          out[1] = in[1];
          in  += 2;
          out += 2;
          aux += 1;
        }
    }
  return TRUE;
}
95

96
#include "opencl/threshold.cl.h"
97

98 99 100 101 102 103 104 105 106 107 108 109 110 111 112
static const gchar *composition =
    "<?xml version='1.0'             encoding='UTF-8'?>"
    "<gegl>"
    "<node operation='gegl:threshold'>"
    "  <params>"
    "    <param name='value'>0.5</param>"
    "  </params>"
    "</node>"
    "<node operation='gegl:load'>"
    "  <params>"
    "    <param name='path'>standard-input.png</param>"
    "  </params>"
    "</node>"
    "</gegl>";

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

  operation_class = GEGL_OPERATION_CLASS (klass);
  point_composer_class = GEGL_OPERATION_POINT_COMPOSER_CLASS (klass);

  point_composer_class->process = process;
  operation_class->prepare = prepare;
Victor Oliveira's avatar
Victor Oliveira committed
124

125
  gegl_operation_class_set_keys (operation_class,
126 127
    "name" ,       "gegl:threshold",
    "title",       _("Threshold"),
128
    "categories" , "color",
129
    "reference-hash", "d20432270a1364932ee88a326a3e26c8",
130 131 132
    "description",
          _("Thresholds the image to white/black based on either the global value "
            "set in the value property, or per pixel from the aux input."),
133
    "cl-source",   threshold_cl_source,
134
    "reference-composition", composition,
135
    NULL);
136 137
}

138
#endif