|
Packit Service |
2781ba |
/* This file is an image processing operation for GEGL
|
|
Packit Service |
2781ba |
*
|
|
Packit Service |
2781ba |
* GEGL is free software; you can redistribute it and/or
|
|
Packit Service |
2781ba |
* modify it under the terms of the GNU Lesser General Public
|
|
Packit Service |
2781ba |
* License as published by the Free Software Foundation; either
|
|
Packit Service |
2781ba |
* version 3 of the License, or (at your option) any later version.
|
|
Packit Service |
2781ba |
*
|
|
Packit Service |
2781ba |
* GEGL is distributed in the hope that it will be useful,
|
|
Packit Service |
2781ba |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
Packit Service |
2781ba |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
Packit Service |
2781ba |
* Lesser General Public License for more details.
|
|
Packit Service |
2781ba |
*
|
|
Packit Service |
2781ba |
* You should have received a copy of the GNU Lesser General Public
|
|
Packit Service |
2781ba |
* License along with GEGL; if not, see <http://www.gnu.org/licenses/>.
|
|
Packit Service |
2781ba |
*
|
|
Packit Service |
2781ba |
* Copyright 2005 Øyvind Kolås <pippin@gimp.org>,
|
|
Packit Service |
2781ba |
* 2007 Øyvind Kolås <oeyvindk@hig.no>
|
|
Packit Service |
2781ba |
*/
|
|
Packit Service |
2781ba |
|
|
Packit Service |
2781ba |
#include "config.h"
|
|
Packit Service |
2781ba |
#include <glib/gi18n-lib.h>
|
|
Packit Service |
2781ba |
|
|
Packit Service |
2781ba |
|
|
Packit Service |
2781ba |
#ifdef GEGL_CHANT_PROPERTIES
|
|
Packit Service |
2781ba |
|
|
Packit Service |
2781ba |
|
|
Packit Service |
2781ba |
gegl_chant_double_ui (blur_radius, _("Blur radius"), 0.0, 1000.0, 4.0, 0.0, 100.0, 1.5,
|
|
Packit Service |
2781ba |
_("Radius of square pixel region, (width and height will be radius*2+1)."))
|
|
Packit Service |
2781ba |
gegl_chant_double (edge_preservation, _("Edge preservation"), 0.0, 100.0, 8.0,
|
|
Packit Service |
2781ba |
_("Amount of edge preservation"))
|
|
Packit Service |
2781ba |
|
|
Packit Service |
2781ba |
#else
|
|
Packit Service |
2781ba |
|
|
Packit Service |
2781ba |
#define GEGL_CHANT_TYPE_AREA_FILTER
|
|
Packit Service |
2781ba |
#define GEGL_CHANT_C_FILE "bilateral-filter.c"
|
|
Packit Service |
2781ba |
|
|
Packit Service |
2781ba |
#include "gegl-chant.h"
|
|
Packit Service |
2781ba |
#include <math.h>
|
|
Packit Service |
2781ba |
|
|
Packit Service |
2781ba |
static void
|
|
Packit Service |
2781ba |
bilateral_filter (GeglBuffer *src,
|
|
Packit Service |
2781ba |
const GeglRectangle *src_rect,
|
|
Packit Service |
2781ba |
GeglBuffer *dst,
|
|
Packit Service |
2781ba |
const GeglRectangle *dst_rect,
|
|
Packit Service |
2781ba |
gdouble radius,
|
|
Packit Service |
2781ba |
gdouble preserve);
|
|
Packit Service |
2781ba |
|
|
Packit Service |
2781ba |
#include <stdio.h>
|
|
Packit Service |
2781ba |
|
|
Packit Service |
2781ba |
static void prepare (GeglOperation *operation)
|
|
Packit Service |
2781ba |
{
|
|
Packit Service |
2781ba |
GeglOperationAreaFilter *area = GEGL_OPERATION_AREA_FILTER (operation);
|
|
Packit Service |
2781ba |
GeglChantO *o = GEGL_CHANT_PROPERTIES (operation);
|
|
Packit Service |
2781ba |
|
|
Packit Service |
2781ba |
area->left = area->right = area->top = area->bottom = ceil (o->blur_radius);
|
|
Packit Service |
2781ba |
gegl_operation_set_format (operation, "input", babl_format ("RGBA float"));
|
|
Packit Service |
2781ba |
gegl_operation_set_format (operation, "output", babl_format ("RGBA float"));
|
|
Packit Service |
2781ba |
}
|
|
Packit Service |
2781ba |
|
|
Packit Service |
2781ba |
#include "opencl/gegl-cl.h"
|
|
Packit Service |
2781ba |
#include "buffer/gegl-buffer-cl-iterator.h"
|
|
Packit Service |
2781ba |
|
|
Packit Service |
2781ba |
static const char* kernel_source =
|
|
Packit Service |
2781ba |
"#define POW2(a) ((a) * (a)) \n"
|
|
Packit Service |
2781ba |
"kernel void bilateral_filter(global float4 *in, \n"
|
|
Packit Service |
2781ba |
" global float4 *out, \n"
|
|
Packit Service |
2781ba |
" const float radius, \n"
|
|
Packit Service |
2781ba |
" const float preserve) \n"
|
|
Packit Service |
2781ba |
"{ \n"
|
|
Packit Service |
2781ba |
" int gidx = get_global_id(0); \n"
|
|
Packit Service |
2781ba |
" int gidy = get_global_id(1); \n"
|
|
Packit Service |
2781ba |
" int n_radius = ceil(radius); \n"
|
|
Packit Service |
2781ba |
" int dst_width = get_global_size(0); \n"
|
|
Packit Service |
2781ba |
" int src_width = dst_width + n_radius * 2; \n"
|
|
Packit Service |
2781ba |
" \n"
|
|
Packit Service |
2781ba |
" int u, v, i, j; \n"
|
|
Packit Service |
2781ba |
" float4 center_pix = \n"
|
|
Packit Service |
2781ba |
" in[(gidy + n_radius) * src_width + gidx + n_radius]; \n"
|
|
Packit Service |
2781ba |
" float4 accumulated = 0.0f; \n"
|
|
Packit Service |
2781ba |
" float4 tempf = 0.0f; \n"
|
|
Packit Service |
2781ba |
" float count = 0.0f; \n"
|
|
Packit Service |
2781ba |
" float diff_map, gaussian_weight, weight; \n"
|
|
Packit Service |
2781ba |
" \n"
|
|
Packit Service |
2781ba |
" for (v = -n_radius;v <= n_radius; ++v) \n"
|
|
Packit Service |
2781ba |
" { \n"
|
|
Packit Service |
2781ba |
" for (u = -n_radius;u <= n_radius; ++u) \n"
|
|
Packit Service |
2781ba |
" { \n"
|
|
Packit Service |
2781ba |
" i = gidx + n_radius + u; \n"
|
|
Packit Service |
2781ba |
" j = gidy + n_radius + v; \n"
|
|
Packit Service |
2781ba |
" \n"
|
|
Packit Service |
2781ba |
" int gid1d = i + j * src_width; \n"
|
|
Packit Service |
2781ba |
" tempf = in[gid1d]; \n"
|
|
Packit Service |
2781ba |
" \n"
|
|
Packit Service |
2781ba |
" diff_map = exp ( \n"
|
|
Packit Service |
2781ba |
" - ( POW2(center_pix.x - tempf.x) \n"
|
|
Packit Service |
2781ba |
" + POW2(center_pix.y - tempf.y) \n"
|
|
Packit Service |
2781ba |
" + POW2(center_pix.z - tempf.z)) \n"
|
|
Packit Service |
2781ba |
" * preserve); \n"
|
|
Packit Service |
2781ba |
" \n"
|
|
Packit Service |
2781ba |
" gaussian_weight = \n"
|
|
Packit Service |
2781ba |
" exp( - 0.5f * (POW2(u) + POW2(v)) / radius); \n"
|
|
Packit Service |
2781ba |
" \n"
|
|
Packit Service |
2781ba |
" weight = diff_map * gaussian_weight; \n"
|
|
Packit Service |
2781ba |
" \n"
|
|
Packit Service |
2781ba |
" accumulated += tempf * weight; \n"
|
|
Packit Service |
2781ba |
" count += weight; \n"
|
|
Packit Service |
2781ba |
" } \n"
|
|
Packit Service |
2781ba |
" } \n"
|
|
Packit Service |
2781ba |
" out[gidx + gidy * dst_width] = accumulated / count; \n"
|
|
Packit Service |
2781ba |
"} \n";
|
|
Packit Service |
2781ba |
|
|
Packit Service |
2781ba |
static gegl_cl_run_data *cl_data = NULL;
|
|
Packit Service |
2781ba |
|
|
Packit Service |
2781ba |
static cl_int
|
|
Packit Service |
2781ba |
cl_bilateral_filter (cl_mem in_tex,
|
|
Packit Service |
2781ba |
cl_mem out_tex,
|
|
Packit Service |
2781ba |
size_t global_worksize,
|
|
Packit Service |
2781ba |
const GeglRectangle *roi,
|
|
Packit Service |
2781ba |
gfloat radius,
|
|
Packit Service |
2781ba |
gfloat preserve)
|
|
Packit Service |
2781ba |
{
|
|
Packit Service |
2781ba |
cl_int cl_err = 0;
|
|
Packit Service |
2781ba |
size_t global_ws[2];
|
|
Packit Service |
2781ba |
|
|
Packit Service |
2781ba |
if (!cl_data)
|
|
Packit Service |
2781ba |
{
|
|
Packit Service |
2781ba |
const char *kernel_name[] = {"bilateral_filter", NULL};
|
|
Packit Service |
2781ba |
cl_data = gegl_cl_compile_and_build (kernel_source, kernel_name);
|
|
Packit Service |
2781ba |
}
|
|
Packit Service |
2781ba |
|
|
Packit Service |
2781ba |
if (!cl_data) return 1;
|
|
Packit Service |
2781ba |
|
|
Packit Service |
2781ba |
global_ws[0] = roi->width;
|
|
Packit Service |
2781ba |
global_ws[1] = roi->height;
|
|
Packit Service |
2781ba |
|
|
Packit Service |
2781ba |
cl_err |= gegl_clSetKernelArg(cl_data->kernel[0], 0, sizeof(cl_mem), (void*)&in_tex);
|
|
Packit Service |
2781ba |
cl_err |= gegl_clSetKernelArg(cl_data->kernel[0], 1, sizeof(cl_mem), (void*)&out_tex);
|
|
Packit Service |
2781ba |
cl_err |= gegl_clSetKernelArg(cl_data->kernel[0], 2, sizeof(cl_float), (void*)&radius);
|
|
Packit Service |
2781ba |
cl_err |= gegl_clSetKernelArg(cl_data->kernel[0], 3, sizeof(cl_float), (void*)&preserve);
|
|
Packit Service |
2781ba |
if (cl_err != CL_SUCCESS) return cl_err;
|
|
Packit Service |
2781ba |
|
|
Packit Service |
2781ba |
cl_err = gegl_clEnqueueNDRangeKernel(gegl_cl_get_command_queue (),
|
|
Packit Service |
2781ba |
cl_data->kernel[0], 2,
|
|
Packit Service |
2781ba |
NULL, global_ws, NULL,
|
|
Packit Service |
2781ba |
0, NULL, NULL);
|
|
Packit Service |
2781ba |
if (cl_err != CL_SUCCESS) return cl_err;
|
|
Packit Service |
2781ba |
|
|
Packit Service |
2781ba |
return cl_err;
|
|
Packit Service |
2781ba |
}
|
|
Packit Service |
2781ba |
|
|
Packit Service |
2781ba |
static gboolean
|
|
Packit Service |
2781ba |
cl_process (GeglOperation *operation,
|
|
Packit Service |
2781ba |
GeglBuffer *input,
|
|
Packit Service |
2781ba |
GeglBuffer *output,
|
|
Packit Service |
2781ba |
const GeglRectangle *result)
|
|
Packit Service |
2781ba |
{
|
|
Packit Service |
2781ba |
const Babl *in_format = gegl_operation_get_format (operation, "input");
|
|
Packit Service |
2781ba |
const Babl *out_format = gegl_operation_get_format (operation, "output");
|
|
Packit Service |
2781ba |
gint err;
|
|
Packit Service |
2781ba |
gint j;
|
|
Packit Service |
2781ba |
cl_int cl_err;
|
|
Packit Service |
2781ba |
|
|
Packit Service |
2781ba |
GeglOperationAreaFilter *op_area = GEGL_OPERATION_AREA_FILTER (operation);
|
|
Packit Service |
2781ba |
GeglChantO *o = GEGL_CHANT_PROPERTIES (operation);
|
|
Packit Service |
2781ba |
|
|
Packit Service |
2781ba |
GeglBufferClIterator *i = gegl_buffer_cl_iterator_new (output, result, out_format, GEGL_CL_BUFFER_WRITE, GEGL_ABYSS_NONE);
|
|
Packit Service |
2781ba |
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);
|
|
Packit Service |
2781ba |
while (gegl_buffer_cl_iterator_next (i, &err))
|
|
Packit Service |
2781ba |
{
|
|
Packit Service |
2781ba |
if (err) return FALSE;
|
|
Packit Service |
2781ba |
for (j=0; j < i->n; j++)
|
|
Packit Service |
2781ba |
{
|
|
Packit Service |
2781ba |
cl_err = cl_bilateral_filter(i->tex[read][j], i->tex[0][j], i->size[0][j], &i->roi[0][j], ceil(o->blur_radius), o->edge_preservation);
|
|
Packit Service |
2781ba |
if (cl_err != CL_SUCCESS)
|
|
Packit Service |
2781ba |
{
|
|
Packit Service |
2781ba |
g_warning("[OpenCL] Error in gegl:bilateral-filter: %s", gegl_cl_errstring(cl_err));
|
|
Packit Service |
2781ba |
return FALSE;
|
|
Packit Service |
2781ba |
}
|
|
Packit Service |
2781ba |
}
|
|
Packit Service |
2781ba |
}
|
|
Packit Service |
2781ba |
return TRUE;
|
|
Packit Service |
2781ba |
}
|
|
Packit Service |
2781ba |
|
|
Packit Service |
2781ba |
static gboolean
|
|
Packit Service |
2781ba |
process (GeglOperation *operation,
|
|
Packit Service |
2781ba |
GeglBuffer *input,
|
|
Packit Service |
2781ba |
GeglBuffer *output,
|
|
Packit Service |
2781ba |
const GeglRectangle *result,
|
|
Packit Service |
2781ba |
gint level)
|
|
Packit Service |
2781ba |
{
|
|
Packit Service |
2781ba |
GeglChantO *o = GEGL_CHANT_PROPERTIES (operation);
|
|
Packit Service |
2781ba |
GeglRectangle compute;
|
|
Packit Service |
2781ba |
|
|
Packit Service |
2781ba |
if (o->blur_radius >= 1.0 && gegl_cl_is_accelerated ())
|
|
Packit Service |
2781ba |
if (cl_process (operation, input, output, result))
|
|
Packit Service |
2781ba |
return TRUE;
|
|
Packit Service |
2781ba |
|
|
Packit Service |
2781ba |
compute = gegl_operation_get_required_for_output (operation, "input",result);
|
|
Packit Service |
2781ba |
|
|
Packit Service |
2781ba |
if (o->blur_radius < 1.0)
|
|
Packit Service |
2781ba |
{
|
|
Packit Service |
2781ba |
output = g_object_ref (input);
|
|
Packit Service |
2781ba |
}
|
|
Packit Service |
2781ba |
else
|
|
Packit Service |
2781ba |
{
|
|
Packit Service |
2781ba |
bilateral_filter (input, &compute, output, result, o->blur_radius, o->edge_preservation);
|
|
Packit Service |
2781ba |
}
|
|
Packit Service |
2781ba |
|
|
Packit Service |
2781ba |
return TRUE;
|
|
Packit Service |
2781ba |
}
|
|
Packit Service |
2781ba |
|
|
Packit Service |
2781ba |
static void
|
|
Packit Service |
2781ba |
bilateral_filter (GeglBuffer *src,
|
|
Packit Service |
2781ba |
const GeglRectangle *src_rect,
|
|
Packit Service |
2781ba |
GeglBuffer *dst,
|
|
Packit Service |
2781ba |
const GeglRectangle *dst_rect,
|
|
Packit Service |
2781ba |
gdouble radius,
|
|
Packit Service |
2781ba |
gdouble preserve)
|
|
Packit Service |
2781ba |
{
|
|
Packit Service |
2781ba |
gfloat *gauss;
|
|
Packit Service |
2781ba |
gint x,y;
|
|
Packit Service |
2781ba |
gint offset;
|
|
Packit Service |
2781ba |
gfloat *src_buf;
|
|
Packit Service |
2781ba |
gfloat *dst_buf;
|
|
Packit Service |
2781ba |
gint width = (gint) radius * 2 + 1;
|
|
Packit Service |
2781ba |
gint iradius = radius;
|
|
Packit Service |
2781ba |
gint src_width = src_rect->width;
|
|
Packit Service |
2781ba |
gint src_height = src_rect->height;
|
|
Packit Service |
2781ba |
|
|
Packit Service |
2781ba |
gauss = g_newa (gfloat, width * width);
|
|
Packit Service |
2781ba |
src_buf = g_new0 (gfloat, src_rect->width * src_rect->height * 4);
|
|
Packit Service |
2781ba |
dst_buf = g_new0 (gfloat, dst_rect->width * dst_rect->height * 4);
|
|
Packit Service |
2781ba |
|
|
Packit Service |
2781ba |
gegl_buffer_get (src, src_rect, 1.0, babl_format ("RGBA float"), src_buf, GEGL_AUTO_ROWSTRIDE,
|
|
Packit Service |
2781ba |
GEGL_ABYSS_NONE);
|
|
Packit Service |
2781ba |
|
|
Packit Service |
2781ba |
offset = 0;
|
|
Packit Service |
2781ba |
|
|
Packit Service |
2781ba |
#define POW2(a) ((a)*(a))
|
|
Packit Service |
2781ba |
for (y=-iradius;y<=iradius;y++)
|
|
Packit Service |
2781ba |
for (x=-iradius;x<=iradius;x++)
|
|
Packit Service |
2781ba |
{
|
|
Packit Service |
2781ba |
gauss[x+(int)radius + (y+(int)radius)*width] = exp(- 0.5*(POW2(x)+POW2(y))/radius );
|
|
Packit Service |
2781ba |
}
|
|
Packit Service |
2781ba |
|
|
Packit Service |
2781ba |
for (y=0; y<dst_rect->height; y++)
|
|
Packit Service |
2781ba |
for (x=0; x<dst_rect->width; x++)
|
|
Packit Service |
2781ba |
{
|
|
Packit Service |
2781ba |
gint u,v;
|
|
Packit Service |
2781ba |
gfloat *center_pix = src_buf + ((x+iradius)+((y+iradius) * src_width)) * 4;
|
|
Packit Service |
2781ba |
gfloat accumulated[4]={0,0,0,0};
|
|
Packit Service |
2781ba |
gfloat count=0.0;
|
|
Packit Service |
2781ba |
|
|
Packit Service |
2781ba |
for (v=-iradius;v<=iradius;v++)
|
|
Packit Service |
2781ba |
for (u=-iradius;u<=iradius;u++)
|
|
Packit Service |
2781ba |
{
|
|
Packit Service |
2781ba |
gint i,j;
|
|
Packit Service |
2781ba |
i = x + radius + u;
|
|
Packit Service |
2781ba |
j = y + radius + v;
|
|
Packit Service |
2781ba |
if (i >= 0 && i < src_width &&
|
|
Packit Service |
2781ba |
j >= 0 && j < src_height)
|
|
Packit Service |
2781ba |
{
|
|
Packit Service |
2781ba |
gint c;
|
|
Packit Service |
2781ba |
|
|
Packit Service |
2781ba |
gfloat *src_pix = src_buf + (i + j * src_width) * 4;
|
|
Packit Service |
2781ba |
|
|
Packit Service |
2781ba |
gfloat diff_map = exp (- (POW2(center_pix[0] - src_pix[0])+
|
|
Packit Service |
2781ba |
POW2(center_pix[1] - src_pix[1])+
|
|
Packit Service |
2781ba |
POW2(center_pix[2] - src_pix[2])) * preserve
|
|
Packit Service |
2781ba |
);
|
|
Packit Service |
2781ba |
gfloat gaussian_weight;
|
|
Packit Service |
2781ba |
gfloat weight;
|
|
Packit Service |
2781ba |
|
|
Packit Service |
2781ba |
gaussian_weight = gauss[u+(int)radius+(v+(int)radius)*width];
|
|
Packit Service |
2781ba |
|
|
Packit Service |
2781ba |
weight = diff_map * gaussian_weight;
|
|
Packit Service |
2781ba |
|
|
Packit Service |
2781ba |
for (c=0;c<4;c++)
|
|
Packit Service |
2781ba |
{
|
|
Packit Service |
2781ba |
accumulated[c] += src_pix[c] * weight;
|
|
Packit Service |
2781ba |
}
|
|
Packit Service |
2781ba |
count += weight;
|
|
Packit Service |
2781ba |
}
|
|
Packit Service |
2781ba |
}
|
|
Packit Service |
2781ba |
|
|
Packit Service |
2781ba |
for (u=0; u<4;u++)
|
|
Packit Service |
2781ba |
dst_buf[offset*4+u] = accumulated[u]/count;
|
|
Packit Service |
2781ba |
offset++;
|
|
Packit Service |
2781ba |
}
|
|
Packit Service |
2781ba |
gegl_buffer_set (dst, dst_rect, 0, babl_format ("RGBA float"), dst_buf,
|
|
Packit Service |
2781ba |
GEGL_AUTO_ROWSTRIDE);
|
|
Packit Service |
2781ba |
g_free (src_buf);
|
|
Packit Service |
2781ba |
g_free (dst_buf);
|
|
Packit Service |
2781ba |
}
|
|
Packit Service |
2781ba |
|
|
Packit Service |
2781ba |
|
|
Packit Service |
2781ba |
static void
|
|
Packit Service |
2781ba |
gegl_chant_class_init (GeglChantClass *klass)
|
|
Packit Service |
2781ba |
{
|
|
Packit Service |
2781ba |
GeglOperationClass *operation_class;
|
|
Packit Service |
2781ba |
GeglOperationFilterClass *filter_class;
|
|
Packit Service |
2781ba |
|
|
Packit Service |
2781ba |
operation_class = GEGL_OPERATION_CLASS (klass);
|
|
Packit Service |
2781ba |
filter_class = GEGL_OPERATION_FILTER_CLASS (klass);
|
|
Packit Service |
2781ba |
|
|
Packit Service |
2781ba |
filter_class->process = process;
|
|
Packit Service |
2781ba |
operation_class->prepare = prepare;
|
|
Packit Service |
2781ba |
|
|
Packit Service |
2781ba |
operation_class->opencl_support = TRUE;
|
|
Packit Service |
2781ba |
|
|
Packit Service |
2781ba |
gegl_operation_class_set_keys (operation_class,
|
|
Packit Service |
2781ba |
"name", "gegl:bilateral-filter",
|
|
Packit Service |
2781ba |
"categories", "misc",
|
|
Packit Service |
2781ba |
"description",
|
|
Packit Service |
2781ba |
_("An edge preserving blur filter that can be used for noise reduction. "
|
|
Packit Service |
2781ba |
"It is a gaussian blur where the contribution of neighbourhood pixels "
|
|
Packit Service |
2781ba |
"are weighted by the color difference from the center pixel."),
|
|
Packit Service |
2781ba |
NULL);
|
|
Packit Service |
2781ba |
}
|
|
Packit Service |
2781ba |
|
|
Packit Service |
2781ba |
#endif
|