- Move served planet (animated webp, posters, 300 frames) and team mascots into src/assets/ - Use relative paths (assets/...) that work in file://, on the dev server and with docroot=src - Screenshot pipeline writes to src/assets/projects - Halo audit: report and measurements, ticker ring fix, shadow rules in DESIGN.md and spec.md
72 lines
3.2 KiB
JavaScript
72 lines
3.2 KiB
JavaScript
'use strict';
|
|
/* png.cjs — минимальный декодер PNG без зависимостей (8/16-bit, color type 0/2/4/6,
|
|
non-interlaced). Нужен только для чтения пикселей скриншотов Playwright.
|
|
Playwright отдаёт 8-bit RGBA (color type 6), поэтому 16-bit оставлен как запас. */
|
|
const zlib = require('zlib');
|
|
|
|
function decode(buf) {
|
|
if (buf.readUInt32BE(0) !== 0x89504e47) throw new Error('not a png');
|
|
let pos = 8, width = 0, height = 0, bitDepth = 8, colorType = 6, interlace = 0;
|
|
const idat = [];
|
|
while (pos < buf.length) {
|
|
const len = buf.readUInt32BE(pos);
|
|
const type = buf.toString('ascii', pos + 4, pos + 8);
|
|
const data = buf.subarray(pos + 8, pos + 8 + len);
|
|
if (type === 'IHDR') {
|
|
width = data.readUInt32BE(0); height = data.readUInt32BE(4);
|
|
bitDepth = data[8]; colorType = data[9]; interlace = data[12];
|
|
} else if (type === 'IDAT') idat.push(data);
|
|
else if (type === 'IEND') break;
|
|
pos += 12 + len;
|
|
}
|
|
if (interlace !== 0) throw new Error('interlaced png unsupported');
|
|
if (bitDepth !== 8 && bitDepth !== 16) throw new Error('bit depth ' + bitDepth);
|
|
const channels = { 0: 1, 2: 3, 4: 2, 6: 4 }[colorType];
|
|
if (!channels) throw new Error('color type ' + colorType);
|
|
const bytesPerSample = bitDepth / 8;
|
|
const bpp = channels * bytesPerSample;
|
|
const raw = zlib.inflateSync(Buffer.concat(idat));
|
|
const stride = width * bpp;
|
|
const out = Buffer.alloc(width * height * 4);
|
|
let prev = Buffer.alloc(stride);
|
|
let p = 0;
|
|
for (let y = 0; y < height; y++) {
|
|
const filter = raw[p++];
|
|
const line = Buffer.from(raw.subarray(p, p + stride)); p += stride;
|
|
for (let x = 0; x < stride; x++) {
|
|
const a = x >= bpp ? line[x - bpp] : 0;
|
|
const b = prev[x];
|
|
const c = x >= bpp ? prev[x - bpp] : 0;
|
|
switch (filter) {
|
|
case 1: line[x] = (line[x] + a) & 0xff; break;
|
|
case 2: line[x] = (line[x] + b) & 0xff; break;
|
|
case 3: line[x] = (line[x] + ((a + b) >> 1)) & 0xff; break;
|
|
case 4: {
|
|
const pa = Math.abs(b - c), pb = Math.abs(a - c), pc = Math.abs(a + b - 2 * c);
|
|
const pr = (pa <= pb && pa <= pc) ? a : (pb <= pc ? b : c);
|
|
line[x] = (line[x] + pr) & 0xff; break;
|
|
}
|
|
}
|
|
}
|
|
for (let x = 0; x < width; x++) {
|
|
const s = x * bpp, d = (y * width + x) * 4;
|
|
const g = (i) => bytesPerSample === 2 ? line[s + i * 2] : line[s + i];
|
|
if (colorType === 6) { out[d] = g(0); out[d + 1] = g(1); out[d + 2] = g(2); out[d + 3] = g(3); }
|
|
else if (colorType === 2) { out[d] = g(0); out[d + 1] = g(1); out[d + 2] = g(2); out[d + 3] = 255; }
|
|
else if (colorType === 4) { out[d] = out[d + 1] = out[d + 2] = g(0); out[d + 3] = g(1); }
|
|
else { out[d] = out[d + 1] = out[d + 2] = g(0); out[d + 3] = 255; }
|
|
}
|
|
prev = line;
|
|
}
|
|
return { width, height, data: out };
|
|
}
|
|
|
|
function px(img, x, y) {
|
|
x = Math.max(0, Math.min(img.width - 1, Math.round(x)));
|
|
y = Math.max(0, Math.min(img.height - 1, Math.round(y)));
|
|
const i = (y * img.width + x) * 4;
|
|
return [img.data[i], img.data[i + 1], img.data[i + 2], img.data[i + 3]];
|
|
}
|
|
|
|
module.exports = { decode, px };
|