#### Archived

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

# Converting RGB to 16-bit color - speed question

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

## Recommended Posts

Currently, I am using a macro to convert the three RGB values into a 16-bit color value. I was thinking, would it be faster to compute a 3-demensional array of the 16-bit color values, indexed by the RGB values? Example: // Current method color = rgb(red, blue, green); // Proposed method color = rgb[red][blue][green]; And if this would be faster, would it be worth using up half a MB of memory?

##### Share on other sites
Half a megabyte?

Assuming your source red, green and blue each have a range of 0-255, that''s:

256 * 256 * 256 = 16777216

Also I assume the data type of the array will be 16bits wide, so that''s:

16777216 * 2 (16bits==2bytes) = 33554432.

33554432bytes / 1024 = 32768Kilobytes

32768Kb / 1024 = 32Megabytes...

If the memory usage didn''t kill you, the cache misses certainly would. Memory runs much slower than the CPU clock speed - on modern CPUs doing things which don''t require access to memory is preferable (unless they take more cycles than an access to memory+the cost of cache misses).

Assuming the macro is just doing shifts and masking, then it''ll be better than the look up table.

--
Simon O''''Connor
Creative Asylum Ltd
www.creative-asylum.com

##### Share on other sites
It be better just to calculate it.

inline unsigned short rgb(unsigned char r, unsigned char g, unsigned char b)
{
static unsigned short Ret,tg,tb;
Ret=(r>>3);
tg=(g>>2);
tb=(b>>3);
Ret+=(tg)+(tb);
return Ret;
}

That should/would/could be a way to do it. Obviously, there''s more than one way to write this, and probably much more efficient ways (think inline assembly), but this would be much better than having a 256*256*256*2 array!!!

Billy

BillyB@mrsnj.com <-> If you''ve got any questions.

1. 1
2. 2
Rutin
22
3. 3
4. 4
JoeJ
16
5. 5

• 14
• 29
• 9
• 11
• 11
• ### Forum Statistics

• Total Topics
631775
• Total Posts
3002278
×