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-diffuse
52 * @title: diffuse
53 * @see_also: geometrictransform
54 *
55 * Diffuse is a geometric image transform element. It diffuses the image by
56 * moving its pixels in random directions.
57 *
58 * ## Example launch line
59 * |[
60 * gst-launch-1.0 -v videotestsrc ! diffuse ! 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 "gstdiffuse.h"
73
74 GST_DEBUG_CATEGORY_STATIC (gst_diffuse_debug);
75 #define GST_CAT_DEFAULT gst_diffuse_debug
76
77 enum
78 {
79 PROP_0,
80 PROP_SCALE
81 };
82
83 #define DEFAULT_SCALE 4
84
85 #define gst_diffuse_parent_class parent_class
86 G_DEFINE_TYPE (GstDiffuse, gst_diffuse, GST_TYPE_GEOMETRIC_TRANSFORM);
87
88 static void
gst_diffuse_set_property(GObject * object,guint prop_id,const GValue * value,GParamSpec * pspec)89 gst_diffuse_set_property (GObject * object, guint prop_id, const GValue * value,
90 GParamSpec * pspec)
91 {
92 GstDiffuse *diffuse;
93 GstGeometricTransform *gt;
94 gdouble v;
95
96 gt = GST_GEOMETRIC_TRANSFORM_CAST (object);
97 diffuse = GST_DIFFUSE_CAST (object);
98
99 GST_OBJECT_LOCK (diffuse);
100 switch (prop_id) {
101 case PROP_SCALE:
102 v = g_value_get_double (value);
103 if (v != diffuse->scale) {
104 diffuse->scale = v;
105 gst_geometric_transform_set_need_remap (gt);
106 }
107 break;
108 default:
109 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
110 break;
111 }
112 GST_OBJECT_UNLOCK (diffuse);
113 }
114
115 static void
gst_diffuse_get_property(GObject * object,guint prop_id,GValue * value,GParamSpec * pspec)116 gst_diffuse_get_property (GObject * object, guint prop_id,
117 GValue * value, GParamSpec * pspec)
118 {
119 GstDiffuse *diffuse;
120
121 diffuse = GST_DIFFUSE_CAST (object);
122
123 switch (prop_id) {
124 case PROP_SCALE:
125 g_value_set_double (value, diffuse->scale);
126 break;
127 default:
128 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
129 break;
130 }
131 }
132
133 /* Clean up */
134 static void
gst_diffuse_finalize(GObject * obj)135 gst_diffuse_finalize (GObject * obj)
136 {
137 GstDiffuse *diffuse = GST_DIFFUSE_CAST (obj);
138
139 g_free (diffuse->sin_table);
140 g_free (diffuse->cos_table);
141
142 G_OBJECT_CLASS (parent_class)->finalize (obj);
143 }
144
145 /* GObject vmethod implementations */
146
147 static gboolean
diffuse_prepare(GstGeometricTransform * trans)148 diffuse_prepare (GstGeometricTransform * trans)
149 {
150 GstDiffuse *diffuse = GST_DIFFUSE_CAST (trans);
151 gint i;
152
153 if (diffuse->sin_table)
154 return TRUE;
155
156 diffuse->sin_table = g_malloc0 (sizeof (gdouble) * 256);
157 diffuse->cos_table = g_malloc0 (sizeof (gdouble) * 256);
158
159 for (i = 0; i < 256; i++) {
160 gdouble angle = (G_PI * 2 * i) / 256.0;
161
162 diffuse->sin_table[i] = diffuse->scale * sin (angle);
163 diffuse->cos_table[i] = diffuse->scale * cos (angle);
164 }
165 return TRUE;
166 }
167
168 static gboolean
diffuse_map(GstGeometricTransform * gt,gint x,gint y,gdouble * in_x,gdouble * in_y)169 diffuse_map (GstGeometricTransform * gt, gint x, gint y, gdouble * in_x,
170 gdouble * in_y)
171 {
172 GstDiffuse *diffuse = GST_DIFFUSE_CAST (gt);
173 gint angle;
174 gdouble distance;
175
176 angle = g_random_int_range (0, 256);
177 distance = g_random_double ();
178
179 *in_x = x + distance * diffuse->sin_table[angle];
180 *in_y = y + distance * diffuse->cos_table[angle];
181
182 GST_DEBUG_OBJECT (diffuse, "Inversely mapped %d %d into %lf %lf",
183 x, y, *in_x, *in_y);
184
185 return TRUE;
186 }
187
188 static void
gst_diffuse_class_init(GstDiffuseClass * klass)189 gst_diffuse_class_init (GstDiffuseClass * klass)
190 {
191 GObjectClass *gobject_class;
192 GstElementClass *gstelement_class;
193 GstGeometricTransformClass *gstgt_class;
194
195 gobject_class = (GObjectClass *) klass;
196 gstelement_class = (GstElementClass *) klass;
197 gstgt_class = (GstGeometricTransformClass *) klass;
198
199 gst_element_class_set_static_metadata (gstelement_class,
200 "diffuse",
201 "Transform/Effect/Video",
202 "Diffuses the image by moving its pixels in random directions",
203 "Thiago Santos<thiago.sousa.santos@collabora.co.uk>");
204
205 gobject_class->finalize = gst_diffuse_finalize;
206 gobject_class->set_property = gst_diffuse_set_property;
207 gobject_class->get_property = gst_diffuse_get_property;
208
209 g_object_class_install_property (gobject_class, PROP_SCALE,
210 g_param_spec_double ("scale", "scale",
211 "Scale of the texture",
212 1, G_MAXDOUBLE, DEFAULT_SCALE,
213 GST_PARAM_CONTROLLABLE | G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
214
215 gstgt_class->prepare_func = diffuse_prepare;
216 gstgt_class->map_func = diffuse_map;
217 }
218
219 static void
gst_diffuse_init(GstDiffuse * filter)220 gst_diffuse_init (GstDiffuse * filter)
221 {
222 GstGeometricTransform *gt = GST_GEOMETRIC_TRANSFORM (filter);
223
224 gt->precalc_map = FALSE;
225
226 gt->off_edge_pixels = GST_GT_OFF_EDGES_PIXELS_CLAMP;
227 filter->scale = DEFAULT_SCALE;
228 }
229
230 gboolean
gst_diffuse_plugin_init(GstPlugin * plugin)231 gst_diffuse_plugin_init (GstPlugin * plugin)
232 {
233 GST_DEBUG_CATEGORY_INIT (gst_diffuse_debug, "diffuse", 0, "diffuse");
234
235 return gst_element_register (plugin, "diffuse", GST_RANK_NONE,
236 GST_TYPE_DIFFUSE);
237 }
238