From 772b64d2c2aed922afbd6f0d79193f0df88b8b60 Mon Sep 17 00:00:00 2001 From: Martin Bruse Date: Thu, 19 Sep 2024 13:51:02 +0200 Subject: [PATCH] Added test of ToU8ForWriting. Found a bug. --- jxl/src/image.rs | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/jxl/src/image.rs b/jxl/src/image.rs index 0803be3..4a0c5df 100644 --- a/jxl/src/image.rs +++ b/jxl/src/image.rs @@ -189,7 +189,7 @@ pub mod debug_tools { impl ToU8ForWriting for u16 { fn to_u8_for_writing(self) -> u8 { - (self as u32 * 255 / 65535) as u8 + (self as u32 >> 8) as u8 } } @@ -255,4 +255,14 @@ mod test { assert!(image.as_rect().to_pgm().starts_with(b"P5\n32 32\n255\n")); Ok(()) } + + #[cfg(feature = "debug_tools")] + #[test] + fn to_u8() { + use super::debug_tools::ToU8ForWriting; + assert!(0xd7a0u16.to_u8_for_writing() == 0xd7); + assert!(0.5f32.to_u8_for_writing() == 0x80); + assert!(0xd7a02953u32.to_u8_for_writing() == 0xd7); + assert!((half::f16::from_f32(0.5)).to_u8_for_writing() == 0x80); + } }