| // usage: corepack pnpm tsx scripts/blitz-experiment/magnify.mts out.png scale x,y,w,h file1 file2 ... → crops stacked, nearest-neighbour upscaled |
| 5 | const [out, scaleS, cropS, ...files] = process.argv.slice(2); |
| 6 | const scale = Number(scaleS); const [x, y, w, h] = cropS.split(',').map(Number); |
| 7 | const gap = 4; |
| 8 | const o = new PNG({ width: w * scale, height: (h * scale + gap) * files.length - gap }); o.data.fill(255); |
| 9 | files.forEach((f, k) => { |
| 10 | const img = PNG.sync.read(fs.readFileSync(f)); |
| 11 | for (let j = 0; j < h; j++) for (let i = 0; i < w; i++) { |
| 12 | const si = ((y + j) * img.width + (x + i)) * 4; if (y + j >= img.height || x + i >= img.width) continue; |
| 13 | for (let dy = 0; dy < scale; dy++) for (let dx = 0; dx < scale; dx++) { |
| 14 | const di = ((k * (h * scale + gap) + j * scale + dy) * o.width + i * scale + dx) * 4; |
| 15 | // composite over white (premultiplied sources were saved straight by the harness) |
| 16 | const a = img.data[si + 3] / 255; |
| 17 | for (let c = 0; c < 3; c++) o.data[di + c] = Math.round(img.data[si + c] * a + 255 * (1 - a)); |
| 18 | o.data[di + 3] = 255; |
| 19 | } |
| 20 | } |
| 21 | }); |
| 22 | fs.writeFileSync(out, PNG.sync.write(o)); |
| 23 |