1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583
|
/*
* Copyright (C) 1995 Spencer Kimball and Peter Mattis
*
* This is a plug-in for the GIMP.
*
* Plugin to convert a selection to a path.
*
* Copyright (C) 1999 Andy Thomas alt@gimp.org
*
* 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.
*
*/
/* Change log:-
* 0.1 First version.
*/
#include "config.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "libgimp/gimp.h"
#include "libgimp/gimpui.h"
#include "libgimpmath/gimpmath.h"
#
#include "global.h"
#include "types.h"
#include "pxl-outline.h"
#include "fit.h"
#include "spline.h"
#include "sel2path.h"
#include "libgimp/stdplugins-intl.h"
#define RESPONSE_RESET 1
#define MID_POINT 127
/***** Magic numbers *****/
/* Variables set in dialog box */
static void query (void);
static void run (const gchar *name,
gint nparams,
const GimpParam *param,
gint *nreturn_vals,
GimpParam **return_vals);
static gint sel2path_dialog (SELVALS *sels);
static void sel2path_response (GtkWidget *widget,
gint response_id,
gpointer data);
static void dialog_print_selVals (SELVALS *sels);
gboolean do_sel2path (gint32 image_ID);
GimpPlugInInfo PLUG_IN_INFO =
{
NULL, /* init_proc */
NULL, /* quit_proc */
query, /* query_proc */
run, /* run_proc */
};
static gint sel_x1, sel_y1, sel_x2, sel_y2;
static gint has_sel, sel_width, sel_height;
static SELVALS selVals;
GimpPixelRgn selection_rgn;
gboolean retVal = TRUE; /* Toggle if cancle button clicked */
MAIN ()
static void
query (void)
{
static GimpParamDef args[] =
{
{ GIMP_PDB_INT32, "run_mode", "Interactive, non-interactive" },
{ GIMP_PDB_IMAGE, "image", "Input image" },
{ GIMP_PDB_DRAWABLE, "drawable", "Input drawable (unused)" },
};
static GimpParamDef advanced_args[] =
{
{ GIMP_PDB_INT32, "run_mode", "Interactive, non-interactive" },
{ GIMP_PDB_IMAGE, "image", "Input image" },
{ GIMP_PDB_DRAWABLE, "drawable", "Input drawable (unused)" },
{ GIMP_PDB_FLOAT, "align_threshold", "align_threshold"},
{ GIMP_PDB_FLOAT, "corner_always_threshold", "corner_always_threshold"},
{ GIMP_PDB_INT8, "corner_surround", "corner_surround"},
{ GIMP_PDB_FLOAT, "corner_threshold", "corner_threshold"},
{ GIMP_PDB_FLOAT, "error_threshold", "error_threshold"},
{ GIMP_PDB_INT8, "filter_alternative_surround", "filter_alternative_surround"},
{ GIMP_PDB_FLOAT, "filter_epsilon", "filter_epsilon"},
{ GIMP_PDB_INT8, "filter_iteration_count", "filter_iteration_count"},
{ GIMP_PDB_FLOAT, "filter_percent", "filter_percent"},
{ GIMP_PDB_INT8, "filter_secondary_surround", "filter_secondary_surround"},
{ GIMP_PDB_INT8, "filter_surround", "filter_surround"},
{ GIMP_PDB_INT8, "keep_knees", "{1-Yes, 0-No}"},
{ GIMP_PDB_FLOAT, "line_reversion_threshold", "line_reversion_threshold"},
{ GIMP_PDB_FLOAT, "line_threshold", "line_threshold"},
{ GIMP_PDB_FLOAT, "reparameterize_improvement", "reparameterize_improvement"},
{ GIMP_PDB_FLOAT, "reparameterize_threshold", "reparameterize_threshold"},
{ GIMP_PDB_FLOAT, "subdivide_search", "subdivide_search"},
{ GIMP_PDB_INT8, "subdivide_surround", "subdivide_surround"},
{ GIMP_PDB_FLOAT, "subdivide_threshold", "subdivide_threshold"},
{ GIMP_PDB_INT8, "tangent_surround", "tangent_surround"},
};
gimp_install_procedure ("plug_in_sel2path",
"Converts a selection to a path",
"Converts a selection to a path",
"Andy Thomas",
"Andy Thomas",
"1999",
NULL,
"RGB*, INDEXED*, GRAY*",
GIMP_PLUGIN,
G_N_ELEMENTS (args), 0,
args, NULL);
gimp_install_procedure ("plug_in_sel2path_advanced",
"Converts a selection to a path (with advanced user menu)",
"Converts a selection to a path (with advanced user menu)",
"Andy Thomas",
"Andy Thomas",
"1999",
NULL,
"RGB*, INDEXED*, GRAY*",
GIMP_PLUGIN,
G_N_ELEMENTS (advanced_args), 0,
advanced_args, NULL);
}
static void
run (const gchar *name,
gint nparams,
const GimpParam *param,
gint *nreturn_vals,
GimpParam **return_vals)
{
static GimpParam values[1];
gint32 image_ID;
GimpRunMode run_mode;
GimpPDBStatusType status = GIMP_PDB_SUCCESS;
gboolean no_dialog;
run_mode = param[0].data.d_int32;
INIT_I18N ();
no_dialog = (strcmp (name, "plug_in_sel2path") == 0);
*nreturn_vals = 1;
*return_vals = values;
values[0].type = GIMP_PDB_STATUS;
values[0].data.d_status = status;
image_ID = param[1].data.d_image;
if (image_ID < 0)
{
g_warning ("plug_in_sel2path needs a valid image ID");
return;
}
if (gimp_selection_is_empty (image_ID))
{
g_message (_("No selection to convert"));
return;
}
fit_set_default_params (&selVals);
if (!no_dialog)
{
switch (run_mode)
{
case GIMP_RUN_INTERACTIVE:
if (gimp_get_data_size ("plug_in_sel2path_advanced") > 0)
{
gimp_get_data ("plug_in_sel2path_advanced", &selVals);
}
if (!sel2path_dialog (&selVals))
return;
/* Get the current settings */
fit_set_params (&selVals);
break;
case GIMP_RUN_NONINTERACTIVE:
if (nparams != 23)
status = GIMP_PDB_CALLING_ERROR;
if (status == GIMP_PDB_SUCCESS)
{
selVals.align_threshold = param[3].data.d_float;
selVals.corner_always_threshold = param[4].data.d_float;
selVals.corner_surround = param[5].data.d_int8;
selVals.corner_threshold = param[6].data.d_float;
selVals.error_threshold = param[7].data.d_float;
selVals.filter_alternative_surround = param[8].data.d_int8;
selVals.filter_epsilon = param[9].data.d_float;
selVals.filter_iteration_count = param[10].data.d_int8;
selVals.filter_percent = param[11].data.d_float;
selVals.filter_secondary_surround = param[12].data.d_int8;
selVals.filter_surround = param[13].data.d_int8;
selVals.keep_knees = param[14].data.d_int8;
selVals.line_reversion_threshold = param[15].data.d_float;
selVals.line_threshold = param[16].data.d_float;
selVals.reparameterize_improvement = param[17].data.d_float;
selVals.reparameterize_threshold = param[18].data.d_float;
selVals.subdivide_search = param[19].data.d_float;
selVals.subdivide_surround = param[20].data.d_int8;
selVals.subdivide_threshold = param[21].data.d_float;
selVals.tangent_surround = param[22].data.d_int8;
fit_set_params (&selVals);
}
break;
case GIMP_RUN_WITH_LAST_VALS:
if(gimp_get_data_size ("plug_in_sel2path_advanced") > 0)
{
gimp_get_data ("plug_in_sel2path_advanced", &selVals);
/* Set up the last values */
fit_set_params (&selVals);
}
break;
default:
break;
}
}
do_sel2path (image_ID);
values[0].data.d_status = status;
if (status == GIMP_PDB_SUCCESS)
{
dialog_print_selVals(&selVals);
if (run_mode == GIMP_RUN_INTERACTIVE && !no_dialog)
gimp_set_data ("plug_in_sel2path_advanced", &selVals, sizeof(SELVALS));
}
}
static void
dialog_print_selVals (SELVALS *sels)
{
#if 0
printf ("selVals.align_threshold %g\n", selVals.align_threshold);
printf ("selVals.corner_always_threshol %g\n", selVals.corner_always_threshold);
printf ("selVals.corner_surround %g\n", selVals.corner_surround);
printf ("selVals.corner_threshold %g\n", selVals.corner_threshold);
printf ("selVals.error_threshold %g\n", selVals.error_threshold);
printf ("selVals.filter_alternative_surround %g\n", selVals.filter_alternative_surround);
printf ("selVals.filter_epsilon %g\n", selVals.filter_epsilon);
printf ("selVals.filter_iteration_count %g\n", selVals.filter_iteration_count);
printf ("selVals.filter_percent %g\n", selVals.filter_percent);
printf ("selVals.filter_secondary_surround %g\n", selVals.filter_secondary_surround);
printf ("selVals.filter_surround %g\n", selVals.filter_surround);
printf ("selVals.keep_knees %d\n", selVals.keep_knees);
printf ("selVals.line_reversion_threshold %g\n", selVals.line_reversion_threshold);
printf ("selVals.line_threshold %g\n", selVals.line_threshold);
printf ("selVals.reparameterize_improvement %g\n", selVals.reparameterize_improvement);
printf ("selVals.reparameterize_threshold %g\n", selVals.reparameterize_threshold);
printf ("selVals.subdivide_search %g\n" selVals.subdivide_search);
printf ("selVals.subdivide_surround %g\n", selVals.subdivide_surround);
printf ("selVals.subdivide_threshold %g\n", selVals.subdivide_threshold);
printf ("selVals.tangent_surround %g\n", selVals.tangent_surround);
#endif /* 0 */
}
/* Build the dialog up. This was the hard part! */
static gint
sel2path_dialog (SELVALS *sels)
{
GtkWidget *dlg;
GtkWidget *table;
retVal = FALSE;
gimp_ui_init ("sel2path", FALSE);
dlg = gimp_dialog_new (_("Selection To Path Advanced Settings"), "sel2path",
NULL, 0,
gimp_standard_help_func, "plug-in-sel2path-advanced",
GIMP_STOCK_RESET, RESPONSE_RESET,
GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
GTK_STOCK_OK, GTK_RESPONSE_OK,
NULL);
g_signal_connect (dlg, "response",
G_CALLBACK (sel2path_response),
NULL);
g_signal_connect (dlg, "destroy",
G_CALLBACK (gtk_main_quit),
NULL);
table = dialog_create_selection_area (sels);
gtk_container_set_border_width (GTK_CONTAINER (table), 12);
gtk_container_add (GTK_CONTAINER (GTK_DIALOG (dlg)->vbox), table);
gtk_widget_show (table);
gtk_widget_show (dlg);
gtk_main ();
return retVal;
}
static void
sel2path_response (GtkWidget *widget,
gint response_id,
gpointer data)
{
switch (response_id)
{
case RESPONSE_RESET:
reset_adv_dialog ();
fit_set_params (&selVals);
break;
case GTK_RESPONSE_OK:
retVal = TRUE;
default:
gtk_widget_destroy (widget);
break;
}
}
guchar
sel_pixel_value (gint row,
gint col)
{
guchar ret;
if (col > sel_width || row > sel_height)
{
g_warning ("sel_pixel_value [%d,%d] out of bounds", col, row);
return 0;
}
gimp_pixel_rgn_get_pixel(&selection_rgn,&ret,col+sel_x1,row+sel_y1);
return ret;
}
gboolean
sel_pixel_is_white (gint row,
gint col)
{
if (sel_pixel_value (row, col) < MID_POINT)
return TRUE;
else
return FALSE;
}
gint
sel_get_width (void)
{
return sel_width;
}
gint
sel_get_height (void)
{
return sel_height;
}
gint
sel_valid_pixel (gint row,
gint col)
{
return (0 <= (row) && (row) < sel_get_height ()
&& 0 <= (col) && (col) < sel_get_width ());
}
void
gen_anchor (gdouble *p,
gdouble x,
gdouble y,
gboolean is_newcurve)
{
/* printf("TYPE: %s X: %d Y: %d\n", */
/* (is_newcurve)?"3":"1", */
/* sel_x1+(int)RINT(x), */
/* sel_y1 + sel_height - (int)RINT(y)+1); */
*p++ = (sel_x1 + (gint)RINT(x));
*p++ = sel_y1 + sel_height - (gint)RINT(y) + 1;
*p++ = (is_newcurve) ? 3.0 : 1.0;
}
void
gen_control (gdouble *p,
gdouble x,
gdouble y)
{
/* printf("TYPE: 2 X: %d Y: %d\n", */
/* sel_x1+(int)RINT(x), */
/* sel_y1 + sel_height - (int)RINT(y)+1); */
*p++ = sel_x1 + (gint)RINT(x);
*p++ = sel_y1 + sel_height - (gint)RINT(y) + 1;
*p++ = 2.0;
}
void
do_points (spline_list_array_type in_splines,
gint32 image_ID)
{
unsigned this_list;
gint seg_count = 0;
gint point_count = 0;
gdouble last_x,last_y;
gdouble *parray;
gdouble *cur_parray;
gint path_point_count;
for (this_list = 0; this_list < SPLINE_LIST_ARRAY_LENGTH (in_splines);
this_list++)
{
spline_list_type in_list = SPLINE_LIST_ARRAY_ELT (in_splines, this_list);
/* Ignore single points that are on their own */
if(SPLINE_LIST_LENGTH (in_list) < 2)
continue;
point_count += SPLINE_LIST_LENGTH (in_list);
}
/* printf("Name SEL2PATH\n"); */
/* printf("#POINTS: %d\n",point_count*3); */
/* printf("CLOSED: 1\n"); */
/* printf("DRAW: 0\n"); */
/* printf("STATE: 4\n"); */
path_point_count = point_count*9;
cur_parray = (gdouble *)g_new0(gdouble ,point_count*9);
parray = cur_parray;
point_count = 0;
for (this_list = 0; this_list < SPLINE_LIST_ARRAY_LENGTH (in_splines);
this_list++)
{
unsigned this_spline;
spline_list_type in_list = SPLINE_LIST_ARRAY_ELT (in_splines, this_list);
/* if(seg_count > 0 && point_count > 0) */
/* gen_anchor(last_x,last_y,0); */
point_count = 0;
/* Ignore single points that are on their own */
if(SPLINE_LIST_LENGTH (in_list) < 2)
continue;
for (this_spline = 0; this_spline < SPLINE_LIST_LENGTH (in_list);
this_spline++)
{
spline_type s = SPLINE_LIST_ELT (in_list, this_spline);
if (SPLINE_DEGREE (s) == LINEAR)
{
gen_anchor (cur_parray,
START_POINT (s).x, START_POINT (s).y,
seg_count && !point_count);
cur_parray += 3;
gen_control (cur_parray, START_POINT (s).x, START_POINT (s).y);
cur_parray += 3;
gen_control (cur_parray,END_POINT (s).x, END_POINT (s).y);
cur_parray += 3;
last_x = END_POINT (s).x;
last_y = END_POINT (s).y;
}
else if (SPLINE_DEGREE (s) == CUBIC)
{
gen_anchor (cur_parray,
START_POINT (s).x, START_POINT (s).y,
seg_count && !point_count);
cur_parray += 3;
gen_control (cur_parray,CONTROL1 (s).x, CONTROL1 (s).y);
cur_parray += 3;
gen_control (cur_parray,CONTROL2 (s).x, CONTROL2 (s).y);
cur_parray += 3;
last_x = END_POINT (s).x;
last_y = END_POINT (s).y;
}
else
g_warning ("print_spline: strange degree (%d)", SPLINE_DEGREE (s));
point_count++;
}
seg_count++;
}
gimp_path_set_points (image_ID,
"selection_to_path",
1,
path_point_count,
parray);
}
gboolean
do_sel2path (gint32 image_ID)
{
gint32 selection_ID;
GimpDrawable *sel_drawable;
pixel_outline_list_type olt;
spline_list_array_type splines;
gimp_selection_bounds (image_ID, &has_sel,
&sel_x1, &sel_y1, &sel_x2, &sel_y2);
sel_width = sel_x2 - sel_x1;
sel_height = sel_y2 - sel_y1;
/* Now get the selection channel */
selection_ID = gimp_image_get_selection (image_ID);
if (selection_ID < 0)
return FALSE;
sel_drawable = gimp_drawable_get (selection_ID);
if (gimp_drawable_bpp (selection_ID) != 1)
{
g_warning ("Internal error. Selection bpp > 1");
return FALSE;
}
gimp_pixel_rgn_init (&selection_rgn, sel_drawable,
sel_x1, sel_y1, sel_width, sel_height,
FALSE, FALSE);
gimp_tile_cache_ntiles (2 * (sel_drawable->width + gimp_tile_width () - 1) /
gimp_tile_width ());
olt = find_outline_pixels ();
splines = fitted_splines (olt);
do_points (splines, image_ID);
gimp_drawable_detach (sel_drawable);
gimp_displays_flush ();
return TRUE;
}
void
safe_free (address *item)
{
g_return_if_fail (item != NULL);
g_free (*item);
*item = NULL;
}
|