-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathlib.rs
154 lines (134 loc) · 4.29 KB
/
lib.rs
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
extern crate clap;
extern crate image;
use std::path::Path;
use clap::ArgMatches;
use image::{DynamicImage, GenericImage, GenericImageView};
#[derive(Debug)]
pub struct Config<'a> {
pub image1: &'a str,
pub image2: &'a str,
pub filename: Option<&'a str>,
}
impl<'a> Config<'a> {
pub fn from_clap_matches(matches: &'a ArgMatches) -> Config<'a> {
// unwrap() should be safe here because clap does argument validation
let image1 = matches.value_of("image1").unwrap();
let image2 = matches.value_of("image2").unwrap();
let filename = matches.value_of("filename");
Config {
image1,
image2,
filename,
}
}
}
/// abs(x - y) for u8
fn abs_diff(x: u8, y: u8) -> u8 {
let large: u8;
let small: u8;
if x > y {
large = x;
small = y;
} else {
large = y;
small = x;
}
return large - small;
}
#[test]
fn test_abs_diff() {
assert_eq!(abs_diff(5, 8), 3);
assert_eq!(abs_diff(8, 5), 3);
assert_eq!(abs_diff(11, 11), 0);
assert_eq!(abs_diff(0, 255), 255);
}
/// Return the image from the file path, or throw an error
fn safe_load_image(raw_path: &str) -> Result<DynamicImage, String> {
let path = &Path::new(raw_path);
if !Path::exists(path) {
return Err(format!("Path \"{}\" does not exist", raw_path));
}
match image::open(path) {
Ok(image) => Ok(image),
Err(msg) => Err(format!("{:?}", msg)),
}
}
/// Check if two images are the same size and color mode
fn validate_image_compatibility(
image1: &DynamicImage,
image2: &DynamicImage,
) -> Result<(), String> {
if image1.dimensions() != image2.dimensions() {
return Err("images must have the same dimensions".to_string());
}
if image1.color() != image2.color() {
return Err("images must have the same color mode".to_string());
}
Ok(())
}
/// Return a difference ratio between 0 and 1 for the two images
pub fn calculate_diff(image1: DynamicImage, image2: DynamicImage) -> Result<f64, String> {
// All color types wrap an 8-bit value for each channel
let max_val = u32::pow(2, 8) - 1;
// u32 can handle up to 2^16 x 2^16 pixel images
let mut diffsum: u32 = 0;
for (&p1, &p2) in image1.raw_pixels().iter().zip(image2.raw_pixels().iter()) {
diffsum += u32::from(abs_diff(p1, p2));
}
let total_possible = max_val * image1.raw_pixels().len() as u32;
let ratio = diffsum as f64 / total_possible as f64;
Ok(ratio)
}
/// Create an image that is the difference of the two images given, and write to the given filename
pub fn create_diff_image(
image1: DynamicImage,
image2: DynamicImage,
filename: &str,
) -> Result<(), String> {
let w = image1.width();
let h = image1.height();
let mut diff = match image1.color() {
image::ColorType::RGB(_) => image::DynamicImage::new_rgb8(w, h),
image::ColorType::RGBA(_) => image::DynamicImage::new_rgba8(w, h),
_ => return Err(format!("color mode {:?} not yet supported", image1.color())),
};
for x in 0..w {
for y in 0..h {
let mut rgba = [0; 4];
for c in 0..4 {
rgba[c] = abs_diff(
image1.get_pixel(x, y).data[c],
image2.get_pixel(x, y).data[c],
);
}
let new_pix = image::Pixel::from_slice(&rgba);
diff.put_pixel(x, y, *new_pix);
}
}
if let Err(msg) = diff.save(filename) {
return Err(msg.to_string());
}
Ok(())
}
/// Run the appropriate diffing process given the configuration settings
pub fn run(config: Config) -> Result<(), String> {
let image1 = safe_load_image(&config.image1)?;
let image2 = safe_load_image(&config.image2)?;
validate_image_compatibility(&image1, &image2)?;
match config.filename {
Some(filename) => match create_diff_image(image1, image2, filename) {
Ok(_) => {
println!("Wrote diff image to {}", filename);
Ok(())
}
Err(msg) => Err(msg),
},
None => match calculate_diff(image1, image2) {
Ok(ratio) => {
println!("{}", ratio);
Ok(())
}
Err(msg) => Err(msg),
},
}
}