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-circle
52 * @title: circle
53 * @see_also: geometrictransform
54 *
55 * Circle is a geometric image transform element. It warps the picture into an
56 * arc shaped form.
57 *
58 * ## Example launch line
59 * |[
60 * gst-launch-1.0 -v videotestsrc ! circle ! videoconvert ! autovideosink
61 * ]|
62 *
63 */
64
65 #ifdef HAVE_CONFIG_H
66 # include <config.h>
67 #endif
68
69 #include <gst/gst.h>
70 #include <math.h>
71
72 #include "geometricmath.h"
73
74 #include "gstcircle.h"
75
76 GST_DEBUG_CATEGORY_STATIC (gst_circle_debug);
77 #define GST_CAT_DEFAULT gst_circle_debug
78
79 enum
80 {
81 PROP_0,
82 PROP_ANGLE,
83 PROP_HEIGHT,
84 PROP_SPREAD_ANGLE
85 };
86
87 #define DEFAULT_ANGLE 0
88 #define DEFAULT_HEIGHT 20
89 #define DEFAULT_SPREAD_ANGLE G_PI
90
91 #define gst_circle_parent_class parent_class
92 G_DEFINE_TYPE (GstCircle, gst_circle, GST_TYPE_CIRCLE_GEOMETRIC_TRANSFORM);
93 GST_ELEMENT_REGISTER_DEFINE_WITH_CODE (circle, "circle", GST_RANK_NONE,
94 GST_TYPE_CIRCLE, GST_DEBUG_CATEGORY_INIT (gst_circle_debug, "circle", 0,
95 "circle"));
96
97 static void
gst_circle_set_property(GObject * object,guint prop_id,const GValue * value,GParamSpec * pspec)98 gst_circle_set_property (GObject * object, guint prop_id, const GValue * value,
99 GParamSpec * pspec)
100 {
101 GstCircle *circle;
102 GstGeometricTransform *gt;
103 gdouble v;
104 gint h;
105
106 gt = GST_GEOMETRIC_TRANSFORM_CAST (object);
107 circle = GST_CIRCLE_CAST (object);
108
109 GST_OBJECT_LOCK (circle);
110 switch (prop_id) {
111 case PROP_ANGLE:
112 v = g_value_get_double (value);
113 if (v != circle->angle) {
114 circle->angle = v;
115 gst_geometric_transform_set_need_remap (gt);
116 }
117 break;
118 case PROP_SPREAD_ANGLE:
119 v = g_value_get_double (value);
120 if (v != circle->spread_angle) {
121 circle->spread_angle = v;
122 gst_geometric_transform_set_need_remap (gt);
123 }
124 break;
125 case PROP_HEIGHT:
126 h = g_value_get_int (value);
127 if (h != circle->height) {
128 circle->height = h;
129 gst_geometric_transform_set_need_remap (gt);
130 }
131 break;
132 default:
133 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
134 break;
135 }
136 GST_OBJECT_UNLOCK (circle);
137 }
138
139 static void
gst_circle_get_property(GObject * object,guint prop_id,GValue * value,GParamSpec * pspec)140 gst_circle_get_property (GObject * object, guint prop_id,
141 GValue * value, GParamSpec * pspec)
142 {
143 GstCircle *circle;
144
145 circle = GST_CIRCLE_CAST (object);
146
147 switch (prop_id) {
148 case PROP_ANGLE:
149 g_value_set_double (value, circle->angle);
150 break;
151 case PROP_SPREAD_ANGLE:
152 g_value_set_double (value, circle->spread_angle);
153 break;
154 case PROP_HEIGHT:
155 g_value_set_int (value, circle->height);
156 break;
157 default:
158 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
159 break;
160 }
161 }
162
163 static gboolean
circle_map(GstGeometricTransform * gt,gint x,gint y,gdouble * in_x,gdouble * in_y)164 circle_map (GstGeometricTransform * gt, gint x, gint y, gdouble * in_x,
165 gdouble * in_y)
166 {
167 GstCircleGeometricTransform *cgt = GST_CIRCLE_GEOMETRIC_TRANSFORM_CAST (gt);
168 GstCircle *circle = GST_CIRCLE_CAST (gt);
169 gdouble distance;
170 gdouble dx, dy;
171 gdouble theta;
172
173 dx = x - cgt->precalc_x_center;
174 dy = y - cgt->precalc_y_center;
175 distance = sqrt (dx * dx + dy * dy);
176 theta = atan2 (-dy, -dx) + circle->angle;
177
178 theta = gst_gm_mod_float (theta, 2 * G_PI);
179
180 *in_x = gt->width * theta / (circle->spread_angle + 0.0001);
181 *in_y =
182 gt->height * (1 - (distance - cgt->precalc_radius) / (circle->height +
183 0.0001));
184
185 GST_DEBUG_OBJECT (circle, "Inversely mapped %d %d into %lf %lf",
186 x, y, *in_x, *in_y);
187
188 return TRUE;
189 }
190
191 static void
gst_circle_class_init(GstCircleClass * klass)192 gst_circle_class_init (GstCircleClass * klass)
193 {
194 GObjectClass *gobject_class;
195 GstElementClass *gstelement_class;
196 GstGeometricTransformClass *gstgt_class;
197
198 gobject_class = (GObjectClass *) klass;
199 gstelement_class = (GstElementClass *) klass;
200 gstgt_class = (GstGeometricTransformClass *) klass;
201
202 gst_element_class_set_static_metadata (gstelement_class,
203 "circle",
204 "Transform/Effect/Video",
205 "Warps the picture into an arc shaped form",
206 "Thiago Santos<thiago.sousa.santos@collabora.co.uk>");
207
208 gobject_class->set_property = gst_circle_set_property;
209 gobject_class->get_property = gst_circle_get_property;
210
211 g_object_class_install_property (gobject_class, PROP_ANGLE,
212 g_param_spec_double ("angle", "angle",
213 "Angle at which the arc starts in radians",
214 -G_MAXDOUBLE, G_MAXDOUBLE, DEFAULT_ANGLE,
215 GST_PARAM_CONTROLLABLE | G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
216 g_object_class_install_property (gobject_class, PROP_SPREAD_ANGLE,
217 g_param_spec_double ("spread-angle", "spread angle",
218 "Length of the arc in radians",
219 -G_MAXDOUBLE, G_MAXDOUBLE, DEFAULT_SPREAD_ANGLE,
220 GST_PARAM_CONTROLLABLE | G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
221 g_object_class_install_property (gobject_class, PROP_HEIGHT,
222 g_param_spec_int ("height", "height",
223 "Height of the arc",
224 0, G_MAXINT, DEFAULT_HEIGHT,
225 GST_PARAM_CONTROLLABLE | G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
226
227 gstgt_class->map_func = circle_map;
228 }
229
230 static void
gst_circle_init(GstCircle * filter)231 gst_circle_init (GstCircle * filter)
232 {
233 filter->angle = DEFAULT_ANGLE;
234 filter->spread_angle = DEFAULT_SPREAD_ANGLE;
235 filter->height = DEFAULT_HEIGHT;
236 }
237