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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
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
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
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
483
484
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
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
|
/* GIMP - The GNU Image Manipulation Program
*
* gimpseamlessclonetool.c
* Copyright (C) 2011 Barak Itkin <lightningismyname@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
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#include "config.h"
#include <gegl.h>
#include <gegl-plugin.h> /* gegl_operation_invalidate() */
#include <gtk/gtk.h>
#include <gdk/gdkkeysyms.h>
#include "libgimpbase/gimpbase.h"
#include "libgimpwidgets/gimpwidgets.h"
#include "tools-types.h"
#include "config/gimpguiconfig.h" /* playground */
#include "gegl/gimp-gegl-utils.h"
#include "core/gimp.h"
#include "core/gimpbuffer.h"
#include "core/gimpdrawablefilter.h"
#include "core/gimpimage.h"
#include "core/gimpitem.h"
#include "core/gimpprogress.h"
#include "core/gimpprojection.h"
#include "core/gimptoolinfo.h"
#include "widgets/gimphelp-ids.h"
#include "widgets/gimpclipboard.h"
#include "display/gimpdisplay.h"
#include "display/gimpdisplayshell.h"
#include "display/gimpdisplayshell-transform.h"
#include "gimpseamlessclonetool.h"
#include "gimpseamlesscloneoptions.h"
#include "gimptoolcontrol.h"
#include "gimp-intl.h"
#define SC_DEBUG TRUE
#ifdef SC_DEBUG
#define sc_debug_fstart() g_debug ("%s::start", __FUNCTION__);
#define sc_debug_fend() g_debug ("%s::end", __FUNCTION__);
#else
#define sc_debug_fstart()
#define sc_debug_fend()
#endif
#define gimp_seamless_clone_tool_is_in_paste(sc,x0,y0) \
( ((sc)->xoff <= (x0) && (x0) < (sc)->xoff + (sc)->width) \
&& ((sc)->yoff <= (y0) && (y0) < (sc)->yoff + (sc)->height)) \
#define gimp_seamless_clone_tool_is_in_paste_c(sc,coords) \
gimp_seamless_clone_tool_is_in_paste((sc),(coords)->x,(coords)->y)
/* init ----------> preprocess
* | |
* | |
* | |
* | v
* | render(wait, motion)
* | / |
* | _____/ |
* | _____/ |
* v v v
* quit <---------- commit
*
* Begin at INIT state
*
* INIT: Wait for click on canvas
* have a paste ? -> PREPROCESS : -> QUIT
*
* PREPROCESS: Do the preprocessing
* -> RENDER
*
* RENDER: Interact and wait for quit signal
* commit quit ? -> COMMIT : -> QUIT
*
* COMMIT: Commit the changes
* -> QUIT
*
* QUIT: Invoked by sending a ACTION_HALT to the tool_control
* Free resources
*/
enum
{
SC_STATE_INIT,
SC_STATE_PREPROCESS,
SC_STATE_RENDER_WAIT,
SC_STATE_RENDER_MOTION,
SC_STATE_COMMIT,
SC_STATE_QUIT
};
static void gimp_seamless_clone_tool_control (GimpTool *tool,
GimpToolAction action,
GimpDisplay *display);
static void gimp_seamless_clone_tool_button_press (GimpTool *tool,
const GimpCoords *coords,
guint32 time,
GdkModifierType state,
GimpButtonPressType press_type,
GimpDisplay *display);
static void gimp_seamless_clone_tool_button_release (GimpTool *tool,
const GimpCoords *coords,
guint32 time,
GdkModifierType state,
GimpButtonReleaseType release_type,
GimpDisplay *display);
static void gimp_seamless_clone_tool_motion (GimpTool *tool,
const GimpCoords *coords,
guint32 time,
GdkModifierType state,
GimpDisplay *display);
static gboolean gimp_seamless_clone_tool_key_press (GimpTool *tool,
GdkEventKey *kevent,
GimpDisplay *display);
static void gimp_seamless_clone_tool_oper_update (GimpTool *tool,
const GimpCoords *coords,
GdkModifierType state,
gboolean proximity,
GimpDisplay *display);
static void gimp_seamless_clone_tool_cursor_update (GimpTool *tool,
const GimpCoords *coords,
GdkModifierType state,
GimpDisplay *display);
static void gimp_seamless_clone_tool_options_notify (GimpTool *tool,
GimpToolOptions *options,
const GParamSpec *pspec);
static void gimp_seamless_clone_tool_draw (GimpDrawTool *draw_tool);
static void gimp_seamless_clone_tool_start (GimpSeamlessCloneTool *sc,
GimpDisplay *display);
static void gimp_seamless_clone_tool_stop (GimpSeamlessCloneTool *sc,
gboolean display_change_only);
static void gimp_seamless_clone_tool_commit (GimpSeamlessCloneTool *sc);
static void gimp_seamless_clone_tool_create_render_node (GimpSeamlessCloneTool *sc);
static gboolean gimp_seamless_clone_tool_render_node_update (GimpSeamlessCloneTool *sc);
static void gimp_seamless_clone_tool_create_filter (GimpSeamlessCloneTool *sc,
GimpDrawable *drawable);
static void gimp_seamless_clone_tool_filter_flush (GimpDrawableFilter *filter,
GimpTool *tool);
static void gimp_seamless_clone_tool_filter_update (GimpSeamlessCloneTool *sc);
G_DEFINE_TYPE (GimpSeamlessCloneTool, gimp_seamless_clone_tool,
GIMP_TYPE_DRAW_TOOL)
#define parent_class gimp_seamless_clone_tool_parent_class
void
gimp_seamless_clone_tool_register (GimpToolRegisterCallback callback,
gpointer data)
{
/* we should not know that "data" is a Gimp*, but what the heck this
* is experimental playground stuff
*/
if (GIMP_GUI_CONFIG (GIMP (data)->config)->playground_seamless_clone_tool)
(* callback) (GIMP_TYPE_SEAMLESS_CLONE_TOOL,
GIMP_TYPE_SEAMLESS_CLONE_OPTIONS,
gimp_seamless_clone_options_gui,
0,
"gimp-seamless-clone-tool",
_("Seamless Clone"),
_("Seamless Clone: Seamlessly paste one image into another"),
N_("_Seamless Clone"), NULL,
NULL, GIMP_HELP_TOOL_SEAMLESS_CLONE,
GIMP_ICON_TOOL_SEAMLESS_CLONE,
data);
}
static void
gimp_seamless_clone_tool_class_init (GimpSeamlessCloneToolClass *klass)
{
GimpToolClass *tool_class = GIMP_TOOL_CLASS (klass);
GimpDrawToolClass *draw_tool_class = GIMP_DRAW_TOOL_CLASS (klass);
tool_class->control = gimp_seamless_clone_tool_control;
tool_class->button_press = gimp_seamless_clone_tool_button_press;
tool_class->button_release = gimp_seamless_clone_tool_button_release;
tool_class->motion = gimp_seamless_clone_tool_motion;
tool_class->key_press = gimp_seamless_clone_tool_key_press;
tool_class->oper_update = gimp_seamless_clone_tool_oper_update;
tool_class->cursor_update = gimp_seamless_clone_tool_cursor_update;
tool_class->options_notify = gimp_seamless_clone_tool_options_notify;
draw_tool_class->draw = gimp_seamless_clone_tool_draw;
}
static void
gimp_seamless_clone_tool_init (GimpSeamlessCloneTool *self)
{
GimpTool *tool = GIMP_TOOL (self);
gimp_tool_control_set_dirty_mask (tool->control,
GIMP_DIRTY_IMAGE |
GIMP_DIRTY_IMAGE_STRUCTURE |
GIMP_DIRTY_DRAWABLE |
GIMP_DIRTY_SELECTION);
gimp_tool_control_set_tool_cursor (tool->control,
GIMP_TOOL_CURSOR_MOVE);
self->tool_state = SC_STATE_INIT;
}
static void
gimp_seamless_clone_tool_control (GimpTool *tool,
GimpToolAction action,
GimpDisplay *display)
{
GimpSeamlessCloneTool *sc = GIMP_SEAMLESS_CLONE_TOOL (tool);
switch (action)
{
case GIMP_TOOL_ACTION_PAUSE:
case GIMP_TOOL_ACTION_RESUME:
break;
case GIMP_TOOL_ACTION_HALT:
if (tool->display)
gimp_seamless_clone_tool_stop (sc, FALSE);
/* TODO: If we have any tool options that should be reset, here is
* a good place to do so.
*/
break;
case GIMP_TOOL_ACTION_COMMIT:
gimp_seamless_clone_tool_commit (sc);
break;
}
GIMP_TOOL_CLASS (parent_class)->control (tool, action, display);
}
/**
* gimp_seamless_clone_tool_start:
* @sc: The GimpSeamlessCloneTool to initialize for usage on the given
* display
* @display: The display to initialize the tool for
*
* A utility function to initialize a tool for working on a given
* display. At the beginning of each function, we can check if the event's
* display is the same as the tool's one, and if not call this. This is
* not required by the gimptool interface or anything like that, but
* this is a convenient way to do all the initialization work in one
* place, and this is how the base class (GimpDrawTool) does that
*/
static void
gimp_seamless_clone_tool_start (GimpSeamlessCloneTool *sc,
GimpDisplay *display)
{
GimpTool *tool = GIMP_TOOL (sc);
GimpImage *image = gimp_display_get_image (display);
GimpDrawable *drawable = gimp_image_get_active_drawable (image);
/* First handle the paste - we need to make sure we have one in order
* to do anything else.
*/
if (sc->paste == NULL)
{
GimpBuffer *buffer = gimp_clipboard_get_buffer (tool->tool_info->gimp);
if (! buffer)
{
gimp_tool_push_status (tool, display,
"%s",
_("There is no image data in the clipboard to paste."));
return;
}
sc->paste = gimp_gegl_buffer_dup (gimp_buffer_get_buffer (buffer));
g_object_unref (buffer);
sc->width = gegl_buffer_get_width (sc->paste);
sc->height = gegl_buffer_get_height (sc->paste);
}
/* Free resources which are relevant only for the previous display */
gimp_seamless_clone_tool_stop (sc, TRUE);
tool->display = display;
gimp_seamless_clone_tool_create_filter (sc, drawable);
gimp_draw_tool_start (GIMP_DRAW_TOOL (sc), display);
sc->tool_state = SC_STATE_RENDER_WAIT;
}
/**
* gimp_seamless_clone_tool_stop:
* @sc: The seamless clone tool whose resources should be freed
* @display_change_only: Mark that the only reason for this call was a
* switch of the working display.
*
* This function frees any resources associated with the seamless clone
* tool, including caches, gegl graphs, and anything the tool created.
* Afterwards, it initializes all the relevant pointers to some initial
* value (usually NULL) like the init function does.
*
* Note that for seamless cloning, no change needs to be done when
* switching to a different display, except for clearing the image map.
* So for that, we provide a boolean parameter to specify that the only
* change was one of the display
*/
static void
gimp_seamless_clone_tool_stop (GimpSeamlessCloneTool *sc,
gboolean display_change_only)
{
/* See if we actually have any reason to stop */
if (sc->tool_state == SC_STATE_INIT)
return;
if (! display_change_only)
{
sc->tool_state = SC_STATE_INIT;
g_clear_object (&sc->paste);
g_clear_object (&sc->render_node);
sc->sc_node = NULL;
}
/* This should always happen, even when we just switch a display */
if (sc->filter)
{
gimp_drawable_filter_abort (sc->filter);
g_clear_object (&sc->filter);
if (GIMP_TOOL (sc)->display)
gimp_image_flush (gimp_display_get_image (GIMP_TOOL (sc)->display));
}
gimp_draw_tool_stop (GIMP_DRAW_TOOL (sc));
}
static void
gimp_seamless_clone_tool_commit (GimpSeamlessCloneTool *sc)
{
GimpTool *tool = GIMP_TOOL (sc);
if (sc->filter)
{
gimp_tool_control_push_preserve (tool->control, TRUE);
gimp_drawable_filter_commit (sc->filter, GIMP_PROGRESS (tool), FALSE);
g_clear_object (&sc->filter);
gimp_tool_control_pop_preserve (tool->control);
gimp_image_flush (gimp_display_get_image (tool->display));
}
}
static void
gimp_seamless_clone_tool_button_press (GimpTool *tool,
const GimpCoords *coords,
guint32 time,
GdkModifierType state,
GimpButtonPressType press_type,
GimpDisplay *display)
{
GimpSeamlessCloneTool *sc = GIMP_SEAMLESS_CLONE_TOOL (tool);
if (display != tool->display)
{
gimp_seamless_clone_tool_start (sc, display);
/* Center the paste on the mouse */
sc->xoff = (gint) coords->x - sc->width / 2;
sc->yoff = (gint) coords->y - sc->height / 2;
}
if (sc->tool_state == SC_STATE_RENDER_WAIT &&
gimp_seamless_clone_tool_is_in_paste_c (sc, coords))
{
gimp_draw_tool_pause (GIMP_DRAW_TOOL (sc));
/* Record previous location, in case the user cancels the
* movement
*/
sc->xoff_p = sc->xoff;
sc->yoff_p = sc->yoff;
/* Record the mouse location, so that the dragging offset can be
* calculated
*/
sc->xclick = coords->x;
sc->yclick = coords->y;
gimp_draw_tool_resume (GIMP_DRAW_TOOL (tool));
if (gimp_seamless_clone_tool_render_node_update (sc))
{
gimp_seamless_clone_tool_filter_update (sc);
}
sc->tool_state = SC_STATE_RENDER_MOTION;
/* In order to receive motion events from the current click, we must
* activate the tool control
*/
gimp_tool_control_activate (tool->control);
}
}
void
gimp_seamless_clone_tool_button_release (GimpTool *tool,
const GimpCoords *coords,
guint32 time,
GdkModifierType state,
GimpButtonReleaseType release_type,
GimpDisplay *display)
{
GimpSeamlessCloneTool *sc = GIMP_SEAMLESS_CLONE_TOOL (tool);
gimp_tool_control_halt (tool->control);
/* There is nothing to do, unless we were actually moving a paste */
if (sc->tool_state == SC_STATE_RENDER_MOTION)
{
gimp_draw_tool_pause (GIMP_DRAW_TOOL (sc));
if (release_type == GIMP_BUTTON_RELEASE_CANCEL)
{
sc->xoff = sc->xoff_p;
sc->yoff = sc->yoff_p;
}
else
{
sc->xoff = sc->xoff_p + (gint) (coords->x - sc->xclick);
sc->yoff = sc->yoff_p + (gint) (coords->y - sc->yclick);
}
gimp_draw_tool_resume (GIMP_DRAW_TOOL (tool));
if (gimp_seamless_clone_tool_render_node_update (sc))
{
gimp_seamless_clone_tool_filter_update (sc);
}
sc->tool_state = SC_STATE_RENDER_WAIT;
}
}
static void
gimp_seamless_clone_tool_motion (GimpTool *tool,
const GimpCoords *coords,
guint32 time,
GdkModifierType state,
GimpDisplay *display)
{
GimpSeamlessCloneTool *sc = GIMP_SEAMLESS_CLONE_TOOL (tool);
if (sc->tool_state == SC_STATE_RENDER_MOTION)
{
gimp_draw_tool_pause (GIMP_DRAW_TOOL (sc));
sc->xoff = sc->xoff_p + (gint) (coords->x - sc->xclick);
sc->yoff = sc->yoff_p + (gint) (coords->y - sc->yclick);
gimp_draw_tool_resume (GIMP_DRAW_TOOL (tool));
if (gimp_seamless_clone_tool_render_node_update (sc))
{
gimp_seamless_clone_tool_filter_update (sc);
}
}
}
static gboolean
gimp_seamless_clone_tool_key_press (GimpTool *tool,
GdkEventKey *kevent,
GimpDisplay *display)
{
GimpSeamlessCloneTool *sct = GIMP_SEAMLESS_CLONE_TOOL (tool);
if (sct->tool_state == SC_STATE_RENDER_MOTION ||
sct->tool_state == SC_STATE_RENDER_WAIT)
{
switch (kevent->keyval)
{
case GDK_KEY_Return:
case GDK_KEY_KP_Enter:
case GDK_KEY_ISO_Enter:
gimp_tool_control_set_preserve (tool->control, TRUE);
/* TODO: there may be issues with committing the image map
* result after some changes were made and the preview
* was scrolled. We can fix these by either invalidating
* the area which is a union of the previous paste
* rectangle each time (in the update function) or by
* invalidating and re-rendering all now (expensive and
* perhaps useless */
gimp_drawable_filter_commit (sct->filter, GIMP_PROGRESS (tool), FALSE);
g_clear_object (&sct->filter);
gimp_tool_control_set_preserve (tool->control, FALSE);
gimp_image_flush (gimp_display_get_image (display));
gimp_seamless_clone_tool_control (tool, GIMP_TOOL_ACTION_HALT,
display);
return TRUE;
case GDK_KEY_Escape:
gimp_seamless_clone_tool_control (tool, GIMP_TOOL_ACTION_HALT,
display);
return TRUE;
default:
break;
}
}
return FALSE;
}
static void
gimp_seamless_clone_tool_oper_update (GimpTool *tool,
const GimpCoords *coords,
GdkModifierType state,
gboolean proximity,
GimpDisplay *display)
{
gimp_draw_tool_pause (GIMP_DRAW_TOOL (tool));
/* TODO: Modify data here */
gimp_draw_tool_resume (GIMP_DRAW_TOOL (tool));
}
/* Mouse cursor policy:
* - Always use the move cursor
* - While dragging the paste, use a move modified
* - Else, While hovering above it, display no modifier
* - Else, display a "bad" modifier
*/
static void
gimp_seamless_clone_tool_cursor_update (GimpTool *tool,
const GimpCoords *coords,
GdkModifierType state,
GimpDisplay *display)
{
GimpSeamlessCloneTool *sc = GIMP_SEAMLESS_CLONE_TOOL (tool);
GimpCursorModifier modifier = GIMP_CURSOR_MODIFIER_BAD;
/* Only update if the tool is actually active on some display */
if (tool->display)
{
if (sc->tool_state == SC_STATE_RENDER_MOTION)
{
modifier = GIMP_CURSOR_MODIFIER_MOVE;
}
else if (sc->tool_state == SC_STATE_RENDER_WAIT &&
gimp_seamless_clone_tool_is_in_paste_c (sc, coords))
{
modifier = GIMP_CURSOR_MODIFIER_NONE;
}
gimp_tool_control_set_cursor_modifier (tool->control, modifier);
}
GIMP_TOOL_CLASS (parent_class)->cursor_update (tool, coords, state, display);
}
static void
gimp_seamless_clone_tool_options_notify (GimpTool *tool,
GimpToolOptions *options,
const GParamSpec *pspec)
{
GimpSeamlessCloneTool *sc = GIMP_SEAMLESS_CLONE_TOOL (tool);
GIMP_TOOL_CLASS (parent_class)->options_notify (tool, options, pspec);
if (! tool->display)
return;
gimp_draw_tool_pause (GIMP_DRAW_TOOL (tool));
if (! strcmp (pspec->name, "max-refine-scale"))
{
if (gimp_seamless_clone_tool_render_node_update (sc))
{
gimp_seamless_clone_tool_filter_update (sc);
}
}
gimp_draw_tool_resume (GIMP_DRAW_TOOL (tool));
}
static void
gimp_seamless_clone_tool_draw (GimpDrawTool *draw_tool)
{
GimpSeamlessCloneTool *sc = GIMP_SEAMLESS_CLONE_TOOL (draw_tool);
if (sc->tool_state == SC_STATE_RENDER_WAIT ||
sc->tool_state == SC_STATE_RENDER_MOTION)
{
gimp_draw_tool_add_rectangle (draw_tool, FALSE,
sc->xoff, sc->yoff, sc->width, sc->height);
}
}
/**
* gimp_seamless_clone_tool_create_render_node:
* @sc: The GimpSeamlessCloneTool to initialize
*
* This function creates a Gegl node graph of the composition which is
* needed to render the drawable. The graph should have an "input" pad
* which will receive the drawable on which the preview is applied, and
* it should also have an "output" pad to which the final result will be
* rendered
*/
static void
gimp_seamless_clone_tool_create_render_node (GimpSeamlessCloneTool *sc)
{
/* Here is a textual description of the graph we are going to create:
*
* <input> <- drawable
* +--+--------------------------+
* | |output |
* | | |
* | | <buffer-source> <- paste |
* | | |output |
* | | | |
* | |input |aux |
* |<seamless-paste-render> |
* | |output |
* | | |
* | |input |
* +----+------------------------+
* <output>
*/
GimpSeamlessCloneOptions *options = GIMP_SEAMLESS_CLONE_TOOL_GET_OPTIONS (sc);
GeglNode *node;
GeglNode *op, *paste, *overlay;
GeglNode *input, *output;
node = gegl_node_new ();
input = gegl_node_get_input_proxy (node, "input");
output = gegl_node_get_output_proxy (node, "output");
paste = gegl_node_new_child (node,
"operation", "gegl:buffer-source",
"buffer", sc->paste,
NULL);
op = gegl_node_new_child (node,
"operation", "gegl:seamless-clone",
"max-refine-scale", options->max_refine_scale,
NULL);
overlay = gegl_node_new_child (node,
"operation", "svg:dst-over",
NULL);
gegl_node_connect_to (input, "output",
op, "input");
gegl_node_connect_to (paste, "output",
op, "aux");
gegl_node_connect_to (op, "output",
overlay, "input");
gegl_node_connect_to (input, "output",
overlay, "aux");
gegl_node_connect_to (overlay, "output",
output, "input");
sc->render_node = node;
sc->sc_node = op;
}
/* gimp_seamless_clone_tool_render_node_update:
* sc: the Seamless Clone tool whose render has to be updated.
*
* Returns: TRUE if any property changed.
*/
static gboolean
gimp_seamless_clone_tool_render_node_update (GimpSeamlessCloneTool *sc)
{
static gint rendered__max_refine_scale = -1;
static gint rendered_xoff = G_MAXINT;
static gint rendered_yoff = G_MAXINT;
GimpSeamlessCloneOptions *options = GIMP_SEAMLESS_CLONE_TOOL_GET_OPTIONS (sc);
GimpDrawable *bg = GIMP_TOOL (sc)->drawable;
gint off_x, off_y;
/* All properties stay the same. No need to update. */
if (rendered__max_refine_scale == options->max_refine_scale &&
rendered_xoff == sc->xoff &&
rendered_yoff == sc->yoff)
return FALSE;
gimp_item_get_offset (GIMP_ITEM (bg), &off_x, &off_y);
gegl_node_set (sc->sc_node,
"xoff", (gint) sc->xoff - off_x,
"yoff", (gint) sc->yoff - off_y,
"max-refine-scale", (gint) options->max_refine_scale,
NULL);
rendered__max_refine_scale = options->max_refine_scale;
rendered_xoff = sc->xoff;
rendered_yoff = sc->yoff;
return TRUE;
}
static void
gimp_seamless_clone_tool_create_filter (GimpSeamlessCloneTool *sc,
GimpDrawable *drawable)
{
if (! sc->render_node)
gimp_seamless_clone_tool_create_render_node (sc);
sc->filter = gimp_drawable_filter_new (drawable,
_("Seamless Clone"),
sc->render_node,
GIMP_ICON_TOOL_SEAMLESS_CLONE);
gimp_drawable_filter_set_region (sc->filter, GIMP_FILTER_REGION_DRAWABLE);
g_signal_connect (sc->filter, "flush",
G_CALLBACK (gimp_seamless_clone_tool_filter_flush),
sc);
}
static void
gimp_seamless_clone_tool_filter_flush (GimpDrawableFilter *filter,
GimpTool *tool)
{
GimpImage *image = gimp_display_get_image (tool->display);
gimp_projection_flush (gimp_image_get_projection (image));
}
static void
gimp_seamless_clone_tool_filter_update (GimpSeamlessCloneTool *sc)
{
GimpTool *tool = GIMP_TOOL (sc);
GimpDisplayShell *shell = gimp_display_get_shell (tool->display);
GimpItem *item = GIMP_ITEM (tool->drawable);
gint x, y;
gint w, h;
gint off_x, off_y;
GeglRectangle visible;
GeglOperation *op = NULL;
GimpProgress *progress;
GeglNode *output;
GeglProcessor *processor;
gdouble value;
progress = gimp_progress_start (GIMP_PROGRESS (sc), FALSE,
_("Cloning the foreground object"));
/* Find out at which x,y is the top left corner of the currently
* displayed part */
gimp_display_shell_untransform_viewport (shell, ! shell->show_all,
&x, &y, &w, &h);
/* Find out where is our drawable positioned */
gimp_item_get_offset (item, &off_x, &off_y);
/* Create a rectangle from the intersection of the currently displayed
* part with the drawable */
gimp_rectangle_intersect (x, y, w, h,
off_x,
off_y,
gimp_item_get_width (item),
gimp_item_get_height (item),
&visible.x,
&visible.y,
&visible.width,
&visible.height);
/* Since the filter_apply function receives a rectangle describing
* where it should update the preview, and since that rectangle should
* be relative to the drawable's location, we now offset back by the
* drawable's offsetts. */
visible.x -= off_x;
visible.y -= off_y;
g_object_get (sc->sc_node, "gegl-operation", &op, NULL);
/* If any cache of the visible area was present, clear it!
* We need to clear the cache in the sc_node, since that is
* where the previous paste was located
*/
gegl_operation_invalidate (op, &visible, TRUE);
g_object_unref (op);
/* Now update the image map and show this area */
gimp_drawable_filter_apply (sc->filter, NULL);
/* Show update progress. */
output = gegl_node_get_output_proxy (sc->render_node, "output");
processor = gegl_node_new_processor (output, NULL);
while (gegl_processor_work (processor, &value))
{
if (progress)
gimp_progress_set_value (progress, value);
}
if (progress)
gimp_progress_end (progress);
g_object_unref (processor);
}
|