Commit d7bf9de5 authored by Michael Natterer's avatar Michael Natterer 😴

Bug 759104 - Allow coordinates of sample points to be accessed from scripts

Add PDB sample point API similar to how the guide API works. Add core
API similar to the core guide API to make guide and sample point APIs
as similar as possible.
parent 96339dd6
......@@ -75,8 +75,7 @@ gimp_image_add_sample_point (GimpImage *image,
private->sample_points = g_list_append (private->sample_points, sample_point);
sample_point->x = x;
sample_point->y = y;
gimp_sample_point_set_position (sample_point, x, y);
gimp_sample_point_ref (sample_point);
gimp_image_sample_point_added (image, sample_point);
......@@ -103,8 +102,7 @@ gimp_image_remove_sample_point (GimpImage *image,
gimp_image_sample_point_removed (image, sample_point);
sample_point->x = -1;
sample_point->y = -1;
gimp_sample_point_set_position (sample_point, -1, -1);
gimp_sample_point_unref (sample_point);
}
......@@ -127,8 +125,7 @@ gimp_image_move_sample_point (GimpImage *image,
C_("undo-type", "Move Sample Point"),
sample_point);
sample_point->x = x;
sample_point->y = y;
gimp_sample_point_set_position (sample_point, x, y);
gimp_image_sample_point_moved (image, sample_point);
}
......@@ -141,6 +138,58 @@ gimp_image_get_sample_points (GimpImage *image)
return GIMP_IMAGE_GET_PRIVATE (image)->sample_points;
}
GimpSamplePoint *
gimp_image_get_sample_point (GimpImage *image,
guint32 id)
{
GList *sample_points;
g_return_val_if_fail (GIMP_IS_IMAGE (image), NULL);
for (sample_points = GIMP_IMAGE_GET_PRIVATE (image)->sample_points;
sample_points;
sample_points = g_list_next (sample_points))
{
GimpSamplePoint *sample_point = sample_points->data;
if (gimp_sample_point_get_ID (sample_point) == id)
return sample_point;
}
return NULL;
}
GimpSamplePoint *
gimp_image_get_next_sample_point (GimpImage *image,
guint32 id,
gboolean *sample_point_found)
{
GList *sample_points;
g_return_val_if_fail (GIMP_IS_IMAGE (image), NULL);
g_return_val_if_fail (sample_point_found != NULL, NULL);
if (id == 0)
*sample_point_found = TRUE;
else
*sample_point_found = FALSE;
for (sample_points = GIMP_IMAGE_GET_PRIVATE (image)->sample_points;
sample_points;
sample_points = g_list_next (sample_points))
{
GimpSamplePoint *sample_point = sample_points->data;
if (*sample_point_found) /* this is the first guide after the found one */
return sample_point;
if (gimp_sample_point_get_ID (sample_point) == id) /* found it, next one will be returned */
*sample_point_found = TRUE;
}
return NULL;
}
GimpSamplePoint *
gimp_image_find_sample_point (GimpImage *image,
gdouble x,
......@@ -166,13 +215,17 @@ gimp_image_find_sample_point (GimpImage *image,
list = g_list_next (list))
{
GimpSamplePoint *sample_point = list->data;
gint sp_x;
gint sp_y;
gdouble dist;
if (sample_point->x < 0 || sample_point->y < 0)
gimp_sample_point_get_position (sample_point, &sp_x, &sp_y);
if (sp_x < 0 || sp_y < 0)
continue;
dist = hypot ((sample_point->x + 0.5) - x,
(sample_point->y + 0.5) - y);
dist = hypot ((sp_x + 0.5) - x,
(sp_y + 0.5) - y);
if (dist < MIN (epsilon_y, mindist))
{
mindist = dist;
......
......@@ -45,6 +45,12 @@ void gimp_image_move_sample_point (GimpImage *image,
gboolean push_undo);
GList * gimp_image_get_sample_points (GimpImage *image);
GimpSamplePoint * gimp_image_get_sample_point (GimpImage *image,
guint32 id);
GimpSamplePoint * gimp_image_get_next_sample_point (GimpImage *image,
guint32 id,
gboolean *sample_point_found);
GimpSamplePoint * gimp_image_find_sample_point (GimpImage *image,
gdouble x,
gdouble y,
......
......@@ -72,3 +72,35 @@ gimp_sample_point_unref (GimpSamplePoint *sample_point)
if (sample_point->ref_count < 1)
g_slice_free (GimpSamplePoint, sample_point);
}
guint32
gimp_sample_point_get_ID (GimpSamplePoint *sample_point)
{
g_return_val_if_fail (sample_point != NULL, 0);
return sample_point->sample_point_ID;
}
void
gimp_sample_point_get_position (GimpSamplePoint *sample_point,
gint *position_x,
gint *position_y)
{
g_return_if_fail (sample_point != NULL);
g_return_if_fail (position_x != NULL);
g_return_if_fail (position_y != NULL);
*position_x = sample_point->x;
*position_y = sample_point->y;
}
void
gimp_sample_point_set_position (GimpSamplePoint *sample_point,
gint position_x,
gint position_y)
{
g_return_if_fail (sample_point != NULL);
sample_point->x = position_x;
sample_point->y = position_y;
}
......@@ -33,10 +33,18 @@ struct _GimpSamplePoint
GType gimp_sample_point_get_type (void) G_GNUC_CONST;
GimpSamplePoint * gimp_sample_point_new (guint32 sample_point_ID);
GimpSamplePoint * gimp_sample_point_new (guint32 sample_point_ID);
GimpSamplePoint * gimp_sample_point_ref (GimpSamplePoint *sample_point);
void gimp_sample_point_unref (GimpSamplePoint *sample_point);
GimpSamplePoint * gimp_sample_point_ref (GimpSamplePoint *sample_point);
void gimp_sample_point_unref (GimpSamplePoint *sample_point);
guint32 gimp_sample_point_get_ID (GimpSamplePoint *sample_point);
void gimp_sample_point_get_position (GimpSamplePoint *sample_point,
gint *position_x,
gint *position_y);
void gimp_sample_point_set_position (GimpSamplePoint *sample_point,
gint position_x,
gint position_y);
#endif /* __GIMP_SAMPLE_POINT_H__ */
......@@ -91,8 +91,9 @@ gimp_sample_point_undo_constructed (GObject *object)
g_assert (sample_point_undo->sample_point != NULL);
sample_point_undo->x = sample_point_undo->sample_point->x;
sample_point_undo->y = sample_point_undo->sample_point->y;
gimp_sample_point_get_position (sample_point_undo->sample_point,
&sample_point_undo->x,
&sample_point_undo->y);
}
static void
......@@ -146,8 +147,7 @@ gimp_sample_point_undo_pop (GimpUndo *undo,
GIMP_UNDO_CLASS (parent_class)->pop (undo, undo_mode, accum);
x = sample_point_undo->sample_point->x;
y = sample_point_undo->sample_point->y;
gimp_sample_point_get_position (sample_point_undo->sample_point, &x, &y);
if (x == -1)
{
......@@ -163,8 +163,9 @@ gimp_sample_point_undo_pop (GimpUndo *undo,
}
else
{
sample_point_undo->sample_point->x = sample_point_undo->x;
sample_point_undo->sample_point->y = sample_point_undo->y;
gimp_sample_point_set_position (sample_point_undo->sample_point,
sample_point_undo->x,
sample_point_undo->y);
gimp_image_sample_point_moved (undo->image,
sample_point_undo->sample_point);
......
......@@ -62,8 +62,9 @@ libappinternal_procs_a_SOURCES = \
image-convert-cmds.c \
image-grid-cmds.c \
image-guides-cmds.c \
image-transform-cmds.c \
image-sample-points-cmds.c \
image-select-cmds.c \
image-transform-cmds.c \
image-undo-cmds.c \
item-cmds.c \
item-transform-cmds.c \
......
......@@ -31,6 +31,7 @@
#include "core/gimpdrawable.h"
#include "core/gimpimage.h"
#include "core/gimpimage-guides.h"
#include "core/gimpimage-sample-points.h"
#include "core/gimpitem.h"
#include "text/gimptextlayer.h"
......@@ -737,6 +738,29 @@ gimp_pdb_image_get_guide (GimpImage *image,
return NULL;
}
GimpSamplePoint *
gimp_pdb_image_get_sample_point (GimpImage *image,
gint sample_point_ID,
GError **error)
{
GimpSamplePoint *sample_point;
g_return_val_if_fail (GIMP_IS_IMAGE (image), NULL);
g_return_val_if_fail (error == NULL || *error == NULL, NULL);
sample_point = gimp_image_get_sample_point (image, sample_point_ID);
if (sample_point)
return sample_point;
g_set_error (error, GIMP_PDB_ERROR, GIMP_PDB_ERROR_INVALID_ARGUMENT,
_("Image '%s' (%d) does not contain sample point with ID %d"),
gimp_image_get_display_name (image),
gimp_image_get_ID (image),
sample_point_ID);
return NULL;
}
GimpStroke *
gimp_pdb_get_vectors_stroke (GimpVectors *vectors,
gint stroke_ID,
......
......@@ -103,6 +103,10 @@ gboolean gimp_pdb_image_is_not_precision (GimpImage *image,
GimpGuide * gimp_pdb_image_get_guide (GimpImage *image,
gint guide_ID,
GError **error);
GimpSamplePoint *
gimp_pdb_image_get_sample_point (GimpImage *image,
gint sample_point_ID,
GError **error);
GimpStroke * gimp_pdb_get_vectors_stroke (GimpVectors *vectors,
gint stroke_ID,
......
/* GIMP - The GNU Image Manipulation Program
* Copyright (C) 1995-2003 Spencer Kimball and Peter Mattis
*
* 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 <http://www.gnu.org/licenses/>.
*/
/* NOTE: This file is auto-generated by pdbgen.pl. */
#include "config.h"
#include <gegl.h>
#include <gdk-pixbuf/gdk-pixbuf.h>
#include "libgimpbase/gimpbase.h"
#include "pdb-types.h"
#include "core/gimpimage-sample-points.h"
#include "core/gimpimage.h"
#include "core/gimpparamspecs.h"
#include "core/gimpsamplepoint.h"
#include "gimppdb.h"
#include "gimppdberror.h"
#include "gimppdb-utils.h"
#include "gimpprocedure.h"
#include "internal-procs.h"
#include "gimp-intl.h"
static GimpValueArray *
image_add_sample_point_invoker (GimpProcedure *procedure,
Gimp *gimp,
GimpContext *context,
GimpProgress *progress,
const GimpValueArray *args,
GError **error)
{
gboolean success = TRUE;
GimpValueArray *return_vals;
GimpImage *image;
gint32 position_x;
gint32 position_y;
gint32 sample_point = 0;
image = gimp_value_get_image (gimp_value_array_index (args, 0), gimp);
position_x = g_value_get_int (gimp_value_array_index (args, 1));
position_y = g_value_get_int (gimp_value_array_index (args, 2));
if (success)
{
if (position_x <= gimp_image_get_width (image) &&
position_y <= gimp_image_get_height (image))
{
GimpSamplePoint *sp;
sp = gimp_image_add_sample_point_at_pos (image, position_x, position_y,
TRUE);
sample_point = gimp_sample_point_get_ID (sp);
}
else
success = FALSE;
}
return_vals = gimp_procedure_get_return_values (procedure, success,
error ? *error : NULL);
if (success)
g_value_set_uint (gimp_value_array_index (return_vals, 1), sample_point);
return return_vals;
}
static GimpValueArray *
image_delete_sample_point_invoker (GimpProcedure *procedure,
Gimp *gimp,
GimpContext *context,
GimpProgress *progress,
const GimpValueArray *args,
GError **error)
{
gboolean success = TRUE;
GimpImage *image;
gint32 sample_point;
image = gimp_value_get_image (gimp_value_array_index (args, 0), gimp);
sample_point = g_value_get_uint (gimp_value_array_index (args, 1));
if (success)
{
GimpSamplePoint *sp = gimp_pdb_image_get_sample_point (image, sample_point,
error);
if (sp)
gimp_image_remove_sample_point (image, sp, TRUE);
else
success = FALSE;
}
return gimp_procedure_get_return_values (procedure, success,
error ? *error : NULL);
}
static GimpValueArray *
image_find_next_sample_point_invoker (GimpProcedure *procedure,
Gimp *gimp,
GimpContext *context,
GimpProgress *progress,
const GimpValueArray *args,
GError **error)
{
gboolean success = TRUE;
GimpValueArray *return_vals;
GimpImage *image;
gint32 sample_point;
gint32 next_sample_point = 0;
image = gimp_value_get_image (gimp_value_array_index (args, 0), gimp);
sample_point = g_value_get_uint (gimp_value_array_index (args, 1));
if (success)
{
GimpSamplePoint *sp = gimp_image_get_next_sample_point (image, sample_point,
&success);
if (sp)
next_sample_point = gimp_sample_point_get_ID (sp);
if (! success)
g_set_error (error, GIMP_PDB_ERROR, GIMP_PDB_ERROR_INVALID_ARGUMENT,
_("Image '%s' (%d) does not contain sample point with ID %d"),
gimp_image_get_display_name (image),
gimp_image_get_ID (image),
sample_point);
}
return_vals = gimp_procedure_get_return_values (procedure, success,
error ? *error : NULL);
if (success)
g_value_set_uint (gimp_value_array_index (return_vals, 1), next_sample_point);
return return_vals;
}
static GimpValueArray *
image_get_sample_point_position_invoker (GimpProcedure *procedure,
Gimp *gimp,
GimpContext *context,
GimpProgress *progress,
const GimpValueArray *args,
GError **error)
{
gboolean success = TRUE;
GimpValueArray *return_vals;
GimpImage *image;
gint32 sample_point;
gint32 position_x = 0;
gint32 position_y = 0;
image = gimp_value_get_image (gimp_value_array_index (args, 0), gimp);
sample_point = g_value_get_uint (gimp_value_array_index (args, 1));
if (success)
{
GimpSamplePoint *sp = gimp_pdb_image_get_sample_point (image, sample_point,
error);
if (sp)
gimp_sample_point_get_position (sp, &position_x, &position_y);
else
success = FALSE;
}
return_vals = gimp_procedure_get_return_values (procedure, success,
error ? *error : NULL);
if (success)
{
g_value_set_int (gimp_value_array_index (return_vals, 1), position_x);
g_value_set_int (gimp_value_array_index (return_vals, 2), position_y);
}
return return_vals;
}
void
register_image_sample_points_procs (GimpPDB *pdb)
{
GimpProcedure *procedure;
/*
* gimp-image-add-sample-point
*/
procedure = gimp_procedure_new (image_add_sample_point_invoker);
gimp_object_set_static_name (GIMP_OBJECT (procedure),
"gimp-image-add-sample-point");
gimp_procedure_set_static_strings (procedure,
"gimp-image-add-sample-point",
"Add a sample point to an image.",
"This procedure adds a sample point to an image. It takes the input image and the position of the new sample points as parameters. It returns the sample point ID of the new sample point.",
"Michael Natterer <mitch@gimp.org>",
"Michael Natterer",
"2016",
NULL);
gimp_procedure_add_argument (procedure,
gimp_param_spec_image_id ("image",
"image",
"The image",
pdb->gimp, FALSE,
GIMP_PARAM_READWRITE));
gimp_procedure_add_argument (procedure,
gimp_param_spec_int32 ("position-x",
"position x",
"The guide'sample points x-offset from left of image",
0, G_MAXINT32, 0,
GIMP_PARAM_READWRITE));
gimp_procedure_add_argument (procedure,
gimp_param_spec_int32 ("position-y",
"position y",
"The guide'sample points y-offset from top of image",
0, G_MAXINT32, 0,
GIMP_PARAM_READWRITE));
gimp_procedure_add_return_value (procedure,
g_param_spec_uint ("sample-point",
"sample point",
"The new sample point",
1, G_MAXUINT32, 1,
GIMP_PARAM_READWRITE));
gimp_pdb_register_procedure (pdb, procedure);
g_object_unref (procedure);
/*
* gimp-image-delete-sample-point
*/
procedure = gimp_procedure_new (image_delete_sample_point_invoker);
gimp_object_set_static_name (GIMP_OBJECT (procedure),
"gimp-image-delete-sample-point");
gimp_procedure_set_static_strings (procedure,
"gimp-image-delete-sample-point",
"Deletes a sample point from an image.",
"This procedure takes an image and a sample point ID as input and removes the specified sample point from the specified image.",
"Michael Natterer <mitch@gimp.org>",
"Michael Natterer",
"2016",
NULL);
gimp_procedure_add_argument (procedure,
gimp_param_spec_image_id ("image",
"image",
"The image",
pdb->gimp, FALSE,
GIMP_PARAM_READWRITE));
gimp_procedure_add_argument (procedure,
g_param_spec_uint ("sample-point",
"sample point",
"The ID of the sample point to be removed",
1, G_MAXUINT32, 1,
GIMP_PARAM_READWRITE));
gimp_pdb_register_procedure (pdb, procedure);
g_object_unref (procedure);
/*
* gimp-image-find-next-sample-point
*/
procedure = gimp_procedure_new (image_find_next_sample_point_invoker);
gimp_object_set_static_name (GIMP_OBJECT (procedure),
"gimp-image-find-next-sample-point");
gimp_procedure_set_static_strings (procedure,
"gimp-image-find-next-sample-point",
"Find next sample point on an image.",
"This procedure takes an image and a sample point ID as input and finds the sample point ID of the successor of the given sample point ID in the image's sample point list. If the supplied sample point ID is 0, the procedure will return the first sample point. The procedure will return 0 if given the final sample point ID as an argument or the image has no sample points.",
"Michael Natterer <mitch@gimp.org>",
"Michael Natterer",
"2016",
NULL);
gimp_procedure_add_argument (procedure,
gimp_param_spec_image_id ("image",
"image",
"The image",
pdb->gimp, FALSE,
GIMP_PARAM_READWRITE));
gimp_procedure_add_argument (procedure,
g_param_spec_uint ("sample-point",
"sample point",
"The ID of the current sample point (0 if first invocation)",
1, G_MAXUINT32, 1,
GIMP_PARAM_READWRITE | GIMP_PARAM_NO_VALIDATE));
gimp_procedure_add_return_value (procedure,
g_param_spec_uint ("next-sample-point",
"next sample point",
"The next sample point's ID",
1, G_MAXUINT32, 1,
GIMP_PARAM_READWRITE));
gimp_pdb_register_procedure (pdb, procedure);
g_object_unref (procedure);
/*
* gimp-image-get-sample-point-position
*/
procedure = gimp_procedure_new (image_get_sample_point_position_invoker);
gimp_object_set_static_name (GIMP_OBJECT (procedure),
"gimp-image-get-sample-point-position");
gimp_procedure_set_static_strings (procedure,
"gimp-image-get-sample-point-position",
"Get position of a sample point on an image.",
"This procedure takes an image and a sample point ID as input and returns the position of the sample point relative to the top and left of the image.",
"Michael Natterer <mitch@gimp.org>",
"Michael Natterer",
"2016",
NULL);
gimp_procedure_add_argument (procedure,
gimp_param_spec_image_id ("image",
"image",
"The image",
pdb->gimp, FALSE,
GIMP_PARAM_READWRITE));
gimp_procedure_add_argument (procedure,
g_param_spec_uint ("sample-point",
"sample point",
"The guide",
1, G_MAXUINT32, 1,
GIMP_PARAM_READWRITE));
gimp_procedure_add_return_value (procedure,
gimp_param_spec_int32 ("position-x",
"position x",
"The sample points's position relative to top of image",
G_MININT32, G_MAXINT32, 0,
GIMP_PARAM_READWRITE));
gimp_procedure_add_return_value (procedure,
gimp_param_spec_int32 ("position-y",
"position y",
"The sample points's position relative to top of image",
G_MININT32, G_MAXINT32, 0,
GIMP_PARAM_READWRITE));
gimp_pdb_register_procedure (pdb, procedure);
g_object_unref (procedure);
}
......@@ -28,7 +28,7 @@
#include "internal-procs.h"
/* 793 procedures registered total */
/* 797 procedures registered total */
void
internal_procs_init (GimpPDB *pdb)
......@@ -63,6 +63,7 @@ internal_procs_init (GimpPDB *pdb)
register_image_convert_procs (pdb);
register_image_grid_procs (pdb);
register_image_guides_procs (pdb);
register_image_sample_points_procs (pdb);
register_image_select_procs (pdb);
register_image_transform_procs (pdb);
register_image_undo_procs (pdb);
......
......@@ -52,6 +52,7 @@ void register_image_color_profile_procs (GimpPDB *pdb);
void register_image_convert_procs (GimpPDB *pdb);
void register_image_grid_procs (GimpPDB *pdb);
void register_image_guides_procs (GimpPDB *pdb);
void register_image_sample_points_procs (GimpPDB *pdb);
void register_image_select_procs (GimpPDB *pdb);
void register_image_transform_procs (GimpPDB *pdb);
void register_image_undo_procs (GimpPDB *pdb);
......
......@@ -107,6 +107,7 @@ PDB_WRAPPERS_C = \
gimpimageconvert_pdb.c \
gimpimagegrid_pdb.c \
gimpimageguides_pdb.c \
gimpimagesamplepoints_pdb.c \
gimpimageselect_pdb.c \
gimpimagetransform_pdb.c \
gimpimageundo_pdb.c \
......@@ -163,6 +164,7 @@ PDB_WRAPPERS_H = \
gimpimageconvert_pdb.h \
gimpimagegrid_pdb.h \
gimpimageguides_pdb.h \
gimpimagesamplepoints_pdb.h \
gimpimageselect_pdb.h \
gimpimagetransform_pdb.h \
gimpimageundo_pdb.h \
......
......@@ -391,6 +391,7 @@ EXPORTS
gimp_image_add_channel
gimp_image_add_hguide
gimp_image_add_layer
gimp_image_add_sample_point
gimp_image_add_vectors
gimp_image_add_vguide
gimp_image_attach_new_parasite
......@@ -407,9 +408,11 @@ EXPORTS
gimp_image_crop
gimp_image_delete
gimp_image_delete_guide
gimp_image_delete_sample_point
gimp_image_detach_parasite
gimp_image_duplicate
gimp_image_find_next_guide
gimp_image_find_next_sample_point
gimp_image_flatten
gimp_image_flip
gimp_image_floating_sel_attached_to
......@@ -445,6 +448,7 @@ EXPORTS
gimp_image_get_parasite_list
gimp_image_get_precision
gimp_image_get_resolution
gimp_image_get_sample_point_position
gimp_image_get_selection
gimp_image_get_tattoo_state
gimp_image_get_thumbnail
......
......@@ -55,6 +55,7 @@
#include <libgimp/gimpimageconvert_pdb.h>
#include <libgimp/gimpimagegrid_pdb.h>
#include <libgimp/gimpimageguides_pdb.h>
#include <libgimp/gimpimagesamplepoints_pdb.h>
#include <libgimp/gimpimageselect_pdb.h>
#include <libgimp/gimpimagetransform_pdb.h>
#include <libgimp/gimpimageundo_pdb.h>
......
/* LIBGIMP - The GIMP Library
* Copyright (C) 1995-2003 Peter Mattis and Spencer Kimball
*
* gimpimagesamplepoints_pdb.c
*
* This library is free software: you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or (at your option) any later version.
*
* This library 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
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library. If not, see
* <http://www.gnu.org/licenses/>.
*/
/* NOTE: This file is auto-generated by pdbgen.pl */
#include "config.h"
#include "gimp.h"