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  * As cvdilate_erode and cverode are all the same, except for the transform function,
46  * we hope this base class should keep maintenance easier.
47  */
48 
49 #ifdef HAVE_CONFIG_H
50 #  include <config.h>
51 #endif
52 
53 #include "gst/opencv/gstopencvutils.h"
54 #include "gstcvdilateerode.h"
55 
56 #include <opencv2/core.hpp>
57 
58 /*
59 GST_DEBUG_CATEGORY_STATIC (gst_cv_dilate_erode_debug);
60 #define GST_CAT_DEFAULT gst_cv_dilate_erode_debug
61 */
62 
63 
64 /* Filter signals and args */
65 enum
66 {
67   /* FILL ME */
68   LAST_SIGNAL
69 };
70 enum
71 {
72   PROP_0,
73   PROP_ITERATIONS,
74 };
75 
76 #define DEFAULT_ITERATIONS 1
77 
78 static void gst_cv_dilate_erode_class_init (GstCvDilateErodeClass * klass);
79 static void gst_cv_dilate_erode_init (GstCvDilateErode * filter,
80     GstCvDilateErodeClass * gclass);
81 
82 static void gst_cv_dilate_erode_set_property (GObject * object, guint prop_id,
83     const GValue * value, GParamSpec * pspec);
84 static void gst_cv_dilate_erode_get_property (GObject * object, guint prop_id,
85     GValue * value, GParamSpec * pspec);
86 
87 GType
gst_cv_dilate_erode_get_type(void)88 gst_cv_dilate_erode_get_type (void)
89 {
90   static volatile gsize opencv_dilate_erode_type = 0;
91 
92   if (g_once_init_enter ((unsigned long *)&opencv_dilate_erode_type)) {
93     GType _type;
94     static const GTypeInfo opencv_dilate_erode_info = {
95       sizeof (GstCvDilateErodeClass),
96       NULL,
97       NULL,
98       (GClassInitFunc) gst_cv_dilate_erode_class_init,
99       NULL,
100       NULL,
101       sizeof (GstCvDilateErode),
102       0,
103       (GInstanceInitFunc) gst_cv_dilate_erode_init,
104     };
105 
106     _type = g_type_register_static (GST_TYPE_OPENCV_VIDEO_FILTER,
107         "GstCvDilateErode", &opencv_dilate_erode_info, G_TYPE_FLAG_ABSTRACT);
108 /*
109     GST_DEBUG_CATEGORY_INIT (gst_cv_dilate_erode_debug, "cvdilateerode", 0,
110         "cvdilateerode");
111 */
112     g_once_init_leave (&opencv_dilate_erode_type, _type);
113   }
114   return opencv_dilate_erode_type;
115 }
116 
117 /* initialize the cvdilate_erode's class */
118 static void
gst_cv_dilate_erode_class_init(GstCvDilateErodeClass * klass)119 gst_cv_dilate_erode_class_init (GstCvDilateErodeClass * klass)
120 {
121   GObjectClass *gobject_class;
122   GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
123   GstCaps *caps;
124   GstPadTemplate *templ;
125 
126   gobject_class = (GObjectClass *) klass;
127 
128   gobject_class->set_property = gst_cv_dilate_erode_set_property;
129   gobject_class->get_property = gst_cv_dilate_erode_get_property;
130 
131   g_object_class_install_property (gobject_class, PROP_ITERATIONS,
132       g_param_spec_int ("iterations", "iterations",
133           "Number of iterations to run the algorithm", 1, G_MAXINT,
134           DEFAULT_ITERATIONS,
135           (GParamFlags) (G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS)));
136 
137   /* add sink and source pad templates */
138   caps = gst_opencv_caps_from_cv_image_type (CV_16UC1);
139   gst_caps_append (caps, gst_opencv_caps_from_cv_image_type (CV_8UC4));
140   gst_caps_append (caps, gst_opencv_caps_from_cv_image_type (CV_8UC3));
141   gst_caps_append (caps, gst_opencv_caps_from_cv_image_type (CV_8UC1));
142   templ = gst_pad_template_new ("sink", GST_PAD_SINK, GST_PAD_ALWAYS,
143       gst_caps_ref (caps));
144   gst_element_class_add_pad_template (element_class, templ);
145   templ = gst_pad_template_new ("src", GST_PAD_SRC, GST_PAD_ALWAYS, caps);
146   gst_element_class_add_pad_template (element_class, templ);
147   gst_caps_unref (caps);
148 }
149 
150 /* initialize the new element
151  * instantiate pads and add them to element
152  * set pad calback functions
153  * initialize instance structure
154  */
155 static void
gst_cv_dilate_erode_init(GstCvDilateErode * filter,GstCvDilateErodeClass * gclass)156 gst_cv_dilate_erode_init (GstCvDilateErode * filter,
157     GstCvDilateErodeClass * gclass)
158 {
159   filter->iterations = DEFAULT_ITERATIONS;
160   gst_opencv_video_filter_set_in_place (GST_OPENCV_VIDEO_FILTER_CAST (filter),
161       TRUE);
162 }
163 
164 static void
gst_cv_dilate_erode_set_property(GObject * object,guint prop_id,const GValue * value,GParamSpec * pspec)165 gst_cv_dilate_erode_set_property (GObject * object, guint prop_id,
166     const GValue * value, GParamSpec * pspec)
167 {
168   GstCvDilateErode *filter = GST_CV_DILATE_ERODE (object);
169 
170   switch (prop_id) {
171     case PROP_ITERATIONS:
172       filter->iterations = g_value_get_int (value);
173       break;
174     default:
175       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
176       break;
177   }
178 }
179 
180 static void
gst_cv_dilate_erode_get_property(GObject * object,guint prop_id,GValue * value,GParamSpec * pspec)181 gst_cv_dilate_erode_get_property (GObject * object, guint prop_id,
182     GValue * value, GParamSpec * pspec)
183 {
184   GstCvDilateErode *filter = GST_CV_DILATE_ERODE (object);
185 
186   switch (prop_id) {
187     case PROP_ITERATIONS:
188       g_value_set_int (value, filter->iterations);
189       break;
190     default:
191       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
192       break;
193   }
194 }
195