Preserve accumulated client implementation since v0.1.4, including local identities, multi-server state, chat, administration, audio and streaming controls. Refine official-source layout, original artwork, responsive composer overflow, settings navigation and chat scroll anchoring. Current verification: Node suite passes; isolated packaged chat and responsive control smokes pass without device capture. Native core is reused, not rebuilt in this pass. Strict whole-screen visual parity remains incomplete; no 1:1 or cross-platform acceptance claim. Local checkpoint only; no release or push.
174 lines
9.8 KiB
JavaScript
174 lines
9.8 KiB
JavaScript
// Dependency-free PNG codec and pixel comparison for QA (8-bit RGB/RGBA/grey, non-interlaced: what
|
|
// Chromium screenshots produce). Validated by scripts/official-pair-capture-offscreen.mjs --self-test
|
|
// against Electron's own decoder (nativeImage) and synthetic perturbations.
|
|
import { deflateSync, inflateSync } from 'node:zlib';
|
|
|
|
const SIGNATURE = Buffer.from([137, 80, 78, 71, 13, 10, 26, 10]);
|
|
const CRC_TABLE = Array.from({ length: 256 }, (_, n) => { let c = n; for (let k = 0; k < 8; k++) c = c & 1 ? 0xedb88320 ^ (c >>> 1) : c >>> 1; return c >>> 0; });
|
|
const crc32 = buf => { let c = 0xffffffff; for (const b of buf) c = CRC_TABLE[(c ^ b) & 0xff] ^ (c >>> 8); return (c ^ 0xffffffff) >>> 0; };
|
|
|
|
/** @returns {{ width: number, height: number, data: Uint8Array }} RGBA */
|
|
export function decodePng(buffer) {
|
|
if (!Buffer.from(buffer.subarray(0, 8)).equals(SIGNATURE)) throw new Error('Not a PNG');
|
|
let offset = 8, header, palette, transparency;
|
|
const idat = [];
|
|
while (offset < buffer.length) {
|
|
const length = buffer.readUInt32BE(offset);
|
|
const type = buffer.toString('latin1', offset + 4, offset + 8);
|
|
const body = buffer.subarray(offset + 8, offset + 8 + length);
|
|
if (crc32(buffer.subarray(offset + 4, offset + 8 + length)) !== buffer.readUInt32BE(offset + 8 + length)) throw new Error(`PNG CRC mismatch in ${type}`);
|
|
if (type === 'IHDR') header = { width: body.readUInt32BE(0), height: body.readUInt32BE(4), depth: body[8], color: body[9], interlace: body[12] };
|
|
else if (type === 'PLTE') palette = body;
|
|
else if (type === 'tRNS') transparency = body;
|
|
else if (type === 'IDAT') idat.push(body);
|
|
else if (type === 'IEND') break;
|
|
offset += 12 + length;
|
|
}
|
|
if (!header) throw new Error('PNG without IHDR');
|
|
const { width, height, depth, color, interlace } = header;
|
|
if (depth !== 8 || interlace !== 0) throw new Error(`Unsupported PNG depth ${depth} / interlace ${interlace}`);
|
|
const channels = { 0: 1, 2: 3, 3: 1, 4: 2, 6: 4 }[color];
|
|
if (!channels) throw new Error(`Unsupported PNG colour type ${color}`);
|
|
const raw = inflateSync(Buffer.concat(idat));
|
|
const stride = width * channels;
|
|
const pixels = new Uint8Array(stride * height);
|
|
for (let y = 0; y < height; y++) {
|
|
const filter = raw[y * (stride + 1)];
|
|
const src = y * (stride + 1) + 1, dst = y * stride, prev = dst - stride;
|
|
for (let x = 0; x < stride; x++) {
|
|
const a = x >= channels ? pixels[dst + x - channels] : 0;
|
|
const b = y > 0 ? pixels[prev + x] : 0;
|
|
const c = x >= channels && y > 0 ? pixels[prev + x - channels] : 0;
|
|
let v = raw[src + x];
|
|
if (filter === 1) v += a;
|
|
else if (filter === 2) v += b;
|
|
else if (filter === 3) v += (a + b) >> 1;
|
|
else if (filter === 4) { const p = a + b - c, pa = Math.abs(p - a), pb = Math.abs(p - b), pc = Math.abs(p - c); v += pa <= pb && pa <= pc ? a : pb <= pc ? b : c; }
|
|
else if (filter !== 0) throw new Error(`Bad PNG filter ${filter}`);
|
|
pixels[dst + x] = v & 0xff;
|
|
}
|
|
}
|
|
const data = new Uint8Array(width * height * 4);
|
|
for (let i = 0, j = 0; i < width * height; i++, j += channels) {
|
|
const o = i * 4;
|
|
if (color === 6) { data[o] = pixels[j]; data[o + 1] = pixels[j + 1]; data[o + 2] = pixels[j + 2]; data[o + 3] = pixels[j + 3]; }
|
|
else if (color === 2) { data[o] = pixels[j]; data[o + 1] = pixels[j + 1]; data[o + 2] = pixels[j + 2]; data[o + 3] = 255; }
|
|
else if (color === 0) { data[o] = data[o + 1] = data[o + 2] = pixels[j]; data[o + 3] = 255; }
|
|
else if (color === 4) { data[o] = data[o + 1] = data[o + 2] = pixels[j]; data[o + 3] = pixels[j + 1]; }
|
|
else { const p = pixels[j] * 3; data[o] = palette[p]; data[o + 1] = palette[p + 1]; data[o + 2] = palette[p + 2]; data[o + 3] = transparency?.[pixels[j]] ?? 255; }
|
|
}
|
|
return { width, height, data };
|
|
}
|
|
|
|
export function encodePng({ width, height, data }) {
|
|
const stride = width * 4;
|
|
const raw = Buffer.alloc((stride + 1) * height);
|
|
for (let y = 0; y < height; y++) Buffer.from(data.buffer, data.byteOffset + y * stride, stride).copy(raw, y * (stride + 1) + 1);
|
|
const chunk = (type, body) => {
|
|
const out = Buffer.alloc(12 + body.length);
|
|
out.writeUInt32BE(body.length, 0); out.write(type, 4, 'latin1'); body.copy(out, 8);
|
|
out.writeUInt32BE(crc32(out.subarray(4, 8 + body.length)), 8 + body.length);
|
|
return out;
|
|
};
|
|
const ihdr = Buffer.alloc(13);
|
|
ihdr.writeUInt32BE(width, 0); ihdr.writeUInt32BE(height, 4); ihdr[8] = 8; ihdr[9] = 6;
|
|
return Buffer.concat([SIGNATURE, chunk('IHDR', ihdr), chunk('IDAT', deflateSync(raw, { level: 6 })), chunk('IEND', Buffer.alloc(0))]);
|
|
}
|
|
|
|
export const blank = (width, height, rgba = [0, 0, 0, 255]) => {
|
|
const data = new Uint8Array(width * height * 4);
|
|
for (let i = 0; i < data.length; i += 4) data.set(rgba, i);
|
|
return { width, height, data };
|
|
};
|
|
|
|
/** Integer crop clamped to the image; returns the effective rectangle too. */
|
|
export function crop(image, rect) {
|
|
const x = Math.max(0, Math.round(rect.x)), y = Math.max(0, Math.round(rect.y));
|
|
const w = Math.max(0, Math.min(image.width, Math.round(rect.x + rect.width)) - x);
|
|
const h = Math.max(0, Math.min(image.height, Math.round(rect.y + rect.height)) - y);
|
|
const data = new Uint8Array(w * h * 4);
|
|
for (let row = 0; row < h; row++) data.set(image.data.subarray(((y + row) * image.width + x) * 4, ((y + row) * image.width + x + w) * 4), row * w * 4);
|
|
return { width: w, height: h, data, rect: { x, y, width: w, height: h } };
|
|
}
|
|
|
|
export function paste(target, source, x, y) {
|
|
for (let row = 0; row < source.height; row++) {
|
|
if (y + row < 0 || y + row >= target.height) continue;
|
|
const from = Math.max(0, -x), to = Math.min(source.width, target.width - x);
|
|
if (to <= from) continue;
|
|
target.data.set(source.data.subarray((row * source.width + from) * 4, (row * source.width + to) * 4), ((y + row) * target.width + x + from) * 4);
|
|
}
|
|
return target;
|
|
}
|
|
|
|
const inside = (rects, x, y) => rects.some(r => x >= r.x && y >= r.y && x < r.x + r.width && y < r.y + r.height);
|
|
|
|
/**
|
|
* Pixel comparison over the shared top-left-anchored area. A pixel differs when any RGB channel
|
|
* differs by more than `tolerance`. Areas outside the overlap (size mismatch) count as differing.
|
|
* `ignore` rectangles (in image coordinates) are excluded from both numerator and denominator.
|
|
* `dx,dy`: product offset relative to the reference (used by the bounded shift search).
|
|
*/
|
|
export function compare(a, b, { tolerance = 16, ignore = [], dx = 0, dy = 0, diff = false } = {}) {
|
|
const width = Math.max(a.width, b.width), height = Math.max(a.height, b.height);
|
|
const out = diff ? blank(width, height, [0, 0, 0, 255]) : null;
|
|
let counted = 0, differing = 0, absolute = 0, overlap = 0;
|
|
let minX = Infinity, minY = Infinity, maxX = -1, maxY = -1;
|
|
for (let y = 0; y < height; y++) for (let x = 0; x < width; x++) {
|
|
if (ignore.length && inside(ignore, x, y)) { if (out) out.data.set([40, 40, 80, 255], (y * width + x) * 4); continue; }
|
|
counted++;
|
|
const bx = x + dx, by = y + dy;
|
|
const inA = x < a.width && y < a.height, inB = bx >= 0 && by >= 0 && bx < b.width && by < b.height;
|
|
let delta = 255;
|
|
if (inA && inB) {
|
|
overlap++;
|
|
const i = (y * a.width + x) * 4, j = (by * b.width + bx) * 4;
|
|
delta = Math.max(Math.abs(a.data[i] - b.data[j]), Math.abs(a.data[i + 1] - b.data[j + 1]), Math.abs(a.data[i + 2] - b.data[j + 2]));
|
|
absolute += (Math.abs(a.data[i] - b.data[j]) + Math.abs(a.data[i + 1] - b.data[j + 1]) + Math.abs(a.data[i + 2] - b.data[j + 2])) / 3;
|
|
if (out) { const g = Math.round((a.data[i] + a.data[i + 1] + a.data[i + 2]) / 9); out.data.set(delta > tolerance ? [255, Math.max(0, 200 - delta), 0, 255] : [g, g, g, 255], (y * width + x) * 4); }
|
|
} else if (out) out.data.set([255, 0, 255, 255], (y * width + x) * 4);
|
|
if (delta > tolerance) { differing++; if (x < minX) minX = x; if (y < minY) minY = y; if (x > maxX) maxX = x; if (y > maxY) maxY = y; }
|
|
}
|
|
return {
|
|
width, height, counted, overlap, differing,
|
|
ratio: counted ? differing / counted : 0,
|
|
meanAbs: overlap ? absolute / overlap : null,
|
|
bbox: maxX < 0 ? null : { x: minX, y: minY, width: maxX - minX + 1, height: maxY - minY + 1 },
|
|
sizeDelta: { width: b.width - a.width, height: b.height - a.height },
|
|
diff: out,
|
|
};
|
|
}
|
|
|
|
/** Smallest strict mismatch over integer offsets within ±radius (diagnoses pure misalignment). */
|
|
export function bestShift(a, b, { radius = 3, tolerance = 16, ignore = [] } = {}) {
|
|
let best = null;
|
|
for (let dy = -radius; dy <= radius; dy++) for (let dx = -radius; dx <= radius; dx++) {
|
|
const r = compare(a, b, { tolerance, ignore, dx, dy });
|
|
if (!best || r.differing < best.differing) best = { dx, dy, differing: r.differing, ratio: r.ratio };
|
|
}
|
|
return best;
|
|
}
|
|
|
|
/** Dominant colours (quantised to 4 bits per channel) with their share of the image. */
|
|
export function palette(image, top = 4) {
|
|
const counts = new Map();
|
|
for (let i = 0; i < image.data.length; i += 4) {
|
|
const key = ((image.data[i] >> 4) << 8) | ((image.data[i + 1] >> 4) << 4) | (image.data[i + 2] >> 4);
|
|
counts.set(key, (counts.get(key) ?? 0) + 1);
|
|
}
|
|
const total = image.width * image.height || 1;
|
|
return [...counts].sort((x, y) => y[1] - x[1]).slice(0, top).map(([key, n]) => ({
|
|
color: `#${[(key >> 8) & 15, (key >> 4) & 15, key & 15].map(v => (v * 17).toString(16).padStart(2, '0')).join('')}`, share: Number((n / total).toFixed(4)),
|
|
}));
|
|
}
|
|
|
|
/** [reference | product | diff] strip with 6 px gutters for human inspection. */
|
|
export function sideBySide(a, b, diffImage) {
|
|
const gutter = 6, height = Math.max(a.height, b.height, diffImage?.height ?? 0);
|
|
const width = a.width + b.width + (diffImage ? diffImage.width + gutter : 0) + gutter;
|
|
const out = blank(width, height, [255, 0, 255, 255]);
|
|
paste(out, a, 0, 0); paste(out, b, a.width + gutter, 0);
|
|
if (diffImage) paste(out, diffImage, a.width + b.width + gutter * 2, 0);
|
|
return out;
|
|
}
|