1
/* GIMP - The GNU Image Manipulation Program
2
* Copyright (C) 1995 Spencer Kimball and Peter Mattis
4
* Normalize 1.00 --- image filter plug-in
6
* Copyright (C) 1997 Adam D. Moss (adam@foxbox.org)
7
* Very largely based on Quartic's "Contrast Autostretch"
9
* This program is free software; you can redistribute it and/or modify
10
* it under the terms of the GNU General Public License as published by
11
* the Free Software Foundation; either version 2 of the License, or
12
* (at your option) any later version.
14
* This program is distributed in the hope that it will be useful,
15
* but WITHOUT ANY WARRANTY; without even the implied warranty of
16
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17
* GNU General Public License for more details.
19
* You should have received a copy of the GNU General Public License
20
* along with this program; if not, write to the Free Software
21
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
25
/* This plugin performs almost the same operation as the 'contrast
26
* autostretch' plugin, except that it won't allow the colour channels
27
* to normalize independently. This is actually what most people probably
28
* want instead of contrast-autostretch; use c-a only if you wish to remove
29
* an undesirable colour-tint from a source image which is supposed to
30
* contain pure-white and pure-black.
37
#include <libgimp/gimp.h>
39
#include "libgimp/stdplugins-intl.h"
42
#define PLUG_IN_PROC "plug-in-normalize"
45
/* Declare local functions.
47
static void query (void);
48
static void run (const gchar *name,
50
const GimpParam *param,
52
GimpParam **return_vals);
54
static void normalize (GimpDrawable *drawable);
55
static void indexed_normalize (gint32 image_ID);
58
const GimpPlugInInfo PLUG_IN_INFO =
62
query, /* query_proc */
72
static const GimpParamDef args[] =
74
{ GIMP_PDB_INT32, "run-mode", "Interactive, non-interactive" },
75
{ GIMP_PDB_IMAGE, "image", "Input image" },
76
{ GIMP_PDB_DRAWABLE, "drawable", "Input drawable" }
79
gimp_install_procedure (PLUG_IN_PROC,
80
N_("Stretch brightness values to cover the full range"),
81
"This plugin performs almost the same operation as "
82
"the 'contrast autostretch' plugin, except that it "
83
"won't allow the color channels to normalize "
84
"independently. This is actually what most people "
85
"probably want instead of contrast-autostretch; use "
86
"c-a only if you wish to remove an undesirable "
87
"color-tint from a source image which is supposed to "
88
"contain pure-white and pure-black.",
89
"Adam D. Moss, Federico Mena Quintero",
90
"Adam D. Moss, Federico Mena Quintero",
93
"RGB*, GRAY*, INDEXED*",
95
G_N_ELEMENTS (args), 0,
98
gimp_plugin_menu_register (PLUG_IN_PROC, "<Image>/Colors/Auto");
102
run (const gchar *name,
104
const GimpParam *param,
106
GimpParam **return_vals)
108
static GimpParam values[1];
109
GimpDrawable *drawable;
110
GimpPDBStatusType status = GIMP_PDB_SUCCESS;
111
GimpRunMode run_mode;
116
run_mode = param[0].data.d_int32;
118
/* Get the specified drawable */
119
drawable = gimp_drawable_get (param[2].data.d_drawable);
120
image_ID = param[1].data.d_image;
122
/* Make sure that the drawable is gray or RGB color */
123
if (gimp_drawable_is_rgb (drawable->drawable_id) ||
124
gimp_drawable_is_gray (drawable->drawable_id))
126
gimp_progress_init (_("Normalizing"));
127
gimp_tile_cache_ntiles (2 * (drawable->width / gimp_tile_width () + 1));
129
normalize (drawable);
131
if (run_mode != GIMP_RUN_NONINTERACTIVE)
132
gimp_displays_flush ();
134
else if (gimp_drawable_is_indexed (drawable->drawable_id))
136
indexed_normalize (image_ID);
138
if (run_mode != GIMP_RUN_NONINTERACTIVE)
139
gimp_displays_flush ();
143
status = GIMP_PDB_EXECUTION_ERROR;
147
*return_vals = values;
149
values[0].type = GIMP_PDB_STATUS;
150
values[0].data.d_status = status;
152
gimp_drawable_detach (drawable);
157
indexed_normalize (gint32 image_ID) /* a.d.m. */
163
cmap = gimp_image_get_colormap (image_ID, &ncols);
167
g_printerr ("normalize: cmap was NULL! Quitting...\n");
171
for (i=0;i<ncols;i++)
173
if (cmap[i*3 +0] > hi) hi=cmap[i*3 +0];
174
if (cmap[i*3 +1] > hi) hi=cmap[i*3 +1];
175
if (cmap[i*3 +2] > hi) hi=cmap[i*3 +2];
176
if (cmap[i*3 +0] < lo) lo=cmap[i*3 +0];
177
if (cmap[i*3 +1] < lo) lo=cmap[i*3 +1];
178
if (cmap[i*3 +2] < lo) lo=cmap[i*3 +2];
182
for (i=0;i<ncols;i++)
184
cmap[i*3 +0] = (255 * (cmap[i*3 +0] - lo)) / (hi-lo);
185
cmap[i*3 +1] = (255 * (cmap[i*3 +1] - lo)) / (hi-lo);
186
cmap[i*3 +2] = (255 * (cmap[i*3 +2] - lo)) / (hi-lo);
189
gimp_image_set_colormap (image_ID, cmap, ncols);
202
find_min_max (const guchar *src,
206
NormalizeParam_t *param = (NormalizeParam_t*) data;
209
for (b = 0; b < param->alpha; b++)
211
if (!param->has_alpha || src[param->alpha])
213
if (src[b] < param->min)
215
if (src[b] > param->max)
222
normalize_func (const guchar *src,
227
NormalizeParam_t *param = (NormalizeParam_t*) data;
230
for (b = 0; b < param->alpha; b++)
231
dest[b] = param->lut[src[b]];
233
if (param->has_alpha)
234
dest[param->alpha] = src[param->alpha];
238
normalize (GimpDrawable *drawable)
240
NormalizeParam_t param;
246
param.has_alpha = gimp_drawable_has_alpha (drawable->drawable_id);
247
param.alpha = (param.has_alpha) ? drawable->bpp - 1 : drawable->bpp;
249
gimp_rgn_iterate1 (drawable, 0 /* unused */, find_min_max, ¶m);
253
range = param.max - param.min;
256
for (x = param.min; x <= param.max; x++)
257
param.lut[x] = 255 * (x - param.min) / range;
259
param.lut[(gint)param.min] = param.min;
261
gimp_rgn_iterate2 (drawable, 0 /* unused */, normalize_func, ¶m);