1 /*
2 * GStreamer
3 * Copyright (C) 2010 Filippo Argiolas <filippo.argiolas@gmail.com>
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 * SECTION:element-bulge
46 * @title: bulge
47 * @see_also: geometrictransform
48 *
49 * Bugle is a geometric image transform element. It adds a protuberance in the
50 * center point.
51 *
52 * ## Example launch line
53 * |[
54 * gst-launch-1.0 -v videotestsrc ! bulge ! videoconvert ! autovideosink
55 * ]|
56 *
57 */
58
59 #ifdef HAVE_CONFIG_H
60 # include <config.h>
61 #endif
62
63 #include <gst/gst.h>
64 #include <math.h>
65
66 #include "gstbulge.h"
67 #include "geometricmath.h"
68
69 GST_DEBUG_CATEGORY_STATIC (gst_bulge_debug);
70 #define GST_CAT_DEFAULT gst_bulge_debug
71
72 enum
73 {
74 PROP_0,
75 PROP_ZOOM
76 };
77
78 #define DEFAULT_ZOOM 3.0
79
80 #define gst_bulge_parent_class parent_class
81 G_DEFINE_TYPE (GstBulge, gst_bulge, GST_TYPE_CIRCLE_GEOMETRIC_TRANSFORM);
82 GST_ELEMENT_REGISTER_DEFINE_WITH_CODE (bulge, "bulge", GST_RANK_NONE,
83 GST_TYPE_BULGE, GST_DEBUG_CATEGORY_INIT (gst_bulge_debug, "bulge", 0,
84 "bulge"));
85
86 static void
gst_bulge_set_property(GObject * object,guint prop_id,const GValue * value,GParamSpec * pspec)87 gst_bulge_set_property (GObject * object, guint prop_id, const GValue * value,
88 GParamSpec * pspec)
89 {
90 GstBulge *bulge;
91 GstGeometricTransform *gt;
92 gdouble v;
93
94 gt = GST_GEOMETRIC_TRANSFORM_CAST (object);
95 bulge = GST_BULGE_CAST (object);
96
97 GST_OBJECT_LOCK (gt);
98 switch (prop_id) {
99 case PROP_ZOOM:
100 v = g_value_get_double (value);
101 if (v != bulge->zoom) {
102 bulge->zoom = v;
103 gst_geometric_transform_set_need_remap (gt);
104 }
105 break;
106 default:
107 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
108 break;
109 }
110 GST_OBJECT_UNLOCK (gt);
111 }
112
113 static void
gst_bulge_get_property(GObject * object,guint prop_id,GValue * value,GParamSpec * pspec)114 gst_bulge_get_property (GObject * object, guint prop_id,
115 GValue * value, GParamSpec * pspec)
116 {
117 GstBulge *bulge;
118
119 bulge = GST_BULGE_CAST (object);
120
121 switch (prop_id) {
122 case PROP_ZOOM:
123 g_value_set_double (value, bulge->zoom);
124 break;
125 default:
126 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
127 break;
128 }
129 }
130
131 static gboolean
bulge_map(GstGeometricTransform * gt,gint x,gint y,gdouble * in_x,gdouble * in_y)132 bulge_map (GstGeometricTransform * gt, gint x, gint y, gdouble * in_x,
133 gdouble * in_y)
134 {
135 GstCircleGeometricTransform *cgt = GST_CIRCLE_GEOMETRIC_TRANSFORM_CAST (gt);
136 GstBulge *bulge = GST_BULGE_CAST (gt);
137
138 gdouble norm_x, norm_y;
139 gdouble r;
140 gdouble scale;
141
142 gdouble width = gt->width;
143 gdouble height = gt->height;
144
145 /* normalize in ((-1.0, -1.0), (1.0, 1.0) and translate the center */
146 norm_x = 2.0 * (x / width - cgt->x_center);
147 norm_y = 2.0 * (y / height - cgt->y_center);
148
149 /* calculate radius, normalize to 1 for future convenience */
150 r = sqrt (0.5 * (norm_x * norm_x + norm_y * norm_y));
151
152 /* zoom in the center region and smoothly get back to no zoom while
153 * r increases */
154
155 /* the scale factor goes from bulge->zoom when r == 0 to 1.0
156 * when r == cgt->radius using Hermite interpolation */
157 /* scale is inverted because we're doing an inverse transform so
158 * zoom is achieved dividing */
159
160 scale =
161 1.0 / (bulge->zoom + ((1.0 - bulge->zoom) * gst_gm_smoothstep (0,
162 cgt->radius, r)));
163
164 norm_x *= scale;
165 norm_y *= scale;
166
167 /* unnormalize */
168 *in_x = (0.5 * norm_x + cgt->x_center) * width;
169 *in_y = (0.5 * norm_y + cgt->y_center) * height;
170
171 GST_DEBUG_OBJECT (bulge, "Inversely mapped %d %d into %lf %lf",
172 x, y, *in_x, *in_y);
173
174 return TRUE;
175 }
176
177 static void
gst_bulge_class_init(GstBulgeClass * klass)178 gst_bulge_class_init (GstBulgeClass * klass)
179 {
180 GObjectClass *gobject_class;
181 GstElementClass *gstelement_class;
182 GstGeometricTransformClass *gstgt_class;
183
184 gobject_class = (GObjectClass *) klass;
185 gstelement_class = (GstElementClass *) klass;
186 gstgt_class = (GstGeometricTransformClass *) klass;
187
188 gst_element_class_set_static_metadata (gstelement_class,
189 "bulge",
190 "Transform/Effect/Video",
191 "Adds a protuberance in the center point",
192 "Filippo Argiolas <filippo.argiolas@gmail.com>");
193
194 gobject_class->set_property = gst_bulge_set_property;
195 gobject_class->get_property = gst_bulge_get_property;
196
197 g_object_class_install_property (gobject_class, PROP_ZOOM,
198 g_param_spec_double ("zoom", "zoom",
199 "Zoom of the bulge effect",
200 1.0, 100.0, DEFAULT_ZOOM,
201 GST_PARAM_CONTROLLABLE | G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
202
203 gstgt_class->map_func = bulge_map;
204 }
205
206 static void
gst_bulge_init(GstBulge * filter)207 gst_bulge_init (GstBulge * filter)
208 {
209 GstGeometricTransform *gt = GST_GEOMETRIC_TRANSFORM (filter);
210
211 filter->zoom = DEFAULT_ZOOM;
212 gt->off_edge_pixels = GST_GT_OFF_EDGES_PIXELS_CLAMP;
213 }
214