|
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 2006 Øyvind Kolås <pippin@gimp.org>
|
|
Packit Service |
2781ba |
*/
|
|
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 |
gegl_chant_double_ui (in_low, _("Low input"), -1.0, 4.0, 0.0, 0.0, 1.0, 1.0,
|
|
Packit Service |
2781ba |
_("Input luminance level to become lowest output"))
|
|
Packit Service |
2781ba |
gegl_chant_double_ui (in_high, _("High input"), -1.0, 4.0, 1.0, 0.0, 1.0, 1.0,
|
|
Packit Service |
2781ba |
_("Input luminance level to become white."))
|
|
Packit Service |
2781ba |
gegl_chant_double_ui (out_low, _("Low output"), -1.0, 4.0, 0.0, 0.0, 1.0, 1.0,
|
|
Packit Service |
2781ba |
_("Lowest luminance level in output"))
|
|
Packit Service |
2781ba |
gegl_chant_double_ui (out_high, _("High output"), -1.0, 4.0, 1.0, 0.0, 1.0, 1.0,
|
|
Packit Service |
2781ba |
_("Highest luminance level in output"))
|
|
Packit Service |
2781ba |
|
|
Packit Service |
2781ba |
#else
|
|
Packit Service |
2781ba |
|
|
Packit Service |
2781ba |
#define GEGL_CHANT_TYPE_POINT_FILTER
|
|
Packit Service |
2781ba |
#define GEGL_CHANT_C_FILE "levels.c"
|
|
Packit Service |
2781ba |
|
|
Packit Service |
2781ba |
#include "gegl-chant.h"
|
|
Packit Service |
2781ba |
|
|
Packit Service |
2781ba |
/* GeglOperationPointFilter gives us a linear buffer to operate on
|
|
Packit Service |
2781ba |
* in our requested pixel format
|
|
Packit Service |
2781ba |
*/
|
|
Packit Service |
2781ba |
static gboolean
|
|
Packit Service |
2781ba |
process (GeglOperation *op,
|
|
Packit Service |
2781ba |
void *in_buf,
|
|
Packit Service |
2781ba |
void *out_buf,
|
|
Packit Service |
2781ba |
glong n_pixels,
|
|
Packit Service |
2781ba |
const GeglRectangle *roi,
|
|
Packit Service |
2781ba |
gint level)
|
|
Packit Service |
2781ba |
{
|
|
Packit Service |
2781ba |
GeglChantO *o = GEGL_CHANT_PROPERTIES (op);
|
|
Packit Service |
2781ba |
gfloat *in_pixel;
|
|
Packit Service |
2781ba |
gfloat *out_pixel;
|
|
Packit Service |
2781ba |
gfloat in_range;
|
|
Packit Service |
2781ba |
gfloat out_range;
|
|
Packit Service |
2781ba |
gfloat in_offset;
|
|
Packit Service |
2781ba |
gfloat out_offset;
|
|
Packit Service |
2781ba |
gfloat scale;
|
|
Packit Service |
2781ba |
glong i;
|
|
Packit Service |
2781ba |
|
|
Packit Service |
2781ba |
in_pixel = in_buf;
|
|
Packit Service |
2781ba |
out_pixel = out_buf;
|
|
Packit Service |
2781ba |
|
|
Packit Service |
2781ba |
in_offset = o->in_low * 1.0;
|
|
Packit Service |
2781ba |
out_offset = o->out_low * 1.0;
|
|
Packit Service |
2781ba |
in_range = o->in_high-o->in_low;
|
|
Packit Service |
2781ba |
out_range = o->out_high-o->out_low;
|
|
Packit Service |
2781ba |
|
|
Packit Service |
2781ba |
if (in_range == 0.0)
|
|
Packit Service |
2781ba |
in_range = 0.00000001;
|
|
Packit Service |
2781ba |
|
|
Packit Service |
2781ba |
scale = out_range/in_range;
|
|
Packit Service |
2781ba |
|
|
Packit Service |
2781ba |
for (i=0; i
|
|
Packit Service |
2781ba |
{
|
|
Packit Service |
2781ba |
int c;
|
|
Packit Service |
2781ba |
for (c=0;c<3;c++)
|
|
Packit Service |
2781ba |
out_pixel[c] = (in_pixel[c]- in_offset) * scale + out_offset;
|
|
Packit Service |
2781ba |
out_pixel[3] = in_pixel[3];
|
|
Packit Service |
2781ba |
out_pixel += 4;
|
|
Packit Service |
2781ba |
in_pixel += 4;
|
|
Packit Service |
2781ba |
}
|
|
Packit Service |
2781ba |
return TRUE;
|
|
Packit Service |
2781ba |
}
|
|
Packit Service |
2781ba |
|
|
Packit Service |
2781ba |
#include "opencl/gegl-cl.h"
|
|
Packit Service |
2781ba |
|
|
Packit Service |
2781ba |
static const char* kernel_source =
|
|
Packit Service |
2781ba |
"__kernel void kernel_levels(__global const float4 *in, \n"
|
|
Packit Service |
2781ba |
" __global float4 *out, \n"
|
|
Packit Service |
2781ba |
" float in_offset, \n"
|
|
Packit Service |
2781ba |
" float out_offset, \n"
|
|
Packit Service |
2781ba |
" float scale) \n"
|
|
Packit Service |
2781ba |
"{ \n"
|
|
Packit Service |
2781ba |
" int gid = get_global_id(0); \n"
|
|
Packit Service |
2781ba |
" float4 in_v = in[gid]; \n"
|
|
Packit Service |
2781ba |
" float4 out_v; \n"
|
|
Packit Service |
2781ba |
" out_v.xyz = (in_v.xyz - in_offset) * scale + out_offset; \n"
|
|
Packit Service |
2781ba |
" out_v.w = in_v.w; \n"
|
|
Packit Service |
2781ba |
" out[gid] = out_v; \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 |
/* OpenCL processing function */
|
|
Packit Service |
2781ba |
static cl_int
|
|
Packit Service |
2781ba |
cl_process (GeglOperation *op,
|
|
Packit Service |
2781ba |
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 |
gint level)
|
|
Packit Service |
2781ba |
{
|
|
Packit Service |
2781ba |
/* Retrieve a pointer to GeglChantO structure which contains all the
|
|
Packit Service |
2781ba |
* chanted properties
|
|
Packit Service |
2781ba |
*/
|
|
Packit Service |
2781ba |
|
|
Packit Service |
2781ba |
GeglChantO *o = GEGL_CHANT_PROPERTIES (op);
|
|
Packit Service |
2781ba |
|
|
Packit Service |
2781ba |
gfloat in_range;
|
|
Packit Service |
2781ba |
gfloat out_range;
|
|
Packit Service |
2781ba |
gfloat in_offset;
|
|
Packit Service |
2781ba |
gfloat out_offset;
|
|
Packit Service |
2781ba |
gfloat scale;
|
|
Packit Service |
2781ba |
|
|
Packit Service |
2781ba |
cl_int cl_err = 0;
|
|
Packit Service |
2781ba |
|
|
Packit Service |
2781ba |
in_offset = o->in_low * 1.0;
|
|
Packit Service |
2781ba |
out_offset = o->out_low * 1.0;
|
|
Packit Service |
2781ba |
in_range = o->in_high-o->in_low;
|
|
Packit Service |
2781ba |
out_range = o->out_high-o->out_low;
|
|
Packit Service |
2781ba |
|
|
Packit Service |
2781ba |
if (in_range == 0.0)
|
|
Packit Service |
2781ba |
in_range = 0.00000001;
|
|
Packit Service |
2781ba |
|
|
Packit Service |
2781ba |
scale = out_range/in_range;
|
|
Packit Service |
2781ba |
|
|
Packit Service |
2781ba |
if (!cl_data)
|
|
Packit Service |
2781ba |
{
|
|
Packit Service |
2781ba |
const char *kernel_name[] = {"kernel_levels", NULL};
|
|
Packit Service |
2781ba |
cl_data = gegl_cl_compile_and_build (kernel_source, kernel_name);
|
|
Packit Service |
2781ba |
}
|
|
Packit Service |
2781ba |
if (!cl_data) return 1;
|
|
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*)&in_offset);
|
|
Packit Service |
2781ba |
cl_err |= gegl_clSetKernelArg(cl_data->kernel[0], 3, sizeof(cl_float), (void*)&out_offset);
|
|
Packit Service |
2781ba |
cl_err |= gegl_clSetKernelArg(cl_data->kernel[0], 4, sizeof(cl_float), (void*)&scale);
|
|
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], 1,
|
|
Packit Service |
2781ba |
NULL, &global_worksize, 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 |
|
|
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 |
GeglOperationPointFilterClass *point_filter_class;
|
|
Packit Service |
2781ba |
|
|
Packit Service |
2781ba |
operation_class = GEGL_OPERATION_CLASS (klass);
|
|
Packit Service |
2781ba |
point_filter_class = GEGL_OPERATION_POINT_FILTER_CLASS (klass);
|
|
Packit Service |
2781ba |
|
|
Packit Service |
2781ba |
point_filter_class->process = process;
|
|
Packit Service |
2781ba |
point_filter_class->cl_process = cl_process;
|
|
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:levels",
|
|
Packit Service |
2781ba |
"categories" , "color",
|
|
Packit Service |
2781ba |
"description", _("Remaps the intensity range of the image"),
|
|
Packit Service |
2781ba |
NULL);
|
|
Packit Service |
2781ba |
}
|
|
Packit Service |
2781ba |
|
|
Packit Service |
2781ba |
#endif
|