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-marble
52 * @title: marble
53 * @see_also: geometrictransform
54 *
55 * Marble is a geometric image transform element. It applies a marbling effect
56 * to the image.
57 *
58 * ## Example launch line
59 * |[
60 * gst-launch-1.0 -v videotestsrc ! marble ! videoconvert ! autovideosink
61 * ]|
62 *
63 */
64
65 #ifdef HAVE_CONFIG_H
66 # include <config.h>
67 #endif
68
69 #include <gst/gst.h>
70 #include <math.h>
71
72 #include "gstmarble.h"
73
74 GST_DEBUG_CATEGORY_STATIC (gst_marble_debug);
75 #define GST_CAT_DEFAULT gst_marble_debug
76
77 enum
78 {
79 PROP_0,
80 PROP_XSCALE,
81 PROP_YSCALE,
82 PROP_AMOUNT,
83 PROP_TURBULENCE
84 };
85
86 #define DEFAULT_XSCALE 4
87 #define DEFAULT_YSCALE 4
88 #define DEFAULT_AMOUNT 1
89 #define DEFAULT_TURBULENCE 1
90
91 #define gst_marble_parent_class parent_class
92 G_DEFINE_TYPE (GstMarble, gst_marble, GST_TYPE_GEOMETRIC_TRANSFORM);
93 GST_ELEMENT_REGISTER_DEFINE_WITH_CODE (marble, "marble", GST_RANK_NONE,
94 GST_TYPE_MARBLE, GST_DEBUG_CATEGORY_INIT (gst_marble_debug, "marble", 0,
95 "marble"));
96 static void
gst_marble_set_property(GObject * object,guint prop_id,const GValue * value,GParamSpec * pspec)97 gst_marble_set_property (GObject * object, guint prop_id, const GValue * value,
98 GParamSpec * pspec)
99 {
100 GstMarble *marble;
101 GstGeometricTransform *gt;
102 gdouble v;
103
104 gt = GST_GEOMETRIC_TRANSFORM_CAST (object);
105 marble = GST_MARBLE_CAST (object);
106
107 GST_OBJECT_LOCK (gt);
108 switch (prop_id) {
109 case PROP_XSCALE:
110 v = g_value_get_double (value);
111 if (v != marble->xscale) {
112 marble->xscale = v;
113 gst_geometric_transform_set_need_remap (gt);
114 }
115 break;
116 case PROP_YSCALE:
117 v = g_value_get_double (value);
118 if (v != marble->yscale) {
119 marble->yscale = v;
120 gst_geometric_transform_set_need_remap (gt);
121 }
122 break;
123 case PROP_AMOUNT:
124 v = g_value_get_double (value);
125 if (v != marble->amount) {
126 marble->amount = v;
127 gst_geometric_transform_set_need_remap (gt);
128 }
129 break;
130 case PROP_TURBULENCE:
131 v = g_value_get_double (value);
132 if (v != marble->turbulence) {
133 marble->turbulence = v;
134 gst_geometric_transform_set_need_remap (gt);
135 }
136 break;
137 default:
138 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
139 break;
140 }
141 GST_OBJECT_UNLOCK (gt);
142 }
143
144 static void
gst_marble_get_property(GObject * object,guint prop_id,GValue * value,GParamSpec * pspec)145 gst_marble_get_property (GObject * object, guint prop_id,
146 GValue * value, GParamSpec * pspec)
147 {
148 GstMarble *marble;
149
150 marble = GST_MARBLE_CAST (object);
151
152 switch (prop_id) {
153 case PROP_XSCALE:
154 g_value_set_double (value, marble->xscale);
155 break;
156 case PROP_YSCALE:
157 g_value_set_double (value, marble->yscale);
158 break;
159 case PROP_AMOUNT:
160 g_value_set_double (value, marble->amount);
161 break;
162 case PROP_TURBULENCE:
163 g_value_set_double (value, marble->turbulence);
164 break;
165 default:
166 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
167 break;
168 }
169 }
170
171 /* Clean up */
172 static void
gst_marble_finalize(GObject * obj)173 gst_marble_finalize (GObject * obj)
174 {
175 GstMarble *marble = GST_MARBLE_CAST (obj);
176
177 gst_gm_noise_free (marble->noise);
178 g_free (marble->sin_table);
179 g_free (marble->cos_table);
180
181 G_OBJECT_CLASS (parent_class)->finalize (obj);
182 }
183
184 static gboolean
marble_prepare(GstGeometricTransform * trans)185 marble_prepare (GstGeometricTransform * trans)
186 {
187 GstMarble *marble = GST_MARBLE_CAST (trans);
188 gint i;
189
190 if (!marble->noise) {
191 marble->noise = gst_gm_noise_new ();
192 }
193
194 g_free (marble->sin_table);
195 g_free (marble->cos_table);
196
197 marble->sin_table = g_malloc0 (sizeof (gdouble) * 256);
198 marble->cos_table = g_malloc0 (sizeof (gdouble) * 256);
199
200 for (i = 0; i < 256; i++) {
201 gdouble angle = (G_PI * 2 * i) / 256.0 * marble->turbulence;
202
203 marble->sin_table[i] = -marble->yscale * sin (angle);
204 marble->cos_table[i] = marble->yscale * cos (angle);
205 }
206 return TRUE;
207 }
208
209 static gboolean
marble_map(GstGeometricTransform * gt,gint x,gint y,gdouble * in_x,gdouble * in_y)210 marble_map (GstGeometricTransform * gt, gint x, gint y, gdouble * in_x,
211 gdouble * in_y)
212 {
213 GstMarble *marble = GST_MARBLE_CAST (gt);
214 gint displacement;
215
216 displacement = 127 * (1 + gst_gm_noise_2 (marble->noise, x / marble->xscale,
217 y / marble->xscale));
218 displacement = CLAMP (displacement, 0, 255);
219
220 *in_x = x + marble->sin_table[displacement];
221 *in_y = y + marble->cos_table[displacement];
222
223 GST_DEBUG_OBJECT (marble, "Inversely mapped %d %d into %lf %lf",
224 x, y, *in_x, *in_y);
225
226 return TRUE;
227 }
228
229 static void
gst_marble_class_init(GstMarbleClass * klass)230 gst_marble_class_init (GstMarbleClass * klass)
231 {
232 GObjectClass *gobject_class;
233 GstElementClass *gstelement_class;
234 GstGeometricTransformClass *gstgt_class;
235
236 gobject_class = (GObjectClass *) klass;
237 gstelement_class = (GstElementClass *) klass;
238 gstgt_class = (GstGeometricTransformClass *) klass;
239
240 gst_element_class_set_static_metadata (gstelement_class,
241 "marble",
242 "Transform/Effect/Video",
243 "Applies a marbling effect to the image",
244 "Thiago Santos<thiago.sousa.santos@collabora.co.uk>");
245
246 gobject_class->finalize = gst_marble_finalize;
247 gobject_class->set_property = gst_marble_set_property;
248 gobject_class->get_property = gst_marble_get_property;
249
250 g_object_class_install_property (gobject_class, PROP_XSCALE,
251 g_param_spec_double ("x-scale", "x-scale",
252 "X scale of the texture",
253 0, G_MAXDOUBLE, DEFAULT_XSCALE,
254 GST_PARAM_CONTROLLABLE | G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
255 g_object_class_install_property (gobject_class, PROP_YSCALE,
256 g_param_spec_double ("y-scale", "y-scale",
257 "Y scale of the texture",
258 0, G_MAXDOUBLE, DEFAULT_YSCALE,
259 GST_PARAM_CONTROLLABLE | G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
260 g_object_class_install_property (gobject_class, PROP_AMOUNT,
261 g_param_spec_double ("amount", "amount",
262 "Amount of effect",
263 0.0, 1.0, DEFAULT_AMOUNT,
264 GST_PARAM_CONTROLLABLE | G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
265 g_object_class_install_property (gobject_class, PROP_YSCALE,
266 g_param_spec_double ("turbulence", "turbulence",
267 "Turbulence of the effect",
268 0.0, 1.0, DEFAULT_TURBULENCE,
269 GST_PARAM_CONTROLLABLE | G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
270
271 gstgt_class->prepare_func = marble_prepare;
272 gstgt_class->map_func = marble_map;
273 }
274
275 static void
gst_marble_init(GstMarble * filter)276 gst_marble_init (GstMarble * filter)
277 {
278 GstGeometricTransform *gt = GST_GEOMETRIC_TRANSFORM (filter);
279
280 gt->precalc_map = TRUE;
281 gt->off_edge_pixels = GST_GT_OFF_EDGES_PIXELS_CLAMP;
282 filter->xscale = DEFAULT_XSCALE;
283 filter->yscale = DEFAULT_YSCALE;
284 filter->amount = DEFAULT_AMOUNT;
285 filter->turbulence = DEFAULT_TURBULENCE;
286 }
287