aboutsummaryrefslogtreecommitdiff
path: root/src/embedders.rs
blob: 94fc122e1ebe6018c9b40dc1a738727b5ff5eaa8 (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
use rayon::prelude::*;
use std::path::PathBuf;

pub trait MetricElem {
    fn dist(&self, _: &Self) -> f64;
}

impl MetricElem for f64 {
    fn dist(&self, b: &f64) -> f64 {
        (self - b).abs()
    }
}

pub trait EmbedderT {
    type Embedding: MetricElem;

    fn embed(&mut self, _: &[PathBuf]) -> Result<Vec<Self::Embedding>, String>;
}

pub struct BrightnessEmbedder;
impl EmbedderT for BrightnessEmbedder {
    type Embedding = f64;

    fn embed(&mut self, paths: &[PathBuf]) -> Result<Vec<f64>, String> {
        paths
            .par_iter()
            .map(|p| {
                let im = image::open(p).map_err(|e| e.to_string())?;
                let num_bytes = 3 * (im.height() * im.width());

                if num_bytes == 0 {
                    Err("Encountered NaN brightness, due to an empty image")?;
                }

                Ok(im.to_rgb8()
                   .iter()
                   .map(|e| *e as u64)
                   .sum::<u64>() as f64 / num_bytes as f64)
            })
            .collect::<Vec<_>>()
            .into_iter()
            .try_collect()
    }
}

#[repr(transparent)]
pub struct Hue (f64);
impl MetricElem for Hue {
    fn dist(&self, b: &Hue) -> f64 {
        let d = self.0.dist(&b.0);
        d.min(6.-d)
    }
}

pub struct HueEmbedder;
impl EmbedderT for HueEmbedder {
    type Embedding = Hue;

    fn embed(&mut self, paths: &[PathBuf]) -> Result<Vec<Hue>, String> {
        paths
            .par_iter()
            .map(|p| {
                let im = image::open(p).map_err(|e| e.to_string())?;
                let num_pixels = im.height() * im.width();
                let [sr, sg, sb] = im
                    .to_rgb8()
                    .pixels()
                    .fold([0, 0, 0], |[or, og, ob], n| {
                        let [nr, ng, nb] = n.0;
                        [or + nr as u64, og + ng as u64, ob + nb as u64]
                    })
                    .map(|e| e as f64 / 255. / num_pixels as f64);

                let hue =
                    if sr >= sg && sr >= sb {
                        (sg - sb) / (sr - sg.min(sb))
                    }
                    else if sg >= sb {
                        2. + (sb - sr) / (sg - sr.min(sb))
                    }
                    else {
                        4. + (sr - sg) / (sb - sr.min(sg))
                    };

                if hue.is_nan() {
                    Err("Encountered NaN hue, possibly because of a colorless or empty image")?;
                }

                Ok(Hue(hue))
            })
            .collect::<Vec<_>>()
            .into_iter()
            .try_collect()
    }
}

impl MetricElem for (f64, f64, f64) {
    fn dist(&self, o: &(f64, f64, f64)) -> f64 {
        let (dr, dg, db) =
            ((self.0 - o.0), (self.1 - o.1), (self.2 - o.2));
        (dr*dr + dg*dg + db*db).sqrt()
    }
}

pub struct ColorEmbedder;
impl EmbedderT for ColorEmbedder {
    type Embedding = (f64, f64, f64);

    fn embed(&mut self, paths: &[PathBuf]) -> Result<Vec<(f64, f64, f64)>, String> {
        paths
            .par_iter()
            .map(|p| {
                let im = image::open(p).map_err(|e| e.to_string())?;
                let num_pixels = im.height() * im.width();
                let [sr, sg, sb] = im
                    .to_rgb8()
                    .pixels()
                    .fold([0, 0, 0], |[or, og, ob], n| {
                        let [nr, ng, nb] = n.0;
                        [or + nr as u64, og + ng as u64, ob + nb as u64]
                    })
                    .map(|e| e as f64 / num_pixels as f64);

                Ok((sr, sg, sb))
            })
            .collect::<Vec<_>>()
            .into_iter()
            .try_collect()
    }
}