wonfy_tools/util/image/
encode_image.rs1use std::{io::Cursor, ops::Deref};
2
3use image::{EncodableLayout, ImageBuffer, ImageFormat, ImageResult, PixelWithColorType};
4
5pub fn encode_image_as<P: image::Pixel, Container>(
6 image: &ImageBuffer<P, Container>,
7 format: ImageFormat,
8) -> ImageResult<Vec<u8>>
9where
10 P: image::Pixel + PixelWithColorType,
11 P: image::Pixel,
12 [P::Subpixel]: EncodableLayout,
13 Container: Deref<Target = [P::Subpixel]>,
14{
15 let mut bytes: Cursor<Vec<u8>> = Cursor::new(Vec::new());
16 image.write_to(&mut bytes, format)?;
17 Ok(bytes.into_inner())
18}