NTU – EE5704 Notes Run-Length Encoding

Run-Lenght Encoding is: instead of saying “apple apple apple pear pear banana orange orange apple”, you simpily say “Three apples, two pears, one banana, two oranges, and one apple. RLE is as simple as that.

run length encoding rle_003.jpg

run length encoding rle_008.jpg

Of course, in the world of digital computers you don’t count apple, pears or bananas. Digital computer use 0 and 1 to represent everything, so you count 0 and 1 instead. And the following is a simple and real world run-length encoding example

run length encoding rle_010.jpg

Run-length encoding (RLE) is a very simple form of data compression in which runs of data (that is, sequences in which the same data value occurs in many consecutive data elements) are stored as a single data value and count, rather than as the original run.

run length encoding algorithm is fairly simple and straight forward to implement and that’s the becauty of run length encoding compression.

Run Length Encoding Examples

Run Length Encoding Algorithm Matlab Implementation

function encoded = RLE_encode(input)

my_size = size(input);
length = my_size(2);

run_length = 1;
encoded = [];

for i=2:length
if input(i) == input(i-1)
run_length = run_length + 1;
encoded = [encoded input(i-1) run_length];
run_length = 1;

if length > 1
% Add last value and run length to output
encoded = [encoded input(i) run_length];
% Special case if input is of length 1
encoded = [input(1) 1];

Run Length Decoding Java Example Code

public class RunLength{
private final static int R = 256; //max run length count
private final static int lgR = 8; //number of bits per count

public static void expand(){
boolean bit = flase;
int run = BinaryStdIn.readInt(lgR); //read 8-bit count from stdin
for (int i=0; i<run; i++){
BinaryStdOut.write(bit); //write 1 bit to stdout
bit = !bit;
BinaryStdOut.close(); //pad 0s for byte alignment

Run-Length Coding Interactive Visual Demo

run length encoding rle_014.jpg

Run-Length Coding Video

JPEG Video

This entry was posted in Uncategorized. Bookmark the permalink.

Comments are closed.