gimpoperationcagetransform.c 24.9 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
/* GIMP - The GNU Image Manipulation Program
 *
 * gimpoperationcage.c
 * Copyright (C) 2010 Michael Muré <batolettre@gmail.com>
 *
 * 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.
 *
 * This program 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 General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
17
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
18 19 20 21
 */

#include "config.h"

22
#include <cairo.h>
23
#include <gegl.h>
24
#include <gdk-pixbuf/gdk-pixbuf.h>
25 26 27 28

#include "libgimpcolor/gimpcolor.h"
#include "libgimpmath/gimpmath.h"

29
#include "operations-types.h"
30

31 32 33
#include "gimpoperationcagetransform.h"
#include "gimpcageconfig.h"

34
#include "gimp-intl.h"
35

36 37 38 39
enum
{
  PROP_0,
  PROP_CONFIG,
40
  PROP_FILL,
41 42
};

43

44 45 46 47 48 49 50 51 52 53 54 55 56 57
static void         gimp_operation_cage_transform_finalize                (GObject             *object);
static void         gimp_operation_cage_transform_get_property            (GObject             *object,
                                                                           guint                property_id,
                                                                           GValue              *value,
                                                                           GParamSpec          *pspec);
static void         gimp_operation_cage_transform_set_property            (GObject             *object,
                                                                           guint                property_id,
                                                                           const GValue        *value,
                                                                           GParamSpec          *pspec);
static void         gimp_operation_cage_transform_prepare                 (GeglOperation       *operation);
static gboolean     gimp_operation_cage_transform_process                 (GeglOperation       *operation,
                                                                           GeglBuffer          *in_buf,
                                                                           GeglBuffer          *aux_buf,
                                                                           GeglBuffer          *out_buf,
58 59
                                                                           const GeglRectangle *roi,
                                                                           gint                 level);
60 61 62 63 64 65 66 67 68 69 70 71 72
static void         gimp_operation_cage_transform_interpolate_source_coords_recurs
                                                                          (GimpOperationCageTransform  *oct,
                                                                           GeglBuffer          *out_buf,
                                                                           const GeglRectangle *roi,
                                                                           GimpVector2          p1_s,
                                                                           GimpVector2          p1_d,
                                                                           GimpVector2          p2_s,
                                                                           GimpVector2          p2_d,
                                                                           GimpVector2          p3_s,
                                                                           GimpVector2          p3_d,
                                                                           gint                 recursion_depth,
                                                                           gfloat              *coords);
static GimpVector2  gimp_cage_transform_compute_destination               (GimpCageConfig      *config,
73
                                                                           gfloat              *coef,
74
                                                                           GeglSampler         *coef_sampler,
75 76 77 78 79 80 81 82
                                                                           GimpVector2          coords);
GeglRectangle       gimp_operation_cage_transform_get_cached_region       (GeglOperation       *operation,
                                                                           const GeglRectangle *roi);
GeglRectangle       gimp_operation_cage_transform_get_required_for_output (GeglOperation       *operation,
                                                                           const gchar         *input_pad,
                                                                           const GeglRectangle *roi);
GeglRectangle       gimp_operation_cage_transform_get_bounding_box        (GeglOperation       *operation);

83

84
G_DEFINE_TYPE (GimpOperationCageTransform, gimp_operation_cage_transform,
85
                      GEGL_TYPE_OPERATION_COMPOSER)
86 87 88 89 90 91 92

#define parent_class gimp_operation_cage_transform_parent_class


static void
gimp_operation_cage_transform_class_init (GimpOperationCageTransformClass *klass)
{
93 94 95
  GObjectClass               *object_class    = G_OBJECT_CLASS (klass);
  GeglOperationClass         *operation_class = GEGL_OPERATION_CLASS (klass);
  GeglOperationComposerClass *filter_class    = GEGL_OPERATION_COMPOSER_CLASS (klass);
96

97 98 99
  object_class->get_property               = gimp_operation_cage_transform_get_property;
  object_class->set_property               = gimp_operation_cage_transform_set_property;
  object_class->finalize                   = gimp_operation_cage_transform_finalize;
100

101
  gegl_operation_class_set_keys (operation_class,
102 103 104 105
                                 "name",        "gimp:cage-transform",
                                 "categories",  "transform",
                                 "description", _("Convert a set of coefficient buffer to a coordinate buffer for the GIMP cage tool"),
                                 NULL);
106

107
  operation_class->prepare                 = gimp_operation_cage_transform_prepare;
108

109 110 111 112
  operation_class->get_required_for_output = gimp_operation_cage_transform_get_required_for_output;
  operation_class->get_cached_region       = gimp_operation_cage_transform_get_cached_region;
  operation_class->no_cache                = FALSE;
  operation_class->get_bounding_box        = gimp_operation_cage_transform_get_bounding_box;
Jehan's avatar
Jehan committed
113 114
  /* XXX Temporarily disable multi-threading on this operation because
   * it is much faster when single-threaded. See bug 787663.
115 116
   */
  operation_class->threaded                = FALSE;
117

118
  filter_class->process                    = gimp_operation_cage_transform_process;
119

120
  g_object_class_install_property (object_class, PROP_CONFIG,
121
                                   g_param_spec_object ("config",
122 123
                                                        "Config",
                                                        "A GimpCageConfig object, that define the transformation",
124 125 126
                                                        GIMP_TYPE_CAGE_CONFIG,
                                                        G_PARAM_READWRITE |
                                                        G_PARAM_CONSTRUCT));
127

128 129
  g_object_class_install_property (object_class, PROP_FILL,
                                   g_param_spec_boolean ("fill-plain-color",
130 131
                                                         _("Fill with plain color"),
                                                         _("Fill the original position of the cage with a plain color"),
132 133
                                                         FALSE,
                                                         G_PARAM_READWRITE));
134 135 136 137 138
}

static void
gimp_operation_cage_transform_init (GimpOperationCageTransform *self)
{
139
  self->format_coords = babl_format_n(babl_type("float"), 2);
140 141 142
}

static void
143
gimp_operation_cage_transform_finalize (GObject *object)
144 145 146
{
  GimpOperationCageTransform *self = GIMP_OPERATION_CAGE_TRANSFORM (object);

147
  g_clear_object (&self->config);
148

149 150 151 152
  G_OBJECT_CLASS (parent_class)->finalize (object);
}

static void
153 154 155 156
gimp_operation_cage_transform_get_property (GObject    *object,
                                            guint       property_id,
                                            GValue     *value,
                                            GParamSpec *pspec)
157 158 159 160 161
{
  GimpOperationCageTransform *self = GIMP_OPERATION_CAGE_TRANSFORM (object);

  switch (property_id)
    {
162
    case PROP_CONFIG:
163 164
      g_value_set_object (value, self->config);
      break;
165
    case PROP_FILL:
166 167 168
      g_value_set_boolean (value, self->fill_plain_color);
      break;

169 170 171 172 173 174 175
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
      break;
    }
}

static void
176 177 178 179
gimp_operation_cage_transform_set_property (GObject      *object,
                                            guint         property_id,
                                            const GValue *value,
                                            GParamSpec   *pspec)
180 181 182 183 184
{
  GimpOperationCageTransform *self = GIMP_OPERATION_CAGE_TRANSFORM (object);

  switch (property_id)
    {
185
    case PROP_CONFIG:
186 187 188 189
      if (self->config)
        g_object_unref (self->config);
      self->config = g_value_dup_object (value);
      break;
190
    case PROP_FILL:
191 192 193 194
      self->fill_plain_color = g_value_get_boolean (value);
      break;

    default:
195 196 197 198 199 200
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
      break;
    }
}

static void
201
gimp_operation_cage_transform_prepare (GeglOperation *operation)
202
{
203 204 205 206 207
  GimpOperationCageTransform *oct    = GIMP_OPERATION_CAGE_TRANSFORM (operation);
  GimpCageConfig             *config = GIMP_CAGE_CONFIG (oct->config);

  gegl_operation_set_format (operation, "input",
                             babl_format_n (babl_type ("float"),
208
                                            2 * gimp_cage_config_get_n_points (config)));
209 210
  gegl_operation_set_format (operation, "output",
                             babl_format_n (babl_type ("float"), 2));
211 212 213 214 215
}

static gboolean
gimp_operation_cage_transform_process (GeglOperation       *operation,
                                       GeglBuffer          *in_buf,
216
                                       GeglBuffer          *aux_buf,
217
                                       GeglBuffer          *out_buf,
218 219
                                       const GeglRectangle *roi,
                                       gint                 level)
220
{
221 222
  GimpOperationCageTransform *oct    = GIMP_OPERATION_CAGE_TRANSFORM (operation);
  GimpCageConfig             *config = GIMP_CAGE_CONFIG (oct->config);
223 224
  GeglRectangle               cage_bb;
  gfloat                     *coords;
225
  gfloat                     *coef;
Michael Natterer's avatar
Michael Natterer committed
226
  const Babl                 *format_coef;
227
  GeglSampler                *coef_sampler;
228 229 230
  GimpVector2                 plain_color;
  GeglBufferIterator         *it;
  gint                        x, y;
231
  gboolean                    output_set;
232 233
  GimpCagePoint              *point;
  guint                       n_cage_vertices;
234

Alexia Death's avatar
Alexia Death committed
235
  /* pre-fill the out buffer with no-displacement coordinate */
236
  it      = gegl_buffer_iterator_new (out_buf, roi, 0, NULL,
237
                                      GEGL_ACCESS_WRITE, GEGL_ABYSS_NONE, 1);
238
  cage_bb = gimp_cage_config_get_bounding_box (config);
Alexia Death's avatar
Alexia Death committed
239

240 241 242 243
  point = &(g_array_index (config->cage_points, GimpCagePoint, 0));
  plain_color.x = (gint) point->src_point.x;
  plain_color.y = (gint) point->src_point.y;

244
  n_cage_vertices = gimp_cage_config_get_n_points (config);
245

246 247
  while (gegl_buffer_iterator_next (it))
    {
248
      /* iterate inside the roi */
249
      gint    n_pixels = it->length;
250
      gfloat *output   = it->items[0].data;
251

252 253
      x = it->items[0].roi.x; /* initial x         */
      y = it->items[0].roi.y; /* and y coordinates */
254

255
      while (n_pixels--)
256
        {
257 258
          output_set = FALSE;
          if (oct->fill_plain_color)
259
            {
260 261 262 263 264 265 266 267 268 269 270 271
              if (x > cage_bb.x &&
                  y > cage_bb.y &&
                  x < cage_bb.x + cage_bb.width &&
                  y < cage_bb.y + cage_bb.height)
                {
                  if (gimp_cage_config_point_inside (config, x, y))
                    {
                      output[0] = plain_color.x;
                      output[1] = plain_color.y;
                      output_set = TRUE;
                    }
                }
272
            }
273
          if (!output_set)
274 275 276 277 278 279 280 281 282
            {
              output[0] = x;
              output[1] = y;
            }

          output += 2;

          /* update x and y coordinates */
          x++;
283
          if (x >= (it->items[0].roi.x + it->items[0].roi.width))
284
            {
285
              x = it->items[0].roi.x;
286 287 288
              y++;
            }
        }
289
    }
290

291 292 293
  if (! aux_buf)
    return TRUE;

294
  gegl_operation_progress (operation, 0.0, "");
295

296
  /* pre-allocate memory outside of the loop */
297 298 299 300 301
  coords       = g_slice_alloc (2 * sizeof (gfloat));
  coef         = g_malloc (n_cage_vertices * 2 * sizeof (gfloat));
  format_coef  = babl_format_n (babl_type ("float"), 2 * n_cage_vertices);
  coef_sampler = gegl_buffer_sampler_new (aux_buf,
                                          format_coef, GEGL_SAMPLER_NEAREST);
302

303
  /* compute, reverse and interpolate the transformation */
304
  for (y = cage_bb.y; y < cage_bb.y + cage_bb.height - 1; y++)
305
    {
306 307 308
      GimpVector2 p1_d, p2_d, p3_d, p4_d;
      GimpVector2 p1_s, p2_s, p3_s, p4_s;

309 310 311 312 313 314
      p1_s.y = y;
      p2_s.y = y+1;
      p3_s.y = y+1;
      p3_s.x = cage_bb.x;
      p4_s.y = y;
      p4_s.x = cage_bb.x;
315

316 317
      p3_d = gimp_cage_transform_compute_destination (config, coef, coef_sampler, p3_s);
      p4_d = gimp_cage_transform_compute_destination (config, coef, coef_sampler, p4_s);
318

319
      for (x = cage_bb.x; x < cage_bb.x + cage_bb.width - 1; x++)
320 321 322
        {
          p1_s = p4_s;
          p2_s = p3_s;
323 324
          p3_s.x = x+1;
          p4_s.x = x+1;
325 326 327

          p1_d = p4_d;
          p2_d = p3_d;
328 329
          p3_d = gimp_cage_transform_compute_destination (config, coef, coef_sampler, p3_s);
          p4_d = gimp_cage_transform_compute_destination (config, coef, coef_sampler, p4_s);
330

331
          if (gimp_cage_config_point_inside (config, x, y))
332
            {
333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349
              gimp_operation_cage_transform_interpolate_source_coords_recurs (oct,
                                                                              out_buf,
                                                                              roi,
                                                                              p1_s, p1_d,
                                                                              p2_s, p2_d,
                                                                              p3_s, p3_d,
                                                                              0,
                                                                              coords);

              gimp_operation_cage_transform_interpolate_source_coords_recurs (oct,
                                                                              out_buf,
                                                                              roi,
                                                                              p1_s, p1_d,
                                                                              p3_s, p3_d,
                                                                              p4_s, p4_d,
                                                                              0,
                                                                              coords);
350 351
            }
        }
352 353 354 355 356 357 358 359 360

      if ((y - cage_bb.y) % 20 == 0)
        {
          gdouble fraction = ((gdouble) (y - cage_bb.y) /
                              (gdouble) (cage_bb.height));

          /*  0.0 and 1.0 indicate progress start/end, so avoid them  */
          if (fraction > 0.0 && fraction < 1.0)
            {
361
              gegl_operation_progress (operation, fraction, "");
362 363
            }
        }
364 365
    }

366
  g_object_unref (coef_sampler);
367
  g_free (coef);
368 369
  g_slice_free1 (2 * sizeof (gfloat), coords);

370
  gegl_operation_progress (operation, 1.0, "");
371

372 373 374 375 376
  return TRUE;
}


static void
377 378 379 380 381 382 383 384 385 386 387
gimp_operation_cage_transform_interpolate_source_coords_recurs (GimpOperationCageTransform *oct,
                                                                GeglBuffer                 *out_buf,
                                                                const GeglRectangle        *roi,
                                                                GimpVector2                 p1_s,
                                                                GimpVector2                 p1_d,
                                                                GimpVector2                 p2_s,
                                                                GimpVector2                 p2_d,
                                                                GimpVector2                 p3_s,
                                                                GimpVector2                 p3_d,
                                                                gint                        recursion_depth,
                                                                gfloat                     *coords)
388
{
389
  GeglRectangle rect = { 0, 0, 1, 1 };
390
  gint          xmin, xmax, ymin, ymax;
391

Jehan's avatar
Jehan committed
392 393 394
  /* Stop recursion if all 3 vertices of the triangle are outside the
   * ROI (left/right or above/below).
   */
395 396 397 398 399 400 401 402 403 404 405 406 407
  if (p1_d.x >= roi->x + roi->width &&
      p2_d.x >= roi->x + roi->width &&
      p3_d.x >= roi->x + roi->width) return;
  if (p1_d.y >= roi->y + roi->height &&
      p2_d.y >= roi->y + roi->height &&
      p3_d.y >= roi->y + roi->height) return;

  if (p1_d.x < roi->x &&
      p2_d.x < roi->x &&
      p3_d.x < roi->x) return;
  if (p1_d.y < roi->y &&
      p2_d.y < roi->y &&
      p3_d.y < roi->y) return;
408

409 410
  xmin = xmax = p1_d.x;
  ymin = ymax = p1_d.y;
411

412 413
  if (xmin > p2_d.x) xmin = p2_d.x;
  if (xmin > p3_d.x) xmin = p3_d.x;
414

415 416
  if (xmax < p2_d.x) xmax = p2_d.x;
  if (xmax < p3_d.x) xmax = p3_d.x;
417

418 419
  if (ymin > p2_d.y) ymin = p2_d.y;
  if (ymin > p3_d.y) ymin = p3_d.y;
420

421 422
  if (ymax < p2_d.y) ymax = p2_d.y;
  if (ymax < p3_d.y) ymax = p3_d.y;
423

424 425 426
  /* test if there is no more pixel in the triangle */
  if (xmin == xmax || ymin == ymax)
    return;
427 428

  /* test if the triangle is implausibly large as manifested by too deep recursion */
429
  if (recursion_depth > 5)
430 431
    return;

432
  /* test if the triangle is small enough.
433 434 435 436
   *
   * if yes, we compute the coefficient of the barycenter for the
   * pixel (x,y) and see if a pixel is inside (ie the 3 coef have the
   * same sign).
437
   */
438
  if (xmax - xmin == 1 && ymax - ymin == 1)
439 440
    {
      gdouble a, b, c, denom, x, y;
441

442 443
      rect.x = xmax;
      rect.y = ymax;
444

445 446
      x = (gdouble) xmax;
      y = (gdouble) ymax;
447

448 449 450 451
      denom = (p2_d.x - p1_d.x) * p3_d.y + (p1_d.x - p3_d.x) * p2_d.y + (p3_d.x - p2_d.x) * p1_d.y;
      a = ((p2_d.x - x) * p3_d.y + (x - p3_d.x) * p2_d.y + (p3_d.x - p2_d.x) * y) / denom;
      b = - ((p1_d.x - x) * p3_d.y + (x - p3_d.x) * p1_d.y + (p3_d.x - p1_d.x) * y) / denom;
      c = 1.0 - a - b;
452

453 454 455
      /* if a pixel is inside, we compute its source coordinate and
       * set it in the output buffer
       */
456 457 458 459
      if ((a > 0 && b > 0 && c > 0) || (a < 0 && b < 0 && c < 0))
        {
          coords[0] = (a * p1_s.x + b * p2_s.x + c * p3_s.x);
          coords[1] = (a * p1_s.y + b * p2_s.y + c * p3_s.y);
460

461 462
          gegl_buffer_set (out_buf,
                           &rect,
463
                           0,
464 465 466 467
                           oct->format_coords,
                           coords,
                           GEGL_AUTO_ROWSTRIDE);
        }
468

469 470
      return;
    }
471
  else
472 473 474 475 476 477 478 479 480 481 482 483
    {
      /* we cut the triangle in 4 sub-triangle and treat it recursively */
      /*
       *       /\
       *      /__\
       *     /\  /\
       *    /__\/__\
       *
       */

      GimpVector2 pm1_d, pm2_d, pm3_d;
      GimpVector2 pm1_s, pm2_s, pm3_s;
484
      gint        next_depth = recursion_depth + 1;
485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539

      pm1_d.x = (p1_d.x + p2_d.x) / 2.0;
      pm1_d.y = (p1_d.y + p2_d.y) / 2.0;

      pm2_d.x = (p2_d.x + p3_d.x) / 2.0;
      pm2_d.y = (p2_d.y + p3_d.y) / 2.0;

      pm3_d.x = (p3_d.x + p1_d.x) / 2.0;
      pm3_d.y = (p3_d.y + p1_d.y) / 2.0;

      pm1_s.x = (p1_s.x + p2_s.x) / 2.0;
      pm1_s.y = (p1_s.y + p2_s.y) / 2.0;

      pm2_s.x = (p2_s.x + p3_s.x) / 2.0;
      pm2_s.y = (p2_s.y + p3_s.y) / 2.0;

      pm3_s.x = (p3_s.x + p1_s.x) / 2.0;
      pm3_s.y = (p3_s.y + p1_s.y) / 2.0;

      gimp_operation_cage_transform_interpolate_source_coords_recurs (oct,
                                                                      out_buf,
                                                                      roi,
                                                                      p1_s, p1_d,
                                                                      pm1_s, pm1_d,
                                                                      pm3_s, pm3_d,
                                                                      next_depth,
                                                                      coords);

      gimp_operation_cage_transform_interpolate_source_coords_recurs (oct,
                                                                      out_buf,
                                                                      roi,
                                                                      pm1_s, pm1_d,
                                                                      p2_s, p2_d,
                                                                      pm2_s, pm2_d,
                                                                      next_depth,
                                                                      coords);

      gimp_operation_cage_transform_interpolate_source_coords_recurs (oct,
                                                                      out_buf,
                                                                      roi,
                                                                      pm1_s, pm1_d,
                                                                      pm2_s, pm2_d,
                                                                      pm3_s, pm3_d,
                                                                      next_depth,
                                                                      coords);

      gimp_operation_cage_transform_interpolate_source_coords_recurs (oct,
                                                                      out_buf,
                                                                      roi,
                                                                      pm3_s, pm3_d,
                                                                      pm2_s, pm2_d,
                                                                      p3_s, p3_d,
                                                                      next_depth,
                                                                      coords);
    }
540 541
}

542
static GimpVector2
543
gimp_cage_transform_compute_destination (GimpCageConfig *config,
544
                                         gfloat         *coef,
545
                                         GeglSampler    *coef_sampler,
546
                                         GimpVector2     coords)
547
{
548 549
  GimpVector2    result = {0, 0};
  gint           n_cage_vertices = gimp_cage_config_get_n_points (config);
550
  gint           i;
551
  GimpCagePoint *point;
552

553 554
  gegl_sampler_get (coef_sampler,
                    coords.x, coords.y, NULL, coef, GEGL_ABYSS_NONE);
555

556
  for (i = 0; i < n_cage_vertices; i++)
557
    {
558
      point = &g_array_index (config->cage_points, GimpCagePoint, i);
559

560 561
      result.x += coef[i] * point->dest_point.x;
      result.y += coef[i] * point->dest_point.y;
562

563 564 565
      result.x += coef[i + n_cage_vertices] * point->edge_scaling_factor * point->edge_normal.x;
      result.y += coef[i + n_cage_vertices] * point->edge_scaling_factor * point->edge_normal.y;
    }
566

567 568 569 570
  return result;
}

GeglRectangle
571 572
gimp_operation_cage_transform_get_cached_region (GeglOperation       *operation,
                                                 const GeglRectangle *roi)
573
{
574 575
  GeglRectangle result = *gegl_operation_source_get_bounding_box (operation,
                                                                  "input");
576

577 578 579 580
  return result;
}

GeglRectangle
581 582 583
gimp_operation_cage_transform_get_required_for_output (GeglOperation       *operation,
                                                       const gchar         *input_pad,
                                                       const GeglRectangle *roi)
584
{
585 586
  GeglRectangle result = *gegl_operation_source_get_bounding_box (operation,
                                                                  "input");
587

588 589 590 591
  return result;
}

GeglRectangle
592
gimp_operation_cage_transform_get_bounding_box (GeglOperation *operation)
593
{
594 595
  GeglRectangle result = *gegl_operation_source_get_bounding_box (operation,
                                                                  "input");
596

597 598
  return result;
}