MayaFlux 1.0.0
Digital-First Multimedia Processing Framework
Loading...
Searching...
No Matches

◆ open()

bool MayaFlux::IO::NpyReader::open ( const std::string & filepath,
FileReadOptions options = FileReadOptions::ALL )
overridevirtual

Open a file for reading.

Parameters
filepathPath to the file.
optionsReading options (see FileReadOptions).
Returns
true if file was successfully opened.

Implements MayaFlux::IO::FileReader.

Definition at line 49 of file NpyFormat.cpp.

50{
51 close();
52 m_path = resolve_path(filepath);
53 m_file.open(m_path, std::ios::binary);
54 if (!m_file) {
55 return fail("cannot open '" + m_path + "'");
56 }
57
58 m_file.seekg(0, std::ios::end);
59 const auto file_size = static_cast<uint64_t>(m_file.tellg());
60 m_file.seekg(0);
61
62 std::array<std::byte, 12> leading {};
63 m_file.read(reinterpret_cast<char*>(leading.data()), static_cast<std::streamsize>(leading.size()));
64 const auto got = static_cast<size_t>(m_file.gcount());
65 m_file.clear();
66
67 std::string why;
68 const auto prefix_size = Detail::npy_prefix_size(std::span<const std::byte>(leading).first(got), why);
69 if (!prefix_size || *prefix_size > file_size) {
70 close();
71 return fail(prefix_size ? "numpy header is truncated" : why);
72 }
73
74 std::vector<std::byte> prefix(*prefix_size);
75 m_file.seekg(0);
76 if (!m_file.read(reinterpret_cast<char*>(prefix.data()), static_cast<std::streamsize>(prefix.size()))) {
77 close();
78 return fail("numpy header is truncated");
79 }
80
81 const auto stem = std::filesystem::path(m_path).stem().string();
82 const auto header = Detail::parse_npy_header(prefix, stem, why);
83 if (!header) {
84 close();
85 return fail(why);
86 }
87
88 const auto bytes = header->spec.dtype.storage_bytes(header->spec.element_count().value_or(0));
89 if (!bytes || header->data_offset + *bytes > file_size) {
90 close();
91 return fail("numpy data is shorter than its header declares");
92 }
93
94 m_spec = header->spec;
95 m_data_offset = header->data_offset;
96 m_big_endian = header->big_endian;
97 m_open = true;
98 m_last_error.clear();
99 return true;
100}
static std::string resolve_path(const std::string &filepath)
Resolve a filepath against the project source root if not found as-is.
Kakshya::TensorSpec m_spec
Definition NpyFormat.hpp:51
void close() override
Close the currently open file.
Definition NpyFormat.cpp:39
bool fail(std::string reason)
Record a failure reason and return false.
std::optional< NpyHeader > parse_npy_header(std::span< const std::byte > prefix, std::string_view name, std::string &why)
Decode a .npy prefix into a tensor spec.
Definition NpyCodec.cpp:137
std::optional< size_t > npy_prefix_size(std::span< const std::byte > leading, std::string &why)
Total prefix size (magic, version, length field and header) of a .npy array.
Definition NpyCodec.cpp:107

References close(), MayaFlux::IO::TensorReader::fail(), m_big_endian, m_data_offset, m_file, MayaFlux::IO::TensorReader::m_last_error, m_open, MayaFlux::IO::TensorReader::m_path, m_spec, MayaFlux::IO::Detail::npy_prefix_size(), MayaFlux::IO::Detail::parse_npy_header(), and MayaFlux::IO::FileReader::resolve_path().

Here is the call graph for this function: