Manual Color Transforms and Lookup Tables
At the very least, you can use the gamma correction system to perform filter operations on the entire image on the screen. Anyway, I'll talk about lookup tables in RGB modes first, and then discuss the DirectX gamma correction system.
When dealing with pixels that are encoded as RGB WORDs, there's really no way out of doing the work if you want to perform color animation. Not only must you write each pixel for which you want to change the color, but you may also have to read it. Hence, in the worst case you might have to perform a read, transform, and write cycle for each pixel you want to manipulate. There's simply no way around this.
However, help is available. In most cases, performing mathematical transformation in RGB space is very computationally expensive. For example, let's say that you want to simulate a square-shaped spotlight with a 16-bit graphics mode at location (x,y) with size (width, height). What would you do?
Well, you would begin by scanning out the rectangle of pixels that made up the spotlight area and storing them into a bitmap. Then, for each pixel in the bitmap, you would perform a color transform that looked something like this:
I*pixel(r,g,b) = pixel(I*r, I*g, I*b)
Three multiplies to modulate the intensity. Not to mention that you would have to first extract out the RGB components of the 16-bit WORD and then put the 16-bit RGB WORD back together after the transform. The trick here is to use a lookup table.
Instead of using all 65,536 colors available in 16-bit mode, you only draw objects that can possibly be illuminated with, say, 1,024 colors that are equally distributed throughout the 64KB color space. Then you create a lookup table that contains a 2D array that's 1,024 times however many levels of intensity you want, such as 64. Then you take the RGB level of each real color, compute 64 shades of it, and store each of them in the table. Then, when you create the spotlight, you use the 16-bit WORD as the index into the table, along with the light level as the second index, and the resulting 16-bit in the table is the RGB value premodulated! Hence, the lighting operation is a simple lookup.
This technique can be used for transparency, alpha-blending, lighting, darkening, and so on. I'm not going to show you a demo of it until the next chapter, but if you want to use lighting or color effects in 16-, 24-, or 32-bit color modes with any kind of speed, using lookup tables is the only way to go.