about summary refs log tree commit diff
path: root/src/main.rs
blob: b72e912f1fe26bbc3be78e24c79a5e503898c110 (plain)
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
use std::{collections::HashMap, env::args, time::Instant};

use image::io::Reader as ImageReader;
use image::{buffer::Pixels, Rgb};

const MAX_COLORS: usize = 256;

const TOLERANCE: f32 = 0.6;
const RGB_TOLERANCE: f32 = 10.0 * TOLERANCE;

fn main() {
    let before = Instant::now();
    let filename = args().nth(1).unwrap();
    let outname = args().nth(2).unwrap();
    // The percent of RGB value difference a color has to surpass to be considered unique

    println!("File is {}", &filename);

    let imageread = ImageReader::open(&filename).expect("Failed to open image!");
    let mut image = imageread
        .decode()
        .expect("Failed to decode image!")
        .into_rgb8();

    println!("Decoded!");
    let before_algo = Instant::now();

    let selected_colors = quantize(image.pixels());

    println!("Selected {} colors! Creating map...", selected_colors.len());

    let mut color_map: HashMap<Rgb<u8>, Rgb<u8>> = HashMap::with_capacity(image.len() / 2);
    // Selected colors are themselves
    for color in selected_colors.iter() {
        color_map.insert(*color, *color);
    }

    // Max complexity is O(n * max_colors)
    for color in image.pixels() {
        let mut min_difference = f32::MAX;
        let mut min_difference_color = *color;

        for index in 0..selected_colors.len() {
            let difference = rgb_difference(color, unsafe { selected_colors.get_unchecked(index) });
            /*if difference == 0.0 {
                continue 'sorted_colors;
            }*/
            if difference < min_difference {
                min_difference = difference;
                min_difference_color = unsafe { *selected_colors.get_unchecked(index) };
            }
        }

        color_map.insert(*color, min_difference_color);
    }

    println!("Mapped! Filling in image...");

    for pixel in image.pixels_mut() {
        pixel.clone_from(color_map.get(pixel).unwrap());
    }

    println!(
        "Filled! Took {}ms. Recounting colors...",
        Instant::now().duration_since(before_algo).as_millis()
    );

    let mut recounted_colors = Vec::with_capacity(MAX_COLORS);
    // Recount colors
    for pixel in image.pixels() {
        if !recounted_colors.contains(pixel) {
            println!("Found unique color! Now {}", recounted_colors.len());
            recounted_colors.push(*pixel);
        }
    }

    println!(
        "Aiming for a max of {} colors, got {}",
        MAX_COLORS,
        recounted_colors.len()
    );

    image.save(outname).expect("Failed to write out");
    println!(
        "Took {}ms",
        Instant::now().duration_since(before).as_millis()
    );
}

fn quantize(pixels: Pixels<Rgb<u8>>) -> Vec<Rgb<u8>> {
    let mut colors: HashMap<Rgb<u8>, usize> = HashMap::new();

    //count pixels
    for pixel in pixels {
        match colors.get_mut(pixel) {
            None => {
                colors.insert(*pixel, 1);
            }
            Some(n) => *n += 1,
        }
    }

    let mut sorted: Vec<(Rgb<u8>, usize)> = colors.into_iter().collect();
    sorted.sort_by(|(colour1, freq1), (colour2, freq2)| {
        freq2
            .cmp(freq1)
            .then(colour2[0].cmp(&colour1[0]))
            .then(colour2[1].cmp(&colour1[1]))
            .then(colour2[2].cmp(&colour1[2]))
    });

    println!("Sorted! Selecting colors...");

    let mut sorted_iter = sorted.iter();

    let mut selected_colors: Vec<Rgb<u8>> = Vec::with_capacity(MAX_COLORS);
    selected_colors.push(sorted_iter.next().unwrap().0);

    for (key, _value) in sorted_iter {
        if selected_colors.len() < MAX_COLORS {
            for selected_color in selected_colors.iter() {
                if rgb_difference(key, selected_color) > RGB_TOLERANCE {
                    selected_colors.push(*key);
                    break;
                }
            }
        } else {
            break;
        }
    }

    selected_colors
}

fn rgb_difference(a: &Rgb<u8>, z: &Rgb<u8>) -> f32 {
    //((a.0[0] as i16 - b.0[0] as i16).abs() + (a.0[1] as i16 - b.0[1] as i16).abs() +(a.0[2] as i16 - b.0[2] as i16).abs()) as u16
    //(a.0[0] as i16 - b.0[0] as i16).abs().max((a.0[1] as i16 - b.0[1] as i16).abs().max(a.0[2] as i16 - b.0[2] as i16).abs()) as u16
    //(a.0[0] as i16 - b.0[0] as i16).abs().max((a.0[1] as i16 - b.0[1] as i16).abs()).max((a.0[2] as i16 - b.0[2] as i16).abs()) as u16
    /*(((a.0[0] as i32 - b.0[0] as i32) * (a.0[0] as i32 - b.0[0] as i32))
    + ((a.0[1] as i32 - b.0[1] as i32) * (a.0[1] as i32 - b.0[1] as i32))
    + ((a.0[2] as i32 - b.0[2] as i32) * (a.0[2] as i32 - b.0[2] as i32)))
    .abs() as u16*/
    let (a, b, c) = pixel_rgb_to_hsv(a);
    let (d, e, f) = pixel_rgb_to_hsv(z);

    (((c - f) * (c - f)) + ((a - d).abs() / 90.0) + (b - e).abs()) as f32
}

#[warn(clippy::float_cmp)]
fn pixel_rgb_to_hsv(a: &Rgb<u8>) -> (f32, f32, f32) {
    let (r, g, b) = (
        a.0[0] as f32 / 256.0,
        a.0[1] as f32 / 256.0,
        a.0[2] as f32 / 256.0,
    );

    let value = r.max(g.max(b));
    let x_min = r.min(g.min(b));
    let chroma = value - x_min;

    let hue = if chroma == 0.0 {
        0.0
    } else if value == r {
        60.0 * ((g - b) / chroma)
    } else if value == g {
        60.0 * (2.0 + (b - r) / chroma)
    } else if value == b {
        60.0 * (4.0 + (r - g) / chroma)
    } else {
        unreachable!()
    };

    let value_saturation = if value == 0.0 { 0.0 } else { chroma / value };

    /* Rotate the color wheel counter clockwise to the negative location
          |       Keep the wheel in place and remove any full rotations
     _____V____ _____V____
    |          |          |*/
    ((hue + 360.0) % 360.0, value_saturation * 2.0, value * 2.0)
}