use super::config::{ToneMapConfig, ToneMappingConfig};
use super::error::ToneMappingError;
use super::gamma::{apply_gamma, gamma_correct, srgb_gamma};
use super::operator::apply_operator;
pub fn apply_exposure(image: &[f32], stops: f32) -> Vec<f32> {
let scale = (2.0_f32).powf(stops);
image.iter().map(|&v| v * scale).collect()
}
pub fn tone_map_image(
image: &[f32],
config: &ToneMappingConfig,
) -> Result<Vec<f32>, ToneMappingError> {
if image.is_empty() {
return Err(ToneMappingError::EmptyImage);
}
if !image.len().is_multiple_of(3) {
return Err(ToneMappingError::InvalidImage(format!(
"RGB image length must be a multiple of 3, got {}",
image.len()
)));
}
config.validate()?;
let exposure_scale = (2.0_f32).powf(config.exposure_stops);
let mut out = Vec::with_capacity(image.len());
for pixel in image.chunks_exact(3) {
let r = pixel[0] * exposure_scale;
let g = pixel[1] * exposure_scale;
let b = pixel[2] * exposure_scale;
let r = config.operator.apply_channel(r);
let g = config.operator.apply_channel(g);
let b = config.operator.apply_channel(b);
let (r, g, b) = apply_saturation_rgb(r, g, b, config.saturation);
let (r, g, b) = if config.use_srgb_gamma {
(srgb_gamma(r), srgb_gamma(g), srgb_gamma(b))
} else if (config.gamma - 1.0).abs() > 1e-6 {
(
gamma_correct(r, config.gamma),
gamma_correct(g, config.gamma),
gamma_correct(b, config.gamma),
)
} else {
(r, g, b)
};
out.push(r.clamp(0.0, 1.0));
out.push(g.clamp(0.0, 1.0));
out.push(b.clamp(0.0, 1.0));
}
Ok(out)
}
pub fn tone_map_rgba_image(
image: &[f32],
config: &ToneMappingConfig,
) -> Result<Vec<f32>, ToneMappingError> {
if image.is_empty() {
return Err(ToneMappingError::EmptyImage);
}
if !image.len().is_multiple_of(4) {
return Err(ToneMappingError::InvalidImage(format!(
"RGBA image length must be a multiple of 4, got {}",
image.len()
)));
}
config.validate()?;
let exposure_scale = (2.0_f32).powf(config.exposure_stops);
let mut out = Vec::with_capacity(image.len());
for pixel in image.chunks_exact(4) {
let alpha = pixel[3];
let r = pixel[0] * exposure_scale;
let g = pixel[1] * exposure_scale;
let b = pixel[2] * exposure_scale;
let r = config.operator.apply_channel(r);
let g = config.operator.apply_channel(g);
let b = config.operator.apply_channel(b);
let (r, g, b) = apply_saturation_rgb(r, g, b, config.saturation);
let (r, g, b) = if config.use_srgb_gamma {
(srgb_gamma(r), srgb_gamma(g), srgb_gamma(b))
} else if (config.gamma - 1.0).abs() > 1e-6 {
(
gamma_correct(r, config.gamma),
gamma_correct(g, config.gamma),
gamma_correct(b, config.gamma),
)
} else {
(r, g, b)
};
out.push(r.clamp(0.0, 1.0));
out.push(g.clamp(0.0, 1.0));
out.push(b.clamp(0.0, 1.0));
out.push(alpha);
}
Ok(out)
}
#[inline]
fn apply_saturation_rgb(r: f32, g: f32, b: f32, saturation: f32) -> (f32, f32, f32) {
let luma = 0.2126 * r + 0.7152 * g + 0.0722 * b;
let r_out = luma + saturation * (r - luma);
let g_out = luma + saturation * (g - luma);
let b_out = luma + saturation * (b - luma);
(r_out, g_out, b_out)
}
fn validate_tone_map_input(
img_len: usize,
width: usize,
height: usize,
config: &ToneMapConfig,
) -> Result<(), ToneMappingError> {
let expected = width * height * 3;
if img_len == 0 {
return Err(ToneMappingError::EmptyImage);
}
if img_len != expected {
return Err(ToneMappingError::SizeMismatch {
got: img_len,
width,
height,
channels: 3,
});
}
if config.gamma <= 0.0 {
return Err(ToneMappingError::InvalidParam {
name: "gamma".to_string(),
reason: format!("must be > 0, got {}", config.gamma),
});
}
Ok(())
}
pub fn tone_map(
img: &[f32],
width: usize,
height: usize,
config: &ToneMapConfig,
) -> Result<Vec<f32>, ToneMappingError> {
validate_tone_map_input(img.len(), width, height, config)?;
let mut out = img.to_vec();
tone_map_inplace(&mut out, width, height, config)?;
Ok(out)
}
pub fn tone_map_inplace(
img: &mut [f32],
width: usize,
height: usize,
config: &ToneMapConfig,
) -> Result<(), ToneMappingError> {
validate_tone_map_input(img.len(), width, height, config)?;
for pixel in img.chunks_exact_mut(3) {
let (mut r, mut g, mut b) = apply_operator(pixel[0], pixel[1], pixel[2], &config.operator);
if config.apply_gamma {
r = apply_gamma(r, config.gamma);
g = apply_gamma(g, config.gamma);
b = apply_gamma(b, config.gamma);
}
if config.clip {
r = r.clamp(0.0, 1.0);
g = g.clamp(0.0, 1.0);
b = b.clamp(0.0, 1.0);
}
pixel[0] = r;
pixel[1] = g;
pixel[2] = b;
}
Ok(())
}