snn-mean.c 10.4 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 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 40
#define GEGL_OP_AREA_FILTER
#define GEGL_OP_C_FILE       "snn-mean.c"
41

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

static void
46 47 48 49 50
snn_mean (GeglBuffer          *src,
          GeglBuffer          *dst,
          const GeglRectangle *dst_rect,
          gdouble              radius,
          gint                 pairs);
51 52


53
static void prepare (GeglOperation *operation)
54 55
{
  GeglOperationAreaFilter *area = GEGL_OPERATION_AREA_FILTER (operation);
56
  GeglProperties          *o    = GEGL_PROPERTIES (operation);
57 58

  area->left = area->right = area->top = area->bottom = ceil (o->radius);
59 60
  gegl_operation_set_format (operation, "input", babl_format ("RGBA float"));
  gegl_operation_set_format (operation, "output", babl_format ("RGBA float"));
61 62
}

63 64 65 66 67 68
static gboolean
cl_process (GeglOperation       *operation,
            GeglBuffer          *input,
            GeglBuffer          *output,
            const GeglRectangle *result);

69
static gboolean
70
process (GeglOperation       *operation,
71 72
         GeglBuffer          *input,
         GeglBuffer          *output,
73 74
         const GeglRectangle *result,
         gint                 level)
75
{
76
  GeglProperties      *o = GEGL_PROPERTIES (operation);
77 78
  GeglBuffer          *temp_in;
  GeglRectangle        compute;
79

80
  if (gegl_operation_use_opencl (operation))
81 82 83
    if (cl_process (operation, input, output, result))
      return TRUE;

84
  compute = gegl_operation_get_required_for_output (operation, "input", result);
85

86
  if (o->radius < 1.0)
87
    {
88 89 90 91 92 93
      output = g_object_ref (input);
    }
  else
    {
      temp_in = gegl_buffer_create_sub_buffer (input, &compute);

94
      snn_mean (temp_in, output, result, o->radius, o->pairs);
95
      g_object_unref (temp_in);
96
    }
97

98 99 100 101 102 103 104 105 106 107 108 109 110 111 112
  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
113 114 115 116 117
snn_mean (GeglBuffer          *src,
          GeglBuffer          *dst,
          const GeglRectangle *dst_rect,
          gdouble              dradius,
          gint                 pairs)
118 119 120 121 122
{
  gint x,y;
  gint offset;
  gfloat *src_buf;
  gfloat *dst_buf;
123 124 125
  gint radius = dradius;
  gint src_width = gegl_buffer_get_width (src);
  gint src_height = gegl_buffer_get_height (src);
126

127
  src_buf = g_new0 (gfloat, gegl_buffer_get_pixel_count (src) * 4);
128
  dst_buf = g_new0 (gfloat, dst_rect->width * dst_rect->height * 4);
129

130 131
  gegl_buffer_get (src, NULL, 1.0, babl_format ("RGBA float"), src_buf, 
                   GEGL_AUTO_ROWSTRIDE, GEGL_ABYSS_NONE);
132 133 134

  offset = 0;

135
  for (y=0; y<dst_rect->height; y++)
136 137
    {
      gfloat *center_pix;
138

139 140
      center_pix = src_buf + ((radius) + (y+radius)* src_width)*4;

141
      for (x=0; x<dst_rect->width; x++)
142 143 144
        {
          gint u,v;

145
          gfloat  accumulated[4]={0.0f, 0.0f, 0.0f, 0.0f};
146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162
          gint    count=0;

          /* iterate through the upper left quater of pixels */
          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
                     */
163 164 165 166 167 168 169 170 171 172
                    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;
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

                    /* check which member of the symmetric quadruple to use */
                    for (i=0;i<pairs*2;i++)
                      {
                        if (xs[i] >= 0 && xs[i] < src_width &&
                            ys[i] >= 0 && ys[i] < src_height)
                          {
                            gfloat *tpix = src_buf + (xs[i]+ys[i]* src_width)*4;
                            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;
        }
    }
210
  gegl_buffer_set (dst, dst_rect, 0, babl_format ("RGBA float"), dst_buf,
211
                   GEGL_AUTO_ROWSTRIDE);
212 213 214 215
  g_free (src_buf);
  g_free (dst_buf);
}

216

217 218 219
#include "opencl/gegl-cl.h"
#include "buffer/gegl-buffer-cl-iterator.h"

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

222
static GeglClRunData *cl_data = NULL;
223

Victor Oliveira's avatar
Victor Oliveira committed
224
static gboolean
225 226 227 228 229 230 231 232 233 234 235 236
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
237 238
      const char *kernel_name[] = {"snn_mean", NULL};
      cl_data = gegl_cl_compile_and_build (snn_mean_cl_source, kernel_name);
239
    }
Victor Oliveira's avatar
Victor Oliveira committed
240
  if (!cl_data) return TRUE;
241 242 243 244 245


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

Victor Oliveira's avatar
Victor Oliveira committed
246 247 248 249 250 251 252 253 254 255 256 257
  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;
258 259 260 261 262

  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
263
  CL_CHECK;
264

Victor Oliveira's avatar
Victor Oliveira committed
265 266 267 268
  return FALSE;

error:
  return TRUE;
269 270 271 272 273 274 275 276 277 278 279 280 281
}

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);
282
  GeglProperties *o = GEGL_PROPERTIES (operation);
283

284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299
  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);

300 301 302
  while (gegl_buffer_cl_iterator_next (i, &err))
    {
      if (err) return FALSE;
303 304 305 306 307 308 309 310 311

      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;
312
    }
313

314 315 316
  return TRUE;
}

317
static void
318
gegl_op_class_init (GeglOpClass *klass)
319
{
320 321 322
  GeglOperationClass       *operation_class;
  GeglOperationFilterClass *filter_class;

323 324
  operation_class = GEGL_OPERATION_CLASS (klass);
  filter_class    = GEGL_OPERATION_FILTER_CLASS (klass);
325

326
  filter_class->process    = process;
327
  operation_class->prepare = prepare;
328

329 330
  operation_class->opencl_support = TRUE;

331 332
  gegl_operation_class_set_keys (operation_class,
    "name"       , "gegl:snn-mean",
333
    "categories" , "enhance:noise-reduction",
334
    "title",       _("Symmetric Nearest Neighbour"),
335
    "description",
336
        _("Noise reducing edge preserving blur filter based "
337 338
          " on Symmetric Nearest Neighbours"),
        NULL);
339 340 341
}

#endif