author | František Kučera <franta-hg@frantovo.cz> |
Fri, 16 Apr 2021 23:18:03 +0200 | |
branch | v_0 |
changeset 4 | 500ce0b934e7 |
parent 2 | 6cc693048318 |
permissions | -rw-r--r-- |
0 | 1 |
/** |
2 |
* Relational pipes |
|
3 |
* Copyright © 2021 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 |
#pragma once |
|
18 |
||
19 |
#include <codecvt> |
|
20 |
#include <memory> |
|
21 |
#include <algorithm> |
|
22 |
#include <iostream> |
|
23 |
#include <stdexcept> |
|
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 | 26 |
#include <zbar.h> |
27 |
||
28 |
#include "Configuration.h" |
|
29 |
||
30 |
namespace relpipe { |
|
31 |
namespace in { |
|
32 |
namespace qr { |
|
33 |
||
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 | 37 |
|
38 |
||
39 |
public: |
|
40 |
||
41 |
void process(Configuration& configuration, std::shared_ptr<writer::RelationalWriter> writer, std::function<void() > relationalWriterFlush) { |
|
42 |
||
4
500ce0b934e7
optional listing of polygon points (more accurate than rectangular box, but requires separate relation): --list-polygon-points
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
43 |
// TODO: require PNM input and get rid off the ImageMagick dependency? |
1
79d699ed88ab
dirty and buggy first version
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
44 |
Magick::Image magick("/dev/stdin"); |
79d699ed88ab
dirty and buggy first version
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
45 |
int width = magick.columns(); |
79d699ed88ab
dirty and buggy first version
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
46 |
int height = magick.rows(); |
79d699ed88ab
dirty and buggy first version
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
47 |
Magick::Blob blob; |
79d699ed88ab
dirty and buggy first version
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
48 |
magick.modifyImage(); |
79d699ed88ab
dirty and buggy first version
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
49 |
magick.write(&blob, "GRAY", 8); |
79d699ed88ab
dirty and buggy first version
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
50 |
const void *raw = blob.data(); |
79d699ed88ab
dirty and buggy first version
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
51 |
|
79d699ed88ab
dirty and buggy first version
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
52 |
zbar::Image image(width, height, "Y800", raw, width * height); |
0 | 53 |
zbar::ImageScanner scanner; |
54 |
||
4
500ce0b934e7
optional listing of polygon points (more accurate than rectangular box, but requires separate relation): --list-polygon-points
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
55 |
writer->startRelation(L"symbol",{ |
500ce0b934e7
optional listing of polygon points (more accurate than rectangular box, but requires separate relation): --list-polygon-points
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
56 |
{L"id", relpipe::writer::TypeId::INTEGER}, |
1
79d699ed88ab
dirty and buggy first version
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
57 |
{L"type", relpipe::writer::TypeId::STRING}, |
79d699ed88ab
dirty and buggy first version
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
58 |
// {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
|
59 |
{L"value", relpipe::writer::TypeId::STRING}, |
79d699ed88ab
dirty and buggy first version
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
60 |
// {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
|
61 |
{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
|
62 |
{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
|
63 |
{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
|
64 |
{L"height", relpipe::writer::TypeId::INTEGER}, |
0 | 65 |
}, true); |
66 |
||
1
79d699ed88ab
dirty and buggy first version
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
67 |
int n = scanner.scan(image); |
79d699ed88ab
dirty and buggy first version
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
68 |
|
4
500ce0b934e7
optional listing of polygon points (more accurate than rectangular box, but requires separate relation): --list-polygon-points
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
69 |
relpipe::common::type::Integer id = 0; |
500ce0b934e7
optional listing of polygon points (more accurate than rectangular box, but requires separate relation): --list-polygon-points
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
70 |
for (zbar::Image::SymbolIterator symbol = image.symbol_begin(); symbol != image.symbol_end(); ++symbol, id++) { |
500ce0b934e7
optional listing of polygon points (more accurate than rectangular box, but requires separate relation): --list-polygon-points
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
71 |
writer->writeAttribute(&id, typeid (id)); |
1
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 |
|
2
6cc693048318
simplify arbitrary polygon to a rectangular box
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
77 |
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
|
78 |
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
|
79 |
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
|
80 |
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
|
81 |
|
6cc693048318
simplify arbitrary polygon to a rectangular box
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
82 |
// 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
|
83 |
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
|
84 |
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
|
85 |
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
|
86 |
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
|
87 |
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
|
88 |
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
|
89 |
maxY = std::max(maxY, y); |
1
79d699ed88ab
dirty and buggy first version
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
90 |
} |
0 | 91 |
|
2
6cc693048318
simplify arbitrary polygon to a rectangular box
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
92 |
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
|
93 |
relpipe::common::type::Integer height = maxY - minY; |
0 | 94 |
|
2
6cc693048318
simplify arbitrary polygon to a rectangular box
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
95 |
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
|
96 |
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
|
97 |
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
|
98 |
writer->writeAttribute(&height, typeid (height)); |
1
79d699ed88ab
dirty and buggy first version
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
99 |
} |
79d699ed88ab
dirty and buggy first version
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
100 |
|
4
500ce0b934e7
optional listing of polygon points (more accurate than rectangular box, but requires separate relation): --list-polygon-points
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
101 |
if (configuration.listPolygonPoints) { |
500ce0b934e7
optional listing of polygon points (more accurate than rectangular box, but requires separate relation): --list-polygon-points
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
102 |
relpipe::common::type::Integer id = 0; |
500ce0b934e7
optional listing of polygon points (more accurate than rectangular box, but requires separate relation): --list-polygon-points
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
103 |
|
500ce0b934e7
optional listing of polygon points (more accurate than rectangular box, but requires separate relation): --list-polygon-points
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
104 |
writer->startRelation(L"polygon_point",{ |
500ce0b934e7
optional listing of polygon points (more accurate than rectangular box, but requires separate relation): --list-polygon-points
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
105 |
{L"symbol", relpipe::writer::TypeId::INTEGER}, |
500ce0b934e7
optional listing of polygon points (more accurate than rectangular box, but requires separate relation): --list-polygon-points
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
106 |
{L"point", relpipe::writer::TypeId::INTEGER}, |
500ce0b934e7
optional listing of polygon points (more accurate than rectangular box, but requires separate relation): --list-polygon-points
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
107 |
{L"x", relpipe::writer::TypeId::INTEGER}, |
500ce0b934e7
optional listing of polygon points (more accurate than rectangular box, but requires separate relation): --list-polygon-points
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
108 |
{L"y", relpipe::writer::TypeId::INTEGER}, |
500ce0b934e7
optional listing of polygon points (more accurate than rectangular box, but requires separate relation): --list-polygon-points
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
109 |
}, true); |
500ce0b934e7
optional listing of polygon points (more accurate than rectangular box, but requires separate relation): --list-polygon-points
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
110 |
|
500ce0b934e7
optional listing of polygon points (more accurate than rectangular box, but requires separate relation): --list-polygon-points
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
111 |
for (zbar::Image::SymbolIterator symbol = image.symbol_begin(); symbol != image.symbol_end(); ++symbol, id++) { |
500ce0b934e7
optional listing of polygon points (more accurate than rectangular box, but requires separate relation): --list-polygon-points
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
112 |
for (relpipe::common::type::Integer i = 0, locationSize = symbol->get_location_size(); i < locationSize; i++) { |
500ce0b934e7
optional listing of polygon points (more accurate than rectangular box, but requires separate relation): --list-polygon-points
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
113 |
relpipe::common::type::Integer x = symbol->get_location_x(i); |
500ce0b934e7
optional listing of polygon points (more accurate than rectangular box, but requires separate relation): --list-polygon-points
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
114 |
relpipe::common::type::Integer y = symbol->get_location_y(i); |
500ce0b934e7
optional listing of polygon points (more accurate than rectangular box, but requires separate relation): --list-polygon-points
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
115 |
writer->writeAttribute(&id, typeid (id)); |
500ce0b934e7
optional listing of polygon points (more accurate than rectangular box, but requires separate relation): --list-polygon-points
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
116 |
writer->writeAttribute(&i, typeid (i)); |
500ce0b934e7
optional listing of polygon points (more accurate than rectangular box, but requires separate relation): --list-polygon-points
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
117 |
writer->writeAttribute(&x, typeid (x)); |
500ce0b934e7
optional listing of polygon points (more accurate than rectangular box, but requires separate relation): --list-polygon-points
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
118 |
writer->writeAttribute(&y, typeid (y)); |
500ce0b934e7
optional listing of polygon points (more accurate than rectangular box, but requires separate relation): --list-polygon-points
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
119 |
} |
500ce0b934e7
optional listing of polygon points (more accurate than rectangular box, but requires separate relation): --list-polygon-points
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
120 |
} |
500ce0b934e7
optional listing of polygon points (more accurate than rectangular box, but requires separate relation): --list-polygon-points
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
121 |
} |
500ce0b934e7
optional listing of polygon points (more accurate than rectangular box, but requires separate relation): --list-polygon-points
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
122 |
|
1
79d699ed88ab
dirty and buggy first version
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
123 |
image.set_data(nullptr, 0); |
0 | 124 |
} |
125 |
}; |
|
126 |
||
127 |
} |
|
128 |
} |
|
129 |
} |