1*74dc1de3SZardshard# Utility functions to aid in converting from the special data types the HVIF 2*74dc1de3SZardshard# (aka flat icon format) has 3*74dc1de3SZardshard 4*74dc1de3SZardshardimport math 5*74dc1de3SZardshard 6*74dc1de3SZardshard# Example usage: 7*74dc1de3SZardshard# >>> from FlatIconFormat import * 8*74dc1de3SZardshard# >>> read_float_24(0x3ffffd) 9*74dc1de3SZardsharddef read_float_24(value): 10*74dc1de3SZardshard sign = (-1) ** ((value & 0x800000) >> 23) 11*74dc1de3SZardshard exponent = 2 ** (((value & 0x7e0000) >> 17) - 32) 12*74dc1de3SZardshard mantissa = (value & 0x01ffff) * 2 ** -17 13*74dc1de3SZardshard return sign * exponent * (1 + mantissa) 14*74dc1de3SZardshard 15*74dc1de3SZardshard# Example usage: 16*74dc1de3SZardshard# >>> hex(write_float_24(0.9999885559082031)) 17*74dc1de3SZardshard# 18*74dc1de3SZardshard# Does not perform bounds checking. Do not input numbers that are too large. 19*74dc1de3SZardsharddef write_float_24(value): 20*74dc1de3SZardshard # TODO: does not differentiate between 0.0 and -0.0 21*74dc1de3SZardshard if value >= 0: 22*74dc1de3SZardshard sign = 0 23*74dc1de3SZardshard else: 24*74dc1de3SZardshard sign = 1 25*74dc1de3SZardshard 26*74dc1de3SZardshard if value != 0: 27*74dc1de3SZardshard # TODO: make sure exponent fits in 6 bits 28*74dc1de3SZardshard exponent = math.floor(math.log2(abs(value))) 29*74dc1de3SZardshard else: 30*74dc1de3SZardshard exponent = -32 31*74dc1de3SZardshard 32*74dc1de3SZardshard if value != 0: 33*74dc1de3SZardshard mantissa = abs(value) / 2**exponent - 1 34*74dc1de3SZardshard else: 35*74dc1de3SZardshard mantissa = 0 36*74dc1de3SZardshard 37*74dc1de3SZardshard return (sign << 23) + ((exponent+32) << 17) + math.floor(mantissa * 2 ** 17) 38