Skip to content
Source
Sign inSign up
TanStack/ai
Open History to see the last commit

make-repro-image.mjs

123 lines3.7 KB

1// One-off generator for the issue #363 repro image.
2// Renders a fixed 3-digit secret number as black pixels on white.
3// The number is impossible to guess (1/1000) and appears nowhere in any text
4// the tool returns, so a model that genuinely *sees* the image can read it and
5// a model that only received stringified JSON cannot. Writes the PNG to
6// public/ and prints its base64 for embedding in the tool.
7import { deflateSync } from 'node:zlib'
8import { writeFileSync } from 'node:fs'
9import { fileURLToPath } from 'node:url'
10import { dirname, join } from 'node:path'
11​
12// Keep this in sync with REPRO_SECRET in src/lib/image-tool-repro.ts
13const SECRET = '473'
14const CELL = 16 // px per font cell
15const MARGIN = 24
16const GAP = CELL // gap between digits
17​
18// 3x5 bitmap font for digits 0-9.
19const FONT = {
20 0: ['111', '101', '101', '101', '111'],
21 1: ['010', '110', '010', '010', '111'],
22 2: ['111', '001', '111', '100', '111'],
23 3: ['111', '001', '111', '001', '111'],
24 4: ['101', '101', '111', '001', '001'],
25 5: ['111', '100', '111', '001', '111'],
26 6: ['111', '100', '111', '101', '111'],
27 7: ['111', '001', '010', '010', '010'],
28 8: ['111', '101', '111', '101', '111'],
29 9: ['111', '101', '111', '001', '111'],
31​
32const digits = [...SECRET]
33const digitW = 3 * CELL
34const digitH = 5 * CELL
35const width = MARGIN * 2 + digits.length * digitW + (digits.length - 1) * GAP
36const height = MARGIN * 2 + digitH
37​
38// White background, black digit pixels.
39const px = new Uint8Array(width * height * 3).fill(255)
40function setBlack(x, y) {
41 const i = (y * width + x) * 3
42 px[i] = 0
43 px[i + 1] = 0
44 px[i + 2] = 0
46​
47digits.forEach((d, di) => {
48 const glyph = FONT[d]
49 const originX = MARGIN + di * (digitW + GAP)
50 for (let gy = 0; gy < 5; gy++) {
51 for (let gx = 0; gx < 3; gx++) {
52 if (glyph[gy][gx] !== '1') continue
53 for (let cy = 0; cy < CELL; cy++) {
54 for (let cx = 0; cx < CELL; cx++) {
55 setBlack(originX + gx * CELL + cx, MARGIN + gy * CELL + cy)
56 }
57 }
58 }
59 }
60})
61​
62// Pack into PNG scanlines (filter byte 0 per row).
63const raw = Buffer.alloc((width * 3 + 1) * height)
64let o = 0
65for (let y = 0; y < height; y++) {
66 raw[o++] = 0
67 for (let x = 0; x < width; x++) {
68 const i = (y * width + x) * 3
69 raw[o++] = px[i]
70 raw[o++] = px[i + 1]
71 raw[o++] = px[i + 2]
72 }
74​
75// CRC32 (PNG chunks require it).
76const crcTable = (() => {
77 const t = new Uint32Array(256)
78 for (let n = 0; n < 256; n++) {
79 let c = n
80 for (let k = 0; k < 8; k++) c = c & 1 ? 0xedb88320 ^ (c >>> 1) : c >>> 1
81 t[n] = c >>> 0
82 }
83 return t
84})()
85function crc32(buf) {
86 let c = 0xffffffff
87 for (let i = 0; i < buf.length; i++)
88 c = crcTable[(c ^ buf[i]) & 0xff] ^ (c >>> 8)
89 return (c ^ 0xffffffff) >>> 0
91​
92function chunk(type, data) {
93 const typeBuf = Buffer.from(type, 'ascii')
94 const len = Buffer.alloc(4)
95 len.writeUInt32BE(data.length, 0)
96 const crc = Buffer.alloc(4)
97 crc.writeUInt32BE(crc32(Buffer.concat([typeBuf, data])), 0)
98 return Buffer.concat([len, typeBuf, data, crc])
100​
101const ihdr = Buffer.alloc(13)
102ihdr.writeUInt32BE(width, 0)
103ihdr.writeUInt32BE(height, 4)
104ihdr[8] = 8 // bit depth
105ihdr[9] = 2 // colour type: truecolour RGB
106ihdr[10] = 0
107ihdr[11] = 0
108ihdr[12] = 0
109​
110const png = Buffer.concat([
111 Buffer.from([0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a]),
112 chunk('IHDR', ihdr),
113 chunk('IDAT', deflateSync(raw)),
114 chunk('IEND', Buffer.alloc(0)),
115])
116​
117const here = dirname(fileURLToPath(import.meta.url))
118const outPath = join(here, '..', 'public', 'repro-secret.png')
119writeFileSync(outPath, png)
120console.log('Wrote', outPath, `(${png.length} bytes, secret=${SECRET})`)
121console.log('BASE64_START')
122console.log(png.toString('base64'))
123console.log('BASE64_END')