-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdebug_decoder.js
More file actions
72 lines (57 loc) · 2.54 KB
/
debug_decoder.js
File metadata and controls
72 lines (57 loc) · 2.54 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
import { generateColourQr } from './dist/generator.js';
import sharp from 'sharp';
// Debug the SPQR decoder color mapping
async function debugDecoder() {
console.log('🧪 Debugging SPQR decoder color mapping...');
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 color classification thresholds
console.log('\n=== Color Classification Test ===');
// Test the specific RGB values we're seeing
const testPixels = [
{ r: 85, g: 85, b: 85, desc: 'Dark gray (85,85,85)' },
{ r: 255, g: 255, b: 255, desc: 'White (255,255,255)' },
{ r: 0, g: 255, b: 0, desc: 'Green (0,255,0)' },
{ r: 255, g: 0, b: 0, desc: 'Red (255,0,0)' },
{ r: 0, g: 0, b: 0, desc: 'Black (0,0,0)' }
];
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';
};
testPixels.forEach(pixel => {
const tag = classifyTag(pixel.r, pixel.g, pixel.b);
console.log(`${pixel.desc} → ${tag}`);
});
console.log('\n✅ Debug complete');
} catch (error) {
console.error('❌ Debug failed:', error.message);
}
}
debugDecoder();