Environment lighting: - ship all eight desktop studiolights/world datasets (WBENV1 float32) and a shared loader used by both the main viewport and the offscreen worker - add the N-panel "studio 环境" selector - rotate the world background together with the environment (backgroundRotation/backgroundIntensity), so camera view and offscreen renders match the desktop Mapping-node behaviour Verification (Chromium, OPFS, NOT_CLAIMED): - environment lighting + 8 world pixel identity - camera-view rotation correspondence (camera matrices, 207 samples) - offscreen vs on-screen background parity after hiding geometry (1305 samples) - metals case extended with material-field OPFS roundtrip - material Principled edits and rename undo/redo - eight studio worlds: pairwise frame differences + luminance rank correlation - undo history depth capped at 64 steps and undo across OPFS save/reopen Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
199 lines
8.2 KiB
JavaScript
199 lines
8.2 KiB
JavaScript
/**
|
|
* Image statistics for render evidence.
|
|
*
|
|
* Shared by tools/web/inspect-image.mjs, capture-render-basics.mjs and
|
|
* check-render-basics.mjs. Written because a frame whose pixels are all
|
|
* transparent still reports SUCCEEDED and still has a plausible file size:
|
|
* the only way to tell "rendered a picture" from "wrote an empty PNG" is to
|
|
* decode the pixels.
|
|
*
|
|
* Two details that were learned the hard way:
|
|
* - PNG rows carry a filter byte. Without undoing the filter every value is a
|
|
* delta against its neighbour and even a truly empty frame reports a
|
|
* handful of "opaque" pixels.
|
|
* - An empty EEVEE frame can still carry RGB values (dither noise, max 1/255).
|
|
* Alpha alone is not enough; look at maxRgb as well.
|
|
*/
|
|
import crypto from "node:crypto";
|
|
import fs from "node:fs";
|
|
import zlib from "node:zlib";
|
|
|
|
export function sha256File(file) {
|
|
return crypto.createHash("sha256").update(fs.readFileSync(file)).digest("hex");
|
|
}
|
|
|
|
const PNG_MAGIC = 0x89504e47;
|
|
const JPEG_SOF = new Set([0xc0, 0xc1, 0xc2, 0xc3, 0xc5, 0xc6, 0xc7, 0xc9, 0xca, 0xcb, 0xcd, 0xce, 0xcf]);
|
|
|
|
export function formatOf(bytes) {
|
|
if (bytes.length > 8 && bytes.readUInt32BE(0) === PNG_MAGIC) return "png";
|
|
if (bytes.length > 3 && bytes[0] === 0xff && bytes[1] === 0xd8 && bytes[2] === 0xff) return "jpeg";
|
|
return "unknown";
|
|
}
|
|
|
|
/** Decode a non-interlaced 8-bit PNG into raw samples. Throws on anything else. */
|
|
export function decodePng(bytes) {
|
|
if (bytes.length < 8 || bytes.readUInt32BE(0) !== PNG_MAGIC) throw new Error("not a PNG");
|
|
let offset = 8;
|
|
let width = 0, height = 0, bitDepth = 0, colorType = 0, interlace = 0;
|
|
let palette = null, transparency = null;
|
|
const idat = [];
|
|
while (offset + 8 <= bytes.length) {
|
|
const length = bytes.readUInt32BE(offset);
|
|
const type = bytes.toString("ascii", offset + 4, offset + 8);
|
|
const data = bytes.subarray(offset + 8, offset + 8 + length);
|
|
if (type === "IHDR") {
|
|
width = data.readUInt32BE(0);
|
|
height = data.readUInt32BE(4);
|
|
bitDepth = data[8];
|
|
colorType = data[9];
|
|
interlace = data[12];
|
|
}
|
|
else if (type === "PLTE") palette = data;
|
|
else if (type === "tRNS") transparency = data;
|
|
else if (type === "IDAT") idat.push(data);
|
|
else if (type === "IEND") break;
|
|
offset += 12 + length;
|
|
}
|
|
if (!width || !height) throw new Error("PNG has no IHDR");
|
|
if (interlace) throw new Error("interlaced PNG is not supported");
|
|
if (bitDepth !== 8) throw new Error(`unsupported PNG bit depth ${bitDepth} (only 8 is supported)`);
|
|
const channels = {0: 1, 2: 3, 3: 1, 4: 2, 6: 4}[colorType];
|
|
if (!channels) throw new Error(`unsupported PNG color type ${colorType}`);
|
|
if (colorType === 3 && !palette) throw new Error("palette PNG without PLTE");
|
|
|
|
const raw = zlib.inflateSync(Buffer.concat(idat));
|
|
const stride = width * channels;
|
|
if (raw.length < height * (stride + 1)) throw new Error("PNG image data is truncated");
|
|
const pixels = Buffer.alloc(width * height * channels);
|
|
// Undo the per-row filter (None / Sub / Up / Average / Paeth).
|
|
for (let y = 0; y < height; y += 1) {
|
|
const filter = raw[y * (stride + 1)];
|
|
const rowIn = y * (stride + 1) + 1;
|
|
const rowOut = y * stride;
|
|
for (let index = 0; index < stride; index += 1) {
|
|
const value = raw[rowIn + index];
|
|
const left = index >= channels ? pixels[rowOut + index - channels] : 0;
|
|
const up = y ? pixels[rowOut - stride + index] : 0;
|
|
const upLeft = y && index >= channels ? pixels[rowOut - stride + index - channels] : 0;
|
|
let predictor = 0;
|
|
if (filter === 1) predictor = left;
|
|
else if (filter === 2) predictor = up;
|
|
else if (filter === 3) predictor = (left + up) >> 1;
|
|
else if (filter === 4) {
|
|
const estimate = left + up - upLeft;
|
|
const [a, b, c] = [Math.abs(estimate - left), Math.abs(estimate - up), Math.abs(estimate - upLeft)];
|
|
predictor = a <= b && a <= c ? left : b <= c ? up : upLeft;
|
|
}
|
|
pixels[rowOut + index] = (value + predictor) & 255;
|
|
}
|
|
}
|
|
return {width, height, colorType, channels, pixels, palette, transparency};
|
|
}
|
|
|
|
/** Expand any supported PNG layout to tightly packed RGBA. */
|
|
export function toRgba(image) {
|
|
const {width, height, colorType, channels, pixels, palette, transparency} = image;
|
|
const count = width * height;
|
|
const out = Buffer.alloc(count * 4);
|
|
const key = transparency && colorType === 3 ? transparency : null;
|
|
for (let index = 0; index < count; index += 1) {
|
|
const source = index * channels;
|
|
const target = index * 4;
|
|
let r, g, b, a = 255;
|
|
if (colorType === 6) { r = pixels[source]; g = pixels[source + 1]; b = pixels[source + 2]; a = pixels[source + 3]; }
|
|
else if (colorType === 2) { r = pixels[source]; g = pixels[source + 1]; b = pixels[source + 2]; }
|
|
else if (colorType === 4) { r = g = b = pixels[source]; a = pixels[source + 1]; }
|
|
else if (colorType === 0) { r = g = b = pixels[source]; }
|
|
else {
|
|
const slot = pixels[source] * 3;
|
|
r = palette[slot]; g = palette[slot + 1]; b = palette[slot + 2];
|
|
if (key && pixels[source] < key.length) a = key[pixels[source]];
|
|
}
|
|
out[target] = r; out[target + 1] = g; out[target + 2] = b; out[target + 3] = a;
|
|
}
|
|
return out;
|
|
}
|
|
|
|
/** Dimensions of a JPEG without decoding it (baseline + progressive SOF markers). */
|
|
export function jpegSize(bytes) {
|
|
let offset = 2;
|
|
while (offset + 4 <= bytes.length) {
|
|
if (bytes[offset] !== 0xff) { offset += 1; continue; }
|
|
const marker = bytes[offset + 1];
|
|
if (marker === 0xd8 || marker === 0x01 || (marker >= 0xd0 && marker <= 0xd7)) { offset += 2; continue; }
|
|
const length = bytes.readUInt16BE(offset + 2);
|
|
if (JPEG_SOF.has(marker)) {
|
|
return {height: bytes.readUInt16BE(offset + 5), width: bytes.readUInt16BE(offset + 7)};
|
|
}
|
|
offset += 2 + length;
|
|
}
|
|
throw new Error("JPEG has no SOF marker");
|
|
}
|
|
|
|
/**
|
|
* Pixels-in-the-frame verdict.
|
|
* picture — a real image: enough opaque pixels and real colour values
|
|
* transparent — alpha is (nearly) everywhere 0; nothing was drawn
|
|
* noise-only — opaque but every channel is <= 2; a cleared buffer, not a scene
|
|
*/
|
|
export function verdictOf({opaqueRatio, maxRgb}) {
|
|
if (opaqueRatio < 0.02) return "transparent";
|
|
if (maxRgb <= 2) return "noise-only";
|
|
return "picture";
|
|
}
|
|
|
|
/** Full statistics for one image file. JPEG reports dimensions only. */
|
|
export function imageStats(file) {
|
|
const bytes = fs.readFileSync(file);
|
|
const format = formatOf(bytes);
|
|
const base = {path: file, format, bytes: bytes.length, sha256: crypto.createHash("sha256").update(bytes).digest("hex")};
|
|
if (format === "jpeg") {
|
|
const {width, height} = jpegSize(bytes);
|
|
return {...base, width, height, maxAlpha: null, opaqueRatio: null, maxRgb: null, verdict: "opaque-jpeg"};
|
|
}
|
|
if (format !== "png") return {...base, width: null, height: null, verdict: "unknown-format"};
|
|
const decoded = decodePng(bytes);
|
|
const rgba = toRgba(decoded);
|
|
const pixels = decoded.width * decoded.height;
|
|
let opaque = 0, maxAlpha = 0, maxRgb = 0, lumaSum = 0;
|
|
const colours = new Set();
|
|
for (let index = 0; index < pixels; index += 1) {
|
|
const target = index * 4;
|
|
const [r, g, b, a] = [rgba[target], rgba[target + 1], rgba[target + 2], rgba[target + 3]];
|
|
if (a > maxAlpha) maxAlpha = a;
|
|
if (a > 8) opaque += 1;
|
|
const max = r > g ? (r > b ? r : b) : (g > b ? g : b);
|
|
if (max > maxRgb) maxRgb = max;
|
|
lumaSum += 0.2126 * r + 0.7152 * g + 0.0722 * b;
|
|
if (colours.size < 4096) colours.add((r << 16) | (g << 8) | b);
|
|
}
|
|
const stats = {
|
|
...base,
|
|
width: decoded.width,
|
|
height: decoded.height,
|
|
maxAlpha,
|
|
opaqueRatio: Number((opaque / pixels).toFixed(4)),
|
|
maxRgb,
|
|
meanLuma: Number((lumaSum / pixels).toFixed(2)),
|
|
distinctColors: colours.size,
|
|
};
|
|
return {...stats, verdict: verdictOf(stats)};
|
|
}
|
|
|
|
/** Directory or list of files, expanded, sorted. Missing paths are reported, not skipped. */
|
|
export function collectImages(inputs) {
|
|
const files = [];
|
|
for (const input of inputs) {
|
|
const stat = fs.existsSync(input) ? fs.statSync(input) : null;
|
|
if (!stat) { files.push(input); continue; }
|
|
if (stat.isDirectory()) {
|
|
for (const name of fs.readdirSync(input).sort()) {
|
|
if (/\.(png|jpe?g)$/i.test(name)) files.push(`${input.replace(/\/$/, "")}/${name}`);
|
|
}
|
|
}
|
|
else files.push(input);
|
|
}
|
|
return files;
|
|
}
|