From badaf937e3fcfd460c48d9db92f8c700a61470c8 Mon Sep 17 00:00:00 2001 From: dotCooCoo Date: Tue, 17 Nov 2026 06:01:01 -0800 Subject: [PATCH] Fix #8316: preserve native CMYK PSD composites --- crates/engine/Cargo.toml | 2 +- crates/engine/src/cmd/fileio.rs | 2 +- crates/engine/src/cmd/fileio/load.rs | 50 ++++++- crates/engine/src/cmd/fileio/psdread.rs | 42 ++++++ crates/engine/src/cmd/fileio/tests_psdread.rs | 126 ++++++++++++++++++ crates/engine/src/cmd/links.rs | 4 +- crates/engine/src/cmd/place.rs | 2 +- docs/file-format-parity.md | 9 ++ docs/mcp.md | 6 + 9 files changed, 232 insertions(+), 11 deletions(-) diff --git a/crates/engine/Cargo.toml b/crates/engine/Cargo.toml index f972cbb7b3..229a9b9764 100644 --- a/crates/engine/Cargo.toml +++ b/crates/engine/Cargo.toml @@ -47,6 +47,7 @@ vectorcraft-js = { workspace = true } vectorcraft-apischema = { workspace = true } image = { workspace = true } craft-image-codecs = { workspace = true } +tiff = { workspace = true } jxl-oxide = { workspace = true } jxl-color = { workspace = true } jxl-grid = { workspace = true } @@ -88,7 +89,6 @@ craft-jxl = { workspace = true } # vectorcraft-doc's Group 3 TIFF reader before craft-stream-codecs, the reference # tests/group3_reference.rs compares it with. hayro-ccitt = { workspace = true } -tiff = { workspace = true } craft-stream-codecs = { workspace = true } vectorcraft-affinity = { workspace = true, features = ["synth"] } crc32fast = { workspace = true } diff --git a/crates/engine/src/cmd/fileio.rs b/crates/engine/src/cmd/fileio.rs index 6817f28b35..899269ed39 100644 --- a/crates/engine/src/cmd/fileio.rs +++ b/crates/engine/src/cmd/fileio.rs @@ -39,7 +39,7 @@ pub mod jxlread; mod legacy_ai; mod load; mod mesh3d; -pub(crate) use load::{MAX_RASTER_ALLOC, file_clipped, png_clip, size_note}; +pub(crate) use load::{MAX_RASTER_ALLOC, file_clipped, image_clip, size_note}; mod heifread; mod hpgl; mod jp2read; diff --git a/crates/engine/src/cmd/fileio/load.rs b/crates/engine/src/cmd/fileio/load.rs index ff0e14c40d..4e59e378a5 100644 --- a/crates/engine/src/cmd/fileio/load.rs +++ b/crates/engine/src/cmd/fileio/load.rs @@ -60,8 +60,7 @@ impl Loaded { } } -/// An image ready to embed: PNG/JPEG/GIF/WebP keep their bytes, other formats are stored as PNG -/// (what browsers, PDF and SVG viewers show). +/// An image ready to embed. CMYK images use JPEG or TIFF; other images use browser-readable formats. #[derive(Clone)] pub struct RasterImage { /// Content key for `Document::images` (identical files share one blob). @@ -937,8 +936,9 @@ pub(crate) fn size_note(bytes: &[u8]) -> Option { /// ink are written again with their inks together ([`vectorcraft_doc::cmyk::stored_cmyk_tiff`]); /// CMYK TIFFs with an alpha channel, which the decoder can't read, become RGBA in the active color /// settings' CMYK, whichever of their extra channels is the alpha -/// ([`vectorcraft_doc::cmyk::extra_samples_tiff_rgba`]), as do CMYK Photoshop documents, whose -/// merged image is read ([`psdread`]). +/// ([`vectorcraft_doc::cmyk::extra_samples_tiff_rgba`]). Opaque CMYK PSD/PSB merged images retain +/// 8- or 16-bit inks and their CMYK ICC profile as TIFF, within the native CMYK pixel and allocation limits; +/// other CMYK composites use the RGBA decoder's allocation limit ([`psdread`]). /// 1-bit TIFFs compressed with CCITT Group 3, which the decoder can't read either, become RGBA /// too ([`vectorcraft_doc::cmyk::group3_tiff_rgba`]). /// Grayscale TIFFs with an alpha channel or other extra channels, which the decoder can't read @@ -961,14 +961,27 @@ pub(crate) fn size_note(bytes: &[u8]) -> Option { /// or TIFF keeps its inks, at the 8 or 16 bits the file has, and its CMYK ICC profile as a CMYK /// TIFF ([`vectorcraft_doc::cmyk::turned_cmyk`]). An image the decoder reads and that is stored as /// PNG keeps its gray or RGB samples at 8 or 16 bits, and the gray or RGB ICC profile they are in; -/// other samples become 8-bit RGBA. A Photoshop document's PNG keeps the clipping path the file -/// names ([`png_with_clip`]). +/// other samples become 8-bit RGBA. A PSD/PSB composite's stored image keeps its imported +/// clipping path ([`image_clip`]). pub fn raster_image(bytes: &[u8]) -> Result { let ppi = super::ppi::resolution(bytes); if let Some((w, h)) = vectorcraft_doc::cmyk::oversized_tiff(bytes) { return Err(err(format!("an image of {w} × {h} pixels is too large to draw"))); } if psdread::is_psd(bytes) { + if let Some(mut native) = psdread::decode_cmyk(bytes, MAX_RASTER_ALLOC).map_err(err)? { + native.meta.dpi = ppi.map(|(x, y)| (x as f32, y as f32)); + let clip = psdread::clipping_path(bytes).and_then(|path| { + let json = serde_json::to_string(&path).ok().filter(|j| j.len() <= MAX_PNG_CLIP)?; + native.meta.text.push(("Description".into(), format!("{CLIP_KEYWORD}\n{json}"))); + Some((path, vectorcraft_geom::FillRule::EvenOdd)) + }); + let data = + craft_image_codecs::encode(&native, craft_image_codecs::Format::Tiff, &craft_image_codecs::EncodeOptions::default()).map_err(err)?; + let mut img = stored(data, "image/tiff", native.dimensions(), ppi)?; + img.clip = clip; + return Ok(img); + } let cms = vectorcraft_color::cms::active(); let img = as_png(psdread::decode(bytes, MAX_RASTER_ALLOC, |c| cms.cmyk_to_srgb(c, false)).map_err(err)?, None, ppi)?; return Ok(png_with_clip(img, psdread::clipping_path(bytes))); @@ -1173,6 +1186,31 @@ pub(crate) fn png_clip(png: &[u8]) -> Option { vectorcraft_geom::path::with_anchor_budget(vectorcraft_geom::path::MAX_READ_ANCHORS, || serde_json::from_slice(json).ok()) } +/// The imported clipping-path marker in a stored PNG or CMYK TIFF, within the path budget. +pub(crate) fn image_clip(blob: &ImageBlob) -> Option { + if blob.mime == "image/png" { + return png_clip(&blob.bytes); + } + if blob.mime != "image/tiff" { + return None; + } + use tiff::tags::{Tag, Type, ValueBuffer}; + let mut d = tiff::decoder::Decoder::new(std::io::Cursor::new(blob.bytes.as_slice())).ok()?; + let mut ifd = d.image_ifd(); + let entry = ifd.find_entry(Tag::ImageDescription)?; + if entry.field_type() != Type::ASCII || entry.count() > (MAX_PNG_CLIP + CLIP_KEYWORD.len() + 2) as u64 { + return None; + } + let mut buf = ValueBuffer::empty(Type::ASCII); + ifd.find_tag_buf(Tag::ImageDescription, &mut buf).ok()??; + let json = buf.as_bytes().strip_prefix(CLIP_KEYWORD.as_bytes())?.strip_prefix(b"\n")?; + let json = json.strip_suffix(b"\0").unwrap_or(json); + if json.len() > MAX_PNG_CLIP { + return None; + } + vectorcraft_geom::path::with_anchor_budget(vectorcraft_geom::path::MAX_READ_ANCHORS, || serde_json::from_slice(json).ok()) +} + /// An image as a document of its physical size at the resolution it declares (as Place sizes it; /// 72 ppi, 1 px = 1 pt, when it declares none), the image named after the file. pub(super) fn raster_doc(name: &str, bytes: &[u8]) -> Result { diff --git a/crates/engine/src/cmd/fileio/psdread.rs b/crates/engine/src/cmd/fileio/psdread.rs index 8571a19e4e..0b421e6c90 100644 --- a/crates/engine/src/cmd/fileio/psdread.rs +++ b/crates/engine/src/cmd/fileio/psdread.rs @@ -500,6 +500,48 @@ pub(super) fn to_rgb(h: &Header<'_>, v: impl Fn(usize) -> f32, cmyk: &impl Fn([f } } +/// An opaque CMYK composite as its original 8- or 16-bit ink samples. Other modes, +/// composites with transparency, and images beyond the native CMYK pixel or allocation limit return `None`. +pub(super) fn decode_cmyk(bytes: &[u8], max_alloc: u64) -> Result, String> { + use craft_image_codecs::{ChannelLayout, Image, SampleType}; + let mut r = Reader::new(bytes); + let h = header(&mut r)?; + if h.mode != Mode::Cmyk || (h.channels > 4 && merged_alpha(h.layers, h.psb)) { + return Ok(None); + } + let pixels = u64::from(h.width) * u64::from(h.height); + if pixels > vectorcraft_doc::cmyk::MAX_CMYK_PIXELS { + return Ok(None); + } + let sample_bytes = usize::from(h.depth / 8); + let samples = pixels * 4 * sample_bytes as u64; + let icc = resources(h.resources, false) + .find(|(id, _, _)| *id == 1039) + .map(|(_, _, data)| data) + .filter(|data| data.len() <= 16 << 20 && vectorcraft_doc::cmyk::icc_color_space(data) == Some(*b"CMYK")); + if samples.saturating_mul(2).saturating_add(icc.map_or(0, |p| p.len() as u64)) > max_alloc { + return Ok(None); + } + let count = pixels as usize; + let data = channel_data(r.rest(), &h, 4, row_bytes(h.width as usize, h.depth))?; + let mut out = Vec::with_capacity(samples as usize); + for i in 0..count { + for channel in 0..4 { + let at = (channel * count + i) * sample_bytes; + if h.depth == 8 { + out.push(255 - data.get(at).copied().ok_or(TRUNCATED)?); + } else { + let pair = data.get(at..at + 2).ok_or(TRUNCATED)?.try_into().map_err(|_| TRUNCATED)?; + out.extend((65535 - u16::from_be_bytes(pair)).to_ne_bytes()); + } + } + } + let sample = if h.depth == 16 { SampleType::U16 } else { SampleType::U8 }; + let mut image = Image::from_raw(h.width, h.height, ChannelLayout::Cmyk, sample, out).map_err(|e| e.to_string())?; + image.icc = icc.map(<[u8]>::to_vec); + Ok(Some(image)) +} + /// A Photoshop document's merged image as straight RGBA. CMYK converts through `cmyk` (ink /// amounts 0..1 → sRGB). Fails, without allocating, when the image would need more than /// `max_alloc` bytes. diff --git a/crates/engine/src/cmd/fileio/tests_psdread.rs b/crates/engine/src/cmd/fileio/tests_psdread.rs index ac728c8cb9..249a3f320c 100644 --- a/crates/engine/src/cmd/fileio/tests_psdread.rs +++ b/crates/engine/src/cmd/fileio/tests_psdread.rs @@ -97,6 +97,132 @@ fn every_colour_mode_and_depth_reads() { assert_eq!(pixels(&psd(1, 1, (1, 1), 8, 8, &[0; 40], &[], &[], &raw(&[&[64]]))), [[64, 64, 64, 255]]); } +#[test] +fn cmyk_psd_place_keeps_the_native_ink_samples() { + let f = psd(1, 4, (2, 1), 8, 4, &[], &[], &[], &raw(&[&[191, 255], &[127, 0], &[63, 127], &[223, 0]])); + let p = json!({"name": "inks.psd", "dataBase64": vectorcraft_format::base64_encode(&f)}); + let mut s = Session::new(); + s.execute("file.new", &json!({"width": 40, "height": 20})).unwrap(); + let info = s.execute("file.place.info", &p).unwrap(); + assert_eq!(info["colorMode"], "CMYK"); + let placed = s.execute("file.place", &p).unwrap(); + let id = NodeId(placed["ids"][0].as_u64().unwrap()); + let d = &s.doc().unwrap().doc; + let NodeKind::Image(im) = &d.node(id).unwrap().kind else { panic!("placed PSD is an image") }; + let blob = d.images.get(&im.key).unwrap(); + assert!(blob.is_cmyk()); + assert_eq!(blob.cmyk().unwrap().data, [64, 128, 192, 32, 0, 255, 128, 255]); +} + +#[test] +fn cmyk_psd_native_budget_keeps_the_rgba_fallback_available() { + let bytes = psd(1, 4, (2, 1), 16, 4, &[], &[], &[], &raw(&[&[0xff; 4], &[0xff; 4], &[0xff; 4], &[0; 4]])); + assert!(super::psdread::decode_cmyk(&bytes, 24).unwrap().is_none()); + assert_eq!(decode(&bytes, 24, naive).unwrap().into_raw(), [0, 0, 0, 255, 0, 0, 0, 255]); + assert!(decode(&bytes, 1, naive).is_err()); +} + +#[test] +fn cmyk_psd_depth_and_compression_preserve_exact_samples() { + for version in [1, 2] { + for depth in [8, 16] { + let values = if depth == 8 { [191u16, 127, 63, 223] } else { [0xcdef, 0x89ab, 0x4567, 0x0123] }; + let planes: Vec> = values.iter().map(|v| if depth == 8 { vec![*v as u8] } else { v.to_be_bytes().to_vec() }).collect(); + let uncompressed = planes.concat(); + for method in 0..=3u16 { + let mut data = method.to_be_bytes().to_vec(); + match method { + 0 => data.extend(&uncompressed), + 1 => { + for p in &planes { + let count = (p.len() + 1) as u32; + if version == 2 { data.extend(count.to_be_bytes()) } else { data.extend((count as u16).to_be_bytes()) } + } + for p in &planes { + data.push((p.len() - 1) as u8); + data.extend(p); + } + } + _ => { + // One sample per row has the same bytes with or without prediction. + let mut z = flate2::write::ZlibEncoder::new(Vec::new(), flate2::Compression::default()); + std::io::Write::write_all(&mut z, &uncompressed).unwrap(); + data.extend(z.finish().unwrap()); + } + } + let bytes = psd(version, 4, (1, 1), depth, 4, &[], &[], &[], &data); + let img = raster_image(&bytes).unwrap(); + assert!(img.blob.is_cmyk(), "version {version}, depth {depth}, compression {method}"); + assert_eq!(img.blob.is_cmyk16(), depth == 16); + let decoded = craft_image_codecs::decode(&img.blob.bytes).unwrap(); + let expected: Vec = + values.iter().flat_map(|v| if depth == 8 { vec![255 - *v as u8] } else { (65535 - v).to_ne_bytes().to_vec() }).collect(); + assert_eq!(decoded.data(), expected); + assert!(super::psdread::decode_cmyk(&bytes, 1).unwrap().is_none()); + assert!(super::psdread::decode_cmyk(&bytes[..bytes.len() - data.len() + 2], 1 << 30).is_err()); + } + } + } + let planes: [&[u8]; 5] = [&[255], &[255], &[255], &[255], &[128]]; + let saved = psd(1, 5, (1, 1), 8, 4, &[], &[], &[], &raw(&planes)); + assert!(raster_image(&saved).unwrap().blob.is_cmyk(), "a saved selection is not transparency"); + let alpha = psd(1, 5, (1, 1), 8, 4, &[], &[], &layer_count(-1, false), &raw(&planes)); + let img = raster_image(&alpha).unwrap(); + assert!(!img.blob.is_cmyk()); + assert_eq!(craft_image_codecs::decode(&img.blob.bytes).unwrap().to_rgba8()[3], 128); +} + +#[test] +fn cmyk_psd_links_keep_inks_profile_resolution_and_clipping() { + let profile = vectorcraft_color::cms::icc_bytes(vectorcraft_color::cms::GENERIC_CMYK).unwrap(); + let mut res = Vec::new(); + res.extend((144u32 << 16).to_be_bytes()); + res.extend([0, 1, 0, 1]); + res.extend((144u32 << 16).to_be_bytes()); + res.extend([0, 1, 0, 1]); + let make = |clip: &[u8], ink: u8| { + let resources = [resource(1039, b"", &profile), resource(1005, b"", &res), clip.to_vec()].concat(); + psd(1, 4, (2, 1), 8, 4, &[], &resources, &[], &raw(&[&[255 - ink; 2], &[127; 2], &[63; 2], &[223; 2]])) + }; + let dir = vectorcraft_testkit::TempDir::new("psd-cmyk-links"); + let cut = write_file(&dir, "cut.psd", &make(&triangle_resources(), 64)); + let square = write_file(&dir, "square.psd", &make(&square_resources(), 96)); + let mut s = Session::new(); + s.execute("file.new", &json!({"width": 20, "height": 10, "colorMode": "cmyk"})).unwrap(); + assert_eq!(s.execute("file.place.info", &json!({"path": cut})).unwrap()["colorMode"], "CMYK"); + let r = s.execute("file.place", &json!({"path": cut, "at": [0.5, 0.25]})).unwrap(); + let group = NodeId(r["ids"][0].as_u64().unwrap()); + let id = r["image"].as_u64().unwrap(); + assert_eq!(r["width"], 1.0); + assert_eq!(r["height"], 0.5); + let check = |s: &mut Session, ink, linked| { + let info = s.execute("links.info", &json!({"id": id})).unwrap(); + assert_eq!(info["colorMode"], "CMYK"); + assert_eq!(info["linked"], linked); + assert_eq!(info["ppi"], json!([144.0, 144.0])); + let d = &s.doc().unwrap().doc; + let NodeKind::Image(im) = &d.node(NodeId(id)).unwrap().kind else { panic!("placed image") }; + let blob = d.images.get(&im.key).unwrap(); + assert_eq!(blob.cmyk().unwrap().data, [ink, 128, 192, 32, ink, 128, 192, 32]); + assert_eq!(blob.cmyk_icc_profile().as_deref(), Some(profile.as_ref())); + assert_eq!( + super::load::image_clip(blob), + Some(clipping_path(&make(&if ink == 64 { triangle_resources() } else { square_resources() }, ink)).unwrap()) + ); + }; + check(&mut s, 64, true); + let triangle = clip_group(&s.doc().unwrap().doc, group); + s.execute("links.relink", &json!({"ids": [id], "path": square})).unwrap(); + check(&mut s, 96, true); + assert_ne!(clip_group(&s.doc().unwrap().doc, group).0, triangle.0); + std::fs::write(&square, make(&triangle_resources(), 64)).unwrap(); + s.execute("links.update", &json!({"ids": [id]})).unwrap(); + check(&mut s, 64, true); + assert_eq!(clip_group(&s.doc().unwrap().doc, group), triangle); + s.execute("links.embed", &json!({"ids": [id]})).unwrap(); + check(&mut s, 64, false); +} + #[test] fn transparency_comes_back_straight_and_extra_channels_without_it_stay_out() { // Half-transparent red, stored on white: (255, 127, 127) with alpha 128. diff --git a/crates/engine/src/cmd/links.rs b/crates/engine/src/cmd/links.rs index 3c517c006a..81d1c72497 100644 --- a/crates/engine/src/cmd/links.rs +++ b/crates/engine/src/cmd/links.rs @@ -713,12 +713,12 @@ type FileClip = (PathData, FillRule); /// The clipping path the file of image content `blob` draws it through, in its pixel space, as it /// was kept when the file was read: a DCS file's ([`ImageBlob::separated_clip`]) or a PSD or PSB -/// file's ([`fileio::png_clip`]). +/// file's ([`fileio::image_clip`]). fn file_clip(blob: &ImageBlob) -> Option { if let Some(path) = blob.separated_clip() { return Some((path, FillRule::NonZero)); } - (blob.mime == "image/png").then(|| fileio::png_clip(&blob.bytes)).flatten().map(|path| (path, FillRule::EvenOdd)) + fileio::image_clip(blob).map(|path| (path, FillRule::EvenOdd)) } /// What happens to the clip of an object a link update remakes. diff --git a/crates/engine/src/cmd/place.rs b/crates/engine/src/cmd/place.rs index 85d2f769fc..486a5633a7 100644 --- a/crates/engine/src/cmd/place.rs +++ b/crates/engine/src/cmd/place.rs @@ -91,7 +91,7 @@ pub fn specs() -> Vec { "Placed File Info", [], None, - "{path | name | dataBase64 (a file's path, or its name with its bytes), page?, crop?, password?, thumbnail?: px, link?: bool, dxf?: {…}, layers?, hiddenLayers?, importSlices? (all as file.place)} what file.place would place, without placing it → {name, format, width, height (pt at 100%), pixelWidth?, pixelHeight?, ppi?: [x, y], colorMode?: RGB|Grayscale|CMYK (raster images), plates?: [ink names] (a DCS file), slices?: the user slices a PSD or PSB file would bring in, warnings, thumbnailBase64?: PNG of at most `thumbnail` (≤ 512) px on its longer side}", + "{path | name | dataBase64 (a file's path, or its name with its bytes), page?, crop?, password?, thumbnail?: px, link?: bool, dxf?: {…}, layers?, hiddenLayers?, importSlices? (all as file.place)} what file.place would place, without placing it → {name, format, width, height (pt at 100%), pixelWidth?, pixelHeight?, ppi?: [x, y], colorMode?: RGB|Grayscale|CMYK (raster images; opaque CMYK PSD/PSB merged images keep 8- or 16-bit inks within VectorCraft's 67,108,864-pixel native CMYK limit and allocation allowance; other CMYK composites use the RGB decoder's allocation limit), plates?: [ink names] (a DCS file), slices?: the user slices a PSD or PSB file would bring in, warnings, thumbnailBase64?: PNG of at most `thumbnail` (≤ 512) px on its longer side}", always, info ), diff --git a/docs/file-format-parity.md b/docs/file-format-parity.md index 8ac21ef37f..406a0a0c93 100644 --- a/docs/file-format-parity.md +++ b/docs/file-format-parity.md @@ -337,6 +337,15 @@ Names in the editing data that aren't UTF-8 (layers, objects, artboards, colors, | Affinity `.af`, `.afdesign`, `.afpub` | no | yes (beyond Illustrator) | no (#665) | 51 pinned files, [affinity-validation.md](affinity-validation.md) | | Clipboard: PDF, SVG, PNG, EMF, text | yes | yes | yes | copied files paste as art; cells copied from a spreadsheet (tab-separated text) paste as a table of point type over gridlines, at most 1000 rows, 256 columns and 20,000 cells (#5296) | +Opaque CMYK PSD/PSB merged images preserve their 8-bit or 16-bit ink samples, CMYK ICC profile, resolution, +and clipping path through placement, relinking, updating, and embedding (#8316). VectorCraft stores these images +as CMYK TIFFs up to its native limit of 67,108,864 pixels and within its allocation allowance. Other CMYK composites +use the RGB import path, subject to its allocation limit; importing individual layers keeps its existing behavior. +Synthetic regressions in +`tests_psdread.rs` cover native samples, compression, metadata, and linked-image operations. The +[public PSD specification](https://www.adobe.com/devnet-apps/photoshop/fileformatashtml/index.html) defines +the CMYK planes and profile and resolution resources. + ## PDF form gradients PDF export's linear and radial gradients stay aligned inside transparency groups, opacity masks, diff --git a/docs/mcp.md b/docs/mcp.md index a27f99feba..d03e215793 100644 --- a/docs/mcp.md +++ b/docs/mcp.md @@ -7582,6 +7582,12 @@ they can't be read, as with a PNG cut short by a download or a sync still runnin can't be read" and places nothing; `clipboard.importImage` fails the same way. A JPEG cut short is placed when its pixels still decode. Both keep the decoded pixels (except a CMYK JPEG's) in the image store the renderers share, so the first render that draws the image does not decode the file again. +Opaque CMYK PSD/PSB merged images retain their 8-bit or 16-bit ink samples, CMYK ICC profile, resolution, and +clipping path when placed, linked, relinked, updated, or embedded. `file.place.info`, `image.info`, and `links.info` +report CMYK for these images. VectorCraft's native CMYK limit is 67,108,864 pixels. Composites with transparency, +images beyond that limit, and native samples beyond the allocation allowance use the RGB import path, subject to +its allocation limit. Importing individual layers with `layers: true` retains its existing behavior. + `file.place.info` describes a file without placing it and `image.info` reports a placed image's link, colour mode and effective ppi. `file.place.queue` loads the place cursor (the `place` tool) with several files: headless, drive it with `pointer_gesture` (a click places at 100% with the top-left corner there, a drag at the dragged size) and