encodeAstc4x4Block function

Uint8List encodeAstc4x4Block(
  1. List<(int, int, int, int)> pixels
)

Encodes one 4×4 block (row-major, alpha ignored) as sixteen bytes: the 11-bit _kBlockMode, a 2-bit partition count of zero (one partition), a 4-bit CEM of _kCemLdrRgbDirect, six _kColorBits-wide endpoint channel values, then sixteen _kWeightBits-wide per-texel weights packed downward from bit 127 — 17 + 48 from the bottom and 48 from the top, leaving the fifteen bits between them zero.

Implementation

Uint8List encodeAstc4x4Block(List<(int, int, int, int)> pixels) {
  final fit = blockEndpoints(pixels);

  // The weights are chosen against the endpoints as the block will *store*
  // them, which is why the ordering happens first and the search reads the
  // pair back out of it.
  final ordered = orderAstc4x4Endpoints(
    _quantizeColor(fit.high),
    _quantizeColor(fit.low),
  );
  final lowExpanded = _expandColor(ordered.low);
  final highExpanded = _expandColor(ordered.high);

  final weights = <int>[
    for (final (er, eg, eb, _) in pixels)
      _nearestWeight(er, eg, eb, lowExpanded, highExpanded),
  ];
  return packAstc4x4Block(
    endpoint0: ordered.low,
    endpoint1: ordered.high,
    weights: weights,
  );
}