OSVR-Core
OpenCVTypeDispatch.h
Go to the documentation of this file.
1 
11 // Copyright 2015 Sensics, Inc.
12 //
13 // Licensed under the Apache License, Version 2.0 (the "License");
14 // you may not use this file except in compliance with the License.
15 // You may obtain a copy of the License at
16 //
17 // http://www.apache.org/licenses/LICENSE-2.0
18 //
19 // Unless required by applicable law or agreed to in writing, software
20 // distributed under the License is distributed on an "AS IS" BASIS,
21 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
22 // See the License for the specific language governing permissions and
23 // limitations under the License.
24 
25 #ifndef INCLUDED_OpenCVTypeDispatch_h_GUID_BA2EFF7D_152D_4FA6_6454_B8F3AC30FB3F
26 #define INCLUDED_OpenCVTypeDispatch_h_GUID_BA2EFF7D_152D_4FA6_6454_B8F3AC30FB3F
27 
28 // Internal Includes
29 #include <osvr/Util/StdInt.h>
32 
33 // Library/third-party includes
34 #include <opencv2/core/version.hpp>
35 #if CV_MAJOR_VERSION == 2 || CV_VERSION_EPOCH == 2
36 #include <opencv2/core/core_c.h>
37 #else
38 #include <opencv2/core/core.hpp>
39 #endif
40 #include <boost/mpl/identity.hpp>
41 
42 // Standard includes
43 #include <stdexcept>
44 
45 namespace osvr {
46 namespace util {
53  template <typename Functor>
54  inline bool opencvTypeDispatch(int openCVType, Functor &f) {
55  using boost::mpl::identity;
56  switch (CV_MAT_DEPTH(openCVType)) {
57  case CV_8U:
58  f(identity<uint8_t>());
59  break;
60  case CV_8S:
61  f(identity<int8_t>());
62  break;
63  case CV_16U:
64  f(identity<uint16_t>());
65  break;
66  case CV_16S:
67  f(identity<int16_t>());
68  break;
69  case CV_32S:
70  f(identity<int32_t>());
71  break;
72  case CV_32F:
73  f(identity<float>());
74  break;
75  case CV_64F:
76  f(identity<double>());
77  break;
78  default:
79  return false;
80  };
81  return true;
82  }
83 
84  inline NumberTypeData opencvNumberTypeData(int openCVType) {
85  NumberTypeData ret;
87  opencvTypeDispatch(openCVType, f);
88  return ret;
89  }
90 
93  inline int cvTypeFromData(bool isSigned, bool isFloat, size_t depth) {
94  switch (depth) {
95  case 1:
96  return isSigned ? CV_8S : CV_8U;
97  case 2:
98  return isSigned ? CV_16S : CV_16U;
99 
100  case 4:
101  if (isFloat) {
102  return CV_32F;
103  }
104  if (isSigned) {
105  return CV_32S;
106  }
107  throw std::runtime_error("No OpenCV 32-bit unsigned type!");
108  case 8:
109  if (isFloat) {
110  return CV_64F;
111  }
112  throw std::runtime_error("No OpenCV 64-bit integer types!");
113  default:
114 
115  throw std::runtime_error(
116  "No OpenCV type matching the requested parameters!");
117  }
118  }
119 
122  inline int computeOpenCVMatType(OSVR_ImagingMetadata const &metadata) {
123  return CV_MAKETYPE(
124  cvTypeFromData(metadata.type == OSVR_IVT_SIGNED_INT,
125  metadata.type == OSVR_IVT_FLOATING_POINT,
126  metadata.depth),
127  metadata.channels);
128  }
129 } // namespace util
130 } // namespace osvr
131 
132 #endif // INCLUDED_OpenCVTypeDispatch_h_GUID_BA2EFF7D_152D_4FA6_6454_B8F3AC30FB3F
Definition: ImagingReportTypesC.h:61
Definition: NonLinearOptimization.cpp:107
Definition: RunLoopManager.h:42
OSVR_ImageDepth depth
the depth (size) in bytes of each channel - valid values are 1, 2, 4, and 8
Definition: ImagingReportTypesC.h:70
OSVR_ImagingValueType type
Whether values are unsigned ints, signed ints, or floating point.
Definition: ImagingReportTypesC.h:73
Runtime data on numeric types.
Definition: NumberTypeManipulation.h:47
int cvTypeFromData(bool isSigned, bool isFloat, size_t depth)
Computes the OpenCV "type" (also known as depth - as in CV_8U) for a given combination of signed...
Definition: OpenCVTypeDispatch.h:93
Header wrapping the C99 standard stdint header.
The main namespace for all C++ elements of the framework, internal and external.
Definition: namespace_osvr.dox:3
bool opencvTypeDispatch(int openCVType, Functor &f)
Given an OpenCV type depth and a reference to a functor, calls that functor with a single parameter: ...
Definition: OpenCVTypeDispatch.h:54
int computeOpenCVMatType(OSVR_ImagingMetadata const &metadata)
Computes the OpenCV matrix type (as in CV_8UC3) from a metadata struct.
Definition: OpenCVTypeDispatch.h:122
OSVR_ImageChannels channels
number of channels of data for each pixel
Definition: ImagingReportTypesC.h:67
Definition: NumberTypeManipulation.h:152