Slightly simplified the calculation of alignment pattern spacing in a non-obvious way, adapted from David Evans's "Lean QR" project.

pull/207/head
Project Nayuki 3 months ago
parent 42a886d784
commit 8329a7108f

@ -554,8 +554,7 @@ testable int getAlignmentPatternPositions(int version, uint8_t result[7]) {
if (version == 1) if (version == 1)
return 0; return 0;
int numAlign = version / 7 + 2; int numAlign = version / 7 + 2;
int step = (version == 32) ? 26 : int step = (version * 8 + numAlign * 3 + 5) / (numAlign * 4 - 4) * 2;
(version * 4 + numAlign * 2 + 1) / (numAlign * 2 - 2) * 2;
for (int i = numAlign - 1, pos = version * 4 + 10; i >= 1; i--, pos -= step) for (int i = numAlign - 1, pos = version * 4 + 10; i >= 1; i--, pos -= step)
result[i] = (uint8_t)pos; result[i] = (uint8_t)pos;
result[0] = 6; result[0] = 6;

@ -666,8 +666,7 @@ vector<int> QrCode::getAlignmentPatternPositions() const {
return vector<int>(); return vector<int>();
else { else {
int numAlign = version / 7 + 2; int numAlign = version / 7 + 2;
int step = (version == 32) ? 26 : int step = (version * 8 + numAlign * 3 + 5) / (numAlign * 4 - 4) * 2;
(version * 4 + numAlign * 2 + 1) / (numAlign * 2 - 2) * 2;
vector<int> result; vector<int> result;
for (int i = 0, pos = size - 7; i < numAlign - 1; i++, pos -= step) for (int i = 0, pos = size - 7; i < numAlign - 1; i++, pos -= step)
result.insert(result.begin(), pos); result.insert(result.begin(), pos);

@ -240,8 +240,7 @@ final class QrTemplate {
return new int[]{}; return new int[]{};
else { else {
int numAlign = version / 7 + 2; int numAlign = version / 7 + 2;
int step = (version == 32) ? 26 : int step = (version * 8 + numAlign * 3 + 5) / (numAlign * 4 - 4) * 2;
(version * 4 + numAlign * 2 + 1) / (numAlign * 2 - 2) * 2;
int[] result = new int[numAlign]; int[] result = new int[numAlign];
result[0] = 6; result[0] = 6;
for (int i = result.length - 1, pos = size - 7; i >= 1; i--, pos -= step) for (int i = result.length - 1, pos = size - 7; i >= 1; i--, pos -= step)

@ -612,11 +612,7 @@ public final class QrCode {
return new int[]{}; return new int[]{};
else { else {
int numAlign = version / 7 + 2; int numAlign = version / 7 + 2;
int step; int step = (version * 8 + numAlign * 3 + 5) / (numAlign * 4 - 4) * 2;
if (version == 32) // Special snowflake
step = 26;
else // step = ceil[(size - 13) / (numAlign * 2 - 2)] * 2
step = (version * 4 + numAlign * 2 + 1) / (numAlign * 2 - 2) * 2;
int[] result = new int[numAlign]; int[] result = new int[numAlign];
result[0] = 6; result[0] = 6;
for (int i = result.length - 1, pos = size - 7; i >= 1; i--, pos -= step) for (int i = result.length - 1, pos = size - 7; i >= 1; i--, pos -= step)

@ -484,8 +484,7 @@ class QrCode:
return [] return []
else: else:
numalign: int = ver // 7 + 2 numalign: int = ver // 7 + 2
step: int = 26 if (ver == 32) else \ step: int = (ver * 8 + numalign * 3 + 8) // (numalign * 4 - 4) * 2
(ver * 4 + numalign * 2 + 1) // (numalign * 2 - 2) * 2
result: list[int] = [(self._size - 7 - i * step) for i in range(numalign - 1)] + [6] result: list[int] = [(self._size - 7 - i * step) for i in range(numalign - 1)] + [6]
return list(reversed(result)) return list(reversed(result))

@ -796,8 +796,8 @@ impl<'a> QrCode<'a> {
&resultbuf[ .. 0] &resultbuf[ .. 0]
} else { } else {
let numalign: u8 = ver / 7 + 2; let numalign: u8 = ver / 7 + 2;
let step: u8 = if ver == 32 { 26 } else let step = u8::try_from((i32::from(ver) * 8 + i32::from(numalign) * 3 + 5)
{(ver * 4 + numalign * 2 + 1) / (numalign * 2 - 2) * 2}; / (i32::from(numalign) * 4 - 4) * 2).unwrap();
let result = &mut resultbuf[ .. usize::from(numalign)]; let result = &mut resultbuf[ .. usize::from(numalign)];
for i in 0 .. numalign-1 { for i in 0 .. numalign-1 {
result[usize::from(i)] = *self.size - 7 - i * step; result[usize::from(i)] = *self.size - 7 - i * step;

@ -701,8 +701,7 @@ impl QrCode {
vec![] vec![]
} else { } else {
let numalign = i32::from(ver) / 7 + 2; let numalign = i32::from(ver) / 7 + 2;
let step: i32 = if ver == 32 { 26 } else let step: i32 = (i32::from(ver) * 8 + numalign * 3 + 5) / (numalign * 4 - 4) * 2;
{(i32::from(ver) * 4 + numalign * 2 + 1) / (numalign * 2 - 2) * 2};
let mut result: Vec<i32> = (0 .. numalign-1).map( let mut result: Vec<i32> = (0 .. numalign-1).map(
|i| self.size - 7 - i * step).collect(); |i| self.size - 7 - i * step).collect();
result.push(6); result.push(6);

@ -540,8 +540,7 @@ namespace qrcodegen {
return []; return [];
else { else {
const numAlign: int = Math.floor(this.version / 7) + 2; const numAlign: int = Math.floor(this.version / 7) + 2;
const step: int = (this.version == 32) ? 26 : const step: int = Math.floor((this.version * 8 + numAlign * 3 + 5) / (numAlign * 4 - 4)) * 2;
Math.ceil((this.version * 4 + 4) / (numAlign * 2 - 2)) * 2;
let result: Array<int> = [6]; let result: Array<int> = [6];
for (let pos = this.size - 7; result.length < numAlign; pos -= step) for (let pos = this.size - 7; result.length < numAlign; pos -= step)
result.splice(1, 0, pos); result.splice(1, 0, pos);

Loading…
Cancel
Save