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.
23
#include <glib-object.h>
25
#include "core-types.h"
27
#include "base/pixel-region.h"
28
#include "base/temp-buf.h"
29
#include "base/tile-manager.h"
30
#include "base/tile-manager-crop.h"
32
#include "paint-funcs/paint-funcs.h"
35
#include "gimp-edit.h"
36
#include "gimp-utils.h"
37
#include "gimpbuffer.h"
38
#include "gimpchannel.h"
39
#include "gimpcontext.h"
40
#include "gimpimage.h"
41
#include "gimpimage-undo.h"
42
#include "gimplayer.h"
43
#include "gimplayer-floating-sel.h"
45
#include "gimppattern.h"
46
#include "gimpprojection.h"
47
#include "gimpselection.h"
49
#include "gimp-intl.h"
52
/* local function protypes */
54
static const GimpBuffer * gimp_edit_extract (GimpImage *gimage,
55
GimpDrawable *drawable,
58
static gboolean gimp_edit_fill_internal (GimpImage *gimage,
59
GimpDrawable *drawable,
61
GimpFillType fill_type,
62
const gchar *undo_desc);
65
/* public functions */
68
gimp_edit_cut (GimpImage *gimage,
69
GimpDrawable *drawable,
72
g_return_val_if_fail (GIMP_IS_IMAGE (gimage), NULL);
73
g_return_val_if_fail (GIMP_IS_DRAWABLE (drawable), NULL);
74
g_return_val_if_fail (gimp_item_is_attached (GIMP_ITEM (drawable)), NULL);
75
g_return_val_if_fail (GIMP_IS_CONTEXT (context), NULL);
77
return gimp_edit_extract (gimage, drawable, context, TRUE);
81
gimp_edit_copy (GimpImage *gimage,
82
GimpDrawable *drawable,
85
g_return_val_if_fail (GIMP_IS_IMAGE (gimage), NULL);
86
g_return_val_if_fail (GIMP_IS_DRAWABLE (drawable), NULL);
87
g_return_val_if_fail (gimp_item_is_attached (GIMP_ITEM (drawable)), NULL);
88
g_return_val_if_fail (GIMP_IS_CONTEXT (context), NULL);
90
return gimp_edit_extract (gimage, drawable, context, FALSE);
94
gimp_edit_copy_visible (GimpImage *gimage,
97
PixelRegion srcPR, destPR;
102
g_return_val_if_fail (GIMP_IS_IMAGE (gimage), NULL);
103
g_return_val_if_fail (GIMP_IS_CONTEXT (context), NULL);
105
non_empty = gimp_channel_bounds (gimp_image_get_mask (gimage),
107
if ((x1 == x2) || (y1 == y2))
109
g_message (_("Unable to cut or copy because the "
110
"selected region is empty."));
114
tiles = tile_manager_new (x2 - x1, y2 - y1,
115
gimp_projection_get_bytes (gimage->projection));
116
tile_manager_set_offsets (tiles, x1, y1);
118
pixel_region_init (&srcPR, gimp_projection_get_tiles (gimage->projection),
122
pixel_region_init (&destPR, tiles,
127
/* use EEKy no-COW copying because sharing tiles with the projection
128
* is buggy as hell, probably because tile_invalidate() doesn't
129
* do what it should --mitch
131
copy_region_nocow (&srcPR, &destPR);
133
/* Only crop if the gimage mask wasn't empty */
136
TileManager *crop = tile_manager_crop (tiles, 0);
140
tile_manager_unref (tiles);
147
GimpBuffer *buffer = gimp_buffer_new (tiles, "Global Buffer", FALSE);
149
gimp_set_global_buffer (gimage->gimp, buffer);
150
g_object_unref (buffer);
152
return gimage->gimp->global_buffer;
159
gimp_edit_paste (GimpImage *gimage,
160
GimpDrawable *drawable,
166
gint viewport_height)
177
g_return_val_if_fail (GIMP_IS_IMAGE (gimage), NULL);
178
g_return_val_if_fail (drawable == NULL || GIMP_IS_DRAWABLE (drawable), NULL);
179
g_return_val_if_fail (drawable == NULL ||
180
gimp_item_is_attached (GIMP_ITEM (drawable)), NULL);
181
g_return_val_if_fail (GIMP_IS_BUFFER (paste), NULL);
183
/* Make a new layer: if drawable == NULL,
184
* user is pasting into an empty image.
188
type = gimp_drawable_type_with_alpha (drawable);
190
type = gimp_image_base_type_with_alpha (gimage);
192
layer = gimp_layer_new_from_tiles (paste->tiles,
196
GIMP_OPACITY_OPAQUE, GIMP_NORMAL_MODE);
203
/* if pasting to a drawable */
207
gint paste_x, paste_y;
208
gint paste_width, paste_height;
211
gimp_item_offsets (GIMP_ITEM (drawable), &off_x, &off_y);
212
have_mask = gimp_drawable_mask_bounds (drawable, &x1, &y1, &x2, &y2);
215
viewport_width > 0 &&
216
viewport_height > 0 &&
217
gimp_rectangle_intersect (viewport_x, viewport_y,
218
viewport_width, viewport_height,
222
&paste_width, &paste_height))
224
center_x = paste_x + paste_width / 2;
225
center_y = paste_y + paste_height / 2;
229
center_x = off_x + (x1 + x2) / 2;
230
center_y = off_y + (y1 + y2) / 2;
233
else if (viewport_width > 0 && viewport_height > 0)
235
/* if we got a viewport set the offsets to the center of the viewport */
237
center_x = viewport_x + viewport_width / 2;
238
center_y = viewport_y + viewport_height / 2;
242
/* otherwise the offsets to the center of the image */
244
center_x = gimage->width / 2;
245
center_y = gimage->height / 2;
248
width = gimp_item_width (GIMP_ITEM (layer));
249
height = gimp_item_height (GIMP_ITEM (layer));
251
offset_x = center_x - width / 2;
252
offset_y = center_y - height / 2;
254
/* Ensure that the pasted layer is always within the image, if it
255
* fits and aligned at top left if it doesn't. (See bug #142944).
257
offset_x = MIN (offset_x, gimage->width - width);
258
offset_y = MIN (offset_y, gimage->height - height);
259
offset_x = MAX (offset_x, 0);
260
offset_y = MAX (offset_y, 0);
262
GIMP_ITEM (layer)->offset_x = offset_x;
263
GIMP_ITEM (layer)->offset_y = offset_y;
265
/* Start a group undo */
266
gimp_image_undo_group_start (gimage, GIMP_UNDO_GROUP_EDIT_PASTE,
269
/* If there is a selection mask clear it--
270
* this might not always be desired, but in general,
271
* it seems like the correct behavior.
273
if (! gimp_channel_is_empty (gimp_image_get_mask (gimage)) && ! paste_into)
274
gimp_channel_clear (gimp_image_get_mask (gimage), NULL, TRUE);
276
/* if there's a drawable, add a new floating selection */
278
floating_sel_attach (layer, drawable);
280
gimp_image_add_layer (gimage, layer, 0);
282
/* end the group undo */
283
gimp_image_undo_group_end (gimage);
289
gimp_edit_paste_as_new (Gimp *gimp,
296
g_return_val_if_fail (GIMP_IS_GIMP (gimp), NULL);
297
g_return_val_if_fail (invoke == NULL || GIMP_IS_IMAGE (invoke), NULL);
298
g_return_val_if_fail (GIMP_IS_BUFFER (paste), NULL);
300
/* create a new image (always of type GIMP_RGB) */
301
gimage = gimp_create_image (gimp,
302
gimp_buffer_get_width (paste),
303
gimp_buffer_get_height (paste),
306
gimp_image_undo_disable (gimage);
310
gimp_image_set_resolution (gimage,
311
invoke->xresolution, invoke->yresolution);
312
gimp_image_set_unit (gimage,
313
gimp_image_get_unit (invoke));
316
layer = gimp_layer_new_from_tiles (paste->tiles,
318
gimp_image_base_type_with_alpha (gimage),
320
GIMP_OPACITY_OPAQUE, GIMP_NORMAL_MODE);
324
g_object_unref (gimage);
328
gimp_image_add_layer (gimage, layer, 0);
330
gimp_image_undo_enable (gimage);
332
gimp_create_display (gimp, gimage, GIMP_UNIT_PIXEL, 1.0);
333
g_object_unref (gimage);
339
gimp_edit_clear (GimpImage *gimage,
340
GimpDrawable *drawable,
341
GimpContext *context)
343
g_return_val_if_fail (GIMP_IS_IMAGE (gimage), FALSE);
344
g_return_val_if_fail (GIMP_IS_DRAWABLE (drawable), FALSE);
345
g_return_val_if_fail (gimp_item_is_attached (GIMP_ITEM (drawable)), FALSE);
346
g_return_val_if_fail (GIMP_IS_CONTEXT (context), FALSE);
348
return gimp_edit_fill_internal (gimage, drawable, context,
349
GIMP_TRANSPARENT_FILL,
354
gimp_edit_fill (GimpImage *gimage,
355
GimpDrawable *drawable,
356
GimpContext *context,
357
GimpFillType fill_type)
359
const gchar *undo_desc;
361
g_return_val_if_fail (GIMP_IS_IMAGE (gimage), FALSE);
362
g_return_val_if_fail (GIMP_IS_DRAWABLE (drawable), FALSE);
363
g_return_val_if_fail (gimp_item_is_attached (GIMP_ITEM (drawable)), FALSE);
364
g_return_val_if_fail (GIMP_IS_CONTEXT (context), FALSE);
368
case GIMP_FOREGROUND_FILL:
369
undo_desc = _("Fill with FG Color");
372
case GIMP_BACKGROUND_FILL:
373
undo_desc = _("Fill with BG Color");
376
case GIMP_WHITE_FILL:
377
undo_desc = _("Fill with White");
380
case GIMP_TRANSPARENT_FILL:
381
undo_desc = _("Fill with Transparency");
384
case GIMP_PATTERN_FILL:
385
undo_desc = _("Fill with Pattern");
389
return TRUE; /* nothing to do, but the fill succeded */
392
g_warning ("%s: unknown fill type", G_STRFUNC);
393
fill_type = GIMP_BACKGROUND_FILL;
394
undo_desc = _("Fill with BG Color");
398
return gimp_edit_fill_internal (gimage, drawable, context,
399
fill_type, undo_desc);
403
/* private functions */
406
gimp_edit_extract (GimpImage *gimage,
407
GimpDrawable *drawable,
408
GimpContext *context,
414
/* See if the gimage mask is empty */
415
empty = gimp_channel_is_empty (gimp_image_get_mask (gimage));
418
gimp_image_undo_group_start (gimage, GIMP_UNDO_GROUP_EDIT_CUT, _("Cut"));
420
/* Cut/copy the mask portion from the gimage */
421
tiles = gimp_selection_extract (gimp_image_get_mask (gimage),
422
drawable, context, cut_pixels, FALSE, TRUE);
425
gimp_image_undo_group_end (gimage);
427
/* Only crop if the gimage mask wasn't empty */
428
if (tiles && ! empty)
430
TileManager *crop = tile_manager_crop (tiles, 0);
434
tile_manager_unref (tiles);
441
GimpBuffer *buffer = gimp_buffer_new (tiles, "Global Buffer", FALSE);
443
gimp_set_global_buffer (gimage->gimp, buffer);
444
g_object_unref (buffer);
446
return gimage->gimp->global_buffer;
453
gimp_edit_fill_internal (GimpImage *gimage,
454
GimpDrawable *drawable,
455
GimpContext *context,
456
GimpFillType fill_type,
457
const gchar *undo_desc)
459
TileManager *buf_tiles;
461
gint x, y, width, height;
463
guchar col[MAX_CHANNELS];
464
TempBuf *pat_buf = NULL;
467
if (! gimp_drawable_mask_intersect (drawable, &x, &y, &width, &height))
468
return TRUE; /* nothing to do, but the fill succeded */
470
tiles_bytes = gimp_drawable_bytes (drawable);
474
case GIMP_FOREGROUND_FILL:
475
gimp_image_get_foreground (gimage, drawable, context, col);
478
case GIMP_BACKGROUND_FILL:
479
case GIMP_TRANSPARENT_FILL:
480
gimp_image_get_background (gimage, drawable, context, col);
483
case GIMP_WHITE_FILL:
485
guchar tmp_col[MAX_CHANNELS];
487
tmp_col[RED_PIX] = 255;
488
tmp_col[GREEN_PIX] = 255;
489
tmp_col[BLUE_PIX] = 255;
490
gimp_image_transform_color (gimage, drawable, col, GIMP_RGB, tmp_col);
494
case GIMP_PATTERN_FILL:
496
GimpPattern *pattern = gimp_context_get_pattern (context);
498
pat_buf = gimp_image_transform_temp_buf (gimage, drawable,
499
pattern->mask, &new_buf);
501
if (! gimp_drawable_has_alpha (drawable) &&
502
(pat_buf->bytes == 2 || pat_buf->bytes == 4))
508
return TRUE; /* nothing to do, but the fill succeded */
511
buf_tiles = tile_manager_new (width, height, tiles_bytes);
513
pixel_region_init (&bufPR, buf_tiles, 0, 0, width, height, TRUE);
517
pattern_region (&bufPR, NULL, pat_buf, 0, 0);
520
temp_buf_free (pat_buf);
524
if (gimp_drawable_has_alpha (drawable))
525
col[gimp_drawable_bytes (drawable) - 1] = OPAQUE_OPACITY;
527
color_region (&bufPR, col);
530
pixel_region_init (&bufPR, buf_tiles, 0, 0, width, height, FALSE);
531
gimp_drawable_apply_region (drawable, &bufPR,
534
(fill_type == GIMP_TRANSPARENT_FILL) ?
535
GIMP_ERASE_MODE : GIMP_NORMAL_MODE,
538
tile_manager_unref (buf_tiles);
540
gimp_drawable_update (drawable, x, y, width, height);