Differential encoding is a form of lossless compression that reduces file size by recording the difference between neighboring values rather than recording the values themselves. Differential encoding can be applied to digital images, audio, or video. The
of a compression algorithm is the ratio of the original file size
to the size of the compressed file
, expressed as
. Alternatively, you can speak of the ratio of
as a percentage. For example, for a file that is reduced by compression to half its original size, you could say that 50% compression is achieved, or alternatively, that the compression rate is 2:1.
In the sections that follow, we will look more closely at three algorithmic approaches to lossless compression.
7.2 Run-Length Encoding
is a simple example of lossless compression. It is used in image compression. For example, files that you see with the
suffix-a Microsoft version of bitmap image files-optionally use run-length encoding. Here's how it works. An image file is stored as a sequence of colour values for repeated pixel locations across rows and down columns. If the file is in RGB colour mode, there are three bytes per pixel, one for each of the red, green, and blue colour channels. If the file is grayscale, there is one byte per pixel. For simplicity, let use a grayscale file in this example. (Extending the algorithm to three bytes per pixel is straightforward.) Since each pixel position is encoded in one byte, it represents one of 256 grayscale values. (You recall that 2
= 256, so eight bits can encode 256 different things.) Thus, a grayscale image file consists of a string of numbers, each of them between 0 and 255. Assume that the image has dimensions 100 X 100, for a total of 10,000 pixels. Assume also that the pixels are stored in row-major order, which means that the values from a whole row are stored from left to right, then the next row from left to right, and so forth.
You can easily imagine that in many images, there could be rows with strings of repeated grayscale values. The simple idea in run-length encoding