use std::{collections::TryReserveError, error::Error, fmt};
use crate::identity::ApplicationId;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct ImageAssetLimits {
max_images: usize,
max_width: u32,
max_height: u32,
max_pixel_bytes: usize,
}
impl ImageAssetLimits {
pub const fn new(
max_images: usize,
max_width: u32,
max_height: u32,
max_pixel_bytes: usize,
) -> Self {
Self {
max_images,
max_width,
max_height,
max_pixel_bytes,
}
}
pub const fn max_images(self) -> usize {
self.max_images
}
pub const fn max_width(self) -> u32 {
self.max_width
}
pub const fn max_height(self) -> u32 {
self.max_height
}
pub const fn max_pixel_bytes(self) -> usize {
self.max_pixel_bytes
}
}
impl Default for ImageAssetLimits {
fn default() -> Self {
Self::new(64, 4096, 4096, 64 * 1024 * 1024)
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct ImageAssetId {
application: ApplicationId,
slot: usize,
width: u32,
height: u32,
}
impl ImageAssetId {
pub const fn width(self) -> u32 {
self.width
}
pub const fn height(self) -> u32 {
self.height
}
}
#[derive(Debug)]
pub struct ImageAsset {
width: u32,
height: u32,
pixels: Vec<u8>,
}
impl ImageAsset {
pub const fn width(&self) -> u32 {
self.width
}
pub const fn height(&self) -> u32 {
self.height
}
pub fn pixels(&self) -> &[u8] {
&self.pixels
}
}
#[derive(Debug)]
#[non_exhaustive]
pub enum ImageAssetError {
InvalidDimensions {
width: u32,
height: u32,
},
DimensionsExceeded {
width: u32,
height: u32,
max_width: u32,
max_height: u32,
},
PixelByteCountOverflow {
width: u32,
height: u32,
},
PixelLengthMismatch {
expected: usize,
actual: usize,
},
ImageLimitExceeded {
limit: usize,
},
PixelByteLimitExceeded {
limit: usize,
retained: usize,
incoming: usize,
},
AllocationFailed {
source: TryReserveError,
},
}
impl fmt::Display for ImageAssetError {
fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::InvalidDimensions { width, height } => write!(
formatter,
"image dimensions must be positive, got {width} by {height}"
),
Self::DimensionsExceeded {
width,
height,
max_width,
max_height,
} => write!(
formatter,
"image dimensions {width} by {height} exceed limits {max_width} by {max_height}"
),
Self::PixelByteCountOverflow { width, height } => write!(
formatter,
"RGBA8 byte length overflows for image dimensions {width} by {height}"
),
Self::PixelLengthMismatch { expected, actual } => write!(
formatter,
"RGBA8 image requires exactly {expected} bytes, got {actual}"
),
Self::ImageLimitExceeded { limit } => {
write!(
formatter,
"image registry reached its limit of {limit} entries"
)
}
Self::PixelByteLimitExceeded {
limit,
retained,
incoming,
} => write!(
formatter,
"image pixel capacity {retained} plus {incoming} bytes exceeds limit {limit}"
),
Self::AllocationFailed { source } => {
write!(formatter, "image asset storage allocation failed: {source}")
}
}
}
}
impl Error for ImageAssetError {
fn source(&self) -> Option<&(dyn Error + 'static)> {
match self {
Self::AllocationFailed { source } => Some(source),
_ => None,
}
}
}
#[derive(Debug)]
pub(crate) struct ImageAssetRegistry {
application: ApplicationId,
limits: ImageAssetLimits,
images: Vec<ImageAsset>,
pixel_bytes: usize,
}
impl ImageAssetRegistry {
pub(crate) const fn new(application: ApplicationId, limits: ImageAssetLimits) -> Self {
Self {
application,
limits,
images: Vec::new(),
pixel_bytes: 0,
}
}
pub(crate) fn register(
&mut self,
width: u32,
height: u32,
pixels: &[u8],
) -> Result<ImageAssetId, ImageAssetError> {
if width == 0 || height == 0 {
return Err(ImageAssetError::InvalidDimensions { width, height });
}
if width > self.limits.max_width || height > self.limits.max_height {
return Err(ImageAssetError::DimensionsExceeded {
width,
height,
max_width: self.limits.max_width,
max_height: self.limits.max_height,
});
}
let pixel_length = usize::try_from(width)
.ok()
.zip(usize::try_from(height).ok())
.and_then(|(width, height)| width.checked_mul(height))
.and_then(|texels| texels.checked_mul(4))
.ok_or(ImageAssetError::PixelByteCountOverflow { width, height })?;
if pixels.len() != pixel_length {
return Err(ImageAssetError::PixelLengthMismatch {
expected: pixel_length,
actual: pixels.len(),
});
}
if self.images.len() >= self.limits.max_images {
return Err(ImageAssetError::ImageLimitExceeded {
limit: self.limits.max_images,
});
}
self.check_pixel_capacity(pixel_length)?;
let mut owned = Vec::new();
owned
.try_reserve_exact(pixel_length)
.map_err(|source| ImageAssetError::AllocationFailed { source })?;
self.check_pixel_capacity(owned.capacity())?;
owned.extend_from_slice(pixels);
self.images
.try_reserve_exact(1)
.map_err(|source| ImageAssetError::AllocationFailed { source })?;
let id = ImageAssetId {
application: self.application,
slot: self.images.len(),
width,
height,
};
self.pixel_bytes += owned.capacity();
self.images.push(ImageAsset {
width,
height,
pixels: owned,
});
Ok(id)
}
pub(crate) fn get(&self, id: ImageAssetId) -> Option<&ImageAsset> {
if id.application != self.application {
return None;
}
self.images
.get(id.slot)
.filter(|image| image.width == id.width && image.height == id.height)
}
pub(crate) fn len(&self) -> usize {
self.images.len()
}
pub(crate) const fn pixel_bytes(&self) -> usize {
self.pixel_bytes
}
fn check_pixel_capacity(&self, incoming: usize) -> Result<(), ImageAssetError> {
if incoming > self.limits.max_pixel_bytes.saturating_sub(self.pixel_bytes) {
return Err(ImageAssetError::PixelByteLimitExceeded {
limit: self.limits.max_pixel_bytes,
retained: self.pixel_bytes,
incoming,
});
}
Ok(())
}
}
#[cfg(test)]
#[path = "image_tests.rs"]
mod tests;