map-common.h 11.2 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
/* 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 <https://www.gnu.org/licenses/>.
 *
 * Copyright 2011 Michael Muré <batolettre@gmail.com>
 *
 */


#include <math.h>


#define EPSILON 1e-6


static void
prepare (GeglOperation *operation)
{
  const Babl *space = gegl_operation_get_source_space (operation, "input");
  const Babl *format = babl_format_with_space ("RGBA float", space);

  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)
{
43 44
  GeglProperties *o = GEGL_PROPERTIES (operation);

45 46 47 48 49 50 51 52
  if (! strcmp (input_pad, "input"))
    {
      return *gegl_operation_source_get_bounding_box (operation, "input");
    }
  else
    {
      GeglRectangle rect = *region;

53 54 55 56 57 58 59
      if (o->sampler_type != GEGL_SAMPLER_NEAREST)
        {
          rect.x      -= 1;
          rect.y      -= 1;
          rect.width  += 2;
          rect.height += 2;
        }
60 61 62 63 64 65 66 67 68 69

      return rect;
    }
}

static GeglRectangle
get_invalidated_by_change (GeglOperation       *operation,
                           const gchar         *input_pad,
                           const GeglRectangle *region)
{
70 71
  GeglProperties *o = GEGL_PROPERTIES (operation);

72 73 74 75 76 77 78 79
  if (! strcmp (input_pad, "input"))
    {
      return gegl_operation_get_bounding_box (operation);
    }
  else
    {
      GeglRectangle rect = *region;

80 81 82 83 84 85 86
      if (o->sampler_type != GEGL_SAMPLER_NEAREST)
        {
          rect.x      -= 1;
          rect.y      -= 1;
          rect.width  += 2;
          rect.height += 2;
        }
87 88 89 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 124 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 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 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 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 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329

      return rect;
    }
}

static gboolean
process (GeglOperation       *operation,
         GeglBuffer          *input,
         GeglBuffer          *aux,
         GeglBuffer          *output,
         const GeglRectangle *result,
         gint                 level)
{
  GeglProperties     *o = GEGL_PROPERTIES (operation);
  const Babl         *format_io, *format_coords;
  GeglSampler        *sampler;
  GeglBufferIterator *it;
  gint                index_in, index_out, index_coords;

  format_io = gegl_operation_get_format (operation, "output");
  format_coords = babl_format_n (babl_type ("float"), 2);

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

  if (aux != NULL
#ifdef MAP_RELATIVE
      && fabs (o->scaling) > EPSILON
#endif
     )
    {
      it = gegl_buffer_iterator_new (output, result, level, format_io,
                                     GEGL_ACCESS_WRITE, GEGL_ABYSS_NONE, 3);
      index_out = 0;

      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, o->abyss_policy);

      while (gegl_buffer_iterator_next (it))
        {
          gint        c;
          gint        r;
          gfloat      x;
          gfloat      y;
#ifdef MAP_RELATIVE
          gdouble     scaling = GEGL_PROPERTIES (operation)->scaling;
          gdouble     scaling_2 = scaling / 2.0;
#endif
          gfloat     *in = it->items[index_in].data;
          gfloat     *out = it->items[index_out].data;
          gfloat     *coords = it->items[index_coords].data;
          GeglRectangle *roi = &it->items[0].roi;

          y = roi->y + 0.5; /* initial y coordinate */

          if (o->sampler_type == GEGL_SAMPLER_NEAREST)
            {
              for (r = 0; r < roi->height; r++, y++)
                {
                  x = roi->x + 0.5; /* initial x coordinate */

                  for (c = 0; c < roi->width; c++, x++)
                    {
                      /* if the coordinate asked is an exact pixel, we
                       * fetch it directly
                       */
#ifdef MAP_RELATIVE
                      if (coords[0] == 0.0f && coords[1] == 0.0f)
#else
                      if (coords[0] == x    && coords[1] == y)
#endif
                        {
                          out[0] = in[0];
                          out[1] = in[1];
                          out[2] = in[2];
                          out[3] = in[3];
                        }
                      else
                        {
                          gdouble coords_x = coords[0];
                          gdouble coords_y = coords[1];

#ifdef MAP_RELATIVE
                          coords_x = x + coords_x * scaling;
                          coords_y = y + coords_y * scaling;
#endif

                          gegl_sampler_get (sampler,
                                            coords_x, coords_y,
                                            NULL, out,
                                            o->abyss_policy);
                        }

                      coords += 2;
                      in += 4;
                      out += 4;
                    }
                }
            }
          else
            {
              gint   stride = 2 * roi->width;
              gfloat coords_top[2 * roi->width];
              gfloat coords_bottom[2 * roi->width];
              gfloat coords_left[2 * roi->height];
              gfloat coords_right[2 * roi->height];

              gegl_buffer_get (aux,
                               GEGL_RECTANGLE (roi->x, roi->y - 1,
                                               roi->width, 1),
                               1.0, format_coords, coords_top,
                               GEGL_AUTO_ROWSTRIDE, GEGL_ABYSS_CLAMP);
              gegl_buffer_get (aux,
                               GEGL_RECTANGLE (roi->x, roi->y + roi->height,
                                               roi->width, 1),
                               1.0, format_coords, coords_bottom,
                               GEGL_AUTO_ROWSTRIDE, GEGL_ABYSS_CLAMP);
              gegl_buffer_get (aux,
                               GEGL_RECTANGLE (roi->x - 1, roi->y,
                                               1, roi->height),
                               1.0, format_coords, coords_left,
                               GEGL_AUTO_ROWSTRIDE, GEGL_ABYSS_CLAMP);
              gegl_buffer_get (aux,
                               GEGL_RECTANGLE (roi->x + roi->width, roi->y,
                                               1, roi->height),
                               1.0, format_coords, coords_right,
                               GEGL_AUTO_ROWSTRIDE, GEGL_ABYSS_CLAMP);

              for (r = 0; r < roi->height; r++, y++)
                {
                  x = roi->x + 0.5; /* initial x coordinate */

                  for (c = 0; c < roi->width; c++, x++)
                    {
                      GeglBufferMatrix2 scale;

                      if (c < roi->width - 1)
                        {
                          scale.coeff[0][0] = coords[2];
                          scale.coeff[1][0] = coords[3];
                        }
                      else
                        {
                          scale.coeff[0][0] = coords_right[2 * r + 0];
                          scale.coeff[1][0] = coords_right[2 * r + 1];
                        }

                      if (c > 0)
                        {
                          scale.coeff[0][0] -= coords[-2];
                          scale.coeff[1][0] -= coords[-1];
                        }
                      else
                        {
                          scale.coeff[0][0] -= coords_left[2 * r + 0];
                          scale.coeff[1][0] -= coords_left[2 * r + 1];
                        }

                      if (r < roi->height - 1)
                        {
                          scale.coeff[0][1] = coords[stride + 0];
                          scale.coeff[1][1] = coords[stride + 1];
                        }
                      else
                        {
                          scale.coeff[0][1] = coords_bottom[2 * c + 0];
                          scale.coeff[1][1] = coords_bottom[2 * c + 1];
                        }

                      if (r > 0)
                        {
                          scale.coeff[0][1] -= coords[-stride + 0];
                          scale.coeff[1][1] -= coords[-stride + 1];
                        }
                      else
                        {
                          scale.coeff[0][1] -= coords_top[2 * c + 0];
                          scale.coeff[1][1] -= coords_top[2 * c + 1];
                        }

#ifdef MAP_RELATIVE
                      scale.coeff[0][0] = scale.coeff[0][0] * scaling_2 + 1.0;
                      scale.coeff[0][1] = scale.coeff[0][1] * scaling_2;
                      scale.coeff[1][0] = scale.coeff[1][0] * scaling_2;
                      scale.coeff[1][1] = scale.coeff[1][1] * scaling_2 + 1.0;
#else
                      scale.coeff[0][0] /= 2.0;
                      scale.coeff[0][1] /= 2.0;
                      scale.coeff[1][0] /= 2.0;
                      scale.coeff[1][1] /= 2.0;
#endif

                      /* if the coordinate asked is an exact pixel, we fetch it
                       * directly, to avoid the blur of sampling
                       */
#ifdef MAP_RELATIVE
                      if (coords[0] == 0.0f && coords[1] == 0.0f &&
#else
                      if (coords[0] == x    && coords[1] == y    &&
#endif
                          gegl_buffer_matrix2_is_identity (&scale))
                        {
                          out[0] = in[0];
                          out[1] = in[1];
                          out[2] = in[2];
                          out[3] = in[3];
                        }
                      else
                        {
                          gdouble coords_x = coords[0];
                          gdouble coords_y = coords[1];

#ifdef MAP_RELATIVE
                          coords_x = x + coords_x * scaling;
                          coords_y = y + coords_y * scaling;
#endif

                          gegl_sampler_get (sampler,
                                            coords_x, coords_y,
                                            &scale, out,
                                            o->abyss_policy);
                        }

                      coords += 2;
                      in += 4;
                      out += 4;
                    }
                }
            }
        }
    }
  else
    {
      gegl_buffer_copy (input, result, o->abyss_policy,
                        output, result);
    }

  g_object_unref (sampler);

  return TRUE;
}