aboutsummaryrefslogtreecommitdiff
path: root/svg-flatten/src/nopencv.hpp
diff options
context:
space:
mode:
authorjaseg <git@jaseg.de>2021-05-30 19:39:45 +0200
committerjaseg <git@jaseg.de>2021-05-30 19:39:45 +0200
commitd1755701779b88389dc1d2e30d2430320a17f78e (patch)
tree5df31b227736737e270411f3b6c2d2c830c52442 /svg-flatten/src/nopencv.hpp
parente06bbdbe9b359fb168278b44f039b6d1108e5a2d (diff)
downloadgerbolyze-d1755701779b88389dc1d2e30d2430320a17f78e.tar.gz
gerbolyze-d1755701779b88389dc1d2e30d2430320a17f78e.tar.bz2
gerbolyze-d1755701779b88389dc1d2e30d2430320a17f78e.zip
Add beginnings of minimalist contour tracing code
Diffstat (limited to 'svg-flatten/src/nopencv.hpp')
-rw-r--r--svg-flatten/src/nopencv.hpp98
1 files changed, 98 insertions, 0 deletions
diff --git a/svg-flatten/src/nopencv.hpp b/svg-flatten/src/nopencv.hpp
new file mode 100644
index 0000000..b749808
--- /dev/null
+++ b/svg-flatten/src/nopencv.hpp
@@ -0,0 +1,98 @@
+/*
+ * This file is part of gerbolyze, a vector image preprocessing toolchain
+ * Copyright (C) 2021 Jan Sebastian Götte <gerbolyze@jaseg.de>
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program. If not, see <https://www.gnu.org/licenses/>.
+ */
+
+#pragma once
+
+#include <array>
+#include <string>
+#include <sstream>
+#include <cmath>
+#include <algorithm>
+#include <assert.h>
+
+#include "geom2d.hpp"
+
+using namespace std;
+
+namespace gerbolyze {
+ namespace nopencv {
+
+ enum ContourPolarity {
+ CP_CONTOUR,
+ CP_HOLE
+ };
+
+ typedef std::function<void(Polygon, ContourPolarity)> ContourCallback;
+
+ class Image32 {
+ public:
+ Image32(int size_x, int size_y, const int32_t *data=nullptr) {
+ 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);
+ }
+ }
+
+ Image32(const Image32 &other) : Image32(other.cols(), other.rows(), other.ptr()) {}
+
+ ~Image32() {
+ delete m_data;
+ }
+
+ int32_t &at(int x, int y) {
+ assert(x >= 0 && y >= 0 && x < m_cols && y < m_rows);
+ assert(m_data != nullptr);
+
+ return m_data[y*m_cols + x];
+ };
+
+ const int32_t &at(int x, int y) const {
+ assert(x >= 0 && y >= 0 && x < m_cols && y < m_rows);
+ assert(m_data != nullptr);
+
+ return m_data[y*m_cols + x];
+ };
+
+ int32_t at_default(int x, int y, int32_t default_value=0) const {
+ assert(m_data != nullptr);
+
+ if (x >= 0 && y >= 0 && x < m_cols && y < m_rows) {
+ return at(x, y);
+
+ } else {
+ return default_value;
+ }
+ };
+
+ int rows() const { return m_rows; }
+ int cols() const { return m_cols; }
+ const int32_t *ptr() const { return m_data; }
+
+ private:
+ int32_t *m_data = nullptr;
+ int m_rows=0, m_cols=0;
+ };
+
+ void find_blobs(Image32 img, ContourCallback cb);
+ }
+}
+