If you find this article contains errors or problems rendering it unreadable (missing images or files, mangled code, improper text formatting, etc) please contact the editor so corrections can be made. Thank you for helping us improve this resource
Today, I'm going to address a problem that you might be dealing with, or dealing with in the near future… rectifying a 24-bit image onto a 16-bit DirectDraw surface.
A few days ago, I was working on my old computer, an ATI VGA Wonder. It had 5 bits for each R,G, or B value(using only 15 bits), so the pixel format looked something like this
Now, my new computer has a better video card. It uses all 16 bits, making the pixel format look like:
So, I got to thinking… how am I going to make it so that I can specify any color to put on the screen, and have it turn out right, no matter what video card it is.
As a side note, I have written my own bitmap loader to read in 24 bit BMPs, and rectify them to a 16 bit surface (I'll probably share it at some later time, most likely in a separate article)
I sat down and set about my task. The first thing I did was open up the DirectX.hlp file, and took a look at the IDirectDrawSurface Interface. One of its member functions was called "GetPixelFormat". Okay. I had something to work with.
GetPixelFormat takes a pointer to a DDPIXELFORMAT structure (not surprisingly). I looked at the DDPIXELFORMAT structure, and I found a couple of fields that interested me… namely dwRBitMask, dwGBitMask, dwBBitMask.
Now I had all of the information I wanted, just not in a format I could use.
At this point, if we assumed that I had the correct number of bits stored in three UCHARs named Red, Green, and Blue, I could calculate the 16-bit color by doing this:
Color=Blue << dwBSHL + Green << dwGSHL + Red << dwRSHL;
However, right now, we have 8 bits in each of Red, Green, and Blue, and we need only 5 or 6. We have to be able to shift the bits of the colors to the right before shifting them to the left to make a 16-bit color.
So, let's determine how many bits to the right we have to shift them: