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.
21 #include <gstmedian.h>
24 static GstElementDetails median_details = {
27 "Apply a median filter to an image",
29 "Wim Taymans <wim.taymans@chello.be>",
33 GST_PAD_TEMPLATE_FACTORY (median_src_factory,
40 "format", GST_PROPS_FOURCC (GST_STR_FOURCC ("I420"))
44 GST_PAD_TEMPLATE_FACTORY (median_sink_factory,
51 "format", GST_PROPS_FOURCC (GST_STR_FOURCC ("I420"))
56 /* Median signals and args */
69 static GType gst_median_get_type (void);
70 static void gst_median_class_init (GstMedianClass *klass);
71 static void gst_median_init (GstMedian *median);
73 static void median_5 (unsigned char *src, unsigned char *dest, int height, int width);
74 static void median_9 (unsigned char *src, unsigned char *dest, int height, int width);
75 static void gst_median_chain (GstPad *pad, GstBuffer *buf);
77 static void gst_median_set_property (GObject *object, guint prop_id, const GValue *value, GParamSpec *pspec);
78 static void gst_median_get_property (GObject *object, guint prop_id, GValue *value, GParamSpec *pspec);
80 static GstElementClass *parent_class = NULL;
81 /*static guint gst_median_signals[LAST_SIGNAL] = { 0 }; */
84 gst_median_get_type (void)
86 static GType median_type = 0;
89 static const GTypeInfo median_info = {
90 sizeof(GstMedianClass), NULL, NULL, (GClassInitFunc)gst_median_class_init,
95 (GInstanceInitFunc)gst_median_init,
97 median_type = g_type_register_static(GST_TYPE_ELEMENT, "GstMedian", &median_info, 0);
103 gst_median_class_init (GstMedianClass *klass)
105 GObjectClass *gobject_class;
106 GstElementClass *gstelement_class;
108 gobject_class = (GObjectClass*)klass;
109 gstelement_class = (GstElementClass*)klass;
111 parent_class = g_type_class_ref(GST_TYPE_ELEMENT);
113 g_object_class_install_property(G_OBJECT_CLASS(klass), ARG_ACTIVE,
114 g_param_spec_boolean("active","active","active",
115 TRUE,G_PARAM_READWRITE)); /* CHECKME */
116 g_object_class_install_property(G_OBJECT_CLASS(klass), ARG_FILTERSIZE,
117 g_param_spec_int("filtersize","filtersize","filtersize",
118 G_MININT,G_MAXINT,0,G_PARAM_READWRITE)); /* CHECKME */
119 g_object_class_install_property(G_OBJECT_CLASS(klass), ARG_LUM_ONLY,
120 g_param_spec_boolean("lum_only","lum_only","lum_only",
121 TRUE,G_PARAM_READWRITE)); /* CHECKME */
123 gobject_class->set_property = gst_median_set_property;
124 gobject_class->get_property = gst_median_get_property;
128 gst_median_sinkconnect (GstPad *pad, GstCaps *caps)
132 filter = GST_MEDIAN (gst_pad_get_parent (pad));
134 if (!GST_CAPS_IS_FIXED (caps))
135 return GST_PAD_CONNECT_DELAYED;
137 gst_caps_get_int (caps, "width", &filter->width);
138 gst_caps_get_int (caps, "height", &filter->height);
140 /* forward to the next plugin */
141 if (!gst_pad_try_set_caps(filter->srcpad, gst_caps_copy_1(caps)))
142 return GST_PAD_CONNECT_REFUSED;
144 return GST_PAD_CONNECT_OK;
147 void gst_median_init (GstMedian *median)
149 median->sinkpad = gst_pad_new_from_template (
150 GST_PAD_TEMPLATE_GET (median_sink_factory), "sink");
151 gst_pad_set_connect_function (median->sinkpad, gst_median_sinkconnect);
152 gst_pad_set_chain_function (median->sinkpad, gst_median_chain);
153 gst_element_add_pad (GST_ELEMENT (median), median->sinkpad);
155 median->srcpad = gst_pad_new_from_template (
156 GST_PAD_TEMPLATE_GET (median_src_factory), "src");
157 gst_element_add_pad (GST_ELEMENT (median), median->srcpad);
159 median->filtersize = 5;
160 median->lum_only = TRUE;
161 median->active = TRUE;
164 #define PIX_SORT(a,b) { if ((a)>(b)) PIX_SWAP((a),(b)); }
165 #define PIX_SWAP(a,b) { unsigned char temp=(a);(a)=(b);(b)=temp; }
168 median_5 (unsigned char *src, unsigned char *dest, int width, int height)
175 nLastCol = width - 1;
176 nLastRow = height - 1;
178 /*copy the top and bottom rows into the result array */
179 for (i=0; i<width; i++) {
181 dest[nLastRow * width + i] = src[nLastRow * width + i];
188 /* process the interior pixels */
190 for (k=0; k < nLastRow; k++) {
191 for (j=0; j < nLastCol; j++, i++) {
197 PIX_SORT(p[0],p[1]) ; PIX_SORT(p[3],p[4]) ; PIX_SORT(p[0],p[3]) ;
198 PIX_SORT(p[1],p[4]) ; PIX_SORT(p[1],p[2]) ; PIX_SORT(p[2],p[3]) ;
199 PIX_SORT(p[1],p[2]) ;
212 median_9 (unsigned char *src, unsigned char *dest, int width, int height)
219 nLastCol = width - 1;
220 nLastRow = height - 1;
222 /*copy the top and bottom rows into the result array */
223 for (i=0; i<width; i++) {
225 dest[nLastRow * width + i] = src[nLastRow * width + i];
232 /* process the interior pixels */
234 for (k=0; k < nLastRow; k++) {
235 for (j=0; j < nLastCol; j++, i++) {
236 p[0] = src[i-width-1];
238 p[2] = src[i-width+1];
242 p[6] = src[i+width-1];
244 p[8] = src[i+width+1];
245 PIX_SORT(p[1], p[2]) ; PIX_SORT(p[4], p[5]) ; PIX_SORT(p[7], p[8]) ;
246 PIX_SORT(p[0], p[1]) ; PIX_SORT(p[3], p[4]) ; PIX_SORT(p[6], p[7]) ;
247 PIX_SORT(p[1], p[2]) ; PIX_SORT(p[4], p[5]) ; PIX_SORT(p[7], p[8]) ;
248 PIX_SORT(p[0], p[3]) ; PIX_SORT(p[5], p[8]) ; PIX_SORT(p[4], p[7]) ;
249 PIX_SORT(p[3], p[6]) ; PIX_SORT(p[1], p[4]) ; PIX_SORT(p[2], p[5]) ;
250 PIX_SORT(p[4], p[7]) ; PIX_SORT(p[4], p[2]) ; PIX_SORT(p[6], p[4]) ;
251 PIX_SORT(p[4], p[2]) ;
264 gst_median_chain (GstPad *pad, GstBuffer *buf)
271 int lumsize, chromsize;
273 g_return_if_fail(pad != NULL);
274 g_return_if_fail(GST_IS_PAD(pad));
275 g_return_if_fail(buf != NULL);
277 median = GST_MEDIAN (GST_OBJECT_PARENT (pad));
279 if (!median->active) {
280 gst_pad_push(median->srcpad,buf);
284 data = GST_BUFFER_DATA(buf);
285 size = GST_BUFFER_SIZE(buf);
287 GST_DEBUG (0,"median: have buffer of %d", GST_BUFFER_SIZE(buf));
289 outbuf = gst_buffer_new();
290 GST_BUFFER_DATA(outbuf) = g_malloc(GST_BUFFER_SIZE(buf));
291 GST_BUFFER_SIZE(outbuf) = GST_BUFFER_SIZE(buf);
293 lumsize = median->width * median->height;
294 chromsize = lumsize/4;
296 if (median->filtersize == 5) {
297 median_5(data, GST_BUFFER_DATA(outbuf), median->width, median->height);
298 if (!median->lum_only) {
299 median_5(data+lumsize, GST_BUFFER_DATA(outbuf)+lumsize, median->width/2, median->height/2);
300 median_5(data+lumsize+chromsize, GST_BUFFER_DATA(outbuf)+lumsize+chromsize, median->width/2, median->height/2);
303 memcpy (GST_BUFFER_DATA (outbuf)+lumsize, data+lumsize, chromsize*2);
307 median_9(data, GST_BUFFER_DATA(outbuf), median->width, median->height);
308 if (!median->lum_only) {
309 median_9(data+lumsize, GST_BUFFER_DATA(outbuf)+lumsize, median->width/2, median->height/2);
310 median_9(data+lumsize+chromsize, GST_BUFFER_DATA(outbuf)+lumsize+chromsize, median->width/2, median->height/2);
313 memcpy (GST_BUFFER_DATA (outbuf)+lumsize, data+lumsize, chromsize*2);
316 GST_BUFFER_TIMESTAMP (outbuf) = GST_BUFFER_TIMESTAMP (buf);
318 gst_buffer_unref(buf);
320 gst_pad_push(median->srcpad,outbuf);
324 gst_median_set_property (GObject *object, guint prop_id, const GValue *value, GParamSpec *pspec)
329 /* it's not null if we got it, but it might not be ours */
330 g_return_if_fail(GST_IS_MEDIAN(object));
331 median = GST_MEDIAN(object);
335 argvalue = g_value_get_int (value);
336 if (argvalue != 5 && argvalue != 9) {
337 g_warning ("median: invalid filtersize (%d), must be 5 or 9\n", argvalue);
340 median->filtersize = argvalue;
344 median->active = g_value_get_boolean (value);
347 median->lum_only = g_value_get_boolean (value);
355 gst_median_get_property (GObject *object, guint prop_id, GValue *value, GParamSpec *pspec)
359 /* it's not null if we got it, but it might not be ours */
360 g_return_if_fail(GST_IS_MEDIAN(object));
361 median = GST_MEDIAN(object);
365 g_value_set_int (value, median->filtersize);
368 g_value_set_boolean (value, median->active);
371 g_value_set_boolean (value, median->lum_only);
374 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
381 plugin_init (GModule *module, GstPlugin *plugin)
383 GstElementFactory *factory;
385 factory = gst_element_factory_new("median",GST_TYPE_MEDIAN,
387 g_return_val_if_fail(factory != NULL, FALSE);
389 gst_element_factory_add_pad_template (factory, GST_PAD_TEMPLATE_GET (median_sink_factory));
390 gst_element_factory_add_pad_template (factory, GST_PAD_TEMPLATE_GET (median_src_factory));
392 gst_plugin_add_feature (plugin, GST_PLUGIN_FEATURE (factory));
397 GstPluginDesc plugin_desc = {