-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdecoder.html
More file actions
398 lines (328 loc) · 14.6 KB
/
Copy pathdecoder.html
File metadata and controls
398 lines (328 loc) · 14.6 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
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>LumiLink v0.1 - Decoder (Aspect-Correct Auto-Align)</title>
<style>
body {
font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
background-color: #1e1e1e;
color: #fff;
display: flex;
flex-direction: column;
align-items: center;
padding: 20px;
margin: 0;
}
h1 { margin-bottom: 5px; font-size: 24px; }
.container {
position: relative;
margin-top: 10px;
width: 100%;
max-width: 600px;
display: flex;
justify-content: center;
}
video {
background: #000;
border-radius: 12px;
box-shadow: 0 10px 30px rgba(0,0,0,0.5);
width: 100%;
height: auto;
max-height: 60vh;
}
/* Debug Overlay */
#debug-canvas {
position: absolute;
top: 0; left: 0;
width: 100%;
height: 100%;
pointer-events: none;
object-fit: contain;
}
.output-box {
margin-top: 20px;
width: 100%;
max-width: 600px;
background: #2a2a2a;
padding: 20px;
border-radius: 12px;
box-sizing: border-box;
}
h3 { margin-top: 0; color: #007bff; }
#decoded-text {
font-family: monospace;
font-size: 16px;
color: #00FF00;
word-wrap: break-word;
min-height: 50px;
}
.stats {
margin-top: 15px;
font-size: 12px;
color: #aaa;
display: flex;
justify-content: space-between;
}
button {
margin-top: 20px;
padding: 12px 20px;
background: #007bff;
color: white;
border: none;
border-radius: 6px;
cursor: pointer;
font-weight: bold;
}
</style>
</head>
<body>
<h1>LumiLink Decoder</h1>
<p style="font-size: 14px; color: #ccc; margin-top:0; text-align:center;">Point at the Iris.<br>Green tracker means it has locked on.</p>
<div class="container" id="video-container">
<video id="video" autoplay playsinline></video>
<canvas id="debug-canvas"></canvas>
<canvas id="hidden-canvas" style="display:none;"></canvas>
</div>
<button id="btn-start">Start Camera</button>
<div class="output-box">
<h3>Decoded Payload</h3>
<div id="decoded-text">Waiting for data...</div>
<div class="stats">
<span id="stat-packets">Packets: 0 / 0</span>
<span id="stat-crc">CRC Fails: 0</span>
</div>
</div>
<script>
const video = document.getElementById('video');
const hiddenCanvas = document.getElementById('hidden-canvas');
const ctxHidden = hiddenCanvas.getContext('2d', { willReadFrequently: true });
const debugCanvas = document.getElementById('debug-canvas');
const ctxDebug = debugCanvas.getContext('2d');
const RINGS = [16, 32, 48, 64, 96]; // 256 sectors
let packetBuffer = {};
let totalPacketsExpected = 0;
let crcFails = 0;
let lastSeqId = -1;
let isDecoding = false;
// CRC-32 Implementation
const crcTable = (function() {
let c;
let crcTable = [];
for(let n =0; n < 256; n++){
c = n;
for(let k =0; k < 8; k++){
c = ((c&1) ? (0xEDB88320 ^ (c >>> 1)) : (c >>> 1));
}
crcTable[n] = c;
}
return crcTable;
})();
function crc32(buffer) {
let crc = 0 ^ (-1);
for (let i = 0; i < buffer.length; i++) {
crc = (crc >>> 8) ^ crcTable[(crc ^ buffer[i]) & 0xFF];
}
return (crc ^ (-1)) >>> 0;
}
// Extremely Robust Color Classifier
function classifyColorRobust(r, g, b) {
let max = Math.max(r, g, b);
let min = Math.min(r, g, b);
// If it's overall dark, or the color is very desaturated (gray glare), call it black
if (max < 80 || (max - min) < 30) return 0; // Black
// Otherwise, find the dominant channel
if (r === max) return 1; // Red
if (g === max) return 2; // Green
return 3; // Blue
}
function sampleFrame() {
if (!isDecoding) return;
let vw = video.videoWidth;
let vh = video.videoHeight;
if (vw === 0 || vh === 0) {
requestAnimationFrame(sampleFrame);
return;
}
// Dynamically resize internal canvases to match exact camera resolution to avoid aspect ratio squashing!
if (hiddenCanvas.width !== vw) {
hiddenCanvas.width = vw;
hiddenCanvas.height = vh;
debugCanvas.width = vw;
debugCanvas.height = vh;
}
ctxHidden.drawImage(video, 0, 0, vw, vh);
const imgData = ctxHidden.getImageData(0, 0, vw, vh);
const pixels = imgData.data;
// 1. Find the Magenta Center (immune to white glare)
let wSumX = 0, wSumY = 0, wCount = 0;
// Scan middle 80% of screen for speed
let scanSizeX = vw * 0.8;
let scanSizeY = vh * 0.8;
let startX = Math.floor((vw - scanSizeX) / 2);
let endX = Math.floor(startX + scanSizeX);
let startY = Math.floor((vh - scanSizeY) / 2);
let endY = Math.floor(startY + scanSizeY);
// Sample every 4th pixel for speed
for (let y = startY; y < endY; y += 4) {
for (let x = startX; x < endX; x += 4) {
let idx = (y * vw + x) * 4;
let r = pixels[idx], g = pixels[idx+1], b = pixels[idx+2];
// Magenta: High Red, High Blue, Low Green
if (r > 150 && b > 150 && g < 100) {
wSumX += x; wSumY += y; wCount++;
}
}
}
ctxDebug.clearRect(0, 0, vw, vh);
if (wCount > 10) {
let cx = wSumX / wCount;
let cy = wSumY / wCount;
// 2. Find the Cyan Orientation Dot
let cSumX = 0, cSumY = 0, cCount = 0;
// Search in a bounding box around the center (max 20% of screen size)
let searchRad = Math.min(vw, vh) * 0.2;
let cStartX = Math.max(0, Math.floor(cx - searchRad));
let cEndX = Math.min(vw, Math.floor(cx + searchRad));
let cStartY = Math.max(0, Math.floor(cy - searchRad));
let cEndY = Math.min(vh, Math.floor(cy + searchRad));
// Finer resolution search for the smaller dot
for (let y = cStartY; y < cEndY; y += 2) {
for (let x = cStartX; x < cEndX; x += 2) {
let idx = (y * vw + x) * 4;
let r = pixels[idx], g = pixels[idx+1], b = pixels[idx+2];
// Cyan: High Green, High Blue, Low Red
if (g > 150 && b > 150 && r < 100) {
cSumX += x; cSumY += y; cCount++;
}
}
}
if (cCount > 2) {
let rx = cSumX / cCount;
let ry = cSumY / cCount;
let dx = rx - cx;
let dy = ry - cy;
let distance = Math.sqrt(dx*dx + dy*dy);
let scale = distance / 25.0; // The cyan dot is at radius 25 in the encoder
let angle = Math.atan2(dy, dx);
// Draw Debug Overlay
ctxDebug.strokeStyle = '#00FF00';
ctxDebug.lineWidth = 2;
ctxDebug.beginPath(); ctxDebug.arc(cx, cy, 35 * scale, 0, Math.PI*2); ctxDebug.stroke();
ctxDebug.beginPath(); ctxDebug.moveTo(cx, cy); ctxDebug.lineTo(rx, ry); ctxDebug.stroke();
// 3. Sample Sectors
const maxRadius = 190;
const ringThickness = (maxRadius - 40) / RINGS.length;
let colorIndices = [];
for (let ring = 0; ring < RINGS.length; ring++) {
let midRadius = (40 + ring * ringThickness + (ringThickness / 2)) * scale;
let sectors = RINGS[ring];
for (let s = 0; s < sectors; s++) {
let theta = (s * Math.PI * 2) / sectors + (Math.PI / sectors) + angle;
let px = Math.floor(cx + midRadius * Math.cos(theta));
let py = Math.floor(cy + midRadius * Math.sin(theta));
if (px >= 0 && px < vw && py >= 0 && py < vh) {
let idx = (py * vw + px) * 4;
let pr = pixels[idx], pg = pixels[idx+1], pb = pixels[idx+2];
colorIndices.push(classifyColorRobust(pr, pg, pb));
// Draw debug sample point
ctxDebug.fillStyle = 'rgba(255,255,255,0.7)';
ctxDebug.fillRect(px - 1, py - 1, 3, 3);
} else {
colorIndices.push(0);
}
}
}
processSampledColors(colorIndices);
}
}
requestAnimationFrame(sampleFrame);
}
function processSampledColors(colorIndices) {
if (colorIndices.length !== 256) return;
// Try Normal Decode
if (tryDecode(colorIndices)) return;
// Try Mirrored Decode (in case front-camera or OS horizontally flips the video)
let mirroredIndices = [];
let ptr = 0;
for (let ring = 0; ring < RINGS.length; ring++) {
let sectors = RINGS[ring];
let ringColors = colorIndices.slice(ptr, ptr + sectors);
mirroredIndices.push(ringColors[0]);
for (let i = sectors - 1; i > 0; i--) {
mirroredIndices.push(ringColors[i]);
}
ptr += sectors;
}
tryDecode(mirroredIndices);
}
function tryDecode(colorIndices) {
let packet = new Uint8Array(64);
for (let i = 0; i < 64; i++) {
let c1 = colorIndices[i*4];
let c2 = colorIndices[i*4 + 1];
let c3 = colorIndices[i*4 + 2];
let c4 = colorIndices[i*4 + 3];
packet[i] = (c1 << 6) | (c2 << 4) | (c3 << 2) | c4;
}
let seqId = packet[0];
if (seqId === lastSeqId) return true; // Already processed this frame, treat as success to stop mirror check
// CRC
let crcBuffer = new Uint8Array(60);
crcBuffer.set(packet.slice(0, 7), 0);
crcBuffer.set(packet.slice(11, 64), 7);
let calculatedCrc = crc32(crcBuffer);
let packetCrc = (packet[7] << 24) | (packet[8] << 16) | (packet[9] << 8) | packet[10];
if ((calculatedCrc >>> 0) !== (packetCrc >>> 0)) {
crcFails++;
document.getElementById('stat-crc').innerText = `CRC Fails: ${crcFails}`;
return false;
}
// CRC Passed!
lastSeqId = seqId;
let total = (packet[3] << 8) | packet[4];
let current = (packet[5] << 8) | packet[6];
if (total === 0) return true;
totalPacketsExpected = total;
packetBuffer[current] = packet.slice(11, 48); // 37 payload bytes
let receivedCount = Object.keys(packetBuffer).length;
document.getElementById('stat-packets').innerText = `Packets: ${receivedCount} / ${totalPacketsExpected}`;
if (receivedCount === totalPacketsExpected) {
assemblePayload();
}
return true;
}
function assemblePayload() {
let totalBytes = totalPacketsExpected * 37;
let fullBuffer = new Uint8Array(totalBytes);
for (let i = 0; i < totalPacketsExpected; i++) {
if (packetBuffer[i]) {
fullBuffer.set(packetBuffer[i], i * 37);
}
}
let endIdx = fullBuffer.indexOf(0);
if (endIdx === -1) endIdx = fullBuffer.length;
let text = new TextDecoder().decode(fullBuffer.slice(0, endIdx));
document.getElementById('decoded-text').innerText = text;
packetBuffer = {};
}
document.getElementById('btn-start').addEventListener('click', async () => {
try {
// Remove environment facing mode constraint for easier desktop testing, but prefer rear camera
const stream = await navigator.mediaDevices.getUserMedia({
video: { facingMode: 'environment', width: { ideal: 1920 }, height: { ideal: 1080 } }
});
video.srcObject = stream;
document.getElementById('btn-start').style.display = 'none';
video.onloadedmetadata = () => {
isDecoding = true;
requestAnimationFrame(sampleFrame);
};
} catch (err) {
alert("Camera access denied or unavailable. " + err.message);
}
});
</script>
</body>
</html>