/* 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.
*/
/* This plugin by thorsten@arch.usyd.edu.au */
/* Based on S&P's Gauss and Blur filters */
#include "config.h"
#include <stdlib.h>
#include <string.h>
#include <gtk/gtk.h>
#include <libgimp/gimp.h>
#include <libgimp/gimpui.h>
#include "libgimp/stdplugins-intl.h"
typedef struct
{
gboolean horizontal;
gboolean vertical;
gboolean keep_sign;
} 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);
/*
* Sobel interface
*/
static gboolean sobel_dialog (GimpDrawable *drawable);
static void sobel_preview_update (GimpPreview *preview);
/*
* Sobel helper functions
*/
static void sobel_prepare_row (GimpPixelRgn *pixel_rgn,
guchar *data,
gint x,
gint y,
gint w);
GimpPlugInInfo PLUG_IN_INFO =
{
NULL, /* init_proc */
NULL, /* quit_proc */
query, /* query_proc */
run, /* run_proc */
};
static SobelValues bvals =
{
TRUE, /* horizontal sobel */
TRUE, /* vertical sobel */
TRUE /* keep sign */
};
MAIN ()
static void
query (void)
{
static GimpParamDef args[] =
{
{ 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)" }
};
gimp_install_procedure ("plug_in_sobel",
"Edge Detection with Sobel Operation",
"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_sobel", "<Image>/Filters/Edge-Detect");
}
static void
run (const gchar *name,
gint nparams,
const GimpParam *param,
gint *nreturn_vals,
GimpParam **return_vals)
{
static GimpParam values[1];
GimpDrawable *drawable;
GimpRunMode run_mode;
GimpPDBStatusType status = GIMP_PDB_SUCCESS;
run_mode = param[0].data.d_int32;
INIT_I18N ();
*nreturn_vals = 1;
*return_vals = values;
values[0].type = GIMP_PDB_STATUS;
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);
switch (run_mode)
{
case GIMP_RUN_INTERACTIVE:
/* Possibly retrieve data */
gimp_get_data ("plug_in_sobel", &bvals);
/* First acquire information with a dialog */
if (! sobel_dialog (drawable))
return;
break;
case GIMP_RUN_NONINTERACTIVE:
/* 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;
}
break;
case GIMP_RUN_WITH_LAST_VALS:
/* Possibly retrieve data */
gimp_get_data ("plug_in_sobel", &bvals);
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))
{
sobel (drawable,
bvals.horizontal, bvals.vertical, bvals.keep_sign,
NULL);
if (run_mode != GIMP_RUN_NONINTERACTIVE)
gimp_displays_flush ();
/* Store data */
if (run_mode == GIMP_RUN_INTERACTIVE)
gimp_set_data ("plug_in_sobel", &bvals, sizeof (bvals));
}
else
{
/* g_message ("Cannot operate on indexed color images"); */
status = GIMP_PDB_EXECUTION_ERROR;
}
gimp_drawable_detach (drawable);
values[0].data.d_status = status;
}
static gboolean
sobel_dialog (GimpDrawable *drawable)
{
GtkWidget *dialog;
GtkWidget *main_vbox;
GtkWidget *preview;
GtkWidget *toggle;
gboolean run;
gimp_ui_init ("sobel", FALSE);
dialog = gimp_dialog_new (_("Sobel Edge Detection"), "sobel",
NULL, 0,
gimp_standard_help_func, "plug-in-sobel",
GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
GTK_STOCK_OK, GTK_RESPONSE_OK,
NULL);
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);
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),