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-tunnel
46  * @title: tunnel
47  * @see_also: geometrictransform
48  *
49  * Tunnel is a geometric image transform element. It applies a light tunnel
50  * effect.
51  *
52  * ## Example launch line
53  * |[
54  * gst-launch-1.0 -v videotestsrc ! tunnel ! 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 "gsttunnel.h"
67 #include "geometricmath.h"
68 
69 GST_DEBUG_CATEGORY_STATIC (gst_tunnel_debug);
70 #define GST_CAT_DEFAULT gst_tunnel_debug
71 
72 #define gst_tunnel_parent_class parent_class
73 G_DEFINE_TYPE (GstTunnel, gst_tunnel, GST_TYPE_CIRCLE_GEOMETRIC_TRANSFORM);
74 
75 static gboolean
tunnel_map(GstGeometricTransform * gt,gint x,gint y,gdouble * in_x,gdouble * in_y)76 tunnel_map (GstGeometricTransform * gt, gint x, gint y, gdouble * in_x,
77     gdouble * in_y)
78 {
79   GstCircleGeometricTransform *cgt = GST_CIRCLE_GEOMETRIC_TRANSFORM_CAST (gt);
80 #ifndef GST_DISABLE_GST_DEBUG
81   GstTunnel *tunnel = GST_TUNNEL_CAST (gt);
82 #endif
83 
84   gdouble norm_x, norm_y;
85   gdouble width = gt->width;
86   gdouble height = gt->height;
87   gdouble r;
88 
89 
90   /* normalize in ((-1.0, -1.0), (1.0, 1.0) and traslate the center */
91   /* plus a little trick to obtain a perfect circle, normalize in a
92    * square with sides equal to MAX(width, height) */
93   norm_x = 2.0 * (x - cgt->x_center * width) / MAX (width, height);
94   norm_y = 2.0 * (y - cgt->y_center * height) / MAX (width, height);
95 
96   /* calculate radius, normalize to 1 for future convenience */
97   r = sqrt (0.5 * (norm_x * norm_x + norm_y * norm_y));
98 
99   /* do nothing if r < radius */
100   /* zoom otherwise */
101   norm_x *= CLAMP (r, 0.0, cgt->radius) / r;
102   norm_y *= CLAMP (r, 0.0, cgt->radius) / r;
103 
104   /* unnormalize */
105   *in_x = 0.5 * (norm_x) * MAX (width, height) + cgt->x_center * width;
106   *in_y = 0.5 * (norm_y) * MAX (width, height) + cgt->y_center * height;
107 
108   GST_DEBUG_OBJECT (tunnel, "Inversely mapped %d %d into %lf %lf",
109       x, y, *in_x, *in_y);
110 
111   return TRUE;
112 }
113 
114 static void
gst_tunnel_class_init(GstTunnelClass * klass)115 gst_tunnel_class_init (GstTunnelClass * klass)
116 {
117   GstElementClass *gstelement_class;
118   GstGeometricTransformClass *gstgt_class;
119 
120   gstelement_class = (GstElementClass *) klass;
121   gstgt_class = (GstGeometricTransformClass *) klass;
122 
123   gst_element_class_set_static_metadata (gstelement_class,
124       "tunnel",
125       "Transform/Effect/Video",
126       "Light tunnel effect", "Filippo Argiolas <filippo.argiolas@gmail.com>");
127 
128   gstgt_class->map_func = tunnel_map;
129 }
130 
131 static void
gst_tunnel_init(GstTunnel * filter)132 gst_tunnel_init (GstTunnel * filter)
133 {
134   GstGeometricTransform *gt = GST_GEOMETRIC_TRANSFORM (filter);
135 
136   gt->off_edge_pixels = GST_GT_OFF_EDGES_PIXELS_CLAMP;
137 }
138 
139 gboolean
gst_tunnel_plugin_init(GstPlugin * plugin)140 gst_tunnel_plugin_init (GstPlugin * plugin)
141 {
142   GST_DEBUG_CATEGORY_INIT (gst_tunnel_debug, "tunnel", 0, "tunnel");
143 
144   return gst_element_register (plugin, "tunnel", GST_RANK_NONE,
145       GST_TYPE_TUNNEL);
146 }
147