1997-11-25 06:05:25 +08:00
|
|
|
/* Normalize 1.00 --- image filter plug-in for The GIMP
|
|
|
|
*
|
|
|
|
* Copyright (C) 1997 Adam D. Moss (adam@foxbox.org)
|
|
|
|
* Very largely based on Quartic's "Contrast Autostretch"
|
|
|
|
*
|
|
|
|
* 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
|
1998-04-13 13:44:11 +08:00
|
|
|
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
|
1997-11-25 06:05:25 +08:00
|
|
|
*/
|
|
|
|
|
1999-02-28 01:02:12 +08:00
|
|
|
|
1997-11-25 06:05:25 +08:00
|
|
|
/* This plugin performs almost the same operation as the 'contrast
|
|
|
|
* autostretch' plugin, except that it won't allow the colour channels
|
|
|
|
* to normalize independently. This is actually what most people probably
|
|
|
|
* want instead of contrast-autostretch; use c-a only if you wish to remove
|
|
|
|
* an undesirable colour-tint from a source image which is supposed to
|
|
|
|
* contain pure-white and pure-black.
|
|
|
|
*/
|
|
|
|
|
2000-05-02 01:01:18 +08:00
|
|
|
#include "config.h"
|
1997-11-25 06:05:25 +08:00
|
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
|
2000-05-02 01:01:18 +08:00
|
|
|
#include <libgimp/gimp.h>
|
|
|
|
|
1999-09-09 15:09:33 +08:00
|
|
|
#include "libgimp/stdplugins-intl.h"
|
|
|
|
|
2000-05-02 01:01:18 +08:00
|
|
|
|
1997-11-25 06:05:25 +08:00
|
|
|
/* Declare local functions.
|
|
|
|
*/
|
2001-12-04 01:59:48 +08:00
|
|
|
static void query (void);
|
2003-07-02 08:15:09 +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
|
|
|
|
2003-07-02 08:15:09 +08:00
|
|
|
static void normalize (GimpDrawable *drawable);
|
|
|
|
static void indexed_normalize (gint32 image_ID);
|
1997-11-25 06:05:25 +08:00
|
|
|
|
|
|
|
|
2000-08-22 09:26:57 +08:00
|
|
|
GimpPlugInInfo PLUG_IN_INFO =
|
1997-11-25 06:05:25 +08:00
|
|
|
{
|
2000-05-02 01:01:18 +08:00
|
|
|
NULL, /* init_proc */
|
|
|
|
NULL, /* quit_proc */
|
|
|
|
query, /* query_proc */
|
|
|
|
run, /* run_proc */
|
1997-11-25 06:05:25 +08:00
|
|
|
};
|
|
|
|
|
2003-01-31 05:58:11 +08:00
|
|
|
static GimpRunMode run_mode;
|
|
|
|
|
1997-11-25 06:05:25 +08:00
|
|
|
|
|
|
|
MAIN ()
|
|
|
|
|
|
|
|
static void
|
2000-05-02 01:01:18 +08:00
|
|
|
query (void)
|
1997-11-25 06:05:25 +08:00
|
|
|
{
|
2000-08-22 09:26:57 +08:00
|
|
|
static GimpParamDef args[] =
|
1997-11-25 06:05:25 +08:00
|
|
|
{
|
2000-08-22 09:26:57 +08:00
|
|
|
{ GIMP_PDB_INT32, "run_mode", "Interactive, non-interactive" },
|
|
|
|
{ GIMP_PDB_IMAGE, "image", "Input image" },
|
|
|
|
{ GIMP_PDB_DRAWABLE, "drawable", "Input drawable" }
|
1997-11-25 06:05:25 +08:00
|
|
|
};
|
1999-09-09 15:09:33 +08:00
|
|
|
|
1997-11-25 06:05:25 +08:00
|
|
|
gimp_install_procedure ("plug_in_normalize",
|
2000-05-02 01:01:18 +08:00
|
|
|
"Normalize the contrast of the specified drawable to "
|
|
|
|
"cover all possible ranges.",
|
|
|
|
"This plugin performs almost the same operation as "
|
|
|
|
"the 'contrast autostretch' plugin, except that it "
|
|
|
|
"won't allow the color channels to normalize "
|
|
|
|
"independently. This is actually what most people "
|
|
|
|
"probably want instead of contrast-autostretch; use "
|
|
|
|
"c-a only if you wish to remove an undesirable "
|
|
|
|
"color-tint from a source image which is supposed to "
|
|
|
|
"contain pure-white and pure-black.",
|
1997-11-25 06:05:25 +08:00
|
|
|
"Adam D. Moss, Federico Mena Quintero",
|
|
|
|
"Adam D. Moss, Federico Mena Quintero",
|
|
|
|
"1997",
|
2003-07-17 23:47:18 +08:00
|
|
|
N_("<Image>/Layer/Colors/Auto/_Normalize"),
|
1997-11-25 06:05:25 +08:00
|
|
|
"RGB*, GRAY*, INDEXED*",
|
2000-08-22 09:26:57 +08:00
|
|
|
GIMP_PLUGIN,
|
2001-12-04 01:59:48 +08:00
|
|
|
G_N_ELEMENTS (args), 0,
|
2000-05-02 01:01:18 +08:00
|
|
|
args, NULL);
|
1997-11-25 06:05:25 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2003-07-02 08:15:09 +08:00
|
|
|
run (const gchar *name,
|
|
|
|
gint nparams,
|
|
|
|
const GimpParam *param,
|
|
|
|
gint *nreturn_vals,
|
|
|
|
GimpParam **return_vals)
|
1997-11-25 06:05:25 +08:00
|
|
|
{
|
2001-12-04 01:59:48 +08:00
|
|
|
static GimpParam values[1];
|
|
|
|
GimpDrawable *drawable;
|
|
|
|
GimpPDBStatusType status = GIMP_PDB_SUCCESS;
|
1997-11-25 06:05:25 +08:00
|
|
|
|
|
|
|
gint32 image_ID;
|
|
|
|
|
2003-03-26 00:38:19 +08:00
|
|
|
INIT_I18N ();
|
1999-09-09 15:09:33 +08:00
|
|
|
|
1997-11-25 06:05:25 +08:00
|
|
|
run_mode = param[0].data.d_int32;
|
|
|
|
|
|
|
|
/* Get the specified drawable */
|
|
|
|
drawable = gimp_drawable_get (param[2].data.d_drawable);
|
|
|
|
image_ID = param[1].data.d_image;
|
|
|
|
|
|
|
|
/* Make sure that the drawable is gray or RGB color */
|
2003-06-13 22:37:00 +08:00
|
|
|
if (gimp_drawable_is_rgb (drawable->drawable_id) ||
|
|
|
|
gimp_drawable_is_gray (drawable->drawable_id))
|
1997-11-25 06:05:25 +08:00
|
|
|
{
|
1999-09-09 15:09:33 +08:00
|
|
|
gimp_progress_init (_("Normalizing..."));
|
1997-11-25 06:05:25 +08:00
|
|
|
gimp_tile_cache_ntiles (2 * (drawable->width / gimp_tile_width () + 1));
|
2003-06-13 22:37:00 +08:00
|
|
|
|
2001-12-04 01:59:48 +08:00
|
|
|
normalize (drawable);
|
1997-11-25 06:05:25 +08:00
|
|
|
|
2000-08-22 09:26:57 +08:00
|
|
|
if (run_mode != GIMP_RUN_NONINTERACTIVE)
|
1997-11-25 06:05:25 +08:00
|
|
|
gimp_displays_flush ();
|
|
|
|
}
|
2001-06-15 04:07:38 +08:00
|
|
|
else if (gimp_drawable_is_indexed (drawable->drawable_id))
|
1997-11-25 06:05:25 +08:00
|
|
|
{
|
2001-12-04 01:59:48 +08:00
|
|
|
indexed_normalize (image_ID);
|
2003-06-13 22:37:00 +08:00
|
|
|
|
2000-08-22 09:26:57 +08:00
|
|
|
if (run_mode != GIMP_RUN_NONINTERACTIVE)
|
1999-02-28 01:02:12 +08:00
|
|
|
gimp_displays_flush ();
|
1997-11-25 06:05:25 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* gimp_message ("normalize: cannot operate on indexed color images"); */
|
2000-08-22 09:26:57 +08:00
|
|
|
status = GIMP_PDB_EXECUTION_ERROR;
|
1997-11-25 06:05:25 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
*nreturn_vals = 1;
|
2003-06-13 22:37:00 +08:00
|
|
|
*return_vals = values;
|
1997-11-25 06:05:25 +08:00
|
|
|
|
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;
|
|
|
|
|
|
|
|
gimp_drawable_detach (drawable);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void
|
2001-12-04 01:59:48 +08:00
|
|
|
indexed_normalize (gint32 image_ID) /* a.d.m. */
|
1997-11-25 06:05:25 +08:00
|
|
|
{
|
|
|
|
guchar *cmap;
|
|
|
|
gint ncols,i;
|
|
|
|
gint hi=0,lo=255;
|
|
|
|
|
|
|
|
cmap = gimp_image_get_cmap (image_ID, &ncols);
|
|
|
|
|
|
|
|
if (cmap==NULL)
|
|
|
|
{
|
1999-12-31 02:54:17 +08:00
|
|
|
printf ("normalize: cmap was NULL! Quitting...\n");
|
|
|
|
return;
|
1997-11-25 06:05:25 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
for (i=0;i<ncols;i++)
|
|
|
|
{
|
|
|
|
if (cmap[i*3 +0] > hi) hi=cmap[i*3 +0];
|
|
|
|
if (cmap[i*3 +1] > hi) hi=cmap[i*3 +1];
|
|
|
|
if (cmap[i*3 +2] > hi) hi=cmap[i*3 +2];
|
|
|
|
if (cmap[i*3 +0] < lo) lo=cmap[i*3 +0];
|
|
|
|
if (cmap[i*3 +1] < lo) lo=cmap[i*3 +1];
|
|
|
|
if (cmap[i*3 +2] < lo) lo=cmap[i*3 +2];
|
|
|
|
}
|
|
|
|
|
|
|
|
if (hi!=lo)
|
|
|
|
for (i=0;i<ncols;i++)
|
|
|
|
{
|
|
|
|
cmap[i*3 +0] = (255 * (cmap[i*3 +0] - lo)) / (hi-lo);
|
|
|
|
cmap[i*3 +1] = (255 * (cmap[i*3 +1] - lo)) / (hi-lo);
|
|
|
|
cmap[i*3 +2] = (255 * (cmap[i*3 +2] - lo)) / (hi-lo);
|
|
|
|
}
|
|
|
|
|
|
|
|
gimp_image_set_cmap (image_ID, cmap, ncols);
|
|
|
|
}
|
|
|
|
|
2003-01-31 05:58:11 +08:00
|
|
|
typedef struct {
|
|
|
|
guchar lut[256];
|
|
|
|
gdouble min;
|
|
|
|
gdouble max;
|
|
|
|
gint alpha;
|
|
|
|
gboolean has_alpha;
|
|
|
|
} NormalizeParam_t;
|
|
|
|
|
|
|
|
static void
|
2003-07-02 08:15:09 +08:00
|
|
|
find_min_max (const guchar *src,
|
|
|
|
gint bpp,
|
|
|
|
gpointer data)
|
2003-01-31 05:58:11 +08:00
|
|
|
{
|
|
|
|
NormalizeParam_t *param = (NormalizeParam_t*) data;
|
2003-07-02 08:15:09 +08:00
|
|
|
gint b;
|
2003-01-31 05:58:11 +08:00
|
|
|
|
|
|
|
for (b = 0; b < param->alpha; b++)
|
|
|
|
{
|
|
|
|
if (!param->has_alpha || src[param->alpha])
|
|
|
|
{
|
|
|
|
if (src[b] < param->min)
|
|
|
|
param->min = src[b];
|
|
|
|
if (src[b] > param->max)
|
|
|
|
param->max = src[b];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2003-07-02 08:15:09 +08:00
|
|
|
normalize_func (const guchar *src,
|
|
|
|
guchar *dest,
|
|
|
|
gint bpp,
|
|
|
|
gpointer data)
|
2003-01-31 05:58:11 +08:00
|
|
|
{
|
|
|
|
NormalizeParam_t *param = (NormalizeParam_t*) data;
|
2003-07-02 08:15:09 +08:00
|
|
|
gint b;
|
2003-01-31 05:58:11 +08:00
|
|
|
|
|
|
|
for (b = 0; b < param->alpha; b++)
|
|
|
|
dest[b] = param->lut[src[b]];
|
|
|
|
|
|
|
|
if (param->has_alpha)
|
|
|
|
dest[param->alpha] = src[param->alpha];
|
|
|
|
}
|
1997-11-25 06:05:25 +08:00
|
|
|
|
|
|
|
static void
|
2001-12-04 01:59:48 +08:00
|
|
|
normalize (GimpDrawable *drawable)
|
1997-11-25 06:05:25 +08:00
|
|
|
{
|
2003-01-31 05:58:11 +08:00
|
|
|
NormalizeParam_t param;
|
|
|
|
gint x;
|
1997-11-25 06:05:25 +08:00
|
|
|
guchar range;
|
|
|
|
|
2003-01-31 05:58:11 +08:00
|
|
|
param.min = 255;
|
|
|
|
param.max = 0;
|
|
|
|
param.has_alpha = gimp_drawable_has_alpha (drawable->drawable_id);
|
|
|
|
param.alpha = (param.has_alpha) ? drawable->bpp - 1 : drawable->bpp;
|
1997-11-25 06:05:25 +08:00
|
|
|
|
2003-01-31 05:58:11 +08:00
|
|
|
gimp_rgn_iterate1 (drawable, run_mode, find_min_max, ¶m);
|
1997-11-25 06:05:25 +08:00
|
|
|
|
|
|
|
/* Calculate LUT */
|
|
|
|
|
2003-01-31 05:58:11 +08:00
|
|
|
range = param.max - param.min;
|
1997-11-25 06:05:25 +08:00
|
|
|
|
|
|
|
if (range != 0)
|
2003-01-31 05:58:11 +08:00
|
|
|
for (x = param.min; x <= param.max; x++)
|
|
|
|
param.lut[x] = 255 * (x - param.min) / range;
|
1997-11-25 06:05:25 +08:00
|
|
|
else
|
2003-01-31 05:58:11 +08:00
|
|
|
param.lut[(gint)param.min] = param.min;
|
1997-11-25 06:05:25 +08:00
|
|
|
|
2003-01-31 05:58:11 +08:00
|
|
|
gimp_rgn_iterate2 (drawable, run_mode, normalize_func, ¶m);
|
1997-11-25 06:05:25 +08:00
|
|
|
}
|
2003-01-31 05:58:11 +08:00
|
|
|
|