mirror of
https://github.com/storytold/photocraft.git
synced 2026-12-02 06:20:48 +01:00
fix(io): honor version-aware OpenRaster group isolation (#5872)
This commit is contained in:
1 parent
840edc311c
commit
f61ed26e14
3 files changed
+120
-7
No files matched your search
+32
-5
@@ -258,9 +258,23 @@ fn props(node: roxmltree::Node<'_, '_>, layer: &mut Layer, index: usize, warning
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// OpenRaster 0.0.6 defaults to isolated groups and makes `auto` explicitly pass-through.
|
||||
/// Older files used inference from the group's blend mode; keep that behavior when the
|
||||
/// version is absent or unrecognized, too. Compare the numeric core, not version strings.
|
||||
fn uses_explicit_group_isolation(version: Option<&str>) -> bool {
|
||||
let Some(core) = version.and_then(|v| v.trim().split(['-', '+']).next()) else { return false };
|
||||
let mut parts = core.split('.');
|
||||
let number = |part: Option<&str>| part.and_then(|s| s.parse::<u32>().ok());
|
||||
match (number(parts.next()), number(parts.next()), number(parts.next()), parts.next()) {
|
||||
(Some(major), Some(minor), Some(patch), None) => [major, minor, patch] >= [0, 0, 6],
|
||||
_ => false,
|
||||
}
|
||||
}
|
||||
|
||||
struct Reader<'a, 'c> {
|
||||
zip: ZipReader<'a>,
|
||||
ctl: &'c Interrupt<'c>,
|
||||
explicit_group_isolation: bool,
|
||||
nodes: usize,
|
||||
budget: usize,
|
||||
warnings: Warnings,
|
||||
@@ -325,10 +339,15 @@ impl Reader<'_, '_> {
|
||||
"stack" => {
|
||||
let mut layer = Layer::group("", Vec::new());
|
||||
props(child, &mut layer, index, &mut self.warnings)?;
|
||||
// `auto` (the default) isolates only when something needs it: an
|
||||
// OpenRaster group without its own blend mode is a pass-through group.
|
||||
let isolate = child.attribute("isolation").map(str::trim) == Some("isolate");
|
||||
if !isolate && layer.blend == BlendMode::Normal {
|
||||
let isolation = child.attribute("isolation").map(str::trim);
|
||||
let pass_through = if self.explicit_group_isolation {
|
||||
// Since 0.0.6 only explicit `auto` passes through, ignoring the
|
||||
// group's composite-op. An omitted isolation means `isolate`.
|
||||
isolation == Some("auto")
|
||||
} else {
|
||||
isolation != Some("isolate") && layer.blend == BlendMode::Normal
|
||||
};
|
||||
if pass_through {
|
||||
layer.blend = BlendMode::PassThrough;
|
||||
}
|
||||
let children = self.stack(child, depth + 1)?;
|
||||
@@ -415,7 +434,15 @@ pub(crate) fn import(name: &str, bytes: &[u8], ctl: &Interrupt<'_>) -> Result<Im
|
||||
return Err(invalid("the canvas exceeds the 1 GiB pixel limit"));
|
||||
}
|
||||
let root = image.children().find(|n| n.has_tag_name("stack")).ok_or_else(|| invalid("stack.xml has no root <stack>"))?;
|
||||
let mut reader = Reader { zip, ctl, nodes: 0, budget: MAX_PIXEL_BYTES, warnings: Warnings::default(), images: Vec::new() };
|
||||
let mut reader = Reader {
|
||||
zip,
|
||||
ctl,
|
||||
explicit_group_isolation: uses_explicit_group_isolation(image.attribute("version")),
|
||||
nodes: 0,
|
||||
budget: MAX_PIXEL_BYTES,
|
||||
warnings: Warnings::default(),
|
||||
images: Vec::new(),
|
||||
};
|
||||
let nodes = reader.stack(root, 1)?;
|
||||
ctl.progress(0.8);
|
||||
let depth = document_depth(&reader.images);
|
||||
|
||||
@@ -205,6 +205,84 @@ fn gray16_png(w: u32, h: u32) -> Vec<u8> {
|
||||
codecs::encode(&Image::from_raw(w, h, ChannelLayout::Gray, CSample::U16, data).unwrap(), Format::Png, &Default::default()).unwrap()
|
||||
}
|
||||
|
||||
/// A green Multiply layer over a red backdrop distinguishes isolation (green) from
|
||||
/// pass-through (black), even though both groups use Normal compositing by default.
|
||||
fn group_isolation_document(version: Option<&str>, attributes: &str, depth: SampleType) -> Document {
|
||||
let version = version.map(|v| format!("version=\"{v}\"")).unwrap_or_default();
|
||||
let xml = format!(
|
||||
r#"<image {version} w="1" h="1"><stack>
|
||||
<stack name="Group" {attributes}>
|
||||
<layer name="Multiply" src="green.png" composite-op="svg:multiply"/>
|
||||
</stack>
|
||||
<layer name="Backdrop" src="red.png"/>
|
||||
</stack></image>"#
|
||||
);
|
||||
let fmt = PixelFormat::new(ColorMode::Rgb, depth, true);
|
||||
let png = |rgba| {
|
||||
let image = Image::from_raw(1, 1, ChannelLayout::Rgba, csample(depth), px(fmt, rgba)).unwrap();
|
||||
codecs::encode(&image, Format::Png, &Default::default()).unwrap()
|
||||
};
|
||||
let bytes = archive(&xml, &[("green.png", png([0.0, 1.0, 0.0, 1.0])), ("red.png", png([1.0, 0.0, 0.0, 1.0]))]);
|
||||
let result = crate::import("isolation.ora", &bytes).unwrap();
|
||||
assert!(result.warnings.is_empty(), "{:?}", result.warnings);
|
||||
assert_eq!(result.document.depth, depth);
|
||||
result.document
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn omitted_group_isolation_defaults_to_isolated_since_version_0_0_6() {
|
||||
for depth in [SampleType::U8, SampleType::U16] {
|
||||
for version in ["0.0.6", " 0.0.6 ", "0.0.6+app.1", "0.0.6-rc.1", "0.0.10", "0.1.0", "1.0.0"] {
|
||||
let d = group_isolation_document(Some(version), "", depth);
|
||||
assert_eq!(d.layers[1].blend, BlendMode::Normal, "{version}, {depth:?}");
|
||||
assert_eq!(photocraft_compose::flatten(&d).px, [[0.0, 1.0, 0.0, 1.0]], "{version}, {depth:?}");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn explicit_group_isolation_preserves_isolate_and_auto() {
|
||||
for depth in [SampleType::U8, SampleType::U16] {
|
||||
for version in ["0.0.3", "0.0.5", "0.0.6"] {
|
||||
for (attributes, blend, expected) in [
|
||||
("isolation=' isolate '", BlendMode::Normal, [0.0, 1.0, 0.0, 1.0]),
|
||||
("isolation=' auto '", BlendMode::PassThrough, [0.0, 0.0, 0.0, 1.0]),
|
||||
("isolation='isolate' composite-op='svg:multiply'", BlendMode::Multiply, [0.0, 0.0, 0.0, 1.0]),
|
||||
] {
|
||||
let d = group_isolation_document(Some(version), attributes, depth);
|
||||
assert_eq!(d.layers[1].blend, blend, "{version}, {attributes}, {depth:?}");
|
||||
assert_eq!(photocraft_compose::flatten(&d).px, [expected], "{version}, {attributes}, {depth:?}");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn auto_group_ignores_its_composite_op_since_version_0_0_6() {
|
||||
for depth in [SampleType::U8, SampleType::U16] {
|
||||
let d = group_isolation_document(Some("0.0.6"), "isolation='auto' composite-op='svg:screen'", depth);
|
||||
assert_eq!(d.layers[1].blend, BlendMode::PassThrough);
|
||||
assert_eq!(photocraft_compose::flatten(&d).px, [[0.0, 0.0, 0.0, 1.0]], "{depth:?}");
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn legacy_group_isolation_keeps_inference_from_blend_mode() {
|
||||
for depth in [SampleType::U8, SampleType::U16] {
|
||||
for version in [None, Some("0.0.3"), Some("0.0.5"), Some("unknown"), Some("0.0"), Some("0.0.6.0")] {
|
||||
for (attributes, blend, expected) in [
|
||||
("", BlendMode::PassThrough, [0.0, 0.0, 0.0, 1.0]),
|
||||
("composite-op='svg:screen'", BlendMode::Screen, [1.0, 1.0, 0.0, 1.0]),
|
||||
("isolation='auto' composite-op='svg:screen'", BlendMode::Screen, [1.0, 1.0, 0.0, 1.0]),
|
||||
] {
|
||||
let d = group_isolation_document(version, attributes, depth);
|
||||
assert_eq!(d.layers[1].blend, blend, "{version:?}, {attributes}, {depth:?}");
|
||||
assert_eq!(photocraft_compose::flatten(&d).px, [expected], "{version:?}, {attributes}, {depth:?}");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn reads_other_applications_stacks() {
|
||||
// The shapes Krita, MyPaint and GIMP write: a root stack with attributes, `auto`
|
||||
|
||||
+10
-2
@@ -12,7 +12,7 @@ entry, so an OpenRaster file with another extension still opens as one.
|
||||
|---|---|---|
|
||||
| Pixel layers (any size and position, also off-canvas) | yes | yes, cropped to their pixels |
|
||||
| Groups, nested | yes | yes |
|
||||
| Pass-through groups | `isolation="auto"` without a blend mode | `isolation="auto"` |
|
||||
| Pass-through groups | `isolation="auto"` (before 0.0.6, only without a blend mode) | `isolation="auto"` |
|
||||
| Names, visibility, opacity | yes | yes |
|
||||
| Blend modes | the `svg:*` names, `svg:plus`, and Krita's `krita:*` names | `svg:*` where the spec has the mode, else `krita:*` (read back by Krita, Normal elsewhere) |
|
||||
| Locks | `edit-locked`, `alpha-preserve` | the same |
|
||||
@@ -20,6 +20,12 @@ entry, so an OpenRaster file with another extension still opens as one.
|
||||
| Resolution | `xres` | `xres` and `yres` |
|
||||
| Merged image and thumbnail | not read (we render our own) | written (`mergedimage.png`, `Thumbnails/thumbnail.png`) |
|
||||
|
||||
Files declaring OpenRaster 0.0.6 or newer default to isolated groups; an explicit
|
||||
`isolation="auto"` passes through regardless of the group's own composite operation. Older
|
||||
files and files without a recognized version keep the legacy inference from the group's
|
||||
blend mode. Pass-through group opacity still uses PhotoCraft's interpolation against the
|
||||
backdrop, which can differ from 0.0.6's per-child opacity multiplication for overlapping children.
|
||||
|
||||
Saving reports what OpenRaster can't hold, layer by layer: pixel masks are applied to the
|
||||
layer's pixels, fill opacity is folded into the pixels, text, shape, fill and smart object layers
|
||||
are written as their rendered pixels, and adjustment layers, layer styles, vector masks,
|
||||
@@ -53,7 +59,9 @@ are covered by tests.
|
||||
|
||||
Unit tests (`crates/io/src/ora/tests.rs`) round-trip layers, groups, pass-through, offsets,
|
||||
opacity, visibility, locks and every blend mode at 8 and 16 bit, check masks, rendered layer
|
||||
kinds, the archive layout and malformed input. `crates/automation/tests/openraster.rs` saves,
|
||||
kinds, the archive layout and malformed input. Synthetic 8/16-bit pixel fixtures check modern
|
||||
and legacy group-isolation defaults, explicit isolation and pass-through.
|
||||
`crates/automation/tests/openraster.rs` saves,
|
||||
reopens, edits and saves in place through the agent surface, and checks that a document with an
|
||||
adjustment layer never overwrites its `.ora`; the CLI test converts both ways.
|
||||
|
||||
|
||||
Reference in new issue
Block a user