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

« back to all changes in this revision

Viewing changes to app/core/gimp-transform-utils.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-2001 Spencer Kimball, Peter Mattis, and others
 
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 <glib-object.h>
 
22
 
 
23
#include "libgimpmath/gimpmath.h"
 
24
 
 
25
#include "core-types.h"
 
26
 
 
27
#include "gimp-transform-utils.h"
 
28
 
 
29
 
 
30
void
 
31
gimp_transform_matrix_flip (GimpOrientationType  flip_type,
 
32
                            gdouble              axis,
 
33
                            GimpMatrix3         *result)
 
34
{
 
35
  g_return_if_fail (result != NULL);
 
36
 
 
37
  gimp_matrix3_identity (result);
 
38
 
 
39
  switch (flip_type)
 
40
    {
 
41
    case GIMP_ORIENTATION_HORIZONTAL:
 
42
      gimp_matrix3_translate (result, - axis, 0.0);
 
43
      gimp_matrix3_scale (result, -1.0, 1.0);
 
44
      gimp_matrix3_translate (result, axis, 0.0);
 
45
      break;
 
46
 
 
47
    case GIMP_ORIENTATION_VERTICAL:
 
48
      gimp_matrix3_translate (result, 0.0, - axis);
 
49
      gimp_matrix3_scale (result, 1.0, -1.0);
 
50
      gimp_matrix3_translate (result, 0.0, axis);
 
51
      break;
 
52
 
 
53
    case GIMP_ORIENTATION_UNKNOWN:
 
54
      break;
 
55
    }
 
56
}
 
57
 
 
58
void
 
59
gimp_transform_matrix_flip_free (gint         x,
 
60
                                 gint         y,
 
61
                                 gint         width,
 
62
                                 gint         height,
 
63
                                 gdouble      x1,
 
64
                                 gdouble      y1,
 
65
                                 gdouble      x2,
 
66
                                 gdouble      y2,
 
67
                                 GimpMatrix3 *result)
 
68
{
 
69
  gdouble angle;
 
70
 
 
71
  g_return_if_fail (result != NULL);
 
72
 
 
73
  angle = atan2  (y2 - y1, x2 - x1);
 
74
 
 
75
  gimp_matrix3_identity  (result);
 
76
  gimp_matrix3_translate (result, -x1, -y1);
 
77
  gimp_matrix3_rotate    (result, -angle);
 
78
  gimp_matrix3_scale     (result, 1.0, -1.0);
 
79
  gimp_matrix3_rotate    (result, angle);
 
80
  gimp_matrix3_translate (result, x1, y1);
 
81
}
 
82
 
 
83
void
 
84
gimp_transform_matrix_rotate (gint         x,
 
85
                              gint         y,
 
86
                              gint         width,
 
87
                              gint         height,
 
88
                              gdouble      angle,
 
89
                              GimpMatrix3 *result)
 
90
{
 
91
  gdouble center_x;
 
92
  gdouble center_y;
 
93
 
 
94
  g_return_if_fail (result != NULL);
 
95
 
 
96
  center_x = (gdouble) x + (gdouble) width  / 2.0;
 
97
  center_y = (gdouble) y + (gdouble) height / 2.0;
 
98
 
 
99
  gimp_matrix3_identity  (result);
 
100
  gimp_matrix3_translate (result, -center_x, -center_y);
 
101
  gimp_matrix3_rotate    (result, angle);
 
102
  gimp_matrix3_translate (result, +center_x, +center_y);
 
103
}
 
104
 
 
105
void
 
106
gimp_transform_matrix_rotate_center (gdouble      center_x,
 
107
                                     gdouble      center_y,
 
108
                                     gdouble      angle,
 
109
                                     GimpMatrix3 *result)
 
110
{
 
111
  g_return_if_fail (result != NULL);
 
112
 
 
113
  gimp_matrix3_identity  (result);
 
114
  gimp_matrix3_translate (result, -center_x, -center_y);
 
115
  gimp_matrix3_rotate    (result, angle);
 
116
  gimp_matrix3_translate (result, +center_x, +center_y);
 
117
}
 
118
 
 
119
void
 
120
gimp_transform_matrix_scale (gint         x,
 
121
                             gint         y,
 
122
                             gint         width,
 
123
                             gint         height,
 
124
                             gdouble      t_x,
 
125
                             gdouble      t_y,
 
126
                             gdouble      t_width,
 
127
                             gdouble      t_height,
 
128
                             GimpMatrix3 *result)
 
129
{
 
130
  gdouble scale_x = 1.0;
 
131
  gdouble scale_y = 1.0;
 
132
 
 
133
  g_return_if_fail (result != NULL);
 
134
 
 
135
  if (width > 0)
 
136
    scale_x = t_width / (gdouble) width;
 
137
 
 
138
  if (height > 0)
 
139
    scale_y = t_height / (gdouble) height;
 
140
 
 
141
  gimp_matrix3_identity  (result);
 
142
  gimp_matrix3_translate (result, -x, -y);
 
143
  gimp_matrix3_scale     (result, scale_x, scale_y);
 
144
  gimp_matrix3_translate (result, t_x, t_y);
 
145
}
 
146
 
 
147
void
 
148
gimp_transform_matrix_shear (gint                 x,
 
149
                             gint                 y,
 
150
                             gint                 width,
 
151
                             gint                 height,
 
152
                             GimpOrientationType  orientation,
 
153
                             gdouble              amount,
 
154
                             GimpMatrix3         *result)
 
155
{
 
156
  gdouble center_x;
 
157
  gdouble center_y;
 
158
 
 
159
  g_return_if_fail (result != NULL);
 
160
 
 
161
  if (width == 0)
 
162
    width = 1;
 
163
 
 
164
  if (height == 0)
 
165
    height = 1;
 
166
 
 
167
  center_x = (gdouble) x + (gdouble) width  / 2.0;
 
168
  center_y = (gdouble) y + (gdouble) height / 2.0;
 
169
 
 
170
  gimp_matrix3_identity  (result);
 
171
  gimp_matrix3_translate (result, -center_x, -center_y);
 
172
 
 
173
  if (orientation == GIMP_ORIENTATION_HORIZONTAL)
 
174
    gimp_matrix3_xshear (result, amount / height);
 
175
  else
 
176
    gimp_matrix3_yshear (result, amount / width);
 
177
 
 
178
  gimp_matrix3_translate (result, +center_x, +center_y);
 
179
}
 
180
 
 
181
void
 
182
gimp_transform_matrix_perspective (gint         x,
 
183
                                   gint         y,
 
184
                                   gint         width,
 
185
                                   gint         height,
 
186
                                   gdouble      t_x1,
 
187
                                   gdouble      t_y1,
 
188
                                   gdouble      t_x2,
 
189
                                   gdouble      t_y2,
 
190
                                   gdouble      t_x3,
 
191
                                   gdouble      t_y3,
 
192
                                   gdouble      t_x4,
 
193
                                   gdouble      t_y4,
 
194
                                   GimpMatrix3 *result)
 
195
{
 
196
  GimpMatrix3 matrix;
 
197
  gdouble     scalex;
 
198
  gdouble     scaley;
 
199
 
 
200
  g_return_if_fail (result != NULL);
 
201
 
 
202
  scalex = scaley = 1.0;
 
203
 
 
204
  if (width > 0)
 
205
    scalex = 1.0 / (gdouble) width;
 
206
 
 
207
  if (height > 0)
 
208
    scaley = 1.0 / (gdouble) height;
 
209
 
 
210
  /* Determine the perspective transform that maps from
 
211
   * the unit cube to the transformed coordinates
 
212
   */
 
213
  {
 
214
    gdouble dx1, dx2, dx3, dy1, dy2, dy3;
 
215
 
 
216
    dx1 = t_x2 - t_x4;
 
217
    dx2 = t_x3 - t_x4;
 
218
    dx3 = t_x1 - t_x2 + t_x4 - t_x3;
 
219
 
 
220
    dy1 = t_y2 - t_y4;
 
221
    dy2 = t_y3 - t_y4;
 
222
    dy3 = t_y1 - t_y2 + t_y4 - t_y3;
 
223
 
 
224
    /*  Is the mapping affine?  */
 
225
    if ((dx3 == 0.0) && (dy3 == 0.0))
 
226
      {
 
227
        matrix.coeff[0][0] = t_x2 - t_x1;
 
228
        matrix.coeff[0][1] = t_x4 - t_x2;
 
229
        matrix.coeff[0][2] = t_x1;
 
230
        matrix.coeff[1][0] = t_y2 - t_y1;
 
231
        matrix.coeff[1][1] = t_y4 - t_y2;
 
232
        matrix.coeff[1][2] = t_y1;
 
233
        matrix.coeff[2][0] = 0.0;
 
234
        matrix.coeff[2][1] = 0.0;
 
235
      }
 
236
    else
 
237
      {
 
238
        gdouble det1, det2;
 
239
 
 
240
        det1 = dx3 * dy2 - dy3 * dx2;
 
241
        det2 = dx1 * dy2 - dy1 * dx2;
 
242
 
 
243
        if (det1 == 0.0 && det2 == 0.0)
 
244
          matrix.coeff[2][0] = 1.0;
 
245
        else
 
246
          matrix.coeff[2][0] = det1 / det2;
 
247
 
 
248
        det1 = dx1 * dy3 - dy1 * dx3;
 
249
 
 
250
        if (det1 == 0.0 && det2 == 0.0)
 
251
          matrix.coeff[2][1] = 1.0;
 
252
        else
 
253
          matrix.coeff[2][1] = det1 / det2;
 
254
 
 
255
        matrix.coeff[0][0] = t_x2 - t_x1 + matrix.coeff[2][0] * t_x2;
 
256
        matrix.coeff[0][1] = t_x3 - t_x1 + matrix.coeff[2][1] * t_x3;
 
257
        matrix.coeff[0][2] = t_x1;
 
258
 
 
259
        matrix.coeff[1][0] = t_y2 - t_y1 + matrix.coeff[2][0] * t_y2;
 
260
        matrix.coeff[1][1] = t_y3 - t_y1 + matrix.coeff[2][1] * t_y3;
 
261
        matrix.coeff[1][2] = t_y1;
 
262
      }
 
263
 
 
264
    matrix.coeff[2][2] = 1.0;
 
265
  }
 
266
 
 
267
  gimp_matrix3_identity  (result);
 
268
  gimp_matrix3_translate (result, -x, -y);
 
269
  gimp_matrix3_scale     (result, scalex, scaley);
 
270
  gimp_matrix3_mult      (&matrix, result);
 
271
}