Skip to content

Commit 3e16603

Browse files
fix(render): a PNG's colour tags no longer change its pixels in preview, check, snapshot or render (#6453)
* fix(render): a PNG's colour tags no longer change its pixels in preview, check, snapshot or render * fix(render): the play and present server also serves a PNG without its colour tags * fix(render): bundled pages inline a PNG without its colour tags too
1 parent e3e93aa commit 3e16603

14 files changed

Lines changed: 258 additions & 10 deletions
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import { afterEach, describe, expect, it } from "vitest";
2+
import { mkdtempSync, rmSync, writeFileSync } from "node:fs";
3+
import { tmpdir } from "node:os";
4+
import { join } from "node:path";
5+
import { buildRangeResponse } from "./compositionServer.js";
6+
7+
const TAGGED_PNG = Buffer.from(
8+
"iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAIAAACQd1PeAAAABGNJQ1ABAQABlXPCVgAAAAxJREFUeJxj0NfXBwABHgCOTp25kgAAAABJRU5ErkJggg==",
9+
"base64",
10+
);
11+
12+
let dir: string | undefined;
13+
14+
afterEach(() => {
15+
if (dir) rmSync(dir, { recursive: true, force: true });
16+
dir = undefined;
17+
});
18+
19+
describe("buildRangeResponse colour-tagged PNGs", () => {
20+
it("serves a BT.709-tagged PNG without the tag, so play and present show the file's values", async () => {
21+
dir = mkdtempSync(join(tmpdir(), "hf-composition-server-"));
22+
const file = join(dir, "tagged.png");
23+
writeFileSync(file, TAGGED_PNG);
24+
25+
const res = buildRangeResponse(file, "image/png", undefined);
26+
const body = Buffer.from(await res.arrayBuffer());
27+
28+
expect(body.includes("cICP")).toBe(false);
29+
expect(body.includes("IDAT")).toBe(true);
30+
expect(res.headers.get("content-length")).toBe(String(body.length));
31+
});
32+
});

‎packages/cli/src/utils/compositionServer.ts‎

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,12 @@
22
// `present`: locating the built runtime/player/slideshow bundles, serving
33
// composition asset files, and binding to a free port.
44
import { createHash } from "node:crypto";
5-
import { createReadStream, existsSync, statSync } from "node:fs";
5+
import { createReadStream, existsSync, readFileSync, statSync } from "node:fs";
66
import { resolve, dirname } from "node:path";
77
import { Readable } from "node:stream";
88
import { fileURLToPath } from "node:url";
99
import { getMimeType } from "@hyperframes/studio-server";
10+
import { stripSdrPngColourTags } from "@hyperframes/core/compiler/png-colour-tags";
1011
import { injectTagsAtHeadStart } from "@hyperframes/core/compiler/html-document";
1112

1213
/**
@@ -95,6 +96,10 @@ export function revalidatedResponse(
9596
return new Response(body, { status: 200, headers });
9697
}
9798

99+
function untaggedPngBody(filePath: string, contentType: string): Buffer | null {
100+
return contentType === "image/png" ? stripSdrPngColourTags(readFileSync(filePath)) : null;
101+
}
102+
98103
/**
99104
* Hono-native Range/206 response for a file on disk, mirroring the inline
100105
* Range logic in `packages/studio-server/src/routes/preview.ts`'s static
@@ -116,6 +121,15 @@ export function buildRangeResponse(
116121
ETag: `"${mtimeMs.toString(36)}-${size.toString(36)}"`,
117122
};
118123
if (ifNoneMatch === cache.ETag) return new Response(null, { status: 304, headers: cache });
124+
const untagged = untaggedPngBody(filePath, contentType);
125+
if (untagged) {
126+
const headers = {
127+
...cache,
128+
"Content-Type": contentType,
129+
"Content-Length": `${untagged.length}`,
130+
};
131+
return new Response(new Uint8Array(untagged), { status: 200, headers });
132+
}
119133
const match = rangeHeader ? /^bytes=(\d*)-(\d*)$/.exec(rangeHeader.trim()) : null;
120134

121135
const body = (start: number, end: number): ReadableStream<Uint8Array> | null =>

‎packages/cli/src/utils/staticProjectServer.test.ts‎

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -133,13 +133,31 @@ afterEach(async () => {
133133
mocks.decideMediaProxyEligibility.mockReturnValue({ eligible: true });
134134
});
135135

136-
async function serveWith(bytes: Buffer): Promise<{ url: string }> {
136+
const TAGGED_PNG = Buffer.from(
137+
"iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAIAAACQd1PeAAAABGNJQ1ABAQABlXPCVgAAAAxJREFUeJxj0NfXBwABHgCOTp25kgAAAABJRU5ErkJggg==",
138+
"base64",
139+
);
140+
141+
async function serveWith(bytes: Buffer, name = "tone.wav"): Promise<{ url: string }> {
137142
dir = mkdtempSync(join(tmpdir(), "hf-static-"));
138-
writeFileSync(join(dir, "tone.wav"), bytes);
143+
writeFileSync(join(dir, name), bytes);
139144
server = await serveStaticProjectHtml(dir, "<html></html>");
140145
return { url: server.url };
141146
}
142147

148+
describe("serveStaticProjectHtml colour-tagged PNGs", () => {
149+
it("serves a BT.709-tagged PNG without the tag, so check and snapshot see the file's values", async () => {
150+
const { url } = await serveWith(TAGGED_PNG, "tagged.png");
151+
152+
const res = await fetch(`${url}tagged.png`);
153+
const body = Buffer.from(await res.arrayBuffer());
154+
155+
expect(body.includes("cICP")).toBe(false);
156+
expect(body.includes("IDAT")).toBe(true);
157+
expect(res.headers.get("content-length")).toBe(String(body.length));
158+
});
159+
});
160+
143161
describe("serveStaticProjectHtml range support", () => {
144162
it("answers a Range request with 206 + the requested byte slice", async () => {
145163
// Chromium needs byte-range seekability or WAV `.duration` reports Infinity,

‎packages/cli/src/utils/staticProjectServer.ts‎

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { createServer, request as httpRequest, type Server, type ServerResponse } from "node:http";
22
import { Duplex } from "node:stream";
3-
import { createReadStream, existsSync, statSync } from "node:fs";
3+
import { createReadStream, existsSync, readFileSync, statSync } from "node:fs";
4+
import { stripSdrPngColourTags } from "@hyperframes/core/compiler/png-colour-tags";
45
import { isAbsolute, relative, resolve } from "node:path";
56
import {
67
PREVIEW_PROXY_BOX_PARAM,
@@ -117,6 +118,15 @@ export function inProcessAnswer(url: string) {
117118
return inProcessAnswers.get(new URL(url).origin);
118119
}
119120

121+
function serveUntaggedPng(filePath: string, res: ServerResponse): boolean {
122+
if (getMimeType(filePath) !== "image/png") return false;
123+
const untagged = stripSdrPngColourTags(readFileSync(filePath));
124+
if (!untagged) return false;
125+
res.writeHead(200, { "Content-Type": "image/png", "Content-Length": String(untagged.length) });
126+
res.end(untagged);
127+
return true;
128+
}
129+
120130
/**
121131
* Serve a file with HTTP Range support. Chromium needs byte-range seekability
122132
* to determine the duration of formats that carry it in a trailing/implicit
@@ -323,7 +333,8 @@ export async function serveStaticProjectHtml(
323333
copies.signal,
324334
);
325335
} else {
326-
serveFileWithRange(filePath, req.headers.range, res);
336+
if (!serveUntaggedPng(filePath, res))
337+
serveFileWithRange(filePath, req.headers.range, res);
327338
}
328339
return;
329340
}

‎packages/core/package-subpaths.json‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -422,6 +422,12 @@
422422
"types": "./dist/compiler/htmlDocument.d.ts",
423423
"environments": ["browser", "bun", "node"]
424424
},
425+
"./compiler/png-colour-tags": {
426+
"source": "./src/compiler/pngColourTags.ts",
427+
"runtime": "./dist/compiler/pngColourTags.js",
428+
"types": "./dist/compiler/pngColourTags.d.ts",
429+
"environments": ["bun", "node"]
430+
},
425431
"./runtime/position-edits": {
426432
"source": "./src/runtime/positionEdits.ts",
427433
"runtime": "./dist/runtime/positionEdits.js",

‎packages/core/package.json‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -429,6 +429,12 @@
429429
"import": "./src/compiler/htmlDocument.ts",
430430
"types": "./src/compiler/htmlDocument.ts"
431431
},
432+
"./compiler/png-colour-tags": {
433+
"bun": "./src/compiler/pngColourTags.ts",
434+
"node": "./dist/compiler/pngColourTags.js",
435+
"import": "./src/compiler/pngColourTags.ts",
436+
"types": "./src/compiler/pngColourTags.ts"
437+
},
432438
"./runtime/position-edits": {
433439
"bun": "./src/runtime/positionEdits.ts",
434440
"node": "./dist/runtime/positionEdits.js",
@@ -859,6 +865,10 @@
859865
"import": "./dist/compiler/htmlDocument.js",
860866
"types": "./dist/compiler/htmlDocument.d.ts"
861867
},
868+
"./compiler/png-colour-tags": {
869+
"import": "./dist/compiler/pngColourTags.js",
870+
"types": "./dist/compiler/pngColourTags.d.ts"
871+
},
862872
"./runtime/position-edits": {
863873
"import": "./dist/runtime/positionEdits.js",
864874
"types": "./dist/runtime/positionEdits.d.ts"

‎packages/core/src/compiler/htmlBundler.test.ts‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2128,6 +2128,26 @@ describe("bundleToSingleHtml", () => {
21282128
},
21292129
);
21302130

2131+
it("inlines a BT.709-tagged PNG without its tag, so the file's own values show", async () => {
2132+
const tagged = Buffer.from(
2133+
"iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAIAAACQd1PeAAAABGNJQ1ABAQABlXPCVgAAAAxJREFUeJxj0NfXBwABHgCOTp25kgAAAABJRU5ErkJggg==",
2134+
"base64",
2135+
);
2136+
const dir = makeTempProject({
2137+
"index.html": `<div data-composition-id="root" data-width="320" data-height="180"><img id="photo" src="tagged.png"></div>`,
2138+
});
2139+
writeFileSync(join(dir, "tagged.png"), tagged);
2140+
try {
2141+
const { document } = parseHTML(await bundleToSingleHtml(dir));
2142+
const src = document.getElementById("photo")?.getAttribute("src") ?? "";
2143+
const inlined = Buffer.from(src.slice(src.indexOf(",") + 1), "base64");
2144+
expect(inlined.includes("cICP")).toBe(false);
2145+
expect(inlined.includes("IDAT")).toBe(true);
2146+
} finally {
2147+
rmSync(dir, { recursive: true, force: true });
2148+
}
2149+
});
2150+
21312151
it.each(["?v=2", "#sprite", "?v=2#sprite"])(
21322152
"keeps inlined image bytes decodable with suffix %s",
21332153
async (suffix) => {

‎packages/core/src/compiler/htmlBundler.ts‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import {
1717
planCompositionAssembly,
1818
} from "./compositionAssembly";
1919
import { SCENE_FONTS_ATTR, SCENE_PART_ATTR } from "../sceneParts";
20+
import { stripSdrPngColourTags } from "./pngColourTags";
2021
import {
2122
ensureExternalScriptTag,
2223
readExternalScriptAttributes,
@@ -392,7 +393,8 @@ function createAssetInliner(
392393
// just to be discarded.
393394
const byteLength = safeStatSize(filePath);
394395
if (byteLength !== null && byteLength > MAX_INLINE_ASSET_BYTES) return { tooLarge: byteLength };
395-
const content = safeReadFileBuffer(filePath);
396+
const read = safeReadFileBuffer(filePath);
397+
const content = read && mimeType === "image/png" ? (stripSdrPngColourTags(read) ?? read) : read;
396398
return {
397399
dataUrl: content === null ? null : `data:${mimeType};base64,${content.toString("base64")}`,
398400
};
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
import { describe, expect, it } from "vitest";
2+
import { stripSdrPngColourTags } from "./pngColourTags";
3+
4+
const SIGNATURE = Buffer.from([0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a]);
5+
6+
function chunk(type: string, data: Buffer): Buffer {
7+
const length = Buffer.alloc(4);
8+
length.writeUInt32BE(data.length);
9+
// The helper never reads a CRC, so a filler is enough to tell the chunks apart.
10+
return Buffer.concat([length, Buffer.from(type, "latin1"), data, Buffer.alloc(4, 0xcc)]);
11+
}
12+
13+
const IDAT = chunk("IDAT", Buffer.from([1, 2, 3]));
14+
const png = (...before: Buffer[]) =>
15+
Buffer.concat([
16+
SIGNATURE,
17+
chunk("IHDR", Buffer.alloc(13)),
18+
...before,
19+
IDAT,
20+
chunk("IEND", Buffer.alloc(0)),
21+
]);
22+
const cicp = (transfer: number) => chunk("cICP", Buffer.from([1, transfer, 0, 1]));
23+
24+
describe("stripSdrPngColourTags", () => {
25+
it.each([
26+
["a BT.709 cICP", cicp(1)],
27+
["an sRGB cICP", cicp(13)],
28+
["a gAMA", chunk("gAMA", Buffer.alloc(4))],
29+
["a cHRM", chunk("cHRM", Buffer.alloc(32))],
30+
])("drops %s and nothing else", (_name, tag) => {
31+
expect(stripSdrPngColourTags(png(tag))?.equals(png())).toBe(true);
32+
});
33+
34+
it("keeps the chunks that already match the file's values", () => {
35+
const kept = [chunk("sRGB", Buffer.from([0])), chunk("iCCP", Buffer.alloc(8))];
36+
expect(stripSdrPngColourTags(png(...kept, cicp(1)))?.equals(png(...kept))).toBe(true);
37+
});
38+
39+
it.each([
40+
["has no colour tag", png()],
41+
["carries a PQ cICP", png(cicp(16), chunk("gAMA", Buffer.alloc(4)))],
42+
["carries an HLG cICP", png(cicp(18))],
43+
["is cut short", png(cicp(1)).subarray(0, 40)],
44+
])("leaves a PNG that %s alone", (_name, bytes) => {
45+
expect(stripSdrPngColourTags(bytes)).toBeNull();
46+
});
47+
});
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
const PNG_SIGNATURE = Buffer.from([0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a]);
2+
const PNG_SIGNATURE_BYTES = PNG_SIGNATURE.length;
3+
const CHUNK_OVERHEAD_BYTES = 12;
4+
const REMOVED_CHUNKS = new Set(["cICP", "gAMA", "cHRM"]);
5+
const HDR_CICP_TRANSFERS = new Set([16, 18]);
6+
7+
/** An SDR PNG's copy without the tags that make Chrome re-map its pixels; null if none apply or it is HDR. */
8+
export function stripSdrPngColourTags(png: Uint8Array): Buffer | null {
9+
const bytes = Buffer.from(png.buffer, png.byteOffset, png.byteLength);
10+
if (!bytes.subarray(0, PNG_SIGNATURE_BYTES).equals(PNG_SIGNATURE)) return null;
11+
const kept: Buffer[] = [bytes.subarray(0, PNG_SIGNATURE_BYTES)];
12+
let removed = false;
13+
for (let at = PNG_SIGNATURE_BYTES; at + CHUNK_OVERHEAD_BYTES <= bytes.length; ) {
14+
const end = at + CHUNK_OVERHEAD_BYTES + bytes.readUInt32BE(at);
15+
if (end > bytes.length) return null;
16+
const type = bytes.toString("latin1", at + 4, at + 8);
17+
if (type === "cICP" && HDR_CICP_TRANSFERS.has(bytes[at + 9] ?? 0)) return null;
18+
if (REMOVED_CHUNKS.has(type)) removed = true;
19+
else kept.push(bytes.subarray(at, end));
20+
at = end;
21+
}
22+
return removed ? Buffer.concat(kept) : null;
23+
}

0 commit comments

Comments
 (0)