-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.rs
41 lines (37 loc) · 1.17 KB
/
main.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
use image::{io::Reader as ImageReader, GenericImage, GenericImageView, Rgba};
use rand::random;
pub trait Ditherable {
fn squish_to_web_safe(&mut self) -> Self;
fn apply_noise(&mut self, multiplier: f64) -> Self;
}
impl Ditherable for Rgba<u8> {
fn squish_to_web_safe(&mut self) -> Self {
for idx in 0..3 {
self.0[idx] = (self.0[idx] / 0x33) * 0x33;
}
*self
}
fn apply_noise(&mut self, multiplier: f64) -> Self {
for idx in 0..3 {
let mut noise: f64 = random();
noise = noise - 0.5;
noise = noise * multiplier;
noise = 1f64 + noise;
self.0[idx] = ((self.0[idx] as f64) * noise) as u8;
}
*self
}
}
fn main() -> Result<(), Box<dyn std::error::Error>> {
let mut image = ImageReader::open("input.png")?.decode()?;
image = image.resize(80, 80, image::imageops::FilterType::Gaussian);
let (w, h) = image.dimensions();
for y in 0..h {
for x in 0..w {
let mut val = image.get_pixel(x, y);
image.put_pixel(x, y, val.apply_noise(0.5).squish_to_web_safe())
}
}
image.save("output.png")?;
Ok(())
}