/* GIMP - The GNU Image Manipulation Program
 * Copyright (C) 1995 Spencer Kimball and Peter Mattis
 * mapcolor plugin
 * Copyright (C) 1998 Peter Kirchgessner
 * email: peter@kirchgessner.net, WWW: http://www.kirchgessner.net)
 *
 * 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 2 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, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 *
 */

/* Event history:
 * V 1.00, PK, 26-Oct-98: Creation.
 * V 1.01, PK, 21-Nov-99: Fix problem with working on layered images
 *                        Internationalization
 * V 1.02, PK, 19-Mar-00: Better explaining text/dialogue
 *                        Preview added
 *                        Fix problem with divide by zero
 * V 1.03,neo, 22-May-00: Fixed divide by zero in preview code.
 * V 2.00,eric lamarque, 30-Oct-08: Rewrite mapcolor using levels tools
 */

#include "config.h"

#include <string.h>

#include <libgimp/gimp.h>
#include <libgimp/gimpui.h>

#include "libgimp/stdplugins-intl.h"


static const gchar dversio[] = "v2.00  30-Oct-08";


#define COLOR_MAP_PROC    "plug-in-color-map"
#define COLOR_ADJUST_PROC "plug-in-color-adjust"
#define PLUG_IN_BINARY    "mapcolor"
#define PRV_WIDTH         50
#define PRV_HEIGHT        20

typedef struct
{
  GimpRGB  colors[4];
  gint32   map_mode;
  gboolean preview;
} PluginValues;

PluginValues plvals =
{
  {
    { 0.0, 0.0, 0.0, 1.0 },
    { 1.0, 1.0, 1.0, 1.0 },
    { 0.0, 0.0, 0.0, 1.0 },
    { 1.0, 1.0, 1.0, 1.0 },
  },
  0
};

/* Declare some local functions.
 */
static void   query (void);
static void   run   (const gchar      *name,
                     gint              nparams,
                     const GimpParam  *param,
                     gint             *nreturn_vals,
                     GimpParam       **return_vals);

const GimpPlugInInfo PLUG_IN_INFO =
{
  NULL,  /* init_proc  */
  NULL,  /* quit_proc  */
  query, /* query_proc */
  run,   /* run_proc   */
};

static void      update_img_preview (GimpDrawable *drawable,
                                     GimpPreview  *preview);

static gboolean  mapcolor_dialog    (GimpDrawable *drawable);
static void      add_color_button   (gint          csel_index,
                                     gint          left,
                                     gint          top,
                                     GtkWidget    *table,
                                     GtkWidget    *preview);

static void      color_mapping      (GimpDrawable *drawable);

static gchar *csel_title[4] =
{
  N_("First Source Color"),
  N_("Second Source Color"),
  N_("First Destination Color"),
  N_("Second Destination Color")
};

MAIN ()

static void
query (void)

{
  static const GimpParamDef adjust_args[] =
  {
    { GIMP_PDB_INT32,    "run-mode", "Interactive, non-interactive" },
    { GIMP_PDB_IMAGE,    "image",    "Input image (not used)" },
    { GIMP_PDB_DRAWABLE, "drawable", "Input drawable to adjust" }
  };

  static const GimpParamDef map_args[] =
  {
    { GIMP_PDB_INT32,    "run-mode",   "Interactive, non-interactive" },
    { GIMP_PDB_IMAGE,    "image",      "Input image (not used)" },
    { GIMP_PDB_DRAWABLE, "drawable",   "Input drawable where colors are to map" },
    { GIMP_PDB_COLOR,    "srccolor-1", "First source color" },
    { GIMP_PDB_COLOR,    "srccolor-2", "Second source color" },
    { GIMP_PDB_COLOR,    "dstcolor-1", "First destination color" },
    { GIMP_PDB_COLOR,    "dstcolor-2", "Second destination color" },
    { GIMP_PDB_INT32,    "map-mode",   "Mapping mode (0: linear, others reserved)" }
  };

  gimp_install_procedure (COLOR_ADJUST_PROC,
                          N_("Map colors sending foreground to black, background to white"),
                          "The current foreground color is mapped to black "
                          "(black point), the current background color is "
                          "mapped to white (white point). Intermediate "
                          "colors are interpolated",
                          "Peter Kirchgessner",
                          "Peter Kirchgessner",
                          dversio,
                          N_("Adjust _Foreground & Background"),
                          "RGB*",
                          GIMP_PLUGIN,
                          G_N_ELEMENTS (adjust_args), 0,
                          adjust_args, NULL);

  gimp_plugin_menu_register (COLOR_ADJUST_PROC, "<Image>/Colors/Map");

  gimp_install_procedure (COLOR_MAP_PROC,
                          N_("Map color range specified by two colors "
                             "to another range"),
                          "Map color range specified by two colors"
                          "to color range specified by two other color. "
                          "Intermediate colors are interpolated.",
                          "Peter Kirchgessner",
                          "Peter Kirchgessner",
                          dversio,
                          N_("Color Range _Mapping..."),
                          "RGB*",
                          GIMP_PLUGIN,
                          G_N_ELEMENTS (map_args), 0,
                          map_args, NULL);

  gimp_plugin_menu_register (COLOR_MAP_PROC, "<Image>/Colors/Map");
}


static void
run (const gchar      *name,
     gint              nparams,
     const GimpParam  *param,
     gint             *nreturn_vals,
     GimpParam       **return_vals)

{
  static GimpParam   values[1];
  GimpRunMode        run_mode;
  GimpDrawable      *drawable = NULL;
  GimpPDBStatusType  status = GIMP_PDB_SUCCESS;
  gint               j;

  INIT_I18N ();

  run_mode = param[0].data.d_int32;

  *nreturn_vals = 1;
  *return_vals  = values;

  values[0].type          = GIMP_PDB_STATUS;
  values[0].data.d_status = status;

  while (status == GIMP_PDB_SUCCESS)
    {
      if (nparams < 3)
        {
          status = GIMP_PDB_CALLING_ERROR;
          break;
        }

      /* Make sure the drawable is RGB color */
      drawable = gimp_drawable_get (param[2].data.d_drawable);
      if (!gimp_drawable_is_rgb (drawable->drawable_id))
        {
          g_message (_("Cannot operate on gray or indexed color images."));
          status = GIMP_PDB_EXECUTION_ERROR;
          break;
        }

      if (strcmp (name, COLOR_ADJUST_PROC) == 0)
        {
          if (nparams != 3)  /* Make sure all the arguments are there */
            {
              status = GIMP_PDB_CALLING_ERROR;
              break;
            }

          gimp_context_get_foreground (plvals.colors);
          gimp_context_get_background (plvals.colors + 1);

          gimp_rgb_set (plvals.colors + 2, 0.0, 0.0, 0.0);
          gimp_rgb_set (plvals.colors + 3, 1.0, 1.0, 1.0);

          plvals.map_mode = 0;

          gimp_progress_init (_("Adjusting foreground and background colors"));

          color_mapping (drawable);
          break;
        }

      if (strcmp (name, COLOR_MAP_PROC) == 0)
        {
          if (run_mode == GIMP_RUN_NONINTERACTIVE)
            {
              if (nparams != 8)  /* Make sure all the arguments are there */
                {
                  status = GIMP_PDB_CALLING_ERROR;
                  break;
                }

              for (j = 0; j < 4; j++)
                {
                  plvals.colors[j] = param[3+j].data.d_color;
                }
              plvals.map_mode = param[7].data.d_int32;
            }
          else if (run_mode == GIMP_RUN_INTERACTIVE)
            {
              gimp_get_data (name, &plvals);

              gimp_context_get_foreground (plvals.colors);
              gimp_context_get_background (plvals.colors + 1);

              if (!mapcolor_dialog (drawable))
                break;
            }
          else if (run_mode == GIMP_RUN_WITH_LAST_VALS)
            {
              gimp_get_data (name, &plvals);
            }
          else
            {
              status = GIMP_PDB_CALLING_ERROR;
              break;
            }

          gimp_progress_init (_("Mapping colors"));

          color_mapping (drawable);

          if (run_mode == GIMP_RUN_INTERACTIVE)
            gimp_set_data (name, &plvals, sizeof (plvals));

          break;
        }

      status = GIMP_PDB_EXECUTION_ERROR;
    }

  if ((status == GIMP_PDB_SUCCESS) && (run_mode != GIMP_RUN_NONINTERACTIVE))
    gimp_displays_flush ();

  if (drawable != NULL)
    gimp_drawable_detach (drawable);

  values[0].data.d_status = status;
}

static void
update_img_preview (GimpDrawable *drawable,
                    GimpPreview  *preview)
{
  guchar  src1[3];
  guchar  src2[3];
  guchar  dst1[3];
  guchar  dst2[3];

  gint            width, height, bpp;
  guchar         *raw_pixels;
  GimpDrawable   *preview_drawable;
  GimpPixelRgn    pixel_rgn;

  gint32  image_id;
  gint32  preview_drawable_id;
  gint32  layer_type = GIMP_RGB_IMAGE;

  raw_pixels = gimp_zoom_preview_get_source (GIMP_ZOOM_PREVIEW (preview),
                                             &width, &height, &bpp);

  if (gimp_drawable_has_alpha (drawable->drawable_id))
    layer_type = GIMP_RGBA_IMAGE;

  /* build new image for preview */

  image_id = gimp_image_new (width, height, GIMP_RGB_IMAGE);
  preview_drawable_id = gimp_layer_new (image_id, "Map Color Preview",
                                        width, height, layer_type, 100,
                                        GIMP_NORMAL_MODE);
  gimp_image_add_layer (image_id, preview_drawable_id, -1);

  preview_drawable = gimp_drawable_get (preview_drawable_id);
  gimp_pixel_rgn_init (&pixel_rgn, preview_drawable,
                       0, 0, width, height,
                       TRUE, FALSE);

  gimp_pixel_rgn_set_rect (&pixel_rgn, raw_pixels, 0, 0, width, height);

  /* get source range */
  gimp_rgb_get_uchar (plvals.colors, src1, src1 + 1, src1 + 2);
  gimp_rgb_get_uchar (plvals.colors + 1, src2, src2 + 1, src2 + 2);
  /* get destination range */
  gimp_rgb_get_uchar (plvals.colors + 2, dst1, dst1 + 1, dst1 + 2);
  gimp_rgb_get_uchar (plvals.colors + 3, dst2, dst2 + 1, dst2 + 2);

  /* apply levels on new image */
  gimp_levels (preview_drawable_id, GIMP_HISTOGRAM_RED, src1[0], src2[0], 1.0,
               dst1[0], dst2[0]);
  gimp_levels (preview_drawable_id, GIMP_HISTOGRAM_GREEN, src1[1], src2[1], 1.0,
               dst1[1], dst2[1]);
  gimp_levels (preview_drawable_id, GIMP_HISTOGRAM_BLUE, src1[2], src2[2], 1.0,
               dst1[2], dst2[2]);

  /* set the result to preview */
  gimp_pixel_rgn_get_rect (&pixel_rgn, raw_pixels, 0, 0, width, height);

  gimp_preview_draw_buffer (preview, raw_pixels, width * bpp);

  gimp_image_delete (image_id);
  g_free (raw_pixels);
}

static gboolean
mapcolor_dialog (GimpDrawable *drawable)
{
  GtkWidget *dialog;
  GtkWidget *main_vbox;
  GtkWidget *preview;
  GtkWidget *frame;
  GtkWidget *table;
  gint       j;
  gboolean   run;

  gimp_ui_init (PLUG_IN_BINARY, TRUE);

  dialog = gimp_dialog_new (_("Map Color Range"), PLUG_IN_BINARY,
                            NULL, 0,
                            gimp_standard_help_func, COLOR_MAP_PROC,

                            GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
                            GTK_STOCK_OK,     GTK_RESPONSE_OK,

                            NULL);

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

  gimp_window_set_transient (GTK_WINDOW (dialog));

  main_vbox = gtk_vbox_new (FALSE, 12);
  gtk_container_set_border_width (GTK_CONTAINER (main_vbox), 12);
  gtk_container_add (GTK_CONTAINER (GTK_DIALOG (dialog)->vbox), main_vbox);
  gtk_widget_show (main_vbox);

  preview = gimp_zoom_preview_new (drawable);
  gtk_box_pack_start_defaults (GTK_BOX (main_vbox), preview);
  gtk_widget_show (preview);
  g_signal_connect_swapped (preview, "invalidated",
                            G_CALLBACK (update_img_preview),
                            drawable);

  for (j = 0; j < 2; j++)
    {
      frame = gimp_frame_new ((j == 0) ?
                              _("Source Color Range") :
                              _("Destination Color Range"));
      gtk_box_pack_start (GTK_BOX (main_vbox), frame, FALSE, FALSE, 0);
      gtk_widget_show (frame);

      /* The table keeps the color selections */
      table = gtk_table_new (1, 4, FALSE);
      gtk_table_set_col_spacings (GTK_TABLE (table), 6);
      gtk_container_add (GTK_CONTAINER (frame), table);
      gtk_widget_show (table);

      add_color_button (j * 2, 0, 0, table, preview);
      add_color_button (j * 2 + 1, 2, 0, table, preview);
    }

  gtk_widget_show (dialog);

  run = (gimp_dialog_run (GIMP_DIALOG (dialog)) == GTK_RESPONSE_OK);

  if (run)
    plvals.map_mode = 0;  /* Currently always linear mapping */

  gtk_widget_destroy (dialog);

  return run;
}

static void
add_color_button (gint       csel_index,
                  gint       left,
                  gint       top,
                  GtkWidget *table,
                  GtkWidget *preview)
{
  GtkWidget *button;

  button = gimp_color_button_new (gettext (csel_title[csel_index]),
                                  PRV_WIDTH, PRV_HEIGHT,
                                  &plvals.colors[csel_index],
                                  GIMP_COLOR_AREA_FLAT);
  gimp_table_attach_aligned (GTK_TABLE (table), left + 1, top,
                             (left == 0) ? _("From:") : _("To:"),
                             0.0, 0.5,
                             button, 1, TRUE);

  g_signal_connect (button, "color-changed",
                    G_CALLBACK (gimp_color_button_get_color),
                    &plvals.colors[csel_index]);
  g_signal_connect_swapped (button, "color-changed",
                            G_CALLBACK (gimp_preview_invalidate),
                            preview);
}

static void
color_mapping (GimpDrawable *drawable)
{
  guchar  src1[3];
  guchar  src2[3];
  guchar  dst1[3];
  guchar  dst2[3];

  gint32  image_id;

  if (!gimp_drawable_is_rgb (drawable->drawable_id))
    {
      g_message (_("Cannot operate on gray or indexed color images."));
      return;
    }

  /* get source range */
  gimp_rgb_get_uchar (plvals.colors, src1, src1 + 1, src1 + 2);
  gimp_rgb_get_uchar (plvals.colors + 1, src2, src2 + 1, src2 + 2);
  /* get destination range */
  gimp_rgb_get_uchar (plvals.colors + 2, dst1, dst1 + 1, dst1 + 2);
  gimp_rgb_get_uchar (plvals.colors + 3, dst2, dst2 + 1, dst2 + 2);

  image_id = gimp_drawable_get_image (drawable->drawable_id);
  
  gimp_image_undo_group_start (image_id);

  gimp_levels (drawable->drawable_id, GIMP_HISTOGRAM_RED, src1[0], src2[0], 1.0,
               dst1[0], dst2[0]);
  gimp_levels (drawable->drawable_id, GIMP_HISTOGRAM_GREEN, src1[1], src2[1], 1.0,
               dst1[1], dst2[1]);
  gimp_levels (drawable->drawable_id, GIMP_HISTOGRAM_BLUE, src1[2], src2[2], 1.0,
               dst1[2], dst2[2]);

  gimp_image_undo_group_end (image_id);
}
