From 78912c68eb66f93677ed3e4e78625b774c3dfc53 Mon Sep 17 00:00:00 2001 From: BRADSEC <7948876+bradsec@users.noreply.github.com> Date: Fri, 30 Oct 2026 08:07:36 +1000 Subject: [PATCH] fix(decode): correct image size sniffing for WebP, JPEG and oriented PNG - Read lossless WebP (VP8L) dimensions after the 0x2f signature byte; they were read 3 bytes late, so lossless WebPs decoded distorted. - Slide the JPEG marker scan past the first 256 KB so metadata-heavy files (ICC profiles, extended XMP) are no longer rejected. - Never force both resize sides in decodeImage: Chromium applies PNG EXIF orientation and WebKit does not, so forcing both stretched rotated PNGs. Fitting images decode as-is; larger ones pass a width. --- index.html | 2 +- js/app.js | 2 +- js/pipeline.js | 33 ++++++++++++++---- scripts/cache-busters.json | 8 ++--- tests/pipeline.test.mjs | 68 ++++++++++++++++++++++++++++++++++++-- 5 files changed, 98 insertions(+), 15 deletions(-) diff --git a/index.html b/index.html index 0eccff3..9be7439 100644 --- a/index.html +++ b/index.html @@ -2007,6 +2007,6 @@

About rastertrace

location.reload(); }; - + diff --git a/js/app.js b/js/app.js index 1b5de06..ca159f2 100644 --- a/js/app.js +++ b/js/app.js @@ -10,7 +10,7 @@ import { rotateBitmap, sniffImageSize, Tracer, -} from "./pipeline.js?v=105"; +} from "./pipeline.js?v=108"; import { parseAse, parseHexColor, parseSwatchText, SWATCHES_MAX, toHexColor } from "./color.js?v=2"; import { analyzeFlatness } from "./quantize.js?v=2"; import { diff --git a/js/pipeline.js b/js/pipeline.js index 2d0a4f8..0fbaae2 100644 --- a/js/pipeline.js +++ b/js/pipeline.js @@ -9,8 +9,9 @@ export const MAX_SOURCE_SIDE = 32_768; * @param {Blob} file */ export async function sniffImageSize(file) { - const bytes = new Uint8Array(await file.slice(0, 256 * 1024).arrayBuffer()); - const view = new DataView(bytes.buffer, bytes.byteOffset, bytes.byteLength); + const windowSize = 256 * 1024; + let bytes = new Uint8Array(await file.slice(0, windowSize).arrayBuffer()); + let view = new DataView(bytes.buffer, bytes.byteOffset, bytes.byteLength); /** * @param {number} offset * @param {number} length @@ -33,10 +34,11 @@ export async function sniffImageSize(file) { width: view.getUint16(26, true) & 0x3fff, height: view.getUint16(28, true) & 0x3fff, }; + // Byte 20 is the 0x2f signature; the packed sizes start after it. if (type === "VP8L") return { - width: 1 + (((bytes[25] & 0x3f) << 8) | bytes[24]), - height: 1 + (((bytes[27] & 0x0f) << 10) | (bytes[26] << 2) | ((bytes[25] & 0xc0) >> 6)), + width: 1 + (((bytes[22] & 0x3f) << 8) | bytes[21]), + height: 1 + (((bytes[24] & 0x0f) << 10) | (bytes[23] << 2) | ((bytes[22] & 0xc0) >> 6)), }; if (type === "VP8X") { /** @@ -50,9 +52,20 @@ export async function sniffImageSize(file) { if (bytes.length >= 4 && bytes[0] === 0xff && bytes[1] === 0xd8) { // EXIF orientations 5-8 are 90-degree rotations. createImageBitmap // applies them during decode, so the sniffed size must be the - // oriented one or resizeWidth/resizeHeight would distort the image. + // oriented one or resizeWidth would size the wrong side. let rotated = false; - for (let i = 2; i + 9 < bytes.length;) { + let base = 0; // file offset of bytes[0] + for (let i = 2; ;) { + // Metadata segments (ICC profiles, extended XMP) can push the frame + // header past the first window. Slide it so a whole segment, at most + // 64 KB, always fits. + if (bytes.length - i < 0x10004 && base + bytes.length < file.size) { + base += i; + bytes = new Uint8Array(await file.slice(base, base + windowSize).arrayBuffer()); + view = new DataView(bytes.buffer, bytes.byteOffset, bytes.byteLength); + i = 0; + } + if (i + 9 >= bytes.length) break; if (bytes[i] !== 0xff) break; // T.81 B.1.1.2: any marker may be preceded by any number of 0xFF fill // bytes. Skipping them costs one loop; not skipping them reads the @@ -166,9 +179,15 @@ export async function decodeImage(file, maxSide = MAX_TRACE_SIDE, knownSize = nu } try { const resized = fitDecodeSize(size.width, size.height, maxSide); + // Browsers differ on which formats' EXIF orientation they apply + // (Chromium honors it in PNG, WebKit does not), so never force both + // sides: a fitting image decodes as-is, and a width alone lets the + // browser keep the oriented aspect. capBitmap trims any overshoot. + if (resized.width === size.width && resized.height === size.height) { + return await createImageBitmap(file); + } return await createImageBitmap(file, { resizeWidth: resized.width, - resizeHeight: resized.height, resizeQuality: "high", }); } catch { diff --git a/scripts/cache-busters.json b/scripts/cache-busters.json index dea2459..40c43c7 100644 --- a/scripts/cache-busters.json +++ b/scripts/cache-busters.json @@ -64,8 +64,8 @@ "sha": "c791ac290668f1933993695c39260283c51955aad4524eed259bbfb501e43f72" }, "js/app.js": { - "v": 231, - "sha": "6f7369ac5e357e149bea688c5c50bbe506ca46cce7f592c692b5494f0f637a48" + "v": 234, + "sha": "2793923ce7effe9f8d18e460b1c8c8e3d11ce79184c04a9cf60794ba5e3c7c5e" }, "js/background.js": { "v": 2, @@ -112,8 +112,8 @@ "sha": "4990c503e7ffce5aca43cd7ccec3fe624b7d97e65232119c5344879bab25517c" }, "js/pipeline.js": { - "v": 105, - "sha": "95069f588ee6f8a0f57914c6315305ec203ad9e073d71ab5c0f3ffb55ce09bfc" + "v": 108, + "sha": "007ef1c4294aa174e9eb0c6a59e99c9de3e8fe0f77353b9b7e71308e2599b250" }, "js/quantize.js": { "v": 2, diff --git a/tests/pipeline.test.mjs b/tests/pipeline.test.mjs index 189a92b..07315e7 100644 --- a/tests/pipeline.test.mjs +++ b/tests/pipeline.test.mjs @@ -28,13 +28,12 @@ test("decodeImage uses known intrinsic dimensions for a bounded decode", async ( let receivedOptions; globalThis.createImageBitmap = async (_file, options) => { receivedOptions = options; - return { width: options.resizeWidth, height: options.resizeHeight }; + return { width: options.resizeWidth, height: 1024 }; }; try { const result = await decodeImage(blob([]), 2048, { width: 8192, height: 4096 }); assert.deepEqual(receivedOptions, { resizeWidth: 2048, - resizeHeight: 1024, resizeQuality: "high", }); assert.deepEqual(result, { width: 2048, height: 1024 }); @@ -43,6 +42,51 @@ test("decodeImage uses known intrinsic dimensions for a bounded decode", async ( } }); +test("decodeImage omits resize options when the source fits", async () => { + const original = globalThis.createImageBitmap; + let receivedOptions = "not called"; + globalThis.createImageBitmap = async (_file, options) => { + receivedOptions = options; + return { width: 300, height: 200 }; + }; + try { + await decodeImage(blob([]), 2048, { width: 300, height: 200 }); + assert.equal(receivedOptions, undefined); + } finally { + globalThis.createImageBitmap = original; + } +}); + +test("decodeImage lets WebP retain its oriented aspect ratio when resized", async () => { + const original = globalThis.createImageBitmap; + let receivedOptions; + globalThis.createImageBitmap = async (_file, options) => { + receivedOptions = options; + return { width: 2048, height: 1024 }; + }; + try { + await decodeImage(blob([], "image/webp"), 2048, { width: 8192, height: 4096 }); + assert.deepEqual(receivedOptions, { resizeWidth: 2048, resizeQuality: "high" }); + } finally { + globalThis.createImageBitmap = original; + } +}); + +test("decodeImage sets only the resize width for oversized JPEG", async () => { + const original = globalThis.createImageBitmap; + let receivedOptions; + globalThis.createImageBitmap = async (_file, options) => { + receivedOptions = options; + return { width: 2048, height: 1024 }; + }; + try { + await decodeImage(blob([], "image/jpeg"), 2048, { width: 8192, height: 4096 }); + assert.deepEqual(receivedOptions, { resizeWidth: 2048, resizeQuality: "high" }); + } finally { + globalThis.createImageBitmap = original; + } +}); + test("decodeImage rejects unknown formats before browser decoding", async () => { const original = globalThis.createImageBitmap; let decoded = false; @@ -193,6 +237,26 @@ test("sniffImageSize reads WebP VP8X dimensions", async () => { assert.deepEqual(await sniffImageSize(blob(bytes, "image/webp")), { width: 1024, height: 512 }); }); +test("sniffImageSize reads WebP VP8L dimensions after the signature byte", async () => { + // Header of a 300x200 lossless file: 0x2f signature at byte 20, then + // 14-bit width-1 and height-1 packed little-endian from byte 21. + const bytes = [ + 0x52, 0x49, 0x46, 0x46, 0, 0, 0, 0, 0x57, 0x45, 0x42, 0x50, 0x56, 0x50, 0x38, 0x4c, 0x18, 0, 0, + 0, 0x2f, 0x2b, 0xc1, 0x31, 0x00, 0, 0, 0, 0, 0, + ]; + assert.deepEqual(await sniffImageSize(blob(bytes, "image/webp")), { width: 300, height: 200 }); +}); + +test("sniffImageSize finds a JPEG frame header past 256 KB of metadata", async () => { + // Five full-size APP2 segments (ICC profiles, extended XMP) put SOF0 + // beyond the first read window. + const app2 = [0xff, 0xe2, 0xff, 0xff, ...new Array(0xfffd).fill(0x58)]; + const bytes = [0xff, 0xd8]; + for (let n = 0; n < 5; n++) bytes.push(...app2); + bytes.push(...SOF0); + assert.deepEqual(await sniffImageSize(blob(bytes, "image/jpeg")), { width: 640, height: 400 }); +}); + test("fitDecodeSize caps the longest side", () => { assert.deepEqual(fitDecodeSize(4032, 3024, 2048), { width: 2048, height: 1536 }); assert.deepEqual(fitDecodeSize(100, 50, 2048), { width: 100, height: 50 });