edge-sobel.c 12.4 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
/* 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
 * version 3 of the License, or (at your option) any later version.
 *
 * 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
 * License along with GEGL; if not, see <http://www.gnu.org/licenses/>.
 *
 */

/*
 * Copyright 2011 Victor Oliveira <victormatheus@gmail.com>
 */

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

25
#ifdef GEGL_PROPERTIES
26

27
property_boolean (horizontal,  _("Horizontal"), TRUE)
28

29
property_boolean (vertical,  _("Vertical"), TRUE)
30

31
property_boolean (keep_sign,  _("Keep Sign"), TRUE)
32
     description (_("Keep negative values in result; when off, the absolute value of the result is used instead."))
33

34 35
#else

36
#define GEGL_OP_AREA_FILTER
37
#define GEGL_OP_NAME     edge_sobel
38
#define GEGL_OP_C_SOURCE edge-sobel.c
39

40
#include "gegl-op.h"
41
#include <math.h>
42
#include <stdio.h>
43 44 45 46 47 48 49 50 51 52

#define SOBEL_RADIUS 1

static void
edge_sobel (GeglBuffer          *src,
            const GeglRectangle *src_rect,
            GeglBuffer          *dst,
            const GeglRectangle *dst_rect,
            gboolean            horizontal,
            gboolean            vertical,
53
            gboolean            keep_sign,
54
            gboolean            has_alpha);
55

56

57 58 59 60 61

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

62 63
  const Babl *source_format = gegl_operation_get_source_format (operation, "input");

64
  area->left = area->right = area->top = area->bottom = SOBEL_RADIUS;
65

66
  gegl_operation_set_format (operation, "input", babl_format ("RGBA float"));
67 68 69 70 71

  if (source_format && !babl_format_has_alpha (source_format))
    gegl_operation_set_format (operation, "output", babl_format ("RGB float"));
  else
    gegl_operation_set_format (operation, "output", babl_format ("RGBA float"));
72 73
}

74 75 76 77 78 79 80
/** FIXME - disabling CL
 *  CL border handling is broken. As is the whole result is shifted to avoid the
 *  issue. This is causing tests to fail. The shift can be corrected, but real
 *  issue is handling pixels that the sobel kernel needs from outside the image.
 */
/*

81
#include "opencl/gegl-cl.h"
82
#include "gegl-buffer-cl-iterator.h"
83

84
#include "opencl/edge-sobel.cl.h"
85

86
static GeglClRunData *cl_data = NULL;
87

88
static gboolean
89 90 91 92 93 94
cl_edge_sobel (cl_mem              in_tex,
               cl_mem              out_tex,
               size_t              global_worksize,
               const GeglRectangle *roi,
               gboolean            horizontal,
               gboolean            vertical,
95
               gboolean            keep_sign,
96
               gboolean            has_alpha)
97
{
Daniel Sabo's avatar
Daniel Sabo committed
98
  const size_t gbl_size[2] = {roi->width, roi->height};
99 100
  cl_int n_horizontal  = horizontal;
  cl_int n_vertical    = vertical;
101
  cl_int n_keep_sign = keep_sign;
102
  cl_int n_has_alpha   = has_alpha;
103 104
  cl_int cl_err = 0;

Daniel Sabo's avatar
Daniel Sabo committed
105 106 107
  if (!cl_data)
    {
      const char *kernel_name[] = {"kernel_edgesobel", NULL};
108
      cl_data = gegl_cl_compile_and_build (edge_sobel_cl_source, kernel_name);
Daniel Sabo's avatar
Daniel Sabo committed
109
    }
110 111 112 113 114 115 116 117 118 119
  if (!cl_data) return TRUE;

  cl_err = gegl_clSetKernelArg(cl_data->kernel[0], 0, sizeof(cl_mem), (void*)&in_tex);
  CL_CHECK;
  cl_err = gegl_clSetKernelArg(cl_data->kernel[0], 1, sizeof(cl_mem), (void*)&out_tex);
  CL_CHECK;
  cl_err = gegl_clSetKernelArg(cl_data->kernel[0], 2, sizeof(cl_int), (void*)&n_horizontal);
  CL_CHECK;
  cl_err = gegl_clSetKernelArg(cl_data->kernel[0], 3, sizeof(cl_int), (void*)&n_vertical);
  CL_CHECK;
120
  cl_err = gegl_clSetKernelArg(cl_data->kernel[0], 4, sizeof(cl_int), (void*)&n_keep_sign);
121 122 123
  CL_CHECK;
  cl_err = gegl_clSetKernelArg(cl_data->kernel[0], 5, sizeof(cl_int), (void*)&n_has_alpha);
  CL_CHECK;
124 125 126 127 128

  cl_err = gegl_clEnqueueNDRangeKernel(gegl_cl_get_command_queue(),
                                       cl_data->kernel[0], 2,
                                       NULL, gbl_size, NULL,
                                       0, NULL, NULL);
129 130 131
  CL_CHECK;

  return FALSE;
132

133 134
error:
  return TRUE;
135 136 137 138
}

static gboolean
cl_process (GeglOperation       *operation,
Daniel Sabo's avatar
Daniel Sabo committed
139 140 141 142
            GeglBuffer          *input,
            GeglBuffer          *output,
            const GeglRectangle *result,
            gboolean             has_alpha)
143
{
144 145
  const Babl *in_format  = babl_format ("RGBA float");
  const Babl *out_format = babl_format ("RGBA float");
146 147 148
  gint err;

  GeglOperationAreaFilter *op_area = GEGL_OPERATION_AREA_FILTER (operation);
149
  GeglProperties *o = GEGL_PROPERTIES (operation);
150

151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166
  GeglBufferClIterator *i = gegl_buffer_cl_iterator_new (output,
                                                         result,
                                                         out_format,
                                                         GEGL_CL_BUFFER_WRITE);

  gint read = gegl_buffer_cl_iterator_add_2 (i,
                                             input,
                                             result,
                                             in_format,
                                             GEGL_CL_BUFFER_READ,
                                             op_area->left,
                                             op_area->right,
                                             op_area->top,
                                             op_area->bottom,
                                             GEGL_ABYSS_NONE);

167 168
  while (gegl_buffer_cl_iterator_next (i, &err))
    {
169 170 171 172 173 174 175 176
      if (err) return FALSE;

      err = cl_edge_sobel(i->tex[read],
                          i->tex[0],
                          i->size[0],
                          &i->roi[0],
                          o->horizontal,
                          o->vertical,
177
                          o->keep_sign,
178 179 180
                          has_alpha);

      if (err) return FALSE;
181
    }
182

183 184
  return TRUE;
}
185
*/
186

187 188 189 190
static gboolean
process (GeglOperation       *operation,
         GeglBuffer          *input,
         GeglBuffer          *output,
191 192
         const GeglRectangle *result,
         gint                 level)
193
{
194
  GeglProperties   *o = GEGL_PROPERTIES (operation);
195
  GeglRectangle compute;
196
  gboolean has_alpha;
197

Daniel Sabo's avatar
Daniel Sabo committed
198 199
  compute = gegl_operation_get_required_for_output (operation, "input", result);
  has_alpha = babl_format_has_alpha (gegl_operation_get_format (operation, "output"));
200

201
  /*
202
  if (gegl_operation_use_opencl (operation))
Daniel Sabo's avatar
Daniel Sabo committed
203
    if (cl_process (operation, input, output, result, has_alpha))
204
      return TRUE;
205
   */
206

207 208
  edge_sobel (input, &compute, output, result,
              o->horizontal, o->vertical, o->keep_sign, has_alpha);
Daniel Sabo's avatar
Daniel Sabo committed
209
  return TRUE;
210 211 212
}

inline static gfloat
213
magnitude(gfloat a, gfloat b)
214 215 216 217 218 219 220 221 222 223 224
{
  return sqrtf(a*a+b*b);
}

static void
edge_sobel (GeglBuffer          *src,
            const GeglRectangle *src_rect,
            GeglBuffer          *dst,
            const GeglRectangle *dst_rect,
            gboolean            horizontal,
            gboolean            vertical,
225
            gboolean            keep_sign,
226
            gboolean            has_alpha)
227 228 229 230
{
  gint x,y;
  gint offset;
  gfloat *src_buf;
231
  gfloat *after_src_buf;
232
  gfloat *dst_buf;
233 234

  src_buf = g_new0 (gfloat, src_rect->width * src_rect->height * 4);
235 236
  dst_buf = g_new0 (gfloat, dst_rect->width * dst_rect->height * 4);

237
  gegl_buffer_get (src, src_rect, 1.0, babl_format ("RGBA float"),
238
                   src_buf, GEGL_AUTO_ROWSTRIDE, GEGL_ABYSS_NONE);
239

240
  after_src_buf = src_buf + src_rect->width * src_rect->height * 4;
241

242 243 244 245 246 247 248 249 250 251 252 253
  /* Apply the Sobel operator. Technically, the following is not Sobel
     as it does not use pixel intensities, but works on the individual
     RGB channels. But it is similar to the classic GIMP and PhotoShop
     filter.

     See paper "History and Definition of the Sobel Operator" by Irwin
     Sobel that seems to be the only free and authentic description.
  */
  offset = 0;
  for (y = 0; y < dst_rect->height; y++)
    for (x = 0; x < dst_rect->width; x++)
      {
254 255 256
        gfloat hor_grad[3] = {0.0f, 0.0f, 0.0f};
        gfloat ver_grad[3] = {0.0f, 0.0f, 0.0f};
        gfloat gradient[4] = {0.0f, 0.0f, 0.0f, 0.0f};
257
        gfloat *row_start, *row_next;
258 259 260
        gfloat *tl_px, *t_px, *tr_px;
        gfloat *l_px, *center_px, *r_px;
        gfloat *bl_px, *b_px, *br_px;
261 262
        gint c;

263 264
        row_start = src_buf + y * src_rect->width * 4;
        row_next = row_start + src_rect->width * 4;
265

266 267
        /* This pixel */
        center_px = row_start + x * 4;
268
        /* Top pixel */
269 270 271
        t_px = center_px - src_rect->width * 4;
        /* Top-left pixel */
        tl_px = t_px - 4;
272
        /* Top-right pixel */
273
        tr_px = t_px + 4;
274 275 276 277 278
        /* Left pixel */
        l_px = center_px - 4;
        /* Right pixel */
        r_px = center_px + 4;
        /* Bottom pixel */
279 280 281
        b_px = center_px + src_rect->width * 4;
        /* Bottom-left pixel */
        bl_px = b_px - 4;
282
        /* Bottom-right pixel */
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
        br_px = b_px + 4;

        /* If pixels around the center pixel are out of source rect
           bounds, clamp them into the rect. */
        if (t_px < src_buf)
          {
            tl_px += src_rect->width * 4;
            t_px += src_rect->width * 4;
            tr_px += src_rect->width * 4;
          }
        else if (b_px >= after_src_buf)
          {
            bl_px -= src_rect->width * 4;
            b_px -= src_rect->width * 4;
            br_px -= src_rect->width * 4;
          }

        if (l_px < row_start)
          {
            tl_px += 4;
            l_px += 4;
            bl_px += 4;
          }
        else if (r_px >= row_next)
          {
            tr_px -= 4;
            r_px -= 4;
            br_px -= 4;
          }
312

313
        if (horizontal)
314
          {
315 316 317 318 319 320 321 322 323 324 325 326 327
            /*
             * Horizontal kernel:
             *
             *      [-1  0  +1]
             * Gx = [-2  0  +2] * P
             *      [-1  0  -1]
             */
            for (c = 0; c < 3; c++)
              {
                hor_grad[c] += (-1.0f * tl_px[c]) + (0.0f * t_px[c]) + (1.0f * tr_px[c]);
                hor_grad[c] += (-2.0f * l_px[c]) + (0.0f * center_px[c]) + (2.0f * r_px[c]);
                hor_grad[c] += (-1.0f * bl_px[c]) + (0.0f * b_px[c]) + (1.0f * br_px[c]);
              }
328 329
          }

330
        if (vertical)
331
          {
332 333 334 335 336 337 338 339 340 341 342 343 344 345
            /*
             * Vertical kernel:
             *
             *      [+1  +2  +1]
             * Gy = [ 0   0   0] * P
             *      [-1  -2  -1]
             */
            for (c = 0; c < 3; c++)
              {
                ver_grad[c] += (1.0f * tl_px[c]) + (2.0f * t_px[c]) + (1.0f * tr_px[c]);
                ver_grad[c] += (0.0f * l_px[c]) + (0.0f * center_px[c]) + (0.0f * r_px[c]);
                ver_grad[c] += (-1.0f * bl_px[c]) + (-2.0f * b_px[c]) + (-1.0f * br_px[c]);
              }
          }
346 347 348

        if (horizontal && vertical)
          {
349
            for (c = 0; c < 3; c++)
350
              gradient[c] = magnitude (hor_grad[c], ver_grad[c]) / sqrtf (32.0f);
351
          }
352
        else
353
          {
354
            if (keep_sign)
355
              {
356
                for (c = 0; c < 3; c++)
357
                  gradient[c] = 0.5f + (hor_grad[c] + ver_grad[c]) / 8.0f;
358 359 360
              }
            else
              {
361
                for (c = 0; c < 3; c++)
362
                  gradient[c] = fabsf (hor_grad[c] + ver_grad[c]) / 4.0f;
363
              }
364 365
          }

366
        if (has_alpha)
367
          gradient[3] = center_px[3];
368 369 370
        else
          gradient[3] = 1.0f;

371 372
        for (c = 0; c < 4; c++)
          dst_buf[offset * 4 + c] = gradient[c];
373

374 375
        offset++;
      }
376

377
  gegl_buffer_set (dst, dst_rect, 0, babl_format ("RGBA float"), dst_buf,
378 379 380 381 382 383
                   GEGL_AUTO_ROWSTRIDE);
  g_free (src_buf);
  g_free (dst_buf);
}

static void
384
gegl_op_class_init (GeglOpClass *klass)
385 386 387 388 389 390 391
{
  GeglOperationClass       *operation_class;
  GeglOperationFilterClass *filter_class;

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

392
  operation_class->prepare        = prepare;
393 394
  operation_class->opencl_support = TRUE;

395 396
  filter_class->process           = process;

397
  gegl_operation_class_set_keys (operation_class,
398
    "name",        "gegl:edge-sobel",
399
    "title",       _("Sobel Edge Detection"),
400 401
    "categories",  "edge-detect",
    "description", _("Specialized direction-dependent edge detection"),
402
          NULL);
403 404 405
}

#endif