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-twirl
52 * @title: twirl
53 * @see_also: geometrictransform
54 *
55 * The twirl element twists the image from the center out.
56 *
57 * ## Example launch line
58 * |[
59 * gst-launch-1.0 -v videotestsrc ! twirl ! 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 "gsttwirl.h"
72
73 GST_DEBUG_CATEGORY_STATIC (gst_twirl_debug);
74 #define GST_CAT_DEFAULT gst_twirl_debug
75
76 enum
77 {
78 PROP_0,
79 PROP_ANGLE
80 };
81
82 #define DEFAULT_ANGLE (G_PI)
83
84 #define gst_twirl_parent_class parent_class
85 G_DEFINE_TYPE (GstTwirl, gst_twirl, GST_TYPE_CIRCLE_GEOMETRIC_TRANSFORM);
86
87 static void
gst_twirl_set_property(GObject * object,guint prop_id,const GValue * value,GParamSpec * pspec)88 gst_twirl_set_property (GObject * object, guint prop_id, const GValue * value,
89 GParamSpec * pspec)
90 {
91 GstTwirl *twirl;
92 GstGeometricTransform *gt;
93 gdouble v;
94
95 gt = GST_GEOMETRIC_TRANSFORM_CAST (object);
96 twirl = GST_TWIRL_CAST (object);
97
98 GST_OBJECT_LOCK (gt);
99 switch (prop_id) {
100 case PROP_ANGLE:
101 v = g_value_get_double (value);
102 if (v != twirl->angle) {
103 twirl->angle = v;
104 gst_geometric_transform_set_need_remap (gt);
105 }
106 break;
107 default:
108 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
109 break;
110 }
111 GST_OBJECT_UNLOCK (gt);
112 }
113
114 static void
gst_twirl_get_property(GObject * object,guint prop_id,GValue * value,GParamSpec * pspec)115 gst_twirl_get_property (GObject * object, guint prop_id,
116 GValue * value, GParamSpec * pspec)
117 {
118 GstTwirl *twirl;
119
120 twirl = GST_TWIRL_CAST (object);
121
122 switch (prop_id) {
123 case PROP_ANGLE:
124 g_value_set_double (value, twirl->angle);
125 break;
126 default:
127 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
128 break;
129 }
130 }
131
132 static gboolean
twirl_map(GstGeometricTransform * gt,gint x,gint y,gdouble * in_x,gdouble * in_y)133 twirl_map (GstGeometricTransform * gt, gint x, gint y, gdouble * in_x,
134 gdouble * in_y)
135 {
136 GstCircleGeometricTransform *cgt = GST_CIRCLE_GEOMETRIC_TRANSFORM_CAST (gt);
137 GstTwirl *twirl = GST_TWIRL_CAST (gt);
138 gdouble distance;
139 gdouble dx, dy;
140
141 dx = x - cgt->precalc_x_center;
142 dy = y - cgt->precalc_y_center;
143 distance = dx * dx + dy * dy;
144
145 if (distance > cgt->precalc_radius2) {
146 *in_x = x;
147 *in_y = y;
148 } else {
149 gdouble d = sqrt (distance);
150 gdouble a = atan2 (dy,
151 dx) + twirl->angle * (cgt->precalc_radius - d) / cgt->precalc_radius;
152
153 *in_x = cgt->precalc_x_center + d * cos (a);
154 *in_y = cgt->precalc_y_center + d * sin (a);
155 }
156
157 GST_DEBUG_OBJECT (twirl, "Inversely mapped %d %d into %lf %lf",
158 x, y, *in_x, *in_y);
159
160 return TRUE;
161 }
162
163 static void
gst_twirl_class_init(GstTwirlClass * klass)164 gst_twirl_class_init (GstTwirlClass * klass)
165 {
166 GObjectClass *gobject_class;
167 GstElementClass *gstelement_class;
168 GstGeometricTransformClass *gstgt_class;
169
170 gobject_class = (GObjectClass *) klass;
171 gstelement_class = (GstElementClass *) klass;
172 gstgt_class = (GstGeometricTransformClass *) klass;
173
174 gst_element_class_set_static_metadata (gstelement_class,
175 "twirl",
176 "Transform/Effect/Video",
177 "Twists the image from the center out",
178 "Thiago Santos<thiago.sousa.santos@collabora.co.uk>");
179
180 gobject_class->set_property = gst_twirl_set_property;
181 gobject_class->get_property = gst_twirl_get_property;
182
183 g_object_class_install_property (gobject_class, PROP_ANGLE,
184 g_param_spec_double ("angle", "angle",
185 "This is the angle in radians by which pixels at the "
186 "nearest edge of the image will move",
187 -G_MAXDOUBLE, G_MAXDOUBLE, DEFAULT_ANGLE,
188 GST_PARAM_CONTROLLABLE | G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
189
190 gstgt_class->map_func = twirl_map;
191 }
192
193 static void
gst_twirl_init(GstTwirl * filter)194 gst_twirl_init (GstTwirl * filter)
195 {
196 GstGeometricTransform *gt = GST_GEOMETRIC_TRANSFORM (filter);
197
198 gt->off_edge_pixels = GST_GT_OFF_EDGES_PIXELS_CLAMP;
199 filter->angle = DEFAULT_ANGLE;
200 }
201
202 gboolean
gst_twirl_plugin_init(GstPlugin * plugin)203 gst_twirl_plugin_init (GstPlugin * plugin)
204 {
205 GST_DEBUG_CATEGORY_INIT (gst_twirl_debug, "twirl", 0, "twirl");
206
207 return gst_element_register (plugin, "twirl", GST_RANK_NONE, GST_TYPE_TWIRL);
208 }
209