gimp/plug-ins/common/color_enhance.c

295 lines
7 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.
*/
/* 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 be total crap :) This version operates in HSV space
and preserves hue. Most code is taken from autostretch_hsv */
#include "config.h"
1999-09-03 23:14:44 +00:00
#include <stdlib.h>
#include <stdio.h>
#include <libgimp/gimp.h>
#include "libgimp/stdplugins-intl.h"
static GimpRunMode run_mode;
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 (gchar *name,
gint nparams,
GimpParam *param,
gint *nreturn_vals,
GimpParam **return_vals);
static void Color_Enhance (GimpDrawable *drawable);
static void indexed_Color_Enhance (gint32 image_ID);
1999-09-03 23:14:44 +00:00
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 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
};
2000-01-31 02:32:30 +00:00
gimp_install_procedure ("plug_in_color_enhance",
"Automatically stretch the saturation of the "
"specified drawable to cover all possible ranges.",
"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 be total crap :). "
"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",
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
N_("<Image>/Layer/Colors/Auto/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);
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
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
run (gchar *name,
gint nparams,
GimpParam *param,
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
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;
1999-09-03 23:14:44 +00:00
gint32 image_ID;
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;
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_cmap (image_ID, &ncols);
if (!cmap)
1999-09-03 23:14:44 +00:00
{
printf("Color_Enhance: cmap was NULL! Quitting...\n");
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_cmap (image_ID, cmap, ncols);
}
typedef struct {
gdouble vhi;
gdouble vlo;
gboolean has_alpha;
} ColorEnhanceParam_t;
1999-09-03 23:14:44 +00:00
static void
find_vhi_vlo (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 (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, run_mode, find_vhi_vlo, &param);
gimp_rgn_iterate2 (drawable, run_mode, color_enhance_func, &param);
}