gimpperspectiveclone.c 19.5 KB
Newer Older
1
/* GIMP - The GNU Image Manipulation Program
2 3
 * Copyright (C) 1995 Spencer Kimball and Peter Mattis
 *
4
 * This program is free software: you can redistribute it and/or modify
5
 * it under the terms of the GNU General Public License as published by
6
 * the Free Software Foundation; either version 3 of the License, or
7 8 9 10 11 12 13 14
 * (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
15
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
16 17 18 19 20 21 22
 */

#include "config.h"

#include <stdlib.h>
#include <string.h>

23
#include <gdk-pixbuf/gdk-pixbuf.h>
24
#include <gegl.h>
25 26

#include "libgimpbase/gimpbase.h"
27
#include "libgimpmath/gimpmath.h"
28 29 30

#include "paint-types.h"

31
#include "gegl/gimp-gegl-nodes.h"
32
#include "gegl/gimp-gegl-utils.h"
33 34

#include "core/gimp.h"
35
#include "core/gimp-utils.h"
36
#include "core/gimpdrawable.h"
37
#include "core/gimperror.h"
38
#include "core/gimpimage.h"
39
#include "core/gimppattern.h"
40
#include "core/gimppickable.h"
Jehan's avatar
Jehan committed
41
#include "core/gimpsymmetry.h"
42 43 44 45 46 47 48

#include "gimpperspectiveclone.h"
#include "gimpperspectivecloneoptions.h"

#include "gimp-intl.h"


49 50 51
static void         gimp_perspective_clone_paint      (GimpPaintCore     *paint_core,
                                                       GimpDrawable      *drawable,
                                                       GimpPaintOptions  *paint_options,
Jehan's avatar
Jehan committed
52
                                                       GimpSymmetry      *sym,
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71
                                                       GimpPaintState     paint_state,
                                                       guint32            time);

static gboolean     gimp_perspective_clone_use_source (GimpSourceCore    *source_core,
                                                       GimpSourceOptions *options);
static GeglBuffer * gimp_perspective_clone_get_source (GimpSourceCore    *source_core,
                                                       GimpDrawable      *drawable,
                                                       GimpPaintOptions  *paint_options,
                                                       GimpPickable      *src_pickable,
                                                       gint               src_offset_x,
                                                       gint               src_offset_y,
                                                       GeglBuffer        *paint_buffer,
                                                       gint               paint_buffer_x,
                                                       gint               paint_buffer_y,
                                                       gint              *paint_area_offset_x,
                                                       gint              *paint_area_offset_y,
                                                       gint              *paint_area_width,
                                                       gint              *paint_area_height,
                                                       GeglRectangle     *src_rect);
72 73 74

static void         gimp_perspective_clone_get_matrix (GimpPerspectiveClone *clone,
                                                       GimpMatrix3          *matrix);
75

76 77

G_DEFINE_TYPE (GimpPerspectiveClone, gimp_perspective_clone,
78 79 80
               GIMP_TYPE_CLONE)

#define parent_class gimp_perspective_clone_parent_class
81 82 83 84 85 86 87 88 89 90


void
gimp_perspective_clone_register (Gimp                      *gimp,
                                 GimpPaintRegisterCallback  callback)
{
  (* callback) (gimp,
                GIMP_TYPE_PERSPECTIVE_CLONE,
                GIMP_TYPE_PERSPECTIVE_CLONE_OPTIONS,
                "gimp-perspective-clone",
91
                _("Perspective Clone"),
92 93 94 95 96 97
                "gimp-tool-perspective-clone");
}

static void
gimp_perspective_clone_class_init (GimpPerspectiveCloneClass *klass)
{
98 99 100 101 102
  GimpPaintCoreClass  *paint_core_class  = GIMP_PAINT_CORE_CLASS (klass);
  GimpSourceCoreClass *source_core_class = GIMP_SOURCE_CORE_CLASS (klass);

  paint_core_class->paint       = gimp_perspective_clone_paint;

103
  source_core_class->use_source = gimp_perspective_clone_use_source;
104
  source_core_class->get_source = gimp_perspective_clone_get_source;
105 106 107 108 109
}

static void
gimp_perspective_clone_init (GimpPerspectiveClone *clone)
{
110 111
  clone->src_x_fv  = 0.0;    /* source coords in front_view perspective */
  clone->src_y_fv  = 0.0;
112

113 114
  clone->dest_x_fv = 0.0;    /* destination coords in front_view perspective */
  clone->dest_y_fv = 0.0;
115 116 117 118 119 120 121 122 123

  gimp_matrix3_identity (&clone->transform);
  gimp_matrix3_identity (&clone->transform_inv);
}

static void
gimp_perspective_clone_paint (GimpPaintCore    *paint_core,
                              GimpDrawable     *drawable,
                              GimpPaintOptions *paint_options,
Jehan's avatar
Jehan committed
124
                              GimpSymmetry     *sym,
125 126 127
                              GimpPaintState    paint_state,
                              guint32           time)
{
128 129 130 131 132
  GimpSourceCore       *source_core   = GIMP_SOURCE_CORE (paint_core);
  GimpPerspectiveClone *clone         = GIMP_PERSPECTIVE_CLONE (paint_core);
  GimpContext          *context       = GIMP_CONTEXT (paint_options);
  GimpCloneOptions     *clone_options = GIMP_CLONE_OPTIONS (paint_options);
  GimpSourceOptions    *options       = GIMP_SOURCE_OPTIONS (paint_options);
Jehan's avatar
Jehan committed
133 134 135 136
  const GimpCoords     *coords;

  /* The source is based on the original stroke */
  coords = gimp_symmetry_get_origin (sym);
137 138 139 140

  switch (paint_state)
    {
    case GIMP_PAINT_STATE_INIT:
141
      if (source_core->set_source)
142
        {
143
          g_object_set (source_core, "src-drawable", drawable, NULL);
144

145 146
          source_core->src_x = floor (coords->x);
          source_core->src_y = floor (coords->y);
147 148 149

          /* get source coordinates in front view perspective */
          gimp_matrix3_transform_point (&clone->transform_inv,
150 151 152 153
                                        source_core->src_x,
                                        source_core->src_y,
                                        &clone->src_x_fv,
                                        &clone->src_y_fv);
154

155
          source_core->first_stroke = TRUE;
156
        }
157
      else
158
        {
159 160 161
          GeglBuffer *orig_buffer = NULL;
          GeglNode   *tile        = NULL;
          GeglNode   *src_node;
162

163 164 165 166
          if (options->align_mode == GIMP_SOURCE_ALIGN_NO)
            {
              source_core->orig_src_x = source_core->src_x;
              source_core->orig_src_y = source_core->src_y;
167

168 169 170 171 172 173 174 175 176
              source_core->first_stroke = TRUE;
            }

          clone->node = gegl_node_new ();

          g_object_set (clone->node,
                        "dont-cache", TRUE,
                        NULL);

177 178
          switch (clone_options->clone_type)
            {
179
            case GIMP_CLONE_IMAGE:
180 181 182 183 184 185 186 187 188 189 190 191 192 193 194
              {
                GimpPickable *src_pickable;
                GimpImage    *src_image;
                GimpImage    *dest_image;

                /*  If the source image is different from the
                 *  destination, then we should copy straight from the
                 *  source image to the canvas.
                 *  Otherwise, we need a call to get_orig_image to make sure
                 *  we get a copy of the unblemished (offset) image
                 */
                src_pickable = GIMP_PICKABLE (source_core->src_drawable);
                src_image    = gimp_pickable_get_image (src_pickable);

                if (options->sample_merged)
195
                  src_pickable = GIMP_PICKABLE (src_image);
196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215

                dest_image = gimp_item_get_image (GIMP_ITEM (drawable));

                if ((options->sample_merged &&
                     (src_image != dest_image)) ||
                    (! options->sample_merged &&
                     (source_core->src_drawable != drawable)))
                  {
                    orig_buffer = gimp_pickable_get_buffer (src_pickable);
                  }
                else
                  {
                    if (options->sample_merged)
                      orig_buffer = gimp_paint_core_get_orig_proj (paint_core);
                    else
                      orig_buffer = gimp_paint_core_get_orig_image (paint_core);
                  }
              }
              break;

216
            case GIMP_CLONE_PATTERN:
217 218 219 220 221 222 223 224
              {
                GimpPattern *pattern = gimp_context_get_pattern (context);

                orig_buffer = gimp_pattern_create_buffer (pattern);

                tile = gegl_node_new_child (clone->node,
                                            "operation", "gegl:tile",
                                            NULL);
225 226 227
                clone->crop = gegl_node_new_child (clone->node,
                                                   "operation", "gegl:crop",
                                                   NULL);
228 229 230 231 232 233 234 235
              }
              break;
            }

          src_node = gegl_node_new_child (clone->node,
                                          "operation", "gegl:buffer-source",
                                          "buffer",    orig_buffer,
                                          NULL);
236 237 238

          clone->transform_node =
            gegl_node_new_child (clone->node,
239
                                 "operation", "gegl:transform",
240
                                 "sampler",    GIMP_INTERPOLATION_LINEAR,
241 242 243 244 245 246 247
                                 NULL);

          clone->dest_node =
            gegl_node_new_child (clone->node,
                                 "operation", "gegl:write-buffer",
                                 NULL);

248 249 250
          if (tile)
            {
              gegl_node_link_many (src_node,
251 252
                                   tile,
                                   clone->crop,
253 254 255 256 257 258 259 260 261 262 263 264 265
                                   clone->transform_node,
                                   clone->dest_node,
                                   NULL);

              g_object_unref (orig_buffer);
            }
          else
            {
              gegl_node_link_many (src_node,
                                   clone->transform_node,
                                   clone->dest_node,
                                   NULL);
            }
266 267 268 269
        }
      break;

    case GIMP_PAINT_STATE_MOTION:
270
      if (source_core->set_source)
271 272 273
        {
          /*  If the control key is down, move the src target and return */

274 275
          source_core->src_x = floor (coords->x);
          source_core->src_y = floor (coords->y);
276 277 278

          /* get source coordinates in front view perspective */
          gimp_matrix3_transform_point (&clone->transform_inv,
279 280 281 282
                                        source_core->src_x,
                                        source_core->src_y,
                                        &clone->src_x_fv,
                                        &clone->src_y_fv);
283

284
          source_core->first_stroke = TRUE;
285 286 287 288 289 290 291
        }
      else
        {
          /*  otherwise, update the target  */

          gint dest_x;
          gint dest_y;
Jehan's avatar
Jehan committed
292 293
          gint n_strokes;
          gint i;
294

Jehan's avatar
Jehan committed
295 296
          n_strokes = gimp_symmetry_get_size (sym);
          for (i = 0; i < n_strokes; i++)
297
            {
Jehan's avatar
Jehan committed
298 299
              coords = gimp_symmetry_get_coords (sym, i);

300 301
              dest_x = floor (coords->x);
              dest_y = floor (coords->y);
Jehan's avatar
Jehan committed
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

              if (options->align_mode == GIMP_SOURCE_ALIGN_REGISTERED)
                {
                  source_core->offset_x = 0;
                  source_core->offset_y = 0;
                }
              else if (options->align_mode == GIMP_SOURCE_ALIGN_FIXED)
                {
                  source_core->offset_x = source_core->src_x - dest_x;
                  source_core->offset_y = source_core->src_y - dest_y;
                }
              else if (source_core->first_stroke)
                {
                  source_core->offset_x = source_core->src_x - dest_x;
                  source_core->offset_y = source_core->src_y - dest_y;

                  /* get destination coordinates in front view perspective */
                  gimp_matrix3_transform_point (&clone->transform_inv,
                                                dest_x,
                                                dest_y,
                                                &clone->dest_x_fv,
                                                &clone->dest_y_fv);

                  source_core->first_stroke = FALSE;
                }
327 328
            }

Jehan's avatar
Jehan committed
329
          gimp_source_core_motion (source_core, drawable, paint_options, sym);
330 331 332 333
        }
      break;

    case GIMP_PAINT_STATE_FINISH:
334 335 336 337
      g_clear_object (&clone->node);
      clone->crop           = NULL;
      clone->transform_node = NULL;
      clone->dest_node      = NULL;
338 339 340 341 342 343 344 345 346 347
      break;

    default:
      break;
    }

  g_object_notify (G_OBJECT (clone), "src-x");
  g_object_notify (G_OBJECT (clone), "src-y");
}

348 349 350 351 352 353 354
static gboolean
gimp_perspective_clone_use_source (GimpSourceCore    *source_core,
                                   GimpSourceOptions *options)
{
  return TRUE;
}

355
static GeglBuffer *
356 357 358 359 360 361
gimp_perspective_clone_get_source (GimpSourceCore   *source_core,
                                   GimpDrawable     *drawable,
                                   GimpPaintOptions *paint_options,
                                   GimpPickable     *src_pickable,
                                   gint              src_offset_x,
                                   gint              src_offset_y,
362 363 364
                                   GeglBuffer       *paint_buffer,
                                   gint              paint_buffer_x,
                                   gint              paint_buffer_y,
365 366 367
                                   gint             *paint_area_offset_x,
                                   gint             *paint_area_offset_y,
                                   gint             *paint_area_width,
368 369
                                   gint             *paint_area_height,
                                   GeglRectangle    *src_rect)
370
{
371 372
  GimpPerspectiveClone *clone         = GIMP_PERSPECTIVE_CLONE (source_core);
  GimpCloneOptions     *clone_options = GIMP_CLONE_OPTIONS (paint_options);
373 374
  GeglBuffer           *src_buffer;
  GeglBuffer           *dest_buffer;
375
  const Babl           *src_format_alpha;
376 377
  gint                  x1d, y1d, x2d, y2d;
  gdouble               x1s, y1s, x2s, y2s, x3s, y3s, x4s, y4s;
378
  gint                  xmin, ymin, xmax, ymax;
379
  GimpMatrix3           matrix;
380
  GimpMatrix3           gegl_matrix;
381

382
  src_buffer       = gimp_pickable_get_buffer (src_pickable);
383
  src_format_alpha = gimp_pickable_get_format_with_alpha (src_pickable);
384 385

  /* Destination coordinates that will be painted */
386 387 388 389
  x1d = paint_buffer_x;
  y1d = paint_buffer_y;
  x2d = paint_buffer_x + gegl_buffer_get_width  (paint_buffer);
  y2d = paint_buffer_y + gegl_buffer_get_height (paint_buffer);
390 391 392 393 394 395 396 397 398 399 400 401 402 403 404

  /* Boundary box for source pixels to copy: Convert all the vertex of
   * the box to paint in destination area to its correspondent in
   * source area bearing in mind perspective
   */
  gimp_perspective_clone_get_source_point (clone, x1d, y1d, &x1s, &y1s);
  gimp_perspective_clone_get_source_point (clone, x1d, y2d, &x2s, &y2s);
  gimp_perspective_clone_get_source_point (clone, x2d, y1d, &x3s, &y3s);
  gimp_perspective_clone_get_source_point (clone, x2d, y2d, &x4s, &y4s);

  xmin = floor (MIN4 (x1s, x2s, x3s, x4s));
  ymin = floor (MIN4 (y1s, y2s, y3s, y4s));
  xmax = ceil  (MAX4 (x1s, x2s, x3s, x4s));
  ymax = ceil  (MAX4 (y1s, y2s, y3s, y4s));

405
  switch (clone_options->clone_type)
406
    {
407
    case GIMP_CLONE_IMAGE:
408 409 410 411 412 413 414 415 416 417 418 419
      if (! gimp_rectangle_intersect (xmin, ymin,
                                      xmax - xmin, ymax - ymin,
                                      0, 0,
                                      gegl_buffer_get_width  (src_buffer),
                                      gegl_buffer_get_height (src_buffer),
                                      NULL, NULL, NULL, NULL))
        {
          /* if the source area is completely out of the image */
          return NULL;
        }
      break;

420
    case GIMP_CLONE_PATTERN:
421 422 423 424 425 426 427
      gegl_node_set (clone->crop,
                     "x",      (gdouble) xmin,
                     "y",      (gdouble) ymin,
                     "width",  (gdouble) xmax - xmin,
                     "height", (gdouble) ymax - ymin,
                     NULL);
      break;
428 429
    }

430
  dest_buffer = gegl_buffer_new (GEGL_RECTANGLE (0, 0, x2d - x1d, y2d - y1d),
431
                                 src_format_alpha);
432

433 434
  gimp_perspective_clone_get_matrix (clone, &matrix);

435 436 437 438
  gimp_matrix3_identity (&gegl_matrix);
  gimp_matrix3_mult (&matrix, &gegl_matrix);
  gimp_matrix3_translate (&gegl_matrix, -x1d, -y1d);

439
  gimp_gegl_node_set_matrix (clone->transform_node, &gegl_matrix);
440

441 442 443
  gegl_node_set (clone->dest_node,
                 "buffer", dest_buffer,
                 NULL);
444

445 446 447
  gegl_node_blit (clone->dest_node, 1.0,
                  GEGL_RECTANGLE (0, 0, x2d - x1d, y2d - y1d),
                  NULL, NULL, 0, GEGL_BLIT_DEFAULT);
448

449
  *src_rect = *GEGL_RECTANGLE (0, 0, x2d - x1d, y2d - y1d);
450

451
  return dest_buffer;
452 453
}

454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482

/*  public functions  */

void
gimp_perspective_clone_set_transform (GimpPerspectiveClone *clone,
                                      GimpMatrix3          *transform)
{
  g_return_if_fail (GIMP_IS_PERSPECTIVE_CLONE (clone));
  g_return_if_fail (transform != NULL);

  clone->transform = *transform;

  clone->transform_inv = clone->transform;
  gimp_matrix3_invert (&clone->transform_inv);

#if 0
  g_printerr ("%f\t%f\t%f\n%f\t%f\t%f\n%f\t%f\t%f\n\n",
              clone->transform.coeff[0][0],
              clone->transform.coeff[0][1],
              clone->transform.coeff[0][2],
              clone->transform.coeff[1][0],
              clone->transform.coeff[1][1],
              clone->transform.coeff[1][2],
              clone->transform.coeff[2][0],
              clone->transform.coeff[2][1],
              clone->transform.coeff[2][2]);
#endif
}

483
void
484
gimp_perspective_clone_get_source_point (GimpPerspectiveClone *clone,
485 486 487 488 489 490 491
                                         gdouble               x,
                                         gdouble               y,
                                         gdouble              *newx,
                                         gdouble              *newy)
{
  gdouble temp_x, temp_y;

492 493 494 495
  g_return_if_fail (GIMP_IS_PERSPECTIVE_CLONE (clone));
  g_return_if_fail (newx != NULL);
  g_return_if_fail (newy != NULL);

496
  gimp_matrix3_transform_point (&clone->transform_inv,
497 498
                                x, y, &temp_x, &temp_y);

499
#if 0
500 501 502
  /* Get the offset of each pixel in destination area from the
   * destination pixel in front view perspective
   */
503 504
  offset_x_fv = temp_x - clone->dest_x_fv;
  offset_y_fv = temp_y - clone->dest_y_fv;
505 506

  /* Get the source pixel in front view perspective */
507 508 509 510 511 512
  temp_x = offset_x_fv + clone->src_x_fv;
  temp_y = offset_y_fv + clone->src_y_fv;
#endif

  temp_x += clone->src_x_fv - clone->dest_x_fv;
  temp_y += clone->src_y_fv - clone->dest_y_fv;
513 514

  /* Convert the source pixel to perspective view */
515
  gimp_matrix3_transform_point (&clone->transform,
516 517
                                temp_x, temp_y, newx, newy);
}
518

519 520 521 522

/*  private functions  */

static void
523 524 525 526 527 528 529 530 531 532 533 534 535 536
gimp_perspective_clone_get_matrix (GimpPerspectiveClone *clone,
                                   GimpMatrix3          *matrix)
{
  GimpMatrix3 temp;

  gimp_matrix3_identity (&temp);
  gimp_matrix3_translate (&temp,
                          clone->dest_x_fv - clone->src_x_fv,
                          clone->dest_y_fv - clone->src_y_fv);

  *matrix = clone->transform_inv;
  gimp_matrix3_mult (&temp, matrix);
  gimp_matrix3_mult (&clone->transform, matrix);
}