lzw static method
Implementation
static Uint8List lzw(Uint8List input, {int earlyChange = 1}) {
final List<int> out = <int>[];
final List<List<int>> table = <List<int>>[];
void reset() {
table.clear();
for (int i = 0; i < 256; i++) {
table.add(<int>[i]);
}
table.add(<int>[]);
table.add(<int>[]);
}
reset();
int codeWidth = 9;
int buffer = 0;
int bits = 0;
List<int>? previous;
for (final int byte in input) {
buffer = (buffer << 8) | byte;
bits += 8;
while (bits >= codeWidth) {
final int code = (buffer >> (bits - codeWidth)) & ((1 << codeWidth) - 1);
bits -= codeWidth;
if (code == 256) {
reset();
codeWidth = 9;
previous = null;
continue;
}
if (code == 257) return Uint8List.fromList(out);
List<int> entry;
if (code < table.length && table[code].isNotEmpty) {
entry = table[code];
} else if (previous != null) {
entry = <int>[...previous, previous.first];
} else {
return Uint8List.fromList(out);
}
out.addAll(entry);
if (previous != null) table.add(<int>[...previous, entry.first]);
previous = entry;
if (table.length + earlyChange >= (1 << codeWidth) && codeWidth < 12) codeWidth++;
}
}
return Uint8List.fromList(out);
}