transcodeUniversal function

Uint8List transcodeUniversal(
  1. Uint8List blocks,
  2. UniversalTarget target, {
  3. required int width,
  4. required int height,
})

blocks as target, ready to upload.

width and height are the level's pixel dimensions; only UniversalTarget.rgba8 reads them, and it needs them because it is the one target whose bytes are a raster rather than a sequence of blocks.

Implementation

Uint8List transcodeUniversal(
  Uint8List blocks,
  UniversalTarget target, {
  required int width,
  required int height,
}) {
  if (blocks.lengthInBytes % kUniversalBlockBytes != 0) {
    throw ArgumentError(
      'A universal level is whole $kUniversalBlockBytes-byte blocks; '
      '${blocks.lengthInBytes} bytes is not.',
    );
  }
  final count = blocks.lengthInBytes ~/ kUniversalBlockBytes;
  final encodeBlock = target.encodeBlock;
  if (encodeBlock == null) return _toRgba8(blocks, count, width, height);

  final out = Uint8List(count * target.bytesPerBlock);
  for (var i = 0; i < count; i++) {
    out.setRange(
      i * target.bytesPerBlock,
      (i + 1) * target.bytesPerBlock,
      encodeBlock(blocks, i),
    );
  }
  return out;
}