plate-engine - v0.1.0
    Preparing search index...

    plate-engine - v0.1.0

    plate-engine

    Procedural engravings and the dither that prints them, in TypeScript with no runtime dependencies.

    The eleven generators, each printed in three tones with the 8×8 Bayer plate dither

    It is the engine behind every image on speyyman.github.io: generators that draw a fern, a sunflower, the moon and eight other subjects from a seed and a few numbers, and a dither that reduces any picture to a handful of tones. The golden tests check that it produces the site's plates bit for bit.

    npm install plate-engine
    

    ES modules only, with TypeScript types included. Runs in browsers, web workers and Node 20 or later.

    import { generate, renderCPU, palettes, encodeBMP, dither, grayPalette } from 'plate-engine'

    // Draw a fern into a 300 × 500 luminance buffer (0 = dark, 1 = light).
    const lum = generate('fern', 300, 500, { seed: 7, params: { curl: 0.06 } })

    // Dither it to three tones and paint it. Works in browsers, workers and Node.
    const image = renderCPU(lum, 300, 500, { method: 'atkinson', palette: palettes.herbarium })

    // Or keep the tone indices and save a 4-bit greyscale BMP for an e-paper panel.
    const idx = dither(lum, 300, 500, { method: 'floyd-steinberg', levels: 16 })
    const bmp = encodeBMP(idx, 300, 500, grayPalette(16), 4)

    In a browser, renderAuto uses one shared WebGL1 context for the ordered methods and falls back to the CPU when WebGL is missing or the context is lost. imageToLuminance turns an <img>, ImageBitmap or canvas into a buffer, with the contrast curve the site uses for scanned engravings.

    • Generators (11): sunburst, sphere, orbits, eye, ridges, medallion, fern, phyllotaxis, branches, chladni, moon. Each implements one interface, draw(width, height, rng, params) => Float32Array, and documents its parameters (label, description, default, range, step). Defaults reproduce the original plates. Write your own with defineGenerator.
    • Dither (7 methods): threshold; ordered Bayer 2×2, 4×4 and 8×8; ordered blue noise with a 64×64 void-and-cluster mask; Floyd–Steinberg; Atkinson. Any number of tones from 2 to 16, any palette, optional seeded grain.
    • Renderers: CPU (anywhere) and WebGL1 (browsers, ordered methods). On the same 8-bit input they produce identical pixels; see BENCHMARKS.md.
    • Exporters: PNG (RGBA, palette-indexed at 1, 2, 4 or 8 bits, 8-bit greyscale) and BMP (1-, 4-, 8- and 24-bit). PNG compression uses the platform's CompressionStream; without it the encoder writes valid uncompressed files.
    • Helpers: Mulberry32 PRNG, the moon's phase for any date, palette parsing, brightness/contrast/gamma, rotate, nearest-neighbour scale, fit and crop.

    One sphere, two tones, every method: threshold, Bayer 2, 4 and 8, blue noise, Floyd–Steinberg, Atkinson

    A generator fills a Float32Array of luminance at cell resolution. Ordered methods compare each cell with a threshold from a tiled matrix: with L tones the value is split into L − 1 bands and the threshold picks between the two tones of its band. Error diffusion rounds each cell to the nearest tone and pushes the remainder onto neighbours that have not been visited yet. The blue-noise mask is generated by scripts/gen-blue-noise.mjs (void-and-cluster after Ulichney, 1993) and shipped as data, so nothing is computed at load time.

    npm test runs 147 tests:

    • Golden: every generator at three sizes and two seeds, and the plate dither, compared bit for bit with verbatim copies of the site's code in test/fixtures/site-art (commit recorded in its SOURCE.md), plus stored SHA-256 hashes.
    • Dither: matrices are permutations, the blue-noise mask spreads its points apart, every method emits only valid tones, keeps the mean tone of a ramp, and uses every tone.
    • Export: PNGs decoded with an independent decoder (pngjs), BMPs parsed field by field, for every depth.

    npm run bench checks the WebGL output against the CPU output and fails on any difference.

    • The WebGL path covers the ordered methods only; error diffusion is sequential and runs on the CPU.
    • WebGL grain uses a hash, not the PRNG, so with grain switched on the two paths make different (equally random) noise. Without grain they match.
    • Some generators are slow at large sizes: on one laptop CPU (Node 24) the moon took 0.6 s at 358 × 483 cells and 4.9 s at 1072 × 1448 (npm run bench; see BENCHMARKS.md). Draw at a coarser cell size and enlarge, and run generators in a worker, as Plate Studio does.
    • Colour images are reduced to luminance. Dithering to a colour palette maps tones, not hues.

    MIT. See LICENSE.