TextureSync/server/texture-sync-server/src/persistency/image_convert/mod.rs

24 lines
703 B
Rust
Raw Normal View History

2019-05-09 20:13:15 +02:00
use crate::model::*;
use ::image::*;
2019-04-20 01:24:19 +02:00
2019-05-09 20:13:15 +02:00
// TODO: Deside on ::image::FilterType;
// TODO: Think about size; Hard Code??
2019-04-20 01:24:19 +02:00
2019-05-09 20:13:15 +02:00
const RESIZE: u32 = 256;
2019-04-20 01:24:19 +02:00
2019-05-09 20:13:15 +02:00
pub fn generate_preview(input: &[u8], format: TextureFormat) -> ImageResult<Vec<u8>> {
// Yes, this guesses the format :D
// Also the resize function takes a maximum size and preservs the ratio.
let img =
::image::load_from_memory(input)?.resize(RESIZE, RESIZE, ::image::FilterType::Lanczos3);
let mut out: Vec<u8> = Vec::new();
match format {
TextureFormat::JPEG => img.write_to(&mut out, ImageOutputFormat::JPEG(95))?,
TextureFormat::PNG => img.write_to(&mut out, ImageOutputFormat::PNG)?,
}
2019-04-20 01:24:19 +02:00
2019-05-09 20:13:15 +02:00
Ok(out)
2019-04-24 17:01:43 +02:00
}