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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
use basis_universal_sys as sys;
use std::ffi::CStr;
#[derive(Copy, Clone, Debug, PartialEq)]
#[repr(u32)]
pub enum BasisTextureType {
TextureType2D = sys::basist_basis_texture_type_cBASISTexType2D,
TextureType2DArray = sys::basist_basis_texture_type_cBASISTexType2DArray,
TextureTypeCubemapArray = sys::basist_basis_texture_type_cBASISTexTypeCubemapArray,
TextureTypeVideoFrames = sys::basist_basis_texture_type_cBASISTexTypeVideoFrames,
TextureTypeVolume = sys::basist_basis_texture_type_cBASISTexTypeVolume,
}
impl Into<sys::basist_basis_texture_type> for BasisTextureType {
fn into(self) -> sys::basist_basis_texture_type {
self as sys::basist_basis_texture_type
}
}
impl From<sys::basist_basis_texture_type> for BasisTextureType {
fn from(value: sys::basist_basis_texture_type) -> Self {
unsafe { std::mem::transmute(value as u32) }
}
}
impl BasisTextureType {
pub fn texture_type_name(self) -> &'static str {
unsafe {
let value = sys::basis_get_texture_type_name(self.into());
CStr::from_ptr(value).to_str().unwrap()
}
}
}
#[allow(non_camel_case_types)]
#[derive(Copy, Clone, Debug, PartialEq)]
#[repr(i32)]
pub enum BasisTextureFormat {
ETC1S = sys::basist_basis_tex_format_cETC1S,
UASTC4x4 = sys::basist_basis_tex_format_cUASTC4x4,
}
impl Into<sys::basist_basis_tex_format> for BasisTextureFormat {
fn into(self) -> sys::basist_basis_tex_format {
self as sys::basist_basis_tex_format
}
}
impl From<sys::basist_basis_tex_format> for BasisTextureFormat {
fn from(value: sys::basist_basis_tex_format) -> Self {
unsafe { std::mem::transmute(value as i32) }
}
}
impl BasisTextureFormat {
pub fn can_transcode_to_format(
self,
transcoder_texture_format: TranscoderTextureFormat,
) -> bool {
unsafe { sys::basis_is_format_supported(transcoder_texture_format.into(), self.into()) }
}
}
#[allow(non_camel_case_types)]
#[derive(Copy, Clone, Debug, PartialEq)]
#[repr(i32)]
pub enum TranscoderTextureFormat {
ETC1_RGB = sys::basist_transcoder_texture_format_cTFETC1_RGB,
ETC2_RBG = sys::basist_transcoder_texture_format_cTFETC2_RGBA,
BC1_RGB = sys::basist_transcoder_texture_format_cTFBC1_RGB,
BC3_RGBA = sys::basist_transcoder_texture_format_cTFBC3_RGBA,
BC4_R = sys::basist_transcoder_texture_format_cTFBC4_R,
BC5_RG = sys::basist_transcoder_texture_format_cTFBC5_RG,
BC7_RGBA = sys::basist_transcoder_texture_format_cTFBC7_RGBA,
PVRTC1_4_RGB = sys::basist_transcoder_texture_format_cTFPVRTC1_4_RGB,
PVRTC1_4_RGBA = sys::basist_transcoder_texture_format_cTFPVRTC1_4_RGBA,
ASTC_4x4_RGBA = sys::basist_transcoder_texture_format_cTFASTC_4x4_RGBA,
ATC_RGB = sys::basist_transcoder_texture_format_cTFATC_RGB,
ATC_RGBA = sys::basist_transcoder_texture_format_cTFATC_RGBA,
FXT1_RGB = sys::basist_transcoder_texture_format_cTFFXT1_RGB,
PVRTC2_4_RGB = sys::basist_transcoder_texture_format_cTFPVRTC2_4_RGB,
PVRTC2_4_RGBA = sys::basist_transcoder_texture_format_cTFPVRTC2_4_RGBA,
ETC2_EAC_R11 = sys::basist_transcoder_texture_format_cTFETC2_EAC_R11,
ETC2_EAC_RG11 = sys::basist_transcoder_texture_format_cTFETC2_EAC_RG11,
RGBA32 = sys::basist_transcoder_texture_format_cTFRGBA32,
RGB565 = sys::basist_transcoder_texture_format_cTFRGB565,
BGR565 = sys::basist_transcoder_texture_format_cTFBGR565,
RGBA4444 = sys::basist_transcoder_texture_format_cTFRGBA4444,
}
impl Into<sys::basist_transcoder_texture_format> for TranscoderTextureFormat {
fn into(self) -> sys::basist_transcoder_texture_format {
self as sys::basist_transcoder_texture_format
}
}
impl From<sys::basist_transcoder_texture_format> for TranscoderTextureFormat {
fn from(value: sys::basist_transcoder_texture_format) -> Self {
unsafe { std::mem::transmute(value as i32) }
}
}
impl TranscoderTextureFormat {
pub fn bytes_per_block_or_pixel(self) -> u32 {
unsafe { sys::basis_get_bytes_per_block_or_pixel(self.into()) }
}
pub fn format_name(self) -> &'static str {
unsafe {
let value = sys::basis_get_format_name(self.into());
CStr::from_ptr(value).to_str().unwrap()
}
}
pub fn has_alpha(self) -> bool {
unsafe { sys::basis_transcoder_format_has_alpha(self.into()) }
}
pub fn is_compressed(self) -> bool {
unsafe { !sys::basis_transcoder_format_is_uncompressed(self.into()) }
}
pub fn uncompressed_bytes_per_pixel(self) -> u32 {
unsafe { sys::basis_get_uncompressed_bytes_per_pixel(self.into()) }
}
pub fn block_width(self) -> u32 {
unsafe { sys::basis_get_block_width(self.into()) }
}
pub fn block_height(self) -> u32 {
unsafe { sys::basis_get_block_height(self.into()) }
}
pub fn can_transcode_from_format(
self,
basis_texture_format: BasisTextureFormat,
) -> bool {
basis_texture_format.can_transcode_to_format(self)
}
pub fn calculate_minimum_output_buffer_blocks_or_pixels(
self,
original_width: u32,
original_height: u32,
total_slice_blocks: u32,
output_row_pitch_in_blocks_or_pixels: Option<u32>,
output_rows_in_pixels: Option<u32>,
) -> u32 {
let mut output_row_pitch_in_blocks_or_pixels =
output_row_pitch_in_blocks_or_pixels.unwrap_or(0);
let mut output_rows_in_pixels = output_rows_in_pixels.unwrap_or(0);
let minimum_output_buffer_blocks_or_pixels = if !self.is_compressed() {
if output_row_pitch_in_blocks_or_pixels == 0 {
output_row_pitch_in_blocks_or_pixels = original_width;
}
if output_rows_in_pixels == 0 {
output_rows_in_pixels = original_height;
}
output_rows_in_pixels * output_row_pitch_in_blocks_or_pixels
} else if self == TranscoderTextureFormat::FXT1_RGB {
let num_blocks_fxt1_x = (original_width + 7) / 8;
let num_blocks_fxt1_y = (original_height + 3) / 4;
num_blocks_fxt1_x * num_blocks_fxt1_y
} else {
total_slice_blocks
};
debug_assert!(self.validate_output_buffer_size(
minimum_output_buffer_blocks_or_pixels,
original_width,
original_height,
total_slice_blocks,
Some(output_row_pitch_in_blocks_or_pixels),
Some(output_rows_in_pixels),
));
minimum_output_buffer_blocks_or_pixels
}
pub fn calculate_minimum_output_buffer_bytes(
self,
original_width: u32,
original_height: u32,
total_slice_blocks: u32,
output_row_pitch_in_blocks_or_pixels: Option<u32>,
output_rows_in_pixels: Option<u32>,
) -> u32 {
self.calculate_minimum_output_buffer_blocks_or_pixels(
original_width,
original_height,
total_slice_blocks,
output_row_pitch_in_blocks_or_pixels,
output_rows_in_pixels,
) * self.bytes_per_block_or_pixel()
}
pub fn validate_output_buffer_size(
self,
output_blocks_buf_size_in_blocks_or_pixels: u32,
original_width: u32,
original_height: u32,
total_slice_blocks: u32,
output_row_pitch_in_blocks_or_pixels: Option<u32>,
output_rows_in_pixels: Option<u32>,
) -> bool {
unsafe {
sys::basis_validate_output_buffer_size(
self.into(),
output_blocks_buf_size_in_blocks_or_pixels,
original_width,
original_height,
output_row_pitch_in_blocks_or_pixels.unwrap_or(0),
output_rows_in_pixels.unwrap_or(0),
total_slice_blocks,
)
}
}
}
bitflags::bitflags! {
pub struct DecodeFlags: u32 {
const PVRTC_DECODE_TO_NEXT_POW_2 = sys::basist_basisu_decode_flags_cDecodeFlagsPVRTCDecodeToNextPow2;
const TRANSCODE_ALPHA_DATA_TO_OPAQUE_FORMATS = sys::basist_basisu_decode_flags_cDecodeFlagsTranscodeAlphaDataToOpaqueFormats;
const BC1_FORBID_THREE_COLOR_BLOCKS = sys::basist_basisu_decode_flags_cDecodeFlagsBC1ForbidThreeColorBlocks;
const OUTPUT_HAS_ALPHA_INDICES = sys::basist_basisu_decode_flags_cDecodeFlagsOutputHasAlphaIndices;
const HIGH_QULITY = sys::basist_basisu_decode_flags_cDecodeFlagsHighQuality;
}
}