NeKernel dev
Loading...
Searching...
No Matches
Qr.h
Go to the documentation of this file.
1#ifndef QR_H
2#define QR_H
3
4#include <BootKit/QrPrelude.h>
7#include <BootKit/Support.h>
9#include <NeKit/KernelPanic.h>
11
14
15namespace qr {
16inline uint8_t min_poly = 0b11101, /* Minimal polynomial x^8 + x^4 + x^3 + x^2 + 1 */
17 generator = 0b10; /* Generator of Galois field */
18
20inline uint8_t gf_mul(uint8_t a, uint8_t b) {
21 uint8_t res = 0;
22
23 for (; b; b >>= 1) {
24 if (b & 1) res ^= a;
25 if (a & 0x80)
26 a = (a << 1) ^ min_poly;
27 else
28 a <<= 1;
29 }
30
31 return res;
32}
33
34// Size of Ecc block with respect to level and version. 0 version is for
35// padding.
36constexpr int ECC_CODEWORDS_PER_BLOCK[4][41] = {
37 {0, 7, 10, 15, 20, 26, 18, 20, 24, 30, 18, 20, 24, 26, 30, 22, 24, 28, 30, 28, 28,
38 28, 28, 30, 30, 26, 28, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30},
39 {0, 10, 16, 26, 18, 24, 16, 18, 22, 22, 26, 30, 22, 22, 24, 24, 28, 28, 26, 26, 26,
40 26, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28},
41 {0, 13, 22, 18, 26, 18, 24, 18, 22, 20, 24, 28, 26, 24, 20, 30, 24, 28, 28, 26, 30,
42 28, 30, 30, 30, 30, 28, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30},
43 {0, 17, 28, 22, 16, 22, 28, 26, 26, 24, 28, 24, 28, 22, 24, 24, 30, 28, 28, 26, 28,
44 30, 24, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30},
45};
46
47// Number of Ecc blocks with respect to level and version. 0 version is for
48// padding.
49constexpr int N_ECC_BLOCKS[4][41] = {
50 {0, 1, 1, 1, 1, 1, 2, 2, 2, 2, 4, 4, 4, 4, 4, 6, 6, 6, 6, 7, 8,
51 8, 9, 9, 10, 12, 12, 12, 13, 14, 15, 16, 17, 18, 19, 19, 20, 21, 22, 24, 25},
52 {0, 1, 1, 1, 2, 2, 4, 4, 4, 5, 5, 5, 8, 9, 9, 10, 10, 11, 13, 14, 16,
53 17, 17, 18, 20, 21, 23, 25, 26, 28, 29, 31, 33, 35, 37, 38, 40, 43, 45, 47, 49},
54 {0, 1, 1, 2, 2, 4, 4, 6, 6, 8, 8, 8, 10, 12, 16, 12, 17, 16, 18, 21, 20,
55 23, 23, 25, 27, 29, 34, 34, 35, 38, 40, 43, 45, 48, 51, 53, 56, 59, 62, 65, 68},
56 {0, 1, 1, 2, 4, 4, 4, 5, 6, 8, 8, 11, 11, 16, 16, 18, 16, 19, 21, 25, 25,
57 25, 34, 30, 32, 35, 37, 40, 42, 45, 48, 51, 54, 57, 60, 63, 66, 70, 74, 77, 81},
58};
59
60// Positions of central modules of alignment patterns according to version. 0
61// version is for padding.
62constexpr int ALIGN_POS[41][7] = {
63 {},
64 {0},
65 {6, 18},
66 {6, 22},
67 {6, 26},
68 {6, 30},
69 {6, 34},
70 {6, 22, 38},
71 {6, 24, 42},
72 {6, 26, 46},
73 {6, 28, 50},
74 {6, 30, 54},
75 {6, 32, 58},
76 {6, 34, 62},
77 {6, 26, 46, 66},
78 {6, 26, 48, 70},
79 {6, 26, 50, 74},
80 {6, 30, 54, 78},
81 {6, 30, 56, 82},
82 {6, 30, 58, 86},
83 {6, 34, 62, 90},
84 {6, 28, 50, 72, 94},
85 {6, 26, 50, 74, 98},
86 {6, 30, 54, 78, 102},
87 {6, 28, 54, 80, 106},
88 {6, 32, 58, 84, 110},
89 {6, 30, 58, 86, 114},
90 {6, 34, 62, 90, 118},
91 {6, 26, 50, 74, 98, 122},
92 {6, 30, 54, 78, 102, 126},
93 {6, 26, 52, 78, 104, 130},
94 {6, 30, 56, 82, 108, 134},
95 {6, 34, 60, 86, 112, 138},
96 {6, 30, 58, 86, 114, 142},
97 {6, 34, 62, 90, 118, 146},
98 {6, 30, 54, 78, 102, 126, 150},
99 {6, 24, 50, 76, 102, 128, 154},
100 {6, 28, 54, 80, 106, 132, 158},
101 {6, 32, 58, 84, 110, 136, 162},
102 {6, 26, 54, 82, 110, 138, 166},
103 {6, 30, 58, 86, 114, 142, 170},
104};
105
106// Return n-th bit of arr starting from MSB.
107constexpr uint8_t get_bit_r(uint8_t* arr, int n) {
108 return (arr[n >> 3] >> (7 - (n & 7))) & 1;
109}
110
111// Add up to 16 bits to arr. Data starts from MSB as well as each byte of an
112// array.
113constexpr void add_bits(uint16_t data, int n, uint8_t* arr, size_t& pos) {
114 while (n--) {
115 arr[pos >> 3] |= ((data >> n) & 1) << (7 - (pos & 7));
116 ++pos;
117 }
118}
119
120// Translate char to alphanumeric encoding value,
121constexpr int alphanumeric(char c) {
122 if (c >= '0' && c <= '9') return c - '0';
123
124 if (c >= 'A' && c <= 'Z') return c - 'A' + 10;
125
126 switch (c) {
127 case ' ':
128 return 36;
129 case '$':
130 return 37;
131 case '%':
132 return 38;
133 case '*':
134 return 39;
135 case '+':
136 return 40;
137 case '-':
138 return 41;
139 case '.':
140 return 42;
141 case '/':
142 return 43;
143 case ':':
144 return 44;
145 }
146 return -1;
147}
148
149// Check if string can be encoded in alphanumeric mode.
150constexpr bool is_alphanumeric(const char* str, size_t len) {
151 for (size_t i = 0; i < len; ++i)
152 if (alphanumeric(str[i]) == -1) return false;
153 return true;
154}
155
156// Check if string can be encoded in numeric mode.
157constexpr bool is_numeric(const char* str, size_t len) {
158 for (size_t i = 0; i < len; ++i)
159 if (str[i] < '0' || str[i] > '9') return false;
160 return true;
161}
162
163// Check if string can be encoded in kanji mode.
164constexpr bool is_kanji(const char* str, size_t len) {
165 for (size_t i = 0; i < len; i += 2) {
166 uint16_t val = uint16_t(str[i]) | (uint16_t(str[i + 1]) << 8);
167 if (val < 0x8140 || val > 0xebbf || (val > 0x9ffc && val < 0xe040)) return false;
168 }
169 return true;
170}
171
172// Reed-Solomon Ecc generator polynomial for the given degree.
173constexpr void gf_gen_poly(int degree, uint8_t* poly) {
174 SetMem(poly, 0, degree);
175
176 uint8_t root = poly[degree - 1] = 1;
177
178 for (int i = 0; i < degree; ++i) {
179 for (int j = 0; j < degree - 1; ++j) poly[j] = gf_mul(poly[j], root) ^ poly[j + 1];
180 poly[degree - 1] = gf_mul(poly[degree - 1], root);
181 root = (root << 1) ^ ((root >> 7) * 0x11d);
182 }
183}
184
185// Polynomial division if Galois Field.
186constexpr void gf_poly_div(uint8_t* dividend, size_t len, uint8_t* divisor, int degree,
187 uint8_t* result) {
188 SetMem(result, 0, degree);
189
190 for (size_t i = 0; i < len; ++i) {
191 uint8_t factor = dividend[i] ^ result[0];
192 MoveMem(&result[0], &result[1], degree - 1);
193 result[degree - 1] = 0;
194 for (int j = 0; j < degree; ++j) result[j] ^= gf_mul(divisor[j], factor);
195 }
196}
197
198enum Ecc {
203};
204
211
212// Select appropriate encoding mode for string.
213constexpr Mode select_mode(const char* str, size_t len) {
214 if (is_numeric(str, len)) return M_NUMERIC;
215 if (is_alphanumeric(str, len)) return M_ALPHANUMERIC;
216 if (is_kanji(str, len)) return M_KANJI;
217 return M_BYTE;
218}
219
220// Return size of Character Control Indicator in bits for given version and
221// mode.
222constexpr int cci(int ver, Mode mode) {
223 constexpr int cnt[4][3] = {
224 {10, 12, 14},
225 {9, 11, 13},
226 {8, 16, 16},
227 {8, 10, 12},
228 };
229 if (ver < 10) return cnt[mode][0];
230 if (ver < 27) return cnt[mode][1];
231 return cnt[mode][2];
232}
233
234template <int V>
235struct Qr {
236 private:
237 friend class QrDelegate;
238 bool draw(int x, int y);
239
240 public:
241 constexpr auto side_size() const { return SIDE; }
242
243 bool module(int x, int y);
244 bool encode(const char* str, size_t len, Ecc ecc, int mask = -1);
245
246 private:
247 bool encode_data(const char* data, size_t len, Ecc ecc, uint8_t* out);
248 void encode_ecc(uint8_t* data, Ecc ecc, uint8_t* out);
249
250 void add_data(uint8_t* data, uint8_t* patterns);
251 void add_patterns();
252 void add_version();
253 void add_format(Ecc ecc, int mask);
254 void reserve_patterns(uint8_t* out);
255
256 template <bool Black>
257 void draw_rect(int y, int x, int height, int width, uint8_t* out);
258 template <bool Black>
259 void draw_bound(int y, int x, int height, int width, uint8_t* out);
260
261 template <bool Horizontal>
262 int rule_1_3_score();
263 int penalty_score();
264 int select_mask(Ecc ecc, uint8_t* patterns);
265 void apply_mask(int mask, uint8_t* patterns);
266
267 private:
268 STATIC_PASS(V >= 1 && V <= 40, "invalid version");
269 static constexpr int SIDE = 17 + V * 4;
270 static constexpr int N_BITS = SIDE * SIDE;
271 static constexpr int N_ALIGN = V == 1 ? 0 : V / 7 + 2;
272 static constexpr int N_ALIGN_BITS = V > 1 ? (N_ALIGN* N_ALIGN - 3) * 25 : 0;
273 static constexpr int N_TIMING_BITS = (SIDE - 16) * 2 - (10 * (V > 1 ? N_ALIGN - 2 : 0));
274 static constexpr int N_VER_BITS = V > 6 ? 36 : 0;
275 static constexpr int N_DAT_BITS = N_BITS - (192 + N_ALIGN_BITS + N_TIMING_BITS + 31 + N_VER_BITS);
276 static constexpr int N_BYTES = utl::bytes_in_bits(N_BITS); // Actual number of bytes_in_bits
277 // required to store whole Qr code
278 static constexpr int N_DAT_BYTES =
279 utl::bytes_in_bits(N_DAT_BITS); // Actual number of bytes_in_bits required to store
280 // [data + ecc]
281 static constexpr int N_DAT_CAPACITY =
282 N_DAT_BITS >> 3; // Capacity of [data + ecc] without remainder bits
283 private:
285 uint8_t get_arr_bit(uint8_t* arr, unsigned bit) const { return utl::get_arr_bit(arr, bit); }
286
288 void set_arr_bit(uint8_t* arr, unsigned bit) { utl::set_arr_bit(arr, bit); }
289
291 void clr_arr_bit(uint8_t* arr, unsigned bit) { utl::clr_arr_bit(arr, bit); }
292
293 uint8_t code[N_BYTES] = {};
294
295 bool status = false;
296};
297
298// Get color of a module from left-to-right and top-to-bottom. Black is true.
299template <int V>
300bool Qr<V>::module(int x, int y) {
301 return get_arr_bit(code, y * SIDE + x);
302}
303
305template <int V>
306bool Qr<V>::draw(int whereX, int whereY) {
307 if (!this->status) return false; // it may be invalid.
308
309 cg_init();
310
311 for (int y = 0; y < (this->side_size()); ++y) {
312 for (int x = 0; x < (this->side_size()); ++x) {
313 FBDrawInRegion((this->module(x, y) ? RGB(00, 00, 00) : RGB(0xFF, 0xFF, 0xFF)), 1, 1,
314 x + whereX, y + whereY);
315 }
316 }
317
318 cg_clear();
319
320 return false;
321}
322
323// Create Qr code with given error correction level. If mask == -1,
324// then best mask selected automatically. NOTE: Automatic mask is the
325// most expensive operation. Takes about 95 % of all computation time.
326template <int V>
327bool Qr<V>::encode(const char* str, size_t len, Ecc ecc, int mask) {
328 uint8_t data[N_DAT_BYTES] = {};
329 uint8_t data_with_ecc[N_DAT_BYTES] = {};
330 uint8_t patterns[N_BYTES] = {};
331
332 if (!encode_data(str, len, ecc, data)) {
333 return status = false;
334 }
335
336 encode_ecc(data, ecc, data_with_ecc);
337
338 reserve_patterns(patterns);
339 CopyMem(code, patterns, N_BYTES);
340
341 add_data(data_with_ecc, patterns);
342 add_patterns();
343 add_version();
344
345 mask = mask != -1 ? mask & 7 : select_mask(ecc, patterns);
346
347 add_format(ecc, mask);
348 apply_mask(mask, patterns);
349
350 return status = true;
351}
352
353template <int V>
354bool Qr<V>::encode_data(const char* data, size_t len, Ecc ecc, uint8_t* out) {
355 Mode mode = select_mode(data, len);
356
357 size_t n_bits = (N_DAT_CAPACITY - ECC_CODEWORDS_PER_BLOCK[ecc][V] * N_ECC_BLOCKS[ecc][V]) << 3;
358 size_t pos = 0;
359
360 add_bits(1 << mode, 4, out, pos);
361 add_bits(len, cci(V, mode), out, pos);
362
363 if (mode == M_NUMERIC) {
364 const size_t triplets = len / 3;
365 const size_t triplets_size = triplets * 3;
366 const size_t rem = len % 3;
367 const size_t rem_bits = rem == 2 ? 7 : rem == 1 ? 4 : 0;
368 const size_t total_size = 10 * triplets + rem_bits;
369
370 if (pos + total_size > n_bits) return false;
371
372 char buf[4] = {};
373
374 for (size_t i = 0; i < triplets_size; i += 3) {
375 buf[0] = data[i];
376 buf[1] = data[i + 1];
377 buf[2] = data[i + 2];
378
379 uint16_t num = StringToLong(buf, NULL, 10);
380 add_bits(num, 10, out, pos);
381 }
382
383 if (rem) {
384 buf[0] = data[triplets_size];
385 buf[1] = data[triplets_size + 1];
386 buf[rem] = 0;
387
388 uint16_t num = StringToLong(buf, NULL, 10);
389 add_bits(num, rem_bits, out, pos);
390 }
391 } else if (mode == M_ALPHANUMERIC) {
392 if (pos + 11 * (int(len & ~1ul) / 2) > n_bits) return false;
393
394 for (int i = 0; i < int(len & ~1ul); i += 2) {
395 uint16_t num = alphanumeric(data[i]) * 45 + alphanumeric(data[i + 1]);
396 add_bits(num, 11, out, pos);
397 }
398 if (len & 1) {
399 if (pos + 6 > n_bits) return false;
400
401 add_bits(alphanumeric(data[len - 1]), 6, out, pos);
402 }
403 } else if (mode == M_BYTE) {
404 if (pos + len * 8 > n_bits) return false;
405
406 for (size_t i = 0; i < len; ++i) add_bits(data[i], 8, out, pos);
407 } else {
408 if (pos + 13 * (len / 2) > n_bits) return false;
409
410 for (size_t i = 0; i < len; i += 2) {
411 uint16_t val = ((uint8_t) data[i]) | (((uint8_t) data[i + 1]) << 8);
412 uint16_t res = 0;
413 val -= val < 0x9FFC ? 0x8140 : 0xC140;
414 res += val & 0xff;
415 res += (val >> 8) * 0xc0;
416 add_bits(res, 13, out, pos);
417 }
418 }
419
420 size_t padding = n_bits - pos;
421 size_t i = 0;
422
423 add_bits(0, padding > 4 ? 4 : padding, out, pos);
424
425 if (pos & 7) add_bits(0, (8 - pos) & 7, out, pos);
426
427 while (pos < n_bits) add_bits(++i & 1 ? 0xec : 0x11, 8, out, pos);
428
429 return true;
430}
431
432template <int V>
433void Qr<V>::encode_ecc(uint8_t* data, Ecc ecc, uint8_t* out) {
434 int n_blocks = N_ECC_BLOCKS[ecc][V];
435 int ecc_len = ECC_CODEWORDS_PER_BLOCK[ecc][V];
436
437 int n_data_bytes = N_DAT_CAPACITY - ecc_len * n_blocks;
438
439 int n_short_blocks = n_blocks - N_DAT_CAPACITY % n_blocks;
440 int short_len = N_DAT_CAPACITY / n_blocks - ecc_len;
441
442 uint8_t gen_poly[30];
443 uint8_t ecc_buf[30];
444
445 gf_gen_poly(ecc_len, gen_poly);
446
447 uint8_t* data_ptr = data;
448
449 for (int i = 0; i < n_blocks; ++i) {
450 int data_len = short_len;
451
452 if (i >= n_short_blocks) ++data_len;
453
454 gf_poly_div(data_ptr, data_len, gen_poly, ecc_len, ecc_buf);
455
456 for (int j = 0, k = i; j < data_len; ++j, k += n_blocks) {
457 if (j == short_len) k -= n_short_blocks;
458 out[k] = data_ptr[j];
459 }
460 for (int j = 0, k = n_data_bytes + i; j < ecc_len; ++j, k += n_blocks) out[k] = ecc_buf[j];
461
462 data_ptr += data_len;
463 }
464}
465
466template <int V>
467void Qr<V>::add_data(uint8_t* data, uint8_t* patterns) {
468 int data_pos = 0;
469
470 for (int x = SIDE - 1; x >= 1; x -= 2) {
471 if (x == 6) x = 5;
472
473 for (int i = 0; i < SIDE; ++i) {
474 int y = !((x + 1) & 2) ? SIDE - 1 - i : i;
475 int coord = y * SIDE + x;
476
477 if (!get_arr_bit(patterns, coord)) {
478 if (get_bit_r(data, data_pos)) set_arr_bit(code, coord);
479
480 ++data_pos;
481 }
482
483 if (!get_arr_bit(patterns, coord - 1)) {
484 if (get_bit_r(data, data_pos)) set_arr_bit(code, coord - 1);
485
486 ++data_pos;
487 }
488 }
489 }
490}
491
492template <int V>
494 // White bounds inside finders
495 draw_bound<false>(1, 1, 5, 5, code);
496 draw_bound<false>(1, SIDE - 6, 5, 5, code);
497 draw_bound<false>(SIDE - 6, 1, 5, 5, code);
498
499 // Finish alignment patterns
500 for (int i = 0; i < N_ALIGN; ++i) {
501 for (int j = 0; j < N_ALIGN; ++j) {
502 if ((!i && !j) || (!i && j == N_ALIGN - 1) || (!j && i == N_ALIGN - 1)) continue;
503 draw_bound<false>(ALIGN_POS[V][i] - 1, ALIGN_POS[V][j] - 1, 3, 3, code);
504 }
505 }
506
507 // Draw white separators
508 draw_rect<false>(7, 0, 1, 8, code);
509 draw_rect<false>(0, 7, 8, 1, code);
510 draw_rect<false>(SIDE - 8, 0, 1, 8, code);
511 draw_rect<false>(SIDE - 8, 7, 8, 1, code);
512 draw_rect<false>(7, SIDE - 8, 1, 8, code);
513 draw_rect<false>(0, SIDE - 8, 8, 1, code);
514
515 // Perforate timing patterns
516 for (int i = 7; i < SIDE - 7; i += 2) {
517 clr_arr_bit(code, 6 * SIDE + i);
518 clr_arr_bit(code, i * SIDE + 6);
519 }
520}
521
522template <int V>
524 if (V < 7) return;
525
526 uint32_t rem = V;
527
528 for (uint8_t i = 0; i < 12; ++i) rem = (rem << 1) ^ ((rem >> 11) * 0x1F25);
529
530 uint32_t data = V << 12 | rem;
531
532 for (int x = 0; x < 6; ++x) {
533 for (int j = 0; j < 3; ++j) {
534 int y = SIDE - 11 + j;
535
536 bool black = (data >> (x * 3 + j)) & 1;
537
538 if (!black) {
539 clr_arr_bit(code, y * SIDE + x);
540 clr_arr_bit(code, y + SIDE * x);
541 }
542 }
543 }
544}
545
546template <int V>
547void Qr<V>::add_format(Ecc ecc, int mask) {
548 int data = (ecc ^ 1) << 3 | mask;
549 int rem = data;
550
551 for (int i = 0; i < 10; i++) rem = (rem << 1) ^ ((rem >> 9) * 0b10100110111);
552
553 int res = (data << 10 | rem) ^ 0b101010000010010;
554
555 for (int i = 0; i < 6; ++i) {
556 if ((res >> i) & 1) {
557 set_arr_bit(code, SIDE * 8 + SIDE - 1 - i);
558 set_arr_bit(code, SIDE * i + 8);
559 } else {
560 clr_arr_bit(code, SIDE * 8 + SIDE - 1 - i);
561 clr_arr_bit(code, SIDE * i + 8);
562 }
563 }
564
565 for (int i = 6; i < 8; ++i) {
566 if ((res >> i) & 1) {
567 set_arr_bit(code, SIDE * 8 + SIDE - 1 - i);
568 set_arr_bit(code, SIDE * (i + 1) + 8);
569 } else {
570 clr_arr_bit(code, SIDE * 8 + SIDE - 1 - i);
571 clr_arr_bit(code, SIDE * (i + 1) + 8);
572 }
573 }
574
575 if ((res >> 8) & 1) {
576 set_arr_bit(code, SIDE * 8 + 7);
577 set_arr_bit(code, SIDE * (SIDE - 7) + 8);
578 } else {
579 clr_arr_bit(code, SIDE * 8 + 7);
580 clr_arr_bit(code, SIDE * (SIDE - 7) + 8);
581 }
582
583 for (int i = 9, j = 5; i < 15; ++i, --j) {
584 if ((res >> i) & 1) {
585 set_arr_bit(code, SIDE * 8 + j);
586 set_arr_bit(code, SIDE * (SIDE - 1 - j) + 8);
587 } else {
588 clr_arr_bit(code, SIDE * 8 + j);
589 clr_arr_bit(code, SIDE * (SIDE - 1 - j) + 8);
590 }
591 }
592}
593
594template <int V>
595template <bool B>
596void Qr<V>::draw_rect(int y, int x, int height, int width, uint8_t* out) {
597 if (B) {
598 for (int dy = y * SIDE; dy < (y + height) * SIDE; dy += SIDE)
599 for (int dx = x; dx < x + width; ++dx) set_arr_bit(out, dy + dx);
600 } else {
601 for (int dy = y * SIDE; dy < (y + height) * SIDE; dy += SIDE)
602 for (int dx = x; dx < x + width; ++dx) clr_arr_bit(out, dy + dx);
603 }
604}
605
606template <int V>
607template <bool B>
608void Qr<V>::draw_bound(int y, int x, int height, int width, uint8_t* out) {
609 if (B) {
610 for (int i = y * SIDE + x; i < y * SIDE + x + width; ++i) set_arr_bit(out, i);
611 for (int i = (y + height - 1) * SIDE + x; i < (y + height - 1) * SIDE + x + width; ++i)
612 set_arr_bit(out, i);
613 for (int i = (y + 1) * SIDE + x; i < (y + height - 1) * SIDE + x; i += SIDE)
614 set_arr_bit(out, i);
615 for (int i = (y + 1) * SIDE + x + width - 1; i < (y + height - 1) * SIDE + x + width - 1;
616 i += SIDE)
617 set_arr_bit(out, i);
618 } else {
619 for (int i = y * SIDE + x; i < y * SIDE + x + width; ++i) clr_arr_bit(out, i);
620 for (int i = (y + height - 1) * SIDE + x; i < (y + height - 1) * SIDE + x + width; ++i)
621 clr_arr_bit(out, i);
622 for (int i = (y + 1) * SIDE + x; i < (y + height - 1) * SIDE + x; i += SIDE)
623 clr_arr_bit(out, i);
624 for (int i = (y + 1) * SIDE + x + width - 1; i < (y + height - 1) * SIDE + x + width - 1;
625 i += SIDE)
626 clr_arr_bit(out, i);
627 }
628}
629
630template <int V>
631void Qr<V>::reserve_patterns(uint8_t* out) {
632 draw_rect<true>(0, 6, SIDE, 1, out);
633 draw_rect<true>(6, 0, 1, SIDE, out);
634
635 draw_rect<true>(0, 0, 9, 9, out);
636 draw_rect<true>(SIDE - 8, 0, 8, 9, out);
637 draw_rect<true>(0, SIDE - 8, 9, 8, out);
638
639 for (int i = 0; i < N_ALIGN; ++i) {
640 for (int j = 0; j < N_ALIGN; ++j) {
641 if ((!i && !j) || (!i && j == N_ALIGN - 1) || (!j && i == N_ALIGN - 1)) continue;
642 draw_rect<true>(ALIGN_POS[V][i] - 2, ALIGN_POS[V][j] - 2, 5, 5, out);
643 }
644 }
645
646 if (V >= 7) {
647 draw_rect<true>(SIDE - 11, 0, 3, 6, out);
648 draw_rect<true>(0, SIDE - 11, 6, 3, out);
649 }
650}
651
652template <int V>
653template <bool H>
655 constexpr int y_max = H ? N_BITS : SIDE;
656 constexpr int x_max = H ? SIDE : N_BITS;
657 constexpr int y_step = H ? SIDE : 1;
658 constexpr int x_step = H ? 1 : SIDE;
659
660 int res = 0;
661
662 for (int y = 0; y < y_max; y += y_step) {
663 bool color = get_arr_bit(code, y);
664 int finder = color;
665 int cnt = 1;
666 for (int x = 1; x < x_max; x += x_step) {
667 if (get_arr_bit(code, y + x) == color) {
668 ++cnt;
669 if (cnt == 5) res += 3;
670 if (cnt > 5) ++res;
671 } else {
672 color = !color;
673 cnt = 1;
674 }
675 // Finder-like
676 finder = ((finder << 1) & 0x7ff) | color;
677 if (x >= x_step * 10) {
678 if (finder == 0x05d || finder == 0x5d0) res += 40;
679 }
680 }
681 }
682 return res;
683}
684
685template <int V>
687 int res = 0;
688
689 res += rule_1_3_score<true>();
690 res += rule_1_3_score<false>();
691
692 for (int y = 0; y < N_BITS - SIDE; y += SIDE) {
693 for (int x = 0; x < SIDE - 1; ++x) {
694 bool c = get_arr_bit(code, y + x);
695
696 if (c == get_arr_bit(code, y + x + 1) && c == get_arr_bit(code, y + x + SIDE) &&
697 c == get_arr_bit(code, y + x + SIDE + 1))
698 res += 3;
699 }
700 }
701
702 int black = 0;
703 for (int y = 0; y < N_BITS; y += SIDE) {
704 for (int x = 0; x < SIDE; ++x) black += get_arr_bit(code, y + x);
705 }
706 res += abs((black * 100) / N_BITS - 50) / 5 * 10;
707
708 return res;
709}
710
711template <int V>
712int Qr<V>::select_mask(Ecc ecc, uint8_t* patterns) {
713 unsigned min_score = -1;
714 unsigned score = 0;
715 uint8_t mask = 0;
716
717 for (int i = 0; i < 8; ++i) {
718 add_format(ecc, i);
719 apply_mask(i, patterns);
720 score = penalty_score();
721 if (score < min_score) {
722 mask = i;
723 min_score = score;
724 }
725 apply_mask(i, patterns);
726 }
727 return mask;
728}
729
730template <int V>
731void Qr<V>::apply_mask(int mask, uint8_t* patterns) {
732 for (int y = 0, dy = 0; y < SIDE; ++y, dy += SIDE) {
733 for (int x = 0; x < SIDE; ++x) {
734 int coord = dy + x;
735
736 if (get_arr_bit(patterns, coord)) continue;
737
738 bool keep = true;
739
740 switch (mask) {
741 case 0:
742 keep = (x + y) & 1;
743 break;
744 case 1:
745 keep = y & 1;
746 break;
747 case 2:
748 keep = x % 3;
749 break;
750 case 3:
751 keep = (x + y) % 3;
752 break;
753 case 4:
754 keep = (y / 2 + x / 3) & 1;
755 break;
756 case 5:
757 keep = x * y % 2 + x * y % 3;
758 break;
759 case 6:
760 keep = (x * y % 2 + x * y % 3) & 1;
761 break;
762 case 7:
763 keep = ((x + y) % 2 + x * y % 3) & 1;
764 break;
765 }
766
767 if (!keep) {
768 if (get_arr_bit(code, coord))
769 clr_arr_bit(code, coord);
770 else
771 set_arr_bit(code, coord);
772 }
773 }
774 }
775}
776
778class QrDelegate final {
779 public:
780 explicit QrDelegate() = default;
781 ~QrDelegate() = default;
782
784
785
786 template <Int32 V>
787 bool draw(Qr<V>& subject, Int32 x, Int32 y) {
788 return subject.draw(x, y);
789 }
790};
791} // namespace qr
792
793namespace Kernel::Qr {
794using namespace qr;
795} // namespace Kernel::Qr
796
797#endif // QR_H
#define cg_init()
Definition CoreGfx.h:11
#define FBDrawInRegion(clr, height, width, base_x, base_y)
Definition CoreGfx.h:97
#define cg_clear()
Definition CoreGfx.h:17
#define NE_COPY_DEFAULT(KLASS)
Definition Detail.h:17
#define RGB(R, G, B)
Purpose of this file is to help port libs into the bootloader.
#define CopyMem(dst, src, sz)
Definition Support.h:49
#define SetMem(dst, c, sz)
Definition Support.h:47
#define MoveMem(dst, src, sz)
Definition Support.h:48
long StringToLong(const char *nptr, char **endptr, int base)
Definition Support.h:67
~QrDelegate()=default
QrDelegate()=default
bool draw(Qr< V > &subject, Int32 x, Int32 y)
Draw method delegate.
Definition Qr.h:787
#define NULL
Definition macros.h:31
Definition Qr.h:793
__INT32_TYPE__ Int32
Definition Config.h:38
Definition Qr.h:15
Mode
Definition Qr.h:205
@ M_KANJI
Definition Qr.h:209
@ M_BYTE
Definition Qr.h:208
@ M_NUMERIC
Definition Qr.h:206
@ M_ALPHANUMERIC
Definition Qr.h:207
constexpr bool is_kanji(const char *str, size_t len)
Definition Qr.h:164
constexpr int N_ECC_BLOCKS[4][41]
Definition Qr.h:49
constexpr int cci(int ver, Mode mode)
Definition Qr.h:222
uint8_t min_poly
Definition Qr.h:16
constexpr int ECC_CODEWORDS_PER_BLOCK[4][41]
Definition Qr.h:36
constexpr int ALIGN_POS[41][7]
Definition Qr.h:62
constexpr void gf_poly_div(uint8_t *dividend, size_t len, uint8_t *divisor, int degree, uint8_t *result)
Definition Qr.h:186
uint8_t generator
Definition Qr.h:17
constexpr bool is_numeric(const char *str, size_t len)
Definition Qr.h:157
constexpr uint8_t get_bit_r(uint8_t *arr, int n)
Definition Qr.h:107
constexpr Mode select_mode(const char *str, size_t len)
Definition Qr.h:213
constexpr int alphanumeric(char c)
Definition Qr.h:121
constexpr bool is_alphanumeric(const char *str, size_t len)
Definition Qr.h:150
constexpr void add_bits(uint16_t data, int n, uint8_t *arr, size_t &pos)
Definition Qr.h:113
constexpr void gf_gen_poly(int degree, uint8_t *poly)
Definition Qr.h:173
uint8_t gf_mul(uint8_t a, uint8_t b)
galois finite field multiplication.
Definition Qr.h:20
Ecc
Definition Qr.h:198
@ H
Definition Qr.h:202
@ Q
Definition Qr.h:201
@ M
Definition Qr.h:200
@ L
Definition Qr.h:199
constexpr void clr_arr_bit(T *p, unsigned n)
Clear n-th bit in array of words (starting from LSB).
Definition bit.h:203
constexpr size_t bytes_in_bits(size_t n)
Get number of bytes required to store N bits.
Definition bit.h:117
constexpr void set_arr_bit(T *p, unsigned n)
Set n-th bit in array of words (starting from LSB).
Definition bit.h:191
constexpr bool get_arr_bit(const T *p, unsigned n)
Get n-th bit in array of words (starting from LSB).
Definition bit.h:179
Definition Qr.h:235
static constexpr int N_VER_BITS
Definition Qr.h:274
static constexpr int N_TIMING_BITS
Definition Qr.h:273
int penalty_score()
Definition Qr.h:686
void add_patterns()
Definition Qr.h:493
static constexpr int SIDE
Definition Qr.h:269
bool encode(const char *str, size_t len, Ecc ecc, int mask=-1)
Definition Qr.h:327
void add_data(uint8_t *data, uint8_t *patterns)
Definition Qr.h:467
void add_version()
Definition Qr.h:523
uint8_t code[N_BYTES]
Definition Qr.h:293
void draw_bound(int y, int x, int height, int width, uint8_t *out)
Definition Qr.h:608
bool encode_data(const char *data, size_t len, Ecc ecc, uint8_t *out)
Definition Qr.h:354
void draw_rect(int y, int x, int height, int width, uint8_t *out)
Definition Qr.h:596
int select_mask(Ecc ecc, uint8_t *patterns)
Definition Qr.h:712
static constexpr int N_ALIGN
Definition Qr.h:271
int rule_1_3_score()
Definition Qr.h:654
void reserve_patterns(uint8_t *out)
Definition Qr.h:631
uint8_t get_arr_bit(uint8_t *arr, unsigned bit) const
internal function to retrieve bit from a bitset.
Definition Qr.h:285
friend class QrDelegate
Definition Qr.h:237
static constexpr int N_DAT_CAPACITY
Definition Qr.h:281
static constexpr int N_DAT_BYTES
Definition Qr.h:278
void clr_arr_bit(uint8_t *arr, unsigned bit)
internal function to clear bit from a bitset.
Definition Qr.h:291
void add_format(Ecc ecc, int mask)
Definition Qr.h:547
constexpr auto side_size() const
Definition Qr.h:241
STATIC_PASS(V >=1 &&V<=40, "invalid version")
static constexpr int N_DAT_BITS
Definition Qr.h:275
bool status
Definition Qr.h:295
static constexpr int N_ALIGN_BITS
Definition Qr.h:272
void encode_ecc(uint8_t *data, Ecc ecc, uint8_t *out)
Definition Qr.h:433
bool module(int x, int y)
Definition Qr.h:300
static constexpr int N_BITS
Definition Qr.h:270
void set_arr_bit(uint8_t *arr, unsigned bit)
internal function to set bit from a bitset.
Definition Qr.h:288
bool draw(int x, int y)
draw a new QR code.
Definition Qr.h:306
static constexpr int N_BYTES
Definition Qr.h:276
void apply_mask(int mask, uint8_t *patterns)
Definition Qr.h:731