1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521
|
/* The GIMP -- an 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.
*/
#define VERSIO 1.03
static char dversio[] = "v1.03 22-May-00";
#include "config.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <gtk/gtk.h>
#include <libgimp/gimp.h>
#include <libgimp/gimpui.h>
#include "libgimp/stdplugins-intl.h"
#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
};
static guchar redmap[256], greenmap[256], bluemap[256];
/* 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);
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 get_mapping (GimpRGB *src_col1,
GimpRGB *src_col2,
GimpRGB *dst_col1,
GimpRGB *dst_col2,
gint32 map_mode,
guchar *redmap,
guchar *greenmap,
guchar *bluemap);
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 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 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 ("plug_in_color_adjust",
"Adjust color range given by foreground/background "
"color to black/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 _FG-BG"),
"RGB*",
GIMP_PLUGIN,
G_N_ELEMENTS (adjust_args), 0,
adjust_args, NULL);
gimp_plugin_menu_register ("plug_in_color_adjust",
"<Image>/Filters/Colors/Map");
gimp_install_procedure ("plug_in_color_map",
"Map color range specified by two colors"
"to color range specified by two other color.",
"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 ("plug_in_color_map",
"<Image>/Filters/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, "plug_in_color_adjust") == 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/Background..."));
color_mapping (drawable);
break;
}
if (strcmp (name, "plug_in_color_map") == 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 redmap[256], greenmap[256], bluemap[256];
gint width, height, bpp;
gboolean has_alpha;
guchar *src, *dest, *s, *d;
gint j;
get_mapping (plvals.colors,
plvals.colors + 1,
plvals.colors + 2,
plvals.colors + 3,
plvals.map_mode,
redmap, greenmap, bluemap);
gimp_preview_get_size (preview, &width, &height);
bpp = gimp_drawable_bpp (drawable->drawable_id);
src = gimp_drawable_get_thumbnail_data (drawable->drawable_id,
&width, &height, &bpp);
has_alpha = gimp_drawable_has_alpha (drawable->drawable_id);
j = width * height;
dest = g_new (guchar, j * bpp);
s = src; d = dest;
while (j-- > 0)
{
*(d++) = redmap[*(s++)];
*(d++) = greenmap[*(s++)];
*(d++) = bluemap[*(s++)];
if (has_alpha)
*(d++) = *(s++);
}
gimp_preview_draw_buffer (preview, dest, width * bpp);
g_free (src);
g_free (dest);
}
static gboolean
mapcolor_dialog (GimpDrawable *drawable)
{
GtkWidget *dialog;
GtkWidget *main_vbox;
GtkWidget *preview;
GtkWidget *frame;
GtkWidget *table;
gint j;
gboolean run;
gimp_ui_init ("mapcolor", TRUE);
dialog = gimp_dialog_new (_("Map Color Range"), "mapcolor",
NULL, 0,
gimp_standard_help_func, "plug-in-color-map",
GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
GTK_STOCK_OK, GTK_RESPONSE_OK,
NULL);
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_aspect_preview_new (drawable, NULL);
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
get_mapping (GimpRGB *src_col1,
GimpRGB *src_col2,
GimpRGB *dst_col1,
GimpRGB *dst_col2,
gint32 map_mode,
guchar *redmap,
guchar *greenmap,
guchar *bluemap)
{
guchar src1[3];
guchar src2[3];
guchar dst1[3];
guchar dst2[3];
gint rgb, i, a, as, b, bs;
guchar *colormap[3];
/* Currently we always do a linear mapping */
colormap[0] = redmap;
colormap[1] = greenmap;
colormap[2] = bluemap;
gimp_rgb_get_uchar (src_col1, src1, src1 + 1, src1 + 2);
gimp_rgb_get_uchar (src_col2, src2, src2 + 1, src2 + 2);
gimp_rgb_get_uchar (dst_col1, dst1, dst1 + 1, dst1 + 2);
gimp_rgb_get_uchar (dst_col2, dst2, dst2 + 1, dst2 + 2);
switch (map_mode)
{
case 0:
default:
for (rgb = 0; rgb < 3; rgb++)
{
a = src1[rgb]; as = dst1[rgb];
b = src2[rgb]; bs = dst2[rgb];
if (b == a)
b = a + 1;
for (i = 0; i < 256; i++)
{
gint j = ((i - a) * (bs - as)) / (b - a) + as;
colormap[rgb][i] = CLAMP0255(j);
}
}
break;
}
}
static void
mapcolor_func (const guchar *src,
guchar *dest,
gint bpp,
gpointer data)
{
dest[0] = redmap[src[0]];
dest[1] = greenmap[src[1]];
dest[2] = bluemap[src[2]];
if (bpp > 3)
dest[3] = src[3];
}
static void
color_mapping (GimpDrawable *drawable)
{
if (gimp_rgb_distance (&plvals.colors[0], &plvals.colors[1]) < 0.0001)
return;
if (!gimp_drawable_is_rgb (drawable->drawable_id))
{
g_message (_("Cannot operate on gray or indexed color images."));
return;
}
gimp_tile_cache_ntiles (2 * (drawable->width / gimp_tile_width () + 1));
get_mapping (plvals.colors,
plvals.colors + 1,
plvals.colors + 2,
plvals.colors + 3,
plvals.map_mode,
redmap, greenmap, bluemap);
gimp_rgn_iterate2 (drawable, 0 /* unused */, mapcolor_func, NULL);
}
|