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
88 static void
gst_rotate_set_property(GObject * object,guint prop_id,const GValue * value,GParamSpec * pspec)89 gst_rotate_set_property (GObject * object, guint prop_id, const GValue * value,
90 GParamSpec * pspec)
91 {
92 GstRotate *rotate;
93 GstGeometricTransform *gt;
94 gdouble v;
95
96 gt = GST_GEOMETRIC_TRANSFORM_CAST (object);
97 rotate = GST_ROTATE_CAST (object);
98
99 GST_OBJECT_LOCK (rotate);
100 switch (prop_id) {
101 case PROP_ANGLE:
102 v = g_value_get_double (value);
103 if (v != rotate->angle) {
104 rotate->angle = v;
105 gst_geometric_transform_set_need_remap (gt);
106 }
107 break;
108 default:
109 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
110 break;
111 }
112 GST_OBJECT_UNLOCK (rotate);
113 }
114
115 static void
gst_rotate_get_property(GObject * object,guint prop_id,GValue * value,GParamSpec * pspec)116 gst_rotate_get_property (GObject * object, guint prop_id,
117 GValue * value, GParamSpec * pspec)
118 {
119 GstRotate *rotate;
120
121 rotate = GST_ROTATE_CAST (object);
122
123 switch (prop_id) {
124 case PROP_ANGLE:
125 g_value_set_double (value, rotate->angle);
126 break;
127 default:
128 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
129 break;
130 }
131 }
132
133 static gboolean
rotate_map(GstGeometricTransform * gt,gint x,gint y,gdouble * in_x,gdouble * in_y)134 rotate_map (GstGeometricTransform * gt, gint x, gint y, gdouble * in_x,
135 gdouble * in_y)
136 {
137 GstRotate *rotate = GST_ROTATE_CAST (gt);
138 gint w, h;
139 gdouble cix, ciy, cox, coy; /* centers, in/out x/y */
140 gdouble ai, ao, ar; /* angles, in/out/rotate (radians) */
141 gdouble r; /* radius */
142 gdouble xi, yi, xo, yo; /* positions in/out x/y */
143
144 /* input and output image height and width */
145 w = gt->width;
146 h = gt->height;
147
148 /* our parameters */
149 ar = rotate->angle; /* angle of rotation */
150
151 /* get in and out centers */
152 cox = 0.5 * w;
153 coy = 0.5 * h;
154 cix = cox;
155 ciy = coy;
156
157 /* convert output image position to polar form */
158 xo = x - cox;
159 yo = y - coy;
160 ao = atan2 (yo, xo);
161 r = sqrt (xo * xo + yo * yo);
162
163 /* perform rotation backward to get input image rotation
164 * this seems wrong, but rotation from in-->out is counterclockwise */
165 ai = ao + ar;
166
167 /* back to rectangular for input image position */
168 xi = r * cos (ai);
169 yi = r * sin (ai);
170
171 /* restore center offset, return values to caller */
172 *in_x = xi + cix;
173 *in_y = yi + ciy;
174
175 GST_DEBUG_OBJECT (rotate, "Inversely mapped %d %d into %lf %lf",
176 x, y, *in_x, *in_y);
177
178 return TRUE;
179 }
180
181 static void
gst_rotate_class_init(GstRotateClass * klass)182 gst_rotate_class_init (GstRotateClass * klass)
183 {
184 GObjectClass *gobject_class;
185 GstElementClass *gstelement_class;
186 GstGeometricTransformClass *gstgt_class;
187
188 gobject_class = (GObjectClass *) klass;
189 gstelement_class = (GstElementClass *) klass;
190 gstgt_class = (GstGeometricTransformClass *) klass;
191
192 gst_element_class_set_static_metadata (gstelement_class,
193 "rotate",
194 "Transform/Effect/Video",
195 "Rotates the picture by an arbitrary angle",
196 "Thiago Santos<thiago.sousa.santos@collabora.co.uk>");
197
198 gobject_class->set_property = gst_rotate_set_property;
199 gobject_class->get_property = gst_rotate_get_property;
200
201 g_object_class_install_property (gobject_class, PROP_ANGLE,
202 g_param_spec_double ("angle", "angle",
203 "Angle by which the picture is rotated, in radians",
204 -G_MAXDOUBLE, G_MAXDOUBLE, DEFAULT_ANGLE,
205 GST_PARAM_CONTROLLABLE | G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
206
207 gstgt_class->map_func = rotate_map;
208 }
209
210 static void
gst_rotate_init(GstRotate * filter)211 gst_rotate_init (GstRotate * filter)
212 {
213 filter->angle = DEFAULT_ANGLE;
214 }
215
216 gboolean
gst_rotate_plugin_init(GstPlugin * plugin)217 gst_rotate_plugin_init (GstPlugin * plugin)
218 {
219 GST_DEBUG_CATEGORY_INIT (gst_rotate_debug, "rotate", 0, "rotate");
220
221 return gst_element_register (plugin, "rotate", GST_RANK_NONE,
222 GST_TYPE_ROTATE);
223 }
224