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

## 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 on other sites
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 on other sites
Quote:
 Original post by vovansimHi,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 on other sites
Hi,

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

Vovan

##### 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 on other sites
Is the function inlined?

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

##### Share on other sites
this is for a Pocket PC, Windows CE, not for PC.

1. 1
Rutin
23
2. 2
3. 3
JoeJ
20
4. 4
5. 5

• 30
• 41
• 23
• 13
• 13
• ### Forum Statistics

• Total Topics
631741
• Total Posts
3001980
×