~michael-sheldon/gst-opencv/trunk

« back to all changes in this revision

Viewing changes to src/basicfilters/gstcvsmooth.c

  • Committer: Thiago Santos
  • Date: 2010-06-02 02:13:50 UTC
  • Revision ID: git-v1:ee213b627eb05ccaef73a924d8c90de2fff78dd3
cvsmooth: Adds new element cvsmooth

Adds new cvsmooth element

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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., 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
 
 
50
#include "gstcvsmooth.h"
 
51
 
 
52
GST_DEBUG_CATEGORY_STATIC (gst_cv_smooth_debug);
 
53
#define GST_CAT_DEFAULT gst_cv_smooth_debug
 
54
 
 
55
static GstStaticPadTemplate sink_factory = GST_STATIC_PAD_TEMPLATE ("sink",
 
56
    GST_PAD_SINK,
 
57
    GST_PAD_ALWAYS,
 
58
    GST_STATIC_CAPS ("video/x-raw-rgb, depth=(int)24, bpp=(int)24")
 
59
    );
 
60
 
 
61
static GstStaticPadTemplate src_factory = GST_STATIC_PAD_TEMPLATE ("src",
 
62
    GST_PAD_SRC,
 
63
    GST_PAD_ALWAYS,
 
64
    GST_STATIC_CAPS ("video/x-raw-rgb, depth=(int)24, bpp=(int)24")
 
65
    );
 
66
 
 
67
/* Filter signals and args */
 
68
enum
 
69
{
 
70
  /* FILL ME */
 
71
  LAST_SIGNAL
 
72
};
 
73
enum
 
74
{
 
75
  PROP_0,
 
76
  PROP_SMOOTH_TYPE,
 
77
  PROP_PARAM1,
 
78
  PROP_PARAM2,
 
79
  PROP_PARAM3,
 
80
  PROP_PARAM4
 
81
};
 
82
 
 
83
#define GST_TYPE_CV_SMOOTH_TYPE (gst_cv_smooth_type_get_type ())
 
84
static GType
 
85
gst_cv_smooth_type_get_type (void)
 
86
{
 
87
  static GType cv_smooth_type_type = 0;
 
88
 
 
89
  static const GEnumValue smooth_types[] = {
 
90
    {CV_BLUR_NO_SCALE, "CV Blur No Scale", "blur-no-scale"},
 
91
    {CV_BLUR, "CV Blur", "blur"},
 
92
    {CV_GAUSSIAN, "CV Gaussian", "gaussian"},
 
93
    {CV_MEDIAN, "CV Median", "median"},
 
94
    {CV_BILATERAL, "CV Bilateral", "bilateral"},
 
95
    {0, NULL, NULL},
 
96
  };
 
97
 
 
98
  if (!cv_smooth_type_type) {
 
99
    cv_smooth_type_type =
 
100
        g_enum_register_static ("GstCvSmoothTypeType", smooth_types);
 
101
  }
 
102
  return cv_smooth_type_type;
 
103
}
 
104
 
 
105
#define DEFAULT_CV_SMOOTH_TYPE CV_GAUSSIAN
 
106
#define DEFAULT_PARAM1 3
 
107
#define DEFAULT_PARAM2 0.0
 
108
#define DEFAULT_PARAM3 0.0
 
109
#define DEFAULT_PARAM4 0.0
 
110
 
 
111
GST_BOILERPLATE (GstCvSmooth, gst_cv_smooth, GstOpencvBaseTransform,
 
112
    GST_TYPE_OPENCV_BASE_TRANSFORM);
 
113
 
 
114
static void gst_cv_smooth_set_property (GObject * object, guint prop_id,
 
115
    const GValue * value, GParamSpec * pspec);
 
116
static void gst_cv_smooth_get_property (GObject * object, guint prop_id,
 
117
    GValue * value, GParamSpec * pspec);
 
118
 
 
119
static GstFlowReturn gst_cv_smooth_transform_ip (GstOpencvBaseTransform * filter,
 
120
    GstBuffer * buf, IplImage * img);
 
121
static GstFlowReturn gst_cv_smooth_transform (GstOpencvBaseTransform * filter,
 
122
    GstBuffer * buf, IplImage * img, GstBuffer * outbuf, IplImage * outimg);
 
123
 
 
124
/* Clean up */
 
125
static void
 
126
gst_cv_smooth_finalize (GObject * obj)
 
127
{
 
128
  G_OBJECT_CLASS (parent_class)->finalize (obj);
 
129
}
 
130
 
 
131
 
 
132
/* GObject vmethod implementations */
 
133
 
 
134
static void
 
135
gst_cv_smooth_base_init (gpointer gclass)
 
136
{
 
137
  GstElementClass *element_class = GST_ELEMENT_CLASS (gclass);
 
138
 
 
139
  gst_element_class_add_pad_template (element_class,
 
140
      gst_static_pad_template_get (&src_factory));
 
141
  gst_element_class_add_pad_template (element_class,
 
142
      gst_static_pad_template_get (&sink_factory));
 
143
 
 
144
  gst_element_class_set_details_simple (element_class,
 
145
      "cvsmooth",
 
146
      "Transform/Effect/Video",
 
147
      "Applies cvSmooth OpenCV function to the image",
 
148
      "Thiago Santos<thiago.sousa.santos@collabora.co.uk>");
 
149
}
 
150
 
 
151
/* initialize the cvsmooth's class */
 
152
static void
 
153
gst_cv_smooth_class_init (GstCvSmoothClass * klass)
 
154
{
 
155
  GObjectClass *gobject_class;
 
156
  GstOpencvBaseTransformClass *gstopencvbasefilter_class;
 
157
  GstElementClass *gstelement_class;
 
158
 
 
159
  gobject_class = (GObjectClass *) klass;
 
160
  gstelement_class = (GstElementClass *) klass;
 
161
  gstopencvbasefilter_class = (GstOpencvBaseTransformClass *) klass;
 
162
 
 
163
  parent_class = g_type_class_peek_parent (klass);
 
164
 
 
165
  gobject_class->finalize = GST_DEBUG_FUNCPTR (gst_cv_smooth_finalize);
 
166
  gobject_class->set_property = gst_cv_smooth_set_property;
 
167
  gobject_class->get_property = gst_cv_smooth_get_property;
 
168
 
 
169
  gstopencvbasefilter_class->cv_trans_ip_func = gst_cv_smooth_transform_ip;
 
170
  gstopencvbasefilter_class->cv_trans_func = gst_cv_smooth_transform;
 
171
 
 
172
  g_object_class_install_property (gobject_class, PROP_SMOOTH_TYPE,
 
173
      g_param_spec_enum ("type",
 
174
          "type",
 
175
          "Smooth Type",
 
176
          GST_TYPE_CV_SMOOTH_TYPE,
 
177
          DEFAULT_CV_SMOOTH_TYPE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS)
 
178
      );
 
179
  g_object_class_install_property (gobject_class, PROP_PARAM1,
 
180
      g_param_spec_int ("param1", "param1",
 
181
          "Param1 (Check cvSmooth OpenCV docs)", 1, G_MAXINT, DEFAULT_PARAM1,
 
182
          G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
 
183
  g_object_class_install_property (gobject_class, PROP_PARAM2,
 
184
      g_param_spec_int ("param2", "param2",
 
185
          "Param2 (Check cvSmooth OpenCV docs)", 0, G_MAXINT, DEFAULT_PARAM2,
 
186
          G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
 
187
  g_object_class_install_property (gobject_class, PROP_PARAM3,
 
188
      g_param_spec_double ("param3", "param3",
 
189
          "Param3 (Check cvSmooth OpenCV docs)", 0, G_MAXDOUBLE, DEFAULT_PARAM3,
 
190
          G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
 
191
  g_object_class_install_property (gobject_class, PROP_PARAM4,
 
192
      g_param_spec_double ("param4", "param4",
 
193
          "Param4 (Check cvSmooth OpenCV docs)", 0, G_MAXDOUBLE, DEFAULT_PARAM4,
 
194
          G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
 
195
}
 
196
 
 
197
/* initialize the new element
 
198
 * instantiate pads and add them to element
 
199
 * set pad calback functions
 
200
 * initialize instance structure
 
201
 */
 
202
static void
 
203
gst_cv_smooth_init (GstCvSmooth * filter, GstCvSmoothClass * gclass)
 
204
{
 
205
  filter->type = DEFAULT_CV_SMOOTH_TYPE;
 
206
  filter->param1 = DEFAULT_PARAM1;
 
207
  filter->param2 = DEFAULT_PARAM2;
 
208
  filter->param3 = DEFAULT_PARAM3;
 
209
  filter->param4 = DEFAULT_PARAM4;
 
210
 
 
211
  gst_base_transform_set_in_place (GST_BASE_TRANSFORM (filter), FALSE);
 
212
}
 
213
 
 
214
static void
 
215
gst_cv_smooth_change_type (GstCvSmooth * filter, gint value)
 
216
{
 
217
  GST_DEBUG_OBJECT (filter, "Changing type from %d to %d", filter->type, value);
 
218
  if (filter->type == value)
 
219
    return;
 
220
 
 
221
  filter->type = value;
 
222
  switch (value) {
 
223
    case CV_GAUSSIAN:
 
224
    case CV_BLUR:
 
225
      gst_base_transform_set_in_place (GST_BASE_TRANSFORM (filter), TRUE);
 
226
      break;
 
227
    default:
 
228
      gst_base_transform_set_in_place (GST_BASE_TRANSFORM (filter), FALSE);
 
229
      break;
 
230
  }
 
231
}
 
232
 
 
233
static void
 
234
gst_cv_smooth_set_property (GObject * object, guint prop_id,
 
235
    const GValue * value, GParamSpec * pspec)
 
236
{
 
237
  GstCvSmooth *filter = GST_CV_SMOOTH (object);
 
238
 
 
239
  switch (prop_id) {
 
240
    case PROP_SMOOTH_TYPE:
 
241
      gst_cv_smooth_change_type (filter, g_value_get_enum (value));
 
242
      break;
 
243
    case PROP_PARAM1:{
 
244
      gint prop = g_value_get_int (value);
 
245
 
 
246
      if (prop % 2 == 1) {
 
247
        filter->param1 = prop;
 
248
      } else {
 
249
        GST_WARNING_OBJECT (filter, "Ignoring value for param1, not odd"
 
250
            "(%d)", prop);
 
251
      }
 
252
    }
 
253
      break;
 
254
    case PROP_PARAM2:{
 
255
      gint prop = g_value_get_int (value);
 
256
 
 
257
      if (prop % 2 == 1 || prop == 0) {
 
258
        filter->param1 = prop;
 
259
      } else {
 
260
        GST_WARNING_OBJECT (filter, "Ignoring value for param2, not odd"
 
261
            " nor zero (%d)", prop);
 
262
      }
 
263
    }
 
264
      break;
 
265
    case PROP_PARAM3:
 
266
      filter->param3 = g_value_get_double (value);
 
267
      break;
 
268
    case PROP_PARAM4:
 
269
      filter->param4 = g_value_get_double (value);
 
270
      break;
 
271
    default:
 
272
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
 
273
      break;
 
274
  }
 
275
}
 
276
 
 
277
static void
 
278
gst_cv_smooth_get_property (GObject * object, guint prop_id,
 
279
    GValue * value, GParamSpec * pspec)
 
280
{
 
281
  GstCvSmooth *filter = GST_CV_SMOOTH (object);
 
282
 
 
283
  switch (prop_id) {
 
284
    case PROP_SMOOTH_TYPE:
 
285
      g_value_set_enum (value, filter->type);
 
286
      break;
 
287
    case PROP_PARAM1:
 
288
      g_value_set_int (value, filter->param1);
 
289
      break;
 
290
    case PROP_PARAM2:
 
291
      g_value_set_int (value, filter->param2);
 
292
      break;
 
293
    case PROP_PARAM3:
 
294
      g_value_set_double (value, filter->param3);
 
295
      break;
 
296
    case PROP_PARAM4:
 
297
      g_value_set_double (value, filter->param4);
 
298
      break;
 
299
    default:
 
300
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
 
301
      break;
 
302
  }
 
303
}
 
304
 
 
305
static GstFlowReturn
 
306
gst_cv_smooth_transform (GstOpencvBaseTransform * base, GstBuffer * buf,
 
307
    IplImage * img, GstBuffer * outbuf, IplImage * outimg)
 
308
{
 
309
  GstCvSmooth *filter = GST_CV_SMOOTH (base);
 
310
 
 
311
  cvSmooth (img, outimg, filter->type, filter->param1, filter->param2,
 
312
      filter->param3, filter->param4);
 
313
 
 
314
  return GST_FLOW_OK;
 
315
}
 
316
 
 
317
static GstFlowReturn
 
318
gst_cv_smooth_transform_ip (GstOpencvBaseTransform * base, GstBuffer * buf,
 
319
    IplImage * img)
 
320
{
 
321
  GstCvSmooth *filter = GST_CV_SMOOTH (base);
 
322
 
 
323
  cvSmooth (img, img, filter->type, filter->param1, filter->param2,
 
324
      filter->param3, filter->param4);
 
325
 
 
326
  return GST_FLOW_OK;
 
327
}
 
328
 
 
329
gboolean
 
330
gst_cv_smooth_plugin_init (GstPlugin * plugin)
 
331
{
 
332
  GST_DEBUG_CATEGORY_INIT (gst_cv_smooth_debug, "cvsmooth", 0, "cvsmooth");
 
333
 
 
334
  return gst_element_register (plugin, "cvsmooth", GST_RANK_NONE,
 
335
      GST_TYPE_CV_SMOOTH);
 
336
}