/* 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 "gegl/pika-gegl-utils.h"

#include "operations/layer-modes/pika-layer-modes.h"

#include "pika-utils.h"
#include "pikabezierdesc.h"
#include "pikachannel.h"
#include "pikadrawable-fill.h"
#include "pikaerror.h"
#include "pikafilloptions.h"
#include "pikaimage.h"
#include "pikapattern.h"
#include "pikapickable.h"
#include "pikascanconvert.h"

#include "vectors/pikavectors.h"

#include "pika-intl.h"


/*  public functions  */

void
pika_drawable_fill (PikaDrawable *drawable,
                    PikaContext  *context,
                    PikaFillType  fill_type)
{
  PikaRGB      color;
  PikaPattern *pattern;

  g_return_if_fail (PIKA_IS_DRAWABLE (drawable));
  g_return_if_fail (PIKA_IS_CONTEXT (context));

  if (fill_type == PIKA_FILL_TRANSPARENT &&
      ! pika_drawable_has_alpha (drawable))
    {
      fill_type = PIKA_FILL_BACKGROUND;
    }

  if (! pika_get_fill_params (context, fill_type, &color, &pattern, NULL))
    return;

  pika_drawable_fill_buffer (drawable,
                             pika_drawable_get_buffer (drawable),
                             &color, pattern, 0, 0);

  pika_drawable_update (drawable, 0, 0, -1, -1);
}

void
pika_drawable_fill_buffer (PikaDrawable  *drawable,
                           GeglBuffer    *buffer,
                           const PikaRGB *color,
                           PikaPattern   *pattern,
                           gint           pattern_offset_x,
                           gint           pattern_offset_y)
{
  g_return_if_fail (PIKA_IS_DRAWABLE (drawable));
  g_return_if_fail (GEGL_IS_BUFFER (buffer));
  g_return_if_fail (color != NULL || pattern != NULL);
  g_return_if_fail (pattern == NULL || PIKA_IS_PATTERN (pattern));

  if (pattern)
    {
      GeglBuffer       *src_buffer;
      GeglBuffer       *dest_buffer;
      PikaColorProfile *src_profile;
      PikaColorProfile *dest_profile;

      src_buffer = pika_pattern_create_buffer (pattern);

      src_profile  = pika_babl_format_get_color_profile (
                       gegl_buffer_get_format (src_buffer));
      dest_profile = pika_color_managed_get_color_profile (
                       PIKA_COLOR_MANAGED (drawable));

      if (pika_color_transform_can_gegl_copy (src_profile, dest_profile))
        {
          dest_buffer = g_object_ref (src_buffer);
        }
      else
        {
          dest_buffer = gegl_buffer_new (gegl_buffer_get_extent (src_buffer),
                                         gegl_buffer_get_format (buffer));

          pika_gegl_convert_color_profile (
            src_buffer,  NULL, src_profile,
            dest_buffer, NULL, dest_profile,
            PIKA_COLOR_RENDERING_INTENT_PERCEPTUAL,
            TRUE,
            NULL);
        }

      g_object_unref (src_profile);

      gegl_buffer_set_pattern (buffer, NULL, dest_buffer,
                               pattern_offset_x, pattern_offset_y);

      g_object_unref (src_buffer);
      g_object_unref (dest_buffer);
    }
  else
    {
      PikaRGB    image_color;
      GeglColor *gegl_color;

      pika_pickable_srgb_to_image_color (PIKA_PICKABLE (drawable),
                                         color, &image_color);

      if (! pika_drawable_has_alpha (drawable))
        pika_rgb_set_alpha (&image_color, 1.0);

      gegl_color = pika_gegl_color_new (&image_color,
                                        pika_drawable_get_space (drawable));
      gegl_buffer_set_color (buffer, NULL, gegl_color);
      g_object_unref (gegl_color);
    }
}

void
pika_drawable_fill_boundary (PikaDrawable       *drawable,
                             PikaFillOptions    *options,
                             const PikaBoundSeg *bound_segs,
                             gint                n_bound_segs,
                             gint                offset_x,
                             gint                offset_y,
                             gboolean            push_undo)
{
  PikaScanConvert *scan_convert;

  g_return_if_fail (PIKA_IS_DRAWABLE (drawable));
  g_return_if_fail (pika_item_is_attached (PIKA_ITEM (drawable)));
  g_return_if_fail (PIKA_IS_FILL_OPTIONS (options));
  g_return_if_fail (bound_segs == NULL || n_bound_segs != 0);
  g_return_if_fail (pika_fill_options_get_style (options) !=
                    PIKA_FILL_STYLE_PATTERN ||
                    pika_context_get_pattern (PIKA_CONTEXT (options)) != NULL);

  scan_convert = pika_scan_convert_new_from_boundary (bound_segs, n_bound_segs,
                                                      offset_x, offset_y);

  if (scan_convert)
    {
      pika_drawable_fill_scan_convert (drawable, options,
                                       scan_convert, push_undo);
      pika_scan_convert_free (scan_convert);
    }
}

gboolean
pika_drawable_fill_vectors (PikaDrawable     *drawable,
                            PikaFillOptions  *options,
                            PikaVectors      *vectors,
                            gboolean          push_undo,
                            GError          **error)
{
  const PikaBezierDesc *bezier;

  g_return_val_if_fail (PIKA_IS_DRAWABLE (drawable), FALSE);
  g_return_val_if_fail (pika_item_is_attached (PIKA_ITEM (drawable)), FALSE);
  g_return_val_if_fail (PIKA_IS_FILL_OPTIONS (options), FALSE);
  g_return_val_if_fail (PIKA_IS_VECTORS (vectors), FALSE);
  g_return_val_if_fail (pika_fill_options_get_style (options) !=
                        PIKA_FILL_STYLE_PATTERN ||
                        pika_context_get_pattern (PIKA_CONTEXT (options)) != NULL,
                        FALSE);
  g_return_val_if_fail (error == NULL || *error == NULL, FALSE);

  bezier = pika_vectors_get_bezier (vectors);

  if (bezier && bezier->num_data > 4)
    {
      PikaScanConvert *scan_convert = pika_scan_convert_new ();

      pika_scan_convert_add_bezier (scan_convert, bezier);
      pika_drawable_fill_scan_convert (drawable, options,
                                       scan_convert, push_undo);

      pika_scan_convert_free (scan_convert);

      return TRUE;
    }

  g_set_error_literal (error, PIKA_ERROR, PIKA_FAILED,
                       _("Not enough points to fill"));

  return FALSE;
}

void
pika_drawable_fill_scan_convert (PikaDrawable    *drawable,
                                 PikaFillOptions *options,
                                 PikaScanConvert *scan_convert,
                                 gboolean         push_undo)
{
  PikaContext *context;
  GeglBuffer  *buffer;
  GeglBuffer  *mask_buffer;
  gint         x, y, w, h;
  gint         off_x;
  gint         off_y;

  g_return_if_fail (PIKA_IS_DRAWABLE (drawable));
  g_return_if_fail (pika_item_is_attached (PIKA_ITEM (drawable)));
  g_return_if_fail (PIKA_IS_FILL_OPTIONS (options));
  g_return_if_fail (scan_convert != NULL);
  g_return_if_fail (pika_fill_options_get_style (options) !=
                    PIKA_FILL_STYLE_PATTERN ||
                    pika_context_get_pattern (PIKA_CONTEXT (options)) != NULL);

  context = PIKA_CONTEXT (options);

  if (! pika_item_mask_intersect (PIKA_ITEM (drawable), &x, &y, &w, &h))
    return;

  /* fill a 1-bpp GeglBuffer with black, this will describe the shape
   * of the stroke.
   */
  mask_buffer = gegl_buffer_new (GEGL_RECTANGLE (0, 0, w, h),
                                 babl_format ("Y u8"));

  /* render the stroke into it */
  pika_item_get_offset (PIKA_ITEM (drawable), &off_x, &off_y);

  pika_scan_convert_render (scan_convert, mask_buffer,
                            x + off_x, y + off_y,
                            pika_fill_options_get_antialias (options));

  buffer = pika_fill_options_create_buffer (options, drawable,
                                            GEGL_RECTANGLE (0, 0, w, h),
                                            -x, -y);

  pika_gegl_apply_opacity (buffer, NULL, NULL, buffer,
                           mask_buffer, 0, 0, 1.0);
  g_object_unref (mask_buffer);

  /* Apply to drawable */
  pika_drawable_apply_buffer (drawable, buffer,
                              GEGL_RECTANGLE (0, 0, w, h),
                              push_undo, C_("undo-type", "Render Stroke"),
                              pika_context_get_opacity (context),
                              pika_context_get_paint_mode (context),
                              PIKA_LAYER_COLOR_SPACE_AUTO,
                              PIKA_LAYER_COLOR_SPACE_AUTO,
                              pika_layer_mode_get_paint_composite_mode (
                                pika_context_get_paint_mode (context)),
                              NULL, x, y);

  g_object_unref (buffer);

  pika_drawable_update (drawable, x, y, w, h);
}
