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.rs37
1 files changed, 20 insertions, 17 deletions
diff --git a/src/classes/texture2d.rs b/src/classes/texture2d.rs
index 147cb5d..752bfeb 100644
--- a/src/classes/texture2d.rs
+++ b/src/classes/texture2d.rs
@@ -1,10 +1,9 @@
use super::streaminginfo::StreamingInfo;
use crate::object::{Value, parser::FromValue};
-use anyhow::{Result, bail};
+use anyhow::{Result, anyhow, bail};
use image::{DynamicImage, ImageBuffer, Luma, Rgb, Rgba};
use log::info;
use serde::Serialize;
-use std::mem::transmute;
#[derive(Debug, Serialize)]
pub struct Texture2D {
@@ -23,25 +22,18 @@ impl FromValue for Texture2D {
fn from_value(v: Value) -> Result<Self> {
let mut fields = v.as_class("Texture2D").unwrap();
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(),
- texture_dimension: fields
- .remove("m_TextureDimension")
- .unwrap()
- .as_i32()
- .unwrap(),
- format: unsafe {
- transmute::<_, TextureFormat>(
- fields.remove("m_TextureFormat").unwrap().as_i32().unwrap(),
- )
- },
- name: fields.remove("m_Name").unwrap().as_string().unwrap(),
+ width: fields.field("m_Width")?,
+ height: fields.field("m_Height")?,
+ mip_count: fields.field("m_MipCount")?,
+ texture_dimension: fields.field("m_TextureDimension")?,
+ format: fields.field("m_TextureFormat")?,
+ name: fields.field("m_Name")?,
+ stream_data: fields.field("m_StreamData")?,
image_data: fields.remove("image data").unwrap().as_typeless().unwrap(),
- stream_data: fields.remove("m_StreamData").unwrap().parse().unwrap(),
})
}
}
+
impl Texture2D {
pub fn to_image(&self) -> Result<DynamicImage> {
let w = self.width as usize;
@@ -142,6 +134,17 @@ impl Texture2D {
}
}
+impl FromValue for TextureFormat {
+ fn from_value(v: Value) -> Result<Self> {
+ let x = v.as_i32().ok_or(anyhow!("expected i32 TextureFormat"))?;
+ if x < 72 {
+ Ok(unsafe { std::mem::transmute(x) })
+ } else {
+ bail!("TextureFormat out of range")
+ }
+ }
+}
+
#[allow(non_camel_case_types)]
#[repr(i32)]
#[derive(Debug, Serialize, PartialEq, Clone, Copy)]