streamlet-examples/qr-decode.cpp
branchv_0
changeset 92 cf4971342380
parent 91 cb1adcd17d0c
child 93 979848ccdc9c
equal deleted inserted replaced
91:cb1adcd17d0c 92:cf4971342380
     1 /**
       
     2  * Relational pipes
       
     3  * Copyright © 2020 František Kučera (Frantovo.cz, GlobalCode.info)
       
     4  *
       
     5  * This program is free software: you can redistribute it and/or modify
       
     6  * it under the terms of the GNU General Public License as published by
       
     7  * the Free Software Foundation, version 3 of the License.
       
     8  *
       
     9  * This program is distributed in the hope that it will be useful,
       
    10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
       
    11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
       
    12  * GNU General Public License for more details.
       
    13  *
       
    14  * You should have received a copy of the GNU General Public License
       
    15  * along with this program. If not, see <http://www.gnu.org/licenses/>.
       
    16  */
       
    17 
       
    18 #include <unistd.h>
       
    19 
       
    20 #include <Magick++.h>
       
    21 #include <zbar.h>
       
    22 
       
    23 #include <relpipe/xmlwriter/XMLWriter.h>
       
    24 
       
    25 #include "streamlet-common.h"
       
    26 
       
    27 /**
       
    28  * This streamlet extracts QR codes from image files.
       
    29  * 
       
    30  * It provides three attributes:
       
    31  *  - qr: first QR code found (if any)
       
    32  *  - qr_count: number of QR codes found
       
    33  *  - qr_xml: XML containing all QR cpdes found an additional metadata
       
    34  * 
       
    35  * Options:
       
    36  *  - value-pattern: regular expression describing expected value of QR code;
       
    37  *    if one or more options are given, the "qr" attribute will contain first value matching any of these patterns
       
    38  * 
       
    39  */
       
    40 class QRStreamlet : public Streamlet {
       
    41 private:
       
    42 
       
    43 	const wstring XMLNS = L"tag:globalcode.info,2018:qr:FIXME:final-xmlns"; // FIXME: correct xmlns URI
       
    44 
       
    45 	class Point {
       
    46 	public:
       
    47 		int x;
       
    48 		int y;
       
    49 	};
       
    50 
       
    51 	class Symbol {
       
    52 	public:
       
    53 		int id;
       
    54 		std::wstring value;
       
    55 		std::wstring type;
       
    56 		int x;
       
    57 		int y;
       
    58 		int width;
       
    59 		int height;
       
    60 		std::vector<Point> polygon;
       
    61 	};
       
    62 
       
    63 	std::vector<Symbol> findSymbols(std::wstring fileName) {
       
    64 		std::vector<Symbol> result;
       
    65 
       
    66 		Magick::Image magick(toBytes(fileName));
       
    67 		int width = magick.columns();
       
    68 		int height = magick.rows();
       
    69 		Magick::Blob blob;
       
    70 		magick.modifyImage();
       
    71 		magick.write(&blob, "GRAY", 8);
       
    72 		const void *raw = blob.data();
       
    73 
       
    74 		zbar::Image image(width, height, "Y800", raw, width * height);
       
    75 		zbar::ImageScanner scanner;
       
    76 
       
    77 		scanner.scan(image);
       
    78 
       
    79 		int id = 0;
       
    80 		for (zbar::Image::SymbolIterator symbol = image.symbol_begin(); symbol != image.symbol_end(); ++symbol, id++) {
       
    81 			Symbol s;
       
    82 
       
    83 
       
    84 			s.id = id;
       
    85 			s.type = fromBytes(symbol->get_type_name());
       
    86 			s.value = fromBytes(symbol->get_data());
       
    87 
       
    88 			int minX = 0;
       
    89 			int minY = 0;
       
    90 			int maxX = 0;
       
    91 			int maxY = 0;
       
    92 
       
    93 			// TODO: return original polygon in XML
       
    94 			for (int i = 0, locationSize = symbol->get_location_size(); i < locationSize; i++) {
       
    95 				int x = symbol->get_location_x(i);
       
    96 				int y = symbol->get_location_y(i);
       
    97 				minX = minX ? std::min(minX, x) : x;
       
    98 				minY = minY ? std::min(minY, y) : y;
       
    99 				maxX = std::max(maxX, x);
       
   100 				maxY = std::max(maxY, y);
       
   101 				s.polygon.push_back({x, y});
       
   102 			}
       
   103 
       
   104 			s.x = minX;
       
   105 			s.y = minY;
       
   106 			s.width = maxX - minX;
       
   107 			s.height = maxY - minY;
       
   108 
       
   109 			result.push_back(s);
       
   110 		}
       
   111 
       
   112 
       
   113 
       
   114 		return result;
       
   115 	}
       
   116 
       
   117 	bool matchesAny(const std::wstring& value, const std::vector<std::wregex>& valuePatterns) {
       
   118 		for (const std::wregex& pattern : valuePatterns) {
       
   119 			if (std::regex_match(value, pattern)) return true;
       
   120 		}
       
   121 		return false;
       
   122 	}
       
   123 
       
   124 	std::vector<std::wregex> getOptionsAsPatterns(std::wstring optionName) {
       
   125 		std::vector<std::wregex> result;
       
   126 		for (Option o : getOptions(optionName)) result.push_back(std::wregex(o.value));
       
   127 		return result;
       
   128 	}
       
   129 
       
   130 protected:
       
   131 
       
   132 	std::vector<AttributeMetadata> getOutputAttributesMetadata() override {
       
   133 		std::vector<AttributeMetadata> oam;
       
   134 		int i = 0;
       
   135 		oam.push_back({getAlias(i++, L"qr"), STRING});
       
   136 		oam.push_back({getAlias(i++, L"qr_count"), INTEGER});
       
   137 		oam.push_back({getAlias(i++, L"qr_xml"), STRING});
       
   138 		return oam;
       
   139 	}
       
   140 
       
   141 	std::vector<OutputAttribute> getOutputAttributes() override {
       
   142 		bool matchedFile = false;
       
   143 		bool validInput = false;
       
   144 		bool matchedFirst = false;
       
   145 		std::wstring first;
       
   146 		std::stringstream xml;
       
   147 
       
   148 		std::vector<std::wregex> filePatterns = getOptionsAsPatterns(L"file-pattern");
       
   149 		matchedFile = filePatterns.size() == 0 || matchesAny(getCurrentFile(), filePatterns);
       
   150 
       
   151 		std::vector<Symbol> symbols;
       
   152 		if (matchedFile) {
       
   153 			try {
       
   154 				symbols = findSymbols(getCurrentFile());
       
   155 				validInput = true;
       
   156 			} catch (...) {
       
   157 				// just ignore the errors;
       
   158 				// the file is probably not an image or we do not have read permissions
       
   159 				validInput = false;
       
   160 			}
       
   161 
       
   162 			std::vector<std::wregex> valuePatterns = getOptionsAsPatterns(L"value-pattern");
       
   163 
       
   164 			for (Symbol s : symbols) {
       
   165 				if (valuePatterns.size() == 0 || matchesAny(s.value, valuePatterns)) {
       
   166 					first = s.value;
       
   167 					matchedFirst = true;
       
   168 					break;
       
   169 				}
       
   170 			}
       
   171 
       
   172 			relpipe::xmlwriter::XMLWriter xmlWriter(xml);
       
   173 			xmlWriter.writeStartElement(L"qr",{L"xmlns", XMLNS});
       
   174 
       
   175 			// TODO: common metadata
       
   176 			// xmlWriter.writeStartElement(L"source");
       
   177 			// xmlWriter.writeTextElement(L"height",{}, std::to_wstring(...));
       
   178 			// xmlWriter.writeTextElement(L"width",{}, std::to_wstring(...));
       
   179 			// xmlWriter.writeEndElement();
       
   180 
       
   181 			for (Symbol s : symbols) {
       
   182 				xmlWriter.writeStartElement(L"symbol");
       
   183 				xmlWriter.writeTextElement(L"value",{}, s.value);
       
   184 
       
   185 				// TODO: well-designed XML schema
       
   186 				// TODO: synchronize/share common XML parts with relpipe-in-qr
       
   187 				xmlWriter.writeStartElement(L"rectangular-box");
       
   188 				xmlWriter.writeTextElement(L"x",{}, std::to_wstring(s.x));
       
   189 				xmlWriter.writeTextElement(L"y",{}, std::to_wstring(s.y));
       
   190 				xmlWriter.writeTextElement(L"height",{}, std::to_wstring(s.height));
       
   191 				xmlWriter.writeTextElement(L"width",{}, std::to_wstring(s.width));
       
   192 				xmlWriter.writeEndElement();
       
   193 
       
   194 				xmlWriter.writeStartElement(L"polygon");
       
   195 				for (Point p : s.polygon) xmlWriter.writeEmptyElement(L"point",{L"x", std::to_wstring(p.x), L"y", std::to_wstring(p.y)});
       
   196 				xmlWriter.writeEndElement();
       
   197 
       
   198 				xmlWriter.writeEndElement();
       
   199 			}
       
   200 
       
   201 			xmlWriter.writeEndElement();
       
   202 
       
   203 		}
       
   204 
       
   205 		std::vector<OutputAttribute> oa;
       
   206 		// TODO: report also validInput and matchedFile (distinguish them from matchedFirst)?
       
   207 		oa.push_back({first, !matchedFirst});
       
   208 		oa.push_back({std::to_wstring(symbols.size()), !matchedFile});
       
   209 		oa.push_back({fromBytes(xml.str()), !matchedFile});
       
   210 		return oa;
       
   211 	}
       
   212 };
       
   213 
       
   214 STREAMLET_RUN(QRStreamlet)