/* 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 <libpika/pika.h>
#include <libpika/pikaui.h>

#include "libpika/stdplugins-intl.h"

#include "openexr-wrapper.h"

#define LOAD_PROC       "file-exr-load"
#define PLUG_IN_BINARY  "file-exr"
#define PLUG_IN_VERSION "0.0.0"


typedef struct _Exr      Exr;
typedef struct _ExrClass ExrClass;

struct _Exr
{
  PikaPlugIn      parent_instance;
};

struct _ExrClass
{
  PikaPlugInClass parent_class;
};


#define EXR_TYPE  (exr_get_type ())
#define EXR(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), EXR_TYPE, Exr))

GType                   exr_get_type         (void) G_GNUC_CONST;

static GList          * exr_query_procedures (PikaPlugIn            *plug_in);
static PikaProcedure  * exr_create_procedure (PikaPlugIn            *plug_in,
                                              const gchar           *name);

static PikaValueArray * exr_load             (PikaProcedure         *procedure,
                                              PikaRunMode            run_mode,
                                              GFile                 *file,
                                              PikaMetadata          *metadata,
                                              PikaMetadataLoadFlags *flags,
                                              PikaProcedureConfig   *config,
                                              gpointer               run_data);

static PikaImage      * load_image           (GFile                 *file,
                                              gboolean               interactive,
                                              GError               **error);
static void             sanitize_comment     (gchar                 *comment);
void                    load_dialog          (void);


G_DEFINE_TYPE (Exr, exr, PIKA_TYPE_PLUG_IN)

PIKA_MAIN (EXR_TYPE)
DEFINE_STD_SET_I18N


static void
exr_class_init (ExrClass *klass)
{
  PikaPlugInClass *plug_in_class = PIKA_PLUG_IN_CLASS (klass);

  plug_in_class->query_procedures = exr_query_procedures;
  plug_in_class->create_procedure = exr_create_procedure;
  plug_in_class->set_i18n         = STD_SET_I18N;
}

static void
exr_init (Exr *exr)
{
}

static GList *
exr_query_procedures (PikaPlugIn *plug_in)
{
  return g_list_append (NULL, g_strdup (LOAD_PROC));
}

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

  if (! strcmp (name, LOAD_PROC))
    {
      procedure = pika_load_procedure_new (plug_in, name,
                                           PIKA_PDB_PROC_TYPE_PLUGIN,
                                           exr_load, NULL, NULL);

      pika_procedure_set_menu_label (procedure, _("OpenEXR image"));

      pika_procedure_set_documentation (procedure,
                                        _("Loads files in the OpenEXR file format"),
                                        "This plug-in loads OpenEXR files. ",
                                        name);
      pika_procedure_set_attribution (procedure,
                                      "Dominik Ernst <dernst@gmx.de>, "
                                      "Mukund Sivaraman <muks@banu.com>",
                                      "Dominik Ernst <dernst@gmx.de>, "
                                      "Mukund Sivaraman <muks@banu.com>",
                                      NULL);

      pika_file_procedure_set_mime_types (PIKA_FILE_PROCEDURE (procedure),
                                          "image/x-exr");
      pika_file_procedure_set_extensions (PIKA_FILE_PROCEDURE (procedure),
                                          "exr");
      pika_file_procedure_set_magics (PIKA_FILE_PROCEDURE (procedure),
                                      "0,long,0x762f3101");
    }

  return procedure;
}

static PikaValueArray *
exr_load (PikaProcedure         *procedure,
          PikaRunMode            run_mode,
          GFile                 *file,
          PikaMetadata          *metadata,
          PikaMetadataLoadFlags *flags,
          PikaProcedureConfig   *config,
          gpointer               run_data)
{
  PikaValueArray *return_vals;
  PikaImage      *image;
  GError         *error  = NULL;

  gegl_init (NULL, NULL);

  image = load_image (file, run_mode == PIKA_RUN_INTERACTIVE,
                      &error);

  if (! image)
    return pika_procedure_new_return_values (procedure,
                                             PIKA_PDB_EXECUTION_ERROR,
                                             error);

  return_vals =  pika_procedure_new_return_values (procedure,
                                                   PIKA_PDB_SUCCESS,
                                                   NULL);

  PIKA_VALUES_SET_IMAGE (return_vals, 1, image);

  return return_vals;
}

static PikaImage *
load_image (GFile        *file,
            gboolean      interactive,
            GError      **error)
{
  EXRLoader        *loader;
  gint              width;
  gint              height;
  gboolean          has_alpha;
  PikaImageBaseType image_type;
  PikaPrecision     image_precision;
  PikaImage        *image = NULL;
  PikaImageType     layer_type;
  PikaLayer        *layer;
  const Babl       *format;
  GeglBuffer       *buffer = NULL;
  gint              bpp;
  gint              tile_height;
  gchar            *pixels = NULL;
  gint              begin;
  gint32            success = FALSE;
  gchar            *comment = NULL;
  PikaColorProfile *profile = NULL;
  guchar           *exif_data;
  guint             exif_size;
  guchar           *xmp_data;
  guint             xmp_size;

  pika_progress_init_printf (_("Opening '%s'"),
                             pika_file_get_utf8_name (file));

  loader = exr_loader_new (g_file_peek_path (file));

  if (! loader)
    {
      g_set_error (error, G_FILE_ERROR, G_FILE_ERROR_FAILED,
                   _("Error opening file '%s' for reading"),
                   pika_file_get_utf8_name (file));
      goto out;
    }

  width  = exr_loader_get_width (loader);
  height = exr_loader_get_height (loader);

  if ((width < 1) || (height < 1))
    {
      g_set_error (error, G_FILE_ERROR, G_FILE_ERROR_FAILED,
                   _("Error querying image dimensions from '%s'"),
                   pika_file_get_utf8_name (file));
      goto out;
    }

  has_alpha = exr_loader_has_alpha (loader) ? TRUE : FALSE;

  switch (exr_loader_get_precision (loader))
    {
    case PREC_UINT:
      image_precision = PIKA_PRECISION_U32_LINEAR;
      break;
    case PREC_HALF:
      image_precision = PIKA_PRECISION_HALF_LINEAR;
      break;
    case PREC_FLOAT:
      image_precision = PIKA_PRECISION_FLOAT_LINEAR;
      break;
    default:
      g_set_error (error, G_FILE_ERROR, G_FILE_ERROR_FAILED,
                   _("Error querying image precision from '%s'"),
                   pika_file_get_utf8_name (file));
      goto out;
    }

  switch (exr_loader_get_image_type (loader))
    {
    case IMAGE_TYPE_RGB:
      image_type = PIKA_RGB;
      layer_type = has_alpha ? PIKA_RGBA_IMAGE : PIKA_RGB_IMAGE;
      break;
    case IMAGE_TYPE_GRAY:
    case IMAGE_TYPE_UNKNOWN_1_CHANNEL:
      image_type = PIKA_GRAY;
      layer_type = has_alpha ? PIKA_GRAYA_IMAGE : PIKA_GRAY_IMAGE;
      break;
    default:
      g_set_error (error, G_FILE_ERROR, G_FILE_ERROR_FAILED,
                   _("Error querying image type from '%s'"),
                   pika_file_get_utf8_name (file));
      goto out;
    }

  image = pika_image_new_with_precision (width, height,
                                         image_type, image_precision);
  if (! image)
    {
      g_set_error (error, 0, 0,
                   _("Could not create new image for '%s': %s"),
                   pika_file_get_utf8_name (file),
                   pika_pdb_get_last_error (pika_get_pdb ()));
      goto out;
    }

  if (exr_loader_get_image_type (loader) == IMAGE_TYPE_UNKNOWN_1_CHANNEL &&
      interactive)
    load_dialog ();

  /* try to load an icc profile, it will be generated on the fly if
   * chromaticities are given
   */
  if (image_type == PIKA_RGB)
    {
      profile = exr_loader_get_profile (loader);

      if (profile)
        pika_image_set_color_profile (image, profile);
    }

  layer = pika_layer_new (image, _("Background"), width, height,
                          layer_type, 100,
                          pika_image_get_default_new_layer_mode (image));
  pika_image_insert_layer (image, layer, NULL, 0);

  buffer = pika_drawable_get_buffer (PIKA_DRAWABLE (layer));
  format = pika_drawable_get_format (PIKA_DRAWABLE (layer));
  bpp = babl_format_get_bytes_per_pixel (format);

  tile_height = pika_tile_height ();
  pixels = g_new0 (gchar, tile_height * width * bpp);

  for (begin = 0; begin < height; begin += tile_height)
    {
      gint end;
      gint num;
      gint i;

      end = MIN (begin + tile_height, height);
      num = end - begin;

      for (i = 0; i < num; i++)
        {
          gint retval;

          retval = exr_loader_read_pixel_row (loader,
                                              pixels + (i * width * bpp),
                                              bpp, begin + i);
          if (retval < 0)
            {
              g_set_error (error, G_FILE_ERROR, G_FILE_ERROR_FAILED,
                           _("Error reading pixel data from '%s'"),
                           pika_file_get_utf8_name (file));
              goto out;
            }
        }

      gegl_buffer_set (buffer, GEGL_RECTANGLE (0, begin, width, num),
                       0, NULL, pixels, GEGL_AUTO_ROWSTRIDE);

      pika_progress_update ((gdouble) begin / (gdouble) height);
    }

  /* try to read the file comment */
  comment = exr_loader_get_comment (loader);
  if (comment)
    {
      PikaParasite *parasite;

      sanitize_comment (comment);
      parasite = pika_parasite_new ("pika-comment",
                                    PIKA_PARASITE_PERSISTENT,
                                    strlen (comment) + 1,
                                    comment);
      pika_image_attach_parasite (image, parasite);
      pika_parasite_free (parasite);
    }

  /* check if the image contains Exif or Xmp data and read it */
  exif_data = exr_loader_get_exif (loader, &exif_size);
  xmp_data  = exr_loader_get_xmp  (loader, &xmp_size);

  if (exif_data || xmp_data)
    {
      PikaMetadata          *metadata = pika_metadata_new ();
      PikaMetadataLoadFlags  flags    = PIKA_METADATA_LOAD_ALL;

      if (exif_data)
        {
          pika_metadata_set_from_exif (metadata, exif_data, exif_size, NULL);
          g_free (exif_data);
        }

      if (xmp_data)
        {
          pika_metadata_set_from_xmp (metadata, xmp_data, xmp_size, NULL);
          g_free (xmp_data);
        }

      if (comment)
        flags &= ~PIKA_METADATA_LOAD_COMMENT;

      if (profile)
        flags &= ~PIKA_METADATA_LOAD_COLORSPACE;

      pika_image_metadata_load_finish (image, "image/exr",
                                       metadata, flags);
      g_object_unref (metadata);
    }

  pika_progress_update (1.0);

  success = TRUE;

 out:
  g_clear_object (&profile);
  g_clear_object (&buffer);
  g_clear_pointer (&pixels, g_free);
  g_clear_pointer (&comment, g_free);
  g_clear_pointer (&loader, exr_loader_unref);

  if (success)
    return image;

  if (image)
    pika_image_delete (image);

  return NULL;
}

/* copy & pasted from file-jpeg/jpeg-load.c */
static void
sanitize_comment (gchar *comment)
{
  const gchar *start_invalid;

  if (! g_utf8_validate (comment, -1, &start_invalid))
    {
      guchar *c;

      for (c = (guchar *) start_invalid; *c; c++)
        {
          if (*c > 126 || (*c < 32 && *c != '\t' && *c != '\n' && *c != '\r'))
            *c = '?';
        }
    }
}

void
load_dialog (void)
{
  GtkWidget *dialog;
  GtkWidget *label;
  GtkWidget *vbox;
  gchar     *label_text;

  pika_ui_init (PLUG_IN_BINARY);

  dialog = pika_dialog_new (_("Import OpenEXR"),
                            "openexr-notice",
                            NULL, 0, NULL, NULL,
                            _("_OK"), GTK_RESPONSE_OK,
                            NULL);

  pika_window_set_transient (GTK_WINDOW (dialog));

  vbox = gtk_box_new (GTK_ORIENTATION_VERTICAL, 2);
  gtk_container_set_border_width (GTK_CONTAINER (vbox), 12);
  gtk_box_pack_start (GTK_BOX (gtk_dialog_get_content_area (GTK_DIALOG (dialog))),
                      vbox, TRUE, TRUE, 0);
  gtk_widget_show (vbox);

  label_text = g_strdup_printf ("<b>%s</b>\n%s", _("Unknown Channel Name"),
                                _("The image contains a single unknown channel.\n"
                                  "It has been converted to grayscale."));
  label = gtk_label_new (NULL);
  gtk_label_set_markup (GTK_LABEL (label), label_text);

  gtk_label_set_selectable (GTK_LABEL (label), TRUE);
  gtk_label_set_justify (GTK_LABEL (label), GTK_JUSTIFY_LEFT);
  gtk_label_set_line_wrap (GTK_LABEL (label), TRUE);
  gtk_label_set_yalign (GTK_LABEL (label), 0.0);
  gtk_box_pack_start (GTK_BOX (vbox), label, TRUE, TRUE, 0);
  gtk_widget_show (label);

  g_free (label_text);

  gtk_widget_show (dialog);

  /* run the dialog */
  pika_dialog_run (PIKA_DIALOG (dialog));

  gtk_widget_destroy (dialog);
}
