Commit 004a5e0c authored by David Odin's avatar David Odin
Browse files

ported to GimpPreviewArea. 100 less lines of code and much more features!

* plug-ins/common/edge.c: ported to GimpPreviewArea. 100 less lines of
  code and much more features!
parent 9f0842f0
2004-09-11 DindinX <david@dindinx.org>
* plug-ins/common/edge.c: ported to GimpPreviewArea. 100 less lines of
code and much more features!
2004-09-11 DindinX <david@dindinx.org>
* plug-ins/common/oilify.c: some code cleanup and small optimisations.
......
......@@ -85,6 +85,7 @@ typedef struct
gdouble amount;
gint edgemode;
gint wrapmode;
gboolean update_preview;
} EdgeVals;
/*
......@@ -92,16 +93,15 @@ typedef struct
*/
static void query (void);
static void run (const gchar *name,
gint nparams,
const GimpParam *param,
gint *nreturn_vals,
GimpParam **return_vals);
static void run (const gchar *name,
gint nparams,
const GimpParam *param,
gint *nreturn_vals,
GimpParam **return_vals);
static void edge (GimpDrawable *drawable);
static gboolean edge_dialog (GimpDrawable *drawable);
static GtkWidget *edge_preview_new (GimpDrawable *drawable);
static void edge_preview_update (GimpDrawable *drawable);
static void edge (GimpDrawable *drawable);
static gboolean edge_dialog (GimpDrawable *drawable);
static void edge_preview_update (GimpDrawablePreview *preview);
static gint edge_detect (const guchar *data);
static gint prewitt (const guchar *data);
......@@ -128,13 +128,6 @@ static EdgeVals evals =
GIMP_PIXEL_FETCHER_EDGE_SMEAR /* wrapmode */
};
/* Preview stuff */
#define PREVIEW_SIZE 128
static GimpDrawable *drawable;
static GtkWidget *preview;
static gint delta_x = 0;
static gint delta_y = 0;
/***** Functions *****/
MAIN ()
......@@ -185,6 +178,7 @@ run (const gchar *name,
static GimpParam values[1];
GimpRunMode run_mode;
GimpPDBStatusType status = GIMP_PDB_SUCCESS;
GimpDrawable *drawable;
run_mode = param[0].data.d_int32;
......@@ -634,8 +628,9 @@ laplace (const guchar *data)
static gboolean
edge_dialog (GimpDrawable *drawable)
{
GtkWidget *dlg;
GtkWidget *vbox;
GtkWidget *dialog;
GtkWidget *main_vbox;
GtkWidget *preview;
GtkWidget *hbox;
GtkWidget *table;
GtkWidget *combo;
......@@ -650,32 +645,31 @@ edge_dialog (GimpDrawable *drawable)
gimp_ui_init ("edge", FALSE);
dlg = gimp_dialog_new (_("Edge Detection"), "edge",
NULL, 0,
gimp_standard_help_func, "plug-in-edge",
GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
GTK_STOCK_OK, GTK_RESPONSE_OK,
dialog = gimp_dialog_new (_("Edge Detection"), "edge",
NULL, 0,
gimp_standard_help_func, "plug-in-edge",
NULL);
GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
GTK_STOCK_OK, GTK_RESPONSE_OK,
vbox = gtk_vbox_new (FALSE, 12);
gtk_container_set_border_width (GTK_CONTAINER (vbox), 12);
gtk_box_pack_start_defaults (GTK_BOX (GTK_DIALOG (dlg)->vbox), vbox);
gtk_widget_show (vbox);
NULL);
hbox = gtk_hbox_new (FALSE, 0);
gtk_box_pack_start (GTK_BOX (vbox), hbox, FALSE, FALSE, 0);
gtk_widget_show (hbox);
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);
table = edge_preview_new (drawable);
gtk_box_pack_start (GTK_BOX (hbox), table, FALSE, FALSE, 0);
gtk_widget_show (table);
preview = gimp_drawable_preview_new (drawable, &evals.update_preview);
gtk_box_pack_start_defaults (GTK_BOX (main_vbox), preview);
gtk_widget_show (preview);
g_signal_connect (preview, "invalidated",
G_CALLBACK (edge_preview_update),
NULL);
table = gtk_table_new (3, 3, FALSE);
gtk_table_set_col_spacings (GTK_TABLE (table), 6);
gtk_table_set_row_spacings (GTK_TABLE (table), 6);
gtk_box_pack_start (GTK_BOX (vbox), table, FALSE, FALSE, 0);
gtk_box_pack_start (GTK_BOX (main_vbox), table, FALSE, FALSE, 0);
gtk_widget_show (table);
combo = gimp_int_combo_box_new (_("Sobel"), SOBEL,
......@@ -695,8 +689,8 @@ edge_dialog (GimpDrawable *drawable)
_("_Algorithm:"), 0.0, 0.5,
combo, 2, FALSE);
g_signal_connect_swapped (combo, "changed",
G_CALLBACK (edge_preview_update),
drawable);
G_CALLBACK (gimp_preview_invalidate),
preview);
/* Label, scale, entry for evals.amount */
scale_data = gimp_scale_entry_new (GTK_TABLE (table), 0, 1,
......@@ -709,8 +703,8 @@ edge_dialog (GimpDrawable *drawable)
G_CALLBACK (gimp_double_adjustment_update),
&evals.amount);
g_signal_connect_swapped (scale_data, "value_changed",
G_CALLBACK (edge_preview_update),
drawable);
G_CALLBACK (gimp_preview_invalidate),
preview);
/* Radio buttons WRAP, SMEAR, BLACK */
......@@ -729,8 +723,8 @@ edge_dialog (GimpDrawable *drawable)
G_CALLBACK (gimp_toggle_button_update),
&use_wrap);
g_signal_connect_swapped (toggle, "toggled",
G_CALLBACK (edge_preview_update),
drawable);
G_CALLBACK (gimp_preview_invalidate),
preview);
toggle = gtk_radio_button_new_with_mnemonic (group, _("_Smear"));
group = gtk_radio_button_get_group (GTK_RADIO_BUTTON (toggle));
......@@ -742,8 +736,8 @@ edge_dialog (GimpDrawable *drawable)
G_CALLBACK (gimp_toggle_button_update),
&use_smear);
g_signal_connect_swapped (toggle, "toggled",
G_CALLBACK (edge_preview_update),
drawable);
G_CALLBACK (gimp_preview_invalidate),
preview);
toggle = gtk_radio_button_new_with_mnemonic (group, _("_Black"));
group = gtk_radio_button_get_group (GTK_RADIO_BUTTON (toggle));
......@@ -755,16 +749,14 @@ edge_dialog (GimpDrawable *drawable)
G_CALLBACK (gimp_toggle_button_update),
&use_black);
g_signal_connect_swapped (toggle, "toggled",
G_CALLBACK (edge_preview_update),
drawable);
gtk_widget_show (dlg);
G_CALLBACK (gimp_preview_invalidate),
preview);
edge_preview_update (drawable);
gtk_widget_show (dialog);
run = (gimp_dialog_run (GIMP_DIALOG (dlg)) == GTK_RESPONSE_OK);
run = (gimp_dialog_run (GIMP_DIALOG (dialog)) == GTK_RESPONSE_OK);
gtk_widget_destroy (dlg);
gtk_widget_destroy (dialog);
if (use_wrap)
evals.wrapmode = GIMP_PIXEL_FETCHER_EDGE_WRAP;
......@@ -776,84 +768,11 @@ edge_dialog (GimpDrawable *drawable)
return run;
}
/* Preview stuff */
static GtkWidget *
edge_preview_new (GimpDrawable *drawable)
{
GtkWidget *table;
GtkWidget *frame;
GtkObject *adj;
GtkWidget *scrollbar;
gint sel_width;
gint sel_height;
gint x1, y1, x2, y2;
gint preview_width;
gint preview_height;
gimp_drawable_mask_bounds (drawable->drawable_id, &x1, &y1, &x2, &y2);
sel_width = x2 - x1;
sel_height = y2 - y1;
preview_width = MIN (sel_width, PREVIEW_SIZE);
preview_height = MIN (sel_height, PREVIEW_SIZE);
table = gtk_table_new (2, 2, FALSE);
frame = gtk_frame_new (NULL);
gtk_frame_set_shadow_type (GTK_FRAME (frame), GTK_SHADOW_IN);
gtk_table_attach (GTK_TABLE (table), frame, 0, 1, 0, 1, 0, 0, 0, 0);
gtk_widget_show (frame);
preview = gimp_preview_area_new ();
gtk_widget_set_size_request (preview, preview_width, preview_height);
gtk_container_add (GTK_CONTAINER (frame), preview);
gtk_widget_show (preview);
adj = gtk_adjustment_new (0, 0, sel_width - 1, 1.0,
MIN (preview_width, sel_width),
MIN (preview_width, sel_width));
g_signal_connect (adj, "value_changed",
G_CALLBACK (gimp_int_adjustment_update),
&delta_x);
g_signal_connect_swapped (adj, "value_changed",
G_CALLBACK (edge_preview_update),
drawable);
scrollbar = gtk_hscrollbar_new (GTK_ADJUSTMENT (adj));
gtk_range_set_update_policy (GTK_RANGE (scrollbar),
GTK_UPDATE_CONTINUOUS);
gtk_table_attach (GTK_TABLE (table), scrollbar, 0, 1, 1, 2,
GTK_FILL, 0, 0, 0);
gtk_widget_show (scrollbar);
adj = gtk_adjustment_new (0, 0, sel_height - 1, 1.0,
MIN (preview_height, sel_height),
MIN (preview_height, sel_height));
g_signal_connect (adj, "value_changed",
G_CALLBACK (gimp_int_adjustment_update),
&delta_y);
g_signal_connect_swapped (adj, "value_changed",
G_CALLBACK (edge_preview_update),
drawable);
scrollbar = gtk_vscrollbar_new (GTK_ADJUSTMENT (adj));
gtk_range_set_update_policy (GTK_RANGE (scrollbar),
GTK_UPDATE_CONTINUOUS);
gtk_table_attach (GTK_TABLE (table), scrollbar, 1, 2, 0, 1,
0, GTK_FILL, 0, 0);
gtk_widget_show (scrollbar);
return table;
}
static void
edge_preview_update (GimpDrawable *drawable)
edge_preview_update (GimpDrawablePreview *preview)
{
/* drawable */
glong width, height;
glong bytes;
gint x1, y1, x2, y2;
gint alpha;
gboolean has_alpha;
......@@ -861,59 +780,49 @@ edge_preview_update (GimpDrawable *drawable)
guchar *src = NULL; /* Buffer to hold source image */
guchar *render_buffer = NULL; /* Buffer to hold rendered image */
guchar *dest;
gint preview_width; /* Width of preview widget */
gint preview_height; /* Height of preview widget */
gint preview_x1; /* Upper-left X of preview */
gint preview_y1; /* Upper-left Y of preview */
gint preview_x2; /* Lower-right X of preview */
gint preview_y2; /* Lower-right Y of preview */
gint width; /* Width of preview widget */
gint height; /* Height of preview widget */
gint x1; /* Upper-left X of preview */
gint y1; /* Upper-left Y of preview */
GimpPixelRgn srcPR; /* Pixel regions */
/* algorithm */
gint x, y;
/* Get drawable info */
gimp_drawable_mask_bounds (drawable->drawable_id, &x1, &y1, &x2, &y2);
width = drawable->width;
height = drawable->height;
bytes = gimp_drawable_bpp (drawable->drawable_id);
bytes = gimp_drawable_bpp (preview->drawable->drawable_id);
alpha = bytes;
has_alpha = gimp_drawable_has_alpha (drawable->drawable_id);
has_alpha = gimp_drawable_has_alpha (preview->drawable->drawable_id);
if (has_alpha)
alpha--;
/*
* Setup for filter...
*/
preview_x1 = x1 + delta_x;
preview_y1 = y1 + delta_y;
preview_x2 = preview_x1 + MIN (PREVIEW_SIZE, x2 - x1);
preview_y2 = preview_y1 + MIN (PREVIEW_SIZE, y2 - y1);
preview_width = preview_x2 - preview_x1;
preview_height = preview_y2 - preview_y1;
gimp_preview_get_position (GIMP_PREVIEW (preview), &x1, &y1);
gimp_preview_get_size (GIMP_PREVIEW (preview),
&width, &height);
/* initialize pixel regions */
gimp_pixel_rgn_init (&srcPR, drawable,
preview_x1,preview_y1,
preview_width, preview_height, FALSE, FALSE);
src = g_new (guchar, preview_width * preview_height * bytes);
render_buffer = g_new (guchar, preview_width * preview_height * bytes);
gimp_pixel_rgn_init (&srcPR, preview->drawable,
x1, y1, width, height, FALSE, FALSE);
src = g_new (guchar, width * height * bytes);
render_buffer = g_new (guchar, width * height * bytes);
/* render image */
gimp_pixel_rgn_get_rect(&srcPR, src,
preview_x1, preview_y1,
preview_width, preview_height);
gimp_pixel_rgn_get_rect(&srcPR, src, x1, y1, width, height);
dest = render_buffer;
/* render algorithm */
for (y = 0 ; y < preview_height ; y++)
for (x = 0 ; x < preview_width ; x++)
for (y = 0 ; y < height ; y++)
for (x = 0 ; x < width ; x++)
{
gint chan;
for (chan = 0; chan < alpha; chan++)
{
guchar kernel[9];
#define SRC(X,Y) src[bytes * ( CLAMP((X),0,preview_width) + preview_width * CLAMP((Y),0,preview_height) )+chan]
#define SRC(X,Y) src[bytes * ( CLAMP((X),0,width) + width * CLAMP((Y),0,height) )+chan]
kernel[0] = SRC(x-1, y-1);
kernel[1] = SRC(x-1, y );
kernel[2] = SRC(x-1, y+1);
......@@ -927,17 +836,13 @@ edge_preview_update (GimpDrawable *drawable)
dest[chan] = edge_detect (kernel);
}
if (has_alpha)
dest[alpha] = src[bytes * (x + preview_width * y) + alpha];
dest[alpha] = src[bytes * (x + width * y) + alpha];
dest += bytes;
}
/*
* Draw the preview image on the screen...
*/
gimp_preview_area_draw (GIMP_PREVIEW_AREA (preview),
0, 0, preview_width, preview_height,
gimp_drawable_type (drawable->drawable_id),
render_buffer,
preview_width * bytes);
gimp_drawable_preview_draw (preview, render_buffer);
g_free (render_buffer);
g_free (src);
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment