The Unofficial Newsletter of Delphi Users - by Robert Vivrette




Compressing 256 colour bitmaps

by Grahame Marsh - GSMarsh@aol.com
 
The general tools for saving windows bitmaps to disc provided with Delphi save the images in an uncompressed format.  This format, known as RGB encoded can be very greedy on system resources, especially disc space and memory to load.  But the windows specification for bitmaps includes another encoding scheme known as run length encoding or RLE.  For bitmaps that contain areas of the same colour, the technique can save huge amounts of resources, and savings are even possible on complex images as I will show.

Consider this 16 by 16 bitmap: 

it uses 256 bytes to store the colour information (together with a bunch of file and format information), but with RLE the information can be stored in just 66 bytes!

I won't go into how RLE works, as you can easily read about it in win32.hlp if you search on "RLE".  What I want to show you is a simple method of incorporating RLE into your own code and how to compress images for use in splash screens etc.

Part of Windows 95 apparently not implemented by Borland is the bitmap compression and decompression routines held in msvfw32.dll.  The dll contains a great deal of useful stuff but I'll just look at four functions and some constants and a record:

The key function here is ICImageCompress (suprise, suprise), it is used like this:

The handle for the compressor is obtained using the ICLocate function, ICGetInfo is used to obtain information on the compressor (such as a name, description, the driver name and where it's located),  and  finally ICClose is used to dispose of the Handle when we are finished.

Below, you will see a screen shot of a small demo program I put together to illustrate this technique. You can load a file into the left hand pane and compress it into another file in the righthand pane.  The quality slider allows you to see how the compressor trades-off file size for the quality of the final image. If you want to download the unit for this project, you can click here.

Not within the ICxxxx calls (or at least I couldn't find it) is compression of the palette.  Not all images use all 256 colours and the unused information can be stripped out to save a little more space (but only 4 bytes per unused palette entry).  This doesn't matter too much on large images like those above, but is very useful on small images where the 1024 bytes used to store the colour information can become significant.  You can turn this feature on and off to see the effect on space.

Turning quality to zero on the "handshake" bitmap reduces the filesize to 25122 bytes which is getting down toward saving half the space on the original image.

To give you one more example, the large bitmap above has a file length of 299002 bytes, compressed at 85% quality (which more-or-less is identical with the original) it now occupies 96718 bytes - about a third of the original!

In these days of 20 K Delphi 3 applications (like this one), it can seem silly to have a 50K graphic on a splash screen.  This utility will I hope allow you to manage with a 25K splash!

    Cheers
        Grahame