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
+88
View File
@@ -49,6 +49,73 @@ describe("PNG visual diff", () => {
).toMatchObject({ status: "dimension_mismatch", compared_pixels: 0 });
});
it("preserves RGB transparent-color samples when comparing RGBA pixels", () => {
const header = Buffer.alloc(13);
header.writeUInt32BE(2, 0);
header.writeUInt32BE(1, 4);
header[8] = 8;
header[9] = 2;
const transparent = Buffer.alloc(6);
transparent.writeUInt16BE(10, 0);
transparent.writeUInt16BE(20, 2);
transparent.writeUInt16BE(30, 4);
const rgb = createWebScreenshotArtifact(
Buffer.concat([
Buffer.from([137, 80, 78, 71, 13, 10, 26, 10]),
chunk("IHDR", header),
chunk("tRNS", transparent),
chunk("IDAT", deflateSync(Buffer.from([0, 10, 20, 30, 10, 20, 31]))),
chunk("IEND", Buffer.alloc(0)),
]),
);
const rgba = artifact(2, 1, [10, 20, 30, 0, 10, 20, 31, 255]);
expect(
comparePngScreenshots(
compareWebScreenshotsInputSchema.parse({ before: rgb, after: rgba }),
),
).toMatchObject({
status: "identical",
changed_pixels: 0,
maximum_channel_delta: 0,
});
});
it("keeps 8-bit samples opaque when a 16-bit transparency key exceeds 255", () => {
const key = Buffer.alloc(6);
key.writeUInt16BE(266, 0);
key.writeUInt16BE(20, 2);
key.writeUInt16BE(30, 4);
const rgb = transparencyArtifact(2, key, [10, 20, 30]);
const result = comparePngScreenshots(
compareWebScreenshotsInputSchema.parse({
before: rgb,
after: artifact(1, 1, [10, 20, 30, 255]),
}),
);
expect(result).toMatchObject({ status: "identical", changed_pixels: 0 });
expect(result.limitations).toContain(
"PNG tRNS transparency is accepted only as a six-byte RGB color key; tRNS on RGBA or with another length is rejected.",
);
});
it.each([0, 5, 7])("rejects an RGB tRNS chunk of length %i", (length) => {
const image = transparencyArtifact(2, Buffer.alloc(length), [10, 20, 30]);
expect(() =>
comparePngScreenshots(
compareWebScreenshotsInputSchema.parse({ before: image, after: image }),
),
).toThrow("Unsupported PNG transparency");
});
it("rejects tRNS on an RGBA image instead of dropping its transparency data", () => {
const image = transparencyArtifact(6, Buffer.alloc(6), [10, 20, 30, 255]);
expect(() =>
comparePngScreenshots(
compareWebScreenshotsInputSchema.parse({ before: image, after: image }),
),
).toThrow("Unsupported PNG transparency");
});
it("rejects malformed PNG dimensions after validating image data", () => {
const image = artifact(32_000_001, 1, [0, 0, 0, 255]);
expect(() =>
@@ -117,3 +184,24 @@ const chunk = (type: string, data: Buffer): Buffer => {
);
return result;
};
const transparencyArtifact = (
colorType: number,
transparency: Buffer,
pixels: readonly number[],
) => {
const header = Buffer.alloc(13);
header.writeUInt32BE(1, 0);
header.writeUInt32BE(1, 4);
header[8] = 8;
header[9] = colorType;
return createWebScreenshotArtifact(
Buffer.concat([
Buffer.from([137, 80, 78, 71, 13, 10, 26, 10]),
chunk("IHDR", header),
chunk("tRNS", transparency),
chunk("IDAT", deflateSync(Buffer.from([0, ...pixels]))),
chunk("IEND", Buffer.alloc(0)),
]),
);
};
+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]);