• 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-kaleidoscope
52  * @title: kaleidoscope
53  * @see_also: geometrictransform
54  *
55  * The kaleidscope element applies 'kaleidoscope' geometric transform to the
56  * image.
57  *
58  * ## Example launch line
59  * |[
60  * gst-launch-1.0 -v videotestsrc ! kaleidoscope ! 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 "geometricmath.h"
73 #include "gstkaleidoscope.h"
74 
75 GST_DEBUG_CATEGORY_STATIC (gst_kaleidoscope_debug);
76 #define GST_CAT_DEFAULT gst_kaleidoscope_debug
77 
78 enum
79 {
80   PROP_0,
81   PROP_ANGLE,
82   PROP_ANGLE2,
83   PROP_SIDES
84 };
85 
86 #define DEFAULT_ANGLE 0
87 #define DEFAULT_ANGLE2 0
88 #define DEFAULT_SIDES 3
89 
90 #define gst_kaleidoscope_parent_class parent_class
91 G_DEFINE_TYPE (GstKaleidoscope, gst_kaleidoscope,
92     GST_TYPE_CIRCLE_GEOMETRIC_TRANSFORM);
93 GST_ELEMENT_REGISTER_DEFINE_WITH_CODE (kaleidoscope, "kaleidoscope",
94     GST_RANK_NONE, GST_TYPE_KALEIDOSCOPE,
95     GST_DEBUG_CATEGORY_INIT (gst_kaleidoscope_debug, "kaleidoscope", 0,
96         "kaleidoscope"));
97 
98 static void
gst_kaleidoscope_set_property(GObject * object,guint prop_id,const GValue * value,GParamSpec * pspec)99 gst_kaleidoscope_set_property (GObject * object, guint prop_id,
100     const GValue * value, GParamSpec * pspec)
101 {
102   GstKaleidoscope *kaleidoscope;
103   GstGeometricTransform *gt;
104   gdouble v;
105   gint s;
106 
107   gt = GST_GEOMETRIC_TRANSFORM_CAST (object);
108   kaleidoscope = GST_KALEIDOSCOPE_CAST (object);
109 
110   GST_OBJECT_LOCK (gt);
111   switch (prop_id) {
112     case PROP_ANGLE:
113       v = g_value_get_double (value);
114       if (v != kaleidoscope->angle) {
115         kaleidoscope->angle = v;
116         gst_geometric_transform_set_need_remap (gt);
117       }
118       break;
119     case PROP_ANGLE2:
120       v = g_value_get_double (value);
121       if (v != kaleidoscope->angle2) {
122         kaleidoscope->angle2 = v;
123         gst_geometric_transform_set_need_remap (gt);
124       }
125       break;
126     case PROP_SIDES:
127       s = g_value_get_int (value);
128       if (s != kaleidoscope->sides) {
129         kaleidoscope->sides = s;
130         gst_geometric_transform_set_need_remap (gt);
131       }
132       break;
133     default:
134       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
135       break;
136   }
137   GST_OBJECT_UNLOCK (gt);
138 }
139 
140 static void
gst_kaleidoscope_get_property(GObject * object,guint prop_id,GValue * value,GParamSpec * pspec)141 gst_kaleidoscope_get_property (GObject * object, guint prop_id,
142     GValue * value, GParamSpec * pspec)
143 {
144   GstKaleidoscope *kaleidoscope;
145 
146   kaleidoscope = GST_KALEIDOSCOPE_CAST (object);
147 
148   switch (prop_id) {
149     case PROP_ANGLE:
150       g_value_set_double (value, kaleidoscope->angle);
151       break;
152     case PROP_ANGLE2:
153       g_value_set_double (value, kaleidoscope->angle2);
154       break;
155     case PROP_SIDES:
156       g_value_set_int (value, kaleidoscope->sides);
157       break;
158     default:
159       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
160       break;
161   }
162 }
163 
164 static gboolean
kaleidoscope_map(GstGeometricTransform * gt,gint x,gint y,gdouble * in_x,gdouble * in_y)165 kaleidoscope_map (GstGeometricTransform * gt, gint x, gint y, gdouble * in_x,
166     gdouble * in_y)
167 {
168   GstCircleGeometricTransform *cgt = GST_CIRCLE_GEOMETRIC_TRANSFORM_CAST (gt);
169   GstKaleidoscope *kaleidoscope = GST_KALEIDOSCOPE_CAST (gt);
170   gdouble dx, dy;
171   gdouble distance;
172   gdouble theta;
173 
174   dx = x - cgt->precalc_x_center;
175   dy = y - cgt->precalc_y_center;
176   distance = sqrt (dx * dx + dy * dy);
177   theta = atan2 (dy, dx) - kaleidoscope->angle - kaleidoscope->angle2;
178 
179   theta = gst_gm_triangle (theta / G_PI * kaleidoscope->sides * 0.5);
180 
181   if (cgt->precalc_radius != 0) {
182     gdouble radiusc = cgt->precalc_radius / cos (theta);
183 
184     distance = radiusc * gst_gm_triangle (distance / radiusc);
185   }
186   theta += kaleidoscope->angle;
187 
188   *in_x = cgt->precalc_x_center + distance * cos (theta);
189   *in_y = cgt->precalc_y_center + distance * sin (theta);
190 
191   GST_DEBUG_OBJECT (kaleidoscope, "Inversely mapped %d %d into %lf %lf",
192       x, y, *in_x, *in_y);
193 
194   return TRUE;
195 }
196 
197 static void
gst_kaleidoscope_class_init(GstKaleidoscopeClass * klass)198 gst_kaleidoscope_class_init (GstKaleidoscopeClass * 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       "kaleidoscope",
210       "Transform/Effect/Video",
211       "Applies 'kaleidoscope' geometric transform to the image",
212       "Thiago Santos<thiago.sousa.santos@collabora.co.uk>");
213 
214   gobject_class->set_property = gst_kaleidoscope_set_property;
215   gobject_class->get_property = gst_kaleidoscope_get_property;
216 
217   g_object_class_install_property (gobject_class, PROP_ANGLE,
218       g_param_spec_double ("angle", "angle",
219           "primary angle in radians of the kaleidoscope effect",
220           -G_MAXDOUBLE, G_MAXDOUBLE, DEFAULT_ANGLE,
221           GST_PARAM_CONTROLLABLE | G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
222   g_object_class_install_property (gobject_class, PROP_ANGLE2,
223       g_param_spec_double ("angle2", "angle2",
224           "secondary angle in radians of the kaleidoscope effect",
225           -G_MAXDOUBLE, G_MAXDOUBLE, DEFAULT_ANGLE,
226           GST_PARAM_CONTROLLABLE | G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
227   g_object_class_install_property (gobject_class, PROP_SIDES,
228       g_param_spec_int ("sides", "sides", "Number of sides of the kaleidoscope",
229           2, G_MAXINT, DEFAULT_SIDES,
230           GST_PARAM_CONTROLLABLE | G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
231 
232   gstgt_class->map_func = kaleidoscope_map;
233 }
234 
235 static void
gst_kaleidoscope_init(GstKaleidoscope * filter)236 gst_kaleidoscope_init (GstKaleidoscope * filter)
237 {
238   GstGeometricTransform *gt = GST_GEOMETRIC_TRANSFORM (filter);
239 
240   gt->off_edge_pixels = GST_GT_OFF_EDGES_PIXELS_CLAMP;
241   filter->angle = DEFAULT_ANGLE;
242   filter->angle2 = DEFAULT_ANGLE2;
243   filter->sides = DEFAULT_SIDES;
244 }
245