encodeRowInto static method

int encodeRowInto(
  1. Uint8List row,
  2. Uint8List output,
  3. int start
)

Encodes row into output at start and returns the next write offset.

output must have at least maxEncodedLength bytes available beyond start. Writing into a caller-owned buffer lets a whole image be encoded without allocating per row or per run, which dominates the cost of compressing large planes.

Implementation

static int encodeRowInto(Uint8List row, Uint8List output, int start) {
  int write = start;
  int offset = 0;
  while (offset < row.length) {
    int runLength = _repeatedRunLength(row, offset);
    if (runLength >= 3) {
      output[write++] = 257 - runLength;
      output[write++] = row[offset];
      offset += runLength;
      continue;
    }

    final int literalStart = offset;
    offset += runLength;
    while (offset < row.length && offset - literalStart < 128) {
      runLength = _repeatedRunLength(row, offset);
      if (runLength >= 3) {
        break;
      }
      final int remaining = 128 - (offset - literalStart);
      offset += runLength.clamp(1, remaining);
    }
    final int literalLength = offset - literalStart;
    output[write++] = literalLength - 1;
    output.setRange(write, write + literalLength, row, literalStart);
    write += literalLength;
  }
  return write;
}