/* 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
 *
 * Destripe filter
 *
 * Copyright 1997 Marc Lehmann, heavily modified from a filter by
 * Michael Sweet.
 *
 * 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 <string.h>

#include <libpika/pika.h>
#include <libpika/pikaui.h>

#include "libpika/stdplugins-intl.h"


#define PLUG_IN_PROC    "plug-in-destripe"
#define PLUG_IN_BINARY  "destripe"
#define PLUG_IN_ROLE    "pika-destripe"
#define PLUG_IN_VERSION "0.2"
#define MAX_AVG         100


typedef struct _Destripe      Destripe;
typedef struct _DestripeClass DestripeClass;

struct _Destripe
{
  PikaPlugIn parent_instance;
};

struct _DestripeClass
{
  PikaPlugInClass parent_class;
};


#define DESTRIPE_TYPE  (destripe_get_type ())
#define DESTRIPE(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), DESTRIPE_TYPE, Destripe))

GType                   destripe_get_type         (void) G_GNUC_CONST;

static GList          * destripe_query_procedures (PikaPlugIn           *plug_in);
static PikaProcedure  * destripe_create_procedure (PikaPlugIn           *plug_in,
                                                   const gchar          *name);

static PikaValueArray * destripe_run              (PikaProcedure        *procedure,
                                                   PikaRunMode           run_mode,
                                                   PikaImage            *image,
                                                   gint                  n_drawables,
                                                   PikaDrawable        **drawables,
                                                   PikaProcedureConfig  *config,
                                                   gpointer              run_data);

static void             destripe                  (GObject              *config,
                                                   PikaDrawable         *drawable,
                                                   PikaPreview          *preview);
static void             destripe_preview          (GtkWidget            *widget,
                                                   GObject              *config);

static gboolean         destripe_dialog           (PikaProcedure        *procedure,
                                                   GObject              *config,
                                                   PikaDrawable         *drawable);


G_DEFINE_TYPE (Destripe, destripe, PIKA_TYPE_PLUG_IN)

PIKA_MAIN (DESTRIPE_TYPE)
DEFINE_STD_SET_I18N


static void
destripe_class_init (DestripeClass *klass)
{
  PikaPlugInClass *plug_in_class = PIKA_PLUG_IN_CLASS (klass);

  plug_in_class->query_procedures = destripe_query_procedures;
  plug_in_class->create_procedure = destripe_create_procedure;
  plug_in_class->set_i18n         = STD_SET_I18N;
}

static void
destripe_init (Destripe *destripe)
{
}

static GList *
destripe_query_procedures (PikaPlugIn *plug_in)
{
  return g_list_append (NULL, g_strdup (PLUG_IN_PROC));
}

static PikaProcedure *
destripe_create_procedure (PikaPlugIn  *plug_in,
                               const gchar *name)
{
  PikaProcedure *procedure = NULL;

  if (! strcmp (name, PLUG_IN_PROC))
    {
      procedure = pika_image_procedure_new (plug_in, name,
                                            PIKA_PDB_PROC_TYPE_PLUGIN,
                                            destripe_run, NULL, NULL);

      pika_procedure_set_image_types (procedure, "RGB*, GRAY*");
      pika_procedure_set_sensitivity_mask (procedure,
                                           PIKA_PROCEDURE_SENSITIVE_DRAWABLE);

      pika_procedure_set_menu_label (procedure, _("Des_tripe..."));
      pika_procedure_add_menu_path (procedure, "<Image>/Colors/Tone Mapping");

      pika_procedure_set_documentation (procedure,
                                        _("Remove vertical stripe artifacts "
                                          "from the image"),
                                        _("This plug-in tries to remove vertical "
                                          "stripes from an image."),
                                        name);
      pika_procedure_set_attribution (procedure,
                                      "Marc Lehmann <pcg@goof.com>",
                                      "Marc Lehmann <pcg@goof.com>",
                                      PLUG_IN_VERSION);

      PIKA_PROC_ARG_INT (procedure, "avg-width",
                         _("_Width"),
                         _("Averaging filter width"),
                         2, MAX_AVG, 36,
                         G_PARAM_READWRITE);

      PIKA_PROC_ARG_BOOLEAN (procedure,
                             "create-histogram",
                             _("Create _histogram"),
                             _("Output a histogram"),
                             FALSE,
                             G_PARAM_READWRITE);
    }

  return procedure;
}

static PikaValueArray *
destripe_run (PikaProcedure        *procedure,
              PikaRunMode           run_mode,
              PikaImage            *image,
              gint                  n_drawables,
              PikaDrawable        **drawables,
              PikaProcedureConfig  *config,
              gpointer              run_data)
{
  PikaDrawable *drawable;

  gegl_init (NULL, NULL);

  if (n_drawables != 1)
    {
      GError *error = NULL;

      g_set_error (&error, PIKA_PLUG_IN_ERROR, 0,
                   _("Procedure '%s' only works with one drawable."),
                   PLUG_IN_PROC);

      return pika_procedure_new_return_values (procedure,
                                               PIKA_PDB_CALLING_ERROR,
                                               error);
    }
  else
    {
      drawable = drawables[0];
    }

  if (run_mode == PIKA_RUN_INTERACTIVE && ! destripe_dialog (procedure, G_OBJECT (config), drawable))
    return pika_procedure_new_return_values (procedure,
                                             PIKA_PDB_CANCEL,
                                             NULL);

  if (pika_drawable_is_rgb (drawable)|| pika_drawable_is_gray (drawable))
    destripe (G_OBJECT (config), drawable, NULL);
  else
    return pika_procedure_new_return_values (procedure,
                                             PIKA_PDB_EXECUTION_ERROR,
                                             NULL);

  if (run_mode != PIKA_RUN_NONINTERACTIVE)
    pika_displays_flush ();

  return pika_procedure_new_return_values (procedure, PIKA_PDB_SUCCESS, NULL);
}

static void
destripe (GObject      *config,
          PikaDrawable *drawable,
          PikaPreview  *preview)
{
  GeglBuffer *src_buffer;
  GeglBuffer *dest_buffer;
  const Babl *format;
  guchar     *src_rows;       /* image data */
  gdouble     progress, progress_inc;
  gint        x1, x2, y1;
  gint        width, height;
  gint        bpp;
  glong      *hist, *corr;        /* "histogram" data */
  gint        tile_width = pika_tile_width ();
  gint        i, x, y, ox, cols;
  gint        avg_width;
  gboolean    histogram;

  g_object_get (config,
                "avg-width",        &avg_width,
                "create-histogram", &histogram,
                NULL);

  progress     = 0.0;
  progress_inc = 0.0;

  if (preview)
    {
      pika_preview_get_position (preview, &x1, &y1);
      pika_preview_get_size (preview, &width, &height);
    }
  else
    {
      pika_progress_init (_("Destriping"));

      if (! pika_drawable_mask_intersect (drawable,
                                          &x1, &y1, &width, &height))
        {
          return;
        }

      progress = 0;
      progress_inc = 0.5 * tile_width / width;
    }

  x2 = x1 + width;

  if (pika_drawable_is_rgb (drawable))
    {
      if (pika_drawable_has_alpha (drawable))
        format = babl_format ("R'G'B'A u8");
      else
        format = babl_format ("R'G'B' u8");
    }
  else
    {
      if (pika_drawable_has_alpha (drawable))
        format = babl_format ("Y'A u8");
      else
        format = babl_format ("Y' u8");
    }

  bpp = babl_format_get_bytes_per_pixel (format);

  /*
   * Setup for filter...
   */

  src_buffer  = pika_drawable_get_buffer (drawable);
  dest_buffer = pika_drawable_get_shadow_buffer (drawable);

  hist = g_new (long, width * bpp);
  corr = g_new (long, width * bpp);
  src_rows = g_new (guchar, tile_width * height * bpp);

  memset (hist, 0, width * bpp * sizeof (long));

  /*
   * collect "histogram" data.
   */

  for (ox = x1; ox < x2; ox += tile_width)
    {
      guchar *rows = src_rows;

      cols = x2 - ox;
      if (cols > tile_width)
        cols = tile_width;

      gegl_buffer_get (src_buffer, GEGL_RECTANGLE (ox, y1, cols, height), 1.0,
                       format, rows,
                       GEGL_AUTO_ROWSTRIDE, GEGL_ABYSS_NONE);

      for (y = 0; y < height; y++)
        {
          long   *h       = hist + (ox - x1) * bpp;
          guchar *row_end = rows + cols * bpp;

          while (rows < row_end)
            *h++ += *rows++;
        }

      if (! preview)
        pika_progress_update (progress += progress_inc);
    }

  /*
   * average out histogram
   */

  {
    gint extend = (avg_width / 2) * bpp;

    for (i = 0; i < MIN (3, bpp); i++)
      {
        long *h   = hist - extend + i;
        long *c   = corr - extend + i;
        long  sum = 0;
        gint  cnt = 0;

        for (x = -extend; x < width * bpp; x += bpp)
          {
            if (x + extend < width * bpp)
              {
                sum += h[ extend]; cnt++;
              }

            if (x - extend >= 0)
              {
                sum -= h[-extend]; cnt--;
              }

            if (x >= 0)
              {
                if (*h)
                  *c = ((sum / cnt - *h) << 10) / *h;
                else
                  *c = G_MAXINT;
              }

            h += bpp;
            c += bpp;
          }
      }
  }

  /*
   * remove stripes.
   */

  for (ox = x1; ox < x2; ox += tile_width)
    {
      guchar *rows = src_rows;

      cols = x2 - ox;
      if (cols > tile_width)
        cols = tile_width;

      gegl_buffer_get (src_buffer, GEGL_RECTANGLE (ox, y1, cols, height), 1.0,
                       format, rows,
                       GEGL_AUTO_ROWSTRIDE, GEGL_ABYSS_NONE);

      if (! preview)
        pika_progress_update (progress += progress_inc);

      for (y = 0; y < height; y++)
        {
          long   *c = corr + (ox - x1) * bpp;
          guchar *row_end = rows + cols * bpp;

          if (histogram)
            {
              while (rows < row_end)
                {
                  *rows = MIN (255, MAX (0, 128 + (*rows * *c >> 10)));
                  c++; rows++;
                }
            }
          else
            {
              while (rows < row_end)
                {
                  *rows = MIN (255, MAX (0, *rows + (*rows * *c >> 10) ));
                  c++; rows++;
                }
            }
        }

      gegl_buffer_set (dest_buffer, GEGL_RECTANGLE (ox, y1, cols, height), 0,
                       format, src_rows,
                       GEGL_AUTO_ROWSTRIDE);

      if (! preview)
        pika_progress_update (progress += progress_inc);
    }

  g_free (src_rows);

  g_object_unref (src_buffer);

  if (preview)
    {
      guchar *buffer = g_new (guchar, width * height * bpp);

      gegl_buffer_get (dest_buffer, GEGL_RECTANGLE (x1, y1, width, height), 1.0,
                       format, buffer,
                       GEGL_AUTO_ROWSTRIDE, GEGL_ABYSS_NONE);

      pika_preview_draw_buffer (PIKA_PREVIEW (preview),
                                buffer, width * bpp);

      g_free (buffer);
      g_object_unref (dest_buffer);
    }
  else
    {
      g_object_unref (dest_buffer);

      pika_progress_update (1.0);

      pika_drawable_merge_shadow (drawable, TRUE);
      pika_drawable_update (drawable,
                            x1, y1, width, height);
    }

  g_free (hist);
  g_free (corr);
}

static void
destripe_preview (GtkWidget *widget,
                  GObject   *config)
{
  PikaPreview  *preview  = PIKA_PREVIEW (widget);
  PikaDrawable *drawable = g_object_get_data (config, "drawable");

  destripe (config, drawable, preview);
}


static gboolean
destripe_dialog (PikaProcedure *procedure,
                 GObject       *config,
                 PikaDrawable  *drawable)
{
  GtkWidget     *dialog;
  GtkWidget     *preview;
  GtkWidget     *scale;
  GtkWidget     *button;
  gboolean       run;

  pika_ui_init (PLUG_IN_BINARY);

  dialog = pika_procedure_dialog_new (procedure,
                                      PIKA_PROCEDURE_CONFIG (config),
                                      _("Destripe"));

  pika_dialog_set_alternative_button_order (GTK_DIALOG (dialog),
                                            GTK_RESPONSE_OK,
                                            GTK_RESPONSE_CANCEL,
                                           -1);

  pika_window_set_transient (GTK_WINDOW (dialog));

  preview = pika_drawable_preview_new_from_drawable (drawable);
  gtk_box_pack_start (GTK_BOX (gtk_dialog_get_content_area (GTK_DIALOG (dialog))),
                      preview, TRUE, TRUE, 0);
  gtk_widget_show (preview);

  scale = pika_procedure_dialog_get_scale_entry (PIKA_PROCEDURE_DIALOG (dialog),
                                                 "avg-width", 1.0);
  gtk_widget_set_margin_bottom (scale, 12);

  button = pika_procedure_dialog_get_widget (PIKA_PROCEDURE_DIALOG (dialog),
                                             "create-histogram",
                                             GTK_TYPE_CHECK_BUTTON);
  gtk_widget_set_margin_bottom (button, 12);

  g_object_set_data (config, "drawable", drawable);

  g_signal_connect (preview, "invalidated",
                    G_CALLBACK (destripe_preview),
                    config);

  g_signal_connect_swapped (config, "notify",
                            G_CALLBACK (pika_preview_invalidate),
                            preview);

  pika_procedure_dialog_fill (PIKA_PROCEDURE_DIALOG (dialog),
                              "avg-width", "create-histogram",
                              NULL);

  gtk_widget_show (dialog);

  run = pika_procedure_dialog_run (PIKA_PROCEDURE_DIALOG (dialog));

  gtk_widget_destroy (dialog);

  return run;
}
