Claude Fable 5.2max

render.js

// bun render.js [--s 16] [--view x,y,w,h] [--out 土谷大地.png] [--until 工程名] [--only 工程名,…]
// --view は cm(本紙の左上が 0,0、本紙は 360×160)。切り出しだけを高い倍率で描ける。
const B = require('./engine.js');
require('./plants.js');
require('./scene.js');
const { deflateSync } = require('node:zlib');
const { writeFileSync } = require('node:fs');

const arg = (k, d) => { const i = process.argv.indexOf('--' + k); return i > 0 ? process.argv[i + 1] : d; };
const S = +arg('s', 16);
const view = arg('view', null);
const out = arg('out', '土谷大地.png');
const t0 = performance.now();
const E = B.create({ S, view: view ? view.split(',').map(Number) : null });
B.paint(E, { until: arg('until', null), only: arg('only', null), log: (m) => console.log(m, Math.round(performance.now() - t0) + 'ms') });
const rgba = new Uint8ClampedArray(E.W * E.H * 4);
E.develop(rgba);
console.log('develop', Math.round(performance.now() - t0) + 'ms', E.W + 'x' + E.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;
}
const { W, H } = E;
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 = y * W * 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;
writeFileSync(out, 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))]));
console.log('wrote', out, Math.round(performance.now() - t0) + 'ms');