threshold.c 3.52 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
 *
 * 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 28
property_double (value, _("Threshold"), 0.5)
    value_range (-200, 200)
    ui_range    (-1, 2)
    description(_("Scalar threshold level (overriden if an auxiliary input buffer is provided.)."))
29 30 31

#else

32 33
#define GEGL_OP_POINT_COMPOSER
#define GEGL_OP_C_FILE       "threshold.c"
34

35
#include "gegl-op.h"
36

37
static void prepare (GeglOperation *operation)
38
{
39 40
  gegl_operation_set_format (operation, "input",  babl_format ("YA float"));
  gegl_operation_set_format (operation, "aux",    babl_format ("Y float"));
41
  gegl_operation_set_format (operation, "output", babl_format ("YA float"));
42 43 44
}

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

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

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

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

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

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

94
#include "opencl/threshold.cl.h"
95

96 97 98 99 100 101 102 103 104 105 106 107 108 109 110
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>";

111
static void
112
gegl_op_class_init (GeglOpClass *klass)
113 114 115 116 117 118 119 120 121
{
  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
122

123
  gegl_operation_class_set_keys (operation_class,
124 125
    "name" ,       "gegl:threshold",
    "title",       _("Threshold"),
126 127 128 129
    "categories" , "color",
    "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."),
130
    "cl-source",   threshold_cl_source,
131
    "reference-composition", composition,
132
    NULL);
133 134
}

135
#endif