Imported Upstream version 0.10.23
[profile/ivi/gst-plugins-bad.git] / ext / opencv / gstopencvutils.c
1 /* GStreamer
2  * Copyright (C) <2010> Thiago Santos <thiago.sousa.santos@collabora.co.uk>
3  *
4  * gstopencvutils.c: miscellaneous utility functions
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Library General Public
8  * License as published by the Free Software Foundation; either
9  * version 2 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Library General Public License for more details.
15  *
16  * You should have received a copy of the GNU Library General Public
17  * License along with this library; if not, write to the
18  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19  * Boston, MA 02111-1307, USA.
20  */
21
22 #include "gstopencvutils.h"
23
24 static gboolean
25 gst_opencv_get_ipl_depth_and_channels (GstStructure * structure,
26     gint * ipldepth, gint * channels, GError ** err)
27 {
28   gint depth, bpp;
29
30   if (!gst_structure_get_int (structure, "depth", &depth) ||
31       !gst_structure_get_int (structure, "bpp", &bpp)) {
32     g_set_error (err, GST_CORE_ERROR, GST_CORE_ERROR_NEGOTIATION,
33         "No depth/bpp in caps");
34     return FALSE;
35   }
36
37   if (depth != bpp) {
38     g_set_error (err, GST_CORE_ERROR, GST_CORE_ERROR_NEGOTIATION,
39         "Depth and bpp should be equal");
40     return FALSE;
41   }
42
43   if (gst_structure_has_name (structure, "video/x-raw-rgb")) {
44     *channels = 3;
45   } else if (gst_structure_has_name (structure, "video/x-raw-gray")) {
46     *channels = 1;
47   } else {
48     g_set_error (err, GST_CORE_ERROR, GST_CORE_ERROR_NEGOTIATION,
49         "Unsupported caps %s", gst_structure_get_name (structure));
50     return FALSE;
51   }
52
53   if (depth / *channels == 8) {
54     /* TODO signdness? */
55     *ipldepth = IPL_DEPTH_8U;
56   } else if (depth / *channels == 16) {
57     *ipldepth = IPL_DEPTH_16U;
58   } else {
59     g_set_error (err, GST_CORE_ERROR, GST_CORE_ERROR_NEGOTIATION,
60         "Unsupported depth/channels %d/%d", depth, *channels);
61     return FALSE;
62   }
63
64   return TRUE;
65 }
66
67 gboolean
68 gst_opencv_parse_iplimage_params_from_structure (GstStructure * structure,
69     gint * width, gint * height, gint * ipldepth, gint * channels,
70     GError ** err)
71 {
72   if (!gst_opencv_get_ipl_depth_and_channels (structure, ipldepth, channels,
73           err)) {
74     return FALSE;
75   }
76
77   if (!gst_structure_get_int (structure, "width", width) ||
78       !gst_structure_get_int (structure, "height", height)) {
79     g_set_error (err, GST_CORE_ERROR, GST_CORE_ERROR_NEGOTIATION,
80         "No width/height in caps");
81     return FALSE;
82   }
83
84   return TRUE;
85 }
86
87 gboolean
88 gst_opencv_parse_iplimage_params_from_caps (GstCaps * caps, gint * width,
89     gint * height, gint * ipldepth, gint * channels, GError ** err)
90 {
91   return
92       gst_opencv_parse_iplimage_params_from_structure (gst_caps_get_structure
93       (caps, 0), width, height, ipldepth, channels, err);
94 }
95
96 GstCaps *
97 gst_opencv_caps_from_cv_image_type (int cv_type)
98 {
99   GstCaps *caps = gst_caps_new_empty ();
100   switch (cv_type) {
101     case CV_8UC1:
102       gst_caps_append (caps, gst_caps_from_string (GST_VIDEO_CAPS_GRAY8));
103       break;
104     case CV_8UC3:
105       gst_caps_append (caps, gst_caps_from_string (GST_VIDEO_CAPS_RGB));
106       gst_caps_append (caps, gst_caps_from_string (GST_VIDEO_CAPS_BGR));
107       break;
108     case CV_8UC4:
109       gst_caps_append (caps, gst_caps_from_string (GST_VIDEO_CAPS_RGBx));
110       gst_caps_append (caps, gst_caps_from_string (GST_VIDEO_CAPS_xRGB));
111       gst_caps_append (caps, gst_caps_from_string (GST_VIDEO_CAPS_BGRx));
112       gst_caps_append (caps, gst_caps_from_string (GST_VIDEO_CAPS_xBGR));
113       gst_caps_append (caps, gst_caps_from_string (GST_VIDEO_CAPS_RGBA));
114       gst_caps_append (caps, gst_caps_from_string (GST_VIDEO_CAPS_ARGB));
115       gst_caps_append (caps, gst_caps_from_string (GST_VIDEO_CAPS_BGRA));
116       gst_caps_append (caps, gst_caps_from_string (GST_VIDEO_CAPS_ABGR));
117       break;
118     case CV_16UC1:
119       gst_caps_append (caps,
120           gst_caps_from_string (GST_VIDEO_CAPS_GRAY16 ("1234")));
121       gst_caps_append (caps,
122           gst_caps_from_string (GST_VIDEO_CAPS_GRAY16 ("4321")));
123       break;
124   }
125   return caps;
126 }