PIKApp/plug-ins/selection-to-path/selection-to-path.c

641 lines
21 KiB
C
Raw Normal View History

2023-09-26 00:35:21 +02:00
/*
* Copyright (C) 1995 Spencer Kimball and Peter Mattis
*
* This is a plug-in for PIKA.
*
* 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 3 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, see <https://www.gnu.org/licenses/>.
*
*/
/* Change log:-
* 0.1 First version.
*/
#include "config.h"
#include <string.h>
#include "libpika/pika.h"
#include "libpika/pikaui.h"
#include "libpikamath/pikamath.h"
#include "global.h"
#include "types.h"
#include "pxl-outline.h"
#include "fit.h"
#include "spline.h"
#include "selection-to-path.h"
#include "libpika/stdplugins-intl.h"
#define PLUG_IN_PROC "plug-in-sel2path"
#define PLUG_IN_BINARY "selection-to-path"
#define PLUG_IN_ROLE "pika-selection-to-path"
#define RESPONSE_RESET 1
#define MID_POINT 127
typedef struct _Sel2path Sel2path;
typedef struct _Sel2pathClass Sel2pathClass;
struct _Sel2path
{
PikaPlugIn parent_instance;
};
struct _Sel2pathClass
{
PikaPlugInClass parent_class;
};
#define SEL2PATH_TYPE (sel2path_get_type ())
#define SEL2PATH (obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), SEL2PATH_TYPE, Sel2path))
GType sel2path_get_type (void) G_GNUC_CONST;
static GList * sel2path_query_procedures (PikaPlugIn *plug_in);
static PikaProcedure * sel2path_create_procedure (PikaPlugIn *plug_in,
const gchar *name);
static PikaValueArray * sel2path_run (PikaProcedure *procedure,
PikaRunMode run_mode,
PikaImage *image,
gint n_drawables,
PikaDrawable **drawables,
const PikaValueArray *args,
gpointer run_data);
static gint sel2path_dialog (SELVALS *sels);
static void sel2path_response (GtkWidget *widget,
gint response_id,
gpointer data);
static void dialog_print_selVals (SELVALS *sels);
static gboolean sel2path (PikaImage *image);
G_DEFINE_TYPE (Sel2path, sel2path, PIKA_TYPE_PLUG_IN)
PIKA_MAIN (SEL2PATH_TYPE)
DEFINE_STD_SET_I18N
static gint sel_x1, sel_y1, sel_x2, sel_y2;
static gint has_sel, sel_width, sel_height;
static SELVALS selVals;
static GeglSampler *sel_sampler;
static gboolean retVal = TRUE; /* Toggle if cancel button clicked */
static void
sel2path_class_init (Sel2pathClass *klass)
{
PikaPlugInClass *plug_in_class = PIKA_PLUG_IN_CLASS (klass);
plug_in_class->query_procedures = sel2path_query_procedures;
plug_in_class->create_procedure = sel2path_create_procedure;
plug_in_class->set_i18n = STD_SET_I18N;
}
static void
sel2path_init (Sel2path *sel2path)
{
}
static GList *
sel2path_query_procedures (PikaPlugIn *plug_in)
{
return g_list_append (NULL, g_strdup (PLUG_IN_PROC));
}
static PikaProcedure *
sel2path_create_procedure (PikaPlugIn *plug_in,
const gchar *name)
{
PikaProcedure *procedure = NULL;
if (! strcmp (name, PLUG_IN_PROC))
{
procedure = pika_image_procedure_new (plug_in, name,
PIKA_PDB_PROC_TYPE_PLUGIN,
sel2path_run, NULL, NULL);
pika_procedure_set_image_types (procedure, "*");
pika_procedure_set_sensitivity_mask (procedure,
PIKA_PROCEDURE_SENSITIVE_DRAWABLE |
PIKA_PROCEDURE_SENSITIVE_DRAWABLES |
PIKA_PROCEDURE_SENSITIVE_NO_DRAWABLES);
pika_procedure_set_documentation (procedure,
"Converts a selection to a path",
"Converts a selection to a path",
name);
pika_procedure_set_attribution (procedure,
"Andy Thomas",
"Andy Thomas",
"1999");
PIKA_PROC_ARG_DOUBLE (procedure, "align-threshold",
"Align threshold",
"Align threshold",
0.2, 2.0, 0.5,
G_PARAM_READWRITE);
PIKA_PROC_ARG_DOUBLE (procedure, "corner-always-threshold",
"Corner always threshold",
"Corner always threshold",
30, 180, 60.0,
G_PARAM_READWRITE);
PIKA_PROC_ARG_INT (procedure, "corner-surround",
"Corner surround",
"Corner surround",
3, 8, 4,
G_PARAM_READWRITE);
PIKA_PROC_ARG_DOUBLE (procedure, "corner-threshold",
"Corner threshold",
"Corner threshold",
0, 180, 100.0,
G_PARAM_READWRITE);
PIKA_PROC_ARG_DOUBLE (procedure, "error-threshold",
"Error threshold",
"Error threshold",
0.2, 10, 0.4,
G_PARAM_READWRITE);
PIKA_PROC_ARG_INT (procedure, "filter-alternative-surround",
"Filter alternative surround",
"Filter alternative surround",
1, 10, 1,
G_PARAM_READWRITE);
PIKA_PROC_ARG_DOUBLE (procedure, "filter-epsilon",
"Filter epsilon",
"Filter epsilon",
5, 40, 10.0,
G_PARAM_READWRITE);
PIKA_PROC_ARG_INT (procedure, "filter-iteration-count",
"Filter iteration count",
"Filter iteration count",
4, 70, 4,
G_PARAM_READWRITE);
PIKA_PROC_ARG_DOUBLE (procedure, "filter-percent",
"Filter percent",
"Filter percent",
0, 1, 0.33,
G_PARAM_READWRITE);
PIKA_PROC_ARG_INT (procedure, "filter-secondary-surround",
"Filter secondary surround",
"Filter secondary surround",
3, 10, 3,
G_PARAM_READWRITE);
PIKA_PROC_ARG_INT (procedure, "filter-surround",
"Filter surround",
"Filter surround",
2, 10, 2,
G_PARAM_READWRITE);
PIKA_PROC_ARG_BOOLEAN (procedure, "keep-knees",
"Keep knees",
"Keep knees",
FALSE,
G_PARAM_READWRITE);
PIKA_PROC_ARG_DOUBLE (procedure, "line-reversion-threshold",
"Line reversion threshold",
"Line reversion threshold",
0.01, 0.2, 0.01,
G_PARAM_READWRITE);
PIKA_PROC_ARG_DOUBLE (procedure, "line-threshold",
"Line threshold",
"Line threshold",
0.2, 4, 0.5,
G_PARAM_READWRITE);
PIKA_PROC_ARG_DOUBLE (procedure, "reparametrize-improvement",
"Reparametrize improvement",
"Reparametrize improvement",
0, 1, 0.01,
G_PARAM_READWRITE);
PIKA_PROC_ARG_DOUBLE (procedure, "reparametrize-threshold",
"Reparametrize threshold",
"Reparametrize threshold",
1, 50, 1.0,
G_PARAM_READWRITE);
PIKA_PROC_ARG_DOUBLE (procedure, "subdivide-search",
"Subdivide search",
"Subdivide search",
0.05, 1, 0.1,
G_PARAM_READWRITE);
PIKA_PROC_ARG_INT (procedure, "subdivide-surround",
"Subdivide surround",
"Subdivide surround",
2, 10, 4,
G_PARAM_READWRITE);
PIKA_PROC_ARG_DOUBLE (procedure, "subdivide-threshold",
"Subdivide threshold",
"Subdivide threshold",
0.01, 1, 0.03,
G_PARAM_READWRITE);
PIKA_PROC_ARG_INT (procedure, "tangent-surround",
"Tangent surround",
"Tangent surround",
2, 10, 3,
G_PARAM_READWRITE);
}
return procedure;
}
static PikaValueArray *
sel2path_run (PikaProcedure *procedure,
PikaRunMode run_mode,
PikaImage *image,
gint n_drawables,
PikaDrawable **drawables,
const PikaValueArray *args,
gpointer run_data)
{
gegl_init (NULL, NULL);
if (pika_selection_is_empty (image))
{
g_message (_("No selection to convert"));
return pika_procedure_new_return_values (procedure,
PIKA_PDB_SUCCESS,
NULL);
}
fit_set_default_params (&selVals);
switch (run_mode)
{
case PIKA_RUN_INTERACTIVE:
if (pika_get_data_size (PLUG_IN_PROC) > 0)
pika_get_data (PLUG_IN_PROC, &selVals);
if (! sel2path_dialog (&selVals))
return pika_procedure_new_return_values (procedure,
PIKA_PDB_CANCEL,
NULL);
/* Get the current settings */
fit_set_params (&selVals);
break;
case PIKA_RUN_NONINTERACTIVE:
selVals.align_threshold = PIKA_VALUES_GET_DOUBLE (args, 0);
selVals.corner_always_threshold = PIKA_VALUES_GET_DOUBLE (args, 1);
selVals.corner_surround = PIKA_VALUES_GET_INT (args, 2);
selVals.corner_threshold = PIKA_VALUES_GET_DOUBLE (args, 3);
selVals.error_threshold = PIKA_VALUES_GET_DOUBLE (args, 4);
selVals.filter_alternative_surround = PIKA_VALUES_GET_INT (args, 5);
selVals.filter_epsilon = PIKA_VALUES_GET_DOUBLE (args, 6);
selVals.filter_iteration_count = PIKA_VALUES_GET_INT (args, 7);
selVals.filter_percent = PIKA_VALUES_GET_DOUBLE (args, 8);
selVals.filter_secondary_surround = PIKA_VALUES_GET_INT (args, 9);
selVals.filter_surround = PIKA_VALUES_GET_INT (args, 10);
selVals.keep_knees = PIKA_VALUES_GET_BOOLEAN (args, 11);
selVals.line_reversion_threshold = PIKA_VALUES_GET_DOUBLE (args, 12);
selVals.line_threshold = PIKA_VALUES_GET_DOUBLE (args, 13);
selVals.reparameterize_improvement = PIKA_VALUES_GET_DOUBLE (args, 14);
selVals.reparameterize_threshold = PIKA_VALUES_GET_DOUBLE (args, 15);
selVals.subdivide_search = PIKA_VALUES_GET_DOUBLE (args, 16);
selVals.subdivide_surround = PIKA_VALUES_GET_INT (args, 17);
selVals.subdivide_threshold = PIKA_VALUES_GET_DOUBLE (args, 18);
selVals.tangent_surround = PIKA_VALUES_GET_INT (args, 19);
fit_set_params (&selVals);
break;
case PIKA_RUN_WITH_LAST_VALS:
if (pika_get_data_size (PLUG_IN_PROC) > 0)
{
pika_get_data (PLUG_IN_PROC, &selVals);
/* Set up the last values */
fit_set_params (&selVals);
}
break;
default:
break;
}
if (! sel2path (image))
return pika_procedure_new_return_values (procedure,
PIKA_PDB_EXECUTION_ERROR,
NULL);
dialog_print_selVals (&selVals);
if (run_mode == PIKA_RUN_INTERACTIVE)
pika_set_data (PLUG_IN_PROC, &selVals, sizeof(SELVALS));
return pika_procedure_new_return_values (procedure, PIKA_PDB_SUCCESS, NULL);
}
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;
pika_ui_init (PLUG_IN_BINARY);
dlg = pika_dialog_new (_("Selection to Path Advanced Settings"),
PLUG_IN_ROLE,
NULL, 0,
pika_standard_help_func, PLUG_IN_PROC,
_("_Reset"), RESPONSE_RESET,
_("_Cancel"), GTK_RESPONSE_CANCEL,
_("_OK"), GTK_RESPONSE_OK,
NULL);
pika_dialog_set_alternative_button_order (GTK_DIALOG (dlg),
RESPONSE_RESET,
GTK_RESPONSE_OK,
GTK_RESPONSE_CANCEL,
-1);
pika_window_set_transient (GTK_WINDOW (dlg));
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_box_pack_start (GTK_BOX (gtk_dialog_get_content_area (GTK_DIALOG (dlg))),
table, TRUE, TRUE, 0);
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;
}
gegl_sampler_get (sel_sampler,
col + sel_x1, row + sel_y1, NULL, &ret, GEGL_ABYSS_NONE);
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;
}
gboolean
sel_valid_pixel (gint row,
gint col)
{
return (0 <= (row) && (row) < sel_get_height ()
&& 0 <= (col) && (col) < sel_get_width ());
}
static void
do_points (spline_list_array_type in_splines,
PikaImage *image)
{
PikaVectors *vectors;
gint32 stroke;
gint i, j;
gboolean have_points = FALSE;
spline_list_type spline_list;
/* check if there really is something to do... */
for (i = 0; i < SPLINE_LIST_ARRAY_LENGTH (in_splines); i++)
{
spline_list = SPLINE_LIST_ARRAY_ELT (in_splines, i);
/* Ignore single points that are on their own */
if (SPLINE_LIST_LENGTH (spline_list) < 2)
continue;
have_points = TRUE;
break;
}
if (! have_points)
return;
vectors = pika_vectors_new (image, _("Selection"));
for (j = 0; j < SPLINE_LIST_ARRAY_LENGTH (in_splines); j++)
{
spline_type seg;
spline_list = SPLINE_LIST_ARRAY_ELT (in_splines, j);
/* Ignore single points that are on their own */
if (SPLINE_LIST_LENGTH (spline_list) < 2)
continue;
/*
* we're constructing the path backwards
* to have the result of least surprise for "Text along Path".
*/
seg = SPLINE_LIST_ELT (spline_list, SPLINE_LIST_LENGTH (spline_list) - 1);
stroke = pika_vectors_bezier_stroke_new_moveto (vectors,
END_POINT (seg).x,
END_POINT (seg).y);
for (i = SPLINE_LIST_LENGTH (spline_list); i > 0; i--)
{
seg = SPLINE_LIST_ELT (spline_list, i-1);
if (SPLINE_DEGREE (seg) == LINEAR)
pika_vectors_bezier_stroke_lineto (vectors, stroke,
START_POINT (seg).x,
START_POINT (seg).y);
else if (SPLINE_DEGREE (seg) == CUBIC)
pika_vectors_bezier_stroke_cubicto (vectors, stroke,
CONTROL2 (seg).x,
CONTROL2 (seg).y,
CONTROL1 (seg).x,
CONTROL1 (seg).y,
START_POINT (seg).x,
START_POINT (seg).y);
else
g_warning ("print_spline: strange degree (%d)",
SPLINE_DEGREE (seg));
}
pika_vectors_stroke_close (vectors, stroke);
/* transform to PIKAs coordinate system, taking the selections
* bounding box into account */
pika_vectors_stroke_scale (vectors, stroke, 1.0, -1.0);
pika_vectors_stroke_translate (vectors, stroke,
sel_x1, sel_y1 + sel_height + 1);
}
pika_image_insert_vectors (image, vectors, NULL, -1);
}
static gboolean
sel2path (PikaImage *image)
{
PikaSelection *selection;
GeglBuffer *sel_buffer;
pixel_outline_list_type olt;
spline_list_array_type splines;
pika_selection_bounds (image, &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 = pika_image_get_selection (image);
if (! selection)
return FALSE;
sel_buffer = pika_drawable_get_buffer (PIKA_DRAWABLE (selection));
sel_sampler = gegl_buffer_sampler_new (sel_buffer,
babl_format ("Y u8"),
GEGL_SAMPLER_NEAREST);
olt = find_outline_pixels ();
splines = fitted_splines (olt);
do_points (splines, image);
g_object_unref (sel_sampler);
g_object_unref (sel_buffer);
pika_displays_flush ();
return TRUE;
}
void
safe_free (address *item)
{
g_free (*item);
*item = NULL;
}