Run-Length Encoding Simulator
Compress text or a black-and-white image with run-length encoding, step by step, and compare the sizes before and after.
Data to compress
Encoding step by step
Read each symbol. While it repeats, count it; when it changes, output the run as (count, value).
Output so far
Pseudocode
The current line is highlighted.
Full encoded output
4A 3B 2C 1D 6A
Decoded: AAAABBBCCDAAAAAA ✓ identical
About This Simulation
This run-length encoding (RLE) simulator compresses text or a black-and-white bitmap one symbol at a time. It shows runs being counted, the (count, value) pairs that are output, and how the compressed size compares with the original.
Features
- Text mode and a clickable black-and-white image mode
- Step-by-step encoding with the current run highlighted
- Pseudocode with the active line highlighted
- Original and compressed sizes with the compression ratio
- Worst-case checkerboard showing RLE making data bigger
Topics covered
- Run-Length Encoding
- Lossless Compression
- Compression Ratio
- Bitmap Compression
Explanation
Run-length encoding is a simple lossless compression method. A run is a sequence of the same value repeated. Instead of storing every value, RLE stores how many times each value repeats, followed by the value.
How it works
- Start with a count of 1 and read the data from left to right.
- If the next value is the same as the previous one, add 1 to the count.
- When the value changes, output the count and the value, then reset the count to 1.
- At the end, output the final count and value. AAAABBBCC becomes 4A 3B 2C.
- To decode, write each value the number of times given by its count.
Why it matters
RLE appears in IGCSE and AS Level exams, often with a bitmap or a string to encode. It works well for images with large areas of one colour, such as icons and simple graphics, but badly for photos or text with few repeats.
Frequently Asked Questions
Is run-length encoding lossy or lossless?
Lossless. Decoding the runs gives back exactly the original data.
When does RLE not work well?
When values rarely repeat, each single value becomes a pair (count, value), so the 'compressed' data can be larger than the original.
How is RLE used for images?
Each row of pixels is encoded as runs of the same colour, such as 5 white, 3 black, 4 white. It suits images with large blocks of a single colour.
Related Simulations
Data Representation
Compression Simulator
Compare lossless dictionary compression of text with lossy compression of an image, and see the size saved and the data lost.
Data Representation
Huffman Coding Simulator
Build a Huffman tree from character frequencies one merge at a time, read off the prefix codes and compare the bits with ASCII.
Data Representation
Bitmap File Size Simulator
Calculate image file size from resolution and colour depth, with a live preview of how colour depth changes the image.
Data Representation
Sound File Size Simulator
Calculate audio file size from sample rate, bit depth, channels and duration, and see sampling, quantisation and aliasing on a wave.

