libbhoo 0.3.0

Minimal UI foundations for SpectreOS
Documentation
//! Bounded caller-owned RGBA images and object-fit geometry.
use crate::Rect;
use std::sync::{
    Arc,
    atomic::{AtomicU32, Ordering},
};
static NEXT_IMAGE_ID: AtomicU32 = AtomicU32::new(1);
/// Immutable, shared RGBA8 pixels. Loading and decoding are the host application's responsibility.
#[derive(Clone, Debug, PartialEq)]
pub struct Image {
    pub(crate) id: u32,
    pub(crate) width: u32,
    pub(crate) height: u32,
    pub(crate) msdf_px_range: f32,
    pub(crate) pixels: Arc<[u8]>,
}
impl Image {
    /// Maximum width or height (in pixels) packed into the shared 2048×2048 GPU image atlas.
    pub const ATLAS_MAX_DIMENSION: u32 = 1022;
    /// Maximum width or height (in pixels) accepted for standalone RGBA images.
    pub const MAX_DIMENSION: u32 = 4096;
    const MAX_RGBA_BYTES: usize = 64 * 1024 * 1024;

    /// Construct a bounded RGBA8 image (up to `4096×4096`, `<= 64 MiB`).
    /// Images up to `1022×1022` pack into the shared atlas; larger images render via standalone GPU textures.
    pub fn rgba(width: u32, height: u32, pixels: Vec<u8>) -> Option<Self> {
        Self::new_inner(width, height, 0.0, Self::MAX_DIMENSION, pixels)
    }
    /// Construct a Multi-Channel Signed Distance Field (MSDF) RGBA8 image (`px_range > 0`)
    /// for resolution-independent vector rendering in the GLES fragment shader.
    pub fn msdf(width: u32, height: u32, px_range: f32, pixels: Vec<u8>) -> Option<Self> {
        if !px_range.is_finite() || px_range <= 0.0 {
            return None;
        }
        Self::new_inner(width, height, px_range, Self::ATLAS_MAX_DIMENSION, pixels)
    }
    fn new_inner(
        width: u32,
        height: u32,
        msdf_px_range: f32,
        max_dimension: u32,
        pixels: Vec<u8>,
    ) -> Option<Self> {
        if width == 0 || height == 0 || width > max_dimension || height > max_dimension {
            return None;
        }
        let length = (width as usize)
            .checked_mul(height as usize)?
            .checked_mul(4)?;
        if length > Self::MAX_RGBA_BYTES || pixels.len() != length {
            return None;
        }
        let id = NEXT_IMAGE_ID
            .try_update(Ordering::Relaxed, Ordering::Relaxed, |id| id.checked_add(1))
            .ok()?;
        Some(Self {
            id,
            width,
            height,
            msdf_px_range,
            pixels: pixels.into(),
        })
    }
    /// Whether this image fits in the shared 2048×2048 GPU image atlas (`<= 1022×1022`).
    pub fn is_atlas_eligible(&self) -> bool {
        self.width <= Self::ATLAS_MAX_DIMENSION && self.height <= Self::ATLAS_MAX_DIMENSION
    }
    pub fn msdf_px_range(&self) -> f32 {
        self.msdf_px_range
    }
    pub fn width(&self) -> u32 {
        self.width
    }
    pub fn height(&self) -> u32 {
        self.height
    }
    pub fn pixels(&self) -> &[u8] {
        &self.pixels
    }
    pub fn id(&self) -> u32 {
        self.id
    }
}
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub enum ImageFit {
    #[default]
    Cover,
    Contain,
}
/// Destination and normalized source coordinates for centered object-fit.
pub fn fit_image(bounds: Rect, width: u32, height: u32, fit: ImageFit) -> Option<(Rect, [f32; 4])> {
    if width == 0
        || height == 0
        || !bounds.width.is_finite()
        || !bounds.height.is_finite()
        || bounds.width <= 0.0
        || bounds.height <= 0.0
    {
        return None;
    }
    let iw = width as f32;
    let ih = height as f32;
    match fit {
        ImageFit::Contain => {
            let scale = (bounds.width / iw).min(bounds.height / ih);
            let w = iw * scale;
            let h = ih * scale;
            Some((
                Rect {
                    x: bounds.x + (bounds.width - w) / 2.0,
                    y: bounds.y + (bounds.height - h) / 2.0,
                    width: w,
                    height: h,
                },
                [0.0, 0.0, 1.0, 1.0],
            ))
        }
        ImageFit::Cover => {
            let scale = (bounds.width / iw).max(bounds.height / ih);
            let u = bounds.width / (iw * scale);
            let v = bounds.height / (ih * scale);
            Some((
                bounds,
                [
                    (1.0 - u) / 2.0,
                    (1.0 - v) / 2.0,
                    (1.0 + u) / 2.0,
                    (1.0 + v) / 2.0,
                ],
            ))
        }
    }
}

#[cfg(test)]
#[path = "media.spec.rs"]
mod tests;