gimp/app/core/gimpdrawable-desaturate.c

243 lines
6.7 KiB
C
Raw Normal View History

/* 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
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
1997-11-25 06:05:25 +08:00
*/
#include "config.h"
#include <gegl.h>
#include "libgimpcolor/gimpcolor.h"
#include "core-types.h"
#include "base/pixel-processor.h"
new directory app/base/ 2001-05-15 Michael Natterer <mitch@gimp.org> * configure.in: new directory app/base/ * app/Makefile.am * app/boundary.[ch] * app/brush_scale.[ch] * app/gimpchecks.h * app/gimplut.[ch] * app/pixel_processor.[ch] * app/pixel_region.[ch] * app/pixel_surround.[ch] * app/temp_buf.[ch] * app/tile.[ch] * app/tile_cache.[ch] * app/tile_manager.[ch] * app/tile_manager_pvt.h * app/tile_pvt.h * app/tile_swap.[ch]: moved to base/ * app/base/Makefile.am * app/base/base-types.h * app/base/*: new directory for the sub-object pixel maniplation and storage stuff. Does not include Gtk+ or anything outside base/. Did some cleanup in all files. * app/appenums.h * app/apptypes.h * app/core/gimpimage.h: removed types which are now in base/base-types.h. * app/base/base-config.[ch] * app/gimprc.[ch]: put the config variables for base/ to their own file so base/ doesn not have to include gimprc.h (does not yet work, i.e. the variables are un-configurable right now) * app/main.c: set a log handler for "Gimp-Base". * app/paint-funcs/Makefile.am * app/paint-funcs/paint-funcs.[ch]: removed the color hash which maps RGB to color indices because it's a totally standalone system which has nothing to do with the paint-funcs and introduced a GimpImage dependency. paint-funcs/ should be considered on the same sub-object (glib-only) level as base/, only in a different directory. * app/core/Makefile.am * app/core/gimpimage-colorhash.[ch]: put the color hash here. * app/gimage.c: don't invalidate the color hash here... * app/core/gimpimage.c: ... but in the colormap_changed() default inplementation. Initialize the hash in class_init(). * tools/pdbgen/Makefile.am: scan app/base/base-types.h for enums. * tools/pdbgen/enums.pl: regenerated. * app/[lots] * app/core/[of] * app/gui/[files] * app/pdb/[all] * app/tools/[over] * app/widgets/[the] * tools/pdbgen/pdb/[place]: changed #includes accordingly. And use base_config->value instead of the stuff from gimprc.h.
2001-05-15 19:25:25 +08:00
#include "base/pixel-region.h"
/* temp */
#include "gimp.h"
#include "gimpimage.h"
#include "gimpdrawable.h"
#include "gimpdrawable-desaturate.h"
#include "gimpdrawable-operation.h"
1997-11-25 06:05:25 +08:00
#include "gimp-intl.h"
Moved the undo step implementations to the core and pass around lots of 2003-02-14 Michael Natterer <mitch@gimp.org> Moved the undo step implementations to the core and pass around lots of "const gchar *undo_desc". Fixes bug #104367. * app/Makefile.am * app/undo.[ch]: removed... * app/core/Makefile.am * app/core/gimpimage-undo-push.[ch]: ...and added here. * app/paint/Makefile.am * app/tools/Makefile.am * app/paint/gimppaintcore-undo.[ch] * app/tools/gimptransformtool-undo.[ch]: new files for the paint and transform undos. * app/core/gimppaintinfo.[ch]: added a blurb. * app/paint/gimpairbrush.c * app/paint/gimpclone.c * app/paint/gimpconvolve.c * app/paint/gimpdodgeburn.c * app/paint/gimperaser.c * app/paint/gimppaintbrush.c * app/paint/gimppaintcore.c * app/paint/gimppencil.c * app/paint/gimpsmudge.c * app/paint/paint-types.h * app/paint/paint.c: pass the blurb when registering the core. * app/core/gimpdrawable.[ch] * app/core/gimpimage.[ch] * app/core/gimpimage-mask-select.[ch] * app/core/gimpimage-mask.[ch] * app/core/gimpimagemap.[ch] * app/core/gimplayer-floating-sel.[ch]: added "undo_desc" parameters to all undo pushing helper functions. * app/undo_history.c * app/core/gimpchannel.c * app/core/gimpdrawable-blend.c * app/core/gimpdrawable-bucket-fill.c * app/core/gimpdrawable-desaturate.c * app/core/gimpdrawable-equalize.c * app/core/gimpdrawable-invert.c * app/core/gimpdrawable-offset.c * app/core/gimpdrawable-transform.c * app/core/gimpedit.c * app/core/gimpimage-convert.c * app/core/gimpimage-crop.c * app/core/gimpimage-guides.c * app/core/gimpimage-merge.c * app/core/gimpimage-qmask.c * app/core/gimpimage-resize.c * app/core/gimpimage-scale.c * app/core/gimpimage-undo.c * app/core/gimpitem.c * app/core/gimplayer.c * app/core/gimplayermask.c * app/display/gimpdisplayshell-dnd.c * app/file/file-open.c * app/file/file-save.c * app/gui/channels-commands.c * app/gui/file-commands.c * app/gui/file-open-dialog.c * app/gui/image-commands.c * app/gui/layers-commands.c * app/gui/paths-dialog.c * app/gui/select-commands.c * app/gui/vectors-commands.c * app/text/gimptext-compat.c * app/tools/gimpbezierselecttool.c * app/tools/gimpbucketfilltool.c * app/tools/gimpeditselectiontool.c * app/tools/gimpfreeselecttool.c * app/tools/gimpfuzzyselecttool.c * app/tools/gimpimagemaptool.c * app/tools/gimpinktool.c * app/tools/gimpiscissorstool.c * app/tools/gimpmeasuretool.c * app/tools/gimpmovetool.c * app/tools/gimppainttool.c * app/tools/gimprectselecttool.c * app/tools/gimptexttool.c * app/tools/gimptransformtool.c * app/widgets/gimpchannellistview.c * app/widgets/gimpdrawablelistview.c * app/widgets/gimpselectioneditor.c * tools/pdbgen/pdb/color.pdb * tools/pdbgen/pdb/drawable.pdb * tools/pdbgen/pdb/guides.pdb * tools/pdbgen/pdb/layer.pdb * tools/pdbgen/pdb/selection.pdb * tools/pdbgen/pdb/selection_tools.pdb: changed accordingly: pass "undo_desc" strings, changed includes or simply removed inclusion of "undo.h". Some random cleanups. * tools/pdbgen/pdb/guides.pdb: cleaned up a lot. Fixed gimp_image_find_next_guide() to not return guides with position < 0 (and made it shorter and readable). * app/pdb/color_cmds.c * app/pdb/drawable_cmds.c * app/pdb/guides_cmds.c * app/pdb/layer_cmds.c * app/pdb/selection_cmds.c * app/pdb/selection_tools_cmds.c: regenerated.
2003-02-14 22:14:29 +08:00
1997-11-25 06:05:25 +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);
void
gimp_drawable_desaturate (GimpDrawable *drawable,
GimpDesaturateMode mode)
1997-11-25 06:05:25 +08:00
{
g_return_if_fail (GIMP_IS_DRAWABLE (drawable));
g_return_if_fail (gimp_drawable_is_rgb (drawable));
g_return_if_fail (gimp_item_is_attached (GIMP_ITEM (drawable)));
if (gimp_use_gegl (GIMP_ITEM (drawable)->image->gimp))
{
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);
}
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;
break;
case GIMP_DESATURATE_LUMINOSITY:
function = (PixelProcessorFunc) desaturate_region_luminosity;
break;
case GIMP_DESATURATE_AVERAGE:
function = (PixelProcessorFunc) desaturate_region_average;
break;
1997-11-25 06:05:25 +08:00
default:
g_return_if_reached ();
return;
}
1997-11-25 06:05:25 +08:00
has_alpha = gimp_drawable_has_alpha (drawable);
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
}
static void
desaturate_region_lightness (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 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;
}
}
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++)
{
gint luminosity = GIMP_RGB_LUMINANCE (s[RED_PIX],
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;
}
}