The fastest way from R5G6B5 to unsigned short value?

Started by
18 comments, last by GameDev.net 19 years, 9 months ago
I'm currently using this:

unsigned short rgb(short r,short g,short b)
{
	unsigned short PixelCol;
	PixelCol = (unsigned short) (r & 0xff) << 11 | (g & 0xff) << 6 | (b & 0xff);
    
	return PixelCol;
}

But speed is REALLY important, and if I call this function something about 10,000 times so it's really slowing down...
No Soup For You!Come Back 1 Year Later!
Advertisement
Hi,

Are you sure that it's this function that is slowing you down? That's about as fast as you can get, not to mention that the compiler probably optimizes it further using it's knowledge of the target architecture.

Vovan
Vovan
Quote:Original post by vovansim
Hi,

Are you sure that it's this function that is slowing you down? That's about as fast as you can get, not to mention that the compiler probably optimizes it further using it's knowledge of the target architecture.

Vovan

Yes I'm sure.
If this the fastest way to do it, so there is nothing to do...
No Soup For You!Come Back 1 Year Later!
Hi,

Well, I am by no means an expert, so there might be some way that I don't know of...

Vovan
Vovan
also, shouldn't the masks be 0x1F 0x3F 0x1F instead of 0xFF all around? you could try bumping down the inputs to char sizes... that would make all arguments fit in a single word...

The only thing faster you'll find would be:
1) Removing the & ___ masks, since anything out of range won't display the expected output anyways (since at least 1 value is in error)

2) Assmebly instructions specifically targeted for multimedia.
Is the function inlined?
For tiny functions like this, I would (in a header file, else inline expansion wont work):

inline unsigned short rgb(short r,short g,short b){return (unsigned short) (r & 0xff) << 11 | (g & 0xff) << 6 | (b & 0xff);;}


Alan
"There will come a time when you believe everything is finished. That will be the beginning." -Louis L'Amour
Assuming you are on a PC, then it is not a good idea to use shorts. Use ints, or chars - they will be faster. The masking is probably not necesary if there is only 8 bits per component.

But more importantly, I dont think that will work at all. You are trying to squeeze 24 bits in to 16 without discarding the extra bits. Here's what I use..

Packed=	(int)( ( (uint32)Col.R ) >> 3UL ) |	(int)( ( (uint32)Col.G & 0xfc ) << 3UL ) |	(int)( ( (uint32)Col.B & 0xf8 ) << 8UL );


EDIT: I think I misunderstood the requirements ;) I thought you were trying to pack 3 8-bit colour components in to 16 bit.
Your masks are wrong and you shouldn't have to do bit depth conversion in any time critical situation - you should do it while loading. Redesigning your system is probably the best optimization.

And if this is being done during loading and loading is too slow, make smaller graphics. You should have a system in place to warn you when stuff takes too long to load and loading being too slow isn't only because of code - the size of the data files is important too.

Have you profiled your code?
this is for a Pocket PC, Windows CE, not for PC.
No Soup For You!Come Back 1 Year Later!

This topic is closed to new replies.

Advertisement