/* 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 <gdk-pixbuf/gdk-pixbuf.h>
#include <gegl.h>

#include "paint-types.h"

#include "gegl/pika-gegl-loops.h"

#include "core/pika.h"
#include "core/pikabrush.h"
#include "core/pikadrawable.h"
#include "core/pikadynamics.h"
#include "core/pikaimage.h"
#include "core/pikapickable.h"
#include "core/pikasymmetry.h"
#include "core/pikatempbuf.h"

#include "pikaconvolve.h"
#include "pikaconvolveoptions.h"

#include "pika-intl.h"


#define FIELD_COLS    4
#define MIN_BLUR      64         /*  (8/9 original pixel)   */
#define MAX_BLUR      0.25       /*  (1/33 original pixel)  */
#define MIN_SHARPEN   -512
#define MAX_SHARPEN   -64


static void    pika_convolve_paint            (PikaPaintCore    *paint_core,
                                               GList            *drawables,
                                               PikaPaintOptions *paint_options,
                                               PikaSymmetry     *sym,
                                               PikaPaintState    paint_state,
                                               guint32           time);
static void    pika_convolve_motion           (PikaPaintCore    *paint_core,
                                               PikaDrawable     *drawable,
                                               PikaPaintOptions *paint_options,
                                               PikaSymmetry     *sym);

static void    pika_convolve_calculate_matrix (PikaConvolve     *convolve,
                                               PikaConvolveType  type,
                                               gint              radius_x,
                                               gint              radius_y,
                                               gdouble           rate);
static gdouble pika_convolve_sum_matrix       (const gfloat     *matrix);


G_DEFINE_TYPE (PikaConvolve, pika_convolve, PIKA_TYPE_BRUSH_CORE)


void
pika_convolve_register (Pika                      *pika,
                        PikaPaintRegisterCallback  callback)
{
  (* callback) (pika,
                PIKA_TYPE_CONVOLVE,
                PIKA_TYPE_CONVOLVE_OPTIONS,
                "pika-convolve",
                _("Convolve"),
                "pika-tool-blur");
}

static void
pika_convolve_class_init (PikaConvolveClass *klass)
{
  PikaPaintCoreClass *paint_core_class = PIKA_PAINT_CORE_CLASS (klass);

  paint_core_class->paint = pika_convolve_paint;
}

static void
pika_convolve_init (PikaConvolve *convolve)
{
  gint i;

  for (i = 0; i < 9; i++)
    convolve->matrix[i] = 1.0;

  convolve->matrix_divisor = 9.0;
}

static void
pika_convolve_paint (PikaPaintCore    *paint_core,
                     GList            *drawables,
                     PikaPaintOptions *paint_options,
                     PikaSymmetry     *sym,
                     PikaPaintState    paint_state,
                     guint32           time)
{
  g_return_if_fail (g_list_length (drawables) == 1);

  switch (paint_state)
    {
    case PIKA_PAINT_STATE_MOTION:
      for (GList *iter = drawables; iter; iter = iter->next)
        pika_convolve_motion (paint_core, iter->data, paint_options, sym);
      break;

    default:
      break;
    }
}

static void
pika_convolve_motion (PikaPaintCore    *paint_core,
                      PikaDrawable     *drawable,
                      PikaPaintOptions *paint_options,
                      PikaSymmetry     *sym)
{
  PikaConvolve        *convolve   = PIKA_CONVOLVE (paint_core);
  PikaBrushCore       *brush_core = PIKA_BRUSH_CORE (paint_core);
  PikaConvolveOptions *options    = PIKA_CONVOLVE_OPTIONS (paint_options);
  PikaContext         *context    = PIKA_CONTEXT (paint_options);
  PikaDynamics        *dynamics   = PIKA_BRUSH_CORE (paint_core)->dynamics;
  PikaImage           *image      = pika_item_get_image (PIKA_ITEM (drawable));
  GeglBuffer          *paint_buffer;
  gint                 paint_buffer_x;
  gint                 paint_buffer_y;
  PikaTempBuf         *temp_buf;
  GeglBuffer          *convolve_buffer;
  gdouble              fade_point;
  gdouble              opacity;
  gdouble              rate;
  PikaCoords           coords;
  gint                 off_x, off_y;
  gint                 paint_width, paint_height;
  gint                 n_strokes;
  gint                 i;

  fade_point = pika_paint_options_get_fade (paint_options, image,
                                            paint_core->pixel_dist);

  pika_item_get_offset (PIKA_ITEM (drawable), &off_x, &off_y);
  coords = *(pika_symmetry_get_origin (sym));
  coords.x -= off_x;
  coords.y -= off_y;
  pika_symmetry_set_origin (sym, drawable, &coords);

  opacity = pika_dynamics_get_linear_value (dynamics,
                                            PIKA_DYNAMICS_OUTPUT_OPACITY,
                                            &coords,
                                            paint_options,
                                            fade_point);
  if (opacity == 0.0)
    return;

  pika_brush_core_eval_transform_dynamics (PIKA_BRUSH_CORE (paint_core),
                                           image,
                                           paint_options,
                                           &coords);
  n_strokes = pika_symmetry_get_size (sym);
  for (i = 0; i < n_strokes; i++)
    {
      coords = *(pika_symmetry_get_coords (sym, i));

      pika_brush_core_eval_transform_symmetry (brush_core, sym, i);

      paint_buffer = pika_paint_core_get_paint_buffer (paint_core, drawable,
                                                       paint_options,
                                                       PIKA_LAYER_MODE_NORMAL,
                                                       &coords,
                                                       &paint_buffer_x,
                                                       &paint_buffer_y,
                                                       &paint_width,
                                                       &paint_height);
      if (! paint_buffer)
        continue;

      rate = (options->rate *
              pika_dynamics_get_linear_value (dynamics,
                                              PIKA_DYNAMICS_OUTPUT_RATE,
                                              &coords,
                                              paint_options,
                                              fade_point));

      pika_convolve_calculate_matrix (convolve, options->type,
                                      pika_brush_get_width  (brush_core->brush) / 2,
                                      pika_brush_get_height (brush_core->brush) / 2,
                                      rate);

      /*  need a linear buffer for pika_gegl_convolve()  */
      temp_buf = pika_temp_buf_new (gegl_buffer_get_width  (paint_buffer),
                                    gegl_buffer_get_height (paint_buffer),
                                    gegl_buffer_get_format (paint_buffer));
      convolve_buffer = pika_temp_buf_create_buffer (temp_buf);
      pika_temp_buf_unref (temp_buf);

      pika_gegl_buffer_copy (
        pika_drawable_get_buffer (drawable),
        GEGL_RECTANGLE (paint_buffer_x,
                        paint_buffer_y,
                        gegl_buffer_get_width  (paint_buffer),
                        gegl_buffer_get_height (paint_buffer)),
        GEGL_ABYSS_NONE,
        convolve_buffer,
        GEGL_RECTANGLE (0, 0, 0, 0));

      pika_gegl_convolve (convolve_buffer,
                          GEGL_RECTANGLE (0, 0,
                                          gegl_buffer_get_width  (convolve_buffer),
                                          gegl_buffer_get_height (convolve_buffer)),
                          paint_buffer,
                          GEGL_RECTANGLE (0, 0,
                                          gegl_buffer_get_width  (paint_buffer),
                                          gegl_buffer_get_height (paint_buffer)),
                          convolve->matrix, 3, convolve->matrix_divisor,
                          PIKA_NORMAL_CONVOL, TRUE);

      g_object_unref (convolve_buffer);

      pika_brush_core_replace_canvas (brush_core, drawable,
                                      &coords,
                                      MIN (opacity, PIKA_OPACITY_OPAQUE),
                                      pika_context_get_opacity (context),
                                      pika_paint_options_get_brush_mode (paint_options),
                                      1.0,
                                      PIKA_PAINT_INCREMENTAL);
    }
}

static void
pika_convolve_calculate_matrix (PikaConvolve    *convolve,
                                PikaConvolveType type,
                                gint             radius_x,
                                gint             radius_y,
                                gdouble          rate)
{
  /*  find percent of tool pressure  */
  const gdouble percent = MIN (rate / 100.0, 1.0);

  convolve->matrix[0] = (radius_x && radius_y) ? 1.0 : 0.0;
  convolve->matrix[1] = (radius_y)             ? 1.0 : 0.0;
  convolve->matrix[2] = (radius_x && radius_y) ? 1.0 : 0.0;
  convolve->matrix[3] = (radius_x)             ? 1.0 : 0.0;

  /*  get the appropriate convolution matrix and size and divisor  */
  switch (type)
    {
    case PIKA_CONVOLVE_BLUR:
      convolve->matrix[4] = MIN_BLUR + percent * (MAX_BLUR - MIN_BLUR);
      break;

    case PIKA_CONVOLVE_SHARPEN:
      convolve->matrix[4] = MIN_SHARPEN + percent * (MAX_SHARPEN - MIN_SHARPEN);
      break;
    }

  convolve->matrix[5] = (radius_x)             ? 1.0 : 0.0;
  convolve->matrix[6] = (radius_x && radius_y) ? 1.0 : 0.0;
  convolve->matrix[7] = (radius_y)             ? 1.0 : 0.0;
  convolve->matrix[8] = (radius_x && radius_y) ? 1.0 : 0.0;

  convolve->matrix_divisor = pika_convolve_sum_matrix (convolve->matrix);
}

static gdouble
pika_convolve_sum_matrix (const gfloat *matrix)
{
  gdouble sum = 0.0;
  gint    i;

  for (i = 0; i < 9; i++)
    sum += matrix[i];

  return sum;
}
