47 lines
1.2 KiB
Rust
47 lines
1.2 KiB
Rust
use crate::core::pbrt::Float;
|
|
use crate::spectra::color::{ColorEncoding, ColorEncodingTrait};
|
|
use half::f16;
|
|
|
|
// Allows writing generic algorithms that work on any image format.
|
|
pub trait PixelStorage: Copy + Send + Sync + 'static + PartialEq {
|
|
fn from_linear(val: Float, encoding: ColorEncoding) -> Self;
|
|
fn to_linear(self, encoding: ColorEncoding) -> Float;
|
|
}
|
|
|
|
impl PixelStorage for f32 {
|
|
#[inline(always)]
|
|
fn from_linear(val: Float, _enc: ColorEncoding) -> Self {
|
|
val
|
|
}
|
|
#[inline(always)]
|
|
fn to_linear(self, _enc: ColorEncoding) -> Float {
|
|
self
|
|
}
|
|
}
|
|
|
|
impl PixelStorage for f16 {
|
|
#[inline(always)]
|
|
fn from_linear(val: Float, _enc: ColorEncoding) -> Self {
|
|
f16::from_f32(val)
|
|
}
|
|
#[inline(always)]
|
|
fn to_linear(self, _enc: ColorEncoding) -> Float {
|
|
self.to_f32()
|
|
}
|
|
}
|
|
|
|
impl PixelStorage for u8 {
|
|
#[inline(always)]
|
|
fn from_linear(val: Float, enc: ColorEncoding) -> Self {
|
|
let mut out = [0u8];
|
|
enc.from_linear_slice(&[val], &mut out);
|
|
out[0]
|
|
}
|
|
|
|
#[inline(always)]
|
|
fn to_linear(self, enc: ColorEncoding) -> Float {
|
|
let mut out = [0.0];
|
|
enc.to_linear_slice(&[self], &mut out);
|
|
out[0]
|
|
}
|
|
}
|