devmandan's picture
PNG files that say how they were made, checked in your browser
de7cd1e verified
Raw History Blame Contribute Delete
5.68 kB
// A code's squares, as a file's record says they were meant and as zxing-cpp read them: each packed the same way (1
// dark, row by row, 8 to a byte from the high bit, no row padding, the last byte zero-filled, in base64), and what the
// QR standard fixes in them: the format and version bits, and the patterns every code has in the same place. The
// standard's tables come from record.json ("standard", from ISO/IEC 18004). Nothing is sent or stored.
import { squares } from './plan.js';
const FINDER = 7; // a corner pattern is 7 × 7 squares, and with its light border 8 × 8
const ALIGN = 2; // an alignment pattern reaches 2 squares round its centre (5 × 5)
const VERSIONS = 40; // the QR standard's versions, 1 to 40
/** The bytes of a base64 text, or null when it isn't one. */
function bytesOf(b64) {
if (typeof b64 !== 'string') return null;
try {
return Uint8Array.from(atob(b64), (c) => c.charCodeAt(0));
} catch {
return null;
}
}
/** The side of the code whose packed squares this base64 holds: the n whose ceil(n² / 8) bytes it decodes to (that
* count is different for every version), or null. */
export function sideOf(b64) {
const raw = bytesOf(b64);
for (let v = 1; raw && v <= VERSIONS; v += 1) if (Math.ceil(squares(v) ** 2 / 8) === raw.length) return squares(v);
return null;
}
/** The n × n squares (0 light, 1 dark, row by row) a base64 text packs; null unless it holds exactly n × n squares'
* bytes and, with pad, its pad bits are all 0. */
export function unpack(b64, n, pad = true) {
const raw = bytesOf(b64);
if (!raw || raw.length !== Math.ceil((n * n) / 8)) return null;
const bit = (i) => (raw[i >> 3] >> (7 - (i & 7))) & 1;
for (let i = n * n; pad && i < 8 * raw.length; i += 1) if (bit(i)) return null;
return Uint8Array.from({ length: n * n }, (_, i) => bit(i));
}
/** The number the squares at these (row, column) cells make, the first cell its highest bit. */
const bitsAt = (m, n, cells) => cells.reduce((x, [r, c]) => x * 2 + m[r * n + c], 0);
/** { ec, mask } from both copies of the squares' format bits, read in zxing's order, or null unless both are the same
* codeword of the standard's table exactly: a code as meant has no wrong bits. S: record.json's "standard". */
export function formatOf(m, n, S) {
const one = [0, 1, 2, 3, 4, 5, 7, 8].map((c) => [8, c]).concat([7, 5, 4, 3, 2, 1, 0].map((r) => [r, 8]));
const two = [0, 1, 2, 3, 4, 5, 6].map((k) => [n - 1 - k, 8]).concat([8, 7, 6, 5, 4, 3, 2, 1].map((k) => [8, n - k]));
const a = bitsAt(m, n, one);
const i = a === bitsAt(m, n, two) ? S.format.indexOf(a) : -1;
if (i < 0) return null;
return { ec: Object.keys(S.format_ec).find((k) => S.format_ec[k] === i >> 3), mask: i & 7 };
}
/** Whether both blocks of the n × n squares' version bits hold the standard's codeword for version v (only the
* versions in its table have them). */
export function versionOk(m, n, S, v) {
const want = S.version[String(v)];
if (want == null) return true;
const top = [];
const left = [];
for (let a = 5; a >= 0; a -= 1) {
for (let b = n - 9; b >= n - 11; b -= 1) {
top.push([a, b]);
left.push([b, a]);
}
}
return bitsAt(m, n, top) === want && bitsAt(m, n, left) === want;
}
/** Each square's fixed pattern: 1 in a corner pattern, 2 in the other patterns every code has in the same place (the
* corner patterns' light borders, the timing lines, the alignment patterns and the one square always dark), else 0.
* The format and version bits aren't fixed patterns: their values vary. */
function patterns(n, centres) {
const p = new Uint8Array(n * n);
const fill = (r0, c0, size, value) => {
for (let r = Math.max(0, r0); r < Math.min(n, r0 + size); r += 1) {
for (let c = Math.max(0, c0); c < Math.min(n, c0 + size); c += 1) if (!p[r * n + c]) p[r * n + c] = value;
}
};
const far = n - FINDER;
for (const [r, c] of [[0, 0], [0, far], [far, 0]]) fill(r, c, FINDER, 1);
for (const [r, c] of [[0, 0], [0, far - 1], [far - 1, 0]]) fill(r, c, FINDER + 1, 2);
for (let k = FINDER + 1; k < far - 1; k += 1) {
p[(FINDER - 1) * n + k] = 2;
p[k * n + FINDER - 1] = 2;
}
const [first, last] = [centres[0], centres[centres.length - 1]];
for (const r of centres) {
for (const c of centres) {
const corner = (r === first && (c === first || c === last)) || (r === last && c === first);
if (!corner) fill(r - ALIGN, c - ALIGN, 2 * ALIGN + 1, 2);
}
}
p[(far - 1) * n + FINDER + 1] = 2;
return p;
}
/** The square check: the squares meant (the record's) against those read, both n × n, also against the read ones
* transposed, since zxing-cpp returns a mirrored code transposed. mirrored: a better match transposed. Over the
* better match: wrong, the squares that differ; f, those in the corner patterns; g, those in the other fixed
* patterns. k and kT: the counts as read and transposed. */
export function compare(meant, seen, n, v, S) {
let k = 0;
let kT = 0;
for (let r = 0; r < n; r += 1) {
for (let c = 0; c < n; c += 1) {
if (meant[r * n + c] !== seen[r * n + c]) k += 1;
if (meant[r * n + c] !== seen[c * n + r]) kT += 1;
}
}
const mirrored = kT < k;
const fixed = patterns(n, S.alignment[String(v)] || []);
let f = 0;
let g = 0;
for (let r = 0; r < n; r += 1) {
for (let c = 0; c < n; c += 1) {
const read = mirrored ? seen[c * n + r] : seen[r * n + c];
if (meant[r * n + c] !== read) {
if (fixed[r * n + c] === 1) f += 1;
else if (fixed[r * n + c] === 2) g += 1;
}
}
}
return { N: n * n, k, kT, mirrored, wrong: Math.min(k, kT), f, g };
}