3 * Copyright (C) 2010 Thiago Santos <thiago.sousa.santos@collabora.co.uk>
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:
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
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.
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
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.
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.
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.
45 * Thanks to Jerry Huxtable <http://www.jhlabs.com> work on its java
46 * image editor and filters. The algorithms here were extracted from
51 * SECTION:element-pinch
53 * @see_also: geometrictransform
55 * Pinch applies a 'pinch' geometric transform to the image.
57 * ## Example launch line
59 * gst-launch-1.0 -v videotestsrc ! pinch ! videoconvert ! autovideosink
73 GST_DEBUG_CATEGORY_STATIC (gst_pinch_debug);
74 #define GST_CAT_DEFAULT gst_pinch_debug
82 #define DEFAULT_INTENSITY 0.5
84 #define gst_pinch_parent_class parent_class
85 G_DEFINE_TYPE (GstPinch, gst_pinch, GST_TYPE_CIRCLE_GEOMETRIC_TRANSFORM);
86 GST_ELEMENT_REGISTER_DEFINE_WITH_CODE (pinch, "pinch", GST_RANK_NONE,
87 GST_TYPE_PINCH, GST_DEBUG_CATEGORY_INIT (gst_pinch_debug, "pinch", 0,
91 gst_pinch_set_property (GObject * object, guint prop_id, const GValue * value,
95 GstGeometricTransform *gt;
98 gt = GST_GEOMETRIC_TRANSFORM_CAST (object);
99 pinch = GST_PINCH_CAST (object);
101 GST_OBJECT_LOCK (gt);
104 v = g_value_get_double (value);
105 if (v != pinch->intensity) {
106 pinch->intensity = v;
107 gst_geometric_transform_set_need_remap (gt);
111 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
114 GST_OBJECT_UNLOCK (gt);
118 gst_pinch_get_property (GObject * object, guint prop_id,
119 GValue * value, GParamSpec * pspec)
123 pinch = GST_PINCH_CAST (object);
127 g_value_set_double (value, pinch->intensity);
130 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
136 pinch_map (GstGeometricTransform * gt, gint x, gint y, gdouble * in_x,
139 GstCircleGeometricTransform *cgt = GST_CIRCLE_GEOMETRIC_TRANSFORM_CAST (gt);
140 GstPinch *pinch = GST_PINCH_CAST (gt);
144 dx = x - cgt->precalc_x_center;
145 dy = y - cgt->precalc_y_center;
146 distance = dx * dx + dy * dy;
148 GST_LOG_OBJECT (pinch, "Center %0.5lf (%0.2lf) %0.5lf (%0.2lf)",
149 cgt->precalc_x_center, cgt->x_center, cgt->precalc_y_center,
151 GST_LOG_OBJECT (pinch, "Input %d %d, distance=%lf, radius2=%lf, dx=%lf"
152 ", dy=%lf", x, y, distance, cgt->precalc_radius2, dx, dy);
154 if (distance > cgt->precalc_radius2 || distance == 0) {
158 gdouble d = sqrt (distance / cgt->precalc_radius2);
159 gdouble t = pow (sin (G_PI * 0.5 * d), -pinch->intensity);
164 GST_LOG_OBJECT (pinch, "D=%lf, t=%lf, dx=%lf" ", dy=%lf", d, t, dx, dy);
166 *in_x = cgt->precalc_x_center + dx;
167 *in_y = cgt->precalc_y_center + dy;
170 GST_DEBUG_OBJECT (pinch, "Inversely mapped %d %d into %lf %lf",
177 gst_pinch_class_init (GstPinchClass * klass)
179 GObjectClass *gobject_class;
180 GstElementClass *gstelement_class;
181 GstGeometricTransformClass *gstgt_class;
183 gobject_class = (GObjectClass *) klass;
184 gstelement_class = (GstElementClass *) klass;
185 gstgt_class = (GstGeometricTransformClass *) klass;
187 gst_element_class_set_static_metadata (gstelement_class,
189 "Transform/Effect/Video",
190 "Applies 'pinch' geometric transform to the image",
191 "Thiago Santos<thiago.sousa.santos@collabora.co.uk>");
193 gobject_class->set_property = gst_pinch_set_property;
194 gobject_class->get_property = gst_pinch_get_property;
196 g_object_class_install_property (gobject_class, PROP_INTENSITY,
197 g_param_spec_double ("intensity", "intensity",
198 "intensity of the pinch effect",
199 -1.0, 1.0, DEFAULT_INTENSITY,
200 GST_PARAM_CONTROLLABLE | G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
202 gstgt_class->map_func = pinch_map;
206 gst_pinch_init (GstPinch * filter)
208 GstGeometricTransform *gt = GST_GEOMETRIC_TRANSFORM (filter);
210 gt->off_edge_pixels = GST_GT_OFF_EDGES_PIXELS_CLAMP;
211 filter->intensity = DEFAULT_INTENSITY;