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-fisheye
46 * @title: fisheye
47 * @see_also: geometrictransform
48 *
49 * Fisheye is a geometric image transform element. It simulates a fisheye lens
50 * by zooming on the center of the image and compressing the edges.
51 *
52 * ## Example launch line
53 * |[
54 * gst-launch-1.0 -v videotestsrc ! fisheye ! 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 "gstfisheye.h"
67
68 GST_DEBUG_CATEGORY_STATIC (gst_fisheye_debug);
69 #define GST_CAT_DEFAULT gst_fisheye_debug
70
71 #define gst_fisheye_parent_class parent_class
72 G_DEFINE_TYPE (GstFisheye, gst_fisheye, GST_TYPE_GEOMETRIC_TRANSFORM);
73
74 static gboolean
fisheye_map(GstGeometricTransform * gt,gint x,gint y,gdouble * in_x,gdouble * in_y)75 fisheye_map (GstGeometricTransform * gt, gint x, gint y, gdouble * in_x,
76 gdouble * in_y)
77 {
78 #ifndef GST_DISABLE_GST_DEBUG
79 GstFisheye *fisheye = GST_FISHEYE_CAST (gt);
80 #endif
81 gdouble norm_x;
82 gdouble norm_y;
83 gdouble r;
84
85 gdouble width = gt->width;
86 gdouble height = gt->height;
87
88 /* normalize in ((-1.0, -1.0), (1.0, 1.0) */
89 norm_x = 2.0 * x / width - 1.0;
90 norm_y = 2.0 * y / height - 1.0;
91
92 /* normalize radius to 1, simplifies following formula */
93 r = sqrt ((norm_x * norm_x + norm_y * norm_y) / 2.0);
94
95 /* the idea is roughly to map r to tan(r) */
96 /* to avoid switching back and forth to polar coordinates use
97 tangent expansion */
98 /* r = a*r + br^3 + cr^5 + dr^7 + o(8)) */
99 /* = r(a + br^2 + cr^4 + dr^6) */
100 /* so we can just multiply both x and y by the quantity in parenthesis */
101 /* forgetting about the tangent thing and simplifying things a
102 little bit we have a first linear term that, inverted, gives
103 the zoom amount in the center region (3x here), than a high
104 power term that makes the function blow up at the edges and a
105 quadratic term smooths the middle region */
106 /* coefficients must sum up to 1 to keep vertices in the +-1
107 square */
108 /* obviously this is a poor and arbitrary implementation of a
109 fisheye filter, if you have a more rigorous method or one
110 that gives better results please step up */
111 norm_x *= (0.33 + 0.1 * r * r + 0.57 * pow (r, 6.0));
112 norm_y *= (0.33 + 0.1 * r * r + 0.57 * pow (r, 6.0));
113
114 /* unnormalize */
115 *in_x = 0.5 * (norm_x + 1.0) * width;
116 *in_y = 0.5 * (norm_y + 1.0) * height;
117
118 GST_DEBUG_OBJECT (fisheye, "Inversely mapped %d %d into %lf %lf",
119 x, y, *in_x, *in_y);
120
121 return TRUE;
122 }
123
124 static void
gst_fisheye_class_init(GstFisheyeClass * klass)125 gst_fisheye_class_init (GstFisheyeClass * klass)
126 {
127 GstElementClass *gstelement_class;
128 GstGeometricTransformClass *gstgt_class;
129
130 gstelement_class = (GstElementClass *) klass;
131 gstgt_class = (GstGeometricTransformClass *) klass;
132
133 gst_element_class_set_static_metadata (gstelement_class,
134 "fisheye",
135 "Transform/Effect/Video",
136 "Simulate a fisheye lens by zooming on the center of the image and compressing the edges",
137 "Filippo Argiolas <filippo.argiolas@gmail.com>");
138
139 gstgt_class->map_func = fisheye_map;
140 }
141
142 static void
gst_fisheye_init(GstFisheye * filter)143 gst_fisheye_init (GstFisheye * filter)
144 {
145 GstGeometricTransform *gt = GST_GEOMETRIC_TRANSFORM (filter);
146
147 gt->off_edge_pixels = GST_GT_OFF_EDGES_PIXELS_CLAMP;
148 }
149
150 gboolean
gst_fisheye_plugin_init(GstPlugin * plugin)151 gst_fisheye_plugin_init (GstPlugin * plugin)
152 {
153 GST_DEBUG_CATEGORY_INIT (gst_fisheye_debug, "fisheye", 0, "fisheye");
154
155 return gst_element_register (plugin, "fisheye", GST_RANK_NONE,
156 GST_TYPE_FISHEYE);
157 }
158