Claude Opus 5.5

render.js

#!/usr/bin/env bun
// 「土谷大地」を PNG に書き出す。
//   bun render.js [--w 2400] [--out 出力先] [--name 名前] [--until ラベル] [--crop x,y,w,h ...] [--half]
const fs = require('fs');
const path = require('path');
const zlib = require('zlib');
const { createPainting } = require('./paint.js');

const args = { w: 2400, out: __dirname, crop: [], half: false, until: null, name: '土谷大地' };
for (let i = 2; i < process.argv.length; i++) {
  const a = process.argv[i];
  if (a === '--w') args.w = +process.argv[++i];
  else if (a === '--out') args.out = process.argv[++i];
  else if (a === '--until') args.until = process.argv[++i];
  else if (a === '--name') args.name = process.argv[++i];
  else if (a === '--crop') args.crop.push(process.argv[++i].split(',').map(Number));
  else if (a === '--half') args.half = true;
  else if (a === '--seed') args.seed = +process.argv[++i];
}

const CRC = new Uint32Array(256);
for (let n = 0; n < 256; n++) { let c = n; for (let k = 0; k < 8; k++) c = c & 1 ? 0xedb88320 ^ (c >>> 1) : c >>> 1; CRC[n] = c >>> 0; }
function crc32(buf) { let c = 0xffffffff; for (let i = 0; i < buf.length; i++) c = CRC[(c ^ buf[i]) & 255] ^ (c >>> 8); return (c ^ 0xffffffff) >>> 0; }
function chunk(type, data) {
  const out = Buffer.alloc(12 + data.length);
  out.writeUInt32BE(data.length, 0); out.write(type, 4, 'latin1'); data.copy(out, 8);
  out.writeUInt32BE(crc32(out.subarray(4, 8 + data.length)), 8 + data.length);
  return out;
}
// RGBA → RGB PNG。行ごとに 5 種のフィルタから絶対値和が最小のものを選ぶ
function encodePNG(rgba, w, h) {
  const bpp = 3, stride = w * bpp;
  const raw = Buffer.alloc((stride + 1) * h);
  const cur = new Uint8Array(stride), prev = new Uint8Array(stride);
  const cand = [0, 1, 2, 3, 4].map(() => new Uint8Array(stride));
  for (let y = 0; y < h; y++) {
    for (let x = 0; x < w; x++) { const s = (y * w + x) * 4, d = x * 3; cur[d] = rgba[s]; cur[d + 1] = rgba[s + 1]; cur[d + 2] = rgba[s + 2]; }
    const sums = [0, 0, 0, 0, 0];
    for (let i = 0; i < stride; i++) {
      const a = i >= bpp ? cur[i - bpp] : 0, b = prev[i], c = i >= bpp ? prev[i - bpp] : 0, v = cur[i];
      const p = a + b - c, pa = Math.abs(p - a), pb = Math.abs(p - b), pc = Math.abs(p - c);
      const pae = pa <= pb && pa <= pc ? a : pb <= pc ? b : c;
      const f = [v, v - a, v - b, v - ((a + b) >> 1), v - pae];
      for (let k = 0; k < 5; k++) { const q = f[k] & 255; cand[k][i] = q; sums[k] += q < 128 ? q : 256 - q; }
    }
    let best = 0; for (let k = 1; k < 5; k++) if (sums[k] < sums[best]) best = k;
    const o = y * (stride + 1); raw[o] = best; raw.set(cand[best], o + 1);
    prev.set(cur);
  }
  const ihdr = Buffer.alloc(13); ihdr.writeUInt32BE(w, 0); ihdr.writeUInt32BE(h, 4); ihdr[8] = 8; ihdr[9] = 2;
  return Buffer.concat([Buffer.from([137, 80, 78, 71, 13, 10, 26, 10]), chunk('IHDR', ihdr), chunk('IDAT', zlib.deflateSync(raw, { level: 7 })), chunk('IEND', Buffer.alloc(0))]);
}
function crop(rgba, W, x, y, w, h) {
  const out = new Uint8ClampedArray(w * h * 4);
  for (let j = 0; j < h; j++) out.set(rgba.subarray(((y + j) * W + x) * 4, ((y + j) * W + x + w) * 4), j * w * 4);
  return out;
}
function halve(rgba, W, H) {
  const w = W >> 1, h = H >> 1, out = new Uint8ClampedArray(w * h * 4);
  for (let y = 0; y < h; y++) for (let x = 0; x < w; x++) for (let c = 0; c < 4; c++) {
    const i = (2 * y * W + 2 * x) * 4 + c;
    out[(y * w + x) * 4 + c] = (rgba[i] + rgba[i + 4] + rgba[i + W * 4] + rgba[i + W * 4 + 4] + 2) >> 2;
  }
  return out;
}

fs.mkdirSync(args.out, { recursive: true });
const t0 = performance.now();
const p = createPainting({ width: args.w, seed: args.seed });
let last = t0;
for (const step of p.run()) {
  const now = performance.now();
  console.log(`${String(Math.round(now - last)).padStart(6)} ms  ${step.label}`);
  last = now;
  if (args.until && step.label === args.until) break;
}
const rgba = p.compose();
console.log(`total ${(performance.now() - t0) / 1000 | 0}s  ${p.W}x${p.H}`);
fs.writeFileSync(path.join(args.out, `${args.name}.png`), encodePNG(rgba, p.W, p.H));
if (args.half) fs.writeFileSync(path.join(args.out, `${args.name}_half.png`), encodePNG(halve(rgba, p.W, p.H), p.W >> 1, p.H >> 1));
args.crop.forEach((c, i) => {
  const [x, y, w, h] = c.map(v => Math.round(v));
  fs.writeFileSync(path.join(args.out, `${args.name}_crop${i}.png`), encodePNG(crop(rgba, p.W, x, y, w, h), w, h));
});