1
/* The GIMP -- an image manipulation program
2
* Copyright (C) 1995 Spencer Kimball and Peter Mattis
4
* This program is free software; you can redistribute it and/or modify
5
* it under the terms of the GNU General Public License as published by
6
* the Free Software Foundation; either version 2 of the License, or
7
* (at your option) any later version.
9
* This program is distributed in the hope that it will be useful,
10
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12
* GNU General Public License for more details.
14
* You should have received a copy of the GNU General Public License
15
* along with this program; if not, write to the Free Software
16
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
23
#include "display-types.h"
25
#include "core/gimpdrawable.h"
26
#include "core/gimpimage.h"
28
#include "gimpdisplay.h"
29
#include "gimpdisplayshell.h"
30
#include "gimpdisplayshell-transform.h"
34
* gimp_display_shell_transform_coords:
35
* @shell: a #GimpDisplayShell
36
* @image_coords: image coordinates
37
* @display_coords: returns the corresponding display coordinates
39
* Transforms from image coordinates to display coordinates, so that
40
* objects can be rendered at the correct points on the display.
43
gimp_display_shell_transform_coords (GimpDisplayShell *shell,
44
GimpCoords *image_coords,
45
GimpCoords *display_coords)
50
g_return_if_fail (GIMP_IS_DISPLAY_SHELL (shell));
51
g_return_if_fail (image_coords != NULL);
52
g_return_if_fail (display_coords != NULL);
54
*display_coords = *image_coords;
56
scalex = SCALEFACTOR_X (shell);
57
scaley = SCALEFACTOR_Y (shell);
59
display_coords->x = scalex * image_coords->x;
60
display_coords->y = scaley * image_coords->y;
62
display_coords->x += - shell->offset_x + shell->disp_xoffset;
63
display_coords->y += - shell->offset_y + shell->disp_yoffset;
67
* gimp_display_shell_untransform_coords:
68
* @shell: a #GimpDisplayShell
69
* @display_coords: display coordinates
70
* @image_coords: returns the corresonding image coordinates
72
* Transforms from display coordinates to image coordinates, so that
73
* points on the display can be mapped to points in the image.
76
gimp_display_shell_untransform_coords (GimpDisplayShell *shell,
77
GimpCoords *display_coords,
78
GimpCoords *image_coords)
83
g_return_if_fail (GIMP_IS_DISPLAY_SHELL (shell));
84
g_return_if_fail (display_coords != NULL);
85
g_return_if_fail (image_coords != NULL);
87
*image_coords = *display_coords;
89
scalex = SCALEFACTOR_X (shell);
90
scaley = SCALEFACTOR_Y (shell);
92
image_coords->x = display_coords->x - shell->disp_xoffset + shell->offset_x;
93
image_coords->y = display_coords->y - shell->disp_yoffset + shell->offset_y;
95
image_coords->x /= scalex;
96
image_coords->y /= scaley;
100
* gimp_display_shell_transform_xy:
101
* @shell: a #GimpDisplayShell
102
* @x: x coordinate of point in image coordinates
103
* @y: y coordinate of point in image coordinate
104
* @nx: returns the transformed x coordinate
105
* @ny: returns the transformed y coordinate
106
* @use_offsets: if %TRUE, the @x and @y coordinates are in the coordinate
107
* system of the active drawable instead of the image
109
* Transforms from image coordinates to display coordinates, so that objects
110
* can be rendered at the correct points on the display.
113
gimp_display_shell_transform_xy (GimpDisplayShell *shell,
118
gboolean use_offsets)
125
g_return_if_fail (GIMP_IS_DISPLAY_SHELL (shell));
126
g_return_if_fail (nx != NULL);
127
g_return_if_fail (ny != NULL);
129
/* transform from image coordinates to screen coordinates */
130
scalex = SCALEFACTOR_X (shell);
131
scaley = SCALEFACTOR_Y (shell);
134
gimp_item_offsets (GIMP_ITEM (gimp_image_active_drawable (shell->gdisp->gimage)),
135
&offset_x, &offset_y);
137
x = (scalex * (x + offset_x) - shell->offset_x);
138
y = (scaley * (y + offset_y) - shell->offset_y);
140
/* The projected coordinates can easily overflow a gint in the case of big
141
images at high zoom levels, so we clamp them here to avoid problems. */
142
x = CLAMP (x, G_MININT, G_MAXINT);
143
y = CLAMP (y, G_MININT, G_MAXINT);
145
*nx = PROJ_ROUND (x) + shell->disp_xoffset;
146
*ny = PROJ_ROUND (y) + shell->disp_yoffset;
150
* gimp_display_shell_untransform_xy:
151
* @shell: a #GimpDisplayShell
152
* @x: x coordinate in display coordinates
153
* @y: y coordinate in display coordinates
154
* @nx: returns x oordinate in image coordinates
155
* @ny: returns y coordinate in image coordinates
156
* @round: if %TRUE, round the results to the nearest integer;
157
* if %FALSE, simply cast them to @gint.
158
* @use_offsets: if %TRUE, @nx and @ny will be returned in the coordinate
159
* system of the active drawable instead of the image
161
* Transform from display coordinates to image coordinates, so that
162
* points on the display can be mapped to the corresponding points
166
gimp_display_shell_untransform_xy (GimpDisplayShell *shell,
172
gboolean use_offsets)
179
g_return_if_fail (GIMP_IS_DISPLAY_SHELL (shell));
180
g_return_if_fail (nx != NULL);
181
g_return_if_fail (ny != NULL);
183
x -= shell->disp_xoffset;
184
y -= shell->disp_yoffset;
186
/* transform from screen coordinates to image coordinates */
187
scalex = SCALEFACTOR_X (shell);
188
scaley = SCALEFACTOR_Y (shell);
191
gimp_item_offsets (GIMP_ITEM (gimp_image_active_drawable (shell->gdisp->gimage)),
192
&offset_x, &offset_y);
196
*nx = ROUND ((x + shell->offset_x) / scalex - offset_x);
197
*ny = ROUND ((y + shell->offset_y) / scaley - offset_y);
201
*nx = (gint) ((x + shell->offset_x) / scalex - offset_x);
202
*ny = (gint) ((y + shell->offset_y) / scaley - offset_y);
207
* gimp_display_shell_transform_xy_f:
208
* @shell: a #GimpDisplayShell
209
* @x: x coordinate of point in image coordinates
210
* @y: y coordinate of point in image coordinate
211
* @nx: returns the transformed x coordinate
212
* @ny: returns the transformed y coordinate
213
* @use_offsets: if %TRUE, the @x and @y coordinates are in the coordinate
214
* system of the active drawable instead of the image
216
* This function is identical to gimp_display_shell_transfrom_xy(),
217
* except that it returns its results as doubles rather than ints.
220
gimp_display_shell_transform_xy_f (GimpDisplayShell *shell,
225
gboolean use_offsets)
232
g_return_if_fail (GIMP_IS_DISPLAY_SHELL (shell));
233
g_return_if_fail (nx != NULL);
234
g_return_if_fail (ny != NULL);
236
/* transform from gimp coordinates to screen coordinates */
237
scalex = SCALEFACTOR_X (shell);
238
scaley = SCALEFACTOR_Y (shell);
241
gimp_item_offsets (GIMP_ITEM (gimp_image_active_drawable (shell->gdisp->gimage)),
242
&offset_x, &offset_y);
244
*nx = scalex * (x + offset_x) - shell->offset_x;
245
*ny = scaley * (y + offset_y) - shell->offset_y;
247
*nx += shell->disp_xoffset;
248
*ny += shell->disp_yoffset;
252
* gimp_display_shell_untransform_xy_f:
253
* @shell: a #GimpDisplayShell
254
* @x: x coordinate in display coordinates
255
* @y: y coordinate in display coordinates
256
* @nx: place to return x coordinate in image coordinates
257
* @ny: place to return y coordinate in image coordinates
258
* @use_offsets: if %TRUE, @nx and @ny will be returned in the coordinate
259
* system of the active drawable instead of the image
261
* This function is identical to gimp_display_shell_untransform_xy(),
262
* except that the input and output coordinates are doubles rather than
263
* ints, and consequently there is no option related to rounding.
266
gimp_display_shell_untransform_xy_f (GimpDisplayShell *shell,
271
gboolean use_offsets)
278
g_return_if_fail (GIMP_IS_DISPLAY_SHELL (shell));
279
g_return_if_fail (nx != NULL);
280
g_return_if_fail (ny != NULL);
282
x -= shell->disp_xoffset;
283
y -= shell->disp_yoffset;
285
/* transform from screen coordinates to gimp coordinates */
286
scalex = SCALEFACTOR_X (shell);
287
scaley = SCALEFACTOR_Y (shell);
290
gimp_item_offsets (GIMP_ITEM (gimp_image_active_drawable (shell->gdisp->gimage)),
291
&offset_x, &offset_y);
293
*nx = (x + shell->offset_x) / scalex - offset_x;
294
*ny = (y + shell->offset_y) / scaley - offset_y;
298
* gimp_display_shell_untransform_viewport:
299
* @shell: a #GimpDisplayShell
300
* @x: returns image x coordinate of display upper left corner
301
* @y: returns image y coordinate of display upper left corner
302
* @width: returns width of display measured in image coordinates
303
* @height: returns height of display measured in image coordinates
305
* This function calculates the part of the image, im image coordinates,
306
* that corresponds to the display viewport.
309
gimp_display_shell_untransform_viewport (GimpDisplayShell *shell,
317
g_return_if_fail (GIMP_IS_DISPLAY_SHELL (shell));
319
gimp_display_shell_untransform_xy (shell,
323
gimp_display_shell_untransform_xy (shell,
324
shell->disp_width, shell->disp_height,
330
if (x2 > shell->gdisp->gimage->width) x2 = shell->gdisp->gimage->width;
331
if (y2 > shell->gdisp->gimage->height) y2 = shell->gdisp->gimage->height;
335
if (width) *width = x2 - x1;
336
if (height) *height = y2 - y1;