import { CFW_MSG_BOUNDING_BOX } from "./cfw-message-type"; import { concat, rle4 } from './ble-protocol' /** Android BleImageOptimizer's mode-3 changed rectangle. Coordinates are in * units of 4 columns / 2 rows; pixels are top-down packed 4bpp, high nibble first. * null means identical, incomparable, or a full-screen box: use dedup/mode 6. Compression belongs to the CFW transport. */ export function buildBoundingBoxPayload(previous: Uint8Array | null, next: Uint8Array, width: number, height: number, frameId: number): Uint8Array | null { if (!Number.isInteger(width) || !Number.isInteger(height) || width <= 0 || height <= 0 || width % 4 || height % 2 || width / 4 > 255 || height / 2 > 255 || !previous || previous.length !== width * height / 2 || next.length !== previous.length) return null const stride = width / 2 let minX = stride, maxX = -1, minY = height, maxY = -1 for (let y = 0; y < height; y++) { const row = y * stride for (let x = 0; x < stride; x++) { if (previous[row + x] === next[row + x]) continue minX = Math.min(minX, x); maxX = Math.max(maxX, x) minY = Math.min(minY, y); maxY = y } } if (maxY < 0) return null const left = (minX * 2) & ~3, right = Math.min(width, ((maxX + 1) * 2 + 3) & ~3) const top = minY & ~1, bottom = Math.min(height, (maxY + 2) & ~1) const boxWidth = right - left, boxHeight = bottom - top if (boxWidth === width && boxHeight === height) return null const regionStride = boxWidth / 2, region = new Uint8Array(regionStride * boxHeight) for (let y = 0; y < boxHeight; y++) { const start = (top + y) * stride + left / 2 region.set(next.subarray(start, start + regionStride), y * regionStride) } return concat(new Uint8Array([CFW_MSG_BOUNDING_BOX, left / 4, top / 2, boxWidth / 4, boxHeight / 2, frameId & 255, (frameId >>> 8) & 255]), rle4(region)) } /** Full repaint as independent 64-row mode-3 bands when RLE exceeds uint16. */ export function buildFullFrameBands(packed: Uint8Array, width: number, height: number, firstId: number): Uint8Array[] { if (width <= 0 || width > 640 || width % 4 || height <= 0 || height > 480 || height % 2 || packed.length !== width * height / 2) throw new Error('Invalid framebuffer dimensions') const bands: Uint8Array[] = [] let id = firstId for (let top = 0; top < height; top += 64) { const rows = Math.min(64, height - top) bands.push(concat(new Uint8Array([CFW_MSG_BOUNDING_BOX, 0, top / 2, width / 4, rows / 2, id & 255, id >>> 8]), rle4(packed.subarray(top * width / 2, (top + rows) * width / 2)))) id = id >= 0xfffe ? 1 : id + 1 } return bands }