1 /*
2 * Copyright (C) 1995 Spencer Kimball and Peter Mattis
3 *
4 * This is a plug-in for GIMP.
5 *
6 * Plugin to convert a selection to a path.
7 *
8 * Copyright (C) 1999 Andy Thomas alt@gimp.org
9 *
10 * This program is free software: you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 3 of the License, or
13 * (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with this program. If not, see <https://www.gnu.org/licenses/>.
22 *
23 */
24
25 /* Change log:-
26 * 0.1 First version.
27 */
28
29 #include "config.h"
30
31 #include <string.h>
32
33 #include "libgimp/gimp.h"
34 #include "libgimp/gimpui.h"
35
36 #include "libgimpmath/gimpmath.h"
37
38 #include "global.h"
39 #include "types.h"
40 #include "pxl-outline.h"
41 #include "fit.h"
42 #include "spline.h"
43 #include "selection-to-path.h"
44
45 #include "libgimp/stdplugins-intl.h"
46
47
48 #define PLUG_IN_BINARY "selection-to-path"
49 #define PLUG_IN_ROLE "gimp-selection-to-path"
50
51 #define RESPONSE_RESET 1
52 #define MID_POINT 127
53
54 /***** Magic numbers *****/
55
56 /* Variables set in dialog box */
57
58 static void query (void);
59 static void run (const gchar *name,
60 gint nparams,
61 const GimpParam *param,
62 gint *nreturn_vals,
63 GimpParam **return_vals);
64
65 static gint sel2path_dialog (SELVALS *sels);
66 static void sel2path_response (GtkWidget *widget,
67 gint response_id,
68 gpointer data);
69 static void dialog_print_selVals (SELVALS *sels);
70 static gboolean sel2path (gint32 image_ID);
71
72
73 const GimpPlugInInfo PLUG_IN_INFO =
74 {
75 NULL, /* init_proc */
76 NULL, /* quit_proc */
77 query, /* query_proc */
78 run, /* run_proc */
79 };
80
81 static gint sel_x1, sel_y1, sel_x2, sel_y2;
82 static gint has_sel, sel_width, sel_height;
83 static SELVALS selVals;
84 static GeglSampler *sel_sampler;
85 static gboolean retVal = TRUE; /* Toggle if cancel button clicked */
86
MAIN()87 MAIN ()
88
89 static void
90 query (void)
91 {
92 static const GimpParamDef args[] =
93 {
94 { GIMP_PDB_INT32, "run-mode", "The run mode { RUN-INTERACTIVE (0), RUN-NONINTERACTIVE (1) }" },
95 { GIMP_PDB_IMAGE, "image", "Input image" },
96 { GIMP_PDB_DRAWABLE, "drawable", "Input drawable (unused)" },
97 };
98
99 static const GimpParamDef advanced_args[] =
100 {
101 { GIMP_PDB_INT32, "run-mode", "The run mode { RUN-INTERACTIVE (0), RUN-NONINTERACTIVE (1) }" },
102 { GIMP_PDB_IMAGE, "image", "Input image" },
103 { GIMP_PDB_DRAWABLE, "drawable", "Input drawable (unused)" },
104 { GIMP_PDB_FLOAT, "align-threshold", "align_threshold"},
105 { GIMP_PDB_FLOAT, "corner-always-threshold", "corner_always_threshold"},
106 { GIMP_PDB_INT8, "corner-surround", "corner_surround"},
107 { GIMP_PDB_FLOAT, "corner-threshold", "corner_threshold"},
108 { GIMP_PDB_FLOAT, "error-threshold", "error_threshold"},
109 { GIMP_PDB_INT8, "filter-alternative-surround", "filter_alternative_surround"},
110 { GIMP_PDB_FLOAT, "filter-epsilon", "filter_epsilon"},
111 { GIMP_PDB_INT8, "filter-iteration-count", "filter_iteration_count"},
112 { GIMP_PDB_FLOAT, "filter-percent", "filter_percent"},
113 { GIMP_PDB_INT8, "filter-secondary-surround", "filter_secondary_surround"},
114 { GIMP_PDB_INT8, "filter-surround", "filter_surround"},
115 { GIMP_PDB_INT8, "keep-knees", "{1-Yes, 0-No}"},
116 { GIMP_PDB_FLOAT, "line-reversion-threshold", "line_reversion_threshold"},
117 { GIMP_PDB_FLOAT, "line-threshold", "line_threshold"},
118 { GIMP_PDB_FLOAT, "reparameterize-improvement", "reparameterize_improvement"},
119 { GIMP_PDB_FLOAT, "reparameterize-threshold", "reparameterize_threshold"},
120 { GIMP_PDB_FLOAT, "subdivide-search", "subdivide_search"},
121 { GIMP_PDB_INT8, "subdivide-surround", "subdivide_surround"},
122 { GIMP_PDB_FLOAT, "subdivide-threshold", "subdivide_threshold"},
123 { GIMP_PDB_INT8, "tangent-surround", "tangent_surround"},
124 };
125
126 gimp_install_procedure ("plug-in-sel2path",
127 "Converts a selection to a path",
128 "Converts a selection to a path",
129 "Andy Thomas",
130 "Andy Thomas",
131 "1999",
132 NULL,
133 "RGB*, INDEXED*, GRAY*",
134 GIMP_PLUGIN,
135 G_N_ELEMENTS (args), 0,
136 args, NULL);
137
138 gimp_install_procedure ("plug-in-sel2path-advanced",
139 "Converts a selection to a path (with advanced user menu)",
140 "Converts a selection to a path (with advanced user menu)",
141 "Andy Thomas",
142 "Andy Thomas",
143 "1999",
144 NULL,
145 "RGB*, INDEXED*, GRAY*",
146 GIMP_PLUGIN,
147 G_N_ELEMENTS (advanced_args), 0,
148 advanced_args, NULL);
149 }
150
151 static void
run(const gchar * name,gint nparams,const GimpParam * param,gint * nreturn_vals,GimpParam ** return_vals)152 run (const gchar *name,
153 gint nparams,
154 const GimpParam *param,
155 gint *nreturn_vals,
156 GimpParam **return_vals)
157 {
158 static GimpParam values[1];
159 gint32 image_ID;
160 GimpRunMode run_mode;
161 GimpPDBStatusType status = GIMP_PDB_SUCCESS;
162 gboolean no_dialog;
163
164 INIT_I18N ();
165 gegl_init (NULL, NULL);
166
167 run_mode = param[0].data.d_int32;
168
169 no_dialog = (strcmp (name, "plug-in-sel2path") == 0);
170
171 *nreturn_vals = 1;
172 *return_vals = values;
173
174 values[0].type = GIMP_PDB_STATUS;
175 values[0].data.d_status = status;
176
177 image_ID = param[1].data.d_image;
178 if (image_ID < 0)
179 {
180 g_warning ("plug-in-sel2path needs a valid image ID");
181 return;
182 }
183
184 if (gimp_selection_is_empty (image_ID))
185 {
186 g_message (_("No selection to convert"));
187 return;
188 }
189
190 fit_set_default_params (&selVals);
191
192 if (!no_dialog)
193 {
194 switch (run_mode)
195 {
196 case GIMP_RUN_INTERACTIVE:
197 if (gimp_get_data_size ("plug-in-sel2path-advanced") > 0)
198 {
199 gimp_get_data ("plug-in-sel2path-advanced", &selVals);
200 }
201
202 if (!sel2path_dialog (&selVals))
203 return;
204
205 /* Get the current settings */
206 fit_set_params (&selVals);
207 break;
208
209 case GIMP_RUN_NONINTERACTIVE:
210 if (nparams != 23)
211 status = GIMP_PDB_CALLING_ERROR;
212
213 if (status == GIMP_PDB_SUCCESS)
214 {
215 selVals.align_threshold = param[3].data.d_float;
216 selVals.corner_always_threshold = param[4].data.d_float;
217 selVals.corner_surround = param[5].data.d_int8;
218 selVals.corner_threshold = param[6].data.d_float;
219 selVals.error_threshold = param[7].data.d_float;
220 selVals.filter_alternative_surround = param[8].data.d_int8;
221 selVals.filter_epsilon = param[9].data.d_float;
222 selVals.filter_iteration_count = param[10].data.d_int8;
223 selVals.filter_percent = param[11].data.d_float;
224 selVals.filter_secondary_surround = param[12].data.d_int8;
225 selVals.filter_surround = param[13].data.d_int8;
226 selVals.keep_knees = param[14].data.d_int8;
227 selVals.line_reversion_threshold = param[15].data.d_float;
228 selVals.line_threshold = param[16].data.d_float;
229 selVals.reparameterize_improvement = param[17].data.d_float;
230 selVals.reparameterize_threshold = param[18].data.d_float;
231 selVals.subdivide_search = param[19].data.d_float;
232 selVals.subdivide_surround = param[20].data.d_int8;
233 selVals.subdivide_threshold = param[21].data.d_float;
234 selVals.tangent_surround = param[22].data.d_int8;
235 fit_set_params (&selVals);
236 }
237 break;
238
239 case GIMP_RUN_WITH_LAST_VALS:
240 if(gimp_get_data_size ("plug-in-sel2path-advanced") > 0)
241 {
242 gimp_get_data ("plug-in-sel2path-advanced", &selVals);
243
244 /* Set up the last values */
245 fit_set_params (&selVals);
246 }
247 break;
248
249 default:
250 break;
251 }
252 }
253
254 sel2path (image_ID);
255 values[0].data.d_status = status;
256
257 if (status == GIMP_PDB_SUCCESS)
258 {
259 dialog_print_selVals(&selVals);
260 if (run_mode == GIMP_RUN_INTERACTIVE && !no_dialog)
261 gimp_set_data ("plug-in-sel2path-advanced", &selVals, sizeof(SELVALS));
262 }
263 }
264
265 static void
dialog_print_selVals(SELVALS * sels)266 dialog_print_selVals (SELVALS *sels)
267 {
268 #if 0
269 printf ("selVals.align_threshold %g\n", selVals.align_threshold);
270 printf ("selVals.corner_always_threshol %g\n", selVals.corner_always_threshold);
271 printf ("selVals.corner_surround %g\n", selVals.corner_surround);
272 printf ("selVals.corner_threshold %g\n", selVals.corner_threshold);
273 printf ("selVals.error_threshold %g\n", selVals.error_threshold);
274 printf ("selVals.filter_alternative_surround %g\n", selVals.filter_alternative_surround);
275 printf ("selVals.filter_epsilon %g\n", selVals.filter_epsilon);
276 printf ("selVals.filter_iteration_count %g\n", selVals.filter_iteration_count);
277 printf ("selVals.filter_percent %g\n", selVals.filter_percent);
278 printf ("selVals.filter_secondary_surround %g\n", selVals.filter_secondary_surround);
279 printf ("selVals.filter_surround %g\n", selVals.filter_surround);
280 printf ("selVals.keep_knees %d\n", selVals.keep_knees);
281 printf ("selVals.line_reversion_threshold %g\n", selVals.line_reversion_threshold);
282 printf ("selVals.line_threshold %g\n", selVals.line_threshold);
283 printf ("selVals.reparameterize_improvement %g\n", selVals.reparameterize_improvement);
284 printf ("selVals.reparameterize_threshold %g\n", selVals.reparameterize_threshold);
285 printf ("selVals.subdivide_search %g\n" selVals.subdivide_search);
286 printf ("selVals.subdivide_surround %g\n", selVals.subdivide_surround);
287 printf ("selVals.subdivide_threshold %g\n", selVals.subdivide_threshold);
288 printf ("selVals.tangent_surround %g\n", selVals.tangent_surround);
289 #endif /* 0 */
290 }
291
292 /* Build the dialog up. This was the hard part! */
293 static gint
sel2path_dialog(SELVALS * sels)294 sel2path_dialog (SELVALS *sels)
295 {
296 GtkWidget *dlg;
297 GtkWidget *table;
298
299 retVal = FALSE;
300
301 gimp_ui_init (PLUG_IN_BINARY, FALSE);
302
303 dlg = gimp_dialog_new (_("Selection to Path Advanced Settings"),
304 PLUG_IN_ROLE,
305 NULL, 0,
306 gimp_standard_help_func, "plug-in-sel2path-advanced",
307
308 _("_Reset"), RESPONSE_RESET,
309 _("_Cancel"), GTK_RESPONSE_CANCEL,
310 _("_OK"), GTK_RESPONSE_OK,
311
312 NULL);
313
314 gtk_dialog_set_alternative_button_order (GTK_DIALOG (dlg),
315 RESPONSE_RESET,
316 GTK_RESPONSE_OK,
317 GTK_RESPONSE_CANCEL,
318 -1);
319
320 gimp_window_set_transient (GTK_WINDOW (dlg));
321
322 g_signal_connect (dlg, "response",
323 G_CALLBACK (sel2path_response),
324 NULL);
325 g_signal_connect (dlg, "destroy",
326 G_CALLBACK (gtk_main_quit),
327 NULL);
328
329 table = dialog_create_selection_area (sels);
330 gtk_container_set_border_width (GTK_CONTAINER (table), 12);
331 gtk_box_pack_start (GTK_BOX (gtk_dialog_get_content_area (GTK_DIALOG (dlg))),
332 table, TRUE, TRUE, 0);
333 gtk_widget_show (table);
334
335 gtk_widget_show (dlg);
336
337 gtk_main ();
338
339 return retVal;
340 }
341
342 static void
sel2path_response(GtkWidget * widget,gint response_id,gpointer data)343 sel2path_response (GtkWidget *widget,
344 gint response_id,
345 gpointer data)
346 {
347 switch (response_id)
348 {
349 case RESPONSE_RESET:
350 reset_adv_dialog ();
351 fit_set_params (&selVals);
352 break;
353
354 case GTK_RESPONSE_OK:
355 retVal = TRUE;
356
357 default:
358 gtk_widget_destroy (widget);
359 break;
360 }
361 }
362
363 guchar
sel_pixel_value(gint row,gint col)364 sel_pixel_value (gint row,
365 gint col)
366 {
367 guchar ret;
368
369 if (col > sel_width || row > sel_height)
370 {
371 g_warning ("sel_pixel_value [%d,%d] out of bounds", col, row);
372 return 0;
373 }
374
375 gegl_sampler_get (sel_sampler,
376 col + sel_x1, row + sel_y1, NULL, &ret, GEGL_ABYSS_NONE);
377
378 return ret;
379 }
380
381 gboolean
sel_pixel_is_white(gint row,gint col)382 sel_pixel_is_white (gint row,
383 gint col)
384 {
385 if (sel_pixel_value (row, col) < MID_POINT)
386 return TRUE;
387 else
388 return FALSE;
389 }
390
391 gint
sel_get_width(void)392 sel_get_width (void)
393 {
394 return sel_width;
395 }
396
397 gint
sel_get_height(void)398 sel_get_height (void)
399 {
400 return sel_height;
401 }
402
403 gboolean
sel_valid_pixel(gint row,gint col)404 sel_valid_pixel (gint row,
405 gint col)
406 {
407 return (0 <= (row) && (row) < sel_get_height ()
408 && 0 <= (col) && (col) < sel_get_width ());
409 }
410
411
412 static void
do_points(spline_list_array_type in_splines,gint32 image_ID)413 do_points (spline_list_array_type in_splines,
414 gint32 image_ID)
415 {
416 gint32 vectors;
417 gint32 stroke;
418 gint i, j;
419 gboolean have_points = FALSE;
420 spline_list_type spline_list;
421
422 /* check if there really is something to do... */
423 for (i = 0; i < SPLINE_LIST_ARRAY_LENGTH (in_splines); i++)
424 {
425 spline_list = SPLINE_LIST_ARRAY_ELT (in_splines, i);
426 /* Ignore single points that are on their own */
427 if (SPLINE_LIST_LENGTH (spline_list) < 2)
428 continue;
429 have_points = TRUE;
430 break;
431 }
432
433 if (!have_points)
434 return;
435
436 vectors = gimp_vectors_new (image_ID, _("Selection"));
437
438 for (j = 0; j < SPLINE_LIST_ARRAY_LENGTH (in_splines); j++)
439 {
440 spline_type seg;
441
442 spline_list = SPLINE_LIST_ARRAY_ELT (in_splines, j);
443
444 /* Ignore single points that are on their own */
445 if (SPLINE_LIST_LENGTH (spline_list) < 2)
446 continue;
447
448 /*
449 * we're constructing the path backwards
450 * to have the result of least surprise for "Text along Path".
451 */
452 seg = SPLINE_LIST_ELT (spline_list, SPLINE_LIST_LENGTH (spline_list) - 1);
453 stroke = gimp_vectors_bezier_stroke_new_moveto (vectors,
454 END_POINT (seg).x,
455 END_POINT (seg).y);
456
457 for (i = SPLINE_LIST_LENGTH (spline_list); i > 0; i--)
458 {
459 seg = SPLINE_LIST_ELT (spline_list, i-1);
460
461 if (SPLINE_DEGREE (seg) == LINEAR)
462 gimp_vectors_bezier_stroke_lineto (vectors, stroke,
463 START_POINT (seg).x,
464 START_POINT (seg).y);
465 else if (SPLINE_DEGREE (seg) == CUBIC)
466 gimp_vectors_bezier_stroke_cubicto (vectors, stroke,
467 CONTROL2 (seg).x,
468 CONTROL2 (seg).y,
469 CONTROL1 (seg).x,
470 CONTROL1 (seg).y,
471 START_POINT (seg).x,
472 START_POINT (seg).y);
473 else
474 g_warning ("print_spline: strange degree (%d)",
475 SPLINE_DEGREE (seg));
476 }
477
478 gimp_vectors_stroke_close (vectors, stroke);
479
480 /* transform to GIMPs coordinate system, taking the selections
481 * bounding box into account */
482 gimp_vectors_stroke_scale (vectors, stroke, 1.0, -1.0);
483 gimp_vectors_stroke_translate (vectors, stroke,
484 sel_x1, sel_y1 + sel_height + 1);
485 }
486
487 gimp_image_insert_vectors (image_ID, vectors, -1, -1);
488 }
489
490
491 static gboolean
sel2path(gint32 image_ID)492 sel2path (gint32 image_ID)
493 {
494 gint32 selection_ID;
495 GeglBuffer *sel_buffer;
496 pixel_outline_list_type olt;
497 spline_list_array_type splines;
498
499 gimp_selection_bounds (image_ID, &has_sel,
500 &sel_x1, &sel_y1, &sel_x2, &sel_y2);
501
502 sel_width = sel_x2 - sel_x1;
503 sel_height = sel_y2 - sel_y1;
504
505 /* Now get the selection channel */
506
507 selection_ID = gimp_image_get_selection (image_ID);
508
509 if (selection_ID < 0)
510 return FALSE;
511
512 sel_buffer = gimp_drawable_get_buffer (selection_ID);
513 sel_sampler = gegl_buffer_sampler_new (sel_buffer,
514 babl_format ("Y u8"),
515 GEGL_SAMPLER_NEAREST);
516
517 olt = find_outline_pixels ();
518
519 splines = fitted_splines (olt);
520
521 do_points (splines, image_ID);
522
523 g_object_unref (sel_sampler);
524 g_object_unref (sel_buffer);
525
526 gimp_displays_flush ();
527
528 return TRUE;
529 }
530
531 void
safe_free(address * item)532 safe_free (address *item)
533 {
534 g_free (*item);
535 *item = NULL;
536 }
537