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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
//! Monochrome-related effects and greyscaling/duotoning.

extern crate image;
use image::{GenericImage, GenericImageView};
use crate::{PhotonImage};
use crate::helpers;
use wasm_bindgen::prelude::*;

/// Apply a monochrome effect of a certain colour.
/// 
/// It does so by averaging the R, G, and B values of a pixel, and then adding a 
/// separate value to that averaged value for each channel to produce a tint.
/// # Arguments
/// * `photon_image` - A PhotonImage.
/// * `r_offset` - The value to add to the Red channel per pixel.
/// * `g_offset` - The value to add to the Green channel per pixel.
/// * `b_offset` - The value to add to the Blue channel per pixel.
///
/// # Example
///
/// ```
/// // For example, to apply a monochrome effect to an image:
/// use photon::monochrome;
/// monochrome::monochroma(&mut img, 40, 50, 100);
/// ```
/// 
#[wasm_bindgen]
pub fn monochrome(mut photon_image: &mut PhotonImage, r_offset: u32, g_offset: u32, b_offset: u32) {
    let mut img = helpers::dyn_image_from_raw(&photon_image);
    let (width, height) = img.dimensions();

    for x in 0..width {
        for y in 0..height {
            let px = img.get_pixel(x, y);
            let (r_val, g_val, b_val) = (px.data[0] as u32, px.data[1] as u32, px.data[2] as u32);
            let mut avg = (r_val + g_val + b_val) / 3;
            if avg >= 255 {
                avg = 255
            }
            
            let new_r = if avg as u32 + r_offset < 255 { avg as u8 + r_offset as u8} else { 255 };
            let new_g = if avg as u32 + g_offset < 255 { avg as u8 + g_offset as u8} else { 255 };
            let new_b = if avg as u32 + b_offset < 255 { avg as u8 + b_offset as u8} else { 255 };

            img.put_pixel(x, y, image::Rgba([new_r, new_g, new_b, 255]));
        }
    }
    let raw_pixels = img.raw_pixels();
    photon_image.raw_pixels = raw_pixels;
}

/// Convert an image to sepia.
/// 
/// # Arguments
/// * `photon_image` - A PhotonImage.
/// # Example
///
/// ```
/// // For example, to tint an image of type `PhotonImage`:
/// use photon::monochrome;
/// monochrome::sepia(&mut img);
/// ```
/// 
#[wasm_bindgen]
pub fn sepia(mut photon_image: &mut PhotonImage) {
    let mut img = helpers::dyn_image_from_raw(&photon_image);
    let (width, height) = img.dimensions();

    for x in 0..width {
        for y in 0..height {
            let px = img.get_pixel(x, y);
            let (r_val, g_val, b_val) = (px.data[0] as f32, px.data[1] as f32, px.data[2] as f32);
            let avg = 0.3 * r_val + 0.59 * g_val + 0.11 * b_val;

            let new_r = if avg as u32 + 100 < 255 { avg as u8 + 100} else { 255 };
            let new_g = if avg as u32 + 50 < 255 { avg as u8 + 50 } else { 255 };
      
            img.put_pixel(x, y, image::Rgba([new_r, new_g, b_val as u8, 255]));
        }
    }
    let raw_pixels = img.raw_pixels();
    photon_image.raw_pixels = raw_pixels;
}

/// Convert an image to grayscale using the conventional averaging algorithm.
/// 
/// # Arguments
/// * `photon_image` - A PhotonImage.

/// # Example
///
/// ```
/// // For example, to convert an image of type `PhotonImage` to greyscale:
/// use photon::monochrome;
/// monochrome::grayscale(&mut img);
/// ```
#[wasm_bindgen]
pub fn grayscale(mut photon_image: &mut PhotonImage) {
    let mut img = helpers::dyn_image_from_raw(&photon_image);    
    let (width, height) = img.dimensions();

    for x in 0..width {
        for y in 0..height {
            let px = img.get_pixel(x, y);
            let (r_val, g_val, b_val) = (px.data[0] as u32, px.data[1] as u32, px.data[2] as u32);
            let mut avg = ((r_val + g_val + b_val) / 3) as u8;
            if avg >= 255 {
                avg = 255
            }

            img.put_pixel(x, y, image::Rgba([avg, avg, avg, 255]));
        }
    }
    let raw_pixels = img.raw_pixels();
    photon_image.raw_pixels = raw_pixels;
}

/// Convert an image to grayscale with a human corrected factor, to account for human vision.
/// 
/// # Arguments
/// * `photon_image` - A PhotonImage.

/// # Example
///
/// ```
/// // For example, to convert an image of type `PhotonImage` to greyscale with a human corrected factor:
/// use photon::monochrome;
/// monochrome::grayscale_human_corrected(&mut img);
/// ```
#[wasm_bindgen]
pub fn grayscale_human_corrected(mut photon_image: &mut PhotonImage) {
    let mut img = helpers::dyn_image_from_raw(&photon_image);
    let (width, height) = img.dimensions();

    for x in 0..width {
        for y in 0..height {
            let px = img.get_pixel(x, y);
            let (r_val, g_val, b_val) = (px.data[0] as f32, px.data[1] as f32, px.data[2] as f32);

            let mut avg: u8 = (r_val * 0.3 + g_val * 0.59 + b_val * 0.11) as u8;
            
            if avg >= 255 {
                avg = 255
            }
            

            img.put_pixel(x, y, image::Rgba([avg, avg, avg, 255]));
        }
    }
    let raw_pixels = img.raw_pixels();
    photon_image.raw_pixels = raw_pixels;
}

/// Desaturate an image by getting the min/max of each pixel's RGB values.
/// 
/// # Arguments
/// * `photon_image` - A PhotonImage.

/// # Example
///
/// ```
/// // For example, to desaturate an image:
/// use photon::monochrome;
/// monochrome::desaturate(&mut img);
/// ```
#[wasm_bindgen]
pub fn desaturate(mut photon_image: &mut PhotonImage) {
    let mut img = helpers::dyn_image_from_raw(&photon_image);
    let (width, height) = img.dimensions();

    for x in 0..width {
        for y in 0..height {
            let px = img.get_pixel(x, y);
            let (r_val, g_val, b_val) = (px.data[0] as u32, px.data[1] as u32, px.data[2] as u32);
            
            // get the max and min vals of a pixel's 3 rgb values by sorting a vec of these
            let mut rgb_vals = vec![r_val, g_val, b_val];
            rgb_vals.sort();

            let mut gray = ((rgb_vals[0] + rgb_vals[2]) / 2) as u8;

            if gray >= 255 {
                gray = 255
            }
            
            img.put_pixel(x, y, image::Rgba([gray, gray, gray, 255]));
        }
    }
    let raw_pixels = img.raw_pixels();
    photon_image.raw_pixels = raw_pixels;
}

/// Uses a min. decomposition algorithm to convert an image to greyscale.
/// 
/// # Arguments
/// * `photon_image` - A PhotonImage.

/// # Example
///
/// ```
/// // For example, to decompose an image with min decomposition:
/// monochrome::decompose_min(&mut img);
/// ```
#[wasm_bindgen]
pub fn decompose_min(mut photon_image: &mut PhotonImage) {
    let mut img = helpers::dyn_image_from_raw(&photon_image);
    let (width, height) = img.dimensions();

    for x in 0..width {
        for y in 0..height {
            let px = img.get_pixel(x, y);
            let (r_val, g_val, b_val) = (px.data[0] as u32, px.data[1] as u32, px.data[2] as u32);
            
            // get the max and min vals of a pixel's 3 rgb values by sorting a vec of these
            let mut rgb_vals = vec![r_val, g_val, b_val];
            rgb_vals.sort();

            let mut gray = rgb_vals[0] as u8;

            if gray >= 255 {
                gray = 255
            }
            
            img.put_pixel(x, y, image::Rgba([gray, gray, gray, 255]));
        }
    }
    let raw_pixels = img.raw_pixels();
    photon_image.raw_pixels = raw_pixels;
}

/// Uses a max. decomposition algorithm to convert an image to greyscale.
/// 
/// # Arguments
/// * `photon_image` - A PhotonImage.

/// # Example
///
/// ```
/// // For example, to decompose an image with max decomposition:
/// monochrome::decompose_max(&mut img);
/// ```
#[wasm_bindgen]
pub fn decompose_max(mut photon_image: &mut PhotonImage) {
    let mut img = helpers::dyn_image_from_raw(&photon_image);
    let (width, height) = img.dimensions();

    for x in 0..width {
        for y in 0..height {
            let px = img.get_pixel(x, y);
            let (r_val, g_val, b_val) = (px.data[0] as u32, px.data[1] as u32, px.data[2] as u32);
            
            // get the max and min vals of a pixel's 3 rgb values by sorting a vec of these
            let mut rgb_vals = vec![r_val, g_val, b_val];
            rgb_vals.sort();

            let gray = rgb_vals[2] as u8;
            
            img.put_pixel(x, y, image::Rgba([gray, gray, gray, 255]));
        }
    }
    let raw_pixels = img.raw_pixels();
    photon_image.raw_pixels = raw_pixels;
}

/// Employ only a limited number of gray shades in an image.
/// 
/// # Arguments
/// * `photon_image` - A PhotonImage.
/// * `num_shades` - The number of grayscale shades to be displayed in the image.

/// # Example
///
/// ```
/// // For example, to limit an image to four shades of gray only:
/// monochrome::grayscale_shades(&mut img, 4);
/// ```
#[wasm_bindgen]
pub fn grayscale_shades(mut photon_image: &mut PhotonImage, num_shades: u8) {
    let mut img = helpers::dyn_image_from_raw(&photon_image);
    let (width, height) = img.dimensions();

    for x in 0..width {
        for y in 0..height {
            let px = img.get_pixel(x, y);

            let conversion: f32 = 255.0 / (num_shades as f32 - 1.0);
            let (r_val, g_val, b_val) = (px.data[0] as u32, px.data[1] as u32, px.data[2] as u32);
            
            let avg: f32 = (r_val + g_val + b_val) as f32 / 3.0;
            
            let dividend = avg / conversion as f32;

            let gray = ((dividend + 0.5) * conversion) as u8;
            
            img.put_pixel(x, y, image::Rgba([gray, gray, gray, 255]));
        }
    }
    let raw_pixels = img.raw_pixels();
    photon_image.raw_pixels = raw_pixels;
}

/// Convert an image to grayscale by setting a pixel's 3 RGB values to the Red channel's value.
/// 
/// # Arguments
/// * `photon_image` - A PhotonImage.

/// # Example
///
/// ```
/// monochrome::r_grayscale(&mut img);
/// ```
#[wasm_bindgen]
pub fn r_grayscale(photon_image: &mut PhotonImage) {
    return single_channel_grayscale(photon_image, 0)
}

/// Convert an image to grayscale by setting a pixel's 3 RGB values to the Green channel's value.
/// 
/// # Arguments
/// * `photon_image` - A PhotonImage.

/// # Example
///
/// ```
/// monochrome::g_grayscale(&mut img);
/// ```
#[wasm_bindgen]
pub fn g_grayscale(photon_image: &mut PhotonImage) {
    return single_channel_grayscale(photon_image, 1)
}

/// Convert an image to grayscale by setting a pixel's 3 RGB values to the Blue channel's value.
/// 
/// # Arguments
/// * `photon_image` - A PhotonImage.

/// # Example
///
/// ```
/// monochrome::b_grayscale(&mut img);
/// ```
#[wasm_bindgen]
pub fn b_grayscale(photon_image: &mut PhotonImage) {
    return single_channel_grayscale(photon_image, 2)
}

/// Convert an image to grayscale by setting a pixel's 3 RGB values to a chosen channel's value.
/// 
/// # Arguments
/// * `photon_image` - A PhotonImage.
/// * `channel` - A usize representing the channel from 0 to 2. O represents the Red channel, 1 the Green channel, and 2 the Blue channel. 

/// # Example
/// To grayscale using only values from the Red channel:
/// ```
/// monochrome::single_channel_grayscale(&mut img, 0);
/// ```
#[wasm_bindgen]
pub fn single_channel_grayscale(mut photon_image: &mut PhotonImage, channel: usize) {
    let mut img = helpers::dyn_image_from_raw(&photon_image);
    let (width, height) = img.dimensions();

    for x in 0..width {
        for y in 0..height {
            let mut px = img.get_pixel(x, y);

            let channel_data = px.data[channel];
            
            px.data[0] = channel_data as u8;
            px.data[1] = channel_data as u8;
            px.data[2] = channel_data as u8;
            img.put_pixel(x, y, px);
        }
    }
    let raw_pixels = img.raw_pixels();
    photon_image.raw_pixels = raw_pixels;
}

/// Threshold an image using a standard thresholding algorithm.
/// 
/// # Arguments
/// * `photon_image` - A PhotonImage.
/// * `threshold` - The amount the image should be thresholded by from 0 to 255.
/// # Example
///
/// ```
/// // For example, to threshold an image of type `PhotonImage`:
/// use photon::monochrome;
/// monochrome::threshold(&mut img, 30);
/// ```
#[wasm_bindgen]
pub fn threshold(mut photon_image: &mut PhotonImage, threshold: u32) {
    let mut img = helpers::dyn_image_from_raw(&photon_image);
    let (width, height) = img.dimensions();

    for x in 0..width {
        for y in 0..height {
            let mut px = img.get_pixel(x, y);

            let r: f32 = px.data[0].into();
            let g: f32 = px.data[1].into();
            let b: f32 = px.data[2].into();

            let mut v = 0.2126 * r + 0.7152 * g + 0.072 * b;

            if v >= threshold as f32 {
                v = 255.0;
            }
            else {
                v = 0.0;
            }
            px.data[0] = v as u8;
            px.data[1] = v as u8;
            px.data[2] = v as u8;

            img.put_pixel(x, y, px);
        }
    }
    let raw_pixels = img.raw_pixels();
    photon_image.raw_pixels = raw_pixels;
}