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

◆ read()

std::optional< Kakshya::TensorData > MayaFlux::IO::NpzReader::read ( std::string_view name)
nodiscardoverridevirtual

Read one tensor by name.

Returns
The tensor, or std::nullopt with get_last_error() set.

Implements MayaFlux::IO::TensorReader.

Definition at line 152 of file NpzFormat.cpp.

153{
154 if (!m_open) {
155 fail("no file is open");
156 return std::nullopt;
157 }
158
159 const auto entry = std::ranges::find_if(m_entries,
160 [name](const Entry& candidate) { return candidate.spec.name == name; });
161 if (entry == m_entries.end()) {
162 fail("no tensor named '" + std::string(name) + "'");
163 return std::nullopt;
164 }
165
166 size_t size = 0;
167 void* heap = mz_zip_reader_extract_to_heap(&m_archive->zip, entry->index, &size, 0);
168 if (!heap) {
169 fail("cannot extract '" + std::string(name) + "'");
170 return std::nullopt;
171 }
172
173 auto tensor = Kakshya::TensorData::allocate(entry->spec);
174 const auto storage = tensor ? tensor->storage() : std::span<std::byte> {};
175 if (!tensor || entry->data_offset + storage.size() > size) {
176 mz_free(heap);
177 fail("tensor '" + std::string(name) + "' is truncated");
178 return std::nullopt;
179 }
180
181 if (!storage.empty()) {
182 std::memcpy(storage.data(), static_cast<const std::byte*>(heap) + entry->data_offset, storage.size());
183 if (entry->big_endian == Detail::host_is_little_endian()) {
184 Detail::swap_components(storage, Detail::component_bytes(entry->spec.dtype));
185 }
186 }
187 mz_free(heap);
188 return tensor;
189}
std::vector< Entry > m_entries
Definition NpzFormat.hpp:57
std::unique_ptr< Detail::NpzArchive > m_archive
Definition NpzFormat.hpp:56
bool fail(std::string reason)
Record a failure reason and return false.
static std::optional< TensorData > allocate(TensorSpec spec)
Zero-filled tensor for a spec.
Definition Tensor.cpp:72
size_t component_bytes(Kakshya::TensorDType dtype) noexcept
Bytes of one independently ordered component of an element.
void swap_components(std::span< std::byte > data, size_t component_bytes) noexcept
Reverse the byte order of every component in place.
bool host_is_little_endian() noexcept
Whether this machine stores multi-byte values least significant byte first.

References MayaFlux::Kakshya::TensorData::allocate(), MayaFlux::IO::Detail::component_bytes(), MayaFlux::IO::TensorReader::fail(), MayaFlux::IO::Detail::host_is_little_endian(), m_archive, m_entries, m_open, MayaFlux::Kakshya::TensorSpec::name, MayaFlux::IO::NpzReader::Entry::spec, and MayaFlux::IO::Detail::swap_components().

Here is the call graph for this function: