Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion crates/ltk_texture/src/dds.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ impl Dds {
#[inline]
#[must_use]
pub fn height(&self) -> u32 {
self.file.get_width()
self.file.get_height()
}

/// Get the number of mipmaps in the texture
Expand Down
2 changes: 2 additions & 0 deletions crates/ltk_texture/src/tex/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,6 @@ pub enum DecodeErr {
Bc3(&'static str),
#[error("Could not decode BC1: {0}")]
Bc1(&'static str),
#[error("Could not decode: {0}")]
ImageDds(#[from] image_dds::error::SurfaceError),
}
33 changes: 25 additions & 8 deletions crates/ltk_texture/src/tex/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -133,26 +133,43 @@ impl Tex {
// size of mip
let (w, h) = mip_dims(level);

let data = match matches!(self.format, Format::Bgra8) {
true => TexSurfaceData::Bgra8Slice(
let data = match self.format {
Format::Bgra8 => TexSurfaceData::Bgra8Slice(
// TODO: test me (this is likely wrong)
&self.data[off..off + (w * h * self.format.bytes_per_block())],
),
false => {
let mut data = vec![0; w * h];
Format::Bc1 | Format::Bc3 => {
let image_format = match self.format {
Format::Bc1 => image_dds::ImageFormat::BC1RgbaUnorm,
Format::Bc3 => image_dds::ImageFormat::BC3RgbaUnorm,
// Safety: outer match guarantees only Bc1/Bc3 reach here
_ => unsafe { unreachable_unchecked() },
};
let surface = image_dds::Surface {
width: w as u32,
height: h as u32,
depth: 1,
layers: 1,
mipmaps: 1,
image_format,
data: &self.data[off..off + mip_bytes((w, h))],
};
let rgba8 = surface.decode_layers_mipmaps_rgba8(0..1, 0..1)?;
TexSurfaceData::Rgba8Owned(rgba8.data)
}
_ => {
let mut data = vec![0u32; w * h];
let i = &self.data[off..off + mip_bytes((w, h))];
let o = &mut data;
match self.format {
Format::Etc1 => {
texture2ddecoder::decode_etc1(i, w, h, o).map_err(DecodeErr::Etc1)
}
Format::Bc1 => texture2ddecoder::decode_bc1(i, w, h, o).map_err(DecodeErr::Bc1),
Format::Bc3 => texture2ddecoder::decode_bc3(i, w, h, o).map_err(DecodeErr::Bc3),
Format::Etc2Eac => {
texture2ddecoder::decode_etc2_rgba8(i, w, h, o).map_err(DecodeErr::Etc2Eac)
}
// Safety: the outer match ensures we can't reach this arm
Format::Bgra8 => unsafe { unreachable_unchecked() },
// Safety: Bgra8/Bc1/Bc3 are handled above
Format::Bgra8 | Format::Bc1 | Format::Bc3 => unsafe { unreachable_unchecked() },
}?;
TexSurfaceData::Bgra8Owned(data)
}
Expand Down
2 changes: 2 additions & 0 deletions crates/ltk_texture/src/tex/surface.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ pub struct TexSurface<'a> {
pub enum TexSurfaceData<'a> {
Bgra8Slice(&'a [u8]),
Bgra8Owned(Vec<u32>),
Rgba8Owned(Vec<u8>),
}

impl TexSurface<'_> {
Expand All @@ -37,6 +38,7 @@ impl TexSurface<'_> {
[r, g, b, a]
})
.collect(),
TexSurfaceData::Rgba8Owned(data) => data,
},
)
.ok_or(ToImageError::InvalidContainerSize)
Expand Down
Loading