use super::version_db::VERSIONS;
fn reserved_cell(version: usize, i: usize, j: usize) -> bool {
debug_assert!(
(1..=40).contains(&version),
"reserved_cell: QR version {version} out of 1..=40"
);
let ver = &VERSIONS[version];
let size = version * 4 + 17;
if j < 9 && (i < 9 || i + 8 >= size) {
return true;
}
if i < 9 && j + 8 >= size {
return true;
}
if i == 6 || j == 6 {
return true;
}
if version >= 7 && ((i < 6 && j + 11 >= size) || (i + 11 >= size && j < 6)) {
return true;
}
let mut ai = None;
let mut aj = None;
let mut len = 0;
for (a, &pattern) in ver.apat.iter().take_while(|&&p| p != 0).enumerate() {
len = a;
if pattern.abs_diff(i) < 3 {
ai = Some(a);
}
if pattern.abs_diff(j) < 3 {
aj = Some(a);
}
}
match (ai, aj) {
(Some(x), Some(y)) if x == len && y == len => true,
(Some(x), Some(_)) if 0 < x && x < len => true,
(Some(_), Some(y)) if 0 < y && y < len => true,
_ => false,
}
}
fn mask_bit(mask: u8, y: usize, x: usize) -> bool {
match mask {
0 => (y + x).is_multiple_of(2),
1 => y.is_multiple_of(2),
2 => x.is_multiple_of(3),
3 => (y + x).is_multiple_of(3),
4 => ((y / 2) + (x / 3)).is_multiple_of(2),
5 => (y * x) % 2 + (y * x) % 3 == 0,
6 => ((y * x) % 2 + (y * x) % 3).is_multiple_of(2),
7 => ((y * x) % 3 + (y + x) % 2).is_multiple_of(2),
_ => false,
}
}
pub(crate) fn zigzag_positions(version: usize) -> Vec<(usize, usize)> {
debug_assert!(
(1..=40).contains(&version),
"zigzag_positions: QR version {version} out of 1..=40"
);
let size = version * 4 + 17;
let mut positions = Vec::with_capacity(VERSIONS[version].total * 8);
let mut y = size - 1;
let mut x = size - 1;
let mut neg_dir = true;
while x > 0 {
if x == 6 {
x -= 1;
}
if !reserved_cell(version, y, x) {
positions.push((y, x));
}
if !reserved_cell(version, y, x - 1) {
positions.push((y, x - 1));
}
let (new_y, new_neg_dir) = match (y, neg_dir) {
(0, true) => {
x = x.saturating_sub(2);
(0, false)
}
(yy, false) if yy == size - 1 => {
x = x.saturating_sub(2);
(size - 1, true)
}
(yy, true) => (yy - 1, true),
(yy, false) => (yy + 1, false),
};
y = new_y;
neg_dir = new_neg_dir;
}
positions
}
pub(crate) fn unmask_to_codewords(
masked: &[u8],
bit_len: usize,
version: usize,
mask: u8,
) -> Option<Vec<u8>> {
let total = VERSIONS[version].total;
if bit_len < total * 8 || masked.len() < total {
return None;
}
let positions = zigzag_positions(version);
let mut codewords = vec![0u8; total];
for (k, &(y, x)) in positions.iter().take(total * 8).enumerate() {
let mut bit = (masked[k / 8] >> (7 - (k % 8))) & 1;
if mask_bit(mask, y, x) {
bit ^= 1;
}
codewords[k / 8] |= bit << (7 - (k % 8));
}
Some(codewords)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn reserved_cell_finder_bounds_are_strict() {
assert!(!reserved_cell(5, 0, 9), "column 9 is data, not reserved");
assert!(!reserved_cell(5, 9, 0), "row 9 is data, not reserved");
assert!(reserved_cell(5, 8, 8), "the finder+format box is reserved");
}
#[test]
fn mask_formulas_truth_table() {
let table: [(u8, usize, usize, bool); 24] = [
(0, 0, 0, true),
(0, 0, 1, false),
(0, 1, 2, false),
(0, 2, 2, true),
(1, 0, 5, true),
(1, 1, 5, false),
(1, 2, 0, true),
(2, 0, 0, true),
(2, 0, 1, false),
(2, 0, 3, true),
(3, 0, 0, true),
(3, 1, 2, true),
(3, 2, 2, false),
(4, 0, 0, true),
(4, 2, 0, false),
(4, 2, 3, true),
(5, 0, 4, true),
(5, 1, 1, false),
(5, 2, 3, true),
(6, 1, 1, true),
(6, 1, 5, false),
(6, 2, 3, true),
(7, 0, 1, false),
(7, 1, 5, true),
];
for (mask, y, x, expected) in table {
assert_eq!(
mask_bit(mask, y, x),
expected,
"mask {mask} at (y={y}, x={x})"
);
}
assert!(!mask_bit(8, 0, 0), "unknown mask is never set");
}
#[test]
fn mask_bit_uses_modulo_not_addition() {
assert!(mask_bit(5, 2, 3), "y·x=6 → 6%2 + 6%3 = 0 → set");
assert!(mask_bit(5, 1, 6), "y·x=6 → set");
assert!(!mask_bit(5, 1, 1), "y·x=1 → 1%2 + 1%3 = 2 → clear");
assert!(mask_bit(6, 1, 1), "1%2 + 1%3 = 2 even → set");
assert!(!mask_bit(7, 1, 1), "1%3 + 2%2 = 1 odd → clear");
}
#[test]
fn a_stream_one_bit_short_of_the_capacity_is_refused() {
let total = VERSIONS[1].total;
let masked = vec![0u8; total];
assert_eq!(unmask_to_codewords(&masked, total * 8 - 1, 1, 0), None);
assert_eq!(unmask_to_codewords(&masked[1..], total * 8, 1, 0), None);
let full = unmask_to_codewords(&masked, total * 8, 1, 0);
assert_eq!(full.as_ref().map(Vec::len), Some(total));
}
}