Data compression reduces the size of files so that they take up less storage space and can be transmitted faster over networks. Compression is essential for streaming video, sending email attachments, and storing large image libraries.
Lossy compression permanently removes some data from the file to reduce its size. The original file cannot be perfectly reconstructed — some quality is lost, but the file becomes significantly smaller. This is acceptable when humans cannot easily detect small quality reductions.
Lossless compression reduces file size without losing any data. The original file can be perfectly reconstructed by decompressing. Used when exact data preservation is essential.
Run-Length Encoding is a simple lossless compression algorithm. It replaces consecutive repeated values (a "run") with a count and the value. Very effective for images with large areas of the same colour.
Example: Compress the pixel sequence AAABBCCCCDDDDDD
RLE works best when there are many repeated consecutive values. It performs poorly (or even makes files larger) when there are few runs — e.g. a checkerboard pattern like ABABAB.
| Feature | Lossy | Lossless |
|---|---|---|
| Data lost? | Yes — permanently | No — fully recoverable |
| File size reduction | Very high | Moderate |
| Best for | Media (audio, video, images) where small quality loss acceptable | Text, programs, data where accuracy is critical |
| Examples | MP3, JPEG, MP4 | FLAC, PNG, ZIP, GIF |