Newer
Older
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
use image::{GenericImage, Pixel, Rgba};
use num_traits::ToPrimitive;
use crate::commands::ColourMapping;
use crate::utils::{new_image, BasicRgba};
use crate::OutputFormat;
pub fn remap_image(
image: impl GenericImage,
mappings: ColourMapping,
) -> anyhow::Result<OutputFormat> {
let mut output = new_image(image.width(), image.height());
for (x, y, pixel) in image.pixels().into_iter() {
let pixel = pixel.to_rgba();
if pixel.0[3].to_u8().unwrap() == 0 {
output.put_pixel(x, y, Rgba::from(BasicRgba::transparent()));
continue;
}
let data = Rgba::from([
pixel.0[0].to_u8().unwrap(),
pixel.0[1].to_u8().unwrap(),
pixel.0[2].to_u8().unwrap(),
pixel.0[3].to_u8().unwrap(),
]);
let basic = BasicRgba::from(data);
match mappings.get(&basic) {
Some(mapped_data) => output.put_pixel(
x,
y,
Rgba::from(BasicRgba {
a: basic.a,
..mapped_data.clone()
}),
),
None => output.put_pixel(x, y, Rgba::from(BasicRgba::transparent())),
};
}
Ok(output)
}