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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
use crate::Error;
use std::path::Path;
#[derive(Clone)]
pub enum ImageSource<'a> {
Memory(&'a [u8]),
Path(&'a Path),
Image(image::DynamicImage),
}
impl<'a, S> From<&'a S> for ImageSource<'a>
where
S: AsRef<Path> + 'a,
{
fn from(path: &'a S) -> Self {
Self::Path(path.as_ref())
}
}
pub(crate) fn load_image(
src: ImageSource<'_>,
resize: Option<(u32, u32)>,
) -> Result<image::RgbaImage, Error> {
let img = match src {
ImageSource::Memory(data) => image::load_from_memory(data),
ImageSource::Path(path) => image::open(path),
ImageSource::Image(img) => Ok(img),
}?;
Ok(match resize {
None => img.to_rgba(),
Some(ref size) => {
use image::GenericImageView;
if img.width() != size.0 || img.height() != size.1 {
image::imageops::resize(&img.to_rgba(), size.0, size.1, image::imageops::CatmullRom)
} else {
img.to_rgba()
}
}
})
}
pub(crate) fn transform_to_guide_map(
image: image::RgbaImage,
size: Option<(u32, u32)>,
blur_sigma: f32,
) -> image::RgbaImage {
use image::GenericImageView;
let dyn_img = image::DynamicImage::ImageRgba8(image);
if let Some(s) = size {
if dyn_img.width() != s.0 || dyn_img.height() != s.1 {
dyn_img.resize(s.0, s.1, image::imageops::Triangle);
}
}
dyn_img.blur(blur_sigma).grayscale().to_rgba()
}
pub(crate) fn get_histogram(img: &image::RgbaImage) -> Vec<u32> {
let mut hist = vec![0; 256];
let pixels = &img;
for (i, pixel_value) in pixels.iter().enumerate() {
if i % 4 == 0 {
hist[*pixel_value as usize] += 1;
}
}
hist
}
pub(crate) fn match_histograms(source: &mut image::RgbaImage, target: &image::RgbaImage) {
let target_hist = get_histogram(target);
let source_hist = get_histogram(source);
let target_cdf = get_cdf(&target_hist);
let source_cdf = get_cdf(&source_hist);
let (dx, dy) = source.dimensions();
for x in 0..dx {
for y in 0..dy {
let pixel_value = source.get_pixel(x, y)[0];
let pixel_source_cdf = source_cdf[pixel_value as usize];
let new_pixel_val = target_cdf
.iter()
.position(|cdf| *cdf > pixel_source_cdf)
.unwrap_or((pixel_value + 1) as usize) as u8
- 1;
let new_color: image::Rgba<u8> =
image::Rgba([new_pixel_val, new_pixel_val, new_pixel_val, 255]);
source.put_pixel(x, y, new_color);
}
}
}
pub(crate) fn get_cdf(a: &[u32]) -> Vec<f32> {
let mut cumm = vec![0.0; 256];
for i in 0..a.len() {
if i != 0 {
cumm[i] = cumm[i - 1] + (a[i] as f32);
} else {
cumm[i] = a[i] as f32;
}
}
let max = cumm[255];
for i in cumm.iter_mut() {
*i /= max;
}
cumm
}