Skip to content

Mobile MCP: let AI agents drive your iOS and Android apps. MCP server for simulators, emulators and real devices. Works with Claude Code, Codex, Cursor and any MCP client.

1.4 KB
Raw Blame
// width and height from a PNG's IHDR chunk, big-endian at bytes 16..23
export function pngSize(bytes: Uint8Array): { width: number; height: number } {
	const view = new DataView(bytes.buffer, bytes.byteOffset, bytes.byteLength);

	return { width: view.getUint32(16), height: view.getUint32(20) };
}

const MAX_CELLS = 255;
// cell height / cell width of the terminal font, measured 17.3px / 8.1px; no API reports it, so tune per font
export const CELL_ASPECT = 2.14;

function clampCells(n: number): number {
	return Math.max(1, Math.min(MAX_CELLS, Math.floor(n)));
}

// the largest box of cells that keeps the picture's aspect and fits inside maxColumns x maxRows
export function fitInside(columnsLimit: number, rowsLimit: number, width: number, height: number): { columns: number; rows: number } {
	const maxColumns = clampCells(columnsLimit);
	const maxRows = clampCells(rowsLimit);
	// a broken PNG header can report zero; without an aspect to keep, fill the box instead of dividing by zero
	if (width <= 0 || height <= 0) {
		return { columns: maxColumns, rows: maxRows };
	}

	const rowsAtFullWidth = (maxColumns * height) / width / CELL_ASPECT;
	if (rowsAtFullWidth <= maxRows) {
		return { columns: clampCells(maxColumns), rows: clampCells(rowsAtFullWidth) };
	}

	const columnsAtFullHeight = (maxRows * width * CELL_ASPECT) / height;

	return { columns: clampCells(columnsAtFullHeight), rows: clampCells(maxRows) };
}