box-blur.c 13.1 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
 *
 * Copyright 2006 Øyvind Kolås <pippin@gimp.org>
17
 *           2012 Pavel Roschin <roshin@scriptumplus.ru>
18
 */
19 20 21 22

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

23
#ifdef GEGL_PROPERTIES
24

25 26 27 28 29
property_int (radius, _("Radius"), 4)
   description(_("Radius of square pixel region, (width and height will be radius*2+1)"))
   value_range (0, 1000)
   ui_range    (0, 100)
   ui_gamma   (1.5)
30 31 32

#else

33
#define GEGL_OP_AREA_FILTER
34
#define GEGL_OP_NAME     box_blur
35
#define GEGL_OP_C_SOURCE box-blur.c
36

37
#include "gegl-op.h"
38 39
#include <stdio.h>
#include <math.h>
40

41
#define SRC_OFFSET (row + u + radius * 2) * 4
42

43
static void
44 45 46 47
hor_blur (GeglBuffer          *src,
          const GeglRectangle *src_rect,
          GeglBuffer          *dst,
          const GeglRectangle *dst_rect,
48 49
          gint                 radius,
          const Babl          *format)
50 51
{
  gint u,v;
52
  gint i;
53
  gint offset;
54 55 56 57
  gint src_offset;
  gint prev_rad = radius * 4 + 4;
  gint next_rad = radius * 4;
  gint row;
58 59
  gfloat *src_buf;
  gfloat *dst_buf;
60
  gfloat rad1 = 1.0 / (gfloat)(radius * 2 + 1);
61

62 63
  src_buf = g_new0 (gfloat, src_rect->width * src_rect->height * 4);
  dst_buf = g_new0 (gfloat, dst_rect->width * dst_rect->height * 4);
64

65
  gegl_buffer_get (src, src_rect, 1.0, format,
66
                   src_buf, GEGL_AUTO_ROWSTRIDE, GEGL_ABYSS_CLAMP);
67 68

  offset = 0;
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
  for (v = 0; v < dst_rect->height; v++)
    {
      /* here just radius, not radius * 2 as in ver_blur beacuse
       * we enlarged dst_buf by y earlier */
      row = (v + radius) * src_rect->width;
      /* prepare - set first column of pixels */
      for (u = -radius; u <= radius; u++)
        {
          src_offset = SRC_OFFSET;
          for (i = 0; i < 4; i++)
            dst_buf[offset + i] += src_buf[src_offset + i] * rad1;
        }
      offset += 4;
      /* iterate other pixels by moving a window - very fast */
      for (u = 1; u < dst_rect->width; u++)
        {
          src_offset = SRC_OFFSET;
          for (i = 0; i < 4; i++)
          {
            dst_buf[offset] = dst_buf[offset - 4]
                            - src_buf[src_offset - prev_rad] * rad1
                            + src_buf[src_offset + next_rad] * rad1;
            src_offset++;
            offset++;
          }
        }
    }

97
  gegl_buffer_set (dst, dst_rect, 0, format,
98 99
                   dst_buf, GEGL_AUTO_ROWSTRIDE);

100 101 102 103 104
  g_free (src_buf);
  g_free (dst_buf);
}

static void
105 106 107 108
ver_blur (GeglBuffer          *src,
          const GeglRectangle *src_rect,
          GeglBuffer          *dst,
          const GeglRectangle *dst_rect,
109 110
          gint                 radius,
          const Babl          *format)
111
{
112 113
  gint u, v;
  gint i;
114
  gint offset;
115 116 117 118
  gint src_offset;
  gint prev_rad = (radius * 4 + 4) * src_rect->width;
  gint next_rad = (radius * 4) * src_rect->width;
  gint row;
119 120
  gfloat *src_buf;
  gfloat *dst_buf;
121
  gfloat rad1 = 1.0 / (gfloat)(radius * 2 + 1);
122

123 124
  src_buf = g_new0 (gfloat, src_rect->width * src_rect->height * 4);
  dst_buf = g_new0 (gfloat, dst_rect->width * dst_rect->height * 4);
125

126
  gegl_buffer_get (src, src_rect, 1.0, format,
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
                   src_buf, GEGL_AUTO_ROWSTRIDE, GEGL_ABYSS_CLAMP);

  /* prepare: set first row of pixels */
  for (v = -radius; v <= radius; v++)
    {
      row = (v + radius * 2) * src_rect->width;
      for (u = 0; u < dst_rect->width; u++)
        {
          src_offset = SRC_OFFSET;
          for (i = 0; i < 4; i++)
            dst_buf[u * 4 + i] += src_buf[src_offset + i] * rad1;
        }
    }
  /* skip first row */
  offset = dst_rect->width * 4;
  for (v = 1; v < dst_rect->height; v++)
    {
      row = (v + radius * 2) * src_rect->width;
      for (u = 0; u < dst_rect->width; u++)
        {
          src_offset = SRC_OFFSET;
          for (i = 0; i < 4; i++)
          {
            dst_buf[offset] = dst_buf[offset - 4 * dst_rect->width]
                            - src_buf[src_offset - prev_rad] * rad1
                            + src_buf[src_offset + next_rad] * rad1;
            src_offset++;
            offset++;
          }
        }
    }

159
  gegl_buffer_set (dst, dst_rect, 0, format,
160 161
                   dst_buf, GEGL_AUTO_ROWSTRIDE);

162 163 164 165
  g_free (src_buf);
  g_free (dst_buf);
}

166 167
#undef SRC_OFFSET

168 169
static void prepare (GeglOperation *operation)
{
170
  GeglProperties              *o;
171
  GeglOperationAreaFilter *op_area;
172 173
  const Babl *space = gegl_operation_get_source_space (operation, "input");
  const Babl *format = babl_format_with_space ("RaGaBaA float", space);
174 175

  op_area = GEGL_OPERATION_AREA_FILTER (operation);
176
  o       = GEGL_PROPERTIES (operation);
177 178 179 180

  op_area->left   =
  op_area->right  =
  op_area->top    =
181
  op_area->bottom = o->radius;
Kevin Cozens's avatar
Kevin Cozens committed
182

183 184
  gegl_operation_set_format (operation, "input",  format);
  gegl_operation_set_format (operation, "output", format);
185 186 187
}

#include "opencl/gegl-cl.h"
188
#include "gegl-buffer-cl-iterator.h"
189

190
#include "opencl/box-blur.cl.h"
191
static GeglClRunData *cl_data = NULL;
Victor Oliveira's avatar
Victor Oliveira committed
192
static gboolean
193
cl_box_blur (cl_mem                in_tex,
194
             cl_mem                aux_tex,
195 196 197 198 199 200
             cl_mem                out_tex,
             size_t                global_worksize,
             const GeglRectangle  *roi,
             gint                  radius)
{
  cl_int cl_err = 0;
201
  
202 203
  if (!cl_data)
    {
204
      const char *kernel_name[] = { "kernel_blur_hor", "kernel_blur_ver","kernel_box_blur_fast", NULL};
205
      cl_data = gegl_cl_compile_and_build (box_blur_cl_source, kernel_name);
206
    }
207 208 209

  if (!cl_data)
    return TRUE;
210

211 212 213
#if 0
  /* XXX: this code path is commented out, because it introduces vertical stripes/gaps,
          wheras the generic code path does not.  */
214 215
  if( radius <=110 )
  {
216 217 218 219 220
    size_t global_ws[2];
    local_ws[2];
    size_t step_size = 64;
    local_ws[0]=256;
    local_ws[1]=1;
221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238
    global_ws[0] = (roi->width + local_ws[0] - 2 * radius - 1) / ( local_ws[0] - 2 * radius ) * local_ws[0];
    global_ws[1] = (roi->height + step_size - 1) / step_size;
    cl_err = gegl_cl_set_kernel_args(cl_data->kernel[2],
                                     sizeof(cl_mem), (void *)&in_tex,
                                     sizeof(cl_mem), (void *)&out_tex,
                                     sizeof(cl_float4)*local_ws[0], (void *)NULL,
                                     sizeof(cl_int), (void *)&roi->width,
                                     sizeof(cl_int), (void *)&roi->height,
                                     sizeof(cl_int), (void *)&radius,
                                     sizeof(cl_int), (void *)&step_size, NULL);
    CL_CHECK;
    cl_err = gegl_clEnqueueNDRangeKernel(gegl_cl_get_command_queue(),
                                         cl_data->kernel[2], 2,
                                         NULL, global_ws, local_ws, 0, NULL, NULL );
       CL_CHECK;

  }
  else
239
#endif
240
  {
241 242 243
    size_t global_ws_hor[2], global_ws_ver[2];
    size_t local_ws_hor[2], local_ws_ver[2];

244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282
    local_ws_hor[0] = 1;
    local_ws_hor[1] = 256;
    global_ws_hor[0] = roi->height + 2 * radius;
    global_ws_hor[1] = ((roi->width + local_ws_hor[1] -1)/local_ws_hor[1]) * local_ws_hor[1];

    local_ws_ver[0] = 1;
    local_ws_ver[1] = 256;
    global_ws_ver[0] = roi->height;
    global_ws_ver[1] = ((roi->width + local_ws_ver[1] -1)/local_ws_ver[1]) * local_ws_ver[1];


    cl_err = gegl_cl_set_kernel_args (cl_data->kernel[0],
                                      sizeof(cl_mem), (void*)&in_tex,
                                      sizeof(cl_mem), (void*)&aux_tex,
                                      sizeof(cl_int), (void*)&roi->width,
                                      sizeof(cl_int), (void*)&radius,
                                      NULL);
    CL_CHECK;
    cl_err = gegl_clEnqueueNDRangeKernel (gegl_cl_get_command_queue (),
                                          cl_data->kernel[0], 2,
                                          NULL, global_ws_hor, local_ws_hor,
                                          0, NULL, NULL);
    CL_CHECK;


    cl_err = gegl_cl_set_kernel_args (cl_data->kernel[1],
                                      sizeof(cl_mem), (void*)&aux_tex,
                                      sizeof(cl_mem), (void*)&out_tex,
                                      sizeof(cl_int), (void*)&roi->width,
                                      sizeof(cl_int), (void*)&radius,
                                      NULL);
    CL_CHECK;

    cl_err = gegl_clEnqueueNDRangeKernel (gegl_cl_get_command_queue (),
                                          cl_data->kernel[1], 2,
                                          NULL, global_ws_ver, local_ws_ver,
                                          0, NULL, NULL);
    CL_CHECK;
  }
283

Victor Oliveira's avatar
Victor Oliveira committed
284 285 286 287
  return FALSE;

error:
  return TRUE;
288 289 290 291 292 293 294 295 296 297
}

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");
298

299
  gint err = 0;
300 301

  GeglOperationAreaFilter *op_area = GEGL_OPERATION_AREA_FILTER (operation);
302
  GeglProperties *o = GEGL_PROPERTIES (operation);
303

304 305 306 307 308 309 310 311 312 313 314 315 316 317
  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,
318
                                             GEGL_ABYSS_CLAMP);
319

320 321 322 323 324 325 326
  gint aux = gegl_buffer_cl_iterator_add_aux (i,
                                              result,
                                              in_format,
                                              0,
                                              0,
                                              op_area->top,
                                              op_area->bottom);
327

328
  while (gegl_buffer_cl_iterator_next (i, &err) && !err)
329
    {
330 331 332 333 334
      err = cl_box_blur (i->tex[read],
                         i->tex[aux],
                         i->tex[0],
                         i->size[0],
                         &i->roi[0],
335
                         o->radius);
336 337 338 339 340 341

      if (err)
        {
          gegl_buffer_cl_iterator_stop (i);
          break;
        }
342
    }
343

344
  return !err;
345 346 347 348 349 350
}

static gboolean
process (GeglOperation       *operation,
         GeglBuffer          *input,
         GeglBuffer          *output,
351 352
         const GeglRectangle *result,
         gint                 level)
353
{
354
  GeglRectangle rect;
355
  GeglRectangle tmprect;
356
  GeglProperties *o = GEGL_PROPERTIES (operation);
357
  GeglBuffer *temp;
358
  GeglOperationAreaFilter *op_area;
359
  const Babl *out_format = gegl_operation_get_format (operation, "output");
360

361 362
  op_area = GEGL_OPERATION_AREA_FILTER (operation);

363 364 365
  if (gegl_operation_use_opencl (operation))
    if (cl_process (operation, input, output, result))
      return TRUE;
366

367
  rect = *result;
368
  tmprect = *result;
369

370 371 372 373 374 375 376
  rect.x       -= op_area->left * 2;
  rect.y       -= op_area->top * 2;
  rect.width   += (op_area->left + op_area->right) * 2;
  rect.height  += (op_area->top + op_area->bottom) * 2;
  /* very tricky: enlarge temp buffer to avoid seams in second pass */
  tmprect.y      -= o->radius;
  tmprect.height += o->radius * 2;
377

378
  temp  = gegl_buffer_new (&tmprect, out_format);
379

380
  /* doing second pass in separate gegl op may be significantly faster */
381 382
  hor_blur (input, &rect, temp, &tmprect, o->radius, out_format);
  ver_blur (temp, &rect, output, result, o->radius, out_format);
383 384 385 386

  g_object_unref (temp);
  return  TRUE;
}
387

388 389

static void
390
gegl_op_class_init (GeglOpClass *klass)
391 392 393 394
{
  GeglOperationClass       *operation_class;
  GeglOperationFilterClass *filter_class;

395 396
  operation_class = GEGL_OPERATION_CLASS (klass);
  filter_class    = GEGL_OPERATION_FILTER_CLASS (klass);
397

398
  filter_class->process    = process;
399
  operation_class->prepare = prepare;
400

Victor Oliveira's avatar
Victor Oliveira committed
401 402
  operation_class->opencl_support = TRUE;

403 404
  gegl_operation_class_set_keys (operation_class,
      "name",        "gegl:box-blur",
405
      "title",       _("Box Blur"),
406
      "categories",  "blur",
407
      "description", _("Blur resulting from averaging the colors of a square neighbourhood."),
408
      NULL);
409 410
}

411
#endif