color-enhance.c 8.24 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13
/* 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
14
 * License along with GEGL; if not, see <https://www.gnu.org/licenses/>.
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
 *
 * This operation is inspired from the color enhance gimp plugin.
 * It alters the chroma component to cover maximum possible range,
 * keeping hue and lightness untouched.
 *
 * Thomas Manni <thomas.manni@free.fr>
 */

#include "config.h"
#include <glib/gi18n-lib.h>
#include <math.h>

#ifdef GEGL_PROPERTIES

#else

#define GEGL_OP_FILTER
32
#define GEGL_OP_NAME     color_enhance
33
#define GEGL_OP_C_SOURCE color-enhance.c
34 35 36 37

#include "gegl-op.h"

static void
38 39 40 41
buffer_get_min_max (GeglOperation       *operation,
                    GeglBuffer          *buffer,
                    const GeglRectangle *result,
                    gdouble             *min,
42 43
                    gdouble             *max,
                    const Babl          *format)
44 45
{
  GeglBufferIterator *gi;
46
  gint                done_pixels = 0;
47

48 49
  gegl_operation_progress (operation, 0.0, "");

50
  gi = gegl_buffer_iterator_new (buffer, result, 0, format,
51
                                 GEGL_ACCESS_READ, GEGL_ABYSS_NONE, 1);
52 53 54 55 56 57 58

  *min = G_MAXDOUBLE;
  *max = -G_MAXDOUBLE;

  while (gegl_buffer_iterator_next (gi))
    {
      gint o;
59
      gfloat *buf = gi->items[0].data;
60 61 62 63 64 65 66

      for (o = 0; o < gi->length; o++)
        {
          *min = MIN (buf[1], *min);
          *max = MAX (buf[1], *max);
          buf += 3;
        }
67 68 69 70 71 72 73

      done_pixels += gi->length;

      gegl_operation_progress (operation,
                               (gdouble) 0.5 * done_pixels /
                               (gdouble) (result->width * result->height),
                               "");
74
    }
75 76

  gegl_operation_progress (operation, 0.5, "");
77 78 79 80
}

static void prepare (GeglOperation *operation)
{
81
  const Babl *space = gegl_operation_get_source_space (operation, "input");
82 83 84 85 86 87
  const Babl *format;
  const Babl *in_format = gegl_operation_get_source_format (operation, "input");

  if (in_format)
    {
       if (babl_format_has_alpha (in_format))
88
         format = babl_format_with_space ("CIE LCH(ab) alpha float", space);
89
       else
90
         format = babl_format_with_space ("CIE LCH(ab) float", space);
91 92 93
    }
  else
    {
94
      format = babl_format_with_space ("CIE LCH(ab) float", space);
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137
    }

  gegl_operation_set_format (operation, "input", format);
  gegl_operation_set_format (operation, "output", format);
}

static GeglRectangle
get_required_for_output (GeglOperation       *operation,
                         const gchar         *input_pad,
                         const GeglRectangle *roi)
{
  GeglRectangle result = *gegl_operation_source_get_bounding_box (operation, "input");

  /* Don't request an infinite plane */
  if (gegl_rectangle_is_infinite_plane (&result))
    return *roi;

  return result;
}

static GeglRectangle
get_cached_region (GeglOperation       *operation,
                   const GeglRectangle *roi)
{
  GeglRectangle result = *gegl_operation_source_get_bounding_box (operation, "input");

  if (gegl_rectangle_is_infinite_plane (&result))
    return *roi;

  return result;
}


static gboolean
process (GeglOperation       *operation,
         GeglBuffer          *input,
         GeglBuffer          *output,
         const GeglRectangle *result,
         gint                 level)
{
  const Babl *format = gegl_operation_get_format (operation, "output");
  gboolean has_alpha = babl_format_has_alpha (format);
  GeglBufferIterator *gi;
138
  gint                done_pixels = 0;
139 140 141 142
  gdouble  min;
  gdouble  max;
  gdouble  delta;

143 144 145
  buffer_get_min_max (operation, input, result, &min, &max,
                      babl_format_with_space ("CIE LCH(ab) float",
                      babl_format_get_space (format)));
146 147

  gegl_operation_progress (operation, 0.5, "");
148 149

  gi = gegl_buffer_iterator_new (input, result, 0, format,
150
                                 GEGL_ACCESS_READ, GEGL_ABYSS_NONE, 2);
151 152 153 154 155 156 157 158

  gegl_buffer_iterator_add (gi, output, result, 0, format,
                            GEGL_ACCESS_WRITE, GEGL_ABYSS_NONE);

  delta = max - min;

  if (! delta)
    {
159 160
      gegl_buffer_copy (input, NULL, GEGL_ABYSS_NONE,
                        output, NULL);
161 162 163 164 165 166 167
      return TRUE;
    }

  if (has_alpha)
    {
      while (gegl_buffer_iterator_next (gi))
        {
168 169
          gfloat *in  = gi->items[0].data;
          gfloat *out = gi->items[1].data;
170 171 172 173 174 175 176 177 178 179 180 181

          gint i;
          for (i = 0; i < gi->length; i++)
            {
              out[0] = in[0];
              out[1] = (in[1] - min) / delta * 100.0;
              out[2] = in[2];
              out[3] = in[3];

              in  += 4;
              out += 4;
            }
182 183 184 185 186 187 188 189 190

          done_pixels += gi->length;

          gegl_operation_progress (operation,
                                   0.5 +
                                   (gdouble) 0.5 * done_pixels /
                                   (gdouble) (result->width * result->height),
                                   "");
       }
191 192 193 194 195
    }
  else
    {
       while (gegl_buffer_iterator_next (gi))
        {
196 197
          gfloat *in  = gi->items[0].data;
          gfloat *out = gi->items[1].data;
198 199 200 201 202 203 204 205 206 207 208

          gint i;
          for (i = 0; i < gi->length; i++)
            {
              out[0] = in[0];
              out[1] = (in[1] - min) / delta * 100.0;
              out[2] = in[2];

              in  += 3;
              out += 3;
            }
209 210 211 212 213 214 215 216

          done_pixels += gi->length;

          gegl_operation_progress (operation,
                                   0.5 +
                                   (gdouble) 0.5 * done_pixels /
                                   (gdouble) (result->width * result->height),
                                   "");
217 218 219
        }
    }

220 221
  gegl_operation_progress (operation, 1.0, "");

222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271
  return TRUE;
}

/* Pass-through when trying to perform a reduction on an infinite plane
 */
static gboolean
operation_process (GeglOperation        *operation,
                   GeglOperationContext *context,
                   const gchar          *output_prop,
                   const GeglRectangle  *result,
                   gint                  level)
{
  GeglOperationClass  *operation_class;

  const GeglRectangle *in_rect =
    gegl_operation_source_get_bounding_box (operation, "input");

  operation_class = GEGL_OPERATION_CLASS (gegl_op_parent_class);

  if (in_rect && gegl_rectangle_is_infinite_plane (in_rect))
    {
      gpointer in = gegl_operation_context_get_object (context, "input");
      gegl_operation_context_take_object (context, "output",
                                          g_object_ref (G_OBJECT (in)));
      return TRUE;
    }

  /* chain up, which will create the needed buffers for our actual
   * process function
   */
  return operation_class->process (operation, context, output_prop, result,
                                   gegl_operation_context_get_level (context));
}

static void
gegl_op_class_init (GeglOpClass *klass)
{
  GeglOperationClass       *operation_class;
  GeglOperationFilterClass *filter_class;

  operation_class = GEGL_OPERATION_CLASS (klass);
  filter_class    = GEGL_OPERATION_FILTER_CLASS (klass);

  filter_class->process = process;

  operation_class->prepare = prepare;
  operation_class->process = operation_process;
  operation_class->get_required_for_output = get_required_for_output;
  operation_class->get_cached_region = get_cached_region;
  operation_class->opencl_support = FALSE;
272
  operation_class->threaded = FALSE;
273 274 275 276 277

  gegl_operation_class_set_keys (operation_class,
    "name",        "gegl:color-enhance",
    "title",       _("Color Enhance"),
    "categories" , "color:enhance",
278
    "reference-hash", "a0d2c5b2bdf39c93bb2c2bdf2a9ea857",
279 280 281 282 283 284 285
    "description",
        _("Stretch color chroma to cover maximum possible range, "
          "keeping hue and lightness untouched."),
        NULL);
}

#endif