Merge branch 'soc-2010-cage-2'

This commit is contained in:
Michael Muré 2011-03-23 20:07:17 +01:00
commit 2aeac1cf4e
7 changed files with 885 additions and 294 deletions

View File

@ -21,6 +21,8 @@
#include "libgimpmodule/gimpmoduletypes.h"
#include "libgimpthumb/gimpthumb-types.h"
#include "libgimpmath/gimpmathtypes.h"
#include "libgimpmath/gimpvector.h"
#include "base/base-types.h"
@ -174,6 +176,7 @@ typedef struct _GimpTagged GimpTagged; /* dummy typedef */
/* non-object types */
typedef struct _GimpArea GimpArea;
typedef struct _GimpCagePoint GimpCagePoint;
typedef struct _GimpCoords GimpCoords;
typedef struct _GimpGradientSegment GimpGradientSegment;
typedef struct _GimpPaletteEntry GimpPaletteEntry;
@ -201,6 +204,15 @@ typedef void (* GimpImageMapApplyFunc) (gpointer apply_data,
/* structs */
struct _GimpCagePoint
{
GimpVector2 src_point;
GimpVector2 dest_point;
GimpVector2 edge_normal;
gdouble edge_scaling_factor;
gboolean selected;
};
struct _GimpCoords
{
gdouble x;
@ -213,7 +225,6 @@ struct _GimpCoords
gdouble direction;
};
#include "gegl/gimp-gegl-types.h"
#include "paint/paint-types.h"
#include "text/text-types.h"

View File

@ -33,6 +33,9 @@
/*#define DEBUG_CAGE */
#define N_ITEMS_PER_ALLOC 10
/* This DELTA is aimed to not have handle on exact pixel during computation,
* to avoid particular case. It shouldn't be so usefull, but it's a double
* safety. */
#define DELTA 0.010309278351
@ -47,8 +50,7 @@ static void gimp_cage_config_set_property (GObject *object,
GParamSpec *pspec);
static void gimp_cage_config_compute_scaling_factor (GimpCageConfig *gcc);
static void gimp_cage_config_compute_edge_normal (GimpCageConfig *gcc);
static void gimp_cage_config_compute_edges_normal (GimpCageConfig *gcc);
G_DEFINE_TYPE_WITH_CODE (GimpCageConfig, gimp_cage_config,
GIMP_TYPE_IMAGE_MAP_CONFIG,
@ -69,10 +71,18 @@ print_cage (GimpCageConfig *gcc)
for (i = 0; i < gcc->n_cage_vertices; i++)
{
printf("cgx: %.0f cgy: %.0f cvdx: %.0f cvdy: %.0f sf: %.2f normx: %.2f normy: %.2f\n", gcc->cage_vertices[i].x, gcc->cage_vertices[i].y, gcc->cage_vertices_d[i].x, gcc->cage_vertices_d[i].y, gcc->scaling_factor[i], gcc->normal_d[i].x, gcc->normal_d[i].y);
printf("cgx: %.0f cgy: %.0f cvdx: %.0f cvdy: %.0f sf: %.2f normx: %.2f normy: %.2f %s\n",
gcc->cage_points[i].src_point.x + ((gcc->cage_mode==GIMP_CAGE_MODE_CAGE_CHANGE)?gcc->displacement_x:0),
gcc->cage_points[i].src_point.y + ((gcc->cage_mode==GIMP_CAGE_MODE_CAGE_CHANGE)?gcc->displacement_y:0),
gcc->cage_points[i].dest_point.x + ((gcc->cage_mode==GIMP_CAGE_MODE_DEFORM)?gcc->displacement_x:0),
gcc->cage_points[i].dest_point.y + ((gcc->cage_mode==GIMP_CAGE_MODE_DEFORM)?gcc->displacement_y:0),
gcc->cage_points[i].edge_scaling_factor,
gcc->cage_points[i].edge_normal.x,
gcc->cage_points[i].edge_normal.y,
((gcc->cage_points[i].selected) ? "S" : "NS"));
}
printf("bounding box: x: %d y: %d width: %d height: %d\n", bounding_box.x, bounding_box.y, bounding_box.width, bounding_box.height);
printf("offsets: (%d, %d)", gcc->offset_x, gcc->offset_y);
printf("disp x: %f disp y: %f\n", gcc->displacement_x, gcc->displacement_y);
printf("done\n");
}
#endif
@ -94,10 +104,7 @@ gimp_cage_config_init (GimpCageConfig *self)
self->n_cage_vertices = 0;
self->max_cage_vertices = 50; /*pre-allocation for 50 vertices for the cage.*/
self->cage_vertices = g_new0 (GimpVector2, self->max_cage_vertices);
self->cage_vertices_d = g_new0 (GimpVector2, self->max_cage_vertices);
self->scaling_factor = g_malloc0 (self->max_cage_vertices * sizeof (gdouble));
self->normal_d = g_new0 (GimpVector2, self->max_cage_vertices);
self->cage_points = g_new0 (GimpCagePoint, self->max_cage_vertices);
}
static void
@ -105,10 +112,7 @@ gimp_cage_config_finalize (GObject *object)
{
GimpCageConfig *gcc = GIMP_CAGE_CONFIG (object);
g_free (gcc->cage_vertices);
g_free (gcc->cage_vertices_d);
g_free (gcc->scaling_factor);
g_free (gcc->normal_d);
g_free (gcc->cage_points);
G_OBJECT_CLASS (parent_class)->finalize (object);
}
@ -162,32 +166,21 @@ gimp_cage_config_add_cage_point (GimpCageConfig *gcc,
{
gcc->max_cage_vertices += N_ITEMS_PER_ALLOC;
gcc->cage_vertices = g_renew (GimpVector2,
gcc->cage_vertices,
gcc->max_cage_vertices);
gcc->cage_vertices_d = g_renew (GimpVector2,
gcc->cage_vertices_d,
gcc->max_cage_vertices);
gcc->scaling_factor = g_realloc (gcc->scaling_factor,
gcc->max_cage_vertices * sizeof (gdouble));
gcc->normal_d = g_renew (GimpVector2,
gcc->normal_d,
gcc->max_cage_vertices);
gcc->cage_points = g_renew (GimpCagePoint,
gcc->cage_points,
gcc->max_cage_vertices);
}
gcc->cage_vertices[gcc->n_cage_vertices].x = x + DELTA - gcc->offset_x;
gcc->cage_vertices[gcc->n_cage_vertices].y = y + DELTA - gcc->offset_y;
gcc->cage_points[gcc->n_cage_vertices].src_point.x = x + DELTA;
gcc->cage_points[gcc->n_cage_vertices].src_point.y = y + DELTA;
gcc->cage_vertices_d[gcc->n_cage_vertices].x = x + DELTA - gcc->offset_x;
gcc->cage_vertices_d[gcc->n_cage_vertices].y = y + DELTA - gcc->offset_y;
gcc->cage_points[gcc->n_cage_vertices].dest_point.x = x + DELTA;
gcc->cage_points[gcc->n_cage_vertices].dest_point.y = y + DELTA;
gcc->n_cage_vertices++;
gimp_cage_config_compute_scaling_factor (gcc);
gimp_cage_config_compute_edge_normal (gcc);
gimp_cage_config_compute_edges_normal (gcc);
}
/**
@ -205,55 +198,144 @@ gimp_cage_config_remove_last_cage_point (GimpCageConfig *gcc)
gcc->n_cage_vertices--;
gimp_cage_config_compute_scaling_factor (gcc);
gimp_cage_config_compute_edge_normal (gcc);
gimp_cage_config_compute_edges_normal (gcc);
}
/**
* gimp_cage_config_move_cage_point:
* gimp_cage_config_get_point_coordinate:
* @gcc: the cage config
* @mode: the actual mode of the cage, GIMP_CAGE_MODE_CAGE_CHANGE or GIMP_CAGE_MODE_DEFORM
* @point_number: the point of the cage to move
* @x: new x value
* @y: new y value
* @point_number: the index of the point to return
*
* Move a point of the source or destination cage, according to the
* cage mode provided
* Returns: the real position of the given point, as a GimpVector2
*/
void
gimp_cage_config_move_cage_point (GimpCageConfig *gcc,
GimpCageMode mode,
gint point_number,
gdouble x,
gdouble y)
GimpVector2
gimp_cage_config_get_point_coordinate (GimpCageConfig *gcc,
GimpCageMode mode,
gint point_number)
{
g_return_if_fail (GIMP_IS_CAGE_CONFIG (gcc));
g_return_if_fail (point_number < gcc->n_cage_vertices);
g_return_if_fail (point_number >= 0);
GimpVector2 point = {0.0, 0.0};
if (mode == GIMP_CAGE_MODE_CAGE_CHANGE)
g_return_val_if_fail (GIMP_IS_CAGE_CONFIG (gcc), point);
g_return_val_if_fail (point_number < gcc->n_cage_vertices, point);
g_return_val_if_fail (point_number >= 0, point);
if (gcc->cage_points[point_number].selected)
{
gcc->cage_vertices[point_number].x = x + DELTA - gcc->offset_x;
gcc->cage_vertices[point_number].y = y + DELTA - gcc->offset_y;
gcc->cage_vertices_d[point_number].x = x + DELTA - gcc->offset_x;
gcc->cage_vertices_d[point_number].y = y + DELTA - gcc->offset_y;
if (mode == GIMP_CAGE_MODE_CAGE_CHANGE)
{
point.x = gcc->cage_points[point_number].src_point.x + gcc->displacement_x;
point.y = gcc->cage_points[point_number].src_point.y + gcc->displacement_y;
}
else
{
point.x = gcc->cage_points[point_number].dest_point.x + gcc->displacement_x;
point.y = gcc->cage_points[point_number].dest_point.y + gcc->displacement_y;
}
}
else
{
gcc->cage_vertices_d[point_number].x = x + DELTA - gcc->offset_x;
gcc->cage_vertices_d[point_number].y = y + DELTA - gcc->offset_y;
if (mode == GIMP_CAGE_MODE_CAGE_CHANGE)
{
point.x = gcc->cage_points[point_number].src_point.x;
point.y = gcc->cage_points[point_number].src_point.y;
}
else
{
point.x = gcc->cage_points[point_number].dest_point.x;
point.y = gcc->cage_points[point_number].dest_point.y;
}
}
return point;
}
/**
* gimp_cage_config_add_displacement:
* @gcc: the cage config
* @mode: the actual mode of the cage, GIMP_CAGE_MODE_CAGE_CHANGE or GIMP_CAGE_MODE_DEFORM
* @point_number: the point of the cage to move
* @x: x displacement value
* @y: y displacement value
*
* Add a displacement for all slected point of the cage.
* This displacement need to be commited to become effective.
*/
void
gimp_cage_config_add_displacement (GimpCageConfig *gcc,
GimpCageMode mode,
gdouble x,
gdouble y)
{
g_return_if_fail (GIMP_IS_CAGE_CONFIG (gcc));
gcc->cage_mode = mode;
gcc->displacement_x = x;
gcc->displacement_y = y;
#ifdef DEBUG_CAGE
print_cage (gcc);
#endif
}
/**
* gimp_cage_config_commit_displacement:
* @gcc: the cage config
*
* Apply the displacement to the cage
*/
void
gimp_cage_config_commit_displacement (GimpCageConfig *gcc)
{
gint i;
g_return_if_fail (GIMP_IS_CAGE_CONFIG (gcc));
for (i = 0; i < gcc->n_cage_vertices; i++)
{
if (gcc->cage_points[i].selected)
{
if (gcc->cage_mode == GIMP_CAGE_MODE_CAGE_CHANGE)
{
gcc->cage_points[i].src_point.x += gcc->displacement_x;
gcc->cage_points[i].src_point.y += gcc->displacement_y;
gcc->cage_points[i].dest_point.x += gcc->displacement_x;
gcc->cage_points[i].dest_point.y += gcc->displacement_y;
}
else
{
gcc->cage_points[i].dest_point.x += gcc->displacement_x;
gcc->cage_points[i].dest_point.y += gcc->displacement_y;
}
}
}
gimp_cage_config_compute_scaling_factor (gcc);
gimp_cage_config_compute_edge_normal (gcc);
gimp_cage_config_compute_edges_normal (gcc);
gimp_cage_config_reset_displacement (gcc);
}
/**
* gimp_cage_config_reset_displacement:
* @gcc: the cage config
*
* Set the displacement to zero.
*/
void
gimp_cage_config_reset_displacement (GimpCageConfig *gcc)
{
g_return_if_fail (GIMP_IS_CAGE_CONFIG (gcc));
gcc->displacement_x = 0.0;
gcc->displacement_y = 0.0;
}
/**
* gimp_cage_config_get_bounding_box:
* @gcc: the cage config
*
* Compute the bounding box of the destination cage
* Compute the bounding box of the source cage
*
* Returns: the bounding box of the destination cage, as a GeglRectangle
* Returns: the bounding box of the source cage, as a GeglRectangle
*/
GeglRectangle
gimp_cage_config_get_bounding_box (GimpCageConfig *gcc)
@ -264,8 +346,17 @@ gimp_cage_config_get_bounding_box (GimpCageConfig *gcc)
g_return_val_if_fail (GIMP_IS_CAGE_CONFIG (gcc), bounding_box);
g_return_val_if_fail (gcc->n_cage_vertices >= 0, bounding_box);
bounding_box.x = gcc->cage_vertices[0].x;
bounding_box.y = gcc->cage_vertices[0].y;
if (gcc->cage_points[0].selected)
{
bounding_box.x = gcc->cage_points[0].src_point.x + gcc->displacement_x;
bounding_box.y = gcc->cage_points[0].src_point.y + gcc->displacement_y;
}
else
{
bounding_box.x = gcc->cage_points[0].src_point.x;
bounding_box.y = gcc->cage_points[0].src_point.y;
}
bounding_box.height = 0;
bounding_box.width = 0;
@ -273,8 +364,16 @@ gimp_cage_config_get_bounding_box (GimpCageConfig *gcc)
{
gdouble x,y;
x = gcc->cage_vertices[i].x;
y = gcc->cage_vertices[i].y;
if (gcc->cage_points[i].selected)
{
x = gcc->cage_points[i].src_point.x + gcc->displacement_x;
y = gcc->cage_points[i].src_point.y + gcc->displacement_y;
}
else
{
x = gcc->cage_points[i].src_point.x;
y = gcc->cage_points[i].src_point.y;
}
if (x < bounding_box.x)
{
@ -313,24 +412,20 @@ gimp_cage_config_get_bounding_box (GimpCageConfig *gcc)
void
gimp_cage_config_reverse_cage (GimpCageConfig *gcc)
{
GimpVector2 temp;
gint i;
GimpCagePoint temp;
gint i;
g_return_if_fail (GIMP_IS_CAGE_CONFIG (gcc));
for (i = 0; i < gcc->n_cage_vertices / 2; i++)
{
temp = gcc->cage_vertices[i];
gcc->cage_vertices[i] = gcc->cage_vertices[gcc->n_cage_vertices - i - 1];
gcc->cage_vertices[gcc->n_cage_vertices - i - 1] = temp;
temp = gcc->cage_vertices_d[i];
gcc->cage_vertices_d[i] = gcc->cage_vertices_d[gcc->n_cage_vertices - i - 1];
gcc->cage_vertices_d[gcc->n_cage_vertices - i - 1] = temp;
temp = gcc->cage_points[i];
gcc->cage_points[i] = gcc->cage_points[gcc->n_cage_vertices - i - 1];
gcc->cage_points[gcc->n_cage_vertices - i - 1] = temp;
}
gimp_cage_config_compute_scaling_factor (gcc);
gimp_cage_config_compute_edge_normal (gcc);
gimp_cage_config_compute_edges_normal (gcc);
}
/**
@ -341,6 +436,8 @@ gimp_cage_config_reverse_cage (GimpCageConfig *gcc)
* topological inside in the actual 'physical' inside of the cage,
* this function compute if the cage is clockwise or not, and reverse
* the cage if needed.
*
* This function does not take into account an eventual displacement
*/
void
gimp_cage_config_reverse_cage_if_needed (GimpCageConfig *gcc)
@ -360,9 +457,9 @@ gimp_cage_config_reverse_cage_if_needed (GimpCageConfig *gcc)
GimpVector2 P1, P2, P3;
gdouble z;
P1 = gcc->cage_vertices[i];
P2 = gcc->cage_vertices[(i+1) % gcc->n_cage_vertices];
P3 = gcc->cage_vertices[(i+2) % gcc->n_cage_vertices];
P1 = gcc->cage_points[i].src_point;
P2 = gcc->cage_points[(i+1) % gcc->n_cage_vertices].src_point;
P3 = gcc->cage_points[(i+2) % gcc->n_cage_vertices].src_point;
z = P1.x * (P2.y - P3.y) + P2.x * (P3.y - P1.y) + P3.x * (P1.y - P2.y);
@ -377,15 +474,11 @@ gimp_cage_config_reverse_cage_if_needed (GimpCageConfig *gcc)
}
/**
* gimp_cage_config_point_inside:
* gimp_cage_config_compute_scaling_factor:
* @gcc: the cage config
* @x: x coordinate of the point to test
* @y: y coordinate of the point to test
*
* Check if the given point is inside the cage. This test is done in
* the regard of the topological inside of the cage.
*
* Returns: TRUE if the point is inside, FALSE if not.
* Update Green Coordinate scaling factor for the destination cage.
* This function does not take into account an eventual displacement.
*/
static void
gimp_cage_config_compute_scaling_factor (GimpCageConfig *gcc)
@ -399,25 +492,28 @@ gimp_cage_config_compute_scaling_factor (GimpCageConfig *gcc)
for (i = 0; i < gcc->n_cage_vertices; i++)
{
gimp_vector2_sub (&edge,
&gcc->cage_vertices[i],
&gcc->cage_vertices[(i + 1) % gcc->n_cage_vertices]);
&gcc->cage_points[i].src_point,
&gcc->cage_points[(i + 1) % gcc->n_cage_vertices].src_point);
length = gimp_vector2_length (&edge);
gimp_vector2_sub (&edge,
&gcc->cage_vertices_d[i],
&gcc->cage_vertices_d[(i + 1) % gcc->n_cage_vertices]);
&gcc->cage_points[i].dest_point,
&gcc->cage_points[(i + 1) % gcc->n_cage_vertices].dest_point);
length_d = gimp_vector2_length (&edge);
gcc->scaling_factor[i] = length_d / length;
gcc->cage_points[i].edge_scaling_factor = length_d / length;
}
#ifdef DEBUG_CAGE
print_cage (gcc);
#endif
}
/**
* gimp_cage_config_compute_edges_normal:
* @gcc: the cage config
*
* Update edges normal for the destination cage.
* This function does not take into account an eventual displacement.
*/
static void
gimp_cage_config_compute_edge_normal (GimpCageConfig *gcc)
gimp_cage_config_compute_edges_normal (GimpCageConfig *gcc)
{
GimpVector2 normal;
gint i;
@ -427,33 +523,46 @@ gimp_cage_config_compute_edge_normal (GimpCageConfig *gcc)
for (i = 0; i < gcc->n_cage_vertices; i++)
{
gimp_vector2_sub (&normal,
&gcc->cage_vertices_d[(i + 1) % gcc->n_cage_vertices],
&gcc->cage_vertices_d[i]);
&gcc->cage_points[(i + 1) % gcc->n_cage_vertices].dest_point,
&gcc->cage_points[i].dest_point);
gcc->normal_d[i] = gimp_vector2_normal (&normal);
gcc->cage_points[i].edge_normal = gimp_vector2_normal (&normal);
}
}
/**
* gimp_cage_config_point_inside:
* @gcc: the cage config
* @x: x coordinate of the point to test
* @y: y coordinate of the point to test
*
* Check if the given point is inside the cage. This test is done in
* the regard of the topological inside of the source cage.
*
* Returns: TRUE if the point is inside, FALSE if not.
* This function does not take into account an eventual displacement.
*/
gboolean
gimp_cage_config_point_inside (GimpCageConfig *gcc,
gfloat x,
gfloat y)
{
GimpVector2 *cv;
GimpVector2 *cpi, *cpj;
gboolean inside = FALSE;
gint i, j;
g_return_val_if_fail (GIMP_IS_CAGE_CONFIG (gcc), FALSE);
cv = gcc->cage_vertices;
for (i = 0, j = gcc->n_cage_vertices - 1;
i < gcc->n_cage_vertices;
j = i++)
{
if ((((cv[i].y <= y) && (y < cv[j].y))
|| ((cv[j].y <= y) && (y < cv[i].y)))
&& (x < (cv[j].x - cv[i].x) * (y - cv[i].y) / (cv[j].y - cv[i].y) + cv[i].x))
cpi = &(gcc->cage_points[i].src_point);
cpj = &(gcc->cage_points[j].src_point);
if ((((cpi->y <= y) && (y < cpj->y))
|| ((cpj->y <= y) && (y < cpi->y)))
&& (x < (cpj->x - cpi->x) * (y - cpi->y) / (cpj->y - cpi->y) + cpi->x))
{
inside = !inside;
}
@ -461,3 +570,131 @@ gimp_cage_config_point_inside (GimpCageConfig *gcc,
return inside;
}
/**
* gimp_cage_config_select_point:
* @gcc: the cage config
* @point_number: the index of the point to select
*
* Select the given point of the cage, and deselect the others.
*/
void
gimp_cage_config_select_point (GimpCageConfig *gcc,
gint point_number)
{
gint i;
g_return_if_fail (GIMP_IS_CAGE_CONFIG (gcc));
g_return_if_fail (point_number < gcc->n_cage_vertices);
g_return_if_fail (point_number >= 0);
for (i = 0; i < gcc->n_cage_vertices; i++)
{
if (i == point_number)
{
gcc->cage_points[i].selected = TRUE;
}
else
{
gcc->cage_points[i].selected = FALSE;
}
}
}
/**
* gimp_cage_config_select_area:
* @gcc: the cage config
* @mode: the actual mode of the cage, GIMP_CAGE_MODE_CAGE_CHANGE or GIMP_CAGE_MODE_DEFORM
* @area: the area to select
*
* Select cage's point inside the given area and deselect others
*/
void
gimp_cage_config_select_area (GimpCageConfig *gcc,
GimpCageMode mode,
GeglRectangle area)
{
g_return_if_fail (GIMP_IS_CAGE_CONFIG (gcc));
gimp_cage_config_deselect_points (gcc);
gimp_cage_config_select_add_area (gcc, mode, area);
}
/**
* gimp_cage_config_select_add_area:
* @gcc: the cage config
* @mode: the actual mode of the cage, GIMP_CAGE_MODE_CAGE_CHANGE or GIMP_CAGE_MODE_DEFORM
* @area: the area to select
*
* Select cage's point inside the given area. Already selected point stay selected.
*/
void
gimp_cage_config_select_add_area (GimpCageConfig *gcc,
GimpCageMode mode,
GeglRectangle area)
{
gint i;
g_return_if_fail (GIMP_IS_CAGE_CONFIG (gcc));
for (i = 0; i < gcc->n_cage_vertices; i++)
{
if (mode == GIMP_CAGE_MODE_CAGE_CHANGE)
{
if (gcc->cage_points[i].src_point.x >= area.x &&
gcc->cage_points[i].src_point.x <= area.x + area.width &&
gcc->cage_points[i].src_point.y >= area.y &&
gcc->cage_points[i].src_point.y <= area.y + area.height)
{
gcc->cage_points[i].selected = TRUE;
}
}
else
{
if (gcc->cage_points[i].dest_point.x >= area.x &&
gcc->cage_points[i].dest_point.x <= area.x + area.width &&
gcc->cage_points[i].dest_point.y >= area.y &&
gcc->cage_points[i].dest_point.y <= area.y + area.height)
{
gcc->cage_points[i].selected = TRUE;
}
}
}
}
/**
* gimp_cage_config_toggle_point_selection:
* @gcc: the cage config
* @point_number: the index of the point to toggle selection
*
* Toggle the selection of the given cage point
*/
void
gimp_cage_config_toggle_point_selection (GimpCageConfig *gcc,
gint point_number)
{
g_return_if_fail (GIMP_IS_CAGE_CONFIG (gcc));
g_return_if_fail (point_number < gcc->n_cage_vertices);
g_return_if_fail (point_number >= 0);
gcc->cage_points[point_number].selected = ! gcc->cage_points[point_number].selected;
}
/**
* gimp_cage_deselect_points:
* @gcc: the cage config
*
* Deselect all cage points.
*/
void
gimp_cage_config_deselect_points (GimpCageConfig *gcc)
{
gint i;
g_return_if_fail (GIMP_IS_CAGE_CONFIG (gcc));
for (i = 0; i < gcc->n_cage_vertices; i++)
{
gcc->cage_points[i].selected = FALSE;
}
}

View File

@ -41,13 +41,11 @@ struct _GimpCageConfig
gint n_cage_vertices; /* vertices used by the cage */
gint max_cage_vertices; /* vertices allocated */
gint offset_x;
gint offset_y;
gdouble displacement_x;
gdouble displacement_y;
GimpCageMode cage_mode; /* Cage mode, used to commit displacement */
GimpVector2 *cage_vertices; /* cage before deformation */
GimpVector2 *cage_vertices_d; /* cage after deformation */
gdouble *scaling_factor;
GimpVector2 *normal_d;
GimpCagePoint *cage_points;
};
struct _GimpCageConfigClass
@ -55,23 +53,37 @@ struct _GimpCageConfigClass
GimpImageMapConfigClass parent_class;
};
GType gimp_cage_config_get_type (void) G_GNUC_CONST;
void gimp_cage_config_add_cage_point (GimpCageConfig *gcc,
gdouble x,
gdouble y);
void gimp_cage_config_remove_last_cage_point (GimpCageConfig *gcc);
void gimp_cage_config_move_cage_point (GimpCageConfig *gcc,
GimpCageMode mode,
gint point_number,
gdouble x,
gdouble y);
GeglRectangle gimp_cage_config_get_bounding_box (GimpCageConfig *gcc);
void gimp_cage_config_reverse_cage_if_needed (GimpCageConfig *gcc);
void gimp_cage_config_reverse_cage (GimpCageConfig *gcc);
gboolean gimp_cage_config_point_inside (GimpCageConfig *gcc,
gfloat x,
gfloat y);
GType gimp_cage_config_get_type (void) G_GNUC_CONST;
void gimp_cage_config_add_cage_point (GimpCageConfig *gcc,
gdouble x,
gdouble y);
void gimp_cage_config_remove_last_cage_point (GimpCageConfig *gcc);
GimpVector2 gimp_cage_config_get_point_coordinate (GimpCageConfig *gcc,
GimpCageMode mode,
gint point_number);
void gimp_cage_config_add_displacement (GimpCageConfig *gcc,
GimpCageMode mode,
gdouble x,
gdouble y);
void gimp_cage_config_commit_displacement (GimpCageConfig *gcc);
void gimp_cage_config_reset_displacement (GimpCageConfig *gcc);
GeglRectangle gimp_cage_config_get_bounding_box (GimpCageConfig *gcc);
void gimp_cage_config_reverse_cage_if_needed (GimpCageConfig *gcc);
void gimp_cage_config_reverse_cage (GimpCageConfig *gcc);
gboolean gimp_cage_config_point_inside (GimpCageConfig *gcc,
gfloat x,
gfloat y);
void gimp_cage_config_select_point (GimpCageConfig *gcc,
gint point_number);
void gimp_cage_config_select_area (GimpCageConfig *gcc,
GimpCageMode mode,
GeglRectangle area);
void gimp_cage_config_select_add_area (GimpCageConfig *gcc,
GimpCageMode mode,
GeglRectangle area);
void gimp_cage_config_toggle_point_selection (GimpCageConfig *gcc,
gint point_number);
void gimp_cage_config_deselect_points (GimpCageConfig *gcc);
#endif /* __GIMP_CAGE_CONFIG_H__ */

View File

@ -221,8 +221,8 @@ gimp_operation_cage_coef_calc_process (GeglOperation *operation,
GimpVector2 v1,v2,a,b,p;
gdouble BA,SRT,L0,L1,A0,A1,A10,L10, Q,S,R, absa;
v1 = config->cage_vertices[j];
v2 = config->cage_vertices[(j+1)%config->n_cage_vertices];
v1 = config->cage_points[j].src_point;
v2 = config->cage_points[(j+1)%config->n_cage_vertices].src_point;
p.x = x;
p.y = y;
a.x = v2.x - v1.x;

View File

@ -220,8 +220,8 @@ gimp_operation_cage_transform_process (GeglOperation *operation,
/* pre-fill the out buffer with no-displacement coordinate */
it = gegl_buffer_iterator_new (out_buf, roi, NULL, GEGL_BUFFER_WRITE);
plain_color.x = (gint) config->cage_vertices[0].x;
plain_color.y = (gint) config->cage_vertices[0].y;
plain_color.x = (gint) config->cage_points[0].src_point.x;
plain_color.y = (gint) config->cage_points[0].src_point.y;
while (gegl_buffer_iterator_next (it))
{
@ -507,14 +507,14 @@ gimp_cage_transform_compute_destination (GimpCageConfig *config,
for (i = 0; i < cvn; i++)
{
pos_x += coef[i] * config->cage_vertices_d[i].x;
pos_y += coef[i] * config->cage_vertices_d[i].y;
pos_x += coef[i] * config->cage_points[i].dest_point.x;
pos_y += coef[i] * config->cage_points[i].dest_point.y;
}
for (i = 0; i < cvn; i++)
{
pos_x += coef[i + cvn] * config->scaling_factor[i] * config->normal_d[i].x;
pos_y += coef[i + cvn] * config->scaling_factor[i] * config->normal_d[i].y;
pos_x += coef[i + cvn] * config->cage_points[i].edge_scaling_factor * config->cage_points[i].edge_normal.x;
pos_y += coef[i + cvn] * config->cage_points[i].edge_scaling_factor * config->cage_points[i].edge_normal.y;
}
result.x = pos_x;

View File

@ -20,6 +20,7 @@
#include "config.h"
#include <string.h>
#include <stdlib.h>
#include <gegl.h>
#include <gtk/gtk.h>
@ -64,6 +65,9 @@ static void gimp_cage_tool_finalize (GObject *obje
static void gimp_cage_tool_start (GimpCageTool *ct,
GimpDisplay *display);
static void gimp_cage_tool_halt (GimpCageTool *ct);
static void gimp_cage_tool_options_notify (GimpTool *tool,
GimpToolOptions *options,
const GParamSpec *pspec);
static void gimp_cage_tool_button_press (GimpTool *tool,
const GimpCoords *coords,
guint32 time,
@ -99,15 +103,13 @@ static void gimp_cage_tool_oper_update (GimpTool *tool
static void gimp_cage_tool_draw (GimpDrawTool *draw_tool);
static gint gimp_cage_tool_is_on_handle (GimpCageConfig *gcc,
static gint gimp_cage_tool_is_on_handle (GimpCageTool *ct,
GimpDrawTool *draw_tool,
GimpDisplay *display,
GimpCageMode mode,
gdouble x,
gdouble y,
gint handle_size);
static void gimp_cage_tool_switch_to_deform (GimpCageTool *ct);
static void gimp_cage_tool_remove_last_handle (GimpCageTool *ct);
static void gimp_cage_tool_compute_coef (GimpCageTool *ct,
GimpDisplay *display);
@ -115,14 +117,26 @@ static void gimp_cage_tool_create_image_map (GimpCageTool *ct,
GimpDrawable *drawable);
static void gimp_cage_tool_image_map_flush (GimpImageMap *image_map,
GimpTool *tool);
static void gimp_cage_tool_image_map_update (GimpCageTool *ct);
static GeglNode * gimp_cage_tool_create_render_node (GimpCageTool *ct);
static void gimp_cage_tool_create_render_node (GimpCageTool *ct);
static void gimp_cage_tool_render_node_update (GimpCageTool *ct);
G_DEFINE_TYPE (GimpCageTool, gimp_cage_tool, GIMP_TYPE_DRAW_TOOL)
#define parent_class gimp_cage_tool_parent_class
enum
{
CAGE_STATE_INIT,
CAGE_STATE_WAIT,
CAGE_STATE_MOVE_HANDLE,
CAGE_STATE_SELECTING,
CAGE_STATE_CLOSING,
DEFORM_STATE_WAIT,
DEFORM_STATE_MOVE_HANDLE,
DEFORM_STATE_SELECTING
};
void
gimp_cage_tool_register (GimpToolRegisterCallback callback,
@ -150,6 +164,7 @@ gimp_cage_tool_class_init (GimpCageToolClass *klass)
object_class->finalize = gimp_cage_tool_finalize;
tool_class->options_notify = gimp_cage_tool_options_notify;
tool_class->button_press = gimp_cage_tool_button_press;
tool_class->button_release = gimp_cage_tool_button_release;
tool_class->key_press = gimp_cage_tool_key_press;
@ -177,11 +192,18 @@ gimp_cage_tool_init (GimpCageTool *self)
self->config = g_object_new (GIMP_TYPE_CAGE_CONFIG, NULL);
self->hovering_handle = -1;
self->moving_handle = -1;
self->cage_complete = FALSE;
self->tool_state = CAGE_STATE_INIT;
self->coef = NULL;
self->render_node = NULL;
self->coef_node = NULL;
self->cage_node = NULL;
self->image_map = NULL;
gimp_tool_control_set_wants_click (tool->control, TRUE);
gimp_tool_control_set_tool_cursor (tool->control,
GIMP_TOOL_CURSOR_PERSPECTIVE);
}
static void
@ -233,6 +255,13 @@ gimp_cage_tool_start (GimpCageTool *ct,
ct->config = NULL;
}
if (ct->coef)
{
gegl_buffer_destroy (ct->coef);
ct->dirty_coef = TRUE;
ct->coef = NULL;
}
if (ct->image_map)
{
gimp_image_map_abort (ct->image_map);
@ -240,22 +269,95 @@ gimp_cage_tool_start (GimpCageTool *ct,
ct->image_map = NULL;
}
if (ct->render_node)
{
g_object_unref (ct->render_node);
ct->render_node = NULL;
ct->coef_node = NULL;
ct->cage_node = NULL;
}
ct->config = g_object_new (GIMP_TYPE_CAGE_CONFIG, NULL);
ct->hovering_handle = -1;
ct->moving_handle = -1;
ct->cage_complete = FALSE;
ct->tool_state = CAGE_STATE_INIT;
/* Setting up cage offset to convert the cage point coords to
* drawable coords
*/
gimp_item_get_offset (GIMP_ITEM (drawable), &off_x, &off_y);
ct->config->offset_x = off_x;
ct->config->offset_y = off_y;
ct->offset_x = off_x;
ct->offset_y = off_y;
gimp_draw_tool_start (GIMP_DRAW_TOOL (ct), display);
}
static void
gimp_cage_tool_options_notify (GimpTool *tool,
GimpToolOptions *options,
const GParamSpec *pspec)
{
GimpCageTool *ct = GIMP_CAGE_TOOL (tool);
GIMP_TOOL_CLASS (parent_class)->options_notify (tool, options, pspec);
gimp_draw_tool_pause (GIMP_DRAW_TOOL (tool));
if (strcmp (pspec->name, "cage-mode") == 0)
{
GimpCageMode mode;
g_object_get (options,
"cage-mode", &mode,
NULL);
if (mode == GIMP_CAGE_MODE_DEFORM)
/* switch to deform mode */
{
ct->cage_complete = TRUE;
gimp_cage_config_reset_displacement (ct->config);
gimp_cage_config_reverse_cage_if_needed (ct->config);
gimp_tool_push_status (tool, tool->display, _("Press ENTER to commit the transform"));
ct->tool_state = DEFORM_STATE_WAIT;
if (ct->dirty_coef)
{
gimp_cage_tool_compute_coef (ct, tool->display);
}
if (!ct->render_node)
{
gimp_cage_tool_create_render_node (ct);
}
if (!ct->image_map)
{
GimpImage *image = gimp_display_get_image (tool->display);
GimpDrawable *drawable = gimp_image_get_active_drawable (image);
gimp_cage_tool_create_image_map (ct, drawable);
}
gimp_cage_tool_image_map_update (ct);
}
else
/* switch to edit mode */
{
gimp_image_map_clear (ct->image_map);
gimp_image_flush (gimp_display_get_image (tool->display));
gimp_tool_pop_status (tool, tool->display);
ct->tool_state = CAGE_STATE_WAIT;
}
}
else if (strcmp (pspec->name, "fill-plain-color") == 0)
{
gimp_cage_tool_render_node_update (ct);
gimp_cage_tool_image_map_update (ct);
}
gimp_draw_tool_resume (GIMP_DRAW_TOOL (tool));
}
static gboolean
gimp_cage_tool_key_press (GimpTool *tool,
GdkEventKey *kevent,
@ -266,14 +368,14 @@ gimp_cage_tool_key_press (GimpTool *tool,
switch (kevent->keyval)
{
case GDK_BackSpace:
if (! ct->cage_complete)
if (! ct->cage_complete && ct->tool_state == CAGE_STATE_WAIT)
gimp_cage_tool_remove_last_handle (ct);
return TRUE;
case GDK_Return:
case GDK_KP_Enter:
case GDK_ISO_Enter:
if (ct->cage_complete)
if (ct->tool_state == DEFORM_STATE_WAIT)
{
gimp_tool_control_set_preserve (tool->control, TRUE);
@ -307,13 +409,26 @@ gimp_cage_tool_motion (GimpTool *tool,
GdkModifierType state,
GimpDisplay *display)
{
GimpCageTool *ct = GIMP_CAGE_TOOL (tool);
GimpCageTool *ct = GIMP_CAGE_TOOL (tool);
GimpCageOptions *options = GIMP_CAGE_TOOL_GET_OPTIONS (ct);
gimp_draw_tool_pause (GIMP_DRAW_TOOL (tool));
ct->cursor_x = coords->x;
ct->cursor_y = coords->y;
switch (ct->tool_state)
{
case CAGE_STATE_MOVE_HANDLE:
case CAGE_STATE_CLOSING:
case DEFORM_STATE_MOVE_HANDLE:
gimp_cage_config_add_displacement (ct->config,
options->cage_mode,
ct->cursor_x - ct->movement_start_x,
ct->cursor_y - ct->movement_start_y);
break;
}
gimp_draw_tool_resume (GIMP_DRAW_TOOL (tool));
}
@ -326,15 +441,13 @@ gimp_cage_tool_oper_update (GimpTool *tool,
{
GimpCageTool *ct = GIMP_CAGE_TOOL (tool);
GimpDrawTool *draw_tool = GIMP_DRAW_TOOL (tool);
GimpCageOptions *options = GIMP_CAGE_TOOL_GET_OPTIONS (ct);
GimpCageConfig *config = ct->config;
gint handle = -1;
if (config)
handle = gimp_cage_tool_is_on_handle (config,
handle = gimp_cage_tool_is_on_handle (ct,
draw_tool,
display,
options->cage_mode,
coords->x,
coords->y,
GIMP_TOOL_HANDLE_SIZE_CIRCLE);
@ -356,12 +469,129 @@ gimp_cage_tool_button_press (GimpTool *tool,
GimpButtonPressType press_type,
GimpDisplay *display)
{
GimpCageTool *ct = GIMP_CAGE_TOOL (tool);
GimpCageTool *ct = GIMP_CAGE_TOOL (tool);
GimpDrawTool *draw_tool = GIMP_DRAW_TOOL (tool);
gint handle = -1;
if (display != tool->display)
gimp_cage_tool_start (ct, display);
ct->moving_handle = ct->hovering_handle;
if (ct->config)
handle = gimp_cage_tool_is_on_handle (ct,
draw_tool,
display,
coords->x,
coords->y,
GIMP_TOOL_HANDLE_SIZE_CIRCLE);
ct->movement_start_x = coords->x;
ct->movement_start_y = coords->y;
switch (ct->tool_state)
{
case CAGE_STATE_INIT:
/* No handle yet, we add the first one and swith the tool to moving handle state. */
gimp_cage_config_add_cage_point (ct->config,
coords->x - ct->offset_x,
coords->y - ct->offset_y);
gimp_cage_config_select_point (ct->config, 0);
ct->tool_state = CAGE_STATE_MOVE_HANDLE;
break;
case CAGE_STATE_WAIT:
if (ct->cage_complete == FALSE)
{
if (handle == -1)
/* User clicked on the background, we add a new handle and move it */
{
gimp_cage_config_add_cage_point (ct->config,
coords->x - ct->offset_x,
coords->y - ct->offset_y);
gimp_cage_config_select_point (ct->config, ct->config->n_cage_vertices - 1);
ct->tool_state = CAGE_STATE_MOVE_HANDLE;
}
if (handle == 0 && ct->config->n_cage_vertices > 2)
/* User clicked on the first handle, we wait for release for closing the cage and switching to deform if possible */
{
gimp_cage_config_select_point (ct->config, 0);
ct->tool_state = CAGE_STATE_CLOSING;
}
if (handle > 0)
/* User clicked on a handle, so we move it */
{
if (state & GDK_SHIFT_MASK)
/* Multiple selection */
{
gimp_cage_config_toggle_point_selection (ct->config, handle);
}
else
/* New selection */
{
if (! ct->config->cage_points[handle].selected)
{
gimp_cage_config_select_point (ct->config, handle);
}
}
ct->tool_state = CAGE_STATE_MOVE_HANDLE;
}
}
else /* Cage already closed */
{
if (handle == -1)
/* User clicked on the background, we start a rubber band selection */
{
ct->selection_start_x = coords->x;
ct->selection_start_y = coords->y;
ct->tool_state = CAGE_STATE_SELECTING;
}
if (handle >= 0)
/* User clicked on a handle, so we move it */
{
if (state & GDK_SHIFT_MASK)
/* Multiple selection */
{
gimp_cage_config_toggle_point_selection (ct->config, handle);
}
else
/* New selection */
{
if (! ct->config->cage_points[handle].selected)
{
gimp_cage_config_select_point (ct->config, handle);
}
}
ct->tool_state = CAGE_STATE_MOVE_HANDLE;
}
}
break;
case DEFORM_STATE_WAIT:
if (handle == -1)
/* User clicked on the background, we start a rubber band selection */
{
ct->selection_start_x = coords->x;
ct->selection_start_y = coords->y;
ct->tool_state = DEFORM_STATE_SELECTING;
}
if (handle >= 0)
/* User clicked on a handle, so we move it */
{
if (state & GDK_SHIFT_MASK)
/* Multiple selection */
{
gimp_cage_config_toggle_point_selection (ct->config, handle);
}
else
/* New selection */
{
if (! ct->config->cage_points[handle].selected)
{
gimp_cage_config_select_point (ct->config, handle);
}
}
ct->tool_state = DEFORM_STATE_MOVE_HANDLE;
}
break;
}
}
void
@ -377,84 +607,98 @@ gimp_cage_tool_button_release (GimpTool *tool,
gimp_draw_tool_pause (GIMP_DRAW_TOOL (ct));
if (release_type == GIMP_BUTTON_RELEASE_CANCEL)
if (state & GDK_BUTTON3_MASK)
/* Cancelling */
{
/* operation canceled, do nothing */
}
else if (ct->moving_handle == 0 && release_type == GIMP_BUTTON_RELEASE_CLICK)
{
/* user clicked on the first handle, we close the cage and
* switch to deform mode
*/
if (ct->config->n_cage_vertices > 2 && ! ct->cage_complete)
switch(ct->tool_state)
{
GimpImage *image = gimp_display_get_image (display);
GimpDrawable *drawable = gimp_image_get_active_drawable (image);
case CAGE_STATE_CLOSING:
ct->tool_state = CAGE_STATE_WAIT;
break;
ct->cage_complete = TRUE;
gimp_cage_tool_switch_to_deform (ct);
case CAGE_STATE_MOVE_HANDLE:
gimp_cage_config_remove_last_cage_point (ct->config);
ct->tool_state = CAGE_STATE_WAIT;
break;
gimp_cage_config_reverse_cage_if_needed (ct->config);
gimp_cage_tool_compute_coef (ct, display);
case CAGE_STATE_SELECTING:
ct->tool_state = CAGE_STATE_WAIT;
break;
gimp_cage_tool_create_image_map (ct, drawable);
case DEFORM_STATE_MOVE_HANDLE:
gimp_cage_tool_image_map_update (ct);
ct->tool_state = DEFORM_STATE_WAIT;
break;
gimp_tool_push_status (tool, display, _("Press ENTER to commit the transform"));
}
}
else if (ct->moving_handle == -1)
{
/* user released outside any handles, add one if the cage is not
* complete yet
*/
if (! ct->cage_complete)
{
gimp_cage_config_add_cage_point (ct->config,
ct->cursor_x, ct->cursor_y);
case DEFORM_STATE_SELECTING:
ct->tool_state = DEFORM_STATE_WAIT;
break;
}
gimp_cage_config_reset_displacement (ct->config);
}
else
/* Normal release */
{
/* user moved a handle
*/
gimp_cage_config_move_cage_point (ct->config,
options->cage_mode,
ct->moving_handle,
ct->cursor_x,
ct->cursor_y);
if (ct->cage_complete)
switch(ct->tool_state)
{
GimpDisplayShell *shell = gimp_display_get_shell (tool->display);
GimpItem *item = GIMP_ITEM (tool->drawable);
gint x, y;
gint w, h;
gint off_x, off_y;
GeglRectangle visible;
case CAGE_STATE_CLOSING:
ct->dirty_coef = TRUE;
gimp_cage_config_commit_displacement (ct->config);
g_object_set (options, "cage-mode", GIMP_CAGE_MODE_DEFORM, NULL);
break;
gimp_display_shell_untransform_viewport (shell, &x, &y, &w, &h);
case CAGE_STATE_MOVE_HANDLE:
ct->dirty_coef = TRUE;
ct->tool_state = CAGE_STATE_WAIT;
gimp_cage_config_commit_displacement (ct->config);
break;
gimp_item_get_offset (item, &off_x, &off_y);
case CAGE_STATE_SELECTING:
{
GeglRectangle area = {MIN(ct->selection_start_x, coords->x) - ct->offset_x,
MIN(ct->selection_start_y, coords->y) - ct->offset_y,
abs (ct->selection_start_x - coords->x),
abs (ct->selection_start_y - coords->y)};
gimp_rectangle_intersect (x, y, w, h,
off_x,
off_y,
gimp_item_get_width (item),
gimp_item_get_height (item),
&visible.x,
&visible.y,
&visible.width,
&visible.height);
if (state & GDK_SHIFT_MASK)
{
gimp_cage_config_select_add_area (ct->config, GIMP_CAGE_MODE_CAGE_CHANGE, area);
}
else
{
gimp_cage_config_select_area (ct->config, GIMP_CAGE_MODE_CAGE_CHANGE, area);
}
ct->tool_state = CAGE_STATE_WAIT;
}
break;
visible.x -= off_x;
visible.y -= off_y;
case DEFORM_STATE_MOVE_HANDLE:
ct->tool_state = DEFORM_STATE_WAIT;
gimp_cage_config_commit_displacement (ct->config);
gimp_cage_tool_image_map_update (ct);
break;
gimp_image_map_apply (ct->image_map, &visible);
case DEFORM_STATE_SELECTING:
{
GeglRectangle area = {MIN(ct->selection_start_x, coords->x) - ct->offset_x,
MIN(ct->selection_start_y, coords->y) - ct->offset_y,
abs (ct->selection_start_x - coords->x),
abs (ct->selection_start_y - coords->y)};
if (state & GDK_SHIFT_MASK)
{
gimp_cage_config_select_add_area (ct->config, GIMP_CAGE_MODE_DEFORM, area);
}
else
{
gimp_cage_config_select_area (ct->config, GIMP_CAGE_MODE_DEFORM, area);
}
ct->tool_state = DEFORM_STATE_WAIT;
}
break;
}
}
ct->moving_handle = -1;
gimp_draw_tool_resume (GIMP_DRAW_TOOL (tool));
}
@ -492,80 +736,65 @@ gimp_cage_tool_draw (GimpDrawTool *draw_tool)
GimpCageOptions *options = GIMP_CAGE_TOOL_GET_OPTIONS (ct);
GimpCageConfig *config = ct->config;
GimpCanvasGroup *stroke_group;
GimpVector2 *vertices;
gint n_vertices;
gint i;
GimpHandleType handle;
n_vertices = config->n_cage_vertices;
if (n_vertices < 1)
if (ct->tool_state == CAGE_STATE_INIT)
return;
stroke_group = gimp_draw_tool_add_stroke_group (draw_tool);
if (options->cage_mode == GIMP_CAGE_MODE_CAGE_CHANGE)
vertices = config->cage_vertices;
else
vertices = config->cage_vertices_d;
gimp_draw_tool_push_group (draw_tool, stroke_group);
if (! ct->cage_complete && (ct->hovering_handle == -1 ||
(ct->hovering_handle == 0 && n_vertices > 2)))
/* If needed, draw ligne to the cursor. */
if (!ct->cage_complete)
{
GimpVector2 last_point = gimp_cage_config_get_point_coordinate (ct->config, GIMP_CAGE_MODE_CAGE_CHANGE, n_vertices - 1);
gimp_draw_tool_add_line (draw_tool,
vertices[n_vertices - 1].x + ct->config->offset_x,
vertices[n_vertices - 1].y + ct->config->offset_y,
last_point.x + ct->offset_x,
last_point.y + ct->offset_y,
ct->cursor_x,
ct->cursor_y);
}
gimp_draw_tool_pop_group (draw_tool);
/* Draw the cage with handles. */
for (i = 0; i < n_vertices; i++)
{
GimpHandleType handle;
gdouble x1, y1;
GimpVector2 point1, point2;
if (i == ct->moving_handle)
{
x1 = ct->cursor_x;
y1 = ct->cursor_y;
}
else
{
x1 = vertices[i].x;
y1 = vertices[i].y;
}
point1 = gimp_cage_config_get_point_coordinate (ct->config,
options->cage_mode,
i);
point1.x += ct->offset_x;
point1.y += ct->offset_y;
if (i > 0 || ct->cage_complete)
{
gdouble x2, y2;
gint point2;
gint index_point2;
if (i == 0)
point2 = n_vertices - 1;
index_point2 = n_vertices - 1;
else
point2 = i - 1;
index_point2 = i - 1;
if (point2 == ct->moving_handle)
{
x2 = ct->cursor_x;
y2 = ct->cursor_y;
}
else
{
x2 = vertices[point2].x;
y2 = vertices[point2].y;
}
point2 = gimp_cage_config_get_point_coordinate (ct->config,
options->cage_mode,
index_point2);
point2.x += ct->offset_x;
point2.y += ct->offset_y;
gimp_draw_tool_push_group (draw_tool, stroke_group);
gimp_draw_tool_add_line (draw_tool,
x1 + ct->config->offset_x,
y1 + ct->config->offset_y,
x2 + ct->config->offset_x,
y2 + ct->config->offset_y);
point1.x,
point1.y,
point2.x,
point2.y);
gimp_draw_tool_pop_group (draw_tool);
}
@ -577,50 +806,67 @@ gimp_cage_tool_draw (GimpDrawTool *draw_tool)
gimp_draw_tool_add_handle (draw_tool,
handle,
x1 + ct->config->offset_x,
y1 + ct->config->offset_y,
point1.x,
point1.y,
GIMP_TOOL_HANDLE_SIZE_CIRCLE,
GIMP_TOOL_HANDLE_SIZE_CIRCLE,
GIMP_HANDLE_ANCHOR_CENTER);
if (ct->config->cage_points[i].selected)
{
gimp_draw_tool_add_handle (draw_tool,
GIMP_HANDLE_SQUARE,
point1.x,
point1.y,
GIMP_TOOL_HANDLE_SIZE_CIRCLE,
GIMP_TOOL_HANDLE_SIZE_CIRCLE,
GIMP_HANDLE_ANCHOR_CENTER);
}
}
if (ct->tool_state == DEFORM_STATE_SELECTING || ct->tool_state == CAGE_STATE_SELECTING)
{
gimp_draw_tool_add_rectangle (draw_tool,
FALSE,
MIN(ct->selection_start_x, ct->cursor_x),
MIN(ct->selection_start_y, ct->cursor_y),
abs (ct->selection_start_x - ct->cursor_x),
abs (ct->selection_start_y - ct->cursor_y));
}
}
static gint
gimp_cage_tool_is_on_handle (GimpCageConfig *gcc,
gimp_cage_tool_is_on_handle (GimpCageTool *ct,
GimpDrawTool *draw_tool,
GimpDisplay *display,
GimpCageMode mode,
gdouble x,
gdouble y,
gint handle_size)
{
gint i;
gdouble vert_x;
gdouble vert_y;
gdouble dist = G_MAXDOUBLE;
GimpCageOptions *options = GIMP_CAGE_TOOL_GET_OPTIONS (ct);
GimpCageConfig *config = ct->config;
gdouble dist = G_MAXDOUBLE;
gint i;
GimpVector2 cage_point;
g_return_val_if_fail (GIMP_IS_CAGE_CONFIG (gcc), -1);
g_return_val_if_fail (GIMP_IS_CAGE_TOOL (ct), -1);
if (gcc->n_cage_vertices == 0)
if (config->n_cage_vertices == 0)
return -1;
for (i = 0; i < gcc->n_cage_vertices; i++)
for (i = 0; i < config->n_cage_vertices; i++)
{
if (mode == GIMP_CAGE_MODE_CAGE_CHANGE)
{
vert_x = gcc->cage_vertices[i].x + gcc->offset_x;
vert_y = gcc->cage_vertices[i].y + gcc->offset_y;
}
else
{
vert_x = gcc->cage_vertices_d[i].x + gcc->offset_x;
vert_y = gcc->cage_vertices_d[i].y + gcc->offset_y;
}
cage_point = gimp_cage_config_get_point_coordinate (config,
options->cage_mode,
i);
cage_point.x += ct->offset_x;
cage_point.y += ct->offset_y;
dist = gimp_draw_tool_calc_distance_square (GIMP_DRAW_TOOL (draw_tool),
display,
x, y,
vert_x, vert_y);
cage_point.x,
cage_point.y);
if (dist <= SQR (handle_size / 2))
return i;
@ -641,14 +887,6 @@ gimp_cage_tool_remove_last_handle (GimpCageTool *ct)
gimp_draw_tool_resume (GIMP_DRAW_TOOL (ct));
}
static void
gimp_cage_tool_switch_to_deform (GimpCageTool *ct)
{
GimpCageOptions *options = GIMP_CAGE_TOOL_GET_OPTIONS (ct);
g_object_set (options, "cage-mode", GIMP_CAGE_MODE_DEFORM, NULL);
}
static void
gimp_cage_tool_compute_coef (GimpCageTool *ct,
GimpDisplay *display)
@ -722,10 +960,12 @@ gimp_cage_tool_compute_coef (GimpCageTool *ct,
ct->coef = buffer;
g_object_unref (gegl);
ct->dirty_coef = FALSE;
gimp_display_shell_remove_item (shell, p);
}
static GeglNode *
static void
gimp_cage_tool_create_render_node (GimpCageTool *ct)
{
GimpCageOptions *options = GIMP_CAGE_TOOL_GET_OPTIONS (ct);
@ -733,6 +973,9 @@ gimp_cage_tool_create_render_node (GimpCageTool *ct)
GeglNode *input, *output; /* Proxy nodes*/
GeglNode *node; /* wraper to be returned */
g_return_if_fail (ct->render_node == NULL);
/* render_node is not supposed to be recreated */
node = gegl_node_new ();
input = gegl_node_get_input_proxy (node, "input");
@ -768,25 +1011,58 @@ gimp_cage_tool_create_render_node (GimpCageTool *ct)
gegl_node_connect_to (render, "output",
output, "input");
return node;
ct->render_node = node;
ct->cage_node = cage;
ct->coef_node = coef;
}
static void
gimp_cage_tool_render_node_update (GimpCageTool *ct)
{
GimpCageOptions *options = GIMP_CAGE_TOOL_GET_OPTIONS (ct);
gboolean option_fill, node_fill;
GeglBuffer *buffer;
g_object_get (options,
"fill-plain-color", &option_fill,
NULL);
gegl_node_get (ct->cage_node,
"fill-plain-color", &node_fill,
NULL);
if (option_fill != node_fill)
{
gegl_node_set (ct->cage_node,
"fill_plain_color", option_fill,
NULL);
}
gegl_node_get (ct->coef_node,
"buffer", &buffer,
NULL);
if (option_fill != node_fill)
{
gegl_node_set (ct->coef_node,
"buffer", ct->coef,
NULL);
}
}
static void
gimp_cage_tool_create_image_map (GimpCageTool *ct,
GimpDrawable *drawable)
{
GeglNode *node;
node = gimp_cage_tool_create_render_node (ct);
if (!ct->render_node)
gimp_cage_tool_create_render_node (ct);
ct->image_map = gimp_image_map_new (drawable,
_("Cage transform"),
node,
ct->render_node,
NULL,
NULL);
g_object_unref (node);
g_signal_connect (ct->image_map, "flush",
G_CALLBACK (gimp_cage_tool_image_map_flush),
ct);
@ -802,6 +1078,37 @@ gimp_cage_tool_image_map_flush (GimpImageMap *image_map,
gimp_display_flush_now (tool->display);
}
static void
gimp_cage_tool_image_map_update (GimpCageTool *ct)
{
GimpTool *tool = GIMP_TOOL (ct);
GimpDisplayShell *shell = gimp_display_get_shell (tool->display);
GimpItem *item = GIMP_ITEM (tool->drawable);
gint x, y;
gint w, h;
gint off_x, off_y;
GeglRectangle visible;
gimp_display_shell_untransform_viewport (shell, &x, &y, &w, &h);
gimp_item_get_offset (item, &off_x, &off_y);
gimp_rectangle_intersect (x, y, w, h,
off_x,
off_y,
gimp_item_get_width (item),
gimp_item_get_height (item),
&visible.x,
&visible.y,
&visible.width,
&visible.height);
visible.x -= off_x;
visible.y -= off_y;
gimp_image_map_apply (ct->image_map, &visible);
}
static void
gimp_cage_tool_halt (GimpCageTool *ct)
{
@ -826,6 +1133,14 @@ gimp_cage_tool_halt (GimpCageTool *ct)
ct->coef = NULL;
}
if (ct->render_node)
{
g_object_unref (ct->render_node);
ct->render_node = NULL;
ct->coef_node = NULL;
ct->cage_node = NULL;
}
if (ct->image_map)
{
gimp_tool_control_set_preserve (tool->control, TRUE);

View File

@ -44,15 +44,31 @@ struct _GimpCageTool
GimpCageConfig *config;
gdouble cursor_x;
gdouble cursor_y;
gint hovering_handle;
gint moving_handle;
gboolean cage_complete;
gint offset_x; /* used to convert the cage point coords */
gint offset_y; /* to drawable coords */
GeglBuffer *coef;
gdouble cursor_x; /* Hold the cursor x position */
gdouble cursor_y; /* Hold the cursor y position */
GimpImageMap *image_map;
gdouble movement_start_x; /* Where the movement started */
gdouble movement_start_y; /* Where the movement started */
gdouble selection_start_x; /* Where the selection started */
gdouble selection_start_y; /* Where the selection started */
gint hovering_handle; /* Handle which the cursor is above */
gboolean cage_complete; /* Cage closed or not */
GeglBuffer *coef; /* Gegl where the coefficient of the transformation are stored */
gboolean dirty_coef; /* Indicate if the coef are still valid */
GeglNode *render_node; /* Gegl node graph to render the transfromation */
GeglNode *cage_node; /* Gegl node that compute the cage transform */
GeglNode *coef_node; /* Gegl node that read in the coef buffer */
gint tool_state; /* Current state in statemachine */
GimpImageMap *image_map; /* For preview */
};
struct _GimpCageToolClass