map-relative.c 5.93 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
/* 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 2011 Michael Muré <batolettre@gmail.com>
 *
 */


21 22
#ifdef GEGL_PROPERTIES

23 24 25 26 27 28 29
property_double (scaling, _("Scaling"), 1.0)
  description   (_("scaling factor of displacement, indicates how large spatial"
              " displacement a relative mapping value of 1.0 corresponds to."))
  value_range (0.0, 5000.0)

property_enum (sampler_type, _("Resampling method"),
    GeglSamplerType, gegl_sampler_type, GEGL_SAMPLER_CUBIC)
30

31 32
#else

33
#define GEGL_OP_COMPOSER
34
#define GEGL_OP_NAME     map_relative
35
#define GEGL_OP_C_SOURCE map-relative.c
36

37 38
#include "config.h"
#include <glib/gi18n-lib.h>
39
#include "gegl-op.h"
40 41 42 43 44


static void
prepare (GeglOperation *operation)
{
45
  const Babl *format = babl_format ("RGBA float");
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66

  gegl_operation_set_format (operation, "input", format);
  gegl_operation_set_format (operation, "aux", babl_format_n (babl_type ("float"), 2));
  gegl_operation_set_format (operation, "output", format);
}

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

  return result;
}

static gboolean
process (GeglOperation       *operation,
         GeglBuffer          *input,
         GeglBuffer          *aux,
         GeglBuffer          *output,
67 68
         const GeglRectangle *result,
         gint                 level)
69
{
70
  GeglProperties       *o = GEGL_PROPERTIES (operation);
71
  const Babl           *format_io, *format_coords;
72 73 74 75 76 77 78
  GeglSampler          *sampler;
  GeglBufferIterator   *it;
  gint                  index_in, index_out, index_coords;

  format_io = babl_format ("RGBA float");
  format_coords = babl_format_n (babl_type ("float"), 2);

79
  sampler = gegl_buffer_sampler_new_at_level (input, format_io, o->sampler_type, level);
80 81 82

  if (aux != NULL)
    {
83 84
      it = gegl_buffer_iterator_new (output, result, level, format_io,
                                     GEGL_ACCESS_WRITE, GEGL_ABYSS_NONE);
85 86
      index_out = 0;

87 88 89 90
      index_coords = gegl_buffer_iterator_add (it, aux, result, level, format_coords,
                                               GEGL_ACCESS_READ, GEGL_ABYSS_NONE);
      index_in = gegl_buffer_iterator_add (it, input, result, level, format_io,
                                           GEGL_ACCESS_READ, GEGL_ABYSS_NONE);
91 92 93 94 95 96 97

      while (gegl_buffer_iterator_next (it))
        {
          gint        i;
          gint        n_pixels = it->length;
          gint        x = it->roi->x; /* initial x                   */
          gint        y = it->roi->y; /*           and y coordinates */
98
          gdouble     scaling = GEGL_PROPERTIES (operation)->scaling;
99 100 101 102 103 104
          gfloat     *in = it->data[index_in];
          gfloat     *out = it->data[index_out];
          gfloat     *coords = it->data[index_coords];

          for (i=0; i<n_pixels; i++)
            {
105 106
              /* if the coordinate asked is an exact pixel, we fetch it
               * directly, to avoid the blur of sampling */
107 108 109 110 111 112 113 114 115
              if (coords[0] == 0 && coords[1] == 0)
                {
                  out[0] = in[0];
                  out[1] = in[1];
                  out[2] = in[2];
                  out[3] = in[3];
                }
              else
                {
116
                  gegl_sampler_get (sampler, x+coords[0] * scaling,
117
                                             y+coords[1] * scaling,
118 119
                                             NULL, out,
                                             GEGL_ABYSS_NONE);
120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138
                }

              coords += 2;
              in += 4;
              out += 4;

              /* update x and y coordinates */
              x++;
              if (x >= (it->roi->x + it->roi->width))
                {
                  x = it->roi->x;
                  y++;
                }

            }
        }
    }
  else
    {
139 140
      gegl_buffer_copy (input, result, GEGL_ABYSS_NONE,
                        output, result);
141 142 143 144 145 146 147 148
    }

  g_object_unref (sampler);

  return TRUE;
}

static void
149
gegl_op_class_init (GeglOpClass *klass)
150 151 152
{
  GeglOperationClass         *operation_class;
  GeglOperationComposerClass *composer_class;
153
  gchar                      *composition = 
154
    "<gegl>"
155 156 157 158 159 160 161 162 163
    "<node operation='gegl:crop' width='200' height='200'/>"
    "<node operation='gegl:over'>"
      "<node operation='gegl:map-relative'>"
      "  <params>"
      "    <param name='scaling'>30</param>"
      "  </params>"
      "  <node operation='gegl:perlin-noise' />"
      "</node>"
      "<node operation='gegl:load' path='standard-input.png'/>"
164
    "</node>"
165
    "<node operation='gegl:checkerboard' color1='rgb(0.25,0.25,0.25)' color2='rgb(0.75,0.75,0.75)'/>"
166
    "</gegl>";
167

168

169 170 171 172 173 174 175
  operation_class = GEGL_OPERATION_CLASS (klass);
  composer_class  = GEGL_OPERATION_COMPOSER_CLASS (klass);

  composer_class->process = process;
  operation_class->prepare = prepare;
  operation_class->get_required_for_output = get_required_for_output;

176
  gegl_operation_class_set_keys (operation_class,
177 178
    "name",        "gegl:map-relative",
    "title",       _("Map Relative"),
179
    "categories" , "map",
180
    "description", _("sample input with an auxiliary buffer that contain relative source coordinates"),
181
    "reference-composition", composition,
182
    NULL);
183 184
}
#endif