1 /*
2 * GStreamer
3 * Copyright (C) 2010 Thiago Santos <thiago.sousa.santos@collabora.co.uk>
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 * Thanks to Jerry Huxtable <http://www.jhlabs.com> work on its java
46 * image editor and filters. The algorithms here were extracted from
47 * his code.
48 */
49
50 /**
51 * SECTION:element-waterripple
52 * @title: waterripple
53 * @see_also: geometrictransform
54 *
55 * The waterripple element creates a water ripple effect on the image.
56 *
57 * ## Example launch line
58 * |[
59 * gst-launch-1.0 -v videotestsrc ! waterripple ! videoconvert ! autovideosink
60 * ]|
61 *
62 */
63
64 #ifdef HAVE_CONFIG_H
65 # include <config.h>
66 #endif
67
68 #include <gst/gst.h>
69 #include <math.h>
70
71 #include "gstwaterripple.h"
72
73 GST_DEBUG_CATEGORY_STATIC (gst_water_ripple_debug);
74 #define GST_CAT_DEFAULT gst_water_ripple_debug
75
76 enum
77 {
78 PROP_0,
79 PROP_AMPLITUDE,
80 PROP_PHASE,
81 PROP_WAVELENGTH
82 };
83
84 #define DEFAULT_AMPLITUDE 10.0
85 #define DEFAULT_PHASE 0.0
86 #define DEFAULT_WAVELENGTH 16.0
87
88 #define gst_water_ripple_parent_class parent_class
89 G_DEFINE_TYPE (GstWaterRipple, gst_water_ripple,
90 GST_TYPE_CIRCLE_GEOMETRIC_TRANSFORM);
91
92 static void
gst_water_ripple_set_property(GObject * object,guint prop_id,const GValue * value,GParamSpec * pspec)93 gst_water_ripple_set_property (GObject * object, guint prop_id,
94 const GValue * value, GParamSpec * pspec)
95 {
96 GstWaterRipple *water_ripple;
97 GstGeometricTransform *gt;
98 gdouble v;
99
100 gt = GST_GEOMETRIC_TRANSFORM_CAST (object);
101 water_ripple = GST_WATER_RIPPLE_CAST (object);
102
103 GST_OBJECT_LOCK (gt);
104 switch (prop_id) {
105 case PROP_AMPLITUDE:
106 v = g_value_get_double (value);
107 if (v != water_ripple->amplitude) {
108 water_ripple->amplitude = v;
109 gst_geometric_transform_set_need_remap (gt);
110 }
111 break;
112 case PROP_PHASE:
113 v = g_value_get_double (value);
114 if (v != water_ripple->phase) {
115 water_ripple->phase = v;
116 gst_geometric_transform_set_need_remap (gt);
117 }
118 break;
119 case PROP_WAVELENGTH:
120 v = g_value_get_double (value);
121 if (v != water_ripple->wavelength) {
122 water_ripple->wavelength = v;
123 gst_geometric_transform_set_need_remap (gt);
124 }
125 break;
126 default:
127 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
128 break;
129 }
130 GST_OBJECT_UNLOCK (gt);
131 }
132
133 static void
gst_water_ripple_get_property(GObject * object,guint prop_id,GValue * value,GParamSpec * pspec)134 gst_water_ripple_get_property (GObject * object, guint prop_id,
135 GValue * value, GParamSpec * pspec)
136 {
137 GstWaterRipple *water_ripple;
138
139 water_ripple = GST_WATER_RIPPLE_CAST (object);
140
141 switch (prop_id) {
142 case PROP_AMPLITUDE:
143 g_value_set_double (value, water_ripple->amplitude);
144 break;
145 case PROP_PHASE:
146 g_value_set_double (value, water_ripple->phase);
147 break;
148 case PROP_WAVELENGTH:
149 g_value_set_double (value, water_ripple->wavelength);
150 break;
151 default:
152 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
153 break;
154 }
155 }
156
157 static gboolean
water_ripple_map(GstGeometricTransform * gt,gint x,gint y,gdouble * in_x,gdouble * in_y)158 water_ripple_map (GstGeometricTransform * gt, gint x, gint y, gdouble * in_x,
159 gdouble * in_y)
160 {
161 GstCircleGeometricTransform *cgt = GST_CIRCLE_GEOMETRIC_TRANSFORM_CAST (gt);
162 GstWaterRipple *water = GST_WATER_RIPPLE_CAST (gt);
163 gdouble distance;
164 gdouble dx, dy;
165
166 dx = x - cgt->precalc_x_center;
167 dy = y - cgt->precalc_y_center;
168 distance = dx * dx + dy * dy;
169
170 if (distance > cgt->precalc_radius2) {
171 *in_x = x;
172 *in_y = y;
173 } else {
174 gdouble d = sqrt (distance);
175 gdouble amount =
176 water->amplitude * sin (d / water->wavelength * G_PI * 2 -
177 water->phase);
178
179 amount *= (cgt->precalc_radius - d) / cgt->precalc_radius;
180 if (d != 0)
181 amount *= water->wavelength / d;
182 *in_x = x + dx * amount;
183 *in_y = y + dy * amount;
184 }
185
186
187 GST_DEBUG_OBJECT (water, "Inversely mapped %d %d into %lf %lf",
188 x, y, *in_x, *in_y);
189
190 return TRUE;
191 }
192
193 static void
gst_water_ripple_class_init(GstWaterRippleClass * klass)194 gst_water_ripple_class_init (GstWaterRippleClass * klass)
195 {
196 GObjectClass *gobject_class;
197 GstElementClass *gstelement_class;
198 GstGeometricTransformClass *gstgt_class;
199
200 gobject_class = (GObjectClass *) klass;
201 gstelement_class = (GstElementClass *) klass;
202 gstgt_class = (GstGeometricTransformClass *) klass;
203
204 gst_element_class_set_static_metadata (gstelement_class,
205 "waterripple",
206 "Transform/Effect/Video",
207 "Creates a water ripple effect on the image",
208 "Thiago Santos<thiago.sousa.santos@collabora.co.uk>");
209
210 gobject_class->set_property = gst_water_ripple_set_property;
211 gobject_class->get_property = gst_water_ripple_get_property;
212
213 g_object_class_install_property (gobject_class, PROP_AMPLITUDE,
214 g_param_spec_double ("amplitude", "amplitude", "amplitude",
215 -G_MAXDOUBLE, G_MAXDOUBLE, DEFAULT_AMPLITUDE,
216 GST_PARAM_CONTROLLABLE | G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
217 g_object_class_install_property (gobject_class, PROP_PHASE,
218 g_param_spec_double ("phase", "phase", "phase",
219 -G_MAXDOUBLE, G_MAXDOUBLE, DEFAULT_PHASE,
220 GST_PARAM_CONTROLLABLE | G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
221 g_object_class_install_property (gobject_class, PROP_WAVELENGTH,
222 g_param_spec_double ("wavelength", "wavelength", "wavelength",
223 -G_MAXDOUBLE, G_MAXDOUBLE, DEFAULT_WAVELENGTH,
224 GST_PARAM_CONTROLLABLE | G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
225
226 gstgt_class->map_func = water_ripple_map;
227 }
228
229 static void
gst_water_ripple_init(GstWaterRipple * filter)230 gst_water_ripple_init (GstWaterRipple * filter)
231 {
232 GstGeometricTransform *gt = GST_GEOMETRIC_TRANSFORM (filter);
233
234 gt->off_edge_pixels = GST_GT_OFF_EDGES_PIXELS_CLAMP;
235 filter->amplitude = DEFAULT_AMPLITUDE;
236 filter->phase = DEFAULT_PHASE;
237 filter->wavelength = DEFAULT_WAVELENGTH;
238 }
239
240 gboolean
gst_water_ripple_plugin_init(GstPlugin * plugin)241 gst_water_ripple_plugin_init (GstPlugin * plugin)
242 {
243 GST_DEBUG_CATEGORY_INIT (gst_water_ripple_debug, "waterripple", 0,
244 "waterripple");
245
246 return gst_element_register (plugin, "waterripple", GST_RANK_NONE,
247 GST_TYPE_WATER_RIPPLE);
248 }
249