From 824d69d5ad917e7814530642db706e0dc8e7c186 Mon Sep 17 00:00:00 2001 From: Thiago Santos Date: Sun, 16 May 2010 00:46:01 -0300 Subject: [PATCH] Adding gstopencvutils Adds a file to keep utilitary functions together --- ext/opencv/Makefile.am | 4 +- ext/opencv/gstopencvbasetrans.c | 32 +++++++++---- ext/opencv/gstopencvutils.c | 82 +++++++++++++++++++++++++++++++++ ext/opencv/gstopencvutils.h | 39 ++++++++++++++++ 4 files changed, 146 insertions(+), 11 deletions(-) create mode 100644 ext/opencv/gstopencvutils.c create mode 100644 ext/opencv/gstopencvutils.h diff --git a/ext/opencv/Makefile.am b/ext/opencv/Makefile.am index 8b8c26fe02..7b5fa9cd25 100644 --- a/ext/opencv/Makefile.am +++ b/ext/opencv/Makefile.am @@ -5,7 +5,7 @@ SUBDIRS = basicfilters edgedetect faceblur facedetect pyramidsegment templatemat plugin_LTLIBRARIES = libgstopencv.la # sources used to compile this plug-in -libgstopencv_la_SOURCES = gstopencv.c gstopencvbasetrans.c +libgstopencv_la_SOURCES = gstopencv.c gstopencvbasetrans.c gstopencvutils.c # flags used to compile this facedetect # add other _CFLAGS and _LIBS as needed @@ -40,4 +40,4 @@ libgstopencv_la_DEPENDENCIES = \ $(top_builddir)/ext/opencv/textwrite/libgsttextwrite.la # headers we need but don't want installed -noinst_HEADERS = gstopencvbasetrans.h +noinst_HEADERS = gstopencvbasetrans.h gstopencvutils.h diff --git a/ext/opencv/gstopencvbasetrans.c b/ext/opencv/gstopencvbasetrans.c index 2c85d2b799..4dfd586813 100644 --- a/ext/opencv/gstopencvbasetrans.c +++ b/ext/opencv/gstopencvbasetrans.c @@ -50,6 +50,7 @@ #include #include "gstopencvbasetrans.h" +#include "gstopencvutils.h" GST_DEBUG_CATEGORY_STATIC (gst_opencv_base_transform_debug); #define GST_CAT_DEFAULT gst_opencv_base_transform_debug @@ -210,13 +211,26 @@ gst_opencv_base_transform_set_caps (GstBaseTransform * trans, GstCaps * incaps, GstCaps * outcaps) { GstOpencvBaseTransform *transform = GST_OPENCV_BASE_TRANSFORM (trans); - GstStructure *structure; - gint width, height; + gint in_width, in_height; + gint in_depth, in_type, in_channels; + gint out_width, out_height; + gint out_depth, out_type, out_channels; + GError *in_err = NULL; + GError *out_err = NULL; - 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"); + if (!gst_opencv_parse_iplimage_params_from_caps (incaps, &in_width, + &in_height, &in_depth, &in_type, &in_channels, &in_err)) { + GST_WARNING_OBJECT (transform, "Failed to parse input caps: %s", + in_err->message); + g_error_free (in_err); + return FALSE; + } + + if (!gst_opencv_parse_iplimage_params_from_caps (outcaps, &out_width, + &out_height, &out_depth, &out_type, &out_channels, &out_err)) { + GST_WARNING_OBJECT (transform, "Failed to parse output caps: %s", + out_err->message); + g_error_free (out_err); return FALSE; } @@ -227,11 +241,11 @@ gst_opencv_base_transform_set_caps (GstBaseTransform * trans, GstCaps * incaps, cvReleaseImage (&transform->out_cvImage); } - /* FIXME - how do we know it is IPL_DEPTH_8U? */ transform->cvImage = - cvCreateImageHeader (cvSize (width, height), IPL_DEPTH_8U, 3); + cvCreateImageHeader (cvSize (in_width, in_height), in_depth, in_channels); transform->out_cvImage = - cvCreateImageHeader (cvSize (width, height), IPL_DEPTH_8U, 3); + cvCreateImageHeader (cvSize (out_width, out_height), out_depth, + out_channels); gst_base_transform_set_in_place (GST_BASE_TRANSFORM (transform), transform->in_place); diff --git a/ext/opencv/gstopencvutils.c b/ext/opencv/gstopencvutils.c new file mode 100644 index 0000000000..653e3c7e33 --- /dev/null +++ b/ext/opencv/gstopencvutils.c @@ -0,0 +1,82 @@ +/* GStreamer + * Copyright (C) <2010> Thiago Santos + * + * gstopencvutils.c: miscellaneous utility functions + * + * 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. + */ + +#include "gstopencvutils.h" + +gboolean +gst_opencv_get_ipl_depth_and_channels (GstStructure * structure, + gint * ipldepth, gint * channels, GError ** err) +{ + gint depth, bpp; + + if (gst_structure_has_name (structure, "video/x-raw-rgb")) { + *channels = 3; + + if (!gst_structure_get_int (structure, "depth", &depth) || + !gst_structure_get_int (structure, "bpp", &bpp)) { + g_set_error (err, GST_CORE_ERROR, GST_CORE_ERROR_NEGOTIATION, + "No depth/bpp in caps"); + return FALSE; + } + + if (depth != bpp) { + g_set_error (err, GST_CORE_ERROR, GST_CORE_ERROR_NEGOTIATION, + "Depth and bpp should be equal"); + return FALSE; + } + + if (depth == 24) { + /* TODO signdness? */ + *ipldepth = IPL_DEPTH_8U; + } else { + g_set_error (err, GST_CORE_ERROR, GST_CORE_ERROR_NEGOTIATION, + "Unsupported depth: %d", depth); + return FALSE; + } + + return TRUE; + } else { + g_set_error (err, GST_CORE_ERROR, GST_CORE_ERROR_NEGOTIATION, + "Unsupported caps %s", gst_structure_get_name (structure)); + return FALSE; + } +} + +gboolean +gst_opencv_parse_iplimage_params_from_caps (GstCaps * caps, gint * width, + gint * height, gint * ipldepth, gint * type, gint * channels, GError ** err) +{ + GstStructure *structure = gst_caps_get_structure (caps, 0); + + if (!gst_opencv_get_ipl_depth_and_channels (structure, ipldepth, channels, + err)) { + return FALSE; + } + + if (!gst_structure_get_int (structure, "width", width) || + !gst_structure_get_int (structure, "height", height)) { + g_set_error (err, GST_CORE_ERROR, GST_CORE_ERROR_NEGOTIATION, + "No width/height in caps"); + return FALSE; + } + + return TRUE; +} diff --git a/ext/opencv/gstopencvutils.h b/ext/opencv/gstopencvutils.h new file mode 100644 index 0000000000..3438600aaf --- /dev/null +++ b/ext/opencv/gstopencvutils.h @@ -0,0 +1,39 @@ +/* GStreamer + * Copyright (C) <2010> Thiago Santos + * + * gstopencvutils.h: miscellaneous utility functions + * + * 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_UTILS__ +#define __GST_OPENCV_UTILS__ + +#ifdef HAVE_CONFIG_H +#include "config.h" +#endif + +#include +#include + +gboolean +gst_opencv_get_ipldepth (gint depth, gint bpp, gint * ipldepth); + +gboolean gst_opencv_parse_iplimage_params_from_caps + (GstCaps * caps, gint * width, gint * height, gint * depth, gint * type, + gint * channels, GError ** err); + +#endif /* __GST_OPENCV_UTILS__ */