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
// Copyright 2021 - 2021, Rupansh Sekar and the Kilogramme (TBD) contributors
// SPDX-License-Identifier: MPL-2.0
use image::{
    codecs::png::PngEncoder, imageops::FilterType, DynamicImage, GenericImageView, ImageResult,
};
use std::io::Cursor;

/// Load image from u8 buffer
pub fn image_from_buf(buf: &[u8]) -> ImageResult<DynamicImage> {
    image::io::Reader::new(Cursor::new(buf))
        .with_guessed_format()
        .unwrap() // Cursor doesn't cause an io error for this method
        .decode()
}

/// Resize image to largest possible size that fits the target w, h
/// uses Linear filter
pub fn im_resize_clamped(im: &DynamicImage, w: u32, h: u32) -> DynamicImage {
    im.resize(w, h, FilterType::Triangle)
}

pub fn png_encode(im: DynamicImage) -> ImageResult<Vec<u8>> {
    let mut out = Vec::new();
    let encoder = PngEncoder::new(&mut out);
    let w = im.width();
    let h = im.height();
    let color = im.color();
    encoder.encode(&im.into_bytes(), w, h, color)?;
    Ok(out)
}