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 
94 static void
gst_circle_set_property(GObject * object,guint prop_id,const GValue * value,GParamSpec * pspec)95 gst_circle_set_property (GObject * object, guint prop_id, const GValue * value,
96     GParamSpec * pspec)
97 {
98   GstCircle *circle;
99   GstGeometricTransform *gt;
100   gdouble v;
101   gint h;
102 
103   gt = GST_GEOMETRIC_TRANSFORM_CAST (object);
104   circle = GST_CIRCLE_CAST (object);
105 
106   GST_OBJECT_LOCK (circle);
107   switch (prop_id) {
108     case PROP_ANGLE:
109       v = g_value_get_double (value);
110       if (v != circle->angle) {
111         circle->angle = v;
112         gst_geometric_transform_set_need_remap (gt);
113       }
114       break;
115     case PROP_SPREAD_ANGLE:
116       v = g_value_get_double (value);
117       if (v != circle->spread_angle) {
118         circle->spread_angle = v;
119         gst_geometric_transform_set_need_remap (gt);
120       }
121       break;
122     case PROP_HEIGHT:
123       h = g_value_get_int (value);
124       if (h != circle->height) {
125         circle->height = h;
126         gst_geometric_transform_set_need_remap (gt);
127       }
128       break;
129     default:
130       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
131       break;
132   }
133   GST_OBJECT_UNLOCK (circle);
134 }
135 
136 static void
gst_circle_get_property(GObject * object,guint prop_id,GValue * value,GParamSpec * pspec)137 gst_circle_get_property (GObject * object, guint prop_id,
138     GValue * value, GParamSpec * pspec)
139 {
140   GstCircle *circle;
141 
142   circle = GST_CIRCLE_CAST (object);
143 
144   switch (prop_id) {
145     case PROP_ANGLE:
146       g_value_set_double (value, circle->angle);
147       break;
148     case PROP_SPREAD_ANGLE:
149       g_value_set_double (value, circle->spread_angle);
150       break;
151     case PROP_HEIGHT:
152       g_value_set_int (value, circle->height);
153       break;
154     default:
155       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
156       break;
157   }
158 }
159 
160 static gboolean
circle_map(GstGeometricTransform * gt,gint x,gint y,gdouble * in_x,gdouble * in_y)161 circle_map (GstGeometricTransform * gt, gint x, gint y, gdouble * in_x,
162     gdouble * in_y)
163 {
164   GstCircleGeometricTransform *cgt = GST_CIRCLE_GEOMETRIC_TRANSFORM_CAST (gt);
165   GstCircle *circle = GST_CIRCLE_CAST (gt);
166   gdouble distance;
167   gdouble dx, dy;
168   gdouble theta;
169 
170   dx = x - cgt->precalc_x_center;
171   dy = y - cgt->precalc_y_center;
172   distance = sqrt (dx * dx + dy * dy);
173   theta = atan2 (-dy, -dx) + circle->angle;
174 
175   theta = gst_gm_mod_float (theta, 2 * G_PI);
176 
177   *in_x = gt->width * theta / (circle->spread_angle + 0.0001);
178   *in_y =
179       gt->height * (1 - (distance - cgt->precalc_radius) / (circle->height +
180           0.0001));
181 
182   GST_DEBUG_OBJECT (circle, "Inversely mapped %d %d into %lf %lf",
183       x, y, *in_x, *in_y);
184 
185   return TRUE;
186 }
187 
188 static void
gst_circle_class_init(GstCircleClass * klass)189 gst_circle_class_init (GstCircleClass * klass)
190 {
191   GObjectClass *gobject_class;
192   GstElementClass *gstelement_class;
193   GstGeometricTransformClass *gstgt_class;
194 
195   gobject_class = (GObjectClass *) klass;
196   gstelement_class = (GstElementClass *) klass;
197   gstgt_class = (GstGeometricTransformClass *) klass;
198 
199   gst_element_class_set_static_metadata (gstelement_class,
200       "circle",
201       "Transform/Effect/Video",
202       "Warps the picture into an arc shaped form",
203       "Thiago Santos<thiago.sousa.santos@collabora.co.uk>");
204 
205   gobject_class->set_property = gst_circle_set_property;
206   gobject_class->get_property = gst_circle_get_property;
207 
208   g_object_class_install_property (gobject_class, PROP_ANGLE,
209       g_param_spec_double ("angle", "angle",
210           "Angle at which the arc starts in radians",
211           -G_MAXDOUBLE, G_MAXDOUBLE, DEFAULT_ANGLE,
212           GST_PARAM_CONTROLLABLE | G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
213   g_object_class_install_property (gobject_class, PROP_SPREAD_ANGLE,
214       g_param_spec_double ("spread-angle", "spread angle",
215           "Length of the arc in radians",
216           -G_MAXDOUBLE, G_MAXDOUBLE, DEFAULT_SPREAD_ANGLE,
217           GST_PARAM_CONTROLLABLE | G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
218   g_object_class_install_property (gobject_class, PROP_HEIGHT,
219       g_param_spec_int ("height", "height",
220           "Height of the arc",
221           0, G_MAXINT, DEFAULT_HEIGHT,
222           GST_PARAM_CONTROLLABLE | G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
223 
224   gstgt_class->map_func = circle_map;
225 }
226 
227 static void
gst_circle_init(GstCircle * filter)228 gst_circle_init (GstCircle * filter)
229 {
230   filter->angle = DEFAULT_ANGLE;
231   filter->spread_angle = DEFAULT_SPREAD_ANGLE;
232   filter->height = DEFAULT_HEIGHT;
233 }
234 
235 gboolean
gst_circle_plugin_init(GstPlugin * plugin)236 gst_circle_plugin_init (GstPlugin * plugin)
237 {
238   GST_DEBUG_CATEGORY_INIT (gst_circle_debug, "circle", 0, "circle");
239 
240   return gst_element_register (plugin, "circle", GST_RANK_NONE,
241       GST_TYPE_CIRCLE);
242 }
243