Skip to content

Commit

Permalink
Replace texpresso with patched image_dds
Browse files Browse the repository at this point in the history
  • Loading branch information
ackwell committed Nov 23, 2024
1 parent 97697b2 commit 54534b2
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 32 deletions.
65 changes: 49 additions & 16 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ image = { version = "0.25.1", default-features = false, features = [
"jpeg",
"png",
] }
image_dds = { git = "https://github.com/ackwell/image_dds.git", branch = "fix-ddsfile-feature-gate", default-features = false, features = [
"image",
] }
ironworks = { git = "https://github.com/ackwell/ironworks.git", features = [
"excel",
"sqpack",
Expand Down Expand Up @@ -54,7 +57,6 @@ seahash = "4.1.0"
serde = { version = "1.0.137", features = ["derive"] }
serde_json = "1.0.95"
strum = { version = "0.26.2", features = ["derive"] }
texpresso = "2.0.1"
thiserror = "2.0.3"
tokio = { version = "1.32.0", features = ["full", "tracing"] }
tokio-util = "0.7.4"
Expand Down
46 changes: 31 additions & 15 deletions src/asset/texture.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use std::io::Cursor;

use anyhow::Context;
use image::{DynamicImage, ImageBuffer, ImageFormat};
use image_dds::Surface;
use ironworks::{file::tex, Ironworks};
use itertools::Itertools;

Expand All @@ -28,9 +29,13 @@ pub fn read(ironworks: &Ironworks, path: &str) -> Result<DynamicImage> {
tex::Format::Bgr5a1Unorm => read_bgr5a1(texture)?,
tex::Format::Bgra8Unorm => read_bgra8(texture)?,

tex::Format::Bc1Unorm => read_texture_bc(texture, texpresso::Format::Bc1)?,
tex::Format::Bc2Unorm => read_texture_bc(texture, texpresso::Format::Bc2)?,
tex::Format::Bc3Unorm => read_texture_bc(texture, texpresso::Format::Bc3)?,
tex::Format::Bc1Unorm => read_texture_bc(texture, image_dds::ImageFormat::BC1RgbaUnorm)?,
tex::Format::Bc2Unorm => read_texture_bc(texture, image_dds::ImageFormat::BC2RgbaUnorm)?,
tex::Format::Bc3Unorm => read_texture_bc(texture, image_dds::ImageFormat::BC3RgbaUnorm)?,
tex::Format::Bc4Unorm => read_texture_bc(texture, image_dds::ImageFormat::BC4RUnorm)?,
tex::Format::Bc5Unorm => read_texture_bc(texture, image_dds::ImageFormat::BC5RgUnorm)?,
tex::Format::Bc6hFloat => read_texture_bc(texture, image_dds::ImageFormat::BC6hRgbSfloat)?,
tex::Format::Bc7Unorm => read_texture_bc(texture, image_dds::ImageFormat::BC7RgbaUnorm)?,

other => {
return Err(Error::UnsupportedSource(
Expand Down Expand Up @@ -109,20 +114,31 @@ fn read_bgra8(texture: tex::Texture) -> Result<DynamicImage> {
Ok(DynamicImage::ImageRgba8(buffer))
}

fn read_texture_bc(texture: tex::Texture, dxt_format: texpresso::Format) -> Result<DynamicImage> {
let width = usize::from(texture.width());
let height = usize::from(texture.height());
fn read_texture_bc(
texture: tex::Texture,
image_format: image_dds::ImageFormat,
) -> Result<DynamicImage> {
let surface = Surface {
width: texture.width().into(),
height: texture.height().into(),
depth: texture.depth().into(),
layers: match texture.kind() {
tex::TextureKind::Cube => 6,
tex::TextureKind::D2Array => texture.array_size().into(),
_other => 1,
},
mipmaps: texture.mip_levels().into(),
image_format,
data: texture.data(),
};

let mut dxt_buffer = vec![0; width * height * 4];
dxt_format.decompress(texture.data(), width, height, &mut dxt_buffer);
let image = surface
.decode_rgba8()
.with_context(|| format!("failed to decode {image_format:?}"))?
.to_image(0)
.context("failed to build image from buffer")?;

let image_buffer = ImageBuffer::from_raw(
width.try_into().unwrap(),
height.try_into().unwrap(),
dxt_buffer,
)
.context("failed to build image buffer")?;
Ok(DynamicImage::ImageRgba8(image_buffer))
Ok(image.into())
}

pub fn write(image: impl Into<DynamicImage>, format: ImageFormat) -> Result<Vec<u8>> {
Expand Down

0 comments on commit 54534b2

Please sign in to comment.