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)
return 0;
int numAlign = version / 7 + 2;
int step = (version == 32) ? 26 :
(version * 4 + numAlign * 2 + 1) / (numAlign * 2 - 2) * 2;
int step = (version * 8 + numAlign * 3 + 5) / (numAlign * 4 - 4) * 2;
for (int i = numAlign - 1, pos = version * 4 + 10; i >= 1; i--, pos -= step)
result[i] = (uint8_t)pos;
result[0] = 6;

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

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

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

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

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

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

Loading…
Cancel
Save