ltk_texture 0.6.1

Texture decoding/encoding utilities for League Toolkit
Documentation
use super::Error;
use num_enum::{IntoPrimitive, TryFromPrimitive};

#[derive(TryFromPrimitive, IntoPrimitive, Clone, Copy, Debug, Hash, PartialEq, Eq)]
#[repr(u8)]
pub enum Format {
    Etc1 = 1,
    #[num_enum(alternatives = [3])]
    Etc2Eac = 2,
    #[num_enum(alternatives = [11])]
    Bc1 = 10,
    Bc3 = 12,
    /// BC7 (`BC7_UNORM_SRGB`)
    Bc7 = 13,
    /// BC5 (`BC5_SNORM`)
    Bc5Snorm = 14,
    /// Uncompressed BGRA8
    Bgra8 = 20,
    /// Uncompressed RGBA16 half-float (`R16G16B16A16_FLOAT`)
    Rgba16Float = 21,
    /// Uncompressed RGBA32 float (`R32G32B32A32_FLOAT`)
    Rgba32Float = 22,
}

impl Format {
    pub fn from_u8(format: u8) -> Result<Self, Error> {
        Self::try_from(format).map_err(|_| Error::UnknownTextureFormat(format))
    }

    pub fn to_u8(&self) -> u8 {
        (*self).into()
    }

    /// Get the block size of the format
    pub fn block_size(&self) -> (usize, usize) {
        match self {
            Format::Bgra8 | Format::Rgba16Float | Format::Rgba32Float => (1, 1),
            _ => (4, 4),
        }
    }

    /// Get the bytes per block of the format
    pub fn bytes_per_block(&self) -> usize {
        match self {
            Format::Etc1 => 8,
            Format::Etc2Eac => 16,
            Format::Bc1 => 8,
            Format::Bc3 => 16,
            Format::Bc7 => 16,
            Format::Bc5Snorm => 16,
            Format::Bgra8 => 4,
            Format::Rgba16Float => 8,
            Format::Rgba32Float => 16,
        }
    }
}