streamlet examples: QR: first version v_0
authorFrantišek Kučera <franta-hg@frantovo.cz>
Sat, 24 Apr 2021 20:03:39 +0200
branchv_0
changeset 86 3caa19520689
parent 85 29a300533bda
child 87 d846da24dd35
streamlet examples: QR: first version
.hgignore
streamlet-examples/Makefile
streamlet-examples/qr.cpp
--- a/.hgignore	Sat Apr 24 20:03:18 2021 +0200
+++ b/.hgignore	Sat Apr 24 20:03:39 2021 +0200
@@ -13,4 +13,4 @@
 ^build/
 ^nbproject/private/
 
-^streamlet-examples/(xpath|pid|jar_info|zip_info)$
\ No newline at end of file
+^streamlet-examples/(xpath|pid|qr|jar_info|zip_info)$
--- a/streamlet-examples/Makefile	Sat Apr 24 20:03:18 2021 +0200
+++ b/streamlet-examples/Makefile	Sat Apr 24 20:03:39 2021 +0200
@@ -15,7 +15,7 @@
 # You should have received a copy of the GNU General Public License
 # along with this program. If not, see <http://www.gnu.org/licenses/>.
 
-all: xpath pid jar_info zip_info
+all: xpath pid qr jar_info zip_info
 	
 .PHONY: all clean
 
@@ -25,6 +25,9 @@
 pid: streamlet-common.h pid.cpp
 	g++ -g -fno-omit-frame-pointer -fsanitize=address pid.cpp -o pid
 
+qr: streamlet-common.h qr.cpp
+	g++ -g -fno-omit-frame-pointer -fsanitize=address qr.cpp -o qr $(shell pkg-config --libs --cflags relpipe-lib-xmlwriter.cpp zbar Magick++)
+
 jar_info: Streamlet.java JarInfo.java
 	javac JarInfo.java
 	jar cfe jar_info JarInfo Streamlet*.class JarInfo.class
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/streamlet-examples/qr.cpp	Sat Apr 24 20:03:39 2021 +0200
@@ -0,0 +1,167 @@
+/**
+ * Relational pipes
+ * Copyright © 2020 František Kučera (Frantovo.cz, GlobalCode.info)
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, version 3 of the License.
+ *
+ * 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 General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include <unistd.h>
+
+#include <Magick++.h>
+#include <zbar.h>
+
+#include <relpipe/xmlwriter/XMLWriter.h>
+
+#include "streamlet-common.h"
+
+/**
+ * This streamlet extracts QR codes from image files.
+ * 
+ * It provides two attributes:
+ *  - qr: first QR code found (if any)
+ *  - qr_xml: XML containing all QR cpdes found an additional metadata
+ *	
+ */
+class QRStreamlet : public Streamlet {
+private:
+
+	const wstring XMLNS = L"tag:globalcode.info,2018:qr:FIXME:final-xmlns"; // FIXME: correct xmlns URI
+
+	class Symbol {
+	public:
+		int id;
+		std::wstring value;
+		std::wstring type;
+		int x;
+		int y;
+		int width;
+		int height;
+	};
+
+	std::vector<Symbol> findSymbols(std::wstring fileName) {
+		std::vector<Symbol> result;
+
+		Magick::Image magick(toBytes(fileName));
+		int width = magick.columns();
+		int height = magick.rows();
+		Magick::Blob blob;
+		magick.modifyImage();
+		magick.write(&blob, "GRAY", 8);
+		const void *raw = blob.data();
+
+		zbar::Image image(width, height, "Y800", raw, width * height);
+		zbar::ImageScanner scanner;
+
+		scanner.scan(image);
+
+		int id = 0;
+		for (zbar::Image::SymbolIterator symbol = image.symbol_begin(); symbol != image.symbol_end(); ++symbol, id++) {
+			Symbol s;
+
+
+			s.id = id;
+			s.type = fromBytes(symbol->get_type_name());
+			s.value = fromBytes(symbol->get_data());
+
+			int minX = 0;
+			int minY = 0;
+			int maxX = 0;
+			int maxY = 0;
+
+			// TODO: return original polygon in XML
+			for (int i = 0, locationSize = symbol->get_location_size(); i < locationSize; i++) {
+				int x = symbol->get_location_x(i);
+				int y = symbol->get_location_y(i);
+				minX = minX ? std::min(minX, x) : x;
+				minY = minY ? std::min(minY, y) : y;
+				maxX = std::max(maxX, x);
+				maxY = std::max(maxY, y);
+			}
+
+			s.x = minX;
+			s.y = minY;
+			s.width = maxX - minX;
+			s.height = maxY - minY;
+
+			result.push_back(s);
+		}
+
+
+
+		return result;
+	}
+
+protected:
+
+	std::vector<AttributeMetadata> getOutputAttributesMetadata() override {
+		std::vector<AttributeMetadata> oam;
+		int i = 0;
+		oam.push_back({getAlias(i++, L"qr"), STRING});
+		oam.push_back({getAlias(i++, L"qr_xml"), STRING});
+		return oam;
+	}
+
+	std::vector<OutputAttribute> getOutputAttributes() override {
+		bool validInput = false;
+		bool hasSymbols = false;
+		std::wstring first;
+		std::stringstream xml;
+
+		std::vector<Symbol> symbols;
+		try {
+			symbols = findSymbols(getCurrentFile());
+			validInput = true;
+		} catch (...) {
+			// just ignore the errors;
+			// the file is probably not an image or we do not have read permissions
+			validInput = false;
+		}
+
+		for (Symbol s : symbols) {
+			// TODO: match against pattern (if any)
+			first = s.value;
+			hasSymbols = true;
+			break;
+		}
+
+		relpipe::xmlwriter::XMLWriter xmlWriter(xml);
+		xmlWriter.writeStartElement(L"qr",{L"xmlns", XMLNS});
+
+		// TODO: common metadata
+		// xmlWriter.writeStartElement(L"source");
+		// xmlWriter.writeTextElement(L"height",{}, std::to_wstring(...));
+		// xmlWriter.writeTextElement(L"width",{}, std::to_wstring(...));
+		// xmlWriter.writeEndElement();
+
+		for (Symbol s : symbols) {
+			xmlWriter.writeStartElement(L"symbol");
+			xmlWriter.writeTextElement(L"value",{}, s.value);
+			xmlWriter.writeTextElement(L"x",{}, std::to_wstring(s.x));
+			xmlWriter.writeTextElement(L"y",{}, std::to_wstring(s.y));
+			xmlWriter.writeTextElement(L"height",{}, std::to_wstring(s.height));
+			xmlWriter.writeTextElement(L"width",{}, std::to_wstring(s.width));
+			// TODO: polygon
+			xmlWriter.writeEndElement();
+		}
+
+		xmlWriter.writeEndElement();
+
+		std::vector<OutputAttribute> oa;
+		// TODO: report also validInput (distinguish it from hasSymbols)
+		oa.push_back({first, !hasSymbols});
+		oa.push_back({fromBytes(xml.str()), false});
+		return oa;
+	}
+};
+
+STREAMLET_RUN(QRStreamlet)