mirror of
https://gitlab.gnome.org/GNOME/gimp
synced 2024-10-22 12:32:37 +00:00
Added a preview and done some cleanups.
* plug-ins/common/cubism.c: Added a preview and done some cleanups.
This commit is contained in:
parent
abc9110b30
commit
e31b8f6a41
|
@ -1,3 +1,7 @@
|
|||
2004-10-13 DindinX <dindinx@gimp.org>
|
||||
|
||||
* plug-ins/common/cubism.c: Added a preview and done some cleanups.
|
||||
|
||||
2004-10-13 Sven Neumann <sven@gimp.org>
|
||||
|
||||
* app/actions/plug-in-commands.c
|
||||
|
|
|
@ -50,53 +50,62 @@ typedef struct
|
|||
|
||||
typedef struct
|
||||
{
|
||||
gdouble tile_size;
|
||||
gdouble tile_saturation;
|
||||
gint bg_color;
|
||||
gdouble tile_size;
|
||||
gdouble tile_saturation;
|
||||
gint bg_color;
|
||||
gboolean preview;
|
||||
} CubismVals;
|
||||
|
||||
/* 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 cubism (GimpDrawable *drawable);
|
||||
static void query (void);
|
||||
static void run (const gchar *name,
|
||||
gint nparams,
|
||||
const GimpParam *param,
|
||||
gint *nreturn_vals,
|
||||
GimpParam **return_vals);
|
||||
|
||||
static void fill_poly_color (Polygon *poly,
|
||||
GimpDrawable *drawable,
|
||||
guchar *col);
|
||||
static void convert_segment (gint x1,
|
||||
gint y1,
|
||||
gint x2,
|
||||
gint y2,
|
||||
gint offset,
|
||||
gint *min,
|
||||
gint *max);
|
||||
static void randomize_indices (gint count,
|
||||
gint *indices);
|
||||
static gdouble calc_alpha_blend (gdouble *vec,
|
||||
gdouble one_over_dist,
|
||||
gdouble x,
|
||||
gdouble y);
|
||||
static void polygon_add_point (Polygon *poly,
|
||||
gdouble x,
|
||||
gdouble y);
|
||||
static void polygon_translate (Polygon *poly,
|
||||
gdouble tx,
|
||||
gdouble ty);
|
||||
static void polygon_rotate (Polygon *poly,
|
||||
gdouble theta);
|
||||
static gint polygon_extents (Polygon *poly,
|
||||
gdouble *min_x,
|
||||
gdouble *min_y,
|
||||
gdouble *max_x,
|
||||
gdouble *max_y);
|
||||
static void polygon_reset (Polygon *poly);
|
||||
static void cubism (GimpDrawable *drawable,
|
||||
GimpPreview *preview);
|
||||
static gboolean cubism_dialog (GimpDrawable *drawable);
|
||||
|
||||
static void fill_poly_color (Polygon *poly,
|
||||
GimpDrawable *drawable,
|
||||
GimpPreview *preview,
|
||||
guchar *col,
|
||||
guchar *dest);
|
||||
|
||||
static void convert_segment (gint x1,
|
||||
gint y1,
|
||||
gint x2,
|
||||
gint y2,
|
||||
gint offset,
|
||||
gint *min,
|
||||
gint *max);
|
||||
|
||||
static void randomize_indices (gint count,
|
||||
gint *indices);
|
||||
|
||||
static gdouble calc_alpha_blend (gdouble *vec,
|
||||
gdouble one_over_dist,
|
||||
gdouble x,
|
||||
gdouble y);
|
||||
|
||||
static void polygon_add_point (Polygon *poly,
|
||||
gdouble x,
|
||||
gdouble y);
|
||||
static void polygon_translate (Polygon *poly,
|
||||
gdouble tx,
|
||||
gdouble ty);
|
||||
static void polygon_rotate (Polygon *poly,
|
||||
gdouble theta);
|
||||
static gint polygon_extents (Polygon *poly,
|
||||
gdouble *min_x,
|
||||
gdouble *min_y,
|
||||
gdouble *max_x,
|
||||
gdouble *max_y);
|
||||
static void polygon_reset (Polygon *poly);
|
||||
|
||||
static gboolean cubism_dialog (void);
|
||||
|
||||
/*
|
||||
* Local variables
|
||||
|
@ -104,9 +113,10 @@ static gboolean cubism_dialog (void);
|
|||
|
||||
static CubismVals cvals =
|
||||
{
|
||||
10.0, /* tile_size */
|
||||
10.0, /* tile_size */
|
||||
2.5, /* tile_saturation */
|
||||
BLACK /* bg_color */
|
||||
BLACK, /* bg_color */
|
||||
TRUE /* preview */
|
||||
};
|
||||
|
||||
GimpPlugInInfo PLUG_IN_INFO =
|
||||
|
@ -138,16 +148,16 @@ query (void)
|
|||
};
|
||||
|
||||
gimp_install_procedure ("plug_in_cubism",
|
||||
"Convert the input drawable into a collection of rotated squares",
|
||||
"Help not yet written for this plug-in",
|
||||
"Spencer Kimball & Tracy Scott",
|
||||
"Spencer Kimball & Tracy Scott",
|
||||
"1996",
|
||||
N_("_Cubism..."),
|
||||
"RGB*, GRAY*",
|
||||
GIMP_PLUGIN,
|
||||
G_N_ELEMENTS (args), 0,
|
||||
args, NULL);
|
||||
"Convert the input drawable into a collection of rotated squares",
|
||||
"Help not yet written for this plug-in",
|
||||
"Spencer Kimball & Tracy Scott",
|
||||
"Spencer Kimball & Tracy Scott",
|
||||
"1996",
|
||||
N_("_Cubism..."),
|
||||
"RGB*, GRAY*",
|
||||
GIMP_PLUGIN,
|
||||
G_N_ELEMENTS (args), 0,
|
||||
args, NULL);
|
||||
|
||||
gimp_plugin_menu_register ("plug_in_cubism", "<Image>/Filters/Artistic");
|
||||
}
|
||||
|
@ -160,7 +170,7 @@ run (const gchar *name,
|
|||
GimpParam **return_vals)
|
||||
{
|
||||
static GimpParam values[1];
|
||||
GimpDrawable *active_drawable;
|
||||
GimpDrawable *drawable;
|
||||
GimpRunMode run_mode;
|
||||
GimpPDBStatusType status = GIMP_PDB_SUCCESS;
|
||||
|
||||
|
@ -174,6 +184,9 @@ run (const gchar *name,
|
|||
values[0].type = GIMP_PDB_STATUS;
|
||||
values[0].data.d_status = status;
|
||||
|
||||
/* get the active drawable */
|
||||
drawable = gimp_drawable_get (param[2].data.d_drawable);
|
||||
|
||||
switch (run_mode)
|
||||
{
|
||||
case GIMP_RUN_INTERACTIVE:
|
||||
|
@ -181,23 +194,23 @@ run (const gchar *name,
|
|||
gimp_get_data ("plug_in_cubism", &cvals);
|
||||
|
||||
/* First acquire information with a dialog */
|
||||
if (! cubism_dialog ())
|
||||
return;
|
||||
if (! cubism_dialog (drawable))
|
||||
return;
|
||||
break;
|
||||
|
||||
case GIMP_RUN_NONINTERACTIVE:
|
||||
/* Make sure all the arguments are there! */
|
||||
if (nparams != 6)
|
||||
status = GIMP_PDB_CALLING_ERROR;
|
||||
status = GIMP_PDB_CALLING_ERROR;
|
||||
if (status == GIMP_PDB_SUCCESS)
|
||||
{
|
||||
cvals.tile_size = param[3].data.d_float;
|
||||
cvals.tile_saturation = param[4].data.d_float;
|
||||
cvals.bg_color = param[5].data.d_int32;
|
||||
}
|
||||
{
|
||||
cvals.tile_size = param[3].data.d_float;
|
||||
cvals.tile_saturation = param[4].data.d_float;
|
||||
cvals.bg_color = param[5].data.d_int32;
|
||||
}
|
||||
if (status == GIMP_PDB_SUCCESS &&
|
||||
(cvals.bg_color < BLACK || cvals.bg_color > BG))
|
||||
status = GIMP_PDB_CALLING_ERROR;
|
||||
(cvals.bg_color < BLACK || cvals.bg_color > BG))
|
||||
status = GIMP_PDB_CALLING_ERROR;
|
||||
break;
|
||||
|
||||
case GIMP_RUN_WITH_LAST_VALS:
|
||||
|
@ -209,26 +222,23 @@ run (const gchar *name,
|
|||
break;
|
||||
}
|
||||
|
||||
/* get the active drawable */
|
||||
active_drawable = gimp_drawable_get (param[2].data.d_drawable);
|
||||
|
||||
/* Render the cubism effect */
|
||||
if ((status == GIMP_PDB_SUCCESS) &&
|
||||
(gimp_drawable_is_rgb (active_drawable->drawable_id) ||
|
||||
gimp_drawable_is_gray (active_drawable->drawable_id)))
|
||||
(gimp_drawable_is_rgb (drawable->drawable_id) ||
|
||||
gimp_drawable_is_gray (drawable->drawable_id)))
|
||||
{
|
||||
/* set cache size */
|
||||
gimp_tile_cache_ntiles (SQR (4 * cvals.tile_size * cvals.tile_saturation) / SQR (gimp_tile_width ()));
|
||||
|
||||
cubism (active_drawable);
|
||||
cubism (drawable, NULL);
|
||||
|
||||
/* If the run mode is interactive, flush the displays */
|
||||
if (run_mode != GIMP_RUN_NONINTERACTIVE)
|
||||
gimp_displays_flush ();
|
||||
gimp_displays_flush ();
|
||||
|
||||
/* Store mvals data */
|
||||
if (run_mode == GIMP_RUN_INTERACTIVE)
|
||||
gimp_set_data ("plug_in_cubism", &cvals, sizeof (CubismVals));
|
||||
gimp_set_data ("plug_in_cubism", &cvals, sizeof (CubismVals));
|
||||
}
|
||||
else if (status == GIMP_PDB_SUCCESS)
|
||||
{
|
||||
|
@ -238,14 +248,15 @@ run (const gchar *name,
|
|||
|
||||
values[0].data.d_status = status;
|
||||
|
||||
gimp_drawable_detach (active_drawable);
|
||||
gimp_drawable_detach (drawable);
|
||||
}
|
||||
|
||||
static gboolean
|
||||
cubism_dialog (void)
|
||||
cubism_dialog (GimpDrawable *drawable)
|
||||
{
|
||||
GtkWidget *dlg;
|
||||
GtkWidget *vbox;
|
||||
GtkWidget *dialog;
|
||||
GtkWidget *main_vbox;
|
||||
GtkWidget *preview;
|
||||
GtkWidget *toggle;
|
||||
GtkWidget *table;
|
||||
GtkObject *scale_data;
|
||||
|
@ -253,68 +264,85 @@ cubism_dialog (void)
|
|||
|
||||
gimp_ui_init ("cubism", FALSE);
|
||||
|
||||
dlg = gimp_dialog_new (_("Cubism"), "cubism",
|
||||
NULL, 0,
|
||||
gimp_standard_help_func, "plug-in-cubism",
|
||||
dialog = gimp_dialog_new (_("Cubism"), "cubism",
|
||||
NULL, 0,
|
||||
gimp_standard_help_func, "plug-in-cubism",
|
||||
|
||||
GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
|
||||
GTK_STOCK_OK, GTK_RESPONSE_OK,
|
||||
GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
|
||||
GTK_STOCK_OK, GTK_RESPONSE_OK,
|
||||
|
||||
NULL);
|
||||
NULL);
|
||||
|
||||
vbox = gtk_vbox_new (FALSE, 12);
|
||||
gtk_container_set_border_width (GTK_CONTAINER (vbox), 12);
|
||||
gtk_box_pack_start (GTK_BOX (GTK_DIALOG (dlg)->vbox), vbox, TRUE, TRUE, 0);
|
||||
gtk_widget_show (vbox);
|
||||
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, &cvals.preview);
|
||||
gtk_box_pack_start_defaults (GTK_BOX (main_vbox), preview);
|
||||
gtk_widget_show (preview);
|
||||
g_signal_connect_swapped (preview, "invalidated",
|
||||
G_CALLBACK (cubism),
|
||||
drawable);
|
||||
|
||||
table = gtk_table_new (2, 3, FALSE);
|
||||
gtk_table_set_col_spacings (GTK_TABLE (table), 6);
|
||||
gtk_table_set_row_spacings (GTK_TABLE (table), 6);
|
||||
gtk_table_set_row_spacing (GTK_TABLE (table), 0, 4);
|
||||
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);
|
||||
|
||||
scale_data = gimp_scale_entry_new (GTK_TABLE (table), 0, 0,
|
||||
_("_Tile size:"), SCALE_WIDTH, 5,
|
||||
cvals.tile_size, 0.0, 100.0, 1.0, 10.0, 1,
|
||||
TRUE, 0, 0,
|
||||
NULL, NULL);
|
||||
_("_Tile size:"), SCALE_WIDTH, 5,
|
||||
cvals.tile_size, 0.0, 100.0, 1.0, 10.0, 1,
|
||||
TRUE, 0, 0,
|
||||
NULL, NULL);
|
||||
g_signal_connect (scale_data, "value_changed",
|
||||
G_CALLBACK (gimp_double_adjustment_update),
|
||||
&cvals.tile_size);
|
||||
g_signal_connect_swapped (scale_data, "value_changed",
|
||||
G_CALLBACK (gimp_preview_invalidate),
|
||||
preview);
|
||||
|
||||
scale_data =
|
||||
gimp_scale_entry_new (GTK_TABLE (table), 0, 1,
|
||||
_("T_ile saturation:"), SCALE_WIDTH, 5,
|
||||
cvals.tile_saturation, 0.0, 10.0, 0.1, 1, 1,
|
||||
TRUE, 0, 0,
|
||||
NULL, NULL);
|
||||
_("T_ile saturation:"), SCALE_WIDTH, 5,
|
||||
cvals.tile_saturation, 0.0, 10.0, 0.1, 1, 1,
|
||||
TRUE, 0, 0,
|
||||
NULL, NULL);
|
||||
g_signal_connect (scale_data, "value_changed",
|
||||
G_CALLBACK (gimp_double_adjustment_update),
|
||||
&cvals.tile_saturation);
|
||||
g_signal_connect_swapped (scale_data, "value_changed",
|
||||
G_CALLBACK (gimp_preview_invalidate),
|
||||
preview);
|
||||
|
||||
toggle = gtk_check_button_new_with_mnemonic (_("_Use background color"));
|
||||
gtk_box_pack_start (GTK_BOX (vbox), toggle, FALSE, FALSE, 0);
|
||||
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),
|
||||
&cvals.bg_color);
|
||||
g_signal_connect_swapped (toggle, "toggled",
|
||||
G_CALLBACK (gimp_preview_invalidate),
|
||||
preview);
|
||||
|
||||
gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (toggle),
|
||||
(cvals.bg_color == BG));
|
||||
(cvals.bg_color == BG));
|
||||
|
||||
gtk_widget_show (dlg);
|
||||
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);
|
||||
|
||||
return run;
|
||||
}
|
||||
|
||||
static void
|
||||
cubism (GimpDrawable *drawable)
|
||||
cubism (GimpDrawable *drawable,
|
||||
GimpPreview *preview)
|
||||
{
|
||||
GimpPixelRgn src_rgn;
|
||||
guchar bg_col[4];
|
||||
|
@ -326,9 +354,10 @@ cubism (GimpDrawable *drawable)
|
|||
gint i, j, count;
|
||||
gint num_tiles;
|
||||
gint x1, y1, x2, y2;
|
||||
gint sel_width, sel_height;
|
||||
Polygon poly;
|
||||
guchar col[4];
|
||||
guchar *dest;
|
||||
guchar *dest = NULL;
|
||||
gint bytes;
|
||||
gboolean has_alpha;
|
||||
gint *random_indices;
|
||||
|
@ -338,7 +367,19 @@ cubism (GimpDrawable *drawable)
|
|||
gr = g_rand_new ();
|
||||
has_alpha = gimp_drawable_has_alpha (drawable->drawable_id);
|
||||
bytes = drawable->bpp;
|
||||
gimp_drawable_mask_bounds (drawable->drawable_id, &x1, &y1, &x2, &y2);
|
||||
|
||||
if (preview)
|
||||
{
|
||||
gimp_preview_get_position (preview, &x1, &y1);
|
||||
gimp_preview_get_size (preview, &sel_width, &sel_height);
|
||||
x2 = x1 + sel_width;
|
||||
y2 = y1 + sel_height;
|
||||
dest = g_new (guchar, sel_height * sel_width * bytes);
|
||||
}
|
||||
else
|
||||
{
|
||||
gimp_drawable_mask_bounds (drawable->drawable_id, &x1, &y1, &x2, &y2);
|
||||
}
|
||||
|
||||
/* determine the background color */
|
||||
if (cvals.bg_color == BLACK)
|
||||
|
@ -354,25 +395,34 @@ cubism (GimpDrawable *drawable)
|
|||
gimp_drawable_get_color_uchar (drawable->drawable_id, &color, bg_col);
|
||||
}
|
||||
|
||||
gimp_progress_init (_("Cubistic Transformation..."));
|
||||
|
||||
cols = ((x2 - x1) + cvals.tile_size - 1) / cvals.tile_size;
|
||||
rows = ((y2 - y1) + cvals.tile_size - 1) / cvals.tile_size;
|
||||
|
||||
/* Fill the image with the background color */
|
||||
gimp_pixel_rgn_init (&src_rgn, drawable,
|
||||
x1, y1, (x2 - x1), (y2 - y1), TRUE, TRUE);
|
||||
|
||||
for (pr = gimp_pixel_rgns_register (1, &src_rgn);
|
||||
pr != NULL;
|
||||
pr = gimp_pixel_rgns_process (pr))
|
||||
if (preview)
|
||||
{
|
||||
count = src_rgn.w * src_rgn.h;
|
||||
dest = src_rgn.data;
|
||||
for (i = 0; i < sel_width * sel_height ; i++)
|
||||
for (count = 0; count < bytes; count++)
|
||||
dest[i * bytes + count] = bg_col[count];
|
||||
}
|
||||
else
|
||||
{
|
||||
gimp_progress_init (_("Cubistic Transformation..."));
|
||||
gimp_pixel_rgn_init (&src_rgn, drawable,
|
||||
x1, y1, (x2 - x1), (y2 - y1), TRUE, TRUE);
|
||||
|
||||
while (count--)
|
||||
for (i = 0; i < bytes; i++)
|
||||
*dest++ = bg_col[i];
|
||||
for (pr = gimp_pixel_rgns_register (1, &src_rgn);
|
||||
pr != NULL;
|
||||
pr = gimp_pixel_rgns_process (pr))
|
||||
{
|
||||
count = src_rgn.w * src_rgn.h;
|
||||
dest = src_rgn.data;
|
||||
|
||||
while (count--)
|
||||
for (i = 0; i < bytes; i++)
|
||||
*dest++ = bg_col[i];
|
||||
}
|
||||
dest = NULL;
|
||||
}
|
||||
|
||||
num_tiles = (rows + 1) * (cols + 1);
|
||||
|
@ -384,22 +434,22 @@ cubism (GimpDrawable *drawable)
|
|||
|
||||
count = 0;
|
||||
gimp_pixel_rgn_init (&src_rgn, drawable,
|
||||
x1, y1, x2 - x1, y2 - y1, FALSE, FALSE);
|
||||
x1, y1, x2 - x1, y2 - y1, FALSE, FALSE);
|
||||
|
||||
while (count < num_tiles)
|
||||
{
|
||||
i = random_indices[count] / (cols + 1);
|
||||
j = random_indices[count] % (cols + 1);
|
||||
x = j * cvals.tile_size + (cvals.tile_size / 4.0)
|
||||
- g_rand_double_range (gr, 0, cvals.tile_size/2.0) + x1;
|
||||
- g_rand_double_range (gr, 0, cvals.tile_size/2.0) + x1;
|
||||
y = i * cvals.tile_size + (cvals.tile_size / 4.0)
|
||||
- g_rand_double_range (gr, 0, cvals.tile_size/2.0) + y1;
|
||||
- g_rand_double_range (gr, 0, cvals.tile_size/2.0) + y1;
|
||||
width = (cvals.tile_size +
|
||||
g_rand_double_range (gr, 0, cvals.tile_size / 4.0) -
|
||||
cvals.tile_size / 8.0) * cvals.tile_saturation;
|
||||
g_rand_double_range (gr, 0, cvals.tile_size / 4.0) -
|
||||
cvals.tile_size / 8.0) * cvals.tile_saturation;
|
||||
height = (cvals.tile_size +
|
||||
g_rand_double_range (gr, 0, cvals.tile_size / 4.0) -
|
||||
cvals.tile_size / 8.0) * cvals.tile_saturation;
|
||||
g_rand_double_range (gr, 0, cvals.tile_size / 4.0) -
|
||||
cvals.tile_size / 8.0) * cvals.tile_saturation;
|
||||
theta = g_rand_double_range (gr, 0, 2 * G_PI);
|
||||
polygon_reset (&poly);
|
||||
polygon_add_point (&poly, -width / 2.0, -height / 2.0);
|
||||
|
@ -416,28 +466,39 @@ cubism (GimpDrawable *drawable)
|
|||
gimp_pixel_rgn_get_pixel (&src_rgn, col, ix, iy);
|
||||
|
||||
if (!has_alpha || col[bytes - 1])
|
||||
fill_poly_color (&poly, drawable, col);
|
||||
fill_poly_color (&poly, drawable, preview, col, dest);
|
||||
|
||||
count++;
|
||||
if ((count % 5) == 0)
|
||||
gimp_progress_update ((double) count / (double) num_tiles);
|
||||
if (!preview)
|
||||
{
|
||||
if ((count % 5) == 0)
|
||||
gimp_progress_update ((double) count / (double) num_tiles);
|
||||
}
|
||||
}
|
||||
|
||||
gimp_progress_update (1.0);
|
||||
g_free (random_indices);
|
||||
g_rand_free (gr);
|
||||
|
||||
/* merge the shadow, update the drawable */
|
||||
gimp_drawable_flush (drawable);
|
||||
gimp_drawable_merge_shadow (drawable->drawable_id, TRUE);
|
||||
gimp_drawable_update (drawable->drawable_id, x1, y1, x2 - x1, y2 - y1);
|
||||
if (preview)
|
||||
{
|
||||
gimp_preview_draw_buffer (preview, dest, sel_width * bytes);
|
||||
g_free (dest);
|
||||
}
|
||||
else
|
||||
{
|
||||
gimp_progress_update (1.0);
|
||||
/* merge the shadow, update the drawable */
|
||||
gimp_drawable_flush (drawable);
|
||||
gimp_drawable_merge_shadow (drawable->drawable_id, TRUE);
|
||||
gimp_drawable_update (drawable->drawable_id, x1, y1, x2 - x1, y2 - y1);
|
||||
}
|
||||
}
|
||||
|
||||
static inline gdouble
|
||||
calc_alpha_blend (gdouble *vec,
|
||||
gdouble one_over_dist,
|
||||
gdouble x,
|
||||
gdouble y)
|
||||
gdouble one_over_dist,
|
||||
gdouble x,
|
||||
gdouble y)
|
||||
{
|
||||
gdouble r;
|
||||
|
||||
|
@ -447,40 +508,44 @@ calc_alpha_blend (gdouble *vec,
|
|||
{
|
||||
r = (vec[0] * x + vec[1] * y) * one_over_dist;
|
||||
if (r < 0.2)
|
||||
r = 0.2;
|
||||
r = 0.2;
|
||||
else if (r > 1.0)
|
||||
r = 1.0;
|
||||
r = 1.0;
|
||||
}
|
||||
return r;
|
||||
}
|
||||
|
||||
static void
|
||||
fill_poly_color (Polygon *poly,
|
||||
GimpDrawable *drawable,
|
||||
guchar *col)
|
||||
fill_poly_color (Polygon *poly,
|
||||
GimpDrawable *drawable,
|
||||
GimpPreview *preview,
|
||||
guchar *col,
|
||||
guchar *dest)
|
||||
{
|
||||
GimpPixelRgn src_rgn;
|
||||
gdouble dmin_x, dmin_y;
|
||||
gdouble dmax_x, dmax_y;
|
||||
gint xs, ys;
|
||||
gint xe, ye;
|
||||
gint min_x, min_y;
|
||||
gint max_x, max_y;
|
||||
gint size_x, size_y;
|
||||
gint * max_scanlines, *max_scanlines_iter;
|
||||
gint * min_scanlines, *min_scanlines_iter;
|
||||
gint val;
|
||||
gint alpha;
|
||||
gint bytes;
|
||||
guchar buf[4];
|
||||
gint i, j, x, y;
|
||||
gdouble sx, sy;
|
||||
gdouble ex, ey;
|
||||
gdouble xx, yy;
|
||||
gdouble vec[2];
|
||||
gdouble dist, one_over_dist;
|
||||
gint x1, y1, x2, y2;
|
||||
gint *vals, *vals_iter, *vals_end;
|
||||
GimpPixelRgn src_rgn;
|
||||
gdouble dmin_x, dmin_y;
|
||||
gdouble dmax_x, dmax_y;
|
||||
gint xs, ys;
|
||||
gint xe, ye;
|
||||
gint min_x, min_y;
|
||||
gint max_x, max_y;
|
||||
gint size_x, size_y;
|
||||
gint *max_scanlines, *max_scanlines_iter;
|
||||
gint *min_scanlines, *min_scanlines_iter;
|
||||
gint val;
|
||||
gint alpha;
|
||||
gint bytes;
|
||||
guchar buf[4];
|
||||
gint i, j, x, y;
|
||||
gdouble sx, sy;
|
||||
gdouble ex, ey;
|
||||
gdouble xx, yy;
|
||||
gdouble vec[2];
|
||||
gdouble dist, one_over_dist;
|
||||
gint x1, y1, x2, y2;
|
||||
gint sel_width, sel_height;
|
||||
gint *vals, *vals_iter, *vals_end;
|
||||
gint b;
|
||||
|
||||
sx = poly->pts[0].x;
|
||||
sy = poly->pts[0].y;
|
||||
|
@ -497,7 +562,20 @@ fill_poly_color (Polygon *poly,
|
|||
else
|
||||
one_over_dist = 0.0;
|
||||
|
||||
gimp_drawable_mask_bounds (drawable->drawable_id, &x1, &y1, &x2, &y2);
|
||||
if (preview)
|
||||
{
|
||||
gimp_preview_get_position (preview, &x1, &y1);
|
||||
gimp_preview_get_size (preview, &sel_width, &sel_height);
|
||||
x2 = x1 + sel_width;
|
||||
y2 = y1 + sel_height;
|
||||
}
|
||||
else
|
||||
{
|
||||
gimp_drawable_mask_bounds (drawable->drawable_id, &x1, &y1, &x2, &y2);
|
||||
gimp_pixel_rgn_init (&src_rgn, drawable,
|
||||
x1, y1, x2 - x1, y2 - y1,
|
||||
TRUE, TRUE);
|
||||
}
|
||||
bytes = drawable->bpp;
|
||||
|
||||
polygon_extents (poly, &dmin_x, &dmin_y, &dmax_x, &dmax_y);
|
||||
|
@ -517,109 +595,123 @@ fill_poly_color (Polygon *poly,
|
|||
max_scanlines[i] = min_x * SUPERSAMPLE;
|
||||
}
|
||||
|
||||
if(poly->npts) {
|
||||
gint poly_npts = poly->npts;
|
||||
GimpVector2 *curptr;
|
||||
if (poly->npts)
|
||||
{
|
||||
gint poly_npts = poly->npts;
|
||||
GimpVector2 *curptr;
|
||||
|
||||
xs = (gint) (poly->pts[poly_npts-1].x);
|
||||
ys = (gint) (poly->pts[poly_npts-1].y);
|
||||
xe = (gint) poly->pts[0].x;
|
||||
ye = (gint) poly->pts[0].y;
|
||||
xs = (gint) (poly->pts[poly_npts-1].x);
|
||||
ys = (gint) (poly->pts[poly_npts-1].y);
|
||||
xe = (gint) poly->pts[0].x;
|
||||
ye = (gint) poly->pts[0].y;
|
||||
|
||||
xs *= SUPERSAMPLE;
|
||||
ys *= SUPERSAMPLE;
|
||||
xe *= SUPERSAMPLE;
|
||||
ye *= SUPERSAMPLE;
|
||||
xs *= SUPERSAMPLE;
|
||||
ys *= SUPERSAMPLE;
|
||||
xe *= SUPERSAMPLE;
|
||||
ye *= SUPERSAMPLE;
|
||||
|
||||
convert_segment (xs, ys, xe, ye, min_y * SUPERSAMPLE,
|
||||
min_scanlines, max_scanlines);
|
||||
convert_segment (xs, ys, xe, ye, min_y * SUPERSAMPLE,
|
||||
min_scanlines, max_scanlines);
|
||||
|
||||
for (i = 1, curptr = &poly->pts[0]; i < poly_npts; i++)
|
||||
{
|
||||
xs = (gint) curptr->x;
|
||||
ys = (gint) curptr->y;
|
||||
curptr++;
|
||||
xe = (gint) curptr->x;
|
||||
ye = (gint) curptr->y;
|
||||
for (i = 1, curptr = &poly->pts[0]; i < poly_npts; i++)
|
||||
{
|
||||
xs = (gint) curptr->x;
|
||||
ys = (gint) curptr->y;
|
||||
curptr++;
|
||||
xe = (gint) curptr->x;
|
||||
ye = (gint) curptr->y;
|
||||
|
||||
xs *= SUPERSAMPLE;
|
||||
ys *= SUPERSAMPLE;
|
||||
xe *= SUPERSAMPLE;
|
||||
ye *= SUPERSAMPLE;
|
||||
xs *= SUPERSAMPLE;
|
||||
ys *= SUPERSAMPLE;
|
||||
xe *= SUPERSAMPLE;
|
||||
ye *= SUPERSAMPLE;
|
||||
|
||||
convert_segment (xs, ys, xe, ye, min_y * SUPERSAMPLE,
|
||||
min_scanlines, max_scanlines);
|
||||
}
|
||||
convert_segment (xs, ys, xe, ye, min_y * SUPERSAMPLE,
|
||||
min_scanlines, max_scanlines);
|
||||
}
|
||||
}
|
||||
|
||||
gimp_pixel_rgn_init (&src_rgn, drawable, 0, 0,
|
||||
drawable->width, drawable->height, TRUE, TRUE);
|
||||
|
||||
vals = g_new (gint, size_x);
|
||||
|
||||
for (i = 0; i < size_y; i++, min_scanlines_iter++, max_scanlines_iter++)
|
||||
{
|
||||
if (! (i % SUPERSAMPLE))
|
||||
{
|
||||
memset (vals, 0, sizeof (gint) * size_x);
|
||||
}
|
||||
{
|
||||
memset (vals, 0, sizeof (gint) * size_x);
|
||||
}
|
||||
|
||||
yy = (gdouble)i / (gdouble)SUPERSAMPLE + min_y;
|
||||
|
||||
for (j = *min_scanlines_iter; j < *max_scanlines_iter; j++)
|
||||
{
|
||||
x = j - min_x * SUPERSAMPLE;
|
||||
vals[x] += 255;
|
||||
}
|
||||
{
|
||||
x = j - min_x * SUPERSAMPLE;
|
||||
vals[x] += 255;
|
||||
}
|
||||
|
||||
if (! ((i + 1) % SUPERSAMPLE))
|
||||
{
|
||||
y = (i / SUPERSAMPLE) + min_y;
|
||||
{
|
||||
y = (i / SUPERSAMPLE) + min_y;
|
||||
|
||||
if (y >= y1 && y < y2)
|
||||
{
|
||||
for (j = 0; j < size_x; j += SUPERSAMPLE)
|
||||
{
|
||||
x = (j / SUPERSAMPLE) + min_x;
|
||||
if (y >= y1 && y < y2)
|
||||
{
|
||||
for (j = 0; j < size_x; j += SUPERSAMPLE)
|
||||
{
|
||||
x = (j / SUPERSAMPLE) + min_x;
|
||||
|
||||
if (x >= x1 && x < x2)
|
||||
{
|
||||
for (val = 0, vals_iter = &vals[j],
|
||||
vals_end = &vals_iter[SUPERSAMPLE];
|
||||
vals_iter < vals_end;
|
||||
vals_iter++)
|
||||
val += *vals_iter;
|
||||
if (x >= x1 && x < x2)
|
||||
{
|
||||
for (val = 0, vals_iter = &vals[j],
|
||||
vals_end = &vals_iter[SUPERSAMPLE];
|
||||
vals_iter < vals_end;
|
||||
vals_iter++)
|
||||
val += *vals_iter;
|
||||
|
||||
val /= SQR(SUPERSAMPLE);
|
||||
val /= SQR(SUPERSAMPLE);
|
||||
|
||||
if (val > 0)
|
||||
{
|
||||
xx = (gdouble) j / (gdouble) SUPERSAMPLE + min_x;
|
||||
alpha = (gint) (val * calc_alpha_blend (vec, one_over_dist, xx - sx, yy - sy));
|
||||
gimp_pixel_rgn_get_pixel (&src_rgn, buf, x, y);
|
||||
if (val > 0)
|
||||
{
|
||||
xx = (gdouble) j / (gdouble) SUPERSAMPLE + min_x;
|
||||
alpha = (gint) (val * calc_alpha_blend (vec, one_over_dist, xx - sx, yy - sy));
|
||||
if (preview)
|
||||
{
|
||||
for (b = 0; b < bytes; b++)
|
||||
buf[b] = dest[ ( (y-y1) * sel_width + (x-x1) ) * bytes + b];
|
||||
}
|
||||
else
|
||||
{
|
||||
gimp_pixel_rgn_get_pixel (&src_rgn, buf, x, y);
|
||||
}
|
||||
|
||||
#ifndef USE_READABLE_BUT_SLOW_CODE
|
||||
{
|
||||
guchar *buf_iter = buf,
|
||||
*col_iter = col,
|
||||
*buf_end = buf+bytes;
|
||||
{
|
||||
guchar *buf_iter = buf,
|
||||
*col_iter = col,
|
||||
*buf_end = buf+bytes;
|
||||
|
||||
for(; buf_iter < buf_end; buf_iter++, col_iter++)
|
||||
*buf_iter = ((guint)(*col_iter * alpha)
|
||||
+ (((guint)*buf_iter)
|
||||
* (256 - alpha))) >> 8;
|
||||
}
|
||||
for(; buf_iter < buf_end; buf_iter++, col_iter++)
|
||||
*buf_iter = ((guint)(*col_iter * alpha)
|
||||
+ (((guint)*buf_iter)
|
||||
* (256 - alpha))) >> 8;
|
||||
}
|
||||
#else /* original, pre-ECL code */
|
||||
for (b = 0; b < bytes; b++)
|
||||
buf[b] = ((col[b] * alpha) + (buf[b] * (255 - alpha))) / 255;
|
||||
for (b = 0; b < bytes; b++)
|
||||
buf[b] = ((col[b] * alpha) + (buf[b] * (255 - alpha))) / 255;
|
||||
|
||||
#endif
|
||||
gimp_pixel_rgn_set_pixel (&src_rgn, buf, x, y);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
if (preview)
|
||||
{
|
||||
for (b = 0; b < bytes; b++)
|
||||
dest[ ( (y-y1) * sel_width + (x-x1) ) * bytes + b] = buf[b];
|
||||
}
|
||||
else
|
||||
{
|
||||
gimp_pixel_rgn_set_pixel (&src_rgn, buf, x, y);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
g_free (vals);
|
||||
|
@ -629,12 +721,12 @@ fill_poly_color (Polygon *poly,
|
|||
|
||||
static void
|
||||
convert_segment (gint x1,
|
||||
gint y1,
|
||||
gint x2,
|
||||
gint y2,
|
||||
gint offset,
|
||||
gint *min,
|
||||
gint *max)
|
||||
gint y1,
|
||||
gint x2,
|
||||
gint y2,
|
||||
gint offset,
|
||||
gint *min,
|
||||
gint *max)
|
||||
{
|
||||
gint ydiff, y, tmp;
|
||||
gdouble xinc, xstart;
|
||||
|
@ -651,20 +743,20 @@ convert_segment (gint x1,
|
|||
xinc = (gdouble) (x2 - x1) / (gdouble) ydiff;
|
||||
xstart = x1 + 0.5 * xinc;
|
||||
for (y = y1 ; y < y2; y++)
|
||||
{
|
||||
if (xstart < min[y - offset])
|
||||
min[y-offset] = xstart;
|
||||
if (xstart > max[y - offset])
|
||||
max[y-offset] = xstart;
|
||||
{
|
||||
if (xstart < min[y - offset])
|
||||
min[y-offset] = xstart;
|
||||
if (xstart > max[y - offset])
|
||||
max[y-offset] = xstart;
|
||||
|
||||
xstart += xinc;
|
||||
}
|
||||
xstart += xinc;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static void
|
||||
randomize_indices (gint count,
|
||||
gint *indices)
|
||||
gint *indices)
|
||||
{
|
||||
gint i;
|
||||
gint index1, index2;
|
||||
|
@ -687,10 +779,10 @@ randomize_indices (gint count,
|
|||
|
||||
static void
|
||||
polygon_add_point (Polygon *poly,
|
||||
gdouble x,
|
||||
gdouble y)
|
||||
gdouble x,
|
||||
gdouble y)
|
||||
{
|
||||
if (poly->npts < 12)
|
||||
if (poly->npts < MAX_POINTS)
|
||||
{
|
||||
poly->pts[poly->npts].x = x;
|
||||
poly->pts[poly->npts].y = y;
|
||||
|
@ -702,7 +794,7 @@ polygon_add_point (Polygon *poly,
|
|||
|
||||
static void
|
||||
polygon_rotate (Polygon *poly,
|
||||
gdouble theta)
|
||||
gdouble theta)
|
||||
{
|
||||
gint i;
|
||||
gdouble ct, st;
|
||||
|
@ -722,8 +814,8 @@ polygon_rotate (Polygon *poly,
|
|||
|
||||
static void
|
||||
polygon_translate (Polygon *poly,
|
||||
gdouble tx,
|
||||
gdouble ty)
|
||||
gdouble tx,
|
||||
gdouble ty)
|
||||
{
|
||||
gint i;
|
||||
|
||||
|
@ -736,10 +828,10 @@ polygon_translate (Polygon *poly,
|
|||
|
||||
static gint
|
||||
polygon_extents (Polygon *poly,
|
||||
gdouble *x1,
|
||||
gdouble *y1,
|
||||
gdouble *x2,
|
||||
gdouble *y2)
|
||||
gdouble *x1,
|
||||
gdouble *y1,
|
||||
gdouble *x2,
|
||||
gdouble *y2)
|
||||
{
|
||||
gint i;
|
||||
|
||||
|
@ -752,13 +844,13 @@ polygon_extents (Polygon *poly,
|
|||
for (i = 1; i < poly->npts; i++)
|
||||
{
|
||||
if (poly->pts[i].x < *x1)
|
||||
*x1 = poly->pts[i].x;
|
||||
*x1 = poly->pts[i].x;
|
||||
if (poly->pts[i].x > *x2)
|
||||
*x2 = poly->pts[i].x;
|
||||
*x2 = poly->pts[i].x;
|
||||
if (poly->pts[i].y < *y1)
|
||||
*y1 = poly->pts[i].y;
|
||||
*y1 = poly->pts[i].y;
|
||||
if (poly->pts[i].y > *y2)
|
||||
*y2 = poly->pts[i].y;
|
||||
*y2 = poly->pts[i].y;
|
||||
}
|
||||
|
||||
return 1;
|
||||
|
|
Loading…
Reference in a new issue