Skip to content

PhotoCraft: An open-source, photo editor written by scratch in pure Rust

Integration: Levels gamma just above 1 no longer turns black into NaN (#5143)

David Mannae8809c02962
diff --git a/crates/compose/proptest-regressions/cmyk_tests.txt b/crates/compose/proptest-regressions/cmyk_tests.txt
new file mode 100644
index 000000000..23a4b6e95
--- /dev/null
+++ b/crates/compose/proptest-regressions/cmyk_tests.txt
@@ -0,0 +1,7 @@
+# Seeds for failure cases proptest has generated in the past. It is
+# automatically read and these particular cases re-run before any
+# novel cases are generated.
+#
+# It is recommended to check this file in to source control so that
+# everyone who runs the test benefits from these saved cases.
+cc a795914e4492bef1e662315563d48d54f09030c48e046df95329465c86dde4b4 # shrinks to s = Spec { w: 4, h: 6, layers: [Group { children: [Pixels { kind: Raster, blend: Normal, fills: [(Rect { x0: 0, y0: 0, x1: 1, y1: 1 }, [1.0, 0.0, 0.0, 0.0, 1.0])], opacity: 1.0, fill: 1.0, clipped: false, visible: true, mask: None }], pass_through: false, blend: Normal, opacity: 1.0, fill: 1.0, mask: None }, Pixels { kind: Adjust(Levels { channel: 0, levels: [0.0, 0.6, 1.0105422, 0.0, 0.7] }), blend: Normal, fills: [(Rect { x0: 0, y0: 0, x1: 1, y1: 1 }, [0.0, 0.0, 0.0, 0.0, 0.0])], opacity: 0.0, fill: 0.0, clipped: false, visible: true, mask: None }] }
diff --git a/crates/compose/src/adjust.rs b/crates/compose/src/adjust.rs
index d122a0999..205896a56 100644
--- a/crates/compose/src/adjust.rs
+++ b/crates/compose/src/adjust.rs
@@ -1299,6 +1299,11 @@ pub fn levels_gamma(u: f32, g: f32) -> f32 {
return u.powf(c);
}
let b = (a * u1 - u1.powf(c)) / u1.powf(q);
+ // Just above gamma 1 the toe shrinks to nothing: u1^q underflows to 0 while u1 doesn't, and
+ // the power curve is the toe's limit there (otherwise b = inf and black turns NaN).
+ if !b.is_finite() {
+ return u.powf(c);
+ }
a * u - b * u.powf(q)
}
diff --git a/crates/compose/src/tests.rs b/crates/compose/src/tests.rs
index 88e1dd0ac..f6989b429 100644
--- a/crates/compose/src/tests.rs
+++ b/crates/compose/src/tests.rs
@@ -2724,7 +2724,7 @@ fn levels_steps_match_photoshop() {
// jump where it meets the power curve.
#[test]
fn levels_gamma_toe_is_monotone_for_any_gamma() {
- for g in [1.0001, 1.05, 1.5, 3.0, 9.99, 1e6, f32::INFINITY] {
+ for g in [1.0001, 1.01, 1.0105422, 1.02, 1.05, 1.5, 3.0, 9.99, 1e6, f32::INFINITY] {
let mut last = 0.0f32;
for k in 0..=4096 {
let u = k as f32 / 4096.0;