I stated that I need to change a signed int to a float 16
That is not the actual case.
I needed to change 4 bytes to a float 16 (Half Float)
Here's the Python code to do what I needed.
Code: Select all
import struct
def HalfToFloat(h):
s = int((h >> 15) & 0x00000001) # sign
e = int((h >> 10) & 0x0000001f) # exponent
f = int(h & 0x000003ff) # fraction
if e == 0:
if f == 0:
return int(s << 31)
else:
while not (f & 0x00000400):
f <<= 1
e -= 1
e += 1
f &= ~0x00000400
print s,e,f
elif e == 31:
if f == 0:
return int((s << 31) | 0x7f800000)
else:
return int((s << 31) | 0x7f800000 | (f << 13))
e = e + (127 -15)
f = f << 13
return int((s << 31) | (e << 23) | f)
if __name__ == "__main__":
# sample (1.0) - see Wikipedia
FP16='\x00\x3c'
v = struct.unpack('H', FP16)
x = HalfToFloat(v[0])
# hack to coerce int to float
str = struct.pack('I',x)
f=struct.unpack('f', str)
# print the floating point
print f[0]Note that We ONLY do that to the V not the U
So the code needs to account for that.
print - f[0]
This is done so We don't need to flip the texture upside down.


