gimp/plug-ins/common/color_enhance.c

292 lines
6.8 KiB
C
Raw Normal View History

1999-09-03 23:14:44 +00:00
/* Color Enhance 0.10 --- image filter plug-in for The Gimp image
* manipulation program
*
* Copyright (C) 1999 Martin Weber
* Copyright (C) 1996 Federico Mena Quintero
*
* You can contact me at martweb@gmx.net
1999-09-03 23:14:44 +00:00
* 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.
*/
#include "config.h"
#include <libgimp/gimp.h>
#include "libgimp/stdplugins-intl.h"
#define PLUG_IN_PROC "plug-in-color-enhance"
1999-09-03 23:14:44 +00: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-03 17:59:48 +00:00
static void query (void);
static void run (const gchar *name,
gint nparams,
const GimpParam *param,
gint *nreturn_vals,
GimpParam **return_vals);
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-03 17:59:48 +00:00
static void Color_Enhance (GimpDrawable *drawable);
static void indexed_Color_Enhance (gint32 image_ID);
1999-09-03 23:14:44 +00:00
const GimpPlugInInfo PLUG_IN_INFO =
1999-09-03 23:14:44 +00:00
{
NULL, /* init_proc */
NULL, /* quit_proc */
query, /* query_proc */
run, /* run_proc */
1999-09-03 23:14:44 +00:00
};
MAIN ()
static void
query (void)
1999-09-03 23:14:44 +00:00
{
static const GimpParamDef args[] =
1999-09-03 23:14:44 +00:00
{
{ GIMP_PDB_INT32, "run-mode", "Interactive, non-interactive" },
{ GIMP_PDB_IMAGE, "image", "Input image" },
{ GIMP_PDB_DRAWABLE, "drawable", "Input drawable" }
1999-09-03 23:14:44 +00:00
};
gimp_install_procedure (PLUG_IN_PROC,
N_("Stretch color saturation to cover maximum possible range"),
"This simple plug-in does an automatic saturation "
"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 range. For some images it may do just what "
"you want; for others it may not work that well. "
"This version differs from Contrast Autostretch in "
"that it works in HSV space, and preserves hue.",
1999-09-03 23:14:44 +00:00
"Martin Weber",
"Martin Weber",
"1997",
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 00:30:24 +00:00
N_("_Color Enhance"),
1999-09-03 23:14:44 +00:00
"RGB*, INDEXED*",
GIMP_PLUGIN,
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-03 17:59:48 +00:00
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 00:30:24 +00:00
gimp_plugin_menu_register (PLUG_IN_PROC, "<Image>/Colors/Auto");
Reorganized the core menu items (everything except <Image>/Filters). 1999-11-20 Michael Natterer <mitch@gimp.org> Reorganized the core menu items (everything except <Image>/Filters). Everything is of course trivial to change again, so please comment on the new "menu feeling" ;-) * app/menus.[ch]: - Applied the suggestions collected by Olof. - Added "..." to all items which open a dialog. - Introduced some additional separators (e.g. in "Dialogs"). - Reorder some plugins and the color correct tools after initialisation. - A menu entry to invoke the tooltips inspector. - A debugging menu entry which dumps the menu paths and their help pages (will of course go away when the help sys is consistent). There are currently two identical "Help" menus because <Toolbox>/Help trashes the menu bar if the toolbox is too narrow (gtk doesn't seem to support multi-line menubars, any idea?) * app/app_procs.c: call menus_reorder_plugins() after loading the plugins to beautify the "Xtns" menu. * app/commands.[ch]: reordered some functions to match the new menu structure (for easier source navigation) and renamed some to be consistent (e.g. all help functions are now called help_*). Show/Hide the rulers with ordinary gtk_widget_[show|hide]() commands. I've tested it several times and it looks exactly the same as the old code which used internal gtk knowledge. * app/gdisplay.c: applied the menu changes to gdisplay_set_menu_sensitivity(). * app/gimphelp.[ch]: new public function gimp_context_help() which invokes the tooltips inspector. Code cleanup. * app/resize.c: changed the dialogs' titles to match the menu entries. * app/session.c: renamed the gradient selection cmd callback to be consistent with brushes/patterns. * app/tools.c: added "..." to the menu paths of the tools which have dialogs. * app/fileops.c * app/channels_dialog.c * app/layers_dialog.c * app/paths_dialog.c: added some "...". * plug-ins/common/align_layers.c * plug-ins/common/autostretch_hsv.c * plug-ins/common/c_astretch.c * plug-ins/common/color_enhance.c * plug-ins/common/compose.c * plug-ins/common/decompose.c * plug-ins/common/mail.c * plug-ins/common/normalize.c * plug-ins/common/threshold_alpha.c * plug-ins/dbbrowser/dbbrowser.c * plug-ins/fp/fp.c * plug-ins/print/print.c * plug-ins/rcm/rcm.c: changed the menu paths and added "...".
1999-11-20 12:12:41 +00:00
}
1999-09-03 23:14:44 +00:00
static void
run (const gchar *name,
gint nparams,
const GimpParam *param,
gint *nreturn_vals,
GimpParam **return_vals)
1999-09-03 23:14:44 +00: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-03 17:59:48 +00:00
static GimpParam values[1];
GimpDrawable *drawable;
GimpPDBStatusType status = GIMP_PDB_SUCCESS;
GimpRunMode run_mode;
gint32 image_ID;
1999-09-03 23:14:44 +00:00
INIT_I18N();
1999-09-03 23:14:44 +00: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))
1999-09-03 23:14:44 +00:00
{
gimp_progress_init (_("Color Enhance"));
1999-09-03 23:14:44 +00:00
gimp_tile_cache_ntiles (2 * (drawable->width / gimp_tile_width () + 1));
Color_Enhance (drawable);
if (run_mode != GIMP_RUN_NONINTERACTIVE)
1999-09-03 23:14:44 +00:00
gimp_displays_flush ();
}
else if (gimp_drawable_is_indexed (drawable->drawable_id))
1999-09-03 23:14:44 +00:00
{
indexed_Color_Enhance (image_ID);
if (run_mode != GIMP_RUN_NONINTERACTIVE)
1999-09-03 23:14:44 +00:00
gimp_displays_flush ();
}
else
{
status = GIMP_PDB_EXECUTION_ERROR;
1999-09-03 23:14:44 +00:00
}
*nreturn_vals = 1;
*return_vals = values;
1999-09-03 23:14:44 +00:00
values[0].type = GIMP_PDB_STATUS;
1999-09-03 23:14:44 +00:00
values[0].data.d_status = status;
gimp_drawable_detach (drawable);
}
static gdouble
get_v (const guchar *src)
{
gdouble h, z, v;
gint c, m, y;
gint k;
guchar map[3];
c = 255 - src[0];
m = 255 - src[1];
y = 255 - src[2];
k = c;
if (m < k) k = m;
if (y < k) k = y;
map[0] = c - k;
map[1] = m - k;
map[2] = y - k;
gimp_rgb_to_hsv4(map, &h, &z, &v);
return v;
}
1999-09-03 23:14:44 +00:00
static void
enhance_it (const guchar *src, guchar *dest, gdouble vlo, gdouble vhi)
{
gdouble h, z, v;
gint c, m, y;
gint k;
guchar map[3];
c = 255 - src[0];
m = 255 - src[1];
y = 255 - src[2];
k = c;
if (m < k) k = m;
if (y < k) k = y;
map[0] = c - k;
map[1] = m - k;
map[2] = y - k;
gimp_rgb_to_hsv4 (map, &h, &z, &v);
if (vhi != vlo)
v = (v - vlo) / (vhi - vlo);
gimp_hsv_to_rgb4 (map, h, z, v);
c = map[0];
m = map[1];
y = map[2];
c += k;
if (c > 255) c = 255;
m += k;
if (m > 255) m = 255;
y += k;
if (y > 255) y = 255;
dest[0] = 255 - c;
dest[1] = 255 - m;
dest[2] = 255 - y;
}
static void
indexed_Color_Enhance (gint32 image_ID)
1999-09-03 23:14:44 +00:00
{
guchar *cmap;
gint ncols,i;
gdouble vhi = 0.0, vlo = 1.0;
1999-09-03 23:14:44 +00:00
cmap = gimp_image_get_colormap (image_ID, &ncols);
1999-09-03 23:14:44 +00:00
if (!cmap)
1999-09-03 23:14:44 +00:00
{
g_message ("colormap was NULL! Quitting.");
1999-09-03 23:14:44 +00:00
gimp_quit();
}
for (i = 0; i < ncols; i++)
1999-09-03 23:14:44 +00:00
{
gdouble v = get_v (&cmap[3 * i]);
1999-09-03 23:14:44 +00:00
if (v > vhi) vhi = v;
if (v < vlo) vlo = v;
}
for (i = 0; i < ncols; i++)
1999-09-03 23:14:44 +00:00
{
enhance_it (&cmap[3 * i], &cmap[3 * i], vlo, vhi);
1999-09-03 23:14:44 +00:00
}
gimp_image_set_colormap (image_ID, cmap, ncols);
1999-09-03 23:14:44 +00:00
}
typedef struct
{
gdouble vhi;
gdouble vlo;
gboolean has_alpha;
} ColorEnhanceParam_t;
1999-09-03 23:14:44 +00:00
static void
find_vhi_vlo (const guchar *src,
gint bpp,
gpointer data)
1999-09-03 23:14:44 +00:00
{
ColorEnhanceParam_t *param = (ColorEnhanceParam_t*) data;
if (!param->has_alpha || src[3])
1999-09-03 23:14:44 +00:00
{
gdouble v = get_v (src);
if (v > param->vhi) param->vhi = v;
if (v < param->vlo) param->vlo = v;
1999-09-03 23:14:44 +00:00
}
}
1999-09-03 23:14:44 +00:00
static void
color_enhance_func (const guchar *src,
guchar *dest,
gint bpp,
gpointer data)
{
ColorEnhanceParam_t *param = (ColorEnhanceParam_t*) data;
enhance_it (src, dest, param->vlo, param->vhi);
1999-09-03 23:14:44 +00:00
if (param->has_alpha)
dest[3] = src[3];
1999-09-03 23:14:44 +00:00
}
static void
Color_Enhance (GimpDrawable *drawable)
{
ColorEnhanceParam_t param;
param.has_alpha = gimp_drawable_has_alpha (drawable->drawable_id);
param.vhi = 0.0;
param.vlo = 1.0;
gimp_rgn_iterate1 (drawable, 0 /* unused */, find_vhi_vlo, &param);
gimp_rgn_iterate2 (drawable, 0 /* unused */, color_enhance_func, &param);
}