mirror of
https://github.com/storytold/pdfcraft.git
synced 2026-11-16 07:42:39 +00:00
M2: a JPX image on a rotated page draws instead of crashing (#3884)
The decode-size hint `image_draw_size` hands hayro-jpeg2000 mapped the image's corners (w, 0) and (0, h) as points, so it took in the translation. On a /Rotate 90 page (Acrobat Paper Capture scans) one corner lands on the device origin, the hint gets a side of 0, and hayro-jpeg2000 0.3.5 divides the image size by it: "renderer crashed on page N: attempt to divide by zero" on every rotated JPX page. The hint now comes from the transform's linear part (the axis lengths the preview tiny-image check already used, now computed once), and each side is at least 1. As a side effect, translated JPX images no longer get an overestimated hint, so they can decode at a reduced resolution level when drawn small, which is what the hint is for. Regression test: a 1x1 JPX image filling a 40x40 page at /Rotate 0, 90, 180 and 270, rendered at 0.5x, 1x and 1.5x. It fails before the fix (/Rotate 90 at 0.5x: attempt to divide by zero) and passes after. Supersedes the guard in #1883 and does not depend on the hayro-jpeg2000 bump in #2309; both are open and conflicting with main. Fixes #3884 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01C19EMwwBCMSkqew7KepPV2
This commit is contained in:
3 files changed
+73
-34
No files matched your search
@@ -2673,6 +2673,26 @@ fn a_large_step_still_tiles_where_the_next_cell_shows() {
|
||||
/// no decomposition, reversible wavelet, and one empty packet per component. Empty
|
||||
/// coefficients decode to the midpoint (128) after the unsigned DC level shift.
|
||||
fn jpx_midpoint_pdf(bpc: u8, components: u16) -> Vec<u8> {
|
||||
let codestream = jpx_midpoint_codestream(components);
|
||||
let content = "q 20 0 0 20 5 5 cm /Im1 Do Q 1 0 0 rg 0 0 4 4 re f";
|
||||
let mut pdf = format!(
|
||||
"%PDF-1.7\n\
|
||||
1 0 obj << /Type /Catalog /Pages 2 0 R >> endobj\n\
|
||||
2 0 obj << /Type /Pages /Kids [3 0 R] /Count 1 >> endobj\n\
|
||||
3 0 obj << /Type /Page /Parent 2 0 R /MediaBox [0 0 40 40] /Contents 4 0 R /Resources << /XObject << /Im1 5 0 R >> >> >> endobj\n\
|
||||
4 0 obj << /Length {} >> stream\n{content}\nendstream endobj\n\
|
||||
5 0 obj << /Type /XObject /Subtype /Image /Width 1 /Height 1 /BitsPerComponent {bpc} /Filter /JPXDecode /Length {} >> stream\n",
|
||||
content.len(),
|
||||
codestream.len()
|
||||
)
|
||||
.into_bytes();
|
||||
pdf.extend_from_slice(&codestream);
|
||||
pdf.extend_from_slice(b"\nendstream endobj\ntrailer << /Root 1 0 R >>\n%%EOF");
|
||||
pdf
|
||||
}
|
||||
|
||||
/// [`jpx_midpoint_pdf`]'s one-pixel codestream on its own.
|
||||
fn jpx_midpoint_codestream(components: u16) -> Vec<u8> {
|
||||
let mut codestream = vec![0xff, 0x4f, 0xff, 0x51]; // SOC, SIZ.
|
||||
codestream.extend_from_slice(&(38 + 3 * components).to_be_bytes());
|
||||
codestream.extend_from_slice(&0_u16.to_be_bytes()); // Rsiz.
|
||||
@@ -2692,22 +2712,7 @@ fn jpx_midpoint_pdf(bpc: u8, components: u16) -> Vec<u8> {
|
||||
codestream.extend_from_slice(&[0, 1, 0xff, 0x93]); // One tile part, SOD.
|
||||
codestream.extend(std::iter::repeat_n(0, usize::from(components)));
|
||||
codestream.extend_from_slice(&[0xff, 0xd9]); // EOC.
|
||||
|
||||
let content = "q 20 0 0 20 5 5 cm /Im1 Do Q 1 0 0 rg 0 0 4 4 re f";
|
||||
let mut pdf = format!(
|
||||
"%PDF-1.7\n\
|
||||
1 0 obj << /Type /Catalog /Pages 2 0 R >> endobj\n\
|
||||
2 0 obj << /Type /Pages /Kids [3 0 R] /Count 1 >> endobj\n\
|
||||
3 0 obj << /Type /Page /Parent 2 0 R /MediaBox [0 0 40 40] /Contents 4 0 R /Resources << /XObject << /Im1 5 0 R >> >> >> endobj\n\
|
||||
4 0 obj << /Length {} >> stream\n{content}\nendstream endobj\n\
|
||||
5 0 obj << /Type /XObject /Subtype /Image /Width 1 /Height 1 /BitsPerComponent {bpc} /Filter /JPXDecode /Length {} >> stream\n",
|
||||
content.len(),
|
||||
codestream.len()
|
||||
)
|
||||
.into_bytes();
|
||||
pdf.extend_from_slice(&codestream);
|
||||
pdf.extend_from_slice(b"\nendstream endobj\ntrailer << /Root 1 0 R >>\n%%EOF");
|
||||
pdf
|
||||
codestream
|
||||
}
|
||||
|
||||
/// [`jpx_midpoint_pdf`]'s codestream (every sample 128) wrapped in a JP2 file whose header
|
||||
@@ -2875,6 +2880,39 @@ fn jpx_32_bit_image_does_not_prevent_other_page_content_from_rendering() {
|
||||
}
|
||||
}
|
||||
|
||||
/// #3884: a JPX image filling a `/Rotate 90` page (Acrobat Paper Capture scans) crashed the page
|
||||
/// with "attempt to divide by zero". The decode-size hint mapped the image's corners as points,
|
||||
/// so it took in the translation, and on a rotated page one corner lands on the device origin:
|
||||
/// a hint side of 0, which `hayro-jpeg2000` divides the image size by. The hint now comes from
|
||||
/// the transform's linear part and is never below one pixel.
|
||||
#[test]
|
||||
fn jpx_image_on_a_rotated_page_draws_instead_of_crashing() {
|
||||
let codestream = jpx_midpoint_codestream(1);
|
||||
for rotate in [0, 90, 180, 270] {
|
||||
let content = "q 40 0 0 40 0 0 cm /Im1 Do Q";
|
||||
let mut bytes = format!(
|
||||
"%PDF-1.7\n\
|
||||
1 0 obj << /Type /Catalog /Pages 2 0 R >> endobj\n\
|
||||
2 0 obj << /Type /Pages /Kids [3 0 R] /Count 1 >> endobj\n\
|
||||
3 0 obj << /Type /Page /Parent 2 0 R /MediaBox [0 0 40 40] /Rotate {rotate} /Contents 4 0 R /Resources << /XObject << /Im1 5 0 R >> >> >> endobj\n\
|
||||
4 0 obj << /Length {} >> stream\n{content}\nendstream endobj\n\
|
||||
5 0 obj << /Type /XObject /Subtype /Image /Width 1 /Height 1 /ColorSpace /DeviceGray /BitsPerComponent 8 /Filter /JPXDecode /Length {} >> stream\n",
|
||||
content.len(),
|
||||
codestream.len()
|
||||
)
|
||||
.into_bytes();
|
||||
bytes.extend_from_slice(&codestream);
|
||||
bytes.extend_from_slice(b"\nendstream endobj\ntrailer << /Root 1 0 R >>\n%%EOF");
|
||||
let mut renderer = PageRenderer::new(Arc::new(bytes), RenderConfig::default());
|
||||
for scale in [0.5, 1.0, 1.5] {
|
||||
let page = renderer.render(RenderRequest { page: 0, kind: RequestKind::Pixels, tile: None, scale, tag: 0, ..Default::default() });
|
||||
assert!(page.error.is_none(), "/Rotate {rotate} at {scale}x: {:?}", page.error);
|
||||
let centre = (page.height / 2 * page.width + page.width / 2) as usize * 4;
|
||||
assert_eq!(page.rgba.get(centre..centre + 4), Some(&[128, 128, 128, 255][..]), "/Rotate {rotate} at {scale}x paints the image");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// From the nightly `cargo xtask fuzz` (CI caps each child at 4 GiB): a stencil mask claiming
|
||||
/// /W 4294967295 and a CCITT image claiming /Columns 4294967295 each allocated 4 GiB while
|
||||
/// decoding (locally: 9.6 GB and 4.3 GB). Vendored hayro patches `image_size_ok` and
|
||||
|
||||
Vendored
+8
@@ -132,6 +132,14 @@ order the font lists them, platform ID first, so a wrong (1, 0) table (SAS 9.4's
|
||||
drew "8" as "p" and "+" as "V". Covered by `raster::tests::symbolic_truetype_cmap`, whose font
|
||||
is built in memory.
|
||||
|
||||
JPX decode-size hint on rotated pages (#3884): in `hayro` `Renderer::image_draw_size`, the
|
||||
size an image is decoded at is the length of each image axis on the device, taken from the
|
||||
transform's linear part, and each side is at least 1. It mapped the corners `(w, 0)` and
|
||||
`(0, h)` as points, which took in the translation, so on a `/Rotate 90` page (Acrobat Paper
|
||||
Capture scans) a corner on the device origin gave a side of 0 and `hayro-jpeg2000` 0.3.5
|
||||
divided the image size by it: "attempt to divide by zero" on every rotated page. Covered by
|
||||
`raster::tests::jpx_image_on_a_rotated_page_draws_instead_of_crashing`.
|
||||
|
||||
## KenM76 integration patches
|
||||
|
||||
| PR | Vendored change | Regression coverage |
|
||||
|
||||
Vendored
+11
-18
@@ -848,8 +848,10 @@ impl Renderer {
|
||||
/// `transform`, or `None` if `draw_image` skips it: off the canvas (PdfCraft patch: see
|
||||
/// `may_paint_rect`; checked before decoding, so a tile no longer decodes every image on its
|
||||
/// page), or, in a preview, covering only a few device pixels (PdfCraft patch; final renders
|
||||
/// always draw them). The tiny-image test measures axis lengths: the size returned maps a
|
||||
/// point, so it includes the translation and only suits the decode hint.
|
||||
/// always draw them). Both sides are the lengths of the image's axes on the device, from the
|
||||
/// transform's linear part (PdfCraft patch, #3884: mapping the corners as points took in the
|
||||
/// translation, so on a `/Rotate 90` page a corner on the device origin gave a side of 0,
|
||||
/// which `hayro-jpeg2000` divides the image size by); as the decode hint each is at least 1.
|
||||
fn image_draw_size(&self, width: u32, height: u32, transform: Affine) -> Option<(u32, u32)> {
|
||||
if !may_paint_rect(
|
||||
Rect::new(0.0, 0.0, f64::from(width), f64::from(height)),
|
||||
@@ -859,23 +861,14 @@ impl Renderer {
|
||||
) {
|
||||
return None;
|
||||
}
|
||||
let target_width = (transform * Point::new(width as f64, 0.0))
|
||||
.to_vec2()
|
||||
.length()
|
||||
.ceil() as u32;
|
||||
let target_height = (transform * Point::new(0.0, height as f64))
|
||||
.to_vec2()
|
||||
.length()
|
||||
.ceil() as u32;
|
||||
if self.preview {
|
||||
let c = transform.as_coeffs();
|
||||
let tiny_w = (f64::from(width) * (c[0] * c[0] + c[1] * c[1]).sqrt()).ceil() as u32;
|
||||
let tiny_h = (f64::from(height) * (c[2] * c[2] + c[3] * c[3]).sqrt()).ceil() as u32;
|
||||
if tiny_w.saturating_mul(tiny_h) < PREVIEW_MIN_IMAGE_AREA {
|
||||
return None;
|
||||
}
|
||||
let (x_axis, y_axis) = x_y_advances(&transform);
|
||||
// `as` saturates, and turns NaN into 0.
|
||||
let drawn_width = (f64::from(width) * x_axis.length()).ceil() as u32;
|
||||
let drawn_height = (f64::from(height) * y_axis.length()).ceil() as u32;
|
||||
if self.preview && drawn_width.saturating_mul(drawn_height) < PREVIEW_MIN_IMAGE_AREA {
|
||||
return None;
|
||||
}
|
||||
Some((target_width, target_height))
|
||||
Some((drawn_width.max(1), drawn_height.max(1)))
|
||||
}
|
||||
|
||||
fn draw_image(&mut self, image_data: ImageData, alpha_data: Option<LumaData>) {
|
||||
|
||||
Reference in new issue
Block a user