Skip to content

PhotoCraft: An open-source, photo editor written by scratch in pure Rust

Opening a file with different horizontal and vertical resolutions says the vertical one is not kept (#1018) (#1445)

MarekSpodBiedryeb06c9e105d8
diff --git a/crates/io/src/flat.rs b/crates/io/src/flat.rs
index 5d8fc59f8..e5e3313df 100644
--- a/crates/io/src/flat.rs
+++ b/crates/io/src/flat.rs
@@ -96,8 +96,9 @@ pub(crate) fn image_to_document(name: &str, img: &Image) -> Result<ImportResult,
doc.icc_profile = img.icc.clone().map(Arc::new);
doc.metadata.exif = img.meta.exif.clone().map(Arc::new);
doc.metadata.xmp = img.meta.xmp.clone();
- if let Some((x, _)) = img.meta.dpi {
+ if let Some((x, y)) = img.meta.dpi {
doc.resolution_dpi = x;
+ warnings.extend(crate::unequal_resolution_warning(f64::from(x), f64::from(y)));
}
if !img.meta.text.is_empty() {
warnings.push(format!("{} text metadata entries are not kept in the document", img.meta.text.len()));
diff --git a/crates/io/src/lib.rs b/crates/io/src/lib.rs
index f49db88fd..bff525fef 100644
--- a/crates/io/src/lib.rs
+++ b/crates/io/src/lib.rs
@@ -94,6 +94,14 @@ pub struct ImportResult {
pub warnings: Vec<String>,
}
+/// The import note for a file whose horizontal and vertical resolutions (`x`, `y`, in pixels per
+/// inch) differ: a document has one resolution, so it keeps the horizontal one and saves it on
+/// both axes (#1018). Differences under 0.01 ppi are below what the warning can show.
+pub(crate) fn unequal_resolution_warning(x: f64, y: f64) -> Option<String> {
+ ((x - y).abs() >= 0.01)
+ .then(|| format!("the vertical resolution ({y:.2} ppi) differs from the horizontal ({x:.2} ppi); the document keeps {x:.2} ppi for both"))
+}
+
/// Result of [`export`].
#[derive(Debug, Clone)]
pub struct ExportResult {
diff --git a/crates/io/src/psd_import.rs b/crates/io/src/psd_import.rs
index 14d7a7a8f..0d1ee6b0e 100644
--- a/crates/io/src/psd_import.rs
+++ b/crates/io/src/psd_import.rs
@@ -540,8 +540,10 @@ pub fn psd_to_document_with(file: &PsdFile, ctl: &photocraft_raster::Interrupt)
match r.id {
ids::RESOLUTION_INFO => {
if let Ok(ri) = photocraft_psd::ResolutionInfo::from_bytes(&r.data) {
- let f = if ri.h_res_unit == 2 { 2.54 } else { 1.0 };
- doc.resolution_dpi = (ri.h_res() * f) as f32;
+ let ppi = |res: f64, unit: u16| if unit == 2 { res * 2.54 } else { res };
+ let (x, y) = (ppi(ri.h_res(), ri.h_res_unit), ppi(ri.v_res(), ri.v_res_unit));
+ doc.resolution_dpi = x as f32;
+ warnings.extend(crate::unequal_resolution_warning(x, y));
}
}
ids::ICC_PROFILE => doc.icc_profile = Some(Arc::new(r.data.clone())),
diff --git a/crates/io/src/tiff_layers.rs b/crates/io/src/tiff_layers.rs
index bb5770e5f..1172eb7d9 100644
--- a/crates/io/src/tiff_layers.rs
+++ b/crates/io/src/tiff_layers.rs
@@ -70,8 +70,10 @@ pub(crate) fn import_layered(name: &str, img: &Image, layers: &[u8]) -> Result<I
// The profile, resolution, XMP and EXIF are the TIFF's own tags; they win over any copy
// in the resources.
set_resource(&mut resources, ids::ICC_PROFILE, img.icc.clone());
- if let Some((x, _)) = img.meta.dpi.filter(|d| d.0 > 0.0) {
- set_resource(&mut resources, ids::RESOLUTION_INFO, Some(ResolutionInfo::from_dpi(f64::from(x)).to_bytes()));
+ if let Some((x, y)) = img.meta.dpi.filter(|d| d.0 > 0.0) {
+ // Both axes, so the PSD import notes a vertical resolution the document can't keep.
+ let ri = ResolutionInfo { v_res_fixed: ResolutionInfo::from_dpi(f64::from(y)).v_res_fixed, ..ResolutionInfo::from_dpi(f64::from(x)) };
+ set_resource(&mut resources, ids::RESOLUTION_INFO, Some(ri.to_bytes()));
}
if let Some(xmp) = &img.meta.xmp {
set_resource(&mut resources, ids::XMP, Some(xmp.as_bytes().to_vec()));
diff --git a/crates/io/tests/resolution_axes.rs b/crates/io/tests/resolution_axes.rs
new file mode 100644
index 000000000..bc069d7f3
--- /dev/null
+++ b/crates/io/tests/resolution_axes.rs
@@ -0,0 +1,92 @@
+//! A file whose horizontal and vertical resolutions differ opens at the horizontal one, and the
+//! import says the vertical one is not kept (#1018). Equal axes, the common case, open silently.
+
+mod common;
+
+use common::*;
+use photocraft_codecs::{ChannelLayout, EncodeOptions, Format, Image, Metadata, SampleType as CSample};
+use photocraft_color::{ColorMode, SampleType};
+use photocraft_io::*;
+use photocraft_psd::resources::ids;
+use photocraft_psd::testgen;
+use photocraft_psd::{Compression, ImageResource, ResolutionInfo, Version};
+
+fn resolution_notes(r: &ImportResult) -> Vec<&String> {
+ r.warnings.iter().filter(|w| w.contains("vertical resolution")).collect()
+}
+
+fn png(dpi: (f32, f32)) -> Vec<u8> {
+ let meta = Metadata { dpi: Some(dpi), ..Default::default() };
+ let img = Image::from_raw(2, 2, ChannelLayout::Rgb, CSample::U8, vec![10u8; 12]).unwrap().with_meta(meta);
+ photocraft_codecs::encode(&img, Format::Png, &EncodeOptions::default()).unwrap()
+}
+
+#[test]
+fn png_with_unequal_axes_reports_the_dropped_vertical_resolution() {
+ let r = import("x.png", &png((300.0, 150.0))).unwrap();
+ assert!((r.document.resolution_dpi - 300.0).abs() < 0.01, "{}", r.document.resolution_dpi);
+ let notes = resolution_notes(&r);
+ assert_eq!(notes.len(), 1, "{:?}", r.warnings);
+ assert!(notes[0].contains("150.01") && notes[0].contains("300.00"), "{notes:?}");
+}
+
+#[test]
+fn png_with_equal_axes_opens_silently() {
+ let r = import("x.png", &png((300.0, 300.0))).unwrap();
+ assert!((r.document.resolution_dpi - 300.0).abs() < 0.01, "{}", r.document.resolution_dpi);
+ assert!(r.warnings.is_empty(), "{:?}", r.warnings);
+}
+
+fn psd(ri: ResolutionInfo) -> Vec<u8> {
+ let mut f = testgen::small(Version::Psd, Compression::Raw);
+ f.resources.retain(|r| r.id != ids::RESOLUTION_INFO);
+ f.resources.push(ImageResource::new(ids::RESOLUTION_INFO, ri.to_bytes()));
+ f.to_bytes().unwrap()
+}
+
+#[test]
+fn psd_with_unequal_axes_reports_the_dropped_vertical_resolution() {
+ let ri = ResolutionInfo { v_res_fixed: ResolutionInfo::from_dpi(150.0).v_res_fixed, ..ResolutionInfo::from_dpi(300.0) };
+ let r = import("x.psd", &psd(ri)).unwrap();
+ assert!((r.document.resolution_dpi - 300.0).abs() < 1e-3, "{}", r.document.resolution_dpi);
+ let notes = resolution_notes(&r);
+ assert_eq!(notes.len(), 1, "{:?}", r.warnings);
+ assert!(notes[0].contains("150.00") && notes[0].contains("300.00"), "{notes:?}");
+}
+
+#[test]
+fn psd_with_equal_axes_opens_silently() {
+ let r = import("x.psd", &psd(ResolutionInfo::from_dpi(300.0))).unwrap();
+ assert!((r.document.resolution_dpi - 300.0).abs() < 1e-3, "{}", r.document.resolution_dpi);
+ assert!(resolution_notes(&r).is_empty(), "{:?}", r.warnings);
+ // Each axis is read in its own unit: 100 pixels per centimetre on both is 254 ppi on both.
+ let mut cm = ResolutionInfo::from_dpi(100.0);
+ cm.h_res_unit = 2;
+ cm.v_res_unit = 2;
+ let r = import("x.psd", &psd(cm)).unwrap();
+ assert!((r.document.resolution_dpi - 254.0).abs() < 1e-3, "{}", r.document.resolution_dpi);
+ assert!(resolution_notes(&r).is_empty(), "{:?}", r.warnings);
+}
+
+/// A layered TIFF opens through the PSD importer with the TIFF's own resolution tags.
+fn layered_tiff(dpi: (f32, f32)) -> Vec<u8> {
+ let d = gen_doc(ColorMode::Rgb, SampleType::U8, Features::PIXELS);
+ let out = export(&d, "x.tif", &ExportOptions { tiff_layers: true, ..Default::default() }).unwrap();
+ let mut img = photocraft_codecs::decode(&out.bytes).unwrap();
+ assert!(img.meta.photoshop_layers.is_some(), "layer data");
+ img.meta.dpi = Some(dpi);
+ photocraft_codecs::encode(&img, Format::Tiff, &EncodeOptions::default()).unwrap()
+}
+
+#[test]
+fn layered_tiff_with_unequal_axes_reports_the_dropped_vertical_resolution() {
+ let r = import("x.tif", &layered_tiff((300.0, 150.0))).unwrap();
+ assert!(r.document.layers.len() > 1, "opened with its layers");
+ assert!((r.document.resolution_dpi - 300.0).abs() < 1e-3, "{}", r.document.resolution_dpi);
+ let notes = resolution_notes(&r);
+ assert_eq!(notes.len(), 1, "{:?}", r.warnings);
+ assert!(notes[0].contains("150.00") && notes[0].contains("300.00"), "{notes:?}");
+ // Equal axes: no note.
+ let r = import("x.tif", &layered_tiff((300.0, 300.0))).unwrap();
+ assert!(resolution_notes(&r).is_empty(), "{:?}", r.warnings);
+}