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-stretch
46  * @title: stretch
47  * @see_also: geometrictransform
48  *
49  * The stretch element stretches the image in a circle around the center point.
50  *
51  * ## Example launch line
52  * |[
53  * gst-launch-1.0 -v videotestsrc ! stretch ! videoconvert ! autovideosink
54  * ]|
55  *
56  */
57 
58 #ifdef HAVE_CONFIG_H
59 #  include <config.h>
60 #endif
61 
62 #include <gst/gst.h>
63 #include <math.h>
64 
65 #include "gststretch.h"
66 #include "geometricmath.h"
67 
68 GST_DEBUG_CATEGORY_STATIC (gst_stretch_debug);
69 #define GST_CAT_DEFAULT gst_stretch_debug
70 
71 enum
72 {
73   PROP_0,
74   PROP_INTENSITY
75 };
76 
77 #define DEFAULT_INTENSITY 0.5
78 #define MAX_SHRINK_AMOUNT 3.0
79 
80 #define gst_stretch_parent_class parent_class
81 G_DEFINE_TYPE (GstStretch, gst_stretch, GST_TYPE_CIRCLE_GEOMETRIC_TRANSFORM);
82 
83 static void
gst_stretch_set_property(GObject * object,guint prop_id,const GValue * value,GParamSpec * pspec)84 gst_stretch_set_property (GObject * object, guint prop_id, const GValue * value,
85     GParamSpec * pspec)
86 {
87   GstStretch *stretch;
88   GstGeometricTransform *gt;
89   gdouble v;
90 
91   gt = GST_GEOMETRIC_TRANSFORM_CAST (object);
92   stretch = GST_STRETCH_CAST (object);
93 
94   GST_OBJECT_LOCK (gt);
95   switch (prop_id) {
96     case PROP_INTENSITY:
97       v = g_value_get_double (value);
98       if (v != stretch->intensity) {
99         stretch->intensity = v;
100         gst_geometric_transform_set_need_remap (gt);
101       }
102       break;
103     default:
104       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
105       break;
106   }
107   GST_OBJECT_UNLOCK (gt);
108 }
109 
110 static void
gst_stretch_get_property(GObject * object,guint prop_id,GValue * value,GParamSpec * pspec)111 gst_stretch_get_property (GObject * object, guint prop_id,
112     GValue * value, GParamSpec * pspec)
113 {
114   GstStretch *stretch;
115 
116   stretch = GST_STRETCH_CAST (object);
117 
118   switch (prop_id) {
119     case PROP_INTENSITY:
120       g_value_set_double (value, stretch->intensity);
121       break;
122     default:
123       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
124       break;
125   }
126 }
127 
128 static gboolean
stretch_map(GstGeometricTransform * gt,gint x,gint y,gdouble * in_x,gdouble * in_y)129 stretch_map (GstGeometricTransform * gt, gint x, gint y, gdouble * in_x,
130     gdouble * in_y)
131 {
132   GstCircleGeometricTransform *cgt = GST_CIRCLE_GEOMETRIC_TRANSFORM_CAST (gt);
133   GstStretch *stretch = GST_STRETCH_CAST (gt);
134 
135   gdouble norm_x, norm_y;
136   gdouble r;
137 
138   gdouble width = gt->width;
139   gdouble height = gt->height;
140   gdouble a, b;
141 
142   /* normalize in ((-1.0, -1.0), (1.0, 1.0) and traslate the center */
143   norm_x = 2.0 * (x / width - cgt->x_center);
144   norm_y = 2.0 * (y / height - cgt->y_center);
145 
146   /* calculate radius, normalize to 1 for future convenience */
147   r = sqrt (0.5 * (norm_x * norm_x + norm_y * norm_y));
148 
149   /* actually "stretch" name is a bit misleading, what the transform
150    * really does is shrink the center and gradually return to normal
151    * size while r increases. The shrink thing drags pixels giving
152    * stretching the image around the center */
153 
154   /* a is the current maximum shrink amount, it goes from 1.0 to
155    * MAX_SHRINK_AMOUNT * intensity */
156   /* smoothstep goes from 0.0 when r == 0 to b when r == radius */
157   /* total shrink factor is MAX_SHRINK_AMOUNT at center and gradually
158    * decreases while r goes to radius */
159   a = 1.0 + (MAX_SHRINK_AMOUNT - 1.0) * stretch->intensity;
160   b = a - 1.0;
161 
162   norm_x *= a - b * gst_gm_smoothstep (0.0, cgt->radius, r);
163   norm_y *= a - b * gst_gm_smoothstep (0.0, cgt->radius, r);
164 
165   /* unnormalize */
166   *in_x = (0.5 * norm_x + cgt->x_center) * width;
167   *in_y = (0.5 * norm_y + cgt->y_center) * height;
168 
169   GST_DEBUG_OBJECT (stretch, "Inversely mapped %d %d into %lf %lf",
170       x, y, *in_x, *in_y);
171 
172   return TRUE;
173 }
174 
175 static void
gst_stretch_class_init(GstStretchClass * klass)176 gst_stretch_class_init (GstStretchClass * klass)
177 {
178   GObjectClass *gobject_class;
179   GstElementClass *gstelement_class;
180   GstGeometricTransformClass *gstgt_class;
181 
182   gobject_class = (GObjectClass *) klass;
183   gstelement_class = (GstElementClass *) klass;
184   gstgt_class = (GstGeometricTransformClass *) klass;
185 
186   gst_element_class_set_static_metadata (gstelement_class,
187       "stretch",
188       "Transform/Effect/Video",
189       "Stretch the image in a circle around the center point",
190       "Filippo Argiolas <filippo.argiolas@gmail.com>");
191 
192   gobject_class->set_property = gst_stretch_set_property;
193   gobject_class->get_property = gst_stretch_get_property;
194 
195   g_object_class_install_property (gobject_class, PROP_INTENSITY,
196       g_param_spec_double ("intensity", "intensity",
197           "Intensity of the stretch effect",
198           0.0, 1.0, DEFAULT_INTENSITY,
199           GST_PARAM_CONTROLLABLE | G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
200 
201   gstgt_class->map_func = stretch_map;
202 }
203 
204 static void
gst_stretch_init(GstStretch * filter)205 gst_stretch_init (GstStretch * filter)
206 {
207   GstGeometricTransform *gt = GST_GEOMETRIC_TRANSFORM (filter);
208 
209   filter->intensity = DEFAULT_INTENSITY;
210   gt->off_edge_pixels = GST_GT_OFF_EDGES_PIXELS_CLAMP;
211 }
212 
213 gboolean
gst_stretch_plugin_init(GstPlugin * plugin)214 gst_stretch_plugin_init (GstPlugin * plugin)
215 {
216   GST_DEBUG_CATEGORY_INIT (gst_stretch_debug, "stretch", 0, "stretch");
217 
218   return gst_element_register (plugin, "stretch", GST_RANK_NONE,
219       GST_TYPE_STRETCH);
220 }
221