feat(m4): cap map images at Full HD and shrink on import (#85)

The owner fixed the map cap at 1920 px on the long side and 1080 px on the
short side (either orientation). fff-vtt import now shrinks a larger source
with ImageMagick Lanczos before tiling (alpha kept, proportions preserved,
no upscaling), prints `scaled WxH -> WxH`, and records the shrunk w/h; a
source at or below the cap is untouched, so the pixel-identity tests still
hold. A scene row already above the cap is not rewritten: the server logs
`map_above_cap` at startup and serve prints the re-import advice.

Docs: ADR-0023, DESIGN 3.3/5.3/8/11.1/13/14, TESTING 5.3/13, README,
HANDOFF-M4, client README. The M4 phone gate and the fogbench measurement
now use the Full HD maximum; the 16384 px / 262144-cell bench and test rows
stay as labelled server stress references. import-scale now proves a 16384²
input is shrunk to 1080x1080 (289 tiles at 64 px) quickly and bounded.

Part of #63, #85. #77 is blocked by this issue.
This commit is contained in:
2026-11-21 22:37:34 +01:00
parent 52ab50d765
commit a79d809394
22 changed files with 515 additions and 193 deletions
+66
View File
@@ -10,6 +10,7 @@ import assert from 'node:assert/strict'
import fs from 'node:fs'
import path from 'node:path'
import { spawnSync } from 'node:child_process'
import { DatabaseSync } from 'node:sqlite'
import { createPlayer, freePort, httpsBaseUrl, namePlayer } from './lib.mjs'
import { buildServer, gmKeyOf, makeStateDir, openAs, startServer, stopServer } from './withserver.mjs'
import { startTestNginx, stopTestNginx } from './nginx.mjs'
@@ -192,6 +193,71 @@ test('map import CLI: the streamed slice matches the one-shot crop pixel for pix
}
})
function sceneDims(stateDir) {
const db = new DatabaseSync(path.join(stateDir, 'state.db'), { readOnly: true })
try {
const row = db.prepare('SELECT w, h FROM scene WHERE id = 1').get()
return [row.w, row.h]
} finally {
db.close()
}
}
test('map import CLI: images above Full HD are shrunk to the cap, smaller ones untouched (#85)', () => {
const stateDir = makeStateDir()
try {
const cases = [
// [name, source w/h, expected scene w/h, expected stdout]
['landscape', 4000, 3000, 1440, 1080, 'scaled 4000×3000 → 1440×1080'],
['portrait', 3000, 4000, 1080, 1440, 'scaled 3000×4000 → 1080×1440'],
['square', 2000, 2000, 1080, 1080, 'scaled 2000×2000 → 1080×1080'],
['exactly-at-cap', 1920, 1080, 1920, 1080, null],
['one-px-over', 1921, 1080, 1920, 1079, 'scaled 1921×1080 → 1920×1079'],
['smaller', 256, 192, 256, 192, null],
]
for (const [name, w, h, wantW, wantH, scaled] of cases) {
const state = path.join(stateDir, name)
const img = path.join(state, `${name}.png`)
fs.mkdirSync(state, { recursive: true })
fs.writeFileSync(img, pngBytesWH(w, h, (x, y) => [x & 0xff, y & 0xff, 128]))
const imported = runImport(state, [`--image=${img}`, '--tile-size=64'])
assert.equal(imported.status, 0, `${name}: ${imported.stderr}`)
assert.deepEqual(sceneDims(state), [wantW, wantH], `${name}: scene row is the imported size`)
assert.match(imported.stdout, new RegExp(`${wantW}x${wantH} px, tile 64 px`),
`${name}: stdout reports the final size`)
if (scaled) assert.ok(imported.stdout.includes(scaled), `${name}: shrink is printed: ${imported.stdout}`)
else assert.equal(imported.stdout.includes('scaled '), false, `${name}: no needless resize`)
const cols = Math.ceil(wantW / 64)
const rows = Math.ceil(wantH / 64)
assert.equal(tileNames(path.join(state, 'tiles', '1', 'v1')).length, cols * rows,
`${name}: the tile grid follows the shrunk size`)
}
// Alpha survives the resize: a 50% transparent RGBA source is still RGBA at the cap size.
const alphaState = path.join(stateDir, 'alpha')
const alphaImg = path.join(alphaState, 'alpha.png')
fs.mkdirSync(alphaState, { recursive: true })
const made = spawnSync('magick', ['-size', '2000x1500', 'xc:rgba(255,0,0,0.5)',
'-depth', '8', alphaImg], { encoding: 'utf8' })
assert.equal(made.status, 0, made.stderr)
const alphaRun = runImport(alphaState, [`--image=${alphaImg}`, '--tile-size=64'])
assert.equal(alphaRun.status, 0, alphaRun.stderr)
assert.deepEqual(sceneDims(alphaState), [1440, 1080])
const tile = path.join(alphaState, 'tiles', '1', 'v1', '0_0.png')
const channels = spawnSync('magick', ['identify', '-format', '%[channels]', tile],
{ encoding: 'utf8' })
assert.equal(channels.status, 0, channels.stderr)
assert.match(channels.stdout, /a/, 'the tile still has an alpha channel')
const alphaMean = spawnSync('magick', [tile, '-format', '%[fx:mean.a]', 'info:'],
{ encoding: 'utf8' })
assert.equal(alphaMean.status, 0, alphaMean.stderr)
assert.ok(Math.abs(Number(alphaMean.stdout) - 0.50196) < 0.02,
`the alpha value survives the resize: ${alphaMean.stdout}`)
} finally {
fs.rmSync(stateDir, { recursive: true, force: true })
}
})
test('map import CLI: a truncated source fails loudly and publishes nothing (#83)', () => {
// The streaming path reads the decoder's stdout band by band; a decode that dies mid-stream must
// surface magick's own message, not an empty tile tree or a byte-count mystery.