src/QRCommand.h
author František Kučera <franta-hg@frantovo.cz>
Wed, 14 Apr 2021 22:02:23 +0200
branchv_0
changeset 2 6cc693048318
parent 1 79d699ed88ab
child 4 500ce0b934e7
permissions -rw-r--r--
simplify arbitrary polygon to a rectangular box
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
0
0a0cb749c10f projekt skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     1
/**
0a0cb749c10f projekt skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     2
 * Relational pipes
0a0cb749c10f projekt skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     3
 * Copyright © 2021 František Kučera (Frantovo.cz, GlobalCode.info)
0a0cb749c10f projekt skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     4
 *
0a0cb749c10f projekt skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     5
 * This program is free software: you can redistribute it and/or modify
0a0cb749c10f projekt skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     6
 * it under the terms of the GNU General Public License as published by
0a0cb749c10f projekt skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     7
 * the Free Software Foundation, version 3 of the License.
0a0cb749c10f projekt skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     8
 *
0a0cb749c10f projekt skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     9
 * This program is distributed in the hope that it will be useful,
0a0cb749c10f projekt skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
0a0cb749c10f projekt skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
0a0cb749c10f projekt skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    12
 * GNU General Public License for more details.
0a0cb749c10f projekt skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    13
 *
0a0cb749c10f projekt skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    14
 * You should have received a copy of the GNU General Public License
0a0cb749c10f projekt skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    15
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
0a0cb749c10f projekt skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    16
 */
0a0cb749c10f projekt skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    17
#pragma once
0a0cb749c10f projekt skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    18
0a0cb749c10f projekt skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    19
#include <codecvt>
0a0cb749c10f projekt skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    20
#include <memory>
0a0cb749c10f projekt skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    21
#include <algorithm>
0a0cb749c10f projekt skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    22
#include <iostream>
0a0cb749c10f projekt skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    23
#include <stdexcept>
0a0cb749c10f projekt skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    24
1
79d699ed88ab dirty and buggy first version
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    25
#include <Magick++.h>
0
0a0cb749c10f projekt skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    26
#include <zbar.h>
0a0cb749c10f projekt skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    27
0a0cb749c10f projekt skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    28
#include "Configuration.h"
0a0cb749c10f projekt skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    29
0a0cb749c10f projekt skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    30
namespace relpipe {
0a0cb749c10f projekt skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    31
namespace in {
0a0cb749c10f projekt skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    32
namespace qr {
0a0cb749c10f projekt skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    33
0a0cb749c10f projekt skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    34
class QRCommand {
2
6cc693048318 simplify arbitrary polygon to a rectangular box
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
    35
private:
1
79d699ed88ab dirty and buggy first version
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    36
	std::wstring_convert<codecvt_utf8<wchar_t>> convertor; // TODO: use platform encoding as default / check zbar encoding
0
0a0cb749c10f projekt skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    37
0a0cb749c10f projekt skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    38
0a0cb749c10f projekt skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    39
public:
0a0cb749c10f projekt skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    40
0a0cb749c10f projekt skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    41
	void process(Configuration& configuration, std::shared_ptr<writer::RelationalWriter> writer, std::function<void() > relationalWriterFlush) {
0a0cb749c10f projekt skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    42
1
79d699ed88ab dirty and buggy first version
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    43
		Magick::Image magick("/dev/stdin");
79d699ed88ab dirty and buggy first version
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    44
		int width = magick.columns();
79d699ed88ab dirty and buggy first version
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    45
		int height = magick.rows();
79d699ed88ab dirty and buggy first version
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    46
		Magick::Blob blob;
79d699ed88ab dirty and buggy first version
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    47
		magick.modifyImage();
79d699ed88ab dirty and buggy first version
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    48
		magick.write(&blob, "GRAY", 8);
79d699ed88ab dirty and buggy first version
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    49
		const void *raw = blob.data();
79d699ed88ab dirty and buggy first version
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    50
79d699ed88ab dirty and buggy first version
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    51
		zbar::Image image(width, height, "Y800", raw, width * height);
0
0a0cb749c10f projekt skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    52
		zbar::ImageScanner scanner;
0a0cb749c10f projekt skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    53
0a0cb749c10f projekt skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    54
		writer->startRelation(L"qr",{
1
79d699ed88ab dirty and buggy first version
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    55
			{L"type", relpipe::writer::TypeId::STRING},
79d699ed88ab dirty and buggy first version
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    56
			// {L"addon_name", relpipe::writer::TypeId::STRING},
79d699ed88ab dirty and buggy first version
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    57
			{L"value", relpipe::writer::TypeId::STRING},
79d699ed88ab dirty and buggy first version
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    58
			// {L"xml", relpipe::writer::TypeId::STRING},
2
6cc693048318 simplify arbitrary polygon to a rectangular box
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
    59
			{L"x", relpipe::writer::TypeId::INTEGER},
6cc693048318 simplify arbitrary polygon to a rectangular box
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
    60
			{L"y", relpipe::writer::TypeId::INTEGER},
6cc693048318 simplify arbitrary polygon to a rectangular box
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
    61
			{L"width", relpipe::writer::TypeId::INTEGER},
6cc693048318 simplify arbitrary polygon to a rectangular box
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
    62
			{L"height", relpipe::writer::TypeId::INTEGER},
0
0a0cb749c10f projekt skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    63
		}, true);
0a0cb749c10f projekt skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    64
1
79d699ed88ab dirty and buggy first version
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    65
		int n = scanner.scan(image);
79d699ed88ab dirty and buggy first version
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    66
79d699ed88ab dirty and buggy first version
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    67
		for (zbar::Image::SymbolIterator symbol = image.symbol_begin(); symbol != image.symbol_end(); ++symbol) {
79d699ed88ab dirty and buggy first version
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    68
			writer->writeAttribute(convertor.from_bytes(symbol->get_type_name()));
79d699ed88ab dirty and buggy first version
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    69
			// writer->writeAttribute(convertor.from_bytes(symbol->get_addon_name()));
79d699ed88ab dirty and buggy first version
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    70
			writer->writeAttribute(convertor.from_bytes(symbol->get_data()));
79d699ed88ab dirty and buggy first version
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    71
			// writer->writeAttribute(convertor.from_bytes(symbol->xml()));
79d699ed88ab dirty and buggy first version
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    72
2
6cc693048318 simplify arbitrary polygon to a rectangular box
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
    73
			relpipe::common::type::Integer minX = 0;
6cc693048318 simplify arbitrary polygon to a rectangular box
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
    74
			relpipe::common::type::Integer minY = 0;
6cc693048318 simplify arbitrary polygon to a rectangular box
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
    75
			relpipe::common::type::Integer maxX = 0;
6cc693048318 simplify arbitrary polygon to a rectangular box
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
    76
			relpipe::common::type::Integer maxY = 0;
6cc693048318 simplify arbitrary polygon to a rectangular box
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
    77
6cc693048318 simplify arbitrary polygon to a rectangular box
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
    78
			// TODO: return original polygon in XML
6cc693048318 simplify arbitrary polygon to a rectangular box
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
    79
			for (int i = 0, locationSize = symbol->get_location_size(); i < locationSize; i++) {
6cc693048318 simplify arbitrary polygon to a rectangular box
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
    80
				relpipe::common::type::Integer x = symbol->get_location_x(i);
6cc693048318 simplify arbitrary polygon to a rectangular box
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
    81
				relpipe::common::type::Integer y = symbol->get_location_y(i);
6cc693048318 simplify arbitrary polygon to a rectangular box
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
    82
				minX = minX ? std::min(minX, x) : x;
6cc693048318 simplify arbitrary polygon to a rectangular box
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
    83
				minY = minY ? std::min(minY, y) : y;
6cc693048318 simplify arbitrary polygon to a rectangular box
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
    84
				maxX = std::max(maxX, x);
6cc693048318 simplify arbitrary polygon to a rectangular box
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
    85
				maxY = std::max(maxY, y);
1
79d699ed88ab dirty and buggy first version
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    86
			}
0
0a0cb749c10f projekt skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    87
2
6cc693048318 simplify arbitrary polygon to a rectangular box
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
    88
			relpipe::common::type::Integer width = maxX - minX;
6cc693048318 simplify arbitrary polygon to a rectangular box
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
    89
			relpipe::common::type::Integer height = maxY - minY;
0
0a0cb749c10f projekt skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    90
2
6cc693048318 simplify arbitrary polygon to a rectangular box
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
    91
			writer->writeAttribute(&minX, typeid (minX));
6cc693048318 simplify arbitrary polygon to a rectangular box
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
    92
			writer->writeAttribute(&minY, typeid (minY));
6cc693048318 simplify arbitrary polygon to a rectangular box
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
    93
			writer->writeAttribute(&width, typeid (width));
6cc693048318 simplify arbitrary polygon to a rectangular box
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
    94
			writer->writeAttribute(&height, typeid (height));
1
79d699ed88ab dirty and buggy first version
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    95
		}
79d699ed88ab dirty and buggy first version
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    96
79d699ed88ab dirty and buggy first version
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    97
		image.set_data(nullptr, 0);
0
0a0cb749c10f projekt skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    98
	}
0a0cb749c10f projekt skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    99
};
0a0cb749c10f projekt skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   100
0a0cb749c10f projekt skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   101
}
0a0cb749c10f projekt skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   102
}
0a0cb749c10f projekt skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   103
}