src/BarcodeCommand.h
branchv_0
changeset 5 8f434dc38444
parent 4 500ce0b934e7
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/BarcodeCommand.h	Sat May 01 17:59:40 2021 +0200
@@ -0,0 +1,129 @@
+/**
+ * Relational pipes
+ * Copyright © 2021 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/>.
+ */
+#pragma once
+
+#include <codecvt>
+#include <memory>
+#include <algorithm>
+#include <iostream>
+#include <stdexcept>
+
+#include <Magick++.h>
+#include <zbar.h>
+
+#include "Configuration.h"
+
+namespace relpipe {
+namespace in {
+namespace barcode {
+
+class BarcodeCommand {
+private:
+	std::wstring_convert<codecvt_utf8<wchar_t>> convertor; // TODO: use platform encoding as default / check zbar encoding
+
+
+public:
+
+	void process(Configuration& configuration, std::shared_ptr<writer::RelationalWriter> writer, std::function<void() > relationalWriterFlush) {
+
+		// TODO: require PNM input and get rid off the ImageMagick dependency?
+		Magick::Image magick("/dev/stdin");
+		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;
+
+		writer->startRelation(L"symbol",{
+			{L"id", relpipe::writer::TypeId::INTEGER},
+			{L"type", relpipe::writer::TypeId::STRING},
+			// {L"addon_name", relpipe::writer::TypeId::STRING},
+			{L"value", relpipe::writer::TypeId::STRING},
+			// {L"xml", relpipe::writer::TypeId::STRING},
+			{L"x", relpipe::writer::TypeId::INTEGER},
+			{L"y", relpipe::writer::TypeId::INTEGER},
+			{L"width", relpipe::writer::TypeId::INTEGER},
+			{L"height", relpipe::writer::TypeId::INTEGER},
+		}, true);
+
+		int n = scanner.scan(image);
+
+		relpipe::common::type::Integer id = 0;
+		for (zbar::Image::SymbolIterator symbol = image.symbol_begin(); symbol != image.symbol_end(); ++symbol, id++) {
+			writer->writeAttribute(&id, typeid (id));
+			writer->writeAttribute(convertor.from_bytes(symbol->get_type_name()));
+			// writer->writeAttribute(convertor.from_bytes(symbol->get_addon_name()));
+			writer->writeAttribute(convertor.from_bytes(symbol->get_data()));
+			// writer->writeAttribute(convertor.from_bytes(symbol->xml()));
+
+			relpipe::common::type::Integer minX = 0;
+			relpipe::common::type::Integer minY = 0;
+			relpipe::common::type::Integer maxX = 0;
+			relpipe::common::type::Integer maxY = 0;
+
+			// TODO: return original polygon in XML
+			for (int i = 0, locationSize = symbol->get_location_size(); i < locationSize; i++) {
+				relpipe::common::type::Integer x = symbol->get_location_x(i);
+				relpipe::common::type::Integer 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);
+			}
+
+			relpipe::common::type::Integer width = maxX - minX;
+			relpipe::common::type::Integer height = maxY - minY;
+
+			writer->writeAttribute(&minX, typeid (minX));
+			writer->writeAttribute(&minY, typeid (minY));
+			writer->writeAttribute(&width, typeid (width));
+			writer->writeAttribute(&height, typeid (height));
+		}
+
+		if (configuration.listPolygonPoints) {
+			relpipe::common::type::Integer id = 0;
+
+			writer->startRelation(L"polygon_point",{
+				{L"symbol", relpipe::writer::TypeId::INTEGER},
+				{L"point", relpipe::writer::TypeId::INTEGER},
+				{L"x", relpipe::writer::TypeId::INTEGER},
+				{L"y", relpipe::writer::TypeId::INTEGER},
+			}, true);
+
+			for (zbar::Image::SymbolIterator symbol = image.symbol_begin(); symbol != image.symbol_end(); ++symbol, id++) {
+				for (relpipe::common::type::Integer i = 0, locationSize = symbol->get_location_size(); i < locationSize; i++) {
+					relpipe::common::type::Integer x = symbol->get_location_x(i);
+					relpipe::common::type::Integer y = symbol->get_location_y(i);
+					writer->writeAttribute(&id, typeid (id));
+					writer->writeAttribute(&i, typeid (i));
+					writer->writeAttribute(&x, typeid (x));
+					writer->writeAttribute(&y, typeid (y));
+				}
+			}
+		}
+
+		image.set_data(nullptr, 0);
+	}
+};
+
+}
+}
+}