Blame operations/workshop/noise-spread.c

Packit bc1512
/* This file is an image processing operation for GEGL
Packit bc1512
 *
Packit bc1512
 * GEGL is free software; you can redistribute it and/or
Packit bc1512
 * modify it under the terms of the GNU Lesser General Public
Packit bc1512
 * License as published by the Free Software Foundation; either
Packit bc1512
 * version 3 of the License, or (at your option) any later version.
Packit bc1512
 *
Packit bc1512
 * GEGL is distributed in the hope that it will be useful,
Packit bc1512
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit bc1512
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit bc1512
 * Lesser General Public License for more details.
Packit bc1512
 *
Packit bc1512
 * You should have received a copy of the GNU Lesser General Public
Packit bc1512
 * License along with GEGL; if not, see <http://www.gnu.org/licenses/>.
Packit bc1512
 *
Packit bc1512
 * Copyright (C) 2011 Barak Itkin <lightningismyname@gmail.org>
Packit bc1512
 *
Packit bc1512
 * Based on "Spread" (Noise) GIMP plugin
Packit bc1512
 * Copyright (C) 1997 Brian Degenhardt and Federico Mena Quintero
Packit bc1512
 *
Packit bc1512
 * The workshop/whirl-pinch.c and common/pixelise.c were used as
Packit bc1512
 * templates for this op file.
Packit bc1512
 */
Packit bc1512
#include "config.h"
Packit bc1512
#include <glib/gi18n-lib.h>
Packit bc1512
Packit bc1512
#ifdef GEGL_CHANT_PROPERTIES
Packit bc1512
Packit bc1512
gegl_chant_int (x_amount, _("Horizontal"), 0, 256, 5,
Packit bc1512
   _("Horizontal spread amount"))
Packit bc1512
gegl_chant_int (y_amount, _("Vertical"), 0, 256, 5,
Packit bc1512
   _("Vertical spread amount"))
Packit bc1512
Packit bc1512
#else
Packit bc1512
Packit bc1512
#define GEGL_CHANT_TYPE_AREA_FILTER
Packit bc1512
#define GEGL_CHANT_C_FILE       "noise-spread.c"
Packit bc1512
Packit bc1512
#include "gegl-chant.h"
Packit bc1512
#include <math.h>
Packit bc1512
Packit bc1512
static void
Packit bc1512
calc_sample_coords (gint      src_x,
Packit bc1512
                    gint      src_y,
Packit bc1512
                    gint      x_amount,
Packit bc1512
                    gint      y_amount,
Packit bc1512
                    GRand    *gr,
Packit bc1512
                    gdouble  *x,
Packit bc1512
                    gdouble  *y)
Packit bc1512
{
Packit bc1512
  gdouble        angle;
Packit bc1512
  gint           xdist, ydist;
Packit bc1512
Packit bc1512
  /* get random angle, x distance, and y distance */
Packit bc1512
  xdist = (x_amount > 0
Packit bc1512
           ? g_rand_int_range (gr, -x_amount, x_amount)
Packit bc1512
           : 0);
Packit bc1512
  ydist = (y_amount > 0
Packit bc1512
           ? g_rand_int_range (gr, -y_amount, y_amount)
Packit bc1512
           : 0);
Packit bc1512
  angle = g_rand_double_range (gr, -G_PI, G_PI);
Packit bc1512
Packit bc1512
  *x = src_x + floor (sin (angle) * xdist);
Packit bc1512
  *y = src_y + floor (cos (angle) * ydist);
Packit bc1512
}
Packit bc1512
Packit bc1512
/* Apply the actual transform */
Packit bc1512
static void
Packit bc1512
apply_spread (gint                 x_amount,
Packit bc1512
              gint                 y_amount,
Packit bc1512
              gint                 img_width,
Packit bc1512
              gint                 img_height,
Packit bc1512
              const Babl          *format,
Packit bc1512
              GeglBuffer          *src,
Packit bc1512
              GeglBuffer          *dst,
Packit bc1512
              const GeglRectangle *roi)
Packit bc1512
{
Packit bc1512
  gfloat *dst_buf;
Packit bc1512
  gint x1, y1; // Noise image
Packit bc1512
  gdouble x, y;   // Source Image
Packit bc1512
Packit bc1512
  GRand    *gr = g_rand_new ();
Packit bc1512
Packit bc1512
  /* Get buffer in which to place dst pixels. */
Packit bc1512
  dst_buf = g_new0 (gfloat, roi->width * roi->height * 4);
Packit bc1512
Packit bc1512
  for (y1 = 0; y1 < roi->height; y1++) {
Packit bc1512
    for (x1 = 0; x1 < roi->width; x1++) {
Packit bc1512
Packit bc1512
      calc_sample_coords (x1, y1, x_amount, y_amount, gr, &x, &y);
Packit bc1512
      /* Only displace the pixel if it's within the bounds of the image. */
Packit bc1512
      if (x >= 0 && x < img_width && y >= 0 && y < img_height)
Packit bc1512
        gegl_buffer_sample (src, x, y, NULL, &dst_buf[(y1 * roi->width + x1) * 4], format,
Packit bc1512
                            GEGL_SAMPLER_LINEAR, GEGL_ABYSS_NONE);
Packit bc1512
      else /* Else just copy it */
Packit bc1512
        gegl_buffer_sample (src, x1, y1, NULL, &dst_buf[(y1 * roi->width + x1) * 4], format,
Packit bc1512
                            GEGL_SAMPLER_LINEAR, GEGL_ABYSS_NONE);
Packit bc1512
    } /* for */
Packit bc1512
  } /* for */
Packit bc1512
Packit bc1512
  gegl_buffer_sample_cleanup (src);
Packit bc1512
Packit bc1512
  /* Store dst pixels. */
Packit bc1512
  gegl_buffer_set (dst, roi, 0, format, dst_buf, GEGL_AUTO_ROWSTRIDE);
Packit bc1512
Packit bc1512
  gegl_buffer_flush(dst);
Packit bc1512
Packit bc1512
  g_free (dst_buf);
Packit bc1512
  g_rand_free (gr);
Packit bc1512
}
Packit bc1512
Packit bc1512
/*****************************************************************************/
Packit bc1512
Packit bc1512
static void
Packit bc1512
prepare (GeglOperation *operation)
Packit bc1512
{
Packit bc1512
  GeglChantO              *o;
Packit bc1512
  GeglOperationAreaFilter *op_area;
Packit bc1512
Packit bc1512
  op_area = GEGL_OPERATION_AREA_FILTER (operation);
Packit bc1512
  o       = GEGL_CHANT_PROPERTIES (operation);
Packit bc1512
Packit bc1512
  op_area->left   =
Packit bc1512
  op_area->right  = o->x_amount;
Packit bc1512
  op_area->top    =
Packit bc1512
  op_area->bottom = o->y_amount;
Packit bc1512
Packit bc1512
  gegl_operation_set_format (operation, "output",
Packit bc1512
                             babl_format ("RaGaBaA float"));
Packit bc1512
}
Packit bc1512
Packit bc1512
/* Perform the specified operation.
Packit bc1512
 */
Packit bc1512
static gboolean
Packit bc1512
process (GeglOperation       *operation,
Packit bc1512
         GeglBuffer          *input,
Packit bc1512
         GeglBuffer          *output,
Packit bc1512
         const GeglRectangle *result,
Packit bc1512
         gint                 level)
Packit bc1512
{
Packit bc1512
  GeglChantO *o = GEGL_CHANT_PROPERTIES (operation);
Packit bc1512
  GeglRectangle boundary = gegl_operation_get_bounding_box (operation);
Packit bc1512
  const Babl *format = babl_format ("RaGaBaA float");
Packit bc1512
Packit bc1512
  apply_spread ((o->x_amount + 1) / 2,
Packit bc1512
                (o->y_amount + 1) / 2,
Packit bc1512
                boundary.width - 2 * o->x_amount,
Packit bc1512
                boundary.height - 2 * o->y_amount,
Packit bc1512
                format,
Packit bc1512
                input,
Packit bc1512
                output,
Packit bc1512
                result);
Packit bc1512
  return TRUE;
Packit bc1512
}
Packit bc1512
Packit bc1512
Packit bc1512
static void
Packit bc1512
gegl_chant_class_init (GeglChantClass *klass)
Packit bc1512
{
Packit bc1512
  GeglOperationClass       *operation_class;
Packit bc1512
  GeglOperationFilterClass *filter_class;
Packit bc1512
Packit bc1512
  operation_class = GEGL_OPERATION_CLASS (klass);
Packit bc1512
  filter_class    = GEGL_OPERATION_FILTER_CLASS (klass);
Packit bc1512
Packit bc1512
  filter_class->process    = process;
Packit bc1512
  operation_class->prepare = prepare;
Packit bc1512
Packit bc1512
  gegl_operation_class_set_keys (operation_class,
Packit bc1512
    "categories" , "noise",
Packit bc1512
    "name"       , "gegl:noise-spread",
Packit bc1512
    "description", _("Noise spread filter"),
Packit bc1512
    NULL);
Packit bc1512
}
Packit bc1512
Packit bc1512
Packit bc1512
#endif