1 /*
2  * GStreamer
3  * Copyright (C) 2010 Filippo Argiolas <filippo.argiolas@gmail.com>
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  * SECTION:element-mirror
46  * @title: mirror
47  * @see_also: geometrictransform
48  *
49  * Mirror is a geometric transform element. It splits the image into two halves
50  * and reflects one over each other.
51  *
52  * ## Example launch line
53  * |[
54  * gst-launch-1.0 -v videotestsrc ! mirror ! videoconvert ! autovideosink
55  * ]|
56  *
57  */
58 
59 #ifdef HAVE_CONFIG_H
60 #  include <config.h>
61 #endif
62 
63 #include <gst/gst.h>
64 #include <math.h>
65 
66 #include "gstmirror.h"
67 
68 GST_DEBUG_CATEGORY_STATIC (gst_mirror_debug);
69 #define GST_CAT_DEFAULT gst_mirror_debug
70 
71 enum
72 {
73   PROP_0,
74   PROP_MODE
75 };
76 
77 #define DEFAULT_PROP_MODE GST_MIRROR_MODE_LEFT
78 
79 #define gst_mirror_parent_class parent_class
80 G_DEFINE_TYPE (GstMirror, gst_mirror, GST_TYPE_GEOMETRIC_TRANSFORM);
81 
82 #define GST_TYPE_MIRROR_MODE (gst_mirror_mode_get_type())
83 static GType
gst_mirror_mode_get_type(void)84 gst_mirror_mode_get_type (void)
85 {
86   static GType mode_type = 0;
87 
88   static const GEnumValue modes[] = {
89     {GST_MIRROR_MODE_LEFT, "Split horizontally and reflect left into right",
90         "left"},
91     {GST_MIRROR_MODE_RIGHT, "Split horizontally and reflect right into left",
92         "right"},
93     {GST_MIRROR_MODE_TOP, "Split vertically and reflect top into bottom",
94         "top"},
95     {GST_MIRROR_MODE_BOTTOM, "Split vertically and reflect bottom into top",
96         "bottom"},
97     {0, NULL, NULL},
98   };
99 
100   if (!mode_type) {
101     mode_type = g_enum_register_static ("GstMirrorMode", modes);
102   }
103   return mode_type;
104 }
105 
106 
107 static void
gst_mirror_set_property(GObject * object,guint prop_id,const GValue * value,GParamSpec * pspec)108 gst_mirror_set_property (GObject * object, guint prop_id,
109     const GValue * value, GParamSpec * pspec)
110 {
111   GstMirror *filter = GST_MIRROR_CAST (object);
112 
113   switch (prop_id) {
114     case PROP_MODE:
115     {
116       gint mode;
117 
118       GST_OBJECT_LOCK (filter);
119       mode = g_value_get_enum (value);
120 
121       if (mode != filter->mode) {
122         GstGeometricTransform *gt;
123 
124         gt = GST_GEOMETRIC_TRANSFORM_CAST (object);
125         filter->mode = mode;
126         gst_geometric_transform_set_need_remap (gt);
127       }
128 
129       GST_OBJECT_UNLOCK (filter);
130       break;
131     }
132     default:
133       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
134       break;
135   }
136 }
137 
138 static void
gst_mirror_get_property(GObject * object,guint prop_id,GValue * value,GParamSpec * pspec)139 gst_mirror_get_property (GObject * object, guint prop_id,
140     GValue * value, GParamSpec * pspec)
141 {
142   GstMirror *filter = GST_MIRROR_CAST (object);
143 
144   switch (prop_id) {
145     case PROP_MODE:
146       g_value_set_enum (value, filter->mode);
147       break;
148     default:
149       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
150       break;
151   }
152 }
153 
154 static gboolean
mirror_map(GstGeometricTransform * gt,gint x,gint y,gdouble * in_x,gdouble * in_y)155 mirror_map (GstGeometricTransform * gt, gint x, gint y, gdouble * in_x,
156     gdouble * in_y)
157 {
158   GstMirror *mirror = GST_MIRROR_CAST (gt);
159 
160   gdouble hw = gt->width / 2.0 - 1.0;
161   gdouble hh = gt->height / 2.0 - 1.0;
162 
163   switch (mirror->mode) {
164     case GST_MIRROR_MODE_LEFT:
165       if (x > hw)
166         *in_x = gt->width - 1.0 - x;
167       else
168         *in_x = x;
169       *in_y = y;
170       break;
171     case GST_MIRROR_MODE_RIGHT:
172       if (x > hw)
173         *in_x = x;
174       else
175         *in_x = gt->width - 1.0 - x;
176       *in_y = y;
177       break;
178     case GST_MIRROR_MODE_TOP:
179       if (y > hh)
180         *in_y = gt->height - 1.0 - y;
181       else
182         *in_y = y;
183       *in_x = x;
184       break;
185     case GST_MIRROR_MODE_BOTTOM:
186       if (y > hh)
187         *in_y = y;
188       else
189         *in_y = gt->height - 1.0 - y;
190       *in_x = x;
191       break;
192     default:
193       g_assert_not_reached ();
194   }
195 
196   GST_DEBUG_OBJECT (mirror, "Inversely mapped %d %d into %lf %lf",
197       x, y, *in_x, *in_y);
198 
199   return TRUE;
200 }
201 
202 static void
gst_mirror_class_init(GstMirrorClass * klass)203 gst_mirror_class_init (GstMirrorClass * klass)
204 {
205   GObjectClass *gobject_class;
206   GstElementClass *gstelement_class;
207   GstGeometricTransformClass *gstgt_class;
208 
209   gobject_class = (GObjectClass *) klass;
210   gstelement_class = (GstElementClass *) klass;
211   gstgt_class = (GstGeometricTransformClass *) klass;
212 
213   gst_element_class_set_static_metadata (gstelement_class,
214       "mirror",
215       "Transform/Effect/Video",
216       "Split the image into two halves and reflect one over each other",
217       "Filippo Argiolas <filippo.argiolas@gmail.com>");
218 
219   gobject_class->set_property = gst_mirror_set_property;
220   gobject_class->get_property = gst_mirror_get_property;
221 
222   g_object_class_install_property (gobject_class, PROP_MODE,
223       g_param_spec_enum ("mode", "Mirror Mode",
224           "How to split the video frame and which side reflect",
225           GST_TYPE_MIRROR_MODE, DEFAULT_PROP_MODE,
226           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
227 
228   gstgt_class->map_func = mirror_map;
229 }
230 
231 static void
gst_mirror_init(GstMirror * filter)232 gst_mirror_init (GstMirror * filter)
233 {
234   GstGeometricTransform *gt = GST_GEOMETRIC_TRANSFORM (filter);
235 
236   filter->mode = DEFAULT_PROP_MODE;
237   gt->off_edge_pixels = GST_GT_OFF_EDGES_PIXELS_CLAMP;
238 }
239 
240 gboolean
gst_mirror_plugin_init(GstPlugin * plugin)241 gst_mirror_plugin_init (GstPlugin * plugin)
242 {
243   GST_DEBUG_CATEGORY_INIT (gst_mirror_debug, "mirror", 0, "mirror");
244 
245   return gst_element_register (plugin, "mirror", GST_RANK_NONE,
246       GST_TYPE_MIRROR);
247 }
248