Jump to content
  • Advertisement
Sign in to follow this  
AssDruid

The fastest way from R5G6B5 to unsigned short value?

This topic is 5096 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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...

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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...

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!