diff --git a/.vscode/settings.json b/.vscode/settings.json index c83a419..06f8243 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -96,7 +96,8 @@ "source_location": "cpp", "future": "cpp", "typeindex": "cpp", - "*.in": "c" + "*.in": "c", + "stdfloat": "cpp" }, "C_Cpp.errorSquiggles": "enabled" } \ No newline at end of file diff --git a/core/include/aare/Frame.hpp b/core/include/aare/Frame.hpp index bf33b07..27e1709 100644 --- a/core/include/aare/Frame.hpp +++ b/core/include/aare/Frame.hpp @@ -1,8 +1,10 @@ #pragma once +#include "aare/View.hpp" #include "aare/defs.hpp" -#include #include #include +#include +#include #include #include @@ -16,36 +18,35 @@ class Frame { ssize_t m_rows; ssize_t m_cols; ssize_t m_bitdepth; - std::byte* m_data; - + std::byte *m_data; public: - Frame(ssize_t rows, ssize_t cols,ssize_t m_bitdepth); - Frame(std::byte *fp, ssize_t rows, ssize_t cols,ssize_t m_bitdepth); - std::byte* get(int row, int col); - template - void set(int row, int col,T data); + Frame(ssize_t rows, ssize_t cols, ssize_t m_bitdepth); + Frame(std::byte *fp, ssize_t rows, ssize_t cols, ssize_t m_bitdepth); + std::byte *get(int row, int col); + template void set(int row, int col, T data); // std::vector> get_array(); - ssize_t rows() const{ - return m_rows; - } - ssize_t cols() const{ - return m_cols; - } - ssize_t bitdepth() const{ - return m_bitdepth; - } - std::byte* _get_data(){ - return m_data; - } - Frame& operator=(Frame& other){ - m_rows = other.rows(); - m_cols = other.cols(); - m_bitdepth = other.bitdepth(); - m_data = new std::byte[m_rows*m_cols*m_bitdepth/8]; - std::memcpy(m_data, other.m_data, m_rows*m_cols*m_bitdepth/8); + ssize_t rows() const { return m_rows; } + ssize_t cols() const { return m_cols; } + ssize_t bitdepth() const { return m_bitdepth; } + std::byte *_get_data() { return m_data; } + Frame &operator=(Frame &other) { + m_rows = other.rows(); + m_cols = other.cols(); + m_bitdepth = other.bitdepth(); + m_data = new std::byte[m_rows * m_cols * m_bitdepth / 8]; + std::memcpy(m_data, other.m_data, m_rows * m_cols * m_bitdepth / 8); return *this; } + template + View view() { + std::vector shape = {m_rows, m_cols}; + T* data = reinterpret_cast(m_data); + return View(data, shape); + } + ~Frame() { delete[] m_data; } }; + + diff --git a/core/include/aare/VariableSizeClusterFinder.hpp b/core/include/aare/VariableSizeClusterFinder.hpp index 24c2a83..af3fd7a 100644 --- a/core/include/aare/VariableSizeClusterFinder.hpp +++ b/core/include/aare/VariableSizeClusterFinder.hpp @@ -30,12 +30,12 @@ template class ClusterFinder { private: const std::array shape_; - DataSpan original_; + View original_; ImageData labeled_; ImageData peripheral_labeled_; ImageData binary_; // over threshold flag T threshold_; - DataSpan noiseMap; + View noiseMap; bool use_noise_map = false; int peripheralThresholdFactor_ = 5; int current_label; @@ -58,12 +58,12 @@ template class ClusterFinder { ImageData labeled() { return labeled_; } - void set_noiseMap(DataSpan noise_map) { noiseMap = noise_map; use_noise_map = true; } + void set_noiseMap(View noise_map) { noiseMap = noise_map; use_noise_map = true; } void set_peripheralThresholdFactor(int factor) { peripheralThresholdFactor_ = factor; } - void find_clusters(DataSpan img); - void find_clusters_X(DataSpan img); + void find_clusters(View img); + void find_clusters_X(View img); void rec_FillHit(int clusterIndex, int i, int j); - void single_pass(DataSpan img); + void single_pass(View img); void first_pass(); void second_pass(); void store_clusters(); @@ -144,7 +144,7 @@ template int ClusterFinder::check_neighbours(int i, int j) { } } -template void ClusterFinder::find_clusters(DataSpan img) { +template void ClusterFinder::find_clusters(View img) { original_ = img; labeled_ = 0; peripheral_labeled_ = 0; @@ -156,7 +156,7 @@ template void ClusterFinder::find_clusters(DataSpan img) { store_clusters(); } -template void ClusterFinder::find_clusters_X(DataSpan img) { +template void ClusterFinder::find_clusters_X(View img) { original_ = img; int clusterIndex = 0; for (int i = 0; i < shape_[0]; ++i) { @@ -214,7 +214,7 @@ template void ClusterFinder::rec_FillHit(int clusterIndex, int i } } -template void ClusterFinder::single_pass(DataSpan img) { +template void ClusterFinder::single_pass(View img) { original_ = img; labeled_ = 0; current_label = 0; diff --git a/core/include/aare/DataSpan.hpp b/core/include/aare/View.hpp similarity index 68% rename from core/include/aare/DataSpan.hpp rename to core/include/aare/View.hpp index 7df8edc..7f32302 100644 --- a/core/include/aare/DataSpan.hpp +++ b/core/include/aare/View.hpp @@ -4,7 +4,6 @@ #include #include #include -#include #include @@ -31,18 +30,18 @@ template std::array make_array(const std::vector class DataSpan { +template class View { public: - DataSpan(){}; + View(){}; - DataSpan(T* buffer, std::array shape) { + View(T* buffer, std::array shape) { buffer_ = buffer; strides_ = c_strides(shape); shape_ = shape; size_ = std::accumulate(std::begin(shape), std::end(shape), 1, std::multiplies()); } - DataSpan(T *buffer, const std::vector &shape) { + View(T *buffer, const std::vector &shape) { buffer_ = buffer; strides_ = c_strides(make_array(shape)); shape_ = make_array(shape); @@ -50,14 +49,6 @@ template class DataSpan { } - DataSpan(Frame& frame) { - assert(Ndim == 2); - buffer_ = reinterpret_cast(frame._get_data()); - strides_ = c_strides({frame.rows(), frame.cols()}); - shape_ = {frame.rows(), frame.cols()}; - size_ = frame.rows() * frame.cols(); - } - template typename std::enable_if::type operator()(Ix... index) { return buffer_[element_offset(strides_, index...)]; } @@ -68,28 +59,28 @@ template class DataSpan { ssize_t size() const { return size_; } - DataSpan(const DataSpan &) = default; - DataSpan(DataSpan &&) = default; + View(const View &) = default; + View(View &&) = default; T *begin() { return buffer_; } T *end() { return buffer_ + size_; } T &operator()(ssize_t i) { return buffer_[i]; } T &operator[](ssize_t i) { return buffer_[i]; } - DataSpan &operator+=(const T val) { return elemenwise(val, std::plus()); } - DataSpan &operator-=(const T val) { return elemenwise(val, std::minus()); } - DataSpan &operator*=(const T val) { return elemenwise(val, std::multiplies()); } - DataSpan &operator/=(const T val) { return elemenwise(val, std::divides()); } + View &operator+=(const T val) { return elemenwise(val, std::plus()); } + View &operator-=(const T val) { return elemenwise(val, std::minus()); } + View &operator*=(const T val) { return elemenwise(val, std::multiplies()); } + View &operator/=(const T val) { return elemenwise(val, std::divides()); } - DataSpan &operator/=(const DataSpan &other) { return elemenwise(other, std::divides()); } + View &operator/=(const View &other) { return elemenwise(other, std::divides()); } - DataSpan &operator=(const T val) { + View &operator=(const T val) { for (auto it = begin(); it != end(); ++it) *it = val; return *this; } - DataSpan &operator=(const DataSpan &other) { + View &operator=(const View &other) { shape_ = other.shape_; strides_ = other.strides_; size_ = other.size_; @@ -107,13 +98,13 @@ template class DataSpan { std::array shape_{}; ssize_t size_{}; - template DataSpan &elemenwise(T val, BinaryOperation op) { + template View &elemenwise(T val, BinaryOperation op) { for (ssize_t i = 0; i != size_; ++i) { buffer_[i] = op(buffer_[i], val); } return *this; } - template DataSpan &elemenwise(const DataSpan &other, BinaryOperation op) { + template View &elemenwise(const View &other, BinaryOperation op) { for (ssize_t i = 0; i != size_; ++i) { buffer_[i] = op(buffer_[i], other.buffer_[i]); } @@ -121,5 +112,5 @@ template class DataSpan { } }; -template class DataSpan; +template class View; diff --git a/core/test/wrappers.test.cpp b/core/test/wrappers.test.cpp index fc29af2..e2983c3 100644 --- a/core/test/wrappers.test.cpp +++ b/core/test/wrappers.test.cpp @@ -1,4 +1,4 @@ -#include +#include #include #include #include @@ -22,20 +22,20 @@ TEST_CASE("Frame") { delete[] data; } -TEST_CASE("DataSpan") { +TEST_CASE("View") { auto data = new uint16_t[100]; for (int i = 0; i < 100; i++) { data[i] = i; } SECTION("constructors") { - DataSpan ds(data, std::vector({10, 10})); + View ds(data, std::vector({10, 10})); for (int i = 0; i < 100; i++) { REQUIRE(ds(i / 10, i % 10) == data[i]); } } SECTION("from Frame") { Frame f(reinterpret_cast(data), 10, 10, 16); - DataSpan ds(f); + View ds = f.view(); for (int i = 0; i < 100; i++) { REQUIRE(ds(i / 10, i % 10) == data[i]); }