/* PIKA - Photo and Image Kooker Application
 * a rebranding of The GNU Image Manipulation Program (created with heckimp)
 * A derived work which may be trivial. However, any changes may be (C)2023 by Aldercone Studio
 *
 * Original copyright, applying to most contents (license remains unchanged): 
 * Copyright (C) 1995 Spencer Kimball and Peter Mattis
 *
 * 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 <cairo.h>
#include <gdk-pixbuf/gdk-pixbuf.h>
#include <gegl.h>

#include "libpikacolor/pikacolor.h"

#include "core-types.h"

#include "gegl/pika-babl.h"
#include "gegl/pika-gegl-apply-operation.h"
#include "gegl/pika-gegl-loops.h"

#include "pika.h"
#include "pikacontext.h"
#include "pikadrawable-edit.h"
#include "pikadrawable-private.h"
#include "pikaerror.h"
#include "pikaimage.h"
#include "pikaimage-new.h"
#include "pikaimage-undo.h"
#include "pikaimage-undo-push.h"
#include "pikalayer.h"
#include "pikalayer-new.h"
#include "pikalayermask.h"
#include "pikalayer-floating-selection.h"
#include "pikapickable.h"
#include "pikaselection.h"

#include "pika-intl.h"


static gboolean   pika_selection_is_attached   (PikaItem            *item);
static PikaItemTree * pika_selection_get_tree  (PikaItem            *item);
static void       pika_selection_translate     (PikaItem            *item,
                                                gdouble              offset_x,
                                                gdouble              offset_y,
                                                gboolean             push_undo);
static void       pika_selection_scale         (PikaItem            *item,
                                                gint                 new_width,
                                                gint                 new_height,
                                                gint                 new_offset_x,
                                                gint                 new_offset_y,
                                                PikaInterpolationType interp_type,
                                                PikaProgress        *progress);
static void       pika_selection_resize        (PikaItem            *item,
                                                PikaContext         *context,
                                                PikaFillType         fill_type,
                                                gint                 new_width,
                                                gint                 new_height,
                                                gint                 offset_x,
                                                gint                 offset_y);
static void       pika_selection_flip          (PikaItem            *item,
                                                PikaContext         *context,
                                                PikaOrientationType  flip_type,
                                                gdouble              axis,
                                                gboolean             clip_result);
static void       pika_selection_rotate        (PikaItem            *item,
                                                PikaContext         *context,
                                                PikaRotationType     rotation_type,
                                                gdouble              center_x,
                                                gdouble              center_y,
                                                gboolean             clip_result);
static gboolean   pika_selection_fill          (PikaItem            *item,
                                                PikaDrawable        *drawable,
                                                PikaFillOptions     *fill_options,
                                                gboolean             push_undo,
                                                PikaProgress        *progress,
                                                GError             **error);
static gboolean   pika_selection_stroke        (PikaItem            *item,
                                                PikaDrawable        *drawable,
                                                PikaStrokeOptions   *stroke_options,
                                                gboolean             push_undo,
                                                PikaProgress        *progress,
                                                GError             **error);
static void       pika_selection_convert_type  (PikaDrawable        *drawable,
                                                PikaImage           *dest_image,
                                                const Babl          *new_format,
                                                PikaColorProfile    *src_profile,
                                                PikaColorProfile    *dest_profile,
                                                GeglDitherMethod     layer_dither_type,
                                                GeglDitherMethod     mask_dither_type,
                                                gboolean             push_undo,
                                                PikaProgress        *progress);
static void pika_selection_invalidate_boundary (PikaDrawable        *drawable);

static gboolean   pika_selection_boundary      (PikaChannel         *channel,
                                                const PikaBoundSeg **segs_in,
                                                const PikaBoundSeg **segs_out,
                                                gint                *num_segs_in,
                                                gint                *num_segs_out,
                                                gint                 x1,
                                                gint                 y1,
                                                gint                 x2,
                                                gint                 y2);
static gboolean   pika_selection_is_empty      (PikaChannel         *channel);
static void       pika_selection_feather       (PikaChannel         *channel,
                                                gdouble              radius_x,
                                                gdouble              radius_y,
                                                gboolean             edge_lock,
                                                gboolean             push_undo);
static void       pika_selection_sharpen       (PikaChannel         *channel,
                                                gboolean             push_undo);
static void       pika_selection_clear         (PikaChannel         *channel,
                                                const gchar         *undo_desc,
                                                gboolean             push_undo);
static void       pika_selection_all           (PikaChannel         *channel,
                                                gboolean             push_undo);
static void       pika_selection_invert        (PikaChannel         *channel,
                                                gboolean             push_undo);
static void       pika_selection_border        (PikaChannel         *channel,
                                                gint                 radius_x,
                                                gint                 radius_y,
                                                PikaChannelBorderStyle style,
                                                gboolean             edge_lock,
                                                gboolean             push_undo);
static void       pika_selection_grow          (PikaChannel         *channel,
                                                gint                 radius_x,
                                                gint                 radius_y,
                                                gboolean             push_undo);
static void       pika_selection_shrink        (PikaChannel         *channel,
                                                gint                 radius_x,
                                                gint                 radius_y,
                                                gboolean             edge_lock,
                                                gboolean             push_undo);
static void       pika_selection_flood         (PikaChannel         *channel,
                                                gboolean             push_undo);


G_DEFINE_TYPE (PikaSelection, pika_selection, PIKA_TYPE_CHANNEL)

#define parent_class pika_selection_parent_class


static void
pika_selection_class_init (PikaSelectionClass *klass)
{
  PikaViewableClass *viewable_class = PIKA_VIEWABLE_CLASS (klass);
  PikaItemClass     *item_class     = PIKA_ITEM_CLASS (klass);
  PikaDrawableClass *drawable_class = PIKA_DRAWABLE_CLASS (klass);
  PikaChannelClass  *channel_class  = PIKA_CHANNEL_CLASS (klass);

  viewable_class->default_icon_name   = "pika-selection";

  item_class->is_attached             = pika_selection_is_attached;
  item_class->get_tree                = pika_selection_get_tree;
  item_class->translate               = pika_selection_translate;
  item_class->scale                   = pika_selection_scale;
  item_class->resize                  = pika_selection_resize;
  item_class->flip                    = pika_selection_flip;
  item_class->rotate                  = pika_selection_rotate;
  item_class->fill                    = pika_selection_fill;
  item_class->stroke                  = pika_selection_stroke;
  item_class->default_name            = _("Selection Mask");
  item_class->translate_desc          = C_("undo-type", "Move Selection");
  item_class->fill_desc               = C_("undo-type", "Fill Selection");
  item_class->stroke_desc             = C_("undo-type", "Stroke Selection");

  drawable_class->convert_type        = pika_selection_convert_type;
  drawable_class->invalidate_boundary = pika_selection_invalidate_boundary;

  channel_class->boundary             = pika_selection_boundary;
  channel_class->is_empty             = pika_selection_is_empty;
  channel_class->feather              = pika_selection_feather;
  channel_class->sharpen              = pika_selection_sharpen;
  channel_class->clear                = pika_selection_clear;
  channel_class->all                  = pika_selection_all;
  channel_class->invert               = pika_selection_invert;
  channel_class->border               = pika_selection_border;
  channel_class->grow                 = pika_selection_grow;
  channel_class->shrink               = pika_selection_shrink;
  channel_class->flood                = pika_selection_flood;

  channel_class->feather_desc         = C_("undo-type", "Feather Selection");
  channel_class->sharpen_desc         = C_("undo-type", "Sharpen Selection");
  channel_class->clear_desc           = C_("undo-type", "Select None");
  channel_class->all_desc             = C_("undo-type", "Select All");
  channel_class->invert_desc          = C_("undo-type", "Invert Selection");
  channel_class->border_desc          = C_("undo-type", "Border Selection");
  channel_class->grow_desc            = C_("undo-type", "Grow Selection");
  channel_class->shrink_desc          = C_("undo-type", "Shrink Selection");
  channel_class->flood_desc           = C_("undo-type", "Remove Holes");
}

static void
pika_selection_init (PikaSelection *selection)
{
}

static gboolean
pika_selection_is_attached (PikaItem *item)
{
  return (PIKA_IS_IMAGE (pika_item_get_image (item)) &&
          pika_image_get_mask (pika_item_get_image (item)) ==
          PIKA_CHANNEL (item));
}

static PikaItemTree *
pika_selection_get_tree (PikaItem *item)
{
  return NULL;
}

static void
pika_selection_translate (PikaItem *item,
                          gdouble   offset_x,
                          gdouble   offset_y,
                          gboolean  push_undo)
{
  PIKA_ITEM_CLASS (parent_class)->translate (item, offset_x, offset_y,
                                             push_undo);
}

static void
pika_selection_scale (PikaItem              *item,
                      gint                   new_width,
                      gint                   new_height,
                      gint                   new_offset_x,
                      gint                   new_offset_y,
                      PikaInterpolationType  interp_type,
                      PikaProgress          *progress)
{
  PIKA_ITEM_CLASS (parent_class)->scale (item, new_width, new_height,
                                         new_offset_x, new_offset_y,
                                         interp_type, progress);

  pika_item_set_offset (item, 0, 0);
}

static void
pika_selection_resize (PikaItem     *item,
                       PikaContext  *context,
                       PikaFillType  fill_type,
                       gint          new_width,
                       gint          new_height,
                       gint          offset_x,
                       gint          offset_y)
{
  PIKA_ITEM_CLASS (parent_class)->resize (item, context, PIKA_FILL_TRANSPARENT,
                                          new_width, new_height,
                                          offset_x, offset_y);

  pika_item_set_offset (item, 0, 0);
}

static void
pika_selection_flip (PikaItem            *item,
                     PikaContext         *context,
                     PikaOrientationType  flip_type,
                     gdouble              axis,
                     gboolean             clip_result)
{
  PIKA_ITEM_CLASS (parent_class)->flip (item, context, flip_type, axis, TRUE);
}

static void
pika_selection_rotate (PikaItem         *item,
                       PikaContext      *context,
                       PikaRotationType  rotation_type,
                       gdouble           center_x,
                       gdouble           center_y,
                       gboolean          clip_result)
{
  PIKA_ITEM_CLASS (parent_class)->rotate (item, context, rotation_type,
                                          center_x, center_y,
                                          clip_result);
}

static gboolean
pika_selection_fill (PikaItem         *item,
                     PikaDrawable     *drawable,
                     PikaFillOptions  *fill_options,
                     gboolean          push_undo,
                     PikaProgress     *progress,
                     GError          **error)
{
  PikaSelection      *selection = PIKA_SELECTION (item);
  const PikaBoundSeg *dummy_in;
  const PikaBoundSeg *dummy_out;
  gint                num_dummy_in;
  gint                num_dummy_out;
  gboolean            retval;

  if (! pika_channel_boundary (PIKA_CHANNEL (selection),
                               &dummy_in, &dummy_out,
                               &num_dummy_in, &num_dummy_out,
                               0, 0, 0, 0))
    {
      g_set_error_literal (error, PIKA_ERROR, PIKA_FAILED,
                           _("There is no selection to fill."));
      return FALSE;
    }

  pika_selection_suspend (selection);

  retval = PIKA_ITEM_CLASS (parent_class)->fill (item, drawable,
                                                 fill_options,
                                                 push_undo, progress, error);

  pika_selection_resume (selection);

  return retval;
}

static gboolean
pika_selection_stroke (PikaItem           *item,
                       PikaDrawable       *drawable,
                       PikaStrokeOptions  *stroke_options,
                       gboolean            push_undo,
                       PikaProgress       *progress,
                       GError            **error)
{
  PikaSelection      *selection = PIKA_SELECTION (item);
  const PikaBoundSeg *dummy_in;
  const PikaBoundSeg *dummy_out;
  gint                num_dummy_in;
  gint                num_dummy_out;
  gboolean            retval;

  if (! pika_channel_boundary (PIKA_CHANNEL (selection),
                               &dummy_in, &dummy_out,
                               &num_dummy_in, &num_dummy_out,
                               0, 0, 0, 0))
    {
      g_set_error_literal (error, PIKA_ERROR, PIKA_FAILED,
                           _("There is no selection to stroke."));
      return FALSE;
    }

  pika_selection_suspend (selection);

  retval = PIKA_ITEM_CLASS (parent_class)->stroke (item, drawable,
                                                   stroke_options,
                                                   push_undo, progress, error);

  pika_selection_resume (selection);

  return retval;
}

static void
pika_selection_convert_type (PikaDrawable      *drawable,
                             PikaImage         *dest_image,
                             const Babl        *new_format,
                             PikaColorProfile  *src_profile,
                             PikaColorProfile  *dest_profile,
                             GeglDitherMethod   layer_dither_type,
                             GeglDitherMethod   mask_dither_type,
                             gboolean           push_undo,
                             PikaProgress      *progress)
{
  new_format =
    pika_babl_mask_format (pika_babl_format_get_precision (new_format));

  PIKA_DRAWABLE_CLASS (parent_class)->convert_type (drawable, dest_image,
                                                    new_format,
                                                    src_profile,
                                                    dest_profile,
                                                    layer_dither_type,
                                                    mask_dither_type,
                                                    push_undo,
                                                    progress);
}

static void
pika_selection_invalidate_boundary (PikaDrawable *drawable)
{
  PikaImage *image = pika_item_get_image (PIKA_ITEM (drawable));
  GList     *layers;

  /*  Turn the current selection off  */
  pika_image_selection_invalidate (image);

  PIKA_DRAWABLE_CLASS (parent_class)->invalidate_boundary (drawable);

  /*  If there is a floating selection, update it's area...
   *  we need to do this since this selection mask can act as an additional
   *  mask in the composition of the floating selection
   */
  layers = pika_image_get_selected_layers (image);

  if (g_list_length (layers) == 1 && pika_layer_is_floating_sel (layers->data))
    {
      pika_drawable_update (PIKA_DRAWABLE (layers->data), 0, 0, -1, -1);
    }

#if 0
  /*  invalidate the preview  */
  drawable->private->preview_valid = FALSE;
#endif
}

static gboolean
pika_selection_boundary (PikaChannel         *channel,
                         const PikaBoundSeg **segs_in,
                         const PikaBoundSeg **segs_out,
                         gint                *num_segs_in,
                         gint                *num_segs_out,
                         gint                 unused1,
                         gint                 unused2,
                         gint                 unused3,
                         gint                 unused4)
{
  PikaImage    *image = pika_item_get_image (PIKA_ITEM (channel));
  PikaLayer    *floating_selection;
  GList        *drawables;
  GList        *layers;
  gboolean      channel_selected;

  drawables = pika_image_get_selected_drawables (image);
  channel_selected = (drawables && PIKA_IS_CHANNEL (drawables->data));
  g_list_free (drawables);

  if ((floating_selection = pika_image_get_floating_selection (image)))
    {
      /*  If there is a floating selection, then
       *  we need to do some slightly different boundaries.
       *  Instead of inside and outside boundaries being defined
       *  by the extents of the layer, the inside boundary (the one
       *  that actually marches and is black/white) is the boundary of
       *  the floating selection.  The outside boundary (doesn't move,
       *  is black/gray) is defined as the normal selection mask
       */

      /*  Find the selection mask boundary  */
      PIKA_CHANNEL_CLASS (parent_class)->boundary (channel,
                                                   segs_in, segs_out,
                                                   num_segs_in, num_segs_out,
                                                   0, 0, 0, 0);

      /*  Find the floating selection boundary  */
      *segs_in = floating_sel_boundary (floating_selection, num_segs_in);

      return TRUE;
    }
  else if (channel_selected)
    {
      /*  Otherwise, return the boundary...if a channels are selected  */

      return PIKA_CHANNEL_CLASS (parent_class)->boundary (channel,
                                                          segs_in, segs_out,
                                                          num_segs_in,
                                                          num_segs_out,
                                                          0, 0,
                                                          pika_image_get_width  (image),
                                                          pika_image_get_height (image));
    }
  else if ((layers = pika_image_get_selected_layers (image)))
    {
      /*  If layers are selected, we return multiple boundaries based
       *  on the extents
       */
      GList *iter;
      gint    x1, y1;
      gint    x2       = G_MININT;
      gint    y2       = G_MININT;
      gint    offset_x = G_MAXINT;
      gint    offset_y = G_MAXINT;

      for (iter = layers; iter; iter = iter->next)
        {
          gint item_off_x, item_off_y;
          gint item_x2, item_y2;

          pika_item_get_offset (iter->data, &item_off_x, &item_off_y);
          offset_x = MIN (offset_x, item_off_x);
          offset_y = MIN (offset_y, item_off_y);

          item_x2 = item_off_x + pika_item_get_width (PIKA_ITEM (iter->data));
          item_y2 = item_off_y + pika_item_get_height (PIKA_ITEM (iter->data));
          x2 = MAX (x2, item_x2);
          y2 = MAX (y2, item_y2);
        }

      x1 = CLAMP (offset_x, 0, pika_image_get_width  (image));
      y1 = CLAMP (offset_y, 0, pika_image_get_height (image));
      x2 = CLAMP (x2, 0, pika_image_get_width (image));
      y2 = CLAMP (y2, 0, pika_image_get_height (image));

      return PIKA_CHANNEL_CLASS (parent_class)->boundary (channel,
                                                          segs_in, segs_out,
                                                          num_segs_in,
                                                          num_segs_out,
                                                          x1, y1, x2, y2);
    }

  *segs_in      = NULL;
  *segs_out     = NULL;
  *num_segs_in  = 0;
  *num_segs_out = 0;

  return FALSE;
}

static gboolean
pika_selection_is_empty (PikaChannel *channel)
{
  PikaSelection *selection = PIKA_SELECTION (channel);

  /*  in order to allow stroking of selections, we need to pretend here
   *  that the selection mask is empty so that it doesn't mask the paint
   *  during the stroke operation.
   */
  if (selection->suspend_count > 0)
    return TRUE;

  return PIKA_CHANNEL_CLASS (parent_class)->is_empty (channel);
}

static void
pika_selection_feather (PikaChannel *channel,
                        gdouble      radius_x,
                        gdouble      radius_y,
                        gboolean     edge_lock,
                        gboolean     push_undo)
{
  PIKA_CHANNEL_CLASS (parent_class)->feather (channel, radius_x, radius_y,
                                              edge_lock, push_undo);
}

static void
pika_selection_sharpen (PikaChannel *channel,
                        gboolean     push_undo)
{
  PIKA_CHANNEL_CLASS (parent_class)->sharpen (channel, push_undo);
}

static void
pika_selection_clear (PikaChannel *channel,
                      const gchar *undo_desc,
                      gboolean     push_undo)
{
  PIKA_CHANNEL_CLASS (parent_class)->clear (channel, undo_desc, push_undo);
}

static void
pika_selection_all (PikaChannel *channel,
                    gboolean     push_undo)
{
  PIKA_CHANNEL_CLASS (parent_class)->all (channel, push_undo);
}

static void
pika_selection_invert (PikaChannel *channel,
                       gboolean     push_undo)
{
  PIKA_CHANNEL_CLASS (parent_class)->invert (channel, push_undo);
}

static void
pika_selection_border (PikaChannel            *channel,
                       gint                    radius_x,
                       gint                    radius_y,
                       PikaChannelBorderStyle  style,
                       gboolean                edge_lock,
                       gboolean                push_undo)
{
  PIKA_CHANNEL_CLASS (parent_class)->border (channel,
                                             radius_x, radius_y,
                                             style, edge_lock,
                                             push_undo);
}

static void
pika_selection_grow (PikaChannel *channel,
                     gint         radius_x,
                     gint         radius_y,
                     gboolean     push_undo)
{
  PIKA_CHANNEL_CLASS (parent_class)->grow (channel,
                                           radius_x, radius_y,
                                           push_undo);
}

static void
pika_selection_shrink (PikaChannel *channel,
                       gint         radius_x,
                       gint         radius_y,
                       gboolean     edge_lock,
                       gboolean     push_undo)
{
  PIKA_CHANNEL_CLASS (parent_class)->shrink (channel,
                                             radius_x, radius_y, edge_lock,
                                             push_undo);
}

static void
pika_selection_flood (PikaChannel *channel,
                      gboolean     push_undo)
{
  PIKA_CHANNEL_CLASS (parent_class)->flood (channel, push_undo);
}


/*  public functions  */

PikaChannel *
pika_selection_new (PikaImage *image,
                    gint       width,
                    gint       height)
{
  PikaRGB      black = { 0.0, 0.0, 0.0, 0.5 };
  PikaChannel *channel;

  g_return_val_if_fail (PIKA_IS_IMAGE (image), NULL);
  g_return_val_if_fail (width > 0 && height > 0, NULL);

  channel = PIKA_CHANNEL (pika_drawable_new (PIKA_TYPE_SELECTION,
                                             image, NULL,
                                             0, 0, width, height,
                                             pika_image_get_mask_format (image)));

  pika_channel_set_color (channel, &black, FALSE);
  pika_channel_set_show_masked (channel, TRUE);

  channel->x2 = width;
  channel->y2 = height;

  return channel;
}

gint
pika_selection_suspend (PikaSelection *selection)
{
  g_return_val_if_fail (PIKA_IS_SELECTION (selection), 0);

  selection->suspend_count++;

  return selection->suspend_count;
}

gint
pika_selection_resume (PikaSelection *selection)
{
  g_return_val_if_fail (PIKA_IS_SELECTION (selection), 0);
  g_return_val_if_fail (selection->suspend_count > 0, 0);

  selection->suspend_count--;

  return selection->suspend_count;
}

GeglBuffer *
pika_selection_extract (PikaSelection *selection,
                        GList         *pickables,
                        PikaContext   *context,
                        gboolean       cut_image,
                        gboolean       keep_indexed,
                        gboolean       add_alpha,
                        gint          *offset_x,
                        gint          *offset_y,
                        GError       **error)
{
  PikaImage    *image      = NULL;
  PikaImage    *temp_image = NULL;
  PikaPickable *pickable   = NULL;
  GeglBuffer   *src_buffer;
  GeglBuffer   *dest_buffer;
  GList        *iter;
  const Babl   *src_format;
  const Babl   *dest_format;
  gint          x1, y1, x2, y2;
  gboolean      non_empty;
  gint          off_x, off_y;

  g_return_val_if_fail (PIKA_IS_SELECTION (selection), NULL);
  g_return_val_if_fail (PIKA_IS_CONTEXT (context), NULL);
  g_return_val_if_fail (error == NULL || *error == NULL, NULL);
  g_return_val_if_fail (pickables != NULL, NULL);

  for (iter = pickables; iter; iter = iter->next)
    {
      g_return_val_if_fail (PIKA_IS_PICKABLE (iter->data), NULL);

      if (PIKA_IS_ITEM (iter->data))
        g_return_val_if_fail (pika_item_is_attached (iter->data), NULL);

      if (! image)
        image = pika_pickable_get_image (iter->data);
      else
        g_return_val_if_fail (image == pika_pickable_get_image (iter->data), NULL);
    }

  if (g_list_length (pickables) == 1)
    {
      pickable = pickables->data;
    }
  else
    {
      for (iter = pickables; iter; iter = iter->next)
        g_return_val_if_fail (PIKA_IS_DRAWABLE (iter->data), NULL);

      temp_image = pika_image_new_from_drawables (image->pika, pickables, TRUE, FALSE);
      selection  = PIKA_SELECTION (pika_image_get_mask (temp_image));

      pickable   = PIKA_PICKABLE (temp_image);

      /* Don't cut from the temporary image. */
      cut_image = FALSE;
    }

  /*  If there are no bounds, then just extract the entire image
   *  This may not be the correct behavior, but after getting rid
   *  of floating selections, it's still tempting to "cut" or "copy"
   *  a small layer and expect it to work, even though there is no
   *  actual selection mask
   */
  if (PIKA_IS_DRAWABLE (pickable))
    {
      non_empty = pika_item_mask_bounds (PIKA_ITEM (pickable),
                                         &x1, &y1, &x2, &y2);

      pika_item_get_offset (PIKA_ITEM (pickable), &off_x, &off_y);
    }
  else
    {
      non_empty = pika_item_bounds (PIKA_ITEM (selection),
                                    &x1, &y1, &x2, &y2);
      x2 += x1;
      y2 += y1;

      off_x = 0;
      off_y = 0;

      /* can't cut from non-drawables, fall back to copy */
      cut_image = FALSE;
    }

  if (non_empty && ((x1 == x2) || (y1 == y2)))
    {
      g_set_error_literal (error, PIKA_ERROR, PIKA_FAILED,
                           _("Unable to cut or copy because the "
                             "selected region is empty."));

      if (temp_image)
        g_object_unref (temp_image);

      return NULL;
    }

  /*  If there is a selection, we must add alpha because the selection
   *  could have any shape.
   */
  if (non_empty)
    add_alpha = TRUE;

  src_format = pika_pickable_get_format (pickable);

  /*  How many bytes in the temp buffer?  */
  if (babl_format_is_palette (src_format) && ! keep_indexed)
    {
      dest_format = pika_image_get_format (image, PIKA_RGB,
                                           pika_image_get_precision (image),
                                           add_alpha ||
                                           babl_format_has_alpha (src_format),
                                           babl_format_get_space (src_format));
    }
  else
    {
      if (add_alpha)
        dest_format = pika_pickable_get_format_with_alpha (pickable);
      else
        dest_format = src_format;
    }

  pika_pickable_flush (pickable);

  src_buffer = pika_pickable_get_buffer (pickable);

  /*  Allocate the temp buffer  */
  dest_buffer = gegl_buffer_new (GEGL_RECTANGLE (0, 0, x2 - x1, y2 - y1),
                                 dest_format);

  /*  First, copy the pixels, possibly doing INDEXED->RGB and adding alpha  */
  pika_gegl_buffer_copy (src_buffer,  GEGL_RECTANGLE (x1, y1, x2 - x1, y2 - y1),
                         GEGL_ABYSS_NONE,
                         dest_buffer, GEGL_RECTANGLE (0, 0, 0, 0));

  if (non_empty)
    {
      /*  If there is a selection, mask the dest_buffer with it  */

      GeglBuffer *mask_buffer;

      mask_buffer = pika_drawable_get_buffer (PIKA_DRAWABLE (selection));

      pika_gegl_apply_opacity (dest_buffer, NULL, NULL, dest_buffer,
                               mask_buffer,
                               - (off_x + x1),
                               - (off_y + y1),
                               1.0);

      if (cut_image)
        {
          pika_drawable_edit_clear (PIKA_DRAWABLE (pickable), context);
        }
    }
  else if (cut_image)
    {
      /*  If we're cutting without selection, remove either the layer
       *  (or floating selection), the layer mask, or the channel
       */
      if (PIKA_IS_LAYER (pickable))
        {
          pika_image_remove_layer (image, PIKA_LAYER (pickable),
                                   TRUE, NULL);
        }
      else if (PIKA_IS_LAYER_MASK (pickable))
        {
          pika_layer_apply_mask (pika_layer_mask_get_layer (PIKA_LAYER_MASK (pickable)),
                                 PIKA_MASK_DISCARD, TRUE);
        }
      else if (PIKA_IS_CHANNEL (pickable))
        {
          pika_image_remove_channel (image, PIKA_CHANNEL (pickable),
                                     TRUE, NULL);
        }
    }

  *offset_x = x1 + off_x;
  *offset_y = y1 + off_y;

  if (temp_image)
    g_object_unref (temp_image);

  return dest_buffer;
}

PikaLayer *
pika_selection_float (PikaSelection *selection,
                      GList         *drawables,
                      PikaContext   *context,
                      gboolean       cut_image,
                      gint           off_x,
                      gint           off_y,
                      GError       **error)
{
  PikaImage        *image;
  PikaLayer        *layer;
  GeglBuffer       *buffer;
  PikaColorProfile *profile;
  PikaImage        *temp_image = NULL;
  const Babl       *format     = NULL;
  GList            *iter;
  gint              x1, y1;
  gint              x2, y2;

  g_return_val_if_fail (PIKA_IS_SELECTION (selection), NULL);
  g_return_val_if_fail (PIKA_IS_CONTEXT (context), NULL);
  g_return_val_if_fail (error == NULL || *error == NULL, NULL);

  for (iter = drawables; iter; iter = iter->next)
    {
      g_return_val_if_fail (PIKA_IS_DRAWABLE (iter->data), NULL);
      g_return_val_if_fail (pika_item_is_attached (iter->data), NULL);

      if (! format)
        format = pika_drawable_get_format_with_alpha (iter->data);
      else
        g_return_val_if_fail (format == pika_drawable_get_format_with_alpha (iter->data),
                              NULL);
    }

  image = pika_item_get_image (PIKA_ITEM (selection));

  /*  Make sure there is a region to float...  */
  for (iter = drawables; iter; iter = iter->next)
    {
      if (pika_item_mask_bounds (iter->data, &x1, &y1, &x2, &y2) &&
          x1 != x2 && y1 != y2)
        break;
    }
  if (iter == NULL)
    {
      g_set_error_literal (error, PIKA_ERROR, PIKA_FAILED,
                           _("Cannot float selection because the selected "
                             "region is empty."));
      return NULL;
    }

  /*  Start an undo group  */
  pika_image_undo_group_start (image, PIKA_UNDO_GROUP_FS_FLOAT,
                               C_("undo-type", "Float Selection"));

  /*  Cut or copy the selected region  */
  buffer = pika_selection_extract (selection, drawables, context,
                                   cut_image, FALSE, TRUE,
                                   &x1, &y1, NULL);

  profile = pika_color_managed_get_color_profile (PIKA_COLOR_MANAGED (drawables->data));

  /*  Clear the selection  */
  pika_channel_clear (PIKA_CHANNEL (selection), NULL, TRUE);

  /* Create a new layer from the buffer, using the drawables' type
   *  because it may be different from the image's type if we cut from
   *  a channel or layer mask
   */
  layer = pika_layer_new_from_gegl_buffer (buffer, image, format,
                                           _("Floated Layer"),
                                           PIKA_OPACITY_OPAQUE,
                                           pika_image_get_default_new_layer_mode (image),
                                           profile);

  /*  Set the offsets  */
  pika_item_set_offset (PIKA_ITEM (layer), x1 + off_x, y1 + off_y);

  /*  Free the temp buffer  */
  g_object_unref (buffer);

  /*  Add the floating layer to the image  */
  floating_sel_attach (layer, drawables->data);

  /*  End an undo group  */
  pika_image_undo_group_end (image);

  /*  invalidate the image's boundary variables  */
  PIKA_CHANNEL (selection)->boundary_known = FALSE;

  if (temp_image)
    g_object_unref (temp_image);

  return layer;
}
