按组蒙版范围导出多页画布的纯色填充像素
This commit is contained in:
parent
d52a1d0f4c
commit
19d2c8bf3b
@ -1,7 +1,8 @@
|
||||
//! [`Document`] → PSD/PSB.
|
||||
|
||||
use photocraft_color::{ColorMode, PixelFormat, SampleType};
|
||||
use photocraft_doc::{Document, Layer, LayerContent, LayerMask, SmartContentsId};
|
||||
use photocraft_doc::{Document, Fill, Layer, LayerContent, LayerMask, SmartContentsId};
|
||||
use photocraft_geom::Rect;
|
||||
use photocraft_psd::file::{GlobalLayerMask, LayerInfoPlacement};
|
||||
use photocraft_psd::layer::{BlendingRanges, ChannelData, LayerFlags, LayerInfo, LayerMask as PsdMask, MaskData, MaskParameters};
|
||||
use photocraft_psd::resources::{ImageResource, ResolutionInfo, ids, version_info_resource};
|
||||
@ -968,18 +969,37 @@ impl Ex {
|
||||
}
|
||||
|
||||
/// Pixels for a fill layer: Photoshop's cached rendering while valid,
|
||||
/// otherwise our own rendering of the fill over the canvas.
|
||||
fn fill_pixels(&self, l: &Layer, f: &photocraft_doc::Fill) -> Surface {
|
||||
/// otherwise our own rendering of the fill.
|
||||
fn fill_pixels(&self, l: &Layer, f: &Fill, group_clip: Rect) -> (Surface, bool) {
|
||||
if let Some(c) = &l.fill_cache
|
||||
&& c.fill == *f
|
||||
{
|
||||
return c.surface.clone();
|
||||
return (c.surface.clone(), false);
|
||||
}
|
||||
if let Fill::Solid(color) = f
|
||||
&& !photocraft_compose::effects::has_effects(l)
|
||||
{
|
||||
// Keep full pixel data where the fill uses alpha beyond the standard solid
|
||||
// color descriptor.
|
||||
let rect = if color.alpha != 1.0 || group_clip.is_empty() { self.canvas } else { group_clip };
|
||||
let clipped_cache = rect != self.canvas;
|
||||
if self.cmyk
|
||||
&& let Some(surface) = photocraft_compose::gradient_fill::render_cmyk_fill(f, rect, self.canvas, self.fmt.sample)
|
||||
{
|
||||
return (surface, clipped_cache);
|
||||
}
|
||||
let rgb = color.to_rgb();
|
||||
let samples = photocraft_raster::from_rgba(&self.fmt, [rgb[0], rgb[1], rgb[2], color.alpha]);
|
||||
let mut surface = Surface::new(self.fmt);
|
||||
surface.fill_rect(rect, &samples);
|
||||
surface.prune();
|
||||
return (surface, clipped_cache);
|
||||
}
|
||||
if self.cmyk
|
||||
&& let Some(s) =
|
||||
photocraft_compose::gradient_fill::render_cmyk_fill(f, self.canvas, photocraft_compose::fill_frame(l, self.canvas), self.fmt.sample)
|
||||
{
|
||||
return s;
|
||||
return (s, false);
|
||||
}
|
||||
// In the frame the layer's masks give it, like the compositor (masks are stored apart).
|
||||
// Readers composite these pixels (ours keeps them as the fill's rendering), so a pattern
|
||||
@ -989,12 +1009,12 @@ impl Ex {
|
||||
let vals: Vec<f32> = buf.px.iter().flat_map(|p| photocraft_raster::from_rgba(&self.fmt, *p)).collect();
|
||||
s.write_region(self.canvas, &vals);
|
||||
s.prune();
|
||||
s
|
||||
(s, false)
|
||||
}
|
||||
|
||||
/// Write the records of `layers` (and their groups' children). `tick` runs after each one:
|
||||
/// it reports progress and returns `false` to stop early (a cancelled save, #3389).
|
||||
fn emit(&mut self, layers: &[Layer], tick: &mut dyn FnMut() -> bool) {
|
||||
fn emit(&mut self, layers: &[Layer], group_clip: Rect, tick: &mut dyn FnMut() -> bool) {
|
||||
for l in layers {
|
||||
let extra = self.content_blocks(l);
|
||||
match &l.content {
|
||||
@ -1011,7 +1031,21 @@ impl Ex {
|
||||
],
|
||||
..Default::default()
|
||||
});
|
||||
self.emit(&g.children, tick);
|
||||
// A hard vector mask bounds the visible fill pixels of every child.
|
||||
let clip = if photocraft_compose::effects::has_effects(l) {
|
||||
self.canvas
|
||||
} else if let Some(vm) = &l.vector_mask
|
||||
&& vm.enabled
|
||||
&& !vm.path.inverted
|
||||
&& vm.density == 1.0
|
||||
&& vm.feather == 0.0
|
||||
&& let Some((x0, y0, x1, y1)) = vm.path.control_bounds()
|
||||
{
|
||||
group_clip.intersect(&Rect::new(x0.floor() as i32, y0.floor() as i32, x1.ceil() as i32, y1.ceil() as i32))
|
||||
} else {
|
||||
group_clip
|
||||
};
|
||||
self.emit(&g.children, clip, tick);
|
||||
let kind = if g.expanded { SectionType::OpenFolder } else { SectionType::ClosedFolder };
|
||||
let lsct = TaggedBlock::section_divider(kind, Some(PsdBlend::from_key(l.blend.psd_key())), None);
|
||||
let r = self.record(l, std::iter::once(lsct).chain(extra).collect(), None);
|
||||
@ -1026,8 +1060,12 @@ impl Ex {
|
||||
self.records.push(r);
|
||||
}
|
||||
LayerContent::Fill(f) => {
|
||||
let px = self.fill_pixels(l, f);
|
||||
let r = self.record(l, extra, Some(&px));
|
||||
let (px, clipped_cache) = self.fill_pixels(l, f, group_clip);
|
||||
let mut r = self.record(l, extra, Some(&px));
|
||||
// Readers can composite the clipped pixels or use the editable SoCo descriptor.
|
||||
if clipped_cache {
|
||||
r.flags.set_pixel_data_irrelevant(true);
|
||||
}
|
||||
self.records.push(r);
|
||||
}
|
||||
LayerContent::Text(t) => {
|
||||
|
||||
@ -102,7 +102,7 @@ fn export_supported_document(
|
||||
!ctl.cancelled()
|
||||
};
|
||||
let space = photocraft_compose::cmyk_space(doc);
|
||||
photocraft_color::convert::with_cmyk_space(space.as_ref(), || ex.emit(&doc.layers, &mut tick));
|
||||
photocraft_color::convert::with_cmyk_space(space.as_ref(), || ex.emit(&doc.layers, doc.bounds(), &mut tick));
|
||||
drop(tick);
|
||||
ctl.progress(layers_share);
|
||||
|
||||
|
||||
@ -4,6 +4,41 @@ fn document(width: u32, height: u32) -> Document {
|
||||
Document::new("size estimate", photocraft_geom::Size::new(width, height), ColorMode::Rgb, SampleType::U8)
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn solid_fills_in_stacked_pages_store_only_their_page_pixels() {
|
||||
use photocraft_doc::{Color, Path, Subpath, VectorMask};
|
||||
|
||||
let mut doc = Document::new("pages", photocraft_geom::Size::new(12, 24), ColorMode::Rgb, SampleType::U16);
|
||||
let page = |name: &str, color, top: i32| {
|
||||
let mut group = Layer::group(name, vec![Layer::new(format!("{name} fill"), LayerContent::Fill(Fill::Solid(color)))]);
|
||||
let (y0, y1) = (f64::from(top), f64::from(top + 12));
|
||||
group.vector_mask = Some(VectorMask::new(Path::new(vec![Subpath::polygon(&[(0.0, y0), (12.0, y0), (12.0, y1), (0.0, y1)])])));
|
||||
group
|
||||
};
|
||||
doc.layers = vec![page("first", Color::rgb(1., 0., 0.), 0), page("second", Color::rgb(0., 1., 0.), 12)];
|
||||
let options = PsdExportOptions { force_psb: true, ..Default::default() };
|
||||
let (file, _) = document_to_psd_with(&doc, &options);
|
||||
let layers = file.layers();
|
||||
for (name, top) in [(b"first fill".as_slice(), 0), (b"second fill".as_slice(), 12)] {
|
||||
let record = layers.iter().find(|layer| layer.name == name).unwrap();
|
||||
assert_eq!(record.rect, PsdRect::from_xywh(0, top, 12, 12));
|
||||
assert!(record.flags.pixel_data_irrelevant());
|
||||
assert!(record.block(b"SoCo").is_some());
|
||||
}
|
||||
let mut reopened = crate::import("pages.psb", &file.to_bytes().unwrap()).unwrap().document;
|
||||
assert_eq!(reopened.depth, SampleType::U16);
|
||||
let expected = photocraft_compose::render(&doc, doc.bounds());
|
||||
assert_eq!(photocraft_compose::render(&reopened, reopened.bounds()).px, expected.px);
|
||||
reopened.layers[1].vector_mask = None;
|
||||
assert_eq!(photocraft_compose::render(&reopened, reopened.bounds()).get(6, 6), [0., 1., 0., 1.]);
|
||||
|
||||
doc.layers[1].vector_mask.as_mut().unwrap().density = 0.5;
|
||||
let file = document_to_psd(&doc);
|
||||
let second_fill = file.layers().iter().find(|layer| layer.name == b"second fill").unwrap();
|
||||
assert_eq!(second_fill.rect, PsdRect::from_xywh(0, 0, 12, 24));
|
||||
assert!(!second_fill.flags.pixel_data_irrelevant());
|
||||
}
|
||||
|
||||
/// Photoshop opens a PSD's bottom layer as its Background only when that layer has no
|
||||
/// transparency channel (measured on Photoshop 27.11: with the channel written, re-exported
|
||||
/// files reopened with the Background as an ordinary layer).
|
||||
|
||||
@ -596,8 +596,10 @@ impl Ctx<'_> {
|
||||
} else if let Some(k) = fill_key {
|
||||
match rec.block(k).and_then(|b| blocks::parse_fill(k, &b.data)) {
|
||||
Some(f) => {
|
||||
// Keep Photoshop's rendering of the fill so it composites exactly.
|
||||
if !rec.rect.is_empty() {
|
||||
// A solid fill marked as descriptor-driven stays editable outside its
|
||||
// stored preview, for example when the containing group's mask is moved.
|
||||
let descriptor_driven = matches!(f, photocraft_doc::Fill::Solid(_)) && rec.flags.bit4_useful() && rec.flags.pixel_data_irrelevant();
|
||||
if !rec.rect.is_empty() && !descriptor_driven {
|
||||
fill_cache = Some(FillCache { fill: f.clone(), surface: self.record_surface(rec, &name) });
|
||||
}
|
||||
LayerContent::Fill(f)
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user