|
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 2007 Mukund Sivaraman <muks@mukund.org>
|
|
Packit Service |
2781ba |
*/
|
|
Packit Service |
2781ba |
|
|
Packit Service |
2781ba |
/* XXX
|
|
Packit Service |
2781ba |
* This plug-in isn't really useful apart for compatibility with GIMP
|
|
Packit Service |
2781ba |
* scripts, operating with HSV as a color model for filters isn't really
|
|
Packit Service |
2781ba |
* useful.
|
|
Packit Service |
2781ba |
*/
|
|
Packit Service |
2781ba |
|
|
Packit Service |
2781ba |
/*
|
|
Packit Service |
2781ba |
* The plug-in only does v = 1.0 - v; for each pixel in the image, or
|
|
Packit Service |
2781ba |
* each entry in the colormap depending upon the type of image, where 'v'
|
|
Packit Service |
2781ba |
* is the value in HSV color model.
|
|
Packit Service |
2781ba |
*
|
|
Packit Service |
2781ba |
* The plug-in code is optimized towards this, in that it is not a full
|
|
Packit Service |
2781ba |
* RGB->HSV->RGB transform, but shortcuts many of the calculations to
|
|
Packit Service |
2781ba |
* effectively only do v = 1.0 - v. In fact, hue is never calculated. The
|
|
Packit Service |
2781ba |
* shortcuts can be derived from running a set of r, g, b values through the
|
|
Packit Service |
2781ba |
* RGB->HSV transform and then from HSV->RGB and solving out the redundant
|
|
Packit Service |
2781ba |
* portions.
|
|
Packit Service |
2781ba |
*
|
|
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 |
/* no properties */
|
|
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 "value-invert.c"
|
|
Packit Service |
2781ba |
|
|
Packit Service |
2781ba |
#include "gegl-chant.h"
|
|
Packit Service |
2781ba |
|
|
Packit Service |
2781ba |
static void
|
|
Packit Service |
2781ba |
prepare (GeglOperation *operation)
|
|
Packit Service |
2781ba |
{
|
|
Packit Service |
2781ba |
gegl_operation_set_format (operation, "input", babl_format ("R'G'B'A float"));
|
|
Packit Service |
2781ba |
gegl_operation_set_format (operation, "output", babl_format ("R'G'B'A float"));
|
|
Packit Service |
2781ba |
}
|
|
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 samples,
|
|
Packit Service |
2781ba |
const GeglRectangle *roi,
|
|
Packit Service |
2781ba |
gint level)
|
|
Packit Service |
2781ba |
{
|
|
Packit Service |
2781ba |
glong j;
|
|
Packit Service |
2781ba |
gfloat *src = in_buf;
|
|
Packit Service |
2781ba |
gfloat *dest = out_buf;
|
|
Packit Service |
2781ba |
gfloat r, g, b;
|
|
Packit Service |
2781ba |
gfloat value, min;
|
|
Packit Service |
2781ba |
gfloat delta;
|
|
Packit Service |
2781ba |
|
|
Packit Service |
2781ba |
for (j = 0; j < samples; j++)
|
|
Packit Service |
2781ba |
{
|
|
Packit Service |
2781ba |
r = *src++;
|
|
Packit Service |
2781ba |
g = *src++;
|
|
Packit Service |
2781ba |
b = *src++;
|
|
Packit Service |
2781ba |
|
|
Packit Service |
2781ba |
if (r > g)
|
|
Packit Service |
2781ba |
{
|
|
Packit Service |
2781ba |
value = MAX (r, b);
|
|
Packit Service |
2781ba |
min = MIN (g, b);
|
|
Packit Service |
2781ba |
}
|
|
Packit Service |
2781ba |
else
|
|
Packit Service |
2781ba |
{
|
|
Packit Service |
2781ba |
value = MAX (g, b);
|
|
Packit Service |
2781ba |
min = MIN (r, b);
|
|
Packit Service |
2781ba |
}
|
|
Packit Service |
2781ba |
|
|
Packit Service |
2781ba |
delta = value - min;
|
|
Packit Service |
2781ba |
if ((value == 0) || (delta == 0))
|
|
Packit Service |
2781ba |
{
|
|
Packit Service |
2781ba |
r = 1.0 - value;
|
|
Packit Service |
2781ba |
g = 1.0 - value;
|
|
Packit Service |
2781ba |
b = 1.0 - value;
|
|
Packit Service |
2781ba |
}
|
|
Packit Service |
2781ba |
else
|
|
Packit Service |
2781ba |
{
|
|
Packit Service |
2781ba |
if (r == value)
|
|
Packit Service |
2781ba |
{
|
|
Packit Service |
2781ba |
r = 1.0 - r;
|
|
Packit Service |
2781ba |
b = r * b / value;
|
|
Packit Service |
2781ba |
g = r * g / value;
|
|
Packit Service |
2781ba |
}
|
|
Packit Service |
2781ba |
else if (g == value)
|
|
Packit Service |
2781ba |
{
|
|
Packit Service |
2781ba |
g = 1.0 - g;
|
|
Packit Service |
2781ba |
r = g * r / value;
|
|
Packit Service |
2781ba |
b = g * b / value;
|
|
Packit Service |
2781ba |
}
|
|
Packit Service |
2781ba |
else
|
|
Packit Service |
2781ba |
{
|
|
Packit Service |
2781ba |
b = 1.0 - b;
|
|
Packit Service |
2781ba |
g = b * g / value;
|
|
Packit Service |
2781ba |
r = b * r / value;
|
|
Packit Service |
2781ba |
}
|
|
Packit Service |
2781ba |
}
|
|
Packit Service |
2781ba |
|
|
Packit Service |
2781ba |
*dest++ = r;
|
|
Packit Service |
2781ba |
*dest++ = g;
|
|
Packit Service |
2781ba |
*dest++ = b;
|
|
Packit Service |
2781ba |
|
|
Packit Service |
2781ba |
*dest++ = *src++;
|
|
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_vinv(__global const float4 *in, \n"
|
|
Packit Service |
2781ba |
" __global float4 *out) \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 |
" \n"
|
|
Packit Service |
2781ba |
" float value = fmax (in_v.x, fmax (in_v.y, in_v.z)); \n"
|
|
Packit Service |
2781ba |
" float minv = fmin (in_v.x, fmin (in_v.y, in_v.z)); \n"
|
|
Packit Service |
2781ba |
" float delta = value - minv; \n"
|
|
Packit Service |
2781ba |
" \n"
|
|
Packit Service |
2781ba |
" if (value == 0.0f || delta == 0.0f) \n"
|
|
Packit Service |
2781ba |
" { \n"
|
|
Packit Service |
2781ba |
" out_v.xyz = (float3) (1.0f - value); \n"
|
|
Packit Service |
2781ba |
" } \n"
|
|
Packit Service |
2781ba |
" else \n"
|
|
Packit Service |
2781ba |
" { \n"
|
|
Packit Service |
2781ba |
" if (in_v.x == value) \n"
|
|
Packit Service |
2781ba |
" { \n"
|
|
Packit Service |
2781ba |
" out_v.xzy = (float3) ((1.0f - in_v.x), \n"
|
|
Packit Service |
2781ba |
" (1.0f - in_v.x) * in_v.z / value, \n"
|
|
Packit Service |
2781ba |
" (1.0f - in_v.x) * in_v.y / value); \n"
|
|
Packit Service |
2781ba |
" } \n"
|
|
Packit Service |
2781ba |
" else if (in_v.y == value) \n"
|
|
Packit Service |
2781ba |
" { \n"
|
|
Packit Service |
2781ba |
" out_v.yxz = (float3) ((1.0f - in_v.y), \n"
|
|
Packit Service |
2781ba |
" (1.0f - in_v.y) * in_v.x / value, \n"
|
|
Packit Service |
2781ba |
" (1.0f - in_v.y) * in_v.z / value); \n"
|
|
Packit Service |
2781ba |
" } \n"
|
|
Packit Service |
2781ba |
" else \n"
|
|
Packit Service |
2781ba |
" { \n"
|
|
Packit Service |
2781ba |
" out_v.zyx = (float3) ((1.0f - in_v.z), \n"
|
|
Packit Service |
2781ba |
" (1.0f - in_v.z) * in_v.y / value, \n"
|
|
Packit Service |
2781ba |
" (1.0f - in_v.z) * in_v.x / value); \n"
|
|
Packit Service |
2781ba |
" } \n"
|
|
Packit Service |
2781ba |
" } \n"
|
|
Packit Service |
2781ba |
" \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 |
int level)
|
|
Packit Service |
2781ba |
{
|
|
Packit Service |
2781ba |
|
|
Packit Service |
2781ba |
cl_int cl_err = 0;
|
|
Packit Service |
2781ba |
|
|
Packit Service |
2781ba |
if (!cl_data)
|
|
Packit Service |
2781ba |
{
|
|
Packit Service |
2781ba |
const char *kernel_name[] = {"kernel_vinv", 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 |
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 |
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 |
operation_class->prepare = prepare;
|
|
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:value-invert",
|
|
Packit Service |
2781ba |
"categories" , "color",
|
|
Packit Service |
2781ba |
"description",
|
|
Packit Service |
2781ba |
_("Inverts just the value component, the result is the corresponding "
|
|
Packit Service |
2781ba |
"`inverted' image."),
|
|
Packit Service |
2781ba |
NULL);
|
|
Packit Service |
2781ba |
}
|
|
Packit Service |
2781ba |
|
|
Packit Service |
2781ba |
#endif
|
|
Packit Service |
2781ba |
|