pbrt/src/spectra/mod.rs

137 lines
4.1 KiB
Rust

use crate::globals::{ACES_TABLE, DCI_P3_TABLE, REC2020_TABLE, SRGB_TABLE};
use crate::spectra::colorspace::RGBColorSpaceData;
use anyhow::{Result, anyhow};
use shared::core::geometry::Point2f;
use shared::core::spectrum::Spectrum;
use shared::core::spectrum::StandardSpectra;
use shared::spectra::DeviceStandardColorSpaces;
use shared::spectra::cie::{CIE_D65, CIE_X, CIE_Y, CIE_Z};
use shared::utils::Ptr;
use std::sync::Arc;
use std::sync::LazyLock;
pub mod colorspace;
pub mod data;
pub mod dense;
pub mod piecewise;
pub use dense::DenselySampledSpectrumBuffer;
static CIE_X_DATA: LazyLock<DenselySampledSpectrumBuffer> =
LazyLock::new(|| data::create_cie_buffer(&CIE_X));
static CIE_Y_DATA: LazyLock<DenselySampledSpectrumBuffer> =
LazyLock::new(|| data::create_cie_buffer(&CIE_Y));
static CIE_Z_DATA: LazyLock<DenselySampledSpectrumBuffer> =
LazyLock::new(|| data::create_cie_buffer(&CIE_Z));
static CIE_D65_DATA: LazyLock<DenselySampledSpectrumBuffer> =
LazyLock::new(|| data::create_cie_buffer(&CIE_D65));
fn get_d65_illuminant_buffer() -> Arc<DenselySampledSpectrumBuffer> {
Arc::from(*&CIE_D65_DATA)
}
pub fn cie_x() -> Spectrum {
Spectrum::Dense(CIE_X_DATA.device())
}
pub fn cie_y() -> Spectrum {
Spectrum::Dense(CIE_Y_DATA.device())
}
pub fn cie_z() -> Spectrum {
Spectrum::Dense(CIE_Z_DATA.device())
}
pub fn cie_d65() -> Spectrum {
Spectrum::Dense(CIE_D65_DATA.device())
}
pub fn get_spectra_context() -> StandardSpectra {
StandardSpectra {
x: CIE_X_DATA.device(),
y: CIE_Y_DATA.device(),
z: CIE_Z_DATA.device(),
d65: CIE_D65_DATA.device(),
}
}
pub static SRGB: LazyLock<Arc<RGBColorSpaceData>> = LazyLock::new(|| {
let illum = get_d65_illuminant_buffer();
let r = Point2f::new(0.64, 0.33);
let g = Point2f::new(0.3, 0.6);
let b = Point2f::new(0.15, 0.06);
let table_ptr = Ptr::from(&SRGB_TABLE.clone());
Arc::new(RGBColorSpaceData::new(r, g, b, illum, table_ptr))
});
pub static DCI_P3: LazyLock<Arc<RGBColorSpaceData>> = LazyLock::new(|| {
let illum = get_d65_illuminant_buffer();
let r = Point2f::new(0.680, 0.320);
let g = Point2f::new(0.265, 0.690);
let b = Point2f::new(0.150, 0.060);
let table_ptr = Ptr::from(&DCI_P3_TABLE.clone());
Arc::new(RGBColorSpaceData::new(r, g, b, illum, table_ptr))
});
pub static REC2020: LazyLock<Arc<RGBColorSpaceData>> = LazyLock::new(|| {
let illum = get_d65_illuminant_buffer();
let r = Point2f::new(0.708, 0.292);
let g = Point2f::new(0.170, 0.797);
let b = Point2f::new(0.131, 0.046);
let table_ptr = Ptr::from(&REC2020_TABLE.clone());
Arc::new(RGBColorSpaceData::new(r, g, b, illum, table_ptr))
});
pub static ACES: LazyLock<Arc<RGBColorSpaceData>> = LazyLock::new(|| {
let illum = get_d65_illuminant_buffer();
let r = Point2f::new(0.7347, 0.2653);
let g = Point2f::new(0.0000, 1.0000);
let b = Point2f::new(0.0001, -0.0770);
let table_ptr = Ptr::from(&ACES_TABLE.clone());
Arc::new(RGBColorSpaceData::new(r, g, b, illum, table_ptr))
});
#[derive(Debug, Clone)]
pub struct StandardColorSpaces {
pub srgb: Arc<RGBColorSpaceData>,
pub dci_p3: Arc<RGBColorSpaceData>,
pub rec2020: Arc<RGBColorSpaceData>,
pub aces2065_1: Arc<RGBColorSpaceData>,
}
impl StandardColorSpaces {
pub fn get_named(&self, name: &str) -> Result<Arc<RGBColorSpaceData>> {
match name.to_lowercase().as_str() {
"srgb" => Ok(self.srgb.clone()),
"dci-p3" => Ok(self.dci_p3.clone()),
"rec2020" => Ok(self.rec2020.clone()),
"aces2065-1" => Ok(self.aces2065_1.clone()),
_ => Err(anyhow!("No such spectrum")),
}
}
}
pub fn get_colorspace_context() -> StandardColorSpaces {
StandardColorSpaces {
srgb: SRGB.clone().into(),
dci_p3: DCI_P3.clone().into(),
rec2020: REC2020.clone().into(),
aces2065_1: ACES.clone().into(),
}
}
pub fn get_colorspace_device() -> DeviceStandardColorSpaces {
DeviceStandardColorSpaces {
srgb: Ptr::from(&SRGB.view),
dci_p3: Ptr::from(&DCI_P3.view),
rec2020: Ptr::from(&REC2020.view),
aces2065_1: Ptr::from(&ACES.view),
}
}