Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-04-19 08:55:35

0001 /*
0002  *  Copyright (c), 2017, Adrien Devresse <adrien.devresse@epfl.ch>
0003  *
0004  *  Distributed under the Boost Software License, Version 1.0.
0005  *    (See accompanying file LICENSE_1_0.txt or copy at
0006  *          http://www.boost.org/LICENSE_1_0.txt)
0007  *
0008  */
0009 #pragma once
0010 
0011 #include "../H5Easy.hpp"
0012 #include "H5Easy_misc.hpp"
0013 #include "H5Easy_scalar.hpp"
0014 
0015 #ifdef H5_USE_OPENCV
0016 
0017 namespace H5Easy {
0018 
0019 namespace detail {
0020 
0021 template <class T>
0022 struct is_opencv: std::false_type {};
0023 template <class T>
0024 struct is_opencv<cv::Mat_<T>>: std::true_type {};
0025 
0026 template <typename T>
0027 struct io_impl<T, typename std::enable_if<is_opencv<T>::value>::type> {
0028     inline static std::vector<size_t> shape(const T& data) {
0029         return std::vector<size_t>{static_cast<size_t>(data.rows), static_cast<size_t>(data.cols)};
0030     }
0031 
0032     inline static std::vector<int> shape(const File& file,
0033                                          const std::string& path,
0034                                          std::vector<size_t> dims) {
0035         if (dims.size() == 1) {
0036             return std::vector<int>{static_cast<int>(dims[0]), 1ul};
0037         }
0038         if (dims.size() == 2) {
0039             return std::vector<int>{static_cast<int>(dims[0]), static_cast<int>(dims[1])};
0040         }
0041 
0042         throw detail::error(file, path, "H5Easy::load: Inconsistent rank");
0043     }
0044 
0045     inline static DataSet dump(File& file,
0046                                const std::string& path,
0047                                const T& data,
0048                                const DumpOptions& options) {
0049         using value_type = typename T::value_type;
0050         DataSet dataset = initDataset<value_type>(file, path, shape(data), options);
0051         std::vector<value_type> v(data.begin(), data.end());
0052         dataset.write_raw(v.data());
0053         if (options.flush()) {
0054             file.flush();
0055         }
0056         return dataset;
0057     }
0058 
0059     inline static T load(const File& file, const std::string& path) {
0060         using value_type = typename T::value_type;
0061         DataSet dataset = file.getDataSet(path);
0062         std::vector<int> dims = shape(file, path, dataset.getDimensions());
0063         T data(dims[0], dims[1]);
0064         dataset.read_raw(reinterpret_cast<value_type*>(data.data));
0065         return data;
0066     }
0067 
0068     inline static Attribute dumpAttribute(File& file,
0069                                           const std::string& path,
0070                                           const std::string& key,
0071                                           const T& data,
0072                                           const DumpOptions& options) {
0073         using value_type = typename T::value_type;
0074         Attribute attribute = initAttribute<value_type>(file, path, key, shape(data), options);
0075         std::vector<value_type> v(data.begin(), data.end());
0076         attribute.write_raw(v.data());
0077         if (options.flush()) {
0078             file.flush();
0079         }
0080         return attribute;
0081     }
0082 
0083     inline static T loadAttribute(const File& file,
0084                                   const std::string& path,
0085                                   const std::string& key) {
0086         using value_type = typename T::value_type;
0087         DataSet dataset = file.getDataSet(path);
0088         Attribute attribute = dataset.getAttribute(key);
0089         DataSpace dataspace = attribute.getSpace();
0090         std::vector<int> dims = shape(file, path, dataspace.getDimensions());
0091         T data(dims[0], dims[1]);
0092         attribute.read_raw(reinterpret_cast<value_type*>(data.data));
0093         return data;
0094     }
0095 };
0096 
0097 }  // namespace detail
0098 }  // namespace H5Easy
0099 
0100 #endif  // H5_USE_OPENCV