brightness-contrast.c 5.46 KB
Newer Older
1 2 3 4 5
/* 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
6
 * version 3 of the License, or (at your option) any later version.
7 8 9 10 11 12 13
 *
 * 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
14
 * License along with GEGL; if not, see <http://www.gnu.org/licenses/>.
15 16 17
 *
 * Copyright 2006 Øyvind Kolås <pippin@gimp.org>
 */
18

19 20
#include "config.h"
#include <glib/gi18n-lib.h>
21

22 23 24 25 26
#ifdef GEGL_PROPERTIES

/*  Here in the top of the file the properties of the operation is declared,
 *  this causes the declaration of a structure for containing the data.
 *
27
 *  The first member of each property_ macro becomes a struct member
28
 *  in the GeglProperties struct used when processing.
Sven Neumann's avatar
Sven Neumann committed
29
 */
30

31 32 33 34
property_double (contrast, _("Contrast"),  1.0)
   description  (_("Magnitude of contrast scaling >1.0 brighten < 1.0 darken"))
   value_range  (-5.0, 5.0)
   ui_range     (0.0, 2.0)
35

36 37 38 39
property_double (brightness, _("Brightness"), 0.0)
   description  (_("Amount to increase brightness"))
   value_range  (-3.0, 3.0)
   ui_range     (-1.0, 1.0)
40

41
#else
42

Sven Neumann's avatar
Sven Neumann committed
43 44 45 46
/* 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.
47
 */
48 49

#define GEGL_OP_POINT_FILTER
50

51 52 53 54
/* The C prefix used for some generated functions
 */
#define GEGL_OP_NAME     brightness_contrast

Sven Neumann's avatar
Sven Neumann committed
55 56
/* We specify the file we're in, this is needed to make the code
 * generation for the properties work.
57
 */
58
#define GEGL_OP_C_SOURCE brightness-contrast.c
59

60 61
/* Including gegl-op.h creates most of the GObject boiler plate
 * needed, creating a GeglOp instance structure a GeglOpClass
Sven Neumann's avatar
Sven Neumann committed
62 63
 * structure for our operation, as well as the needed code to register
 * our new gobject with GEGL.
64
 */
65
#include "gegl-op.h"
66

67
/* prepare() is called on each operation providing data to a node that
Sven Neumann's avatar
Sven Neumann committed
68 69 70
 * 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.
71
 */
72 73
static void prepare (GeglOperation *operation)
{
74 75
  gegl_operation_set_format (operation, "input", babl_format ("RGBA float"));
  gegl_operation_set_format (operation, "output", babl_format ("RGBA float"));
76 77
}

Sven Neumann's avatar
Sven Neumann committed
78 79
/* For GeglOperationPointFilter subclasses, we operate on linear
 * buffers with a pixel count.
80
 */
81
static gboolean
82 83 84 85
process (GeglOperation       *op,
         void                *in_buf,
         void                *out_buf,
         glong                n_pixels,
86 87
         const GeglRectangle *roi,
         gint                 level)
88
{
89
  /* Retrieve a pointer to GeglProperties structure which contains all the
Sven Neumann's avatar
Sven Neumann committed
90
   * chanted properties
91
   */
92
  GeglProperties *o = GEGL_PROPERTIES (op);
93 94
  gfloat     * GEGL_ALIGNED in_pixel;
  gfloat     * GEGL_ALIGNED out_pixel;
95
  gfloat      brightness, contrast;
96
  glong       i;
97

98 99 100 101 102
  in_pixel   = in_buf;
  out_pixel  = out_buf;

  brightness = o->brightness;
  contrast   = o->contrast;
103

104
  for (i=0; i<n_pixels; i++)
105
    {
106 107 108
      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;
109
      out_pixel[3] = in_pixel[3]; /* copy the alpha */
110
      in_pixel  += 4;
111
      out_pixel += 4;
112 113 114
    }
  return TRUE;
}
115

116
#include "opencl/brightness-contrast.cl.h"
117

118 119 120 121
/*
 * The class init function sets up information needed for this operations class
 * (template) in the GObject OO framework.
 */
122
static void
123
gegl_op_class_init (GeglOpClass *klass)
124 125 126
{
  GeglOperationClass            *operation_class;
  GeglOperationPointFilterClass *point_filter_class;
127 128 129 130 131 132 133 134 135 136 137 138 139 140
  gchar                         *composition = "<?xml version='1.0' encoding='UTF-8'?>"
    "<gegl>"
    "<node operation='gegl:brightness-contrast'>"
    "  <params>"
    "    <param name='contrast'>1.8</param>"
    "    <param name='brightness'>0.25</param>"
    "  </params>"
    "</node>"
    "<node operation='gegl:load'>"
    "  <params>"
    "    <param name='path'>standard-input.png</param>"
    "  </params>"
    "</node>"
    "</gegl>";
141 142 143 144

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

145
  /* override the prepare methods of the GeglOperation class */
146
  operation_class->prepare = prepare;
147 148 149 150
  /* override the process method of the point filter class (the process methods
   * of our superclasses deal with the handling on their level of abstraction)
   */
  point_filter_class->process = process;
151

152 153
  gegl_operation_class_set_keys (operation_class,
      "name",       "gegl:brightness-contrast",
154
      "title",      _("Brightness Contrast"),
155
      "categories", "color",
156
      "description", _("Changes the light level and contrast. This operation operates in linear light, 'contrast' is a scale factor around 50%% gray, and 'brightness' a constant offset to apply after contrast scaling."),
157
      "cl-source"  , brightness_contrast_cl_source,
158
      "reference-composition", composition,
159
      NULL);
160
}
161

162
#endif /* closing #ifdef GEGL_PROPERTIES ... else ... */