fix(heal): feed crisp photo context to inpaint model and cache patch across zoom levels
This commit is contained in:
@@ -505,18 +505,13 @@ export function cropPlate(
|
||||
const plane = w * h;
|
||||
const src = new Uint8Array(3 * plane);
|
||||
const mask = new Uint8Array(plane).fill(255);
|
||||
const field = rimFill(small, w, h, smallHole, 0);
|
||||
for (let d = 0; d < plane; d++) {
|
||||
const s = d * 4;
|
||||
src[d] = small[s];
|
||||
src[plane + d] = small[s + 1];
|
||||
src[2 * plane + d] = small[s + 2];
|
||||
if (smallHole[d]) {
|
||||
src[d] = byte(field.r[d]);
|
||||
src[plane + d] = byte(field.g[d]);
|
||||
src[2 * plane + d] = byte(field.b[d]);
|
||||
mask[d] = 0;
|
||||
} else {
|
||||
src[d] = small[s];
|
||||
src[plane + d] = small[s + 1];
|
||||
src[2 * plane + d] = small[s + 2];
|
||||
}
|
||||
}
|
||||
return { src, hole: mask };
|
||||
@@ -571,22 +566,25 @@ export async function inpaintCrop(
|
||||
w = Math.max(8, Math.floor((w * scale) / 8) * 8);
|
||||
h = Math.max(8, Math.floor((h * scale) / 8) * 8);
|
||||
|
||||
// Quick signature to avoid redundant re-inferences on subsequent renders
|
||||
let holeCount = 0;
|
||||
let holeHash = 0;
|
||||
const step = Math.max(1, Math.floor(hole.length / 64));
|
||||
for (let i = 0; i < hole.length; i += step) {
|
||||
if (hole[i]) {
|
||||
holeCount++;
|
||||
holeHash = (holeHash * 31 + i) | 0;
|
||||
// Quick signature to avoid redundant re-inferences on subsequent renders.
|
||||
// When patchKey is supplied via (hole as any).patchKey or parameter, it is based on
|
||||
// normalized coordinates and is invariant to zoom in/out preview resamplings.
|
||||
const patchKey = (hole as { patchKey?: string }).patchKey;
|
||||
let key: string;
|
||||
if (patchKey) {
|
||||
key = patchKey;
|
||||
} else {
|
||||
let holeCount = 0;
|
||||
let holeHash = 0;
|
||||
const step = Math.max(1, Math.floor(hole.length / 64));
|
||||
for (let i = 0; i < hole.length; i += step) {
|
||||
if (hole[i]) {
|
||||
holeCount++;
|
||||
holeHash = (holeHash * 31 + i) | 0;
|
||||
}
|
||||
}
|
||||
key = `${w}x${h}_${holeCount}_${holeHash}`;
|
||||
}
|
||||
let pixHash = 0;
|
||||
const pixStep = Math.max(4, Math.floor(rgba.length / 64) * 4);
|
||||
for (let i = 0; i < rgba.length; i += pixStep) {
|
||||
pixHash = (pixHash * 33 + rgba[i] + rgba[i + 1]) | 0;
|
||||
}
|
||||
const key = `${w}x${h}_${holeCount}_${holeHash}_${pixHash}`;
|
||||
|
||||
if (patchCache.has(key)) {
|
||||
return patchCache.get(key)!.slice();
|
||||
|
||||
Reference in New Issue
Block a user