1use crate::{PhotonImage, Rgb};
4use image::DynamicImage::ImageRgba8;
5use image::{DynamicImage, ImageBuffer};
6
7#[cfg(feature = "enable_wasm")]
8extern crate wasm_bindgen;
9
10pub fn square_distance(color1: Rgb, color2: Rgb) -> i32 {
12 let (r1, g1, b1) = (color1.r as i32, color1.g as i32, color1.b as i32);
13 let (r2, g2, b2) = (color2.r as i32, color2.g as i32, color2.b as i32);
14 i32::pow(r1 - r2, 2) + i32::pow(g1 - g2, 2) + i32::pow(b1 - b2, 2)
15}
16
17pub fn open_dyn_image(img_path: &'static str) -> DynamicImage {
19 image::open(img_path).unwrap()
20}
21
22pub fn save_dyn_image(img: DynamicImage, filtered_img_path: &str) {
24 img.save(filtered_img_path).unwrap();
32}
33
34pub fn get_pixels(img: DynamicImage) -> Vec<u8> {
36 img.into_bytes()
38}
39
40pub fn dyn_image_from_raw(photon_image: &PhotonImage) -> DynamicImage {
42 let _len_vec = photon_image.raw_pixels.len() as u128;
44 let raw_pixels = &photon_image.raw_pixels;
45 let img_buffer = ImageBuffer::from_vec(
46 photon_image.width,
47 photon_image.height,
48 raw_pixels.to_vec(),
49 )
50 .unwrap();
51 ImageRgba8(img_buffer)
52}