ported to GimpPreviewArea.

* plug-ins/common/whirlpinch.c: ported to GimpPreviewArea.
This commit is contained in:
David Odin 2004-08-02 08:51:49 +00:00
parent b099e695e7
commit ff27b1f157
2 changed files with 177 additions and 216 deletions

View file

@ -1,3 +1,7 @@
2004-08-02 DindinX <david@dindinx.org>
* plug-ins/common/whirlpinch.c: ported to GimpPreviewArea.
2004-08-02 DindinX <david@dindinx.org>
* plug-ins/common/video.c: ported to GimpPreviewArea.

View file

@ -53,11 +53,6 @@
#include <unistd.h>
#endif
#ifdef __GNUC__
#warning GTK_DISABLE_DEPRECATED
#endif
#undef GTK_DISABLE_DEPRECATED
#include <gtk/gtk.h>
#include <libgimp/gimp.h>
@ -88,8 +83,6 @@ typedef struct
typedef struct
{
GtkWidget *preview;
guchar *check_row_0;
guchar *check_row_1;
guchar *image;
guchar *dimage;
} whirl_pinch_interface_t;
@ -98,10 +91,10 @@ typedef struct
static void query (void);
static void run (const gchar *name,
gint nparams,
const GimpParam *param,
gint *nreturn_vals,
GimpParam **return_vals);
gint nparams,
const GimpParam *param,
gint *nreturn_vals,
GimpParam **return_vals);
static void whirl_pinch (void);
static int calc_undistorted_coords (double wx, double wy,
@ -135,8 +128,6 @@ static whirl_pinch_vals_t wpvals =
static whirl_pinch_interface_t wpint =
{
NULL, /* preview */
NULL, /* check_row_0 */
NULL, /* check_row_1 */
NULL, /* image */
NULL /* dimage */
};
@ -172,22 +163,22 @@ query (void)
};
gimp_install_procedure (PLUG_IN_NAME,
"Distort an image by whirling and pinching",
"Distorts the image by whirling and pinching, which "
"are two common center-based, circular distortions. "
"Whirling is like projecting the image onto the "
"surface of water in a toilet and flushing. "
"Pinching is similar to projecting the image onto "
"an elastic surface and pressing or pulling on the "
"center of the surface.",
"Federico Mena Quintero and Scott Goehring",
"Federico Mena Quintero and Scott Goehring",
PLUG_IN_VERSION,
N_("W_hirl and Pinch..."),
"RGB*, GRAY*",
GIMP_PLUGIN,
G_N_ELEMENTS (args), 0,
args, NULL);
"Distort an image by whirling and pinching",
"Distorts the image by whirling and pinching, which "
"are two common center-based, circular distortions. "
"Whirling is like projecting the image onto the "
"surface of water in a toilet and flushing. "
"Pinching is similar to projecting the image onto "
"an elastic surface and pressing or pulling on the "
"center of the surface.",
"Federico Mena Quintero and Scott Goehring",
"Federico Mena Quintero and Scott Goehring",
PLUG_IN_VERSION,
N_("W_hirl and Pinch..."),
"RGB*, GRAY*",
GIMP_PLUGIN,
G_N_ELEMENTS (args), 0,
args, NULL);
gimp_plugin_menu_register (PLUG_IN_NAME,
N_("<Image>/Filters/Distorts"));
@ -227,7 +218,7 @@ run (const gchar *name,
img_has_alpha = gimp_drawable_has_alpha (drawable->drawable_id);
gimp_drawable_mask_bounds (drawable->drawable_id,
&sel_x1, &sel_y1, &sel_x2, &sel_y2);
&sel_x1, &sel_y1, &sel_x2, &sel_y2);
/* Calculate scaling parameters */
@ -284,22 +275,22 @@ run (const gchar *name,
/* Get information from the dialog */
if (!whirl_pinch_dialog ())
return;
return;
break;
case GIMP_RUN_NONINTERACTIVE:
/* Make sure all the arguments are present */
if (nparams != 6)
{
status = GIMP_PDB_CALLING_ERROR;
}
{
status = GIMP_PDB_CALLING_ERROR;
}
else
{
wpvals.whirl = param[3].data.d_float;
wpvals.pinch = param[4].data.d_float;
wpvals.radius = param[5].data.d_float;
}
{
wpvals.whirl = param[3].data.d_float;
wpvals.pinch = param[4].data.d_float;
wpvals.radius = param[5].data.d_float;
}
break;
@ -319,19 +310,19 @@ run (const gchar *name,
{
/* Set the tile cache size */
gimp_tile_cache_ntiles (2 * (drawable->width + gimp_tile_width () - 1) /
gimp_tile_width ());
gimp_tile_width ());
/* Run! */
whirl_pinch ();
/* If run mode is interactive, flush displays */
if (run_mode != GIMP_RUN_NONINTERACTIVE)
gimp_displays_flush ();
gimp_displays_flush ();
/* Store data */
if (run_mode == GIMP_RUN_INTERACTIVE)
gimp_set_data (PLUG_IN_NAME, &wpvals, sizeof (whirl_pinch_vals_t));
gimp_set_data (PLUG_IN_NAME, &wpvals, sizeof (whirl_pinch_vals_t));
}
else if (status == GIMP_PDB_SUCCESS)
status = GIMP_PDB_EXECUTION_ERROR;
@ -366,7 +357,7 @@ whirl_pinch (void)
/* Initialize pixel region */
gimp_pixel_rgn_init (&dest_rgn, drawable,
sel_x1, sel_y1, sel_width, sel_height, TRUE, TRUE);
sel_x1, sel_y1, sel_width, sel_height, TRUE, TRUE);
pft = gimp_pixel_fetcher_new (drawable, FALSE);
pfb = gimp_pixel_fetcher_new (drawable, FALSE);
@ -400,87 +391,87 @@ whirl_pinch (void)
bot_p = bot_row + img_bpp * (sel_width - 1);
for (col = sel_x1; col < sel_x2; col++)
{
if (calc_undistorted_coords (col, row, whirl, wpvals.pinch, &cx, &cy))
{
/* We are inside the distortion area */
{
if (calc_undistorted_coords (col, row, whirl, wpvals.pinch, &cx, &cy))
{
/* We are inside the distortion area */
/* Top */
/* Top */
if (cx >= 0.0)
ix = (int) cx;
else
ix = -((int) -cx + 1);
if (cx >= 0.0)
ix = (int) cx;
else
ix = -((int) -cx + 1);
if (cy >= 0.0)
iy = (int) cy;
else
iy = -((int) -cy + 1);
if (cy >= 0.0)
iy = (int) cy;
else
iy = -((int) -cy + 1);
gimp_pixel_fetcher_get_pixel (pft, ix, iy, pixel[0]);
gimp_pixel_fetcher_get_pixel (pft, ix + 1, iy, pixel[1]);
gimp_pixel_fetcher_get_pixel (pft, ix, iy + 1, pixel[2]);
gimp_pixel_fetcher_get_pixel (pft, ix + 1, iy + 1, pixel[3]);
gimp_pixel_fetcher_get_pixel (pft, ix, iy, pixel[0]);
gimp_pixel_fetcher_get_pixel (pft, ix + 1, iy, pixel[1]);
gimp_pixel_fetcher_get_pixel (pft, ix, iy + 1, pixel[2]);
gimp_pixel_fetcher_get_pixel (pft, ix + 1, iy + 1, pixel[3]);
gimp_bilinear_pixels_8 (top_p, cx, cy, img_bpp, img_has_alpha,
pixel);
top_p += img_bpp;
/* Bottom */
/* Bottom */
cx = cen_x + (cen_x - cx);
cy = cen_y + (cen_y - cy);
cx = cen_x + (cen_x - cx);
cy = cen_y + (cen_y - cy);
if (cx >= 0.0)
ix = (int) cx;
else
ix = -((int) -cx + 1);
if (cx >= 0.0)
ix = (int) cx;
else
ix = -((int) -cx + 1);
if (cy >= 0.0)
iy = (int) cy;
else
iy = -((int) -cy + 1);
if (cy >= 0.0)
iy = (int) cy;
else
iy = -((int) -cy + 1);
gimp_pixel_fetcher_get_pixel (pfb, ix, iy, pixel[0]);
gimp_pixel_fetcher_get_pixel (pfb, ix + 1, iy, pixel[1]);
gimp_pixel_fetcher_get_pixel (pfb, ix, iy + 1, pixel[2]);
gimp_pixel_fetcher_get_pixel (pfb, ix + 1, iy + 1, pixel[3]);
gimp_pixel_fetcher_get_pixel (pfb, ix, iy, pixel[0]);
gimp_pixel_fetcher_get_pixel (pfb, ix + 1, iy, pixel[1]);
gimp_pixel_fetcher_get_pixel (pfb, ix, iy + 1, pixel[2]);
gimp_pixel_fetcher_get_pixel (pfb, ix + 1, iy + 1, pixel[3]);
gimp_bilinear_pixels_8 (bot_p, cx, cy, img_bpp, img_has_alpha,
pixel);
bot_p -= img_bpp;
}
else
{
/* We are outside the distortion area;
* just copy the source pixels
*/
}
else
{
/* We are outside the distortion area;
* just copy the source pixels
*/
/* Top */
/* Top */
gimp_pixel_fetcher_get_pixel (pft, col, row, pixel[0]);
gimp_pixel_fetcher_get_pixel (pft, col, row, pixel[0]);
for (i = 0; i < img_bpp; i++)
*top_p++ = pixel[0][i];
for (i = 0; i < img_bpp; i++)
*top_p++ = pixel[0][i];
/* Bottom */
/* Bottom */
gimp_pixel_fetcher_get_pixel (pfb,
(sel_x2 - 1) - (col - sel_x1),
(sel_y2 - 1) - (row - sel_y1),
pixel[0]);
gimp_pixel_fetcher_get_pixel (pfb,
(sel_x2 - 1) - (col - sel_x1),
(sel_y2 - 1) - (row - sel_y1),
pixel[0]);
for (i = 0; i < img_bpp; i++)
*bot_p++ = pixel[0][i];
for (i = 0; i < img_bpp; i++)
*bot_p++ = pixel[0][i];
bot_p -= 2 * img_bpp; /* We move backwards! */
}
}
bot_p -= 2 * img_bpp; /* We move backwards! */
}
}
/* Paint rows to image */
gimp_pixel_rgn_set_row (&dest_rgn, top_row, sel_x1, row, sel_width);
gimp_pixel_rgn_set_row (&dest_rgn, bot_row,
sel_x1, (sel_y2 - 1) - (row - sel_y1), sel_width);
sel_x1, (sel_y2 - 1) - (row - sel_y1), sel_width);
/* Update progress */
@ -500,16 +491,16 @@ whirl_pinch (void)
gimp_drawable_flush (drawable);
gimp_drawable_merge_shadow (drawable->drawable_id, TRUE);
gimp_drawable_update (drawable->drawable_id,
sel_x1, sel_y1, sel_width, sel_height);
sel_x1, sel_y1, sel_width, sel_height);
}
static gint
calc_undistorted_coords (gdouble wx,
gdouble wy,
gdouble whirl,
gdouble pinch,
gdouble *x,
gdouble *y)
gdouble wy,
gdouble whirl,
gdouble pinch,
gdouble *x,
gdouble *y)
{
gdouble dx, dy;
gdouble d, factor;
@ -575,10 +566,8 @@ build_preview_source_image (void)
guchar pixel[4];
GimpPixelFetcher *pf;
wpint.check_row_0 = g_new (guchar, preview_width);
wpint.check_row_1 = g_new (guchar, preview_width);
wpint.image = g_new (guchar, preview_width * preview_height * 4);
wpint.dimage = g_new (guchar, preview_width * preview_height * 3);
wpint.dimage = g_new (guchar, preview_width * preview_height * 4);
left = sel_x1;
right = sel_x2 - 1;
@ -599,45 +588,32 @@ build_preview_source_image (void)
px = left;
for (x = 0; x < preview_width; x++)
{
/* Checks */
{
/* Thumbnail image */
if ((x / GIMP_CHECK_SIZE) & 1)
{
wpint.check_row_0[x] = GIMP_CHECK_DARK * 255;
wpint.check_row_1[x] = GIMP_CHECK_LIGHT * 255;
}
else
{
wpint.check_row_0[x] = GIMP_CHECK_LIGHT * 255;
wpint.check_row_1[x] = GIMP_CHECK_DARK * 255;
}
gimp_pixel_fetcher_get_pixel (pf, (int) px, (int) py, pixel);
/* Thumbnail image */
if (img_bpp < 3)
{
if (img_has_alpha)
pixel[3] = pixel[1];
else
pixel[3] = 255;
gimp_pixel_fetcher_get_pixel (pf, (int) px, (int) py, pixel);
pixel[1] = pixel[0];
pixel[2] = pixel[0];
}
else
if (!img_has_alpha)
pixel[3] = 255;
if (img_bpp < 3)
{
if (img_has_alpha)
pixel[3] = pixel[1];
else
pixel[3] = 255;
*p++ = pixel[0];
*p++ = pixel[1];
*p++ = pixel[2];
*p++ = pixel[3];
pixel[1] = pixel[0];
pixel[2] = pixel[0];
}
else
if (!img_has_alpha)
pixel[3] = 255;
*p++ = pixel[0];
*p++ = pixel[1];
*p++ = pixel[2];
*p++ = pixel[3];
px += dx;
}
px += dx;
}
py += dy;
}
@ -662,7 +638,7 @@ whirl_pinch_dialog (void)
dialog = gimp_dialog_new (_("Whirl and Pinch"), "whirlpinch",
NULL, 0,
gimp_standard_help_func, HELP_ID,
gimp_standard_help_func, HELP_ID,
GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
GTK_STOCK_OK, GTK_RESPONSE_OK,
@ -672,7 +648,7 @@ whirl_pinch_dialog (void)
vbox = gtk_vbox_new (FALSE, 12);
gtk_container_set_border_width (GTK_CONTAINER (vbox), 12);
gtk_box_pack_start (GTK_BOX (GTK_DIALOG(dialog)->vbox), vbox,
FALSE, FALSE, 0);
FALSE, FALSE, 0);
gtk_widget_show (vbox);
/* Preview */
@ -686,9 +662,9 @@ whirl_pinch_dialog (void)
gtk_container_add (GTK_CONTAINER (abox), frame);
gtk_widget_show (frame);
wpint.preview = gtk_preview_new (GTK_PREVIEW_COLOR);
gtk_preview_size (GTK_PREVIEW (wpint.preview),
preview_width, preview_height);
wpint.preview = gimp_preview_area_new ();
gtk_widget_set_size_request (wpint.preview,
preview_width, preview_height);
gtk_container_add (GTK_CONTAINER (frame), wpint.preview);
gtk_widget_show (wpint.preview);
@ -700,28 +676,28 @@ whirl_pinch_dialog (void)
gtk_widget_show (table);
adj = gimp_scale_entry_new (GTK_TABLE (table), 0, 0,
_("_Whirl angle:"), SCALE_WIDTH, 7,
wpvals.whirl, -360.0, 360.0, 1.0, 15.0, 2,
TRUE, 0, 0,
NULL, NULL);
_("_Whirl angle:"), SCALE_WIDTH, 7,
wpvals.whirl, -360.0, 360.0, 1.0, 15.0, 2,
TRUE, 0, 0,
NULL, NULL);
g_signal_connect (adj, "value_changed",
G_CALLBACK (dialog_scale_update),
&wpvals.whirl);
adj = gimp_scale_entry_new (GTK_TABLE (table), 0, 1,
_("_Pinch amount:"), SCALE_WIDTH, 7,
wpvals.pinch, -1.0, 1.0, 0.01, 0.1, 3,
TRUE, 0, 0,
NULL, NULL);
_("_Pinch amount:"), SCALE_WIDTH, 7,
wpvals.pinch, -1.0, 1.0, 0.01, 0.1, 3,
TRUE, 0, 0,
NULL, NULL);
g_signal_connect (adj, "value_changed",
G_CALLBACK (dialog_scale_update),
&wpvals.pinch);
adj = gimp_scale_entry_new (GTK_TABLE (table), 0, 2,
_("_Radius:"), SCALE_WIDTH, 7,
wpvals.radius, 0.0, 2.0, 0.01, 0.1, 3,
TRUE, 0, 0,
NULL, NULL);
_("_Radius:"), SCALE_WIDTH, 7,
wpvals.radius, 0.0, 2.0, 0.01, 0.1, 3,
TRUE, 0, 0,
NULL, NULL);
g_signal_connect (adj, "value_changed",
G_CALLBACK (dialog_scale_update),
&wpvals.radius);
@ -735,8 +711,6 @@ whirl_pinch_dialog (void)
gtk_widget_destroy (dialog);
g_free (wpint.check_row_0);
g_free (wpint.check_row_1);
g_free (wpint.image);
g_free (wpint.dimage);
@ -755,8 +729,6 @@ dialog_update_preview (void)
gdouble whirl;
gdouble scale_x, scale_y;
guchar *p_ul, *p_lr, *i, *p;
guchar *check_ul, *check_lr;
gint check;
guchar outside[4];
GimpRGB background;
@ -776,13 +748,13 @@ dialog_update_preview (void)
case 3:
gimp_rgb_get_uchar (&background,
&outside[0], &outside[1], &outside[2]);
&outside[0], &outside[1], &outside[2]);
outside[3] = 255;
break;
case 4:
gimp_rgb_get_uchar (&background,
&outside[0], &outside[1], &outside[2]);
&outside[0], &outside[1], &outside[2]);
outside[3] = 0;
break;
}
@ -804,88 +776,73 @@ dialog_update_preview (void)
py = top;
p_ul = wpint.dimage;
p_lr = wpint.dimage + 3 * (preview_width * preview_height - 1);
p_lr = wpint.dimage + 4 * (preview_width * preview_height - 1);
for (y = 0; y <= (preview_height / 2); y++)
{
px = left;
if ((y / GIMP_CHECK_SIZE) & 1)
check_ul = wpint.check_row_0;
else
check_ul = wpint.check_row_1;
if (((preview_height - y - 1) / GIMP_CHECK_SIZE) & 1)
check_lr = wpint.check_row_0;
else
check_lr = wpint.check_row_1;
for (x = 0; x < preview_width; x++)
{
calc_undistorted_coords (px, py, whirl, wpvals.pinch, &cx, &cy);
{
calc_undistorted_coords (px, py, whirl, wpvals.pinch, &cx, &cy);
cx = (cx - left) * scale_x;
cy = (cy - top) * scale_y;
cx = (cx - left) * scale_x;
cy = (cy - top) * scale_y;
/* Upper left mirror */
/* Upper left mirror */
ix = (int) (cx + 0.5);
iy = (int) (cy + 0.5);
ix = (int) (cx + 0.5);
iy = (int) (cy + 0.5);
check = check_ul[x];
if ((ix >= 0) && (ix < preview_width) &&
(iy >= 0) && (iy < preview_height))
i = wpint.image + 4 * (preview_width * iy + ix);
else
i = outside;
if ((ix >= 0) && (ix < preview_width) &&
(iy >= 0) && (iy < preview_height))
i = wpint.image + 4 * (preview_width * iy + ix);
else
i = outside;
p_ul[0] = i[0];
p_ul[1] = i[1];
p_ul[2] = i[2];
p_ul[3] = i[3];
p_ul[0] = check + ((i[0] - check) * i[3]) / 255;
p_ul[1] = check + ((i[1] - check) * i[3]) / 255;
p_ul[2] = check + ((i[2] - check) * i[3]) / 255;
p_ul += 4;
p_ul += 3;
/* Lower right mirror */
/* Lower right mirror */
ix = preview_width - ix - 1;
iy = preview_height - iy - 1;
ix = preview_width - ix - 1;
iy = preview_height - iy - 1;
if ((ix >= 0) && (ix < preview_width) &&
(iy >= 0) && (iy < preview_height))
i = wpint.image + 4 * (preview_width * iy + ix);
else
i = outside;
check = check_lr[preview_width - x - 1];
p_lr[0] = i[0];
p_lr[1] = i[1];
p_lr[2] = i[2];
p_lr[3] = i[3];
if ((ix >= 0) && (ix < preview_width) &&
(iy >= 0) && (iy < preview_height))
i = wpint.image + 4 * (preview_width * iy + ix);
else
i = outside;
p_lr -= 4;
p_lr[0] = check + ((i[0] - check) * i[3]) / 255;
p_lr[1] = check + ((i[1] - check) * i[3]) / 255;
p_lr[2] = check + ((i[2] - check) * i[3]) / 255;
p_lr -= 3;
px += dx;
}
px += dx;
}
py += dy;
}
p = wpint.dimage;
for (y = 0; y < preview_height; y++)
{
gtk_preview_draw_row (GTK_PREVIEW (wpint.preview), p, 0, y, preview_width);
p += preview_width * 3;
}
gtk_widget_queue_draw (wpint.preview);
gimp_preview_area_draw (GIMP_PREVIEW_AREA (wpint.preview),
0, 0, preview_width, preview_height,
GIMP_RGBA_IMAGE,
wpint.dimage,
preview_width * 4);
}
static void
dialog_scale_update (GtkAdjustment *adjustment,
gdouble *value)
gdouble *value)
{
gimp_double_adjustment_update (adjustment, value);