gimp/plug-ins/common/gradient-map.c

345 lines
10 KiB
C
Raw Normal View History

/* GIMP - The GNU Image Manipulation Program
1997-11-25 06:05:25 +08:00
* Copyright (C) 1995 Spencer Kimball and Peter Mattis
*
* Gradient Map plug-in
* Copyright (C) 1997 Eiichi Takamori <taka@ma1.seikyou.ne.jp>
*
* This program is free software: you can redistribute it and/or modify
1997-11-25 06:05:25 +08:00
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
1997-11-25 06:05:25 +08:00
* (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 <http://www.gnu.org/licenses/>.
1997-11-25 06:05:25 +08:00
*/
#include "config.h"
#include <string.h>
#include <libgimp/gimp.h>
#include "libgimp/stdplugins-intl.h"
1997-11-25 06:05:25 +08:00
1997-11-25 06:05:25 +08:00
/* Some useful macros */
#define GRADMAP_PROC "plug-in-gradmap"
#define PALETTEMAP_PROC "plug-in-palettemap"
Renamed tons of plug-ins to make more sense and to be consistent: 2008-03-24 Michael Natterer <mitch@gimp.org> Renamed tons of plug-ins to make more sense and to be consistent: * plug-ins/common/AlienMap2.c -> alien-map.c * plug-ins/common/CEL.c -> cel.c * plug-ins/common/CML_explorer.c -> cml-explorer.c * plug-ins/common/align_layers.c -> align-layers.c * plug-ins/common/animationplay.c -> animation-play.c * plug-ins/common/animoptimize.c -> animation-optimize.c * plug-ins/common/apply_lens.c -> lens-apply.c * plug-ins/common/autocrop.c -> crop-auto.c * plug-ins/common/autostretch_hsv.c -> contrast-stretch-hsv.c * plug-ins/common/borderaverage.c -> border-average.c * plug-ins/common/bumpmap.c -> bump-map.c * plug-ins/common/c_astretch.c -> contrast-stretch.c * plug-ins/common/ccanalyze.c -> color-cube-analyze.c * plug-ins/common/channel_mixer.c -> channel-mixer.c * plug-ins/common/color_enhance.c -> color-enhance.c * plug-ins/common/colortoalpha.c -> color-to-alpha.c * plug-ins/common/convmatrix.c -> convolution-matrix.c * plug-ins/common/curve_bend.c -> curve-bend.c * plug-ins/common/depthmerge.c -> depth-merge.c * plug-ins/common/dog.c -> edge-dog.c * plug-ins/common/exchange.c -> color-exchange.c * plug-ins/common/flarefx.c -> lens-flare.c * plug-ins/common/fp.c -> filter-pack.c * plug-ins/common/fractaltrace.c -> fractal-trace.c * plug-ins/common/gauss.c -> blur-gauss.c * plug-ins/common/gee_zoom.c -> gee-zoom.c * plug-ins/common/glasstile.c -> tile-glass.c * plug-ins/common/gqbist.c -> qbist.c * plug-ins/common/gradmap.c -> gradient-map.c * plug-ins/common/laplace.c -> edge-laplace.c * plug-ins/common/lens.c -> lens-distortion.c * plug-ins/common/lic.c -> van-gogh-lic.c * plug-ins/common/max_rgb.c -> max-rgb.c * plug-ins/common/mblur.c -> blur-motion.c * plug-ins/common/nlfilt.c -> nl-filter.c * plug-ins/common/noisify.c -> noise-rgb.c * plug-ins/common/normalize.c -> contrast-normalize.c * plug-ins/common/papertile.c -> tile-paper.c * plug-ins/common/polar.c -> polar-coords.c * plug-ins/common/randomize.c -> noise-randomize.c * plug-ins/common/redeye.c -> red-eye-removal.c * plug-ins/common/retinex.c -> contrast-retinex.c * plug-ins/common/sample_colorize.c -> sample-colorize.c * plug-ins/common/scatter_hsv.c -> noise-hsv.c * plug-ins/common/sel_gauss.c -> blur-gauss-selective.c * plug-ins/common/semiflatten.c -> semi-flatten.c * plug-ins/common/smooth_palette.c -> smooth-palette.c * plug-ins/common/snoise.c -> noise-solid.c * plug-ins/common/sobel.c -> edge-sobel.c * plug-ins/common/spheredesigner.c -> sphere-designer.c * plug-ins/common/spread.c -> noise-spread.c * plug-ins/common/struc.c -> apply-canvas.c * plug-ins/common/threshold_alpha.c -> threshold-alpha.c * plug-ins/common/tileit.c -> tile-small.c * plug-ins/common/tiler.c -> tile-seamless.c * plug-ins/common/uniteditor.c -> unit-editor.c * plug-ins/common/unsharp.c -> unsharp-mask.c * plug-ins/common/vinvert.c -> value-invert.c * plug-ins/common/vpropagate.c -> value-propagate.c * plug-ins/common/webbrowser.c -> web-browser.c * plug-ins/common/whirlpinch.c -> whirl-pinch.c * plug-ins/common/zealouscrop.c -> crop-zealous.c * plug-ins/common/plugin-defs.pl: changed accordingly. * plug-ins/common/Makefile.am: regenerated. svn path=/trunk/; revision=25192
2008-03-24 23:29:55 +08:00
#define PLUG_IN_BINARY "gradient-map"
#define NSAMPLES 256
#define LUMINOSITY(X) (GIMP_RGB_LUMINANCE (X[0], X[1], X[2]) + 0.5)
typedef enum
{
GRADIENT_MODE = 1,
PALETTE_MODE
} MapMode;
1997-11-25 06:05:25 +08:00
/* Declare a local function.
*/
static void query (void);
static void run (const gchar *name,
gint nparams,
const GimpParam *param,
gint *nreturn_vals,
GimpParam **return_vals);
1997-11-25 06:05:25 +08:00
static void map (GimpDrawable *drawable,
MapMode mode);
static guchar * get_samples_gradient (GimpDrawable *drawable);
static guchar * get_samples_palette (GimpDrawable *drawable);
static void map_func (const guchar *src,
guchar *dest,
gint bpp,
gpointer data);
1997-11-25 06:05:25 +08:00
const GimpPlugInInfo PLUG_IN_INFO =
1997-11-25 06:05:25 +08:00
{
NULL, /* init_proc */
NULL, /* quit_proc */
query, /* query_proc */
run, /* run_proc */
1997-11-25 06:05:25 +08:00
};
MAIN ()
static void
query (void)
1997-11-25 06:05:25 +08:00
{
static const GimpParamDef args[]=
{
{ GIMP_PDB_INT32, "run-mode", "The run mode { RUN-INTERACTIVE (0), RUN-NONINTERACTIVE (1) }" },
{ GIMP_PDB_IMAGE, "image", "Input image (unused)" },
{ GIMP_PDB_DRAWABLE, "drawable", "Input drawable" }
};
1997-11-25 06:05:25 +08:00
gimp_install_procedure (GRADMAP_PROC,
N_("Recolor the image using colors from the active gradient"),
"This plug-in maps the contents of the specified "
"drawable with active gradient. It calculates "
"luminosity of each pixel and replaces the pixel "
"by the sample of active gradient at the position "
"proportional to that luminosity. Complete black "
"pixel becomes the leftmost color of the gradient, "
"and complete white becomes the rightmost. Works on "
"both Grayscale and RGB image with/without alpha "
"channel.",
"Eiichi Takamori",
"Eiichi Takamori",
"1997",
N_("_Gradient Map"),
"RGB*, GRAY*",
GIMP_PLUGIN,
G_N_ELEMENTS (args), 0,
args, NULL);
gimp_plugin_menu_register (GRADMAP_PROC, "<Image>/Colors/Map");
gimp_install_procedure (PALETTEMAP_PROC,
N_("Recolor the image using colors from the active palette"),
"This plug-in maps the contents of the specified "
"drawable with the active palette. It calculates "
"luminosity of each pixel and replaces the pixel "
"by the palette sample at the corresponding "
"index. A complete black "
"pixel becomes the lowest palette entry, "
"and complete white becomes the highest. Works on "
"both Grayscale and RGB image with/without alpha "
"channel.",
"Bill Skaggs",
"Bill Skaggs",
"2004",
N_("_Palette Map"),
"RGB*, GRAY*",
GIMP_PLUGIN,
G_N_ELEMENTS (args), 0,
args, NULL);
gimp_plugin_menu_register (PALETTEMAP_PROC, "<Image>/Colors/Map");
1997-11-25 06:05:25 +08:00
}
static void
run (const gchar *name,
gint nparams,
const GimpParam *param,
gint *nreturn_vals,
GimpParam **return_vals)
1997-11-25 06:05:25 +08:00
{
Cleaned up and improved the message system: 2003-06-13 Michael Natterer <mitch@gimp.org> Cleaned up and improved the message system: * app/core/gimp.[ch]: added "const gchar *domain" to GimpMessageFunc (a NULL domain means the message is from the GIMP core, everything else is a plug-in). * app/errors.c: pass "domain == NULL" to gimp_message(). * tools/pdbgen/pdb/message.pdb: derive the message domain from the current plug-in's menu_path (evil hack but works reasonably well). * app/pdb/message_cmds.c: regenerated. * app/widgets/gimpwidgets-utils.[ch] (gimp_message_box): added a header showing the message domain and changed the dialog layout to follow the HIG more closely. * app/gui/error-console-dialog.[ch]: removed. * app/widgets/gimperrorconsole.[ch] * app/gui/error-console-commands.[ch] * app/gui/error-console-menu.[ch]: new files containing a re-implementation of the error console dialog. * app/gui/Makefile.am * app/gui/dialogs-constructors.c * app/gui/gui.c * app/gui/menus.c * app/widgets/Makefile.am * app/widgets/widgets-types.h: changed accordingly. * app/display/gimpprogress.c: added more spacing and removed the separator (more HIG compliant). * plug-ins/[most plug-ins].c: Changed lots of messages and progress strings: - Removed plug-in names from messages since that's automatically covered by "domain" now. - Put all filenames in ''. - Changed "Loading" to "Opening". - Added "..." to all progress messages. - Cleaned up all file open/save error messages to look the same and include g_strerror(errno). - Removed special casing for progress bars and *always* show them, not only if run_mode != GIMP_RUN_NONINTERACTIVE (we can't expect all plug-ins to do this correctly but need to hack the core to sort out unwanted progress bars). Unrelated: - Cleaned up indentation, spacing, #includes, coding style and other stuff while I was at all these files.
2003-06-13 22:37:00 +08:00
static GimpParam values[1];
GimpDrawable *drawable;
GimpPDBStatusType status = GIMP_PDB_SUCCESS;
GimpRunMode run_mode;
1997-11-25 06:05:25 +08:00
run_mode = param[0].data.d_int32;
INIT_I18N();
1997-11-25 06:05:25 +08:00
*nreturn_vals = 1;
Cleaned up and improved the message system: 2003-06-13 Michael Natterer <mitch@gimp.org> Cleaned up and improved the message system: * app/core/gimp.[ch]: added "const gchar *domain" to GimpMessageFunc (a NULL domain means the message is from the GIMP core, everything else is a plug-in). * app/errors.c: pass "domain == NULL" to gimp_message(). * tools/pdbgen/pdb/message.pdb: derive the message domain from the current plug-in's menu_path (evil hack but works reasonably well). * app/pdb/message_cmds.c: regenerated. * app/widgets/gimpwidgets-utils.[ch] (gimp_message_box): added a header showing the message domain and changed the dialog layout to follow the HIG more closely. * app/gui/error-console-dialog.[ch]: removed. * app/widgets/gimperrorconsole.[ch] * app/gui/error-console-commands.[ch] * app/gui/error-console-menu.[ch]: new files containing a re-implementation of the error console dialog. * app/gui/Makefile.am * app/gui/dialogs-constructors.c * app/gui/gui.c * app/gui/menus.c * app/widgets/Makefile.am * app/widgets/widgets-types.h: changed accordingly. * app/display/gimpprogress.c: added more spacing and removed the separator (more HIG compliant). * plug-ins/[most plug-ins].c: Changed lots of messages and progress strings: - Removed plug-in names from messages since that's automatically covered by "domain" now. - Put all filenames in ''. - Changed "Loading" to "Opening". - Added "..." to all progress messages. - Cleaned up all file open/save error messages to look the same and include g_strerror(errno). - Removed special casing for progress bars and *always* show them, not only if run_mode != GIMP_RUN_NONINTERACTIVE (we can't expect all plug-ins to do this correctly but need to hack the core to sort out unwanted progress bars). Unrelated: - Cleaned up indentation, spacing, #includes, coding style and other stuff while I was at all these files.
2003-06-13 22:37:00 +08:00
*return_vals = values;
1997-11-25 06:05:25 +08:00
Cleaned up and improved the message system: 2003-06-13 Michael Natterer <mitch@gimp.org> Cleaned up and improved the message system: * app/core/gimp.[ch]: added "const gchar *domain" to GimpMessageFunc (a NULL domain means the message is from the GIMP core, everything else is a plug-in). * app/errors.c: pass "domain == NULL" to gimp_message(). * tools/pdbgen/pdb/message.pdb: derive the message domain from the current plug-in's menu_path (evil hack but works reasonably well). * app/pdb/message_cmds.c: regenerated. * app/widgets/gimpwidgets-utils.[ch] (gimp_message_box): added a header showing the message domain and changed the dialog layout to follow the HIG more closely. * app/gui/error-console-dialog.[ch]: removed. * app/widgets/gimperrorconsole.[ch] * app/gui/error-console-commands.[ch] * app/gui/error-console-menu.[ch]: new files containing a re-implementation of the error console dialog. * app/gui/Makefile.am * app/gui/dialogs-constructors.c * app/gui/gui.c * app/gui/menus.c * app/widgets/Makefile.am * app/widgets/widgets-types.h: changed accordingly. * app/display/gimpprogress.c: added more spacing and removed the separator (more HIG compliant). * plug-ins/[most plug-ins].c: Changed lots of messages and progress strings: - Removed plug-in names from messages since that's automatically covered by "domain" now. - Put all filenames in ''. - Changed "Loading" to "Opening". - Added "..." to all progress messages. - Cleaned up all file open/save error messages to look the same and include g_strerror(errno). - Removed special casing for progress bars and *always* show them, not only if run_mode != GIMP_RUN_NONINTERACTIVE (we can't expect all plug-ins to do this correctly but need to hack the core to sort out unwanted progress bars). Unrelated: - Cleaned up indentation, spacing, #includes, coding style and other stuff while I was at all these files.
2003-06-13 22:37:00 +08:00
values[0].type = GIMP_PDB_STATUS;
1997-11-25 06:05:25 +08:00
values[0].data.d_status = status;
/* Get the specified drawable */
drawable = gimp_drawable_get (param[2].data.d_drawable);
/* Make sure that the drawable is gray or RGB color */
if (gimp_drawable_is_rgb (drawable->drawable_id) ||
gimp_drawable_is_gray (drawable->drawable_id))
{
MapMode mode = 0;
if ( !strcmp (name, GRADMAP_PROC))
{
mode = GRADIENT_MODE;
gimp_progress_init (_("Gradient Map"));
}
else if ( !strcmp (name, PALETTEMAP_PROC))
{
mode = PALETTE_MODE;
gimp_progress_init (_("Palette Map"));
}
else
{
status = GIMP_PDB_CALLING_ERROR;
}
1997-11-25 06:05:25 +08:00
if (status == GIMP_PDB_SUCCESS)
{
if (mode)
map (drawable, mode);
1997-11-25 06:05:25 +08:00
if (run_mode != GIMP_RUN_NONINTERACTIVE)
gimp_displays_flush ();
}
}
else
{
status = GIMP_PDB_EXECUTION_ERROR;
}
1997-11-25 06:05:25 +08:00
values[0].data.d_status = status;
gimp_drawable_detach (drawable);
}
typedef struct
{
guchar *samples;
gboolean is_rgb;
gboolean has_alpha;
MapMode mode;
} MapParam;
1997-11-25 06:05:25 +08:00
static void
map_func (const guchar *src,
guchar *dest,
gint bpp,
gpointer data)
{
MapParam *param = data;
gint lum;
gint b;
guchar *samp;
1997-11-25 06:05:25 +08:00
lum = (param->is_rgb) ? LUMINOSITY (src) : src[0];
samp = &param->samples[lum * bpp];
1997-11-25 06:05:25 +08:00
if (param->has_alpha)
1997-11-25 06:05:25 +08:00
{
for (b = 0; b < bpp - 1; b++)
dest[b] = samp[b];
dest[b] = ((guint)samp[b] * (guint)src[b]) / 255;
1997-11-25 06:05:25 +08:00
}
else
{
for (b = 0; b < bpp; b++)
dest[b] = samp[b];
}
}
static void
map (GimpDrawable *drawable,
MapMode mode)
{
MapParam param;
1997-11-25 06:05:25 +08:00
param.is_rgb = gimp_drawable_is_rgb (drawable->drawable_id);
param.has_alpha = gimp_drawable_has_alpha (drawable->drawable_id);
1997-11-25 06:05:25 +08:00
switch (mode)
{
case GRADIENT_MODE:
param.samples = get_samples_gradient (drawable);
break;
case PALETTE_MODE:
param.samples = get_samples_palette (drawable);
break;
default:
g_error ("plug_in_gradmap: invalid mode");
}
gimp_rgn_iterate2 (drawable, 0 /* unused */, map_func, &param);
1997-11-25 06:05:25 +08:00
}
/*
Returns 256 samples of active gradient.
Each sample has (gimp_drawable_bpp (drawable->drawable_id)) bytes.
1997-11-25 06:05:25 +08:00
*/
static guchar *
get_samples_gradient (GimpDrawable *drawable)
1997-11-25 06:05:25 +08:00
{
gchar *gradient_name;
gint n_f_samples;
gdouble *f_samples, *f_samp; /* float samples */
guchar *byte_samples, *b_samp; /* byte samples */
gint bpp, color, has_alpha, alpha;
gint i, j;
1997-11-25 06:05:25 +08:00
gradient_name = gimp_context_get_gradient ();
/* FIXME: "reverse" hardcoded to FALSE. */
gimp_gradient_get_uniform_samples (gradient_name, NSAMPLES, FALSE,
&n_f_samples, &f_samples);
1997-11-25 06:05:25 +08:00
bpp = gimp_drawable_bpp (drawable->drawable_id);
color = gimp_drawable_is_rgb (drawable->drawable_id);
has_alpha = gimp_drawable_has_alpha (drawable->drawable_id);
alpha = (has_alpha ? bpp - 1 : bpp);
1997-11-25 06:05:25 +08:00
byte_samples = g_new (guchar, NSAMPLES * bpp);
1997-11-25 06:05:25 +08:00
for (i = 0; i < NSAMPLES; i++)
{
b_samp = &byte_samples[i * bpp];
1997-11-25 06:05:25 +08:00
f_samp = &f_samples[i * 4];
if (color)
for (j = 0; j < 3; j++)
b_samp[j] = f_samp[j] * 255;
1997-11-25 06:05:25 +08:00
else
b_samp[0] = LUMINOSITY (f_samp) * 255;
1997-11-25 06:05:25 +08:00
if (has_alpha)
b_samp[alpha] = f_samp[3] * 255;
1997-11-25 06:05:25 +08:00
}
g_free (f_samples);
return byte_samples;
}
/*
Returns 256 samples of the palette.
Each sample has (gimp_drawable_bpp (drawable->drawable_id)) bytes.
*/
static guchar *
get_samples_palette (GimpDrawable *drawable)
{
gchar *palette_name;
GimpRGB color_sample;
guchar *byte_samples;
guchar *b_samp;
gint bpp, color, has_alpha, alpha;
gint i;
gint num_colors;
gfloat factor;
gint pal_entry;
palette_name = gimp_context_get_palette ();
gimp_palette_get_info (palette_name, &num_colors);
bpp = gimp_drawable_bpp (drawable->drawable_id);
color = gimp_drawable_is_rgb (drawable->drawable_id);
has_alpha = gimp_drawable_has_alpha (drawable->drawable_id);
alpha = (has_alpha ? bpp - 1 : bpp);
byte_samples = g_new (guchar, NSAMPLES * bpp);
factor = ( (float) num_colors) / NSAMPLES;
for (i = 0; i < NSAMPLES; i++)
{
b_samp = &byte_samples[i * bpp];
pal_entry = CLAMP( (int)(i * factor), 0, num_colors);
gimp_palette_entry_get_color (palette_name, pal_entry, &color_sample);
if (color)
gimp_rgb_get_uchar (&color_sample,
b_samp, b_samp + 1, b_samp + 2);
else
*b_samp = gimp_rgb_luminance_uchar (&color_sample);
if (has_alpha)
b_samp[alpha] = 255;
}
return byte_samples;
1997-11-25 06:05:25 +08:00
}