use std::path::Path;
use std::ffi::OsStr;
use crate::ffi::root::bindings;
use crate::texture::GliTexture;
use crate::error::{Result, Error};
use std::ffi::CString;
pub fn save(texture: &impl GliTexture, path: impl AsRef<Path>) -> Result<()> {
if let Some(dst_extension) = path.as_ref().extension().and_then(OsStr::to_str) {
match dst_extension {
| "dds" => save_dds(texture, path),
| "ktx" => save_ktx(texture, path),
| "kmg" => save_kmg(texture, path),
| _ => Err(Error::save_texture(format!("Saving {} format is not support.", dst_extension)))
}
} else {
Err(Error::save_texture("Invalid path to save texture."))
}
}
pub fn save_dds(texture: &impl GliTexture, path: impl AsRef<Path>) -> Result<()> {
let dst_path = path.as_ref().to_str()
.and_then(|p| CString::new(p).ok())
.ok_or(Error::save_texture("Invalid file path."))?;
if unsafe { bindings::Save::save_save_dds(texture.raw_texture(), dst_path.as_ptr()) } {
Ok(())
} else {
Err(Error::save_texture("Failed to save dds texture."))
}
}
pub fn save_ktx(texture: &impl GliTexture, path: impl AsRef<Path>) -> Result<()> {
let dst_path = path.as_ref().to_str()
.and_then(|p| CString::new(p).ok())
.ok_or(Error::save_texture("Invalid file path."))?;
if unsafe { bindings::Save::save_save_ktx(texture.raw_texture(), dst_path.as_ptr()) } {
Ok(())
} else {
Err(Error::save_texture("Failed to save ktx texture."))
}
}
pub fn save_kmg(texture: &impl GliTexture, path: impl AsRef<Path>) -> Result<()> {
let dst_path = path.as_ref().to_str()
.and_then(|p| CString::new(p).ok())
.ok_or(Error::save_texture("Invalid file path."))?;
if unsafe { bindings::Save::save_save_kmg(texture.raw_texture(), dst_path.as_ptr()) } {
Ok(())
} else {
Err(Error::save_texture("Failed to save kmg texture."))
}
}