libicon: Add tool to encode/decode custom float format
I've encoded and decoded the HVIF format's custom three byte float format by hand several times by now. This adds a python tool to automatically encode and decode these. Change-Id: I92cc1697a0b4fc3b55bb20a5bbcf6e93e3c0738b Reviewed-on: https://review.haiku-os.org/c/haiku/+/6448 Reviewed-by: waddlesplash <waddlesplash@gmail.com>
This commit is contained in:
parent
fa82fcc532
commit
74dc1de3a3
37
src/tests/libs/icon/flat_icon/FlatIconFormat.py
Normal file
37
src/tests/libs/icon/flat_icon/FlatIconFormat.py
Normal file
@ -0,0 +1,37 @@
|
||||
# Utility functions to aid in converting from the special data types the HVIF
|
||||
# (aka flat icon format) has
|
||||
|
||||
import math
|
||||
|
||||
# Example usage:
|
||||
# >>> from FlatIconFormat import *
|
||||
# >>> read_float_24(0x3ffffd)
|
||||
def read_float_24(value):
|
||||
sign = (-1) ** ((value & 0x800000) >> 23)
|
||||
exponent = 2 ** (((value & 0x7e0000) >> 17) - 32)
|
||||
mantissa = (value & 0x01ffff) * 2 ** -17
|
||||
return sign * exponent * (1 + mantissa)
|
||||
|
||||
# Example usage:
|
||||
# >>> hex(write_float_24(0.9999885559082031))
|
||||
#
|
||||
# Does not perform bounds checking. Do not input numbers that are too large.
|
||||
def write_float_24(value):
|
||||
# TODO: does not differentiate between 0.0 and -0.0
|
||||
if value >= 0:
|
||||
sign = 0
|
||||
else:
|
||||
sign = 1
|
||||
|
||||
if value != 0:
|
||||
# TODO: make sure exponent fits in 6 bits
|
||||
exponent = math.floor(math.log2(abs(value)))
|
||||
else:
|
||||
exponent = -32
|
||||
|
||||
if value != 0:
|
||||
mantissa = abs(value) / 2**exponent - 1
|
||||
else:
|
||||
mantissa = 0
|
||||
|
||||
return (sign << 23) + ((exponent+32) << 17) + math.floor(mantissa * 2 ** 17)
|
Loading…
x
Reference in New Issue
Block a user