Skip to content

Free open-source image editor for the browser

Open images up to the GPU's texture limit and reject larger ones by size (#92)

Rodrigo Belfiore6fa9f67fd93e
diff --git a/API.md b/API.md
index 1b9bc4a..7299dac 100644
--- a/API.md
+++ b/API.md
@@ -26,7 +26,7 @@ These return promises; await them before editing. File arguments are browser `Fi
| `importXmp(file)` | Applies supported Camera Raw adjustments as one edit. |
| `recoverDraft()`, `discardDraft()` | Opens or deletes the draft kept in the browser. |
-File loads are queued and `loadUrl` starts at once; the last load started wins. Image, scene, and `.cube` failures do not reject: the open document, if any, stays with its history, and `getState().failure` names the file and error. Check `failure` to confirm success; a `documentId` may be the previous document's. Loading completion does not mean the preview has rendered.
+File loads are queued and `loadUrl` starts at once; the last load started wins. Image, scene, and `.cube` failures do not reject: the open document, if any, stays with its history, and `getState().failure` names the file and error. Check `failure` to confirm success; a `documentId` may be the previous document's. An image with a side longer than the GPU's texture limit (`maxTextureDimension2D`: at least 8,192 pixels, usually 16,384) fails with an error naming its size, before decoding when its format declares the size up front. Loading completion does not mean the preview has rendered.
## Editing
diff --git a/bun.lock b/bun.lock
index 29da01c..f1ff2e5 100644
--- a/bun.lock
+++ b/bun.lock
@@ -16,7 +16,8 @@
"class-variance-authority": "^0.7.1",
"cn": "^0.4.0",
"idb": "^8.0.3",
- "raw-webgpu": "0.1.1",
+ "image-size": "2.0.4",
+ "raw-webgpu": "0.2.0",
"react": "^19.2.8",
"react-dom": "^19.2.8",
"tailwindcss": "^4.3.3",
@@ -270,6 +271,8 @@
"idb": ["[email protected]", "", {}, "sha512-LtwtVyVYO5BqRvcsKuB2iUMnHwPVByPCXFXOpuU96IZPPoPN6xjOGxZQ74pgSVVLQWtUOYgyeL4GE98BY5D3wg=="],
+ "image-size": ["[email protected]", "", { "bin": { "image-size": "bin/image-size.js" } }, "sha512-QRUkFFsRV/6fuESxb9Vkq+a0LkSrgKXuc2NEqfikiXxxN/G3tjWt5EVUlMaImRBZRZK/jRBEbYvpPYZL8t08Zw=="],
+
"jiti": ["[email protected]", "", { "bin": { "jiti": "lib/jiti-cli.mjs" } }, "sha512-AC/7JofJvZGrrneWNaEnJeOLUx+JlGt7tNa0wZiRPT4MY1wmfKjt2+6O2p2uz2+skll8OZZmJMNqeke7kKbNgQ=="],
"json-schema": ["[email protected]", "", {}, "sha512-es94M3nTIfsEPisRafak+HDLfHXnKBhV3vU5eqPcS3flIWqcxJWgXHXiey3YrpaNsanY5ei1VoYEbOzijuq9BA=="],
@@ -318,7 +321,7 @@
"postcss": ["[email protected]", "", { "dependencies": { "nanoid": "^3.3.17", "picocolors": "^1.1.1", "source-map-js": "^1.2.1" } }, "sha512-u82N74LFzG8ca+dD8puPnplTXoGH4fTPpVGuIbt36G3qvNlkvfD0lEAZSxaly3KX8TS/L1A1gsCEmvKmBcVbkQ=="],
- "raw-webgpu": ["[email protected]", "", {}, "sha512-JqFqFax+IAKb+Frlh5QTFD8viowAvBJ2ofY1lWSq76t1mjXa09oYuLYoClraaCuK1tOPpB9zOfr1Mmn7+aqLag=="],
+ "raw-webgpu": ["[email protected]", "", {}, "sha512-PO3aSUKNXs89lGrzPl8YKewgrRk0FkWgftDwri4k9GIM130NgtvXlkOW1dwUahmU8HNgP5ra5qOUMU/mLTr/sw=="],
"react": ["[email protected]", "", {}, "sha512-PWaYA1L/q9u2u7xYQi+Y3L3Yfnie7XyLeaJICV1MGD6LprsBxcAqGjYyr0eY3p+QdsA+x/Irkt4Qif8D63+Sbw=="],
diff --git a/package.json b/package.json
index 2bb36b8..3ae9993 100644
--- a/package.json
+++ b/package.json
@@ -23,7 +23,8 @@
"class-variance-authority": "^0.7.1",
"cn": "^0.4.0",
"idb": "^8.0.3",
- "raw-webgpu": "0.1.1",
+ "image-size": "2.0.4",
+ "raw-webgpu": "0.2.0",
"react": "^19.2.8",
"react-dom": "^19.2.8",
"tailwindcss": "^4.3.3",
diff --git a/src/core/image/decode/heic.ts b/src/core/image/decode/heic.ts
index 8a5b98d..abf4af8 100644
--- a/src/core/image/decode/heic.ts
+++ b/src/core/image/decode/heic.ts
@@ -3,6 +3,8 @@
* image, one hvc1 item or a grid of hvc1 tiles. Parsing is independent of WebCodecs.
*/
+import { checkSize } from "./size";
+
type Box = { type: string; start: number; end: number };
function check(condition: unknown): asserts condition {
@@ -278,10 +280,11 @@ function parseHeic(bytes: Uint8Array) {
};
}
-export async function decodeHeic(file: Blob) {
+export async function decodeHeic(file: Blob, limit: number) {
const { chunks, codec, description, ...image } = parseHeic(
new Uint8Array(await file.arrayBuffer()),
);
+ checkSize(limit, image.width, image.height);
const tiles: VideoFrame[] = [];
let failure: DOMException | undefined;
const decoder = new VideoDecoder({
diff --git a/src/core/image/decode/index.ts b/src/core/image/decode/index.ts
index c153a54..a41a23b 100644
--- a/src/core/image/decode/index.ts
+++ b/src/core/image/decode/index.ts
@@ -1,5 +1,6 @@
import type { Gpu, Target } from "vgpu";
import { createImageSource, type ImageSource } from "@/core/image";
+import { checkHeader } from "./size";
import type { Decoder } from "./types";
import { uploadDecoded } from "./upload";
@@ -15,13 +16,21 @@ type Format = {
function pixelFormat(load: () => Promise<Decoder>) {
return async (gpu: Gpu, file: File) => {
const decoder = await load();
- const decoded = await decoder(file);
+ const decoded = await decoder(file, gpu.gpu.limits.maxTextureDimension2D);
const { image, primaries } = uploadDecoded(gpu, decoded);
return createImageSource(image, { primaries });
};
}
-const native = pixelFormat(async () => createImageBitmap);
+/** Decodes through the browser once the size its header declares fits. */
+async function decodeBrowser(file: Blob, limit: number) {
+ await checkHeader(limit, file);
+ return createImageBitmap(file);
+}
+
+const native = pixelFormat(async () => decodeBrowser);
+// An AVIF header lists several images without marking the primary one, so upload checks its size.
+const avif = pixelFormat(async () => (file: Blob) => createImageBitmap(file));
const svg = pixelFormat(async () => (await import("./svg")).decodeSvg);
const heic = pixelFormat(async () => (await import("./heic")).decodeHeic);
const tiff = async (gpu: Gpu, file: File) =>
@@ -74,7 +83,7 @@ const formats: Format[] = [
{ types: ["image/jpeg"], extensions: ["jpg", "jpeg"], decode: native },
{ types: ["image/gif"], extensions: ["gif"], decode: native },
{ types: ["image/webp"], extensions: ["webp"], decode: native },
- { types: ["image/avif"], extensions: ["avif"], decode: native },
+ { types: ["image/avif"], extensions: ["avif"], decode: avif },
{ types: ["image/bmp"], extensions: ["bmp"], decode: native },
{ types: ["image/svg+xml"], extensions: ["svg"], decode: svg },
{
diff --git a/src/core/image/decode/size.ts b/src/core/image/decode/size.ts
new file mode 100644
index 0000000..63d2086
--- /dev/null
+++ b/src/core/image/decode/size.ts
@@ -0,0 +1,24 @@
+const count = new Intl.NumberFormat("en-US");
+
+/** Rejects an image with a side longer than `limit`, the device's largest texture. */
+export function checkSize(limit: number, width: number, height: number) {
+ if (Math.max(width, height) <= limit) {
+ return;
+ }
+ throw new Error(
+ `This device opens images up to ${count.format(limit)} pixels per side; this one is ${count.format(width)} × ${count.format(height)}.`,
+ );
+}
+
+/** Checks the size declared in the first 512 KiB; a header it can't read is left to the decoder. */
+export async function checkHeader(limit: number, file: Blob) {
+ const { imageSize } = await import("image-size");
+ const bytes = new Uint8Array(await file.slice(0, 512 * 1024).arrayBuffer());
+ let size: { width: number; height: number };
+ try {
+ size = imageSize(bytes);
+ } catch {
+ return;
+ }
+ checkSize(limit, size.width, size.height);
+}
diff --git a/src/core/image/decode/svg.ts b/src/core/image/decode/svg.ts
index 93375da..d57ea45 100644
--- a/src/core/image/decode/svg.ts
+++ b/src/core/image/decode/svg.ts
@@ -1,15 +1,17 @@
+import { checkSize } from "./size";
+
const fallbackSize = 1024;
/** Rasterizes through an <img>, since SVGs without intrinsic size can't go straight to a bitmap. */
-export async function decodeSvg(file: Blob) {
+export async function decodeSvg(file: Blob, limit: number) {
const image = new Image();
image.src = URL.createObjectURL(file);
await image.decode();
URL.revokeObjectURL(image.src);
- const canvas = new OffscreenCanvas(
- image.naturalWidth || fallbackSize,
- image.naturalHeight || fallbackSize,
- );
+ const width = image.naturalWidth || fallbackSize;
+ const height = image.naturalHeight || fallbackSize;
+ checkSize(limit, width, height);
+ const canvas = new OffscreenCanvas(width, height);
canvas.getContext("2d")?.drawImage(image, 0, 0, canvas.width, canvas.height);
return canvas.transferToImageBitmap();
}
diff --git a/src/core/image/decode/types.ts b/src/core/image/decode/types.ts
index f508195..736e112 100644
--- a/src/core/image/decode/types.ts
+++ b/src/core/image/decode/types.ts
@@ -13,4 +13,5 @@ export type Frames = {
rotation: number;
};
export type Decoded = ImageBitmap | Pixels | Frames;
-export type Decoder = (file: Blob) => Promise<Decoded>;
+/** Decodes a file, rejecting one with a side longer than `limit` before allocating its pixels. */
+export type Decoder = (file: Blob, limit: number) => Promise<Decoded>;
diff --git a/src/core/image/decode/upload.ts b/src/core/image/decode/upload.ts
index 7edf49f..e4ff910 100644
--- a/src/core/image/decode/upload.ts
+++ b/src/core/image/decode/upload.ts
@@ -1,6 +1,7 @@
import { effect, frame, type Gpu, type Target, target } from "vgpu";
import type { EncodedImage } from "@/core/image";
import { weakMemo } from "@/lib/weak-memo";
+import { checkSize } from "./size";
import type { Decoded } from "./types";
import shader from "./upload.wgsl";
@@ -12,6 +13,7 @@ const isP3 = (frame?: VideoFrame) =>
/** A texture of sRGB-encoded bytes, which the GPU decodes to linear when it reads them. */
function encoded(gpu: Gpu, size: Size) {
+ checkSize(gpu.gpu.limits.maxTextureDimension2D, ...size);
return gpu.device.createTexture({
size,
format: "rgba8unorm-srgb",
diff --git a/src/main.tsx b/src/main.tsx
index f48dcbd..8899628 100644
--- a/src/main.tsx
+++ b/src/main.tsx
@@ -1,8 +1,9 @@
import { TooltipProvider } from "@roprgm/ui/tooltip";
import { Analytics } from "@vercel/analytics/react";
import { SpeedInsights } from "@vercel/speed-insights/react";
-import { Component, type ReactNode } from "react";
+import { Component, type ReactNode, Suspense, use } from "react";
import { createRoot } from "react-dom/client";
+import { initFromDevice } from "vgpu";
import { GpuProvider } from "vgpu-react";
import { App } from "@/app";
import { GpuStats } from "@/components/gpu-stats";
@@ -45,17 +46,42 @@ class GpuBoundary extends Component<
}
}
+/**
+ * A device with its adapter's own texture limit rather than WebGPU's default, which stops at
+ * 8,192 pixels a side; it lives as long as the page.
+ */
+async function createGpu() {
+ const adapter = await navigator.gpu?.requestAdapter();
+ if (!adapter) {
+ throw Error("This browser offers no WebGPU adapter.");
+ }
+ const { maxTextureDimension2D } = adapter.limits;
+ const device = await adapter.requestDevice({
+ requiredLimits: { maxTextureDimension2D },
+ });
+ return initFromDevice(device);
+}
+
+const gpu = createGpu();
+
+/** Provides the page's GPU once it exists; a failure reaches the boundary above. */
+function PageGpu({ children }: { children: ReactNode }) {
+ return <GpuProvider gpu={use(gpu)}>{children}</GpuProvider>;
+}
+
const root = document.getElementById("root");
if (root) {
createRoot(root).render(
<>
<GpuBoundary>
- <GpuProvider>
- <TooltipProvider delay={500}>
- <App />
- </TooltipProvider>
- </GpuProvider>
+ <Suspense>
+ <PageGpu>
+ <TooltipProvider delay={500}>
+ <App />
+ </TooltipProvider>
+ </PageGpu>
+ </Suspense>
</GpuBoundary>
{statsEnabled && <GpuStats />}
{/* Their scripts load from Vercel's CDN; development and tests stay offline. */}
diff --git a/tests/loaders.e2e.ts b/tests/loaders.e2e.ts
index eba492a..7407635 100644
--- a/tests/loaders.e2e.ts
+++ b/tests/loaders.e2e.ts
@@ -1,6 +1,54 @@
import { expect, openPhoto, test } from "./fixtures";
import { readImage } from "./images";
+test("an image beyond the GPU's texture limit fails before decoding, saying how large it is", async ({
+ page,
+}) => {
+ await openPhoto(page);
+ const limit = await page.evaluate(
+ async () =>
+ (await navigator.gpu.requestAdapter())?.limits.maxTextureDimension2D ?? 0,
+ );
+ const count = new Intl.NumberFormat("en-US");
+ const over = limit + 1;
+ // A PNG header with nothing decodable after it: the size error shows the check came first.
+ const png = Buffer.alloc(40);
+ png.write("\x89PNG\r\n\x1a\n", "latin1");
+ png.writeUInt32BE(13, 8);
+ png.write("IHDR", 12);
+ png.writeUInt32BE(over, 16);
+ png.writeUInt32BE(1, 20);
+ const svg = `<svg xmlns="http://www.w3.org/2000/svg" width="10" height="${over}"/>`;
+ const cases = [
+ {
+ file: { name: "wide.png", mimeType: "image/png", buffer: png },
+ size: `${count.format(over)} × 1`,
+ },
+ {
+ file: {
+ name: "tall.svg",
+ mimeType: "image/svg+xml",
+ buffer: Buffer.from(svg),
+ },
+ size: `10 × ${count.format(over)}`,
+ },
+ ];
+ for (const { file, size } of cases) {
+ const chooser = page.waitForEvent("filechooser");
+ await page.getByRole("button", { name: "Open an image or scene" }).click();
+ await (await chooser).setFiles(file);
+ await expect(
+ page.getByText(
+ `Couldn't open ${file.name}: Error: This device opens images up to ${count.format(limit)} pixels per side; this one is ${size}.`,
+ ),
+ ).toBeVisible();
+ await page.getByRole("button", { name: "Dismiss", exact: true }).click();
+ }
+ expect(await page.evaluate(() => window.openlight.getState().file)).toBe(
+ "photo.svg",
+ );
+});
+
test("a photo takes XMP settings, survives a failed open, and exports while another replaces it", async ({
page,
}) => {