pack method
Corresponding parser should be called from frame_app.lua data_handler()
Implementation
@override
Uint8List pack() {
int widthMsb = _width >> 8;
int widthLsb = _width & 0xFF;
int heightMsb = _height >> 8;
int heightLsb = _height & 0xFF;
int bpp = 0;
Uint8List packed;
switch (_numColors) {
case <= 2:
bpp = 1;
packed = pack1Bit(_pixelData);
break;
case <= 4:
bpp = 2;
packed = pack2Bit(_pixelData);
break;
case <= 16:
bpp = 4;
packed = pack4Bit(_pixelData);
break;
default:
throw Exception(
'Image must have 16 or fewer colors. Actual: $_numColors');
}
// preallocate the list of bytes to send - sprite header, palette, pixel data
// (packed.length already adds the extra byte if WxH is not divisible by 8)
Uint8List payload =
Uint8List.fromList(List.filled(6 + _numColors * 3 + packed.length, 0));
// NB: palette data could be numColors=12 x 3 (RGB) bytes even if bpp is 4 (max 16 colors)
// hence we provide both numColors and bpp here.
// sendMessage will prepend the data byte, msgCode to each packet
// and the Uint16 payload length to the first packet
payload
.setAll(0, [widthMsb, widthLsb, heightMsb, heightLsb, bpp, _numColors]);
payload.setAll(6, _paletteData);
payload.setAll(6 + _numColors * 3, packed);
return payload;
}