// Copy the yellow sparkles back from the cut-out into the refined pose. // refine-edges.cjs treats any bright shape with no dark outline as fringe, so it strips loose sparkles. const sharp = require("module").createRequire("/home/ubuntu/kalki-cli/projects/mybazaar-web/package.json")("sharp"); const [, , cutFile, refinedFile, box] = process.argv; const [x0, y0, x1, y1] = box.split(",").map(Number); (async () => { const cut = await sharp(cutFile).ensureAlpha().raw().toBuffer({ resolveWithObject: true }); const ref = await sharp(refinedFile).ensureAlpha().raw().toBuffer({ resolveWithObject: true }); const { width, height } = ref.info; const yellow = (d, i) => d[i + 3] > 0 && d[i] > 150 && d[i + 1] > 110 && d[i + 2] < 160; let n = 0; for (let y = y0; y <= y1; y++) for (let x = x0; x <= x1; x++) { const i = (y * width + x) * 4; if (yellow(cut.data, i)) { cut.data.copy(ref.data, i, i, i + 4); n++; } } await sharp(ref.data, { raw: { width, height, channels: 4 } }).png().toFile(refinedFile + ".t.png"); require("node:fs").renameSync(refinedFile + ".t.png", refinedFile); console.log(`restored ${n} sparkle px`); })();