noise-hsv.c 8.17 KB
Newer Older
Maxime Nicco's avatar
Maxime Nicco committed
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/>.
Maxime Nicco's avatar
Maxime Nicco committed
15 16 17 18 19 20 21 22 23 24
 *
 * 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
property_int  (holdness, _("Dulling"), 2)
28
  value_range (1, 8)
29
  description (_("A high value lowers the randomness of the noise"))
Maxime Nicco's avatar
Maxime Nicco committed
30

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

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

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

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

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

44
#define GEGL_OP_POINT_FILTER
45
#define GEGL_OP_NAME     noise_hsv
46
#define GEGL_OP_C_SOURCE noise-hsv.c
Maxime Nicco's avatar
Maxime Nicco committed
47

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

static gfloat
54 55 56 57 58 59 60 61 62 63
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
64 65
{
  gint    flag, i;
66
  gfloat rand_val, new_val, steps;
Maxime Nicco's avatar
Maxime Nicco committed
67

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

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

78
  flag = (gegl_random_float (rand, x, y, 0, n) < 0.5) ? -1 : 1;
Maxime Nicco's avatar
Maxime Nicco committed
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99
  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;
}

100 101
static void
prepare (GeglOperation *operation)
Maxime Nicco's avatar
Maxime Nicco committed
102
{
103 104 105
  const Babl *space = gegl_operation_get_source_space (operation, "input");
  gegl_operation_set_format (operation, "input", babl_format_with_space ("HSVA float", space));
  gegl_operation_set_format (operation, "output", babl_format_with_space ("HSVA float", space));
Maxime Nicco's avatar
Maxime Nicco committed
106 107 108 109 110 111 112 113 114 115
}

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

  gfloat   * GEGL_ALIGNED in_pixel;
  gfloat   * GEGL_ALIGNED out_pixel;

  gfloat    hue, saturation, value, alpha;

  in_pixel      = in_buf;
  out_pixel     = out_buf;

129 130 131 132 133
  x = roi->x;
  y = roi->y;

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

Maxime Nicco's avatar
Maxime Nicco committed
134 135
  for (i = 0; i < n_pixels; i++)
  {
136 137 138
    /* 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
139 140 141 142
    hue        = in_pixel[0];
    saturation = in_pixel[1];
    value      = in_pixel[2];
    alpha      = in_pixel[3];
Maxime Nicco's avatar
Maxime Nicco committed
143 144 145

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

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

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

164 165 166 167
    out_pixel[0] = hue;
    out_pixel[1] = saturation;
    out_pixel[2] = value;
    out_pixel[3] = alpha;
Maxime Nicco's avatar
Maxime Nicco committed
168

Maxime Nicco's avatar
Maxime Nicco committed
169 170
    in_pixel  += 4;
    out_pixel += 4;
Maxime Nicco's avatar
Maxime Nicco committed
171

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

  return TRUE;
}

183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 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
#include "opencl/gegl-cl.h"
#include "opencl/noise-hsv.cl.h"

static GeglClRunData *cl_data = NULL;

static gboolean
cl_process (GeglOperation       *operation,
            cl_mem               in,
            cl_mem               out,
            size_t               global_worksize,
            const GeglRectangle *roi,
            gint                 level)
{
  GeglProperties *o = GEGL_PROPERTIES (operation);
  GeglRectangle *wr = gegl_operation_source_get_bounding_box (operation,
                                                              "input");
  cl_int      cl_err           = 0;
  cl_mem      cl_random_data   = NULL;
  cl_int      x_offset         = roi->x;
  cl_int      y_offset         = roi->y;
  cl_int      roi_width        = roi->width;
  cl_int      wr_width         = wr->width;
  cl_ushort4  rand;
  cl_int      holdness;
  cl_float    hue_distance;
  cl_float    saturation_distance;
  cl_float    value_distance;

  gegl_cl_random_get_ushort4 (o->rand, &rand);

  if (!cl_data)
    {
      const char *kernel_name[] = { "cl_noise_hsv", NULL };
      cl_data = gegl_cl_compile_and_build (noise_hsv_cl_source, kernel_name);
    }

  if (!cl_data)
    return TRUE;

  cl_random_data = gegl_cl_load_random_data (&cl_err);
  CL_CHECK;

  holdness = o->holdness;
  hue_distance = o->hue_distance / 360.0;
  saturation_distance = o->saturation_distance;
  value_distance = o->value_distance;

  gegl_cl_set_kernel_args (cl_data->kernel[0],
                           sizeof(cl_mem),     &in,
                           sizeof(cl_mem),     &out,
                           sizeof(cl_mem),     &cl_random_data,
                           sizeof(cl_ushort4), &rand,
                           sizeof(cl_int),     &x_offset,
                           sizeof(cl_int),     &y_offset,
                           sizeof(cl_int),     &roi_width,
                           sizeof(cl_int),     &wr_width,
                           sizeof(cl_int),     &holdness,
                           sizeof(cl_float),   &hue_distance,
                           sizeof(cl_float),   &saturation_distance,
                           sizeof(cl_float),   &value_distance,
                           NULL);

  CL_CHECK;
  cl_err = gegl_clEnqueueNDRangeKernel (gegl_cl_get_command_queue (),
                                        cl_data->kernel[0], 1,
                                        NULL, &global_worksize, NULL,
                                        0, NULL, NULL);
  CL_CHECK;

  cl_err = gegl_clFinish (gegl_cl_get_command_queue ());
  CL_CHECK;

  return  FALSE;

error:
  return TRUE;
}

Maxime Nicco's avatar
Maxime Nicco committed
261
static void
262
gegl_op_class_init (GeglOpClass *klass)
Maxime Nicco's avatar
Maxime Nicco committed
263 264 265 266 267 268 269 270
{
  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;
271
  operation_class->opencl_support = TRUE;
Maxime Nicco's avatar
Maxime Nicco committed
272
  point_filter_class->process = process;
273
  point_filter_class->cl_process = cl_process;
Maxime Nicco's avatar
Maxime Nicco committed
274 275

  gegl_operation_class_set_keys (operation_class,
276
    "name",       "gegl:noise-hsv",
277
    "title",      _("Add HSV Noise"),
278
    "categories", "noise",
279
    "reference-hash", "742a94075b6ddfaf86638691bf654b3b",
280
    "description", _("Randomize hue, saturation and value independently"),
Maxime Nicco's avatar
Maxime Nicco committed
281 282 283 284
      NULL);
}

#endif