2006-12-10 05:33:38 +08:00
|
|
|
/* GIMP - The GNU Image Manipulation Program
|
1997-11-25 06:05:25 +08:00
|
|
|
* Copyright (C) 1995 Spencer Kimball and Peter Mattis
|
|
|
|
*
|
|
|
|
* 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
|
|
|
*/
|
2000-12-17 05:37:03 +08:00
|
|
|
|
|
|
|
#include "config.h"
|
|
|
|
|
2008-01-04 02:54:58 +08:00
|
|
|
#include <gegl.h>
|
2000-12-17 05:37:03 +08:00
|
|
|
|
2005-04-16 22:51:49 +08:00
|
|
|
#include "libgimpcolor/gimpcolor.h"
|
|
|
|
|
2001-05-10 06:34:59 +08:00
|
|
|
#include "core-types.h"
|
2000-12-17 05:37:03 +08:00
|
|
|
|
2005-02-16 07:53:56 +08:00
|
|
|
#include "base/pixel-processor.h"
|
2001-05-15 19:25:25 +08:00
|
|
|
#include "base/pixel-region.h"
|
|
|
|
|
2008-01-05 01:28:49 +08:00
|
|
|
/* temp */
|
|
|
|
#include "gimp.h"
|
|
|
|
#include "gimpimage.h"
|
|
|
|
|
2001-05-09 10:32:03 +08:00
|
|
|
#include "gimpdrawable.h"
|
2001-04-19 08:23:43 +08:00
|
|
|
#include "gimpdrawable-desaturate.h"
|
2008-01-04 02:54:58 +08:00
|
|
|
#include "gimpdrawable-operation.h"
|
1997-11-25 06:05:25 +08:00
|
|
|
|
2003-03-26 00:38:19 +08:00
|
|
|
#include "gimp-intl.h"
|
2003-02-14 22:14:29 +08:00
|
|
|
|
1997-11-25 06:05:25 +08:00
|
|
|
|
2005-04-16 22:51:49 +08:00
|
|
|
static void desaturate_region_lightness (gpointer data,
|
|
|
|
PixelRegion *srcPR,
|
|
|
|
PixelRegion *destPR);
|
|
|
|
|
|
|
|
static void desaturate_region_luminosity (gpointer data,
|
|
|
|
PixelRegion *srcPR,
|
|
|
|
PixelRegion *destPR);
|
|
|
|
|
|
|
|
static void desaturate_region_average (gpointer data,
|
|
|
|
PixelRegion *srcPR,
|
|
|
|
PixelRegion *destPR);
|
2005-02-16 07:53:56 +08:00
|
|
|
|
|
|
|
|
moved a bunch of PDB stuff here
* app/color_cmds.c: moved a bunch of PDB stuff here
* app/color_balance.[ch]: removed PDB proc, exported TransferMode
enum, ColorBalanceDialog, color_balance_create_lookup_tables, and
color_balance
* app/curves.[ch]: removed PDB procs, exported SMOOTH and GFREE
#defines, CurvesDialog, curves_lut_func and curves_calculate_curve
* app/desaturate.[ch]: removed PDB proc, exported desaturate
* app/equalize.[ch]: removed PDB proc, exported equalize
* app/histogram_tool.[ch]: removed PDB proc, exported HISTOGRAM_WIDTH
and HISTOGRAM_HEIGHT #defines, HistogramToolDialog,
histogram_tool_histogram_range
* app/hue_saturation.[ch]: removed PDB proc, exported HueRange enum,
HueSaturationDialog, hue_saturation_calculate_transfers,
hue_saturation
* app/invert.[ch]: remove PDB proc, export invert
* app/threshold.[ch]: remove PDB proc, export ThresholdDialog and
threshold_2
* internal_procs.c: changes for pdbgen
* app/gimprc.c: removed leftover declaration
* app/image_map.h: add #include "gimpdrawableF.h"
* app/lut_funcs.h: add ALPHA_LUT to ChannelLutType
-Yosh
1999-04-21 13:39:57 +08:00
|
|
|
void
|
2005-04-16 22:51:49 +08:00
|
|
|
gimp_drawable_desaturate (GimpDrawable *drawable,
|
|
|
|
GimpDesaturateMode mode)
|
1997-11-25 06:05:25 +08:00
|
|
|
{
|
2001-04-19 08:23:43 +08:00
|
|
|
g_return_if_fail (GIMP_IS_DRAWABLE (drawable));
|
2003-04-14 02:45:38 +08:00
|
|
|
g_return_if_fail (gimp_drawable_is_rgb (drawable));
|
2004-11-16 21:41:55 +08:00
|
|
|
g_return_if_fail (gimp_item_is_attached (GIMP_ITEM (drawable)));
|
1998-01-22 15:02:57 +08:00
|
|
|
|
2008-01-22 19:28:48 +08:00
|
|
|
if (gimp_use_gegl (GIMP_ITEM (drawable)->image->gimp))
|
2005-04-16 22:51:49 +08:00
|
|
|
{
|
2008-01-04 02:54:58 +08:00
|
|
|
GeglNode *desaturate;
|
|
|
|
|
|
|
|
desaturate = g_object_new (GEGL_TYPE_NODE,
|
|
|
|
"operation", "gimp-desaturate",
|
|
|
|
NULL);
|
|
|
|
|
|
|
|
gegl_node_set (desaturate,
|
|
|
|
"mode", mode,
|
|
|
|
NULL);
|
|
|
|
|
|
|
|
gimp_drawable_apply_operation (drawable, desaturate, TRUE,
|
|
|
|
NULL, _("Desaturate"));
|
|
|
|
|
|
|
|
g_object_unref (desaturate);
|
2005-04-16 22:51:49 +08:00
|
|
|
}
|
2008-01-04 02:54:58 +08:00
|
|
|
else
|
|
|
|
{
|
|
|
|
PixelRegion srcPR, destPR;
|
|
|
|
PixelProcessorFunc function;
|
|
|
|
gint x, y;
|
|
|
|
gint width, height;
|
|
|
|
gboolean has_alpha;
|
|
|
|
|
|
|
|
if (! gimp_drawable_mask_intersect (drawable, &x, &y, &width, &height))
|
|
|
|
return;
|
|
|
|
|
|
|
|
switch (mode)
|
|
|
|
{
|
|
|
|
case GIMP_DESATURATE_LIGHTNESS:
|
|
|
|
function = (PixelProcessorFunc) desaturate_region_lightness;
|
|
|
|
break;
|
2005-04-16 22:51:49 +08:00
|
|
|
|
2008-01-04 02:54:58 +08:00
|
|
|
break;
|
|
|
|
case GIMP_DESATURATE_LUMINOSITY:
|
|
|
|
function = (PixelProcessorFunc) desaturate_region_luminosity;
|
|
|
|
break;
|
2005-04-16 22:51:49 +08:00
|
|
|
|
2008-01-04 02:54:58 +08:00
|
|
|
case GIMP_DESATURATE_AVERAGE:
|
|
|
|
function = (PixelProcessorFunc) desaturate_region_average;
|
|
|
|
break;
|
1997-11-25 06:05:25 +08:00
|
|
|
|
2008-01-04 02:54:58 +08:00
|
|
|
default:
|
|
|
|
g_return_if_reached ();
|
|
|
|
return;
|
|
|
|
}
|
1997-11-25 06:05:25 +08:00
|
|
|
|
2008-01-04 02:54:58 +08:00
|
|
|
has_alpha = gimp_drawable_has_alpha (drawable);
|
2001-04-19 08:23:43 +08:00
|
|
|
|
2008-01-04 02:54:58 +08:00
|
|
|
pixel_region_init (&srcPR, gimp_drawable_get_tiles (drawable),
|
|
|
|
x, y, width, height, FALSE);
|
|
|
|
pixel_region_init (&destPR, gimp_drawable_get_shadow_tiles (drawable),
|
|
|
|
x, y, width, height, TRUE);
|
|
|
|
|
|
|
|
pixel_regions_process_parallel (function, GINT_TO_POINTER (has_alpha),
|
|
|
|
2, &srcPR, &destPR);
|
|
|
|
|
|
|
|
gimp_drawable_merge_shadow (drawable, TRUE, _("Desaturate"));
|
|
|
|
|
|
|
|
gimp_drawable_update (drawable, x, y, width, height);
|
|
|
|
}
|
1997-11-25 06:05:25 +08:00
|
|
|
}
|
2005-02-16 07:53:56 +08:00
|
|
|
|
|
|
|
static void
|
2005-04-16 22:51:49 +08:00
|
|
|
desaturate_region_lightness (gpointer data,
|
|
|
|
PixelRegion *srcPR,
|
|
|
|
PixelRegion *destPR)
|
2005-02-16 07:53:56 +08:00
|
|
|
{
|
|
|
|
const guchar *src = srcPR->data;
|
|
|
|
guchar *dest = destPR->data;
|
|
|
|
gint h = srcPR->h;
|
|
|
|
gboolean has_alpha = GPOINTER_TO_INT (data);
|
|
|
|
|
|
|
|
while (h--)
|
|
|
|
{
|
|
|
|
const guchar *s = src;
|
|
|
|
guchar *d = dest;
|
|
|
|
gint j;
|
|
|
|
|
|
|
|
for (j = 0; j < srcPR->w; j++)
|
|
|
|
{
|
|
|
|
gint min, max;
|
|
|
|
gint lightness;
|
|
|
|
|
|
|
|
max = MAX (s[RED_PIX], s[GREEN_PIX]);
|
|
|
|
max = MAX (max, s[BLUE_PIX]);
|
|
|
|
min = MIN (s[RED_PIX], s[GREEN_PIX]);
|
|
|
|
min = MIN (min, s[BLUE_PIX]);
|
|
|
|
|
|
|
|
lightness = (max + min) / 2;
|
|
|
|
|
|
|
|
d[RED_PIX] = lightness;
|
|
|
|
d[GREEN_PIX] = lightness;
|
|
|
|
d[BLUE_PIX] = lightness;
|
|
|
|
|
|
|
|
if (has_alpha)
|
|
|
|
d[ALPHA_PIX] = s[ALPHA_PIX];
|
|
|
|
|
|
|
|
d += destPR->bytes;
|
|
|
|
s += srcPR->bytes;
|
|
|
|
}
|
|
|
|
|
|
|
|
src += srcPR->rowstride;
|
|
|
|
dest += destPR->rowstride;
|
|
|
|
}
|
|
|
|
}
|
2005-04-16 22:51:49 +08:00
|
|
|
|
|
|
|
static void
|
|
|
|
desaturate_region_luminosity (gpointer data,
|
|
|
|
PixelRegion *srcPR,
|
|
|
|
PixelRegion *destPR)
|
|
|
|
{
|
|
|
|
const guchar *src = srcPR->data;
|
|
|
|
guchar *dest = destPR->data;
|
|
|
|
gint h = srcPR->h;
|
|
|
|
gboolean has_alpha = GPOINTER_TO_INT (data);
|
|
|
|
|
|
|
|
while (h--)
|
|
|
|
{
|
|
|
|
const guchar *s = src;
|
|
|
|
guchar *d = dest;
|
|
|
|
gint j;
|
|
|
|
|
|
|
|
for (j = 0; j < srcPR->w; j++)
|
|
|
|
{
|
2005-08-03 08:36:41 +08:00
|
|
|
gint luminosity = GIMP_RGB_LUMINANCE (s[RED_PIX],
|
2005-04-16 22:51:49 +08:00
|
|
|
s[GREEN_PIX],
|
|
|
|
s[BLUE_PIX]) + 0.5;
|
|
|
|
|
|
|
|
d[RED_PIX] = luminosity;
|
|
|
|
d[GREEN_PIX] = luminosity;
|
|
|
|
d[BLUE_PIX] = luminosity;
|
|
|
|
|
|
|
|
if (has_alpha)
|
|
|
|
d[ALPHA_PIX] = s[ALPHA_PIX];
|
|
|
|
|
|
|
|
d += destPR->bytes;
|
|
|
|
s += srcPR->bytes;
|
|
|
|
}
|
|
|
|
|
|
|
|
src += srcPR->rowstride;
|
|
|
|
dest += destPR->rowstride;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
desaturate_region_average (gpointer data,
|
|
|
|
PixelRegion *srcPR,
|
|
|
|
PixelRegion *destPR)
|
|
|
|
{
|
|
|
|
const guchar *src = srcPR->data;
|
|
|
|
guchar *dest = destPR->data;
|
|
|
|
gint h = srcPR->h;
|
|
|
|
gboolean has_alpha = GPOINTER_TO_INT (data);
|
|
|
|
|
|
|
|
while (h--)
|
|
|
|
{
|
|
|
|
const guchar *s = src;
|
|
|
|
guchar *d = dest;
|
|
|
|
gint j;
|
|
|
|
|
|
|
|
for (j = 0; j < srcPR->w; j++)
|
|
|
|
{
|
|
|
|
gint average = (s[RED_PIX] + s[GREEN_PIX] + s[BLUE_PIX] + 1) / 3;
|
|
|
|
|
|
|
|
d[RED_PIX] = average;
|
|
|
|
d[GREEN_PIX] = average;
|
|
|
|
d[BLUE_PIX] = average;
|
|
|
|
|
|
|
|
if (has_alpha)
|
|
|
|
d[ALPHA_PIX] = s[ALPHA_PIX];
|
|
|
|
|
|
|
|
d += destPR->bytes;
|
|
|
|
s += srcPR->bytes;
|
|
|
|
}
|
|
|
|
|
|
|
|
src += srcPR->rowstride;
|
|
|
|
dest += destPR->rowstride;
|
|
|
|
}
|
|
|
|
}
|