/** List differing pixels (Δ>t) between a truth crop and a shot: bun bench/slint/diffpx.ts truth.png x y w h shot.png [t] [skipRowsCsv] */
import { readFileSync } from 'node:fs'
import { crop, decodePng, pixelHex } from '../../tools/bench/png'
const [tf, xs, ys, ws, hs, sf, ts] = process.argv.slice(2)
if (!sf) throw new Error('usage: diffpx truth.png x y w h shot.png [t]')
const [x, y, w, h] = [xs, ys, ws, hs].map(Number) as [number, number, number, number]
const t = crop(decodePng(readFileSync(tf!)), x, y, w, h), o = decodePng(readFileSync(sf))
const th = Number(ts ?? 2)
const rows = new Map<number, string[]>()
for (let r = 0; r < h; r++) for (let c = 0; c < w; c++) {
const i = (r * w + c) * 4
let d = 0
for (let k = 0; k < 4; k++) d = Math.max(d, Math.abs(t.data[i + k]! - o.data[i + k]!))
if (d > th) { const a = rows.get(r) ?? []; a.push(`${c}:${pixelHex(t, c, r)}/${pixelHex(o, c, r)}`); rows.set(r, a) }
}
for (const [r, a] of rows) console.log(`y${r} (${a.length}) ${a.slice(0, 8).join(' ')}${a.length > 8 ? ' …' : ''}`)
console.log(rows.size ? '' : 'no pixels over ' + th)