From 74dc1de3a3c1414d29d502518fd3f68c3d335934 Mon Sep 17 00:00:00 2001 From: Zardshard <0azrune6@zard.anonaddy.com> Date: Thu, 18 May 2023 10:27:33 -0400 Subject: [PATCH] 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 --- .../libs/icon/flat_icon/FlatIconFormat.py | 37 +++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 src/tests/libs/icon/flat_icon/FlatIconFormat.py diff --git a/src/tests/libs/icon/flat_icon/FlatIconFormat.py b/src/tests/libs/icon/flat_icon/FlatIconFormat.py new file mode 100644 index 0000000000..0ef32bbde0 --- /dev/null +++ b/src/tests/libs/icon/flat_icon/FlatIconFormat.py @@ -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)