anyd 0.1.1

From-scratch encoding and decoding of 1D and 2D barcodes with lossless round-trip and live-video detection
Documentation
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
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
//! Static rMQR specification tables (ISO/IEC 23941): the 32 sizes with their
//! dimensions, remainder bits, character-count field widths, and per-EC-level
//! Reed–Solomon block layout. Transcribed and cross-checked against the reference
//! `rmqrcode` implementation.

use super::{RmqrEcLevel, RmqrSize};
use crate::segment::Mode;

/// One Reed–Solomon block group: `num` blocks of `total` codewords each, `data` of
/// which are data codewords.
#[derive(Debug, Clone, Copy)]
pub struct BlockGroup {
    /// Number of blocks in this group.
    pub num: usize,
    /// Total codewords per block (data + EC).
    pub total: usize,
    /// Data codewords per block.
    pub data: usize,
}

/// Everything the encoder/decoder needs for one rMQR size.
pub struct SizeInfo {
    /// Symbol height in modules.
    pub height: usize,
    /// Symbol width in modules.
    pub width: usize,
    /// Trailing remainder bits after the last codeword.
    pub remainder: usize,
    /// Character-count field widths `[numeric, alphanumeric, byte, kanji]`.
    pub cc: [u8; 4],
    /// RS block layout for EC level M.
    pub blocks_m: &'static [BlockGroup],
    /// RS block layout for EC level H.
    pub blocks_h: &'static [BlockGroup],
}

const fn bg(num: usize, total: usize, data: usize) -> BlockGroup {
    BlockGroup { num, total, data }
}

/// The 32 rMQR sizes, indexed by version indicator (0..=31), matching the
/// ISO/IEC 23941 ordering R7x43, R7x59, …, R17x139.
pub static SIZES: [SizeInfo; 32] = [
    // R7x43
    SizeInfo {
        height: 7,
        width: 43,
        remainder: 0,
        cc: [4, 3, 3, 2],
        blocks_m: &[bg(1, 13, 6)],
        blocks_h: &[bg(1, 13, 3)],
    },
    // R7x59
    SizeInfo {
        height: 7,
        width: 59,
        remainder: 3,
        cc: [5, 5, 4, 3],
        blocks_m: &[bg(1, 21, 12)],
        blocks_h: &[bg(1, 21, 7)],
    },
    // R7x77
    SizeInfo {
        height: 7,
        width: 77,
        remainder: 5,
        cc: [6, 5, 5, 4],
        blocks_m: &[bg(1, 32, 20)],
        blocks_h: &[bg(1, 32, 10)],
    },
    // R7x99
    SizeInfo {
        height: 7,
        width: 99,
        remainder: 6,
        cc: [7, 6, 5, 5],
        blocks_m: &[bg(1, 44, 28)],
        blocks_h: &[bg(1, 44, 14)],
    },
    // R7x139
    SizeInfo {
        height: 7,
        width: 139,
        remainder: 1,
        cc: [7, 6, 6, 5],
        blocks_m: &[bg(1, 68, 44)],
        blocks_h: &[bg(2, 34, 12)],
    },
    // R9x43
    SizeInfo {
        height: 9,
        width: 43,
        remainder: 2,
        cc: [5, 5, 4, 3],
        blocks_m: &[bg(1, 21, 12)],
        blocks_h: &[bg(1, 21, 7)],
    },
    // R9x59
    SizeInfo {
        height: 9,
        width: 59,
        remainder: 3,
        cc: [6, 5, 5, 4],
        blocks_m: &[bg(1, 33, 21)],
        blocks_h: &[bg(1, 33, 11)],
    },
    // R9x77
    SizeInfo {
        height: 9,
        width: 77,
        remainder: 1,
        cc: [7, 6, 5, 5],
        blocks_m: &[bg(1, 49, 31)],
        blocks_h: &[bg(1, 24, 8), bg(1, 25, 9)],
    },
    // R9x99
    SizeInfo {
        height: 9,
        width: 99,
        remainder: 4,
        cc: [7, 6, 6, 5],
        blocks_m: &[bg(1, 66, 42)],
        blocks_h: &[bg(2, 33, 11)],
    },
    // R9x139
    SizeInfo {
        height: 9,
        width: 139,
        remainder: 5,
        cc: [8, 7, 6, 6],
        blocks_m: &[bg(1, 49, 31), bg(1, 50, 32)],
        blocks_h: &[bg(3, 33, 11)],
    },
    // R11x27
    SizeInfo {
        height: 11,
        width: 27,
        remainder: 2,
        cc: [4, 4, 3, 2],
        blocks_m: &[bg(1, 15, 7)],
        blocks_h: &[bg(1, 15, 5)],
    },
    // R11x43
    SizeInfo {
        height: 11,
        width: 43,
        remainder: 1,
        cc: [6, 5, 5, 4],
        blocks_m: &[bg(1, 31, 19)],
        blocks_h: &[bg(1, 31, 11)],
    },
    // R11x59
    SizeInfo {
        height: 11,
        width: 59,
        remainder: 0,
        cc: [7, 6, 5, 5],
        blocks_m: &[bg(1, 47, 31)],
        blocks_h: &[bg(1, 23, 7), bg(1, 24, 8)],
    },
    // R11x77
    SizeInfo {
        height: 11,
        width: 77,
        remainder: 2,
        cc: [7, 6, 6, 5],
        blocks_m: &[bg(1, 67, 43)],
        blocks_h: &[bg(1, 33, 11), bg(1, 34, 12)],
    },
    // R11x99
    SizeInfo {
        height: 11,
        width: 99,
        remainder: 7,
        cc: [8, 7, 6, 6],
        blocks_m: &[bg(1, 44, 28), bg(1, 45, 29)],
        blocks_h: &[bg(1, 44, 14), bg(1, 45, 15)],
    },
    // R11x139
    SizeInfo {
        height: 11,
        width: 139,
        remainder: 6,
        cc: [8, 7, 7, 6],
        blocks_m: &[bg(2, 66, 42)],
        blocks_h: &[bg(3, 44, 14)],
    },
    // R13x27 (rmqrcode lists k=14 for M, but ISO/IEC 23941 Table 8 and zxing-cpp
    // give k=12 — matching this size's 96 data-bit capacity).
    SizeInfo {
        height: 13,
        width: 27,
        remainder: 4,
        cc: [5, 5, 4, 3],
        blocks_m: &[bg(1, 21, 12)],
        blocks_h: &[bg(1, 21, 7)],
    },
    // R13x43
    SizeInfo {
        height: 13,
        width: 43,
        remainder: 1,
        cc: [6, 6, 5, 5],
        blocks_m: &[bg(1, 41, 27)],
        blocks_h: &[bg(1, 41, 13)],
    },
    // R13x59
    SizeInfo {
        height: 13,
        width: 59,
        remainder: 6,
        cc: [7, 6, 6, 5],
        blocks_m: &[bg(1, 60, 38)],
        blocks_h: &[bg(2, 30, 10)],
    },
    // R13x77
    SizeInfo {
        height: 13,
        width: 77,
        remainder: 4,
        cc: [7, 7, 6, 6],
        blocks_m: &[bg(1, 42, 26), bg(1, 43, 27)],
        blocks_h: &[bg(1, 42, 14), bg(1, 43, 15)],
    },
    // R13x99
    SizeInfo {
        height: 13,
        width: 99,
        remainder: 3,
        cc: [8, 7, 7, 6],
        blocks_m: &[bg(1, 56, 36), bg(1, 57, 37)],
        blocks_h: &[bg(1, 37, 11), bg(2, 38, 12)],
    },
    // R13x139
    SizeInfo {
        height: 13,
        width: 139,
        remainder: 0,
        cc: [8, 8, 7, 7],
        blocks_m: &[bg(2, 55, 35), bg(1, 56, 36)],
        blocks_h: &[bg(2, 41, 13), bg(2, 42, 14)],
    },
    // R15x43
    SizeInfo {
        height: 15,
        width: 43,
        remainder: 1,
        cc: [7, 6, 6, 5],
        blocks_m: &[bg(1, 51, 33)],
        blocks_h: &[bg(1, 25, 7), bg(1, 26, 8)],
    },
    // R15x59
    SizeInfo {
        height: 15,
        width: 59,
        remainder: 4,
        cc: [7, 7, 6, 5],
        blocks_m: &[bg(1, 74, 48)],
        blocks_h: &[bg(2, 37, 13)],
    },
    // R15x77
    SizeInfo {
        height: 15,
        width: 77,
        remainder: 6,
        cc: [8, 7, 7, 6],
        blocks_m: &[bg(1, 51, 33), bg(1, 52, 34)],
        blocks_h: &[bg(2, 34, 10), bg(1, 35, 11)],
    },
    // R15x99
    SizeInfo {
        height: 15,
        width: 99,
        remainder: 7,
        cc: [8, 7, 7, 6],
        blocks_m: &[bg(2, 68, 44)],
        blocks_h: &[bg(4, 34, 12)],
    },
    // R15x139
    SizeInfo {
        height: 15,
        width: 139,
        remainder: 2,
        cc: [9, 8, 7, 7],
        blocks_m: &[bg(2, 66, 42), bg(1, 67, 43)],
        blocks_h: &[bg(1, 39, 13), bg(4, 40, 14)],
    },
    // R17x43 (rmqrcode lists c=60 for M; ISO/IEC 23941 and zxing-cpp give 22 EC
    // codewords → c=61, matching the R17x43 codeword total of 61).
    SizeInfo {
        height: 17,
        width: 43,
        remainder: 1,
        cc: [7, 6, 6, 5],
        blocks_m: &[bg(1, 61, 39)],
        blocks_h: &[bg(1, 30, 10), bg(1, 31, 11)],
    },
    // R17x59
    SizeInfo {
        height: 17,
        width: 59,
        remainder: 2,
        cc: [8, 7, 6, 6],
        blocks_m: &[bg(2, 44, 28)],
        blocks_h: &[bg(2, 44, 14)],
    },
    // R17x77
    SizeInfo {
        height: 17,
        width: 77,
        remainder: 0,
        cc: [8, 7, 7, 6],
        blocks_m: &[bg(2, 61, 39)],
        blocks_h: &[bg(1, 40, 12), bg(2, 41, 13)],
    },
    // R17x99
    SizeInfo {
        height: 17,
        width: 99,
        remainder: 3,
        cc: [8, 8, 7, 6],
        blocks_m: &[bg(2, 53, 33), bg(1, 54, 34)],
        blocks_h: &[bg(4, 40, 14)],
    },
    // R17x139
    SizeInfo {
        height: 17,
        width: 139,
        remainder: 4,
        cc: [9, 8, 8, 7],
        blocks_m: &[bg(4, 58, 38)],
        blocks_h: &[bg(2, 38, 12), bg(4, 39, 13)],
    },
];

impl SizeInfo {
    /// The RS block layout for an EC level.
    pub fn blocks(&self, ec: RmqrEcLevel) -> &'static [BlockGroup] {
        match ec {
            RmqrEcLevel::M => self.blocks_m,
            RmqrEcLevel::H => self.blocks_h,
        }
    }

    /// Total codewords (data + EC) across all blocks.
    pub fn total_codewords(&self, ec: RmqrEcLevel) -> usize {
        self.blocks(ec).iter().map(|b| b.num * b.total).sum()
    }

    /// Total data codewords across all blocks.
    pub fn total_data_codewords(&self, ec: RmqrEcLevel) -> usize {
        self.blocks(ec).iter().map(|b| b.num * b.data).sum()
    }

    /// Usable data-bit capacity (content + terminator + padding), i.e. data
    /// codewords × 8.
    pub fn data_bit_capacity(&self, ec: RmqrEcLevel) -> usize {
        self.total_data_codewords(ec) * 8
    }
}

/// Look up a size's parameters.
pub fn info(size: RmqrSize) -> &'static SizeInfo {
    &SIZES[size.indicator() as usize]
}

/// Character-count indicator width for a mode at a size, or `None` for ECI.
pub fn char_count_bits(size: RmqrSize, mode: &Mode) -> Option<usize> {
    let cc = info(size).cc;
    Some(match mode {
        Mode::Numeric => cc[0] as usize,
        Mode::Alphanumeric => cc[1] as usize,
        Mode::Byte => cc[2] as usize,
        Mode::Kanji => cc[3] as usize,
        Mode::Eci(_) => return None,
    })
}

/// The 3-bit rMQR mode indicator value for a data mode.
pub fn mode_value(mode: &Mode) -> Option<u8> {
    Some(match mode {
        Mode::Numeric => 0b001,
        Mode::Alphanumeric => 0b010,
        Mode::Byte => 0b011,
        Mode::Kanji => 0b100,
        Mode::Eci(_) => return None,
    })
}

/// Recover a data mode from a 3-bit rMQR mode indicator value.
pub fn mode_from_value(v: u8) -> Option<Mode> {
    Some(match v {
        0b001 => Mode::Numeric,
        0b010 => Mode::Alphanumeric,
        0b011 => Mode::Byte,
        0b100 => Mode::Kanji,
        _ => return None,
    })
}

/// Alignment-pattern center columns for a symbol width.
pub fn alignment_columns(width: usize) -> &'static [usize] {
    match width {
        27 => &[],
        43 => &[21],
        59 => &[19, 39],
        77 => &[25, 51],
        99 => &[23, 49, 75],
        139 => &[27, 55, 83, 111],
        _ => &[],
    }
}

/// The 18-bit rMQR format code: 6 data bits (5-bit version indicator plus the H
/// flag in bit 5) followed by a 12-bit BCH remainder (generator `0x1F25`). The
/// per-copy XOR masks are applied at placement time.
pub fn format_bits(size: RmqrSize, ec_h: bool) -> u32 {
    let data = size.indicator() as u32 | if ec_h { 1 << 5 } else { 0 };
    let mut rem = data;
    for _ in 0..12 {
        rem = (rem << 1) ^ (((rem >> 11) & 1) * 0x1F25);
    }
    (data << 12) | (rem & 0xFFF)
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn capacities_are_consistent() {
        // number_of_data_bits from the ISO tables must equal data codewords × 8.
        let data_bits_m: [usize; 32] = [
            48, 96, 160, 224, 352, 96, 168, 248, 336, 504, 56, 152, 248, 344, 456, 672, 96, 216,
            304, 424, 584, 848, 264, 384, 536, 704, 1016, 312, 448, 624, 800, 1216,
        ];
        let data_bits_h: [usize; 32] = [
            24, 56, 80, 112, 192, 56, 88, 136, 176, 264, 40, 88, 120, 184, 232, 336, 56, 104, 160,
            232, 280, 432, 120, 208, 248, 384, 552, 168, 224, 304, 448, 608,
        ];
        for (i, s) in SIZES.iter().enumerate() {
            let size = RmqrSize::from_indicator(i as u8).unwrap();
            assert_eq!(
                s.data_bit_capacity(RmqrEcLevel::M),
                data_bits_m[i],
                "M vi={i}"
            );
            assert_eq!(
                s.data_bit_capacity(RmqrEcLevel::H),
                data_bits_h[i],
                "H vi={i}"
            );
            // The data region must exactly hold all codeword bits plus remainder.
            let modules = s.total_codewords(RmqrEcLevel::M) * 8 + s.remainder;
            let modules_h = s.total_codewords(RmqrEcLevel::H) * 8 + s.remainder;
            assert_eq!(modules, modules_h, "codeword bits differ by level vi={i}");
            let _ = size;
        }
    }
}