Refactored Rust code to collect some private functions into new private struct.

pull/76/head
Project Nayuki 5 years ago
parent 29119d3f2e
commit 6ea933f159

@ -647,7 +647,7 @@ impl QrCode {
for y in 0 .. size { for y in 0 .. size {
let mut runcolor = false; let mut runcolor = false;
let mut runx: i32 = 0; let mut runx: i32 = 0;
let mut runhistory = [0i32; 7]; let mut runhistory = FinderPenalty::new(size);
let mut padrun = size; // Add white border to initial run let mut padrun = size; // Add white border to initial run
for x in 0 .. size { for x in 0 .. size {
if self.module(x, y) == runcolor { if self.module(x, y) == runcolor {
@ -658,22 +658,22 @@ impl QrCode {
result += 1; result += 1;
} }
} else { } else {
QrCode::finder_penalty_add_history(runx + padrun, &mut runhistory); runhistory.add_history(runx + padrun);
padrun = 0; padrun = 0;
if !runcolor { if !runcolor {
result += self.finder_penalty_count_patterns(&runhistory) * PENALTY_N3; result += runhistory.count_patterns() * PENALTY_N3;
} }
runcolor = self.module(x, y); runcolor = self.module(x, y);
runx = 1; runx = 1;
} }
} }
result += self.finder_penalty_terminate_and_count(runcolor, runx + padrun, &mut runhistory) * PENALTY_N3; result += runhistory.terminate_and_count(runcolor, runx + padrun) * PENALTY_N3;
} }
// Adjacent modules in column having same color, and finder-like patterns // Adjacent modules in column having same color, and finder-like patterns
for x in 0 .. size { for x in 0 .. size {
let mut runcolor = false; let mut runcolor = false;
let mut runy: i32 = 0; let mut runy: i32 = 0;
let mut runhistory = [0i32; 7]; let mut runhistory = FinderPenalty::new(size);
let mut padrun = size; // Add white border to initial run let mut padrun = size; // Add white border to initial run
for y in 0 .. size { for y in 0 .. size {
if self.module(x, y) == runcolor { if self.module(x, y) == runcolor {
@ -684,16 +684,16 @@ impl QrCode {
result += 1; result += 1;
} }
} else { } else {
QrCode::finder_penalty_add_history(runy + padrun, &mut runhistory); runhistory.add_history(runy + padrun);
padrun = 0; padrun = 0;
if !runcolor { if !runcolor {
result += self.finder_penalty_count_patterns(&runhistory) * PENALTY_N3; result += runhistory.count_patterns() * PENALTY_N3;
} }
runcolor = self.module(x, y); runcolor = self.module(x, y);
runy = 1; runy = 1;
} }
} }
result += self.finder_penalty_terminate_and_count(runcolor, runy + padrun, &mut runhistory) * PENALTY_N3; result += runhistory.terminate_and_count(runcolor, runy + padrun) * PENALTY_N3;
} }
// 2*2 blocks of modules having same color // 2*2 blocks of modules having same color
@ -827,36 +827,57 @@ impl QrCode {
z z
} }
}
/*---- Helper struct for get_penalty_score() ----*/
struct FinderPenalty {
qr_size: i32,
run_history: [i32; 7],
}
// Can only be called immediately after a white run is added, and impl FinderPenalty {
// returns either 0, 1, or 2. A helper function for get_penalty_score().
fn finder_penalty_count_patterns(&self, runhistory: &[i32;7]) -> i32 { pub fn new(size: i32) -> Self {
let n = runhistory[1]; Self {
assert!(n <= self.size * 3); qr_size: size,
let core = n > 0 && runhistory[2] == n && runhistory[3] == n * 3 && runhistory[4] == n && runhistory[5] == n; run_history: [0i32; 7],
( i32::from(core && runhistory[0] >= n * 4 && runhistory[6] >= n) }
+ i32::from(core && runhistory[6] >= n * 4 && runhistory[0] >= n))
} }
// Must be called at the end of a line (row or column) of modules. A helper function for get_penalty_score(). // Pushes the given value to the front and drops the last value.
fn finder_penalty_terminate_and_count(&self, currentruncolor: bool, mut currentrunlength: i32, runhistory: &mut [i32;7]) -> i32 { pub fn add_history(&mut self, currentrunlength: i32) {
if currentruncolor { // Terminate black run let rh = &mut self.run_history;
QrCode::finder_penalty_add_history(currentrunlength, runhistory); for i in (0 .. rh.len()-1).rev() {
currentrunlength = 0; rh[i + 1] = rh[i];
} }
currentrunlength += self.size; // Add white border to final run rh[0] = currentrunlength;
QrCode::finder_penalty_add_history(currentrunlength, runhistory);
self.finder_penalty_count_patterns(runhistory)
} }
// Pushes the given value to the front and drops the last value. A helper function for get_penalty_score(). // Can only be called immediately after a white run is added, and returns either 0, 1, or 2.
fn finder_penalty_add_history(currentrunlength: i32, runhistory: &mut [i32;7]) { pub fn count_patterns(&self) -> i32 {
for i in (0 .. runhistory.len()-1).rev() { let rh = &self.run_history;
runhistory[i + 1] = runhistory[i]; let n = rh[1];
assert!(n <= self.qr_size * 3);
let core = n > 0 && rh[2] == n && rh[3] == n * 3 && rh[4] == n && rh[5] == n;
( i32::from(core && rh[0] >= n * 4 && rh[6] >= n)
+ i32::from(core && rh[6] >= n * 4 && rh[0] >= n))
}
// Must be called at the end of a line (row or column) of modules.
pub fn terminate_and_count(mut self, currentruncolor: bool, mut currentrunlength: i32) -> i32 {
if currentruncolor { // Terminate black run
self.add_history(currentrunlength);
currentrunlength = 0;
} }
runhistory[0] = currentrunlength; currentrunlength += self.qr_size; // Add white border to final run
self.add_history(currentrunlength);
self.count_patterns()
} }
} }

Loading…
Cancel
Save