General info on converting color depths

Started by
2 comments, last by Zeke 22 years, 8 months ago
I need to be able to convert images inside my app from anything higher than 16bpp down to 16bpp. I have no idea how to do this. I dont want the code to do it or anything just some information on what I have to do to convert 32bit or 24bit to 16 bit 5,6,5 format. Thanks for any help you can offer
Just my thoughts take them as you will. "People spend too much time thinking about the past, whatever else it is, its gone"-Mel Gibson, Man Without A Face
Advertisement
If you just want a "nearest colour" type reduction, it''s pretty easy. Just take each component, which will be 8-bit each and >> it 2 or 3 (depending on whether you want 6 or 5 bits of precision.

OK, you didn''t want code, but that''s the best way to explain this :

      unsigned long source = whatever;    unsigned short dest = 0;.    // assuming RGB order for both....    // extract red component    char r = (source & 0x00FF0000) >> 16;.    // convert it to 5 bits    r = r >> 3;.    // insert it into the destination    dest = r << 11;.    // similar for g and b.  


Of course, that could all be combined into on statement (getting rid of the temporary variable r but that''s how you do it for clarity''s sake.

If you want to do dithering, well that''s a whole other story. Have a look around on google for "dithering", "error diffusion", and other such terms. I''m afraid a full description of such algorithms is probably beyond the scope of these message boards.

War Worlds - A 3D Real-Time Strategy game in development.
Ok, well let me try to explain this in words to you.

Lets start off with a 32bit image and we want to convert this to 16bit. There is absolutely no way that we can preform this conversion with out loosing some data.

00000000000000000000000000000000 <- 32bit image data (binary)
0000000000000000 <- 16bit image data (binary)

Now in a 32bit image we usualy have the colors split up like this
8 bits alpha
8 bits red
8 bits green
8 bits blue
(not nessasssaraly in that order).

So lets look at this again...
RRRRRRRRGGGGGGGGBBBBBBBBAAAAAAAA <- 32bit image data (binary)

Now the only diffrence between a 24bit image and a 32bit image is that the 32bit image has an extra 8 bits for alpha. So if we get rid of the last 8 bits of data we are left with a 24bit image.

So now our image data looks like this...
RRRRRRRRGGGGGGGGBBBBBBBB <- 24bit image data (binary)

Now we want to convert this image to 16bit format. There are 2 types of 16bit formats, 5-5-5 and 5-6-5. Here is what they look like...

0RRRRRGGGGGBBBBB <- 5-5-5 (15bit in reality with an added 0 to make it 16bit)
RRRRRGGGGGGBBBBB <- 5-6-5 (16bit)

Now each part of our 24bit image(red green and blue) are made up of 8 bits, our destination is 5 bits. Which means we need to ignore 3 bits of data for each Red, Green and Blue value. Typicaly we ignore the last 3 bits of data.

RRRRR000GGGGG000BBBBB000 <- What you actualy read.
RRRRRGGGGGBBBBB <- 16 bit data.

Now all the information I just gave you can be achieved by bit shifting. By right shifting your 24 or 32bit red value by 3 you get 5 bits left over. Then you need to place this red value into your 16 bit variable the way the computer expects it to be formatted.
Joseph FernaldSoftware EngineerRed Storm Entertainment.------------------------The opinions expressed are that of the person postingand not that of Red Storm Entertainment.
Hey thanks guys between you, you explained it really well and ive managed to do it. Much appreciated
Just my thoughts take them as you will. "People spend too much time thinking about the past, whatever else it is, its gone"-Mel Gibson, Man Without A Face

This topic is closed to new replies.

Advertisement