snn-mean.c 11.3 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 19
 *
 * Copyright 2005 Øyvind Kolås <pippin@gimp.org>,
 *           2007 Øyvind Kolås <oeyvindk@hig.no>
 */

20 21 22 23
#include "config.h"
#include <glib/gi18n-lib.h>


24
#ifdef GEGL_PROPERTIES
25

26 27 28 29 30 31 32 33 34 35
property_int (radius, _("Radius"), 8)
    description(_("Radius of square pixel region, (width and height will be radius*2+1)"))
    value_range (0, 100)
    ui_range    (0, 40)
    ui_gamma    (1.5)
    ui_meta     ("unit", "pixel-distance")

property_int (pairs, _("Pairs"), 2)
  description(_("Number of pairs; higher number preserves more acute features"))
  value_range (1, 2)
36 37 38

#else

39
#define GEGL_OP_AREA_FILTER
40
#define GEGL_OP_NAME     snn_mean
41
#define GEGL_OP_C_SOURCE snn-mean.c
42

43
#include "gegl-op.h"
44 45 46
#include <math.h>

static void
47
snn_mean (GeglBuffer          *src,
48
          const GeglRectangle *src_rect,
49 50 51
          GeglBuffer          *dst,
          const GeglRectangle *dst_rect,
          gdouble              radius,
52
          gint                 pairs,
53 54
          gint                 level,
          const Babl          *space);
55 56


57
static void prepare (GeglOperation *operation)
58
{
59
  const Babl *space = gegl_operation_get_source_space (operation, "input");
60
  GeglOperationAreaFilter *area = GEGL_OPERATION_AREA_FILTER (operation);
61
  GeglProperties          *o    = GEGL_PROPERTIES (operation);
62 63

  area->left = area->right = area->top = area->bottom = ceil (o->radius);
64 65 66 67
  gegl_operation_set_format (operation, "input",
                             babl_format_with_space ("RGBA float", space));
  gegl_operation_set_format (operation, "output",
                             babl_format_with_space ("RGBA float", space));
68 69
}

70 71 72 73 74 75
static gboolean
cl_process (GeglOperation       *operation,
            GeglBuffer          *input,
            GeglBuffer          *output,
            const GeglRectangle *result);

76
static gboolean
77
process (GeglOperation       *operation,
78 79
         GeglBuffer          *input,
         GeglBuffer          *output,
80 81
         const GeglRectangle *result,
         gint                 level)
82
{
83
  GeglProperties      *o = GEGL_PROPERTIES (operation);
84
  GeglRectangle        compute;
85

86
  if (gegl_operation_use_opencl (operation))
87 88 89
    if (cl_process (operation, input, output, result))
      return TRUE;

90
  compute = gegl_operation_get_required_for_output (operation, "input", result);
91

92
  if (o->radius < 1.0)
93
    {
94 95 96 97
      output = g_object_ref (input);
    }
  else
    {
98
      snn_mean (input, &compute, output, result, o->radius, o->pairs, level, gegl_operation_get_format (operation, "output"));
99
    }
100

101 102 103 104 105 106 107 108 109 110 111 112 113 114 115
  return  TRUE;
}

#define POW2(a)((a)*(a))

static inline gfloat colordiff (gfloat *pixA,
                                gfloat *pixB)
{
  return POW2(pixA[0]-pixB[0])+
         POW2(pixA[1]-pixB[1])+
         POW2(pixA[2]-pixB[2]);
}


static void
116
snn_mean (GeglBuffer          *src,
117
          const GeglRectangle *src_rect,
118 119 120
          GeglBuffer          *dst,
          const GeglRectangle *dst_rect,
          gdouble              dradius,
121
          gint                 pairs,
122 123
          gint                 level,
          const Babl          *space)
124 125 126 127 128
{
  gint x,y;
  gint offset;
  gfloat *src_buf;
  gfloat *dst_buf;
129
  gint radius = dradius;
130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150
  GeglRectangle src_rect_scaled, dst_rect_scaled;
  if (level)
  {
    src_rect_scaled = *src_rect;
    dst_rect_scaled = *dst_rect;
    src_rect_scaled.x >>= level;
    src_rect_scaled.y >>= level;
    src_rect_scaled.width >>= level;
    src_rect_scaled.height >>= level;
    dst_rect_scaled.x >>= level;
    dst_rect_scaled.y >>= level;
    dst_rect_scaled.width >>= level;
    dst_rect_scaled.height >>= level;
    src_rect = &src_rect_scaled;
    dst_rect = &dst_rect_scaled;
    dradius /= (1<<level);
  }
  radius = dradius;


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

153
  gegl_buffer_get (src, src_rect, 1.0/(1<<level), babl_format_with_space ("RGBA float", space), src_buf, 
154
                   GEGL_AUTO_ROWSTRIDE, GEGL_ABYSS_NONE);
155 156 157

  offset = 0;

158
  for (y=0; y<dst_rect->height; y++)
159 160
    {
      gfloat *center_pix;
161

162
      center_pix = src_buf + ((radius) + (y+radius)* src_rect->width)*4;
163

164
      for (x=0; x<dst_rect->width; x++)
165 166 167
        {
          gint u,v;

168
          gfloat  accumulated[4]={0.0f, 0.0f, 0.0f, 0.0f};
169 170
          gint    count=0;

Anders Jonsson's avatar
Anders Jonsson committed
171
          /* iterate through the upper left quarter of pixels */
172 173 174 175 176 177 178 179 180 181 182 183 184 185
          for (v=-radius;v<=0;v++)
            for (u=-radius;u<= (pairs==1?radius:0);u++)
              {
                gfloat *selected_pix = center_pix;
                gfloat  best_diff = 1000.0;
                gint    i;

                /* skip computations for the center pixel */
                if (u != 0 &&
                    v != 0)
                  {
                    /* compute the coordinates of the symmetric pairs for
                     * this locaiton in the quadrant
                     */
186 187 188 189 190 191 192 193 194 195
                    gint xs[4], ys[4];

                    xs[0] = x+u+radius;
                    xs[1] = x-u+radius;
                    xs[2] = x-u+radius;
                    xs[3] = x+u+radius;
                    ys[0] = y+v+radius;
                    ys[1] = y-v+radius;
                    ys[2] = y+v+radius;
                    ys[3] = y-v+radius;
196 197 198 199

                    /* check which member of the symmetric quadruple to use */
                    for (i=0;i<pairs*2;i++)
                      {
200 201
                        if (xs[i] >= 0 && xs[i] < src_rect->width &&
                            ys[i] >= 0 && ys[i] < src_rect->height)
202
                          {
203
                            gfloat *tpix = src_buf + (xs[i]+ys[i]* src_rect->width)*4;
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
                            gfloat diff = colordiff (tpix, center_pix);
                            if (diff < best_diff)
                              {
                                best_diff = diff;
                                selected_pix = tpix;
                              }
                          }
                      }
                  }

                /* accumulate the components of the best sample from
                 * the symmetric quadruple
                 */
                for (i=0;i<4;i++)
                  {
                    accumulated[i] += selected_pix[i];
                  }
                count++;

                if (u==0 && v==0)
                  break; /* to avoid doubly processing when using only 1 pair */
              }
          for (u=0; u<4; u++)
            dst_buf[offset*4+u] = accumulated[u]/count;
          offset++;

          center_pix += 4;
        }
    }
233
  gegl_buffer_set (dst, dst_rect, level, babl_format_with_space ("RGBA float", space), dst_buf,
234
                   GEGL_AUTO_ROWSTRIDE);
235 236 237 238
  g_free (src_buf);
  g_free (dst_buf);
}

239

240
#include "opencl/gegl-cl.h"
241
#include "gegl-buffer-cl-iterator.h"
242

Victor Oliveira's avatar
Victor Oliveira committed
243
#include "opencl/snn-mean.cl.h"
244

245
static GeglClRunData *cl_data = NULL;
246

Victor Oliveira's avatar
Victor Oliveira committed
247
static gboolean
248 249 250 251 252 253 254 255 256 257 258 259
cl_snn_mean (cl_mem                in_tex,
             cl_mem                out_tex,
             const GeglRectangle  *src_rect,
             const GeglRectangle  *roi,
             gint                  radius,
             gint                  pairs)
{
  cl_int cl_err = 0;
  size_t global_ws[2];

  if (!cl_data)
    {
Victor Oliveira's avatar
Victor Oliveira committed
260 261
      const char *kernel_name[] = {"snn_mean", NULL};
      cl_data = gegl_cl_compile_and_build (snn_mean_cl_source, kernel_name);
262
    }
Victor Oliveira's avatar
Victor Oliveira committed
263
  if (!cl_data) return TRUE;
264 265 266 267 268


  global_ws[0] = roi->width;
  global_ws[1] = roi->height;

Victor Oliveira's avatar
Victor Oliveira committed
269 270 271 272 273 274 275 276 277 278 279 280
  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_int),   (void*)&src_rect->width);
  CL_CHECK;
  cl_err = gegl_clSetKernelArg(cl_data->kernel[0], 2, sizeof(cl_int),   (void*)&src_rect->height);
  CL_CHECK;
  cl_err = gegl_clSetKernelArg(cl_data->kernel[0], 3, sizeof(cl_mem),   (void*)&out_tex);
  CL_CHECK;
  cl_err = gegl_clSetKernelArg(cl_data->kernel[0], 4, sizeof(cl_int),   (void*)&radius);
  CL_CHECK;
  cl_err = gegl_clSetKernelArg(cl_data->kernel[0], 5, sizeof(cl_int),   (void*)&pairs);
  CL_CHECK;
281 282 283 284 285

  cl_err = gegl_clEnqueueNDRangeKernel(gegl_cl_get_command_queue (),
                                        cl_data->kernel[0], 2,
                                        NULL, global_ws, NULL,
                                        0, NULL, NULL);
Victor Oliveira's avatar
Victor Oliveira committed
286
  CL_CHECK;
287

Victor Oliveira's avatar
Victor Oliveira committed
288 289 290 291
  return FALSE;

error:
  return TRUE;
292 293 294 295 296 297 298 299 300 301 302 303 304
}

static gboolean
cl_process (GeglOperation       *operation,
            GeglBuffer          *input,
            GeglBuffer          *output,
            const GeglRectangle *result)
{
  const Babl *in_format  = gegl_operation_get_format (operation, "input");
  const Babl *out_format = gegl_operation_get_format (operation, "output");
  gint err;

  GeglOperationAreaFilter *op_area = GEGL_OPERATION_AREA_FILTER (operation);
305
  GeglProperties *o = GEGL_PROPERTIES (operation);
306

307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322
  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);

323 324 325
  while (gegl_buffer_cl_iterator_next (i, &err))
    {
      if (err) return FALSE;
326 327 328 329 330 331 332 333 334

      err = cl_snn_mean(i->tex[read],
                        i->tex[0],
                        &i->roi[read],
                        &i->roi[0],
                        ceil(o->radius),
                        o->pairs);

      if (err) return FALSE;
335
    }
336

337 338 339
  return TRUE;
}

340
static void
341
gegl_op_class_init (GeglOpClass *klass)
342
{
343 344 345
  GeglOperationClass       *operation_class;
  GeglOperationFilterClass *filter_class;

346 347
  operation_class = GEGL_OPERATION_CLASS (klass);
  filter_class    = GEGL_OPERATION_FILTER_CLASS (klass);
348

349
  filter_class->process    = process;
350
  operation_class->prepare = prepare;
351

352 353
  operation_class->opencl_support = TRUE;

354 355
  gegl_operation_class_set_keys (operation_class,
    "name"       , "gegl:snn-mean",
356
    "categories" , "enhance:noise-reduction",
357
    "title",       _("Symmetric Nearest Neighbour"),
358
    "reference-hash", "dc342e01d1016f92f21d1b2196dc1a7f",
359
    "description",
360
        _("Noise reducing edge preserving blur filter based "
Anders Jonsson's avatar
Anders Jonsson committed
361
          "on Symmetric Nearest Neighbours"),
362
        NULL);
363 364 365
}

#endif