Merge pull request #1135 from vangel76/fix-psd-lenient-layer-section
# Conflicts: # crates/engine/src/psd_import.rs # crates/engine/src/tests_vector_import.rs # crates/psd/src/tests.rs
This commit is contained in:
@@ -115,10 +115,45 @@ pub fn import(proj: &mut Project, psd: &Psd, path: &str, name: &str, retain: boo
|
||||
if flattened > 0 {
|
||||
cx.warnings.push(format!("{flattened} group(s) nested deeper than {} levels were flattened into their parent", effectcraft_psd::MAX_GROUP_DEPTH));
|
||||
}
|
||||
let comp = build_comp(proj, &mut cx, name, &tree, None);
|
||||
cx.warnings.extend(psd.warnings.iter().cloned());
|
||||
let comp = if psd.layers.is_empty() && !psd.warnings.is_empty() {
|
||||
flattened_comp(proj, &mut cx, name)
|
||||
} else {
|
||||
build_comp(proj, &mut cx, name, &tree, None)
|
||||
};
|
||||
PsdImport { comp, folder, items: cx.items, warnings: cx.warnings }
|
||||
}
|
||||
|
||||
/// The layers could not be read: the merged image as the composition's only layer.
|
||||
fn flattened_comp(proj: &mut Project, cx: &mut Ctx, name: &str) -> ItemId {
|
||||
let cid = proj.add_item(name, Label::Sandstone, None, ItemKind::Comp(Arc::new(new_comp(cx))));
|
||||
cx.items.push(cid);
|
||||
let mut comp = new_comp(cx);
|
||||
let file = std::path::Path::new(cx.path).file_name().map(|n| n.to_string_lossy().to_string()).unwrap_or_default();
|
||||
let f = Footage {
|
||||
path: cx.path.to_string(),
|
||||
kind: FootageKind::Still,
|
||||
width: cx.psd.width,
|
||||
height: cx.psd.height,
|
||||
pixel_aspect: 1.0,
|
||||
frame_rate: cx.rate,
|
||||
has_video: true,
|
||||
alpha: if cx.psd.merged_alpha { AlphaMode::Straight } else { AlphaMode::Ignore },
|
||||
loop_count: 1,
|
||||
codec: "PSD".into(),
|
||||
layer: None,
|
||||
..Default::default()
|
||||
};
|
||||
let id = proj.add_item(&file, Label::Lavender, Some(cx.folder), ItemKind::Footage(f));
|
||||
cx.items.push(id);
|
||||
let l = build::layer(proj, &comp, name, LayerSource::Footage { item: id }, (cx.psd.width, cx.psd.height), None);
|
||||
comp.layers.push(l);
|
||||
if let Some(item) = proj.item_mut(cid) {
|
||||
item.kind = ItemKind::Comp(Arc::new(comp));
|
||||
}
|
||||
cid
|
||||
}
|
||||
|
||||
fn new_comp(cx: &Ctx) -> Comp {
|
||||
// A PSB can be 300,000 px a side; a composition is at most 30,000 (#788).
|
||||
let size = |v: u32| effectcraft_project::limits::size(v.into());
|
||||
|
||||
@@ -310,3 +310,30 @@ fn psd_with_thousands_of_nested_groups_imports_on_a_small_stack() {
|
||||
assert_eq!(comps, 65);
|
||||
assert!(warned);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn psd_with_damaged_layer_section_imports_the_flattened_image() {
|
||||
let mut d = WDoc::new(30, 20);
|
||||
d.layers = vec![
|
||||
WLayer::solid("Back", Rect::new(0, 0, 30, 20), [0.0, 0.0, 1.0, 1.0]),
|
||||
WLayer::solid("Box", Rect::new(2, 2, 8, 8), [1.0, 0.0, 0.0, 1.0]).with_blend(b"mul "),
|
||||
];
|
||||
let mut bytes = write(&d);
|
||||
let at = bytes.windows(8).position(|w| w == b"8BIMmul ").unwrap();
|
||||
bytes[at] = b'X';
|
||||
let path = tmp("damaged.psd");
|
||||
std::fs::write(&path, bytes).unwrap();
|
||||
let mut s = Session::default();
|
||||
let r = s.execute_checked("file.import", json!({"paths": [path], "importAs": "composition"})).unwrap();
|
||||
// Photoshop import warnings are reported with the import's `errors` (as for adjustment layers).
|
||||
let warnings = r["errors"].as_array().unwrap();
|
||||
assert_eq!(warnings.len(), 1, "{r}");
|
||||
assert!(warnings[0].as_str().unwrap().contains("Layers could not be read"), "{r}");
|
||||
let cid = effectcraft_project::ItemId(r["comps"][0].as_u64().unwrap());
|
||||
let c = s.project.comp(cid).unwrap().clone();
|
||||
assert_eq!((c.width, c.height), (30, 20));
|
||||
assert_eq!(c.layers.len(), 1);
|
||||
let LayerSource::Footage { item } = c.layers[0].source else { panic!("footage") };
|
||||
let ItemKind::Footage(f) = &s.project.item(item).unwrap().kind else { panic!() };
|
||||
assert!(f.layer.is_none(), "the merged image, not a Photoshop layer");
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user