From bbf1c02e799573532d1f5416fafe1b2255168bba Mon Sep 17 00:00:00 2001 From: jaseg Date: Sun, 30 May 2021 20:22:59 +0200 Subject: Contour finding tests run through --- svg-flatten/src/nopencv.cpp | 4 +- svg-flatten/src/nopencv.hpp | 10 ++++- svg-flatten/src/nopencv_test.cpp | 79 +++++++++++++++++++++++++++++++--------- 3 files changed, 72 insertions(+), 21 deletions(-) (limited to 'svg-flatten/src') diff --git a/svg-flatten/src/nopencv.cpp b/svg-flatten/src/nopencv.cpp index 77c6cc6..22c3fff 100644 --- a/svg-flatten/src/nopencv.cpp +++ b/svg-flatten/src/nopencv.cpp @@ -47,7 +47,7 @@ static Direction flip_direction[8] = { D_SE }; -static void follow(gerbolyze::nopencv::Image32 img, int start_x, int start_y, Direction initial_direction, int nbd, int connectivity, Polygon &poly) { +static void follow(gerbolyze::nopencv::Image32 &img, int start_x, int start_y, Direction initial_direction, int nbd, int connectivity, Polygon &poly) { //cerr << "follow " << start_x << " " << start_y << " | dir=" << dir_str[initial_direction] << " nbd=" << nbd << " conn=" << connectivity << endl; int dir_inc = (connectivity == 4) ? 2 : 1; @@ -122,7 +122,7 @@ static void follow(gerbolyze::nopencv::Image32 img, int start_x, int start_y, Di } -void gerbolyze::nopencv::find_blobs(gerbolyze::nopencv::Image32 img, gerbolyze::nopencv::ContourCallback cb) { +void gerbolyze::nopencv::find_blobs(gerbolyze::nopencv::Image32 &img, gerbolyze::nopencv::ContourCallback cb) { int nbd = 1; Polygon poly; for (int y=0; y= 0 && y >= 0 && x < m_cols && y < m_rows); + assert(m_data != nullptr); + + m_data[y*m_cols + x] = val; + cerr << "set_at " << x << " " << y << ": " << val << " -> " << at(x, y) << endl; + }; + const int32_t &at(int x, int y) const { assert(x >= 0 && y >= 0 && x < m_cols && y < m_rows); assert(m_data != nullptr); @@ -92,7 +100,7 @@ namespace gerbolyze { int m_rows=0, m_cols=0; }; - void find_blobs(Image32 img, ContourCallback cb); + void find_blobs(Image32 &img, ContourCallback cb); } } diff --git a/svg-flatten/src/nopencv_test.cpp b/svg-flatten/src/nopencv_test.cpp index cc1c988..21710b9 100644 --- a/svg-flatten/src/nopencv_test.cpp +++ b/svg-flatten/src/nopencv_test.cpp @@ -18,6 +18,17 @@ using namespace gerbolyze::nopencv; char msg[1024]; +class TempfileHack { +public: + TempfileHack(const string ext) : m_path { filesystem::temp_directory_path() / (std::tmpnam(nullptr) + ext) } {} + ~TempfileHack() { remove(m_path); } + + const char *c_str() { return m_path.c_str(); } + +private: + filesystem::path m_path; +}; + MU_TEST(test_complex_example_from_paper) { int32_t img_data[6*9] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, @@ -116,38 +127,39 @@ MU_TEST(test_complex_example_from_paper) { } } -MU_TEST(test_round_trip) { +static void testdata_roundtrip(const char *fn) { int x, y; - uint8_t *data = stbi_load("testdata/paper-example.png", &x, &y, nullptr, 1); + uint8_t *data = stbi_load(fn, &x, &y, nullptr, 1); Image32 ref_img(x, y); for (int cy=0; cy" << endl; - svg << "" << endl; + svg << "" << endl; gerbolyze::nopencv::find_blobs(ref_img, [&svg](Polygon poly, ContourPolarity pol) { mu_assert(poly.size() > 0, "Empty contour returned"); mu_assert(poly.size() > 2, "Contour has less than three points, no area"); mu_assert(pol == CP_CONTOUR || pol == CP_HOLE, "Contour has invalid polarity"); - svg << "" << endl; + svg << " Z\"/>" << endl; }); svg << "" << endl; svg.close(); @@ -155,27 +167,27 @@ MU_TEST(test_round_trip) { const char *command_line[] = {"resvg", tmp_svg.c_str(), tmp_png.c_str()}; struct subprocess_s subprocess; int rc = subprocess_create(command_line, subprocess_option_inherit_environment, &subprocess); - mu_assert_int_eq(rc, 0); + mu_assert_int_eq(0, rc); int resvg_rc = -1; rc = subprocess_join(&subprocess, &resvg_rc); - mu_assert_int_eq(rc, 0); - mu_assert_int_eq(resvg_rc, 0); + mu_assert_int_eq(0, rc); + mu_assert_int_eq(0, resvg_rc); rc = subprocess_destroy(&subprocess); - mu_assert_int_eq(rc, 0); + mu_assert_int_eq(0, rc); int out_x, out_y; uint8_t *out_data = stbi_load(tmp_png.c_str(), &out_x, &out_y, nullptr, 1); - mu_assert_int_eq(out_x, x); - mu_assert_int_eq(out_y, y); + mu_assert_int_eq(x, out_x); + mu_assert_int_eq(y, out_y); for (int cy=0; cy