Newer
Older
rpg / v2 / apps / game / tools / crop-zoom.mjs
/**
 * Зум-кроп скриншота для визуальной проверки агентом.
 * Использование: node tools/crop-zoom.mjs <in.png> <out.png> cx cy half scale
 * (cx, cy — центр кропа в пикселях, half — полуширина, scale — кратность).
 */
import { readFileSync, writeFileSync } from 'node:fs';
import { decodePng, encodePng } from '@rpg/engine/tools/png.mjs';

const [inPath, outPath, cx, cy, half, scale] = process.argv.slice(2);
const img = decodePng(readFileSync(inPath));
const { width: W, height: H, rgba } = img;
const px = Math.max(0, Number(cx) - Number(half));
const py = Math.max(0, Number(cy) - Number(half));
const sw = Math.min(W - px, Number(half) * 2);
const sh = Math.min(H - py, Number(half) * 2);
const s = Number(scale);
const out = Buffer.alloc(sw * sh * s * s * 4);
for (let y = 0; y < sh * s; y++)
    for (let x = 0; x < sw * s; x++) {
        const sx = px + Math.floor(x / s), sy = py + Math.floor(y / s);
        const si = (sy * W + sx) * 4, di = (y * sw * s + x) * 4;
        out[di] = rgba[si]; out[di + 1] = rgba[si + 1];
        out[di + 2] = rgba[si + 2]; out[di + 3] = 255;
    }
writeFileSync(outPath, encodePng(sw * s, sh * s, out));
console.log('ok', outPath);