static void gimp_image_map_tool_guide_moved (GimpGuide *guide, const GParamSpec *pspec, GimpImageMapTool *im_tool) { GimpImageMapOptions *options = GIMP_IMAGE_MAP_TOOL_GET_OPTIONS (im_tool); GimpItem *item = GIMP_ITEM (im_tool->drawable); gdouble position; if (options->preview_alignment == GIMP_ALIGN_LEFT || options->preview_alignment == GIMP_ALIGN_RIGHT) { position = ((gdouble) (gimp_guide_get_position (guide) - gimp_item_get_offset_x (item)) / (gdouble) gimp_item_get_width (item)); } else { position = ((gdouble) (gimp_guide_get_position (guide) - gimp_item_get_offset_y (item)) / (gdouble) gimp_item_get_height (item)); } g_object_set (options, "preview-position", CLAMP (position, 0.0, 1.0), NULL); }
static GimpValueArray * image_get_guide_position_invoker (GimpProcedure *procedure, Gimp *gimp, GimpContext *context, GimpProgress *progress, const GimpValueArray *args, GError **error) { gboolean success = TRUE; GimpValueArray *return_vals; GimpImage *image; gint32 guide; gint32 position = 0; image = gimp_value_get_image (gimp_value_array_index (args, 0), gimp); guide = g_value_get_uint (gimp_value_array_index (args, 1)); if (success) { GimpGuide *g = gimp_pdb_image_get_guide (image, guide, error); if (g) position = gimp_guide_get_position (g); 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); return return_vals; }
static GValueArray * image_get_guide_position_invoker (GimpProcedure *procedure, Gimp *gimp, GimpContext *context, GimpProgress *progress, const GValueArray *args) { gboolean success = TRUE; GValueArray *return_vals; GimpImage *image; gint32 guide; gint32 position = 0; image = gimp_value_get_image (&args->values[0], gimp); guide = g_value_get_uint (&args->values[1]); if (success) { GimpGuide *g = gimp_image_get_guide (image, guide); if (g) position = gimp_guide_get_position (g); else success = FALSE; } return_vals = gimp_procedure_get_return_values (procedure, success); if (success) g_value_set_int (&return_vals->values[1], position); return return_vals; }
static void gimp_mandala_guide_position_cb (GObject *object, GParamSpec *pspec, GimpMandala *mandala) { GimpGuide *guide = GIMP_GUIDE (object); if (guide == mandala->horizontal_guide) { g_object_set (G_OBJECT (mandala), "center-y", (gdouble) gimp_guide_get_position (guide), NULL); } else if (guide == mandala->vertical_guide) { g_object_set (G_OBJECT (mandala), "center-x", (gdouble) gimp_guide_get_position (guide), NULL); } }
static void gimp_display_shell_guide_move_handler (GimpImage *image, GimpGuide *guide, GimpDisplayShell *shell) { GimpCanvasProxyGroup *group = GIMP_CANVAS_PROXY_GROUP (shell->guides); GimpCanvasItem *item; item = gimp_canvas_proxy_group_get_item (group, guide); gimp_canvas_guide_set (item, gimp_guide_get_orientation (guide), gimp_guide_get_position (guide)); }
static void gimp_display_shell_guide_add_handler (GimpImage *image, GimpGuide *guide, GimpDisplayShell *shell) { GimpCanvasProxyGroup *group = GIMP_CANVAS_PROXY_GROUP (shell->guides); GimpCanvasItem *item; item = gimp_canvas_guide_new (shell, gimp_guide_get_orientation (guide), gimp_guide_get_position (guide), TRUE); gimp_canvas_proxy_group_add_item (group, guide, item); g_object_unref (item); }
static void gimp_guide_undo_pop (GimpUndo *undo, GimpUndoMode undo_mode, GimpUndoAccumulator *accum) { GimpGuideUndo *guide_undo = GIMP_GUIDE_UNDO (undo); GimpOrientationType orientation; gint position; GIMP_UNDO_CLASS (parent_class)->pop (undo, undo_mode, accum); orientation = gimp_guide_get_orientation (guide_undo->guide); position = gimp_guide_get_position (guide_undo->guide); /* add and move guides manually (nor using the gimp_image_guide * API), because we might be in the middle of an image resizing * undo group and the guide's position might be temporarily out of * image. */ if (position == -1) { undo->image->guides = g_list_prepend (undo->image->guides, guide_undo->guide); gimp_guide_set_position (guide_undo->guide, guide_undo->position); g_object_ref (guide_undo->guide); gimp_image_update_guide (undo->image, guide_undo->guide); } else if (guide_undo->position == -1) { gimp_image_remove_guide (undo->image, guide_undo->guide, FALSE); } else { gimp_image_update_guide (undo->image, guide_undo->guide); gimp_guide_set_position (guide_undo->guide, guide_undo->position); gimp_image_update_guide (undo->image, guide_undo->guide); } gimp_guide_set_orientation (guide_undo->guide, guide_undo->orientation); guide_undo->position = position; guide_undo->orientation = orientation; }
static void gimp_flip_tool_draw (GimpDrawTool *draw_tool) { GimpFlipTool *flip = GIMP_FLIP_TOOL (draw_tool); if (flip->guide) { GimpCanvasItem *item; GimpGuideStyle style; style = gimp_guide_get_style (flip->guide); item = gimp_draw_tool_add_guide (draw_tool, gimp_guide_get_orientation (flip->guide), gimp_guide_get_position (flip->guide), style); gimp_canvas_item_set_highlight (item, TRUE); } }
static GObject * gimp_guide_undo_constructor (GType type, guint n_params, GObjectConstructParam *params) { GObject *object; GimpGuideUndo *guide_undo; object = G_OBJECT_CLASS (parent_class)->constructor (type, n_params, params); guide_undo = GIMP_GUIDE_UNDO (object); g_assert (GIMP_IS_GUIDE (guide_undo->guide)); guide_undo->orientation = gimp_guide_get_orientation (guide_undo->guide); guide_undo->position = gimp_guide_get_position (guide_undo->guide); return object; }
static void gimp_image_map_tool_move_guide (GimpImageMapTool *im_tool) { GimpImageMapOptions *options = GIMP_IMAGE_MAP_TOOL_GET_OPTIONS (im_tool); GimpItem *item; GimpOrientationType orientation; gint position; if (! im_tool->percent_guide) return; item = GIMP_ITEM (im_tool->drawable); if (options->preview_alignment == GIMP_ALIGN_LEFT || options->preview_alignment == GIMP_ALIGN_RIGHT) { orientation = GIMP_ORIENTATION_VERTICAL; position = (gimp_item_get_offset_x (item) + gimp_item_get_width (item) * options->preview_position); } else { orientation = GIMP_ORIENTATION_HORIZONTAL; position = (gimp_item_get_offset_y (item) + gimp_item_get_height (item) * options->preview_position); } if (orientation != gimp_guide_get_orientation (im_tool->percent_guide) || position != gimp_guide_get_position (im_tool->percent_guide)) { gimp_guide_set_orientation (im_tool->percent_guide, orientation); gimp_image_move_guide (gimp_item_get_image (item), im_tool->percent_guide, position, FALSE); } }
gboolean gimp_image_map_tool_on_guide (GimpImageMapTool *im_tool, const GimpCoords *coords, GimpDisplay *display) { GimpDisplayShell *shell; g_return_val_if_fail (GIMP_IS_IMAGE_MAP_TOOL (im_tool), FALSE); g_return_val_if_fail (coords != NULL, FALSE); g_return_val_if_fail (GIMP_IS_DISPLAY (display), FALSE); shell = gimp_display_get_shell (display); if (im_tool->image_map && im_tool->percent_guide && gimp_display_shell_get_show_guides (shell)) { const gint snap_distance = display->config->snap_distance; GimpOrientationType orientation; gint position; orientation = gimp_guide_get_orientation (im_tool->percent_guide); position = gimp_guide_get_position (im_tool->percent_guide); if (orientation == GIMP_ORIENTATION_HORIZONTAL) { if (fabs (coords->y - position) <= FUNSCALEY (shell, snap_distance)) return TRUE; } else { if (fabs (coords->x - position) <= FUNSCALEX (shell, snap_distance)) return TRUE; } } return FALSE; }
static void gimp_move_tool_draw (GimpDrawTool *draw_tool) { GimpMoveTool *move = GIMP_MOVE_TOOL (draw_tool); if (move->guide) { GimpCanvasItem *item; item = gimp_draw_tool_add_guide (draw_tool, gimp_guide_get_orientation (move->guide), gimp_guide_get_position (move->guide), TRUE); gimp_canvas_item_set_highlight (item, TRUE); } if (move->moving_guide && move->guide_position != GUIDE_POSITION_INVALID) { gimp_draw_tool_add_guide (draw_tool, move->guide_orientation, move->guide_position, FALSE); } }
static void gimp_guide_tool_start (GimpTool *parent_tool, GimpDisplay *display, GimpGuide *guide, GimpOrientationType orientation) { GimpGuideTool *guide_tool; GimpTool *tool; guide_tool = g_object_new (GIMP_TYPE_GUIDE_TOOL, "tool-info", parent_tool->tool_info, NULL); tool = GIMP_TOOL (guide_tool); gimp_display_shell_selection_pause (gimp_display_get_shell (display)); if (guide) { guide_tool->guide = guide; guide_tool->guide_old_position = gimp_guide_get_position (guide); guide_tool->guide_position = gimp_guide_get_position (guide); guide_tool->guide_orientation = gimp_guide_get_orientation (guide); guide_tool->guide_custom = gimp_guide_is_custom (guide); } else { guide_tool->guide = NULL; guide_tool->guide_old_position = 0; guide_tool->guide_position = GIMP_GUIDE_POSITION_UNDEFINED; guide_tool->guide_orientation = orientation; guide_tool->guide_custom = FALSE; } gimp_tool_set_cursor (tool, display, GIMP_CURSOR_MOUSE, GIMP_TOOL_CURSOR_HAND, GIMP_CURSOR_MODIFIER_MOVE); tool_manager_push_tool (display->gimp, tool); tool->display = display; gimp_tool_control_activate (tool->control); gimp_draw_tool_start (GIMP_DRAW_TOOL (guide_tool), display); if (guide_tool->guide) { gimp_tool_push_status_length (tool, display, _("Move Guide: "), SWAP_ORIENT (guide_tool->guide_orientation), guide_tool->guide_position - guide_tool->guide_old_position, NULL); } else { gimp_tool_push_status_length (tool, display, _("Add Guide: "), SWAP_ORIENT (guide_tool->guide_orientation), guide_tool->guide_position, NULL); } }
void gimp_image_crop (GimpImage *image, GimpContext *context, GimpFillType fill_type, gint x, gint y, gint width, gint height, gboolean crop_layers) { GList *list; gint previous_width; gint previous_height; g_return_if_fail (GIMP_IS_IMAGE (image)); g_return_if_fail (GIMP_IS_CONTEXT (context)); previous_width = gimp_image_get_width (image); previous_height = gimp_image_get_height (image); /* Make sure new width and height are non-zero */ if (width < 1 || height < 1) return; gimp_set_busy (image->gimp); g_object_freeze_notify (G_OBJECT (image)); if (crop_layers) gimp_image_undo_group_start (image, GIMP_UNDO_GROUP_IMAGE_CROP, C_("undo-type", "Crop Image")); else gimp_image_undo_group_start (image, GIMP_UNDO_GROUP_IMAGE_RESIZE, C_("undo-type", "Resize Image")); /* Push the image size to the stack */ gimp_image_undo_push_image_size (image, NULL, x, y, width, height); /* Set the new width and height */ g_object_set (image, "width", width, "height", height, NULL); /* Resize all channels */ for (list = gimp_image_get_channel_iter (image); list; list = g_list_next (list)) { GimpItem *item = list->data; gimp_item_resize (item, context, GIMP_FILL_TRANSPARENT, width, height, -x, -y); } /* Resize all vectors */ for (list = gimp_image_get_vectors_iter (image); list; list = g_list_next (list)) { GimpItem *item = list->data; gimp_item_resize (item, context, GIMP_FILL_TRANSPARENT, width, height, -x, -y); } /* Don't forget the selection mask! */ gimp_item_resize (GIMP_ITEM (gimp_image_get_mask (image)), context, GIMP_FILL_TRANSPARENT, width, height, -x, -y); /* crop all layers */ list = gimp_image_get_layer_iter (image); while (list) { GimpItem *item = list->data; list = g_list_next (list); gimp_item_translate (item, -x, -y, TRUE); if (crop_layers) { gint off_x, off_y; gint lx1, ly1, lx2, ly2; gimp_item_get_offset (item, &off_x, &off_y); lx1 = CLAMP (off_x, 0, gimp_image_get_width (image)); ly1 = CLAMP (off_y, 0, gimp_image_get_height (image)); lx2 = CLAMP (gimp_item_get_width (item) + off_x, 0, gimp_image_get_width (image)); ly2 = CLAMP (gimp_item_get_height (item) + off_y, 0, gimp_image_get_height (image)); width = lx2 - lx1; height = ly2 - ly1; if (width > 0 && height > 0) { gimp_item_resize (item, context, fill_type, width, height, -(lx1 - off_x), -(ly1 - off_y)); } else { gimp_image_remove_layer (image, GIMP_LAYER (item), TRUE, NULL); } } } /* Reposition or remove guides */ list = gimp_image_get_guides (image); while (list) { GimpGuide *guide = list->data; gboolean remove_guide = FALSE; gint position = gimp_guide_get_position (guide); list = g_list_next (list); switch (gimp_guide_get_orientation (guide)) { case GIMP_ORIENTATION_HORIZONTAL: position -= y; if ((position < 0) || (position > height)) remove_guide = TRUE; break; case GIMP_ORIENTATION_VERTICAL: position -= x; if ((position < 0) || (position > width)) remove_guide = TRUE; break; default: break; } if (remove_guide) gimp_image_remove_guide (image, guide, TRUE); else if (position != gimp_guide_get_position (guide)) gimp_image_move_guide (image, guide, position, TRUE); } /* Reposition or remove sample points */ list = gimp_image_get_sample_points (image); while (list) { GimpSamplePoint *sample_point = list->data; gboolean remove_sample_point = FALSE; gint old_x; gint old_y; gint new_x; gint new_y; list = g_list_next (list); gimp_sample_point_get_position (sample_point, &old_x, &old_y); new_x = old_x; new_y = old_y; new_y -= y; if ((new_y < 0) || (new_y > height)) remove_sample_point = TRUE; new_x -= x; if ((new_x < 0) || (new_x > width)) remove_sample_point = TRUE; if (remove_sample_point) gimp_image_remove_sample_point (image, sample_point, TRUE); else if (new_x != old_x || new_y != old_y) gimp_image_move_sample_point (image, sample_point, new_x, new_y, TRUE); } gimp_image_undo_group_end (image); gimp_image_size_changed_detailed (image, -x, -y, previous_width, previous_height); g_object_thaw_notify (G_OBJECT (image)); gimp_unset_busy (image->gimp); }
void gimp_image_scale (GimpImage *image, gint new_width, gint new_height, GimpInterpolationType interpolation_type, GimpProgress *progress) { GimpProgress *sub_progress; GList *all_layers; GList *all_channels; GList *all_vectors; GList *list; gint old_width; gint old_height; gint offset_x; gint offset_y; gdouble img_scale_w = 1.0; gdouble img_scale_h = 1.0; gint progress_steps; gint progress_current = 0; g_return_if_fail (GIMP_IS_IMAGE (image)); g_return_if_fail (new_width > 0 && new_height > 0); g_return_if_fail (progress == NULL || GIMP_IS_PROGRESS (progress)); gimp_set_busy (image->gimp); sub_progress = gimp_sub_progress_new (progress); all_layers = gimp_image_get_layer_list (image); all_channels = gimp_image_get_channel_list (image); all_vectors = gimp_image_get_vectors_list (image); progress_steps = (g_list_length (all_layers) + g_list_length (all_channels) + g_list_length (all_vectors) + 1 /* selection */); g_object_freeze_notify (G_OBJECT (image)); gimp_image_undo_group_start (image, GIMP_UNDO_GROUP_IMAGE_SCALE, C_("undo-type", "Scale Image")); old_width = gimp_image_get_width (image); old_height = gimp_image_get_height (image); img_scale_w = (gdouble) new_width / (gdouble) old_width; img_scale_h = (gdouble) new_height / (gdouble) old_height; offset_x = (old_width - new_width) / 2; offset_y = (old_height - new_height) / 2; /* Push the image size to the stack */ gimp_image_undo_push_image_size (image, NULL, offset_x, offset_y, new_width, new_height); /* Set the new width and height early, so below image item setters * (esp. guides and sample points) don't choke about moving stuff * out of the image */ g_object_set (image, "width", new_width, "height", new_height, NULL); /* Scale all channels */ for (list = all_channels; list; list = g_list_next (list)) { GimpItem *item = list->data; gimp_sub_progress_set_step (GIMP_SUB_PROGRESS (sub_progress), progress_current++, progress_steps); gimp_item_scale (item, new_width, new_height, 0, 0, interpolation_type, sub_progress); } /* Scale all vectors */ for (list = all_vectors; list; list = g_list_next (list)) { GimpItem *item = list->data; gimp_sub_progress_set_step (GIMP_SUB_PROGRESS (sub_progress), progress_current++, progress_steps); gimp_item_scale (item, new_width, new_height, 0, 0, interpolation_type, sub_progress); } /* Don't forget the selection mask! */ gimp_sub_progress_set_step (GIMP_SUB_PROGRESS (sub_progress), progress_current++, progress_steps); gimp_item_scale (GIMP_ITEM (gimp_image_get_mask (image)), new_width, new_height, 0, 0, interpolation_type, sub_progress); /* Scale all layers */ for (list = all_layers; list; list = g_list_next (list)) { GimpItem *item = list->data; gimp_sub_progress_set_step (GIMP_SUB_PROGRESS (sub_progress), progress_current++, progress_steps); /* group layers are updated automatically */ if (gimp_viewable_get_children (GIMP_VIEWABLE (item))) { gimp_group_layer_suspend_resize (GIMP_GROUP_LAYER (item), FALSE); continue; } if (! gimp_item_scale_by_factors (item, img_scale_w, img_scale_h, interpolation_type, sub_progress)) { /* Since 0 < img_scale_w, img_scale_h, failure due to one or more * vanishing scaled layer dimensions. Implicit delete implemented * here. Upstream warning implemented in resize_check_layer_scaling(), * which offers the user the chance to bail out. */ gimp_image_remove_layer (image, GIMP_LAYER (item), TRUE, NULL); } } for (list = all_layers; list; list = g_list_next (list)) if (gimp_viewable_get_children (list->data)) gimp_group_layer_resume_resize (list->data, FALSE); /* Scale all Guides */ for (list = gimp_image_get_guides (image); list; list = g_list_next (list)) { GimpGuide *guide = list->data; gint position = gimp_guide_get_position (guide); switch (gimp_guide_get_orientation (guide)) { case GIMP_ORIENTATION_HORIZONTAL: gimp_image_move_guide (image, guide, (position * new_height) / old_height, TRUE); break; case GIMP_ORIENTATION_VERTICAL: gimp_image_move_guide (image, guide, (position * new_width) / old_width, TRUE); break; default: break; } } /* Scale all sample points */ for (list = gimp_image_get_sample_points (image); list; list = g_list_next (list)) { GimpSamplePoint *sample_point = list->data; gimp_image_move_sample_point (image, sample_point, sample_point->x * new_width / old_width, sample_point->y * new_height / old_height, TRUE); } gimp_image_undo_group_end (image); g_list_free (all_layers); g_list_free (all_channels); g_list_free (all_vectors); g_object_unref (sub_progress); gimp_image_size_changed_detailed (image, -offset_x, -offset_y, old_width, old_height); g_object_thaw_notify (G_OBJECT (image)); gimp_unset_busy (image->gimp); }
gboolean gimp_image_snap_point (GimpImage *image, gdouble x, gdouble y, gdouble *tx, gdouble *ty, gdouble epsilon_x, gdouble epsilon_y, gboolean snap_to_guides, gboolean snap_to_grid, gboolean snap_to_canvas, gboolean snap_to_vectors) { gdouble mindist_x = G_MAXDOUBLE; gdouble mindist_y = G_MAXDOUBLE; gboolean snapped = FALSE; g_return_val_if_fail (GIMP_IS_IMAGE (image), FALSE); g_return_val_if_fail (tx != NULL, FALSE); g_return_val_if_fail (ty != NULL, FALSE); *tx = x; *ty = y; if (! gimp_image_get_guides (image)) snap_to_guides = FALSE; if (! gimp_image_get_grid (image)) snap_to_grid = FALSE; if (! gimp_image_get_active_vectors (image)) snap_to_vectors = FALSE; if (! (snap_to_guides || snap_to_grid || snap_to_canvas || snap_to_vectors)) return FALSE; if (x < -epsilon_x || x >= (gimp_image_get_width (image) + epsilon_x) || y < -epsilon_y || y >= (gimp_image_get_height (image) + epsilon_y)) { return FALSE; } if (snap_to_guides) { GList *list; for (list = gimp_image_get_guides (image); list; list = g_list_next (list)) { GimpGuide *guide = list->data; gint position = gimp_guide_get_position (guide); switch (gimp_guide_get_orientation (guide)) { case GIMP_ORIENTATION_HORIZONTAL: snapped |= gimp_image_snap_distance (y, position, epsilon_y, &mindist_y, ty); break; case GIMP_ORIENTATION_VERTICAL: snapped |= gimp_image_snap_distance (x, position, epsilon_x, &mindist_x, tx); break; default: break; } } } if (snap_to_grid) { GimpGrid *grid = gimp_image_get_grid (image); gdouble xspacing, yspacing; gdouble xoffset, yoffset; gdouble i; gimp_grid_get_spacing (grid, &xspacing, &yspacing); gimp_grid_get_offset (grid, &xoffset, &yoffset); while (xoffset > xspacing) xoffset -= xspacing; while (yoffset > yspacing) yoffset -= yspacing; for (i = xoffset; i <= gimp_image_get_width (image); i += xspacing) { if (i < 0) continue; snapped |= gimp_image_snap_distance (x, i, epsilon_x, &mindist_x, tx); } for (i = yoffset; i <= gimp_image_get_height (image); i += yspacing) { if (i < 0) continue; snapped |= gimp_image_snap_distance (y, i, epsilon_y, &mindist_y, ty); } } if (snap_to_canvas) { snapped |= gimp_image_snap_distance (x, 0, epsilon_x, &mindist_x, tx); snapped |= gimp_image_snap_distance (x, gimp_image_get_width (image), epsilon_x, &mindist_x, tx); snapped |= gimp_image_snap_distance (y, 0, epsilon_y, &mindist_y, ty); snapped |= gimp_image_snap_distance (y, gimp_image_get_height (image), epsilon_y, &mindist_y, ty); } if (snap_to_vectors) { GimpVectors *vectors = gimp_image_get_active_vectors (image); GimpStroke *stroke = NULL; GimpCoords coords = { 0, 0, 0, 0, 0 }; coords.x = x; coords.y = y; while ((stroke = gimp_vectors_stroke_get_next (vectors, stroke))) { GimpCoords nearest; if (gimp_stroke_nearest_point_get (stroke, &coords, 1.0, &nearest, NULL, NULL, NULL) >= 0) { snapped |= gimp_image_snap_distance (x, nearest.x, epsilon_x, &mindist_x, tx); snapped |= gimp_image_snap_distance (y, nearest.y, epsilon_y, &mindist_y, ty); } } } return snapped; }
gboolean gimp_image_snap_x (GimpImage *image, gdouble x, gdouble *tx, gdouble epsilon_x, gboolean snap_to_guides, gboolean snap_to_grid, gboolean snap_to_canvas) { gdouble mindist = G_MAXDOUBLE; gboolean snapped = FALSE; g_return_val_if_fail (GIMP_IS_IMAGE (image), FALSE); g_return_val_if_fail (tx != NULL, FALSE); *tx = x; if (! gimp_image_get_guides (image)) snap_to_guides = FALSE; if (! gimp_image_get_grid (image)) snap_to_grid = FALSE; if (! (snap_to_guides || snap_to_grid || snap_to_canvas)) return FALSE; if (x < -epsilon_x || x >= (gimp_image_get_width (image) + epsilon_x)) return FALSE; if (snap_to_guides) { GList *list; for (list = gimp_image_get_guides (image); list; list = g_list_next (list)) { GimpGuide *guide = list->data; gint position = gimp_guide_get_position (guide); if (gimp_guide_get_orientation (guide) == GIMP_ORIENTATION_VERTICAL) { snapped |= gimp_image_snap_distance (x, position, epsilon_x, &mindist, tx); } } } if (snap_to_grid) { GimpGrid *grid = gimp_image_get_grid (image); gdouble xspacing; gdouble xoffset; gdouble i; gimp_grid_get_spacing (grid, &xspacing, NULL); gimp_grid_get_offset (grid, &xoffset, NULL); /* the snap-to-grid part could probably be rewritten */ while (xoffset > xspacing) xoffset -= xspacing; for (i = xoffset; i <= gimp_image_get_width (image); i += xspacing) { if (i < 0) continue; snapped |= gimp_image_snap_distance (x, i, epsilon_x, &mindist, tx); } } if (snap_to_canvas) { snapped |= gimp_image_snap_distance (x, 0, epsilon_x, &mindist, tx); snapped |= gimp_image_snap_distance (x, gimp_image_get_width (image), epsilon_x, &mindist, tx); } return snapped; }
void gimp_image_flip (GimpImage *image, GimpContext *context, GimpOrientationType flip_type, GimpProgress *progress) { GList *list; gdouble axis; gdouble progress_max; gdouble progress_current = 1.0; g_return_if_fail (GIMP_IS_IMAGE (image)); g_return_if_fail (GIMP_IS_CONTEXT (context)); g_return_if_fail (progress == NULL || GIMP_IS_PROGRESS (progress)); gimp_set_busy (image->gimp); switch (flip_type) { case GIMP_ORIENTATION_HORIZONTAL: axis = (gdouble) gimp_image_get_width (image) / 2.0; break; case GIMP_ORIENTATION_VERTICAL: axis = (gdouble) gimp_image_get_height (image) / 2.0; break; default: g_warning ("%s: unknown flip_type", G_STRFUNC); return; } progress_max = (image->channels->num_children + image->layers->num_children + image->vectors->num_children + 1 /* selection */); gimp_image_undo_group_start (image, GIMP_UNDO_GROUP_IMAGE_FLIP, NULL); /* Flip all channels */ for (list = GIMP_LIST (image->channels)->list; list; list = g_list_next (list)) { GimpItem *item = list->data; gimp_item_flip (item, context, flip_type, axis, TRUE); if (progress) gimp_progress_set_value (progress, progress_current++ / progress_max); } /* Flip all vectors */ for (list = GIMP_LIST (image->vectors)->list; list; list = g_list_next (list)) { GimpItem *item = list->data; gimp_item_flip (item, context, flip_type, axis, FALSE); if (progress) gimp_progress_set_value (progress, progress_current++ / progress_max); } /* Don't forget the selection mask! */ gimp_item_flip (GIMP_ITEM (gimp_image_get_mask (image)), context, flip_type, axis, TRUE); if (progress) gimp_progress_set_value (progress, progress_current++ / progress_max); /* Flip all layers */ for (list = GIMP_LIST (image->layers)->list; list; list = g_list_next (list)) { GimpItem *item = list->data; gimp_item_flip (item, context, flip_type, axis, FALSE); if (progress) gimp_progress_set_value (progress, progress_current++ / progress_max); } /* Flip all Guides */ for (list = gimp_image_get_guides (image); list; list = g_list_next (list)) { GimpGuide *guide = list->data; gint position = gimp_guide_get_position (guide); switch (gimp_guide_get_orientation (guide)) { case GIMP_ORIENTATION_HORIZONTAL: if (flip_type == GIMP_ORIENTATION_VERTICAL) gimp_image_move_guide (image, guide, gimp_image_get_height (image) - position, TRUE); break; case GIMP_ORIENTATION_VERTICAL: if (flip_type == GIMP_ORIENTATION_HORIZONTAL) gimp_image_move_guide (image, guide, gimp_image_get_width (image) - position, TRUE); break; default: break; } } /* Flip all sample points */ for (list = gimp_image_get_sample_points (image); list; list = g_list_next (list)) { GimpSamplePoint *sample_point = list->data; if (flip_type == GIMP_ORIENTATION_VERTICAL) gimp_image_move_sample_point (image, sample_point, sample_point->x, gimp_image_get_height (image) - sample_point->y, TRUE); if (flip_type == GIMP_ORIENTATION_HORIZONTAL) gimp_image_move_sample_point (image, sample_point, gimp_image_get_width (image) - sample_point->x, sample_point->y, TRUE); } gimp_image_undo_group_end (image); gimp_unset_busy (image->gimp); }
void gimp_image_crop (GimpImage *image, GimpContext *context, gint x1, gint y1, gint x2, gint y2, gboolean active_layer_only, gboolean crop_layers) { gint width, height; gint previous_width, previous_height; g_return_if_fail (GIMP_IS_IMAGE (image)); g_return_if_fail (GIMP_IS_CONTEXT (context)); previous_width = gimp_image_get_width (image); previous_height = gimp_image_get_height (image); width = x2 - x1; height = y2 - y1; /* Make sure new width and height are non-zero */ if (width < 1 || height < 1) return; gimp_set_busy (image->gimp); if (active_layer_only) { GimpLayer *layer; gint off_x, off_y; layer = gimp_image_get_active_layer (image); gimp_item_offsets (GIMP_ITEM (layer), &off_x, &off_y); off_x -= x1; off_y -= y1; gimp_item_resize (GIMP_ITEM (layer), context, width, height, off_x, off_y); } else { GimpItem *item; GList *list; g_object_freeze_notify (G_OBJECT (image)); if (crop_layers) gimp_image_undo_group_start (image, GIMP_UNDO_GROUP_IMAGE_CROP, C_("command", "Crop Image")); else gimp_image_undo_group_start (image, GIMP_UNDO_GROUP_IMAGE_RESIZE, _("Resize Image")); /* Push the image size to the stack */ gimp_image_undo_push_image_size (image, NULL, x1, y1, width, height); /* Set the new width and height */ g_object_set (image, "width", width, "height", height, NULL); /* Resize all channels */ for (list = GIMP_LIST (image->channels)->list; list; list = g_list_next (list)) { item = (GimpItem *) list->data; gimp_item_resize (item, context, width, height, -x1, -y1); } /* Resize all vectors */ for (list = GIMP_LIST (image->vectors)->list; list; list = g_list_next (list)) { item = (GimpItem *) list->data; gimp_item_resize (item, context, width, height, -x1, -y1); } /* Don't forget the selection mask! */ gimp_item_resize (GIMP_ITEM (gimp_image_get_mask (image)), context, width, height, -x1, -y1); /* crop all layers */ list = GIMP_LIST (image->layers)->list; while (list) { item = (GimpItem *) list->data; list = g_list_next (list); gimp_item_translate (item, -x1, -y1, TRUE); if (crop_layers) { gint off_x, off_y; gint lx1, ly1, lx2, ly2; gimp_item_offsets (item, &off_x, &off_y); lx1 = CLAMP (off_x, 0, gimp_image_get_width (image)); ly1 = CLAMP (off_y, 0, gimp_image_get_height (image)); lx2 = CLAMP (gimp_item_width (item) + off_x, 0, gimp_image_get_width (image)); ly2 = CLAMP (gimp_item_height (item) + off_y, 0, gimp_image_get_height (image)); width = lx2 - lx1; height = ly2 - ly1; if (width > 0 && height > 0) gimp_item_resize (item, context, width, height, -(lx1 - off_x), -(ly1 - off_y)); else gimp_image_remove_layer (image, GIMP_LAYER (item)); } } /* Reposition or remove all guides */ list = gimp_image_get_guides (image); while (list) { GimpGuide *guide = list->data; gboolean remove_guide = FALSE; gint position = gimp_guide_get_position (guide); list = g_list_next (list); switch (gimp_guide_get_orientation (guide)) { case GIMP_ORIENTATION_HORIZONTAL: if ((position < y1) || (position > y2)) remove_guide = TRUE; else position -= y1; break; case GIMP_ORIENTATION_VERTICAL: if ((position < x1) || (position > x2)) remove_guide = TRUE; else position -= x1; break; default: break; } if (remove_guide) gimp_image_remove_guide (image, guide, TRUE); else if (position != gimp_guide_get_position (guide)) gimp_image_move_guide (image, guide, position, TRUE); } /* Reposition or remove sample points */ list = gimp_image_get_sample_points (image); while (list) { GimpSamplePoint *sample_point = list->data; gboolean remove_sample_point = FALSE; gint new_x = sample_point->x; gint new_y = sample_point->y; list = g_list_next (list); new_y -= y1; if ((sample_point->y < y1) || (sample_point->y > y2)) remove_sample_point = TRUE; new_x -= x1; if ((sample_point->x < x1) || (sample_point->x > x2)) remove_sample_point = TRUE; if (remove_sample_point) gimp_image_remove_sample_point (image, sample_point, TRUE); else if (new_x != sample_point->x || new_y != sample_point->y) gimp_image_move_sample_point (image, sample_point, new_x, new_y, TRUE); } gimp_image_undo_group_end (image); gimp_image_update (image, 0, 0, gimp_image_get_width (image), gimp_image_get_height (image)); gimp_image_size_changed_detailed (image, -x1, -y1, previous_width, previous_height); g_object_thaw_notify (G_OBJECT (image)); } gimp_unset_busy (image->gimp); }
static void gimp_move_tool_button_press (GimpTool *tool, const GimpCoords *coords, guint32 time, GdkModifierType state, GimpButtonPressType press_type, GimpDisplay *display) { GimpMoveTool *move = GIMP_MOVE_TOOL (tool); GimpMoveOptions *options = GIMP_MOVE_TOOL_GET_OPTIONS (tool); GimpDisplayShell *shell = gimp_display_get_shell (display); GimpImage *image = gimp_display_get_image (display); tool->display = display; move->floating_layer = NULL; move->guide = NULL; move->moving_guide = FALSE; move->old_active_layer = NULL; move->old_active_vectors = NULL; if (! options->move_current) { if (options->move_type == GIMP_TRANSFORM_TYPE_PATH) { GimpVectors *vectors; if (gimp_draw_tool_on_vectors (GIMP_DRAW_TOOL (tool), display, coords, 7, 7, NULL, NULL, NULL, NULL, NULL, &vectors)) { move->old_active_vectors = gimp_image_get_active_vectors (image); gimp_image_set_active_vectors (image, vectors); } else { /* no path picked */ return; } } else if (options->move_type == GIMP_TRANSFORM_TYPE_LAYER) { GimpGuide *guide; GimpLayer *layer; const gint snap_distance = display->config->snap_distance; if (gimp_display_shell_get_show_guides (shell) && (guide = gimp_image_find_guide (image, coords->x, coords->y, FUNSCALEX (shell, snap_distance), FUNSCALEY (shell, snap_distance)))) { move->guide = guide; move->moving_guide = TRUE; move->guide_position = gimp_guide_get_position (guide); move->guide_orientation = gimp_guide_get_orientation (guide); gimp_tool_control_set_scroll_lock (tool->control, TRUE); gimp_tool_control_set_precision (tool->control, GIMP_CURSOR_PRECISION_PIXEL_BORDER); gimp_tool_control_activate (tool->control); gimp_display_shell_selection_pause (shell); gimp_draw_tool_start (GIMP_DRAW_TOOL (tool), display); gimp_tool_push_status_length (tool, display, _("Move Guide: "), SWAP_ORIENT (move->guide_orientation), move->guide_position, NULL); return; } else if ((layer = gimp_image_pick_layer (image, coords->x, coords->y))) { if (gimp_image_get_floating_selection (image) && ! gimp_layer_is_floating_sel (layer)) { /* If there is a floating selection, and this aint it, * use the move tool to anchor it. */ move->floating_layer = gimp_image_get_floating_selection (image); gimp_tool_control_activate (tool->control); return; } else { move->old_active_layer = gimp_image_get_active_layer (image); gimp_image_set_active_layer (image, layer); } } else { /* no guide and no layer picked */ return; } } } switch (options->move_type) { case GIMP_TRANSFORM_TYPE_PATH: if (gimp_image_get_active_vectors (image)) gimp_edit_selection_tool_start (tool, display, coords, GIMP_TRANSLATE_MODE_VECTORS, TRUE); break; case GIMP_TRANSFORM_TYPE_SELECTION: if (! gimp_channel_is_empty (gimp_image_get_mask (image))) gimp_edit_selection_tool_start (tool, display, coords, GIMP_TRANSLATE_MODE_MASK, TRUE); break; case GIMP_TRANSFORM_TYPE_LAYER: { GimpDrawable *drawable = gimp_image_get_active_drawable (image); if (GIMP_IS_LAYER_MASK (drawable)) gimp_edit_selection_tool_start (tool, display, coords, GIMP_TRANSLATE_MODE_LAYER_MASK, TRUE); else if (GIMP_IS_CHANNEL (drawable)) gimp_edit_selection_tool_start (tool, display, coords, GIMP_TRANSLATE_MODE_CHANNEL, TRUE); else if (GIMP_IS_LAYER (drawable)) gimp_edit_selection_tool_start (tool, display, coords, GIMP_TRANSLATE_MODE_LAYER, TRUE); } break; } }
void gimp_image_resize_with_layers (GimpImage *image, GimpContext *context, gint new_width, gint new_height, gint offset_x, gint offset_y, GimpItemSet layer_set, gboolean resize_text_layers, GimpProgress *progress) { GList *list; GList *resize_layers; gdouble progress_max; gdouble progress_current = 1.0; gint old_width, old_height; g_return_if_fail (GIMP_IS_IMAGE (image)); g_return_if_fail (GIMP_IS_CONTEXT (context)); g_return_if_fail (new_width > 0 && new_height > 0); g_return_if_fail (progress == NULL || GIMP_IS_PROGRESS (progress)); gimp_set_busy (image->gimp); g_object_freeze_notify (G_OBJECT (image)); gimp_image_undo_group_start (image, GIMP_UNDO_GROUP_IMAGE_RESIZE, C_("undo-type", "Resize Image")); resize_layers = gimp_image_item_list_get_list (image, NULL, GIMP_ITEM_TYPE_LAYERS, layer_set); progress_max = (gimp_container_get_n_children (gimp_image_get_layers (image)) + gimp_container_get_n_children (gimp_image_get_channels (image)) + gimp_container_get_n_children (gimp_image_get_vectors (image)) + g_list_length (resize_layers) + 1 /* selection */); old_width = gimp_image_get_width (image); old_height = gimp_image_get_height (image); /* Push the image size to the stack */ gimp_image_undo_push_image_size (image, NULL, -offset_x, -offset_y, new_width, new_height); /* Set the new width and height */ g_object_set (image, "width", new_width, "height", new_height, NULL); /* Resize all channels */ for (list = gimp_image_get_channel_iter (image); list; list = g_list_next (list)) { GimpItem *item = list->data; gimp_item_resize (item, context, new_width, new_height, offset_x, offset_y); if (progress) gimp_progress_set_value (progress, progress_current++ / progress_max); } /* Resize all vectors */ for (list = gimp_image_get_vectors_iter (image); list; list = g_list_next (list)) { GimpItem *item = list->data; gimp_item_resize (item, context, new_width, new_height, offset_x, offset_y); if (progress) gimp_progress_set_value (progress, progress_current++ / progress_max); } /* Don't forget the selection mask! */ gimp_item_resize (GIMP_ITEM (gimp_image_get_mask (image)), context, new_width, new_height, offset_x, offset_y); if (progress) gimp_progress_set_value (progress, progress_current++ / progress_max); /* Reposition all layers */ for (list = gimp_image_get_layer_iter (image); list; list = g_list_next (list)) { GimpItem *item = list->data; gimp_item_translate (item, offset_x, offset_y, TRUE); if (progress) gimp_progress_set_value (progress, progress_current++ / progress_max); } /* Resize all resize_layers to image size */ for (list = resize_layers; list; list = g_list_next (list)) { GimpItem *item = list->data; gint old_offset_x; gint old_offset_y; /* group layers can't be resized here */ if (gimp_viewable_get_children (GIMP_VIEWABLE (item))) continue; if (! resize_text_layers && gimp_item_is_text_layer (item)) continue; gimp_item_get_offset (item, &old_offset_x, &old_offset_y); gimp_item_resize (item, context, new_width, new_height, old_offset_x, old_offset_y); if (progress) gimp_progress_set_value (progress, progress_current++ / progress_max); } g_list_free (resize_layers); /* Reposition or remove all guides */ list = gimp_image_get_guides (image); while (list) { GimpGuide *guide = list->data; gboolean remove_guide = FALSE; gint new_position = gimp_guide_get_position (guide); list = g_list_next (list); switch (gimp_guide_get_orientation (guide)) { case GIMP_ORIENTATION_HORIZONTAL: new_position += offset_y; if (new_position < 0 || new_position > new_height) remove_guide = TRUE; break; case GIMP_ORIENTATION_VERTICAL: new_position += offset_x; if (new_position < 0 || new_position > new_width) remove_guide = TRUE; break; default: break; } if (remove_guide) gimp_image_remove_guide (image, guide, TRUE); else if (new_position != gimp_guide_get_position (guide)) gimp_image_move_guide (image, guide, new_position, TRUE); } /* Reposition or remove sample points */ list = gimp_image_get_sample_points (image); while (list) { GimpSamplePoint *sample_point = list->data; gboolean remove_sample_point = FALSE; gint new_x = sample_point->x; gint new_y = sample_point->y; list = g_list_next (list); new_y += offset_y; if ((sample_point->y < 0) || (sample_point->y > new_height)) remove_sample_point = TRUE; new_x += offset_x; if ((sample_point->x < 0) || (sample_point->x > new_width)) remove_sample_point = TRUE; if (remove_sample_point) gimp_image_remove_sample_point (image, sample_point, TRUE); else if (new_x != sample_point->x || new_y != sample_point->y) gimp_image_move_sample_point (image, sample_point, new_x, new_y, TRUE); } gimp_image_undo_group_end (image); gimp_image_size_changed_detailed (image, offset_x, offset_y, old_width, old_height); g_object_thaw_notify (G_OBJECT (image)); gimp_unset_busy (image->gimp); }
static void compute_offset (GObject *object, GimpAlignmentType alignment) { gint object_offset_x = 0; gint object_offset_y = 0; gint object_height = 0; gint object_width = 0; gint offset = 0; if (GIMP_IS_IMAGE (object)) { GimpImage *image = GIMP_IMAGE (object); object_offset_x = 0; object_offset_y = 0; object_height = gimp_image_get_height (image); object_width = gimp_image_get_width (image); } else if (GIMP_IS_CHANNEL (object)) { /* for channels, we use the bounds of the visible area, not the layer bounds. This includes the selection channel */ GimpChannel *channel = GIMP_CHANNEL (object); if (gimp_channel_is_empty (channel)) { /* fall back on using the offsets instead */ GimpItem *item = GIMP_ITEM (object); gimp_item_get_offset (item, &object_offset_x, &object_offset_y); object_width = gimp_item_get_width (item); object_height = gimp_item_get_height (item); } else { gint x1, x2, y1, y2; gimp_channel_bounds (channel, &x1, &y1, &x2, &y2); object_offset_x = x1; object_offset_y = y1; object_width = x2 - x1; object_height = y2 - y1; } } else if (GIMP_IS_ITEM (object)) { GimpItem *item = GIMP_ITEM (object); if (GIMP_IS_VECTORS (object)) { gdouble x1_f, y1_f, x2_f, y2_f; gimp_vectors_bounds (GIMP_VECTORS (item), &x1_f, &y1_f, &x2_f, &y2_f); object_offset_x = ROUND (x1_f); object_offset_y = ROUND (y1_f); object_height = ROUND (y2_f - y1_f); object_width = ROUND (x2_f - x1_f); } else { gimp_item_get_offset (item, &object_offset_x, &object_offset_y); object_width = gimp_item_get_width (item); object_height = gimp_item_get_height (item); } } else if (GIMP_IS_GUIDE (object)) { GimpGuide *guide = GIMP_GUIDE (object); switch (gimp_guide_get_orientation (guide)) { case GIMP_ORIENTATION_VERTICAL: object_offset_x = gimp_guide_get_position (guide); object_width = 0; break; case GIMP_ORIENTATION_HORIZONTAL: object_offset_y = gimp_guide_get_position (guide); object_height = 0; break; default: break; } } else { g_printerr ("Alignment object is not an image, item or guide.\n"); } switch (alignment) { case GIMP_ALIGN_LEFT: case GIMP_ARRANGE_LEFT: offset = object_offset_x; break; case GIMP_ALIGN_HCENTER: case GIMP_ARRANGE_HCENTER: offset = object_offset_x + object_width/2; break; case GIMP_ALIGN_RIGHT: case GIMP_ARRANGE_RIGHT: offset = object_offset_x + object_width; break; case GIMP_ALIGN_TOP: case GIMP_ARRANGE_TOP: offset = object_offset_y; break; case GIMP_ALIGN_VCENTER: case GIMP_ARRANGE_VCENTER: offset = object_offset_y + object_height/2; break; case GIMP_ALIGN_BOTTOM: case GIMP_ARRANGE_BOTTOM: offset = object_offset_y + object_height; break; default: g_assert_not_reached (); } g_object_set_data (object, "align-offset", GINT_TO_POINTER (offset)); }
static GeglBuffer * gimp_flip_tool_transform (GimpTransformTool *tr_tool, GimpItem *active_item, GeglBuffer *orig_buffer, gint orig_offset_x, gint orig_offset_y, GimpColorProfile **buffer_profile, gint *new_offset_x, gint *new_offset_y) { GimpFlipTool *flip = GIMP_FLIP_TOOL (tr_tool); GimpFlipOptions *options = GIMP_FLIP_TOOL_GET_OPTIONS (tr_tool); GimpTransformOptions *tr_options = GIMP_TRANSFORM_TOOL_GET_OPTIONS (tr_tool); GimpContext *context = GIMP_CONTEXT (options); GimpOrientationType flip_type = GIMP_ORIENTATION_UNKNOWN; gdouble axis = 0.0; gboolean clip_result = FALSE; GeglBuffer *ret = NULL; flip_type = gimp_flip_tool_get_flip_type (flip); if (flip->guide) { axis = gimp_guide_get_position (flip->guide); } else { switch (flip_type) { case GIMP_ORIENTATION_HORIZONTAL: axis = ((gdouble) tr_tool->x1 + (gdouble) (tr_tool->x2 - tr_tool->x1) / 2.0); break; case GIMP_ORIENTATION_VERTICAL: axis = ((gdouble) tr_tool->y1 + (gdouble) (tr_tool->y2 - tr_tool->y1) / 2.0); break; default: break; } } switch (tr_options->clip) { case GIMP_TRANSFORM_RESIZE_ADJUST: clip_result = FALSE; break; case GIMP_TRANSFORM_RESIZE_CLIP: clip_result = TRUE; break; default: g_return_val_if_reached (NULL); } if (orig_buffer) { /* this happens when transforming a selection cut out of a * normal drawable, or the selection */ /* always clip the selection and unfloated channels * so they keep their size */ if (GIMP_IS_CHANNEL (active_item) && ! babl_format_has_alpha (gegl_buffer_get_format (orig_buffer))) clip_result = TRUE; ret = gimp_drawable_transform_buffer_flip (GIMP_DRAWABLE (active_item), context, orig_buffer, orig_offset_x, orig_offset_y, flip_type, axis, clip_result, buffer_profile, new_offset_x, new_offset_y); } else { /* this happens for entire drawables, paths and layer groups */ /* always clip layer masks so they keep their size */ if (GIMP_IS_CHANNEL (active_item)) clip_result = TRUE; if (gimp_item_get_linked (active_item)) { gimp_item_linked_flip (active_item, context, flip_type, axis, clip_result); } else { gimp_item_flip (active_item, context, flip_type, axis, clip_result); } } return ret; }
GimpImage * gimp_image_duplicate (GimpImage *image) { GimpImage *new_image; GimpLayer *floating_layer; GList *list; GimpLayer *active_layer = NULL; GimpChannel *active_channel = NULL; GimpVectors *active_vectors = NULL; GimpDrawable *new_floating_sel_drawable = NULL; GimpDrawable *floating_sel_drawable = NULL; gchar *filename; gint count; g_return_val_if_fail (GIMP_IS_IMAGE (image), NULL); gimp_set_busy_until_idle (image->gimp); /* Create a new image */ new_image = gimp_create_image (image->gimp, image->width, image->height, image->base_type, FALSE); gimp_image_undo_disable (new_image); /* Store the folder to be used by the save dialog */ filename = gimp_image_get_filename (image); if (filename) { g_object_set_data_full (G_OBJECT (new_image), "gimp-image-dirname", g_path_get_dirname (filename), (GDestroyNotify) g_free); g_free (filename); } /* Copy the colormap if necessary */ if (new_image->base_type == GIMP_INDEXED) gimp_image_set_colormap (new_image, gimp_image_get_colormap (image), gimp_image_get_colormap_size (image), FALSE); /* Copy resolution information */ new_image->xresolution = image->xresolution; new_image->yresolution = image->yresolution; new_image->resolution_unit = image->resolution_unit; /* Copy floating layer */ floating_layer = gimp_image_floating_sel (image); if (floating_layer) { floating_sel_relax (floating_layer, FALSE); floating_sel_drawable = floating_layer->fs.drawable; floating_layer = NULL; } /* Copy the layers */ for (list = GIMP_LIST (image->layers)->list, count = 0; list; list = g_list_next (list)) { GimpLayer *layer = list->data; GimpLayer *new_layer; new_layer = GIMP_LAYER (gimp_item_convert (GIMP_ITEM (layer), new_image, G_TYPE_FROM_INSTANCE (layer), FALSE)); /* Make sure the copied layer doesn't say: "<old layer> copy" */ gimp_object_set_name (GIMP_OBJECT (new_layer), gimp_object_get_name (GIMP_OBJECT (layer))); /* Make sure that if the layer has a layer mask, * its name isn't screwed up */ if (new_layer->mask) gimp_object_set_name (GIMP_OBJECT (new_layer->mask), gimp_object_get_name (GIMP_OBJECT (layer->mask))); if (gimp_image_get_active_layer (image) == layer) active_layer = new_layer; if (image->floating_sel == layer) floating_layer = new_layer; if (floating_sel_drawable == GIMP_DRAWABLE (layer)) new_floating_sel_drawable = GIMP_DRAWABLE (new_layer); if (floating_layer != new_layer) gimp_image_add_layer (new_image, new_layer, count++); } /* Copy the channels */ for (list = GIMP_LIST (image->channels)->list, count = 0; list; list = g_list_next (list)) { GimpChannel *channel = list->data; GimpChannel *new_channel; new_channel = GIMP_CHANNEL (gimp_item_convert (GIMP_ITEM (channel), new_image, G_TYPE_FROM_INSTANCE (channel), FALSE)); /* Make sure the copied channel doesn't say: "<old channel> copy" */ gimp_object_set_name (GIMP_OBJECT (new_channel), gimp_object_get_name (GIMP_OBJECT (channel))); if (gimp_image_get_active_channel (image) == channel) active_channel = (new_channel); if (floating_sel_drawable == GIMP_DRAWABLE (channel)) new_floating_sel_drawable = GIMP_DRAWABLE (new_channel); gimp_image_add_channel (new_image, new_channel, count++); } /* Copy any vectors */ for (list = GIMP_LIST (image->vectors)->list, count = 0; list; list = g_list_next (list)) { GimpVectors *vectors = list->data; GimpVectors *new_vectors; new_vectors = GIMP_VECTORS (gimp_item_convert (GIMP_ITEM (vectors), new_image, G_TYPE_FROM_INSTANCE (vectors), FALSE)); /* Make sure the copied vectors doesn't say: "<old vectors> copy" */ gimp_object_set_name (GIMP_OBJECT (new_vectors), gimp_object_get_name (GIMP_OBJECT (vectors))); if (gimp_image_get_active_vectors (image) == vectors) active_vectors = new_vectors; gimp_image_add_vectors (new_image, new_vectors, count++); } /* Copy the selection mask */ { TileManager *src_tiles; TileManager *dest_tiles; PixelRegion srcPR, destPR; src_tiles = gimp_drawable_get_tiles (GIMP_DRAWABLE (image->selection_mask)); dest_tiles = gimp_drawable_get_tiles (GIMP_DRAWABLE (new_image->selection_mask)); pixel_region_init (&srcPR, src_tiles, 0, 0, image->width, image->height, FALSE); pixel_region_init (&destPR, dest_tiles, 0, 0, image->width, image->height, TRUE); copy_region (&srcPR, &destPR); new_image->selection_mask->bounds_known = FALSE; new_image->selection_mask->boundary_known = FALSE; } if (floating_layer) floating_sel_attach (floating_layer, new_floating_sel_drawable); /* Set active layer, active channel, active vectors */ if (active_layer) gimp_image_set_active_layer (new_image, active_layer); if (active_channel) gimp_image_set_active_channel (new_image, active_channel); if (active_vectors) gimp_image_set_active_vectors (new_image, active_vectors); /* Copy state of all color channels */ for (count = 0; count < MAX_CHANNELS; count++) { new_image->visible[count] = image->visible[count]; new_image->active[count] = image->active[count]; } /* Copy any guides */ for (list = image->guides; list; list = g_list_next (list)) { GimpGuide *guide = list->data; gint position = gimp_guide_get_position (guide); switch (gimp_guide_get_orientation (guide)) { case GIMP_ORIENTATION_HORIZONTAL: gimp_image_add_hguide (new_image, position, FALSE); break; case GIMP_ORIENTATION_VERTICAL: gimp_image_add_vguide (new_image, position, FALSE); break; default: g_error ("Unknown guide orientation.\n"); } } /* Copy any sample points */ for (list = image->sample_points; list; list = g_list_next (list)) { GimpSamplePoint *sample_point = list->data; gimp_image_add_sample_point_at_pos (new_image, sample_point->x, sample_point->y, FALSE); } /* Copy the grid */ if (image->grid) gimp_image_set_grid (new_image, image->grid, FALSE); /* Copy the quick mask info */ new_image->quick_mask_state = image->quick_mask_state; new_image->quick_mask_inverted = image->quick_mask_inverted; new_image->quick_mask_color = image->quick_mask_color; /* Copy parasites */ if (image->parasites) { g_object_unref (new_image->parasites); new_image->parasites = gimp_parasite_list_copy (image->parasites); } gimp_image_undo_enable (new_image); return new_image; }
static void gimp_align_tool_draw (GimpDrawTool *draw_tool) { GimpAlignTool *align_tool = GIMP_ALIGN_TOOL (draw_tool); GList *list; gint x, y, w, h; /* draw rubber-band rectangle */ x = MIN (align_tool->x2, align_tool->x1); y = MIN (align_tool->y2, align_tool->y1); w = MAX (align_tool->x2, align_tool->x1) - x; h = MAX (align_tool->y2, align_tool->y1) - y; gimp_draw_tool_add_rectangle (draw_tool, FALSE, x, y, w, h); for (list = align_tool->selected_objects; list; list = g_list_next (list)) { if (GIMP_IS_ITEM (list->data)) { GimpItem *item = list->data; if (GIMP_IS_VECTORS (item)) { gdouble x1_f, y1_f, x2_f, y2_f; gimp_vectors_bounds (GIMP_VECTORS (item), &x1_f, &y1_f, &x2_f, &y2_f); x = ROUND (x1_f); y = ROUND (y1_f); w = ROUND (x2_f - x1_f); h = ROUND (y2_f - y1_f); } else { gimp_item_get_offset (item, &x, &y); w = gimp_item_get_width (item); h = gimp_item_get_height (item); } gimp_draw_tool_add_handle (draw_tool, GIMP_HANDLE_FILLED_SQUARE, x, y, GIMP_TOOL_HANDLE_SIZE_SMALL, GIMP_TOOL_HANDLE_SIZE_SMALL, GIMP_HANDLE_ANCHOR_NORTH_WEST); gimp_draw_tool_add_handle (draw_tool, GIMP_HANDLE_FILLED_SQUARE, x + w, y, GIMP_TOOL_HANDLE_SIZE_SMALL, GIMP_TOOL_HANDLE_SIZE_SMALL, GIMP_HANDLE_ANCHOR_NORTH_EAST); gimp_draw_tool_add_handle (draw_tool, GIMP_HANDLE_FILLED_SQUARE, x, y + h, GIMP_TOOL_HANDLE_SIZE_SMALL, GIMP_TOOL_HANDLE_SIZE_SMALL, GIMP_HANDLE_ANCHOR_SOUTH_WEST); gimp_draw_tool_add_handle (draw_tool, GIMP_HANDLE_FILLED_SQUARE, x + w, y + h, GIMP_TOOL_HANDLE_SIZE_SMALL, GIMP_TOOL_HANDLE_SIZE_SMALL, GIMP_HANDLE_ANCHOR_SOUTH_EAST); } else if (GIMP_IS_GUIDE (list->data)) { GimpGuide *guide = list->data; GimpImage *image = gimp_display_get_image (GIMP_TOOL (draw_tool)->display); gint x, y; gint w, h; switch (gimp_guide_get_orientation (guide)) { case GIMP_ORIENTATION_VERTICAL: x = gimp_guide_get_position (guide); h = gimp_image_get_height (image); gimp_draw_tool_add_handle (draw_tool, GIMP_HANDLE_FILLED_SQUARE, x, h, GIMP_TOOL_HANDLE_SIZE_SMALL, GIMP_TOOL_HANDLE_SIZE_SMALL, GIMP_HANDLE_ANCHOR_SOUTH); gimp_draw_tool_add_handle (draw_tool, GIMP_HANDLE_FILLED_SQUARE, x, 0, GIMP_TOOL_HANDLE_SIZE_SMALL, GIMP_TOOL_HANDLE_SIZE_SMALL, GIMP_HANDLE_ANCHOR_NORTH); break; case GIMP_ORIENTATION_HORIZONTAL: y = gimp_guide_get_position (guide); w = gimp_image_get_width (image); gimp_draw_tool_add_handle (draw_tool, GIMP_HANDLE_FILLED_SQUARE, w, y, GIMP_TOOL_HANDLE_SIZE_SMALL, GIMP_TOOL_HANDLE_SIZE_SMALL, GIMP_HANDLE_ANCHOR_EAST); gimp_draw_tool_add_handle (draw_tool, GIMP_HANDLE_FILLED_SQUARE, 0, y, GIMP_TOOL_HANDLE_SIZE_SMALL, GIMP_TOOL_HANDLE_SIZE_SMALL, GIMP_HANDLE_ANCHOR_WEST); break; default: break; } } } }
gboolean gimp_image_snap_point (GimpImage *image, gdouble x, gdouble y, gdouble *tx, gdouble *ty, gdouble epsilon_x, gdouble epsilon_y, gboolean snap_to_guides, gboolean snap_to_grid, gboolean snap_to_canvas, gboolean snap_to_vectors) { gdouble mindist_x = G_MAXDOUBLE; gdouble mindist_y = G_MAXDOUBLE; gdouble dist; gboolean snapped = FALSE; g_return_val_if_fail (GIMP_IS_IMAGE (image), FALSE); g_return_val_if_fail (tx != NULL, FALSE); g_return_val_if_fail (ty != NULL, FALSE); *tx = x; *ty = y; if (! gimp_image_get_guides (image)) snap_to_guides = FALSE; if (! gimp_image_get_grid (image)) snap_to_grid = FALSE; if (! gimp_image_get_active_vectors (image)) snap_to_vectors = FALSE; if (! (snap_to_guides || snap_to_grid || snap_to_canvas || snap_to_vectors)) return FALSE; if (x < -epsilon_x || x >= (gimp_image_get_width (image) + epsilon_x) || y < -epsilon_y || y >= (gimp_image_get_height (image) + epsilon_y)) { return FALSE; } if (snap_to_guides) { GList *list; for (list = gimp_image_get_guides (image); list; list = g_list_next (list)) { GimpGuide *guide = list->data; gint position = gimp_guide_get_position (guide); if (position < 0) continue; switch (gimp_guide_get_orientation (guide)) { case GIMP_ORIENTATION_HORIZONTAL: dist = ABS (position - y); if (dist < MIN (epsilon_y, mindist_y)) { mindist_y = dist; *ty = position; snapped = TRUE; } break; case GIMP_ORIENTATION_VERTICAL: dist = ABS (position - x); if (dist < MIN (epsilon_x, mindist_x)) { mindist_x = dist; *tx = position; snapped = TRUE; } break; default: break; } } } if (snap_to_grid) { GimpGrid *grid = gimp_image_get_grid (image); gdouble xspacing, yspacing; gdouble xoffset, yoffset; gdouble i; g_object_get (grid, "xspacing", &xspacing, "yspacing", &yspacing, "xoffset", &xoffset, "yoffset", &yoffset, NULL); while (xoffset > xspacing) xoffset -= xspacing; while (yoffset > yspacing) yoffset -= yspacing; for (i = xoffset; i <= gimp_image_get_width (image); i += xspacing) { if (i < 0) continue; dist = ABS (i - x); if (dist < MIN (epsilon_x, mindist_x)) { mindist_x = dist; *tx = i; snapped = TRUE; } } for (i = yoffset; i <= gimp_image_get_height (image); i += yspacing) { if (i < 0) continue; dist = ABS (i - y); if (dist < MIN (epsilon_y, mindist_y)) { mindist_y = dist; *ty = i; snapped = TRUE; } } } if (snap_to_canvas) { dist = ABS (x); if (dist < MIN (epsilon_x, mindist_x)) { mindist_x = dist; *tx = 0; snapped = TRUE; } dist = ABS (gimp_image_get_width (image) - x); if (dist < MIN (epsilon_x, mindist_x)) { mindist_x = dist; *tx = gimp_image_get_width (image); snapped = TRUE; } dist = ABS (y); if (dist < MIN (epsilon_y, mindist_y)) { mindist_y = dist; *ty = 0; snapped = TRUE; } dist = ABS (gimp_image_get_height (image) - y); if (dist < MIN (epsilon_y, mindist_y)) { mindist_y = dist; *ty = gimp_image_get_height (image); snapped = TRUE; } } if (snap_to_vectors) { GimpVectors *vectors = gimp_image_get_active_vectors (image); GimpStroke *stroke = NULL; GimpCoords coords = { 0, 0, 0, 0, 0 }; coords.x = x; coords.y = y; while ((stroke = gimp_vectors_stroke_get_next (vectors, stroke))) { GimpCoords nearest; if (gimp_stroke_nearest_point_get (stroke, &coords, 1.0, &nearest, NULL, NULL, NULL) >= 0) { dist = ABS (nearest.x - x); if (dist < MIN (epsilon_x, mindist_x)) { mindist_x = dist; *tx = nearest.x; snapped = TRUE; } dist = ABS (nearest.y - y); if (dist < MIN (epsilon_y, mindist_y)) { mindist_y = dist; *ty = nearest.y; snapped = TRUE; } } } } return snapped; }
gboolean gimp_image_snap_x (GimpImage *image, gdouble x, gdouble *tx, gdouble epsilon_x, gboolean snap_to_guides, gboolean snap_to_grid, gboolean snap_to_canvas) { gdouble mindist = G_MAXDOUBLE; gdouble dist; gboolean snapped = FALSE; g_return_val_if_fail (GIMP_IS_IMAGE (image), FALSE); g_return_val_if_fail (tx != NULL, FALSE); *tx = x; if (! gimp_image_get_guides (image)) snap_to_guides = FALSE; if (! gimp_image_get_grid (image)) snap_to_grid = FALSE; if (! (snap_to_guides || snap_to_grid || snap_to_canvas)) return FALSE; if (x < -epsilon_x || x >= (gimp_image_get_width (image) + epsilon_x)) return FALSE; if (snap_to_guides) { GList *list; for (list = gimp_image_get_guides (image); list; list = g_list_next (list)) { GimpGuide *guide = list->data; gint position = gimp_guide_get_position (guide); if (position < 0) continue; if (gimp_guide_get_orientation (guide) == GIMP_ORIENTATION_VERTICAL) { dist = ABS (position - x); if (dist < MIN (epsilon_x, mindist)) { mindist = dist; *tx = position; snapped = TRUE; } } } } if (snap_to_grid) { GimpGrid *grid = gimp_image_get_grid (image); gdouble xspacing; gdouble xoffset; gdouble i; g_object_get (grid, "xspacing", &xspacing, "xoffset", &xoffset, NULL); while (xoffset > xspacing) xoffset -= xspacing; for (i = xoffset; i <= gimp_image_get_width (image); i += xspacing) { if (i < 0) continue; dist = ABS (i - x); if (dist < MIN (epsilon_x, mindist)) { mindist = dist; *tx = i; snapped = TRUE; } } } if (snap_to_canvas) { dist = ABS (x); if (dist < MIN (epsilon_x, mindist)) { mindist = dist; *tx = 0; snapped = TRUE; } dist = ABS (gimp_image_get_width (image) - x); if (dist < MIN (epsilon_x, mindist)) { mindist = dist; *tx = gimp_image_get_width (image); snapped = TRUE; } } return snapped; }
gboolean gimp_image_snap_y (GimpImage *image, gdouble y, gdouble *ty, gdouble epsilon_y, gboolean snap_to_guides, gboolean snap_to_grid, gboolean snap_to_canvas) { gdouble mindist = G_MAXDOUBLE; gboolean snapped = FALSE; g_return_val_if_fail (GIMP_IS_IMAGE (image), FALSE); g_return_val_if_fail (ty != NULL, FALSE); *ty = y; if (! gimp_image_get_guides (image)) snap_to_guides = FALSE; if (! gimp_image_get_grid (image)) snap_to_grid = FALSE; if (! (snap_to_guides || snap_to_grid || snap_to_canvas)) return FALSE; if (y < -epsilon_y || y >= (gimp_image_get_height (image) + epsilon_y)) return FALSE; if (snap_to_guides) { GList *list; for (list = gimp_image_get_guides (image); list; list = g_list_next (list)) { GimpGuide *guide = list->data; gint position = gimp_guide_get_position (guide); if (gimp_guide_get_orientation (guide) == GIMP_ORIENTATION_HORIZONTAL) { snapped |= gimp_image_snap_distance (y, position, epsilon_y, &mindist, ty); } } } if (snap_to_grid) { GimpGrid *grid = gimp_image_get_grid (image); gdouble yspacing; gdouble yoffset; gdouble i; gimp_grid_get_spacing (grid, NULL, &yspacing); gimp_grid_get_offset (grid, NULL, &yoffset); while (yoffset > yspacing) yoffset -= yspacing; for (i = yoffset; i <= gimp_image_get_height (image); i += yspacing) { if (i < 0) continue; snapped |= gimp_image_snap_distance (y, i, epsilon_y, &mindist, ty); } } if (snap_to_canvas) { snapped |= gimp_image_snap_distance (y, 0, epsilon_y, &mindist, ty); snapped |= gimp_image_snap_distance (y, gimp_image_get_height (image), epsilon_y, &mindist, ty); } return snapped; }
gboolean gimp_image_snap_y (GimpImage *image, gdouble y, gdouble *ty, gdouble epsilon_y, gboolean snap_to_guides, gboolean snap_to_grid, gboolean snap_to_canvas) { gdouble mindist = G_MAXDOUBLE; gdouble dist; gboolean snapped = FALSE; g_return_val_if_fail (GIMP_IS_IMAGE (image), FALSE); g_return_val_if_fail (ty != NULL, FALSE); *ty = y; if (! gimp_image_get_guides (image)) snap_to_guides = FALSE; if (! gimp_image_get_grid (image)) snap_to_grid = FALSE; if (! (snap_to_guides || snap_to_grid || snap_to_canvas)) return FALSE; if (y < -epsilon_y || y >= (gimp_image_get_height (image) + epsilon_y)) return FALSE; if (snap_to_guides) { GList *list; for (list = gimp_image_get_guides (image); list; list = g_list_next (list)) { GimpGuide *guide = list->data; gint position = gimp_guide_get_position (guide); if (position < 0) continue; if (gimp_guide_get_orientation (guide) == GIMP_ORIENTATION_HORIZONTAL) { dist = ABS (position - y); if (dist < MIN (epsilon_y, mindist)) { mindist = dist; *ty = position; snapped = TRUE; } } } } if (snap_to_grid) { GimpGrid *grid = gimp_image_get_grid (image); gdouble yspacing; gdouble yoffset; gdouble i; g_object_get (grid, "yspacing", &yspacing, "yoffset", &yoffset, NULL); while (yoffset > yspacing) yoffset -= yspacing; for (i = yoffset; i <= gimp_image_get_height (image); i += yspacing) { if (i < 0) continue; dist = ABS (i - y); if (dist < MIN (epsilon_y, mindist)) { mindist = dist; *ty = i; snapped = TRUE; } } } if (snap_to_canvas) { dist = ABS (y); if (dist < MIN (epsilon_y, mindist)) { mindist = dist; *ty = 0; snapped = TRUE; } dist = ABS (gimp_image_get_height (image) - y); if (dist < MIN (epsilon_y, mindist)) { mindist = dist; *ty = gimp_image_get_height (image); snapped = TRUE; } } return snapped; }
static void gimp_image_rotate_guides (GimpImage *image, GimpRotationType rotate_type) { GList *list; /* Rotate all Guides */ for (list = gimp_image_get_guides (image); list; list = g_list_next (list)) { GimpGuide *guide = list->data; GimpOrientationType orientation = gimp_guide_get_orientation (guide); gint position = gimp_guide_get_position (guide); switch (rotate_type) { case GIMP_ROTATE_90: switch (orientation) { case GIMP_ORIENTATION_HORIZONTAL: gimp_image_undo_push_guide (image, NULL, guide); gimp_guide_set_orientation (guide, GIMP_ORIENTATION_VERTICAL); gimp_guide_set_position (guide, gimp_image_get_height (image) - position); break; case GIMP_ORIENTATION_VERTICAL: gimp_image_undo_push_guide (image, NULL, guide); gimp_guide_set_orientation (guide, GIMP_ORIENTATION_HORIZONTAL); break; default: break; } break; case GIMP_ROTATE_180: switch (orientation) { case GIMP_ORIENTATION_HORIZONTAL: gimp_image_move_guide (image, guide, gimp_image_get_height (image) - position, TRUE); break; case GIMP_ORIENTATION_VERTICAL: gimp_image_move_guide (image, guide, gimp_image_get_width (image) - position, TRUE); break; default: break; } break; case GIMP_ROTATE_270: switch (orientation) { case GIMP_ORIENTATION_HORIZONTAL: gimp_image_undo_push_guide (image, NULL, guide); gimp_guide_set_orientation (guide, GIMP_ORIENTATION_VERTICAL); break; case GIMP_ORIENTATION_VERTICAL: gimp_image_undo_push_guide (image, NULL, guide); gimp_guide_set_orientation (guide, GIMP_ORIENTATION_HORIZONTAL); gimp_guide_set_position (guide, gimp_image_get_width (image) - position); break; default: break; } break; } } }