2004-12-22 06:47:58 +08:00
|
|
|
/*
|
1999-06-05 10:11:16 +08:00
|
|
|
* Copyright (C) 1999 Winston Chang
|
2000-11-26 20:13:23 +08:00
|
|
|
* <winstonc@cs.wisc.edu>
|
|
|
|
* <winston@stdout.org>
|
1999-06-05 10:11:16 +08:00
|
|
|
*
|
2009-01-18 06:28:01 +08:00
|
|
|
* This program is free software: you can redistribute it and/or modify
|
1999-06-05 10:11:16 +08:00
|
|
|
* it under the terms of the GNU General Public License as published by
|
2009-01-18 06:28:01 +08:00
|
|
|
* the Free Software Foundation; either version 3 of the License, or
|
1999-06-05 10:11:16 +08:00
|
|
|
* (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
|
2009-01-18 06:28:01 +08:00
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
1999-06-05 10:11:16 +08:00
|
|
|
*/
|
|
|
|
|
2000-01-02 23:51:41 +08:00
|
|
|
#include "config.h"
|
1999-06-05 10:11:16 +08:00
|
|
|
|
|
|
|
#include <stdlib.h>
|
2000-01-14 20:41:00 +08:00
|
|
|
|
|
|
|
#include <libgimp/gimp.h>
|
|
|
|
#include <libgimp/gimpui.h>
|
1999-06-05 10:11:16 +08:00
|
|
|
|
2000-01-14 20:41:00 +08:00
|
|
|
#include "libgimp/stdplugins-intl.h"
|
1999-06-05 10:11:16 +08:00
|
|
|
|
2004-12-11 21:45:06 +08:00
|
|
|
|
2005-08-16 06:42:34 +08:00
|
|
|
#define PLUG_IN_PROC "plug-in-unsharp-mask"
|
2008-03-24 23:29:55 +08:00
|
|
|
#define PLUG_IN_BINARY "unsharp-mask"
|
1999-06-05 10:11:16 +08:00
|
|
|
|
2005-02-13 05:17:11 +08:00
|
|
|
#define SCALE_WIDTH 120
|
|
|
|
#define ENTRY_WIDTH 5
|
2004-06-06 01:13:21 +08:00
|
|
|
|
2004-12-22 06:04:55 +08:00
|
|
|
/* Uncomment this line to get a rough estimate of how long the plug-in
|
|
|
|
* takes to run.
|
2004-12-11 21:45:06 +08:00
|
|
|
*/
|
2004-12-22 06:04:55 +08:00
|
|
|
|
|
|
|
/* #define TIMER */
|
1999-06-05 10:11:16 +08:00
|
|
|
|
2004-12-11 21:45:06 +08:00
|
|
|
|
2000-01-14 20:41:00 +08:00
|
|
|
typedef struct
|
|
|
|
{
|
2004-08-31 06:10:26 +08:00
|
|
|
gdouble radius;
|
|
|
|
gdouble amount;
|
|
|
|
gint threshold;
|
1999-06-05 10:11:16 +08:00
|
|
|
} UnsharpMaskParams;
|
|
|
|
|
2000-01-14 20:41:00 +08:00
|
|
|
typedef struct
|
|
|
|
{
|
2003-07-02 21:00:16 +08:00
|
|
|
gboolean run;
|
1999-06-05 10:11:16 +08:00
|
|
|
} UnsharpMaskInterface;
|
|
|
|
|
|
|
|
/* local function prototypes */
|
2004-05-20 08:50:05 +08:00
|
|
|
static void query (void);
|
|
|
|
static void run (const gchar *name,
|
|
|
|
gint nparams,
|
|
|
|
const GimpParam *param,
|
|
|
|
gint *nreturn_vals,
|
|
|
|
GimpParam **return_vals);
|
|
|
|
|
2009-01-18 06:17:42 +08:00
|
|
|
static void gaussian_blur_line (const gdouble *cmatrix,
|
2006-04-26 21:19:56 +08:00
|
|
|
const gint cmatrix_length,
|
2004-12-22 06:04:55 +08:00
|
|
|
const guchar *src,
|
|
|
|
guchar *dest,
|
2006-04-26 21:19:56 +08:00
|
|
|
const gint len,
|
2009-01-18 06:17:42 +08:00
|
|
|
const gint bpp);
|
|
|
|
static void box_blur_line (const gint box_width,
|
|
|
|
const gint even_offset,
|
|
|
|
const guchar *src,
|
|
|
|
guchar *dest,
|
|
|
|
const gint len,
|
|
|
|
const gint bpp);
|
2004-12-22 06:04:55 +08:00
|
|
|
static gint gen_convolve_matrix (gdouble std_dev,
|
|
|
|
gdouble **cmatrix);
|
|
|
|
static void unsharp_region (GimpPixelRgn *srcPTR,
|
|
|
|
GimpPixelRgn *dstPTR,
|
2009-01-18 06:17:42 +08:00
|
|
|
gint bpp,
|
2004-12-22 06:04:55 +08:00
|
|
|
gdouble radius,
|
|
|
|
gdouble amount,
|
|
|
|
gint x1,
|
|
|
|
gint x2,
|
|
|
|
gint y1,
|
|
|
|
gint y2,
|
|
|
|
gboolean show_progress);
|
|
|
|
|
|
|
|
static void unsharp_mask (GimpDrawable *drawable,
|
|
|
|
gdouble radius,
|
|
|
|
gdouble amount);
|
|
|
|
|
|
|
|
static gboolean unsharp_mask_dialog (GimpDrawable *drawable);
|
|
|
|
static void preview_update (GimpPreview *preview);
|
2004-09-16 03:56:01 +08:00
|
|
|
|
1999-06-05 10:11:16 +08:00
|
|
|
|
|
|
|
/* create a few globals, set default values */
|
|
|
|
static UnsharpMaskParams unsharp_params =
|
2007-09-21 21:27:33 +08:00
|
|
|
{
|
|
|
|
5.0, /* default radius */
|
|
|
|
0.5, /* default amount */
|
|
|
|
0 /* default threshold */
|
|
|
|
};
|
1999-06-05 10:11:16 +08:00
|
|
|
|
|
|
|
/* Setting PLUG_IN_INFO */
|
2006-05-16 20:26:20 +08:00
|
|
|
const GimpPlugInInfo PLUG_IN_INFO =
|
2004-06-26 22:18:25 +08:00
|
|
|
{
|
|
|
|
NULL, /* init_proc */
|
|
|
|
NULL, /* quit_proc */
|
|
|
|
query, /* query_proc */
|
|
|
|
run, /* run_proc */
|
|
|
|
};
|
1999-06-05 10:11:16 +08:00
|
|
|
|
2004-06-06 01:13:21 +08:00
|
|
|
|
2000-01-14 20:41:00 +08:00
|
|
|
MAIN ()
|
2004-09-01 22:01:10 +08:00
|
|
|
|
2000-01-14 20:41:00 +08:00
|
|
|
static void
|
|
|
|
query (void)
|
|
|
|
{
|
2006-05-16 20:26:20 +08:00
|
|
|
static const GimpParamDef args[] =
|
2004-06-26 22:18:25 +08:00
|
|
|
{
|
2006-06-27 20:38:45 +08:00
|
|
|
{ GIMP_PDB_INT32, "run-mode", "Interactive, non-interactive" },
|
2004-06-26 22:18:25 +08:00
|
|
|
{ GIMP_PDB_IMAGE, "image", "(unused)" },
|
|
|
|
{ GIMP_PDB_DRAWABLE, "drawable", "Drawable to draw on" },
|
|
|
|
{ GIMP_PDB_FLOAT, "radius", "Radius of gaussian blur (in pixels > 1.0)" },
|
|
|
|
{ GIMP_PDB_FLOAT, "amount", "Strength of effect" },
|
2005-12-28 02:21:11 +08:00
|
|
|
{ GIMP_PDB_INT32, "threshold", "Threshold (0-255)" }
|
2004-06-26 22:18:25 +08:00
|
|
|
};
|
2004-09-01 22:01:10 +08:00
|
|
|
|
2005-08-16 06:42:34 +08:00
|
|
|
gimp_install_procedure (PLUG_IN_PROC,
|
2006-03-17 17:29:48 +08:00
|
|
|
N_("The most widely useful method for sharpening an image"),
|
2004-06-26 22:18:25 +08:00
|
|
|
"The unsharp mask is a sharpening filter that works "
|
|
|
|
"by comparing using the difference of the image and "
|
|
|
|
"a blurred version of the image. It is commonly "
|
|
|
|
"used on photographic images, and is provides a much "
|
|
|
|
"more pleasing result than the standard sharpen "
|
|
|
|
"filter.",
|
|
|
|
"Winston Chang <winstonc@cs.wisc.edu>",
|
|
|
|
"Winston Chang",
|
2009-01-18 18:40:03 +08:00
|
|
|
"1999-2009",
|
2004-06-26 22:18:25 +08:00
|
|
|
N_("_Unsharp Mask..."),
|
|
|
|
"GRAY*, RGB*",
|
|
|
|
GIMP_PLUGIN,
|
|
|
|
G_N_ELEMENTS (args), 0,
|
|
|
|
args, NULL);
|
2004-09-01 22:01:10 +08:00
|
|
|
|
2005-08-16 06:42:34 +08:00
|
|
|
gimp_plugin_menu_register (PLUG_IN_PROC, "<Image>/Filters/Enhance");
|
1999-06-05 10:11:16 +08:00
|
|
|
}
|
|
|
|
|
2000-01-14 20:41:00 +08:00
|
|
|
static void
|
2003-07-02 21:00:16 +08:00
|
|
|
run (const gchar *name,
|
|
|
|
gint nparams,
|
|
|
|
const GimpParam *param,
|
|
|
|
gint *nreturn_vals,
|
|
|
|
GimpParam **return_vals)
|
2000-01-14 20:41:00 +08:00
|
|
|
{
|
2002-01-08 17:20:02 +08:00
|
|
|
static GimpParam values[1];
|
|
|
|
GimpPDBStatusType status = GIMP_PDB_SUCCESS;
|
2004-08-31 06:10:26 +08:00
|
|
|
GimpDrawable *drawable;
|
2004-12-11 21:45:06 +08:00
|
|
|
GimpRunMode run_mode;
|
1999-06-05 10:11:16 +08:00
|
|
|
#ifdef TIMER
|
2004-12-11 21:45:06 +08:00
|
|
|
GTimer *timer = g_timer_new ();
|
1999-06-05 10:11:16 +08:00
|
|
|
#endif
|
2004-09-01 22:01:10 +08:00
|
|
|
|
2000-01-14 20:41:00 +08:00
|
|
|
run_mode = param[0].data.d_int32;
|
2004-09-01 22:01:10 +08:00
|
|
|
|
2002-01-08 17:20:02 +08:00
|
|
|
*return_vals = values;
|
2000-01-14 20:41:00 +08:00
|
|
|
*nreturn_vals = 1;
|
2004-09-01 22:01:10 +08:00
|
|
|
|
2002-01-08 17:20:02 +08:00
|
|
|
values[0].type = GIMP_PDB_STATUS;
|
|
|
|
values[0].data.d_status = status;
|
2004-09-01 22:01:10 +08:00
|
|
|
|
2003-11-06 23:27:05 +08:00
|
|
|
INIT_I18N ();
|
2004-09-01 22:01:10 +08:00
|
|
|
|
2004-06-06 01:13:21 +08:00
|
|
|
/*
|
|
|
|
* Get drawable information...
|
|
|
|
*/
|
|
|
|
drawable = gimp_drawable_get (param[2].data.d_drawable);
|
2006-02-16 15:52:46 +08:00
|
|
|
gimp_tile_cache_ntiles (2 * MAX (drawable->width / gimp_tile_width () + 1 ,
|
2008-10-20 14:04:39 +08:00
|
|
|
drawable->height / gimp_tile_height () + 1));
|
2004-09-01 22:01:10 +08:00
|
|
|
|
2000-01-14 20:41:00 +08:00
|
|
|
switch (run_mode)
|
|
|
|
{
|
2000-08-22 09:26:57 +08:00
|
|
|
case GIMP_RUN_INTERACTIVE:
|
2005-08-16 06:42:34 +08:00
|
|
|
gimp_get_data (PLUG_IN_PROC, &unsharp_params);
|
2004-06-06 01:13:21 +08:00
|
|
|
/* Reset default values show preview unmodified */
|
2004-09-01 22:01:10 +08:00
|
|
|
|
2004-06-06 01:13:21 +08:00
|
|
|
/* initialize pixel regions and buffer */
|
2004-08-31 06:10:26 +08:00
|
|
|
if (! unsharp_mask_dialog (drawable))
|
2004-06-06 01:13:21 +08:00
|
|
|
return;
|
2004-09-01 22:01:10 +08:00
|
|
|
|
2000-01-14 20:41:00 +08:00
|
|
|
break;
|
2004-09-01 22:01:10 +08:00
|
|
|
|
2000-08-22 09:26:57 +08:00
|
|
|
case GIMP_RUN_NONINTERACTIVE:
|
2000-01-14 20:41:00 +08:00
|
|
|
if (nparams != 6)
|
2004-06-26 22:18:25 +08:00
|
|
|
{
|
|
|
|
status = GIMP_PDB_CALLING_ERROR;
|
|
|
|
}
|
2000-02-04 23:12:17 +08:00
|
|
|
else
|
2004-06-26 22:18:25 +08:00
|
|
|
{
|
|
|
|
unsharp_params.radius = param[3].data.d_float;
|
|
|
|
unsharp_params.amount = param[4].data.d_float;
|
|
|
|
unsharp_params.threshold = param[5].data.d_int32;
|
|
|
|
|
|
|
|
/* make sure there are legal values */
|
|
|
|
if ((unsharp_params.radius < 0.0) ||
|
|
|
|
(unsharp_params.amount < 0.0))
|
|
|
|
status = GIMP_PDB_CALLING_ERROR;
|
|
|
|
}
|
2000-01-14 20:41:00 +08:00
|
|
|
break;
|
1999-06-05 10:11:16 +08:00
|
|
|
|
2000-08-22 09:26:57 +08:00
|
|
|
case GIMP_RUN_WITH_LAST_VALS:
|
2005-08-16 06:42:34 +08:00
|
|
|
gimp_get_data (PLUG_IN_PROC, &unsharp_params);
|
2000-01-14 20:41:00 +08:00
|
|
|
break;
|
1999-06-05 10:11:16 +08:00
|
|
|
|
2000-01-14 20:41:00 +08:00
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
1999-06-05 10:11:16 +08:00
|
|
|
|
2000-08-22 09:26:57 +08:00
|
|
|
if (status == GIMP_PDB_SUCCESS)
|
2000-01-14 20:41:00 +08:00
|
|
|
{
|
|
|
|
drawable = gimp_drawable_get (param[2].data.d_drawable);
|
1999-06-05 10:11:16 +08:00
|
|
|
|
2000-01-14 20:41:00 +08:00
|
|
|
/* here we go */
|
|
|
|
unsharp_mask (drawable, unsharp_params.radius, unsharp_params.amount);
|
1999-06-05 10:11:16 +08:00
|
|
|
|
2000-01-14 20:41:00 +08:00
|
|
|
gimp_displays_flush ();
|
1999-06-05 10:11:16 +08:00
|
|
|
|
2000-01-14 20:41:00 +08:00
|
|
|
/* set data for next use of filter */
|
2006-09-15 16:01:12 +08:00
|
|
|
if (run_mode == GIMP_RUN_INTERACTIVE)
|
|
|
|
gimp_set_data (PLUG_IN_PROC,
|
|
|
|
&unsharp_params, sizeof (UnsharpMaskParams));
|
1999-06-05 10:11:16 +08:00
|
|
|
|
2000-01-14 20:41:00 +08:00
|
|
|
gimp_drawable_detach(drawable);
|
|
|
|
values[0].data.d_status = status;
|
|
|
|
}
|
2002-12-02 03:33:34 +08:00
|
|
|
|
2000-01-14 20:41:00 +08:00
|
|
|
#ifdef TIMER
|
2004-12-22 06:04:55 +08:00
|
|
|
g_printerr ("%f seconds\n", g_timer_elapsed (timer, NULL));
|
2002-12-02 03:33:34 +08:00
|
|
|
g_timer_destroy (timer);
|
2000-01-14 20:41:00 +08:00
|
|
|
#endif
|
1999-06-05 10:11:16 +08:00
|
|
|
}
|
|
|
|
|
2009-01-18 06:17:42 +08:00
|
|
|
/* This function is written as if it is blurring a row of pixels,
|
|
|
|
* even though it can operate on colums, too. There is no difference
|
|
|
|
* in the processing of the lines, at least to the blur_line function.
|
|
|
|
*/
|
|
|
|
static void
|
2009-01-18 18:40:03 +08:00
|
|
|
box_blur_line (const gint box_width, /* Width of the kernel */
|
|
|
|
const gint even_offset, /* If even width,
|
|
|
|
offset to left or right */
|
|
|
|
const guchar *src, /* Pointer to source buffer */
|
|
|
|
guchar *dest, /* Pointer to destination buffer */
|
|
|
|
const gint len, /* Length of buffer, in pixels */
|
|
|
|
const gint bpp) /* Bytes per pixel */
|
2009-01-18 06:17:42 +08:00
|
|
|
{
|
|
|
|
gint i;
|
2009-01-18 18:40:03 +08:00
|
|
|
gint lead; /* This marks the leading edge of the kernel */
|
|
|
|
gint output; /* This marks the center of ther kernel */
|
2009-01-18 06:17:42 +08:00
|
|
|
gint trail; /* This marks the pixel BEHIND the last 1 in the
|
|
|
|
kernel; it's the pixel to remove from the accumulator. */
|
2009-01-18 18:40:03 +08:00
|
|
|
gint ac[bpp]; /* Accumulator for each channel */
|
2009-01-18 06:17:42 +08:00
|
|
|
|
|
|
|
|
|
|
|
/* The algorithm differs for even and odd-sized kernels.
|
|
|
|
* With the output at the center,
|
|
|
|
* If odd, the kernel might look like this: 0011100
|
|
|
|
* If even, the kernel will either be centered on the boundary between
|
|
|
|
* the output and its left neighbor, or on the boundary between the
|
|
|
|
* output and its right neighbor, depending on even_lr.
|
|
|
|
* So it might be 0111100 or 0011110, where output is on the center
|
|
|
|
* of these arrays.
|
|
|
|
*/
|
|
|
|
lead = 0;
|
|
|
|
|
|
|
|
if (box_width % 2)
|
|
|
|
/* Odd-width kernel */
|
|
|
|
{
|
|
|
|
output = lead - (box_width - 1) / 2;
|
|
|
|
trail = lead - box_width;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
/* Even-width kernel. */
|
|
|
|
{
|
|
|
|
/* Right offset */
|
|
|
|
if (even_offset == 1)
|
|
|
|
{
|
|
|
|
output = lead + 1 - box_width / 2;
|
|
|
|
trail = lead - box_width;
|
|
|
|
}
|
|
|
|
/* Left offset */
|
|
|
|
else if (even_offset == -1)
|
|
|
|
{
|
|
|
|
output = lead - box_width / 2;
|
|
|
|
trail = lead - box_width;
|
|
|
|
}
|
|
|
|
/* If even_offset isn't 1 or -1, there's some error. */
|
|
|
|
else
|
|
|
|
g_assert_not_reached ();
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Initialize accumulator */
|
|
|
|
for (i = 0; i < bpp; i++)
|
|
|
|
ac[i] = 0;
|
|
|
|
|
2009-01-18 19:15:43 +08:00
|
|
|
/* As the kernel moves across the image, it has a leading edge and a
|
|
|
|
* trailing edge, and the output is in the middle. */
|
2009-01-18 06:17:42 +08:00
|
|
|
while (output < len)
|
|
|
|
{
|
|
|
|
/* The number of pixels that are both in the image and
|
|
|
|
* currently covered by the kernel. This is necessary to
|
|
|
|
* handle edge cases. */
|
|
|
|
guint coverage = (lead < len ? lead : len-1) - (trail >=0 ? trail : -1);
|
|
|
|
|
2009-01-18 19:15:43 +08:00
|
|
|
#ifdef READABLE_BOXBLUR_CODE
|
|
|
|
/* The code here does the same as the code below, but the code below
|
|
|
|
* has been optimized by moving the if statements out of the tight for
|
|
|
|
* loop, and is harder to understand.
|
|
|
|
* Don't use both this code and the code below. */
|
2009-01-18 06:17:42 +08:00
|
|
|
for (i = 0; i < bpp; i++)
|
|
|
|
{
|
|
|
|
/* If the leading edge of the kernel is still on the image,
|
|
|
|
* add the value there to the accumulator. */
|
|
|
|
if (lead < len)
|
|
|
|
ac[i] += src[bpp * lead + i];
|
|
|
|
|
|
|
|
/* If the trailing edge of the kernel is on the image,
|
|
|
|
* subtract the value there from the accumulator. */
|
|
|
|
if (trail >= 0)
|
|
|
|
ac[i] -= src[bpp * trail + i];
|
|
|
|
|
|
|
|
/* Take the averaged value in the accumulator and store
|
|
|
|
* that value in the output. The number of pixels currently
|
|
|
|
* stored in the accumulator can be less than the nominal
|
|
|
|
* width of the kernel because the kernel can go "over the edge"
|
|
|
|
* of the image. */
|
|
|
|
if (output >= 0)
|
|
|
|
dest[bpp * output + i] = (ac[i] + (coverage >> 1)) / coverage;
|
|
|
|
}
|
2009-01-18 19:15:43 +08:00
|
|
|
#endif
|
|
|
|
|
|
|
|
/* If the leading edge of the kernel is still on the image... */
|
|
|
|
if (lead < len)
|
|
|
|
{
|
|
|
|
/* If the trailing edge of the kernel is on the image. (Since
|
|
|
|
* the output is in between the lead and trail, it must be on
|
|
|
|
* the image. */
|
|
|
|
if (trail >= 0)
|
|
|
|
for (i = 0; i < bpp; i++)
|
|
|
|
{
|
|
|
|
ac[i] += src[bpp * lead + i];
|
|
|
|
ac[i] -= src[bpp * trail + i];
|
|
|
|
dest[bpp * output + i] = (ac[i] + (coverage >> 1)) / coverage;
|
|
|
|
}
|
|
|
|
/* If the output is on the image, but the trailing edge isn't yet
|
|
|
|
* on the image. */
|
|
|
|
else if (output >= 0)
|
|
|
|
for (i = 0; i < bpp; i++)
|
|
|
|
{
|
|
|
|
ac[i] += src[bpp * lead + i];
|
|
|
|
dest[bpp * output + i] = (ac[i] + (coverage >> 1)) / coverage;
|
|
|
|
}
|
|
|
|
/* If leading edge is on the image, but the output and trailing
|
|
|
|
* edge aren't yet on the image. */
|
|
|
|
else
|
|
|
|
for (i = 0; i < bpp; i++)
|
|
|
|
ac[i] += src[bpp * lead + i];
|
|
|
|
}
|
|
|
|
/* If the leading edge has gone off the image, but the output and
|
|
|
|
* trailing edge are on the image. (The big loop exits when the
|
|
|
|
* output goes off the image. */
|
|
|
|
else
|
|
|
|
{
|
|
|
|
for (i = 0; i < bpp; i++)
|
|
|
|
{
|
|
|
|
ac[i] -= src[bpp * trail + i];
|
|
|
|
dest[bpp * output + i] = (ac[i] + (coverage >> 1)) / coverage;
|
|
|
|
}
|
|
|
|
}
|
2009-01-18 06:17:42 +08:00
|
|
|
|
|
|
|
lead++;
|
|
|
|
output++;
|
|
|
|
trail++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2004-12-22 08:20:25 +08:00
|
|
|
/* This function is written as if it is blurring a column at a time,
|
|
|
|
* even though it can operate on rows, too. There is no difference
|
|
|
|
* in the processing of the lines, at least to the blur_line function.
|
2004-11-14 10:50:33 +08:00
|
|
|
*/
|
2004-12-22 06:04:55 +08:00
|
|
|
static void
|
2009-01-18 06:17:42 +08:00
|
|
|
gaussian_blur_line (const gdouble *cmatrix,
|
|
|
|
const gint cmatrix_length,
|
|
|
|
const guchar *src,
|
|
|
|
guchar *dest,
|
|
|
|
const gint len,
|
|
|
|
const gint bpp)
|
2004-11-14 10:50:33 +08:00
|
|
|
{
|
2004-12-21 08:04:34 +08:00
|
|
|
const gdouble *cmatrix_p;
|
|
|
|
const guchar *src_p;
|
|
|
|
const guchar *src_p1;
|
2006-04-26 21:13:38 +08:00
|
|
|
const gint cmatrix_middle = cmatrix_length / 2;
|
|
|
|
gint row;
|
|
|
|
gint i, j;
|
2004-11-14 10:50:33 +08:00
|
|
|
|
2004-12-22 08:20:25 +08:00
|
|
|
/* This first block is the same as the optimized version --
|
2004-11-14 10:50:33 +08:00
|
|
|
* it is only used for very small pictures, so speed isn't a
|
|
|
|
* big concern.
|
|
|
|
*/
|
2004-12-22 06:04:55 +08:00
|
|
|
if (cmatrix_length > len)
|
2004-11-14 10:50:33 +08:00
|
|
|
{
|
2004-12-22 06:04:55 +08:00
|
|
|
for (row = 0; row < len; row++)
|
2004-11-14 10:50:33 +08:00
|
|
|
{
|
|
|
|
/* find the scale factor */
|
2006-04-26 21:13:38 +08:00
|
|
|
gdouble scale = 0;
|
|
|
|
|
2004-12-22 06:04:55 +08:00
|
|
|
for (j = 0; j < len; j++)
|
2004-11-14 10:50:33 +08:00
|
|
|
{
|
|
|
|
/* if the index is in bounds, add it to the scale counter */
|
2004-12-22 06:04:55 +08:00
|
|
|
if (j + cmatrix_middle - row >= 0 &&
|
|
|
|
j + cmatrix_middle - row < cmatrix_length)
|
2004-12-22 06:47:58 +08:00
|
|
|
scale += cmatrix[j];
|
2004-11-14 10:50:33 +08:00
|
|
|
}
|
2004-12-21 04:45:42 +08:00
|
|
|
|
2004-12-22 06:47:58 +08:00
|
|
|
src_p = src;
|
2006-04-26 21:13:38 +08:00
|
|
|
|
2009-01-18 06:17:42 +08:00
|
|
|
for (i = 0; i < bpp; i++)
|
2004-11-14 10:50:33 +08:00
|
|
|
{
|
2006-04-26 21:13:38 +08:00
|
|
|
gdouble sum = 0;
|
|
|
|
|
2004-12-22 06:47:58 +08:00
|
|
|
src_p1 = src_p++;
|
2006-04-26 21:13:38 +08:00
|
|
|
|
2004-12-22 06:04:55 +08:00
|
|
|
for (j = 0; j < len; j++)
|
2004-11-14 10:50:33 +08:00
|
|
|
{
|
2004-12-22 06:47:58 +08:00
|
|
|
if (j + cmatrix_middle - row >= 0 &&
|
|
|
|
j + cmatrix_middle - row < cmatrix_length)
|
|
|
|
sum += *src_p1 * cmatrix[j];
|
|
|
|
|
2009-01-18 06:17:42 +08:00
|
|
|
src_p1 += bpp;
|
2004-11-14 10:50:33 +08:00
|
|
|
}
|
2004-12-21 04:45:42 +08:00
|
|
|
|
2004-12-22 06:04:55 +08:00
|
|
|
*dest++ = (guchar) ROUND (sum / scale);
|
2004-11-14 10:50:33 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* for the edge condition, we only use available info and scale to one */
|
|
|
|
for (row = 0; row < cmatrix_middle; row++)
|
|
|
|
{
|
|
|
|
/* find scale factor */
|
2006-04-26 21:13:38 +08:00
|
|
|
gdouble scale = 0;
|
|
|
|
|
2004-12-21 08:04:34 +08:00
|
|
|
for (j = cmatrix_middle - row; j < cmatrix_length; j++)
|
2004-11-14 10:50:33 +08:00
|
|
|
scale += cmatrix[j];
|
2004-12-21 04:45:42 +08:00
|
|
|
|
2004-12-22 06:47:58 +08:00
|
|
|
src_p = src;
|
2006-04-26 21:13:38 +08:00
|
|
|
|
2009-01-18 06:17:42 +08:00
|
|
|
for (i = 0; i < bpp; i++)
|
2004-11-14 10:50:33 +08:00
|
|
|
{
|
2006-04-26 21:13:38 +08:00
|
|
|
gdouble sum = 0;
|
|
|
|
|
2004-12-22 06:47:58 +08:00
|
|
|
src_p1 = src_p++;
|
2006-04-26 21:13:38 +08:00
|
|
|
|
2004-12-21 08:04:34 +08:00
|
|
|
for (j = cmatrix_middle - row; j < cmatrix_length; j++)
|
2004-12-22 06:47:58 +08:00
|
|
|
{
|
|
|
|
sum += *src_p1 * cmatrix[j];
|
2009-01-18 06:17:42 +08:00
|
|
|
src_p1 += bpp;
|
2004-12-22 06:47:58 +08:00
|
|
|
}
|
2004-12-21 18:48:32 +08:00
|
|
|
|
2006-04-26 21:13:38 +08:00
|
|
|
*dest++ = (guchar) ROUND (sum / scale);
|
2004-11-14 10:50:33 +08:00
|
|
|
}
|
|
|
|
}
|
2004-12-22 06:04:55 +08:00
|
|
|
|
2004-11-14 10:50:33 +08:00
|
|
|
/* go through each pixel in each col */
|
2004-12-22 06:04:55 +08:00
|
|
|
for (; row < len - cmatrix_middle; row++)
|
2004-11-14 10:50:33 +08:00
|
|
|
{
|
2009-01-18 06:17:42 +08:00
|
|
|
src_p = src + (row - cmatrix_middle) * bpp;
|
2006-04-26 21:13:38 +08:00
|
|
|
|
2009-01-18 06:17:42 +08:00
|
|
|
for (i = 0; i < bpp; i++)
|
2004-11-14 10:50:33 +08:00
|
|
|
{
|
2006-04-26 21:13:38 +08:00
|
|
|
gdouble sum = 0;
|
|
|
|
|
2004-11-14 10:50:33 +08:00
|
|
|
cmatrix_p = cmatrix;
|
2004-12-21 08:04:34 +08:00
|
|
|
src_p1 = src_p;
|
2006-04-26 21:13:38 +08:00
|
|
|
|
2005-02-07 03:37:20 +08:00
|
|
|
for (j = 0; j < cmatrix_length; j++)
|
2004-11-14 10:50:33 +08:00
|
|
|
{
|
2005-02-07 03:37:20 +08:00
|
|
|
sum += cmatrix[j] * *src_p1;
|
2009-01-18 06:17:42 +08:00
|
|
|
src_p1 += bpp;
|
2004-11-14 10:50:33 +08:00
|
|
|
}
|
2004-12-21 18:48:32 +08:00
|
|
|
|
2004-12-21 08:04:34 +08:00
|
|
|
src_p++;
|
2006-04-26 21:13:38 +08:00
|
|
|
*dest++ = (guchar) ROUND (sum);
|
2004-11-14 10:50:33 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-12-21 21:01:01 +08:00
|
|
|
/* for the edge condition, we only use available info and scale to one */
|
2004-12-22 06:04:55 +08:00
|
|
|
for (; row < len; row++)
|
2004-11-14 10:50:33 +08:00
|
|
|
{
|
|
|
|
/* find scale factor */
|
2006-04-26 21:13:38 +08:00
|
|
|
gdouble scale = 0;
|
|
|
|
|
2004-12-22 06:04:55 +08:00
|
|
|
for (j = 0; j < len - row + cmatrix_middle; j++)
|
2004-11-14 10:50:33 +08:00
|
|
|
scale += cmatrix[j];
|
2004-12-22 06:04:55 +08:00
|
|
|
|
2009-01-18 06:17:42 +08:00
|
|
|
src_p = src + (row - cmatrix_middle) * bpp;
|
2006-04-26 21:13:38 +08:00
|
|
|
|
2009-01-18 06:17:42 +08:00
|
|
|
for (i = 0; i < bpp; i++)
|
2004-11-14 10:50:33 +08:00
|
|
|
{
|
2006-04-26 21:13:38 +08:00
|
|
|
gdouble sum = 0;
|
|
|
|
|
2004-12-22 06:47:58 +08:00
|
|
|
src_p1 = src_p++;
|
2006-04-26 21:13:38 +08:00
|
|
|
|
2004-12-22 06:04:55 +08:00
|
|
|
for (j = 0; j < len - row + cmatrix_middle; j++)
|
2004-12-22 06:47:58 +08:00
|
|
|
{
|
|
|
|
sum += *src_p1 * cmatrix[j];
|
2009-01-18 06:17:42 +08:00
|
|
|
src_p1 += bpp;
|
2004-12-22 06:47:58 +08:00
|
|
|
}
|
2004-12-21 18:48:32 +08:00
|
|
|
|
2006-04-26 21:13:38 +08:00
|
|
|
*dest++ = (guchar) ROUND (sum / scale);
|
2004-11-14 10:50:33 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2000-01-14 20:41:00 +08:00
|
|
|
static void
|
2000-08-22 09:26:57 +08:00
|
|
|
unsharp_mask (GimpDrawable *drawable,
|
2004-06-26 22:18:25 +08:00
|
|
|
gdouble radius,
|
|
|
|
gdouble amount)
|
2000-01-14 20:41:00 +08:00
|
|
|
{
|
2000-08-22 09:26:57 +08:00
|
|
|
GimpPixelRgn srcPR, destPR;
|
2004-09-03 02:55:20 +08:00
|
|
|
gint x1, y1, x2, y2;
|
2000-01-14 20:41:00 +08:00
|
|
|
|
|
|
|
/* initialize pixel regions */
|
2004-12-21 21:01:01 +08:00
|
|
|
gimp_pixel_rgn_init (&srcPR, drawable,
|
|
|
|
0, 0, drawable->width, drawable->height, FALSE, FALSE);
|
|
|
|
gimp_pixel_rgn_init (&destPR, drawable,
|
|
|
|
0, 0, drawable->width, drawable->height, TRUE, TRUE);
|
2000-01-14 20:41:00 +08:00
|
|
|
|
2004-12-22 08:20:25 +08:00
|
|
|
/* Get the input */
|
|
|
|
gimp_drawable_mask_bounds (drawable->drawable_id, &x1, &y1, &x2, &y2);
|
|
|
|
|
2004-12-21 21:01:01 +08:00
|
|
|
unsharp_region (&srcPR, &destPR, drawable->bpp,
|
|
|
|
radius, amount,
|
|
|
|
x1, x2, y1, y2,
|
|
|
|
TRUE);
|
2000-01-14 20:41:00 +08:00
|
|
|
|
2004-09-03 02:55:20 +08:00
|
|
|
gimp_drawable_flush (drawable);
|
|
|
|
gimp_drawable_merge_shadow (drawable->drawable_id, TRUE);
|
2004-12-21 04:45:42 +08:00
|
|
|
gimp_drawable_update (drawable->drawable_id, x1, y1, x2 - x1, y2 - y1);
|
2000-01-14 20:41:00 +08:00
|
|
|
}
|
1999-06-05 10:11:16 +08:00
|
|
|
|
2004-12-21 04:45:42 +08:00
|
|
|
/* Perform an unsharp mask on the region, given a source region, dest.
|
|
|
|
* region, width and height of the regions, and corner coordinates of
|
|
|
|
* a subregion to act upon. Everything outside the subregion is unaffected.
|
|
|
|
*/
|
2000-01-14 20:41:00 +08:00
|
|
|
static void
|
2004-09-16 04:51:00 +08:00
|
|
|
unsharp_region (GimpPixelRgn *srcPR,
|
|
|
|
GimpPixelRgn *destPR,
|
2009-01-18 06:17:42 +08:00
|
|
|
gint bpp,
|
|
|
|
gdouble radius, /* Radius, AKA standard deviation */
|
2004-09-16 04:51:00 +08:00
|
|
|
gdouble amount,
|
|
|
|
gint x1,
|
|
|
|
gint x2,
|
|
|
|
gint y1,
|
|
|
|
gint y2,
|
|
|
|
gboolean show_progress)
|
2000-01-14 20:41:00 +08:00
|
|
|
{
|
2009-01-18 18:40:03 +08:00
|
|
|
guchar *src; /* Temporary copy of source row/col */
|
|
|
|
guchar *dest; /* Temporary copy of destination row/col */
|
2009-01-18 06:17:42 +08:00
|
|
|
const gint width = x2 - x1;
|
|
|
|
const gint height = y2 - y1;
|
2009-01-18 18:40:03 +08:00
|
|
|
gdouble *cmatrix = NULL; /* Convolution matrix (for gaussian) */
|
2009-01-18 06:17:42 +08:00
|
|
|
gint cmatrix_length = 0;
|
2009-01-18 18:40:03 +08:00
|
|
|
gint row, col; /* Row, column counters */
|
2009-01-18 06:17:42 +08:00
|
|
|
const gint threshold = unsharp_params.threshold;
|
2009-01-18 18:40:03 +08:00
|
|
|
gboolean box_blur; /* If we want to use a three pass box
|
|
|
|
blur instead of a gaussian blur */
|
2009-01-18 06:17:42 +08:00
|
|
|
gint box_width = 0;
|
2000-01-14 20:41:00 +08:00
|
|
|
|
2004-12-22 08:20:25 +08:00
|
|
|
if (show_progress)
|
2005-09-30 16:16:10 +08:00
|
|
|
gimp_progress_init (_("Blurring"));
|
2000-01-14 20:41:00 +08:00
|
|
|
|
2009-01-18 06:17:42 +08:00
|
|
|
/* If the radius is less than 10, use a true gaussian kernel. This
|
|
|
|
* is slower, but more accurate and allows for finer adjustments.
|
|
|
|
* Otherwise use a three-pass box blur; this is much faster but it
|
|
|
|
* isn't a perfect approximation, and it only allows radius
|
|
|
|
* increments of about 0.42.
|
|
|
|
*/
|
|
|
|
if (radius < 10)
|
|
|
|
{
|
|
|
|
box_blur = FALSE;
|
|
|
|
/* If true gaussian, generate convolution matrix
|
|
|
|
and make sure it's smaller than each dimension */
|
|
|
|
cmatrix_length = gen_convolve_matrix (radius, &cmatrix);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
box_blur = TRUE;
|
|
|
|
/* Three box blurs of this width approximate a gaussian */
|
|
|
|
box_width = ROUND (radius * 3 * sqrt (2 * G_PI) / 4);
|
|
|
|
}
|
2004-08-10 05:07:57 +08:00
|
|
|
|
2009-01-18 06:17:42 +08:00
|
|
|
/* Allocate buffers temporary copies of a row/column */
|
|
|
|
src = g_new (guchar, MAX (width, height) * bpp);
|
|
|
|
dest = g_new (guchar, MAX (width, height) * bpp);
|
2000-01-14 20:41:00 +08:00
|
|
|
|
2009-01-18 06:17:42 +08:00
|
|
|
/* Blur the rows */
|
2004-12-21 06:27:05 +08:00
|
|
|
for (row = 0; row < height; row++)
|
2000-01-14 20:41:00 +08:00
|
|
|
{
|
2004-12-21 06:27:05 +08:00
|
|
|
gimp_pixel_rgn_get_row (srcPR, src, x1, y1 + row, width);
|
2009-01-18 06:17:42 +08:00
|
|
|
|
|
|
|
if (box_blur)
|
|
|
|
{
|
|
|
|
/* Odd-width box blur: repeat 3 times, centered on output pixel.
|
|
|
|
* Swap back and forth between the buffers. */
|
|
|
|
if (box_width % 2)
|
|
|
|
{
|
|
|
|
box_blur_line (box_width, 0, src, dest, width, bpp);
|
|
|
|
box_blur_line (box_width, 0, dest, src, width, bpp);
|
|
|
|
box_blur_line (box_width, 0, src, dest, width, bpp);
|
|
|
|
}
|
|
|
|
/* Even-width box blur:
|
|
|
|
* This method is suggested by the specification for SVG.
|
|
|
|
* One pass with width n, centered between output and right pixel
|
|
|
|
* One pass with width n, centered between output and left pixel
|
|
|
|
* One pass with width n+1, centered on output pixel
|
|
|
|
* Swap back and forth between buffers.
|
|
|
|
*/
|
|
|
|
else
|
|
|
|
{
|
|
|
|
box_blur_line (box_width, -1, src, dest, width, bpp);
|
|
|
|
box_blur_line (box_width, 1, dest, src, width, bpp);
|
|
|
|
box_blur_line (box_width+1, 0, src, dest, width, bpp);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* Gaussian blur */
|
|
|
|
gaussian_blur_line (cmatrix, cmatrix_length, src, dest, width, bpp);
|
|
|
|
}
|
|
|
|
|
2004-12-21 06:27:05 +08:00
|
|
|
gimp_pixel_rgn_set_row (destPR, dest, x1, y1 + row, width);
|
2000-01-14 20:41:00 +08:00
|
|
|
|
2009-01-18 19:15:43 +08:00
|
|
|
if (show_progress && row % 64 == 0)
|
2004-12-21 06:27:05 +08:00
|
|
|
gimp_progress_update ((gdouble) row / (3 * height));
|
2000-01-14 20:41:00 +08:00
|
|
|
}
|
|
|
|
|
2009-01-18 06:17:42 +08:00
|
|
|
/* Blur the cols. Essentially same as above. */
|
2004-12-21 06:27:05 +08:00
|
|
|
for (col = 0; col < width; col++)
|
2000-01-14 20:41:00 +08:00
|
|
|
{
|
2004-12-21 21:01:01 +08:00
|
|
|
gimp_pixel_rgn_get_col (destPR, src, x1 + col, y1, height);
|
2009-01-18 06:17:42 +08:00
|
|
|
|
|
|
|
if (box_blur)
|
|
|
|
{
|
|
|
|
/* Odd-width box blur */
|
|
|
|
if (box_width % 2)
|
|
|
|
{
|
|
|
|
box_blur_line (box_width, 0, src, dest, height, bpp);
|
|
|
|
box_blur_line (box_width, 0, dest, src, height, bpp);
|
|
|
|
box_blur_line (box_width, 0, src, dest, height, bpp);
|
|
|
|
}
|
|
|
|
/* Even-width box blur */
|
|
|
|
else
|
|
|
|
{
|
|
|
|
box_blur_line (box_width, -1, src, dest, height, bpp);
|
|
|
|
box_blur_line (box_width, 1, dest, src, height, bpp);
|
|
|
|
box_blur_line (box_width+1, 0, src, dest, height, bpp);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* Gaussian blur */
|
|
|
|
gaussian_blur_line (cmatrix, cmatrix_length, src, dest,height, bpp);
|
|
|
|
}
|
|
|
|
|
2004-12-21 06:27:05 +08:00
|
|
|
gimp_pixel_rgn_set_col (destPR, dest, x1 + col, y1, height);
|
2000-01-14 20:41:00 +08:00
|
|
|
|
2009-01-18 19:15:43 +08:00
|
|
|
if (show_progress && col % 64 == 0)
|
2004-12-21 06:27:05 +08:00
|
|
|
gimp_progress_update ((gdouble) col / (3 * width) + 0.33);
|
2000-01-14 20:41:00 +08:00
|
|
|
}
|
|
|
|
|
2004-08-10 07:35:40 +08:00
|
|
|
if (show_progress)
|
2006-04-26 21:13:38 +08:00
|
|
|
gimp_progress_set_text (_("Merging"));
|
2000-01-14 20:41:00 +08:00
|
|
|
|
|
|
|
/* merge the source and destination (which currently contains
|
|
|
|
the blurred version) images */
|
2004-12-21 06:27:05 +08:00
|
|
|
for (row = 0; row < height; row++)
|
2000-01-14 20:41:00 +08:00
|
|
|
{
|
2004-12-22 08:03:33 +08:00
|
|
|
const guchar *s = src;
|
|
|
|
guchar *d = dest;
|
|
|
|
gint u, v;
|
2004-08-10 05:07:57 +08:00
|
|
|
|
2000-01-14 20:41:00 +08:00
|
|
|
/* get source row */
|
2004-12-21 06:27:05 +08:00
|
|
|
gimp_pixel_rgn_get_row (srcPR, src, x1, y1 + row, width);
|
2004-08-10 05:07:57 +08:00
|
|
|
|
2000-01-14 20:41:00 +08:00
|
|
|
/* get dest row */
|
2004-12-21 06:27:05 +08:00
|
|
|
gimp_pixel_rgn_get_row (destPR, dest, x1, y1 + row, width);
|
2004-08-10 05:07:57 +08:00
|
|
|
|
2000-01-14 20:41:00 +08:00
|
|
|
/* combine the two */
|
2004-12-21 06:27:05 +08:00
|
|
|
for (u = 0; u < width; u++)
|
2004-06-26 22:18:25 +08:00
|
|
|
{
|
2009-01-18 06:17:42 +08:00
|
|
|
for (v = 0; v < bpp; v++)
|
2004-06-26 22:18:25 +08:00
|
|
|
{
|
2006-04-26 21:13:38 +08:00
|
|
|
gint value;
|
2004-12-22 08:03:33 +08:00
|
|
|
gint diff = *s - *d;
|
2004-08-10 05:07:57 +08:00
|
|
|
|
2004-06-26 22:18:25 +08:00
|
|
|
/* do tresholding */
|
|
|
|
if (abs (2 * diff) < threshold)
|
|
|
|
diff = 0;
|
|
|
|
|
2004-12-22 08:03:33 +08:00
|
|
|
value = *s++ + amount * diff;
|
|
|
|
*d++ = CLAMP (value, 0, 255);
|
2004-06-26 22:18:25 +08:00
|
|
|
}
|
|
|
|
}
|
2004-12-22 08:03:33 +08:00
|
|
|
|
2009-01-18 19:15:43 +08:00
|
|
|
if (show_progress && row % 64 == 0)
|
2004-12-21 06:27:05 +08:00
|
|
|
gimp_progress_update ((gdouble) row / (3 * height) + 0.67);
|
2003-06-13 22:37:00 +08:00
|
|
|
|
2004-12-21 06:27:05 +08:00
|
|
|
gimp_pixel_rgn_set_row (destPR, dest, x1, y1 + row, width);
|
2000-01-14 20:41:00 +08:00
|
|
|
}
|
|
|
|
|
2004-08-10 07:35:40 +08:00
|
|
|
if (show_progress)
|
2006-04-26 21:13:38 +08:00
|
|
|
gimp_progress_update (1.0);
|
2004-08-10 05:07:57 +08:00
|
|
|
|
2004-12-21 06:27:05 +08:00
|
|
|
g_free (dest);
|
|
|
|
g_free (src);
|
2004-12-21 08:04:34 +08:00
|
|
|
g_free (cmatrix);
|
1999-06-05 10:11:16 +08:00
|
|
|
}
|
|
|
|
|
2003-11-06 23:27:05 +08:00
|
|
|
/* generates a 1-D convolution matrix to be used for each pass of
|
1999-06-05 10:11:16 +08:00
|
|
|
* a two-pass gaussian blur. Returns the length of the matrix.
|
|
|
|
*/
|
2000-01-14 20:41:00 +08:00
|
|
|
static gint
|
|
|
|
gen_convolve_matrix (gdouble radius,
|
2004-06-26 22:18:25 +08:00
|
|
|
gdouble **cmatrix_p)
|
2000-01-14 20:41:00 +08:00
|
|
|
{
|
2004-09-03 02:55:20 +08:00
|
|
|
gdouble *cmatrix;
|
|
|
|
gdouble std_dev;
|
|
|
|
gdouble sum;
|
2004-12-22 08:20:25 +08:00
|
|
|
gint matrix_length;
|
|
|
|
gint i, j;
|
2003-11-06 23:27:05 +08:00
|
|
|
|
2000-01-14 20:41:00 +08:00
|
|
|
/* we want to generate a matrix that goes out a certain radius
|
|
|
|
* from the center, so we have to go out ceil(rad-0.5) pixels,
|
|
|
|
* inlcuding the center pixel. Of course, that's only in one direction,
|
|
|
|
* so we have to go the same amount in the other direction, but not count
|
|
|
|
* the center pixel again. So we double the previous result and subtract
|
|
|
|
* one.
|
1999-07-12 02:53:53 +08:00
|
|
|
* The radius parameter that is passed to this function is used as
|
2000-01-14 20:41:00 +08:00
|
|
|
* the standard deviation, and the radius of effect is the
|
|
|
|
* standard deviation * 2. It's a little confusing.
|
|
|
|
*/
|
2004-12-21 18:48:32 +08:00
|
|
|
radius = fabs (radius) + 1.0;
|
2003-11-06 23:27:05 +08:00
|
|
|
|
2000-01-14 20:41:00 +08:00
|
|
|
std_dev = radius;
|
|
|
|
radius = std_dev * 2;
|
|
|
|
|
|
|
|
/* go out 'radius' in each direction */
|
2004-12-21 08:04:34 +08:00
|
|
|
matrix_length = 2 * ceil (radius - 0.5) + 1;
|
|
|
|
if (matrix_length <= 0)
|
|
|
|
matrix_length = 1;
|
|
|
|
|
2000-05-02 04:22:55 +08:00
|
|
|
*cmatrix_p = g_new (gdouble, matrix_length);
|
2000-01-14 20:41:00 +08:00
|
|
|
cmatrix = *cmatrix_p;
|
|
|
|
|
|
|
|
/* Now we fill the matrix by doing a numeric integration approximation
|
|
|
|
* from -2*std_dev to 2*std_dev, sampling 50 points per pixel.
|
|
|
|
* We do the bottom half, mirror it to the top half, then compute the
|
|
|
|
* center point. Otherwise asymmetric quantization errors will occur.
|
|
|
|
* The formula to integrate is e^-(x^2/2s^2).
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* first we do the top (right) half of matrix */
|
2004-12-21 04:45:42 +08:00
|
|
|
for (i = matrix_length / 2 + 1; i < matrix_length; i++)
|
2000-01-14 20:41:00 +08:00
|
|
|
{
|
2004-12-22 08:20:25 +08:00
|
|
|
gdouble base_x = i - (matrix_length / 2) - 0.5;
|
|
|
|
|
2000-01-14 20:41:00 +08:00
|
|
|
sum = 0;
|
|
|
|
for (j = 1; j <= 50; j++)
|
2004-06-26 22:18:25 +08:00
|
|
|
{
|
2008-05-05 22:02:07 +08:00
|
|
|
gdouble r = base_x + 0.02 * j;
|
|
|
|
|
|
|
|
if (r <= radius)
|
|
|
|
sum += exp (- SQR (r) / (2 * SQR (std_dev)));
|
2004-06-26 22:18:25 +08:00
|
|
|
}
|
2004-12-21 21:01:01 +08:00
|
|
|
|
2004-12-21 04:45:42 +08:00
|
|
|
cmatrix[i] = sum / 50;
|
2000-01-14 20:41:00 +08:00
|
|
|
}
|
1999-06-05 10:11:16 +08:00
|
|
|
|
2000-01-14 20:41:00 +08:00
|
|
|
/* mirror the thing to the bottom half */
|
2004-12-21 04:45:42 +08:00
|
|
|
for (i = 0; i <= matrix_length / 2; i++)
|
|
|
|
cmatrix[i] = cmatrix[matrix_length - 1 - i];
|
2003-11-06 23:27:05 +08:00
|
|
|
|
2009-01-18 18:40:03 +08:00
|
|
|
/* find center val -- calculate an odd number of quanta to make it
|
|
|
|
* symmetric, even if the center point is weighted slightly higher
|
|
|
|
* than others.
|
|
|
|
*/
|
2000-01-14 20:41:00 +08:00
|
|
|
sum = 0;
|
2004-09-03 02:55:20 +08:00
|
|
|
for (j = 0; j <= 50; j++)
|
2008-05-05 22:02:07 +08:00
|
|
|
sum += exp (- SQR (- 0.5 + 0.02 * j) / (2 * SQR (std_dev)));
|
2004-12-21 18:48:32 +08:00
|
|
|
|
2004-12-21 04:45:42 +08:00
|
|
|
cmatrix[matrix_length / 2] = sum / 51;
|
2003-11-06 23:27:05 +08:00
|
|
|
|
2000-01-14 20:41:00 +08:00
|
|
|
/* normalize the distribution by scaling the total sum to one */
|
2004-12-21 08:04:34 +08:00
|
|
|
sum = 0;
|
|
|
|
for (i = 0; i < matrix_length; i++)
|
|
|
|
sum += cmatrix[i];
|
2004-12-22 08:03:33 +08:00
|
|
|
|
2004-12-21 08:04:34 +08:00
|
|
|
for (i = 0; i < matrix_length; i++)
|
|
|
|
cmatrix[i] = cmatrix[i] / sum;
|
1999-06-05 10:11:16 +08:00
|
|
|
|
2000-01-14 20:41:00 +08:00
|
|
|
return matrix_length;
|
1999-06-05 10:11:16 +08:00
|
|
|
}
|
|
|
|
|
2004-05-20 08:50:05 +08:00
|
|
|
static gboolean
|
2004-08-31 06:10:26 +08:00
|
|
|
unsharp_mask_dialog (GimpDrawable *drawable)
|
1999-11-24 07:49:45 +08:00
|
|
|
{
|
2004-06-06 01:13:21 +08:00
|
|
|
GtkWidget *dialog;
|
2004-09-10 00:08:56 +08:00
|
|
|
GtkWidget *main_vbox;
|
2004-08-31 06:10:26 +08:00
|
|
|
GtkWidget *preview;
|
2004-09-10 00:08:56 +08:00
|
|
|
GtkWidget *table;
|
2000-01-14 20:41:00 +08:00
|
|
|
GtkObject *adj;
|
2003-11-06 23:27:05 +08:00
|
|
|
gboolean run;
|
1999-06-05 10:11:16 +08:00
|
|
|
|
2005-08-16 06:42:34 +08:00
|
|
|
gimp_ui_init (PLUG_IN_BINARY, TRUE);
|
1999-06-05 10:11:16 +08:00
|
|
|
|
2005-08-16 06:42:34 +08:00
|
|
|
dialog = gimp_dialog_new (_("Unsharp Mask"), PLUG_IN_BINARY,
|
2003-11-06 23:27:05 +08:00
|
|
|
NULL, 0,
|
2005-08-16 06:42:34 +08:00
|
|
|
gimp_standard_help_func, PLUG_IN_PROC,
|
1999-06-05 10:11:16 +08:00
|
|
|
|
2003-11-06 23:27:05 +08:00
|
|
|
GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
|
|
|
|
GTK_STOCK_OK, GTK_RESPONSE_OK,
|
2002-10-29 03:12:17 +08:00
|
|
|
|
2003-11-06 23:27:05 +08:00
|
|
|
NULL);
|
2000-01-14 20:41:00 +08:00
|
|
|
|
2005-02-09 04:40:33 +08:00
|
|
|
gtk_dialog_set_alternative_button_order (GTK_DIALOG (dialog),
|
2005-08-16 06:42:34 +08:00
|
|
|
GTK_RESPONSE_OK,
|
|
|
|
GTK_RESPONSE_CANCEL,
|
|
|
|
-1);
|
2005-02-09 04:40:33 +08:00
|
|
|
|
2005-09-10 02:07:31 +08:00
|
|
|
gimp_window_set_transient (GTK_WINDOW (dialog));
|
2005-09-06 05:40:29 +08:00
|
|
|
|
2004-09-10 00:08:56 +08:00
|
|
|
main_vbox = gtk_vbox_new (FALSE, 12);
|
|
|
|
gtk_container_set_border_width (GTK_CONTAINER (main_vbox), 12);
|
|
|
|
gtk_container_add (GTK_CONTAINER (GTK_DIALOG (dialog)->vbox), main_vbox);
|
|
|
|
gtk_widget_show (main_vbox);
|
2004-06-06 01:13:21 +08:00
|
|
|
|
2007-09-21 21:27:33 +08:00
|
|
|
preview = gimp_drawable_preview_new (drawable, NULL);
|
2004-09-10 00:08:56 +08:00
|
|
|
gtk_box_pack_start (GTK_BOX (main_vbox), preview, TRUE, TRUE, 0);
|
2004-08-31 06:10:26 +08:00
|
|
|
gtk_widget_show (preview);
|
2004-09-03 02:55:20 +08:00
|
|
|
|
2004-08-31 22:29:25 +08:00
|
|
|
g_signal_connect (preview, "invalidated",
|
2004-09-03 02:55:20 +08:00
|
|
|
G_CALLBACK (preview_update),
|
|
|
|
NULL);
|
2004-06-06 01:13:21 +08:00
|
|
|
|
2003-12-11 05:13:29 +08:00
|
|
|
table = gtk_table_new (3, 3, FALSE);
|
2004-05-20 08:50:05 +08:00
|
|
|
gtk_table_set_col_spacings (GTK_TABLE (table), 6);
|
|
|
|
gtk_table_set_row_spacings (GTK_TABLE (table), 6);
|
2004-09-10 00:08:56 +08:00
|
|
|
gtk_box_pack_start (GTK_BOX (main_vbox), table, FALSE, FALSE, 0);
|
2003-12-11 05:13:29 +08:00
|
|
|
gtk_widget_show (table);
|
2000-01-14 20:41:00 +08:00
|
|
|
|
|
|
|
adj = gimp_scale_entry_new (GTK_TABLE (table), 0, 0,
|
2004-06-26 22:18:25 +08:00
|
|
|
_("_Radius:"), SCALE_WIDTH, ENTRY_WIDTH,
|
|
|
|
unsharp_params.radius, 0.1, 120.0, 0.1, 1.0, 1,
|
|
|
|
TRUE, 0, 0,
|
|
|
|
NULL, NULL);
|
2004-06-06 01:13:21 +08:00
|
|
|
|
2005-07-01 00:03:24 +08:00
|
|
|
g_signal_connect (adj, "value-changed",
|
2002-01-08 17:20:02 +08:00
|
|
|
G_CALLBACK (gimp_double_adjustment_update),
|
|
|
|
&unsharp_params.radius);
|
2005-07-01 00:03:24 +08:00
|
|
|
g_signal_connect_swapped (adj, "value-changed",
|
2004-08-31 22:29:25 +08:00
|
|
|
G_CALLBACK (gimp_preview_invalidate),
|
2004-08-31 06:10:26 +08:00
|
|
|
preview);
|
2004-06-06 01:13:21 +08:00
|
|
|
|
2000-01-14 20:41:00 +08:00
|
|
|
adj = gimp_scale_entry_new (GTK_TABLE (table), 0, 1,
|
2004-06-26 22:18:25 +08:00
|
|
|
_("_Amount:"), SCALE_WIDTH, ENTRY_WIDTH,
|
2005-02-13 05:17:11 +08:00
|
|
|
unsharp_params.amount, 0.0, 10.0, 0.01, 0.1, 2,
|
2004-06-26 22:18:25 +08:00
|
|
|
TRUE, 0, 0,
|
|
|
|
NULL, NULL);
|
2004-06-06 01:13:21 +08:00
|
|
|
|
2005-07-01 00:03:24 +08:00
|
|
|
g_signal_connect (adj, "value-changed",
|
2002-01-08 17:20:02 +08:00
|
|
|
G_CALLBACK (gimp_double_adjustment_update),
|
|
|
|
&unsharp_params.amount);
|
2005-07-01 00:03:24 +08:00
|
|
|
g_signal_connect_swapped (adj, "value-changed",
|
2004-08-31 22:29:25 +08:00
|
|
|
G_CALLBACK (gimp_preview_invalidate),
|
2004-08-31 06:10:26 +08:00
|
|
|
preview);
|
2000-01-14 20:41:00 +08:00
|
|
|
|
|
|
|
adj = gimp_scale_entry_new (GTK_TABLE (table), 0, 2,
|
2004-06-26 22:18:25 +08:00
|
|
|
_("_Threshold:"), SCALE_WIDTH, ENTRY_WIDTH,
|
2004-12-21 21:01:01 +08:00
|
|
|
unsharp_params.threshold,
|
|
|
|
0.0, 255.0, 1.0, 10.0, 0,
|
2004-06-26 22:18:25 +08:00
|
|
|
TRUE, 0, 0,
|
|
|
|
NULL, NULL);
|
2004-06-06 01:13:21 +08:00
|
|
|
|
2005-07-01 00:03:24 +08:00
|
|
|
g_signal_connect (adj, "value-changed",
|
2002-01-08 17:20:02 +08:00
|
|
|
G_CALLBACK (gimp_int_adjustment_update),
|
|
|
|
&unsharp_params.threshold);
|
2005-07-01 00:03:24 +08:00
|
|
|
g_signal_connect_swapped (adj, "value-changed",
|
2004-08-31 22:29:25 +08:00
|
|
|
G_CALLBACK (gimp_preview_invalidate),
|
2004-08-31 06:10:26 +08:00
|
|
|
preview);
|
2000-01-14 20:41:00 +08:00
|
|
|
|
2004-06-06 01:13:21 +08:00
|
|
|
gtk_widget_show (dialog);
|
2000-01-14 20:41:00 +08:00
|
|
|
|
2004-06-06 01:13:21 +08:00
|
|
|
run = (gimp_dialog_run (GIMP_DIALOG (dialog)) == GTK_RESPONSE_OK);
|
|
|
|
|
|
|
|
gtk_widget_destroy (dialog);
|
1999-06-05 10:11:16 +08:00
|
|
|
|
2003-11-06 23:27:05 +08:00
|
|
|
return run;
|
1999-06-05 10:11:16 +08:00
|
|
|
}
|
2004-06-06 01:13:21 +08:00
|
|
|
|
2004-07-28 21:58:08 +08:00
|
|
|
static void
|
2004-09-16 04:51:00 +08:00
|
|
|
preview_update (GimpPreview *preview)
|
2004-06-06 01:13:21 +08:00
|
|
|
{
|
2004-09-16 03:56:01 +08:00
|
|
|
GimpDrawable *drawable;
|
2004-12-21 05:20:59 +08:00
|
|
|
gint x1, x2;
|
|
|
|
gint y1, y2;
|
2004-12-21 08:04:34 +08:00
|
|
|
gint x, y;
|
|
|
|
gint width, height;
|
2004-12-21 21:01:01 +08:00
|
|
|
gint border;
|
2004-12-21 05:20:59 +08:00
|
|
|
GimpPixelRgn srcPR;
|
|
|
|
GimpPixelRgn destPR;
|
2004-06-06 01:13:21 +08:00
|
|
|
|
2004-09-16 04:51:00 +08:00
|
|
|
drawable =
|
|
|
|
gimp_drawable_preview_get_drawable (GIMP_DRAWABLE_PREVIEW (preview));
|
2004-09-16 03:56:01 +08:00
|
|
|
|
2004-12-21 21:01:01 +08:00
|
|
|
gimp_pixel_rgn_init (&srcPR, drawable,
|
|
|
|
0, 0, drawable->width, drawable->height, FALSE, FALSE);
|
|
|
|
gimp_pixel_rgn_init (&destPR, drawable,
|
|
|
|
0, 0, drawable->width, drawable->height, TRUE, TRUE);
|
|
|
|
|
2004-12-21 08:04:34 +08:00
|
|
|
gimp_preview_get_position (preview, &x, &y);
|
|
|
|
gimp_preview_get_size (preview, &width, &height);
|
2004-06-06 01:13:21 +08:00
|
|
|
|
2004-12-21 08:04:34 +08:00
|
|
|
/* enlarge the region to avoid artefacts at the edges of the preview */
|
2004-12-21 21:01:01 +08:00
|
|
|
border = 2.0 * unsharp_params.radius + 0.5;
|
|
|
|
x1 = MAX (0, x - border);
|
|
|
|
y1 = MAX (0, y - border);
|
|
|
|
x2 = MIN (x + width + border, drawable->width);
|
|
|
|
y2 = MIN (y + height + border, drawable->height);
|
2004-08-31 06:10:26 +08:00
|
|
|
|
2004-12-21 06:27:05 +08:00
|
|
|
unsharp_region (&srcPR, &destPR, drawable->bpp,
|
2004-08-31 06:10:26 +08:00
|
|
|
unsharp_params.radius, unsharp_params.amount,
|
2004-12-21 21:01:01 +08:00
|
|
|
x1, x2, y1, y2,
|
2004-08-31 06:10:26 +08:00
|
|
|
FALSE);
|
|
|
|
|
2004-12-21 08:04:34 +08:00
|
|
|
gimp_pixel_rgn_init (&destPR, drawable, x, y, width, height, FALSE, TRUE);
|
2004-09-16 04:51:00 +08:00
|
|
|
gimp_drawable_preview_draw_region (GIMP_DRAWABLE_PREVIEW (preview), &destPR);
|
2004-06-06 01:13:21 +08:00
|
|
|
}
|