Skip to content

Free open-source image editor for the browser

Import .cube LUTs as effect layers (#62)

Rodrigo Belfioreaa0d31b9c4fb
diff --git a/API.md b/API.md
index 5e8084e..85af622 100644
--- a/API.md
+++ b/API.md
@@ -25,8 +25,8 @@ These methods return promises; await them before editing. All but `loadUrl`, whi
| Method | Behavior |
| --- | --- |
-| `openFile(file)` | Opens one image or [scene file](#scene-files), or imports one Camera Raw XMP file. |
-| `openFiles(files)` | Opens the first recognized image or scene file, then imports recognized XMP files in order. Unsupported files are ignored. |
+| `openFile(file)` | Opens one image or [scene file](#scene-files), imports one Camera Raw XMP file, or adds a [LUT](#luts) layer from one `.cube` file. |
+| `openFiles(files)` | Opens the first recognized image or scene file, then imports recognized XMP and `.cube` files in order. Unsupported files are ignored. |
| `loadImage(file)` | Loads a file as an image, replacing the current document and its history. |
| `loadScene(file)` | Opens a scene file as a new document, replacing the current one and its history. |
| `recoverDraft()` | Opens the stored [draft](#drafts) as a new document, like `loadScene`. Rejects when no draft is stored or storage fails. |
@@ -34,7 +34,7 @@ These methods return promises; await them before editing. All but `loadUrl`, whi
| `loadUrl(url)` | Fetches an image and loads it. A URL on another origin must allow CORS; the response's content type identifies an image whose URL has no extension. |
| `importXmp(file)` | Applies supported Camera Raw adjustments as one undoable edit. |
-File loads are queued; `loadUrl` starts at once, and when two opens overlap the later one wins. XMP import is skipped if no document is ready; an invalid XMP import can reject without blocking later loads. Image and scene failures do not reject the loading promise. Without a document open, a failure leaves the workspace in its error state; with one, that document stays open and keeps its history. Either way `getState().failure` names the file and the error, and settings loaded in the same batch are skipped. Check `getState().documentId` or `failure` to confirm success. Loading completion does not guarantee the preview has rendered.
+File loads are queued; `loadUrl` starts at once, and when two opens overlap the later one wins. XMP and `.cube` imports are skipped if no document is ready; an invalid XMP import can reject without blocking later loads. Image, scene, and `.cube` failures do not reject the loading promise. Without a document open, a failure leaves the workspace in its error state; with one, that document stays open and keeps its history. Either way `getState().failure` names the file and the error, and settings loaded in the same batch are skipped. Check `getState().documentId` or `failure` to confirm success. Loading completion does not guarantee the preview has rendered.
## Editing
@@ -82,7 +82,7 @@ Direct mask children of another mask modify coverage instead of processing image
| Method | Behavior |
| --- | --- |
-| `addLayer(kind, placement?)` | Adds `"exposure"`, `"color-mixer"`, `"details"`, `"vignette"`, `"grain"`, `"fill"`, `"heal"`, or `"mask"`; selects and returns its ID. `{ inside: id }` appends a child to a processing layer; `{ above: id }` inserts directly above that layer among its siblings; without a placement, the layer goes on top of the root stack. Exposure starts at +1 EV, Vignette at intensity 50, Grain at amount 25; Color Mixer, Details, Healing, and masks start neutral. |
+| `addLayer(kind, placement?)` | Adds `"exposure"`, `"color-mixer"`, `"details"`, `"vignette"`, `"grain"`, `"fill"`, `"heal"`, or `"mask"`; selects and returns its ID. A `"lut"` layer comes from a `.cube` file instead. `{ inside: id }` appends a child to a processing layer; `{ above: id }` inserts directly above that layer among its siblings; without a placement, the layer goes on top of the root stack. Exposure starts at +1 EV, Vignette at intensity 50, Grain at amount 25; Color Mixer, Details, Healing, and masks start neutral. |
| `selectLayer(id)` | Selects any layer. |
| `setLayer(id, change)` | Updates processing-layer `name`, `visible`, or `opacity` (0–1). |
| `setExposure(id, value)` | Sets an Exposure layer to -5…5 EV. |
@@ -96,6 +96,14 @@ A linear gradient has full coverage at `start`, zero at `end`; its points must b
A brush mask is a list of strokes. Each stroke has `mode` (`"paint"` or `"erase"`), `size` (diameter in source pixels), `feather` and `flow` (0–1), and `points`, each `[x, y, pressure]` in source pixels with pressure 0–1. Dabs land every quarter diameter along the points; a paint stroke adds `flow × pressure` of the remaining coverage under each dab, and an erase stroke removes that share of the existing coverage. The renderer rasterizes strokes into a cached coverage texture at source resolution and only stamps new points, so appending to the last stroke is cheap and undo replays the rest. A brush inside another mask keeps its own coverage, paint and erase strokes alike, which the mask adds or subtracts scaled by the brush layer's opacity. A mask with no painted coverage and nothing added to it is bypassed.
+## LUTs
+
+A LUT layer grades the image below it with a 3D lookup table from an Adobe or Resolve `.cube` file. `openFile` with a `.cube` file adds one on top of the stack, named after the file's `TITLE` or, without one, the file; the Add menu asks for a file and places the layer as it places other effects, and the layer's panel replaces its file. A replacement keeps the layer's name unless it still reads the previous LUT's name. The layer's opacity sets its strength. `addLayer` cannot create a LUT layer, since it needs a file.
+
+The file needs `LUT_3D_SIZE`, a whole number from 2 to 65, before its rows: `LUT_3D_SIZE`³ lines of three numbers, red varying fastest, then green, then blue. `TITLE`, `DOMAIN_MIN` and `DOMAIN_MAX` (default 0 and 1), or Resolve's `LUT_3D_INPUT_RANGE`, are read; comments, blank lines, and other keywords are skipped. 1D LUTs are rejected, and so is a file with too few or too many rows, naming the line where one is wrong.
+
+Creative LUTs expect and return display-referred, sRGB-encoded color. The layer converts the working color as display does, into sRGB clipped to its gamut with hue and luminance kept, so headroom above white clips at the LUT; the color, clamped to the LUT's domain, is interpolated tetrahedrally between its entries, and the result, clamped to 0–1, converts back to the working space. The image layer's adjustments still apply after it, on the composite.
+
## History
Each content change creates an undo step unless a group is open. No-op edits add no history. Preview changes stay outside history. Groups do not nest. While a group is open, the preview renders a reduced proxy of the image at the display's scale; committing or cancelling renders the full image again.
@@ -147,7 +155,7 @@ The image renders at the document dimensions and downsamples to `longEdge` with
## Scene files
-`exportScene()` returns `Promise<File>`: the document as an `.openlight` file named after its source image, which **Save scene** in the Export panel downloads. It is a ZIP archive holding the source file's original bytes at `sources/<id>` and a deflated `scene.json`:
+`exportScene()` returns `Promise<File>`: the document as an `.openlight` file named after its source image, which **Save scene** in the Export panel downloads. It is a ZIP archive holding the original bytes of the source image and of each LUT the scene uses at `sources/<id>`, and a deflated `scene.json`:
```json
{
@@ -158,7 +166,7 @@ The image renders at the document dimensions and downsamples to `longEdge` with
}
```
-`scene` is the `getState()` scene; each image layer's `source` names an entry in `sources`. Opening the file with `loadScene`, `openFile`, a drop, or the file picker decodes the stored source again and restores the frame and every layer as a new document with empty history. Preview settings and history are not saved.
+`scene` is the `getState()` scene; the image layer's `source` and each LUT layer's `lut` name entries in `sources`. Opening the file with `loadScene`, `openFile`, a drop, or the file picker decodes the stored source again and restores the frame and every layer as a new document with empty history. Preview settings and history are not saved.
Opening validates every value as the matching command does, and a file that fails leaves the workspace in its error state with a message naming the first invalid field. Fields OpenLight does not know are dropped. A parameter missing from `adjustments`, `details`, `vignette`, `grain`, `fill`, or `colorMixer` takes its default, so older files still open when a group gains a parameter; a RAW image without a white balance uses its As Shot value. `version` increases only when older files can no longer open as written; a newer version is rejected.
@@ -166,7 +174,7 @@ Opening validates every value as the matching command does, and a file that fail
Once a document has an edit, OpenLight keeps it as the draft in the browser's IndexedDB, so closing the tab loses nothing. A save follows 1.5 s after the last scene change and flushes when the tab is hidden or the page unloads; saves run one at a time and never render. Opening an image or scene without editing it keeps the previous draft. Only the latest document is kept.
-A draft stores the same `scene.json` object a [scene file](#scene-files) holds, in a record with its own `version` and the document's name, while each source file sits in a separate store under its source ID. A save keeps source files already stored under their ID and deletes unreferenced ones in the same transaction, so edits never rewrite the photo.
+A draft stores the same `scene.json` object a [scene file](#scene-files) holds, in a record with its own `version` and the document's name, while each source file, the image and any LUTs, sits in a separate store under its ID. A save keeps source files already stored under their ID and deletes unreferenced ones in the same transaction, so edits never rewrite the photo.
On a fresh load the start screen shows a notice in the viewport's corner with **Recover** and **Forget**. `recoverDraft()` opens the draft through the same validation as a scene file, restoring each source under its original ID, with empty history; a newer draft `version` is rejected rather than dropped, and a parameter added to a group since takes its default. `discardDraft()` removes it. Both return `Promise<void>`. When IndexedDB is unavailable or fails, a dismissible notice suggests saving a scene file, and editing continues.
diff --git a/ARCHITECTURE.md b/ARCHITECTURE.md
index f509c8e..454dda1 100644
--- a/ARCHITECTURE.md
+++ b/ARCHITECTURE.md
@@ -9,7 +9,7 @@ Where the code lives and the rules that keep it in place. Keep this file true: w
| `src/main.tsx` | Mounts the GPU provider and the app. |
| `app/index.tsx` | The app: workspace, file drop, draft notice. |
| `app/workspace` | The open document, its replacement, and loading state. A replacement loads beside the open document and takes its place only once it opens; a failure leaves the open document in place. |
-| `app/loaders` | Image, scene file, and Camera Raw XMP loaders. |
+| `app/loaders` | Image, scene file, Camera Raw XMP, and `.cube` LUT loaders. |
| `app/draft` | The draft autosaved in the browser. |
| `app/controls.ts` | `window.openlight`, documented in [API.md](API.md). |
| `app/commands.ts` | The commands: serializable, validated edits that `run`, WebMCP, and other callers share; see [API.md](API.md#commands). |
@@ -28,7 +28,7 @@ Where the code lives and the rules that keep it in place. Keep this file true: w
| `components/editor` | Editor primitives: layout, panel, dock, parameters, viewport, document and renderer contexts, brush input. |
| `components/ui` | What [`@roprgm/ui`](https://ui.roprgm.com) lacks; see [DESIGN.md](DESIGN.md). |
| `core/document` | Scene contract, layer tree, history, resources. |
-| `core/image` | Image sources, decoding, geometry, color. |
+| `core/image` | Image sources, decoding, geometry, color, and the lookup-table type LUTs read into. |
| `core/renderer` | Render nodes, graph execution, masks, proxy, transform, display. |
| `features/<name>` | One capability: usually `model.ts` (parameters, defaults), `edits.ts`, `pass.ts` with its `.wgsl`, and `controls.tsx`. |
| `lib` | Utilities independent of OpenLight. |
@@ -56,7 +56,7 @@ Dependencies point downward. Features do not import each other; `app/` connects
## Engine and React
- Document edits and rendering run without React, a mounted UI, or an implicit active document: pass workspace, document, and GPU explicitly. Feature processing imports without its panel.
-- Each document owns a vanilla Zustand scene store, history, and image resources. Scenes hold immutable, serializable content and image-source IDs; files and GPU resources stay outside history.
+- Each document owns a vanilla Zustand scene store, history, and resources: its image sources and LUT tables with their files. Scenes hold immutable, serializable content and the IDs of those resources; files, tables, and GPU resources stay outside history, and a resource goes once no state in history uses it.
- UI controls, `window.openlight`, and commands call the same edits. A slider or curve gesture is one edit; cancelling restores the previous scene. Preview settings and navigation stay outside history.
- The engine owns GPU resources, rendering, and derived data such as histogram bins; frame data stays out of React state. Every resource owner disposes what it creates.
@@ -71,6 +71,8 @@ Dependencies point downward. Features do not import each other; `app/` connects
- The graph keeps intermediate textures for the last two sets of sizes it rendered, so interactive proxy renders and full renders alternate without reallocating full-size textures, which a phone's GPU memory cannot absorb gesture after gesture.
- Use `vgpu` for GPU work and `vgpu-react` for bindings. Create pipelines once per engine and reuse them. Keep `.wgsl` beside its owner.
- The working space is linear Rec.2020 in `rgba16float`. Decoders convert into it and display converts out; passes preserve format and primaries unless they explicitly convert.
+- A LUT layer converts explicitly: its table expects display-referred sRGB, so it receives the working color as the display shows it, headroom above white clipped, and its output returns to linear Rec.2020.
+- A node's storage arrays upload when the array changes; one passed again, such as a LUT's table, keeps its buffer, which the graph owns and releases with the layer.
- Layers process in stack order; the image layer's adjustments and tone curve run last, on the composite.
- Preserve HDR headroom through exposure, curves, and vibrance. Exposure clips negatives and applies one luminance gain to all channels.
- The adjustment shader's parameters use UI units; its fitted constants are calibration data.
diff --git a/CONTEXT.md b/CONTEXT.md
index af05a08..83e07d5 100644
--- a/CONTEXT.md
+++ b/CONTEXT.md
@@ -5,14 +5,15 @@ OpenLight edits documents containing image content and adjustments.
| Term | Meaning |
| --- | --- |
| **Document** | An independent editing session with its own canvas, history, and resources. Opening an image creates a document containing that image. |
-| **Scene** | The document's serializable content: canvas geometry, image-source references, and adjustments. Files and GPU resources live outside it. |
+| **Scene** | The document's serializable content: canvas geometry, references to image sources and LUTs, and adjustments. Files and GPU resources live outside it. |
| **Layer** | A source, effect, or mask in the composition. Image layers own basic adjustments; effects process the image below; masks scope their basic adjustments and child effects. Linear, radial, and brush child masks add or subtract coverage. Layers compose render nodes without owning textures. |
| **Stroke** | One brush drag: paint or erase, size, feather, flow, and points with pressure in source pixels. Strokes are content; the renderer rasterizes them into a cached coverage texture. A Healing patch stores a hard stroke and feathers its accumulated shape separately. |
| **Healing patch** | One painted, non-destructive repair in a Healing layer. It copies a nearby donor, found automatically or set by hand, with a bounded boundary color correction. Patches replay in order and each sees the result of the preceding patches. Size belongs to the recorded hard stroke; feather and opacity remain editable on its accumulated shape. |
| **Image source** | An image used as input to the document's composition. It is content within a document. |
+| **LUT** | A 3D color lookup table read from a `.cube` file, such as a creative grade. A LUT layer applies one to the image below it, at the layer's opacity. Like an image source, its file is content within the document, which the scene references by ID. |
| **Edit** | One reversible content change. A continuous slider or curve drag is one edit, as is importing a set of adjustments. |
-| **Scene file** | A document saved as an `.openlight` archive: its scene and the original source files. Opening it creates a document that continues editing; history and preview settings are not saved. |
-| **Draft** | The latest edited document, kept in the browser as a scene with its source files so it survives closing the tab. The start screen offers to recover or forget it; recovering opens a new document with empty history. |
+| **Scene file** | A document saved as an `.openlight` archive: its scene and the original files it uses, the image and any LUTs. Opening it creates a document that continues editing; history and preview settings are not saved. |
+| **Draft** | The latest edited document, kept in the browser as a scene with its files so it survives closing the tab. The start screen offers to recover or forget it; recovering opens a new document with empty history. |
| **Workspace** | The open-document session. It currently holds at most one document, replaced when another image or scene file is opened. |
| **Tool** | What a drag on the canvas does: Brush paints, Healing repairs, and Linear and Radial gradient draw masks and edit the selected gradient's guides. Adjust leaves the canvas to pan and zoom. Crop and Export bring their own view. Switching tools never changes the sidebar; Brush and Healing create their layer on entry and remove it again if it leaves untouched. A selected mask is *active*; it is *edited* only while a shape tool is on. |
diff --git a/README.md b/README.md
index 17b5284..2c50d94 100644
--- a/README.md
+++ b/README.md
@@ -20,7 +20,7 @@ Image processing runs on your own GPU with WebGPU. Photos never leave your devic
**Layers and masks**
-- Draggable effect layers: Details, Exposure, Color Mixer, Vignette, Grain, and Color, which paints one color with Photoshop blend modes.
+- Draggable effect layers: Details, Exposure, Color Mixer, Vignette, Grain, Color, which paints one color with Photoshop blend modes, and LUT, which grades the photo with a 3D LUT from a `.cube` file.
- Linear and radial masks with their own adjustments and child effects.
- Add and Subtract submasks, from a mask's actions menu, to shape its coverage.
@@ -44,7 +44,7 @@ Image processing runs on your own GPU with WebGPU. Photos never leave your devic
**Files**
- Open JPEG, PNG, WebP, AVIF, GIF, BMP, SVG, HEIC, TIFF at 8-bit, 16-bit, and floating-point precision, and camera RAW/DNG with absolute white balance and As Shot reset.
-- Import Camera Raw XMP settings.
+- Import Camera Raw XMP settings, and `.cube` LUTs as layers.
- Export PNG, JPEG, or WebP with resizing, a live preview, and the resulting file size.
- Save the photo with every edit as an OpenLight scene, then open it to continue editing.
- Recover the latest edited photo after closing the tab, from a draft kept in the browser.
diff --git a/src/app/controls.ts b/src/app/controls.ts
index 08fca0e..398c2d8 100644
--- a/src/app/controls.ts
+++ b/src/app/controls.ts
@@ -10,6 +10,7 @@ import {
} from "@/app/editor/export/export-image";
import { createCameraRawXmpLoader } from "@/app/loaders/camera-raw-xmp";
import { createImageLoader } from "@/app/loaders/image";
+import { createLutLoader } from "@/app/loaders/lut";
import { createLoaderRegistry } from "@/app/loaders/registry";
import { createSceneLoader, writeSceneFile } from "@/app/loaders/scene";
import type {
@@ -20,7 +21,6 @@ import type {
Grain,
Mask,
Preview,
- ProcessingLayer,
ToneCurve,
Vignette,
} from "@/core/document";
@@ -63,6 +63,7 @@ import { type Command, runCommand } from "./commands";
import {
createLayer,
createMask,
+ type DefaultEffect,
editEffect,
findEffect,
} from "./editor/layers";
@@ -77,8 +78,9 @@ export function createControls(
const image = createImageLoader(gpu, workspace);
const xmp = createCameraRawXmpLoader(workspace);
const scene = createSceneLoader(gpu, workspace);
+ const lut = createLutLoader(workspace);
const files = createLoaderRegistry(
- [scene, xmp, image],
+ [scene, xmp, lut, image],
() => workspace.state.getState().status === "ready",
);
@@ -145,7 +147,7 @@ export function createControls(
addHealPatch(workspace.getDocument(), id, stroke, offset),
setHealSource: (id: string, patchId: string, offset: Point) =>
setHealSource(workspace.getDocument(), id, patchId, offset),
- addLayer(kind: ProcessingLayer["kind"], placement?: LayerPlacement) {
+ addLayer(kind: DefaultEffect | "mask", placement?: LayerPlacement) {
const document = workspace.getDocument();
if (kind !== "mask") {
return addLayer(document, createLayer(kind), placement);
diff --git a/src/app/editor/adjust/index.tsx b/src/app/editor/adjust/index.tsx
index 231b96c..a172a03 100644
--- a/src/app/editor/adjust/index.tsx
+++ b/src/app/editor/adjust/index.tsx
@@ -3,6 +3,7 @@ import { Slider } from "@roprgm/ui/slider";
import { type ReactNode, useCallback, useState } from "react";
import { useStore } from "zustand";
import { effectKinds } from "@/app/editor/layers";
+import { useReportFailure } from "@/app/editor/open";
import { DockChips, DockControls } from "@/components/editor/dock";
import { PanelBody, PanelHeader } from "@/components/editor/panel";
import type { Parameter } from "@/components/editor/parameter";
@@ -38,6 +39,7 @@ import { GrainControls, grainParameters } from "@/features/grain/controls";
import { HealControls } from "@/features/heal/controls";
import { Histogram } from "@/features/histogram";
import { OverlayToggle } from "@/features/layers/overlay-toggle";
+import { LutControls } from "@/features/lut/controls";
import { defaultCurve } from "@/features/tone-curves/curve";
import { setToneCurve } from "@/features/tone-curves/edits";
import { ToneCurves } from "@/features/tone-curves/tone-curves";
@@ -115,6 +117,14 @@ function exposureParameter(
};
}
+/** A replacement file that can't be read shows beside the document, as a failed open does. */
+function LayerLut({ layer }: { layer: Kind<"lut"> }) {
+ const reportFailure = useReportFailure();
+ return (
+ <LutControls id={layer.id} lut={layer.lut} onFailure={reportFailure} />
+ );
+}
+
function SelectedControls({ layer }: { layer: Layer }) {
const document = useDocument();
switch (layer.kind) {
@@ -142,6 +152,8 @@ function SelectedControls({ layer }: { layer: Layer }) {
return <GrainControls id={layer.id} grain={layer.grain} />;
case "fill":
return <FillControls id={layer.id} fill={layer.fill} />;
+ case "lut":
+ return <LayerLut layer={layer} />;
case "heal":
return <HealControls id={layer.id} patches={layer.patches} />;
case "exposure": {
@@ -361,6 +373,12 @@ function SelectedDials({
<FillControls id={layer.id} fill={layer.fill} />
</DockControls>
);
+ case "lut":
+ return (
+ <DockControls header={<DockTitle layer={layer} />}>
+ <LayerLut layer={layer} />
+ </DockControls>
+ );
case "heal":
return (
<DockControls header={<DockTitle layer={layer} />}>
diff --git a/src/app/editor/export/export-image.ts b/src/app/editor/export/export-image.ts
index 6e0955f..236225f 100644
--- a/src/app/editor/export/export-image.ts
+++ b/src/app/editor/export/export-image.ts
@@ -82,7 +82,7 @@ export async function exportImage(
) {
const scene = document.scene.getState();
const source = document.resources.get(scene.layers[0].source);
- const renderer = createEditorRenderer(gpu, source);
+ const renderer = createEditorRenderer(gpu, source, document.resources.getLut);
try {
await renderer.update(scene);
// The renderer retains the source until encoding finishes.
diff --git a/src/app/editor/index.tsx b/src/app/editor/index.tsx
index 6e489b0..d113f83 100644
--- a/src/app/editor/index.tsx
+++ b/src/app/editor/index.tsx
@@ -33,7 +33,7 @@ import { ImageHistogram } from "./histogram";
import { HistoryControls } from "./history";
import { createMask } from "./layers";
import { MaskOverlaySync } from "./mask-overlay";
-import { OpenButton, OpenStatus } from "./open";
+import { FailureContext, OpenButton, OpenStatus } from "./open";
import { createEditorRenderer } from "./renderer";
import { EditorSidebar } from "./sidebar";
import { ToolRail } from "./tool-rail";
@@ -164,10 +164,12 @@ type ReadyState = Extract<
function DocumentEditor({
state,
onOpen,
+ onFailure,
onDismissFailure,
}: {
state: ReadyState;
onOpen: (files: File[]) => void;
+ onFailure: (file: string, error: unknown) => void;
onDismissFailure: () => void;
}) {
return (
@@ -187,11 +189,13 @@ function DocumentEditor({
<ComparisonControl />
<ExportButton />
</EditorHeader>
- <DockProvider>
- <EditorFrame rail={<ToolRail />} tabs={<DockTabs />}>
- <ToolView />
- </EditorFrame>
- </DockProvider>
+ <FailureContext value={onFailure}>
+ <DockProvider>
+ <EditorFrame rail={<ToolRail />} tabs={<DockTabs />}>
+ <ToolView />
+ </EditorFrame>
+ </DockProvider>
+ </FailureContext>
<OpenStatus
opening={state.opening}
failure={state.failure}
@@ -207,12 +211,15 @@ function DocumentEditor({
type EditorProps = {
state: ReturnType<Workspace["state"]["getState"]>;
onOpen: (files: File[]) => void;
+ /** A file the editor reads into the open document, such as a LUT, failed. */
+ onFailure: (file: string, error: unknown) => void;
onDismissFailure: () => void;
draft?: Recovery;
};
function EditorContent({
state,
onOpen,
+ onFailure,
onDismissFailure,
draft,
}: EditorProps) {
@@ -225,6 +232,7 @@ function EditorContent({
<DocumentEditor
state={state}
onOpen={onOpen}
+ onFailure={onFailure}
onDismissFailure={onDismissFailure}
/>
</RendererProvider>
diff --git a/src/app/editor/layers.ts b/src/app/editor/layers.ts
index 7b68075..19b008c 100644
--- a/src/app/editor/layers.ts
+++ b/src/app/editor/layers.ts
@@ -3,6 +3,7 @@ import { DetailsIcon } from "@/components/icons/details";
import { ExposureIcon } from "@/components/icons/exposure";
import { GrainIcon } from "@/components/icons/grain";
import { HealIcon } from "@/components/icons/heal";
+import { LutIcon } from "@/components/icons/lut";
import { VignetteIcon } from "@/components/icons/vignette";
import type {
EditorDocument,
@@ -14,12 +15,14 @@ import type {
ProcessingLayer,
} from "@/core/document";
import type { WhiteBalance } from "@/core/image";
+import type { LookupTable } from "@/core/image/lut";
import { defaultAdjustments } from "@/features/adjustments/model";
import { defaultMixer, isNeutral } from "@/features/color-mixer/model";
import { defaultDetails } from "@/features/details/model";
import { defaultFill } from "@/features/fill/model";
import { defaultGrain } from "@/features/grain/model";
import type { EffectKind } from "@/features/layers/controls";
+import { addLayer, type LayerPlacement } from "@/features/layers/edits";
import { defaultCurve } from "@/features/tone-curves/curve";
import { defaultVignette } from "@/features/vignette/model";
@@ -57,13 +60,17 @@ export const effectKinds = [
{ kind: "vignette", label: "Vignette", Icon: VignetteIcon, addable: true },
{ kind: "grain", label: "Grain", Icon: GrainIcon, addable: true },
{ kind: "fill", label: "Color", addable: true },
+ { kind: "lut", label: "LUT", Icon: LutIcon, addable: true },
{ kind: "heal", label: "Healing", Icon: HealIcon, addable: false },
] as const satisfies readonly EffectKind[];
-export function createLayer<K extends EffectLayer["kind"]>(
+/** Effects that start from defaults; a LUT layer needs its table first. */
+export type DefaultEffect = Exclude<EffectLayer["kind"], "lut">;
+
+export function createLayer<K extends DefaultEffect>(
kind: K,
): Extract<EffectLayer, { kind: K }>;
-export function createLayer(kind: EffectLayer["kind"]): EffectLayer {
+export function createLayer(kind: DefaultEffect): EffectLayer {
const name = effectKinds.find((entry) => entry.kind === kind)?.label ?? kind;
const base = { ...baseLayer(), name };
switch (kind) {
@@ -84,6 +91,25 @@ export function createLayer(kind: EffectLayer["kind"]): EffectLayer {
}
}
+/** Keeps a table read from `file` with the document and adds a layer named after it; returns the layer's ID. */
+export function addLut(
+ document: EditorDocument,
+ file: File,
+ table: LookupTable,
+ placement?: LayerPlacement,
+) {
+ // An open group commits first: releasing it would drop a table no layer uses yet.
+ document.history.commit();
+ const lut = document.resources.addLut(file, table);
+ const layer: ProcessingLayer = {
+ ...baseLayer(),
+ kind: "lut",
+ name: table.name,
+ lut,
+ };
+ return addLayer(document, layer, placement);
+}
+
const maskNames: Record<Mask["kind"], string> = {
linear: "Linear Gradient",
radial: "Radial Gradient",
@@ -121,6 +147,7 @@ function effectNeutral(layer: ProcessingLayer): boolean {
case "color-mixer":
return isNeutral(layer.colorMixer);
case "fill":
+ case "lut":
return false;
case "heal":
return layer.patches.length === 0;
@@ -153,7 +180,7 @@ export function findEffect<K extends EffectLayer["kind"]>(
/** Convenience commands address the first root effect of a kind or create one on top of the stack as one entry; returns the layer edited. */
export function editEffect(
document: EditorDocument,
- kind: EffectLayer["kind"],
+ kind: DefaultEffect,
id: string | undefined,
edit: (id: string) => void,
) {
diff --git a/src/app/editor/open.tsx b/src/app/editor/open.tsx
index 9af0628..fa6cfa2 100644
--- a/src/app/editor/open.tsx
+++ b/src/app/editor/open.tsx
@@ -2,7 +2,7 @@ import { IconButton } from "@roprgm/ui/icon-button";
import { Notice, NoticeClose } from "@roprgm/ui/notice";
import { Section, SectionAction } from "@roprgm/ui/section";
import { Spinner } from "@roprgm/ui/spinner";
-import { type RefObject, useRef } from "react";
+import { createContext, type RefObject, useContext, useRef } from "react";
import { sceneExtension } from "@/app/loaders/scene";
import type { OpenFailure } from "@/app/workspace";
import { OpenIcon } from "@/components/icons/open";
@@ -12,6 +12,19 @@ import { useShortcuts } from "@/hooks/use-shortcuts";
type OpenProps = { onOpen: (files: File[]) => void };
+type ReportFailure = (file: string, error: unknown) => void;
+
+/** Where the editor reports a file it read itself but couldn't use, such as a LUT, to show like a failed open. */
+export const FailureContext = createContext<ReportFailure | null>(null);
+
+export function useReportFailure() {
+ const report = useContext(FailureContext);
+ if (!report) {
+ throw new Error("useReportFailure requires FailureContext.");
+ }
+ return report;
+}
+
/** The hidden picker for images and scenes; clearing it after each pick lets the same file open again. */
function FileInput({
ref,
diff --git a/src/app/editor/renderer.ts b/src/app/editor/renderer.ts
index cc74d00..e46ab7a 100644
--- a/src/app/editor/renderer.ts
+++ b/src/app/editor/renderer.ts
@@ -1,6 +1,7 @@
import type { Gpu, Timer } from "vgpu";
import { maskModifiers, type ProcessingLayer } from "@/core/document";
import type { ImageSource } from "@/core/image";
+import type { LookupTable } from "@/core/image/lut";
import {
type Composition,
createRenderer,
@@ -18,6 +19,7 @@ import { unsharpMask } from "@/features/details/unsharp-mask";
import { fill } from "@/features/fill/pass";
import { grain } from "@/features/grain/pass";
import { heal } from "@/features/heal/pass";
+import { lut } from "@/features/lut/pass";
import { toneCurves } from "@/features/tone-curves/pass";
import { vignette } from "@/features/vignette/pass";
@@ -26,10 +28,18 @@ type Branch = {
input?: RenderImage;
};
+/** Finds the table a LUT layer names; a document's resources hold them. */
+type Luts = (id: string) => LookupTable;
+
+function noLuts(): never {
+ throw Error("LUT is unavailable.");
+}
+
function composeLayer(
below: RenderImage,
layer: ProcessingLayer,
composition: Composition,
+ luts: Luts,
): Branch {
const name = `layer/${layer.id}`;
composition.retain(name);
@@ -85,6 +95,9 @@ function composeLayer(
case "fill":
edited = pipeline(below, [fill(layer.fill, `${name}/fill`)]);
break;
+ case "lut":
+ edited = pipeline(below, [lut(luts(layer.lut), `${name}/lut`)]);
+ break;
case "heal": {
const result = heal(below, layer.patches, name, composition);
edited = result.image;
@@ -97,7 +110,7 @@ function composeLayer(
layer.kind === "mask"
? layer.children.filter((child) => child.kind !== "mask")
: layer.children;
- const children = composeLayers(edited, effects, composition);
+ const children = composeLayers(edited, effects, composition, luts);
const image = mixAdjustment(
name,
below,
@@ -114,11 +127,12 @@ function composeLayers(
below: RenderImage,
layers: readonly ProcessingLayer[],
composition: Composition,
+ luts: Luts,
): Branch {
let image = below;
let input: RenderImage | undefined;
for (const layer of layers) {
- const branch = composeLayer(image, layer, composition);
+ const branch = composeLayer(image, layer, composition, luts);
image = branch.image;
input ??= branch.input;
}
@@ -133,6 +147,7 @@ function composeLayers(
export function createEditorRenderer(
gpu: Gpu,
source: ImageSource,
+ luts: Luts = noLuts,
timer?: Timer,
) {
return createRenderer(
@@ -142,8 +157,18 @@ export function createEditorRenderer(
const [sourceLayer, ...layers] = scene.layers;
const name = `layer/${sourceLayer.id}`;
composition.retain(name);
- const children = composeLayers(image, sourceLayer.children, composition);
- const composite = composeLayers(children.image, layers, composition);
+ const children = composeLayers(
+ image,
+ sourceLayer.children,
+ composition,
+ luts,
+ );
+ const composite = composeLayers(
+ children.image,
+ layers,
+ composition,
+ luts,
+ );
const adjusted = pipeline(composite.image, [
adjustments(sourceLayer.adjustments, name),
]);
diff --git a/src/app/editor/sidebar.tsx b/src/app/editor/sidebar.tsx
index 8deb451..0babcce 100644
--- a/src/app/editor/sidebar.tsx
+++ b/src/app/editor/sidebar.tsx
@@ -1,26 +1,49 @@
+import { useRef } from "react";
import { useDocument } from "@/components/editor/session";
import type { EffectLayer } from "@/core/document";
import { findLayer } from "@/core/document";
import { LayersControls, LayersSection } from "@/features/layers/controls";
-import { addLayer } from "@/features/layers/edits";
+import { addLayer, type LayerPlacement } from "@/features/layers/edits";
+import { readCubeFile } from "@/features/lut/cube";
+import { LutInput } from "@/features/lut/input";
import { AdjustPanel } from "./adjust";
import { ImageHistogram } from "./histogram";
-import { createLayer, effectKinds } from "./layers";
+import { addLut, createLayer, effectKinds } from "./layers";
+import { useReportFailure } from "./open";
/** The layer stack with the effects the app offers; a new effect goes inside a selected root mask or above the selection. */
export function EditorLayers({ fill }: { fill?: boolean }) {
const document = useDocument();
- function add(kind: EffectLayer["kind"]) {
+ const reportFailure = useReportFailure();
+ const lutInput = useRef<HTMLInputElement>(null);
+ function placement(): LayerPlacement {
const scene = document.scene.getState();
const selected = document.selection.getState().layerId;
const layer = findLayer(scene.layers, selected);
- const placement =
- layer?.kind === "mask" && scene.layers.includes(layer)
- ? { inside: selected }
- : { above: selected };
- addLayer(document, createLayer(kind), placement);
+ return layer?.kind === "mask" && scene.layers.includes(layer)
+ ? { inside: selected }
+ : { above: selected };
+ }
+ function add(kind: EffectLayer["kind"]) {
+ if (kind === "lut") {
+ lutInput.current?.click();
+ return;
+ }
+ addLayer(document, createLayer(kind), placement());
+ }
+ async function addLutFile(file: File) {
+ try {
+ addLut(document, file, await readCubeFile(file), placement());
+ } catch (error) {
+ reportFailure(file.name, error);
+ }
}
- return <LayersControls effects={effectKinds} onAdd={add} fill={fill} />;
+ return (
+ <>
+ <LayersControls effects={effectKinds} onAdd={add} fill={fill} />
+ <LutInput ref={lutInput} onChoose={addLutFile} />
+ </>
+ );
}
/** The editing sidebar's sections, top to bottom; reorder them here. */
diff --git a/src/app/index.tsx b/src/app/index.tsx
index 31f5f43..3f38aa6 100644
--- a/src/app/index.tsx
+++ b/src/app/index.tsx
@@ -24,6 +24,7 @@ export function App() {
<Editor
state={state}
onOpen={controls.openFiles}
+ onFailure={workspace.reportFailure}
onDismissFailure={workspace.dismissFailure}
draft={recovery}
/>
diff --git a/src/app/loaders/lut.ts b/src/app/loaders/lut.ts
new file mode 100644
index 0000000..96ced16
--- /dev/null
+++ b/src/app/loaders/lut.ts
@@ -0,0 +1,23 @@
+import { addLut } from "@/app/editor/layers";
+import type { Workspace } from "@/app/workspace";
+import { isCubeFile, readCubeFile } from "@/features/lut/cube";
+import type { FileLoader } from "./registry";
+
+/** A `.cube` file adds a LUT layer on top of the stack; one that can't be read shows beside the document. */
+export function createLutLoader(workspace: Workspace): FileLoader {
+ return {
+ kind: "settings",
+ accepts: isCubeFile,
+ async load(file) {
+ const document = workspace.getDocument();
+ try {
+ const table = await readCubeFile(file);
+ if (workspace.state.getState().document === document) {
+ addLut(document, file, table);
+ }
+ } catch (error) {
+ workspace.reportFailure(file.name, error);
+ }
+ },
+ };
+}
diff --git a/src/app/scene-file.ts b/src/app/scene-file.ts
index a5fc060..64897d4 100644
--- a/src/app/scene-file.ts
+++ b/src/app/scene-file.ts
@@ -3,6 +3,7 @@ import {
createDocument,
createResources,
type EditorDocument,
+ layerLuts,
type ProcessingLayer,
type Scene,
walkLayers,
@@ -24,6 +25,7 @@ import {
maskOperation,
maskSchema,
} from "@/features/layers/model";
+import { readCubeFile } from "@/features/lut/cube";
import { curveSchema } from "@/features/tone-curves/curve";
import { defaultVignette, vignetteSchema } from "@/features/vignette/model";
import { whiteBalanceSchema } from "@/features/white-balance/edits";
@@ -32,7 +34,7 @@ import { parse, withDefaults } from "@/lib/parse";
/** Raised only when older files can no longer load as written; a parameter added later takes its default. */
const version = 1;
-/** A saved scene: the scene as edited and the name and type of each source file, stored beside it by ID. */
+/** A saved scene: the scene as edited and the name and type of each source file, its image and LUTs, stored beside it by ID. */
export type SceneJson = {
format: "openlight";
version: number;
@@ -44,14 +46,25 @@ export type SceneJson = {
export function snapshotScene(document: EditorDocument) {
const scene = document.scene.getState();
const { source } = scene.layers[0];
- const { file } = document.resources.get(source);
+ const files = new Map([
+ [source, document.resources.get(source).file],
+ ...Array.from(
+ layerLuts(scene.layers),
+ (id) => [id, document.resources.getLut(id).file] as const,
+ ),
+ ]);
const json: SceneJson = {
format: "openlight",
version,
- sources: { [source]: { name: file.name, type: file.type } },
+ sources: Object.fromEntries(
+ Array.from(files, ([id, file]) => [
+ id,
+ { name: file.name, type: file.type },
+ ]),
+ ),
scene,
};
- return { json, files: new Map([[source, file]]) };
+ return { json, files };
}
const id = z.string().check(z.minLength(1));
@@ -93,6 +106,7 @@ function processingLayer(children: z.ZodMiniType<readonly ProcessingLayer[]>) {
kind: z.literal("fill"),
fill: withDefaults(defaultFill, fillSchema),
}),
+ z.object({ ...base, kind: z.literal("lut"), lut: id }),
z.object({
...base,
kind: z.literal("heal"),
@@ -187,13 +201,22 @@ export async function openScene(
}
const { sources, scene } = parse(savedSchema, saved, "Invalid scene");
const [image, ...layers] = scene.layers;
- const source = sources[image.source];
- const data = files.get(image.source);
- if (!source || !data) {
- throw Error("The scene's image is missing.");
+ function sourceFile(id: string, missing: string) {
+ const source = sources[id];
+ const data = files.get(id);
+ if (!source || !data) {
+ throw Error(missing);
+ }
+ return new File([data], source.name, { type: source.type });
}
- const sourceFile = new File([data], source.name, { type: source.type });
- const decoded = await decode(sourceFile);
+ const imageFile = sourceFile(image.source, "The scene's image is missing.");
+ const luts = await Promise.all(
+ Array.from(layerLuts(scene.layers), async (id) => {
+ const file = sourceFile(id, "A LUT in this scene is missing.");
+ return { id, file, table: await readCubeFile(file) };
+ }),
+ );
+ const decoded = await decode(imageFile);
const asShot = decoded.raw?.asShot;
// Absolute white balance applies only to RAW images, which fall back to their as-shot balance.
const whiteBalance =
@@ -206,7 +229,10 @@ export async function openScene(
: asShot;
const resources = createResources();
try {
- resources.add(sourceFile, decoded, image.source);
+ resources.add(imageFile, decoded, image.source);
+ for (const { id, file, table } of luts) {
+ resources.addLut(file, table, id);
+ }
return createDocument(
{
frame: scene.frame,
diff --git a/src/app/workspace/index.ts b/src/app/workspace/index.ts
index 3dbfd41..882c085 100644
--- a/src/app/workspace/index.ts
+++ b/src/app/workspace/index.ts
@@ -89,6 +89,16 @@ export function createWorkspace() {
return false;
}
},
+ /** Shows why a file could not be applied to the open document, as a failed open does. */
+ reportFailure(file: string, error: unknown) {
+ const current = state.getState();
+ if (current.status === "ready") {
+ state.setState(
+ { ...current, failure: { file, error: String(error) } },
+ true,
+ );
+ }
+ },
/** Clears a failure shown beside the open document. */
dismissFailure() {
const current = state.getState();
diff --git a/src/components/editor/pipeline.tsx b/src/components/editor/pipeline.tsx
index fe69fe0..f7c434e 100644
--- a/src/components/editor/pipeline.tsx
+++ b/src/components/editor/pipeline.tsx
@@ -10,6 +10,7 @@ import type { Gpu } from "vgpu";
import { useGpu } from "vgpu-react";
import { adjustmentTarget } from "@/core/document";
import type { ImageSource } from "@/core/image";
+import type { LookupTable } from "@/core/image/lut";
import type { createRenderer } from "@/core/renderer";
import { useDisposable } from "@/hooks/use-disposable";
import { useDocument, useScene } from "./session";
@@ -39,6 +40,7 @@ type RendererProviderProps = {
createRenderer: (
gpu: Gpu,
source: ImageSource,
+ luts: (id: string) => LookupTable,
) => ReturnType<typeof createRenderer>;
};
@@ -51,8 +53,8 @@ export function RendererProvider({
const sourceId = useScene((scene) => scene.layers[0].source);
const source = document.resources.get(sourceId);
const renderer = useDisposable(
- () => createRenderer(gpu, source),
- [gpu, source, createRenderer],
+ () => createRenderer(gpu, source, document.resources.getLut),
+ [gpu, source, document, createRenderer],
);
const [error, setError] = useState<string>();
diff --git a/src/components/icons/lut.tsx b/src/components/icons/lut.tsx
new file mode 100644
index 0000000..ad742d7
--- /dev/null
+++ b/src/components/icons/lut.tsx
@@ -0,0 +1,10 @@
+import { Icon, type IconProps } from "./icon";
+
+export function LutIcon(props: IconProps) {
+ return (
+ <Icon {...props}>
+ <path d="m12 3 8 4.5v9L12 21l-8-4.5v-9L12 3Z" />
+ <path d="m4 7.5 8 4.5 8-4.5M12 12v9" />
+ </Icon>
+ );
+}
diff --git a/src/core/document/index.ts b/src/core/document/index.ts
index aa64637..ee83fe7 100644
--- a/src/core/document/index.ts
+++ b/src/core/document/index.ts
@@ -3,7 +3,7 @@ import { validateFrame } from "@/core/image/frame";
import { createHistory } from "./history";
import { createResources } from "./resources";
import type { Mask, MaskModifier, Scene } from "./scene";
-import { findLayer } from "./tree";
+import { findLayer, layerLuts } from "./tree";
export type {
Adjustments,
@@ -35,6 +35,7 @@ export {
adjustmentTarget,
editLayer,
findLayer,
+ layerLuts,
locateLayer,
maskModifiers,
updateLayer,
@@ -92,7 +93,12 @@ export function createDocument(initial: Scene, resources = createResources()) {
100,
(retained) => {
resources.retain(
- new Set(retained.map((state) => state.layers[0].source)),
+ new Set(
+ retained.flatMap((state) => [
+ state.layers[0].source,
+ ...layerLuts(state.layers),
+ ]),
+ ),
);
},
);
diff --git a/src/core/document/resources.ts b/src/core/document/resources.ts
index 1dcb505..c9d7262 100644
--- a/src/core/document/resources.ts
+++ b/src/core/document/resources.ts
@@ -1,18 +1,23 @@
import type { ImageSource } from "@/core/image";
+import type { LookupTable } from "@/core/image/lut";
-/** Owns the document's image files and GPU targets, outside scene history. */
+/** Owns the document's files, with each image's GPU target and each LUT's table, outside scene history. */
export function createResources() {
const images = new Map<string, { file: File } & ImageSource>();
+ const luts = new Map<string, { file: File } & LookupTable>();
let disposed = false;
+ function checkNew(id: string) {
+ if (disposed) {
+ throw new Error("Document is closed.");
+ }
+ if (images.has(id) || luts.has(id)) {
+ throw new Error("Resource already exists.");
+ }
+ }
return {
/** A saved document restores each source under its original ID. */
add(file: File, source: ImageSource, id: string = crypto.randomUUID()) {
- if (disposed) {
- throw new Error("Document is closed.");
- }
- if (images.has(id)) {
- throw new Error("Image resource already exists.");
- }
+ checkNew(id);
images.set(id, { file, ...source });
return id;
},
@@ -23,6 +28,18 @@ export function createResources() {
}
return resource;
},
+ addLut(file: File, lut: LookupTable, id: string = crypto.randomUUID()) {
+ checkNew(id);
+ luts.set(id, { file, ...lut });
+ return id;
+ },
+ getLut(id: string) {
+ const resource = luts.get(id);
+ if (!resource) {
+ throw new Error("LUT is unavailable.");
+ }
+ return resource;
+ },
retain(ids: ReadonlySet<string>) {
for (const [id, source] of images) {
if (!ids.has(id)) {
@@ -30,7 +47,13 @@ export function createResources() {
images.delete(id);
}
}
+ for (const id of luts.keys()) {
+ if (!ids.has(id)) {
+ luts.delete(id);
+ }
+ }
},
+ /** Tables hold no GPU memory, so they stay readable to an export still rendering the closed document. */
dispose() {
disposed = true;
for (const source of images.values()) {
diff --git a/src/core/document/scene.ts b/src/core/document/scene.ts
index 521f3f0..f2d3d2b 100644
--- a/src/core/document/scene.ts
+++ b/src/core/document/scene.ts
@@ -122,6 +122,8 @@ export type ProcessingLayer = {
| { readonly kind: "grain"; readonly grain: Grain }
| { readonly kind: "color-mixer"; readonly colorMixer: ColorMixer }
| { readonly kind: "fill"; readonly fill: Fill }
+ /** A 3D LUT from the document's resources, by ID. */
+ | { readonly kind: "lut"; readonly lut: string }
| { readonly kind: "heal"; readonly patches: readonly HealPatch[] }
| {
readonly kind: "mask";
diff --git a/src/core/document/tree.ts b/src/core/document/tree.ts
index 90c69d5..6e904d5 100644
--- a/src/core/document/tree.ts
+++ b/src/core/document/tree.ts
@@ -18,6 +18,17 @@ export function* walkLayers(
}
}
+/** The IDs of the LUTs these layers use, each once. */
+export function layerLuts(layers: readonly Layer[]) {
+ const ids = new Set<string>();
+ for (const { layer } of walkLayers(layers)) {
+ if (layer.kind === "lut") {
+ ids.add(layer.lut);
+ }
+ }
+ return ids;
+}
+
export function locateLayer(layers: readonly Layer[], id: string) {
for (const location of walkLayers(layers)) {
if (location.layer.id === id) {
diff --git a/src/core/image/lut.ts b/src/core/image/lut.ts
new file mode 100644
index 0000000..3d14c49
--- /dev/null
+++ b/src/core/image/lut.ts
@@ -0,0 +1,11 @@
+type Rgb = readonly [number, number, number];
+
+/** A 3D color lookup table: `size`³ output colors for inputs spread evenly across `domain`. */
+export type LookupTable = {
+ /** Its TITLE, or the name of the file it came from. */
+ readonly name: string;
+ readonly size: number;
+ readonly domain: readonly [min: Rgb, max: Rgb];
+ /** RGB triplets, with the red input varying fastest, then green, then blue. */
+ readonly table: Float32Array<ArrayBuffer>;
+};
diff --git a/src/core/renderer/graph.ts b/src/core/renderer/graph.ts
index e664b01..f4559df 100644
--- a/src/core/renderer/graph.ts
+++ b/src/core/renderer/graph.ts
@@ -14,7 +14,8 @@ import type { RenderImage, RenderNode } from "./node";
type Pass = {
shader: RenderNode["shader"];
effect: Effect;
- buffers: Map<string, Buffer>;
+ /** Each storage buffer with the array it holds, so the same array is not uploaded again. */
+ buffers: Map<string, { buffer: Buffer; data: Float32Array }>;
};
function plan(outputs: readonly RenderImage[]) {
@@ -83,17 +84,21 @@ export function createRenderGraph(gpu: Gpu, timer?: Timer) {
throw Error(`Render node ${node.name} changed shader; use a new name.`);
}
for (const [name, data] of Object.entries(node.storage ?? {})) {
- let buffer = pass.buffers.get(name);
+ const stored = pass.buffers.get(name);
+ if (stored?.data === data) {
+ continue;
+ }
+ let buffer = stored?.buffer;
if (!buffer || buffer.options.size !== data.byteLength) {
buffer?.dispose();
buffer = gpu.device.createBuffer({
size: data.byteLength,
usage: ["storage", "copy_dst"],
});
- pass.buffers.set(name, buffer);
pass.effect.set({ [name]: buffer });
}
buffer.write(data);
+ pass.buffers.set(name, { buffer, data });
}
return pass.effect;
}
@@ -117,7 +122,7 @@ export function createRenderGraph(gpu: Gpu, timer?: Timer) {
release(prefix: string) {
for (const [name, pass] of effects) {
if (name.startsWith(prefix)) {
- for (const buffer of pass.buffers.values()) {
+ for (const { buffer } of pass.buffers.values()) {
buffer.dispose();
}
effects.delete(name);
@@ -221,7 +226,7 @@ export function createRenderGraph(gpu: Gpu, timer?: Timer) {
dispose() {
disposed = true;
for (const pass of effects.values()) {
- for (const buffer of pass.buffers.values()) {
+ for (const { buffer } of pass.buffers.values()) {
buffer.dispose();
}
}
diff --git a/src/core/renderer/node.ts b/src/core/renderer/node.ts
index 7e9114d..ffc28d9 100644
--- a/src/core/renderer/node.ts
+++ b/src/core/renderer/node.ts
@@ -5,6 +5,7 @@ type NodeOptions = {
readonly instance?: string;
readonly set?: EffectOptions["set"];
readonly samplers?: Readonly<Record<string, GPUSamplerDescriptor>>;
+ /** Uploaded when the array changes: pass the same array to keep the buffer as it is. */
readonly storage?: Readonly<Record<string, Float32Array<ArrayBuffer>>>;
readonly size?: readonly [number, number];
readonly format?: GPUTextureFormat;
diff --git a/src/features/lut/controls.tsx b/src/features/lut/controls.tsx
new file mode 100644
index 0000000..df52afd
--- /dev/null
+++ b/src/features/lut/controls.tsx
@@ -0,0 +1,40 @@
+import { Button } from "@roprgm/ui/button";
+import { ScrollText } from "@roprgm/ui/scroll-text";
+import { useRef } from "react";
+import { useDocument } from "@/components/editor/session";
+import { readCubeFile } from "./cube";
+import { setLut } from "./edits";
+import { LutInput } from "./input";
+
+/** The LUT's name and a way to replace its file; the layer's opacity sets its strength. */
+export function LutControls({
+ id,
+ lut,
+ onFailure,
+}: {
+ id: string;
+ lut: string;
+ onFailure: (file: string, error: unknown) => void;
+}) {
+ const document = useDocument();
+ const input = useRef<HTMLInputElement>(null);
+ async function replace(file: File) {
+ try {
+ const table = await readCubeFile(file);
+ setLut(document, id, document.resources.addLut(file, table));
+ } catch (error) {
+ onFailure(file.name, error);
+ }
+ }
+ return (
+ <section className="flex items-center gap-3 p-3.5">
+ <ScrollText className="min-w-0 flex-1 text-foreground">
+ {document.resources.getLut(lut).name}
+ </ScrollText>
+ <Button size="sm" onClick={() => input.current?.click()}>
+ Replace…
+ </Button>
+ <LutInput ref={input} onChoose={replace} />
+ </section>
+ );
+}
diff --git a/src/features/lut/cube.ts b/src/features/lut/cube.ts
new file mode 100644
index 0000000..d5ff607
--- /dev/null
+++ b/src/features/lut/cube.ts
@@ -0,0 +1,111 @@
+import type { LookupTable } from "@/core/image/lut";
+
+type Rgb = [number, number, number];
+
+/** Editors write sizes up to 65; two points already span the domain. */
+const maxSize = 65;
+
+export function isCubeFile(file: File) {
+ return /\.cube$/i.test(file.name);
+}
+
+function numbers(tokens: readonly string[], count: number, at: string) {
+ const values = tokens.map(Number);
+ if (values.length !== count || !values.every(Number.isFinite)) {
+ throw Error(
+ `${at}: expected ${count > 1 ? `${count} numbers` : "a number"}.`,
+ );
+ }
+ return values;
+}
+
+function rgb(tokens: readonly string[], at: string): Rgb {
+ const [r, g, b] = numbers(tokens, 3, at);
+ return [r, g, b];
+}
+
+/**
+ * Reads an Adobe or Resolve `.cube` 3D LUT: keywords, then `LUT_3D_SIZE`³ rows of RGB with red
+ * varying fastest. Other keywords are skipped; `name` stands in for a missing TITLE.
+ */
+export function readCube(text: string, name: string): LookupTable {
+ let title = name;
+ let min: Rgb = [0, 0, 0];
+ let max: Rgb = [1, 1, 1];
+ let size = 0;
+ let table: Float32Array<ArrayBuffer> | undefined;
+ let filled = 0;
+ const lines = text.replace(/^\uFEFF/, "").split(/\r?\n|\r/);
+ for (const [index, line] of lines.entries()) {
+ const [keyword, ...tokens] = line.trim().split(/\s+/);
+ const at = `Line ${index + 1}`;
+ if (!keyword || keyword.startsWith("#")) {
+ continue;
+ }
+ switch (keyword) {
+ case "TITLE":
+ title =
+ line
+ .trim()
+ .slice(keyword.length)
+ .trim()
+ .replace(/^"(.*)"$/, "$1") || name;
+ break;
+ case "LUT_1D_SIZE":
+ throw Error("This is a 1D LUT; choose a 3D LUT.");
+ case "LUT_3D_SIZE":
+ if (table) {
+ throw Error(`${at}: LUT_3D_SIZE appears twice.`);
+ }
+ [size] = numbers(tokens, 1, at);
+ if (!Number.isInteger(size) || size < 2 || size > maxSize) {
+ throw Error(
+ `${at}: LUT_3D_SIZE must be a whole number from 2 to 65.`,
+ );
+ }
+ table = new Float32Array(3 * size ** 3);
+ break;
+ case "DOMAIN_MIN":
+ min = rgb(tokens, at);
+ break;
+ case "DOMAIN_MAX":
+ max = rgb(tokens, at);
+ break;
+ case "LUT_3D_INPUT_RANGE": {
+ const [low, high] = numbers(tokens, 2, at);
+ min = [low, low, low];
+ max = [high, high, high];
+ break;
+ }
+ default:
+ if (/^[a-z_]/i.test(keyword)) {
+ break;
+ }
+ if (!table) {
+ throw Error(`${at}: the table starts before LUT_3D_SIZE.`);
+ }
+ if (filled === table.length) {
+ throw Error(`${at}: LUT_3D_SIZE ${size} needs ${size ** 3} rows.`);
+ }
+ table.set(rgb([keyword, ...tokens], at), filled);
+ filled += 3;
+ }
+ }
+ if (!table) {
+ throw Error("LUT_3D_SIZE is missing, so this isn't a 3D LUT.");
+ }
+ if (filled < table.length) {
+ throw Error(
+ `LUT_3D_SIZE ${size} needs ${size ** 3} rows; found ${filled / 3}.`,
+ );
+ }
+ if (min.some((low, channel) => low >= max[channel])) {
+ throw Error("DOMAIN_MIN must be below DOMAIN_MAX in every channel.");
+ }
+ return { name: title, size, domain: [min, max], table };
+}
+
+/** Reads a `.cube` file, named after its TITLE or else the file. */
+export async function readCubeFile(file: File) {
+ return readCube(await file.text(), file.name.replace(/\.[^.]*$/, ""));
+}
diff --git a/src/features/lut/edits.ts b/src/features/lut/edits.ts
new file mode 100644
index 0000000..0522aed
--- /dev/null
+++ b/src/features/lut/edits.ts
@@ -0,0 +1,13 @@
+import { type EditorDocument, editLayer } from "@/core/document";
+
+/** Points a LUT layer at another table; a layer still named after its old table takes the new name. */
+export function setLut(document: EditorDocument, id: string, lut: string) {
+ const { name } = document.resources.getLut(lut);
+ editLayer(document, id, (layer) => {
+ if (layer.kind !== "lut") {
+ throw Error("Select a LUT layer.");
+ }
+ const named = layer.name === document.resources.getLut(layer.lut).name;
+ return { ...layer, lut, name: named ? name : layer.name };
+ });
+}
diff --git a/src/features/lut/input.tsx b/src/features/lut/input.tsx
new file mode 100644
index 0000000..57febb9
--- /dev/null
+++ b/src/features/lut/input.tsx
@@ -0,0 +1,26 @@
+import type { RefObject } from "react";
+
+/** The hidden `.cube` picker; clearing it after each pick lets the same file be chosen again. */
+export function LutInput({
+ ref,
+ onChoose,
+}: {
+ ref: RefObject<HTMLInputElement | null>;
+ onChoose: (file: File) => void;
+}) {
+ return (
+ <input
+ ref={ref}
+ type="file"
+ accept=".cube"
+ hidden
+ onChange={(event) => {
+ const file = event.currentTarget.files?.[0];
+ if (file) {
+ onChoose(file);
+ }
+ event.currentTarget.value = "";
+ }}
+ />
+ );
+}
diff --git a/src/features/lut/lut.wgsl b/src/features/lut/lut.wgsl
new file mode 100644
index 0000000..cc651a9
--- /dev/null
+++ b/src/features/lut/lut.wgsl
@@ -0,0 +1,57 @@
+import { srgbToLinear3 } from "@vgpu/wgsl-std/color";
+import { display, srgbToRec2020 } from "../../core/image/color.wgsl";
+
+// Creative LUTs take and return display-referred sRGB. The LUT sees the working color as a screen
+// shows it, clipped to sRGB with its hue and luminance kept, so headroom above white clips here;
+// what it returns is clamped to the same range.
+struct Params {
+ size: u32,
+ domainMin: vec3f,
+ domainMax: vec3f,
+}
+@group(0) @binding(0) var source: texture_2d<f32>;
+@group(0) @binding(1) var<uniform> params: Params;
+@group(0) @binding(2) var<storage, read> table: array<f32>;
+
+fn entry(point: vec3u) -> vec3f {
+ let index = 3u * (point.r + params.size * (point.g + params.size * point.b));
+ return vec3f(table[index], table[index + 1u], table[index + 2u]);
+}
+
+// Tetrahedral interpolation: the lattice cell splits along its gray diagonal into six tetrahedra, so
+// a LUT that keeps grays neutral keeps them neutral between its points too.
+fn lookup(color: vec3f) -> vec3f {
+ let range = params.domainMax - params.domainMin;
+ let last = f32(params.size - 1u);
+ let position = clamp((color - params.domainMin) / range, vec3f(0.0), vec3f(1.0)) * last;
+ let cell = min(floor(position), vec3f(last - 1.0));
+ let f = position - cell;
+ let base = vec3u(cell);
+ // The corners between the cell's darkest and brightest, taken along the largest fractions first.
+ var first: vec3u;
+ var second: vec3u;
+ var sorted: vec3f;
+ if (f.r >= f.g && f.g >= f.b) {
+ first = vec3u(1u, 0u, 0u); second = vec3u(1u, 1u, 0u); sorted = f.rgb;
+ } else if (f.r >= f.g && f.r >= f.b) {
+ first = vec3u(1u, 0u, 0u); second = vec3u(1u, 0u, 1u); sorted = f.rbg;
+ } else if (f.r >= f.g) {
+ first = vec3u(0u, 0u, 1u); second = vec3u(1u, 0u, 1u); sorted = f.brg;
+ } else if (f.g >= f.b && f.r >= f.b) {
+ first = vec3u(0u, 1u, 0u); second = vec3u(1u, 1u, 0u); sorted = f.grb;
+ } else if (f.g >= f.b) {
+ first = vec3u(0u, 1u, 0u); second = vec3u(0u, 1u, 1u); sorted = f.gbr;
+ } else {
+ first = vec3u(0u, 0u, 1u); second = vec3u(0u, 1u, 1u); sorted = f.bgr;
+ }
+ return (1.0 - sorted.x) * entry(base)
+ + (sorted.x - sorted.y) * entry(base + first)
+ + (sorted.y - sorted.z) * entry(base + second)
+ + sorted.z * entry(base + 1u);
+}
+
+@fragment fn fs_main(@builtin(position) position: vec4f) -> @location(0) vec4f {
+ let input = textureLoad(source, vec2i(position.xy), 0);
+ let graded = clamp(lookup(display(input.rgb)), vec3f(0.0), vec3f(1.0));
+ return vec4f(srgbToRec2020 * srgbToLinear3(graded), input.a);
+}
diff --git a/src/features/lut/pass.ts b/src/features/lut/pass.ts
new file mode 100644
index 0000000..00f1037
--- /dev/null
+++ b/src/features/lut/pass.ts
@@ -0,0 +1,16 @@
+import type { LookupTable } from "@/core/image/lut";
+import { node } from "@/core/renderer";
+import shader from "./lut.wgsl";
+
+export function lut(table: LookupTable, name = "lut") {
+ return node(name, shader, {
+ set: {
+ params: {
+ size: table.size,
+ domainMin: table.domain[0],
+ domainMax: table.domain[1],
+ },
+ },
+ storage: { table: table.table },
+ });
+}
diff --git a/tests/assistant.e2e.ts b/tests/assistant.e2e.ts
index ed489c3..cbcf042 100644
--- a/tests/assistant.e2e.ts
+++ b/tests/assistant.e2e.ts
@@ -81,9 +81,9 @@ test("the assistant shows only at /?assistant, opens, runs the commands a messag
// An answer that arrives after another photo opens leaves the new photo alone.
await message.fill("brighter");
await message.press("Enter");
- await page
- .locator('input[type="file"]')
- .setInputFiles("tests/fixtures/tones.png");
+ const chooser = page.waitForEvent("filechooser");
+ await page.getByRole("button", { name: "Open an image or scene" }).click();
+ await (await chooser).setFiles("tests/fixtures/tones.png");
await expect
.poll(() => page.evaluate(() => window.openlight.getState().file))
.toBe("tones.png");
diff --git a/tests/fixtures/swap.cube b/tests/fixtures/swap.cube
new file mode 100644
index 0000000..d0903d3
--- /dev/null
+++ b/tests/fixtures/swap.cube
@@ -0,0 +1,12 @@
+# Swaps the red and blue channels.
+TITLE "Swap red and blue"
+LUT_3D_SIZE 2
+
+0 0 0
+0 0 1
+0 1 0
+0 1 1
+1 0 0
+1 0 1
+1 1 0
+1 1 1
diff --git a/tests/lut.e2e.ts b/tests/lut.e2e.ts
new file mode 100644
index 0000000..38374b9
--- /dev/null
+++ b/tests/lut.e2e.ts
@@ -0,0 +1,120 @@
+import { expect, test } from "./fixtures";
+import { readImage } from "./images";
+
+/** A two-point LUT that returns its input, rows listed with red varying fastest. */
+const identity = [
+ 'TITLE "Identity"',
+ "LUT_3D_SIZE 2",
+ ...Array.from({ length: 8 }, (_, i) => `${i & 1} ${(i >> 1) & 1} ${i >> 2}`),
+].join("\n");
+
+test("a LUT layer grades the photo from a .cube file at the layer's opacity, replaces its file, undoes, and saves with the scene", async ({
+ page,
+}) => {
+ test.setTimeout(90_000);
+ await page.setViewportSize({ width: 1440, height: 1000 });
+ await page.goto("/");
+ await page
+ .locator('input[type="file"]')
+ .setInputFiles("tests/fixtures/photo.svg");
+ const exposure = page.getByRole("textbox", { name: "Exposure", exact: true });
+ await expect(exposure).toHaveValue("0.00");
+ // The blue patch, #305080, and the gray middle.
+ const points = [
+ [350, 200],
+ [600, 400],
+ ] as const;
+ const sample = async () => (await readImage(page, undefined, points)).samples;
+ const near = (actual: number[] | undefined, expected: number[]) => {
+ for (const [channel, value] of expected.entries()) {
+ expect(Math.abs((actual?.[channel] ?? 0) - value)).toBeLessThanOrEqual(1);
+ }
+ };
+ const original = await sample();
+ expect(original).toEqual([
+ [48, 80, 128, 255],
+ [128, 128, 128, 255],
+ ]);
+ const layers = page.getByRole("region", { name: "Layers", exact: true });
+
+ await test.step("the Add menu asks for a .cube file and grades the photo with it", async () => {
+ const chooser = page.waitForEvent("filechooser");
+ await page.getByRole("button", { name: "Add effect", exact: true }).click();
+ await page.getByRole("menuitem", { name: "LUT", exact: true }).click();
+ await (await chooser).setFiles("tests/fixtures/swap.cube");
+ await expect(layers.getByText("Swap red and blue")).toBeVisible();
+ await expect(
+ page.getByRole("button", { name: "Replace…", exact: true }),
+ ).toBeVisible();
+ const [patch, gray] = (await sample()) ?? [];
+ near(patch, [128, 80, 48, 255]);
+ near(gray, [128, 128, 128, 255]);
+ });
+ const swapped = await sample();
+
+ await test.step("the layer's opacity sets its strength", async () => {
+ const opacity = page.getByRole("textbox", { name: "Opacity", exact: true });
+ await opacity.fill("50");
+ await opacity.press("Enter");
+ const [patch] = (await sample()) ?? [];
+ expect(patch?.[0]).toBeGreaterThan(48);
+ expect(patch?.[0]).toBeLessThan(128);
+ expect(patch?.[2]).toBeGreaterThan(48);
+ expect(patch?.[2]).toBeLessThan(128);
+ });
+ const half = await sample();
+
+ await test.step("replacing the file with an identity LUT leaves the photo as it was", async () => {
+ const chooser = page.waitForEvent("filechooser");
+ await page.getByRole("button", { name: "Replace…", exact: true }).click();
+ await (await chooser).setFiles({
+ name: "identity.cube",
+ mimeType: "",
+ buffer: Buffer.from(identity),
+ });
+ await expect(layers.getByText("Identity")).toBeVisible();
+ const [patch, gray] = (await sample()) ?? [];
+ near(patch, original?.[0] ?? []);
+ near(gray, original?.[1] ?? []);
+ });
+
+ await test.step("undo steps back through the replacement, the opacity, and the layer", async () => {
+ await page.keyboard.press("ControlOrMeta+z");
+ expect(await sample()).toEqual(half);
+ await page.keyboard.press("ControlOrMeta+z");
+ expect(await sample()).toEqual(swapped);
+ await page.keyboard.press("ControlOrMeta+z");
+ expect(await sample()).toEqual(original);
+ await page.keyboard.press("ControlOrMeta+Shift+z");
+ await page.keyboard.press("ControlOrMeta+Shift+z");
+ expect(await sample()).toEqual(half);
+ });
+
+ await test.step("a scene file carries the LUT", async () => {
+ const reopened = await page.evaluate(async () => {
+ const api = window.openlight;
+ const before = api.getState().documentId;
+ await api.loadScene(await api.exportScene());
+ return before !== api.getState().documentId;
+ });
+ expect(reopened).toBe(true);
+ await expect(layers.getByText("Swap red and blue")).toBeVisible();
+ expect(await sample()).toEqual(half);
+ });
+
+ await test.step("a file that isn't a 3D LUT says why and changes nothing", async () => {
+ const before = await page.evaluate(() => window.openlight.getState());
+ await page.evaluate(() =>
+ window.openlight.openFile(
+ new File(["LUT_1D_SIZE 2\n0 0 0\n1 1 1\n"], "curve.cube"),
+ ),
+ );
+ await expect(
+ page.getByText("Couldn't open curve.cube: Error: This is a 1D LUT"),
+ ).toBeVisible();
+ const after = await page.evaluate(() => window.openlight.getState());
+ expect(after.failure?.file).toBe("curve.cube");
+ expect(after.scene).toEqual(before.scene);
+ await page.getByRole("button", { name: "Dismiss", exact: true }).click();
+ });
+});
diff --git a/tests/lut.test.ts b/tests/lut.test.ts
new file mode 100644
index 0000000..a4561a8
--- /dev/null
+++ b/tests/lut.test.ts
@@ -0,0 +1,153 @@
+import { expect, test } from "bun:test";
+import { init, target } from "vgpu/mock";
+import { addLut, createImageLayer, createMask } from "@/app/editor/layers";
+import { createEditorRenderer } from "@/app/editor/renderer";
+import { createDocument, createResources, findLayer } from "@/core/document";
+import { createImageSource } from "@/core/image";
+import { imageFrame } from "@/core/image/frame";
+import { setAdjustments } from "@/features/adjustments/edits";
+import { addLayer, setLayer } from "@/features/layers/edits";
+import { defaultGradient } from "@/features/layers/gradient";
+import { readCube, readCubeFile } from "@/features/lut/cube";
+import { setLut } from "@/features/lut/edits";
+
+/** A two-point LUT that swaps red and blue, rows listed with red varying fastest. */
+const swap = [
+ "0 0 0",
+ "0 0 1",
+ "0 1 0",
+ "0 1 1",
+ "1 0 0",
+ "1 0 1",
+ "1 1 0",
+ "1 1 1",
+];
+
+test("a .cube file reads into a 3D table with its title, domain, and row order, and a broken one says why", async () => {
+ const lut = readCube(
+ [
+ "\uFEFF# Created by hand",
+ 'TITLE "Red and blue swapped"',
+ "",
+ "LUT_3D_SIZE 2",
+ "DOMAIN_MIN 0 0 0",
+ "DOMAIN_MAX 1 1 2",
+ "LUT_IN_VIDEO_RANGE",
+ " # the table",
+ ...swap.map((row) => `${row} `),
+ ].join("\r\n"),
+ "swap",
+ );
+ expect(lut).toMatchObject({
+ name: "Red and blue swapped",
+ size: 2,
+ domain: [
+ [0, 0, 0],
+ [1, 1, 2],
+ ],
+ });
+ // Red varies fastest: the second row answers pure red, the fifth pure blue.
+ expect([...lut.table.subarray(3, 6)]).toEqual([0, 0, 1]);
+ expect([...lut.table.subarray(12, 15)]).toEqual([1, 0, 0]);
+ const untitled = await readCubeFile(
+ new File(
+ [["LUT_3D_SIZE 2", "LUT_3D_INPUT_RANGE -0.5 1.5", ...swap].join("\n")],
+ "Warm film.cube",
+ ),
+ );
+ expect(untitled).toMatchObject({
+ name: "Warm film",
+ domain: [
+ [-0.5, -0.5, -0.5],
+ [1.5, 1.5, 1.5],
+ ],
+ });
+ const broken: [string[], string][] = [
+ [swap, "Line 1: the table starts before LUT_3D_SIZE."],
+ [["TITLE x"], "LUT_3D_SIZE is missing"],
+ [["LUT_1D_SIZE 2", "0 0 0", "1 1 1"], "This is a 1D LUT"],
+ [["LUT_3D_SIZE 1"], "Line 1: LUT_3D_SIZE must be a whole number from 2"],
+ [["LUT_3D_SIZE 66"], "from 2 to 65"],
+ [["LUT_3D_SIZE 2.5"], "from 2 to 65"],
+ [["LUT_3D_SIZE two"], "Line 1: expected a number."],
+ [["LUT_3D_SIZE 2", "LUT_3D_SIZE 2"], "Line 2: LUT_3D_SIZE appears twice."],
+ [["LUT_3D_SIZE 2", ...swap.slice(1)], "needs 8 rows; found 7."],
+ [["LUT_3D_SIZE 2", ...swap, "1 1 1"], "Line 10: LUT_3D_SIZE 2 needs 8"],
+ [["LUT_3D_SIZE 2", "0 0", ...swap.slice(1)], "Line 2: expected 3 numbers."],
+ [["LUT_3D_SIZE 2", "0 0 NaN", ...swap.slice(1)], "expected 3 numbers."],
+ [["DOMAIN_MIN 0 0", "LUT_3D_SIZE 2", ...swap], "Line 1: expected 3"],
+ [["DOMAIN_MAX 1 0 1", "LUT_3D_SIZE 2", ...swap], "below DOMAIN_MAX"],
+ ];
+ for (const [lines, message] of broken) {
+ expect(() => readCube(lines.join("\n"), "broken")).toThrow(message);
+ }
+});
+
+test("a LUT layer renders its table, nests in a mask, follows a replacement's name, and keeps its file only while history uses it", async () => {
+ const gpu = await init();
+ const source = createImageSource(
+ target(gpu, { size: [16, 8], format: "rgba16float" }),
+ );
+ const resources = createResources();
+ const sourceId = resources.add(new File([], "photo.png"), source);
+ const document = createDocument(
+ {
+ frame: imageFrame(source.image.size),
+ layers: [{ ...createImageLayer(sourceId, "Photo"), id: "base" }],
+ },
+ resources,
+ );
+ const renderer = createEditorRenderer(gpu, source, resources.getLut);
+ const read = (lines: string[], name: string) =>
+ readCube(["LUT_3D_SIZE 2", ...lines].join("\n"), name);
+ const swapFile = new File([], "swap.cube");
+ const layer = (id: string) => findLayer(document.scene.getState().layers, id);
+ const lutOf = (id: string) => {
+ const item = layer(id);
+ if (item?.kind !== "lut") throw Error("Missing LUT layer.");
+ return item.lut;
+ };
+ try {
+ const mask = addLayer(document, createMask(defaultGradient([16, 8])));
+ const nested = addLut(document, swapFile, read(swap, "Swap"), {
+ inside: mask,
+ });
+ expect(layer(nested)).toMatchObject({ kind: "lut", name: "Swap" });
+ await renderer.update(document.scene.getState());
+ expect(renderer.inspect().passes).toEqual([
+ `layer/${nested}/lut`,
+ `layer/${mask}/mix`,
+ ]);
+
+ const identity = read(
+ Array.from({ length: 8 }, (_, i) => `${i & 1} ${(i >> 1) & 1} ${i >> 2}`),
+ "Identity",
+ );
+ const replaced = resources.addLut(new File([], "identity.cube"), identity);
+ setLut(document, nested, replaced);
+ expect(layer(nested)).toMatchObject({ lut: replaced, name: "Identity" });
+ setLayer(document, nested, { name: "Neutral" });
+ setLut(document, nested, resources.addLut(swapFile, read(swap, "Swap")));
+ expect(layer(nested)).toMatchObject({ name: "Neutral" });
+ expect(() => setLut(document, mask, replaced)).toThrow("Select a LUT");
+ expect(() => setLut(document, nested, "missing")).toThrow("unavailable");
+
+ // A LUT stays while undo or redo can reach it and goes once a new edit drops that branch.
+ const top = lutOf(addLut(document, swapFile, read(swap, "Top")));
+ document.history.undo();
+ expect(resources.getLut(top).name).toBe("Top");
+ setAdjustments(document, { exposure: 1 });
+ expect(() => resources.getLut(top)).toThrow("unavailable");
+ expect(resources.getLut(replaced).name).toBe("Identity");
+
+ // Adding while a gesture is open commits it first, so the new table is kept.
+ document.history.begin();
+ setAdjustments(document, { exposure: 0.5 });
+ const grouped = lutOf(addLut(document, swapFile, read(swap, "Grouped")));
+ expect(resources.getLut(grouped).name).toBe("Grouped");
+ } finally {
+ renderer.dispose();
+ document.dispose();
+ gpu.dispose();
+ }
+});
diff --git a/tests/render-graph.test.ts b/tests/render-graph.test.ts
index e192bd7..43e07a9 100644
--- a/tests/render-graph.test.ts
+++ b/tests/render-graph.test.ts
@@ -1,4 +1,4 @@
-import { expect, test } from "bun:test";
+import { expect, spyOn, test } from "bun:test";
import { getMockGPUDeviceInstrumentation, init, target } from "vgpu/mock";
import {
createRenderGraph,
@@ -76,9 +76,11 @@ test("a shared branch renders once and reuses effects, buffers, and temporary st
{ source: shared, base: branch },
node("join", shader, options),
);
+ const uploads = spyOn(gpu.gpu.queue, "writeBuffer");
try {
const [saved, output] = graph.render([shared, joined]);
expect(output).not.toBe(saved);
+ expect(uploads).toHaveBeenCalledTimes(4);
expect(graph.inspect().passes).toEqual([
"shared",
"middle",
@@ -97,6 +99,8 @@ test("a shared branch renders once and reuses effects, buffers, and temporary st
]);
expect(calls.createBuffer).toBe(buffers);
expect(calls.createRenderPipeline).toBe(pipelines);
+ // A storage array uploads once while passes keep receiving it.
+ expect(uploads).toHaveBeenCalledTimes(4);
// Bypassing releases scratch storage.
expect(graph.render([shared])[0]).toBe(saved);
expect(graph.inspect().textures).toHaveLength(1);
diff --git a/tests/rendering-benchmark.ts b/tests/rendering-benchmark.ts
index b23b2c3..a367e9c 100644
--- a/tests/rendering-benchmark.ts
+++ b/tests/rendering-benchmark.ts
@@ -11,6 +11,7 @@ import type {
} from "@/core/document";
import { createImageSource } from "@/core/image";
import { imageFrame } from "@/core/image/frame";
+import type { LookupTable } from "@/core/image/lut";
import { defaultAdjustments } from "@/features/adjustments/model";
import { createHistogram } from "@/features/histogram/histogram";
import { defaultCurve } from "@/features/tone-curves/curve";
@@ -28,6 +29,7 @@ export type Workload =
| "brush-exposure"
| "layer-stack"
| "fill"
+ | "lut"
| "heal"
| "heal-empty"
| "heal-proxy"
@@ -79,6 +81,27 @@ function benchmarkMask(workload: Workload, size: [number, number]): Mask {
};
}
+/** A 33-point LUT that warms highlights and cools shadows, as a creative grade does. */
+function benchmarkLut(): LookupTable {
+ const size = 33;
+ const table = new Float32Array(3 * size ** 3);
+ for (let index = 0; index < size ** 3; index++) {
+ const r = (index % size) / (size - 1);
+ const g = (Math.floor(index / size) % size) / (size - 1);
+ const b = Math.floor(index / size ** 2) / (size - 1);
+ table.set([r ** 0.9, g, b ** 1.1], 3 * index);
+ }
+ return {
+ name: "Benchmark",
+ size,
+ domain: [
+ [0, 0, 0],
+ [1, 1, 1],
+ ],
+ table,
+ };
+}
+
/** Repeated processing of a resident texture; decoding, display, and export are outside timings. */
export async function benchmarkRendering(
workload: Workload,
@@ -193,6 +216,15 @@ export async function benchmarkRendering(
fill: { color: "#f0763c", blend: "soft-light" },
});
}
+ if (workload === "lut") {
+ effects.push({
+ ...common,
+ id: "benchmark-lut",
+ name: "LUT",
+ kind: "lut",
+ lut: "benchmark-table",
+ });
+ }
if (
workload === "heal" ||
workload === "heal-empty" ||
@@ -245,8 +277,9 @@ export async function benchmarkRendering(
...effects,
],
};
+ const lut = benchmarkLut();
function create(clock?: Timer) {
- const renderer = createEditorRenderer(gpu, source, clock);
+ const renderer = createEditorRenderer(gpu, source, () => lut, clock);
// Half a device pixel per source pixel, as a fitted view of a large photo, renders at a factor of 2.
renderer.setDisplayScale(proxy ? 0.5 : 1);
return renderer;
diff --git a/tests/rendering.bench.ts b/tests/rendering.bench.ts
index 2396310..03ca910 100644
--- a/tests/rendering.bench.ts
+++ b/tests/rendering.bench.ts
@@ -17,6 +17,7 @@ for (const workload of [
"brush-exposure",
"layer-stack",
"fill",
+ "lut",
"heal",
"heal-empty",
"heal-proxy",
diff --git a/tests/scene-file.test.ts b/tests/scene-file.test.ts
index 3342ec4..5c0cbae 100644
--- a/tests/scene-file.test.ts
+++ b/tests/scene-file.test.ts
@@ -104,6 +104,15 @@ test("scene files reopen the photo with every layer for further editing", async
api.setVignette({ intensity: 70, softness: 20 });
api.setColorMixer("blue", { hue: 20, luminance: -10 });
api.setExposure(api.addLayer("exposure"), -0.5);
+ const cube = [
+ "TITLE Grade",
+ "LUT_3D_SIZE 2",
+ ...Array(8).fill("0.5 0.5 0.5"),
+ ];
+ await api.openFile(new File([cube.join("\n")], "grade.cube"));
+ const lut = api.getState().scene?.layers.at(-1);
+ if (lut?.kind !== "lut") throw Error("Missing LUT layer.");
+ api.duplicateLayer(lut.id);
const edited = workspace.getDocument().scene.getState();
const file = await api.exportScene();
@@ -114,11 +123,18 @@ test("scene files reopen the photo with every layer for further editing", async
expect(json).toMatchObject({
format: "openlight",
version: 1,
- sources: { [source]: { name: "photo.nef", type: "image/x-nikon-nef" } },
+ sources: {
+ [source]: { name: "photo.nef", type: "image/x-nikon-nef" },
+ [lut.lut]: { name: "grade.cube", type: "" },
+ },
scene: edited,
});
+ expect(Object.keys(json.sources)).toHaveLength(2);
const stored = await entries.get(`sources/${source}`)?.bytes();
expect(stored).toEqual(bytes);
+ expect(await entries.get(`sources/${lut.lut}`)?.text()).toBe(
+ cube.join("\n"),
+ );
const opened = await openSceneFile(file, raw.decode);
const reopened = raw.decoded.at(-1);
@@ -126,6 +142,7 @@ test("scene files reopen the photo with every layer for further editing", async
expect(reopened?.type).toBe("image/x-nikon-nef");
expect(await reopened?.bytes()).toEqual(bytes);
expect(opened.scene.getState()).toEqual(edited);
+ expect(opened.resources.getLut(lut.lut).name).toBe("Grade");
expect(opened.history.status.getState().undoCount).toBe(0);
opened.dispose();
@@ -145,6 +162,13 @@ test("scene files reopen the photo with every layer for further editing", async
],
[await archive({ ...json, version: 2 }), "needs a newer version"],
[await archive(json, new Map()), "The scene's image is missing."],
+ [
+ await archive(
+ json,
+ new Map([...entries].filter(([name]) => !name.endsWith(lut.lut))),
+ ),
+ "A LUT in this scene is missing.",
+ ],
[
await archive({
...json,