mirror of
https://github.com/storytold/photocraft.git
synced 2026-12-02 06:20:48 +01:00
Merge pr/1473
This commit is contained in:
2 files changed
+75
-3
No files matched your search
@@ -676,7 +676,11 @@ pub fn write(adj: &Adjustment) -> Vec<([u8; 4], Vec<u8>)> {
|
||||
put16(&mut v, 0);
|
||||
v.extend_from_slice(&((density.clamp(0.0, 1.0) * 100.0).round() as u32).to_be_bytes());
|
||||
v.push(u8::from(*preserve_luminosity));
|
||||
v.push(0);
|
||||
// Photoshop requires the 17-byte Photo Filter payload after the version to be
|
||||
// padded to a 4-byte boundary INSIDE the tagged block's declared length.
|
||||
// A single pad byte made an 18-byte `phfl` block that Photoshop could not open
|
||||
// even though PhotoCraft and other tolerant readers accepted it (#1455).
|
||||
v.extend_from_slice(&[0, 0, 0]);
|
||||
return vec![(*b"phfl", v)];
|
||||
}
|
||||
Adjustment::ChannelMixer { matrix, monochrome } => {
|
||||
@@ -923,12 +927,35 @@ mod tests {
|
||||
let mut v2 = i16s(&[2, 0]);
|
||||
v2.extend([0xff, 0xff, 0x80, 0x00, 0, 0, 0, 0]);
|
||||
v2.extend(40u32.to_be_bytes());
|
||||
v2.extend([1, 0]);
|
||||
v2.extend([1, 0, 0, 0]);
|
||||
assert_eq!(v2.len(), 20, "Photo Filter must occupy 20 bytes");
|
||||
assert_eq!(&v2[17..], &[0, 0, 0], "padding is included inside the block length");
|
||||
let a = parse(b"phfl", &v2, None, Channels::Rgb);
|
||||
let Adjustment::PhotoFilter { color, density, preserve_luminosity: true } = a else { panic!("{a:?}") };
|
||||
assert_eq!(color, [1.0, 32768.0 / 65535.0, 0.0]);
|
||||
assert_eq!(density, 0.4);
|
||||
assert_eq!(write(&a)[0].1, v2, "version 2 RGB is written back byte-exact");
|
||||
// Older PhotoCraft files used only one padding byte (18 bytes); keep importing them,
|
||||
// but never produce that Photoshop-incompatible length again.
|
||||
assert_eq!(parse(b"phfl", &v2[..18], None, Channels::Rgb), a);
|
||||
for preserve_luminosity in [false, true] {
|
||||
for density in [0.0, 0.14, 1.0] {
|
||||
let adjustment = Adjustment::PhotoFilter { color: [0.8, 0.4, 0.2], density, preserve_luminosity };
|
||||
let blocks = write(&adjustment);
|
||||
assert_eq!(blocks.len(), 1);
|
||||
assert_eq!(blocks[0].0, *b"phfl");
|
||||
assert_eq!(blocks[0].1.len(), 20);
|
||||
assert_eq!(&blocks[0].1[17..], &[0, 0, 0]);
|
||||
let Adjustment::PhotoFilter { color, density: read_density, preserve_luminosity: read_preserve } =
|
||||
parse(b"phfl", &blocks[0].1, None, Channels::Rgb)
|
||||
else {
|
||||
panic!("written phfl must parse")
|
||||
};
|
||||
assert!(color.iter().zip([0.8, 0.4, 0.2]).all(|(got, expected)| (got - expected).abs() <= 1.0 / 65535.0));
|
||||
assert!((read_density - density).abs() < 0.00001);
|
||||
assert_eq!(read_preserve, preserve_luminosity);
|
||||
}
|
||||
}
|
||||
// Version 2, Lab colour structure: L 50, a 0, b 0 is mid gray.
|
||||
let mut lab = i16s(&[2, 7, 5000, 0, 0, 0]);
|
||||
lab.extend(25u32.to_be_bytes());
|
||||
|
||||
@@ -437,7 +437,13 @@ impl Ex {
|
||||
_ => adjust_map::Channels::Other,
|
||||
};
|
||||
let cged = raw.iter().find(|(k, _)| k == b"CgEd").map(|(_, d)| d.clone());
|
||||
let keep = raw.iter().any(|(k, d)| adjust_map::ADJUSTMENT_KEYS.contains(&k) && adjust_map::parse(k, d, cged.as_deref(), channels) == *a);
|
||||
let keep = raw.iter().any(|(k, d)| {
|
||||
// A previously saved PhotoCraft v2 Photo Filter block may be 18 bytes.
|
||||
// It parses here, but Photoshop requires four-byte alignment, so repair
|
||||
// that legacy record on re-save rather than preserving the broken bytes.
|
||||
let legacy_phfl = k == b"phfl" && d.starts_with(&[0, 2]) && d.len() % 4 != 0;
|
||||
!legacy_phfl && adjust_map::ADJUSTMENT_KEYS.contains(&k) && adjust_map::parse(k, d, cged.as_deref(), channels) == *a
|
||||
});
|
||||
if !keep {
|
||||
raw.retain(|(k, _)| !adjust_map::ADJUSTMENT_KEYS.contains(&k) && k != b"CgEd");
|
||||
let w = adjust_map::write(a);
|
||||
@@ -1369,6 +1375,45 @@ mod tests {
|
||||
Document::new("size estimate", photocraft_geom::Size::new(width, height), ColorMode::Rgb, SampleType::U8)
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn photo_filter_psd_is_four_byte_aligned_and_round_trips() {
|
||||
let color = [60000u16, 30000, 0].map(|x| x as f32 / 65535.0);
|
||||
let adjustment = photocraft_doc::Adjustment::PhotoFilter { color, density: 0.14, preserve_luminosity: true };
|
||||
let mut doc =
|
||||
Document::with_background("Photo Filter PSD", photocraft_geom::Size::new(32, 24), ColorMode::Rgb, SampleType::U8, photocraft_doc::Color::WHITE);
|
||||
let mut layer = Layer::new("Warming Filter", LayerContent::Adjustment(adjustment.clone()));
|
||||
doc.layers.push(layer.clone());
|
||||
|
||||
let assert_phfl = |file: &PsdFile| {
|
||||
let records = &file.layer_info.as_ref().unwrap().layers;
|
||||
let data =
|
||||
records.iter().flat_map(|r| r.blocks.iter()).find(|b| b.key == *b"phfl").map(|b| b.data.as_slice()).expect("Photo Filter adjustment data");
|
||||
assert_eq!(data.len(), 20, "PSD layer block length includes four-byte padding");
|
||||
assert_eq!(&data[0..2], &[0, 2], "version 2 RGB Photo Filter");
|
||||
assert_eq!(&data[17..], &[0, 0, 0], "three padding bytes after the 17-byte payload");
|
||||
};
|
||||
|
||||
let exported = document_to_psd(&doc);
|
||||
assert_phfl(&exported);
|
||||
let encoded = exported.to_bytes().unwrap();
|
||||
let parsed = PsdFile::from_bytes(&encoded).unwrap();
|
||||
assert_phfl(&parsed);
|
||||
let (restored, _) = crate::psd_import::psd_to_document(&parsed);
|
||||
assert!(restored.layers.iter().any(|l| l.content == LayerContent::Adjustment(adjustment.clone())));
|
||||
|
||||
// Importing a legacy 18-byte block must still work; saving it again should repair it
|
||||
// even when the adjustment values have not changed (normally raw blocks are reused).
|
||||
let mut legacy = crate::adjust_map::write(&adjustment)[0].1.clone();
|
||||
legacy.truncate(18);
|
||||
layer.psd_blocks = vec![(*b"phfl", std::sync::Arc::new(legacy))];
|
||||
doc.layers.pop();
|
||||
doc.layers.push(layer);
|
||||
let repaired = document_to_psd(&doc);
|
||||
assert_phfl(&repaired);
|
||||
let repaired_bytes = repaired.to_bytes().unwrap();
|
||||
assert_phfl(&PsdFile::from_bytes(&repaired_bytes).unwrap());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn size_limit_boundary_keeps_limit_and_switches_above_it() {
|
||||
assert!(!exceeds_psd_size_limit(Some(PSD_MAX_FILE_SIZE)));
|
||||
|
||||
Reference in new issue
Block a user