gimp/plug-ins/common/c_astretch.c

256 lines
7.4 KiB
C
Raw Normal View History

1997-11-25 06:05:25 +08:00
/* Contrast Autostretch 1.06 --- image filter plug-in for The Gimp image
* manipulation program
*
* Copyright (C) 1996 Federico Mena Quintero
*
* You can contact me at quartic@polloux.fciencias.unam.mx
* You can contact the original The Gimp authors at gimp@xcf.berkeley.edu
*
* 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.
1997-11-25 06:05:25 +08:00
*/
#include "config.h"
1997-11-25 06:05:25 +08:00
#include <libgimp/gimp.h>
#include "libgimp/stdplugins-intl.h"
1997-11-25 06:05:25 +08:00
/* Declare local functions.
*/
use the passed Gimp pointer instead of using "the_gimp". 2001-12-03 Michael Natterer <mitch@gimp.org> * app/devices.c: use the passed Gimp pointer instead of using "the_gimp". * app/base/temp-buf.c: indentation. * app/gui/preferences-dialog.c: prefs_toggle_callback(): fixed segfault when trying to find the prefs_dlg widget from a menu item callback (Fixes #65757). * app/gui/offset-dialog.[ch]: fixed public prototype, include the header in the .c file. * app/gui/menus.c: some menu cleanup: moved all functions which operate on the active layer/drawable to <Image>/Layer. Renamed "Layers" to "Layer". * app/display/gimpdisplayshell.c: changed menu update function accordingly. * app/gui/image-commands.[ch] * app/gui/layers-commands.[ch]: moved stuff from image-commands.* to layers-commads.*- * app/tools/gimpblendtool.c * app/tools/gimpbrightnesscontrasttool.c * app/tools/gimpcolorbalancetool.c * app/tools/gimpcurvestool.c * app/tools/gimphistogramtool.c * app/tools/gimphuesaturationtool.c * app/tools/gimplevelstool.c * app/tools/gimpposterizetool.c * app/tools/gimpthresholdtool.c * app/tools/paint_options.c * app/tools/transform_options.c * plug-ins/common/align_layers.c * plug-ins/common/autocrop.c * plug-ins/common/autostretch_hsv.c * plug-ins/common/c_astretch.c * plug-ins/common/color_enhance.c * plug-ins/common/guillotine.c * plug-ins/common/normalize.c * plug-ins/common/rotate.c * plug-ins/common/threshold_alpha.c * plug-ins/common/zealouscrop.c * plug-ins/rcm/rcm.c * plug-ins/fp/fp.c: register under <Image>/Layer, some cosmetic fixes.
2001-12-04 01:59:48 +08:00
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 c_astretch (GimpDrawable *drawable);
static void indexed_c_astretch (gint32 image_ID);
1997-11-25 06:05:25 +08:00
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
};
static GimpRunMode run_mode;
1997-11-25 06:05:25 +08:00
MAIN ()
static void
query (void)
1997-11-25 06:05:25 +08:00
{
static GimpParamDef args[] =
1997-11-25 06:05:25 +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
};
1997-11-25 06:05:25 +08:00
gimp_install_procedure ("plug_in_c_astretch",
"Automatically stretch the contrast of the "
"specified drawable to cover all possible ranges.",
"This simple plug-in does an automatic contrast "
"stretch. For each channel in the image, it finds "
"the minimum and maximum values... it uses those "
"values to stretch the individual histograms to the "
"full contrast range. For some images it may do "
"just what you want; for others it may not work "
"that well.",
"Federico Mena Quintero",
"Federico Mena Quintero",
"1996",
Changed plug-in menu registration again to allow passing just the menu 2004-05-07 Michael Natterer <mitch@gimp.org> Changed plug-in menu registration again to allow passing just the menu item's label (not the full path) in gimp_install_procedure() and only the path (excluding the item's label) in gimp_plugin_menu_register(). Matches the internal action system better and makes translating the menu paths much easier. (Of yourse it's still possible to use the old syntax for backward compatibility). * app/plug-in/plug-in-proc.[ch]: added "gchar *menu_label". * app/plug-in/plug-in-params.[ch]: added new functions plug_in_param_defs_check() and plug_in_proc_args_check() which check if a procedure's parameters match its menu location (e.g. <Image> needs RUN-MODE, IMAGE, DRAWABLE). * app/plug-in/plug-in-message.c (plug_in_handle_proc_install): if registering an old-style (full) menu_path, use plug_in_param_defs_check(), set proc_def->menu_label otherwise. * tools/pdbgen/pdb/plug_in.pdb (plugin_menu_register): use plug_in_proc_args_check() on the passed menu_path and make sugre old and new style menu registration are not mixed. * app/pdb/plug_in_cmds.c: regenerated. * app/plug-in/plug-in-rc.c: save/restore "menu_label". * app/actions/file-dialog-actions.c * app/actions/plug-in-actions.c * app/menus/plug-in-menus.c: changed action/menu creation accordingly. Some hacks needed to allow both old and new style menu_label/menu_paths. * app/plug-in/plug-in.c * app/widgets/gimpfiledialog.c * app/xcf/xcf.c: changed accordingly. * plug-ins/common/align_layers.c * plug-ins/common/animationplay.c * plug-ins/common/animoptimize.c * plug-ins/common/apply_lens.c * plug-ins/common/autocrop.c * plug-ins/common/autostretch_hsv.c * plug-ins/common/blinds.c * plug-ins/common/blur.c * plug-ins/common/borderaverage.c * plug-ins/common/bumpmap.c * plug-ins/common/c_astretch.c * plug-ins/common/ccanalyze.c * plug-ins/common/channel_mixer.c * plug-ins/common/checkerboard.c * plug-ins/common/color_enhance.c * plug-ins/common/colorify.c * plug-ins/common/colortoalpha.c * plug-ins/common/compose.c * plug-ins/common/convmatrix.c * plug-ins/common/cubism.c * plug-ins/common/curve_bend.c * plug-ins/common/decompose.c * plug-ins/common/deinterlace.c * plug-ins/common/depthmerge.c * plug-ins/common/destripe.c * plug-ins/common/diffraction.c * plug-ins/common/displace.c * plug-ins/common/edge.c * plug-ins/common/emboss.c * plug-ins/common/engrave.c * plug-ins/common/exchange.c * plug-ins/common/film.c * plug-ins/common/flarefx.c * plug-ins/common/fractaltrace.c * plug-ins/common/screenshot.c: ported the first few plug-ins to the new registration scheme.
2004-05-07 08:30:24 +08:00
N_("_Stretch Contrast"),
"RGB*, GRAY*, INDEXED*",
GIMP_PLUGIN,
G_N_ELEMENTS (args), 0,
args, NULL);
Changed plug-in menu registration again to allow passing just the menu 2004-05-07 Michael Natterer <mitch@gimp.org> Changed plug-in menu registration again to allow passing just the menu item's label (not the full path) in gimp_install_procedure() and only the path (excluding the item's label) in gimp_plugin_menu_register(). Matches the internal action system better and makes translating the menu paths much easier. (Of yourse it's still possible to use the old syntax for backward compatibility). * app/plug-in/plug-in-proc.[ch]: added "gchar *menu_label". * app/plug-in/plug-in-params.[ch]: added new functions plug_in_param_defs_check() and plug_in_proc_args_check() which check if a procedure's parameters match its menu location (e.g. <Image> needs RUN-MODE, IMAGE, DRAWABLE). * app/plug-in/plug-in-message.c (plug_in_handle_proc_install): if registering an old-style (full) menu_path, use plug_in_param_defs_check(), set proc_def->menu_label otherwise. * tools/pdbgen/pdb/plug_in.pdb (plugin_menu_register): use plug_in_proc_args_check() on the passed menu_path and make sugre old and new style menu registration are not mixed. * app/pdb/plug_in_cmds.c: regenerated. * app/plug-in/plug-in-rc.c: save/restore "menu_label". * app/actions/file-dialog-actions.c * app/actions/plug-in-actions.c * app/menus/plug-in-menus.c: changed action/menu creation accordingly. Some hacks needed to allow both old and new style menu_label/menu_paths. * app/plug-in/plug-in.c * app/widgets/gimpfiledialog.c * app/xcf/xcf.c: changed accordingly. * plug-ins/common/align_layers.c * plug-ins/common/animationplay.c * plug-ins/common/animoptimize.c * plug-ins/common/apply_lens.c * plug-ins/common/autocrop.c * plug-ins/common/autostretch_hsv.c * plug-ins/common/blinds.c * plug-ins/common/blur.c * plug-ins/common/borderaverage.c * plug-ins/common/bumpmap.c * plug-ins/common/c_astretch.c * plug-ins/common/ccanalyze.c * plug-ins/common/channel_mixer.c * plug-ins/common/checkerboard.c * plug-ins/common/color_enhance.c * plug-ins/common/colorify.c * plug-ins/common/colortoalpha.c * plug-ins/common/compose.c * plug-ins/common/convmatrix.c * plug-ins/common/cubism.c * plug-ins/common/curve_bend.c * plug-ins/common/decompose.c * plug-ins/common/deinterlace.c * plug-ins/common/depthmerge.c * plug-ins/common/destripe.c * plug-ins/common/diffraction.c * plug-ins/common/displace.c * plug-ins/common/edge.c * plug-ins/common/emboss.c * plug-ins/common/engrave.c * plug-ins/common/exchange.c * plug-ins/common/film.c * plug-ins/common/flarefx.c * plug-ins/common/fractaltrace.c * plug-ins/common/screenshot.c: ported the first few plug-ins to the new registration scheme.
2004-05-07 08:30:24 +08:00
gimp_plugin_menu_register ("plug_in_c_astretch",
N_("<Image>/Layer/Colors/Auto"));
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
{
use the passed Gimp pointer instead of using "the_gimp". 2001-12-03 Michael Natterer <mitch@gimp.org> * app/devices.c: use the passed Gimp pointer instead of using "the_gimp". * app/base/temp-buf.c: indentation. * app/gui/preferences-dialog.c: prefs_toggle_callback(): fixed segfault when trying to find the prefs_dlg widget from a menu item callback (Fixes #65757). * app/gui/offset-dialog.[ch]: fixed public prototype, include the header in the .c file. * app/gui/menus.c: some menu cleanup: moved all functions which operate on the active layer/drawable to <Image>/Layer. Renamed "Layers" to "Layer". * app/display/gimpdisplayshell.c: changed menu update function accordingly. * app/gui/image-commands.[ch] * app/gui/layers-commands.[ch]: moved stuff from image-commands.* to layers-commads.*- * app/tools/gimpblendtool.c * app/tools/gimpbrightnesscontrasttool.c * app/tools/gimpcolorbalancetool.c * app/tools/gimpcurvestool.c * app/tools/gimphistogramtool.c * app/tools/gimphuesaturationtool.c * app/tools/gimplevelstool.c * app/tools/gimpposterizetool.c * app/tools/gimpthresholdtool.c * app/tools/paint_options.c * app/tools/transform_options.c * plug-ins/common/align_layers.c * plug-ins/common/autocrop.c * plug-ins/common/autostretch_hsv.c * plug-ins/common/c_astretch.c * plug-ins/common/color_enhance.c * plug-ins/common/guillotine.c * plug-ins/common/normalize.c * plug-ins/common/rotate.c * plug-ins/common/threshold_alpha.c * plug-ins/common/zealouscrop.c * plug-ins/rcm/rcm.c * plug-ins/fp/fp.c: register under <Image>/Layer, some cosmetic fixes.
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;
INIT_I18N();
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 */
if (gimp_drawable_is_rgb (drawable->drawable_id) ||
gimp_drawable_is_gray (drawable->drawable_id))
1997-11-25 06:05:25 +08:00
{
gimp_progress_init (_("Auto-Stretching Contrast..."));
1997-11-25 06:05:25 +08:00
gimp_tile_cache_ntiles (2 * (drawable->width / gimp_tile_width () + 1));
c_astretch (drawable);
if (run_mode != GIMP_RUN_NONINTERACTIVE)
gimp_displays_flush ();
1997-11-25 06:05:25 +08:00
}
else if (gimp_drawable_is_indexed (drawable->drawable_id))
1997-11-25 06:05:25 +08:00
{
indexed_c_astretch (image_ID);
if (run_mode != GIMP_RUN_NONINTERACTIVE)
gimp_displays_flush ();
1997-11-25 06:05:25 +08:00
}
else
{
/* gimp_message ("c_astretch: cannot operate on indexed color images"); */
status = GIMP_PDB_EXECUTION_ERROR;
1997-11-25 06:05:25 +08:00
}
*nreturn_vals = 1;
*return_vals = values;
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
indexed_c_astretch (gint32 image_ID)
1997-11-25 06:05:25 +08:00
{
guchar *cmap;
gint ncols, i;
gint rhi = 0, ghi = 0, bhi = 0, rlo = 255, glo = 255, blo = 255;
1997-11-25 06:05:25 +08:00
cmap = gimp_image_get_cmap (image_ID, &ncols);
if (!cmap)
1997-11-25 06:05:25 +08:00
{
g_message (_("c_astretch: cmap was NULL! Quitting...\n"));
1997-11-25 06:05:25 +08:00
gimp_quit();
}
for (i = 0; i < ncols; i++)
1997-11-25 06:05:25 +08:00
{
if (cmap[i * 3 + 0] > rhi) rhi = cmap[i * 3 + 0];
if (cmap[i * 3 + 1] > ghi) ghi = cmap[i * 3 + 1];
if (cmap[i * 3 + 2] > bhi) bhi = cmap[i * 3 + 2];
if (cmap[i * 3 + 0] < rlo) rlo = cmap[i * 3 + 0];
if (cmap[i * 3 + 1] < glo) glo = cmap[i * 3 + 1];
if (cmap[i * 3 + 2] < blo) blo = cmap[i * 3 + 2];
1997-11-25 06:05:25 +08:00
}
for (i = 0; i < ncols; i++)
1997-11-25 06:05:25 +08:00
{
if (rhi != rlo)
cmap[i * 3 + 0] = (255 * (cmap[i * 3 + 0] - rlo)) / (rhi - rlo);
if (ghi != glo)
cmap[i * 3 + 1] = (255 * (cmap[i * 3 + 1] - glo)) / (ghi - glo);
if (rhi != rlo)
cmap[i * 3 + 2] = (255 * (cmap[i * 3 + 2] - blo)) / (bhi - blo);
1997-11-25 06:05:25 +08:00
}
gimp_image_set_cmap (image_ID, cmap, ncols);
}
typedef struct {
gint alpha;
guchar lut[256][3];
guchar min[3];
guchar max[3];
gboolean has_alpha;
} AutoStretchParam_t;
1997-11-25 06:05:25 +08:00
static void
find_min_max (const guchar *src,
gint bpp,
gpointer data)
1997-11-25 06:05:25 +08:00
{
AutoStretchParam_t *param = data;
gint b;
1997-11-25 06:05:25 +08:00
for (b = 0; b < param->alpha; b++)
1997-11-25 06:05:25 +08:00
{
if (!param->has_alpha || src[param->alpha])
{
if (src[b] < param->min[b])
param->min[b] = src[b];
if (src[b] > param->max[b])
param->max[b] = src[b];
}
1997-11-25 06:05:25 +08:00
}
}
1997-11-25 06:05:25 +08:00
static void
c_astretch_func (const guchar *src,
guchar *dest,
gint bpp,
gpointer data)
{
AutoStretchParam_t *param = data;
gint b;
for (b = 0; b < param->alpha; b++)
dest[b] = param->lut[src[b]][b];
if (param->has_alpha)
dest[param->alpha] = src[param->alpha];
}
1997-11-25 06:05:25 +08:00
static void
c_astretch (GimpDrawable *drawable)
{
AutoStretchParam_t param;
gint b;
1997-11-25 06:05:25 +08:00
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
/* Get minimum and maximum values for each channel */
param.min[0] = param.min[1] = param.min[2] = 255;
param.max[0] = param.max[1] = param.max[2] = 0;
1997-11-25 06:05:25 +08:00
gimp_rgn_iterate1 (drawable, run_mode, find_min_max, &param);
1997-11-25 06:05:25 +08:00
/* Calculate LUTs with stretched contrast */
for (b = 0; b < param.alpha; b++)
{
gint range = param.max[b] - param.min[b];
gint x;
1997-11-25 06:05:25 +08:00
if (range != 0)
for (x = param.min[b]; x <= param.max[b]; x++)
param.lut[x][b] = 255 * (x - param.min[b]) / range;
else
param.lut[param.min[b]][b] = param.min[b];
1997-11-25 06:05:25 +08:00
}
gimp_rgn_iterate2 (drawable, run_mode, c_astretch_func, &param);
1997-11-25 06:05:25 +08:00
}