1
/* GIMP - The GNU Image Manipulation Program
2
* Copyright (C) 1995 Spencer Kimball and Peter Mattis
5
* Copyright (C) 1997 Eiichi Takamori <taka@ma1.seikyou.ne.jp>
7
* This program is free software; you can redistribute it and/or modify
8
* it under the terms of the GNU General Public License as published by
9
* the Free Software Foundation; either version 2 of the License, or
10
* (at your option) any later version.
12
* This program is distributed in the hope that it will be useful,
13
* but WITHOUT ANY WARRANTY; without even the implied warranty of
14
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15
* GNU General Public License for more details.
17
* You should have received a copy of the GNU General Public License
18
* along with this program; if not, write to the Free Software
19
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
25
#include <libgimp/gimp.h>
27
#include "libgimp/stdplugins-intl.h"
30
/* Some useful macros */
32
#define GRADMAP_PROC "plug-in-gradmap"
33
#define PALETTEMAP_PROC "plug-in-palettemap"
34
#define PLUG_IN_BINARY "gradient-map"
36
#define LUMINOSITY(X) (GIMP_RGB_LUMINANCE (X[0], X[1], X[2]) + 0.5)
44
/* Declare a local function.
46
static void query (void);
47
static void run (const gchar *name,
49
const GimpParam *param,
51
GimpParam **return_vals);
53
static void map (GimpDrawable *drawable,
55
static guchar * get_samples_gradient (GimpDrawable *drawable);
56
static guchar * get_samples_palette (GimpDrawable *drawable);
57
static void map_func (const guchar *src,
63
const GimpPlugInInfo PLUG_IN_INFO =
67
query, /* query_proc */
76
static const GimpParamDef args[]=
78
{ GIMP_PDB_INT32, "run-mode", "Interactive, non-interactive" },
79
{ GIMP_PDB_IMAGE, "image", "Input image (unused)" },
80
{ GIMP_PDB_DRAWABLE, "drawable", "Input drawable" }
83
gimp_install_procedure (GRADMAP_PROC,
84
N_("Recolor the image using colors from the active gradient"),
85
"This plug-in maps the contents of the specified "
86
"drawable with active gradient. It calculates "
87
"luminosity of each pixel and replaces the pixel "
88
"by the sample of active gradient at the position "
89
"proportional to that luminosity. Complete black "
90
"pixel becomes the leftmost color of the gradient, "
91
"and complete white becomes the rightmost. Works on "
92
"both Grayscale and RGB image with/without alpha "
100
G_N_ELEMENTS (args), 0,
103
gimp_plugin_menu_register (GRADMAP_PROC, "<Image>/Colors/Map");
105
gimp_install_procedure (PALETTEMAP_PROC,
106
N_("Recolor the image using colors from the active palette"),
107
"This plug-in maps the contents of the specified "
108
"drawable with the active palette. It calculates "
109
"luminosity of each pixel and replaces the pixel "
110
"by the palette sample at the corresponding "
111
"index. A complete black "
112
"pixel becomes the lowest palette entry, "
113
"and complete white becomes the highest. Works on "
114
"both Grayscale and RGB image with/without alpha "
122
G_N_ELEMENTS (args), 0,
125
gimp_plugin_menu_register (PALETTEMAP_PROC, "<Image>/Colors/Map");
129
run (const gchar *name,
131
const GimpParam *param,
133
GimpParam **return_vals)
135
static GimpParam values[1];
136
GimpDrawable *drawable;
137
GimpPDBStatusType status = GIMP_PDB_SUCCESS;
138
GimpRunMode run_mode;
140
run_mode = param[0].data.d_int32;
145
*return_vals = values;
147
values[0].type = GIMP_PDB_STATUS;
148
values[0].data.d_status = status;
150
/* Get the specified drawable */
151
drawable = gimp_drawable_get (param[2].data.d_drawable);
153
/* Make sure that the drawable is gray or RGB color */
154
if (gimp_drawable_is_rgb (drawable->drawable_id) ||
155
gimp_drawable_is_gray (drawable->drawable_id))
159
if ( !strcmp (name, GRADMAP_PROC))
161
mode = GRADIENT_MODE;
162
gimp_progress_init (_("Gradient Map"));
164
else if ( !strcmp (name, PALETTEMAP_PROC))
167
gimp_progress_init (_("Palette Map"));
171
status = GIMP_PDB_CALLING_ERROR;
174
if (status == GIMP_PDB_SUCCESS)
177
map (drawable, mode);
179
if (run_mode != GIMP_RUN_NONINTERACTIVE)
180
gimp_displays_flush ();
185
status = GIMP_PDB_EXECUTION_ERROR;
188
values[0].data.d_status = status;
190
gimp_drawable_detach (drawable);
202
map_func (const guchar *src,
207
MapParam *param = data;
212
lum = (param->is_rgb) ? LUMINOSITY (src) : src[0];
213
samp = ¶m->samples[lum * bpp];
215
if (param->has_alpha)
217
for (b = 0; b < bpp - 1; b++)
219
dest[b] = ((guint)samp[b] * (guint)src[b]) / 255;
223
for (b = 0; b < bpp; b++)
229
map (GimpDrawable *drawable,
234
param.is_rgb = gimp_drawable_is_rgb (drawable->drawable_id);
235
param.has_alpha = gimp_drawable_has_alpha (drawable->drawable_id);
240
param.samples = get_samples_gradient (drawable);
243
param.samples = get_samples_palette (drawable);
246
g_error ("plug_in_gradmap: invalid mode");
249
gimp_rgn_iterate2 (drawable, 0 /* unused */, map_func, ¶m);
253
Returns 256 samples of active gradient.
254
Each sample has (gimp_drawable_bpp (drawable->drawable_id)) bytes.
257
get_samples_gradient (GimpDrawable *drawable)
259
gchar *gradient_name;
261
gdouble *f_samples, *f_samp; /* float samples */
262
guchar *byte_samples, *b_samp; /* byte samples */
263
gint bpp, color, has_alpha, alpha;
266
gradient_name = gimp_context_get_gradient ();
269
#warning FIXME: "reverse" hardcoded to FALSE.
271
gimp_gradient_get_uniform_samples (gradient_name, NSAMPLES, FALSE,
272
&n_f_samples, &f_samples);
274
bpp = gimp_drawable_bpp (drawable->drawable_id);
275
color = gimp_drawable_is_rgb (drawable->drawable_id);
276
has_alpha = gimp_drawable_has_alpha (drawable->drawable_id);
277
alpha = (has_alpha ? bpp - 1 : bpp);
279
byte_samples = g_new (guchar, NSAMPLES * bpp);
281
for (i = 0; i < NSAMPLES; i++)
283
b_samp = &byte_samples[i * bpp];
284
f_samp = &f_samples[i * 4];
286
for (j = 0; j < 3; j++)
287
b_samp[j] = f_samp[j] * 255;
289
b_samp[0] = LUMINOSITY (f_samp) * 255;
292
b_samp[alpha] = f_samp[3] * 255;
301
Returns 256 samples of the palette.
302
Each sample has (gimp_drawable_bpp (drawable->drawable_id)) bytes.
305
get_samples_palette (GimpDrawable *drawable)
308
GimpRGB color_sample;
309
guchar *byte_samples;
311
gint bpp, color, has_alpha, alpha;
317
palette_name = gimp_context_get_palette ();
318
gimp_palette_get_info (palette_name, &num_colors);
320
bpp = gimp_drawable_bpp (drawable->drawable_id);
321
color = gimp_drawable_is_rgb (drawable->drawable_id);
322
has_alpha = gimp_drawable_has_alpha (drawable->drawable_id);
323
alpha = (has_alpha ? bpp - 1 : bpp);
325
byte_samples = g_new (guchar, NSAMPLES * bpp);
326
factor = ( (float) num_colors) / NSAMPLES;
328
for (i = 0; i < NSAMPLES; i++)
330
b_samp = &byte_samples[i * bpp];
332
pal_entry = CLAMP( (int)(i * factor), 0, num_colors);
333
gimp_palette_entry_get_color (palette_name, pal_entry, &color_sample);
336
gimp_rgb_get_uchar (&color_sample,
337
b_samp, b_samp + 1, b_samp + 2);
339
*b_samp = gimp_rgb_luminance_uchar (&color_sample);