image_blend/pixelops.rs
1/*!
2This module contains functions for common blending operations on pixel values, such as addition, subtraction, multiplication, etc.
3
4All arguments and returns are f64 values in the range 0.0..1.0.
5
6`a` is self, `b` is the other pixel.
7
8Returns 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.
9
10Formulas taken from [Wikipedia](https://en.wikipedia.org/wiki/Blend_modes).
11
12Analagous blend modes of the same name in Photoshop.
13
14# Examples
15
16```
17use image::open;
18use image_blend::{BufferBlend};
19use image_blend::pixelops::pixel_mult;
20
21// Load an image
22let mut img1_dynamic = open("test_data/1.png").unwrap();
23let mut img1_buffer = img1_dynamic.as_mut_rgba8().unwrap();
24
25// Load another image
26let img2_dynamic = open("test_data/2.png").unwrap();
27let img2_buffer = img2_dynamic.to_rgba16();
28
29// Blend the images using the pixel_mult function
30img1_buffer.blend(&img2_buffer, pixel_mult, true, false).unwrap();
31img1_buffer.save("tests_out/doctest_buffer_blend_result.png").unwrap();
32
33```
34*/
35
36/// Adds `a` to `b`.
37#[must_use]
38pub fn pixel_add(a: f64, b: f64) -> f64 {
39 a + b
40}
41
42/// Subtracts `b` from `a`.
43#[must_use]
44pub fn pixel_sub(a: f64, b: f64) -> f64 {
45 a - b
46}
47
48/// Divides `a` by `b`. If `b` is 0, returns 1.
49#[must_use]
50pub fn pixel_div(a: f64, b: f64) -> f64 {
51 if b == 0. {
52 return 1.;
53 }
54 a / b
55}
56
57/// Returns the darker value between `a` and `b`.
58#[must_use]
59pub fn pixel_darker(a: f64, b: f64) -> f64 {
60 a.min(b)
61}
62
63/// Returns the lighter value between `a` and `b`.
64#[must_use]
65pub fn pixel_lighter(a: f64, b: f64) -> f64 {
66 a.max(b)
67}
68
69/// Returns the absolute difference between `a` and `b`.
70#[must_use]
71pub fn pixel_diff(a: f64, b: f64) -> f64 {
72 (a - b).abs()
73}
74
75/// Multiplies `a` by `b`.
76#[must_use]
77pub fn pixel_mult(a: f64, b: f64) -> f64 {
78 a * b
79}
80
81/// Applies the screen blend mode to `a` and `b`.
82#[must_use]
83pub fn pixel_screen(a: f64, b: f64) -> f64 {
84 1.0 - (1.0 - a) * (1.0 - b)
85}
86
87/// Applies the overlay blend mode to `a` and `b`.
88#[must_use]
89pub fn pixel_overlay(a: f64, b: f64) -> f64 {
90 if a < 0.5 {
91 2.0 * a * b
92 } else {
93 1.0 - 2.0 * (1.0 - a) * (1.0 - b)
94 }
95}
96
97/// Applies the hard light blend mode to `a` and `b`.
98#[must_use]
99pub fn pixel_hard_light(a: f64, b: f64) -> f64 {
100 if b < 0.5 {
101 2.0 * a * b
102 } else {
103 1.0 - 2.0 * (1.0 - a) * (1.0 - b)
104 }
105}
106
107/// Applies the soft light blend mode to `a` and `b`. Uses W3C formula.
108#[must_use]
109pub fn pixel_soft_light(a: f64, b: f64) -> f64 {
110 if b <= 0.5 {
111 a - (1.0 - 2.0 * b) * a * (1.0 - a)
112 } else {
113 let gwc3 = if a <= 0.25 {
114 ((16.0 * a - 12.0) * a + 4.0) * a
115 } else {
116 a.sqrt()
117 };
118 a + (2.0 * b - 1.0) * (gwc3 - a)
119 }
120}
121
122/// Returns `b`. Basically paste/overwrite.
123#[must_use]
124pub fn pixel_normal(_a: f64, b: f64) -> f64 {
125 b
126}