Skip to content

Free open-source image editor for the browser

Replace embedded TIFF decoder with raw-webgpu (#17)

Rodrigo Belfiorec2c1fbd531aa
diff --git a/AGENTS.md b/AGENTS.md
index c9ce24a..961ceca 100644
--- a/AGENTS.md
+++ b/AGENTS.md
@@ -56,15 +56,15 @@ All React bindings come from `vgpu-react`; everything else comes from `vgpu`.
- Shader code lives in `.wgsl` files beside the module that owns it, or in `lib/` for reusable GPU operations. The Vite loader and ambient types are already configured.
- The working space is linear Rec.2020 in `rgba16float`. Decoders convert into it, the editor shader converts out of it; nothing in between assumes a format or primaries, so both can change (an 8-bit variant is planned).
- `lib/adjustments/adjustments.wgsl` works in that space; its `Adjustments` struct mirrors the TypeScript type in the UI's units, and its constants were fitted against reference exports; treat them as data, not formulas to tidy.
-- `lib/decode` converts every format into the working space. Browser-decoded formats go through `linearize`. TIFF uses `lib/tiff-gpu`, which returns a linear Rec.2020 target on its own: `prepareTiff` (directory, color profile, CPU codecs) runs in the worker and `uploadTiff` does the rest on the GPU. Reuse `workerDecoder` for decoders to come (16-bit PNG, RAW).
+- `lib/decode` converts every format into the working space. Browser-decoded formats go through `linearize`. TIFF and camera RAW use `raw-webgpu`; OpenLight adapts the returned GPU resources to document ownership. Codec implementation and coverage belong to the package.
- Samples above 1.0 (HDR) keep their headroom through exposure, curves, and vibrance; negatives clip at exposure. Exposure scales all channels by one luminance gain so hue holds, and `display()` in `lib/color.wgsl` maps out-of-gamut colors toward their luminance, so bright saturated light rolls off to white instead of clipping per channel.
## Testing
-Prefer a few broad integration tests over many granular tests. Run document, history, loader, and renderer orchestration checks in Bun using real application modules and `vgpu/mock`. Keep GPU pixel, browser UI, and codec checks in Playwright; the mock does not execute shaders. Keep one main editing session that opens a fixture, changes controls, checks the preview and histogram, edits curves, uses undo/redo, and exports. Extend that session with named steps instead of adding a test per control. Check actual pixels against known values, with a small tolerance when needed; changed state or a changed screenshot alone does not prove correct rendering. Keep independent loading and rendering checks separate, and load real files for format coverage. Use browser-free tests for invariants that the editing session cannot meaningfully exercise. A library that may leave the repo, such as `lib/tiff-gpu`, keeps its tests and fixtures in its own folder with sibling imports only.
+Prefer a few broad integration tests over many granular tests. Run document, history, loader, and renderer orchestration checks in Bun using real application modules and `vgpu/mock`. Keep GPU pixel, browser UI, and codec checks in Playwright; the mock does not execute shaders. Keep one main editing session that opens a fixture, changes controls, checks the preview and histogram, edits curves, uses undo/redo, and exports. Extend that session with named steps instead of adding a test per control. Check actual pixels against known values, with a small tolerance when needed; changed state or a changed screenshot alone does not prove correct rendering. Keep independent loading and rendering checks separate, and load real files for format coverage. Use browser-free tests for invariants that the editing session cannot meaningfully exercise. Codec libraries keep their tests and fixtures outside OpenLight; the app keeps only integration fixtures.
Maintain a browser-side control API with semantic commands for loading, editing, and reading state. Extend it with new workflows; it must work in local browsers, CI, and remote sessions. Use DOM interaction when testing the UI itself. Wait for visible results without adding production completion tracking.
## Done
-`bun run check` formats and lints. `bun run build` type-checks. `bun run test` runs browser-free tests. `bun run test:gpu` also runs the shader tests of `lib/tiff-gpu` through vgpu's Node entry. `bun run test:browser` runs GPU/browser tests. Run all of them after changing files and before a commit; CI runs only the first three.
+`bun run check` formats and lints. `bun run build` type-checks. `bun run test` runs browser-free tests. `bun run test:browser` runs GPU/browser tests. Run all of them after changing files and before a commit; CI runs only the first three.
diff --git a/README.md b/README.md
index 3f2f376..9875cdb 100644
--- a/README.md
+++ b/README.md
@@ -31,7 +31,7 @@ bun install
bun dev
```
-Run `bun run check` to format and lint, `bun run build` to type-check and build, and `bun run test` for integration tests. Shader tests run with `bun run test:gpu`, and GPU and UI tests with `bun run test:browser`, which requires Chromium with WebGPU.
+Run `bun run check` to format and lint, `bun run build` to type-check and build, and `bun run test` for integration tests. GPU and UI tests run with `bun run test:browser`, which requires Chromium with WebGPU.
## Scripting
diff --git a/TODO.md b/TODO.md
deleted file mode 100644
index cfe9ef4..0000000
--- a/TODO.md
+++ /dev/null
@@ -1,12 +0,0 @@
-# TODO
-
-## Evaluate replacing tiff-gpu with raw-webgpu
-
-Keep the current TIFF loader until raw-webgpu covers the TIFF formats already supported by OpenLight and its performance is acceptable.
-
-- Add support for bilevel, palette and float64 TIFF in raw-webgpu. These are the three known coverage regressions in the shared 21-fixture corpus. Both loaders currently reject the YCbCr JPEG fixture.
-- Verify reference pixels, ICC color conversion, alpha, orientation, HDR and precision with every TIFF fixture.
-- Compare loading time and memory on the large compressed and uncompressed TIFF files in `public/debug`. The measured raw-webgpu path was slower; shared RAW infrastructure alone is not a reason to accept that regression.
-- Once coverage and performance are accepted, route TIFF through raw-webgpu and remove the superseded loader and dependencies. Keep the integration small and preserve regression coverage.
-
-Verify with `bun run test:tiff` and `bun run benchmark:tiff` in raw-webgpu.
diff --git a/bun.lock b/bun.lock
index 0cbe4fd..364fb3b 100644
--- a/bun.lock
+++ b/bun.lock
@@ -11,8 +11,7 @@
"@vercel/speed-insights": "^2.0.0",
"class-variance-authority": "^0.7.1",
"cn": "^0.2.4",
- "gl-matrix": "^3.4.4",
- "raw-webgpu": "0.1.0-alpha.1",
+ "raw-webgpu": "0.1.0-alpha.2",
"react": "^19.2.8",
"react-dom": "^19.2.8",
"tailwindcss": "^4.3.3",
@@ -223,8 +222,6 @@
"fsevents": ["[email protected]", "", { "os": "darwin" }, "sha512-5xoDfX+fL7faATnagmWPpbFtwh/R77WmMMqqHGS65C3vvB0YHrgF+B1YmZ3441tMj5n63k0212XNoJwzlhffQw=="],
- "gl-matrix": ["[email protected]", "", {}, "sha512-latSnyDNt/8zYUB6VIJ6PCh2jBjJX6gnDsoCZ7LyW7GkqrD51EWwa9qCoGixj8YqBtETQK/xY7OmpTF8xz1DdQ=="],
-
"graceful-fs": ["[email protected]", "", {}, "sha512-RbJ5/jmFcNNCcDV5o9eTnBLJ/HszWV0P73bc+Ff4nS/rJj+YaS6IGyiOL0VoBYX+l1Wrl3k63h/KrH+nhJ0XvQ=="],
"jiti": ["[email protected]", "", { "bin": { "jiti": "lib/jiti-cli.mjs" } }, "sha512-AC/7JofJvZGrrneWNaEnJeOLUx+JlGt7tNa0wZiRPT4MY1wmfKjt2+6O2p2uz2+skll8OZZmJMNqeke7kKbNgQ=="],
@@ -273,7 +270,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-sDPYERiNCRO6OhyvH62lgD1CBmRon2GrbeJaAt12nCCnVivd5ROzBHTH94o7dAS7hmLScGlsHSk6L35iFZX9hw=="],
+ "raw-webgpu": ["[email protected]", "", {}, "sha512-YajtCnv1h/KUDPQdsn+nuYvs9IGBoVvlR6YNJveIP4gwh54rfYMDmGv8nG+SWtWhnUyoLMX0xDhwkvOjBUpzzA=="],
"react": ["[email protected]", "", {}, "sha512-PWaYA1L/q9u2u7xYQi+Y3L3Yfnie7XyLeaJICV1MGD6LprsBxcAqGjYyr0eY3p+QdsA+x/Irkt4Qif8D63+Sbw=="],
diff --git a/package.json b/package.json
index 45bc674..9f4cd34 100644
--- a/package.json
+++ b/package.json
@@ -7,7 +7,6 @@
"build": "tsc && vite build",
"check": "biome check --write",
"test": "bun test",
- "test:gpu": "GPU=1 bun test",
"test:browser": "playwright test"
},
"dependencies": {
@@ -17,8 +16,7 @@
"@vercel/speed-insights": "^2.0.0",
"class-variance-authority": "^0.7.1",
"cn": "^0.2.4",
- "gl-matrix": "^3.4.4",
- "raw-webgpu": "0.1.0-alpha.1",
+ "raw-webgpu": "0.1.0-alpha.2",
"react": "^19.2.8",
"react-dom": "^19.2.8",
"tailwindcss": "^4.3.3",
diff --git a/src/lib/decode/index.ts b/src/lib/decode/index.ts
index c86fbb3..799e708 100644
--- a/src/lib/decode/index.ts
+++ b/src/lib/decode/index.ts
@@ -1,33 +1,12 @@
import type { Gpu, Target } from "vgpu";
import { createImageSource, type ImageSource } from "@/lib/image-source";
-import { uploadTiff } from "@/lib/tiff-gpu";
import { decodeHeic } from "./heic";
import linearize from "./linearize";
import decodeSvg from "./svg";
-import type { Decoded, Decoder } from "./types";
+import type { Decoder } from "./types";
export type { Target };
-/** Decoder backed by a worker module: post the file, receive transferred pixels or an error. */
-export function workerDecoder(
- load: () => Promise<{ default: new () => Worker }>,
-) {
- return async (): Promise<Decoder> => {
- const { default: Spawn } = await load();
- return (file) =>
- new Promise((resolve, reject) => {
- const worker = new Spawn();
- worker.onmessage = ({
- data,
- }: MessageEvent<Decoded | { error: string }>) => {
- "error" in data ? reject(new Error(data.error)) : resolve(data);
- worker.terminate();
- };
- worker.postMessage(file);
- });
- };
-}
-
type Format = {
types: string[];
extensions: string[];
@@ -39,16 +18,15 @@ function pixelFormat(load: () => Promise<Decoder>) {
return async (gpu: Gpu, file: File) => {
const decoder = await load();
const decoded = await decoder(file);
- return createImageSource(
- "chunks" in decoded ? uploadTiff(gpu, decoded) : linearize(gpu, decoded),
- );
+ return createImageSource(linearize(gpu, decoded));
};
}
const native = pixelFormat(async () => createImageBitmap);
const svg = pixelFormat(async () => decodeSvg);
const heic = pixelFormat(async () => decodeHeic);
-const tiff = pixelFormat(workerDecoder(() => import("./tiff.worker?worker")));
+const tiff = async (gpu: Gpu, file: File) =>
+ (await import("./tiff")).decodeTiff(gpu, file);
const raw = async (gpu: Gpu, file: File) =>
(await import("@/lib/raw")).decodeRaw(gpu, file);
diff --git a/src/lib/decode/linearize.ts b/src/lib/decode/linearize.ts
index 3a0d8b8..197c420 100644
--- a/src/lib/decode/linearize.ts
+++ b/src/lib/decode/linearize.ts
@@ -1,5 +1,4 @@
import { effect, frame, type Gpu, target } from "vgpu";
-import type { Prepared } from "@/lib/tiff-gpu";
import shader from "./linearize.wgsl";
import type { Decoded } from "./types";
@@ -19,7 +18,7 @@ function staging(gpu: Gpu, size: Size) {
}
/** Uploads the decoded image into a staging texture, tiles laid out on their grid. */
-function stage(gpu: Gpu, decoded: Exclude<Decoded, Prepared>) {
+function stage(gpu: Gpu, decoded: Decoded) {
const queue = gpu.gpu.queue;
const size: Size = [decoded.width, decoded.height];
if (decoded instanceof ImageBitmap) {
@@ -66,10 +65,7 @@ function stage(gpu: Gpu, decoded: Exclude<Decoded, Prepared>) {
}
/** GPU leg for sRGB-encoded decoders: renders the decoded image into a linear Rec.2020 rgba16float target. */
-export default function linearize(
- gpu: Gpu,
- decoded: Exclude<Decoded, Prepared>,
-) {
+export default function linearize(gpu: Gpu, decoded: Decoded) {
const frames = "tiles" in decoded ? decoded : undefined;
const rotation = frames?.rotation ?? 0;
const p3 = Number(isP3(frames?.tiles[0]));
diff --git a/src/lib/decode/tiff.ts b/src/lib/decode/tiff.ts
new file mode 100644
index 0000000..c9dc177
--- /dev/null
+++ b/src/lib/decode/tiff.ts
@@ -0,0 +1,37 @@
+import {
+ type Effect,
+ effect,
+ frame,
+ type Gpu,
+ type Target,
+ target,
+} from "vgpu";
+import { createImageSource } from "@/lib/image-source";
+import shader from "./tiff.wgsl";
+
+const transfers = new WeakMap<Gpu, Effect>();
+
+/** Transfers package-owned pixels into the editor's owned vgpu target, entirely on GPU. */
+export async function decodeTiff(gpu: Gpu, file: File) {
+ const { decodeTiff } = await import("raw-webgpu");
+ const decoded = await decodeTiff(gpu.gpu, file);
+ let image: Target | undefined;
+ try {
+ const transfer = transfers.get(gpu) ?? effect(gpu, shader);
+ transfers.set(gpu, transfer);
+ const output = target(gpu, { size: decoded.size, format: "rgba16float" });
+ image = output;
+ frame(gpu, (frame) =>
+ frame.pass(
+ output,
+ transfer.set({ source: decoded.texture.createView() }),
+ ),
+ );
+ return createImageSource(image);
+ } catch (error) {
+ image?.color.dispose();
+ throw error;
+ } finally {
+ decoded.dispose();
+ }
+}
diff --git a/src/lib/decode/tiff.wgsl b/src/lib/decode/tiff.wgsl
new file mode 100644
index 0000000..fef615c
--- /dev/null
+++ b/src/lib/decode/tiff.wgsl
@@ -0,0 +1,5 @@
+@group(0) @binding(0) var source: texture_2d<f32>;
+
+@fragment fn fs_main(@builtin(position) position: vec4f) -> @location(0) vec4f {
+ return textureLoad(source, vec2i(position.xy), 0);
+}
diff --git a/src/lib/decode/tiff.worker.ts b/src/lib/decode/tiff.worker.ts
deleted file mode 100644
index 7b39b23..0000000
--- a/src/lib/decode/tiff.worker.ts
+++ /dev/null
@@ -1,16 +0,0 @@
-import { prepareTiff } from "@/lib/tiff-gpu/prepare";
-
-self.onmessage = async ({ data }: MessageEvent<Blob>) => {
- try {
- const prepared = await prepareTiff(await data.arrayBuffer());
- self.postMessage(prepared, {
- transfer: [
- prepared.data.buffer,
- prepared.chunks.buffer,
- prepared.curves.buffer,
- ],
- });
- } catch (error) {
- self.postMessage({ error: String(error) });
- }
-};
diff --git a/src/lib/decode/types.ts b/src/lib/decode/types.ts
index 91955b4..c693006 100644
--- a/src/lib/decode/types.ts
+++ b/src/lib/decode/types.ts
@@ -1,5 +1,3 @@
-import type { Prepared } from "@/lib/tiff-gpu";
-
/** Raw interleaved rgba8, for decoders that don't go through the browser's image pipeline. */
export type Pixels = {
width: number;
@@ -14,5 +12,5 @@ export type Frames = {
columns: number;
rotation: number;
};
-export type Decoded = ImageBitmap | Pixels | Frames | Prepared;
+export type Decoded = ImageBitmap | Pixels | Frames;
export type Decoder = (file: Blob) => Promise<Decoded>;
diff --git a/src/lib/tiff-gpu/README.md b/src/lib/tiff-gpu/README.md
deleted file mode 100644
index 881c99d..0000000
--- a/src/lib/tiff-gpu/README.md
+++ /dev/null
@@ -1,64 +0,0 @@
-# tiff-gpu
-
-Decodes TIFF files into a linear RGB texture, the way a browser hands you a JPEG: color profile applied, orientation applied, alpha straight. The CPU reads the directory and decompresses; the GPU does everything about pixels. Built on [vgpu](https://vgpu.sh); internal to OpenLight for now.
-
-## Usage
-
-```ts
-import { decodeTiff } from "@/lib/tiff-gpu";
-
-const image = await decodeTiff(gpu, await file.arrayBuffer());
-// a vgpu Target: rgba16float, linear Rec.2020 by default
-```
-
-Options: `colorSpace` picks the output primaries, `"rec2020"` (default, holds every photo gamut), `"srgb"`, `"display-p3"`, or `"none"` to keep sample values as they are; `format` can be `"rgba32float"` to keep those values exact; `image` decodes a layout other than the first, such as a DNG SubIFD. `prepareTiff(bytes, options)` is the CPU half and runs in a worker, returning transferable buffers; `uploadTiff(gpu, prepared, options)` is the GPU half and returns synchronously once its work is queued.
-
-Color comes from the file's ICC profile when it is the matrix/TRC kind every photo editor writes, otherwise samples are treated as sRGB; float samples are taken as linear. Values above 1.0 and below 0.0 pass through.
-
-## Coverage
-
-| | Supported |
-| --- | --- |
-| Container | TIFF and BigTIFF, both byte orders, strips and tiles, chunky and planar, first image |
-| Samples | Unsigned integers of any depth up to 32 bits, packed or byte-aligned; 16, 32, 64-bit floats |
-| Color | RGB, grayscale (black or white is zero), palette, camera raw mosaics, alpha as an extra sample |
-| Compression | None, LZW, Deflate (8 and 32946), PackBits |
-| Predictors | Horizontal (GPU) and floating point (CPU) |
-
-Not supported: JPEG, CMYK, YCbCr, Lab, signed integers, sub-byte fill order.
-
-## How it works
-
-`prepareTiff` parses the directory, turns the ICC profile into a curve table and a matrix, and decompresses every strip or tile into raw rows with the CPU codecs; uncompressed files are used in place. Codecs are serial per strip, and a worker keeps them off the page.
-
-`uploadTiff` splits the rows into bands sized to the device's buffer limits, so file size is not bounded by them, and runs one compute pass per band that walks each chunk row: byte order, bit depth, horizontal prediction, plane interleaving, palette lookup, alpha, the transfer curve, the matrix into the output primaries, and orientation. A small render pass places each band in the target.
-
-## Benchmarks
-
-24 MP RGB, Apple M-series, Chromium, median of 3, file bytes to GPU texture. The geotiff.js column is a JavaScript decoder measured for reference.
-
-| File | geotiff.js | tiff-gpu |
-| --- | --- | --- |
-| 16-bit uncompressed, 144 MB | 882 ms | 42 ms |
-| 16-bit LZW | 4216 ms, crashes on large strips | ~850 ms |
-| 16-bit ZIP | ~2500 ms | 540 to 1100 ms |
-
-Compressed files spend their time in the CPU codec. GPU kernels for LZW and Deflate were built and measured too: 2 to 4× faster on files with thousands of strips, slower on files with few, and about 350 lines. They were removed for simplicity and live in this branch's history (commit `ac6d140`); splitting strips at LZW clear codes would let them win on every file.
-
-Run the benchmark on your own files with `bun run --preload ./tests/setup.ts src/lib/tiff-gpu/bench.ts /folder/of/tiffs`.
-
-## Building a RAW loader on top
-
-Camera raw formats are TIFF containers, so a RAW loader is a few pieces on top of this one:
-
-- `readTiff(bytes)` lists every directory with its SubIFDs and gives typed access to any tag, so a DNG loader can find the raw image (photometric CFA or linear raw) and read its black level, white level, and color matrices. `parseTiff(bytes, directory)` turns that directory into a layout, passed to `prepareTiff` as `image`.
-- Codecs are a table keyed by compression code in `codecs.ts`; lossless JPEG (code 7) is one entry away.
-- Raw mosaics decode as gray with `colorSpace: "none"` and `format: "rgba32float"`, which yields the exact sample values in a target; demosaicing and camera color then run as passes of their own.
-
-## Tests
-
-Everything the library needs lives in this folder: `fixtures/` with reference pixels, `testing.ts` with the helpers, and `tiff-gpu.test.ts`, which covers the directory reader, codecs, profiles, every fixture against its reference in linear Rec.2020, raw output, and row banding. The shaders run for real under `bun run test:gpu` through vgpu's Node entry, so no page or browser is involved; plain `bun test` and CI skip them.
-
-## Follow-ups
-
-A persistent worker would save the spawn cost per file; OpenLight already runs `prepareTiff` in a fresh one.
diff --git a/src/lib/tiff-gpu/bench.ts b/src/lib/tiff-gpu/bench.ts
deleted file mode 100644
index 0fe4be3..0000000
--- a/src/lib/tiff-gpu/bench.ts
+++ /dev/null
@@ -1,22 +0,0 @@
-import { readdir } from "node:fs/promises";
-import { init } from "vgpu/node";
-import { benchmark } from "./testing";
-
-/** `bun run --preload ./tests/setup.ts src/lib/tiff-gpu/bench.ts <folder>`: times both halves on every TIFF in the folder. */
-const folder = process.argv[2];
-if (!folder) {
- throw new Error("Pass a folder of TIFF files.");
-}
-const gpu = await init();
-const variants = { default: {} };
-for (const name of (await readdir(folder))
- .filter((n) => /\.tiff?$/i.test(n))
- .sort()) {
- const row = await benchmark(
- gpu,
- await Bun.file(`${folder}/${name}`).arrayBuffer(),
- variants,
- );
- console.log(name, JSON.stringify(row));
-}
-gpu.dispose();
diff --git a/src/lib/tiff-gpu/blit.wgsl b/src/lib/tiff-gpu/blit.wgsl
deleted file mode 100644
index 039b717..0000000
--- a/src/lib/tiff-gpu/blit.wgsl
+++ /dev/null
@@ -1,14 +0,0 @@
-struct Rect { origin: vec2u, rowWords: u32, wide: u32 }
-@group(0) @binding(0) var<uniform> rect: Rect;
-@group(0) @binding(1) var<storage, read> pixels: array<u32>;
-
-// Copies a band's packed pixels into its rectangle of the target; the pass scissor bounds it.
-@fragment fn fs_main(@builtin(position) position: vec4f) -> @location(0) vec4f {
- let p = vec2u(position.xy) - rect.origin;
- if rect.wide == 1u {
- let at = p.y * rect.rowWords + p.x * 4u;
- return vec4f(bitcast<f32>(pixels[at]), bitcast<f32>(pixels[at + 1u]), bitcast<f32>(pixels[at + 2u]), bitcast<f32>(pixels[at + 3u]));
- }
- let at = p.y * rect.rowWords + p.x * 2u;
- return vec4f(unpack2x16float(pixels[at]), unpack2x16float(pixels[at + 1u]));
-}
diff --git a/src/lib/tiff-gpu/codecs.ts b/src/lib/tiff-gpu/codecs.ts
deleted file mode 100644
index b36826e..0000000
--- a/src/lib/tiff-gpu/codecs.ts
+++ /dev/null
@@ -1,136 +0,0 @@
-/** TIFF LZW (MSB-first, early change) into a preallocated output. */
-export function decodeLzw(input: Uint8Array, output: Uint8Array) {
- const prefix = new Uint16Array(4096);
- const suffix = new Uint8Array(4096);
- const length = new Uint16Array(4096);
- for (let i = 0; i < 256; i++) {
- suffix[i] = i;
- length[i] = 1;
- }
- let next = 258;
- let width = 9;
- let prev = -1;
- let bit = 0;
- let out = 0;
- const bits = input.length * 8;
- while (bit + width <= bits && out < output.length) {
- const p = bit >> 3;
- const window =
- (input[p] << 16) | ((input[p + 1] ?? 0) << 8) | (input[p + 2] ?? 0);
- const code = (window >>> (24 - (bit & 7) - width)) & ((1 << width) - 1);
- bit += width;
- if (code === 256) {
- next = 258;
- width = 9;
- prev = -1;
- continue;
- }
- if (code === 257) {
- break;
- }
- if (prev < 0) {
- output[out++] = code;
- prev = code;
- continue;
- }
- let first: number;
- if (code < next) {
- const n = length[code];
- for (let k = n - 1, c = code; k >= 0; k--) {
- output[out + k] = suffix[c];
- c = prefix[c];
- }
- first = output[out];
- out += n;
- } else {
- const n = length[prev] + 1;
- for (let k = n - 2, c = prev; k >= 0; k--) {
- output[out + k] = suffix[c];
- c = prefix[c];
- }
- first = output[out];
- output[out + n - 1] = first;
- out += n;
- }
- if (next < 4096) {
- prefix[next] = prev;
- suffix[next] = first;
- length[next] = length[prev] + 1;
- next++;
- }
- if (next + 1 >= 1 << width && width < 12) {
- width++;
- }
- prev = code;
- }
-}
-
-export function decodePackBits(input: Uint8Array, output: Uint8Array) {
- let out = 0;
- for (let i = 0; i < input.length && out < output.length; ) {
- const n = (input[i++] << 24) >> 24;
- if (n >= 0) {
- const count = Math.min(n + 1, output.length - out);
- output.set(input.subarray(i, i + count), out);
- i += n + 1;
- out += count;
- } else if (n !== -128) {
- output.fill(input[i++], out, out + 1 - n);
- out += 1 - n;
- }
- }
-}
-
-export async function inflate(
- input: Uint8Array<ArrayBuffer>,
- output: Uint8Array,
-) {
- const stream = new Blob([input])
- .stream()
- .pipeThrough(new DecompressionStream("deflate"));
- output.set(
- new Uint8Array(await new Response(stream).arrayBuffer()).subarray(
- 0,
- output.length,
- ),
- );
-}
-
-/** Predictor 3: bytes are differenced along the row, then stored as planes from most to least significant. */
-export function undoFloatPrediction(
- data: Uint8Array,
- rowBytes: number,
- stride: number,
- bytesPerSample: number,
- littleEndian: boolean,
-) {
- const planes = new Uint8Array(rowBytes);
- const count = rowBytes / bytesPerSample;
- for (let start = 0; start + rowBytes <= data.length; start += rowBytes) {
- const row = data.subarray(start, start + rowBytes);
- for (let i = stride; i < rowBytes; i++) {
- row[i] = (row[i] + row[i - stride]) & 255;
- }
- planes.set(row);
- for (let i = 0; i < count; i++) {
- for (let b = 0; b < bytesPerSample; b++) {
- row[i * bytesPerSample + b] =
- planes[(littleEndian ? bytesPerSample - b - 1 : b) * count + i];
- }
- }
- }
-}
-
-export type Codec = (
- input: Uint8Array<ArrayBuffer>,
- output: Uint8Array,
-) => void | Promise<void>;
-
-/** Decoders by TIFF compression code; a RAW loader adds its own here. */
-export const codecs: Record<number, Codec> = {
- 1: (input, output) => output.set(input.subarray(0, output.length)),
- 5: decodeLzw,
- 8: inflate,
- 32946: inflate,
- 32773: decodePackBits,
-};
diff --git a/src/lib/tiff-gpu/color.ts b/src/lib/tiff-gpu/color.ts
deleted file mode 100644
index 6302c53..0000000
--- a/src/lib/tiff-gpu/color.ts
+++ /dev/null
@@ -1,181 +0,0 @@
-import { mat3 } from "gl-matrix";
-
-export type ColorSpace = "rec2020" | "srgb" | "display-p3";
-type Curve = (encoded: number) => number;
-type Profile = { colorants: mat3; curves: [Curve, Curve, Curve] };
-
-const tableSize = 1024;
-const d50 = [0.9642, 1, 0.8249] as const;
-const identity: Curve = (v) => v;
-
-/** XYZ (D50, the ICC connection space) to linear RGB with D65 white, Bradford-adapted. Column-major. */
-const targets: Record<ColorSpace, mat3> = {
- rec2020: mat3.fromValues(
- 1.6472945,
- -0.6826024,
- 0.0296711,
- -0.3935777,
- 1.6475829,
- -0.0629319,
- -0.2359823,
- 0.0128128,
- 1.253617,
- ),
- srgb: mat3.fromValues(
- 3.1337826,
- -0.9787618,
- 0.0719669,
- -1.6168778,
- 1.9161365,
- -0.2290271,
- -0.4906235,
- 0.0334411,
- 1.4057905,
- ),
- "display-p3": mat3.fromValues(
- 2.4040638,
- -0.8422108,
- 0.0481952,
- -0.9898529,
- 1.7988097,
- -0.0973884,
- -0.3976465,
- 0.0160419,
- 1.2737316,
- ),
-};
-
-/** What untagged samples mean: sRGB colorants adapted to D50, and the sRGB curve. */
-const srgbCurve: Curve = (v) =>
- v <= 0.04045 ? v / 12.92 : ((v + 0.055) / 1.055) ** 2.4;
-const srgb: Profile = {
- colorants: mat3.fromValues(
- 0.4361,
- 0.2225,
- 0.0139,
- 0.3851,
- 0.7169,
- 0.0971,
- 0.1431,
- 0.0606,
- 0.7141,
- ),
- curves: [srgbCurve, srgbCurve, srgbCurve],
-};
-
-/**
- * Reads a matrix/TRC profile: RGB colorants and a curve per channel, or a single curve for gray.
- * Profiles built on lookup tables, which photo files do not use, and malformed ones return undefined.
- */
-export function readProfile(bytes: Uint8Array): Profile | undefined {
- const view = new DataView(bytes.buffer, bytes.byteOffset, bytes.byteLength);
- const text = (at: number) =>
- String.fromCharCode(...bytes.subarray(at, at + 4));
- const fixed = (at: number) => view.getInt32(at) / 65536;
- if (bytes.length < 132 || text(36) !== "acsp") {
- return;
- }
- // The tag table follows the 128-byte header: a count, then 12-byte entries of signature, offset, size.
- const tags = new Map<string, number>();
- for (let i = 0, count = view.getUint32(128); i < count; i++) {
- tags.set(text(132 + i * 12), view.getUint32(136 + i * 12));
- }
- const xyz = (name: string) => {
- const at = tags.get(name);
- return at === undefined
- ? undefined
- : ([fixed(at + 8), fixed(at + 12), fixed(at + 16)] as const);
- };
- const curve = (name: string): Curve | undefined => {
- const at = tags.get(name);
- if (at === undefined) {
- return;
- }
- if (text(at) === "curv") {
- // A count of 0 is identity, 1 is a gamma in 8.8 fixed point, more is a table of 16-bit values.
- const count = view.getUint32(at + 8);
- const entry = (i: number) =>
- view.getUint16(at + 12 + 2 * Math.min(i, count - 1)) / 65535;
- if (count === 0) {
- return identity;
- }
- if (count === 1) {
- const gamma = view.getUint16(at + 12) / 256;
- return (x) => x ** gamma;
- }
- return (x) => {
- const position = x * (count - 1);
- const low = Math.floor(position);
- return entry(low) + (entry(low + 1) - entry(low)) * (position - low);
- };
- }
- if (text(at) === "para") {
- // Parametric kinds 0 to 4 take 1, 3, 4, 5, or 7 parameters: gamma, then a, b, c, d, e, f.
- const kind = view.getUint16(at + 8);
- const [g, a = 1, b = 0, c = 0, d = 0, e = 0, f = 0] = Array.from(
- { length: [1, 3, 4, 5, 7][kind] ?? 0 },
- (_, i) => fixed(at + 12 + i * 4),
- );
- if (kind === 0) {
- return (x) => x ** g;
- }
- if (kind <= 2) {
- return (x) => (x >= -b / a ? (a * x + b) ** g + c : c);
- }
- return (x) => (x >= d ? (a * x + b) ** g + e : c * x + f);
- }
- };
- try {
- if (text(16) === "GRAY") {
- const k = curve("kTRC");
- return (
- k && {
- colorants: mat3.fromValues(...d50, 0, 0, 0, 0, 0, 0),
- curves: [k, k, k],
- }
- );
- }
- const [r, g, b] = [xyz("rXYZ"), xyz("gXYZ"), xyz("bXYZ")];
- const [rc, gc, bc] = [curve("rTRC"), curve("gTRC"), curve("bTRC")];
- if (text(16) !== "RGB " || !r || !g || !b || !rc || !gc || !bc) {
- return;
- }
- return {
- colorants: mat3.fromValues(...r, ...g, ...b),
- curves: [rc, gc, bc],
- };
- } catch {
- return;
- }
-}
-
-const sample = (curves: Profile["curves"]) =>
- Float32Array.from({ length: 3 * tableSize }, (_, i) =>
- curves[Math.floor(i / tableSize)]((i % tableSize) / (tableSize - 1)),
- );
-
-/** Curve table over 0..1 for the shader and the matrix from the file's primaries into `colorSpace`; `none` keeps values. */
-export function colorOf(
- icc: Uint8Array | undefined,
- linear: boolean,
- colorSpace: ColorSpace | "none",
-) {
- if (colorSpace === "none") {
- return {
- table: sample([identity, identity, identity]),
- matrix: [...mat3.create()],
- };
- }
- let profile = (icc && readProfile(icc)) ?? srgb;
- let table = sample(linear ? [identity, identity, identity] : profile.curves);
- if (!table.every(Number.isFinite)) {
- profile = srgb;
- table = sample(profile.curves);
- }
- return {
- table,
- matrix: [
- ...mat3.multiply(mat3.create(), targets[colorSpace], profile.colorants),
- ],
- };
-}
diff --git a/src/lib/tiff-gpu/fixtures/README.md b/src/lib/tiff-gpu/fixtures/README.md
deleted file mode 100644
index 980130e..0000000
--- a/src/lib/tiff-gpu/fixtures/README.md
+++ /dev/null
@@ -1,26 +0,0 @@
-# Fixtures
-
-Small synthetic TIFF files, one per feature, written once with tifffile. `reference.json` holds the output size and, for a few pixels of each, the linear Rec.2020 `rgba` values tiff-gpu must produce, computed from standard sRGB and ICC color math rather than from the shaders.
-
-Most files hold a 19×17 RGB gradient where red is `x·3000 + 123`, green `y·3500 + 45`, and blue `(x + y)·1700 + 67` in 16 bits, or those values shifted to 8 bits.
-
-| File | What it covers |
-| --- | --- |
-| `rgb16-le.tif` | Uncompressed strips, little-endian |
-| `rgb16-lzw-be.tif` | LZW, big-endian |
-| `rgb16-planar-tiled.tif` | Deflate, horizontal prediction, planar 16×16 tiles |
-| `rgb16-bigtiff.tif` | BigTIFF |
-| `rgb8-packbits.tif` | PackBits, 8-bit |
-| `lzw-strips.tif` | 200×130 gradient in 130 one-row LZW strips with prediction, enough for the GPU codec |
-| `lzw-tiles.tif` | 40×17 8-bit gray `(x·6 + y·3) % 256` in 16×16 LZW tiles with prediction, so bottom tiles are clipped |
-| `rgb16-prophoto.tif` | ICC profile with ProPhoto colorants and a gamma 1.8 `curv` |
-| `rgb8-srgb-table.tif` | ICC profile with sRGB colorants and a 256-entry `curv` table |
-| `gray16-para.tif` | 5×7 gray at 16384 with a `para` sRGB `kTRC` profile |
-| `bilevel.tif` | 1-bit, white is zero, black where `(x + y) % 3 == 0` |
-| `palette8.tif` | 8-bit indices `(x·7 + y·11) % 256` into a 256-entry color map |
-| `alpha16.tif` | 5×7 associated alpha: premultiplied `[8192, 16384, 24576]` at alpha 32768 |
-| `orientation-3.tif`, `orientation-6.tif` | Orientation tags 3 and 6 |
-| `float32-be.tif`, `float64.tif`, `half-predictor.tif` | Floats `-0.125, 0, 0.18, 0.5, 1, 2, 4` across a 7×3 image, big-endian 32-bit, 64-bit, and 16-bit with Deflate and floating-point prediction |
-| `precision16.tif` | 2×1 gray with adjacent values 32768 and 32800 |
-| `packed12.tif` | 9×5 gray `(x·397 + y·811) % 4096` in packed 12-bit samples, as raw camera files store them |
-| `rgb8-jpeg.tif` | JPEG compression, which the decoder must reject |
diff --git a/src/lib/tiff-gpu/fixtures/bilevel.tif b/src/lib/tiff-gpu/fixtures/bilevel.tif
deleted file mode 100644
index 5b07714..0000000
Binary files a/src/lib/tiff-gpu/fixtures/bilevel.tif and /dev/null differ
diff --git a/src/lib/tiff-gpu/fixtures/float64.tif b/src/lib/tiff-gpu/fixtures/float64.tif
deleted file mode 100644
index d3dc42b..0000000
Binary files a/src/lib/tiff-gpu/fixtures/float64.tif and /dev/null differ
diff --git a/src/lib/tiff-gpu/fixtures/half-predictor.tif b/src/lib/tiff-gpu/fixtures/half-predictor.tif
deleted file mode 100644
index fc8bc5c..0000000
Binary files a/src/lib/tiff-gpu/fixtures/half-predictor.tif and /dev/null differ
diff --git a/src/lib/tiff-gpu/fixtures/lzw-strips.tif b/src/lib/tiff-gpu/fixtures/lzw-strips.tif
deleted file mode 100644
index 8d0a4a8..0000000
Binary files a/src/lib/tiff-gpu/fixtures/lzw-strips.tif and /dev/null differ
diff --git a/src/lib/tiff-gpu/fixtures/lzw-tiles.tif b/src/lib/tiff-gpu/fixtures/lzw-tiles.tif
deleted file mode 100644
index 7f64e82..0000000
Binary files a/src/lib/tiff-gpu/fixtures/lzw-tiles.tif and /dev/null differ
diff --git a/src/lib/tiff-gpu/fixtures/orientation-3.tif b/src/lib/tiff-gpu/fixtures/orientation-3.tif
deleted file mode 100644
index 92dcb39..0000000
Binary files a/src/lib/tiff-gpu/fixtures/orientation-3.tif and /dev/null differ
diff --git a/src/lib/tiff-gpu/fixtures/packed12.tif b/src/lib/tiff-gpu/fixtures/packed12.tif
deleted file mode 100644
index 7c70846..0000000
Binary files a/src/lib/tiff-gpu/fixtures/packed12.tif and /dev/null differ
diff --git a/src/lib/tiff-gpu/fixtures/palette8.tif b/src/lib/tiff-gpu/fixtures/palette8.tif
deleted file mode 100644
index cb0f442..0000000
Binary files a/src/lib/tiff-gpu/fixtures/palette8.tif and /dev/null differ
diff --git a/src/lib/tiff-gpu/fixtures/precision16.tif b/src/lib/tiff-gpu/fixtures/precision16.tif
deleted file mode 100644
index 1fac721..0000000
Binary files a/src/lib/tiff-gpu/fixtures/precision16.tif and /dev/null differ
diff --git a/src/lib/tiff-gpu/fixtures/reference.json b/src/lib/tiff-gpu/fixtures/reference.json
deleted file mode 100644
index dacf28b..0000000
--- a/src/lib/tiff-gpu/fixtures/reference.json
+++ /dev/null
@@ -1,491 +0,0 @@
-[
- {
- "name": "rgb16-le.tif",
- "size": [19, 17],
- "tolerance": 0.0007,
- "points": [
- {
- "x": 0,
- "y": 0,
- "rgba": [
- 0.00011206924619723206, 5.980719895142648e-5, 7.792676543458332e-5, 1
- ]
- },
- {
- "x": 1,
- "y": 3,
- "rgba": [
- 0.010117439342137052, 0.020797571049808644, 0.011671048198589294, 1
- ]
- },
- {
- "x": 16,
- "y": 15,
- "rgba": [0.5390683815910875, 0.5994294774464016, 0.6102511497916351, 1]
- },
- {
- "x": 18,
- "y": 16,
- "rgba": [0.6706360408760472, 0.6985694759636359, 0.7478227732168989, 1]
- }
- ]
- },
- {
- "name": "rgb16-lzw-be.tif",
- "size": [19, 17],
- "tolerance": 0.0007,
- "points": [
- {
- "x": 0,
- "y": 0,
- "rgba": [
- 0.00011206924619723206, 5.980719895142648e-5, 7.792676543458332e-5, 1
- ]
- },
- {
- "x": 1,
- "y": 3,
- "rgba": [
- 0.010117439342137052, 0.020797571049808644, 0.011671048198589294, 1
- ]
- },
- {
- "x": 16,
- "y": 15,
- "rgba": [0.5390683815910875, 0.5994294774464016, 0.6102511497916351, 1]
- },
- {
- "x": 18,
- "y": 16,
- "rgba": [0.6706360408760472, 0.6985694759636359, 0.7478227732168989, 1]
- }
- ]
- },
- {
- "name": "rgb16-planar-tiled.tif",
- "size": [19, 17],
- "tolerance": 0.0007,
- "points": [
- {
- "x": 0,
- "y": 0,
- "rgba": [
- 0.00011206924619723206, 5.980719895142648e-5, 7.792676543458332e-5, 1
- ]
- },
- {
- "x": 1,
- "y": 3,
- "rgba": [
- 0.010117439342137052, 0.020797571049808644, 0.011671048198589294, 1
- ]
- },
- {
- "x": 16,
- "y": 15,
- "rgba": [0.5390683815910875, 0.5994294774464016, 0.6102511497916351, 1]
- },
- {
- "x": 18,
- "y": 16,
- "rgba": [0.6706360408760472, 0.6985694759636359, 0.7478227732168989, 1]
- }
- ]
- },
- {
- "name": "rgb16-bigtiff.tif",
- "size": [19, 17],
- "tolerance": 0.0007,
- "points": [
- {
- "x": 0,
- "y": 0,
- "rgba": [
- 0.00011206924619723206, 5.980719895142648e-5, 7.792676543458332e-5, 1
- ]
- },
- {
- "x": 1,
- "y": 3,
- "rgba": [
- 0.010117439342137052, 0.020797571049808644, 0.011671048198589294, 1
- ]
- },
- {
- "x": 16,
- "y": 15,
- "rgba": [0.5390683815910875, 0.5994294774464016, 0.6102511497916351, 1]
- },
- {
- "x": 18,
- "y": 16,
- "rgba": [0.6706360408760472, 0.6985694759636359, 0.7478227732168989, 1]
- }
- ]
- },
- {
- "name": "rgb8-packbits.tif",
- "size": [19, 17],
- "tolerance": 0.0007,
- "points": [
- { "x": 0, "y": 0, "rgba": [0.0, 0.0, 0.0, 1] },
- {
- "x": 9,
- "y": 8,
- "rgba": [
- 0.14613750676277001, 0.15225881542838723, 0.16366650375543837, 1
- ]
- },
- {
- "x": 18,
- "y": 16,
- "rgba": [0.6724960346921134, 0.6983424884589644, 0.7535052143823135, 1]
- }
- ]
- },
- {
- "name": "bilevel.tif",
- "size": [19, 17],
- "tolerance": 0.0007,
- "points": [
- { "x": 0, "y": 0, "rgba": [0.0, 0.0, 0.0, 1] },
- { "x": 9, "y": 8, "rgba": [1.0, 1.0, 1.0, 1] },
- { "x": 18, "y": 16, "rgba": [1.0, 1.0, 1.0, 1] }
- ]
- },
- {
- "name": "palette8.tif",
- "size": [19, 17],
- "tolerance": 0.0007,
- "points": [
- {
- "x": 0,
- "y": 0,
- "rgba": [
- 0.33056493057826697, 0.9198762799905402, 0.11451983665973267, 1
- ]
- },
- {
- "x": 9,
- "y": 8,
- "rgba": [
- 0.24102714812239392, 0.14901306143740237, 0.043763123507485645, 1
- ]
- },
- {
- "x": 18,
- "y": 16,
- "rgba": [
- 0.22837297873556617, 0.5885199011525982, 0.08307146706542554, 1
- ]
- }
- ]
- },
- {
- "name": "lzw-strips.tif",
- "size": [200, 130],
- "tolerance": 0.0007,
- "points": [
- {
- "x": 0,
- "y": 0,
- "rgba": [
- 0.00011206924619723206, 5.980719895142648e-5, 7.792676543458332e-5, 1
- ]
- },
- {
- "x": 77,
- "y": 33,
- "rgba": [
- 0.07805424711722604, 0.03876253107812007, 0.051234963783732955, 1
- ]
- },
- {
- "x": 199,
- "y": 129,
- "rgba": [0.7250959703236781, 0.5989518851902659, 0.5351422844895685, 1]
- }
- ]
- },
- {
- "name": "rgb16-prophoto.tif",
- "size": [19, 17],
- "tolerance": 0.002,
- "points": [
- {
- "x": 0,
- "y": 0,
- "rgba": [
- 1.406788234086084e-5, 1.2691486363781114e-6, 4.250983161659325e-6, 1
- ]
- },
- {
- "x": 9,
- "y": 8,
- "rgba": [
- 0.19980859709851734, 0.2176181879165905, 0.23021840843953595, 1
- ]
- },
- {
- "x": 18,
- "y": 16,
- "rgba": [0.692967378807906, 0.7572426560925016, 0.8004041047837895, 1]
- }
- ]
- },
- {
- "name": "rgb8-srgb-table.tif",
- "size": [19, 17],
- "tolerance": 0.0007,
- "points": [
- { "x": 0, "y": 0, "rgba": [0.0, 0.0, 0.0, 1] },
- {
- "x": 9,
- "y": 8,
- "rgba": [
- 0.14616855211682608, 0.15224697303015192, 0.16367509850451648, 1
- ]
- },
- {
- "x": 18,
- "y": 16,
- "rgba": [0.6726282614894274, 0.6982915860493553, 0.753541313596653, 1]
- }
- ]
- },
- {
- "name": "gray16-para.tif",
- "size": [7, 5],
- "tolerance": 0.002,
- "points": [
- {
- "x": 0,
- "y": 0,
- "rgba": [
- 0.05087761537052123, 0.05087761537052123, 0.05087761537052123, 1
- ]
- },
- {
- "x": 3,
- "y": 2,
- "rgba": [
- 0.05087761537052123, 0.05087761537052123, 0.05087761537052123, 1
- ]
- },
- {
- "x": 6,
- "y": 4,
- "rgba": [
- 0.05087761537052123, 0.05087761537052123, 0.05087761537052123, 1
- ]
- }
- ]
- },
- {
- "name": "alpha16.tif",
- "size": [7, 5],
- "tolerance": 0.0007,
- "points": [
- {
- "x": 0,
- "y": 0,
- "rgba": [
- 0.12503194615163135, 0.20627166527961105, 0.48764000398480484, 0.5
- ]
- },
- {
- "x": 3,
- "y": 2,
- "rgba": [
- 0.12503194615163135, 0.20627166527961105, 0.48764000398480484, 0.5
- ]
- },
- {
- "x": 6,
- "y": 4,
- "rgba": [
- 0.12503194615163135, 0.20627166527961105, 0.4876400039848048, 0.5
- ]
- }
- ]
- },
- {
- "name": "orientation-3.tif",
- "size": [19, 17],
- "tolerance": 0.0007,
- "points": [
- {
- "x": 0,
- "y": 0,
- "rgba": [0.6706360408760472, 0.6985694759636359, 0.7478227732168989, 1]
- },
- {
- "x": 9,
- "y": 8,
- "rgba": [
- 0.14718370513830395, 0.1526964597748831, 0.16292107561885874, 1
- ]
- },
- {
- "x": 18,
- "y": 16,
- "rgba": [
- 0.00011206924619723206, 5.980719895142648e-5, 7.792676543458332e-5, 1
- ]
- }
- ]
- },
- {
- "name": "orientation-6.tif",
- "size": [17, 19],
- "tolerance": 0.0007,
- "points": [
- {
- "x": 0,
- "y": 0,
- "rgba": [0.23738344300290853, 0.6468351580623225, 0.1910876175482113, 1]
- },
- {
- "x": 8,
- "y": 9,
- "rgba": [
- 0.14718370513830395, 0.1526964597748831, 0.16292107561885874, 1
- ]
- },
- {
- "x": 16,
- "y": 18,
- "rgba": [0.4149876641251806, 0.04697341288869796, 0.1768266581146218, 1]
- }
- ]
- },
- {
- "name": "float32-be.tif",
- "size": [7, 3],
- "tolerance": 0.004,
- "points": [
- { "x": 0, "y": 1, "rgba": [-0.125, -0.125, -0.125, 1] },
- { "x": 1, "y": 1, "rgba": [0.0, 0.0, 0.0, 1] },
- {
- "x": 2,
- "y": 1,
- "rgba": [
- 0.18000000715255737, 0.18000000715255737, 0.18000000715255737, 1
- ]
- },
- { "x": 3, "y": 1, "rgba": [0.5, 0.5, 0.5, 1] },
- { "x": 4, "y": 1, "rgba": [1.0, 1.0, 1.0, 1] },
- { "x": 5, "y": 1, "rgba": [2.0, 2.0, 2.0, 1] },
- { "x": 6, "y": 1, "rgba": [4.0, 4.0, 4.0, 1] }
- ]
- },
- {
- "name": "half-predictor.tif",
- "size": [7, 3],
- "tolerance": 0.004,
- "points": [
- { "x": 0, "y": 1, "rgba": [-0.125, -0.125, -0.125, 1] },
- { "x": 1, "y": 1, "rgba": [0.0, 0.0, 0.0, 1] },
- {
- "x": 2,
- "y": 1,
- "rgba": [
- 0.18000000715255737, 0.18000000715255737, 0.18000000715255737, 1
- ]
- },
- { "x": 3, "y": 1, "rgba": [0.5, 0.5, 0.5, 1] },
- { "x": 4, "y": 1, "rgba": [1.0, 1.0, 1.0, 1] },
- { "x": 5, "y": 1, "rgba": [2.0, 2.0, 2.0, 1] },
- { "x": 6, "y": 1, "rgba": [4.0, 4.0, 4.0, 1] }
- ]
- },
- {
- "name": "float64.tif",
- "size": [7, 3],
- "tolerance": 0.004,
- "points": [
- { "x": 0, "y": 1, "rgba": [-0.125, -0.125, -0.125, 1] },
- { "x": 1, "y": 1, "rgba": [0.0, 0.0, 0.0, 1] },
- {
- "x": 2,
- "y": 1,
- "rgba": [
- 0.18000000715255737, 0.18000000715255737, 0.18000000715255737, 1
- ]
- },
- { "x": 3, "y": 1, "rgba": [0.5, 0.5, 0.5, 1] },
- { "x": 4, "y": 1, "rgba": [1.0, 1.0, 1.0, 1] },
- { "x": 5, "y": 1, "rgba": [2.0, 2.0, 2.0, 1] },
- { "x": 6, "y": 1, "rgba": [4.0, 4.0, 4.0, 1] }
- ]
- },
- {
- "name": "precision16.tif",
- "size": [2, 1],
- "tolerance": 0.0007,
- "points": [
- {
- "x": 0,
- "y": 0,
- "rgba": [0.2140482022981852, 0.2140482022981852, 0.2140482022981852, 1]
- },
- {
- "x": 1,
- "y": 0,
- "rgba": [
- 0.21450044124130488, 0.21450044124130488, 0.21450044124130488, 1
- ]
- }
- ]
- },
- {
- "name": "lzw-tiles.tif",
- "size": [40, 17],
- "tolerance": 0.0007,
- "points": [
- { "x": 0, "y": 0, "rgba": [0.0, 0.0, 0.0, 1] },
- {
- "x": 20,
- "y": 8,
- "rgba": [0.2788942634768104, 0.2788942634768104, 0.2788942634768104, 1]
- },
- {
- "x": 39,
- "y": 16,
- "rgba": [
- 0.010329823029626936, 0.010329823029626936, 0.010329823029626936, 1
- ]
- },
- {
- "x": 17,
- "y": 16,
- "rgba": [0.3049873140698863, 0.3049873140698863, 0.3049873140698863, 1]
- }
- ]
- },
- {
- "name": "packed12.tif",
- "size": [9, 5],
- "tolerance": 0.0007,
- "points": [
- { "x": 0, "y": 0, "rgba": [0.0, 0.0, 0.0, 1] },
- {
- "x": 4,
- "y": 2,
- "rgba": [0.5768689620400628, 0.5768689620400628, 0.5768689620400628, 1]
- },
- {
- "x": 8,
- "y": 4,
- "rgba": [
- 0.28194495481752957, 0.28194495481752957, 0.28194495481752957, 1
- ]
- },
- {
- "x": 7,
- "y": 1,
- "rgba": [0.7420498725168261, 0.7420498725168261, 0.7420498725168261, 1]
- }
- ]
- }
-]
diff --git a/src/lib/tiff-gpu/fixtures/rgb16-bigtiff.tif b/src/lib/tiff-gpu/fixtures/rgb16-bigtiff.tif
deleted file mode 100644
index 3853682..0000000
Binary files a/src/lib/tiff-gpu/fixtures/rgb16-bigtiff.tif and /dev/null differ
diff --git a/src/lib/tiff-gpu/fixtures/rgb16-le.tif b/src/lib/tiff-gpu/fixtures/rgb16-le.tif
deleted file mode 100644
index 530594b..0000000
Binary files a/src/lib/tiff-gpu/fixtures/rgb16-le.tif and /dev/null differ
diff --git a/src/lib/tiff-gpu/fixtures/rgb16-lzw-be.tif b/src/lib/tiff-gpu/fixtures/rgb16-lzw-be.tif
deleted file mode 100644
index d22864e..0000000
Binary files a/src/lib/tiff-gpu/fixtures/rgb16-lzw-be.tif and /dev/null differ
diff --git a/src/lib/tiff-gpu/fixtures/rgb16-planar-tiled.tif b/src/lib/tiff-gpu/fixtures/rgb16-planar-tiled.tif
deleted file mode 100644
index 68bd0e5..0000000
Binary files a/src/lib/tiff-gpu/fixtures/rgb16-planar-tiled.tif and /dev/null differ
diff --git a/src/lib/tiff-gpu/fixtures/rgb16-prophoto.tif b/src/lib/tiff-gpu/fixtures/rgb16-prophoto.tif
deleted file mode 100644
index ba3960f..0000000
Binary files a/src/lib/tiff-gpu/fixtures/rgb16-prophoto.tif and /dev/null differ
diff --git a/src/lib/tiff-gpu/fixtures/rgb8-packbits.tif b/src/lib/tiff-gpu/fixtures/rgb8-packbits.tif
deleted file mode 100644
index 39f1a4c..0000000
Binary files a/src/lib/tiff-gpu/fixtures/rgb8-packbits.tif and /dev/null differ
diff --git a/src/lib/tiff-gpu/fixtures/rgb8-srgb-table.tif b/src/lib/tiff-gpu/fixtures/rgb8-srgb-table.tif
deleted file mode 100644
index 300ac19..0000000
Binary files a/src/lib/tiff-gpu/fixtures/rgb8-srgb-table.tif and /dev/null differ
diff --git a/src/lib/tiff-gpu/ifd.ts b/src/lib/tiff-gpu/ifd.ts
deleted file mode 100644
index 0487497..0000000
--- a/src/lib/tiff-gpu/ifd.ts
+++ /dev/null
@@ -1,243 +0,0 @@
-/** TIFF and BigTIFF directories: every tag of every image, and the layout the decoder needs. */
-
-export type Tag = { type: number; count: number; at: number };
-export type Directory = { tags: Map<number, Tag>; subdirectories: Directory[] };
-
-export type Tiff = {
- littleEndian: boolean;
- /** Image directories in file order, each with its SubIFDs; DNG keeps the raw image in a SubIFD of the first. */
- directories: Directory[];
- /** A tag's values by type: numbers, rationals divided, or text for ASCII. */
- value(directory: Directory, tag: number): number[] | string | undefined;
- /** A BYTE or UNDEFINED tag's bytes, copied. */
- bytes(directory: Directory, tag: number): Uint8Array | undefined;
-};
-
-export type Chunk = {
- offset: number;
- length: number;
- x: number;
- y: number;
- width: number;
- height: number;
-};
-
-export type TiffInfo = {
- width: number;
- height: number;
- samplesPerPixel: number;
- bitsPerSample: number;
- sampleFormat: number;
- photometric: number;
- compression: number;
- predictor: number;
- planar: number;
- orientation: number;
- littleEndian: boolean;
- premultiplied: boolean;
- colorMap?: Uint16Array;
- icc?: Uint8Array;
- /** Strips or tiles in plane, row, column order; `across` chunks per row, `chunkHeight` rows each. */
- chunks: Chunk[];
- across: number;
- chunkHeight: number;
-};
-
-const sizes: Record<number, number> = {
- 1: 1,
- 2: 1,
- 3: 2,
- 4: 4,
- 5: 8,
- 6: 1,
- 7: 1,
- 8: 2,
- 9: 4,
- 10: 8,
- 11: 4,
- 12: 8,
- 16: 8,
- 17: 8,
- 18: 8,
-};
-
-export function readTiff(buffer: ArrayBuffer): Tiff {
- const view = new DataView(buffer);
- const littleEndian = view.getUint16(0, true) === 0x4949;
- const magic = buffer.byteLength < 8 ? 0 : view.getUint16(2, littleEndian);
- const big = magic === 43;
- if (magic !== 42 && !big) {
- throw new Error("Not a TIFF file.");
- }
- const offsetSize = big ? 8 : 4;
- const unsigned = (at: number, size: number) =>
- size === 8
- ? Number(view.getBigUint64(at, littleEndian))
- : size === 4
- ? view.getUint32(at, littleEndian)
- : size === 2
- ? view.getUint16(at, littleEndian)
- : view.getUint8(at);
- const signed = (at: number, size: number) =>
- size === 8
- ? Number(view.getBigInt64(at, littleEndian))
- : size === 4
- ? view.getInt32(at, littleEndian)
- : size === 2
- ? view.getInt16(at, littleEndian)
- : view.getInt8(at);
- function number({ type, at }: Tag, i: number) {
- const size = sizes[type];
- const p = at + i * size;
- switch (type) {
- case 5:
- return unsigned(p, 4) / unsigned(p + 4, 4);
- case 10:
- return signed(p, 4) / signed(p + 4, 4);
- case 11:
- return view.getFloat32(p, littleEndian);
- case 12:
- return view.getFloat64(p, littleEndian);
- case 6:
- case 8:
- case 9:
- case 17:
- return signed(p, size);
- default:
- return unsigned(p, size);
- }
- }
- const numbers = (tag: Tag) =>
- Array.from({ length: tag.count }, (_, i) => number(tag, i));
- function directory(at: number, depth: number): Directory {
- const count = unsigned(at, big ? 8 : 2);
- const tags = new Map<number, Tag>();
- for (let i = 0; i < count; i++) {
- const entry = at + (big ? 8 : 2) + i * (big ? 20 : 12);
- const type = unsigned(entry + 2, 2);
- const length = unsigned(entry + 4, offsetSize);
- const inline = (sizes[type] ?? 1) * length <= offsetSize;
- tags.set(unsigned(entry, 2), {
- type,
- count: length,
- at: inline
- ? entry + 4 + offsetSize
- : unsigned(entry + 4 + offsetSize, offsetSize),
- });
- }
- const subdirectories = tags.get(330);
- return {
- tags,
- subdirectories:
- subdirectories && depth < 4
- ? numbers(subdirectories).map((offset) =>
- directory(offset, depth + 1),
- )
- : [],
- };
- }
- const directories: Directory[] = [];
- for (
- let at = unsigned(big ? 8 : 4, offsetSize);
- at > 0 && at < buffer.byteLength && directories.length < 64;
- ) {
- const count = unsigned(at, big ? 8 : 2);
- directories.push(directory(at, 0));
- at = unsigned(at + (big ? 8 : 2) + count * (big ? 20 : 12), offsetSize);
- }
- return {
- littleEndian,
- directories,
- value(directory, tag) {
- const found = directory.tags.get(tag);
- if (!found) {
- return;
- }
- return found.type === 2
- ? new TextDecoder().decode(
- new Uint8Array(buffer, found.at, Math.max(0, found.count - 1)),
- )
- : numbers(found);
- },
- bytes(directory, tag) {
- const found = directory.tags.get(tag);
- return (
- found &&
- new Uint8Array(
- buffer.slice(
- found.at,
- found.at + found.count * (sizes[found.type] ?? 1),
- ),
- )
- );
- },
- };
-}
-
-/** Layout of one image, the first by default. */
-export function parseTiff(
- buffer: ArrayBuffer,
- directory?: Directory,
-): TiffInfo {
- const tiff = readTiff(buffer);
- const image = directory ?? tiff.directories[0];
- const list = (tag: number) => {
- const value = tiff.value(image, tag);
- return typeof value === "string" ? [] : (value ?? []);
- };
- const one = (tag: number, fallback: number) => list(tag)[0] ?? fallback;
- const width = one(256, 0);
- const height = one(257, 0);
- const tiled = image.tags.has(324);
- const offsets = list(tiled ? 324 : 273);
- const counts = list(tiled ? 325 : 279);
- const chunkWidth = tiled ? one(322, 0) : width;
- const chunkHeight = tiled ? one(323, 0) : Math.min(one(278, height), height);
- if (
- width < 1 ||
- height < 1 ||
- chunkWidth < 1 ||
- chunkHeight < 1 ||
- !offsets.length
- ) {
- throw new Error("TIFF has no image data.");
- }
- const across = Math.ceil(width / chunkWidth);
- const chunksPerPlane = across * Math.ceil(height / chunkHeight);
- const chunks = offsets.map((offset, i) => {
- const length = counts[i] ?? 0;
- if (offset + length > buffer.byteLength) {
- throw new Error("Truncated TIFF file.");
- }
- const j = i % chunksPerPlane;
- const y = Math.floor(j / across) * chunkHeight;
- return {
- offset,
- length,
- x: (j % across) * chunkWidth,
- y,
- width: chunkWidth,
- height: tiled ? chunkHeight : Math.min(chunkHeight, height - y),
- };
- });
- const colorMap = list(320);
- return {
- width,
- height,
- samplesPerPixel: one(277, 1),
- bitsPerSample: one(258, 1),
- sampleFormat: one(339, 1),
- photometric: one(262, 1),
- compression: one(259, 1),
- predictor: one(317, 1),
- planar: one(284, 1),
- orientation: one(274, 1),
- littleEndian: tiff.littleEndian,
- premultiplied: one(338, 0) === 1,
- colorMap: colorMap.length ? Uint16Array.from(colorMap) : undefined,
- icc: tiff.bytes(image, 34675),
- chunks,
- across,
- chunkHeight,
- };
-}
diff --git a/src/lib/tiff-gpu/index.ts b/src/lib/tiff-gpu/index.ts
deleted file mode 100644
index 29ae0c6..0000000
--- a/src/lib/tiff-gpu/index.ts
+++ /dev/null
@@ -1,25 +0,0 @@
-import type { Gpu, Target } from "vgpu";
-import { type PrepareOptions, prepareTiff } from "./prepare";
-import { type UploadOptions, uploadTiff } from "./upload";
-
-export type { ColorSpace } from "./color";
-export {
- type Directory,
- parseTiff,
- readTiff,
- type Tiff,
- type TiffInfo,
-} from "./ifd";
-export { type Prepared, type PrepareOptions, prepareTiff } from "./prepare";
-export { type UploadOptions, uploadTiff } from "./upload";
-
-export type DecodeOptions = PrepareOptions & UploadOptions;
-
-/** File bytes to a linear rgba16float target, oriented, with straight alpha. `prepareTiff` alone can run in a worker. */
-export async function decodeTiff(
- gpu: Gpu,
- bytes: ArrayBuffer,
- options: DecodeOptions = {},
-): Promise<Target> {
- return uploadTiff(gpu, await prepareTiff(bytes, options), options);
-}
diff --git a/src/lib/tiff-gpu/prepare.ts b/src/lib/tiff-gpu/prepare.ts
deleted file mode 100644
index e9e28a5..0000000
--- a/src/lib/tiff-gpu/prepare.ts
+++ /dev/null
@@ -1,151 +0,0 @@
-import { codecs, undoFloatPrediction } from "./codecs";
-import { type ColorSpace, colorOf } from "./color";
-import { type Chunk, parseTiff, type TiffInfo } from "./ifd";
-
-export type PrepareOptions = {
- /** Linear RGB primaries of the output; Rec.2020 by default, which holds every photo gamut. `none` keeps sample values as they are. */
- colorSpace?: ColorSpace | "none";
- /** Layout to decode instead of the file's first image, for example a DNG SubIFD. */
- image?: TiffInfo;
-};
-
-/** CPU output: raw rows of every chunk in `data`, with the color conversion the GPU applies. */
-export type Prepared = {
- info: TiffInfo;
- data: Uint8Array<ArrayBuffer>;
- /** Per chunk: offset and length in `data`, then x, y, width, height. */
- chunks: Uint32Array<ArrayBuffer>;
- /** Transfer curves sampled over 0..1, three channels, and the matrix into the output primaries. */
- curves: Float32Array<ArrayBuffer>;
- matrix: number[];
-};
-
-/** Photometric interpretations the unpack pass understands: gray, RGB, palette, and camera raw mosaics. */
-const photometrics = [0, 1, 2, 3, 32803, 34892];
-
-export function rowBytes(info: TiffInfo, width: number) {
- const stride = info.planar === 2 ? 1 : info.samplesPerPixel;
- return Math.ceil((width * stride * info.bitsPerSample) / 8);
-}
-
-/** Samples that are already linear light: floats and camera raw data. */
-export const isLinear = (info: TiffInfo) =>
- info.sampleFormat === 3 || info.photometric >= 32803;
-
-function check(info: TiffInfo) {
- const { photometric, sampleFormat, compression, bitsPerSample } = info;
- if (
- !photometrics.includes(photometric) ||
- sampleFormat === 2 ||
- sampleFormat > 3
- ) {
- throw new Error(
- `Unsupported TIFF color: photometric ${photometric}, sample format ${sampleFormat}.`,
- );
- }
- if (!(compression in codecs)) {
- throw new Error(`Unsupported TIFF compression: ${compression}.`);
- }
- if (
- bitsPerSample < 1 ||
- bitsPerSample > 64 ||
- (bitsPerSample > 32 && bitsPerSample < 64) ||
- (sampleFormat === 3 && ![16, 32, 64].includes(bitsPerSample))
- ) {
- throw new Error(`Unsupported TIFF depth: ${bitsPerSample} bits.`);
- }
-}
-
-async function forEachLimited<T>(
- items: T[],
- limit: number,
- task: (item: T, i: number) => Promise<void>,
-) {
- let next = 0;
- const lanes = Array.from(
- { length: Math.min(limit, items.length) },
- async () => {
- while (next < items.length) {
- const i = next++;
- await task(items[i], i);
- }
- },
- );
- await Promise.all(lanes);
-}
-
-export function offsetsOf(sizes: number[]) {
- let total = 0;
- return sizes.map((size) => {
- total += size;
- return total - size;
- });
-}
-
-function table(chunks: Chunk[], offsets: number[], lengths: number[]) {
- return Uint32Array.from(
- chunks.flatMap((c, i) => [
- offsets[i],
- lengths[i],
- c.x,
- c.y,
- c.width,
- c.height,
- ]),
- );
-}
-
-/** Reads the layout, resolves the color, and decompresses every chunk; uncompressed files are used in place. */
-export async function prepareTiff(
- bytes: ArrayBuffer,
- options: PrepareOptions = {},
-): Promise<Prepared> {
- const info = options.image ?? parseTiff(bytes);
- check(info);
- const { chunks, compression, predictor } = info;
- const { table: curves, matrix } = colorOf(
- info.icc,
- isLinear(info),
- options.colorSpace ?? "rec2020",
- );
- const file = new Uint8Array(bytes);
- if (compression === 1 && predictor !== 3) {
- return {
- info,
- data: file,
- chunks: table(
- chunks,
- chunks.map((c) => c.offset),
- chunks.map((c) => c.length),
- ),
- curves,
- matrix,
- };
- }
- const decode = codecs[compression];
- const sizes = chunks.map((c) => rowBytes(info, c.width) * c.height);
- const offsets = offsetsOf(sizes);
- const data = new Uint8Array(sizes.reduce((total, size) => total + size, 3));
- await forEachLimited(chunks, 64, async (chunk, i) => {
- const input = file.subarray(chunk.offset, chunk.offset + chunk.length);
- const output = data.subarray(offsets[i], offsets[i] + sizes[i]);
- await decode(input, output);
- if (predictor === 3) {
- const stride = info.planar === 2 ? 1 : info.samplesPerPixel;
- undoFloatPrediction(
- output,
- rowBytes(info, chunk.width),
- stride,
- info.bitsPerSample / 8,
- info.littleEndian,
- );
- }
- });
- return {
- info: { ...info, predictor: predictor === 3 ? 1 : predictor },
- data,
- chunks: table(chunks, offsets, sizes),
- curves,
- matrix,
- };
-}
diff --git a/src/lib/tiff-gpu/testing.ts b/src/lib/tiff-gpu/testing.ts
deleted file mode 100644
index 8ae2c98..0000000
--- a/src/lib/tiff-gpu/testing.ts
+++ /dev/null
@@ -1,82 +0,0 @@
-import type { Gpu } from "vgpu";
-import {
- type DecodeOptions,
- decodeTiff,
- prepareTiff,
- uploadTiff,
-} from "./index";
-
-export function mismatches(a: Float32Array, b: Float32Array) {
- let count = a.length === b.length ? 0 : Number.POSITIVE_INFINITY;
- for (let i = 0; i < a.length && count < 1e6; i++) {
- if (a[i] !== b[i]) count++;
- }
- return count;
-}
-
-const median = (values: number[]) =>
- [...values].sort((a, b) => a - b)[Math.floor(values.length / 2)];
-
-/** Times `prepareTiff` and `uploadTiff` per variant; the first variant is the reference the others must match. */
-export async function benchmark(
- gpu: Gpu,
- buffer: ArrayBuffer,
- variants: Record<string, DecodeOptions>,
- runs = 3,
-) {
- const row: Record<string, unknown> = {
- megabytes: Math.round(buffer.byteLength / 1e5) / 10,
- };
- let reference: Float32Array | undefined;
- for (const [name, options] of Object.entries(variants)) {
- const prepare: number[] = [];
- const upload: number[] = [];
- try {
- for (let i = 0; i <= runs; i++) {
- const start = performance.now();
- const prepared = await prepareTiff(buffer, options);
- const middle = performance.now();
- const image = uploadTiff(gpu, prepared, options);
- await gpu.gpu.queue.onSubmittedWorkDone();
- if (i > 0) {
- prepare.push(middle - start);
- upload.push(performance.now() - middle);
- }
- if (i === runs) {
- const output = await image.readFloats();
- row[`${name} mismatches`] = reference
- ? mismatches(reference, output)
- : 0;
- reference ??= output;
- }
- image.color.dispose();
- }
- row[name] = {
- prepare: Math.round(median(prepare)),
- upload: Math.round(median(upload)),
- total: Math.round(median(prepare) + median(upload)),
- };
- } catch (error) {
- row[name] = { error: String(error) };
- }
- }
- return row;
-}
-
-/** Output size and linear rgba values at the given pixels. */
-export async function decodeAt(
- gpu: Gpu,
- bytes: ArrayBuffer,
- points: { x: number; y: number }[],
- options: DecodeOptions = {},
-) {
- const image = await decodeTiff(gpu, bytes, options);
- const pixels = await image.readFloats();
- const [width] = image.size;
- const values = points.map(({ x, y }) => [
- ...pixels.subarray((y * width + x) * 4, (y * width + x + 1) * 4),
- ]);
- const size = [...image.size];
- image.color.dispose();
- return { size, values };
-}
diff --git a/src/lib/tiff-gpu/tiff-gpu.test.ts b/src/lib/tiff-gpu/tiff-gpu.test.ts
deleted file mode 100644
index 4e1f7c7..0000000
--- a/src/lib/tiff-gpu/tiff-gpu.test.ts
+++ /dev/null
@@ -1,246 +0,0 @@
-import { afterAll, expect, test } from "bun:test";
-import { Readable, Writable } from "node:stream";
-import { createDeflate, createInflate } from "node:zlib";
-import { init } from "vgpu/node";
-import { decodeLzw, decodePackBits, inflate } from "./codecs";
-import { readProfile } from "./color";
-import { parseTiff, prepareTiff, readTiff } from "./index";
-import { benchmark, decodeAt } from "./testing";
-
-const fixture = (name: string) =>
- Bun.file(`${import.meta.dir}/fixtures/${name}`).arrayBuffer();
-
-// Bun lacks the compression stream globals the CPU codecs use; zlib streams stand in.
-const webStream = (create: () => import("node:stream").Duplex) =>
- class {
- readable = Readable.toWeb(create()) as unknown as ReadableStream;
- writable: WritableStream;
- constructor() {
- const stream = create();
- this.readable = Readable.toWeb(stream) as unknown as ReadableStream;
- this.writable = Writable.toWeb(stream);
- }
- };
-Object.assign(globalThis, {
- CompressionStream: webStream(createDeflate),
- DecompressionStream: webStream(createInflate),
-});
-const strip = (
- buffer: ArrayBuffer,
- chunk: { offset: number; length: number },
-) => new Uint8Array(buffer, chunk.offset, chunk.length);
-
-test("directories describe strips, tiles, planes, BigTIFF, palettes, and profiles", async () => {
- const plain = parseTiff(await fixture("rgb16-le.tif"));
- const tiff = readTiff(await fixture("rgb16-prophoto.tif"));
- expect(tiff.directories).toHaveLength(1);
- expect(tiff.value(tiff.directories[0], 256)).toEqual([19]);
- expect(tiff.value(tiff.directories[0], 305)).toMatch(/^tifffile/);
- expect(tiff.bytes(tiff.directories[0], 34675)?.length).toBeGreaterThan(128);
- expect(plain).toMatchObject({
- width: 19,
- height: 17,
- samplesPerPixel: 3,
- bitsPerSample: 16,
- compression: 1,
- planar: 1,
- littleEndian: true,
- across: 1,
- chunkHeight: 5,
- });
- expect(plain.chunks.map((chunk) => chunk.height)).toEqual([5, 5, 5, 2]);
- const tiled = parseTiff(await fixture("rgb16-planar-tiled.tif"));
- expect(tiled).toMatchObject({
- planar: 2,
- compression: 32946,
- predictor: 2,
- across: 2,
- chunkHeight: 16,
- });
- expect(tiled.chunks).toHaveLength(12);
- expect(parseTiff(await fixture("rgb16-bigtiff.tif"))).toMatchObject({
- width: 19,
- height: 17,
- });
- expect(parseTiff(await fixture("rgb16-lzw-be.tif")).littleEndian).toBe(false);
- expect(
- parseTiff(await fixture("rgb16-prophoto.tif")).icc?.length,
- ).toBeGreaterThan(128);
- expect(parseTiff(await fixture("palette8.tif")).colorMap).toHaveLength(768);
- expect(parseTiff(await fixture("bilevel.tif"))).toMatchObject({
- bitsPerSample: 1,
- photometric: 0,
- });
- expect(parseTiff(await fixture("alpha16.tif"))).toMatchObject({
- samplesPerPixel: 4,
- premultiplied: true,
- });
- expect(() =>
- parseTiff(new TextEncoder().encode("not a tiff").buffer),
- ).toThrow("Not a TIFF");
- const truncated = new Uint8Array(await fixture("rgb16-le.tif")).slice(
- 0,
- plain.chunks[3].offset + 10,
- );
- expect(() => parseTiff(truncated.buffer)).toThrow();
-});
-
-test("CPU codecs reproduce uncompressed strips", async () => {
- const plainBuffer = await fixture("rgb16-le.tif");
- const lzwBuffer = await fixture("rgb16-lzw-be.tif");
- const plain = parseTiff(plainBuffer);
- const output = new Uint8Array(plain.chunks[0].length);
- decodeLzw(strip(lzwBuffer, parseTiff(lzwBuffer).chunks[0]), output);
- expect(Uint8Array.from(output, (_, i) => output[i ^ 1])).toEqual(
- strip(plainBuffer, plain.chunks[0]),
- );
- const packedBuffer = await fixture("rgb8-packbits.tif");
- const rows = new Uint8Array(19 * 5 * 3);
- decodePackBits(strip(packedBuffer, parseTiff(packedBuffer).chunks[0]), rows);
- expect([...rows.subarray(0, 6)]).toEqual([0, 0, 0, 12, 0, 6]);
- const source = Uint8Array.from({ length: 200_000 }, (_, i) => (i * 7) % 251);
- const stream = new Blob([source])
- .stream()
- .pipeThrough(new CompressionStream("deflate"));
- const zipped = new Uint8Array(await new Response(stream).arrayBuffer());
- const restored = new Uint8Array(source.length);
- await inflate(zipped, restored);
- expect(restored).toEqual(source);
-});
-
-test("prepare decompresses on the CPU and carries the file's color", async () => {
- const file = await fixture("lzw-strips.tif");
- const strips = await prepareTiff(file);
- expect(strips.chunks).toHaveLength(130 * 6);
- expect(strips.curves).toHaveLength(3 * 1024);
- expect(strips.matrix.map((v) => Math.round(v * 100) / 100)).toEqual([
- 0.63, 0.07, 0.02, 0.33, 0.92, 0.09, 0.04, 0.01, 0.9,
- ]);
- expect([...new Uint16Array(strips.data.buffer, strips.chunks[0], 3)]).toEqual(
- [123, 45, 67],
- );
- const plain = await fixture("rgb16-le.tif");
- expect((await prepareTiff(plain)).data.byteLength).toBe(plain.byteLength);
- const planar = await prepareTiff(await fixture("rgb16-planar-tiled.tif"));
- expect([planar.chunks.length, planar.data.byteLength]).toEqual([
- 72,
- 12 * 16 * 16 * 2 + 3,
- ]);
- const half = await prepareTiff(await fixture("half-predictor.tif"));
- expect([half.info.predictor, ...half.data.subarray(0, 2)]).toEqual([
- 1, 0x00, 0xb0,
- ]);
- expect(half.curves[512]).toBeCloseTo(512 / 1023, 6);
- const srgb = await prepareTiff(await fixture("rgb16-le.tif"), {
- colorSpace: "srgb",
- });
- expect(srgb.matrix.map((v) => Math.round(v * 100) / 100 + 0)).toEqual([
- 1, 0, 0, 0, 1, 0, 0, 0, 1,
- ]);
- await expect(prepareTiff(await fixture("rgb8-jpeg.tif"))).rejects.toThrow(
- "Unsupported TIFF",
- );
-});
-
-test("profiles yield colorants and curves; anything else falls back to sRGB", async () => {
- const prophoto = readProfile(
- parseTiff(await fixture("rgb16-prophoto.tif")).icc ?? new Uint8Array(),
- );
- expect(
- [...(prophoto?.colorants ?? [])].map((v) => Math.round(v * 1e4) / 1e4),
- ).toEqual([0.7977, 0.288, 0, 0.1352, 0.7119, 0, 0.0313, 0.0001, 0.8249]);
- expect(prophoto?.curves[1](0.5)).toBeCloseTo(0.5 ** (461 / 256), 6);
- const table = readProfile(
- parseTiff(await fixture("rgb8-srgb-table.tif")).icc ?? new Uint8Array(),
- );
- expect(table?.curves[2](128 / 255)).toBeCloseTo(0.2158605, 4);
- const gray = readProfile(
- parseTiff(await fixture("gray16-para.tif")).icc ?? new Uint8Array(),
- );
- expect(gray?.curves[0](0.5)).toBeCloseTo(0.2140411, 5);
- expect(readProfile(new Uint8Array(200))).toBeUndefined();
- expect(
- readProfile(new Uint8Array(await fixture("rgb16-le.tif"))),
- ).toBeUndefined();
- const untagged = await prepareTiff(await fixture("gray16-para.tif"));
- expect(
- untagged.matrix.slice(0, 3).map((v) => Math.round(v * 100) / 100),
- ).toEqual([1, 1, 1]);
-});
-
-// Shader tests run for real through vgpu's Node entry; `bun run test:gpu` opts in, so CI stays fast.
-const gpu = process.env.GPU
- ? await init().catch((error) => {
- console.warn(`Skipping GPU tests: ${error}`);
- return undefined;
- })
- : undefined;
-afterAll(() => gpu?.dispose());
-
-type Reference = {
- name: string;
- size: number[];
- tolerance: number;
- points: { x: number; y: number; rgba: number[] }[];
-};
-const banded = [
- "rgb16-le.tif",
- "lzw-strips.tif",
- "lzw-tiles.tif",
- "rgb16-planar-tiled.tif",
- "palette8.tif",
- "half-predictor.tif",
- "orientation-6.tif",
-];
-
-test.skipIf(!gpu)(
- "every fixture decodes to its linear Rec.2020 reference, in row bands alike",
- async () => {
- if (!gpu) return;
- const references: Reference[] = JSON.parse(
- await Bun.file(`${import.meta.dir}/fixtures/reference.json`).text(),
- );
- const decoded = new Map<string, number[][]>();
- for (const reference of references) {
- const result = await decodeAt(
- gpu,
- await fixture(reference.name),
- reference.points,
- );
- expect(result.size, reference.name).toEqual(reference.size);
- reference.points.forEach((point, i) => {
- point.rgba.forEach((value, channel) => {
- expect(
- Math.abs(result.values[i][channel] - value),
- `${reference.name} point ${i} channel ${channel}`,
- ).toBeLessThan(reference.tolerance);
- });
- });
- decoded.set(reference.name, result.values);
- }
- const [first, second] = decoded.get("precision16.tif") ?? [];
- expect(second[0] - first[0]).toBeGreaterThan(0.0002);
- // Raw values in a float32 target: exact 16-bit samples over 65535, no curve or matrix.
- const raw = await decodeAt(
- gpu,
- await fixture("rgb16-le.tif"),
- [{ x: 18, y: 16 }],
- { colorSpace: "none", format: "rgba32float" },
- );
- [54123, 56045, 57867, 65535].forEach((sample, channel) => {
- expect(raw.values[0][channel]).toBeCloseTo(sample / 65535, 6);
- });
- for (const name of banded) {
- // An 8 KB limit splits the wider fixtures into many bands; a transposed row alone pads to 256 bytes per column.
- const row = await benchmark(
- gpu,
- await fixture(name),
- { reference: {}, banded: { limit: 8192 } },
- 1,
- );
- expect(row, `${name} ${JSON.stringify(row)}`).toMatchObject({
- "banded mismatches": 0,
- });
- }
- },
-);
diff --git a/src/lib/tiff-gpu/unpack.wgsl b/src/lib/tiff-gpu/unpack.wgsl
deleted file mode 100644
index e5afb3e..0000000
--- a/src/lib/tiff-gpu/unpack.wgsl
+++ /dev/null
@@ -1,145 +0,0 @@
-struct Params {
- width: u32,
- height: u32,
- samples: u32,
- colors: u32,
- bits: u32,
- littleEndian: u32,
- float: u32,
- whiteIsZero: u32,
- planar: u32,
- predictor: u32,
- palette: u32,
- wide: u32,
- premultiplied: u32,
- orientation: u32,
- chunksPerPlane: u32,
- maxRows: u32,
- rowWords: u32,
- origin: vec2u,
- matrix: mat3x3f,
-}
-struct Chunk { offset: u32, length: u32, x: u32, y: u32, width: u32, height: u32 }
-@group(0) @binding(0) var<uniform> params: Params;
-@group(0) @binding(1) var<storage, read> data: array<u32>;
-@group(0) @binding(2) var<storage, read> chunks: array<Chunk>;
-@group(0) @binding(3) var<storage, read> colorMap: array<u32>;
-@group(0) @binding(4) var<storage, read> curves: array<f32>;
-// Linear rgba pixels for this band's output rectangle, float16 pairs or float32, rows padded to `rowWords`.
-@group(0) @binding(5) var<storage, read_write> output: array<u32>;
-
-fn byteAt(i: u32) -> u32 { return (data[i >> 2u] >> ((i & 3u) * 8u)) & 0xFFu; }
-
-// Sample bits at a byte offset plus a bit offset. Depths that are not whole bytes (1, 2, 4, 12, 14 bits)
-// form a most-significant-bit-first stream, read through a 40-bit window so any depth up to 32 fits at
-// any offset; whole-byte samples follow the file's byte order, and the high word only matters for 64 bits.
-fn sampleAt(byte: u32, bit: u32) -> vec2u {
- if params.bits % 8u != 0u {
- var window = 0u;
- for (var k = 0u; k < 4u; k++) { window = (window << 8u) | byteAt(byte + k); }
- let aligned = (window << bit) | (byteAt(byte + 4u) >> (8u - bit));
- return vec2u(aligned >> (32u - params.bits), 0u);
- }
- let count = params.bits / 8u;
- var value = vec2u(0u);
- for (var k = 0u; k < count; k++) {
- let b = byteAt(byte + select(count - 1u - k, k, params.littleEndian == 1u));
- if k < 4u { value.x |= b << (8u * k); } else { value.y |= b << (8u * (k - 4u)); }
- }
- return value;
-}
-
-// Integers normalize to 0..1; floats keep their value.
-fn toFloat(sample: vec2u) -> f32 {
- if params.float == 1u {
- if params.bits == 16u { return unpack2x16float(sample.x).x; }
- if params.bits == 32u { return bitcast<f32>(sample.x); }
- // Double to single: sign, rebiased exponent, top 23 mantissa bits.
- let exponent = i32((sample.y >> 20u) & 0x7FFu) - 896;
- let mantissa = ((sample.y & 0xFFFFFu) << 3u) | (sample.x >> 29u);
- return bitcast<f32>((sample.y & 0x80000000u) | (u32(clamp(exponent, 0, 255)) << 23u) | mantissa);
- }
- if params.bits >= 32u { return f32(select(sample.x, sample.y, params.bits == 64u)) / 4294967295.0; }
- return f32(sample.x) / f32((1u << params.bits) - 1u);
-}
-
-// Transfer curve over 0..1; samples outside keep their distance, so HDR and negative values pass through.
-fn transfer(value: f32, channel: u32) -> f32 {
- let last = arrayLength(&curves) / 3u - 1u;
- let bounded = clamp(value, 0.0, 1.0);
- let position = bounded * f32(last);
- let low = min(u32(position), last - 1u);
- let base = channel * (last + 1u) + low;
- return mix(curves[base], curves[base + 1u], position - f32(low)) + (value - bounded);
-}
-
-// Where a stored pixel lands after the orientation tag.
-fn oriented(p: vec2u) -> vec2u {
- let w = params.width - 1u;
- let h = params.height - 1u;
- switch params.orientation {
- case 2u: { return vec2u(w - p.x, p.y); }
- case 3u: { return vec2u(w - p.x, h - p.y); }
- case 4u: { return vec2u(p.x, h - p.y); }
- case 5u: { return p.yx; }
- case 6u: { return vec2u(h - p.y, p.x); }
- case 7u: { return vec2u(h - p.y, w - p.x); }
- case 8u: { return vec2u(p.y, w - p.x); }
- default: { return p; }
- }
-}
-
-// One thread per chunk row: walks the row, undoing horizontal prediction, and writes linear pixels.
-@compute @workgroup_size(64) fn unpack(@builtin(global_invocation_id) id: vec3u) {
- let chunk = id.x / params.maxRows;
- let row = id.x % params.maxRows;
- if chunk >= params.chunksPerPlane { return; }
- let first = chunks[chunk];
- let y = first.y + row;
- if row >= first.height || y >= params.height { return; }
- let planar = params.planar == 2u;
- let stride = select(params.samples, 1u, planar);
- let rowBytes = (first.width * stride * params.bits + 7u) / 8u;
- let mask = select((1u << params.bits) - 1u, 0xFFFFFFFFu, params.bits >= 32u);
- let count = min(params.samples, 4u);
- var acc = vec4u(0u);
- for (var x = 0u; x < first.width; x++) {
- var texel = vec4f(0.0);
- for (var s = 0u; s < count; s++) {
- let c = chunks[chunk + select(0u, s, planar) * params.chunksPerPlane];
- let bit = (x * stride + select(s, 0u, planar)) * params.bits;
- var sample = sampleAt(c.offset + row * rowBytes + bit / 8u, bit & 7u);
- if params.predictor == 2u {
- acc[s] = (acc[s] + sample.x) & mask;
- sample.x = acc[s];
- }
- texel[s] = toFloat(sample);
- }
- let last = params.colors - 1u;
- var rgb = vec3f(texel[0], texel[min(1u, last)], texel[min(2u, last)]);
- if params.palette == 1u {
- let entries = 1u << params.bits;
- let index = u32(round(texel[0] * f32(entries - 1u)));
- rgb = vec3f(f32(colorMap[index]), f32(colorMap[entries + index]), f32(colorMap[2u * entries + index])) / 65535.0;
- }
- let alpha = select(1.0, texel[params.colors], params.samples > params.colors);
- if params.whiteIsZero == 1u { rgb = 1.0 - rgb; }
- if params.premultiplied == 1u { rgb /= max(alpha, 0.000001); }
- let linear = vec3f(transfer(rgb.r, 0u), transfer(rgb.g, 1u), transfer(rgb.b, 2u));
- let color = params.matrix * linear;
- if first.x + x < params.width {
- let p = oriented(vec2u(first.x + x, y)) - params.origin;
- if params.wide == 1u {
- let at = p.y * params.rowWords + p.x * 4u;
- output[at] = bitcast<u32>(color.r);
- output[at + 1u] = bitcast<u32>(color.g);
- output[at + 2u] = bitcast<u32>(color.b);
- output[at + 3u] = bitcast<u32>(alpha);
- } else {
- let at = p.y * params.rowWords + p.x * 2u;
- output[at] = pack2x16float(color.rg);
- output[at + 1u] = pack2x16float(vec2f(color.b, alpha));
- }
- }
- }
-}
diff --git a/src/lib/tiff-gpu/upload.ts b/src/lib/tiff-gpu/upload.ts
deleted file mode 100644
index 23cfef7..0000000
--- a/src/lib/tiff-gpu/upload.ts
+++ /dev/null
@@ -1,278 +0,0 @@
-import type { Buffer } from "@vgpu/core";
-import {
- type Compute,
- compute,
- type Effect,
- effect,
- frame,
- type Gpu,
- type Target,
- target,
-} from "vgpu";
-import blitShader from "./blit.wgsl";
-import type { TiffInfo } from "./ifd";
-import { type Prepared, rowBytes } from "./prepare";
-import unpackShader from "./unpack.wgsl";
-
-export type UploadOptions = {
- /** Largest buffer to bind, in bytes; defaults to the device limits. */
- limit?: number;
- /** Target format; `rgba32float` keeps raw sample values exact. */
- format?: "rgba16float" | "rgba32float";
-};
-
-type Entry = {
- offset: number;
- length: number;
- x: number;
- y: number;
- width: number;
- height: number;
-};
-type Band = { y: number; rows: number; entries: Entry[] };
-
-const align = (n: number) => (n + 3) & ~3;
-const align256 = (n: number) => (n + 255) & ~255;
-const pipelines = new WeakMap<Gpu, { unpack: Compute; blit: Effect }>();
-
-/**
- * writeBuffer takes multiples of 4: over-read up to 3 bytes of `data`, or pad at its end. Offsets are
- * passed explicitly because the Node WebGPU binding ignores a typed array view's byte offset.
- */
-function writeBytes(
- gpu: Gpu,
- target: Buffer,
- data: Uint8Array<ArrayBuffer>,
- start: number,
- end: number,
- at: number,
-) {
- const stop = Math.min(align(end), data.byteLength);
- const whole = stop - ((stop - start) & 3);
- gpu.gpu.queue.writeBuffer(
- target.gpu,
- at,
- data.buffer,
- data.byteOffset + start,
- whole - start,
- );
- if (whole < end) {
- const tail = new Uint8Array(4);
- tail.set(data.subarray(whole, end));
- target.write(tail, at + whole - start);
- }
-}
-
-type Layout = { transposed: boolean; flipped: boolean; bytesPerPixel: number };
-
-/** Rows of the padded pixel buffer a band of `rows` needs, in the orientation the band is written in. */
-function pixelBytes(info: TiffInfo, layout: Layout, rows: number) {
- const { width } = info;
- return layout.transposed
- ? align256(rows * layout.bytesPerPixel) * width
- : align256(width * layout.bytesPerPixel) * rows;
-}
-
-/**
- * Row bands whose input and pixel buffers both fit the limit, each listing the rows it takes from
- * every chunk in plane, chunk row, column order.
- */
-function bands(prepared: Prepared, layout: Layout, limit: number): Band[] {
- const { info, chunks } = prepared;
- const { width, height, across, chunkHeight } = info;
- const planes = info.planar === 2 ? info.samplesPerPixel : 1;
- const perPlane = chunks.length / 6 / planes;
- // Input per row, plus up to 4 bytes of alignment for each chunk slice a band can hold.
- const rowInput =
- planes * across * (rowBytes(info, chunks[4]) + 4 / chunkHeight);
- const byInput = Math.floor((limit - 8 * planes * across) / rowInput);
- const byPixels = layout.transposed
- ? Math.floor((Math.floor(limit / width) & ~255) / layout.bytesPerPixel)
- : Math.floor(limit / align256(width * layout.bytesPerPixel));
- const rowsPerBand = Math.min(byInput, byPixels);
- if (rowsPerBand < 1) {
- throw new Error("TIFF row exceeds the GPU buffer limit.");
- }
- const result: Band[] = [];
- for (let y = 0; y < height; y += rowsPerBand) {
- const end = Math.min(y + rowsPerBand, height);
- const entries: Entry[] = [];
- for (let p = 0; p < planes; p++) {
- for (let j = Math.floor(y / chunkHeight); j * chunkHeight < end; j++) {
- const top = j * chunkHeight;
- const from = Math.max(y, top) - top;
- const to = Math.min(end, top + chunkHeight) - top;
- for (let i = 0; i < across; i++) {
- const at = (p * perPlane + j * across + i) * 6;
- const [offset, , x, chunkY, chunkWidth] = chunks.subarray(at, at + 5);
- const bytes = rowBytes(info, chunkWidth);
- entries.push({
- offset: offset + from * bytes,
- length: (to - from) * bytes,
- x,
- y: chunkY + from,
- width: chunkWidth,
- height: to - from,
- });
- }
- }
- }
- result.push({ y, rows: end - y, entries });
- }
- return result;
-}
-
-/** Unpacks the prepared rows on the GPU, one band at a time, into a linear target of the oriented size. */
-export function uploadTiff(
- gpu: Gpu,
- prepared: Prepared,
- options: UploadOptions = {},
-): Target {
- const { info, data } = prepared;
- const device = gpu.device;
- const shaders = pipelines.get(gpu) ?? {
- unpack: compute(gpu, unpackShader),
- blit: effect(gpu, blitShader),
- };
- pipelines.set(gpu, shaders);
- const { width, height, orientation } = info;
- const limit = Math.min(
- options.limit ?? Number.POSITIVE_INFINITY,
- device.limits.maxStorageBufferBindingSize,
- device.limits.maxBufferSize,
- );
- const planes = info.planar === 2 ? info.samplesPerPixel : 1;
- const wide = options.format === "rgba32float";
- const layout: Layout = {
- transposed: orientation >= 5,
- flipped: [3, 4, 6, 7].includes(orientation),
- bytesPerPixel: wide ? 16 : 8,
- };
- const plan = bands(prepared, layout, limit);
- const params = {
- width,
- height,
- samples: info.samplesPerPixel,
- colors: info.photometric === 2 ? 3 : 1,
- bits: info.bitsPerSample,
- littleEndian: Number(info.littleEndian),
- float: Number(info.sampleFormat === 3),
- whiteIsZero: Number(info.photometric === 0),
- planar: info.planar,
- predictor: info.predictor,
- palette: Number(info.photometric === 3),
- wide: Number(wide),
- premultiplied: Number(info.premultiplied),
- orientation,
- matrix: prepared.matrix,
- };
- const image = target(gpu, {
- size: layout.transposed ? [height, width] : [width, height],
- format: wide ? "rgba32float" : "rgba16float",
- });
- const owned: Buffer[] = [];
- const buffer = (size: number, usage: ("storage" | "copy_dst")[]) => {
- const created = device.createBuffer({ size: align(size), usage });
- owned.push(created);
- return created;
- };
- try {
- const colorMap = buffer(Math.max(4, (info.colorMap?.length ?? 0) * 4), [
- "storage",
- "copy_dst",
- ]);
- if (info.colorMap) {
- colorMap.write(Uint32Array.from(info.colorMap));
- }
- const curves = buffer(prepared.curves.byteLength, ["storage", "copy_dst"]);
- curves.write(prepared.curves);
- plan.forEach((band, index) => {
- // Chunk bytes packed together: contiguous runs upload with one write, each starting 4-byte aligned.
- const runs: { start: number; end: number; at: number }[] = [];
- const packed = band.entries.map((entry) => {
- let run = runs.at(-1);
- if (!run || entry.offset !== run.end) {
- run = {
- start: entry.offset,
- end: entry.offset,
- at: align(run ? run.at + run.end - run.start : 0),
- };
- runs.push(run);
- }
- run.end += entry.length;
- return run.at + entry.offset - run.start;
- });
- const last = runs.at(-1);
- const input = buffer(last ? last.at + last.end - last.start : 0, [
- "storage",
- "copy_dst",
- ]);
- for (const run of runs) {
- writeBytes(gpu, input, data, run.start, run.end, run.at);
- }
- const table = buffer(band.entries.length * 24, ["storage", "copy_dst"]);
- table.write(
- Uint32Array.from(
- band.entries.flatMap((e, i) => [
- packed[i],
- e.length,
- e.x,
- e.y,
- e.width,
- e.height,
- ]),
- ),
- );
- // The band's stored rows land in a rectangle of the oriented output.
- const along = layout.flipped ? height - band.y - band.rows : band.y;
- const rect = layout.transposed
- ? [along, 0, band.rows, width]
- : [0, along, width, band.rows];
- const rowWords = align256(rect[2] * layout.bytesPerPixel) / 4;
- const pixels = buffer(pixelBytes(info, layout, band.rows), ["storage"]);
- const chunksPerPlane = band.entries.length / planes;
- const maxRows = Math.max(...band.entries.map((e) => e.height));
- shaders.unpack
- .set({
- params: {
- ...params,
- chunksPerPlane,
- maxRows,
- rowWords,
- origin: [rect[0], rect[1]],
- },
- data: input,
- chunks: table,
- colorMap,
- curves,
- output: pixels,
- })
- .dispatch(Math.ceil((chunksPerPlane * maxRows) / 64));
- frame(gpu, (f) => {
- f.pass(
- {
- target: image,
- clear: index === 0,
- scissor: [rect[0], rect[1], rect[2], rect[3]],
- },
- shaders.blit.set({
- rect: { origin: [rect[0], rect[1]], rowWords, wide: Number(wide) },
- pixels,
- }),
- );
- });
- for (const scratch of [input, table, pixels]) {
- scratch.dispose();
- owned.splice(owned.indexOf(scratch), 1);
- }
- });
- } catch (error) {
- image.color.dispose();
- throw error;
- } finally {
- for (const scratch of owned) {
- scratch.dispose();
- }
- }
- return image;
-}
diff --git a/src/lib/tiff-gpu/fixtures/alpha16.tif b/tests/fixtures/alpha16.tif
similarity index 100%
rename from src/lib/tiff-gpu/fixtures/alpha16.tif
rename to tests/fixtures/alpha16.tif
diff --git a/src/lib/tiff-gpu/fixtures/float32-be.tif b/tests/fixtures/float32-be.tif
similarity index 100%
rename from src/lib/tiff-gpu/fixtures/float32-be.tif
rename to tests/fixtures/float32-be.tif
diff --git a/src/lib/tiff-gpu/fixtures/gray16-para.tif b/tests/fixtures/gray16-para.tif
similarity index 100%
rename from src/lib/tiff-gpu/fixtures/gray16-para.tif
rename to tests/fixtures/gray16-para.tif
diff --git a/src/lib/tiff-gpu/fixtures/orientation-6.tif b/tests/fixtures/orientation-6.tif
similarity index 100%
rename from src/lib/tiff-gpu/fixtures/orientation-6.tif
rename to tests/fixtures/orientation-6.tif
diff --git a/src/lib/tiff-gpu/fixtures/rgb8-jpeg.tif b/tests/fixtures/rgb8-jpeg.tif
similarity index 100%
rename from src/lib/tiff-gpu/fixtures/rgb8-jpeg.tif
rename to tests/fixtures/rgb8-jpeg.tif
diff --git a/tests/tiff.e2e.ts b/tests/tiff.e2e.ts
index ad8e725..95e76c9 100644
--- a/tests/tiff.e2e.ts
+++ b/tests/tiff.e2e.ts
@@ -2,15 +2,15 @@ import { readFile } from "node:fs/promises";
import { expect, test } from "./fixtures";
import { readImage } from "./images";
-const directory = "src/lib/tiff-gpu/fixtures";
+const directory = "tests/fixtures";
-test("TIFF files open through the loader with their color, orientation, and headroom", async ({
+test("TIFF files open through the loader with their color, orientation, headroom, alpha, and error recovery", async ({
page,
}) => {
await page.goto("/");
await page.waitForFunction(() => window.openlight);
- const load = async (name: string, folder = directory) => {
- const bytes = [...(await readFile(`${folder}/${name}`))];
+ const load = async (name: string) => {
+ const bytes = [...(await readFile(`${directory}/${name}`))];
await page.evaluate(
({ bytes, name }) =>
window.openlight.loadImage(new File([new Uint8Array(bytes)], name)),
@@ -49,7 +49,7 @@ test("TIFF files open through the loader with their color, orientation, and head
expect(recovered[5]).toBeGreaterThan(recovered[4]);
expect(recovered[6]).toBeGreaterThan(240);
// The blue pixel brightens without losing its hue, then approaches white.
- await load("blue-float.tif", "tests/fixtures");
+ await load("blue-float.tif");
const colors = [];
for (const exposure of [0, 2, 5]) {
await page.evaluate(
@@ -72,4 +72,15 @@ test("TIFF files open through the loader with their color, orientation, and head
await expect(
page.getByText("Couldn't open rgb8-jpeg.tif:", { exact: false }),
).toBeVisible();
+ // Recover in the same session after the package rejects an unsupported TIFF.
+ await load("alpha16.tif");
+ const alpha = await readImage(page);
+ expect(alpha.size).toEqual([7, 5]);
+ // Export composites 50% alpha over the editor's 0.09 display background.
+ for (const [i, expected] of [43, 75, 107, 255].entries()) {
+ expect(Math.abs(alpha.center[i] - expected)).toBeLessThanOrEqual(1);
+ }
+ await expect(
+ page.getByText("Couldn't open rgb8-jpeg.tif:", { exact: false }),
+ ).toBeHidden();
});
diff --git a/tests/tiff.test.ts b/tests/tiff.test.ts
new file mode 100644
index 0000000..b030559
--- /dev/null
+++ b/tests/tiff.test.ts
@@ -0,0 +1,54 @@
+import { expect, mock, spyOn, test } from "bun:test";
+import { init, target } from "vgpu/mock";
+import { decodeTiff } from "@/lib/decode/tiff";
+
+const decode = mock();
+mock.module("raw-webgpu", () => ({ decodeTiff: decode }));
+
+test("TIFF transfer releases package pixels and owns or cleans up its destination", async () => {
+ const gpu = await init();
+ const input = target(gpu, { size: [7, 5], format: "rgba16float" });
+ const disposed: (typeof input.color)[] = [];
+ const disposeTexture = input.color.dispose;
+ const cleanup = spyOn(
+ Object.getPrototypeOf(input.color),
+ "dispose",
+ ).mockImplementation(function (this: typeof input.color) {
+ disposed.push(this);
+ disposeTexture.call(this);
+ });
+ const submit = spyOn(gpu.gpu.queue, "submit");
+ try {
+ for (const fail of [false, true]) {
+ disposed.length = 0;
+ const dispose = mock(() => {});
+ decode.mockResolvedValueOnce({
+ texture: input.color.gpu,
+ size: input.size,
+ dispose,
+ });
+ if (fail) {
+ submit.mockImplementationOnce(() => {
+ throw Error("Transfer failed");
+ });
+ }
+ const loading = decodeTiff(gpu, new File([], "photo.tif"));
+ if (fail) {
+ await expect(loading).rejects.toThrow("Transfer failed");
+ } else {
+ const source = await loading;
+ expect(disposed).toHaveLength(0);
+ expect(() => source.image.color.view).not.toThrow();
+ source.dispose();
+ }
+ expect(dispose).toHaveBeenCalledTimes(1);
+ expect(disposed).toHaveLength(1);
+ expect(() => disposed[0].view).toThrow("destroyed");
+ }
+ } finally {
+ cleanup.mockRestore();
+ submit.mockRestore();
+ input.color.dispose();
+ gpu.dispose();
+ }
+});