Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added test of ToU8ForWriting. #16

Merged
merged 1 commit into from
Sep 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions AUTHORS
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,4 @@ Ewout ter Hoeven <[email protected]>
Sami Boukortt
Tomáš Král <[email protected]>
Wonwoo Choi <[email protected]>
Martin Bruse <[email protected]>
71 changes: 61 additions & 10 deletions jxl/src/image.rs
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,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 * 0xff + 0x8000) / 0xffff) as u8
}
}

Expand All @@ -206,7 +206,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 as u64 * 0xff + 0x80000000) / 0xffffffff) as u8
}
}

Expand All @@ -229,6 +229,65 @@ pub mod debug_tools {
ret
}
}

#[cfg(test)]
mod test {
use super::super::Image;
use super::ToU8ForWriting;
use crate::error::Result;

#[test]
fn to_pgm() -> Result<()> {
let image = Image::<u8>::new(32, 32)?;
assert!(image.as_rect().to_pgm().starts_with(b"P5\n32 32\n255\n"));
Ok(())
}

#[test]
fn u16_to_u8() {
let mut left_source_u16 = 0xffffu16 / 510;
for want_u8 in 0x00u8..0xffu8 {
assert!(left_source_u16.to_u8_for_writing() == want_u8);
assert!((left_source_u16 + 1).to_u8_for_writing() == want_u8 + 1);
// Since we have 256 u8 values, but 0x00 and 0xff only have half the
// range, we actually get whole ranges of size 0xffff / 255.
left_source_u16 = left_source_u16.wrapping_add(0xffff / 255);
}
}

#[test]
fn f32_to_u8() {
let epsilon = 1e-4f32;
for want_u8 in 0x00u8..0xffu8 {
let threshold = 1f32 / 510f32 + (1f32 / 255f32) * (want_u8 as f32);
assert!((threshold - epsilon).to_u8_for_writing() == want_u8);
assert!((threshold + epsilon).to_u8_for_writing() == want_u8 + 1);
}
}

#[test]
fn u32_to_u8() {
let mut left_source_u32 = 0xffffffffu32 / 510;
for want_u8 in 0x00u8..0xffu8 {
assert!(left_source_u32.to_u8_for_writing() == want_u8);
assert!((left_source_u32 + 1).to_u8_for_writing() == want_u8 + 1);
// Since we have 256 u8 values, but 0x00 and 0xff only have half the
// range, we actually get whole ranges of size 0xffffffff / 255.
left_source_u32 = left_source_u32.wrapping_add(0xffffffffu32 / 255);
}
}

#[test]
fn f16_to_u8() {
let epsilon = half::f16::from_f32(1e-3f32);
for want_u8 in 0x00u8..0xffu8 {
let threshold =
half::f16::from_f32(1f32 / 510f32 + (1f32 / 255f32) * (want_u8 as f32));
assert!((threshold - epsilon).to_u8_for_writing() == want_u8);
assert!((threshold + epsilon).to_u8_for_writing() == want_u8 + 1);
}
}
}
}

#[cfg(test)]
Expand All @@ -252,12 +311,4 @@ mod test {
assert_eq!(image.as_rect_mut().row(30)[30], 1);
Ok(())
}

#[cfg(feature = "debug_tools")]
#[test]
fn to_pgm() -> Result<()> {
let image = Image::<u8>::new(32, 32)?;
assert!(image.as_rect().to_pgm().starts_with(b"P5\n32 32\n255\n"));
Ok(())
}
}