gimp/plug-ins/common/sobel.c

481 lines
14 KiB
C
Raw Normal View History

1997-11-24 22:05:25 +00:00
/* The GIMP -- an image manipulation program
* 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-24 22:05:25 +00:00
*/
/* This plugin by thorsten@arch.usyd.edu.au */
/* Based on S&P's Gauss and Blur filters */
#include "config.h"
1997-11-24 22:05:25 +00:00
#include <stdlib.h>
#include <string.h>
#include <libgimp/gimp.h>
#include <libgimp/gimpui.h>
#include "libgimp/stdplugins-intl.h"
1997-11-24 22:05:25 +00:00
#define PLUG_IN_PROC "plug-in-sobel"
#define PLUG_IN_BINARY "sobel"
1997-11-24 22:05:25 +00:00
typedef struct
{
gboolean horizontal;
gboolean vertical;
gboolean keep_sign;
1997-11-24 22:05:25 +00:00
} SobelValues;
/* 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 sobel (GimpDrawable *drawable,
gboolean horizontal,
gboolean vertical,
gboolean keep_sign,
GimpPreview *preview);
1997-11-24 22:05:25 +00:00
/*
* Sobel interface
*/
static gboolean sobel_dialog (GimpDrawable *drawable);
static void sobel_preview_update (GimpPreview *preview);
1997-11-24 22:05:25 +00:00
/*
* Sobel helper functions
*/
static void sobel_prepare_row (GimpPixelRgn *pixel_rgn,
guchar *data,
gint x,
gint y,
gint w);
1997-11-24 22:05:25 +00:00
const GimpPlugInInfo PLUG_IN_INFO =
1997-11-24 22:05:25 +00:00
{
NULL, /* init_proc */
NULL, /* quit_proc */
query, /* query_proc */
run, /* run_proc */
1997-11-24 22:05:25 +00:00
};
static SobelValues bvals =
{
TRUE, /* horizontal sobel */
TRUE, /* vertical sobel */
TRUE /* keep sign */
1997-11-24 22:05:25 +00:00
};
MAIN ()
static void
query (void)
1997-11-24 22:05:25 +00:00
{
static const GimpParamDef args[] =
1997-11-24 22:05:25 +00:00
{
{ GIMP_PDB_INT32, "run-mode", "Interactive, non-interactive" },
{ GIMP_PDB_IMAGE, "image", "Input image (unused)" },
{ GIMP_PDB_DRAWABLE, "drawable", "Input drawable" },
{ GIMP_PDB_INT32, "horizontal", "Sobel in horizontal direction" },
{ GIMP_PDB_INT32, "vertical", "Sobel in vertical direction" },
{ GIMP_PDB_INT32, "keep-sign", "Keep sign of result (one direction only)" }
1997-11-24 22:05:25 +00:00
};
gimp_install_procedure (PLUG_IN_PROC,
N_("Specialized direction-dependent edge detection"),
"This plugin calculates the gradient with a sobel "
"operator. The user can specify which direction to "
"use. When both directions are used, the result is "
"the RMS of the two gradients; if only one direction "
"is used, the result either the absolut value of the "
"gradient, or 127 + gradient (if the 'keep sign' "
"switch is on). This way, information about the "
"direction of the gradient is preserved. Resulting "
"images are not autoscaled.",
"Thorsten Schnier",
"Thorsten Schnier",
"1997",
N_("_Sobel..."),
"RGB*, GRAY*",
GIMP_PLUGIN,
G_N_ELEMENTS (args), 0,
args, NULL);
gimp_plugin_menu_register (PLUG_IN_PROC, "<Image>/Filters/Edge-Detect");
1997-11-24 22:05:25 +00:00
}
static void
run (const gchar *name,
gint nparams,
const GimpParam *param,
gint *nreturn_vals,
GimpParam **return_vals)
1997-11-24 22:05:25 +00:00
{
static GimpParam values[1];
GimpDrawable *drawable;
GimpRunMode run_mode;
GimpPDBStatusType status = GIMP_PDB_SUCCESS;
1997-11-24 22:05:25 +00:00
run_mode = param[0].data.d_int32;
INIT_I18N ();
1997-11-24 22:05:25 +00:00
*nreturn_vals = 1;
*return_vals = values;
1997-11-24 22:05:25 +00:00
values[0].type = GIMP_PDB_STATUS;
1997-11-24 22:05:25 +00:00
values[0].data.d_status = status;
/* Get the specified drawable */
drawable = gimp_drawable_get (param[2].data.d_drawable);
gimp_tile_cache_ntiles (2 * drawable->ntile_cols);
1997-11-24 22:05:25 +00:00
switch (run_mode)
{
case GIMP_RUN_INTERACTIVE:
1997-11-24 22:05:25 +00:00
/* Possibly retrieve data */
gimp_get_data (PLUG_IN_PROC, &bvals);
1997-11-24 22:05:25 +00:00
/* First acquire information with a dialog */
if (! sobel_dialog (drawable))
return;
1997-11-24 22:05:25 +00:00
break;
case GIMP_RUN_NONINTERACTIVE:
1997-11-24 22:05:25 +00:00
/* Make sure all the arguments are there! */
if (nparams != 6)
{
status = GIMP_PDB_CALLING_ERROR;
}
else
{
bvals.horizontal = (param[4].data.d_int32) ? TRUE : FALSE;
bvals.vertical = (param[5].data.d_int32) ? TRUE : FALSE;
bvals.keep_sign = (param[6].data.d_int32) ? TRUE : FALSE;
}
1997-11-24 22:05:25 +00:00
break;
case GIMP_RUN_WITH_LAST_VALS:
1997-11-24 22:05:25 +00:00
/* Possibly retrieve data */
gimp_get_data (PLUG_IN_PROC, &bvals);
1997-11-24 22:05:25 +00:00
break;
default:
break;
}
/* 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))
1997-11-24 22:05:25 +00:00
{
sobel (drawable,
bvals.horizontal, bvals.vertical, bvals.keep_sign,
NULL);
1997-11-24 22:05:25 +00:00
if (run_mode != GIMP_RUN_NONINTERACTIVE)
gimp_displays_flush ();
1997-11-24 22:05:25 +00:00
removed our own action_area API and use GtkDialog's one. Create all 2003-11-06 Michael Natterer <mitch@gimp.org> * libgimpwidgets/gimpdialog.[ch]: removed our own action_area API and use GtkDialog's one. Create all dialogs without separator. Changed almost everything else too. Fixes bug #125143. * libgimpwidgets/gimpquerybox.c * libgimpwidgets/gimpunitmenu.c: changed accordingly. * libgimp/gimpexport.[ch]: ditto. Renamed enum GimpExportReturnType to GimpExportReturn. * libgimp/gimpcompat.h: added a #define for the old name. * themes/Default/gtkrc: increased action_area border to 6 pixels. * app/display/gimpdisplayshell-filter-dialog.c * app/display/gimpdisplayshell-scale.c * app/display/gimpprogress.c * app/gui/brush-select.c * app/gui/channels-commands.c * app/gui/color-notebook.c * app/gui/convert-dialog.c * app/gui/file-new-dialog.c * app/gui/font-select.c * app/gui/gradient-editor-commands.c * app/gui/gradient-select.c * app/gui/grid-dialog.c * app/gui/image-commands.c * app/gui/info-window.c * app/gui/layers-commands.c * app/gui/module-browser.c * app/gui/offset-dialog.c * app/gui/palette-import-dialog.c * app/gui/palette-select.c * app/gui/pattern-select.c * app/gui/preferences-dialog.c * app/gui/qmask-commands.c * app/gui/resize-dialog.c * app/gui/resolution-calibrate-dialog.c * app/gui/stroke-dialog.c * app/gui/templates-commands.c * app/gui/user-install-dialog.c * app/gui/vectors-commands.c * app/tools/gimpcolorpickertool.c * app/tools/gimpcroptool.c * app/tools/gimpimagemaptool.c * app/tools/gimpmeasuretool.c * app/tools/gimptransformtool.c * app/widgets/gimptexteditor.c * app/widgets/gimptooldialog.[ch] * app/widgets/gimpviewabledialog.[ch] * app/widgets/gimpwidgets-utils.c: changed accordingly and increased the dialogs' outer borders to 6 pixels all over the place. * plug-ins/*/*.c: changed accordingly. The plug-ins may be arbitrarily broken, I tested none of them.
2003-11-06 15:27:05 +00:00
1997-11-24 22:05:25 +00:00
/* Store data */
if (run_mode == GIMP_RUN_INTERACTIVE)
gimp_set_data (PLUG_IN_PROC, &bvals, sizeof (bvals));
1997-11-24 22:05:25 +00:00
}
else
{
Cleaned up and improved the message system: 2003-06-13 Michael Natterer <mitch@gimp.org> Cleaned up and improved the message system: * app/core/gimp.[ch]: added "const gchar *domain" to GimpMessageFunc (a NULL domain means the message is from the GIMP core, everything else is a plug-in). * app/errors.c: pass "domain == NULL" to gimp_message(). * tools/pdbgen/pdb/message.pdb: derive the message domain from the current plug-in's menu_path (evil hack but works reasonably well). * app/pdb/message_cmds.c: regenerated. * app/widgets/gimpwidgets-utils.[ch] (gimp_message_box): added a header showing the message domain and changed the dialog layout to follow the HIG more closely. * app/gui/error-console-dialog.[ch]: removed. * app/widgets/gimperrorconsole.[ch] * app/gui/error-console-commands.[ch] * app/gui/error-console-menu.[ch]: new files containing a re-implementation of the error console dialog. * app/gui/Makefile.am * app/gui/dialogs-constructors.c * app/gui/gui.c * app/gui/menus.c * app/widgets/Makefile.am * app/widgets/widgets-types.h: changed accordingly. * app/display/gimpprogress.c: added more spacing and removed the separator (more HIG compliant). * plug-ins/[most plug-ins].c: Changed lots of messages and progress strings: - Removed plug-in names from messages since that's automatically covered by "domain" now. - Put all filenames in ''. - Changed "Loading" to "Opening". - Added "..." to all progress messages. - Cleaned up all file open/save error messages to look the same and include g_strerror(errno). - Removed special casing for progress bars and *always* show them, not only if run_mode != GIMP_RUN_NONINTERACTIVE (we can't expect all plug-ins to do this correctly but need to hack the core to sort out unwanted progress bars). Unrelated: - Cleaned up indentation, spacing, #includes, coding style and other stuff while I was at all these files.
2003-06-13 14:37:00 +00:00
/* g_message ("Cannot operate on indexed color images"); */
status = GIMP_PDB_EXECUTION_ERROR;
1997-11-24 22:05:25 +00:00
}
gimp_drawable_detach (drawable);
values[0].data.d_status = status;
}
static gboolean
sobel_dialog (GimpDrawable *drawable)
1997-11-24 22:05:25 +00:00
{
GtkWidget *dialog;
GtkWidget *main_vbox;
GtkWidget *preview;
GtkWidget *toggle;
removed our own action_area API and use GtkDialog's one. Create all 2003-11-06 Michael Natterer <mitch@gimp.org> * libgimpwidgets/gimpdialog.[ch]: removed our own action_area API and use GtkDialog's one. Create all dialogs without separator. Changed almost everything else too. Fixes bug #125143. * libgimpwidgets/gimpquerybox.c * libgimpwidgets/gimpunitmenu.c: changed accordingly. * libgimp/gimpexport.[ch]: ditto. Renamed enum GimpExportReturnType to GimpExportReturn. * libgimp/gimpcompat.h: added a #define for the old name. * themes/Default/gtkrc: increased action_area border to 6 pixels. * app/display/gimpdisplayshell-filter-dialog.c * app/display/gimpdisplayshell-scale.c * app/display/gimpprogress.c * app/gui/brush-select.c * app/gui/channels-commands.c * app/gui/color-notebook.c * app/gui/convert-dialog.c * app/gui/file-new-dialog.c * app/gui/font-select.c * app/gui/gradient-editor-commands.c * app/gui/gradient-select.c * app/gui/grid-dialog.c * app/gui/image-commands.c * app/gui/info-window.c * app/gui/layers-commands.c * app/gui/module-browser.c * app/gui/offset-dialog.c * app/gui/palette-import-dialog.c * app/gui/palette-select.c * app/gui/pattern-select.c * app/gui/preferences-dialog.c * app/gui/qmask-commands.c * app/gui/resize-dialog.c * app/gui/resolution-calibrate-dialog.c * app/gui/stroke-dialog.c * app/gui/templates-commands.c * app/gui/user-install-dialog.c * app/gui/vectors-commands.c * app/tools/gimpcolorpickertool.c * app/tools/gimpcroptool.c * app/tools/gimpimagemaptool.c * app/tools/gimpmeasuretool.c * app/tools/gimptransformtool.c * app/widgets/gimptexteditor.c * app/widgets/gimptooldialog.[ch] * app/widgets/gimpviewabledialog.[ch] * app/widgets/gimpwidgets-utils.c: changed accordingly and increased the dialogs' outer borders to 6 pixels all over the place. * plug-ins/*/*.c: changed accordingly. The plug-ins may be arbitrarily broken, I tested none of them.
2003-11-06 15:27:05 +00:00
gboolean run;
1997-11-24 22:05:25 +00:00
gimp_ui_init (PLUG_IN_BINARY, FALSE);
1997-11-24 22:05:25 +00:00
dialog = gimp_dialog_new (_("Sobel Edge Detection"), PLUG_IN_BINARY,
NULL, 0,
gimp_standard_help_func, PLUG_IN_PROC,
GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
GTK_STOCK_OK, GTK_RESPONSE_OK,
1997-11-24 22:05:25 +00:00
NULL);
1997-11-24 22:05:25 +00:00
gtk_dialog_set_alternative_button_order (GTK_DIALOG (dialog),
GTK_RESPONSE_OK,
GTK_RESPONSE_CANCEL,
-1);
Added parent window API to the GimpProgress interface and to the libgimp 2005-09-09 Michael Natterer <mitch@gimp.org> Added parent window API to the GimpProgress interface and to the libgimp progress stuff. Might look strange, but does the right thing in almost all cases (image window, file dialog, script-fu dialog etc). Fixes bug #62988. * app/core/gimpprogress.[ch]: added GimpProgress::get_window() which should return a toplevel window ID if the progress is in a window that wants to be the transient parent of plug-in dialogs. * app/widgets/gimpwidgets-utils.[ch] (gimp_window_get_native): new function which returns the window handle of a GtkWindow's GdkWindow. * app/widgets/gimpfiledialog.c: implement ::get_window(). * app/display/gimpdisplay.[ch]: ditto. Removed window handle API. * app/gui/gui-vtable.c: changed accordingly. * libgimpbase/gimpbaseenums.[ch] (enum GimpProgressCommand): added GIMP_PROGRESS_COMMAND_GET_WINDOW. * app/plug-in/plug-in-progress.[ch] (plug_in_progress_get_window): new function. Also renamed some functions to match the GimpProgress interface, and not the legacy PDB procedure names. * tools/pdbgen/pdb/progress.pdb * app/core/gimppdbprogress.c: implement get_window() on both sides of the wire, keeping backward compatibility (hopefully). * libgimp/gimpprogress.[ch]: deprecated gimp_progress_install() and added gimp_progress_install_vtable() which takes a vtable with padding to be extensible. Added get_window() vtable entry and dispatch it accordingly. Also added pulse() which was implemented in a hackish way before. Everything is of course backward compatible. * libgimp/gimpprogressbar.c: inmplement the get_window() stuff so a plug-in dialog containing a progress can be the transient parent of another dialog in another plug-in. * libgimp/gimpui.[ch] (gimp_ui_get_progress_window): new function which returns a foreign GdkWindow of this plug-ins progress window. Renamed gimp_window_set_transient_for_default_display() to gimp_window_set_transient() and make it use the progress' window handle instead of the display's (which is the right thing to do in almost all cases). * libgimp/gimp.def * libgimp/gimpui.def: add the new functions. * tools/pdbgen/enums.pl * app/pdb/internal_procs.c * app/pdb/progress_cmds.c * libgimp/gimpprogress_pdb.[ch]: regenerated. * libgimp/gimpexport.c * plug-ins/*/*.c: follow API change.
2005-09-09 18:07:31 +00:00
gimp_window_set_transient (GTK_WINDOW (dialog));
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);
preview = gimp_drawable_preview_new (drawable, NULL);
gtk_box_pack_start (GTK_BOX (main_vbox), preview, TRUE, TRUE, 0);
gtk_widget_show (preview);
g_signal_connect (preview, "invalidated",
G_CALLBACK (sobel_preview_update),
NULL);
toggle = gtk_check_button_new_with_mnemonic (_("Sobel _horizontally"));
gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (toggle), bvals.horizontal);
gtk_box_pack_start (GTK_BOX (main_vbox), toggle, FALSE, FALSE, 0);
1997-11-24 22:05:25 +00:00
gtk_widget_show (toggle);
g_signal_connect (toggle, "toggled",
G_CALLBACK (gimp_toggle_button_update),
&bvals.horizontal);
g_signal_connect_swapped (toggle, "toggled",
G_CALLBACK (gimp_preview_invalidate),
preview);
toggle = gtk_check_button_new_with_mnemonic (_("Sobel _vertically"));
gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (toggle), bvals.vertical);
gtk_box_pack_start (GTK_BOX (main_vbox), toggle, FALSE, FALSE, 0);
1997-11-24 22:05:25 +00:00
gtk_widget_show (toggle);
g_signal_connect (toggle, "toggled",
G_CALLBACK (gimp_toggle_button_update),
&bvals.vertical);
g_signal_connect_swapped (toggle, "toggled",
G_CALLBACK (gimp_preview_invalidate),
preview);
toggle = gtk_check_button_new_with_mnemonic (_("_Keep sign of result "
"(one direction only)"));
gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (toggle), bvals.keep_sign);
gtk_box_pack_start (GTK_BOX (main_vbox), toggle, FALSE, FALSE, 0);
1997-11-24 22:05:25 +00:00
gtk_widget_show (toggle);
g_signal_connect (toggle, "toggled",
G_CALLBACK (gimp_toggle_button_update),
&bvals.keep_sign);
g_signal_connect_swapped (toggle, "toggled",
G_CALLBACK (gimp_preview_invalidate),
preview);
gtk_widget_show (dialog);
1997-11-24 22:05:25 +00:00
run = (gimp_dialog_run (GIMP_DIALOG (dialog)) == GTK_RESPONSE_OK);
removed our own action_area API and use GtkDialog's one. Create all 2003-11-06 Michael Natterer <mitch@gimp.org> * libgimpwidgets/gimpdialog.[ch]: removed our own action_area API and use GtkDialog's one. Create all dialogs without separator. Changed almost everything else too. Fixes bug #125143. * libgimpwidgets/gimpquerybox.c * libgimpwidgets/gimpunitmenu.c: changed accordingly. * libgimp/gimpexport.[ch]: ditto. Renamed enum GimpExportReturnType to GimpExportReturn. * libgimp/gimpcompat.h: added a #define for the old name. * themes/Default/gtkrc: increased action_area border to 6 pixels. * app/display/gimpdisplayshell-filter-dialog.c * app/display/gimpdisplayshell-scale.c * app/display/gimpprogress.c * app/gui/brush-select.c * app/gui/channels-commands.c * app/gui/color-notebook.c * app/gui/convert-dialog.c * app/gui/file-new-dialog.c * app/gui/font-select.c * app/gui/gradient-editor-commands.c * app/gui/gradient-select.c * app/gui/grid-dialog.c * app/gui/image-commands.c * app/gui/info-window.c * app/gui/layers-commands.c * app/gui/module-browser.c * app/gui/offset-dialog.c * app/gui/palette-import-dialog.c * app/gui/palette-select.c * app/gui/pattern-select.c * app/gui/preferences-dialog.c * app/gui/qmask-commands.c * app/gui/resize-dialog.c * app/gui/resolution-calibrate-dialog.c * app/gui/stroke-dialog.c * app/gui/templates-commands.c * app/gui/user-install-dialog.c * app/gui/vectors-commands.c * app/tools/gimpcolorpickertool.c * app/tools/gimpcroptool.c * app/tools/gimpimagemaptool.c * app/tools/gimpmeasuretool.c * app/tools/gimptransformtool.c * app/widgets/gimptexteditor.c * app/widgets/gimptooldialog.[ch] * app/widgets/gimpviewabledialog.[ch] * app/widgets/gimpwidgets-utils.c: changed accordingly and increased the dialogs' outer borders to 6 pixels all over the place. * plug-ins/*/*.c: changed accordingly. The plug-ins may be arbitrarily broken, I tested none of them.
2003-11-06 15:27:05 +00:00
gtk_widget_destroy (dialog);
1997-11-24 22:05:25 +00:00
removed our own action_area API and use GtkDialog's one. Create all 2003-11-06 Michael Natterer <mitch@gimp.org> * libgimpwidgets/gimpdialog.[ch]: removed our own action_area API and use GtkDialog's one. Create all dialogs without separator. Changed almost everything else too. Fixes bug #125143. * libgimpwidgets/gimpquerybox.c * libgimpwidgets/gimpunitmenu.c: changed accordingly. * libgimp/gimpexport.[ch]: ditto. Renamed enum GimpExportReturnType to GimpExportReturn. * libgimp/gimpcompat.h: added a #define for the old name. * themes/Default/gtkrc: increased action_area border to 6 pixels. * app/display/gimpdisplayshell-filter-dialog.c * app/display/gimpdisplayshell-scale.c * app/display/gimpprogress.c * app/gui/brush-select.c * app/gui/channels-commands.c * app/gui/color-notebook.c * app/gui/convert-dialog.c * app/gui/file-new-dialog.c * app/gui/font-select.c * app/gui/gradient-editor-commands.c * app/gui/gradient-select.c * app/gui/grid-dialog.c * app/gui/image-commands.c * app/gui/info-window.c * app/gui/layers-commands.c * app/gui/module-browser.c * app/gui/offset-dialog.c * app/gui/palette-import-dialog.c * app/gui/palette-select.c * app/gui/pattern-select.c * app/gui/preferences-dialog.c * app/gui/qmask-commands.c * app/gui/resize-dialog.c * app/gui/resolution-calibrate-dialog.c * app/gui/stroke-dialog.c * app/gui/templates-commands.c * app/gui/user-install-dialog.c * app/gui/vectors-commands.c * app/tools/gimpcolorpickertool.c * app/tools/gimpcroptool.c * app/tools/gimpimagemaptool.c * app/tools/gimpmeasuretool.c * app/tools/gimptransformtool.c * app/widgets/gimptexteditor.c * app/widgets/gimptooldialog.[ch] * app/widgets/gimpviewabledialog.[ch] * app/widgets/gimpwidgets-utils.c: changed accordingly and increased the dialogs' outer borders to 6 pixels all over the place. * plug-ins/*/*.c: changed accordingly. The plug-ins may be arbitrarily broken, I tested none of them.
2003-11-06 15:27:05 +00:00
return run;
1997-11-24 22:05:25 +00:00
}
static void
sobel_preview_update (GimpPreview *preview)
{
sobel (gimp_drawable_preview_get_drawable (GIMP_DRAWABLE_PREVIEW (preview)),
bvals.horizontal,
bvals.vertical,
bvals.keep_sign,
preview);
}
1997-11-24 22:05:25 +00:00
static void
sobel_prepare_row (GimpPixelRgn *pixel_rgn,
guchar *data,
gint x,
gint y,
gint w)
1997-11-24 22:05:25 +00:00
{
gint b;
1997-11-24 22:05:25 +00:00
y = CLAMP (y, 0, pixel_rgn->h - 1);
gimp_pixel_rgn_get_row (pixel_rgn, data, x, y, w);
1997-11-24 22:05:25 +00:00
/* Fill in edge pixels */
for (b = 0; b < pixel_rgn->bpp; b++)
{
data[-(int)pixel_rgn->bpp + b] = data[b];
1997-11-24 22:05:25 +00:00
data[w * pixel_rgn->bpp + b] = data[(w - 1) * pixel_rgn->bpp + b];
}
}
#define RMS(a, b) (sqrt ((a) * (a) + (b) * (b)))
1997-11-24 22:05:25 +00:00
static void
sobel (GimpDrawable *drawable,
gboolean do_horizontal,
gboolean do_vertical,
gboolean keep_sign,
GimpPreview *preview)
1997-11-24 22:05:25 +00:00
{
GimpPixelRgn srcPR, destPR;
gint width, height;
gint bytes;
gint gradient, hor_gradient, ver_gradient;
guchar *dest, *d;
guchar *prev_row, *pr;
guchar *cur_row, *cr;
guchar *next_row, *nr;
guchar *tmp;
gint row, col;
gint x1, y1, x2, y2;
gboolean alpha;
gint counter;
guchar *preview_buffer = NULL;
if (preview)
{
gimp_preview_get_position (preview, &x1, &y1);
gimp_preview_get_size (preview, &width, &height);
x2 = x1 + width;
y2 = y1 + height;
}
else
{
gimp_drawable_mask_bounds (drawable->drawable_id, &x1, &y1, &x2, &y2);
gimp_progress_init (_("Sobel edge detecting"));
width = x2 - x1;
height = y2 - y1;
}
1997-11-24 22:05:25 +00:00
/* Get the size of the input image. (This will/must be the same
* as the size of the output image.
*/
bytes = drawable->bpp;
alpha = gimp_drawable_has_alpha (drawable->drawable_id);
1997-11-24 22:05:25 +00:00
/* allocate row buffers */
prev_row = g_new (guchar, (width + 2) * bytes);
cur_row = g_new (guchar, (width + 2) * bytes);
next_row = g_new (guchar, (width + 2) * bytes);
dest = g_new (guchar, width * bytes);
1997-11-24 22:05:25 +00:00
/* initialize the pixel regions */
gimp_pixel_rgn_init (&srcPR, drawable, 0, 0,
drawable->width, drawable->height,
FALSE, FALSE);
if (preview)
{
preview_buffer = g_new (guchar, width * height * bytes);
}
else
{
gimp_pixel_rgn_init (&destPR, drawable, 0, 0,
drawable->width, drawable->height,
TRUE, TRUE);
}
1997-11-24 22:05:25 +00:00
pr = prev_row + bytes;
cr = cur_row + bytes;
1997-11-24 22:05:25 +00:00
nr = next_row + bytes;
sobel_prepare_row (&srcPR, pr, x1, y1 - 1, width);
sobel_prepare_row (&srcPR, cr, x1, y1, width);
1997-11-24 22:05:25 +00:00
counter =0;
/* loop through the rows, applying the sobel convolution */
for (row = y1; row < y2; row++)
{
/* prepare the next row */
sobel_prepare_row (&srcPR, nr, x1, row + 1, width);
1997-11-24 22:05:25 +00:00
d = dest;
for (col = 0; col < width * bytes; col++)
{
hor_gradient = (do_horizontal ?
((pr[col - bytes] + 2 * pr[col] + pr[col + bytes]) -
(nr[col - bytes] + 2 * nr[col] + nr[col + bytes]))
: 0);
ver_gradient = (do_vertical ?
((pr[col - bytes] + 2 * cr[col - bytes] + nr[col - bytes]) -
(pr[col + bytes] + 2 * cr[col + bytes] + nr[col + bytes]))
: 0);
gradient = (do_vertical && do_horizontal) ?
(ROUND (RMS (hor_gradient, ver_gradient)) / 5.66) /* always >0 */
: (keep_sign ? (127 + (ROUND ((hor_gradient + ver_gradient) / 8.0)))
: (ROUND (abs (hor_gradient + ver_gradient) / 4.0)));
if (alpha && (((col + 1) % bytes) == 0))
{ /* the alpha channel */
*d++ = (counter == 0) ? 0 : 255;
counter = 0;
}
else
{
*d++ = gradient;
if (gradient > 10) counter ++;
}
}
1997-11-24 22:05:25 +00:00
/* shuffle the row pointers */
tmp = pr;
pr = cr;
cr = nr;
nr = tmp;
/* store the dest */
if (preview)
{
memcpy (preview_buffer + width * (row - y1) * bytes,
dest,
width * bytes);
}
else
{
gimp_pixel_rgn_set_row (&destPR, dest, x1, row, width);
if ((row % 20) == 0)
gimp_progress_update ((double) row / (double) (y2 - y1));
}
1997-11-24 22:05:25 +00:00
}
if (preview)
{
gimp_preview_draw_buffer (preview, preview_buffer, width * bytes);
g_free (preview_buffer);
}
else
{
/* update the sobeled region */
gimp_drawable_flush (drawable);
gimp_drawable_merge_shadow (drawable->drawable_id, TRUE);
gimp_drawable_update (drawable->drawable_id, x1, y1, width, height);
}
1997-11-24 22:05:25 +00:00
g_free (prev_row);
g_free (cur_row);
g_free (next_row);
g_free (dest);
1997-11-24 22:05:25 +00:00
}