Skip to content

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

PSD: Hue/Saturation export writes each triplet in its own slot (#7065)

David Manne1ce85a5dfc6
diff --git a/crates/io/src/adjust_map.rs b/crates/io/src/adjust_map.rs
index d4bc31269..8d37c9158 100644
--- a/crates/io/src/adjust_map.rs
+++ b/crates/io/src/adjust_map.rs
@@ -228,8 +228,10 @@ fn parse_any(key: &[u8; 4], data: &[u8], cged: Option<&[u8]>) -> Adjustment {
*r = HueRange { hue: h, saturation: s, lightness: l, bounds: HueRange::canonical_bounds([a, b, c, d]) };
}
}
+ let hue = f32::from(bei16(data, base)?);
Some(Adjustment::HueSaturation {
- hue: f32::from(bei16(data, base)?),
+ // Photoshop stores the colorize hue signed (200 as -160); the model keeps 0..360.
+ hue: if colorize { hue.rem_euclid(360.0) } else { hue },
saturation: f32::from(bei16(data, base + 2)?),
lightness: f32::from(bei16(data, base + 4)?),
colorize,
@@ -621,10 +623,16 @@ pub fn write(adj: &Adjustment) -> Vec<([u8; 4], Vec<u8>)> {
put16(&mut v, 2);
v.push(u8::from(*colorize));
v.push(0);
- for _ in 0..2 {
- for x in [hue, saturation, lightness] {
- v.extend_from_slice(&clamp_i16(*x).to_be_bytes());
- }
+ // The colorize triplet, then the master triplet. The inactive one gets Photoshop's
+ // defaults; the colorize hue (0..360 in the model) is stored signed, -180..180.
+ let (colorize_hsl, master) = if *colorize {
+ let h = hue.rem_euclid(360.0);
+ ([if h > 180.0 { h - 360.0 } else { h }, *saturation, *lightness], [0.0; 3])
+ } else {
+ ([30.0, 25.0, 0.0], [*hue, *saturation, *lightness])
+ };
+ for x in colorize_hsl.into_iter().chain(master) {
+ v.extend_from_slice(&clamp_i16(x).to_be_bytes());
}
// Six hue ranges (reds, yellows, greens, cyans, blues, magentas): range values in
// 0..360, then the range's hue, saturation and lightness.
@@ -948,6 +956,21 @@ mod tests {
assert_eq!(back, a);
}
+ /// Photoshop keeps the colorize triplet at byte 4 and the master triplet at byte 10, and fills
+ /// the inactive one with its defaults: colorize (30, 25, 0), master (0, 0, 0). The colorize hue
+ /// is stored signed (200 -> -160).
+ #[test]
+ fn hue2_writes_each_triplet_in_its_own_slot() {
+ let triplets = |a: &Adjustment| -> Vec<i16> {
+ let blocks = write(a);
+ (0..6).map(|i| bei16(&blocks[0].1, 4 + 2 * i).unwrap_or(i16::MIN)).collect()
+ };
+ let on = Adjustment::HueSaturation { hue: 200.0, saturation: 40.0, lightness: 10.0, colorize: true, ranges: HueRange::defaults() };
+ assert_eq!(triplets(&on), [-160, 40, 10, 0, 0, 0]);
+ let off = Adjustment::HueSaturation { hue: -30.0, saturation: 20.0, lightness: 5.0, colorize: false, ranges: HueRange::defaults() };
+ assert_eq!(triplets(&off), [30, 25, 0, -30, 20, 5]);
+ }
+
#[test]
fn roundtrips() {
rt(Adjustment::Invert);