use crate::limits::ParseLimits;
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub enum DecodeThreads {
#[default]
Available,
Fixed(usize),
}
impl DecodeThreads {
pub(crate) fn worker_count(self) -> usize {
match self {
DecodeThreads::Available => std::thread::available_parallelism()
.map(|count| count.get())
.unwrap_or(1),
DecodeThreads::Fixed(count) => count.max(1),
}
}
}
#[derive(Clone, Copy, Debug)]
pub struct HeicSettings {
pub threads: DecodeThreads,
pub limits: ParseLimits,
pub decode_alpha: bool,
pub decode_gain_map: bool,
}
impl HeicSettings {
pub const fn new() -> Self {
Self {
threads: DecodeThreads::Available,
limits: ParseLimits::new(),
decode_alpha: true,
decode_gain_map: true,
}
}
pub const fn with_threads(mut self, threads: usize) -> Self {
self.threads = DecodeThreads::Fixed(threads);
self
}
pub const fn with_limits(mut self, limits: ParseLimits) -> Self {
self.limits = limits;
self
}
pub const fn with_decode_alpha(mut self, decode_alpha: bool) -> Self {
self.decode_alpha = decode_alpha;
self
}
pub const fn with_decode_gain_map(mut self, decode_gain_map: bool) -> Self {
self.decode_gain_map = decode_gain_map;
self
}
pub const fn primary_only(mut self) -> Self {
self.decode_alpha = false;
self.decode_gain_map = false;
self
}
}
impl Default for HeicSettings {
fn default() -> Self {
Self::new()
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn defaults_preserve_auxiliary_decoding() {
let settings = HeicSettings::default();
assert_eq!(settings.threads, DecodeThreads::Available);
assert!(settings.decode_alpha);
assert!(settings.decode_gain_map);
}
#[test]
fn primary_only_disables_auxiliary_payloads() {
let settings = HeicSettings::new().primary_only();
assert!(!settings.decode_alpha);
assert!(!settings.decode_gain_map);
}
#[test]
fn fixed_zero_is_resolved_to_one_worker() {
assert_eq!(DecodeThreads::Fixed(0).worker_count(), 1);
}
}