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
/*!
This module contains functions for performing pixel operations.
All arguments and returns are f64 values in the range 0.0..1.0.
Returns are not bounded in these functions, but are clamped to 0.0..1.0 before being converted back to the input type in the blend trait.
Formulas taken from [Wikipedia](https://en.wikipedia.org/wiki/Blend_modes).
Analagous blend modes of the same name in Photoshop.
# Examples
```
use image::open;
use image_blend::{BufferBlend};
use image_blend::pixelops::pixel_mult;
// Load an image
let mut img1_dynamic = open("test_data/1.png").unwrap();
let mut img1_buffer = img1_dynamic.as_mut_rgba8().unwrap();
// Load another image
let img2_dynamic = open("test_data/2.png").unwrap();
let img2_buffer = img2_dynamic.to_rgba16();
// Blend the images using the pixel_mult function
img1_buffer.blend(&img2_buffer, pixel_mult, true, false).unwrap();
img1_buffer.save("tests_out/doctest_buffer_blend_result.png").unwrap();
```
*/
#[must_use]
pub fn pixel_add(a: f64, b: f64) -> f64 {
a + b
}
#[must_use]
pub fn pixel_sub(a: f64, b: f64) -> f64 {
a - b
}
#[must_use]
pub fn pixel_div(a: f64, b: f64) -> f64 {
if b == 0. {
return 1.
}
a / b
}
#[must_use]
pub fn pixel_darker(a: f64, b: f64) -> f64 {
a.min(b)
}
#[must_use]
pub fn pixel_lighter(a: f64, b: f64) -> f64 {
a.max(b)
}
#[must_use]
pub fn pixel_diff(a: f64, b: f64) -> f64 {
(a - b).abs()
}
#[must_use]
pub fn pixel_mult(a: f64, b: f64) -> f64 {
a * b
}
#[must_use]
pub fn pixel_screen(a: f64, b: f64) -> f64 {
1.0 - (1.0 - a) * (1.0 - b)
}
#[must_use]
pub fn pixel_overlay(a: f64, b: f64) -> f64 {
if a < 0.5 {
2.0 * a * b
} else {
1.0 - 2.0 * (1.0 - a) * (1.0 - b)
}
}
#[must_use]
pub fn pixel_hard_light(a: f64, b: f64) -> f64 {
if b < 0.5 {
2.0 * a * b
} else {
1.0 - 2.0 * (1.0 - a) * (1.0 - b)
}
}
#[must_use]
pub fn pixel_soft_light(a: f64, b: f64) -> f64 {
if b <= 0.5 {
a - (1.0 - 2.0 * b) * a * (1.0 - a)
} else {
let gwc3 = if a <= 0.25 {
((16.0 * a - 12.0) * a + 4.0) * a
} else {
a.sqrt()
};
a + (2.0 * b - 1.0) * (gwc3 - a)
}
}