Skip to content

Commit

Permalink
Added test of ToU8ForWriting. Found a bug.
Browse files Browse the repository at this point in the history
  • Loading branch information
zond committed Sep 20, 2024
1 parent b56d3f5 commit 3e46bfd
Showing 1 changed file with 36 additions and 2 deletions.
38 changes: 36 additions & 2 deletions jxl/src/image.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}

Expand All @@ -201,7 +201,7 @@ pub mod debug_tools {

impl ToU8ForWriting for u32 {
fn to_u8_for_writing(self) -> u8 {
(self as f32 / (2.0f32.powi(32) - 1.0)).to_u8_for_writing()
(self >> 24) as u8
}
}

Expand Down Expand Up @@ -255,4 +255,38 @@ 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;
let mut want_u8 = 0u8;
let mut source_u16 = 0x00ffu16;
for _ in 0..256 {
assert!(source_u16.to_u8_for_writing() == want_u8);
want_u8 = want_u8.wrapping_add(1);
source_u16 = source_u16.wrapping_add(0x0100);
}
want_u8 = 0;
let mut source_f32 = 1f32 / 512f32;
for _ in 0..256 {
assert!(source_f32.to_u8_for_writing() == want_u8);
want_u8 = want_u8.wrapping_add(1);
source_f32 = source_f32 + (1f32 / 256f32);
}
want_u8 = 0;
let mut source_u32 = 0x00ffffffu32;
for _ in 0..256 {
assert!(source_u32.to_u8_for_writing() == want_u8);
want_u8 = want_u8.wrapping_add(1);
source_u32 = source_u32.wrapping_add(0x01000000);
}
want_u8 = 0;
let mut source_f16 = half::f16::from_f32(1f32 / 512f32);
for _ in 0..256 {
assert!(source_f16.to_u8_for_writing() == want_u8);
want_u8 = want_u8.wrapping_add(1);
source_f16 = source_f16 + half::f16::from_f32(1f32 / 256f32);
}
}
}

0 comments on commit 3e46bfd

Please sign in to comment.