src/QRCommand.h
author František Kučera <franta-hg@frantovo.cz>
Wed, 14 Apr 2021 20:56:18 +0200
branchv_0
changeset 1 79d699ed88ab
parent 0 0a0cb749c10f
child 2 6cc693048318
permissions -rw-r--r--
dirty and buggy first version
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 {
1
79d699ed88ab dirty and buggy first version
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    35
	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
    36
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
public:
0a0cb749c10f projekt skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    39
0a0cb749c10f projekt skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    40
	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
    41
1
79d699ed88ab dirty and buggy first version
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    42
		Magick::Image magick("/dev/stdin");
79d699ed88ab dirty and buggy first version
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    43
		int width = magick.columns();
79d699ed88ab dirty and buggy first version
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    44
		int height = magick.rows();
79d699ed88ab dirty and buggy first version
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    45
		Magick::Blob blob;
79d699ed88ab dirty and buggy first version
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    46
		magick.modifyImage();
79d699ed88ab dirty and buggy first version
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    47
		magick.write(&blob, "GRAY", 8);
79d699ed88ab dirty and buggy first version
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    48
		const void *raw = blob.data();
79d699ed88ab dirty and buggy first version
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    49
79d699ed88ab dirty and buggy first version
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    50
		zbar::Image image(width, height, "Y800", raw, width * height);
0
0a0cb749c10f projekt skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    51
		zbar::ImageScanner scanner;
0a0cb749c10f projekt skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    52
0a0cb749c10f projekt skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    53
		writer->startRelation(L"qr",{
1
79d699ed88ab dirty and buggy first version
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    54
			{L"type", relpipe::writer::TypeId::STRING},
79d699ed88ab dirty and buggy first version
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    55
			// {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
    56
			{L"value", relpipe::writer::TypeId::STRING},
79d699ed88ab dirty and buggy first version
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    57
			// {L"xml", relpipe::writer::TypeId::STRING},
79d699ed88ab dirty and buggy first version
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    58
			{L"location_size", relpipe::writer::TypeId::INTEGER},
79d699ed88ab dirty and buggy first version
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    59
			{L"x_1", relpipe::writer::TypeId::INTEGER},
79d699ed88ab dirty and buggy first version
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    60
			{L"y_1", relpipe::writer::TypeId::INTEGER},
79d699ed88ab dirty and buggy first version
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    61
			{L"x_2", relpipe::writer::TypeId::INTEGER},
79d699ed88ab dirty and buggy first version
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    62
			{L"y_2", relpipe::writer::TypeId::INTEGER},
79d699ed88ab dirty and buggy first version
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    63
			{L"x_3", relpipe::writer::TypeId::INTEGER},
79d699ed88ab dirty and buggy first version
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    64
			{L"y_3", relpipe::writer::TypeId::INTEGER},
79d699ed88ab dirty and buggy first version
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    65
			{L"x_4", relpipe::writer::TypeId::INTEGER},
79d699ed88ab dirty and buggy first version
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    66
			{L"y_4", relpipe::writer::TypeId::INTEGER},
0
0a0cb749c10f projekt skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    67
		}, true);
0a0cb749c10f projekt skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    68
1
79d699ed88ab dirty and buggy first version
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    69
		int n = scanner.scan(image);
79d699ed88ab dirty and buggy first version
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    70
79d699ed88ab dirty and buggy first version
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    71
		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
    72
			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
    73
			// 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
    74
			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
    75
			// 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
    76
79d699ed88ab dirty and buggy first version
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    77
			relpipe::common::type::Integer locationSize = symbol->get_location_size();
79d699ed88ab dirty and buggy first version
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    78
			writer->writeAttribute(&locationSize, typeid (locationSize));
79d699ed88ab dirty and buggy first version
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    79
			
79d699ed88ab dirty and buggy first version
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    80
			for (int i = 0; i < 4; i++) {
79d699ed88ab dirty and buggy first version
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    81
				relpipe::common::type::Integer x = -5;
79d699ed88ab dirty and buggy first version
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    82
				relpipe::common::type::Integer y = -6;
79d699ed88ab dirty and buggy first version
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    83
				if (i < locationSize) {
79d699ed88ab dirty and buggy first version
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    84
					x = symbol->get_location_x(i);
79d699ed88ab dirty and buggy first version
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    85
					y = symbol->get_location_y(i);
79d699ed88ab dirty and buggy first version
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    86
				}
79d699ed88ab dirty and buggy first version
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    87
				writer->writeAttribute(&x, typeid (x));
79d699ed88ab dirty and buggy first version
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    88
				writer->writeAttribute(&y, typeid (y));
79d699ed88ab dirty and buggy first version
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    89
			}
0
0a0cb749c10f projekt skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    90
0a0cb749c10f projekt skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    91
1
79d699ed88ab dirty and buggy first version
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    92
		}
79d699ed88ab dirty and buggy first version
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    93
79d699ed88ab dirty and buggy first version
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    94
		image.set_data(nullptr, 0);
0
0a0cb749c10f projekt skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    95
	}
0a0cb749c10f projekt skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    96
};
0a0cb749c10f projekt skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    97
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
}