1
/* The GIMP -- an image manipulation program
2
* 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 2 of the License, or
7
* (at your option) any later version.
9
* This program is distributed in the hope that it will be useful,
10
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12
* GNU General Public License for more details.
14
* You should have received a copy of the GNU General Public License
15
* along with this program; if not, write to the Free Software
16
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
21
#include <glib-object.h>
23
#include "libgimpmath/gimpmath.h"
25
#include "core-types.h"
27
#include "base/boundary.h"
28
#include "base/pixel-region.h"
29
#include "base/tile-manager.h"
31
#include "paint-funcs/paint-funcs.h"
33
#include "gimpimage.h"
34
#include "gimpimage-undo.h"
35
#include "gimpimage-undo-push.h"
36
#include "gimplayer.h"
37
#include "gimplayer-floating-sel.h"
38
#include "gimplayermask.h"
40
#include "gimp-intl.h"
44
floating_sel_attach (GimpLayer *layer,
45
GimpDrawable *drawable)
48
GimpLayer *floating_sel;
50
g_return_if_fail (GIMP_IS_LAYER (layer));
51
g_return_if_fail (GIMP_IS_DRAWABLE (drawable));
52
g_return_if_fail (gimp_item_is_attached (GIMP_ITEM (drawable)));
53
g_return_if_fail (drawable != GIMP_DRAWABLE (layer));
54
g_return_if_fail (gimp_item_get_image (GIMP_ITEM (layer)) ==
55
gimp_item_get_image (GIMP_ITEM (drawable)));
57
gimage = gimp_item_get_image (GIMP_ITEM (drawable));
59
floating_sel = gimp_image_floating_sel (gimage);
61
/* If there is already a floating selection, anchor it */
64
floating_sel_anchor (floating_sel);
66
/* if we were pasting to the old floating selection, paste now
69
if (drawable == (GimpDrawable *) floating_sel)
70
drawable = gimp_image_active_drawable (gimage);
73
/* set the drawable and allocate a backing store */
74
gimp_layer_set_preserve_trans (layer, TRUE, FALSE);
75
layer->fs.drawable = drawable;
76
layer->fs.backing_store = tile_manager_new (GIMP_ITEM (layer)->width,
77
GIMP_ITEM (layer)->height,
78
gimp_drawable_bytes (drawable));
80
/* add the layer to the gimage */
81
gimp_image_add_layer (gimage, layer, 0);
83
/* store the affected area from the drawable in the backing store */
84
floating_sel_rigor (layer, TRUE);
88
floating_sel_remove (GimpLayer *layer)
92
g_return_if_fail (GIMP_IS_LAYER (layer));
93
g_return_if_fail (gimp_layer_is_floating_sel (layer));
95
gimage = gimp_item_get_image (GIMP_ITEM (layer->fs.drawable));
97
gimp_image_undo_group_start (gimage, GIMP_UNDO_GROUP_FS_REMOVE,
98
_("Remove Floating Selection"));
100
/* store the affected area from the drawable in the backing store */
101
floating_sel_relax (layer, TRUE);
103
/* Invalidate the preview of the obscured drawable. We do this here
104
* because it will not be done until the floating selection is removed,
105
* at which point the obscured drawable's preview will not be declared
108
gimp_viewable_invalidate_preview (GIMP_VIEWABLE (layer));
110
/* remove the layer from the gimage */
111
gimp_image_remove_layer (gimage, layer);
113
gimp_image_undo_group_end (gimage);
117
floating_sel_anchor (GimpLayer *layer)
120
GimpDrawable *drawable;
122
g_return_if_fail (GIMP_IS_LAYER (layer));
123
g_return_if_fail (gimp_layer_is_floating_sel (layer));
125
gimage = gimp_item_get_image (GIMP_ITEM (layer));
127
if (! gimp_layer_is_floating_sel (layer))
129
g_message (_("Cannot anchor this layer because "
130
"it is not a floating selection."));
134
/* Start a floating selection anchoring undo */
135
gimp_image_undo_group_start (gimage, GIMP_UNDO_GROUP_FS_ANCHOR,
136
_("Anchor Floating Selection"));
138
/* Invalidate the previews of the layer that will be composited
139
* with the floating section.
141
gimp_viewable_invalidate_preview (GIMP_VIEWABLE (layer->fs.drawable));
143
/* Relax the floating selection */
144
floating_sel_relax (layer, TRUE);
146
/* Composite the floating selection contents */
147
floating_sel_composite (layer,
148
GIMP_ITEM (layer)->offset_x,
149
GIMP_ITEM (layer)->offset_y,
150
GIMP_ITEM (layer)->width,
151
GIMP_ITEM (layer)->height, TRUE);
153
drawable = layer->fs.drawable;
155
/* remove the floating selection */
156
gimp_image_remove_layer (gimage, layer);
158
/* end the group undo */
159
gimp_image_undo_group_end (gimage);
161
/* invalidate the boundaries */
162
gimp_drawable_invalidate_boundary (GIMP_DRAWABLE (gimp_image_get_mask (gimage)));
166
floating_sel_activate_drawable (GimpLayer *layer)
170
g_return_if_fail (GIMP_IS_LAYER (layer));
171
g_return_if_fail (gimp_layer_is_floating_sel (layer));
173
gimage = gimp_item_get_image (GIMP_ITEM (layer));
175
/* set the underlying drawable to active */
176
if (GIMP_IS_LAYER_MASK (layer->fs.drawable))
178
GimpLayerMask *mask = GIMP_LAYER_MASK (layer->fs.drawable);
180
gimp_image_set_active_layer (gimage, gimp_layer_mask_get_layer (mask));
182
else if (GIMP_IS_CHANNEL (layer->fs.drawable))
184
gimp_image_set_active_channel (gimage, GIMP_CHANNEL (layer->fs.drawable));
188
gimp_image_set_active_layer (gimage, GIMP_LAYER (layer->fs.drawable));
193
floating_sel_to_layer (GimpLayer *layer)
198
g_return_if_fail (GIMP_IS_LAYER (layer));
199
g_return_if_fail (gimp_layer_is_floating_sel (layer));
201
item = GIMP_ITEM (layer);
203
if (! (gimage = gimp_item_get_image (item)))
206
/* Check if the floating layer belongs to a channel... */
207
if (GIMP_IS_CHANNEL (layer->fs.drawable))
209
g_message (_("Cannot create a new layer from the floating selection "
210
"because it belongs to a layer mask or channel."));
214
gimp_image_undo_group_start (gimage, GIMP_UNDO_GROUP_FS_TO_LAYER,
215
_("Floating Selection to Layer"));
217
/* restore the contents of the drawable */
218
floating_sel_restore (layer,
224
gimp_image_undo_push_fs_to_layer (gimage, NULL,
225
layer, layer->fs.drawable);
227
/* clear the selection */
228
gimp_drawable_invalidate_boundary (GIMP_DRAWABLE (layer));
231
layer->fs.drawable = NULL;
232
gimage->floating_sel = NULL;
233
gimp_item_set_visible (GIMP_ITEM (layer), TRUE, TRUE);
235
gimp_image_undo_group_end (gimage);
237
gimp_object_name_changed (GIMP_OBJECT (layer));
239
gimp_drawable_update (GIMP_DRAWABLE (layer),
241
GIMP_ITEM (layer)->width,
242
GIMP_ITEM (layer)->height);
244
gimp_image_floating_selection_changed (gimage);
248
floating_sel_store (GimpLayer *layer,
254
PixelRegion srcPR, destPR;
258
g_return_if_fail (GIMP_IS_LAYER (layer));
259
g_return_if_fail (gimp_layer_is_floating_sel (layer));
261
/* Check the backing store & make sure it has the correct dimensions */
262
if ((tile_manager_width (layer->fs.backing_store) !=
263
gimp_item_width (GIMP_ITEM(layer))) ||
264
(tile_manager_height (layer->fs.backing_store) !=
265
gimp_item_height (GIMP_ITEM(layer))) ||
266
(tile_manager_bpp (layer->fs.backing_store) !=
267
gimp_drawable_bytes (layer->fs.drawable)))
269
/* free the backing store and allocate anew */
270
tile_manager_unref (layer->fs.backing_store);
272
layer->fs.backing_store =
273
tile_manager_new (GIMP_ITEM (layer)->width,
274
GIMP_ITEM (layer)->height,
275
gimp_drawable_bytes (layer->fs.drawable));
278
/* What this function does is save the specified area of the
279
* drawable that this floating selection obscures. We do this so
280
* that it will be possible to subsequently restore the drawable's area
282
gimp_item_offsets (GIMP_ITEM (layer->fs.drawable), &offx, &offy);
284
/* Find the minimum area we need to uncover -- in gimage space */
285
x1 = MAX (GIMP_ITEM (layer)->offset_x, offx);
286
y1 = MAX (GIMP_ITEM (layer)->offset_y, offy);
287
x2 = MIN (GIMP_ITEM (layer)->offset_x + GIMP_ITEM (layer)->width,
288
offx + gimp_item_width (GIMP_ITEM (layer->fs.drawable)));
289
y2 = MIN (GIMP_ITEM (layer)->offset_y + GIMP_ITEM (layer)->height,
290
offy + gimp_item_height (GIMP_ITEM (layer->fs.drawable)));
292
x1 = CLAMP (x, x1, x2);
293
y1 = CLAMP (y, y1, y2);
294
x2 = CLAMP (x + w, x1, x2);
295
y2 = CLAMP (y + h, y1, y2);
297
if ((x2 - x1) > 0 && (y2 - y1) > 0)
299
/* Copy the area from the drawable to the backing store */
300
pixel_region_init (&srcPR, gimp_drawable_data (layer->fs.drawable),
301
(x1 - offx), (y1 - offy), (x2 - x1), (y2 - y1), FALSE);
302
pixel_region_init (&destPR, layer->fs.backing_store,
303
(x1 - GIMP_ITEM (layer)->offset_x),
304
(y1 - GIMP_ITEM (layer)->offset_y),
305
(x2 - x1), (y2 - y1), TRUE);
307
copy_region (&srcPR, &destPR);
312
floating_sel_restore (GimpLayer *layer,
318
PixelRegion srcPR, destPR;
322
g_return_if_fail (GIMP_IS_LAYER (layer));
323
g_return_if_fail (gimp_layer_is_floating_sel (layer));
325
/* What this function does is "uncover" the specified area in the
326
* drawable that this floating selection obscures. We do this so
327
* that either the floating selection can be removed or it can be
331
/* Find the minimum area we need to uncover -- in gimage space */
332
gimp_item_offsets (GIMP_ITEM (layer->fs.drawable), &offx, &offy);
334
x1 = MAX (GIMP_ITEM (layer)->offset_x, offx);
335
y1 = MAX (GIMP_ITEM (layer)->offset_y, offy);
336
x2 = MIN (GIMP_ITEM (layer)->offset_x + GIMP_ITEM (layer)->width,
337
offx + gimp_item_width (GIMP_ITEM (layer->fs.drawable)));
338
y2 = MIN (GIMP_ITEM(layer)->offset_y + GIMP_ITEM (layer)->height,
339
offy + gimp_item_height (GIMP_ITEM (layer->fs.drawable)));
341
x1 = CLAMP (x, x1, x2);
342
y1 = CLAMP (y, y1, y2);
343
x2 = CLAMP (x + w, x1, x2);
344
y2 = CLAMP (y + h, y1, y2);
346
if ((x2 - x1) > 0 && (y2 - y1) > 0)
348
/* Copy the area from the backing store to the drawable */
349
pixel_region_init (&srcPR, layer->fs.backing_store,
350
(x1 - GIMP_ITEM (layer)->offset_x),
351
(y1 - GIMP_ITEM (layer)->offset_y),
352
(x2 - x1), (y2 - y1), FALSE);
353
pixel_region_init (&destPR, gimp_drawable_data (layer->fs.drawable),
354
(x1 - offx), (y1 - offy), (x2 - x1), (y2 - y1), TRUE);
356
copy_region (&srcPR, &destPR);
361
floating_sel_rigor (GimpLayer *layer,
364
g_return_if_fail (GIMP_IS_LAYER (layer));
365
g_return_if_fail (gimp_layer_is_floating_sel (layer));
367
/* store the affected area from the drawable in the backing store */
368
floating_sel_store (layer,
369
GIMP_ITEM (layer)->offset_x,
370
GIMP_ITEM (layer)->offset_y,
371
GIMP_ITEM (layer)->width,
372
GIMP_ITEM (layer)->height);
373
layer->fs.initial = TRUE;
376
gimp_image_undo_push_fs_rigor (gimp_item_get_image (GIMP_ITEM (layer)),
382
floating_sel_relax (GimpLayer *layer,
385
g_return_if_fail (GIMP_IS_LAYER (layer));
386
g_return_if_fail (gimp_layer_is_floating_sel (layer));
388
/* restore the contents of drawable the floating layer is attached to */
389
if (layer->fs.initial == FALSE)
390
floating_sel_restore (layer,
391
GIMP_ITEM (layer)->offset_x,
392
GIMP_ITEM (layer)->offset_y,
393
GIMP_ITEM (layer)->width,
394
GIMP_ITEM (layer)->height);
395
layer->fs.initial = TRUE;
398
gimp_image_undo_push_fs_relax (gimp_item_get_image (GIMP_ITEM (layer)),
404
floating_sel_composite (GimpLayer *layer,
413
GimpLayer *d_layer = NULL;
415
gint active[MAX_CHANNELS];
420
g_return_if_fail (GIMP_IS_LAYER (layer));
421
g_return_if_fail (gimp_layer_is_floating_sel (layer));
423
if (! (gimage = gimp_item_get_image (GIMP_ITEM (layer))))
426
/* What this function does is composite the specified area of the
427
* drawble with the floating selection. We do this when the gimage
428
* is constructed, before any other composition takes place.
431
/* If this isn't the first composite,
432
* restore the image underneath
434
if (! layer->fs.initial)
435
floating_sel_restore (layer, x, y, w, h);
436
else if (gimp_item_get_visible (GIMP_ITEM (layer)))
437
layer->fs.initial = FALSE;
439
/* First restore what's behind the image if necessary,
440
* then check for visibility
442
if (gimp_item_get_visible (GIMP_ITEM (layer)))
444
/* Find the minimum area we need to composite -- in gimage space */
445
gimp_item_offsets (GIMP_ITEM (layer->fs.drawable), &offx, &offy);
447
x1 = MAX (GIMP_ITEM (layer)->offset_x, offx);
448
y1 = MAX (GIMP_ITEM (layer)->offset_y, offy);
449
x2 = MIN (GIMP_ITEM (layer)->offset_x +
450
GIMP_ITEM (layer)->width,
451
offx + gimp_item_width (GIMP_ITEM (layer->fs.drawable)));
452
y2 = MIN (GIMP_ITEM (layer)->offset_y +
453
GIMP_ITEM (layer)->height,
454
offy + gimp_item_height (GIMP_ITEM (layer->fs.drawable)));
456
x1 = CLAMP (x, x1, x2);
457
y1 = CLAMP (y, y1, y2);
458
x2 = CLAMP (x + w, x1, x2);
459
y2 = CLAMP (y + h, y1, y2);
461
if ((x2 - x1) > 0 && (y2 - y1) > 0)
463
/* composite the area from the layer to the drawable */
464
pixel_region_init (&fsPR, GIMP_DRAWABLE (layer)->tiles,
465
(x1 - GIMP_ITEM (layer)->offset_x),
466
(y1 - GIMP_ITEM (layer)->offset_y),
467
(x2 - x1), (y2 - y1), FALSE);
469
/* a kludge here to prevent the case of the drawable
470
* underneath having preserve transparency on, and disallowing
471
* the composited floating selection from being shown
473
if (GIMP_IS_LAYER (layer->fs.drawable))
475
d_layer = GIMP_LAYER (layer->fs.drawable);
476
if ((preserve_trans = gimp_layer_get_preserve_trans (d_layer)))
477
gimp_layer_set_preserve_trans (d_layer, FALSE, FALSE);
480
preserve_trans = FALSE;
482
/* We need to set all gimage channels to active to make sure that
483
* nothing strange happens while applying the floating selection.
484
* It wouldn't make sense for the floating selection to be affected
485
* by the active gimage channels.
487
for (i = 0; i < MAX_CHANNELS; i++)
489
active[i] = gimage->active[i];
490
gimage->active[i] = 1;
493
/* apply the fs with the undo specified by the value
494
* passed to this function
496
gimp_drawable_apply_region (layer->fs.drawable, &fsPR,
501
(x1 - offx), (y1 - offy));
503
/* restore preserve transparency */
505
gimp_layer_set_preserve_trans (d_layer, TRUE, FALSE);
507
/* restore gimage active channels */
508
for (i = 0; i < MAX_CHANNELS; i++)
509
gimage->active[i] = active[i];
515
floating_sel_boundary (GimpLayer *layer,
521
g_return_val_if_fail (GIMP_IS_LAYER (layer), NULL);
522
g_return_val_if_fail (gimp_layer_is_floating_sel (layer), NULL);
523
g_return_val_if_fail (n_segs != NULL, NULL);
525
if (layer->fs.boundary_known == FALSE)
528
g_free (layer->fs.segs);
530
/* find the segments */
531
pixel_region_init (&bPR, GIMP_DRAWABLE (layer)->tiles,
533
GIMP_ITEM (layer)->width,
534
GIMP_ITEM (layer)->height, FALSE);
535
layer->fs.segs = find_mask_boundary (&bPR, &layer->fs.num_segs,
537
GIMP_ITEM (layer)->width,
538
GIMP_ITEM (layer)->height,
541
/* offset the segments */
542
for (i = 0; i < layer->fs.num_segs; i++)
544
layer->fs.segs[i].x1 += GIMP_ITEM (layer)->offset_x;
545
layer->fs.segs[i].y1 += GIMP_ITEM (layer)->offset_y;
546
layer->fs.segs[i].x2 += GIMP_ITEM (layer)->offset_x;
547
layer->fs.segs[i].y2 += GIMP_ITEM (layer)->offset_y;
550
layer->fs.boundary_known = TRUE;
553
*n_segs = layer->fs.num_segs;
555
return layer->fs.segs;
559
floating_sel_invalidate (GimpLayer *layer)
561
g_return_if_fail (GIMP_IS_LAYER (layer));
562
g_return_if_fail (gimp_layer_is_floating_sel (layer));
564
/* Invalidate the attached-to drawable's preview */
565
gimp_viewable_invalidate_preview (GIMP_VIEWABLE (layer->fs.drawable));
567
/* Invalidate the boundary */
568
layer->fs.boundary_known = FALSE;