/*   PIKA - Photo and Image Kooker Application
 *   Copyright (C) 1995 Spencer Kimball and Peter Mattis
 *
 *   Farbfeld Image Format plug-in
 *
 *   Copyright (C) 2023 Alex S.
 *
 *   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 <errno.h>

#include <glib/gstdio.h>

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

#include "libpika/stdplugins-intl.h"


#define LOAD_PROC      "file-farbfeld-load"
#define SAVE_PROC      "file-farbfeld-save"
#define PLUG_IN_BINARY "file-farbfeld"
#define PLUG_IN_ROLE   "pika-file-farbfeld"


typedef struct _Farbfeld      Farbfeld;
typedef struct _FarbfeldClass FarbfeldClass;

struct _Farbfeld
{
  PikaPlugIn      parent_instance;
};

struct _FarbfeldClass
{
  PikaPlugInClass parent_class;
};


#define FARBFELD_TYPE  (farbfeld_get_type ())
#define FARBFELD(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), FARBFELD_TYPE, Farbfeld))

GType                   farbfeld_get_type         (void) G_GNUC_CONST;


static GList          * farbfeld_query_procedures (PikaPlugIn            *plug_in);
static PikaProcedure  * farbfeld_create_procedure (PikaPlugIn            *plug_in,
                                                   const gchar           *name);

static PikaValueArray * farbfeld_load             (PikaProcedure         *procedure,
                                                   PikaRunMode            run_mode,
                                                   GFile                 *file,
                                                   PikaMetadata          *metadata,
                                                   PikaMetadataLoadFlags *flags,
                                                   PikaProcedureConfig   *config,
                                                   gpointer               run_data);
static PikaValueArray * farbfeld_save             (PikaProcedure         *procedure,
                                                   PikaRunMode            run_mode,
                                                   PikaImage             *image,
                                                   gint                   n_drawables,
                                                   PikaDrawable         **drawables,
                                                   GFile                 *file,
                                                   PikaMetadata          *metadata,
                                                   PikaProcedureConfig   *config,
                                                   gpointer               run_data);

static PikaImage      * load_image                (GFile                 *file,
                                                   GObject               *config,
                                                   PikaRunMode            run_mode,
                                                   GError               **error);
static gboolean         save_image                (GFile                 *file,
                                                   PikaImage             *image,
                                                   PikaDrawable          *drawable,
                                                   GError               **error);


G_DEFINE_TYPE (Farbfeld, farbfeld, PIKA_TYPE_PLUG_IN)

PIKA_MAIN (FARBFELD_TYPE)
DEFINE_STD_SET_I18N


static void
farbfeld_class_init (FarbfeldClass *klass)
{
  PikaPlugInClass *plug_in_class = PIKA_PLUG_IN_CLASS (klass);

  plug_in_class->query_procedures = farbfeld_query_procedures;
  plug_in_class->create_procedure = farbfeld_create_procedure;
  plug_in_class->set_i18n         = STD_SET_I18N;
}

static void
farbfeld_init (Farbfeld *farbfeld)
{
}

static GList *
farbfeld_query_procedures (PikaPlugIn *plug_in)
{
  GList *list = NULL;

  list = g_list_append (list, g_strdup (LOAD_PROC));
  list = g_list_append (list, g_strdup (SAVE_PROC));

  return list;
}

static PikaProcedure *
farbfeld_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,
                                           farbfeld_load, NULL, NULL);

      pika_procedure_set_menu_label (procedure,
                                     N_("Farbfeld"));

      pika_procedure_set_documentation (procedure,
                                        _("Load file in the Farbfeld file "
                                          "format"),
                                        _("Load file in the Farbfeld file "
                                          "format"),
                                        name);
      pika_procedure_set_attribution (procedure,
                                      "Alex S.",
                                      "Alex S.",
                                      "2023");

      pika_file_procedure_set_extensions (PIKA_FILE_PROCEDURE (procedure),
                                          "ff");
      pika_file_procedure_set_magics (PIKA_FILE_PROCEDURE (procedure),
                                      "0,string,farbfeld");
    }
  else if (! strcmp (name, SAVE_PROC))
    {
      procedure = pika_save_procedure_new (plug_in, name,
                                           PIKA_PDB_PROC_TYPE_PLUGIN,
                                           FALSE, farbfeld_save, NULL, NULL);

      pika_procedure_set_image_types (procedure, "*");

      pika_procedure_set_menu_label (procedure, _("Farbfeld"));

      pika_procedure_set_documentation (procedure,
                                        _("Export image in the Farbfeld file "
                                          "format"),
                                        _("Export image in the Farbfeld file "
                                          "format"),
                                        name);
      pika_procedure_set_attribution (procedure,
                                      "Alex S.",
                                      "Alex S.",
                                      "2023");

      pika_file_procedure_set_extensions (PIKA_FILE_PROCEDURE (procedure),
                                          "ff");
    }

  return procedure;
}

static PikaValueArray *
farbfeld_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, G_OBJECT (config), run_mode, &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 PikaValueArray *
farbfeld_save (PikaProcedure        *procedure,
               PikaRunMode           run_mode,
               PikaImage            *image,
               gint                  n_drawables,
               PikaDrawable        **drawables,
               GFile                *file,
               PikaMetadata         *metadata,
               PikaProcedureConfig  *config,
               gpointer              run_data)
{
  PikaPDBStatusType  status = PIKA_PDB_SUCCESS;
  PikaExportReturn   export = PIKA_EXPORT_CANCEL;
  GError            *error  = NULL;

  gegl_init (NULL, NULL);

  switch (run_mode)
    {
    case PIKA_RUN_INTERACTIVE:
    case PIKA_RUN_WITH_LAST_VALS:
      pika_ui_init (PLUG_IN_BINARY);

      export = pika_export_image (&image, &n_drawables, &drawables, "farbfeld",
                                  PIKA_EXPORT_CAN_HANDLE_RGB     |
                                  PIKA_EXPORT_CAN_HANDLE_GRAY    |
                                  PIKA_EXPORT_CAN_HANDLE_INDEXED |
                                  PIKA_EXPORT_CAN_HANDLE_ALPHA);

      if (export == PIKA_EXPORT_CANCEL)
        return pika_procedure_new_return_values (procedure,
                                                 PIKA_PDB_CANCEL,
                                                 NULL);
      break;

    default:
      break;
    }

  if (n_drawables != 1)
    {
      g_set_error (&error, G_FILE_ERROR, 0,
                   _("Farbfeld format does not support multiple layers."));

      return pika_procedure_new_return_values (procedure,
                                               PIKA_PDB_CALLING_ERROR,
                                               error);
    }

  if (! save_image (file, image, drawables[0], &error))
    status = PIKA_PDB_EXECUTION_ERROR;

  if (export == PIKA_EXPORT_EXPORT)
    {
      pika_image_delete (image);
      g_free (drawables);
    }

  return pika_procedure_new_return_values (procedure, status, error);
}

static PikaImage *
load_image (GFile        *file,
            GObject      *config,
            PikaRunMode   run_mode,
            GError      **error)
{
  PikaImage  *image  = NULL;
  PikaLayer  *layer;
  GeglBuffer *buffer;
  guint16    *pixels;
  guchar      magic_number[8];
  guint32     width;
  guint32     height;
  guint32     row_size;
  const Babl *format = babl_format ("R'G'B'A u16");
  FILE       *fp;

  fp = g_fopen (g_file_peek_path (file), "rb");

  if (! fp)
    {
      g_set_error (error, G_FILE_ERROR, g_file_error_from_errno (errno),
                   _("Could not open '%s' for reading: %s"),
                   pika_file_get_utf8_name (file), g_strerror (errno));
      return NULL;
    }

  /* Load the header */
  if (! fread (magic_number, 8, 1, fp)          ||
      ! fread (&width, sizeof (guint32), 1, fp) ||
      ! fread (&height, sizeof (guint32), 1, fp))
    {
      g_set_error (error, G_FILE_ERROR, g_file_error_from_errno (errno),
                   _("Failed to read Farbfeld header"));
      return NULL;
    }

  /* Header information is stored in Big-Endian format */
  width = GUINT32_FROM_BE (width);
  height = GUINT32_FROM_BE (height);
  row_size = width * sizeof (guint16) * 4;

  image = pika_image_new_with_precision (width, height, PIKA_RGB,
                                         PIKA_PRECISION_U16_NON_LINEAR);

  layer = pika_layer_new (image, _("Background"), width, height,
                          PIKA_RGBA_IMAGE, 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));

  for (gint i = 0; i < height; i++)
    {
      pixels = g_malloc (row_size);

      if (! fread (pixels, row_size, 1, fp))
        {
          g_set_error (error, G_FILE_ERROR, g_file_error_from_errno (errno),
                       _("Premature end of Farbfeld pixel data"));
          return NULL;
        }

      /* Pixels are also stored in Big-Endian format */
      for (gint j = 0; j < (width * 4); j++)
        pixels[j] = GUINT16_FROM_BE (pixels[j]);

      gegl_buffer_set (buffer,
                       GEGL_RECTANGLE (0, i, width, 1), 0,
                       format, pixels, GEGL_AUTO_ROWSTRIDE);

      g_free (pixels);
    }

  fclose (fp);
  g_object_unref (buffer);

  return image;
}

static gboolean
save_image (GFile         *file,
            PikaImage     *image,
            PikaDrawable  *drawable,
            GError       **error)
{
  FILE       *fp;
  GeglBuffer *buffer;
  guint16    *pixels;
  const Babl *format = babl_format ("R'G'B'A u16");
  gchar      *magic_number;
  guint32     image_width;
  guint32     image_height;
  guint32     export_width;
  guint32     export_height;

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

  fp = g_fopen (g_file_peek_path (file), "wb");

  if (! fp)
    {
      g_set_error (error, G_FILE_ERROR, g_file_error_from_errno (errno),
                   _("Could not open '%s' for writing: %s"),
                   pika_file_get_utf8_name (file), g_strerror (errno));
      return FALSE;
    }

  buffer = pika_drawable_get_buffer (drawable);

  image_width = gegl_buffer_get_width (buffer);
  image_height = gegl_buffer_get_height (buffer);
  /* Farbfeld values are Big-Endian */
  export_width = GUINT32_TO_BE (image_width);
  export_height = GUINT32_TO_BE (image_height);

  /* Write header */
  magic_number = "farbfeld";
  for (gint i = 0; i < 8; i++)
    fputc (magic_number[i], fp);
  fwrite ((gchar *) &export_width, 1, 4, fp);
  fwrite ((gchar *) &export_height, 1, 4, fp);

  /* Write pixel data */
  for (gint i = 0; i < image_height; i++)
    {
      pixels = g_malloc (image_width * sizeof (guint16) * 4);

      gegl_buffer_get (buffer, GEGL_RECTANGLE (0, i, image_width, 1),
                       1.0, format, pixels,
                       GEGL_AUTO_ROWSTRIDE, GEGL_ABYSS_NONE);

      for (gint j = 0; j < (image_width * 4); j++)
        {
          pixels[j] = GUINT16_TO_BE (pixels[j]);
          fwrite ((gchar *) &pixels[j], 1, 2, fp);
        }

      g_free (pixels);
      pika_progress_update (i / (gdouble) image_height);
    }

  pika_progress_update (1.0);

  fclose (fp);

  if (buffer)
    g_object_unref (buffer);

  return TRUE;
}
