rustagram2 2.1.0

Apply instagram filters to your photos.
Documentation
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
422
423
424
425
426
use image::{GenericImage, ImageBuffer, Pixel, Rgba};

mod blend;

pub fn brighten_by_percent<I, P>(image: &I, value: f32) -> ImageBuffer<P, Vec<u8>>
where
    I: GenericImage<Pixel = P>,
    P: Pixel<Subpixel = u8> + 'static,
{
    let (width, height) = image.dimensions();
    let mut out = ImageBuffer::new(width, height);

    let max = u8::max_value();
    let max: f32 = max as f32;
    let percent = (value + 100.0) / 100.0;

    for y in 0..height {
        for x in 0..width {
            let e = image.get_pixel(x, y).map_with_alpha(
                |b| {
                    let c: f32 = b as f32;
                    let d = (c * percent).clamp(0.0, max);

                    d as u8
                },
                |alpha| alpha,
            );

            out.put_pixel(x, y, e);
        }
    }

    out
}

pub fn sepia<I>(image: &I, intensity: f32) -> ImageBuffer<Rgba<u8>, Vec<u8>>
where
    I: GenericImage<Pixel = Rgba<u8>>,
{
    let (width, height) = image.dimensions();
    let depth = 20;
    let mut out = ImageBuffer::new(width, height);

    let percent = intensity / 100.0;
    for (x, y, pixel) in out.enumerate_pixels_mut() {
        let channels = image.get_pixel(x, y).0;
        let mut r = channels[0] as u16;
        let mut g = channels[1] as u16;
        let mut b = channels[2] as u16;
        let gray = (r + g + b) / 3;

        r += depth * 2;
        g += depth;
        b = gray;

        if r > 255 {
            r = 255
        }
        if g > 255 {
            g = 255
        }
        if b > 255 {
            b = 255
        }

        let f = b as f32;
        b = (f - (f * percent)) as u16;

        if b > 255 {
            b = 255
        }
        *pixel = Rgba([r as u8, g as u8, b as u8, channels[3]]);
    }

    out
}

pub fn fill_with_channels(
    width: u32,
    height: u32,
    channels: &[u8; 4],
) -> ImageBuffer<Rgba<u8>, Vec<u8>> {
    let a = channels[3] as f32;
    let r = ((channels[0] as f32) * a / 255.0) as u8;
    let g = ((channels[1] as f32) * a / 255.0) as u8;
    let b = ((channels[2] as f32) * a / 255.0) as u8;
    let fill = [r, g, b, channels[3]];

    let mut out = ImageBuffer::new(width, height);
    for y in 0..height {
        for x in 0..width {
            out.put_pixel(x, y, *Rgba::from_slice(&fill));
        }
    }

    out
}

pub fn restore_transparency<I>(image: &I) -> ImageBuffer<Rgba<u8>, Vec<u8>>
where
    I: GenericImage<Pixel = Rgba<u8>>,
{
    let (width, height) = image.dimensions();
    let mut out = ImageBuffer::new(width, height);
    for y in 0..height {
        for x in 0..width {
            let mut e = image.get_pixel(x, y).0;
            e[3] = 255;

            out.put_pixel(x, y, *Rgba::from_slice(&e));
        }
    }

    out
}

pub fn over<I>(foreground: &I, background: &I) -> ImageBuffer<Rgba<u8>, Vec<u8>>
where
    I: GenericImage<Pixel = Rgba<u8>>,
{
    let (width, height) = foreground.dimensions();
    let mut out = ImageBuffer::new(width, height);
    for (x, y, pixel) in out.enumerate_pixels_mut() {
        let fg_data = foreground.get_pixel(x, y).0;
        let bg_data = background.get_pixel(x, y).0;
        let final_alpha = blend::compute_final_alpha(&fg_data, &bg_data);
        let mut final_data = [0; 4];
        final_data[3] = final_alpha;
        for i in 0..3 {
            let fg_c = fg_data[i] as f32 / 255.0;
            let bg_c = bg_data[i] as f32 / 255.0;
            let final_c = fg_c + bg_c * (1.0 - fg_c);
            final_data[i] = (final_c * 255.0) as u8;
        }
        *pixel = Rgba(final_data);
    }

    out
}

#[allow(dead_code)]
pub fn blend_screen<I>(foreground: &I, background: &I) -> ImageBuffer<Rgba<u8>, Vec<u8>>
where
    I: GenericImage<Pixel = Rgba<u8>>,
{
    process_blend(foreground, background, &blend::blend_screen)
}

#[allow(dead_code)]
pub fn blend_soft_light<I>(foreground: &I, background: &I) -> ImageBuffer<Rgba<u8>, Vec<u8>>
where
    I: GenericImage<Pixel = Rgba<u8>>,
{
    process_blend(foreground, background, &blend::blend_soft_light)
}

#[allow(dead_code)]
pub fn blend_overlay<I>(foreground: &I, background: &I) -> ImageBuffer<Rgba<u8>, Vec<u8>>
where
    I: GenericImage<Pixel = Rgba<u8>>,
{
    process_blend(foreground, background, &blend::blend_overlay)
}

#[allow(dead_code)]
pub fn blend_color_dodge<I>(foreground: &I, background: &I) -> ImageBuffer<Rgba<u8>, Vec<u8>>
where
    I: GenericImage<Pixel = Rgba<u8>>,
{
    process_blend(foreground, background, &blend::blend_color_dodge)
}

#[allow(dead_code)]
pub fn blend_darken<I>(foreground: &I, background: &I) -> ImageBuffer<Rgba<u8>, Vec<u8>>
where
    I: GenericImage<Pixel = Rgba<u8>>,
{
    process_blend(foreground, background, &blend::blend_darken)
}

#[allow(dead_code)]
pub fn blend_lighten<I>(foreground: &I, background: &I) -> ImageBuffer<Rgba<u8>, Vec<u8>>
where
    I: GenericImage<Pixel = Rgba<u8>>,
{
    process_blend(foreground, background, &blend::blend_lighten)
}

#[allow(dead_code)]
pub fn blend_multiply<I>(foreground: &I, background: &I) -> ImageBuffer<Rgba<u8>, Vec<u8>>
where
    I: GenericImage<Pixel = Rgba<u8>>,
{
    process_blend(foreground, background, &blend::blend_multiply)
}

#[allow(dead_code)]
pub fn blend_color_burn<I>(foreground: &I, background: &I) -> ImageBuffer<Rgba<u8>, Vec<u8>>
where
    I: GenericImage<Pixel = Rgba<u8>>,
{
    process_blend(foreground, background, &blend::blend_color_burn)
}

#[allow(dead_code)]
pub fn blend_linear_burn<I>(foreground: &I, background: &I) -> ImageBuffer<Rgba<u8>, Vec<u8>>
where
    I: GenericImage<Pixel = Rgba<u8>>,
{
    process_blend(foreground, background, &blend::blend_linear_burn)
}

#[allow(dead_code)]
pub fn blend_linear_dodge<I>(foreground: &I, background: &I) -> ImageBuffer<Rgba<u8>, Vec<u8>>
where
    I: GenericImage<Pixel = Rgba<u8>>,
{
    process_blend(foreground, background, &blend::blend_linear_dodge)
}

#[allow(dead_code)]
pub fn blend_hard_light<I>(foreground: &I, background: &I) -> ImageBuffer<Rgba<u8>, Vec<u8>>
where
    I: GenericImage<Pixel = Rgba<u8>>,
{
    process_blend(foreground, background, &blend::blend_hard_light)
}

#[allow(dead_code)]
pub fn blend_vivid_light<I>(foreground: &I, background: &I) -> ImageBuffer<Rgba<u8>, Vec<u8>>
where
    I: GenericImage<Pixel = Rgba<u8>>,
{
    process_blend(foreground, background, &blend::blend_vivid_light)
}

#[allow(dead_code)]
pub fn blend_linear_light<I>(foreground: &I, background: &I) -> ImageBuffer<Rgba<u8>, Vec<u8>>
where
    I: GenericImage<Pixel = Rgba<u8>>,
{
    process_blend(foreground, background, &blend::blend_linear_light)
}

#[allow(dead_code)]
pub fn blend_pin_light<I>(foreground: &I, background: &I) -> ImageBuffer<Rgba<u8>, Vec<u8>>
where
    I: GenericImage<Pixel = Rgba<u8>>,
{
    process_blend(foreground, background, &blend::blend_pin_light)
}

#[allow(dead_code)]
pub fn blend_difference<I>(foreground: &I, background: &I) -> ImageBuffer<Rgba<u8>, Vec<u8>>
where
    I: GenericImage<Pixel = Rgba<u8>>,
{
    process_blend(foreground, background, &blend::blend_difference)
}

#[allow(dead_code)]
pub fn blend_exclusion<I>(foreground: &I, background: &I) -> ImageBuffer<Rgba<u8>, Vec<u8>>
where
    I: GenericImage<Pixel = Rgba<u8>>,
{
    process_blend(foreground, background, &blend::blend_exclusion)
}

fn process_blend<I>(
    foreground: &I,
    background: &I,
    f: &dyn Fn(u8, u8) -> u8,
) -> ImageBuffer<Rgba<u8>, Vec<u8>>
where
    I: GenericImage<Pixel = Rgba<u8>>,
{
    let (width, height) = foreground.dimensions();
    let mut out = ImageBuffer::new(width, height);
    for (x, y, pixel) in out.enumerate_pixels_mut() {
        let fg_data = foreground.get_pixel(x, y).0;
        let bg_data = background.get_pixel(x, y).0;
        let final_r = f(fg_data[0], bg_data[0]);
        let final_g = f(fg_data[1], bg_data[1]);
        let final_b = f(fg_data[2], bg_data[2]);
        let final_alpha = blend::compute_final_alpha(&fg_data, &bg_data);
        *pixel = Rgba([final_r, final_g, final_b, final_alpha]);
    }

    out
}

fn saturate_value(s: f32, percent: f32) -> f32 {
    let mut s = s;
    if percent >= 0.0 {
        let interval = 1.0 - s;
        s = s + percent * interval * s;
    } else {
        s = s + percent * s;
    }
    s
}

fn rgb_to_hls(rgba: &[u8; 4]) -> [f32; 3] {
    let r = rgba[0] as f32 / 255.0;
    let g = rgba[1] as f32 / 255.0;
    let b = rgba[2] as f32 / 255.0;

    let max = float_max(r, g, b);
    let min = float_min(r, g, b);

    let mut hue = 0.0;
    let mut saturation = 0.0;
    let lumination = (max + min) / 2.0;

    if max == min {
        return [hue, lumination, saturation];
    }

    let delta = max - min;
    if lumination < 0.5 {
        saturation = delta / (max + min);
    } else {
        saturation = delta / (2.0 - max - min);
    }

    if r == max {
        hue = (g - b) / delta;
    } else if g == max {
        hue = 2.0 + (b - r) / delta;
    } else {
        hue = 4.0 + (r - g) / delta;
    }

    hue /= 6.0;
    if hue < 0.0 {
        hue += 1.0;
    }

    [hue, lumination, saturation]
}

pub fn saturate<I>(image: &I, value: f32) -> ImageBuffer<Rgba<u8>, Vec<u8>>
where
    I: GenericImage<Pixel = Rgba<u8>>,
{
    let (width, height) = image.dimensions();
    let mut out = ImageBuffer::new(width, height);

    let percent = value / 100.0;
    for (x, y, pixel) in out.enumerate_pixels_mut() {
        let data = image.get_pixel(x, y).0;
        let mut hls = rgb_to_hls(&data);
        hls[2] = saturate_value(hls[2], percent);
        let rgb = hls_to_rgb(&hls, data[3]);

        *pixel = Rgba(rgb);
    }

    out
}

fn hls_to_rgb(hsl: &[f32; 3], alpha: u8) -> [u8; 4] {
    let (r, g, b, m1, m2);
    let hue = hsl[0];
    let lumination = hsl[1];
    let saturation = hsl[2];
    if saturation == 0.0 {
        r = lumination;
        g = lumination;
        b = lumination;
    } else {
        if lumination <= 0.5 {
            m2 = lumination * (1.0 + saturation);
        } else {
            m2 = lumination + saturation - lumination * saturation;
        }
        m1 = 2.0 * lumination - m2;
        r = hue_to_rgb(m1, m2, hue + (1.0 / 3.0));
        g = hue_to_rgb(m1, m2, hue);
        b = hue_to_rgb(m1, m2, hue - (1.0 / 3.0));
    }

    let red = (r * 255.0) as u8;
    let green = (g * 255.0) as u8;
    let blue = (b * 255.0) as u8;
    [red, green, blue, alpha]
}

fn hue_to_rgb(m1: f32, m2: f32, hue: f32) -> f32 {
    let mut hue = hue;
    if hue < 0.0 {
        hue += 1.0;
    } else if hue > 1.0 {
        hue -= 1.0;
    }

    if (6.0 * hue) < 1.0 {
        m1 + (m2 - m1) * hue * 6.0
    } else if (2.0 * hue) < 1.0 {
        m2
    } else if (3.0 * hue) < 2.0 {
        m1 + (m2 - m1) * ((2.0 / 3.0) - hue) * 6.0
    } else {
        m1
    }
}

fn float_max(a: f32, b: f32, c: f32) -> f32 {
    if a >= b && a >= c {
        a
    } else if b >= a && b >= c {
        b
    } else {
        c
    }
}

fn float_min(a: f32, b: f32, c: f32) -> f32 {
    if a <= b && a <= c {
        a
    } else if b <= a && b <= c {
        b
    } else {
        c
    }
}