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