Claude Opus 5.5
render.js
// bun render.js [scale] [out.png] [x,y,w,h ...] — PNG に書き出す(切り出しは設計座標)
import './sumi.js';
import { deflateSync } from 'node:zlib';
import { writeFileSync } from 'node:fs';
const [scale = '1', out = '土谷大地.png', ...crops] = process.argv.slice(2);
const p = globalThis.Sumi.create({ scale: +scale });
const t0 = performance.now();
for (let i = 0; i < p.steps.length; i++) {
const t = performance.now();
p.run(i);
console.log(p.steps[i], Math.round(performance.now() - t) + 'ms');
}
const rgba = new Uint8ClampedArray(p.W * p.H * 4);
p.composite(rgba);
console.log('total', Math.round(performance.now() - t0) + 'ms', p.W + 'x' + p.H);
const CRC = new Uint32Array(256).map((_, n) => { let c = n; for (let k = 0; k < 8; k++) c = c & 1 ? 0xedb88320 ^ (c >>> 1) : c >>> 1; return c >>> 0; });
function chunk(type, data) {
const b = Buffer.alloc(12 + data.length);
b.writeUInt32BE(data.length, 0); b.write(type, 4, 'ascii'); data.copy(b, 8);
let c = 0xffffffff;
for (let i = 4; i < 8 + data.length; i++) c = CRC[(c ^ b[i]) & 255] ^ (c >>> 8);
b.writeUInt32BE((c ^ 0xffffffff) >>> 0, 8 + data.length);
return b;
}
function png(x0, y0, w, h) {
const raw = Buffer.alloc((w * 3 + 1) * h);
for (let y = 0, o = 0; y < h; y++) {
raw[o++] = 0;
for (let x = 0, i = ((y0 + y) * p.W + x0) * 4; x < w; x++, i += 4) { raw[o++] = rgba[i]; raw[o++] = rgba[i + 1]; raw[o++] = rgba[i + 2]; }
}
const ih = Buffer.alloc(13);
ih.writeUInt32BE(w, 0); ih.writeUInt32BE(h, 4); ih[8] = 8; ih[9] = 2;
return Buffer.concat([Buffer.from([137, 80, 78, 71, 13, 10, 26, 10]), chunk('IHDR', ih), chunk('IDAT', deflateSync(raw, { level: 6 })), chunk('IEND', Buffer.alloc(0))]);
}
writeFileSync(out, png(0, 0, p.W, p.H));
crops.forEach((c, k) => {
const [x, y, w, h] = c.split(',').map((v) => Math.round(+v * +scale));
writeFileSync(out.replace(/\.png$/, `.crop${k}.png`), png(x, y, Math.min(w, p.W - x), Math.min(h, p.H - y)));
});