mirror of
https://github.com/storytold/photocraft.git
synced 2026-12-03 06:31:42 +01:00
Write and verify Count groups in the shared layerless PSD exporter
This commit is contained in:
1 parent
9452f31733
commit
2bd2ced6e6
4 files changed
+82
-28
No files matched your search
@@ -396,18 +396,26 @@ pub(crate) fn import_count_resource(doc: &mut Document) {
|
||||
}
|
||||
}
|
||||
|
||||
/// Resource 1080 from the current groups, including groups with no markers yet.
|
||||
pub(crate) fn fresh_count_resource(doc: &Document) -> Option<ImageResource> {
|
||||
/// Resource 1080 from the current groups, warning when PSD cannot represent a size.
|
||||
pub(crate) fn fresh_count_resource(doc: &Document, warnings: &mut Vec<String>) -> Option<ImageResource> {
|
||||
if doc.measurement.count_groups.is_empty() {
|
||||
return None;
|
||||
}
|
||||
let groups = doc.measurement.count_groups.iter().map(count_group_descriptor).collect();
|
||||
let groups = doc.measurement.count_groups.iter().map(|group| count_group_descriptor(group, warnings)).collect();
|
||||
let d = Descriptor::new("Cnt ").with("Vrsn", Value::Integer(1)).with("countGroupList", Value::List(groups));
|
||||
Some(ImageResource::new(COUNT_INFO, VersionedDescriptor::new(d).to_bytes()))
|
||||
}
|
||||
|
||||
/// Write the measured group layout and preserve marker order.
|
||||
fn count_group_descriptor(group: &CountGroup) -> Value {
|
||||
fn count_group_descriptor(group: &CountGroup, warnings: &mut Vec<String>) -> Value {
|
||||
let marker_size = group.marker_size.clamp(1, 10);
|
||||
let label_size = group.label_size.clamp(8, 72);
|
||||
if marker_size != group.marker_size || label_size != group.label_size {
|
||||
warnings.push(format!(
|
||||
"Count group {:?} sizes adjusted for PSD (marker {} to {marker_size}, label {} to {label_size})",
|
||||
group.name, group.marker_size, group.label_size
|
||||
));
|
||||
}
|
||||
let [r, g, b, _] = group.color.to_rgba8();
|
||||
let objects = group
|
||||
.points
|
||||
@@ -421,8 +429,8 @@ fn count_group_descriptor(group: &CountGroup) -> Value {
|
||||
.with("Grn ", Value::Integer(i32::from(g)))
|
||||
.with("Bl ", Value::Integer(i32::from(b)))
|
||||
.with("Nm ", Value::Text(UnicodeString::new_nul(&group.name)))
|
||||
.with("Rds ", Value::Integer(group.marker_size.clamp(1, 10) as i32))
|
||||
.with("fontSize", Value::Integer(group.label_size.clamp(8, 72) as i32))
|
||||
.with("Rds ", Value::Integer(marker_size as i32))
|
||||
.with("fontSize", Value::Integer(label_size as i32))
|
||||
.with("Vsbl", Value::Boolean(group.visible))
|
||||
.with("countObjectList", Value::List(objects)),
|
||||
)
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
use photocraft_color::{ColorMode, SampleType};
|
||||
use photocraft_doc::{Color, CountGroup, Document};
|
||||
use photocraft_doc::{Color, ColorTable, CountGroup, Document};
|
||||
use photocraft_geom::Size;
|
||||
use photocraft_psd::descriptor::{Descriptor, UnicodeString, Value, VersionedDescriptor};
|
||||
use photocraft_psd::{ColorMode as PsdMode, ImageResource, PsdFile};
|
||||
@@ -83,7 +83,9 @@ fn count_groups_round_trip_through_psd() {
|
||||
for (mode, wire_mode) in [(ColorMode::Rgb, PsdMode::Rgb), (ColorMode::Bitmap, PsdMode::Bitmap), (ColorMode::Multichannel, PsdMode::Multichannel)] {
|
||||
let mut doc = document(mode);
|
||||
doc.measurement.count_groups = groups();
|
||||
let bytes = to_psd(&doc);
|
||||
let exported = crate::export(&doc, "counts.psd", &crate::ExportOptions::default()).unwrap();
|
||||
assert!(!exported.warnings.iter().any(|warning| warning.starts_with("Count group ")));
|
||||
let bytes = exported.bytes;
|
||||
let file = PsdFile::from_bytes(&bytes).unwrap();
|
||||
assert_eq!(file.header.color_mode, wire_mode);
|
||||
let resources: Vec<_> = file.resources.iter().filter(|r| r.id == COUNT_INFO).collect();
|
||||
@@ -95,6 +97,39 @@ fn count_groups_round_trip_through_psd() {
|
||||
}
|
||||
}
|
||||
|
||||
/// Both export routes report clamped sizes and preserve the original Count groups.
|
||||
#[test]
|
||||
fn count_size_clamps_warn_without_changing_source_groups() {
|
||||
for mode in [ColorMode::Rgb, ColorMode::Bitmap] {
|
||||
let mut doc = document(mode);
|
||||
let mut original = groups();
|
||||
original[0].marker_size = 0;
|
||||
original[0].label_size = 73;
|
||||
original[1].marker_size = 11;
|
||||
original[1].label_size = 7;
|
||||
doc.measurement.count_groups = original.clone();
|
||||
|
||||
let exported = crate::export(&doc, "counts.psd", &crate::ExportOptions::default()).unwrap();
|
||||
let warnings: Vec<_> = exported.warnings.iter().filter(|warning| warning.starts_with("Count group ")).map(String::as_str).collect();
|
||||
assert_eq!(
|
||||
warnings,
|
||||
[
|
||||
"Count group \"Cells\" sizes adjusted for PSD (marker 0 to 1, label 73 to 72)",
|
||||
"Count group \"Café\" sizes adjusted for PSD (marker 11 to 10, label 7 to 8)",
|
||||
]
|
||||
);
|
||||
assert_eq!(doc.measurement.count_groups, original);
|
||||
|
||||
let reopened = crate::import("counts.psd", &exported.bytes).unwrap().document;
|
||||
let mut expected = original;
|
||||
expected[0].marker_size = 1;
|
||||
expected[0].label_size = 72;
|
||||
expected[1].marker_size = 10;
|
||||
expected[1].label_size = 8;
|
||||
assert_eq!(reopened.measurement.count_groups, expected);
|
||||
}
|
||||
}
|
||||
|
||||
/// Opaque data survives an untouched save and an add-then-delete, but current groups win.
|
||||
#[test]
|
||||
fn unreadable_count_resource_is_preserved_without_groups() {
|
||||
@@ -125,3 +160,27 @@ fn deleting_all_readable_count_groups_writes_no_resource() {
|
||||
let file = PsdFile::from_bytes(&to_psd(&doc)).unwrap();
|
||||
assert!(file.resource(COUNT_INFO).is_none());
|
||||
}
|
||||
|
||||
/// Indexed export uses the shared layerless writer and keeps exactly one Count resource.
|
||||
#[test]
|
||||
fn indexed_count_groups_round_trip_through_psd() {
|
||||
let mut doc = document(ColorMode::Indexed);
|
||||
doc.color_table = Some(ColorTable { colors: vec![[0, 0, 0], [255, 255, 255]], transparent: None });
|
||||
let opaque = vec![0, 0, 0, 16, 0];
|
||||
doc.metadata.psd_resources.push((COUNT_INFO, String::new(), std::sync::Arc::new(opaque.clone())));
|
||||
let file = PsdFile::from_bytes(&to_psd(&doc)).unwrap();
|
||||
assert_eq!(file.header.color_mode, PsdMode::Indexed);
|
||||
assert_eq!(file.resource(COUNT_INFO).unwrap().data, opaque);
|
||||
|
||||
doc.measurement.count_groups = groups();
|
||||
let bytes = to_psd(&doc);
|
||||
let file = PsdFile::from_bytes(&bytes).unwrap();
|
||||
assert_eq!(file.header.color_mode, PsdMode::Indexed);
|
||||
assert!(file.layer_info.is_none());
|
||||
let resources: Vec<_> = file.resources.iter().filter(|r| r.id == COUNT_INFO).collect();
|
||||
assert_eq!(resources.len(), 1);
|
||||
assert_eq!(resources[0].data, measured_resource(2.75));
|
||||
let reopened = crate::import("counts.psd", &bytes).unwrap().document;
|
||||
assert_eq!(reopened.color_table, doc.color_table);
|
||||
assert_eq!(reopened.measurement.count_groups, doc.measurement.count_groups);
|
||||
}
|
||||
@@ -196,6 +196,7 @@ pub(crate) fn layerless_file(
|
||||
if let Some(data) = crate::annotations_map::fresh_scale_resource(doc) {
|
||||
resources.push(ImageResource::new(crate::annotations_map::MEASUREMENT_SCALE, data));
|
||||
}
|
||||
resources.extend(crate::annotations_map::fresh_count_resource(doc, warnings));
|
||||
resources.push(version_info_resource(true));
|
||||
// Document-level tagged blocks (patterns, notes, ...) are valid without a layer section; a
|
||||
// kept layer block would describe layers the document doesn't have. Their signatures follow
|
||||
|
||||
@@ -36,25 +36,11 @@ pub(super) fn document_to_psd_nested(doc: &Document, opts: &PsdExportOptions, de
|
||||
/// Export with a caller-selected PSD size limit; the test suite lowers it to exercise promotion.
|
||||
pub(super) fn document_to_psd_sized(doc: &Document, opts: &PsdExportOptions, depth: u32, ctl: &Interrupt, size_limit: u64) -> (PsdFile, Vec<String>) {
|
||||
let layerless_limit = opts.size_limit_psb.then_some(size_limit);
|
||||
// The layerless builders do not write Count groups. With groups present, size the file
|
||||
// after adding the resource so it counts toward the PSD limit.
|
||||
let early_limit = layerless_limit.filter(|_| doc.measurement.count_groups.is_empty());
|
||||
let layerless = if doc.mode == ColorMode::Multichannel {
|
||||
Some(crate::multichannel_map::document_to_psd(doc, opts.force_psb, early_limit))
|
||||
} else {
|
||||
crate::palette_map::document_to_psd(doc, opts.force_psb, early_limit)
|
||||
};
|
||||
if let Some((mut file, mut warnings)) = layerless {
|
||||
if let Some(resource) = crate::annotations_map::fresh_count_resource(doc) {
|
||||
file.resources.push(resource);
|
||||
if let Some(limit) = layerless_limit
|
||||
&& let Some(warning) = fit_psd_size(&mut file, limit)
|
||||
{
|
||||
warnings.push(warning);
|
||||
sign_global_blocks(&mut file);
|
||||
}
|
||||
}
|
||||
return (file, warnings);
|
||||
if doc.mode == ColorMode::Multichannel {
|
||||
return crate::multichannel_map::document_to_psd(doc, opts.force_psb, layerless_limit);
|
||||
}
|
||||
if let Some(file) = crate::palette_map::document_to_psd(doc, opts.force_psb, layerless_limit) {
|
||||
return file;
|
||||
}
|
||||
let fmt = doc.pixel_format();
|
||||
export_supported_document(doc, opts, depth, ctl, fmt, size_limit)
|
||||
@@ -249,7 +235,7 @@ fn export_supported_document(
|
||||
if let Some(data) = crate::annotations_map::fresh_scale_resource(doc) {
|
||||
resources.push(ImageResource::new(crate::annotations_map::MEASUREMENT_SCALE, data));
|
||||
}
|
||||
resources.extend(crate::annotations_map::fresh_count_resource(doc));
|
||||
resources.extend(crate::annotations_map::fresh_count_resource(doc, &mut ex.warnings));
|
||||
if let Some(Some(data)) = comps_resource {
|
||||
resources.push(ImageResource::new(crate::comps_map::LAYER_COMPS, data));
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user