Skip to content

Commit

Permalink
feat: browser optimized image scaling on request
Browse files Browse the repository at this point in the history
  • Loading branch information
RouHim committed Jan 22, 2022
1 parent 3aa9784 commit 5b717a0
Showing 1 changed file with 5 additions and 7 deletions.
12 changes: 5 additions & 7 deletions src/image_processor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,18 @@ use image::imageops::FilterType;
use image::io::Reader as ImageReader;

pub fn scale(resource_data: Vec<u8>, display_width: u32, display_height: u32) -> Vec<u8> {
let s = Instant::now();

let img = ImageReader::new(Cursor::new(resource_data))
.with_guessed_format().unwrap()
.decode().unwrap();
println!("Decoding took {}s!", s.elapsed().as_secs());

let resize = img.resize(display_width, display_height, FilterType::Nearest);
println!("Scaling took {}s!", s.elapsed().as_secs());
let resize = img.resize(
display_width,
display_height,
FilterType::Nearest
);

let mut bytes: Vec<u8> = Vec::new();
resize.write_to(&mut bytes, image::ImageOutputFormat::Png).unwrap();

println!("Buffer writing took {}s!", s.elapsed().as_secs());

bytes
}

0 comments on commit 5b717a0

Please sign in to comment.