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

636 lines
25 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,
2023-10-30 23:55:30 +01:00
PikaProcedureConfig *config,
2023-09-26 00:35:21 +02:00
gpointer run_data);
2023-10-30 23:55:30 +01:00
static gint sel2path_dialog (PikaProcedure *procedure,
PikaProcedureConfig *config);
2023-09-26 00:35:21 +02:00
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 GeglSampler *sel_sampler;
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,
2023-10-30 23:55:30 +01:00
_("Converts a selection to a path"),
_("Converts a selection to a path"),
2023-09-26 00:35:21 +02:00
name);
pika_procedure_set_attribution (procedure,
"Andy Thomas",
"Andy Thomas",
"1999");
PIKA_PROC_ARG_DOUBLE (procedure, "align-threshold",
2023-10-30 23:55:30 +01:00
_("_Align Threshold"),
_("If two endpoints are closer than this, "
"they are made to be equal."),
2023-09-26 00:35:21 +02:00
0.2, 2.0, 0.5,
G_PARAM_READWRITE);
PIKA_PROC_ARG_DOUBLE (procedure, "corner-always-threshold",
2023-10-30 23:55:30 +01:00
_("Corner Al_ways Threshold"),
_("If the angle defined by a point and its predecessors "
"and successors is smaller than this, it's a corner, "
"even if it's within 'corner_surround' pixels of a "
"point with a smaller angle."),
2023-09-26 00:35:21 +02:00
30, 180, 60.0,
G_PARAM_READWRITE);
PIKA_PROC_ARG_INT (procedure, "corner-surround",
2023-10-30 23:55:30 +01:00
_("Corner _Surround"),
_("Number of points to consider when determining if a "
"point is a corner or not."),
2023-09-26 00:35:21 +02:00
3, 8, 4,
G_PARAM_READWRITE);
PIKA_PROC_ARG_DOUBLE (procedure, "corner-threshold",
2023-10-30 23:55:30 +01:00
_("Cor_ner Threshold"),
_("If a point, its predecessors, and its successors "
"define an angle smaller than this, it's a corner."),
2023-09-26 00:35:21 +02:00
0, 180, 100.0,
G_PARAM_READWRITE);
PIKA_PROC_ARG_DOUBLE (procedure, "error-threshold",
2023-10-30 23:55:30 +01:00
_("Error Thres_hold"),
_("Amount of error at which a fitted spline is "
"unacceptable. If any pixel is further away "
"than this from the fitted curve, we try again."),
2023-09-26 00:35:21 +02:00
0.2, 10, 0.4,
G_PARAM_READWRITE);
PIKA_PROC_ARG_INT (procedure, "filter-alternative-surround",
2023-10-30 23:55:30 +01:00
_("_Filter Alternative Surround"),
_("A second number of adjacent points to consider "
"when filtering."),
2023-09-26 00:35:21 +02:00
1, 10, 1,
G_PARAM_READWRITE);
PIKA_PROC_ARG_DOUBLE (procedure, "filter-epsilon",
2023-10-30 23:55:30 +01:00
_("Filter E_psilon"),
_("If the angles between the vectors produced by "
"filter_surround and filter_alternative_surround "
"points differ by more than this, use the one from "
"filter_alternative_surround."),
2023-09-26 00:35:21 +02:00
5, 40, 10.0,
G_PARAM_READWRITE);
PIKA_PROC_ARG_INT (procedure, "filter-iteration-count",
2023-10-30 23:55:30 +01:00
_("Filter Iteration Co_unt"),
_("Number of times to smooth original data points. "
"Increasing this number dramatically --- to 50 or "
"so --- can produce vastly better results. But if "
"any points that 'should' be corners aren't found, "
"the curve goes to hell around that point."),
2023-09-26 00:35:21 +02:00
4, 70, 4,
G_PARAM_READWRITE);
PIKA_PROC_ARG_DOUBLE (procedure, "filter-percent",
2023-10-30 23:55:30 +01:00
_("Filt_er Percent"),
_("To produce the new point, use the old point plus "
"this times the neighbors."),
2023-09-26 00:35:21 +02:00
0, 1, 0.33,
G_PARAM_READWRITE);
PIKA_PROC_ARG_INT (procedure, "filter-secondary-surround",
2023-10-30 23:55:30 +01:00
_("Filter Secondar_y Surround"),
_("Number of adjacent points to consider if "
"'filter_surround' points defines a straight line."),
2023-09-26 00:35:21 +02:00
3, 10, 3,
G_PARAM_READWRITE);
PIKA_PROC_ARG_INT (procedure, "filter-surround",
2023-10-30 23:55:30 +01:00
_("Filter Surroun_d"),
_("Number of adjacent points to consider when filtering."),
2023-09-26 00:35:21 +02:00
2, 10, 2,
G_PARAM_READWRITE);
PIKA_PROC_ARG_BOOLEAN (procedure, "keep-knees",
2023-10-30 23:55:30 +01:00
_("_Keep Knees"),
_("Says whether or not to remove 'knee' "
"points after finding the outline."),
2023-09-26 00:35:21 +02:00
FALSE,
G_PARAM_READWRITE);
PIKA_PROC_ARG_DOUBLE (procedure, "line-reversion-threshold",
2023-10-30 23:55:30 +01:00
_("_Line Reversion Threshold"),
_("If a spline is closer to a straight line than this, "
"it remains a straight line, even if it would otherwise "
"be changed back to a curve. This is weighted by the "
"square of the curve length, to make shorter curves "
"more likely to be reverted."),
2023-09-26 00:35:21 +02:00
0.01, 0.2, 0.01,
G_PARAM_READWRITE);
PIKA_PROC_ARG_DOUBLE (procedure, "line-threshold",
2023-10-30 23:55:30 +01:00
_("L_ine Threshold"),
_("How many pixels (on the average) a spline can "
"diverge from the line determined by its endpoints "
"before it is changed to a straight line."),
2023-09-26 00:35:21 +02:00
0.2, 4, 0.5,
G_PARAM_READWRITE);
PIKA_PROC_ARG_DOUBLE (procedure, "reparametrize-improvement",
2023-10-30 23:55:30 +01:00
_("Reparametri_ze Improvement"),
_("If reparameterization doesn't improve the fit by this "
"much percent, stop doing it. ""Amount of error at which "
"it is pointless to reparameterize."),
2023-09-26 00:35:21 +02:00
0, 1, 0.01,
G_PARAM_READWRITE);
PIKA_PROC_ARG_DOUBLE (procedure, "reparametrize-threshold",
2023-10-30 23:55:30 +01:00
_("Repara_metrize Threshold"),
_("Amount of error at which it is pointless to reparameterize. "
"This happens, for example, when we are trying to fit the "
"outline of the outside of an 'O' with a single spline. "
"The initial fit is not good enough for the Newton-Raphson "
"iteration to improve it. It may be that it would be better "
"to detect the cases where we didn't find any corners."),
2023-09-26 00:35:21 +02:00
1, 50, 1.0,
G_PARAM_READWRITE);
PIKA_PROC_ARG_DOUBLE (procedure, "subdivide-search",
2023-10-30 23:55:30 +01:00
_("Subdi_vide Search"),
_("Percentage of the curve away from the worst point "
"to look for a better place to subdivide."),
2023-09-26 00:35:21 +02:00
0.05, 1, 0.1,
G_PARAM_READWRITE);
PIKA_PROC_ARG_INT (procedure, "subdivide-surround",
2023-10-30 23:55:30 +01:00
_("Su_bdivide Surround"),
_("Number of points to consider when deciding whether "
"a given point is a better place to subdivide."),
2023-09-26 00:35:21 +02:00
2, 10, 4,
G_PARAM_READWRITE);
PIKA_PROC_ARG_DOUBLE (procedure, "subdivide-threshold",
2023-10-30 23:55:30 +01:00
_("Subdivide Th_reshold"),
_("How many pixels a point can diverge from a straight "
"line and still be considered a better place to "
"subdivide."),
2023-09-26 00:35:21 +02:00
0.01, 1, 0.03,
G_PARAM_READWRITE);
PIKA_PROC_ARG_INT (procedure, "tangent-surround",
2023-10-30 23:55:30 +01:00
_("_Tangent Surround"),
_("Number of points to look at on either side of a "
"point when computing the approximation to the "
"tangent at that point."),
2023-09-26 00:35:21 +02:00
2, 10, 3,
G_PARAM_READWRITE);
}
return procedure;
}
static PikaValueArray *
sel2path_run (PikaProcedure *procedure,
PikaRunMode run_mode,
PikaImage *image,
gint n_drawables,
PikaDrawable **drawables,
2023-10-30 23:55:30 +01:00
PikaProcedureConfig *config,
2023-09-26 00:35:21 +02:00
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);
}
2023-10-30 23:55:30 +01:00
if (run_mode == PIKA_RUN_INTERACTIVE &&
! sel2path_dialog (procedure, config))
return pika_procedure_new_return_values (procedure,
PIKA_PDB_CANCEL,
NULL);
2023-09-26 00:35:21 +02:00
2023-10-30 23:55:30 +01:00
fit_set_params (config);
2023-09-26 00:35:21 +02:00
if (! sel2path (image))
return pika_procedure_new_return_values (procedure,
PIKA_PDB_EXECUTION_ERROR,
NULL);
return pika_procedure_new_return_values (procedure, PIKA_PDB_SUCCESS, NULL);
}
/* Build the dialog up. This was the hard part! */
static gint
2023-10-30 23:55:30 +01:00
sel2path_dialog (PikaProcedure *procedure,
PikaProcedureConfig *config)
2023-09-26 00:35:21 +02:00
{
2023-10-30 23:55:30 +01:00
GtkWidget *dialog;
GtkWidget *vbox;
GtkWidget *scrolled_win;
gboolean run;
2023-09-26 00:35:21 +02:00
pika_ui_init (PLUG_IN_BINARY);
2023-10-30 23:55:30 +01:00
dialog = pika_procedure_dialog_new (procedure,
PIKA_PROCEDURE_CONFIG (config),
_("Selection to Path Advanced Settings"));
2023-09-26 00:35:21 +02:00
2023-10-30 23:55:30 +01:00
pika_dialog_set_alternative_button_order (GTK_DIALOG (dialog),
2023-09-26 00:35:21 +02:00
RESPONSE_RESET,
GTK_RESPONSE_OK,
GTK_RESPONSE_CANCEL,
-1);
2023-10-30 23:55:30 +01:00
pika_window_set_transient (GTK_WINDOW (dialog));
pika_procedure_dialog_get_scale_entry (PIKA_PROCEDURE_DIALOG (dialog),
"align-threshold", 1.0);
pika_procedure_dialog_get_scale_entry (PIKA_PROCEDURE_DIALOG (dialog),
"corner-always-threshold", 1.0);
pika_procedure_dialog_get_scale_entry (PIKA_PROCEDURE_DIALOG (dialog),
"corner-surround", 1.0);
pika_procedure_dialog_get_scale_entry (PIKA_PROCEDURE_DIALOG (dialog),
"corner-threshold", 1.0);
pika_procedure_dialog_get_scale_entry (PIKA_PROCEDURE_DIALOG (dialog),
"error-threshold", 1.0);
pika_procedure_dialog_get_scale_entry (PIKA_PROCEDURE_DIALOG (dialog),
"filter-alternative-surround", 1.0);
pika_procedure_dialog_get_scale_entry (PIKA_PROCEDURE_DIALOG (dialog),
"filter-epsilon", 1.0);
pika_procedure_dialog_get_scale_entry (PIKA_PROCEDURE_DIALOG (dialog),
"filter-iteration-count", 1.0);
pika_procedure_dialog_get_scale_entry (PIKA_PROCEDURE_DIALOG (dialog),
"filter-percent", 1.0);
pika_procedure_dialog_get_scale_entry (PIKA_PROCEDURE_DIALOG (dialog),
"filter-secondary-surround", 1.0);
pika_procedure_dialog_get_scale_entry (PIKA_PROCEDURE_DIALOG (dialog),
"filter-surround", 1.0);
pika_procedure_dialog_get_scale_entry (PIKA_PROCEDURE_DIALOG (dialog),
"line-reversion-threshold", 1.0);
pika_procedure_dialog_get_scale_entry (PIKA_PROCEDURE_DIALOG (dialog),
"line-threshold", 1.0);
pika_procedure_dialog_get_scale_entry (PIKA_PROCEDURE_DIALOG (dialog),
"reparametrize-improvement", 1.0);
pika_procedure_dialog_get_scale_entry (PIKA_PROCEDURE_DIALOG (dialog),
"reparametrize-threshold", 1.0);
pika_procedure_dialog_get_scale_entry (PIKA_PROCEDURE_DIALOG (dialog),
"subdivide-search", 1.0);
pika_procedure_dialog_get_scale_entry (PIKA_PROCEDURE_DIALOG (dialog),
"subdivide-surround", 1.0);
pika_procedure_dialog_get_scale_entry (PIKA_PROCEDURE_DIALOG (dialog),
"subdivide-threshold", 1.0);
pika_procedure_dialog_get_scale_entry (PIKA_PROCEDURE_DIALOG (dialog),
"tangent-surround", 1.0);
vbox = pika_procedure_dialog_fill_box (PIKA_PROCEDURE_DIALOG (dialog),
"selection-to-path-box",
"align-threshold",
"corner-always-threshold",
"corner-surround",
"corner-threshold",
"error-threshold",
"filter-alternative-surround",
"filter-epsilon",
"filter-iteration-count",
"filter-percent",
"filter-secondary-surround",
"filter-surround",
"keep-knees",
"line-reversion-threshold",
"line-threshold",
"reparametrize-improvement",
"reparametrize-threshold",
"subdivide-search",
"subdivide-surround",
"subdivide-threshold",
"tangent-surround",
NULL);
gtk_box_set_spacing (GTK_BOX (vbox), 12);
scrolled_win = pika_procedure_dialog_fill_scrolled_window (PIKA_PROCEDURE_DIALOG (dialog),
"scrollwin",
"selection-to-path-box");
gtk_widget_set_size_request (scrolled_win, 400, 400);
gtk_scrolled_window_set_shadow_type (GTK_SCROLLED_WINDOW (scrolled_win),
GTK_SHADOW_NONE);
gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (scrolled_win),
GTK_POLICY_NEVER,
GTK_POLICY_ALWAYS);
gtk_scrolled_window_set_overlay_scrolling (GTK_SCROLLED_WINDOW (scrolled_win),
FALSE);
pika_procedure_dialog_fill (PIKA_PROCEDURE_DIALOG (dialog),
"scrollwin",
NULL);
gtk_widget_show (dialog);
run = pika_procedure_dialog_run (PIKA_PROCEDURE_DIALOG (dialog));
gtk_widget_destroy (dialog);
return run;
2023-09-26 00:35:21 +02:00
}
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;
}