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

20
#ifdef GEGL_PROPERTIES
21

22
property_enum (sampler_type, _("Resampling method"),
23
    GeglSamplerType, gegl_sampler_type,
24
    GEGL_SAMPLER_CUBIC)
25

26 27
#else

28
#define GEGL_OP_COMPOSER
29
#define GEGL_OP_C_SOURCE map-absolute.c
30

31 32
#include "config.h"
#include <glib/gi18n-lib.h>
33
#include "gegl-op.h"
34 35


36 37
static void
prepare (GeglOperation *operation)
38
{
39
  const Babl *format = babl_format ("RGBA float");
40 41 42 43 44 45

  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);
}

46 47
static GeglRectangle
get_required_for_output (GeglOperation       *operation,
48 49
                         const gchar         *input_pad,
                         const GeglRectangle *region)
50
{
51
  GeglRectangle result = *gegl_operation_source_get_bounding_box (operation, "input");
52

53
  return result;
54 55
}

56 57 58 59 60
static gboolean
process (GeglOperation       *operation,
         GeglBuffer          *input,
         GeglBuffer          *aux,
         GeglBuffer          *output,
61 62
         const GeglRectangle *result,
         gint                 level)
63
{
64
  GeglProperties       *o = GEGL_PROPERTIES (operation);
65
  const Babl           *format_io, *format_coords;
66 67
  GeglSampler          *sampler;
  GeglBufferIterator   *it;
68
  gint                  index_in, index_out, index_coords;
69

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

73 74
  sampler = gegl_buffer_sampler_new_at_level (input, format_io, o->sampler_type,
                                              level);
75

76 77
  if (aux != NULL)
    {
78 79
      it = gegl_buffer_iterator_new (output, result, level, format_io,
                                     GEGL_ACCESS_WRITE, GEGL_ABYSS_NONE);
80
      index_out = 0;
81

82 83 84 85
      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);
86

87
      while (gegl_buffer_iterator_next (it))
88
        {
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108
          gint        i;
          gint        n_pixels = it->length;
          gint        x = it->roi->x; /* initial x                   */
          gint        y = it->roi->y; /*           and y coordinates */
          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++)
            {
              /* if the coordinate asked is an exact pixel, we fetch it directly, to avoid the blur of sampling */
              if (coords[0] == x && coords[1] == y)
                {
                  out[0] = in[0];
                  out[1] = in[1];
                  out[2] = in[2];
                  out[3] = in[3];
                }
              else
                {
109
                  gegl_sampler_get (sampler, coords[0], coords[1], NULL, out, GEGL_ABYSS_NONE);
110 111 112 113 114 115 116 117 118 119 120 121 122 123 124
                }

              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++;
                }

            }
125
        }
126 127
    }
  else
128 129 130
    {
      gegl_buffer_copy (input, result, output, result);
    }
131

132
  g_object_unref (sampler);
133

134 135 136 137
  return TRUE;
}

static void
138
gegl_op_class_init (GeglOpClass *klass)
139 140 141 142 143 144 145 146 147
{
  GeglOperationClass         *operation_class;
  GeglOperationComposerClass *composer_class;

  operation_class = GEGL_OPERATION_CLASS (klass);
  composer_class  = GEGL_OPERATION_COMPOSER_CLASS (klass);

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

150
  gegl_operation_class_set_keys (operation_class,
151 152
    "name",              "gegl:map-absolute",
    "title",              _("Map Absolute"),
153
    "categories",        "map",
154
    "position-dependent", "true",
155 156
    "description", _("sample input with an auxiliary buffer that contain absolute source coordinates"),
    NULL);
157 158
}
#endif