mirror of
https://github.com/storytold/pdfcraft.git
synced 2026-11-16 07:25:23 +00:00
address review: decode a CMYK JPEG past the XMP bound without its Extended XMP chunks
This commit is contained in:
@@ -340,9 +340,15 @@ fn jpeg_samples(data: &[u8], space: &Space, w: usize, h: usize) -> Option<(Vec<u
|
||||
/// a decoder converting to RGB inverts them, with YCCK converted to CMYK.
|
||||
fn cmyk_jpeg_samples(data: &[u8], w: usize, h: usize) -> Option<Vec<u8>> {
|
||||
use zune_jpeg::zune_core::{bytestream::ZCursor, colorspace::ColorSpace, options::DecoderOptions};
|
||||
if !crate::upright_image::decodable_metadata(data) {
|
||||
return None;
|
||||
}
|
||||
// Past the decoder's metadata bound, decode the image without its Extended XMP chunks
|
||||
// rather than not at all: written out as stored, a CMYK JPEG shows inverted colours.
|
||||
let stripped;
|
||||
let data = if crate::upright_image::decodable_metadata(data) {
|
||||
data
|
||||
} else {
|
||||
stripped = crate::upright_image::without_extended_xmp(data);
|
||||
&stripped[..]
|
||||
};
|
||||
let options = DecoderOptions::default().set_strict_mode(false).set_max_width(usize::from(u16::MAX)).set_max_height(usize::from(u16::MAX));
|
||||
let mut dec = zune_jpeg::JpegDecoder::new_with_options(ZCursor::new(data), options);
|
||||
dec.decode_headers().ok()?;
|
||||
|
||||
@@ -2630,3 +2630,30 @@ fn proportions_up_to_the_limit_scale_as_they_did() {
|
||||
let mask = noise(20, 10);
|
||||
assert_eq!(crate::extract::scaled(&mask, 20, 10), mask);
|
||||
}
|
||||
|
||||
/// A CMYK JPEG past the decoder's Extended XMP bound is still decoded (its chunks left out) and
|
||||
/// exported with the colours the page shows, not written as stored, which inverts them.
|
||||
#[test]
|
||||
fn a_cmyk_jpeg_flooded_with_extended_xmp_is_exported_in_its_colours() {
|
||||
let jpeg = crate::upright_image::tests::with_extended_xmp_chunks(&flat_four_component_jpeg([255, 0, 0, 0], 0), 5_000);
|
||||
let mut doc = from_images(&[("flat.png".into(), png_bytes(false))]).unwrap();
|
||||
with_image(&mut doc, |doc| {
|
||||
let mut d = image_dict(8, 8, "DeviceCMYK", 8);
|
||||
d.set(b"Filter".to_vec(), Object::name("DCTDecode"));
|
||||
let m = doc.add(Object::Stream(Stream::from_raw(image_dict(8, 8, "DeviceGray", 8), vec![255; 64])));
|
||||
d.set(b"SMask".to_vec(), Object::Ref(m));
|
||||
Stream::from_raw(d, jpeg.clone())
|
||||
});
|
||||
let (_, rgba) = exported_rgba(&doc);
|
||||
assert!(rgba.as_chunks::<4>().0.iter().all(|p| p[0] <= 6 && p[1] >= 249 && p[2] >= 249), "{:?}", &rgba[..4]);
|
||||
}
|
||||
|
||||
/// Stripping leaves everything but the Extended XMP chunks, byte for byte.
|
||||
#[test]
|
||||
fn stripping_extended_xmp_leaves_the_rest_of_the_jpeg() {
|
||||
let plain = flat_four_component_jpeg([255, 0, 0, 0], 0);
|
||||
let flooded = crate::upright_image::tests::with_extended_xmp_chunks(&plain, 3);
|
||||
assert_ne!(flooded, plain);
|
||||
// The helper puts a stray byte before the chunks, which the decoder skips: it stays.
|
||||
assert_eq!(crate::upright_image::without_extended_xmp(&flooded), [&plain[..2], b"\0".as_slice(), &plain[2..]].concat());
|
||||
}
|
||||
|
||||
@@ -74,8 +74,28 @@ pub(crate) fn decodable_metadata(bytes: &[u8]) -> bool {
|
||||
|
||||
/// How many Extended XMP chunks (APP1 segments) come before the image data.
|
||||
fn extended_xmp_chunks(bytes: &[u8]) -> usize {
|
||||
let mut chunks = 0usize;
|
||||
each_extended_xmp(bytes, |_| chunks += 1);
|
||||
chunks
|
||||
}
|
||||
|
||||
/// JPEG `bytes` without their Extended XMP chunks, which nothing here reads: what is left decodes
|
||||
/// in time linear in its size however many chunks the file carried.
|
||||
pub(crate) fn without_extended_xmp(bytes: &[u8]) -> Vec<u8> {
|
||||
let mut out = Vec::with_capacity(bytes.len());
|
||||
let mut at = 0;
|
||||
each_extended_xmp(bytes, |r| {
|
||||
out.extend_from_slice(bytes.get(at..r.start).unwrap_or_default());
|
||||
at = r.end;
|
||||
});
|
||||
out.extend_from_slice(bytes.get(at..).unwrap_or_default());
|
||||
out
|
||||
}
|
||||
|
||||
/// 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<usize>)) {
|
||||
const PREFIX: &[u8] = b"http://ns.adobe.com/xmp/extension/\0";
|
||||
let (mut i, mut chunks) = (2usize, 0usize);
|
||||
let mut i = 2usize;
|
||||
while let Some(&byte) = bytes.get(i) {
|
||||
// Stray bytes between segments, which the decoder skips too.
|
||||
if byte != 0xFF {
|
||||
@@ -93,14 +113,14 @@ fn extended_xmp_chunks(bytes: &[u8]) -> usize {
|
||||
_ => {
|
||||
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));
|
||||
if marker == 0xE1 && bytes.get(i.saturating_add(4)..).is_some_and(|seg| seg.starts_with(PREFIX)) {
|
||||
chunks += 1;
|
||||
f(i..end.min(bytes.len()));
|
||||
}
|
||||
i = i.saturating_add(2).saturating_add(len.max(2));
|
||||
i = end;
|
||||
}
|
||||
}
|
||||
}
|
||||
chunks
|
||||
}
|
||||
|
||||
/// The resolution in a JFIF header, if the file has one in inches or centimetres.
|
||||
|
||||
Reference in New Issue
Block a user