static void gimp_perspective_clone_tool_motion (GimpTool *tool, const GimpCoords *coords, guint32 time, GdkModifierType state, GimpDisplay *display) { GimpPerspectiveCloneTool *clone_tool = GIMP_PERSPECTIVE_CLONE_TOOL (tool); GimpPaintTool *paint_tool = GIMP_PAINT_TOOL (tool); GimpPerspectiveClone *clone = GIMP_PERSPECTIVE_CLONE (paint_tool->core); GimpPerspectiveCloneOptions *options; options = GIMP_PERSPECTIVE_CLONE_TOOL_GET_OPTIONS (tool); if (options->clone_mode == GIMP_PERSPECTIVE_CLONE_MODE_ADJUST) { if (clone_tool->grab_widget) { gimp_tool_widget_motion (clone_tool->grab_widget, coords, time, state); } } else if (options->clone_mode == GIMP_PERSPECTIVE_CLONE_MODE_PAINT) { gdouble nnx, nny; gimp_draw_tool_pause (GIMP_DRAW_TOOL (tool)); GIMP_TOOL_CLASS (parent_class)->motion (tool, coords, time, state, display); /* Set the coordinates for the reference cross */ gimp_perspective_clone_get_source_point (clone, coords->x, coords->y, &nnx, &nny); clone_tool->src_x = floor (nnx); clone_tool->src_y = floor (nny); gimp_draw_tool_resume (GIMP_DRAW_TOOL (tool)); } }
static GeglBuffer * gimp_perspective_clone_get_source (GimpSourceCore *source_core, GimpDrawable *drawable, GimpPaintOptions *paint_options, GimpPickable *src_pickable, gint src_offset_x, gint src_offset_y, GeglBuffer *paint_buffer, gint paint_buffer_x, gint paint_buffer_y, gint *paint_area_offset_x, gint *paint_area_offset_y, gint *paint_area_width, gint *paint_area_height, GeglRectangle *src_rect) { GimpPerspectiveClone *clone = GIMP_PERSPECTIVE_CLONE (source_core); GimpCloneOptions *clone_options = GIMP_CLONE_OPTIONS (paint_options); GeglBuffer *src_buffer; GeglBuffer *dest_buffer; const Babl *src_format_alpha; gint x1d, y1d, x2d, y2d; gdouble x1s, y1s, x2s, y2s, x3s, y3s, x4s, y4s; gint xmin, ymin, xmax, ymax; GimpMatrix3 matrix; GimpMatrix3 gegl_matrix; src_buffer = gimp_pickable_get_buffer (src_pickable); src_format_alpha = gimp_pickable_get_format_with_alpha (src_pickable); /* Destination coordinates that will be painted */ x1d = paint_buffer_x; y1d = paint_buffer_y; x2d = paint_buffer_x + gegl_buffer_get_width (paint_buffer); y2d = paint_buffer_y + gegl_buffer_get_height (paint_buffer); /* Boundary box for source pixels to copy: Convert all the vertex of * the box to paint in destination area to its correspondent in * source area bearing in mind perspective */ gimp_perspective_clone_get_source_point (clone, x1d, y1d, &x1s, &y1s); gimp_perspective_clone_get_source_point (clone, x1d, y2d, &x2s, &y2s); gimp_perspective_clone_get_source_point (clone, x2d, y1d, &x3s, &y3s); gimp_perspective_clone_get_source_point (clone, x2d, y2d, &x4s, &y4s); xmin = floor (MIN4 (x1s, x2s, x3s, x4s)); ymin = floor (MIN4 (y1s, y2s, y3s, y4s)); xmax = ceil (MAX4 (x1s, x2s, x3s, x4s)); ymax = ceil (MAX4 (y1s, y2s, y3s, y4s)); switch (clone_options->clone_type) { case GIMP_IMAGE_CLONE: if (! gimp_rectangle_intersect (xmin, ymin, xmax - xmin, ymax - ymin, 0, 0, gegl_buffer_get_width (src_buffer), gegl_buffer_get_height (src_buffer), NULL, NULL, NULL, NULL)) { /* if the source area is completely out of the image */ return NULL; } break; case GIMP_PATTERN_CLONE: gegl_node_set (clone->crop, "x", (gdouble) xmin, "y", (gdouble) ymin, "width", (gdouble) xmax - xmin, "height", (gdouble) ymax - ymin, NULL); break; } dest_buffer = gegl_buffer_new (GEGL_RECTANGLE (0, 0, x2d - x1d, y2d - y1d), src_format_alpha); gimp_perspective_clone_get_matrix (clone, &matrix); gimp_matrix3_identity (&gegl_matrix); gimp_matrix3_mult (&matrix, &gegl_matrix); gimp_matrix3_translate (&gegl_matrix, -x1d, -y1d); gimp_gegl_node_set_matrix (clone->transform_node, &gegl_matrix); gegl_node_set (clone->dest_node, "buffer", dest_buffer, NULL); gegl_node_blit (clone->dest_node, 1.0, GEGL_RECTANGLE (0, 0, x2d - x1d, y2d - y1d), NULL, NULL, 0, GEGL_BLIT_DEFAULT); *src_rect = *GEGL_RECTANGLE (0, 0, x2d - x1d, y2d - y1d); return dest_buffer; }
static void gimp_perspective_clone_tool_oper_update (GimpTool *tool, const GimpCoords *coords, GdkModifierType state, gboolean proximity, GimpDisplay *display) { GimpPerspectiveCloneTool *clone_tool = GIMP_PERSPECTIVE_CLONE_TOOL (tool); GimpPerspectiveCloneOptions *options; options = GIMP_PERSPECTIVE_CLONE_TOOL_GET_OPTIONS (tool); if (options->clone_mode == GIMP_PERSPECTIVE_CLONE_MODE_ADJUST) { GimpDrawTool *draw_tool = GIMP_DRAW_TOOL (tool); gdouble closest_dist; gdouble dist; clone_tool->function = TRANSFORM_HANDLE_NONE; if (display != tool->display) return; dist = gimp_draw_tool_calc_distance_square (draw_tool, display, coords->x, coords->y, clone_tool->tx1, clone_tool->ty1); closest_dist = dist; clone_tool->function = TRANSFORM_HANDLE_NW; dist = gimp_draw_tool_calc_distance_square (draw_tool, display, coords->x, coords->y, clone_tool->tx2, clone_tool->ty2); if (dist < closest_dist) { closest_dist = dist; clone_tool->function = TRANSFORM_HANDLE_NE; } dist = gimp_draw_tool_calc_distance_square (draw_tool, display, coords->x, coords->y, clone_tool->tx3, clone_tool->ty3); if (dist < closest_dist) { closest_dist = dist; clone_tool->function = TRANSFORM_HANDLE_SW; } dist = gimp_draw_tool_calc_distance_square (draw_tool, display, coords->x, coords->y, clone_tool->tx4, clone_tool->ty4); if (dist < closest_dist) { closest_dist = dist; clone_tool->function = TRANSFORM_HANDLE_SE; } } else { GIMP_TOOL_CLASS (parent_class)->oper_update (tool, coords, state, proximity, display); if (proximity) { GimpPaintCore *core = GIMP_PAINT_TOOL (tool)->core; GimpPerspectiveClone *clone = GIMP_PERSPECTIVE_CLONE (core); GimpSourceCore *source_core = GIMP_SOURCE_CORE (core); if (source_core->src_drawable == NULL) { gimp_tool_replace_status (tool, display, _("Ctrl-Click to set a clone source")); } else { gimp_draw_tool_pause (GIMP_DRAW_TOOL (tool)); clone_tool->src_x = source_core->src_x; clone_tool->src_y = source_core->src_y; if (! source_core->first_stroke) { if (GIMP_SOURCE_OPTIONS (options)->align_mode == GIMP_SOURCE_ALIGN_YES) { gdouble nnx, nny; /* Set the coordinates for the reference cross */ gimp_perspective_clone_get_source_point (clone, coords->x, coords->y, &nnx, &nny); clone_tool->src_x = nnx; clone_tool->src_y = nny; } } gimp_draw_tool_resume (GIMP_DRAW_TOOL (tool)); } } } }
static void gimp_perspective_clone_tool_motion (GimpTool *tool, const GimpCoords *coords, guint32 time, GdkModifierType state, GimpDisplay *display) { GimpPerspectiveCloneTool *clone_tool = GIMP_PERSPECTIVE_CLONE_TOOL (tool); GimpPaintTool *paint_tool = GIMP_PAINT_TOOL (tool); GimpPerspectiveClone *clone = GIMP_PERSPECTIVE_CLONE (paint_tool->core); GimpPerspectiveCloneOptions *options; options = GIMP_PERSPECTIVE_CLONE_TOOL_GET_OPTIONS (tool); if (options->clone_mode == GIMP_PERSPECTIVE_CLONE_MODE_ADJUST) { gdouble diff_x, diff_y; /* if we are creating, there is nothing to be done so exit. */ if (clone_tool->function == TRANSFORM_CREATING) return; gimp_draw_tool_pause (GIMP_DRAW_TOOL (tool)); clone_tool->curx = coords->x; clone_tool->cury = coords->y; /* recalculate the tool's transformation matrix */ diff_x = clone_tool->curx - clone_tool->lastx; diff_y = clone_tool->cury - clone_tool->lasty; switch (clone_tool->function) { case TRANSFORM_HANDLE_NW: clone_tool->trans_info[X0] += diff_x; clone_tool->trans_info[Y0] += diff_y; break; case TRANSFORM_HANDLE_NE: clone_tool->trans_info[X1] += diff_x; clone_tool->trans_info[Y1] += diff_y; break; case TRANSFORM_HANDLE_SW: clone_tool->trans_info[X2] += diff_x; clone_tool->trans_info[Y2] += diff_y; break; case TRANSFORM_HANDLE_SE: clone_tool->trans_info[X3] += diff_x; clone_tool->trans_info[Y3] += diff_y; break; default: break; } gimp_perspective_clone_tool_recalc_matrix (clone_tool); clone_tool->lastx = clone_tool->curx; clone_tool->lasty = clone_tool->cury; gimp_draw_tool_resume (GIMP_DRAW_TOOL (tool)); } else if (options->clone_mode == GIMP_PERSPECTIVE_CLONE_MODE_PAINT) { gdouble nnx, nny; gimp_draw_tool_pause (GIMP_DRAW_TOOL (tool)); GIMP_TOOL_CLASS (parent_class)->motion (tool, coords, time, state, display); /* Set the coordinates for the reference cross */ gimp_perspective_clone_get_source_point (clone, coords->x, coords->y, &nnx, &nny); clone_tool->src_x = nnx; clone_tool->src_y = nny; gimp_draw_tool_resume (GIMP_DRAW_TOOL (tool)); } }
static void gimp_perspective_clone_tool_button_press (GimpTool *tool, const GimpCoords *coords, guint32 time, GdkModifierType state, GimpButtonPressType press_type, GimpDisplay *display) { GimpPaintTool *paint_tool = GIMP_PAINT_TOOL (tool); GimpPerspectiveCloneTool *clone_tool = GIMP_PERSPECTIVE_CLONE_TOOL (tool); GimpPerspectiveClone *clone = GIMP_PERSPECTIVE_CLONE (paint_tool->core); GimpSourceCore *source_core = GIMP_SOURCE_CORE (clone); GimpPerspectiveCloneOptions *options; options = GIMP_PERSPECTIVE_CLONE_TOOL_GET_OPTIONS (tool); switch (options->clone_mode) { case GIMP_PERSPECTIVE_CLONE_MODE_ADJUST: if (clone_tool->function == TRANSFORM_CREATING) gimp_perspective_clone_tool_oper_update (tool, coords, state, TRUE, display); clone_tool->lastx = coords->x; clone_tool->lasty = coords->y; gimp_tool_control_activate (tool->control); break; case GIMP_PERSPECTIVE_CLONE_MODE_PAINT: { GdkModifierType extend_mask = gimp_get_extend_selection_mask (); GdkModifierType toggle_mask = gimp_get_toggle_behavior_mask (); gdouble nnx, nny; gimp_draw_tool_pause (GIMP_DRAW_TOOL (tool)); if ((state & (toggle_mask | extend_mask)) == toggle_mask) { source_core->set_source = TRUE; clone_tool->src_display = display; } else { source_core->set_source = FALSE; } GIMP_TOOL_CLASS (parent_class)->button_press (tool, coords, time, state, press_type, display); /* Set the coordinates for the reference cross */ gimp_perspective_clone_get_source_point (clone, coords->x, coords->y, &nnx, &nny); clone_tool->src_x = nnx; clone_tool->src_y = nny; gimp_draw_tool_resume (GIMP_DRAW_TOOL (tool)); } break; } }
static void gimp_perspective_clone_tool_oper_update (GimpTool *tool, const GimpCoords *coords, GdkModifierType state, gboolean proximity, GimpDisplay *display) { GimpPerspectiveCloneTool *clone_tool = GIMP_PERSPECTIVE_CLONE_TOOL (tool); GimpPerspectiveCloneOptions *options; options = GIMP_PERSPECTIVE_CLONE_TOOL_GET_OPTIONS (tool); if (options->clone_mode == GIMP_PERSPECTIVE_CLONE_MODE_ADJUST) { if (clone_tool->widget) { if (display == tool->display) { gimp_tool_widget_hover (clone_tool->widget, coords, state, proximity); } } } else { GIMP_TOOL_CLASS (parent_class)->oper_update (tool, coords, state, proximity, display); if (proximity) { GimpPaintCore *core = GIMP_PAINT_TOOL (tool)->core; GimpPerspectiveClone *clone = GIMP_PERSPECTIVE_CLONE (core); GimpSourceCore *source_core = GIMP_SOURCE_CORE (core); if (source_core->src_drawable == NULL) { gimp_tool_replace_status (tool, display, _("Ctrl-Click to set a clone source")); } else { gimp_draw_tool_pause (GIMP_DRAW_TOOL (tool)); clone_tool->src_x = source_core->src_x; clone_tool->src_y = source_core->src_y; if (! source_core->first_stroke) { if (GIMP_SOURCE_OPTIONS (options)->align_mode == GIMP_SOURCE_ALIGN_YES) { gdouble nnx, nny; /* Set the coordinates for the reference cross */ gimp_perspective_clone_get_source_point (clone, coords->x, coords->y, &nnx, &nny); clone_tool->src_x = floor (nnx); clone_tool->src_y = floor (nny); } } gimp_draw_tool_resume (GIMP_DRAW_TOOL (tool)); } } } }
static void gimp_perspective_clone_tool_button_press (GimpTool *tool, const GimpCoords *coords, guint32 time, GdkModifierType state, GimpButtonPressType press_type, GimpDisplay *display) { GimpPaintTool *paint_tool = GIMP_PAINT_TOOL (tool); GimpPerspectiveCloneTool *clone_tool = GIMP_PERSPECTIVE_CLONE_TOOL (tool); GimpPerspectiveClone *clone = GIMP_PERSPECTIVE_CLONE (paint_tool->core); GimpSourceCore *source_core = GIMP_SOURCE_CORE (clone); GimpPerspectiveCloneOptions *options; options = GIMP_PERSPECTIVE_CLONE_TOOL_GET_OPTIONS (tool); if (options->clone_mode == GIMP_PERSPECTIVE_CLONE_MODE_ADJUST) { if (clone_tool->widget) { gimp_tool_widget_hover (clone_tool->widget, coords, state, TRUE); if (gimp_tool_widget_button_press (clone_tool->widget, coords, time, state, press_type)) { clone_tool->grab_widget = clone_tool->widget; } } gimp_tool_control_activate (tool->control); } else { GdkModifierType extend_mask = gimp_get_extend_selection_mask (); GdkModifierType toggle_mask = gimp_get_toggle_behavior_mask (); gdouble nnx, nny; gimp_draw_tool_pause (GIMP_DRAW_TOOL (tool)); if ((state & (toggle_mask | extend_mask)) == toggle_mask) { source_core->set_source = TRUE; clone_tool->src_display = display; } else { source_core->set_source = FALSE; } GIMP_TOOL_CLASS (parent_class)->button_press (tool, coords, time, state, press_type, display); /* Set the coordinates for the reference cross */ gimp_perspective_clone_get_source_point (clone, coords->x, coords->y, &nnx, &nny); clone_tool->src_x = floor (nnx); clone_tool->src_y = floor (nny); gimp_draw_tool_resume (GIMP_DRAW_TOOL (tool)); } }
static gboolean gimp_perspective_clone_get_source (GimpSourceCore *source_core, GimpDrawable *drawable, GimpPaintOptions *paint_options, GimpPickable *src_pickable, gint src_offset_x, gint src_offset_y, TempBuf *paint_area, gint *paint_area_offset_x, gint *paint_area_offset_y, gint *paint_area_width, gint *paint_area_height, PixelRegion *srcPR) { GimpPerspectiveClone *clone = GIMP_PERSPECTIVE_CLONE (source_core); GimpPaintCore *paint_core = GIMP_PAINT_CORE (source_core); GimpSourceOptions *options = GIMP_SOURCE_OPTIONS (paint_options); GimpImage *src_image; GimpImage *image; GimpImageType src_type; gint x1d, y1d, x2d, y2d; gdouble x1s, y1s, x2s, y2s, x3s, y3s, x4s, y4s; gint xmin, ymin, xmax, ymax; TileManager *src_tiles; TileManager *orig_tiles; PixelRegion origPR; PixelRegion destPR; GimpMatrix3 matrix; gint bytes; src_image = gimp_pickable_get_image (src_pickable); image = gimp_item_get_image (GIMP_ITEM (drawable)); src_type = gimp_pickable_get_image_type (src_pickable); src_tiles = gimp_pickable_get_tiles (src_pickable); /* Destination coordinates that will be painted */ x1d = paint_area->x; y1d = paint_area->y; x2d = paint_area->x + paint_area->width; y2d = paint_area->y + paint_area->height; /* Boundary box for source pixels to copy: Convert all the vertex of * the box to paint in destination area to its correspondent in * source area bearing in mind perspective */ gimp_perspective_clone_get_source_point (clone, x1d, y1d, &x1s, &y1s); gimp_perspective_clone_get_source_point (clone, x1d, y2d, &x2s, &y2s); gimp_perspective_clone_get_source_point (clone, x2d, y1d, &x3s, &y3s); gimp_perspective_clone_get_source_point (clone, x2d, y2d, &x4s, &y4s); xmin = floor (MIN4 (x1s, x2s, x3s, x4s)); ymin = floor (MIN4 (y1s, y2s, y3s, y4s)); xmax = ceil (MAX4 (x1s, x2s, x3s, x4s)); ymax = ceil (MAX4 (y1s, y2s, y3s, y4s)); xmin = CLAMP (xmin - 1, 0, tile_manager_width (src_tiles)); ymin = CLAMP (ymin - 1, 0, tile_manager_height (src_tiles)); xmax = CLAMP (xmax + 1, 0, tile_manager_width (src_tiles)); ymax = CLAMP (ymax + 1, 0, tile_manager_height (src_tiles)); /* if the source area is completely out of the image */ if (!(xmax - xmin) || !(ymax - ymin)) return FALSE; /* If the source image is different from the destination, * then we should copy straight from the source image * to the canvas. * Otherwise, we need a call to get_orig_image to make sure * we get a copy of the unblemished (offset) image */ if (( options->sample_merged && (src_image != image)) || (! options->sample_merged && (source_core->src_drawable != drawable))) { pixel_region_init (&origPR, src_tiles, xmin, ymin, xmax - xmin, ymax - ymin, FALSE); } else { TempBuf *orig; /* get the original image */ if (options->sample_merged) orig = gimp_paint_core_get_orig_proj (paint_core, src_pickable, xmin, ymin, xmax, ymax); else orig = gimp_paint_core_get_orig_image (paint_core, GIMP_DRAWABLE (src_pickable), xmin, ymin, xmax, ymax); pixel_region_init_temp_buf (&origPR, orig, 0, 0, xmax - xmin, ymax - ymin); } /* copy the original image to a tile manager, adding alpha if needed */ bytes = GIMP_IMAGE_TYPE_BYTES (GIMP_IMAGE_TYPE_WITH_ALPHA (src_type)); orig_tiles = tile_manager_new (xmax - xmin, ymax - ymin, bytes); tile_manager_set_offsets (orig_tiles, xmin, ymin); pixel_region_init (&destPR, orig_tiles, 0, 0, xmax - xmin, ymax - ymin, TRUE); if (bytes > origPR.bytes) add_alpha_region (&origPR, &destPR); else copy_region (&origPR, &destPR); clone->src_area = temp_buf_resize (clone->src_area, tile_manager_bpp (orig_tiles), 0, 0, x2d - x1d, y2d - y1d); pixel_region_init_temp_buf (&destPR, clone->src_area, 0, 0, x2d - x1d, y2d - y1d); gimp_perspective_clone_get_matrix (clone, &matrix); gimp_transform_region (src_pickable, GIMP_CONTEXT (paint_options), orig_tiles, &destPR, x1d, y1d, x2d, y2d, &matrix, GIMP_INTERPOLATION_LINEAR, 0, NULL); tile_manager_unref (orig_tiles); pixel_region_init_temp_buf (srcPR, clone->src_area, 0, 0, x2d - x1d, y2d - y1d); return TRUE; }