|
| 1 | +import { mkdirSync, readFileSync, writeFileSync } from 'node:fs'; |
| 2 | +import path from 'node:path'; |
| 3 | +import { PNG } from '@agent-device/capture-kit/png'; |
| 4 | + |
| 5 | +/** |
| 6 | + * The captures and crop boxes this benchmark measures. They are generated rather than recorded, |
| 7 | + * so a run costs seconds and needs no device; each one reports its own compressed size, which is |
| 8 | + * how a corpus that stopped resembling a real capture becomes visible instead of flattering. |
| 9 | + * Pass real captures with `--file` to put their numbers in the same table. |
| 10 | + */ |
| 11 | + |
| 12 | +export type CaptureResolution = Readonly<{ |
| 13 | + name: string; |
| 14 | + label: string; |
| 15 | + width: number; |
| 16 | + height: number; |
| 17 | +}>; |
| 18 | + |
| 19 | +const RESOLUTIONS: readonly CaptureResolution[] = [ |
| 20 | + { name: 'ios-phone', label: 'iPhone-class 1206x2622', width: 1206, height: 2622 }, |
| 21 | + { name: 'android-phone', label: 'Android-class 1080x2400', width: 1080, height: 2400 }, |
| 22 | + { name: 'ios-pad', label: 'iPad-class 2048x2732', width: 2048, height: 2732 }, |
| 23 | +]; |
| 24 | + |
| 25 | +export type Capture = Readonly<{ |
| 26 | + name: string; |
| 27 | + label: string; |
| 28 | + width: number; |
| 29 | + height: number; |
| 30 | + bytes: Buffer; |
| 31 | +}>; |
| 32 | + |
| 33 | +export type CropScenario = Readonly<{ |
| 34 | + name: string; |
| 35 | + /** Fractions of the capture, so one scenario reads well at every resolution. */ |
| 36 | + fraction: Readonly<{ x: number; y: number; width: number; height: number }>; |
| 37 | +}>; |
| 38 | + |
| 39 | +/** The box shapes `--crop-on` resolves: a card, a full-bleed header, and a small control. */ |
| 40 | +export const CROP_SCENARIOS: readonly CropScenario[] = [ |
| 41 | + { name: 'card', fraction: { x: 0.08, y: 0.25, width: 0.8, height: 0.2 } }, |
| 42 | + { name: 'header', fraction: { x: 0, y: 0, width: 1, height: 0.12 } }, |
| 43 | + { name: 'control', fraction: { x: 0.3, y: 0.6, width: 0.3, height: 0.04 } }, |
| 44 | +]; |
| 45 | + |
| 46 | +export function buildCorpus( |
| 47 | + outDir: string, |
| 48 | + resolutions: readonly CaptureResolution[] = RESOLUTIONS, |
| 49 | +): Capture[] { |
| 50 | + mkdirSync(outDir, { recursive: true }); |
| 51 | + return resolutions.flatMap(({ name, label, width, height }) => |
| 52 | + (['interface', 'photo'] as const).map((kind) => { |
| 53 | + const bytes = PNG.sync.write(encodeCapture(width, height, kind)); |
| 54 | + writeFileSync(path.join(outDir, `${name}-${kind}.png`), bytes); |
| 55 | + return { name: `${name}-${kind}`, label: `${label} ${kind}`, width, height, bytes }; |
| 56 | + }), |
| 57 | + ); |
| 58 | +} |
| 59 | + |
| 60 | +export function readCaptureFile(filePath: string, index: number): Capture { |
| 61 | + const bytes = readFileSync(filePath); |
| 62 | + const png = PNG.sync.read(bytes); |
| 63 | + return { |
| 64 | + name: `capture-${index}`, |
| 65 | + label: path.basename(filePath), |
| 66 | + width: png.width, |
| 67 | + height: png.height, |
| 68 | + bytes, |
| 69 | + }; |
| 70 | +} |
| 71 | + |
| 72 | +type Content = 'interface' | 'photo'; |
| 73 | + |
| 74 | +/** |
| 75 | + * A capture a device could have produced. `interface` is flat panels with high-frequency text |
| 76 | + * where the copy is drawn, which is what a settings or list screen looks like. `photo` is |
| 77 | + * low-frequency detail in 8px blocks plus a gradient, which is what a photo, artwork, or a blurred |
| 78 | + * background does to the deflate stream. |
| 79 | + */ |
| 80 | +function encodeCapture(width: number, height: number, content: Content): PNG { |
| 81 | + const png = new PNG({ width, height }); |
| 82 | + const tones = content === 'photo' ? photoToneField(width, height) : null; |
| 83 | + for (let y = 0; y < height; y += 1) { |
| 84 | + for (let x = 0; x < width; x += 1) { |
| 85 | + const offset = (y * width + x) * 4; |
| 86 | + const [red, green, blue] = tones ? photoPixel(tones, x, y) : interfacePixel(x, y); |
| 87 | + png.data[offset] = red; |
| 88 | + png.data[offset + 1] = green; |
| 89 | + png.data[offset + 2] = blue; |
| 90 | + png.data[offset + 3] = 255; |
| 91 | + } |
| 92 | + } |
| 93 | + return png; |
| 94 | +} |
| 95 | + |
| 96 | +/** Flat panels with high-frequency text where the copy is drawn. */ |
| 97 | +function interfacePixel(x: number, y: number): readonly [number, number, number] { |
| 98 | + const ink = x % 320 < 2 || y % 96 < 2 || (y % 24 > 6 && y % 24 < 18 && x % 7 < 3); |
| 99 | + if (ink) return [24, 26, 30]; |
| 100 | + const band = Math.floor(y / 96); |
| 101 | + return [(band * 11) % 240, (band * 17) % 244, (band * 23) % 248]; |
| 102 | +} |
| 103 | + |
| 104 | +/** The 8px-block detail and soft gradient of a photo, scaled by the pixel position. */ |
| 105 | +function photoPixel( |
| 106 | + tones: readonly (readonly number[])[], |
| 107 | + x: number, |
| 108 | + y: number, |
| 109 | +): readonly [number, number, number] { |
| 110 | + const tone = tones[y >> 3]?.[x >> 3] ?? 0; |
| 111 | + return [ |
| 112 | + clampByte(tone + y / 6), |
| 113 | + clampByte(tone * 0.8 + x / 9), |
| 114 | + clampByte(tone * 0.6 + (x + y) / 24), |
| 115 | + ]; |
| 116 | +} |
| 117 | + |
| 118 | +/** Smooth 8px-block luminance in [40, 215], so a photo corpus compresses like a real photo. */ |
| 119 | +function photoToneField(width: number, height: number): number[][] { |
| 120 | + let state = 0x9e3779b9; |
| 121 | + const next = () => { |
| 122 | + state = (state * 1664525 + 1013904223) >>> 0; |
| 123 | + return state / 0x100000000; |
| 124 | + }; |
| 125 | + return Array.from({ length: Math.ceil(height / 8) }, (_row, blockRow) => |
| 126 | + Array.from({ length: Math.ceil(width / 8) }, (_column, blockColumn) => { |
| 127 | + const drift = Math.sin(blockRow / 26) * 45 + Math.cos(blockColumn / 19) * 45; |
| 128 | + return clampByte(128 + drift + (next() - 0.5) * 70); |
| 129 | + }), |
| 130 | + ); |
| 131 | +} |
| 132 | + |
| 133 | +function clampByte(value: number): number { |
| 134 | + return Math.max(0, Math.min(255, Math.round(value))); |
| 135 | +} |
| 136 | + |
| 137 | +export function cropBoxOf( |
| 138 | + capture: Capture, |
| 139 | + scenario: CropScenario, |
| 140 | +): { |
| 141 | + x: number; |
| 142 | + y: number; |
| 143 | + width: number; |
| 144 | + height: number; |
| 145 | +} { |
| 146 | + const { fraction } = scenario; |
| 147 | + const x = Math.round(capture.width * fraction.x); |
| 148 | + const y = Math.round(capture.height * fraction.y); |
| 149 | + return { |
| 150 | + x, |
| 151 | + y, |
| 152 | + width: Math.max(1, Math.min(capture.width - x, Math.round(capture.width * fraction.width))), |
| 153 | + height: Math.max(1, Math.min(capture.height - y, Math.round(capture.height * fraction.height))), |
| 154 | + }; |
| 155 | +} |
0 commit comments