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