2023-08-06 08:25:04 -04:00
|
|
|
// SPDX-FileCopyrightText: 2023 Joshua Goins <josh@redstrate.com>
|
|
|
|
// SPDX-License-Identifier: GPL-3.0-or-later
|
|
|
|
|
2023-09-22 19:17:24 -04:00
|
|
|
#![allow(clippy::needless_range_loop)]
|
|
|
|
|
2022-08-16 11:52:07 -04:00
|
|
|
use std::cmp::min;
|
|
|
|
use std::io::{Cursor, Read, Seek, SeekFrom};
|
2023-08-06 08:25:04 -04:00
|
|
|
|
|
|
|
use binrw::BinRead;
|
|
|
|
use binrw::binrw;
|
|
|
|
use bitflags::bitflags;
|
2022-08-11 15:03:12 -04:00
|
|
|
use texpresso::Format;
|
2023-10-13 16:16:04 -04:00
|
|
|
use crate::ByteSpan;
|
2023-08-06 08:25:04 -04:00
|
|
|
|
2022-08-11 09:23:15 -04:00
|
|
|
// Attributes and Format are adapted from Lumina (https://github.com/NotAdam/Lumina/blob/master/src/Lumina/Data/Files/TexFile.cs)
|
|
|
|
bitflags! {
|
2022-10-13 17:11:03 -04:00
|
|
|
#[binrw]
|
2022-08-11 09:23:15 -04:00
|
|
|
struct TextureAttribute : u32 {
|
2022-09-15 16:31:56 -04:00
|
|
|
const DISCARD_PER_FRAME = 0x1;
|
|
|
|
const DISCARD_PER_MAP = 0x2;
|
2022-08-11 09:23:15 -04:00
|
|
|
|
2022-09-15 16:31:56 -04:00
|
|
|
const MANAGED = 0x4;
|
|
|
|
const USER_MANAGED = 0x8;
|
|
|
|
const CPU_READ = 0x10;
|
|
|
|
const LOCATION_MAIN = 0x20;
|
|
|
|
const NO_GPU_READ = 0x40;
|
|
|
|
const ALIGNED_SIZE = 0x80;
|
|
|
|
const EDGE_CULLING = 0x100;
|
|
|
|
const LOCATION_ONION = 0x200;
|
|
|
|
const READ_WRITE = 0x400;
|
|
|
|
const IMMUTABLE = 0x800;
|
2022-08-11 09:23:15 -04:00
|
|
|
|
2022-09-15 16:31:56 -04:00
|
|
|
const TEXTURE_RENDER_TARGET = 0x100000;
|
|
|
|
const TEXTURE_DEPTH_STENCIL = 0x200000;
|
|
|
|
const TEXTURE_TYPE1_D = 0x400000;
|
|
|
|
const TEXTURE_TYPE2_D = 0x800000;
|
|
|
|
const TEXTURE_TYPE3_D = 0x1000000;
|
|
|
|
const TEXTURE_TYPE_CUBE = 0x2000000;
|
|
|
|
const TEXTURE_TYPE_MASK = 0x3C00000;
|
|
|
|
const TEXTURE_SWIZZLE = 0x4000000;
|
|
|
|
const TEXTURE_NO_TILED = 0x8000000;
|
|
|
|
const TEXTURE_NO_SWIZZLE = 0x80000000;
|
2022-08-11 09:23:15 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-13 17:11:03 -04:00
|
|
|
#[binrw]
|
|
|
|
#[brw(repr = u32)]
|
2022-08-11 15:03:12 -04:00
|
|
|
#[derive(Debug)]
|
|
|
|
enum TextureFormat {
|
|
|
|
B8G8R8A8 = 0x1450,
|
|
|
|
BC1 = 0x3420,
|
2022-08-16 11:52:07 -04:00
|
|
|
BC5 = 0x3431,
|
2022-08-11 09:23:15 -04:00
|
|
|
}
|
2022-08-09 19:31:32 -04:00
|
|
|
|
2022-10-13 17:11:03 -04:00
|
|
|
#[binrw]
|
2022-08-09 19:31:32 -04:00
|
|
|
#[derive(Debug)]
|
2022-09-15 16:26:31 -04:00
|
|
|
#[allow(dead_code)]
|
2022-10-13 17:11:03 -04:00
|
|
|
#[brw(little)]
|
2022-08-09 19:31:32 -04:00
|
|
|
struct TexHeader {
|
2022-08-16 11:52:07 -04:00
|
|
|
attribute: TextureAttribute,
|
2022-08-11 09:23:15 -04:00
|
|
|
format: TextureFormat,
|
2022-08-09 19:31:32 -04:00
|
|
|
|
2022-08-16 11:52:07 -04:00
|
|
|
width: u16,
|
|
|
|
height: u16,
|
|
|
|
depth: u16,
|
|
|
|
mip_levels: u16,
|
2022-08-09 19:31:32 -04:00
|
|
|
|
2022-08-16 11:52:07 -04:00
|
|
|
lod_offsets: [u32; 3],
|
|
|
|
offset_to_surface: [u32; 13],
|
2022-08-09 19:31:32 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
pub struct Texture {
|
2023-12-02 19:55:50 -05:00
|
|
|
/// Width of the texture in pixels
|
2022-08-11 15:48:28 -04:00
|
|
|
pub width: u32,
|
2023-12-02 19:55:50 -05:00
|
|
|
/// Height of the texture in pixels
|
2022-08-11 15:48:28 -04:00
|
|
|
pub height: u32,
|
2023-12-02 19:55:50 -05:00
|
|
|
/// Raw RGBA data
|
2022-08-16 11:52:07 -04:00
|
|
|
pub rgba: Vec<u8>,
|
2022-08-09 19:31:32 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Texture {
|
2023-12-02 19:55:50 -05:00
|
|
|
/// Reads an existing TEX file
|
2023-10-13 16:16:04 -04:00
|
|
|
pub fn from_existing(buffer: ByteSpan) -> Option<Texture> {
|
2022-08-09 19:31:32 -04:00
|
|
|
let mut cursor = Cursor::new(buffer);
|
2024-04-16 22:08:22 -04:00
|
|
|
let header = TexHeader::read(&mut cursor).ok()?;
|
2022-08-09 19:31:32 -04:00
|
|
|
|
2022-08-11 15:03:12 -04:00
|
|
|
// TODO: Adapted from Lumina, but this really can be written better...
|
2023-12-02 20:17:05 -05:00
|
|
|
let mut texture_data_size = vec![0; min(13, header.mip_levels as usize)];
|
2022-08-11 15:03:12 -04:00
|
|
|
let size = texture_data_size.len();
|
|
|
|
for i in 0..size - 1 {
|
|
|
|
texture_data_size[i] = header.offset_to_surface[i + 1] - header.offset_to_surface[i];
|
|
|
|
}
|
2023-04-06 14:52:04 -04:00
|
|
|
|
2022-08-16 11:52:07 -04:00
|
|
|
texture_data_size[size - 1] =
|
|
|
|
(buffer.len() - header.offset_to_surface[size - 1] as usize) as u32;
|
2022-08-11 15:03:12 -04:00
|
|
|
|
2022-08-16 11:52:07 -04:00
|
|
|
cursor
|
|
|
|
.seek(SeekFrom::Start(header.offset_to_surface[0] as u64))
|
|
|
|
.ok()?;
|
2022-08-11 15:03:12 -04:00
|
|
|
|
|
|
|
let mut src = vec![0u8; texture_data_size.iter().sum::<u32>() as usize];
|
|
|
|
cursor.read_exact(src.as_mut_slice()).ok()?;
|
|
|
|
|
2023-04-06 14:52:04 -04:00
|
|
|
let mut dst;
|
2022-08-11 15:03:12 -04:00
|
|
|
|
|
|
|
match header.format {
|
|
|
|
TextureFormat::B8G8R8A8 => {
|
2023-04-06 14:52:04 -04:00
|
|
|
dst =
|
|
|
|
vec![0u8; texture_data_size.iter().sum::<u32>() as usize];
|
|
|
|
|
2022-08-11 15:03:12 -04:00
|
|
|
dst.copy_from_slice(&src);
|
|
|
|
}
|
|
|
|
TextureFormat::BC1 => {
|
2023-04-06 14:52:04 -04:00
|
|
|
dst = vec![0u8; header.width as usize * header.height as usize * 4];
|
|
|
|
|
2022-08-11 15:03:12 -04:00
|
|
|
let format = Format::Bc1;
|
2022-08-16 11:52:07 -04:00
|
|
|
format.decompress(
|
|
|
|
&src,
|
|
|
|
header.width as usize,
|
|
|
|
header.height as usize,
|
|
|
|
dst.as_mut_slice(),
|
|
|
|
);
|
2022-08-11 15:03:12 -04:00
|
|
|
}
|
|
|
|
TextureFormat::BC5 => {
|
2023-04-06 14:52:04 -04:00
|
|
|
dst = vec![0u8; header.width as usize * header.height as usize * 4];
|
|
|
|
|
2022-08-11 15:03:12 -04:00
|
|
|
let format = Format::Bc3;
|
2022-08-16 11:52:07 -04:00
|
|
|
format.decompress(
|
|
|
|
&src,
|
|
|
|
header.width as usize,
|
|
|
|
header.height as usize,
|
|
|
|
dst.as_mut_slice(),
|
|
|
|
);
|
2022-08-11 15:03:12 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Some(Texture {
|
2022-08-11 15:47:02 -04:00
|
|
|
width: header.width as u32,
|
|
|
|
height: header.height as u32,
|
2022-08-16 11:52:07 -04:00
|
|
|
rgba: dst,
|
2022-08-11 15:03:12 -04:00
|
|
|
})
|
2022-08-09 19:31:32 -04:00
|
|
|
}
|
2022-08-16 11:52:07 -04:00
|
|
|
}
|
2024-04-16 22:08:22 -04:00
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use std::fs::read;
|
|
|
|
use std::path::PathBuf;
|
|
|
|
|
|
|
|
use super::*;
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_invalid() {
|
|
|
|
let mut d = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
|
|
|
|
d.push("resources/tests");
|
|
|
|
d.push("random");
|
|
|
|
|
|
|
|
// Feeding it invalid data should not panic
|
|
|
|
Texture::from_existing(&read(d).unwrap());
|
|
|
|
}
|
|
|
|
}
|