Archived

This topic is now archived and is closed to further replies.

JonW

Using 16-bit pixels

Recommended Posts

According to Tricks of the Windows Game Programming Gurus (by Andre LaMothe), you should store your 16-bit colors in a USHORT. As Andre seems like a relatively intelligent being, I decided to use unsigned short variables to hold my RGB values. However, when I run certain ''large'' colors through my RGB macro, such as white (255, 255, 255), the number is too big for the variable. When I render a white pixel, for example, it turns out as light blue. What''s the deal? Should I use unsigned long to hold my colors instead? Here is the macro I''m using (also from Andre): #define RGB16(r,g,b) ((b & 31) | ((g & 63) << 6) + ((r & 31) << 11)) Thanks in advance.

Share this post


Link to post
Share on other sites
Let me rewrite that in a way that makes more sense to me (and turning that + into a | would certainly make more sense). Not sure what Andre was doing there.

#define RGB16(r,g,b) (((r&31)<<11) | ((g&63)<<6) | (b&31))

Well I think I see the problem. This should be R5G6B5 format, yes? So lets think through this.

((r&31)<<11) = 0xrrrrr00000000000 // Red bits look good
((g&63)<<6) = 0x0000gggggg000000 // Hmmmm?
(b&31) = 0x00000000000bbbbb
0xrrrrXggggg0bbbbb

Well that certainly doesn''t look write... he overlapped the red and green values. the define should look like this:

#define RGB16(r,g,b) (((r&31)<<11) | ((g&63)<<5) | (b&31))

Brett

Share this post


Link to post
Share on other sites
Thanks!

I checked the book and it had:

#define RGB16BIT565(r,g,b) (b%32) + ((g%64) << 6) + ((r%32) << 11))

which is totally different from the macro I took from the code samples on the book''s cd. I don''t know which one he intended to be the right one.

I have yet to see if your change will make it small enough to fit in a USHORT, although removing an extra shift and add should make it considerable smaller

Share this post


Link to post
Share on other sites

#define RGB16BIT565(r,g,b) (b%32) + ((g%64) << 6) + ((r%32) << 11))

lol, that''s a pretty good macro if you want to suck up a ton of CPU time with 3 modulos per color conversion. If the book is filled with code like that... i dunno, you might want to try and find another book.

Brett

Share this post


Link to post
Share on other sites