From cbcacac205726f0a0362904c49c5ef2551d19372 Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Thu, 19 Nov 2026 17:51:12 -0500 Subject: [PATCH] create: share JPEG and JP2 embedding probes without re-encoding (#7902) --- crates/create/src/extract.rs | 71 ++----- crates/create/src/image_probe.rs | 331 +++++++++++++++++++++++++++++ crates/create/src/lib.rs | 174 ++------------- crates/create/src/upright_image.rs | 142 ++++--------- 4 files changed, 399 insertions(+), 319 deletions(-) create mode 100644 crates/create/src/image_probe.rs diff --git a/crates/create/src/extract.rs b/crates/create/src/extract.rs index 0e66afd8c0..04a4c3ac24 100644 --- a/crates/create/src/extract.rs +++ b/crates/create/src/extract.rs @@ -319,35 +319,17 @@ fn jpeg_size(data: &[u8]) -> Option<(usize, usize)> { if !crate::upright_image::decodable_metadata(data) { return None; } - let mut i = 2usize; - if !data.starts_with(&[0xFF, 0xD8]) { - return None; - } - while let Some(&b) = data.get(i) { - if b != 0xFF { - i += 1; - continue; - } - let marker = *data.get(i + 1)?; - match marker { - 0xFF => i += 1, - 0x00 | 0x01 | 0xD0..=0xD7 => i += 2, - 0xDA | 0xD9 => break, - 0xC0..=0xC3 | 0xC5..=0xC7 | 0xC9..=0xCB | 0xCD..=0xCF => { - let h = usize::from(u16::from_be_bytes([*data.get(i + 5)?, *data.get(i + 6)?])); - let w = usize::from(u16::from_be_bytes([*data.get(i + 7)?, *data.get(i + 8)?])); - return Some((w, h)); - } - _ => { - let len = usize::from(u16::from_be_bytes([*data.get(i + 2)?, *data.get(i + 3)?])); - if len < 2 { - return None; - } - i = i.checked_add(2 + len)?; + let mut size = None; + let _ = crate::image_probe::walk_jpeg_segments(data, |marker, _span, seg| { + if crate::image_probe::is_sof_marker(marker) { + if let Some(&[_, h0, h1, w0, w1, ..]) = seg.get(..5) { + size = Some((usize::from(u16::from_be_bytes([w0, w1])), usize::from(u16::from_be_bytes([h0, h1])))); } + return Ok(crate::image_probe::JpegVisit::Stop); } - } - None + Ok(crate::image_probe::JpegVisit::Continue) + }); + size } /// JPEG `data`, decoded, if its header says it is `w` × `h`. @@ -428,34 +410,15 @@ fn jpeg_samples(data: &[u8], space: &Space, w: usize, h: usize) -> Option<(Vec Option { - if !data.starts_with(&[0xFF, 0xD8]) { - return None; - } - let mut i = 2usize; - while let Some(&b) = data.get(i) { - if b != 0xFF { - i += 1; - continue; + let mut transform = None; + let _ = crate::image_probe::walk_jpeg_segments(data, |marker, _span, seg| { + if marker == 0xEE && seg.starts_with(b"Adobe") && seg.len() >= 12 { + transform = Some(seg[11]); + return Ok(crate::image_probe::JpegVisit::Stop); } - let marker = *data.get(i + 1)?; - match marker { - 0xFF => i += 1, - 0x00 | 0x01 | 0xD0..=0xD7 => i += 2, - 0xDA | 0xD9 => break, - _ => { - let len = usize::from(u16::from_be_bytes([*data.get(i + 2)?, *data.get(i + 3)?])); - if len < 2 { - return None; - } - let seg = data.get(i + 4..i + 2 + len)?; - if marker == 0xEE && seg.starts_with(b"Adobe") && seg.len() >= 12 { - return Some(seg[11]); - } - i = i.checked_add(2 + len)?; - } - } - } - None + Ok(crate::image_probe::JpegVisit::Continue) + }); + transform } fn cmyk_jpeg_samples(data: &[u8], w: usize, h: usize) -> Option> { diff --git a/crates/create/src/image_probe.rs b/crates/create/src/image_probe.rs new file mode 100644 index 0000000000..6e41a55957 --- /dev/null +++ b/crates/create/src/image_probe.rs @@ -0,0 +1,331 @@ +//! Header-only JPEG and JPEG 2000 embedding probes without re-encoding. +//! +//! Used by image embedding (`lib.rs`), EXIF/XMP/JFIF inspection (`upright_image.rs`), and PDF +//! image extraction (`extract.rs`) so marker walking, density parsing, ICC reassembly, and +//! Adobe CMYK detection share one implementation. + +use std::ops::Range; + +/// Signature of a JP2 file box stream. +pub(crate) const JP2_SIGNATURE: &[u8] = &[0, 0, 0, 0x0C, b'j', b'P', b' ', b' ', 0x0D, 0x0A, 0x87, 0x0A]; + +/// Signature of a raw JPEG 2000 codestream (SOC + SIZ). +pub(crate) const J2K_CODESTREAM_SIGNATURE: &[u8] = &[0xFF, 0x4F, 0xFF, 0x51]; + +/// Whether `bytes` begins with a JP2 file signature or a raw JPEG 2000 codestream signature. +pub(crate) fn is_jpx(bytes: &[u8]) -> bool { + bytes.starts_with(JP2_SIGNATURE) || bytes.starts_with(J2K_CODESTREAM_SIGNATURE) +} + +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +pub(crate) enum JpegVisit { + Continue, + Stop, +} + +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +pub(crate) enum JpegProbeError { + NotJpeg, + TruncatedMarker, + TruncatedSegmentLength, + InvalidSegmentLength, + SegmentLengthOverflow, + TruncatedSegment, + BadFrameHeader, + NoImageSize, + UnsupportedComponents(u8), +} + +impl JpegProbeError { + pub(crate) fn message(self) -> String { + match self { + Self::NotJpeg => "not a JPEG file".into(), + Self::TruncatedMarker => "truncated JPEG marker".into(), + Self::TruncatedSegmentLength => "truncated JPEG segment length".into(), + Self::InvalidSegmentLength => "invalid JPEG segment length".into(), + Self::SegmentLengthOverflow => "JPEG segment length overflow".into(), + Self::TruncatedSegment => "truncated JPEG segment".into(), + Self::BadFrameHeader => "bad frame header".into(), + Self::NoImageSize => "no image size".into(), + Self::UnsupportedComponents(n) => format!("{n} colour components are not supported"), + } + } +} + +#[derive(Debug, Clone, PartialEq)] +pub(crate) struct JpegProbe { + pub width: u32, + pub height: u32, + pub components: u8, + pub color_space: &'static str, + pub invert_cmyk: bool, + pub dpi: (f64, f64), + pub icc: Option>, +} + +#[derive(Debug, Clone, Copy, PartialEq)] +pub(crate) struct JpxProbe { + pub width: u32, + pub height: u32, + pub dpi: (f64, f64), +} + +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +pub(crate) enum JpxProbeError { + MissingHeader, + MissingImageSize, + BadImageHeader, + DamagedCodestream, + UnusualImageSize, +} + +impl JpxProbeError { + pub(crate) fn message(self) -> &'static str { + match self { + Self::MissingHeader => "a JPEG 2000 file without a header", + Self::MissingImageSize => "a JPEG 2000 file without image size", + Self::BadImageHeader => "bad image header", + Self::DamagedCodestream => "a damaged JPEG 2000 codestream", + Self::UnusualImageSize => "unusual JPEG 2000 image size", + } + } +} + +pub(crate) fn be32(b: &[u8], at: usize) -> Option { + b.get(at..at + 4).map(|x| u32::from_be_bytes([x[0], x[1], x[2], x[3]])) +} + +pub(crate) fn be16(b: &[u8], at: usize) -> Option { + b.get(at..at + 2).map(|x| u16::from_be_bytes([x[0], x[1]])) +} + +/// Walk JPEG marker segments before SOS (`0xDA`) or EOI (`0xD9`). +/// +/// Calls `visit(marker, segment_span, payload)` for each marker segment with a length field, where +/// `segment_span` covers the `0xFF` marker byte through the end of the segment payload. +pub(crate) fn walk_jpeg_segments<'a>( + bytes: &'a [u8], + mut visit: impl FnMut(u8, Range, &'a [u8]) -> Result, +) -> Result<(), JpegProbeError> { + if bytes.len() < 4 || bytes[0] != 0xFF || bytes[1] != 0xD8 { + return Err(JpegProbeError::NotJpeg); + } + let mut i = 2usize; + while let Some(&byte) = bytes.get(i) { + if byte != 0xFF { + i += 1; + continue; + } + i += 1; + // Markers may be preceded by any number of 0xFF fill bytes. + while bytes.get(i) == Some(&0xFF) { + i += 1; + } + let marker_pos = i; + let marker = *bytes.get(i).ok_or(JpegProbeError::TruncatedMarker)?; + i += 1; + // Metadata ends at the first scan; entropy-coded bytes are not segments. + if marker == 0xDA || marker == 0xD9 { + break; + } + if marker == 0xD8 || marker == 0x01 || (0xD0..=0xD7).contains(&marker) { + continue; + } + let len = usize::from(be16(bytes, i).ok_or(JpegProbeError::TruncatedSegmentLength)?); + if len < 2 { + return Err(JpegProbeError::InvalidSegmentLength); + } + let end = i.checked_add(len).ok_or(JpegProbeError::SegmentLengthOverflow)?; + let seg = bytes.get(i + 2..end).ok_or(JpegProbeError::TruncatedSegment)?; + let span = marker_pos.saturating_sub(1)..end; + if matches!(visit(marker, span, seg)?, JpegVisit::Stop) { + break; + } + i = end; + } + Ok(()) +} + +/// Parse JFIF APP0 density `(x_dpi, y_dpi)` when unit is inches (1) or centimetres (2) and both +/// axes are positive. +pub(crate) fn jfif_dpi(seg: &[u8]) -> Option<(f64, f64)> { + if !seg.starts_with(b"JFIF\0") || seg.len() < 12 { + return None; + } + let (unit, x, y) = (seg[7], u16::from_be_bytes([seg[8], seg[9]]) as f64, u16::from_be_bytes([seg[10], seg[11]]) as f64); + if x > 0.0 && y > 0.0 { + match unit { + 1 => Some((x, y)), + 2 => Some((x * 2.54, y * 2.54)), + _ => None, + } + } else { + None + } +} + +/// A JPEG's ICC profile from its APP2 `ICC_PROFILE` chunks: (sequence number, chunk count, +/// data). `None` unless every chunk from 1 to the count is there exactly once. +fn jpeg_icc(mut chunks: Vec<(u8, u8, &[u8])>) -> Option> { + let count = chunks.first()?.1; + chunks.sort_by_key(|c| c.0); + let complete = usize::from(count) == chunks.len() && chunks.iter().zip(1..=count).all(|(c, seq)| c.0 == seq && c.1 == count); + complete.then(|| chunks.iter().flat_map(|c| c.2.iter().copied()).collect()) +} + +/// Whether `marker` is a JPEG SOFn frame marker (excluding DHT `0xC4`, JPG `0xC8`, DAC `0xCC`). +pub(crate) fn is_sof_marker(marker: u8) -> bool { + matches!(marker, 0xC0..=0xCF) && marker != 0xC4 && marker != 0xC8 && marker != 0xCC +} + +/// Probe a JPEG for PDF `/DCTDecode` embedding without decoding or re-encoding pixels. +pub(crate) fn probe_jpeg(bytes: &[u8]) -> Result { + let (mut size, mut comps, mut dpi, mut adobe) = (None, 0u8, (72.0, 72.0), false); + let mut icc_chunks = Vec::new(); + walk_jpeg_segments(bytes, |marker, _span, seg| { + match marker { + // APP0 JFIF density. + 0xE0 => { + if let Some(d) = jfif_dpi(seg) { + dpi = d; + } + } + 0xEE if seg.starts_with(b"Adobe") => adobe = true, + // APP2 ICC profile, possibly split over several chunks. + 0xE2 => { + if let Some(([seq, count], data)) = seg.strip_prefix(b"ICC_PROFILE\0").and_then(<[u8]>::split_first_chunk::<2>) { + icc_chunks.push((*seq, *count, data)); + } + } + _ if is_sof_marker(marker) && size.is_none() => { + if seg.len() < 6 { + return Err(JpegProbeError::BadFrameHeader); + } + size = Some((u16::from_be_bytes([seg[3], seg[4]]) as u32, u16::from_be_bytes([seg[1], seg[2]]) as u32)); + comps = seg[5]; + } + _ => {} + } + Ok(JpegVisit::Continue) + })?; + let (width, height) = size.filter(|(w, h)| *w > 0 && *h > 0).ok_or(JpegProbeError::NoImageSize)?; + let (color_space, invert_cmyk) = match comps { + 1 => ("DeviceGray", false), + 3 => ("DeviceRGB", false), + 4 => ("DeviceCMYK", adobe), + n => return Err(JpegProbeError::UnsupportedComponents(n)), + }; + let icc = jpeg_icc(icc_chunks); + Ok(JpegProbe { width, height, components: comps, color_space, invert_cmyk, dpi, icc }) +} + +/// The boxes of a JP2 box sequence: (type, payload). +fn jp2_boxes(b: &[u8]) -> Vec<([u8; 4], &[u8])> { + let mut out = Vec::new(); + let mut i = 0usize; + while i + 8 <= b.len() { + let len = be32(b, i).unwrap_or(0) as u64; + let ty = [b[i + 4], b[i + 5], b[i + 6], b[i + 7]]; + let (head, len) = match len { + 0 => (8, (b.len() - i) as u64), + 1 => match b.get(i + 8..i + 16) { + Some(x) => (16, u64::from_be_bytes([x[0], x[1], x[2], x[3], x[4], x[5], x[6], x[7]])), + None => break, + }, + n => (8, n), + }; + // `len as usize` would truncate on 32-bit targets and put the end before the payload. + let end = i.saturating_add(usize::try_from(len).unwrap_or(usize::MAX)).min(b.len()); + if len < head as u64 || end <= i { + break; + } + out.push((ty, &b[i + head..end])); + i = end; + } + out +} + +/// Probe a JPEG 2000 image (JP2 file or raw codestream) for PDF `/JPXDecode` embedding without +/// decoding or re-encoding pixels. +pub(crate) fn probe_jpx(bytes: &[u8]) -> Result { + let mut dpi = (72.0, 72.0); + let (width, height) = if bytes.starts_with(JP2_SIGNATURE) { + let header = jp2_boxes(bytes).into_iter().find(|(t, _)| t == b"jp2h").map(|(_, p)| p).ok_or(JpxProbeError::MissingHeader)?; + let inner = jp2_boxes(header); + let ihdr = inner.iter().find(|(t, _)| t == b"ihdr").map(|(_, p)| *p).ok_or(JpxProbeError::MissingImageSize)?; + // Capture resolution (pixels per metre), when given. + if let Some((_, res)) = inner.iter().find(|(t, _)| t == b"res ") + && let Some((_, r)) = jp2_boxes(res).into_iter().find(|(t, _)| t == b"resc" || t == b"resd") + && r.len() >= 10 + { + let part = |n: u16, d: u16, e: i8| if d == 0 { 0.0 } else { n as f64 / d as f64 * 10f64.powi(e as i32) * 0.0254 }; + let (v, hz) = ( + part(be16(r, 0).unwrap_or(0), be16(r, 2).unwrap_or(0), r[8] as i8), + part(be16(r, 4).unwrap_or(0), be16(r, 6).unwrap_or(0), r[9] as i8), + ); + if v > 1.0 && hz > 1.0 { + dpi = (hz, v); + } + } + (be32(ihdr, 4).ok_or(JpxProbeError::BadImageHeader)?, be32(ihdr, 0).ok_or(JpxProbeError::BadImageHeader)?) + } else { + // SIZ: Lsiz Rsiz Xsiz Ysiz XOsiz YOsiz … + let (x, y, xo, yo) = (be32(bytes, 8), be32(bytes, 12), be32(bytes, 16), be32(bytes, 20)); + match (x, y, xo, yo) { + (Some(x), Some(y), Some(xo), Some(yo)) if x > xo && y > yo => (x - xo, y - yo), + _ => return Err(JpxProbeError::DamagedCodestream), + } + }; + if width == 0 || height == 0 || width > 100_000 || height > 100_000 { + return Err(JpxProbeError::UnusualImageSize); + } + Ok(JpxProbe { width, height, dpi }) +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn jpeg_probe_preserves_error_diagnostics() { + assert_eq!(probe_jpeg(b"\x89PNG").unwrap_err(), JpegProbeError::NotJpeg); + assert_eq!(probe_jpeg(&[0xFF, 0xD8, 0xFF, 0xFF]).unwrap_err(), JpegProbeError::TruncatedMarker); + assert_eq!(probe_jpeg(&[0xFF, 0xD8, 0xFF, 0xE0]).unwrap_err(), JpegProbeError::TruncatedSegmentLength); + assert_eq!(probe_jpeg(&[0xFF, 0xD8, 0xFF, 0xE0, 0, 1]).unwrap_err(), JpegProbeError::InvalidSegmentLength); + assert_eq!(probe_jpeg(&[0xFF, 0xD8, 0xFF, 0xE0, 0, 5, 1]).unwrap_err(), JpegProbeError::TruncatedSegment); + assert_eq!(probe_jpeg(&[0xFF, 0xD8, 0xFF, 0xC0, 0, 4, 8, 0]).unwrap_err(), JpegProbeError::BadFrameHeader); + assert_eq!(probe_jpeg(&[0xFF, 0xD8, 0xFF, 0xD9]).unwrap_err(), JpegProbeError::NoImageSize); + let two_comp = [0xFF, 0xD8, 0xFF, 0xC0, 0, 11, 8, 0, 2, 0, 3, 2, 1, 0x11, 0, 2, 0x11, 0]; + assert_eq!(probe_jpeg(&two_comp).unwrap_err(), JpegProbeError::UnsupportedComponents(2)); + } + + #[test] + fn jpx_probe_preserves_bounds_and_diagnostics() { + let bx = |ty: &[u8], payload: &[u8]| { + let mut v = ((payload.len() + 8) as u32).to_be_bytes().to_vec(); + v.extend_from_slice(ty); + v.extend_from_slice(payload); + v + }; + assert_eq!(probe_jpx(JP2_SIGNATURE).unwrap_err(), JpxProbeError::MissingHeader); + let no_ihdr = [JP2_SIGNATURE.to_vec(), bx(b"jp2h", &[])].concat(); + assert_eq!(probe_jpx(&no_ihdr).unwrap_err(), JpxProbeError::MissingImageSize); + let bad_ihdr = [JP2_SIGNATURE.to_vec(), bx(b"jp2h", &bx(b"ihdr", &[0, 0, 0, 1]))].concat(); + assert_eq!(probe_jpx(&bad_ihdr).unwrap_err(), JpxProbeError::BadImageHeader); + assert_eq!(probe_jpx(&[0xFF, 0x4F, 0xFF, 0x51, 0, 41]).unwrap_err(), JpxProbeError::DamagedCodestream); + + let mut huge = vec![0xFF, 0x4F, 0xFF, 0x51, 0, 41, 0, 0]; + for v in [100_001u32, 10, 0, 0] { + huge.extend_from_slice(&v.to_be_bytes()); + } + assert_eq!(probe_jpx(&huge).unwrap_err(), JpxProbeError::UnusualImageSize); + + // Codestream with non-zero XOsiz/YOsiz offsets. + let mut offset_siz = vec![0xFF, 0x4F, 0xFF, 0x51, 0, 41, 0, 0]; + for v in [100u32, 80, 20, 10] { + offset_siz.extend_from_slice(&v.to_be_bytes()); + } + let p = probe_jpx(&offset_siz).unwrap(); + assert_eq!((p.width, p.height, p.dpi), (80, 70, (72.0, 72.0))); + } +} diff --git a/crates/create/src/lib.rs b/crates/create/src/lib.rs index ee40a15f7e..56a3767c1d 100644 --- a/crates/create/src/lib.rs +++ b/crates/create/src/lib.rs @@ -5,9 +5,13 @@ use pdfcraft_cos::{Dict, Document, ObjRef, Object, PdfString, Stream, serialize}; mod extract; +mod image_probe; mod text_font; mod upright_image; pub use extract::{ExtractedImage, ImageExport, extract_images, image_file, image_file_within}; +#[cfg(test)] +use image_probe::JP2_SIGNATURE; +use image_probe::{is_jpx, probe_jpeg, probe_jpx}; pub use upright_image::jpeg_exif_orientation; mod office; #[cfg(not(target_arch = "wasm32"))] @@ -150,15 +154,6 @@ fn icc_for(profile: Vec, n: usize) -> Option> { (profile.len() >= 128 && profile.get(36..40) == Some(b"acsp") && profile.get(16..20) == Some(space)).then_some(profile) } -/// A JPEG's ICC profile from its APP2 `ICC_PROFILE` chunks: (sequence number, chunk count, -/// data). `None` unless every chunk from 1 to the count is there exactly once. -fn jpeg_icc(mut chunks: Vec<(u8, u8, &[u8])>) -> Option> { - let count = chunks.first()?.1; - chunks.sort_by_key(|c| c.0); - let complete = usize::from(count) == chunks.len() && chunks.iter().zip(1..=count).all(|(c, seq)| c.0 == seq && c.1 == count); - complete.then(|| chunks.iter().flat_map(|c| c.2.iter().copied()).collect()) -} - /// The samples of a black-and-white image packed one bit per pixel (1 for white, each row /// starting on a byte): eight-bit grey (`n` 1) or RGB (`n` 3) whose every pixel is pure black or /// pure white, or one-bit grey, already packed (a bilevel TIFF). `None` for anything else, found @@ -266,164 +261,26 @@ fn add_image(doc: &mut Document, img: Embedded, profiles: &mut Vec<(Vec, Obj doc.add(Object::Stream(stream)) } -const JP2_SIGNATURE: &[u8] = &[0, 0, 0, 0x0C, b'j', b'P', b' ', b' ', 0x0D, 0x0A, 0x87, 0x0A]; - -fn be32(b: &[u8], at: usize) -> Option { - b.get(at..at + 4).map(|x| u32::from_be_bytes([x[0], x[1], x[2], x[3]])) -} - -fn be16(b: &[u8], at: usize) -> Option { - b.get(at..at + 2).map(|x| u16::from_be_bytes([x[0], x[1]])) -} - -/// The boxes of a JP2 box sequence: (type, payload). -fn jp2_boxes(b: &[u8]) -> Vec<([u8; 4], &[u8])> { - let mut out = Vec::new(); - let mut i = 0usize; - while i + 8 <= b.len() { - let len = be32(b, i).unwrap_or(0) as u64; - let ty = [b[i + 4], b[i + 5], b[i + 6], b[i + 7]]; - let (head, len) = match len { - 0 => (8, (b.len() - i) as u64), - 1 => match b.get(i + 8..i + 16) { - Some(x) => (16, u64::from_be_bytes([x[0], x[1], x[2], x[3], x[4], x[5], x[6], x[7]])), - None => break, - }, - n => (8, n), - }; - // `len as usize` would truncate on 32-bit targets and put the end before the payload. - let end = i.saturating_add(usize::try_from(len).unwrap_or(usize::MAX)).min(b.len()); - if len < head as u64 || end <= i { - break; - } - out.push((ty, &b[i + head..end])); - i = end; - } - out -} - /// A JPEG 2000 image (JP2 file or raw codestream), embedded as is (`/JPXDecode`; the colour /// space comes from the file). fn jpx(name: &str, bytes: &[u8]) -> Result { - let bad = |m: &str| CreateError::Image(name.into(), m.into()); - let mut dpi = (72.0, 72.0); - let (w, h) = if bytes.starts_with(JP2_SIGNATURE) { - let header = - jp2_boxes(bytes).into_iter().find(|(t, _)| t == b"jp2h").map(|(_, p)| p).ok_or_else(|| bad("a JPEG 2000 file without a header"))?; - let inner = jp2_boxes(header); - let ihdr = inner.iter().find(|(t, _)| t == b"ihdr").map(|(_, p)| *p).ok_or_else(|| bad("a JPEG 2000 file without image size"))?; - // Capture resolution (pixels per metre), when given. - if let Some((_, res)) = inner.iter().find(|(t, _)| t == b"res ") - && let Some((_, r)) = jp2_boxes(res).into_iter().find(|(t, _)| t == b"resc" || t == b"resd") - && r.len() >= 10 - { - let part = |n: u16, d: u16, e: i8| if d == 0 { 0.0 } else { n as f64 / d as f64 * 10f64.powi(e as i32) * 0.0254 }; - let (v, hz) = ( - part(be16(r, 0).unwrap_or(0), be16(r, 2).unwrap_or(0), r[8] as i8), - part(be16(r, 4).unwrap_or(0), be16(r, 6).unwrap_or(0), r[9] as i8), - ); - if v > 1.0 && hz > 1.0 { - dpi = (hz, v); - } - } - (be32(ihdr, 4).ok_or_else(|| bad("bad image header"))?, be32(ihdr, 0).ok_or_else(|| bad("bad image header"))?) - } else { - // SIZ: Lsiz Rsiz Xsiz Ysiz XOsiz YOsiz … - let (x, y, xo, yo) = (be32(bytes, 8), be32(bytes, 12), be32(bytes, 16), be32(bytes, 20)); - match (x, y, xo, yo) { - (Some(x), Some(y), Some(xo), Some(yo)) if x > xo && y > yo => (x - xo, y - yo), - _ => return Err(bad("a damaged JPEG 2000 codestream")), - } - }; - if w == 0 || h == 0 || w > 100_000 || h > 100_000 { - return Err(bad("unusual JPEG 2000 image size")); - } + let probe = probe_jpx(bytes).map_err(|e| CreateError::Image(name.into(), e.message().into()))?; let mut d = Dict::new(); d.set(b"Filter".to_vec(), Object::name("JPXDecode")); - Ok(Embedded { dict: d, data: bytes.to_vec(), filtered: true, smask: None, icc: None, px: (w, h), dpi }) + Ok(Embedded { dict: d, data: bytes.to_vec(), filtered: true, smask: None, icc: None, px: (probe.width, probe.height), dpi: probe.dpi }) } fn jpeg(name: &str, bytes: &[u8]) -> Result { - let bad = |m: &str| CreateError::Image(name.into(), m.into()); - if bytes.len() < 4 || bytes[0] != 0xFF || bytes[1] != 0xD8 { - return Err(bad("not a JPEG file")); - } - let (mut i, mut size, mut comps, mut dpi, mut adobe) = (2usize, None, 0u8, (72.0, 72.0), false); - let mut icc_chunks = Vec::new(); - while let Some(&byte) = bytes.get(i) { - if byte != 0xFF { - i += 1; - continue; - } - i += 1; - // Markers may be preceded by any number of 0xFF fill bytes. - while bytes.get(i) == Some(&0xFF) { - i += 1; - } - let marker = *bytes.get(i).ok_or_else(|| bad("truncated JPEG marker"))?; - i += 1; - // Metadata ends at the first scan; entropy-coded bytes are not segments. - if marker == 0xDA || marker == 0xD9 { - break; - } - if marker == 0xD8 || marker == 0x01 || (0xD0..=0xD7).contains(&marker) { - continue; - } - let len = usize::from(be16(bytes, i).ok_or_else(|| bad("truncated JPEG segment length"))?); - if len < 2 { - return Err(bad("invalid JPEG segment length")); - } - let end = i.checked_add(len).ok_or_else(|| bad("JPEG segment length overflow"))?; - let seg = bytes.get(i + 2..end).ok_or_else(|| bad("truncated JPEG segment"))?; - match marker { - // APP0 JFIF density. - 0xE0 if seg.starts_with(b"JFIF\0") && seg.len() >= 12 => { - let (unit, x, y) = (seg[7], u16::from_be_bytes([seg[8], seg[9]]) as f64, u16::from_be_bytes([seg[10], seg[11]]) as f64); - if x > 0.0 && y > 0.0 { - dpi = match unit { - 1 => (x, y), - 2 => (x * 2.54, y * 2.54), - _ => dpi, - }; - } - } - 0xEE if seg.starts_with(b"Adobe") => adobe = true, - // APP2 ICC profile, possibly split over several chunks. - 0xE2 => { - if let Some(([seq, count], data)) = seg.strip_prefix(b"ICC_PROFILE\0").and_then(<[u8]>::split_first_chunk::<2>) { - icc_chunks.push((*seq, *count, data)); - } - } - 0xC0..=0xCF if marker != 0xC4 && marker != 0xC8 && marker != 0xCC && size.is_none() => { - if seg.len() < 6 { - return Err(bad("bad frame header")); - } - size = Some((u16::from_be_bytes([seg[3], seg[4]]) as u32, u16::from_be_bytes([seg[1], seg[2]]) as u32)); - comps = seg[5]; - } - _ => {} - } - i = end; - } - let (w, h) = size.filter(|(w, h)| *w > 0 && *h > 0).ok_or_else(|| bad("no image size"))?; + let probe = probe_jpeg(bytes).map_err(|e| CreateError::Image(name.into(), e.message()))?; let mut d = Dict::new(); - let cs = match comps { - 1 => "DeviceGray", - 3 => "DeviceRGB", - 4 => { - if adobe { - // Adobe writes CMYK JPEGs inverted. - d.set(b"Decode".to_vec(), Object::Array([1, 0, 1, 0, 1, 0, 1, 0].iter().map(|v| Object::Int(*v)).collect())); - } - "DeviceCMYK" - } - n => return Err(bad(&format!("{n} colour components are not supported"))), - }; - d.set(b"ColorSpace".to_vec(), Object::name(cs)); + if probe.invert_cmyk { + // Adobe writes CMYK JPEGs inverted. + d.set(b"Decode".to_vec(), Object::Array([1, 0, 1, 0, 1, 0, 1, 0].iter().map(|v| Object::Int(*v)).collect())); + } + d.set(b"ColorSpace".to_vec(), Object::name(probe.color_space)); d.set(b"BitsPerComponent".to_vec(), Object::Int(8)); d.set(b"Filter".to_vec(), Object::name("DCTDecode")); - let icc = jpeg_icc(icc_chunks); - Ok(Embedded { dict: d, data: bytes.to_vec(), filtered: true, smask: None, icc, px: (w, h), dpi }) + Ok(Embedded { dict: d, data: bytes.to_vec(), filtered: true, smask: None, icc: probe.icc, px: (probe.width, probe.height), dpi: probe.dpi }) } fn png_image(name: &str, bytes: &[u8]) -> Result { @@ -769,8 +626,7 @@ fn is_image(bytes: &[u8]) -> bool { || is_gif(bytes) || is_pcx(bytes) // JPEG 2000: a JP2 file or a raw codestream. - || bytes.starts_with(JP2_SIGNATURE) - || bytes.starts_with(&[0xFF, 0x4F, 0xFF, 0x51]) + || is_jpx(bytes) // BMP: "BM" and a known header size (so text starting with "BM" stays text). || (bytes.starts_with(b"BM") && bytes.get(14..18).and_then(|h| <[u8; 4]>::try_from(h).ok()).is_some_and(|h| matches!(u32::from_le_bytes(h), 12 | 40 | 52 | 56 | 108 | 124))) @@ -808,7 +664,7 @@ fn embed(name: &str, bytes: &[u8], wanted: usize) -> Result, Creat Ok(vec![decoded(name, bytes, craft_image_codecs::Format::Gif)?]) } else if is_pcx(bytes) { Ok(vec![pcx(name, bytes)?]) - } else if bytes.starts_with(JP2_SIGNATURE) || bytes.starts_with(&[0xFF, 0x4F, 0xFF, 0x51]) { + } else if is_jpx(bytes) { Ok(vec![jpx(name, bytes)?]) } else { Err(CreateError::Image(name.into(), "use a PNG, JPEG, JPEG 2000, TIFF, GIF or BMP image".into())) diff --git a/crates/create/src/upright_image.rs b/crates/create/src/upright_image.rs index 9ebaea3706..9efaad9fd2 100644 --- a/crates/create/src/upright_image.rs +++ b/crates/create/src/upright_image.rs @@ -17,46 +17,28 @@ thread_local! { /// Reads the EXIF Orientation tag (1..=8) from APP1 Exif metadata in a JPEG, defaulting to 1. pub fn jpeg_exif_orientation(bytes: &[u8]) -> u16 { - let mut i = 2usize; - while let Some(&byte) = bytes.get(i) { - if byte != 0xFF { - i += 1; - continue; - } - let Some(&marker) = bytes.get(i.saturating_add(1)) else { break }; - match marker { - 0xFF => i += 1, - 0x00 | 0x01 | 0xD0..=0xD7 => i += 2, - 0xDA | 0xD9 => break, - _ => { - let Some(&[hi, lo]) = bytes.get(i.saturating_add(2)..i.saturating_add(4)) else { break }; - let len = usize::from(u16::from_be_bytes([hi, lo])); - if len < 2 { - break; + let mut orientation = 1; + let _ = crate::image_probe::walk_jpeg_segments(bytes, |marker, _span, seg| { + if marker == 0xE1 + && let Some(tiff) = seg.strip_prefix(b"Exif\0\0") + { + if let Some(hdr_order) = tiff.get(0..2) + && let Some(order) = match hdr_order { + b"II" => Some(craft_tiff::ByteOrder::Little), + b"MM" => Some(craft_tiff::ByteOrder::Big), + _ => None, } - let Some(seg) = bytes.get(i + 4..i + 2 + len) else { break }; - if marker == 0xE1 - && let Some(tiff) = seg.strip_prefix(b"Exif\0\0") - { - let Some(hdr_order) = tiff.get(0..2) else { return 1 }; - let order = match hdr_order { - b"II" => craft_tiff::ByteOrder::Little, - b"MM" => craft_tiff::ByteOrder::Big, - _ => return 1, - }; - let Some(off) = order.read_u32(tiff, 4) else { return 1 }; - if let Ok((ifd, _)) = craft_tiff::parse_ifd_at(tiff, u64::from(off), order, 0, false, &craft_tiff::ParseOptions::default()) - && let Some(o) = ifd.u16(craft_tiff::tags::ORIENTATION) - { - return o; - } - return 1; - } - i += 2 + len; + && let Some(off) = order.read_u32(tiff, 4) + && let Ok((ifd, _)) = craft_tiff::parse_ifd_at(tiff, u64::from(off), order, 0, false, &craft_tiff::ParseOptions::default()) + && let Some(o) = ifd.u16(craft_tiff::tags::ORIENTATION) + { + orientation = o; } + return Ok(crate::image_probe::JpegVisit::Stop); } - } - 1 + Ok(crate::image_probe::JpegVisit::Continue) + }); + orientation } /// A JPEG whose EXIF Orientation is not "as stored" has its pixels turned (and re-encoded); @@ -131,78 +113,26 @@ pub(crate) fn without_extended_xmp(bytes: &[u8]) -> Vec { /// Calls `f` with the byte range of each Extended XMP chunk (APP1 segment) before the image data. fn each_extended_xmp(bytes: &[u8], mut f: impl FnMut(std::ops::Range)) { const PREFIX: &[u8] = b"http://ns.adobe.com/xmp/extension/\0"; - let mut i = 2usize; - while let Some(&byte) = bytes.get(i) { - // Stray bytes between segments, which the decoder skips too. - if byte != 0xFF { - i += 1; - continue; + let _ = crate::image_probe::walk_jpeg_segments(bytes, |marker, span, seg| { + if marker == 0xE1 && seg.starts_with(PREFIX) { + f(span); } - let Some(&marker) = bytes.get(i.saturating_add(1)) else { break }; - match marker { - // Fill bytes before a marker. - 0xFF => i += 1, - // Markers without a segment. - 0x00 | 0x01 | 0xD0..=0xD7 => i += 2, - // The image data starts (or ends): no headers after it. - 0xDA | 0xD9 => break, - _ => { - let Some(&[hi, lo]) = bytes.get(i.saturating_add(2)..i.saturating_add(4)) else { break }; - let len = usize::from(u16::from_be_bytes([hi, lo])); - let end = i.saturating_add(2).saturating_add(len.max(2)); - // The prefix must start the APP1 segment's own payload: a zero-length header - // followed by prefix bytes is not a chunk. - if marker == 0xE1 && bytes.get(i.saturating_add(4)..end.min(bytes.len())).is_some_and(|seg| seg.starts_with(PREFIX)) { - f(i..end.min(bytes.len())); - } - i = end; - } - } - } -} - -#[derive(Clone, Copy, Debug, PartialEq, Eq)] -enum DensityUnit { - Inches, - Centimeters, -} - -#[derive(Clone, Copy, Debug, PartialEq, Eq)] -struct PixelDensity { - density: (u16, u16), - unit: DensityUnit, -} - -/// The resolution in a JFIF header, if the file has one in inches or centimetres. -fn jfif_density(bytes: &[u8]) -> Option { - let mut i = 2usize; - while let Some(&[0xFF, marker, hi, lo]) = bytes.get(i..i.saturating_add(4)) { - let len = usize::from(u16::from_be_bytes([hi, lo])); - let seg = bytes.get(i + 4..i + 2 + len)?; - if marker == 0xE0 && seg.starts_with(b"JFIF\0") { - let &[unit, x0, x1, y0, y1] = seg.get(7..12)? else { return None }; - let unit = match unit { - 1 => DensityUnit::Inches, - 2 => DensityUnit::Centimeters, - _ => return None, - }; - return Some(PixelDensity { density: (u16::from_be_bytes([x0, x1]), u16::from_be_bytes([y0, y1])), unit }); - } - if marker == 0xDA { - return None; - } - i += 2 + len; - } - None + Ok(crate::image_probe::JpegVisit::Continue) + }); } fn jfif_density_dpi(bytes: &[u8]) -> Option<(f32, f32)> { - let d = jfif_density(bytes)?; - let scale = match d.unit { - DensityUnit::Inches => 1.0, - DensityUnit::Centimeters => 2.54, - }; - Some((f32::from(d.density.0) * scale, f32::from(d.density.1) * scale)) + let mut dpi = None; + let _ = crate::image_probe::walk_jpeg_segments(bytes, |marker, _span, seg| { + if marker == 0xE0 + && let Some((x, y)) = crate::image_probe::jfif_dpi(seg) + { + dpi = Some((x as f32, y as f32)); + return Ok(crate::image_probe::JpegVisit::Stop); + } + Ok(crate::image_probe::JpegVisit::Continue) + }); + dpi } #[cfg(test)] @@ -217,7 +147,7 @@ pub(crate) mod tests { assert!(matches!(turned, Cow::Owned(_))); let decoded = craft_image_codecs::decode(&turned).unwrap(); assert_eq!((decoded.width(), decoded.height()), (20, 60)); - assert_eq!(jfif_density(&turned), Some(PixelDensity { density: (72, 144), unit: DensityUnit::Inches })); + assert_eq!(jfif_density_dpi(&turned), Some((72.0, 144.0))); let stored = phone_jpeg(1, 72); assert!(matches!(upright(&stored), Cow::Borrowed(_))); assert!(matches!(upright(b"\x89PNG\r\n\x1a\n"), Cow::Borrowed(_)));