/* 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 <string.h>

#include <gegl.h>

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

#include "libpikabase/pikabase.h"

#include "pdb-types.h"

#include "core/pikacontext.h"
#include "core/pikadatafactory.h"
#include "core/pikaparamspecs.h"
#include "core/pikapattern.h"
#include "core/pikatempbuf.h"
#include "gegl/pika-babl-compat.h"

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


static PikaValueArray *
pattern_get_by_name_invoker (PikaProcedure         *procedure,
                             Pika                  *pika,
                             PikaContext           *context,
                             PikaProgress          *progress,
                             const PikaValueArray  *args,
                             GError               **error)
{
  gboolean success = TRUE;
  PikaValueArray *return_vals;
  const gchar *name;
  PikaPattern *pattern = NULL;

  name = g_value_get_string (pika_value_array_index (args, 0));

  if (success)
    {
      pattern = PIKA_PATTERN (pika_pdb_get_resource (pika, PIKA_TYPE_PATTERN, name, PIKA_PDB_DATA_ACCESS_READ, error));

      if (! pattern)
        success = FALSE;
    }

  return_vals = pika_procedure_get_return_values (procedure, success,
                                                  error ? *error : NULL);

  if (success)
    g_value_set_object (pika_value_array_index (return_vals, 1), pattern);

  return return_vals;
}

static PikaValueArray *
pattern_get_info_invoker (PikaProcedure         *procedure,
                          Pika                  *pika,
                          PikaContext           *context,
                          PikaProgress          *progress,
                          const PikaValueArray  *args,
                          GError               **error)
{
  gboolean success = TRUE;
  PikaValueArray *return_vals;
  PikaPattern *pattern;
  gint width = 0;
  gint height = 0;
  gint bpp = 0;

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

  if (success)
    {
      const Babl *format;

      format = pika_babl_compat_u8_format (
        pika_temp_buf_get_format (pattern->mask));

      width  = pika_temp_buf_get_width  (pattern->mask);
      height = pika_temp_buf_get_height (pattern->mask);
      bpp    = babl_format_get_bytes_per_pixel (format);
    }

  return_vals = pika_procedure_get_return_values (procedure, success,
                                                  error ? *error : NULL);

  if (success)
    {
      g_value_set_int (pika_value_array_index (return_vals, 1), width);
      g_value_set_int (pika_value_array_index (return_vals, 2), height);
      g_value_set_int (pika_value_array_index (return_vals, 3), bpp);
    }

  return return_vals;
}

static PikaValueArray *
pattern_get_pixels_invoker (PikaProcedure         *procedure,
                            Pika                  *pika,
                            PikaContext           *context,
                            PikaProgress          *progress,
                            const PikaValueArray  *args,
                            GError               **error)
{
  gboolean success = TRUE;
  PikaValueArray *return_vals;
  PikaPattern *pattern;
  gint width = 0;
  gint height = 0;
  gint bpp = 0;
  GBytes *color_bytes = NULL;

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

  if (success)
    {

      const Babl *format;
      gpointer    data;

      format = pika_babl_compat_u8_format (
        pika_temp_buf_get_format (pattern->mask));
      data   = pika_temp_buf_lock (pattern->mask, format, GEGL_ACCESS_READ);

      width           = pika_temp_buf_get_width  (pattern->mask);
      height          = pika_temp_buf_get_height (pattern->mask);
      bpp             = babl_format_get_bytes_per_pixel (format);
      color_bytes     = g_bytes_new (data, pika_temp_buf_get_data_size (pattern->mask));

      pika_temp_buf_unlock (pattern->mask, data);
    }

  return_vals = pika_procedure_get_return_values (procedure, success,
                                                  error ? *error : NULL);

  if (success)
    {
      g_value_set_int (pika_value_array_index (return_vals, 1), width);
      g_value_set_int (pika_value_array_index (return_vals, 2), height);
      g_value_set_int (pika_value_array_index (return_vals, 3), bpp);
      g_value_take_boxed (pika_value_array_index (return_vals, 4), color_bytes);
    }

  return return_vals;
}

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

  /*
   * pika-pattern-get-by-name
   */
  procedure = pika_procedure_new (pattern_get_by_name_invoker);
  pika_object_set_static_name (PIKA_OBJECT (procedure),
                               "pika-pattern-get-by-name");
  pika_procedure_set_static_help (procedure,
                                  "Returns the pattern with the given name.",
                                  "Returns the pattern with the given name.",
                                  NULL);
  pika_procedure_set_static_attribution (procedure,
                                         "Michael Natterer <mitch@gimp.org>",
                                         "Michael Natterer",
                                         "2023");
  pika_procedure_add_argument (procedure,
                               pika_param_spec_string ("name",
                                                       "name",
                                                       "The name of the pattern",
                                                       FALSE, FALSE, TRUE,
                                                       NULL,
                                                       PIKA_PARAM_READWRITE));
  pika_procedure_add_return_value (procedure,
                                   pika_param_spec_pattern ("pattern",
                                                            "pattern",
                                                            "The pattern",
                                                            FALSE,
                                                            PIKA_PARAM_READWRITE));
  pika_pdb_register_procedure (pdb, procedure);
  g_object_unref (procedure);

  /*
   * pika-pattern-get-info
   */
  procedure = pika_procedure_new (pattern_get_info_invoker);
  pika_object_set_static_name (PIKA_OBJECT (procedure),
                               "pika-pattern-get-info");
  pika_procedure_set_static_help (procedure,
                                  "Gets information about the pattern.",
                                  "Gets information about the pattern: the pattern extents (width and height) and bytes per pixel.",
                                  NULL);
  pika_procedure_set_static_attribution (procedure,
                                         "Michael Natterer <mitch@gimp.org>",
                                         "Michael Natterer",
                                         "2004");
  pika_procedure_add_argument (procedure,
                               pika_param_spec_pattern ("pattern",
                                                        "pattern",
                                                        "The pattern",
                                                        FALSE,
                                                        PIKA_PARAM_READWRITE));
  pika_procedure_add_return_value (procedure,
                                   g_param_spec_int ("width",
                                                     "width",
                                                     "The pattern width",
                                                     G_MININT32, G_MAXINT32, 0,
                                                     PIKA_PARAM_READWRITE));
  pika_procedure_add_return_value (procedure,
                                   g_param_spec_int ("height",
                                                     "height",
                                                     "The pattern height",
                                                     G_MININT32, G_MAXINT32, 0,
                                                     PIKA_PARAM_READWRITE));
  pika_procedure_add_return_value (procedure,
                                   g_param_spec_int ("bpp",
                                                     "bpp",
                                                     "The pattern bpp",
                                                     G_MININT32, G_MAXINT32, 0,
                                                     PIKA_PARAM_READWRITE));
  pika_pdb_register_procedure (pdb, procedure);
  g_object_unref (procedure);

  /*
   * pika-pattern-get-pixels
   */
  procedure = pika_procedure_new (pattern_get_pixels_invoker);
  pika_object_set_static_name (PIKA_OBJECT (procedure),
                               "pika-pattern-get-pixels");
  pika_procedure_set_static_help (procedure,
                                  "Gets information about the pattern (including pixels).",
                                  "Gets information about the pattern: the pattern extents (width and height), its bpp, and its pixel data.",
                                  NULL);
  pika_procedure_set_static_attribution (procedure,
                                         "Michael Natterer <mitch@gimp.org>",
                                         "Michael Natterer",
                                         "2004");
  pika_procedure_add_argument (procedure,
                               pika_param_spec_pattern ("pattern",
                                                        "pattern",
                                                        "The pattern",
                                                        FALSE,
                                                        PIKA_PARAM_READWRITE));
  pika_procedure_add_return_value (procedure,
                                   g_param_spec_int ("width",
                                                     "width",
                                                     "The pattern width",
                                                     G_MININT32, G_MAXINT32, 0,
                                                     PIKA_PARAM_READWRITE));
  pika_procedure_add_return_value (procedure,
                                   g_param_spec_int ("height",
                                                     "height",
                                                     "The pattern height",
                                                     G_MININT32, G_MAXINT32, 0,
                                                     PIKA_PARAM_READWRITE));
  pika_procedure_add_return_value (procedure,
                                   g_param_spec_int ("bpp",
                                                     "bpp",
                                                     "The pattern bpp",
                                                     G_MININT32, G_MAXINT32, 0,
                                                     PIKA_PARAM_READWRITE));
  pika_procedure_add_return_value (procedure,
                                   g_param_spec_boxed ("color-bytes",
                                                       "color bytes",
                                                       "The pattern data.",
                                                       G_TYPE_BYTES,
                                                       PIKA_PARAM_READWRITE));
  pika_pdb_register_procedure (pdb, procedure);
  g_object_unref (procedure);
}
