Skip to content

Free open-source image editor for the browser

Keep HDR headroom through adjustments (#13)

Rodrigo Belfiorec310c472a12e
diff --git a/AGENTS.md b/AGENTS.md
index e41f475..c9ce24a 100644
--- a/AGENTS.md
+++ b/AGENTS.md
@@ -57,6 +57,7 @@ All React bindings come from `vgpu-react`; everything else comes from `vgpu`.
- The working space is linear Rec.2020 in `rgba16float`. Decoders convert into it, the editor shader converts out of it; nothing in between assumes a format or primaries, so both can change (an 8-bit variant is planned).
- `lib/adjustments/adjustments.wgsl` works in that space; its `Adjustments` struct mirrors the TypeScript type in the UI's units, and its constants were fitted against reference exports; treat them as data, not formulas to tidy.
- `lib/decode` converts every format into the working space. Browser-decoded formats go through `linearize`. TIFF uses `lib/tiff-gpu`, which returns a linear Rec.2020 target on its own: `prepareTiff` (directory, color profile, CPU codecs) runs in the worker and `uploadTiff` does the rest on the GPU. Reuse `workerDecoder` for decoders to come (16-bit PNG, RAW).
+- Samples above 1.0 (HDR) keep their headroom through exposure, curves, and vibrance; negatives clip at exposure. Exposure scales all channels by one luminance gain so hue holds, and `display()` in `lib/color.wgsl` maps out-of-gamut colors toward their luminance, so bright saturated light rolls off to white instead of clipping per channel.
## Testing
diff --git a/src/lib/adjustments/prepare.wgsl b/src/lib/adjustments/prepare.wgsl
index 4465c9f..c15dabb 100644
--- a/src/lib/adjustments/prepare.wgsl
+++ b/src/lib/adjustments/prepare.wgsl
@@ -1,3 +1,5 @@
+import { luminance } from "../color.wgsl";
+
export struct Adjustments {
exposure: f32,
incrementalTemperature: f32,
@@ -12,13 +14,25 @@ export struct Adjustments {
}
// Source preparation in linear Rec.2020; retain the calibrated constants.
-fn adjustExposure(color: vec3f, stops: f32) -> vec3f {
- let bounded = clamp(color, vec3f(0.0), vec3f(1.0));
+
+// Fitted exposure of a gray level. Light above 1.0 is headroom and scales linearly with the stops.
+fn exposeGray(light: f32, stops: f32) -> f32 {
+ let bounded = min(light, 1.0);
+ let headroom = (light - bounded) * exp2(stops);
if stops < 0.0 {
- return exp2(stops * 1.09) * pow(bounded, vec3f(exp2(-stops * 0.14)));
+ return headroom + exp2(stops * 1.09) * pow(bounded, exp2(-stops * 0.14));
}
let gain = mix(vec2f(1.11, -0.11) * min(stops, 1.0), vec2f(4.05, -0.63), max(stops - 1.0, 0.0) / 4.0);
- return 1.0 - pow(1.0 - pow(bounded, vec3f(exp2(gain.y))), vec3f(exp2(gain.x)));
+ return headroom + 1.0 - pow(1.0 - pow(bounded, exp2(gain.y)), exp2(gain.x));
+}
+
+// Every channel scales by the gain of the pixel's luminance, so hue holds at any exposure.
+// Negatives, from wide-gamut sources or noise below black, clip here.
+fn adjustExposure(color: vec3f, stops: f32) -> vec3f {
+ let clipped = max(color, vec3f(0.0));
+ let light = luminance(clipped);
+ if light <= 0.0 { return clipped; }
+ return clipped * (exposeGray(light, stops) / light);
}
fn adjustWhiteBalance(color: vec3f, temperature: f32, tint: f32) -> vec3f {
@@ -26,6 +40,7 @@ fn adjustWhiteBalance(color: vec3f, temperature: f32, tint: f32) -> vec3f {
+ (vec3f(1.55, 1.89, 2.93) + vec3f(-1.47, -1.05, -0.69) * temperature) * abs(temperature);
let tintGain = (vec3f(0.53, -0.59, 1.02) + vec3f(0.64, 0.94, 1.35) * tint) * tint;
let gain = warmth + tintGain;
+ // The fitted response covers 0..1; headroom above it passes through unchanged.
let bounded = min(color, vec3f(1.0));
return bounded / (bounded + (1.0 - bounded) * exp2(-gain)) + (color - bounded);
}
diff --git a/src/lib/color.wgsl b/src/lib/color.wgsl
index e64d957..fda36cd 100644
--- a/src/lib/color.wgsl
+++ b/src/lib/color.wgsl
@@ -23,7 +23,30 @@ export const rec2020ToSrgb = mat3x3f(
-0.0728, -0.0083, 1.1187,
);
-/** Working space to what the screen shows: sRGB, clipped to its gamut, encoded. */
+/**
+ * Luminance-preserving gamut clip: a color the target cannot show slides toward the gray of its own
+ * luminance until it fits, so brightness and hue hold and only saturation is lost. This is the
+ * simplest member of the family the ACES gamut compressor belongs to; darktable and Krita offer the
+ * same clip as their "preserve luminance" mode.
+ */
+fn clipToGamut(rgb: vec3f, light: f32) -> vec3f {
+ var color = rgb;
+ let low = min(color.r, min(color.g, color.b));
+ if (low < 0.0 && light > 0.0) {
+ color = mix(color, vec3f(light), -low / (light - low));
+ }
+ let high = max(color.r, max(color.g, color.b));
+ if (high > 1.0) {
+ color = mix(color, vec3f(light), (high - 1.0) / (high - light));
+ }
+ return clamp(color, vec3f(0.0), vec3f(1.0));
+}
+
+/** Working space to what the screen shows: sRGB, clipped to its gamut with hue and luminance kept, encoded. */
export fn display(working: vec3f) -> vec3f {
- return linearToSrgb3(clamp(rec2020ToSrgb * working, vec3f(0.0), vec3f(1.0)));
+ let light = luminance(working);
+ if (light >= 1.0) {
+ return vec3f(1.0);
+ }
+ return linearToSrgb3(clipToGamut(rec2020ToSrgb * working, light));
}
diff --git a/src/lib/tone-curves/curves.wgsl b/src/lib/tone-curves/curves.wgsl
index 03c17ed..113650c 100644
--- a/src/lib/tone-curves/curves.wgsl
+++ b/src/lib/tone-curves/curves.wgsl
@@ -4,11 +4,13 @@ import { linearToSrgb, srgbToLinear } from "@vgpu/wgsl-std/color";
@group(0) @binding(1) var<storage, read> curve: array<f32>;
// The graph uses sRGB transfer; input and output remain linear working RGB.
+// Headroom above 1.0 rides on the curve's white endpoint.
fn lookup(linear: f32) -> f32 {
let last = arrayLength(&curve) - 1u;
- let position = clamp(linearToSrgb(linear), 0.0, 1.0) * f32(last);
+ let bounded = clamp(linear, 0.0, 1.0);
+ let position = linearToSrgb(bounded) * f32(last);
let lo = u32(position);
- return srgbToLinear(mix(curve[lo], curve[min(lo + 1u, last)], fract(position)));
+ return srgbToLinear(mix(curve[lo], curve[min(lo + 1u, last)], fract(position))) + max(linear - 1.0, 0.0);
}
// Film-like tone mapping: interpolate the middle channel between mapped extremes in linear RGB.
@@ -25,5 +27,5 @@ fn tone(color: vec3f) -> vec3f {
@fragment fn fs_main(@builtin(position) position: vec4f) -> @location(0) vec4f {
let input = textureLoad(source, vec2i(position.xy), 0);
- return vec4f(tone(clamp(input.rgb, vec3f(0.0), vec3f(1.0))), input.a);
+ return vec4f(tone(input.rgb), input.a);
}
diff --git a/tests/fixtures/blue-float.tif b/tests/fixtures/blue-float.tif
new file mode 100644
index 0000000..21a1a1e
Binary files /dev/null and b/tests/fixtures/blue-float.tif differ
diff --git a/tests/tiff.e2e.ts b/tests/tiff.e2e.ts
index 98e2788..ad8e725 100644
--- a/tests/tiff.e2e.ts
+++ b/tests/tiff.e2e.ts
@@ -4,13 +4,13 @@ import { readImage } from "./images";
const directory = "src/lib/tiff-gpu/fixtures";
-test("TIFF files open through the loader with their color and orientation", async ({
+test("TIFF files open through the loader with their color, orientation, and headroom", async ({
page,
}) => {
await page.goto("/");
await page.waitForFunction(() => window.openlight);
- const load = async (name: string) => {
- const bytes = [...(await readFile(`${directory}/${name}`))];
+ const load = async (name: string, folder = directory) => {
+ const bytes = [...(await readFile(`${folder}/${name}`))];
await page.evaluate(
({ bytes, name }) =>
window.openlight.loadImage(new File([new Uint8Array(bytes)], name)),
@@ -26,6 +26,48 @@ test("TIFF files open through the loader with their color and orientation", asyn
}
await load("orientation-6.tif");
expect((await readImage(page)).size).toEqual([17, 19]);
+ // Row 1 of the float file holds linear 1, 2, and 4 at x = 4..6: all clip at first, then separate at -2 stops.
+ await load("float32-be.tif");
+ const row = () =>
+ page.evaluate(async () => {
+ const image = await createImageBitmap(
+ await window.openlight.exportImage(),
+ );
+ const canvas = new OffscreenCanvas(image.width, image.height);
+ const context = canvas.getContext("2d");
+ if (!context) throw new Error("Cannot read exported image.");
+ context.drawImage(image, 0, 0);
+ image.close();
+ return [...context.getImageData(0, 1, 7, 1).data].filter(
+ (_, i) => i % 4 === 0,
+ );
+ });
+ expect((await row()).slice(4)).toEqual([255, 255, 255]);
+ await page.evaluate(() => window.openlight.setAdjustments({ exposure: -2 }));
+ const recovered = await row();
+ expect(recovered[4]).toBeLessThan(150);
+ expect(recovered[5]).toBeGreaterThan(recovered[4]);
+ expect(recovered[6]).toBeGreaterThan(240);
+ // The blue pixel brightens without losing its hue, then approaches white.
+ await load("blue-float.tif", "tests/fixtures");
+ const colors = [];
+ for (const exposure of [0, 2, 5]) {
+ await page.evaluate(
+ (exposure) => window.openlight.setAdjustments({ exposure }),
+ exposure,
+ );
+ colors.push((await readImage(page)).center.slice(0, 3));
+ }
+ for (const rgb of colors.slice(0, 2)) {
+ expect(rgb[2] - Math.max(rgb[0], rgb[1])).toBeGreaterThan(20);
+ }
+ const brightness = colors.map((rgb) =>
+ rgb.reduce((sum, value) => sum + value, 0),
+ );
+ expect(brightness[1]).toBeGreaterThan(brightness[0]);
+ const white = colors[2];
+ expect(Math.min(...white)).toBeGreaterThan(240);
+ expect(Math.max(...white) - Math.min(...white)).toBeLessThan(10);
await load("rgb8-jpeg.tif");
await expect(
page.getByText("Couldn't open rgb8-jpeg.tif:", { exact: false }),