diff --git a/crates/doc/src/lib.rs b/crates/doc/src/lib.rs index c286492236..8507b50754 100644 --- a/crates/doc/src/lib.rs +++ b/crates/doc/src/lib.rs @@ -55,6 +55,7 @@ mod pixels; pub use pixels::{GraySource, MAX_RASTER_ALLOC, PixelPick}; pub mod placed_document; pub mod plugin_data; +pub mod ppi; pub mod profiles; pub mod puppet; pub mod range; diff --git a/crates/doc/src/ppi.rs b/crates/doc/src/ppi.rs new file mode 100644 index 0000000000..5c3bd24b53 --- /dev/null +++ b/crates/doc/src/ppi.rs @@ -0,0 +1,287 @@ +//! Image resolution metadata (pixels per inch) in the formats the image decoder reads: PNG `pHYs`, +//! JPEG JFIF density or EXIF, TIFF and WebP EXIF resolution tags, and BMP pixels per metre. An image +//! converted to PNG keeps it as a `pHYs` chunk ([`with_png_resolution`]). + +const INCH_M: f64 = 0.0254; +const INCH_CM: f64 = 2.54; + +/// Is `bytes` a PNG, JPEG, TIFF, WebP or BMP file, the formats [`resolution`] reads? +pub fn reads(bytes: &[u8]) -> bool { + bytes.starts_with(b"\x89PNG\r\n\x1a\n") + || bytes.starts_with(&[0xff, 0xd8]) + || bytes.starts_with(b"II*\0") + || bytes.starts_with(b"MM\0*") + || (bytes.len() >= 12 && &bytes[..4] == b"RIFF" && &bytes[8..12] == b"WEBP") + || bytes.starts_with(b"BM") +} + +/// The resolution a PNG, JPEG, TIFF, WebP or BMP file declares as (horizontal, vertical) ppi, each +/// from 1 to 100,000; `None` when it declares none (or only an aspect ratio), and for other files. +pub fn resolution(bytes: &[u8]) -> Option<(f64, f64)> { + let r = if bytes.starts_with(b"\x89PNG\r\n\x1a\n") { + png(bytes) + } else if bytes.starts_with(&[0xff, 0xd8]) { + jpeg(bytes) + } else if bytes.starts_with(b"II*\0") || bytes.starts_with(b"MM\0*") { + tiff(bytes) + } else if bytes.len() >= 12 && &bytes[..4] == b"RIFF" && &bytes[8..12] == b"WEBP" { + webp(bytes) + } else if bytes.starts_with(b"BM") { + bmp(bytes) + } else { + None + }; + r.filter(|(x, y)| [*x, *y].iter().all(|v| v.is_finite() && (1.0..=100_000.0).contains(v))) +} + +/// `n` bytes at `at` (`None` past the end; offsets come from the file, so no overflow either). +fn slice(b: &[u8], at: usize, n: usize) -> Option<&[u8]> { + b.get(at..at.checked_add(n)?) +} +fn bytes(b: &[u8], at: usize) -> Option<[u8; N]> { + slice(b, at, N)?.try_into().ok() +} +fn be16(b: &[u8], at: usize) -> Option { + bytes(b, at).map(u16::from_be_bytes) +} +fn be32(b: &[u8], at: usize) -> Option { + bytes(b, at).map(u32::from_be_bytes) +} +fn le32(b: &[u8], at: usize) -> Option { + bytes(b, at).map(u32::from_le_bytes) +} + +/// Pixels per inch from the whole pixels per metre PNG and BMP store: a whole number of ppi when +/// the metre value is that rounded (300 ppi is stored as 11811 per metre, 299.9994 ppi). +fn per_metre(ppm: u32) -> f64 { + let v = ppm as f64 * INCH_M; + let whole = v.round(); + if (v - whole).abs() <= INCH_M / 2.0 + 1e-9 { whole } else { v } +} + +/// PNG chunks after the signature: (type, data). +pub fn png_chunks(b: &[u8]) -> impl Iterator { + let mut at = 8; + std::iter::from_fn(move || { + let len = be32(b, at)? as usize; + let ty = slice(b, at + 4, 4)?; + let data = slice(b, at + 8, len)?; + at = at.checked_add(12 + len)?; + Some((ty, data)) + }) +} + +fn png(b: &[u8]) -> Option<(f64, f64)> { + let (_, d) = png_chunks(b).take_while(|(ty, _)| *ty != b"IDAT").find(|(ty, _)| *ty == b"pHYs")?; + // Unit 1 = metre; 0 only gives the pixel aspect ratio. + (d.get(8) == Some(&1)).then(|| (per_metre(be32(d, 0).unwrap_or(0)), per_metre(be32(d, 4).unwrap_or(0)))) +} + +fn jpeg(b: &[u8]) -> Option<(f64, f64)> { + let (mut jfif, mut exif) = (None, None); + let mut at = 2; + while at + 4 <= b.len() && b[at] == 0xff { + let marker = b[at + 1]; + // 0xff fill bytes pad between segments. + if marker == 0xff { + at += 1; + continue; + } + if marker == 0xd8 || marker == 0x01 || (0xd0..=0xd7).contains(&marker) { + at += 2; + continue; + } + // Start of scan or end of image: no more metadata. + if marker == 0xda || marker == 0xd9 { + break; + } + let len = be16(b, at + 2)? as usize; + let seg = slice(b, at + 4, len.checked_sub(2)?)?; + match marker { + 0xe0 if seg.starts_with(b"JFIF\0") && seg.len() >= 12 => { + let (x, y) = (be16(seg, 8)? as f64, be16(seg, 10)? as f64); + jfif = match seg[7] { + 1 => Some((x, y)), + 2 => Some((x * INCH_CM, y * INCH_CM)), + _ => None, + }; + } + 0xe1 if seg.starts_with(b"Exif\0\0") => exif = tiff(&seg[6..]), + _ => {} + } + at += 2 + len; + } + jfif.or(exif) +} + +/// TIFF (or EXIF) IFD0 XResolution / YResolution / ResolutionUnit, as (horizontal, vertical) ppi +/// in the stored pixels' axes, unchecked; `None` without an absolute unit. +pub fn tiff(b: &[u8]) -> Option<(f64, f64)> { + let le = b.starts_with(b"II"); + let u16_at = |at: usize| bytes(b, at).map(if le { u16::from_le_bytes } else { u16::from_be_bytes }); + let u32_at = |at: usize| bytes(b, at).map(if le { u32::from_le_bytes } else { u32::from_be_bytes }); + let ifd = u32_at(4)? as usize; + let (mut x, mut y, mut unit) = (None, None, 2); + for i in 0..u16_at(ifd)? as usize { + let e = ifd.checked_add(2 + i * 12)?; + let rational = || -> Option { + let at = u32_at(e + 8)? as usize; + let (n, d) = (u32_at(at)?, u32_at(at.checked_add(4)?)?); + (d != 0).then(|| n as f64 / d as f64) + }; + match u16_at(e)? { + 282 => x = rational(), + 283 => y = rational(), + 296 => unit = u16_at(e + 8)?, + _ => {} + } + } + let k = match unit { + 2 => 1.0, + 3 => INCH_CM, + _ => return None, + }; + let x = x?; + Some((x * k, y.unwrap_or(x) * k)) +} + +fn webp(b: &[u8]) -> Option<(f64, f64)> { + let mut at = 12; + while at + 8 <= b.len() { + let len = le32(b, at + 4)? as usize; + let data = slice(b, at + 8, len)?; + if &b[at..at + 4] == b"EXIF" { + return tiff(data.strip_prefix(b"Exif\0\0").unwrap_or(data)); + } + at += 8 + len + (len & 1); + } + None +} + +fn bmp(b: &[u8]) -> Option<(f64, f64)> { + // BITMAPINFOHEADER (40 bytes or longer) at 14: pixels per metre at +24 and +28. + if le32(b, 14)? < 40 { + return None; + } + let ppm = |at| le32(b, at).filter(|v| (1..=i32::MAX as u32).contains(v)).map(per_metre); + Some((ppm(38)?, ppm(42)?)) +} + +/// Pixels per metre for a resolution in pixels per inch (the unit `pHYs` stores). +pub fn pixels_per_metre(ppi: f64) -> u32 { + (ppi / INCH_M).round().clamp(1.0, u32::MAX as f64) as u32 +} + +/// PNG CRC-32 (the polynomial ZIP uses) of `parts` read one after another. +fn crc32(parts: &[&[u8]]) -> u32 { + let mut crc = !0u32; + for &b in parts.iter().flat_map(|p| p.iter()) { + crc ^= u32::from(b); + for _ in 0..8 { + crc = (crc >> 1) ^ (0xEDB8_8320 & u32::from(crc & 1 != 0).wrapping_neg()); + } + } + !crc +} + +/// A whole `pHYs` chunk (length, type, data, CRC) declaring `x` × `y` pixels per inch. +pub fn phys_chunk(x: f64, y: f64) -> Vec { + let [x, y] = [x, y].map(|v| pixels_per_metre(v).to_be_bytes()); + // x, y, unit 1 = metre. + let data = [x.as_slice(), y.as_slice(), &[1]].concat(); + let mut out = Vec::with_capacity(21); + out.extend_from_slice(&9u32.to_be_bytes()); + out.extend_from_slice(b"pHYs"); + out.extend_from_slice(&data); + out.extend_from_slice(&crc32(&[b"pHYs", &data]).to_be_bytes()); + out +} + +/// `png` with a `pHYs` chunk declaring `ppi` right after the header (any existing one is replaced). +pub fn with_png_resolution(png: &[u8], (x, y): (f64, f64)) -> Vec { + let Some(head) = be32(png, 8).and_then(|len| slice(png, 0, 20usize.checked_add(len as usize)?)) else { return png.to_vec() }; + let chunk = phys_chunk(x, y); + let mut out = Vec::with_capacity(png.len() + chunk.len()); + out.extend_from_slice(head); + out.extend_from_slice(&chunk); + let mut at = head.len(); + while let Some(chunk) = be32(png, at).and_then(|len| slice(png, at, 12usize.checked_add(len as usize)?)) { + if chunk.get(4..8) != Some(b"pHYs") { + out.extend_from_slice(chunk); + } + at += chunk.len(); + } + out.extend_from_slice(png.get(at..).unwrap_or_default()); + out +} + +#[cfg(test)] +mod tests { + use super::*; + + fn png_of(w: u32, h: u32) -> Vec { + let mut out = Vec::new(); + image::RgbaImage::new(w, h).write_to(&mut std::io::Cursor::new(&mut out), image::ImageFormat::Png).unwrap(); + out + } + + /// The `pHYs` chunk carries the CRC a PNG reader checks, and replaces the one already there. + #[test] + fn a_png_takes_its_resolution_and_still_decodes() { + let png = png_of(4, 3); + assert_eq!(resolution(&png), None); + let tagged = with_png_resolution(&png, (300.0, 150.0)); + assert_eq!(resolution(&tagged), Some((300.0, 150.0))); + let again = with_png_resolution(&tagged, (72.0, 72.0)); + assert_eq!(resolution(&again), Some((72.0, 72.0)), "replaced, not duplicated"); + assert_eq!(again.len(), tagged.len()); + let img = image::load_from_memory(&again).unwrap(); + assert_eq!((img.width(), img.height()), (4, 3)); + // Every PNG ends with this IEND chunk CRC. + assert_eq!(crc32(&[b"IEND"]), 0xAE42_6082); + } + + /// RIFF chunk `ty` holding `data`, padded to an even length. + fn riff_chunk(ty: &[u8; 4], len: u32, data: &[u8]) -> Vec { + let mut out = [ty.as_slice(), &len.to_le_bytes(), data].concat(); + if data.len() % 2 == 1 { + out.push(0); + } + out + } + + fn webp_of(chunks: &[Vec]) -> Vec { + let body = chunks.concat(); + let size = u32::try_from(body.len() + 4).unwrap(); + [b"RIFF".as_slice(), &size.to_le_bytes(), b"WEBP", &body].concat() + } + + /// A WebP's EXIF chunk is found past an odd-length chunk and its padding byte, and a chunk whose + /// length runs past the end of the file (up to `u32::MAX`, more than a 32-bit `usize` can add to + /// an offset) gives no resolution. + #[test] + fn a_webp_reads_its_exif_and_rejects_a_chunk_past_the_end() { + // Little-endian TIFF: two IFD0 entries, XResolution 300/1 at offset 38 and ResolutionUnit inch. + let mut tiff = b"II*\0".to_vec(); + tiff.extend_from_slice(&8u32.to_le_bytes()); + tiff.extend_from_slice(&2u16.to_le_bytes()); + for (tag, ty, value) in [(282u16, 5u16, 38u32), (296, 3, 2)] { + tiff.extend_from_slice(&tag.to_le_bytes()); + tiff.extend_from_slice(&ty.to_le_bytes()); + tiff.extend_from_slice(&1u32.to_le_bytes()); + tiff.extend_from_slice(&value.to_le_bytes()); + } + tiff.extend_from_slice(&0u32.to_le_bytes()); + tiff.extend_from_slice(&300u32.to_le_bytes()); + tiff.extend_from_slice(&1u32.to_le_bytes()); + let exif = [b"Exif\0\0".as_slice(), &tiff].concat(); + let exif_len = u32::try_from(exif.len()).unwrap(); + let ok = webp_of(&[riff_chunk(b"VP8X", 3, &[0, 0, 0]), riff_chunk(b"EXIF", exif_len, &exif)]); + assert_eq!(resolution(&ok), Some((300.0, 300.0))); + + for len in [u32::MAX, u32::MAX - 7, 1_000] { + let bad = webp_of(&[riff_chunk(b"VP8X", len, &[0; 10])]); + assert_eq!(resolution(&bad), None, "chunk length {len}"); + } + } +} diff --git a/crates/engine/src/cmd/fileio/ppi.rs b/crates/engine/src/cmd/fileio/ppi.rs index 6998d86b28..119201bb87 100644 --- a/crates/engine/src/cmd/fileio/ppi.rs +++ b/crates/engine/src/cmd/fileio/ppi.rs @@ -3,26 +3,22 @@ //! header's HDpi and VDpi, JP2 capture and display resolution boxes, or a HEIF image's EXIF //! resolution tags. Place //! sizes an image by it (72 ppi when the file has none); images converted to PNG on import keep it -//! as a `pHYs` chunk. +//! as a `pHYs` chunk. The formats the image decoder reads are read by [`vectorcraft_doc::ppi`]. + +pub(super) use vectorcraft_doc::ppi::png_chunks; +use vectorcraft_doc::ppi::tiff; +pub use vectorcraft_doc::ppi::with_png_resolution; const INCH_M: f64 = 0.0254; -const INCH_CM: f64 = 2.54; /// The resolution a file declares as (horizontal, vertical) ppi; `None` when it declares none (or /// only an aspect ratio). pub fn resolution(bytes: &[u8]) -> Option<(f64, f64)> { - let r = if bytes.starts_with(b"\x89PNG\r\n\x1a\n") { - png(bytes) - } else if bytes.starts_with(&[0xff, 0xd8]) { - jpeg(bytes) - } else if bytes.starts_with(b"II*\0") || bytes.starts_with(b"MM\0*") { - tiff(bytes) - } else if super::jxlread::is_jxl(bytes) { + if vectorcraft_doc::ppi::reads(bytes) { + return vectorcraft_doc::ppi::resolution(bytes); + } + let r = if super::jxlread::is_jxl(bytes) { jxl(bytes) - } else if bytes.len() >= 12 && &bytes[..4] == b"RIFF" && &bytes[8..12] == b"WEBP" { - webp(bytes) - } else if bytes.starts_with(b"BM") { - bmp(bytes) } else if super::psdread::is_psd(bytes) { super::psdread::resolution(bytes) } else if super::pcxread::is_pcx(bytes) { @@ -82,121 +78,9 @@ fn slice(b: &[u8], at: usize, n: usize) -> Option<&[u8]> { fn bytes(b: &[u8], at: usize) -> Option<[u8; N]> { slice(b, at, N)?.try_into().ok() } -fn be16(b: &[u8], at: usize) -> Option { - bytes(b, at).map(u16::from_be_bytes) -} fn be32(b: &[u8], at: usize) -> Option { bytes(b, at).map(u32::from_be_bytes) } -fn le32(b: &[u8], at: usize) -> Option { - bytes(b, at).map(u32::from_le_bytes) -} - -/// Pixels per inch from the whole pixels per metre PNG and BMP store: a whole number of ppi when -/// the metre value is that rounded (300 ppi is stored as 11811 per metre, 299.9994 ppi). -fn per_metre(ppm: u32) -> f64 { - let v = ppm as f64 * INCH_M; - let whole = v.round(); - if (v - whole).abs() <= INCH_M / 2.0 + 1e-9 { whole } else { v } -} - -/// PNG chunks after the signature: (type, data). -pub(super) fn png_chunks(b: &[u8]) -> impl Iterator { - let mut at = 8; - std::iter::from_fn(move || { - let len = be32(b, at)? as usize; - let ty = slice(b, at + 4, 4)?; - let data = slice(b, at + 8, len)?; - at = at.checked_add(12 + len)?; - Some((ty, data)) - }) -} - -fn png(b: &[u8]) -> Option<(f64, f64)> { - let (_, d) = png_chunks(b).take_while(|(ty, _)| *ty != b"IDAT").find(|(ty, _)| *ty == b"pHYs")?; - // Unit 1 = metre; 0 only gives the pixel aspect ratio. - (d.get(8) == Some(&1)).then(|| (per_metre(be32(d, 0).unwrap_or(0)), per_metre(be32(d, 4).unwrap_or(0)))) -} - -fn jpeg(b: &[u8]) -> Option<(f64, f64)> { - let (mut jfif, mut exif) = (None, None); - let mut at = 2; - while at + 4 <= b.len() && b[at] == 0xff { - let marker = b[at + 1]; - // 0xff fill bytes pad between segments. - if marker == 0xff { - at += 1; - continue; - } - if marker == 0xd8 || marker == 0x01 || (0xd0..=0xd7).contains(&marker) { - at += 2; - continue; - } - // Start of scan or end of image: no more metadata. - if marker == 0xda || marker == 0xd9 { - break; - } - let len = be16(b, at + 2)? as usize; - let seg = slice(b, at + 4, len.checked_sub(2)?)?; - match marker { - 0xe0 if seg.starts_with(b"JFIF\0") && seg.len() >= 12 => { - let (x, y) = (be16(seg, 8)? as f64, be16(seg, 10)? as f64); - jfif = match seg[7] { - 1 => Some((x, y)), - 2 => Some((x * INCH_CM, y * INCH_CM)), - _ => None, - }; - } - 0xe1 if seg.starts_with(b"Exif\0\0") => exif = tiff(&seg[6..]), - _ => {} - } - at += 2 + len; - } - jfif.or(exif) -} - -/// TIFF (or EXIF) IFD0 XResolution / YResolution / ResolutionUnit. -fn tiff(b: &[u8]) -> Option<(f64, f64)> { - let le = b.starts_with(b"II"); - let u16_at = |at: usize| bytes(b, at).map(if le { u16::from_le_bytes } else { u16::from_be_bytes }); - let u32_at = |at: usize| bytes(b, at).map(if le { u32::from_le_bytes } else { u32::from_be_bytes }); - let ifd = u32_at(4)? as usize; - let (mut x, mut y, mut unit) = (None, None, 2); - for i in 0..u16_at(ifd)? as usize { - let e = ifd.checked_add(2 + i * 12)?; - let rational = || -> Option { - let at = u32_at(e + 8)? as usize; - let (n, d) = (u32_at(at)?, u32_at(at.checked_add(4)?)?); - (d != 0).then(|| n as f64 / d as f64) - }; - match u16_at(e)? { - 282 => x = rational(), - 283 => y = rational(), - 296 => unit = u16_at(e + 8)?, - _ => {} - } - } - let k = match unit { - 2 => 1.0, - 3 => INCH_CM, - _ => return None, - }; - let x = x?; - Some((x * k, y.unwrap_or(x) * k)) -} - -fn webp(b: &[u8]) -> Option<(f64, f64)> { - let mut at = 12; - while at + 8 <= b.len() { - let len = le32(b, at + 4)? as usize; - let data = b.get(at + 8..at + 8 + len)?; - if &b[at..at + 4] == b"EXIF" { - return tiff(data.strip_prefix(b"Exif\0\0").unwrap_or(data)); - } - at += 8 + len + (len & 1); - } - None -} fn jxl(b: &[u8]) -> Option<(f64, f64)> { if !b.starts_with(&[0, 0, 0, 12, b'J', b'X', b'L', b' ', 13, 10, 135, 10]) { @@ -224,33 +108,6 @@ fn jxl(b: &[u8]) -> Option<(f64, f64)> { None } -fn bmp(b: &[u8]) -> Option<(f64, f64)> { - // BITMAPINFOHEADER (40 bytes or longer) at 14: pixels per metre at +24 and +28. - if le32(b, 14)? < 40 { - return None; - } - let ppm = |at| le32(b, at).filter(|v| (1..=i32::MAX as u32).contains(v)).map(per_metre); - Some((ppm(38)?, ppm(42)?)) -} - -/// `png` with a `pHYs` chunk declaring `ppi` right after the header (any existing one is replaced). -pub fn with_png_resolution(png: &[u8], (x, y): (f64, f64)) -> Vec { - let Some(head) = be32(png, 8).and_then(|len| slice(png, 0, 20usize.checked_add(len as usize)?)) else { return png.to_vec() }; - let chunk = vectorcraft_render::encode::png::phys_chunk(x, y); - let mut out = Vec::with_capacity(png.len() + chunk.len()); - out.extend_from_slice(head); - out.extend_from_slice(&chunk); - let mut at = head.len(); - while let Some(chunk) = be32(png, at).and_then(|len| slice(png, at, 12usize.checked_add(len as usize)?)) { - if chunk.get(4..8) != Some(b"pHYs") { - out.extend_from_slice(chunk); - } - at += chunk.len(); - } - out.extend_from_slice(png.get(at..).unwrap_or_default()); - out -} - #[cfg(test)] mod tests { use super::*; diff --git a/crates/render/src/encode/png.rs b/crates/render/src/encode/png.rs index 6a62034e10..d393e23fa7 100644 --- a/crates/render/src/encode/png.rs +++ b/crates/render/src/encode/png.rs @@ -28,16 +28,12 @@ const ADAM7: [(u32, u32, u32, u32); 7] = [(0, 0, 8, 8), (4, 0, 8, 8), (0, 4, 4, /// Pixels per metre for a resolution in pixels per inch (the unit `pHYs` stores). pub fn pixels_per_metre(ppi: f64) -> u32 { - (ppi / 0.0254).round().clamp(1.0, u32::MAX as f64) as u32 + vectorcraft_doc::ppi::pixels_per_metre(ppi) } /// A whole `pHYs` chunk (length, type, data, CRC) declaring `x` × `y` pixels per inch. pub fn phys_chunk(x: f64, y: f64) -> Vec { - let [x, y] = [x, y].map(|v| pixels_per_metre(v).to_be_bytes()); - let mut out = Vec::with_capacity(21); - // x, y, unit 1 = metre. - chunk(&mut out, b"pHYs", &[x.as_slice(), y.as_slice(), &[1]].concat()); - out + vectorcraft_doc::ppi::phys_chunk(x, y) } /// Encode straight-alpha RGBA8 pixels (`width`×`height`, row-major) as a PNG file. diff --git a/crates/svg/src/import/files.rs b/crates/svg/src/import/files.rs index d2b5504f9d..bae6bbd0be 100644 --- a/crates/svg/src/import/files.rs +++ b/crates/svg/src/import/files.rs @@ -615,10 +615,11 @@ fn raster(format: image::ImageFormat) -> Option { /// orientation ([`vectorcraft_doc::cmyk::exif_orientation`], read for PNG, JPEG, GIF, WebP and /// TIFF) leaves the pixels as stored. Otherwise the pixels are read ([`decoded`], /// [`tiff_pixels`]), turned or flipped as the orientation says, and written as PNG, so usvg sizes -/// and fits the image as it is shown. A CMYK TIFF also keeps its inks as a CMYK TIFF -/// ([`vectorcraft_doc::cmyk::stored_cmyk_tiff`]), and a CMYK JPEG or TIFF that is turned keeps them -/// turned ([`vectorcraft_doc::cmyk::turned_cmyk`]). `None` when the pixels can't be read, and for -/// a TIFF with more strips or tiles than the decoder can count +/// and fits the image as it is shown, with the resolution the file declares +/// ([`vectorcraft_doc::ppi::resolution`]), its axes swapped by a quarter turn. A CMYK TIFF also +/// keeps its inks as a CMYK TIFF ([`vectorcraft_doc::cmyk::stored_cmyk_tiff`]), and a CMYK JPEG +/// or TIFF that is turned keeps them turned ([`vectorcraft_doc::cmyk::turned_cmyk`]). `None` when +/// the pixels can't be read, and for a TIFF with more strips or tiles than the decoder can count /// ([`vectorcraft_doc::cmyk::oversized_tiff`]), before any decoder opens it. fn shown(bytes: Arc>, format: image::ImageFormat) -> Option { use image::ImageFormat as F; @@ -634,9 +635,13 @@ fn shown(bytes: Arc>, format: image::ImageFormat) -> Option { } let tiff = image::guess_format(&bytes).ok() == Some(F::Tiff); let mut img = if tiff { tiff_pixels(&bytes)? } else { decoded(&bytes, format)? }; + let ppi = vectorcraft_doc::ppi::resolution(&bytes); + // The resolution describes the stored pixels; a quarter turn swaps its axes. + let quarter = orientation.is_some_and(|o| o.to_exif() >= 5); + let shown_ppi = ppi.map(|(x, y)| if quarter { (y, x) } else { (x, y) }); let cmyk = match orientation { - Some(o) if matches!(format, F::Jpeg | F::Tiff) => vectorcraft_doc::cmyk::turned_cmyk(&bytes, o, None).map(|(tiff, _)| tiff), - None if tiff => vectorcraft_doc::cmyk::stored_cmyk_tiff(&bytes, None), + Some(o) if matches!(format, F::Jpeg | F::Tiff) => vectorcraft_doc::cmyk::turned_cmyk(&bytes, o, shown_ppi).map(|(tiff, _)| tiff), + None if tiff => vectorcraft_doc::cmyk::stored_cmyk_tiff(&bytes, ppi), _ => None, }; if let Some(orientation) = orientation { @@ -644,6 +649,9 @@ fn shown(bytes: Arc>, format: image::ImageFormat) -> Option { } let mut png = Vec::new(); img.write_to(&mut std::io::Cursor::new(&mut png), F::Png).ok()?; + if let Some(ppi) = shown_ppi { + png = vectorcraft_doc::ppi::with_png_resolution(&png, ppi); + } Some(Shown { kind: ImageKind::PNG, bytes: Arc::new(png), cmyk }) } diff --git a/crates/svg/tests/it/image_orientation.rs b/crates/svg/tests/it/image_orientation.rs index 549a40a3bf..d9de61290b 100644 --- a/crates/svg/tests/it/image_orientation.rs +++ b/crates/svg/tests/it/image_orientation.rs @@ -69,13 +69,21 @@ fn jpeg(orientation: Option) -> Vec { /// an Orientation tag (274) of `orientation`. Its RowsPerStrip (278) value is at byte [`ROWS_AT`] /// and its PlanarConfiguration (284, 1) value at [`PLANAR_AT`]. fn tiff(w: u32, h: u32, samples: u16, data: &[u8], orientation: u16) -> Vec { - let entries: u32 = if samples == 2 { 12 } else { 11 }; + tiff_at(w, h, samples, data, orientation, None) +} + +/// [`tiff`], declaring `ppi` (horizontal, vertical pixels per inch) when given, with XResolution +/// (282), YResolution (283) and ResolutionUnit (296, inches). +fn tiff_at(w: u32, h: u32, samples: u16, data: &[u8], orientation: u16, ppi: Option<(u32, u32)>) -> Vec { + let entries = 11 + u32::from(samples == 2) + 3 * u32::from(ppi.is_some()); let ifd_end = 8 + 2 + entries * 12 + 4; let bits_at = ifd_end; - let data_at = bits_at + if samples > 2 { 2 * u32::from(samples) } else { 0 }; + let ppi_at = bits_at + if samples > 2 { 2 * u32::from(samples) } else { 0 }; + let data_at = ppi_at + if ppi.is_some() { 16 } else { 0 }; let entry = |tag: u16, kind: u16, count: u32, value: u32| { [&tag.to_le_bytes()[..], &kind.to_le_bytes(), &count.to_le_bytes(), &value.to_le_bytes()].concat() }; + let resolution = |tags: [(u16, u16, u32); 2]| ppi.map(|_| tags.map(|(tag, kind, value)| entry(tag, kind, 1, value))).into_iter().flatten(); // Two SHORTs fit in the entry; more are written after the directory. let bits = match samples { 1 => 8, @@ -97,9 +105,11 @@ fn tiff(w: u32, h: u32, samples: u16, data: &[u8], orientation: u16) -> Vec entry(277, 3, 1, u32::from(samples)), entry(278, 4, 1, h), entry(279, 4, 1, u32::try_from(data.len()).unwrap()), - entry(284, 3, 1, 1), ] .into_iter() + .chain(resolution([(282, 5, ppi_at), (283, 5, ppi_at + 8)])) + .chain([entry(284, 3, 1, 1)]) + .chain(ppi.map(|_| entry(296, 3, 1, 2))) .chain(extra) { out.extend(e); @@ -110,6 +120,11 @@ fn tiff(w: u32, h: u32, samples: u16, data: &[u8], orientation: u16) -> Vec out.extend(8u16.to_le_bytes()); } } + if let Some((x, y)) = ppi { + for v in [x, 1, y, 1] { + out.extend(v.to_le_bytes()); + } + } assert_eq!(out.len(), data_at as usize); out.extend(data); out @@ -277,6 +292,76 @@ fn a_turned_jpeg_in_an_svg_image_imports_upright() { assert!(red(px.get_pixel(4, 3)) && !red(px.get_pixel(4, 12))); } +/// [`jpeg`] with its JFIF segment declaring `x` × `y` pixels per inch. +fn jpeg_at(orientation: Option, (x, y): (u16, u16)) -> Vec { + let mut jpg = jpeg(orientation); + let jfif = jpg.windows(5).position(|w| w == b"JFIF\0").unwrap(); + jpg[jfif + 7] = 1; + jpg[jfif + 8..jfif + 10].copy_from_slice(&x.to_be_bytes()); + jpg[jfif + 10..jfif + 12].copy_from_slice(&y.to_be_bytes()); + jpg +} + +/// The resolution the stored image of `im` declares. +fn stored_ppi(d: &Document, im: &ImageObject) -> Option<(f64, f64)> { + vectorcraft_doc::ppi::resolution(&d.images[&im.key].bytes) +} + +/// A raster that import writes again as PNG keeps the resolution its file declares, linked, as a +/// data URL and inside an SVG shown as an image: its axes swapped when the image turns a quarter, +/// in their order when it turns a half or is flipped. +#[test] +fn a_turned_raster_keeps_its_resolution() { + let dir = folder("ppi"); + for (orientation, ppi) in [(6, (150.0, 300.0)), (8, (150.0, 300.0)), (3, (300.0, 150.0)), (2, (300.0, 150.0))] { + let jpg = jpeg_at(Some(orientation), (300, 150)); + std::fs::write(dir.join("photo.jpg"), &jpg).unwrap(); + let data = format!("data:image/jpeg;base64,{}", base64_encode(&jpg)); + let inner = format!(r#""#); + let nested = format!("data:image/svg+xml;base64,{}", base64_encode(inner.as_bytes())); + for href in ["photo.jpg", data.as_str(), nested.as_str()] { + let (d, im) = import_one(&format!(r#""#), Some(&dir)); + let what = format!("orientation {orientation}, {}", &href[..href.len().min(20)]); + assert_eq!(d.images[&im.key].mime, "image/png", "{what}"); + assert_eq!(stored_ppi(&d, &im), Some(ppi), "{what}"); + } + } + let href = format!("data:image/tiff;base64,{}", base64_encode(&tiff_at(4, 2, 1, &[0, 0, 255, 255, 0, 0, 255, 255], 6, Some((300, 150))))); + let (d, im) = import_one(&format!(r#""#), None); + assert_eq!((im.width, im.height), (2, 4)); + assert_eq!(stored_ppi(&d, &im), Some((150.0, 300.0)), "a turned TIFF"); +} + +/// A TIFF that is not turned is written as PNG with the resolution it declares; a raster that +/// declares none is stored without one. +#[test] +fn an_upright_tiff_keeps_its_resolution() { + let gray = [0, 0, 255, 255, 0, 0, 255, 255]; + let href = format!("data:image/tiff;base64,{}", base64_encode(&tiff_at(4, 2, 1, &gray, 1, Some((300, 150))))); + let (d, im) = import_one(&format!(r#""#), None); + assert_eq!(d.images[&im.key].mime, "image/png"); + assert_eq!(stored_ppi(&d, &im), Some((300.0, 150.0))); + for bytes in [tiff(4, 2, 1, &gray, 1), jpeg(Some(6))] { + let href = format!("data:;base64,{}", base64_encode(&bytes)); + let (d, im) = import_one(&format!(r#""#), None); + assert_eq!(stored_ppi(&d, &im), None, "no resolution is made up"); + } +} + +/// A CMYK TIFF stored with its inks keeps its resolution, swapped when it turns a quarter. +#[test] +fn a_cmyk_tiff_keeps_its_resolution_turned_or_not() { + let (c, none) = ([255u8, 0, 0, 0], [0u8; 4]); + let data: Vec = [c, c, none, none, c, c, none, none].concat(); + for (orientation, ppi) in [(1, (300.0, 150.0)), (6, (150.0, 300.0))] { + let href = format!("data:image/tiff;base64,{}", base64_encode(&tiff_at(4, 2, 4, &data, orientation, Some((300, 150))))); + let (d, im) = import_one(&format!(r#""#), None); + assert_eq!(d.images[&im.key].mime, "image/tiff", "orientation {orientation}"); + assert!(d.images[&im.key].cmyk().is_some(), "orientation {orientation}: CMYK inks"); + assert_eq!(stored_ppi(&d, &im), Some(ppi), "orientation {orientation}"); + } +} + /// A turned JPEG that a linked SVG file links to is turned too. #[test] fn a_turned_jpeg_linked_from_a_linked_svg_file_imports_upright() { diff --git a/docs/target-app-parity.md b/docs/target-app-parity.md index ddcbb79f72..93e73ad397 100644 --- a/docs/target-app-parity.md +++ b/docs/target-app-parity.md @@ -1844,6 +1844,7 @@ Moved from the ROADMAP's "Shipped so far" on 2026-10-10. Update it when a featur - PDF export writes a CMYK image that keeps its numbers in an `ICCBased` color space holding, byte for byte, the CMYK profile the image carries (a CMYK TIFF's tag 34675, or a CMYK JPEG's `ICC_PROFILE` APP2 segments) when `output.profiles` is `all` or `taggedSource`, and in PDF/A-2b, PDF/X-3 and PDF/X-4 unless it is `destination` ([the help page](https://helpx.adobe.com/illustrator/desktop/save-and-export/export-files-to-different-formats/pdf-marks-bleeds-and-color-output-options.html)). This covers a CMYK JPEG embedded as it is, one the Compression settings resample or compress again, a TIFF written again, and an image written with its mask as its soft mask. With `none`, and in PDF/X-1a, the image is DeviceCMYK and a JPEG's own profile is left out, so PDF/X-1a export writes a CMYK JPEG that carries a profile. Pixels the Output settings convert are never tagged with the profile of their old numbers. A profile tags an image only when its size, signature, class, CMYK color space and tag table are well formed and it is at most 16 MiB (VectorCraft's own limit). Transparency in a tagged CMYK document blends in the document's CMYK profile; when the file holds no space of that profile it blends in DeviceCMYK, or keeps its RGB space in PDF/A, and never in an image's own profile (`crates/pdf/src/tests/cmykimages.rs`, `export::writer::tests`). - An image that Open and Place store again keeps the ICC profile its file embeds and its 16-bit samples. A turned PNG, JPEG, WebP or TIFF stored as PNG, and a TIFF the image decoder reads, keep their gray or RGB samples at 8 or 16 bits with their gray or RGB profile in the PNG's `iCCP` chunk ([PNG specification, iCCP](https://www.w3.org/TR/png-3/#11iCCP)). A CMYK TIFF stored with one plane per ink and a turned CMYK JPEG or TIFF keep their CMYK profile as TIFF tag 34675, a JPEG's put together from all its APP2 segments ([ICC image-format guide](https://github.com/InternationalColorConsortium/iccDEV/blob/master/iccdev-mcp/docs/icc-format-guide.md)). A profile whose color space differs from the stored samples' is left out. The profile is kept in the stored image; how CMYK inks are drawn and exported to PDF does not change (`crates/engine/src/cmd/fileio/tests.rs`). - SVG import shows a raster `` whose EXIF orientation or TIFF Orientation tag turns or flips it upright, linked or as a data URL, and inside an SVG shown as an image: the pixels are stored turned, the width and height swap, and the `` box fits the turned image under `preserveAspectRatio`, as CSS `image-orientation: from-image` describes (). A turned CMYK JPEG or TIFF keeps its inks as a CMYK TIFF, as does a CMYK TIFF that is not turned. TIFF data URLs (`image/tiff`, or untyped TIFF bytes) are read, and TIFFs with an alpha or other extra channel, CCITT Group 3 TIFFs and CMYK TIFFs stored one plane per ink are read as Open and Place read them. The pixels are decoded within the decode allowance, and a TIFF claiming more strips or tiles than the decoder can count is refused before it is decoded (`crates/svg/tests/it/image_orientation.rs`). + - An image that SVG import stores again, as PNG or as a CMYK TIFF, keeps the resolution its PNG, JPEG, TIFF, WebP or BMP file declares, as Open and Place keep it: in a PNG `pHYs` chunk () or the TIFF's resolution tags, with the horizontal and vertical values swapped when the image turns a quarter. An image that declares no resolution is stored without one (`a_turned_raster_keeps_its_resolution`, `an_upright_tiff_keeps_its_resolution` and `a_cmyk_tiff_keeps_its_resolution_turned_or_not` in `crates/svg/tests/it/image_orientation.rs`). - SVG import leaves out an SVG shown as an image that it can't read (text that isn't XML, an SVG without a size) and goes on with the images after it, as SVG 2's error processing describes (). Only images that decode count toward the 256 nested SVG images an SVG may show, so an image after many broken ones is imported. Every nested SVG image read, decoded or not, counts toward VectorCraft's own limits of 1,024 reads and 512 MB of text per import; past them, nested SVG images are left out with a warning (`crates/svg/tests/it/import_details.rs`). - SVG import reads the files that a linked SVG file's own `` elements link to, relative paths from that file's folder (), as it reads the imported SVG's links: a raster stays linked, an SVG file imports as vector art, and one that can't be read gets the placeholder, its link and a warning. SVG 2 tells a viewer not to load these links (); reading them is VectorCraft's own rule. The same href in SVG files of two folders names two files. A link that leads back to an SVG file showing it gets the placeholder and a warning, and reading the links of linked SVG files stops at the nesting depth cap and at 1,024 reads and 512 MB per import, the same limits decoded nested SVG images have, counted separately from them. The text of every linked SVG file read counts, and so does every file read for the links of a linked SVG file, raster or SVG; rasters the imported SVG links to don't. Once either limit is passed, no file linked from a linked SVG file is read and its image is left out with a warning, and an SVG file the imported SVG links to is still shown, without the files its own links name, with a warning. Another href to an SVG file already read, spelled differently or in another SVG file, does not read it again, unless a link back or the depth cap changes what it shows there (`crates/svg/tests/it/import_details.rs`, `crates/svg/src/import/files.rs`). - File › Place and a file dropped on the canvas refuse a PNG, JPEG, GIF or WebP file whose pixels can't be read, such as one cut short by a download or a sync still running, and the status bar shows "Couldn't place : the image's pixels can't be read". Paste and Relink refuse such an image too, and Update Links, or Open when it reads the file again, counts it as missing. A JPEG cut short still places when its pixels can be decoded (`file.place`, `crates/engine/src/tests_place.rs`).