noise-hsv.c 5.27 KB
Newer Older
Maxime Nicco's avatar
Maxime Nicco committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
/* 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 <http://www.gnu.org/licenses/>.
 *
 * Copyright 1995 Spencer Kimball and Peter Mattis
 * Copyright 1996 Torsten Martinsen
 * Copyright 2000 Tim Copperfield <timecop@japan.co.jp>
 * Copyright 2012 Maxime Nicco <maxime.nicco@gmail.com>
 */

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

25
#ifdef GEGL_PROPERTIES
Maxime Nicco's avatar
Maxime Nicco committed
26

27 28
property_int  (holdness, _("Holdness"), 2)
  value_range (1, 8)
Maxime Nicco's avatar
Maxime Nicco committed
29

30 31
property_double (hue_distance, _("Hue"), 3.0)
  value_range   (0.0, 180.0)
Maxime Nicco's avatar
Maxime Nicco committed
32

33 34
property_double (saturation_distance, _("Saturation"), 0.04)
  value_range   (0.0, 1.0)
Maxime Nicco's avatar
Maxime Nicco committed
35

36 37
property_double (value_distance, _("Value"), 0.04)
  value_range   (0.0, 1.0)
Maxime Nicco's avatar
Maxime Nicco committed
38

39
property_seed   (seed, _("Random seed"), rand)
40

Maxime Nicco's avatar
Maxime Nicco committed
41 42
#else

43 44
#define GEGL_OP_POINT_FILTER
#define GEGL_OP_C_FILE "noise-hsv.c"
Maxime Nicco's avatar
Maxime Nicco committed
45

46
#include "gegl-op.h"
Maxime Nicco's avatar
Maxime Nicco committed
47 48 49 50 51
#include <stdio.h>
#include <math.h>
#include <stdlib.h>

static gfloat
52 53 54 55 56 57 58 59 60 61
randomize_value (gfloat      now,
                 gfloat      min,
                 gfloat      max,
                 gboolean    wraps_around,
                 gfloat      rand_max,
                 gint        holdness,
                 gint        x,
                 gint        y,
                 gint        n,
                 GeglRandom *rand)
Maxime Nicco's avatar
Maxime Nicco committed
62 63
{
  gint    flag, i;
64
  gfloat rand_val, new_val, steps;
Maxime Nicco's avatar
Maxime Nicco committed
65

Simon Budig's avatar
Simon Budig committed
66
  steps = max - min;
67
  rand_val = gegl_random_float (rand, x, y, 0, n++);
Maxime Nicco's avatar
Maxime Nicco committed
68 69 70

  for (i = 1; i < holdness; i++)
  {
71
    gfloat tmp = gegl_random_float (rand, x, y, 0, n++);
Maxime Nicco's avatar
Maxime Nicco committed
72 73 74 75
    if (tmp < rand_val)
      rand_val = tmp;
  }

76
  flag = (gegl_random_float (rand, x, y, 0, n) < 0.5) ? -1 : 1;
Maxime Nicco's avatar
Maxime Nicco committed
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97
  new_val = now + flag * fmod (rand_max * rand_val, steps);

  if (new_val < min)
  {
    if (wraps_around)
      new_val += steps;
    else
      new_val = min;
  }

  if (max < new_val)
  {
    if (wraps_around)
      new_val -= steps;
    else
      new_val = max;
  }

  return new_val;
}

98 99
static void
prepare (GeglOperation *operation)
Maxime Nicco's avatar
Maxime Nicco committed
100
{
Maxime Nicco's avatar
Maxime Nicco committed
101 102
  gegl_operation_set_format (operation, "input", babl_format ("HSVA float"));
  gegl_operation_set_format (operation, "output", babl_format ("HSVA float"));
Maxime Nicco's avatar
Maxime Nicco committed
103 104 105 106 107 108 109 110 111 112
}

static gboolean
process (GeglOperation       *operation,
         void                *in_buf,
         void                *out_buf,
         glong                n_pixels,
         const GeglRectangle *roi,
         gint                 level)
{
113
  GeglProperties *o  = GEGL_PROPERTIES (operation);
114
  GeglRectangle whole_region;
Maxime Nicco's avatar
Maxime Nicco committed
115
  gint i;
116
  gint x, y;
Maxime Nicco's avatar
Maxime Nicco committed
117 118 119 120 121 122 123 124 125

  gfloat   * GEGL_ALIGNED in_pixel;
  gfloat   * GEGL_ALIGNED out_pixel;

  gfloat    hue, saturation, value, alpha;

  in_pixel      = in_buf;
  out_pixel     = out_buf;

126 127 128 129 130
  x = roi->x;
  y = roi->y;

  whole_region = *(gegl_operation_source_get_bounding_box (operation, "input"));

Maxime Nicco's avatar
Maxime Nicco committed
131 132
  for (i = 0; i < n_pixels; i++)
  {
133 134 135
    /* n is independent from the roi, but from the whole image */
    gint n = (3 * o->holdness + 4) * (x + whole_region.width * y);

Maxime Nicco's avatar
Maxime Nicco committed
136 137 138 139
    hue        = in_pixel[0];
    saturation = in_pixel[1];
    value      = in_pixel[2];
    alpha      = in_pixel[3];
Maxime Nicco's avatar
Maxime Nicco committed
140 141 142

    /* there is no need for scattering hue of desaturated pixels here */
    if ((o->hue_distance > 0) && (saturation > 0))
143
      hue = randomize_value (hue, 0.0, 1.0, TRUE, o->hue_distance / 360.0,
144
                             o->holdness, x, y, n, o->rand);
Maxime Nicco's avatar
Maxime Nicco committed
145

146
    n += o->holdness + 1;
Maxime Nicco's avatar
Maxime Nicco committed
147 148 149
    /* desaturated pixels get random hue before increasing saturation */
    if (o->saturation_distance > 0) {
      if (saturation == 0)
150
        hue = gegl_random_float_range (o->rand, x, y, 0, n, 0.0, 1.0);
151 152
      saturation = randomize_value (saturation, 0.0, 1.0, FALSE,
                                    o->saturation_distance, o->holdness,
153
                                    x, y, n+1, o->rand);
Maxime Nicco's avatar
Maxime Nicco committed
154 155
    }

156
    n += o->holdness + 2;
Maxime Nicco's avatar
Maxime Nicco committed
157
    if (o->value_distance > 0)
158
      value = randomize_value (value, 0.0, 1.0, FALSE, o->value_distance,
159
                               o->holdness, x, y, n, o->rand);
Maxime Nicco's avatar
Maxime Nicco committed
160

161 162 163 164
    out_pixel[0] = hue;
    out_pixel[1] = saturation;
    out_pixel[2] = value;
    out_pixel[3] = alpha;
Maxime Nicco's avatar
Maxime Nicco committed
165

Maxime Nicco's avatar
Maxime Nicco committed
166 167
    in_pixel  += 4;
    out_pixel += 4;
Maxime Nicco's avatar
Maxime Nicco committed
168

169 170 171 172 173 174
    x++;
    if (x >= roi->x + roi->width)
      {
        x = roi->x;
        y++;
      }
Maxime Nicco's avatar
Maxime Nicco committed
175 176 177 178 179 180
  }

  return TRUE;
}

static void
181
gegl_op_class_init (GeglOpClass *klass)
Maxime Nicco's avatar
Maxime Nicco committed
182 183 184 185 186 187 188 189 190 191 192
{
  GeglOperationClass            *operation_class;
  GeglOperationPointFilterClass *point_filter_class;

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

  operation_class->prepare = prepare;
  point_filter_class->process = process;

  gegl_operation_class_set_keys (operation_class,
193
    "name",       "gegl:noise-hsv",
194
    "title",      _("Add HSV Noise"),
195 196
    "categories", "noise",
    "description", _("Randomize hue, saturation and value independently"),
Maxime Nicco's avatar
Maxime Nicco committed
197 198 199 200
      NULL);
}

#endif