-
Notifications
You must be signed in to change notification settings - Fork 0
/
py_ffi.rs
226 lines (206 loc) · 5.36 KB
/
py_ffi.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
use pyo3::exceptions::PyException;
use pyo3::prelude::*;
use pyo3::wrap_pyfunction;
use super::*;
#[pyclass]
#[derive(Debug, PartialEq, Clone)]
pub struct PyTextureAtlas {
#[pyo3(get, set)]
pub textures: Vec<PyTexture>,
}
#[pyclass]
#[derive(Debug, PartialEq, Clone)]
pub struct PyTexture {
#[pyo3(get, set)]
pub subtextures: Vec<PySubtexture>,
}
#[pyclass]
#[derive(Debug, PartialEq, Clone)]
pub struct PySubtexture {
#[pyo3(get, set)]
pub mipmaps: Vec<PyMipmap>,
}
#[pyclass]
#[derive(Debug, PartialEq, Clone)]
pub struct PyMipmap {
id: u32,
#[pyo3(get, set)]
pub width: u32,
#[pyo3(get, set)]
pub height: u32,
#[pyo3(get, set)]
pub format: TextureFormat,
#[pyo3(get, set)]
pub data: Vec<u8>,
}
#[pymethods]
impl PyMipmap {
#[cfg(feature = "image")]
fn to_rgb(&self) -> Option<Vec<(u8, u8, u8)>> {
let sub: Mipmap<'_> = self.clone().into();
sub.to_dynamic_image().map(|x| {
x.to_rgb()
.pixels()
.map(|x| (x.0[0], x.0[1], x.0[2]))
.collect()
})
}
#[cfg(feature = "image")]
fn to_rgba(&self) -> Option<Vec<(u8, u8, u8, u8)>> {
let sub: Mipmap<'_> = self.clone().into();
sub.to_dynamic_image().map(|x| {
x.to_rgba()
.pixels()
.map(|x| (x.0[0], x.0[1], x.0[2], x.0[3]))
.collect()
})
}
fn __repr__(&self) -> PyResult<String> {
Ok(format!(
"PyMipMap: {:?} {}x{} ({} bytes)",
self.format,
self.width,
self.height,
self.data.len()
))
}
}
impl<'a> From<TextureAtlas<'a>> for PyTextureAtlas {
fn from(atlas: TextureAtlas<'a>) -> Self {
let textures = atlas.0.into_iter().map(Into::into).collect();
Self { textures }
}
}
impl<'a> From<Texture<'a>> for PyTexture {
fn from(tex: Texture<'a>) -> Self {
let subtextures = tex.subtextures.into_iter().map(Into::into).collect();
Self { subtextures }
}
}
impl<'a> From<PyTexture> for Texture<'a> {
fn from(tex: PyTexture) -> Self {
let subtextures = tex.subtextures.into_iter().map(Into::into).collect();
Self { subtextures }
}
}
impl<'a> From<super::Subtexture<'a>> for PySubtexture {
fn from(subtex: super::Subtexture<'a>) -> Self {
let mipmaps = subtex.mipmaps.into_iter().map(Into::into).collect();
Self { mipmaps }
}
}
impl<'a> From<PySubtexture> for super::Subtexture<'a> {
fn from(subtex: PySubtexture) -> Self {
let mipmaps = subtex.mipmaps.into_iter().map(Into::into).collect();
Self { mipmaps }
}
}
impl<'a> From<Mipmap<'a>> for PyMipmap {
fn from(sub: Mipmap<'a>) -> Self {
let Mipmap {
id,
width,
height,
format,
data,
} = sub;
let data = data.into_owned();
Self {
id,
width,
height,
format,
data,
}
}
}
impl<'a> From<PyMipmap> for Mipmap<'a> {
fn from(mip: PyMipmap) -> Self {
let PyMipmap {
id,
width,
height,
format,
data,
} = mip;
let data = data.into();
Self {
id,
width,
height,
format,
data,
}
}
}
#[pymethods]
impl PyTextureAtlas {
fn __repr__(&self) -> PyResult<String> {
Ok(format!(
"PyTextureAtlas: {} texture(s)",
self.textures.len()
))
}
}
struct ExternalError<E>(E);
impl<E: std::error::Error> From<ExternalError<E>> for PyErr {
fn from(err: ExternalError<E>) -> Self {
PyException::new_err(err.0.to_string())
}
}
#[pymethods]
impl PyTexture {
fn to_dds_bytes(&self) -> PyResult<Vec<u8>> {
let tex: Texture<'_> = self.clone().into();
let dds = tex.to_dds().map_err(ExternalError)?;
let mut vec = vec![];
dds.write(&mut vec).map_err(ExternalError)?;
Ok(vec)
}
fn __repr__(&self) -> PyResult<String> {
let mip = match self.subtextures.get(0).and_then(|x| x.mipmaps.get(0)) {
Some(m) => format!(" {:?} {}x{}", m.format, m.width, m.height),
None => "".to_string(),
};
Ok(format!(
"PyTexture: {} subtexture(s){}",
self.subtextures.len(),
mip
))
}
}
#[pymethods]
impl PySubtexture {
fn __repr__(&self) -> PyResult<String> {
let mip = self
.mipmaps
.get(0)
.map(|m| format!(" {:?} {}x{}", m.format, m.width, m.height))
.unwrap_or_default();
Ok(format!(
"Subtexture: {} mipmap(s){}",
self.mipmaps.len(),
mip
))
}
}
#[pyfunction]
fn read(path: String) -> PyResult<PyTextureAtlas> {
use std::fs::File;
use std::io::Read;
let mut file = File::open(path)?;
let mut input = vec![];
file.read_to_end(&mut input)?;
let (_, txp) = TextureAtlas::parse(&input).unwrap();
Ok(txp.into())
}
#[pymodule]
fn txp(_py: Python<'_>, m: &PyModule) -> PyResult<()> {
pyo3_log::init();
m.add_wrapped(wrap_pyfunction!(self::read))?;
m.add_class::<PyTextureAtlas>()?;
m.add_class::<PyTexture>()?;
m.add_class::<PyMipmap>()?;
m.add_class::<TextureFormat>()?;
Ok(())
}