tags:

views:

363

answers:

2

I initially was looking for a way convert byte to float, and found answers that indicate the fastest was is to create a lookup table.

So i was wondering if anyone know of a pre-existing lookup table that i can use.

+4  A: 

Normally you would initialize the lookup table using a few lines of code and a for loop or whatever suits your purpose. It's only useful if you're doing a large number of conversions on a finite number of possible inputs.

The example below is only to demonstrate the basic technique of building and using a lookup table. Unless there is more math involved, there would actually be a performance hit if you implemented this (see below).

float[] lookupTable = new float[256];
for (int i = 0; i < 256; i++)
{
    lookupTable[i] = (float)i;
}

float convertedValue = lookupTable[byteValue];

The code is C#, I have no experience with objective C. In C++ the array declaration would be a bit different, but you get the idea.

When to use a lookup table?

In the above example, there is no performance gain, because no calculation is involved, just a conversion from byte to float. Consider the case where floating point division is involved (like your case):

    lookupTable[i] = i / 255f;

In this case, the lookup table should be faster than using direct calculation. The more complex the math (trigonometry, etc.), the bigger the performance gain. Another common usage is gamma correcting an image (exponential function).

Thorarin
i m using the lookup table for converting raw YUV frame bytes into RGB float.thanks for the code.
ReachConnection
+2  A: 

Lookup table? We don't need no stinkin' lookup tables!

float floatVal = (float)byteVal;
Rob McAfee
can you really perform a direct type cast?
ReachConnection
It's not a type cast, but rather a conversion. It just looks the same as a cast.
Thorarin