Free open-source image editor for the browser
import type { HealPatch } from "@/core/document";
import {
type Composition,
merge,
node,
type PatchInput,
type RenderImage,
sourceSize,
} from "@/core/renderer";
import shader from "./heal.wgsl";
import { removePatch } from "./inpaint/pass";
import { patchBounds } from "./model";
type HealComposition = Pick<
Composition,
"patch" | "inputId" | "retain" | "field" | "keepField"
>;
function healPatch(
source: RenderImage,
{ coverage, origin: coverageOrigin }: PatchInput,
patch: Extract<HealPatch, { mode: "heal" | "clone" }>,
name: string,
) {
const dimensions = sourceSize(source);
const { origin, extent } = patchBounds(patch.strokes, dimensions, 0);
const samplers = {
linearSampler: { minFilter: "linear", magFilter: "linear" },
} as const;
const common = {
origin,
extent,
dimensions,
offset: patch.offset,
coverageOrigin,
coverageSize: coverage.size,
};
let correction = source;
if (patch.mode === "heal") {
// Coarse-to-fine harmonic extension of the destination/donor log color ratio.
// The correction is smooth; the donor retains its full-resolution texture.
for (const resolution of [4, 8, 16, 32, 64, 128]) {
const ratio = Math.min(1, resolution / Math.max(...extent));
const size: [number, number] = [
Math.max(2, Math.ceil(extent[0] * ratio)),
Math.max(2, Math.ceil(extent[1] * ratio)),
];
const params = { ...common, grid: size, feather: 0, opacity: 1 };
correction = merge(
{ source, coverage, previous: correction },
node(`${name}/${resolution}/seed`, shader, {
size,
samplers,
set: { params: { ...params, mode: resolution === 4 ? 0 : 1 } },
}),
);
const iterations = resolution === 4 ? 32 : 8;
for (let i = 0; i < iterations; i++) {
correction = merge(
{ source, coverage, previous: correction },
node(`${name}/${resolution}/relax-${i}`, shader, {
instance: `${name}/${resolution}/relax`,
size,
samplers,
set: { params: { ...params, mode: 2 } },
}),
);
}
if (resolution >= Math.max(...extent)) {
break;
}
}
}
return merge(
{ source, coverage, previous: correction },
node(`${name}/blend`, shader, {
samplers,
set: {
params: {
...common,
grid: source.size,
mode: patch.mode === "clone" ? 4 : 3,
feather: (patch.strokes[0].size * patch.feather) / 2,
opacity: patch.opacity,
},
},
}),
);
}
function repairsPixels(patch: HealPatch) {
return (
patch.mode === "remove" || patch.offset[0] !== 0 || patch.offset[1] !== 0
);
}
export function retainHealPatches(
patches: readonly HealPatch[],
name: string,
composition: Pick<Composition, "retain">,
) {
for (const patch of patches) {
if (repairsPixels(patch)) composition.retain(`${name}/${patch.id}`);
}
}
export function heal(
source: RenderImage,
patches: readonly HealPatch[],
name: string,
composition: HealComposition,
) {
retainHealPatches(patches, name, composition);
let image = source;
let inspected: RenderImage | undefined;
for (const patch of patches) {
if (patch.id === composition.inputId) inspected = image;
const id = `${name}/${patch.id}`;
if (!repairsPixels(patch)) continue;
const coverage = composition.patch(id, patch.strokes);
if (patch.mode === "remove") {
image = removePatch(image, coverage, patch, id, composition);
} else {
image = healPatch(image, coverage, patch, id);
}
}
return { image, input: inspected };
}