More work on the packed HDR RGB 32-bit format.

Switched to a different chroma encoding, which is notably faster
and never produces negative values when decoded.
This commit is contained in:
Nathan Vegdahl 2020-09-11 21:57:43 +09:00
parent 339568ec0c
commit d6ab9d06be
2 changed files with 155 additions and 130 deletions

View File

@ -33,3 +33,8 @@ fn fiddle_log2(n: f32) -> i32 {
use std::f32; use std::f32;
((f32::to_bits(n) >> 23) & 0b1111_1111) as i32 - 127 ((f32::to_bits(n) >> 23) & 0b1111_1111) as i32 - 127
} }
#[inline(always)]
fn clamp_0_1(n: f32) -> f32 {
n.max(0.0).min(1.0)
}

View File

@ -1,33 +1,55 @@
//! Encoding/decoding for specialized HDR RGB 32-bit storage format. //! Encoding/decoding for a specialized HDR RGB 32-bit storage format.
//! //!
//! The motivation for this format is to separate out the luma of //! The motivation for this format is to separate out the luma of
//! the color from its chromaticity, in the same spirit as most //! the color from its chromaticity, in the same spirit as most
//! image and video compression approaches, and then allocate more //! image and video compression approaches, and then allocate more
//! data to the luma component since that's what the human eye is //! bits to storing the luma component since that's what the human
//! most sensitive to. //! eye is most sensitive to.
//! //!
//! This encoding first transforms into YCoCg colorspace, and then //! This encoding first transforms the color into a Y (luma) component
//! fiddles the resulting Y, Co, and Cg components into a special //! and two chroma components (green-magenta and red-blue), and then
//! 32-bit format. The Y component is stored as an unsigned float, //! fiddles those components into a special 32-bit format.
//! with 6 bits of exponent and 10 bits of mantissa. The Co and Cg //! The Y component is stored as an unsigned float, with 6 bits of
//! components are stored as 8-bit integers. //! exponent and 10 bits of mantissa. The two chroma components are
//! each stored as 8-bit integers.
//! //!
//! The layout is: //! The layout is:
//! //!
//! 1. Y-exponent: 6 bits //! 1. Y-exponent: 6 bits
//! 2. Y-mantissa: 10 bits //! 2. Y-mantissa: 10 bits
//! 3. Co: 8 bits //! 3. Green-Magenta: 8 bits
//! 4. Cg: 8 bits //! 4. Red-Blue: 8 bits
//! //!
//! The Y component follows the convention of a mantissa with an //! The Y-mantissa has an implicit leading one, giving 11 bits of
//! implicit leading one, giving it 11 bits of precision. The //! precision.
//! exponent has a bias of 24.
use crate::clamp_0_1;
const EXP_BIAS: i32 = 23;
/// The largest value this format can store.
///
/// More precisely, this is the largest value that can be *reliably*
/// stored.
///
/// This can be exceeded by individual channels in limited cases due
/// to the color transform used. But values *at least* this large
/// can be relied on.
pub const MAX: f32 = ((1u64 << (63 - EXP_BIAS)) - (1 << (52 - EXP_BIAS))) as f32;
/// The smallest non-zero value this format can store.
///
/// Note that since this is effectively a shared-exponent format,
/// the numerical precision of all channels depends on the magnitude
/// of the over-all RGB color.
pub const MIN: f32 = 1.0 / (1 << (EXP_BIAS - 2)) as f32;
/// Encodes three floating point RGB values into a packed 32-bit format. /// Encodes three floating point RGB values into a packed 32-bit format.
/// ///
/// Warning: negative values and NaN's are _not_ supported. There are /// Warning: negative values and NaN's are _not_ supported. There are
/// debug-only assertions in place to catch such values in the input /// debug-only assertions in place to catch such values in the input
/// floats. /// floats. Infinity in any channel will saturate the whole color to
/// the brightest representable white.
#[inline] #[inline]
pub fn encode(floats: (f32, f32, f32)) -> u32 { pub fn encode(floats: (f32, f32, f32)) -> u32 {
debug_assert!( debug_assert!(
@ -45,23 +67,15 @@ pub fn encode(floats: (f32, f32, f32)) -> u32 {
floats.2 floats.2
); );
// Convert to YCoCg colorspace. // Convert to Y/Green-Magenta/Red-Blue components.
let y = (floats.0 * 0.25) + (floats.1 * 0.5) + (floats.2 * 0.25); let u = floats.0 + floats.2;
let co = (floats.0 * 0.5) + (floats.2 * -0.5); let y = (u * 0.5) + floats.1;
let cg = (floats.0 * -0.25) + (floats.1 * 0.5) + (floats.2 * -0.25); let green_magenta = clamp_0_1(floats.1 / y);
let red_blue = if u > 0.0 {
if y <= 0.0 { clamp_0_1(floats.0 / u)
// Corner case: black. } else {
return 0; 0.5
} else if y.is_infinite() { };
// Corner case: infinite white.
return 0xffff7f7f;
}
// Encode Co and Cg as 8-bit integers.
let inv_y = 1.0 / y;
let co_8bit = ((co * inv_y * 63.5) + 127.5).min(255.0).max(0.0) as u8;
let cg_8bit = ((cg * inv_y * 127.0) + 127.5).min(255.0).max(0.0) as u8;
// Bit-fiddle to get the float components of Y. // Bit-fiddle to get the float components of Y.
// This assumes we're working with a standard 32-bit IEEE float. // This assumes we're working with a standard 32-bit IEEE float.
@ -69,22 +83,31 @@ pub fn encode(floats: (f32, f32, f32)) -> u32 {
let y_mantissa = (y_ieee_bits >> 13) & 0b11_1111_1111; let y_mantissa = (y_ieee_bits >> 13) & 0b11_1111_1111;
let y_exp = ((y_ieee_bits >> 23) & 0b1111_1111) as i32 - 127; let y_exp = ((y_ieee_bits >> 23) & 0b1111_1111) as i32 - 127;
// Encode Cg and Cr as 8-bit integers.
let gm_8bit = ((green_magenta * 254.0) + 0.5) as u8;
let rb_8bit = ((red_blue * 254.0) + 0.5) as u8;
// Pack values into a u32 and return. // Pack values into a u32 and return.
if y_exp <= -24 { if y_exp <= (0 - EXP_BIAS) {
// Corner-case: // Early-out corner-case:
// Luma is so dark that it will be zero at our precision, // Luma is so dark that it will be zero at our precision,
// and hence black. // and hence black.
0 0
} else if y_exp >= 40 { } else if y_exp >= (63 - EXP_BIAS) {
dbg!();
// Corner-case: // Corner-case:
// Luma is so bright that it exceeds our max value, so saturate // Luma is so bright that it exceeds our max value, so saturate
// the luma. // the luma.
0xffff0000 | ((co_8bit as u32) << 8) | cg_8bit as u32 if y.is_infinite() {
// If luma is infinity, our chroma values are bogus, so
// just go with white.
0xffff7f7f
} else {
0xffff0000 | ((gm_8bit as u32) << 8) | rb_8bit as u32
}
} else { } else {
// Common case. // Common case.
let exp = (y_exp + 24) as u32; let exp = (y_exp + EXP_BIAS) as u32;
(exp << 26) | (y_mantissa << 16) | ((co_8bit as u32) << 8) | cg_8bit as u32 (exp << 26) | (y_mantissa << 16) | ((gm_8bit as u32) << 8) | rb_8bit as u32
} }
} }
@ -92,29 +115,32 @@ pub fn encode(floats: (f32, f32, f32)) -> u32 {
/// floating point RGB numbers. /// floating point RGB numbers.
#[inline] #[inline]
pub fn decode(packed_rgb: u32) -> (f32, f32, f32) { pub fn decode(packed_rgb: u32) -> (f32, f32, f32) {
// Reconstruct Y, Co, and Cg from the packed bits. // Pull out Y, Green-Magenta, and Red-Blue from the packed
// bits.
let y = { let y = {
let exp = (packed_rgb & 0xfc00_0000) >> 26; let exp = (packed_rgb & 0xfc00_0000) >> 26;
if exp == 0 { if exp == 0 {
0.0 0.0
} else { } else {
f32::from_bits(((exp + 103) << 23) | ((packed_rgb & 0x03ff_0000) >> 3)) f32::from_bits(
((exp + (127 - EXP_BIAS as u32)) << 23) | ((packed_rgb & 0x03ff_0000) >> 3),
)
} }
}; };
let co = { let green_magenta = {
let co_8bit = (packed_rgb >> 8) & 0xff; let gm_8bit = (packed_rgb >> 8) & 0xff;
((co_8bit as f32) - 127.0) * (1.0 / 63.5) * y (gm_8bit as f32) * (1.0 / 254.0)
}; };
let cg = { let red_blue = {
let cg_8bit = packed_rgb & 0xff; let rb_8bit = packed_rgb & 0xff;
((cg_8bit as f32) - 127.0) * (1.0 / 127.0) * y (rb_8bit as f32) * (1.0 / 254.0)
}; };
// Convert back to RGB. // Convert back to RGB.
let tmp = y - cg; let g = y * green_magenta;
let r = (tmp + co).max(0.0); let u = (y - g) * 2.0;
let g = (y + cg).max(0.0); let r = u * red_blue;
let b = (tmp - co).max(0.0); let b = u - r;
(r, g, b) (r, g, b)
} }
@ -161,7 +187,7 @@ mod tests {
} }
#[test] #[test]
fn full_saturation() { fn color_saturation() {
let fs1 = (1.0, 0.0, 0.0); let fs1 = (1.0, 0.0, 0.0);
let fs2 = (0.0, 1.0, 0.0); let fs2 = (0.0, 1.0, 0.0);
let fs3 = (0.0, 0.0, 1.0); let fs3 = (0.0, 0.0, 1.0);
@ -172,112 +198,106 @@ mod tests {
} }
#[test] #[test]
fn saturate() { fn num_saturate() {
let fs = (10000000000000.0, 10000000000000.0, 10000000000000.0); let fs = (10000000000000.0, 10000000000000.0, 10000000000000.0);
assert_eq!( assert_eq!((MAX, MAX, MAX), round_trip(fs));
(1098974760000.0, 1098974760000.0, 1098974760000.0),
round_trip(fs)
);
} }
#[test] #[test]
fn inf_saturate() { fn num_inf_saturate() {
use std::f32::INFINITY; use std::f32::INFINITY;
let fs = (INFINITY, INFINITY, INFINITY); let fs = (INFINITY, INFINITY, INFINITY);
assert_eq!( assert_eq!((MAX, MAX, MAX), round_trip(fs));
(1098974760000.0, 1098974760000.0, 1098974760000.0),
round_trip(fs)
);
} }
#[test] #[test]
fn partial_saturate() { fn num_partial_saturate() {
let fs1 = (10000000000000.0, 0.0, 0.0); let fs1 = (10000000000000.0, 0.0, 0.0);
let fs2 = (0.0, 10000000000000.0, 0.0); let fs2 = (0.0, 10000000000000.0, 0.0);
let fs3 = (0.0, 0.0, 10000000000000.0); let fs3 = (0.0, 0.0, 10000000000000.0);
assert_eq!(round_trip(fs1), (4395899000000.0, 0.0, 0.0)); assert_eq!((MAX * 4.0, 0.0, 0.0), round_trip(fs1));
assert_eq!(round_trip(fs2), (0.0, 2197949500000.0, 0.0)); assert_eq!((0.0, MAX * 2.0, 0.0), round_trip(fs2));
assert_eq!(round_trip(fs3), (0.0, 0.0, 4395899000000.0)); assert_eq!((0.0, 0.0, MAX * 4.0), round_trip(fs3));
} }
// #[test] #[test]
// fn accuracy() { fn largest_value() {
// let mut n = 1.0; let fs1 = (MAX, MAX, MAX);
// for _ in 0..256 { let fs2 = (MAX, 0.0, 0.0);
// let (x, _, _) = round_trip((n, 0.0, 0.0)); let fs3 = (0.0, MAX, 0.0);
// assert_eq!(n, x); let fs4 = (0.0, 0.0, MAX);
// n += 1.0 / 256.0;
// }
// }
// #[test] assert_eq!(fs1, round_trip(fs1));
// fn rounding() { assert_eq!(fs2, round_trip(fs2));
// let fs = (7.0f32, 513.0f32, 1.0f32); assert_eq!(fs3, round_trip(fs3));
// assert_eq!(round_trip(fs), (8.0, 514.0, 2.0)); assert_eq!(fs4, round_trip(fs4));
// } }
// #[test] #[test]
// fn rounding_edge_case() { fn smallest_value() {
// let fs = (1023.0f32, 0.0f32, 0.0f32); let fs1 = (MIN, MIN, MIN);
let fs2 = (MIN, 0.0, 0.0);
let fs3 = (0.0, MIN, 0.0);
let fs4 = (0.0, 0.0, MIN);
// assert_eq!(round_trip(fs), (1024.0, 0.0, 0.0),); assert_eq!(fs1, round_trip(fs1));
// } assert_eq!(fs2, round_trip(fs2));
assert_eq!(fs3, round_trip(fs3));
assert_eq!(fs4, round_trip(fs4));
}
// #[test] #[test]
// fn smallest_value() { fn underflow() {
// let fs = (MIN, MIN * 0.5, MIN * 0.49); let fs1 = (MIN * 0.5, 0.0, 0.0);
// assert_eq!(round_trip(fs), (MIN, MIN, 0.0)); let fs2 = (0.0, MIN * 0.25, 0.0);
// assert_eq!(decode(0x00_80_40_00), (MIN, MIN, 0.0)); let fs3 = (0.0, 0.0, MIN * 0.5);
// }
// #[test] assert_eq!(round_trip(fs1), (0.0, 0.0, 0.0));
// fn underflow() { assert_eq!(round_trip(fs2), (0.0, 0.0, 0.0));
// let fs = (MIN * 0.49, 0.0, 0.0); assert_eq!(round_trip(fs3), (0.0, 0.0, 0.0));
// assert_eq!(encode(fs), 0); }
// assert_eq!(round_trip(fs), (0.0, 0.0, 0.0));
// }
// #[test] #[test]
// #[should_panic] #[should_panic]
// fn nans_01() { fn nans_01() {
// encode((std::f32::NAN, 0.0, 0.0)); encode((std::f32::NAN, 0.0, 0.0));
// } }
// #[test] #[test]
// #[should_panic] #[should_panic]
// fn nans_02() { fn nans_02() {
// encode((0.0, std::f32::NAN, 0.0)); encode((0.0, std::f32::NAN, 0.0));
// } }
// #[test] #[test]
// #[should_panic] #[should_panic]
// fn nans_03() { fn nans_03() {
// encode((0.0, 0.0, std::f32::NAN)); encode((0.0, 0.0, std::f32::NAN));
// } }
// #[test] #[test]
// #[should_panic] #[should_panic]
// fn negative_01() { fn negative_01() {
// encode((-1.0, 0.0, 0.0)); encode((-1.0, 0.0, 0.0));
// } }
// #[test] #[test]
// #[should_panic] #[should_panic]
// fn negative_02() { fn negative_02() {
// encode((0.0, -1.0, 0.0)); encode((0.0, -1.0, 0.0));
// } }
// #[test] #[test]
// #[should_panic] #[should_panic]
// fn negative_03() { fn negative_03() {
// encode((0.0, 0.0, -1.0)); encode((0.0, 0.0, -1.0));
// } }
// #[test] #[test]
// fn negative_04() { fn negative_04() {
// encode((-0.0, -0.0, -0.0)); encode((-0.0, -0.0, -0.0));
// } }
} }