use std::iter::repeat;
use std::cmp::max;
use std::ops::Range;
use num_traits::PrimInt;
use types::{Version, EcLevel};
fn range_inclusive<N: PrimInt>(from: N, to: N) -> Range<N> {
from .. (to + N::one())
}
#[derive(PartialEq, Eq, Clone, Copy, Debug)]
pub enum Module {
Empty,
Light,
Dark,
LightUnmasked,
DarkUnmasked,
}
impl Module {
pub fn is_dark(&self) -> bool {
match *self {
Module::Dark | Module::DarkUnmasked => true,
_ => false,
}
}
pub fn mask(&self, should_invert: bool) -> Module {
match (*self, should_invert) {
(Module::Empty, true) | (Module::LightUnmasked, true) => Module::Dark,
(Module::Empty, false) | (Module::LightUnmasked, false) => Module::Light,
(Module::DarkUnmasked, true) => Module::Light,
(Module::DarkUnmasked, false) => Module::Dark,
(a, _) => a,
}
}
}
#[derive(Clone)]
pub struct Canvas {
width: i16,
version: Version,
ec_level: EcLevel,
modules: Vec<Module>,
}
impl Canvas {
pub fn new(version: Version, ec_level: EcLevel) -> Canvas {
let width = version.width();
Canvas {
width: width,
version: version,
ec_level: ec_level,
modules: repeat(Module::Empty).take((width*width) as usize).collect()
}
}
#[cfg(test)]
fn to_debug_str(&self) -> String {
let width = self.width;
let mut res = String::with_capacity((width * (width + 1)) as usize);
for y in 0 .. width {
res.push('\n');
for x in 0 .. width {
res.push(match self.get(x, y) {
Module::Empty => '?',
Module::Light => '.',
Module::Dark => '#',
Module::LightUnmasked => '-',
Module::DarkUnmasked => '*',
});
}
}
res
}
fn coords_to_index(&self, x: i16, y: i16) -> usize {
let x = if x < 0 { x + self.width } else { x } as usize;
let y = if y < 0 { y + self.width } else { y } as usize;
y * (self.width as usize) + x
}
pub fn get(&self, x: i16, y: i16) -> Module {
self.modules[self.coords_to_index(x, y)]
}
pub fn get_mut(&mut self, x: i16, y: i16) -> &mut Module {
let index = self.coords_to_index(x, y);
&mut self.modules[index]
}
pub fn put(&mut self, x: i16, y: i16, module: Module) {
*self.get_mut(x, y) = module;
}
}
#[cfg(test)]
mod basic_canvas_tests {
use canvas::{Canvas, Module};
use types::{Version, EcLevel};
#[test]
fn test_index() {
let mut c = Canvas::new(Version::Normal(1), EcLevel::L);
assert_eq!(c.get(0, 4), Module::Empty);
assert_eq!(c.get(-1, -7), Module::Empty);
assert_eq!(c.get(21-1, 21-7), Module::Empty);
c.put(0, 0, Module::Dark);
c.put(-1, -7, Module::Light);
assert_eq!(c.get(0, 0), Module::Dark);
assert_eq!(c.get(21-1, -7), Module::Light);
assert_eq!(c.get(-1, 21-7), Module::Light);
}
#[test]
fn test_debug_str() {
let mut c = Canvas::new(Version::Normal(1), EcLevel::L);
for i in 3i16 .. 20 {
for j in 3i16 .. 20 {
c.put(i, j, match ((i * 3) ^ j) % 5 {
0 => Module::Empty,
1 => Module::Light,
2 => Module::Dark,
3 => Module::LightUnmasked,
4 => Module::DarkUnmasked,
_ => panic!(),
});
}
}
assert_eq!(&*c.to_debug_str(), "\n\
?????????????????????\n\
?????????????????????\n\
?????????????????????\n\
?????####****....---?\n\
???--.##-..##?..#??.?\n\
???#*?-.*?#.-*#?-*.??\n\
?????*?*?****-*-*---?\n\
???*.-.-.-?-?#?#?#*#?\n\
???.*#.*.*#.*#*#.*#*?\n\
?????.#-#--??.?.#---?\n\
???-.?*.-#?-.?#*-#?.?\n\
???##*??*..##*--*..??\n\
?????-???--??---?---?\n\
???*.#.*.#**.#*#.#*#?\n\
???##.-##..##..?#..??\n\
???.-?*.-?#.-?#*-?#*?\n\
????-.#?-.**#?-.#?-.?\n\
???**?-**??--**?-**??\n\
???#?*?#?*#.*-.-*-.-?\n\
???..-...--??###?###?\n\
?????????????????????");
}
}
impl Canvas {
fn draw_finder_pattern_at(&mut self, x: i16, y: i16) {
let (dx_left, dx_right) = if x >= 0 { (-3, 4) } else { (-4, 3) };
let (dy_top, dy_bottom) = if y >= 0 { (-3, 4) } else { (-4, 3) };
for j in range_inclusive(dy_top, dy_bottom) {
for i in range_inclusive(dx_left, dx_right) {
self.put(x+i, y+j, match (i, j) {
(4, _) | (_, 4) | (-4, _) | (_, -4) => Module::Light,
(3, _) | (_, 3) | (-3, _) | (_, -3) => Module::Dark,
(2, _) | (_, 2) | (-2, _) | (_, -2) => Module::Light,
_ => Module::Dark,
});
}
}
}
fn draw_finder_patterns(&mut self) {
self.draw_finder_pattern_at(3, 3);
match self.version {
Version::Micro(_) => { return; }
Version::Normal(_) => {
self.draw_finder_pattern_at(-4, 3);
self.draw_finder_pattern_at(3, -4);
}
}
}
}
#[cfg(test)]
mod finder_pattern_tests {
use canvas::Canvas;
use types::{Version, EcLevel};
#[test]
fn test_qr() {
let mut c = Canvas::new(Version::Normal(1), EcLevel::L);
c.draw_finder_patterns();
assert_eq!(&*c.to_debug_str(), "\n\
#######.?????.#######\n\
#.....#.?????.#.....#\n\
#.###.#.?????.#.###.#\n\
#.###.#.?????.#.###.#\n\
#.###.#.?????.#.###.#\n\
#.....#.?????.#.....#\n\
#######.?????.#######\n\
........?????........\n\
?????????????????????\n\
?????????????????????\n\
?????????????????????\n\
?????????????????????\n\
?????????????????????\n\
........?????????????\n\
#######.?????????????\n\
#.....#.?????????????\n\
#.###.#.?????????????\n\
#.###.#.?????????????\n\
#.###.#.?????????????\n\
#.....#.?????????????\n\
#######.?????????????");
}
#[test]
fn test_micro_qr() {
let mut c = Canvas::new(Version::Micro(1), EcLevel::L);
c.draw_finder_patterns();
assert_eq!(&*c.to_debug_str(), "\n\
#######.???\n\
#.....#.???\n\
#.###.#.???\n\
#.###.#.???\n\
#.###.#.???\n\
#.....#.???\n\
#######.???\n\
........???\n\
???????????\n\
???????????\n\
???????????");
}
}
impl Canvas {
fn draw_alignment_pattern_at(&mut self, x: i16, y: i16) {
if self.get(x, y) != Module::Empty {
return;
}
for j in range_inclusive(-2, 2) {
for i in range_inclusive(-2, 2) {
self.put(x+i, y+j, match (i, j) {
(2, _) | (_, 2) | (-2, _) | (_, -2) | (0, 0) => Module::Dark,
_ => Module::Light,
});
}
}
}
fn draw_alignment_patterns(&mut self) {
match self.version {
Version::Micro(_) | Version::Normal(1) => { return; }
Version::Normal(2...6) => self.draw_alignment_pattern_at(-7, -7),
Version::Normal(a) => {
let positions = ALIGNMENT_PATTERN_POSITIONS[a as usize - 7];
for x in positions.iter() {
for y in positions.iter() {
self.draw_alignment_pattern_at(*x, *y);
}
}
}
}
}
}
#[cfg(test)]
mod alignment_pattern_tests {
use canvas::Canvas;
use types::{Version, EcLevel};
#[test]
fn test_draw_alignment_patterns_1() {
let mut c = Canvas::new(Version::Normal(1), EcLevel::L);
c.draw_finder_patterns();
c.draw_alignment_patterns();
assert_eq!(&*c.to_debug_str(), "\n\
#######.?????.#######\n\
#.....#.?????.#.....#\n\
#.###.#.?????.#.###.#\n\
#.###.#.?????.#.###.#\n\
#.###.#.?????.#.###.#\n\
#.....#.?????.#.....#\n\
#######.?????.#######\n\
........?????........\n\
?????????????????????\n\
?????????????????????\n\
?????????????????????\n\
?????????????????????\n\
?????????????????????\n\
........?????????????\n\
#######.?????????????\n\
#.....#.?????????????\n\
#.###.#.?????????????\n\
#.###.#.?????????????\n\
#.###.#.?????????????\n\
#.....#.?????????????\n\
#######.?????????????");
}
#[test]
fn test_draw_alignment_patterns_3() {
let mut c = Canvas::new(Version::Normal(3), EcLevel::L);
c.draw_finder_patterns();
c.draw_alignment_patterns();
assert_eq!(&*c.to_debug_str(), "\n\
#######.?????????????.#######\n\
#.....#.?????????????.#.....#\n\
#.###.#.?????????????.#.###.#\n\
#.###.#.?????????????.#.###.#\n\
#.###.#.?????????????.#.###.#\n\
#.....#.?????????????.#.....#\n\
#######.?????????????.#######\n\
........?????????????........\n\
?????????????????????????????\n\
?????????????????????????????\n\
?????????????????????????????\n\
?????????????????????????????\n\
?????????????????????????????\n\
?????????????????????????????\n\
?????????????????????????????\n\
?????????????????????????????\n\
?????????????????????????????\n\
?????????????????????????????\n\
?????????????????????????????\n\
?????????????????????????????\n\
????????????????????#####????\n\
........????????????#...#????\n\
#######.????????????#.#.#????\n\
#.....#.????????????#...#????\n\
#.###.#.????????????#####????\n\
#.###.#.?????????????????????\n\
#.###.#.?????????????????????\n\
#.....#.?????????????????????\n\
#######.?????????????????????");
}
#[test]
fn test_draw_alignment_patterns_7() {
let mut c = Canvas::new(Version::Normal(7), EcLevel::L);
c.draw_finder_patterns();
c.draw_alignment_patterns();
assert_eq!(&*c.to_debug_str(), "\n\
#######.?????????????????????????????.#######\n\
#.....#.?????????????????????????????.#.....#\n\
#.###.#.?????????????????????????????.#.###.#\n\
#.###.#.?????????????????????????????.#.###.#\n\
#.###.#.????????????#####????????????.#.###.#\n\
#.....#.????????????#...#????????????.#.....#\n\
#######.????????????#.#.#????????????.#######\n\
........????????????#...#????????????........\n\
????????????????????#####????????????????????\n\
?????????????????????????????????????????????\n\
?????????????????????????????????????????????\n\
?????????????????????????????????????????????\n\
?????????????????????????????????????????????\n\
?????????????????????????????????????????????\n\
?????????????????????????????????????????????\n\
?????????????????????????????????????????????\n\
?????????????????????????????????????????????\n\
?????????????????????????????????????????????\n\
?????????????????????????????????????????????\n\
?????????????????????????????????????????????\n\
????#####???????????#####???????????#####????\n\
????#...#???????????#...#???????????#...#????\n\
????#.#.#???????????#.#.#???????????#.#.#????\n\
????#...#???????????#...#???????????#...#????\n\
????#####???????????#####???????????#####????\n\
?????????????????????????????????????????????\n\
?????????????????????????????????????????????\n\
?????????????????????????????????????????????\n\
?????????????????????????????????????????????\n\
?????????????????????????????????????????????\n\
?????????????????????????????????????????????\n\
?????????????????????????????????????????????\n\
?????????????????????????????????????????????\n\
?????????????????????????????????????????????\n\
?????????????????????????????????????????????\n\
?????????????????????????????????????????????\n\
????????????????????#####???????????#####????\n\
........????????????#...#???????????#...#????\n\
#######.????????????#.#.#???????????#.#.#????\n\
#.....#.????????????#...#???????????#...#????\n\
#.###.#.????????????#####???????????#####????\n\
#.###.#.?????????????????????????????????????\n\
#.###.#.?????????????????????????????????????\n\
#.....#.?????????????????????????????????????\n\
#######.?????????????????????????????????????");
}
}
static ALIGNMENT_PATTERN_POSITIONS: [&'static [i16]; 34] = [
&[6, 22, 38],
&[6, 24, 42],
&[6, 26, 46],
&[6, 28, 50],
&[6, 30, 54],
&[6, 32, 58],
&[6, 34, 62],
&[6, 26, 46, 66],
&[6, 26, 48, 70],
&[6, 26, 50, 74],
&[6, 30, 54, 78],
&[6, 30, 56, 82],
&[6, 30, 58, 86],
&[6, 34, 62, 90],
&[6, 28, 50, 72, 94],
&[6, 26, 50, 74, 98],
&[6, 30, 54, 78, 102],
&[6, 28, 54, 80, 106],
&[6, 32, 58, 84, 110],
&[6, 30, 58, 86, 114],
&[6, 34, 62, 90, 118],
&[6, 26, 50, 74, 98, 122],
&[6, 30, 54, 78, 102, 126],
&[6, 26, 52, 78, 104, 130],
&[6, 30, 56, 82, 108, 134],
&[6, 34, 60, 86, 112, 138],
&[6, 30, 58, 86, 114, 142],
&[6, 34, 62, 90, 118, 146],
&[6, 30, 54, 78, 102, 126, 150],
&[6, 24, 50, 76, 102, 128, 154],
&[6, 28, 54, 80, 106, 132, 158],
&[6, 32, 58, 84, 110, 136, 162],
&[6, 26, 54, 82, 110, 138, 166],
&[6, 30, 58, 86, 114, 142, 170],
];
impl Canvas {
fn draw_line(&mut self, x1: i16, y1: i16, x2: i16, y2: i16,
color_even: Module, color_odd: Module) {
debug_assert!(x1 == x2 || y1 == y2);
if y1 == y2 { for x in range_inclusive(x1, x2) {
self.put(x, y1, if x % 2 == 0 { color_even } else { color_odd });
}
} else { for y in range_inclusive(y1, y2) {
self.put(x1, y, if y % 2 == 0 { color_even } else { color_odd });
}
}
}
fn draw_timing_patterns(&mut self) {
let width = self.width;
let (y, x1, x2) = match self.version {
Version::Micro(_) => (0, 8, width-1),
Version::Normal(_) => (6, 8, width-9),
};
self.draw_line(x1, y, x2, y, Module::Dark, Module::Light);
self.draw_line(y, x1, y, x2, Module::Dark, Module::Light);
}
}
#[cfg(test)]
mod timing_pattern_tests {
use canvas::Canvas;
use types::{Version, EcLevel};
#[test]
fn test_draw_timing_patterns_qr() {
let mut c = Canvas::new(Version::Normal(1), EcLevel::L);
c.draw_timing_patterns();
assert_eq!(&*c.to_debug_str(), "\n\
?????????????????????\n\
?????????????????????\n\
?????????????????????\n\
?????????????????????\n\
?????????????????????\n\
?????????????????????\n\
????????#.#.#????????\n\
?????????????????????\n\
??????#??????????????\n\
??????.??????????????\n\
??????#??????????????\n\
??????.??????????????\n\
??????#??????????????\n\
?????????????????????\n\
?????????????????????\n\
?????????????????????\n\
?????????????????????\n\
?????????????????????\n\
?????????????????????\n\
?????????????????????\n\
?????????????????????");
}
#[test]
fn test_draw_timing_patterns_micro_qr() {
let mut c = Canvas::new(Version::Micro(1), EcLevel::L);
c.draw_timing_patterns();
assert_eq!(&*c.to_debug_str(), "\n\
????????#.#\n\
???????????\n\
???????????\n\
???????????\n\
???????????\n\
???????????\n\
???????????\n\
???????????\n\
#??????????\n\
.??????????\n\
#??????????");
}
}
impl Canvas {
fn draw_number<N: PrimInt>(&mut self, number: N,
on_color: Module, off_color: Module,
coords: &[(i16, i16)]) {
let zero: N = N::zero();
let mut mask: N = !(!zero >> 1);
for &(x, y) in coords {
let color = if (mask & number) == zero { off_color } else { on_color };
self.put(x, y, color);
mask = mask >> 1;
}
}
fn draw_format_info_patterns_with_number(&mut self, format_info: u16) {
match self.version {
Version::Micro(_) => {
self.draw_number(format_info, Module::Dark, Module::Light,
&FORMAT_INFO_COORDS_MICRO_QR);
}
Version::Normal(_) => {
self.draw_number(format_info, Module::Dark, Module::Light,
&FORMAT_INFO_COORDS_QR_MAIN);
self.draw_number(format_info, Module::Dark, Module::Light,
&FORMAT_INFO_COORDS_QR_SIDE);
self.put(8, -8, Module::Dark); }
}
}
fn draw_reserved_format_info_patterns(&mut self) {
self.draw_format_info_patterns_with_number(0);
}
fn draw_version_info_patterns(&mut self) {
match self.version {
Version::Micro(_) | Version::Normal(1...6) => { return; }
Version::Normal(a) => {
let version_info = VERSION_INFOS[(a - 7) as usize] << 14;
self.draw_number(version_info, Module::Dark, Module::Light,
&VERSION_INFO_COORDS_BL);
self.draw_number(version_info, Module::Dark, Module::Light,
&VERSION_INFO_COORDS_TR);
}
}
}
}
#[cfg(test)]
mod draw_version_info_tests {
use canvas::{Canvas, Module};
use types::{Version, EcLevel};
#[test]
fn test_draw_number() {
let mut c = Canvas::new(Version::Micro(1), EcLevel::L);
c.draw_number(0b10101101u8, Module::Dark, Module::Light,
&[(0,0), (0,-1), (-2,-2), (-2,0)]);
assert_eq!(&*c.to_debug_str(), "\n\
#????????.?\n\
???????????\n\
???????????\n\
???????????\n\
???????????\n\
???????????\n\
???????????\n\
???????????\n\
???????????\n\
?????????#?\n\
.??????????");
}
#[test]
fn test_draw_version_info_1() {
let mut c = Canvas::new(Version::Normal(1), EcLevel::L);
c.draw_version_info_patterns();
assert_eq!(&*c.to_debug_str(), "\n\
?????????????????????\n\
?????????????????????\n\
?????????????????????\n\
?????????????????????\n\
?????????????????????\n\
?????????????????????\n\
?????????????????????\n\
?????????????????????\n\
?????????????????????\n\
?????????????????????\n\
?????????????????????\n\
?????????????????????\n\
?????????????????????\n\
?????????????????????\n\
?????????????????????\n\
?????????????????????\n\
?????????????????????\n\
?????????????????????\n\
?????????????????????\n\
?????????????????????\n\
?????????????????????");
}
#[test]
fn test_draw_version_info_7() {
let mut c = Canvas::new(Version::Normal(7), EcLevel::L);
c.draw_version_info_patterns();
assert_eq!(&*c.to_debug_str(), "\n\
??????????????????????????????????..#????????\n\
??????????????????????????????????.#.????????\n\
??????????????????????????????????.#.????????\n\
??????????????????????????????????.##????????\n\
??????????????????????????????????###????????\n\
??????????????????????????????????...????????\n\
?????????????????????????????????????????????\n\
?????????????????????????????????????????????\n\
?????????????????????????????????????????????\n\
?????????????????????????????????????????????\n\
?????????????????????????????????????????????\n\
?????????????????????????????????????????????\n\
?????????????????????????????????????????????\n\
?????????????????????????????????????????????\n\
?????????????????????????????????????????????\n\
?????????????????????????????????????????????\n\
?????????????????????????????????????????????\n\
?????????????????????????????????????????????\n\
?????????????????????????????????????????????\n\
?????????????????????????????????????????????\n\
?????????????????????????????????????????????\n\
?????????????????????????????????????????????\n\
?????????????????????????????????????????????\n\
?????????????????????????????????????????????\n\
?????????????????????????????????????????????\n\
?????????????????????????????????????????????\n\
?????????????????????????????????????????????\n\
?????????????????????????????????????????????\n\
?????????????????????????????????????????????\n\
?????????????????????????????????????????????\n\
?????????????????????????????????????????????\n\
?????????????????????????????????????????????\n\
?????????????????????????????????????????????\n\
?????????????????????????????????????????????\n\
....#.???????????????????????????????????????\n\
.####.???????????????????????????????????????\n\
#..##.???????????????????????????????????????\n\
?????????????????????????????????????????????\n\
?????????????????????????????????????????????\n\
?????????????????????????????????????????????\n\
?????????????????????????????????????????????\n\
?????????????????????????????????????????????\n\
?????????????????????????????????????????????\n\
?????????????????????????????????????????????\n\
?????????????????????????????????????????????");
}
#[test]
fn test_draw_reserved_format_info_patterns_qr() {
let mut c = Canvas::new(Version::Normal(1), EcLevel::L);
c.draw_reserved_format_info_patterns();
assert_eq!(&*c.to_debug_str(), "\n\
????????.????????????\n\
????????.????????????\n\
????????.????????????\n\
????????.????????????\n\
????????.????????????\n\
????????.????????????\n\
?????????????????????\n\
????????.????????????\n\
......?..????........\n\
?????????????????????\n\
?????????????????????\n\
?????????????????????\n\
?????????????????????\n\
????????#????????????\n\
????????.????????????\n\
????????.????????????\n\
????????.????????????\n\
????????.????????????\n\
????????.????????????\n\
????????.????????????\n\
????????.????????????");
}
#[test]
fn test_draw_reserved_format_info_patterns_micro_qr() {
let mut c = Canvas::new(Version::Micro(1), EcLevel::L);
c.draw_reserved_format_info_patterns();
assert_eq!(&*c.to_debug_str(), "\n\
???????????\n\
????????.??\n\
????????.??\n\
????????.??\n\
????????.??\n\
????????.??\n\
????????.??\n\
????????.??\n\
?........??\n\
???????????\n\
???????????");
}
}
static VERSION_INFO_COORDS_BL: [(i16, i16); 18] = [
(5, -9), (5, -10), (5, -11),
(4, -9), (4, -10), (4, -11),
(3, -9), (3, -10), (3, -11),
(2, -9), (2, -10), (2, -11),
(1, -9), (1, -10), (1, -11),
(0, -9), (0, -10), (0, -11),
];
static VERSION_INFO_COORDS_TR: [(i16, i16); 18] = [
(-9, 5), (-10, 5), (-11, 5),
(-9, 4), (-10, 4), (-11, 4),
(-9, 3), (-10, 3), (-11, 3),
(-9, 2), (-10, 2), (-11, 2),
(-9, 1), (-10, 1), (-11, 1),
(-9, 0), (-10, 0), (-11, 0),
];
static FORMAT_INFO_COORDS_QR_MAIN: [(i16, i16); 15] = [
(0, 8), (1, 8), (2, 8), (3, 8), (4, 8), (5, 8),
(7, 8), (8, 8), (8, 7),
(8, 5), (8, 4), (8, 3), (8, 2), (8, 1), (8, 0),
];
static FORMAT_INFO_COORDS_QR_SIDE: [(i16, i16); 15] = [
(8, -1), (8, -2), (8, -3), (8, -4), (8, -5), (8, -6), (8, -7),
(-8, 8), (-7, 8), (-6, 8), (-5, 8), (-4, 8), (-3, 8), (-2, 8), (-1, 8),
];
static FORMAT_INFO_COORDS_MICRO_QR: [(i16, i16); 15] = [
(1, 8), (2, 8), (3, 8), (4, 8), (5, 8), (6, 8), (7, 8), (8, 8),
(8, 7), (8, 6), (8, 5), (8, 4), (8, 3), (8, 2), (8, 1),
];
static VERSION_INFOS: [u32; 34] = [
0x07c94, 0x085bc, 0x09a99, 0x0a4d3, 0x0bbf6, 0x0c762, 0x0d847, 0x0e60d,
0x0f928, 0x10b78, 0x1145d, 0x12a17, 0x13532, 0x149a6, 0x15683, 0x168c9,
0x177ec, 0x18ec4, 0x191e1, 0x1afab, 0x1b08e, 0x1cc1a, 0x1d33f, 0x1ed75,
0x1f250, 0x209d5, 0x216f0, 0x228ba, 0x2379f, 0x24b0b, 0x2542e, 0x26a64,
0x27541, 0x28c69,
];
impl Canvas {
pub fn draw_all_functional_patterns(&mut self) {
self.draw_finder_patterns();
self.draw_alignment_patterns();
self.draw_reserved_format_info_patterns();
self.draw_timing_patterns();
self.draw_version_info_patterns();
}
}
pub fn is_functional(version: Version, width: i16, x: i16, y: i16) -> bool {
debug_assert!(width == version.width());
let x = if x < 0 { x + width } else { x };
let y = if y < 0 { y + width } else { y };
match version {
Version::Micro(_) => x == 0 || y == 0 || (x < 9 && y < 9),
Version::Normal(a) => {
let non_alignment_test =
x == 6 || y == 6 || (x < 9 && y < 9) || (x < 9 && y >= width-8) || (x >= width-8 && y < 9); if non_alignment_test {
true
} else if a == 1 {
false
} else if 2 <= a && a <= 6 {
(width - 7 - x).abs() <= 2 && (width - 7 - y).abs() <= 2
} else {
let positions = ALIGNMENT_PATTERN_POSITIONS[a as usize - 7];
let last = positions.len() - 1;
for (i, align_x) in positions.iter().enumerate() {
for (j, align_y) in positions.iter().enumerate() {
if i == 0 && (j == 0 || j == last) || (i == last && j == 0) {
continue;
}
if (*align_x - x).abs() <= 2 && (*align_y - y).abs() <= 2 {
return true;
}
}
};
false
}
},
}
}
#[cfg(test)]
mod all_functional_patterns_tests {
use canvas::{Canvas, is_functional};
use types::{Version, EcLevel};
#[test]
fn test_all_functional_patterns_qr() {
let mut c = Canvas::new(Version::Normal(2), EcLevel::L);
c.draw_all_functional_patterns();
assert_eq!(&*c.to_debug_str(), "\n\
#######..????????.#######\n\
#.....#..????????.#.....#\n\
#.###.#..????????.#.###.#\n\
#.###.#..????????.#.###.#\n\
#.###.#..????????.#.###.#\n\
#.....#..????????.#.....#\n\
#######.#.#.#.#.#.#######\n\
.........????????........\n\
......#..????????........\n\
??????.??????????????????\n\
??????#??????????????????\n\
??????.??????????????????\n\
??????#??????????????????\n\
??????.??????????????????\n\
??????#??????????????????\n\
??????.??????????????????\n\
??????#?????????#####????\n\
........#???????#...#????\n\
#######..???????#.#.#????\n\
#.....#..???????#...#????\n\
#.###.#..???????#####????\n\
#.###.#..????????????????\n\
#.###.#..????????????????\n\
#.....#..????????????????\n\
#######..????????????????");
}
#[test]
fn test_all_functional_patterns_micro_qr() {
let mut c = Canvas::new(Version::Micro(1), EcLevel::L);
c.draw_all_functional_patterns();
assert_eq!(&*c.to_debug_str(), "\n\
#######.#.#\n\
#.....#..??\n\
#.###.#..??\n\
#.###.#..??\n\
#.###.#..??\n\
#.....#..??\n\
#######..??\n\
.........??\n\
#........??\n\
.??????????\n\
#??????????");
}
#[test]
fn test_is_functional_qr_1() {
let version = Version::Normal(1);
assert!(is_functional(version, version.width(), 0, 0));
assert!(is_functional(version, version.width(), 10, 6));
assert!(!is_functional(version, version.width(), 10, 5));
assert!(!is_functional(version, version.width(), 14, 14));
assert!(is_functional(version, version.width(), 6, 11));
assert!(!is_functional(version, version.width(), 4, 11));
assert!(is_functional(version, version.width(), 4, 13));
assert!(is_functional(version, version.width(), 17, 7));
assert!(!is_functional(version, version.width(), 17, 17));
}
#[test]
fn test_is_functional_qr_3() {
let version = Version::Normal(3);
assert!(is_functional(version, version.width(), 0, 0));
assert!(!is_functional(version, version.width(), 25, 24));
assert!(is_functional(version, version.width(), 24, 24));
assert!(!is_functional(version, version.width(), 9, 25));
assert!(!is_functional(version, version.width(), 20, 0));
assert!(is_functional(version, version.width(), 21, 0));
}
#[test]
fn test_is_functional_qr_7() {
let version = Version::Normal(7);
assert!(is_functional(version, version.width(), 21, 4));
assert!(is_functional(version, version.width(), 7, 21));
assert!(is_functional(version, version.width(), 22, 22));
assert!(is_functional(version, version.width(), 8, 8));
assert!(!is_functional(version, version.width(), 19, 5));
assert!(!is_functional(version, version.width(), 36, 3));
assert!(!is_functional(version, version.width(), 4, 36));
assert!(is_functional(version, version.width(), 38, 38));
}
#[test]
fn test_is_functional_micro() {
let version = Version::Micro(1);
assert!(is_functional(version, version.width(), 8, 0));
assert!(is_functional(version, version.width(), 10, 0));
assert!(!is_functional(version, version.width(), 10, 1));
assert!(is_functional(version, version.width(), 8, 8));
assert!(is_functional(version, version.width(), 0, 9));
assert!(!is_functional(version, version.width(), 1, 9));
}
}
struct DataModuleIter {
x: i16,
y: i16,
width: i16,
timing_pattern_column: i16,
}
impl DataModuleIter {
fn new(version: Version) -> DataModuleIter {
let width = version.width();
DataModuleIter {
x: width - 1,
y: width - 1,
width: width,
timing_pattern_column: match version {
Version::Micro(_) => 0,
Version::Normal(_) => 6,
}
}
}
}
impl Iterator for DataModuleIter {
type Item = (i16, i16);
fn next(&mut self) -> Option<(i16, i16)> {
let adjusted_ref_col = if self.x <= self.timing_pattern_column {
self.x + 1
} else {
self.x
};
if adjusted_ref_col <= 0 {
return None;
}
let res = (self.x, self.y);
let column_type = (self.width - adjusted_ref_col) % 4;
match column_type {
2 if self.y > 0 => {
self.y -= 1;
self.x += 1;
}
0 if self.y < self.width - 1 => {
self.y += 1;
self.x += 1;
}
0 | 2 if self.x == self.timing_pattern_column + 1 => {
self.x -= 2;
}
_ => {
self.x -= 1;
}
}
Some(res)
}
}
#[cfg(test)]
mod data_iter_tests {
use canvas::DataModuleIter;
use types::Version;
#[test]
fn test_qr() {
let res = DataModuleIter::new(Version::Normal(1)).collect::<Vec<(i16, i16)>>();
assert_eq!(res, vec![
(20, 20), (19, 20), (20, 19), (19, 19), (20, 18), (19, 18),
(20, 17), (19, 17), (20, 16), (19, 16), (20, 15), (19, 15),
(20, 14), (19, 14), (20, 13), (19, 13), (20, 12), (19, 12),
(20, 11), (19, 11), (20, 10), (19, 10), (20, 9), (19, 9),
(20, 8), (19, 8), (20, 7), (19, 7), (20, 6), (19, 6),
(20, 5), (19, 5), (20, 4), (19, 4), (20, 3), (19, 3),
(20, 2), (19, 2), (20, 1), (19, 1), (20, 0), (19, 0),
(18, 0), (17, 0), (18, 1), (17, 1), (18, 2), (17, 2),
(18, 3), (17, 3), (18, 4), (17, 4), (18, 5), (17, 5),
(18, 6), (17, 6), (18, 7), (17, 7), (18, 8), (17, 8),
(18, 9), (17, 9), (18, 10), (17, 10), (18, 11), (17, 11),
(18, 12), (17, 12), (18, 13), (17, 13), (18, 14), (17, 14),
(18, 15), (17, 15), (18, 16), (17, 16), (18, 17), (17, 17),
(18, 18), (17, 18), (18, 19), (17, 19), (18, 20), (17, 20),
(16, 20), (15, 20), (16, 19), (15, 19), (16, 18), (15, 18),
(16, 17), (15, 17), (16, 16), (15, 16), (16, 15), (15, 15),
(16, 14), (15, 14), (16, 13), (15, 13), (16, 12), (15, 12),
(16, 11), (15, 11), (16, 10), (15, 10), (16, 9), (15, 9),
(16, 8), (15, 8), (16, 7), (15, 7), (16, 6), (15, 6),
(16, 5), (15, 5), (16, 4), (15, 4), (16, 3), (15, 3),
(16, 2), (15, 2), (16, 1), (15, 1), (16, 0), (15, 0),
(14, 0), (13, 0), (14, 1), (13, 1), (14, 2), (13, 2),
(14, 3), (13, 3), (14, 4), (13, 4), (14, 5), (13, 5),
(14, 6), (13, 6), (14, 7), (13, 7), (14, 8), (13, 8),
(14, 9), (13, 9), (14, 10), (13, 10), (14, 11), (13, 11),
(14, 12), (13, 12), (14, 13), (13, 13), (14, 14), (13, 14),
(14, 15), (13, 15), (14, 16), (13, 16), (14, 17), (13, 17),
(14, 18), (13, 18), (14, 19), (13, 19), (14, 20), (13, 20),
(12, 20), (11, 20), (12, 19), (11, 19), (12, 18), (11, 18),
(12, 17), (11, 17), (12, 16), (11, 16), (12, 15), (11, 15),
(12, 14), (11, 14), (12, 13), (11, 13), (12, 12), (11, 12),
(12, 11), (11, 11), (12, 10), (11, 10), (12, 9), (11, 9),
(12, 8), (11, 8), (12, 7), (11, 7), (12, 6), (11, 6),
(12, 5), (11, 5), (12, 4), (11, 4), (12, 3), (11, 3),
(12, 2), (11, 2), (12, 1), (11, 1), (12, 0), (11, 0),
(10, 0), (9, 0), (10, 1), (9, 1), (10, 2), (9, 2),
(10, 3), (9, 3), (10, 4), (9, 4), (10, 5), (9, 5),
(10, 6), (9, 6), (10, 7), (9, 7), (10, 8), (9, 8),
(10, 9), (9, 9), (10, 10), (9, 10), (10, 11), (9, 11),
(10, 12), (9, 12), (10, 13), (9, 13), (10, 14), (9, 14),
(10, 15), (9, 15), (10, 16), (9, 16), (10, 17), (9, 17),
(10, 18), (9, 18), (10, 19), (9, 19), (10, 20), (9, 20),
(8, 20), (7, 20), (8, 19), (7, 19), (8, 18), (7, 18),
(8, 17), (7, 17), (8, 16), (7, 16), (8, 15), (7, 15),
(8, 14), (7, 14), (8, 13), (7, 13), (8, 12), (7, 12),
(8, 11), (7, 11), (8, 10), (7, 10), (8, 9), (7, 9),
(8, 8), (7, 8), (8, 7), (7, 7), (8, 6), (7, 6),
(8, 5), (7, 5), (8, 4), (7, 4), (8, 3), (7, 3),
(8, 2), (7, 2), (8, 1), (7, 1), (8, 0), (7, 0),
(5, 0), (4, 0), (5, 1), (4, 1), (5, 2), (4, 2),
(5, 3), (4, 3), (5, 4), (4, 4), (5, 5), (4, 5),
(5, 6), (4, 6), (5, 7), (4, 7), (5, 8), (4, 8),
(5, 9), (4, 9), (5, 10), (4, 10), (5, 11), (4, 11),
(5, 12), (4, 12), (5, 13), (4, 13), (5, 14), (4, 14),
(5, 15), (4, 15), (5, 16), (4, 16), (5, 17), (4, 17),
(5, 18), (4, 18), (5, 19), (4, 19), (5, 20), (4, 20),
(3, 20), (2, 20), (3, 19), (2, 19), (3, 18), (2, 18),
(3, 17), (2, 17), (3, 16), (2, 16), (3, 15), (2, 15),
(3, 14), (2, 14), (3, 13), (2, 13), (3, 12), (2, 12),
(3, 11), (2, 11), (3, 10), (2, 10), (3, 9), (2, 9),
(3, 8), (2, 8), (3, 7), (2, 7), (3, 6), (2, 6),
(3, 5), (2, 5), (3, 4), (2, 4), (3, 3), (2, 3),
(3, 2), (2, 2), (3, 1), (2, 1), (3, 0), (2, 0),
(1, 0), (0, 0), (1, 1), (0, 1), (1, 2), (0, 2),
(1, 3), (0, 3), (1, 4), (0, 4), (1, 5), (0, 5),
(1, 6), (0, 6), (1, 7), (0, 7), (1, 8), (0, 8),
(1, 9), (0, 9), (1, 10), (0, 10), (1, 11), (0, 11),
(1, 12), (0, 12), (1, 13), (0, 13), (1, 14), (0, 14),
(1, 15), (0, 15), (1, 16), (0, 16), (1, 17), (0, 17),
(1, 18), (0, 18), (1, 19), (0, 19), (1, 20), (0, 20),
]);
}
#[test]
fn test_micro_qr() {
let res = DataModuleIter::new(Version::Micro(1)).collect::<Vec<(i16, i16)>>();
assert_eq!(res, vec![
(10, 10), (9, 10), (10, 9), (9, 9), (10, 8), (9, 8),
(10, 7), (9, 7), (10, 6), (9, 6), (10, 5), (9, 5),
(10, 4), (9, 4), (10, 3), (9, 3), (10, 2), (9, 2),
(10, 1), (9, 1), (10, 0), (9, 0),
(8, 0), (7, 0), (8, 1), (7, 1), (8, 2), (7, 2),
(8, 3), (7, 3), (8, 4), (7, 4), (8, 5), (7, 5),
(8, 6), (7, 6), (8, 7), (7, 7), (8, 8), (7, 8),
(8, 9), (7, 9), (8, 10), (7, 10),
(6, 10), (5, 10), (6, 9), (5, 9), (6, 8), (5, 8),
(6, 7), (5, 7), (6, 6), (5, 6), (6, 5), (5, 5),
(6, 4), (5, 4), (6, 3), (5, 3), (6, 2), (5, 2),
(6, 1), (5, 1), (6, 0), (5, 0),
(4, 0), (3, 0), (4, 1), (3, 1), (4, 2), (3, 2),
(4, 3), (3, 3), (4, 4), (3, 4), (4, 5), (3, 5),
(4, 6), (3, 6), (4, 7), (3, 7), (4, 8), (3, 8),
(4, 9), (3, 9), (4, 10), (3, 10),
(2, 10), (1, 10), (2, 9), (1, 9), (2, 8), (1, 8),
(2, 7), (1, 7), (2, 6), (1, 6), (2, 5), (1, 5),
(2, 4), (1, 4), (2, 3), (1, 3), (2, 2), (1, 2),
(2, 1), (1, 1), (2, 0), (1, 0),
]);
}
#[test]
fn test_micro_qr_2() {
let res = DataModuleIter::new(Version::Micro(2)).collect::<Vec<(i16, i16)>>();
assert_eq!(res, vec![
(12, 12), (11, 12), (12, 11), (11, 11), (12, 10), (11, 10),
(12, 9), (11, 9), (12, 8), (11, 8), (12, 7), (11, 7),
(12, 6), (11, 6), (12, 5), (11, 5), (12, 4), (11, 4),
(12, 3), (11, 3), (12, 2), (11, 2), (12, 1), (11, 1),
(12, 0), (11, 0),
(10, 0), (9, 0), (10, 1), (9, 1), (10, 2), (9, 2),
(10, 3), (9, 3), (10, 4), (9, 4), (10, 5), (9, 5),
(10, 6), (9, 6), (10, 7), (9, 7), (10, 8), (9, 8),
(10, 9), (9, 9), (10, 10), (9, 10), (10, 11), (9, 11),
(10, 12), (9, 12),
(8, 12), (7, 12), (8, 11), (7, 11), (8, 10), (7, 10),
(8, 9), (7, 9), (8, 8), (7, 8), (8, 7), (7, 7),
(8, 6), (7, 6), (8, 5), (7, 5), (8, 4), (7, 4),
(8, 3), (7, 3), (8, 2), (7, 2), (8, 1), (7, 1),
(8, 0), (7, 0),
(6, 0), (5, 0), (6, 1), (5, 1), (6, 2), (5, 2),
(6, 3), (5, 3), (6, 4), (5, 4), (6, 5), (5, 5),
(6, 6), (5, 6), (6, 7), (5, 7), (6, 8), (5, 8),
(6, 9), (5, 9), (6, 10), (5, 10), (6, 11), (5, 11),
(6, 12), (5, 12),
(4, 12), (3, 12), (4, 11), (3, 11), (4, 10), (3, 10),
(4, 9), (3, 9), (4, 8), (3, 8), (4, 7), (3, 7),
(4, 6), (3, 6), (4, 5), (3, 5), (4, 4), (3, 4),
(4, 3), (3, 3), (4, 2), (3, 2), (4, 1), (3, 1),
(4, 0), (3, 0),
(2, 0), (1, 0), (2, 1), (1, 1), (2, 2), (1, 2),
(2, 3), (1, 3), (2, 4), (1, 4), (2, 5), (1, 5),
(2, 6), (1, 6), (2, 7), (1, 7), (2, 8), (1, 8),
(2, 9), (1, 9), (2, 10), (1, 10), (2, 11), (1, 11),
(2, 12), (1, 12),
]);
}
}
impl Canvas {
fn draw_codewords<'a, I>(&mut self,
codewords: &[u8],
is_half_codeword_at_end: bool,
coords: &mut I)
where I: Iterator<Item=(i16, i16)>
{
let length = codewords.len();
let last_word = if is_half_codeword_at_end { length-1 } else { length };
for (i, b) in codewords.iter().enumerate() {
let bits_end = if i == last_word { 4 } else { 0 };
'outside:
for j in range_inclusive(bits_end, 7).rev() {
let color = if (*b & (1 << j)) != 0 {
Module::DarkUnmasked
} else {
Module::LightUnmasked
};
while let Some((x, y)) = coords.next() {
let r = self.get_mut(x, y);
if *r == Module::Empty {
*r = color;
continue 'outside;
}
}
return;
}
}
}
pub fn draw_data(&mut self, data: &[u8], ec: &[u8]) {
let is_half_codeword_at_end = match (self.version, self.ec_level) {
(Version::Micro(1), EcLevel::L) | (Version::Micro(3), EcLevel::M) => true,
_ => false,
};
let mut coords = DataModuleIter::new(self.version);
self.draw_codewords(data, is_half_codeword_at_end, &mut coords);
self.draw_codewords(ec, false, &mut coords);
}
}
#[cfg(test)]
mod draw_codewords_test {
use canvas::Canvas;
use types::{Version, EcLevel};
#[test]
fn test_micro_qr_1() {
let mut c = Canvas::new(Version::Micro(1), EcLevel::L);
c.draw_all_functional_patterns();
c.draw_data(b"\x6e\x5d\xe2", b"\x2b\x63");
assert_eq!(&*c.to_debug_str(), "\n\
#######.#.#\n\
#.....#..-*\n\
#.###.#..**\n\
#.###.#..*-\n\
#.###.#..**\n\
#.....#..*-\n\
#######..*-\n\
.........-*\n\
#........**\n\
.***-**---*\n\
#---*-*-**-");
}
#[test]
fn test_qr_2() {
let mut c = Canvas::new(Version::Normal(2), EcLevel::L);
c.draw_all_functional_patterns();
c.draw_data(b"\x92I$\x92I$\x92I$\x92I$\x92I$\x92I$\x92I$\x92I$\x92I$\x92I$\x92I$\x92I$\x92I$\x92I$\x92I$", b"");
assert_eq!(&*c.to_debug_str(), "\n\
#######..--*---*-.#######\n\
#.....#..-*-*-*-*.#.....#\n\
#.###.#..*---*---.#.###.#\n\
#.###.#..--*---*-.#.###.#\n\
#.###.#..-*-*-*-*.#.###.#\n\
#.....#..*---*---.#.....#\n\
#######.#.#.#.#.#.#######\n\
.........--*---*-........\n\
......#..-*-*-*-*........\n\
--*-*-.-**---*---*--**--*\n\
-*-*--#----*---*---------\n\
*----*.*--*-*-*-*-**--**-\n\
--*-*-#-**---*---*--**--*\n\
-*-*--.----*---*---------\n\
*----*#*--*-*-*-*-**--**-\n\
--*-*-.-**---*---*--**--*\n\
-*-*--#----*---*#####----\n\
........#-*-*-*-#...#-**-\n\
#######..*---*--#.#.#*--*\n\
#.....#..--*---*#...#----\n\
#.###.#..-*-*-*-#####-**-\n\
#.###.#..*---*--*----*--*\n\
#.###.#..--*------**-----\n\
#.....#..-*-*-**-*--*-**-\n\
#######..*---*--*----*--*");
}
}
#[derive(Debug, Copy, Clone)]
pub enum MaskPattern {
Checkerboard = 0b000,
HorizontalLines = 0b001,
VerticalLines = 0b010,
DiagonalLines = 0b011,
LargeCheckerboard = 0b100,
Fields = 0b101,
Diamonds = 0b110,
Meadow = 0b111,
}
mod mask_functions {
pub fn checkerboard(x: i16, y: i16) -> bool { (x + y) % 2 == 0 }
pub fn horizontal_lines(_: i16, y: i16) -> bool { y % 2 == 0 }
pub fn vertical_lines(x: i16, _: i16) -> bool { x % 3 == 0 }
pub fn diagonal_lines(x: i16, y: i16) -> bool { (x + y) % 3 == 0 }
pub fn large_checkerboard(x: i16, y: i16) -> bool { ((y/2) + (x/3)) % 2 == 0 }
pub fn fields(x: i16, y: i16) -> bool { (x*y)%2 + (x*y)%3 == 0 }
pub fn diamonds(x: i16, y: i16) -> bool { ((x*y)%2 + (x*y)%3) % 2 == 0 }
pub fn meadow(x: i16, y: i16) -> bool { ((x+y)%2 + (x*y)%3) % 2 == 0 }
}
fn get_mask_function(pattern: MaskPattern) -> fn(i16, i16) -> bool {
match pattern {
MaskPattern::Checkerboard => mask_functions::checkerboard,
MaskPattern::HorizontalLines => mask_functions::horizontal_lines,
MaskPattern::VerticalLines => mask_functions::vertical_lines,
MaskPattern::DiagonalLines => mask_functions::diagonal_lines,
MaskPattern::LargeCheckerboard => mask_functions::large_checkerboard,
MaskPattern::Fields => mask_functions::fields,
MaskPattern::Diamonds => mask_functions::diamonds,
MaskPattern::Meadow => mask_functions::meadow,
}
}
impl Canvas {
pub fn apply_mask(&mut self, pattern: MaskPattern) {
let mask_fn = get_mask_function(pattern);
for x in 0 .. self.width {
for y in 0 .. self.width {
let module = self.get_mut(x, y);
*module = module.mask(mask_fn(x, y));
}
}
self.draw_format_info_patterns(pattern);
}
fn draw_format_info_patterns(&mut self, pattern: MaskPattern) {
let format_number = match self.version {
Version::Normal(_) => {
let simple_format_number = ((self.ec_level as usize) ^ 1) << 3 | (pattern as usize);
FORMAT_INFOS_QR[simple_format_number]
}
Version::Micro(a) => {
let micro_pattern_number = match pattern {
MaskPattern::HorizontalLines => 0b00,
MaskPattern::LargeCheckerboard => 0b01,
MaskPattern::Diamonds => 0b10,
MaskPattern::Meadow => 0b11,
_ => panic!("Unsupported mask pattern in Micro QR code"),
};
let symbol_number = match (a, self.ec_level) {
(1, EcLevel::L) => 0b000,
(2, EcLevel::L) => 0b001,
(2, EcLevel::M) => 0b010,
(3, EcLevel::L) => 0b011,
(3, EcLevel::M) => 0b100,
(4, EcLevel::L) => 0b101,
(4, EcLevel::M) => 0b110,
(4, EcLevel::Q) => 0b111,
_ => panic!("Unsupported version/ec_level combination in Micro QR code"),
};
let simple_format_number = symbol_number << 2 | micro_pattern_number;
FORMAT_INFOS_MICRO_QR[simple_format_number]
}
};
self.draw_format_info_patterns_with_number(format_number << 1);
}
}
#[cfg(test)]
mod mask_tests {
use canvas::{Canvas, MaskPattern};
use types::{Version, EcLevel};
#[test]
fn test_apply_mask_qr() {
let mut c = Canvas::new(Version::Normal(1), EcLevel::L);
c.draw_all_functional_patterns();
c.apply_mask(MaskPattern::Checkerboard);
assert_eq!(&*c.to_debug_str(), "\n\
#######...#.#.#######\n\
#.....#..#.#..#.....#\n\
#.###.#.#.#.#.#.###.#\n\
#.###.#..#.#..#.###.#\n\
#.###.#...#.#.#.###.#\n\
#.....#..#.#..#.....#\n\
#######.#.#.#.#######\n\
........##.#.........\n\
###.#####.#.###...#..\n\
.#.#.#.#.#.#.#.#.#.#.\n\
#.#.#.#.#.#.#.#.#.#.#\n\
.#.#.#.#.#.#.#.#.#.#.\n\
#.#.#.#.#.#.#.#.#.#.#\n\
........##.#.#.#.#.#.\n\
#######.#.#.#.#.#.#.#\n\
#.....#.##.#.#.#.#.#.\n\
#.###.#.#.#.#.#.#.#.#\n\
#.###.#..#.#.#.#.#.#.\n\
#.###.#.#.#.#.#.#.#.#\n\
#.....#.##.#.#.#.#.#.\n\
#######.#.#.#.#.#.#.#");
}
#[test]
fn test_draw_format_info_patterns_qr() {
let mut c = Canvas::new(Version::Normal(1), EcLevel::L);
c.draw_format_info_patterns(MaskPattern::LargeCheckerboard);
assert_eq!(&*c.to_debug_str(), "\n\
????????#????????????\n\
????????#????????????\n\
????????#????????????\n\
????????#????????????\n\
????????.????????????\n\
????????#????????????\n\
?????????????????????\n\
????????.????????????\n\
##..##?..????..#.####\n\
?????????????????????\n\
?????????????????????\n\
?????????????????????\n\
?????????????????????\n\
????????#????????????\n\
????????.????????????\n\
????????#????????????\n\
????????#????????????\n\
????????.????????????\n\
????????.????????????\n\
????????#????????????\n\
????????#????????????");
}
#[test]
fn test_draw_format_info_patterns_micro_qr() {
let mut c = Canvas::new(Version::Micro(2), EcLevel::L);
c.draw_format_info_patterns(MaskPattern::LargeCheckerboard);
assert_eq!(&*c.to_debug_str(), "\n\
?????????????\n\
????????#????\n\
????????.????\n\
????????.????\n\
????????#????\n\
????????#????\n\
????????.????\n\
????????.????\n\
?#.#....#????\n\
?????????????\n\
?????????????\n\
?????????????\n\
?????????????");
}
}
static FORMAT_INFOS_QR: [u16; 32] = [
0x5412, 0x5125, 0x5e7c, 0x5b4b, 0x45f9, 0x40ce, 0x4f97, 0x4aa0,
0x77c4, 0x72f3, 0x7daa, 0x789d, 0x662f, 0x6318, 0x6c41, 0x6976,
0x1689, 0x13be, 0x1ce7, 0x19d0, 0x0762, 0x0255, 0x0d0c, 0x083b,
0x355f, 0x3068, 0x3f31, 0x3a06, 0x24b4, 0x2183, 0x2eda, 0x2bed,
];
static FORMAT_INFOS_MICRO_QR: [u16; 32] = [
0x4445, 0x4172, 0x4e2b, 0x4b1c, 0x55ae, 0x5099, 0x5fc0, 0x5af7,
0x6793, 0x62a4, 0x6dfd, 0x68ca, 0x7678, 0x734f, 0x7c16, 0x7921,
0x06de, 0x03e9, 0x0cb0, 0x0987, 0x1735, 0x1202, 0x1d5b, 0x186c,
0x2508, 0x203f, 0x2f66, 0x2a51, 0x34e3, 0x31d4, 0x3e8d, 0x3bba,
];
impl Canvas {
fn compute_adjacent_penalty_score(&self, is_horizontal: bool) -> u16 {
let mut total_score = 0;
for i in 0 .. self.width {
let map_fn = |j| if is_horizontal {
self.get(j, i)
} else {
self.get(i, j)
};
let colors = (0 .. self.width).map(map_fn)
.chain(Some(Module::Empty).into_iter());
let mut last_color = Module::Empty;
let mut consecutive_len = 1u16;
for color in colors {
if color == last_color {
consecutive_len += 1;
} else {
last_color = color;
if consecutive_len >= 5 {
total_score += consecutive_len - 2;
}
consecutive_len = 1;
}
}
}
total_score
}
fn compute_block_penalty_score(&self) -> u16 {
let mut total_score = 0;
for i in 0 .. self.width-1 {
for j in 0 .. self.width-1 {
let this = self.get(i, j);
let right = self.get(i+1, j);
let bottom = self.get(i, j+1);
let bottom_right = self.get(i+1, j+1);
if this == right && right == bottom && bottom == bottom_right {
total_score += 3;
}
}
}
total_score
}
fn compute_finder_penalty_score(&self, is_horizontal: bool) -> u16 {
static PATTERN: [Module; 7] = [
Module::Dark, Module::Light, Module::Dark, Module::Dark,
Module::Dark, Module::Light, Module::Dark,
];
fn equals<T, U>(left: T, right: U) -> bool
where T: Iterator, U: Iterator, T::Item: PartialEq<U::Item>
{
left.zip(right).all(|(p, q)| p == q)
}
let mut total_score = 0;
for i in 0 .. self.width {
for j in 0 .. self.width-6 {
let get: Box<Fn(i16) -> Module> = if is_horizontal {
Box::new(|k: i16| self.get(k, i))
} else {
Box::new(|k: i16| self.get(i, k))
};
if !equals((j .. j+7).map(|k| get(k)), PATTERN.iter().map(|m| *m)) {
continue;
}
let check = |k| { 0 <= k && k < self.width && get(k).is_dark() };
if !(j-4 .. j).any(|k| check(k)) || !(j+7 .. j+11).any(|k| check(k)) {
total_score += 40;
}
}
}
total_score - 360
}
fn compute_balance_penalty_score(&self) -> u16 {
let dark_modules = self.modules.iter().filter(|m| m.is_dark()).count();
let total_modules = self.modules.len();
let ratio = dark_modules * 200 / total_modules;
(if ratio >= 100 {
ratio - 100
} else {
100 - ratio
}) as u16
}
fn compute_light_side_penalty_score(&self) -> u16 {
let h = (1 .. self.width).filter(|j| !self.get(*j, -1).is_dark()).count();
let v = (1 .. self.width).filter(|j| !self.get(-1, *j).is_dark()).count();
(h + v + 15 * max(h, v)) as u16
}
fn compute_total_penalty_scores(&self) -> u16 {
match self.version {
Version::Normal(_) => {
let s1a = self.compute_adjacent_penalty_score(true);
let s1b = self.compute_adjacent_penalty_score(false);
let s2 = self.compute_block_penalty_score();
let s3a = self.compute_finder_penalty_score(true);
let s3b = self.compute_finder_penalty_score(false);
let s4 = self.compute_balance_penalty_score();
s1a + s1b + s2 + s3a + s3b + s4
}
Version::Micro(_) => self.compute_light_side_penalty_score(),
}
}
}
#[cfg(test)]
mod penalty_tests {
use canvas::{Canvas, MaskPattern, Module};
use types::{Version, EcLevel};
fn create_test_canvas() -> Canvas {
let mut c = Canvas::new(Version::Normal(1), EcLevel::Q);
c.draw_all_functional_patterns();
c.draw_data(b"\x20\x5b\x0b\x78\xd1\x72\xdc\x4d\x43\x40\xec\x11\x00",
b"\xa8\x48\x16\x52\xd9\x36\x9c\x00\x2e\x0f\xb4\x7a\x10");
c.apply_mask(MaskPattern::Checkerboard);
c
}
#[test]
fn check_penalty_canvas() {
let c = create_test_canvas();
assert_eq!(&*c.to_debug_str(), "\n\
#######.##....#######\n\
#.....#.#..#..#.....#\n\
#.###.#.#..##.#.###.#\n\
#.###.#.#.....#.###.#\n\
#.###.#.#.#...#.###.#\n\
#.....#...#...#.....#\n\
#######.#.#.#.#######\n\
........#............\n\
.##.#.##....#.#.#####\n\
.#......####....#...#\n\
..##.###.##...#.##...\n\
.##.##.#..##.#.#.###.\n\
#...#.#.#.###.###.#.#\n\
........##.#..#...#.#\n\
#######.#.#....#.##..\n\
#.....#..#.##.##.#...\n\
#.###.#.#.#...#######\n\
#.###.#..#.#.#.#...#.\n\
#.###.#.#...####.#..#\n\
#.....#.#.##.#...#.##\n\
#######.....####....#");
}
#[test]
fn test_penalty_score_adjacent() {
let c = create_test_canvas();
assert_eq!(c.compute_adjacent_penalty_score(true), 88);
assert_eq!(c.compute_adjacent_penalty_score(false), 92);
}
#[test]
fn test_penalty_score_block() {
let c = create_test_canvas();
assert_eq!(c.compute_block_penalty_score(), 90);
}
#[test]
fn test_penalty_score_finder() {
let c = create_test_canvas();
assert_eq!(c.compute_finder_penalty_score(true), 0);
assert_eq!(c.compute_finder_penalty_score(false), 40);
}
#[test]
fn test_penalty_score_balance() {
let c = create_test_canvas();
assert_eq!(c.compute_balance_penalty_score(), 2);
}
#[test]
fn test_penalty_score_light_sides() {
static HORIZONTAL_SIDE: [Module; 17] = [
Module::Dark, Module::Light, Module::Light, Module::Dark,
Module::Dark, Module::Dark, Module::Light, Module::Light,
Module::Dark, Module::Light, Module::Dark, Module::Light,
Module::Light, Module::Dark, Module::Light, Module::Light,
Module::Light,
];
static VERTICAL_SIDE: [Module; 17] = [
Module::Dark, Module::Dark, Module::Dark, Module::Light,
Module::Light, Module::Dark, Module::Dark, Module::Light,
Module::Dark, Module::Light, Module::Dark, Module::Light,
Module::Dark, Module::Light, Module::Light, Module::Dark,
Module::Light,
];
let mut c = Canvas::new(Version::Micro(4), EcLevel::Q);
for i in 0i16 .. 17 {
c.put(i, -1, HORIZONTAL_SIDE[i as usize]);
c.put(-1, i, VERTICAL_SIDE[i as usize]);
}
assert_eq!(c.compute_light_side_penalty_score(), 168);
}
}
static ALL_PATTERNS_QR: [MaskPattern; 8] = [
MaskPattern::Checkerboard, MaskPattern::HorizontalLines,
MaskPattern::VerticalLines, MaskPattern::DiagonalLines,
MaskPattern::LargeCheckerboard, MaskPattern::Fields,
MaskPattern::Diamonds, MaskPattern::Meadow,
];
static ALL_PATTERNS_MICRO_QR: [MaskPattern; 4] = [
MaskPattern::HorizontalLines, MaskPattern::LargeCheckerboard,
MaskPattern::Diamonds, MaskPattern::Meadow,
];
impl Canvas {
pub fn apply_best_mask(&self) -> Canvas {
let patterns = match self.version {
Version::Normal(_) => ALL_PATTERNS_QR.iter(),
Version::Micro(_) => ALL_PATTERNS_MICRO_QR.iter(),
};
let mut patterns = patterns.map(|ptn| {
let mut c = self.clone();
c.apply_mask(*ptn);
c
});
let mut best_pattern = patterns.next().unwrap();
let mut best_pattern_score = best_pattern.compute_total_penalty_scores();
for c in patterns {
let score = c.compute_total_penalty_scores();
if score < best_pattern_score {
best_pattern = c;
best_pattern_score = score;
}
}
best_pattern
}
pub fn to_bools(&self) -> Vec<bool> {
self.modules.iter().map(|m| m.is_dark()).collect()
}
}