edge.c 10.2 KB
Newer Older
Thomas Manni's avatar
Thomas Manni committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
/* This file is an image processing operation for GEGL
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program 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 General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 * Author: Jef Poskanzer.
 *
 * GEGL Port: Thomas Manni <thomas.manni@free.fr>
 *
 */

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


#ifdef GEGL_PROPERTIES

enum_start (gegl_edge_algo)
   enum_value (GEGL_EDGE_SOBEL,    "sobel",    N_("Sobel"))
   enum_value (GEGL_EDGE_PREWITT,  "prewitt",  N_("Prewitt compass"))
   enum_value (GEGL_EDGE_GRADIENT, "gradient", N_("Gradient"))
   enum_value (GEGL_EDGE_ROBERTS,  "roberts",  N_("Roberts"))
   enum_value (GEGL_EDGE_DIFFERENTIAL, "differential", N_("Differential"))
   enum_value (GEGL_EDGE_LAPLACE,  "laplace",  N_("Laplace"))
enum_end (GeglEdgeAlgo)

property_enum (algorithm, _("Algorithm"),
               GeglEdgeAlgo, gegl_edge_algo,
               GEGL_EDGE_SOBEL)
  description (_("Edge detection algorithm"))

property_double (amount, _("Amount"), 2.0)
    description (_("Edge detection amount"))
    value_range (1.0, 10.0)
    ui_range    (1.0, 10.0)

47 48 49
property_enum (border_behavior, _("Border behavior"),
               GeglAbyssPolicy, gegl_abyss_policy,
               GEGL_ABYSS_CLAMP)
Thomas Manni's avatar
Thomas Manni committed
50 51 52 53 54
  description (_("Edge detection behavior"))

#else

#define GEGL_OP_AREA_FILTER
55
#define GEGL_OP_NAME        edge
Thomas Manni's avatar
Thomas Manni committed
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265
#define GEGL_OP_C_SOURCE    edge.c

#include <math.h>
#include "gegl-op.h"

static inline gfloat
edge_sobel (gfloat *pixels, gdouble amount)
{
  const gint v_kernel[9] = { -1,  0,  1,
                             -2,  0,  2,
                             -1,  0,  1 };
  const gint h_kernel[9] = { -1, -2, -1,
                              0,  0,  0,
                              1,  2,  1 };

  gint i;
  gfloat v_grad, h_grad;

  for (i = 0, v_grad = 0.0f, h_grad = 0.0f; i < 9; i++)
    {
      v_grad += v_kernel[i] * pixels[i];
      h_grad += h_kernel[i] * pixels[i];
    }

  return sqrt (v_grad * v_grad * amount +
               h_grad * h_grad * amount);
}

static inline gfloat
edge_prewitt (gfloat *pixels, gdouble amount)
{
  gint k;
  gfloat max;
  gfloat m[8];

  m[0] =   pixels[0] +   pixels[1] + pixels[2]
         + pixels[3] - 2*pixels[4] + pixels[5]
         - pixels[6] -   pixels[7] - pixels[8];
  m[1] =   pixels[0] +   pixels[1] + pixels[2]
         + pixels[3] - 2*pixels[4] - pixels[5]
         + pixels[6] -   pixels[7] - pixels[8];
  m[2] =   pixels[0] +   pixels[1] - pixels[2]
         + pixels[3] - 2*pixels[4] - pixels[5]
         + pixels[6] +   pixels[7] - pixels[8];
  m[3] =   pixels[0] -   pixels[1] - pixels[2]
         + pixels[3] - 2*pixels[4] - pixels[5]
         + pixels[6] +   pixels[7] + pixels[8];
  m[4] = - pixels[0] -   pixels[1] - pixels[2]
         + pixels[3] - 2*pixels[4] + pixels[5]
         + pixels[6] +   pixels[7] + pixels[8];
  m[5] = - pixels[0] -   pixels[1] + pixels[2]
         - pixels[3] - 2*pixels[4] + pixels[5]
         + pixels[6] +   pixels[7] + pixels[8];
  m[6] = - pixels[0] +   pixels[1] + pixels[2]
         - pixels[3] - 2*pixels[4] + pixels[5]
         - pixels[6] +   pixels[7] + pixels[8];
  m[7] =   pixels[0] +   pixels[1] + pixels[2]
         - pixels[3] - 2*pixels[4] + pixels[5]
         - pixels[6] -   pixels[7] + pixels[8];

  for (k = 0, max = 0.0f; k < 8; k++)
    if (max < m[k])
      max = m[k];

  return amount * max;
}

static inline gfloat
edge_gradient (gfloat *pixels, gdouble amount)
{
  const gint v_kernel[9] = { 0,  0,  0,
                             0,  4, -4,
                             0,  0,  0 };
  const gint h_kernel[9] = { 0,  0,  0,
                             0, -4,  0,
                             0,  4,  0 };

  gint i;
  gfloat v_grad, h_grad;

  for (i = 0, v_grad = 0.0f, h_grad = 0.0f; i < 9; i++)
    {
      v_grad += v_kernel[i] * pixels[i];
      h_grad += h_kernel[i] * pixels[i];
    }

  return  sqrt (v_grad * v_grad * amount +
                h_grad * h_grad * amount);
}

static inline gfloat
edge_roberts (gfloat *pixels, gdouble amount)
{
  const gint v_kernel[9] = { 0,  0,  0,
                             0,  4,  0,
                             0,  0, -4 };
  const gint h_kernel[9] = { 0,  0,  0,
                             0,  0,  4,
                             0, -4,  0 };
  gint i;
  gfloat v_grad, h_grad;

  for (i = 0, v_grad = 0.0f, h_grad = 0.0f; i < 9; i++)
    {
      v_grad += v_kernel[i] * pixels[i];
      h_grad += h_kernel[i] * pixels[i];
    }

  return sqrt (v_grad * v_grad * amount +
               h_grad * h_grad * amount);
}

static inline gfloat
edge_differential (gfloat *pixels, gdouble amount)
{
  const gint v_kernel[9] = { 0,  0,  0,
                             0,  2, -2,
                             0,  2, -2 };
  const gint h_kernel[9] = { 0,  0,  0,
                             0, -2, -2,
                             0,  2,  2 };
  gint i;
  gfloat v_grad, h_grad;

  for (i = 0, v_grad = 0.0f, h_grad = 0.0f; i < 9; i++)
    {
      v_grad += v_kernel[i] * pixels[i];
      h_grad += h_kernel[i] * pixels[i];
    }

  return sqrt (v_grad * v_grad * amount +
               h_grad * h_grad * amount);
}

static inline gfloat
edge_laplace (gfloat *pixels, gdouble amount)
{
  const gint kernel[9] = { 1,  1,  1,
                           1, -8,  1,
                           1,  1,  1 };
  gint i;
  gfloat grad;

  for (i = 0, grad = 0.0f; i < 9; i++)
    grad += kernel[i] * pixels[i];

  return grad * amount;
}

static void
prepare (GeglOperation *operation)
{
  GeglOperationAreaFilter *area = GEGL_OPERATION_AREA_FILTER (operation);

  const Babl *input_f = gegl_operation_get_source_format (operation, "input");
  const Babl *format  = babl_format ("R'G'B' float");

  area->left   =
  area->right  =
  area->top    =
  area->bottom = 1;

  if (input_f)
    {
      if (babl_format_has_alpha (input_f))
        format = babl_format ("R'G'B'A float");
    }

  gegl_operation_set_format (operation, "input", format);
  gegl_operation_set_format (operation, "output", format);
}

static GeglRectangle
get_bounding_box (GeglOperation *operation)
{
  GeglRectangle  result = { 0, 0, 0, 0 };
  GeglRectangle *in_rect;

  in_rect = gegl_operation_source_get_bounding_box (operation, "input");
  if (in_rect)
    {
      result = *in_rect;
    }

  return result;
}

static gboolean
process (GeglOperation       *operation,
         GeglBuffer          *input,
         GeglBuffer          *output,
         const GeglRectangle *roi,
         gint                 level)
{
  GeglProperties *o      = GEGL_PROPERTIES (operation);
  const Babl     *format = gegl_operation_get_format (operation, "output");
  gint            components = babl_format_get_n_components (format);
  gboolean        has_alpha  = babl_format_has_alpha (format);

  gfloat *src_buff;
  gfloat *dst_buff;
  GeglRectangle rect;
  gint x, y, ix, iy, b, idx;

  rect = gegl_operation_get_required_for_output (operation, "input", roi);

  src_buff = g_new (gfloat, rect.width * rect.height * components);
  dst_buff = g_new0 (gfloat, roi->width * roi->height * components);

  gegl_buffer_get (input, &rect, 1.0, format, src_buff,
266
                   GEGL_AUTO_ROWSTRIDE, o->border_behavior);
Thomas Manni's avatar
Thomas Manni committed
267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358

  for (y = 0; y < roi->height; y++)
    {
      iy = y + 1;
      for (x = 0; x < roi->width; x++)
        {
          ix = x + 1;
          for (b = 0; b < 3; b++)
            {

#define SRCPIX(X,Y,B) src_buff[((X) + (Y) * rect.width) * components + B]

              gfloat window[9];
              window[0] = SRCPIX(ix - 1, iy - 1, b);
              window[1] = SRCPIX(ix, iy - 1, b);
              window[2] = SRCPIX(ix + 1, iy - 1, b);
              window[3] = SRCPIX(ix - 1, iy, b);
              window[4] = SRCPIX(ix, iy, b);
              window[5] = SRCPIX(ix + 1, iy, b);
              window[6] = SRCPIX(ix - 1, iy + 1, b);
              window[7] = SRCPIX(ix, iy + 1, b);
              window[8] = SRCPIX(ix + 1, iy + 1, b);

              idx = (x + y * roi->width) * components + b;

              switch (o->algorithm)
                {
                  default:
                  case GEGL_EDGE_SOBEL:
                    dst_buff[idx] = edge_sobel (window, o->amount);
                    break;

                  case GEGL_EDGE_PREWITT:
                    dst_buff[idx] = edge_prewitt (window, o->amount);
                    break;

                  case GEGL_EDGE_GRADIENT:
                    dst_buff[idx] = edge_gradient (window, o->amount);
                    break;

                  case GEGL_EDGE_ROBERTS:
                    dst_buff[idx] = edge_roberts (window, o->amount);
                    break;

                  case GEGL_EDGE_DIFFERENTIAL:
                    dst_buff[idx] = edge_differential (window, o->amount);
                    break;

                  case GEGL_EDGE_LAPLACE:
                    dst_buff[idx] = edge_laplace (window, o->amount);
                    break;
                }
            }

          if (has_alpha)
            dst_buff[idx + 1] = SRCPIX(ix, iy, 3);
        }
#undef SRCPIX
    }

  gegl_buffer_set (output, roi, level, format, dst_buff, GEGL_AUTO_ROWSTRIDE);

  g_free (src_buff);
  g_free (dst_buff);

  return TRUE;
}

static void
gegl_op_class_init (GeglOpClass *klass)
{
  GeglOperationClass       *operation_class;
  GeglOperationFilterClass *filter_class;

  operation_class = GEGL_OPERATION_CLASS (klass);
  filter_class    = GEGL_OPERATION_FILTER_CLASS (klass);

  filter_class->process             = process;
  operation_class->prepare          = prepare;
  operation_class->get_bounding_box = get_bounding_box;
  operation_class->opencl_support   = FALSE;

  gegl_operation_class_set_keys (operation_class,
    "name",        "gegl:edge",
    "title",       _("Edge Detection"),
    "categories",  "edge-detect",
    "license",     "GPL3+",
    "description", _("Several simple methods for detecting edges"),
    NULL);
}

#endif