Skip to content

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

Open Photoshop guides stored in a JPEG's APP13 resources (#7300) (#7348)

MarekSpodBiedry2c1c6f6693ca
diff --git a/crates/codecs/src/codecs/jpeg.rs b/crates/codecs/src/codecs/jpeg.rs
index 9c3762887..a300de4bd 100644
--- a/crates/codecs/src/codecs/jpeg.rs
+++ b/crates/codecs/src/codecs/jpeg.rs
@@ -33,6 +33,8 @@ pub(crate) struct JpegMeta {
pub exif: Option<Vec<u8>>,
pub xmp: Option<String>,
pub dpi: Option<(f32, f32)>,
+ /// The APP13 Photoshop image resource blocks (`8BIM` ...), segments joined, without the header.
+ pub photoshop: Option<Vec<u8>>,
pub adobe_transform: Option<u8>,
/// (width, height, components) from the first SOFn.
pub frame: Option<(u32, u32, u8)>,
@@ -108,6 +110,7 @@ pub(crate) fn scan_metadata(b: &[u8]) -> JpegMeta {
.or_else(|| m.xmp.as_deref().and_then(xmp_resolution))
.or_else(|| m.exif.as_deref().and_then(exif_resolution))
.or(jfif_dpi);
+ m.photoshop = Some(photoshop).filter(|p| !p.is_empty());
if !icc_parts.is_empty() {
icc_parts.sort_by_key(|p| p.0);
let total = icc_parts[0].1 as usize;
@@ -226,7 +229,7 @@ pub(crate) fn decode(bytes: &[u8], limits: &Limits) -> Result<Image, CodecError>
}
let mut img = Image::from_raw(w, h, layout, SampleType::U8, px)?;
img.icc = meta.icc;
- img.meta = Metadata { exif: meta.exif, xmp: meta.xmp, dpi: meta.dpi, ..Default::default() };
+ img.meta = Metadata { exif: meta.exif, xmp: meta.xmp, dpi: meta.dpi, photoshop_resources: meta.photoshop, ..Default::default() };
if end == DataEnd::Truncated {
img.warnings.push(DecodeWarning::Truncated { format: F });
}
diff --git a/crates/io/src/flat.rs b/crates/io/src/flat.rs
index 674c60561..773e69272 100644
--- a/crates/io/src/flat.rs
+++ b/crates/io/src/flat.rs
@@ -241,6 +241,11 @@ pub(crate) fn image_to_document(name: &str, img: &Image) -> Result<ImportResult,
doc.resolution_dpi = x;
warnings.extend(crate::unequal_resolution_warning(f64::from(x), f64::from(y)));
}
+ // Guides in the Photoshop resources a JPEG (APP13) or TIFF carries: resource 1032.
+ let guides = img.meta.photoshop_resources.as_deref().and_then(|r| photocraft_psd::tiff::resources_from_bytes(r).ok());
+ if let Some(g) = guides.as_ref().and_then(|(res, _)| res.iter().find(|r| r.id == 1032)) {
+ crate::psd_import::parse_guides(&g.data, &mut doc);
+ }
Ok(ImportResult { document: doc, warnings, source_read_only: false, preview_only: false })
}
diff --git a/crates/io/src/psd_import.rs b/crates/io/src/psd_import.rs
index 12307f32b..c224659c6 100644
--- a/crates/io/src/psd_import.rs
+++ b/crates/io/src/psd_import.rs
@@ -686,7 +686,7 @@ fn write_rgba8(s: &mut Surface, canvas: Rect, data: &[u8]) {
}
}
-fn parse_guides(data: &[u8], doc: &mut Document) {
+pub(crate) fn parse_guides(data: &[u8], doc: &mut Document) {
let rd = |at: usize| data.get(at..at + 4).map(|b| u32::from_be_bytes([b[0], b[1], b[2], b[3]]));
let Some(count) = rd(12) else { return };
for i in 0..count as usize {
diff --git a/crates/io/tests/flat.rs b/crates/io/tests/flat.rs
index 8cdecde36..edaff2ec8 100644
--- a/crates/io/tests/flat.rs
+++ b/crates/io/tests/flat.rs
@@ -690,6 +690,37 @@ fn truncated_jpeg_imports_with_a_warning() {
}
}
+/// Guides stored by Photoshop in a JPEG's APP13 resources (resource 1032) open as document guides.
+#[test]
+fn jpeg_photoshop_guides_open_as_document_guides() {
+ let full = export(&smooth(ColorMode::Rgb), "jpg", &ExportOptions::default()).unwrap().bytes;
+ assert!(import("x.jpg", &full).unwrap().document.guides.vertical.is_empty());
+ // Version, grid 576 x 576, two guides in 1/32 px: vertical at 10, horizontal at 5.
+ let mut guides = Vec::new();
+ for v in [1u32, 576, 576, 2] {
+ guides.extend_from_slice(&v.to_be_bytes());
+ }
+ guides.extend_from_slice(&(10i32 * 32).to_be_bytes());
+ guides.push(0);
+ guides.extend_from_slice(&(5i32 * 32).to_be_bytes());
+ guides.push(1);
+ let mut res = b"8BIM".to_vec();
+ res.extend_from_slice(&1032u16.to_be_bytes());
+ res.extend_from_slice(&[0, 0]);
+ res.extend_from_slice(&(guides.len() as u32).to_be_bytes());
+ res.extend_from_slice(&guides);
+ let mut seg = b"Photoshop 3.0\0".to_vec();
+ seg.extend_from_slice(&res);
+ let mut jpeg = full[..2].to_vec();
+ jpeg.extend_from_slice(&[0xFF, 0xED]);
+ jpeg.extend_from_slice(&((seg.len() + 2) as u16).to_be_bytes());
+ jpeg.extend_from_slice(&seg);
+ jpeg.extend_from_slice(&full[2..]);
+ let g = import("x.jpg", &jpeg).unwrap().document.guides;
+ assert_eq!(g.vertical, vec![10.0]);
+ assert_eq!(g.horizontal, vec![5.0]);
+}
+
/// A GIF of `frames` identical 1x1 black frames.
fn gif_frames(frames: usize) -> Vec<u8> {
let mut b = b"GIF89a\x01\0\x01\0\x80\0\0\0\0\0\xFF\xFF\xFF".to_vec();