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 <gstmedian.h>
26 /* elementfactory information */
27 static GstElementDetails median_details = {
31 "Apply a median filter to an image",
33 "Wim Taymans <wim.taymans@chello.be>",
37 GST_PAD_TEMPLATE_FACTORY (median_src_factory,
44 "format", GST_PROPS_FOURCC (GST_STR_FOURCC ("I420"))
48 GST_PAD_TEMPLATE_FACTORY (median_sink_factory,
55 "format", GST_PROPS_FOURCC (GST_STR_FOURCC ("I420"))
60 /* Median signals and args */
73 static GType gst_median_get_type (void);
74 static void gst_median_class_init (GstMedianClass *klass);
75 static void gst_median_init (GstMedian *median);
77 static void median_5 (unsigned char *src, unsigned char *dest, int height, int width);
78 static void median_9 (unsigned char *src, unsigned char *dest, int height, int width);
79 static void gst_median_chain (GstPad *pad, GstBuffer *buf);
81 static void gst_median_set_property (GObject *object, guint prop_id, const GValue *value, GParamSpec *pspec);
82 static void gst_median_get_property (GObject *object, guint prop_id, GValue *value, GParamSpec *pspec);
84 static GstElementClass *parent_class = NULL;
85 /*static guint gst_median_signals[LAST_SIGNAL] = { 0 }; */
88 gst_median_get_type (void)
90 static GType median_type = 0;
93 static const GTypeInfo median_info = {
94 sizeof(GstMedianClass), NULL, NULL, (GClassInitFunc)gst_median_class_init,
99 (GInstanceInitFunc)gst_median_init,
101 median_type = g_type_register_static(GST_TYPE_ELEMENT, "GstMedian", &median_info, 0);
107 gst_median_class_init (GstMedianClass *klass)
109 GObjectClass *gobject_class;
110 GstElementClass *gstelement_class;
112 gobject_class = (GObjectClass*)klass;
113 gstelement_class = (GstElementClass*)klass;
115 parent_class = g_type_class_ref(GST_TYPE_ELEMENT);
117 g_object_class_install_property(G_OBJECT_CLASS(klass), ARG_ACTIVE,
118 g_param_spec_boolean("active","active","active",
119 TRUE,G_PARAM_READWRITE)); /* CHECKME */
120 g_object_class_install_property(G_OBJECT_CLASS(klass), ARG_FILTERSIZE,
121 g_param_spec_int("filtersize","filtersize","filtersize",
122 G_MININT,G_MAXINT,0,G_PARAM_READWRITE)); /* CHECKME */
123 g_object_class_install_property(G_OBJECT_CLASS(klass), ARG_LUM_ONLY,
124 g_param_spec_boolean("lum_only","lum_only","lum_only",
125 TRUE,G_PARAM_READWRITE)); /* CHECKME */
127 gobject_class->set_property = gst_median_set_property;
128 gobject_class->get_property = gst_median_get_property;
132 gst_median_sinkconnect (GstPad *pad, GstCaps *caps)
136 filter = GST_MEDIAN (gst_pad_get_parent (pad));
138 if (!GST_CAPS_IS_FIXED (caps))
139 return GST_PAD_LINK_DELAYED;
141 gst_caps_get_int (caps, "width", &filter->width);
142 gst_caps_get_int (caps, "height", &filter->height);
144 /* forward to the next plugin */
145 return gst_pad_try_set_caps(filter->srcpad, gst_caps_copy_1(caps));
148 void gst_median_init (GstMedian *median)
150 median->sinkpad = gst_pad_new_from_template (
151 GST_PAD_TEMPLATE_GET (median_sink_factory), "sink");
152 gst_pad_set_link_function (median->sinkpad, gst_median_sinkconnect);
153 gst_pad_set_chain_function (median->sinkpad, gst_median_chain);
154 gst_element_add_pad (GST_ELEMENT (median), median->sinkpad);
156 median->srcpad = gst_pad_new_from_template (
157 GST_PAD_TEMPLATE_GET (median_src_factory), "src");
158 gst_element_add_pad (GST_ELEMENT (median), median->srcpad);
160 median->filtersize = 5;
161 median->lum_only = TRUE;
162 median->active = TRUE;
165 #define PIX_SORT(a,b) { if ((a)>(b)) PIX_SWAP((a),(b)); }
166 #define PIX_SWAP(a,b) { unsigned char temp=(a);(a)=(b);(b)=temp; }
169 median_5 (unsigned char *src, unsigned char *dest, int width, int height)
176 nLastCol = width - 1;
177 nLastRow = height - 1;
179 /*copy the top and bottom rows into the result array */
180 for (i=0; i<width; i++) {
182 dest[nLastRow * width + i] = src[nLastRow * width + i];
189 /* process the interior pixels */
191 for (k=0; k < nLastRow; k++) {
192 for (j=0; j < nLastCol; j++, i++) {
198 PIX_SORT(p[0],p[1]) ; PIX_SORT(p[3],p[4]) ; PIX_SORT(p[0],p[3]) ;
199 PIX_SORT(p[1],p[4]) ; PIX_SORT(p[1],p[2]) ; PIX_SORT(p[2],p[3]) ;
200 PIX_SORT(p[1],p[2]) ;
213 median_9 (unsigned char *src, unsigned char *dest, int width, int height)
220 nLastCol = width - 1;
221 nLastRow = height - 1;
223 /*copy the top and bottom rows into the result array */
224 for (i=0; i<width; i++) {
226 dest[nLastRow * width + i] = src[nLastRow * width + i];
233 /* process the interior pixels */
235 for (k=0; k < nLastRow; k++) {
236 for (j=0; j < nLastCol; j++, i++) {
237 p[0] = src[i-width-1];
239 p[2] = src[i-width+1];
243 p[6] = src[i+width-1];
245 p[8] = src[i+width+1];
246 PIX_SORT(p[1], p[2]) ; PIX_SORT(p[4], p[5]) ; PIX_SORT(p[7], p[8]) ;
247 PIX_SORT(p[0], p[1]) ; PIX_SORT(p[3], p[4]) ; PIX_SORT(p[6], p[7]) ;
248 PIX_SORT(p[1], p[2]) ; PIX_SORT(p[4], p[5]) ; PIX_SORT(p[7], p[8]) ;
249 PIX_SORT(p[0], p[3]) ; PIX_SORT(p[5], p[8]) ; PIX_SORT(p[4], p[7]) ;
250 PIX_SORT(p[3], p[6]) ; PIX_SORT(p[1], p[4]) ; PIX_SORT(p[2], p[5]) ;
251 PIX_SORT(p[4], p[7]) ; PIX_SORT(p[4], p[2]) ; PIX_SORT(p[6], p[4]) ;
252 PIX_SORT(p[4], p[2]) ;
265 gst_median_chain (GstPad *pad, GstBuffer *buf)
272 int lumsize, chromsize;
274 g_return_if_fail(pad != NULL);
275 g_return_if_fail(GST_IS_PAD(pad));
276 g_return_if_fail(buf != NULL);
278 median = GST_MEDIAN (GST_OBJECT_PARENT (pad));
280 if (!median->active) {
281 gst_pad_push(median->srcpad,buf);
285 data = GST_BUFFER_DATA(buf);
286 size = GST_BUFFER_SIZE(buf);
288 GST_DEBUG ("median: have buffer of %d", GST_BUFFER_SIZE(buf));
290 outbuf = gst_buffer_new();
291 GST_BUFFER_DATA(outbuf) = g_malloc(GST_BUFFER_SIZE(buf));
292 GST_BUFFER_SIZE(outbuf) = GST_BUFFER_SIZE(buf);
294 lumsize = median->width * median->height;
295 chromsize = lumsize/4;
297 if (median->filtersize == 5) {
298 median_5(data, GST_BUFFER_DATA(outbuf), median->width, median->height);
299 if (!median->lum_only) {
300 median_5(data+lumsize, GST_BUFFER_DATA(outbuf)+lumsize, median->width/2, median->height/2);
301 median_5(data+lumsize+chromsize, GST_BUFFER_DATA(outbuf)+lumsize+chromsize, median->width/2, median->height/2);
304 memcpy (GST_BUFFER_DATA (outbuf)+lumsize, data+lumsize, chromsize*2);
308 median_9(data, GST_BUFFER_DATA(outbuf), median->width, median->height);
309 if (!median->lum_only) {
310 median_9(data+lumsize, GST_BUFFER_DATA(outbuf)+lumsize, median->width/2, median->height/2);
311 median_9(data+lumsize+chromsize, GST_BUFFER_DATA(outbuf)+lumsize+chromsize, median->width/2, median->height/2);
314 memcpy (GST_BUFFER_DATA (outbuf)+lumsize, data+lumsize, chromsize*2);
317 GST_BUFFER_TIMESTAMP (outbuf) = GST_BUFFER_TIMESTAMP (buf);
319 gst_buffer_unref(buf);
321 gst_pad_push(median->srcpad,outbuf);
325 gst_median_set_property (GObject *object, guint prop_id, const GValue *value, GParamSpec *pspec)
330 /* it's not null if we got it, but it might not be ours */
331 g_return_if_fail(GST_IS_MEDIAN(object));
332 median = GST_MEDIAN(object);
336 argvalue = g_value_get_int (value);
337 if (argvalue != 5 && argvalue != 9) {
338 g_warning ("median: invalid filtersize (%d), must be 5 or 9\n", argvalue);
341 median->filtersize = argvalue;
345 median->active = g_value_get_boolean (value);
348 median->lum_only = g_value_get_boolean (value);
356 gst_median_get_property (GObject *object, guint prop_id, GValue *value, GParamSpec *pspec)
360 /* it's not null if we got it, but it might not be ours */
361 g_return_if_fail(GST_IS_MEDIAN(object));
362 median = GST_MEDIAN(object);
366 g_value_set_int (value, median->filtersize);
369 g_value_set_boolean (value, median->active);
372 g_value_set_boolean (value, median->lum_only);
375 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
382 plugin_init (GModule *module, GstPlugin *plugin)
384 GstElementFactory *factory;
386 factory = gst_element_factory_new("median",GST_TYPE_MEDIAN,
388 g_return_val_if_fail(factory != NULL, FALSE);
390 gst_element_factory_add_pad_template (factory, GST_PAD_TEMPLATE_GET (median_sink_factory));
391 gst_element_factory_add_pad_template (factory, GST_PAD_TEMPLATE_GET (median_src_factory));
393 gst_plugin_add_feature (plugin, GST_PLUGIN_FEATURE (factory));
398 GstPluginDesc plugin_desc = {