streamlet examples: QR: rename qr → barcode v_0
authorFrantišek Kučera <franta-hg@frantovo.cz>
Sat, 01 May 2021 18:18:01 +0200
branchv_0
changeset 92 cf4971342380
parent 91 cb1adcd17d0c
child 93 979848ccdc9c
streamlet examples: QR: rename qr → barcode
.hgignore
nbproject/configurations.xml
streamlet-examples/Makefile
streamlet-examples/barcode-reader.cpp
streamlet-examples/qr-decode.cpp
--- a/.hgignore	Sun Apr 25 20:30:53 2021 +0200
+++ b/.hgignore	Sat May 01 18:18:01 2021 +0200
@@ -13,4 +13,4 @@
 ^build/
 ^nbproject/private/
 
-^streamlet-examples/(xpath|pid|qr-decode|jar_info|zip_info)$
+^streamlet-examples/(xpath|pid|barcode-reader|jar_info|zip_info)$
--- a/nbproject/configurations.xml	Sun Apr 25 20:30:53 2021 +0200
+++ b/nbproject/configurations.xml	Sat May 01 18:18:01 2021 +0200
@@ -161,6 +161,12 @@
                             perm="755"
                             owner="root"
                             group="bin"/>
+          <packFileListElem type="File"
+                            to="${PACKAGE_TOP_DIR}bin/${OUTPUT_BASENAME}"
+                            from="${OUTPUT_PATH}"
+                            perm="755"
+                            owner="root"
+                            group="bin"/>
         </packFileList>
       </packaging>
       <item path="src/SubProcess.cpp" ex="false" tool="1" flavor2="11">
--- a/streamlet-examples/Makefile	Sun Apr 25 20:30:53 2021 +0200
+++ b/streamlet-examples/Makefile	Sat May 01 18:18:01 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 qr-decode jar_info zip_info
+all: xpath pid barcode-reader jar_info zip_info
 	
 .PHONY: all clean
 
@@ -25,7 +25,7 @@
 pid: pid.cpp streamlet-common.h
 	g++ -g -fno-omit-frame-pointer -fsanitize=address $(<) -o $(@)
 
-qr-decode: qr-decode.cpp streamlet-common.h
+barcode-reader: barcode-reader.cpp streamlet-common.h
 	g++ -g -fno-omit-frame-pointer -fsanitize=address $(<) -o $(@) $(shell pkg-config --libs --cflags relpipe-lib-xmlwriter.cpp zbar Magick++)
 
 jar_info: JarInfo.java Streamlet.java
@@ -39,6 +39,7 @@
 clean:
 	rm -f xpath
 	rm -f pid
+	rm -f barcode-reader
 	rm -f jar_info
 	rm -f zip_info
 	rm -f *.class
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/streamlet-examples/barcode-reader.cpp	Sat May 01 18:18:01 2021 +0200
@@ -0,0 +1,214 @@
+/**
+ * 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 three attributes:
+ *  - qr: first QR code found (if any)
+ *  - qr_count: number of QR codes found
+ *  - qr_xml: XML containing all QR cpdes found an additional metadata
+ * 
+ * Options:
+ *  - value-pattern: regular expression describing expected value of QR code;
+ *    if one or more options are given, the "qr" attribute will contain first value matching any of these patterns
+ * 
+ */
+class QRStreamlet : public Streamlet {
+private:
+
+	const wstring XMLNS = L"tag:globalcode.info,2018:barcode";
+
+	class Point {
+	public:
+		int x;
+		int y;
+	};
+
+	class Symbol {
+	public:
+		int id;
+		std::wstring value;
+		std::wstring type;
+		int x;
+		int y;
+		int width;
+		int height;
+		std::vector<Point> polygon;
+	};
+
+	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.polygon.push_back({x, y});
+			}
+
+			s.x = minX;
+			s.y = minY;
+			s.width = maxX - minX;
+			s.height = maxY - minY;
+
+			result.push_back(s);
+		}
+
+
+
+		return result;
+	}
+
+	bool matchesAny(const std::wstring& value, const std::vector<std::wregex>& valuePatterns) {
+		for (const std::wregex& pattern : valuePatterns) {
+			if (std::regex_match(value, pattern)) return true;
+		}
+		return false;
+	}
+
+	std::vector<std::wregex> getOptionsAsPatterns(std::wstring optionName) {
+		std::vector<std::wregex> result;
+		for (Option o : getOptions(optionName)) result.push_back(std::wregex(o.value));
+		return result;
+	}
+
+protected:
+
+	std::vector<AttributeMetadata> getOutputAttributesMetadata() override {
+		std::vector<AttributeMetadata> oam;
+		int i = 0;
+		oam.push_back({getAlias(i++, L"barcode"), STRING});
+		oam.push_back({getAlias(i++, L"barcode_count"), INTEGER});
+		oam.push_back({getAlias(i++, L"barcode_xml"), STRING});
+		return oam;
+	}
+
+	std::vector<OutputAttribute> getOutputAttributes() override {
+		bool matchedFile = false;
+		bool validInput = false;
+		bool matchedFirst = false;
+		std::wstring first;
+		std::stringstream xml;
+
+		std::vector<std::wregex> filePatterns = getOptionsAsPatterns(L"file-pattern");
+		matchedFile = filePatterns.size() == 0 || matchesAny(getCurrentFile(), filePatterns);
+
+		std::vector<Symbol> symbols;
+		if (matchedFile) {
+			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;
+			}
+
+			std::vector<std::wregex> valuePatterns = getOptionsAsPatterns(L"value-pattern");
+
+			for (Symbol s : symbols) {
+				if (valuePatterns.size() == 0 || matchesAny(s.value, valuePatterns)) {
+					first = s.value;
+					matchedFirst = true;
+					break;
+				}
+			}
+
+			relpipe::xmlwriter::XMLWriter xmlWriter(xml);
+			xmlWriter.writeStartElement(L"barcodes-recognized",{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);
+
+				// TODO: well-designed XML schema
+				// TODO: synchronize/share common XML parts with relpipe-in-qr
+				xmlWriter.writeStartElement(L"rectangular-box");
+				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));
+				xmlWriter.writeEndElement();
+
+				xmlWriter.writeStartElement(L"polygon");
+				for (Point p : s.polygon) xmlWriter.writeEmptyElement(L"point",{L"x", std::to_wstring(p.x), L"y", std::to_wstring(p.y)});
+				xmlWriter.writeEndElement();
+
+				xmlWriter.writeEndElement();
+			}
+
+			xmlWriter.writeEndElement();
+
+		}
+
+		std::vector<OutputAttribute> oa;
+		// TODO: report also validInput and matchedFile (distinguish them from matchedFirst)?
+		oa.push_back({first, !matchedFirst});
+		oa.push_back({std::to_wstring(symbols.size()), !matchedFile});
+		oa.push_back({fromBytes(xml.str()), !matchedFile});
+		return oa;
+	}
+};
+
+STREAMLET_RUN(QRStreamlet)
--- a/streamlet-examples/qr-decode.cpp	Sun Apr 25 20:30:53 2021 +0200
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,214 +0,0 @@
-/**
- * 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 three attributes:
- *  - qr: first QR code found (if any)
- *  - qr_count: number of QR codes found
- *  - qr_xml: XML containing all QR cpdes found an additional metadata
- * 
- * Options:
- *  - value-pattern: regular expression describing expected value of QR code;
- *    if one or more options are given, the "qr" attribute will contain first value matching any of these patterns
- * 
- */
-class QRStreamlet : public Streamlet {
-private:
-
-	const wstring XMLNS = L"tag:globalcode.info,2018:qr:FIXME:final-xmlns"; // FIXME: correct xmlns URI
-
-	class Point {
-	public:
-		int x;
-		int y;
-	};
-
-	class Symbol {
-	public:
-		int id;
-		std::wstring value;
-		std::wstring type;
-		int x;
-		int y;
-		int width;
-		int height;
-		std::vector<Point> polygon;
-	};
-
-	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.polygon.push_back({x, y});
-			}
-
-			s.x = minX;
-			s.y = minY;
-			s.width = maxX - minX;
-			s.height = maxY - minY;
-
-			result.push_back(s);
-		}
-
-
-
-		return result;
-	}
-
-	bool matchesAny(const std::wstring& value, const std::vector<std::wregex>& valuePatterns) {
-		for (const std::wregex& pattern : valuePatterns) {
-			if (std::regex_match(value, pattern)) return true;
-		}
-		return false;
-	}
-
-	std::vector<std::wregex> getOptionsAsPatterns(std::wstring optionName) {
-		std::vector<std::wregex> result;
-		for (Option o : getOptions(optionName)) result.push_back(std::wregex(o.value));
-		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_count"), INTEGER});
-		oam.push_back({getAlias(i++, L"qr_xml"), STRING});
-		return oam;
-	}
-
-	std::vector<OutputAttribute> getOutputAttributes() override {
-		bool matchedFile = false;
-		bool validInput = false;
-		bool matchedFirst = false;
-		std::wstring first;
-		std::stringstream xml;
-
-		std::vector<std::wregex> filePatterns = getOptionsAsPatterns(L"file-pattern");
-		matchedFile = filePatterns.size() == 0 || matchesAny(getCurrentFile(), filePatterns);
-
-		std::vector<Symbol> symbols;
-		if (matchedFile) {
-			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;
-			}
-
-			std::vector<std::wregex> valuePatterns = getOptionsAsPatterns(L"value-pattern");
-
-			for (Symbol s : symbols) {
-				if (valuePatterns.size() == 0 || matchesAny(s.value, valuePatterns)) {
-					first = s.value;
-					matchedFirst = 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);
-
-				// TODO: well-designed XML schema
-				// TODO: synchronize/share common XML parts with relpipe-in-qr
-				xmlWriter.writeStartElement(L"rectangular-box");
-				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));
-				xmlWriter.writeEndElement();
-
-				xmlWriter.writeStartElement(L"polygon");
-				for (Point p : s.polygon) xmlWriter.writeEmptyElement(L"point",{L"x", std::to_wstring(p.x), L"y", std::to_wstring(p.y)});
-				xmlWriter.writeEndElement();
-
-				xmlWriter.writeEndElement();
-			}
-
-			xmlWriter.writeEndElement();
-
-		}
-
-		std::vector<OutputAttribute> oa;
-		// TODO: report also validInput and matchedFile (distinguish them from matchedFirst)?
-		oa.push_back({first, !matchedFirst});
-		oa.push_back({std::to_wstring(symbols.size()), !matchedFile});
-		oa.push_back({fromBytes(xml.str()), !matchedFile});
-		return oa;
-	}
-};
-
-STREAMLET_RUN(QRStreamlet)