aboutsummaryrefslogtreecommitdiff
path: root/src/classes/texture2d.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/classes/texture2d.rs')
-rw-r--r--src/classes/texture2d.rs35
1 files changed, 25 insertions, 10 deletions
diff --git a/src/classes/texture2d.rs b/src/classes/texture2d.rs
index 372aee7..2bc1ade 100644
--- a/src/classes/texture2d.rs
+++ b/src/classes/texture2d.rs
@@ -1,4 +1,4 @@
-use super::FromValue;
+use super::{FromValue, streaminginfo::StreamingInfo};
use crate::object::Value;
use anyhow::{Result, bail};
use image::{DynamicImage, Rgb, Rgba};
@@ -6,20 +6,21 @@ use serde::Serialize;
use std::mem::transmute;
#[derive(Debug, Serialize)]
-pub struct Texture2d {
+pub struct Texture2D {
pub width: i32,
pub height: i32,
pub mip_count: i32,
pub name: String,
pub image_data: Vec<u8>,
- pub texture_format: TextureFormat,
+ pub format: TextureFormat,
pub texture_dimension: i32,
+ pub stream_data: StreamingInfo,
}
-impl FromValue for Texture2d {
+impl FromValue for Texture2D {
fn from_value(v: Value) -> Result<Self> {
let mut fields = v.as_class("Texture2D").unwrap();
- Ok(Texture2d {
+ Ok(Texture2D {
width: fields.remove("m_Width").unwrap().as_i32().unwrap(),
height: fields.remove("m_Height").unwrap().as_i32().unwrap(),
mip_count: fields.remove("m_MipCount").unwrap().as_i32().unwrap(),
@@ -28,25 +29,33 @@ impl FromValue for Texture2d {
.unwrap()
.as_i32()
.unwrap(),
- texture_format: unsafe {
+ format: unsafe {
transmute::<_, TextureFormat>(
fields.remove("m_TextureFormat").unwrap().as_i32().unwrap(),
)
},
name: fields.remove("m_Name").unwrap().as_string().unwrap(),
image_data: fields.remove("image data").unwrap().as_typeless().unwrap(),
+ stream_data: fields.remove("m_StreamData").unwrap().parse().unwrap(),
})
}
}
-impl Texture2d {
+impl Texture2D {
pub fn to_image(&self) -> Result<DynamicImage> {
let w = self.width as usize;
let h = self.height as usize;
use TextureFormat::*;
- match self.texture_format {
- DXT5 => {
+ match self.format {
+ DXT1 | DXT3 | DXT5 => {
+ use texpresso::Format::*;
let mut buf = vec![0u8; w * h * 4];
- texpresso::Format::Bc3.decompress(&self.image_data, w, h, &mut buf);
+ let format = match self.format {
+ DXT1 => Bc1,
+ DXT3 => Bc2,
+ DXT5 => Bc3,
+ _ => unreachable!(),
+ };
+ format.decompress(&self.image_data, w, h, &mut buf);
let im = image::ImageBuffer::<Rgba<u8>, Vec<_>>::from_raw(w as u32, h as u32, buf)
.unwrap();
Ok(im.into())
@@ -73,6 +82,12 @@ impl Texture2d {
.unwrap();
Ok(im.into())
}
+ RGBA32 => {
+ let buf = self.image_data.clone();
+ let im = image::ImageBuffer::<Rgba<u8>, Vec<_>>::from_raw(w as u32, h as u32, buf)
+ .unwrap();
+ Ok(im.into())
+ }
x => bail!("texture format {x:?} not supported"),
}
}