/* bmp.c                                          */
/* Version 0.52                                   */
/* This is a File input and output filter for the */
/* Pika. It loads and saves images in windows(TM) */
/* bitmap format.                                 */
/* Some Parts that deal with the interaction with */
/* PIKA are taken from the GIF plugin by          */
/* Peter Mattis & Spencer Kimball and from the    */
/* PCX plugin by Francisco Bustamante.            */
/*                                                */
/* Alexander.Schulz@stud.uni-karlsruhe.de         */

/* Changes:   28.11.1997 Noninteractive operation */
/*            16.03.1998 Endian-independent!!     */
/*            21.03.1998 Little Bug-fix           */
/*            06.04.1998 Bugfix in Padding        */
/*            11.04.1998 Arch. cleanup (-Wall)    */
/*                       Parses gtkrc             */
/*            14.04.1998 Another Bug in Padding   */
/*            28.04.1998 RLE-Encoding rewritten   */
/*            29.10.1998 Changes by Tor Lillqvist */
/*                       <tml@iki.fi> to support  */
/*                       16 and 32 bit images     */
/*            28.11.1998 Bug in RLE-read-padding  */
/*                       fixed.                   */
/*            19.12.1999 Resolution support added */
/*            06.05.2000 Overhaul for 16&24-bit   */
/*                       plus better OS/2 code    */
/*                       by njl195@zepler.org.uk  */
/*            29.06.2006 Full support for 16/32   */
/*                       bits bitmaps and support */
/*                       for alpha channel        */
/*                       by p.filiciak@zax.pl     */

/*
 * PIKA - Photo and Image Kooker Application
 * 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 <stdlib.h>
#include <string.h>

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

#include "bmp.h"
#include "bmp-load.h"
#include "bmp-save.h"

#include "libpika/stdplugins-intl.h"


typedef struct _Bmp      Bmp;
typedef struct _BmpClass BmpClass;

struct _Bmp
{
  PikaPlugIn      parent_instance;
};

struct _BmpClass
{
  PikaPlugInClass parent_class;
};


#define BMP_TYPE  (bmp_get_type ())
#define BMP(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), BMP_TYPE, Bmp))

GType                   bmp_get_type         (void) G_GNUC_CONST;

static GList          * bmp_query_procedures (PikaPlugIn            *plug_in);
static PikaProcedure  * bmp_create_procedure (PikaPlugIn            *plug_in,
                                              const gchar           *name);

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



G_DEFINE_TYPE (Bmp, bmp, PIKA_TYPE_PLUG_IN)

PIKA_MAIN (BMP_TYPE)
DEFINE_STD_SET_I18N


static void
bmp_class_init (BmpClass *klass)
{
  PikaPlugInClass *plug_in_class = PIKA_PLUG_IN_CLASS (klass);

  plug_in_class->query_procedures = bmp_query_procedures;
  plug_in_class->create_procedure = bmp_create_procedure;
  plug_in_class->set_i18n         = STD_SET_I18N;
}

static void
bmp_init (Bmp *bmp)
{
}

static GList *
bmp_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 *
bmp_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,
                                           bmp_load, NULL, NULL);

      pika_procedure_set_menu_label (procedure, _("Windows BMP image"));

      pika_procedure_set_documentation (procedure,
                                        _("Loads files of Windows BMP file format"),
                                        _("Loads files of Windows BMP file format"),
                                        name);
      pika_procedure_set_attribution (procedure,
                                      "Alexander Schulz",
                                      "Alexander Schulz",
                                      "1997");

      pika_file_procedure_set_mime_types (PIKA_FILE_PROCEDURE (procedure),
                                          "image/bmp");
      pika_file_procedure_set_extensions (PIKA_FILE_PROCEDURE (procedure),
                                          "bmp");
      pika_file_procedure_set_magics (PIKA_FILE_PROCEDURE (procedure),
                                      "0,string,BM");
    }
  else if (! strcmp (name, SAVE_PROC))
    {
      procedure = pika_save_procedure_new (plug_in, name,
                                           PIKA_PDB_PROC_TYPE_PLUGIN,
                                           FALSE, bmp_save, NULL, NULL);

      pika_procedure_set_image_types (procedure, "INDEXED, GRAY, RGB*");

      pika_procedure_set_menu_label (procedure, _("Windows BMP image"));
      pika_file_procedure_set_format_name (PIKA_FILE_PROCEDURE (procedure),
                                           _("BMP"));

      pika_procedure_set_documentation (procedure,
                                        _("Saves files in Windows BMP file format"),
                                        _("Saves files in Windows BMP file format"),
                                        name);
      pika_procedure_set_attribution (procedure,
                                      "Alexander Schulz",
                                      "Alexander Schulz",
                                      "1997");

      pika_file_procedure_set_mime_types (PIKA_FILE_PROCEDURE (procedure),
                                          "image/bmp");
      pika_file_procedure_set_extensions (PIKA_FILE_PROCEDURE (procedure),
                                          "bmp");

      PIKA_PROC_ARG_BOOLEAN (procedure, "use-rle",
                             _("Ru_n-Length Encoded"),
                             _("Use run-length-encoding compression "
                               "(only valid for 4 and 8-bit indexed images)"),
                             FALSE,
                             G_PARAM_READWRITE);

      PIKA_PROC_ARG_BOOLEAN (procedure, "write-color-space",
                             _("_Write color space information"),
                             _("Whether or not to write BITMAPV5HEADER "
                               "color space data"),
                             TRUE,
                             G_PARAM_READWRITE);

      PIKA_PROC_ARG_INT (procedure, "rgb-format",
                         _("R_GB format"),
                         _("Export format for RGB images "
                           "(0=RGB_565, 1=RGBA_5551, 2=RGB_555, 3=RGB_888, "
                           "4=RGBA_8888, 5=RGBX_8888)"),
                         0, 5, 3,
                         G_PARAM_READWRITE);
    }

  return procedure;
}

static PikaValueArray *
bmp_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, &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 *
bmp_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, "BMP",
                                  PIKA_EXPORT_CAN_HANDLE_RGB   |
                                  PIKA_EXPORT_CAN_HANDLE_GRAY  |
                                  PIKA_EXPORT_CAN_HANDLE_ALPHA |
                                  PIKA_EXPORT_CAN_HANDLE_INDEXED);

      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,
                   _("BMP format does not support multiple layers."));

      return pika_procedure_new_return_values (procedure,
                                               PIKA_PDB_CALLING_ERROR,
                                               error);
    }

  status = save_image (file, image, drawables[0], run_mode,
                       procedure, G_OBJECT (config),
                       &error);

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

  return pika_procedure_new_return_values (procedure, status, error);
}
