gimp/app/display/gimpdisplayshell-transform.c

340 lines
12 KiB
C
Raw Normal View History

/* The GIMP -- an 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 "display-types.h"
#include "core/gimpdrawable.h"
#include "core/gimpimage.h"
#include "gimpdisplay.h"
#include "gimpdisplayshell.h"
#include "gimpdisplayshell-transform.h"
/**
* gimp_display_shell_transform_coords:
* @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_coords (GimpDisplayShell *shell,
GimpCoords *image_coords,
GimpCoords *display_coords)
{
gdouble scalex;
gdouble scaley;
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;
scalex = SCALEFACTOR_X (shell);
scaley = SCALEFACTOR_Y (shell);
display_coords->x = scalex * image_coords->x;
display_coords->y = scaley * image_coords->y;
display_coords->x += - shell->offset_x + shell->disp_xoffset;
display_coords->y += - shell->offset_y + shell->disp_yoffset;
}
/**
* gimp_display_shell_transform_coords:
* @shell: a #GimpDisplayShell
* @display_coords: display coordinates
* @image_coords: returns the corresonding 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_coords (GimpDisplayShell *shell,
GimpCoords *display_coords,
GimpCoords *image_coords)
{
gdouble scalex;
gdouble scaley;
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;
scalex = SCALEFACTOR_X (shell);
scaley = SCALEFACTOR_Y (shell);
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 /= scalex;
image_coords->y /= scaley;
}
/**
* gimp_display_shell_transform_xy:
* @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, add the offsets of the active drawable
* in the image that the shell displays.
*
* Transforms from image coordinates to display coordinates, so that
* objects can be rendered at the correct points on the display. [The
* argument @use_offsets is almost always %FALSE; I have been unable
* to figure out what situations it should be %TRUE in.]
**/
void
gimp_display_shell_transform_xy (GimpDisplayShell *shell,
gdouble x,
gdouble y,
gint *nx,
gint *ny,
gboolean use_offsets)
{
gdouble scalex;
gdouble scaley;
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);
/* transform from image coordinates to screen coordinates */
scalex = SCALEFACTOR_X (shell);
scaley = SCALEFACTOR_Y (shell);
if (use_offsets)
removed gimp_drawable_offsets(). 2003-05-08 Michael Natterer <mitch@gimp.org> * app/core/gimpdrawable.[ch]: removed gimp_drawable_offsets(). * app/core/gimpitem.[ch]: added gimp_item_offsets(). * app/core/gimpdrawable-blend.c * app/core/gimpdrawable-bucket-fill.c * app/core/gimpdrawable-histogram.c * app/core/gimpedit.c * app/core/gimpimage-convert.c * app/core/gimpimage-crop.c * app/core/gimpimage-mask-select.c * app/core/gimpimage-mask.c * app/core/gimpimage-merge.c * app/core/gimpimage-pick-color.c * app/core/gimpimage-preview.c * app/core/gimpimage-projection.c * app/core/gimpimage-undo-push.c * app/core/gimpimage.c * app/core/gimplayer-floating-sel.c * app/core/gimplayer.c * app/display/gimpdisplay.c * app/display/gimpdisplayshell-transform.c * app/display/gimpdisplayshell.c * app/gui/channels-commands.c * app/gui/layers-commands.c * app/paint/gimppaintcore.c * app/tools/gimpbezierselecttool.c * app/tools/gimpblendtool.c * app/tools/gimpbucketfilltool.c * app/tools/gimpbycolorselecttool.c * app/tools/gimpclonetool.c * app/tools/gimpcolorpickertool.c * app/tools/gimpcroptool.c * app/tools/gimpcurvestool.c * app/tools/gimpeditselectiontool.c * app/tools/gimpfliptool.c * app/tools/gimpfuzzyselecttool.c * app/tools/gimpinktool.c * app/tools/gimplevelstool.c * app/tools/gimppainttool.c * app/tools/gimprectselecttool.c * app/tools/gimptransformtool.c * app/widgets/gimpselectioneditor.c * app/widgets/gimptoolbox.c * tools/pdbgen/pdb/color.pdb * tools/pdbgen/pdb/drawable.pdb: changed accordingly. * app/pdb/color_cmds.c * app/pdb/drawable_cmds.c: regenerated.
2003-05-08 22:06:03 +08:00
gimp_item_offsets (GIMP_ITEM (gimp_image_active_drawable (shell->gdisp->gimage)),
&offset_x, &offset_y);
x = (scalex * (x + offset_x) - shell->offset_x);
y = (scaley * (y + offset_y) - shell->offset_y);
/* The projected coordinates can easily overflow a gint in the case of big
images at high zoom levels, so we clamp them here to avoid problems. */
x = CLAMP (x, G_MININT, G_MAXINT);
y = CLAMP (y, G_MININT, G_MAXINT);
*nx = PROJ_ROUND (x) + shell->disp_xoffset;
*ny = PROJ_ROUND (y) + shell->disp_yoffset;
}
/**
* 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, subtract the offsets of the active drawable
* in the image that the shell displays.
*
* 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)
{
gdouble scalex;
gdouble scaley;
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;
/* transform from screen coordinates to image coordinates */
scalex = SCALEFACTOR_X (shell);
scaley = SCALEFACTOR_Y (shell);
if (use_offsets)
removed gimp_drawable_offsets(). 2003-05-08 Michael Natterer <mitch@gimp.org> * app/core/gimpdrawable.[ch]: removed gimp_drawable_offsets(). * app/core/gimpitem.[ch]: added gimp_item_offsets(). * app/core/gimpdrawable-blend.c * app/core/gimpdrawable-bucket-fill.c * app/core/gimpdrawable-histogram.c * app/core/gimpedit.c * app/core/gimpimage-convert.c * app/core/gimpimage-crop.c * app/core/gimpimage-mask-select.c * app/core/gimpimage-mask.c * app/core/gimpimage-merge.c * app/core/gimpimage-pick-color.c * app/core/gimpimage-preview.c * app/core/gimpimage-projection.c * app/core/gimpimage-undo-push.c * app/core/gimpimage.c * app/core/gimplayer-floating-sel.c * app/core/gimplayer.c * app/display/gimpdisplay.c * app/display/gimpdisplayshell-transform.c * app/display/gimpdisplayshell.c * app/gui/channels-commands.c * app/gui/layers-commands.c * app/paint/gimppaintcore.c * app/tools/gimpbezierselecttool.c * app/tools/gimpblendtool.c * app/tools/gimpbucketfilltool.c * app/tools/gimpbycolorselecttool.c * app/tools/gimpclonetool.c * app/tools/gimpcolorpickertool.c * app/tools/gimpcroptool.c * app/tools/gimpcurvestool.c * app/tools/gimpeditselectiontool.c * app/tools/gimpfliptool.c * app/tools/gimpfuzzyselecttool.c * app/tools/gimpinktool.c * app/tools/gimplevelstool.c * app/tools/gimppainttool.c * app/tools/gimprectselecttool.c * app/tools/gimptransformtool.c * app/widgets/gimpselectioneditor.c * app/widgets/gimptoolbox.c * tools/pdbgen/pdb/color.pdb * tools/pdbgen/pdb/drawable.pdb: changed accordingly. * app/pdb/color_cmds.c * app/pdb/drawable_cmds.c: regenerated.
2003-05-08 22:06:03 +08:00
gimp_item_offsets (GIMP_ITEM (gimp_image_active_drawable (shell->gdisp->gimage)),
&offset_x, &offset_y);
if (round)
{
*nx = ROUND ((x + shell->offset_x) / scalex - offset_x);
*ny = ROUND ((y + shell->offset_y) / scaley - offset_y);
}
else
{
*nx = (gint) ((x + shell->offset_x) / scalex - offset_x);
*ny = (gint) ((y + shell->offset_y) / scaley - offset_y);
}
}
/**
* 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, add the offsets of the active drawable
* in the image that the shell displays.
*
* 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)
{
gdouble scalex;
gdouble scaley;
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);
/* transform from gimp coordinates to screen coordinates */
scalex = SCALEFACTOR_X (shell);
scaley = SCALEFACTOR_Y (shell);
if (use_offsets)
removed gimp_drawable_offsets(). 2003-05-08 Michael Natterer <mitch@gimp.org> * app/core/gimpdrawable.[ch]: removed gimp_drawable_offsets(). * app/core/gimpitem.[ch]: added gimp_item_offsets(). * app/core/gimpdrawable-blend.c * app/core/gimpdrawable-bucket-fill.c * app/core/gimpdrawable-histogram.c * app/core/gimpedit.c * app/core/gimpimage-convert.c * app/core/gimpimage-crop.c * app/core/gimpimage-mask-select.c * app/core/gimpimage-mask.c * app/core/gimpimage-merge.c * app/core/gimpimage-pick-color.c * app/core/gimpimage-preview.c * app/core/gimpimage-projection.c * app/core/gimpimage-undo-push.c * app/core/gimpimage.c * app/core/gimplayer-floating-sel.c * app/core/gimplayer.c * app/display/gimpdisplay.c * app/display/gimpdisplayshell-transform.c * app/display/gimpdisplayshell.c * app/gui/channels-commands.c * app/gui/layers-commands.c * app/paint/gimppaintcore.c * app/tools/gimpbezierselecttool.c * app/tools/gimpblendtool.c * app/tools/gimpbucketfilltool.c * app/tools/gimpbycolorselecttool.c * app/tools/gimpclonetool.c * app/tools/gimpcolorpickertool.c * app/tools/gimpcroptool.c * app/tools/gimpcurvestool.c * app/tools/gimpeditselectiontool.c * app/tools/gimpfliptool.c * app/tools/gimpfuzzyselecttool.c * app/tools/gimpinktool.c * app/tools/gimplevelstool.c * app/tools/gimppainttool.c * app/tools/gimprectselecttool.c * app/tools/gimptransformtool.c * app/widgets/gimpselectioneditor.c * app/widgets/gimptoolbox.c * tools/pdbgen/pdb/color.pdb * tools/pdbgen/pdb/drawable.pdb: changed accordingly. * app/pdb/color_cmds.c * app/pdb/drawable_cmds.c: regenerated.
2003-05-08 22:06:03 +08:00
gimp_item_offsets (GIMP_ITEM (gimp_image_active_drawable (shell->gdisp->gimage)),
&offset_x, &offset_y);
*nx = scalex * (x + offset_x) - shell->offset_x;
*ny = scaley * (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, subtract the offsets of the active drawable
* in the image that the shell displays.
*
* 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)
{
gdouble scalex;
gdouble scaley;
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;
/* transform from screen coordinates to gimp coordinates */
scalex = SCALEFACTOR_X (shell);
scaley = SCALEFACTOR_Y (shell);
if (use_offsets)
removed gimp_drawable_offsets(). 2003-05-08 Michael Natterer <mitch@gimp.org> * app/core/gimpdrawable.[ch]: removed gimp_drawable_offsets(). * app/core/gimpitem.[ch]: added gimp_item_offsets(). * app/core/gimpdrawable-blend.c * app/core/gimpdrawable-bucket-fill.c * app/core/gimpdrawable-histogram.c * app/core/gimpedit.c * app/core/gimpimage-convert.c * app/core/gimpimage-crop.c * app/core/gimpimage-mask-select.c * app/core/gimpimage-mask.c * app/core/gimpimage-merge.c * app/core/gimpimage-pick-color.c * app/core/gimpimage-preview.c * app/core/gimpimage-projection.c * app/core/gimpimage-undo-push.c * app/core/gimpimage.c * app/core/gimplayer-floating-sel.c * app/core/gimplayer.c * app/display/gimpdisplay.c * app/display/gimpdisplayshell-transform.c * app/display/gimpdisplayshell.c * app/gui/channels-commands.c * app/gui/layers-commands.c * app/paint/gimppaintcore.c * app/tools/gimpbezierselecttool.c * app/tools/gimpblendtool.c * app/tools/gimpbucketfilltool.c * app/tools/gimpbycolorselecttool.c * app/tools/gimpclonetool.c * app/tools/gimpcolorpickertool.c * app/tools/gimpcroptool.c * app/tools/gimpcurvestool.c * app/tools/gimpeditselectiontool.c * app/tools/gimpfliptool.c * app/tools/gimpfuzzyselecttool.c * app/tools/gimpinktool.c * app/tools/gimplevelstool.c * app/tools/gimppainttool.c * app/tools/gimprectselecttool.c * app/tools/gimptransformtool.c * app/widgets/gimpselectioneditor.c * app/widgets/gimptoolbox.c * tools/pdbgen/pdb/color.pdb * tools/pdbgen/pdb/drawable.pdb: changed accordingly. * app/pdb/color_cmds.c * app/pdb/drawable_cmds.c: regenerated.
2003-05-08 22:06:03 +08:00
gimp_item_offsets (GIMP_ITEM (gimp_image_active_drawable (shell->gdisp->gimage)),
&offset_x, &offset_y);
*nx = (x + shell->offset_x) / scalex - offset_x;
*ny = (y + shell->offset_y) / scaley - 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_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,
* the 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;
if (x2 > shell->gdisp->gimage->width) x2 = shell->gdisp->gimage->width;
if (y2 > shell->gdisp->gimage->height) y2 = shell->gdisp->gimage->height;
if (x) *x = x1;
if (y) *y = y1;
if (width) *width = x2 - x1;
if (height) *height = y2 - y1;
}