# Problem with .hdr/.pic format

So, everything was clear from the specification point of view, but when I moved to reading through some of the original implementations and reimplementations, I ran into this:

f = ldexp(1.0,rgbe[3]-(int)(128+8));


This piece calculates the signed exponent (<128 are, by specification, defined to be negative) and applies it to 2.0f. Why does he add 8 after the fact? And why doesn't he just write 136? That's biasing the encoded exponent information in favor of the smaller values, right? But why? All the values I load with this seem to be in a really cramped floating point space [I'm not even sure anything uses the space above 1.0f]. I understand the ratios remain consistent, but I kind of prefer to rely on original measured radiances.

http://www.graphics.cornell.edu/~bjw/rgbe.html — The code here states that is the standard way to decode "real pixels", which has no basis in the specification and no rationale offered.

ldexp is a function which takes two arguments, the significand and the exponent. And it multiplies the significand by 2 taken to the power of exp. If we take the significand to be 1.0 (as in your case), it reduces down to 2^x, essentially.

RGBE format encodes the exponent in such a way that values less than 128 are considered negative, easily expressed as e-128. When you decode the exponent, you need to restore the significand to its [0,1) range (the largest primary will be normalized to [0.5, 1.0) while others... Well.). To do that, you'd have to divide each of the channels by 256 (the number of values represented by a byte). And then multiply by 2^exp to get the final value.

Notice that 256 is 2^8. Also notice that in your example that the author seemingly doesn't do the 256 divide at all. Why is that? He shoved the divide into the ldexp. How?

f = ldexp(1.0,rgbe[3]-(int)(128+8));


rgbe[3] is the encoded exp.

So, let's express the exponent in a different way:

encodedExp - 128 - 8 (remember that exp = encodedExp - 128)

exp - 8

Now, apply the exponent to the base 2:

2^(exp - 8) = 2^exp * 2^-8 = 2^exp / 2^8 = 2^exp / 256

There it is! So, the author premultiplied the 2^exp calculation with 1/256.