noise-solid.c 8.89 KB
Newer Older
1 2
/* This file is an image processing operation for GEGL
 *
3 4 5 6
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
7
 *
8
 * This program is distributed in the hope that it will be useful,
9
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
12
 *
13
 * You should have received a copy of the GNU General Public License
14
 * along with this program.  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 32 33 34 35 36 37 38
 *
 * This operation generates solid noise textures based on the
 * `Noise' and `Turbulence' functions described in the paper
 *
 *    Perlin, K, and Hoffert, E. M., "Hypertexture",
 *    Computer Graphics 23, 3 (August 1989)
 *
 * The algorithm implemented here also makes possible the
 * creation of seamless tiles.
 *
 * Copyright (C) 1997, 1998 Marcelo de Gomensoro Malheiros
 *
 * GEGL port: Thomas Manni <thomas.manni@free.fr>
 *
 */

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

#ifdef GEGL_PROPERTIES

property_double (x_size, _("X Size"), 4.0)
    description (_("Horizontal texture size"))
39
    value_range (0.1, 16.0)
40 41 42 43 44 45
    ui_range    (0.1, 16.0)
    ui_meta     ("unit", "pixel-distance")
    ui_meta     ("axis", "x")

property_double (y_size, _("Y Size"), 4.0)
    description (_("Vertical texture size"))
46
    value_range (0.1, 16.0)
47 48 49 50 51 52 53 54 55
    ui_range    (0.1, 16.0)
    ui_meta     ("unit", "pixel-distance")
    ui_meta     ("axis", "y")

property_int    (detail, _("Detail"), 1)
    description (_("Detail level"))
    ui_range    (0, 15)
    value_range (0, 15)

56 57
property_boolean (tileable, _("Tileable"), FALSE)
    description  (_("Create a tileable output"))
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82

property_boolean (turbulent, _("Turbulent"), FALSE)
    description  (_("Make a turbulent noise"))

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

property_int    (width, _("Width"), 1024)
    description (_("Width of the generated buffer"))
    value_range (0, G_MAXINT)
    ui_range    (0, 4096)
    ui_meta     ("unit", "pixel-distance")
    ui_meta     ("axis", "x")
    ui_meta     ("role", "output-extent")

property_int (height, _("Height"), 768)
    description(_("Height of the generated buffer"))
    value_range (0, G_MAXINT)
    ui_range    (0, 4096)
    ui_meta     ("unit", "pixel-distance")
    ui_meta     ("axis", "y")
    ui_meta     ("role", "output-extent")

#else

#define GEGL_OP_POINT_RENDER
83
#define GEGL_OP_NAME     noise_solid
84
#define GEGL_OP_C_SOURCE noise-solid.c
85 86 87 88

#include "gegl-op.h"

#define TABLE_SIZE  64
89
#define WEIGHT(T)   ((-6.0*(T)*(T) +15.0*fabs(T)- 10.0) * fabs((T)*(T)*(T)) + 1.0)
90 91 92 93 94 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

typedef struct
{
  gdouble x;
  gdouble y;
} Vector2;

typedef struct
{
  gint         xclip;
  gint         yclip;
  gdouble      offset;
  gdouble      factor;
  gdouble      xsize;
  gdouble      ysize;
  gint         perm_tab[TABLE_SIZE];
  Vector2      grad_tab[TABLE_SIZE];
} NsParamsType;

static void
solid_noise_init (GeglProperties *o)
{
  gint          i, j, k, t;
  gdouble       m;
  GRand        *gr;
  NsParamsType *params;

  params = (NsParamsType *) o->user_data;

  g_assert (params != NULL);

  gr = g_rand_new_with_seed (o->seed);

  /*  Set scaling factors  */
124
  if (o->tileable)
125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197
    {
      params->xsize = ceil (o->x_size);
      params->ysize = ceil (o->y_size);
      params->xclip = (gint) params->xsize;
      params->yclip = (gint) params->ysize;
    }
  else
    {
      params->xsize = o->x_size;
      params->ysize = o->y_size;
    }

  /*  Set totally empiric normalization values  */
  if (o->turbulent)
    {
      params->offset = 0.0;
      params->factor = 1.0;
    }
  else
    {
      params->offset = 0.94;
      params->factor = 0.526;
    }

  /*  Initialize the permutation table  */
  for (i = 0; i < TABLE_SIZE; i++)
    params->perm_tab[i] = i;

  for (i = 0; i < (TABLE_SIZE >> 1); i++)
    {
      j = g_rand_int_range (gr, 0, TABLE_SIZE);
      k = g_rand_int_range (gr, 0, TABLE_SIZE);
      t = params->perm_tab[j];
      params->perm_tab[j] = params->perm_tab[k];
      params->perm_tab[k] = t;
    }

  /*  Initialize the gradient table  */
  for (i = 0; i < TABLE_SIZE; i++)
    {
      do
        {
          params->grad_tab[i].x = g_rand_double_range (gr, -1, 1);
          params->grad_tab[i].y = g_rand_double_range (gr, -1, 1);
          m = params->grad_tab[i].x * params->grad_tab[i].x +
                   params->grad_tab[i].y * params->grad_tab[i].y;
        }
      while (m == 0.0 || m > 1.0);

      m = 1.0 / sqrt(m);
      params->grad_tab[i].x *= m;
      params->grad_tab[i].y *= m;
    }

  g_rand_free (gr);
}

static gdouble
plain_noise (gdouble         x,
             gdouble         y,
             guint           s,
             GeglProperties *o)
{
  Vector2       v;
  gint          a, b, i, j, n;
  gdouble       sum;
  NsParamsType *p;

  p = (NsParamsType *) o->user_data;

  sum = 0.0;
  x *= s;
  y *= s;
198 199
  a = (gint) fabs (floor (x));
  b = (gint) fabs (floor (y));
200 201 202 203 204

  for (i = 0; i < 2; i++)
    {
      for (j = 0; j < 2; j++)
        {
205
          if (o->tileable)
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
            n = p->perm_tab[(((a + i) % (p->xclip * s)) + p->perm_tab[((b + j) % (p->yclip * s)) % TABLE_SIZE]) % TABLE_SIZE];
          else
            n = p->perm_tab[(a + i + p->perm_tab[(b + j) % TABLE_SIZE]) % TABLE_SIZE];
          v.x = x - a - i;
          v.y = y - b - j;
          sum += WEIGHT(v.x) * WEIGHT(v.y) * (p->grad_tab[n].x * v.x + p->grad_tab[n].y * v.y);
        }
    }

  return sum / s;
}

static gdouble
noise (gdouble         x,
       gdouble         y,
       GeglProperties *o)
{
  gint          i;
  guint         s;
  gdouble       sum;
  NsParamsType *p;

  p = (NsParamsType *) o->user_data;

  s = 1;
  sum = 0.0;
  x *= p->xsize;
  y *= p->ysize;

  for (i = 0; i <= o->detail; i++)
    {
      if (o->turbulent)
        sum += fabs (plain_noise (x, y, s, o));
      else
        sum += plain_noise (x, y, s, o);
      s <<= 1;
    }

  return (sum + p->offset) * p->factor;
}

static void
prepare (GeglOperation *operation)
{
  GeglProperties *o = GEGL_PROPERTIES (operation);
  const Babl     *format = babl_format ("Y' float");

  if (o->user_data == NULL)
    o->user_data = g_slice_new0 (NsParamsType);

  solid_noise_init (o);

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

261 262 263 264 265 266 267 268 269 270 271 272 273 274 275
static void
finalize (GObject *object)
{
  GeglOperation *op = (void*) object;
  GeglProperties *o = GEGL_PROPERTIES (op);

  if (o->user_data)
    {
      g_slice_free (NsParamsType, o->user_data);
      o->user_data = NULL;
    }

  G_OBJECT_CLASS (gegl_op_parent_class)->finalize (object);
}

276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311
static gboolean
c_process (GeglOperation       *operation,
           void                *out_buf,
           glong                n_pixels,
           const GeglRectangle *roi,
           gint                 level)
{
  GeglProperties *o = GEGL_PROPERTIES (operation);
  gint      x, y;
  gfloat    val;
  gfloat   *output = out_buf;

  for (y = roi->y; y < (roi->y + roi->height); y++)
    {
      for (x = roi->x; x < (roi->x + roi->width); x++)
        {
          val = noise (((gdouble) x / o->width), ((gdouble) y / o->height), o);
          *output = val;
          output++;
        }
    }

  return TRUE;
}

static gboolean
process (GeglOperation       *operation,
         GeglBuffer          *out_buf,
         const GeglRectangle *roi,
         gint                 level)
{
  GeglBufferIterator *iter;
  const Babl         *format = gegl_operation_get_format (operation,
                                                          "output");

  iter = gegl_buffer_iterator_new (out_buf, roi, level, format,
312
                                   GEGL_ACCESS_WRITE, GEGL_ABYSS_NONE, 1);
313 314

  while (gegl_buffer_iterator_next (iter))
315
    c_process (operation, iter->items[0].data, iter->length, &iter->items[0].roi, level);
316 317 318 319 320 321 322 323 324 325 326 327 328

  return  TRUE;
}

static GeglRectangle
get_bounding_box (GeglOperation *operation)
{
  return gegl_rectangle_infinite_plane ();
}

static void
gegl_op_class_init (GeglOpClass *klass)
{
329
  GObjectClass             *object_class;
330 331 332
  GeglOperationClass       *operation_class;
  GeglOperationSourceClass *source_class;

333
  object_class    = G_OBJECT_CLASS (klass);
334 335 336
  operation_class = GEGL_OPERATION_CLASS (klass);
  source_class = GEGL_OPERATION_SOURCE_CLASS (klass);

337 338
  object_class->finalize = finalize;

339 340 341 342 343 344 345 346 347 348
  source_class->process = process;
  operation_class->get_bounding_box = get_bounding_box;
  operation_class->prepare = prepare;
  operation_class->opencl_support = FALSE;

  gegl_operation_class_set_keys (operation_class,
    "name",               "gegl:noise-solid",
    "title",              _("Solid Noise"),
    "categories",         "render",
    "position-dependent", "true",
349
    "reference-hash",     "db948cc7b2956b5459f7260907c8810c",
350 351 352 353 354 355
    "license",            "GPL3+",
    "description", _("Create a random cloud-like texture"),
    NULL);
}

#endif