From 536a34cd59258d3a75d4caa8431fc688b1e788f6 Mon Sep 17 00:00:00 2001 From: jaseg Date: Wed, 2 Jun 2021 11:31:38 +0200 Subject: Prettify test cases --- svg-flatten/src/nopencv.cpp | 53 ++++++++++++++++++++++++++++ svg-flatten/src/nopencv.hpp | 24 ++++++------- svg-flatten/src/nopencv_test.cpp | 74 ++++++++++++++++------------------------ 3 files changed, 95 insertions(+), 56 deletions(-) diff --git a/svg-flatten/src/nopencv.cpp b/svg-flatten/src/nopencv.cpp index 2384fcc..18d3f20 100644 --- a/svg-flatten/src/nopencv.cpp +++ b/svg-flatten/src/nopencv.cpp @@ -2,6 +2,9 @@ #include #include +#define STB_IMAGE_IMPLEMENTATION +#include "stb_image.h" + #include "nopencv.hpp" using namespace gerbolyze; @@ -374,3 +377,53 @@ ContourCallback gerbolyze::nopencv::simplify_contours_teh_chin(ContourCallback c }; } + +gerbolyze::nopencv::Image32::Image32(int size_x, int size_y, const int32_t *data) { + assert(size_x > 0 && size_x < 100000); + assert(size_y > 0 && size_y < 100000); + m_data = new int32_t[size_x * size_y] { 0 }; + m_rows = size_y; + m_cols = size_x; + if (data != nullptr) { + memcpy(m_data, data, sizeof(int32_t) * size_x * size_y); + } +} + +bool gerbolyze::nopencv::Image32::load(const char *filename) { + return stb_to_internal(stbi_load(filename, &m_cols, &m_rows, nullptr, 1)); +} + +bool gerbolyze::nopencv::Image32::load_memory(uint8_t *buf, size_t len) { + return stb_to_internal(stbi_load_from_memory(buf, len, &m_cols, &m_rows, nullptr, 1)); +} + +void gerbolyze::nopencv::Image32::binarize() { + assert(m_data != nullptr); + assert(m_rows > 0 && m_cols > 0); + + for (int y=0; y 0; + } + } +} + +bool gerbolyze::nopencv::Image32::stb_to_internal(uint8_t *data) { + if (data == nullptr) + return false; + + if (m_rows < 0 || m_rows > 100000) + return false; + if (m_cols < 0 || m_cols > 100000) + return false; + + m_data = new int32_t[size()] { 0 }; + for (int y=0; y 0 && size_x < 100000); - assert(size_y > 0 && size_y < 100000); - m_data = new int32_t[size_x * size_y] { 0 }; - m_rows = size_y; - m_cols = size_x; - if (data != nullptr) { - memcpy(m_data, data, sizeof(int32_t) * size_x * size_y); - } - } - + Image32() {} + Image32(int size_x, int size_y, const int32_t *data=nullptr); Image32(const Image32 &other) : Image32(other.cols(), other.rows(), other.ptr()) {} ~Image32() { - delete m_data; + if (m_data) { + delete m_data; + } } + bool load(const char *filename); + bool load_memory(uint8_t *buf, size_t len); + void binarize(); + int32_t &at(int x, int y) { assert(x >= 0 && y >= 0 && x < m_cols && y < m_rows); assert(m_data != nullptr); @@ -93,9 +90,12 @@ namespace gerbolyze { int rows() const { return m_rows; } int cols() const { return m_cols; } + int size() const { return m_cols*m_rows; } const int32_t *ptr() const { return m_data; } private: + bool stb_to_internal(uint8_t *data); + int32_t *m_data = nullptr; int m_rows=0, m_cols=0; }; diff --git a/svg-flatten/src/nopencv_test.cpp b/svg-flatten/src/nopencv_test.cpp index 389de60..902aa18 100644 --- a/svg-flatten/src/nopencv_test.cpp +++ b/svg-flatten/src/nopencv_test.cpp @@ -10,7 +10,6 @@ #include #include -#define STB_IMAGE_IMPLEMENTATION #include "stb_image.h" using namespace gerbolyze; @@ -180,42 +179,35 @@ int render_svg(const char *in_svg, const char *out_png) { } static void testdata_roundtrip(const char *fn) { - int x, y; - uint8_t *data = stbi_load(fn, &x, &y, nullptr, 1); - Image32 ref_img(x, y); - for (int cy=0; cy 0.5) { snprintf(msg, sizeof(msg), "%s: Chain approximation RMS error is above threshold: %.3f > 0.5\n", fn, rms_sum); mu_fail(msg); -- cgit