From 8329a7108fc22be3e1eec0a9f9318978579e3621 Mon Sep 17 00:00:00 2001 From: Project Nayuki Date: Sun, 1 Sep 2024 01:43:33 +0000 Subject: [PATCH] Slightly simplified the calculation of alignment pattern spacing in a non-obvious way, adapted from David Evans's "Lean QR" project. --- c/qrcodegen.c | 3 +-- cpp/qrcodegen.cpp | 3 +-- java-fast/io/nayuki/fastqrcodegen/QrTemplate.java | 3 +-- java/src/main/java/io/nayuki/qrcodegen/QrCode.java | 6 +----- python/qrcodegen.py | 3 +-- rust-no-heap/src/lib.rs | 4 ++-- rust/src/lib.rs | 3 +-- typescript-javascript/qrcodegen.ts | 3 +-- 8 files changed, 9 insertions(+), 19 deletions(-) diff --git a/c/qrcodegen.c b/c/qrcodegen.c index 871bc92..34f1002 100644 --- a/c/qrcodegen.c +++ b/c/qrcodegen.c @@ -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; diff --git a/cpp/qrcodegen.cpp b/cpp/qrcodegen.cpp index 0957b79..9aa91b4 100644 --- a/cpp/qrcodegen.cpp +++ b/cpp/qrcodegen.cpp @@ -666,8 +666,7 @@ vector QrCode::getAlignmentPatternPositions() const { return vector(); 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 result; for (int i = 0, pos = size - 7; i < numAlign - 1; i++, pos -= step) result.insert(result.begin(), pos); diff --git a/java-fast/io/nayuki/fastqrcodegen/QrTemplate.java b/java-fast/io/nayuki/fastqrcodegen/QrTemplate.java index b841da2..73d59d6 100644 --- a/java-fast/io/nayuki/fastqrcodegen/QrTemplate.java +++ b/java-fast/io/nayuki/fastqrcodegen/QrTemplate.java @@ -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) diff --git a/java/src/main/java/io/nayuki/qrcodegen/QrCode.java b/java/src/main/java/io/nayuki/qrcodegen/QrCode.java index 46f5d75..f0bafbb 100644 --- a/java/src/main/java/io/nayuki/qrcodegen/QrCode.java +++ b/java/src/main/java/io/nayuki/qrcodegen/QrCode.java @@ -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) diff --git a/python/qrcodegen.py b/python/qrcodegen.py index 26ddf0d..3d0a0fc 100644 --- a/python/qrcodegen.py +++ b/python/qrcodegen.py @@ -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)) diff --git a/rust-no-heap/src/lib.rs b/rust-no-heap/src/lib.rs index 17a42ee..48da775 100644 --- a/rust-no-heap/src/lib.rs +++ b/rust-no-heap/src/lib.rs @@ -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; diff --git a/rust/src/lib.rs b/rust/src/lib.rs index b4f2aaf..ad5aecc 100644 --- a/rust/src/lib.rs +++ b/rust/src/lib.rs @@ -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 = (0 .. numalign-1).map( |i| self.size - 7 - i * step).collect(); result.push(6); diff --git a/typescript-javascript/qrcodegen.ts b/typescript-javascript/qrcodegen.ts index 9e68032..c4191ac 100644 --- a/typescript-javascript/qrcodegen.ts +++ b/typescript-javascript/qrcodegen.ts @@ -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 = [6]; for (let pos = this.size - 7; result.length < numAlign; pos -= step) result.splice(1, 0, pos);