1
/* The GIMP -- an image manipulation program
2
* Copyright (C) 1995-2001 Spencer Kimball, Peter Mattis, and others
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.
21
#include <glib-object.h>
23
#include "libgimpmath/gimpmath.h"
25
#include "core-types.h"
27
#include "gimp-transform-utils.h"
31
gimp_transform_matrix_flip (GimpOrientationType flip_type,
35
g_return_if_fail (result != NULL);
37
gimp_matrix3_identity (result);
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);
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);
53
case GIMP_ORIENTATION_UNKNOWN:
59
gimp_transform_matrix_flip_free (gint x,
71
g_return_if_fail (result != NULL);
73
angle = atan2 (y2 - y1, x2 - x1);
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);
84
gimp_transform_matrix_rotate (gint x,
94
g_return_if_fail (result != NULL);
96
center_x = (gdouble) x + (gdouble) width / 2.0;
97
center_y = (gdouble) y + (gdouble) height / 2.0;
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);
106
gimp_transform_matrix_rotate_center (gdouble center_x,
111
g_return_if_fail (result != NULL);
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);
120
gimp_transform_matrix_scale (gint x,
130
gdouble scale_x = 1.0;
131
gdouble scale_y = 1.0;
133
g_return_if_fail (result != NULL);
136
scale_x = t_width / (gdouble) width;
139
scale_y = t_height / (gdouble) height;
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);
148
gimp_transform_matrix_shear (gint x,
152
GimpOrientationType orientation,
159
g_return_if_fail (result != NULL);
167
center_x = (gdouble) x + (gdouble) width / 2.0;
168
center_y = (gdouble) y + (gdouble) height / 2.0;
170
gimp_matrix3_identity (result);
171
gimp_matrix3_translate (result, -center_x, -center_y);
173
if (orientation == GIMP_ORIENTATION_HORIZONTAL)
174
gimp_matrix3_xshear (result, amount / height);
176
gimp_matrix3_yshear (result, amount / width);
178
gimp_matrix3_translate (result, +center_x, +center_y);
182
gimp_transform_matrix_perspective (gint x,
200
g_return_if_fail (result != NULL);
202
scalex = scaley = 1.0;
205
scalex = 1.0 / (gdouble) width;
208
scaley = 1.0 / (gdouble) height;
210
/* Determine the perspective transform that maps from
211
* the unit cube to the transformed coordinates
214
gdouble dx1, dx2, dx3, dy1, dy2, dy3;
218
dx3 = t_x1 - t_x2 + t_x4 - t_x3;
222
dy3 = t_y1 - t_y2 + t_y4 - t_y3;
224
/* Is the mapping affine? */
225
if ((dx3 == 0.0) && (dy3 == 0.0))
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;
240
det1 = dx3 * dy2 - dy3 * dx2;
241
det2 = dx1 * dy2 - dy1 * dx2;
243
if (det1 == 0.0 && det2 == 0.0)
244
matrix.coeff[2][0] = 1.0;
246
matrix.coeff[2][0] = det1 / det2;
248
det1 = dx1 * dy3 - dy1 * dx3;
250
if (det1 == 0.0 && det2 == 0.0)
251
matrix.coeff[2][1] = 1.0;
253
matrix.coeff[2][1] = det1 / det2;
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;
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;
264
matrix.coeff[2][2] = 1.0;
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);