use super::error::ToneMappingError;
use super::operator::{ToneMapOperator, ToneMappingOperator};
#[derive(Debug, Clone)]
pub struct ToneMapConfig {
pub operator: ToneMapOperator,
pub gamma: f32,
pub apply_gamma: bool,
pub clip: bool,
}
impl Default for ToneMapConfig {
fn default() -> Self {
Self {
operator: ToneMapOperator::Aces,
gamma: 2.2,
apply_gamma: true,
clip: true,
}
}
}
#[derive(Debug, Clone)]
pub struct ToneMappingConfig {
pub operator: ToneMappingOperator,
pub exposure_stops: f32,
pub gamma: f32,
pub use_srgb_gamma: bool,
pub saturation: f32,
}
impl ToneMappingConfig {
pub fn validate(&self) -> Result<(), ToneMappingError> {
if self.gamma <= 0.0 {
return Err(ToneMappingError::InvalidConfig(format!(
"gamma must be > 0, got {}",
self.gamma
)));
}
if self.saturation < 0.0 {
return Err(ToneMappingError::InvalidConfig(format!(
"saturation must be >= 0, got {}",
self.saturation
)));
}
if let ToneMappingOperator::Custom {
shadow_gamma,
midtone_scale,
highlight_rolloff,
} = &self.operator
{
if *shadow_gamma <= 0.0 {
return Err(ToneMappingError::InvalidConfig(format!(
"Custom operator shadow_gamma must be > 0, got {shadow_gamma}"
)));
}
if *midtone_scale <= 0.0 {
return Err(ToneMappingError::InvalidConfig(format!(
"Custom operator midtone_scale must be > 0, got {midtone_scale}"
)));
}
if *highlight_rolloff <= 0.0 {
return Err(ToneMappingError::InvalidConfig(format!(
"Custom operator highlight_rolloff must be > 0, got {highlight_rolloff}"
)));
}
}
Ok(())
}
}
impl Default for ToneMappingConfig {
fn default() -> Self {
Self {
operator: ToneMappingOperator::AcesFilmic,
exposure_stops: 0.0,
gamma: 1.0,
use_srgb_gamma: false,
saturation: 1.0,
}
}
}
pub fn format_tone_config(config: &ToneMapConfig) -> String {
let op = match &config.operator {
ToneMapOperator::Reinhard => "reinhard".to_string(),
ToneMapOperator::ReinhardExtended { max_luminance } => {
format!("reinhard_extended(max_lum={max_luminance})")
}
ToneMapOperator::Filmic => "filmic".to_string(),
ToneMapOperator::Aces => "aces".to_string(),
ToneMapOperator::Lottes(params) => format!(
"lottes(contrast={},shoulder={},hdr_max={},mid_in={},mid_out={})",
params.contrast, params.shoulder, params.hdr_max, params.mid_in, params.mid_out
),
ToneMapOperator::Exposure { stops } => format!("exposure(stops={stops})"),
ToneMapOperator::Linear { min, max } => format!("linear(min={min},max={max})"),
};
format!(
"ToneMapConfig {{ op={op}, gamma={}, apply_gamma={}, clip={} }}",
config.gamma, config.apply_gamma, config.clip
)
}