1
0
Fork 0

cogl/tests: Avoid floats when converting between int formats

All we need is an intermediate int format with twice as many bits and
to put division at the end to avoid any loss of precision.

Part-of: <https://gitlab.gnome.org/GNOME/mutter/-/merge_requests/3903>
This commit is contained in:
Daniel van Vugt 2024-07-25 19:08:11 +08:00 committed by Marge Bot
parent 7df6b5c4ed
commit 3eb32cf750

View file

@ -35,19 +35,13 @@ get_bits (uint32_t in,
static int static int
rgb16_to_rgb8 (int rgb16) rgb16_to_rgb8 (int rgb16)
{ {
float r; return (int) ((int32_t) rgb16 * 0xff / 0xffff);
r = rgb16 / (float) ((1 << 16) - 1);
return (int) (r * (float) ((1 << 8) - 1));
} }
static int static int
rgb8_to_rgb16 (int rgb8) rgb8_to_rgb16 (int rgb8)
{ {
float r; return (int) ((int32_t) rgb8 * 0xffff / 0xff);
r = rgb8 / (float) ((1 << 8) - 1);
return (int) (r * (float) ((1 << 16) - 1));
} }
static void static void