-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathverify.js
More file actions
98 lines (94 loc) · 3.71 KB
/
Copy pathverify.js
File metadata and controls
98 lines (94 loc) · 3.71 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
const fs = require('fs');
const zlib = require('zlib');
// reuse decodePNG from reveal.js by requiring its internals via a small copy
const src = fs.readFileSync('./reveal.js', 'utf8');
// extract decodePNG function body by eval in isolated context
const vm = require('vm');
const sandbox = { require, module: {}, exports: {}, console, Buffer, Math, Uint32Array, Uint8Array, Float64Array };
vm.createContext(sandbox);
vm.runInContext(src.replace('main();', ''), sandbox); // define funcs without running main
const { decodePNG } = sandbox;
// ---- paste of the HTML app's processFrame, adapted to take RGBA Uint8Array directly ----
function processFrameRGBA(width, height, data) {
const nearBlack = new Uint8Array(width * height);
for (let y = 0; y < height; y++) {
for (let x = 0; x < width; x++) {
const idx = (y * width + x) * 4;
const r = data[idx], g = data[idx + 1], b = data[idx + 2];
const lum = (r + g + b) / 3;
nearBlack[y * width + x] = lum < 127 ? 1 : 0;
}
}
const out = new Uint8Array(width * height).fill(255);
const visited = new Uint8Array(width * height);
const stack = [];
for (let y = 0; y < height; y++) {
for (let x = 0; x < width; x++) {
const startIdx = y * width + x;
if (!nearBlack[startIdx] || visited[startIdx]) continue;
let minX = x, maxX = x, minY = y, maxY = y;
stack.push(startIdx);
visited[startIdx] = 1;
while (stack.length) {
const idx = stack.pop();
const py = Math.floor(idx / width);
const px = idx % width;
if (px < minX) minX = px;
if (px > maxX) maxX = px;
if (py < minY) minY = py;
if (py > maxY) maxY = py;
for (let dy = -1; dy <= 1; dy++) {
for (let dx = -1; dx <= 1; dx++) {
if (dx === 0 && dy === 0) continue;
const nx = px + dx, ny = py + dy;
if (nx < 0 || nx >= width || ny < 0 || ny >= height) continue;
const nIdx = ny * width + nx;
if (nearBlack[nIdx] && !visited[nIdx]) {
visited[nIdx] = 1;
stack.push(nIdx);
}
}
}
}
const blobW = maxX - minX + 1;
const blobH = maxY - minY + 1;
if (blobH < 3 || blobW < 3) {
const cx = (minX + maxX) / 2;
const cy = (minY + maxY) / 2;
const newW = blobW * 10;
const newH = blobH * 10;
const nx0 = Math.round(cx - newW / 2);
const ny0 = Math.round(cy - newH / 2);
for (let yy = ny0; yy < ny0 + newH; yy++) {
if (yy < 0 || yy >= height) continue;
for (let xx = nx0; xx < nx0 + newW; xx++) {
if (xx < 0 || xx >= width) continue;
out[yy * width + xx] = 0;
}
}
}
}
}
return out;
}
// Load frame_01.png via reveal.js's decodePNG, convert RGB->RGBA, run processFrameRGBA
const buf = fs.readFileSync('./frames/frame_01.png');
const decoded = decodePNG(buf);
const { width, height, channels, pixels } = decoded;
const rgba = new Uint8Array(width * height * 4);
for (let i = 0; i < width * height; i++) {
rgba[i*4] = pixels[i*channels];
rgba[i*4+1] = pixels[i*channels+1];
rgba[i*4+2] = pixels[i*channels+2];
rgba[i*4+3] = 255;
}
const htmlMask = processFrameRGBA(width, height, rgba);
// Compare against reveal.js's own processFrame output (already saved as grayscale PNG)
const savedBuf = fs.readFileSync('./frames/processed_frame_01.png');
const savedDecoded = decodePNG(savedBuf);
let mismatches = 0;
for (let i = 0; i < width * height; i++) {
const savedVal = savedDecoded.pixels[i * savedDecoded.channels]; // grayscale stored as RGB triplet, R==G==B
if (savedVal !== htmlMask[i]) mismatches++;
}
console.log('total pixels', width*height, 'mismatches', mismatches);