1 /*
2 * GStreamer
3 * Copyright (C) 2010 Thiago Santos <thiago.sousa.santos@collabora.co.uk>
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 * and/or sell copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21 * DEALINGS IN THE SOFTWARE.
22 *
23 * Alternatively, the contents of this file may be used under the
24 * GNU Lesser General Public License Version 2.1 (the "LGPL"), in
25 * which case the following provisions apply instead of the ones
26 * mentioned above:
27 *
28 * This library is free software; you can redistribute it and/or
29 * modify it under the terms of the GNU Library General Public
30 * License as published by the Free Software Foundation; either
31 * version 2 of the License, or (at your option) any later version.
32 *
33 * This library is distributed in the hope that it will be useful,
34 * but WITHOUT ANY WARRANTY; without even the implied warranty of
35 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
36 * Library General Public License for more details.
37 *
38 * You should have received a copy of the GNU Library General Public
39 * License along with this library; if not, write to the
40 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
41 * Boston, MA 02110-1301, USA.
42 */
43
44 /*
45 * Thanks to Jerry Huxtable <http://www.jhlabs.com> work on its java
46 * image editor and filters. The algorithms here were extracted from
47 * his code.
48 */
49
50 /**
51 * SECTION:element-rotate
52 * @title: rotate
53 * @see_also: geometrictransform
54 *
55 * The rotate element transforms the image by rotating it by a specified angle.
56 *
57 * ## Example launch line
58 * |[
59 * gst-launch-1.0 -v videotestsrc ! rotate angle=0.78 ! videoconvert ! autovideosink
60 * ]|
61 *
62 */
63
64 #ifdef HAVE_CONFIG_H
65 # include <config.h>
66 #endif
67
68 #include <gst/gst.h>
69 #include <math.h>
70
71 #include "geometricmath.h"
72 #include "gstrotate.h"
73
74 GST_DEBUG_CATEGORY_STATIC (gst_rotate_debug);
75 #define GST_CAT_DEFAULT gst_rotate_debug
76
77 enum
78 {
79 PROP_0,
80 PROP_ANGLE
81 };
82
83 #define DEFAULT_ANGLE 0
84
85 #define gst_rotate_parent_class parent_class
86 G_DEFINE_TYPE (GstRotate, gst_rotate, GST_TYPE_GEOMETRIC_TRANSFORM);
87 GST_ELEMENT_REGISTER_DEFINE_WITH_CODE (rotate, "rotate", GST_RANK_NONE,
88 GST_TYPE_ROTATE, GST_DEBUG_CATEGORY_INIT (gst_rotate_debug, "rotate", 0,
89 "rotate"));
90
91 static void
gst_rotate_set_property(GObject * object,guint prop_id,const GValue * value,GParamSpec * pspec)92 gst_rotate_set_property (GObject * object, guint prop_id, const GValue * value,
93 GParamSpec * pspec)
94 {
95 GstRotate *rotate;
96 GstGeometricTransform *gt;
97 gdouble v;
98
99 gt = GST_GEOMETRIC_TRANSFORM_CAST (object);
100 rotate = GST_ROTATE_CAST (object);
101
102 GST_OBJECT_LOCK (rotate);
103 switch (prop_id) {
104 case PROP_ANGLE:
105 v = g_value_get_double (value);
106 if (v != rotate->angle) {
107 rotate->angle = v;
108 gst_geometric_transform_set_need_remap (gt);
109 }
110 break;
111 default:
112 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
113 break;
114 }
115 GST_OBJECT_UNLOCK (rotate);
116 }
117
118 static void
gst_rotate_get_property(GObject * object,guint prop_id,GValue * value,GParamSpec * pspec)119 gst_rotate_get_property (GObject * object, guint prop_id,
120 GValue * value, GParamSpec * pspec)
121 {
122 GstRotate *rotate;
123
124 rotate = GST_ROTATE_CAST (object);
125
126 switch (prop_id) {
127 case PROP_ANGLE:
128 g_value_set_double (value, rotate->angle);
129 break;
130 default:
131 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
132 break;
133 }
134 }
135
136 static gboolean
rotate_map(GstGeometricTransform * gt,gint x,gint y,gdouble * in_x,gdouble * in_y)137 rotate_map (GstGeometricTransform * gt, gint x, gint y, gdouble * in_x,
138 gdouble * in_y)
139 {
140 GstRotate *rotate = GST_ROTATE_CAST (gt);
141 gint w, h;
142 gdouble cix, ciy, cox, coy; /* centers, in/out x/y */
143 gdouble ai, ao, ar; /* angles, in/out/rotate (radians) */
144 gdouble r; /* radius */
145 gdouble xi, yi, xo, yo; /* positions in/out x/y */
146
147 /* input and output image height and width */
148 w = gt->width;
149 h = gt->height;
150
151 /* our parameters */
152 ar = rotate->angle; /* angle of rotation */
153
154 /* get in and out centers */
155 cox = 0.5 * w;
156 coy = 0.5 * h;
157 cix = cox;
158 ciy = coy;
159
160 /* convert output image position to polar form */
161 xo = x - cox;
162 yo = y - coy;
163 ao = atan2 (yo, xo);
164 r = sqrt (xo * xo + yo * yo);
165
166 /* perform rotation backward to get input image rotation
167 * this seems wrong, but rotation from in-->out is counterclockwise */
168 ai = ao + ar;
169
170 /* back to rectangular for input image position */
171 xi = r * cos (ai);
172 yi = r * sin (ai);
173
174 /* restore center offset, return values to caller */
175 *in_x = xi + cix;
176 *in_y = yi + ciy;
177
178 GST_DEBUG_OBJECT (rotate, "Inversely mapped %d %d into %lf %lf",
179 x, y, *in_x, *in_y);
180
181 return TRUE;
182 }
183
184 static void
gst_rotate_class_init(GstRotateClass * klass)185 gst_rotate_class_init (GstRotateClass * klass)
186 {
187 GObjectClass *gobject_class;
188 GstElementClass *gstelement_class;
189 GstGeometricTransformClass *gstgt_class;
190
191 gobject_class = (GObjectClass *) klass;
192 gstelement_class = (GstElementClass *) klass;
193 gstgt_class = (GstGeometricTransformClass *) klass;
194
195 gst_element_class_set_static_metadata (gstelement_class,
196 "rotate",
197 "Transform/Effect/Video",
198 "Rotates the picture by an arbitrary angle",
199 "Thiago Santos<thiago.sousa.santos@collabora.co.uk>");
200
201 gobject_class->set_property = gst_rotate_set_property;
202 gobject_class->get_property = gst_rotate_get_property;
203
204 g_object_class_install_property (gobject_class, PROP_ANGLE,
205 g_param_spec_double ("angle", "angle",
206 "Angle by which the picture is rotated, in radians",
207 -G_MAXDOUBLE, G_MAXDOUBLE, DEFAULT_ANGLE,
208 GST_PARAM_CONTROLLABLE | G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
209
210 gstgt_class->map_func = rotate_map;
211 }
212
213 static void
gst_rotate_init(GstRotate * filter)214 gst_rotate_init (GstRotate * filter)
215 {
216 filter->angle = DEFAULT_ANGLE;
217 }
218