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
use std::path::Path;
use std::ffi::{CString, CStr};
use crate::ffi::root::bindings::Load as bindings;
use crate::format::TexFormatType;
use crate::texture::GliTexture;
use crate::error::{Result, Error, ErrorKind};
pub fn load<T>(path: impl AsRef<Path>) -> Result<T>
where
T: GliTexture {
let c_path = path_to_cstring(path)?;
let raw_texture = unsafe {
bindings::load_by_path(c_path.as_ptr())
};
construct_texture(raw_texture)
}
pub fn load_from_memory<T>(data: &[u8], format: TexFormatType) -> Result<T>
where
T: GliTexture {
let bytes_length = data.len();
let raw_texture = unsafe {
let texture_data = CStr::from_bytes_with_nul_unchecked(data);
match format {
TexFormatType::DDS => bindings::load_dds_memory(texture_data.as_ptr(), bytes_length),
TexFormatType::KMG => bindings::load_kmg_memory(texture_data.as_ptr(), bytes_length),
TexFormatType::KTX => bindings::load_ktx_memory(texture_data.as_ptr(), bytes_length),
}
};
construct_texture(raw_texture)
}
pub fn load_dds<T>(path: impl AsRef<Path>) -> Result<T>
where
T: GliTexture {
let c_path = path_to_cstring(path)?;
let raw_texture = unsafe {
bindings::load_dds_by_path(c_path.as_ptr())
};
construct_texture(raw_texture)
}
pub fn load_ktx<T>(path: impl AsRef<Path>) -> Result<T>
where
T: GliTexture {
let c_path = path_to_cstring(path)?;
let raw_texture = unsafe {
bindings::load_ktx_by_path(c_path.as_ptr())
};
construct_texture(raw_texture)
}
pub fn load_kmg<T>(path: impl AsRef<Path>) -> Result<T>
where
T: GliTexture {
let c_path = path_to_cstring(path)?;
let raw_texture = unsafe {
bindings::load_kmg_by_path(c_path.as_ptr())
};
construct_texture(raw_texture)
}
#[inline]
fn construct_texture<T>(raw_texture: crate::ffi::root::gli::texture) -> Result<T>
where
T: GliTexture {
let dst_texture = T::from(raw_texture);
if dst_texture.empty() {
Err(Error::load_texture("Failed to load."))
} else {
Ok(dst_texture)
}
}
#[inline]
fn path_to_cstring(path: impl AsRef<Path>) -> Result<CString> {
let path_str = path.as_ref().to_str()
.ok_or(ErrorKind::Io)?;
CString::new(path_str)
.map_err(|_| ErrorKind::Io.into())
}