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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
pub mod color;
pub mod weights;

use rand::{distributions::WeightedIndex, prelude::*};
use color::LAB;
use weights::WeightFn;

#[cfg(target_arch = "wasm32")]
use {
    wasm_bindgen::{prelude::*, JsCast},
    web_sys::{CanvasRenderingContext2d, HtmlCanvasElement},
    weights::{Mood, resolve_mood},
    color::{RGB, HSL},
    serde_derive::Serialize,
};

#[cfg(not(target_arch = "wasm32"))]
use {
    std::cmp,
    crossbeam_utils::thread,
};

pub type Pixels = Vec<LAB>;

/// Recalculates the means using a weight function
fn recal_means(colors: &Vec<LAB>, weight: WeightFn) -> LAB {
    let mut new_color = LAB {
        l: 0.0,
        a: 0.0,
        b: 0.0
    };
    let mut w_sum = 0.0;

    for col in colors.iter() {
        let w = weight(col);
        w_sum += w;
        new_color.l += w * col.l;
        new_color.a += w * col.a;
        new_color.b += w * col.b;
    }

    new_color.l /= w_sum;
    new_color.a /= w_sum;
    new_color.b /= w_sum;

    return new_color;
}

#[cfg(target_arch = "wasm32")]
fn find_clusters(pixels: &Pixels, means: &Pixels, k: usize) -> Vec<Pixels> {
    let mut clusters: Vec<Pixels> = vec![Vec::new(); k];

    for color in pixels.iter() {
        clusters[color.nearest(means).0].push(color.clone());
    }

    return clusters;
}

#[cfg(not(target_arch = "wasm32"))]
fn find_clusters(pixels: &Pixels, means: &Pixels, k: usize) -> Vec<Pixels> {
    const NUM_THREADS: usize = 5;

    let num_pixels = pixels.len();
    let sample_size = num_pixels / NUM_THREADS;
    let mut clusters: Vec<Pixels> = vec![Vec::new(); k as usize];
    
    thread::scope(|s| {
        let mut threads = vec![];
        
        // Data parallelism where each thread operates on a sample of data
        for i in 0..NUM_THREADS {
            let start = i * sample_size;
            let end = cmp::min(start + sample_size, num_pixels);

            // Each thread is responsible in finding the nearest cluster mean for each point in the sample (map phase)
            threads.push(
                s.spawn(move |_| {
                    let mut clusters: Vec<Vec<LAB>> = vec![Vec::new(); k];
                    for pixel_idx in start..end {
                        let color = &pixels[pixel_idx];
                        clusters[color.nearest(&means).0].push(color.clone());
                    }
                    return clusters;
                })
            );
        }

        // Results from each thread is combined (or reduced)
        for t in threads {
            let mut mid_clusters = t.join().unwrap();
            for (i, cluster) in mid_clusters.iter_mut().enumerate() {
                clusters[i].append(cluster);
            }
        }

    }).unwrap();

    return clusters;
}

/// Parallelized K-means++ clustering to create the palette from pixels
pub fn pigments_pixels(pixels: &Pixels, k: u8, weight: WeightFn, max_iter: Option<u16>) -> Vec<(LAB, f32)> {
    // Values referenced from https://scikit-learn.org/stable/modules/generated/sklearn.cluster.KMeans.html
    const TOLERANCE: f32 = 1e-4;
    const MAX_ITER: u16 = 300;

    let mut rng = rand::thread_rng();
    let k = k as usize;

    // Randomly pick the starting cluster center
    let i: usize = rng.gen_range(0, pixels.len());
    let mut means: Pixels = vec![pixels[i].clone()];

    // Pick the remaining (k-1) means
    for _ in 0..(k - 1) {
        // Calculate the (nearest_distance)^2 for every color in the image
        let distances: Vec<f32> = pixels
            .iter()
            .map(|color| (color.nearest(&means).1).powi(2))
            .collect();

        // Create a weighted distribution based on distance^2
        // If error occurs, return the means already found
        let dist = match WeightedIndex::new(&distances) {
            Ok(t) => t,
            Err(_) => {
                // Calculate the dominance of each color
                let mut palette: Vec<(LAB, f32)> = means.iter().map(|c| (c.clone(), 0.0)).collect();

                let len = pixels.len() as f32;
                for color in pixels.iter() {
                    let near = color.nearest(&means).0;
                    palette[near].1 += 1.0 / len;
                }

                return palette;
            }
        };

        // Using the distances^2 as weights, pick a color and use it as a cluster center
        means.push(pixels[dist.sample(&mut rng)].clone());
    }

    let mut clusters: Vec<Vec<LAB>>;
    let mut iters_left = max_iter.unwrap_or(MAX_ITER);

    loop {
        // Assignment step: Clusters are formed in current iteration
        clusters = find_clusters(pixels, &means, k);

        // Updation step: New cluster means are calculated
        let mut changed: bool = false;
        for i in 0..clusters.len() {
            let new_mean = recal_means(&clusters[i], weight);
            if means[i].distance(&new_mean) > TOLERANCE {
                changed = true;
            }

            means[i] = new_mean;
        }

        iters_left -= 1;

        if !changed || iters_left <= 0 {
            break;
        }
    }

    // The length of every cluster divided by total pixels gives the dominance of each mean
    // For every mean, the corresponding dominance is added as a tuple item
    return clusters
        .iter()
        .enumerate()
        .map(|(i, cluster)| {
            (
                means[i].clone(),
                cluster.len() as f32 / pixels.len() as f32,
            )
        })
        .collect();
}


#[cfg(target_arch = "wasm32")]
#[wasm_bindgen]
pub fn pigments(canvas: HtmlCanvasElement, k: u8, mood: Mood, batch_size: Option<u32>) -> JsValue {

    #[derive(Serialize)]
    struct PaletteColor {
        pub dominance: f32,
        pub hex: String,
        pub rgb: RGB,
        pub hsl: HSL,
    }

    // Get context from canvas element
    let ctx = canvas
        .get_context("2d")
        .unwrap()
        .unwrap()
        .dyn_into::<CanvasRenderingContext2d>()
        .unwrap();

    // Image data gathered from the canvas
    let data = ctx
        .get_image_data(0.0, 0.0, canvas.width() as f64, canvas.height() as f64)
        .unwrap()
        .data();

    // Convert to Pixels type
    let mut pixels: Pixels = (0..data.len())
        .step_by(4)
        .map(|i| LAB::from_rgb(data[i], data[i+1], data[i+2]))
        .collect();

    // Randomly choose a sample of batch size if given
    let batch = batch_size.unwrap_or(0);
    if batch != 0 && batch < canvas.width() * canvas.height() && batch > k.into() {
        let mut rng = rand::thread_rng();
        pixels = pixels
            .choose_multiple(&mut rng, batch as usize)
            .cloned()
            .collect();
    }
    
    // Generate the color palette and store it in a Vector of PaletteColor
    let weight: WeightFn = resolve_mood(&mood);
    let palettes: Vec<PaletteColor> = pigments_pixels(&pixels, k, weight, None)
        .iter()
        .map(|(color, dominance)| {
            let rgb = RGB::from(color);
            PaletteColor {
                dominance: *dominance,
                hex: rgb.hex(),
                rgb: rgb,
                hsl: HSL::from(color),
            }
        })
        .collect();

    // Convert to a JS value
    return JsValue::from_serde(&palettes).unwrap();
}