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