Commit c5437ea0 authored by Sven Neumann's avatar Sven Neumann Committed by Sven Neumann
Browse files

applied patch from Daniel Richard G. which adds a new PDB entry with more

2007-06-18  Sven Neumann  <sven@gimp.org>

	* plug-ins/common/oilify.c: applied patch from Daniel Richard G.
	which adds a new PDB entry with more options and improves speed
	and quality of the algorithm (bug #347075).


svn path=/trunk/; revision=22793
parent 5c1a0855
2007-06-18 Sven Neumann <sven@gimp.org>
* plug-ins/common/oilify.c: applied patch from Daniel Richard G.
which adds a new PDB entry with more options and improves speed
and quality of the algorithm (bug #347075).
2007-06-16 Sven Neumann <sven@gimp.org>
 
* app/core/gimpprojection.c (gimp_projection_get_level): take the
......@@ -3,6 +3,7 @@
*
* Copyright (C) 1995 Spencer Kimball and Peter Mattis
* Copyright (C) 1996 Torsten Martinsen
* Copyright (C) 2007 Daniel Richard G.
*
* 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
......@@ -25,12 +26,14 @@
#include <libgimp/gimp.h>
#include <libgimp/gimpui.h>
#include <libgimpmath/gimpmath.h>
#include "libgimp/stdplugins-intl.h"
#define PLUG_IN_PROC "plug-in-oilify"
#define PLUG_IN_BINARY "oilify"
#define OILIFY_PROC "plug-in-oilify"
#define OILIFY_ENHANCED_PROC "plug-in-oilify-enhanced"
#define OILIFY_BINARY "oilify"
#define SCALE_WIDTH 125
#define HISTSIZE 256
......@@ -42,6 +45,11 @@
typedef struct
{
gdouble mask_size;
gboolean use_mask_size_map;
gint mask_size_map;
gdouble exponent;
gboolean use_exponent_map;
gint exponent_map;
gint mode;
gboolean preview;
} OilifyVals;
......@@ -49,17 +57,17 @@ typedef struct
/* Declare local functions.
*/
static void query (void);
static void run (const gchar *name,
gint nparams,
const GimpParam *param,
gint *nreturn_vals,
GimpParam **return_vals);
static void query (void);
static void run (const gchar *name,
gint nparams,
const GimpParam *param,
gint *nreturn_vals,
GimpParam **return_vals);
static void oilify (GimpDrawable *drawable,
GimpPreview *preview);
static void oilify (GimpDrawable *drawable,
GimpPreview *preview);
static gboolean oilify_dialog (GimpDrawable *drawable);
static gboolean oilify_dialog (GimpDrawable *drawable);
const GimpPlugInInfo PLUG_IN_INFO =
......@@ -72,9 +80,14 @@ const GimpPlugInInfo PLUG_IN_INFO =
static OilifyVals ovals =
{
7.0, /* mask size */
MODE_INTEN, /* mode */
TRUE /* preview */
8.0, /* mask size */
FALSE, /* use mask-size map? */
-1, /* mask-size map */
8.0, /* exponent */
FALSE, /* use exponent map? */
-1, /* exponent map */
MODE_INTEN, /* mode */
TRUE /* preview */
};
......@@ -92,11 +105,22 @@ query (void)
{ GIMP_PDB_INT32, "mode", "Algorithm {RGB (0), INTENSITY (1)}" }
};
gimp_install_procedure (PLUG_IN_PROC,
static const GimpParamDef args_enhanced[] =
{
{ GIMP_PDB_INT32, "run-mode", "Interactive, non-interactive" },
{ GIMP_PDB_IMAGE, "image", "Input image (unused)" },
{ GIMP_PDB_DRAWABLE, "drawable", "Input drawable" },
{ GIMP_PDB_INT32, "mode", "Algorithm {RGB (0), INTENSITY (1)}" },
{ GIMP_PDB_INT32, "mask-size", "Oil paint mask size" },
{ GIMP_PDB_DRAWABLE, "mask-size-map", "Mask size control map" },
{ GIMP_PDB_INT32, "exponent", "Oil paint exponent" },
{ GIMP_PDB_DRAWABLE, "exponent-map", "Exponent control map" }
};
gimp_install_procedure (OILIFY_PROC,
N_("Smear colors to simulate an oil painting"),
"This function performs the well-known oil-paint "
"effect on the specified drawable. The size of the "
"input mask is specified by 'mask_size'.",
"effect on the specified drawable.",
"Torsten Martinsen",
"Torsten Martinsen",
"1996",
......@@ -106,7 +130,20 @@ query (void)
G_N_ELEMENTS (args), 0,
args, NULL);
gimp_plugin_menu_register (PLUG_IN_PROC, "<Image>/Filters/Artistic");
gimp_plugin_menu_register (OILIFY_PROC, "<Image>/Filters/Artistic");
gimp_install_procedure (OILIFY_ENHANCED_PROC,
N_("Smear colors to simulate an oil painting"),
"This function performs the well-known oil-paint "
"effect on the specified drawable.",
"Torsten Martinsen, Daniel Richard G.",
"Torsten Martinsen, Daniel Richard G.",
"2007",
NULL,
"RGB*, GRAY*",
GIMP_PLUGIN,
G_N_ELEMENTS (args_enhanced), 0,
args_enhanced, NULL);
}
static void
......@@ -139,7 +176,7 @@ run (const gchar *name,
{
case GIMP_RUN_INTERACTIVE:
/* Possibly retrieve data */
gimp_get_data (PLUG_IN_PROC, &ovals);
gimp_get_data (OILIFY_PROC, &ovals);
/* First acquire information with a dialog */
if (! oilify_dialog (drawable))
......@@ -147,26 +184,57 @@ run (const gchar *name,
break;
case GIMP_RUN_NONINTERACTIVE:
/* Make sure all the arguments are there! */
if (nparams != 5)
/* Interpret the arguments per the name used to invoke us */
if (! strcmp (name, OILIFY_PROC))
{
status = GIMP_PDB_CALLING_ERROR;
if (nparams != 5)
{
status = GIMP_PDB_CALLING_ERROR;
}
else
{
ovals.mask_size = (gdouble) param[3].data.d_int32;
ovals.mode = param[4].data.d_int32;
}
}
else
else if (! strcmp (name, OILIFY_ENHANCED_PROC))
{
ovals.mask_size = (gdouble) param[3].data.d_int32;
ovals.mode = (gint) param[4].data.d_int32;
if (nparams < 5 || nparams > 8)
{
status = GIMP_PDB_CALLING_ERROR;
}
else
{
ovals.mode = param[3].data.d_int32;
ovals.mask_size = (gdouble) param[4].data.d_int32;
if (nparams >= 6)
ovals.mask_size_map = param[5].data.d_int32;
if (nparams >= 7)
ovals.exponent = (gdouble) param[6].data.d_int32;
if (nparams == 8)
ovals.exponent_map = param[7].data.d_int32;
if ((ovals.mask_size < 1.0) ||
((ovals.mode != MODE_INTEN) &&
(ovals.mode != MODE_RGB)))
status = GIMP_PDB_CALLING_ERROR;
ovals.use_mask_size_map = ovals.mask_size_map >= 0;
ovals.use_exponent_map = ovals.exponent_map >= 0;
if (ovals.mask_size < 1.0 ||
ovals.exponent < 1.0 ||
(ovals.mode != MODE_INTEN && ovals.mode != MODE_RGB) ||
(ovals.mode == MODE_INTEN &&
! gimp_drawable_is_rgb (drawable->drawable_id)))
{
status = GIMP_PDB_CALLING_ERROR;
}
}
}
break;
case GIMP_RUN_WITH_LAST_VALS:
/* Possibly retrieve data */
gimp_get_data (PLUG_IN_PROC, &ovals);
gimp_get_data (OILIFY_PROC, &ovals);
break;
default:
......@@ -187,7 +255,7 @@ run (const gchar *name,
/* Store data */
if (run_mode == GIMP_RUN_INTERACTIVE)
gimp_set_data (PLUG_IN_PROC, &ovals, sizeof (OilifyVals));
gimp_set_data (OILIFY_PROC, &ovals, sizeof (OilifyVals));
}
else
{
......@@ -200,123 +268,200 @@ run (const gchar *name,
gimp_drawable_detach (drawable);
}
/*
* Helper function to read a sample from a mask-size/exponent map
*/
static inline gfloat
get_map_value (guchar *src,
gint bpp)
{
gfloat value;
if (bpp >= 3)
value = GIMP_RGB_LUMINANCE (src[0], src[1], src[2]);
else
value = (gfloat) *src;
/* value should be in [0,1] */
value /= 255.0;
return value;
}
/*
* This is a special-case form of the powf() function, limited to integer
* exponents. It calculates e.g. x^13 as (x^8)*(x^4)*(x^1).
*
* x can be anything, y must be in [0,255]
*/
static inline gfloat
fast_powf (gfloat x, gint y)
{
gfloat value;
gfloat x_pow[8];
guint y_uint = (guint) y;
guint bitmask;
gint i;
if (y_uint & 0x01)
value = x;
else
value = 1.0;
x_pow[0] = x;
for (bitmask = 0x02, i = 1;
bitmask <= y_uint;
bitmask <<= 1, i++)
{
/* x_pow[i] == x_pow[i-1]^2 == x^(2^i) */
x_pow[i] = SQR (x_pow[i - 1]);
if (y_uint & bitmask)
value *= x_pow[i];
}
return value;
}
/*
* For each i in [0, HISTSIZE), hist[i] is the number of occurrences of the
* value i. Return a value in [0, HISTSIZE) weighted heavily toward the
* most frequent values in the histogram.
*
* Assuming that hist_max is the maximum number of occurrences for any
* value in the histogram, the weight given to each value i is
* one value in the histogram, the weight given to each value i is
*
* weight = (hist[i] / hist_max)^8
* weight = (hist[i] / hist_max)^exponent
*
* The 8 is subjective. Lower powers give fuzzier edges.
* (i.e. the normalized histogram frequency raised to some power)
*/
static inline guchar
weighted_average_value (gint hist[HISTSIZE])
weighted_average_value (gint hist[HISTSIZE], gfloat exponent)
{
gint i;
gint max = 1;
gfloat weight;
gint i;
gint hist_max = 1;
gint exponent_int = 0;
gfloat sum = 0.0;
gfloat div = 1.0e-6;
gint value;
gint value;
for (i = 0; i < HISTSIZE; i++)
max = MAX (max, hist[i]);
hist_max = MAX (hist_max, hist[i]);
if ((exponent - floor (exponent)) < 0.001 && exponent <= 255.0)
exponent_int = (gint) exponent;
for (i = 0; i < HISTSIZE; i++)
{
weight = (gfloat) hist[i] / (gfloat) max;
weight *= weight;
weight *= weight;
weight *= weight; /* w' = w^8 */
{
gfloat ratio = (gfloat) hist[i] / (gfloat) hist_max;
gfloat weight;
sum += weight * (gfloat) i;
div += weight;
}
if (exponent_int)
weight = fast_powf (ratio, exponent_int);
else
weight = pow (ratio, exponent);
sum += weight * (gfloat) i;
div += weight;
}
value = (gint) (sum / div);
return (guchar) CLAMP (value, 0, HISTSIZE - 1);
return (guchar) CLAMP0255 (value);
}
/*
* For each i in [0, HISTSIZE), hist[i] is the number of occurrences of
* pixels with intensity i. hist_rgb[][i] is the average color of those
* pixels with intensity i, each channel multiplied by hist[i]. Write to
* dest a pixel whose color is a weighted average of all the colors in
* hist_rgb[][], biased heavily toward those with the most frequently-
* occurring intensities (as noted in hist[]).
* pixels with intensity i, but with each channel multiplied by hist[i].
* Write to dest a pixel whose color is a weighted average of all the
* colors in hist_rgb[][], biased heavily toward those with the most
* frequently-occurring intensities (as noted in hist[]).
*
* The weight formula is the same as in weighted_average_value().
*/
static inline void
weighted_average_color (gint hist[HISTSIZE],
gint hist_rgb[4][HISTSIZE],
gfloat exponent,
guchar *dest,
gint bytes)
gint bpp)
{
gint i, b, c;
gint max = 1;
gfloat weight;
gint i, b;
gint hist_max = 1;
gint exponent_int = 0;
gfloat div = 1.0e-6;
gfloat color[4] = { 0.0, 0.0, 0.0, 0.0 };
for (i = 0; i < HISTSIZE; i++)
max = MAX (max, hist[i]);
hist_max = MAX (hist_max, hist[i]);
if ((exponent - floor (exponent)) < 0.001 && exponent <= 255.0)
exponent_int = (gint) exponent;
for (i = 0; i < HISTSIZE; i++)
{
weight = (gfloat) hist[i] / (gfloat) max;
weight *= weight;
weight *= weight;
weight *= weight; /* w' = w^8 */
{
gfloat ratio = (gfloat) hist[i] / (gfloat) hist_max;
gfloat weight;
if (exponent_int)
weight = fast_powf (ratio, exponent_int);
else
weight = pow (ratio, exponent);
if (hist[i] > 0)
for (b = 0; b < bytes; b++)
color[b] += weight * (gfloat) hist_rgb[b][i] / (gfloat) hist[i];
if (hist[i] > 0)
for (b = 0; b < bpp; b++)
color[b] += weight * (gfloat) hist_rgb[b][i] / (gfloat) hist[i];
div += weight;
}
div += weight;
}
for (b = 0; b < bytes; b++)
for (b = 0; b < bpp; b++)
{
c = (gint) (color[b] / div);
dest[b] = (guchar) CLAMP (c, 0, HISTSIZE - 1);
gint c = (gint) (color[b] / div);
dest[b] = (guchar) CLAMP0255 (c);
}
}
/*
* For all x and y as desired, replace the pixel at (x,y)
* For all x and y as requested, replace the pixel at (x,y)
* with a weighted average of the most frequently occurring
* values in a circle of radius n centered at (x,y).
* values in a circle of mask_size diameter centered at (x,y).
*/
static void
oilify (GimpDrawable *drawable,
GimpPreview *preview)
{
gboolean use_inten_alg;
GimpPixelRgn src_rgn, dest_rgn;
gint bytes;
gint width, height;
guchar *src_row, *src;
guchar *dest_row, *dest;
gint x, y, b, c, xx, yy, n;
gboolean use_inten;
gboolean use_msmap = FALSE;
gboolean use_emap = FALSE;
GimpDrawable *mask_size_map_drawable = NULL;
GimpDrawable *exponent_map_drawable = NULL;
GimpPixelRgn mask_size_map_rgn;
GimpPixelRgn exponent_map_rgn;
gint msmap_bpp = 0;
gint emap_bpp = 0;
GimpPixelRgn dest_rgn;
GimpPixelRgn *regions[3];
gint n_regions;
gint bpp;
gint *sqr_lut;
gint x1, y1, x2, y2;
gint x3, y3, x4, y4;
gint width, height;
gint Hist[HISTSIZE];
gint Hist_rgb[4][HISTSIZE];
gpointer pr1;
gpointer pr;
gint progress, max_progress;
guchar *src_buf;
gchar *mask_shape;
gint y_off;
guchar *src_inten_buf = NULL;
gint i;
use_inten_alg = gimp_drawable_is_rgb (drawable->drawable_id) &&
ovals.mode == MODE_INTEN;
use_inten = (ovals.mode == MODE_INTEN);
/* get the selection bounds */
/* Get the selection bounds */
if (preview)
{
gimp_preview_get_position (preview, &x1, &y1);
......@@ -335,98 +480,228 @@ oilify (GimpDrawable *drawable,
progress = 0;
max_progress = width * height;
bytes = drawable->bpp;
n = (int) ovals.mask_size / 2;
bpp = drawable->bpp;
/*
* mask_shape represents a (n+1)x(n+1) bitmap of one-quarter of a
* circular disk, with radius n and center at (0,0). This is used in the
* inner loop below so that we sample pixels only inside a circular area.
* (Think of it as a lookup-table implementation of a distance function,
* returning zero if the distance > n and nonzero otherwise.)
* Look-up-table implementation of the square function, for use in the
* VERY TIGHT inner loops
*/
mask_shape = g_new (gchar, (n + 1) * (n + 1));
{
gint lut_size = (gint) ovals.mask_size / 2 + 1;
for (x = 0; x <= n; x++)
for (y = 0; y <= n; y++)
mask_shape[y * (n + 1) + x] = x * x + y * y <= n * n;
sqr_lut = g_new (gint, lut_size);
for (i = 0; i < lut_size; i++)
sqr_lut[i] = SQR (i);
}
/* Get the map drawables, if applicable */
if (ovals.use_mask_size_map && ovals.mask_size_map >= 0)
{
use_msmap = TRUE;
mask_size_map_drawable = gimp_drawable_get (ovals.mask_size_map);
gimp_pixel_rgn_init (&mask_size_map_rgn, mask_size_map_drawable,
x1, y1, width, height, FALSE, FALSE);
msmap_bpp = mask_size_map_drawable->bpp;
}
if (ovals.use_exponent_map && ovals.exponent_map >= 0)
{
use_emap = TRUE;
exponent_map_drawable = gimp_drawable_get (ovals.exponent_map);
gimp_pixel_rgn_init (&exponent_map_rgn, exponent_map_drawable,
x1, y1, width, height, FALSE, FALSE);
emap_bpp = exponent_map_drawable->bpp;
}
gimp_pixel_rgn_init (&dest_rgn, drawable,
x1, y1, width, height, (preview == NULL), TRUE);
gimp_pixel_rgn_init (&src_rgn, drawable,
x1, y1, width, height, FALSE, FALSE);
src_buf = g_new (guchar, width * height * bytes);
gimp_pixel_rgn_get_rect (&src_rgn, src_buf, x1, y1, width, height);
for (pr1 = gimp_pixel_rgns_register (1, &dest_rgn);
pr1 != NULL;
pr1 = gimp_pixel_rgns_process (pr1))
{
dest_row = dest_rgn.data;
for (y = dest_rgn.y; y < (dest_rgn.y + dest_rgn.h); y++)
{
GimpPixelRgn src_rgn;
gimp_pixel_rgn_init (&src_rgn, drawable,
x1, y1, width, height, FALSE, FALSE);
src_buf = g_new (guchar, width * height * bpp);
gimp_pixel_rgn_get_rect (&src_rgn, src_buf, x1, y1, width, height);
}
/*
* If we're working in intensity mode, then generate a separate intensity
* map of the source image. This way, we can avoid calculating the
* intensity of any given source pixel more than once.
*/
if (use_inten)
{
guchar *src;
guchar *dest;
src_inten_buf = g_new (guchar, width * height);
for (i = 0,
src = src_buf,
dest = src_inten_buf
;
i < (width * height)
;
i++,
src += bpp,
dest++)
{
dest = dest_row;
*dest = (guchar) GIMP_RGB_LUMINANCE (src[0], src[1], src[2]);
}
}
for (x = dest_rgn.x; x < (dest_rgn.x + dest_rgn.w); x++)
n_regions = 0;
regions[n_regions++] = &dest_rgn;
if (use_msmap)
regions[n_regions++] = &mask_size_map_rgn;
if (use_emap)
regions[n_regions++] = &exponent_map_rgn;
for (pr = gimp_pixel_rgns_register2 (n_regions, regions);
pr != NULL;
pr = gimp_pixel_rgns_process (pr))
{
gint y;
guchar *dest_row;
guchar *src_msmap_row = NULL;
guchar *src_emap_row = NULL;
for (y = dest_rgn.y,
dest_row = dest_rgn.data,
src_msmap_row = mask_size_map_rgn.data, /* valid iff use_msmap */
src_emap_row = exponent_map_rgn.data /* valid iff use_emap */
;
y < (gint) (dest_rgn.y + dest_rgn.h)
;
y++,
dest_row += dest_rgn.rowstride,
src_msmap_row += mask_size_map_rgn.rowstride, /* valid iff use_msmap */
src_emap_row += exponent_map_rgn.rowstride) /* valid iff use_emap */
{
gint x;
guchar *dest;
guchar *src_msmap = NULL;
guchar *src_emap = NULL;
for (x = dest_rgn.x,