# HG changeset patch # User František Kučera # Date 1619369277 -7200 # Node ID 25a11859975bbf648e31286d974d50d6ae131f88 # Parent 8eb799bf1d3964b9064080af5f98882d1e4b0b85 streamlet examples: QR: rename qr to qr-decode + simplify Makefile diff -r 8eb799bf1d39 -r 25a11859975b .hgignore --- a/.hgignore Sat Apr 24 20:22:59 2021 +0200 +++ b/.hgignore Sun Apr 25 18:47:57 2021 +0200 @@ -13,4 +13,4 @@ ^build/ ^nbproject/private/ -^streamlet-examples/(xpath|pid|qr|jar_info|zip_info)$ +^streamlet-examples/(xpath|pid|qr-decode|jar_info|zip_info)$ diff -r 8eb799bf1d39 -r 25a11859975b streamlet-examples/Makefile --- a/streamlet-examples/Makefile Sat Apr 24 20:22:59 2021 +0200 +++ b/streamlet-examples/Makefile Sun Apr 25 18:47:57 2021 +0200 @@ -15,26 +15,26 @@ # You should have received a copy of the GNU General Public License # along with this program. If not, see . -all: xpath pid qr jar_info zip_info +all: xpath pid qr-decode jar_info zip_info .PHONY: all clean -xpath: streamlet-common.h xpath.cpp - g++ -g -fno-omit-frame-pointer -fsanitize=address xpath.cpp -o xpath $(shell pkg-config --libs --cflags libxml++-2.6) +xpath: xpath.cpp streamlet-common.h + g++ -g -fno-omit-frame-pointer -fsanitize=address $(<) -o $(@) $(shell pkg-config --libs --cflags libxml++-2.6) -pid: streamlet-common.h pid.cpp - g++ -g -fno-omit-frame-pointer -fsanitize=address pid.cpp -o pid +pid: pid.cpp streamlet-common.h + g++ -g -fno-omit-frame-pointer -fsanitize=address $(<) -o $(@) -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++) +qr-decode: qr-decode.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: Streamlet.java JarInfo.java - javac JarInfo.java +jar_info: JarInfo.java Streamlet.java + javac $(<) jar cfe jar_info JarInfo Streamlet*.class JarInfo.class - chmod +x jar_info + chmod +x $(@) zip_info: jar_info - ln -s jar_info zip_info + ln -s jar_info $(@) clean: rm -f xpath diff -r 8eb799bf1d39 -r 25a11859975b streamlet-examples/qr-decode.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/streamlet-examples/qr-decode.cpp Sun Apr 25 18:47:57 2021 +0200 @@ -0,0 +1,187 @@ +/** + * 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 . + */ + +#include + +#include +#include + +#include + +#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 + * + */ +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 polygon; + }; + + std::vector findSymbols(std::wstring fileName) { + std::vector 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; + } + +protected: + + std::vector getOutputAttributesMetadata() override { + std::vector 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 getOutputAttributes() override { + bool validInput = false; + bool hasSymbols = false; + std::wstring first; + std::stringstream xml; + + std::vector 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); + + // 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 oa; + // TODO: report also validInput (distinguish it from hasSymbols) + oa.push_back({first, !hasSymbols}); + oa.push_back({std::to_wstring(symbols.size()), false}); + oa.push_back({fromBytes(xml.str()), false}); + return oa; + } +}; + +STREAMLET_RUN(QRStreamlet) diff -r 8eb799bf1d39 -r 25a11859975b streamlet-examples/qr.cpp --- a/streamlet-examples/qr.cpp Sat Apr 24 20:22:59 2021 +0200 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,187 +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 . - */ - -#include - -#include -#include - -#include - -#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 - * - */ -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 polygon; - }; - - std::vector findSymbols(std::wstring fileName) { - std::vector 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; - } - -protected: - - std::vector getOutputAttributesMetadata() override { - std::vector 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 getOutputAttributes() override { - bool validInput = false; - bool hasSymbols = false; - std::wstring first; - std::stringstream xml; - - std::vector 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); - - // 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 oa; - // TODO: report also validInput (distinguish it from hasSymbols) - oa.push_back({first, !hasSymbols}); - oa.push_back({std::to_wstring(symbols.size()), false}); - oa.push_back({fromBytes(xml.str()), false}); - return oa; - } -}; - -STREAMLET_RUN(QRStreamlet)