H5FrameProvider.hpp 1.74 KB

#pragma once

#include <array>

#include "data/hdf5/H5SingleDataReader.hpp"
#include "data/provider/FrameProvider.hpp"
#include "data/provider/ProviderOpenFailureException.hpp"

class H5FrameProvider : public FrameProvider, public H5SingleDataReader {
  public:
	H5FrameProvider(const std::string &path, const std::string &dataSetPath,
	                const std::vector<unsigned long> &timestamps) try
	    : H5SingleDataReader(path, dataSetPath, H5SingleDataReader::UINT16, 2),
	      timestamps(timestamps) {
	} catch (const H5::FileIException &exception) {
		throw ProviderOpenFailureException(exception.getDetailMsg());
	}

	H5FrameProvider(const std::string &path, const std::string &dataSetPath) try
	    : H5SingleDataReader(path, dataSetPath, H5SingleDataReader::UINT16, 2),
	      timestamps(std::vector<unsigned long>(dimensions[2], 0)) {
	} catch (const H5::FileIException &exception) {
		throw ProviderOpenFailureException(exception.getDetailMsg());
	}

	bool hasNext() const override {
		return current < dimensions[2] && current <= lastFrame;
	}

	unsigned long next(void *buffer) override {
		selectImage(current);
		read(buffer);
		return timestamps[current++];
	}

	void first(void *buffer) const {
		selectImage(0);
		read(buffer);
	}

	size_t rows() const override { return dimensions[0]; }

	size_t columns() const override { return dimensions[1]; }

  private:
	hsize_t current = 0; // 239 - 5; /* Modify to start from further frame */

	static constexpr hsize_t lastFrame{258}; /* Peak */
	// static constexpr hsize_t lastFrame{41}; /* Rise */
	// static constexpr hsize_t lastFrame{9999}; /* Entire */

	const std::vector<unsigned long> timestamps;

	void selectImage(hsize_t image) const {
		select({dimensions[0], dimensions[1], 1}, {0, 0, image});
	}
};