fix(browser): preserve RGB PNG transparent samples (#595)

Respect the RGB PNG tRNS transparent-color key when expanding 8-bit RGB pixels to RGBA, so equivalent RGB+tRNS and RGBA captures compare identically.
This commit is contained in:
Rudy Mizrahi Celekli
2026-10-06 01:51:32 +08:00
committed by GitHub
parent 50f7e81432
commit 9997152c2d
2 changed files with 121 additions and 4 deletions
+33 -4
View File
@@ -81,6 +81,7 @@ const decodePng = (artifact: WebScreenshotArtifact): DecodedPng => {
let header: ReturnType<typeof parseHeader> | undefined;
const compressed: Buffer[] = [];
let sawEnd = false;
let transparentColor: readonly number[] | undefined;
while (offset < bytes.length) {
if (offset + 12 > bytes.length) throw new TypeError("Truncated PNG chunk");
const length = bytes.readUInt32BE(offset);
@@ -90,7 +91,15 @@ const decodePng = (artifact: WebScreenshotArtifact): DecodedPng => {
if (dataEnd + 4 > bytes.length) throw new TypeError("Truncated PNG data");
const data = bytes.subarray(dataStart, dataEnd);
if (type === "IHDR") header = parseHeader(data);
else if (type === "IDAT") compressed.push(data);
else if (type === "tRNS") {
if (header?.channels !== 3 || data.length !== 6)
throw new TypeError("Unsupported PNG transparency");
transparentColor = [
data.readUInt16BE(0),
data.readUInt16BE(2),
data.readUInt16BE(4),
];
} else if (type === "IDAT") compressed.push(data);
else if (type === "IEND") {
sawEnd = true;
break;
@@ -108,7 +117,11 @@ const decodePng = (artifact: WebScreenshotArtifact): DecodedPng => {
return {
width: header.width,
height: header.height,
rgba: unfilter(raw, header.width, header.height, header.channels),
rgba: expandRgba(
unfilter(raw, header.width, header.height, header.channels),
header.channels,
transparentColor,
),
};
};
@@ -167,8 +180,16 @@ const unfilter = (
});
}
}
return decoded;
};
const expandRgba = (
decoded: Buffer,
channels: number,
transparentColor: readonly number[] | undefined,
): Buffer => {
if (channels === 4) return decoded;
const rgba = Buffer.alloc(width * height * 4);
const rgba = Buffer.alloc((decoded.length / channels) * 4);
for (
let source = 0, target = 0;
source < decoded.length;
@@ -177,7 +198,14 @@ const unfilter = (
rgba[target] = decoded[source] ?? 0;
rgba[target + 1] = decoded[source + 1] ?? 0;
rgba[target + 2] = decoded[source + 2] ?? 0;
rgba[target + 3] = 255;
// PNG tRNS keys are 16-bit values; values above 255 cannot match 8-bit samples.
rgba[target + 3] =
transparentColor !== undefined &&
transparentColor.every(
(value, channel) => decoded[source + channel] === value,
)
? 0
: 255;
}
return rgba;
};
@@ -225,6 +253,7 @@ const dimensions = ({ width, height }: DecodedPng) => ({ width, height });
const limitations = (): string[] => [
"Pixel comparison does not perform OCR, semantic layout analysis, or perceptual color correction.",
"Only non-interlaced 8-bit RGB and RGBA PNG screenshots are accepted.",
"PNG tRNS transparency is accepted only as a six-byte RGB color key; tRNS on RGBA or with another length is rejected.",
];
const PNG_SIGNATURE = Buffer.from([137, 80, 78, 71, 13, 10, 26, 10]);