hops
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
H5Easy_opencv.hpp
Go to the documentation of this file.
1 /*
2  * Copyright (c), 2017, Adrien Devresse <adrien.devresse@epfl.ch>
3  *
4  * Distributed under the Boost Software License, Version 1.0.
5  * (See accompanying file LICENSE_1_0.txt or copy at
6  * http://www.boost.org/LICENSE_1_0.txt)
7  *
8  */
9 #ifndef H5EASY_BITS_OPENCV_HPP
10 #define H5EASY_BITS_OPENCV_HPP
11 
12 #include "../H5Easy.hpp"
13 #include "H5Easy_misc.hpp"
14 #include "H5Easy_scalar.hpp"
15 
16 #ifdef H5_USE_OPENCV
17 
18 namespace H5Easy {
19 
20 namespace detail {
21 
22 template <class T>
23 struct is_opencv : std::false_type {};
24 template <class T>
25 struct is_opencv<cv::Mat_<T>> : std::true_type {};
26 
27 template <typename T>
28 struct io_impl<T, typename std::enable_if<is_opencv<T>::value>::type> {
29 
30  inline static std::vector<size_t> shape(const T& data)
31  {
32  return std::vector<size_t>{static_cast<size_t>(data.rows),
33  static_cast<size_t>(data.cols)};
34  }
35 
36  inline static std::vector<int> shape(const File& file,
37  const std::string& path,
38  std::vector<size_t> dims)
39  {
40  if (dims.size() == 1) {
41  return std::vector<int>{static_cast<int>(dims[0]), 1ul};
42  }
43  if (dims.size() == 2) {
44  return std::vector<int>{static_cast<int>(dims[0]),
45  static_cast<int>(dims[1])};
46  }
47 
48  throw detail::error(file, path, "H5Easy::load: Inconsistent rank");
49  }
50 
51  inline static DataSet dump(File& file,
52  const std::string& path,
53  const T& data,
54  const DumpOptions& options) {
55  using value_type = typename T::value_type;
56  DataSet dataset = initDataset<value_type>(file, path, shape(data), options);
57  std::vector<value_type> v(data.begin(), data.end());
58  dataset.write_raw(v.data());
59  if (options.flush()) {
60  file.flush();
61  }
62  return dataset;
63  }
64 
65  inline static T load(const File& file, const std::string& path) {
66  using value_type = typename T::value_type;
67  DataSet dataset = file.getDataSet(path);
68  std::vector<int> dims = shape(file, path, dataset.getDimensions());
69  T data(dims[0], dims[1]);
70  dataset.read(reinterpret_cast<value_type*>(data.data));
71  return data;
72  }
73 
74  inline static Attribute dumpAttribute(File& file,
75  const std::string& path,
76  const std::string& key,
77  const T& data,
78  const DumpOptions& options) {
79  using value_type = typename T::value_type;
80  Attribute attribute = initAttribute<value_type>(file, path, key, shape(data), options);
81  std::vector<value_type> v(data.begin(), data.end());
82  attribute.write_raw(v.data());
83  if (options.flush()) {
84  file.flush();
85  }
86  return attribute;
87  }
88 
89  inline static T loadAttribute(const File& file,
90  const std::string& path,
91  const std::string& key) {
92  using value_type = typename T::value_type;
93  DataSet dataset = file.getDataSet(path);
94  Attribute attribute = dataset.getAttribute(key);
95  DataSpace dataspace = attribute.getSpace();
96  std::vector<int> dims = shape(file, path, dataspace.getDimensions());
97  T data(dims[0], dims[1]);
98  attribute.read(reinterpret_cast<value_type*>(data.data));
99  return data;
100  }
101 };
102 
103 } // namespace detail
104 } // namespace H5Easy
105 
106 #endif // H5_USE_OPENCV
107 #endif // H5EASY_BITS_OPENCV_HPP
H5Easy_misc.hpp
H5Easy_scalar.hpp
H5Easy::detail::io_impl::dumpAttribute
static Attribute dumpAttribute(File &file, const std::string &path, const std::string &key, const T &data, const DumpOptions &options)
Definition: H5Easy_scalar.hpp:45
HighFive::ObjectType::DataSpace
@ DataSpace
HighFive::ObjectType::Attribute
@ Attribute
H5Easy
Definition: H5Easy.hpp:51
H5Easy::detail::io_impl::loadAttribute
static T loadAttribute(const File &file, const std::string &path, const std::string &key)
Definition: H5Easy_scalar.hpp:58
H5Easy::detail::error
Exception error(const File &file, const std::string &path, const std::string &message)
Definition: H5Easy_misc.hpp:52
string
NAME string(REPLACE ".cpp" "_bin" example_name ${example_filename}) if($
Definition: hops/Third-party/HighFive/src/examples/CMakeLists.txt:6
H5Easy::detail::io_impl::load
static T load(const File &file, const std::string &path)
Definition: H5Easy_scalar.hpp:38
H5Easy::detail::io_impl::dump
static DataSet dump(File &file, const std::string &path, const T &data, const DumpOptions &options)
Definition: H5Easy_scalar.hpp:26