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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
use crate::{PhotonImage, Rgb};
use image::{DynamicImage, ImageBuffer};
extern crate wasm_bindgen;
use image::DynamicImage::ImageRgba8;
pub fn square_distance(color1: Rgb, color2: Rgb) -> i32 {
let (r1, g1, b1) = (color1.r as i32, color1.g as i32, color1.b as i32);
let (r2, g2, b2) = (color2.r as i32, color2.g as i32, color2.b as i32);
i32::pow(r1 - r2, 2) + i32::pow(g1 - g2, 2) + i32::pow(b1 - b2, 2)
}
pub fn open_dyn_image(img_path: &'static str) -> DynamicImage {
image::open(img_path).unwrap()
}
pub fn save_dyn_image(img: DynamicImage, filtered_img_path: &str) {
img.save(filtered_img_path).unwrap();
}
pub fn get_pixels(img: DynamicImage) -> Vec<u8> {
img.to_bytes()
}
pub fn dyn_image_from_raw(photon_image: &PhotonImage) -> DynamicImage {
let _len_vec = photon_image.raw_pixels.len() as u128;
let raw_pixels = &photon_image.raw_pixels;
let img_buffer = ImageBuffer::from_vec(
photon_image.width,
photon_image.height,
raw_pixels.to_vec(),
)
.unwrap();
ImageRgba8(img_buffer)
}