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 
94 static void
gst_kaleidoscope_set_property(GObject * object,guint prop_id,const GValue * value,GParamSpec * pspec)95 gst_kaleidoscope_set_property (GObject * object, guint prop_id,
96     const GValue * value, GParamSpec * pspec)
97 {
98   GstKaleidoscope *kaleidoscope;
99   GstGeometricTransform *gt;
100   gdouble v;
101   gint s;
102 
103   gt = GST_GEOMETRIC_TRANSFORM_CAST (object);
104   kaleidoscope = GST_KALEIDOSCOPE_CAST (object);
105 
106   GST_OBJECT_LOCK (gt);
107   switch (prop_id) {
108     case PROP_ANGLE:
109       v = g_value_get_double (value);
110       if (v != kaleidoscope->angle) {
111         kaleidoscope->angle = v;
112         gst_geometric_transform_set_need_remap (gt);
113       }
114       break;
115     case PROP_ANGLE2:
116       v = g_value_get_double (value);
117       if (v != kaleidoscope->angle2) {
118         kaleidoscope->angle2 = v;
119         gst_geometric_transform_set_need_remap (gt);
120       }
121       break;
122     case PROP_SIDES:
123       s = g_value_get_int (value);
124       if (s != kaleidoscope->sides) {
125         kaleidoscope->sides = s;
126         gst_geometric_transform_set_need_remap (gt);
127       }
128       break;
129     default:
130       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
131       break;
132   }
133   GST_OBJECT_UNLOCK (gt);
134 }
135 
136 static void
gst_kaleidoscope_get_property(GObject * object,guint prop_id,GValue * value,GParamSpec * pspec)137 gst_kaleidoscope_get_property (GObject * object, guint prop_id,
138     GValue * value, GParamSpec * pspec)
139 {
140   GstKaleidoscope *kaleidoscope;
141 
142   kaleidoscope = GST_KALEIDOSCOPE_CAST (object);
143 
144   switch (prop_id) {
145     case PROP_ANGLE:
146       g_value_set_double (value, kaleidoscope->angle);
147       break;
148     case PROP_ANGLE2:
149       g_value_set_double (value, kaleidoscope->angle2);
150       break;
151     case PROP_SIDES:
152       g_value_set_int (value, kaleidoscope->sides);
153       break;
154     default:
155       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
156       break;
157   }
158 }
159 
160 static gboolean
kaleidoscope_map(GstGeometricTransform * gt,gint x,gint y,gdouble * in_x,gdouble * in_y)161 kaleidoscope_map (GstGeometricTransform * gt, gint x, gint y, gdouble * in_x,
162     gdouble * in_y)
163 {
164   GstCircleGeometricTransform *cgt = GST_CIRCLE_GEOMETRIC_TRANSFORM_CAST (gt);
165   GstKaleidoscope *kaleidoscope = GST_KALEIDOSCOPE_CAST (gt);
166   gdouble dx, dy;
167   gdouble distance;
168   gdouble theta;
169 
170   dx = x - cgt->precalc_x_center;
171   dy = y - cgt->precalc_y_center;
172   distance = sqrt (dx * dx + dy * dy);
173   theta = atan2 (dy, dx) - kaleidoscope->angle - kaleidoscope->angle2;
174 
175   theta = gst_gm_triangle (theta / G_PI * kaleidoscope->sides * 0.5);
176 
177   if (cgt->precalc_radius != 0) {
178     gdouble radiusc = cgt->precalc_radius / cos (theta);
179 
180     distance = radiusc * gst_gm_triangle (distance / radiusc);
181   }
182   theta += kaleidoscope->angle;
183 
184   *in_x = cgt->precalc_x_center + distance * cos (theta);
185   *in_y = cgt->precalc_y_center + distance * sin (theta);
186 
187   GST_DEBUG_OBJECT (kaleidoscope, "Inversely mapped %d %d into %lf %lf",
188       x, y, *in_x, *in_y);
189 
190   return TRUE;
191 }
192 
193 static void
gst_kaleidoscope_class_init(GstKaleidoscopeClass * klass)194 gst_kaleidoscope_class_init (GstKaleidoscopeClass * klass)
195 {
196   GObjectClass *gobject_class;
197   GstElementClass *gstelement_class;
198   GstGeometricTransformClass *gstgt_class;
199 
200   gobject_class = (GObjectClass *) klass;
201   gstelement_class = (GstElementClass *) klass;
202   gstgt_class = (GstGeometricTransformClass *) klass;
203 
204   gst_element_class_set_static_metadata (gstelement_class,
205       "kaleidoscope",
206       "Transform/Effect/Video",
207       "Applies 'kaleidoscope' geometric transform to the image",
208       "Thiago Santos<thiago.sousa.santos@collabora.co.uk>");
209 
210   gobject_class->set_property = gst_kaleidoscope_set_property;
211   gobject_class->get_property = gst_kaleidoscope_get_property;
212 
213   g_object_class_install_property (gobject_class, PROP_ANGLE,
214       g_param_spec_double ("angle", "angle",
215           "primary angle in radians of the kaleidoscope effect",
216           -G_MAXDOUBLE, G_MAXDOUBLE, DEFAULT_ANGLE,
217           GST_PARAM_CONTROLLABLE | G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
218   g_object_class_install_property (gobject_class, PROP_ANGLE2,
219       g_param_spec_double ("angle2", "angle2",
220           "secondary angle in radians of the kaleidoscope effect",
221           -G_MAXDOUBLE, G_MAXDOUBLE, DEFAULT_ANGLE,
222           GST_PARAM_CONTROLLABLE | G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
223   g_object_class_install_property (gobject_class, PROP_SIDES,
224       g_param_spec_int ("sides", "sides", "Number of sides of the kaleidoscope",
225           2, G_MAXINT, DEFAULT_SIDES,
226           GST_PARAM_CONTROLLABLE | G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
227 
228   gstgt_class->map_func = kaleidoscope_map;
229 }
230 
231 static void
gst_kaleidoscope_init(GstKaleidoscope * filter)232 gst_kaleidoscope_init (GstKaleidoscope * filter)
233 {
234   GstGeometricTransform *gt = GST_GEOMETRIC_TRANSFORM (filter);
235 
236   gt->off_edge_pixels = GST_GT_OFF_EDGES_PIXELS_CLAMP;
237   filter->angle = DEFAULT_ANGLE;
238   filter->angle2 = DEFAULT_ANGLE2;
239   filter->sides = DEFAULT_SIDES;
240 }
241 
242 gboolean
gst_kaleidoscope_plugin_init(GstPlugin * plugin)243 gst_kaleidoscope_plugin_init (GstPlugin * plugin)
244 {
245   GST_DEBUG_CATEGORY_INIT (gst_kaleidoscope_debug, "kaleidoscope", 0,
246       "kaleidoscope");
247 
248   return gst_element_register (plugin, "kaleidoscope", GST_RANK_NONE,
249       GST_TYPE_KALEIDOSCOPE);
250 }
251