2 * Copyright (C) <1999> Erik Walthinsen <omega@cse.ogi.edu>
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Library General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Library General Public License for more details.
14 * You should have received a copy of the GNU Library General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 * Boston, MA 02111-1307, USA.
24 #include <gstsmooth.h>
26 /* elementfactory information */
27 static GstElementDetails smooth_details = {
31 "Apply a smooth filter to an image",
33 "Wim Taymans <wim.taymans@chello.be>",
38 /* Smooth signals and args */
52 GST_PAD_TEMPLATE_FACTORY (smooth_src_factory,
59 "format", GST_PROPS_FOURCC (GST_STR_FOURCC ("I420"))
63 GST_PAD_TEMPLATE_FACTORY (smooth_sink_factory,
70 "format", GST_PROPS_FOURCC (GST_STR_FOURCC ("I420"))
74 static void gst_smooth_class_init (GstSmoothClass *klass);
75 static void gst_smooth_init (GstSmooth *smooth);
77 static void gst_smooth_chain (GstPad *pad, GstBuffer *buf);
78 static void smooth_filter (unsigned char* dest, unsigned char* src,
79 int width, int height, int tolerance, int filtersize);
81 static void gst_smooth_set_property (GObject *object, guint prop_id, const GValue *value, GParamSpec *pspec);
82 static void gst_smooth_get_property (GObject *object, guint prop_id, GValue *value, GParamSpec *pspec);
84 static GstElementClass *parent_class = NULL;
85 /*static guint gst_smooth_signals[LAST_SIGNAL] = { 0 }; */
88 gst_smooth_get_type (void)
90 static GType smooth_type = 0;
93 static const GTypeInfo smooth_info = {
94 sizeof(GstSmoothClass), NULL,
96 (GClassInitFunc)gst_smooth_class_init,
101 (GInstanceInitFunc)gst_smooth_init,
103 smooth_type = g_type_register_static(GST_TYPE_ELEMENT, "GstSmooth", &smooth_info, 0);
109 gst_smooth_class_init (GstSmoothClass *klass)
111 GObjectClass *gobject_class;
112 GstElementClass *gstelement_class;
114 gobject_class = (GObjectClass*)klass;
115 gstelement_class = (GstElementClass*)klass;
117 parent_class = g_type_class_ref(GST_TYPE_ELEMENT);
119 g_object_class_install_property(G_OBJECT_CLASS(klass), ARG_ACTIVE,
120 g_param_spec_boolean("active","active","active",
121 TRUE,G_PARAM_READWRITE)); /* CHECKME */
122 g_object_class_install_property(G_OBJECT_CLASS(klass), ARG_TOLERANCE,
123 g_param_spec_int("tolerance","tolerance","tolerance",
124 G_MININT,G_MAXINT,0,G_PARAM_READWRITE)); /* CHECKME */
125 g_object_class_install_property(G_OBJECT_CLASS(klass), ARG_FILTERSIZE,
126 g_param_spec_int("filtersize","filtersize","filtersize",
127 G_MININT,G_MAXINT,0,G_PARAM_READWRITE)); /* CHECKME */
129 gobject_class->set_property = gst_smooth_set_property;
130 gobject_class->get_property = gst_smooth_get_property;
134 static GstPadLinkReturn
135 gst_smooth_sinkconnect (GstPad *pad, GstCaps *caps)
139 filter = GST_SMOOTH (gst_pad_get_parent (pad));
141 if (!GST_CAPS_IS_FIXED (caps))
142 return GST_PAD_LINK_DELAYED;
144 gst_caps_get_int (caps, "width", &filter->width);
145 gst_caps_get_int (caps, "height", &filter->height);
147 return GST_PAD_LINK_OK;
151 gst_smooth_init (GstSmooth *smooth)
153 smooth->sinkpad = gst_pad_new_from_template (
154 GST_PAD_TEMPLATE_GET (smooth_sink_factory), "sink");
155 gst_pad_set_link_function (smooth->sinkpad, gst_smooth_sinkconnect);
156 gst_pad_set_chain_function (smooth->sinkpad, gst_smooth_chain);
157 gst_element_add_pad (GST_ELEMENT (smooth), smooth->sinkpad);
159 smooth->srcpad = gst_pad_new_from_template (
160 GST_PAD_TEMPLATE_GET (smooth_src_factory), "src");
161 gst_element_add_pad (GST_ELEMENT (smooth), smooth->srcpad);
163 smooth->active = TRUE;
164 smooth->tolerance = 8;
165 smooth->filtersize = 3;
166 smooth->lum_only = TRUE;
170 smooth_filter (unsigned char* dest, unsigned char* src, int width, int height, int tolerance, int filtersize)
172 int refval, aktval, upperval, lowerval, numvalues, sum;
173 int x, y, fx, fy, fy1, fy2, fx1, fx2;
174 unsigned char *srcp = src;
177 fy2 = MIN(filtersize+1, height) * width;
179 for(y = 0; y < height; y++)
181 if (y>(filtersize+1)) fy1 += width;
182 if (y<height-(filtersize+1)) fy2 += width;
184 for(x = 0; x < width; x++)
187 upperval = refval + tolerance;
188 lowerval = refval - tolerance;
193 fx1 = MAX(x-filtersize, 0) + fy1;
194 fx2 = MIN(x+filtersize+1, width) + fy1;
196 for (fy = fy1; fy<fy2; fy+=width)
198 for (fx = fx1; fx<fx2; fx++)
201 if ((lowerval-aktval)*(upperval-aktval)<0)
212 *dest++ = sum/numvalues;
218 gst_smooth_chain (GstPad *pad, GstBuffer *buf)
224 gint lumsize, chromsize;
226 g_return_if_fail (pad != NULL);
227 g_return_if_fail (GST_IS_PAD (pad));
228 g_return_if_fail (buf != NULL);
230 smooth = GST_SMOOTH (GST_OBJECT_PARENT (pad));
232 if (!smooth->active) {
233 gst_pad_push(smooth->srcpad,buf);
237 data = GST_BUFFER_DATA (buf);
238 size = GST_BUFFER_SIZE (buf);
240 GST_DEBUG ("smooth: have buffer of %d", GST_BUFFER_SIZE (buf));
242 outbuf = gst_buffer_new();
243 GST_BUFFER_DATA (outbuf) = g_malloc (GST_BUFFER_SIZE (buf));
244 GST_BUFFER_SIZE (outbuf) = GST_BUFFER_SIZE (buf);
246 lumsize = smooth->width*smooth->height;
247 chromsize = lumsize/4;
249 smooth_filter (GST_BUFFER_DATA (outbuf), data, smooth->width, smooth->height,
250 smooth->tolerance, smooth->filtersize);
251 if (!smooth->lum_only) {
252 smooth_filter (GST_BUFFER_DATA (outbuf)+lumsize, data+lumsize, smooth->width/2, smooth->height/2,
253 smooth->tolerance, smooth->filtersize/2);
254 smooth_filter (GST_BUFFER_DATA (outbuf)+lumsize+chromsize, data+lumsize+chromsize, smooth->width/2,
255 smooth->height/2, smooth->tolerance, smooth->filtersize/2);
258 memcpy (GST_BUFFER_DATA (outbuf)+lumsize, data+lumsize, chromsize*2);
261 GST_BUFFER_TIMESTAMP (outbuf) = GST_BUFFER_TIMESTAMP (buf);
263 gst_buffer_unref (buf);
265 gst_pad_push (smooth->srcpad, outbuf);
269 gst_smooth_set_property (GObject *object, guint prop_id, const GValue *value, GParamSpec *pspec)
273 /* it's not null if we got it, but it might not be ours */
274 g_return_if_fail(GST_IS_SMOOTH(object));
275 smooth = GST_SMOOTH(object);
279 smooth->active = g_value_get_boolean (value);
282 smooth->tolerance = g_value_get_int (value);
285 smooth->filtersize = g_value_get_int (value);
288 smooth->lum_only = g_value_get_boolean (value);
296 gst_smooth_get_property (GObject *object, guint prop_id, GValue *value, GParamSpec *pspec)
300 /* it's not null if we got it, but it might not be ours */
301 g_return_if_fail(GST_IS_SMOOTH(object));
302 smooth = GST_SMOOTH(object);
306 g_value_set_boolean (value, smooth->active);
309 g_value_set_int (value, smooth->tolerance);
312 g_value_set_int (value, smooth->filtersize);
315 g_value_set_boolean (value, smooth->lum_only);
318 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
325 plugin_init (GModule *module, GstPlugin *plugin)
327 GstElementFactory *factory;
329 factory = gst_element_factory_new("smooth",GST_TYPE_SMOOTH,
331 g_return_val_if_fail(factory != NULL, FALSE);
333 gst_element_factory_add_pad_template (factory, GST_PAD_TEMPLATE_GET (smooth_sink_factory));
334 gst_element_factory_add_pad_template (factory, GST_PAD_TEMPLATE_GET (smooth_src_factory));
336 gst_plugin_add_feature (plugin, GST_PLUGIN_FEATURE (factory));
341 GstPluginDesc plugin_desc = {