use super::Format;
#[cfg(feature = "intel-tex")]
use intel_tex_2::{bc1, bc3, RgbaSurface};
#[derive(Debug, Clone)]
pub struct EncodeOptions {
pub format: Format,
pub generate_mipmaps: bool,
pub mipmap_filter: MipmapFilter,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum MipmapFilter {
Nearest,
Triangle,
CatmullRom,
Lanczos3,
}
impl Default for MipmapFilter {
fn default() -> Self {
Self::Triangle
}
}
impl MipmapFilter {
fn to_image_filter(self) -> image::imageops::FilterType {
match self {
Self::Nearest => image::imageops::FilterType::Nearest,
Self::Triangle => image::imageops::FilterType::Triangle,
Self::CatmullRom => image::imageops::FilterType::CatmullRom,
Self::Lanczos3 => image::imageops::FilterType::Lanczos3,
}
}
}
impl EncodeOptions {
pub fn new(format: Format) -> Self {
Self {
format,
generate_mipmaps: false,
mipmap_filter: MipmapFilter::default(),
}
}
pub fn with_mipmaps(mut self) -> Self {
self.generate_mipmaps = true;
self
}
pub fn with_mipmap_filter(mut self, filter: MipmapFilter) -> Self {
self.mipmap_filter = filter;
self
}
}
impl Default for EncodeOptions {
fn default() -> Self {
Self::new(Format::Bc3)
}
}
pub fn encode_rgba(
width: u32,
height: u32,
rgba_data: &[u8],
format: Format,
) -> Result<Vec<u8>, EncodeError> {
match format {
Format::Bc1 => encode_bc1(width, height, rgba_data),
Format::Bc3 => encode_bc3(width, height, rgba_data),
Format::Bgra8 => encode_bgra8(rgba_data),
_ => Err(EncodeError::UnsupportedFormat(format)),
}
}
pub fn encode_rgba_with_mipmaps(
img: &image::RgbaImage,
format: Format,
filter: MipmapFilter,
) -> Result<(Vec<u8>, u32), EncodeError> {
let (width, height) = img.dimensions();
let mip_count = ((height.max(width) as f32).log2().floor() + 1.0) as u32;
let mut mip_levels = Vec::new();
let mut current_img = img.clone();
for level in 0..mip_count {
let mip_width = (width >> level).max(1);
let mip_height = (height >> level).max(1);
if level > 0 {
current_img = image::imageops::resize(
¤t_img,
mip_width,
mip_height,
filter.to_image_filter(),
);
}
mip_levels.push(current_img.clone());
}
let mut encoded_data = Vec::new();
for img in mip_levels.iter().rev() {
let (w, h) = img.dimensions();
let rgba_data = img.as_raw();
let encoded = encode_rgba(w, h, rgba_data, format)?;
encoded_data.extend_from_slice(&encoded);
}
Ok((encoded_data, mip_count))
}
#[cfg(feature = "intel-tex")]
fn encode_bc1(width: u32, height: u32, rgba_data: &[u8]) -> Result<Vec<u8>, EncodeError> {
let surface = RgbaSurface {
data: rgba_data,
width,
height,
stride: 4 * width,
};
Ok(bc1::compress_blocks(&surface))
}
#[cfg(not(feature = "intel-tex"))]
fn encode_bc1(_width: u32, _height: u32, _rgba_data: &[u8]) -> Result<Vec<u8>, EncodeError> {
Err(EncodeError::UnsupportedFormat(Format::Bc1))
}
#[cfg(feature = "intel-tex")]
fn encode_bc3(width: u32, height: u32, rgba_data: &[u8]) -> Result<Vec<u8>, EncodeError> {
let surface = RgbaSurface {
data: rgba_data,
width,
height,
stride: 4 * width,
};
Ok(bc3::compress_blocks(&surface))
}
#[cfg(not(feature = "intel-tex"))]
fn encode_bc3(_width: u32, _height: u32, _rgba_data: &[u8]) -> Result<Vec<u8>, EncodeError> {
Err(EncodeError::UnsupportedFormat(Format::Bc3))
}
fn encode_bgra8(rgba_data: &[u8]) -> Result<Vec<u8>, EncodeError> {
let mut bgra = Vec::with_capacity(rgba_data.len());
for pixel in rgba_data.chunks_exact(4) {
let [r, g, b, a] = pixel else {
return Err(EncodeError::InvalidPixelData);
};
bgra.extend_from_slice(&[*b, *g, *r, *a]);
}
Ok(bgra)
}
#[derive(Debug, thiserror::Error)]
pub enum EncodeError {
#[error("Unsupported format for encoding: {0:?}")]
UnsupportedFormat(Format),
#[error("Invalid pixel data")]
InvalidPixelData,
}