Permalink
Browse files

gstopencvbasetrans: Adds this new base class

Adds GstOpencvBaseTransform as base class for simple 1:1
opencv filters
  • Loading branch information...
1 parent eecd808 commit 4962b83afaf49a40576d8caefee54eaea92c2310 Thiago Santos committed May 5, 2010
Showing with 404 additions and 6 deletions.
  1. +1 −1 configure.ac
  2. +5 −5 src/Makefile.am
  3. +13 −0 src/basicfilters/Makefile.am
  4. +287 −0 src/gstopencvbasetrans.c
  5. +98 −0 src/gstopencvbasetrans.h
View
2 configure.ac
@@ -161,5 +161,5 @@ dnl set proper LDFLAGS for plugins
GST_PLUGIN_LDFLAGS='-module -avoid-version -export-symbols-regex [_]*\(gst_\|Gst\|GST_\).*'
AC_SUBST(GST_PLUGIN_LDFLAGS)
-AC_OUTPUT(Makefile m4/Makefile src/Makefile src/edgedetect/Makefile src/faceblur/Makefile src/facedetect/Makefile src/pyramidsegment/Makefile src/templatematch/Makefile src/textwrite/Makefile)
+AC_OUTPUT(Makefile m4/Makefile src/Makefile src/basicfilters/Makefile src/edgedetect/Makefile src/faceblur/Makefile src/facedetect/Makefile src/pyramidsegment/Makefile src/templatematch/Makefile src/textwrite/Makefile)
View
10 src/Makefile.am
@@ -5,20 +5,20 @@ SUBDIRS = edgedetect faceblur facedetect pyramidsegment templatematch textwrite
plugin_LTLIBRARIES = libgstopencv.la
# sources used to compile this plug-in
-libgstopencv_la_SOURCES = gstopencv.c
+libgstopencv_la_SOURCES = gstopencv.c gstopencvbasetrans.c
# flags used to compile this facedetect
# add other _CFLAGS and _LIBS as needed
-libgstopencv_la_CFLAGS = $(GST_CFLAGS) $(OPENCV_CFLAGS) \
+libgstopencv_la_CFLAGS = $(GST_CFLAGS) $(GST_BASE_CFLAGS) $(OPENCV_CFLAGS) \
+ -I${top_srcdir}/src \
-I${top_srcdir}/src/edgedetect \
-I${top_srcdir}/src/faceblur \
-I${top_srcdir}/src/facedetect \
-I${top_srcdir}/src/pyramidsegment \
-I${top_srcdir}/src/templatematch \
-I${top_srcdir}/src/textwrite
-libgstopencv_la_LIBADD = $(GST_LIBS) $(OPENCV_LIBS) \
- $(top_builddir)/src/edgedetect/libgstedgedetect.la \
+libgstopencv_la_LIBADD = $(GST_LIBS) $(GST_BASE_LIBS) $(OPENCV_LIBS) \
$(top_builddir)/src/faceblur/libgstfaceblur.la \
$(top_builddir)/src/facedetect/libgstfacedetect.la \
$(top_builddir)/src/pyramidsegment/libgstpyramidsegment.la \
@@ -36,4 +36,4 @@ libgstopencv_la_DEPENDENCIES = \
$(top_builddir)/src/textwrite/libgsttextwrite.la
# headers we need but don't want installed
-noinst_HEADERS =
+noinst_HEADERS = gstopencvbasetrans.h
View
13 src/basicfilters/Makefile.am
@@ -0,0 +1,13 @@
+noinst_LTLIBRARIES = libgstbasicfilters.la
+
+# sources used to compile this plug-in
+libgstbasicfilters_la_SOURCES = gstcvsmooth.c
+
+# flags used to compile this pyramidsegment
+# add other _CFLAGS and _LIBS as needed
+libgstbasicfilters_la_CFLAGS = $(GST_CFLAGS) $(OPENCV_CFLAGS) -I..
+libgstbasicfilters_la_LIBADD = $(GST_LIBS) $(OPENCV_LIBS)
+libgstbasicfilters_la_LDFLAGS = $(GST_PLUGIN_LDFLAGS)
+
+# headers we need but don't want installed
+noinst_HEADERS = gstcvsmooth.h
View
287 src/gstopencvbasetrans.c
@@ -0,0 +1,287 @@
+/*
+ * GStreamer
+ * Copyright (C) 2010 Thiago Santos <thiago.sousa.santos@collabora.co.uk>
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a
+ * copy of this software and associated documentation files (the "Software"),
+ * to deal in the Software without restriction, including without limitation
+ * the rights to use, copy, modify, merge, publish, distribute, sublicense,
+ * and/or sell copies of the Software, and to permit persons to whom the
+ * Software is furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
+ * DEALINGS IN THE SOFTWARE.
+ *
+ * Alternatively, the contents of this file may be used under the
+ * GNU Lesser General Public License Version 2.1 (the "LGPL"), in
+ * which case the following provisions apply instead of the ones
+ * mentioned above:
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Library General Public
+ * License as published by the Free Software Foundation; either
+ * version 2 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Library General Public License for more details.
+ *
+ * You should have received a copy of the GNU Library General Public
+ * License along with this library; if not, write to the
+ * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
+ * Boston, MA 02111-1307, USA.
+ */
+
+/* TODO opencv can do scaling for some cases */
+
+#ifdef HAVE_CONFIG_H
+# include <config.h>
+#endif
+
+#include <gst/gst.h>
+
+#include "gstopencvbasetrans.h"
+
+GST_DEBUG_CATEGORY_STATIC (gst_opencv_base_transform_debug);
+#define GST_CAT_DEFAULT gst_opencv_base_transform_debug
+
+/* Filter signals and args */
+enum
+{
+ /* FILL ME */
+ LAST_SIGNAL
+};
+
+enum
+{
+ PROP_0
+};
+
+static GstElementClass *parent_class = NULL;
+
+static void gst_opencv_base_transform_class_init (GstOpencvBaseTransformClass *
+ klass);
+static void gst_opencv_base_transform_init (GstOpencvBaseTransform * trans,
+ GstOpencvBaseTransformClass * klass);
+static void gst_opencv_base_transform_base_init (gpointer gclass);
+
+static gboolean gst_opencv_base_transform_set_caps (GstBaseTransform * trans,
+ GstCaps * incaps, GstCaps * outcaps);
+static GstFlowReturn gst_opencv_base_transform_transform_ip (GstBaseTransform *
+ trans, GstBuffer * buf);
+static GstFlowReturn gst_opencv_base_transform_transform (GstBaseTransform * trans,
+ GstBuffer * inbuf, GstBuffer * outbuf);
+static gboolean gst_opencv_base_transform_get_unit_size (GstBaseTransform * trans,
+ GstCaps * caps, guint * size);
+
+static void gst_opencv_base_transform_set_property (GObject * object,
+ guint prop_id, const GValue * value, GParamSpec * pspec);
+static void gst_opencv_base_transform_get_property (GObject * object,
+ guint prop_id, GValue * value, GParamSpec * pspec);
+
+GType
+gst_opencv_base_transform_get_type (void)
+{
+ static volatile gsize opencv_base_transform_type = 0;
+
+ if (g_once_init_enter (&opencv_base_transform_type)) {
+ GType _type;
+ static const GTypeInfo opencv_base_transform_info = {
+ sizeof (GstOpencvBaseTransformClass),
+ (GBaseInitFunc) gst_opencv_base_transform_base_init,
+ NULL,
+ (GClassInitFunc) gst_opencv_base_transform_class_init,
+ NULL,
+ NULL,
+ sizeof (GstOpencvBaseTransform),
+ 0,
+ (GInstanceInitFunc) gst_opencv_base_transform_init,
+ };
+
+ _type = g_type_register_static (GST_TYPE_BASE_TRANSFORM,
+ "GstOpencvBaseTransform", &opencv_base_transform_info, G_TYPE_FLAG_ABSTRACT);
+ g_once_init_leave (&opencv_base_transform_type, _type);
+ }
+ return opencv_base_transform_type;
+}
+
+/* Clean up */
+static void
+gst_opencv_base_transform_finalize (GObject * obj)
+{
+ GstOpencvBaseTransform *transform = GST_OPENCV_BASE_TRANSFORM (obj);
+
+ if (transform->cvImage)
+ cvReleaseImage (&transform->cvImage);
+
+ G_OBJECT_CLASS (parent_class)->finalize (obj);
+}
+
+/* GObject vmethod implementations */
+static void
+gst_opencv_base_transform_base_init (gpointer gclass)
+{
+}
+
+static void
+gst_opencv_base_transform_class_init (GstOpencvBaseTransformClass * klass)
+{
+ GObjectClass *gobject_class;
+ GstElementClass *gstelement_class;
+ GstBaseTransformClass *basetrans_class;
+
+ gobject_class = (GObjectClass *) klass;
+ gstelement_class = (GstElementClass *) klass;
+ basetrans_class = (GstBaseTransformClass *) klass;
+ parent_class = g_type_class_peek_parent (klass);
+
+ GST_DEBUG_CATEGORY_INIT (gst_opencv_base_transform_debug, "opencvbasetransform", 0,
+ "opencvbasetransform element");
+
+ gobject_class->finalize = GST_DEBUG_FUNCPTR (gst_opencv_base_transform_finalize);
+ gobject_class->set_property = gst_opencv_base_transform_set_property;
+ gobject_class->get_property = gst_opencv_base_transform_get_property;
+
+ basetrans_class->transform = gst_opencv_base_transform_transform;
+ basetrans_class->transform_ip = gst_opencv_base_transform_transform_ip;
+ basetrans_class->set_caps = gst_opencv_base_transform_set_caps;
+ basetrans_class->get_unit_size = gst_opencv_base_transform_get_unit_size;
+}
+
+static void
+gst_opencv_base_transform_init (GstOpencvBaseTransform * transform,
+ GstOpencvBaseTransformClass * bclass)
+{
+}
+
+static GstFlowReturn
+gst_opencv_base_transform_transform (GstBaseTransform * trans, GstBuffer * inbuf,
+ GstBuffer * outbuf)
+{
+ GstOpencvBaseTransform *transform;
+ GstOpencvBaseTransformClass *fclass;
+
+ transform = GST_OPENCV_BASE_TRANSFORM (trans);
+ fclass = GST_OPENCV_BASE_TRANSFORM_GET_CLASS (transform);
+
+ g_return_val_if_fail (fclass->cv_trans_func != NULL, GST_FLOW_ERROR);
+ g_return_val_if_fail (transform->cvImage != NULL, GST_FLOW_ERROR);
+ g_return_val_if_fail (transform->out_cvImage != NULL, GST_FLOW_ERROR);
+
+ transform->cvImage->imageData = (char *) GST_BUFFER_DATA (inbuf);
+ transform->out_cvImage->imageData = (char *) GST_BUFFER_DATA (outbuf);
+ return fclass->cv_trans_func (transform, inbuf, transform->cvImage, outbuf,
+ transform->out_cvImage);
+}
+
+static GstFlowReturn
+gst_opencv_base_transform_transform_ip (GstBaseTransform * trans,
+ GstBuffer * buffer)
+{
+ GstOpencvBaseTransform *transform;
+ GstOpencvBaseTransformClass *fclass;
+
+ transform = GST_OPENCV_BASE_TRANSFORM (trans);
+ fclass = GST_OPENCV_BASE_TRANSFORM_GET_CLASS (transform);
+
+ g_return_val_if_fail (fclass->cv_trans_ip_func != NULL, GST_FLOW_ERROR);
+ g_return_val_if_fail (transform->cvImage != NULL, GST_FLOW_ERROR);
+
+ buffer = gst_buffer_make_writable (buffer);
+
+ transform->cvImage->imageData = (char *) GST_BUFFER_DATA (buffer);
+
+
+ /* FIXME how to release buffer? */
+ return fclass->cv_trans_ip_func (transform, buffer, transform->cvImage);
+}
+
+static gboolean
+gst_opencv_base_transform_set_caps (GstBaseTransform * trans, GstCaps * incaps,
+ GstCaps * outcaps)
+{
+ GstOpencvBaseTransform *transform = GST_OPENCV_BASE_TRANSFORM (trans);
+ GstStructure *structure;
+ gint width, height;
+
+ structure = gst_caps_get_structure (incaps, 0);
+ if (!gst_structure_get_int (structure, "width", &width) ||
+ !gst_structure_get_int (structure, "height", &height)) {
+ GST_WARNING_OBJECT (transform, "No width/height on caps");
+ return FALSE;
+ }
+
+ if (transform->cvImage) {
+ cvReleaseImage (&transform->cvImage);
+ }
+ if (transform->out_cvImage) {
+ cvReleaseImage (&transform->out_cvImage);
+ }
+
+ /* FIXME - how do we know it is IPL_DEPTH_8U? */
+ transform->cvImage =
+ cvCreateImageHeader (cvSize (width, height), IPL_DEPTH_8U, 3);
+ transform->out_cvImage =
+ cvCreateImageHeader (cvSize (width, height), IPL_DEPTH_8U, 3);
+
+ gst_base_transform_set_in_place (GST_BASE_TRANSFORM (transform),
+ transform->in_place);
+ return TRUE;
+}
+
+static gboolean
+gst_opencv_base_transform_get_unit_size (GstBaseTransform * trans,
+ GstCaps * caps, guint * size)
+{
+ gint width, height;
+ gint bpp;
+ GstStructure *structure;
+
+ structure = gst_caps_get_structure (caps, 0);
+
+ if (!gst_structure_get_int (structure, "width", &width) ||
+ !gst_structure_get_int (structure, "height", &height) ||
+ !gst_structure_get_int (structure, "bpp", &bpp)) {
+ return FALSE;
+ }
+ *size = width * height * bpp / 8;
+ return TRUE;
+}
+
+static void
+gst_opencv_base_transform_set_property (GObject * object, guint prop_id,
+ const GValue * value, GParamSpec * pspec)
+{
+ switch (prop_id) {
+ default:
+ G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
+ break;
+ }
+}
+
+static void
+gst_opencv_base_transform_get_property (GObject * object, guint prop_id,
+ GValue * value, GParamSpec * pspec)
+{
+ switch (prop_id) {
+ default:
+ G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
+ break;
+ }
+}
+
+void
+gst_opencv_base_transform_set_in_place (GstOpencvBaseTransform * transform, gboolean ip)
+{
+ transform->in_place = ip;
+ gst_base_transform_set_in_place (GST_BASE_TRANSFORM (transform), ip);
+}
View
98 src/gstopencvbasetrans.h
@@ -0,0 +1,98 @@
+/*
+ * GStreamer
+ * Copyright (C) 2010 Thiago Santos <thiago.sousa.santos@collabora.co.uk>
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a
+ * copy of this software and associated documentation files (the "Software"),
+ * to deal in the Software without restriction, including without limitation
+ * the rights to use, copy, modify, merge, publish, distribute, sublicense,
+ * and/or sell copies of the Software, and to permit persons to whom the
+ * Software is furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
+ * DEALINGS IN THE SOFTWARE.
+ *
+ * Alternatively, the contents of this file may be used under the
+ * GNU Lesser General Public License Version 2.1 (the "LGPL"), in
+ * which case the following provisions apply instead of the ones
+ * mentioned above:
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Library General Public
+ * License as published by the Free Software Foundation; either
+ * version 2 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Library General Public License for more details.
+ *
+ * You should have received a copy of the GNU Library General Public
+ * License along with this library; if not, write to the
+ * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
+ * Boston, MA 02111-1307, USA.
+ */
+
+#ifndef __GST_OPENCV_BASE_TRANSFORM_H__
+#define __GST_OPENCV_BASE_TRANSFORM_H__
+
+#include <gst/gst.h>
+#include <gst/base/gstbasetransform.h>
+#include <cv.h>
+
+G_BEGIN_DECLS
+/* #defines don't like whitespacey bits */
+#define GST_TYPE_OPENCV_BASE_TRANSFORM \
+ (gst_opencv_base_transform_get_type())
+#define GST_OPENCV_BASE_TRANSFORM(obj) \
+ (G_TYPE_CHECK_INSTANCE_CAST((obj),GST_TYPE_OPENCV_BASE_TRANSFORM,GstOpencvBaseTransform))
+#define GST_OPENCV_BASE_TRANSFORM_CLASS(klass) \
+ (G_TYPE_CHECK_CLASS_CAST((klass),GST_TYPE_OPENCV_BASE_TRANSFORM,GstOpencvBaseTransformClass))
+#define GST_IS_OPENCV_BASE_TRANSFORM(obj) \
+ (G_TYPE_CHECK_INSTANCE_TYPE((obj),GST_TYPE_OPENCV_BASE_TRANSFORM))
+#define GST_IS_OPENCV_BASE_TRANSFORM_CLASS(klass) \
+ (G_TYPE_CHECK_CLASS_TYPE((klass),GST_TYPE_OPENCV_BASE_TRANSFORM))
+#define GST_OPENCV_BASE_TRANSFORM_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS((obj),GST_TYPE_OPENCV_BASE_TRANSFORM,GstOpencvBaseTransformClass))
+
+typedef struct _GstOpencvBaseTransform GstOpencvBaseTransform;
+typedef struct _GstOpencvBaseTransformClass GstOpencvBaseTransformClass;
+
+typedef GstFlowReturn (*GstOpencvBaseTransformTransformIPFunc)
+ (GstOpencvBaseTransform * transform, GstBuffer * buffer, IplImage * img);
+typedef GstFlowReturn (*GstOpencvBaseTransformTransformFunc)
+ (GstOpencvBaseTransform * transform, GstBuffer * buffer, IplImage * img,
+ GstBuffer * outbuf, IplImage * outimg);
+
+struct _GstOpencvBaseTransform
+{
+ GstBaseTransform trans;
+
+ gboolean in_place;
+
+ IplImage *cvImage;
+ IplImage *out_cvImage;
+};
+
+struct _GstOpencvBaseTransformClass
+{
+ GstBaseTransformClass parent_class;
+
+ GstOpencvBaseTransformTransformFunc cv_trans_func;
+ GstOpencvBaseTransformTransformIPFunc cv_trans_ip_func;
+};
+
+GType gst_opencv_base_transform_get_type (void);
+
+void gst_opencv_base_transform_set_in_place (GstOpencvBaseTransform * transform,
+ gboolean ip);
+
+G_END_DECLS
+#endif /* __GST_OPENCV_BASE_TRANSFORM_H__ */

0 comments on commit 4962b83

Please sign in to comment.