~ubuntu-branches/ubuntu/hoary/gimp/hoary

« back to all changes in this revision

Viewing changes to app/display/gimpdisplayshell-transform.c

  • Committer: Bazaar Package Importer
  • Author(s): Sebastien Bacher
  • Date: 2005-04-04 14:51:23 UTC
  • Revision ID: james.westby@ubuntu.com-20050404145123-9py049eeelfymur8
Tags: upstream-2.2.2
ImportĀ upstreamĀ versionĀ 2.2.2

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* The GIMP -- an image manipulation program
 
2
 * Copyright (C) 1995 Spencer Kimball and Peter Mattis
 
3
 *
 
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.
 
8
 *
 
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.
 
13
 *
 
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.
 
17
 */
 
18
 
 
19
#include "config.h"
 
20
 
 
21
#include <gtk/gtk.h>
 
22
 
 
23
#include "display-types.h"
 
24
 
 
25
#include "core/gimpdrawable.h"
 
26
#include "core/gimpimage.h"
 
27
 
 
28
#include "gimpdisplay.h"
 
29
#include "gimpdisplayshell.h"
 
30
#include "gimpdisplayshell-transform.h"
 
31
 
 
32
 
 
33
/**
 
34
 * gimp_display_shell_transform_coords:
 
35
 * @shell:          a #GimpDisplayShell
 
36
 * @image_coords:   image coordinates
 
37
 * @display_coords: returns the corresponding display coordinates
 
38
 *
 
39
 * Transforms from image coordinates to display coordinates, so that
 
40
 * objects can be rendered at the correct points on the display.
 
41
 **/
 
42
void
 
43
gimp_display_shell_transform_coords (GimpDisplayShell *shell,
 
44
                                     GimpCoords       *image_coords,
 
45
                                     GimpCoords       *display_coords)
 
46
{
 
47
  gdouble scalex;
 
48
  gdouble scaley;
 
49
 
 
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);
 
53
 
 
54
  *display_coords = *image_coords;
 
55
 
 
56
  scalex = SCALEFACTOR_X (shell);
 
57
  scaley = SCALEFACTOR_Y (shell);
 
58
 
 
59
  display_coords->x = scalex * image_coords->x;
 
60
  display_coords->y = scaley * image_coords->y;
 
61
 
 
62
  display_coords->x += - shell->offset_x + shell->disp_xoffset;
 
63
  display_coords->y += - shell->offset_y + shell->disp_yoffset;
 
64
}
 
65
 
 
66
/**
 
67
 * gimp_display_shell_untransform_coords:
 
68
 * @shell:          a #GimpDisplayShell
 
69
 * @display_coords: display coordinates
 
70
 * @image_coords:   returns the corresonding image coordinates
 
71
 *
 
72
 * Transforms from display coordinates to image coordinates, so that
 
73
 * points on the display can be mapped to points in the image.
 
74
 **/
 
75
void
 
76
gimp_display_shell_untransform_coords (GimpDisplayShell *shell,
 
77
                                       GimpCoords       *display_coords,
 
78
                                       GimpCoords       *image_coords)
 
79
{
 
80
  gdouble scalex;
 
81
  gdouble scaley;
 
82
 
 
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);
 
86
 
 
87
  *image_coords = *display_coords;
 
88
 
 
89
  scalex = SCALEFACTOR_X (shell);
 
90
  scaley = SCALEFACTOR_Y (shell);
 
91
 
 
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;
 
94
 
 
95
  image_coords->x /= scalex;
 
96
  image_coords->y /= scaley;
 
97
}
 
98
 
 
99
/**
 
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
 
108
 *
 
109
 * Transforms from image coordinates to display coordinates, so that objects
 
110
 * can be rendered at the correct points on the display.
 
111
 **/
 
112
void
 
113
gimp_display_shell_transform_xy (GimpDisplayShell *shell,
 
114
                                 gdouble           x,
 
115
                                 gdouble           y,
 
116
                                 gint             *nx,
 
117
                                 gint             *ny,
 
118
                                 gboolean          use_offsets)
 
119
{
 
120
  gdouble scalex;
 
121
  gdouble scaley;
 
122
  gint    offset_x = 0;
 
123
  gint    offset_y = 0;
 
124
 
 
125
  g_return_if_fail (GIMP_IS_DISPLAY_SHELL (shell));
 
126
  g_return_if_fail (nx != NULL);
 
127
  g_return_if_fail (ny != NULL);
 
128
 
 
129
  /*  transform from image coordinates to screen coordinates  */
 
130
  scalex = SCALEFACTOR_X (shell);
 
131
  scaley = SCALEFACTOR_Y (shell);
 
132
 
 
133
  if (use_offsets)
 
134
    gimp_item_offsets (GIMP_ITEM (gimp_image_active_drawable (shell->gdisp->gimage)),
 
135
                       &offset_x, &offset_y);
 
136
 
 
137
  x = (scalex * (x + offset_x) - shell->offset_x);
 
138
  y = (scaley * (y + offset_y) - shell->offset_y);
 
139
 
 
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);
 
144
 
 
145
  *nx = PROJ_ROUND (x) + shell->disp_xoffset;
 
146
  *ny = PROJ_ROUND (y) + shell->disp_yoffset;
 
147
}
 
148
 
 
149
/**
 
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
 
160
 *
 
161
 * Transform from display coordinates to image coordinates, so that
 
162
 * points on the display can be mapped to the corresponding points
 
163
 * in the image.
 
164
 **/
 
165
void
 
166
gimp_display_shell_untransform_xy (GimpDisplayShell *shell,
 
167
                                   gint              x,
 
168
                                   gint              y,
 
169
                                   gint             *nx,
 
170
                                   gint             *ny,
 
171
                                   gboolean          round,
 
172
                                   gboolean          use_offsets)
 
173
{
 
174
  gdouble scalex;
 
175
  gdouble scaley;
 
176
  gint    offset_x = 0;
 
177
  gint    offset_y = 0;
 
178
 
 
179
  g_return_if_fail (GIMP_IS_DISPLAY_SHELL (shell));
 
180
  g_return_if_fail (nx != NULL);
 
181
  g_return_if_fail (ny != NULL);
 
182
 
 
183
  x -= shell->disp_xoffset;
 
184
  y -= shell->disp_yoffset;
 
185
 
 
186
  /*  transform from screen coordinates to image coordinates  */
 
187
  scalex = SCALEFACTOR_X (shell);
 
188
  scaley = SCALEFACTOR_Y (shell);
 
189
 
 
190
  if (use_offsets)
 
191
    gimp_item_offsets (GIMP_ITEM (gimp_image_active_drawable (shell->gdisp->gimage)),
 
192
                       &offset_x, &offset_y);
 
193
 
 
194
  if (round)
 
195
    {
 
196
      *nx = ROUND ((x + shell->offset_x) / scalex - offset_x);
 
197
      *ny = ROUND ((y + shell->offset_y) / scaley - offset_y);
 
198
    }
 
199
  else
 
200
    {
 
201
      *nx = (gint) ((x + shell->offset_x) / scalex - offset_x);
 
202
      *ny = (gint) ((y + shell->offset_y) / scaley - offset_y);
 
203
    }
 
204
}
 
205
 
 
206
/**
 
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
 
215
 *
 
216
 * This function is identical to gimp_display_shell_transfrom_xy(),
 
217
 * except that it returns its results as doubles rather than ints.
 
218
 **/
 
219
void
 
220
gimp_display_shell_transform_xy_f  (GimpDisplayShell *shell,
 
221
                                    gdouble           x,
 
222
                                    gdouble           y,
 
223
                                    gdouble          *nx,
 
224
                                    gdouble          *ny,
 
225
                                    gboolean          use_offsets)
 
226
{
 
227
  gdouble scalex;
 
228
  gdouble scaley;
 
229
  gint    offset_x = 0;
 
230
  gint    offset_y = 0;
 
231
 
 
232
  g_return_if_fail (GIMP_IS_DISPLAY_SHELL (shell));
 
233
  g_return_if_fail (nx != NULL);
 
234
  g_return_if_fail (ny != NULL);
 
235
 
 
236
  /*  transform from gimp coordinates to screen coordinates  */
 
237
  scalex = SCALEFACTOR_X (shell);
 
238
  scaley = SCALEFACTOR_Y (shell);
 
239
 
 
240
  if (use_offsets)
 
241
    gimp_item_offsets (GIMP_ITEM (gimp_image_active_drawable (shell->gdisp->gimage)),
 
242
                       &offset_x, &offset_y);
 
243
 
 
244
  *nx = scalex * (x + offset_x) - shell->offset_x;
 
245
  *ny = scaley * (y + offset_y) - shell->offset_y;
 
246
 
 
247
  *nx += shell->disp_xoffset;
 
248
  *ny += shell->disp_yoffset;
 
249
}
 
250
 
 
251
/**
 
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
 
260
 *
 
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.
 
264
 **/
 
265
void
 
266
gimp_display_shell_untransform_xy_f (GimpDisplayShell *shell,
 
267
                                     gdouble           x,
 
268
                                     gdouble           y,
 
269
                                     gdouble          *nx,
 
270
                                     gdouble          *ny,
 
271
                                     gboolean          use_offsets)
 
272
{
 
273
  gdouble scalex;
 
274
  gdouble scaley;
 
275
  gint    offset_x = 0;
 
276
  gint    offset_y = 0;
 
277
 
 
278
  g_return_if_fail (GIMP_IS_DISPLAY_SHELL (shell));
 
279
  g_return_if_fail (nx != NULL);
 
280
  g_return_if_fail (ny != NULL);
 
281
 
 
282
  x -= shell->disp_xoffset;
 
283
  y -= shell->disp_yoffset;
 
284
 
 
285
  /*  transform from screen coordinates to gimp coordinates  */
 
286
  scalex = SCALEFACTOR_X (shell);
 
287
  scaley = SCALEFACTOR_Y (shell);
 
288
 
 
289
  if (use_offsets)
 
290
    gimp_item_offsets (GIMP_ITEM (gimp_image_active_drawable (shell->gdisp->gimage)),
 
291
                       &offset_x, &offset_y);
 
292
 
 
293
  *nx = (x + shell->offset_x) / scalex - offset_x;
 
294
  *ny = (y + shell->offset_y) / scaley - offset_y;
 
295
}
 
296
 
 
297
/**
 
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
 
304
 *
 
305
 * This function calculates the part of the image, im image coordinates,
 
306
 * that corresponds to the display viewport.
 
307
 **/
 
308
void
 
309
gimp_display_shell_untransform_viewport (GimpDisplayShell *shell,
 
310
                                         gint             *x,
 
311
                                         gint             *y,
 
312
                                         gint             *width,
 
313
                                         gint             *height)
 
314
{
 
315
  gint x1, y1, x2, y2;
 
316
 
 
317
  g_return_if_fail (GIMP_IS_DISPLAY_SHELL (shell));
 
318
 
 
319
  gimp_display_shell_untransform_xy (shell,
 
320
                                     0, 0,
 
321
                                     &x1, &y1,
 
322
                                     FALSE, FALSE);
 
323
  gimp_display_shell_untransform_xy (shell,
 
324
                                     shell->disp_width, shell->disp_height,
 
325
                                     &x2, &y2,
 
326
                                     FALSE, FALSE);
 
327
 
 
328
  if (x1 < 0) x1 = 0;
 
329
  if (y1 < 0) y1 = 0;
 
330
  if (x2 > shell->gdisp->gimage->width)  x2 = shell->gdisp->gimage->width;
 
331
  if (y2 > shell->gdisp->gimage->height) y2 = shell->gdisp->gimage->height;
 
332
 
 
333
  if (x)      *x      = x1;
 
334
  if (y)      *y      = y1;
 
335
  if (width)  *width  = x2 - x1;
 
336
  if (height) *height = y2 - y1;
 
337
}