-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdebug_qr_decoder.js
More file actions
315 lines (257 loc) · 11.3 KB
/
debug_qr_decoder.js
File metadata and controls
315 lines (257 loc) · 11.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
import { generateColourQr } from './dist/generator.js';
import sharp from 'sharp';
// Debug the QR decoder directly
async function debugQRDecoder() {
console.log('🧪 Debugging QR decoder...');
const testText = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
console.log('Test text:', testText);
try {
// Generate SPQR
const result = await generateColourQr(testText, {
colours: ['#ffffff', '#ff0000', '#00ff00', '#000000'],
composition: 'discrete',
layers: 2
});
console.log('✅ Generated SPQR');
// Convert SVG to PNG buffer
const svgBuffer = Buffer.from(result.svg);
const pngBuffer = await sharp(svgBuffer).png().toBuffer();
// Create ImageData-like object
const image = await sharp(pngBuffer).raw().toBuffer({ resolveWithObject: true });
const { data, info } = image;
const { width, height } = info;
console.log(`Image: ${width}x${height}`);
// Test the exact same logic as the web decoder
const decoded = await decodeSPQRWeb(data, width, height);
console.log('Decoded result:', decoded);
} catch (error) {
console.error('❌ Debug failed:', error.message);
}
}
// Simplified SPQR decoder for debugging
async function decodeSPQRWeb(data, width, height) {
console.log('Starting SPQR decode...');
// Color classification thresholds
const isBlackRGB = (r,g,b) => r < 128 && g < 128 && b < 128;
const isWhiteRGB = (r,g,b) => r > 200 && g > 200 && b > 200;
const classifyTag = (r,g,b) => {
if (isBlackRGB(r,g,b)) return 'BLACK';
if (isWhiteRGB(r,g,b)) return 'WHITE';
const redExcess = r - Math.max(g,b);
const greenExcess = g - Math.max(r,b);
if (redExcess > 35 && r > 120 && g < 220) return 'RED';
if (greenExcess > 35 && g > 120 && r < 220) return 'GREEN';
if (redExcess > 28 && greenExcess > 28) return 'YELLOW';
if (r - g > 20 && r - b > 20) return 'RED';
if (g - r > 20 && g - b > 20) return 'GREEN';
return 'WHITE';
};
// Simple structure detection
const structure = {
modulePx: 5,
originX: 20,
originY: 20,
modules: 21
};
console.log('QR structure:', structure);
const { modulePx, originX, originY, modules } = structure;
const margin = 4;
// Build base and red matrices
const baseMods = Array.from({ length: modules }, () => Array(modules).fill(false));
const redMods = Array.from({ length: modules }, () => Array(modules).fill(false));
const markFinder = (mx, my) => (mx < 7 && my < 7) || (mx >= modules - 7 && my < 7) || (mx < 7 && my >= modules - 7);
let processedCount = 0;
let skippedCount = 0;
for (let my = 0; my < modules; my++) {
for (let mx = 0; mx < modules; mx++) {
if (markFinder(mx, my)) {
skippedCount++;
continue;
}
processedCount++;
// Sample color (simplified version)
const cx = originX + (margin + mx) * modulePx + modulePx / 2;
const cy = originY + (margin + my) * modulePx + modulePx / 2;
const px = Math.round(cx), py = Math.round(cy);
const idx = (py * width + px) * 4;
const r = data[idx], g = data[idx + 1], b = data[idx + 2];
const tag = classifyTag(r, g, b);
// Debug: check what colors are in finder pattern areas
const finderArea = (mx < 7 && my < 7) || (mx >= 14 && my < 7) || (mx < 7 && my >= 14);
if (finderArea && my === 0 && mx < 7) {
console.log(`FINDER Module (${mx},${my}): RGB(${r},${g},${b}) at (${px},${py}) → ${tag}`);
}
// Map to layers
let baseBit = 0, redBit = 0;
if (tag === 'WHITE') {
baseBit = 0; redBit = 0;
} else if (tag === 'RED') {
baseBit = 0; redBit = 1;
} else if (tag === 'BLACK') {
baseBit = 1; redBit = 0;
} else if (tag === 'GREEN') {
baseBit = 1; redBit = 1;
}
baseMods[my][mx] = !!baseBit;
redMods[my][mx] = !!redBit;
// Debug: log color mapping for first few modules
if (my < 2 && mx < 15) {
const bits = (baseBit << 1) | redBit;
console.log(`Module (${mx},${my}): RGB(${r},${g},${b}) → ${tag} → base=${baseBit}, red=${redBit}, bits=${bits}`);
}
// Debug: check finder pattern areas
const isFinderArea = (mx < 7 && my < 7) || (mx >= 14 && my < 7) || (mx < 7 && my >= 14);
if (isFinderArea && my === 0 && mx < 7) {
console.log(`FINDER Module (${mx},${my}): RGB(${r},${g},${b}) → ${tag} → base=${baseBit}, red=${redBit}`);
}
}
}
console.log(`Processed ${processedCount} modules, skipped ${skippedCount} finder modules`);
// Show matrix patterns
console.log('Base matrix row 0:', baseMods[0].map(v => (v ? 1 : 0)).join(''));
console.log('Red matrix row 0:', redMods[0].map(v => (v ? 1 : 0)).join(''));
// Test QR decoding on both matrices
console.log('\n=== Testing QR Decoder ===');
try {
const baseResult = decodeMatrix(baseMods);
console.log('Base decode result:', baseResult);
} catch (e) {
console.log('Base decode error:', e.message);
}
try {
const redResult = decodeMatrix(redMods);
console.log('Red decode result:', redResult);
} catch (e) {
console.log('Red decode error:', e.message);
}
// Use SPQR direct decoding
const spqrBits = extractSPQRColorBitsDirect(data, width, height, originX, originY, modulePx, modules);
console.log(`SPQR: Extracted ${spqrBits.length} bits for direct decoding`);
if (spqrBits.length > 0) {
const combinedText = decodeSPQRBitsDirect(spqrBits);
if (combinedText) {
console.log('SPQR decode successful:', combinedText);
return {
base: null,
red: null,
combined: combinedText
};
}
}
console.log('SPQR direct decoding failed, trying matrix-based approach...');
return { base: null, red: null, combined: null };
}
// Simplified QR decoder for testing
function decodeMatrix(mods) {
if (!mods || !mods[0]) return null;
// Convert to binary string
const binary = mods.map(row => row.map(v => v ? '1' : '0').join('')).join('');
// Check if it looks like a valid QR code (basic check)
if (binary.length !== 441) { // 21x21
throw new Error(`Invalid matrix size: ${binary.length} bits`);
}
// Check for finder patterns (basic check)
const hasFinderPattern = binary.substring(0, 7) === '1111111' ||
binary.substring(434, 441) === '1111111' ||
binary.substring(0, 7) + binary.substring(21, 28) + binary.substring(42, 49) === '111111111111111111111111';
if (!hasFinderPattern) {
throw new Error('No finder pattern found');
}
return 'DECODED:' + binary.substring(0, 50) + '...';
}
// SPQR direct decoding functions
function extractSPQRColorBitsDirect(data, width, height, originX, originY, modulePx, modules) {
const bits = [];
let dataModules = 0;
// Color classification
const isBlackRGB = (r,g,b) => r < 128 && g < 128 && b < 128;
const isWhiteRGB = (r,g,b) => r > 200 && g > 200 && b > 200;
const classifyColor = (r,g,b) => {
if (isBlackRGB(r,g,b)) return 'BLACK';
if (isWhiteRGB(r,g,b)) return 'WHITE';
const redExcess = r - Math.max(g,b);
const greenExcess = g - Math.max(r,b);
if (redExcess > 35 && r > 120 && g < 220) return 'RED';
if (greenExcess > 35 && g > 120 && r < 220) return 'GREEN';
return 'WHITE';
};
// Use proper QR code reading order (zigzag pattern)
let readingUp = true;
for (let col = modules - 1; col > 0; col -= 2) {
if (col === 6) col--; // Skip vertical timing pattern
for (let i = 0; i < modules; i++) {
const y = readingUp ? (modules - 1 - i) : i;
for (let dx = 0; dx < 2; dx++) {
const x = col - dx;
// Skip structure areas
const isStructure = (x < 7 && y < 7) || (x >= modules - 7 && y < 7) || (x < 7 && y >= modules - 7) ||
(y === 8 && x <= 8 && x !== 6) || (x === 8 && y <= 7 && y !== 6) ||
(y === 8 && x >= modules - 8) || (x === 8 && y >= modules - 8 && y !== modules - 1);
if (!isStructure) {
// Sample color
const cx = originX + x * modulePx + modulePx / 2;
const cy = originY + y * modulePx + modulePx / 2;
const px = Math.round(cx), py = Math.round(cy);
const idx = (py * width + px) * 4;
const r = data[idx], g = data[idx + 1], b = data[idx + 2];
const color = classifyColor(r, g, b);
// Map to bits
let baseBit = 0, redBit = 0;
if (color === 'BLACK') {
baseBit = 1; redBit = 0;
} else if (color === 'RED') {
baseBit = 0; redBit = 1;
} else if (color === 'GREEN') {
baseBit = 1; redBit = 1;
}
bits.push(baseBit, redBit);
dataModules++;
if (dataModules <= 10) {
console.log(`SPQR Color Module (${x},${y}): RGB(${r},${g},${b}) → ${color} → bits=${(baseBit << 1) | redBit}`);
}
}
}
}
readingUp = !readingUp;
}
console.log(`SPQR: Extracted ${bits.length} bits from ${dataModules} color modules`);
return bits;
}
function decodeSPQRBitsDirect(bits) {
if (bits.length < 16) return null;
// Convert bits to bytes (QR codes store bits LSB first)
const bytes = [];
for (let i = 0; i < bits.length; i += 8) {
let byte = 0;
for (let j = 0; j < 8 && i + j < bits.length; j++) {
// QR codes store bits LSB first, so reverse the order
byte = (byte << 1) | bits[i + j];
}
bytes.push(byte);
}
if (bytes.length < 4) return null;
// Check mode
const mode = (bytes[0] >> 4) & 0xF;
const length = bytes[1] | ((bytes[0] & 0xF) << 8);
console.log(`SPQR decode: mode=${mode}, length=${length}, bytes=${bytes.length}`);
console.log('First 8 bytes:', bytes.slice(0, 8).map(b => b.toString(16).padStart(2, '0')).join(' '));
if (mode !== 4) {
console.log(`SPQR: Invalid mode ${mode}, expected 4 (byte mode)`);
return null;
}
if (length > bytes.length - 2) {
console.log(`SPQR: Length ${length} too large for ${bytes.length} bytes`);
return null;
}
// Extract data
const dataBytes = bytes.slice(2, 2 + length);
try {
const text = new TextDecoder('utf-8').decode(new Uint8Array(dataBytes));
console.log(`SPQR decoded text: "${text}"`);
return text;
} catch (e) {
console.log('SPQR UTF-8 decode failed:', e.message);
return null;
}
}
debugQRDecoder();