The most straightforward way of storing a bitmap is simply to list the bitmap information, byte after byte, row by row. Files stored by this method are often called RAW files. The amount of disk storage required for any bitmap is easy to calculate given the bitmap dimensions and color depth. The formula for the file size in KBytes is

where N and M are the number of horizontal and vertical pixels, B is the number of bits per pixel. The following table shows the file sizes of a few bitmap types if they are stored in RAW format.
image dimensions |
color depth |
file size |
128 x 128 |
1 bit |
2 KB |
|
8 bits |
16 KB |
|
24 bits |
48 KB |
256 x 256 |
1 bit |
8 KB |
|
8 bits |
64 KB |
|
24 bits |
192 KB |
1K x 1K |
1 bit |
128 KB |
|
8 bits |
1 MB |
|
24 bits |
3 MB |
As can be seen from this table, large 24 bit images will result in very large files, this is why compression becomes important. There are a large number of file formats used for storing compressed bitmaps from the trivial to the very complicated. The complicated formats exist because of the very large bitmap files that would exist if compression was not used. There are two broad categories of compressed file format, those which are lossless (retain the bitmaps perfectly) and those which are lousy. The following shows the main hierarchy of compression techniques.

The crudest way of reducing the size of bitmap files is to reduce the color information, this is called bit reduction or quantization. For example one could convert 24 bit bitmaps to 8 bit indexed bitmaps using dithering to simulate the lost colors. The most common lousy format by far is JPEG, a description of how it works is well outside the scope of this discussion. Its main advantage is that it can offer vastly better compression ratios than the lossless formats. For example consider the following bitmap the original of which is 500 x 350 pixels at 24 bit color. Using the formula given earlier the uncompressed file size is 500 x 350 x 24 / 8 / 1024 = 513K.

Saved in greyscale (bit depth reduction) the file is 171K (3 times smaller), saved and compressed using RLE it is 388K (75% of the original), saved using LZW compression it is 188K (36% of the original), saved as JPEG it is 30K (a compression ratio of 17:1). The following is a description of the simplest lossless compression technique called run length encoding (RLE) that is used with good effect for bitmaps with only a few colors. Consider the following small, 17 x 10 pixel, 8 bit image.

If this was to be stored in RAW form it would need 16 bytes per row for all 10 rows. However the first two rows are all the same level so it is more efficient to simply save the number of same colors in a run along with the run color. The first two rows instead of needing 16 bytes only need 2 bytes each.
In raw format the first three rows would be
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 1 1 1 1 1 1 1 1 1 1 1 1 0 0
Using run length encoding the first three rows would be
16 0
16 0
2 0 12 1 2 0
While there are more details involved in actual implementations of RLE than described here this is the basic principle being run length encoding. In order for RLE to achieve some degree of compression there needs to be runs of the same color, for this reason it is unlikely to be useful for highly colored images such as 24 bit photographs.