Blame operations/common/brightness-contrast.c

Packit bc1512
/* This file is an image processing operation for GEGL
Packit bc1512
 *
Packit bc1512
 * GEGL is free software; you can redistribute it and/or
Packit bc1512
 * modify it under the terms of the GNU Lesser General Public
Packit bc1512
 * License as published by the Free Software Foundation; either
Packit bc1512
 * version 3 of the License, or (at your option) any later version.
Packit bc1512
 *
Packit bc1512
 * GEGL is distributed in the hope that it will be useful,
Packit bc1512
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit bc1512
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit bc1512
 * Lesser General Public License for more details.
Packit bc1512
 *
Packit bc1512
 * You should have received a copy of the GNU Lesser General Public
Packit bc1512
 * License along with GEGL; if not, see <http://www.gnu.org/licenses/>.
Packit bc1512
 *
Packit bc1512
 * Copyright 2006 Øyvind Kolås <pippin@gimp.org>
Packit bc1512
 */
Packit bc1512
Packit bc1512
#include "config.h"
Packit bc1512
#include <glib/gi18n-lib.h>
Packit bc1512
Packit bc1512
/* Followed by this #if ... */
Packit bc1512
#ifdef GEGL_CHANT_PROPERTIES
Packit bc1512
/* ... are the properties of the filter, these are all scalar values
Packit bc1512
 * (doubles), the the parameters are:
Packit bc1512
 *       property name,   min,   max, default, "description of property"
Packit bc1512
 */
Packit bc1512
Packit bc1512
gegl_chant_double_ui (contrast, _("Contrast"), -5.0, 5.0, 1.0, 0.0, 2.0, 1.0,
Packit bc1512
                   _("Range scale factor"))
Packit bc1512
gegl_chant_double_ui (brightness, _("Brightness"), -3.0, 3.0, 0.0, -1.0, 1.0, 1.0,
Packit bc1512
                   _("Amount to increase brightness"))
Packit bc1512
Packit bc1512
/* this will create the instance structure for our use, and register the
Packit bc1512
 * property with the given ranges, default values and a comment for the
Packit bc1512
 * documentation/tooltip.
Packit bc1512
 */
Packit bc1512
#else
Packit bc1512
Packit bc1512
/* Specify the base class we're building our operation on, the base
Packit bc1512
 * class provides a lot of the legwork so we do not have to. For
Packit bc1512
 * brightness contrast the best base class is the POINT_FILTER base
Packit bc1512
 * class.
Packit bc1512
 */
Packit bc1512
#define GEGL_CHANT_TYPE_POINT_FILTER
Packit bc1512
Packit bc1512
/* We specify the file we're in, this is needed to make the code
Packit bc1512
 * generation for the properties work.
Packit bc1512
 */
Packit bc1512
#define GEGL_CHANT_C_FILE       "brightness-contrast.c"
Packit bc1512
Packit bc1512
/* Including gegl-chant.h creates most of the GObject boiler plate
Packit bc1512
 * needed, creating a GeglChant instance structure a GeglChantClass
Packit bc1512
 * structure for our operation, as well as the needed code to register
Packit bc1512
 * our new gobject with GEGL.
Packit bc1512
 */
Packit bc1512
#include "gegl-chant.h"
Packit bc1512
Packit bc1512
/* prepare() is called on each operation providing data to a node that
Packit bc1512
 * is requested to provide a rendered result. When prepare is called
Packit bc1512
 * all properties are known. For brightness contrast we use this
Packit bc1512
 * opportunity to dictate the formats of the input and output buffers.
Packit bc1512
 */
Packit bc1512
static void prepare (GeglOperation *operation)
Packit bc1512
{
Packit bc1512
  gegl_operation_set_format (operation, "input", babl_format ("RGBA float"));
Packit bc1512
  gegl_operation_set_format (operation, "output", babl_format ("RGBA float"));
Packit bc1512
}
Packit bc1512
Packit bc1512
/* For GeglOperationPointFilter subclasses, we operate on linear
Packit bc1512
 * buffers with a pixel count.
Packit bc1512
 */
Packit bc1512
static gboolean
Packit bc1512
process (GeglOperation       *op,
Packit bc1512
         void                *in_buf,
Packit bc1512
         void                *out_buf,
Packit bc1512
         glong                n_pixels,
Packit bc1512
         const GeglRectangle *roi,
Packit bc1512
         gint                 level)
Packit bc1512
{
Packit bc1512
  /* Retrieve a pointer to GeglChantO structure which contains all the
Packit bc1512
   * chanted properties
Packit bc1512
   */
Packit bc1512
  GeglChantO *o = GEGL_CHANT_PROPERTIES (op);
Packit bc1512
  gfloat     * GEGL_ALIGNED in_pixel;
Packit bc1512
  gfloat     * GEGL_ALIGNED out_pixel;
Packit bc1512
  gfloat      brightness, contrast;
Packit bc1512
  glong       i;
Packit bc1512
Packit bc1512
  in_pixel   = in_buf;
Packit bc1512
  out_pixel  = out_buf;
Packit bc1512
Packit bc1512
  brightness = o->brightness;
Packit bc1512
  contrast   = o->contrast;
Packit bc1512
Packit bc1512
  for (i=0; i
Packit bc1512
    {
Packit bc1512
      out_pixel[0] = (in_pixel[0] - 0.5f) * contrast + brightness + 0.5;
Packit bc1512
      out_pixel[1] = (in_pixel[1] - 0.5f) * contrast + brightness + 0.5;
Packit bc1512
      out_pixel[2] = (in_pixel[2] - 0.5f) * contrast + brightness + 0.5;
Packit bc1512
      out_pixel[3] = in_pixel[3]; /* copy the alpha */
Packit bc1512
      in_pixel  += 4;
Packit bc1512
      out_pixel += 4;
Packit bc1512
    }
Packit bc1512
  return TRUE;
Packit bc1512
}
Packit bc1512
Packit bc1512
#include "opencl/gegl-cl.h"
Packit bc1512
Packit bc1512
static const char* kernel_source =
Packit bc1512
"__kernel void kernel_bc(__global const float4     *in,         \n"
Packit bc1512
"                        __global       float4     *out,        \n"
Packit bc1512
"                        float brightness,                      \n"
Packit bc1512
"                        float contrast)                        \n"
Packit bc1512
"{                                                              \n"
Packit bc1512
"  int gid = get_global_id(0);                                  \n"
Packit bc1512
"  float4 in_v  = in[gid];                                      \n"
Packit bc1512
"  float4 out_v;                                                \n"
Packit bc1512
"  out_v.xyz = (in_v.xyz - 0.5f) * contrast + brightness + 0.5f;\n"
Packit bc1512
"  out_v.w   =  in_v.w;                                         \n"
Packit bc1512
"  out[gid]  =  out_v;                                          \n"
Packit bc1512
"}                                                              \n";
Packit bc1512
Packit bc1512
static gegl_cl_run_data *cl_data = NULL;
Packit bc1512
Packit bc1512
/* OpenCL processing function */
Packit bc1512
static cl_int
Packit bc1512
cl_process (GeglOperation       *op,
Packit bc1512
            cl_mem               in_tex,
Packit bc1512
            cl_mem               out_tex,
Packit bc1512
            size_t               global_worksize,
Packit bc1512
            const GeglRectangle *roi,
Packit bc1512
            int                  level)
Packit bc1512
{
Packit bc1512
  /* Retrieve a pointer to GeglChantO structure which contains all the
Packit bc1512
   * chanted properties
Packit bc1512
   */
Packit bc1512
Packit bc1512
  GeglChantO *o = GEGL_CHANT_PROPERTIES (op);
Packit bc1512
Packit bc1512
  gfloat brightness = o->brightness;
Packit bc1512
  gfloat contrast   = o->contrast;
Packit bc1512
Packit bc1512
  cl_int cl_err = 0;
Packit bc1512
Packit bc1512
  if (!cl_data)
Packit bc1512
    {
Packit bc1512
      const char *kernel_name[] = {"kernel_bc", NULL};
Packit bc1512
      cl_data = gegl_cl_compile_and_build (kernel_source, kernel_name);
Packit bc1512
    }
Packit bc1512
Packit bc1512
  if (!cl_data) return 1;
Packit bc1512
Packit bc1512
  cl_err |= gegl_clSetKernelArg(cl_data->kernel[0], 0, sizeof(cl_mem),   (void*)&in_tex);
Packit bc1512
  cl_err |= gegl_clSetKernelArg(cl_data->kernel[0], 1, sizeof(cl_mem),   (void*)&out_tex);
Packit bc1512
  cl_err |= gegl_clSetKernelArg(cl_data->kernel[0], 2, sizeof(cl_float), (void*)&brightness);
Packit bc1512
  cl_err |= gegl_clSetKernelArg(cl_data->kernel[0], 3, sizeof(cl_float), (void*)&contrast);
Packit bc1512
  if (cl_err != CL_SUCCESS) return cl_err;
Packit bc1512
Packit bc1512
  cl_err = gegl_clEnqueueNDRangeKernel(gegl_cl_get_command_queue (),
Packit bc1512
                                        cl_data->kernel[0], 1,
Packit bc1512
                                        NULL, &global_worksize, NULL,
Packit bc1512
                                        0, NULL, NULL);
Packit bc1512
  if (cl_err != CL_SUCCESS) return cl_err;
Packit bc1512
Packit bc1512
  return cl_err;
Packit bc1512
}
Packit bc1512
Packit bc1512
/*
Packit bc1512
 * The class init function sets up information needed for this operations class
Packit bc1512
 * (template) in the GObject OO framework.
Packit bc1512
 */
Packit bc1512
static void
Packit bc1512
gegl_chant_class_init (GeglChantClass *klass)
Packit bc1512
{
Packit bc1512
  GeglOperationClass            *operation_class;
Packit bc1512
  GeglOperationPointFilterClass *point_filter_class;
Packit bc1512
Packit bc1512
  operation_class    = GEGL_OPERATION_CLASS (klass);
Packit bc1512
  point_filter_class = GEGL_OPERATION_POINT_FILTER_CLASS (klass);
Packit bc1512
Packit bc1512
  /* override the prepare methods of the GeglOperation class */
Packit bc1512
  operation_class->prepare = prepare;
Packit bc1512
  /* override the process method of the point filter class (the process methods
Packit bc1512
   * of our superclasses deal with the handling on their level of abstraction)
Packit bc1512
   */
Packit bc1512
  point_filter_class->process = process;
Packit bc1512
  point_filter_class->cl_process = cl_process;
Packit bc1512
Packit bc1512
  /* specify the name this operation is found under in the GUI/when
Packit bc1512
   * programming/in XML
Packit bc1512
   */
Packit bc1512
  operation_class->opencl_support = TRUE;
Packit bc1512
Packit bc1512
  gegl_operation_class_set_keys (operation_class,
Packit bc1512
      "name",       "gegl:brightness-contrast",
Packit bc1512
      "categories", "color", 
Packit bc1512
      "description", _("Changes the light level and contrast."),
Packit bc1512
      NULL);
Packit bc1512
}
Packit bc1512
Packit bc1512
#endif /* closing #ifdef GEGL_CHANT_PROPERTIES ... else ... */