// Retouching brushes: clone stamp and smudge. import { Tool } from './tool.js'; import { BrushTool, Dabber, LiveStroke, pointerPressure, softStamp } from './paint.js'; import { PixelEdit } from '../core/history.js'; import { createCanvas, cloneCanvas, Rect } from '../core/util.js'; import { flattenToCanvas } from '../render/compositor.js'; export class CloneTool extends BrushTool { static id = 'clone'; static label = 'Clone Stamp'; static icon = 'stamp'; static shortcut = 'S'; static schema = [ { key: 'size', label: 'Size', type: 'range', min: 1, max: 500, default: 40, unit: 'px', scale: 'log' }, { key: 'hardness', label: 'Hardness', type: 'range', min: 0, max: 100, default: 50, unit: '%' }, { key: 'opacity', label: 'Opacity', type: 'range', min: 1, max: 100, default: 100, unit: '%' }, { key: 'flow', label: 'Flow', type: 'range', min: 1, max: 100, default: 100, unit: '%' }, { key: 'aligned', label: 'Aligned', type: 'checkbox', default: true }, { key: 'sampleMerged', label: 'All layers', type: 'checkbox', default: false }, { key: 'spacing', type: 'hidden', default: 10 }, { key: 'smoothing', type: 'hidden', default: 0 }, { key: 'pressureSize', label: 'Pressure → size', type: 'checkbox', default: true }, { key: 'pressureOpacity', type: 'hidden', default: false }, ]; get hint() { return this.source ? 'Paint to clone · Alt+click sets a new source' : 'Alt+click to set the clone source'; } onPointerDown(e, p) { if (e.altKey) { this.source = { x: p.x, y: p.y }; this.offset = null; this.app.toast('Clone source set'); this.app.emit('toolHint'); return; } if (!this.source) { this.app.toast('Alt+click to set the clone source first'); return; } const target = this.requireEditable(); if (!target) return; if (!this.offset || !this.options.aligned) this.offset = { x: this.source.x - p.x, y: this.source.y - p.y }; this.sampleCanvas = this.options.sampleMerged ? flattenToCanvas(this.doc) : cloneCanvas(target.layer[target.key]); this.dabCanvas = null; this.color = 'rgba(0,0,0,1)'; this.stroke = new LiveStroke(this.doc, target, { opacity: this.options.opacity / 100, composite: target.layer.alphaLock ? 'source-atop' : 'source-over', label: 'Clone', }); this.dabber = new Dabber((pr) => this.spacing(pr)); this.smoothed = { x: p.x, y: p.y, pressure: pointerPressure(e) }; this.dabber.start({ ...this.smoothed }, (d) => this.dab(d)); this.stroke.update(); } dab(p) { const o = this.options; const size = Math.max(1, o.size * (o.pressureSize ? p.pressure : 1)); const d = Math.ceil(size); if (!this.dabCanvas || this.dabCanvas.width !== d) this.dabCanvas = createCanvas(d, d); const ctx = this.dabCanvas.getContext('2d'); ctx.globalCompositeOperation = 'copy'; ctx.drawImage(softStamp(o.size, o.hardness / 100, this.color), 0, 0, d, d); ctx.globalCompositeOperation = 'source-in'; const x = p.x - d / 2, y = p.y - d / 2; ctx.drawImage(this.sampleCanvas, -(x + this.offset.x), -(y + this.offset.y)); const sctx = this.stroke.ctx; sctx.globalAlpha = o.flow / 100; sctx.drawImage(this.dabCanvas, x, y); this.stroke.addDirty({ x, y, w: d, h: d }); } onPointerUp(e, p) { super.onPointerUp(e, p); this.sampleCanvas = null; } drawOverlay(ctx, view, doc) { const ptr = view.pointer; let src = null; if (this.stroke && this.offset && this.smoothed) src = { x: this.smoothed.x + this.offset.x, y: this.smoothed.y + this.offset.y }; else if (this.source && ptr && this.offset && this.options.aligned) { const dp = view.screenToDoc(ptr, doc); src = { x: dp.x + this.offset.x, y: dp.y + this.offset.y }; } else if (this.source) src = this.source; if (!src) return; const s = view.docToScreen(src, doc); ctx.save(); ctx.strokeStyle = '#fff'; ctx.lineWidth = 3; const cross = () => { ctx.beginPath(); ctx.moveTo(s.x - 8, s.y); ctx.lineTo(s.x + 8, s.y); ctx.moveTo(s.x, s.y - 8); ctx.lineTo(s.x, s.y + 8); ctx.stroke(); }; cross(); ctx.strokeStyle = '#000'; ctx.lineWidth = 1; cross(); ctx.restore(); } } export class SmudgeTool extends Tool { static id = 'smudge'; static label = 'Smudge'; static icon = 'smudge'; static shortcut = 'R'; static schema = [ { key: 'size', label: 'Size', type: 'range', min: 1, max: 300, default: 30, unit: 'px', scale: 'log' }, { key: 'hardness', label: 'Hardness', type: 'range', min: 0, max: 100, default: 30, unit: '%' }, { key: 'strength', label: 'Strength', type: 'range', min: 1, max: 100, default: 60, unit: '%' }, ]; get cursor() { return 'none'; } cursorRadius() { return this.options.size / 2; } onPointerDown(e, p) { const target = this.requireEditable(); if (!target) return; this.target = target; this.canvas = target.layer[target.key]; this.edit = new PixelEdit(this.doc, target.layer, target.key); this.dirty = null; const d = Math.ceil(this.options.size); this.d = d; this.carry = createCanvas(d, d); this.dab = createCanvas(d, d); this.pick(p); this.dabber = new Dabber(() => Math.max(1, this.d * 0.1)); this.dabber.start({ x: p.x, y: p.y, pressure: 1 }, () => {}); } pick(p) { const ctx = this.carry.getContext('2d'); ctx.globalCompositeOperation = 'copy'; ctx.drawImage(this.canvas, -(p.x - this.d / 2), -(p.y - this.d / 2)); } apply(p) { const d = this.d; const x = p.x - d / 2, y = p.y - d / 2; const ctx = this.dab.getContext('2d'); ctx.globalCompositeOperation = 'copy'; ctx.drawImage(this.carry, 0, 0); ctx.globalCompositeOperation = 'destination-in'; ctx.drawImage(softStamp(d, this.options.hardness / 100, 'rgba(0,0,0,1)'), 0, 0); if (this.doc.selection) ctx.drawImage(this.doc.selection.mask, -x, -y); const tctx = this.canvas.getContext('2d'); tctx.save(); tctx.globalAlpha = this.options.strength / 100; if (this.target.layer.alphaLock) tctx.globalCompositeOperation = 'source-atop'; tctx.drawImage(this.dab, x, y); tctx.restore(); this.dirty = Rect.union(this.dirty, { x, y, w: d, h: d }); // pick up the mixed result for the next dab this.pick(p); } onPointerMove(e, p) { if (!this.edit) return; this.dabber.to({ x: p.x, y: p.y, pressure: 1 }, (pt) => this.apply(pt)); if (this.target.key === 'mask') this.target.layer.touchMask(); else this.target.layer.touch(); // in-place edit: refresh cached layer styles this.doc.markDirty(); } onPointerUp() { if (!this.edit) return; if (this.dirty) this.edit.commit('Smudge', this.dirty); this.edit = null; } cancel() { if (this.edit) this.edit.cancel(); this.edit = null; } } // ---------------------------------------------------------------- dodge / burn / sponge const TONE_OPPOSITE = { dodge: 'burn', burn: 'dodge', saturate: 'desaturate', desaturate: 'saturate' }; const TONE_LABELS = { dodge: 'Dodge', burn: 'Burn', saturate: 'Sponge', desaturate: 'Sponge' }; /** The whole canvas with the tone adjustment applied at full strength. */ export function tonedCanvas(src, mode, range, amount) { const w = src.width, h = src.height; const id = src.getContext('2d', { willReadFrequently: true }).getImageData(0, 0, w, h); const d = id.data; const weight = range === 'shadows' ? (l) => (1 - l) * (1 - l) : range === 'highlights' ? (l) => l * l : (l) => 1 - (2 * l - 1) * (2 * l - 1); for (let i = 0; i < d.length; i += 4) { if (!d[i + 3]) continue; const r = d[i], g = d[i + 1], b = d[i + 2]; const lum = 0.2126 * r + 0.7152 * g + 0.0722 * b; if (mode === 'dodge' || mode === 'burn') { const k = amount * weight(lum / 255); if (mode === 'dodge') { d[i] = r + (255 - r) * k; d[i + 1] = g + (255 - g) * k; d[i + 2] = b + (255 - b) * k; } else { d[i] = r * (1 - k); d[i + 1] = g * (1 - k); d[i + 2] = b * (1 - k); } } else { const k = mode === 'saturate' ? 1 + amount : 1 - amount; d[i] = lum + (r - lum) * k; d[i + 1] = lum + (g - lum) * k; d[i + 2] = lum + (b - lum) * k; } } const out = createCanvas(w, h); out.getContext('2d').putImageData(id, 0, 0); return out; } export class ToningTool extends BrushTool { static id = 'dodge'; static label = 'Dodge / Burn'; static icon = 'dodge'; static shortcut = 'O'; static schema = [ { key: 'tone', label: 'Mode', type: 'select', default: 'dodge', options: [ { value: 'dodge', label: 'Dodge (lighten)' }, { value: 'burn', label: 'Burn (darken)' }, { value: 'saturate', label: 'Sponge: saturate' }, { value: 'desaturate', label: 'Sponge: desaturate' }, ], }, { key: 'range', label: 'Range', type: 'select', default: 'midtones', when: (o) => o.tone === 'dodge' || o.tone === 'burn', options: [{ value: 'shadows', label: 'Shadows' }, { value: 'midtones', label: 'Midtones' }, { value: 'highlights', label: 'Highlights' }], }, { key: 'exposure', label: 'Exposure', type: 'range', min: 1, max: 100, default: 40, unit: '%' }, { key: 'size', label: 'Size', type: 'range', min: 1, max: 500, default: 60, unit: 'px', scale: 'log' }, { key: 'hardness', label: 'Hardness', type: 'range', min: 0, max: 100, default: 20, unit: '%', more: true }, { key: 'flow', label: 'Flow', type: 'range', min: 1, max: 100, default: 50, unit: '%', more: true }, { key: 'spacing', label: 'Spacing', type: 'range', min: 1, max: 200, default: 12, unit: '%', more: true }, { key: 'smoothing', label: 'Smoothing', type: 'range', min: 0, max: 95, default: 0, unit: '%', more: true }, { key: 'pressureSize', label: 'Pressure → size', type: 'checkbox', default: true, more: true }, { key: 'pressureOpacity', label: 'Pressure → flow', type: 'checkbox', default: false, more: true }, ]; get usesColor() { return false; } get hint() { return 'Paint to lighten (dodge), darken (burn) or change saturation (sponge) · Alt: the opposite mode'; } beginStroke(target, e) { const mode = e.altKey ? TONE_OPPOSITE[this.options.tone] : this.options.tone; this.strokeLabel = TONE_LABELS[mode]; // Strokes reveal a fully adjusted copy of the layer through the painted coverage. this.toned = tonedCanvas(target.layer[target.key], mode, this.options.range, this.options.exposure / 100); this.coverage = createCanvas(this.toned.width, this.toned.height); } strokeOptions() { return { composite: 'source-atop', opacity: 1, color: 'rgba(0,0,0,1)', label: this.strokeLabel }; } dab(p) { const o = this.options; const size = Math.max(0.5, o.size * (o.pressureSize ? p.pressure : 1)); const flow = (o.flow / 100) * (o.pressureOpacity ? p.pressure : 1); const cctx = this.coverage.getContext('2d'); cctx.globalAlpha = flow; cctx.drawImage(softStamp(o.size, o.hardness / 100, this.color), p.x - size / 2, p.y - size / 2, size, size); const r = { x: p.x - size / 2, y: p.y - size / 2, w: size, h: size }; this.stroke.addDirty(r); const R = Rect.clampTo(Rect.outset(r, 1), this.toned.width, this.toned.height); if (!R) return; const b = this.stroke.ctx; b.save(); b.beginPath(); b.rect(R.x, R.y, R.w, R.h); b.clip(); // 'copy' would otherwise clear the rest of the buffer b.globalAlpha = 1; b.globalCompositeOperation = 'copy'; b.drawImage(this.toned, R.x, R.y, R.w, R.h, R.x, R.y, R.w, R.h); b.globalCompositeOperation = 'destination-in'; b.drawImage(this.coverage, R.x, R.y, R.w, R.h, R.x, R.y, R.w, R.h); b.restore(); } onPointerUp(e, p) { super.onPointerUp(e, p); this.toned = this.coverage = null; } }