Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion index.html
Original file line number Diff line number Diff line change
Expand Up @@ -2007,6 +2007,6 @@ <h2 id="about-title">About rastertrace</h2>
location.reload();
};
</script>
<script type="module" src="js/app.js?v=231" onerror="window.recoverRasterTraceBoot()"></script>
<script type="module" src="js/app.js?v=234" onerror="window.recoverRasterTraceBoot()"></script>
</body>
</html>
2 changes: 1 addition & 1 deletion js/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
33 changes: 26 additions & 7 deletions js/pipeline.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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") {
/**
Expand All @@ -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
Expand Down Expand Up @@ -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 {
Expand Down
8 changes: 4 additions & 4 deletions scripts/cache-busters.json
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,8 @@
"sha": "c791ac290668f1933993695c39260283c51955aad4524eed259bbfb501e43f72"
},
"js/app.js": {
"v": 231,
"sha": "6f7369ac5e357e149bea688c5c50bbe506ca46cce7f592c692b5494f0f637a48"
"v": 234,
"sha": "2793923ce7effe9f8d18e460b1c8c8e3d11ce79184c04a9cf60794ba5e3c7c5e"
},
"js/background.js": {
"v": 2,
Expand Down Expand Up @@ -112,8 +112,8 @@
"sha": "4990c503e7ffce5aca43cd7ccec3fe624b7d97e65232119c5344879bab25517c"
},
"js/pipeline.js": {
"v": 105,
"sha": "95069f588ee6f8a0f57914c6315305ec203ad9e073d71ab5c0f3ffb55ce09bfc"
"v": 108,
"sha": "007ef1c4294aa174e9eb0c6a59e99c9de3e8fe0f77353b9b7e71308e2599b250"
},
"js/quantize.js": {
"v": 2,
Expand Down
68 changes: 66 additions & 2 deletions tests/pipeline.test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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 });
Expand All @@ -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;
Expand Down Expand Up @@ -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 });
Expand Down
Loading