Send a single large image rather than 4 tiles

This commit is contained in:
James Babcock committed 2026-06-23 10:49:20 -07:00
1 parent 401cf6d4c1
commit 8f28ede647
10 files changed
+150 -430

No files matched your search

-182
View File
@@ -1,182 +0,0 @@
import { type ImageTile } from "./image";
const lz4 = require("lz4js");
const pako = require("pako");
type CompressionTotals = {
uncompressed: number;
lz4: number;
png: number;
};
export type CompressionEstimateReport = {
tileLines: string[];
totalLine: string;
};
const cumulativeTotals: CompressionTotals = {
uncompressed: 0,
lz4: 0,
png: 0,
};
let reportSequence = 0;
export function estimateTileCompression(tiles: readonly ImageTile[]): CompressionEstimateReport {
const frameTotals: CompressionTotals = {
uncompressed: 0,
lz4: 0,
png: 0,
};
const frameId = ++reportSequence;
const tileLines: string[] = [];
for (let index = 0; index < tiles.length; index++) {
const tile = tiles[index]!;
const uncompressedSize = tile.bmp.byteLength;
const lz4Size = byteLength(lz4.compress(tile.bmp));
const pngSize = encodePng4BitGrayscaleFromBmpTile(tile).byteLength;
frameTotals.uncompressed += uncompressedSize;
frameTotals.lz4 += lz4Size;
frameTotals.png += pngSize;
tileLines.push(
`compression estimate #${frameId} tile ${index + 1}/${tiles.length} ` +
`${tile.x},${tile.y} ${tile.width}x${tile.height}: ` +
`uncompressed=${uncompressedSize}, lz4=${formatSizeWithRatio(lz4Size, uncompressedSize)}, ` +
`png4=${formatSizeWithRatio(pngSize, uncompressedSize)}`,
);
}
cumulativeTotals.uncompressed += frameTotals.uncompressed;
cumulativeTotals.lz4 += frameTotals.lz4;
cumulativeTotals.png += frameTotals.png;
return {
tileLines,
totalLine:
`compression estimate #${frameId} totals: ` +
`frame uncompressed=${frameTotals.uncompressed}, ` +
`lz4=${formatSizeWithRatio(frameTotals.lz4, frameTotals.uncompressed)}, ` +
`png4=${formatSizeWithRatio(frameTotals.png, frameTotals.uncompressed)}; ` +
`running uncompressed=${cumulativeTotals.uncompressed}, ` +
`lz4=${formatSizeWithRatio(cumulativeTotals.lz4, cumulativeTotals.uncompressed)}, ` +
`png4=${formatSizeWithRatio(cumulativeTotals.png, cumulativeTotals.uncompressed)}`,
};
}
function encodePng4BitGrayscaleFromBmpTile(tile: ImageTile): Uint8Array {
const pixelOffset = readUint32Le(tile.bmp, 10);
const sourceStride = (((tile.width + 1) >> 1) + 3) & ~3;
const pngRowBytes = (tile.width + 1) >> 1;
const scanlineStride = 1 + pngRowBytes;
const scanlines = new Uint8Array(scanlineStride * tile.height);
for (let y = 0; y < tile.height; y++) {
const sourceY = tile.height - 1 - y;
const sourceOffset = pixelOffset + sourceY * sourceStride;
const targetOffset = y * scanlineStride;
scanlines[targetOffset] = 0; // PNG filter type 0 keeps the 4-bit samples unchanged.
scanlines.set(tile.bmp.subarray(sourceOffset, sourceOffset + pngRowBytes), targetOffset + 1);
}
const compressed = toUint8Array(pako.deflate(scanlines));
const ihdr = new Uint8Array(13);
const ihdrView = new DataView(ihdr.buffer);
ihdrView.setUint32(0, tile.width, false);
ihdrView.setUint32(4, tile.height, false);
ihdr[8] = 4; // bit depth: 4-bit grayscale
ihdr[9] = 0; // color type: grayscale
ihdr[10] = 0; // compression method
ihdr[11] = 0; // filter method
ihdr[12] = 0; // no interlace
return concatBytes(
PNG_SIGNATURE,
pngChunk("IHDR", ihdr),
pngChunk("IDAT", compressed),
pngChunk("IEND", new Uint8Array(0)),
);
}
function pngChunk(type: string, data: Uint8Array): Uint8Array {
const typeBytes = asciiBytes(type);
const chunk = new Uint8Array(12 + data.byteLength);
const view = new DataView(chunk.buffer);
view.setUint32(0, data.byteLength, false);
chunk.set(typeBytes, 4);
chunk.set(data, 8);
view.setUint32(8 + data.byteLength, crc32(typeBytes, data), false);
return chunk;
}
const PNG_SIGNATURE = new Uint8Array([0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a]);
const CRC_TABLE = buildCrcTable();
function crc32(typeBytes: Uint8Array, data: Uint8Array): number {
let crc = 0xffffffff;
crc = updateCrc32(crc, typeBytes);
crc = updateCrc32(crc, data);
return (crc ^ 0xffffffff) >>> 0;
}
function updateCrc32(crc: number, data: Uint8Array): number {
for (let i = 0; i < data.byteLength; i++) {
crc = CRC_TABLE[(crc ^ data[i]!) & 0xff]! ^ (crc >>> 8);
}
return crc;
}
function buildCrcTable(): Uint32Array {
const table = new Uint32Array(256);
for (let n = 0; n < table.length; n++) {
let c = n;
for (let k = 0; k < 8; k++) {
c = (c & 1) !== 0 ? 0xedb88320 ^ (c >>> 1) : c >>> 1;
}
table[n] = c >>> 0;
}
return table;
}
function asciiBytes(value: string): Uint8Array {
const out = new Uint8Array(value.length);
for (let i = 0; i < value.length; i++) {
out[i] = value.charCodeAt(i) & 0x7f;
}
return out;
}
function concatBytes(...parts: Uint8Array[]): Uint8Array {
const totalLength = parts.reduce((sum, part) => sum + part.byteLength, 0);
const out = new Uint8Array(totalLength);
let offset = 0;
for (const part of parts) {
out.set(part, offset);
offset += part.byteLength;
}
return out;
}
function readUint32Le(bytes: Uint8Array, offset: number): number {
return (
(bytes[offset] ?? 0) |
((bytes[offset + 1] ?? 0) << 8) |
((bytes[offset + 2] ?? 0) << 16) |
((bytes[offset + 3] ?? 0) << 24)
) >>> 0;
}
function byteLength(value: ArrayLike<number> | Uint8Array): number {
return value instanceof Uint8Array ? value.byteLength : value.length;
}
function toUint8Array(value: ArrayLike<number> | Uint8Array): Uint8Array {
return value instanceof Uint8Array ? value : Uint8Array.from(value);
}
function formatSizeWithRatio(size: number, uncompressedSize: number): string {
if (uncompressedSize <= 0) return `${size}`;
return `${size} (${((size / uncompressedSize) * 100).toFixed(1)}%)`;
}
+8 -83
View File
@@ -3,18 +3,8 @@ import { wrapText } from "./textwrap";
export const G2_LENS_WIDTH = 576;
export const G2_LENS_HEIGHT = 288;
export const G2_TILE_WIDTH = 288;
export const G2_TILE_HEIGHT = 144;
const DEFAULT_CORNER_RADIUS = 8;
export type ImageTile = {
x: number;
y: number;
width: number;
height: number;
bmp: Uint8Array;
};
export function imageFromAsciiArt(lines: readonly string[], value = 255): GrayImage {
const width = Math.max(0, ...lines.map((line) => line.length));
const image = new GrayImage(width, lines.length, 0);
@@ -255,22 +245,14 @@ export class GrayImage {
return `fnv:${(hash >>> 0).toString(16)}`;
}
toEvenHubTiles(): ImageTile[] {
const tiles: ImageTile[] = [];
for (let ty = 0; ty < this.height; ty += G2_TILE_HEIGHT) {
for (let tx = 0; tx < this.width; tx += G2_TILE_WIDTH) {
const width = Math.min(G2_TILE_WIDTH, this.width - tx);
const height = Math.min(G2_TILE_HEIGHT, this.height - ty);
tiles.push({
x: tx,
y: ty,
width,
height,
bmp: buildEvenHubBmp(width, height, (x, y) => grayToEvenHubNibble(this.getPixel(tx + x, ty + y))),
});
}
}
return tiles;
/**
* Snapshot of the full image as a single 8-bit-per-pixel grayscale buffer,
* row-major and top-to-bottom. The Java side turns this into whatever wire
* format the firmware currently expects (today: a 4bpp BMP), so all framing
* concerns stay on one side of the bridge.
*/
to8bppBuffer(): Uint8Array {
return Uint8Array.from(this.pixels);
}
private drawGlyph(font: BdfFont, glyph: Glyph, x: number, y: number, value: number): void {
@@ -290,63 +272,6 @@ export class GrayImage {
}
}
function buildEvenHubBmp(
width: number,
height: number,
pixel: (x: number, y: number) => number,
): Uint8Array {
const bytesPerPixelRow = (width + 1) >> 1;
const rowStride = (bytesPerPixelRow + 3) & ~3;
const pixelDataSize = rowStride * height;
const fileHeaderSize = 14;
const dibHeaderSize = 40;
const paletteSize = 16 * 4;
const pixelOffset = fileHeaderSize + dibHeaderSize + paletteSize;
const fileSize = pixelOffset + pixelDataSize;
const buf = new Uint8Array(fileSize);
const view = new DataView(buf.buffer);
buf[0] = 0x42;
buf[1] = 0x4d;
view.setUint32(2, fileSize, true);
view.setUint32(10, pixelOffset, true);
view.setUint32(14, dibHeaderSize, true);
view.setInt32(18, width, true);
view.setInt32(22, height, true);
view.setUint16(26, 1, true);
view.setUint16(28, 4, true);
view.setUint32(30, 0, true);
view.setUint32(34, pixelDataSize, true);
view.setUint32(46, 16, true);
for (let i = 0; i < 16; i++) {
const v = i * 17;
const base = fileHeaderSize + dibHeaderSize + i * 4;
buf[base + 0] = v;
buf[base + 1] = v;
buf[base + 2] = v;
buf[base + 3] = 0;
}
for (let bmpRow = 0; bmpRow < height; bmpRow++) {
const srcY = height - 1 - bmpRow;
const rowOffset = pixelOffset + bmpRow * rowStride;
for (let x = 0; x < width; x += 2) {
const hi = pixel(x, srcY) & 0x0f;
const lo = (x + 1 < width ? pixel(x + 1, srcY) : 0) & 0x0f;
buf[rowOffset + (x >> 1)] = (hi << 4) | lo;
}
}
return buf;
}
function grayToEvenHubNibble(value: number): number {
const clamped = clampByte(value);
return clamped === 0 ? 0 : Math.min(15, (clamped + 15) >> 4);
}
function clampByte(value: number): number {
return Math.max(0, Math.min(255, Math.round(value)));
}