#ifndef __TEST_UTILITY_H__ #define __TEST_UTILITY_H__ #include #include #include "host_image.h" static std::default_random_engine g_engine; template constexpr T min_of() { return std::numeric_limits::min(); } template <> constexpr uint8_t min_of() { return 0; } template constexpr T max_of() { return std::numeric_limits::max(); } template <> constexpr uint8_t max_of() { return 255; } template static void random_fill_(T* dst, size_t n, T minv = min_of(), T maxv = max_of()) { std::uniform_int_distribution dist(minv, maxv); for (size_t i = 0; i < n; ++i) dst[i] = dist(g_engine); } template <> void random_fill_(uint8_t* dst, size_t n, uint8_t minv, uint8_t maxv) { std::uniform_int_distribution dist(minv, maxv); for (size_t i = 0; i < n; ++i) dst[i] = static_cast(dist(g_engine)); } template static void random_fill_(sgm::HostImage& image, T minv = min_of(), T maxv = max_of()) { random_fill_(image.ptr(), image.rows * (size_t)image.step, minv, maxv); } static void random_fill(sgm::HostImage& image) { if (image.type == sgm::SGM_8U) random_fill_(image); if (image.type == sgm::SGM_16U) random_fill_(image); if (image.type == sgm::SGM_32U) random_fill_(image); if (image.type == sgm::SGM_64U) random_fill_(image); } static void random_fill(sgm::HostImage& image, int minv, int maxv) { if (image.type == sgm::SGM_8U) random_fill_(image, minv, maxv); if (image.type == sgm::SGM_16U) random_fill_(image, minv, maxv); if (image.type == sgm::SGM_32U) random_fill_(image, minv, maxv); if (image.type == sgm::SGM_64U) random_fill_(image, minv, maxv); } template static int count_nonzero_(const sgm::HostImage& a, const sgm::HostImage& b) { if (a.cols != b.cols || a.rows != b.rows) return -1; int count = 0; for (int y = 0; y < a.rows; y++) { const T* pa = a.ptr(y); const T* pb = b.ptr(y); for (int x = 0; x < a.cols; x++) if (pa[x] != pb[x]) count++; } return count; } static int count_nonzero(const sgm::HostImage& a, const sgm::HostImage& b) { if (a.type != b.type) return -1; if (a.type == sgm::SGM_8U) return count_nonzero_(a, b); if (a.type == sgm::SGM_16U) return count_nonzero_(a, b); if (a.type == sgm::SGM_32U) return count_nonzero_(a, b); if (a.type == sgm::SGM_64U) return count_nonzero_(a, b); return -1; } template static bool equals_(const sgm::HostImage& a, const sgm::HostImage& b) { if (a.cols != b.cols || a.rows != b.rows) return false; for (int y = 0; y < a.rows; y++) { const T* pa = a.ptr(y); const T* pb = b.ptr(y); for (int x = 0; x < a.cols; x++) if (pa[x] != pb[x]) return false; } return true; } static bool equals(const sgm::HostImage& a, const sgm::HostImage& b) { if (a.type != b.type) return false; if (a.type == sgm::SGM_8U) return equals_(a, b); if (a.type == sgm::SGM_16U) return equals_(a, b); if (a.type == sgm::SGM_32U) return equals_(a, b); if (a.type == sgm::SGM_64U) return equals_(a, b); return false; } static bool equals(const sgm::HostImage& h_a, const sgm::DeviceImage& d_b) { if (h_a.type != d_b.type || h_a.rows != d_b.rows || h_a.cols != d_b.cols) return false; sgm::HostImage h_b(d_b.rows, d_b.cols, d_b.type, d_b.step); d_b.download(h_b.data); return equals(h_a, h_b); } #endif // !__TEST_UTILITY_H__