diff --git a/scripts/render-check.mjs b/scripts/render-check.mjs index 0f0eb07..e5335f6 100644 --- a/scripts/render-check.mjs +++ b/scripts/render-check.mjs @@ -304,6 +304,81 @@ try { const tintRim = pixelAt(0.25, 0.2 + dabRadiusY * 0.75); const tintOutside = pixelAt(0.25, 0.2 + dabRadiusY * 1.15); + // Source-space alignment. Masks are baked over the whole source, so a crop + // that keeps a dab in frame but moves it on screen must move its tint and + // its local detail with it: source (0.6, 0.4) inside a quarter-frame crop + // lands at screen (0.4, 0.6), and nothing may be tinted at (0.6, 0.4). + const cropRect = { + x: 0.5, + y: 0.25, + w: 0.25, + h: 0.25, + angle: 0, + quarter: 0, + aspect: 'original', + flipH: false, + flipV: false, + }; + const dabEdits = (at, size, adjust) => { + const layer = defaultLocalLayer('brush', 0); + const shape = layer.components[0]; + shape.feather = 0; + shape.brush.strokes = [{ points: [at], size, erase: false }]; + return { + ...defaultEdits(), + local: [{ ...layer, adjust: { ...layer.adjust, ...(adjust || {}) } }], + }; + }; + const cropped = { ...defaultEdits(), crop: cropRect }; + renderer.render(cropped, { width: w, height: h }); + const cropRef = pixelAt(0.4, 0.6); + const cropRefOff = pixelAt(0.6, 0.4); + const cropDab = dabEdits({ x: 0.6, y: 0.4 }, 0.02); + cropDab.crop = cropRect; + renderer.render(cropDab, { width: w, height: h, maskView: 0 }); + const cropTintOn = pixelAt(0.4, 0.6); + const cropTintOff = pixelAt(0.6, 0.4); + // Same crop and dab, but as a local detail amount instead of a tint: the + // finish pass has to read the mask through the same source coordinate. + const cropDetail = dabEdits({ x: 0.6, y: 0.4 }, 0.02, { dehaze: 100 }); + cropDetail.crop = cropRect; + renderer.render(cropDetail, { width: w, height: h }); + const cropDetailOn = pixelAt(0.4, 0.6); + const cropDetailOff = pixelAt(0.6, 0.4); + + // Tint strength and toggle: full strength must read stronger than the default + // half, and no maskView at all must leave the photo untouched. + renderer.render(brushEdits(80), { width: w, height: h, maskView: 0, maskStrength: 1 }); + const tintFull = pixelAt(0.25, 0.2); + renderer.render(brushEdits(80), { width: w, height: h }); + const tintOffPixel = pixelAt(0.25, 0.2); + + // Lens warp: a geometry map that magnifies the centre by 1.6 puts the dab at + // source (0.7, 0.45) at screen (0.625, 0.469); its source uv must be clear. + const lensN = 33; + const lensData = new Float32Array(lensN * lensN * 4); + for (let ly = 0; ly < lensN; ly++) { + for (let lx = 0; lx < lensN; lx++) { + const i = (ly * lensN + lx) * 4; + lensData[i] = 0.5 + ((lx + 0.5) / lensN - 0.5) * 1.6; + lensData[i + 1] = 0.5 + ((ly + 0.5) / lensN - 0.5) * 1.6; + lensData[i + 2] = 0; + lensData[i + 3] = 1; + } + } + const lensWarp = { + key: 'render-check-warp', + scale: 1, + geometry: { data: lensData, width: lensN, height: lensN }, + }; + renderer.render(defaultEdits(), { width: w, height: h, lens: lensWarp }); + const lensRef = pixelAt(0.625, 0.469); + const lensRefOff = pixelAt(0.7, 0.469); + const lensDab = dabEdits({ x: 0.7, y: 0.45 }, 0.04); + renderer.render(lensDab, { width: w, height: h, lens: lensWarp, maskView: 0 }); + const lensTintOn = pixelAt(0.625, 0.469); + const lensTintOff = pixelAt(0.7, 0.469); + pre.textContent = 'RESULT ' + JSON.stringify({ clipPixels, clipRed: near(clipPixels[0], RED_MARK), @@ -342,6 +417,28 @@ try { // coverage without depending on how dark the adjustment left the pixels. tintFollowsMask: tintCore[2] > tintRim[2] && tintRim[2] > tintOutside[2] + 5, tintBounded: Math.abs(tintOutside[2] - dabOutsideSoft[2]) <= 1, + cropRef, + cropRefOff, + cropTintOn, + cropTintOff, + cropDetailOn, + cropDetailOff, + lensRef, + lensRefOff, + lensTintOn, + lensTintOff, + tintFull, + tintOffPixel, + // The tint pushes red up hard on the dark mid band, so red ranks coverage. + cropTintAligned: cropTintOn[0] > cropRef[0] + 40, + cropTintBounded: Math.abs(cropTintOff[0] - cropRefOff[0]) <= 2, + // Local dehaze lifts the blue of that band, so blue marks where it landed. + cropDetailAligned: cropDetailOn[2] > cropDetailOff[2] + 10, + cropDetailBounded: Math.abs(cropDetailOff[2] - cropRefOff[2]) <= 3, + lensTintAligned: lensTintOn[0] > lensRef[0] + 40, + lensTintBounded: Math.abs(lensTintOff[0] - lensRefOff[0]) <= 2, + tintStrengthWorks: tintFull[2] > tintCore[2] + 8, + tintOffMatches: Math.abs(tintOffPixel[2] - dabSoft[2]) <= 1, // A hard dab covers the core, and even an 80 % feather stops at the rim. dabCoreDarkened: dabHard[0] < 140, dabSoftCoreDarkened: dabSoft[0] < 140, @@ -572,6 +669,39 @@ async function main() { `render check: mask overlay core=${JSON.stringify(result.tintCore)}` + ` rim=${JSON.stringify(result.tintRim)} outside=${JSON.stringify(result.tintOutside)}`, ); + console.log( + `render check: crop mask tint on=${JSON.stringify(result.cropTintOn)}` + + ` off=${JSON.stringify(result.cropTintOff)} ref=${JSON.stringify(result.cropRef)}` + + ` | local detail on=${JSON.stringify(result.cropDetailOn)}` + + ` off=${JSON.stringify(result.cropDetailOff)}`, + ); + console.log( + `render check: lens mask tint on=${JSON.stringify(result.lensTintOn)}` + + ` off=${JSON.stringify(result.lensTintOff)} ref=${JSON.stringify(result.lensRef)}` + + ` | tint strength full=${JSON.stringify(result.tintFull)}` + + ` half=${JSON.stringify(result.tintCore)}`, + ); + if ( + !result.cropTintAligned || + !result.cropTintBounded || + !result.cropDetailAligned || + !result.cropDetailBounded || + !result.lensTintAligned || + !result.lensTintBounded || + !result.tintStrengthWorks || + !result.tintOffMatches + ) { + console.error( + 'render check FAILED: the local mask does not stay on its source pixels ' + + 'through a crop or a lens warp ' + + `(crop tint ${JSON.stringify(result.cropTintOn)}/${JSON.stringify(result.cropTintOff)}` + + ` vs ${JSON.stringify(result.cropRef)}, detail ` + + `${JSON.stringify(result.cropDetailOn)}/${JSON.stringify(result.cropDetailOff)}` + + `, lens tint ${JSON.stringify(result.lensTintOn)}/${JSON.stringify(result.lensTintOff)}` + + ` vs ${JSON.stringify(result.lensRef)}).`, + ); + return 1; + } if ( !result.dabCoreDarkened || !result.dabSoftCoreDarkened || diff --git a/src/components/Develop.tsx b/src/components/Develop.tsx index 04b12d2..815fefa 100644 --- a/src/components/Develop.tsx +++ b/src/components/Develop.tsx @@ -267,6 +267,12 @@ export default function Develop({ const [localBrush, setLocalBrush] = useState(72); const [localHover, setLocalHover] = useState<{ x: number; y: number } | null>(null); const [localDraft, setLocalDraft] = useState(null); + // The mask tint is a view aid, not an edit: it has its own toggle (O) and + // strength, and both are display preferences rather than catalog data. + const [maskOverlay, setMaskOverlay] = usePersistentState('develop.maskOverlay', true); + const [maskStrength, setMaskStrength] = usePersistentState('develop.maskStrength', 50); + // How the next mask gesture combines with the group: paint or erase. + const [localErase, setLocalErase] = useState(false); const [lensCorrection, setLensCorrection] = useState(null); const [lensSplit, setLensSplit] = useState(false); const [lensSplitAt, setLensSplitAt] = useState(0.5); @@ -597,11 +603,12 @@ export default function Develop({ : view, ); - // The mask overlay is the baked mask itself, so it can only be shown when a - // local layer is selected. The histogram ignores it: a tinted chart is not - // a measurement of the photo. + // The mask overlay is the baked mask itself, and it is a view toggle: off + // while holding \\ (before/after), while the mask tool is closed, or when + // the user hides it. The histogram ignores it: a tinted chart is not a + // measurement of the photo. const maskView = - showOriginal || !localMode + showOriginal || !localMode || !maskOverlay ? -1 : (edits.local ?? []).findIndex((layer) => layer.id === localSelected); @@ -627,6 +634,7 @@ export default function Develop({ lens: showOriginal ? null : lensCorrection, lensSplit: lensSplit && !showOriginal ? lensSplitAt : undefined, maskView, + maskStrength: maskStrength / 100, }); histRef.current = renderer.histogram; setHistVersion((v) => v + 1); @@ -650,6 +658,8 @@ export default function Develop({ lensSplitAt, localMode, localSelected, + maskOverlay, + maskStrength, // Loading or clearing a printer profile changes the finish pass, and the // cube lives outside the edit record, so the redraw is keyed on it here. proofProfile, @@ -1246,7 +1256,11 @@ export default function Develop({ e.preventDefault(); e.currentTarget.setPointerCapture(e.pointerId); if (activeComponent.kind === 'brush') { - const stroke: LocalStroke = { points: [p], size: localRadius(), erase: e.altKey }; + const stroke: LocalStroke = { + points: [p], + size: localRadius(), + erase: e.altKey || localErase, + }; localDrag.current = { layerId: activeLocal.id, componentId: activeComponent.id, mode: 'brush', stroke }; setLocalDraft(stroke); return; @@ -1381,9 +1395,16 @@ export default function Develop({ setLocalComponentSelected(null); setLocalMode(false); setLocalDraft(null); + setLocalErase(false); localDrag.current = null; }, [photo.id]); + // Leaving the mask tool drops the erase mode, so the next visit starts by + // painting rather than cutting holes into the mask. + useEffect(() => { + if (!localMode) setLocalErase(false); + }, [localMode]); + useEffect(() => { if (!(edits.local ?? []).some((layer) => layer.id === localSelected)) { setLocalSelected(null); @@ -1529,6 +1550,13 @@ export default function Develop({ toggleLocalMode(); e.preventDefault(); } + } else if (e.key.toLowerCase() === 'o' && !e.metaKey && !e.ctrlKey && !e.altKey) { + // The mask tint is a view, so it can be hidden while painting or + // checking the photo underneath without leaving the mask tool. + if (localMode) { + setMaskOverlay((v) => !v); + e.preventDefault(); + } } else if (e.key === 'Enter' && cropMode) { setCropMode(false); } else if (e.key === 'Escape') { @@ -2382,6 +2410,42 @@ export default function Develop({ {Math.round(activeComponent.feather)}% )} + + + + {maskOverlay && ( + <> + + {Math.round(maskStrength)}% + + )} {activeComponent.kind === 'brush' - ? 'Paint the mask · Alt paints an erase stroke · wheel sizes the brush' + ? 'Paint the mask · − erases · Alt erases one stroke · wheel sizes the brush' : activeComponent.kind === 'radial' ? 'Drag the centre or the edge handle · wheel zooms' : 'Drag either end of the gradient'} diff --git a/src/components/Shortcuts.tsx b/src/components/Shortcuts.tsx index e25eccf..68ab3a2 100644 --- a/src/components/Shortcuts.tsx +++ b/src/components/Shortcuts.tsx @@ -36,6 +36,7 @@ const GROUPS: { title: string; rows: [string, string][] }[] = [ ['C', 'Crop & straighten'], ['Q', 'Spot healing brush'], ['M', 'Edit the selected local mask'], + ['O', 'Show or hide the mask tint'], ['Enter', 'Apply the crop'], ['\\ (hold)', 'Before / after'], ['1 – 5', 'Star rating'], diff --git a/src/gl/renderer.ts b/src/gl/renderer.ts index 1afffb8..0ba038a 100644 --- a/src/gl/renderer.ts +++ b/src/gl/renderer.ts @@ -18,7 +18,11 @@ void main(){ const GRADE_FRAG = `#version 300 es precision highp float; in vec2 v_uv; -out vec4 outColor; +layout(location = 0) out vec4 outColor; +// Source coordinate that produced this pixel, written alongside the picture so +// later passes can read the source-space masks at the right place. Alpha is 1 +// when the pixel is inside the cropped image and 0 on the letterbox. +layout(location = 1) out vec4 outMap; uniform sampler2D u_src; uniform sampler2D u_curve; @@ -79,6 +83,8 @@ uniform sampler2D u_masks2; // layers 5-8 uniform int u_localCount; // Local mask overlay: index of the layer tinted over the picture, -1 = off. uniform float u_maskView; +// Opacity of that tint, 0-1. +uniform float u_maskStrength; uniform vec4 u_localAdj[8]; // exposure, contrast, temp, tint uniform vec4 u_localAdj2[8]; // saturation, highlights, shadows, unused uniform vec4 u_localAdj3[8]; // whites, blacks, vibrance, hue degrees @@ -280,6 +286,7 @@ vec2 warpGeometry(vec2 s){ } void main(){ + outMap = vec4(0.0); // The eyedropper renders one pixel at an arbitrary point, so the mapping below // takes its coordinate from the uniform instead of the fragment position. vec2 outUv = u_pick > 0.5 ? u_pickUv : v_uv; @@ -322,6 +329,9 @@ void main(){ : lensQ; } vec2 uv = lensCoord; + // Hand the source coordinate to the passes that need source-space data (the + // local detail masks) instead of making them repeat this warp. + outMap = vec4(uv, 0.0, 1.0); vec3 src; if (showLens && (u_lensFlags & 2) != 0) { vec4 t = lensSample(u_lensTca, u_lensTcaSize, lensCoord); @@ -577,7 +587,11 @@ void main(){ int mk = mv < 4 ? mv : mv - 4; vec4 mtex = mv < 4 ? texture(u_masks, uv) : texture(u_masks2, uv); float cov = mk == 0 ? mtex.r : mk == 1 ? mtex.g : mk == 2 ? mtex.b : mtex.a; - srgb = mix(srgb, vec3(0.98, 0.72, 0.35), clamp(cov, 0.0, 1.0) * 0.5); + srgb = mix( + srgb, + vec3(0.98, 0.72, 0.35), + clamp(cov, 0.0, 1.0) * clamp(u_maskStrength, 0.0, 1.0) + ); } outColor = vec4(clamp(srgb, 0.0, 1.0), 1.0); @@ -609,6 +623,8 @@ uniform sampler2D u_blurBig; // grade pass. Amounts are added to the global sliders per pixel. uniform sampler2D u_masks; uniform sampler2D u_masks2; +// Source coordinate of this output pixel, from the grade pass. +uniform sampler2D u_maskUv; uniform int u_localCount; uniform vec4 u_localDetail[8]; // texture, clarity, dehaze, sharpen uniform vec4 u_localModes[8]; // x = add / subtract / intersect @@ -693,9 +709,14 @@ void main(){ float localClarity = 0.0; float localDehaze = 0.0; float localSharpen = 0.0; - if (u_localCount > 0) { - vec4 maskA = texture(u_masks, v_uv); - vec4 maskB = texture(u_masks2, v_uv); + // The masks are baked in source space, so read them through the source + // coordinate the grade pass wrote for this pixel: crop, straightening, zoom + // and lens correction all move the mask along with the picture. + vec4 maskMap = texture(u_maskUv, v_uv); + if (u_localCount > 0 && maskMap.a > 0.5) { + vec2 maskUv = maskMap.rg; + vec4 maskA = texture(u_masks, maskUv); + vec4 maskB = texture(u_masks2, maskUv); float prevMask = 1.0; for (int i = 0; i < 8; i++) { if (i >= u_localCount) break; @@ -938,6 +959,8 @@ interface Target { h: number; /** Half-float pixels keep subtle gradients from banding between passes. */ float: boolean; + /** Second colour attachment, e.g. the source-coordinate map of the grade. */ + aux?: WebGLTexture; } function makeHistogram(): Histogram { @@ -1656,36 +1679,48 @@ export class Renderer { this.proofLutOn = 1; } - private makeTarget(w: number, h: number, float = this.floatTargets): Target { + private makeTarget(w: number, h: number, float = this.floatTargets, aux = false): Target { const gl = this.gl; const tex = gl.createTexture(); const fbo = gl.createFramebuffer(); if (!tex || !fbo) throw new Error('Unable to allocate render target'); - gl.bindTexture(gl.TEXTURE_2D, tex); - if (float) { - gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA16F, w, h, 0, gl.RGBA, gl.HALF_FLOAT, null); - } else { - gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA8, w, h, 0, gl.RGBA, gl.UNSIGNED_BYTE, null); - } - gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE); - gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE); - gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.LINEAR); - gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.LINEAR); + const alloc = (texture: WebGLTexture) => { + gl.bindTexture(gl.TEXTURE_2D, texture); + if (float) { + gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA16F, w, h, 0, gl.RGBA, gl.HALF_FLOAT, null); + } else { + gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA8, w, h, 0, gl.RGBA, gl.UNSIGNED_BYTE, null); + } + gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE); + gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE); + gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.LINEAR); + gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.LINEAR); + }; + alloc(tex); + const auxTex = aux ? gl.createTexture() : null; + if (aux && !auxTex) throw new Error('Unable to allocate render target'); gl.bindFramebuffer(gl.FRAMEBUFFER, fbo); gl.framebufferTexture2D(gl.FRAMEBUFFER, gl.COLOR_ATTACHMENT0, gl.TEXTURE_2D, tex, 0); + if (auxTex) { + alloc(auxTex); + gl.framebufferTexture2D(gl.FRAMEBUFFER, gl.COLOR_ATTACHMENT1, gl.TEXTURE_2D, auxTex, 0); + gl.drawBuffers([gl.COLOR_ATTACHMENT0, gl.COLOR_ATTACHMENT1]); + } if (float && gl.checkFramebufferStatus(gl.FRAMEBUFFER) !== gl.FRAMEBUFFER_COMPLETE) { gl.bindFramebuffer(gl.FRAMEBUFFER, null); gl.deleteTexture(tex); + if (auxTex) gl.deleteTexture(auxTex); gl.deleteFramebuffer(fbo); throw new Error('Half-float render targets are unavailable'); } gl.bindFramebuffer(gl.FRAMEBUFFER, null); - return { tex, fbo, w, h, float }; + return { tex, fbo, w, h, float, aux: auxTex ?? undefined }; } private freeTarget(target?: Target) { if (!target) return; this.gl.deleteTexture(target.tex); + if (target.aux) this.gl.deleteTexture(target.aux); this.gl.deleteFramebuffer(target.fbo); } @@ -1708,13 +1743,14 @@ export class Renderer { for (const useFloat of modes) { const made: Target[] = []; try { - const mk = (mw: number, mh: number) => { - const target = this.makeTarget(mw, mh, useFloat); + const mk = (mw: number, mh: number, extra = false) => { + const target = this.makeTarget(mw, mh, useFloat, extra); made.push(target); return target; }; this.targets = { - graded: mk(w, h), + // The grade also writes the source-coordinate map for the finish pass. + graded: mk(w, h, true), smallA: mk(sw, sh), smallB: mk(sw, sh), bigA: mk(bw, bh), @@ -1807,6 +1843,7 @@ export class Renderer { upscale: boolean, srcLod = 0, maskView = -1, + maskStrength = 0.5, ) { const g = this.grade; this.gl.useProgram(g.program); @@ -1895,6 +1932,7 @@ export class Renderer { g.i('u_localCount', localCount); // Only tint a layer that this edit actually has; -1 leaves the picture alone. g.f('u_maskView', maskView >= 0 && maskView < localCount ? maskView : -1); + g.f('u_maskStrength', clamp01(maskStrength)); for (let i = 0; i < localCount; i++) { const layer = edits.local[i]; g.v4i('u_localAdj', i, layer.adjust.exposure, layer.adjust.contrast, layer.adjust.temp, layer.adjust.tint); @@ -1981,6 +2019,8 @@ export class Renderer { clipping?: 'highlights' | 'shadows'; /** tint this local layer's baked mask over the picture; -1 or omitted = off */ maskView?: number; + /** opacity of the mask tint, 0-1 (default 0.5) */ + maskStrength?: number; /** return the finished image as linear-space floats instead of drawing the canvas */ floatOut?: boolean; }, @@ -2025,7 +2065,16 @@ export class Renderer { const cropPxW = Math.max(1, bounds.w * crop.w); const cropPxH = Math.max(1, bounds.h * crop.h); const srcLod = Math.max(0, Math.log2(Math.max(cropPxW / w, cropPxH / h))); - this.bindGrade(edits, crop, valid, bounds, !!opts.upscale, srcLod, opts.maskView ?? -1); + this.bindGrade( + edits, + crop, + valid, + bounds, + !!opts.upscale, + srcLod, + opts.maskView ?? -1, + opts.maskStrength ?? 0.5, + ); this.drawQuad(); // Passes 2-3: small blur (detail) and large blur (clarity) from the graded image @@ -2109,6 +2158,9 @@ export class Renderer { this.finish.f('u_detailScale', detailScale); this.finish.tex('u_masks', this.maskTex, 3); this.finish.tex('u_masks2', this.maskTex2, 4); + // Source coordinate per pixel, written by the grade pass. The masks are + // source-space, so local detail reads them through this instead of v_uv. + if (t.graded!.aux) this.finish.tex('u_maskUv', t.graded!.aux, 5); const innerLayers = (edits.local ?? []).slice(0, MAX_LOCAL_LAYERS); this.finish.i('u_localCount', innerLayers.length); for (let i = 0; i < innerLayers.length; i++) {