3.1.8.1 NSCodec Run-Length Encoding

NSCodec run-length encoding is a simple compression scheme that parses an image stream and then encodes run lengths with minimal overhead. The algorithm is run on the stream as a whole and encodes it into segments of two types: runs and literals. The last 4 bytes of the stream are always left unencoded.

For example, an initial stream containing the following 12 ANSI characters:

 AAAABBCCCCCD

Would be transformed after encoding into the following stream:

 AA2BB0CC0CCCD

In this case, encoding the stream has resulted in expansion, and the original image stream is sent instead.

For a second example, an initial stream containing the following 27 ANSI characters:

 ABCDDDTTTTGFRRRRRRRRRRRABCD

Would be transformed after encoding into the following stream:

 ABCDD1TT2GFRR9ABCD

In the case of real image streams, the likelihood of long runs is high, and consequently, the reductions in size are significant.