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 GST_ELEMENT_REGISTER_DEFINE_WITH_CODE (waterripple, "waterripple",
92 GST_RANK_NONE, GST_TYPE_WATER_RIPPLE,
93 GST_DEBUG_CATEGORY_INIT (gst_water_ripple_debug, "waterripple", 0,
94 "waterripple"));
95
96 static void
gst_water_ripple_set_property(GObject * object,guint prop_id,const GValue * value,GParamSpec * pspec)97 gst_water_ripple_set_property (GObject * object, guint prop_id,
98 const GValue * value, GParamSpec * pspec)
99 {
100 GstWaterRipple *water_ripple;
101 GstGeometricTransform *gt;
102 gdouble v;
103
104 gt = GST_GEOMETRIC_TRANSFORM_CAST (object);
105 water_ripple = GST_WATER_RIPPLE_CAST (object);
106
107 GST_OBJECT_LOCK (gt);
108 switch (prop_id) {
109 case PROP_AMPLITUDE:
110 v = g_value_get_double (value);
111 if (v != water_ripple->amplitude) {
112 water_ripple->amplitude = v;
113 gst_geometric_transform_set_need_remap (gt);
114 }
115 break;
116 case PROP_PHASE:
117 v = g_value_get_double (value);
118 if (v != water_ripple->phase) {
119 water_ripple->phase = v;
120 gst_geometric_transform_set_need_remap (gt);
121 }
122 break;
123 case PROP_WAVELENGTH:
124 v = g_value_get_double (value);
125 if (v != water_ripple->wavelength) {
126 water_ripple->wavelength = v;
127 gst_geometric_transform_set_need_remap (gt);
128 }
129 break;
130 default:
131 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
132 break;
133 }
134 GST_OBJECT_UNLOCK (gt);
135 }
136
137 static void
gst_water_ripple_get_property(GObject * object,guint prop_id,GValue * value,GParamSpec * pspec)138 gst_water_ripple_get_property (GObject * object, guint prop_id,
139 GValue * value, GParamSpec * pspec)
140 {
141 GstWaterRipple *water_ripple;
142
143 water_ripple = GST_WATER_RIPPLE_CAST (object);
144
145 switch (prop_id) {
146 case PROP_AMPLITUDE:
147 g_value_set_double (value, water_ripple->amplitude);
148 break;
149 case PROP_PHASE:
150 g_value_set_double (value, water_ripple->phase);
151 break;
152 case PROP_WAVELENGTH:
153 g_value_set_double (value, water_ripple->wavelength);
154 break;
155 default:
156 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
157 break;
158 }
159 }
160
161 static gboolean
water_ripple_map(GstGeometricTransform * gt,gint x,gint y,gdouble * in_x,gdouble * in_y)162 water_ripple_map (GstGeometricTransform * gt, gint x, gint y, gdouble * in_x,
163 gdouble * in_y)
164 {
165 GstCircleGeometricTransform *cgt = GST_CIRCLE_GEOMETRIC_TRANSFORM_CAST (gt);
166 GstWaterRipple *water = GST_WATER_RIPPLE_CAST (gt);
167 gdouble distance;
168 gdouble dx, dy;
169
170 dx = x - cgt->precalc_x_center;
171 dy = y - cgt->precalc_y_center;
172 distance = dx * dx + dy * dy;
173
174 if (distance > cgt->precalc_radius2) {
175 *in_x = x;
176 *in_y = y;
177 } else {
178 gdouble d = sqrt (distance);
179 gdouble amount =
180 water->amplitude * sin (d / water->wavelength * G_PI * 2 -
181 water->phase);
182
183 amount *= (cgt->precalc_radius - d) / cgt->precalc_radius;
184 if (d != 0)
185 amount *= water->wavelength / d;
186 *in_x = x + dx * amount;
187 *in_y = y + dy * amount;
188 }
189
190
191 GST_DEBUG_OBJECT (water, "Inversely mapped %d %d into %lf %lf",
192 x, y, *in_x, *in_y);
193
194 return TRUE;
195 }
196
197 static void
gst_water_ripple_class_init(GstWaterRippleClass * klass)198 gst_water_ripple_class_init (GstWaterRippleClass * klass)
199 {
200 GObjectClass *gobject_class;
201 GstElementClass *gstelement_class;
202 GstGeometricTransformClass *gstgt_class;
203
204 gobject_class = (GObjectClass *) klass;
205 gstelement_class = (GstElementClass *) klass;
206 gstgt_class = (GstGeometricTransformClass *) klass;
207
208 gst_element_class_set_static_metadata (gstelement_class,
209 "waterripple",
210 "Transform/Effect/Video",
211 "Creates a water ripple effect on the image",
212 "Thiago Santos<thiago.sousa.santos@collabora.co.uk>");
213
214 gobject_class->set_property = gst_water_ripple_set_property;
215 gobject_class->get_property = gst_water_ripple_get_property;
216
217 g_object_class_install_property (gobject_class, PROP_AMPLITUDE,
218 g_param_spec_double ("amplitude", "amplitude", "amplitude",
219 -G_MAXDOUBLE, G_MAXDOUBLE, DEFAULT_AMPLITUDE,
220 GST_PARAM_CONTROLLABLE | G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
221 g_object_class_install_property (gobject_class, PROP_PHASE,
222 g_param_spec_double ("phase", "phase", "phase",
223 -G_MAXDOUBLE, G_MAXDOUBLE, DEFAULT_PHASE,
224 GST_PARAM_CONTROLLABLE | G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
225 g_object_class_install_property (gobject_class, PROP_WAVELENGTH,
226 g_param_spec_double ("wavelength", "wavelength", "wavelength",
227 -G_MAXDOUBLE, G_MAXDOUBLE, DEFAULT_WAVELENGTH,
228 GST_PARAM_CONTROLLABLE | G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
229
230 gstgt_class->map_func = water_ripple_map;
231 }
232
233 static void
gst_water_ripple_init(GstWaterRipple * filter)234 gst_water_ripple_init (GstWaterRipple * filter)
235 {
236 GstGeometricTransform *gt = GST_GEOMETRIC_TRANSFORM (filter);
237
238 gt->off_edge_pixels = GST_GT_OFF_EDGES_PIXELS_CLAMP;
239 filter->amplitude = DEFAULT_AMPLITUDE;
240 filter->phase = DEFAULT_PHASE;
241 filter->wavelength = DEFAULT_WAVELENGTH;
242 }
243