/* LIBPIKA - The PIKA Library
 * Copyright (C) 1995-2003 Peter Mattis and Spencer Kimball
 *
 * pikaselection_pdb.c
 *
 * This library is free software: you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 3 of the License, or (at your option) any later version.
 *
 * This library 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library.  If not, see
 * <https://www.gnu.org/licenses/>.
 */

/* NOTE: This file is auto-generated by pdbgen.pl */

#include "config.h"

#include "stamp-pdbgen.h"

#include "pika.h"


/**
 * SECTION: pikaselection
 * @title: pikaselection
 * @short_description: Functions for manipulating selections.
 *
 * Functions for manipulating selections.
 **/


/**
 * pika_selection_bounds:
 * @image: The image.
 * @non_empty: (out): TRUE if there is a selection.
 * @x1: (out): x coordinate of upper left corner of selection bounds.
 * @y1: (out): y coordinate of upper left corner of selection bounds.
 * @x2: (out): x coordinate of lower right corner of selection bounds.
 * @y2: (out): y coordinate of lower right corner of selection bounds.
 *
 * Find the bounding box of the current selection.
 *
 * This procedure returns whether there is a selection for the
 * specified image. If there is one, the upper left and lower right
 * corners of the bounding box are returned. These coordinates are
 * relative to the image. Please note that the pixel specified by the
 * lower right coordinate of the bounding box is not part of the
 * selection. The selection ends at the upper left corner of this
 * pixel. This means the width of the selection can be calculated as
 * (x2 - x1), its height as (y2 - y1).
 *
 * Returns: TRUE on success.
 **/
gboolean
pika_selection_bounds (PikaImage *image,
                       gboolean  *non_empty,
                       gint      *x1,
                       gint      *y1,
                       gint      *x2,
                       gint      *y2)
{
  PikaValueArray *args;
  PikaValueArray *return_vals;
  gboolean success = TRUE;

  args = pika_value_array_new_from_types (NULL,
                                          PIKA_TYPE_IMAGE, image,
                                          G_TYPE_NONE);

  return_vals = _pika_pdb_run_procedure_array (pika_get_pdb (),
                                               "pika-selection-bounds",
                                               args);
  pika_value_array_unref (args);

  *non_empty = FALSE;
  *x1 = 0;
  *y1 = 0;
  *x2 = 0;
  *y2 = 0;

  success = PIKA_VALUES_GET_ENUM (return_vals, 0) == PIKA_PDB_SUCCESS;

  if (success)
    {
      *non_empty = PIKA_VALUES_GET_BOOLEAN (return_vals, 1);
      *x1 = PIKA_VALUES_GET_INT (return_vals, 2);
      *y1 = PIKA_VALUES_GET_INT (return_vals, 3);
      *x2 = PIKA_VALUES_GET_INT (return_vals, 4);
      *y2 = PIKA_VALUES_GET_INT (return_vals, 5);
    }

  pika_value_array_unref (return_vals);

  return success;
}

/**
 * pika_selection_value:
 * @image: The image.
 * @x: x coordinate of value.
 * @y: y coordinate of value.
 *
 * Find the value of the selection at the specified coordinates.
 *
 * This procedure returns the value of the selection at the specified
 * coordinates. If the coordinates lie out of bounds, 0 is returned.
 *
 * Returns: Value of the selection.
 **/
gint
pika_selection_value (PikaImage *image,
                      gint       x,
                      gint       y)
{
  PikaValueArray *args;
  PikaValueArray *return_vals;
  gint value = 0;

  args = pika_value_array_new_from_types (NULL,
                                          PIKA_TYPE_IMAGE, image,
                                          G_TYPE_INT, x,
                                          G_TYPE_INT, y,
                                          G_TYPE_NONE);

  return_vals = _pika_pdb_run_procedure_array (pika_get_pdb (),
                                               "pika-selection-value",
                                               args);
  pika_value_array_unref (args);

  if (PIKA_VALUES_GET_ENUM (return_vals, 0) == PIKA_PDB_SUCCESS)
    value = PIKA_VALUES_GET_INT (return_vals, 1);

  pika_value_array_unref (return_vals);

  return value;
}

/**
 * pika_selection_is_empty:
 * @image: The image.
 *
 * Determine whether the selection is empty.
 *
 * This procedure returns TRUE if the selection for the specified image
 * is empty.
 *
 * Returns: Is the selection empty?
 **/
gboolean
pika_selection_is_empty (PikaImage *image)
{
  PikaValueArray *args;
  PikaValueArray *return_vals;
  gboolean is_empty = FALSE;

  args = pika_value_array_new_from_types (NULL,
                                          PIKA_TYPE_IMAGE, image,
                                          G_TYPE_NONE);

  return_vals = _pika_pdb_run_procedure_array (pika_get_pdb (),
                                               "pika-selection-is-empty",
                                               args);
  pika_value_array_unref (args);

  if (PIKA_VALUES_GET_ENUM (return_vals, 0) == PIKA_PDB_SUCCESS)
    is_empty = PIKA_VALUES_GET_BOOLEAN (return_vals, 1);

  pika_value_array_unref (return_vals);

  return is_empty;
}

/**
 * pika_selection_translate:
 * @image: The image.
 * @offx: x offset for translation.
 * @offy: y offset for translation.
 *
 * Translate the selection by the specified offsets.
 *
 * This procedure actually translates the selection for the specified
 * image by the specified offsets. Regions that are translated from
 * beyond the bounds of the image are set to empty. Valid regions of
 * the selection which are translated beyond the bounds of the image
 * because of this call are lost.
 *
 * Returns: TRUE on success.
 **/
gboolean
pika_selection_translate (PikaImage *image,
                          gint       offx,
                          gint       offy)
{
  PikaValueArray *args;
  PikaValueArray *return_vals;
  gboolean success = TRUE;

  args = pika_value_array_new_from_types (NULL,
                                          PIKA_TYPE_IMAGE, image,
                                          G_TYPE_INT, offx,
                                          G_TYPE_INT, offy,
                                          G_TYPE_NONE);

  return_vals = _pika_pdb_run_procedure_array (pika_get_pdb (),
                                               "pika-selection-translate",
                                               args);
  pika_value_array_unref (args);

  success = PIKA_VALUES_GET_ENUM (return_vals, 0) == PIKA_PDB_SUCCESS;

  pika_value_array_unref (return_vals);

  return success;
}

/**
 * _pika_selection_float:
 * @num_drawables: The number of drawables.
 * @drawables: (array length=num_drawables) (element-type PikaItem): The drawables from which to float selection.
 * @offx: x offset for translation.
 * @offy: y offset for translation.
 *
 * Float the selection from the specified drawable with initial offsets
 * as specified.
 *
 * This procedure determines the region of the specified drawable that
 * lies beneath the current selection. The region is then cut from the
 * drawable and the resulting data is made into a new layer which is
 * instantiated as a floating selection. The offsets allow initial
 * positioning of the new floating selection.
 *
 * Returns: (transfer none): The floated layer.
 **/
PikaLayer *
_pika_selection_float (gint             num_drawables,
                       const PikaItem **drawables,
                       gint             offx,
                       gint             offy)
{
  PikaValueArray *args;
  PikaValueArray *return_vals;
  PikaLayer *layer = NULL;

  args = pika_value_array_new_from_types (NULL,
                                          G_TYPE_INT, num_drawables,
                                          PIKA_TYPE_OBJECT_ARRAY, NULL,
                                          G_TYPE_INT, offx,
                                          G_TYPE_INT, offy,
                                          G_TYPE_NONE);
  pika_value_set_object_array (pika_value_array_index (args, 1), PIKA_TYPE_ITEM, (GObject **) drawables, num_drawables);

  return_vals = _pika_pdb_run_procedure_array (pika_get_pdb (),
                                               "pika-selection-float",
                                               args);
  pika_value_array_unref (args);

  if (PIKA_VALUES_GET_ENUM (return_vals, 0) == PIKA_PDB_SUCCESS)
    layer = PIKA_VALUES_GET_LAYER (return_vals, 1);

  pika_value_array_unref (return_vals);

  return layer;
}

/**
 * pika_selection_invert:
 * @image: The image.
 *
 * Invert the selection mask.
 *
 * This procedure inverts the selection mask. For every pixel in the
 * selection channel, its new value is calculated as (255 - old-value).
 *
 * Returns: TRUE on success.
 **/
gboolean
pika_selection_invert (PikaImage *image)
{
  PikaValueArray *args;
  PikaValueArray *return_vals;
  gboolean success = TRUE;

  args = pika_value_array_new_from_types (NULL,
                                          PIKA_TYPE_IMAGE, image,
                                          G_TYPE_NONE);

  return_vals = _pika_pdb_run_procedure_array (pika_get_pdb (),
                                               "pika-selection-invert",
                                               args);
  pika_value_array_unref (args);

  success = PIKA_VALUES_GET_ENUM (return_vals, 0) == PIKA_PDB_SUCCESS;

  pika_value_array_unref (return_vals);

  return success;
}

/**
 * pika_selection_sharpen:
 * @image: The image.
 *
 * Sharpen the selection mask.
 *
 * This procedure sharpens the selection mask. For every pixel in the
 * selection channel, if the value is &gt; 127, the new pixel is
 * assigned a value of 255. This removes any \"anti-aliasing\" that
 * might exist in the selection mask's boundary.
 *
 * Returns: TRUE on success.
 **/
gboolean
pika_selection_sharpen (PikaImage *image)
{
  PikaValueArray *args;
  PikaValueArray *return_vals;
  gboolean success = TRUE;

  args = pika_value_array_new_from_types (NULL,
                                          PIKA_TYPE_IMAGE, image,
                                          G_TYPE_NONE);

  return_vals = _pika_pdb_run_procedure_array (pika_get_pdb (),
                                               "pika-selection-sharpen",
                                               args);
  pika_value_array_unref (args);

  success = PIKA_VALUES_GET_ENUM (return_vals, 0) == PIKA_PDB_SUCCESS;

  pika_value_array_unref (return_vals);

  return success;
}

/**
 * pika_selection_all:
 * @image: The image.
 *
 * Select all of the image.
 *
 * This procedure sets the selection mask to completely encompass the
 * image. Every pixel in the selection channel is set to 255.
 *
 * Returns: TRUE on success.
 **/
gboolean
pika_selection_all (PikaImage *image)
{
  PikaValueArray *args;
  PikaValueArray *return_vals;
  gboolean success = TRUE;

  args = pika_value_array_new_from_types (NULL,
                                          PIKA_TYPE_IMAGE, image,
                                          G_TYPE_NONE);

  return_vals = _pika_pdb_run_procedure_array (pika_get_pdb (),
                                               "pika-selection-all",
                                               args);
  pika_value_array_unref (args);

  success = PIKA_VALUES_GET_ENUM (return_vals, 0) == PIKA_PDB_SUCCESS;

  pika_value_array_unref (return_vals);

  return success;
}

/**
 * pika_selection_none:
 * @image: The image.
 *
 * Deselect the entire image.
 *
 * This procedure deselects the entire image. Every pixel in the
 * selection channel is set to 0.
 *
 * Returns: TRUE on success.
 **/
gboolean
pika_selection_none (PikaImage *image)
{
  PikaValueArray *args;
  PikaValueArray *return_vals;
  gboolean success = TRUE;

  args = pika_value_array_new_from_types (NULL,
                                          PIKA_TYPE_IMAGE, image,
                                          G_TYPE_NONE);

  return_vals = _pika_pdb_run_procedure_array (pika_get_pdb (),
                                               "pika-selection-none",
                                               args);
  pika_value_array_unref (args);

  success = PIKA_VALUES_GET_ENUM (return_vals, 0) == PIKA_PDB_SUCCESS;

  pika_value_array_unref (return_vals);

  return success;
}

/**
 * pika_selection_feather:
 * @image: The image.
 * @radius: Radius of feather (in pixels).
 *
 * Feather the image's selection
 *
 * This procedure feathers the selection. Feathering is implemented
 * using a gaussian blur.
 *
 * Returns: TRUE on success.
 **/
gboolean
pika_selection_feather (PikaImage *image,
                        gdouble    radius)
{
  PikaValueArray *args;
  PikaValueArray *return_vals;
  gboolean success = TRUE;

  args = pika_value_array_new_from_types (NULL,
                                          PIKA_TYPE_IMAGE, image,
                                          G_TYPE_DOUBLE, radius,
                                          G_TYPE_NONE);

  return_vals = _pika_pdb_run_procedure_array (pika_get_pdb (),
                                               "pika-selection-feather",
                                               args);
  pika_value_array_unref (args);

  success = PIKA_VALUES_GET_ENUM (return_vals, 0) == PIKA_PDB_SUCCESS;

  pika_value_array_unref (return_vals);

  return success;
}

/**
 * pika_selection_border:
 * @image: The image.
 * @radius: Radius of border (in pixels).
 *
 * Border the image's selection
 *
 * This procedure borders the selection. Bordering creates a new
 * selection which is defined along the boundary of the previous
 * selection at every point within the specified radius.
 *
 * Returns: TRUE on success.
 **/
gboolean
pika_selection_border (PikaImage *image,
                       gint       radius)
{
  PikaValueArray *args;
  PikaValueArray *return_vals;
  gboolean success = TRUE;

  args = pika_value_array_new_from_types (NULL,
                                          PIKA_TYPE_IMAGE, image,
                                          G_TYPE_INT, radius,
                                          G_TYPE_NONE);

  return_vals = _pika_pdb_run_procedure_array (pika_get_pdb (),
                                               "pika-selection-border",
                                               args);
  pika_value_array_unref (args);

  success = PIKA_VALUES_GET_ENUM (return_vals, 0) == PIKA_PDB_SUCCESS;

  pika_value_array_unref (return_vals);

  return success;
}

/**
 * pika_selection_grow:
 * @image: The image.
 * @steps: Steps of grow (in pixels).
 *
 * Grow the image's selection
 *
 * This procedure grows the selection. Growing involves expanding the
 * boundary in all directions by the specified pixel amount.
 *
 * Returns: TRUE on success.
 **/
gboolean
pika_selection_grow (PikaImage *image,
                     gint       steps)
{
  PikaValueArray *args;
  PikaValueArray *return_vals;
  gboolean success = TRUE;

  args = pika_value_array_new_from_types (NULL,
                                          PIKA_TYPE_IMAGE, image,
                                          G_TYPE_INT, steps,
                                          G_TYPE_NONE);

  return_vals = _pika_pdb_run_procedure_array (pika_get_pdb (),
                                               "pika-selection-grow",
                                               args);
  pika_value_array_unref (args);

  success = PIKA_VALUES_GET_ENUM (return_vals, 0) == PIKA_PDB_SUCCESS;

  pika_value_array_unref (return_vals);

  return success;
}

/**
 * pika_selection_shrink:
 * @image: The image.
 * @steps: Steps of shrink (in pixels).
 *
 * Shrink the image's selection
 *
 * This procedure shrinks the selection. Shrinking involves trimming
 * the existing selection boundary on all sides by the specified number
 * of pixels.
 *
 * Returns: TRUE on success.
 **/
gboolean
pika_selection_shrink (PikaImage *image,
                       gint       steps)
{
  PikaValueArray *args;
  PikaValueArray *return_vals;
  gboolean success = TRUE;

  args = pika_value_array_new_from_types (NULL,
                                          PIKA_TYPE_IMAGE, image,
                                          G_TYPE_INT, steps,
                                          G_TYPE_NONE);

  return_vals = _pika_pdb_run_procedure_array (pika_get_pdb (),
                                               "pika-selection-shrink",
                                               args);
  pika_value_array_unref (args);

  success = PIKA_VALUES_GET_ENUM (return_vals, 0) == PIKA_PDB_SUCCESS;

  pika_value_array_unref (return_vals);

  return success;
}

/**
 * pika_selection_flood:
 * @image: The image.
 *
 * Remove holes from the image's selection
 *
 * This procedure removes holes from the selection, that can come from
 * selecting a patchy area with the Fuzzy Select Tool. In technical
 * terms this procedure floods the selection. See the Algorithms page
 * in the developer wiki for details.
 *
 * Returns: TRUE on success.
 *
 * Since: 2.10
 **/
gboolean
pika_selection_flood (PikaImage *image)
{
  PikaValueArray *args;
  PikaValueArray *return_vals;
  gboolean success = TRUE;

  args = pika_value_array_new_from_types (NULL,
                                          PIKA_TYPE_IMAGE, image,
                                          G_TYPE_NONE);

  return_vals = _pika_pdb_run_procedure_array (pika_get_pdb (),
                                               "pika-selection-flood",
                                               args);
  pika_value_array_unref (args);

  success = PIKA_VALUES_GET_ENUM (return_vals, 0) == PIKA_PDB_SUCCESS;

  pika_value_array_unref (return_vals);

  return success;
}

/**
 * pika_selection_save:
 * @image: The image.
 *
 * Copy the selection mask to a new channel.
 *
 * This procedure copies the selection mask and stores the content in a
 * new channel. The new channel is automatically inserted into the
 * image's list of channels.
 *
 * Returns: (transfer none): The new channel.
 **/
PikaChannel *
pika_selection_save (PikaImage *image)
{
  PikaValueArray *args;
  PikaValueArray *return_vals;
  PikaChannel *channel = NULL;

  args = pika_value_array_new_from_types (NULL,
                                          PIKA_TYPE_IMAGE, image,
                                          G_TYPE_NONE);

  return_vals = _pika_pdb_run_procedure_array (pika_get_pdb (),
                                               "pika-selection-save",
                                               args);
  pika_value_array_unref (args);

  if (PIKA_VALUES_GET_ENUM (return_vals, 0) == PIKA_PDB_SUCCESS)
    channel = PIKA_VALUES_GET_CHANNEL (return_vals, 1);

  pika_value_array_unref (return_vals);

  return channel;
}
