-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcore.cpp
More file actions
325 lines (271 loc) · 8.66 KB
/
core.cpp
File metadata and controls
325 lines (271 loc) · 8.66 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
#include "core.hpp"
#include "error.hpp"
#include "img.hpp"
#include <cstdint>
#include <iostream>
#include <vector>
#include <cmath>
#define PATTERN_SIZE 7
#define PATTERN_MULT 6
void adjust_for_patterns(int& w, int& h) {
//too small
const int min_pix = (PATTERN_MULT + 2) * PATTERN_SIZE;
if (w < min_pix || h < min_pix) {
w = -1;
return;
}
//remove first pattern temporarily from w & h
w -= PATTERN_SIZE, h -= PATTERN_SIZE;
//round down excess size
const int norm = (PATTERN_MULT + 1) * PATTERN_SIZE;
w /= norm; w *= norm;
h /= norm; h *= norm;
//add back first pattern
w += PATTERN_SIZE, h += PATTERN_SIZE;
}
int get_barcode_capacity(int w, int h) {
const int norm = (PATTERN_MULT + 1) * PATTERN_SIZE;
//get how many timing patterns there are horizontally and vertically
const int seg_x = w / norm + 1;
const int seg_y = h / norm + 1;
//remove them from the total bit capacity and convert to bytes
return (w * h - seg_x * seg_y * PATTERN_SIZE * PATTERN_SIZE) / 8;
}
bool is_pattern(int x, int y) {
const int norm = (PATTERN_MULT + 1) * PATTERN_SIZE;
x %= norm;
y %= norm;
return (x < PATTERN_SIZE && y < PATTERN_SIZE);
}
void draw_pattern(std::vector<bool>& out, int w, int x, int y) {
const int mid = PATTERN_SIZE / 2;
for (int i = 0; i < PATTERN_SIZE; i++)
for (int j = 0; j < PATTERN_SIZE; j++) {
const int d = std::max(abs(i - mid), abs(j - mid));
out[(y+i) * w + x+j] = (d % 2 == 1);
}
}
void draw_patterns(std::vector<bool>& out, int w, int h) {
const int norm = (PATTERN_MULT + 1) * PATTERN_SIZE;
for (int y = 0; y < h; y += norm)
for (int x = 0; x < w; x += norm)
draw_pattern(out, w, x, y);
}
uint32_t cycle_walk(uint32_t x, uint32_t n) {
uint32_t v = n; //get rounded up power of 2 mask
v--;
v |= v >> 1;
v |= v >> 2;
v |= v >> 4;
v |= v >> 8;
v |= v >> 16;
const uint32_t m = 6353;
const uint32_t a = 1;
do {
x = (m * x + a) & v;
} while (x >= n);
return x;
}
int seq_map(const generator_conf& conf, uint32_t seq) {
if (seq / 8 >= conf.total_bytes) return -1;
if (conf.cor == CORRECT_RS) {
//do a multiplicative permutation to spread out bytes
seq = cycle_walk(seq / 8, conf.total_bytes) * 8 + seq % 8;
}
const int norm = (PATTERN_MULT + 1) * PATTERN_SIZE;
const int wide_line = conf.img_w;
const int seg_x = wide_line / norm + 1;
const int thin_line = wide_line - seg_x * PATTERN_SIZE;
const int ppc = (thin_line + wide_line * PATTERN_MULT) * PATTERN_SIZE;
//get "chunks" skipped over in pixels
const int c = seq / ppc * norm * wide_line;
//get offset into chunk
const int o = seq % ppc;
//add back any skipped patterns
const int lines = std::min(o / thin_line, PATTERN_SIZE);
const int i = lines < PATTERN_SIZE ? o % thin_line / (PATTERN_MULT * PATTERN_SIZE) + 1 : 0;
return c + o + (lines * seg_x + i) * PATTERN_SIZE;
}
void init_generator(generator_conf& conf) {
const int border = conf.border * std::min(conf.inch_x, conf.inch_y) / 100;
conf.img_w = (conf.inch_x - border) * conf.ppi / 100;
conf.img_h = (conf.inch_y - border) * conf.ppi / 100;
adjust_for_patterns(conf.img_w, conf.img_h);
if (conf.img_w < 0) {
conf.capacity = -1;
return;
}
int orig_size = conf.img_w * conf.img_h; //used to get efficiency
conf.total_bytes = get_barcode_capacity(conf.img_w, conf.img_h);
//set up EC
correction_used = conf.cor;
checksum_used = conf.cksum;
correction_strength = conf.cor_strength;
correction_data = conf.cor_data;
conf.sector_size = 64;
if (init_error_correction(conf.sector_size)) {
conf.capacity = -1;
return;
}
int sectors = conf.total_bytes / conf.sector_size;
int sec_cap = get_sector_capacity();
conf.capacity = sectors * sec_cap;
conf.efficiency = static_cast<float>(conf.capacity * 8) / orig_size;
}
void stop_generator(generator_conf& conf) {
deinit_error_correction();
}
void generate_barcode(const generator_conf& conf, uint8_t *dat, const std::string& filename) {
//create image data
std::vector<bool> out(conf.img_w * conf.img_h, false);
draw_patterns(out, conf.img_w, conf.img_h);
//encoding loop
uint8_t enc[conf.sector_size];
int index = 0;
const int adv = get_sector_capacity();
for (int i = 0; i < conf.capacity; i += adv) {
encode(&dat[i], enc);
for (int j = 0; j < conf.sector_size * 8; j++) {
int k = seq_map(conf, index++);
if (k < 0) {
std::cerr << "Internal error: out index overflow\n";
return;
}
const int enc_i = j / 8;
const int enc_sh = 7 - j % 8;
out[k] = (enc[enc_i] >> enc_sh) & 1;
}
}
//export to file
pbm_write(out, conf.img_w, conf.img_h, filename);
}
// Barcode decoding code
float get_pix(const std::vector<uint8_t>& img, const int w, const int h, int x, int y) {
if (x < 0 || y < 0 || x >= w || y >= h) return -1;
return static_cast<float>(img[y * w + x]) / 255.;
}
int decode_matrix(const generator_conf& conf, uint8_t *dat, const std::vector<bool>& bits) {
int index = 0;
const int adv = get_sector_capacity();
for (int i = 0; i < conf.capacity; i += adv) {
uint8_t sec[conf.sector_size];
for (int j = 0; j < conf.sector_size * 8; j++) {
int k = seq_map(conf, index++);
if (k < 0) {
std::cerr << "Internal error: index overflow\n";
return -1;
}
const int enc_i = j / 8;
const int enc_sh = 7 - j % 8;
if (j % 8 == 0) sec[enc_i] = 0;
sec[enc_i] |= static_cast<uint8_t>(bits[k]) << enc_sh;
}
int errs = decode(sec, &dat[i]);
if (errs < 0) {
std::cout << "Error at sector " << i / adv << '\n';
return -1;
}
if (errs > 0)
std::cout << errs << " errors at sector " << i / adv << '\n';
}
return 0;
}
int denoise(const generator_conf& conf, const std::vector<uint8_t>& in, std::vector<bool>& out, int w, int h, float R) {
int filter_n = (static_cast<int>(std::round(R - 0.25)) - 1) / 2; //round to <= odd
int filter_size = 2 * filter_n + 1;
if (filter_size < 3) {
//TODO threshold to out here
return -1;
}
for (int y = 0; y < h; y++)
for (int x = 0; x < w; x++) {
float score = 0;
const float rbias = 0;
const bool center = std::round(get_pix(in, w, h, x, y) + rbias);
for (int i = 0; i < filter_size; i++)
for (int j = 0; j < filter_size; j++) {
if (i == filter_n && j == filter_n) continue;
const float val = get_pix(in, w, h, x + j - filter_n, y + i - filter_n);
if (val < 0) continue;
const bool v = std::round(val + rbias);
float s = v != center ? 1 : -2;
s /= abs(j - filter_n) + abs(i - filter_n);
score += s;
}
out[y * w + x] = score > 0 ? !center : center;
}
return 0;
}
int find_corners(const generator_conf& conf, float *out, const std::vector<bool>& img, int w, int h, float R) {
return 0;
}
// Main barcode reading function
int read_barcode(const generator_conf& conf, uint8_t *dat, const std::string& filename) {
std::vector<bool> bits;
std::vector<uint8_t> img;
int w, h;
if (pbm_read(bits, w, h, filename) == -1) {
//TODO add .pbm read attempt here with conversion to 0/255
return -1;
}
if (w < conf.img_w || h < conf.img_h) {
std::cerr << "Image resolution is lower than raw barcode resolution\n";
return -1;
}
//estimate barcode scaling factor R
// float R = ((float)w / conf.img_w + (float)h / conf.img_h) * 0.5 * 0.9;
// std::cout << "Approximated R: " << R << '\n';
// //apply noise filter and rounding
// std::vector<bool> img_f(w * h);
// denoise(conf, img, img_f, w, h, R);
// std::vector<uint8_t> out(w * h, 255);
// for (int y = 0; y < h; y++)
// for (int x = 0; x < w; x++)
// out[y * w + x] = img_f[y * w + x] ? 255 : 0;
// const int m = h / 5;
// int prev = -1;
// int start = 0;
// int start_d = -1;
// int running = 0;
// int sum = 0;
// const int line_min = std::round(R * 0.75 * PATTERN_SIZE);
// const int line_max = std::round(R * 1.5 * PATTERN_SIZE);
// const int tol = std::round(R);
// for (int x = 0; x < w; x++) {
// bool drop_line = false;
// int d = -1;
// for (int y = h-1; y >= h - m; y--) {
// if (d < 0 && !img_f[y * w + x]) {
// d = h-1 - y;
// break;
// }
// }
// if (d >= 0 && x < w - 1 && start_d >= 0 && running <= line_max) {
// if (prev < 0) prev = d;
// if (abs(d - start_d) >= tol || abs(d - prev) > tol/2)
// drop_line = true;
// prev = d;
// } else {
// drop_line = true;
// }
// if (drop_line) {
// if (start_d >= 0 && running >= line_min) {
// int line_d = std::round(static_cast<float>(sum) / running);
// for (int i = start; i < x; i++)
// out[(h-1 - line_d) * w + i] = 128;
// }
// //start new line
// prev = -1;
// start = x;
// start_d = d;
// running = 0;
// sum = 0;
// } else {
// running++;
// sum += d;
// }
// }
// pgm_write(out, w, h, "denoise.pgm");
if (decode_matrix(conf, dat, bits) == -1) return -1;
return 0;
}