/* 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 .
 */
#include "config.h"
#include 
#include 
#include 
#include "libpikabase/pikabase.h"
#include "libpikacolor/pikacolor.h"
#include "core-types.h"
#include "pikapattern.h"
#include "pikapattern-header.h"
#include "pikapattern-load.h"
#include "pikatempbuf.h"
#include "pika-intl.h"
GList *
pika_pattern_load (PikaContext   *context,
                   GFile         *file,
                   GInputStream  *input,
                   GError       **error)
{
  PikaPattern       *pattern = NULL;
  const Babl        *format  = NULL;
  PikaPatternHeader  header;
  gsize              size;
  gsize              bytes_read;
  gsize              bn_size;
  gchar             *name = NULL;
  g_return_val_if_fail (G_IS_FILE (file), NULL);
  g_return_val_if_fail (G_IS_INPUT_STREAM (input), NULL);
  g_return_val_if_fail (error == NULL || *error == NULL, NULL);
  /*  read the size  */
  if (! g_input_stream_read_all (input, &header, sizeof (header),
                                 &bytes_read, NULL, error) ||
      bytes_read != sizeof (header))
    {
      g_prefix_error (error, _("File appears truncated: "));
      goto error;
    }
  /*  rearrange the bytes in each unsigned int  */
  header.header_size  = g_ntohl (header.header_size);
  header.version      = g_ntohl (header.version);
  header.width        = g_ntohl (header.width);
  header.height       = g_ntohl (header.height);
  header.bytes        = g_ntohl (header.bytes);
  header.magic_number = g_ntohl (header.magic_number);
  /*  Check for correct file format */
  if (header.magic_number != PIKA_PATTERN_MAGIC ||
      header.version      != 1                  ||
      header.header_size  <= sizeof (header))
    {
      g_set_error (error, PIKA_DATA_ERROR, PIKA_DATA_ERROR_READ,
                   _("Unknown pattern format version %d."),
                   header.version);
      goto error;
    }
  /*  Check for supported bit depths  */
  if (header.bytes < 1 || header.bytes > 4)
    {
      g_set_error (error, PIKA_DATA_ERROR, PIKA_DATA_ERROR_READ,
                   _("Unsupported pattern depth %d.\n"
                     "PIKA Patterns must be GRAY or RGB."),
                   header.bytes);
      goto error;
    }
  /*  Validate dimensions  */
  if ((header.width  == 0) || (header.width  > PIKA_PATTERN_MAX_SIZE) ||
      (header.height == 0) || (header.height > PIKA_PATTERN_MAX_SIZE) ||
      (G_MAXSIZE / header.width / header.height / header.bytes < 1))
    {
      g_set_error (error, PIKA_DATA_ERROR, PIKA_DATA_ERROR_READ,
                   _("Invalid header data in '%s': width=%lu (maximum %lu), "
                     "height=%lu (maximum %lu), bytes=%lu"),
                   pika_file_get_utf8_name (file),
                   (gulong) header.width,  (gulong) PIKA_PATTERN_MAX_SIZE,
                   (gulong) header.height, (gulong) PIKA_PATTERN_MAX_SIZE,
                   (gulong) header.bytes);
      goto error;
    }
  /*  Read in the pattern name  */
  if ((bn_size = (header.header_size - sizeof (header))))
    {
      gchar *utf8;
      if (bn_size > PIKA_PATTERN_MAX_NAME)
        {
          g_set_error (error, PIKA_DATA_ERROR, PIKA_DATA_ERROR_READ,
                       _("Invalid header data in '%s': "
                         "Pattern name is too long: %lu"),
                       pika_file_get_utf8_name (file),
                       (gulong) bn_size);
          goto error;
        }
      name = g_new0 (gchar, bn_size + 1);
      if (! g_input_stream_read_all (input, name, bn_size,
                                     &bytes_read, NULL, error) ||
          bytes_read != bn_size)
        {
          g_prefix_error (error, _("File appears truncated."));
          g_free (name);
          goto error;
        }
      utf8 = pika_any_to_utf8 (name, bn_size - 1,
                               _("Invalid UTF-8 string in pattern file '%s'."),
                               pika_file_get_utf8_name (file));
      g_free (name);
      name = utf8;
    }
  if (! name)
    name = g_strdup (_("Unnamed"));
  pattern = g_object_new (PIKA_TYPE_PATTERN,
                          "name",      name,
                          "mime-type", "image/x-pika-pat",
                          NULL);
  g_free (name);
  switch (header.bytes)
    {
    case 1: format = babl_format ("Y' u8");      break;
    case 2: format = babl_format ("Y'A u8");     break;
    case 3: format = babl_format ("R'G'B' u8");  break;
    case 4: format = babl_format ("R'G'B'A u8"); break;
    }
  pattern->mask = pika_temp_buf_new (header.width, header.height, format);
  size = (gsize) header.width * header.height * header.bytes;
  if (! g_input_stream_read_all (input,
                                 pika_temp_buf_get_data (pattern->mask), size,
                                 &bytes_read, NULL, error) ||
      bytes_read != size)
    {
      g_prefix_error (error, _("File appears truncated."));
      goto error;
    }
  return g_list_prepend (NULL, pattern);
 error:
  if (pattern)
    g_object_unref (pattern);
  g_prefix_error (error, _("Fatal parse error in pattern file: "));
  return NULL;
}
GList *
pika_pattern_load_pixbuf (PikaContext   *context,
                          GFile         *file,
                          GInputStream  *input,
                          GError       **error)
{
  PikaPattern *pattern;
  GdkPixbuf   *pixbuf;
  gchar       *name;
  g_return_val_if_fail (G_IS_FILE (file), NULL);
  g_return_val_if_fail (G_IS_INPUT_STREAM (input), NULL);
  g_return_val_if_fail (error == NULL || *error == NULL, NULL);
  pixbuf = gdk_pixbuf_new_from_stream (input, NULL, error);
  if (! pixbuf)
    return NULL;
  name = g_strdup (gdk_pixbuf_get_option (pixbuf, "tEXt::Title"));
  if (! name)
    name = g_strdup (gdk_pixbuf_get_option (pixbuf, "tEXt::Comment"));
  if (! name)
    name = g_path_get_basename (pika_file_get_utf8_name (file));
  pattern = g_object_new (PIKA_TYPE_PATTERN,
                          "name",      name,
                          "mime-type", NULL, /* FIXME!! */
                          NULL);
  g_free (name);
  pattern->mask = pika_temp_buf_new_from_pixbuf (pixbuf, NULL);
  g_object_unref (pixbuf);
  return g_list_prepend (NULL, pattern);
}