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-sphere
52 * @title: sphere
53 * @see_also: geometrictransform
54 *
55 * The sphere element applies a 'sphere' geometric transform to the image.
56 *
57 * ## Example launch line
58 * |[
59 * gst-launch-1.0 -v videotestsrc ! sphere ! 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 "gstsphere.h"
72
73 GST_DEBUG_CATEGORY_STATIC (gst_sphere_debug);
74 #define GST_CAT_DEFAULT gst_sphere_debug
75
76 enum
77 {
78 PROP_0,
79 PROP_REFRACTION
80 };
81
82 #define DEFAULT_REFRACTION 1.5
83
84 #define gst_sphere_parent_class parent_class
85 G_DEFINE_TYPE (GstSphere, gst_sphere, GST_TYPE_CIRCLE_GEOMETRIC_TRANSFORM);
86 GST_ELEMENT_REGISTER_DEFINE_WITH_CODE (sphere, "sphere", GST_RANK_NONE,
87 GST_TYPE_SPHERE, GST_DEBUG_CATEGORY_INIT (gst_sphere_debug, "sphere", 0,
88 "sphere"));
89
90 static void
gst_sphere_set_property(GObject * object,guint prop_id,const GValue * value,GParamSpec * pspec)91 gst_sphere_set_property (GObject * object, guint prop_id, const GValue * value,
92 GParamSpec * pspec)
93 {
94 GstSphere *sphere;
95 GstGeometricTransform *gt;
96 gdouble v;
97
98 gt = GST_GEOMETRIC_TRANSFORM_CAST (object);
99 sphere = GST_SPHERE_CAST (object);
100
101 GST_OBJECT_LOCK (gt);
102 switch (prop_id) {
103 case PROP_REFRACTION:
104 v = g_value_get_double (value);
105 if (v != sphere->refraction) {
106 sphere->refraction = v;
107 gst_geometric_transform_set_need_remap (gt);
108 }
109 break;
110 default:
111 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
112 break;
113 }
114 GST_OBJECT_UNLOCK (gt);
115 }
116
117 static void
gst_sphere_get_property(GObject * object,guint prop_id,GValue * value,GParamSpec * pspec)118 gst_sphere_get_property (GObject * object, guint prop_id,
119 GValue * value, GParamSpec * pspec)
120 {
121 GstSphere *sphere;
122
123 sphere = GST_SPHERE_CAST (object);
124
125 switch (prop_id) {
126 case PROP_REFRACTION:
127 g_value_set_double (value, sphere->refraction);
128 break;
129 default:
130 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
131 break;
132 }
133 }
134
135 /* TODO we could have horizontal and vertical 'radius' */
136 static gboolean
sphere_map(GstGeometricTransform * gt,gint x,gint y,gdouble * in_x,gdouble * in_y)137 sphere_map (GstGeometricTransform * gt, gint x, gint y, gdouble * in_x,
138 gdouble * in_y)
139 {
140 GstCircleGeometricTransform *cgt = GST_CIRCLE_GEOMETRIC_TRANSFORM_CAST (gt);
141 GstSphere *sphere = GST_SPHERE_CAST (gt);
142 gdouble dx, dy;
143 gdouble dx2, dy2;
144
145 dx = x - cgt->precalc_x_center;
146 dy = y - cgt->precalc_y_center;
147 dx2 = dx * dx;
148 dy2 = dy * dy;
149
150 if (dy2 >=
151 (cgt->precalc_radius2 -
152 (cgt->precalc_radius2 * dx2) / cgt->precalc_radius2)) {
153 *in_x = x;
154 *in_y = y;
155 } else {
156 gdouble r_refraction = 1.0 / sphere->refraction;
157 gdouble z;
158 gdouble z2;
159 gdouble angle;
160 gdouble angle1;
161 gdouble angle2;
162
163 z = sqrt ((1.0 - dx2 / cgt->precalc_radius2 -
164 dy2 / cgt->precalc_radius2) * (cgt->precalc_radius2));
165 z2 = z * z;
166
167 /* x */
168 angle = acos (dx / sqrt (dx2 + z2));
169 angle1 = G_PI / 2 - angle;
170 angle2 = asin (sin (angle1) * r_refraction);
171 angle2 = G_PI / 2 - angle - angle2;
172 *in_x = x - tan (angle2) * z;
173
174 /* y */
175 angle = acos (dy / sqrt (dy2 + z2));
176 angle1 = G_PI / 2 - angle;
177 angle2 = asin (sin (angle1) * r_refraction);
178 angle2 = G_PI / 2 - angle - angle2;
179 *in_y = y - tan (angle2) * z;
180 }
181
182 GST_DEBUG_OBJECT (sphere, "Inversely mapped %d %d into %lf %lf",
183 x, y, *in_x, *in_y);
184
185 return TRUE;
186 }
187
188 static void
gst_sphere_class_init(GstSphereClass * klass)189 gst_sphere_class_init (GstSphereClass * 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 "sphere",
201 "Transform/Effect/Video",
202 "Applies 'sphere' geometric transform to the image",
203 "Thiago Santos<thiago.sousa.santos@collabora.co.uk>");
204
205 gobject_class->set_property = gst_sphere_set_property;
206 gobject_class->get_property = gst_sphere_get_property;
207
208 g_object_class_install_property (gobject_class, PROP_REFRACTION,
209 g_param_spec_double ("refraction", "refraction",
210 "refraction index",
211 -G_MAXDOUBLE, G_MAXDOUBLE, DEFAULT_REFRACTION,
212 GST_PARAM_CONTROLLABLE | G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
213
214 gstgt_class->map_func = sphere_map;
215 }
216
217 static void
gst_sphere_init(GstSphere * filter)218 gst_sphere_init (GstSphere * filter)
219 {
220 GstGeometricTransform *gt = GST_GEOMETRIC_TRANSFORM (filter);
221
222 gt->off_edge_pixels = GST_GT_OFF_EDGES_PIXELS_CLAMP;
223 filter->refraction = DEFAULT_REFRACTION;
224 }
225