CS With NehalCS With Nehal

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).

Step 1 / 16
AAAABBBCCDAAAAAA

Output so far

counting 1…

Pseudocode

The current line is highlighted.

128 bits
Original
8 bits per character
80 bits
Compressed
5 runs × 16 bits
1.60 : 1
Compression ratio
38%
Saved
RLE is lossless: decoding gives back exactly the original data.

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

  1. Start with a count of 1 and read the data from left to right.
  2. If the next value is the same as the previous one, add 1 to the count.
  3. When the value changes, output the count and the value, then reset the count to 1.
  4. At the end, output the final count and value. AAAABBBCC becomes 4A 3B 2C.
  5. 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