Wednesday, August 11, 2010

Colour Depth

Start at the Beginning
Table of Contents

One other thing to know about computer graphics. On modern computers, each pixel's colour is stored in a 32-bit number. Each colour is stored as a measurement of the intensity of three fundamental colours - red, green, and blue, which are then blended together to form the final colour. Each of these measurements are 8 bits (1 byte). A pixel with 255 for red and 0 for green and blue would be a bright red; a pixel with 255 for red, green, and blue is white; a pixel with 0 for all of the different colour channels is black. The final byte of the colour typically is used to describe how transparent the pixel is (this is called the alpha value of the colour). That way, when drawing two images over top of each other, by performing some computations the computer can modify the colour it draws to make one image appear visible through the other.

Since my computer has a resolution of 1280x1024, that means there are

1280x1024 = 1310720

pixels on my screen. At 32 bits (or 4 bytes) per pixel, that means my screen takes 5242880 bytes to store. A KB (kilobyte) is 1024 bytes, and a MB (megabyte) is 1024KB or 1048576 bytes. My screen alone, then, requires around 5MB to store! Before around 1993, most computers didn't have this much memory for storing *everything* - all the programs and files you had loaded, numbers required for calculations, etc. - much less just to store the screen data. Of course, resolutions were smaller back then, but tricks were still needed to squeeze everything into memory.

One of these tricks is using a 'palette'. Rather than representing each pixel as 32 bits, we'll use only 8. This means we get 256 different colours to choose from per pixel. While we could just store the Red-Green-Blue-Alpha (RGBA) data using only 2 bits per channel, this wouldn't give us a great deal of flexibility regarding the colours we use. Instead, we'll choose 256 colours using 32 bits each and store them in a palette - a list of colours we're using. Because there's only 256 of them, even though we're using 32 bits per colour, it's still significantly smaller than using 32 bits for the thousands of pixels on screen. Suppose our resolution is 320x200. Using 32 bits per pixel, we're using:

320x200x4 bytes per pxiel = 256000 bytes

...around 256KB. Using a palette, we use only:

320x200x1 byte per pixel + 256 colours * 4 bytes per palette entry = 65024 bytes

...around 64KB. We're using only a quarter of the space we were using before; this was a significant saving when memory was expensive as it was. In fact, old computers tended to use only 6 bits per channel, and not store any alpha information whatsoever - meaning each palette entry was only 18 bits. So the size of the palette was smaller still.

No comments:

Post a Comment