use crate::{sys, Error, Result};
const REFUSED: &[(&str, &str)] = &[(
"imagebuf:use_imagecache",
"this would make every file-backed ImageBuf a client of OpenImageIO's \
shared cache, whose invalidation on drop would then land on arbitrary \
threads; this crate does not offer the shared cache for the same reason",
)];
fn check_allowed(name: &str) -> Result<()> {
for (refused, reason) in REFUSED {
if name == *refused {
return Err(Error::operation(
"set attribute",
format!("{name} is not settable through this crate: {reason}"),
));
}
}
Ok(())
}
fn set_int(operation: &'static str, name: &str, value: i32) -> Result<()> {
check_allowed(name)?;
if sys::imageio::attribute_int(name, value) {
Ok(())
} else {
Err(Error::operation(
operation,
format!("OpenImageIO does not accept {value} for {name}"),
))
}
}
pub fn set_threads(count: u32) -> Result<()> {
set_int(
"set threads",
"threads",
i32::try_from(count).unwrap_or(i32::MAX),
)
}
pub fn threads() -> u32 {
sys::imageio::get_int_attribute("threads", 0).max(0) as u32
}
pub fn set_exr_threads(count: i32) -> Result<()> {
set_int("set exr threads", "exr_threads", count)
}
pub fn exr_threads() -> i32 {
sys::imageio::get_int_attribute("exr_threads", 0)
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[non_exhaustive]
pub struct Limits {
pub channels: u32,
pub image_size_mb: u32,
pub resolution: Option<u32>,
}
impl Limits {
pub fn current() -> Self {
Self {
channels: sys::imageio::get_int_attribute("limits:channels", 0).max(0) as u32,
image_size_mb: sys::imageio::get_int_attribute("limits:imagesize_MB", 0).max(0) as u32,
resolution: attribute_int("limits:resolution").map(|v| v.max(0) as u32),
}
}
#[must_use]
pub fn with_channels(mut self, channels: u32) -> Self {
self.channels = channels;
self
}
#[must_use]
pub fn with_image_size_mb(mut self, image_size_mb: u32) -> Self {
self.image_size_mb = image_size_mb;
self
}
#[must_use]
pub fn with_resolution(mut self, resolution: u32) -> Self {
self.resolution = Some(resolution);
self
}
}
pub fn limits() -> Limits {
Limits::current()
}
pub fn set_limits(limits: &Limits) -> Result<()> {
let clamp = |value: u32| i32::try_from(value).unwrap_or(i32::MAX);
set_int("set limits", "limits:channels", clamp(limits.channels))?;
set_int(
"set limits",
"limits:imagesize_MB",
clamp(limits.image_size_mb),
)?;
match limits.resolution {
None => Ok(()),
Some(resolution) => set_int("set limits", "limits:resolution", clamp(resolution)),
}
}
pub fn set_strict_input(strict: bool) -> Result<()> {
set_int("set strict input", "imageinput:strict", i32::from(strict))
}
pub fn strict_input() -> bool {
sys::imageio::get_int_attribute("imageinput:strict", 0) != 0
}
pub fn set_try_all_readers(try_all: bool) -> Result<()> {
set_int("set try all readers", "try_all_readers", i32::from(try_all))
}
pub fn try_all_readers() -> bool {
sys::imageio::get_int_attribute("try_all_readers", 1) != 0
}
pub fn set_plugin_search_path(path: &str) -> Result<()> {
set_attribute_string("plugin_searchpath", path)
}
pub fn plugin_search_path() -> String {
sys::imageio::get_string_attribute("plugin_searchpath", "")
}
pub fn set_font_search_path(path: &str) -> Result<()> {
set_attribute_string("font_searchpath", path)
}
pub fn font_search_path() -> String {
sys::imageio::get_string_attribute("font_searchpath", "")
}
pub fn set_attribute_int(name: &str, value: i32) -> Result<()> {
set_int("set attribute", name, value)
}
pub fn set_attribute_float(name: &str, value: f32) -> Result<()> {
check_allowed(name)?;
if sys::imageio::attribute_float(name, value) {
Ok(())
} else {
Err(Error::operation(
"set attribute",
format!("OpenImageIO does not accept {value} for {name}"),
))
}
}
pub fn set_attribute_string(name: &str, value: &str) -> Result<()> {
check_allowed(name)?;
if sys::imageio::attribute_string(name, value) {
Ok(())
} else {
Err(Error::operation(
"set attribute",
format!("OpenImageIO does not accept a string for {name}"),
))
}
}
pub fn attribute_int(name: &str) -> Option<i32> {
let mut value = 0;
sys::imageio::getattribute_int(name, &mut value).then_some(value)
}
pub fn attribute_float(name: &str) -> Option<f32> {
let mut value = 0.0;
sys::imageio::getattribute_float(name, &mut value).then_some(value)
}
pub fn attribute_string(name: &str) -> Option<String> {
let mut value = String::new();
sys::imageio::getattribute_string(name, &mut value).then_some(value)
}
pub fn version() -> (u32, u32, u32) {
let packed = sys::imageio::openimageio_version();
let packed = if packed < 0 { 0 } else { packed as u32 };
(packed / 10_000, (packed / 100) % 100, packed % 100)
}
pub fn format_names() -> Vec<String> {
split_list(&sys::imageio::get_string_attribute("format_list", ""))
}
pub fn extension_names() -> Vec<String> {
sys::imageio::get_extension_map()
.into_iter()
.flat_map(|item| item.value)
.collect()
}
pub fn library_versions() -> Vec<String> {
split_list(&sys::imageio::get_string_attribute("library_list", ""))
}
fn split_list(packed: &str) -> Vec<String> {
packed
.split(',')
.map(str::trim)
.filter(|part| !part.is_empty())
.map(str::to_owned)
.collect()
}