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/Makefile | 12 +++++- svg-flatten/src/nopencv.cpp | 4 +- svg-flatten/src/nopencv.hpp | 10 ++++- svg-flatten/src/nopencv_test.cpp | 79 +++++++++++++++++++++++++++++++--------- 4 files changed, 83 insertions(+), 22 deletions(-) diff --git a/svg-flatten/Makefile b/svg-flatten/Makefile index a82094c..0824561 100644 --- a/svg-flatten/Makefile +++ b/svg-flatten/Makefile @@ -35,9 +35,11 @@ BASE64_INCLUDES ?= -I$(UPSTREAM_DIR)/cpp-base64 ARGAGG_INCLUDES ?= -I$(UPSTREAM_DIR)/argagg/include/argagg CAVC_INCLUDES ?= -I$(UPSTREAM_DIR)/CavalierContours/include/cavc/ SUBPROCESS_INCLUDES ?= -I$(UPSTREAM_DIR)/subprocess.h +MINUNIT_INCLUDES ?= -I$(UPSTREAM_DIR)/minunit +STB_INCLUDES ?= -isystem$(UPSTREAM_DIR)/stb SOURCES += $(CLIPPER_SOURCES) -INCLUDES := -Iinclude -Isrc $(CLIPPER_INCLUDES) $(VORONOI_INCLUDES) $(POISSON_INCLUDES) $(BASE64_INCLUDES) $(ARGAGG_INCLUDES) $(CAVC_INCLUDES) $(SUBPROCESS_INCLUDES) +INCLUDES := -Iinclude -Isrc $(CLIPPER_INCLUDES) $(VORONOI_INCLUDES) $(POISSON_INCLUDES) $(BASE64_INCLUDES) $(ARGAGG_INCLUDES) $(CAVC_INCLUDES) $(SUBPROCESS_INCLUDES) $(MINUNIT_INCLUDES) $(STB_INCLUDES) PKG_CONFIG_DEPS := pugixml CXXFLAGS := -std=c++2a -g -Wall -Wextra -O0 @@ -74,6 +76,14 @@ $(BUILDDIR)/$(TARGET): $(SOURCES:%.cpp=$(BUILDDIR)/%.o) $(CXX) $(CXXFLAGS) $(LDFLAGS) -o $@ -Wl,--start-group $^ -lstdc++fs -Wl,--end-group; \ fi +$(BUILDDIR)/nopencv-tests: src/nopencv_test.cpp src/nopencv.cpp + @mkdir -p $(dir $@) + $(CXX) $(CXXFLAGS) $(INCLUDES) $(LDFLAGS) -o $@ $^ + + +.PHONY: tests +tests: $(BUILDDIR)/nopencv-tests + .PHONY: install install: $(INSTALL) $(BUILDDIR)/$(TARGET) $(PREFIX)/bin 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