Olivier Naudan | b28313f | 2012-04-16 08:10:18 -0400 | [diff] [blame] | 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., 59 Temple Place - Suite 330, |
| 41 | * Boston, MA 02111-1307, USA. |
| 42 | */ |
| 43 | |
| 44 | #ifdef HAVE_CONFIG_H |
| 45 | # include <config.h> |
| 46 | #endif |
| 47 | |
| 48 | #include <gst/gst.h> |
| 49 | #include <math.h> |
| 50 | |
| 51 | #include "gstmirror.h" |
| 52 | |
| 53 | GST_DEBUG_CATEGORY_STATIC (gst_mirror_debug); |
| 54 | #define GST_CAT_DEFAULT gst_mirror_debug |
| 55 | |
| 56 | enum |
| 57 | { |
| 58 | PROP_0, |
| 59 | PROP_MODE |
| 60 | }; |
| 61 | |
| 62 | #define DEFAULT_PROP_MODE GST_MIRROR_MODE_LEFT |
| 63 | |
Sebastian Dröge | 7e66186 | 2012-05-21 15:50:23 +0200 | [diff] [blame^] | 64 | #define gst_mirror_parent_class parent_class |
| 65 | G_DEFINE_TYPE (GstMirror, gst_mirror, GST_TYPE_GEOMETRIC_TRANSFORM); |
Olivier Naudan | b28313f | 2012-04-16 08:10:18 -0400 | [diff] [blame] | 66 | |
| 67 | #define GST_TYPE_MIRROR_MODE (gst_mirror_mode_get_type()) |
| 68 | static GType |
| 69 | gst_mirror_mode_get_type (void) |
| 70 | { |
| 71 | static GType mode_type = 0; |
| 72 | |
| 73 | static const GEnumValue modes[] = { |
| 74 | {GST_MIRROR_MODE_LEFT, "Split horizontally and reflect left into right", |
| 75 | "left"}, |
| 76 | {GST_MIRROR_MODE_RIGHT, "Split horizontally and reflect right into left", |
| 77 | "right"}, |
| 78 | {GST_MIRROR_MODE_TOP, "Split vertically and reflect top into bottom", |
| 79 | "top"}, |
| 80 | {GST_MIRROR_MODE_BOTTOM, "Split vertically and reflect bottom into top", |
| 81 | "bottom"}, |
| 82 | {0, NULL, NULL}, |
| 83 | }; |
| 84 | |
| 85 | if (!mode_type) { |
| 86 | mode_type = g_enum_register_static ("GstMirrorMode", modes); |
| 87 | } |
| 88 | return mode_type; |
| 89 | } |
| 90 | |
| 91 | |
| 92 | static void |
| 93 | gst_mirror_set_property (GObject * object, guint prop_id, |
| 94 | const GValue * value, GParamSpec * pspec) |
| 95 | { |
| 96 | GstMirror *filter = GST_MIRROR_CAST (object); |
| 97 | |
| 98 | switch (prop_id) { |
| 99 | case PROP_MODE: |
| 100 | GST_OBJECT_LOCK (filter); |
| 101 | filter->mode = g_value_get_enum (value); |
| 102 | GST_OBJECT_UNLOCK (filter); |
| 103 | break; |
| 104 | default: |
| 105 | G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec); |
| 106 | break; |
| 107 | } |
| 108 | } |
| 109 | |
| 110 | static void |
| 111 | gst_mirror_get_property (GObject * object, guint prop_id, |
| 112 | GValue * value, GParamSpec * pspec) |
| 113 | { |
| 114 | GstMirror *filter = GST_MIRROR_CAST (object); |
| 115 | |
| 116 | switch (prop_id) { |
| 117 | case PROP_MODE: |
| 118 | g_value_set_enum (value, filter->mode); |
| 119 | break; |
| 120 | default: |
| 121 | G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec); |
| 122 | break; |
| 123 | } |
| 124 | } |
| 125 | |
Olivier Naudan | b28313f | 2012-04-16 08:10:18 -0400 | [diff] [blame] | 126 | static gboolean |
| 127 | mirror_map (GstGeometricTransform * gt, gint x, gint y, gdouble * in_x, |
| 128 | gdouble * in_y) |
| 129 | { |
| 130 | GstMirror *mirror = GST_MIRROR_CAST (gt); |
| 131 | |
| 132 | gdouble hw = gt->width / 2.0 - 1.0; |
| 133 | gdouble hh = gt->height / 2.0 - 1.0; |
| 134 | |
| 135 | switch (mirror->mode) { |
| 136 | case GST_MIRROR_MODE_LEFT: |
| 137 | if (x > hw) |
| 138 | *in_x = gt->width - 1.0 - x; |
| 139 | else |
| 140 | *in_x = x; |
| 141 | *in_y = y; |
| 142 | break; |
| 143 | case GST_MIRROR_MODE_RIGHT: |
| 144 | if (x > hw) |
| 145 | *in_x = x; |
| 146 | else |
| 147 | *in_x = gt->width - 1.0 - x; |
| 148 | *in_y = y; |
| 149 | break; |
| 150 | case GST_MIRROR_MODE_TOP: |
| 151 | if (y > hh) |
| 152 | *in_y = gt->height - 1.0 - y; |
| 153 | else |
| 154 | *in_y = y; |
| 155 | *in_x = x; |
| 156 | break; |
| 157 | case GST_MIRROR_MODE_BOTTOM: |
| 158 | if (y > hh) |
| 159 | *in_y = y; |
| 160 | else |
| 161 | *in_y = gt->height - 1.0 - y; |
| 162 | *in_x = x; |
| 163 | break; |
| 164 | default: |
| 165 | g_assert_not_reached (); |
| 166 | } |
| 167 | |
| 168 | GST_DEBUG_OBJECT (mirror, "Inversely mapped %d %d into %lf %lf", |
| 169 | x, y, *in_x, *in_y); |
| 170 | |
| 171 | return TRUE; |
| 172 | } |
| 173 | |
| 174 | static void |
| 175 | gst_mirror_class_init (GstMirrorClass * klass) |
| 176 | { |
| 177 | GObjectClass *gobject_class; |
Sebastian Dröge | 7e66186 | 2012-05-21 15:50:23 +0200 | [diff] [blame^] | 178 | GstElementClass *gstelement_class; |
Olivier Naudan | b28313f | 2012-04-16 08:10:18 -0400 | [diff] [blame] | 179 | GstGeometricTransformClass *gstgt_class; |
| 180 | |
| 181 | gobject_class = (GObjectClass *) klass; |
Sebastian Dröge | 7e66186 | 2012-05-21 15:50:23 +0200 | [diff] [blame^] | 182 | gstelement_class = (GstElementClass *) klass; |
Olivier Naudan | b28313f | 2012-04-16 08:10:18 -0400 | [diff] [blame] | 183 | gstgt_class = (GstGeometricTransformClass *) klass; |
| 184 | |
Sebastian Dröge | 7e66186 | 2012-05-21 15:50:23 +0200 | [diff] [blame^] | 185 | gst_element_class_set_details_simple (gstelement_class, |
| 186 | "mirror", |
| 187 | "Transform/Effect/Video", |
| 188 | "Split the image into two halves and reflect one over each other", |
| 189 | "Filippo Argiolas <filippo.argiolas@gmail.com>"); |
| 190 | |
Olivier Naudan | b28313f | 2012-04-16 08:10:18 -0400 | [diff] [blame] | 191 | gobject_class->set_property = gst_mirror_set_property; |
| 192 | gobject_class->get_property = gst_mirror_get_property; |
| 193 | |
| 194 | g_object_class_install_property (gobject_class, PROP_MODE, |
| 195 | g_param_spec_enum ("mode", "Mirror Mode", |
| 196 | "How to split the video frame and which side reflect", |
| 197 | GST_TYPE_MIRROR_MODE, DEFAULT_PROP_MODE, |
| 198 | G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS)); |
| 199 | |
| 200 | parent_class = g_type_class_peek_parent (klass); |
| 201 | |
| 202 | gstgt_class->map_func = mirror_map; |
| 203 | } |
| 204 | |
| 205 | static void |
Sebastian Dröge | 7e66186 | 2012-05-21 15:50:23 +0200 | [diff] [blame^] | 206 | gst_mirror_init (GstMirror * filter) |
Olivier Naudan | b28313f | 2012-04-16 08:10:18 -0400 | [diff] [blame] | 207 | { |
| 208 | GstGeometricTransform *gt = GST_GEOMETRIC_TRANSFORM (filter); |
| 209 | |
| 210 | filter->mode = DEFAULT_PROP_MODE; |
| 211 | gt->off_edge_pixels = GST_GT_OFF_EDGES_PIXELS_CLAMP; |
| 212 | } |
| 213 | |
| 214 | gboolean |
| 215 | gst_mirror_plugin_init (GstPlugin * plugin) |
| 216 | { |
| 217 | GST_DEBUG_CATEGORY_INIT (gst_mirror_debug, "mirror", 0, "mirror"); |
| 218 | |
| 219 | return gst_element_register (plugin, "mirror", GST_RANK_NONE, |
| 220 | GST_TYPE_MIRROR); |
| 221 | } |