• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 GST_ELEMENT_REGISTER_DEFINE_WITH_CODE (tunnel, "tunnel", GST_RANK_NONE,
75     GST_TYPE_TUNNEL, GST_DEBUG_CATEGORY_INIT (gst_tunnel_debug, "tunnel", 0,
76         "tunnel"));
77 
78 static gboolean
tunnel_map(GstGeometricTransform * gt,gint x,gint y,gdouble * in_x,gdouble * in_y)79 tunnel_map (GstGeometricTransform * gt, gint x, gint y, gdouble * in_x,
80     gdouble * in_y)
81 {
82   GstCircleGeometricTransform *cgt = GST_CIRCLE_GEOMETRIC_TRANSFORM_CAST (gt);
83 #ifndef GST_DISABLE_GST_DEBUG
84   GstTunnel *tunnel = GST_TUNNEL_CAST (gt);
85 #endif
86 
87   gdouble norm_x, norm_y;
88   gdouble width = gt->width;
89   gdouble height = gt->height;
90   gdouble r;
91 
92 
93   /* normalize in ((-1.0, -1.0), (1.0, 1.0) and translate the center */
94   /* plus a little trick to obtain a perfect circle, normalize in a
95    * square with sides equal to MAX(width, height) */
96   norm_x = 2.0 * (x - cgt->x_center * width) / MAX (width, height);
97   norm_y = 2.0 * (y - cgt->y_center * height) / MAX (width, height);
98 
99   /* calculate radius, normalize to 1 for future convenience */
100   r = sqrt (0.5 * (norm_x * norm_x + norm_y * norm_y));
101 
102   /* do nothing if r < radius */
103   /* zoom otherwise */
104   norm_x *= CLAMP (r, 0.0, cgt->radius) / r;
105   norm_y *= CLAMP (r, 0.0, cgt->radius) / r;
106 
107   /* unnormalize */
108   *in_x = 0.5 * (norm_x) * MAX (width, height) + cgt->x_center * width;
109   *in_y = 0.5 * (norm_y) * MAX (width, height) + cgt->y_center * height;
110 
111   GST_DEBUG_OBJECT (tunnel, "Inversely mapped %d %d into %lf %lf",
112       x, y, *in_x, *in_y);
113 
114   return TRUE;
115 }
116 
117 static void
gst_tunnel_class_init(GstTunnelClass * klass)118 gst_tunnel_class_init (GstTunnelClass * klass)
119 {
120   GstElementClass *gstelement_class;
121   GstGeometricTransformClass *gstgt_class;
122 
123   gstelement_class = (GstElementClass *) klass;
124   gstgt_class = (GstGeometricTransformClass *) klass;
125 
126   gst_element_class_set_static_metadata (gstelement_class,
127       "tunnel",
128       "Transform/Effect/Video",
129       "Light tunnel effect", "Filippo Argiolas <filippo.argiolas@gmail.com>");
130 
131   gstgt_class->map_func = tunnel_map;
132 }
133 
134 static void
gst_tunnel_init(GstTunnel * filter)135 gst_tunnel_init (GstTunnel * filter)
136 {
137   GstGeometricTransform *gt = GST_GEOMETRIC_TRANSFORM (filter);
138 
139   gt->off_edge_pixels = GST_GT_OFF_EDGES_PIXELS_CLAMP;
140 }
141