gimp-edit.c 20.4 KB
Newer Older
1
/* GIMP - The GNU Image Manipulation Program
Elliot Lee's avatar
Elliot Lee committed
2 3
 * Copyright (C) 1995 Spencer Kimball and Peter Mattis
 *
4
 * This program is free software: you can redistribute it and/or modify
Elliot Lee's avatar
Elliot Lee committed
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
Elliot Lee's avatar
Elliot Lee committed
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/>.
Elliot Lee's avatar
Elliot Lee committed
16
 */
Sven Neumann's avatar
Sven Neumann committed
17 18 19

#include "config.h"

Elliot Lee's avatar
Elliot Lee committed
20
#include <stdlib.h>
Sven Neumann's avatar
Sven Neumann committed
21

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

26
#include "libgimpbase/gimpbase.h"
27
#include "libgimpcolor/gimpcolor.h"
28

29
#include "core-types.h"
Sven Neumann's avatar
Sven Neumann committed
30

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

33
#include "gimp.h"
34
#include "gimp-edit.h"
35
#include "gimp-utils.h"
Michael Natterer's avatar
Michael Natterer committed
36 37 38
#include "gimpbuffer.h"
#include "gimpchannel.h"
#include "gimpcontext.h"
39
#include "gimpfilloptions.h"
40
#include "gimpdrawableundo.h"
Michael Natterer's avatar
Michael Natterer committed
41
#include "gimpimage.h"
42
#include "gimpimage-new.h"
43
#include "gimpimage-undo.h"
Michael Natterer's avatar
Michael Natterer committed
44
#include "gimplayer.h"
45
#include "gimplayer-floating-selection.h"
46
#include "gimplayer-new.h"
Michael Natterer's avatar
Michael Natterer committed
47
#include "gimplist.h"
48
#include "gimppickable.h"
49
#include "gimpselection.h"
50
#include "gimptempbuf.h"
Michael Natterer's avatar
Michael Natterer committed
51

52
#include "gimp-intl.h"
53

54

55 56
/*  local function protypes  */

57 58 59 60 61
static GimpBuffer * gimp_edit_extract (GimpImage     *image,
                                       GimpPickable  *pickable,
                                       GimpContext   *context,
                                       gboolean       cut_pixels,
                                       GError       **error);
62 63 64 65


/*  public functions  */

66
GimpObject *
67 68 69 70
gimp_edit_cut (GimpImage     *image,
               GimpDrawable  *drawable,
               GimpContext   *context,
               GError       **error)
Elliot Lee's avatar
Elliot Lee committed
71
{
72
  g_return_val_if_fail (GIMP_IS_IMAGE (image), NULL);
Michael Natterer's avatar
Michael Natterer committed
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);
76
  g_return_val_if_fail (error == NULL || *error == NULL, NULL);
Elliot Lee's avatar
Elliot Lee committed
77

78 79 80 81
  if (GIMP_IS_LAYER (drawable) &&
      gimp_channel_is_empty (gimp_image_get_mask (image)))
    {
      GimpImage *clip_image;
82

83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98
      clip_image = gimp_image_new_from_drawable (image->gimp, drawable);
      gimp_container_remove (image->gimp->images, GIMP_OBJECT (clip_image));
      gimp_set_clipboard_image (image->gimp, clip_image);
      g_object_unref (clip_image);

      gimp_image_undo_group_start (image, GIMP_UNDO_GROUP_EDIT_CUT,
                                   C_("undo-type", "Cut Layer"));

      gimp_image_remove_layer (image, GIMP_LAYER (drawable),
                               TRUE, NULL);

      gimp_image_undo_group_end (image);

      return GIMP_OBJECT (gimp_get_clipboard_image (image->gimp));
    }
  else
99
    {
100
      GimpBuffer *buffer;
101

102 103 104 105 106 107 108 109 110 111
      buffer = gimp_edit_extract (image, GIMP_PICKABLE (drawable),
                                  context, TRUE, error);

      if (buffer)
        {
          gimp_set_clipboard_buffer (image->gimp, buffer);
          g_object_unref (buffer);

          return GIMP_OBJECT (gimp_get_clipboard_buffer (image->gimp));
        }
112 113 114
    }

  return NULL;
Elliot Lee's avatar
Elliot Lee committed
115 116
}

117
GimpObject *
118 119 120 121
gimp_edit_copy (GimpImage     *image,
                GimpDrawable  *drawable,
                GimpContext   *context,
                GError       **error)
Elliot Lee's avatar
Elliot Lee committed
122
{
123
  g_return_val_if_fail (GIMP_IS_IMAGE (image), NULL);
Michael Natterer's avatar
Michael Natterer committed
124
  g_return_val_if_fail (GIMP_IS_DRAWABLE (drawable), NULL);
125
  g_return_val_if_fail (gimp_item_is_attached (GIMP_ITEM (drawable)), NULL);
126
  g_return_val_if_fail (GIMP_IS_CONTEXT (context), NULL);
127
  g_return_val_if_fail (error == NULL || *error == NULL, NULL);
Elliot Lee's avatar
Elliot Lee committed
128

129 130 131 132
  if (GIMP_IS_LAYER (drawable) &&
      gimp_channel_is_empty (gimp_image_get_mask (image)))
    {
      GimpImage *clip_image;
133

134 135 136 137 138 139 140 141
      clip_image = gimp_image_new_from_drawable (image->gimp, drawable);
      gimp_container_remove (image->gimp->images, GIMP_OBJECT (clip_image));
      gimp_set_clipboard_image (image->gimp, clip_image);
      g_object_unref (clip_image);

      return GIMP_OBJECT (gimp_get_clipboard_image (image->gimp));
    }
  else
142
    {
143
      GimpBuffer *buffer;
144

145 146 147 148 149 150 151 152 153 154
      buffer = gimp_edit_extract (image, GIMP_PICKABLE (drawable),
                                  context, FALSE, error);

      if (buffer)
        {
          gimp_set_clipboard_buffer (image->gimp, buffer);
          g_object_unref (buffer);

          return GIMP_OBJECT (gimp_get_clipboard_buffer (image->gimp));
        }
155 156 157
    }

  return NULL;
Elliot Lee's avatar
Elliot Lee committed
158 159
}

160
GimpBuffer *
161 162 163
gimp_edit_copy_visible (GimpImage    *image,
                        GimpContext  *context,
                        GError      **error)
164
{
165
  GimpBuffer *buffer;
166

167
  g_return_val_if_fail (GIMP_IS_IMAGE (image), NULL);
168
  g_return_val_if_fail (GIMP_IS_CONTEXT (context), NULL);
169
  g_return_val_if_fail (error == NULL || *error == NULL, NULL);
170

171
  buffer = gimp_edit_extract (image, GIMP_PICKABLE (image),
172
                              context, FALSE, error);
173

174 175
  if (buffer)
    {
176
      gimp_set_clipboard_buffer (image->gimp, buffer);
177
      g_object_unref (buffer);
178

179
      return gimp_get_clipboard_buffer (image->gimp);
180
    }
181

182
  return NULL;
183 184
}

185 186 187 188 189 190 191 192 193 194
void
gimp_edit_get_paste_offset (GimpImage    *image,
                            GimpDrawable *drawable,
                            GimpObject   *paste,
                            gint          viewport_x,
                            gint          viewport_y,
                            gint          viewport_width,
                            gint          viewport_height,
                            gint         *offset_x,
                            gint         *offset_y)
Elliot Lee's avatar
Elliot Lee committed
195
{
196 197 198 199 200 201 202 203 204 205 206 207 208
  gint     image_width;
  gint     image_height;
  gint     width;
  gint     height;
  gboolean clamp_to_image = TRUE;

  g_return_if_fail (GIMP_IS_IMAGE (image));
  g_return_if_fail (drawable == NULL || GIMP_IS_DRAWABLE (drawable));
  g_return_if_fail (drawable == NULL ||
                    gimp_item_is_attached (GIMP_ITEM (drawable)));
  g_return_if_fail (GIMP_IS_VIEWABLE (paste));
  g_return_if_fail (offset_x != NULL);
  g_return_if_fail (offset_y != NULL);
Elliot Lee's avatar
Elliot Lee committed
209

210 211 212
  image_width  = gimp_image_get_width  (image);
  image_height = gimp_image_get_height (image);

213
  gimp_viewable_get_size (GIMP_VIEWABLE (paste), &width, &height);
214 215 216 217 218 219 220 221 222 223 224 225

  if (viewport_width  == image_width &&
      viewport_height == image_height)
    {
      /* if the whole image is visible, act as if there was no viewport */

      viewport_x      = 0;
      viewport_y      = 0;
      viewport_width  = 0;
      viewport_height = 0;
    }

226
  if (drawable)
Michael Natterer's avatar
Michael Natterer committed
227
    {
228 229 230
      /*  if pasting to a drawable  */

      gint     off_x, off_y;
231 232
      gint     target_x, target_y;
      gint     target_width, target_height;
233 234 235 236
      gint     paste_x, paste_y;
      gint     paste_width, paste_height;
      gboolean have_mask;

237 238
      have_mask = ! gimp_channel_is_empty (gimp_image_get_mask (image));

239
      gimp_item_get_offset (GIMP_ITEM (drawable), &off_x, &off_y);
240 241 242
      gimp_item_mask_intersect (GIMP_ITEM (drawable),
                                &target_x, &target_y,
                                &target_width, &target_height);
243

244 245
      if (! have_mask         &&    /* if we have no mask */
          viewport_width  > 0 &&    /* and we have a viewport */
246
          viewport_height > 0 &&
247 248
          (width  < target_width || /* and the paste is smaller than the target */
           height < target_height) &&
249 250

          /* and the viewport intersects with the target */
251 252
          gimp_rectangle_intersect (viewport_x, viewport_y,
                                    viewport_width, viewport_height,
253 254
                                    off_x, off_y, /* target_x,y are 0 */
                                    target_width, target_height,
255 256 257
                                    &paste_x, &paste_y,
                                    &paste_width, &paste_height))
        {
258 259
          /*  center on the viewport  */

260 261
          *offset_x = paste_x + (paste_width - width)  / 2;
          *offset_y = paste_y + (paste_height- height) / 2;
262 263 264
        }
      else
        {
265 266
          /*  otherwise center on the target  */

267 268
          *offset_x = off_x + target_x + (target_width  - width)  / 2;
          *offset_y = off_y + target_y + (target_height - height) / 2;
269 270 271

          /*  and keep it that way  */
          clamp_to_image = FALSE;
272 273
        }
    }
274 275 276 277
  else if (viewport_width  > 0 &&  /* if we have a viewport */
           viewport_height > 0 &&
           (width  < image_width || /* and the paste is       */
            height < image_height)) /* smaller than the image */
278
    {
279
      /*  center on the viewport  */
280

281 282
      *offset_x = viewport_x + (viewport_width  - width)  / 2;
      *offset_y = viewport_y + (viewport_height - height) / 2;
Michael Natterer's avatar
Michael Natterer committed
283 284 285
    }
  else
    {
286
      /*  otherwise center on the image  */
Elliot Lee's avatar
Elliot Lee committed
287

288 289
      *offset_x = (image_width  - width)  / 2;
      *offset_y = (image_height - height) / 2;
290

291 292 293
      /*  and keep it that way  */
      clamp_to_image = FALSE;
    }
294

295 296 297 298 299
  if (clamp_to_image)
    {
      /*  Ensure that the pasted layer is always within the image, if it
       *  fits and aligned at top left if it doesn't. (See bug #142944).
       */
300 301 302 303
      *offset_x = MIN (*offset_x, image_width  - width);
      *offset_y = MIN (*offset_y, image_height - height);
      *offset_x = MAX (*offset_x, 0);
      *offset_y = MAX (*offset_y, 0);
304
    }
305 306 307
}

GimpLayer *
308 309 310 311 312 313 314 315
gimp_edit_paste (GimpImage     *image,
                 GimpDrawable  *drawable,
                 GimpObject    *paste,
                 GimpPasteType  paste_type,
                 gint           viewport_x,
                 gint           viewport_y,
                 gint           viewport_width,
                 gint           viewport_height)
316
{
317 318 319
  GimpLayer *layer;
  gint       offset_x;
  gint       offset_y;
320 321 322 323 324

  g_return_val_if_fail (GIMP_IS_IMAGE (image), NULL);
  g_return_val_if_fail (drawable == NULL || GIMP_IS_DRAWABLE (drawable), NULL);
  g_return_val_if_fail (drawable == NULL ||
                        gimp_item_is_attached (GIMP_ITEM (drawable)), NULL);
325
  g_return_val_if_fail (GIMP_IS_IMAGE (paste) || GIMP_IS_BUFFER (paste), NULL);
326

327 328 329
  if (GIMP_IS_IMAGE (paste))
    {
      layer = gimp_image_get_layer_iter (GIMP_IMAGE (paste))->data;
330

331 332 333 334
      layer = GIMP_LAYER (gimp_item_convert (GIMP_ITEM (layer),
                                             image,
                                             G_TYPE_FROM_INSTANCE (layer)));
    }
335
  else
336 337
    {
      const Babl *format;
338

339 340 341 342 343 344 345 346 347 348 349
      /*  If drawable == NULL, user is pasting into an empty image  */
      if (drawable)
        format = gimp_drawable_get_format_with_alpha (drawable);
      else
        format = gimp_image_get_layer_format (image, TRUE);

      layer = gimp_layer_new_from_buffer (GIMP_BUFFER (paste), image,
                                          format,
                                          _("Pasted Layer"),
                                          GIMP_OPACITY_OPAQUE, GIMP_NORMAL_MODE);
    }
350 351 352 353 354 355 356 357 358 359 360

  if (! layer)
    return NULL;

  gimp_edit_get_paste_offset (image, drawable, GIMP_OBJECT (layer),
                              viewport_x,
                              viewport_y,
                              viewport_width,
                              viewport_height,
                              &offset_x,
                              &offset_y);
361

362
  gimp_item_set_offset (GIMP_ITEM (layer), offset_x, offset_y);
363

364 365 366 367 368 369 370 371 372 373 374
  /* change paste type to NEW_LAYER for cases where we can't attach
   * a floating selection
   */
  if (GIMP_IS_IMAGE (paste)                                 ||
      ! drawable                                            ||
      gimp_viewable_get_children (GIMP_VIEWABLE (drawable)) ||
      gimp_item_is_content_locked (GIMP_ITEM (drawable)))
    {
      paste_type = GIMP_PASTE_TYPE_NEW_LAYER;
    }

375
  gimp_image_undo_group_start (image, GIMP_UNDO_GROUP_EDIT_PASTE,
376
                               C_("undo-type", "Paste"));
377

378 379 380 381 382 383 384 385 386
  switch (paste_type)
    {
    case GIMP_PASTE_TYPE_FLOATING:
      /*  If there is a selection mask clear it--
       *  this might not always be desired, but in general,
       *  it seems like the correct behavior.
       */
      if (! gimp_channel_is_empty (gimp_image_get_mask (image)))
        gimp_channel_clear (gimp_image_get_mask (image), NULL, TRUE);
Elliot Lee's avatar
Elliot Lee committed
387

388 389 390 391 392 393 394 395 396 397 398 399
      /* fall thru */

    case GIMP_PASTE_TYPE_FLOATING_INTO:
      floating_sel_attach (layer, drawable);
      break;

    case GIMP_PASTE_TYPE_NEW_LAYER:
      /* always add on top of the drawable or the active layer, where
       * we would attach a floating selection
       */
      gimp_image_add_layer (image, layer,
                            GIMP_IS_LAYER (drawable) ?
400
                            GIMP_LAYER (gimp_item_get_parent (GIMP_ITEM (drawable))) :
401 402 403 404
                            NULL,
                            -1, TRUE);
      break;
    }
Michael Natterer's avatar
Michael Natterer committed
405

406
  gimp_image_undo_group_end (image);
Michael Natterer's avatar
Michael Natterer committed
407 408

  return layer;
Elliot Lee's avatar
Elliot Lee committed
409
}
410

411
const gchar *
412 413 414 415 416
gimp_edit_named_cut (GimpImage     *image,
                     const gchar   *name,
                     GimpDrawable  *drawable,
                     GimpContext   *context,
                     GError       **error)
417
{
418
  GimpBuffer *buffer;
419

420
  g_return_val_if_fail (GIMP_IS_IMAGE (image), NULL);
421 422 423 424
  g_return_val_if_fail (name != NULL, NULL);
  g_return_val_if_fail (GIMP_IS_DRAWABLE (drawable), NULL);
  g_return_val_if_fail (gimp_item_is_attached (GIMP_ITEM (drawable)), NULL);
  g_return_val_if_fail (GIMP_IS_CONTEXT (context), NULL);
425
  g_return_val_if_fail (error == NULL || *error == NULL, NULL);
426

427
  buffer = gimp_edit_extract (image, GIMP_PICKABLE (drawable),
428
                              context, TRUE, error);
429 430 431

  if (buffer)
    {
432
      gimp_object_set_name (GIMP_OBJECT (buffer), name);
433
      gimp_container_add (image->gimp->named_buffers, GIMP_OBJECT (buffer));
434
      g_object_unref (buffer);
435

436
      return gimp_object_get_name (buffer);
437 438 439 440 441 442
    }

  return NULL;
}

const gchar *
443 444 445 446 447
gimp_edit_named_copy (GimpImage     *image,
                      const gchar   *name,
                      GimpDrawable  *drawable,
                      GimpContext   *context,
                      GError       **error)
448
{
449
  GimpBuffer *buffer;
450

451
  g_return_val_if_fail (GIMP_IS_IMAGE (image), NULL);
452 453 454 455
  g_return_val_if_fail (name != NULL, NULL);
  g_return_val_if_fail (GIMP_IS_DRAWABLE (drawable), NULL);
  g_return_val_if_fail (gimp_item_is_attached (GIMP_ITEM (drawable)), NULL);
  g_return_val_if_fail (GIMP_IS_CONTEXT (context), NULL);
456
  g_return_val_if_fail (error == NULL || *error == NULL, NULL);
457

458
  buffer = gimp_edit_extract (image, GIMP_PICKABLE (drawable),
459
                              context, FALSE, error);
460 461 462

  if (buffer)
    {
463
      gimp_object_set_name (GIMP_OBJECT (buffer), name);
464
      gimp_container_add (image->gimp->named_buffers, GIMP_OBJECT (buffer));
465
      g_object_unref (buffer);
466

467
      return gimp_object_get_name (buffer);
468 469 470 471 472 473
    }

  return NULL;
}

const gchar *
474 475 476 477
gimp_edit_named_copy_visible (GimpImage    *image,
                              const gchar  *name,
                              GimpContext  *context,
                              GError      **error)
478
{
479
  GimpBuffer *buffer;
480

481
  g_return_val_if_fail (GIMP_IS_IMAGE (image), NULL);
482 483
  g_return_val_if_fail (name != NULL, NULL);
  g_return_val_if_fail (GIMP_IS_CONTEXT (context), NULL);
484
  g_return_val_if_fail (error == NULL || *error == NULL, NULL);
485

486
  buffer = gimp_edit_extract (image, GIMP_PICKABLE (image),
487
                              context, FALSE, error);
488 489 490

  if (buffer)
    {
491
      gimp_object_set_name (GIMP_OBJECT (buffer), name);
492
      gimp_container_add (image->gimp->named_buffers, GIMP_OBJECT (buffer));
493
      g_object_unref (buffer);
494

495
      return gimp_object_get_name (buffer);
496 497 498 499 500
    }

  return NULL;
}

501
void
502
gimp_edit_clear (GimpImage    *image,
503
                 GimpDrawable *drawable,
504
                 GimpContext  *context)
Elliot Lee's avatar
Elliot Lee committed
505
{
506
  GimpFillOptions *options;
507

508 509 510 511
  g_return_if_fail (GIMP_IS_IMAGE (image));
  g_return_if_fail (GIMP_IS_DRAWABLE (drawable));
  g_return_if_fail (gimp_item_is_attached (GIMP_ITEM (drawable)));
  g_return_if_fail (GIMP_IS_CONTEXT (context));
Elliot Lee's avatar
Elliot Lee committed
512

513
  options = gimp_fill_options_new (context->gimp, NULL, FALSE);
514

515
  if (gimp_drawable_has_alpha (drawable))
516 517
    gimp_fill_options_set_by_fill_type (options, context,
                                        GIMP_FILL_TRANSPARENT, NULL);
518
  else
519 520
    gimp_fill_options_set_by_fill_type (options, context,
                                        GIMP_FILL_BACKGROUND, NULL);
521

522
  gimp_edit_fill (image, drawable, options, C_("undo-type", "Clear"));
523 524

  g_object_unref (options);
Elliot Lee's avatar
Elliot Lee committed
525 526
}

527
void
528 529 530 531
gimp_edit_fill (GimpImage       *image,
                GimpDrawable    *drawable,
                GimpFillOptions *options,
                const gchar     *undo_desc)
Elliot Lee's avatar
Elliot Lee committed
532
{
533
  GeglBuffer  *buffer;
534
  gint         x, y, width, height;
Elliot Lee's avatar
Elliot Lee committed
535

536 537 538 539
  g_return_if_fail (GIMP_IS_IMAGE (image));
  g_return_if_fail (GIMP_IS_DRAWABLE (drawable));
  g_return_if_fail (gimp_item_is_attached (GIMP_ITEM (drawable)));
  g_return_if_fail (GIMP_IS_FILL_OPTIONS (options));
540

541
  if (! gimp_item_mask_intersect (GIMP_ITEM (drawable), &x, &y, &width, &height))
542
    return;  /*  nothing to do, but the fill succeeded  */
Elliot Lee's avatar
Elliot Lee committed
543

544 545 546
  buffer = gimp_fill_options_create_buffer (options, drawable,
                                            GEGL_RECTANGLE (0, 0,
                                                            width, height));
547

548 549 550
  if (! undo_desc)
    undo_desc = gimp_fill_options_get_undo_desc (options);

551
  gimp_drawable_apply_buffer (drawable, buffer,
552
                              GEGL_RECTANGLE (0, 0, width, height),
553
                              TRUE, undo_desc,
554 555
                              gimp_context_get_opacity (GIMP_CONTEXT (options)),
                              gimp_context_get_paint_mode (GIMP_CONTEXT (options)),
556
                              NULL, x, y);
557

558
  g_object_unref (buffer);
559 560

  gimp_drawable_update (drawable, x, y, width, height);
561 562
}

563 564 565 566 567 568 569 570 571 572 573
gboolean
gimp_edit_fade (GimpImage   *image,
                GimpContext *context)
{
  GimpDrawableUndo *undo;

  g_return_val_if_fail (GIMP_IS_IMAGE (image), FALSE);
  g_return_val_if_fail (GIMP_IS_CONTEXT (context), FALSE);

  undo = GIMP_DRAWABLE_UNDO (gimp_image_undo_get_fadeable (image));

574
  if (undo && undo->applied_buffer)
575
    {
576 577
      GimpDrawable *drawable;
      GeglBuffer   *buffer;
578 579 580 581

      drawable = GIMP_DRAWABLE (GIMP_ITEM_UNDO (undo)->item);

      g_object_ref (undo);
582
      buffer = g_object_ref (undo->applied_buffer);
583 584 585

      gimp_image_undo (image);

586
      gimp_drawable_apply_buffer (drawable, buffer,
587
                                  GEGL_RECTANGLE (0, 0,
588 589
                                                  gegl_buffer_get_width (undo->buffer),
                                                  gegl_buffer_get_height (undo->buffer)),
590
                                  TRUE,
591
                                  gimp_object_get_name (undo),
592 593
                                  gimp_context_get_opacity (context),
                                  gimp_context_get_paint_mode (context),
594
                                  NULL, undo->x, undo->y);
595

596
      g_object_unref (buffer);
597 598 599 600 601 602 603 604
      g_object_unref (undo);

      return TRUE;
    }

  return FALSE;
}

605 606 607

/*  private functions  */

608
static GimpBuffer *
609 610 611 612 613
gimp_edit_extract (GimpImage     *image,
                   GimpPickable  *pickable,
                   GimpContext   *context,
                   gboolean       cut_pixels,
                   GError       **error)
614
{
615 616 617
  GeglBuffer *buffer;
  gint        offset_x;
  gint        offset_y;
618 619

  if (cut_pixels)
620 621
    gimp_image_undo_group_start (image, GIMP_UNDO_GROUP_EDIT_CUT,
                                 C_("undo-type", "Cut"));
622

623
  /*  Cut/copy the mask portion from the image  */
624 625 626 627
  buffer = gimp_selection_extract (GIMP_SELECTION (gimp_image_get_mask (image)),
                                   pickable, context,
                                   cut_pixels, FALSE, FALSE,
                                   &offset_x, &offset_y, error);
628 629

  if (cut_pixels)
630
    gimp_image_undo_group_end (image);
631

632
  if (buffer)
633
    {
634 635
      GimpBuffer *gimp_buffer = gimp_buffer_new (buffer, _("Global Buffer"),
                                                 offset_x, offset_y, FALSE);
636
      g_object_unref (buffer);
637

638
      if (GIMP_IS_COLOR_MANAGED (pickable))
639
        {
640
          GimpColorProfile *profile =
641
            gimp_color_managed_get_color_profile (GIMP_COLOR_MANAGED (pickable));
642

643 644
          if (profile)
            gimp_buffer_set_color_profile (gimp_buffer, profile);
645 646
        }

647
      return gimp_buffer;
648 649 650
    }

  return NULL;
651
}