Blob Blame History Raw
<!DOCTYPE html PUBLIC "-//IETF//DTD HTML 2.0//EN">
<HTML>
<HEAD>
<TITLE>Enscript Output</TITLE>
</HEAD>
<BODY BGCOLOR="#FFFFFF" TEXT="#000000" LINK="#1F00FF" ALINK="#FF0000" VLINK="#9900DD">
<A NAME="top">
<A NAME="file1">
<H1>../operations/common/brightness-contrast.c</H1>

<PRE>
<I><FONT COLOR="#B22222">/* 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
 * version 3 of the License, or (at your option) any later version.
 *
 * 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
 * License along with GEGL; if not, see &lt;http://www.gnu.org/licenses/&gt;.
 *
 * Copyright 2006 Øyvind Kolås &lt;pippin@gimp.org&gt;
 */</FONT></I>

#<B><FONT COLOR="#5F9EA0">include</FONT></B> <B><FONT COLOR="#BC8F8F">&quot;config.h&quot;</FONT></B>
#<B><FONT COLOR="#5F9EA0">include</FONT></B> <B><FONT COLOR="#BC8F8F">&lt;glib/gi18n-lib.h&gt;</FONT></B>

<I><FONT COLOR="#B22222">/* Followed by this #if ... */</FONT></I>
#<B><FONT COLOR="#5F9EA0">ifdef</FONT></B> <FONT COLOR="#B8860B">GEGL_CHANT_PROPERTIES</FONT>
<I><FONT COLOR="#B22222">/* ... are the properties of the filter, these are all scalar values
 * (doubles), the the parameters are:
 *       property name,   min,   max, default, &quot;description of property&quot;
 */</FONT></I>

<B><FONT COLOR="#0000FF">gegl_chant_double_ui</FONT></B> (contrast, _(<B><FONT COLOR="#BC8F8F">&quot;Contrast&quot;</FONT></B>), -5.0, 5.0, 1.0, 0.0, 2.0, 1.0,
                   _(<B><FONT COLOR="#BC8F8F">&quot;Range scale factor&quot;</FONT></B>))
<B><FONT COLOR="#0000FF">gegl_chant_double_ui</FONT></B> (brightness, _(<B><FONT COLOR="#BC8F8F">&quot;Brightness&quot;</FONT></B>), -3.0, 3.0, 0.0, -1.0, 1.0, 1.0,
                   _(<B><FONT COLOR="#BC8F8F">&quot;Amount to increase brightness&quot;</FONT></B>))

<I><FONT COLOR="#B22222">/* this will create the instance structure for our use, and register the
 * property with the given ranges, default values and a comment for the
 * documentation/tooltip.
 */</FONT></I>
#<B><FONT COLOR="#5F9EA0">else</FONT></B>

<I><FONT COLOR="#B22222">/* Specify the base class we're building our operation on, the base
 * class provides a lot of the legwork so we do not have to. For
 * brightness contrast the best base class is the POINT_FILTER base
 * class.
 */</FONT></I>
#<B><FONT COLOR="#5F9EA0">define</FONT></B> <FONT COLOR="#B8860B">GEGL_CHANT_TYPE_POINT_FILTER</FONT>

<I><FONT COLOR="#B22222">/* We specify the file we're in, this is needed to make the code
 * generation for the properties work.
 */</FONT></I>
#<B><FONT COLOR="#5F9EA0">define</FONT></B> <FONT COLOR="#B8860B">GEGL_CHANT_C_FILE</FONT>       <B><FONT COLOR="#BC8F8F">&quot;brightness-contrast.c&quot;</FONT></B>

<I><FONT COLOR="#B22222">/* Including gegl-chant.h creates most of the GObject boiler plate
 * needed, creating a GeglChant instance structure a GeglChantClass
 * structure for our operation, as well as the needed code to register
 * our new gobject with GEGL.
 */</FONT></I>
#<B><FONT COLOR="#5F9EA0">include</FONT></B> <B><FONT COLOR="#BC8F8F">&quot;gegl-chant.h&quot;</FONT></B>

<I><FONT COLOR="#B22222">/* prepare() is called on each operation providing data to a node that
 * is requested to provide a rendered result. When prepare is called
 * all properties are known. For brightness contrast we use this
 * opportunity to dictate the formats of the input and output buffers.
 */</FONT></I>
<B><FONT COLOR="#228B22">static</FONT></B> <B><FONT COLOR="#228B22">void</FONT></B> <B><FONT COLOR="#0000FF">prepare</FONT></B> (GeglOperation *operation)
{
  gegl_operation_set_format (operation, <B><FONT COLOR="#BC8F8F">&quot;input&quot;</FONT></B>, babl_format (<B><FONT COLOR="#BC8F8F">&quot;RGBA float&quot;</FONT></B>));
  gegl_operation_set_format (operation, <B><FONT COLOR="#BC8F8F">&quot;output&quot;</FONT></B>, babl_format (<B><FONT COLOR="#BC8F8F">&quot;RGBA float&quot;</FONT></B>));
}

<I><FONT COLOR="#B22222">/* For GeglOperationPointFilter subclasses, we operate on linear
 * buffers with a pixel count.
 */</FONT></I>
<B><FONT COLOR="#228B22">static</FONT></B> gboolean
<B><FONT COLOR="#0000FF">process</FONT></B> (GeglOperation       *op,
         <B><FONT COLOR="#228B22">void</FONT></B>                *in_buf,
         <B><FONT COLOR="#228B22">void</FONT></B>                *out_buf,
         glong                n_pixels,
         <B><FONT COLOR="#228B22">const</FONT></B> GeglRectangle *roi,
         gint                 level)
{
  <I><FONT COLOR="#B22222">/* Retrieve a pointer to GeglChantO structure which contains all the
   * chanted properties
   */</FONT></I>
  GeglChantO *o = GEGL_CHANT_PROPERTIES (op);
  gfloat     * GEGL_ALIGNED in_pixel;
  gfloat     * GEGL_ALIGNED out_pixel;
  gfloat      brightness, contrast;
  glong       i;

  in_pixel   = in_buf;
  out_pixel  = out_buf;

  brightness = o-&gt;brightness;
  contrast   = o-&gt;contrast;

  <B><FONT COLOR="#A020F0">for</FONT></B> (i=0; i&lt;n_pixels; i++)
    {
      out_pixel[0] = (in_pixel[0] - 0.5f) * contrast + brightness + 0.5;
      out_pixel[1] = (in_pixel[1] - 0.5f) * contrast + brightness + 0.5;
      out_pixel[2] = (in_pixel[2] - 0.5f) * contrast + brightness + 0.5;
      out_pixel[3] = in_pixel[3]; <I><FONT COLOR="#B22222">/* copy the alpha */</FONT></I>
      in_pixel  += 4;
      out_pixel += 4;
    }
  <B><FONT COLOR="#A020F0">return</FONT></B> TRUE;
}

#<B><FONT COLOR="#5F9EA0">include</FONT></B> <B><FONT COLOR="#BC8F8F">&quot;opencl/gegl-cl.h&quot;</FONT></B>

<B><FONT COLOR="#228B22">static</FONT></B> <B><FONT COLOR="#228B22">const</FONT></B> <B><FONT COLOR="#228B22">char</FONT></B>* kernel_source =
<B><FONT COLOR="#BC8F8F">&quot;__kernel void kernel_bc(__global const float4     *in,         \n&quot;</FONT></B>
<B><FONT COLOR="#BC8F8F">&quot;                        __global       float4     *out,        \n&quot;</FONT></B>
<B><FONT COLOR="#BC8F8F">&quot;                        float brightness,                      \n&quot;</FONT></B>
<B><FONT COLOR="#BC8F8F">&quot;                        float contrast)                        \n&quot;</FONT></B>
<B><FONT COLOR="#BC8F8F">&quot;{                                                              \n&quot;</FONT></B>
<B><FONT COLOR="#BC8F8F">&quot;  int gid = get_global_id(0);                                  \n&quot;</FONT></B>
<B><FONT COLOR="#BC8F8F">&quot;  float4 in_v  = in[gid];                                      \n&quot;</FONT></B>
<B><FONT COLOR="#BC8F8F">&quot;  float4 out_v;                                                \n&quot;</FONT></B>
<B><FONT COLOR="#BC8F8F">&quot;  out_v.xyz = (in_v.xyz - 0.5f) * contrast + brightness + 0.5f;\n&quot;</FONT></B>
<B><FONT COLOR="#BC8F8F">&quot;  out_v.w   =  in_v.w;                                         \n&quot;</FONT></B>
<B><FONT COLOR="#BC8F8F">&quot;  out[gid]  =  out_v;                                          \n&quot;</FONT></B>
<B><FONT COLOR="#BC8F8F">&quot;}                                                              \n&quot;</FONT></B>;

<B><FONT COLOR="#228B22">static</FONT></B> gegl_cl_run_data *cl_data = NULL;

<I><FONT COLOR="#B22222">/* OpenCL processing function */</FONT></I>
<B><FONT COLOR="#228B22">static</FONT></B> cl_int
<B><FONT COLOR="#0000FF">cl_process</FONT></B> (GeglOperation       *op,
            cl_mem               in_tex,
            cl_mem               out_tex,
            size_t               global_worksize,
            <B><FONT COLOR="#228B22">const</FONT></B> GeglRectangle *roi,
            <B><FONT COLOR="#228B22">int</FONT></B>                  level)
{
  <I><FONT COLOR="#B22222">/* Retrieve a pointer to GeglChantO structure which contains all the
   * chanted properties
   */</FONT></I>

  GeglChantO *o = GEGL_CHANT_PROPERTIES (op);

  gfloat brightness = o-&gt;brightness;
  gfloat contrast   = o-&gt;contrast;

  cl_int cl_err = 0;

  <B><FONT COLOR="#A020F0">if</FONT></B> (!cl_data)
    {
      <B><FONT COLOR="#228B22">const</FONT></B> <B><FONT COLOR="#228B22">char</FONT></B> *kernel_name[] = {<B><FONT COLOR="#BC8F8F">&quot;kernel_bc&quot;</FONT></B>, NULL};
      cl_data = gegl_cl_compile_and_build (kernel_source, kernel_name);
    }

  <B><FONT COLOR="#A020F0">if</FONT></B> (!cl_data) <B><FONT COLOR="#A020F0">return</FONT></B> 1;

  cl_err |= gegl_clSetKernelArg(cl_data-&gt;kernel[0], 0, <B><FONT COLOR="#A020F0">sizeof</FONT></B>(cl_mem),   (<B><FONT COLOR="#228B22">void</FONT></B>*)&amp;in_tex);
  cl_err |= gegl_clSetKernelArg(cl_data-&gt;kernel[0], 1, <B><FONT COLOR="#A020F0">sizeof</FONT></B>(cl_mem),   (<B><FONT COLOR="#228B22">void</FONT></B>*)&amp;out_tex);
  cl_err |= gegl_clSetKernelArg(cl_data-&gt;kernel[0], 2, <B><FONT COLOR="#A020F0">sizeof</FONT></B>(cl_float), (<B><FONT COLOR="#228B22">void</FONT></B>*)&amp;brightness);
  cl_err |= gegl_clSetKernelArg(cl_data-&gt;kernel[0], 3, <B><FONT COLOR="#A020F0">sizeof</FONT></B>(cl_float), (<B><FONT COLOR="#228B22">void</FONT></B>*)&amp;contrast);
  <B><FONT COLOR="#A020F0">if</FONT></B> (cl_err != CL_SUCCESS) <B><FONT COLOR="#A020F0">return</FONT></B> cl_err;

  cl_err = gegl_clEnqueueNDRangeKernel(gegl_cl_get_command_queue (),
                                        cl_data-&gt;kernel[0], 1,
                                        NULL, &amp;global_worksize, NULL,
                                        0, NULL, NULL);
  <B><FONT COLOR="#A020F0">if</FONT></B> (cl_err != CL_SUCCESS) <B><FONT COLOR="#A020F0">return</FONT></B> cl_err;

  <B><FONT COLOR="#A020F0">return</FONT></B> cl_err;
}

<I><FONT COLOR="#B22222">/*
 * The class init function sets up information needed for this operations class
 * (template) in the GObject OO framework.
 */</FONT></I>
<B><FONT COLOR="#228B22">static</FONT></B> <B><FONT COLOR="#228B22">void</FONT></B>
<B><FONT COLOR="#0000FF">gegl_chant_class_init</FONT></B> (GeglChantClass *klass)
{
  GeglOperationClass            *operation_class;
  GeglOperationPointFilterClass *point_filter_class;

  operation_class    = GEGL_OPERATION_CLASS (klass);
  point_filter_class = GEGL_OPERATION_POINT_FILTER_CLASS (klass);

  <I><FONT COLOR="#B22222">/* override the prepare methods of the GeglOperation class */</FONT></I>
  operation_class-&gt;prepare = prepare;
  <I><FONT COLOR="#B22222">/* override the process method of the point filter class (the process methods
   * of our superclasses deal with the handling on their level of abstraction)
   */</FONT></I>
  point_filter_class-&gt;process = process;
  point_filter_class-&gt;cl_process = cl_process;

  <I><FONT COLOR="#B22222">/* specify the name this operation is found under in the GUI/when
   * programming/in XML
   */</FONT></I>
  operation_class-&gt;opencl_support = TRUE;

  gegl_operation_class_set_keys (operation_class,
      <B><FONT COLOR="#BC8F8F">&quot;name&quot;</FONT></B>,       <B><FONT COLOR="#BC8F8F">&quot;gegl:brightness-contrast&quot;</FONT></B>,
      <B><FONT COLOR="#BC8F8F">&quot;categories&quot;</FONT></B>, <B><FONT COLOR="#BC8F8F">&quot;color&quot;</FONT></B>, 
      <B><FONT COLOR="#BC8F8F">&quot;description&quot;</FONT></B>, _(<B><FONT COLOR="#BC8F8F">&quot;Changes the light level and contrast.&quot;</FONT></B>),
      NULL);
}

#<B><FONT COLOR="#5F9EA0">endif</FONT></B> <I><FONT COLOR="#B22222">/* closing #ifdef GEGL_CHANT_PROPERTIES ... else ... */</FONT></I>
</PRE>
<HR>
<ADDRESS>Generated by <A HREF="http://www.iki.fi/~mtr/genscript/">GNU Enscript 1.6.5.90</A>.</ADDRESS>
</BODY>
</HTML>