Session 3

Compression

Images are held in your computer as files on your hard disc.

They can be held in a number of different formats.  You can tell which from the 3 letter suffix after the file name - e.g. bmp, gif, jpg, psd.

Lossless compression

In session 1 we described a format in which the image was held as a string of 3 numbers for each and every pixel in the image.  This is something like the .bmp (bitmap) format.  An image with 1.5Mpixels will require 4.5Mbytes of storage.

Other formats are more efficient than this and manage to use less storage by 'compressing' the file.

Think of a picture in which the top left hand corner is all one colour, say, red - 255,0,0.
Suppose the first 20 pixels of the top row were all the same.  Therefore our bmp file would start off:

    255,0,0,255,0,0,255,0,0, . . .

repeated 20 times for a total of 60 bytes.

But we can find a much better way of storing this if we allocate 4 bytes to one pixel.  The first byte of each group of four specifies the number of following pixels which are all the same.  So we can describe our 20 pixels above as:

   20,255,0,0    meaning  this pixel and the next 19 bytes are all red. 

We've used 4 bytes instead of 60!  We don't often get big blocks of exactly the same colour like this so usually we are not so lucky.  But by using similar techniques we can often achieve a compression of around 4 to 1.

This is called 'lossless' compression - using the information in the file we can reconstruct the original image exactly.

Lossy compression

We can do better if we're prepared to lose a little of the detail of the image.  Suppose the next 10 pixels of our example above were a very slightly darker shade of red - 253,0,0.  With the compression method above we would define the first 30 pixels as:

    20,255,0,0,10,253,0,0 - 8 bytes.

But if we decide that the human eye can scarcely tell the difference between the two shades of red we could compromise and store it as:

    30,254,0,0 - 4 bytes.

However we have lost some detail and we cannot reconstruct the original image exactly.  This is called 'lossy' compression.

In this example we have lost some detail in the colour.  Other techniques lose some detail in the shapes.  But we can manage some very high compression ratios - 10:1 or even 50:1 - while still obtaining acceptable results.

Here's an example of just a small section of an image - 285 x 230 pixels.  You will need to double-click the images to see the effects properly.

Original stored uncompressed as bmp.

194 kbytes

AlcesterDoorsHigh.jpg (18326 bytes) Stored with low compression

18 Kbytes

Slight blurring of the '18', slight loss of colour differentiation

AlcesterDoorsLow.jpg (3532 bytes) Stored with high compression

4Kbytes

Obvious blurring of bricks and '18'.  Curiously shaped 'artifacts' appearing in woodwork.

With some software it is possible to vary the amount of compression and see the effect before committing it.  E.g. in Photoshop take a look at 'Save for Web'.

Back ] Up ] Next ]