Skip to content

Free open-source image editor for the browser

Add TIFF import decoded on the GPU (#12)

Rodrigo Belfiored268477ceef3
diff --git a/AGENTS.md b/AGENTS.md
index 999482f..e41f475 100644
--- a/AGENTS.md
+++ b/AGENTS.md
@@ -56,14 +56,14 @@ 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.
-- Retain `workerDecoder` for the upcoming RAW decoding implementation.
+- `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).
## 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.
+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.
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:browser` runs GPU/browser tests. Run all four after changing files and before a commit.
+`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.
diff --git a/README.md b/README.md
index 79aa73d..6dc14c9 100644
--- a/README.md
+++ b/README.md
@@ -16,6 +16,7 @@ Image processing runs locally with WebGPU. Built with TypeScript and React.
- Highlight and shadow clipping overlays
- Pan and zoom
- Camera Raw XMP import
+- TIFF import at 16-bit and floating-point precision, decoded on the GPU
- PNG and JPEG export
Requires a WebGPU-capable browser.
@@ -27,7 +28,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. GPU and UI tests run with `bun run test:browser` and require 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. Shader tests run with `bun run test:gpu`, and GPU and UI tests with `bun run test:browser`, which requires Chromium with WebGPU.
## Scripting
diff --git a/bun.lock b/bun.lock
index 51493f3..daf764a 100644
--- a/bun.lock
+++ b/bun.lock
@@ -11,6 +11,7 @@
"@vercel/speed-insights": "^2.0.0",
"class-variance-authority": "^0.7.1",
"cn": "^0.2.4",
+ "gl-matrix": "^3.4.4",
"react": "^19.2.8",
"react-dom": "^19.2.8",
"tailwindcss": "^4.3.3",
@@ -221,6 +222,8 @@
"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=="],
diff --git a/bunfig.toml b/bunfig.toml
index 3efd738..ec67e25 100644
--- a/bunfig.toml
+++ b/bunfig.toml
@@ -1,3 +1,3 @@
[test]
-root = "./tests"
+root = "."
preload = ["./tests/setup.ts"]
diff --git a/package.json b/package.json
index 86b1916..1759b2c 100644
--- a/package.json
+++ b/package.json
@@ -7,6 +7,7 @@
"build": "tsc && vite build",
"check": "biome check --write",
"test": "bun test",
+ "test:gpu": "GPU=1 bun test",
"test:browser": "playwright test"
},
"dependencies": {
@@ -16,6 +17,7 @@
"@vercel/speed-insights": "^2.0.0",
"class-variance-authority": "^0.7.1",
"cn": "^0.2.4",
+ "gl-matrix": "^3.4.4",
"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 f4457f3..562ca85 100644
--- a/src/lib/decode/index.ts
+++ b/src/lib/decode/index.ts
@@ -1,15 +1,13 @@
import type { Gpu, Target } from "vgpu";
+import { uploadTiff } from "@/lib/tiff-gpu";
import { decodeHeic } from "./heic";
import linearize from "./linearize";
import decodeSvg from "./svg";
-import type { Decoder, Pixels } from "./types";
+import type { Decoded, Decoder } from "./types";
export type { Target };
-/**
- * Decoder backed by a worker module: post the file, receive transferred pixels or an error.
- * Unused since HEIC moved to WebCodecs; kept for CPU-heavy decoders to come (RAW).
- */
+/** Decoder backed by a worker module: post the file, receive transferred pixels or an error. */
export function workerDecoder(
load: () => Promise<{ default: new () => Worker }>,
) {
@@ -20,7 +18,7 @@ export function workerDecoder(
const worker = new Spawn();
worker.onmessage = ({
data,
- }: MessageEvent<Pixels | { error: string }>) => {
+ }: MessageEvent<Decoded | { error: string }>) => {
"error" in data ? reject(new Error(data.error)) : resolve(data);
worker.terminate();
};
@@ -38,8 +36,14 @@ type Format = {
const native = async () => createImageBitmap;
const svg = async () => decodeSvg;
const heic = async () => decodeHeic;
+const tiff = workerDecoder(() => import("./tiff.worker?worker"));
const formats: Format[] = [
+ {
+ types: ["image/tiff", "image/x-tiff"],
+ extensions: ["tif", "tiff"],
+ load: tiff,
+ },
{ types: ["image/png"], extensions: ["png"], load: native },
{ types: ["image/jpeg"], extensions: ["jpg", "jpeg"], load: native },
{ types: ["image/gif"], extensions: ["gif"], load: native },
@@ -76,5 +80,8 @@ export default async function decode(gpu: Gpu, file: File): Promise<Target> {
throw new Error(`Unsupported image: ${file.name}`);
}
const decoder = await format.load();
- return linearize(gpu, await decoder(file));
+ const decoded = await decoder(file);
+ return "chunks" in decoded
+ ? uploadTiff(gpu, decoded)
+ : linearize(gpu, decoded);
}
diff --git a/src/lib/decode/linearize.ts b/src/lib/decode/linearize.ts
index 197c420..3a0d8b8 100644
--- a/src/lib/decode/linearize.ts
+++ b/src/lib/decode/linearize.ts
@@ -1,4 +1,5 @@
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";
@@ -18,7 +19,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: Decoded) {
+function stage(gpu: Gpu, decoded: Exclude<Decoded, Prepared>) {
const queue = gpu.gpu.queue;
const size: Size = [decoded.width, decoded.height];
if (decoded instanceof ImageBitmap) {
@@ -65,7 +66,10 @@ function stage(gpu: Gpu, decoded: Decoded) {
}
/** GPU leg for sRGB-encoded decoders: renders the decoded image into a linear Rec.2020 rgba16float target. */
-export default function linearize(gpu: Gpu, decoded: Decoded) {
+export default function linearize(
+ gpu: Gpu,
+ decoded: Exclude<Decoded, Prepared>,
+) {
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.worker.ts b/src/lib/decode/tiff.worker.ts
new file mode 100644
index 0000000..7b39b23
--- /dev/null
+++ b/src/lib/decode/tiff.worker.ts
@@ -0,0 +1,16 @@
+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 c693006..91955b4 100644
--- a/src/lib/decode/types.ts
+++ b/src/lib/decode/types.ts
@@ -1,3 +1,5 @@
+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;
@@ -12,5 +14,5 @@ export type Frames = {
columns: number;
rotation: number;
};
-export type Decoded = ImageBitmap | Pixels | Frames;
+export type Decoded = ImageBitmap | Pixels | Frames | Prepared;
export type Decoder = (file: Blob) => Promise<Decoded>;
diff --git a/src/lib/tiff-gpu/README.md b/src/lib/tiff-gpu/README.md
new file mode 100644
index 0000000..881c99d
--- /dev/null
+++ b/src/lib/tiff-gpu/README.md
@@ -0,0 +1,64 @@
+# 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
new file mode 100644
index 0000000..0fe4be3
--- /dev/null
+++ b/src/lib/tiff-gpu/bench.ts
@@ -0,0 +1,22 @@
+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
new file mode 100644
index 0000000..039b717
--- /dev/null
+++ b/src/lib/tiff-gpu/blit.wgsl
@@ -0,0 +1,14 @@
+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
new file mode 100644
index 0000000..b36826e
--- /dev/null
+++ b/src/lib/tiff-gpu/codecs.ts
@@ -0,0 +1,136 @@
+/** 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
new file mode 100644
index 0000000..6302c53
--- /dev/null
+++ b/src/lib/tiff-gpu/color.ts
@@ -0,0 +1,181 @@
+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
new file mode 100644
index 0000000..980130e
--- /dev/null
+++ b/src/lib/tiff-gpu/fixtures/README.md
@@ -0,0 +1,26 @@
+# 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/alpha16.tif b/src/lib/tiff-gpu/fixtures/alpha16.tif
new file mode 100644
index 0000000..5c2d1d3
Binary files /dev/null and b/src/lib/tiff-gpu/fixtures/alpha16.tif differ
diff --git a/src/lib/tiff-gpu/fixtures/bilevel.tif b/src/lib/tiff-gpu/fixtures/bilevel.tif
new file mode 100644
index 0000000..5b07714
Binary files /dev/null and b/src/lib/tiff-gpu/fixtures/bilevel.tif differ
diff --git a/src/lib/tiff-gpu/fixtures/float32-be.tif b/src/lib/tiff-gpu/fixtures/float32-be.tif
new file mode 100644
index 0000000..80febe6
Binary files /dev/null and b/src/lib/tiff-gpu/fixtures/float32-be.tif differ
diff --git a/src/lib/tiff-gpu/fixtures/float64.tif b/src/lib/tiff-gpu/fixtures/float64.tif
new file mode 100644
index 0000000..d3dc42b
Binary files /dev/null and b/src/lib/tiff-gpu/fixtures/float64.tif differ
diff --git a/src/lib/tiff-gpu/fixtures/gray16-para.tif b/src/lib/tiff-gpu/fixtures/gray16-para.tif
new file mode 100644
index 0000000..2275566
Binary files /dev/null and b/src/lib/tiff-gpu/fixtures/gray16-para.tif differ
diff --git a/src/lib/tiff-gpu/fixtures/half-predictor.tif b/src/lib/tiff-gpu/fixtures/half-predictor.tif
new file mode 100644
index 0000000..fc8bc5c
Binary files /dev/null and b/src/lib/tiff-gpu/fixtures/half-predictor.tif differ
diff --git a/src/lib/tiff-gpu/fixtures/lzw-strips.tif b/src/lib/tiff-gpu/fixtures/lzw-strips.tif
new file mode 100644
index 0000000..8d0a4a8
Binary files /dev/null and b/src/lib/tiff-gpu/fixtures/lzw-strips.tif differ
diff --git a/src/lib/tiff-gpu/fixtures/lzw-tiles.tif b/src/lib/tiff-gpu/fixtures/lzw-tiles.tif
new file mode 100644
index 0000000..7f64e82
Binary files /dev/null and b/src/lib/tiff-gpu/fixtures/lzw-tiles.tif differ
diff --git a/src/lib/tiff-gpu/fixtures/orientation-3.tif b/src/lib/tiff-gpu/fixtures/orientation-3.tif
new file mode 100644
index 0000000..92dcb39
Binary files /dev/null and b/src/lib/tiff-gpu/fixtures/orientation-3.tif differ
diff --git a/src/lib/tiff-gpu/fixtures/orientation-6.tif b/src/lib/tiff-gpu/fixtures/orientation-6.tif
new file mode 100644
index 0000000..cb0501e
Binary files /dev/null and b/src/lib/tiff-gpu/fixtures/orientation-6.tif differ
diff --git a/src/lib/tiff-gpu/fixtures/packed12.tif b/src/lib/tiff-gpu/fixtures/packed12.tif
new file mode 100644
index 0000000..7c70846
Binary files /dev/null and b/src/lib/tiff-gpu/fixtures/packed12.tif differ
diff --git a/src/lib/tiff-gpu/fixtures/palette8.tif b/src/lib/tiff-gpu/fixtures/palette8.tif
new file mode 100644
index 0000000..cb0f442
Binary files /dev/null and b/src/lib/tiff-gpu/fixtures/palette8.tif differ
diff --git a/src/lib/tiff-gpu/fixtures/precision16.tif b/src/lib/tiff-gpu/fixtures/precision16.tif
new file mode 100644
index 0000000..1fac721
Binary files /dev/null and b/src/lib/tiff-gpu/fixtures/precision16.tif differ
diff --git a/src/lib/tiff-gpu/fixtures/reference.json b/src/lib/tiff-gpu/fixtures/reference.json
new file mode 100644
index 0000000..dacf28b
--- /dev/null
+++ b/src/lib/tiff-gpu/fixtures/reference.json
@@ -0,0 +1,491 @@
+[
+ {
+ "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
new file mode 100644
index 0000000..3853682
Binary files /dev/null and b/src/lib/tiff-gpu/fixtures/rgb16-bigtiff.tif differ
diff --git a/src/lib/tiff-gpu/fixtures/rgb16-le.tif b/src/lib/tiff-gpu/fixtures/rgb16-le.tif
new file mode 100644
index 0000000..530594b
Binary files /dev/null and b/src/lib/tiff-gpu/fixtures/rgb16-le.tif differ
diff --git a/src/lib/tiff-gpu/fixtures/rgb16-lzw-be.tif b/src/lib/tiff-gpu/fixtures/rgb16-lzw-be.tif
new file mode 100644
index 0000000..d22864e
Binary files /dev/null and b/src/lib/tiff-gpu/fixtures/rgb16-lzw-be.tif differ
diff --git a/src/lib/tiff-gpu/fixtures/rgb16-planar-tiled.tif b/src/lib/tiff-gpu/fixtures/rgb16-planar-tiled.tif
new file mode 100644
index 0000000..68bd0e5
Binary files /dev/null and b/src/lib/tiff-gpu/fixtures/rgb16-planar-tiled.tif differ
diff --git a/src/lib/tiff-gpu/fixtures/rgb16-prophoto.tif b/src/lib/tiff-gpu/fixtures/rgb16-prophoto.tif
new file mode 100644
index 0000000..ba3960f
Binary files /dev/null and b/src/lib/tiff-gpu/fixtures/rgb16-prophoto.tif differ
diff --git a/src/lib/tiff-gpu/fixtures/rgb8-jpeg.tif b/src/lib/tiff-gpu/fixtures/rgb8-jpeg.tif
new file mode 100644
index 0000000..e351659
Binary files /dev/null and b/src/lib/tiff-gpu/fixtures/rgb8-jpeg.tif differ
diff --git a/src/lib/tiff-gpu/fixtures/rgb8-packbits.tif b/src/lib/tiff-gpu/fixtures/rgb8-packbits.tif
new file mode 100644
index 0000000..39f1a4c
Binary files /dev/null and b/src/lib/tiff-gpu/fixtures/rgb8-packbits.tif differ
diff --git a/src/lib/tiff-gpu/fixtures/rgb8-srgb-table.tif b/src/lib/tiff-gpu/fixtures/rgb8-srgb-table.tif
new file mode 100644
index 0000000..300ac19
Binary files /dev/null and b/src/lib/tiff-gpu/fixtures/rgb8-srgb-table.tif differ
diff --git a/src/lib/tiff-gpu/ifd.ts b/src/lib/tiff-gpu/ifd.ts
new file mode 100644
index 0000000..0487497
--- /dev/null
+++ b/src/lib/tiff-gpu/ifd.ts
@@ -0,0 +1,243 @@
+/** 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
new file mode 100644
index 0000000..29ae0c6
--- /dev/null
+++ b/src/lib/tiff-gpu/index.ts
@@ -0,0 +1,25 @@
+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
new file mode 100644
index 0000000..e9e28a5
--- /dev/null
+++ b/src/lib/tiff-gpu/prepare.ts
@@ -0,0 +1,151 @@
+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
new file mode 100644
index 0000000..8ae2c98
--- /dev/null
+++ b/src/lib/tiff-gpu/testing.ts
@@ -0,0 +1,82 @@
+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
new file mode 100644
index 0000000..4e1f7c7
--- /dev/null
+++ b/src/lib/tiff-gpu/tiff-gpu.test.ts
@@ -0,0 +1,246 @@
+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
new file mode 100644
index 0000000..e5afb3e
--- /dev/null
+++ b/src/lib/tiff-gpu/unpack.wgsl
@@ -0,0 +1,145 @@
+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
new file mode 100644
index 0000000..23cfef7
--- /dev/null
+++ b/src/lib/tiff-gpu/upload.ts
@@ -0,0 +1,278 @@
+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/tests/tiff.e2e.ts b/tests/tiff.e2e.ts
new file mode 100644
index 0000000..98e2788
--- /dev/null
+++ b/tests/tiff.e2e.ts
@@ -0,0 +1,33 @@
+import { readFile } from "node:fs/promises";
+import { expect, test } from "./fixtures";
+import { readImage } from "./images";
+
+const directory = "src/lib/tiff-gpu/fixtures";
+
+test("TIFF files open through the loader with their color and orientation", async ({
+ page,
+}) => {
+ await page.goto("/");
+ await page.waitForFunction(() => window.openlight);
+ 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)),
+ { bytes, name },
+ );
+ };
+ // A gray profile file exported back to sRGB returns its encoded value: 16384 of 65535 is 64 of 255.
+ await load("gray16-para.tif");
+ const gray = await readImage(page);
+ expect(gray.size).toEqual([7, 5]);
+ for (const value of gray.center.slice(0, 3)) {
+ expect(Math.abs(value - 64)).toBeLessThanOrEqual(1);
+ }
+ await load("orientation-6.tif");
+ expect((await readImage(page)).size).toEqual([17, 19]);
+ await load("rgb8-jpeg.tif");
+ await expect(
+ page.getByText("Couldn't open rgb8-jpeg.tif:", { exact: false }),
+ ).toBeVisible();
+});