/* 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-2003 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/>.
 */

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

#include "config.h"

#include "stamp-pdbgen.h"

#include <gegl.h>

#include <gdk-pixbuf/gdk-pixbuf.h>

#include "libpikabase/pikabase.h"

#include "pdb-types.h"

#include "core/pika.h"
#include "core/pikaimage-convert-indexed.h"
#include "core/pikaimage-convert-precision.h"
#include "core/pikaimage-convert-type.h"
#include "core/pikaimage.h"
#include "core/pikaitemstack.h"
#include "core/pikapalette.h"
#include "core/pikaparamspecs.h"
#include "gegl/pika-babl.h"

#include "pikapdb.h"
#include "pikapdberror.h"
#include "pikapdb-utils.h"
#include "pikaprocedure.h"
#include "internal-procs.h"

#include "pika-intl.h"


static PikaValueArray *
image_convert_rgb_invoker (PikaProcedure         *procedure,
                           Pika                  *pika,
                           PikaContext           *context,
                           PikaProgress          *progress,
                           const PikaValueArray  *args,
                           GError               **error)
{
  gboolean success = TRUE;
  PikaImage *image;

  image = g_value_get_object (pika_value_array_index (args, 0));

  if (success)
    {
      if (pika_pdb_image_is_not_base_type (image, PIKA_RGB, error) &&
          pika_babl_is_valid (PIKA_RGB, pika_image_get_precision (image)))
        {
          success = pika_image_convert_type (image, PIKA_RGB, NULL, NULL, error);
        }
      else
        {
          success = FALSE;
        }
    }

  return pika_procedure_get_return_values (procedure, success,
                                           error ? *error : NULL);
}

static PikaValueArray *
image_convert_grayscale_invoker (PikaProcedure         *procedure,
                                 Pika                  *pika,
                                 PikaContext           *context,
                                 PikaProgress          *progress,
                                 const PikaValueArray  *args,
                                 GError               **error)
{
  gboolean success = TRUE;
  PikaImage *image;

  image = g_value_get_object (pika_value_array_index (args, 0));

  if (success)
    {
      if (pika_pdb_image_is_not_base_type (image, PIKA_GRAY, error) &&
          pika_babl_is_valid (PIKA_GRAY, pika_image_get_precision (image)))
        {
          success = pika_image_convert_type (image, PIKA_GRAY, NULL, NULL, error);
        }
      else
        {
          success = FALSE;
        }
    }

  return pika_procedure_get_return_values (procedure, success,
                                           error ? *error : NULL);
}

static PikaValueArray *
image_convert_indexed_invoker (PikaProcedure         *procedure,
                               Pika                  *pika,
                               PikaContext           *context,
                               PikaProgress          *progress,
                               const PikaValueArray  *args,
                               GError               **error)
{
  gboolean success = TRUE;
  PikaImage *image;
  gint dither_type;
  gint palette_type;
  gint num_cols;
  gboolean alpha_dither;
  gboolean remove_unused;
  const gchar *palette;

  image = g_value_get_object (pika_value_array_index (args, 0));
  dither_type = g_value_get_enum (pika_value_array_index (args, 1));
  palette_type = g_value_get_enum (pika_value_array_index (args, 2));
  num_cols = g_value_get_int (pika_value_array_index (args, 3));
  alpha_dither = g_value_get_boolean (pika_value_array_index (args, 4));
  remove_unused = g_value_get_boolean (pika_value_array_index (args, 5));
  palette = g_value_get_string (pika_value_array_index (args, 6));

  if (success)
    {
      PikaPalette *pal = NULL;

      if (pika_pdb_image_is_not_base_type (image, PIKA_INDEXED, error)             &&
          pika_pdb_image_is_precision (image, PIKA_PRECISION_U8_NON_LINEAR, error) &&
          pika_babl_is_valid (PIKA_INDEXED, pika_image_get_precision (image))      &&
          pika_item_stack_is_flat (PIKA_ITEM_STACK (pika_image_get_layers (image))))
        {
          switch (palette_type)
            {
            case PIKA_CONVERT_PALETTE_GENERATE:
              if (num_cols < 1 || num_cols > MAXNUMCOLORS)
                success = FALSE;
              break;

            case PIKA_CONVERT_PALETTE_CUSTOM:
              pal = PIKA_PALETTE (pika_pdb_get_resource (pika, PIKA_TYPE_PALETTE, palette,
                                                         PIKA_PDB_DATA_ACCESS_READ, error));
              if (! pal)
                {
                  success = FALSE;
                }
              else if (pal->n_colors > MAXNUMCOLORS)
                {
                  g_set_error_literal (error,
                                       PIKA_PDB_ERROR,
                                       PIKA_PDB_ERROR_INVALID_ARGUMENT,
                                       _("Cannot convert to a palette "
                                         "with more than 256 colors."));
                  success = FALSE;
                }
              break;

            default:
              break;
            }
        }
      else
        {
          success = FALSE;
        }

      if (success)
        success = pika_image_convert_indexed (image,
                                              palette_type, num_cols, remove_unused,
                                              dither_type, alpha_dither, FALSE,
                                              pal,
                                              NULL, error);
    }

  return pika_procedure_get_return_values (procedure, success,
                                           error ? *error : NULL);
}

static PikaValueArray *
image_convert_set_dither_matrix_invoker (PikaProcedure         *procedure,
                                         Pika                  *pika,
                                         PikaContext           *context,
                                         PikaProgress          *progress,
                                         const PikaValueArray  *args,
                                         GError               **error)
{
  gboolean success = TRUE;
  gint width;
  gint height;
  GBytes *matrix;

  width = g_value_get_int (pika_value_array_index (args, 0));
  height = g_value_get_int (pika_value_array_index (args, 1));
  matrix = g_value_get_boxed (pika_value_array_index (args, 2));

  if (success)
    {
      if (width == 0 || height == 0 || g_bytes_get_size (matrix) == width * height)
        {
          pika_image_convert_indexed_set_dither_matrix (g_bytes_get_data (matrix, NULL),
                                                        width, height);
        }
      else
        {
          g_set_error_literal (error, PIKA_PDB_ERROR,
                               PIKA_PDB_ERROR_INVALID_ARGUMENT,
                               "Dither matrix length must be width * height");
          success = FALSE;
        }
    }

  return pika_procedure_get_return_values (procedure, success,
                                           error ? *error : NULL);
}

static PikaValueArray *
image_convert_precision_invoker (PikaProcedure         *procedure,
                                 Pika                  *pika,
                                 PikaContext           *context,
                                 PikaProgress          *progress,
                                 const PikaValueArray  *args,
                                 GError               **error)
{
  gboolean success = TRUE;
  PikaImage *image;
  gint precision;

  image = g_value_get_object (pika_value_array_index (args, 0));
  precision = g_value_get_enum (pika_value_array_index (args, 1));

  if (success)
    {
      if (pika_pdb_image_is_not_base_type (image, PIKA_INDEXED, error) &&
          pika_pdb_image_is_not_precision (image, precision, error)    &&
          pika_babl_is_valid (pika_image_get_base_type (image), precision))
        {
          pika_image_convert_precision (image, precision,
                                        GEGL_DITHER_NONE,
                                        GEGL_DITHER_NONE,
                                        GEGL_DITHER_NONE,
                                        progress);
        }
      else
        {
          success = FALSE;
        }
    }

  return pika_procedure_get_return_values (procedure, success,
                                           error ? *error : NULL);
}

void
register_image_convert_procs (PikaPDB *pdb)
{
  PikaProcedure *procedure;

  /*
   * pika-image-convert-rgb
   */
  procedure = pika_procedure_new (image_convert_rgb_invoker);
  pika_object_set_static_name (PIKA_OBJECT (procedure),
                               "pika-image-convert-rgb");
  pika_procedure_set_static_help (procedure,
                                  "Convert specified image to RGB color",
                                  "This procedure converts the specified image to RGB color. This process requires an image in Grayscale or Indexed color mode. No image content is lost in this process aside from the colormap for an indexed image.",
                                  NULL);
  pika_procedure_set_static_attribution (procedure,
                                         "Spencer Kimball & Peter Mattis",
                                         "Spencer Kimball & Peter Mattis",
                                         "1995-1996");
  pika_procedure_add_argument (procedure,
                               pika_param_spec_image ("image",
                                                      "image",
                                                      "The image",
                                                      FALSE,
                                                      PIKA_PARAM_READWRITE));
  pika_pdb_register_procedure (pdb, procedure);
  g_object_unref (procedure);

  /*
   * pika-image-convert-grayscale
   */
  procedure = pika_procedure_new (image_convert_grayscale_invoker);
  pika_object_set_static_name (PIKA_OBJECT (procedure),
                               "pika-image-convert-grayscale");
  pika_procedure_set_static_help (procedure,
                                  "Convert specified image to grayscale",
                                  "This procedure converts the specified image to grayscale. This process requires an image in RGB or Indexed color mode.",
                                  NULL);
  pika_procedure_set_static_attribution (procedure,
                                         "Spencer Kimball & Peter Mattis",
                                         "Spencer Kimball & Peter Mattis",
                                         "1995-1996");
  pika_procedure_add_argument (procedure,
                               pika_param_spec_image ("image",
                                                      "image",
                                                      "The image",
                                                      FALSE,
                                                      PIKA_PARAM_READWRITE));
  pika_pdb_register_procedure (pdb, procedure);
  g_object_unref (procedure);

  /*
   * pika-image-convert-indexed
   */
  procedure = pika_procedure_new (image_convert_indexed_invoker);
  pika_object_set_static_name (PIKA_OBJECT (procedure),
                               "pika-image-convert-indexed");
  pika_procedure_set_static_help (procedure,
                                  "Convert specified image to and Indexed image",
                                  "This procedure converts the specified image to 'indexed' color. This process requires an image in RGB or Grayscale mode. The 'palette_type' specifies what kind of palette to use, A type of '0' means to use an optimal palette of 'num_cols' generated from the colors in the image. A type of '1' means to re-use the previous palette (not currently implemented). A type of '2' means to use the so-called WWW-optimized palette. Type '3' means to use only black and white colors. A type of '4' means to use a palette from the pika palettes directories. The 'dither type' specifies what kind of dithering to use. '0' means no dithering, '1' means standard Floyd-Steinberg error diffusion, '2' means Floyd-Steinberg error diffusion with reduced bleeding, '3' means dithering based on pixel location ('Fixed' dithering).",
                                  NULL);
  pika_procedure_set_static_attribution (procedure,
                                         "Spencer Kimball & Peter Mattis",
                                         "Spencer Kimball & Peter Mattis",
                                         "1995-1996");
  pika_procedure_add_argument (procedure,
                               pika_param_spec_image ("image",
                                                      "image",
                                                      "The image",
                                                      FALSE,
                                                      PIKA_PARAM_READWRITE));
  pika_procedure_add_argument (procedure,
                               g_param_spec_enum ("dither-type",
                                                  "dither type",
                                                  "The dither type to use",
                                                  PIKA_TYPE_CONVERT_DITHER_TYPE,
                                                  PIKA_CONVERT_DITHER_NONE,
                                                  PIKA_PARAM_READWRITE));
  pika_procedure_add_argument (procedure,
                               g_param_spec_enum ("palette-type",
                                                  "palette type",
                                                  "The type of palette to use",
                                                  PIKA_TYPE_CONVERT_PALETTE_TYPE,
                                                  PIKA_CONVERT_PALETTE_GENERATE,
                                                  PIKA_PARAM_READWRITE));
  pika_procedure_add_argument (procedure,
                               g_param_spec_int ("num-cols",
                                                 "num cols",
                                                 "The number of colors to quantize to, ignored unless (palette_type == PIKA_CONVERT_PALETTE_GENERATE)",
                                                 G_MININT32, G_MAXINT32, 0,
                                                 PIKA_PARAM_READWRITE));
  pika_procedure_add_argument (procedure,
                               g_param_spec_boolean ("alpha-dither",
                                                     "alpha dither",
                                                     "Dither transparency to fake partial opacity",
                                                     FALSE,
                                                     PIKA_PARAM_READWRITE));
  pika_procedure_add_argument (procedure,
                               g_param_spec_boolean ("remove-unused",
                                                     "remove unused",
                                                     "Remove unused or duplicate color entries from final palette, ignored if (palette_type == PIKA_CONVERT_PALETTE_GENERATE)",
                                                     FALSE,
                                                     PIKA_PARAM_READWRITE));
  pika_procedure_add_argument (procedure,
                               pika_param_spec_string ("palette",
                                                       "palette",
                                                       "The name of the custom palette to use, ignored unless (palette_type == PIKA_CONVERT_PALETTE_CUSTOM)",
                                                       FALSE, FALSE, FALSE,
                                                       NULL,
                                                       PIKA_PARAM_READWRITE));
  pika_pdb_register_procedure (pdb, procedure);
  g_object_unref (procedure);

  /*
   * pika-image-convert-set-dither-matrix
   */
  procedure = pika_procedure_new (image_convert_set_dither_matrix_invoker);
  pika_object_set_static_name (PIKA_OBJECT (procedure),
                               "pika-image-convert-set-dither-matrix");
  pika_procedure_set_static_help (procedure,
                                  "Set dither matrix for conversion to indexed",
                                  "This procedure sets the dither matrix used when converting images to INDEXED mode with positional dithering.",
                                  NULL);
  pika_procedure_set_static_attribution (procedure,
                                         "David Gowers",
                                         "David Gowers",
                                         "2006");
  pika_procedure_add_argument (procedure,
                               g_param_spec_int ("width",
                                                 "width",
                                                 "Width of the matrix (0 to reset to default matrix)",
                                                 G_MININT32, G_MAXINT32, 0,
                                                 PIKA_PARAM_READWRITE));
  pika_procedure_add_argument (procedure,
                               g_param_spec_int ("height",
                                                 "height",
                                                 "Height of the matrix (0 to reset to default matrix)",
                                                 G_MININT32, G_MAXINT32, 0,
                                                 PIKA_PARAM_READWRITE));
  pika_procedure_add_argument (procedure,
                               g_param_spec_boxed ("matrix",
                                                   "matrix",
                                                   "The matrix -- all values must be >= 1",
                                                   G_TYPE_BYTES,
                                                   PIKA_PARAM_READWRITE));
  pika_pdb_register_procedure (pdb, procedure);
  g_object_unref (procedure);

  /*
   * pika-image-convert-precision
   */
  procedure = pika_procedure_new (image_convert_precision_invoker);
  pika_object_set_static_name (PIKA_OBJECT (procedure),
                               "pika-image-convert-precision");
  pika_procedure_set_static_help (procedure,
                                  "Convert the image to the specified precision",
                                  "This procedure converts the image to the specified precision. Note that indexed images cannot be converted and are always in PIKA_PRECISION_U8.",
                                  NULL);
  pika_procedure_set_static_attribution (procedure,
                                         "Michael Natterer <mitch@gimp.org>",
                                         "Michael Natterer",
                                         "2012");
  pika_procedure_add_argument (procedure,
                               pika_param_spec_image ("image",
                                                      "image",
                                                      "The image",
                                                      FALSE,
                                                      PIKA_PARAM_READWRITE));
  pika_procedure_add_argument (procedure,
                               g_param_spec_enum ("precision",
                                                  "precision",
                                                  "The new precision",
                                                  PIKA_TYPE_PRECISION,
                                                  PIKA_PRECISION_U8_LINEAR,
                                                  PIKA_PARAM_READWRITE));
  pika_pdb_register_procedure (pdb, procedure);
  g_object_unref (procedure);
}
