gimp/app/display/gimpdisplayshell-transform.c

476 lines
16 KiB
C
Raw Normal View History

/* GIMP - The GNU Image Manipulation Program
* Copyright (C) 1995 Spencer Kimball and Peter Mattis
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#include "config.h"
#include <gtk/gtk.h>
#include "libgimpmath/gimpmath.h"
#include "display-types.h"
#include "base/boundary.h"
#include "core/gimpdrawable.h"
#include "core/gimpimage.h"
#include "gimpdisplay.h"
#include "gimpdisplayshell.h"
#include "gimpdisplayshell-transform.h"
/**
* gimp_display_shell_transform_coordinate:
* @shell: a #GimpDisplayShell
* @image_coords: image coordinates
* @display_coords: returns the corresponding display coordinates
*
* Transforms from image coordinates to display coordinates, so that
* objects can be rendered at the correct points on the display.
**/
void
gimp_display_shell_transform_coordinate (GimpDisplayShell *shell,
GimpCoords *image_coords,
GimpCoords *display_coords)
{
g_return_if_fail (GIMP_IS_DISPLAY_SHELL (shell));
g_return_if_fail (image_coords != NULL);
g_return_if_fail (display_coords != NULL);
*display_coords = *image_coords;
display_coords->x = SCALEX (shell, image_coords->x);
display_coords->y = SCALEY (shell, image_coords->y);
display_coords->x += - shell->offset_x + shell->disp_xoffset;
display_coords->y += - shell->offset_y + shell->disp_yoffset;
}
/**
* gimp_display_shell_untransform_coordinate:
* @shell: a #GimpDisplayShell
* @display_coords: display coordinates
* @image_coords: returns the corresponding image coordinates
*
* Transforms from display coordinates to image coordinates, so that
* points on the display can be mapped to points in the image.
**/
void
gimp_display_shell_untransform_coordinate (GimpDisplayShell *shell,
GimpCoords *display_coords,
GimpCoords *image_coords)
{
g_return_if_fail (GIMP_IS_DISPLAY_SHELL (shell));
g_return_if_fail (display_coords != NULL);
g_return_if_fail (image_coords != NULL);
*image_coords = *display_coords;
image_coords->x = display_coords->x - shell->disp_xoffset + shell->offset_x;
image_coords->y = display_coords->y - shell->disp_yoffset + shell->offset_y;
image_coords->x /= shell->scale_x;
image_coords->y /= shell->scale_y;
}
void
gimp_display_shell_transform_xy (GimpDisplayShell *shell,
gdouble x,
gdouble y,
gint *nx,
gint *ny,
gboolean use_offsets)
{
gint offset_x = 0;
gint offset_y = 0;
gint64 tx, ty;
g_return_if_fail (GIMP_IS_DISPLAY_SHELL (shell));
g_return_if_fail (nx != NULL);
g_return_if_fail (ny != NULL);
if (use_offsets)
{
GimpItem *item;
item = GIMP_ITEM (gimp_image_get_active_drawable (shell->display->image));
gimp_item_offsets (item, &offset_x, &offset_y);
x += offset_x;
y += offset_y;
}
tx = PROJ_ROUND64 (x * shell->x_src_dec) / shell->x_dest_inc;
ty = PROJ_ROUND64 (y * shell->y_src_dec) / shell->y_dest_inc;
tx += shell->disp_xoffset - shell->offset_x;
ty += shell->disp_yoffset - shell->offset_y;
/* The projected coordinates might overflow a gint in the case of big
images at high zoom levels, so we clamp them here to avoid problems. */
*nx = CLAMP (tx, G_MININT, G_MAXINT);
*ny = CLAMP (ty, G_MININT, G_MAXINT);
}
/**
* gimp_display_shell_untransform_xy:
* @shell: a #GimpDisplayShell
* @x: x coordinate in display coordinates
* @y: y coordinate in display coordinates
* @nx: returns x oordinate in image coordinates
* @ny: returns y coordinate in image coordinates
* @round: if %TRUE, round the results to the nearest integer;
* if %FALSE, simply cast them to @gint.
* @use_offsets: if %TRUE, @nx and @ny will be returned in the coordinate
* system of the active drawable instead of the image
*
* Transform from display coordinates to image coordinates, so that
* points on the display can be mapped to the corresponding points
* in the image.
**/
void
gimp_display_shell_untransform_xy (GimpDisplayShell *shell,
gint x,
gint y,
gint *nx,
gint *ny,
gboolean round,
gboolean use_offsets)
{
gint offset_x = 0;
gint offset_y = 0;
gint64 tx, ty;
g_return_if_fail (GIMP_IS_DISPLAY_SHELL (shell));
g_return_if_fail (nx != NULL);
g_return_if_fail (ny != NULL);
if (use_offsets)
{
GimpItem *item;
item = GIMP_ITEM (gimp_image_get_active_drawable (shell->display->image));
gimp_item_offsets (item, &offset_x, &offset_y);
}
tx = x + shell->offset_x - shell->disp_xoffset;
ty = y + shell->offset_y - shell->disp_yoffset;
tx *= shell->x_dest_inc;
ty *= shell->y_dest_inc;
tx += round ? shell->x_dest_inc : shell->x_dest_inc >> 1;
ty += round ? shell->y_dest_inc : shell->y_dest_inc >> 1;
tx /= shell->x_src_dec;
ty /= shell->y_src_dec;
*nx = CLAMP (tx - offset_x, G_MININT, G_MAXINT);
*ny = CLAMP (ty - offset_y, G_MININT, G_MAXINT);
}
/**
* gimp_display_shell_transform_xy_f:
* @shell: a #GimpDisplayShell
* @x: x coordinate of point in image coordinates
* @y: y coordinate of point in image coordinate
* @nx: returns the transformed x coordinate
* @ny: returns the transformed y coordinate
* @use_offsets: if %TRUE, the @x and @y coordinates are in the coordinate
* system of the active drawable instead of the image
*
* This function is identical to gimp_display_shell_transfrom_xy(),
* except that it returns its results as doubles rather than ints.
**/
void
gimp_display_shell_transform_xy_f (GimpDisplayShell *shell,
gdouble x,
gdouble y,
gdouble *nx,
gdouble *ny,
gboolean use_offsets)
{
gint offset_x = 0;
gint offset_y = 0;
g_return_if_fail (GIMP_IS_DISPLAY_SHELL (shell));
g_return_if_fail (nx != NULL);
g_return_if_fail (ny != NULL);
if (use_offsets)
{
GimpItem *item;
item = GIMP_ITEM (gimp_image_get_active_drawable (shell->display->image));
gimp_item_offsets (item, &offset_x, &offset_y);
}
*nx = SCALEX (shell, x + offset_x) - shell->offset_x;
*ny = SCALEY (shell, y + offset_y) - shell->offset_y;
*nx += shell->disp_xoffset;
*ny += shell->disp_yoffset;
}
/**
* gimp_display_shell_untransform_xy_f:
* @shell: a #GimpDisplayShell
* @x: x coordinate in display coordinates
* @y: y coordinate in display coordinates
* @nx: place to return x coordinate in image coordinates
* @ny: place to return y coordinate in image coordinates
* @use_offsets: if %TRUE, @nx and @ny will be returned in the coordinate
* system of the active drawable instead of the image
*
* This function is identical to gimp_display_shell_untransform_xy(),
* except that the input and output coordinates are doubles rather than
* ints, and consequently there is no option related to rounding.
**/
void
gimp_display_shell_untransform_xy_f (GimpDisplayShell *shell,
gdouble x,
gdouble y,
gdouble *nx,
gdouble *ny,
gboolean use_offsets)
{
gint offset_x = 0;
gint offset_y = 0;
g_return_if_fail (GIMP_IS_DISPLAY_SHELL (shell));
g_return_if_fail (nx != NULL);
g_return_if_fail (ny != NULL);
x -= shell->disp_xoffset;
y -= shell->disp_yoffset;
if (use_offsets)
{
GimpItem *item;
item = GIMP_ITEM (gimp_image_get_active_drawable (shell->display->image));
gimp_item_offsets (item, &offset_x, &offset_y);
}
*nx = (x + shell->offset_x) / shell->scale_x - offset_x;
*ny = (y + shell->offset_y) / shell->scale_y - offset_y;
}
Fixed bug #78732 (don't paste off screen): 2004-01-15 Michael Natterer <mitch@gimp.org> Fixed bug #78732 (don't paste off screen): * app/display/gimpdisplayshell-transform.[ch]: added new function gimp_display_shell_untransform_viewport() which returns the visible rectangle of the image in image coordinates. * app/core/gimp-edit.[ch] (gimp_edit_paste): added viewport parameters and changed positioning of the pasted layer as follows: - if there is a selection, center on the selection (just as before). - if there is no viewport, center on the active drawable. - if the viewport intersects with the active drawable, center on the intersection. - if the viewport does *not* intersect with the active drawable, center on the active drawable (off-screen, but better than pasting something that will be invisible due to floating selection clipping). - if there is no active drawable, center on the viewport. - if there is no active drawable and no viewport, center on the image. * app/widgets/gimpbufferview.c (gimp_buffer_view_paste_clicked) (gimp_buffer_view_paste_into_clicked) * app/display/gimpdisplayshell-dnd.c (gimp_display_shell_drop_buffer) * app/gui/edit-commands.c (edit_paste_cmd_callback) (edit_paste_into_cmd_callback): ask the shell for the viewport and pass it to gimp_edit_paste(). * app/display/gimpdisplayshell-dnd.c (gimp_display_shell_drop_drawable): center the created layer on the viewport. * app/tools/gimpmovetool.c (gimp_move_tool_button_release): use gimp_display_shell_untransform_viewport() (its code was taken from here). * tools/pdbgen/pdb/edit.pdb: pass "no viewport" to gimp_edit_paste(). * app/pdb/edit_cmds.c: regenerated.
2004-01-15 22:36:43 +08:00
/**
* gimp_display_shell_transform_points:
* @shell: a #GimpDisplayShell
* @points: array of x, y coordinate pairs
* @coords: returns the corresponding display coordinates
* @n_points: number of points
* @use_offsets: if %TRUE, the source coordinates are in the coordinate
* system of the active drawable instead of the image
*
* Transforms from image coordinates to display coordinates, so that
* objects can be rendered at the correct points on the display.
**/
void
gimp_display_shell_transform_points (GimpDisplayShell *shell,
const gdouble *points,
GdkPoint *coords,
gint n_points,
gboolean use_offsets)
{
gint offset_x = 0;
gint offset_y = 0;
gint i;
g_return_if_fail (GIMP_IS_DISPLAY_SHELL (shell));
if (use_offsets)
{
GimpItem *item;
item = GIMP_ITEM (gimp_image_get_active_drawable (shell->display->image));
gimp_item_offsets (item, &offset_x, &offset_y);
}
for (i = 0; i < n_points ; i++)
{
gdouble x, y;
x = points[i*2] + offset_x;
y = points[i*2+1] + offset_y;
x *= shell->x_src_dec / shell->x_dest_inc;
y *= shell->y_src_dec / shell->y_dest_inc;
coords[i].x = CLAMP (PROJ_ROUND64 (x + shell->disp_xoffset - shell->offset_x),
G_MININT, G_MAXINT);
coords[i].y = CLAMP (PROJ_ROUND64 (y + shell->disp_yoffset - shell->offset_y),
G_MININT, G_MAXINT);
}
}
/**
* gimp_display_shell_transform_coords:
* @shell: a #GimpDisplayShell
* @image_coords: array of image coordinates
* @disp_coords: returns the corresponding display coordinates
* @n_coords: number of coordinates
* @use_offsets: if %TRUE, the source coordinates are in the coordinate
* system of the active drawable instead of the image
*
* Transforms from image coordinates to display coordinates, so that
* objects can be rendered at the correct points on the display.
**/
void
gimp_display_shell_transform_coords (GimpDisplayShell *shell,
const GimpCoords *image_coords,
GdkPoint *disp_coords,
gint n_coords,
gboolean use_offsets)
{
gint offset_x = 0;
gint offset_y = 0;
gint i;
g_return_if_fail (GIMP_IS_DISPLAY_SHELL (shell));
if (use_offsets)
{
GimpItem *item;
item = GIMP_ITEM (gimp_image_get_active_drawable (shell->display->image));
gimp_item_offsets (item, &offset_x, &offset_y);
}
for (i = 0; i < n_coords ; i++)
{
gdouble x, y;
x = image_coords[i].x + offset_x;
y = image_coords[i].y + offset_y;
x *= shell->x_src_dec / shell->x_dest_inc;
y *= shell->y_src_dec / shell->y_dest_inc;
disp_coords[i].x = CLAMP (PROJ_ROUND64 (x + shell->disp_xoffset - shell->offset_x),
G_MININT, G_MAXINT);
disp_coords[i].y = CLAMP (PROJ_ROUND64 (y + shell->disp_yoffset - shell->offset_y),
G_MININT, G_MAXINT);
}
}
/**
* gimp_display_shell_transform_segments:
* @shell: a #GimpDisplayShell
* @src_segs: array of segments in image coordinates
* @dest_segs: returns the corresponding segments in display coordinates
* @n_segs: number of segments
* @use_offsets: if %TRUE, the source coordinates are in the coordinate
* system of the active drawable instead of the image
*
* Transforms from image coordinates to display coordinates, so that
* objects can be rendered at the correct points on the display.
**/
void
gimp_display_shell_transform_segments (GimpDisplayShell *shell,
const BoundSeg *src_segs,
GdkSegment *dest_segs,
gint n_segs,
gboolean use_offsets)
{
gint offset_x = 0;
gint offset_y = 0;
gint i;
g_return_if_fail (GIMP_IS_DISPLAY_SHELL (shell));
if (use_offsets)
{
GimpItem *item;
item = GIMP_ITEM (gimp_image_get_active_drawable (shell->display->image));
gimp_item_offsets (item, &offset_x, &offset_y);
}
for (i = 0; i < n_segs ; i++)
{
gint64 x1, x2;
gint64 y1, y2;
x1 = src_segs[i].x1 + offset_x;
x2 = src_segs[i].x2 + offset_x;
y1 = src_segs[i].y1 + offset_y;
y2 = src_segs[i].y2 + offset_y;
x1 = PROJ_ROUND64 (x1 * shell->x_src_dec) / shell->x_dest_inc;
x2 = PROJ_ROUND64 (x2 * shell->x_src_dec) / shell->x_dest_inc;
y1 = PROJ_ROUND64 (y1 * shell->y_src_dec) / shell->y_dest_inc;
y2 = PROJ_ROUND64 (y2 * shell->y_src_dec) / shell->y_dest_inc;
dest_segs[i].x1 = CLAMP (x1 + shell->disp_xoffset - shell->offset_x,
G_MININT, G_MAXINT);
dest_segs[i].x2 = CLAMP (x2 + shell->disp_xoffset - shell->offset_x,
G_MININT, G_MAXINT);
dest_segs[i].y1 = CLAMP (y1 + shell->disp_yoffset - shell->offset_y,
G_MININT, G_MAXINT);
dest_segs[i].y2 = CLAMP (y2 + shell->disp_yoffset - shell->offset_y,
G_MININT, G_MAXINT);
}
}
/**
* gimp_display_shell_untransform_viewport:
* @shell: a #GimpDisplayShell
* @x: returns image x coordinate of display upper left corner
* @y: returns image y coordinate of display upper left corner
* @width: returns width of display measured in image coordinates
* @height: returns height of display measured in image coordinates
*
* This function calculates the part of the image, im image coordinates,
* that corresponds to the display viewport.
**/
Fixed bug #78732 (don't paste off screen): 2004-01-15 Michael Natterer <mitch@gimp.org> Fixed bug #78732 (don't paste off screen): * app/display/gimpdisplayshell-transform.[ch]: added new function gimp_display_shell_untransform_viewport() which returns the visible rectangle of the image in image coordinates. * app/core/gimp-edit.[ch] (gimp_edit_paste): added viewport parameters and changed positioning of the pasted layer as follows: - if there is a selection, center on the selection (just as before). - if there is no viewport, center on the active drawable. - if the viewport intersects with the active drawable, center on the intersection. - if the viewport does *not* intersect with the active drawable, center on the active drawable (off-screen, but better than pasting something that will be invisible due to floating selection clipping). - if there is no active drawable, center on the viewport. - if there is no active drawable and no viewport, center on the image. * app/widgets/gimpbufferview.c (gimp_buffer_view_paste_clicked) (gimp_buffer_view_paste_into_clicked) * app/display/gimpdisplayshell-dnd.c (gimp_display_shell_drop_buffer) * app/gui/edit-commands.c (edit_paste_cmd_callback) (edit_paste_into_cmd_callback): ask the shell for the viewport and pass it to gimp_edit_paste(). * app/display/gimpdisplayshell-dnd.c (gimp_display_shell_drop_drawable): center the created layer on the viewport. * app/tools/gimpmovetool.c (gimp_move_tool_button_release): use gimp_display_shell_untransform_viewport() (its code was taken from here). * tools/pdbgen/pdb/edit.pdb: pass "no viewport" to gimp_edit_paste(). * app/pdb/edit_cmds.c: regenerated.
2004-01-15 22:36:43 +08:00
void
gimp_display_shell_untransform_viewport (GimpDisplayShell *shell,
gint *x,
gint *y,
gint *width,
gint *height)
{
gint x1, y1, x2, y2;
g_return_if_fail (GIMP_IS_DISPLAY_SHELL (shell));
gimp_display_shell_untransform_xy (shell,
0, 0,
&x1, &y1,
FALSE, FALSE);
gimp_display_shell_untransform_xy (shell,
shell->disp_width, shell->disp_height,
&x2, &y2,
FALSE, FALSE);
if (x1 < 0) x1 = 0;
if (y1 < 0) y1 = 0;
app/actions/channels-commands.c app/actions/colormap-actions.c 2007-12-25 Michael Natterer <mitch@gimp.org> * app/actions/channels-commands.c * app/actions/colormap-actions.c * app/actions/colormap-commands.c * app/actions/image-commands.c * app/core/gimp-edit.c * app/core/gimpdrawable-preview.c * app/core/gimpimage-colorhash.c * app/core/gimpimage-colormap.c * app/core/gimpimage-convert.c * app/core/gimpimage-crop.c * app/core/gimpimage-duplicate.c * app/core/gimpimage-flip.c * app/core/gimpimage-guides.c * app/core/gimpimage-merge.c * app/core/gimpimage-preview.c * app/core/gimpimage-quick-mask.c * app/core/gimpimage-resize.c * app/core/gimpimage-rotate.c * app/core/gimpimage-sample-points.c * app/core/gimpimage-scale.c * app/core/gimpimage-snap.c * app/core/gimpimage.c * app/core/gimpimagefile.c * app/core/gimpimageundo.c * app/core/gimpitem-preview.c * app/core/gimpitem.c * app/core/gimplayer.c * app/core/gimppalette-import.c * app/core/gimpprojection-construct.c * app/core/gimpprojection.c * app/core/gimpselection.c * app/core/gimpundo.c * app/dialogs/layer-options-dialog.c * app/dialogs/print-size-dialog.c * app/display/gimpdisplay.c * app/display/gimpdisplayshell-draw.c * app/display/gimpdisplayshell-scale.c * app/display/gimpdisplayshell-scroll.c * app/display/gimpdisplayshell-title.c * app/display/gimpdisplayshell-transform.c * app/display/gimpdisplayshell.c * app/display/gimpstatusbar.c * app/file/file-open.c * app/paint/gimppaintoptions.c * app/tools/gimpaligntool.c * app/tools/gimpcolortool.c * app/tools/gimpeditselectiontool.c * app/tools/gimpiscissorstool.c * app/tools/gimpmeasuretool.c * app/tools/gimpmovetool.c * app/tools/gimpperspectiveclonetool.c * app/tools/gimprectangleselecttool.c * app/tools/gimprectangletool.c * app/tools/gimprotatetool.c * app/vectors/gimpvectors-export.c * app/vectors/gimpvectors-import.c * app/vectors/gimpvectors.c * app/widgets/gimpimagepropview.c * app/widgets/gimpnavigationview.c * app/widgets/gimpselectioneditor.c * app/widgets/gimpviewrendererdrawable.c * app/widgets/gimpviewrendererimage.c * app/xcf/xcf-load.c * app/xcf/xcf-save.c * tools/pdbgen/pdb/guides.pdb * tools/pdbgen/pdb/image.pdb: use accessors for many image properties. * app/pdb/guides_cmds.c * app/pdb/image_cmds.c: regenerated. svn path=/trunk/; revision=24432
2007-12-26 00:21:40 +08:00
if (x2 > gimp_image_get_width (shell->display->image))
x2 = gimp_image_get_width (shell->display->image);
if (y2 > gimp_image_get_height (shell->display->image))
y2 = gimp_image_get_height (shell->display->image);
Fixed bug #78732 (don't paste off screen): 2004-01-15 Michael Natterer <mitch@gimp.org> Fixed bug #78732 (don't paste off screen): * app/display/gimpdisplayshell-transform.[ch]: added new function gimp_display_shell_untransform_viewport() which returns the visible rectangle of the image in image coordinates. * app/core/gimp-edit.[ch] (gimp_edit_paste): added viewport parameters and changed positioning of the pasted layer as follows: - if there is a selection, center on the selection (just as before). - if there is no viewport, center on the active drawable. - if the viewport intersects with the active drawable, center on the intersection. - if the viewport does *not* intersect with the active drawable, center on the active drawable (off-screen, but better than pasting something that will be invisible due to floating selection clipping). - if there is no active drawable, center on the viewport. - if there is no active drawable and no viewport, center on the image. * app/widgets/gimpbufferview.c (gimp_buffer_view_paste_clicked) (gimp_buffer_view_paste_into_clicked) * app/display/gimpdisplayshell-dnd.c (gimp_display_shell_drop_buffer) * app/gui/edit-commands.c (edit_paste_cmd_callback) (edit_paste_into_cmd_callback): ask the shell for the viewport and pass it to gimp_edit_paste(). * app/display/gimpdisplayshell-dnd.c (gimp_display_shell_drop_drawable): center the created layer on the viewport. * app/tools/gimpmovetool.c (gimp_move_tool_button_release): use gimp_display_shell_untransform_viewport() (its code was taken from here). * tools/pdbgen/pdb/edit.pdb: pass "no viewport" to gimp_edit_paste(). * app/pdb/edit_cmds.c: regenerated.
2004-01-15 22:36:43 +08:00
if (x) *x = x1;
if (y) *y = y1;
if (width) *width = x2 - x1;
if (height) *height = y2 - y1;
}