3#include "../bits/H5Inspector_decl.hpp"
4#include "../H5Exception.hpp"
6#include <opencv2/opencv.hpp>
8#include "../bits/convert_size_vector.hpp"
15struct inspector<cv::Mat_<T>> {
16 using type = cv::Mat_<T>;
18 using base_type =
typename inspector<value_type>::base_type;
19 using hdf5_type = base_type;
21 static void assert_row_major(
const type& type) {
25 size_t ld =
sizeof(T);
26 for (
int i = rank - 1; i >= 0; --i) {
27 if (
static_cast<size_t>(type.step[i]) != ld) {
28 throw DataSetException(
"Padded cv::Mat_ are not supported.");
31 ld *=
static_cast<size_t>(type.size[i]);
36 static constexpr size_t min_ndim = 2 + inspector<value_type>::min_ndim;
37 static constexpr size_t max_ndim = 1024 + inspector<value_type>::max_ndim;
42 static constexpr bool is_trivially_copyable = std::is_trivially_copyable<value_type>::value &&
43 inspector<value_type>::is_trivially_nestable;
44 static constexpr bool is_trivially_nestable =
false;
46 static size_t getRank(
const type& val) {
51 return static_cast<size_t>(val.dims) +
52 inspector<value_type>::getRank(getAnyElement(val));
56 static const T& getAnyElement(
const type& val) {
57 return *
reinterpret_cast<T const*
>(val.data);
60 static T& getAnyElement(type& val) {
61 return *
reinterpret_cast<T*
>(val.data);
64 static size_t getLocalRank(
const type& val) {
65 return static_cast<size_t>(val.dims);
68 static std::vector<size_t> getDimensions(
const type& val) {
69 auto local_rank = getLocalRank(val);
70 auto rank = getRank(val);
71 std::vector<size_t> dims(rank, 1ul);
79 for (
size_t i = 0; i < local_rank; ++i) {
80 dims[i] =
static_cast<size_t>(val.size[
static_cast<int>(i)]);
83 auto s = inspector<value_type>::getDimensions(getAnyElement(val));
84 std::copy(s.cbegin(), s.cend(), dims.begin() +
static_cast<int>(local_rank));
88 static void prepare(type& val,
const std::vector<size_t>& dims) {
89 auto subdims = detail::convertSizeVector<int>(dims);
90 val.create(
static_cast<int>(subdims.size()), subdims.data());
93 static hdf5_type* data(type& val) {
94 assert_row_major(val);
96 if (!is_trivially_copyable) {
97 throw DataSetException(
"Invalid used of `inspector<Eigen::Matrix<...>>::data`.");
104 return inspector<value_type>::data(getAnyElement(val));
107 static const hdf5_type* data(
const type& val) {
108 assert_row_major(val);
110 if (!is_trivially_copyable) {
111 throw DataSetException(
"Invalid used of `inspector<Eigen::Matrix<...>>::data`.");
118 return inspector<value_type>::data(getAnyElement(val));
121 static void serialize(
const type& val,
const std::vector<size_t>& dims, hdf5_type* m) {
126 auto local_rank = val.dims;
127 auto subdims = std::vector<size_t>(dims.begin() + local_rank, dims.end());
129 for (
auto it = val.begin(); it != val.end(); ++it) {
130 inspector<value_type>::serialize(*it, subdims, m);
135 static void unserialize(
const hdf5_type* vec_align,
136 const std::vector<size_t>& dims,
138 auto local_rank = val.dims;
139 auto subdims = std::vector<size_t>(dims.begin() + local_rank, dims.end());
141 for (
auto it = val.begin(); it != val.end(); ++it) {
142 inspector<value_type>::unserialize(vec_align, subdims, *it);
143 vec_align += subsize;
Definition assert_compatible_spaces.hpp:15
size_t compute_total_size(const std::vector< size_t > &dims)
Definition compute_total_size.hpp:10